]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- update from 4.4 branch
[packages/kernel.git] / kernel-aufs4.patch
1 aufs4.4 kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 6ce72d8..4aa31ea 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -221,6 +221,7 @@ source "fs/pstore/Kconfig"
8  source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/exofs/Kconfig"
11 +source "fs/aufs/Kconfig"
12  
13  endif # MISC_FILESYSTEMS
14  
15 diff --git a/fs/Makefile b/fs/Makefile
16 index 79f5225..a7c7f16 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 c2e5d6c..d736c11 100644
26 --- a/include/uapi/linux/Kbuild
27 +++ b/include/uapi/linux/Kbuild
28 @@ -59,6 +59,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 aufs4.4 base patch
37
38 diff --git a/MAINTAINERS b/MAINTAINERS
39 index 233f834..c250892 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -2029,6 +2029,19 @@ 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://github.com/sfjro/aufs4-linux.git
52 +S:     Supported
53 +F:     Documentation/filesystems/aufs/
54 +F:     Documentation/ABI/testing/debugfs-aufs
55 +F:     Documentation/ABI/testing/sysfs-aufs
56 +F:     fs/aufs/
57 +F:     include/uapi/linux/aufs_type.h
58 +
59  AUXILIARY DISPLAY DRIVERS
60  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
61  W:     http://miguelojeda.es/auxdisplay.htm
62 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
63 index 423f4ca..abfdd2b 100644
64 --- a/drivers/block/loop.c
65 +++ b/drivers/block/loop.c
66 @@ -706,6 +706,24 @@ static inline int is_loop_device(struct file *file)
67         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
68  }
69  
70 +/*
71 + * for AUFS
72 + * no get/put for file.
73 + */
74 +struct file *loop_backing_file(struct super_block *sb)
75 +{
76 +       struct file *ret;
77 +       struct loop_device *l;
78 +
79 +       ret = NULL;
80 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
81 +               l = sb->s_bdev->bd_disk->private_data;
82 +               ret = l->lo_backing_file;
83 +       }
84 +       return ret;
85 +}
86 +EXPORT_SYMBOL(loop_backing_file);
87 +
88  /* loop sysfs attributes */
89  
90  static ssize_t loop_attr_show(struct device *dev, char *page,
91 diff --git a/fs/dcache.c b/fs/dcache.c
92 index 5c33aeb..8aa7f26 100644
93 --- a/fs/dcache.c
94 +++ b/fs/dcache.c
95 @@ -1167,7 +1167,7 @@ enum d_walk_ret {
96   *
97   * The @enter() and @finish() callbacks are called with d_lock held.
98   */
99 -static void d_walk(struct dentry *parent, void *data,
100 +void d_walk(struct dentry *parent, void *data,
101                    enum d_walk_ret (*enter)(void *, struct dentry *),
102                    void (*finish)(void *))
103  {
104 diff --git a/fs/read_write.c b/fs/read_write.c
105 index 819ef3f..fd0414e 100644
106 --- a/fs/read_write.c
107 +++ b/fs/read_write.c
108 @@ -494,6 +494,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
109  }
110  EXPORT_SYMBOL(__vfs_write);
111  
112 +vfs_readf_t vfs_readf(struct file *file)
113 +{
114 +       const struct file_operations *fop = file->f_op;
115 +
116 +       if (fop->read)
117 +               return fop->read;
118 +       if (fop->read_iter)
119 +               return new_sync_read;
120 +       return ERR_PTR(-ENOSYS);
121 +}
122 +
123 +vfs_writef_t vfs_writef(struct file *file)
124 +{
125 +       const struct file_operations *fop = file->f_op;
126 +
127 +       if (fop->write)
128 +               return fop->write;
129 +       if (fop->write_iter)
130 +               return new_sync_write;
131 +       return ERR_PTR(-ENOSYS);
132 +}
133 +
134  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
135  {
136         mm_segment_t old_fs;
137 diff --git a/fs/splice.c b/fs/splice.c
138 index 4cf700d..30a091d 100644
139 --- a/fs/splice.c
140 +++ b/fs/splice.c
141 @@ -1110,8 +1110,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
142  /*
143   * Attempt to initiate a splice from pipe to file.
144   */
145 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
146 -                          loff_t *ppos, size_t len, unsigned int flags)
147 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
148 +                   loff_t *ppos, size_t len, unsigned int flags)
149  {
150         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
151                                 loff_t *, size_t, unsigned int);
152 @@ -1127,9 +1127,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
153  /*
154   * Attempt to initiate a splice from a file to a pipe.
155   */
156 -static long do_splice_to(struct file *in, loff_t *ppos,
157 -                        struct pipe_inode_info *pipe, size_t len,
158 -                        unsigned int flags)
159 +long do_splice_to(struct file *in, loff_t *ppos,
160 +                 struct pipe_inode_info *pipe, size_t len,
161 +                 unsigned int flags)
162  {
163         ssize_t (*splice_read)(struct file *, loff_t *,
164                                struct pipe_inode_info *, size_t, unsigned int);
165 diff --git a/include/linux/file.h b/include/linux/file.h
166 index f87d308..9a290b3 100644
167 --- a/include/linux/file.h
168 +++ b/include/linux/file.h
169 @@ -19,6 +19,7 @@ struct dentry;
170  struct path;
171  extern struct file *alloc_file(struct path *, fmode_t mode,
172         const struct file_operations *fop);
173 +extern struct file *get_empty_filp(void);
174  
175  static inline void fput_light(struct file *file, int fput_needed)
176  {
177 diff --git a/include/linux/fs.h b/include/linux/fs.h
178 index 3aa5142..8d48506 100644
179 --- a/include/linux/fs.h
180 +++ b/include/linux/fs.h
181 @@ -1672,6 +1672,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
182                               struct iovec *fast_pointer,
183                               struct iovec **ret_pointer);
184  
185 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
186 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
187 +                               loff_t *);
188 +vfs_readf_t vfs_readf(struct file *file);
189 +vfs_writef_t vfs_writef(struct file *file);
190 +
191  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
192  extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
193  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
194 diff --git a/include/linux/splice.h b/include/linux/splice.h
195 index da2751d..2e0fca6 100644
196 --- a/include/linux/splice.h
197 +++ b/include/linux/splice.h
198 @@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
199  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
200  
201  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
202 +
203 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
204 +                          loff_t *ppos, size_t len, unsigned int flags);
205 +extern long do_splice_to(struct file *in, loff_t *ppos,
206 +                        struct pipe_inode_info *pipe, size_t len,
207 +                        unsigned int flags);
208  #endif
209 aufs4.4 mmap patch
210
211 diff --git a/fs/proc/base.c b/fs/proc/base.c
212 index 4bd5d31..aa41f2a 100644
213 --- a/fs/proc/base.c
214 +++ b/fs/proc/base.c
215 @@ -1921,7 +1921,7 @@ static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
216         down_read(&mm->mmap_sem);
217         vma = find_exact_vma(mm, vm_start, vm_end);
218         if (vma && vma->vm_file) {
219 -               *path = vma->vm_file->f_path;
220 +               *path = vma_pr_or_file(vma)->f_path;
221                 path_get(path);
222                 rc = 0;
223         }
224 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
225 index f8595e8..cb8eda0 100644
226 --- a/fs/proc/nommu.c
227 +++ b/fs/proc/nommu.c
228 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
229         file = region->vm_file;
230  
231         if (file) {
232 -               struct inode *inode = file_inode(region->vm_file);
233 +               struct inode *inode;
234 +
235 +               file = vmr_pr_or_file(region);
236 +               inode = file_inode(file);
237                 dev = inode->i_sb->s_dev;
238                 ino = inode->i_ino;
239         }
240 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
241 index 187b3b5..e03793e 100644
242 --- a/fs/proc/task_mmu.c
243 +++ b/fs/proc/task_mmu.c
244 @@ -281,7 +281,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
245         const char *name = NULL;
246  
247         if (file) {
248 -               struct inode *inode = file_inode(vma->vm_file);
249 +               struct inode *inode;
250 +
251 +               file = vma_pr_or_file(vma);
252 +               inode = file_inode(file);
253                 dev = inode->i_sb->s_dev;
254                 ino = inode->i_ino;
255                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
256 @@ -1505,7 +1508,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
257         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
258         struct vm_area_struct *vma = v;
259         struct numa_maps *md = &numa_priv->md;
260 -       struct file *file = vma->vm_file;
261 +       struct file *file = vma_pr_or_file(vma);
262         struct mm_struct *mm = vma->vm_mm;
263         struct mm_walk walk = {
264                 .hugetlb_entry = gather_hugetlb_stats,
265 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
266 index e0d64c9..7aa92db 100644
267 --- a/fs/proc/task_nommu.c
268 +++ b/fs/proc/task_nommu.c
269 @@ -160,7 +160,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
270         file = vma->vm_file;
271  
272         if (file) {
273 -               struct inode *inode = file_inode(vma->vm_file);
274 +               struct inode *inode;
275 +
276 +               file = vma_pr_or_file(vma);
277 +               inode = file_inode(file);
278                 dev = inode->i_sb->s_dev;
279                 ino = inode->i_ino;
280                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
281 diff --git a/include/linux/mm.h b/include/linux/mm.h
282 index 00bad77..cc616b0 100644
283 --- a/include/linux/mm.h
284 +++ b/include/linux/mm.h
285 @@ -1183,6 +1183,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
286  }
287  #endif
288  
289 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
290 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
291 +                                     int);
292 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
293 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
294 +
295 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
296 +                                                               __LINE__)
297 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
298 +                                                         __LINE__)
299 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
300 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
301 +
302 +#ifndef CONFIG_MMU
303 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
304 +extern void vmr_do_fput(struct vm_region *, const char[], int);
305 +
306 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
307 +                                                         __LINE__)
308 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
309 +#endif /* !CONFIG_MMU */
310 +
311  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
312  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
313                 void *buf, int len, int write);
314 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
315 index f8d1492..c3a3760 100644
316 --- a/include/linux/mm_types.h
317 +++ b/include/linux/mm_types.h
318 @@ -272,6 +272,7 @@ struct vm_region {
319         unsigned long   vm_top;         /* region allocated to here */
320         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
321         struct file     *vm_file;       /* the backing file or NULL */
322 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
323  
324         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
325         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
326 @@ -346,6 +347,7 @@ struct vm_area_struct {
327         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
328                                            units, *not* PAGE_CACHE_SIZE */
329         struct file * vm_file;          /* File we map to (can be NULL). */
330 +       struct file *vm_prfile;         /* shadow of vm_file */
331         void * vm_private_data;         /* was vm_pte (shared mem) */
332  
333  #ifndef CONFIG_MMU
334 diff --git a/kernel/fork.c b/kernel/fork.c
335 index 1155eac..c001ea4 100644
336 --- a/kernel/fork.c
337 +++ b/kernel/fork.c
338 @@ -465,7 +465,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
339                         struct inode *inode = file_inode(file);
340                         struct address_space *mapping = file->f_mapping;
341  
342 -                       get_file(file);
343 +                       vma_get_file(tmp);
344                         if (tmp->vm_flags & VM_DENYWRITE)
345                                 atomic_dec(&inode->i_writecount);
346                         i_mmap_lock_write(mapping);
347 diff --git a/mm/Makefile b/mm/Makefile
348 index 2ed4319..e3a53f5 100644
349 --- a/mm/Makefile
350 +++ b/mm/Makefile
351 @@ -21,7 +21,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
352                            mm_init.o mmu_context.o percpu.o slab_common.o \
353                            compaction.o vmacache.o \
354                            interval_tree.o list_lru.o workingset.o \
355 -                          debug.o $(mmu-y)
356 +                          prfile.o debug.o $(mmu-y)
357  
358  obj-y += init-mm.o
359  
360 diff --git a/mm/filemap.c b/mm/filemap.c
361 index 1bb0076..8eaece8 100644
362 --- a/mm/filemap.c
363 +++ b/mm/filemap.c
364 @@ -2128,7 +2128,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
365         int ret = VM_FAULT_LOCKED;
366  
367         sb_start_pagefault(inode->i_sb);
368 -       file_update_time(vma->vm_file);
369 +       vma_file_update_time(vma);
370         lock_page(page);
371         if (page->mapping != inode->i_mapping) {
372                 unlock_page(page);
373 diff --git a/mm/memory.c b/mm/memory.c
374 index c387430..d434404 100644
375 --- a/mm/memory.c
376 +++ b/mm/memory.c
377 @@ -2035,7 +2035,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
378                 }
379  
380                 if (!page_mkwrite)
381 -                       file_update_time(vma->vm_file);
382 +                       vma_file_update_time(vma);
383         }
384  
385         return VM_FAULT_WRITE;
386 diff --git a/mm/mmap.c b/mm/mmap.c
387 index 2ce04a6..f555c0a 100644
388 --- a/mm/mmap.c
389 +++ b/mm/mmap.c
390 @@ -275,7 +275,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
391         if (vma->vm_ops && vma->vm_ops->close)
392                 vma->vm_ops->close(vma);
393         if (vma->vm_file)
394 -               fput(vma->vm_file);
395 +               vma_fput(vma);
396         mpol_put(vma_policy(vma));
397         kmem_cache_free(vm_area_cachep, vma);
398         return next;
399 @@ -887,7 +887,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
400         if (remove_next) {
401                 if (file) {
402                         uprobe_munmap(next, next->vm_start, next->vm_end);
403 -                       fput(file);
404 +                       vma_fput(vma);
405                 }
406                 if (next->anon_vma)
407                         anon_vma_merge(vma, next);
408 @@ -1681,8 +1681,8 @@ out:
409         return addr;
410  
411  unmap_and_free_vma:
412 +       vma_fput(vma);
413         vma->vm_file = NULL;
414 -       fput(file);
415  
416         /* Undo any partial mapping done by a device driver. */
417         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
418 @@ -2488,7 +2488,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
419                 goto out_free_mpol;
420  
421         if (new->vm_file)
422 -               get_file(new->vm_file);
423 +               vma_get_file(new);
424  
425         if (new->vm_ops && new->vm_ops->open)
426                 new->vm_ops->open(new);
427 @@ -2507,7 +2507,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
428         if (new->vm_ops && new->vm_ops->close)
429                 new->vm_ops->close(new);
430         if (new->vm_file)
431 -               fput(new->vm_file);
432 +               vma_fput(new);
433         unlink_anon_vmas(new);
434   out_free_mpol:
435         mpol_put(vma_policy(new));
436 @@ -2649,7 +2649,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
437         struct vm_area_struct *vma;
438         unsigned long populate = 0;
439         unsigned long ret = -EINVAL;
440 -       struct file *file;
441  
442         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
443                         "See Documentation/vm/remap_file_pages.txt.\n",
444 @@ -2693,10 +2692,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
445                 munlock_vma_pages_range(vma, start, start + size);
446         }
447  
448 -       file = get_file(vma->vm_file);
449 +       vma_get_file(vma);
450         ret = do_mmap_pgoff(vma->vm_file, start, size,
451                         prot, flags, pgoff, &populate);
452 -       fput(file);
453 +       vma_fput(vma);
454  out:
455         up_write(&mm->mmap_sem);
456         if (populate)
457 @@ -2966,7 +2965,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
458                 if (anon_vma_clone(new_vma, vma))
459                         goto out_free_mempol;
460                 if (new_vma->vm_file)
461 -                       get_file(new_vma->vm_file);
462 +                       vma_get_file(new_vma);
463                 if (new_vma->vm_ops && new_vma->vm_ops->open)
464                         new_vma->vm_ops->open(new_vma);
465                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
466 diff --git a/mm/nommu.c b/mm/nommu.c
467 index 92be862..29179f7 100644
468 --- a/mm/nommu.c
469 +++ b/mm/nommu.c
470 @@ -671,7 +671,7 @@ static void __put_nommu_region(struct vm_region *region)
471                 up_write(&nommu_region_sem);
472  
473                 if (region->vm_file)
474 -                       fput(region->vm_file);
475 +                       vmr_fput(region);
476  
477                 /* IO memory and memory shared directly out of the pagecache
478                  * from ramfs/tmpfs mustn't be released here */
479 @@ -829,7 +829,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
480         if (vma->vm_ops && vma->vm_ops->close)
481                 vma->vm_ops->close(vma);
482         if (vma->vm_file)
483 -               fput(vma->vm_file);
484 +               vma_fput(vma);
485         put_nommu_region(vma->vm_region);
486         kmem_cache_free(vm_area_cachep, vma);
487  }
488 @@ -1355,7 +1355,7 @@ unsigned long do_mmap(struct file *file,
489                                         goto error_just_free;
490                                 }
491                         }
492 -                       fput(region->vm_file);
493 +                       vmr_fput(region);
494                         kmem_cache_free(vm_region_jar, region);
495                         region = pregion;
496                         result = start;
497 @@ -1430,10 +1430,10 @@ error_just_free:
498         up_write(&nommu_region_sem);
499  error:
500         if (region->vm_file)
501 -               fput(region->vm_file);
502 +               vmr_fput(region);
503         kmem_cache_free(vm_region_jar, region);
504         if (vma->vm_file)
505 -               fput(vma->vm_file);
506 +               vma_fput(vma);
507         kmem_cache_free(vm_area_cachep, vma);
508         return ret;
509  
510 diff --git a/mm/prfile.c b/mm/prfile.c
511 new file mode 100644
512 index 0000000..b323b8a
513 --- /dev/null
514 +++ b/mm/prfile.c
515 @@ -0,0 +1,86 @@
516 +/*
517 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
518 + * in /proc/PID/maps.
519 + * Call these functions via macros defined in linux/mm.h.
520 + *
521 + * See Documentation/filesystems/aufs/design/06mmap.txt
522 + *
523 + * Copyright (c) 2014 Junjro R. Okajima
524 + * Copyright (c) 2014 Ian Campbell
525 + */
526 +
527 +#include <linux/mm.h>
528 +#include <linux/file.h>
529 +#include <linux/fs.h>
530 +
531 +/* #define PRFILE_TRACE */
532 +static inline void prfile_trace(struct file *f, struct file *pr,
533 +                             const char func[], int line, const char func2[])
534 +{
535 +#ifdef PRFILE_TRACE
536 +       if (pr)
537 +               pr_info("%s:%d: %s, %s\n", func, line, func2,
538 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
539 +#endif
540 +}
541 +
542 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
543 +                            int line)
544 +{
545 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
546 +
547 +       prfile_trace(f, pr, func, line, __func__);
548 +       file_update_time(f);
549 +       if (f && pr)
550 +               file_update_time(pr);
551 +}
552 +
553 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
554 +                              int line)
555 +{
556 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
557 +
558 +       prfile_trace(f, pr, func, line, __func__);
559 +       return (f && pr) ? pr : f;
560 +}
561 +
562 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
563 +{
564 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
565 +
566 +       prfile_trace(f, pr, func, line, __func__);
567 +       get_file(f);
568 +       if (f && pr)
569 +               get_file(pr);
570 +}
571 +
572 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
573 +{
574 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
575 +
576 +       prfile_trace(f, pr, func, line, __func__);
577 +       fput(f);
578 +       if (f && pr)
579 +               fput(pr);
580 +}
581 +
582 +#ifndef CONFIG_MMU
583 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
584 +                              int line)
585 +{
586 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
587 +
588 +       prfile_trace(f, pr, func, line, __func__);
589 +       return (f && pr) ? pr : f;
590 +}
591 +
592 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
593 +{
594 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
595 +
596 +       prfile_trace(f, pr, func, line, __func__);
597 +       fput(f);
598 +       if (f && pr)
599 +               fput(pr);
600 +}
601 +#endif /* !CONFIG_MMU */
602 aufs4.4 standalone patch
603
604 diff --git a/fs/dcache.c b/fs/dcache.c
605 index 8aa7f26..f997345 100644
606 --- a/fs/dcache.c
607 +++ b/fs/dcache.c
608 @@ -1272,6 +1272,7 @@ rename_retry:
609         seq = 1;
610         goto again;
611  }
612 +EXPORT_SYMBOL(d_walk);
613  
614  /*
615   * Search for at least 1 mount point in the dentry's subdirs.
616 diff --git a/fs/exec.c b/fs/exec.c
617 index b06623a..b9206c5 100644
618 --- a/fs/exec.c
619 +++ b/fs/exec.c
620 @@ -103,6 +103,7 @@ bool path_noexec(const struct path *path)
621         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
622                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
623  }
624 +EXPORT_SYMBOL(path_noexec);
625  
626  #ifdef CONFIG_USELIB
627  /*
628 diff --git a/fs/file_table.c b/fs/file_table.c
629 index ad17e05..df66450 100644
630 --- a/fs/file_table.c
631 +++ b/fs/file_table.c
632 @@ -147,6 +147,7 @@ over:
633         }
634         return ERR_PTR(-ENFILE);
635  }
636 +EXPORT_SYMBOL(get_empty_filp);
637  
638  /**
639   * alloc_file - allocate and initialize a 'struct file'
640 @@ -308,6 +309,7 @@ void put_filp(struct file *file)
641                 file_free(file);
642         }
643  }
644 +EXPORT_SYMBOL(put_filp);
645  
646  void __init files_init(void)
647  { 
648 diff --git a/fs/namespace.c b/fs/namespace.c
649 index 0570729..ec560d8 100644
650 --- a/fs/namespace.c
651 +++ b/fs/namespace.c
652 @@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
653         mnt_dec_writers(real_mount(mnt));
654         preempt_enable();
655  }
656 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
657  
658  /**
659   * mnt_drop_write - give up write access to a mount
660 @@ -1803,6 +1804,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
661         }
662         return 0;
663  }
664 +EXPORT_SYMBOL(iterate_mounts);
665  
666  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
667  {
668 diff --git a/fs/notify/group.c b/fs/notify/group.c
669 index d16b62c..06ca6bc 100644
670 --- a/fs/notify/group.c
671 +++ b/fs/notify/group.c
672 @@ -22,6 +22,7 @@
673  #include <linux/srcu.h>
674  #include <linux/rculist.h>
675  #include <linux/wait.h>
676 +#include <linux/module.h>
677  
678  #include <linux/fsnotify_backend.h>
679  #include "fsnotify.h"
680 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
681  {
682         atomic_inc(&group->refcnt);
683  }
684 +EXPORT_SYMBOL(fsnotify_get_group);
685  
686  /*
687   * Drop a reference to a group.  Free it if it's through.
688 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
689         if (atomic_dec_and_test(&group->refcnt))
690                 fsnotify_final_destroy_group(group);
691  }
692 +EXPORT_SYMBOL(fsnotify_put_group);
693  
694  /*
695   * Create a new fsnotify_group and hold a reference for the group returned.
696 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
697  
698         return group;
699  }
700 +EXPORT_SYMBOL(fsnotify_alloc_group);
701  
702  int fsnotify_fasync(int fd, struct file *file, int on)
703  {
704 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
705 index fc0df44..325b5c6 100644
706 --- a/fs/notify/mark.c
707 +++ b/fs/notify/mark.c
708 @@ -109,6 +109,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
709                 mark->free_mark(mark);
710         }
711  }
712 +EXPORT_SYMBOL(fsnotify_put_mark);
713  
714  /* Calculate mask of events for a list of marks */
715  u32 fsnotify_recalc_mask(struct hlist_head *head)
716 @@ -208,6 +209,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
717         mutex_unlock(&group->mark_mutex);
718         fsnotify_free_mark(mark);
719  }
720 +EXPORT_SYMBOL(fsnotify_destroy_mark);
721  
722  void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
723  {
724 @@ -392,6 +394,7 @@ err:
725  
726         return ret;
727  }
728 +EXPORT_SYMBOL(fsnotify_add_mark);
729  
730  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
731                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
732 @@ -492,6 +495,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
733         atomic_set(&mark->refcnt, 1);
734         mark->free_mark = free_mark;
735  }
736 +EXPORT_SYMBOL(fsnotify_init_mark);
737  
738  static int fsnotify_mark_destroy(void *ignored)
739  {
740 diff --git a/fs/open.c b/fs/open.c
741 index b6f1e96..4ab0d4e 100644
742 --- a/fs/open.c
743 +++ b/fs/open.c
744 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
745         mutex_unlock(&dentry->d_inode->i_mutex);
746         return ret;
747  }
748 +EXPORT_SYMBOL(do_truncate);
749  
750  long vfs_truncate(struct path *path, loff_t length)
751  {
752 @@ -678,6 +679,7 @@ int open_check_o_direct(struct file *f)
753         }
754         return 0;
755  }
756 +EXPORT_SYMBOL(open_check_o_direct);
757  
758  static int do_dentry_open(struct file *f,
759                           struct inode *inode,
760 diff --git a/fs/read_write.c b/fs/read_write.c
761 index fd0414e..8ace6ec 100644
762 --- a/fs/read_write.c
763 +++ b/fs/read_write.c
764 @@ -504,6 +504,7 @@ vfs_readf_t vfs_readf(struct file *file)
765                 return new_sync_read;
766         return ERR_PTR(-ENOSYS);
767  }
768 +EXPORT_SYMBOL(vfs_readf);
769  
770  vfs_writef_t vfs_writef(struct file *file)
771  {
772 @@ -515,6 +516,7 @@ vfs_writef_t vfs_writef(struct file *file)
773                 return new_sync_write;
774         return ERR_PTR(-ENOSYS);
775  }
776 +EXPORT_SYMBOL(vfs_writef);
777  
778  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
779  {
780 diff --git a/fs/splice.c b/fs/splice.c
781 index 30a091d..c37c311 100644
782 --- a/fs/splice.c
783 +++ b/fs/splice.c
784 @@ -1123,6 +1123,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
785  
786         return splice_write(pipe, out, ppos, len, flags);
787  }
788 +EXPORT_SYMBOL(do_splice_from);
789  
790  /*
791   * Attempt to initiate a splice from a file to a pipe.
792 @@ -1149,6 +1150,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
793  
794         return splice_read(in, ppos, pipe, len, flags);
795  }
796 +EXPORT_SYMBOL(do_splice_to);
797  
798  /**
799   * splice_direct_to_actor - splices data directly between two non-pipes
800 diff --git a/fs/xattr.c b/fs/xattr.c
801 index 9b932b9..44c457a 100644
802 --- a/fs/xattr.c
803 +++ b/fs/xattr.c
804 @@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
805         *xattr_value = value;
806         return error;
807  }
808 +EXPORT_SYMBOL(vfs_getxattr_alloc);
809  
810  /* Compare an extended attribute value with the given value */
811  int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
812 diff --git a/security/commoncap.c b/security/commoncap.c
813 index 1832cf7..987ff5f 100644
814 --- a/security/commoncap.c
815 +++ b/security/commoncap.c
816 @@ -1053,12 +1053,14 @@ int cap_mmap_addr(unsigned long addr)
817         }
818         return ret;
819  }
820 +EXPORT_SYMBOL(cap_mmap_addr);
821  
822  int cap_mmap_file(struct file *file, unsigned long reqprot,
823                   unsigned long prot, unsigned long flags)
824  {
825         return 0;
826  }
827 +EXPORT_SYMBOL(cap_mmap_file);
828  
829  #ifdef CONFIG_SECURITY
830  
831 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
832 index 03c1652..b00aa76 100644
833 --- a/security/device_cgroup.c
834 +++ b/security/device_cgroup.c
835 @@ -7,6 +7,7 @@
836  #include <linux/device_cgroup.h>
837  #include <linux/cgroup.h>
838  #include <linux/ctype.h>
839 +#include <linux/export.h>
840  #include <linux/list.h>
841  #include <linux/uaccess.h>
842  #include <linux/seq_file.h>
843 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
844         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
845                         access);
846  }
847 +EXPORT_SYMBOL(__devcgroup_inode_permission);
848  
849  int devcgroup_inode_mknod(int mode, dev_t dev)
850  {
851 diff --git a/security/security.c b/security/security.c
852 index 46f405c..54488b0 100644
853 --- a/security/security.c
854 +++ b/security/security.c
855 @@ -433,6 +433,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
856                 return 0;
857         return call_int_hook(path_rmdir, 0, dir, dentry);
858  }
859 +EXPORT_SYMBOL(security_path_rmdir);
860  
861  int security_path_unlink(struct path *dir, struct dentry *dentry)
862  {
863 @@ -449,6 +450,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
864                 return 0;
865         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
866  }
867 +EXPORT_SYMBOL(security_path_symlink);
868  
869  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
870                        struct dentry *new_dentry)
871 @@ -457,6 +459,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
872                 return 0;
873         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
874  }
875 +EXPORT_SYMBOL(security_path_link);
876  
877  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
878                          struct path *new_dir, struct dentry *new_dentry,
879 @@ -484,6 +487,7 @@ int security_path_truncate(struct path *path)
880                 return 0;
881         return call_int_hook(path_truncate, 0, path);
882  }
883 +EXPORT_SYMBOL(security_path_truncate);
884  
885  int security_path_chmod(struct path *path, umode_t mode)
886  {
887 @@ -491,6 +495,7 @@ int security_path_chmod(struct path *path, umode_t mode)
888                 return 0;
889         return call_int_hook(path_chmod, 0, path, mode);
890  }
891 +EXPORT_SYMBOL(security_path_chmod);
892  
893  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
894  {
895 @@ -498,6 +503,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
896                 return 0;
897         return call_int_hook(path_chown, 0, path, uid, gid);
898  }
899 +EXPORT_SYMBOL(security_path_chown);
900  
901  int security_path_chroot(struct path *path)
902  {
903 @@ -583,6 +589,7 @@ int security_inode_readlink(struct dentry *dentry)
904                 return 0;
905         return call_int_hook(inode_readlink, 0, dentry);
906  }
907 +EXPORT_SYMBOL(security_inode_readlink);
908  
909  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
910                                bool rcu)
911 @@ -598,6 +605,7 @@ int security_inode_permission(struct inode *inode, int mask)
912                 return 0;
913         return call_int_hook(inode_permission, 0, inode, mask);
914  }
915 +EXPORT_SYMBOL(security_inode_permission);
916  
917  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
918  {
919 @@ -736,6 +744,7 @@ int security_file_permission(struct file *file, int mask)
920  
921         return fsnotify_perm(file, mask);
922  }
923 +EXPORT_SYMBOL(security_file_permission);
924  
925  int security_file_alloc(struct file *file)
926  {
927 @@ -795,6 +804,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
928                 return ret;
929         return ima_file_mmap(file, prot);
930  }
931 +EXPORT_SYMBOL(security_mmap_file);
932  
933  int security_mmap_addr(unsigned long addr)
934  {
935 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
936 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
937 +++ linux/Documentation/ABI/testing/debugfs-aufs        2016-01-13 20:11:11.663093444 +0100
938 @@ -0,0 +1,50 @@
939 +What:          /debug/aufs/si_<id>/
940 +Date:          March 2009
941 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
942 +Description:
943 +               Under /debug/aufs, a directory named si_<id> is created
944 +               per aufs mount, where <id> is a unique id generated
945 +               internally.
946 +
947 +What:          /debug/aufs/si_<id>/plink
948 +Date:          Apr 2013
949 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
950 +Description:
951 +               It has three lines and shows the information about the
952 +               pseudo-link. The first line is a single number
953 +               representing a number of buckets. The second line is a
954 +               number of pseudo-links per buckets (separated by a
955 +               blank). The last line is a single number representing a
956 +               total number of psedo-links.
957 +               When the aufs mount option 'noplink' is specified, it
958 +               will show "1\n0\n0\n".
959 +
960 +What:          /debug/aufs/si_<id>/xib
961 +Date:          March 2009
962 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
963 +Description:
964 +               It shows the consumed blocks by xib (External Inode Number
965 +               Bitmap), its block size and file size.
966 +               When the aufs mount option 'noxino' is specified, it
967 +               will be empty. About XINO files, see the aufs manual.
968 +
969 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
970 +Date:          March 2009
971 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
972 +Description:
973 +               It shows the consumed blocks by xino (External Inode Number
974 +               Translation Table), its link count, block size and file
975 +               size.
976 +               When the aufs mount option 'noxino' is specified, it
977 +               will be empty. About XINO files, see the aufs manual.
978 +
979 +What:          /debug/aufs/si_<id>/xigen
980 +Date:          March 2009
981 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
982 +Description:
983 +               It shows the consumed blocks by xigen (External Inode
984 +               Generation Table), its block size and file size.
985 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
986 +               be created.
987 +               When the aufs mount option 'noxino' is specified, it
988 +               will be empty. About XINO files, see the aufs manual.
989 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
990 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
991 +++ linux/Documentation/ABI/testing/sysfs-aufs  2016-01-13 20:11:11.663093444 +0100
992 @@ -0,0 +1,31 @@
993 +What:          /sys/fs/aufs/si_<id>/
994 +Date:          March 2009
995 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
996 +Description:
997 +               Under /sys/fs/aufs, a directory named si_<id> is created
998 +               per aufs mount, where <id> is a unique id generated
999 +               internally.
1000 +
1001 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1002 +Date:          March 2009
1003 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1004 +Description:
1005 +               It shows the abolute path of a member directory (which
1006 +               is called branch) in aufs, and its permission.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1009 +Date:          July 2013
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the id of a member directory (which is called
1013 +               branch) in aufs.
1014 +
1015 +What:          /sys/fs/aufs/si_<id>/xi_path
1016 +Date:          March 2009
1017 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1018 +Description:
1019 +               It shows the abolute path of XINO (External Inode Number
1020 +               Bitmap, Translation Table and Generation Table) file
1021 +               even if it is the default path.
1022 +               When the aufs mount option 'noxino' is specified, it
1023 +               will be empty. About XINO files, see the aufs manual.
1024 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1025 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1026 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2016-01-13 20:11:11.663093444 +0100
1027 @@ -0,0 +1,170 @@
1028 +
1029 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1030 +# 
1031 +# This program is free software; you can redistribute it and/or modify
1032 +# it under the terms of the GNU General Public License as published by
1033 +# the Free Software Foundation; either version 2 of the License, or
1034 +# (at your option) any later version.
1035 +# 
1036 +# This program is distributed in the hope that it will be useful,
1037 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1038 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1039 +# GNU General Public License for more details.
1040 +# 
1041 +# You should have received a copy of the GNU General Public License
1042 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1043 +
1044 +Introduction
1045 +----------------------------------------
1046 +
1047 +aufs [ei ju: ef es] | [a u f s]
1048 +1. abbrev. for "advanced multi-layered unification filesystem".
1049 +2. abbrev. for "another unionfs".
1050 +3. abbrev. for "auf das" in German which means "on the" in English.
1051 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1052 +   But "Filesystem aufs Filesystem" is hard to understand.
1053 +
1054 +AUFS is a filesystem with features:
1055 +- multi layered stackable unification filesystem, the member directory
1056 +  is called as a branch.
1057 +- branch permission and attribute, 'readonly', 'real-readonly',
1058 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1059 +  combination.
1060 +- internal "file copy-on-write".
1061 +- logical deletion, whiteout.
1062 +- dynamic branch manipulation, adding, deleting and changing permission.
1063 +- allow bypassing aufs, user's direct branch access.
1064 +- external inode number translation table and bitmap which maintains the
1065 +  persistent aufs inode number.
1066 +- seekable directory, including NFS readdir.
1067 +- file mapping, mmap and sharing pages.
1068 +- pseudo-link, hardlink over branches.
1069 +- loopback mounted filesystem as a branch.
1070 +- several policies to select one among multiple writable branches.
1071 +- revert a single systemcall when an error occurs in aufs.
1072 +- and more...
1073 +
1074 +
1075 +Multi Layered Stackable Unification Filesystem
1076 +----------------------------------------------------------------------
1077 +Most people already knows what it is.
1078 +It is a filesystem which unifies several directories and provides a
1079 +merged single directory. When users access a file, the access will be
1080 +passed/re-directed/converted (sorry, I am not sure which English word is
1081 +correct) to the real file on the member filesystem. The member
1082 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1083 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1084 +readonly branch is handled by creating 'whiteout' on the upper writable
1085 +branch.
1086 +
1087 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1088 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1089 +different approaches to implement the merged-view.
1090 +The former tries putting it into VFS, and the latter implements as a
1091 +separate filesystem.
1092 +(If I misunderstand about these implementations, please let me know and
1093 +I shall correct it. Because it is a long time ago when I read their
1094 +source files last time).
1095 +
1096 +UnionMount's approach will be able to small, but may be hard to share
1097 +branches between several UnionMount since the whiteout in it is
1098 +implemented in the inode on branch filesystem and always
1099 +shared. According to Bharata's post, readdir does not seems to be
1100 +finished yet.
1101 +There are several missing features known in this implementations such as
1102 +- for users, the inode number may change silently. eg. copy-up.
1103 +- link(2) may break by copy-up.
1104 +- read(2) may get an obsoleted filedata (fstat(2) too).
1105 +- fcntl(F_SETLK) may be broken by copy-up.
1106 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1107 +  open(O_RDWR).
1108 +
1109 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1110 +merged into mainline. This is another implementation of UnionMount as a
1111 +separated filesystem. All the limitations and known problems which
1112 +UnionMount are equally inherited to "overlay" filesystem.
1113 +
1114 +Unionfs has a longer history. When I started implementing a stackable
1115 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1116 +inode, dentry and file objects and they have an array pointing lower
1117 +same kind objects. After contributing many patches for Unionfs, I
1118 +re-started my project AUFS (Jun 2006).
1119 +
1120 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1121 +implemented my own ideas, approaches and enhancements and it became
1122 +totally different one.
1123 +
1124 +Comparing DM snapshot and fs based implementation
1125 +- the number of bytes to be copied between devices is much smaller.
1126 +- the type of filesystem must be one and only.
1127 +- the fs must be writable, no readonly fs, even for the lower original
1128 +  device. so the compression fs will not be usable. but if we use
1129 +  loopback mount, we may address this issue.
1130 +  for instance,
1131 +       mount /cdrom/squashfs.img /sq
1132 +       losetup /sq/ext2.img
1133 +       losetup /somewhere/cow
1134 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1135 +- it will be difficult (or needs more operations) to extract the
1136 +  difference between the original device and COW.
1137 +- DM snapshot-merge may help a lot when users try merging. in the
1138 +  fs-layer union, users will use rsync(1).
1139 +
1140 +You may want to read my old paper "Filesystems in LiveCD"
1141 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1142 +
1143 +
1144 +Several characters/aspects/persona of aufs
1145 +----------------------------------------------------------------------
1146 +
1147 +Aufs has several characters, aspects or persona.
1148 +1. a filesystem, callee of VFS helper
1149 +2. sub-VFS, caller of VFS helper for branches
1150 +3. a virtual filesystem which maintains persistent inode number
1151 +4. reader/writer of files on branches such like an application
1152 +
1153 +1. Callee of VFS Helper
1154 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1155 +unlink(2) from an application reaches sys_unlink() kernel function and
1156 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1157 +calls filesystem specific unlink operation. Actually aufs implements the
1158 +unlink operation but it behaves like a redirector.
1159 +
1160 +2. Caller of VFS Helper for Branches
1161 +aufs_unlink() passes the unlink request to the branch filesystem as if
1162 +it were called from VFS. So the called unlink operation of the branch
1163 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1164 +every necessary pre/post operation for the branch filesystem.
1165 +- acquire the lock for the parent dir on a branch
1166 +- lookup in a branch
1167 +- revalidate dentry on a branch
1168 +- mnt_want_write() for a branch
1169 +- vfs_unlink() for a branch
1170 +- mnt_drop_write() for a branch
1171 +- release the lock on a branch
1172 +
1173 +3. Persistent Inode Number
1174 +One of the most important issue for a filesystem is to maintain inode
1175 +numbers. This is particularly important to support exporting a
1176 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1177 +backend block device for its own. But some storage is necessary to
1178 +keep and maintain the inode numbers. It may be a large space and may not
1179 +suit to keep in memory. Aufs rents some space from its first writable
1180 +branch filesystem (by default) and creates file(s) on it. These files
1181 +are created by aufs internally and removed soon (currently) keeping
1182 +opened.
1183 +Note: Because these files are removed, they are totally gone after
1184 +      unmounting aufs. It means the inode numbers are not persistent
1185 +      across unmount or reboot. I have a plan to make them really
1186 +      persistent which will be important for aufs on NFS server.
1187 +
1188 +4. Read/Write Files Internally (copy-on-write)
1189 +Because a branch can be readonly, when you write a file on it, aufs will
1190 +"copy-up" it to the upper writable branch internally. And then write the
1191 +originally requested thing to the file. Generally kernel doesn't
1192 +open/read/write file actively. In aufs, even a single write may cause a
1193 +internal "file copy". This behaviour is very similar to cp(1) command.
1194 +
1195 +Some people may think it is better to pass such work to user space
1196 +helper, instead of doing in kernel space. Actually I am still thinking
1197 +about it. But currently I have implemented it in kernel space.
1198 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1199 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1200 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2016-01-13 20:11:11.663093444 +0100
1201 @@ -0,0 +1,258 @@
1202 +
1203 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1204 +# 
1205 +# This program is free software; you can redistribute it and/or modify
1206 +# it under the terms of the GNU General Public License as published by
1207 +# the Free Software Foundation; either version 2 of the License, or
1208 +# (at your option) any later version.
1209 +# 
1210 +# This program is distributed in the hope that it will be useful,
1211 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1212 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1213 +# GNU General Public License for more details.
1214 +# 
1215 +# You should have received a copy of the GNU General Public License
1216 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1217 +
1218 +Basic Aufs Internal Structure
1219 +
1220 +Superblock/Inode/Dentry/File Objects
1221 +----------------------------------------------------------------------
1222 +As like an ordinary filesystem, aufs has its own
1223 +superblock/inode/dentry/file objects. All these objects have a
1224 +dynamically allocated array and store the same kind of pointers to the
1225 +lower filesystem, branch.
1226 +For example, when you build a union with one readwrite branch and one
1227 +readonly, mounted /au, /rw and /ro respectively.
1228 +- /au = /rw + /ro
1229 +- /ro/fileA exists but /rw/fileA
1230 +
1231 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1232 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1233 +- [0] = NULL (because /rw/fileA doesn't exist)
1234 +- [1] = /ro/fileA
1235 +
1236 +This style of an array is essentially same to the aufs
1237 +superblock/inode/dentry/file objects.
1238 +
1239 +Because aufs supports manipulating branches, ie. add/delete/change
1240 +branches dynamically, these objects has its own generation. When
1241 +branches are changed, the generation in aufs superblock is
1242 +incremented. And a generation in other object are compared when it is
1243 +accessed. When a generation in other objects are obsoleted, aufs
1244 +refreshes the internal array.
1245 +
1246 +
1247 +Superblock
1248 +----------------------------------------------------------------------
1249 +Additionally aufs superblock has some data for policies to select one
1250 +among multiple writable branches, XIB files, pseudo-links and kobject.
1251 +See below in detail.
1252 +About the policies which supports copy-down a directory, see
1253 +wbr_policy.txt too.
1254 +
1255 +
1256 +Branch and XINO(External Inode Number Translation Table)
1257 +----------------------------------------------------------------------
1258 +Every branch has its own xino (external inode number translation table)
1259 +file. The xino file is created and unlinked by aufs internally. When two
1260 +members of a union exist on the same filesystem, they share the single
1261 +xino file.
1262 +The struct of a xino file is simple, just a sequence of aufs inode
1263 +numbers which is indexed by the lower inode number.
1264 +In the above sample, assume the inode number of /ro/fileA is i111 and
1265 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1266 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1267 +
1268 +When the inode numbers are not contiguous, the xino file will be sparse
1269 +which has a hole in it and doesn't consume as much disk space as it
1270 +might appear. If your branch filesystem consumes disk space for such
1271 +holes, then you should specify 'xino=' option at mounting aufs.
1272 +
1273 +Aufs has a mount option to free the disk blocks for such holes in XINO
1274 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1275 +meet a problem of disk shortage due to XINO files, then you should try
1276 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1277 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1278 +the holes in XINO files.
1279 +
1280 +Also a writable branch has three kinds of "whiteout bases". All these
1281 +are existed when the branch is joined to aufs, and their names are
1282 +whiteout-ed doubly, so that users will never see their names in aufs
1283 +hierarchy.
1284 +1. a regular file which will be hardlinked to all whiteouts.
1285 +2. a directory to store a pseudo-link.
1286 +3. a directory to store an "orphan"-ed file temporary.
1287 +
1288 +1. Whiteout Base
1289 +   When you remove a file on a readonly branch, aufs handles it as a
1290 +   logical deletion and creates a whiteout on the upper writable branch
1291 +   as a hardlink of this file in order not to consume inode on the
1292 +   writable branch.
1293 +2. Pseudo-link Dir
1294 +   See below, Pseudo-link.
1295 +3. Step-Parent Dir
1296 +   When "fileC" exists on the lower readonly branch only and it is
1297 +   opened and removed with its parent dir, and then user writes
1298 +   something into it, then aufs copies-up fileC to this
1299 +   directory. Because there is no other dir to store fileC. After
1300 +   creating a file under this dir, the file is unlinked.
1301 +
1302 +Because aufs supports manipulating branches, ie. add/delete/change
1303 +dynamically, a branch has its own id. When the branch order changes,
1304 +aufs finds the new index by searching the branch id.
1305 +
1306 +
1307 +Pseudo-link
1308 +----------------------------------------------------------------------
1309 +Assume "fileA" exists on the lower readonly branch only and it is
1310 +hardlinked to "fileB" on the branch. When you write something to fileA,
1311 +aufs copies-up it to the upper writable branch. Additionally aufs
1312 +creates a hardlink under the Pseudo-link Directory of the writable
1313 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1314 +simple list. If fileB is read after unlinking fileA, aufs returns
1315 +filedata from the pseudo-link instead of the lower readonly
1316 +branch. Because the pseudo-link is based upon the inode, to keep the
1317 +inode number by xino (see above) is essentially necessary.
1318 +
1319 +All the hardlinks under the Pseudo-link Directory of the writable branch
1320 +should be restored in a proper location later. Aufs provides a utility
1321 +to do this. The userspace helpers executed at remounting and unmounting
1322 +aufs by default.
1323 +During this utility is running, it puts aufs into the pseudo-link
1324 +maintenance mode. In this mode, only the process which began the
1325 +maintenance mode (and its child processes) is allowed to operate in
1326 +aufs. Some other processes which are not related to the pseudo-link will
1327 +be allowed to run too, but the rest have to return an error or wait
1328 +until the maintenance mode ends. If a process already acquires an inode
1329 +mutex (in VFS), it has to return an error.
1330 +
1331 +
1332 +XIB(external inode number bitmap)
1333 +----------------------------------------------------------------------
1334 +Addition to the xino file per a branch, aufs has an external inode number
1335 +bitmap in a superblock object. It is also an internal file such like a
1336 +xino file.
1337 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1338 +not.
1339 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1340 +
1341 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1342 +reduce the number of consumed disk blocks for these files.
1343 +
1344 +
1345 +Virtual or Vertical Dir, and Readdir in Userspace
1346 +----------------------------------------------------------------------
1347 +In order to support multiple layers (branches), aufs readdir operation
1348 +constructs a virtual dir block on memory. For readdir, aufs calls
1349 +vfs_readdir() internally for each dir on branches, merges their entries
1350 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1351 +object. So the file object has its entry list until it is closed. The
1352 +entry list will be updated when the file position is zero and becomes
1353 +obsoleted. This decision is made in aufs automatically.
1354 +
1355 +The dynamically allocated memory block for the name of entries has a
1356 +unit of 512 bytes (by default) and stores the names contiguously (no
1357 +padding). Another block for each entry is handled by kmem_cache too.
1358 +During building dir blocks, aufs creates hash list and judging whether
1359 +the entry is whiteouted by its upper branch or already listed.
1360 +The merged result is cached in the corresponding inode object and
1361 +maintained by a customizable life-time option.
1362 +
1363 +Some people may call it can be a security hole or invite DoS attack
1364 +since the opened and once readdir-ed dir (file object) holds its entry
1365 +list and becomes a pressure for system memory. But I'd say it is similar
1366 +to files under /proc or /sys. The virtual files in them also holds a
1367 +memory page (generally) while they are opened. When an idea to reduce
1368 +memory for them is introduced, it will be applied to aufs too.
1369 +For those who really hate this situation, I've developed readdir(3)
1370 +library which operates this merging in userspace. You just need to set
1371 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1372 +kernel space for readdir(3).
1373 +
1374 +
1375 +Workqueue
1376 +----------------------------------------------------------------------
1377 +Aufs sometimes requires privilege access to a branch. For instance,
1378 +in copy-up/down operation. When a user process is going to make changes
1379 +to a file which exists in the lower readonly branch only, and the mode
1380 +of one of ancestor directories may not be writable by a user
1381 +process. Here aufs copy-up the file with its ancestors and they may
1382 +require privilege to set its owner/group/mode/etc.
1383 +This is a typical case of a application character of aufs (see
1384 +Introduction).
1385 +
1386 +Aufs uses workqueue synchronously for this case. It creates its own
1387 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1388 +passes the request to call mkdir or write (for example), and wait for
1389 +its completion. This approach solves a problem of a signal handler
1390 +simply.
1391 +If aufs didn't adopt the workqueue and changed the privilege of the
1392 +process, then the process may receive the unexpected SIGXFSZ or other
1393 +signals.
1394 +
1395 +Also aufs uses the system global workqueue ("events" kernel thread) too
1396 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1397 +whiteout base and etc. This is unrelated to a privilege.
1398 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1399 +superblock at the beginning, at the same time waits for the completion
1400 +of all queued asynchronous tasks.
1401 +
1402 +
1403 +Whiteout
1404 +----------------------------------------------------------------------
1405 +The whiteout in aufs is very similar to Unionfs's. That is represented
1406 +by its filename. UnionMount takes an approach of a file mode, but I am
1407 +afraid several utilities (find(1) or something) will have to support it.
1408 +
1409 +Basically the whiteout represents "logical deletion" which stops aufs to
1410 +lookup further, but also it represents "dir is opaque" which also stop
1411 +further lookup.
1412 +
1413 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1414 +In order to make several functions in a single systemcall to be
1415 +revertible, aufs adopts an approach to rename a directory to a temporary
1416 +unique whiteouted name.
1417 +For example, in rename(2) dir where the target dir already existed, aufs
1418 +renames the target dir to a temporary unique whiteouted name before the
1419 +actual rename on a branch, and then handles other actions (make it opaque,
1420 +update the attributes, etc). If an error happens in these actions, aufs
1421 +simply renames the whiteouted name back and returns an error. If all are
1422 +succeeded, aufs registers a function to remove the whiteouted unique
1423 +temporary name completely and asynchronously to the system global
1424 +workqueue.
1425 +
1426 +
1427 +Copy-up
1428 +----------------------------------------------------------------------
1429 +It is a well-known feature or concept.
1430 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1431 +internally and makes change to the new file on the upper writable branch.
1432 +When the trigger systemcall does not update the timestamps of the parent
1433 +dir, aufs reverts it after copy-up.
1434 +
1435 +
1436 +Move-down (aufs3.9 and later)
1437 +----------------------------------------------------------------------
1438 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1439 +the lower readonly branch to the upper writable branch when a user
1440 +changes something about the file.
1441 +"Move-down" is an opposite action of copy-up. Basically this action is
1442 +ran manually instead of automatically and internally.
1443 +For desgin and implementation, aufs has to consider these issues.
1444 +- whiteout for the file may exist on the lower branch.
1445 +- ancestor directories may not exist on the lower branch.
1446 +- diropq for the ancestor directories may exist on the upper branch.
1447 +- free space on the lower branch will reduce.
1448 +- another access to the file may happen during moving-down, including
1449 +  UDBA (see "Revalidate Dentry and UDBA").
1450 +- the file should not be hard-linked nor pseudo-linked. they should be
1451 +  handled by auplink utility later.
1452 +
1453 +Sometimes users want to move-down a file from the upper writable branch
1454 +to the lower readonly or writable branch. For instance,
1455 +- the free space of the upper writable branch is going to run out.
1456 +- create a new intermediate branch between the upper and lower branch.
1457 +- etc.
1458 +
1459 +For this purpose, use "aumvdown" command in aufs-util.git.
1460 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1461 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1462 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2016-01-13 20:11:11.663093444 +0100
1463 @@ -0,0 +1,85 @@
1464 +
1465 +# Copyright (C) 2015 Junjiro R. Okajima
1466 +# 
1467 +# This program is free software; you can redistribute it and/or modify
1468 +# it under the terms of the GNU General Public License as published by
1469 +# the Free Software Foundation; either version 2 of the License, or
1470 +# (at your option) any later version.
1471 +# 
1472 +# This program is distributed in the hope that it will be useful,
1473 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1474 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1475 +# GNU General Public License for more details.
1476 +# 
1477 +# You should have received a copy of the GNU General Public License
1478 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1479 +
1480 +Support for a branch who has its ->atomic_open()
1481 +----------------------------------------------------------------------
1482 +The filesystems who implement its ->atomic_open() are not majority. For
1483 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1484 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1485 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1486 +sure whether all filesystems who have ->atomic_open() behave like this,
1487 +but NFSv4 surely returns the error.
1488 +
1489 +In order to support ->atomic_open() for aufs, there are a few
1490 +approaches.
1491 +
1492 +A. Introduce aufs_atomic_open()
1493 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1494 +     branch fs.
1495 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1496 +   an aufs user Pip Cet's approach
1497 +   - calls aufs_create(), VFS finish_open() and notify_change().
1498 +   - pass fake-mode to finish_open(), and then correct the mode by
1499 +     notify_change().
1500 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1501 +   - no aufs_atomic_open().
1502 +   - aufs_lookup() registers the TID to an aufs internal object.
1503 +   - aufs_create() does nothing when the matching TID is registered, but
1504 +     registers the mode.
1505 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1506 +     TID is registered.
1507 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1508 +   credential
1509 +   - no aufs_atomic_open().
1510 +   - aufs_create() registers the TID to an internal object. this info
1511 +     represents "this process created this file just now."
1512 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1513 +     registered TID and re-try open() with superuser's credential.
1514 +
1515 +Pros and cons for each approach.
1516 +
1517 +A.
1518 +   - straightforward but highly depends upon VFS internal.
1519 +   - the atomic behavaiour is kept.
1520 +   - some of parameters such as nameidata are hard to reproduce for
1521 +     branch fs.
1522 +   - large overhead.
1523 +B.
1524 +   - easy to implement.
1525 +   - the atomic behavaiour is lost.
1526 +C.
1527 +   - the atomic behavaiour is kept.
1528 +   - dirty and tricky.
1529 +   - VFS checks whether the file is created correctly after calling
1530 +     ->create(), which means this approach doesn't work.
1531 +D.
1532 +   - easy to implement.
1533 +   - the atomic behavaiour is lost.
1534 +   - to open a file with superuser's credential and give it to a user
1535 +     process is a bad idea, since the file object keeps the credential
1536 +     in it. It may affect LSM or something. This approach doesn't work
1537 +     either.
1538 +
1539 +The approach A is ideal, but it hard to implement. So here is a
1540 +variation of A, which is to be implemented.
1541 +
1542 +A-1. Introduce aufs_atomic_open()
1543 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1544 +       vfs_create() and finish_open().
1545 +     - the demerit is that the several checks after branch fs
1546 +       ->atomic_open() are lost. in the ordinary case, the checks are
1547 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1548 +       be implemented in aufs, but not all I am afraid.
1549 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1550 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1551 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2016-01-13 20:11:11.663093444 +0100
1552 @@ -0,0 +1,113 @@
1553 +
1554 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1555 +# 
1556 +# This program is free software; you can redistribute it and/or modify
1557 +# it under the terms of the GNU General Public License as published by
1558 +# the Free Software Foundation; either version 2 of the License, or
1559 +# (at your option) any later version.
1560 +# 
1561 +# This program is distributed in the hope that it will be useful,
1562 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1563 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1564 +# GNU General Public License for more details.
1565 +# 
1566 +# You should have received a copy of the GNU General Public License
1567 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1568 +
1569 +Lookup in a Branch
1570 +----------------------------------------------------------------------
1571 +Since aufs has a character of sub-VFS (see Introduction), it operates
1572 +lookup for branches as VFS does. It may be a heavy work. But almost all
1573 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1574 +directly connected to its parent. Digging down the directory hierarchy
1575 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1576 +aufs calls it.
1577 +
1578 +When a branch is a remote filesystem, aufs basically relies upon its
1579 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1580 +them.
1581 +For d_revalidate, aufs implements three levels of revalidate tests. See
1582 +"Revalidate Dentry and UDBA" in detail.
1583 +
1584 +
1585 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1586 +----------------------------------------------------------------------
1587 +Let's try case study.
1588 +- aufs has two branches, upper readwrite and lower readonly.
1589 +  /au = /rw + /ro
1590 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1591 +- user invoked "chmod a+rx /au/dirA"
1592 +- the internal copy-up is activated and "/rw/dirA" is created and its
1593 +  permission bits are set to world readable.
1594 +- then "/au/dirA" becomes world readable?
1595 +
1596 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1597 +or it may be a natively readonly filesystem. If aufs respects the lower
1598 +branch, it should not respond readdir request from other users. But user
1599 +allowed it by chmod. Should really aufs rejects showing the entries
1600 +under /ro/dirA?
1601 +
1602 +To be honest, I don't have a good solution for this case. So aufs
1603 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1604 +users.
1605 +When dirperm1 is specified, aufs checks only the highest one for the
1606 +directory permission, and shows the entries. Otherwise, as usual, checks
1607 +every dir existing on all branches and rejects the request.
1608 +
1609 +As a side effect, dirperm1 option improves the performance of aufs
1610 +because the number of permission check is reduced when the number of
1611 +branch is many.
1612 +
1613 +
1614 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1615 +----------------------------------------------------------------------
1616 +Generally VFS helpers re-validate a dentry as a part of lookup.
1617 +0. digging down the directory hierarchy.
1618 +1. lock the parent dir by its i_mutex.
1619 +2. lookup the final (child) entry.
1620 +3. revalidate it.
1621 +4. call the actual operation (create, unlink, etc.)
1622 +5. unlock the parent dir
1623 +
1624 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1625 +called. Actually aufs implements it and checks the dentry on a branch is
1626 +still valid.
1627 +But it is not enough. Because aufs has to release the lock for the
1628 +parent dir on a branch at the end of ->lookup() (step 2) and
1629 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1630 +held by VFS.
1631 +If the file on a branch is changed directly, eg. bypassing aufs, after
1632 +aufs released the lock, then the subsequent operation may cause
1633 +something unpleasant result.
1634 +
1635 +This situation is a result of VFS architecture, ->lookup() and
1636 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1637 +design from VFS's point of view. It is just not suitable for sub-VFS
1638 +character in aufs.
1639 +
1640 +Aufs supports such case by three level of revalidation which is
1641 +selectable by user.
1642 +1. Simple Revalidate
1643 +   Addition to the native flow in VFS's, confirm the child-parent
1644 +   relationship on the branch just after locking the parent dir on the
1645 +   branch in the "actual operation" (step 4). When this validation
1646 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1647 +   checks the validation of the dentry on branches.
1648 +2. Monitor Changes Internally by Inotify/Fsnotify
1649 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1650 +   the dentry on the branch, and returns EBUSY if it finds different
1651 +   dentry.
1652 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1653 +   during it is in cache. When the event is notified, aufs registers a
1654 +   function to kernel 'events' thread by schedule_work(). And the
1655 +   function sets some special status to the cached aufs dentry and inode
1656 +   private data. If they are not cached, then aufs has nothing to
1657 +   do. When the same file is accessed through aufs (step 0-3) later,
1658 +   aufs will detect the status and refresh all necessary data.
1659 +   In this mode, aufs has to ignore the event which is fired by aufs
1660 +   itself.
1661 +3. No Extra Validation
1662 +   This is the simplest test and doesn't add any additional revalidation
1663 +   test, and skip the revalidation in step 4. It is useful and improves
1664 +   aufs performance when system surely hide the aufs branches from user,
1665 +   by over-mounting something (or another method).
1666 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1667 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1668 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2016-01-13 20:11:11.663093444 +0100
1669 @@ -0,0 +1,74 @@
1670 +
1671 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1672 +# 
1673 +# This program is free software; you can redistribute it and/or modify
1674 +# it under the terms of the GNU General Public License as published by
1675 +# the Free Software Foundation; either version 2 of the License, or
1676 +# (at your option) any later version.
1677 +# 
1678 +# This program is distributed in the hope that it will be useful,
1679 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1680 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1681 +# GNU General Public License for more details.
1682 +# 
1683 +# You should have received a copy of the GNU General Public License
1684 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1685 +
1686 +Branch Manipulation
1687 +
1688 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1689 +and changing its permission/attribute, there are a lot of works to do.
1690 +
1691 +
1692 +Add a Branch
1693 +----------------------------------------------------------------------
1694 +o Confirm the adding dir exists outside of aufs, including loopback
1695 +  mount, and its various attributes.
1696 +o Initialize the xino file and whiteout bases if necessary.
1697 +  See struct.txt.
1698 +
1699 +o Check the owner/group/mode of the directory
1700 +  When the owner/group/mode of the adding directory differs from the
1701 +  existing branch, aufs issues a warning because it may impose a
1702 +  security risk.
1703 +  For example, when a upper writable branch has a world writable empty
1704 +  top directory, a malicious user can create any files on the writable
1705 +  branch directly, like copy-up and modify manually. If something like
1706 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1707 +  writable branch, and the writable branch is world-writable, then a
1708 +  malicious guy may create /etc/passwd on the writable branch directly
1709 +  and the infected file will be valid in aufs.
1710 +  I am afraid it can be a security issue, but aufs can do nothing except
1711 +  producing a warning.
1712 +
1713 +
1714 +Delete a Branch
1715 +----------------------------------------------------------------------
1716 +o Confirm the deleting branch is not busy
1717 +  To be general, there is one merit to adopt "remount" interface to
1718 +  manipulate branches. It is to discard caches. At deleting a branch,
1719 +  aufs checks the still cached (and connected) dentries and inodes. If
1720 +  there are any, then they are all in-use. An inode without its
1721 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1722 +
1723 +  For the cached one, aufs checks whether the same named entry exists on
1724 +  other branches.
1725 +  If the cached one is a directory, because aufs provides a merged view
1726 +  to users, as long as one dir is left on any branch aufs can show the
1727 +  dir to users. In this case, the branch can be removed from aufs.
1728 +  Otherwise aufs rejects deleting the branch.
1729 +
1730 +  If any file on the deleting branch is opened by aufs, then aufs
1731 +  rejects deleting.
1732 +
1733 +
1734 +Modify the Permission of a Branch
1735 +----------------------------------------------------------------------
1736 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1737 +  See struct.txt.
1738 +
1739 +o rw --> ro: Confirm the modifying branch is not busy
1740 +  Aufs rejects the request if any of these conditions are true.
1741 +  - a file on the branch is mmap-ed.
1742 +  - a regular file on the branch is opened for write and there is no
1743 +    same named entry on the upper branch.
1744 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1745 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1746 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2016-01-13 20:11:11.663093444 +0100
1747 @@ -0,0 +1,64 @@
1748 +
1749 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1750 +# 
1751 +# This program is free software; you can redistribute it and/or modify
1752 +# it under the terms of the GNU General Public License as published by
1753 +# the Free Software Foundation; either version 2 of the License, or
1754 +# (at your option) any later version.
1755 +# 
1756 +# This program is distributed in the hope that it will be useful,
1757 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1758 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1759 +# GNU General Public License for more details.
1760 +# 
1761 +# You should have received a copy of the GNU General Public License
1762 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1763 +
1764 +Policies to Select One among Multiple Writable Branches
1765 +----------------------------------------------------------------------
1766 +When the number of writable branch is more than one, aufs has to decide
1767 +the target branch for file creation or copy-up. By default, the highest
1768 +writable branch which has the parent (or ancestor) dir of the target
1769 +file is chosen (top-down-parent policy).
1770 +By user's request, aufs implements some other policies to select the
1771 +writable branch, for file creation several policies, round-robin,
1772 +most-free-space, and other policies. For copy-up, top-down-parent,
1773 +bottom-up-parent, bottom-up and others.
1774 +
1775 +As expected, the round-robin policy selects the branch in circular. When
1776 +you have two writable branches and creates 10 new files, 5 files will be
1777 +created for each branch. mkdir(2) systemcall is an exception. When you
1778 +create 10 new directories, all will be created on the same branch.
1779 +And the most-free-space policy selects the one which has most free
1780 +space among the writable branches. The amount of free space will be
1781 +checked by aufs internally, and users can specify its time interval.
1782 +
1783 +The policies for copy-up is more simple,
1784 +top-down-parent is equivalent to the same named on in create policy,
1785 +bottom-up-parent selects the writable branch where the parent dir
1786 +exists and the nearest upper one from the copyup-source,
1787 +bottom-up selects the nearest upper writable branch from the
1788 +copyup-source, regardless the existence of the parent dir.
1789 +
1790 +There are some rules or exceptions to apply these policies.
1791 +- If there is a readonly branch above the policy-selected branch and
1792 +  the parent dir is marked as opaque (a variation of whiteout), or the
1793 +  target (creating) file is whiteout-ed on the upper readonly branch,
1794 +  then the result of the policy is ignored and the target file will be
1795 +  created on the nearest upper writable branch than the readonly branch.
1796 +- If there is a writable branch above the policy-selected branch and
1797 +  the parent dir is marked as opaque or the target file is whiteouted
1798 +  on the branch, then the result of the policy is ignored and the target
1799 +  file will be created on the highest one among the upper writable
1800 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1801 +  it as usual.
1802 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1803 +  They try selecting the branch where the source exists as possible
1804 +  since copyup a large file will take long time. If it can't be,
1805 +  ie. the branch where the source exists is readonly, then they will
1806 +  follow the copyup policy.
1807 +- There is an exception for rename(2) when the target exists.
1808 +  If the rename target exists, aufs compares the index of the branches
1809 +  where the source and the target exists and selects the higher
1810 +  one. If the selected branch is readonly, then aufs follows the
1811 +  copyup policy.
1812 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1813 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1814 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2016-01-13 20:11:11.663093444 +0100
1815 @@ -0,0 +1,120 @@
1816 +
1817 +# Copyright (C) 2011-2015 Junjiro R. Okajima
1818 +# 
1819 +# This program is free software; you can redistribute it and/or modify
1820 +# it under the terms of the GNU General Public License as published by
1821 +# the Free Software Foundation; either version 2 of the License, or
1822 +# (at your option) any later version.
1823 +# 
1824 +# This program is distributed in the hope that it will be useful,
1825 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1826 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1827 +# GNU General Public License for more details.
1828 +# 
1829 +# You should have received a copy of the GNU General Public License
1830 +# along with this program; if not, write to the Free Software
1831 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1832 +
1833 +
1834 +File-based Hierarchical Storage Management (FHSM)
1835 +----------------------------------------------------------------------
1836 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1837 +storage world. Aufs provides this feature as file-based with multiple
1838 +writable branches, based upon the principle of "Colder, the Lower".
1839 +Here the word "colder" means that the less used files, and "lower" means
1840 +that the position in the order of the stacked branches vertically.
1841 +These multiple writable branches are prioritized, ie. the topmost one
1842 +should be the fastest drive and be used heavily.
1843 +
1844 +o Characters in aufs FHSM story
1845 +- aufs itself and a new branch attribute.
1846 +- a new ioctl interface to move-down and to establish a connection with
1847 +  the daemon ("move-down" is a converse of "copy-up").
1848 +- userspace tool and daemon.
1849 +
1850 +The userspace daemon establishes a connection with aufs and waits for
1851 +the notification. The notified information is very similar to struct
1852 +statfs containing the number of consumed blocks and inodes.
1853 +When the consumed blocks/inodes of a branch exceeds the user-specified
1854 +upper watermark, the daemon activates its move-down process until the
1855 +consumed blocks/inodes reaches the user-specified lower watermark.
1856 +
1857 +The actual move-down is done by aufs based upon the request from
1858 +user-space since we need to maintain the inode number and the internal
1859 +pointer arrays in aufs.
1860 +
1861 +Currently aufs FHSM handles the regular files only. Additionally they
1862 +must not be hard-linked nor pseudo-linked.
1863 +
1864 +
1865 +o Cowork of aufs and the user-space daemon
1866 +  During the userspace daemon established the connection, aufs sends a
1867 +  small notification to it whenever aufs writes something into the
1868 +  writable branch. But it may cost high since aufs issues statfs(2)
1869 +  internally. So user can specify a new option to cache the
1870 +  info. Actually the notification is controlled by these factors.
1871 +  + the specified cache time.
1872 +  + classified as "force" by aufs internally.
1873 +  Until the specified time expires, aufs doesn't send the info
1874 +  except the forced cases. When aufs decide forcing, the info is always
1875 +  notified to userspace.
1876 +  For example, the number of free inodes is generally large enough and
1877 +  the shortage of it happens rarely. So aufs doesn't force the
1878 +  notification when creating a new file, directory and others. This is
1879 +  the typical case which aufs doesn't force.
1880 +  When aufs writes the actual filedata and the files consumes any of new
1881 +  blocks, the aufs forces notifying.
1882 +
1883 +
1884 +o Interfaces in aufs
1885 +- New branch attribute.
1886 +  + fhsm
1887 +    Specifies that the branch is managed by FHSM feature. In other word,
1888 +    participant in the FHSM.
1889 +    When nofhsm is set to the branch, it will not be the source/target
1890 +    branch of the move-down operation. This attribute is set
1891 +    independently from coo and moo attributes, and if you want full
1892 +    FHSM, you should specify them as well.
1893 +- New mount option.
1894 +  + fhsm_sec
1895 +    Specifies a second to suppress many less important info to be
1896 +    notified.
1897 +- New ioctl.
1898 +  + AUFS_CTL_FHSM_FD
1899 +    create a new file descriptor which userspace can read the notification
1900 +    (a subset of struct statfs) from aufs.
1901 +- Module parameter 'brs'
1902 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1903 +  be set.
1904 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1905 +  When there are two or more branches with fhsm attributes,
1906 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1907 +  terminates it. As a result of remounting and branch-manipulation, the
1908 +  number of branches with fhsm attribute can be one. In this case,
1909 +  /sbin/mount.aufs will terminate the user-space daemon.
1910 +
1911 +
1912 +Finally the operation is done as these steps in kernel-space.
1913 +- make sure that,
1914 +  + no one else is using the file.
1915 +  + the file is not hard-linked.
1916 +  + the file is not pseudo-linked.
1917 +  + the file is a regular file.
1918 +  + the parent dir is not opaqued.
1919 +- find the target writable branch.
1920 +- make sure the file is not whiteout-ed by the upper (than the target)
1921 +  branch.
1922 +- make the parent dir on the target branch.
1923 +- mutex lock the inode on the branch.
1924 +- unlink the whiteout on the target branch (if exists).
1925 +- lookup and create the whiteout-ed temporary name on the target branch.
1926 +- copy the file as the whiteout-ed temporary name on the target branch.
1927 +- rename the whiteout-ed temporary name to the original name.
1928 +- unlink the file on the source branch.
1929 +- maintain the internal pointer array and the external inode number
1930 +  table (XINO).
1931 +- maintain the timestamps and other attributes of the parent dir and the
1932 +  file.
1933 +
1934 +And of course, in every step, an error may happen. So the operation
1935 +should restore the original file state after an error happens.
1936 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1937 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1938 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2016-01-13 20:11:11.663093444 +0100
1939 @@ -0,0 +1,72 @@
1940 +
1941 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1942 +# 
1943 +# This program is free software; you can redistribute it and/or modify
1944 +# it under the terms of the GNU General Public License as published by
1945 +# the Free Software Foundation; either version 2 of the License, or
1946 +# (at your option) any later version.
1947 +# 
1948 +# This program is distributed in the hope that it will be useful,
1949 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1950 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1951 +# GNU General Public License for more details.
1952 +# 
1953 +# You should have received a copy of the GNU General Public License
1954 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1955 +
1956 +mmap(2) -- File Memory Mapping
1957 +----------------------------------------------------------------------
1958 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1959 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1960 +->mmap().
1961 +This approach is simple and good, but there is one problem.
1962 +Under /proc, several entries show the mmapped files by its path (with
1963 +device and inode number), and the printed path will be the path on the
1964 +branch fs's instead of virtual aufs's.
1965 +This is not a problem in most cases, but some utilities lsof(1) (and its
1966 +user) may expect the path on aufs.
1967 +
1968 +To address this issue, aufs adds a new member called vm_prfile in struct
1969 +vm_area_struct (and struct vm_region). The original vm_file points to
1970 +the file on the branch fs in order to handle everything correctly as
1971 +usual. The new vm_prfile points to a virtual file in aufs, and the
1972 +show-functions in procfs refers to vm_prfile if it is set.
1973 +Also we need to maintain several other places where touching vm_file
1974 +such like
1975 +- fork()/clone() copies vma and the reference count of vm_file is
1976 +  incremented.
1977 +- merging vma maintains the ref count too.
1978 +
1979 +This is not a good approach. It just fakes the printed path. But it
1980 +leaves all behaviour around f_mapping unchanged. This is surely an
1981 +advantage.
1982 +Actually aufs had adopted another complicated approach which calls
1983 +generic_file_mmap() and handles struct vm_operations_struct. In this
1984 +approach, aufs met a hard problem and I could not solve it without
1985 +switching the approach.
1986 +
1987 +There may be one more another approach which is
1988 +- bind-mount the branch-root onto the aufs-root internally
1989 +- grab the new vfsmount (ie. struct mount)
1990 +- lazy-umount the branch-root internally
1991 +- in open(2) the aufs-file, open the branch-file with the hidden
1992 +  vfsmount (instead of the original branch's vfsmount)
1993 +- ideally this "bind-mount and lazy-umount" should be done atomically,
1994 +  but it may be possible from userspace by the mount helper.
1995 +
1996 +Adding the internal hidden vfsmount and using it in opening a file, the
1997 +file path under /proc will be printed correctly. This approach looks
1998 +smarter, but is not possible I am afraid.
1999 +- aufs-root may be bind-mount later. when it happens, another hidden
2000 +  vfsmount will be required.
2001 +- it is hard to get the chance to bind-mount and lazy-umount
2002 +  + in kernel-space, FS can have vfsmount in open(2) via
2003 +    file->f_path, and aufs can know its vfsmount. But several locks are
2004 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2005 +    here, then it may cause a deadlock.
2006 +  + in user-space, bind-mount doesn't invoke the mount helper.
2007 +- since /proc shows dev and ino, aufs has to give vma these info. it
2008 +  means a new member vm_prinode will be necessary. this is essentially
2009 +  equivalent to vm_prfile described above.
2010 +
2011 +I have to give up this "looks-smater" approach.
2012 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2013 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2014 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2016-01-13 20:11:11.663093444 +0100
2015 @@ -0,0 +1,96 @@
2016 +
2017 +# Copyright (C) 2014-2015 Junjiro R. Okajima
2018 +#
2019 +# This program is free software; you can redistribute it and/or modify
2020 +# it under the terms of the GNU General Public License as published by
2021 +# the Free Software Foundation; either version 2 of the License, or
2022 +# (at your option) any later version.
2023 +#
2024 +# This program is distributed in the hope that it will be useful,
2025 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2026 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2027 +# GNU General Public License for more details.
2028 +#
2029 +# You should have received a copy of the GNU General Public License
2030 +# along with this program; if not, write to the Free Software
2031 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2032 +
2033 +
2034 +Listing XATTR/EA and getting the value
2035 +----------------------------------------------------------------------
2036 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2037 +shows the values from the topmost existing file. This behaviour is good
2038 +for the non-dir entries since the bahaviour exactly matches the shown
2039 +information. But for the directories, aufs considers all the same named
2040 +entries on the lower branches. Which means, if one of the lower entry
2041 +rejects readdir call, then aufs returns an error even if the topmost
2042 +entry allows it. This behaviour is necessary to respect the branch fs's
2043 +security, but can make users confused since the user-visible standard
2044 +attributes don't match the behaviour.
2045 +To address this issue, aufs has a mount option called dirperm1 which
2046 +checks the permission for the topmost entry only, and ignores the lower
2047 +entry's permission.
2048 +
2049 +A similar issue can happen around XATTR.
2050 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2051 +always set. Otherwise these very unpleasant situation would happen.
2052 +- listxattr(2) may return the duplicated entries.
2053 +- users may not be able to remove or reset the XATTR forever,
2054 +
2055 +
2056 +XATTR/EA support in the internal (copy,move)-(up,down)
2057 +----------------------------------------------------------------------
2058 +Generally the extended attributes of inode are categorized as these.
2059 +- "security" for LSM and capability.
2060 +- "system" for posix ACL, 'acl' mount option is required for the branch
2061 +  fs generally.
2062 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2063 +- "user" for userspace, 'user_xattr' mount option is required for the
2064 +  branch fs generally.
2065 +
2066 +Moreover there are some other categories. Aufs handles these rather
2067 +unpopular categories as the ordinary ones, ie. there is no special
2068 +condition nor exception.
2069 +
2070 +In copy-up, the support for XATTR on the dst branch may differ from the
2071 +src branch. In this case, the copy-up operation will get an error and
2072 +the original user operation which triggered the copy-up will fail. It
2073 +can happen that even all copy-up will fail.
2074 +When both of src and dst branches support XATTR and if an error occurs
2075 +during copying XATTR, then the copy-up should fail obviously. That is a
2076 +good reason and aufs should return an error to userspace. But when only
2077 +the src branch support that XATTR, aufs should not return an error.
2078 +For example, the src branch supports ACL but the dst branch doesn't
2079 +because the dst branch may natively un-support it or temporary
2080 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2081 +may NOT return an error even if the XATTR is not supported. It is
2082 +totally up to the branch fs.
2083 +
2084 +Anyway when the aufs internal copy-up gets an error from the dst branch
2085 +fs, then aufs tries removing the just copied entry and returns the error
2086 +to the userspace. The worst case of this situation will be all copy-up
2087 +will fail.
2088 +
2089 +For the copy-up operation, there two basic approaches.
2090 +- copy the specified XATTR only (by category above), and return the
2091 +  error unconditionally if it happens.
2092 +- copy all XATTR, and ignore the error on the specified category only.
2093 +
2094 +In order to support XATTR and to implement the correct behaviour, aufs
2095 +chooses the latter approach and introduces some new branch attributes,
2096 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2097 +They correspond to the XATTR namespaces (see above). Additionally, to be
2098 +convenient, "icex" is also provided which means all "icex*" attributes
2099 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2100 +
2101 +The meaning of these attributes is to ignore the error from setting
2102 +XATTR on that branch.
2103 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2104 +error from the dst branch according to the specified attributes.
2105 +
2106 +Some XATTR may have its default value. The default value may come from
2107 +the parent dir or the environment. If the default value is set at the
2108 +file creating-time, it will be overwritten by copy-up.
2109 +Some contradiction may happen I am afraid.
2110 +Do we need another attribute to stop copying XATTR? I am unsure. For
2111 +now, aufs implements the branch attributes to ignore the error.
2112 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2113 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2114 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2016-01-13 20:11:11.663093444 +0100
2115 @@ -0,0 +1,58 @@
2116 +
2117 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2118 +# 
2119 +# This program is free software; you can redistribute it and/or modify
2120 +# it under the terms of the GNU General Public License as published by
2121 +# the Free Software Foundation; either version 2 of the License, or
2122 +# (at your option) any later version.
2123 +# 
2124 +# This program is distributed in the hope that it will be useful,
2125 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2126 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2127 +# GNU General Public License for more details.
2128 +# 
2129 +# You should have received a copy of the GNU General Public License
2130 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2131 +
2132 +Export Aufs via NFS
2133 +----------------------------------------------------------------------
2134 +Here is an approach.
2135 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2136 +  generation.
2137 +- iget_locked(): initialize aufs inode generation for a new inode, and
2138 +  store it in xigen file.
2139 +- destroy_inode(): increment aufs inode generation and store it in xigen
2140 +  file. it is necessary even if it is not unlinked, because any data of
2141 +  inode may be changed by UDBA.
2142 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2143 +  build file handle by
2144 +  + branch id (4 bytes)
2145 +  + superblock generation (4 bytes)
2146 +  + inode number (4 or 8 bytes)
2147 +  + parent dir inode number (4 or 8 bytes)
2148 +  + inode generation (4 bytes))
2149 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2150 +    bytes)
2151 +  + file handle for a branch (by exportfs_encode_fh())
2152 +- fh_to_dentry():
2153 +  + find the index of a branch from its id in handle, and check it is
2154 +    still exist in aufs.
2155 +  + 1st level: get the inode number from handle and search it in cache.
2156 +  + 2nd level: if not found in cache, get the parent inode number from
2157 +    the handle and search it in cache. and then open the found parent
2158 +    dir, find the matching inode number by vfs_readdir() and get its
2159 +    name, and call lookup_one_len() for the target dentry.
2160 +  + 3rd level: if the parent dir is not cached, call
2161 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2162 +    build a pathname of it, convert it a pathname in aufs, call
2163 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2164 +    the 2nd level.
2165 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2166 +    for every branch, but not itself. to get this, (currently) aufs
2167 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2168 +    idea, but I didn't get other approach.
2169 +  + test the generation of the gotten inode.
2170 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2171 +  convert it into ESTALE for NFSD.
2172 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2173 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2174 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2175 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2176 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2016-01-13 20:11:11.663093444 +0100
2177 @@ -0,0 +1,52 @@
2178 +
2179 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2180 +# 
2181 +# This program is free software; you can redistribute it and/or modify
2182 +# it under the terms of the GNU General Public License as published by
2183 +# the Free Software Foundation; either version 2 of the License, or
2184 +# (at your option) any later version.
2185 +# 
2186 +# This program is distributed in the hope that it will be useful,
2187 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2188 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2189 +# GNU General Public License for more details.
2190 +# 
2191 +# You should have received a copy of the GNU General Public License
2192 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2193 +
2194 +Show Whiteout Mode (shwh)
2195 +----------------------------------------------------------------------
2196 +Generally aufs hides the name of whiteouts. But in some cases, to show
2197 +them is very useful for users. For instance, creating a new middle layer
2198 +(branch) by merging existing layers.
2199 +
2200 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2201 +When you have three branches,
2202 +- Bottom: 'system', squashfs (underlying base system), read-only
2203 +- Middle: 'mods', squashfs, read-only
2204 +- Top: 'overlay', ram (tmpfs), read-write
2205 +
2206 +The top layer is loaded at boot time and saved at shutdown, to preserve
2207 +the changes made to the system during the session.
2208 +When larger changes have been made, or smaller changes have accumulated,
2209 +the size of the saved top layer data grows. At this point, it would be
2210 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2211 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2212 +restoring save and load speed.
2213 +
2214 +This merging is simplified by the use of another aufs mount, of just the
2215 +two overlay branches using the 'shwh' option.
2216 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2217 +       aufs /livesys/merge_union
2218 +
2219 +A merged view of these two branches is then available at
2220 +/livesys/merge_union, and the new feature is that the whiteouts are
2221 +visible!
2222 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2223 +writing to all branches. Also the default mode for all branches is 'ro'.
2224 +It is now possible to save the combined contents of the two overlay
2225 +branches to a new squashfs, e.g.:
2226 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2227 +
2228 +This new squashfs archive can be stored on the boot device and the
2229 +initramfs will use it to replace the old one at the next boot.
2230 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2231 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2232 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2016-01-13 20:11:11.663093444 +0100
2233 @@ -0,0 +1,47 @@
2234 +
2235 +# Copyright (C) 2010-2015 Junjiro R. Okajima
2236 +#
2237 +# This program is free software; you can redistribute it and/or modify
2238 +# it under the terms of the GNU General Public License as published by
2239 +# the Free Software Foundation; either version 2 of the License, or
2240 +# (at your option) any later version.
2241 +#
2242 +# This program is distributed in the hope that it will be useful,
2243 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2244 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2245 +# GNU General Public License for more details.
2246 +#
2247 +# You should have received a copy of the GNU General Public License
2248 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2249 +
2250 +Dynamically customizable FS operations
2251 +----------------------------------------------------------------------
2252 +Generally FS operations (struct inode_operations, struct
2253 +address_space_operations, struct file_operations, etc.) are defined as
2254 +"static const", but it never means that FS have only one set of
2255 +operation. Some FS have multiple sets of them. For instance, ext2 has
2256 +three sets, one for XIP, for NOBH, and for normal.
2257 +Since aufs overrides and redirects these operations, sometimes aufs has
2258 +to change its behaviour according to the branch FS type. More importantly
2259 +VFS acts differently if a function (member in the struct) is set or
2260 +not. It means aufs should have several sets of operations and select one
2261 +among them according to the branch FS definition.
2262 +
2263 +In order to solve this problem and not to affect the behaviour of VFS,
2264 +aufs defines these operations dynamically. For instance, aufs defines
2265 +dummy direct_IO function for struct address_space_operations, but it may
2266 +not be set to the address_space_operations actually. When the branch FS
2267 +doesn't have it, aufs doesn't set it to its address_space_operations
2268 +while the function definition itself is still alive. So the behaviour
2269 +itself will not change, and it will return an error when direct_IO is
2270 +not set.
2271 +
2272 +The lifetime of these dynamically generated operation object is
2273 +maintained by aufs branch object. When the branch is removed from aufs,
2274 +the reference counter of the object is decremented. When it reaches
2275 +zero, the dynamically generated operation object will be freed.
2276 +
2277 +This approach is designed to support AIO (io_submit), Direct I/O and
2278 +XIP (DAX) mainly.
2279 +Currently this approach is applied to address_space_operations for
2280 +regular files only.
2281 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2282 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2283 +++ linux/Documentation/filesystems/aufs/README 2016-01-13 20:11:11.663093444 +0100
2284 @@ -0,0 +1,390 @@
2285 +
2286 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2287 +http://aufs.sf.net
2288 +Junjiro R. Okajima
2289 +
2290 +
2291 +0. Introduction
2292 +----------------------------------------
2293 +In the early days, aufs was entirely re-designed and re-implemented
2294 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2295 +improvements and implementations, it becomes totally different from
2296 +Unionfs while keeping the basic features.
2297 +Recently, Unionfs Version 2.x series begin taking some of the same
2298 +approaches to aufs1's.
2299 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2300 +University and his team.
2301 +
2302 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2303 +If you want older kernel version support, try aufs2-2.6.git or
2304 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2305 +
2306 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2307 +      According to Christoph Hellwig, linux rejects all union-type
2308 +      filesystems but UnionMount.
2309 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2310 +
2311 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2312 +    UnionMount, and he pointed out an issue around a directory mutex
2313 +    lock and aufs addressed it. But it is still unsure whether aufs will
2314 +    be merged (or any other union solution).
2315 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2316 +
2317 +
2318 +1. Features
2319 +----------------------------------------
2320 +- unite several directories into a single virtual filesystem. The member
2321 +  directory is called as a branch.
2322 +- you can specify the permission flags to the branch, which are 'readonly',
2323 +  'readwrite' and 'whiteout-able.'
2324 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2325 +  readonly branch are modifiable logically.
2326 +- dynamic branch manipulation, add, del.
2327 +- etc...
2328 +
2329 +Also there are many enhancements in aufs, such as:
2330 +- test only the highest one for the directory permission (dirperm1)
2331 +- copyup on open (coo=)
2332 +- 'move' policy for copy-up between two writable branches, after
2333 +  checking free space.
2334 +- xattr, acl
2335 +- readdir(3) in userspace.
2336 +- keep inode number by external inode number table
2337 +- keep the timestamps of file/dir in internal copyup operation
2338 +- seekable directory, supporting NFS readdir.
2339 +- whiteout is hardlinked in order to reduce the consumption of inodes
2340 +  on branch
2341 +- do not copyup, nor create a whiteout when it is unnecessary
2342 +- revert a single systemcall when an error occurs in aufs
2343 +- remount interface instead of ioctl
2344 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2345 +- loopback mounted filesystem as a branch
2346 +- kernel thread for removing the dir who has a plenty of whiteouts
2347 +- support copyup sparse file (a file which has a 'hole' in it)
2348 +- default permission flags for branches
2349 +- selectable permission flags for ro branch, whether whiteout can
2350 +  exist or not
2351 +- export via NFS.
2352 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2353 +- support multiple writable branches, some policies to select one
2354 +  among multiple writable branches.
2355 +- a new semantics for link(2) and rename(2) to support multiple
2356 +  writable branches.
2357 +- no glibc changes are required.
2358 +- pseudo hardlink (hardlink over branches)
2359 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2360 +  including NFS or remote filesystem branch.
2361 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2362 +- and more...
2363 +
2364 +Currently these features are dropped temporary from aufs4.
2365 +See design/08plan.txt in detail.
2366 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2367 +  (robr)
2368 +- statistics of aufs thread (/sys/fs/aufs/stat)
2369 +
2370 +Features or just an idea in the future (see also design/*.txt),
2371 +- reorder the branch index without del/re-add.
2372 +- permanent xino files for NFSD
2373 +- an option for refreshing the opened files after add/del branches
2374 +- light version, without branch manipulation. (unnecessary?)
2375 +- copyup in userspace
2376 +- inotify in userspace
2377 +- readv/writev
2378 +
2379 +
2380 +2. Download
2381 +----------------------------------------
2382 +There are three GIT trees for aufs4, aufs4-linux.git,
2383 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2384 +"aufs-util.git."
2385 +While the aufs-util is always necessary, you need either of aufs4-linux
2386 +or aufs4-standalone.
2387 +
2388 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2389 +git://git.kernel.org/.../torvalds/linux.git.
2390 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2391 +build aufs4 as an external kernel module.
2392 +Several extra patches are not included in this tree. Only
2393 +aufs4-standalone tree contains them. They are described in the later
2394 +section "Configuration and Compilation."
2395 +
2396 +On the other hand, the aufs4-standalone tree has only aufs source files
2397 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2398 +But you need to apply all aufs patches manually.
2399 +
2400 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2401 +represents the linux kernel version, "linux-4.x". For instance,
2402 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2403 +"aufs4.x-rcN" branch.
2404 +
2405 +o aufs4-linux tree
2406 +$ git clone --reference /your/linux/git/tree \
2407 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2408 +- if you don't have linux GIT tree, then remove "--reference ..."
2409 +$ cd aufs4-linux.git
2410 +$ git checkout origin/aufs4.0
2411 +
2412 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2413 +leave the patch-work to GIT.
2414 +$ cd /your/linux/git/tree
2415 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2416 +$ git fetch aufs4
2417 +$ git checkout -b my4.0 v4.0
2418 +$ (add your local change...)
2419 +$ git pull aufs4 aufs4.0
2420 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2421 +- you may need to solve some conflicts between your_changes and
2422 +  aufs4.0. in this case, git-rerere is recommended so that you can
2423 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2424 +  later in the future.
2425 +
2426 +o aufs4-standalone tree
2427 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2428 +$ cd aufs4-standalone.git
2429 +$ git checkout origin/aufs4.0
2430 +
2431 +o aufs-util tree
2432 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2433 +- note that the public aufs-util.git is on SourceForge instead of
2434 +  GitHUB.
2435 +$ cd aufs-util.git
2436 +$ git checkout origin/aufs4.0
2437 +
2438 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2439 +The minor version number, 'x' in '4.x', of aufs may not always
2440 +follow the minor version number of the kernel.
2441 +Because changes in the kernel that cause the use of a new
2442 +minor version number do not always require changes to aufs-util.
2443 +
2444 +Since aufs-util has its own minor version number, you may not be
2445 +able to find a GIT branch in aufs-util for your kernel's
2446 +exact minor version number.
2447 +In this case, you should git-checkout the branch for the
2448 +nearest lower number.
2449 +
2450 +For (an unreleased) example:
2451 +If you are using "linux-4.10" and the "aufs4.10" branch
2452 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2453 +or something numerically smaller is the branch for your kernel.
2454 +
2455 +Also you can view all branches by
2456 +       $ git branch -a
2457 +
2458 +
2459 +3. Configuration and Compilation
2460 +----------------------------------------
2461 +Make sure you have git-checkout'ed the correct branch.
2462 +
2463 +For aufs4-linux tree,
2464 +- enable CONFIG_AUFS_FS.
2465 +- set other aufs configurations if necessary.
2466 +
2467 +For aufs4-standalone tree,
2468 +There are several ways to build.
2469 +
2470 +1.
2471 +- apply ./aufs4-kbuild.patch to your kernel source files.
2472 +- apply ./aufs4-base.patch too.
2473 +- apply ./aufs4-mmap.patch too.
2474 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2475 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2476 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2477 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2478 +- enable CONFIG_AUFS_FS, you can select either
2479 +  =m or =y.
2480 +- and build your kernel as usual.
2481 +- install the built kernel.
2482 +  Note: Since linux-3.9, every filesystem module requires an alias
2483 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2484 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2485 +- install the header files too by "make headers_install" to the
2486 +  directory where you specify. By default, it is $PWD/usr.
2487 +  "make help" shows a brief note for headers_install.
2488 +- and reboot your system.
2489 +
2490 +2.
2491 +- module only (CONFIG_AUFS_FS=m).
2492 +- apply ./aufs4-base.patch to your kernel source files.
2493 +- apply ./aufs4-mmap.patch too.
2494 +- apply ./aufs4-standalone.patch too.
2495 +- build your kernel, don't forget "make headers_install", and reboot.
2496 +- edit ./config.mk and set other aufs configurations if necessary.
2497 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2498 +  every aufs configurations.
2499 +- build the module by simple "make".
2500 +  Note: Since linux-3.9, every filesystem module requires an alias
2501 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2502 +  modules.aliases file.
2503 +- you can specify ${KDIR} make variable which points to your kernel
2504 +  source tree.
2505 +- install the files
2506 +  + run "make install" to install the aufs module, or copy the built
2507 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2508 +  + run "make install_headers" (instead of headers_install) to install
2509 +    the modified aufs header file (you can specify DESTDIR which is
2510 +    available in aufs standalone version's Makefile only), or copy
2511 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2512 +    you like manually. By default, the target directory is $PWD/usr.
2513 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2514 +  kernel source tree.
2515 +
2516 +Note: The header file aufs_type.h is necessary to build aufs-util
2517 +      as well as "make headers_install" in the kernel source tree.
2518 +      headers_install is subject to be forgotten, but it is essentially
2519 +      necessary, not only for building aufs-util.
2520 +      You may not meet problems without headers_install in some older
2521 +      version though.
2522 +
2523 +And then,
2524 +- read README in aufs-util, build and install it
2525 +- note that your distribution may contain an obsoleted version of
2526 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2527 +  utilities, make sure that your compiler refers the correct aufs header
2528 +  file which is built by "make headers_install."
2529 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2530 +  then run "make install_ulib" too. And refer to the aufs manual in
2531 +  detail.
2532 +
2533 +There several other patches in aufs4-standalone.git. They are all
2534 +optional. When you meet some problems, they will help you.
2535 +- aufs4-loopback.patch
2536 +  Supports a nested loopback mount in a branch-fs. This patch is
2537 +  unnecessary until aufs produces a message like "you may want to try
2538 +  another patch for loopback file".
2539 +- vfs-ino.patch
2540 +  Modifies a system global kernel internal function get_next_ino() in
2541 +  order to stop assigning 0 for an inode-number. Not directly related to
2542 +  aufs, but recommended generally.
2543 +- tmpfs-idr.patch
2544 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2545 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2546 +  duplication of inode number, which is important for backup tools and
2547 +  other utilities. When you find aufs XINO files for tmpfs branch
2548 +  growing too much, try this patch.
2549 +- lockdep-debug.patch
2550 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2551 +  also a caller of VFS functions for branch filesystems, subclassing of
2552 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2553 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2554 +  need to apply this debug patch to expand several constant values.
2555 +  If don't know what LOCKDEP, then you don't have apply this patch.
2556 +
2557 +
2558 +4. Usage
2559 +----------------------------------------
2560 +At first, make sure aufs-util are installed, and please read the aufs
2561 +manual, aufs.5 in aufs-util.git tree.
2562 +$ man -l aufs.5
2563 +
2564 +And then,
2565 +$ mkdir /tmp/rw /tmp/aufs
2566 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2567 +
2568 +Here is another example. The result is equivalent.
2569 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2570 +  Or
2571 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2572 +# mount -o remount,append:${HOME} /tmp/aufs
2573 +
2574 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2575 +you modify a file under /tmp/aufs, the one on your home directory is
2576 +not affected, instead the same named file will be newly created under
2577 +/tmp/rw. And all of your modification to a file will be applied to
2578 +the one under /tmp/rw. This is called the file based Copy on Write
2579 +(COW) method.
2580 +Aufs mount options are described in aufs.5.
2581 +If you run chroot or something and make your aufs as a root directory,
2582 +then you need to customize the shutdown script. See the aufs manual in
2583 +detail.
2584 +
2585 +Additionally, there are some sample usages of aufs which are a
2586 +diskless system with network booting, and LiveCD over NFS.
2587 +See sample dir in CVS tree on SourceForge.
2588 +
2589 +
2590 +5. Contact
2591 +----------------------------------------
2592 +When you have any problems or strange behaviour in aufs, please let me
2593 +know with:
2594 +- /proc/mounts (instead of the output of mount(8))
2595 +- /sys/module/aufs/*
2596 +- /sys/fs/aufs/* (if you have them)
2597 +- /debug/aufs/* (if you have them)
2598 +- linux kernel version
2599 +  if your kernel is not plain, for example modified by distributor,
2600 +  the url where i can download its source is necessary too.
2601 +- aufs version which was printed at loading the module or booting the
2602 +  system, instead of the date you downloaded.
2603 +- configuration (define/undefine CONFIG_AUFS_xxx)
2604 +- kernel configuration or /proc/config.gz (if you have it)
2605 +- behaviour which you think to be incorrect
2606 +- actual operation, reproducible one is better
2607 +- mailto: aufs-users at lists.sourceforge.net
2608 +
2609 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2610 +and Feature Requests) on SourceForge. Please join and write to
2611 +aufs-users ML.
2612 +
2613 +
2614 +6. Acknowledgements
2615 +----------------------------------------
2616 +Thanks to everyone who have tried and are using aufs, whoever
2617 +have reported a bug or any feedback.
2618 +
2619 +Especially donators:
2620 +Tomas Matejicek(slax.org) made a donation (much more than once).
2621 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2622 +       scripts) is making "doubling" donations.
2623 +       Unfortunately I cannot list all of the donators, but I really
2624 +       appreciate.
2625 +       It ends Aug 2010, but the ordinary donation URL is still available.
2626 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2627 +Dai Itasaka made a donation (2007/8).
2628 +Chuck Smith made a donation (2008/4, 10 and 12).
2629 +Henk Schoneveld made a donation (2008/9).
2630 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2631 +Francois Dupoux made a donation (2008/11).
2632 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2633 +       aufs2 GIT tree (2009/2).
2634 +William Grant made a donation (2009/3).
2635 +Patrick Lane made a donation (2009/4).
2636 +The Mail Archive (mail-archive.com) made donations (2009/5).
2637 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2638 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2639 +Pavel Pronskiy made a donation (2011/2).
2640 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2641 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2642 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2643 +11).
2644 +Sam Liddicott made a donation (2011/9).
2645 +Era Scarecrow made a donation (2013/4).
2646 +Bor Ratajc made a donation (2013/4).
2647 +Alessandro Gorreta made a donation (2013/4).
2648 +POIRETTE Marc made a donation (2013/4).
2649 +Alessandro Gorreta made a donation (2013/4).
2650 +lauri kasvandik made a donation (2013/5).
2651 +"pemasu from Finland" made a donation (2013/7).
2652 +The Parted Magic Project made a donation (2013/9 and 11).
2653 +Pavel Barta made a donation (2013/10).
2654 +Nikolay Pertsev made a donation (2014/5).
2655 +James B made a donation (2014/7 and 2015/7).
2656 +Stefano Di Biase made a donation (2014/8).
2657 +Daniel Epellei made a donation (2015/1).
2658 +
2659 +Thank you very much.
2660 +Donations are always, including future donations, very important and
2661 +helpful for me to keep on developing aufs.
2662 +
2663 +
2664 +7.
2665 +----------------------------------------
2666 +If you are an experienced user, no explanation is needed. Aufs is
2667 +just a linux filesystem.
2668 +
2669 +
2670 +Enjoy!
2671 +
2672 +# Local variables: ;
2673 +# mode: text;
2674 +# End: ;
2675 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2676 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2677 +++ linux/fs/aufs/aufs.h        2016-01-13 20:11:11.666426853 +0100
2678 @@ -0,0 +1,59 @@
2679 +/*
2680 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2681 + *
2682 + * This program, aufs is free software; you can redistribute it and/or modify
2683 + * it under the terms of the GNU General Public License as published by
2684 + * the Free Software Foundation; either version 2 of the License, or
2685 + * (at your option) any later version.
2686 + *
2687 + * This program is distributed in the hope that it will be useful,
2688 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2689 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2690 + * GNU General Public License for more details.
2691 + *
2692 + * You should have received a copy of the GNU General Public License
2693 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2694 + */
2695 +
2696 +/*
2697 + * all header files
2698 + */
2699 +
2700 +#ifndef __AUFS_H__
2701 +#define __AUFS_H__
2702 +
2703 +#ifdef __KERNEL__
2704 +
2705 +#define AuStub(type, name, body, ...) \
2706 +       static inline type name(__VA_ARGS__) { body; }
2707 +
2708 +#define AuStubVoid(name, ...) \
2709 +       AuStub(void, name, , __VA_ARGS__)
2710 +#define AuStubInt0(name, ...) \
2711 +       AuStub(int, name, return 0, __VA_ARGS__)
2712 +
2713 +#include "debug.h"
2714 +
2715 +#include "branch.h"
2716 +#include "cpup.h"
2717 +#include "dcsub.h"
2718 +#include "dbgaufs.h"
2719 +#include "dentry.h"
2720 +#include "dir.h"
2721 +#include "dynop.h"
2722 +#include "file.h"
2723 +#include "fstype.h"
2724 +#include "inode.h"
2725 +#include "loop.h"
2726 +#include "module.h"
2727 +#include "opts.h"
2728 +#include "rwsem.h"
2729 +#include "spl.h"
2730 +#include "super.h"
2731 +#include "sysaufs.h"
2732 +#include "vfsub.h"
2733 +#include "whout.h"
2734 +#include "wkq.h"
2735 +
2736 +#endif /* __KERNEL__ */
2737 +#endif /* __AUFS_H__ */
2738 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2739 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2740 +++ linux/fs/aufs/branch.c      2016-01-13 20:11:11.666426853 +0100
2741 @@ -0,0 +1,1407 @@
2742 +/*
2743 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2744 + *
2745 + * This program, aufs is free software; you can redistribute it and/or modify
2746 + * it under the terms of the GNU General Public License as published by
2747 + * the Free Software Foundation; either version 2 of the License, or
2748 + * (at your option) any later version.
2749 + *
2750 + * This program is distributed in the hope that it will be useful,
2751 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2752 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2753 + * GNU General Public License for more details.
2754 + *
2755 + * You should have received a copy of the GNU General Public License
2756 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2757 + */
2758 +
2759 +/*
2760 + * branch management
2761 + */
2762 +
2763 +#include <linux/compat.h>
2764 +#include <linux/statfs.h>
2765 +#include "aufs.h"
2766 +
2767 +/*
2768 + * free a single branch
2769 + */
2770 +static void au_br_do_free(struct au_branch *br)
2771 +{
2772 +       int i;
2773 +       struct au_wbr *wbr;
2774 +       struct au_dykey **key;
2775 +
2776 +       au_hnotify_fin_br(br);
2777 +
2778 +       if (br->br_xino.xi_file)
2779 +               fput(br->br_xino.xi_file);
2780 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2781 +
2782 +       AuDebugOn(atomic_read(&br->br_count));
2783 +
2784 +       wbr = br->br_wbr;
2785 +       if (wbr) {
2786 +               for (i = 0; i < AuBrWh_Last; i++)
2787 +                       dput(wbr->wbr_wh[i]);
2788 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2789 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2790 +       }
2791 +
2792 +       if (br->br_fhsm) {
2793 +               au_br_fhsm_fin(br->br_fhsm);
2794 +               kfree(br->br_fhsm);
2795 +       }
2796 +
2797 +       key = br->br_dykey;
2798 +       for (i = 0; i < AuBrDynOp; i++, key++)
2799 +               if (*key)
2800 +                       au_dy_put(*key);
2801 +               else
2802 +                       break;
2803 +
2804 +       /* recursive lock, s_umount of branch's */
2805 +       lockdep_off();
2806 +       path_put(&br->br_path);
2807 +       lockdep_on();
2808 +       kfree(wbr);
2809 +       kfree(br);
2810 +}
2811 +
2812 +/*
2813 + * frees all branches
2814 + */
2815 +void au_br_free(struct au_sbinfo *sbinfo)
2816 +{
2817 +       aufs_bindex_t bmax;
2818 +       struct au_branch **br;
2819 +
2820 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2821 +
2822 +       bmax = sbinfo->si_bend + 1;
2823 +       br = sbinfo->si_branch;
2824 +       while (bmax--)
2825 +               au_br_do_free(*br++);
2826 +}
2827 +
2828 +/*
2829 + * find the index of a branch which is specified by @br_id.
2830 + */
2831 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2832 +{
2833 +       aufs_bindex_t bindex, bend;
2834 +
2835 +       bend = au_sbend(sb);
2836 +       for (bindex = 0; bindex <= bend; bindex++)
2837 +               if (au_sbr_id(sb, bindex) == br_id)
2838 +                       return bindex;
2839 +       return -1;
2840 +}
2841 +
2842 +/* ---------------------------------------------------------------------- */
2843 +
2844 +/*
2845 + * add a branch
2846 + */
2847 +
2848 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2849 +                       struct dentry *h_root)
2850 +{
2851 +       if (unlikely(h_adding == h_root
2852 +                    || au_test_loopback_overlap(sb, h_adding)))
2853 +               return 1;
2854 +       if (h_adding->d_sb != h_root->d_sb)
2855 +               return 0;
2856 +       return au_test_subdir(h_adding, h_root)
2857 +               || au_test_subdir(h_root, h_adding);
2858 +}
2859 +
2860 +/*
2861 + * returns a newly allocated branch. @new_nbranch is a number of branches
2862 + * after adding a branch.
2863 + */
2864 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2865 +                                    int perm)
2866 +{
2867 +       struct au_branch *add_branch;
2868 +       struct dentry *root;
2869 +       struct inode *inode;
2870 +       int err;
2871 +
2872 +       err = -ENOMEM;
2873 +       root = sb->s_root;
2874 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
2875 +       if (unlikely(!add_branch))
2876 +               goto out;
2877 +
2878 +       err = au_hnotify_init_br(add_branch, perm);
2879 +       if (unlikely(err))
2880 +               goto out_br;
2881 +
2882 +       if (au_br_writable(perm)) {
2883 +               /* may be freed separately at changing the branch permission */
2884 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
2885 +                                            GFP_NOFS);
2886 +               if (unlikely(!add_branch->br_wbr))
2887 +                       goto out_hnotify;
2888 +       }
2889 +
2890 +       if (au_br_fhsm(perm)) {
2891 +               err = au_fhsm_br_alloc(add_branch);
2892 +               if (unlikely(err))
2893 +                       goto out_wbr;
2894 +       }
2895 +
2896 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2897 +       if (!err)
2898 +               err = au_di_realloc(au_di(root), new_nbranch);
2899 +       if (!err) {
2900 +               inode = d_inode(root);
2901 +               err = au_ii_realloc(au_ii(inode), new_nbranch);
2902 +       }
2903 +       if (!err)
2904 +               return add_branch; /* success */
2905 +
2906 +out_wbr:
2907 +       kfree(add_branch->br_wbr);
2908 +out_hnotify:
2909 +       au_hnotify_fin_br(add_branch);
2910 +out_br:
2911 +       kfree(add_branch);
2912 +out:
2913 +       return ERR_PTR(err);
2914 +}
2915 +
2916 +/*
2917 + * test if the branch permission is legal or not.
2918 + */
2919 +static int test_br(struct inode *inode, int brperm, char *path)
2920 +{
2921 +       int err;
2922 +
2923 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2924 +       if (!err)
2925 +               goto out;
2926 +
2927 +       err = -EINVAL;
2928 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2929 +
2930 +out:
2931 +       return err;
2932 +}
2933 +
2934 +/*
2935 + * returns:
2936 + * 0: success, the caller will add it
2937 + * plus: success, it is already unified, the caller should ignore it
2938 + * minus: error
2939 + */
2940 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2941 +{
2942 +       int err;
2943 +       aufs_bindex_t bend, bindex;
2944 +       struct dentry *root, *h_dentry;
2945 +       struct inode *inode, *h_inode;
2946 +
2947 +       root = sb->s_root;
2948 +       bend = au_sbend(sb);
2949 +       if (unlikely(bend >= 0
2950 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2951 +               err = 1;
2952 +               if (!remount) {
2953 +                       err = -EINVAL;
2954 +                       pr_err("%s duplicated\n", add->pathname);
2955 +               }
2956 +               goto out;
2957 +       }
2958 +
2959 +       err = -ENOSPC; /* -E2BIG; */
2960 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2961 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2962 +               pr_err("number of branches exceeded %s\n", add->pathname);
2963 +               goto out;
2964 +       }
2965 +
2966 +       err = -EDOM;
2967 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2968 +               pr_err("bad index %d\n", add->bindex);
2969 +               goto out;
2970 +       }
2971 +
2972 +       inode = d_inode(add->path.dentry);
2973 +       err = -ENOENT;
2974 +       if (unlikely(!inode->i_nlink)) {
2975 +               pr_err("no existence %s\n", add->pathname);
2976 +               goto out;
2977 +       }
2978 +
2979 +       err = -EINVAL;
2980 +       if (unlikely(inode->i_sb == sb)) {
2981 +               pr_err("%s must be outside\n", add->pathname);
2982 +               goto out;
2983 +       }
2984 +
2985 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2986 +               pr_err("unsupported filesystem, %s (%s)\n",
2987 +                      add->pathname, au_sbtype(inode->i_sb));
2988 +               goto out;
2989 +       }
2990 +
2991 +       if (unlikely(inode->i_sb->s_stack_depth)) {
2992 +               pr_err("already stacked, %s (%s)\n",
2993 +                      add->pathname, au_sbtype(inode->i_sb));
2994 +               goto out;
2995 +       }
2996 +
2997 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
2998 +       if (unlikely(err))
2999 +               goto out;
3000 +
3001 +       if (bend < 0)
3002 +               return 0; /* success */
3003 +
3004 +       err = -EINVAL;
3005 +       for (bindex = 0; bindex <= bend; bindex++)
3006 +               if (unlikely(test_overlap(sb, add->path.dentry,
3007 +                                         au_h_dptr(root, bindex)))) {
3008 +                       pr_err("%s is overlapped\n", add->pathname);
3009 +                       goto out;
3010 +               }
3011 +
3012 +       err = 0;
3013 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3014 +               h_dentry = au_h_dptr(root, 0);
3015 +               h_inode = d_inode(h_dentry);
3016 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3017 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3018 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3019 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3020 +                               add->pathname,
3021 +                               i_uid_read(inode), i_gid_read(inode),
3022 +                               (inode->i_mode & S_IALLUGO),
3023 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3024 +                               (h_inode->i_mode & S_IALLUGO));
3025 +       }
3026 +
3027 +out:
3028 +       return err;
3029 +}
3030 +
3031 +/*
3032 + * initialize or clean the whiteouts for an adding branch
3033 + */
3034 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3035 +                        int new_perm)
3036 +{
3037 +       int err, old_perm;
3038 +       aufs_bindex_t bindex;
3039 +       struct mutex *h_mtx;
3040 +       struct au_wbr *wbr;
3041 +       struct au_hinode *hdir;
3042 +       struct dentry *h_dentry;
3043 +
3044 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3045 +       if (unlikely(err))
3046 +               goto out;
3047 +
3048 +       wbr = br->br_wbr;
3049 +       old_perm = br->br_perm;
3050 +       br->br_perm = new_perm;
3051 +       hdir = NULL;
3052 +       h_mtx = NULL;
3053 +       bindex = au_br_index(sb, br->br_id);
3054 +       if (0 <= bindex) {
3055 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3056 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
3057 +       } else {
3058 +               h_dentry = au_br_dentry(br);
3059 +               h_mtx = &d_inode(h_dentry)->i_mutex;
3060 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
3061 +       }
3062 +       if (!wbr)
3063 +               err = au_wh_init(br, sb);
3064 +       else {
3065 +               wbr_wh_write_lock(wbr);
3066 +               err = au_wh_init(br, sb);
3067 +               wbr_wh_write_unlock(wbr);
3068 +       }
3069 +       if (hdir)
3070 +               au_hn_imtx_unlock(hdir);
3071 +       else
3072 +               mutex_unlock(h_mtx);
3073 +       vfsub_mnt_drop_write(au_br_mnt(br));
3074 +       br->br_perm = old_perm;
3075 +
3076 +       if (!err && wbr && !au_br_writable(new_perm)) {
3077 +               kfree(wbr);
3078 +               br->br_wbr = NULL;
3079 +       }
3080 +
3081 +out:
3082 +       return err;
3083 +}
3084 +
3085 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3086 +                      int perm)
3087 +{
3088 +       int err;
3089 +       struct kstatfs kst;
3090 +       struct au_wbr *wbr;
3091 +
3092 +       wbr = br->br_wbr;
3093 +       au_rw_init(&wbr->wbr_wh_rwsem);
3094 +       atomic_set(&wbr->wbr_wh_running, 0);
3095 +
3096 +       /*
3097 +        * a limit for rmdir/rename a dir
3098 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3099 +        */
3100 +       err = vfs_statfs(&br->br_path, &kst);
3101 +       if (unlikely(err))
3102 +               goto out;
3103 +       err = -EINVAL;
3104 +       if (kst.f_namelen >= NAME_MAX)
3105 +               err = au_br_init_wh(sb, br, perm);
3106 +       else
3107 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3108 +                      au_br_dentry(br),
3109 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3110 +
3111 +out:
3112 +       return err;
3113 +}
3114 +
3115 +/* initialize a new branch */
3116 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3117 +                     struct au_opt_add *add)
3118 +{
3119 +       int err;
3120 +       struct inode *h_inode;
3121 +
3122 +       err = 0;
3123 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3124 +       br->br_perm = add->perm;
3125 +       br->br_path = add->path; /* set first, path_get() later */
3126 +       spin_lock_init(&br->br_dykey_lock);
3127 +       atomic_set(&br->br_count, 0);
3128 +       atomic_set(&br->br_xino_running, 0);
3129 +       br->br_id = au_new_br_id(sb);
3130 +       AuDebugOn(br->br_id < 0);
3131 +
3132 +       if (au_br_writable(add->perm)) {
3133 +               err = au_wbr_init(br, sb, add->perm);
3134 +               if (unlikely(err))
3135 +                       goto out_err;
3136 +       }
3137 +
3138 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3139 +               h_inode = d_inode(add->path.dentry);
3140 +               err = au_xino_br(sb, br, h_inode->i_ino,
3141 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3142 +               if (unlikely(err)) {
3143 +                       AuDebugOn(br->br_xino.xi_file);
3144 +                       goto out_err;
3145 +               }
3146 +       }
3147 +
3148 +       sysaufs_br_init(br);
3149 +       path_get(&br->br_path);
3150 +       goto out; /* success */
3151 +
3152 +out_err:
3153 +       memset(&br->br_path, 0, sizeof(br->br_path));
3154 +out:
3155 +       return err;
3156 +}
3157 +
3158 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3159 +                            struct au_branch *br, aufs_bindex_t bend,
3160 +                            aufs_bindex_t amount)
3161 +{
3162 +       struct au_branch **brp;
3163 +
3164 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3165 +
3166 +       brp = sbinfo->si_branch + bindex;
3167 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3168 +       *brp = br;
3169 +       sbinfo->si_bend++;
3170 +       if (unlikely(bend < 0))
3171 +               sbinfo->si_bend = 0;
3172 +}
3173 +
3174 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3175 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3176 +{
3177 +       struct au_hdentry *hdp;
3178 +
3179 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3180 +
3181 +       hdp = dinfo->di_hdentry + bindex;
3182 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3183 +       au_h_dentry_init(hdp);
3184 +       dinfo->di_bend++;
3185 +       if (unlikely(bend < 0))
3186 +               dinfo->di_bstart = 0;
3187 +}
3188 +
3189 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3190 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3191 +{
3192 +       struct au_hinode *hip;
3193 +
3194 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3195 +
3196 +       hip = iinfo->ii_hinode + bindex;
3197 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3198 +       hip->hi_inode = NULL;
3199 +       au_hn_init(hip);
3200 +       iinfo->ii_bend++;
3201 +       if (unlikely(bend < 0))
3202 +               iinfo->ii_bstart = 0;
3203 +}
3204 +
3205 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3206 +                        aufs_bindex_t bindex)
3207 +{
3208 +       struct dentry *root, *h_dentry;
3209 +       struct inode *root_inode, *h_inode;
3210 +       aufs_bindex_t bend, amount;
3211 +
3212 +       root = sb->s_root;
3213 +       root_inode = d_inode(root);
3214 +       bend = au_sbend(sb);
3215 +       amount = bend + 1 - bindex;
3216 +       h_dentry = au_br_dentry(br);
3217 +       au_sbilist_lock();
3218 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3219 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3220 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3221 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3222 +       h_inode = d_inode(h_dentry);
3223 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3224 +       au_sbilist_unlock();
3225 +}
3226 +
3227 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3228 +{
3229 +       int err;
3230 +       aufs_bindex_t bend, add_bindex;
3231 +       struct dentry *root, *h_dentry;
3232 +       struct inode *root_inode;
3233 +       struct au_branch *add_branch;
3234 +
3235 +       root = sb->s_root;
3236 +       root_inode = d_inode(root);
3237 +       IMustLock(root_inode);
3238 +       err = test_add(sb, add, remount);
3239 +       if (unlikely(err < 0))
3240 +               goto out;
3241 +       if (err) {
3242 +               err = 0;
3243 +               goto out; /* success */
3244 +       }
3245 +
3246 +       bend = au_sbend(sb);
3247 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3248 +       err = PTR_ERR(add_branch);
3249 +       if (IS_ERR(add_branch))
3250 +               goto out;
3251 +
3252 +       err = au_br_init(add_branch, sb, add);
3253 +       if (unlikely(err)) {
3254 +               au_br_do_free(add_branch);
3255 +               goto out;
3256 +       }
3257 +
3258 +       add_bindex = add->bindex;
3259 +       if (!remount)
3260 +               au_br_do_add(sb, add_branch, add_bindex);
3261 +       else {
3262 +               sysaufs_brs_del(sb, add_bindex);
3263 +               au_br_do_add(sb, add_branch, add_bindex);
3264 +               sysaufs_brs_add(sb, add_bindex);
3265 +       }
3266 +
3267 +       h_dentry = add->path.dentry;
3268 +       if (!add_bindex) {
3269 +               au_cpup_attr_all(root_inode, /*force*/1);
3270 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3271 +       } else
3272 +               au_add_nlink(root_inode, d_inode(h_dentry));
3273 +
3274 +       /*
3275 +        * this test/set prevents aufs from handling unnecesary notify events
3276 +        * of xino files, in case of re-adding a writable branch which was
3277 +        * once detached from aufs.
3278 +        */
3279 +       if (au_xino_brid(sb) < 0
3280 +           && au_br_writable(add_branch->br_perm)
3281 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3282 +           && add_branch->br_xino.xi_file
3283 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3284 +               au_xino_brid_set(sb, add_branch->br_id);
3285 +
3286 +out:
3287 +       return err;
3288 +}
3289 +
3290 +/* ---------------------------------------------------------------------- */
3291 +
3292 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3293 +                                      unsigned long long max __maybe_unused,
3294 +                                      void *arg)
3295 +{
3296 +       unsigned long long n;
3297 +       struct file **p, *f;
3298 +       struct au_sphlhead *files;
3299 +       struct au_finfo *finfo;
3300 +
3301 +       n = 0;
3302 +       p = a;
3303 +       files = &au_sbi(sb)->si_files;
3304 +       spin_lock(&files->spin);
3305 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3306 +               f = finfo->fi_file;
3307 +               if (file_count(f)
3308 +                   && !special_file(file_inode(f)->i_mode)) {
3309 +                       get_file(f);
3310 +                       *p++ = f;
3311 +                       n++;
3312 +                       AuDebugOn(n > max);
3313 +               }
3314 +       }
3315 +       spin_unlock(&files->spin);
3316 +
3317 +       return n;
3318 +}
3319 +
3320 +static struct file **au_farray_alloc(struct super_block *sb,
3321 +                                    unsigned long long *max)
3322 +{
3323 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
3324 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3325 +}
3326 +
3327 +static void au_farray_free(struct file **a, unsigned long long max)
3328 +{
3329 +       unsigned long long ull;
3330 +
3331 +       for (ull = 0; ull < max; ull++)
3332 +               if (a[ull])
3333 +                       fput(a[ull]);
3334 +       kvfree(a);
3335 +}
3336 +
3337 +/* ---------------------------------------------------------------------- */
3338 +
3339 +/*
3340 + * delete a branch
3341 + */
3342 +
3343 +/* to show the line number, do not make it inlined function */
3344 +#define AuVerbose(do_info, fmt, ...) do { \
3345 +       if (do_info) \
3346 +               pr_info(fmt, ##__VA_ARGS__); \
3347 +} while (0)
3348 +
3349 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
3350 +                        aufs_bindex_t bend)
3351 +{
3352 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
3353 +}
3354 +
3355 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
3356 +                        aufs_bindex_t bend)
3357 +{
3358 +       return au_test_ibusy(d_inode(dentry), bstart, bend);
3359 +}
3360 +
3361 +/*
3362 + * test if the branch is deletable or not.
3363 + */
3364 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3365 +                           unsigned int sigen, const unsigned int verbose)
3366 +{
3367 +       int err, i, j, ndentry;
3368 +       aufs_bindex_t bstart, bend;
3369 +       struct au_dcsub_pages dpages;
3370 +       struct au_dpage *dpage;
3371 +       struct dentry *d;
3372 +
3373 +       err = au_dpages_init(&dpages, GFP_NOFS);
3374 +       if (unlikely(err))
3375 +               goto out;
3376 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3377 +       if (unlikely(err))
3378 +               goto out_dpages;
3379 +
3380 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3381 +               dpage = dpages.dpages + i;
3382 +               ndentry = dpage->ndentry;
3383 +               for (j = 0; !err && j < ndentry; j++) {
3384 +                       d = dpage->dentries[j];
3385 +                       AuDebugOn(au_dcount(d) <= 0);
3386 +                       if (!au_digen_test(d, sigen)) {
3387 +                               di_read_lock_child(d, AuLock_IR);
3388 +                               if (unlikely(au_dbrange_test(d))) {
3389 +                                       di_read_unlock(d, AuLock_IR);
3390 +                                       continue;
3391 +                               }
3392 +                       } else {
3393 +                               di_write_lock_child(d);
3394 +                               if (unlikely(au_dbrange_test(d))) {
3395 +                                       di_write_unlock(d);
3396 +                                       continue;
3397 +                               }
3398 +                               err = au_reval_dpath(d, sigen);
3399 +                               if (!err)
3400 +                                       di_downgrade_lock(d, AuLock_IR);
3401 +                               else {
3402 +                                       di_write_unlock(d);
3403 +                                       break;
3404 +                               }
3405 +                       }
3406 +
3407 +                       /* AuDbgDentry(d); */
3408 +                       bstart = au_dbstart(d);
3409 +                       bend = au_dbend(d);
3410 +                       if (bstart <= bindex
3411 +                           && bindex <= bend
3412 +                           && au_h_dptr(d, bindex)
3413 +                           && au_test_dbusy(d, bstart, bend)) {
3414 +                               err = -EBUSY;
3415 +                               AuVerbose(verbose, "busy %pd\n", d);
3416 +                               AuDbgDentry(d);
3417 +                       }
3418 +                       di_read_unlock(d, AuLock_IR);
3419 +               }
3420 +       }
3421 +
3422 +out_dpages:
3423 +       au_dpages_free(&dpages);
3424 +out:
3425 +       return err;
3426 +}
3427 +
3428 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3429 +                          unsigned int sigen, const unsigned int verbose)
3430 +{
3431 +       int err;
3432 +       unsigned long long max, ull;
3433 +       struct inode *i, **array;
3434 +       aufs_bindex_t bstart, bend;
3435 +
3436 +       array = au_iarray_alloc(sb, &max);
3437 +       err = PTR_ERR(array);
3438 +       if (IS_ERR(array))
3439 +               goto out;
3440 +
3441 +       err = 0;
3442 +       AuDbg("b%d\n", bindex);
3443 +       for (ull = 0; !err && ull < max; ull++) {
3444 +               i = array[ull];
3445 +               if (unlikely(!i))
3446 +                       break;
3447 +               if (i->i_ino == AUFS_ROOT_INO)
3448 +                       continue;
3449 +
3450 +               /* AuDbgInode(i); */
3451 +               if (au_iigen(i, NULL) == sigen)
3452 +                       ii_read_lock_child(i);
3453 +               else {
3454 +                       ii_write_lock_child(i);
3455 +                       err = au_refresh_hinode_self(i);
3456 +                       au_iigen_dec(i);
3457 +                       if (!err)
3458 +                               ii_downgrade_lock(i);
3459 +                       else {
3460 +                               ii_write_unlock(i);
3461 +                               break;
3462 +                       }
3463 +               }
3464 +
3465 +               bstart = au_ibstart(i);
3466 +               bend = au_ibend(i);
3467 +               if (bstart <= bindex
3468 +                   && bindex <= bend
3469 +                   && au_h_iptr(i, bindex)
3470 +                   && au_test_ibusy(i, bstart, bend)) {
3471 +                       err = -EBUSY;
3472 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3473 +                       AuDbgInode(i);
3474 +               }
3475 +               ii_read_unlock(i);
3476 +       }
3477 +       au_iarray_free(array, max);
3478 +
3479 +out:
3480 +       return err;
3481 +}
3482 +
3483 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3484 +                             const unsigned int verbose)
3485 +{
3486 +       int err;
3487 +       unsigned int sigen;
3488 +
3489 +       sigen = au_sigen(root->d_sb);
3490 +       DiMustNoWaiters(root);
3491 +       IiMustNoWaiters(d_inode(root));
3492 +       di_write_unlock(root);
3493 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3494 +       if (!err)
3495 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3496 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3497 +
3498 +       return err;
3499 +}
3500 +
3501 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3502 +                        struct file **to_free, int *idx)
3503 +{
3504 +       int err;
3505 +       unsigned char matched, root;
3506 +       aufs_bindex_t bindex, bend;
3507 +       struct au_fidir *fidir;
3508 +       struct au_hfile *hfile;
3509 +
3510 +       err = 0;
3511 +       root = IS_ROOT(file->f_path.dentry);
3512 +       if (root) {
3513 +               get_file(file);
3514 +               to_free[*idx] = file;
3515 +               (*idx)++;
3516 +               goto out;
3517 +       }
3518 +
3519 +       matched = 0;
3520 +       fidir = au_fi(file)->fi_hdir;
3521 +       AuDebugOn(!fidir);
3522 +       bend = au_fbend_dir(file);
3523 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
3524 +               hfile = fidir->fd_hfile + bindex;
3525 +               if (!hfile->hf_file)
3526 +                       continue;
3527 +
3528 +               if (hfile->hf_br->br_id == br_id) {
3529 +                       matched = 1;
3530 +                       break;
3531 +               }
3532 +       }
3533 +       if (matched)
3534 +               err = -EBUSY;
3535 +
3536 +out:
3537 +       return err;
3538 +}
3539 +
3540 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3541 +                         struct file **to_free, int opened)
3542 +{
3543 +       int err, idx;
3544 +       unsigned long long ull, max;
3545 +       aufs_bindex_t bstart;
3546 +       struct file *file, **array;
3547 +       struct dentry *root;
3548 +       struct au_hfile *hfile;
3549 +
3550 +       array = au_farray_alloc(sb, &max);
3551 +       err = PTR_ERR(array);
3552 +       if (IS_ERR(array))
3553 +               goto out;
3554 +
3555 +       err = 0;
3556 +       idx = 0;
3557 +       root = sb->s_root;
3558 +       di_write_unlock(root);
3559 +       for (ull = 0; ull < max; ull++) {
3560 +               file = array[ull];
3561 +               if (unlikely(!file))
3562 +                       break;
3563 +
3564 +               /* AuDbg("%pD\n", file); */
3565 +               fi_read_lock(file);
3566 +               bstart = au_fbstart(file);
3567 +               if (!d_is_dir(file->f_path.dentry)) {
3568 +                       hfile = &au_fi(file)->fi_htop;
3569 +                       if (hfile->hf_br->br_id == br_id)
3570 +                               err = -EBUSY;
3571 +               } else
3572 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3573 +               fi_read_unlock(file);
3574 +               if (unlikely(err))
3575 +                       break;
3576 +       }
3577 +       di_write_lock_child(root);
3578 +       au_farray_free(array, max);
3579 +       AuDebugOn(idx > opened);
3580 +
3581 +out:
3582 +       return err;
3583 +}
3584 +
3585 +static void br_del_file(struct file **to_free, unsigned long long opened,
3586 +                         aufs_bindex_t br_id)
3587 +{
3588 +       unsigned long long ull;
3589 +       aufs_bindex_t bindex, bstart, bend, bfound;
3590 +       struct file *file;
3591 +       struct au_fidir *fidir;
3592 +       struct au_hfile *hfile;
3593 +
3594 +       for (ull = 0; ull < opened; ull++) {
3595 +               file = to_free[ull];
3596 +               if (unlikely(!file))
3597 +                       break;
3598 +
3599 +               /* AuDbg("%pD\n", file); */
3600 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3601 +               bfound = -1;
3602 +               fidir = au_fi(file)->fi_hdir;
3603 +               AuDebugOn(!fidir);
3604 +               fi_write_lock(file);
3605 +               bstart = au_fbstart(file);
3606 +               bend = au_fbend_dir(file);
3607 +               for (bindex = bstart; bindex <= bend; bindex++) {
3608 +                       hfile = fidir->fd_hfile + bindex;
3609 +                       if (!hfile->hf_file)
3610 +                               continue;
3611 +
3612 +                       if (hfile->hf_br->br_id == br_id) {
3613 +                               bfound = bindex;
3614 +                               break;
3615 +                       }
3616 +               }
3617 +               AuDebugOn(bfound < 0);
3618 +               au_set_h_fptr(file, bfound, NULL);
3619 +               if (bfound == bstart) {
3620 +                       for (bstart++; bstart <= bend; bstart++)
3621 +                               if (au_hf_dir(file, bstart)) {
3622 +                                       au_set_fbstart(file, bstart);
3623 +                                       break;
3624 +                               }
3625 +               }
3626 +               fi_write_unlock(file);
3627 +       }
3628 +}
3629 +
3630 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3631 +                            const aufs_bindex_t bindex,
3632 +                            const aufs_bindex_t bend)
3633 +{
3634 +       struct au_branch **brp, **p;
3635 +
3636 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3637 +
3638 +       brp = sbinfo->si_branch + bindex;
3639 +       if (bindex < bend)
3640 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3641 +       sbinfo->si_branch[0 + bend] = NULL;
3642 +       sbinfo->si_bend--;
3643 +
3644 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3645 +       if (p)
3646 +               sbinfo->si_branch = p;
3647 +       /* harmless error */
3648 +}
3649 +
3650 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3651 +                            const aufs_bindex_t bend)
3652 +{
3653 +       struct au_hdentry *hdp, *p;
3654 +
3655 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3656 +
3657 +       hdp = dinfo->di_hdentry;
3658 +       if (bindex < bend)
3659 +               memmove(hdp + bindex, hdp + bindex + 1,
3660 +                       sizeof(*hdp) * (bend - bindex));
3661 +       hdp[0 + bend].hd_dentry = NULL;
3662 +       dinfo->di_bend--;
3663 +
3664 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3665 +       if (p)
3666 +               dinfo->di_hdentry = p;
3667 +       /* harmless error */
3668 +}
3669 +
3670 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3671 +                            const aufs_bindex_t bend)
3672 +{
3673 +       struct au_hinode *hip, *p;
3674 +
3675 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3676 +
3677 +       hip = iinfo->ii_hinode + bindex;
3678 +       if (bindex < bend)
3679 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3680 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3681 +       au_hn_init(iinfo->ii_hinode + bend);
3682 +       iinfo->ii_bend--;
3683 +
3684 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3685 +       if (p)
3686 +               iinfo->ii_hinode = p;
3687 +       /* harmless error */
3688 +}
3689 +
3690 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3691 +                        struct au_branch *br)
3692 +{
3693 +       aufs_bindex_t bend;
3694 +       struct au_sbinfo *sbinfo;
3695 +       struct dentry *root, *h_root;
3696 +       struct inode *inode, *h_inode;
3697 +       struct au_hinode *hinode;
3698 +
3699 +       SiMustWriteLock(sb);
3700 +
3701 +       root = sb->s_root;
3702 +       inode = d_inode(root);
3703 +       sbinfo = au_sbi(sb);
3704 +       bend = sbinfo->si_bend;
3705 +
3706 +       h_root = au_h_dptr(root, bindex);
3707 +       hinode = au_hi(inode, bindex);
3708 +       h_inode = au_igrab(hinode->hi_inode);
3709 +       au_hiput(hinode);
3710 +
3711 +       au_sbilist_lock();
3712 +       au_br_do_del_brp(sbinfo, bindex, bend);
3713 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3714 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3715 +       au_sbilist_unlock();
3716 +
3717 +       dput(h_root);
3718 +       iput(h_inode);
3719 +       au_br_do_free(br);
3720 +}
3721 +
3722 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3723 +                                  unsigned long long max, void *arg)
3724 +{
3725 +       return max;
3726 +}
3727 +
3728 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3729 +{
3730 +       int err, rerr, i;
3731 +       unsigned long long opened;
3732 +       unsigned int mnt_flags;
3733 +       aufs_bindex_t bindex, bend, br_id;
3734 +       unsigned char do_wh, verbose;
3735 +       struct au_branch *br;
3736 +       struct au_wbr *wbr;
3737 +       struct dentry *root;
3738 +       struct file **to_free;
3739 +
3740 +       err = 0;
3741 +       opened = 0;
3742 +       to_free = NULL;
3743 +       root = sb->s_root;
3744 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3745 +       if (bindex < 0) {
3746 +               if (remount)
3747 +                       goto out; /* success */
3748 +               err = -ENOENT;
3749 +               pr_err("%s no such branch\n", del->pathname);
3750 +               goto out;
3751 +       }
3752 +       AuDbg("bindex b%d\n", bindex);
3753 +
3754 +       err = -EBUSY;
3755 +       mnt_flags = au_mntflags(sb);
3756 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3757 +       bend = au_sbend(sb);
3758 +       if (unlikely(!bend)) {
3759 +               AuVerbose(verbose, "no more branches left\n");
3760 +               goto out;
3761 +       }
3762 +       br = au_sbr(sb, bindex);
3763 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3764 +
3765 +       br_id = br->br_id;
3766 +       opened = atomic_read(&br->br_count);
3767 +       if (unlikely(opened)) {
3768 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3769 +               err = PTR_ERR(to_free);
3770 +               if (IS_ERR(to_free))
3771 +                       goto out;
3772 +
3773 +               err = test_file_busy(sb, br_id, to_free, opened);
3774 +               if (unlikely(err)) {
3775 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3776 +                       goto out;
3777 +               }
3778 +       }
3779 +
3780 +       wbr = br->br_wbr;
3781 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3782 +       if (do_wh) {
3783 +               /* instead of WbrWhMustWriteLock(wbr) */
3784 +               SiMustWriteLock(sb);
3785 +               for (i = 0; i < AuBrWh_Last; i++) {
3786 +                       dput(wbr->wbr_wh[i]);
3787 +                       wbr->wbr_wh[i] = NULL;
3788 +               }
3789 +       }
3790 +
3791 +       err = test_children_busy(root, bindex, verbose);
3792 +       if (unlikely(err)) {
3793 +               if (do_wh)
3794 +                       goto out_wh;
3795 +               goto out;
3796 +       }
3797 +
3798 +       err = 0;
3799 +       if (to_free) {
3800 +               /*
3801 +                * now we confirmed the branch is deletable.
3802 +                * let's free the remaining opened dirs on the branch.
3803 +                */
3804 +               di_write_unlock(root);
3805 +               br_del_file(to_free, opened, br_id);
3806 +               di_write_lock_child(root);
3807 +       }
3808 +
3809 +       if (!remount)
3810 +               au_br_do_del(sb, bindex, br);
3811 +       else {
3812 +               sysaufs_brs_del(sb, bindex);
3813 +               au_br_do_del(sb, bindex, br);
3814 +               sysaufs_brs_add(sb, bindex);
3815 +       }
3816 +
3817 +       if (!bindex) {
3818 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3819 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3820 +       } else
3821 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3822 +       if (au_opt_test(mnt_flags, PLINK))
3823 +               au_plink_half_refresh(sb, br_id);
3824 +
3825 +       if (au_xino_brid(sb) == br_id)
3826 +               au_xino_brid_set(sb, -1);
3827 +       goto out; /* success */
3828 +
3829 +out_wh:
3830 +       /* revert */
3831 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3832 +       if (rerr)
3833 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3834 +                       del->pathname, rerr);
3835 +out:
3836 +       if (to_free)
3837 +               au_farray_free(to_free, opened);
3838 +       return err;
3839 +}
3840 +
3841 +/* ---------------------------------------------------------------------- */
3842 +
3843 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3844 +{
3845 +       int err;
3846 +       aufs_bindex_t bstart, bend;
3847 +       struct aufs_ibusy ibusy;
3848 +       struct inode *inode, *h_inode;
3849 +
3850 +       err = -EPERM;
3851 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3852 +               goto out;
3853 +
3854 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3855 +       if (!err)
3856 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3857 +       if (unlikely(err)) {
3858 +               err = -EFAULT;
3859 +               AuTraceErr(err);
3860 +               goto out;
3861 +       }
3862 +
3863 +       err = -EINVAL;
3864 +       si_read_lock(sb, AuLock_FLUSH);
3865 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3866 +               goto out_unlock;
3867 +
3868 +       err = 0;
3869 +       ibusy.h_ino = 0; /* invalid */
3870 +       inode = ilookup(sb, ibusy.ino);
3871 +       if (!inode
3872 +           || inode->i_ino == AUFS_ROOT_INO
3873 +           || is_bad_inode(inode))
3874 +               goto out_unlock;
3875 +
3876 +       ii_read_lock_child(inode);
3877 +       bstart = au_ibstart(inode);
3878 +       bend = au_ibend(inode);
3879 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3880 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3881 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3882 +                       ibusy.h_ino = h_inode->i_ino;
3883 +       }
3884 +       ii_read_unlock(inode);
3885 +       iput(inode);
3886 +
3887 +out_unlock:
3888 +       si_read_unlock(sb);
3889 +       if (!err) {
3890 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3891 +               if (unlikely(err)) {
3892 +                       err = -EFAULT;
3893 +                       AuTraceErr(err);
3894 +               }
3895 +       }
3896 +out:
3897 +       return err;
3898 +}
3899 +
3900 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3901 +{
3902 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3903 +}
3904 +
3905 +#ifdef CONFIG_COMPAT
3906 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3907 +{
3908 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3909 +}
3910 +#endif
3911 +
3912 +/* ---------------------------------------------------------------------- */
3913 +
3914 +/*
3915 + * change a branch permission
3916 + */
3917 +
3918 +static void au_warn_ima(void)
3919 +{
3920 +#ifdef CONFIG_IMA
3921 +       /* since it doesn't support mark_files_ro() */
3922 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3923 +#endif
3924 +}
3925 +
3926 +static int do_need_sigen_inc(int a, int b)
3927 +{
3928 +       return au_br_whable(a) && !au_br_whable(b);
3929 +}
3930 +
3931 +static int need_sigen_inc(int old, int new)
3932 +{
3933 +       return do_need_sigen_inc(old, new)
3934 +               || do_need_sigen_inc(new, old);
3935 +}
3936 +
3937 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3938 +{
3939 +       int err, do_warn;
3940 +       unsigned int mnt_flags;
3941 +       unsigned long long ull, max;
3942 +       aufs_bindex_t br_id;
3943 +       unsigned char verbose, writer;
3944 +       struct file *file, *hf, **array;
3945 +       struct au_hfile *hfile;
3946 +
3947 +       mnt_flags = au_mntflags(sb);
3948 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3949 +
3950 +       array = au_farray_alloc(sb, &max);
3951 +       err = PTR_ERR(array);
3952 +       if (IS_ERR(array))
3953 +               goto out;
3954 +
3955 +       do_warn = 0;
3956 +       br_id = au_sbr_id(sb, bindex);
3957 +       for (ull = 0; ull < max; ull++) {
3958 +               file = array[ull];
3959 +               if (unlikely(!file))
3960 +                       break;
3961 +
3962 +               /* AuDbg("%pD\n", file); */
3963 +               fi_read_lock(file);
3964 +               if (unlikely(au_test_mmapped(file))) {
3965 +                       err = -EBUSY;
3966 +                       AuVerbose(verbose, "mmapped %pD\n", file);
3967 +                       AuDbgFile(file);
3968 +                       FiMustNoWaiters(file);
3969 +                       fi_read_unlock(file);
3970 +                       goto out_array;
3971 +               }
3972 +
3973 +               hfile = &au_fi(file)->fi_htop;
3974 +               hf = hfile->hf_file;
3975 +               if (!d_is_reg(file->f_path.dentry)
3976 +                   || !(file->f_mode & FMODE_WRITE)
3977 +                   || hfile->hf_br->br_id != br_id
3978 +                   || !(hf->f_mode & FMODE_WRITE))
3979 +                       array[ull] = NULL;
3980 +               else {
3981 +                       do_warn = 1;
3982 +                       get_file(file);
3983 +               }
3984 +
3985 +               FiMustNoWaiters(file);
3986 +               fi_read_unlock(file);
3987 +               fput(file);
3988 +       }
3989 +
3990 +       err = 0;
3991 +       if (do_warn)
3992 +               au_warn_ima();
3993 +
3994 +       for (ull = 0; ull < max; ull++) {
3995 +               file = array[ull];
3996 +               if (!file)
3997 +                       continue;
3998 +
3999 +               /* todo: already flushed? */
4000 +               /*
4001 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4002 +                * approach which resets f_mode and calls mnt_drop_write() and
4003 +                * file_release_write() for each file, because the branch
4004 +                * attribute in aufs world is totally different from the native
4005 +                * fs rw/ro mode.
4006 +               */
4007 +               /* fi_read_lock(file); */
4008 +               hfile = &au_fi(file)->fi_htop;
4009 +               hf = hfile->hf_file;
4010 +               /* fi_read_unlock(file); */
4011 +               spin_lock(&hf->f_lock);
4012 +               writer = !!(hf->f_mode & FMODE_WRITER);
4013 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4014 +               spin_unlock(&hf->f_lock);
4015 +               if (writer) {
4016 +                       put_write_access(file_inode(hf));
4017 +                       __mnt_drop_write(hf->f_path.mnt);
4018 +               }
4019 +       }
4020 +
4021 +out_array:
4022 +       au_farray_free(array, max);
4023 +out:
4024 +       AuTraceErr(err);
4025 +       return err;
4026 +}
4027 +
4028 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4029 +             int *do_refresh)
4030 +{
4031 +       int err, rerr;
4032 +       aufs_bindex_t bindex;
4033 +       struct dentry *root;
4034 +       struct au_branch *br;
4035 +       struct au_br_fhsm *bf;
4036 +
4037 +       root = sb->s_root;
4038 +       bindex = au_find_dbindex(root, mod->h_root);
4039 +       if (bindex < 0) {
4040 +               if (remount)
4041 +                       return 0; /* success */
4042 +               err = -ENOENT;
4043 +               pr_err("%s no such branch\n", mod->path);
4044 +               goto out;
4045 +       }
4046 +       AuDbg("bindex b%d\n", bindex);
4047 +
4048 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4049 +       if (unlikely(err))
4050 +               goto out;
4051 +
4052 +       br = au_sbr(sb, bindex);
4053 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4054 +       if (br->br_perm == mod->perm)
4055 +               return 0; /* success */
4056 +
4057 +       /* pre-allocate for non-fhsm --> fhsm */
4058 +       bf = NULL;
4059 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4060 +               err = au_fhsm_br_alloc(br);
4061 +               if (unlikely(err))
4062 +                       goto out;
4063 +               bf = br->br_fhsm;
4064 +               br->br_fhsm = NULL;
4065 +       }
4066 +
4067 +       if (au_br_writable(br->br_perm)) {
4068 +               /* remove whiteout base */
4069 +               err = au_br_init_wh(sb, br, mod->perm);
4070 +               if (unlikely(err))
4071 +                       goto out_bf;
4072 +
4073 +               if (!au_br_writable(mod->perm)) {
4074 +                       /* rw --> ro, file might be mmapped */
4075 +                       DiMustNoWaiters(root);
4076 +                       IiMustNoWaiters(d_inode(root));
4077 +                       di_write_unlock(root);
4078 +                       err = au_br_mod_files_ro(sb, bindex);
4079 +                       /* aufs_write_lock() calls ..._child() */
4080 +                       di_write_lock_child(root);
4081 +
4082 +                       if (unlikely(err)) {
4083 +                               rerr = -ENOMEM;
4084 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4085 +                                                    GFP_NOFS);
4086 +                               if (br->br_wbr)
4087 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4088 +                               if (unlikely(rerr)) {
4089 +                                       AuIOErr("nested error %d (%d)\n",
4090 +                                               rerr, err);
4091 +                                       br->br_perm = mod->perm;
4092 +                               }
4093 +                       }
4094 +               }
4095 +       } else if (au_br_writable(mod->perm)) {
4096 +               /* ro --> rw */
4097 +               err = -ENOMEM;
4098 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4099 +               if (br->br_wbr) {
4100 +                       err = au_wbr_init(br, sb, mod->perm);
4101 +                       if (unlikely(err)) {
4102 +                               kfree(br->br_wbr);
4103 +                               br->br_wbr = NULL;
4104 +                       }
4105 +               }
4106 +       }
4107 +       if (unlikely(err))
4108 +               goto out_bf;
4109 +
4110 +       if (au_br_fhsm(br->br_perm)) {
4111 +               if (!au_br_fhsm(mod->perm)) {
4112 +                       /* fhsm --> non-fhsm */
4113 +                       au_br_fhsm_fin(br->br_fhsm);
4114 +                       kfree(br->br_fhsm);
4115 +                       br->br_fhsm = NULL;
4116 +               }
4117 +       } else if (au_br_fhsm(mod->perm))
4118 +               /* non-fhsm --> fhsm */
4119 +               br->br_fhsm = bf;
4120 +
4121 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4122 +       br->br_perm = mod->perm;
4123 +       goto out; /* success */
4124 +
4125 +out_bf:
4126 +       kfree(bf);
4127 +out:
4128 +       AuTraceErr(err);
4129 +       return err;
4130 +}
4131 +
4132 +/* ---------------------------------------------------------------------- */
4133 +
4134 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4135 +{
4136 +       int err;
4137 +       struct kstatfs kstfs;
4138 +
4139 +       err = vfs_statfs(&br->br_path, &kstfs);
4140 +       if (!err) {
4141 +               stfs->f_blocks = kstfs.f_blocks;
4142 +               stfs->f_bavail = kstfs.f_bavail;
4143 +               stfs->f_files = kstfs.f_files;
4144 +               stfs->f_ffree = kstfs.f_ffree;
4145 +       }
4146 +
4147 +       return err;
4148 +}
4149 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4150 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4151 +++ linux/fs/aufs/branch.h      2016-01-13 20:11:11.666426853 +0100
4152 @@ -0,0 +1,279 @@
4153 +/*
4154 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4155 + *
4156 + * This program, aufs is free software; you can redistribute it and/or modify
4157 + * it under the terms of the GNU General Public License as published by
4158 + * the Free Software Foundation; either version 2 of the License, or
4159 + * (at your option) any later version.
4160 + *
4161 + * This program is distributed in the hope that it will be useful,
4162 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4163 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4164 + * GNU General Public License for more details.
4165 + *
4166 + * You should have received a copy of the GNU General Public License
4167 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4168 + */
4169 +
4170 +/*
4171 + * branch filesystems and xino for them
4172 + */
4173 +
4174 +#ifndef __AUFS_BRANCH_H__
4175 +#define __AUFS_BRANCH_H__
4176 +
4177 +#ifdef __KERNEL__
4178 +
4179 +#include <linux/mount.h>
4180 +#include "dynop.h"
4181 +#include "rwsem.h"
4182 +#include "super.h"
4183 +
4184 +/* ---------------------------------------------------------------------- */
4185 +
4186 +/* a xino file */
4187 +struct au_xino_file {
4188 +       struct file             *xi_file;
4189 +       struct mutex            xi_nondir_mtx;
4190 +
4191 +       /* todo: make xino files an array to support huge inode number */
4192 +
4193 +#ifdef CONFIG_DEBUG_FS
4194 +       struct dentry            *xi_dbgaufs;
4195 +#endif
4196 +};
4197 +
4198 +/* File-based Hierarchical Storage Management */
4199 +struct au_br_fhsm {
4200 +#ifdef CONFIG_AUFS_FHSM
4201 +       struct mutex            bf_lock;
4202 +       unsigned long           bf_jiffy;
4203 +       struct aufs_stfs        bf_stfs;
4204 +       int                     bf_readable;
4205 +#endif
4206 +};
4207 +
4208 +/* members for writable branch only */
4209 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4210 +struct au_wbr {
4211 +       struct au_rwsem         wbr_wh_rwsem;
4212 +       struct dentry           *wbr_wh[AuBrWh_Last];
4213 +       atomic_t                wbr_wh_running;
4214 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4215 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4216 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4217 +
4218 +       /* mfs mode */
4219 +       unsigned long long      wbr_bytes;
4220 +};
4221 +
4222 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4223 +#define AuBrDynOp (AuDyLast * 4)
4224 +
4225 +#ifdef CONFIG_AUFS_HFSNOTIFY
4226 +/* support for asynchronous destruction */
4227 +struct au_br_hfsnotify {
4228 +       struct fsnotify_group   *hfsn_group;
4229 +};
4230 +#endif
4231 +
4232 +/* sysfs entries */
4233 +struct au_brsysfs {
4234 +       char                    name[16];
4235 +       struct attribute        attr;
4236 +};
4237 +
4238 +enum {
4239 +       AuBrSysfs_BR,
4240 +       AuBrSysfs_BRID,
4241 +       AuBrSysfs_Last
4242 +};
4243 +
4244 +/* protected by superblock rwsem */
4245 +struct au_branch {
4246 +       struct au_xino_file     br_xino;
4247 +
4248 +       aufs_bindex_t           br_id;
4249 +
4250 +       int                     br_perm;
4251 +       struct path             br_path;
4252 +       spinlock_t              br_dykey_lock;
4253 +       struct au_dykey         *br_dykey[AuBrDynOp];
4254 +       atomic_t                br_count;
4255 +
4256 +       struct au_wbr           *br_wbr;
4257 +       struct au_br_fhsm       *br_fhsm;
4258 +
4259 +       /* xino truncation */
4260 +       atomic_t                br_xino_running;
4261 +
4262 +#ifdef CONFIG_AUFS_HFSNOTIFY
4263 +       struct au_br_hfsnotify  *br_hfsn;
4264 +#endif
4265 +
4266 +#ifdef CONFIG_SYSFS
4267 +       /* entries under sysfs per mount-point */
4268 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4269 +#endif
4270 +};
4271 +
4272 +/* ---------------------------------------------------------------------- */
4273 +
4274 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4275 +{
4276 +       return br->br_path.mnt;
4277 +}
4278 +
4279 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4280 +{
4281 +       return br->br_path.dentry;
4282 +}
4283 +
4284 +static inline struct super_block *au_br_sb(struct au_branch *br)
4285 +{
4286 +       return au_br_mnt(br)->mnt_sb;
4287 +}
4288 +
4289 +static inline int au_br_rdonly(struct au_branch *br)
4290 +{
4291 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4292 +               || !au_br_writable(br->br_perm))
4293 +               ? -EROFS : 0;
4294 +}
4295 +
4296 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4297 +{
4298 +#ifdef CONFIG_AUFS_HNOTIFY
4299 +       return !(brperm & AuBrPerm_RR);
4300 +#else
4301 +       return 0;
4302 +#endif
4303 +}
4304 +
4305 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4306 +{
4307 +       int err, exec_flag;
4308 +
4309 +       err = 0;
4310 +       exec_flag = oflag & __FMODE_EXEC;
4311 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4312 +               err = -EACCES;
4313 +
4314 +       return err;
4315 +}
4316 +
4317 +/* ---------------------------------------------------------------------- */
4318 +
4319 +/* branch.c */
4320 +struct au_sbinfo;
4321 +void au_br_free(struct au_sbinfo *sinfo);
4322 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4323 +struct au_opt_add;
4324 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4325 +struct au_opt_del;
4326 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4327 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4328 +#ifdef CONFIG_COMPAT
4329 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4330 +#endif
4331 +struct au_opt_mod;
4332 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4333 +             int *do_refresh);
4334 +struct aufs_stfs;
4335 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4336 +
4337 +/* xino.c */
4338 +static const loff_t au_loff_max = LLONG_MAX;
4339 +
4340 +int au_xib_trunc(struct super_block *sb);
4341 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4342 +                  loff_t *pos);
4343 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4344 +                   size_t size, loff_t *pos);
4345 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4346 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4347 +ino_t au_xino_new_ino(struct super_block *sb);
4348 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4349 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4350 +                 ino_t ino);
4351 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4352 +                ino_t *ino);
4353 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4354 +              struct file *base_file, int do_test);
4355 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4356 +
4357 +struct au_opt_xino;
4358 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4359 +void au_xino_clr(struct super_block *sb);
4360 +struct file *au_xino_def(struct super_block *sb);
4361 +int au_xino_path(struct seq_file *seq, struct file *file);
4362 +
4363 +/* ---------------------------------------------------------------------- */
4364 +
4365 +/* Superblock to branch */
4366 +static inline
4367 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4368 +{
4369 +       return au_sbr(sb, bindex)->br_id;
4370 +}
4371 +
4372 +static inline
4373 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4374 +{
4375 +       return au_br_mnt(au_sbr(sb, bindex));
4376 +}
4377 +
4378 +static inline
4379 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4380 +{
4381 +       return au_br_sb(au_sbr(sb, bindex));
4382 +}
4383 +
4384 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4385 +{
4386 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4387 +}
4388 +
4389 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4390 +{
4391 +       return au_sbr(sb, bindex)->br_perm;
4392 +}
4393 +
4394 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4395 +{
4396 +       return au_br_whable(au_sbr_perm(sb, bindex));
4397 +}
4398 +
4399 +/* ---------------------------------------------------------------------- */
4400 +
4401 +/*
4402 + * wbr_wh_read_lock, wbr_wh_write_lock
4403 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4404 + */
4405 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4406 +
4407 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4408 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4409 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4410 +
4411 +/* ---------------------------------------------------------------------- */
4412 +
4413 +#ifdef CONFIG_AUFS_FHSM
4414 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4415 +{
4416 +       mutex_init(&brfhsm->bf_lock);
4417 +       brfhsm->bf_jiffy = 0;
4418 +       brfhsm->bf_readable = 0;
4419 +}
4420 +
4421 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4422 +{
4423 +       mutex_destroy(&brfhsm->bf_lock);
4424 +}
4425 +#else
4426 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4427 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4428 +#endif
4429 +
4430 +#endif /* __KERNEL__ */
4431 +#endif /* __AUFS_BRANCH_H__ */
4432 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4433 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4434 +++ linux/fs/aufs/conf.mk       2016-01-13 20:11:11.666426853 +0100
4435 @@ -0,0 +1,38 @@
4436 +
4437 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4438 +
4439 +define AuConf
4440 +ifdef ${1}
4441 +AuConfStr += ${1}=${${1}}
4442 +endif
4443 +endef
4444 +
4445 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4446 +       SBILIST \
4447 +       HNOTIFY HFSNOTIFY \
4448 +       EXPORT INO_T_64 \
4449 +       XATTR \
4450 +       FHSM \
4451 +       RDU \
4452 +       SHWH \
4453 +       BR_RAMFS \
4454 +       BR_FUSE POLL \
4455 +       BR_HFSPLUS \
4456 +       BDEV_LOOP \
4457 +       DEBUG MAGIC_SYSRQ
4458 +$(foreach i, ${AuConfAll}, \
4459 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4460 +
4461 +AuConfName = ${obj}/conf.str
4462 +${AuConfName}.tmp: FORCE
4463 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4464 +${AuConfName}: ${AuConfName}.tmp
4465 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4466 +       echo '  GEN    ' $@; \
4467 +       cp -p $< $@; \
4468 +       }
4469 +FORCE:
4470 +clean-files += ${AuConfName} ${AuConfName}.tmp
4471 +${obj}/sysfs.o: ${AuConfName}
4472 +
4473 +-include ${srctree}/${src}/conf_priv.mk
4474 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4475 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4476 +++ linux/fs/aufs/cpup.c        2016-01-13 20:11:11.666426853 +0100
4477 @@ -0,0 +1,1319 @@
4478 +/*
4479 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4480 + *
4481 + * This program, aufs is free software; you can redistribute it and/or modify
4482 + * it under the terms of the GNU General Public License as published by
4483 + * the Free Software Foundation; either version 2 of the License, or
4484 + * (at your option) any later version.
4485 + *
4486 + * This program is distributed in the hope that it will be useful,
4487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4489 + * GNU General Public License for more details.
4490 + *
4491 + * You should have received a copy of the GNU General Public License
4492 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4493 + */
4494 +
4495 +/*
4496 + * copy-up functions, see wbr_policy.c for copy-down
4497 + */
4498 +
4499 +#include <linux/fs_stack.h>
4500 +#include <linux/mm.h>
4501 +#include "aufs.h"
4502 +
4503 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4504 +{
4505 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4506 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4507 +
4508 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4509 +
4510 +       dst->i_flags |= iflags & ~mask;
4511 +       if (au_test_fs_notime(dst->i_sb))
4512 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4513 +}
4514 +
4515 +void au_cpup_attr_timesizes(struct inode *inode)
4516 +{
4517 +       struct inode *h_inode;
4518 +
4519 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4520 +       fsstack_copy_attr_times(inode, h_inode);
4521 +       fsstack_copy_inode_size(inode, h_inode);
4522 +}
4523 +
4524 +void au_cpup_attr_nlink(struct inode *inode, int force)
4525 +{
4526 +       struct inode *h_inode;
4527 +       struct super_block *sb;
4528 +       aufs_bindex_t bindex, bend;
4529 +
4530 +       sb = inode->i_sb;
4531 +       bindex = au_ibstart(inode);
4532 +       h_inode = au_h_iptr(inode, bindex);
4533 +       if (!force
4534 +           && !S_ISDIR(h_inode->i_mode)
4535 +           && au_opt_test(au_mntflags(sb), PLINK)
4536 +           && au_plink_test(inode))
4537 +               return;
4538 +
4539 +       /*
4540 +        * 0 can happen in revalidating.
4541 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4542 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4543 +        * case.
4544 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4545 +        *       the incorrect link count.
4546 +        */
4547 +       set_nlink(inode, h_inode->i_nlink);
4548 +
4549 +       /*
4550 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4551 +        * it may includes whplink directory.
4552 +        */
4553 +       if (S_ISDIR(h_inode->i_mode)) {
4554 +               bend = au_ibend(inode);
4555 +               for (bindex++; bindex <= bend; bindex++) {
4556 +                       h_inode = au_h_iptr(inode, bindex);
4557 +                       if (h_inode)
4558 +                               au_add_nlink(inode, h_inode);
4559 +               }
4560 +       }
4561 +}
4562 +
4563 +void au_cpup_attr_changeable(struct inode *inode)
4564 +{
4565 +       struct inode *h_inode;
4566 +
4567 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4568 +       inode->i_mode = h_inode->i_mode;
4569 +       inode->i_uid = h_inode->i_uid;
4570 +       inode->i_gid = h_inode->i_gid;
4571 +       au_cpup_attr_timesizes(inode);
4572 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4573 +}
4574 +
4575 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4576 +{
4577 +       struct au_iinfo *iinfo = au_ii(inode);
4578 +
4579 +       IiMustWriteLock(inode);
4580 +
4581 +       iinfo->ii_higen = h_inode->i_generation;
4582 +       iinfo->ii_hsb1 = h_inode->i_sb;
4583 +}
4584 +
4585 +void au_cpup_attr_all(struct inode *inode, int force)
4586 +{
4587 +       struct inode *h_inode;
4588 +
4589 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4590 +       au_cpup_attr_changeable(inode);
4591 +       if (inode->i_nlink > 0)
4592 +               au_cpup_attr_nlink(inode, force);
4593 +       inode->i_rdev = h_inode->i_rdev;
4594 +       inode->i_blkbits = h_inode->i_blkbits;
4595 +       au_cpup_igen(inode, h_inode);
4596 +}
4597 +
4598 +/* ---------------------------------------------------------------------- */
4599 +
4600 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4601 +
4602 +/* keep the timestamps of the parent dir when cpup */
4603 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4604 +                   struct path *h_path)
4605 +{
4606 +       struct inode *h_inode;
4607 +
4608 +       dt->dt_dentry = dentry;
4609 +       dt->dt_h_path = *h_path;
4610 +       h_inode = d_inode(h_path->dentry);
4611 +       dt->dt_atime = h_inode->i_atime;
4612 +       dt->dt_mtime = h_inode->i_mtime;
4613 +       /* smp_mb(); */
4614 +}
4615 +
4616 +void au_dtime_revert(struct au_dtime *dt)
4617 +{
4618 +       struct iattr attr;
4619 +       int err;
4620 +
4621 +       attr.ia_atime = dt->dt_atime;
4622 +       attr.ia_mtime = dt->dt_mtime;
4623 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4624 +               | ATTR_ATIME | ATTR_ATIME_SET;
4625 +
4626 +       /* no delegation since this is a directory */
4627 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4628 +       if (unlikely(err))
4629 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4630 +}
4631 +
4632 +/* ---------------------------------------------------------------------- */
4633 +
4634 +/* internal use only */
4635 +struct au_cpup_reg_attr {
4636 +       int             valid;
4637 +       struct kstat    st;
4638 +       unsigned int    iflags; /* inode->i_flags */
4639 +};
4640 +
4641 +static noinline_for_stack
4642 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4643 +              struct au_cpup_reg_attr *h_src_attr)
4644 +{
4645 +       int err, sbits, icex;
4646 +       unsigned int mnt_flags;
4647 +       unsigned char verbose;
4648 +       struct iattr ia;
4649 +       struct path h_path;
4650 +       struct inode *h_isrc, *h_idst;
4651 +       struct kstat *h_st;
4652 +       struct au_branch *br;
4653 +
4654 +       h_path.dentry = au_h_dptr(dst, bindex);
4655 +       h_idst = d_inode(h_path.dentry);
4656 +       br = au_sbr(dst->d_sb, bindex);
4657 +       h_path.mnt = au_br_mnt(br);
4658 +       h_isrc = d_inode(h_src);
4659 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4660 +               | ATTR_ATIME | ATTR_MTIME
4661 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4662 +       if (h_src_attr && h_src_attr->valid) {
4663 +               h_st = &h_src_attr->st;
4664 +               ia.ia_uid = h_st->uid;
4665 +               ia.ia_gid = h_st->gid;
4666 +               ia.ia_atime = h_st->atime;
4667 +               ia.ia_mtime = h_st->mtime;
4668 +               if (h_idst->i_mode != h_st->mode
4669 +                   && !S_ISLNK(h_idst->i_mode)) {
4670 +                       ia.ia_valid |= ATTR_MODE;
4671 +                       ia.ia_mode = h_st->mode;
4672 +               }
4673 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4674 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4675 +       } else {
4676 +               ia.ia_uid = h_isrc->i_uid;
4677 +               ia.ia_gid = h_isrc->i_gid;
4678 +               ia.ia_atime = h_isrc->i_atime;
4679 +               ia.ia_mtime = h_isrc->i_mtime;
4680 +               if (h_idst->i_mode != h_isrc->i_mode
4681 +                   && !S_ISLNK(h_idst->i_mode)) {
4682 +                       ia.ia_valid |= ATTR_MODE;
4683 +                       ia.ia_mode = h_isrc->i_mode;
4684 +               }
4685 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4686 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4687 +       }
4688 +       /* no delegation since it is just created */
4689 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4690 +
4691 +       /* is this nfs only? */
4692 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4693 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4694 +               ia.ia_mode = h_isrc->i_mode;
4695 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4696 +       }
4697 +
4698 +       icex = br->br_perm & AuBrAttr_ICEX;
4699 +       if (!err) {
4700 +               mnt_flags = au_mntflags(dst->d_sb);
4701 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4702 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4703 +       }
4704 +
4705 +       return err;
4706 +}
4707 +
4708 +/* ---------------------------------------------------------------------- */
4709 +
4710 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4711 +                          char *buf, unsigned long blksize)
4712 +{
4713 +       int err;
4714 +       size_t sz, rbytes, wbytes;
4715 +       unsigned char all_zero;
4716 +       char *p, *zp;
4717 +       struct mutex *h_mtx;
4718 +       /* reduce stack usage */
4719 +       struct iattr *ia;
4720 +
4721 +       zp = page_address(ZERO_PAGE(0));
4722 +       if (unlikely(!zp))
4723 +               return -ENOMEM; /* possible? */
4724 +
4725 +       err = 0;
4726 +       all_zero = 0;
4727 +       while (len) {
4728 +               AuDbg("len %lld\n", len);
4729 +               sz = blksize;
4730 +               if (len < blksize)
4731 +                       sz = len;
4732 +
4733 +               rbytes = 0;
4734 +               /* todo: signal_pending? */
4735 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4736 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4737 +                       err = rbytes;
4738 +               }
4739 +               if (unlikely(err < 0))
4740 +                       break;
4741 +
4742 +               all_zero = 0;
4743 +               if (len >= rbytes && rbytes == blksize)
4744 +                       all_zero = !memcmp(buf, zp, rbytes);
4745 +               if (!all_zero) {
4746 +                       wbytes = rbytes;
4747 +                       p = buf;
4748 +                       while (wbytes) {
4749 +                               size_t b;
4750 +
4751 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4752 +                               err = b;
4753 +                               /* todo: signal_pending? */
4754 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4755 +                                       continue;
4756 +                               if (unlikely(err < 0))
4757 +                                       break;
4758 +                               wbytes -= b;
4759 +                               p += b;
4760 +                       }
4761 +                       if (unlikely(err < 0))
4762 +                               break;
4763 +               } else {
4764 +                       loff_t res;
4765 +
4766 +                       AuLabel(hole);
4767 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4768 +                       err = res;
4769 +                       if (unlikely(res < 0))
4770 +                               break;
4771 +               }
4772 +               len -= rbytes;
4773 +               err = 0;
4774 +       }
4775 +
4776 +       /* the last block may be a hole */
4777 +       if (!err && all_zero) {
4778 +               AuLabel(last hole);
4779 +
4780 +               err = 1;
4781 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4782 +                       /* nfs requires this step to make last hole */
4783 +                       /* is this only nfs? */
4784 +                       do {
4785 +                               /* todo: signal_pending? */
4786 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4787 +                       } while (err == -EAGAIN || err == -EINTR);
4788 +                       if (err == 1)
4789 +                               dst->f_pos--;
4790 +               }
4791 +
4792 +               if (err == 1) {
4793 +                       ia = (void *)buf;
4794 +                       ia->ia_size = dst->f_pos;
4795 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4796 +                       ia->ia_file = dst;
4797 +                       h_mtx = &file_inode(dst)->i_mutex;
4798 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4799 +                       /* no delegation since it is just created */
4800 +                       err = vfsub_notify_change(&dst->f_path, ia,
4801 +                                                 /*delegated*/NULL);
4802 +                       mutex_unlock(h_mtx);
4803 +               }
4804 +       }
4805 +
4806 +       return err;
4807 +}
4808 +
4809 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4810 +{
4811 +       int err;
4812 +       unsigned long blksize;
4813 +       unsigned char do_kfree;
4814 +       char *buf;
4815 +
4816 +       err = -ENOMEM;
4817 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
4818 +       if (!blksize || PAGE_SIZE < blksize)
4819 +               blksize = PAGE_SIZE;
4820 +       AuDbg("blksize %lu\n", blksize);
4821 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4822 +       if (do_kfree)
4823 +               buf = kmalloc(blksize, GFP_NOFS);
4824 +       else
4825 +               buf = (void *)__get_free_page(GFP_NOFS);
4826 +       if (unlikely(!buf))
4827 +               goto out;
4828 +
4829 +       if (len > (1 << 22))
4830 +               AuDbg("copying a large file %lld\n", (long long)len);
4831 +
4832 +       src->f_pos = 0;
4833 +       dst->f_pos = 0;
4834 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4835 +       if (do_kfree)
4836 +               kfree(buf);
4837 +       else
4838 +               free_page((unsigned long)buf);
4839 +
4840 +out:
4841 +       return err;
4842 +}
4843 +
4844 +/*
4845 + * to support a sparse file which is opened with O_APPEND,
4846 + * we need to close the file.
4847 + */
4848 +static int au_cp_regular(struct au_cp_generic *cpg)
4849 +{
4850 +       int err, i;
4851 +       enum { SRC, DST };
4852 +       struct {
4853 +               aufs_bindex_t bindex;
4854 +               unsigned int flags;
4855 +               struct dentry *dentry;
4856 +               int force_wr;
4857 +               struct file *file;
4858 +               void *label;
4859 +       } *f, file[] = {
4860 +               {
4861 +                       .bindex = cpg->bsrc,
4862 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4863 +                       .label = &&out
4864 +               },
4865 +               {
4866 +                       .bindex = cpg->bdst,
4867 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4868 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4869 +                       .label = &&out_src
4870 +               }
4871 +       };
4872 +       struct super_block *sb;
4873 +
4874 +       /* bsrc branch can be ro/rw. */
4875 +       sb = cpg->dentry->d_sb;
4876 +       f = file;
4877 +       for (i = 0; i < 2; i++, f++) {
4878 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4879 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4880 +                                   /*file*/NULL, f->force_wr);
4881 +               err = PTR_ERR(f->file);
4882 +               if (IS_ERR(f->file))
4883 +                       goto *f->label;
4884 +       }
4885 +
4886 +       /* try stopping to update while we copyup */
4887 +       IMustLock(d_inode(file[SRC].dentry));
4888 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4889 +
4890 +       fput(file[DST].file);
4891 +       au_sbr_put(sb, file[DST].bindex);
4892 +
4893 +out_src:
4894 +       fput(file[SRC].file);
4895 +       au_sbr_put(sb, file[SRC].bindex);
4896 +out:
4897 +       return err;
4898 +}
4899 +
4900 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4901 +                             struct au_cpup_reg_attr *h_src_attr)
4902 +{
4903 +       int err, rerr;
4904 +       loff_t l;
4905 +       struct path h_path;
4906 +       struct inode *h_src_inode, *h_dst_inode;
4907 +
4908 +       err = 0;
4909 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
4910 +       l = i_size_read(h_src_inode);
4911 +       if (cpg->len == -1 || l < cpg->len)
4912 +               cpg->len = l;
4913 +       if (cpg->len) {
4914 +               /* try stopping to update while we are referencing */
4915 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4916 +               au_pin_hdir_unlock(cpg->pin);
4917 +
4918 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4919 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4920 +               h_src_attr->iflags = h_src_inode->i_flags;
4921 +               if (!au_test_nfs(h_src_inode->i_sb))
4922 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
4923 +               else {
4924 +                       mutex_unlock(&h_src_inode->i_mutex);
4925 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
4926 +                       mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4927 +               }
4928 +               if (unlikely(err)) {
4929 +                       mutex_unlock(&h_src_inode->i_mutex);
4930 +                       goto out;
4931 +               }
4932 +               h_src_attr->valid = 1;
4933 +               err = au_cp_regular(cpg);
4934 +               mutex_unlock(&h_src_inode->i_mutex);
4935 +               rerr = au_pin_hdir_relock(cpg->pin);
4936 +               if (!err && rerr)
4937 +                       err = rerr;
4938 +       }
4939 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
4940 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
4941 +               h_dst_inode = d_inode(h_path.dentry);
4942 +               spin_lock(&h_dst_inode->i_lock);
4943 +               h_dst_inode->i_state |= I_LINKABLE;
4944 +               spin_unlock(&h_dst_inode->i_lock);
4945 +       }
4946 +
4947 +out:
4948 +       return err;
4949 +}
4950 +
4951 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4952 +                             struct inode *h_dir)
4953 +{
4954 +       int err, symlen;
4955 +       mm_segment_t old_fs;
4956 +       union {
4957 +               char *k;
4958 +               char __user *u;
4959 +       } sym;
4960 +       struct inode *h_inode = d_inode(h_src);
4961 +       const struct inode_operations *h_iop = h_inode->i_op;
4962 +
4963 +       err = -ENOSYS;
4964 +       if (unlikely(!h_iop->readlink))
4965 +               goto out;
4966 +
4967 +       err = -ENOMEM;
4968 +       sym.k = (void *)__get_free_page(GFP_NOFS);
4969 +       if (unlikely(!sym.k))
4970 +               goto out;
4971 +
4972 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
4973 +       old_fs = get_fs();
4974 +       set_fs(KERNEL_DS);
4975 +       symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
4976 +       err = symlen;
4977 +       set_fs(old_fs);
4978 +
4979 +       if (symlen > 0) {
4980 +               sym.k[symlen] = 0;
4981 +               err = vfsub_symlink(h_dir, h_path, sym.k);
4982 +       }
4983 +       free_page((unsigned long)sym.k);
4984 +
4985 +out:
4986 +       return err;
4987 +}
4988 +
4989 +static noinline_for_stack
4990 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
4991 +              struct au_cpup_reg_attr *h_src_attr)
4992 +{
4993 +       int err;
4994 +       umode_t mode;
4995 +       unsigned int mnt_flags;
4996 +       unsigned char isdir, isreg, force;
4997 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
4998 +       struct au_dtime dt;
4999 +       struct path h_path;
5000 +       struct dentry *h_src, *h_dst, *h_parent;
5001 +       struct inode *h_inode, *h_dir, *dir, *inode;
5002 +       struct super_block *sb;
5003 +
5004 +       /* bsrc branch can be ro/rw. */
5005 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5006 +       h_inode = d_inode(h_src);
5007 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5008 +
5009 +       /* try stopping to be referenced while we are creating */
5010 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5011 +       if (au_ftest_cpup(cpg->flags, RENAME))
5012 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5013 +                                 AUFS_WH_PFX_LEN));
5014 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5015 +       h_dir = d_inode(h_parent);
5016 +       IMustLock(h_dir);
5017 +       AuDebugOn(h_parent != h_dst->d_parent);
5018 +
5019 +       sb = cpg->dentry->d_sb;
5020 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5021 +       if (do_dt) {
5022 +               h_path.dentry = h_parent;
5023 +               au_dtime_store(&dt, dst_parent, &h_path);
5024 +       }
5025 +       h_path.dentry = h_dst;
5026 +
5027 +       isreg = 0;
5028 +       isdir = 0;
5029 +       mode = h_inode->i_mode;
5030 +       switch (mode & S_IFMT) {
5031 +       case S_IFREG:
5032 +               isreg = 1;
5033 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
5034 +                                  /*want_excl*/true);
5035 +               if (!err)
5036 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5037 +               break;
5038 +       case S_IFDIR:
5039 +               isdir = 1;
5040 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5041 +               if (!err) {
5042 +                       /*
5043 +                        * strange behaviour from the users view,
5044 +                        * particularry setattr case
5045 +                        */
5046 +                       dir = d_inode(dst_parent);
5047 +                       if (au_ibstart(dir) == cpg->bdst)
5048 +                               au_cpup_attr_nlink(dir, /*force*/1);
5049 +                       inode = d_inode(cpg->dentry);
5050 +                       au_cpup_attr_nlink(inode, /*force*/1);
5051 +               }
5052 +               break;
5053 +       case S_IFLNK:
5054 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5055 +               break;
5056 +       case S_IFCHR:
5057 +       case S_IFBLK:
5058 +               AuDebugOn(!capable(CAP_MKNOD));
5059 +               /*FALLTHROUGH*/
5060 +       case S_IFIFO:
5061 +       case S_IFSOCK:
5062 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5063 +               break;
5064 +       default:
5065 +               AuIOErr("Unknown inode type 0%o\n", mode);
5066 +               err = -EIO;
5067 +       }
5068 +
5069 +       mnt_flags = au_mntflags(sb);
5070 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5071 +           && !isdir
5072 +           && au_opt_test(mnt_flags, XINO)
5073 +           && (h_inode->i_nlink == 1
5074 +               || (h_inode->i_state & I_LINKABLE))
5075 +           /* todo: unnecessary? */
5076 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5077 +           && cpg->bdst < cpg->bsrc
5078 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5079 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5080 +               /* ignore this error */
5081 +
5082 +       if (!err) {
5083 +               force = 0;
5084 +               if (isreg) {
5085 +                       force = !!cpg->len;
5086 +                       if (cpg->len == -1)
5087 +                               force = !!i_size_read(h_inode);
5088 +               }
5089 +               au_fhsm_wrote(sb, cpg->bdst, force);
5090 +       }
5091 +
5092 +       if (do_dt)
5093 +               au_dtime_revert(&dt);
5094 +       return err;
5095 +}
5096 +
5097 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5098 +{
5099 +       int err;
5100 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5101 +       struct inode *h_dir;
5102 +       aufs_bindex_t bdst;
5103 +
5104 +       dentry = cpg->dentry;
5105 +       bdst = cpg->bdst;
5106 +       h_dentry = au_h_dptr(dentry, bdst);
5107 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5108 +               dget(h_dentry);
5109 +               au_set_h_dptr(dentry, bdst, NULL);
5110 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5111 +               if (!err)
5112 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5113 +               au_set_h_dptr(dentry, bdst, h_dentry);
5114 +       } else {
5115 +               err = 0;
5116 +               parent = dget_parent(dentry);
5117 +               h_parent = au_h_dptr(parent, bdst);
5118 +               dput(parent);
5119 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5120 +               if (IS_ERR(h_path->dentry))
5121 +                       err = PTR_ERR(h_path->dentry);
5122 +       }
5123 +       if (unlikely(err))
5124 +               goto out;
5125 +
5126 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5127 +       h_dir = d_inode(h_parent);
5128 +       IMustLock(h_dir);
5129 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5130 +       /* no delegation since it is just created */
5131 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
5132 +       dput(h_path->dentry);
5133 +
5134 +out:
5135 +       return err;
5136 +}
5137 +
5138 +/*
5139 + * copyup the @dentry from @bsrc to @bdst.
5140 + * the caller must set the both of lower dentries.
5141 + * @len is for truncating when it is -1 copyup the entire file.
5142 + * in link/rename cases, @dst_parent may be different from the real one.
5143 + * basic->bsrc can be larger than basic->bdst.
5144 + */
5145 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5146 +{
5147 +       int err, rerr;
5148 +       aufs_bindex_t old_ibstart;
5149 +       unsigned char isdir, plink;
5150 +       struct dentry *h_src, *h_dst, *h_parent;
5151 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5152 +       struct super_block *sb;
5153 +       struct au_branch *br;
5154 +       /* to reuduce stack size */
5155 +       struct {
5156 +               struct au_dtime dt;
5157 +               struct path h_path;
5158 +               struct au_cpup_reg_attr h_src_attr;
5159 +       } *a;
5160 +
5161 +       err = -ENOMEM;
5162 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5163 +       if (unlikely(!a))
5164 +               goto out;
5165 +       a->h_src_attr.valid = 0;
5166 +
5167 +       sb = cpg->dentry->d_sb;
5168 +       br = au_sbr(sb, cpg->bdst);
5169 +       a->h_path.mnt = au_br_mnt(br);
5170 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5171 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5172 +       h_dir = d_inode(h_parent);
5173 +       IMustLock(h_dir);
5174 +
5175 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5176 +       inode = d_inode(cpg->dentry);
5177 +
5178 +       if (!dst_parent)
5179 +               dst_parent = dget_parent(cpg->dentry);
5180 +       else
5181 +               dget(dst_parent);
5182 +
5183 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5184 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5185 +       if (dst_inode) {
5186 +               if (unlikely(!plink)) {
5187 +                       err = -EIO;
5188 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5189 +                               "but plink is disabled\n",
5190 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5191 +                       goto out_parent;
5192 +               }
5193 +
5194 +               if (dst_inode->i_nlink) {
5195 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5196 +
5197 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5198 +                       err = PTR_ERR(h_src);
5199 +                       if (IS_ERR(h_src))
5200 +                               goto out_parent;
5201 +                       if (unlikely(d_is_negative(h_src))) {
5202 +                               err = -EIO;
5203 +                               AuIOErr("i%lu exists on b%d "
5204 +                                       "but not pseudo-linked\n",
5205 +                                       inode->i_ino, cpg->bdst);
5206 +                               dput(h_src);
5207 +                               goto out_parent;
5208 +                       }
5209 +
5210 +                       if (do_dt) {
5211 +                               a->h_path.dentry = h_parent;
5212 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5213 +                       }
5214 +
5215 +                       a->h_path.dentry = h_dst;
5216 +                       delegated = NULL;
5217 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5218 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5219 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5220 +                       if (do_dt)
5221 +                               au_dtime_revert(&a->dt);
5222 +                       if (unlikely(err == -EWOULDBLOCK)) {
5223 +                               pr_warn("cannot retry for NFSv4 delegation"
5224 +                                       " for an internal link\n");
5225 +                               iput(delegated);
5226 +                       }
5227 +                       dput(h_src);
5228 +                       goto out_parent;
5229 +               } else
5230 +                       /* todo: cpup_wh_file? */
5231 +                       /* udba work */
5232 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5233 +       }
5234 +
5235 +       isdir = S_ISDIR(inode->i_mode);
5236 +       old_ibstart = au_ibstart(inode);
5237 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5238 +       if (unlikely(err))
5239 +               goto out_rev;
5240 +       dst_inode = d_inode(h_dst);
5241 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
5242 +       /* todo: necessary? */
5243 +       /* au_pin_hdir_unlock(cpg->pin); */
5244 +
5245 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5246 +       if (unlikely(err)) {
5247 +               /* todo: necessary? */
5248 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5249 +               mutex_unlock(&dst_inode->i_mutex);
5250 +               goto out_rev;
5251 +       }
5252 +
5253 +       if (cpg->bdst < old_ibstart) {
5254 +               if (S_ISREG(inode->i_mode)) {
5255 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5256 +                       if (unlikely(err)) {
5257 +                               /* ignore an error */
5258 +                               /* au_pin_hdir_relock(cpg->pin); */
5259 +                               mutex_unlock(&dst_inode->i_mutex);
5260 +                               goto out_rev;
5261 +                       }
5262 +               }
5263 +               au_set_ibstart(inode, cpg->bdst);
5264 +       } else
5265 +               au_set_ibend(inode, cpg->bdst);
5266 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5267 +                     au_hi_flags(inode, isdir));
5268 +
5269 +       /* todo: necessary? */
5270 +       /* err = au_pin_hdir_relock(cpg->pin); */
5271 +       mutex_unlock(&dst_inode->i_mutex);
5272 +       if (unlikely(err))
5273 +               goto out_rev;
5274 +
5275 +       src_inode = d_inode(h_src);
5276 +       if (!isdir
5277 +           && (src_inode->i_nlink > 1
5278 +               || src_inode->i_state & I_LINKABLE)
5279 +           && plink)
5280 +               au_plink_append(inode, cpg->bdst, h_dst);
5281 +
5282 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5283 +               a->h_path.dentry = h_dst;
5284 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5285 +       }
5286 +       if (!err)
5287 +               goto out_parent; /* success */
5288 +
5289 +       /* revert */
5290 +out_rev:
5291 +       a->h_path.dentry = h_parent;
5292 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5293 +       a->h_path.dentry = h_dst;
5294 +       rerr = 0;
5295 +       if (d_is_positive(h_dst)) {
5296 +               if (!isdir) {
5297 +                       /* no delegation since it is just created */
5298 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5299 +                                           /*delegated*/NULL, /*force*/0);
5300 +               } else
5301 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5302 +       }
5303 +       au_dtime_revert(&a->dt);
5304 +       if (rerr) {
5305 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5306 +               err = -EIO;
5307 +       }
5308 +out_parent:
5309 +       dput(dst_parent);
5310 +       kfree(a);
5311 +out:
5312 +       return err;
5313 +}
5314 +
5315 +#if 0 /* reserved */
5316 +struct au_cpup_single_args {
5317 +       int *errp;
5318 +       struct au_cp_generic *cpg;
5319 +       struct dentry *dst_parent;
5320 +};
5321 +
5322 +static void au_call_cpup_single(void *args)
5323 +{
5324 +       struct au_cpup_single_args *a = args;
5325 +
5326 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5327 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5328 +       au_pin_hdir_release(a->cpg->pin);
5329 +}
5330 +#endif
5331 +
5332 +/*
5333 + * prevent SIGXFSZ in copy-up.
5334 + * testing CAP_MKNOD is for generic fs,
5335 + * but CAP_FSETID is for xfs only, currently.
5336 + */
5337 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5338 +{
5339 +       int do_sio;
5340 +       struct super_block *sb;
5341 +       struct inode *h_dir;
5342 +
5343 +       do_sio = 0;
5344 +       sb = au_pinned_parent(pin)->d_sb;
5345 +       if (!au_wkq_test()
5346 +           && (!au_sbi(sb)->si_plink_maint_pid
5347 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5348 +               switch (mode & S_IFMT) {
5349 +               case S_IFREG:
5350 +                       /* no condition about RLIMIT_FSIZE and the file size */
5351 +                       do_sio = 1;
5352 +                       break;
5353 +               case S_IFCHR:
5354 +               case S_IFBLK:
5355 +                       do_sio = !capable(CAP_MKNOD);
5356 +                       break;
5357 +               }
5358 +               if (!do_sio)
5359 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5360 +                                 && !capable(CAP_FSETID));
5361 +               /* this workaround may be removed in the future */
5362 +               if (!do_sio) {
5363 +                       h_dir = au_pinned_h_dir(pin);
5364 +                       do_sio = h_dir->i_mode & S_ISVTX;
5365 +               }
5366 +       }
5367 +
5368 +       return do_sio;
5369 +}
5370 +
5371 +#if 0 /* reserved */
5372 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5373 +{
5374 +       int err, wkq_err;
5375 +       struct dentry *h_dentry;
5376 +
5377 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5378 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5379 +               err = au_cpup_single(cpg, dst_parent);
5380 +       else {
5381 +               struct au_cpup_single_args args = {
5382 +                       .errp           = &err,
5383 +                       .cpg            = cpg,
5384 +                       .dst_parent     = dst_parent
5385 +               };
5386 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5387 +               if (unlikely(wkq_err))
5388 +                       err = wkq_err;
5389 +       }
5390 +
5391 +       return err;
5392 +}
5393 +#endif
5394 +
5395 +/*
5396 + * copyup the @dentry from the first active lower branch to @bdst,
5397 + * using au_cpup_single().
5398 + */
5399 +static int au_cpup_simple(struct au_cp_generic *cpg)
5400 +{
5401 +       int err;
5402 +       unsigned int flags_orig;
5403 +       struct dentry *dentry;
5404 +
5405 +       AuDebugOn(cpg->bsrc < 0);
5406 +
5407 +       dentry = cpg->dentry;
5408 +       DiMustWriteLock(dentry);
5409 +
5410 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5411 +       if (!err) {
5412 +               flags_orig = cpg->flags;
5413 +               au_fset_cpup(cpg->flags, RENAME);
5414 +               err = au_cpup_single(cpg, NULL);
5415 +               cpg->flags = flags_orig;
5416 +               if (!err)
5417 +                       return 0; /* success */
5418 +
5419 +               /* revert */
5420 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5421 +               au_set_dbstart(dentry, cpg->bsrc);
5422 +       }
5423 +
5424 +       return err;
5425 +}
5426 +
5427 +struct au_cpup_simple_args {
5428 +       int *errp;
5429 +       struct au_cp_generic *cpg;
5430 +};
5431 +
5432 +static void au_call_cpup_simple(void *args)
5433 +{
5434 +       struct au_cpup_simple_args *a = args;
5435 +
5436 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5437 +       *a->errp = au_cpup_simple(a->cpg);
5438 +       au_pin_hdir_release(a->cpg->pin);
5439 +}
5440 +
5441 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5442 +{
5443 +       int err, wkq_err;
5444 +       struct dentry *dentry, *parent;
5445 +       struct file *h_file;
5446 +       struct inode *h_dir;
5447 +
5448 +       dentry = cpg->dentry;
5449 +       h_file = NULL;
5450 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5451 +               AuDebugOn(cpg->bsrc < 0);
5452 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5453 +               err = PTR_ERR(h_file);
5454 +               if (IS_ERR(h_file))
5455 +                       goto out;
5456 +       }
5457 +
5458 +       parent = dget_parent(dentry);
5459 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5460 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5461 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5462 +               err = au_cpup_simple(cpg);
5463 +       else {
5464 +               struct au_cpup_simple_args args = {
5465 +                       .errp           = &err,
5466 +                       .cpg            = cpg
5467 +               };
5468 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5469 +               if (unlikely(wkq_err))
5470 +                       err = wkq_err;
5471 +       }
5472 +
5473 +       dput(parent);
5474 +       if (h_file)
5475 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5476 +
5477 +out:
5478 +       return err;
5479 +}
5480 +
5481 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5482 +{
5483 +       aufs_bindex_t bsrc, bend;
5484 +       struct dentry *dentry, *h_dentry;
5485 +
5486 +       if (cpg->bsrc < 0) {
5487 +               dentry = cpg->dentry;
5488 +               bend = au_dbend(dentry);
5489 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
5490 +                       h_dentry = au_h_dptr(dentry, bsrc);
5491 +                       if (h_dentry) {
5492 +                               AuDebugOn(d_is_negative(h_dentry));
5493 +                               break;
5494 +                       }
5495 +               }
5496 +               AuDebugOn(bsrc > bend);
5497 +               cpg->bsrc = bsrc;
5498 +       }
5499 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5500 +       return au_do_sio_cpup_simple(cpg);
5501 +}
5502 +
5503 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5504 +{
5505 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5506 +       return au_do_sio_cpup_simple(cpg);
5507 +}
5508 +
5509 +/* ---------------------------------------------------------------------- */
5510 +
5511 +/*
5512 + * copyup the deleted file for writing.
5513 + */
5514 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5515 +                        struct file *file)
5516 +{
5517 +       int err;
5518 +       unsigned int flags_orig;
5519 +       aufs_bindex_t bsrc_orig;
5520 +       struct dentry *h_d_dst, *h_d_start;
5521 +       struct au_dinfo *dinfo;
5522 +       struct au_hdentry *hdp;
5523 +
5524 +       dinfo = au_di(cpg->dentry);
5525 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5526 +
5527 +       bsrc_orig = cpg->bsrc;
5528 +       cpg->bsrc = dinfo->di_bstart;
5529 +       hdp = dinfo->di_hdentry;
5530 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
5531 +       dinfo->di_bstart = cpg->bdst;
5532 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
5533 +       h_d_start = NULL;
5534 +       if (file) {
5535 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
5536 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
5537 +       }
5538 +       flags_orig = cpg->flags;
5539 +       cpg->flags = !AuCpup_DTIME;
5540 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5541 +       cpg->flags = flags_orig;
5542 +       if (file) {
5543 +               if (!err)
5544 +                       err = au_reopen_nondir(file);
5545 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
5546 +       }
5547 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
5548 +       dinfo->di_bstart = cpg->bsrc;
5549 +       cpg->bsrc = bsrc_orig;
5550 +
5551 +       return err;
5552 +}
5553 +
5554 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5555 +{
5556 +       int err;
5557 +       aufs_bindex_t bdst;
5558 +       struct au_dtime dt;
5559 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5560 +       struct au_branch *br;
5561 +       struct path h_path;
5562 +
5563 +       dentry = cpg->dentry;
5564 +       bdst = cpg->bdst;
5565 +       br = au_sbr(dentry->d_sb, bdst);
5566 +       parent = dget_parent(dentry);
5567 +       h_parent = au_h_dptr(parent, bdst);
5568 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5569 +       err = PTR_ERR(wh_dentry);
5570 +       if (IS_ERR(wh_dentry))
5571 +               goto out;
5572 +
5573 +       h_path.dentry = h_parent;
5574 +       h_path.mnt = au_br_mnt(br);
5575 +       au_dtime_store(&dt, parent, &h_path);
5576 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5577 +       if (unlikely(err))
5578 +               goto out_wh;
5579 +
5580 +       dget(wh_dentry);
5581 +       h_path.dentry = wh_dentry;
5582 +       if (!d_is_dir(wh_dentry)) {
5583 +               /* no delegation since it is just created */
5584 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5585 +                                  /*delegated*/NULL, /*force*/0);
5586 +       } else
5587 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5588 +       if (unlikely(err)) {
5589 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5590 +                       wh_dentry, err);
5591 +               err = -EIO;
5592 +       }
5593 +       au_dtime_revert(&dt);
5594 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5595 +
5596 +out_wh:
5597 +       dput(wh_dentry);
5598 +out:
5599 +       dput(parent);
5600 +       return err;
5601 +}
5602 +
5603 +struct au_cpup_wh_args {
5604 +       int *errp;
5605 +       struct au_cp_generic *cpg;
5606 +       struct file *file;
5607 +};
5608 +
5609 +static void au_call_cpup_wh(void *args)
5610 +{
5611 +       struct au_cpup_wh_args *a = args;
5612 +
5613 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5614 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5615 +       au_pin_hdir_release(a->cpg->pin);
5616 +}
5617 +
5618 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5619 +{
5620 +       int err, wkq_err;
5621 +       aufs_bindex_t bdst;
5622 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5623 +       struct inode *dir, *h_dir, *h_tmpdir;
5624 +       struct au_wbr *wbr;
5625 +       struct au_pin wh_pin, *pin_orig;
5626 +
5627 +       dentry = cpg->dentry;
5628 +       bdst = cpg->bdst;
5629 +       parent = dget_parent(dentry);
5630 +       dir = d_inode(parent);
5631 +       h_orph = NULL;
5632 +       h_parent = NULL;
5633 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5634 +       h_tmpdir = h_dir;
5635 +       pin_orig = NULL;
5636 +       if (!h_dir->i_nlink) {
5637 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5638 +               h_orph = wbr->wbr_orph;
5639 +
5640 +               h_parent = dget(au_h_dptr(parent, bdst));
5641 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5642 +               h_tmpdir = d_inode(h_orph);
5643 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5644 +
5645 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
5646 +               /* todo: au_h_open_pre()? */
5647 +
5648 +               pin_orig = cpg->pin;
5649 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5650 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5651 +               cpg->pin = &wh_pin;
5652 +       }
5653 +
5654 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5655 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5656 +               err = au_cpup_wh(cpg, file);
5657 +       else {
5658 +               struct au_cpup_wh_args args = {
5659 +                       .errp   = &err,
5660 +                       .cpg    = cpg,
5661 +                       .file   = file
5662 +               };
5663 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5664 +               if (unlikely(wkq_err))
5665 +                       err = wkq_err;
5666 +       }
5667 +
5668 +       if (h_orph) {
5669 +               mutex_unlock(&h_tmpdir->i_mutex);
5670 +               /* todo: au_h_open_post()? */
5671 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5672 +               au_set_h_dptr(parent, bdst, h_parent);
5673 +               AuDebugOn(!pin_orig);
5674 +               cpg->pin = pin_orig;
5675 +       }
5676 +       iput(h_dir);
5677 +       dput(parent);
5678 +
5679 +       return err;
5680 +}
5681 +
5682 +/* ---------------------------------------------------------------------- */
5683 +
5684 +/*
5685 + * generic routine for both of copy-up and copy-down.
5686 + */
5687 +/* cf. revalidate function in file.c */
5688 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5689 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5690 +                        struct au_pin *pin,
5691 +                        struct dentry *h_parent, void *arg),
5692 +              void *arg)
5693 +{
5694 +       int err;
5695 +       struct au_pin pin;
5696 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
5697 +
5698 +       err = 0;
5699 +       parent = dget_parent(dentry);
5700 +       if (IS_ROOT(parent))
5701 +               goto out;
5702 +
5703 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5704 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5705 +
5706 +       /* do not use au_dpage */
5707 +       real_parent = parent;
5708 +       while (1) {
5709 +               dput(parent);
5710 +               parent = dget_parent(dentry);
5711 +               h_parent = au_h_dptr(parent, bdst);
5712 +               if (h_parent)
5713 +                       goto out; /* success */
5714 +
5715 +               /* find top dir which is necessary to cpup */
5716 +               do {
5717 +                       d = parent;
5718 +                       dput(parent);
5719 +                       parent = dget_parent(d);
5720 +                       di_read_lock_parent3(parent, !AuLock_IR);
5721 +                       h_parent = au_h_dptr(parent, bdst);
5722 +                       di_read_unlock(parent, !AuLock_IR);
5723 +               } while (!h_parent);
5724 +
5725 +               if (d != real_parent)
5726 +                       di_write_lock_child3(d);
5727 +
5728 +               /* somebody else might create while we were sleeping */
5729 +               h_dentry = au_h_dptr(d, bdst);
5730 +               if (!h_dentry || d_is_negative(h_dentry)) {
5731 +                       if (h_dentry)
5732 +                               au_update_dbstart(d);
5733 +
5734 +                       au_pin_set_dentry(&pin, d);
5735 +                       err = au_do_pin(&pin);
5736 +                       if (!err) {
5737 +                               err = cp(d, bdst, &pin, h_parent, arg);
5738 +                               au_unpin(&pin);
5739 +                       }
5740 +               }
5741 +
5742 +               if (d != real_parent)
5743 +                       di_write_unlock(d);
5744 +               if (unlikely(err))
5745 +                       break;
5746 +       }
5747 +
5748 +out:
5749 +       dput(parent);
5750 +       return err;
5751 +}
5752 +
5753 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5754 +                      struct au_pin *pin,
5755 +                      struct dentry *h_parent __maybe_unused,
5756 +                      void *arg __maybe_unused)
5757 +{
5758 +       struct au_cp_generic cpg = {
5759 +               .dentry = dentry,
5760 +               .bdst   = bdst,
5761 +               .bsrc   = -1,
5762 +               .len    = 0,
5763 +               .pin    = pin,
5764 +               .flags  = AuCpup_DTIME
5765 +       };
5766 +       return au_sio_cpup_simple(&cpg);
5767 +}
5768 +
5769 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5770 +{
5771 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5772 +}
5773 +
5774 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5775 +{
5776 +       int err;
5777 +       struct dentry *parent;
5778 +       struct inode *dir;
5779 +
5780 +       parent = dget_parent(dentry);
5781 +       dir = d_inode(parent);
5782 +       err = 0;
5783 +       if (au_h_iptr(dir, bdst))
5784 +               goto out;
5785 +
5786 +       di_read_unlock(parent, AuLock_IR);
5787 +       di_write_lock_parent(parent);
5788 +       /* someone else might change our inode while we were sleeping */
5789 +       if (!au_h_iptr(dir, bdst))
5790 +               err = au_cpup_dirs(dentry, bdst);
5791 +       di_downgrade_lock(parent, AuLock_IR);
5792 +
5793 +out:
5794 +       dput(parent);
5795 +       return err;
5796 +}
5797 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5798 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5799 +++ linux/fs/aufs/cpup.h        2016-01-13 20:11:11.666426853 +0100
5800 @@ -0,0 +1,94 @@
5801 +/*
5802 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5803 + *
5804 + * This program, aufs is free software; you can redistribute it and/or modify
5805 + * it under the terms of the GNU General Public License as published by
5806 + * the Free Software Foundation; either version 2 of the License, or
5807 + * (at your option) any later version.
5808 + *
5809 + * This program is distributed in the hope that it will be useful,
5810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5812 + * GNU General Public License for more details.
5813 + *
5814 + * You should have received a copy of the GNU General Public License
5815 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5816 + */
5817 +
5818 +/*
5819 + * copy-up/down functions
5820 + */
5821 +
5822 +#ifndef __AUFS_CPUP_H__
5823 +#define __AUFS_CPUP_H__
5824 +
5825 +#ifdef __KERNEL__
5826 +
5827 +#include <linux/path.h>
5828 +
5829 +struct inode;
5830 +struct file;
5831 +struct au_pin;
5832 +
5833 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5834 +void au_cpup_attr_timesizes(struct inode *inode);
5835 +void au_cpup_attr_nlink(struct inode *inode, int force);
5836 +void au_cpup_attr_changeable(struct inode *inode);
5837 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5838 +void au_cpup_attr_all(struct inode *inode, int force);
5839 +
5840 +/* ---------------------------------------------------------------------- */
5841 +
5842 +struct au_cp_generic {
5843 +       struct dentry   *dentry;
5844 +       aufs_bindex_t   bdst, bsrc;
5845 +       loff_t          len;
5846 +       struct au_pin   *pin;
5847 +       unsigned int    flags;
5848 +};
5849 +
5850 +/* cpup flags */
5851 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5852 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5853 +                                                  for link(2) */
5854 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5855 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5856 +                                                  cpup */
5857 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5858 +                                                  existing entry */
5859 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5860 +                                                  the branch is marked as RO */
5861 +
5862 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5863 +#define au_fset_cpup(flags, name) \
5864 +       do { (flags) |= AuCpup_##name; } while (0)
5865 +#define au_fclr_cpup(flags, name) \
5866 +       do { (flags) &= ~AuCpup_##name; } while (0)
5867 +
5868 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5869 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5870 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5871 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5872 +
5873 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5874 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5875 +                        struct au_pin *pin,
5876 +                        struct dentry *h_parent, void *arg),
5877 +              void *arg);
5878 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5879 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5880 +
5881 +/* ---------------------------------------------------------------------- */
5882 +
5883 +/* keep timestamps when copyup */
5884 +struct au_dtime {
5885 +       struct dentry *dt_dentry;
5886 +       struct path dt_h_path;
5887 +       struct timespec dt_atime, dt_mtime;
5888 +};
5889 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5890 +                   struct path *h_path);
5891 +void au_dtime_revert(struct au_dtime *dt);
5892 +
5893 +#endif /* __KERNEL__ */
5894 +#endif /* __AUFS_CPUP_H__ */
5895 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
5896 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
5897 +++ linux/fs/aufs/dbgaufs.c     2016-01-13 20:11:11.666426853 +0100
5898 @@ -0,0 +1,432 @@
5899 +/*
5900 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5901 + *
5902 + * This program, aufs is free software; you can redistribute it and/or modify
5903 + * it under the terms of the GNU General Public License as published by
5904 + * the Free Software Foundation; either version 2 of the License, or
5905 + * (at your option) any later version.
5906 + *
5907 + * This program is distributed in the hope that it will be useful,
5908 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5909 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5910 + * GNU General Public License for more details.
5911 + *
5912 + * You should have received a copy of the GNU General Public License
5913 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5914 + */
5915 +
5916 +/*
5917 + * debugfs interface
5918 + */
5919 +
5920 +#include <linux/debugfs.h>
5921 +#include "aufs.h"
5922 +
5923 +#ifndef CONFIG_SYSFS
5924 +#error DEBUG_FS depends upon SYSFS
5925 +#endif
5926 +
5927 +static struct dentry *dbgaufs;
5928 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5929 +
5930 +/* 20 is max digits length of ulong 64 */
5931 +struct dbgaufs_arg {
5932 +       int n;
5933 +       char a[20 * 4];
5934 +};
5935 +
5936 +/*
5937 + * common function for all XINO files
5938 + */
5939 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5940 +                             struct file *file)
5941 +{
5942 +       kfree(file->private_data);
5943 +       return 0;
5944 +}
5945 +
5946 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5947 +{
5948 +       int err;
5949 +       struct kstat st;
5950 +       struct dbgaufs_arg *p;
5951 +
5952 +       err = -ENOMEM;
5953 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5954 +       if (unlikely(!p))
5955 +               goto out;
5956 +
5957 +       err = 0;
5958 +       p->n = 0;
5959 +       file->private_data = p;
5960 +       if (!xf)
5961 +               goto out;
5962 +
5963 +       err = vfs_getattr(&xf->f_path, &st);
5964 +       if (!err) {
5965 +               if (do_fcnt)
5966 +                       p->n = snprintf
5967 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5968 +                                (long)file_count(xf), st.blocks, st.blksize,
5969 +                                (long long)st.size);
5970 +               else
5971 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5972 +                                       st.blocks, st.blksize,
5973 +                                       (long long)st.size);
5974 +               AuDebugOn(p->n >= sizeof(p->a));
5975 +       } else {
5976 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5977 +               err = 0;
5978 +       }
5979 +
5980 +out:
5981 +       return err;
5982 +
5983 +}
5984 +
5985 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5986 +                              size_t count, loff_t *ppos)
5987 +{
5988 +       struct dbgaufs_arg *p;
5989 +
5990 +       p = file->private_data;
5991 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
5992 +}
5993 +
5994 +/* ---------------------------------------------------------------------- */
5995 +
5996 +struct dbgaufs_plink_arg {
5997 +       int n;
5998 +       char a[];
5999 +};
6000 +
6001 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6002 +                                struct file *file)
6003 +{
6004 +       free_page((unsigned long)file->private_data);
6005 +       return 0;
6006 +}
6007 +
6008 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6009 +{
6010 +       int err, i, limit;
6011 +       unsigned long n, sum;
6012 +       struct dbgaufs_plink_arg *p;
6013 +       struct au_sbinfo *sbinfo;
6014 +       struct super_block *sb;
6015 +       struct au_sphlhead *sphl;
6016 +
6017 +       err = -ENOMEM;
6018 +       p = (void *)get_zeroed_page(GFP_NOFS);
6019 +       if (unlikely(!p))
6020 +               goto out;
6021 +
6022 +       err = -EFBIG;
6023 +       sbinfo = inode->i_private;
6024 +       sb = sbinfo->si_sb;
6025 +       si_noflush_read_lock(sb);
6026 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6027 +               limit = PAGE_SIZE - sizeof(p->n);
6028 +
6029 +               /* the number of buckets */
6030 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6031 +               p->n += n;
6032 +               limit -= n;
6033 +
6034 +               sum = 0;
6035 +               for (i = 0, sphl = sbinfo->si_plink;
6036 +                    i < AuPlink_NHASH;
6037 +                    i++, sphl++) {
6038 +                       n = au_sphl_count(sphl);
6039 +                       sum += n;
6040 +
6041 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6042 +                       p->n += n;
6043 +                       limit -= n;
6044 +                       if (unlikely(limit <= 0))
6045 +                               goto out_free;
6046 +               }
6047 +               p->a[p->n - 1] = '\n';
6048 +
6049 +               /* the sum of plinks */
6050 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6051 +               p->n += n;
6052 +               limit -= n;
6053 +               if (unlikely(limit <= 0))
6054 +                       goto out_free;
6055 +       } else {
6056 +#define str "1\n0\n0\n"
6057 +               p->n = sizeof(str) - 1;
6058 +               strcpy(p->a, str);
6059 +#undef str
6060 +       }
6061 +       si_read_unlock(sb);
6062 +
6063 +       err = 0;
6064 +       file->private_data = p;
6065 +       goto out; /* success */
6066 +
6067 +out_free:
6068 +       free_page((unsigned long)p);
6069 +out:
6070 +       return err;
6071 +}
6072 +
6073 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6074 +                                 size_t count, loff_t *ppos)
6075 +{
6076 +       struct dbgaufs_plink_arg *p;
6077 +
6078 +       p = file->private_data;
6079 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6080 +}
6081 +
6082 +static const struct file_operations dbgaufs_plink_fop = {
6083 +       .owner          = THIS_MODULE,
6084 +       .open           = dbgaufs_plink_open,
6085 +       .release        = dbgaufs_plink_release,
6086 +       .read           = dbgaufs_plink_read
6087 +};
6088 +
6089 +/* ---------------------------------------------------------------------- */
6090 +
6091 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6092 +{
6093 +       int err;
6094 +       struct au_sbinfo *sbinfo;
6095 +       struct super_block *sb;
6096 +
6097 +       sbinfo = inode->i_private;
6098 +       sb = sbinfo->si_sb;
6099 +       si_noflush_read_lock(sb);
6100 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6101 +       si_read_unlock(sb);
6102 +       return err;
6103 +}
6104 +
6105 +static const struct file_operations dbgaufs_xib_fop = {
6106 +       .owner          = THIS_MODULE,
6107 +       .open           = dbgaufs_xib_open,
6108 +       .release        = dbgaufs_xi_release,
6109 +       .read           = dbgaufs_xi_read
6110 +};
6111 +
6112 +/* ---------------------------------------------------------------------- */
6113 +
6114 +#define DbgaufsXi_PREFIX "xi"
6115 +
6116 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6117 +{
6118 +       int err;
6119 +       long l;
6120 +       struct au_sbinfo *sbinfo;
6121 +       struct super_block *sb;
6122 +       struct file *xf;
6123 +       struct qstr *name;
6124 +
6125 +       err = -ENOENT;
6126 +       xf = NULL;
6127 +       name = &file->f_path.dentry->d_name;
6128 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6129 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6130 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6131 +               goto out;
6132 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6133 +       if (unlikely(err))
6134 +               goto out;
6135 +
6136 +       sbinfo = inode->i_private;
6137 +       sb = sbinfo->si_sb;
6138 +       si_noflush_read_lock(sb);
6139 +       if (l <= au_sbend(sb)) {
6140 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6141 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6142 +       } else
6143 +               err = -ENOENT;
6144 +       si_read_unlock(sb);
6145 +
6146 +out:
6147 +       return err;
6148 +}
6149 +
6150 +static const struct file_operations dbgaufs_xino_fop = {
6151 +       .owner          = THIS_MODULE,
6152 +       .open           = dbgaufs_xino_open,
6153 +       .release        = dbgaufs_xi_release,
6154 +       .read           = dbgaufs_xi_read
6155 +};
6156 +
6157 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6158 +{
6159 +       aufs_bindex_t bend;
6160 +       struct au_branch *br;
6161 +       struct au_xino_file *xi;
6162 +
6163 +       if (!au_sbi(sb)->si_dbgaufs)
6164 +               return;
6165 +
6166 +       bend = au_sbend(sb);
6167 +       for (; bindex <= bend; bindex++) {
6168 +               br = au_sbr(sb, bindex);
6169 +               xi = &br->br_xino;
6170 +               debugfs_remove(xi->xi_dbgaufs);
6171 +               xi->xi_dbgaufs = NULL;
6172 +       }
6173 +}
6174 +
6175 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6176 +{
6177 +       struct au_sbinfo *sbinfo;
6178 +       struct dentry *parent;
6179 +       struct au_branch *br;
6180 +       struct au_xino_file *xi;
6181 +       aufs_bindex_t bend;
6182 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6183 +
6184 +       sbinfo = au_sbi(sb);
6185 +       parent = sbinfo->si_dbgaufs;
6186 +       if (!parent)
6187 +               return;
6188 +
6189 +       bend = au_sbend(sb);
6190 +       for (; bindex <= bend; bindex++) {
6191 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6192 +               br = au_sbr(sb, bindex);
6193 +               xi = &br->br_xino;
6194 +               AuDebugOn(xi->xi_dbgaufs);
6195 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6196 +                                                    sbinfo, &dbgaufs_xino_fop);
6197 +               /* ignore an error */
6198 +               if (unlikely(!xi->xi_dbgaufs))
6199 +                       AuWarn1("failed %s under debugfs\n", name);
6200 +       }
6201 +}
6202 +
6203 +/* ---------------------------------------------------------------------- */
6204 +
6205 +#ifdef CONFIG_AUFS_EXPORT
6206 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6207 +{
6208 +       int err;
6209 +       struct au_sbinfo *sbinfo;
6210 +       struct super_block *sb;
6211 +
6212 +       sbinfo = inode->i_private;
6213 +       sb = sbinfo->si_sb;
6214 +       si_noflush_read_lock(sb);
6215 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6216 +       si_read_unlock(sb);
6217 +       return err;
6218 +}
6219 +
6220 +static const struct file_operations dbgaufs_xigen_fop = {
6221 +       .owner          = THIS_MODULE,
6222 +       .open           = dbgaufs_xigen_open,
6223 +       .release        = dbgaufs_xi_release,
6224 +       .read           = dbgaufs_xi_read
6225 +};
6226 +
6227 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6228 +{
6229 +       int err;
6230 +
6231 +       /*
6232 +        * This function is a dynamic '__init' function actually,
6233 +        * so the tiny check for si_rwsem is unnecessary.
6234 +        */
6235 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6236 +
6237 +       err = -EIO;
6238 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6239 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6240 +                &dbgaufs_xigen_fop);
6241 +       if (sbinfo->si_dbgaufs_xigen)
6242 +               err = 0;
6243 +
6244 +       return err;
6245 +}
6246 +#else
6247 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6248 +{
6249 +       return 0;
6250 +}
6251 +#endif /* CONFIG_AUFS_EXPORT */
6252 +
6253 +/* ---------------------------------------------------------------------- */
6254 +
6255 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6256 +{
6257 +       /*
6258 +        * This function is a dynamic '__fin' function actually,
6259 +        * so the tiny check for si_rwsem is unnecessary.
6260 +        */
6261 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6262 +
6263 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6264 +       sbinfo->si_dbgaufs = NULL;
6265 +       kobject_put(&sbinfo->si_kobj);
6266 +}
6267 +
6268 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6269 +{
6270 +       int err;
6271 +       char name[SysaufsSiNameLen];
6272 +
6273 +       /*
6274 +        * This function is a dynamic '__init' function actually,
6275 +        * so the tiny check for si_rwsem is unnecessary.
6276 +        */
6277 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6278 +
6279 +       err = -ENOENT;
6280 +       if (!dbgaufs) {
6281 +               AuErr1("/debug/aufs is uninitialized\n");
6282 +               goto out;
6283 +       }
6284 +
6285 +       err = -EIO;
6286 +       sysaufs_name(sbinfo, name);
6287 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6288 +       if (unlikely(!sbinfo->si_dbgaufs))
6289 +               goto out;
6290 +       kobject_get(&sbinfo->si_kobj);
6291 +
6292 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6293 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6294 +                &dbgaufs_xib_fop);
6295 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6296 +               goto out_dir;
6297 +
6298 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6299 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6300 +                &dbgaufs_plink_fop);
6301 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6302 +               goto out_dir;
6303 +
6304 +       err = dbgaufs_xigen_init(sbinfo);
6305 +       if (!err)
6306 +               goto out; /* success */
6307 +
6308 +out_dir:
6309 +       dbgaufs_si_fin(sbinfo);
6310 +out:
6311 +       return err;
6312 +}
6313 +
6314 +/* ---------------------------------------------------------------------- */
6315 +
6316 +void dbgaufs_fin(void)
6317 +{
6318 +       debugfs_remove(dbgaufs);
6319 +}
6320 +
6321 +int __init dbgaufs_init(void)
6322 +{
6323 +       int err;
6324 +
6325 +       err = -EIO;
6326 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6327 +       if (dbgaufs)
6328 +               err = 0;
6329 +       return err;
6330 +}
6331 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6332 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6333 +++ linux/fs/aufs/dbgaufs.h     2016-01-13 20:11:11.666426853 +0100
6334 @@ -0,0 +1,48 @@
6335 +/*
6336 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6337 + *
6338 + * This program, aufs is free software; you can redistribute it and/or modify
6339 + * it under the terms of the GNU General Public License as published by
6340 + * the Free Software Foundation; either version 2 of the License, or
6341 + * (at your option) any later version.
6342 + *
6343 + * This program is distributed in the hope that it will be useful,
6344 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6345 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6346 + * GNU General Public License for more details.
6347 + *
6348 + * You should have received a copy of the GNU General Public License
6349 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6350 + */
6351 +
6352 +/*
6353 + * debugfs interface
6354 + */
6355 +
6356 +#ifndef __DBGAUFS_H__
6357 +#define __DBGAUFS_H__
6358 +
6359 +#ifdef __KERNEL__
6360 +
6361 +struct super_block;
6362 +struct au_sbinfo;
6363 +
6364 +#ifdef CONFIG_DEBUG_FS
6365 +/* dbgaufs.c */
6366 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6367 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6368 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6369 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6370 +void dbgaufs_fin(void);
6371 +int __init dbgaufs_init(void);
6372 +#else
6373 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6374 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6375 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6376 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6377 +AuStubVoid(dbgaufs_fin, void)
6378 +AuStubInt0(__init dbgaufs_init, void)
6379 +#endif /* CONFIG_DEBUG_FS */
6380 +
6381 +#endif /* __KERNEL__ */
6382 +#endif /* __DBGAUFS_H__ */
6383 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6384 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6385 +++ linux/fs/aufs/dcsub.c       2016-01-13 20:11:11.666426853 +0100
6386 @@ -0,0 +1,224 @@
6387 +/*
6388 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6389 + *
6390 + * This program, aufs is free software; you can redistribute it and/or modify
6391 + * it under the terms of the GNU General Public License as published by
6392 + * the Free Software Foundation; either version 2 of the License, or
6393 + * (at your option) any later version.
6394 + *
6395 + * This program is distributed in the hope that it will be useful,
6396 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6397 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6398 + * GNU General Public License for more details.
6399 + *
6400 + * You should have received a copy of the GNU General Public License
6401 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6402 + */
6403 +
6404 +/*
6405 + * sub-routines for dentry cache
6406 + */
6407 +
6408 +#include "aufs.h"
6409 +
6410 +static void au_dpage_free(struct au_dpage *dpage)
6411 +{
6412 +       int i;
6413 +       struct dentry **p;
6414 +
6415 +       p = dpage->dentries;
6416 +       for (i = 0; i < dpage->ndentry; i++)
6417 +               dput(*p++);
6418 +       free_page((unsigned long)dpage->dentries);
6419 +}
6420 +
6421 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6422 +{
6423 +       int err;
6424 +       void *p;
6425 +
6426 +       err = -ENOMEM;
6427 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6428 +       if (unlikely(!dpages->dpages))
6429 +               goto out;
6430 +
6431 +       p = (void *)__get_free_page(gfp);
6432 +       if (unlikely(!p))
6433 +               goto out_dpages;
6434 +
6435 +       dpages->dpages[0].ndentry = 0;
6436 +       dpages->dpages[0].dentries = p;
6437 +       dpages->ndpage = 1;
6438 +       return 0; /* success */
6439 +
6440 +out_dpages:
6441 +       kfree(dpages->dpages);
6442 +out:
6443 +       return err;
6444 +}
6445 +
6446 +void au_dpages_free(struct au_dcsub_pages *dpages)
6447 +{
6448 +       int i;
6449 +       struct au_dpage *p;
6450 +
6451 +       p = dpages->dpages;
6452 +       for (i = 0; i < dpages->ndpage; i++)
6453 +               au_dpage_free(p++);
6454 +       kfree(dpages->dpages);
6455 +}
6456 +
6457 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6458 +                           struct dentry *dentry, gfp_t gfp)
6459 +{
6460 +       int err, sz;
6461 +       struct au_dpage *dpage;
6462 +       void *p;
6463 +
6464 +       dpage = dpages->dpages + dpages->ndpage - 1;
6465 +       sz = PAGE_SIZE / sizeof(dentry);
6466 +       if (unlikely(dpage->ndentry >= sz)) {
6467 +               AuLabel(new dpage);
6468 +               err = -ENOMEM;
6469 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6470 +               p = au_kzrealloc(dpages->dpages, sz,
6471 +                                sz + sizeof(*dpages->dpages), gfp);
6472 +               if (unlikely(!p))
6473 +                       goto out;
6474 +
6475 +               dpages->dpages = p;
6476 +               dpage = dpages->dpages + dpages->ndpage;
6477 +               p = (void *)__get_free_page(gfp);
6478 +               if (unlikely(!p))
6479 +                       goto out;
6480 +
6481 +               dpage->ndentry = 0;
6482 +               dpage->dentries = p;
6483 +               dpages->ndpage++;
6484 +       }
6485 +
6486 +       AuDebugOn(au_dcount(dentry) <= 0);
6487 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6488 +       return 0; /* success */
6489 +
6490 +out:
6491 +       return err;
6492 +}
6493 +
6494 +/* todo: BAD approach */
6495 +/* copied from linux/fs/dcache.c */
6496 +enum d_walk_ret {
6497 +       D_WALK_CONTINUE,
6498 +       D_WALK_QUIT,
6499 +       D_WALK_NORETRY,
6500 +       D_WALK_SKIP,
6501 +};
6502 +
6503 +extern void d_walk(struct dentry *parent, void *data,
6504 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6505 +                  void (*finish)(void *));
6506 +
6507 +struct ac_dpages_arg {
6508 +       int err;
6509 +       struct au_dcsub_pages *dpages;
6510 +       struct super_block *sb;
6511 +       au_dpages_test test;
6512 +       void *arg;
6513 +};
6514 +
6515 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6516 +{
6517 +       enum d_walk_ret ret;
6518 +       struct ac_dpages_arg *arg = _arg;
6519 +
6520 +       ret = D_WALK_CONTINUE;
6521 +       if (dentry->d_sb == arg->sb
6522 +           && !IS_ROOT(dentry)
6523 +           && au_dcount(dentry) > 0
6524 +           && au_di(dentry)
6525 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6526 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6527 +               if (unlikely(arg->err))
6528 +                       ret = D_WALK_QUIT;
6529 +       }
6530 +
6531 +       return ret;
6532 +}
6533 +
6534 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6535 +                  au_dpages_test test, void *arg)
6536 +{
6537 +       struct ac_dpages_arg args = {
6538 +               .err    = 0,
6539 +               .dpages = dpages,
6540 +               .sb     = root->d_sb,
6541 +               .test   = test,
6542 +               .arg    = arg
6543 +       };
6544 +
6545 +       d_walk(root, &args, au_call_dpages_append, NULL);
6546 +
6547 +       return args.err;
6548 +}
6549 +
6550 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6551 +                      int do_include, au_dpages_test test, void *arg)
6552 +{
6553 +       int err;
6554 +
6555 +       err = 0;
6556 +       write_seqlock(&rename_lock);
6557 +       spin_lock(&dentry->d_lock);
6558 +       if (do_include
6559 +           && au_dcount(dentry) > 0
6560 +           && (!test || test(dentry, arg)))
6561 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6562 +       spin_unlock(&dentry->d_lock);
6563 +       if (unlikely(err))
6564 +               goto out;
6565 +
6566 +       /*
6567 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6568 +        * mount
6569 +        */
6570 +       while (!IS_ROOT(dentry)) {
6571 +               dentry = dentry->d_parent; /* rename_lock is locked */
6572 +               spin_lock(&dentry->d_lock);
6573 +               if (au_dcount(dentry) > 0
6574 +                   && (!test || test(dentry, arg)))
6575 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6576 +               spin_unlock(&dentry->d_lock);
6577 +               if (unlikely(err))
6578 +                       break;
6579 +       }
6580 +
6581 +out:
6582 +       write_sequnlock(&rename_lock);
6583 +       return err;
6584 +}
6585 +
6586 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6587 +{
6588 +       return au_di(dentry) && dentry->d_sb == arg;
6589 +}
6590 +
6591 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6592 +                           struct dentry *dentry, int do_include)
6593 +{
6594 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6595 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6596 +}
6597 +
6598 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6599 +{
6600 +       struct path path[2] = {
6601 +               {
6602 +                       .dentry = d1
6603 +               },
6604 +               {
6605 +                       .dentry = d2
6606 +               }
6607 +       };
6608 +
6609 +       return path_is_under(path + 0, path + 1);
6610 +}
6611 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6612 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6613 +++ linux/fs/aufs/dcsub.h       2016-01-13 20:11:11.666426853 +0100
6614 @@ -0,0 +1,136 @@
6615 +/*
6616 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6617 + *
6618 + * This program, aufs is free software; you can redistribute it and/or modify
6619 + * it under the terms of the GNU General Public License as published by
6620 + * the Free Software Foundation; either version 2 of the License, or
6621 + * (at your option) any later version.
6622 + *
6623 + * This program is distributed in the hope that it will be useful,
6624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6626 + * GNU General Public License for more details.
6627 + *
6628 + * You should have received a copy of the GNU General Public License
6629 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6630 + */
6631 +
6632 +/*
6633 + * sub-routines for dentry cache
6634 + */
6635 +
6636 +#ifndef __AUFS_DCSUB_H__
6637 +#define __AUFS_DCSUB_H__
6638 +
6639 +#ifdef __KERNEL__
6640 +
6641 +#include <linux/dcache.h>
6642 +#include <linux/fs.h>
6643 +
6644 +struct au_dpage {
6645 +       int ndentry;
6646 +       struct dentry **dentries;
6647 +};
6648 +
6649 +struct au_dcsub_pages {
6650 +       int ndpage;
6651 +       struct au_dpage *dpages;
6652 +};
6653 +
6654 +/* ---------------------------------------------------------------------- */
6655 +
6656 +/* dcsub.c */
6657 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6658 +void au_dpages_free(struct au_dcsub_pages *dpages);
6659 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6660 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6661 +                  au_dpages_test test, void *arg);
6662 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6663 +                      int do_include, au_dpages_test test, void *arg);
6664 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6665 +                           struct dentry *dentry, int do_include);
6666 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6667 +
6668 +/* ---------------------------------------------------------------------- */
6669 +
6670 +/*
6671 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6672 + * include/linux/dcache.h. Try them (in the future).
6673 + */
6674 +
6675 +static inline int au_d_hashed_positive(struct dentry *d)
6676 +{
6677 +       int err;
6678 +       struct inode *inode = d_inode(d);
6679 +
6680 +       err = 0;
6681 +       if (unlikely(d_unhashed(d)
6682 +                    || d_is_negative(d)
6683 +                    || !inode->i_nlink))
6684 +               err = -ENOENT;
6685 +       return err;
6686 +}
6687 +
6688 +static inline int au_d_linkable(struct dentry *d)
6689 +{
6690 +       int err;
6691 +       struct inode *inode = d_inode(d);
6692 +
6693 +       err = au_d_hashed_positive(d);
6694 +       if (err
6695 +           && d_is_positive(d)
6696 +           && (inode->i_state & I_LINKABLE))
6697 +               err = 0;
6698 +       return err;
6699 +}
6700 +
6701 +static inline int au_d_alive(struct dentry *d)
6702 +{
6703 +       int err;
6704 +       struct inode *inode;
6705 +
6706 +       err = 0;
6707 +       if (!IS_ROOT(d))
6708 +               err = au_d_hashed_positive(d);
6709 +       else {
6710 +               inode = d_inode(d);
6711 +               if (unlikely(d_unlinked(d)
6712 +                            || d_is_negative(d)
6713 +                            || !inode->i_nlink))
6714 +                       err = -ENOENT;
6715 +       }
6716 +       return err;
6717 +}
6718 +
6719 +static inline int au_alive_dir(struct dentry *d)
6720 +{
6721 +       int err;
6722 +
6723 +       err = au_d_alive(d);
6724 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
6725 +               err = -ENOENT;
6726 +       return err;
6727 +}
6728 +
6729 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6730 +{
6731 +       return a->len == b->len
6732 +               && !memcmp(a->name, b->name, a->len);
6733 +}
6734 +
6735 +/*
6736 + * by the commit
6737 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6738 + *                     taking d_lock
6739 + * the type of d_lockref.count became int, but the inlined function d_count()
6740 + * still returns unsigned int.
6741 + * I don't know why. Maybe it is for every d_count() users?
6742 + * Anyway au_dcount() lives on.
6743 + */
6744 +static inline int au_dcount(struct dentry *d)
6745 +{
6746 +       return (int)d_count(d);
6747 +}
6748 +
6749 +#endif /* __KERNEL__ */
6750 +#endif /* __AUFS_DCSUB_H__ */
6751 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6752 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6753 +++ linux/fs/aufs/debug.c       2016-01-13 20:11:11.666426853 +0100
6754 @@ -0,0 +1,438 @@
6755 +/*
6756 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6757 + *
6758 + * This program, aufs is free software; you can redistribute it and/or modify
6759 + * it under the terms of the GNU General Public License as published by
6760 + * the Free Software Foundation; either version 2 of the License, or
6761 + * (at your option) any later version.
6762 + *
6763 + * This program is distributed in the hope that it will be useful,
6764 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6765 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6766 + * GNU General Public License for more details.
6767 + *
6768 + * You should have received a copy of the GNU General Public License
6769 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6770 + */
6771 +
6772 +/*
6773 + * debug print functions
6774 + */
6775 +
6776 +#include "aufs.h"
6777 +
6778 +/* Returns 0, or -errno.  arg is in kp->arg. */
6779 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6780 +{
6781 +       int err, n;
6782 +
6783 +       err = kstrtoint(val, 0, &n);
6784 +       if (!err) {
6785 +               if (n > 0)
6786 +                       au_debug_on();
6787 +               else
6788 +                       au_debug_off();
6789 +       }
6790 +       return err;
6791 +}
6792 +
6793 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6794 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6795 +{
6796 +       atomic_t *a;
6797 +
6798 +       a = kp->arg;
6799 +       return sprintf(buffer, "%d", atomic_read(a));
6800 +}
6801 +
6802 +static struct kernel_param_ops param_ops_atomic_t = {
6803 +       .set = param_atomic_t_set,
6804 +       .get = param_atomic_t_get
6805 +       /* void (*free)(void *arg) */
6806 +};
6807 +
6808 +atomic_t aufs_debug = ATOMIC_INIT(0);
6809 +MODULE_PARM_DESC(debug, "debug print");
6810 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6811 +
6812 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
6813 +char *au_plevel = KERN_DEBUG;
6814 +#define dpri(fmt, ...) do {                                    \
6815 +       if ((au_plevel                                          \
6816 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6817 +           || au_debug_test())                                 \
6818 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6819 +} while (0)
6820 +
6821 +/* ---------------------------------------------------------------------- */
6822 +
6823 +void au_dpri_whlist(struct au_nhash *whlist)
6824 +{
6825 +       unsigned long ul, n;
6826 +       struct hlist_head *head;
6827 +       struct au_vdir_wh *pos;
6828 +
6829 +       n = whlist->nh_num;
6830 +       head = whlist->nh_head;
6831 +       for (ul = 0; ul < n; ul++) {
6832 +               hlist_for_each_entry(pos, head, wh_hash)
6833 +                       dpri("b%d, %.*s, %d\n",
6834 +                            pos->wh_bindex,
6835 +                            pos->wh_str.len, pos->wh_str.name,
6836 +                            pos->wh_str.len);
6837 +               head++;
6838 +       }
6839 +}
6840 +
6841 +void au_dpri_vdir(struct au_vdir *vdir)
6842 +{
6843 +       unsigned long ul;
6844 +       union au_vdir_deblk_p p;
6845 +       unsigned char *o;
6846 +
6847 +       if (!vdir || IS_ERR(vdir)) {
6848 +               dpri("err %ld\n", PTR_ERR(vdir));
6849 +               return;
6850 +       }
6851 +
6852 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6853 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6854 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6855 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6856 +               p.deblk = vdir->vd_deblk[ul];
6857 +               o = p.deblk;
6858 +               dpri("[%lu]: %p\n", ul, o);
6859 +       }
6860 +}
6861 +
6862 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6863 +                       struct dentry *wh)
6864 +{
6865 +       char *n = NULL;
6866 +       int l = 0;
6867 +
6868 +       if (!inode || IS_ERR(inode)) {
6869 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6870 +               return -1;
6871 +       }
6872 +
6873 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
6874 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6875 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6876 +       if (wh) {
6877 +               n = (void *)wh->d_name.name;
6878 +               l = wh->d_name.len;
6879 +       }
6880 +
6881 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6882 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
6883 +            bindex, inode,
6884 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6885 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6886 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6887 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6888 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6889 +            inode->i_state, inode->i_flags, inode->i_version,
6890 +            inode->i_generation,
6891 +            l ? ", wh " : "", l, n);
6892 +       return 0;
6893 +}
6894 +
6895 +void au_dpri_inode(struct inode *inode)
6896 +{
6897 +       struct au_iinfo *iinfo;
6898 +       aufs_bindex_t bindex;
6899 +       int err, hn;
6900 +
6901 +       err = do_pri_inode(-1, inode, -1, NULL);
6902 +       if (err || !au_test_aufs(inode->i_sb))
6903 +               return;
6904 +
6905 +       iinfo = au_ii(inode);
6906 +       if (!iinfo)
6907 +               return;
6908 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6909 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
6910 +       if (iinfo->ii_bstart < 0)
6911 +               return;
6912 +       hn = 0;
6913 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
6914 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
6915 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
6916 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6917 +       }
6918 +}
6919 +
6920 +void au_dpri_dalias(struct inode *inode)
6921 +{
6922 +       struct dentry *d;
6923 +
6924 +       spin_lock(&inode->i_lock);
6925 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
6926 +               au_dpri_dentry(d);
6927 +       spin_unlock(&inode->i_lock);
6928 +}
6929 +
6930 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6931 +{
6932 +       struct dentry *wh = NULL;
6933 +       int hn;
6934 +       struct au_iinfo *iinfo;
6935 +
6936 +       if (!dentry || IS_ERR(dentry)) {
6937 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6938 +               return -1;
6939 +       }
6940 +       /* do not call dget_parent() here */
6941 +       /* note: access d_xxx without d_lock */
6942 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
6943 +            bindex, dentry, dentry,
6944 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6945 +            au_dcount(dentry), dentry->d_flags,
6946 +            d_unhashed(dentry) ? "un" : "");
6947 +       hn = -1;
6948 +       if (bindex >= 0
6949 +           && d_is_positive(dentry)
6950 +           && au_test_aufs(dentry->d_sb)) {
6951 +               iinfo = au_ii(d_inode(dentry));
6952 +               if (iinfo) {
6953 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
6954 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6955 +               }
6956 +       }
6957 +       do_pri_inode(bindex, d_inode(dentry), hn, wh);
6958 +       return 0;
6959 +}
6960 +
6961 +void au_dpri_dentry(struct dentry *dentry)
6962 +{
6963 +       struct au_dinfo *dinfo;
6964 +       aufs_bindex_t bindex;
6965 +       int err;
6966 +       struct au_hdentry *hdp;
6967 +
6968 +       err = do_pri_dentry(-1, dentry);
6969 +       if (err || !au_test_aufs(dentry->d_sb))
6970 +               return;
6971 +
6972 +       dinfo = au_di(dentry);
6973 +       if (!dinfo)
6974 +               return;
6975 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
6976 +            dinfo->di_bstart, dinfo->di_bend,
6977 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
6978 +            dinfo->di_tmpfile);
6979 +       if (dinfo->di_bstart < 0)
6980 +               return;
6981 +       hdp = dinfo->di_hdentry;
6982 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6983 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
6984 +}
6985 +
6986 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
6987 +{
6988 +       char a[32];
6989 +
6990 +       if (!file || IS_ERR(file)) {
6991 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
6992 +               return -1;
6993 +       }
6994 +       a[0] = 0;
6995 +       if (bindex < 0
6996 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
6997 +           && au_test_aufs(file->f_path.dentry->d_sb)
6998 +           && au_fi(file))
6999 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7000 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7001 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7002 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7003 +            file->f_version, file->f_pos, a);
7004 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7005 +               do_pri_dentry(bindex, file->f_path.dentry);
7006 +       return 0;
7007 +}
7008 +
7009 +void au_dpri_file(struct file *file)
7010 +{
7011 +       struct au_finfo *finfo;
7012 +       struct au_fidir *fidir;
7013 +       struct au_hfile *hfile;
7014 +       aufs_bindex_t bindex;
7015 +       int err;
7016 +
7017 +       err = do_pri_file(-1, file);
7018 +       if (err
7019 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7020 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7021 +               return;
7022 +
7023 +       finfo = au_fi(file);
7024 +       if (!finfo)
7025 +               return;
7026 +       if (finfo->fi_btop < 0)
7027 +               return;
7028 +       fidir = finfo->fi_hdir;
7029 +       if (!fidir)
7030 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7031 +       else
7032 +               for (bindex = finfo->fi_btop;
7033 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7034 +                    bindex++) {
7035 +                       hfile = fidir->fd_hfile + bindex;
7036 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7037 +               }
7038 +}
7039 +
7040 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7041 +{
7042 +       struct vfsmount *mnt;
7043 +       struct super_block *sb;
7044 +
7045 +       if (!br || IS_ERR(br))
7046 +               goto out;
7047 +       mnt = au_br_mnt(br);
7048 +       if (!mnt || IS_ERR(mnt))
7049 +               goto out;
7050 +       sb = mnt->mnt_sb;
7051 +       if (!sb || IS_ERR(sb))
7052 +               goto out;
7053 +
7054 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
7055 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7056 +            "xino %d\n",
7057 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
7058 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7059 +            sb->s_flags, sb->s_count,
7060 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7061 +       return 0;
7062 +
7063 +out:
7064 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7065 +       return -1;
7066 +}
7067 +
7068 +void au_dpri_sb(struct super_block *sb)
7069 +{
7070 +       struct au_sbinfo *sbinfo;
7071 +       aufs_bindex_t bindex;
7072 +       int err;
7073 +       /* to reuduce stack size */
7074 +       struct {
7075 +               struct vfsmount mnt;
7076 +               struct au_branch fake;
7077 +       } *a;
7078 +
7079 +       /* this function can be called from magic sysrq */
7080 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7081 +       if (unlikely(!a)) {
7082 +               dpri("no memory\n");
7083 +               return;
7084 +       }
7085 +
7086 +       a->mnt.mnt_sb = sb;
7087 +       a->fake.br_path.mnt = &a->mnt;
7088 +       atomic_set(&a->fake.br_count, 0);
7089 +       smp_mb(); /* atomic_set */
7090 +       err = do_pri_br(-1, &a->fake);
7091 +       kfree(a);
7092 +       dpri("dev 0x%x\n", sb->s_dev);
7093 +       if (err || !au_test_aufs(sb))
7094 +               return;
7095 +
7096 +       sbinfo = au_sbi(sb);
7097 +       if (!sbinfo)
7098 +               return;
7099 +       dpri("nw %d, gen %u, kobj %d\n",
7100 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7101 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
7102 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
7103 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7104 +}
7105 +
7106 +/* ---------------------------------------------------------------------- */
7107 +
7108 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7109 +{
7110 +       struct inode *h_inode, *inode = d_inode(dentry);
7111 +       struct dentry *h_dentry;
7112 +       aufs_bindex_t bindex, bend, bi;
7113 +
7114 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7115 +               return;
7116 +
7117 +       bend = au_dbend(dentry);
7118 +       bi = au_ibend(inode);
7119 +       if (bi < bend)
7120 +               bend = bi;
7121 +       bindex = au_dbstart(dentry);
7122 +       bi = au_ibstart(inode);
7123 +       if (bi > bindex)
7124 +               bindex = bi;
7125 +
7126 +       for (; bindex <= bend; bindex++) {
7127 +               h_dentry = au_h_dptr(dentry, bindex);
7128 +               if (!h_dentry)
7129 +                       continue;
7130 +               h_inode = au_h_iptr(inode, bindex);
7131 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7132 +                       au_debug_on();
7133 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7134 +                       AuDbgDentry(dentry);
7135 +                       AuDbgInode(inode);
7136 +                       au_debug_off();
7137 +                       BUG();
7138 +               }
7139 +       }
7140 +}
7141 +
7142 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7143 +{
7144 +       int err, i, j;
7145 +       struct au_dcsub_pages dpages;
7146 +       struct au_dpage *dpage;
7147 +       struct dentry **dentries;
7148 +
7149 +       err = au_dpages_init(&dpages, GFP_NOFS);
7150 +       AuDebugOn(err);
7151 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7152 +       AuDebugOn(err);
7153 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7154 +               dpage = dpages.dpages + i;
7155 +               dentries = dpage->dentries;
7156 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7157 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7158 +       }
7159 +       au_dpages_free(&dpages);
7160 +}
7161 +
7162 +void au_dbg_verify_kthread(void)
7163 +{
7164 +       if (au_wkq_test()) {
7165 +               au_dbg_blocked();
7166 +               /*
7167 +                * It may be recursive, but udba=notify between two aufs mounts,
7168 +                * where a single ro branch is shared, is not a problem.
7169 +                */
7170 +               /* WARN_ON(1); */
7171 +       }
7172 +}
7173 +
7174 +/* ---------------------------------------------------------------------- */
7175 +
7176 +int __init au_debug_init(void)
7177 +{
7178 +       aufs_bindex_t bindex;
7179 +       struct au_vdir_destr destr;
7180 +
7181 +       bindex = -1;
7182 +       AuDebugOn(bindex >= 0);
7183 +
7184 +       destr.len = -1;
7185 +       AuDebugOn(destr.len < NAME_MAX);
7186 +
7187 +#ifdef CONFIG_4KSTACKS
7188 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7189 +#endif
7190 +
7191 +       return 0;
7192 +}
7193 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7194 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7195 +++ linux/fs/aufs/debug.h       2016-01-13 20:11:11.666426853 +0100
7196 @@ -0,0 +1,225 @@
7197 +/*
7198 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7199 + *
7200 + * This program, aufs is free software; you can redistribute it and/or modify
7201 + * it under the terms of the GNU General Public License as published by
7202 + * the Free Software Foundation; either version 2 of the License, or
7203 + * (at your option) any later version.
7204 + *
7205 + * This program is distributed in the hope that it will be useful,
7206 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7207 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7208 + * GNU General Public License for more details.
7209 + *
7210 + * You should have received a copy of the GNU General Public License
7211 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7212 + */
7213 +
7214 +/*
7215 + * debug print functions
7216 + */
7217 +
7218 +#ifndef __AUFS_DEBUG_H__
7219 +#define __AUFS_DEBUG_H__
7220 +
7221 +#ifdef __KERNEL__
7222 +
7223 +#include <linux/atomic.h>
7224 +#include <linux/module.h>
7225 +#include <linux/kallsyms.h>
7226 +#include <linux/sysrq.h>
7227 +
7228 +#ifdef CONFIG_AUFS_DEBUG
7229 +#define AuDebugOn(a)           BUG_ON(a)
7230 +
7231 +/* module parameter */
7232 +extern atomic_t aufs_debug;
7233 +static inline void au_debug_on(void)
7234 +{
7235 +       atomic_inc(&aufs_debug);
7236 +}
7237 +static inline void au_debug_off(void)
7238 +{
7239 +       atomic_dec_if_positive(&aufs_debug);
7240 +}
7241 +
7242 +static inline int au_debug_test(void)
7243 +{
7244 +       return atomic_read(&aufs_debug) > 0;
7245 +}
7246 +#else
7247 +#define AuDebugOn(a)           do {} while (0)
7248 +AuStubVoid(au_debug_on, void)
7249 +AuStubVoid(au_debug_off, void)
7250 +AuStubInt0(au_debug_test, void)
7251 +#endif /* CONFIG_AUFS_DEBUG */
7252 +
7253 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7254 +
7255 +/* ---------------------------------------------------------------------- */
7256 +
7257 +/* debug print */
7258 +
7259 +#define AuDbg(fmt, ...) do { \
7260 +       if (au_debug_test()) \
7261 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7262 +} while (0)
7263 +#define AuLabel(l)             AuDbg(#l "\n")
7264 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7265 +#define AuWarn1(fmt, ...) do { \
7266 +       static unsigned char _c; \
7267 +       if (!_c++) \
7268 +               pr_warn(fmt, ##__VA_ARGS__); \
7269 +} while (0)
7270 +
7271 +#define AuErr1(fmt, ...) do { \
7272 +       static unsigned char _c; \
7273 +       if (!_c++) \
7274 +               pr_err(fmt, ##__VA_ARGS__); \
7275 +} while (0)
7276 +
7277 +#define AuIOErr1(fmt, ...) do { \
7278 +       static unsigned char _c; \
7279 +       if (!_c++) \
7280 +               AuIOErr(fmt, ##__VA_ARGS__); \
7281 +} while (0)
7282 +
7283 +#define AuUnsupportMsg "This operation is not supported." \
7284 +                       " Please report this application to aufs-users ML."
7285 +#define AuUnsupport(fmt, ...) do { \
7286 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7287 +       dump_stack(); \
7288 +} while (0)
7289 +
7290 +#define AuTraceErr(e) do { \
7291 +       if (unlikely((e) < 0)) \
7292 +               AuDbg("err %d\n", (int)(e)); \
7293 +} while (0)
7294 +
7295 +#define AuTraceErrPtr(p) do { \
7296 +       if (IS_ERR(p)) \
7297 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7298 +} while (0)
7299 +
7300 +/* dirty macros for debug print, use with "%.*s" and caution */
7301 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7302 +
7303 +/* ---------------------------------------------------------------------- */
7304 +
7305 +struct dentry;
7306 +#ifdef CONFIG_AUFS_DEBUG
7307 +extern struct mutex au_dbg_mtx;
7308 +extern char *au_plevel;
7309 +struct au_nhash;
7310 +void au_dpri_whlist(struct au_nhash *whlist);
7311 +struct au_vdir;
7312 +void au_dpri_vdir(struct au_vdir *vdir);
7313 +struct inode;
7314 +void au_dpri_inode(struct inode *inode);
7315 +void au_dpri_dalias(struct inode *inode);
7316 +void au_dpri_dentry(struct dentry *dentry);
7317 +struct file;
7318 +void au_dpri_file(struct file *filp);
7319 +struct super_block;
7320 +void au_dpri_sb(struct super_block *sb);
7321 +
7322 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7323 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7324 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7325 +void au_dbg_verify_kthread(void);
7326 +
7327 +int __init au_debug_init(void);
7328 +
7329 +#define AuDbgWhlist(w) do { \
7330 +       mutex_lock(&au_dbg_mtx); \
7331 +       AuDbg(#w "\n"); \
7332 +       au_dpri_whlist(w); \
7333 +       mutex_unlock(&au_dbg_mtx); \
7334 +} while (0)
7335 +
7336 +#define AuDbgVdir(v) do { \
7337 +       mutex_lock(&au_dbg_mtx); \
7338 +       AuDbg(#v "\n"); \
7339 +       au_dpri_vdir(v); \
7340 +       mutex_unlock(&au_dbg_mtx); \
7341 +} while (0)
7342 +
7343 +#define AuDbgInode(i) do { \
7344 +       mutex_lock(&au_dbg_mtx); \
7345 +       AuDbg(#i "\n"); \
7346 +       au_dpri_inode(i); \
7347 +       mutex_unlock(&au_dbg_mtx); \
7348 +} while (0)
7349 +
7350 +#define AuDbgDAlias(i) do { \
7351 +       mutex_lock(&au_dbg_mtx); \
7352 +       AuDbg(#i "\n"); \
7353 +       au_dpri_dalias(i); \
7354 +       mutex_unlock(&au_dbg_mtx); \
7355 +} while (0)
7356 +
7357 +#define AuDbgDentry(d) do { \
7358 +       mutex_lock(&au_dbg_mtx); \
7359 +       AuDbg(#d "\n"); \
7360 +       au_dpri_dentry(d); \
7361 +       mutex_unlock(&au_dbg_mtx); \
7362 +} while (0)
7363 +
7364 +#define AuDbgFile(f) do { \
7365 +       mutex_lock(&au_dbg_mtx); \
7366 +       AuDbg(#f "\n"); \
7367 +       au_dpri_file(f); \
7368 +       mutex_unlock(&au_dbg_mtx); \
7369 +} while (0)
7370 +
7371 +#define AuDbgSb(sb) do { \
7372 +       mutex_lock(&au_dbg_mtx); \
7373 +       AuDbg(#sb "\n"); \
7374 +       au_dpri_sb(sb); \
7375 +       mutex_unlock(&au_dbg_mtx); \
7376 +} while (0)
7377 +
7378 +#define AuDbgSym(addr) do {                            \
7379 +       char sym[KSYM_SYMBOL_LEN];                      \
7380 +       sprint_symbol(sym, (unsigned long)addr);        \
7381 +       AuDbg("%s\n", sym);                             \
7382 +} while (0)
7383 +#else
7384 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7385 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7386 +AuStubVoid(au_dbg_verify_kthread, void)
7387 +AuStubInt0(__init au_debug_init, void)
7388 +
7389 +#define AuDbgWhlist(w)         do {} while (0)
7390 +#define AuDbgVdir(v)           do {} while (0)
7391 +#define AuDbgInode(i)          do {} while (0)
7392 +#define AuDbgDAlias(i)         do {} while (0)
7393 +#define AuDbgDentry(d)         do {} while (0)
7394 +#define AuDbgFile(f)           do {} while (0)
7395 +#define AuDbgSb(sb)            do {} while (0)
7396 +#define AuDbgSym(addr)         do {} while (0)
7397 +#endif /* CONFIG_AUFS_DEBUG */
7398 +
7399 +/* ---------------------------------------------------------------------- */
7400 +
7401 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7402 +int __init au_sysrq_init(void);
7403 +void au_sysrq_fin(void);
7404 +
7405 +#ifdef CONFIG_HW_CONSOLE
7406 +#define au_dbg_blocked() do { \
7407 +       WARN_ON(1); \
7408 +       handle_sysrq('w'); \
7409 +} while (0)
7410 +#else
7411 +AuStubVoid(au_dbg_blocked, void)
7412 +#endif
7413 +
7414 +#else
7415 +AuStubInt0(__init au_sysrq_init, void)
7416 +AuStubVoid(au_sysrq_fin, void)
7417 +AuStubVoid(au_dbg_blocked, void)
7418 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7419 +
7420 +#endif /* __KERNEL__ */
7421 +#endif /* __AUFS_DEBUG_H__ */
7422 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7423 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7424 +++ linux/fs/aufs/dentry.c      2016-01-13 20:11:11.666426853 +0100
7425 @@ -0,0 +1,1136 @@
7426 +/*
7427 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7428 + *
7429 + * This program, aufs is free software; you can redistribute it and/or modify
7430 + * it under the terms of the GNU General Public License as published by
7431 + * the Free Software Foundation; either version 2 of the License, or
7432 + * (at your option) any later version.
7433 + *
7434 + * This program is distributed in the hope that it will be useful,
7435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7437 + * GNU General Public License for more details.
7438 + *
7439 + * You should have received a copy of the GNU General Public License
7440 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7441 + */
7442 +
7443 +/*
7444 + * lookup and dentry operations
7445 + */
7446 +
7447 +#include <linux/namei.h>
7448 +#include "aufs.h"
7449 +
7450 +#define AuLkup_ALLOW_NEG       1
7451 +#define AuLkup_IGNORE_PERM     (1 << 1)
7452 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
7453 +#define au_fset_lkup(flags, name) \
7454 +       do { (flags) |= AuLkup_##name; } while (0)
7455 +#define au_fclr_lkup(flags, name) \
7456 +       do { (flags) &= ~AuLkup_##name; } while (0)
7457 +
7458 +struct au_do_lookup_args {
7459 +       unsigned int            flags;
7460 +       mode_t                  type;
7461 +};
7462 +
7463 +/*
7464 + * returns positive/negative dentry, NULL or an error.
7465 + * NULL means whiteout-ed or not-found.
7466 + */
7467 +static struct dentry*
7468 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7469 +            aufs_bindex_t bindex, struct qstr *wh_name,
7470 +            struct au_do_lookup_args *args)
7471 +{
7472 +       struct dentry *h_dentry;
7473 +       struct inode *h_inode;
7474 +       struct au_branch *br;
7475 +       int wh_found, opq;
7476 +       unsigned char wh_able;
7477 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7478 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7479 +                                                         IGNORE_PERM);
7480 +
7481 +       wh_found = 0;
7482 +       br = au_sbr(dentry->d_sb, bindex);
7483 +       wh_able = !!au_br_whable(br->br_perm);
7484 +       if (wh_able)
7485 +               wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
7486 +       h_dentry = ERR_PTR(wh_found);
7487 +       if (!wh_found)
7488 +               goto real_lookup;
7489 +       if (unlikely(wh_found < 0))
7490 +               goto out;
7491 +
7492 +       /* We found a whiteout */
7493 +       /* au_set_dbend(dentry, bindex); */
7494 +       au_set_dbwh(dentry, bindex);
7495 +       if (!allow_neg)
7496 +               return NULL; /* success */
7497 +
7498 +real_lookup:
7499 +       if (!ignore_perm)
7500 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7501 +       else
7502 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7503 +       if (IS_ERR(h_dentry)) {
7504 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7505 +                   && !allow_neg)
7506 +                       h_dentry = NULL;
7507 +               goto out;
7508 +       }
7509 +
7510 +       h_inode = d_inode(h_dentry);
7511 +       if (d_is_negative(h_dentry)) {
7512 +               if (!allow_neg)
7513 +                       goto out_neg;
7514 +       } else if (wh_found
7515 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7516 +               goto out_neg;
7517 +
7518 +       if (au_dbend(dentry) <= bindex)
7519 +               au_set_dbend(dentry, bindex);
7520 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7521 +               au_set_dbstart(dentry, bindex);
7522 +       au_set_h_dptr(dentry, bindex, h_dentry);
7523 +
7524 +       if (!d_is_dir(h_dentry)
7525 +           || !wh_able
7526 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7527 +               goto out; /* success */
7528 +
7529 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
7530 +       opq = au_diropq_test(h_dentry);
7531 +       mutex_unlock(&h_inode->i_mutex);
7532 +       if (opq > 0)
7533 +               au_set_dbdiropq(dentry, bindex);
7534 +       else if (unlikely(opq < 0)) {
7535 +               au_set_h_dptr(dentry, bindex, NULL);
7536 +               h_dentry = ERR_PTR(opq);
7537 +       }
7538 +       goto out;
7539 +
7540 +out_neg:
7541 +       dput(h_dentry);
7542 +       h_dentry = NULL;
7543 +out:
7544 +       return h_dentry;
7545 +}
7546 +
7547 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7548 +{
7549 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7550 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7551 +               return -EPERM;
7552 +       return 0;
7553 +}
7554 +
7555 +/*
7556 + * returns the number of lower positive dentries,
7557 + * otherwise an error.
7558 + * can be called at unlinking with @type is zero.
7559 + */
7560 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
7561 +{
7562 +       int npositive, err;
7563 +       aufs_bindex_t bindex, btail, bdiropq;
7564 +       unsigned char isdir, dirperm1;
7565 +       struct qstr whname;
7566 +       struct au_do_lookup_args args = {
7567 +               .flags          = 0,
7568 +               .type           = type
7569 +       };
7570 +       const struct qstr *name = &dentry->d_name;
7571 +       struct dentry *parent;
7572 +       struct super_block *sb;
7573 +
7574 +       sb = dentry->d_sb;
7575 +       err = au_test_shwh(sb, name);
7576 +       if (unlikely(err))
7577 +               goto out;
7578 +
7579 +       err = au_wh_name_alloc(&whname, name);
7580 +       if (unlikely(err))
7581 +               goto out;
7582 +
7583 +       isdir = !!d_is_dir(dentry);
7584 +       if (!type)
7585 +               au_fset_lkup(args.flags, ALLOW_NEG);
7586 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7587 +
7588 +       npositive = 0;
7589 +       parent = dget_parent(dentry);
7590 +       btail = au_dbtaildir(parent);
7591 +       for (bindex = bstart; bindex <= btail; bindex++) {
7592 +               struct dentry *h_parent, *h_dentry;
7593 +               struct inode *h_inode, *h_dir;
7594 +
7595 +               h_dentry = au_h_dptr(dentry, bindex);
7596 +               if (h_dentry) {
7597 +                       if (d_is_positive(h_dentry))
7598 +                               npositive++;
7599 +                       if (type != S_IFDIR)
7600 +                               break;
7601 +                       continue;
7602 +               }
7603 +               h_parent = au_h_dptr(parent, bindex);
7604 +               if (!h_parent || !d_is_dir(h_parent))
7605 +                       continue;
7606 +
7607 +               h_dir = d_inode(h_parent);
7608 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
7609 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7610 +                                       &args);
7611 +               mutex_unlock(&h_dir->i_mutex);
7612 +               err = PTR_ERR(h_dentry);
7613 +               if (IS_ERR(h_dentry))
7614 +                       goto out_parent;
7615 +               if (h_dentry)
7616 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7617 +               if (dirperm1)
7618 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7619 +
7620 +               if (au_dbwh(dentry) == bindex)
7621 +                       break;
7622 +               if (!h_dentry)
7623 +                       continue;
7624 +               if (d_is_negative(h_dentry))
7625 +                       continue;
7626 +               h_inode = d_inode(h_dentry);
7627 +               npositive++;
7628 +               if (!args.type)
7629 +                       args.type = h_inode->i_mode & S_IFMT;
7630 +               if (args.type != S_IFDIR)
7631 +                       break;
7632 +               else if (isdir) {
7633 +                       /* the type of lower may be different */
7634 +                       bdiropq = au_dbdiropq(dentry);
7635 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7636 +                               break;
7637 +               }
7638 +       }
7639 +
7640 +       if (npositive) {
7641 +               AuLabel(positive);
7642 +               au_update_dbstart(dentry);
7643 +       }
7644 +       err = npositive;
7645 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7646 +                    && au_dbstart(dentry) < 0)) {
7647 +               err = -EIO;
7648 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7649 +                       dentry, err);
7650 +       }
7651 +
7652 +out_parent:
7653 +       dput(parent);
7654 +       kfree(whname.name);
7655 +out:
7656 +       return err;
7657 +}
7658 +
7659 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7660 +{
7661 +       struct dentry *dentry;
7662 +       int wkq_err;
7663 +
7664 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
7665 +               dentry = vfsub_lkup_one(name, parent);
7666 +       else {
7667 +               struct vfsub_lkup_one_args args = {
7668 +                       .errp   = &dentry,
7669 +                       .name   = name,
7670 +                       .parent = parent
7671 +               };
7672 +
7673 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7674 +               if (unlikely(wkq_err))
7675 +                       dentry = ERR_PTR(wkq_err);
7676 +       }
7677 +
7678 +       return dentry;
7679 +}
7680 +
7681 +/*
7682 + * lookup @dentry on @bindex which should be negative.
7683 + */
7684 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7685 +{
7686 +       int err;
7687 +       struct dentry *parent, *h_parent, *h_dentry;
7688 +       struct au_branch *br;
7689 +
7690 +       parent = dget_parent(dentry);
7691 +       h_parent = au_h_dptr(parent, bindex);
7692 +       br = au_sbr(dentry->d_sb, bindex);
7693 +       if (wh)
7694 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7695 +       else
7696 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7697 +       err = PTR_ERR(h_dentry);
7698 +       if (IS_ERR(h_dentry))
7699 +               goto out;
7700 +       if (unlikely(d_is_positive(h_dentry))) {
7701 +               err = -EIO;
7702 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7703 +               dput(h_dentry);
7704 +               goto out;
7705 +       }
7706 +
7707 +       err = 0;
7708 +       if (bindex < au_dbstart(dentry))
7709 +               au_set_dbstart(dentry, bindex);
7710 +       if (au_dbend(dentry) < bindex)
7711 +               au_set_dbend(dentry, bindex);
7712 +       au_set_h_dptr(dentry, bindex, h_dentry);
7713 +
7714 +out:
7715 +       dput(parent);
7716 +       return err;
7717 +}
7718 +
7719 +/* ---------------------------------------------------------------------- */
7720 +
7721 +/* subset of struct inode */
7722 +struct au_iattr {
7723 +       unsigned long           i_ino;
7724 +       /* unsigned int         i_nlink; */
7725 +       kuid_t                  i_uid;
7726 +       kgid_t                  i_gid;
7727 +       u64                     i_version;
7728 +/*
7729 +       loff_t                  i_size;
7730 +       blkcnt_t                i_blocks;
7731 +*/
7732 +       umode_t                 i_mode;
7733 +};
7734 +
7735 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7736 +{
7737 +       ia->i_ino = h_inode->i_ino;
7738 +       /* ia->i_nlink = h_inode->i_nlink; */
7739 +       ia->i_uid = h_inode->i_uid;
7740 +       ia->i_gid = h_inode->i_gid;
7741 +       ia->i_version = h_inode->i_version;
7742 +/*
7743 +       ia->i_size = h_inode->i_size;
7744 +       ia->i_blocks = h_inode->i_blocks;
7745 +*/
7746 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7747 +}
7748 +
7749 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7750 +{
7751 +       return ia->i_ino != h_inode->i_ino
7752 +               /* || ia->i_nlink != h_inode->i_nlink */
7753 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7754 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7755 +               || ia->i_version != h_inode->i_version
7756 +/*
7757 +               || ia->i_size != h_inode->i_size
7758 +               || ia->i_blocks != h_inode->i_blocks
7759 +*/
7760 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7761 +}
7762 +
7763 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7764 +                             struct au_branch *br)
7765 +{
7766 +       int err;
7767 +       struct au_iattr ia;
7768 +       struct inode *h_inode;
7769 +       struct dentry *h_d;
7770 +       struct super_block *h_sb;
7771 +
7772 +       err = 0;
7773 +       memset(&ia, -1, sizeof(ia));
7774 +       h_sb = h_dentry->d_sb;
7775 +       h_inode = NULL;
7776 +       if (d_is_positive(h_dentry)) {
7777 +               h_inode = d_inode(h_dentry);
7778 +               au_iattr_save(&ia, h_inode);
7779 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7780 +               /* nfs d_revalidate may return 0 for negative dentry */
7781 +               /* fuse d_revalidate always return 0 for negative dentry */
7782 +               goto out;
7783 +
7784 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7785 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7786 +       err = PTR_ERR(h_d);
7787 +       if (IS_ERR(h_d))
7788 +               goto out;
7789 +
7790 +       err = 0;
7791 +       if (unlikely(h_d != h_dentry
7792 +                    || d_inode(h_d) != h_inode
7793 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7794 +               err = au_busy_or_stale();
7795 +       dput(h_d);
7796 +
7797 +out:
7798 +       AuTraceErr(err);
7799 +       return err;
7800 +}
7801 +
7802 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7803 +               struct dentry *h_parent, struct au_branch *br)
7804 +{
7805 +       int err;
7806 +
7807 +       err = 0;
7808 +       if (udba == AuOpt_UDBA_REVAL
7809 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7810 +               IMustLock(h_dir);
7811 +               err = (d_inode(h_dentry->d_parent) != h_dir);
7812 +       } else if (udba != AuOpt_UDBA_NONE)
7813 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7814 +
7815 +       return err;
7816 +}
7817 +
7818 +/* ---------------------------------------------------------------------- */
7819 +
7820 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7821 +{
7822 +       int err;
7823 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7824 +       struct au_hdentry tmp, *p, *q;
7825 +       struct au_dinfo *dinfo;
7826 +       struct super_block *sb;
7827 +
7828 +       DiMustWriteLock(dentry);
7829 +
7830 +       sb = dentry->d_sb;
7831 +       dinfo = au_di(dentry);
7832 +       bend = dinfo->di_bend;
7833 +       bwh = dinfo->di_bwh;
7834 +       bdiropq = dinfo->di_bdiropq;
7835 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7836 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7837 +               if (!p->hd_dentry)
7838 +                       continue;
7839 +
7840 +               new_bindex = au_br_index(sb, p->hd_id);
7841 +               if (new_bindex == bindex)
7842 +                       continue;
7843 +
7844 +               if (dinfo->di_bwh == bindex)
7845 +                       bwh = new_bindex;
7846 +               if (dinfo->di_bdiropq == bindex)
7847 +                       bdiropq = new_bindex;
7848 +               if (new_bindex < 0) {
7849 +                       au_hdput(p);
7850 +                       p->hd_dentry = NULL;
7851 +                       continue;
7852 +               }
7853 +
7854 +               /* swap two lower dentries, and loop again */
7855 +               q = dinfo->di_hdentry + new_bindex;
7856 +               tmp = *q;
7857 +               *q = *p;
7858 +               *p = tmp;
7859 +               if (tmp.hd_dentry) {
7860 +                       bindex--;
7861 +                       p--;
7862 +               }
7863 +       }
7864 +
7865 +       dinfo->di_bwh = -1;
7866 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7867 +               dinfo->di_bwh = bwh;
7868 +
7869 +       dinfo->di_bdiropq = -1;
7870 +       if (bdiropq >= 0
7871 +           && bdiropq <= au_sbend(sb)
7872 +           && au_sbr_whable(sb, bdiropq))
7873 +               dinfo->di_bdiropq = bdiropq;
7874 +
7875 +       err = -EIO;
7876 +       dinfo->di_bstart = -1;
7877 +       dinfo->di_bend = -1;
7878 +       bend = au_dbend(parent);
7879 +       p = dinfo->di_hdentry;
7880 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7881 +               if (p->hd_dentry) {
7882 +                       dinfo->di_bstart = bindex;
7883 +                       break;
7884 +               }
7885 +
7886 +       if (dinfo->di_bstart >= 0) {
7887 +               p = dinfo->di_hdentry + bend;
7888 +               for (bindex = bend; bindex >= 0; bindex--, p--)
7889 +                       if (p->hd_dentry) {
7890 +                               dinfo->di_bend = bindex;
7891 +                               err = 0;
7892 +                               break;
7893 +                       }
7894 +       }
7895 +
7896 +       return err;
7897 +}
7898 +
7899 +static void au_do_hide(struct dentry *dentry)
7900 +{
7901 +       struct inode *inode;
7902 +
7903 +       if (d_really_is_positive(dentry)) {
7904 +               inode = d_inode(dentry);
7905 +               if (!d_is_dir(dentry)) {
7906 +                       if (inode->i_nlink && !d_unhashed(dentry))
7907 +                               drop_nlink(inode);
7908 +               } else {
7909 +                       clear_nlink(inode);
7910 +                       /* stop next lookup */
7911 +                       inode->i_flags |= S_DEAD;
7912 +               }
7913 +               smp_mb(); /* necessary? */
7914 +       }
7915 +       d_drop(dentry);
7916 +}
7917 +
7918 +static int au_hide_children(struct dentry *parent)
7919 +{
7920 +       int err, i, j, ndentry;
7921 +       struct au_dcsub_pages dpages;
7922 +       struct au_dpage *dpage;
7923 +       struct dentry *dentry;
7924 +
7925 +       err = au_dpages_init(&dpages, GFP_NOFS);
7926 +       if (unlikely(err))
7927 +               goto out;
7928 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
7929 +       if (unlikely(err))
7930 +               goto out_dpages;
7931 +
7932 +       /* in reverse order */
7933 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
7934 +               dpage = dpages.dpages + i;
7935 +               ndentry = dpage->ndentry;
7936 +               for (j = ndentry - 1; j >= 0; j--) {
7937 +                       dentry = dpage->dentries[j];
7938 +                       if (dentry != parent)
7939 +                               au_do_hide(dentry);
7940 +               }
7941 +       }
7942 +
7943 +out_dpages:
7944 +       au_dpages_free(&dpages);
7945 +out:
7946 +       return err;
7947 +}
7948 +
7949 +static void au_hide(struct dentry *dentry)
7950 +{
7951 +       int err;
7952 +
7953 +       AuDbgDentry(dentry);
7954 +       if (d_is_dir(dentry)) {
7955 +               /* shrink_dcache_parent(dentry); */
7956 +               err = au_hide_children(dentry);
7957 +               if (unlikely(err))
7958 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
7959 +                               dentry, err);
7960 +       }
7961 +       au_do_hide(dentry);
7962 +}
7963 +
7964 +/*
7965 + * By adding a dirty branch, a cached dentry may be affected in various ways.
7966 + *
7967 + * a dirty branch is added
7968 + * - on the top of layers
7969 + * - in the middle of layers
7970 + * - to the bottom of layers
7971 + *
7972 + * on the added branch there exists
7973 + * - a whiteout
7974 + * - a diropq
7975 + * - a same named entry
7976 + *   + exist
7977 + *     * negative --> positive
7978 + *     * positive --> positive
7979 + *      - type is unchanged
7980 + *      - type is changed
7981 + *   + doesn't exist
7982 + *     * negative --> negative
7983 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
7984 + * - none
7985 + */
7986 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
7987 +                              struct au_dinfo *tmp)
7988 +{
7989 +       int err;
7990 +       aufs_bindex_t bindex, bend;
7991 +       struct {
7992 +               struct dentry *dentry;
7993 +               struct inode *inode;
7994 +               mode_t mode;
7995 +       } orig_h, tmp_h = {
7996 +               .dentry = NULL
7997 +       };
7998 +       struct au_hdentry *hd;
7999 +       struct inode *inode, *h_inode;
8000 +       struct dentry *h_dentry;
8001 +
8002 +       err = 0;
8003 +       AuDebugOn(dinfo->di_bstart < 0);
8004 +       orig_h.mode = 0;
8005 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
8006 +       orig_h.inode = NULL;
8007 +       if (d_is_positive(orig_h.dentry)) {
8008 +               orig_h.inode = d_inode(orig_h.dentry);
8009 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8010 +       }
8011 +       if (tmp->di_bstart >= 0) {
8012 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
8013 +               if (d_is_positive(tmp_h.dentry)) {
8014 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8015 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8016 +               }
8017 +       }
8018 +
8019 +       inode = NULL;
8020 +       if (d_really_is_positive(dentry))
8021 +               inode = d_inode(dentry);
8022 +       if (!orig_h.inode) {
8023 +               AuDbg("nagative originally\n");
8024 +               if (inode) {
8025 +                       au_hide(dentry);
8026 +                       goto out;
8027 +               }
8028 +               AuDebugOn(inode);
8029 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8030 +               AuDebugOn(dinfo->di_bdiropq != -1);
8031 +
8032 +               if (!tmp_h.inode) {
8033 +                       AuDbg("negative --> negative\n");
8034 +                       /* should have only one negative lower */
8035 +                       if (tmp->di_bstart >= 0
8036 +                           && tmp->di_bstart < dinfo->di_bstart) {
8037 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
8038 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8039 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
8040 +                               au_di_cp(dinfo, tmp);
8041 +                               hd = tmp->di_hdentry + tmp->di_bstart;
8042 +                               au_set_h_dptr(dentry, tmp->di_bstart,
8043 +                                             dget(hd->hd_dentry));
8044 +                       }
8045 +                       au_dbg_verify_dinode(dentry);
8046 +               } else {
8047 +                       AuDbg("negative --> positive\n");
8048 +                       /*
8049 +                        * similar to the behaviour of creating with bypassing
8050 +                        * aufs.
8051 +                        * unhash it in order to force an error in the
8052 +                        * succeeding create operation.
8053 +                        * we should not set S_DEAD here.
8054 +                        */
8055 +                       d_drop(dentry);
8056 +                       /* au_di_swap(tmp, dinfo); */
8057 +                       au_dbg_verify_dinode(dentry);
8058 +               }
8059 +       } else {
8060 +               AuDbg("positive originally\n");
8061 +               /* inode may be NULL */
8062 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8063 +               if (!tmp_h.inode) {
8064 +                       AuDbg("positive --> negative\n");
8065 +                       /* or bypassing aufs */
8066 +                       au_hide(dentry);
8067 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
8068 +                               dinfo->di_bwh = tmp->di_bwh;
8069 +                       if (inode)
8070 +                               err = au_refresh_hinode_self(inode);
8071 +                       au_dbg_verify_dinode(dentry);
8072 +               } else if (orig_h.mode == tmp_h.mode) {
8073 +                       AuDbg("positive --> positive, same type\n");
8074 +                       if (!S_ISDIR(orig_h.mode)
8075 +                           && dinfo->di_bstart > tmp->di_bstart) {
8076 +                               /*
8077 +                                * similar to the behaviour of removing and
8078 +                                * creating.
8079 +                                */
8080 +                               au_hide(dentry);
8081 +                               if (inode)
8082 +                                       err = au_refresh_hinode_self(inode);
8083 +                               au_dbg_verify_dinode(dentry);
8084 +                       } else {
8085 +                               /* fill empty slots */
8086 +                               if (dinfo->di_bstart > tmp->di_bstart)
8087 +                                       dinfo->di_bstart = tmp->di_bstart;
8088 +                               if (dinfo->di_bend < tmp->di_bend)
8089 +                                       dinfo->di_bend = tmp->di_bend;
8090 +                               dinfo->di_bwh = tmp->di_bwh;
8091 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8092 +                               hd = tmp->di_hdentry;
8093 +                               bend = dinfo->di_bend;
8094 +                               for (bindex = tmp->di_bstart; bindex <= bend;
8095 +                                    bindex++) {
8096 +                                       if (au_h_dptr(dentry, bindex))
8097 +                                               continue;
8098 +                                       h_dentry = hd[bindex].hd_dentry;
8099 +                                       if (!h_dentry)
8100 +                                               continue;
8101 +                                       AuDebugOn(d_is_negative(h_dentry));
8102 +                                       h_inode = d_inode(h_dentry);
8103 +                                       AuDebugOn(orig_h.mode
8104 +                                                 != (h_inode->i_mode
8105 +                                                     & S_IFMT));
8106 +                                       au_set_h_dptr(dentry, bindex,
8107 +                                                     dget(h_dentry));
8108 +                               }
8109 +                               err = au_refresh_hinode(inode, dentry);
8110 +                               au_dbg_verify_dinode(dentry);
8111 +                       }
8112 +               } else {
8113 +                       AuDbg("positive --> positive, different type\n");
8114 +                       /* similar to the behaviour of removing and creating */
8115 +                       au_hide(dentry);
8116 +                       if (inode)
8117 +                               err = au_refresh_hinode_self(inode);
8118 +                       au_dbg_verify_dinode(dentry);
8119 +               }
8120 +       }
8121 +
8122 +out:
8123 +       return err;
8124 +}
8125 +
8126 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8127 +{
8128 +       const struct dentry_operations *dop
8129 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8130 +       static const unsigned int mask
8131 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8132 +
8133 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8134 +
8135 +       if (dentry->d_op == dop)
8136 +               return;
8137 +
8138 +       AuDbg("%pd\n", dentry);
8139 +       spin_lock(&dentry->d_lock);
8140 +       if (dop == &aufs_dop)
8141 +               dentry->d_flags |= mask;
8142 +       else
8143 +               dentry->d_flags &= ~mask;
8144 +       dentry->d_op = dop;
8145 +       spin_unlock(&dentry->d_lock);
8146 +}
8147 +
8148 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8149 +{
8150 +       int err, ebrange;
8151 +       unsigned int sigen;
8152 +       struct au_dinfo *dinfo, *tmp;
8153 +       struct super_block *sb;
8154 +       struct inode *inode;
8155 +
8156 +       DiMustWriteLock(dentry);
8157 +       AuDebugOn(IS_ROOT(dentry));
8158 +       AuDebugOn(d_really_is_negative(parent));
8159 +
8160 +       sb = dentry->d_sb;
8161 +       sigen = au_sigen(sb);
8162 +       err = au_digen_test(parent, sigen);
8163 +       if (unlikely(err))
8164 +               goto out;
8165 +
8166 +       dinfo = au_di(dentry);
8167 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
8168 +       if (unlikely(err))
8169 +               goto out;
8170 +       ebrange = au_dbrange_test(dentry);
8171 +       if (!ebrange)
8172 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8173 +
8174 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8175 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
8176 +               if (d_really_is_positive(dentry)) {
8177 +                       inode = d_inode(dentry);
8178 +                       err = au_refresh_hinode_self(inode);
8179 +               }
8180 +               au_dbg_verify_dinode(dentry);
8181 +               if (!err)
8182 +                       goto out_dgen; /* success */
8183 +               goto out;
8184 +       }
8185 +
8186 +       /* temporary dinfo */
8187 +       AuDbgDentry(dentry);
8188 +       err = -ENOMEM;
8189 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8190 +       if (unlikely(!tmp))
8191 +               goto out;
8192 +       au_di_swap(tmp, dinfo);
8193 +       /* returns the number of positive dentries */
8194 +       /*
8195 +        * if current working dir is removed, it returns an error.
8196 +        * but the dentry is legal.
8197 +        */
8198 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
8199 +       AuDbgDentry(dentry);
8200 +       au_di_swap(tmp, dinfo);
8201 +       if (err == -ENOENT)
8202 +               err = 0;
8203 +       if (err >= 0) {
8204 +               /* compare/refresh by dinfo */
8205 +               AuDbgDentry(dentry);
8206 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8207 +               au_dbg_verify_dinode(dentry);
8208 +               AuTraceErr(err);
8209 +       }
8210 +       au_rw_write_unlock(&tmp->di_rwsem);
8211 +       au_di_free(tmp);
8212 +       if (unlikely(err))
8213 +               goto out;
8214 +
8215 +out_dgen:
8216 +       au_update_digen(dentry);
8217 +out:
8218 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8219 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8220 +               AuDbgDentry(dentry);
8221 +       }
8222 +       AuTraceErr(err);
8223 +       return err;
8224 +}
8225 +
8226 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8227 +                          struct dentry *dentry, aufs_bindex_t bindex)
8228 +{
8229 +       int err, valid;
8230 +
8231 +       err = 0;
8232 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8233 +               goto out;
8234 +
8235 +       AuDbg("b%d\n", bindex);
8236 +       /*
8237 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8238 +        * due to whiteout and branch permission.
8239 +        */
8240 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8241 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8242 +       /* it may return tri-state */
8243 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8244 +
8245 +       if (unlikely(valid < 0))
8246 +               err = valid;
8247 +       else if (!valid)
8248 +               err = -EINVAL;
8249 +
8250 +out:
8251 +       AuTraceErr(err);
8252 +       return err;
8253 +}
8254 +
8255 +/* todo: remove this */
8256 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8257 +                         unsigned int flags, int do_udba)
8258 +{
8259 +       int err;
8260 +       umode_t mode, h_mode;
8261 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
8262 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8263 +       struct inode *h_inode, *h_cached_inode;
8264 +       struct dentry *h_dentry;
8265 +       struct qstr *name, *h_name;
8266 +
8267 +       err = 0;
8268 +       plus = 0;
8269 +       mode = 0;
8270 +       ibs = -1;
8271 +       ibe = -1;
8272 +       unhashed = !!d_unhashed(dentry);
8273 +       is_root = !!IS_ROOT(dentry);
8274 +       name = &dentry->d_name;
8275 +       tmpfile = au_di(dentry)->di_tmpfile;
8276 +
8277 +       /*
8278 +        * Theoretically, REVAL test should be unnecessary in case of
8279 +        * {FS,I}NOTIFY.
8280 +        * But {fs,i}notify doesn't fire some necessary events,
8281 +        *      IN_ATTRIB for atime/nlink/pageio
8282 +        * Let's do REVAL test too.
8283 +        */
8284 +       if (do_udba && inode) {
8285 +               mode = (inode->i_mode & S_IFMT);
8286 +               plus = (inode->i_nlink > 0);
8287 +               ibs = au_ibstart(inode);
8288 +               ibe = au_ibend(inode);
8289 +       }
8290 +
8291 +       bstart = au_dbstart(dentry);
8292 +       btail = bstart;
8293 +       if (inode && S_ISDIR(inode->i_mode))
8294 +               btail = au_dbtaildir(dentry);
8295 +       for (bindex = bstart; bindex <= btail; bindex++) {
8296 +               h_dentry = au_h_dptr(dentry, bindex);
8297 +               if (!h_dentry)
8298 +                       continue;
8299 +
8300 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8301 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8302 +               spin_lock(&h_dentry->d_lock);
8303 +               h_name = &h_dentry->d_name;
8304 +               if (unlikely(do_udba
8305 +                            && !is_root
8306 +                            && ((!h_nfs
8307 +                                 && (unhashed != !!d_unhashed(h_dentry)
8308 +                                     || (!tmpfile
8309 +                                         && !au_qstreq(name, h_name))
8310 +                                         ))
8311 +                                || (h_nfs
8312 +                                    && !(flags & LOOKUP_OPEN)
8313 +                                    && (h_dentry->d_flags
8314 +                                        & DCACHE_NFSFS_RENAMED)))
8315 +                           )) {
8316 +                       int h_unhashed;
8317 +
8318 +                       h_unhashed = d_unhashed(h_dentry);
8319 +                       spin_unlock(&h_dentry->d_lock);
8320 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8321 +                             unhashed, h_unhashed, dentry, h_dentry);
8322 +                       goto err;
8323 +               }
8324 +               spin_unlock(&h_dentry->d_lock);
8325 +
8326 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8327 +               if (unlikely(err))
8328 +                       /* do not goto err, to keep the errno */
8329 +                       break;
8330 +
8331 +               /* todo: plink too? */
8332 +               if (!do_udba)
8333 +                       continue;
8334 +
8335 +               /* UDBA tests */
8336 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8337 +                       goto err;
8338 +
8339 +               h_inode = NULL;
8340 +               if (d_is_positive(h_dentry))
8341 +                       h_inode = d_inode(h_dentry);
8342 +               h_plus = plus;
8343 +               h_mode = mode;
8344 +               h_cached_inode = h_inode;
8345 +               if (h_inode) {
8346 +                       h_mode = (h_inode->i_mode & S_IFMT);
8347 +                       h_plus = (h_inode->i_nlink > 0);
8348 +               }
8349 +               if (inode && ibs <= bindex && bindex <= ibe)
8350 +                       h_cached_inode = au_h_iptr(inode, bindex);
8351 +
8352 +               if (!h_nfs) {
8353 +                       if (unlikely(plus != h_plus && !tmpfile))
8354 +                               goto err;
8355 +               } else {
8356 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8357 +                                    && !is_root
8358 +                                    && !IS_ROOT(h_dentry)
8359 +                                    && unhashed != d_unhashed(h_dentry)))
8360 +                               goto err;
8361 +               }
8362 +               if (unlikely(mode != h_mode
8363 +                            || h_cached_inode != h_inode))
8364 +                       goto err;
8365 +               continue;
8366 +
8367 +err:
8368 +               err = -EINVAL;
8369 +               break;
8370 +       }
8371 +
8372 +       AuTraceErr(err);
8373 +       return err;
8374 +}
8375 +
8376 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8377 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8378 +{
8379 +       int err;
8380 +       struct dentry *parent;
8381 +
8382 +       if (!au_digen_test(dentry, sigen))
8383 +               return 0;
8384 +
8385 +       parent = dget_parent(dentry);
8386 +       di_read_lock_parent(parent, AuLock_IR);
8387 +       AuDebugOn(au_digen_test(parent, sigen));
8388 +       au_dbg_verify_gen(parent, sigen);
8389 +       err = au_refresh_dentry(dentry, parent);
8390 +       di_read_unlock(parent, AuLock_IR);
8391 +       dput(parent);
8392 +       AuTraceErr(err);
8393 +       return err;
8394 +}
8395 +
8396 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8397 +{
8398 +       int err;
8399 +       struct dentry *d, *parent;
8400 +
8401 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8402 +               return simple_reval_dpath(dentry, sigen);
8403 +
8404 +       /* slow loop, keep it simple and stupid */
8405 +       /* cf: au_cpup_dirs() */
8406 +       err = 0;
8407 +       parent = NULL;
8408 +       while (au_digen_test(dentry, sigen)) {
8409 +               d = dentry;
8410 +               while (1) {
8411 +                       dput(parent);
8412 +                       parent = dget_parent(d);
8413 +                       if (!au_digen_test(parent, sigen))
8414 +                               break;
8415 +                       d = parent;
8416 +               }
8417 +
8418 +               if (d != dentry)
8419 +                       di_write_lock_child2(d);
8420 +
8421 +               /* someone might update our dentry while we were sleeping */
8422 +               if (au_digen_test(d, sigen)) {
8423 +                       /*
8424 +                        * todo: consolidate with simple_reval_dpath(),
8425 +                        * do_refresh() and au_reval_for_attr().
8426 +                        */
8427 +                       di_read_lock_parent(parent, AuLock_IR);
8428 +                       err = au_refresh_dentry(d, parent);
8429 +                       di_read_unlock(parent, AuLock_IR);
8430 +               }
8431 +
8432 +               if (d != dentry)
8433 +                       di_write_unlock(d);
8434 +               dput(parent);
8435 +               if (unlikely(err))
8436 +                       break;
8437 +       }
8438 +
8439 +       return err;
8440 +}
8441 +
8442 +/*
8443 + * if valid returns 1, otherwise 0.
8444 + */
8445 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8446 +{
8447 +       int valid, err;
8448 +       unsigned int sigen;
8449 +       unsigned char do_udba;
8450 +       struct super_block *sb;
8451 +       struct inode *inode;
8452 +
8453 +       /* todo: support rcu-walk? */
8454 +       if (flags & LOOKUP_RCU)
8455 +               return -ECHILD;
8456 +
8457 +       valid = 0;
8458 +       if (unlikely(!au_di(dentry)))
8459 +               goto out;
8460 +
8461 +       valid = 1;
8462 +       sb = dentry->d_sb;
8463 +       /*
8464 +        * todo: very ugly
8465 +        * i_mutex of parent dir may be held,
8466 +        * but we should not return 'invalid' due to busy.
8467 +        */
8468 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8469 +       if (unlikely(err)) {
8470 +               valid = err;
8471 +               AuTraceErr(err);
8472 +               goto out;
8473 +       }
8474 +       inode = NULL;
8475 +       if (d_really_is_positive(dentry))
8476 +               inode = d_inode(dentry);
8477 +       if (unlikely(inode && is_bad_inode(inode))) {
8478 +               err = -EINVAL;
8479 +               AuTraceErr(err);
8480 +               goto out_dgrade;
8481 +       }
8482 +       if (unlikely(au_dbrange_test(dentry))) {
8483 +               err = -EINVAL;
8484 +               AuTraceErr(err);
8485 +               goto out_dgrade;
8486 +       }
8487 +
8488 +       sigen = au_sigen(sb);
8489 +       if (au_digen_test(dentry, sigen)) {
8490 +               AuDebugOn(IS_ROOT(dentry));
8491 +               err = au_reval_dpath(dentry, sigen);
8492 +               if (unlikely(err)) {
8493 +                       AuTraceErr(err);
8494 +                       goto out_dgrade;
8495 +               }
8496 +       }
8497 +       di_downgrade_lock(dentry, AuLock_IR);
8498 +
8499 +       err = -EINVAL;
8500 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8501 +           && inode
8502 +           && !(inode->i_state && I_LINKABLE)
8503 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8504 +               AuTraceErr(err);
8505 +               goto out_inval;
8506 +       }
8507 +
8508 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8509 +       if (do_udba && inode) {
8510 +               aufs_bindex_t bstart = au_ibstart(inode);
8511 +               struct inode *h_inode;
8512 +
8513 +               if (bstart >= 0) {
8514 +                       h_inode = au_h_iptr(inode, bstart);
8515 +                       if (h_inode && au_test_higen(inode, h_inode)) {
8516 +                               AuTraceErr(err);
8517 +                               goto out_inval;
8518 +                       }
8519 +               }
8520 +       }
8521 +
8522 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8523 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
8524 +               err = -EIO;
8525 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8526 +                     dentry, err);
8527 +       }
8528 +       goto out_inval;
8529 +
8530 +out_dgrade:
8531 +       di_downgrade_lock(dentry, AuLock_IR);
8532 +out_inval:
8533 +       aufs_read_unlock(dentry, AuLock_IR);
8534 +       AuTraceErr(err);
8535 +       valid = !err;
8536 +out:
8537 +       if (!valid) {
8538 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8539 +               d_drop(dentry);
8540 +       }
8541 +       return valid;
8542 +}
8543 +
8544 +static void aufs_d_release(struct dentry *dentry)
8545 +{
8546 +       if (au_di(dentry)) {
8547 +               au_di_fin(dentry);
8548 +               au_hn_di_reinit(dentry);
8549 +       }
8550 +}
8551 +
8552 +const struct dentry_operations aufs_dop = {
8553 +       .d_revalidate           = aufs_d_revalidate,
8554 +       .d_weak_revalidate      = aufs_d_revalidate,
8555 +       .d_release              = aufs_d_release
8556 +};
8557 +
8558 +/* aufs_dop without d_revalidate */
8559 +const struct dentry_operations aufs_dop_noreval = {
8560 +       .d_release              = aufs_d_release
8561 +};
8562 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8563 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8564 +++ linux/fs/aufs/dentry.h      2016-01-13 20:11:11.669760262 +0100
8565 @@ -0,0 +1,234 @@
8566 +/*
8567 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8568 + *
8569 + * This program, aufs is free software; you can redistribute it and/or modify
8570 + * it under the terms of the GNU General Public License as published by
8571 + * the Free Software Foundation; either version 2 of the License, or
8572 + * (at your option) any later version.
8573 + *
8574 + * This program is distributed in the hope that it will be useful,
8575 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8576 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8577 + * GNU General Public License for more details.
8578 + *
8579 + * You should have received a copy of the GNU General Public License
8580 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8581 + */
8582 +
8583 +/*
8584 + * lookup and dentry operations
8585 + */
8586 +
8587 +#ifndef __AUFS_DENTRY_H__
8588 +#define __AUFS_DENTRY_H__
8589 +
8590 +#ifdef __KERNEL__
8591 +
8592 +#include <linux/dcache.h>
8593 +#include "rwsem.h"
8594 +
8595 +struct au_hdentry {
8596 +       struct dentry           *hd_dentry;
8597 +       aufs_bindex_t           hd_id;
8598 +};
8599 +
8600 +struct au_dinfo {
8601 +       atomic_t                di_generation;
8602 +
8603 +       struct au_rwsem         di_rwsem;
8604 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8605 +       unsigned char           di_tmpfile; /* to allow the different name */
8606 +       struct au_hdentry       *di_hdentry;
8607 +} ____cacheline_aligned_in_smp;
8608 +
8609 +/* ---------------------------------------------------------------------- */
8610 +
8611 +/* dentry.c */
8612 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
8613 +struct au_branch;
8614 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8615 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8616 +               struct dentry *h_parent, struct au_branch *br);
8617 +
8618 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8619 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8620 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8621 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8622 +void au_refresh_dop(struct dentry *dentry, int force_reval);
8623 +
8624 +/* dinfo.c */
8625 +void au_di_init_once(void *_di);
8626 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8627 +void au_di_free(struct au_dinfo *dinfo);
8628 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8629 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8630 +int au_di_init(struct dentry *dentry);
8631 +void au_di_fin(struct dentry *dentry);
8632 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8633 +
8634 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8635 +void di_read_unlock(struct dentry *d, int flags);
8636 +void di_downgrade_lock(struct dentry *d, int flags);
8637 +void di_write_lock(struct dentry *d, unsigned int lsc);
8638 +void di_write_unlock(struct dentry *d);
8639 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8640 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8641 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8642 +
8643 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8644 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8645 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8646 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8647 +
8648 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8649 +                  struct dentry *h_dentry);
8650 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8651 +int au_dbrange_test(struct dentry *dentry);
8652 +void au_update_digen(struct dentry *dentry);
8653 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8654 +void au_update_dbstart(struct dentry *dentry);
8655 +void au_update_dbend(struct dentry *dentry);
8656 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8657 +
8658 +/* ---------------------------------------------------------------------- */
8659 +
8660 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8661 +{
8662 +       return dentry->d_fsdata;
8663 +}
8664 +
8665 +/* ---------------------------------------------------------------------- */
8666 +
8667 +/* lock subclass for dinfo */
8668 +enum {
8669 +       AuLsc_DI_CHILD,         /* child first */
8670 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8671 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8672 +       AuLsc_DI_PARENT,
8673 +       AuLsc_DI_PARENT2,
8674 +       AuLsc_DI_PARENT3,
8675 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8676 +};
8677 +
8678 +/*
8679 + * di_read_lock_child, di_write_lock_child,
8680 + * di_read_lock_child2, di_write_lock_child2,
8681 + * di_read_lock_child3, di_write_lock_child3,
8682 + * di_read_lock_parent, di_write_lock_parent,
8683 + * di_read_lock_parent2, di_write_lock_parent2,
8684 + * di_read_lock_parent3, di_write_lock_parent3,
8685 + */
8686 +#define AuReadLockFunc(name, lsc) \
8687 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8688 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8689 +
8690 +#define AuWriteLockFunc(name, lsc) \
8691 +static inline void di_write_lock_##name(struct dentry *d) \
8692 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8693 +
8694 +#define AuRWLockFuncs(name, lsc) \
8695 +       AuReadLockFunc(name, lsc) \
8696 +       AuWriteLockFunc(name, lsc)
8697 +
8698 +AuRWLockFuncs(child, CHILD);
8699 +AuRWLockFuncs(child2, CHILD2);
8700 +AuRWLockFuncs(child3, CHILD3);
8701 +AuRWLockFuncs(parent, PARENT);
8702 +AuRWLockFuncs(parent2, PARENT2);
8703 +AuRWLockFuncs(parent3, PARENT3);
8704 +
8705 +#undef AuReadLockFunc
8706 +#undef AuWriteLockFunc
8707 +#undef AuRWLockFuncs
8708 +
8709 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8710 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8711 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8712 +
8713 +/* ---------------------------------------------------------------------- */
8714 +
8715 +/* todo: memory barrier? */
8716 +static inline unsigned int au_digen(struct dentry *d)
8717 +{
8718 +       return atomic_read(&au_di(d)->di_generation);
8719 +}
8720 +
8721 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8722 +{
8723 +       hdentry->hd_dentry = NULL;
8724 +}
8725 +
8726 +static inline void au_hdput(struct au_hdentry *hd)
8727 +{
8728 +       if (hd)
8729 +               dput(hd->hd_dentry);
8730 +}
8731 +
8732 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8733 +{
8734 +       DiMustAnyLock(dentry);
8735 +       return au_di(dentry)->di_bstart;
8736 +}
8737 +
8738 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8739 +{
8740 +       DiMustAnyLock(dentry);
8741 +       return au_di(dentry)->di_bend;
8742 +}
8743 +
8744 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8745 +{
8746 +       DiMustAnyLock(dentry);
8747 +       return au_di(dentry)->di_bwh;
8748 +}
8749 +
8750 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8751 +{
8752 +       DiMustAnyLock(dentry);
8753 +       return au_di(dentry)->di_bdiropq;
8754 +}
8755 +
8756 +/* todo: hard/soft set? */
8757 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8758 +{
8759 +       DiMustWriteLock(dentry);
8760 +       au_di(dentry)->di_bstart = bindex;
8761 +}
8762 +
8763 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8764 +{
8765 +       DiMustWriteLock(dentry);
8766 +       au_di(dentry)->di_bend = bindex;
8767 +}
8768 +
8769 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8770 +{
8771 +       DiMustWriteLock(dentry);
8772 +       /* dbwh can be outside of bstart - bend range */
8773 +       au_di(dentry)->di_bwh = bindex;
8774 +}
8775 +
8776 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8777 +{
8778 +       DiMustWriteLock(dentry);
8779 +       au_di(dentry)->di_bdiropq = bindex;
8780 +}
8781 +
8782 +/* ---------------------------------------------------------------------- */
8783 +
8784 +#ifdef CONFIG_AUFS_HNOTIFY
8785 +static inline void au_digen_dec(struct dentry *d)
8786 +{
8787 +       atomic_dec(&au_di(d)->di_generation);
8788 +}
8789 +
8790 +static inline void au_hn_di_reinit(struct dentry *dentry)
8791 +{
8792 +       dentry->d_fsdata = NULL;
8793 +}
8794 +#else
8795 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8796 +#endif /* CONFIG_AUFS_HNOTIFY */
8797 +
8798 +#endif /* __KERNEL__ */
8799 +#endif /* __AUFS_DENTRY_H__ */
8800 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8801 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8802 +++ linux/fs/aufs/dinfo.c       2016-01-13 20:11:11.669760262 +0100
8803 @@ -0,0 +1,550 @@
8804 +/*
8805 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8806 + *
8807 + * This program, aufs is free software; you can redistribute it and/or modify
8808 + * it under the terms of the GNU General Public License as published by
8809 + * the Free Software Foundation; either version 2 of the License, or
8810 + * (at your option) any later version.
8811 + *
8812 + * This program is distributed in the hope that it will be useful,
8813 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8814 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8815 + * GNU General Public License for more details.
8816 + *
8817 + * You should have received a copy of the GNU General Public License
8818 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8819 + */
8820 +
8821 +/*
8822 + * dentry private data
8823 + */
8824 +
8825 +#include "aufs.h"
8826 +
8827 +void au_di_init_once(void *_dinfo)
8828 +{
8829 +       struct au_dinfo *dinfo = _dinfo;
8830 +       static struct lock_class_key aufs_di;
8831 +
8832 +       au_rw_init(&dinfo->di_rwsem);
8833 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8834 +}
8835 +
8836 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8837 +{
8838 +       struct au_dinfo *dinfo;
8839 +       int nbr, i;
8840 +
8841 +       dinfo = au_cache_alloc_dinfo();
8842 +       if (unlikely(!dinfo))
8843 +               goto out;
8844 +
8845 +       nbr = au_sbend(sb) + 1;
8846 +       if (nbr <= 0)
8847 +               nbr = 1;
8848 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8849 +       if (dinfo->di_hdentry) {
8850 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8851 +               dinfo->di_bstart = -1;
8852 +               dinfo->di_bend = -1;
8853 +               dinfo->di_bwh = -1;
8854 +               dinfo->di_bdiropq = -1;
8855 +               dinfo->di_tmpfile = 0;
8856 +               for (i = 0; i < nbr; i++)
8857 +                       dinfo->di_hdentry[i].hd_id = -1;
8858 +               goto out;
8859 +       }
8860 +
8861 +       au_cache_free_dinfo(dinfo);
8862 +       dinfo = NULL;
8863 +
8864 +out:
8865 +       return dinfo;
8866 +}
8867 +
8868 +void au_di_free(struct au_dinfo *dinfo)
8869 +{
8870 +       struct au_hdentry *p;
8871 +       aufs_bindex_t bend, bindex;
8872 +
8873 +       /* dentry may not be revalidated */
8874 +       bindex = dinfo->di_bstart;
8875 +       if (bindex >= 0) {
8876 +               bend = dinfo->di_bend;
8877 +               p = dinfo->di_hdentry + bindex;
8878 +               while (bindex++ <= bend)
8879 +                       au_hdput(p++);
8880 +       }
8881 +       kfree(dinfo->di_hdentry);
8882 +       au_cache_free_dinfo(dinfo);
8883 +}
8884 +
8885 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
8886 +{
8887 +       struct au_hdentry *p;
8888 +       aufs_bindex_t bi;
8889 +
8890 +       AuRwMustWriteLock(&a->di_rwsem);
8891 +       AuRwMustWriteLock(&b->di_rwsem);
8892 +
8893 +#define DiSwap(v, name)                                \
8894 +       do {                                    \
8895 +               v = a->di_##name;               \
8896 +               a->di_##name = b->di_##name;    \
8897 +               b->di_##name = v;               \
8898 +       } while (0)
8899 +
8900 +       DiSwap(p, hdentry);
8901 +       DiSwap(bi, bstart);
8902 +       DiSwap(bi, bend);
8903 +       DiSwap(bi, bwh);
8904 +       DiSwap(bi, bdiropq);
8905 +       /* smp_mb(); */
8906 +
8907 +#undef DiSwap
8908 +}
8909 +
8910 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
8911 +{
8912 +       AuRwMustWriteLock(&dst->di_rwsem);
8913 +       AuRwMustWriteLock(&src->di_rwsem);
8914 +
8915 +       dst->di_bstart = src->di_bstart;
8916 +       dst->di_bend = src->di_bend;
8917 +       dst->di_bwh = src->di_bwh;
8918 +       dst->di_bdiropq = src->di_bdiropq;
8919 +       /* smp_mb(); */
8920 +}
8921 +
8922 +int au_di_init(struct dentry *dentry)
8923 +{
8924 +       int err;
8925 +       struct super_block *sb;
8926 +       struct au_dinfo *dinfo;
8927 +
8928 +       err = 0;
8929 +       sb = dentry->d_sb;
8930 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
8931 +       if (dinfo) {
8932 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
8933 +               /* smp_mb(); */ /* atomic_set */
8934 +               dentry->d_fsdata = dinfo;
8935 +       } else
8936 +               err = -ENOMEM;
8937 +
8938 +       return err;
8939 +}
8940 +
8941 +void au_di_fin(struct dentry *dentry)
8942 +{
8943 +       struct au_dinfo *dinfo;
8944 +
8945 +       dinfo = au_di(dentry);
8946 +       AuRwDestroy(&dinfo->di_rwsem);
8947 +       au_di_free(dinfo);
8948 +}
8949 +
8950 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
8951 +{
8952 +       int err, sz;
8953 +       struct au_hdentry *hdp;
8954 +
8955 +       AuRwMustWriteLock(&dinfo->di_rwsem);
8956 +
8957 +       err = -ENOMEM;
8958 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
8959 +       if (!sz)
8960 +               sz = sizeof(*hdp);
8961 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
8962 +       if (hdp) {
8963 +               dinfo->di_hdentry = hdp;
8964 +               err = 0;
8965 +       }
8966 +
8967 +       return err;
8968 +}
8969 +
8970 +/* ---------------------------------------------------------------------- */
8971 +
8972 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
8973 +{
8974 +       switch (lsc) {
8975 +       case AuLsc_DI_CHILD:
8976 +               ii_write_lock_child(inode);
8977 +               break;
8978 +       case AuLsc_DI_CHILD2:
8979 +               ii_write_lock_child2(inode);
8980 +               break;
8981 +       case AuLsc_DI_CHILD3:
8982 +               ii_write_lock_child3(inode);
8983 +               break;
8984 +       case AuLsc_DI_PARENT:
8985 +               ii_write_lock_parent(inode);
8986 +               break;
8987 +       case AuLsc_DI_PARENT2:
8988 +               ii_write_lock_parent2(inode);
8989 +               break;
8990 +       case AuLsc_DI_PARENT3:
8991 +               ii_write_lock_parent3(inode);
8992 +               break;
8993 +       default:
8994 +               BUG();
8995 +       }
8996 +}
8997 +
8998 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
8999 +{
9000 +       switch (lsc) {
9001 +       case AuLsc_DI_CHILD:
9002 +               ii_read_lock_child(inode);
9003 +               break;
9004 +       case AuLsc_DI_CHILD2:
9005 +               ii_read_lock_child2(inode);
9006 +               break;
9007 +       case AuLsc_DI_CHILD3:
9008 +               ii_read_lock_child3(inode);
9009 +               break;
9010 +       case AuLsc_DI_PARENT:
9011 +               ii_read_lock_parent(inode);
9012 +               break;
9013 +       case AuLsc_DI_PARENT2:
9014 +               ii_read_lock_parent2(inode);
9015 +               break;
9016 +       case AuLsc_DI_PARENT3:
9017 +               ii_read_lock_parent3(inode);
9018 +               break;
9019 +       default:
9020 +               BUG();
9021 +       }
9022 +}
9023 +
9024 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9025 +{
9026 +       struct inode *inode;
9027 +
9028 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9029 +       if (d_really_is_positive(d)) {
9030 +               inode = d_inode(d);
9031 +               if (au_ftest_lock(flags, IW))
9032 +                       do_ii_write_lock(inode, lsc);
9033 +               else if (au_ftest_lock(flags, IR))
9034 +                       do_ii_read_lock(inode, lsc);
9035 +       }
9036 +}
9037 +
9038 +void di_read_unlock(struct dentry *d, int flags)
9039 +{
9040 +       struct inode *inode;
9041 +
9042 +       if (d_really_is_positive(d)) {
9043 +               inode = d_inode(d);
9044 +               if (au_ftest_lock(flags, IW)) {
9045 +                       au_dbg_verify_dinode(d);
9046 +                       ii_write_unlock(inode);
9047 +               } else if (au_ftest_lock(flags, IR)) {
9048 +                       au_dbg_verify_dinode(d);
9049 +                       ii_read_unlock(inode);
9050 +               }
9051 +       }
9052 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9053 +}
9054 +
9055 +void di_downgrade_lock(struct dentry *d, int flags)
9056 +{
9057 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9058 +               ii_downgrade_lock(d_inode(d));
9059 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9060 +}
9061 +
9062 +void di_write_lock(struct dentry *d, unsigned int lsc)
9063 +{
9064 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9065 +       if (d_really_is_positive(d))
9066 +               do_ii_write_lock(d_inode(d), lsc);
9067 +}
9068 +
9069 +void di_write_unlock(struct dentry *d)
9070 +{
9071 +       au_dbg_verify_dinode(d);
9072 +       if (d_really_is_positive(d))
9073 +               ii_write_unlock(d_inode(d));
9074 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9075 +}
9076 +
9077 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9078 +{
9079 +       AuDebugOn(d1 == d2
9080 +                 || d_inode(d1) == d_inode(d2)
9081 +                 || d1->d_sb != d2->d_sb);
9082 +
9083 +       if (isdir && au_test_subdir(d1, d2)) {
9084 +               di_write_lock_child(d1);
9085 +               di_write_lock_child2(d2);
9086 +       } else {
9087 +               /* there should be no races */
9088 +               di_write_lock_child(d2);
9089 +               di_write_lock_child2(d1);
9090 +       }
9091 +}
9092 +
9093 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9094 +{
9095 +       AuDebugOn(d1 == d2
9096 +                 || d_inode(d1) == d_inode(d2)
9097 +                 || d1->d_sb != d2->d_sb);
9098 +
9099 +       if (isdir && au_test_subdir(d1, d2)) {
9100 +               di_write_lock_parent(d1);
9101 +               di_write_lock_parent2(d2);
9102 +       } else {
9103 +               /* there should be no races */
9104 +               di_write_lock_parent(d2);
9105 +               di_write_lock_parent2(d1);
9106 +       }
9107 +}
9108 +
9109 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9110 +{
9111 +       di_write_unlock(d1);
9112 +       if (d_inode(d1) == d_inode(d2))
9113 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9114 +       else
9115 +               di_write_unlock(d2);
9116 +}
9117 +
9118 +/* ---------------------------------------------------------------------- */
9119 +
9120 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9121 +{
9122 +       struct dentry *d;
9123 +
9124 +       DiMustAnyLock(dentry);
9125 +
9126 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
9127 +               return NULL;
9128 +       AuDebugOn(bindex < 0);
9129 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
9130 +       AuDebugOn(d && au_dcount(d) <= 0);
9131 +       return d;
9132 +}
9133 +
9134 +/*
9135 + * extended version of au_h_dptr().
9136 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9137 + * error.
9138 + */
9139 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9140 +{
9141 +       struct dentry *h_dentry;
9142 +       struct inode *inode, *h_inode;
9143 +
9144 +       AuDebugOn(d_really_is_negative(dentry));
9145 +
9146 +       h_dentry = NULL;
9147 +       if (au_dbstart(dentry) <= bindex
9148 +           && bindex <= au_dbend(dentry))
9149 +               h_dentry = au_h_dptr(dentry, bindex);
9150 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9151 +               dget(h_dentry);
9152 +               goto out; /* success */
9153 +       }
9154 +
9155 +       inode = d_inode(dentry);
9156 +       AuDebugOn(bindex < au_ibstart(inode));
9157 +       AuDebugOn(au_ibend(inode) < bindex);
9158 +       h_inode = au_h_iptr(inode, bindex);
9159 +       h_dentry = d_find_alias(h_inode);
9160 +       if (h_dentry) {
9161 +               if (!IS_ERR(h_dentry)) {
9162 +                       if (!au_d_linkable(h_dentry))
9163 +                               goto out; /* success */
9164 +                       dput(h_dentry);
9165 +               } else
9166 +                       goto out;
9167 +       }
9168 +
9169 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9170 +               h_dentry = au_plink_lkup(inode, bindex);
9171 +               AuDebugOn(!h_dentry);
9172 +               if (!IS_ERR(h_dentry)) {
9173 +                       if (!au_d_hashed_positive(h_dentry))
9174 +                               goto out; /* success */
9175 +                       dput(h_dentry);
9176 +                       h_dentry = NULL;
9177 +               }
9178 +       }
9179 +
9180 +out:
9181 +       AuDbgDentry(h_dentry);
9182 +       return h_dentry;
9183 +}
9184 +
9185 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9186 +{
9187 +       aufs_bindex_t bend, bwh;
9188 +
9189 +       bend = au_dbend(dentry);
9190 +       if (0 <= bend) {
9191 +               bwh = au_dbwh(dentry);
9192 +               if (!bwh)
9193 +                       return bwh;
9194 +               if (0 < bwh && bwh < bend)
9195 +                       return bwh - 1;
9196 +       }
9197 +       return bend;
9198 +}
9199 +
9200 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9201 +{
9202 +       aufs_bindex_t bend, bopq;
9203 +
9204 +       bend = au_dbtail(dentry);
9205 +       if (0 <= bend) {
9206 +               bopq = au_dbdiropq(dentry);
9207 +               if (0 <= bopq && bopq < bend)
9208 +                       bend = bopq;
9209 +       }
9210 +       return bend;
9211 +}
9212 +
9213 +/* ---------------------------------------------------------------------- */
9214 +
9215 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9216 +                  struct dentry *h_dentry)
9217 +{
9218 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
9219 +       struct au_branch *br;
9220 +
9221 +       DiMustWriteLock(dentry);
9222 +
9223 +       au_hdput(hd);
9224 +       hd->hd_dentry = h_dentry;
9225 +       if (h_dentry) {
9226 +               br = au_sbr(dentry->d_sb, bindex);
9227 +               hd->hd_id = br->br_id;
9228 +       }
9229 +}
9230 +
9231 +int au_dbrange_test(struct dentry *dentry)
9232 +{
9233 +       int err;
9234 +       aufs_bindex_t bstart, bend;
9235 +
9236 +       err = 0;
9237 +       bstart = au_dbstart(dentry);
9238 +       bend = au_dbend(dentry);
9239 +       if (bstart >= 0)
9240 +               AuDebugOn(bend < 0 && bstart > bend);
9241 +       else {
9242 +               err = -EIO;
9243 +               AuDebugOn(bend >= 0);
9244 +       }
9245 +
9246 +       return err;
9247 +}
9248 +
9249 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9250 +{
9251 +       int err;
9252 +
9253 +       err = 0;
9254 +       if (unlikely(au_digen(dentry) != sigen
9255 +                    || au_iigen_test(d_inode(dentry), sigen)))
9256 +               err = -EIO;
9257 +
9258 +       return err;
9259 +}
9260 +
9261 +void au_update_digen(struct dentry *dentry)
9262 +{
9263 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9264 +       /* smp_mb(); */ /* atomic_set */
9265 +}
9266 +
9267 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9268 +{
9269 +       struct au_dinfo *dinfo;
9270 +       struct dentry *h_d;
9271 +       struct au_hdentry *hdp;
9272 +
9273 +       DiMustWriteLock(dentry);
9274 +
9275 +       dinfo = au_di(dentry);
9276 +       if (!dinfo || dinfo->di_bstart < 0)
9277 +               return;
9278 +
9279 +       hdp = dinfo->di_hdentry;
9280 +       if (do_put_zero) {
9281 +               aufs_bindex_t bindex, bend;
9282 +
9283 +               bend = dinfo->di_bend;
9284 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
9285 +                       h_d = hdp[0 + bindex].hd_dentry;
9286 +                       if (h_d && d_is_negative(h_d))
9287 +                               au_set_h_dptr(dentry, bindex, NULL);
9288 +               }
9289 +       }
9290 +
9291 +       dinfo->di_bstart = -1;
9292 +       while (++dinfo->di_bstart <= dinfo->di_bend)
9293 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
9294 +                       break;
9295 +       if (dinfo->di_bstart > dinfo->di_bend) {
9296 +               dinfo->di_bstart = -1;
9297 +               dinfo->di_bend = -1;
9298 +               return;
9299 +       }
9300 +
9301 +       dinfo->di_bend++;
9302 +       while (0 <= --dinfo->di_bend)
9303 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
9304 +                       break;
9305 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
9306 +}
9307 +
9308 +void au_update_dbstart(struct dentry *dentry)
9309 +{
9310 +       aufs_bindex_t bindex, bend;
9311 +       struct dentry *h_dentry;
9312 +
9313 +       bend = au_dbend(dentry);
9314 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
9315 +               h_dentry = au_h_dptr(dentry, bindex);
9316 +               if (!h_dentry)
9317 +                       continue;
9318 +               if (d_is_positive(h_dentry)) {
9319 +                       au_set_dbstart(dentry, bindex);
9320 +                       return;
9321 +               }
9322 +               au_set_h_dptr(dentry, bindex, NULL);
9323 +       }
9324 +}
9325 +
9326 +void au_update_dbend(struct dentry *dentry)
9327 +{
9328 +       aufs_bindex_t bindex, bstart;
9329 +       struct dentry *h_dentry;
9330 +
9331 +       bstart = au_dbstart(dentry);
9332 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
9333 +               h_dentry = au_h_dptr(dentry, bindex);
9334 +               if (!h_dentry)
9335 +                       continue;
9336 +               if (d_is_positive(h_dentry)) {
9337 +                       au_set_dbend(dentry, bindex);
9338 +                       return;
9339 +               }
9340 +               au_set_h_dptr(dentry, bindex, NULL);
9341 +       }
9342 +}
9343 +
9344 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9345 +{
9346 +       aufs_bindex_t bindex, bend;
9347 +
9348 +       bend = au_dbend(dentry);
9349 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
9350 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9351 +                       return bindex;
9352 +       return -1;
9353 +}
9354 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9355 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9356 +++ linux/fs/aufs/dir.c 2016-01-13 20:11:11.669760262 +0100
9357 @@ -0,0 +1,753 @@
9358 +/*
9359 + * Copyright (C) 2005-2015 Junjiro R. Okajima
9360 + *
9361 + * This program, aufs is free software; you can redistribute it and/or modify
9362 + * it under the terms of the GNU General Public License as published by
9363 + * the Free Software Foundation; either version 2 of the License, or
9364 + * (at your option) any later version.
9365 + *
9366 + * This program is distributed in the hope that it will be useful,
9367 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9368 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9369 + * GNU General Public License for more details.
9370 + *
9371 + * You should have received a copy of the GNU General Public License
9372 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9373 + */
9374 +
9375 +/*
9376 + * directory operations
9377 + */
9378 +
9379 +#include <linux/fs_stack.h>
9380 +#include "aufs.h"
9381 +
9382 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9383 +{
9384 +       unsigned int nlink;
9385 +
9386 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9387 +
9388 +       nlink = dir->i_nlink;
9389 +       nlink += h_dir->i_nlink - 2;
9390 +       if (h_dir->i_nlink < 2)
9391 +               nlink += 2;
9392 +       smp_mb(); /* for i_nlink */
9393 +       /* 0 can happen in revaliding */
9394 +       set_nlink(dir, nlink);
9395 +}
9396 +
9397 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9398 +{
9399 +       unsigned int nlink;
9400 +
9401 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9402 +
9403 +       nlink = dir->i_nlink;
9404 +       nlink -= h_dir->i_nlink - 2;
9405 +       if (h_dir->i_nlink < 2)
9406 +               nlink -= 2;
9407 +       smp_mb(); /* for i_nlink */
9408 +       /* nlink == 0 means the branch-fs is broken */
9409 +       set_nlink(dir, nlink);
9410 +}
9411 +
9412 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9413 +{
9414 +       loff_t sz;
9415 +       aufs_bindex_t bindex, bend;
9416 +       struct file *h_file;
9417 +       struct dentry *h_dentry;
9418 +
9419 +       sz = 0;
9420 +       if (file) {
9421 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9422 +
9423 +               bend = au_fbend_dir(file);
9424 +               for (bindex = au_fbstart(file);
9425 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9426 +                    bindex++) {
9427 +                       h_file = au_hf_dir(file, bindex);
9428 +                       if (h_file && file_inode(h_file))
9429 +                               sz += vfsub_f_size_read(h_file);
9430 +               }
9431 +       } else {
9432 +               AuDebugOn(!dentry);
9433 +               AuDebugOn(!d_is_dir(dentry));
9434 +
9435 +               bend = au_dbtaildir(dentry);
9436 +               for (bindex = au_dbstart(dentry);
9437 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9438 +                    bindex++) {
9439 +                       h_dentry = au_h_dptr(dentry, bindex);
9440 +                       if (h_dentry && d_is_positive(h_dentry))
9441 +                               sz += i_size_read(d_inode(h_dentry));
9442 +               }
9443 +       }
9444 +       if (sz < KMALLOC_MAX_SIZE)
9445 +               sz = roundup_pow_of_two(sz);
9446 +       if (sz > KMALLOC_MAX_SIZE)
9447 +               sz = KMALLOC_MAX_SIZE;
9448 +       else if (sz < NAME_MAX) {
9449 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9450 +               sz = AUFS_RDBLK_DEF;
9451 +       }
9452 +       return sz;
9453 +}
9454 +
9455 +struct au_dir_ts_arg {
9456 +       struct dentry *dentry;
9457 +       aufs_bindex_t brid;
9458 +};
9459 +
9460 +static void au_do_dir_ts(void *arg)
9461 +{
9462 +       struct au_dir_ts_arg *a = arg;
9463 +       struct au_dtime dt;
9464 +       struct path h_path;
9465 +       struct inode *dir, *h_dir;
9466 +       struct super_block *sb;
9467 +       struct au_branch *br;
9468 +       struct au_hinode *hdir;
9469 +       int err;
9470 +       aufs_bindex_t bstart, bindex;
9471 +
9472 +       sb = a->dentry->d_sb;
9473 +       if (d_really_is_negative(a->dentry))
9474 +               goto out;
9475 +       /* no dir->i_mutex lock */
9476 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9477 +
9478 +       dir = d_inode(a->dentry);
9479 +       bstart = au_ibstart(dir);
9480 +       bindex = au_br_index(sb, a->brid);
9481 +       if (bindex < bstart)
9482 +               goto out_unlock;
9483 +
9484 +       br = au_sbr(sb, bindex);
9485 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9486 +       if (!h_path.dentry)
9487 +               goto out_unlock;
9488 +       h_path.mnt = au_br_mnt(br);
9489 +       au_dtime_store(&dt, a->dentry, &h_path);
9490 +
9491 +       br = au_sbr(sb, bstart);
9492 +       if (!au_br_writable(br->br_perm))
9493 +               goto out_unlock;
9494 +       h_path.dentry = au_h_dptr(a->dentry, bstart);
9495 +       h_path.mnt = au_br_mnt(br);
9496 +       err = vfsub_mnt_want_write(h_path.mnt);
9497 +       if (err)
9498 +               goto out_unlock;
9499 +       hdir = au_hi(dir, bstart);
9500 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
9501 +       h_dir = au_h_iptr(dir, bstart);
9502 +       if (h_dir->i_nlink
9503 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9504 +               dt.dt_h_path = h_path;
9505 +               au_dtime_revert(&dt);
9506 +       }
9507 +       au_hn_imtx_unlock(hdir);
9508 +       vfsub_mnt_drop_write(h_path.mnt);
9509 +       au_cpup_attr_timesizes(dir);
9510 +
9511 +out_unlock:
9512 +       aufs_read_unlock(a->dentry, AuLock_DW);
9513 +out:
9514 +       dput(a->dentry);
9515 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9516 +       kfree(arg);
9517 +}
9518 +
9519 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9520 +{
9521 +       int perm, wkq_err;
9522 +       aufs_bindex_t bstart;
9523 +       struct au_dir_ts_arg *arg;
9524 +       struct dentry *dentry;
9525 +       struct super_block *sb;
9526 +
9527 +       IMustLock(dir);
9528 +
9529 +       dentry = d_find_any_alias(dir);
9530 +       AuDebugOn(!dentry);
9531 +       sb = dentry->d_sb;
9532 +       bstart = au_ibstart(dir);
9533 +       if (bstart == bindex) {
9534 +               au_cpup_attr_timesizes(dir);
9535 +               goto out;
9536 +       }
9537 +
9538 +       perm = au_sbr_perm(sb, bstart);
9539 +       if (!au_br_writable(perm))
9540 +               goto out;
9541 +
9542 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9543 +       if (!arg)
9544 +               goto out;
9545 +
9546 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9547 +       arg->brid = au_sbr_id(sb, bindex);
9548 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9549 +       if (unlikely(wkq_err)) {
9550 +               pr_err("wkq %d\n", wkq_err);
9551 +               dput(dentry);
9552 +               kfree(arg);
9553 +       }
9554 +
9555 +out:
9556 +       dput(dentry);
9557 +}
9558 +
9559 +/* ---------------------------------------------------------------------- */
9560 +
9561 +static int reopen_dir(struct file *file)
9562 +{
9563 +       int err;
9564 +       unsigned int flags;
9565 +       aufs_bindex_t bindex, btail, bstart;
9566 +       struct dentry *dentry, *h_dentry;
9567 +       struct file *h_file;
9568 +
9569 +       /* open all lower dirs */
9570 +       dentry = file->f_path.dentry;
9571 +       bstart = au_dbstart(dentry);
9572 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
9573 +               au_set_h_fptr(file, bindex, NULL);
9574 +       au_set_fbstart(file, bstart);
9575 +
9576 +       btail = au_dbtaildir(dentry);
9577 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
9578 +               au_set_h_fptr(file, bindex, NULL);
9579 +       au_set_fbend_dir(file, btail);
9580 +
9581 +       flags = vfsub_file_flags(file);
9582 +       for (bindex = bstart; bindex <= btail; bindex++) {
9583 +               h_dentry = au_h_dptr(dentry, bindex);
9584 +               if (!h_dentry)
9585 +                       continue;
9586 +               h_file = au_hf_dir(file, bindex);
9587 +               if (h_file)
9588 +                       continue;
9589 +
9590 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9591 +               err = PTR_ERR(h_file);
9592 +               if (IS_ERR(h_file))
9593 +                       goto out; /* close all? */
9594 +               au_set_h_fptr(file, bindex, h_file);
9595 +       }
9596 +       au_update_figen(file);
9597 +       /* todo: necessary? */
9598 +       /* file->f_ra = h_file->f_ra; */
9599 +       err = 0;
9600 +
9601 +out:
9602 +       return err;
9603 +}
9604 +
9605 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9606 +{
9607 +       int err;
9608 +       aufs_bindex_t bindex, btail;
9609 +       struct dentry *dentry, *h_dentry;
9610 +
9611 +       FiMustWriteLock(file);
9612 +       AuDebugOn(h_file);
9613 +
9614 +       err = 0;
9615 +       dentry = file->f_path.dentry;
9616 +       file->f_version = d_inode(dentry)->i_version;
9617 +       bindex = au_dbstart(dentry);
9618 +       au_set_fbstart(file, bindex);
9619 +       btail = au_dbtaildir(dentry);
9620 +       au_set_fbend_dir(file, btail);
9621 +       for (; !err && bindex <= btail; bindex++) {
9622 +               h_dentry = au_h_dptr(dentry, bindex);
9623 +               if (!h_dentry)
9624 +                       continue;
9625 +
9626 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9627 +               if (IS_ERR(h_file)) {
9628 +                       err = PTR_ERR(h_file);
9629 +                       break;
9630 +               }
9631 +               au_set_h_fptr(file, bindex, h_file);
9632 +       }
9633 +       au_update_figen(file);
9634 +       /* todo: necessary? */
9635 +       /* file->f_ra = h_file->f_ra; */
9636 +       if (!err)
9637 +               return 0; /* success */
9638 +
9639 +       /* close all */
9640 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
9641 +               au_set_h_fptr(file, bindex, NULL);
9642 +       au_set_fbstart(file, -1);
9643 +       au_set_fbend_dir(file, -1);
9644 +
9645 +       return err;
9646 +}
9647 +
9648 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9649 +                        struct file *file)
9650 +{
9651 +       int err;
9652 +       struct super_block *sb;
9653 +       struct au_fidir *fidir;
9654 +
9655 +       err = -ENOMEM;
9656 +       sb = file->f_path.dentry->d_sb;
9657 +       si_read_lock(sb, AuLock_FLUSH);
9658 +       fidir = au_fidir_alloc(sb);
9659 +       if (fidir) {
9660 +               struct au_do_open_args args = {
9661 +                       .open   = do_open_dir,
9662 +                       .fidir  = fidir
9663 +               };
9664 +               err = au_do_open(file, &args);
9665 +               if (unlikely(err))
9666 +                       kfree(fidir);
9667 +       }
9668 +       si_read_unlock(sb);
9669 +       return err;
9670 +}
9671 +
9672 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9673 +                           struct file *file)
9674 +{
9675 +       struct au_vdir *vdir_cache;
9676 +       struct au_finfo *finfo;
9677 +       struct au_fidir *fidir;
9678 +       aufs_bindex_t bindex, bend;
9679 +
9680 +       finfo = au_fi(file);
9681 +       fidir = finfo->fi_hdir;
9682 +       if (fidir) {
9683 +               au_sphl_del(&finfo->fi_hlist,
9684 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9685 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9686 +               if (vdir_cache)
9687 +                       au_vdir_free(vdir_cache);
9688 +
9689 +               bindex = finfo->fi_btop;
9690 +               if (bindex >= 0) {
9691 +                       /*
9692 +                        * calls fput() instead of filp_close(),
9693 +                        * since no dnotify or lock for the lower file.
9694 +                        */
9695 +                       bend = fidir->fd_bbot;
9696 +                       for (; bindex <= bend; bindex++)
9697 +                               au_set_h_fptr(file, bindex, NULL);
9698 +               }
9699 +               kfree(fidir);
9700 +               finfo->fi_hdir = NULL;
9701 +       }
9702 +       au_finfo_fin(file);
9703 +       return 0;
9704 +}
9705 +
9706 +/* ---------------------------------------------------------------------- */
9707 +
9708 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
9709 +{
9710 +       int err;
9711 +       aufs_bindex_t bindex, bend;
9712 +       struct file *h_file;
9713 +
9714 +       err = 0;
9715 +       bend = au_fbend_dir(file);
9716 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9717 +               h_file = au_hf_dir(file, bindex);
9718 +               if (h_file)
9719 +                       err = vfsub_flush(h_file, id);
9720 +       }
9721 +       return err;
9722 +}
9723 +
9724 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9725 +{
9726 +       return au_do_flush(file, id, au_do_flush_dir);
9727 +}
9728 +
9729 +/* ---------------------------------------------------------------------- */
9730 +
9731 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9732 +{
9733 +       int err;
9734 +       aufs_bindex_t bend, bindex;
9735 +       struct inode *inode;
9736 +       struct super_block *sb;
9737 +
9738 +       err = 0;
9739 +       sb = dentry->d_sb;
9740 +       inode = d_inode(dentry);
9741 +       IMustLock(inode);
9742 +       bend = au_dbend(dentry);
9743 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9744 +               struct path h_path;
9745 +
9746 +               if (au_test_ro(sb, bindex, inode))
9747 +                       continue;
9748 +               h_path.dentry = au_h_dptr(dentry, bindex);
9749 +               if (!h_path.dentry)
9750 +                       continue;
9751 +
9752 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9753 +               err = vfsub_fsync(NULL, &h_path, datasync);
9754 +       }
9755 +
9756 +       return err;
9757 +}
9758 +
9759 +static int au_do_fsync_dir(struct file *file, int datasync)
9760 +{
9761 +       int err;
9762 +       aufs_bindex_t bend, bindex;
9763 +       struct file *h_file;
9764 +       struct super_block *sb;
9765 +       struct inode *inode;
9766 +
9767 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9768 +       if (unlikely(err))
9769 +               goto out;
9770 +
9771 +       inode = file_inode(file);
9772 +       sb = inode->i_sb;
9773 +       bend = au_fbend_dir(file);
9774 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9775 +               h_file = au_hf_dir(file, bindex);
9776 +               if (!h_file || au_test_ro(sb, bindex, inode))
9777 +                       continue;
9778 +
9779 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9780 +       }
9781 +
9782 +out:
9783 +       return err;
9784 +}
9785 +
9786 +/*
9787 + * @file may be NULL
9788 + */
9789 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9790 +                         int datasync)
9791 +{
9792 +       int err;
9793 +       struct dentry *dentry;
9794 +       struct inode *inode;
9795 +       struct super_block *sb;
9796 +       struct mutex *mtx;
9797 +
9798 +       err = 0;
9799 +       dentry = file->f_path.dentry;
9800 +       inode = d_inode(dentry);
9801 +       mtx = &inode->i_mutex;
9802 +       mutex_lock(mtx);
9803 +       sb = dentry->d_sb;
9804 +       si_noflush_read_lock(sb);
9805 +       if (file)
9806 +               err = au_do_fsync_dir(file, datasync);
9807 +       else {
9808 +               di_write_lock_child(dentry);
9809 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9810 +       }
9811 +       au_cpup_attr_timesizes(inode);
9812 +       di_write_unlock(dentry);
9813 +       if (file)
9814 +               fi_write_unlock(file);
9815 +
9816 +       si_read_unlock(sb);
9817 +       mutex_unlock(mtx);
9818 +       return err;
9819 +}
9820 +
9821 +/* ---------------------------------------------------------------------- */
9822 +
9823 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9824 +{
9825 +       int err;
9826 +       struct dentry *dentry;
9827 +       struct inode *inode, *h_inode;
9828 +       struct super_block *sb;
9829 +
9830 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9831 +
9832 +       dentry = file->f_path.dentry;
9833 +       inode = d_inode(dentry);
9834 +       IMustLock(inode);
9835 +
9836 +       sb = dentry->d_sb;
9837 +       si_read_lock(sb, AuLock_FLUSH);
9838 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9839 +       if (unlikely(err))
9840 +               goto out;
9841 +       err = au_alive_dir(dentry);
9842 +       if (!err)
9843 +               err = au_vdir_init(file);
9844 +       di_downgrade_lock(dentry, AuLock_IR);
9845 +       if (unlikely(err))
9846 +               goto out_unlock;
9847 +
9848 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9849 +       if (!au_test_nfsd()) {
9850 +               err = au_vdir_fill_de(file, ctx);
9851 +               fsstack_copy_attr_atime(inode, h_inode);
9852 +       } else {
9853 +               /*
9854 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9855 +                * encode_fh() and others.
9856 +                */
9857 +               atomic_inc(&h_inode->i_count);
9858 +               di_read_unlock(dentry, AuLock_IR);
9859 +               si_read_unlock(sb);
9860 +               err = au_vdir_fill_de(file, ctx);
9861 +               fsstack_copy_attr_atime(inode, h_inode);
9862 +               fi_write_unlock(file);
9863 +               iput(h_inode);
9864 +
9865 +               AuTraceErr(err);
9866 +               return err;
9867 +       }
9868 +
9869 +out_unlock:
9870 +       di_read_unlock(dentry, AuLock_IR);
9871 +       fi_write_unlock(file);
9872 +out:
9873 +       si_read_unlock(sb);
9874 +       return err;
9875 +}
9876 +
9877 +/* ---------------------------------------------------------------------- */
9878 +
9879 +#define AuTestEmpty_WHONLY     1
9880 +#define AuTestEmpty_CALLED     (1 << 1)
9881 +#define AuTestEmpty_SHWH       (1 << 2)
9882 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
9883 +#define au_fset_testempty(flags, name) \
9884 +       do { (flags) |= AuTestEmpty_##name; } while (0)
9885 +#define au_fclr_testempty(flags, name) \
9886 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
9887 +
9888 +#ifndef CONFIG_AUFS_SHWH
9889 +#undef AuTestEmpty_SHWH
9890 +#define AuTestEmpty_SHWH       0
9891 +#endif
9892 +
9893 +struct test_empty_arg {
9894 +       struct dir_context ctx;
9895 +       struct au_nhash *whlist;
9896 +       unsigned int flags;
9897 +       int err;
9898 +       aufs_bindex_t bindex;
9899 +};
9900 +
9901 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
9902 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
9903 +                        unsigned int d_type)
9904 +{
9905 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
9906 +                                                 ctx);
9907 +       char *name = (void *)__name;
9908 +
9909 +       arg->err = 0;
9910 +       au_fset_testempty(arg->flags, CALLED);
9911 +       /* smp_mb(); */
9912 +       if (name[0] == '.'
9913 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
9914 +               goto out; /* success */
9915 +
9916 +       if (namelen <= AUFS_WH_PFX_LEN
9917 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
9918 +               if (au_ftest_testempty(arg->flags, WHONLY)
9919 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
9920 +                       arg->err = -ENOTEMPTY;
9921 +               goto out;
9922 +       }
9923 +
9924 +       name += AUFS_WH_PFX_LEN;
9925 +       namelen -= AUFS_WH_PFX_LEN;
9926 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
9927 +               arg->err = au_nhash_append_wh
9928 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
9929 +                        au_ftest_testempty(arg->flags, SHWH));
9930 +
9931 +out:
9932 +       /* smp_mb(); */
9933 +       AuTraceErr(arg->err);
9934 +       return arg->err;
9935 +}
9936 +
9937 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9938 +{
9939 +       int err;
9940 +       struct file *h_file;
9941 +
9942 +       h_file = au_h_open(dentry, arg->bindex,
9943 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
9944 +                          /*file*/NULL, /*force_wr*/0);
9945 +       err = PTR_ERR(h_file);
9946 +       if (IS_ERR(h_file))
9947 +               goto out;
9948 +
9949 +       err = 0;
9950 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
9951 +           && !file_inode(h_file)->i_nlink)
9952 +               goto out_put;
9953 +
9954 +       do {
9955 +               arg->err = 0;
9956 +               au_fclr_testempty(arg->flags, CALLED);
9957 +               /* smp_mb(); */
9958 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
9959 +               if (err >= 0)
9960 +                       err = arg->err;
9961 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
9962 +
9963 +out_put:
9964 +       fput(h_file);
9965 +       au_sbr_put(dentry->d_sb, arg->bindex);
9966 +out:
9967 +       return err;
9968 +}
9969 +
9970 +struct do_test_empty_args {
9971 +       int *errp;
9972 +       struct dentry *dentry;
9973 +       struct test_empty_arg *arg;
9974 +};
9975 +
9976 +static void call_do_test_empty(void *args)
9977 +{
9978 +       struct do_test_empty_args *a = args;
9979 +       *a->errp = do_test_empty(a->dentry, a->arg);
9980 +}
9981 +
9982 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9983 +{
9984 +       int err, wkq_err;
9985 +       struct dentry *h_dentry;
9986 +       struct inode *h_inode;
9987 +
9988 +       h_dentry = au_h_dptr(dentry, arg->bindex);
9989 +       h_inode = d_inode(h_dentry);
9990 +       /* todo: i_mode changes anytime? */
9991 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
9992 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
9993 +       mutex_unlock(&h_inode->i_mutex);
9994 +       if (!err)
9995 +               err = do_test_empty(dentry, arg);
9996 +       else {
9997 +               struct do_test_empty_args args = {
9998 +                       .errp   = &err,
9999 +                       .dentry = dentry,
10000 +                       .arg    = arg
10001 +               };
10002 +               unsigned int flags = arg->flags;
10003 +
10004 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10005 +               if (unlikely(wkq_err))
10006 +                       err = wkq_err;
10007 +               arg->flags = flags;
10008 +       }
10009 +
10010 +       return err;
10011 +}
10012 +
10013 +int au_test_empty_lower(struct dentry *dentry)
10014 +{
10015 +       int err;
10016 +       unsigned int rdhash;
10017 +       aufs_bindex_t bindex, bstart, btail;
10018 +       struct au_nhash whlist;
10019 +       struct test_empty_arg arg = {
10020 +               .ctx = {
10021 +                       .actor = test_empty_cb
10022 +               }
10023 +       };
10024 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10025 +
10026 +       SiMustAnyLock(dentry->d_sb);
10027 +
10028 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10029 +       if (!rdhash)
10030 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10031 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10032 +       if (unlikely(err))
10033 +               goto out;
10034 +
10035 +       arg.flags = 0;
10036 +       arg.whlist = &whlist;
10037 +       bstart = au_dbstart(dentry);
10038 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10039 +               au_fset_testempty(arg.flags, SHWH);
10040 +       test_empty = do_test_empty;
10041 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10042 +               test_empty = sio_test_empty;
10043 +       arg.bindex = bstart;
10044 +       err = test_empty(dentry, &arg);
10045 +       if (unlikely(err))
10046 +               goto out_whlist;
10047 +
10048 +       au_fset_testempty(arg.flags, WHONLY);
10049 +       btail = au_dbtaildir(dentry);
10050 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
10051 +               struct dentry *h_dentry;
10052 +
10053 +               h_dentry = au_h_dptr(dentry, bindex);
10054 +               if (h_dentry && d_is_positive(h_dentry)) {
10055 +                       arg.bindex = bindex;
10056 +                       err = test_empty(dentry, &arg);
10057 +               }
10058 +       }
10059 +
10060 +out_whlist:
10061 +       au_nhash_wh_free(&whlist);
10062 +out:
10063 +       return err;
10064 +}
10065 +
10066 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10067 +{
10068 +       int err;
10069 +       struct test_empty_arg arg = {
10070 +               .ctx = {
10071 +                       .actor = test_empty_cb
10072 +               }
10073 +       };
10074 +       aufs_bindex_t bindex, btail;
10075 +
10076 +       err = 0;
10077 +       arg.whlist = whlist;
10078 +       arg.flags = AuTestEmpty_WHONLY;
10079 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10080 +               au_fset_testempty(arg.flags, SHWH);
10081 +       btail = au_dbtaildir(dentry);
10082 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
10083 +               struct dentry *h_dentry;
10084 +
10085 +               h_dentry = au_h_dptr(dentry, bindex);
10086 +               if (h_dentry && d_is_positive(h_dentry)) {
10087 +                       arg.bindex = bindex;
10088 +                       err = sio_test_empty(dentry, &arg);
10089 +               }
10090 +       }
10091 +
10092 +       return err;
10093 +}
10094 +
10095 +/* ---------------------------------------------------------------------- */
10096 +
10097 +const struct file_operations aufs_dir_fop = {
10098 +       .owner          = THIS_MODULE,
10099 +       .llseek         = default_llseek,
10100 +       .read           = generic_read_dir,
10101 +       .iterate        = aufs_iterate,
10102 +       .unlocked_ioctl = aufs_ioctl_dir,
10103 +#ifdef CONFIG_COMPAT
10104 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10105 +#endif
10106 +       .open           = aufs_open_dir,
10107 +       .release        = aufs_release_dir,
10108 +       .flush          = aufs_flush_dir,
10109 +       .fsync          = aufs_fsync_dir
10110 +};
10111 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10112 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10113 +++ linux/fs/aufs/dir.h 2016-01-13 20:11:11.669760262 +0100
10114 @@ -0,0 +1,131 @@
10115 +/*
10116 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10117 + *
10118 + * This program, aufs is free software; you can redistribute it and/or modify
10119 + * it under the terms of the GNU General Public License as published by
10120 + * the Free Software Foundation; either version 2 of the License, or
10121 + * (at your option) any later version.
10122 + *
10123 + * This program is distributed in the hope that it will be useful,
10124 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10125 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10126 + * GNU General Public License for more details.
10127 + *
10128 + * You should have received a copy of the GNU General Public License
10129 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10130 + */
10131 +
10132 +/*
10133 + * directory operations
10134 + */
10135 +
10136 +#ifndef __AUFS_DIR_H__
10137 +#define __AUFS_DIR_H__
10138 +
10139 +#ifdef __KERNEL__
10140 +
10141 +#include <linux/fs.h>
10142 +
10143 +/* ---------------------------------------------------------------------- */
10144 +
10145 +/* need to be faster and smaller */
10146 +
10147 +struct au_nhash {
10148 +       unsigned int            nh_num;
10149 +       struct hlist_head       *nh_head;
10150 +};
10151 +
10152 +struct au_vdir_destr {
10153 +       unsigned char   len;
10154 +       unsigned char   name[0];
10155 +} __packed;
10156 +
10157 +struct au_vdir_dehstr {
10158 +       struct hlist_node       hash;
10159 +       struct au_vdir_destr    *str;
10160 +} ____cacheline_aligned_in_smp;
10161 +
10162 +struct au_vdir_de {
10163 +       ino_t                   de_ino;
10164 +       unsigned char           de_type;
10165 +       /* caution: packed */
10166 +       struct au_vdir_destr    de_str;
10167 +} __packed;
10168 +
10169 +struct au_vdir_wh {
10170 +       struct hlist_node       wh_hash;
10171 +#ifdef CONFIG_AUFS_SHWH
10172 +       ino_t                   wh_ino;
10173 +       aufs_bindex_t           wh_bindex;
10174 +       unsigned char           wh_type;
10175 +#else
10176 +       aufs_bindex_t           wh_bindex;
10177 +#endif
10178 +       /* caution: packed */
10179 +       struct au_vdir_destr    wh_str;
10180 +} __packed;
10181 +
10182 +union au_vdir_deblk_p {
10183 +       unsigned char           *deblk;
10184 +       struct au_vdir_de       *de;
10185 +};
10186 +
10187 +struct au_vdir {
10188 +       unsigned char   **vd_deblk;
10189 +       unsigned long   vd_nblk;
10190 +       struct {
10191 +               unsigned long           ul;
10192 +               union au_vdir_deblk_p   p;
10193 +       } vd_last;
10194 +
10195 +       unsigned long   vd_version;
10196 +       unsigned int    vd_deblk_sz;
10197 +       unsigned long   vd_jiffy;
10198 +} ____cacheline_aligned_in_smp;
10199 +
10200 +/* ---------------------------------------------------------------------- */
10201 +
10202 +/* dir.c */
10203 +extern const struct file_operations aufs_dir_fop;
10204 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10205 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10206 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10207 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10208 +int au_test_empty_lower(struct dentry *dentry);
10209 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10210 +
10211 +/* vdir.c */
10212 +unsigned int au_rdhash_est(loff_t sz);
10213 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10214 +void au_nhash_wh_free(struct au_nhash *whlist);
10215 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10216 +                           int limit);
10217 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10218 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10219 +                      unsigned int d_type, aufs_bindex_t bindex,
10220 +                      unsigned char shwh);
10221 +void au_vdir_free(struct au_vdir *vdir);
10222 +int au_vdir_init(struct file *file);
10223 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10224 +
10225 +/* ioctl.c */
10226 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10227 +
10228 +#ifdef CONFIG_AUFS_RDU
10229 +/* rdu.c */
10230 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10231 +#ifdef CONFIG_COMPAT
10232 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10233 +                        unsigned long arg);
10234 +#endif
10235 +#else
10236 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10237 +       unsigned int cmd, unsigned long arg)
10238 +#ifdef CONFIG_COMPAT
10239 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10240 +       unsigned int cmd, unsigned long arg)
10241 +#endif
10242 +#endif
10243 +
10244 +#endif /* __KERNEL__ */
10245 +#endif /* __AUFS_DIR_H__ */
10246 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10247 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10248 +++ linux/fs/aufs/dynop.c       2016-01-13 20:11:11.669760262 +0100
10249 @@ -0,0 +1,369 @@
10250 +/*
10251 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10252 + *
10253 + * This program, aufs is free software; you can redistribute it and/or modify
10254 + * it under the terms of the GNU General Public License as published by
10255 + * the Free Software Foundation; either version 2 of the License, or
10256 + * (at your option) any later version.
10257 + *
10258 + * This program is distributed in the hope that it will be useful,
10259 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10260 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10261 + * GNU General Public License for more details.
10262 + *
10263 + * You should have received a copy of the GNU General Public License
10264 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10265 + */
10266 +
10267 +/*
10268 + * dynamically customizable operations for regular files
10269 + */
10270 +
10271 +#include "aufs.h"
10272 +
10273 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10274 +
10275 +/*
10276 + * How large will these lists be?
10277 + * Usually just a few elements, 20-30 at most for each, I guess.
10278 + */
10279 +static struct au_splhead dynop[AuDyLast];
10280 +
10281 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
10282 +{
10283 +       struct au_dykey *key, *tmp;
10284 +       struct list_head *head;
10285 +
10286 +       key = NULL;
10287 +       head = &spl->head;
10288 +       rcu_read_lock();
10289 +       list_for_each_entry_rcu(tmp, head, dk_list)
10290 +               if (tmp->dk_op.dy_hop == h_op) {
10291 +                       key = tmp;
10292 +                       kref_get(&key->dk_kref);
10293 +                       break;
10294 +               }
10295 +       rcu_read_unlock();
10296 +
10297 +       return key;
10298 +}
10299 +
10300 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10301 +{
10302 +       struct au_dykey **k, *found;
10303 +       const void *h_op = key->dk_op.dy_hop;
10304 +       int i;
10305 +
10306 +       found = NULL;
10307 +       k = br->br_dykey;
10308 +       for (i = 0; i < AuBrDynOp; i++)
10309 +               if (k[i]) {
10310 +                       if (k[i]->dk_op.dy_hop == h_op) {
10311 +                               found = k[i];
10312 +                               break;
10313 +                       }
10314 +               } else
10315 +                       break;
10316 +       if (!found) {
10317 +               spin_lock(&br->br_dykey_lock);
10318 +               for (; i < AuBrDynOp; i++)
10319 +                       if (k[i]) {
10320 +                               if (k[i]->dk_op.dy_hop == h_op) {
10321 +                                       found = k[i];
10322 +                                       break;
10323 +                               }
10324 +                       } else {
10325 +                               k[i] = key;
10326 +                               break;
10327 +                       }
10328 +               spin_unlock(&br->br_dykey_lock);
10329 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10330 +       }
10331 +
10332 +       return found;
10333 +}
10334 +
10335 +/* kref_get() if @key is already added */
10336 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10337 +{
10338 +       struct au_dykey *tmp, *found;
10339 +       struct list_head *head;
10340 +       const void *h_op = key->dk_op.dy_hop;
10341 +
10342 +       found = NULL;
10343 +       head = &spl->head;
10344 +       spin_lock(&spl->spin);
10345 +       list_for_each_entry(tmp, head, dk_list)
10346 +               if (tmp->dk_op.dy_hop == h_op) {
10347 +                       kref_get(&tmp->dk_kref);
10348 +                       found = tmp;
10349 +                       break;
10350 +               }
10351 +       if (!found)
10352 +               list_add_rcu(&key->dk_list, head);
10353 +       spin_unlock(&spl->spin);
10354 +
10355 +       if (!found)
10356 +               DyPrSym(key);
10357 +       return found;
10358 +}
10359 +
10360 +static void dy_free_rcu(struct rcu_head *rcu)
10361 +{
10362 +       struct au_dykey *key;
10363 +
10364 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10365 +       DyPrSym(key);
10366 +       kfree(key);
10367 +}
10368 +
10369 +static void dy_free(struct kref *kref)
10370 +{
10371 +       struct au_dykey *key;
10372 +       struct au_splhead *spl;
10373 +
10374 +       key = container_of(kref, struct au_dykey, dk_kref);
10375 +       spl = dynop + key->dk_op.dy_type;
10376 +       au_spl_del_rcu(&key->dk_list, spl);
10377 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10378 +}
10379 +
10380 +void au_dy_put(struct au_dykey *key)
10381 +{
10382 +       kref_put(&key->dk_kref, dy_free);
10383 +}
10384 +
10385 +/* ---------------------------------------------------------------------- */
10386 +
10387 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10388 +
10389 +#ifdef CONFIG_AUFS_DEBUG
10390 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10391 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10392 +#else
10393 +#define DyDbgDeclare(cnt)      do {} while (0)
10394 +#define DyDbgInc(cnt)          do {} while (0)
10395 +#endif
10396 +
10397 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10398 +       DyDbgInc(cnt);                                                  \
10399 +       if (h_op->func) {                                               \
10400 +               if (src.func)                                           \
10401 +                       dst.func = src.func;                            \
10402 +               else                                                    \
10403 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10404 +       }                                                               \
10405 +} while (0)
10406 +
10407 +#define DySetForce(func, dst, src) do {                \
10408 +       AuDebugOn(!src.func);                   \
10409 +       DyDbgInc(cnt);                          \
10410 +       dst.func = src.func;                    \
10411 +} while (0)
10412 +
10413 +#define DySetAop(func) \
10414 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10415 +#define DySetAopForce(func) \
10416 +       DySetForce(func, dyaop->da_op, aufs_aop)
10417 +
10418 +static void dy_aop(struct au_dykey *key, const void *h_op,
10419 +                  struct super_block *h_sb __maybe_unused)
10420 +{
10421 +       struct au_dyaop *dyaop = (void *)key;
10422 +       const struct address_space_operations *h_aop = h_op;
10423 +       DyDbgDeclare(cnt);
10424 +
10425 +       AuDbg("%s\n", au_sbtype(h_sb));
10426 +
10427 +       DySetAop(writepage);
10428 +       DySetAopForce(readpage);        /* force */
10429 +       DySetAop(writepages);
10430 +       DySetAop(set_page_dirty);
10431 +       DySetAop(readpages);
10432 +       DySetAop(write_begin);
10433 +       DySetAop(write_end);
10434 +       DySetAop(bmap);
10435 +       DySetAop(invalidatepage);
10436 +       DySetAop(releasepage);
10437 +       DySetAop(freepage);
10438 +       /* this one will be changed according to an aufs mount option */
10439 +       DySetAop(direct_IO);
10440 +       DySetAop(migratepage);
10441 +       DySetAop(launder_page);
10442 +       DySetAop(is_partially_uptodate);
10443 +       DySetAop(is_dirty_writeback);
10444 +       DySetAop(error_remove_page);
10445 +       DySetAop(swap_activate);
10446 +       DySetAop(swap_deactivate);
10447 +
10448 +       DyDbgSize(cnt, *h_aop);
10449 +}
10450 +
10451 +/* ---------------------------------------------------------------------- */
10452 +
10453 +static void dy_bug(struct kref *kref)
10454 +{
10455 +       BUG();
10456 +}
10457 +
10458 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10459 +{
10460 +       struct au_dykey *key, *old;
10461 +       struct au_splhead *spl;
10462 +       struct op {
10463 +               unsigned int sz;
10464 +               void (*set)(struct au_dykey *key, const void *h_op,
10465 +                           struct super_block *h_sb __maybe_unused);
10466 +       };
10467 +       static const struct op a[] = {
10468 +               [AuDy_AOP] = {
10469 +                       .sz     = sizeof(struct au_dyaop),
10470 +                       .set    = dy_aop
10471 +               }
10472 +       };
10473 +       const struct op *p;
10474 +
10475 +       spl = dynop + op->dy_type;
10476 +       key = dy_gfind_get(spl, op->dy_hop);
10477 +       if (key)
10478 +               goto out_add; /* success */
10479 +
10480 +       p = a + op->dy_type;
10481 +       key = kzalloc(p->sz, GFP_NOFS);
10482 +       if (unlikely(!key)) {
10483 +               key = ERR_PTR(-ENOMEM);
10484 +               goto out;
10485 +       }
10486 +
10487 +       key->dk_op.dy_hop = op->dy_hop;
10488 +       kref_init(&key->dk_kref);
10489 +       p->set(key, op->dy_hop, au_br_sb(br));
10490 +       old = dy_gadd(spl, key);
10491 +       if (old) {
10492 +               kfree(key);
10493 +               key = old;
10494 +       }
10495 +
10496 +out_add:
10497 +       old = dy_bradd(br, key);
10498 +       if (old)
10499 +               /* its ref-count should never be zero here */
10500 +               kref_put(&key->dk_kref, dy_bug);
10501 +out:
10502 +       return key;
10503 +}
10504 +
10505 +/* ---------------------------------------------------------------------- */
10506 +/*
10507 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10508 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10509 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10510 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10511 + * See the aufs manual in detail.
10512 + */
10513 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10514 +{
10515 +       if (!do_dx)
10516 +               dyaop->da_op.direct_IO = NULL;
10517 +       else
10518 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10519 +}
10520 +
10521 +static struct au_dyaop *dy_aget(struct au_branch *br,
10522 +                               const struct address_space_operations *h_aop,
10523 +                               int do_dx)
10524 +{
10525 +       struct au_dyaop *dyaop;
10526 +       struct au_dynop op;
10527 +
10528 +       op.dy_type = AuDy_AOP;
10529 +       op.dy_haop = h_aop;
10530 +       dyaop = (void *)dy_get(&op, br);
10531 +       if (IS_ERR(dyaop))
10532 +               goto out;
10533 +       dy_adx(dyaop, do_dx);
10534 +
10535 +out:
10536 +       return dyaop;
10537 +}
10538 +
10539 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10540 +               struct inode *h_inode)
10541 +{
10542 +       int err, do_dx;
10543 +       struct super_block *sb;
10544 +       struct au_branch *br;
10545 +       struct au_dyaop *dyaop;
10546 +
10547 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10548 +       IiMustWriteLock(inode);
10549 +
10550 +       sb = inode->i_sb;
10551 +       br = au_sbr(sb, bindex);
10552 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10553 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10554 +       err = PTR_ERR(dyaop);
10555 +       if (IS_ERR(dyaop))
10556 +               /* unnecessary to call dy_fput() */
10557 +               goto out;
10558 +
10559 +       err = 0;
10560 +       inode->i_mapping->a_ops = &dyaop->da_op;
10561 +
10562 +out:
10563 +       return err;
10564 +}
10565 +
10566 +/*
10567 + * Is it safe to replace a_ops during the inode/file is in operation?
10568 + * Yes, I hope so.
10569 + */
10570 +int au_dy_irefresh(struct inode *inode)
10571 +{
10572 +       int err;
10573 +       aufs_bindex_t bstart;
10574 +       struct inode *h_inode;
10575 +
10576 +       err = 0;
10577 +       if (S_ISREG(inode->i_mode)) {
10578 +               bstart = au_ibstart(inode);
10579 +               h_inode = au_h_iptr(inode, bstart);
10580 +               err = au_dy_iaop(inode, bstart, h_inode);
10581 +       }
10582 +       return err;
10583 +}
10584 +
10585 +void au_dy_arefresh(int do_dx)
10586 +{
10587 +       struct au_splhead *spl;
10588 +       struct list_head *head;
10589 +       struct au_dykey *key;
10590 +
10591 +       spl = dynop + AuDy_AOP;
10592 +       head = &spl->head;
10593 +       spin_lock(&spl->spin);
10594 +       list_for_each_entry(key, head, dk_list)
10595 +               dy_adx((void *)key, do_dx);
10596 +       spin_unlock(&spl->spin);
10597 +}
10598 +
10599 +/* ---------------------------------------------------------------------- */
10600 +
10601 +void __init au_dy_init(void)
10602 +{
10603 +       int i;
10604 +
10605 +       /* make sure that 'struct au_dykey *' can be any type */
10606 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10607 +
10608 +       for (i = 0; i < AuDyLast; i++)
10609 +               au_spl_init(dynop + i);
10610 +}
10611 +
10612 +void au_dy_fin(void)
10613 +{
10614 +       int i;
10615 +
10616 +       for (i = 0; i < AuDyLast; i++)
10617 +               WARN_ON(!list_empty(&dynop[i].head));
10618 +}
10619 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10620 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10621 +++ linux/fs/aufs/dynop.h       2016-01-13 20:11:11.669760262 +0100
10622 @@ -0,0 +1,74 @@
10623 +/*
10624 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10625 + *
10626 + * This program, aufs is free software; you can redistribute it and/or modify
10627 + * it under the terms of the GNU General Public License as published by
10628 + * the Free Software Foundation; either version 2 of the License, or
10629 + * (at your option) any later version.
10630 + *
10631 + * This program is distributed in the hope that it will be useful,
10632 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10633 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10634 + * GNU General Public License for more details.
10635 + *
10636 + * You should have received a copy of the GNU General Public License
10637 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10638 + */
10639 +
10640 +/*
10641 + * dynamically customizable operations (for regular files only)
10642 + */
10643 +
10644 +#ifndef __AUFS_DYNOP_H__
10645 +#define __AUFS_DYNOP_H__
10646 +
10647 +#ifdef __KERNEL__
10648 +
10649 +#include <linux/fs.h>
10650 +#include <linux/kref.h>
10651 +
10652 +enum {AuDy_AOP, AuDyLast};
10653 +
10654 +struct au_dynop {
10655 +       int                                             dy_type;
10656 +       union {
10657 +               const void                              *dy_hop;
10658 +               const struct address_space_operations   *dy_haop;
10659 +       };
10660 +};
10661 +
10662 +struct au_dykey {
10663 +       union {
10664 +               struct list_head        dk_list;
10665 +               struct rcu_head         dk_rcu;
10666 +       };
10667 +       struct au_dynop         dk_op;
10668 +
10669 +       /*
10670 +        * during I am in the branch local array, kref is gotten. when the
10671 +        * branch is removed, kref is put.
10672 +        */
10673 +       struct kref             dk_kref;
10674 +};
10675 +
10676 +/* stop unioning since their sizes are very different from each other */
10677 +struct au_dyaop {
10678 +       struct au_dykey                 da_key;
10679 +       struct address_space_operations da_op; /* not const */
10680 +};
10681 +
10682 +/* ---------------------------------------------------------------------- */
10683 +
10684 +/* dynop.c */
10685 +struct au_branch;
10686 +void au_dy_put(struct au_dykey *key);
10687 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10688 +               struct inode *h_inode);
10689 +int au_dy_irefresh(struct inode *inode);
10690 +void au_dy_arefresh(int do_dio);
10691 +
10692 +void __init au_dy_init(void);
10693 +void au_dy_fin(void);
10694 +
10695 +#endif /* __KERNEL__ */
10696 +#endif /* __AUFS_DYNOP_H__ */
10697 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10698 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
10699 +++ linux/fs/aufs/export.c      2016-01-13 20:11:11.669760262 +0100
10700 @@ -0,0 +1,832 @@
10701 +/*
10702 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10703 + *
10704 + * This program, aufs is free software; you can redistribute it and/or modify
10705 + * it under the terms of the GNU General Public License as published by
10706 + * the Free Software Foundation; either version 2 of the License, or
10707 + * (at your option) any later version.
10708 + *
10709 + * This program is distributed in the hope that it will be useful,
10710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10712 + * GNU General Public License for more details.
10713 + *
10714 + * You should have received a copy of the GNU General Public License
10715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10716 + */
10717 +
10718 +/*
10719 + * export via nfs
10720 + */
10721 +
10722 +#include <linux/exportfs.h>
10723 +#include <linux/fs_struct.h>
10724 +#include <linux/namei.h>
10725 +#include <linux/nsproxy.h>
10726 +#include <linux/random.h>
10727 +#include <linux/writeback.h>
10728 +#include "../fs/mount.h"
10729 +#include "aufs.h"
10730 +
10731 +union conv {
10732 +#ifdef CONFIG_AUFS_INO_T_64
10733 +       __u32 a[2];
10734 +#else
10735 +       __u32 a[1];
10736 +#endif
10737 +       ino_t ino;
10738 +};
10739 +
10740 +static ino_t decode_ino(__u32 *a)
10741 +{
10742 +       union conv u;
10743 +
10744 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10745 +       u.a[0] = a[0];
10746 +#ifdef CONFIG_AUFS_INO_T_64
10747 +       u.a[1] = a[1];
10748 +#endif
10749 +       return u.ino;
10750 +}
10751 +
10752 +static void encode_ino(__u32 *a, ino_t ino)
10753 +{
10754 +       union conv u;
10755 +
10756 +       u.ino = ino;
10757 +       a[0] = u.a[0];
10758 +#ifdef CONFIG_AUFS_INO_T_64
10759 +       a[1] = u.a[1];
10760 +#endif
10761 +}
10762 +
10763 +/* NFS file handle */
10764 +enum {
10765 +       Fh_br_id,
10766 +       Fh_sigen,
10767 +#ifdef CONFIG_AUFS_INO_T_64
10768 +       /* support 64bit inode number */
10769 +       Fh_ino1,
10770 +       Fh_ino2,
10771 +       Fh_dir_ino1,
10772 +       Fh_dir_ino2,
10773 +#else
10774 +       Fh_ino1,
10775 +       Fh_dir_ino1,
10776 +#endif
10777 +       Fh_igen,
10778 +       Fh_h_type,
10779 +       Fh_tail,
10780 +
10781 +       Fh_ino = Fh_ino1,
10782 +       Fh_dir_ino = Fh_dir_ino1
10783 +};
10784 +
10785 +static int au_test_anon(struct dentry *dentry)
10786 +{
10787 +       /* note: read d_flags without d_lock */
10788 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10789 +}
10790 +
10791 +int au_test_nfsd(void)
10792 +{
10793 +       int ret;
10794 +       struct task_struct *tsk = current;
10795 +       char comm[sizeof(tsk->comm)];
10796 +
10797 +       ret = 0;
10798 +       if (tsk->flags & PF_KTHREAD) {
10799 +               get_task_comm(comm, tsk);
10800 +               ret = !strcmp(comm, "nfsd");
10801 +       }
10802 +
10803 +       return ret;
10804 +}
10805 +
10806 +/* ---------------------------------------------------------------------- */
10807 +/* inode generation external table */
10808 +
10809 +void au_xigen_inc(struct inode *inode)
10810 +{
10811 +       loff_t pos;
10812 +       ssize_t sz;
10813 +       __u32 igen;
10814 +       struct super_block *sb;
10815 +       struct au_sbinfo *sbinfo;
10816 +
10817 +       sb = inode->i_sb;
10818 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10819 +
10820 +       sbinfo = au_sbi(sb);
10821 +       pos = inode->i_ino;
10822 +       pos *= sizeof(igen);
10823 +       igen = inode->i_generation + 1;
10824 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10825 +                        sizeof(igen), &pos);
10826 +       if (sz == sizeof(igen))
10827 +               return; /* success */
10828 +
10829 +       if (unlikely(sz >= 0))
10830 +               AuIOErr("xigen error (%zd)\n", sz);
10831 +}
10832 +
10833 +int au_xigen_new(struct inode *inode)
10834 +{
10835 +       int err;
10836 +       loff_t pos;
10837 +       ssize_t sz;
10838 +       struct super_block *sb;
10839 +       struct au_sbinfo *sbinfo;
10840 +       struct file *file;
10841 +
10842 +       err = 0;
10843 +       /* todo: dirty, at mount time */
10844 +       if (inode->i_ino == AUFS_ROOT_INO)
10845 +               goto out;
10846 +       sb = inode->i_sb;
10847 +       SiMustAnyLock(sb);
10848 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10849 +               goto out;
10850 +
10851 +       err = -EFBIG;
10852 +       pos = inode->i_ino;
10853 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10854 +               AuIOErr1("too large i%lld\n", pos);
10855 +               goto out;
10856 +       }
10857 +       pos *= sizeof(inode->i_generation);
10858 +
10859 +       err = 0;
10860 +       sbinfo = au_sbi(sb);
10861 +       file = sbinfo->si_xigen;
10862 +       BUG_ON(!file);
10863 +
10864 +       if (vfsub_f_size_read(file)
10865 +           < pos + sizeof(inode->i_generation)) {
10866 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
10867 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
10868 +                                sizeof(inode->i_generation), &pos);
10869 +       } else
10870 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
10871 +                               sizeof(inode->i_generation), &pos);
10872 +       if (sz == sizeof(inode->i_generation))
10873 +               goto out; /* success */
10874 +
10875 +       err = sz;
10876 +       if (unlikely(sz >= 0)) {
10877 +               err = -EIO;
10878 +               AuIOErr("xigen error (%zd)\n", sz);
10879 +       }
10880 +
10881 +out:
10882 +       return err;
10883 +}
10884 +
10885 +int au_xigen_set(struct super_block *sb, struct file *base)
10886 +{
10887 +       int err;
10888 +       struct au_sbinfo *sbinfo;
10889 +       struct file *file;
10890 +
10891 +       SiMustWriteLock(sb);
10892 +
10893 +       sbinfo = au_sbi(sb);
10894 +       file = au_xino_create2(base, sbinfo->si_xigen);
10895 +       err = PTR_ERR(file);
10896 +       if (IS_ERR(file))
10897 +               goto out;
10898 +       err = 0;
10899 +       if (sbinfo->si_xigen)
10900 +               fput(sbinfo->si_xigen);
10901 +       sbinfo->si_xigen = file;
10902 +
10903 +out:
10904 +       return err;
10905 +}
10906 +
10907 +void au_xigen_clr(struct super_block *sb)
10908 +{
10909 +       struct au_sbinfo *sbinfo;
10910 +
10911 +       SiMustWriteLock(sb);
10912 +
10913 +       sbinfo = au_sbi(sb);
10914 +       if (sbinfo->si_xigen) {
10915 +               fput(sbinfo->si_xigen);
10916 +               sbinfo->si_xigen = NULL;
10917 +       }
10918 +}
10919 +
10920 +/* ---------------------------------------------------------------------- */
10921 +
10922 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
10923 +                                   ino_t dir_ino)
10924 +{
10925 +       struct dentry *dentry, *d;
10926 +       struct inode *inode;
10927 +       unsigned int sigen;
10928 +
10929 +       dentry = NULL;
10930 +       inode = ilookup(sb, ino);
10931 +       if (!inode)
10932 +               goto out;
10933 +
10934 +       dentry = ERR_PTR(-ESTALE);
10935 +       sigen = au_sigen(sb);
10936 +       if (unlikely(is_bad_inode(inode)
10937 +                    || IS_DEADDIR(inode)
10938 +                    || sigen != au_iigen(inode, NULL)))
10939 +               goto out_iput;
10940 +
10941 +       dentry = NULL;
10942 +       if (!dir_ino || S_ISDIR(inode->i_mode))
10943 +               dentry = d_find_alias(inode);
10944 +       else {
10945 +               spin_lock(&inode->i_lock);
10946 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
10947 +                       spin_lock(&d->d_lock);
10948 +                       if (!au_test_anon(d)
10949 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
10950 +                               dentry = dget_dlock(d);
10951 +                               spin_unlock(&d->d_lock);
10952 +                               break;
10953 +                       }
10954 +                       spin_unlock(&d->d_lock);
10955 +               }
10956 +               spin_unlock(&inode->i_lock);
10957 +       }
10958 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
10959 +               /* need to refresh */
10960 +               dput(dentry);
10961 +               dentry = NULL;
10962 +       }
10963 +
10964 +out_iput:
10965 +       iput(inode);
10966 +out:
10967 +       AuTraceErrPtr(dentry);
10968 +       return dentry;
10969 +}
10970 +
10971 +/* ---------------------------------------------------------------------- */
10972 +
10973 +/* todo: dirty? */
10974 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
10975 +
10976 +struct au_compare_mnt_args {
10977 +       /* input */
10978 +       struct super_block *sb;
10979 +
10980 +       /* output */
10981 +       struct vfsmount *mnt;
10982 +};
10983 +
10984 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
10985 +{
10986 +       struct au_compare_mnt_args *a = arg;
10987 +
10988 +       if (mnt->mnt_sb != a->sb)
10989 +               return 0;
10990 +       a->mnt = mntget(mnt);
10991 +       return 1;
10992 +}
10993 +
10994 +static struct vfsmount *au_mnt_get(struct super_block *sb)
10995 +{
10996 +       int err;
10997 +       struct path root;
10998 +       struct au_compare_mnt_args args = {
10999 +               .sb = sb
11000 +       };
11001 +
11002 +       get_fs_root(current->fs, &root);
11003 +       rcu_read_lock();
11004 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
11005 +       rcu_read_unlock();
11006 +       path_put(&root);
11007 +       AuDebugOn(!err);
11008 +       AuDebugOn(!args.mnt);
11009 +       return args.mnt;
11010 +}
11011 +
11012 +struct au_nfsd_si_lock {
11013 +       unsigned int sigen;
11014 +       aufs_bindex_t bindex, br_id;
11015 +       unsigned char force_lock;
11016 +};
11017 +
11018 +static int si_nfsd_read_lock(struct super_block *sb,
11019 +                            struct au_nfsd_si_lock *nsi_lock)
11020 +{
11021 +       int err;
11022 +       aufs_bindex_t bindex;
11023 +
11024 +       si_read_lock(sb, AuLock_FLUSH);
11025 +
11026 +       /* branch id may be wrapped around */
11027 +       err = 0;
11028 +       bindex = au_br_index(sb, nsi_lock->br_id);
11029 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11030 +               goto out; /* success */
11031 +
11032 +       err = -ESTALE;
11033 +       bindex = -1;
11034 +       if (!nsi_lock->force_lock)
11035 +               si_read_unlock(sb);
11036 +
11037 +out:
11038 +       nsi_lock->bindex = bindex;
11039 +       return err;
11040 +}
11041 +
11042 +struct find_name_by_ino {
11043 +       struct dir_context ctx;
11044 +       int called, found;
11045 +       ino_t ino;
11046 +       char *name;
11047 +       int namelen;
11048 +};
11049 +
11050 +static int
11051 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11052 +                loff_t offset, u64 ino, unsigned int d_type)
11053 +{
11054 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11055 +                                                 ctx);
11056 +
11057 +       a->called++;
11058 +       if (a->ino != ino)
11059 +               return 0;
11060 +
11061 +       memcpy(a->name, name, namelen);
11062 +       a->namelen = namelen;
11063 +       a->found = 1;
11064 +       return 1;
11065 +}
11066 +
11067 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11068 +                                    struct au_nfsd_si_lock *nsi_lock)
11069 +{
11070 +       struct dentry *dentry, *parent;
11071 +       struct file *file;
11072 +       struct inode *dir;
11073 +       struct find_name_by_ino arg = {
11074 +               .ctx = {
11075 +                       .actor = find_name_by_ino
11076 +               }
11077 +       };
11078 +       int err;
11079 +
11080 +       parent = path->dentry;
11081 +       if (nsi_lock)
11082 +               si_read_unlock(parent->d_sb);
11083 +       file = vfsub_dentry_open(path, au_dir_roflags);
11084 +       dentry = (void *)file;
11085 +       if (IS_ERR(file))
11086 +               goto out;
11087 +
11088 +       dentry = ERR_PTR(-ENOMEM);
11089 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11090 +       if (unlikely(!arg.name))
11091 +               goto out_file;
11092 +       arg.ino = ino;
11093 +       arg.found = 0;
11094 +       do {
11095 +               arg.called = 0;
11096 +               /* smp_mb(); */
11097 +               err = vfsub_iterate_dir(file, &arg.ctx);
11098 +       } while (!err && !arg.found && arg.called);
11099 +       dentry = ERR_PTR(err);
11100 +       if (unlikely(err))
11101 +               goto out_name;
11102 +       /* instead of ENOENT */
11103 +       dentry = ERR_PTR(-ESTALE);
11104 +       if (!arg.found)
11105 +               goto out_name;
11106 +
11107 +       /* do not call vfsub_lkup_one() */
11108 +       dir = d_inode(parent);
11109 +       mutex_lock(&dir->i_mutex);
11110 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
11111 +       mutex_unlock(&dir->i_mutex);
11112 +       AuTraceErrPtr(dentry);
11113 +       if (IS_ERR(dentry))
11114 +               goto out_name;
11115 +       AuDebugOn(au_test_anon(dentry));
11116 +       if (unlikely(d_really_is_negative(dentry))) {
11117 +               dput(dentry);
11118 +               dentry = ERR_PTR(-ENOENT);
11119 +       }
11120 +
11121 +out_name:
11122 +       free_page((unsigned long)arg.name);
11123 +out_file:
11124 +       fput(file);
11125 +out:
11126 +       if (unlikely(nsi_lock
11127 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11128 +               if (!IS_ERR(dentry)) {
11129 +                       dput(dentry);
11130 +                       dentry = ERR_PTR(-ESTALE);
11131 +               }
11132 +       AuTraceErrPtr(dentry);
11133 +       return dentry;
11134 +}
11135 +
11136 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11137 +                                       ino_t dir_ino,
11138 +                                       struct au_nfsd_si_lock *nsi_lock)
11139 +{
11140 +       struct dentry *dentry;
11141 +       struct path path;
11142 +
11143 +       if (dir_ino != AUFS_ROOT_INO) {
11144 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11145 +               dentry = path.dentry;
11146 +               if (!path.dentry || IS_ERR(path.dentry))
11147 +                       goto out;
11148 +               AuDebugOn(au_test_anon(path.dentry));
11149 +       } else
11150 +               path.dentry = dget(sb->s_root);
11151 +
11152 +       path.mnt = au_mnt_get(sb);
11153 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11154 +       path_put(&path);
11155 +
11156 +out:
11157 +       AuTraceErrPtr(dentry);
11158 +       return dentry;
11159 +}
11160 +
11161 +/* ---------------------------------------------------------------------- */
11162 +
11163 +static int h_acceptable(void *expv, struct dentry *dentry)
11164 +{
11165 +       return 1;
11166 +}
11167 +
11168 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11169 +                          char *buf, int len, struct super_block *sb)
11170 +{
11171 +       char *p;
11172 +       int n;
11173 +       struct path path;
11174 +
11175 +       p = d_path(h_rootpath, buf, len);
11176 +       if (IS_ERR(p))
11177 +               goto out;
11178 +       n = strlen(p);
11179 +
11180 +       path.mnt = h_rootpath->mnt;
11181 +       path.dentry = h_parent;
11182 +       p = d_path(&path, buf, len);
11183 +       if (IS_ERR(p))
11184 +               goto out;
11185 +       if (n != 1)
11186 +               p += n;
11187 +
11188 +       path.mnt = au_mnt_get(sb);
11189 +       path.dentry = sb->s_root;
11190 +       p = d_path(&path, buf, len - strlen(p));
11191 +       mntput(path.mnt);
11192 +       if (IS_ERR(p))
11193 +               goto out;
11194 +       if (n != 1)
11195 +               p[strlen(p)] = '/';
11196 +
11197 +out:
11198 +       AuTraceErrPtr(p);
11199 +       return p;
11200 +}
11201 +
11202 +static
11203 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11204 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11205 +{
11206 +       struct dentry *dentry, *h_parent, *root;
11207 +       struct super_block *h_sb;
11208 +       char *pathname, *p;
11209 +       struct vfsmount *h_mnt;
11210 +       struct au_branch *br;
11211 +       int err;
11212 +       struct path path;
11213 +
11214 +       br = au_sbr(sb, nsi_lock->bindex);
11215 +       h_mnt = au_br_mnt(br);
11216 +       h_sb = h_mnt->mnt_sb;
11217 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11218 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11219 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11220 +                                     h_acceptable, /*context*/NULL);
11221 +       dentry = h_parent;
11222 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11223 +               AuWarn1("%s decode_fh failed, %ld\n",
11224 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11225 +               goto out;
11226 +       }
11227 +       dentry = NULL;
11228 +       if (unlikely(au_test_anon(h_parent))) {
11229 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11230 +                       au_sbtype(h_sb));
11231 +               goto out_h_parent;
11232 +       }
11233 +
11234 +       dentry = ERR_PTR(-ENOMEM);
11235 +       pathname = (void *)__get_free_page(GFP_NOFS);
11236 +       if (unlikely(!pathname))
11237 +               goto out_h_parent;
11238 +
11239 +       root = sb->s_root;
11240 +       path.mnt = h_mnt;
11241 +       di_read_lock_parent(root, !AuLock_IR);
11242 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11243 +       di_read_unlock(root, !AuLock_IR);
11244 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11245 +       dentry = (void *)p;
11246 +       if (IS_ERR(p))
11247 +               goto out_pathname;
11248 +
11249 +       si_read_unlock(sb);
11250 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11251 +       dentry = ERR_PTR(err);
11252 +       if (unlikely(err))
11253 +               goto out_relock;
11254 +
11255 +       dentry = ERR_PTR(-ENOENT);
11256 +       AuDebugOn(au_test_anon(path.dentry));
11257 +       if (unlikely(d_really_is_negative(path.dentry)))
11258 +               goto out_path;
11259 +
11260 +       if (ino != d_inode(path.dentry)->i_ino)
11261 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11262 +       else
11263 +               dentry = dget(path.dentry);
11264 +
11265 +out_path:
11266 +       path_put(&path);
11267 +out_relock:
11268 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11269 +               if (!IS_ERR(dentry)) {
11270 +                       dput(dentry);
11271 +                       dentry = ERR_PTR(-ESTALE);
11272 +               }
11273 +out_pathname:
11274 +       free_page((unsigned long)pathname);
11275 +out_h_parent:
11276 +       dput(h_parent);
11277 +out:
11278 +       AuTraceErrPtr(dentry);
11279 +       return dentry;
11280 +}
11281 +
11282 +/* ---------------------------------------------------------------------- */
11283 +
11284 +static struct dentry *
11285 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11286 +                 int fh_type)
11287 +{
11288 +       struct dentry *dentry;
11289 +       __u32 *fh = fid->raw;
11290 +       struct au_branch *br;
11291 +       ino_t ino, dir_ino;
11292 +       struct au_nfsd_si_lock nsi_lock = {
11293 +               .force_lock     = 0
11294 +       };
11295 +
11296 +       dentry = ERR_PTR(-ESTALE);
11297 +       /* it should never happen, but the file handle is unreliable */
11298 +       if (unlikely(fh_len < Fh_tail))
11299 +               goto out;
11300 +       nsi_lock.sigen = fh[Fh_sigen];
11301 +       nsi_lock.br_id = fh[Fh_br_id];
11302 +
11303 +       /* branch id may be wrapped around */
11304 +       br = NULL;
11305 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11306 +               goto out;
11307 +       nsi_lock.force_lock = 1;
11308 +
11309 +       /* is this inode still cached? */
11310 +       ino = decode_ino(fh + Fh_ino);
11311 +       /* it should never happen */
11312 +       if (unlikely(ino == AUFS_ROOT_INO))
11313 +               goto out;
11314 +
11315 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11316 +       dentry = decode_by_ino(sb, ino, dir_ino);
11317 +       if (IS_ERR(dentry))
11318 +               goto out_unlock;
11319 +       if (dentry)
11320 +               goto accept;
11321 +
11322 +       /* is the parent dir cached? */
11323 +       br = au_sbr(sb, nsi_lock.bindex);
11324 +       atomic_inc(&br->br_count);
11325 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11326 +       if (IS_ERR(dentry))
11327 +               goto out_unlock;
11328 +       if (dentry)
11329 +               goto accept;
11330 +
11331 +       /* lookup path */
11332 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11333 +       if (IS_ERR(dentry))
11334 +               goto out_unlock;
11335 +       if (unlikely(!dentry))
11336 +               /* todo?: make it ESTALE */
11337 +               goto out_unlock;
11338 +
11339 +accept:
11340 +       if (!au_digen_test(dentry, au_sigen(sb))
11341 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
11342 +               goto out_unlock; /* success */
11343 +
11344 +       dput(dentry);
11345 +       dentry = ERR_PTR(-ESTALE);
11346 +out_unlock:
11347 +       if (br)
11348 +               atomic_dec(&br->br_count);
11349 +       si_read_unlock(sb);
11350 +out:
11351 +       AuTraceErrPtr(dentry);
11352 +       return dentry;
11353 +}
11354 +
11355 +#if 0 /* reserved for future use */
11356 +/* support subtreecheck option */
11357 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11358 +                                       int fh_len, int fh_type)
11359 +{
11360 +       struct dentry *parent;
11361 +       __u32 *fh = fid->raw;
11362 +       ino_t dir_ino;
11363 +
11364 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11365 +       parent = decode_by_ino(sb, dir_ino, 0);
11366 +       if (IS_ERR(parent))
11367 +               goto out;
11368 +       if (!parent)
11369 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11370 +                                       dir_ino, fh, fh_len);
11371 +
11372 +out:
11373 +       AuTraceErrPtr(parent);
11374 +       return parent;
11375 +}
11376 +#endif
11377 +
11378 +/* ---------------------------------------------------------------------- */
11379 +
11380 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11381 +                         struct inode *dir)
11382 +{
11383 +       int err;
11384 +       aufs_bindex_t bindex;
11385 +       struct super_block *sb, *h_sb;
11386 +       struct dentry *dentry, *parent, *h_parent;
11387 +       struct inode *h_dir;
11388 +       struct au_branch *br;
11389 +
11390 +       err = -ENOSPC;
11391 +       if (unlikely(*max_len <= Fh_tail)) {
11392 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11393 +               goto out;
11394 +       }
11395 +
11396 +       err = FILEID_ROOT;
11397 +       if (inode->i_ino == AUFS_ROOT_INO) {
11398 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11399 +               goto out;
11400 +       }
11401 +
11402 +       h_parent = NULL;
11403 +       sb = inode->i_sb;
11404 +       err = si_read_lock(sb, AuLock_FLUSH);
11405 +       if (unlikely(err))
11406 +               goto out;
11407 +
11408 +#ifdef CONFIG_AUFS_DEBUG
11409 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11410 +               AuWarn1("NFS-exporting requires xino\n");
11411 +#endif
11412 +       err = -EIO;
11413 +       parent = NULL;
11414 +       ii_read_lock_child(inode);
11415 +       bindex = au_ibstart(inode);
11416 +       if (!dir) {
11417 +               dentry = d_find_any_alias(inode);
11418 +               if (unlikely(!dentry))
11419 +                       goto out_unlock;
11420 +               AuDebugOn(au_test_anon(dentry));
11421 +               parent = dget_parent(dentry);
11422 +               dput(dentry);
11423 +               if (unlikely(!parent))
11424 +                       goto out_unlock;
11425 +               if (d_really_is_positive(parent))
11426 +                       dir = d_inode(parent);
11427 +       }
11428 +
11429 +       ii_read_lock_parent(dir);
11430 +       h_dir = au_h_iptr(dir, bindex);
11431 +       ii_read_unlock(dir);
11432 +       if (unlikely(!h_dir))
11433 +               goto out_parent;
11434 +       h_parent = d_find_any_alias(h_dir);
11435 +       if (unlikely(!h_parent))
11436 +               goto out_hparent;
11437 +
11438 +       err = -EPERM;
11439 +       br = au_sbr(sb, bindex);
11440 +       h_sb = au_br_sb(br);
11441 +       if (unlikely(!h_sb->s_export_op)) {
11442 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11443 +               goto out_hparent;
11444 +       }
11445 +
11446 +       fh[Fh_br_id] = br->br_id;
11447 +       fh[Fh_sigen] = au_sigen(sb);
11448 +       encode_ino(fh + Fh_ino, inode->i_ino);
11449 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11450 +       fh[Fh_igen] = inode->i_generation;
11451 +
11452 +       *max_len -= Fh_tail;
11453 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11454 +                                          max_len,
11455 +                                          /*connectable or subtreecheck*/0);
11456 +       err = fh[Fh_h_type];
11457 +       *max_len += Fh_tail;
11458 +       /* todo: macros? */
11459 +       if (err != FILEID_INVALID)
11460 +               err = 99;
11461 +       else
11462 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11463 +
11464 +out_hparent:
11465 +       dput(h_parent);
11466 +out_parent:
11467 +       dput(parent);
11468 +out_unlock:
11469 +       ii_read_unlock(inode);
11470 +       si_read_unlock(sb);
11471 +out:
11472 +       if (unlikely(err < 0))
11473 +               err = FILEID_INVALID;
11474 +       return err;
11475 +}
11476 +
11477 +/* ---------------------------------------------------------------------- */
11478 +
11479 +static int aufs_commit_metadata(struct inode *inode)
11480 +{
11481 +       int err;
11482 +       aufs_bindex_t bindex;
11483 +       struct super_block *sb;
11484 +       struct inode *h_inode;
11485 +       int (*f)(struct inode *inode);
11486 +
11487 +       sb = inode->i_sb;
11488 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11489 +       ii_write_lock_child(inode);
11490 +       bindex = au_ibstart(inode);
11491 +       AuDebugOn(bindex < 0);
11492 +       h_inode = au_h_iptr(inode, bindex);
11493 +
11494 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11495 +       if (f)
11496 +               err = f(h_inode);
11497 +       else {
11498 +               struct writeback_control wbc = {
11499 +                       .sync_mode      = WB_SYNC_ALL,
11500 +                       .nr_to_write    = 0 /* metadata only */
11501 +               };
11502 +
11503 +               err = sync_inode(h_inode, &wbc);
11504 +       }
11505 +
11506 +       au_cpup_attr_timesizes(inode);
11507 +       ii_write_unlock(inode);
11508 +       si_read_unlock(sb);
11509 +       return err;
11510 +}
11511 +
11512 +/* ---------------------------------------------------------------------- */
11513 +
11514 +static struct export_operations aufs_export_op = {
11515 +       .fh_to_dentry           = aufs_fh_to_dentry,
11516 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11517 +       .encode_fh              = aufs_encode_fh,
11518 +       .commit_metadata        = aufs_commit_metadata
11519 +};
11520 +
11521 +void au_export_init(struct super_block *sb)
11522 +{
11523 +       struct au_sbinfo *sbinfo;
11524 +       __u32 u;
11525 +
11526 +       sb->s_export_op = &aufs_export_op;
11527 +       sbinfo = au_sbi(sb);
11528 +       sbinfo->si_xigen = NULL;
11529 +       get_random_bytes(&u, sizeof(u));
11530 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11531 +       atomic_set(&sbinfo->si_xigen_next, u);
11532 +}
11533 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11534 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11535 +++ linux/fs/aufs/fhsm.c        2016-01-13 20:11:11.669760262 +0100
11536 @@ -0,0 +1,426 @@
11537 +/*
11538 + * Copyright (C) 2011-2015 Junjiro R. Okajima
11539 + *
11540 + * This program, aufs is free software; you can redistribute it and/or modify
11541 + * it under the terms of the GNU General Public License as published by
11542 + * the Free Software Foundation; either version 2 of the License, or
11543 + * (at your option) any later version.
11544 + *
11545 + * This program is distributed in the hope that it will be useful,
11546 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11547 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11548 + * GNU General Public License for more details.
11549 + *
11550 + * You should have received a copy of the GNU General Public License
11551 + * along with this program; if not, write to the Free Software
11552 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11553 + */
11554 +
11555 +/*
11556 + * File-based Hierarchy Storage Management
11557 + */
11558 +
11559 +#include <linux/anon_inodes.h>
11560 +#include <linux/poll.h>
11561 +#include <linux/seq_file.h>
11562 +#include <linux/statfs.h>
11563 +#include "aufs.h"
11564 +
11565 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11566 +{
11567 +       struct au_sbinfo *sbinfo;
11568 +       struct au_fhsm *fhsm;
11569 +
11570 +       SiMustAnyLock(sb);
11571 +
11572 +       sbinfo = au_sbi(sb);
11573 +       fhsm = &sbinfo->si_fhsm;
11574 +       AuDebugOn(!fhsm);
11575 +       return fhsm->fhsm_bottom;
11576 +}
11577 +
11578 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11579 +{
11580 +       struct au_sbinfo *sbinfo;
11581 +       struct au_fhsm *fhsm;
11582 +
11583 +       SiMustWriteLock(sb);
11584 +
11585 +       sbinfo = au_sbi(sb);
11586 +       fhsm = &sbinfo->si_fhsm;
11587 +       AuDebugOn(!fhsm);
11588 +       fhsm->fhsm_bottom = bindex;
11589 +}
11590 +
11591 +/* ---------------------------------------------------------------------- */
11592 +
11593 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11594 +{
11595 +       struct au_br_fhsm *bf;
11596 +
11597 +       bf = br->br_fhsm;
11598 +       MtxMustLock(&bf->bf_lock);
11599 +
11600 +       return !bf->bf_readable
11601 +               || time_after(jiffies,
11602 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11603 +}
11604 +
11605 +/* ---------------------------------------------------------------------- */
11606 +
11607 +static void au_fhsm_notify(struct super_block *sb, int val)
11608 +{
11609 +       struct au_sbinfo *sbinfo;
11610 +       struct au_fhsm *fhsm;
11611 +
11612 +       SiMustAnyLock(sb);
11613 +
11614 +       sbinfo = au_sbi(sb);
11615 +       fhsm = &sbinfo->si_fhsm;
11616 +       if (au_fhsm_pid(fhsm)
11617 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11618 +               atomic_set(&fhsm->fhsm_readable, val);
11619 +               if (val)
11620 +                       wake_up(&fhsm->fhsm_wqh);
11621 +       }
11622 +}
11623 +
11624 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11625 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11626 +{
11627 +       int err;
11628 +       struct au_branch *br;
11629 +       struct au_br_fhsm *bf;
11630 +
11631 +       br = au_sbr(sb, bindex);
11632 +       AuDebugOn(au_br_rdonly(br));
11633 +       bf = br->br_fhsm;
11634 +       AuDebugOn(!bf);
11635 +
11636 +       if (do_lock)
11637 +               mutex_lock(&bf->bf_lock);
11638 +       else
11639 +               MtxMustLock(&bf->bf_lock);
11640 +
11641 +       /* sb->s_root for NFS is unreliable */
11642 +       err = au_br_stfs(br, &bf->bf_stfs);
11643 +       if (unlikely(err)) {
11644 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11645 +               goto out;
11646 +       }
11647 +
11648 +       bf->bf_jiffy = jiffies;
11649 +       bf->bf_readable = 1;
11650 +       if (do_notify)
11651 +               au_fhsm_notify(sb, /*val*/1);
11652 +       if (rstfs)
11653 +               *rstfs = bf->bf_stfs;
11654 +
11655 +out:
11656 +       if (do_lock)
11657 +               mutex_unlock(&bf->bf_lock);
11658 +       au_fhsm_notify(sb, /*val*/1);
11659 +
11660 +       return err;
11661 +}
11662 +
11663 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11664 +{
11665 +       int err;
11666 +       struct au_sbinfo *sbinfo;
11667 +       struct au_fhsm *fhsm;
11668 +       struct au_branch *br;
11669 +       struct au_br_fhsm *bf;
11670 +
11671 +       AuDbg("b%d, force %d\n", bindex, force);
11672 +       SiMustAnyLock(sb);
11673 +
11674 +       sbinfo = au_sbi(sb);
11675 +       fhsm = &sbinfo->si_fhsm;
11676 +       if (!au_ftest_si(sbinfo, FHSM)
11677 +           || fhsm->fhsm_bottom == bindex)
11678 +               return;
11679 +
11680 +       br = au_sbr(sb, bindex);
11681 +       bf = br->br_fhsm;
11682 +       AuDebugOn(!bf);
11683 +       mutex_lock(&bf->bf_lock);
11684 +       if (force
11685 +           || au_fhsm_pid(fhsm)
11686 +           || au_fhsm_test_jiffy(sbinfo, br))
11687 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11688 +                                 /*do_notify*/1);
11689 +       mutex_unlock(&bf->bf_lock);
11690 +}
11691 +
11692 +void au_fhsm_wrote_all(struct super_block *sb, int force)
11693 +{
11694 +       aufs_bindex_t bindex, bend;
11695 +       struct au_branch *br;
11696 +
11697 +       /* exclude the bottom */
11698 +       bend = au_fhsm_bottom(sb);
11699 +       for (bindex = 0; bindex < bend; bindex++) {
11700 +               br = au_sbr(sb, bindex);
11701 +               if (au_br_fhsm(br->br_perm))
11702 +                       au_fhsm_wrote(sb, bindex, force);
11703 +       }
11704 +}
11705 +
11706 +/* ---------------------------------------------------------------------- */
11707 +
11708 +static unsigned int au_fhsm_poll(struct file *file,
11709 +                                struct poll_table_struct *wait)
11710 +{
11711 +       unsigned int mask;
11712 +       struct au_sbinfo *sbinfo;
11713 +       struct au_fhsm *fhsm;
11714 +
11715 +       mask = 0;
11716 +       sbinfo = file->private_data;
11717 +       fhsm = &sbinfo->si_fhsm;
11718 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
11719 +       if (atomic_read(&fhsm->fhsm_readable))
11720 +               mask = POLLIN /* | POLLRDNORM */;
11721 +
11722 +       AuTraceErr((int)mask);
11723 +       return mask;
11724 +}
11725 +
11726 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11727 +                             struct aufs_stfs *stfs, __s16 brid)
11728 +{
11729 +       int err;
11730 +
11731 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11732 +       if (!err)
11733 +               err = __put_user(brid, &stbr->brid);
11734 +       if (unlikely(err))
11735 +               err = -EFAULT;
11736 +
11737 +       return err;
11738 +}
11739 +
11740 +static ssize_t au_fhsm_do_read(struct super_block *sb,
11741 +                              struct aufs_stbr __user *stbr, size_t count)
11742 +{
11743 +       ssize_t err;
11744 +       int nstbr;
11745 +       aufs_bindex_t bindex, bend;
11746 +       struct au_branch *br;
11747 +       struct au_br_fhsm *bf;
11748 +
11749 +       /* except the bottom branch */
11750 +       err = 0;
11751 +       nstbr = 0;
11752 +       bend = au_fhsm_bottom(sb);
11753 +       for (bindex = 0; !err && bindex < bend; bindex++) {
11754 +               br = au_sbr(sb, bindex);
11755 +               if (!au_br_fhsm(br->br_perm))
11756 +                       continue;
11757 +
11758 +               bf = br->br_fhsm;
11759 +               mutex_lock(&bf->bf_lock);
11760 +               if (bf->bf_readable) {
11761 +                       err = -EFAULT;
11762 +                       if (count >= sizeof(*stbr))
11763 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
11764 +                                                         br->br_id);
11765 +                       if (!err) {
11766 +                               bf->bf_readable = 0;
11767 +                               count -= sizeof(*stbr);
11768 +                               nstbr++;
11769 +                       }
11770 +               }
11771 +               mutex_unlock(&bf->bf_lock);
11772 +       }
11773 +       if (!err)
11774 +               err = sizeof(*stbr) * nstbr;
11775 +
11776 +       return err;
11777 +}
11778 +
11779 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
11780 +                          loff_t *pos)
11781 +{
11782 +       ssize_t err;
11783 +       int readable;
11784 +       aufs_bindex_t nfhsm, bindex, bend;
11785 +       struct au_sbinfo *sbinfo;
11786 +       struct au_fhsm *fhsm;
11787 +       struct au_branch *br;
11788 +       struct super_block *sb;
11789 +
11790 +       err = 0;
11791 +       sbinfo = file->private_data;
11792 +       fhsm = &sbinfo->si_fhsm;
11793 +need_data:
11794 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
11795 +       if (!atomic_read(&fhsm->fhsm_readable)) {
11796 +               if (vfsub_file_flags(file) & O_NONBLOCK)
11797 +                       err = -EAGAIN;
11798 +               else
11799 +                       err = wait_event_interruptible_locked_irq
11800 +                               (fhsm->fhsm_wqh,
11801 +                                atomic_read(&fhsm->fhsm_readable));
11802 +       }
11803 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
11804 +       if (unlikely(err))
11805 +               goto out;
11806 +
11807 +       /* sb may already be dead */
11808 +       au_rw_read_lock(&sbinfo->si_rwsem);
11809 +       readable = atomic_read(&fhsm->fhsm_readable);
11810 +       if (readable > 0) {
11811 +               sb = sbinfo->si_sb;
11812 +               AuDebugOn(!sb);
11813 +               /* exclude the bottom branch */
11814 +               nfhsm = 0;
11815 +               bend = au_fhsm_bottom(sb);
11816 +               for (bindex = 0; bindex < bend; bindex++) {
11817 +                       br = au_sbr(sb, bindex);
11818 +                       if (au_br_fhsm(br->br_perm))
11819 +                               nfhsm++;
11820 +               }
11821 +               err = -EMSGSIZE;
11822 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
11823 +                       atomic_set(&fhsm->fhsm_readable, 0);
11824 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
11825 +                                            count);
11826 +               }
11827 +       }
11828 +       au_rw_read_unlock(&sbinfo->si_rwsem);
11829 +       if (!readable)
11830 +               goto need_data;
11831 +
11832 +out:
11833 +       return err;
11834 +}
11835 +
11836 +static int au_fhsm_release(struct inode *inode, struct file *file)
11837 +{
11838 +       struct au_sbinfo *sbinfo;
11839 +       struct au_fhsm *fhsm;
11840 +
11841 +       /* sb may already be dead */
11842 +       sbinfo = file->private_data;
11843 +       fhsm = &sbinfo->si_fhsm;
11844 +       spin_lock(&fhsm->fhsm_spin);
11845 +       fhsm->fhsm_pid = 0;
11846 +       spin_unlock(&fhsm->fhsm_spin);
11847 +       kobject_put(&sbinfo->si_kobj);
11848 +
11849 +       return 0;
11850 +}
11851 +
11852 +static const struct file_operations au_fhsm_fops = {
11853 +       .owner          = THIS_MODULE,
11854 +       .llseek         = noop_llseek,
11855 +       .read           = au_fhsm_read,
11856 +       .poll           = au_fhsm_poll,
11857 +       .release        = au_fhsm_release
11858 +};
11859 +
11860 +int au_fhsm_fd(struct super_block *sb, int oflags)
11861 +{
11862 +       int err, fd;
11863 +       struct au_sbinfo *sbinfo;
11864 +       struct au_fhsm *fhsm;
11865 +
11866 +       err = -EPERM;
11867 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
11868 +               goto out;
11869 +
11870 +       err = -EINVAL;
11871 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
11872 +               goto out;
11873 +
11874 +       err = 0;
11875 +       sbinfo = au_sbi(sb);
11876 +       fhsm = &sbinfo->si_fhsm;
11877 +       spin_lock(&fhsm->fhsm_spin);
11878 +       if (!fhsm->fhsm_pid)
11879 +               fhsm->fhsm_pid = current->pid;
11880 +       else
11881 +               err = -EBUSY;
11882 +       spin_unlock(&fhsm->fhsm_spin);
11883 +       if (unlikely(err))
11884 +               goto out;
11885 +
11886 +       oflags |= O_RDONLY;
11887 +       /* oflags |= FMODE_NONOTIFY; */
11888 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
11889 +       err = fd;
11890 +       if (unlikely(fd < 0))
11891 +               goto out_pid;
11892 +
11893 +       /* succeed reglardless 'fhsm' status */
11894 +       kobject_get(&sbinfo->si_kobj);
11895 +       si_noflush_read_lock(sb);
11896 +       if (au_ftest_si(sbinfo, FHSM))
11897 +               au_fhsm_wrote_all(sb, /*force*/0);
11898 +       si_read_unlock(sb);
11899 +       goto out; /* success */
11900 +
11901 +out_pid:
11902 +       spin_lock(&fhsm->fhsm_spin);
11903 +       fhsm->fhsm_pid = 0;
11904 +       spin_unlock(&fhsm->fhsm_spin);
11905 +out:
11906 +       AuTraceErr(err);
11907 +       return err;
11908 +}
11909 +
11910 +/* ---------------------------------------------------------------------- */
11911 +
11912 +int au_fhsm_br_alloc(struct au_branch *br)
11913 +{
11914 +       int err;
11915 +
11916 +       err = 0;
11917 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
11918 +       if (br->br_fhsm)
11919 +               au_br_fhsm_init(br->br_fhsm);
11920 +       else
11921 +               err = -ENOMEM;
11922 +
11923 +       return err;
11924 +}
11925 +
11926 +/* ---------------------------------------------------------------------- */
11927 +
11928 +void au_fhsm_fin(struct super_block *sb)
11929 +{
11930 +       au_fhsm_notify(sb, /*val*/-1);
11931 +}
11932 +
11933 +void au_fhsm_init(struct au_sbinfo *sbinfo)
11934 +{
11935 +       struct au_fhsm *fhsm;
11936 +
11937 +       fhsm = &sbinfo->si_fhsm;
11938 +       spin_lock_init(&fhsm->fhsm_spin);
11939 +       init_waitqueue_head(&fhsm->fhsm_wqh);
11940 +       atomic_set(&fhsm->fhsm_readable, 0);
11941 +       fhsm->fhsm_expire
11942 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
11943 +       fhsm->fhsm_bottom = -1;
11944 +}
11945 +
11946 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
11947 +{
11948 +       sbinfo->si_fhsm.fhsm_expire
11949 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
11950 +}
11951 +
11952 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
11953 +{
11954 +       unsigned int u;
11955 +
11956 +       if (!au_ftest_si(sbinfo, FHSM))
11957 +               return;
11958 +
11959 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
11960 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
11961 +               seq_printf(seq, ",fhsm_sec=%u", u);
11962 +}
11963 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
11964 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
11965 +++ linux/fs/aufs/file.c        2016-01-13 20:11:11.669760262 +0100
11966 @@ -0,0 +1,844 @@
11967 +/*
11968 + * Copyright (C) 2005-2015 Junjiro R. Okajima
11969 + *
11970 + * This program, aufs is free software; you can redistribute it and/or modify
11971 + * it under the terms of the GNU General Public License as published by
11972 + * the Free Software Foundation; either version 2 of the License, or
11973 + * (at your option) any later version.
11974 + *
11975 + * This program is distributed in the hope that it will be useful,
11976 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11977 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11978 + * GNU General Public License for more details.
11979 + *
11980 + * You should have received a copy of the GNU General Public License
11981 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11982 + */
11983 +
11984 +/*
11985 + * handling file/dir, and address_space operation
11986 + */
11987 +
11988 +#ifdef CONFIG_AUFS_DEBUG
11989 +#include <linux/migrate.h>
11990 +#endif
11991 +#include <linux/pagemap.h>
11992 +#include "aufs.h"
11993 +
11994 +/* drop flags for writing */
11995 +unsigned int au_file_roflags(unsigned int flags)
11996 +{
11997 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
11998 +       flags |= O_RDONLY | O_NOATIME;
11999 +       return flags;
12000 +}
12001 +
12002 +/* common functions to regular file and dir */
12003 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12004 +                      struct file *file, int force_wr)
12005 +{
12006 +       struct file *h_file;
12007 +       struct dentry *h_dentry;
12008 +       struct inode *h_inode;
12009 +       struct super_block *sb;
12010 +       struct au_branch *br;
12011 +       struct path h_path;
12012 +       int err;
12013 +
12014 +       /* a race condition can happen between open and unlink/rmdir */
12015 +       h_file = ERR_PTR(-ENOENT);
12016 +       h_dentry = au_h_dptr(dentry, bindex);
12017 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
12018 +               goto out;
12019 +       h_inode = d_inode(h_dentry);
12020 +       spin_lock(&h_dentry->d_lock);
12021 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
12022 +               /* || !d_inode(dentry)->i_nlink */
12023 +               ;
12024 +       spin_unlock(&h_dentry->d_lock);
12025 +       if (unlikely(err))
12026 +               goto out;
12027 +
12028 +       sb = dentry->d_sb;
12029 +       br = au_sbr(sb, bindex);
12030 +       err = au_br_test_oflag(flags, br);
12031 +       h_file = ERR_PTR(err);
12032 +       if (unlikely(err))
12033 +               goto out;
12034 +
12035 +       /* drop flags for writing */
12036 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
12037 +               if (force_wr && !(flags & O_WRONLY))
12038 +                       force_wr = 0;
12039 +               flags = au_file_roflags(flags);
12040 +               if (force_wr) {
12041 +                       h_file = ERR_PTR(-EROFS);
12042 +                       flags = au_file_roflags(flags);
12043 +                       if (unlikely(vfsub_native_ro(h_inode)
12044 +                                    || IS_APPEND(h_inode)))
12045 +                               goto out;
12046 +                       flags &= ~O_ACCMODE;
12047 +                       flags |= O_WRONLY;
12048 +               }
12049 +       }
12050 +       flags &= ~O_CREAT;
12051 +       atomic_inc(&br->br_count);
12052 +       h_path.dentry = h_dentry;
12053 +       h_path.mnt = au_br_mnt(br);
12054 +       h_file = vfsub_dentry_open(&h_path, flags);
12055 +       if (IS_ERR(h_file))
12056 +               goto out_br;
12057 +
12058 +       if (flags & __FMODE_EXEC) {
12059 +               err = deny_write_access(h_file);
12060 +               if (unlikely(err)) {
12061 +                       fput(h_file);
12062 +                       h_file = ERR_PTR(err);
12063 +                       goto out_br;
12064 +               }
12065 +       }
12066 +       fsnotify_open(h_file);
12067 +       goto out; /* success */
12068 +
12069 +out_br:
12070 +       atomic_dec(&br->br_count);
12071 +out:
12072 +       return h_file;
12073 +}
12074 +
12075 +static int au_cmoo(struct dentry *dentry)
12076 +{
12077 +       int err, cmoo;
12078 +       unsigned int udba;
12079 +       struct path h_path;
12080 +       struct au_pin pin;
12081 +       struct au_cp_generic cpg = {
12082 +               .dentry = dentry,
12083 +               .bdst   = -1,
12084 +               .bsrc   = -1,
12085 +               .len    = -1,
12086 +               .pin    = &pin,
12087 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12088 +       };
12089 +       struct inode *delegated;
12090 +       struct super_block *sb;
12091 +       struct au_sbinfo *sbinfo;
12092 +       struct au_fhsm *fhsm;
12093 +       pid_t pid;
12094 +       struct au_branch *br;
12095 +       struct dentry *parent;
12096 +       struct au_hinode *hdir;
12097 +
12098 +       DiMustWriteLock(dentry);
12099 +       IiMustWriteLock(d_inode(dentry));
12100 +
12101 +       err = 0;
12102 +       if (IS_ROOT(dentry))
12103 +               goto out;
12104 +       cpg.bsrc = au_dbstart(dentry);
12105 +       if (!cpg.bsrc)
12106 +               goto out;
12107 +
12108 +       sb = dentry->d_sb;
12109 +       sbinfo = au_sbi(sb);
12110 +       fhsm = &sbinfo->si_fhsm;
12111 +       pid = au_fhsm_pid(fhsm);
12112 +       if (pid
12113 +           && (current->pid == pid
12114 +               || current->real_parent->pid == pid))
12115 +               goto out;
12116 +
12117 +       br = au_sbr(sb, cpg.bsrc);
12118 +       cmoo = au_br_cmoo(br->br_perm);
12119 +       if (!cmoo)
12120 +               goto out;
12121 +       if (!d_is_reg(dentry))
12122 +               cmoo &= AuBrAttr_COO_ALL;
12123 +       if (!cmoo)
12124 +               goto out;
12125 +
12126 +       parent = dget_parent(dentry);
12127 +       di_write_lock_parent(parent);
12128 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12129 +       cpg.bdst = err;
12130 +       if (unlikely(err < 0)) {
12131 +               err = 0;        /* there is no upper writable branch */
12132 +               goto out_dgrade;
12133 +       }
12134 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12135 +
12136 +       /* do not respect the coo attrib for the target branch */
12137 +       err = au_cpup_dirs(dentry, cpg.bdst);
12138 +       if (unlikely(err))
12139 +               goto out_dgrade;
12140 +
12141 +       di_downgrade_lock(parent, AuLock_IR);
12142 +       udba = au_opt_udba(sb);
12143 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12144 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12145 +       if (unlikely(err))
12146 +               goto out_parent;
12147 +
12148 +       err = au_sio_cpup_simple(&cpg);
12149 +       au_unpin(&pin);
12150 +       if (unlikely(err))
12151 +               goto out_parent;
12152 +       if (!(cmoo & AuBrWAttr_MOO))
12153 +               goto out_parent; /* success */
12154 +
12155 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12156 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12157 +       if (unlikely(err))
12158 +               goto out_parent;
12159 +
12160 +       h_path.mnt = au_br_mnt(br);
12161 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12162 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
12163 +       delegated = NULL;
12164 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12165 +       au_unpin(&pin);
12166 +       /* todo: keep h_dentry or not? */
12167 +       if (unlikely(err == -EWOULDBLOCK)) {
12168 +               pr_warn("cannot retry for NFSv4 delegation"
12169 +                       " for an internal unlink\n");
12170 +               iput(delegated);
12171 +       }
12172 +       if (unlikely(err)) {
12173 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12174 +                      dentry, err);
12175 +               err = 0;
12176 +       }
12177 +       goto out_parent; /* success */
12178 +
12179 +out_dgrade:
12180 +       di_downgrade_lock(parent, AuLock_IR);
12181 +out_parent:
12182 +       di_read_unlock(parent, AuLock_IR);
12183 +       dput(parent);
12184 +out:
12185 +       AuTraceErr(err);
12186 +       return err;
12187 +}
12188 +
12189 +int au_do_open(struct file *file, struct au_do_open_args *args)
12190 +{
12191 +       int err, no_lock = args->no_lock;
12192 +       struct dentry *dentry;
12193 +       struct au_finfo *finfo;
12194 +
12195 +       if (!no_lock)
12196 +               err = au_finfo_init(file, args->fidir);
12197 +       else {
12198 +               lockdep_off();
12199 +               err = au_finfo_init(file, args->fidir);
12200 +               lockdep_on();
12201 +       }
12202 +       if (unlikely(err))
12203 +               goto out;
12204 +
12205 +       dentry = file->f_path.dentry;
12206 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12207 +       if (!no_lock) {
12208 +               di_write_lock_child(dentry);
12209 +               err = au_cmoo(dentry);
12210 +               di_downgrade_lock(dentry, AuLock_IR);
12211 +               if (!err)
12212 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12213 +               di_read_unlock(dentry, AuLock_IR);
12214 +       } else {
12215 +               err = au_cmoo(dentry);
12216 +               if (!err)
12217 +                       err = args->open(file, vfsub_file_flags(file),
12218 +                                        args->h_file);
12219 +               if (!err && au_fbstart(file) != au_dbstart(dentry))
12220 +                       /*
12221 +                        * cmoo happens after h_file was opened.
12222 +                        * need to refresh file later.
12223 +                        */
12224 +                       atomic_dec(&au_fi(file)->fi_generation);
12225 +       }
12226 +
12227 +       finfo = au_fi(file);
12228 +       if (!err) {
12229 +               finfo->fi_file = file;
12230 +               au_sphl_add(&finfo->fi_hlist,
12231 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12232 +       }
12233 +       if (!no_lock)
12234 +               fi_write_unlock(file);
12235 +       else {
12236 +               lockdep_off();
12237 +               fi_write_unlock(file);
12238 +               lockdep_on();
12239 +       }
12240 +       if (unlikely(err)) {
12241 +               finfo->fi_hdir = NULL;
12242 +               au_finfo_fin(file);
12243 +       }
12244 +
12245 +out:
12246 +       return err;
12247 +}
12248 +
12249 +int au_reopen_nondir(struct file *file)
12250 +{
12251 +       int err;
12252 +       aufs_bindex_t bstart;
12253 +       struct dentry *dentry;
12254 +       struct file *h_file, *h_file_tmp;
12255 +
12256 +       dentry = file->f_path.dentry;
12257 +       bstart = au_dbstart(dentry);
12258 +       h_file_tmp = NULL;
12259 +       if (au_fbstart(file) == bstart) {
12260 +               h_file = au_hf_top(file);
12261 +               if (file->f_mode == h_file->f_mode)
12262 +                       return 0; /* success */
12263 +               h_file_tmp = h_file;
12264 +               get_file(h_file_tmp);
12265 +               au_set_h_fptr(file, bstart, NULL);
12266 +       }
12267 +       AuDebugOn(au_fi(file)->fi_hdir);
12268 +       /*
12269 +        * it can happen
12270 +        * file exists on both of rw and ro
12271 +        * open --> dbstart and fbstart are both 0
12272 +        * prepend a branch as rw, "rw" become ro
12273 +        * remove rw/file
12274 +        * delete the top branch, "rw" becomes rw again
12275 +        *      --> dbstart is 1, fbstart is still 0
12276 +        * write --> fbstart is 0 but dbstart is 1
12277 +        */
12278 +       /* AuDebugOn(au_fbstart(file) < bstart); */
12279 +
12280 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
12281 +                          file, /*force_wr*/0);
12282 +       err = PTR_ERR(h_file);
12283 +       if (IS_ERR(h_file)) {
12284 +               if (h_file_tmp) {
12285 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
12286 +                       au_set_h_fptr(file, bstart, h_file_tmp);
12287 +                       h_file_tmp = NULL;
12288 +               }
12289 +               goto out; /* todo: close all? */
12290 +       }
12291 +
12292 +       err = 0;
12293 +       au_set_fbstart(file, bstart);
12294 +       au_set_h_fptr(file, bstart, h_file);
12295 +       au_update_figen(file);
12296 +       /* todo: necessary? */
12297 +       /* file->f_ra = h_file->f_ra; */
12298 +
12299 +out:
12300 +       if (h_file_tmp)
12301 +               fput(h_file_tmp);
12302 +       return err;
12303 +}
12304 +
12305 +/* ---------------------------------------------------------------------- */
12306 +
12307 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12308 +                       struct dentry *hi_wh)
12309 +{
12310 +       int err;
12311 +       aufs_bindex_t bstart;
12312 +       struct au_dinfo *dinfo;
12313 +       struct dentry *h_dentry;
12314 +       struct au_hdentry *hdp;
12315 +
12316 +       dinfo = au_di(file->f_path.dentry);
12317 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12318 +
12319 +       bstart = dinfo->di_bstart;
12320 +       dinfo->di_bstart = btgt;
12321 +       hdp = dinfo->di_hdentry;
12322 +       h_dentry = hdp[0 + btgt].hd_dentry;
12323 +       hdp[0 + btgt].hd_dentry = hi_wh;
12324 +       err = au_reopen_nondir(file);
12325 +       hdp[0 + btgt].hd_dentry = h_dentry;
12326 +       dinfo->di_bstart = bstart;
12327 +
12328 +       return err;
12329 +}
12330 +
12331 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12332 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12333 +{
12334 +       int err;
12335 +       struct inode *inode, *h_inode;
12336 +       struct dentry *h_dentry, *hi_wh;
12337 +       struct au_cp_generic cpg = {
12338 +               .dentry = file->f_path.dentry,
12339 +               .bdst   = bcpup,
12340 +               .bsrc   = -1,
12341 +               .len    = len,
12342 +               .pin    = pin
12343 +       };
12344 +
12345 +       au_update_dbstart(cpg.dentry);
12346 +       inode = d_inode(cpg.dentry);
12347 +       h_inode = NULL;
12348 +       if (au_dbstart(cpg.dentry) <= bcpup
12349 +           && au_dbend(cpg.dentry) >= bcpup) {
12350 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12351 +               if (h_dentry && d_is_positive(h_dentry))
12352 +                       h_inode = d_inode(h_dentry);
12353 +       }
12354 +       hi_wh = au_hi_wh(inode, bcpup);
12355 +       if (!hi_wh && !h_inode)
12356 +               err = au_sio_cpup_wh(&cpg, file);
12357 +       else
12358 +               /* already copied-up after unlink */
12359 +               err = au_reopen_wh(file, bcpup, hi_wh);
12360 +
12361 +       if (!err
12362 +           && (inode->i_nlink > 1
12363 +               || (inode->i_state & I_LINKABLE))
12364 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12365 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12366 +
12367 +       return err;
12368 +}
12369 +
12370 +/*
12371 + * prepare the @file for writing.
12372 + */
12373 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12374 +{
12375 +       int err;
12376 +       aufs_bindex_t dbstart;
12377 +       struct dentry *parent;
12378 +       struct inode *inode;
12379 +       struct super_block *sb;
12380 +       struct file *h_file;
12381 +       struct au_cp_generic cpg = {
12382 +               .dentry = file->f_path.dentry,
12383 +               .bdst   = -1,
12384 +               .bsrc   = -1,
12385 +               .len    = len,
12386 +               .pin    = pin,
12387 +               .flags  = AuCpup_DTIME
12388 +       };
12389 +
12390 +       sb = cpg.dentry->d_sb;
12391 +       inode = d_inode(cpg.dentry);
12392 +       cpg.bsrc = au_fbstart(file);
12393 +       err = au_test_ro(sb, cpg.bsrc, inode);
12394 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12395 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12396 +                            /*flags*/0);
12397 +               goto out;
12398 +       }
12399 +
12400 +       /* need to cpup or reopen */
12401 +       parent = dget_parent(cpg.dentry);
12402 +       di_write_lock_parent(parent);
12403 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12404 +       cpg.bdst = err;
12405 +       if (unlikely(err < 0))
12406 +               goto out_dgrade;
12407 +       err = 0;
12408 +
12409 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12410 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12411 +               if (unlikely(err))
12412 +                       goto out_dgrade;
12413 +       }
12414 +
12415 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12416 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12417 +       if (unlikely(err))
12418 +               goto out_dgrade;
12419 +
12420 +       dbstart = au_dbstart(cpg.dentry);
12421 +       if (dbstart <= cpg.bdst)
12422 +               cpg.bsrc = cpg.bdst;
12423 +
12424 +       if (dbstart <= cpg.bdst         /* just reopen */
12425 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12426 +               ) {
12427 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12428 +               if (IS_ERR(h_file))
12429 +                       err = PTR_ERR(h_file);
12430 +               else {
12431 +                       di_downgrade_lock(parent, AuLock_IR);
12432 +                       if (dbstart > cpg.bdst)
12433 +                               err = au_sio_cpup_simple(&cpg);
12434 +                       if (!err)
12435 +                               err = au_reopen_nondir(file);
12436 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12437 +               }
12438 +       } else {                        /* copyup as wh and reopen */
12439 +               /*
12440 +                * since writable hfsplus branch is not supported,
12441 +                * h_open_pre/post() are unnecessary.
12442 +                */
12443 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12444 +               di_downgrade_lock(parent, AuLock_IR);
12445 +       }
12446 +
12447 +       if (!err) {
12448 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12449 +               goto out_dput; /* success */
12450 +       }
12451 +       au_unpin(pin);
12452 +       goto out_unlock;
12453 +
12454 +out_dgrade:
12455 +       di_downgrade_lock(parent, AuLock_IR);
12456 +out_unlock:
12457 +       di_read_unlock(parent, AuLock_IR);
12458 +out_dput:
12459 +       dput(parent);
12460 +out:
12461 +       return err;
12462 +}
12463 +
12464 +/* ---------------------------------------------------------------------- */
12465 +
12466 +int au_do_flush(struct file *file, fl_owner_t id,
12467 +               int (*flush)(struct file *file, fl_owner_t id))
12468 +{
12469 +       int err;
12470 +       struct super_block *sb;
12471 +       struct inode *inode;
12472 +
12473 +       inode = file_inode(file);
12474 +       sb = inode->i_sb;
12475 +       si_noflush_read_lock(sb);
12476 +       fi_read_lock(file);
12477 +       ii_read_lock_child(inode);
12478 +
12479 +       err = flush(file, id);
12480 +       au_cpup_attr_timesizes(inode);
12481 +
12482 +       ii_read_unlock(inode);
12483 +       fi_read_unlock(file);
12484 +       si_read_unlock(sb);
12485 +       return err;
12486 +}
12487 +
12488 +/* ---------------------------------------------------------------------- */
12489 +
12490 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12491 +{
12492 +       int err;
12493 +       struct au_pin pin;
12494 +       struct au_finfo *finfo;
12495 +       struct dentry *parent, *hi_wh;
12496 +       struct inode *inode;
12497 +       struct super_block *sb;
12498 +       struct au_cp_generic cpg = {
12499 +               .dentry = file->f_path.dentry,
12500 +               .bdst   = -1,
12501 +               .bsrc   = -1,
12502 +               .len    = -1,
12503 +               .pin    = &pin,
12504 +               .flags  = AuCpup_DTIME
12505 +       };
12506 +
12507 +       FiMustWriteLock(file);
12508 +
12509 +       err = 0;
12510 +       finfo = au_fi(file);
12511 +       sb = cpg.dentry->d_sb;
12512 +       inode = d_inode(cpg.dentry);
12513 +       cpg.bdst = au_ibstart(inode);
12514 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12515 +               goto out;
12516 +
12517 +       parent = dget_parent(cpg.dentry);
12518 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12519 +               di_read_lock_parent(parent, !AuLock_IR);
12520 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12521 +               cpg.bdst = err;
12522 +               di_read_unlock(parent, !AuLock_IR);
12523 +               if (unlikely(err < 0))
12524 +                       goto out_parent;
12525 +               err = 0;
12526 +       }
12527 +
12528 +       di_read_lock_parent(parent, AuLock_IR);
12529 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12530 +       if (!S_ISDIR(inode->i_mode)
12531 +           && au_opt_test(au_mntflags(sb), PLINK)
12532 +           && au_plink_test(inode)
12533 +           && !d_unhashed(cpg.dentry)
12534 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
12535 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12536 +               if (unlikely(err))
12537 +                       goto out_unlock;
12538 +
12539 +               /* always superio. */
12540 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12541 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12542 +               if (!err) {
12543 +                       err = au_sio_cpup_simple(&cpg);
12544 +                       au_unpin(&pin);
12545 +               }
12546 +       } else if (hi_wh) {
12547 +               /* already copied-up after unlink */
12548 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12549 +               *need_reopen = 0;
12550 +       }
12551 +
12552 +out_unlock:
12553 +       di_read_unlock(parent, AuLock_IR);
12554 +out_parent:
12555 +       dput(parent);
12556 +out:
12557 +       return err;
12558 +}
12559 +
12560 +static void au_do_refresh_dir(struct file *file)
12561 +{
12562 +       aufs_bindex_t bindex, bend, new_bindex, brid;
12563 +       struct au_hfile *p, tmp, *q;
12564 +       struct au_finfo *finfo;
12565 +       struct super_block *sb;
12566 +       struct au_fidir *fidir;
12567 +
12568 +       FiMustWriteLock(file);
12569 +
12570 +       sb = file->f_path.dentry->d_sb;
12571 +       finfo = au_fi(file);
12572 +       fidir = finfo->fi_hdir;
12573 +       AuDebugOn(!fidir);
12574 +       p = fidir->fd_hfile + finfo->fi_btop;
12575 +       brid = p->hf_br->br_id;
12576 +       bend = fidir->fd_bbot;
12577 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
12578 +               if (!p->hf_file)
12579 +                       continue;
12580 +
12581 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12582 +               if (new_bindex == bindex)
12583 +                       continue;
12584 +               if (new_bindex < 0) {
12585 +                       au_set_h_fptr(file, bindex, NULL);
12586 +                       continue;
12587 +               }
12588 +
12589 +               /* swap two lower inode, and loop again */
12590 +               q = fidir->fd_hfile + new_bindex;
12591 +               tmp = *q;
12592 +               *q = *p;
12593 +               *p = tmp;
12594 +               if (tmp.hf_file) {
12595 +                       bindex--;
12596 +                       p--;
12597 +               }
12598 +       }
12599 +
12600 +       p = fidir->fd_hfile;
12601 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12602 +               bend = au_sbend(sb);
12603 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
12604 +                    finfo->fi_btop++, p++)
12605 +                       if (p->hf_file) {
12606 +                               if (file_inode(p->hf_file))
12607 +                                       break;
12608 +                               au_hfput(p, file);
12609 +                       }
12610 +       } else {
12611 +               bend = au_br_index(sb, brid);
12612 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
12613 +                    finfo->fi_btop++, p++)
12614 +                       if (p->hf_file)
12615 +                               au_hfput(p, file);
12616 +               bend = au_sbend(sb);
12617 +       }
12618 +
12619 +       p = fidir->fd_hfile + bend;
12620 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
12621 +            fidir->fd_bbot--, p--)
12622 +               if (p->hf_file) {
12623 +                       if (file_inode(p->hf_file))
12624 +                               break;
12625 +                       au_hfput(p, file);
12626 +               }
12627 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12628 +}
12629 +
12630 +/*
12631 + * after branch manipulating, refresh the file.
12632 + */
12633 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12634 +{
12635 +       int err, need_reopen;
12636 +       aufs_bindex_t bend, bindex;
12637 +       struct dentry *dentry;
12638 +       struct au_finfo *finfo;
12639 +       struct au_hfile *hfile;
12640 +
12641 +       dentry = file->f_path.dentry;
12642 +       finfo = au_fi(file);
12643 +       if (!finfo->fi_hdir) {
12644 +               hfile = &finfo->fi_htop;
12645 +               AuDebugOn(!hfile->hf_file);
12646 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12647 +               AuDebugOn(bindex < 0);
12648 +               if (bindex != finfo->fi_btop)
12649 +                       au_set_fbstart(file, bindex);
12650 +       } else {
12651 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
12652 +               if (unlikely(err))
12653 +                       goto out;
12654 +               au_do_refresh_dir(file);
12655 +       }
12656 +
12657 +       err = 0;
12658 +       need_reopen = 1;
12659 +       if (!au_test_mmapped(file))
12660 +               err = au_file_refresh_by_inode(file, &need_reopen);
12661 +       if (!err && need_reopen && !d_unlinked(dentry))
12662 +               err = reopen(file);
12663 +       if (!err) {
12664 +               au_update_figen(file);
12665 +               goto out; /* success */
12666 +       }
12667 +
12668 +       /* error, close all lower files */
12669 +       if (finfo->fi_hdir) {
12670 +               bend = au_fbend_dir(file);
12671 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
12672 +                       au_set_h_fptr(file, bindex, NULL);
12673 +       }
12674 +
12675 +out:
12676 +       return err;
12677 +}
12678 +
12679 +/* common function to regular file and dir */
12680 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12681 +                         int wlock)
12682 +{
12683 +       int err;
12684 +       unsigned int sigen, figen;
12685 +       aufs_bindex_t bstart;
12686 +       unsigned char pseudo_link;
12687 +       struct dentry *dentry;
12688 +       struct inode *inode;
12689 +
12690 +       err = 0;
12691 +       dentry = file->f_path.dentry;
12692 +       inode = d_inode(dentry);
12693 +       sigen = au_sigen(dentry->d_sb);
12694 +       fi_write_lock(file);
12695 +       figen = au_figen(file);
12696 +       di_write_lock_child(dentry);
12697 +       bstart = au_dbstart(dentry);
12698 +       pseudo_link = (bstart != au_ibstart(inode));
12699 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
12700 +               if (!wlock) {
12701 +                       di_downgrade_lock(dentry, AuLock_IR);
12702 +                       fi_downgrade_lock(file);
12703 +               }
12704 +               goto out; /* success */
12705 +       }
12706 +
12707 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
12708 +       if (au_digen_test(dentry, sigen)) {
12709 +               err = au_reval_dpath(dentry, sigen);
12710 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
12711 +       }
12712 +
12713 +       if (!err)
12714 +               err = refresh_file(file, reopen);
12715 +       if (!err) {
12716 +               if (!wlock) {
12717 +                       di_downgrade_lock(dentry, AuLock_IR);
12718 +                       fi_downgrade_lock(file);
12719 +               }
12720 +       } else {
12721 +               di_write_unlock(dentry);
12722 +               fi_write_unlock(file);
12723 +       }
12724 +
12725 +out:
12726 +       return err;
12727 +}
12728 +
12729 +/* ---------------------------------------------------------------------- */
12730 +
12731 +/* cf. aufs_nopage() */
12732 +/* for madvise(2) */
12733 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
12734 +{
12735 +       unlock_page(page);
12736 +       return 0;
12737 +}
12738 +
12739 +/* it will never be called, but necessary to support O_DIRECT */
12740 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
12741 +                             loff_t offset)
12742 +{ BUG(); return 0; }
12743 +
12744 +/* they will never be called. */
12745 +#ifdef CONFIG_AUFS_DEBUG
12746 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
12747 +                           loff_t pos, unsigned len, unsigned flags,
12748 +                           struct page **pagep, void **fsdata)
12749 +{ AuUnsupport(); return 0; }
12750 +static int aufs_write_end(struct file *file, struct address_space *mapping,
12751 +                         loff_t pos, unsigned len, unsigned copied,
12752 +                         struct page *page, void *fsdata)
12753 +{ AuUnsupport(); return 0; }
12754 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12755 +{ AuUnsupport(); return 0; }
12756 +
12757 +static int aufs_set_page_dirty(struct page *page)
12758 +{ AuUnsupport(); return 0; }
12759 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
12760 +                               unsigned int length)
12761 +{ AuUnsupport(); }
12762 +static int aufs_releasepage(struct page *page, gfp_t gfp)
12763 +{ AuUnsupport(); return 0; }
12764 +#if 0 /* called by memory compaction regardless file */
12765 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
12766 +                           struct page *page, enum migrate_mode mode)
12767 +{ AuUnsupport(); return 0; }
12768 +#endif
12769 +static int aufs_launder_page(struct page *page)
12770 +{ AuUnsupport(); return 0; }
12771 +static int aufs_is_partially_uptodate(struct page *page,
12772 +                                     unsigned long from,
12773 +                                     unsigned long count)
12774 +{ AuUnsupport(); return 0; }
12775 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
12776 +                                   bool *writeback)
12777 +{ AuUnsupport(); }
12778 +static int aufs_error_remove_page(struct address_space *mapping,
12779 +                                 struct page *page)
12780 +{ AuUnsupport(); return 0; }
12781 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
12782 +                             sector_t *span)
12783 +{ AuUnsupport(); return 0; }
12784 +static void aufs_swap_deactivate(struct file *file)
12785 +{ AuUnsupport(); }
12786 +#endif /* CONFIG_AUFS_DEBUG */
12787 +
12788 +const struct address_space_operations aufs_aop = {
12789 +       .readpage               = aufs_readpage,
12790 +       .direct_IO              = aufs_direct_IO,
12791 +#ifdef CONFIG_AUFS_DEBUG
12792 +       .writepage              = aufs_writepage,
12793 +       /* no writepages, because of writepage */
12794 +       .set_page_dirty         = aufs_set_page_dirty,
12795 +       /* no readpages, because of readpage */
12796 +       .write_begin            = aufs_write_begin,
12797 +       .write_end              = aufs_write_end,
12798 +       /* no bmap, no block device */
12799 +       .invalidatepage         = aufs_invalidatepage,
12800 +       .releasepage            = aufs_releasepage,
12801 +       /* is fallback_migrate_page ok? */
12802 +       /* .migratepage         = aufs_migratepage, */
12803 +       .launder_page           = aufs_launder_page,
12804 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
12805 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
12806 +       .error_remove_page      = aufs_error_remove_page,
12807 +       .swap_activate          = aufs_swap_activate,
12808 +       .swap_deactivate        = aufs_swap_deactivate
12809 +#endif /* CONFIG_AUFS_DEBUG */
12810 +};
12811 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
12812 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
12813 +++ linux/fs/aufs/file.h        2016-01-13 20:11:11.669760262 +0100
12814 @@ -0,0 +1,291 @@
12815 +/*
12816 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12817 + *
12818 + * This program, aufs is free software; you can redistribute it and/or modify
12819 + * it under the terms of the GNU General Public License as published by
12820 + * the Free Software Foundation; either version 2 of the License, or
12821 + * (at your option) any later version.
12822 + *
12823 + * This program is distributed in the hope that it will be useful,
12824 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12825 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12826 + * GNU General Public License for more details.
12827 + *
12828 + * You should have received a copy of the GNU General Public License
12829 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12830 + */
12831 +
12832 +/*
12833 + * file operations
12834 + */
12835 +
12836 +#ifndef __AUFS_FILE_H__
12837 +#define __AUFS_FILE_H__
12838 +
12839 +#ifdef __KERNEL__
12840 +
12841 +#include <linux/file.h>
12842 +#include <linux/fs.h>
12843 +#include <linux/poll.h>
12844 +#include "rwsem.h"
12845 +
12846 +struct au_branch;
12847 +struct au_hfile {
12848 +       struct file             *hf_file;
12849 +       struct au_branch        *hf_br;
12850 +};
12851 +
12852 +struct au_vdir;
12853 +struct au_fidir {
12854 +       aufs_bindex_t           fd_bbot;
12855 +       aufs_bindex_t           fd_nent;
12856 +       struct au_vdir          *fd_vdir_cache;
12857 +       struct au_hfile         fd_hfile[];
12858 +};
12859 +
12860 +static inline int au_fidir_sz(int nent)
12861 +{
12862 +       AuDebugOn(nent < 0);
12863 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
12864 +}
12865 +
12866 +struct au_finfo {
12867 +       atomic_t                fi_generation;
12868 +
12869 +       struct au_rwsem         fi_rwsem;
12870 +       aufs_bindex_t           fi_btop;
12871 +
12872 +       /* do not union them */
12873 +       struct {                                /* for non-dir */
12874 +               struct au_hfile                 fi_htop;
12875 +               atomic_t                        fi_mmapped;
12876 +       };
12877 +       struct au_fidir         *fi_hdir;       /* for dir only */
12878 +
12879 +       struct hlist_node       fi_hlist;
12880 +       struct file             *fi_file;       /* very ugly */
12881 +} ____cacheline_aligned_in_smp;
12882 +
12883 +/* ---------------------------------------------------------------------- */
12884 +
12885 +/* file.c */
12886 +extern const struct address_space_operations aufs_aop;
12887 +unsigned int au_file_roflags(unsigned int flags);
12888 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12889 +                      struct file *file, int force_wr);
12890 +struct au_do_open_args {
12891 +       int             no_lock;
12892 +       int             (*open)(struct file *file, int flags,
12893 +                               struct file *h_file);
12894 +       struct au_fidir *fidir;
12895 +       struct file     *h_file;
12896 +};
12897 +int au_do_open(struct file *file, struct au_do_open_args *args);
12898 +int au_reopen_nondir(struct file *file);
12899 +struct au_pin;
12900 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
12901 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12902 +                         int wlock);
12903 +int au_do_flush(struct file *file, fl_owner_t id,
12904 +               int (*flush)(struct file *file, fl_owner_t id));
12905 +
12906 +/* poll.c */
12907 +#ifdef CONFIG_AUFS_POLL
12908 +unsigned int aufs_poll(struct file *file, poll_table *wait);
12909 +#endif
12910 +
12911 +#ifdef CONFIG_AUFS_BR_HFSPLUS
12912 +/* hfsplus.c */
12913 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
12914 +                          int force_wr);
12915 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12916 +                   struct file *h_file);
12917 +#else
12918 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
12919 +       aufs_bindex_t bindex, int force_wr)
12920 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
12921 +          struct file *h_file);
12922 +#endif
12923 +
12924 +/* f_op.c */
12925 +extern const struct file_operations aufs_file_fop;
12926 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
12927 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
12928 +struct file *au_read_pre(struct file *file, int keep_fi);
12929 +
12930 +/* finfo.c */
12931 +void au_hfput(struct au_hfile *hf, struct file *file);
12932 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
12933 +                  struct file *h_file);
12934 +
12935 +void au_update_figen(struct file *file);
12936 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
12937 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
12938 +
12939 +void au_fi_init_once(void *_fi);
12940 +void au_finfo_fin(struct file *file);
12941 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
12942 +
12943 +/* ioctl.c */
12944 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
12945 +#ifdef CONFIG_COMPAT
12946 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
12947 +                          unsigned long arg);
12948 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
12949 +                             unsigned long arg);
12950 +#endif
12951 +
12952 +/* ---------------------------------------------------------------------- */
12953 +
12954 +static inline struct au_finfo *au_fi(struct file *file)
12955 +{
12956 +       return file->private_data;
12957 +}
12958 +
12959 +/* ---------------------------------------------------------------------- */
12960 +
12961 +/*
12962 + * fi_read_lock, fi_write_lock,
12963 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
12964 + */
12965 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
12966 +
12967 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
12968 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
12969 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
12970 +
12971 +/* ---------------------------------------------------------------------- */
12972 +
12973 +/* todo: hard/soft set? */
12974 +static inline aufs_bindex_t au_fbstart(struct file *file)
12975 +{
12976 +       FiMustAnyLock(file);
12977 +       return au_fi(file)->fi_btop;
12978 +}
12979 +
12980 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
12981 +{
12982 +       FiMustAnyLock(file);
12983 +       AuDebugOn(!au_fi(file)->fi_hdir);
12984 +       return au_fi(file)->fi_hdir->fd_bbot;
12985 +}
12986 +
12987 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
12988 +{
12989 +       FiMustAnyLock(file);
12990 +       AuDebugOn(!au_fi(file)->fi_hdir);
12991 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
12992 +}
12993 +
12994 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
12995 +{
12996 +       FiMustWriteLock(file);
12997 +       au_fi(file)->fi_btop = bindex;
12998 +}
12999 +
13000 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
13001 +{
13002 +       FiMustWriteLock(file);
13003 +       AuDebugOn(!au_fi(file)->fi_hdir);
13004 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
13005 +}
13006 +
13007 +static inline void au_set_fvdir_cache(struct file *file,
13008 +                                     struct au_vdir *vdir_cache)
13009 +{
13010 +       FiMustWriteLock(file);
13011 +       AuDebugOn(!au_fi(file)->fi_hdir);
13012 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13013 +}
13014 +
13015 +static inline struct file *au_hf_top(struct file *file)
13016 +{
13017 +       FiMustAnyLock(file);
13018 +       AuDebugOn(au_fi(file)->fi_hdir);
13019 +       return au_fi(file)->fi_htop.hf_file;
13020 +}
13021 +
13022 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13023 +{
13024 +       FiMustAnyLock(file);
13025 +       AuDebugOn(!au_fi(file)->fi_hdir);
13026 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
13027 +}
13028 +
13029 +/* todo: memory barrier? */
13030 +static inline unsigned int au_figen(struct file *f)
13031 +{
13032 +       return atomic_read(&au_fi(f)->fi_generation);
13033 +}
13034 +
13035 +static inline void au_set_mmapped(struct file *f)
13036 +{
13037 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13038 +               return;
13039 +       pr_warn("fi_mmapped wrapped around\n");
13040 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13041 +               ;
13042 +}
13043 +
13044 +static inline void au_unset_mmapped(struct file *f)
13045 +{
13046 +       atomic_dec(&au_fi(f)->fi_mmapped);
13047 +}
13048 +
13049 +static inline int au_test_mmapped(struct file *f)
13050 +{
13051 +       return atomic_read(&au_fi(f)->fi_mmapped);
13052 +}
13053 +
13054 +/* customize vma->vm_file */
13055 +
13056 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13057 +                                      struct file *file)
13058 +{
13059 +       struct file *f;
13060 +
13061 +       f = vma->vm_file;
13062 +       get_file(file);
13063 +       vma->vm_file = file;
13064 +       fput(f);
13065 +}
13066 +
13067 +#ifdef CONFIG_MMU
13068 +#define AuDbgVmRegion(file, vma) do {} while (0)
13069 +
13070 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13071 +                                   struct file *file)
13072 +{
13073 +       au_do_vm_file_reset(vma, file);
13074 +}
13075 +#else
13076 +#define AuDbgVmRegion(file, vma) \
13077 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13078 +
13079 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13080 +                                   struct file *file)
13081 +{
13082 +       struct file *f;
13083 +
13084 +       au_do_vm_file_reset(vma, file);
13085 +       f = vma->vm_region->vm_file;
13086 +       get_file(file);
13087 +       vma->vm_region->vm_file = file;
13088 +       fput(f);
13089 +}
13090 +#endif /* CONFIG_MMU */
13091 +
13092 +/* handle vma->vm_prfile */
13093 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13094 +                                   struct file *file)
13095 +{
13096 +       get_file(file);
13097 +       vma->vm_prfile = file;
13098 +#ifndef CONFIG_MMU
13099 +       get_file(file);
13100 +       vma->vm_region->vm_prfile = file;
13101 +#endif
13102 +}
13103 +
13104 +#endif /* __KERNEL__ */
13105 +#endif /* __AUFS_FILE_H__ */
13106 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13107 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13108 +++ linux/fs/aufs/finfo.c       2016-01-13 20:11:11.669760262 +0100
13109 @@ -0,0 +1,156 @@
13110 +/*
13111 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13112 + *
13113 + * This program, aufs is free software; you can redistribute it and/or modify
13114 + * it under the terms of the GNU General Public License as published by
13115 + * the Free Software Foundation; either version 2 of the License, or
13116 + * (at your option) any later version.
13117 + *
13118 + * This program is distributed in the hope that it will be useful,
13119 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13120 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13121 + * GNU General Public License for more details.
13122 + *
13123 + * You should have received a copy of the GNU General Public License
13124 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13125 + */
13126 +
13127 +/*
13128 + * file private data
13129 + */
13130 +
13131 +#include "aufs.h"
13132 +
13133 +void au_hfput(struct au_hfile *hf, struct file *file)
13134 +{
13135 +       /* todo: direct access f_flags */
13136 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
13137 +               allow_write_access(hf->hf_file);
13138 +       fput(hf->hf_file);
13139 +       hf->hf_file = NULL;
13140 +       atomic_dec(&hf->hf_br->br_count);
13141 +       hf->hf_br = NULL;
13142 +}
13143 +
13144 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13145 +{
13146 +       struct au_finfo *finfo = au_fi(file);
13147 +       struct au_hfile *hf;
13148 +       struct au_fidir *fidir;
13149 +
13150 +       fidir = finfo->fi_hdir;
13151 +       if (!fidir) {
13152 +               AuDebugOn(finfo->fi_btop != bindex);
13153 +               hf = &finfo->fi_htop;
13154 +       } else
13155 +               hf = fidir->fd_hfile + bindex;
13156 +
13157 +       if (hf && hf->hf_file)
13158 +               au_hfput(hf, file);
13159 +       if (val) {
13160 +               FiMustWriteLock(file);
13161 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13162 +               hf->hf_file = val;
13163 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13164 +       }
13165 +}
13166 +
13167 +void au_update_figen(struct file *file)
13168 +{
13169 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13170 +       /* smp_mb(); */ /* atomic_set */
13171 +}
13172 +
13173 +/* ---------------------------------------------------------------------- */
13174 +
13175 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13176 +{
13177 +       struct au_fidir *fidir;
13178 +       int nbr;
13179 +
13180 +       nbr = au_sbend(sb) + 1;
13181 +       if (nbr < 2)
13182 +               nbr = 2; /* initial allocate for 2 branches */
13183 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13184 +       if (fidir) {
13185 +               fidir->fd_bbot = -1;
13186 +               fidir->fd_nent = nbr;
13187 +       }
13188 +
13189 +       return fidir;
13190 +}
13191 +
13192 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13193 +{
13194 +       int err;
13195 +       struct au_fidir *fidir, *p;
13196 +
13197 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13198 +       fidir = finfo->fi_hdir;
13199 +       AuDebugOn(!fidir);
13200 +
13201 +       err = -ENOMEM;
13202 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13203 +                        GFP_NOFS);
13204 +       if (p) {
13205 +               p->fd_nent = nbr;
13206 +               finfo->fi_hdir = p;
13207 +               err = 0;
13208 +       }
13209 +
13210 +       return err;
13211 +}
13212 +
13213 +/* ---------------------------------------------------------------------- */
13214 +
13215 +void au_finfo_fin(struct file *file)
13216 +{
13217 +       struct au_finfo *finfo;
13218 +
13219 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13220 +
13221 +       finfo = au_fi(file);
13222 +       AuDebugOn(finfo->fi_hdir);
13223 +       AuRwDestroy(&finfo->fi_rwsem);
13224 +       au_cache_free_finfo(finfo);
13225 +}
13226 +
13227 +void au_fi_init_once(void *_finfo)
13228 +{
13229 +       struct au_finfo *finfo = _finfo;
13230 +       static struct lock_class_key aufs_fi;
13231 +
13232 +       au_rw_init(&finfo->fi_rwsem);
13233 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
13234 +}
13235 +
13236 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13237 +{
13238 +       int err;
13239 +       struct au_finfo *finfo;
13240 +       struct dentry *dentry;
13241 +
13242 +       err = -ENOMEM;
13243 +       dentry = file->f_path.dentry;
13244 +       finfo = au_cache_alloc_finfo();
13245 +       if (unlikely(!finfo))
13246 +               goto out;
13247 +
13248 +       err = 0;
13249 +       au_nfiles_inc(dentry->d_sb);
13250 +       /* verbose coding for lock class name */
13251 +       if (!fidir)
13252 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
13253 +       else
13254 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
13255 +       au_rw_write_lock(&finfo->fi_rwsem);
13256 +       finfo->fi_btop = -1;
13257 +       finfo->fi_hdir = fidir;
13258 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13259 +       /* smp_mb(); */ /* atomic_set */
13260 +
13261 +       file->private_data = finfo;
13262 +
13263 +out:
13264 +       return err;
13265 +}
13266 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13267 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13268 +++ linux/fs/aufs/f_op.c        2016-01-13 20:11:11.669760262 +0100
13269 @@ -0,0 +1,738 @@
13270 +/*
13271 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13272 + *
13273 + * This program, aufs is free software; you can redistribute it and/or modify
13274 + * it under the terms of the GNU General Public License as published by
13275 + * the Free Software Foundation; either version 2 of the License, or
13276 + * (at your option) any later version.
13277 + *
13278 + * This program is distributed in the hope that it will be useful,
13279 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13280 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13281 + * GNU General Public License for more details.
13282 + *
13283 + * You should have received a copy of the GNU General Public License
13284 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13285 + */
13286 +
13287 +/*
13288 + * file and vm operations
13289 + */
13290 +
13291 +#include <linux/aio.h>
13292 +#include <linux/fs_stack.h>
13293 +#include <linux/mman.h>
13294 +#include <linux/security.h>
13295 +#include "aufs.h"
13296 +
13297 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13298 +{
13299 +       int err;
13300 +       aufs_bindex_t bindex;
13301 +       struct dentry *dentry;
13302 +       struct au_finfo *finfo;
13303 +       struct inode *h_inode;
13304 +
13305 +       FiMustWriteLock(file);
13306 +
13307 +       err = 0;
13308 +       dentry = file->f_path.dentry;
13309 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13310 +       finfo = au_fi(file);
13311 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13312 +       atomic_set(&finfo->fi_mmapped, 0);
13313 +       bindex = au_dbstart(dentry);
13314 +       if (!h_file)
13315 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13316 +       else
13317 +               get_file(h_file);
13318 +       if (IS_ERR(h_file))
13319 +               err = PTR_ERR(h_file);
13320 +       else {
13321 +               if ((flags & __O_TMPFILE)
13322 +                   && !(flags & O_EXCL)) {
13323 +                       h_inode = file_inode(h_file);
13324 +                       spin_lock(&h_inode->i_lock);
13325 +                       h_inode->i_state |= I_LINKABLE;
13326 +                       spin_unlock(&h_inode->i_lock);
13327 +               }
13328 +               au_set_fbstart(file, bindex);
13329 +               au_set_h_fptr(file, bindex, h_file);
13330 +               au_update_figen(file);
13331 +               /* todo: necessary? */
13332 +               /* file->f_ra = h_file->f_ra; */
13333 +       }
13334 +
13335 +       return err;
13336 +}
13337 +
13338 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13339 +                           struct file *file)
13340 +{
13341 +       int err;
13342 +       struct super_block *sb;
13343 +       struct au_do_open_args args = {
13344 +               .open   = au_do_open_nondir
13345 +       };
13346 +
13347 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13348 +             file, vfsub_file_flags(file), file->f_mode);
13349 +
13350 +       sb = file->f_path.dentry->d_sb;
13351 +       si_read_lock(sb, AuLock_FLUSH);
13352 +       err = au_do_open(file, &args);
13353 +       si_read_unlock(sb);
13354 +       return err;
13355 +}
13356 +
13357 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13358 +{
13359 +       struct au_finfo *finfo;
13360 +       aufs_bindex_t bindex;
13361 +
13362 +       finfo = au_fi(file);
13363 +       au_sphl_del(&finfo->fi_hlist,
13364 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13365 +       bindex = finfo->fi_btop;
13366 +       if (bindex >= 0)
13367 +               au_set_h_fptr(file, bindex, NULL);
13368 +
13369 +       au_finfo_fin(file);
13370 +       return 0;
13371 +}
13372 +
13373 +/* ---------------------------------------------------------------------- */
13374 +
13375 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13376 +{
13377 +       int err;
13378 +       struct file *h_file;
13379 +
13380 +       err = 0;
13381 +       h_file = au_hf_top(file);
13382 +       if (h_file)
13383 +               err = vfsub_flush(h_file, id);
13384 +       return err;
13385 +}
13386 +
13387 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13388 +{
13389 +       return au_do_flush(file, id, au_do_flush_nondir);
13390 +}
13391 +
13392 +/* ---------------------------------------------------------------------- */
13393 +/*
13394 + * read and write functions acquire [fdi]_rwsem once, but release before
13395 + * mmap_sem. This is because to stop a race condition between mmap(2).
13396 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13397 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13398 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13399 + */
13400 +
13401 +/* Callers should call au_read_post() or fput() in the end */
13402 +struct file *au_read_pre(struct file *file, int keep_fi)
13403 +{
13404 +       struct file *h_file;
13405 +       int err;
13406 +
13407 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13408 +       if (!err) {
13409 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13410 +               h_file = au_hf_top(file);
13411 +               get_file(h_file);
13412 +               if (!keep_fi)
13413 +                       fi_read_unlock(file);
13414 +       } else
13415 +               h_file = ERR_PTR(err);
13416 +
13417 +       return h_file;
13418 +}
13419 +
13420 +static void au_read_post(struct inode *inode, struct file *h_file)
13421 +{
13422 +       /* update without lock, I don't think it a problem */
13423 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13424 +       fput(h_file);
13425 +}
13426 +
13427 +struct au_write_pre {
13428 +       blkcnt_t blks;
13429 +       aufs_bindex_t bstart;
13430 +};
13431 +
13432 +/*
13433 + * return with iinfo is write-locked
13434 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13435 + * end
13436 + */
13437 +static struct file *au_write_pre(struct file *file, int do_ready,
13438 +                                struct au_write_pre *wpre)
13439 +{
13440 +       struct file *h_file;
13441 +       struct dentry *dentry;
13442 +       int err;
13443 +       struct au_pin pin;
13444 +
13445 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13446 +       h_file = ERR_PTR(err);
13447 +       if (unlikely(err))
13448 +               goto out;
13449 +
13450 +       dentry = file->f_path.dentry;
13451 +       if (do_ready) {
13452 +               err = au_ready_to_write(file, -1, &pin);
13453 +               if (unlikely(err)) {
13454 +                       h_file = ERR_PTR(err);
13455 +                       di_write_unlock(dentry);
13456 +                       goto out_fi;
13457 +               }
13458 +       }
13459 +
13460 +       di_downgrade_lock(dentry, /*flags*/0);
13461 +       if (wpre)
13462 +               wpre->bstart = au_fbstart(file);
13463 +       h_file = au_hf_top(file);
13464 +       get_file(h_file);
13465 +       if (wpre)
13466 +               wpre->blks = file_inode(h_file)->i_blocks;
13467 +       if (do_ready)
13468 +               au_unpin(&pin);
13469 +       di_read_unlock(dentry, /*flags*/0);
13470 +
13471 +out_fi:
13472 +       fi_write_unlock(file);
13473 +out:
13474 +       return h_file;
13475 +}
13476 +
13477 +static void au_write_post(struct inode *inode, struct file *h_file,
13478 +                         struct au_write_pre *wpre, ssize_t written)
13479 +{
13480 +       struct inode *h_inode;
13481 +
13482 +       au_cpup_attr_timesizes(inode);
13483 +       AuDebugOn(au_ibstart(inode) != wpre->bstart);
13484 +       h_inode = file_inode(h_file);
13485 +       inode->i_mode = h_inode->i_mode;
13486 +       ii_write_unlock(inode);
13487 +       fput(h_file);
13488 +
13489 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13490 +       if (written > 0)
13491 +               au_fhsm_wrote(inode->i_sb, wpre->bstart,
13492 +                             /*force*/h_inode->i_blocks > wpre->blks);
13493 +}
13494 +
13495 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13496 +                        loff_t *ppos)
13497 +{
13498 +       ssize_t err;
13499 +       struct inode *inode;
13500 +       struct file *h_file;
13501 +       struct super_block *sb;
13502 +
13503 +       inode = file_inode(file);
13504 +       sb = inode->i_sb;
13505 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13506 +
13507 +       h_file = au_read_pre(file, /*keep_fi*/0);
13508 +       err = PTR_ERR(h_file);
13509 +       if (IS_ERR(h_file))
13510 +               goto out;
13511 +
13512 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13513 +       err = vfsub_read_u(h_file, buf, count, ppos);
13514 +       /* todo: necessary? */
13515 +       /* file->f_ra = h_file->f_ra; */
13516 +       au_read_post(inode, h_file);
13517 +
13518 +out:
13519 +       si_read_unlock(sb);
13520 +       return err;
13521 +}
13522 +
13523 +/*
13524 + * todo: very ugly
13525 + * it locks both of i_mutex and si_rwsem for read in safe.
13526 + * if the plink maintenance mode continues forever (that is the problem),
13527 + * may loop forever.
13528 + */
13529 +static void au_mtx_and_read_lock(struct inode *inode)
13530 +{
13531 +       int err;
13532 +       struct super_block *sb = inode->i_sb;
13533 +
13534 +       while (1) {
13535 +               mutex_lock(&inode->i_mutex);
13536 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13537 +               if (!err)
13538 +                       break;
13539 +               mutex_unlock(&inode->i_mutex);
13540 +               si_read_lock(sb, AuLock_NOPLMW);
13541 +               si_read_unlock(sb);
13542 +       }
13543 +}
13544 +
13545 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13546 +                         size_t count, loff_t *ppos)
13547 +{
13548 +       ssize_t err;
13549 +       struct au_write_pre wpre;
13550 +       struct inode *inode;
13551 +       struct file *h_file;
13552 +       char __user *buf = (char __user *)ubuf;
13553 +
13554 +       inode = file_inode(file);
13555 +       au_mtx_and_read_lock(inode);
13556 +
13557 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13558 +       err = PTR_ERR(h_file);
13559 +       if (IS_ERR(h_file))
13560 +               goto out;
13561 +
13562 +       err = vfsub_write_u(h_file, buf, count, ppos);
13563 +       au_write_post(inode, h_file, &wpre, err);
13564 +
13565 +out:
13566 +       si_read_unlock(inode->i_sb);
13567 +       mutex_unlock(&inode->i_mutex);
13568 +       return err;
13569 +}
13570 +
13571 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13572 +                         struct iov_iter *iov_iter)
13573 +{
13574 +       ssize_t err;
13575 +       struct file *file;
13576 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13577 +
13578 +       err = security_file_permission(h_file, rw);
13579 +       if (unlikely(err))
13580 +               goto out;
13581 +
13582 +       err = -ENOSYS;
13583 +       iter = NULL;
13584 +       if (rw == MAY_READ)
13585 +               iter = h_file->f_op->read_iter;
13586 +       else if (rw == MAY_WRITE)
13587 +               iter = h_file->f_op->write_iter;
13588 +
13589 +       file = kio->ki_filp;
13590 +       kio->ki_filp = h_file;
13591 +       if (iter) {
13592 +               lockdep_off();
13593 +               err = iter(kio, iov_iter);
13594 +               lockdep_on();
13595 +       } else
13596 +               /* currently there is no such fs */
13597 +               WARN_ON_ONCE(1);
13598 +       kio->ki_filp = file;
13599 +
13600 +out:
13601 +       return err;
13602 +}
13603 +
13604 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13605 +{
13606 +       ssize_t err;
13607 +       struct file *file, *h_file;
13608 +       struct inode *inode;
13609 +       struct super_block *sb;
13610 +
13611 +       file = kio->ki_filp;
13612 +       inode = file_inode(file);
13613 +       sb = inode->i_sb;
13614 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13615 +
13616 +       h_file = au_read_pre(file, /*keep_fi*/0);
13617 +       err = PTR_ERR(h_file);
13618 +       if (IS_ERR(h_file))
13619 +               goto out;
13620 +
13621 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13622 +       /* todo: necessary? */
13623 +       /* file->f_ra = h_file->f_ra; */
13624 +       au_read_post(inode, h_file);
13625 +
13626 +out:
13627 +       si_read_unlock(sb);
13628 +       return err;
13629 +}
13630 +
13631 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13632 +{
13633 +       ssize_t err;
13634 +       struct au_write_pre wpre;
13635 +       struct inode *inode;
13636 +       struct file *file, *h_file;
13637 +
13638 +       file = kio->ki_filp;
13639 +       inode = file_inode(file);
13640 +       au_mtx_and_read_lock(inode);
13641 +
13642 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13643 +       err = PTR_ERR(h_file);
13644 +       if (IS_ERR(h_file))
13645 +               goto out;
13646 +
13647 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13648 +       au_write_post(inode, h_file, &wpre, err);
13649 +
13650 +out:
13651 +       si_read_unlock(inode->i_sb);
13652 +       mutex_unlock(&inode->i_mutex);
13653 +       return err;
13654 +}
13655 +
13656 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13657 +                               struct pipe_inode_info *pipe, size_t len,
13658 +                               unsigned int flags)
13659 +{
13660 +       ssize_t err;
13661 +       struct file *h_file;
13662 +       struct inode *inode;
13663 +       struct super_block *sb;
13664 +
13665 +       inode = file_inode(file);
13666 +       sb = inode->i_sb;
13667 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13668 +
13669 +       h_file = au_read_pre(file, /*keep_fi*/1);
13670 +       err = PTR_ERR(h_file);
13671 +       if (IS_ERR(h_file))
13672 +               goto out;
13673 +
13674 +       if (au_test_loopback_kthread()) {
13675 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13676 +               if (file->f_mapping != h_file->f_mapping) {
13677 +                       file->f_mapping = h_file->f_mapping;
13678 +                       smp_mb(); /* unnecessary? */
13679 +               }
13680 +       }
13681 +       fi_read_unlock(file);
13682 +
13683 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13684 +       /* todo: necessasry? */
13685 +       /* file->f_ra = h_file->f_ra; */
13686 +       au_read_post(inode, h_file);
13687 +
13688 +out:
13689 +       si_read_unlock(sb);
13690 +       return err;
13691 +}
13692 +
13693 +static ssize_t
13694 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13695 +                 size_t len, unsigned int flags)
13696 +{
13697 +       ssize_t err;
13698 +       struct au_write_pre wpre;
13699 +       struct inode *inode;
13700 +       struct file *h_file;
13701 +
13702 +       inode = file_inode(file);
13703 +       au_mtx_and_read_lock(inode);
13704 +
13705 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13706 +       err = PTR_ERR(h_file);
13707 +       if (IS_ERR(h_file))
13708 +               goto out;
13709 +
13710 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13711 +       au_write_post(inode, h_file, &wpre, err);
13712 +
13713 +out:
13714 +       si_read_unlock(inode->i_sb);
13715 +       mutex_unlock(&inode->i_mutex);
13716 +       return err;
13717 +}
13718 +
13719 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13720 +                          loff_t len)
13721 +{
13722 +       long err;
13723 +       struct au_write_pre wpre;
13724 +       struct inode *inode;
13725 +       struct file *h_file;
13726 +
13727 +       inode = file_inode(file);
13728 +       au_mtx_and_read_lock(inode);
13729 +
13730 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13731 +       err = PTR_ERR(h_file);
13732 +       if (IS_ERR(h_file))
13733 +               goto out;
13734 +
13735 +       lockdep_off();
13736 +       err = vfs_fallocate(h_file, mode, offset, len);
13737 +       lockdep_on();
13738 +       au_write_post(inode, h_file, &wpre, /*written*/1);
13739 +
13740 +out:
13741 +       si_read_unlock(inode->i_sb);
13742 +       mutex_unlock(&inode->i_mutex);
13743 +       return err;
13744 +}
13745 +
13746 +/* ---------------------------------------------------------------------- */
13747 +
13748 +/*
13749 + * The locking order around current->mmap_sem.
13750 + * - in most and regular cases
13751 + *   file I/O syscall -- aufs_read() or something
13752 + *     -- si_rwsem for read -- mmap_sem
13753 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
13754 + * - in mmap case
13755 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13756 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
13757 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
13758 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13759 + * It means that when aufs acquires si_rwsem for write, the process should never
13760 + * acquire mmap_sem.
13761 + *
13762 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13763 + * problem either since any directory is not able to be mmap-ed.
13764 + * The similar scenario is applied to aufs_readlink() too.
13765 + */
13766 +
13767 +#if 0 /* stop calling security_file_mmap() */
13768 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13769 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
13770 +
13771 +static unsigned long au_arch_prot_conv(unsigned long flags)
13772 +{
13773 +       /* currently ppc64 only */
13774 +#ifdef CONFIG_PPC64
13775 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
13776 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13777 +       return AuConv_VM_PROT(flags, SAO);
13778 +#else
13779 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
13780 +       return 0;
13781 +#endif
13782 +}
13783 +
13784 +static unsigned long au_prot_conv(unsigned long flags)
13785 +{
13786 +       return AuConv_VM_PROT(flags, READ)
13787 +               | AuConv_VM_PROT(flags, WRITE)
13788 +               | AuConv_VM_PROT(flags, EXEC)
13789 +               | au_arch_prot_conv(flags);
13790 +}
13791 +
13792 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13793 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
13794 +
13795 +static unsigned long au_flag_conv(unsigned long flags)
13796 +{
13797 +       return AuConv_VM_MAP(flags, GROWSDOWN)
13798 +               | AuConv_VM_MAP(flags, DENYWRITE)
13799 +               | AuConv_VM_MAP(flags, LOCKED);
13800 +}
13801 +#endif
13802 +
13803 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13804 +{
13805 +       int err;
13806 +       const unsigned char wlock
13807 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13808 +       struct super_block *sb;
13809 +       struct file *h_file;
13810 +       struct inode *inode;
13811 +
13812 +       AuDbgVmRegion(file, vma);
13813 +
13814 +       inode = file_inode(file);
13815 +       sb = inode->i_sb;
13816 +       lockdep_off();
13817 +       si_read_lock(sb, AuLock_NOPLMW);
13818 +
13819 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
13820 +       lockdep_on();
13821 +       err = PTR_ERR(h_file);
13822 +       if (IS_ERR(h_file))
13823 +               goto out;
13824 +
13825 +       err = 0;
13826 +       au_set_mmapped(file);
13827 +       au_vm_file_reset(vma, h_file);
13828 +       /*
13829 +        * we cannot call security_mmap_file() here since it may acquire
13830 +        * mmap_sem or i_mutex.
13831 +        *
13832 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13833 +        *                       au_flag_conv(vma->vm_flags));
13834 +        */
13835 +       if (!err)
13836 +               err = h_file->f_op->mmap(h_file, vma);
13837 +       if (!err) {
13838 +               au_vm_prfile_set(vma, file);
13839 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
13840 +               goto out_fput; /* success */
13841 +       }
13842 +       au_unset_mmapped(file);
13843 +       au_vm_file_reset(vma, file);
13844 +
13845 +out_fput:
13846 +       lockdep_off();
13847 +       ii_write_unlock(inode);
13848 +       lockdep_on();
13849 +       fput(h_file);
13850 +out:
13851 +       lockdep_off();
13852 +       si_read_unlock(sb);
13853 +       lockdep_on();
13854 +       AuTraceErr(err);
13855 +       return err;
13856 +}
13857 +
13858 +/* ---------------------------------------------------------------------- */
13859 +
13860 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
13861 +                            int datasync)
13862 +{
13863 +       int err;
13864 +       struct au_write_pre wpre;
13865 +       struct inode *inode;
13866 +       struct file *h_file;
13867 +
13868 +       err = 0; /* -EBADF; */ /* posix? */
13869 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13870 +               goto out;
13871 +
13872 +       inode = file_inode(file);
13873 +       au_mtx_and_read_lock(inode);
13874 +
13875 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13876 +       err = PTR_ERR(h_file);
13877 +       if (IS_ERR(h_file))
13878 +               goto out_unlock;
13879 +
13880 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
13881 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13882 +
13883 +out_unlock:
13884 +       si_read_unlock(inode->i_sb);
13885 +       mutex_unlock(&inode->i_mutex);
13886 +out:
13887 +       return err;
13888 +}
13889 +
13890 +/* no one supports this operation, currently */
13891 +#if 0
13892 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
13893 +{
13894 +       int err;
13895 +       struct au_write_pre wpre;
13896 +       struct inode *inode;
13897 +       struct file *file, *h_file;
13898 +
13899 +       err = 0; /* -EBADF; */ /* posix? */
13900 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13901 +               goto out;
13902 +
13903 +       file = kio->ki_filp;
13904 +       inode = file_inode(file);
13905 +       au_mtx_and_read_lock(inode);
13906 +
13907 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13908 +       err = PTR_ERR(h_file);
13909 +       if (IS_ERR(h_file))
13910 +               goto out_unlock;
13911 +
13912 +       err = -ENOSYS;
13913 +       h_file = au_hf_top(file);
13914 +       if (h_file->f_op->aio_fsync) {
13915 +               struct mutex *h_mtx;
13916 +
13917 +               h_mtx = &file_inode(h_file)->i_mutex;
13918 +               if (!is_sync_kiocb(kio)) {
13919 +                       get_file(h_file);
13920 +                       fput(file);
13921 +               }
13922 +               kio->ki_filp = h_file;
13923 +               err = h_file->f_op->aio_fsync(kio, datasync);
13924 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13925 +               if (!err)
13926 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
13927 +               /*ignore*/
13928 +               mutex_unlock(h_mtx);
13929 +       }
13930 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13931 +
13932 +out_unlock:
13933 +       si_read_unlock(inode->sb);
13934 +       mutex_unlock(&inode->i_mutex);
13935 +out:
13936 +       return err;
13937 +}
13938 +#endif
13939 +
13940 +static int aufs_fasync(int fd, struct file *file, int flag)
13941 +{
13942 +       int err;
13943 +       struct file *h_file;
13944 +       struct super_block *sb;
13945 +
13946 +       sb = file->f_path.dentry->d_sb;
13947 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13948 +
13949 +       h_file = au_read_pre(file, /*keep_fi*/0);
13950 +       err = PTR_ERR(h_file);
13951 +       if (IS_ERR(h_file))
13952 +               goto out;
13953 +
13954 +       if (h_file->f_op->fasync)
13955 +               err = h_file->f_op->fasync(fd, h_file, flag);
13956 +       fput(h_file); /* instead of au_read_post() */
13957 +
13958 +out:
13959 +       si_read_unlock(sb);
13960 +       return err;
13961 +}
13962 +
13963 +/* ---------------------------------------------------------------------- */
13964 +
13965 +/* no one supports this operation, currently */
13966 +#if 0
13967 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
13968 +                            size_t len, loff_t *pos, int more)
13969 +{
13970 +}
13971 +#endif
13972 +
13973 +/* ---------------------------------------------------------------------- */
13974 +
13975 +const struct file_operations aufs_file_fop = {
13976 +       .owner          = THIS_MODULE,
13977 +
13978 +       .llseek         = default_llseek,
13979 +
13980 +       .read           = aufs_read,
13981 +       .write          = aufs_write,
13982 +       .read_iter      = aufs_read_iter,
13983 +       .write_iter     = aufs_write_iter,
13984 +
13985 +#ifdef CONFIG_AUFS_POLL
13986 +       .poll           = aufs_poll,
13987 +#endif
13988 +       .unlocked_ioctl = aufs_ioctl_nondir,
13989 +#ifdef CONFIG_COMPAT
13990 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
13991 +#endif
13992 +       .mmap           = aufs_mmap,
13993 +       .open           = aufs_open_nondir,
13994 +       .flush          = aufs_flush_nondir,
13995 +       .release        = aufs_release_nondir,
13996 +       .fsync          = aufs_fsync_nondir,
13997 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
13998 +       .fasync         = aufs_fasync,
13999 +       /* .sendpage    = aufs_sendpage, */
14000 +       .splice_write   = aufs_splice_write,
14001 +       .splice_read    = aufs_splice_read,
14002 +#if 0
14003 +       .aio_splice_write = aufs_aio_splice_write,
14004 +       .aio_splice_read  = aufs_aio_splice_read,
14005 +#endif
14006 +       .fallocate      = aufs_fallocate
14007 +};
14008 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14009 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
14010 +++ linux/fs/aufs/fstype.h      2016-01-13 20:11:11.669760262 +0100
14011 @@ -0,0 +1,400 @@
14012 +/*
14013 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14014 + *
14015 + * This program, aufs is free software; you can redistribute it and/or modify
14016 + * it under the terms of the GNU General Public License as published by
14017 + * the Free Software Foundation; either version 2 of the License, or
14018 + * (at your option) any later version.
14019 + *
14020 + * This program is distributed in the hope that it will be useful,
14021 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14022 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14023 + * GNU General Public License for more details.
14024 + *
14025 + * You should have received a copy of the GNU General Public License
14026 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14027 + */
14028 +
14029 +/*
14030 + * judging filesystem type
14031 + */
14032 +
14033 +#ifndef __AUFS_FSTYPE_H__
14034 +#define __AUFS_FSTYPE_H__
14035 +
14036 +#ifdef __KERNEL__
14037 +
14038 +#include <linux/fs.h>
14039 +#include <linux/magic.h>
14040 +#include <linux/nfs_fs.h>
14041 +#include <linux/romfs_fs.h>
14042 +
14043 +static inline int au_test_aufs(struct super_block *sb)
14044 +{
14045 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14046 +}
14047 +
14048 +static inline const char *au_sbtype(struct super_block *sb)
14049 +{
14050 +       return sb->s_type->name;
14051 +}
14052 +
14053 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14054 +{
14055 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
14056 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14057 +#else
14058 +       return 0;
14059 +#endif
14060 +}
14061 +
14062 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14063 +{
14064 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
14065 +       return sb->s_magic == ROMFS_MAGIC;
14066 +#else
14067 +       return 0;
14068 +#endif
14069 +}
14070 +
14071 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14072 +{
14073 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
14074 +       return sb->s_magic == CRAMFS_MAGIC;
14075 +#endif
14076 +       return 0;
14077 +}
14078 +
14079 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14080 +{
14081 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
14082 +       return sb->s_magic == NFS_SUPER_MAGIC;
14083 +#else
14084 +       return 0;
14085 +#endif
14086 +}
14087 +
14088 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14089 +{
14090 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
14091 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14092 +#else
14093 +       return 0;
14094 +#endif
14095 +}
14096 +
14097 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14098 +{
14099 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
14100 +       return sb->s_magic == XFS_SB_MAGIC;
14101 +#else
14102 +       return 0;
14103 +#endif
14104 +}
14105 +
14106 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14107 +{
14108 +#ifdef CONFIG_TMPFS
14109 +       return sb->s_magic == TMPFS_MAGIC;
14110 +#else
14111 +       return 0;
14112 +#endif
14113 +}
14114 +
14115 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14116 +{
14117 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
14118 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14119 +#else
14120 +       return 0;
14121 +#endif
14122 +}
14123 +
14124 +static inline int au_test_ramfs(struct super_block *sb)
14125 +{
14126 +       return sb->s_magic == RAMFS_MAGIC;
14127 +}
14128 +
14129 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14130 +{
14131 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
14132 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14133 +#else
14134 +       return 0;
14135 +#endif
14136 +}
14137 +
14138 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14139 +{
14140 +#ifdef CONFIG_PROC_FS
14141 +       return sb->s_magic == PROC_SUPER_MAGIC;
14142 +#else
14143 +       return 0;
14144 +#endif
14145 +}
14146 +
14147 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14148 +{
14149 +#ifdef CONFIG_SYSFS
14150 +       return sb->s_magic == SYSFS_MAGIC;
14151 +#else
14152 +       return 0;
14153 +#endif
14154 +}
14155 +
14156 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14157 +{
14158 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
14159 +       return sb->s_magic == CONFIGFS_MAGIC;
14160 +#else
14161 +       return 0;
14162 +#endif
14163 +}
14164 +
14165 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14166 +{
14167 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
14168 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14169 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14170 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14171 +               || sb->s_magic == MINIX_SUPER_MAGIC
14172 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14173 +#else
14174 +       return 0;
14175 +#endif
14176 +}
14177 +
14178 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14179 +{
14180 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
14181 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14182 +#else
14183 +       return 0;
14184 +#endif
14185 +}
14186 +
14187 +static inline int au_test_msdos(struct super_block *sb)
14188 +{
14189 +       return au_test_fat(sb);
14190 +}
14191 +
14192 +static inline int au_test_vfat(struct super_block *sb)
14193 +{
14194 +       return au_test_fat(sb);
14195 +}
14196 +
14197 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14198 +{
14199 +#ifdef CONFIG_SECURITYFS
14200 +       return sb->s_magic == SECURITYFS_MAGIC;
14201 +#else
14202 +       return 0;
14203 +#endif
14204 +}
14205 +
14206 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14207 +{
14208 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
14209 +       return sb->s_magic == SQUASHFS_MAGIC;
14210 +#else
14211 +       return 0;
14212 +#endif
14213 +}
14214 +
14215 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14216 +{
14217 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
14218 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14219 +#else
14220 +       return 0;
14221 +#endif
14222 +}
14223 +
14224 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14225 +{
14226 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
14227 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14228 +#else
14229 +       return 0;
14230 +#endif
14231 +}
14232 +
14233 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14234 +{
14235 +#ifdef CONFIG_DEBUG_FS
14236 +       return sb->s_magic == DEBUGFS_MAGIC;
14237 +#else
14238 +       return 0;
14239 +#endif
14240 +}
14241 +
14242 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14243 +{
14244 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
14245 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14246 +#else
14247 +       return 0;
14248 +#endif
14249 +}
14250 +
14251 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14252 +{
14253 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
14254 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14255 +#else
14256 +       return 0;
14257 +#endif
14258 +}
14259 +
14260 +/* ---------------------------------------------------------------------- */
14261 +/*
14262 + * they can't be an aufs branch.
14263 + */
14264 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14265 +{
14266 +       return
14267 +#ifndef CONFIG_AUFS_BR_RAMFS
14268 +               au_test_ramfs(sb) ||
14269 +#endif
14270 +               au_test_procfs(sb)
14271 +               || au_test_sysfs(sb)
14272 +               || au_test_configfs(sb)
14273 +               || au_test_debugfs(sb)
14274 +               || au_test_securityfs(sb)
14275 +               || au_test_xenfs(sb)
14276 +               || au_test_ecryptfs(sb)
14277 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14278 +               || au_test_aufs(sb); /* will be supported in next version */
14279 +}
14280 +
14281 +static inline int au_test_fs_remote(struct super_block *sb)
14282 +{
14283 +       return !au_test_tmpfs(sb)
14284 +#ifdef CONFIG_AUFS_BR_RAMFS
14285 +               && !au_test_ramfs(sb)
14286 +#endif
14287 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14288 +}
14289 +
14290 +/* ---------------------------------------------------------------------- */
14291 +
14292 +/*
14293 + * Note: these functions (below) are created after reading ->getattr() in all
14294 + * filesystems under linux/fs. it means we have to do so in every update...
14295 + */
14296 +
14297 +/*
14298 + * some filesystems require getattr to refresh the inode attributes before
14299 + * referencing.
14300 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14301 + * and leave the work for d_revalidate()
14302 + */
14303 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14304 +{
14305 +       return au_test_nfs(sb)
14306 +               || au_test_fuse(sb)
14307 +               /* || au_test_btrfs(sb) */      /* untested */
14308 +               ;
14309 +}
14310 +
14311 +/*
14312 + * filesystems which don't maintain i_size or i_blocks.
14313 + */
14314 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14315 +{
14316 +       return au_test_xfs(sb)
14317 +               || au_test_btrfs(sb)
14318 +               || au_test_ubifs(sb)
14319 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14320 +               /* || au_test_minix(sb) */      /* untested */
14321 +               ;
14322 +}
14323 +
14324 +/*
14325 + * filesystems which don't store the correct value in some of their inode
14326 + * attributes.
14327 + */
14328 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14329 +{
14330 +       return au_test_fs_bad_iattr_size(sb)
14331 +               || au_test_fat(sb)
14332 +               || au_test_msdos(sb)
14333 +               || au_test_vfat(sb);
14334 +}
14335 +
14336 +/* they don't check i_nlink in link(2) */
14337 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14338 +{
14339 +       return au_test_tmpfs(sb)
14340 +#ifdef CONFIG_AUFS_BR_RAMFS
14341 +               || au_test_ramfs(sb)
14342 +#endif
14343 +               || au_test_ubifs(sb)
14344 +               || au_test_hfsplus(sb);
14345 +}
14346 +
14347 +/*
14348 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14349 + */
14350 +static inline int au_test_fs_notime(struct super_block *sb)
14351 +{
14352 +       return au_test_nfs(sb)
14353 +               || au_test_fuse(sb)
14354 +               || au_test_ubifs(sb)
14355 +               ;
14356 +}
14357 +
14358 +/* temporary support for i#1 in cramfs */
14359 +static inline int au_test_fs_unique_ino(struct inode *inode)
14360 +{
14361 +       if (au_test_cramfs(inode->i_sb))
14362 +               return inode->i_ino != 1;
14363 +       return 1;
14364 +}
14365 +
14366 +/* ---------------------------------------------------------------------- */
14367 +
14368 +/*
14369 + * the filesystem where the xino files placed must support i/o after unlink and
14370 + * maintain i_size and i_blocks.
14371 + */
14372 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14373 +{
14374 +       return au_test_fs_remote(sb)
14375 +               || au_test_fs_bad_iattr_size(sb)
14376 +               /* don't want unnecessary work for xino */
14377 +               || au_test_aufs(sb)
14378 +               || au_test_ecryptfs(sb)
14379 +               || au_test_nilfs(sb);
14380 +}
14381 +
14382 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14383 +{
14384 +       return au_test_tmpfs(sb)
14385 +               || au_test_ramfs(sb);
14386 +}
14387 +
14388 +/*
14389 + * test if the @sb is real-readonly.
14390 + */
14391 +static inline int au_test_fs_rr(struct super_block *sb)
14392 +{
14393 +       return au_test_squashfs(sb)
14394 +               || au_test_iso9660(sb)
14395 +               || au_test_cramfs(sb)
14396 +               || au_test_romfs(sb);
14397 +}
14398 +
14399 +/*
14400 + * test if the @inode is nfs with 'noacl' option
14401 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14402 + */
14403 +static inline int au_test_nfs_noacl(struct inode *inode)
14404 +{
14405 +       return au_test_nfs(inode->i_sb)
14406 +               /* && IS_POSIXACL(inode) */
14407 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14408 +}
14409 +
14410 +#endif /* __KERNEL__ */
14411 +#endif /* __AUFS_FSTYPE_H__ */
14412 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14413 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14414 +++ linux/fs/aufs/hfsnotify.c   2016-01-13 20:11:11.669760262 +0100
14415 @@ -0,0 +1,288 @@
14416 +/*
14417 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14418 + *
14419 + * This program, aufs is free software; you can redistribute it and/or modify
14420 + * it under the terms of the GNU General Public License as published by
14421 + * the Free Software Foundation; either version 2 of the License, or
14422 + * (at your option) any later version.
14423 + *
14424 + * This program is distributed in the hope that it will be useful,
14425 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14426 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14427 + * GNU General Public License for more details.
14428 + *
14429 + * You should have received a copy of the GNU General Public License
14430 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14431 + */
14432 +
14433 +/*
14434 + * fsnotify for the lower directories
14435 + */
14436 +
14437 +#include "aufs.h"
14438 +
14439 +/* FS_IN_IGNORED is unnecessary */
14440 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14441 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14442 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14443 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14444 +
14445 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14446 +{
14447 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14448 +                                            hn_mark);
14449 +       AuDbg("here\n");
14450 +       au_cache_free_hnotify(hn);
14451 +       smp_mb__before_atomic();
14452 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14453 +               wake_up(&au_hfsn_wq);
14454 +}
14455 +
14456 +static int au_hfsn_alloc(struct au_hinode *hinode)
14457 +{
14458 +       int err;
14459 +       struct au_hnotify *hn;
14460 +       struct super_block *sb;
14461 +       struct au_branch *br;
14462 +       struct fsnotify_mark *mark;
14463 +       aufs_bindex_t bindex;
14464 +
14465 +       hn = hinode->hi_notify;
14466 +       sb = hn->hn_aufs_inode->i_sb;
14467 +       bindex = au_br_index(sb, hinode->hi_id);
14468 +       br = au_sbr(sb, bindex);
14469 +       AuDebugOn(!br->br_hfsn);
14470 +
14471 +       mark = &hn->hn_mark;
14472 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14473 +       mark->mask = AuHfsnMask;
14474 +       /*
14475 +        * by udba rename or rmdir, aufs assign a new inode to the known
14476 +        * h_inode, so specify 1 to allow dups.
14477 +        */
14478 +       lockdep_off();
14479 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14480 +                                /*mnt*/NULL, /*allow_dups*/1);
14481 +       /* even if err */
14482 +       fsnotify_put_mark(mark);
14483 +       lockdep_on();
14484 +
14485 +       return err;
14486 +}
14487 +
14488 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14489 +{
14490 +       struct fsnotify_mark *mark;
14491 +       unsigned long long ull;
14492 +       struct fsnotify_group *group;
14493 +
14494 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14495 +       BUG_ON(!ull);
14496 +
14497 +       mark = &hn->hn_mark;
14498 +       spin_lock(&mark->lock);
14499 +       group = mark->group;
14500 +       fsnotify_get_group(group);
14501 +       spin_unlock(&mark->lock);
14502 +       lockdep_off();
14503 +       fsnotify_destroy_mark(mark, group);
14504 +       fsnotify_put_group(group);
14505 +       lockdep_on();
14506 +
14507 +       /* free hn by myself */
14508 +       return 0;
14509 +}
14510 +
14511 +/* ---------------------------------------------------------------------- */
14512 +
14513 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14514 +{
14515 +       struct fsnotify_mark *mark;
14516 +
14517 +       mark = &hinode->hi_notify->hn_mark;
14518 +       spin_lock(&mark->lock);
14519 +       if (do_set) {
14520 +               AuDebugOn(mark->mask & AuHfsnMask);
14521 +               mark->mask |= AuHfsnMask;
14522 +       } else {
14523 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14524 +               mark->mask &= ~AuHfsnMask;
14525 +       }
14526 +       spin_unlock(&mark->lock);
14527 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14528 +}
14529 +
14530 +/* ---------------------------------------------------------------------- */
14531 +
14532 +/* #define AuDbgHnotify */
14533 +#ifdef AuDbgHnotify
14534 +static char *au_hfsn_name(u32 mask)
14535 +{
14536 +#ifdef CONFIG_AUFS_DEBUG
14537 +#define test_ret(flag)                         \
14538 +       do {                                    \
14539 +               if (mask & flag)                \
14540 +                       return #flag;           \
14541 +       } while (0)
14542 +       test_ret(FS_ACCESS);
14543 +       test_ret(FS_MODIFY);
14544 +       test_ret(FS_ATTRIB);
14545 +       test_ret(FS_CLOSE_WRITE);
14546 +       test_ret(FS_CLOSE_NOWRITE);
14547 +       test_ret(FS_OPEN);
14548 +       test_ret(FS_MOVED_FROM);
14549 +       test_ret(FS_MOVED_TO);
14550 +       test_ret(FS_CREATE);
14551 +       test_ret(FS_DELETE);
14552 +       test_ret(FS_DELETE_SELF);
14553 +       test_ret(FS_MOVE_SELF);
14554 +       test_ret(FS_UNMOUNT);
14555 +       test_ret(FS_Q_OVERFLOW);
14556 +       test_ret(FS_IN_IGNORED);
14557 +       test_ret(FS_ISDIR);
14558 +       test_ret(FS_IN_ONESHOT);
14559 +       test_ret(FS_EVENT_ON_CHILD);
14560 +       return "";
14561 +#undef test_ret
14562 +#else
14563 +       return "??";
14564 +#endif
14565 +}
14566 +#endif
14567 +
14568 +/* ---------------------------------------------------------------------- */
14569 +
14570 +static void au_hfsn_free_group(struct fsnotify_group *group)
14571 +{
14572 +       struct au_br_hfsnotify *hfsn = group->private;
14573 +
14574 +       AuDbg("here\n");
14575 +       kfree(hfsn);
14576 +}
14577 +
14578 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14579 +                               struct inode *inode,
14580 +                               struct fsnotify_mark *inode_mark,
14581 +                               struct fsnotify_mark *vfsmount_mark,
14582 +                               u32 mask, void *data, int data_type,
14583 +                               const unsigned char *file_name, u32 cookie)
14584 +{
14585 +       int err;
14586 +       struct au_hnotify *hnotify;
14587 +       struct inode *h_dir, *h_inode;
14588 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14589 +
14590 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14591 +
14592 +       err = 0;
14593 +       /* if FS_UNMOUNT happens, there must be another bug */
14594 +       AuDebugOn(mask & FS_UNMOUNT);
14595 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14596 +               goto out;
14597 +
14598 +       h_dir = inode;
14599 +       h_inode = NULL;
14600 +#ifdef AuDbgHnotify
14601 +       au_debug_on();
14602 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14603 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14604 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14605 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14606 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14607 +               /* WARN_ON(1); */
14608 +       }
14609 +       au_debug_off();
14610 +#endif
14611 +
14612 +       AuDebugOn(!inode_mark);
14613 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14614 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14615 +
14616 +out:
14617 +       return err;
14618 +}
14619 +
14620 +static struct fsnotify_ops au_hfsn_ops = {
14621 +       .handle_event           = au_hfsn_handle_event,
14622 +       .free_group_priv        = au_hfsn_free_group
14623 +};
14624 +
14625 +/* ---------------------------------------------------------------------- */
14626 +
14627 +static void au_hfsn_fin_br(struct au_branch *br)
14628 +{
14629 +       struct au_br_hfsnotify *hfsn;
14630 +
14631 +       hfsn = br->br_hfsn;
14632 +       if (hfsn) {
14633 +               lockdep_off();
14634 +               fsnotify_put_group(hfsn->hfsn_group);
14635 +               lockdep_on();
14636 +       }
14637 +}
14638 +
14639 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14640 +{
14641 +       int err;
14642 +       struct fsnotify_group *group;
14643 +       struct au_br_hfsnotify *hfsn;
14644 +
14645 +       err = 0;
14646 +       br->br_hfsn = NULL;
14647 +       if (!au_br_hnotifyable(perm))
14648 +               goto out;
14649 +
14650 +       err = -ENOMEM;
14651 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14652 +       if (unlikely(!hfsn))
14653 +               goto out;
14654 +
14655 +       err = 0;
14656 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14657 +       if (IS_ERR(group)) {
14658 +               err = PTR_ERR(group);
14659 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14660 +               goto out_hfsn;
14661 +       }
14662 +
14663 +       group->private = hfsn;
14664 +       hfsn->hfsn_group = group;
14665 +       br->br_hfsn = hfsn;
14666 +       goto out; /* success */
14667 +
14668 +out_hfsn:
14669 +       kfree(hfsn);
14670 +out:
14671 +       return err;
14672 +}
14673 +
14674 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14675 +{
14676 +       int err;
14677 +
14678 +       err = 0;
14679 +       if (!br->br_hfsn)
14680 +               err = au_hfsn_init_br(br, perm);
14681 +
14682 +       return err;
14683 +}
14684 +
14685 +/* ---------------------------------------------------------------------- */
14686 +
14687 +static void au_hfsn_fin(void)
14688 +{
14689 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14690 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14691 +}
14692 +
14693 +const struct au_hnotify_op au_hnotify_op = {
14694 +       .ctl            = au_hfsn_ctl,
14695 +       .alloc          = au_hfsn_alloc,
14696 +       .free           = au_hfsn_free,
14697 +
14698 +       .fin            = au_hfsn_fin,
14699 +
14700 +       .reset_br       = au_hfsn_reset_br,
14701 +       .fin_br         = au_hfsn_fin_br,
14702 +       .init_br        = au_hfsn_init_br
14703 +};
14704 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14705 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
14706 +++ linux/fs/aufs/hfsplus.c     2016-01-13 20:11:11.669760262 +0100
14707 @@ -0,0 +1,56 @@
14708 +/*
14709 + * Copyright (C) 2010-2015 Junjiro R. Okajima
14710 + *
14711 + * This program, aufs is free software; you can redistribute it and/or modify
14712 + * it under the terms of the GNU General Public License as published by
14713 + * the Free Software Foundation; either version 2 of the License, or
14714 + * (at your option) any later version.
14715 + *
14716 + * This program is distributed in the hope that it will be useful,
14717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14719 + * GNU General Public License for more details.
14720 + *
14721 + * You should have received a copy of the GNU General Public License
14722 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14723 + */
14724 +
14725 +/*
14726 + * special support for filesystems which aqucires an inode mutex
14727 + * at final closing a file, eg, hfsplus.
14728 + *
14729 + * This trick is very simple and stupid, just to open the file before really
14730 + * neceeary open to tell hfsplus that this is not the final closing.
14731 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
14732 + * and au_h_open_post() after releasing it.
14733 + */
14734 +
14735 +#include "aufs.h"
14736 +
14737 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14738 +                          int force_wr)
14739 +{
14740 +       struct file *h_file;
14741 +       struct dentry *h_dentry;
14742 +
14743 +       h_dentry = au_h_dptr(dentry, bindex);
14744 +       AuDebugOn(!h_dentry);
14745 +       AuDebugOn(d_is_negative(h_dentry));
14746 +
14747 +       h_file = NULL;
14748 +       if (au_test_hfsplus(h_dentry->d_sb)
14749 +           && d_is_reg(h_dentry))
14750 +               h_file = au_h_open(dentry, bindex,
14751 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
14752 +                                  /*file*/NULL, force_wr);
14753 +       return h_file;
14754 +}
14755 +
14756 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14757 +                   struct file *h_file)
14758 +{
14759 +       if (h_file) {
14760 +               fput(h_file);
14761 +               au_sbr_put(dentry->d_sb, bindex);
14762 +       }
14763 +}
14764 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
14765 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
14766 +++ linux/fs/aufs/hnotify.c     2016-01-13 20:11:11.669760262 +0100
14767 @@ -0,0 +1,710 @@
14768 +/*
14769 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14770 + *
14771 + * This program, aufs is free software; you can redistribute it and/or modify
14772 + * it under the terms of the GNU General Public License as published by
14773 + * the Free Software Foundation; either version 2 of the License, or
14774 + * (at your option) any later version.
14775 + *
14776 + * This program is distributed in the hope that it will be useful,
14777 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14778 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14779 + * GNU General Public License for more details.
14780 + *
14781 + * You should have received a copy of the GNU General Public License
14782 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14783 + */
14784 +
14785 +/*
14786 + * abstraction to notify the direct changes on lower directories
14787 + */
14788 +
14789 +#include "aufs.h"
14790 +
14791 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
14792 +{
14793 +       int err;
14794 +       struct au_hnotify *hn;
14795 +
14796 +       err = -ENOMEM;
14797 +       hn = au_cache_alloc_hnotify();
14798 +       if (hn) {
14799 +               hn->hn_aufs_inode = inode;
14800 +               hinode->hi_notify = hn;
14801 +               err = au_hnotify_op.alloc(hinode);
14802 +               AuTraceErr(err);
14803 +               if (unlikely(err)) {
14804 +                       hinode->hi_notify = NULL;
14805 +                       au_cache_free_hnotify(hn);
14806 +                       /*
14807 +                        * The upper dir was removed by udba, but the same named
14808 +                        * dir left. In this case, aufs assignes a new inode
14809 +                        * number and set the monitor again.
14810 +                        * For the lower dir, the old monitnor is still left.
14811 +                        */
14812 +                       if (err == -EEXIST)
14813 +                               err = 0;
14814 +               }
14815 +       }
14816 +
14817 +       AuTraceErr(err);
14818 +       return err;
14819 +}
14820 +
14821 +void au_hn_free(struct au_hinode *hinode)
14822 +{
14823 +       struct au_hnotify *hn;
14824 +
14825 +       hn = hinode->hi_notify;
14826 +       if (hn) {
14827 +               hinode->hi_notify = NULL;
14828 +               if (au_hnotify_op.free(hinode, hn))
14829 +                       au_cache_free_hnotify(hn);
14830 +       }
14831 +}
14832 +
14833 +/* ---------------------------------------------------------------------- */
14834 +
14835 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
14836 +{
14837 +       if (hinode->hi_notify)
14838 +               au_hnotify_op.ctl(hinode, do_set);
14839 +}
14840 +
14841 +void au_hn_reset(struct inode *inode, unsigned int flags)
14842 +{
14843 +       aufs_bindex_t bindex, bend;
14844 +       struct inode *hi;
14845 +       struct dentry *iwhdentry;
14846 +
14847 +       bend = au_ibend(inode);
14848 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14849 +               hi = au_h_iptr(inode, bindex);
14850 +               if (!hi)
14851 +                       continue;
14852 +
14853 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
14854 +               iwhdentry = au_hi_wh(inode, bindex);
14855 +               if (iwhdentry)
14856 +                       dget(iwhdentry);
14857 +               au_igrab(hi);
14858 +               au_set_h_iptr(inode, bindex, NULL, 0);
14859 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
14860 +                             flags & ~AuHi_XINO);
14861 +               iput(hi);
14862 +               dput(iwhdentry);
14863 +               /* mutex_unlock(&hi->i_mutex); */
14864 +       }
14865 +}
14866 +
14867 +/* ---------------------------------------------------------------------- */
14868 +
14869 +static int hn_xino(struct inode *inode, struct inode *h_inode)
14870 +{
14871 +       int err;
14872 +       aufs_bindex_t bindex, bend, bfound, bstart;
14873 +       struct inode *h_i;
14874 +
14875 +       err = 0;
14876 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14877 +               pr_warn("branch root dir was changed\n");
14878 +               goto out;
14879 +       }
14880 +
14881 +       bfound = -1;
14882 +       bend = au_ibend(inode);
14883 +       bstart = au_ibstart(inode);
14884 +#if 0 /* reserved for future use */
14885 +       if (bindex == bend) {
14886 +               /* keep this ino in rename case */
14887 +               goto out;
14888 +       }
14889 +#endif
14890 +       for (bindex = bstart; bindex <= bend; bindex++)
14891 +               if (au_h_iptr(inode, bindex) == h_inode) {
14892 +                       bfound = bindex;
14893 +                       break;
14894 +               }
14895 +       if (bfound < 0)
14896 +               goto out;
14897 +
14898 +       for (bindex = bstart; bindex <= bend; bindex++) {
14899 +               h_i = au_h_iptr(inode, bindex);
14900 +               if (!h_i)
14901 +                       continue;
14902 +
14903 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
14904 +               /* ignore this error */
14905 +               /* bad action? */
14906 +       }
14907 +
14908 +       /* children inode number will be broken */
14909 +
14910 +out:
14911 +       AuTraceErr(err);
14912 +       return err;
14913 +}
14914 +
14915 +static int hn_gen_tree(struct dentry *dentry)
14916 +{
14917 +       int err, i, j, ndentry;
14918 +       struct au_dcsub_pages dpages;
14919 +       struct au_dpage *dpage;
14920 +       struct dentry **dentries;
14921 +
14922 +       err = au_dpages_init(&dpages, GFP_NOFS);
14923 +       if (unlikely(err))
14924 +               goto out;
14925 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
14926 +       if (unlikely(err))
14927 +               goto out_dpages;
14928 +
14929 +       for (i = 0; i < dpages.ndpage; i++) {
14930 +               dpage = dpages.dpages + i;
14931 +               dentries = dpage->dentries;
14932 +               ndentry = dpage->ndentry;
14933 +               for (j = 0; j < ndentry; j++) {
14934 +                       struct dentry *d;
14935 +
14936 +                       d = dentries[j];
14937 +                       if (IS_ROOT(d))
14938 +                               continue;
14939 +
14940 +                       au_digen_dec(d);
14941 +                       if (d_really_is_positive(d))
14942 +                               /* todo: reset children xino?
14943 +                                  cached children only? */
14944 +                               au_iigen_dec(d_inode(d));
14945 +               }
14946 +       }
14947 +
14948 +out_dpages:
14949 +       au_dpages_free(&dpages);
14950 +
14951 +#if 0
14952 +       /* discard children */
14953 +       dentry_unhash(dentry);
14954 +       dput(dentry);
14955 +#endif
14956 +out:
14957 +       return err;
14958 +}
14959 +
14960 +/*
14961 + * return 0 if processed.
14962 + */
14963 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
14964 +                          const unsigned int isdir)
14965 +{
14966 +       int err;
14967 +       struct dentry *d;
14968 +       struct qstr *dname;
14969 +
14970 +       err = 1;
14971 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14972 +               pr_warn("branch root dir was changed\n");
14973 +               err = 0;
14974 +               goto out;
14975 +       }
14976 +
14977 +       if (!isdir) {
14978 +               AuDebugOn(!name);
14979 +               au_iigen_dec(inode);
14980 +               spin_lock(&inode->i_lock);
14981 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
14982 +                       spin_lock(&d->d_lock);
14983 +                       dname = &d->d_name;
14984 +                       if (dname->len != nlen
14985 +                           && memcmp(dname->name, name, nlen)) {
14986 +                               spin_unlock(&d->d_lock);
14987 +                               continue;
14988 +                       }
14989 +                       err = 0;
14990 +                       au_digen_dec(d);
14991 +                       spin_unlock(&d->d_lock);
14992 +                       break;
14993 +               }
14994 +               spin_unlock(&inode->i_lock);
14995 +       } else {
14996 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
14997 +               d = d_find_any_alias(inode);
14998 +               if (!d) {
14999 +                       au_iigen_dec(inode);
15000 +                       goto out;
15001 +               }
15002 +
15003 +               spin_lock(&d->d_lock);
15004 +               dname = &d->d_name;
15005 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15006 +                       spin_unlock(&d->d_lock);
15007 +                       err = hn_gen_tree(d);
15008 +                       spin_lock(&d->d_lock);
15009 +               }
15010 +               spin_unlock(&d->d_lock);
15011 +               dput(d);
15012 +       }
15013 +
15014 +out:
15015 +       AuTraceErr(err);
15016 +       return err;
15017 +}
15018 +
15019 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15020 +{
15021 +       int err;
15022 +
15023 +       if (IS_ROOT(dentry)) {
15024 +               pr_warn("branch root dir was changed\n");
15025 +               return 0;
15026 +       }
15027 +
15028 +       err = 0;
15029 +       if (!isdir) {
15030 +               au_digen_dec(dentry);
15031 +               if (d_really_is_positive(dentry))
15032 +                       au_iigen_dec(d_inode(dentry));
15033 +       } else {
15034 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15035 +               if (d_really_is_positive(dentry))
15036 +                       err = hn_gen_tree(dentry);
15037 +       }
15038 +
15039 +       AuTraceErr(err);
15040 +       return err;
15041 +}
15042 +
15043 +/* ---------------------------------------------------------------------- */
15044 +
15045 +/* hnotify job flags */
15046 +#define AuHnJob_XINO0          1
15047 +#define AuHnJob_GEN            (1 << 1)
15048 +#define AuHnJob_DIRENT         (1 << 2)
15049 +#define AuHnJob_ISDIR          (1 << 3)
15050 +#define AuHnJob_TRYXINO0       (1 << 4)
15051 +#define AuHnJob_MNTPNT         (1 << 5)
15052 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15053 +#define au_fset_hnjob(flags, name) \
15054 +       do { (flags) |= AuHnJob_##name; } while (0)
15055 +#define au_fclr_hnjob(flags, name) \
15056 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15057 +
15058 +enum {
15059 +       AuHn_CHILD,
15060 +       AuHn_PARENT,
15061 +       AuHnLast
15062 +};
15063 +
15064 +struct au_hnotify_args {
15065 +       struct inode *h_dir, *dir, *h_child_inode;
15066 +       u32 mask;
15067 +       unsigned int flags[AuHnLast];
15068 +       unsigned int h_child_nlen;
15069 +       char h_child_name[];
15070 +};
15071 +
15072 +struct hn_job_args {
15073 +       unsigned int flags;
15074 +       struct inode *inode, *h_inode, *dir, *h_dir;
15075 +       struct dentry *dentry;
15076 +       char *h_name;
15077 +       int h_nlen;
15078 +};
15079 +
15080 +static int hn_job(struct hn_job_args *a)
15081 +{
15082 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15083 +       int e;
15084 +
15085 +       /* reset xino */
15086 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15087 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15088 +
15089 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15090 +           && a->inode
15091 +           && a->h_inode) {
15092 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
15093 +               if (!a->h_inode->i_nlink
15094 +                   && !(a->h_inode->i_state & I_LINKABLE))
15095 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15096 +               mutex_unlock(&a->h_inode->i_mutex);
15097 +       }
15098 +
15099 +       /* make the generation obsolete */
15100 +       if (au_ftest_hnjob(a->flags, GEN)) {
15101 +               e = -1;
15102 +               if (a->inode)
15103 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15104 +                                             isdir);
15105 +               if (e && a->dentry)
15106 +                       hn_gen_by_name(a->dentry, isdir);
15107 +               /* ignore this error */
15108 +       }
15109 +
15110 +       /* make dir entries obsolete */
15111 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15112 +               struct au_vdir *vdir;
15113 +
15114 +               vdir = au_ivdir(a->inode);
15115 +               if (vdir)
15116 +                       vdir->vd_jiffy = 0;
15117 +               /* IMustLock(a->inode); */
15118 +               /* a->inode->i_version++; */
15119 +       }
15120 +
15121 +       /* can do nothing but warn */
15122 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15123 +           && a->dentry
15124 +           && d_mountpoint(a->dentry))
15125 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15126 +
15127 +       return 0;
15128 +}
15129 +
15130 +/* ---------------------------------------------------------------------- */
15131 +
15132 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15133 +                                          struct inode *dir)
15134 +{
15135 +       struct dentry *dentry, *d, *parent;
15136 +       struct qstr *dname;
15137 +
15138 +       parent = d_find_any_alias(dir);
15139 +       if (!parent)
15140 +               return NULL;
15141 +
15142 +       dentry = NULL;
15143 +       spin_lock(&parent->d_lock);
15144 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15145 +               /* AuDbg("%pd\n", d); */
15146 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15147 +               dname = &d->d_name;
15148 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15149 +                       goto cont_unlock;
15150 +               if (au_di(d))
15151 +                       au_digen_dec(d);
15152 +               else
15153 +                       goto cont_unlock;
15154 +               if (au_dcount(d) > 0) {
15155 +                       dentry = dget_dlock(d);
15156 +                       spin_unlock(&d->d_lock);
15157 +                       break;
15158 +               }
15159 +
15160 +cont_unlock:
15161 +               spin_unlock(&d->d_lock);
15162 +       }
15163 +       spin_unlock(&parent->d_lock);
15164 +       dput(parent);
15165 +
15166 +       if (dentry)
15167 +               di_write_lock_child(dentry);
15168 +
15169 +       return dentry;
15170 +}
15171 +
15172 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15173 +                                        aufs_bindex_t bindex, ino_t h_ino)
15174 +{
15175 +       struct inode *inode;
15176 +       ino_t ino;
15177 +       int err;
15178 +
15179 +       inode = NULL;
15180 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15181 +       if (!err && ino)
15182 +               inode = ilookup(sb, ino);
15183 +       if (!inode)
15184 +               goto out;
15185 +
15186 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15187 +               pr_warn("wrong root branch\n");
15188 +               iput(inode);
15189 +               inode = NULL;
15190 +               goto out;
15191 +       }
15192 +
15193 +       ii_write_lock_child(inode);
15194 +
15195 +out:
15196 +       return inode;
15197 +}
15198 +
15199 +static void au_hn_bh(void *_args)
15200 +{
15201 +       struct au_hnotify_args *a = _args;
15202 +       struct super_block *sb;
15203 +       aufs_bindex_t bindex, bend, bfound;
15204 +       unsigned char xino, try_iput;
15205 +       int err;
15206 +       struct inode *inode;
15207 +       ino_t h_ino;
15208 +       struct hn_job_args args;
15209 +       struct dentry *dentry;
15210 +       struct au_sbinfo *sbinfo;
15211 +
15212 +       AuDebugOn(!_args);
15213 +       AuDebugOn(!a->h_dir);
15214 +       AuDebugOn(!a->dir);
15215 +       AuDebugOn(!a->mask);
15216 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15217 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15218 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15219 +
15220 +       inode = NULL;
15221 +       dentry = NULL;
15222 +       /*
15223 +        * do not lock a->dir->i_mutex here
15224 +        * because of d_revalidate() may cause a deadlock.
15225 +        */
15226 +       sb = a->dir->i_sb;
15227 +       AuDebugOn(!sb);
15228 +       sbinfo = au_sbi(sb);
15229 +       AuDebugOn(!sbinfo);
15230 +       si_write_lock(sb, AuLock_NOPLMW);
15231 +
15232 +       ii_read_lock_parent(a->dir);
15233 +       bfound = -1;
15234 +       bend = au_ibend(a->dir);
15235 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
15236 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15237 +                       bfound = bindex;
15238 +                       break;
15239 +               }
15240 +       ii_read_unlock(a->dir);
15241 +       if (unlikely(bfound < 0))
15242 +               goto out;
15243 +
15244 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15245 +       h_ino = 0;
15246 +       if (a->h_child_inode)
15247 +               h_ino = a->h_child_inode->i_ino;
15248 +
15249 +       if (a->h_child_nlen
15250 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15251 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15252 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15253 +                                             a->dir);
15254 +       try_iput = 0;
15255 +       if (dentry && d_really_is_positive(dentry))
15256 +               inode = d_inode(dentry);
15257 +       if (xino && !inode && h_ino
15258 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15259 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15260 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15261 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15262 +               try_iput = 1;
15263 +           }
15264 +
15265 +       args.flags = a->flags[AuHn_CHILD];
15266 +       args.dentry = dentry;
15267 +       args.inode = inode;
15268 +       args.h_inode = a->h_child_inode;
15269 +       args.dir = a->dir;
15270 +       args.h_dir = a->h_dir;
15271 +       args.h_name = a->h_child_name;
15272 +       args.h_nlen = a->h_child_nlen;
15273 +       err = hn_job(&args);
15274 +       if (dentry) {
15275 +               if (au_di(dentry))
15276 +                       di_write_unlock(dentry);
15277 +               dput(dentry);
15278 +       }
15279 +       if (inode && try_iput) {
15280 +               ii_write_unlock(inode);
15281 +               iput(inode);
15282 +       }
15283 +
15284 +       ii_write_lock_parent(a->dir);
15285 +       args.flags = a->flags[AuHn_PARENT];
15286 +       args.dentry = NULL;
15287 +       args.inode = a->dir;
15288 +       args.h_inode = a->h_dir;
15289 +       args.dir = NULL;
15290 +       args.h_dir = NULL;
15291 +       args.h_name = NULL;
15292 +       args.h_nlen = 0;
15293 +       err = hn_job(&args);
15294 +       ii_write_unlock(a->dir);
15295 +
15296 +out:
15297 +       iput(a->h_child_inode);
15298 +       iput(a->h_dir);
15299 +       iput(a->dir);
15300 +       si_write_unlock(sb);
15301 +       au_nwt_done(&sbinfo->si_nowait);
15302 +       kfree(a);
15303 +}
15304 +
15305 +/* ---------------------------------------------------------------------- */
15306 +
15307 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15308 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15309 +{
15310 +       int err, len;
15311 +       unsigned int flags[AuHnLast], f;
15312 +       unsigned char isdir, isroot, wh;
15313 +       struct inode *dir;
15314 +       struct au_hnotify_args *args;
15315 +       char *p, *h_child_name;
15316 +
15317 +       err = 0;
15318 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15319 +       dir = igrab(hnotify->hn_aufs_inode);
15320 +       if (!dir)
15321 +               goto out;
15322 +
15323 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15324 +       wh = 0;
15325 +       h_child_name = (void *)h_child_qstr->name;
15326 +       len = h_child_qstr->len;
15327 +       if (h_child_name) {
15328 +               if (len > AUFS_WH_PFX_LEN
15329 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15330 +                       h_child_name += AUFS_WH_PFX_LEN;
15331 +                       len -= AUFS_WH_PFX_LEN;
15332 +                       wh = 1;
15333 +               }
15334 +       }
15335 +
15336 +       isdir = 0;
15337 +       if (h_child_inode)
15338 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15339 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15340 +       flags[AuHn_CHILD] = 0;
15341 +       if (isdir)
15342 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15343 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15344 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15345 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15346 +       case FS_MOVED_FROM:
15347 +       case FS_MOVED_TO:
15348 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15349 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15350 +               /*FALLTHROUGH*/
15351 +       case FS_CREATE:
15352 +               AuDebugOn(!h_child_name);
15353 +               break;
15354 +
15355 +       case FS_DELETE:
15356 +               /*
15357 +                * aufs never be able to get this child inode.
15358 +                * revalidation should be in d_revalidate()
15359 +                * by checking i_nlink, i_generation or d_unhashed().
15360 +                */
15361 +               AuDebugOn(!h_child_name);
15362 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15363 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15364 +               break;
15365 +
15366 +       default:
15367 +               AuDebugOn(1);
15368 +       }
15369 +
15370 +       if (wh)
15371 +               h_child_inode = NULL;
15372 +
15373 +       err = -ENOMEM;
15374 +       /* iput() and kfree() will be called in au_hnotify() */
15375 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15376 +       if (unlikely(!args)) {
15377 +               AuErr1("no memory\n");
15378 +               iput(dir);
15379 +               goto out;
15380 +       }
15381 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15382 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15383 +       args->mask = mask;
15384 +       args->dir = dir;
15385 +       args->h_dir = igrab(h_dir);
15386 +       if (h_child_inode)
15387 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15388 +       args->h_child_inode = h_child_inode;
15389 +       args->h_child_nlen = len;
15390 +       if (len) {
15391 +               p = (void *)args;
15392 +               p += sizeof(*args);
15393 +               memcpy(p, h_child_name, len);
15394 +               p[len] = 0;
15395 +       }
15396 +
15397 +       /* NFS fires the event for silly-renamed one from kworker */
15398 +       f = 0;
15399 +       if (!dir->i_nlink
15400 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15401 +               f = AuWkq_NEST;
15402 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15403 +       if (unlikely(err)) {
15404 +               pr_err("wkq %d\n", err);
15405 +               iput(args->h_child_inode);
15406 +               iput(args->h_dir);
15407 +               iput(args->dir);
15408 +               kfree(args);
15409 +       }
15410 +
15411 +out:
15412 +       return err;
15413 +}
15414 +
15415 +/* ---------------------------------------------------------------------- */
15416 +
15417 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15418 +{
15419 +       int err;
15420 +
15421 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15422 +
15423 +       err = 0;
15424 +       if (au_hnotify_op.reset_br)
15425 +               err = au_hnotify_op.reset_br(udba, br, perm);
15426 +
15427 +       return err;
15428 +}
15429 +
15430 +int au_hnotify_init_br(struct au_branch *br, int perm)
15431 +{
15432 +       int err;
15433 +
15434 +       err = 0;
15435 +       if (au_hnotify_op.init_br)
15436 +               err = au_hnotify_op.init_br(br, perm);
15437 +
15438 +       return err;
15439 +}
15440 +
15441 +void au_hnotify_fin_br(struct au_branch *br)
15442 +{
15443 +       if (au_hnotify_op.fin_br)
15444 +               au_hnotify_op.fin_br(br);
15445 +}
15446 +
15447 +static void au_hn_destroy_cache(void)
15448 +{
15449 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15450 +       au_cachep[AuCache_HNOTIFY] = NULL;
15451 +}
15452 +
15453 +int __init au_hnotify_init(void)
15454 +{
15455 +       int err;
15456 +
15457 +       err = -ENOMEM;
15458 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15459 +       if (au_cachep[AuCache_HNOTIFY]) {
15460 +               err = 0;
15461 +               if (au_hnotify_op.init)
15462 +                       err = au_hnotify_op.init();
15463 +               if (unlikely(err))
15464 +                       au_hn_destroy_cache();
15465 +       }
15466 +       AuTraceErr(err);
15467 +       return err;
15468 +}
15469 +
15470 +void au_hnotify_fin(void)
15471 +{
15472 +       if (au_hnotify_op.fin)
15473 +               au_hnotify_op.fin();
15474 +       /* cf. au_cache_fin() */
15475 +       if (au_cachep[AuCache_HNOTIFY])
15476 +               au_hn_destroy_cache();
15477 +}
15478 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15479 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15480 +++ linux/fs/aufs/iinfo.c       2016-01-13 20:11:11.669760262 +0100
15481 @@ -0,0 +1,277 @@
15482 +/*
15483 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15484 + *
15485 + * This program, aufs is free software; you can redistribute it and/or modify
15486 + * it under the terms of the GNU General Public License as published by
15487 + * the Free Software Foundation; either version 2 of the License, or
15488 + * (at your option) any later version.
15489 + *
15490 + * This program is distributed in the hope that it will be useful,
15491 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15492 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15493 + * GNU General Public License for more details.
15494 + *
15495 + * You should have received a copy of the GNU General Public License
15496 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15497 + */
15498 +
15499 +/*
15500 + * inode private data
15501 + */
15502 +
15503 +#include "aufs.h"
15504 +
15505 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15506 +{
15507 +       struct inode *h_inode;
15508 +
15509 +       IiMustAnyLock(inode);
15510 +
15511 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15512 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15513 +       return h_inode;
15514 +}
15515 +
15516 +/* todo: hard/soft set? */
15517 +void au_hiput(struct au_hinode *hinode)
15518 +{
15519 +       au_hn_free(hinode);
15520 +       dput(hinode->hi_whdentry);
15521 +       iput(hinode->hi_inode);
15522 +}
15523 +
15524 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15525 +{
15526 +       unsigned int flags;
15527 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15528 +
15529 +       flags = 0;
15530 +       if (au_opt_test(mnt_flags, XINO))
15531 +               au_fset_hi(flags, XINO);
15532 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15533 +               au_fset_hi(flags, HNOTIFY);
15534 +       return flags;
15535 +}
15536 +
15537 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15538 +                  struct inode *h_inode, unsigned int flags)
15539 +{
15540 +       struct au_hinode *hinode;
15541 +       struct inode *hi;
15542 +       struct au_iinfo *iinfo = au_ii(inode);
15543 +
15544 +       IiMustWriteLock(inode);
15545 +
15546 +       hinode = iinfo->ii_hinode + bindex;
15547 +       hi = hinode->hi_inode;
15548 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15549 +
15550 +       if (hi)
15551 +               au_hiput(hinode);
15552 +       hinode->hi_inode = h_inode;
15553 +       if (h_inode) {
15554 +               int err;
15555 +               struct super_block *sb = inode->i_sb;
15556 +               struct au_branch *br;
15557 +
15558 +               AuDebugOn(inode->i_mode
15559 +                         && (h_inode->i_mode & S_IFMT)
15560 +                         != (inode->i_mode & S_IFMT));
15561 +               if (bindex == iinfo->ii_bstart)
15562 +                       au_cpup_igen(inode, h_inode);
15563 +               br = au_sbr(sb, bindex);
15564 +               hinode->hi_id = br->br_id;
15565 +               if (au_ftest_hi(flags, XINO)) {
15566 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15567 +                                           inode->i_ino);
15568 +                       if (unlikely(err))
15569 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15570 +               }
15571 +
15572 +               if (au_ftest_hi(flags, HNOTIFY)
15573 +                   && au_br_hnotifyable(br->br_perm)) {
15574 +                       err = au_hn_alloc(hinode, inode);
15575 +                       if (unlikely(err))
15576 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15577 +               }
15578 +       }
15579 +}
15580 +
15581 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15582 +                 struct dentry *h_wh)
15583 +{
15584 +       struct au_hinode *hinode;
15585 +
15586 +       IiMustWriteLock(inode);
15587 +
15588 +       hinode = au_ii(inode)->ii_hinode + bindex;
15589 +       AuDebugOn(hinode->hi_whdentry);
15590 +       hinode->hi_whdentry = h_wh;
15591 +}
15592 +
15593 +void au_update_iigen(struct inode *inode, int half)
15594 +{
15595 +       struct au_iinfo *iinfo;
15596 +       struct au_iigen *iigen;
15597 +       unsigned int sigen;
15598 +
15599 +       sigen = au_sigen(inode->i_sb);
15600 +       iinfo = au_ii(inode);
15601 +       iigen = &iinfo->ii_generation;
15602 +       spin_lock(&iigen->ig_spin);
15603 +       iigen->ig_generation = sigen;
15604 +       if (half)
15605 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15606 +       else
15607 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15608 +       spin_unlock(&iigen->ig_spin);
15609 +}
15610 +
15611 +/* it may be called at remount time, too */
15612 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15613 +{
15614 +       struct au_iinfo *iinfo;
15615 +       aufs_bindex_t bindex, bend;
15616 +
15617 +       iinfo = au_ii(inode);
15618 +       if (!iinfo)
15619 +               return;
15620 +
15621 +       IiMustWriteLock(inode);
15622 +
15623 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
15624 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15625 +                    bindex++) {
15626 +                       struct inode *h_i;
15627 +
15628 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15629 +                       if (h_i
15630 +                           && !h_i->i_nlink
15631 +                           && !(h_i->i_state & I_LINKABLE))
15632 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15633 +               }
15634 +       }
15635 +
15636 +       iinfo->ii_bstart = -1;
15637 +       iinfo->ii_bend = -1;
15638 +       bend = au_sbend(inode->i_sb);
15639 +       for (bindex = 0; bindex <= bend; bindex++)
15640 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15641 +                       iinfo->ii_bstart = bindex;
15642 +                       break;
15643 +               }
15644 +       if (iinfo->ii_bstart >= 0)
15645 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
15646 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15647 +                               iinfo->ii_bend = bindex;
15648 +                               break;
15649 +                       }
15650 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
15651 +}
15652 +
15653 +/* ---------------------------------------------------------------------- */
15654 +
15655 +void au_icntnr_init_once(void *_c)
15656 +{
15657 +       struct au_icntnr *c = _c;
15658 +       struct au_iinfo *iinfo = &c->iinfo;
15659 +       static struct lock_class_key aufs_ii;
15660 +
15661 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
15662 +       au_rw_init(&iinfo->ii_rwsem);
15663 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
15664 +       inode_init_once(&c->vfs_inode);
15665 +}
15666 +
15667 +int au_iinfo_init(struct inode *inode)
15668 +{
15669 +       struct au_iinfo *iinfo;
15670 +       struct super_block *sb;
15671 +       int nbr, i;
15672 +
15673 +       sb = inode->i_sb;
15674 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15675 +       nbr = au_sbend(sb) + 1;
15676 +       if (unlikely(nbr <= 0))
15677 +               nbr = 1;
15678 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15679 +       if (iinfo->ii_hinode) {
15680 +               au_ninodes_inc(sb);
15681 +               for (i = 0; i < nbr; i++)
15682 +                       iinfo->ii_hinode[i].hi_id = -1;
15683 +
15684 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
15685 +               iinfo->ii_bstart = -1;
15686 +               iinfo->ii_bend = -1;
15687 +               iinfo->ii_vdir = NULL;
15688 +               return 0;
15689 +       }
15690 +       return -ENOMEM;
15691 +}
15692 +
15693 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15694 +{
15695 +       int err, sz;
15696 +       struct au_hinode *hip;
15697 +
15698 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
15699 +
15700 +       err = -ENOMEM;
15701 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15702 +       if (!sz)
15703 +               sz = sizeof(*hip);
15704 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15705 +       if (hip) {
15706 +               iinfo->ii_hinode = hip;
15707 +               err = 0;
15708 +       }
15709 +
15710 +       return err;
15711 +}
15712 +
15713 +void au_iinfo_fin(struct inode *inode)
15714 +{
15715 +       struct au_iinfo *iinfo;
15716 +       struct au_hinode *hi;
15717 +       struct super_block *sb;
15718 +       aufs_bindex_t bindex, bend;
15719 +       const unsigned char unlinked = !inode->i_nlink;
15720 +
15721 +       iinfo = au_ii(inode);
15722 +       /* bad_inode case */
15723 +       if (!iinfo)
15724 +               return;
15725 +
15726 +       sb = inode->i_sb;
15727 +       au_ninodes_dec(sb);
15728 +       if (si_pid_test(sb))
15729 +               au_xino_delete_inode(inode, unlinked);
15730 +       else {
15731 +               /*
15732 +                * it is safe to hide the dependency between sbinfo and
15733 +                * sb->s_umount.
15734 +                */
15735 +               lockdep_off();
15736 +               si_noflush_read_lock(sb);
15737 +               au_xino_delete_inode(inode, unlinked);
15738 +               si_read_unlock(sb);
15739 +               lockdep_on();
15740 +       }
15741 +
15742 +       if (iinfo->ii_vdir)
15743 +               au_vdir_free(iinfo->ii_vdir);
15744 +
15745 +       bindex = iinfo->ii_bstart;
15746 +       if (bindex >= 0) {
15747 +               hi = iinfo->ii_hinode + bindex;
15748 +               bend = iinfo->ii_bend;
15749 +               while (bindex++ <= bend) {
15750 +                       if (hi->hi_inode)
15751 +                               au_hiput(hi);
15752 +                       hi++;
15753 +               }
15754 +       }
15755 +       kfree(iinfo->ii_hinode);
15756 +       iinfo->ii_hinode = NULL;
15757 +       AuRwDestroy(&iinfo->ii_rwsem);
15758 +}
15759 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
15760 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
15761 +++ linux/fs/aufs/inode.c       2016-01-24 20:34:04.746537160 +0100
15762 @@ -0,0 +1,527 @@
15763 +/*
15764 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15765 + *
15766 + * This program, aufs is free software; you can redistribute it and/or modify
15767 + * it under the terms of the GNU General Public License as published by
15768 + * the Free Software Foundation; either version 2 of the License, or
15769 + * (at your option) any later version.
15770 + *
15771 + * This program is distributed in the hope that it will be useful,
15772 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15773 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15774 + * GNU General Public License for more details.
15775 + *
15776 + * You should have received a copy of the GNU General Public License
15777 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15778 + */
15779 +
15780 +/*
15781 + * inode functions
15782 + */
15783 +
15784 +#include "aufs.h"
15785 +
15786 +struct inode *au_igrab(struct inode *inode)
15787 +{
15788 +       if (inode) {
15789 +               AuDebugOn(!atomic_read(&inode->i_count));
15790 +               ihold(inode);
15791 +       }
15792 +       return inode;
15793 +}
15794 +
15795 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15796 +{
15797 +       au_cpup_attr_all(inode, /*force*/0);
15798 +       au_update_iigen(inode, /*half*/1);
15799 +       if (do_version)
15800 +               inode->i_version++;
15801 +}
15802 +
15803 +static int au_ii_refresh(struct inode *inode, int *update)
15804 +{
15805 +       int err, e;
15806 +       umode_t type;
15807 +       aufs_bindex_t bindex, new_bindex;
15808 +       struct super_block *sb;
15809 +       struct au_iinfo *iinfo;
15810 +       struct au_hinode *p, *q, tmp;
15811 +
15812 +       IiMustWriteLock(inode);
15813 +
15814 +       *update = 0;
15815 +       sb = inode->i_sb;
15816 +       type = inode->i_mode & S_IFMT;
15817 +       iinfo = au_ii(inode);
15818 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15819 +       if (unlikely(err))
15820 +               goto out;
15821 +
15822 +       AuDebugOn(iinfo->ii_bstart < 0);
15823 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15824 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15825 +            bindex++, p++) {
15826 +               if (!p->hi_inode)
15827 +                       continue;
15828 +
15829 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
15830 +               new_bindex = au_br_index(sb, p->hi_id);
15831 +               if (new_bindex == bindex)
15832 +                       continue;
15833 +
15834 +               if (new_bindex < 0) {
15835 +                       *update = 1;
15836 +                       au_hiput(p);
15837 +                       p->hi_inode = NULL;
15838 +                       continue;
15839 +               }
15840 +
15841 +               if (new_bindex < iinfo->ii_bstart)
15842 +                       iinfo->ii_bstart = new_bindex;
15843 +               if (iinfo->ii_bend < new_bindex)
15844 +                       iinfo->ii_bend = new_bindex;
15845 +               /* swap two lower inode, and loop again */
15846 +               q = iinfo->ii_hinode + new_bindex;
15847 +               tmp = *q;
15848 +               *q = *p;
15849 +               *p = tmp;
15850 +               if (tmp.hi_inode) {
15851 +                       bindex--;
15852 +                       p--;
15853 +               }
15854 +       }
15855 +       au_update_ibrange(inode, /*do_put_zero*/0);
15856 +       e = au_dy_irefresh(inode);
15857 +       if (unlikely(e && !err))
15858 +               err = e;
15859 +
15860 +out:
15861 +       AuTraceErr(err);
15862 +       return err;
15863 +}
15864 +
15865 +void au_refresh_iop(struct inode *inode, int force_getattr)
15866 +{
15867 +       int type;
15868 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
15869 +       const struct inode_operations *iop
15870 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
15871 +
15872 +       if (inode->i_op == iop)
15873 +               return;
15874 +
15875 +       switch (inode->i_mode & S_IFMT) {
15876 +       case S_IFDIR:
15877 +               type = AuIop_DIR;
15878 +               break;
15879 +       case S_IFLNK:
15880 +               type = AuIop_SYMLINK;
15881 +               break;
15882 +       default:
15883 +               type = AuIop_OTHER;
15884 +               break;
15885 +       }
15886 +
15887 +       inode->i_op = iop + type;
15888 +       /* unnecessary smp_wmb() */
15889 +}
15890 +
15891 +int au_refresh_hinode_self(struct inode *inode)
15892 +{
15893 +       int err, update;
15894 +
15895 +       err = au_ii_refresh(inode, &update);
15896 +       if (!err)
15897 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15898 +
15899 +       AuTraceErr(err);
15900 +       return err;
15901 +}
15902 +
15903 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
15904 +{
15905 +       int err, e, update;
15906 +       unsigned int flags;
15907 +       umode_t mode;
15908 +       aufs_bindex_t bindex, bend;
15909 +       unsigned char isdir;
15910 +       struct au_hinode *p;
15911 +       struct au_iinfo *iinfo;
15912 +
15913 +       err = au_ii_refresh(inode, &update);
15914 +       if (unlikely(err))
15915 +               goto out;
15916 +
15917 +       update = 0;
15918 +       iinfo = au_ii(inode);
15919 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15920 +       mode = (inode->i_mode & S_IFMT);
15921 +       isdir = S_ISDIR(mode);
15922 +       flags = au_hi_flags(inode, isdir);
15923 +       bend = au_dbend(dentry);
15924 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
15925 +               struct inode *h_i, *h_inode;
15926 +               struct dentry *h_d;
15927 +
15928 +               h_d = au_h_dptr(dentry, bindex);
15929 +               if (!h_d || d_is_negative(h_d))
15930 +                       continue;
15931 +
15932 +               h_inode = d_inode(h_d);
15933 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
15934 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15935 +                       h_i = au_h_iptr(inode, bindex);
15936 +                       if (h_i) {
15937 +                               if (h_i == h_inode)
15938 +                                       continue;
15939 +                               err = -EIO;
15940 +                               break;
15941 +                       }
15942 +               }
15943 +               if (bindex < iinfo->ii_bstart)
15944 +                       iinfo->ii_bstart = bindex;
15945 +               if (iinfo->ii_bend < bindex)
15946 +                       iinfo->ii_bend = bindex;
15947 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
15948 +               update = 1;
15949 +       }
15950 +       au_update_ibrange(inode, /*do_put_zero*/0);
15951 +       e = au_dy_irefresh(inode);
15952 +       if (unlikely(e && !err))
15953 +               err = e;
15954 +       if (!err)
15955 +               au_refresh_hinode_attr(inode, update && isdir);
15956 +
15957 +out:
15958 +       AuTraceErr(err);
15959 +       return err;
15960 +}
15961 +
15962 +static int set_inode(struct inode *inode, struct dentry *dentry)
15963 +{
15964 +       int err;
15965 +       unsigned int flags;
15966 +       umode_t mode;
15967 +       aufs_bindex_t bindex, bstart, btail;
15968 +       unsigned char isdir;
15969 +       struct dentry *h_dentry;
15970 +       struct inode *h_inode;
15971 +       struct au_iinfo *iinfo;
15972 +       struct inode_operations *iop;
15973 +
15974 +       IiMustWriteLock(inode);
15975 +
15976 +       err = 0;
15977 +       isdir = 0;
15978 +       iop = au_sbi(inode->i_sb)->si_iop_array;
15979 +       bstart = au_dbstart(dentry);
15980 +       h_dentry = au_h_dptr(dentry, bstart);
15981 +       h_inode = d_inode(h_dentry);
15982 +       mode = h_inode->i_mode;
15983 +       switch (mode & S_IFMT) {
15984 +       case S_IFREG:
15985 +               btail = au_dbtail(dentry);
15986 +               inode->i_op = iop + AuIop_OTHER;
15987 +               inode->i_fop = &aufs_file_fop;
15988 +               err = au_dy_iaop(inode, bstart, h_inode);
15989 +               if (unlikely(err))
15990 +                       goto out;
15991 +               break;
15992 +       case S_IFDIR:
15993 +               isdir = 1;
15994 +               btail = au_dbtaildir(dentry);
15995 +               inode->i_op = iop + AuIop_DIR;
15996 +               inode->i_fop = &aufs_dir_fop;
15997 +               break;
15998 +       case S_IFLNK:
15999 +               btail = au_dbtail(dentry);
16000 +               inode->i_op = iop + AuIop_SYMLINK;
16001 +               break;
16002 +       case S_IFBLK:
16003 +       case S_IFCHR:
16004 +       case S_IFIFO:
16005 +       case S_IFSOCK:
16006 +               btail = au_dbtail(dentry);
16007 +               inode->i_op = iop + AuIop_OTHER;
16008 +               init_special_inode(inode, mode, h_inode->i_rdev);
16009 +               break;
16010 +       default:
16011 +               AuIOErr("Unknown file type 0%o\n", mode);
16012 +               err = -EIO;
16013 +               goto out;
16014 +       }
16015 +
16016 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
16017 +       flags = au_hi_flags(inode, isdir);
16018 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16019 +           && au_ftest_hi(flags, HNOTIFY)
16020 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
16021 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16022 +               au_fclr_hi(flags, HNOTIFY);
16023 +       iinfo = au_ii(inode);
16024 +       iinfo->ii_bstart = bstart;
16025 +       iinfo->ii_bend = btail;
16026 +       for (bindex = bstart; bindex <= btail; bindex++) {
16027 +               h_dentry = au_h_dptr(dentry, bindex);
16028 +               if (h_dentry)
16029 +                       au_set_h_iptr(inode, bindex,
16030 +                                     au_igrab(d_inode(h_dentry)), flags);
16031 +       }
16032 +       au_cpup_attr_all(inode, /*force*/1);
16033 +       /*
16034 +        * to force calling aufs_get_acl() every time,
16035 +        * do not call cache_no_acl() for aufs inode.
16036 +        */
16037 +
16038 +out:
16039 +       return err;
16040 +}
16041 +
16042 +/*
16043 + * successful returns with iinfo write_locked
16044 + * minus: errno
16045 + * zero: success, matched
16046 + * plus: no error, but unmatched
16047 + */
16048 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16049 +{
16050 +       int err;
16051 +       unsigned int gen, igflags;
16052 +       aufs_bindex_t bindex, bend;
16053 +       struct inode *h_inode, *h_dinode;
16054 +       struct dentry *h_dentry;
16055 +
16056 +       /*
16057 +        * before this function, if aufs got any iinfo lock, it must be only
16058 +        * one, the parent dir.
16059 +        * it can happen by UDBA and the obsoleted inode number.
16060 +        */
16061 +       err = -EIO;
16062 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16063 +               goto out;
16064 +
16065 +       err = 1;
16066 +       ii_write_lock_new_child(inode);
16067 +       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
16068 +       h_dinode = d_inode(h_dentry);
16069 +       bend = au_ibend(inode);
16070 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
16071 +               h_inode = au_h_iptr(inode, bindex);
16072 +               if (!h_inode || h_inode != h_dinode)
16073 +                       continue;
16074 +
16075 +               err = 0;
16076 +               gen = au_iigen(inode, &igflags);
16077 +               if (gen == au_digen(dentry)
16078 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
16079 +                       break;
16080 +
16081 +               /* fully refresh inode using dentry */
16082 +               err = au_refresh_hinode(inode, dentry);
16083 +               if (!err)
16084 +                       au_update_iigen(inode, /*half*/0);
16085 +               break;
16086 +       }
16087 +
16088 +       if (unlikely(err))
16089 +               ii_write_unlock(inode);
16090 +out:
16091 +       return err;
16092 +}
16093 +
16094 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16095 +          unsigned int d_type, ino_t *ino)
16096 +{
16097 +       int err;
16098 +       struct mutex *mtx;
16099 +
16100 +       /* prevent hardlinked inode number from race condition */
16101 +       mtx = NULL;
16102 +       if (d_type != DT_DIR) {
16103 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16104 +               mutex_lock(mtx);
16105 +       }
16106 +       err = au_xino_read(sb, bindex, h_ino, ino);
16107 +       if (unlikely(err))
16108 +               goto out;
16109 +
16110 +       if (!*ino) {
16111 +               err = -EIO;
16112 +               *ino = au_xino_new_ino(sb);
16113 +               if (unlikely(!*ino))
16114 +                       goto out;
16115 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16116 +               if (unlikely(err))
16117 +                       goto out;
16118 +       }
16119 +
16120 +out:
16121 +       if (mtx)
16122 +               mutex_unlock(mtx);
16123 +       return err;
16124 +}
16125 +
16126 +/* successful returns with iinfo write_locked */
16127 +/* todo: return with unlocked? */
16128 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16129 +{
16130 +       struct inode *inode, *h_inode;
16131 +       struct dentry *h_dentry;
16132 +       struct super_block *sb;
16133 +       struct mutex *mtx;
16134 +       ino_t h_ino, ino;
16135 +       int err;
16136 +       aufs_bindex_t bstart;
16137 +
16138 +       sb = dentry->d_sb;
16139 +       bstart = au_dbstart(dentry);
16140 +       h_dentry = au_h_dptr(dentry, bstart);
16141 +       h_inode = d_inode(h_dentry);
16142 +       h_ino = h_inode->i_ino;
16143 +
16144 +       /*
16145 +        * stop 'race'-ing between hardlinks under different
16146 +        * parents.
16147 +        */
16148 +       mtx = NULL;
16149 +       if (!d_is_dir(h_dentry))
16150 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
16151 +
16152 +new_ino:
16153 +       if (mtx)
16154 +               mutex_lock(mtx);
16155 +       err = au_xino_read(sb, bstart, h_ino, &ino);
16156 +       inode = ERR_PTR(err);
16157 +       if (unlikely(err))
16158 +               goto out;
16159 +
16160 +       if (!ino) {
16161 +               ino = au_xino_new_ino(sb);
16162 +               if (unlikely(!ino)) {
16163 +                       inode = ERR_PTR(-EIO);
16164 +                       goto out;
16165 +               }
16166 +       }
16167 +
16168 +       AuDbg("i%lu\n", (unsigned long)ino);
16169 +       inode = au_iget_locked(sb, ino);
16170 +       err = PTR_ERR(inode);
16171 +       if (IS_ERR(inode))
16172 +               goto out;
16173 +
16174 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16175 +       if (inode->i_state & I_NEW) {
16176 +               /* verbose coding for lock class name */
16177 +               if (unlikely(d_is_symlink(h_dentry)))
16178 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16179 +                                   au_lc_key + AuLcSymlink_IIINFO);
16180 +               else if (unlikely(d_is_dir(h_dentry)))
16181 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16182 +                                   au_lc_key + AuLcDir_IIINFO);
16183 +               else /* likely */
16184 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16185 +                                   au_lc_key + AuLcNonDir_IIINFO);
16186 +
16187 +               ii_write_lock_new_child(inode);
16188 +               err = set_inode(inode, dentry);
16189 +               if (!err) {
16190 +                       unlock_new_inode(inode);
16191 +                       goto out; /* success */
16192 +               }
16193 +
16194 +               /*
16195 +                * iget_failed() calls iput(), but we need to call
16196 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16197 +                * i_count.
16198 +                */
16199 +               atomic_inc(&inode->i_count);
16200 +               iget_failed(inode);
16201 +               ii_write_unlock(inode);
16202 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
16203 +               /* ignore this error */
16204 +               goto out_iput;
16205 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16206 +               /*
16207 +                * horrible race condition between lookup, readdir and copyup
16208 +                * (or something).
16209 +                */
16210 +               if (mtx)
16211 +                       mutex_unlock(mtx);
16212 +               err = reval_inode(inode, dentry);
16213 +               if (unlikely(err < 0)) {
16214 +                       mtx = NULL;
16215 +                       goto out_iput;
16216 +               }
16217 +
16218 +               if (!err) {
16219 +                       mtx = NULL;
16220 +                       goto out; /* success */
16221 +               } else if (mtx)
16222 +                       mutex_lock(mtx);
16223 +       }
16224 +
16225 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
16226 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16227 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16228 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
16229 +                       (unsigned long)h_ino, (unsigned long)ino);
16230 +       ino = 0;
16231 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
16232 +       if (!err) {
16233 +               iput(inode);
16234 +               if (mtx)
16235 +                       mutex_unlock(mtx);
16236 +               goto new_ino;
16237 +       }
16238 +
16239 +out_iput:
16240 +       iput(inode);
16241 +       inode = ERR_PTR(err);
16242 +out:
16243 +       if (mtx)
16244 +               mutex_unlock(mtx);
16245 +       return inode;
16246 +}
16247 +
16248 +/* ---------------------------------------------------------------------- */
16249 +
16250 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16251 +              struct inode *inode)
16252 +{
16253 +       int err;
16254 +       struct inode *hi;
16255 +
16256 +       err = au_br_rdonly(au_sbr(sb, bindex));
16257 +
16258 +       /* pseudo-link after flushed may happen out of bounds */
16259 +       if (!err
16260 +           && inode
16261 +           && au_ibstart(inode) <= bindex
16262 +           && bindex <= au_ibend(inode)) {
16263 +               /*
16264 +                * permission check is unnecessary since vfsub routine
16265 +                * will be called later
16266 +                */
16267 +               hi = au_h_iptr(inode, bindex);
16268 +               if (hi)
16269 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16270 +       }
16271 +
16272 +       return err;
16273 +}
16274 +
16275 +int au_test_h_perm(struct inode *h_inode, int mask)
16276 +{
16277 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16278 +               return 0;
16279 +       return inode_permission(h_inode, mask);
16280 +}
16281 +
16282 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16283 +{
16284 +       if (au_test_nfs(h_inode->i_sb)
16285 +           && (mask & MAY_WRITE)
16286 +           && S_ISDIR(h_inode->i_mode))
16287 +               mask |= MAY_READ; /* force permission check */
16288 +       return au_test_h_perm(h_inode, mask);
16289 +}
16290 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16291 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16292 +++ linux/fs/aufs/inode.h       2016-01-24 20:34:04.746537160 +0100
16293 @@ -0,0 +1,685 @@
16294 +/*
16295 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16296 + *
16297 + * This program, aufs is free software; you can redistribute it and/or modify
16298 + * it under the terms of the GNU General Public License as published by
16299 + * the Free Software Foundation; either version 2 of the License, or
16300 + * (at your option) any later version.
16301 + *
16302 + * This program is distributed in the hope that it will be useful,
16303 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16304 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16305 + * GNU General Public License for more details.
16306 + *
16307 + * You should have received a copy of the GNU General Public License
16308 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16309 + */
16310 +
16311 +/*
16312 + * inode operations
16313 + */
16314 +
16315 +#ifndef __AUFS_INODE_H__
16316 +#define __AUFS_INODE_H__
16317 +
16318 +#ifdef __KERNEL__
16319 +
16320 +#include <linux/fsnotify.h>
16321 +#include "rwsem.h"
16322 +
16323 +struct vfsmount;
16324 +
16325 +struct au_hnotify {
16326 +#ifdef CONFIG_AUFS_HNOTIFY
16327 +#ifdef CONFIG_AUFS_HFSNOTIFY
16328 +       /* never use fsnotify_add_vfsmount_mark() */
16329 +       struct fsnotify_mark            hn_mark;
16330 +#endif
16331 +       struct inode                    *hn_aufs_inode; /* no get/put */
16332 +#endif
16333 +} ____cacheline_aligned_in_smp;
16334 +
16335 +struct au_hinode {
16336 +       struct inode            *hi_inode;
16337 +       aufs_bindex_t           hi_id;
16338 +#ifdef CONFIG_AUFS_HNOTIFY
16339 +       struct au_hnotify       *hi_notify;
16340 +#endif
16341 +
16342 +       /* reference to the copied-up whiteout with get/put */
16343 +       struct dentry           *hi_whdentry;
16344 +};
16345 +
16346 +/* ig_flags */
16347 +#define AuIG_HALF_REFRESHED            1
16348 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16349 +#define au_ig_fset(flags, name) \
16350 +       do { (flags) |= AuIG_##name; } while (0)
16351 +#define au_ig_fclr(flags, name) \
16352 +       do { (flags) &= ~AuIG_##name; } while (0)
16353 +
16354 +struct au_iigen {
16355 +       spinlock_t      ig_spin;
16356 +       __u32           ig_generation, ig_flags;
16357 +};
16358 +
16359 +struct au_vdir;
16360 +struct au_iinfo {
16361 +       struct au_iigen         ii_generation;
16362 +       struct super_block      *ii_hsb1;       /* no get/put */
16363 +
16364 +       struct au_rwsem         ii_rwsem;
16365 +       aufs_bindex_t           ii_bstart, ii_bend;
16366 +       __u32                   ii_higen;
16367 +       struct au_hinode        *ii_hinode;
16368 +       struct au_vdir          *ii_vdir;
16369 +};
16370 +
16371 +struct au_icntnr {
16372 +       struct au_iinfo iinfo;
16373 +       struct inode vfs_inode;
16374 +} ____cacheline_aligned_in_smp;
16375 +
16376 +/* au_pin flags */
16377 +#define AuPin_DI_LOCKED                1
16378 +#define AuPin_MNT_WRITE                (1 << 1)
16379 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16380 +#define au_fset_pin(flags, name) \
16381 +       do { (flags) |= AuPin_##name; } while (0)
16382 +#define au_fclr_pin(flags, name) \
16383 +       do { (flags) &= ~AuPin_##name; } while (0)
16384 +
16385 +struct au_pin {
16386 +       /* input */
16387 +       struct dentry *dentry;
16388 +       unsigned int udba;
16389 +       unsigned char lsc_di, lsc_hi, flags;
16390 +       aufs_bindex_t bindex;
16391 +
16392 +       /* output */
16393 +       struct dentry *parent;
16394 +       struct au_hinode *hdir;
16395 +       struct vfsmount *h_mnt;
16396 +
16397 +       /* temporary unlock/relock for copyup */
16398 +       struct dentry *h_dentry, *h_parent;
16399 +       struct au_branch *br;
16400 +       struct task_struct *task;
16401 +};
16402 +
16403 +void au_pin_hdir_unlock(struct au_pin *p);
16404 +int au_pin_hdir_lock(struct au_pin *p);
16405 +int au_pin_hdir_relock(struct au_pin *p);
16406 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
16407 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16408 +void au_pin_hdir_release(struct au_pin *p);
16409 +
16410 +/* ---------------------------------------------------------------------- */
16411 +
16412 +static inline struct au_iinfo *au_ii(struct inode *inode)
16413 +{
16414 +       struct au_iinfo *iinfo;
16415 +
16416 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16417 +       if (iinfo->ii_hinode)
16418 +               return iinfo;
16419 +       return NULL; /* debugging bad_inode case */
16420 +}
16421 +
16422 +/* ---------------------------------------------------------------------- */
16423 +
16424 +/* inode.c */
16425 +struct inode *au_igrab(struct inode *inode);
16426 +void au_refresh_iop(struct inode *inode, int force_getattr);
16427 +int au_refresh_hinode_self(struct inode *inode);
16428 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16429 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16430 +          unsigned int d_type, ino_t *ino);
16431 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16432 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16433 +              struct inode *inode);
16434 +int au_test_h_perm(struct inode *h_inode, int mask);
16435 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16436 +
16437 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16438 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16439 +{
16440 +#ifdef CONFIG_AUFS_SHWH
16441 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16442 +#else
16443 +       return 0;
16444 +#endif
16445 +}
16446 +
16447 +/* i_op.c */
16448 +enum {
16449 +       AuIop_SYMLINK,
16450 +       AuIop_DIR,
16451 +       AuIop_OTHER,
16452 +       AuIop_Last
16453 +};
16454 +extern struct inode_operations aufs_iop[AuIop_Last],
16455 +       aufs_iop_nogetattr[AuIop_Last];
16456 +
16457 +/* au_wr_dir flags */
16458 +#define AuWrDir_ADD_ENTRY      1
16459 +#define AuWrDir_ISDIR          (1 << 1)
16460 +#define AuWrDir_TMPFILE                (1 << 2)
16461 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16462 +#define au_fset_wrdir(flags, name) \
16463 +       do { (flags) |= AuWrDir_##name; } while (0)
16464 +#define au_fclr_wrdir(flags, name) \
16465 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16466 +
16467 +struct au_wr_dir_args {
16468 +       aufs_bindex_t force_btgt;
16469 +       unsigned char flags;
16470 +};
16471 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16472 +             struct au_wr_dir_args *args);
16473 +
16474 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16475 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16476 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16477 +                unsigned int udba, unsigned char flags);
16478 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16479 +          unsigned int udba, unsigned char flags) __must_check;
16480 +int au_do_pin(struct au_pin *pin) __must_check;
16481 +void au_unpin(struct au_pin *pin);
16482 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16483 +
16484 +#define AuIcpup_DID_CPUP       1
16485 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16486 +#define au_fset_icpup(flags, name) \
16487 +       do { (flags) |= AuIcpup_##name; } while (0)
16488 +#define au_fclr_icpup(flags, name) \
16489 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16490 +
16491 +struct au_icpup_args {
16492 +       unsigned char flags;
16493 +       unsigned char pin_flags;
16494 +       aufs_bindex_t btgt;
16495 +       unsigned int udba;
16496 +       struct au_pin pin;
16497 +       struct path h_path;
16498 +       struct inode *h_inode;
16499 +};
16500 +
16501 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16502 +                    struct au_icpup_args *a);
16503 +
16504 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
16505 +
16506 +/* i_op_add.c */
16507 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16508 +              struct dentry *h_parent, int isdir);
16509 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16510 +              dev_t dev);
16511 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16512 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16513 +               bool want_excl);
16514 +struct vfsub_aopen_args;
16515 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16516 +                      struct vfsub_aopen_args *args);
16517 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16518 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16519 +             struct dentry *dentry);
16520 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16521 +
16522 +/* i_op_del.c */
16523 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16524 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16525 +              struct dentry *h_parent, int isdir);
16526 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16527 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16528 +
16529 +/* i_op_ren.c */
16530 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16531 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16532 +               struct inode *dir, struct dentry *dentry);
16533 +
16534 +/* iinfo.c */
16535 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16536 +void au_hiput(struct au_hinode *hinode);
16537 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16538 +                 struct dentry *h_wh);
16539 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16540 +
16541 +/* hinode flags */
16542 +#define AuHi_XINO      1
16543 +#define AuHi_HNOTIFY   (1 << 1)
16544 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16545 +#define au_fset_hi(flags, name) \
16546 +       do { (flags) |= AuHi_##name; } while (0)
16547 +#define au_fclr_hi(flags, name) \
16548 +       do { (flags) &= ~AuHi_##name; } while (0)
16549 +
16550 +#ifndef CONFIG_AUFS_HNOTIFY
16551 +#undef AuHi_HNOTIFY
16552 +#define AuHi_HNOTIFY   0
16553 +#endif
16554 +
16555 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16556 +                  struct inode *h_inode, unsigned int flags);
16557 +
16558 +void au_update_iigen(struct inode *inode, int half);
16559 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16560 +
16561 +void au_icntnr_init_once(void *_c);
16562 +int au_iinfo_init(struct inode *inode);
16563 +void au_iinfo_fin(struct inode *inode);
16564 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
16565 +
16566 +#ifdef CONFIG_PROC_FS
16567 +/* plink.c */
16568 +int au_plink_maint(struct super_block *sb, int flags);
16569 +struct au_sbinfo;
16570 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16571 +int au_plink_maint_enter(struct super_block *sb);
16572 +#ifdef CONFIG_AUFS_DEBUG
16573 +void au_plink_list(struct super_block *sb);
16574 +#else
16575 +AuStubVoid(au_plink_list, struct super_block *sb)
16576 +#endif
16577 +int au_plink_test(struct inode *inode);
16578 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16579 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16580 +                    struct dentry *h_dentry);
16581 +void au_plink_put(struct super_block *sb, int verbose);
16582 +void au_plink_clean(struct super_block *sb, int verbose);
16583 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16584 +#else
16585 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16586 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16587 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16588 +AuStubVoid(au_plink_list, struct super_block *sb);
16589 +AuStubInt0(au_plink_test, struct inode *inode);
16590 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16591 +       struct inode *inode, aufs_bindex_t bindex);
16592 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16593 +          struct dentry *h_dentry);
16594 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16595 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16596 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16597 +#endif /* CONFIG_PROC_FS */
16598 +
16599 +#ifdef CONFIG_AUFS_XATTR
16600 +/* xattr.c */
16601 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16602 +                 unsigned int verbose);
16603 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16604 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
16605 +                     size_t size);
16606 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
16607 +                 size_t size, int flags);
16608 +int aufs_removexattr(struct dentry *dentry, const char *name);
16609 +
16610 +/* void au_xattr_init(struct super_block *sb); */
16611 +#else
16612 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16613 +          int ignore_flags, unsigned int verbose);
16614 +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16615 +#endif
16616 +
16617 +#ifdef CONFIG_FS_POSIX_ACL
16618 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16619 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16620 +#endif
16621 +
16622 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16623 +enum {
16624 +       AU_XATTR_SET,
16625 +       AU_XATTR_REMOVE,
16626 +       AU_ACL_SET
16627 +};
16628 +
16629 +struct au_srxattr {
16630 +       int type;
16631 +       union {
16632 +               struct {
16633 +                       const char      *name;
16634 +                       const void      *value;
16635 +                       size_t          size;
16636 +                       int             flags;
16637 +               } set;
16638 +               struct {
16639 +                       const char      *name;
16640 +               } remove;
16641 +               struct {
16642 +                       struct posix_acl *acl;
16643 +                       int             type;
16644 +               } acl_set;
16645 +       } u;
16646 +};
16647 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
16648 +#endif
16649 +
16650 +/* ---------------------------------------------------------------------- */
16651 +
16652 +/* lock subclass for iinfo */
16653 +enum {
16654 +       AuLsc_II_CHILD,         /* child first */
16655 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16656 +       AuLsc_II_CHILD3,        /* copyup dirs */
16657 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16658 +       AuLsc_II_PARENT2,
16659 +       AuLsc_II_PARENT3,       /* copyup dirs */
16660 +       AuLsc_II_NEW_CHILD
16661 +};
16662 +
16663 +/*
16664 + * ii_read_lock_child, ii_write_lock_child,
16665 + * ii_read_lock_child2, ii_write_lock_child2,
16666 + * ii_read_lock_child3, ii_write_lock_child3,
16667 + * ii_read_lock_parent, ii_write_lock_parent,
16668 + * ii_read_lock_parent2, ii_write_lock_parent2,
16669 + * ii_read_lock_parent3, ii_write_lock_parent3,
16670 + * ii_read_lock_new_child, ii_write_lock_new_child,
16671 + */
16672 +#define AuReadLockFunc(name, lsc) \
16673 +static inline void ii_read_lock_##name(struct inode *i) \
16674 +{ \
16675 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16676 +}
16677 +
16678 +#define AuWriteLockFunc(name, lsc) \
16679 +static inline void ii_write_lock_##name(struct inode *i) \
16680 +{ \
16681 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16682 +}
16683 +
16684 +#define AuRWLockFuncs(name, lsc) \
16685 +       AuReadLockFunc(name, lsc) \
16686 +       AuWriteLockFunc(name, lsc)
16687 +
16688 +AuRWLockFuncs(child, CHILD);
16689 +AuRWLockFuncs(child2, CHILD2);
16690 +AuRWLockFuncs(child3, CHILD3);
16691 +AuRWLockFuncs(parent, PARENT);
16692 +AuRWLockFuncs(parent2, PARENT2);
16693 +AuRWLockFuncs(parent3, PARENT3);
16694 +AuRWLockFuncs(new_child, NEW_CHILD);
16695 +
16696 +#undef AuReadLockFunc
16697 +#undef AuWriteLockFunc
16698 +#undef AuRWLockFuncs
16699 +
16700 +/*
16701 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
16702 + */
16703 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
16704 +
16705 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16706 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16707 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
16708 +
16709 +/* ---------------------------------------------------------------------- */
16710 +
16711 +static inline void au_icntnr_init(struct au_icntnr *c)
16712 +{
16713 +#ifdef CONFIG_AUFS_DEBUG
16714 +       c->vfs_inode.i_mode = 0;
16715 +#endif
16716 +}
16717 +
16718 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
16719 +{
16720 +       unsigned int gen;
16721 +       struct au_iinfo *iinfo;
16722 +       struct au_iigen *iigen;
16723 +
16724 +       iinfo = au_ii(inode);
16725 +       iigen = &iinfo->ii_generation;
16726 +       spin_lock(&iigen->ig_spin);
16727 +       if (igflags)
16728 +               *igflags = iigen->ig_flags;
16729 +       gen = iigen->ig_generation;
16730 +       spin_unlock(&iigen->ig_spin);
16731 +
16732 +       return gen;
16733 +}
16734 +
16735 +/* tiny test for inode number */
16736 +/* tmpfs generation is too rough */
16737 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16738 +{
16739 +       struct au_iinfo *iinfo;
16740 +
16741 +       iinfo = au_ii(inode);
16742 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
16743 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
16744 +                && iinfo->ii_higen == h_inode->i_generation);
16745 +}
16746 +
16747 +static inline void au_iigen_dec(struct inode *inode)
16748 +{
16749 +       struct au_iinfo *iinfo;
16750 +       struct au_iigen *iigen;
16751 +
16752 +       iinfo = au_ii(inode);
16753 +       iigen = &iinfo->ii_generation;
16754 +       spin_lock(&iigen->ig_spin);
16755 +       iigen->ig_generation--;
16756 +       spin_unlock(&iigen->ig_spin);
16757 +}
16758 +
16759 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
16760 +{
16761 +       int err;
16762 +
16763 +       err = 0;
16764 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
16765 +               err = -EIO;
16766 +
16767 +       return err;
16768 +}
16769 +
16770 +/* ---------------------------------------------------------------------- */
16771 +
16772 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
16773 +                                       aufs_bindex_t bindex)
16774 +{
16775 +       IiMustAnyLock(inode);
16776 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
16777 +}
16778 +
16779 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
16780 +{
16781 +       IiMustAnyLock(inode);
16782 +       return au_ii(inode)->ii_bstart;
16783 +}
16784 +
16785 +static inline aufs_bindex_t au_ibend(struct inode *inode)
16786 +{
16787 +       IiMustAnyLock(inode);
16788 +       return au_ii(inode)->ii_bend;
16789 +}
16790 +
16791 +static inline struct au_vdir *au_ivdir(struct inode *inode)
16792 +{
16793 +       IiMustAnyLock(inode);
16794 +       return au_ii(inode)->ii_vdir;
16795 +}
16796 +
16797 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
16798 +{
16799 +       IiMustAnyLock(inode);
16800 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
16801 +}
16802 +
16803 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
16804 +{
16805 +       IiMustWriteLock(inode);
16806 +       au_ii(inode)->ii_bstart = bindex;
16807 +}
16808 +
16809 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
16810 +{
16811 +       IiMustWriteLock(inode);
16812 +       au_ii(inode)->ii_bend = bindex;
16813 +}
16814 +
16815 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
16816 +{
16817 +       IiMustWriteLock(inode);
16818 +       au_ii(inode)->ii_vdir = vdir;
16819 +}
16820 +
16821 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
16822 +{
16823 +       IiMustAnyLock(inode);
16824 +       return au_ii(inode)->ii_hinode + bindex;
16825 +}
16826 +
16827 +/* ---------------------------------------------------------------------- */
16828 +
16829 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
16830 +{
16831 +       if (pin)
16832 +               return pin->parent;
16833 +       return NULL;
16834 +}
16835 +
16836 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
16837 +{
16838 +       if (pin && pin->hdir)
16839 +               return pin->hdir->hi_inode;
16840 +       return NULL;
16841 +}
16842 +
16843 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16844 +{
16845 +       if (pin)
16846 +               return pin->hdir;
16847 +       return NULL;
16848 +}
16849 +
16850 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16851 +{
16852 +       if (pin)
16853 +               pin->dentry = dentry;
16854 +}
16855 +
16856 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16857 +                                          unsigned char lflag)
16858 +{
16859 +       if (pin) {
16860 +               if (lflag)
16861 +                       au_fset_pin(pin->flags, DI_LOCKED);
16862 +               else
16863 +                       au_fclr_pin(pin->flags, DI_LOCKED);
16864 +       }
16865 +}
16866 +
16867 +#if 0 /* reserved */
16868 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16869 +{
16870 +       if (pin) {
16871 +               dput(pin->parent);
16872 +               pin->parent = dget(parent);
16873 +       }
16874 +}
16875 +#endif
16876 +
16877 +/* ---------------------------------------------------------------------- */
16878 +
16879 +struct au_branch;
16880 +#ifdef CONFIG_AUFS_HNOTIFY
16881 +struct au_hnotify_op {
16882 +       void (*ctl)(struct au_hinode *hinode, int do_set);
16883 +       int (*alloc)(struct au_hinode *hinode);
16884 +
16885 +       /*
16886 +        * if it returns true, the the caller should free hinode->hi_notify,
16887 +        * otherwise ->free() frees it.
16888 +        */
16889 +       int (*free)(struct au_hinode *hinode,
16890 +                   struct au_hnotify *hn) __must_check;
16891 +
16892 +       void (*fin)(void);
16893 +       int (*init)(void);
16894 +
16895 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
16896 +       void (*fin_br)(struct au_branch *br);
16897 +       int (*init_br)(struct au_branch *br, int perm);
16898 +};
16899 +
16900 +/* hnotify.c */
16901 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
16902 +void au_hn_free(struct au_hinode *hinode);
16903 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
16904 +void au_hn_reset(struct inode *inode, unsigned int flags);
16905 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
16906 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
16907 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
16908 +int au_hnotify_init_br(struct au_branch *br, int perm);
16909 +void au_hnotify_fin_br(struct au_branch *br);
16910 +int __init au_hnotify_init(void);
16911 +void au_hnotify_fin(void);
16912 +
16913 +/* hfsnotify.c */
16914 +extern const struct au_hnotify_op au_hnotify_op;
16915 +
16916 +static inline
16917 +void au_hn_init(struct au_hinode *hinode)
16918 +{
16919 +       hinode->hi_notify = NULL;
16920 +}
16921 +
16922 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
16923 +{
16924 +       return hinode->hi_notify;
16925 +}
16926 +
16927 +#else
16928 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
16929 +       struct au_hinode *hinode __maybe_unused,
16930 +       struct inode *inode __maybe_unused)
16931 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
16932 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
16933 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
16934 +          int do_set __maybe_unused)
16935 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
16936 +          unsigned int flags __maybe_unused)
16937 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
16938 +          struct au_branch *br __maybe_unused,
16939 +          int perm __maybe_unused)
16940 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
16941 +          int perm __maybe_unused)
16942 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
16943 +AuStubInt0(__init au_hnotify_init, void)
16944 +AuStubVoid(au_hnotify_fin, void)
16945 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
16946 +#endif /* CONFIG_AUFS_HNOTIFY */
16947 +
16948 +static inline void au_hn_suspend(struct au_hinode *hdir)
16949 +{
16950 +       au_hn_ctl(hdir, /*do_set*/0);
16951 +}
16952 +
16953 +static inline void au_hn_resume(struct au_hinode *hdir)
16954 +{
16955 +       au_hn_ctl(hdir, /*do_set*/1);
16956 +}
16957 +
16958 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
16959 +{
16960 +       mutex_lock(&hdir->hi_inode->i_mutex);
16961 +       au_hn_suspend(hdir);
16962 +}
16963 +
16964 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
16965 +                                         unsigned int sc __maybe_unused)
16966 +{
16967 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16968 +       au_hn_suspend(hdir);
16969 +}
16970 +
16971 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
16972 +{
16973 +       au_hn_resume(hdir);
16974 +       mutex_unlock(&hdir->hi_inode->i_mutex);
16975 +}
16976 +
16977 +#endif /* __KERNEL__ */
16978 +#endif /* __AUFS_INODE_H__ */
16979 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
16980 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
16981 +++ linux/fs/aufs/ioctl.c       2016-01-13 20:11:11.669760262 +0100
16982 @@ -0,0 +1,219 @@
16983 +/*
16984 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16985 + *
16986 + * This program, aufs is free software; you can redistribute it and/or modify
16987 + * it under the terms of the GNU General Public License as published by
16988 + * the Free Software Foundation; either version 2 of the License, or
16989 + * (at your option) any later version.
16990 + *
16991 + * This program is distributed in the hope that it will be useful,
16992 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16993 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16994 + * GNU General Public License for more details.
16995 + *
16996 + * You should have received a copy of the GNU General Public License
16997 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16998 + */
16999 +
17000 +/*
17001 + * ioctl
17002 + * plink-management and readdir in userspace.
17003 + * assist the pathconf(3) wrapper library.
17004 + * move-down
17005 + * File-based Hierarchical Storage Management.
17006 + */
17007 +
17008 +#include <linux/compat.h>
17009 +#include <linux/file.h>
17010 +#include "aufs.h"
17011 +
17012 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
17013 +{
17014 +       int err, fd;
17015 +       aufs_bindex_t wbi, bindex, bend;
17016 +       struct file *h_file;
17017 +       struct super_block *sb;
17018 +       struct dentry *root;
17019 +       struct au_branch *br;
17020 +       struct aufs_wbr_fd wbrfd = {
17021 +               .oflags = au_dir_roflags,
17022 +               .brid   = -1
17023 +       };
17024 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17025 +               | O_NOATIME | O_CLOEXEC;
17026 +
17027 +       AuDebugOn(wbrfd.oflags & ~valid);
17028 +
17029 +       if (arg) {
17030 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17031 +               if (unlikely(err)) {
17032 +                       err = -EFAULT;
17033 +                       goto out;
17034 +               }
17035 +
17036 +               err = -EINVAL;
17037 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17038 +               wbrfd.oflags |= au_dir_roflags;
17039 +               AuDbg("0%o\n", wbrfd.oflags);
17040 +               if (unlikely(wbrfd.oflags & ~valid))
17041 +                       goto out;
17042 +       }
17043 +
17044 +       fd = get_unused_fd_flags(0);
17045 +       err = fd;
17046 +       if (unlikely(fd < 0))
17047 +               goto out;
17048 +
17049 +       h_file = ERR_PTR(-EINVAL);
17050 +       wbi = 0;
17051 +       br = NULL;
17052 +       sb = path->dentry->d_sb;
17053 +       root = sb->s_root;
17054 +       aufs_read_lock(root, AuLock_IR);
17055 +       bend = au_sbend(sb);
17056 +       if (wbrfd.brid >= 0) {
17057 +               wbi = au_br_index(sb, wbrfd.brid);
17058 +               if (unlikely(wbi < 0 || wbi > bend))
17059 +                       goto out_unlock;
17060 +       }
17061 +
17062 +       h_file = ERR_PTR(-ENOENT);
17063 +       br = au_sbr(sb, wbi);
17064 +       if (!au_br_writable(br->br_perm)) {
17065 +               if (arg)
17066 +                       goto out_unlock;
17067 +
17068 +               bindex = wbi + 1;
17069 +               wbi = -1;
17070 +               for (; bindex <= bend; bindex++) {
17071 +                       br = au_sbr(sb, bindex);
17072 +                       if (au_br_writable(br->br_perm)) {
17073 +                               wbi = bindex;
17074 +                               br = au_sbr(sb, wbi);
17075 +                               break;
17076 +                       }
17077 +               }
17078 +       }
17079 +       AuDbg("wbi %d\n", wbi);
17080 +       if (wbi >= 0)
17081 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17082 +                                  /*force_wr*/0);
17083 +
17084 +out_unlock:
17085 +       aufs_read_unlock(root, AuLock_IR);
17086 +       err = PTR_ERR(h_file);
17087 +       if (IS_ERR(h_file))
17088 +               goto out_fd;
17089 +
17090 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
17091 +       fd_install(fd, h_file);
17092 +       err = fd;
17093 +       goto out; /* success */
17094 +
17095 +out_fd:
17096 +       put_unused_fd(fd);
17097 +out:
17098 +       AuTraceErr(err);
17099 +       return err;
17100 +}
17101 +
17102 +/* ---------------------------------------------------------------------- */
17103 +
17104 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17105 +{
17106 +       long err;
17107 +       struct dentry *dentry;
17108 +
17109 +       switch (cmd) {
17110 +       case AUFS_CTL_RDU:
17111 +       case AUFS_CTL_RDU_INO:
17112 +               err = au_rdu_ioctl(file, cmd, arg);
17113 +               break;
17114 +
17115 +       case AUFS_CTL_WBR_FD:
17116 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17117 +               break;
17118 +
17119 +       case AUFS_CTL_IBUSY:
17120 +               err = au_ibusy_ioctl(file, arg);
17121 +               break;
17122 +
17123 +       case AUFS_CTL_BRINFO:
17124 +               err = au_brinfo_ioctl(file, arg);
17125 +               break;
17126 +
17127 +       case AUFS_CTL_FHSM_FD:
17128 +               dentry = file->f_path.dentry;
17129 +               if (IS_ROOT(dentry))
17130 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17131 +               else
17132 +                       err = -ENOTTY;
17133 +               break;
17134 +
17135 +       default:
17136 +               /* do not call the lower */
17137 +               AuDbg("0x%x\n", cmd);
17138 +               err = -ENOTTY;
17139 +       }
17140 +
17141 +       AuTraceErr(err);
17142 +       return err;
17143 +}
17144 +
17145 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17146 +{
17147 +       long err;
17148 +
17149 +       switch (cmd) {
17150 +       case AUFS_CTL_MVDOWN:
17151 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17152 +               break;
17153 +
17154 +       case AUFS_CTL_WBR_FD:
17155 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17156 +               break;
17157 +
17158 +       default:
17159 +               /* do not call the lower */
17160 +               AuDbg("0x%x\n", cmd);
17161 +               err = -ENOTTY;
17162 +       }
17163 +
17164 +       AuTraceErr(err);
17165 +       return err;
17166 +}
17167 +
17168 +#ifdef CONFIG_COMPAT
17169 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17170 +                          unsigned long arg)
17171 +{
17172 +       long err;
17173 +
17174 +       switch (cmd) {
17175 +       case AUFS_CTL_RDU:
17176 +       case AUFS_CTL_RDU_INO:
17177 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17178 +               break;
17179 +
17180 +       case AUFS_CTL_IBUSY:
17181 +               err = au_ibusy_compat_ioctl(file, arg);
17182 +               break;
17183 +
17184 +       case AUFS_CTL_BRINFO:
17185 +               err = au_brinfo_compat_ioctl(file, arg);
17186 +               break;
17187 +
17188 +       default:
17189 +               err = aufs_ioctl_dir(file, cmd, arg);
17190 +       }
17191 +
17192 +       AuTraceErr(err);
17193 +       return err;
17194 +}
17195 +
17196 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17197 +                             unsigned long arg)
17198 +{
17199 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17200 +}
17201 +#endif
17202 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17203 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17204 +++ linux/fs/aufs/i_op_add.c    2016-01-13 20:11:11.669760262 +0100
17205 @@ -0,0 +1,932 @@
17206 +/*
17207 + * Copyright (C) 2005-2015 Junjiro R. Okajima
17208 + *
17209 + * This program, aufs is free software; you can redistribute it and/or modify
17210 + * it under the terms of the GNU General Public License as published by
17211 + * the Free Software Foundation; either version 2 of the License, or
17212 + * (at your option) any later version.
17213 + *
17214 + * This program is distributed in the hope that it will be useful,
17215 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17216 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17217 + * GNU General Public License for more details.
17218 + *
17219 + * You should have received a copy of the GNU General Public License
17220 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17221 + */
17222 +
17223 +/*
17224 + * inode operations (add entry)
17225 + */
17226 +
17227 +#include "aufs.h"
17228 +
17229 +/*
17230 + * final procedure of adding a new entry, except link(2).
17231 + * remove whiteout, instantiate, copyup the parent dir's times and size
17232 + * and update version.
17233 + * if it failed, re-create the removed whiteout.
17234 + */
17235 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17236 +                 struct dentry *wh_dentry, struct dentry *dentry)
17237 +{
17238 +       int err, rerr;
17239 +       aufs_bindex_t bwh;
17240 +       struct path h_path;
17241 +       struct super_block *sb;
17242 +       struct inode *inode, *h_dir;
17243 +       struct dentry *wh;
17244 +
17245 +       bwh = -1;
17246 +       sb = dir->i_sb;
17247 +       if (wh_dentry) {
17248 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
17249 +               IMustLock(h_dir);
17250 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17251 +               bwh = au_dbwh(dentry);
17252 +               h_path.dentry = wh_dentry;
17253 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17254 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17255 +                                         dentry);
17256 +               if (unlikely(err))
17257 +                       goto out;
17258 +       }
17259 +
17260 +       inode = au_new_inode(dentry, /*must_new*/1);
17261 +       if (!IS_ERR(inode)) {
17262 +               d_instantiate(dentry, inode);
17263 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
17264 +               IMustLock(dir);
17265 +               au_dir_ts(dir, bindex);
17266 +               dir->i_version++;
17267 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17268 +               return 0; /* success */
17269 +       }
17270 +
17271 +       err = PTR_ERR(inode);
17272 +       if (!wh_dentry)
17273 +               goto out;
17274 +
17275 +       /* revert */
17276 +       /* dir inode is locked */
17277 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17278 +       rerr = PTR_ERR(wh);
17279 +       if (IS_ERR(wh)) {
17280 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17281 +                       dentry, err, rerr);
17282 +               err = -EIO;
17283 +       } else
17284 +               dput(wh);
17285 +
17286 +out:
17287 +       return err;
17288 +}
17289 +
17290 +static int au_d_may_add(struct dentry *dentry)
17291 +{
17292 +       int err;
17293 +
17294 +       err = 0;
17295 +       if (unlikely(d_unhashed(dentry)))
17296 +               err = -ENOENT;
17297 +       if (unlikely(d_really_is_positive(dentry)))
17298 +               err = -EEXIST;
17299 +       return err;
17300 +}
17301 +
17302 +/*
17303 + * simple tests for the adding inode operations.
17304 + * following the checks in vfs, plus the parent-child relationship.
17305 + */
17306 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17307 +              struct dentry *h_parent, int isdir)
17308 +{
17309 +       int err;
17310 +       umode_t h_mode;
17311 +       struct dentry *h_dentry;
17312 +       struct inode *h_inode;
17313 +
17314 +       err = -ENAMETOOLONG;
17315 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17316 +               goto out;
17317 +
17318 +       h_dentry = au_h_dptr(dentry, bindex);
17319 +       if (d_really_is_negative(dentry)) {
17320 +               err = -EEXIST;
17321 +               if (unlikely(d_is_positive(h_dentry)))
17322 +                       goto out;
17323 +       } else {
17324 +               /* rename(2) case */
17325 +               err = -EIO;
17326 +               if (unlikely(d_is_negative(h_dentry)))
17327 +                       goto out;
17328 +               h_inode = d_inode(h_dentry);
17329 +               if (unlikely(!h_inode->i_nlink))
17330 +                       goto out;
17331 +
17332 +               h_mode = h_inode->i_mode;
17333 +               if (!isdir) {
17334 +                       err = -EISDIR;
17335 +                       if (unlikely(S_ISDIR(h_mode)))
17336 +                               goto out;
17337 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17338 +                       err = -ENOTDIR;
17339 +                       goto out;
17340 +               }
17341 +       }
17342 +
17343 +       err = 0;
17344 +       /* expected parent dir is locked */
17345 +       if (unlikely(h_parent != h_dentry->d_parent))
17346 +               err = -EIO;
17347 +
17348 +out:
17349 +       AuTraceErr(err);
17350 +       return err;
17351 +}
17352 +
17353 +/*
17354 + * initial procedure of adding a new entry.
17355 + * prepare writable branch and the parent dir, lock it,
17356 + * and lookup whiteout for the new entry.
17357 + */
17358 +static struct dentry*
17359 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17360 +                 struct dentry *src_dentry, struct au_pin *pin,
17361 +                 struct au_wr_dir_args *wr_dir_args)
17362 +{
17363 +       struct dentry *wh_dentry, *h_parent;
17364 +       struct super_block *sb;
17365 +       struct au_branch *br;
17366 +       int err;
17367 +       unsigned int udba;
17368 +       aufs_bindex_t bcpup;
17369 +
17370 +       AuDbg("%pd\n", dentry);
17371 +
17372 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17373 +       bcpup = err;
17374 +       wh_dentry = ERR_PTR(err);
17375 +       if (unlikely(err < 0))
17376 +               goto out;
17377 +
17378 +       sb = dentry->d_sb;
17379 +       udba = au_opt_udba(sb);
17380 +       err = au_pin(pin, dentry, bcpup, udba,
17381 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17382 +       wh_dentry = ERR_PTR(err);
17383 +       if (unlikely(err))
17384 +               goto out;
17385 +
17386 +       h_parent = au_pinned_h_parent(pin);
17387 +       if (udba != AuOpt_UDBA_NONE
17388 +           && au_dbstart(dentry) == bcpup)
17389 +               err = au_may_add(dentry, bcpup, h_parent,
17390 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17391 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17392 +               err = -ENAMETOOLONG;
17393 +       wh_dentry = ERR_PTR(err);
17394 +       if (unlikely(err))
17395 +               goto out_unpin;
17396 +
17397 +       br = au_sbr(sb, bcpup);
17398 +       if (dt) {
17399 +               struct path tmp = {
17400 +                       .dentry = h_parent,
17401 +                       .mnt    = au_br_mnt(br)
17402 +               };
17403 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17404 +       }
17405 +
17406 +       wh_dentry = NULL;
17407 +       if (bcpup != au_dbwh(dentry))
17408 +               goto out; /* success */
17409 +
17410 +       /*
17411 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17412 +        * would not be able to removed in the future. So we don't allow such
17413 +        * name here and we don't handle ENAMETOOLONG differently here.
17414 +        */
17415 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17416 +
17417 +out_unpin:
17418 +       if (IS_ERR(wh_dentry))
17419 +               au_unpin(pin);
17420 +out:
17421 +       return wh_dentry;
17422 +}
17423 +
17424 +/* ---------------------------------------------------------------------- */
17425 +
17426 +enum { Mknod, Symlink, Creat };
17427 +struct simple_arg {
17428 +       int type;
17429 +       union {
17430 +               struct {
17431 +                       umode_t                 mode;
17432 +                       bool                    want_excl;
17433 +                       bool                    try_aopen;
17434 +                       struct vfsub_aopen_args *aopen;
17435 +               } c;
17436 +               struct {
17437 +                       const char *symname;
17438 +               } s;
17439 +               struct {
17440 +                       umode_t mode;
17441 +                       dev_t dev;
17442 +               } m;
17443 +       } u;
17444 +};
17445 +
17446 +static int add_simple(struct inode *dir, struct dentry *dentry,
17447 +                     struct simple_arg *arg)
17448 +{
17449 +       int err, rerr;
17450 +       aufs_bindex_t bstart;
17451 +       unsigned char created;
17452 +       const unsigned char try_aopen
17453 +               = (arg->type == Creat && arg->u.c.try_aopen);
17454 +       struct dentry *wh_dentry, *parent;
17455 +       struct inode *h_dir;
17456 +       struct super_block *sb;
17457 +       struct au_branch *br;
17458 +       /* to reuduce stack size */
17459 +       struct {
17460 +               struct au_dtime dt;
17461 +               struct au_pin pin;
17462 +               struct path h_path;
17463 +               struct au_wr_dir_args wr_dir_args;
17464 +       } *a;
17465 +
17466 +       AuDbg("%pd\n", dentry);
17467 +       IMustLock(dir);
17468 +
17469 +       err = -ENOMEM;
17470 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17471 +       if (unlikely(!a))
17472 +               goto out;
17473 +       a->wr_dir_args.force_btgt = -1;
17474 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17475 +
17476 +       parent = dentry->d_parent; /* dir inode is locked */
17477 +       if (!try_aopen) {
17478 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17479 +               if (unlikely(err))
17480 +                       goto out_free;
17481 +       }
17482 +       err = au_d_may_add(dentry);
17483 +       if (unlikely(err))
17484 +               goto out_unlock;
17485 +       if (!try_aopen)
17486 +               di_write_lock_parent(parent);
17487 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17488 +                                     &a->pin, &a->wr_dir_args);
17489 +       err = PTR_ERR(wh_dentry);
17490 +       if (IS_ERR(wh_dentry))
17491 +               goto out_parent;
17492 +
17493 +       bstart = au_dbstart(dentry);
17494 +       sb = dentry->d_sb;
17495 +       br = au_sbr(sb, bstart);
17496 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17497 +       a->h_path.mnt = au_br_mnt(br);
17498 +       h_dir = au_pinned_h_dir(&a->pin);
17499 +       switch (arg->type) {
17500 +       case Creat:
17501 +               err = 0;
17502 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17503 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17504 +                                          arg->u.c.want_excl);
17505 +               else
17506 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17507 +                                               arg->u.c.aopen, br);
17508 +               break;
17509 +       case Symlink:
17510 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17511 +               break;
17512 +       case Mknod:
17513 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17514 +                                 arg->u.m.dev);
17515 +               break;
17516 +       default:
17517 +               BUG();
17518 +       }
17519 +       created = !err;
17520 +       if (!err)
17521 +               err = epilog(dir, bstart, wh_dentry, dentry);
17522 +
17523 +       /* revert */
17524 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
17525 +               /* no delegation since it is just created */
17526 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17527 +                                   /*force*/0);
17528 +               if (rerr) {
17529 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17530 +                               dentry, err, rerr);
17531 +                       err = -EIO;
17532 +               }
17533 +               au_dtime_revert(&a->dt);
17534 +       }
17535 +
17536 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
17537 +               *arg->u.c.aopen->opened |= FILE_CREATED;
17538 +
17539 +       au_unpin(&a->pin);
17540 +       dput(wh_dentry);
17541 +
17542 +out_parent:
17543 +       if (!try_aopen)
17544 +               di_write_unlock(parent);
17545 +out_unlock:
17546 +       if (unlikely(err)) {
17547 +               au_update_dbstart(dentry);
17548 +               d_drop(dentry);
17549 +       }
17550 +       if (!try_aopen)
17551 +               aufs_read_unlock(dentry, AuLock_DW);
17552 +out_free:
17553 +       kfree(a);
17554 +out:
17555 +       return err;
17556 +}
17557 +
17558 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17559 +              dev_t dev)
17560 +{
17561 +       struct simple_arg arg = {
17562 +               .type = Mknod,
17563 +               .u.m = {
17564 +                       .mode   = mode,
17565 +                       .dev    = dev
17566 +               }
17567 +       };
17568 +       return add_simple(dir, dentry, &arg);
17569 +}
17570 +
17571 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17572 +{
17573 +       struct simple_arg arg = {
17574 +               .type = Symlink,
17575 +               .u.s.symname = symname
17576 +       };
17577 +       return add_simple(dir, dentry, &arg);
17578 +}
17579 +
17580 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17581 +               bool want_excl)
17582 +{
17583 +       struct simple_arg arg = {
17584 +               .type = Creat,
17585 +               .u.c = {
17586 +                       .mode           = mode,
17587 +                       .want_excl      = want_excl
17588 +               }
17589 +       };
17590 +       return add_simple(dir, dentry, &arg);
17591 +}
17592 +
17593 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17594 +                      struct vfsub_aopen_args *aopen_args)
17595 +{
17596 +       struct simple_arg arg = {
17597 +               .type = Creat,
17598 +               .u.c = {
17599 +                       .mode           = aopen_args->create_mode,
17600 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
17601 +                       .try_aopen      = true,
17602 +                       .aopen          = aopen_args
17603 +               }
17604 +       };
17605 +       return add_simple(dir, dentry, &arg);
17606 +}
17607 +
17608 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17609 +{
17610 +       int err;
17611 +       aufs_bindex_t bindex;
17612 +       struct super_block *sb;
17613 +       struct dentry *parent, *h_parent, *h_dentry;
17614 +       struct inode *h_dir, *inode;
17615 +       struct vfsmount *h_mnt;
17616 +       struct au_wr_dir_args wr_dir_args = {
17617 +               .force_btgt     = -1,
17618 +               .flags          = AuWrDir_TMPFILE
17619 +       };
17620 +
17621 +       /* copy-up may happen */
17622 +       mutex_lock(&dir->i_mutex);
17623 +
17624 +       sb = dir->i_sb;
17625 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17626 +       if (unlikely(err))
17627 +               goto out;
17628 +
17629 +       err = au_di_init(dentry);
17630 +       if (unlikely(err))
17631 +               goto out_si;
17632 +
17633 +       err = -EBUSY;
17634 +       parent = d_find_any_alias(dir);
17635 +       AuDebugOn(!parent);
17636 +       di_write_lock_parent(parent);
17637 +       if (unlikely(d_inode(parent) != dir))
17638 +               goto out_parent;
17639 +
17640 +       err = au_digen_test(parent, au_sigen(sb));
17641 +       if (unlikely(err))
17642 +               goto out_parent;
17643 +
17644 +       bindex = au_dbstart(parent);
17645 +       au_set_dbstart(dentry, bindex);
17646 +       au_set_dbend(dentry, bindex);
17647 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17648 +       bindex = err;
17649 +       if (unlikely(err < 0))
17650 +               goto out_parent;
17651 +
17652 +       err = -EOPNOTSUPP;
17653 +       h_dir = au_h_iptr(dir, bindex);
17654 +       if (unlikely(!h_dir->i_op->tmpfile))
17655 +               goto out_parent;
17656 +
17657 +       h_mnt = au_sbr_mnt(sb, bindex);
17658 +       err = vfsub_mnt_want_write(h_mnt);
17659 +       if (unlikely(err))
17660 +               goto out_parent;
17661 +
17662 +       h_parent = au_h_dptr(parent, bindex);
17663 +       err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
17664 +       if (unlikely(err))
17665 +               goto out_mnt;
17666 +
17667 +       err = -ENOMEM;
17668 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17669 +       if (unlikely(!h_dentry))
17670 +               goto out_mnt;
17671 +
17672 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17673 +       if (unlikely(err))
17674 +               goto out_dentry;
17675 +
17676 +       au_set_dbstart(dentry, bindex);
17677 +       au_set_dbend(dentry, bindex);
17678 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
17679 +       inode = au_new_inode(dentry, /*must_new*/1);
17680 +       if (IS_ERR(inode)) {
17681 +               err = PTR_ERR(inode);
17682 +               au_set_h_dptr(dentry, bindex, NULL);
17683 +               au_set_dbstart(dentry, -1);
17684 +               au_set_dbend(dentry, -1);
17685 +       } else {
17686 +               if (!inode->i_nlink)
17687 +                       set_nlink(inode, 1);
17688 +               d_tmpfile(dentry, inode);
17689 +               au_di(dentry)->di_tmpfile = 1;
17690 +
17691 +               /* update without i_mutex */
17692 +               if (au_ibstart(dir) == au_dbstart(dentry))
17693 +                       au_cpup_attr_timesizes(dir);
17694 +       }
17695 +
17696 +out_dentry:
17697 +       dput(h_dentry);
17698 +out_mnt:
17699 +       vfsub_mnt_drop_write(h_mnt);
17700 +out_parent:
17701 +       di_write_unlock(parent);
17702 +       dput(parent);
17703 +       di_write_unlock(dentry);
17704 +       if (!err)
17705 +#if 0
17706 +               /* verbose coding for lock class name */
17707 +               au_rw_class(&au_di(dentry)->di_rwsem,
17708 +                           au_lc_key + AuLcNonDir_DIINFO);
17709 +#else
17710 +               ;
17711 +#endif
17712 +       else {
17713 +               au_di_fin(dentry);
17714 +               dentry->d_fsdata = NULL;
17715 +       }
17716 +out_si:
17717 +       si_read_unlock(sb);
17718 +out:
17719 +       mutex_unlock(&dir->i_mutex);
17720 +       return err;
17721 +}
17722 +
17723 +/* ---------------------------------------------------------------------- */
17724 +
17725 +struct au_link_args {
17726 +       aufs_bindex_t bdst, bsrc;
17727 +       struct au_pin pin;
17728 +       struct path h_path;
17729 +       struct dentry *src_parent, *parent;
17730 +};
17731 +
17732 +static int au_cpup_before_link(struct dentry *src_dentry,
17733 +                              struct au_link_args *a)
17734 +{
17735 +       int err;
17736 +       struct dentry *h_src_dentry;
17737 +       struct au_cp_generic cpg = {
17738 +               .dentry = src_dentry,
17739 +               .bdst   = a->bdst,
17740 +               .bsrc   = a->bsrc,
17741 +               .len    = -1,
17742 +               .pin    = &a->pin,
17743 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
17744 +       };
17745 +
17746 +       di_read_lock_parent(a->src_parent, AuLock_IR);
17747 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
17748 +       if (unlikely(err))
17749 +               goto out;
17750 +
17751 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
17752 +       err = au_pin(&a->pin, src_dentry, a->bdst,
17753 +                    au_opt_udba(src_dentry->d_sb),
17754 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17755 +       if (unlikely(err))
17756 +               goto out;
17757 +
17758 +       err = au_sio_cpup_simple(&cpg);
17759 +       au_unpin(&a->pin);
17760 +
17761 +out:
17762 +       di_read_unlock(a->src_parent, AuLock_IR);
17763 +       return err;
17764 +}
17765 +
17766 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
17767 +                          struct au_link_args *a)
17768 +{
17769 +       int err;
17770 +       unsigned char plink;
17771 +       aufs_bindex_t bend;
17772 +       struct dentry *h_src_dentry;
17773 +       struct inode *h_inode, *inode, *delegated;
17774 +       struct super_block *sb;
17775 +       struct file *h_file;
17776 +
17777 +       plink = 0;
17778 +       h_inode = NULL;
17779 +       sb = src_dentry->d_sb;
17780 +       inode = d_inode(src_dentry);
17781 +       if (au_ibstart(inode) <= a->bdst)
17782 +               h_inode = au_h_iptr(inode, a->bdst);
17783 +       if (!h_inode || !h_inode->i_nlink) {
17784 +               /* copyup src_dentry as the name of dentry. */
17785 +               bend = au_dbend(dentry);
17786 +               if (bend < a->bsrc)
17787 +                       au_set_dbend(dentry, a->bsrc);
17788 +               au_set_h_dptr(dentry, a->bsrc,
17789 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
17790 +               dget(a->h_path.dentry);
17791 +               au_set_h_dptr(dentry, a->bdst, NULL);
17792 +               AuDbg("temporary d_inode...\n");
17793 +               spin_lock(&dentry->d_lock);
17794 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
17795 +               spin_unlock(&dentry->d_lock);
17796 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
17797 +               if (IS_ERR(h_file))
17798 +                       err = PTR_ERR(h_file);
17799 +               else {
17800 +                       struct au_cp_generic cpg = {
17801 +                               .dentry = dentry,
17802 +                               .bdst   = a->bdst,
17803 +                               .bsrc   = -1,
17804 +                               .len    = -1,
17805 +                               .pin    = &a->pin,
17806 +                               .flags  = AuCpup_KEEPLINO
17807 +                       };
17808 +                       err = au_sio_cpup_simple(&cpg);
17809 +                       au_h_open_post(dentry, a->bsrc, h_file);
17810 +                       if (!err) {
17811 +                               dput(a->h_path.dentry);
17812 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17813 +                       } else
17814 +                               au_set_h_dptr(dentry, a->bdst,
17815 +                                             a->h_path.dentry);
17816 +               }
17817 +               spin_lock(&dentry->d_lock);
17818 +               dentry->d_inode = NULL; /* restore */
17819 +               spin_unlock(&dentry->d_lock);
17820 +               AuDbg("temporary d_inode...done\n");
17821 +               au_set_h_dptr(dentry, a->bsrc, NULL);
17822 +               au_set_dbend(dentry, bend);
17823 +       } else {
17824 +               /* the inode of src_dentry already exists on a.bdst branch */
17825 +               h_src_dentry = d_find_alias(h_inode);
17826 +               if (!h_src_dentry && au_plink_test(inode)) {
17827 +                       plink = 1;
17828 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
17829 +                       err = PTR_ERR(h_src_dentry);
17830 +                       if (IS_ERR(h_src_dentry))
17831 +                               goto out;
17832 +
17833 +                       if (unlikely(d_is_negative(h_src_dentry))) {
17834 +                               dput(h_src_dentry);
17835 +                               h_src_dentry = NULL;
17836 +                       }
17837 +
17838 +               }
17839 +               if (h_src_dentry) {
17840 +                       delegated = NULL;
17841 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17842 +                                        &a->h_path, &delegated);
17843 +                       if (unlikely(err == -EWOULDBLOCK)) {
17844 +                               pr_warn("cannot retry for NFSv4 delegation"
17845 +                                       " for an internal link\n");
17846 +                               iput(delegated);
17847 +                       }
17848 +                       dput(h_src_dentry);
17849 +               } else {
17850 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
17851 +                               h_inode->i_ino, a->bdst);
17852 +                       err = -EIO;
17853 +               }
17854 +       }
17855 +
17856 +       if (!err && !plink)
17857 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
17858 +
17859 +out:
17860 +       AuTraceErr(err);
17861 +       return err;
17862 +}
17863 +
17864 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
17865 +             struct dentry *dentry)
17866 +{
17867 +       int err, rerr;
17868 +       struct au_dtime dt;
17869 +       struct au_link_args *a;
17870 +       struct dentry *wh_dentry, *h_src_dentry;
17871 +       struct inode *inode, *delegated;
17872 +       struct super_block *sb;
17873 +       struct au_wr_dir_args wr_dir_args = {
17874 +               /* .force_btgt  = -1, */
17875 +               .flags          = AuWrDir_ADD_ENTRY
17876 +       };
17877 +
17878 +       IMustLock(dir);
17879 +       inode = d_inode(src_dentry);
17880 +       IMustLock(inode);
17881 +
17882 +       err = -ENOMEM;
17883 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17884 +       if (unlikely(!a))
17885 +               goto out;
17886 +
17887 +       a->parent = dentry->d_parent; /* dir inode is locked */
17888 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
17889 +                                       AuLock_NOPLM | AuLock_GEN);
17890 +       if (unlikely(err))
17891 +               goto out_kfree;
17892 +       err = au_d_linkable(src_dentry);
17893 +       if (unlikely(err))
17894 +               goto out_unlock;
17895 +       err = au_d_may_add(dentry);
17896 +       if (unlikely(err))
17897 +               goto out_unlock;
17898 +
17899 +       a->src_parent = dget_parent(src_dentry);
17900 +       wr_dir_args.force_btgt = au_ibstart(inode);
17901 +
17902 +       di_write_lock_parent(a->parent);
17903 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
17904 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
17905 +                                     &wr_dir_args);
17906 +       err = PTR_ERR(wh_dentry);
17907 +       if (IS_ERR(wh_dentry))
17908 +               goto out_parent;
17909 +
17910 +       err = 0;
17911 +       sb = dentry->d_sb;
17912 +       a->bdst = au_dbstart(dentry);
17913 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17914 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
17915 +       a->bsrc = au_ibstart(inode);
17916 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17917 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
17918 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
17919 +       if (!h_src_dentry) {
17920 +               a->bsrc = au_dbstart(src_dentry);
17921 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17922 +               AuDebugOn(!h_src_dentry);
17923 +       } else if (IS_ERR(h_src_dentry)) {
17924 +               err = PTR_ERR(h_src_dentry);
17925 +               goto out_parent;
17926 +       }
17927 +
17928 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
17929 +               if (a->bdst < a->bsrc
17930 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
17931 +                       err = au_cpup_or_link(src_dentry, dentry, a);
17932 +               else {
17933 +                       delegated = NULL;
17934 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17935 +                                        &a->h_path, &delegated);
17936 +                       if (unlikely(err == -EWOULDBLOCK)) {
17937 +                               pr_warn("cannot retry for NFSv4 delegation"
17938 +                                       " for an internal link\n");
17939 +                               iput(delegated);
17940 +                       }
17941 +               }
17942 +               dput(h_src_dentry);
17943 +       } else {
17944 +               /*
17945 +                * copyup src_dentry to the branch we process,
17946 +                * and then link(2) to it.
17947 +                */
17948 +               dput(h_src_dentry);
17949 +               if (a->bdst < a->bsrc
17950 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
17951 +                       au_unpin(&a->pin);
17952 +                       di_write_unlock(a->parent);
17953 +                       err = au_cpup_before_link(src_dentry, a);
17954 +                       di_write_lock_parent(a->parent);
17955 +                       if (!err)
17956 +                               err = au_pin(&a->pin, dentry, a->bdst,
17957 +                                            au_opt_udba(sb),
17958 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17959 +                       if (unlikely(err))
17960 +                               goto out_wh;
17961 +               }
17962 +               if (!err) {
17963 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
17964 +                       err = -ENOENT;
17965 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
17966 +                               delegated = NULL;
17967 +                               err = vfsub_link(h_src_dentry,
17968 +                                                au_pinned_h_dir(&a->pin),
17969 +                                                &a->h_path, &delegated);
17970 +                               if (unlikely(err == -EWOULDBLOCK)) {
17971 +                                       pr_warn("cannot retry"
17972 +                                               " for NFSv4 delegation"
17973 +                                               " for an internal link\n");
17974 +                                       iput(delegated);
17975 +                               }
17976 +                       }
17977 +               }
17978 +       }
17979 +       if (unlikely(err))
17980 +               goto out_unpin;
17981 +
17982 +       if (wh_dentry) {
17983 +               a->h_path.dentry = wh_dentry;
17984 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
17985 +                                         dentry);
17986 +               if (unlikely(err))
17987 +                       goto out_revert;
17988 +       }
17989 +
17990 +       au_dir_ts(dir, a->bdst);
17991 +       dir->i_version++;
17992 +       inc_nlink(inode);
17993 +       inode->i_ctime = dir->i_ctime;
17994 +       d_instantiate(dentry, au_igrab(inode));
17995 +       if (d_unhashed(a->h_path.dentry))
17996 +               /* some filesystem calls d_drop() */
17997 +               d_drop(dentry);
17998 +       /* some filesystems consume an inode even hardlink */
17999 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
18000 +       goto out_unpin; /* success */
18001 +
18002 +out_revert:
18003 +       /* no delegation since it is just created */
18004 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18005 +                           /*delegated*/NULL, /*force*/0);
18006 +       if (unlikely(rerr)) {
18007 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
18008 +               err = -EIO;
18009 +       }
18010 +       au_dtime_revert(&dt);
18011 +out_unpin:
18012 +       au_unpin(&a->pin);
18013 +out_wh:
18014 +       dput(wh_dentry);
18015 +out_parent:
18016 +       di_write_unlock(a->parent);
18017 +       dput(a->src_parent);
18018 +out_unlock:
18019 +       if (unlikely(err)) {
18020 +               au_update_dbstart(dentry);
18021 +               d_drop(dentry);
18022 +       }
18023 +       aufs_read_and_write_unlock2(dentry, src_dentry);
18024 +out_kfree:
18025 +       kfree(a);
18026 +out:
18027 +       AuTraceErr(err);
18028 +       return err;
18029 +}
18030 +
18031 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18032 +{
18033 +       int err, rerr;
18034 +       aufs_bindex_t bindex;
18035 +       unsigned char diropq;
18036 +       struct path h_path;
18037 +       struct dentry *wh_dentry, *parent, *opq_dentry;
18038 +       struct mutex *h_mtx;
18039 +       struct super_block *sb;
18040 +       struct {
18041 +               struct au_pin pin;
18042 +               struct au_dtime dt;
18043 +       } *a; /* reduce the stack usage */
18044 +       struct au_wr_dir_args wr_dir_args = {
18045 +               .force_btgt     = -1,
18046 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18047 +       };
18048 +
18049 +       IMustLock(dir);
18050 +
18051 +       err = -ENOMEM;
18052 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18053 +       if (unlikely(!a))
18054 +               goto out;
18055 +
18056 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18057 +       if (unlikely(err))
18058 +               goto out_free;
18059 +       err = au_d_may_add(dentry);
18060 +       if (unlikely(err))
18061 +               goto out_unlock;
18062 +
18063 +       parent = dentry->d_parent; /* dir inode is locked */
18064 +       di_write_lock_parent(parent);
18065 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18066 +                                     &a->pin, &wr_dir_args);
18067 +       err = PTR_ERR(wh_dentry);
18068 +       if (IS_ERR(wh_dentry))
18069 +               goto out_parent;
18070 +
18071 +       sb = dentry->d_sb;
18072 +       bindex = au_dbstart(dentry);
18073 +       h_path.dentry = au_h_dptr(dentry, bindex);
18074 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18075 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18076 +       if (unlikely(err))
18077 +               goto out_unpin;
18078 +
18079 +       /* make the dir opaque */
18080 +       diropq = 0;
18081 +       h_mtx = &d_inode(h_path.dentry)->i_mutex;
18082 +       if (wh_dentry
18083 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18084 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18085 +               opq_dentry = au_diropq_create(dentry, bindex);
18086 +               mutex_unlock(h_mtx);
18087 +               err = PTR_ERR(opq_dentry);
18088 +               if (IS_ERR(opq_dentry))
18089 +                       goto out_dir;
18090 +               dput(opq_dentry);
18091 +               diropq = 1;
18092 +       }
18093 +
18094 +       err = epilog(dir, bindex, wh_dentry, dentry);
18095 +       if (!err) {
18096 +               inc_nlink(dir);
18097 +               goto out_unpin; /* success */
18098 +       }
18099 +
18100 +       /* revert */
18101 +       if (diropq) {
18102 +               AuLabel(revert opq);
18103 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18104 +               rerr = au_diropq_remove(dentry, bindex);
18105 +               mutex_unlock(h_mtx);
18106 +               if (rerr) {
18107 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18108 +                               dentry, err, rerr);
18109 +                       err = -EIO;
18110 +               }
18111 +       }
18112 +
18113 +out_dir:
18114 +       AuLabel(revert dir);
18115 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18116 +       if (rerr) {
18117 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18118 +                       dentry, err, rerr);
18119 +               err = -EIO;
18120 +       }
18121 +       au_dtime_revert(&a->dt);
18122 +out_unpin:
18123 +       au_unpin(&a->pin);
18124 +       dput(wh_dentry);
18125 +out_parent:
18126 +       di_write_unlock(parent);
18127 +out_unlock:
18128 +       if (unlikely(err)) {
18129 +               au_update_dbstart(dentry);
18130 +               d_drop(dentry);
18131 +       }
18132 +       aufs_read_unlock(dentry, AuLock_DW);
18133 +out_free:
18134 +       kfree(a);
18135 +out:
18136 +       return err;
18137 +}
18138 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18139 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18140 +++ linux/fs/aufs/i_op.c        2016-01-13 20:11:11.669760262 +0100
18141 @@ -0,0 +1,1484 @@
18142 +/*
18143 + * Copyright (C) 2005-2015 Junjiro R. Okajima
18144 + *
18145 + * This program, aufs is free software; you can redistribute it and/or modify
18146 + * it under the terms of the GNU General Public License as published by
18147 + * the Free Software Foundation; either version 2 of the License, or
18148 + * (at your option) any later version.
18149 + *
18150 + * This program is distributed in the hope that it will be useful,
18151 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18152 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18153 + * GNU General Public License for more details.
18154 + *
18155 + * You should have received a copy of the GNU General Public License
18156 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18157 + */
18158 +
18159 +/*
18160 + * inode operations (except add/del/rename)
18161 + */
18162 +
18163 +#include <linux/device_cgroup.h>
18164 +#include <linux/fs_stack.h>
18165 +#include <linux/namei.h>
18166 +#include <linux/security.h>
18167 +#include "aufs.h"
18168 +
18169 +static int h_permission(struct inode *h_inode, int mask,
18170 +                       struct path *h_path, int brperm)
18171 +{
18172 +       int err;
18173 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18174 +
18175 +       err = -EACCES;
18176 +       if ((write_mask && IS_IMMUTABLE(h_inode))
18177 +           || ((mask & MAY_EXEC)
18178 +               && S_ISREG(h_inode->i_mode)
18179 +               && (path_noexec(h_path)
18180 +                   || !(h_inode->i_mode & S_IXUGO))))
18181 +               goto out;
18182 +
18183 +       /*
18184 +        * - skip the lower fs test in the case of write to ro branch.
18185 +        * - nfs dir permission write check is optimized, but a policy for
18186 +        *   link/rename requires a real check.
18187 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18188 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18189 +        */
18190 +       if ((write_mask && !au_br_writable(brperm))
18191 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18192 +               && write_mask && !(mask & MAY_READ))
18193 +           || !h_inode->i_op->permission) {
18194 +               /* AuLabel(generic_permission); */
18195 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18196 +               err = generic_permission(h_inode, mask);
18197 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18198 +                       err = h_inode->i_op->permission(h_inode, mask);
18199 +               AuTraceErr(err);
18200 +       } else {
18201 +               /* AuLabel(h_inode->permission); */
18202 +               err = h_inode->i_op->permission(h_inode, mask);
18203 +               AuTraceErr(err);
18204 +       }
18205 +
18206 +       if (!err)
18207 +               err = devcgroup_inode_permission(h_inode, mask);
18208 +       if (!err)
18209 +               err = security_inode_permission(h_inode, mask);
18210 +
18211 +#if 0
18212 +       if (!err) {
18213 +               /* todo: do we need to call ima_path_check()? */
18214 +               struct path h_path = {
18215 +                       .dentry =
18216 +                       .mnt    = h_mnt
18217 +               };
18218 +               err = ima_path_check(&h_path,
18219 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18220 +                                    IMA_COUNT_LEAVE);
18221 +       }
18222 +#endif
18223 +
18224 +out:
18225 +       return err;
18226 +}
18227 +
18228 +static int aufs_permission(struct inode *inode, int mask)
18229 +{
18230 +       int err;
18231 +       aufs_bindex_t bindex, bend;
18232 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18233 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18234 +       struct inode *h_inode;
18235 +       struct super_block *sb;
18236 +       struct au_branch *br;
18237 +
18238 +       /* todo: support rcu-walk? */
18239 +       if (mask & MAY_NOT_BLOCK)
18240 +               return -ECHILD;
18241 +
18242 +       sb = inode->i_sb;
18243 +       si_read_lock(sb, AuLock_FLUSH);
18244 +       ii_read_lock_child(inode);
18245 +#if 0
18246 +       err = au_iigen_test(inode, au_sigen(sb));
18247 +       if (unlikely(err))
18248 +               goto out;
18249 +#endif
18250 +
18251 +       if (!isdir
18252 +           || write_mask
18253 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18254 +               err = au_busy_or_stale();
18255 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
18256 +               if (unlikely(!h_inode
18257 +                            || (h_inode->i_mode & S_IFMT)
18258 +                            != (inode->i_mode & S_IFMT)))
18259 +                       goto out;
18260 +
18261 +               err = 0;
18262 +               bindex = au_ibstart(inode);
18263 +               br = au_sbr(sb, bindex);
18264 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
18265 +               if (write_mask
18266 +                   && !err
18267 +                   && !special_file(h_inode->i_mode)) {
18268 +                       /* test whether the upper writable branch exists */
18269 +                       err = -EROFS;
18270 +                       for (; bindex >= 0; bindex--)
18271 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18272 +                                       err = 0;
18273 +                                       break;
18274 +                               }
18275 +               }
18276 +               goto out;
18277 +       }
18278 +
18279 +       /* non-write to dir */
18280 +       err = 0;
18281 +       bend = au_ibend(inode);
18282 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
18283 +               h_inode = au_h_iptr(inode, bindex);
18284 +               if (h_inode) {
18285 +                       err = au_busy_or_stale();
18286 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18287 +                               break;
18288 +
18289 +                       br = au_sbr(sb, bindex);
18290 +                       err = h_permission(h_inode, mask, &br->br_path,
18291 +                                          br->br_perm);
18292 +               }
18293 +       }
18294 +
18295 +out:
18296 +       ii_read_unlock(inode);
18297 +       si_read_unlock(sb);
18298 +       return err;
18299 +}
18300 +
18301 +/* ---------------------------------------------------------------------- */
18302 +
18303 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18304 +                                 unsigned int flags)
18305 +{
18306 +       struct dentry *ret, *parent;
18307 +       struct inode *inode;
18308 +       struct super_block *sb;
18309 +       int err, npositive;
18310 +
18311 +       IMustLock(dir);
18312 +
18313 +       /* todo: support rcu-walk? */
18314 +       ret = ERR_PTR(-ECHILD);
18315 +       if (flags & LOOKUP_RCU)
18316 +               goto out;
18317 +
18318 +       ret = ERR_PTR(-ENAMETOOLONG);
18319 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18320 +               goto out;
18321 +
18322 +       sb = dir->i_sb;
18323 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18324 +       ret = ERR_PTR(err);
18325 +       if (unlikely(err))
18326 +               goto out;
18327 +
18328 +       err = au_di_init(dentry);
18329 +       ret = ERR_PTR(err);
18330 +       if (unlikely(err))
18331 +               goto out_si;
18332 +
18333 +       inode = NULL;
18334 +       npositive = 0; /* suppress a warning */
18335 +       parent = dentry->d_parent; /* dir inode is locked */
18336 +       di_read_lock_parent(parent, AuLock_IR);
18337 +       err = au_alive_dir(parent);
18338 +       if (!err)
18339 +               err = au_digen_test(parent, au_sigen(sb));
18340 +       if (!err) {
18341 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
18342 +                                          /*type*/0);
18343 +               err = npositive;
18344 +       }
18345 +       di_read_unlock(parent, AuLock_IR);
18346 +       ret = ERR_PTR(err);
18347 +       if (unlikely(err < 0))
18348 +               goto out_unlock;
18349 +
18350 +       if (npositive) {
18351 +               inode = au_new_inode(dentry, /*must_new*/0);
18352 +               if (IS_ERR(inode)) {
18353 +                       ret = (void *)inode;
18354 +                       inode = NULL;
18355 +                       goto out_unlock;
18356 +               }
18357 +       }
18358 +
18359 +       if (inode)
18360 +               atomic_inc(&inode->i_count);
18361 +       ret = d_splice_alias(inode, dentry);
18362 +#if 0
18363 +       if (unlikely(d_need_lookup(dentry))) {
18364 +               spin_lock(&dentry->d_lock);
18365 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18366 +               spin_unlock(&dentry->d_lock);
18367 +       } else
18368 +#endif
18369 +       if (inode) {
18370 +               if (!IS_ERR(ret)) {
18371 +                       iput(inode);
18372 +                       if (ret && ret != dentry)
18373 +                               ii_write_unlock(inode);
18374 +               } else {
18375 +                       ii_write_unlock(inode);
18376 +                       iput(inode);
18377 +                       inode = NULL;
18378 +               }
18379 +       }
18380 +
18381 +out_unlock:
18382 +       di_write_unlock(dentry);
18383 +       if (inode) {
18384 +               /* verbose coding for lock class name */
18385 +               if (unlikely(S_ISLNK(inode->i_mode)))
18386 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18387 +                                   au_lc_key + AuLcSymlink_DIINFO);
18388 +               else if (unlikely(S_ISDIR(inode->i_mode)))
18389 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18390 +                                   au_lc_key + AuLcDir_DIINFO);
18391 +               else /* likely */
18392 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18393 +                                   au_lc_key + AuLcNonDir_DIINFO);
18394 +       }
18395 +out_si:
18396 +       si_read_unlock(sb);
18397 +out:
18398 +       return ret;
18399 +}
18400 +
18401 +/* ---------------------------------------------------------------------- */
18402 +
18403 +struct aopen_node {
18404 +       struct hlist_node hlist;
18405 +       struct file *file, *h_file;
18406 +};
18407 +
18408 +static int au_do_aopen(struct inode *inode, struct file *file)
18409 +{
18410 +       struct au_sphlhead *aopen;
18411 +       struct aopen_node *node;
18412 +       struct au_do_open_args args = {
18413 +               .no_lock        = 1,
18414 +               .open           = au_do_open_nondir
18415 +       };
18416 +
18417 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18418 +       spin_lock(&aopen->spin);
18419 +       hlist_for_each_entry(node, &aopen->head, hlist)
18420 +               if (node->file == file) {
18421 +                       args.h_file = node->h_file;
18422 +                       break;
18423 +               }
18424 +       spin_unlock(&aopen->spin);
18425 +       /* AuDebugOn(!args.h_file); */
18426 +
18427 +       return au_do_open(file, &args);
18428 +}
18429 +
18430 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18431 +                           struct file *file, unsigned int open_flag,
18432 +                           umode_t create_mode, int *opened)
18433 +{
18434 +       int err, h_opened = *opened;
18435 +       struct dentry *parent;
18436 +       struct dentry *d;
18437 +       struct au_sphlhead *aopen;
18438 +       struct vfsub_aopen_args args = {
18439 +               .open_flag      = open_flag,
18440 +               .create_mode    = create_mode,
18441 +               .opened         = &h_opened
18442 +       };
18443 +       struct aopen_node aopen_node = {
18444 +               .file   = file
18445 +       };
18446 +
18447 +       IMustLock(dir);
18448 +       AuDbg("open_flag 0x%x\n", open_flag);
18449 +       AuDbgDentry(dentry);
18450 +
18451 +       err = 0;
18452 +       if (!au_di(dentry)) {
18453 +               d = aufs_lookup(dir, dentry, /*flags*/0);
18454 +               if (IS_ERR(d)) {
18455 +                       err = PTR_ERR(d);
18456 +                       goto out;
18457 +               } else if (d) {
18458 +                       /*
18459 +                        * obsoleted dentry found.
18460 +                        * another error will be returned later.
18461 +                        */
18462 +                       d_drop(d);
18463 +                       dput(d);
18464 +                       AuDbgDentry(d);
18465 +               }
18466 +               AuDbgDentry(dentry);
18467 +       }
18468 +
18469 +       if (d_is_positive(dentry)
18470 +           || d_unhashed(dentry)
18471 +           || d_unlinked(dentry)
18472 +           || !(open_flag & O_CREAT))
18473 +               goto out_no_open;
18474 +
18475 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18476 +       if (unlikely(err))
18477 +               goto out;
18478 +
18479 +       parent = dentry->d_parent;      /* dir is locked */
18480 +       di_write_lock_parent(parent);
18481 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
18482 +       if (unlikely(err))
18483 +               goto out_unlock;
18484 +
18485 +       AuDbgDentry(dentry);
18486 +       if (d_is_positive(dentry))
18487 +               goto out_unlock;
18488 +
18489 +       args.file = get_empty_filp();
18490 +       err = PTR_ERR(args.file);
18491 +       if (IS_ERR(args.file))
18492 +               goto out_unlock;
18493 +
18494 +       args.file->f_flags = file->f_flags;
18495 +       err = au_aopen_or_create(dir, dentry, &args);
18496 +       AuTraceErr(err);
18497 +       AuDbgFile(args.file);
18498 +       if (unlikely(err < 0)) {
18499 +               if (h_opened & FILE_OPENED)
18500 +                       fput(args.file);
18501 +               else
18502 +                       put_filp(args.file);
18503 +               goto out_unlock;
18504 +       }
18505 +
18506 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18507 +       *opened |= FILE_CREATED;
18508 +       if (h_opened & FILE_OPENED)
18509 +               aopen_node.h_file = args.file;
18510 +       else {
18511 +               put_filp(args.file);
18512 +               args.file = NULL;
18513 +       }
18514 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18515 +       au_sphl_add(&aopen_node.hlist, aopen);
18516 +       err = finish_open(file, dentry, au_do_aopen, opened);
18517 +       au_sphl_del(&aopen_node.hlist, aopen);
18518 +       AuTraceErr(err);
18519 +       AuDbgFile(file);
18520 +       if (aopen_node.h_file)
18521 +               fput(aopen_node.h_file);
18522 +
18523 +out_unlock:
18524 +       di_write_unlock(parent);
18525 +       aufs_read_unlock(dentry, AuLock_DW);
18526 +       AuDbgDentry(dentry);
18527 +       if (unlikely(err))
18528 +               goto out;
18529 +out_no_open:
18530 +       if (!err && !(*opened & FILE_CREATED)) {
18531 +               AuLabel(out_no_open);
18532 +               dget(dentry);
18533 +               err = finish_no_open(file, dentry);
18534 +       }
18535 +out:
18536 +       AuDbg("%pd%s%s\n", dentry,
18537 +             (*opened & FILE_CREATED) ? " created" : "",
18538 +             (*opened & FILE_OPENED) ? " opened" : "");
18539 +       AuTraceErr(err);
18540 +       return err;
18541 +}
18542 +
18543 +
18544 +/* ---------------------------------------------------------------------- */
18545 +
18546 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18547 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18548 +                         aufs_bindex_t bstart)
18549 +{
18550 +       int err;
18551 +       struct dentry *h_parent;
18552 +       struct inode *h_dir;
18553 +
18554 +       if (add_entry)
18555 +               IMustLock(d_inode(parent));
18556 +       else
18557 +               di_write_lock_parent(parent);
18558 +
18559 +       err = 0;
18560 +       if (!au_h_dptr(parent, bcpup)) {
18561 +               if (bstart > bcpup)
18562 +                       err = au_cpup_dirs(dentry, bcpup);
18563 +               else if (bstart < bcpup)
18564 +                       err = au_cpdown_dirs(dentry, bcpup);
18565 +               else
18566 +                       BUG();
18567 +       }
18568 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18569 +               h_parent = au_h_dptr(parent, bcpup);
18570 +               h_dir = d_inode(h_parent);
18571 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
18572 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18573 +               /* todo: no unlock here */
18574 +               mutex_unlock(&h_dir->i_mutex);
18575 +
18576 +               AuDbg("bcpup %d\n", bcpup);
18577 +               if (!err) {
18578 +                       if (d_really_is_negative(dentry))
18579 +                               au_set_h_dptr(dentry, bstart, NULL);
18580 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18581 +               }
18582 +       }
18583 +
18584 +       if (!add_entry)
18585 +               di_write_unlock(parent);
18586 +       if (!err)
18587 +               err = bcpup; /* success */
18588 +
18589 +       AuTraceErr(err);
18590 +       return err;
18591 +}
18592 +
18593 +/*
18594 + * decide the branch and the parent dir where we will create a new entry.
18595 + * returns new bindex or an error.
18596 + * copyup the parent dir if needed.
18597 + */
18598 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18599 +             struct au_wr_dir_args *args)
18600 +{
18601 +       int err;
18602 +       unsigned int flags;
18603 +       aufs_bindex_t bcpup, bstart, src_bstart;
18604 +       const unsigned char add_entry
18605 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18606 +               | au_ftest_wrdir(args->flags, TMPFILE);
18607 +       struct super_block *sb;
18608 +       struct dentry *parent;
18609 +       struct au_sbinfo *sbinfo;
18610 +
18611 +       sb = dentry->d_sb;
18612 +       sbinfo = au_sbi(sb);
18613 +       parent = dget_parent(dentry);
18614 +       bstart = au_dbstart(dentry);
18615 +       bcpup = bstart;
18616 +       if (args->force_btgt < 0) {
18617 +               if (src_dentry) {
18618 +                       src_bstart = au_dbstart(src_dentry);
18619 +                       if (src_bstart < bstart)
18620 +                               bcpup = src_bstart;
18621 +               } else if (add_entry) {
18622 +                       flags = 0;
18623 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18624 +                               au_fset_wbr(flags, DIR);
18625 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18626 +                       bcpup = err;
18627 +               }
18628 +
18629 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
18630 +                       if (add_entry)
18631 +                               err = AuWbrCopyup(sbinfo, dentry);
18632 +                       else {
18633 +                               if (!IS_ROOT(dentry)) {
18634 +                                       di_read_lock_parent(parent, !AuLock_IR);
18635 +                                       err = AuWbrCopyup(sbinfo, dentry);
18636 +                                       di_read_unlock(parent, !AuLock_IR);
18637 +                               } else
18638 +                                       err = AuWbrCopyup(sbinfo, dentry);
18639 +                       }
18640 +                       bcpup = err;
18641 +                       if (unlikely(err < 0))
18642 +                               goto out;
18643 +               }
18644 +       } else {
18645 +               bcpup = args->force_btgt;
18646 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
18647 +       }
18648 +
18649 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
18650 +       err = bcpup;
18651 +       if (bcpup == bstart)
18652 +               goto out; /* success */
18653 +
18654 +       /* copyup the new parent into the branch we process */
18655 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
18656 +       if (err >= 0) {
18657 +               if (d_really_is_negative(dentry)) {
18658 +                       au_set_h_dptr(dentry, bstart, NULL);
18659 +                       au_set_dbstart(dentry, bcpup);
18660 +                       au_set_dbend(dentry, bcpup);
18661 +               }
18662 +               AuDebugOn(add_entry
18663 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18664 +                         && !au_h_dptr(dentry, bcpup));
18665 +       }
18666 +
18667 +out:
18668 +       dput(parent);
18669 +       return err;
18670 +}
18671 +
18672 +/* ---------------------------------------------------------------------- */
18673 +
18674 +void au_pin_hdir_unlock(struct au_pin *p)
18675 +{
18676 +       if (p->hdir)
18677 +               au_hn_imtx_unlock(p->hdir);
18678 +}
18679 +
18680 +int au_pin_hdir_lock(struct au_pin *p)
18681 +{
18682 +       int err;
18683 +
18684 +       err = 0;
18685 +       if (!p->hdir)
18686 +               goto out;
18687 +
18688 +       /* even if an error happens later, keep this lock */
18689 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
18690 +
18691 +       err = -EBUSY;
18692 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
18693 +               goto out;
18694 +
18695 +       err = 0;
18696 +       if (p->h_dentry)
18697 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18698 +                                 p->h_parent, p->br);
18699 +
18700 +out:
18701 +       return err;
18702 +}
18703 +
18704 +int au_pin_hdir_relock(struct au_pin *p)
18705 +{
18706 +       int err, i;
18707 +       struct inode *h_i;
18708 +       struct dentry *h_d[] = {
18709 +               p->h_dentry,
18710 +               p->h_parent
18711 +       };
18712 +
18713 +       err = au_pin_hdir_lock(p);
18714 +       if (unlikely(err))
18715 +               goto out;
18716 +
18717 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18718 +               if (!h_d[i])
18719 +                       continue;
18720 +               if (d_is_positive(h_d[i])) {
18721 +                       h_i = d_inode(h_d[i]);
18722 +                       err = !h_i->i_nlink;
18723 +               }
18724 +       }
18725 +
18726 +out:
18727 +       return err;
18728 +}
18729 +
18730 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
18731 +{
18732 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
18733 +       p->hdir->hi_inode->i_mutex.owner = task;
18734 +#endif
18735 +}
18736 +
18737 +void au_pin_hdir_acquire_nest(struct au_pin *p)
18738 +{
18739 +       if (p->hdir) {
18740 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
18741 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
18742 +               au_pin_hdir_set_owner(p, current);
18743 +       }
18744 +}
18745 +
18746 +void au_pin_hdir_release(struct au_pin *p)
18747 +{
18748 +       if (p->hdir) {
18749 +               au_pin_hdir_set_owner(p, p->task);
18750 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
18751 +       }
18752 +}
18753 +
18754 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
18755 +{
18756 +       if (pin && pin->parent)
18757 +               return au_h_dptr(pin->parent, pin->bindex);
18758 +       return NULL;
18759 +}
18760 +
18761 +void au_unpin(struct au_pin *p)
18762 +{
18763 +       if (p->hdir)
18764 +               au_pin_hdir_unlock(p);
18765 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
18766 +               vfsub_mnt_drop_write(p->h_mnt);
18767 +       if (!p->hdir)
18768 +               return;
18769 +
18770 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18771 +               di_read_unlock(p->parent, AuLock_IR);
18772 +       iput(p->hdir->hi_inode);
18773 +       dput(p->parent);
18774 +       p->parent = NULL;
18775 +       p->hdir = NULL;
18776 +       p->h_mnt = NULL;
18777 +       /* do not clear p->task */
18778 +}
18779 +
18780 +int au_do_pin(struct au_pin *p)
18781 +{
18782 +       int err;
18783 +       struct super_block *sb;
18784 +       struct inode *h_dir;
18785 +
18786 +       err = 0;
18787 +       sb = p->dentry->d_sb;
18788 +       p->br = au_sbr(sb, p->bindex);
18789 +       if (IS_ROOT(p->dentry)) {
18790 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
18791 +                       p->h_mnt = au_br_mnt(p->br);
18792 +                       err = vfsub_mnt_want_write(p->h_mnt);
18793 +                       if (unlikely(err)) {
18794 +                               au_fclr_pin(p->flags, MNT_WRITE);
18795 +                               goto out_err;
18796 +                       }
18797 +               }
18798 +               goto out;
18799 +       }
18800 +
18801 +       p->h_dentry = NULL;
18802 +       if (p->bindex <= au_dbend(p->dentry))
18803 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
18804 +
18805 +       p->parent = dget_parent(p->dentry);
18806 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18807 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
18808 +
18809 +       h_dir = NULL;
18810 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
18811 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
18812 +       if (p->hdir)
18813 +               h_dir = p->hdir->hi_inode;
18814 +
18815 +       /*
18816 +        * udba case, or
18817 +        * if DI_LOCKED is not set, then p->parent may be different
18818 +        * and h_parent can be NULL.
18819 +        */
18820 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
18821 +               err = -EBUSY;
18822 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
18823 +                       di_read_unlock(p->parent, AuLock_IR);
18824 +               dput(p->parent);
18825 +               p->parent = NULL;
18826 +               goto out_err;
18827 +       }
18828 +
18829 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
18830 +               p->h_mnt = au_br_mnt(p->br);
18831 +               err = vfsub_mnt_want_write(p->h_mnt);
18832 +               if (unlikely(err)) {
18833 +                       au_fclr_pin(p->flags, MNT_WRITE);
18834 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
18835 +                               di_read_unlock(p->parent, AuLock_IR);
18836 +                       dput(p->parent);
18837 +                       p->parent = NULL;
18838 +                       goto out_err;
18839 +               }
18840 +       }
18841 +
18842 +       au_igrab(h_dir);
18843 +       err = au_pin_hdir_lock(p);
18844 +       if (!err)
18845 +               goto out; /* success */
18846 +
18847 +       au_unpin(p);
18848 +
18849 +out_err:
18850 +       pr_err("err %d\n", err);
18851 +       err = au_busy_or_stale();
18852 +out:
18853 +       return err;
18854 +}
18855 +
18856 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
18857 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18858 +                unsigned int udba, unsigned char flags)
18859 +{
18860 +       p->dentry = dentry;
18861 +       p->udba = udba;
18862 +       p->lsc_di = lsc_di;
18863 +       p->lsc_hi = lsc_hi;
18864 +       p->flags = flags;
18865 +       p->bindex = bindex;
18866 +
18867 +       p->parent = NULL;
18868 +       p->hdir = NULL;
18869 +       p->h_mnt = NULL;
18870 +
18871 +       p->h_dentry = NULL;
18872 +       p->h_parent = NULL;
18873 +       p->br = NULL;
18874 +       p->task = current;
18875 +}
18876 +
18877 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18878 +          unsigned int udba, unsigned char flags)
18879 +{
18880 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
18881 +                   udba, flags);
18882 +       return au_do_pin(pin);
18883 +}
18884 +
18885 +/* ---------------------------------------------------------------------- */
18886 +
18887 +/*
18888 + * ->setattr() and ->getattr() are called in various cases.
18889 + * chmod, stat: dentry is revalidated.
18890 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
18891 + *               unhashed.
18892 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
18893 + */
18894 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
18895 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
18896 +{
18897 +       int err;
18898 +       struct dentry *parent;
18899 +
18900 +       err = 0;
18901 +       if (au_digen_test(dentry, sigen)) {
18902 +               parent = dget_parent(dentry);
18903 +               di_read_lock_parent(parent, AuLock_IR);
18904 +               err = au_refresh_dentry(dentry, parent);
18905 +               di_read_unlock(parent, AuLock_IR);
18906 +               dput(parent);
18907 +       }
18908 +
18909 +       AuTraceErr(err);
18910 +       return err;
18911 +}
18912 +
18913 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18914 +                    struct au_icpup_args *a)
18915 +{
18916 +       int err;
18917 +       loff_t sz;
18918 +       aufs_bindex_t bstart, ibstart;
18919 +       struct dentry *hi_wh, *parent;
18920 +       struct inode *inode;
18921 +       struct au_wr_dir_args wr_dir_args = {
18922 +               .force_btgt     = -1,
18923 +               .flags          = 0
18924 +       };
18925 +
18926 +       if (d_is_dir(dentry))
18927 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18928 +       /* plink or hi_wh() case */
18929 +       bstart = au_dbstart(dentry);
18930 +       inode = d_inode(dentry);
18931 +       ibstart = au_ibstart(inode);
18932 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
18933 +               wr_dir_args.force_btgt = ibstart;
18934 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
18935 +       if (unlikely(err < 0))
18936 +               goto out;
18937 +       a->btgt = err;
18938 +       if (err != bstart)
18939 +               au_fset_icpup(a->flags, DID_CPUP);
18940 +
18941 +       err = 0;
18942 +       a->pin_flags = AuPin_MNT_WRITE;
18943 +       parent = NULL;
18944 +       if (!IS_ROOT(dentry)) {
18945 +               au_fset_pin(a->pin_flags, DI_LOCKED);
18946 +               parent = dget_parent(dentry);
18947 +               di_write_lock_parent(parent);
18948 +       }
18949 +
18950 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
18951 +       if (unlikely(err))
18952 +               goto out_parent;
18953 +
18954 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
18955 +       sz = -1;
18956 +       a->h_inode = d_inode(a->h_path.dentry);
18957 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
18958 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18959 +               if (ia->ia_size < i_size_read(a->h_inode))
18960 +                       sz = ia->ia_size;
18961 +               mutex_unlock(&a->h_inode->i_mutex);
18962 +       }
18963 +
18964 +       hi_wh = NULL;
18965 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
18966 +               hi_wh = au_hi_wh(inode, a->btgt);
18967 +               if (!hi_wh) {
18968 +                       struct au_cp_generic cpg = {
18969 +                               .dentry = dentry,
18970 +                               .bdst   = a->btgt,
18971 +                               .bsrc   = -1,
18972 +                               .len    = sz,
18973 +                               .pin    = &a->pin
18974 +                       };
18975 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
18976 +                       if (unlikely(err))
18977 +                               goto out_unlock;
18978 +                       hi_wh = au_hi_wh(inode, a->btgt);
18979 +                       /* todo: revalidate hi_wh? */
18980 +               }
18981 +       }
18982 +
18983 +       if (parent) {
18984 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
18985 +               di_downgrade_lock(parent, AuLock_IR);
18986 +               dput(parent);
18987 +               parent = NULL;
18988 +       }
18989 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
18990 +               goto out; /* success */
18991 +
18992 +       if (!d_unhashed(dentry)) {
18993 +               struct au_cp_generic cpg = {
18994 +                       .dentry = dentry,
18995 +                       .bdst   = a->btgt,
18996 +                       .bsrc   = bstart,
18997 +                       .len    = sz,
18998 +                       .pin    = &a->pin,
18999 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
19000 +               };
19001 +               err = au_sio_cpup_simple(&cpg);
19002 +               if (!err)
19003 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19004 +       } else if (!hi_wh)
19005 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19006 +       else
19007 +               a->h_path.dentry = hi_wh; /* do not dget here */
19008 +
19009 +out_unlock:
19010 +       a->h_inode = d_inode(a->h_path.dentry);
19011 +       if (!err)
19012 +               goto out; /* success */
19013 +       au_unpin(&a->pin);
19014 +out_parent:
19015 +       if (parent) {
19016 +               di_write_unlock(parent);
19017 +               dput(parent);
19018 +       }
19019 +out:
19020 +       if (!err)
19021 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
19022 +       return err;
19023 +}
19024 +
19025 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
19026 +{
19027 +       int err;
19028 +       struct inode *inode, *delegated;
19029 +       struct super_block *sb;
19030 +       struct file *file;
19031 +       struct au_icpup_args *a;
19032 +
19033 +       inode = d_inode(dentry);
19034 +       IMustLock(inode);
19035 +
19036 +       err = -ENOMEM;
19037 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19038 +       if (unlikely(!a))
19039 +               goto out;
19040 +
19041 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19042 +               ia->ia_valid &= ~ATTR_MODE;
19043 +
19044 +       file = NULL;
19045 +       sb = dentry->d_sb;
19046 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19047 +       if (unlikely(err))
19048 +               goto out_kfree;
19049 +
19050 +       if (ia->ia_valid & ATTR_FILE) {
19051 +               /* currently ftruncate(2) only */
19052 +               AuDebugOn(!d_is_reg(dentry));
19053 +               file = ia->ia_file;
19054 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
19055 +               if (unlikely(err))
19056 +                       goto out_si;
19057 +               ia->ia_file = au_hf_top(file);
19058 +               a->udba = AuOpt_UDBA_NONE;
19059 +       } else {
19060 +               /* fchmod() doesn't pass ia_file */
19061 +               a->udba = au_opt_udba(sb);
19062 +               di_write_lock_child(dentry);
19063 +               /* no d_unlinked(), to set UDBA_NONE for root */
19064 +               if (d_unhashed(dentry))
19065 +                       a->udba = AuOpt_UDBA_NONE;
19066 +               if (a->udba != AuOpt_UDBA_NONE) {
19067 +                       AuDebugOn(IS_ROOT(dentry));
19068 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19069 +                       if (unlikely(err))
19070 +                               goto out_dentry;
19071 +               }
19072 +       }
19073 +
19074 +       err = au_pin_and_icpup(dentry, ia, a);
19075 +       if (unlikely(err < 0))
19076 +               goto out_dentry;
19077 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19078 +               ia->ia_file = NULL;
19079 +               ia->ia_valid &= ~ATTR_FILE;
19080 +       }
19081 +
19082 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19083 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19084 +           == (ATTR_MODE | ATTR_CTIME)) {
19085 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19086 +               if (unlikely(err))
19087 +                       goto out_unlock;
19088 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19089 +                  && (ia->ia_valid & ATTR_CTIME)) {
19090 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19091 +               if (unlikely(err))
19092 +                       goto out_unlock;
19093 +       }
19094 +
19095 +       if (ia->ia_valid & ATTR_SIZE) {
19096 +               struct file *f;
19097 +
19098 +               if (ia->ia_size < i_size_read(inode))
19099 +                       /* unmap only */
19100 +                       truncate_setsize(inode, ia->ia_size);
19101 +
19102 +               f = NULL;
19103 +               if (ia->ia_valid & ATTR_FILE)
19104 +                       f = ia->ia_file;
19105 +               mutex_unlock(&a->h_inode->i_mutex);
19106 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19107 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
19108 +       } else {
19109 +               delegated = NULL;
19110 +               while (1) {
19111 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19112 +                       if (delegated) {
19113 +                               err = break_deleg_wait(&delegated);
19114 +                               if (!err)
19115 +                                       continue;
19116 +                       }
19117 +                       break;
19118 +               }
19119 +       }
19120 +       if (!err)
19121 +               au_cpup_attr_changeable(inode);
19122 +
19123 +out_unlock:
19124 +       mutex_unlock(&a->h_inode->i_mutex);
19125 +       au_unpin(&a->pin);
19126 +       if (unlikely(err))
19127 +               au_update_dbstart(dentry);
19128 +out_dentry:
19129 +       di_write_unlock(dentry);
19130 +       if (file) {
19131 +               fi_write_unlock(file);
19132 +               ia->ia_file = file;
19133 +               ia->ia_valid |= ATTR_FILE;
19134 +       }
19135 +out_si:
19136 +       si_read_unlock(sb);
19137 +out_kfree:
19138 +       kfree(a);
19139 +out:
19140 +       AuTraceErr(err);
19141 +       return err;
19142 +}
19143 +
19144 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19145 +static int au_h_path_to_set_attr(struct dentry *dentry,
19146 +                                struct au_icpup_args *a, struct path *h_path)
19147 +{
19148 +       int err;
19149 +       struct super_block *sb;
19150 +
19151 +       sb = dentry->d_sb;
19152 +       a->udba = au_opt_udba(sb);
19153 +       /* no d_unlinked(), to set UDBA_NONE for root */
19154 +       if (d_unhashed(dentry))
19155 +               a->udba = AuOpt_UDBA_NONE;
19156 +       if (a->udba != AuOpt_UDBA_NONE) {
19157 +               AuDebugOn(IS_ROOT(dentry));
19158 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19159 +               if (unlikely(err))
19160 +                       goto out;
19161 +       }
19162 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19163 +       if (unlikely(err < 0))
19164 +               goto out;
19165 +
19166 +       h_path->dentry = a->h_path.dentry;
19167 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19168 +
19169 +out:
19170 +       return err;
19171 +}
19172 +
19173 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
19174 +{
19175 +       int err;
19176 +       struct path h_path;
19177 +       struct super_block *sb;
19178 +       struct au_icpup_args *a;
19179 +       struct inode *inode, *h_inode;
19180 +
19181 +       inode = d_inode(dentry);
19182 +       IMustLock(inode);
19183 +
19184 +       err = -ENOMEM;
19185 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19186 +       if (unlikely(!a))
19187 +               goto out;
19188 +
19189 +       sb = dentry->d_sb;
19190 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19191 +       if (unlikely(err))
19192 +               goto out_kfree;
19193 +
19194 +       h_path.dentry = NULL;   /* silence gcc */
19195 +       di_write_lock_child(dentry);
19196 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19197 +       if (unlikely(err))
19198 +               goto out_di;
19199 +
19200 +       mutex_unlock(&a->h_inode->i_mutex);
19201 +       switch (arg->type) {
19202 +       case AU_XATTR_SET:
19203 +               err = vfsub_setxattr(h_path.dentry,
19204 +                                    arg->u.set.name, arg->u.set.value,
19205 +                                    arg->u.set.size, arg->u.set.flags);
19206 +               break;
19207 +       case AU_XATTR_REMOVE:
19208 +               err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19209 +               break;
19210 +       case AU_ACL_SET:
19211 +               err = -EOPNOTSUPP;
19212 +               h_inode = d_inode(h_path.dentry);
19213 +               if (h_inode->i_op->set_acl)
19214 +                       err = h_inode->i_op->set_acl(h_inode,
19215 +                                                    arg->u.acl_set.acl,
19216 +                                                    arg->u.acl_set.type);
19217 +               break;
19218 +       }
19219 +       if (!err)
19220 +               au_cpup_attr_timesizes(inode);
19221 +
19222 +       au_unpin(&a->pin);
19223 +       if (unlikely(err))
19224 +               au_update_dbstart(dentry);
19225 +
19226 +out_di:
19227 +       di_write_unlock(dentry);
19228 +       si_read_unlock(sb);
19229 +out_kfree:
19230 +       kfree(a);
19231 +out:
19232 +       AuTraceErr(err);
19233 +       return err;
19234 +}
19235 +#endif
19236 +
19237 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19238 +                            unsigned int nlink)
19239 +{
19240 +       unsigned int n;
19241 +
19242 +       inode->i_mode = st->mode;
19243 +       /* don't i_[ug]id_write() here */
19244 +       inode->i_uid = st->uid;
19245 +       inode->i_gid = st->gid;
19246 +       inode->i_atime = st->atime;
19247 +       inode->i_mtime = st->mtime;
19248 +       inode->i_ctime = st->ctime;
19249 +
19250 +       au_cpup_attr_nlink(inode, /*force*/0);
19251 +       if (S_ISDIR(inode->i_mode)) {
19252 +               n = inode->i_nlink;
19253 +               n -= nlink;
19254 +               n += st->nlink;
19255 +               smp_mb(); /* for i_nlink */
19256 +               /* 0 can happen */
19257 +               set_nlink(inode, n);
19258 +       }
19259 +
19260 +       spin_lock(&inode->i_lock);
19261 +       inode->i_blocks = st->blocks;
19262 +       i_size_write(inode, st->size);
19263 +       spin_unlock(&inode->i_lock);
19264 +}
19265 +
19266 +/*
19267 + * common routine for aufs_getattr() and aufs_getxattr().
19268 + * returns zero or negative (an error).
19269 + * @dentry will be read-locked in success.
19270 + */
19271 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
19272 +{
19273 +       int err;
19274 +       unsigned int mnt_flags, sigen;
19275 +       unsigned char udba_none;
19276 +       aufs_bindex_t bindex;
19277 +       struct super_block *sb, *h_sb;
19278 +       struct inode *inode;
19279 +
19280 +       h_path->mnt = NULL;
19281 +       h_path->dentry = NULL;
19282 +
19283 +       err = 0;
19284 +       sb = dentry->d_sb;
19285 +       mnt_flags = au_mntflags(sb);
19286 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19287 +
19288 +       /* support fstat(2) */
19289 +       if (!d_unlinked(dentry) && !udba_none) {
19290 +               sigen = au_sigen(sb);
19291 +               err = au_digen_test(dentry, sigen);
19292 +               if (!err) {
19293 +                       di_read_lock_child(dentry, AuLock_IR);
19294 +                       err = au_dbrange_test(dentry);
19295 +                       if (unlikely(err)) {
19296 +                               di_read_unlock(dentry, AuLock_IR);
19297 +                               goto out;
19298 +                       }
19299 +               } else {
19300 +                       AuDebugOn(IS_ROOT(dentry));
19301 +                       di_write_lock_child(dentry);
19302 +                       err = au_dbrange_test(dentry);
19303 +                       if (!err)
19304 +                               err = au_reval_for_attr(dentry, sigen);
19305 +                       if (!err)
19306 +                               di_downgrade_lock(dentry, AuLock_IR);
19307 +                       else {
19308 +                               di_write_unlock(dentry);
19309 +                               goto out;
19310 +                       }
19311 +               }
19312 +       } else
19313 +               di_read_lock_child(dentry, AuLock_IR);
19314 +
19315 +       inode = d_inode(dentry);
19316 +       bindex = au_ibstart(inode);
19317 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19318 +       h_sb = h_path->mnt->mnt_sb;
19319 +       if (!force
19320 +           && !au_test_fs_bad_iattr(h_sb)
19321 +           && udba_none)
19322 +               goto out; /* success */
19323 +
19324 +       if (au_dbstart(dentry) == bindex)
19325 +               h_path->dentry = au_h_dptr(dentry, bindex);
19326 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19327 +               h_path->dentry = au_plink_lkup(inode, bindex);
19328 +               if (IS_ERR(h_path->dentry))
19329 +                       /* pretending success */
19330 +                       h_path->dentry = NULL;
19331 +               else
19332 +                       dput(h_path->dentry);
19333 +       }
19334 +
19335 +out:
19336 +       return err;
19337 +}
19338 +
19339 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19340 +                       struct dentry *dentry, struct kstat *st)
19341 +{
19342 +       int err;
19343 +       unsigned char positive;
19344 +       struct path h_path;
19345 +       struct inode *inode;
19346 +       struct super_block *sb;
19347 +
19348 +       inode = d_inode(dentry);
19349 +       sb = dentry->d_sb;
19350 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19351 +       if (unlikely(err))
19352 +               goto out;
19353 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19354 +       if (unlikely(err))
19355 +               goto out_si;
19356 +       if (unlikely(!h_path.dentry))
19357 +               /* illegally overlapped or something */
19358 +               goto out_fill; /* pretending success */
19359 +
19360 +       positive = d_is_positive(h_path.dentry);
19361 +       if (positive)
19362 +               err = vfs_getattr(&h_path, st);
19363 +       if (!err) {
19364 +               if (positive)
19365 +                       au_refresh_iattr(inode, st,
19366 +                                        d_inode(h_path.dentry)->i_nlink);
19367 +               goto out_fill; /* success */
19368 +       }
19369 +       AuTraceErr(err);
19370 +       goto out_di;
19371 +
19372 +out_fill:
19373 +       generic_fillattr(inode, st);
19374 +out_di:
19375 +       di_read_unlock(dentry, AuLock_IR);
19376 +out_si:
19377 +       si_read_unlock(sb);
19378 +out:
19379 +       AuTraceErr(err);
19380 +       return err;
19381 +}
19382 +
19383 +/* ---------------------------------------------------------------------- */
19384 +
19385 +/*
19386 + * Assumption:
19387 + * - the number of symlinks is not so many.
19388 + *
19389 + * Structure:
19390 + * - sbinfo (instead of iinfo) contains an hlist of struct au_symlink.
19391 + *   If iinfo contained the hlist, then it would be rather large waste of memory
19392 + *   I am afraid.
19393 + * - struct au_symlink contains the necessary info for h_inode follow_link() and
19394 + *   put_link().
19395 + */
19396 +
19397 +struct au_symlink {
19398 +       union {
19399 +               struct hlist_node hlist;
19400 +               struct rcu_head rcu;
19401 +       };
19402 +
19403 +       struct inode *h_inode;
19404 +       void *h_cookie;
19405 +};
19406 +
19407 +static void au_symlink_add(struct super_block *sb, struct au_symlink *slink,
19408 +                          struct inode *h_inode, void *cookie)
19409 +{
19410 +       struct au_sbinfo *sbinfo;
19411 +
19412 +       ihold(h_inode);
19413 +       slink->h_inode = h_inode;
19414 +       slink->h_cookie = cookie;
19415 +       sbinfo = au_sbi(sb);
19416 +       au_sphl_add(&slink->hlist, &sbinfo->si_symlink);
19417 +}
19418 +
19419 +static void au_symlink_del(struct super_block *sb, struct au_symlink *slink)
19420 +{
19421 +       struct au_sbinfo *sbinfo;
19422 +
19423 +       /* do not iput() within rcu */
19424 +       iput(slink->h_inode);
19425 +       slink->h_inode = NULL;
19426 +       sbinfo = au_sbi(sb);
19427 +       au_sphl_del_rcu(&slink->hlist, &sbinfo->si_symlink);
19428 +       kfree_rcu(slink, rcu);
19429 +}
19430 +
19431 +static const char *aufs_follow_link(struct dentry *dentry, void **cookie)
19432 +{
19433 +       const char *ret;
19434 +       struct inode *inode, *h_inode;
19435 +       struct dentry *h_dentry;
19436 +       struct au_symlink *slink;
19437 +       int err;
19438 +       aufs_bindex_t bindex;
19439 +
19440 +       ret = NULL; /* suppress a warning */
19441 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19442 +       if (unlikely(err))
19443 +               goto out;
19444 +
19445 +       err = au_d_hashed_positive(dentry);
19446 +       if (unlikely(err))
19447 +               goto out_unlock;
19448 +
19449 +       err = -EINVAL;
19450 +       inode = d_inode(dentry);
19451 +       bindex = au_ibstart(inode);
19452 +       h_inode = au_h_iptr(inode, bindex);
19453 +       if (unlikely(!h_inode->i_op->follow_link))
19454 +               goto out_unlock;
19455 +
19456 +       err = -ENOMEM;
19457 +       slink = kmalloc(sizeof(*slink), GFP_NOFS);
19458 +       if (unlikely(!slink))
19459 +               goto out_unlock;
19460 +
19461 +       err = -EBUSY;
19462 +       h_dentry = NULL;
19463 +       if (au_dbstart(dentry) <= bindex) {
19464 +               h_dentry = au_h_dptr(dentry, bindex);
19465 +               if (h_dentry)
19466 +                       dget(h_dentry);
19467 +       }
19468 +       if (!h_dentry) {
19469 +               h_dentry = d_find_any_alias(h_inode);
19470 +               if (IS_ERR(h_dentry)) {
19471 +                       err = PTR_ERR(h_dentry);
19472 +                       goto out_free;
19473 +               }
19474 +       }
19475 +       if (unlikely(!h_dentry))
19476 +               goto out_free;
19477 +
19478 +       err = 0;
19479 +       AuDbg("%pf\n", h_inode->i_op->follow_link);
19480 +       AuDbgDentry(h_dentry);
19481 +       ret = h_inode->i_op->follow_link(h_dentry, cookie);
19482 +       dput(h_dentry);
19483 +
19484 +       if (!IS_ERR_OR_NULL(ret)) {
19485 +               au_symlink_add(inode->i_sb, slink, h_inode, *cookie);
19486 +               *cookie = slink;
19487 +               AuDbg("slink %p\n", slink);
19488 +               goto out_unlock; /* success */
19489 +       }
19490 +
19491 +out_free:
19492 +       slink->h_inode = NULL;
19493 +       kfree_rcu(slink, rcu);
19494 +out_unlock:
19495 +       aufs_read_unlock(dentry, AuLock_IR);
19496 +out:
19497 +       if (unlikely(err))
19498 +               ret = ERR_PTR(err);
19499 +       AuTraceErrPtr(ret);
19500 +       return ret;
19501 +}
19502 +
19503 +static void aufs_put_link(struct inode *inode, void *cookie)
19504 +{
19505 +       struct au_symlink *slink;
19506 +       struct inode *h_inode;
19507 +
19508 +       slink = cookie;
19509 +       AuDbg("slink %p\n", slink);
19510 +       h_inode = slink->h_inode;
19511 +       AuDbg("%pf\n", h_inode->i_op->put_link);
19512 +       AuDbgInode(h_inode);
19513 +       if (h_inode->i_op->put_link)
19514 +               h_inode->i_op->put_link(h_inode, slink->h_cookie);
19515 +       au_symlink_del(inode->i_sb, slink);
19516 +}
19517 +
19518 +/* ---------------------------------------------------------------------- */
19519 +
19520 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19521 +{
19522 +       int err;
19523 +       struct super_block *sb;
19524 +       struct inode *h_inode;
19525 +
19526 +       sb = inode->i_sb;
19527 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19528 +       lockdep_off();
19529 +       si_read_lock(sb, AuLock_FLUSH);
19530 +       ii_write_lock_child(inode);
19531 +       lockdep_on();
19532 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
19533 +       err = vfsub_update_time(h_inode, ts, flags);
19534 +       lockdep_off();
19535 +       if (!err)
19536 +               au_cpup_attr_timesizes(inode);
19537 +       ii_write_unlock(inode);
19538 +       si_read_unlock(sb);
19539 +       lockdep_on();
19540 +
19541 +       if (!err && (flags & S_VERSION))
19542 +               inode_inc_iversion(inode);
19543 +
19544 +       return err;
19545 +}
19546 +
19547 +/* ---------------------------------------------------------------------- */
19548 +
19549 +/* no getattr version will be set by module.c:aufs_init() */
19550 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
19551 +       aufs_iop[] = {
19552 +       [AuIop_SYMLINK] = {
19553 +               .permission     = aufs_permission,
19554 +#ifdef CONFIG_FS_POSIX_ACL
19555 +               .get_acl        = aufs_get_acl,
19556 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19557 +#endif
19558 +
19559 +               .setattr        = aufs_setattr,
19560 +               .getattr        = aufs_getattr,
19561 +
19562 +#ifdef CONFIG_AUFS_XATTR
19563 +               .setxattr       = aufs_setxattr,
19564 +               .getxattr       = aufs_getxattr,
19565 +               .listxattr      = aufs_listxattr,
19566 +               .removexattr    = aufs_removexattr,
19567 +#endif
19568 +
19569 +               .readlink       = generic_readlink,
19570 +               .follow_link    = aufs_follow_link,
19571 +               .put_link       = aufs_put_link,
19572 +
19573 +               /* .update_time = aufs_update_time */
19574 +       },
19575 +       [AuIop_DIR] = {
19576 +               .create         = aufs_create,
19577 +               .lookup         = aufs_lookup,
19578 +               .link           = aufs_link,
19579 +               .unlink         = aufs_unlink,
19580 +               .symlink        = aufs_symlink,
19581 +               .mkdir          = aufs_mkdir,
19582 +               .rmdir          = aufs_rmdir,
19583 +               .mknod          = aufs_mknod,
19584 +               .rename         = aufs_rename,
19585 +
19586 +               .permission     = aufs_permission,
19587 +#ifdef CONFIG_FS_POSIX_ACL
19588 +               .get_acl        = aufs_get_acl,
19589 +               .set_acl        = aufs_set_acl,
19590 +#endif
19591 +
19592 +               .setattr        = aufs_setattr,
19593 +               .getattr        = aufs_getattr,
19594 +
19595 +#ifdef CONFIG_AUFS_XATTR
19596 +               .setxattr       = aufs_setxattr,
19597 +               .getxattr       = aufs_getxattr,
19598 +               .listxattr      = aufs_listxattr,
19599 +               .removexattr    = aufs_removexattr,
19600 +#endif
19601 +
19602 +               .update_time    = aufs_update_time,
19603 +               .atomic_open    = aufs_atomic_open,
19604 +               .tmpfile        = aufs_tmpfile
19605 +       },
19606 +       [AuIop_OTHER] = {
19607 +               .permission     = aufs_permission,
19608 +#ifdef CONFIG_FS_POSIX_ACL
19609 +               .get_acl        = aufs_get_acl,
19610 +               .set_acl        = aufs_set_acl,
19611 +#endif
19612 +
19613 +               .setattr        = aufs_setattr,
19614 +               .getattr        = aufs_getattr,
19615 +
19616 +#ifdef CONFIG_AUFS_XATTR
19617 +               .setxattr       = aufs_setxattr,
19618 +               .getxattr       = aufs_getxattr,
19619 +               .listxattr      = aufs_listxattr,
19620 +               .removexattr    = aufs_removexattr,
19621 +#endif
19622 +
19623 +               .update_time    = aufs_update_time
19624 +       }
19625 +};
19626 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19627 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19628 +++ linux/fs/aufs/i_op_del.c    2016-01-13 20:11:11.669760262 +0100
19629 @@ -0,0 +1,510 @@
19630 +/*
19631 + * Copyright (C) 2005-2015 Junjiro R. Okajima
19632 + *
19633 + * This program, aufs is free software; you can redistribute it and/or modify
19634 + * it under the terms of the GNU General Public License as published by
19635 + * the Free Software Foundation; either version 2 of the License, or
19636 + * (at your option) any later version.
19637 + *
19638 + * This program is distributed in the hope that it will be useful,
19639 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19640 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19641 + * GNU General Public License for more details.
19642 + *
19643 + * You should have received a copy of the GNU General Public License
19644 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19645 + */
19646 +
19647 +/*
19648 + * inode operations (del entry)
19649 + */
19650 +
19651 +#include "aufs.h"
19652 +
19653 +/*
19654 + * decide if a new whiteout for @dentry is necessary or not.
19655 + * when it is necessary, prepare the parent dir for the upper branch whose
19656 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19657 + * be done by caller.
19658 + * return value:
19659 + * 0: wh is unnecessary
19660 + * plus: wh is necessary
19661 + * minus: error
19662 + */
19663 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19664 +{
19665 +       int need_wh, err;
19666 +       aufs_bindex_t bstart;
19667 +       struct super_block *sb;
19668 +
19669 +       sb = dentry->d_sb;
19670 +       bstart = au_dbstart(dentry);
19671 +       if (*bcpup < 0) {
19672 +               *bcpup = bstart;
19673 +               if (au_test_ro(sb, bstart, d_inode(dentry))) {
19674 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19675 +                       *bcpup = err;
19676 +                       if (unlikely(err < 0))
19677 +                               goto out;
19678 +               }
19679 +       } else
19680 +               AuDebugOn(bstart < *bcpup
19681 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
19682 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
19683 +
19684 +       if (*bcpup != bstart) {
19685 +               err = au_cpup_dirs(dentry, *bcpup);
19686 +               if (unlikely(err))
19687 +                       goto out;
19688 +               need_wh = 1;
19689 +       } else {
19690 +               struct au_dinfo *dinfo, *tmp;
19691 +
19692 +               need_wh = -ENOMEM;
19693 +               dinfo = au_di(dentry);
19694 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19695 +               if (tmp) {
19696 +                       au_di_cp(tmp, dinfo);
19697 +                       au_di_swap(tmp, dinfo);
19698 +                       /* returns the number of positive dentries */
19699 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
19700 +                       au_di_swap(tmp, dinfo);
19701 +                       au_rw_write_unlock(&tmp->di_rwsem);
19702 +                       au_di_free(tmp);
19703 +               }
19704 +       }
19705 +       AuDbg("need_wh %d\n", need_wh);
19706 +       err = need_wh;
19707 +
19708 +out:
19709 +       return err;
19710 +}
19711 +
19712 +/*
19713 + * simple tests for the del-entry operations.
19714 + * following the checks in vfs, plus the parent-child relationship.
19715 + */
19716 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19717 +              struct dentry *h_parent, int isdir)
19718 +{
19719 +       int err;
19720 +       umode_t h_mode;
19721 +       struct dentry *h_dentry, *h_latest;
19722 +       struct inode *h_inode;
19723 +
19724 +       h_dentry = au_h_dptr(dentry, bindex);
19725 +       if (d_really_is_positive(dentry)) {
19726 +               err = -ENOENT;
19727 +               if (unlikely(d_is_negative(h_dentry)))
19728 +                       goto out;
19729 +               h_inode = d_inode(h_dentry);
19730 +               if (unlikely(!h_inode->i_nlink))
19731 +                       goto out;
19732 +
19733 +               h_mode = h_inode->i_mode;
19734 +               if (!isdir) {
19735 +                       err = -EISDIR;
19736 +                       if (unlikely(S_ISDIR(h_mode)))
19737 +                               goto out;
19738 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19739 +                       err = -ENOTDIR;
19740 +                       goto out;
19741 +               }
19742 +       } else {
19743 +               /* rename(2) case */
19744 +               err = -EIO;
19745 +               if (unlikely(d_is_positive(h_dentry)))
19746 +                       goto out;
19747 +       }
19748 +
19749 +       err = -ENOENT;
19750 +       /* expected parent dir is locked */
19751 +       if (unlikely(h_parent != h_dentry->d_parent))
19752 +               goto out;
19753 +       err = 0;
19754 +
19755 +       /*
19756 +        * rmdir a dir may break the consistency on some filesystem.
19757 +        * let's try heavy test.
19758 +        */
19759 +       err = -EACCES;
19760 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
19761 +                    && au_test_h_perm(d_inode(h_parent),
19762 +                                      MAY_EXEC | MAY_WRITE)))
19763 +               goto out;
19764 +
19765 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
19766 +       err = -EIO;
19767 +       if (IS_ERR(h_latest))
19768 +               goto out;
19769 +       if (h_latest == h_dentry)
19770 +               err = 0;
19771 +       dput(h_latest);
19772 +
19773 +out:
19774 +       return err;
19775 +}
19776 +
19777 +/*
19778 + * decide the branch where we operate for @dentry. the branch index will be set
19779 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19780 + * dir for reverting.
19781 + * when a new whiteout is necessary, create it.
19782 + */
19783 +static struct dentry*
19784 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19785 +                   struct au_dtime *dt, struct au_pin *pin)
19786 +{
19787 +       struct dentry *wh_dentry;
19788 +       struct super_block *sb;
19789 +       struct path h_path;
19790 +       int err, need_wh;
19791 +       unsigned int udba;
19792 +       aufs_bindex_t bcpup;
19793 +
19794 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19795 +       wh_dentry = ERR_PTR(need_wh);
19796 +       if (unlikely(need_wh < 0))
19797 +               goto out;
19798 +
19799 +       sb = dentry->d_sb;
19800 +       udba = au_opt_udba(sb);
19801 +       bcpup = *rbcpup;
19802 +       err = au_pin(pin, dentry, bcpup, udba,
19803 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19804 +       wh_dentry = ERR_PTR(err);
19805 +       if (unlikely(err))
19806 +               goto out;
19807 +
19808 +       h_path.dentry = au_pinned_h_parent(pin);
19809 +       if (udba != AuOpt_UDBA_NONE
19810 +           && au_dbstart(dentry) == bcpup) {
19811 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
19812 +               wh_dentry = ERR_PTR(err);
19813 +               if (unlikely(err))
19814 +                       goto out_unpin;
19815 +       }
19816 +
19817 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
19818 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
19819 +       wh_dentry = NULL;
19820 +       if (!need_wh)
19821 +               goto out; /* success, no need to create whiteout */
19822 +
19823 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
19824 +       if (IS_ERR(wh_dentry))
19825 +               goto out_unpin;
19826 +
19827 +       /* returns with the parent is locked and wh_dentry is dget-ed */
19828 +       goto out; /* success */
19829 +
19830 +out_unpin:
19831 +       au_unpin(pin);
19832 +out:
19833 +       return wh_dentry;
19834 +}
19835 +
19836 +/*
19837 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
19838 + * in order to be revertible and save time for removing many child whiteouts
19839 + * under the dir.
19840 + * returns 1 when there are too many child whiteout and caller should remove
19841 + * them asynchronously. returns 0 when the number of children is enough small to
19842 + * remove now or the branch fs is a remote fs.
19843 + * otherwise return an error.
19844 + */
19845 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
19846 +                          struct au_nhash *whlist, struct inode *dir)
19847 +{
19848 +       int rmdir_later, err, dirwh;
19849 +       struct dentry *h_dentry;
19850 +       struct super_block *sb;
19851 +       struct inode *inode;
19852 +
19853 +       sb = dentry->d_sb;
19854 +       SiMustAnyLock(sb);
19855 +       h_dentry = au_h_dptr(dentry, bindex);
19856 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
19857 +       if (unlikely(err))
19858 +               goto out;
19859 +
19860 +       /* stop monitoring */
19861 +       inode = d_inode(dentry);
19862 +       au_hn_free(au_hi(inode, bindex));
19863 +
19864 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
19865 +               dirwh = au_sbi(sb)->si_dirwh;
19866 +               rmdir_later = (dirwh <= 1);
19867 +               if (!rmdir_later)
19868 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
19869 +                                                             dirwh);
19870 +               if (rmdir_later)
19871 +                       return rmdir_later;
19872 +       }
19873 +
19874 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
19875 +       if (unlikely(err)) {
19876 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
19877 +                       h_dentry, bindex, err);
19878 +               err = 0;
19879 +       }
19880 +
19881 +out:
19882 +       AuTraceErr(err);
19883 +       return err;
19884 +}
19885 +
19886 +/*
19887 + * final procedure for deleting a entry.
19888 + * maintain dentry and iattr.
19889 + */
19890 +static void epilog(struct inode *dir, struct dentry *dentry,
19891 +                  aufs_bindex_t bindex)
19892 +{
19893 +       struct inode *inode;
19894 +
19895 +       inode = d_inode(dentry);
19896 +       d_drop(dentry);
19897 +       inode->i_ctime = dir->i_ctime;
19898 +
19899 +       au_dir_ts(dir, bindex);
19900 +       dir->i_version++;
19901 +}
19902 +
19903 +/*
19904 + * when an error happened, remove the created whiteout and revert everything.
19905 + */
19906 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
19907 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
19908 +                    struct dentry *dentry, struct au_dtime *dt)
19909 +{
19910 +       int rerr;
19911 +       struct path h_path = {
19912 +               .dentry = wh_dentry,
19913 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
19914 +       };
19915 +
19916 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
19917 +       if (!rerr) {
19918 +               au_set_dbwh(dentry, bwh);
19919 +               au_dtime_revert(dt);
19920 +               return 0;
19921 +       }
19922 +
19923 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
19924 +       return -EIO;
19925 +}
19926 +
19927 +/* ---------------------------------------------------------------------- */
19928 +
19929 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
19930 +{
19931 +       int err;
19932 +       aufs_bindex_t bwh, bindex, bstart;
19933 +       struct inode *inode, *h_dir, *delegated;
19934 +       struct dentry *parent, *wh_dentry;
19935 +       /* to reuduce stack size */
19936 +       struct {
19937 +               struct au_dtime dt;
19938 +               struct au_pin pin;
19939 +               struct path h_path;
19940 +       } *a;
19941 +
19942 +       IMustLock(dir);
19943 +
19944 +       err = -ENOMEM;
19945 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19946 +       if (unlikely(!a))
19947 +               goto out;
19948 +
19949 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19950 +       if (unlikely(err))
19951 +               goto out_free;
19952 +       err = au_d_hashed_positive(dentry);
19953 +       if (unlikely(err))
19954 +               goto out_unlock;
19955 +       inode = d_inode(dentry);
19956 +       IMustLock(inode);
19957 +       err = -EISDIR;
19958 +       if (unlikely(d_is_dir(dentry)))
19959 +               goto out_unlock; /* possible? */
19960 +
19961 +       bstart = au_dbstart(dentry);
19962 +       bwh = au_dbwh(dentry);
19963 +       bindex = -1;
19964 +       parent = dentry->d_parent; /* dir inode is locked */
19965 +       di_write_lock_parent(parent);
19966 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
19967 +                                       &a->pin);
19968 +       err = PTR_ERR(wh_dentry);
19969 +       if (IS_ERR(wh_dentry))
19970 +               goto out_parent;
19971 +
19972 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
19973 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
19974 +       dget(a->h_path.dentry);
19975 +       if (bindex == bstart) {
19976 +               h_dir = au_pinned_h_dir(&a->pin);
19977 +               delegated = NULL;
19978 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
19979 +               if (unlikely(err == -EWOULDBLOCK)) {
19980 +                       pr_warn("cannot retry for NFSv4 delegation"
19981 +                               " for an internal unlink\n");
19982 +                       iput(delegated);
19983 +               }
19984 +       } else {
19985 +               /* dir inode is locked */
19986 +               h_dir = d_inode(wh_dentry->d_parent);
19987 +               IMustLock(h_dir);
19988 +               err = 0;
19989 +       }
19990 +
19991 +       if (!err) {
19992 +               vfsub_drop_nlink(inode);
19993 +               epilog(dir, dentry, bindex);
19994 +
19995 +               /* update target timestamps */
19996 +               if (bindex == bstart) {
19997 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
19998 +                       /*ignore*/
19999 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20000 +               } else
20001 +                       /* todo: this timestamp may be reverted later */
20002 +                       inode->i_ctime = h_dir->i_ctime;
20003 +               goto out_unpin; /* success */
20004 +       }
20005 +
20006 +       /* revert */
20007 +       if (wh_dentry) {
20008 +               int rerr;
20009 +
20010 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20011 +                                &a->dt);
20012 +               if (rerr)
20013 +                       err = rerr;
20014 +       }
20015 +
20016 +out_unpin:
20017 +       au_unpin(&a->pin);
20018 +       dput(wh_dentry);
20019 +       dput(a->h_path.dentry);
20020 +out_parent:
20021 +       di_write_unlock(parent);
20022 +out_unlock:
20023 +       aufs_read_unlock(dentry, AuLock_DW);
20024 +out_free:
20025 +       kfree(a);
20026 +out:
20027 +       return err;
20028 +}
20029 +
20030 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
20031 +{
20032 +       int err, rmdir_later;
20033 +       aufs_bindex_t bwh, bindex, bstart;
20034 +       struct inode *inode;
20035 +       struct dentry *parent, *wh_dentry, *h_dentry;
20036 +       struct au_whtmp_rmdir *args;
20037 +       /* to reuduce stack size */
20038 +       struct {
20039 +               struct au_dtime dt;
20040 +               struct au_pin pin;
20041 +       } *a;
20042 +
20043 +       IMustLock(dir);
20044 +
20045 +       err = -ENOMEM;
20046 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20047 +       if (unlikely(!a))
20048 +               goto out;
20049 +
20050 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20051 +       if (unlikely(err))
20052 +               goto out_free;
20053 +       err = au_alive_dir(dentry);
20054 +       if (unlikely(err))
20055 +               goto out_unlock;
20056 +       inode = d_inode(dentry);
20057 +       IMustLock(inode);
20058 +       err = -ENOTDIR;
20059 +       if (unlikely(!d_is_dir(dentry)))
20060 +               goto out_unlock; /* possible? */
20061 +
20062 +       err = -ENOMEM;
20063 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20064 +       if (unlikely(!args))
20065 +               goto out_unlock;
20066 +
20067 +       parent = dentry->d_parent; /* dir inode is locked */
20068 +       di_write_lock_parent(parent);
20069 +       err = au_test_empty(dentry, &args->whlist);
20070 +       if (unlikely(err))
20071 +               goto out_parent;
20072 +
20073 +       bstart = au_dbstart(dentry);
20074 +       bwh = au_dbwh(dentry);
20075 +       bindex = -1;
20076 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20077 +                                       &a->pin);
20078 +       err = PTR_ERR(wh_dentry);
20079 +       if (IS_ERR(wh_dentry))
20080 +               goto out_parent;
20081 +
20082 +       h_dentry = au_h_dptr(dentry, bstart);
20083 +       dget(h_dentry);
20084 +       rmdir_later = 0;
20085 +       if (bindex == bstart) {
20086 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
20087 +               if (err > 0) {
20088 +                       rmdir_later = err;
20089 +                       err = 0;
20090 +               }
20091 +       } else {
20092 +               /* stop monitoring */
20093 +               au_hn_free(au_hi(inode, bstart));
20094 +
20095 +               /* dir inode is locked */
20096 +               IMustLock(d_inode(wh_dentry->d_parent));
20097 +               err = 0;
20098 +       }
20099 +
20100 +       if (!err) {
20101 +               vfsub_dead_dir(inode);
20102 +               au_set_dbdiropq(dentry, -1);
20103 +               epilog(dir, dentry, bindex);
20104 +
20105 +               if (rmdir_later) {
20106 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
20107 +                       args = NULL;
20108 +               }
20109 +
20110 +               goto out_unpin; /* success */
20111 +       }
20112 +
20113 +       /* revert */
20114 +       AuLabel(revert);
20115 +       if (wh_dentry) {
20116 +               int rerr;
20117 +
20118 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20119 +                                &a->dt);
20120 +               if (rerr)
20121 +                       err = rerr;
20122 +       }
20123 +
20124 +out_unpin:
20125 +       au_unpin(&a->pin);
20126 +       dput(wh_dentry);
20127 +       dput(h_dentry);
20128 +out_parent:
20129 +       di_write_unlock(parent);
20130 +       if (args)
20131 +               au_whtmp_rmdir_free(args);
20132 +out_unlock:
20133 +       aufs_read_unlock(dentry, AuLock_DW);
20134 +out_free:
20135 +       kfree(a);
20136 +out:
20137 +       AuTraceErr(err);
20138 +       return err;
20139 +}
20140 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20141 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20142 +++ linux/fs/aufs/i_op_ren.c    2016-01-13 20:11:11.669760262 +0100
20143 @@ -0,0 +1,1015 @@
20144 +/*
20145 + * Copyright (C) 2005-2015 Junjiro R. Okajima
20146 + *
20147 + * This program, aufs is free software; you can redistribute it and/or modify
20148 + * it under the terms of the GNU General Public License as published by
20149 + * the Free Software Foundation; either version 2 of the License, or
20150 + * (at your option) any later version.
20151 + *
20152 + * This program is distributed in the hope that it will be useful,
20153 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20154 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20155 + * GNU General Public License for more details.
20156 + *
20157 + * You should have received a copy of the GNU General Public License
20158 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20159 + */
20160 +
20161 +/*
20162 + * inode operation (rename entry)
20163 + * todo: this is crazy monster
20164 + */
20165 +
20166 +#include "aufs.h"
20167 +
20168 +enum { AuSRC, AuDST, AuSrcDst };
20169 +enum { AuPARENT, AuCHILD, AuParentChild };
20170 +
20171 +#define AuRen_ISDIR    1
20172 +#define AuRen_ISSAMEDIR        (1 << 1)
20173 +#define AuRen_WHSRC    (1 << 2)
20174 +#define AuRen_WHDST    (1 << 3)
20175 +#define AuRen_MNT_WRITE        (1 << 4)
20176 +#define AuRen_DT_DSTDIR        (1 << 5)
20177 +#define AuRen_DIROPQ   (1 << 6)
20178 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20179 +#define au_fset_ren(flags, name) \
20180 +       do { (flags) |= AuRen_##name; } while (0)
20181 +#define au_fclr_ren(flags, name) \
20182 +       do { (flags) &= ~AuRen_##name; } while (0)
20183 +
20184 +struct au_ren_args {
20185 +       struct {
20186 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20187 +                       *wh_dentry;
20188 +               struct inode *dir, *inode;
20189 +               struct au_hinode *hdir;
20190 +               struct au_dtime dt[AuParentChild];
20191 +               aufs_bindex_t bstart;
20192 +       } sd[AuSrcDst];
20193 +
20194 +#define src_dentry     sd[AuSRC].dentry
20195 +#define src_dir                sd[AuSRC].dir
20196 +#define src_inode      sd[AuSRC].inode
20197 +#define src_h_dentry   sd[AuSRC].h_dentry
20198 +#define src_parent     sd[AuSRC].parent
20199 +#define src_h_parent   sd[AuSRC].h_parent
20200 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20201 +#define src_hdir       sd[AuSRC].hdir
20202 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20203 +#define src_dt         sd[AuSRC].dt
20204 +#define src_bstart     sd[AuSRC].bstart
20205 +
20206 +#define dst_dentry     sd[AuDST].dentry
20207 +#define dst_dir                sd[AuDST].dir
20208 +#define dst_inode      sd[AuDST].inode
20209 +#define dst_h_dentry   sd[AuDST].h_dentry
20210 +#define dst_parent     sd[AuDST].parent
20211 +#define dst_h_parent   sd[AuDST].h_parent
20212 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20213 +#define dst_hdir       sd[AuDST].hdir
20214 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20215 +#define dst_dt         sd[AuDST].dt
20216 +#define dst_bstart     sd[AuDST].bstart
20217 +
20218 +       struct dentry *h_trap;
20219 +       struct au_branch *br;
20220 +       struct au_hinode *src_hinode;
20221 +       struct path h_path;
20222 +       struct au_nhash whlist;
20223 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
20224 +
20225 +       unsigned int flags;
20226 +
20227 +       struct au_whtmp_rmdir *thargs;
20228 +       struct dentry *h_dst;
20229 +};
20230 +
20231 +/* ---------------------------------------------------------------------- */
20232 +
20233 +/*
20234 + * functions for reverting.
20235 + * when an error happened in a single rename systemcall, we should revert
20236 + * everything as if nothing happened.
20237 + * we don't need to revert the copied-up/down the parent dir since they are
20238 + * harmless.
20239 + */
20240 +
20241 +#define RevertFailure(fmt, ...) do { \
20242 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20243 +               ##__VA_ARGS__, err, rerr); \
20244 +       err = -EIO; \
20245 +} while (0)
20246 +
20247 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20248 +{
20249 +       int rerr;
20250 +
20251 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20252 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
20253 +       au_hn_imtx_unlock(a->src_hinode);
20254 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
20255 +       if (rerr)
20256 +               RevertFailure("remove diropq %pd", a->src_dentry);
20257 +}
20258 +
20259 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20260 +{
20261 +       int rerr;
20262 +       struct inode *delegated;
20263 +
20264 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20265 +                                         a->src_h_parent);
20266 +       rerr = PTR_ERR(a->h_path.dentry);
20267 +       if (IS_ERR(a->h_path.dentry)) {
20268 +               RevertFailure("lkup one %pd", a->src_dentry);
20269 +               return;
20270 +       }
20271 +
20272 +       delegated = NULL;
20273 +       rerr = vfsub_rename(a->dst_h_dir,
20274 +                           au_h_dptr(a->src_dentry, a->btgt),
20275 +                           a->src_h_dir, &a->h_path, &delegated);
20276 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20277 +               pr_warn("cannot retry for NFSv4 delegation"
20278 +                       " for an internal rename\n");
20279 +               iput(delegated);
20280 +       }
20281 +       d_drop(a->h_path.dentry);
20282 +       dput(a->h_path.dentry);
20283 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20284 +       if (rerr)
20285 +               RevertFailure("rename %pd", a->src_dentry);
20286 +}
20287 +
20288 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20289 +{
20290 +       int rerr;
20291 +       struct inode *delegated;
20292 +
20293 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20294 +                                         a->dst_h_parent);
20295 +       rerr = PTR_ERR(a->h_path.dentry);
20296 +       if (IS_ERR(a->h_path.dentry)) {
20297 +               RevertFailure("lkup one %pd", a->dst_dentry);
20298 +               return;
20299 +       }
20300 +       if (d_is_positive(a->h_path.dentry)) {
20301 +               d_drop(a->h_path.dentry);
20302 +               dput(a->h_path.dentry);
20303 +               return;
20304 +       }
20305 +
20306 +       delegated = NULL;
20307 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20308 +                           &delegated);
20309 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20310 +               pr_warn("cannot retry for NFSv4 delegation"
20311 +                       " for an internal rename\n");
20312 +               iput(delegated);
20313 +       }
20314 +       d_drop(a->h_path.dentry);
20315 +       dput(a->h_path.dentry);
20316 +       if (!rerr)
20317 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20318 +       else
20319 +               RevertFailure("rename %pd", a->h_dst);
20320 +}
20321 +
20322 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20323 +{
20324 +       int rerr;
20325 +
20326 +       a->h_path.dentry = a->src_wh_dentry;
20327 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20328 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20329 +       if (rerr)
20330 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20331 +}
20332 +#undef RevertFailure
20333 +
20334 +/* ---------------------------------------------------------------------- */
20335 +
20336 +/*
20337 + * when we have to copyup the renaming entry, do it with the rename-target name
20338 + * in order to minimize the cost (the later actual rename is unnecessary).
20339 + * otherwise rename it on the target branch.
20340 + */
20341 +static int au_ren_or_cpup(struct au_ren_args *a)
20342 +{
20343 +       int err;
20344 +       struct dentry *d;
20345 +       struct inode *delegated;
20346 +
20347 +       d = a->src_dentry;
20348 +       if (au_dbstart(d) == a->btgt) {
20349 +               a->h_path.dentry = a->dst_h_dentry;
20350 +               if (au_ftest_ren(a->flags, DIROPQ)
20351 +                   && au_dbdiropq(d) == a->btgt)
20352 +                       au_fclr_ren(a->flags, DIROPQ);
20353 +               AuDebugOn(au_dbstart(d) != a->btgt);
20354 +               delegated = NULL;
20355 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20356 +                                  a->dst_h_dir, &a->h_path, &delegated);
20357 +               if (unlikely(err == -EWOULDBLOCK)) {
20358 +                       pr_warn("cannot retry for NFSv4 delegation"
20359 +                               " for an internal rename\n");
20360 +                       iput(delegated);
20361 +               }
20362 +       } else
20363 +               BUG();
20364 +
20365 +       if (!err && a->h_dst)
20366 +               /* it will be set to dinfo later */
20367 +               dget(a->h_dst);
20368 +
20369 +       return err;
20370 +}
20371 +
20372 +/* cf. aufs_rmdir() */
20373 +static int au_ren_del_whtmp(struct au_ren_args *a)
20374 +{
20375 +       int err;
20376 +       struct inode *dir;
20377 +
20378 +       dir = a->dst_dir;
20379 +       SiMustAnyLock(dir->i_sb);
20380 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20381 +                                    au_sbi(dir->i_sb)->si_dirwh)
20382 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20383 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20384 +               if (unlikely(err))
20385 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20386 +                               "ignored.\n", a->h_dst, err);
20387 +       } else {
20388 +               au_nhash_wh_free(&a->thargs->whlist);
20389 +               a->thargs->whlist = a->whlist;
20390 +               a->whlist.nh_num = 0;
20391 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20392 +               dput(a->h_dst);
20393 +               a->thargs = NULL;
20394 +       }
20395 +
20396 +       return 0;
20397 +}
20398 +
20399 +/* make it 'opaque' dir. */
20400 +static int au_ren_diropq(struct au_ren_args *a)
20401 +{
20402 +       int err;
20403 +       struct dentry *diropq;
20404 +
20405 +       err = 0;
20406 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
20407 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
20408 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20409 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
20410 +       au_hn_imtx_unlock(a->src_hinode);
20411 +       if (IS_ERR(diropq))
20412 +               err = PTR_ERR(diropq);
20413 +       else
20414 +               dput(diropq);
20415 +
20416 +       return err;
20417 +}
20418 +
20419 +static int do_rename(struct au_ren_args *a)
20420 +{
20421 +       int err;
20422 +       struct dentry *d, *h_d;
20423 +
20424 +       /* prepare workqueue args for asynchronous rmdir */
20425 +       h_d = a->dst_h_dentry;
20426 +       if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
20427 +               err = -ENOMEM;
20428 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20429 +               if (unlikely(!a->thargs))
20430 +                       goto out;
20431 +               a->h_dst = dget(h_d);
20432 +       }
20433 +
20434 +       /* create whiteout for src_dentry */
20435 +       if (au_ftest_ren(a->flags, WHSRC)) {
20436 +               a->src_bwh = au_dbwh(a->src_dentry);
20437 +               AuDebugOn(a->src_bwh >= 0);
20438 +               a->src_wh_dentry
20439 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20440 +               err = PTR_ERR(a->src_wh_dentry);
20441 +               if (IS_ERR(a->src_wh_dentry))
20442 +                       goto out_thargs;
20443 +       }
20444 +
20445 +       /* lookup whiteout for dentry */
20446 +       if (au_ftest_ren(a->flags, WHDST)) {
20447 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20448 +                                a->br);
20449 +               err = PTR_ERR(h_d);
20450 +               if (IS_ERR(h_d))
20451 +                       goto out_whsrc;
20452 +               if (d_is_negative(h_d))
20453 +                       dput(h_d);
20454 +               else
20455 +                       a->dst_wh_dentry = h_d;
20456 +       }
20457 +
20458 +       /* rename dentry to tmpwh */
20459 +       if (a->thargs) {
20460 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
20461 +               if (unlikely(err))
20462 +                       goto out_whdst;
20463 +
20464 +               d = a->dst_dentry;
20465 +               au_set_h_dptr(d, a->btgt, NULL);
20466 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
20467 +               if (unlikely(err))
20468 +                       goto out_whtmp;
20469 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
20470 +       }
20471 +
20472 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_bstart != a->btgt);
20473 +
20474 +       /* rename by vfs_rename or cpup */
20475 +       d = a->dst_dentry;
20476 +       if (au_ftest_ren(a->flags, ISDIR)
20477 +           && (a->dst_wh_dentry
20478 +               || au_dbdiropq(d) == a->btgt
20479 +               /* hide the lower to keep xino */
20480 +               || a->btgt < au_dbend(d)
20481 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20482 +               au_fset_ren(a->flags, DIROPQ);
20483 +       err = au_ren_or_cpup(a);
20484 +       if (unlikely(err))
20485 +               /* leave the copied-up one */
20486 +               goto out_whtmp;
20487 +
20488 +       /* make dir opaque */
20489 +       if (au_ftest_ren(a->flags, DIROPQ)) {
20490 +               err = au_ren_diropq(a);
20491 +               if (unlikely(err))
20492 +                       goto out_rename;
20493 +       }
20494 +
20495 +       /* update target timestamps */
20496 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
20497 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20498 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20499 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20500 +
20501 +       /* remove whiteout for dentry */
20502 +       if (a->dst_wh_dentry) {
20503 +               a->h_path.dentry = a->dst_wh_dentry;
20504 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20505 +                                         a->dst_dentry);
20506 +               if (unlikely(err))
20507 +                       goto out_diropq;
20508 +       }
20509 +
20510 +       /* remove whtmp */
20511 +       if (a->thargs)
20512 +               au_ren_del_whtmp(a); /* ignore this error */
20513 +
20514 +       au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
20515 +       err = 0;
20516 +       goto out_success;
20517 +
20518 +out_diropq:
20519 +       if (au_ftest_ren(a->flags, DIROPQ))
20520 +               au_ren_rev_diropq(err, a);
20521 +out_rename:
20522 +       au_ren_rev_rename(err, a);
20523 +       dput(a->h_dst);
20524 +out_whtmp:
20525 +       if (a->thargs)
20526 +               au_ren_rev_whtmp(err, a);
20527 +out_whdst:
20528 +       dput(a->dst_wh_dentry);
20529 +       a->dst_wh_dentry = NULL;
20530 +out_whsrc:
20531 +       if (a->src_wh_dentry)
20532 +               au_ren_rev_whsrc(err, a);
20533 +out_success:
20534 +       dput(a->src_wh_dentry);
20535 +       dput(a->dst_wh_dentry);
20536 +out_thargs:
20537 +       if (a->thargs) {
20538 +               dput(a->h_dst);
20539 +               au_whtmp_rmdir_free(a->thargs);
20540 +               a->thargs = NULL;
20541 +       }
20542 +out:
20543 +       return err;
20544 +}
20545 +
20546 +/* ---------------------------------------------------------------------- */
20547 +
20548 +/*
20549 + * test if @dentry dir can be rename destination or not.
20550 + * success means, it is a logically empty dir.
20551 + */
20552 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20553 +{
20554 +       return au_test_empty(dentry, whlist);
20555 +}
20556 +
20557 +/*
20558 + * test if @dentry dir can be rename source or not.
20559 + * if it can, return 0 and @children is filled.
20560 + * success means,
20561 + * - it is a logically empty dir.
20562 + * - or, it exists on writable branch and has no children including whiteouts
20563 + *       on the lower branch.
20564 + */
20565 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20566 +{
20567 +       int err;
20568 +       unsigned int rdhash;
20569 +       aufs_bindex_t bstart;
20570 +
20571 +       bstart = au_dbstart(dentry);
20572 +       if (bstart != btgt) {
20573 +               struct au_nhash whlist;
20574 +
20575 +               SiMustAnyLock(dentry->d_sb);
20576 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20577 +               if (!rdhash)
20578 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20579 +                                                          dentry));
20580 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20581 +               if (unlikely(err))
20582 +                       goto out;
20583 +               err = au_test_empty(dentry, &whlist);
20584 +               au_nhash_wh_free(&whlist);
20585 +               goto out;
20586 +       }
20587 +
20588 +       if (bstart == au_dbtaildir(dentry))
20589 +               return 0; /* success */
20590 +
20591 +       err = au_test_empty_lower(dentry);
20592 +
20593 +out:
20594 +       if (err == -ENOTEMPTY) {
20595 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20596 +                       " is not supported\n");
20597 +               err = -EXDEV;
20598 +       }
20599 +       return err;
20600 +}
20601 +
20602 +/* side effect: sets whlist and h_dentry */
20603 +static int au_ren_may_dir(struct au_ren_args *a)
20604 +{
20605 +       int err;
20606 +       unsigned int rdhash;
20607 +       struct dentry *d;
20608 +
20609 +       d = a->dst_dentry;
20610 +       SiMustAnyLock(d->d_sb);
20611 +
20612 +       err = 0;
20613 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20614 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20615 +               if (!rdhash)
20616 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20617 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20618 +               if (unlikely(err))
20619 +                       goto out;
20620 +
20621 +               au_set_dbstart(d, a->dst_bstart);
20622 +               err = may_rename_dstdir(d, &a->whlist);
20623 +               au_set_dbstart(d, a->btgt);
20624 +       }
20625 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
20626 +       if (unlikely(err))
20627 +               goto out;
20628 +
20629 +       d = a->src_dentry;
20630 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
20631 +       if (au_ftest_ren(a->flags, ISDIR)) {
20632 +               err = may_rename_srcdir(d, a->btgt);
20633 +               if (unlikely(err)) {
20634 +                       au_nhash_wh_free(&a->whlist);
20635 +                       a->whlist.nh_num = 0;
20636 +               }
20637 +       }
20638 +out:
20639 +       return err;
20640 +}
20641 +
20642 +/* ---------------------------------------------------------------------- */
20643 +
20644 +/*
20645 + * simple tests for rename.
20646 + * following the checks in vfs, plus the parent-child relationship.
20647 + */
20648 +static int au_may_ren(struct au_ren_args *a)
20649 +{
20650 +       int err, isdir;
20651 +       struct inode *h_inode;
20652 +
20653 +       if (a->src_bstart == a->btgt) {
20654 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20655 +                                au_ftest_ren(a->flags, ISDIR));
20656 +               if (unlikely(err))
20657 +                       goto out;
20658 +               err = -EINVAL;
20659 +               if (unlikely(a->src_h_dentry == a->h_trap))
20660 +                       goto out;
20661 +       }
20662 +
20663 +       err = 0;
20664 +       if (a->dst_bstart != a->btgt)
20665 +               goto out;
20666 +
20667 +       err = -ENOTEMPTY;
20668 +       if (unlikely(a->dst_h_dentry == a->h_trap))
20669 +               goto out;
20670 +
20671 +       err = -EIO;
20672 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
20673 +       if (d_really_is_negative(a->dst_dentry)) {
20674 +               if (d_is_negative(a->dst_h_dentry))
20675 +                       err = au_may_add(a->dst_dentry, a->btgt,
20676 +                                        a->dst_h_parent, isdir);
20677 +       } else {
20678 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
20679 +                       goto out;
20680 +               h_inode = d_inode(a->dst_h_dentry);
20681 +               if (h_inode->i_nlink)
20682 +                       err = au_may_del(a->dst_dentry, a->btgt,
20683 +                                        a->dst_h_parent, isdir);
20684 +       }
20685 +
20686 +out:
20687 +       if (unlikely(err == -ENOENT || err == -EEXIST))
20688 +               err = -EIO;
20689 +       AuTraceErr(err);
20690 +       return err;
20691 +}
20692 +
20693 +/* ---------------------------------------------------------------------- */
20694 +
20695 +/*
20696 + * locking order
20697 + * (VFS)
20698 + * - src_dir and dir by lock_rename()
20699 + * - inode if exitsts
20700 + * (aufs)
20701 + * - lock all
20702 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20703 + *     + si_read_lock
20704 + *     + di_write_lock2_child()
20705 + *       + di_write_lock_child()
20706 + *        + ii_write_lock_child()
20707 + *       + di_write_lock_child2()
20708 + *        + ii_write_lock_child2()
20709 + *     + src_parent and parent
20710 + *       + di_write_lock_parent()
20711 + *        + ii_write_lock_parent()
20712 + *       + di_write_lock_parent2()
20713 + *        + ii_write_lock_parent2()
20714 + *   + lower src_dir and dir by vfsub_lock_rename()
20715 + *   + verify the every relationships between child and parent. if any
20716 + *     of them failed, unlock all and return -EBUSY.
20717 + */
20718 +static void au_ren_unlock(struct au_ren_args *a)
20719 +{
20720 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20721 +                           a->dst_h_parent, a->dst_hdir);
20722 +       if (au_ftest_ren(a->flags, MNT_WRITE))
20723 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
20724 +}
20725 +
20726 +static int au_ren_lock(struct au_ren_args *a)
20727 +{
20728 +       int err;
20729 +       unsigned int udba;
20730 +
20731 +       err = 0;
20732 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20733 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
20734 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20735 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
20736 +
20737 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
20738 +       if (unlikely(err))
20739 +               goto out;
20740 +       au_fset_ren(a->flags, MNT_WRITE);
20741 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20742 +                                     a->dst_h_parent, a->dst_hdir);
20743 +       udba = au_opt_udba(a->src_dentry->d_sb);
20744 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
20745 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
20746 +               err = au_busy_or_stale();
20747 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
20748 +               err = au_h_verify(a->src_h_dentry, udba,
20749 +                                 d_inode(a->src_h_parent), a->src_h_parent,
20750 +                                 a->br);
20751 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
20752 +               err = au_h_verify(a->dst_h_dentry, udba,
20753 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
20754 +                                 a->br);
20755 +       if (!err)
20756 +               goto out; /* success */
20757 +
20758 +       err = au_busy_or_stale();
20759 +       au_ren_unlock(a);
20760 +
20761 +out:
20762 +       return err;
20763 +}
20764 +
20765 +/* ---------------------------------------------------------------------- */
20766 +
20767 +static void au_ren_refresh_dir(struct au_ren_args *a)
20768 +{
20769 +       struct inode *dir;
20770 +
20771 +       dir = a->dst_dir;
20772 +       dir->i_version++;
20773 +       if (au_ftest_ren(a->flags, ISDIR)) {
20774 +               /* is this updating defined in POSIX? */
20775 +               au_cpup_attr_timesizes(a->src_inode);
20776 +               au_cpup_attr_nlink(dir, /*force*/1);
20777 +       }
20778 +
20779 +       au_dir_ts(dir, a->btgt);
20780 +
20781 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20782 +               return;
20783 +
20784 +       dir = a->src_dir;
20785 +       dir->i_version++;
20786 +       if (au_ftest_ren(a->flags, ISDIR))
20787 +               au_cpup_attr_nlink(dir, /*force*/1);
20788 +       au_dir_ts(dir, a->btgt);
20789 +}
20790 +
20791 +static void au_ren_refresh(struct au_ren_args *a)
20792 +{
20793 +       aufs_bindex_t bend, bindex;
20794 +       struct dentry *d, *h_d;
20795 +       struct inode *i, *h_i;
20796 +       struct super_block *sb;
20797 +
20798 +       d = a->dst_dentry;
20799 +       d_drop(d);
20800 +       if (a->h_dst)
20801 +               /* already dget-ed by au_ren_or_cpup() */
20802 +               au_set_h_dptr(d, a->btgt, a->h_dst);
20803 +
20804 +       i = a->dst_inode;
20805 +       if (i) {
20806 +               if (!au_ftest_ren(a->flags, ISDIR))
20807 +                       vfsub_drop_nlink(i);
20808 +               else {
20809 +                       vfsub_dead_dir(i);
20810 +                       au_cpup_attr_timesizes(i);
20811 +               }
20812 +               au_update_dbrange(d, /*do_put_zero*/1);
20813 +       } else {
20814 +               bend = a->btgt;
20815 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
20816 +                       au_set_h_dptr(d, bindex, NULL);
20817 +               bend = au_dbend(d);
20818 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
20819 +                       au_set_h_dptr(d, bindex, NULL);
20820 +               au_update_dbrange(d, /*do_put_zero*/0);
20821 +       }
20822 +
20823 +       d = a->src_dentry;
20824 +       au_set_dbwh(d, -1);
20825 +       bend = au_dbend(d);
20826 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20827 +               h_d = au_h_dptr(d, bindex);
20828 +               if (h_d)
20829 +                       au_set_h_dptr(d, bindex, NULL);
20830 +       }
20831 +       au_set_dbend(d, a->btgt);
20832 +
20833 +       sb = d->d_sb;
20834 +       i = a->src_inode;
20835 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
20836 +               return; /* success */
20837 +
20838 +       bend = au_ibend(i);
20839 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20840 +               h_i = au_h_iptr(i, bindex);
20841 +               if (h_i) {
20842 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
20843 +                       /* ignore this error */
20844 +                       au_set_h_iptr(i, bindex, NULL, 0);
20845 +               }
20846 +       }
20847 +       au_set_ibend(i, a->btgt);
20848 +}
20849 +
20850 +/* ---------------------------------------------------------------------- */
20851 +
20852 +/* mainly for link(2) and rename(2) */
20853 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
20854 +{
20855 +       aufs_bindex_t bdiropq, bwh;
20856 +       struct dentry *parent;
20857 +       struct au_branch *br;
20858 +
20859 +       parent = dentry->d_parent;
20860 +       IMustLock(d_inode(parent)); /* dir is locked */
20861 +
20862 +       bdiropq = au_dbdiropq(parent);
20863 +       bwh = au_dbwh(dentry);
20864 +       br = au_sbr(dentry->d_sb, btgt);
20865 +       if (au_br_rdonly(br)
20866 +           || (0 <= bdiropq && bdiropq < btgt)
20867 +           || (0 <= bwh && bwh < btgt))
20868 +               btgt = -1;
20869 +
20870 +       AuDbg("btgt %d\n", btgt);
20871 +       return btgt;
20872 +}
20873 +
20874 +/* sets src_bstart, dst_bstart and btgt */
20875 +static int au_ren_wbr(struct au_ren_args *a)
20876 +{
20877 +       int err;
20878 +       struct au_wr_dir_args wr_dir_args = {
20879 +               /* .force_btgt  = -1, */
20880 +               .flags          = AuWrDir_ADD_ENTRY
20881 +       };
20882 +
20883 +       a->src_bstart = au_dbstart(a->src_dentry);
20884 +       a->dst_bstart = au_dbstart(a->dst_dentry);
20885 +       if (au_ftest_ren(a->flags, ISDIR))
20886 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
20887 +       wr_dir_args.force_btgt = a->src_bstart;
20888 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
20889 +               wr_dir_args.force_btgt = a->dst_bstart;
20890 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
20891 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
20892 +       a->btgt = err;
20893 +
20894 +       return err;
20895 +}
20896 +
20897 +static void au_ren_dt(struct au_ren_args *a)
20898 +{
20899 +       a->h_path.dentry = a->src_h_parent;
20900 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
20901 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
20902 +               a->h_path.dentry = a->dst_h_parent;
20903 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
20904 +       }
20905 +
20906 +       au_fclr_ren(a->flags, DT_DSTDIR);
20907 +       if (!au_ftest_ren(a->flags, ISDIR))
20908 +               return;
20909 +
20910 +       a->h_path.dentry = a->src_h_dentry;
20911 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
20912 +       if (d_is_positive(a->dst_h_dentry)) {
20913 +               au_fset_ren(a->flags, DT_DSTDIR);
20914 +               a->h_path.dentry = a->dst_h_dentry;
20915 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
20916 +       }
20917 +}
20918 +
20919 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
20920 +{
20921 +       struct dentry *h_d;
20922 +       struct mutex *h_mtx;
20923 +
20924 +       au_dtime_revert(a->src_dt + AuPARENT);
20925 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
20926 +               au_dtime_revert(a->dst_dt + AuPARENT);
20927 +
20928 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
20929 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
20930 +               h_mtx = &d_inode(h_d)->i_mutex;
20931 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20932 +               au_dtime_revert(a->src_dt + AuCHILD);
20933 +               mutex_unlock(h_mtx);
20934 +
20935 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
20936 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
20937 +                       h_mtx = &d_inode(h_d)->i_mutex;
20938 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20939 +                       au_dtime_revert(a->dst_dt + AuCHILD);
20940 +                       mutex_unlock(h_mtx);
20941 +               }
20942 +       }
20943 +}
20944 +
20945 +/* ---------------------------------------------------------------------- */
20946 +
20947 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
20948 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
20949 +{
20950 +       int err, flags;
20951 +       /* reduce stack space */
20952 +       struct au_ren_args *a;
20953 +
20954 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
20955 +       IMustLock(_src_dir);
20956 +       IMustLock(_dst_dir);
20957 +
20958 +       err = -ENOMEM;
20959 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
20960 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20961 +       if (unlikely(!a))
20962 +               goto out;
20963 +
20964 +       a->src_dir = _src_dir;
20965 +       a->src_dentry = _src_dentry;
20966 +       a->src_inode = NULL;
20967 +       if (d_really_is_positive(a->src_dentry))
20968 +               a->src_inode = d_inode(a->src_dentry);
20969 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
20970 +       a->dst_dir = _dst_dir;
20971 +       a->dst_dentry = _dst_dentry;
20972 +       a->dst_inode = NULL;
20973 +       if (d_really_is_positive(a->dst_dentry))
20974 +               a->dst_inode = d_inode(a->dst_dentry);
20975 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
20976 +       if (a->dst_inode) {
20977 +               IMustLock(a->dst_inode);
20978 +               au_igrab(a->dst_inode);
20979 +       }
20980 +
20981 +       err = -ENOTDIR;
20982 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
20983 +       if (d_is_dir(a->src_dentry)) {
20984 +               au_fset_ren(a->flags, ISDIR);
20985 +               if (unlikely(d_really_is_positive(a->dst_dentry)
20986 +                            && !d_is_dir(a->dst_dentry)))
20987 +                       goto out_free;
20988 +               flags |= AuLock_DIRS;
20989 +       }
20990 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
20991 +       if (unlikely(err))
20992 +               goto out_free;
20993 +
20994 +       err = au_d_hashed_positive(a->src_dentry);
20995 +       if (unlikely(err))
20996 +               goto out_unlock;
20997 +       err = -ENOENT;
20998 +       if (a->dst_inode) {
20999 +               /*
21000 +                * If it is a dir, VFS unhash dst_dentry before this
21001 +                * function. It means we cannot rely upon d_unhashed().
21002 +                */
21003 +               if (unlikely(!a->dst_inode->i_nlink))
21004 +                       goto out_unlock;
21005 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
21006 +                       err = au_d_hashed_positive(a->dst_dentry);
21007 +                       if (unlikely(err))
21008 +                               goto out_unlock;
21009 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21010 +                       goto out_unlock;
21011 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
21012 +               goto out_unlock;
21013 +
21014 +       /*
21015 +        * is it possible?
21016 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
21017 +        * there may exist a problem somewhere else.
21018 +        */
21019 +       err = -EINVAL;
21020 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
21021 +               goto out_unlock;
21022 +
21023 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
21024 +       di_write_lock_parent(a->dst_parent);
21025 +
21026 +       /* which branch we process */
21027 +       err = au_ren_wbr(a);
21028 +       if (unlikely(err < 0))
21029 +               goto out_parent;
21030 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
21031 +       a->h_path.mnt = au_br_mnt(a->br);
21032 +
21033 +       /* are they available to be renamed */
21034 +       err = au_ren_may_dir(a);
21035 +       if (unlikely(err))
21036 +               goto out_children;
21037 +
21038 +       /* prepare the writable parent dir on the same branch */
21039 +       if (a->dst_bstart == a->btgt) {
21040 +               au_fset_ren(a->flags, WHDST);
21041 +       } else {
21042 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
21043 +               if (unlikely(err))
21044 +                       goto out_children;
21045 +       }
21046 +
21047 +       if (a->src_dir != a->dst_dir) {
21048 +               /*
21049 +                * this temporary unlock is safe,
21050 +                * because both dir->i_mutex are locked.
21051 +                */
21052 +               di_write_unlock(a->dst_parent);
21053 +               di_write_lock_parent(a->src_parent);
21054 +               err = au_wr_dir_need_wh(a->src_dentry,
21055 +                                       au_ftest_ren(a->flags, ISDIR),
21056 +                                       &a->btgt);
21057 +               di_write_unlock(a->src_parent);
21058 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
21059 +               au_fclr_ren(a->flags, ISSAMEDIR);
21060 +       } else
21061 +               err = au_wr_dir_need_wh(a->src_dentry,
21062 +                                       au_ftest_ren(a->flags, ISDIR),
21063 +                                       &a->btgt);
21064 +       if (unlikely(err < 0))
21065 +               goto out_children;
21066 +       if (err)
21067 +               au_fset_ren(a->flags, WHSRC);
21068 +
21069 +       /* cpup src */
21070 +       if (a->src_bstart != a->btgt) {
21071 +               struct au_pin pin;
21072 +
21073 +               err = au_pin(&pin, a->src_dentry, a->btgt,
21074 +                            au_opt_udba(a->src_dentry->d_sb),
21075 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21076 +               if (!err) {
21077 +                       struct au_cp_generic cpg = {
21078 +                               .dentry = a->src_dentry,
21079 +                               .bdst   = a->btgt,
21080 +                               .bsrc   = a->src_bstart,
21081 +                               .len    = -1,
21082 +                               .pin    = &pin,
21083 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21084 +                       };
21085 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
21086 +                       err = au_sio_cpup_simple(&cpg);
21087 +                       au_unpin(&pin);
21088 +               }
21089 +               if (unlikely(err))
21090 +                       goto out_children;
21091 +               a->src_bstart = a->btgt;
21092 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21093 +               au_fset_ren(a->flags, WHSRC);
21094 +       }
21095 +
21096 +       /* lock them all */
21097 +       err = au_ren_lock(a);
21098 +       if (unlikely(err))
21099 +               /* leave the copied-up one */
21100 +               goto out_children;
21101 +
21102 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21103 +               err = au_may_ren(a);
21104 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21105 +               err = -ENAMETOOLONG;
21106 +       if (unlikely(err))
21107 +               goto out_hdir;
21108 +
21109 +       /* store timestamps to be revertible */
21110 +       au_ren_dt(a);
21111 +
21112 +       /* here we go */
21113 +       err = do_rename(a);
21114 +       if (unlikely(err))
21115 +               goto out_dt;
21116 +
21117 +       /* update dir attributes */
21118 +       au_ren_refresh_dir(a);
21119 +
21120 +       /* dput/iput all lower dentries */
21121 +       au_ren_refresh(a);
21122 +
21123 +       goto out_hdir; /* success */
21124 +
21125 +out_dt:
21126 +       au_ren_rev_dt(err, a);
21127 +out_hdir:
21128 +       au_ren_unlock(a);
21129 +out_children:
21130 +       au_nhash_wh_free(&a->whlist);
21131 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
21132 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
21133 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21134 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
21135 +       }
21136 +out_parent:
21137 +       if (!err)
21138 +               d_move(a->src_dentry, a->dst_dentry);
21139 +       else {
21140 +               au_update_dbstart(a->dst_dentry);
21141 +               if (!a->dst_inode)
21142 +                       d_drop(a->dst_dentry);
21143 +       }
21144 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
21145 +               di_write_unlock(a->dst_parent);
21146 +       else
21147 +               di_write_unlock2(a->src_parent, a->dst_parent);
21148 +out_unlock:
21149 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21150 +out_free:
21151 +       iput(a->dst_inode);
21152 +       if (a->thargs)
21153 +               au_whtmp_rmdir_free(a->thargs);
21154 +       kfree(a);
21155 +out:
21156 +       AuTraceErr(err);
21157 +       return err;
21158 +}
21159 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21160 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21161 +++ linux/fs/aufs/Kconfig       2016-01-13 20:11:11.666426853 +0100
21162 @@ -0,0 +1,185 @@
21163 +config AUFS_FS
21164 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21165 +       help
21166 +       Aufs is a stackable unification filesystem such as Unionfs,
21167 +       which unifies several directories and provides a merged single
21168 +       directory.
21169 +       In the early days, aufs was entirely re-designed and
21170 +       re-implemented Unionfs Version 1.x series. Introducing many
21171 +       original ideas, approaches and improvements, it becomes totally
21172 +       different from Unionfs while keeping the basic features.
21173 +
21174 +if AUFS_FS
21175 +choice
21176 +       prompt "Maximum number of branches"
21177 +       default AUFS_BRANCH_MAX_127
21178 +       help
21179 +       Specifies the maximum number of branches (or member directories)
21180 +       in a single aufs. The larger value consumes more system
21181 +       resources and has a minor impact to performance.
21182 +config AUFS_BRANCH_MAX_127
21183 +       bool "127"
21184 +       help
21185 +       Specifies the maximum number of branches (or member directories)
21186 +       in a single aufs. The larger value consumes more system
21187 +       resources and has a minor impact to performance.
21188 +config AUFS_BRANCH_MAX_511
21189 +       bool "511"
21190 +       help
21191 +       Specifies the maximum number of branches (or member directories)
21192 +       in a single aufs. The larger value consumes more system
21193 +       resources and has a minor impact to performance.
21194 +config AUFS_BRANCH_MAX_1023
21195 +       bool "1023"
21196 +       help
21197 +       Specifies the maximum number of branches (or member directories)
21198 +       in a single aufs. The larger value consumes more system
21199 +       resources and has a minor impact to performance.
21200 +config AUFS_BRANCH_MAX_32767
21201 +       bool "32767"
21202 +       help
21203 +       Specifies the maximum number of branches (or member directories)
21204 +       in a single aufs. The larger value consumes more system
21205 +       resources and has a minor impact to performance.
21206 +endchoice
21207 +
21208 +config AUFS_SBILIST
21209 +       bool
21210 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21211 +       default y
21212 +       help
21213 +       Automatic configuration for internal use.
21214 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21215 +
21216 +config AUFS_HNOTIFY
21217 +       bool "Detect direct branch access (bypassing aufs)"
21218 +       help
21219 +       If you want to modify files on branches directly, eg. bypassing aufs,
21220 +       and want aufs to detect the changes of them fully, then enable this
21221 +       option and use 'udba=notify' mount option.
21222 +       Currently there is only one available configuration, "fsnotify".
21223 +       It will have a negative impact to the performance.
21224 +       See detail in aufs.5.
21225 +
21226 +choice
21227 +       prompt "method" if AUFS_HNOTIFY
21228 +       default AUFS_HFSNOTIFY
21229 +config AUFS_HFSNOTIFY
21230 +       bool "fsnotify"
21231 +       select FSNOTIFY
21232 +endchoice
21233 +
21234 +config AUFS_EXPORT
21235 +       bool "NFS-exportable aufs"
21236 +       depends on EXPORTFS
21237 +       help
21238 +       If you want to export your mounted aufs via NFS, then enable this
21239 +       option. There are several requirements for this configuration.
21240 +       See detail in aufs.5.
21241 +
21242 +config AUFS_INO_T_64
21243 +       bool
21244 +       depends on AUFS_EXPORT
21245 +       depends on 64BIT && !(ALPHA || S390)
21246 +       default y
21247 +       help
21248 +       Automatic configuration for internal use.
21249 +       /* typedef unsigned long/int __kernel_ino_t */
21250 +       /* alpha and s390x are int */
21251 +
21252 +config AUFS_XATTR
21253 +       bool "support for XATTR/EA (including Security Labels)"
21254 +       help
21255 +       If your branch fs supports XATTR/EA and you want to make them
21256 +       available in aufs too, then enable this opsion and specify the
21257 +       branch attributes for EA.
21258 +       See detail in aufs.5.
21259 +
21260 +config AUFS_FHSM
21261 +       bool "File-based Hierarchical Storage Management"
21262 +       help
21263 +       Hierarchical Storage Management (or HSM) is a well-known feature
21264 +       in the storage world. Aufs provides this feature as file-based.
21265 +       with multiple branches.
21266 +       These multiple branches are prioritized, ie. the topmost one
21267 +       should be the fastest drive and be used heavily.
21268 +
21269 +config AUFS_RDU
21270 +       bool "Readdir in userspace"
21271 +       help
21272 +       Aufs has two methods to provide a merged view for a directory,
21273 +       by a user-space library and by kernel-space natively. The latter
21274 +       is always enabled but sometimes large and slow.
21275 +       If you enable this option, install the library in aufs2-util
21276 +       package, and set some environment variables for your readdir(3),
21277 +       then the work will be handled in user-space which generally
21278 +       shows better performance in most cases.
21279 +       See detail in aufs.5.
21280 +
21281 +config AUFS_SHWH
21282 +       bool "Show whiteouts"
21283 +       help
21284 +       If you want to make the whiteouts in aufs visible, then enable
21285 +       this option and specify 'shwh' mount option. Although it may
21286 +       sounds like philosophy or something, but in technically it
21287 +       simply shows the name of whiteout with keeping its behaviour.
21288 +
21289 +config AUFS_BR_RAMFS
21290 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21291 +       help
21292 +       If you want to use ramfs as an aufs branch fs, then enable this
21293 +       option. Generally tmpfs is recommended.
21294 +       Aufs prohibited them to be a branch fs by default, because
21295 +       initramfs becomes unusable after switch_root or something
21296 +       generally. If you sets initramfs as an aufs branch and boot your
21297 +       system by switch_root, you will meet a problem easily since the
21298 +       files in initramfs may be inaccessible.
21299 +       Unless you are going to use ramfs as an aufs branch fs without
21300 +       switch_root or something, leave it N.
21301 +
21302 +config AUFS_BR_FUSE
21303 +       bool "Fuse fs as an aufs branch"
21304 +       depends on FUSE_FS
21305 +       select AUFS_POLL
21306 +       help
21307 +       If you want to use fuse-based userspace filesystem as an aufs
21308 +       branch fs, then enable this option.
21309 +       It implements the internal poll(2) operation which is
21310 +       implemented by fuse only (curretnly).
21311 +
21312 +config AUFS_POLL
21313 +       bool
21314 +       help
21315 +       Automatic configuration for internal use.
21316 +
21317 +config AUFS_BR_HFSPLUS
21318 +       bool "Hfsplus as an aufs branch"
21319 +       depends on HFSPLUS_FS
21320 +       default y
21321 +       help
21322 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21323 +       this option. This option introduces a small overhead at
21324 +       copying-up a file on hfsplus.
21325 +
21326 +config AUFS_BDEV_LOOP
21327 +       bool
21328 +       depends on BLK_DEV_LOOP
21329 +       default y
21330 +       help
21331 +       Automatic configuration for internal use.
21332 +       Convert =[ym] into =y.
21333 +
21334 +config AUFS_DEBUG
21335 +       bool "Debug aufs"
21336 +       help
21337 +       Enable this to compile aufs internal debug code.
21338 +       It will have a negative impact to the performance.
21339 +
21340 +config AUFS_MAGIC_SYSRQ
21341 +       bool
21342 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21343 +       default y
21344 +       help
21345 +       Automatic configuration for internal use.
21346 +       When aufs supports Magic SysRq, enabled automatically.
21347 +endif
21348 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21349 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21350 +++ linux/fs/aufs/loop.c        2016-01-13 20:11:11.669760262 +0100
21351 @@ -0,0 +1,146 @@
21352 +/*
21353 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21354 + *
21355 + * This program, aufs is free software; you can redistribute it and/or modify
21356 + * it under the terms of the GNU General Public License as published by
21357 + * the Free Software Foundation; either version 2 of the License, or
21358 + * (at your option) any later version.
21359 + *
21360 + * This program is distributed in the hope that it will be useful,
21361 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21362 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21363 + * GNU General Public License for more details.
21364 + *
21365 + * You should have received a copy of the GNU General Public License
21366 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21367 + */
21368 +
21369 +/*
21370 + * support for loopback block device as a branch
21371 + */
21372 +
21373 +#include "aufs.h"
21374 +
21375 +/* added into drivers/block/loop.c */
21376 +static struct file *(*backing_file_func)(struct super_block *sb);
21377 +
21378 +/*
21379 + * test if two lower dentries have overlapping branches.
21380 + */
21381 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21382 +{
21383 +       struct super_block *h_sb;
21384 +       struct file *backing_file;
21385 +
21386 +       if (unlikely(!backing_file_func)) {
21387 +               /* don't load "loop" module here */
21388 +               backing_file_func = symbol_get(loop_backing_file);
21389 +               if (unlikely(!backing_file_func))
21390 +                       /* "loop" module is not loaded */
21391 +                       return 0;
21392 +       }
21393 +
21394 +       h_sb = h_adding->d_sb;
21395 +       backing_file = backing_file_func(h_sb);
21396 +       if (!backing_file)
21397 +               return 0;
21398 +
21399 +       h_adding = backing_file->f_path.dentry;
21400 +       /*
21401 +        * h_adding can be local NFS.
21402 +        * in this case aufs cannot detect the loop.
21403 +        */
21404 +       if (unlikely(h_adding->d_sb == sb))
21405 +               return 1;
21406 +       return !!au_test_subdir(h_adding, sb->s_root);
21407 +}
21408 +
21409 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21410 +int au_test_loopback_kthread(void)
21411 +{
21412 +       int ret;
21413 +       struct task_struct *tsk = current;
21414 +       char c, comm[sizeof(tsk->comm)];
21415 +
21416 +       ret = 0;
21417 +       if (tsk->flags & PF_KTHREAD) {
21418 +               get_task_comm(comm, tsk);
21419 +               c = comm[4];
21420 +               ret = ('0' <= c && c <= '9'
21421 +                      && !strncmp(comm, "loop", 4));
21422 +       }
21423 +
21424 +       return ret;
21425 +}
21426 +
21427 +/* ---------------------------------------------------------------------- */
21428 +
21429 +#define au_warn_loopback_step  16
21430 +static int au_warn_loopback_nelem = au_warn_loopback_step;
21431 +static unsigned long *au_warn_loopback_array;
21432 +
21433 +void au_warn_loopback(struct super_block *h_sb)
21434 +{
21435 +       int i, new_nelem;
21436 +       unsigned long *a, magic;
21437 +       static DEFINE_SPINLOCK(spin);
21438 +
21439 +       magic = h_sb->s_magic;
21440 +       spin_lock(&spin);
21441 +       a = au_warn_loopback_array;
21442 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21443 +               if (a[i] == magic) {
21444 +                       spin_unlock(&spin);
21445 +                       return;
21446 +               }
21447 +
21448 +       /* h_sb is new to us, print it */
21449 +       if (i < au_warn_loopback_nelem) {
21450 +               a[i] = magic;
21451 +               goto pr;
21452 +       }
21453 +
21454 +       /* expand the array */
21455 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21456 +       a = au_kzrealloc(au_warn_loopback_array,
21457 +                        au_warn_loopback_nelem * sizeof(unsigned long),
21458 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21459 +       if (a) {
21460 +               au_warn_loopback_nelem = new_nelem;
21461 +               au_warn_loopback_array = a;
21462 +               a[i] = magic;
21463 +               goto pr;
21464 +       }
21465 +
21466 +       spin_unlock(&spin);
21467 +       AuWarn1("realloc failed, ignored\n");
21468 +       return;
21469 +
21470 +pr:
21471 +       spin_unlock(&spin);
21472 +       pr_warn("you may want to try another patch for loopback file "
21473 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
21474 +}
21475 +
21476 +int au_loopback_init(void)
21477 +{
21478 +       int err;
21479 +       struct super_block *sb __maybe_unused;
21480 +
21481 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
21482 +
21483 +       err = 0;
21484 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21485 +                                        sizeof(unsigned long), GFP_NOFS);
21486 +       if (unlikely(!au_warn_loopback_array))
21487 +               err = -ENOMEM;
21488 +
21489 +       return err;
21490 +}
21491 +
21492 +void au_loopback_fin(void)
21493 +{
21494 +       if (backing_file_func)
21495 +               symbol_put(loop_backing_file);
21496 +       kfree(au_warn_loopback_array);
21497 +}
21498 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21499 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
21500 +++ linux/fs/aufs/loop.h        2016-01-13 20:11:11.669760262 +0100
21501 @@ -0,0 +1,52 @@
21502 +/*
21503 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21504 + *
21505 + * This program, aufs is free software; you can redistribute it and/or modify
21506 + * it under the terms of the GNU General Public License as published by
21507 + * the Free Software Foundation; either version 2 of the License, or
21508 + * (at your option) any later version.
21509 + *
21510 + * This program is distributed in the hope that it will be useful,
21511 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21512 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21513 + * GNU General Public License for more details.
21514 + *
21515 + * You should have received a copy of the GNU General Public License
21516 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21517 + */
21518 +
21519 +/*
21520 + * support for loopback mount as a branch
21521 + */
21522 +
21523 +#ifndef __AUFS_LOOP_H__
21524 +#define __AUFS_LOOP_H__
21525 +
21526 +#ifdef __KERNEL__
21527 +
21528 +struct dentry;
21529 +struct super_block;
21530 +
21531 +#ifdef CONFIG_AUFS_BDEV_LOOP
21532 +/* drivers/block/loop.c */
21533 +struct file *loop_backing_file(struct super_block *sb);
21534 +
21535 +/* loop.c */
21536 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
21537 +int au_test_loopback_kthread(void);
21538 +void au_warn_loopback(struct super_block *h_sb);
21539 +
21540 +int au_loopback_init(void);
21541 +void au_loopback_fin(void);
21542 +#else
21543 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21544 +          struct dentry *h_adding)
21545 +AuStubInt0(au_test_loopback_kthread, void)
21546 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21547 +
21548 +AuStubInt0(au_loopback_init, void)
21549 +AuStubVoid(au_loopback_fin, void)
21550 +#endif /* BLK_DEV_LOOP */
21551 +
21552 +#endif /* __KERNEL__ */
21553 +#endif /* __AUFS_LOOP_H__ */
21554 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21555 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21556 +++ linux/fs/aufs/magic.mk      2016-01-13 20:11:11.669760262 +0100
21557 @@ -0,0 +1,30 @@
21558 +
21559 +# defined in ${srctree}/fs/fuse/inode.c
21560 +# tristate
21561 +ifdef CONFIG_FUSE_FS
21562 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21563 +endif
21564 +
21565 +# defined in ${srctree}/fs/xfs/xfs_sb.h
21566 +# tristate
21567 +ifdef CONFIG_XFS_FS
21568 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
21569 +endif
21570 +
21571 +# defined in ${srctree}/fs/configfs/mount.c
21572 +# tristate
21573 +ifdef CONFIG_CONFIGFS_FS
21574 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21575 +endif
21576 +
21577 +# defined in ${srctree}/fs/ubifs/ubifs.h
21578 +# tristate
21579 +ifdef CONFIG_UBIFS_FS
21580 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21581 +endif
21582 +
21583 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21584 +# tristate
21585 +ifdef CONFIG_HFSPLUS_FS
21586 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21587 +endif
21588 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21589 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
21590 +++ linux/fs/aufs/Makefile      2016-01-13 20:11:11.666426853 +0100
21591 @@ -0,0 +1,44 @@
21592 +
21593 +include ${src}/magic.mk
21594 +ifeq (${CONFIG_AUFS_FS},m)
21595 +include ${src}/conf.mk
21596 +endif
21597 +-include ${src}/priv_def.mk
21598 +
21599 +# cf. include/linux/kernel.h
21600 +# enable pr_debug
21601 +ccflags-y += -DDEBUG
21602 +# sparse requires the full pathname
21603 +ifdef M
21604 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
21605 +else
21606 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
21607 +endif
21608 +
21609 +obj-$(CONFIG_AUFS_FS) += aufs.o
21610 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21611 +       wkq.o vfsub.o dcsub.o \
21612 +       cpup.o whout.o wbr_policy.o \
21613 +       dinfo.o dentry.o \
21614 +       dynop.o \
21615 +       finfo.o file.o f_op.o \
21616 +       dir.o vdir.o \
21617 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
21618 +       mvdown.o ioctl.o
21619 +
21620 +# all are boolean
21621 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
21622 +aufs-$(CONFIG_SYSFS) += sysfs.o
21623 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21624 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21625 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21626 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
21627 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
21628 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21629 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
21630 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
21631 +aufs-$(CONFIG_AUFS_POLL) += poll.o
21632 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
21633 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21634 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21635 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
21636 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21637 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
21638 +++ linux/fs/aufs/module.c      2016-01-13 20:11:11.669760262 +0100
21639 @@ -0,0 +1,221 @@
21640 +/*
21641 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21642 + *
21643 + * This program, aufs is free software; you can redistribute it and/or modify
21644 + * it under the terms of the GNU General Public License as published by
21645 + * the Free Software Foundation; either version 2 of the License, or
21646 + * (at your option) any later version.
21647 + *
21648 + * This program is distributed in the hope that it will be useful,
21649 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21650 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21651 + * GNU General Public License for more details.
21652 + *
21653 + * You should have received a copy of the GNU General Public License
21654 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21655 + */
21656 +
21657 +/*
21658 + * module global variables and operations
21659 + */
21660 +
21661 +#include <linux/module.h>
21662 +#include <linux/seq_file.h>
21663 +#include "aufs.h"
21664 +
21665 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21666 +{
21667 +       if (new_sz <= nused)
21668 +               return p;
21669 +
21670 +       p = krealloc(p, new_sz, gfp);
21671 +       if (p)
21672 +               memset(p + nused, 0, new_sz - nused);
21673 +       return p;
21674 +}
21675 +
21676 +/* ---------------------------------------------------------------------- */
21677 +
21678 +/*
21679 + * aufs caches
21680 + */
21681 +struct kmem_cache *au_cachep[AuCache_Last];
21682 +static int __init au_cache_init(void)
21683 +{
21684 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
21685 +       if (au_cachep[AuCache_DINFO])
21686 +               /* SLAB_DESTROY_BY_RCU */
21687 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21688 +                                                       au_icntnr_init_once);
21689 +       if (au_cachep[AuCache_ICNTNR])
21690 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21691 +                                                      au_fi_init_once);
21692 +       if (au_cachep[AuCache_FINFO])
21693 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21694 +       if (au_cachep[AuCache_VDIR])
21695 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21696 +       if (au_cachep[AuCache_DEHSTR])
21697 +               return 0;
21698 +
21699 +       return -ENOMEM;
21700 +}
21701 +
21702 +static void au_cache_fin(void)
21703 +{
21704 +       int i;
21705 +
21706 +       /*
21707 +        * Make sure all delayed rcu free inodes are flushed before we
21708 +        * destroy cache.
21709 +        */
21710 +       rcu_barrier();
21711 +
21712 +       /* excluding AuCache_HNOTIFY */
21713 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21714 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
21715 +               kmem_cache_destroy(au_cachep[i]);
21716 +               au_cachep[i] = NULL;
21717 +       }
21718 +}
21719 +
21720 +/* ---------------------------------------------------------------------- */
21721 +
21722 +int au_dir_roflags;
21723 +
21724 +#ifdef CONFIG_AUFS_SBILIST
21725 +/*
21726 + * iterate_supers_type() doesn't protect us from
21727 + * remounting (branch management)
21728 + */
21729 +struct au_splhead au_sbilist;
21730 +#endif
21731 +
21732 +struct lock_class_key au_lc_key[AuLcKey_Last];
21733 +
21734 +/*
21735 + * functions for module interface.
21736 + */
21737 +MODULE_LICENSE("GPL");
21738 +/* MODULE_LICENSE("GPL v2"); */
21739 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
21740 +MODULE_DESCRIPTION(AUFS_NAME
21741 +       " -- Advanced multi layered unification filesystem");
21742 +MODULE_VERSION(AUFS_VERSION);
21743 +MODULE_ALIAS_FS(AUFS_NAME);
21744 +
21745 +/* this module parameter has no meaning when SYSFS is disabled */
21746 +int sysaufs_brs = 1;
21747 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21748 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21749 +
21750 +/* this module parameter has no meaning when USER_NS is disabled */
21751 +static bool au_userns;
21752 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21753 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21754 +
21755 +/* ---------------------------------------------------------------------- */
21756 +
21757 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21758 +
21759 +int au_seq_path(struct seq_file *seq, struct path *path)
21760 +{
21761 +       int err;
21762 +
21763 +       err = seq_path(seq, path, au_esc_chars);
21764 +       if (err > 0)
21765 +               err = 0;
21766 +       else if (err < 0)
21767 +               err = -ENOMEM;
21768 +
21769 +       return err;
21770 +}
21771 +
21772 +/* ---------------------------------------------------------------------- */
21773 +
21774 +static int __init aufs_init(void)
21775 +{
21776 +       int err, i;
21777 +       char *p;
21778 +
21779 +       p = au_esc_chars;
21780 +       for (i = 1; i <= ' '; i++)
21781 +               *p++ = i;
21782 +       *p++ = '\\';
21783 +       *p++ = '\x7f';
21784 +       *p = 0;
21785 +
21786 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21787 +
21788 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
21789 +       for (i = 0; i < AuIop_Last; i++)
21790 +               aufs_iop_nogetattr[i].getattr = NULL;
21791 +
21792 +       au_sbilist_init();
21793 +       sysaufs_brs_init();
21794 +       au_debug_init();
21795 +       au_dy_init();
21796 +       err = sysaufs_init();
21797 +       if (unlikely(err))
21798 +               goto out;
21799 +       err = au_procfs_init();
21800 +       if (unlikely(err))
21801 +               goto out_sysaufs;
21802 +       err = au_wkq_init();
21803 +       if (unlikely(err))
21804 +               goto out_procfs;
21805 +       err = au_loopback_init();
21806 +       if (unlikely(err))
21807 +               goto out_wkq;
21808 +       err = au_hnotify_init();
21809 +       if (unlikely(err))
21810 +               goto out_loopback;
21811 +       err = au_sysrq_init();
21812 +       if (unlikely(err))
21813 +               goto out_hin;
21814 +       err = au_cache_init();
21815 +       if (unlikely(err))
21816 +               goto out_sysrq;
21817 +
21818 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
21819 +       err = register_filesystem(&aufs_fs_type);
21820 +       if (unlikely(err))
21821 +               goto out_cache;
21822 +
21823 +       /* since we define pr_fmt, call printk directly */
21824 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
21825 +       goto out; /* success */
21826 +
21827 +out_cache:
21828 +       au_cache_fin();
21829 +out_sysrq:
21830 +       au_sysrq_fin();
21831 +out_hin:
21832 +       au_hnotify_fin();
21833 +out_loopback:
21834 +       au_loopback_fin();
21835 +out_wkq:
21836 +       au_wkq_fin();
21837 +out_procfs:
21838 +       au_procfs_fin();
21839 +out_sysaufs:
21840 +       sysaufs_fin();
21841 +       au_dy_fin();
21842 +out:
21843 +       return err;
21844 +}
21845 +
21846 +static void __exit aufs_exit(void)
21847 +{
21848 +       unregister_filesystem(&aufs_fs_type);
21849 +       au_cache_fin();
21850 +       au_sysrq_fin();
21851 +       au_hnotify_fin();
21852 +       au_loopback_fin();
21853 +       au_wkq_fin();
21854 +       au_procfs_fin();
21855 +       sysaufs_fin();
21856 +       au_dy_fin();
21857 +}
21858 +
21859 +module_init(aufs_init);
21860 +module_exit(aufs_exit);
21861 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
21862 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
21863 +++ linux/fs/aufs/module.h      2016-01-13 20:11:11.669760262 +0100
21864 @@ -0,0 +1,104 @@
21865 +/*
21866 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21867 + *
21868 + * This program, aufs is free software; you can redistribute it and/or modify
21869 + * it under the terms of the GNU General Public License as published by
21870 + * the Free Software Foundation; either version 2 of the License, or
21871 + * (at your option) any later version.
21872 + *
21873 + * This program is distributed in the hope that it will be useful,
21874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21876 + * GNU General Public License for more details.
21877 + *
21878 + * You should have received a copy of the GNU General Public License
21879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21880 + */
21881 +
21882 +/*
21883 + * module initialization and module-global
21884 + */
21885 +
21886 +#ifndef __AUFS_MODULE_H__
21887 +#define __AUFS_MODULE_H__
21888 +
21889 +#ifdef __KERNEL__
21890 +
21891 +#include <linux/slab.h>
21892 +
21893 +struct path;
21894 +struct seq_file;
21895 +
21896 +/* module parameters */
21897 +extern int sysaufs_brs;
21898 +
21899 +/* ---------------------------------------------------------------------- */
21900 +
21901 +extern int au_dir_roflags;
21902 +
21903 +enum {
21904 +       AuLcNonDir_FIINFO,
21905 +       AuLcNonDir_DIINFO,
21906 +       AuLcNonDir_IIINFO,
21907 +
21908 +       AuLcDir_FIINFO,
21909 +       AuLcDir_DIINFO,
21910 +       AuLcDir_IIINFO,
21911 +
21912 +       AuLcSymlink_DIINFO,
21913 +       AuLcSymlink_IIINFO,
21914 +
21915 +       AuLcKey_Last
21916 +};
21917 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
21918 +
21919 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
21920 +int au_seq_path(struct seq_file *seq, struct path *path);
21921 +
21922 +#ifdef CONFIG_PROC_FS
21923 +/* procfs.c */
21924 +int __init au_procfs_init(void);
21925 +void au_procfs_fin(void);
21926 +#else
21927 +AuStubInt0(au_procfs_init, void);
21928 +AuStubVoid(au_procfs_fin, void);
21929 +#endif
21930 +
21931 +/* ---------------------------------------------------------------------- */
21932 +
21933 +/* kmem cache */
21934 +enum {
21935 +       AuCache_DINFO,
21936 +       AuCache_ICNTNR,
21937 +       AuCache_FINFO,
21938 +       AuCache_VDIR,
21939 +       AuCache_DEHSTR,
21940 +       AuCache_HNOTIFY, /* must be last */
21941 +       AuCache_Last
21942 +};
21943 +
21944 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
21945 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
21946 +#define AuCacheCtor(type, ctor)        \
21947 +       kmem_cache_create(#type, sizeof(struct type), \
21948 +                         __alignof__(struct type), AuCacheFlags, ctor)
21949 +
21950 +extern struct kmem_cache *au_cachep[];
21951 +
21952 +#define AuCacheFuncs(name, index) \
21953 +static inline struct au_##name *au_cache_alloc_##name(void) \
21954 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
21955 +static inline void au_cache_free_##name(struct au_##name *p) \
21956 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
21957 +
21958 +AuCacheFuncs(dinfo, DINFO);
21959 +AuCacheFuncs(icntnr, ICNTNR);
21960 +AuCacheFuncs(finfo, FINFO);
21961 +AuCacheFuncs(vdir, VDIR);
21962 +AuCacheFuncs(vdir_dehstr, DEHSTR);
21963 +#ifdef CONFIG_AUFS_HNOTIFY
21964 +AuCacheFuncs(hnotify, HNOTIFY);
21965 +#endif
21966 +
21967 +#endif /* __KERNEL__ */
21968 +#endif /* __AUFS_MODULE_H__ */
21969 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
21970 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
21971 +++ linux/fs/aufs/mvdown.c      2016-01-13 20:11:11.669760262 +0100
21972 @@ -0,0 +1,703 @@
21973 +/*
21974 + * Copyright (C) 2011-2015 Junjiro R. Okajima
21975 + *
21976 + * This program, aufs is free software; you can redistribute it and/or modify
21977 + * it under the terms of the GNU General Public License as published by
21978 + * the Free Software Foundation; either version 2 of the License, or
21979 + * (at your option) any later version.
21980 + *
21981 + * This program is distributed in the hope that it will be useful,
21982 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21983 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21984 + * GNU General Public License for more details.
21985 + *
21986 + * You should have received a copy of the GNU General Public License
21987 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21988 + */
21989 +
21990 +/*
21991 + * move-down, opposite of copy-up
21992 + */
21993 +
21994 +#include "aufs.h"
21995 +
21996 +struct au_mvd_args {
21997 +       struct {
21998 +               struct super_block *h_sb;
21999 +               struct dentry *h_parent;
22000 +               struct au_hinode *hdir;
22001 +               struct inode *h_dir, *h_inode;
22002 +               struct au_pin pin;
22003 +       } info[AUFS_MVDOWN_NARRAY];
22004 +
22005 +       struct aufs_mvdown mvdown;
22006 +       struct dentry *dentry, *parent;
22007 +       struct inode *inode, *dir;
22008 +       struct super_block *sb;
22009 +       aufs_bindex_t bopq, bwh, bfound;
22010 +       unsigned char rename_lock;
22011 +};
22012 +
22013 +#define mvd_errno              mvdown.au_errno
22014 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22015 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22016 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22017 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
22018 +
22019 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
22020 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
22021 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
22022 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
22023 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
22024 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
22025 +
22026 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
22027 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
22028 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
22029 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
22030 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
22031 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
22032 +
22033 +#define AU_MVD_PR(flag, ...) do {                      \
22034 +               if (flag)                               \
22035 +                       pr_err(__VA_ARGS__);            \
22036 +       } while (0)
22037 +
22038 +static int find_lower_writable(struct au_mvd_args *a)
22039 +{
22040 +       struct super_block *sb;
22041 +       aufs_bindex_t bindex, bend;
22042 +       struct au_branch *br;
22043 +
22044 +       sb = a->sb;
22045 +       bindex = a->mvd_bsrc;
22046 +       bend = au_sbend(sb);
22047 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
22048 +               for (bindex++; bindex <= bend; bindex++) {
22049 +                       br = au_sbr(sb, bindex);
22050 +                       if (au_br_fhsm(br->br_perm)
22051 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22052 +                               return bindex;
22053 +               }
22054 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
22055 +               for (bindex++; bindex <= bend; bindex++) {
22056 +                       br = au_sbr(sb, bindex);
22057 +                       if (!au_br_rdonly(br))
22058 +                               return bindex;
22059 +               }
22060 +       else
22061 +               for (bindex++; bindex <= bend; bindex++) {
22062 +                       br = au_sbr(sb, bindex);
22063 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22064 +                               if (au_br_rdonly(br))
22065 +                                       a->mvdown.flags
22066 +                                               |= AUFS_MVDOWN_ROLOWER_R;
22067 +                               return bindex;
22068 +                       }
22069 +               }
22070 +
22071 +       return -1;
22072 +}
22073 +
22074 +/* make the parent dir on bdst */
22075 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
22076 +{
22077 +       int err;
22078 +
22079 +       err = 0;
22080 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22081 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22082 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22083 +       a->mvd_h_dst_parent = NULL;
22084 +       if (au_dbend(a->parent) >= a->mvd_bdst)
22085 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22086 +       if (!a->mvd_h_dst_parent) {
22087 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22088 +               if (unlikely(err)) {
22089 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
22090 +                       goto out;
22091 +               }
22092 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22093 +       }
22094 +
22095 +out:
22096 +       AuTraceErr(err);
22097 +       return err;
22098 +}
22099 +
22100 +/* lock them all */
22101 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
22102 +{
22103 +       int err;
22104 +       struct dentry *h_trap;
22105 +
22106 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22107 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
22108 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22109 +                    au_opt_udba(a->sb),
22110 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22111 +       AuTraceErr(err);
22112 +       if (unlikely(err)) {
22113 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
22114 +               goto out;
22115 +       }
22116 +
22117 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22118 +               a->rename_lock = 0;
22119 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22120 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22121 +                           au_opt_udba(a->sb),
22122 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22123 +               err = au_do_pin(&a->mvd_pin_src);
22124 +               AuTraceErr(err);
22125 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22126 +               if (unlikely(err)) {
22127 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22128 +                       goto out_dst;
22129 +               }
22130 +               goto out; /* success */
22131 +       }
22132 +
22133 +       a->rename_lock = 1;
22134 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22135 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22136 +                    au_opt_udba(a->sb),
22137 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22138 +       AuTraceErr(err);
22139 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22140 +       if (unlikely(err)) {
22141 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22142 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22143 +               goto out_dst;
22144 +       }
22145 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22146 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22147 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22148 +       if (h_trap) {
22149 +               err = (h_trap != a->mvd_h_src_parent);
22150 +               if (err)
22151 +                       err = (h_trap != a->mvd_h_dst_parent);
22152 +       }
22153 +       BUG_ON(err); /* it should never happen */
22154 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22155 +               err = -EBUSY;
22156 +               AuTraceErr(err);
22157 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22158 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22159 +               au_pin_hdir_lock(&a->mvd_pin_src);
22160 +               au_unpin(&a->mvd_pin_src);
22161 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22162 +               goto out_dst;
22163 +       }
22164 +       goto out; /* success */
22165 +
22166 +out_dst:
22167 +       au_unpin(&a->mvd_pin_dst);
22168 +out:
22169 +       AuTraceErr(err);
22170 +       return err;
22171 +}
22172 +
22173 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22174 +{
22175 +       if (!a->rename_lock)
22176 +               au_unpin(&a->mvd_pin_src);
22177 +       else {
22178 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22179 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22180 +               au_pin_hdir_lock(&a->mvd_pin_src);
22181 +               au_unpin(&a->mvd_pin_src);
22182 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22183 +       }
22184 +       au_unpin(&a->mvd_pin_dst);
22185 +}
22186 +
22187 +/* copy-down the file */
22188 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22189 +{
22190 +       int err;
22191 +       struct au_cp_generic cpg = {
22192 +               .dentry = a->dentry,
22193 +               .bdst   = a->mvd_bdst,
22194 +               .bsrc   = a->mvd_bsrc,
22195 +               .len    = -1,
22196 +               .pin    = &a->mvd_pin_dst,
22197 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22198 +       };
22199 +
22200 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22201 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22202 +               au_fset_cpup(cpg.flags, OVERWRITE);
22203 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22204 +               au_fset_cpup(cpg.flags, RWDST);
22205 +       err = au_sio_cpdown_simple(&cpg);
22206 +       if (unlikely(err))
22207 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22208 +
22209 +       AuTraceErr(err);
22210 +       return err;
22211 +}
22212 +
22213 +/*
22214 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22215 + * were sleeping
22216 + */
22217 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22218 +{
22219 +       int err;
22220 +       struct path h_path;
22221 +       struct au_branch *br;
22222 +       struct inode *delegated;
22223 +
22224 +       br = au_sbr(a->sb, a->mvd_bdst);
22225 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22226 +       err = PTR_ERR(h_path.dentry);
22227 +       if (IS_ERR(h_path.dentry)) {
22228 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22229 +               goto out;
22230 +       }
22231 +
22232 +       err = 0;
22233 +       if (d_is_positive(h_path.dentry)) {
22234 +               h_path.mnt = au_br_mnt(br);
22235 +               delegated = NULL;
22236 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
22237 +                                  &delegated, /*force*/0);
22238 +               if (unlikely(err == -EWOULDBLOCK)) {
22239 +                       pr_warn("cannot retry for NFSv4 delegation"
22240 +                               " for an internal unlink\n");
22241 +                       iput(delegated);
22242 +               }
22243 +               if (unlikely(err))
22244 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22245 +       }
22246 +       dput(h_path.dentry);
22247 +
22248 +out:
22249 +       AuTraceErr(err);
22250 +       return err;
22251 +}
22252 +
22253 +/*
22254 + * unlink the topmost h_dentry
22255 + */
22256 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22257 +{
22258 +       int err;
22259 +       struct path h_path;
22260 +       struct inode *delegated;
22261 +
22262 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22263 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22264 +       delegated = NULL;
22265 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22266 +       if (unlikely(err == -EWOULDBLOCK)) {
22267 +               pr_warn("cannot retry for NFSv4 delegation"
22268 +                       " for an internal unlink\n");
22269 +               iput(delegated);
22270 +       }
22271 +       if (unlikely(err))
22272 +               AU_MVD_PR(dmsg, "unlink failed\n");
22273 +
22274 +       AuTraceErr(err);
22275 +       return err;
22276 +}
22277 +
22278 +/* Since mvdown succeeded, we ignore an error of this function */
22279 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22280 +{
22281 +       int err;
22282 +       struct au_branch *br;
22283 +
22284 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22285 +       br = au_sbr(a->sb, a->mvd_bsrc);
22286 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22287 +       if (!err) {
22288 +               br = au_sbr(a->sb, a->mvd_bdst);
22289 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22290 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22291 +       }
22292 +       if (!err)
22293 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22294 +       else
22295 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22296 +}
22297 +
22298 +/*
22299 + * copy-down the file and unlink the bsrc file.
22300 + * - unlink the bdst whout if exist
22301 + * - copy-down the file (with whtmp name and rename)
22302 + * - unlink the bsrc file
22303 + */
22304 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22305 +{
22306 +       int err;
22307 +
22308 +       err = au_do_mkdir(dmsg, a);
22309 +       if (!err)
22310 +               err = au_do_lock(dmsg, a);
22311 +       if (unlikely(err))
22312 +               goto out;
22313 +
22314 +       /*
22315 +        * do not revert the activities we made on bdst since they should be
22316 +        * harmless in aufs.
22317 +        */
22318 +
22319 +       err = au_do_cpdown(dmsg, a);
22320 +       if (!err)
22321 +               err = au_do_unlink_wh(dmsg, a);
22322 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22323 +               err = au_do_unlink(dmsg, a);
22324 +       if (unlikely(err))
22325 +               goto out_unlock;
22326 +
22327 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22328 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22329 +       if (find_lower_writable(a) < 0)
22330 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22331 +
22332 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22333 +               au_do_stfs(dmsg, a);
22334 +
22335 +       /* maintain internal array */
22336 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22337 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22338 +               au_set_dbstart(a->dentry, a->mvd_bdst);
22339 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22340 +               au_set_ibstart(a->inode, a->mvd_bdst);
22341 +       } else {
22342 +               /* hide the lower */
22343 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
22344 +               au_set_dbend(a->dentry, a->mvd_bsrc);
22345 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
22346 +               au_set_ibend(a->inode, a->mvd_bsrc);
22347 +       }
22348 +       if (au_dbend(a->dentry) < a->mvd_bdst)
22349 +               au_set_dbend(a->dentry, a->mvd_bdst);
22350 +       if (au_ibend(a->inode) < a->mvd_bdst)
22351 +               au_set_ibend(a->inode, a->mvd_bdst);
22352 +
22353 +out_unlock:
22354 +       au_do_unlock(dmsg, a);
22355 +out:
22356 +       AuTraceErr(err);
22357 +       return err;
22358 +}
22359 +
22360 +/* ---------------------------------------------------------------------- */
22361 +
22362 +/* make sure the file is idle */
22363 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
22364 +{
22365 +       int err, plinked;
22366 +
22367 +       err = 0;
22368 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
22369 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
22370 +           && au_dcount(a->dentry) == 1
22371 +           && atomic_read(&a->inode->i_count) == 1
22372 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
22373 +           && (!plinked || !au_plink_test(a->inode))
22374 +           && a->inode->i_nlink == 1)
22375 +               goto out;
22376 +
22377 +       err = -EBUSY;
22378 +       AU_MVD_PR(dmsg,
22379 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
22380 +                 a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
22381 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
22382 +                 a->mvd_h_src_inode->i_nlink,
22383 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
22384 +
22385 +out:
22386 +       AuTraceErr(err);
22387 +       return err;
22388 +}
22389 +
22390 +/* make sure the parent dir is fine */
22391 +static int au_mvd_args_parent(const unsigned char dmsg,
22392 +                             struct au_mvd_args *a)
22393 +{
22394 +       int err;
22395 +       aufs_bindex_t bindex;
22396 +
22397 +       err = 0;
22398 +       if (unlikely(au_alive_dir(a->parent))) {
22399 +               err = -ENOENT;
22400 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
22401 +               goto out;
22402 +       }
22403 +
22404 +       a->bopq = au_dbdiropq(a->parent);
22405 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22406 +       AuDbg("b%d\n", bindex);
22407 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22408 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22409 +               err = -EINVAL;
22410 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
22411 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
22412 +                         a->bopq, a->mvd_bdst);
22413 +       }
22414 +
22415 +out:
22416 +       AuTraceErr(err);
22417 +       return err;
22418 +}
22419 +
22420 +static int au_mvd_args_intermediate(const unsigned char dmsg,
22421 +                                   struct au_mvd_args *a)
22422 +{
22423 +       int err;
22424 +       struct au_dinfo *dinfo, *tmp;
22425 +
22426 +       /* lookup the next lower positive entry */
22427 +       err = -ENOMEM;
22428 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22429 +       if (unlikely(!tmp))
22430 +               goto out;
22431 +
22432 +       a->bfound = -1;
22433 +       a->bwh = -1;
22434 +       dinfo = au_di(a->dentry);
22435 +       au_di_cp(tmp, dinfo);
22436 +       au_di_swap(tmp, dinfo);
22437 +
22438 +       /* returns the number of positive dentries */
22439 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
22440 +       if (!err)
22441 +               a->bwh = au_dbwh(a->dentry);
22442 +       else if (err > 0)
22443 +               a->bfound = au_dbstart(a->dentry);
22444 +
22445 +       au_di_swap(tmp, dinfo);
22446 +       au_rw_write_unlock(&tmp->di_rwsem);
22447 +       au_di_free(tmp);
22448 +       if (unlikely(err < 0))
22449 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
22450 +
22451 +       /*
22452 +        * here, we have these cases.
22453 +        * bfound == -1
22454 +        *      no positive dentry under bsrc. there are more sub-cases.
22455 +        *      bwh < 0
22456 +        *              there no whiteout, we can safely move-down.
22457 +        *      bwh <= bsrc
22458 +        *              impossible
22459 +        *      bsrc < bwh && bwh < bdst
22460 +        *              there is a whiteout on RO branch. cannot proceed.
22461 +        *      bwh == bdst
22462 +        *              there is a whiteout on the RW target branch. it should
22463 +        *              be removed.
22464 +        *      bdst < bwh
22465 +        *              there is a whiteout somewhere unrelated branch.
22466 +        * -1 < bfound && bfound <= bsrc
22467 +        *      impossible.
22468 +        * bfound < bdst
22469 +        *      found, but it is on RO branch between bsrc and bdst. cannot
22470 +        *      proceed.
22471 +        * bfound == bdst
22472 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22473 +        *      error.
22474 +        * bdst < bfound
22475 +        *      found, after we create the file on bdst, it will be hidden.
22476 +        */
22477 +
22478 +       AuDebugOn(a->bfound == -1
22479 +                 && a->bwh != -1
22480 +                 && a->bwh <= a->mvd_bsrc);
22481 +       AuDebugOn(-1 < a->bfound
22482 +                 && a->bfound <= a->mvd_bsrc);
22483 +
22484 +       err = -EINVAL;
22485 +       if (a->bfound == -1
22486 +           && a->mvd_bsrc < a->bwh
22487 +           && a->bwh != -1
22488 +           && a->bwh < a->mvd_bdst) {
22489 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22490 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
22491 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22492 +               goto out;
22493 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
22494 +               a->mvd_errno = EAU_MVDOWN_UPPER;
22495 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
22496 +                         a->mvd_bdst, a->bfound);
22497 +               goto out;
22498 +       }
22499 +
22500 +       err = 0; /* success */
22501 +
22502 +out:
22503 +       AuTraceErr(err);
22504 +       return err;
22505 +}
22506 +
22507 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
22508 +{
22509 +       int err;
22510 +
22511 +       err = 0;
22512 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22513 +           && a->bfound == a->mvd_bdst)
22514 +               err = -EEXIST;
22515 +       AuTraceErr(err);
22516 +       return err;
22517 +}
22518 +
22519 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
22520 +{
22521 +       int err;
22522 +       struct au_branch *br;
22523 +
22524 +       err = -EISDIR;
22525 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
22526 +               goto out;
22527 +
22528 +       err = -EINVAL;
22529 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
22530 +               a->mvd_bsrc = au_ibstart(a->inode);
22531 +       else {
22532 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22533 +               if (unlikely(a->mvd_bsrc < 0
22534 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
22535 +                                || au_dbend(a->dentry) < a->mvd_bsrc
22536 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
22537 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
22538 +                                || au_ibend(a->inode) < a->mvd_bsrc
22539 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22540 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
22541 +                       AU_MVD_PR(dmsg, "no upper\n");
22542 +                       goto out;
22543 +               }
22544 +       }
22545 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
22546 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
22547 +               AU_MVD_PR(dmsg, "on the bottom\n");
22548 +               goto out;
22549 +       }
22550 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
22551 +       br = au_sbr(a->sb, a->mvd_bsrc);
22552 +       err = au_br_rdonly(br);
22553 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22554 +               if (unlikely(err))
22555 +                       goto out;
22556 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22557 +                    || IS_APPEND(a->mvd_h_src_inode))) {
22558 +               if (err)
22559 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22560 +               /* go on */
22561 +       } else
22562 +               goto out;
22563 +
22564 +       err = -EINVAL;
22565 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22566 +               a->mvd_bdst = find_lower_writable(a);
22567 +               if (unlikely(a->mvd_bdst < 0)) {
22568 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
22569 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
22570 +                       goto out;
22571 +               }
22572 +       } else {
22573 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22574 +               if (unlikely(a->mvd_bdst < 0
22575 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
22576 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22577 +                       AU_MVD_PR(dmsg, "no lower brid\n");
22578 +                       goto out;
22579 +               }
22580 +       }
22581 +
22582 +       err = au_mvd_args_busy(dmsg, a);
22583 +       if (!err)
22584 +               err = au_mvd_args_parent(dmsg, a);
22585 +       if (!err)
22586 +               err = au_mvd_args_intermediate(dmsg, a);
22587 +       if (!err)
22588 +               err = au_mvd_args_exist(dmsg, a);
22589 +       if (!err)
22590 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22591 +
22592 +out:
22593 +       AuTraceErr(err);
22594 +       return err;
22595 +}
22596 +
22597 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22598 +{
22599 +       int err, e;
22600 +       unsigned char dmsg;
22601 +       struct au_mvd_args *args;
22602 +       struct inode *inode;
22603 +
22604 +       inode = d_inode(dentry);
22605 +       err = -EPERM;
22606 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
22607 +               goto out;
22608 +
22609 +       err = -ENOMEM;
22610 +       args = kmalloc(sizeof(*args), GFP_NOFS);
22611 +       if (unlikely(!args))
22612 +               goto out;
22613 +
22614 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22615 +       if (!err)
22616 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
22617 +       if (unlikely(err)) {
22618 +               err = -EFAULT;
22619 +               AuTraceErr(err);
22620 +               goto out_free;
22621 +       }
22622 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
22623 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22624 +       args->mvdown.au_errno = 0;
22625 +       args->dentry = dentry;
22626 +       args->inode = inode;
22627 +       args->sb = dentry->d_sb;
22628 +
22629 +       err = -ENOENT;
22630 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22631 +       args->parent = dget_parent(dentry);
22632 +       args->dir = d_inode(args->parent);
22633 +       mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
22634 +       dput(args->parent);
22635 +       if (unlikely(args->parent != dentry->d_parent)) {
22636 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
22637 +               goto out_dir;
22638 +       }
22639 +
22640 +       mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
22641 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
22642 +       if (unlikely(err))
22643 +               goto out_inode;
22644 +
22645 +       di_write_lock_parent(args->parent);
22646 +       err = au_mvd_args(dmsg, args);
22647 +       if (unlikely(err))
22648 +               goto out_parent;
22649 +
22650 +       err = au_do_mvdown(dmsg, args);
22651 +       if (unlikely(err))
22652 +               goto out_parent;
22653 +
22654 +       au_cpup_attr_timesizes(args->dir);
22655 +       au_cpup_attr_timesizes(inode);
22656 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
22657 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
22658 +       /* au_digen_dec(dentry); */
22659 +
22660 +out_parent:
22661 +       di_write_unlock(args->parent);
22662 +       aufs_read_unlock(dentry, AuLock_DW);
22663 +out_inode:
22664 +       mutex_unlock(&inode->i_mutex);
22665 +out_dir:
22666 +       mutex_unlock(&args->dir->i_mutex);
22667 +out_free:
22668 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22669 +       if (unlikely(e))
22670 +               err = -EFAULT;
22671 +       kfree(args);
22672 +out:
22673 +       AuTraceErr(err);
22674 +       return err;
22675 +}
22676 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22677 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
22678 +++ linux/fs/aufs/opts.c        2016-01-13 20:11:11.673093671 +0100
22679 @@ -0,0 +1,1859 @@
22680 +/*
22681 + * Copyright (C) 2005-2015 Junjiro R. Okajima
22682 + *
22683 + * This program, aufs is free software; you can redistribute it and/or modify
22684 + * it under the terms of the GNU General Public License as published by
22685 + * the Free Software Foundation; either version 2 of the License, or
22686 + * (at your option) any later version.
22687 + *
22688 + * This program is distributed in the hope that it will be useful,
22689 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22690 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22691 + * GNU General Public License for more details.
22692 + *
22693 + * You should have received a copy of the GNU General Public License
22694 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22695 + */
22696 +
22697 +/*
22698 + * mount options/flags
22699 + */
22700 +
22701 +#include <linux/namei.h>
22702 +#include <linux/types.h> /* a distribution requires */
22703 +#include <linux/parser.h>
22704 +#include "aufs.h"
22705 +
22706 +/* ---------------------------------------------------------------------- */
22707 +
22708 +enum {
22709 +       Opt_br,
22710 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22711 +       Opt_idel, Opt_imod,
22712 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22713 +       Opt_rdblk_def, Opt_rdhash_def,
22714 +       Opt_xino, Opt_noxino,
22715 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22716 +       Opt_trunc_xino_path, Opt_itrunc_xino,
22717 +       Opt_trunc_xib, Opt_notrunc_xib,
22718 +       Opt_shwh, Opt_noshwh,
22719 +       Opt_plink, Opt_noplink, Opt_list_plink,
22720 +       Opt_udba,
22721 +       Opt_dio, Opt_nodio,
22722 +       Opt_diropq_a, Opt_diropq_w,
22723 +       Opt_warn_perm, Opt_nowarn_perm,
22724 +       Opt_wbr_copyup, Opt_wbr_create,
22725 +       Opt_fhsm_sec,
22726 +       Opt_verbose, Opt_noverbose,
22727 +       Opt_sum, Opt_nosum, Opt_wsum,
22728 +       Opt_dirperm1, Opt_nodirperm1,
22729 +       Opt_acl, Opt_noacl,
22730 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22731 +};
22732 +
22733 +static match_table_t options = {
22734 +       {Opt_br, "br=%s"},
22735 +       {Opt_br, "br:%s"},
22736 +
22737 +       {Opt_add, "add=%d:%s"},
22738 +       {Opt_add, "add:%d:%s"},
22739 +       {Opt_add, "ins=%d:%s"},
22740 +       {Opt_add, "ins:%d:%s"},
22741 +       {Opt_append, "append=%s"},
22742 +       {Opt_append, "append:%s"},
22743 +       {Opt_prepend, "prepend=%s"},
22744 +       {Opt_prepend, "prepend:%s"},
22745 +
22746 +       {Opt_del, "del=%s"},
22747 +       {Opt_del, "del:%s"},
22748 +       /* {Opt_idel, "idel:%d"}, */
22749 +       {Opt_mod, "mod=%s"},
22750 +       {Opt_mod, "mod:%s"},
22751 +       /* {Opt_imod, "imod:%d:%s"}, */
22752 +
22753 +       {Opt_dirwh, "dirwh=%d"},
22754 +
22755 +       {Opt_xino, "xino=%s"},
22756 +       {Opt_noxino, "noxino"},
22757 +       {Opt_trunc_xino, "trunc_xino"},
22758 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22759 +       {Opt_notrunc_xino, "notrunc_xino"},
22760 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
22761 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
22762 +       /* {Opt_zxino, "zxino=%s"}, */
22763 +       {Opt_trunc_xib, "trunc_xib"},
22764 +       {Opt_notrunc_xib, "notrunc_xib"},
22765 +
22766 +#ifdef CONFIG_PROC_FS
22767 +       {Opt_plink, "plink"},
22768 +#else
22769 +       {Opt_ignore_silent, "plink"},
22770 +#endif
22771 +
22772 +       {Opt_noplink, "noplink"},
22773 +
22774 +#ifdef CONFIG_AUFS_DEBUG
22775 +       {Opt_list_plink, "list_plink"},
22776 +#endif
22777 +
22778 +       {Opt_udba, "udba=%s"},
22779 +
22780 +       {Opt_dio, "dio"},
22781 +       {Opt_nodio, "nodio"},
22782 +
22783 +#ifdef CONFIG_AUFS_FHSM
22784 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
22785 +#else
22786 +       {Opt_ignore_silent, "fhsm_sec=%d"},
22787 +#endif
22788 +
22789 +       {Opt_diropq_a, "diropq=always"},
22790 +       {Opt_diropq_a, "diropq=a"},
22791 +       {Opt_diropq_w, "diropq=whiteouted"},
22792 +       {Opt_diropq_w, "diropq=w"},
22793 +
22794 +       {Opt_warn_perm, "warn_perm"},
22795 +       {Opt_nowarn_perm, "nowarn_perm"},
22796 +
22797 +       /* keep them temporary */
22798 +       {Opt_ignore_silent, "nodlgt"},
22799 +       {Opt_ignore_silent, "clean_plink"},
22800 +
22801 +#ifdef CONFIG_AUFS_SHWH
22802 +       {Opt_shwh, "shwh"},
22803 +#endif
22804 +       {Opt_noshwh, "noshwh"},
22805 +
22806 +       {Opt_dirperm1, "dirperm1"},
22807 +       {Opt_nodirperm1, "nodirperm1"},
22808 +
22809 +       {Opt_verbose, "verbose"},
22810 +       {Opt_verbose, "v"},
22811 +       {Opt_noverbose, "noverbose"},
22812 +       {Opt_noverbose, "quiet"},
22813 +       {Opt_noverbose, "q"},
22814 +       {Opt_noverbose, "silent"},
22815 +
22816 +       {Opt_sum, "sum"},
22817 +       {Opt_nosum, "nosum"},
22818 +       {Opt_wsum, "wsum"},
22819 +
22820 +       {Opt_rdcache, "rdcache=%d"},
22821 +       {Opt_rdblk, "rdblk=%d"},
22822 +       {Opt_rdblk_def, "rdblk=def"},
22823 +       {Opt_rdhash, "rdhash=%d"},
22824 +       {Opt_rdhash_def, "rdhash=def"},
22825 +
22826 +       {Opt_wbr_create, "create=%s"},
22827 +       {Opt_wbr_create, "create_policy=%s"},
22828 +       {Opt_wbr_copyup, "cpup=%s"},
22829 +       {Opt_wbr_copyup, "copyup=%s"},
22830 +       {Opt_wbr_copyup, "copyup_policy=%s"},
22831 +
22832 +       /* generic VFS flag */
22833 +#ifdef CONFIG_FS_POSIX_ACL
22834 +       {Opt_acl, "acl"},
22835 +       {Opt_noacl, "noacl"},
22836 +#else
22837 +       {Opt_ignore_silent, "acl"},
22838 +       {Opt_ignore_silent, "noacl"},
22839 +#endif
22840 +
22841 +       /* internal use for the scripts */
22842 +       {Opt_ignore_silent, "si=%s"},
22843 +
22844 +       {Opt_br, "dirs=%s"},
22845 +       {Opt_ignore, "debug=%d"},
22846 +       {Opt_ignore, "delete=whiteout"},
22847 +       {Opt_ignore, "delete=all"},
22848 +       {Opt_ignore, "imap=%s"},
22849 +
22850 +       /* temporary workaround, due to old mount(8)? */
22851 +       {Opt_ignore_silent, "relatime"},
22852 +
22853 +       {Opt_err, NULL}
22854 +};
22855 +
22856 +/* ---------------------------------------------------------------------- */
22857 +
22858 +static const char *au_parser_pattern(int val, match_table_t tbl)
22859 +{
22860 +       struct match_token *p;
22861 +
22862 +       p = tbl;
22863 +       while (p->pattern) {
22864 +               if (p->token == val)
22865 +                       return p->pattern;
22866 +               p++;
22867 +       }
22868 +       BUG();
22869 +       return "??";
22870 +}
22871 +
22872 +static const char *au_optstr(int *val, match_table_t tbl)
22873 +{
22874 +       struct match_token *p;
22875 +       int v;
22876 +
22877 +       v = *val;
22878 +       if (!v)
22879 +               goto out;
22880 +       p = tbl;
22881 +       while (p->pattern) {
22882 +               if (p->token
22883 +                   && (v & p->token) == p->token) {
22884 +                       *val &= ~p->token;
22885 +                       return p->pattern;
22886 +               }
22887 +               p++;
22888 +       }
22889 +
22890 +out:
22891 +       return NULL;
22892 +}
22893 +
22894 +/* ---------------------------------------------------------------------- */
22895 +
22896 +static match_table_t brperm = {
22897 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
22898 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
22899 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
22900 +       {0, NULL}
22901 +};
22902 +
22903 +static match_table_t brattr = {
22904 +       /* general */
22905 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
22906 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
22907 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
22908 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
22909 +#ifdef CONFIG_AUFS_FHSM
22910 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
22911 +#endif
22912 +#ifdef CONFIG_AUFS_XATTR
22913 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
22914 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
22915 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
22916 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
22917 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
22918 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
22919 +#endif
22920 +
22921 +       /* ro/rr branch */
22922 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
22923 +
22924 +       /* rw branch */
22925 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
22926 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
22927 +
22928 +       {0, NULL}
22929 +};
22930 +
22931 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
22932 +{
22933 +       int attr, v;
22934 +       char *p;
22935 +
22936 +       attr = 0;
22937 +       do {
22938 +               p = strchr(str, '+');
22939 +               if (p)
22940 +                       *p = 0;
22941 +               v = match_token(str, table, args);
22942 +               if (v) {
22943 +                       if (v & AuBrAttr_CMOO_Mask)
22944 +                               attr &= ~AuBrAttr_CMOO_Mask;
22945 +                       attr |= v;
22946 +               } else {
22947 +                       if (p)
22948 +                               *p = '+';
22949 +                       pr_warn("ignored branch attribute %s\n", str);
22950 +                       break;
22951 +               }
22952 +               if (p)
22953 +                       str = p + 1;
22954 +       } while (p);
22955 +
22956 +       return attr;
22957 +}
22958 +
22959 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
22960 +{
22961 +       int sz;
22962 +       const char *p;
22963 +       char *q;
22964 +
22965 +       q = str->a;
22966 +       *q = 0;
22967 +       p = au_optstr(&perm, brattr);
22968 +       if (p) {
22969 +               sz = strlen(p);
22970 +               memcpy(q, p, sz + 1);
22971 +               q += sz;
22972 +       } else
22973 +               goto out;
22974 +
22975 +       do {
22976 +               p = au_optstr(&perm, brattr);
22977 +               if (p) {
22978 +                       *q++ = '+';
22979 +                       sz = strlen(p);
22980 +                       memcpy(q, p, sz + 1);
22981 +                       q += sz;
22982 +               }
22983 +       } while (p);
22984 +
22985 +out:
22986 +       return q - str->a;
22987 +}
22988 +
22989 +static int noinline_for_stack br_perm_val(char *perm)
22990 +{
22991 +       int val, bad, sz;
22992 +       char *p;
22993 +       substring_t args[MAX_OPT_ARGS];
22994 +       au_br_perm_str_t attr;
22995 +
22996 +       p = strchr(perm, '+');
22997 +       if (p)
22998 +               *p = 0;
22999 +       val = match_token(perm, brperm, args);
23000 +       if (!val) {
23001 +               if (p)
23002 +                       *p = '+';
23003 +               pr_warn("ignored branch permission %s\n", perm);
23004 +               val = AuBrPerm_RO;
23005 +               goto out;
23006 +       }
23007 +       if (!p)
23008 +               goto out;
23009 +
23010 +       val |= br_attr_val(p + 1, brattr, args);
23011 +
23012 +       bad = 0;
23013 +       switch (val & AuBrPerm_Mask) {
23014 +       case AuBrPerm_RO:
23015 +       case AuBrPerm_RR:
23016 +               bad = val & AuBrWAttr_Mask;
23017 +               val &= ~AuBrWAttr_Mask;
23018 +               break;
23019 +       case AuBrPerm_RW:
23020 +               bad = val & AuBrRAttr_Mask;
23021 +               val &= ~AuBrRAttr_Mask;
23022 +               break;
23023 +       }
23024 +
23025 +       /*
23026 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23027 +        * does not treat it as an error, just warning.
23028 +        * this is a tiny guard for the user operation.
23029 +        */
23030 +       if (val & AuBrAttr_UNPIN) {
23031 +               bad |= AuBrAttr_UNPIN;
23032 +               val &= ~AuBrAttr_UNPIN;
23033 +       }
23034 +
23035 +       if (unlikely(bad)) {
23036 +               sz = au_do_optstr_br_attr(&attr, bad);
23037 +               AuDebugOn(!sz);
23038 +               pr_warn("ignored branch attribute %s\n", attr.a);
23039 +       }
23040 +
23041 +out:
23042 +       return val;
23043 +}
23044 +
23045 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
23046 +{
23047 +       au_br_perm_str_t attr;
23048 +       const char *p;
23049 +       char *q;
23050 +       int sz;
23051 +
23052 +       q = str->a;
23053 +       p = au_optstr(&perm, brperm);
23054 +       AuDebugOn(!p || !*p);
23055 +       sz = strlen(p);
23056 +       memcpy(q, p, sz + 1);
23057 +       q += sz;
23058 +
23059 +       sz = au_do_optstr_br_attr(&attr, perm);
23060 +       if (sz) {
23061 +               *q++ = '+';
23062 +               memcpy(q, attr.a, sz + 1);
23063 +       }
23064 +
23065 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
23066 +}
23067 +
23068 +/* ---------------------------------------------------------------------- */
23069 +
23070 +static match_table_t udbalevel = {
23071 +       {AuOpt_UDBA_REVAL, "reval"},
23072 +       {AuOpt_UDBA_NONE, "none"},
23073 +#ifdef CONFIG_AUFS_HNOTIFY
23074 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23075 +#ifdef CONFIG_AUFS_HFSNOTIFY
23076 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
23077 +#endif
23078 +#endif
23079 +       {-1, NULL}
23080 +};
23081 +
23082 +static int noinline_for_stack udba_val(char *str)
23083 +{
23084 +       substring_t args[MAX_OPT_ARGS];
23085 +
23086 +       return match_token(str, udbalevel, args);
23087 +}
23088 +
23089 +const char *au_optstr_udba(int udba)
23090 +{
23091 +       return au_parser_pattern(udba, udbalevel);
23092 +}
23093 +
23094 +/* ---------------------------------------------------------------------- */
23095 +
23096 +static match_table_t au_wbr_create_policy = {
23097 +       {AuWbrCreate_TDP, "tdp"},
23098 +       {AuWbrCreate_TDP, "top-down-parent"},
23099 +       {AuWbrCreate_RR, "rr"},
23100 +       {AuWbrCreate_RR, "round-robin"},
23101 +       {AuWbrCreate_MFS, "mfs"},
23102 +       {AuWbrCreate_MFS, "most-free-space"},
23103 +       {AuWbrCreate_MFSV, "mfs:%d"},
23104 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
23105 +
23106 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
23107 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23108 +       {AuWbrCreate_PMFS, "pmfs"},
23109 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
23110 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23111 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
23112 +
23113 +       {-1, NULL}
23114 +};
23115 +
23116 +/*
23117 + * cf. linux/lib/parser.c and cmdline.c
23118 + * gave up calling memparse() since it uses simple_strtoull() instead of
23119 + * kstrto...().
23120 + */
23121 +static int noinline_for_stack
23122 +au_match_ull(substring_t *s, unsigned long long *result)
23123 +{
23124 +       int err;
23125 +       unsigned int len;
23126 +       char a[32];
23127 +
23128 +       err = -ERANGE;
23129 +       len = s->to - s->from;
23130 +       if (len + 1 <= sizeof(a)) {
23131 +               memcpy(a, s->from, len);
23132 +               a[len] = '\0';
23133 +               err = kstrtoull(a, 0, result);
23134 +       }
23135 +       return err;
23136 +}
23137 +
23138 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23139 +                           struct au_opt_wbr_create *create)
23140 +{
23141 +       int err;
23142 +       unsigned long long ull;
23143 +
23144 +       err = 0;
23145 +       if (!au_match_ull(arg, &ull))
23146 +               create->mfsrr_watermark = ull;
23147 +       else {
23148 +               pr_err("bad integer in %s\n", str);
23149 +               err = -EINVAL;
23150 +       }
23151 +
23152 +       return err;
23153 +}
23154 +
23155 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23156 +                         struct au_opt_wbr_create *create)
23157 +{
23158 +       int n, err;
23159 +
23160 +       err = 0;
23161 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23162 +               create->mfs_second = n;
23163 +       else {
23164 +               pr_err("bad integer in %s\n", str);
23165 +               err = -EINVAL;
23166 +       }
23167 +
23168 +       return err;
23169 +}
23170 +
23171 +static int noinline_for_stack
23172 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23173 +{
23174 +       int err, e;
23175 +       substring_t args[MAX_OPT_ARGS];
23176 +
23177 +       err = match_token(str, au_wbr_create_policy, args);
23178 +       create->wbr_create = err;
23179 +       switch (err) {
23180 +       case AuWbrCreate_MFSRRV:
23181 +       case AuWbrCreate_PMFSRRV:
23182 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23183 +               if (!e)
23184 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23185 +               if (unlikely(e))
23186 +                       err = e;
23187 +               break;
23188 +       case AuWbrCreate_MFSRR:
23189 +       case AuWbrCreate_PMFSRR:
23190 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23191 +               if (unlikely(e)) {
23192 +                       err = e;
23193 +                       break;
23194 +               }
23195 +               /*FALLTHROUGH*/
23196 +       case AuWbrCreate_MFS:
23197 +       case AuWbrCreate_PMFS:
23198 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23199 +               break;
23200 +       case AuWbrCreate_MFSV:
23201 +       case AuWbrCreate_PMFSV:
23202 +               e = au_wbr_mfs_sec(&args[0], str, create);
23203 +               if (unlikely(e))
23204 +                       err = e;
23205 +               break;
23206 +       }
23207 +
23208 +       return err;
23209 +}
23210 +
23211 +const char *au_optstr_wbr_create(int wbr_create)
23212 +{
23213 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23214 +}
23215 +
23216 +static match_table_t au_wbr_copyup_policy = {
23217 +       {AuWbrCopyup_TDP, "tdp"},
23218 +       {AuWbrCopyup_TDP, "top-down-parent"},
23219 +       {AuWbrCopyup_BUP, "bup"},
23220 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23221 +       {AuWbrCopyup_BU, "bu"},
23222 +       {AuWbrCopyup_BU, "bottom-up"},
23223 +       {-1, NULL}
23224 +};
23225 +
23226 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23227 +{
23228 +       substring_t args[MAX_OPT_ARGS];
23229 +
23230 +       return match_token(str, au_wbr_copyup_policy, args);
23231 +}
23232 +
23233 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23234 +{
23235 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23236 +}
23237 +
23238 +/* ---------------------------------------------------------------------- */
23239 +
23240 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23241 +
23242 +static void dump_opts(struct au_opts *opts)
23243 +{
23244 +#ifdef CONFIG_AUFS_DEBUG
23245 +       /* reduce stack space */
23246 +       union {
23247 +               struct au_opt_add *add;
23248 +               struct au_opt_del *del;
23249 +               struct au_opt_mod *mod;
23250 +               struct au_opt_xino *xino;
23251 +               struct au_opt_xino_itrunc *xino_itrunc;
23252 +               struct au_opt_wbr_create *create;
23253 +       } u;
23254 +       struct au_opt *opt;
23255 +
23256 +       opt = opts->opt;
23257 +       while (opt->type != Opt_tail) {
23258 +               switch (opt->type) {
23259 +               case Opt_add:
23260 +                       u.add = &opt->add;
23261 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23262 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23263 +                                 u.add->path.dentry);
23264 +                       break;
23265 +               case Opt_del:
23266 +               case Opt_idel:
23267 +                       u.del = &opt->del;
23268 +                       AuDbg("del {%s, %p}\n",
23269 +                             u.del->pathname, u.del->h_path.dentry);
23270 +                       break;
23271 +               case Opt_mod:
23272 +               case Opt_imod:
23273 +                       u.mod = &opt->mod;
23274 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23275 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23276 +                       break;
23277 +               case Opt_append:
23278 +                       u.add = &opt->add;
23279 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23280 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23281 +                                 u.add->path.dentry);
23282 +                       break;
23283 +               case Opt_prepend:
23284 +                       u.add = &opt->add;
23285 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23286 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23287 +                                 u.add->path.dentry);
23288 +                       break;
23289 +               case Opt_dirwh:
23290 +                       AuDbg("dirwh %d\n", opt->dirwh);
23291 +                       break;
23292 +               case Opt_rdcache:
23293 +                       AuDbg("rdcache %d\n", opt->rdcache);
23294 +                       break;
23295 +               case Opt_rdblk:
23296 +                       AuDbg("rdblk %u\n", opt->rdblk);
23297 +                       break;
23298 +               case Opt_rdblk_def:
23299 +                       AuDbg("rdblk_def\n");
23300 +                       break;
23301 +               case Opt_rdhash:
23302 +                       AuDbg("rdhash %u\n", opt->rdhash);
23303 +                       break;
23304 +               case Opt_rdhash_def:
23305 +                       AuDbg("rdhash_def\n");
23306 +                       break;
23307 +               case Opt_xino:
23308 +                       u.xino = &opt->xino;
23309 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23310 +                       break;
23311 +               case Opt_trunc_xino:
23312 +                       AuLabel(trunc_xino);
23313 +                       break;
23314 +               case Opt_notrunc_xino:
23315 +                       AuLabel(notrunc_xino);
23316 +                       break;
23317 +               case Opt_trunc_xino_path:
23318 +               case Opt_itrunc_xino:
23319 +                       u.xino_itrunc = &opt->xino_itrunc;
23320 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23321 +                       break;
23322 +               case Opt_noxino:
23323 +                       AuLabel(noxino);
23324 +                       break;
23325 +               case Opt_trunc_xib:
23326 +                       AuLabel(trunc_xib);
23327 +                       break;
23328 +               case Opt_notrunc_xib:
23329 +                       AuLabel(notrunc_xib);
23330 +                       break;
23331 +               case Opt_shwh:
23332 +                       AuLabel(shwh);
23333 +                       break;
23334 +               case Opt_noshwh:
23335 +                       AuLabel(noshwh);
23336 +                       break;
23337 +               case Opt_dirperm1:
23338 +                       AuLabel(dirperm1);
23339 +                       break;
23340 +               case Opt_nodirperm1:
23341 +                       AuLabel(nodirperm1);
23342 +                       break;
23343 +               case Opt_plink:
23344 +                       AuLabel(plink);
23345 +                       break;
23346 +               case Opt_noplink:
23347 +                       AuLabel(noplink);
23348 +                       break;
23349 +               case Opt_list_plink:
23350 +                       AuLabel(list_plink);
23351 +                       break;
23352 +               case Opt_udba:
23353 +                       AuDbg("udba %d, %s\n",
23354 +                                 opt->udba, au_optstr_udba(opt->udba));
23355 +                       break;
23356 +               case Opt_dio:
23357 +                       AuLabel(dio);
23358 +                       break;
23359 +               case Opt_nodio:
23360 +                       AuLabel(nodio);
23361 +                       break;
23362 +               case Opt_diropq_a:
23363 +                       AuLabel(diropq_a);
23364 +                       break;
23365 +               case Opt_diropq_w:
23366 +                       AuLabel(diropq_w);
23367 +                       break;
23368 +               case Opt_warn_perm:
23369 +                       AuLabel(warn_perm);
23370 +                       break;
23371 +               case Opt_nowarn_perm:
23372 +                       AuLabel(nowarn_perm);
23373 +                       break;
23374 +               case Opt_verbose:
23375 +                       AuLabel(verbose);
23376 +                       break;
23377 +               case Opt_noverbose:
23378 +                       AuLabel(noverbose);
23379 +                       break;
23380 +               case Opt_sum:
23381 +                       AuLabel(sum);
23382 +                       break;
23383 +               case Opt_nosum:
23384 +                       AuLabel(nosum);
23385 +                       break;
23386 +               case Opt_wsum:
23387 +                       AuLabel(wsum);
23388 +                       break;
23389 +               case Opt_wbr_create:
23390 +                       u.create = &opt->wbr_create;
23391 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
23392 +                                 au_optstr_wbr_create(u.create->wbr_create));
23393 +                       switch (u.create->wbr_create) {
23394 +                       case AuWbrCreate_MFSV:
23395 +                       case AuWbrCreate_PMFSV:
23396 +                               AuDbg("%d sec\n", u.create->mfs_second);
23397 +                               break;
23398 +                       case AuWbrCreate_MFSRR:
23399 +                               AuDbg("%llu watermark\n",
23400 +                                         u.create->mfsrr_watermark);
23401 +                               break;
23402 +                       case AuWbrCreate_MFSRRV:
23403 +                       case AuWbrCreate_PMFSRRV:
23404 +                               AuDbg("%llu watermark, %d sec\n",
23405 +                                         u.create->mfsrr_watermark,
23406 +                                         u.create->mfs_second);
23407 +                               break;
23408 +                       }
23409 +                       break;
23410 +               case Opt_wbr_copyup:
23411 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23412 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
23413 +                       break;
23414 +               case Opt_fhsm_sec:
23415 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23416 +                       break;
23417 +               case Opt_acl:
23418 +                       AuLabel(acl);
23419 +                       break;
23420 +               case Opt_noacl:
23421 +                       AuLabel(noacl);
23422 +                       break;
23423 +               default:
23424 +                       BUG();
23425 +               }
23426 +               opt++;
23427 +       }
23428 +#endif
23429 +}
23430 +
23431 +void au_opts_free(struct au_opts *opts)
23432 +{
23433 +       struct au_opt *opt;
23434 +
23435 +       opt = opts->opt;
23436 +       while (opt->type != Opt_tail) {
23437 +               switch (opt->type) {
23438 +               case Opt_add:
23439 +               case Opt_append:
23440 +               case Opt_prepend:
23441 +                       path_put(&opt->add.path);
23442 +                       break;
23443 +               case Opt_del:
23444 +               case Opt_idel:
23445 +                       path_put(&opt->del.h_path);
23446 +                       break;
23447 +               case Opt_mod:
23448 +               case Opt_imod:
23449 +                       dput(opt->mod.h_root);
23450 +                       break;
23451 +               case Opt_xino:
23452 +                       fput(opt->xino.file);
23453 +                       break;
23454 +               }
23455 +               opt++;
23456 +       }
23457 +}
23458 +
23459 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23460 +                  aufs_bindex_t bindex)
23461 +{
23462 +       int err;
23463 +       struct au_opt_add *add = &opt->add;
23464 +       char *p;
23465 +
23466 +       add->bindex = bindex;
23467 +       add->perm = AuBrPerm_RO;
23468 +       add->pathname = opt_str;
23469 +       p = strchr(opt_str, '=');
23470 +       if (p) {
23471 +               *p++ = 0;
23472 +               if (*p)
23473 +                       add->perm = br_perm_val(p);
23474 +       }
23475 +
23476 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23477 +       if (!err) {
23478 +               if (!p) {
23479 +                       add->perm = AuBrPerm_RO;
23480 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
23481 +                               add->perm = AuBrPerm_RR;
23482 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
23483 +                               add->perm = AuBrPerm_RW;
23484 +               }
23485 +               opt->type = Opt_add;
23486 +               goto out;
23487 +       }
23488 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
23489 +       err = -EINVAL;
23490 +
23491 +out:
23492 +       return err;
23493 +}
23494 +
23495 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23496 +{
23497 +       int err;
23498 +
23499 +       del->pathname = args[0].from;
23500 +       AuDbg("del path %s\n", del->pathname);
23501 +
23502 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23503 +       if (unlikely(err))
23504 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
23505 +
23506 +       return err;
23507 +}
23508 +
23509 +#if 0 /* reserved for future use */
23510 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23511 +                             struct au_opt_del *del, substring_t args[])
23512 +{
23513 +       int err;
23514 +       struct dentry *root;
23515 +
23516 +       err = -EINVAL;
23517 +       root = sb->s_root;
23518 +       aufs_read_lock(root, AuLock_FLUSH);
23519 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23520 +               pr_err("out of bounds, %d\n", bindex);
23521 +               goto out;
23522 +       }
23523 +
23524 +       err = 0;
23525 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
23526 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23527 +
23528 +out:
23529 +       aufs_read_unlock(root, !AuLock_IR);
23530 +       return err;
23531 +}
23532 +#endif
23533 +
23534 +static int noinline_for_stack
23535 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
23536 +{
23537 +       int err;
23538 +       struct path path;
23539 +       char *p;
23540 +
23541 +       err = -EINVAL;
23542 +       mod->path = args[0].from;
23543 +       p = strchr(mod->path, '=');
23544 +       if (unlikely(!p)) {
23545 +               pr_err("no permssion %s\n", args[0].from);
23546 +               goto out;
23547 +       }
23548 +
23549 +       *p++ = 0;
23550 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23551 +       if (unlikely(err)) {
23552 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
23553 +               goto out;
23554 +       }
23555 +
23556 +       mod->perm = br_perm_val(p);
23557 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23558 +       mod->h_root = dget(path.dentry);
23559 +       path_put(&path);
23560 +
23561 +out:
23562 +       return err;
23563 +}
23564 +
23565 +#if 0 /* reserved for future use */
23566 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23567 +                             struct au_opt_mod *mod, substring_t args[])
23568 +{
23569 +       int err;
23570 +       struct dentry *root;
23571 +
23572 +       err = -EINVAL;
23573 +       root = sb->s_root;
23574 +       aufs_read_lock(root, AuLock_FLUSH);
23575 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23576 +               pr_err("out of bounds, %d\n", bindex);
23577 +               goto out;
23578 +       }
23579 +
23580 +       err = 0;
23581 +       mod->perm = br_perm_val(args[1].from);
23582 +       AuDbg("mod path %s, perm 0x%x, %s\n",
23583 +             mod->path, mod->perm, args[1].from);
23584 +       mod->h_root = dget(au_h_dptr(root, bindex));
23585 +
23586 +out:
23587 +       aufs_read_unlock(root, !AuLock_IR);
23588 +       return err;
23589 +}
23590 +#endif
23591 +
23592 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23593 +                             substring_t args[])
23594 +{
23595 +       int err;
23596 +       struct file *file;
23597 +
23598 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
23599 +       err = PTR_ERR(file);
23600 +       if (IS_ERR(file))
23601 +               goto out;
23602 +
23603 +       err = -EINVAL;
23604 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
23605 +               fput(file);
23606 +               pr_err("%s must be outside\n", args[0].from);
23607 +               goto out;
23608 +       }
23609 +
23610 +       err = 0;
23611 +       xino->file = file;
23612 +       xino->path = args[0].from;
23613 +
23614 +out:
23615 +       return err;
23616 +}
23617 +
23618 +static int noinline_for_stack
23619 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
23620 +                              struct au_opt_xino_itrunc *xino_itrunc,
23621 +                              substring_t args[])
23622 +{
23623 +       int err;
23624 +       aufs_bindex_t bend, bindex;
23625 +       struct path path;
23626 +       struct dentry *root;
23627 +
23628 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23629 +       if (unlikely(err)) {
23630 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
23631 +               goto out;
23632 +       }
23633 +
23634 +       xino_itrunc->bindex = -1;
23635 +       root = sb->s_root;
23636 +       aufs_read_lock(root, AuLock_FLUSH);
23637 +       bend = au_sbend(sb);
23638 +       for (bindex = 0; bindex <= bend; bindex++) {
23639 +               if (au_h_dptr(root, bindex) == path.dentry) {
23640 +                       xino_itrunc->bindex = bindex;
23641 +                       break;
23642 +               }
23643 +       }
23644 +       aufs_read_unlock(root, !AuLock_IR);
23645 +       path_put(&path);
23646 +
23647 +       if (unlikely(xino_itrunc->bindex < 0)) {
23648 +               pr_err("no such branch %s\n", args[0].from);
23649 +               err = -EINVAL;
23650 +       }
23651 +
23652 +out:
23653 +       return err;
23654 +}
23655 +
23656 +/* called without aufs lock */
23657 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23658 +{
23659 +       int err, n, token;
23660 +       aufs_bindex_t bindex;
23661 +       unsigned char skipped;
23662 +       struct dentry *root;
23663 +       struct au_opt *opt, *opt_tail;
23664 +       char *opt_str;
23665 +       /* reduce the stack space */
23666 +       union {
23667 +               struct au_opt_xino_itrunc *xino_itrunc;
23668 +               struct au_opt_wbr_create *create;
23669 +       } u;
23670 +       struct {
23671 +               substring_t args[MAX_OPT_ARGS];
23672 +       } *a;
23673 +
23674 +       err = -ENOMEM;
23675 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23676 +       if (unlikely(!a))
23677 +               goto out;
23678 +
23679 +       root = sb->s_root;
23680 +       err = 0;
23681 +       bindex = 0;
23682 +       opt = opts->opt;
23683 +       opt_tail = opt + opts->max_opt - 1;
23684 +       opt->type = Opt_tail;
23685 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23686 +               err = -EINVAL;
23687 +               skipped = 0;
23688 +               token = match_token(opt_str, options, a->args);
23689 +               switch (token) {
23690 +               case Opt_br:
23691 +                       err = 0;
23692 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23693 +                              && *opt_str) {
23694 +                               err = opt_add(opt, opt_str, opts->sb_flags,
23695 +                                             bindex++);
23696 +                               if (unlikely(!err && ++opt > opt_tail)) {
23697 +                                       err = -E2BIG;
23698 +                                       break;
23699 +                               }
23700 +                               opt->type = Opt_tail;
23701 +                               skipped = 1;
23702 +                       }
23703 +                       break;
23704 +               case Opt_add:
23705 +                       if (unlikely(match_int(&a->args[0], &n))) {
23706 +                               pr_err("bad integer in %s\n", opt_str);
23707 +                               break;
23708 +                       }
23709 +                       bindex = n;
23710 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
23711 +                                     bindex);
23712 +                       if (!err)
23713 +                               opt->type = token;
23714 +                       break;
23715 +               case Opt_append:
23716 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23717 +                                     /*dummy bindex*/1);
23718 +                       if (!err)
23719 +                               opt->type = token;
23720 +                       break;
23721 +               case Opt_prepend:
23722 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23723 +                                     /*bindex*/0);
23724 +                       if (!err)
23725 +                               opt->type = token;
23726 +                       break;
23727 +               case Opt_del:
23728 +                       err = au_opts_parse_del(&opt->del, a->args);
23729 +                       if (!err)
23730 +                               opt->type = token;
23731 +                       break;
23732 +#if 0 /* reserved for future use */
23733 +               case Opt_idel:
23734 +                       del->pathname = "(indexed)";
23735 +                       if (unlikely(match_int(&args[0], &n))) {
23736 +                               pr_err("bad integer in %s\n", opt_str);
23737 +                               break;
23738 +                       }
23739 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23740 +                       if (!err)
23741 +                               opt->type = token;
23742 +                       break;
23743 +#endif
23744 +               case Opt_mod:
23745 +                       err = au_opts_parse_mod(&opt->mod, a->args);
23746 +                       if (!err)
23747 +                               opt->type = token;
23748 +                       break;
23749 +#ifdef IMOD /* reserved for future use */
23750 +               case Opt_imod:
23751 +                       u.mod->path = "(indexed)";
23752 +                       if (unlikely(match_int(&a->args[0], &n))) {
23753 +                               pr_err("bad integer in %s\n", opt_str);
23754 +                               break;
23755 +                       }
23756 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23757 +                       if (!err)
23758 +                               opt->type = token;
23759 +                       break;
23760 +#endif
23761 +               case Opt_xino:
23762 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
23763 +                       if (!err)
23764 +                               opt->type = token;
23765 +                       break;
23766 +
23767 +               case Opt_trunc_xino_path:
23768 +                       err = au_opts_parse_xino_itrunc_path
23769 +                               (sb, &opt->xino_itrunc, a->args);
23770 +                       if (!err)
23771 +                               opt->type = token;
23772 +                       break;
23773 +
23774 +               case Opt_itrunc_xino:
23775 +                       u.xino_itrunc = &opt->xino_itrunc;
23776 +                       if (unlikely(match_int(&a->args[0], &n))) {
23777 +                               pr_err("bad integer in %s\n", opt_str);
23778 +                               break;
23779 +                       }
23780 +                       u.xino_itrunc->bindex = n;
23781 +                       aufs_read_lock(root, AuLock_FLUSH);
23782 +                       if (n < 0 || au_sbend(sb) < n) {
23783 +                               pr_err("out of bounds, %d\n", n);
23784 +                               aufs_read_unlock(root, !AuLock_IR);
23785 +                               break;
23786 +                       }
23787 +                       aufs_read_unlock(root, !AuLock_IR);
23788 +                       err = 0;
23789 +                       opt->type = token;
23790 +                       break;
23791 +
23792 +               case Opt_dirwh:
23793 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23794 +                               break;
23795 +                       err = 0;
23796 +                       opt->type = token;
23797 +                       break;
23798 +
23799 +               case Opt_rdcache:
23800 +                       if (unlikely(match_int(&a->args[0], &n))) {
23801 +                               pr_err("bad integer in %s\n", opt_str);
23802 +                               break;
23803 +                       }
23804 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
23805 +                               pr_err("rdcache must be smaller than %d\n",
23806 +                                      AUFS_RDCACHE_MAX);
23807 +                               break;
23808 +                       }
23809 +                       opt->rdcache = n;
23810 +                       err = 0;
23811 +                       opt->type = token;
23812 +                       break;
23813 +               case Opt_rdblk:
23814 +                       if (unlikely(match_int(&a->args[0], &n)
23815 +                                    || n < 0
23816 +                                    || n > KMALLOC_MAX_SIZE)) {
23817 +                               pr_err("bad integer in %s\n", opt_str);
23818 +                               break;
23819 +                       }
23820 +                       if (unlikely(n && n < NAME_MAX)) {
23821 +                               pr_err("rdblk must be larger than %d\n",
23822 +                                      NAME_MAX);
23823 +                               break;
23824 +                       }
23825 +                       opt->rdblk = n;
23826 +                       err = 0;
23827 +                       opt->type = token;
23828 +                       break;
23829 +               case Opt_rdhash:
23830 +                       if (unlikely(match_int(&a->args[0], &n)
23831 +                                    || n < 0
23832 +                                    || n * sizeof(struct hlist_head)
23833 +                                    > KMALLOC_MAX_SIZE)) {
23834 +                               pr_err("bad integer in %s\n", opt_str);
23835 +                               break;
23836 +                       }
23837 +                       opt->rdhash = n;
23838 +                       err = 0;
23839 +                       opt->type = token;
23840 +                       break;
23841 +
23842 +               case Opt_trunc_xino:
23843 +               case Opt_notrunc_xino:
23844 +               case Opt_noxino:
23845 +               case Opt_trunc_xib:
23846 +               case Opt_notrunc_xib:
23847 +               case Opt_shwh:
23848 +               case Opt_noshwh:
23849 +               case Opt_dirperm1:
23850 +               case Opt_nodirperm1:
23851 +               case Opt_plink:
23852 +               case Opt_noplink:
23853 +               case Opt_list_plink:
23854 +               case Opt_dio:
23855 +               case Opt_nodio:
23856 +               case Opt_diropq_a:
23857 +               case Opt_diropq_w:
23858 +               case Opt_warn_perm:
23859 +               case Opt_nowarn_perm:
23860 +               case Opt_verbose:
23861 +               case Opt_noverbose:
23862 +               case Opt_sum:
23863 +               case Opt_nosum:
23864 +               case Opt_wsum:
23865 +               case Opt_rdblk_def:
23866 +               case Opt_rdhash_def:
23867 +               case Opt_acl:
23868 +               case Opt_noacl:
23869 +                       err = 0;
23870 +                       opt->type = token;
23871 +                       break;
23872 +
23873 +               case Opt_udba:
23874 +                       opt->udba = udba_val(a->args[0].from);
23875 +                       if (opt->udba >= 0) {
23876 +                               err = 0;
23877 +                               opt->type = token;
23878 +                       } else
23879 +                               pr_err("wrong value, %s\n", opt_str);
23880 +                       break;
23881 +
23882 +               case Opt_wbr_create:
23883 +                       u.create = &opt->wbr_create;
23884 +                       u.create->wbr_create
23885 +                               = au_wbr_create_val(a->args[0].from, u.create);
23886 +                       if (u.create->wbr_create >= 0) {
23887 +                               err = 0;
23888 +                               opt->type = token;
23889 +                       } else
23890 +                               pr_err("wrong value, %s\n", opt_str);
23891 +                       break;
23892 +               case Opt_wbr_copyup:
23893 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
23894 +                       if (opt->wbr_copyup >= 0) {
23895 +                               err = 0;
23896 +                               opt->type = token;
23897 +                       } else
23898 +                               pr_err("wrong value, %s\n", opt_str);
23899 +                       break;
23900 +
23901 +               case Opt_fhsm_sec:
23902 +                       if (unlikely(match_int(&a->args[0], &n)
23903 +                                    || n < 0)) {
23904 +                               pr_err("bad integer in %s\n", opt_str);
23905 +                               break;
23906 +                       }
23907 +                       if (sysaufs_brs) {
23908 +                               opt->fhsm_second = n;
23909 +                               opt->type = token;
23910 +                       } else
23911 +                               pr_warn("ignored %s\n", opt_str);
23912 +                       err = 0;
23913 +                       break;
23914 +
23915 +               case Opt_ignore:
23916 +                       pr_warn("ignored %s\n", opt_str);
23917 +                       /*FALLTHROUGH*/
23918 +               case Opt_ignore_silent:
23919 +                       skipped = 1;
23920 +                       err = 0;
23921 +                       break;
23922 +               case Opt_err:
23923 +                       pr_err("unknown option %s\n", opt_str);
23924 +                       break;
23925 +               }
23926 +
23927 +               if (!err && !skipped) {
23928 +                       if (unlikely(++opt > opt_tail)) {
23929 +                               err = -E2BIG;
23930 +                               opt--;
23931 +                               opt->type = Opt_tail;
23932 +                               break;
23933 +                       }
23934 +                       opt->type = Opt_tail;
23935 +               }
23936 +       }
23937 +
23938 +       kfree(a);
23939 +       dump_opts(opts);
23940 +       if (unlikely(err))
23941 +               au_opts_free(opts);
23942 +
23943 +out:
23944 +       return err;
23945 +}
23946 +
23947 +static int au_opt_wbr_create(struct super_block *sb,
23948 +                            struct au_opt_wbr_create *create)
23949 +{
23950 +       int err;
23951 +       struct au_sbinfo *sbinfo;
23952 +
23953 +       SiMustWriteLock(sb);
23954 +
23955 +       err = 1; /* handled */
23956 +       sbinfo = au_sbi(sb);
23957 +       if (sbinfo->si_wbr_create_ops->fin) {
23958 +               err = sbinfo->si_wbr_create_ops->fin(sb);
23959 +               if (!err)
23960 +                       err = 1;
23961 +       }
23962 +
23963 +       sbinfo->si_wbr_create = create->wbr_create;
23964 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
23965 +       switch (create->wbr_create) {
23966 +       case AuWbrCreate_MFSRRV:
23967 +       case AuWbrCreate_MFSRR:
23968 +       case AuWbrCreate_PMFSRR:
23969 +       case AuWbrCreate_PMFSRRV:
23970 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
23971 +               /*FALLTHROUGH*/
23972 +       case AuWbrCreate_MFS:
23973 +       case AuWbrCreate_MFSV:
23974 +       case AuWbrCreate_PMFS:
23975 +       case AuWbrCreate_PMFSV:
23976 +               sbinfo->si_wbr_mfs.mfs_expire
23977 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
23978 +               break;
23979 +       }
23980 +
23981 +       if (sbinfo->si_wbr_create_ops->init)
23982 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
23983 +
23984 +       return err;
23985 +}
23986 +
23987 +/*
23988 + * returns,
23989 + * plus: processed without an error
23990 + * zero: unprocessed
23991 + */
23992 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
23993 +                        struct au_opts *opts)
23994 +{
23995 +       int err;
23996 +       struct au_sbinfo *sbinfo;
23997 +
23998 +       SiMustWriteLock(sb);
23999 +
24000 +       err = 1; /* handled */
24001 +       sbinfo = au_sbi(sb);
24002 +       switch (opt->type) {
24003 +       case Opt_udba:
24004 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24005 +               sbinfo->si_mntflags |= opt->udba;
24006 +               opts->given_udba |= opt->udba;
24007 +               break;
24008 +
24009 +       case Opt_plink:
24010 +               au_opt_set(sbinfo->si_mntflags, PLINK);
24011 +               break;
24012 +       case Opt_noplink:
24013 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24014 +                       au_plink_put(sb, /*verbose*/1);
24015 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
24016 +               break;
24017 +       case Opt_list_plink:
24018 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24019 +                       au_plink_list(sb);
24020 +               break;
24021 +
24022 +       case Opt_dio:
24023 +               au_opt_set(sbinfo->si_mntflags, DIO);
24024 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24025 +               break;
24026 +       case Opt_nodio:
24027 +               au_opt_clr(sbinfo->si_mntflags, DIO);
24028 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24029 +               break;
24030 +
24031 +       case Opt_fhsm_sec:
24032 +               au_fhsm_set(sbinfo, opt->fhsm_second);
24033 +               break;
24034 +
24035 +       case Opt_diropq_a:
24036 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24037 +               break;
24038 +       case Opt_diropq_w:
24039 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24040 +               break;
24041 +
24042 +       case Opt_warn_perm:
24043 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24044 +               break;
24045 +       case Opt_nowarn_perm:
24046 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24047 +               break;
24048 +
24049 +       case Opt_verbose:
24050 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
24051 +               break;
24052 +       case Opt_noverbose:
24053 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24054 +               break;
24055 +
24056 +       case Opt_sum:
24057 +               au_opt_set(sbinfo->si_mntflags, SUM);
24058 +               break;
24059 +       case Opt_wsum:
24060 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24061 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
24062 +       case Opt_nosum:
24063 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24064 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
24065 +               break;
24066 +
24067 +       case Opt_wbr_create:
24068 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
24069 +               break;
24070 +       case Opt_wbr_copyup:
24071 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
24072 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24073 +               break;
24074 +
24075 +       case Opt_dirwh:
24076 +               sbinfo->si_dirwh = opt->dirwh;
24077 +               break;
24078 +
24079 +       case Opt_rdcache:
24080 +               sbinfo->si_rdcache
24081 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
24082 +               break;
24083 +       case Opt_rdblk:
24084 +               sbinfo->si_rdblk = opt->rdblk;
24085 +               break;
24086 +       case Opt_rdblk_def:
24087 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24088 +               break;
24089 +       case Opt_rdhash:
24090 +               sbinfo->si_rdhash = opt->rdhash;
24091 +               break;
24092 +       case Opt_rdhash_def:
24093 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24094 +               break;
24095 +
24096 +       case Opt_shwh:
24097 +               au_opt_set(sbinfo->si_mntflags, SHWH);
24098 +               break;
24099 +       case Opt_noshwh:
24100 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
24101 +               break;
24102 +
24103 +       case Opt_dirperm1:
24104 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24105 +               break;
24106 +       case Opt_nodirperm1:
24107 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24108 +               break;
24109 +
24110 +       case Opt_trunc_xino:
24111 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24112 +               break;
24113 +       case Opt_notrunc_xino:
24114 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24115 +               break;
24116 +
24117 +       case Opt_trunc_xino_path:
24118 +       case Opt_itrunc_xino:
24119 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24120 +               if (!err)
24121 +                       err = 1;
24122 +               break;
24123 +
24124 +       case Opt_trunc_xib:
24125 +               au_fset_opts(opts->flags, TRUNC_XIB);
24126 +               break;
24127 +       case Opt_notrunc_xib:
24128 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24129 +               break;
24130 +
24131 +       case Opt_acl:
24132 +               sb->s_flags |= MS_POSIXACL;
24133 +               break;
24134 +       case Opt_noacl:
24135 +               sb->s_flags &= ~MS_POSIXACL;
24136 +               break;
24137 +
24138 +       default:
24139 +               err = 0;
24140 +               break;
24141 +       }
24142 +
24143 +       return err;
24144 +}
24145 +
24146 +/*
24147 + * returns tri-state.
24148 + * plus: processed without an error
24149 + * zero: unprocessed
24150 + * minus: error
24151 + */
24152 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24153 +                    struct au_opts *opts)
24154 +{
24155 +       int err, do_refresh;
24156 +
24157 +       err = 0;
24158 +       switch (opt->type) {
24159 +       case Opt_append:
24160 +               opt->add.bindex = au_sbend(sb) + 1;
24161 +               if (opt->add.bindex < 0)
24162 +                       opt->add.bindex = 0;
24163 +               goto add;
24164 +       case Opt_prepend:
24165 +               opt->add.bindex = 0;
24166 +       add: /* indented label */
24167 +       case Opt_add:
24168 +               err = au_br_add(sb, &opt->add,
24169 +                               au_ftest_opts(opts->flags, REMOUNT));
24170 +               if (!err) {
24171 +                       err = 1;
24172 +                       au_fset_opts(opts->flags, REFRESH);
24173 +               }
24174 +               break;
24175 +
24176 +       case Opt_del:
24177 +       case Opt_idel:
24178 +               err = au_br_del(sb, &opt->del,
24179 +                               au_ftest_opts(opts->flags, REMOUNT));
24180 +               if (!err) {
24181 +                       err = 1;
24182 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24183 +                       au_fset_opts(opts->flags, REFRESH);
24184 +               }
24185 +               break;
24186 +
24187 +       case Opt_mod:
24188 +       case Opt_imod:
24189 +               err = au_br_mod(sb, &opt->mod,
24190 +                               au_ftest_opts(opts->flags, REMOUNT),
24191 +                               &do_refresh);
24192 +               if (!err) {
24193 +                       err = 1;
24194 +                       if (do_refresh)
24195 +                               au_fset_opts(opts->flags, REFRESH);
24196 +               }
24197 +               break;
24198 +       }
24199 +
24200 +       return err;
24201 +}
24202 +
24203 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24204 +                      struct au_opt_xino **opt_xino,
24205 +                      struct au_opts *opts)
24206 +{
24207 +       int err;
24208 +       aufs_bindex_t bend, bindex;
24209 +       struct dentry *root, *parent, *h_root;
24210 +
24211 +       err = 0;
24212 +       switch (opt->type) {
24213 +       case Opt_xino:
24214 +               err = au_xino_set(sb, &opt->xino,
24215 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24216 +               if (unlikely(err))
24217 +                       break;
24218 +
24219 +               *opt_xino = &opt->xino;
24220 +               au_xino_brid_set(sb, -1);
24221 +
24222 +               /* safe d_parent access */
24223 +               parent = opt->xino.file->f_path.dentry->d_parent;
24224 +               root = sb->s_root;
24225 +               bend = au_sbend(sb);
24226 +               for (bindex = 0; bindex <= bend; bindex++) {
24227 +                       h_root = au_h_dptr(root, bindex);
24228 +                       if (h_root == parent) {
24229 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24230 +                               break;
24231 +                       }
24232 +               }
24233 +               break;
24234 +
24235 +       case Opt_noxino:
24236 +               au_xino_clr(sb);
24237 +               au_xino_brid_set(sb, -1);
24238 +               *opt_xino = (void *)-1;
24239 +               break;
24240 +       }
24241 +
24242 +       return err;
24243 +}
24244 +
24245 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24246 +                  unsigned int pending)
24247 +{
24248 +       int err, fhsm;
24249 +       aufs_bindex_t bindex, bend;
24250 +       unsigned char do_plink, skip, do_free, can_no_dreval;
24251 +       struct au_branch *br;
24252 +       struct au_wbr *wbr;
24253 +       struct dentry *root, *dentry;
24254 +       struct inode *dir, *h_dir;
24255 +       struct au_sbinfo *sbinfo;
24256 +       struct au_hinode *hdir;
24257 +
24258 +       SiMustAnyLock(sb);
24259 +
24260 +       sbinfo = au_sbi(sb);
24261 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24262 +
24263 +       if (!(sb_flags & MS_RDONLY)) {
24264 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24265 +                       pr_warn("first branch should be rw\n");
24266 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24267 +                       pr_warn("shwh should be used with ro\n");
24268 +       }
24269 +
24270 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24271 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24272 +               pr_warn("udba=*notify requires xino\n");
24273 +
24274 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24275 +               pr_warn("dirperm1 breaks the protection"
24276 +                       " by the permission bits on the lower branch\n");
24277 +
24278 +       err = 0;
24279 +       fhsm = 0;
24280 +       root = sb->s_root;
24281 +       dir = d_inode(root);
24282 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24283 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24284 +                                     UDBA_NONE);
24285 +       bend = au_sbend(sb);
24286 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24287 +               skip = 0;
24288 +               h_dir = au_h_iptr(dir, bindex);
24289 +               br = au_sbr(sb, bindex);
24290 +
24291 +               if ((br->br_perm & AuBrAttr_ICEX)
24292 +                   && !h_dir->i_op->listxattr)
24293 +                       br->br_perm &= ~AuBrAttr_ICEX;
24294 +#if 0
24295 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24296 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24297 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24298 +#endif
24299 +
24300 +               do_free = 0;
24301 +               wbr = br->br_wbr;
24302 +               if (wbr)
24303 +                       wbr_wh_read_lock(wbr);
24304 +
24305 +               if (!au_br_writable(br->br_perm)) {
24306 +                       do_free = !!wbr;
24307 +                       skip = (!wbr
24308 +                               || (!wbr->wbr_whbase
24309 +                                   && !wbr->wbr_plink
24310 +                                   && !wbr->wbr_orph));
24311 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24312 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24313 +                       skip = (!wbr || !wbr->wbr_whbase);
24314 +                       if (skip && wbr) {
24315 +                               if (do_plink)
24316 +                                       skip = !!wbr->wbr_plink;
24317 +                               else
24318 +                                       skip = !wbr->wbr_plink;
24319 +                       }
24320 +               } else {
24321 +                       /* skip = (br->br_whbase && br->br_ohph); */
24322 +                       skip = (wbr && wbr->wbr_whbase);
24323 +                       if (skip) {
24324 +                               if (do_plink)
24325 +                                       skip = !!wbr->wbr_plink;
24326 +                               else
24327 +                                       skip = !wbr->wbr_plink;
24328 +                       }
24329 +               }
24330 +               if (wbr)
24331 +                       wbr_wh_read_unlock(wbr);
24332 +
24333 +               if (can_no_dreval) {
24334 +                       dentry = br->br_path.dentry;
24335 +                       spin_lock(&dentry->d_lock);
24336 +                       if (dentry->d_flags &
24337 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24338 +                               can_no_dreval = 0;
24339 +                       spin_unlock(&dentry->d_lock);
24340 +               }
24341 +
24342 +               if (au_br_fhsm(br->br_perm)) {
24343 +                       fhsm++;
24344 +                       AuDebugOn(!br->br_fhsm);
24345 +               }
24346 +
24347 +               if (skip)
24348 +                       continue;
24349 +
24350 +               hdir = au_hi(dir, bindex);
24351 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
24352 +               if (wbr)
24353 +                       wbr_wh_write_lock(wbr);
24354 +               err = au_wh_init(br, sb);
24355 +               if (wbr)
24356 +                       wbr_wh_write_unlock(wbr);
24357 +               au_hn_imtx_unlock(hdir);
24358 +
24359 +               if (!err && do_free) {
24360 +                       kfree(wbr);
24361 +                       br->br_wbr = NULL;
24362 +               }
24363 +       }
24364 +
24365 +       if (can_no_dreval)
24366 +               au_fset_si(sbinfo, NO_DREVAL);
24367 +       else
24368 +               au_fclr_si(sbinfo, NO_DREVAL);
24369 +
24370 +       if (fhsm >= 2) {
24371 +               au_fset_si(sbinfo, FHSM);
24372 +               for (bindex = bend; bindex >= 0; bindex--) {
24373 +                       br = au_sbr(sb, bindex);
24374 +                       if (au_br_fhsm(br->br_perm)) {
24375 +                               au_fhsm_set_bottom(sb, bindex);
24376 +                               break;
24377 +                       }
24378 +               }
24379 +       } else {
24380 +               au_fclr_si(sbinfo, FHSM);
24381 +               au_fhsm_set_bottom(sb, -1);
24382 +       }
24383 +
24384 +       return err;
24385 +}
24386 +
24387 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24388 +{
24389 +       int err;
24390 +       unsigned int tmp;
24391 +       aufs_bindex_t bindex, bend;
24392 +       struct au_opt *opt;
24393 +       struct au_opt_xino *opt_xino, xino;
24394 +       struct au_sbinfo *sbinfo;
24395 +       struct au_branch *br;
24396 +       struct inode *dir;
24397 +
24398 +       SiMustWriteLock(sb);
24399 +
24400 +       err = 0;
24401 +       opt_xino = NULL;
24402 +       opt = opts->opt;
24403 +       while (err >= 0 && opt->type != Opt_tail)
24404 +               err = au_opt_simple(sb, opt++, opts);
24405 +       if (err > 0)
24406 +               err = 0;
24407 +       else if (unlikely(err < 0))
24408 +               goto out;
24409 +
24410 +       /* disable xino and udba temporary */
24411 +       sbinfo = au_sbi(sb);
24412 +       tmp = sbinfo->si_mntflags;
24413 +       au_opt_clr(sbinfo->si_mntflags, XINO);
24414 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24415 +
24416 +       opt = opts->opt;
24417 +       while (err >= 0 && opt->type != Opt_tail)
24418 +               err = au_opt_br(sb, opt++, opts);
24419 +       if (err > 0)
24420 +               err = 0;
24421 +       else if (unlikely(err < 0))
24422 +               goto out;
24423 +
24424 +       bend = au_sbend(sb);
24425 +       if (unlikely(bend < 0)) {
24426 +               err = -EINVAL;
24427 +               pr_err("no branches\n");
24428 +               goto out;
24429 +       }
24430 +
24431 +       if (au_opt_test(tmp, XINO))
24432 +               au_opt_set(sbinfo->si_mntflags, XINO);
24433 +       opt = opts->opt;
24434 +       while (!err && opt->type != Opt_tail)
24435 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
24436 +       if (unlikely(err))
24437 +               goto out;
24438 +
24439 +       err = au_opts_verify(sb, sb->s_flags, tmp);
24440 +       if (unlikely(err))
24441 +               goto out;
24442 +
24443 +       /* restore xino */
24444 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
24445 +               xino.file = au_xino_def(sb);
24446 +               err = PTR_ERR(xino.file);
24447 +               if (IS_ERR(xino.file))
24448 +                       goto out;
24449 +
24450 +               err = au_xino_set(sb, &xino, /*remount*/0);
24451 +               fput(xino.file);
24452 +               if (unlikely(err))
24453 +                       goto out;
24454 +       }
24455 +
24456 +       /* restore udba */
24457 +       tmp &= AuOptMask_UDBA;
24458 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24459 +       sbinfo->si_mntflags |= tmp;
24460 +       bend = au_sbend(sb);
24461 +       for (bindex = 0; bindex <= bend; bindex++) {
24462 +               br = au_sbr(sb, bindex);
24463 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
24464 +               if (unlikely(err))
24465 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24466 +                               bindex, err);
24467 +               /* go on even if err */
24468 +       }
24469 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
24470 +               dir = d_inode(sb->s_root);
24471 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
24472 +       }
24473 +
24474 +out:
24475 +       return err;
24476 +}
24477 +
24478 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24479 +{
24480 +       int err, rerr;
24481 +       unsigned char no_dreval;
24482 +       struct inode *dir;
24483 +       struct au_opt_xino *opt_xino;
24484 +       struct au_opt *opt;
24485 +       struct au_sbinfo *sbinfo;
24486 +
24487 +       SiMustWriteLock(sb);
24488 +
24489 +       err = 0;
24490 +       dir = d_inode(sb->s_root);
24491 +       sbinfo = au_sbi(sb);
24492 +       opt_xino = NULL;
24493 +       opt = opts->opt;
24494 +       while (err >= 0 && opt->type != Opt_tail) {
24495 +               err = au_opt_simple(sb, opt, opts);
24496 +               if (!err)
24497 +                       err = au_opt_br(sb, opt, opts);
24498 +               if (!err)
24499 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
24500 +               opt++;
24501 +       }
24502 +       if (err > 0)
24503 +               err = 0;
24504 +       AuTraceErr(err);
24505 +       /* go on even err */
24506 +
24507 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
24508 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24509 +       if (unlikely(rerr && !err))
24510 +               err = rerr;
24511 +
24512 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
24513 +               au_fset_opts(opts->flags, REFRESH_IDOP);
24514 +
24515 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24516 +               rerr = au_xib_trunc(sb);
24517 +               if (unlikely(rerr && !err))
24518 +                       err = rerr;
24519 +       }
24520 +
24521 +       /* will be handled by the caller */
24522 +       if (!au_ftest_opts(opts->flags, REFRESH)
24523 +           && (opts->given_udba
24524 +               || au_opt_test(sbinfo->si_mntflags, XINO)
24525 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
24526 +                   ))
24527 +               au_fset_opts(opts->flags, REFRESH);
24528 +
24529 +       AuDbg("status 0x%x\n", opts->flags);
24530 +       return err;
24531 +}
24532 +
24533 +/* ---------------------------------------------------------------------- */
24534 +
24535 +unsigned int au_opt_udba(struct super_block *sb)
24536 +{
24537 +       return au_mntflags(sb) & AuOptMask_UDBA;
24538 +}
24539 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24540 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
24541 +++ linux/fs/aufs/opts.h        2016-01-13 20:11:11.673093671 +0100
24542 @@ -0,0 +1,211 @@
24543 +/*
24544 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24545 + *
24546 + * This program, aufs is free software; you can redistribute it and/or modify
24547 + * it under the terms of the GNU General Public License as published by
24548 + * the Free Software Foundation; either version 2 of the License, or
24549 + * (at your option) any later version.
24550 + *
24551 + * This program is distributed in the hope that it will be useful,
24552 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24553 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24554 + * GNU General Public License for more details.
24555 + *
24556 + * You should have received a copy of the GNU General Public License
24557 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24558 + */
24559 +
24560 +/*
24561 + * mount options/flags
24562 + */
24563 +
24564 +#ifndef __AUFS_OPTS_H__
24565 +#define __AUFS_OPTS_H__
24566 +
24567 +#ifdef __KERNEL__
24568 +
24569 +#include <linux/path.h>
24570 +
24571 +struct file;
24572 +struct super_block;
24573 +
24574 +/* ---------------------------------------------------------------------- */
24575 +
24576 +/* mount flags */
24577 +#define AuOpt_XINO             1               /* external inode number bitmap
24578 +                                                  and translation table */
24579 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
24580 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
24581 +#define AuOpt_UDBA_REVAL       (1 << 3)
24582 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
24583 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
24584 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
24585 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
24586 +                                                  bits */
24587 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
24588 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
24589 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
24590 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
24591 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
24592 +#define AuOpt_DIO              (1 << 14)       /* direct io */
24593 +
24594 +#ifndef CONFIG_AUFS_HNOTIFY
24595 +#undef AuOpt_UDBA_HNOTIFY
24596 +#define AuOpt_UDBA_HNOTIFY     0
24597 +#endif
24598 +#ifndef CONFIG_AUFS_SHWH
24599 +#undef AuOpt_SHWH
24600 +#define AuOpt_SHWH             0
24601 +#endif
24602 +
24603 +#define AuOpt_Def      (AuOpt_XINO \
24604 +                        | AuOpt_UDBA_REVAL \
24605 +                        | AuOpt_PLINK \
24606 +                        /* | AuOpt_DIRPERM1 */ \
24607 +                        | AuOpt_WARN_PERM)
24608 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24609 +                        | AuOpt_UDBA_REVAL \
24610 +                        | AuOpt_UDBA_HNOTIFY)
24611 +
24612 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
24613 +#define au_opt_set(flags, name) do { \
24614 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24615 +       ((flags) |= AuOpt_##name); \
24616 +} while (0)
24617 +#define au_opt_set_udba(flags, name) do { \
24618 +       (flags) &= ~AuOptMask_UDBA; \
24619 +       ((flags) |= AuOpt_##name); \
24620 +} while (0)
24621 +#define au_opt_clr(flags, name) do { \
24622 +       ((flags) &= ~AuOpt_##name); \
24623 +} while (0)
24624 +
24625 +static inline unsigned int au_opts_plink(unsigned int mntflags)
24626 +{
24627 +#ifdef CONFIG_PROC_FS
24628 +       return mntflags;
24629 +#else
24630 +       return mntflags & ~AuOpt_PLINK;
24631 +#endif
24632 +}
24633 +
24634 +/* ---------------------------------------------------------------------- */
24635 +
24636 +/* policies to select one among multiple writable branches */
24637 +enum {
24638 +       AuWbrCreate_TDP,        /* top down parent */
24639 +       AuWbrCreate_RR,         /* round robin */
24640 +       AuWbrCreate_MFS,        /* most free space */
24641 +       AuWbrCreate_MFSV,       /* mfs with seconds */
24642 +       AuWbrCreate_MFSRR,      /* mfs then rr */
24643 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
24644 +       AuWbrCreate_PMFS,       /* parent and mfs */
24645 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
24646 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
24647 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
24648 +
24649 +       AuWbrCreate_Def = AuWbrCreate_TDP
24650 +};
24651 +
24652 +enum {
24653 +       AuWbrCopyup_TDP,        /* top down parent */
24654 +       AuWbrCopyup_BUP,        /* bottom up parent */
24655 +       AuWbrCopyup_BU,         /* bottom up */
24656 +
24657 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
24658 +};
24659 +
24660 +/* ---------------------------------------------------------------------- */
24661 +
24662 +struct au_opt_add {
24663 +       aufs_bindex_t   bindex;
24664 +       char            *pathname;
24665 +       int             perm;
24666 +       struct path     path;
24667 +};
24668 +
24669 +struct au_opt_del {
24670 +       char            *pathname;
24671 +       struct path     h_path;
24672 +};
24673 +
24674 +struct au_opt_mod {
24675 +       char            *path;
24676 +       int             perm;
24677 +       struct dentry   *h_root;
24678 +};
24679 +
24680 +struct au_opt_xino {
24681 +       char            *path;
24682 +       struct file     *file;
24683 +};
24684 +
24685 +struct au_opt_xino_itrunc {
24686 +       aufs_bindex_t   bindex;
24687 +};
24688 +
24689 +struct au_opt_wbr_create {
24690 +       int                     wbr_create;
24691 +       int                     mfs_second;
24692 +       unsigned long long      mfsrr_watermark;
24693 +};
24694 +
24695 +struct au_opt {
24696 +       int type;
24697 +       union {
24698 +               struct au_opt_xino      xino;
24699 +               struct au_opt_xino_itrunc xino_itrunc;
24700 +               struct au_opt_add       add;
24701 +               struct au_opt_del       del;
24702 +               struct au_opt_mod       mod;
24703 +               int                     dirwh;
24704 +               int                     rdcache;
24705 +               unsigned int            rdblk;
24706 +               unsigned int            rdhash;
24707 +               int                     udba;
24708 +               struct au_opt_wbr_create wbr_create;
24709 +               int                     wbr_copyup;
24710 +               unsigned int            fhsm_second;
24711 +       };
24712 +};
24713 +
24714 +/* opts flags */
24715 +#define AuOpts_REMOUNT         1
24716 +#define AuOpts_REFRESH         (1 << 1)
24717 +#define AuOpts_TRUNC_XIB       (1 << 2)
24718 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
24719 +#define AuOpts_REFRESH_IDOP    (1 << 4)
24720 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
24721 +#define au_fset_opts(flags, name) \
24722 +       do { (flags) |= AuOpts_##name; } while (0)
24723 +#define au_fclr_opts(flags, name) \
24724 +       do { (flags) &= ~AuOpts_##name; } while (0)
24725 +
24726 +struct au_opts {
24727 +       struct au_opt   *opt;
24728 +       int             max_opt;
24729 +
24730 +       unsigned int    given_udba;
24731 +       unsigned int    flags;
24732 +       unsigned long   sb_flags;
24733 +};
24734 +
24735 +/* ---------------------------------------------------------------------- */
24736 +
24737 +/* opts.c */
24738 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
24739 +const char *au_optstr_udba(int udba);
24740 +const char *au_optstr_wbr_copyup(int wbr_copyup);
24741 +const char *au_optstr_wbr_create(int wbr_create);
24742 +
24743 +void au_opts_free(struct au_opts *opts);
24744 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24745 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24746 +                  unsigned int pending);
24747 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24748 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24749 +
24750 +unsigned int au_opt_udba(struct super_block *sb);
24751 +
24752 +#endif /* __KERNEL__ */
24753 +#endif /* __AUFS_OPTS_H__ */
24754 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24755 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
24756 +++ linux/fs/aufs/plink.c       2016-01-13 20:11:11.673093671 +0100
24757 @@ -0,0 +1,528 @@
24758 +/*
24759 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24760 + *
24761 + * This program, aufs is free software; you can redistribute it and/or modify
24762 + * it under the terms of the GNU General Public License as published by
24763 + * the Free Software Foundation; either version 2 of the License, or
24764 + * (at your option) any later version.
24765 + *
24766 + * This program is distributed in the hope that it will be useful,
24767 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24768 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24769 + * GNU General Public License for more details.
24770 + *
24771 + * You should have received a copy of the GNU General Public License
24772 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24773 + */
24774 +
24775 +/*
24776 + * pseudo-link
24777 + */
24778 +
24779 +#include "aufs.h"
24780 +
24781 +/*
24782 + * the pseudo-link maintenance mode.
24783 + * during a user process maintains the pseudo-links,
24784 + * prohibit adding a new plink and branch manipulation.
24785 + *
24786 + * Flags
24787 + * NOPLM:
24788 + *     For entry functions which will handle plink, and i_mutex is already held
24789 + *     in VFS.
24790 + *     They cannot wait and should return an error at once.
24791 + *     Callers has to check the error.
24792 + * NOPLMW:
24793 + *     For entry functions which will handle plink, but i_mutex is not held
24794 + *     in VFS.
24795 + *     They can wait the plink maintenance mode to finish.
24796 + *
24797 + * They behave like F_SETLK and F_SETLKW.
24798 + * If the caller never handle plink, then both flags are unnecessary.
24799 + */
24800 +
24801 +int au_plink_maint(struct super_block *sb, int flags)
24802 +{
24803 +       int err;
24804 +       pid_t pid, ppid;
24805 +       struct au_sbinfo *sbi;
24806 +
24807 +       SiMustAnyLock(sb);
24808 +
24809 +       err = 0;
24810 +       if (!au_opt_test(au_mntflags(sb), PLINK))
24811 +               goto out;
24812 +
24813 +       sbi = au_sbi(sb);
24814 +       pid = sbi->si_plink_maint_pid;
24815 +       if (!pid || pid == current->pid)
24816 +               goto out;
24817 +
24818 +       /* todo: it highly depends upon /sbin/mount.aufs */
24819 +       rcu_read_lock();
24820 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
24821 +       rcu_read_unlock();
24822 +       if (pid == ppid)
24823 +               goto out;
24824 +
24825 +       if (au_ftest_lock(flags, NOPLMW)) {
24826 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
24827 +               /* AuDebugOn(!lockdep_depth(current)); */
24828 +               while (sbi->si_plink_maint_pid) {
24829 +                       si_read_unlock(sb);
24830 +                       /* gave up wake_up_bit() */
24831 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
24832 +
24833 +                       if (au_ftest_lock(flags, FLUSH))
24834 +                               au_nwt_flush(&sbi->si_nowait);
24835 +                       si_noflush_read_lock(sb);
24836 +               }
24837 +       } else if (au_ftest_lock(flags, NOPLM)) {
24838 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
24839 +               err = -EAGAIN;
24840 +       }
24841 +
24842 +out:
24843 +       return err;
24844 +}
24845 +
24846 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
24847 +{
24848 +       spin_lock(&sbinfo->si_plink_maint_lock);
24849 +       sbinfo->si_plink_maint_pid = 0;
24850 +       spin_unlock(&sbinfo->si_plink_maint_lock);
24851 +       wake_up_all(&sbinfo->si_plink_wq);
24852 +}
24853 +
24854 +int au_plink_maint_enter(struct super_block *sb)
24855 +{
24856 +       int err;
24857 +       struct au_sbinfo *sbinfo;
24858 +
24859 +       err = 0;
24860 +       sbinfo = au_sbi(sb);
24861 +       /* make sure i am the only one in this fs */
24862 +       si_write_lock(sb, AuLock_FLUSH);
24863 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
24864 +               spin_lock(&sbinfo->si_plink_maint_lock);
24865 +               if (!sbinfo->si_plink_maint_pid)
24866 +                       sbinfo->si_plink_maint_pid = current->pid;
24867 +               else
24868 +                       err = -EBUSY;
24869 +               spin_unlock(&sbinfo->si_plink_maint_lock);
24870 +       }
24871 +       si_write_unlock(sb);
24872 +
24873 +       return err;
24874 +}
24875 +
24876 +/* ---------------------------------------------------------------------- */
24877 +
24878 +#ifdef CONFIG_AUFS_DEBUG
24879 +void au_plink_list(struct super_block *sb)
24880 +{
24881 +       int i;
24882 +       struct au_sbinfo *sbinfo;
24883 +       struct hlist_head *plink_hlist;
24884 +       struct pseudo_link *plink;
24885 +
24886 +       SiMustAnyLock(sb);
24887 +
24888 +       sbinfo = au_sbi(sb);
24889 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24890 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24891 +
24892 +       for (i = 0; i < AuPlink_NHASH; i++) {
24893 +               plink_hlist = &sbinfo->si_plink[i].head;
24894 +               rcu_read_lock();
24895 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24896 +                       AuDbg("%lu\n", plink->inode->i_ino);
24897 +               rcu_read_unlock();
24898 +       }
24899 +}
24900 +#endif
24901 +
24902 +/* is the inode pseudo-linked? */
24903 +int au_plink_test(struct inode *inode)
24904 +{
24905 +       int found, i;
24906 +       struct au_sbinfo *sbinfo;
24907 +       struct hlist_head *plink_hlist;
24908 +       struct pseudo_link *plink;
24909 +
24910 +       sbinfo = au_sbi(inode->i_sb);
24911 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24912 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
24913 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24914 +
24915 +       found = 0;
24916 +       i = au_plink_hash(inode->i_ino);
24917 +       plink_hlist = &sbinfo->si_plink[i].head;
24918 +       rcu_read_lock();
24919 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24920 +               if (plink->inode == inode) {
24921 +                       found = 1;
24922 +                       break;
24923 +               }
24924 +       rcu_read_unlock();
24925 +       return found;
24926 +}
24927 +
24928 +/* ---------------------------------------------------------------------- */
24929 +
24930 +/*
24931 + * generate a name for plink.
24932 + * the file will be stored under AUFS_WH_PLINKDIR.
24933 + */
24934 +/* 20 is max digits length of ulong 64 */
24935 +#define PLINK_NAME_LEN ((20 + 1) * 2)
24936 +
24937 +static int plink_name(char *name, int len, struct inode *inode,
24938 +                     aufs_bindex_t bindex)
24939 +{
24940 +       int rlen;
24941 +       struct inode *h_inode;
24942 +
24943 +       h_inode = au_h_iptr(inode, bindex);
24944 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
24945 +       return rlen;
24946 +}
24947 +
24948 +struct au_do_plink_lkup_args {
24949 +       struct dentry **errp;
24950 +       struct qstr *tgtname;
24951 +       struct dentry *h_parent;
24952 +       struct au_branch *br;
24953 +};
24954 +
24955 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
24956 +                                      struct dentry *h_parent,
24957 +                                      struct au_branch *br)
24958 +{
24959 +       struct dentry *h_dentry;
24960 +       struct mutex *h_mtx;
24961 +
24962 +       h_mtx = &d_inode(h_parent)->i_mutex;
24963 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
24964 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
24965 +       mutex_unlock(h_mtx);
24966 +       return h_dentry;
24967 +}
24968 +
24969 +static void au_call_do_plink_lkup(void *args)
24970 +{
24971 +       struct au_do_plink_lkup_args *a = args;
24972 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
24973 +}
24974 +
24975 +/* lookup the plink-ed @inode under the branch at @bindex */
24976 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
24977 +{
24978 +       struct dentry *h_dentry, *h_parent;
24979 +       struct au_branch *br;
24980 +       int wkq_err;
24981 +       char a[PLINK_NAME_LEN];
24982 +       struct qstr tgtname = QSTR_INIT(a, 0);
24983 +
24984 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24985 +
24986 +       br = au_sbr(inode->i_sb, bindex);
24987 +       h_parent = br->br_wbr->wbr_plink;
24988 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24989 +
24990 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24991 +               struct au_do_plink_lkup_args args = {
24992 +                       .errp           = &h_dentry,
24993 +                       .tgtname        = &tgtname,
24994 +                       .h_parent       = h_parent,
24995 +                       .br             = br
24996 +               };
24997 +
24998 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
24999 +               if (unlikely(wkq_err))
25000 +                       h_dentry = ERR_PTR(wkq_err);
25001 +       } else
25002 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25003 +
25004 +       return h_dentry;
25005 +}
25006 +
25007 +/* create a pseudo-link */
25008 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25009 +                     struct dentry *h_dentry, struct au_branch *br)
25010 +{
25011 +       int err;
25012 +       struct path h_path = {
25013 +               .mnt = au_br_mnt(br)
25014 +       };
25015 +       struct inode *h_dir, *delegated;
25016 +
25017 +       h_dir = d_inode(h_parent);
25018 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
25019 +again:
25020 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
25021 +       err = PTR_ERR(h_path.dentry);
25022 +       if (IS_ERR(h_path.dentry))
25023 +               goto out;
25024 +
25025 +       err = 0;
25026 +       /* wh.plink dir is not monitored */
25027 +       /* todo: is it really safe? */
25028 +       if (d_is_positive(h_path.dentry)
25029 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
25030 +               delegated = NULL;
25031 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25032 +               if (unlikely(err == -EWOULDBLOCK)) {
25033 +                       pr_warn("cannot retry for NFSv4 delegation"
25034 +                               " for an internal unlink\n");
25035 +                       iput(delegated);
25036 +               }
25037 +               dput(h_path.dentry);
25038 +               h_path.dentry = NULL;
25039 +               if (!err)
25040 +                       goto again;
25041 +       }
25042 +       if (!err && d_is_negative(h_path.dentry)) {
25043 +               delegated = NULL;
25044 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25045 +               if (unlikely(err == -EWOULDBLOCK)) {
25046 +                       pr_warn("cannot retry for NFSv4 delegation"
25047 +                               " for an internal link\n");
25048 +                       iput(delegated);
25049 +               }
25050 +       }
25051 +       dput(h_path.dentry);
25052 +
25053 +out:
25054 +       mutex_unlock(&h_dir->i_mutex);
25055 +       return err;
25056 +}
25057 +
25058 +struct do_whplink_args {
25059 +       int *errp;
25060 +       struct qstr *tgt;
25061 +       struct dentry *h_parent;
25062 +       struct dentry *h_dentry;
25063 +       struct au_branch *br;
25064 +};
25065 +
25066 +static void call_do_whplink(void *args)
25067 +{
25068 +       struct do_whplink_args *a = args;
25069 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25070 +}
25071 +
25072 +static int whplink(struct dentry *h_dentry, struct inode *inode,
25073 +                  aufs_bindex_t bindex, struct au_branch *br)
25074 +{
25075 +       int err, wkq_err;
25076 +       struct au_wbr *wbr;
25077 +       struct dentry *h_parent;
25078 +       char a[PLINK_NAME_LEN];
25079 +       struct qstr tgtname = QSTR_INIT(a, 0);
25080 +
25081 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25082 +       h_parent = wbr->wbr_plink;
25083 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25084 +
25085 +       /* always superio. */
25086 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25087 +               struct do_whplink_args args = {
25088 +                       .errp           = &err,
25089 +                       .tgt            = &tgtname,
25090 +                       .h_parent       = h_parent,
25091 +                       .h_dentry       = h_dentry,
25092 +                       .br             = br
25093 +               };
25094 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
25095 +               if (unlikely(wkq_err))
25096 +                       err = wkq_err;
25097 +       } else
25098 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
25099 +
25100 +       return err;
25101 +}
25102 +
25103 +/* free a single plink */
25104 +static void do_put_plink(struct pseudo_link *plink, int do_del)
25105 +{
25106 +       if (do_del)
25107 +               hlist_del(&plink->hlist);
25108 +       iput(plink->inode);
25109 +       kfree(plink);
25110 +}
25111 +
25112 +static void do_put_plink_rcu(struct rcu_head *rcu)
25113 +{
25114 +       struct pseudo_link *plink;
25115 +
25116 +       plink = container_of(rcu, struct pseudo_link, rcu);
25117 +       iput(plink->inode);
25118 +       kfree(plink);
25119 +}
25120 +
25121 +/*
25122 + * create a new pseudo-link for @h_dentry on @bindex.
25123 + * the linked inode is held in aufs @inode.
25124 + */
25125 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25126 +                    struct dentry *h_dentry)
25127 +{
25128 +       struct super_block *sb;
25129 +       struct au_sbinfo *sbinfo;
25130 +       struct hlist_head *plink_hlist;
25131 +       struct pseudo_link *plink, *tmp;
25132 +       struct au_sphlhead *sphl;
25133 +       int found, err, cnt, i;
25134 +
25135 +       sb = inode->i_sb;
25136 +       sbinfo = au_sbi(sb);
25137 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25138 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25139 +
25140 +       found = au_plink_test(inode);
25141 +       if (found)
25142 +               return;
25143 +
25144 +       i = au_plink_hash(inode->i_ino);
25145 +       sphl = sbinfo->si_plink + i;
25146 +       plink_hlist = &sphl->head;
25147 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
25148 +       if (tmp)
25149 +               tmp->inode = au_igrab(inode);
25150 +       else {
25151 +               err = -ENOMEM;
25152 +               goto out;
25153 +       }
25154 +
25155 +       spin_lock(&sphl->spin);
25156 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
25157 +               if (plink->inode == inode) {
25158 +                       found = 1;
25159 +                       break;
25160 +               }
25161 +       }
25162 +       if (!found)
25163 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
25164 +       spin_unlock(&sphl->spin);
25165 +       if (!found) {
25166 +               cnt = au_sphl_count(sphl);
25167 +#define msg "unexpectedly unblanced or too many pseudo-links"
25168 +               if (cnt > AUFS_PLINK_WARN)
25169 +                       AuWarn1(msg ", %d\n", cnt);
25170 +#undef msg
25171 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25172 +       } else {
25173 +               do_put_plink(tmp, 0);
25174 +               return;
25175 +       }
25176 +
25177 +out:
25178 +       if (unlikely(err)) {
25179 +               pr_warn("err %d, damaged pseudo link.\n", err);
25180 +               if (tmp) {
25181 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
25182 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
25183 +               }
25184 +       }
25185 +}
25186 +
25187 +/* free all plinks */
25188 +void au_plink_put(struct super_block *sb, int verbose)
25189 +{
25190 +       int i, warned;
25191 +       struct au_sbinfo *sbinfo;
25192 +       struct hlist_head *plink_hlist;
25193 +       struct hlist_node *tmp;
25194 +       struct pseudo_link *plink;
25195 +
25196 +       SiMustWriteLock(sb);
25197 +
25198 +       sbinfo = au_sbi(sb);
25199 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25200 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25201 +
25202 +       /* no spin_lock since sbinfo is write-locked */
25203 +       warned = 0;
25204 +       for (i = 0; i < AuPlink_NHASH; i++) {
25205 +               plink_hlist = &sbinfo->si_plink[i].head;
25206 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25207 +                       pr_warn("pseudo-link is not flushed");
25208 +                       warned = 1;
25209 +               }
25210 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
25211 +                       do_put_plink(plink, 0);
25212 +               INIT_HLIST_HEAD(plink_hlist);
25213 +       }
25214 +}
25215 +
25216 +void au_plink_clean(struct super_block *sb, int verbose)
25217 +{
25218 +       struct dentry *root;
25219 +
25220 +       root = sb->s_root;
25221 +       aufs_write_lock(root);
25222 +       if (au_opt_test(au_mntflags(sb), PLINK))
25223 +               au_plink_put(sb, verbose);
25224 +       aufs_write_unlock(root);
25225 +}
25226 +
25227 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25228 +{
25229 +       int do_put;
25230 +       aufs_bindex_t bstart, bend, bindex;
25231 +
25232 +       do_put = 0;
25233 +       bstart = au_ibstart(inode);
25234 +       bend = au_ibend(inode);
25235 +       if (bstart >= 0) {
25236 +               for (bindex = bstart; bindex <= bend; bindex++) {
25237 +                       if (!au_h_iptr(inode, bindex)
25238 +                           || au_ii_br_id(inode, bindex) != br_id)
25239 +                               continue;
25240 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25241 +                       do_put = 1;
25242 +                       break;
25243 +               }
25244 +               if (do_put)
25245 +                       for (bindex = bstart; bindex <= bend; bindex++)
25246 +                               if (au_h_iptr(inode, bindex)) {
25247 +                                       do_put = 0;
25248 +                                       break;
25249 +                               }
25250 +       } else
25251 +               do_put = 1;
25252 +
25253 +       return do_put;
25254 +}
25255 +
25256 +/* free the plinks on a branch specified by @br_id */
25257 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25258 +{
25259 +       struct au_sbinfo *sbinfo;
25260 +       struct hlist_head *plink_hlist;
25261 +       struct hlist_node *tmp;
25262 +       struct pseudo_link *plink;
25263 +       struct inode *inode;
25264 +       int i, do_put;
25265 +
25266 +       SiMustWriteLock(sb);
25267 +
25268 +       sbinfo = au_sbi(sb);
25269 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25270 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25271 +
25272 +       /* no spin_lock since sbinfo is write-locked */
25273 +       for (i = 0; i < AuPlink_NHASH; i++) {
25274 +               plink_hlist = &sbinfo->si_plink[i].head;
25275 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
25276 +                       inode = au_igrab(plink->inode);
25277 +                       ii_write_lock_child(inode);
25278 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25279 +                       if (do_put)
25280 +                               do_put_plink(plink, 1);
25281 +                       ii_write_unlock(inode);
25282 +                       iput(inode);
25283 +               }
25284 +       }
25285 +}
25286 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25287 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25288 +++ linux/fs/aufs/poll.c        2016-01-13 20:11:11.673093671 +0100
25289 @@ -0,0 +1,52 @@
25290 +/*
25291 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25292 + *
25293 + * This program, aufs is free software; you can redistribute it and/or modify
25294 + * it under the terms of the GNU General Public License as published by
25295 + * the Free Software Foundation; either version 2 of the License, or
25296 + * (at your option) any later version.
25297 + *
25298 + * This program is distributed in the hope that it will be useful,
25299 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25300 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25301 + * GNU General Public License for more details.
25302 + *
25303 + * You should have received a copy of the GNU General Public License
25304 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25305 + */
25306 +
25307 +/*
25308 + * poll operation
25309 + * There is only one filesystem which implements ->poll operation, currently.
25310 + */
25311 +
25312 +#include "aufs.h"
25313 +
25314 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25315 +{
25316 +       unsigned int mask;
25317 +       int err;
25318 +       struct file *h_file;
25319 +       struct super_block *sb;
25320 +
25321 +       /* We should pretend an error happened. */
25322 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25323 +       sb = file->f_path.dentry->d_sb;
25324 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25325 +
25326 +       h_file = au_read_pre(file, /*keep_fi*/0);
25327 +       err = PTR_ERR(h_file);
25328 +       if (IS_ERR(h_file))
25329 +               goto out;
25330 +
25331 +       /* it is not an error if h_file has no operation */
25332 +       mask = DEFAULT_POLLMASK;
25333 +       if (h_file->f_op->poll)
25334 +               mask = h_file->f_op->poll(h_file, wait);
25335 +       fput(h_file); /* instead of au_read_post() */
25336 +
25337 +out:
25338 +       si_read_unlock(sb);
25339 +       AuTraceErr((int)mask);
25340 +       return mask;
25341 +}
25342 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25343 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25344 +++ linux/fs/aufs/posix_acl.c   2016-01-13 20:11:11.673093671 +0100
25345 @@ -0,0 +1,99 @@
25346 +/*
25347 + * Copyright (C) 2014-2015 Junjiro R. Okajima
25348 + *
25349 + * This program, aufs is free software; you can redistribute it and/or modify
25350 + * it under the terms of the GNU General Public License as published by
25351 + * the Free Software Foundation; either version 2 of the License, or
25352 + * (at your option) any later version.
25353 + *
25354 + * This program is distributed in the hope that it will be useful,
25355 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25356 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25357 + * GNU General Public License for more details.
25358 + *
25359 + * You should have received a copy of the GNU General Public License
25360 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25361 + */
25362 +
25363 +/*
25364 + * posix acl operations
25365 + */
25366 +
25367 +#include <linux/fs.h>
25368 +#include <linux/posix_acl.h>
25369 +#include "aufs.h"
25370 +
25371 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25372 +{
25373 +       struct posix_acl *acl;
25374 +       int err;
25375 +       aufs_bindex_t bindex;
25376 +       struct inode *h_inode;
25377 +       struct super_block *sb;
25378 +
25379 +       acl = NULL;
25380 +       sb = inode->i_sb;
25381 +       si_read_lock(sb, AuLock_FLUSH);
25382 +       ii_read_lock_child(inode);
25383 +       if (!(sb->s_flags & MS_POSIXACL))
25384 +               goto out;
25385 +
25386 +       bindex = au_ibstart(inode);
25387 +       h_inode = au_h_iptr(inode, bindex);
25388 +       if (unlikely(!h_inode
25389 +                    || ((h_inode->i_mode & S_IFMT)
25390 +                        != (inode->i_mode & S_IFMT)))) {
25391 +               err = au_busy_or_stale();
25392 +               acl = ERR_PTR(err);
25393 +               goto out;
25394 +       }
25395 +
25396 +       /* always topmost only */
25397 +       acl = get_acl(h_inode, type);
25398 +
25399 +out:
25400 +       ii_read_unlock(inode);
25401 +       si_read_unlock(sb);
25402 +
25403 +       AuTraceErrPtr(acl);
25404 +       return acl;
25405 +}
25406 +
25407 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25408 +{
25409 +       int err;
25410 +       ssize_t ssz;
25411 +       struct dentry *dentry;
25412 +       struct au_srxattr arg = {
25413 +               .type = AU_ACL_SET,
25414 +               .u.acl_set = {
25415 +                       .acl    = acl,
25416 +                       .type   = type
25417 +               },
25418 +       };
25419 +
25420 +       mutex_lock(&inode->i_mutex);
25421 +       if (inode->i_ino == AUFS_ROOT_INO)
25422 +               dentry = dget(inode->i_sb->s_root);
25423 +       else {
25424 +               dentry = d_find_alias(inode);
25425 +               if (!dentry)
25426 +                       dentry = d_find_any_alias(inode);
25427 +               if (!dentry) {
25428 +                       pr_warn("cannot handle this inode, "
25429 +                               "please report to aufs-users ML\n");
25430 +                       err = -ENOENT;
25431 +                       goto out;
25432 +               }
25433 +       }
25434 +
25435 +       ssz = au_srxattr(dentry, &arg);
25436 +       dput(dentry);
25437 +       err = ssz;
25438 +       if (ssz >= 0)
25439 +               err = 0;
25440 +
25441 +out:
25442 +       mutex_unlock(&inode->i_mutex);
25443 +       return err;
25444 +}
25445 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25446 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
25447 +++ linux/fs/aufs/procfs.c      2016-01-13 20:11:11.673093671 +0100
25448 @@ -0,0 +1,169 @@
25449 +/*
25450 + * Copyright (C) 2010-2015 Junjiro R. Okajima
25451 + *
25452 + * This program, aufs is free software; you can redistribute it and/or modify
25453 + * it under the terms of the GNU General Public License as published by
25454 + * the Free Software Foundation; either version 2 of the License, or
25455 + * (at your option) any later version.
25456 + *
25457 + * This program is distributed in the hope that it will be useful,
25458 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25459 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25460 + * GNU General Public License for more details.
25461 + *
25462 + * You should have received a copy of the GNU General Public License
25463 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25464 + */
25465 +
25466 +/*
25467 + * procfs interfaces
25468 + */
25469 +
25470 +#include <linux/proc_fs.h>
25471 +#include "aufs.h"
25472 +
25473 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
25474 +{
25475 +       struct au_sbinfo *sbinfo;
25476 +
25477 +       sbinfo = file->private_data;
25478 +       if (sbinfo) {
25479 +               au_plink_maint_leave(sbinfo);
25480 +               kobject_put(&sbinfo->si_kobj);
25481 +       }
25482 +
25483 +       return 0;
25484 +}
25485 +
25486 +static void au_procfs_plm_write_clean(struct file *file)
25487 +{
25488 +       struct au_sbinfo *sbinfo;
25489 +
25490 +       sbinfo = file->private_data;
25491 +       if (sbinfo)
25492 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25493 +}
25494 +
25495 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25496 +{
25497 +       int err;
25498 +       struct super_block *sb;
25499 +       struct au_sbinfo *sbinfo;
25500 +
25501 +       err = -EBUSY;
25502 +       if (unlikely(file->private_data))
25503 +               goto out;
25504 +
25505 +       sb = NULL;
25506 +       /* don't use au_sbilist_lock() here */
25507 +       spin_lock(&au_sbilist.spin);
25508 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
25509 +               if (id == sysaufs_si_id(sbinfo)) {
25510 +                       kobject_get(&sbinfo->si_kobj);
25511 +                       sb = sbinfo->si_sb;
25512 +                       break;
25513 +               }
25514 +       spin_unlock(&au_sbilist.spin);
25515 +
25516 +       err = -EINVAL;
25517 +       if (unlikely(!sb))
25518 +               goto out;
25519 +
25520 +       err = au_plink_maint_enter(sb);
25521 +       if (!err)
25522 +               /* keep kobject_get() */
25523 +               file->private_data = sbinfo;
25524 +       else
25525 +               kobject_put(&sbinfo->si_kobj);
25526 +out:
25527 +       return err;
25528 +}
25529 +
25530 +/*
25531 + * Accept a valid "si=xxxx" only.
25532 + * Once it is accepted successfully, accept "clean" too.
25533 + */
25534 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25535 +                                  size_t count, loff_t *ppos)
25536 +{
25537 +       ssize_t err;
25538 +       unsigned long id;
25539 +       /* last newline is allowed */
25540 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
25541 +
25542 +       err = -EACCES;
25543 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25544 +               goto out;
25545 +
25546 +       err = -EINVAL;
25547 +       if (unlikely(count > sizeof(buf)))
25548 +               goto out;
25549 +
25550 +       err = copy_from_user(buf, ubuf, count);
25551 +       if (unlikely(err)) {
25552 +               err = -EFAULT;
25553 +               goto out;
25554 +       }
25555 +       buf[count] = 0;
25556 +
25557 +       err = -EINVAL;
25558 +       if (!strcmp("clean", buf)) {
25559 +               au_procfs_plm_write_clean(file);
25560 +               goto out_success;
25561 +       } else if (unlikely(strncmp("si=", buf, 3)))
25562 +               goto out;
25563 +
25564 +       err = kstrtoul(buf + 3, 16, &id);
25565 +       if (unlikely(err))
25566 +               goto out;
25567 +
25568 +       err = au_procfs_plm_write_si(file, id);
25569 +       if (unlikely(err))
25570 +               goto out;
25571 +
25572 +out_success:
25573 +       err = count; /* success */
25574 +out:
25575 +       return err;
25576 +}
25577 +
25578 +static const struct file_operations au_procfs_plm_fop = {
25579 +       .write          = au_procfs_plm_write,
25580 +       .release        = au_procfs_plm_release,
25581 +       .owner          = THIS_MODULE
25582 +};
25583 +
25584 +/* ---------------------------------------------------------------------- */
25585 +
25586 +static struct proc_dir_entry *au_procfs_dir;
25587 +
25588 +void au_procfs_fin(void)
25589 +{
25590 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25591 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25592 +}
25593 +
25594 +int __init au_procfs_init(void)
25595 +{
25596 +       int err;
25597 +       struct proc_dir_entry *entry;
25598 +
25599 +       err = -ENOMEM;
25600 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25601 +       if (unlikely(!au_procfs_dir))
25602 +               goto out;
25603 +
25604 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25605 +                           au_procfs_dir, &au_procfs_plm_fop);
25606 +       if (unlikely(!entry))
25607 +               goto out_dir;
25608 +
25609 +       err = 0;
25610 +       goto out; /* success */
25611 +
25612 +
25613 +out_dir:
25614 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25615 +out:
25616 +       return err;
25617 +}
25618 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25619 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
25620 +++ linux/fs/aufs/rdu.c 2016-01-13 20:11:11.673093671 +0100
25621 @@ -0,0 +1,388 @@
25622 +/*
25623 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25624 + *
25625 + * This program, aufs is free software; you can redistribute it and/or modify
25626 + * it under the terms of the GNU General Public License as published by
25627 + * the Free Software Foundation; either version 2 of the License, or
25628 + * (at your option) any later version.
25629 + *
25630 + * This program is distributed in the hope that it will be useful,
25631 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25632 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25633 + * GNU General Public License for more details.
25634 + *
25635 + * You should have received a copy of the GNU General Public License
25636 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25637 + */
25638 +
25639 +/*
25640 + * readdir in userspace.
25641 + */
25642 +
25643 +#include <linux/compat.h>
25644 +#include <linux/fs_stack.h>
25645 +#include <linux/security.h>
25646 +#include "aufs.h"
25647 +
25648 +/* bits for struct aufs_rdu.flags */
25649 +#define        AuRdu_CALLED    1
25650 +#define        AuRdu_CONT      (1 << 1)
25651 +#define        AuRdu_FULL      (1 << 2)
25652 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
25653 +#define au_fset_rdu(flags, name) \
25654 +       do { (flags) |= AuRdu_##name; } while (0)
25655 +#define au_fclr_rdu(flags, name) \
25656 +       do { (flags) &= ~AuRdu_##name; } while (0)
25657 +
25658 +struct au_rdu_arg {
25659 +       struct dir_context              ctx;
25660 +       struct aufs_rdu                 *rdu;
25661 +       union au_rdu_ent_ul             ent;
25662 +       unsigned long                   end;
25663 +
25664 +       struct super_block              *sb;
25665 +       int                             err;
25666 +};
25667 +
25668 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
25669 +                      loff_t offset, u64 h_ino, unsigned int d_type)
25670 +{
25671 +       int err, len;
25672 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
25673 +       struct aufs_rdu *rdu = arg->rdu;
25674 +       struct au_rdu_ent ent;
25675 +
25676 +       err = 0;
25677 +       arg->err = 0;
25678 +       au_fset_rdu(rdu->cookie.flags, CALLED);
25679 +       len = au_rdu_len(nlen);
25680 +       if (arg->ent.ul + len  < arg->end) {
25681 +               ent.ino = h_ino;
25682 +               ent.bindex = rdu->cookie.bindex;
25683 +               ent.type = d_type;
25684 +               ent.nlen = nlen;
25685 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
25686 +                       ent.type = DT_UNKNOWN;
25687 +
25688 +               /* unnecessary to support mmap_sem since this is a dir */
25689 +               err = -EFAULT;
25690 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25691 +                       goto out;
25692 +               if (copy_to_user(arg->ent.e->name, name, nlen))
25693 +                       goto out;
25694 +               /* the terminating NULL */
25695 +               if (__put_user(0, arg->ent.e->name + nlen))
25696 +                       goto out;
25697 +               err = 0;
25698 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25699 +               arg->ent.ul += len;
25700 +               rdu->rent++;
25701 +       } else {
25702 +               err = -EFAULT;
25703 +               au_fset_rdu(rdu->cookie.flags, FULL);
25704 +               rdu->full = 1;
25705 +               rdu->tail = arg->ent;
25706 +       }
25707 +
25708 +out:
25709 +       /* AuTraceErr(err); */
25710 +       return err;
25711 +}
25712 +
25713 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25714 +{
25715 +       int err;
25716 +       loff_t offset;
25717 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25718 +
25719 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
25720 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25721 +       err = offset;
25722 +       if (unlikely(offset != cookie->h_pos))
25723 +               goto out;
25724 +
25725 +       err = 0;
25726 +       do {
25727 +               arg->err = 0;
25728 +               au_fclr_rdu(cookie->flags, CALLED);
25729 +               /* smp_mb(); */
25730 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
25731 +               if (err >= 0)
25732 +                       err = arg->err;
25733 +       } while (!err
25734 +                && au_ftest_rdu(cookie->flags, CALLED)
25735 +                && !au_ftest_rdu(cookie->flags, FULL));
25736 +       cookie->h_pos = h_file->f_pos;
25737 +
25738 +out:
25739 +       AuTraceErr(err);
25740 +       return err;
25741 +}
25742 +
25743 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25744 +{
25745 +       int err;
25746 +       aufs_bindex_t bend;
25747 +       struct au_rdu_arg arg = {
25748 +               .ctx = {
25749 +                       .actor = au_rdu_fill
25750 +               }
25751 +       };
25752 +       struct dentry *dentry;
25753 +       struct inode *inode;
25754 +       struct file *h_file;
25755 +       struct au_rdu_cookie *cookie = &rdu->cookie;
25756 +
25757 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25758 +       if (unlikely(err)) {
25759 +               err = -EFAULT;
25760 +               AuTraceErr(err);
25761 +               goto out;
25762 +       }
25763 +       rdu->rent = 0;
25764 +       rdu->tail = rdu->ent;
25765 +       rdu->full = 0;
25766 +       arg.rdu = rdu;
25767 +       arg.ent = rdu->ent;
25768 +       arg.end = arg.ent.ul;
25769 +       arg.end += rdu->sz;
25770 +
25771 +       err = -ENOTDIR;
25772 +       if (unlikely(!file->f_op->iterate))
25773 +               goto out;
25774 +
25775 +       err = security_file_permission(file, MAY_READ);
25776 +       AuTraceErr(err);
25777 +       if (unlikely(err))
25778 +               goto out;
25779 +
25780 +       dentry = file->f_path.dentry;
25781 +       inode = d_inode(dentry);
25782 +#if 1
25783 +       mutex_lock(&inode->i_mutex);
25784 +#else
25785 +       err = mutex_lock_killable(&inode->i_mutex);
25786 +       AuTraceErr(err);
25787 +       if (unlikely(err))
25788 +               goto out;
25789 +#endif
25790 +
25791 +       arg.sb = inode->i_sb;
25792 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25793 +       if (unlikely(err))
25794 +               goto out_mtx;
25795 +       err = au_alive_dir(dentry);
25796 +       if (unlikely(err))
25797 +               goto out_si;
25798 +       /* todo: reval? */
25799 +       fi_read_lock(file);
25800 +
25801 +       err = -EAGAIN;
25802 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25803 +                    && cookie->generation != au_figen(file)))
25804 +               goto out_unlock;
25805 +
25806 +       err = 0;
25807 +       if (!rdu->blk) {
25808 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
25809 +               if (!rdu->blk)
25810 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
25811 +       }
25812 +       bend = au_fbstart(file);
25813 +       if (cookie->bindex < bend)
25814 +               cookie->bindex = bend;
25815 +       bend = au_fbend_dir(file);
25816 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
25817 +       for (; !err && cookie->bindex <= bend;
25818 +            cookie->bindex++, cookie->h_pos = 0) {
25819 +               h_file = au_hf_dir(file, cookie->bindex);
25820 +               if (!h_file)
25821 +                       continue;
25822 +
25823 +               au_fclr_rdu(cookie->flags, FULL);
25824 +               err = au_rdu_do(h_file, &arg);
25825 +               AuTraceErr(err);
25826 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25827 +                       break;
25828 +       }
25829 +       AuDbg("rent %llu\n", rdu->rent);
25830 +
25831 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25832 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25833 +               au_fset_rdu(cookie->flags, CONT);
25834 +               cookie->generation = au_figen(file);
25835 +       }
25836 +
25837 +       ii_read_lock_child(inode);
25838 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
25839 +       ii_read_unlock(inode);
25840 +
25841 +out_unlock:
25842 +       fi_read_unlock(file);
25843 +out_si:
25844 +       si_read_unlock(arg.sb);
25845 +out_mtx:
25846 +       mutex_unlock(&inode->i_mutex);
25847 +out:
25848 +       AuTraceErr(err);
25849 +       return err;
25850 +}
25851 +
25852 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
25853 +{
25854 +       int err;
25855 +       ino_t ino;
25856 +       unsigned long long nent;
25857 +       union au_rdu_ent_ul *u;
25858 +       struct au_rdu_ent ent;
25859 +       struct super_block *sb;
25860 +
25861 +       err = 0;
25862 +       nent = rdu->nent;
25863 +       u = &rdu->ent;
25864 +       sb = file->f_path.dentry->d_sb;
25865 +       si_read_lock(sb, AuLock_FLUSH);
25866 +       while (nent-- > 0) {
25867 +               /* unnecessary to support mmap_sem since this is a dir */
25868 +               err = copy_from_user(&ent, u->e, sizeof(ent));
25869 +               if (!err)
25870 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
25871 +               if (unlikely(err)) {
25872 +                       err = -EFAULT;
25873 +                       AuTraceErr(err);
25874 +                       break;
25875 +               }
25876 +
25877 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
25878 +               if (!ent.wh)
25879 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
25880 +               else
25881 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
25882 +                                       &ino);
25883 +               if (unlikely(err)) {
25884 +                       AuTraceErr(err);
25885 +                       break;
25886 +               }
25887 +
25888 +               err = __put_user(ino, &u->e->ino);
25889 +               if (unlikely(err)) {
25890 +                       err = -EFAULT;
25891 +                       AuTraceErr(err);
25892 +                       break;
25893 +               }
25894 +               u->ul += au_rdu_len(ent.nlen);
25895 +       }
25896 +       si_read_unlock(sb);
25897 +
25898 +       return err;
25899 +}
25900 +
25901 +/* ---------------------------------------------------------------------- */
25902 +
25903 +static int au_rdu_verify(struct aufs_rdu *rdu)
25904 +{
25905 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
25906 +             "%llu, b%d, 0x%x, g%u}\n",
25907 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
25908 +             rdu->blk,
25909 +             rdu->rent, rdu->shwh, rdu->full,
25910 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
25911 +             rdu->cookie.generation);
25912 +
25913 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
25914 +               return 0;
25915 +
25916 +       AuDbg("%u:%u\n",
25917 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
25918 +       return -EINVAL;
25919 +}
25920 +
25921 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25922 +{
25923 +       long err, e;
25924 +       struct aufs_rdu rdu;
25925 +       void __user *p = (void __user *)arg;
25926 +
25927 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25928 +       if (unlikely(err)) {
25929 +               err = -EFAULT;
25930 +               AuTraceErr(err);
25931 +               goto out;
25932 +       }
25933 +       err = au_rdu_verify(&rdu);
25934 +       if (unlikely(err))
25935 +               goto out;
25936 +
25937 +       switch (cmd) {
25938 +       case AUFS_CTL_RDU:
25939 +               err = au_rdu(file, &rdu);
25940 +               if (unlikely(err))
25941 +                       break;
25942 +
25943 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25944 +               if (unlikely(e)) {
25945 +                       err = -EFAULT;
25946 +                       AuTraceErr(err);
25947 +               }
25948 +               break;
25949 +       case AUFS_CTL_RDU_INO:
25950 +               err = au_rdu_ino(file, &rdu);
25951 +               break;
25952 +
25953 +       default:
25954 +               /* err = -ENOTTY; */
25955 +               err = -EINVAL;
25956 +       }
25957 +
25958 +out:
25959 +       AuTraceErr(err);
25960 +       return err;
25961 +}
25962 +
25963 +#ifdef CONFIG_COMPAT
25964 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25965 +{
25966 +       long err, e;
25967 +       struct aufs_rdu rdu;
25968 +       void __user *p = compat_ptr(arg);
25969 +
25970 +       /* todo: get_user()? */
25971 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25972 +       if (unlikely(err)) {
25973 +               err = -EFAULT;
25974 +               AuTraceErr(err);
25975 +               goto out;
25976 +       }
25977 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
25978 +       err = au_rdu_verify(&rdu);
25979 +       if (unlikely(err))
25980 +               goto out;
25981 +
25982 +       switch (cmd) {
25983 +       case AUFS_CTL_RDU:
25984 +               err = au_rdu(file, &rdu);
25985 +               if (unlikely(err))
25986 +                       break;
25987 +
25988 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
25989 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
25990 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25991 +               if (unlikely(e)) {
25992 +                       err = -EFAULT;
25993 +                       AuTraceErr(err);
25994 +               }
25995 +               break;
25996 +       case AUFS_CTL_RDU_INO:
25997 +               err = au_rdu_ino(file, &rdu);
25998 +               break;
25999 +
26000 +       default:
26001 +               /* err = -ENOTTY; */
26002 +               err = -EINVAL;
26003 +       }
26004 +
26005 +out:
26006 +       AuTraceErr(err);
26007 +       return err;
26008 +}
26009 +#endif
26010 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26011 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
26012 +++ linux/fs/aufs/rwsem.h       2016-01-13 20:11:11.673093671 +0100
26013 @@ -0,0 +1,191 @@
26014 +/*
26015 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26016 + *
26017 + * This program, aufs is free software; you can redistribute it and/or modify
26018 + * it under the terms of the GNU General Public License as published by
26019 + * the Free Software Foundation; either version 2 of the License, or
26020 + * (at your option) any later version.
26021 + *
26022 + * This program is distributed in the hope that it will be useful,
26023 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26024 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26025 + * GNU General Public License for more details.
26026 + *
26027 + * You should have received a copy of the GNU General Public License
26028 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26029 + */
26030 +
26031 +/*
26032 + * simple read-write semaphore wrappers
26033 + */
26034 +
26035 +#ifndef __AUFS_RWSEM_H__
26036 +#define __AUFS_RWSEM_H__
26037 +
26038 +#ifdef __KERNEL__
26039 +
26040 +#include "debug.h"
26041 +
26042 +struct au_rwsem {
26043 +       struct rw_semaphore     rwsem;
26044 +#ifdef CONFIG_AUFS_DEBUG
26045 +       /* just for debugging, not almighty counter */
26046 +       atomic_t                rcnt, wcnt;
26047 +#endif
26048 +};
26049 +
26050 +#ifdef CONFIG_AUFS_DEBUG
26051 +#define AuDbgCntInit(rw) do { \
26052 +       atomic_set(&(rw)->rcnt, 0); \
26053 +       atomic_set(&(rw)->wcnt, 0); \
26054 +       smp_mb(); /* atomic set */ \
26055 +} while (0)
26056 +
26057 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
26058 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
26059 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
26060 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
26061 +#else
26062 +#define AuDbgCntInit(rw)       do {} while (0)
26063 +#define AuDbgRcntInc(rw)       do {} while (0)
26064 +#define AuDbgRcntDec(rw)       do {} while (0)
26065 +#define AuDbgWcntInc(rw)       do {} while (0)
26066 +#define AuDbgWcntDec(rw)       do {} while (0)
26067 +#endif /* CONFIG_AUFS_DEBUG */
26068 +
26069 +/* to debug easier, do not make them inlined functions */
26070 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
26071 +/* rwsem_is_locked() is unusable */
26072 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
26073 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
26074 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
26075 +                                       && atomic_read(&(rw)->wcnt) <= 0)
26076 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
26077 +                                       || atomic_read(&(rw)->wcnt))
26078 +
26079 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
26080 +
26081 +static inline void au_rw_init(struct au_rwsem *rw)
26082 +{
26083 +       AuDbgCntInit(rw);
26084 +       init_rwsem(&rw->rwsem);
26085 +}
26086 +
26087 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
26088 +{
26089 +       au_rw_init(rw);
26090 +       down_write(&rw->rwsem);
26091 +       AuDbgWcntInc(rw);
26092 +}
26093 +
26094 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
26095 +                                          unsigned int lsc)
26096 +{
26097 +       au_rw_init(rw);
26098 +       down_write_nested(&rw->rwsem, lsc);
26099 +       AuDbgWcntInc(rw);
26100 +}
26101 +
26102 +static inline void au_rw_read_lock(struct au_rwsem *rw)
26103 +{
26104 +       down_read(&rw->rwsem);
26105 +       AuDbgRcntInc(rw);
26106 +}
26107 +
26108 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26109 +{
26110 +       down_read_nested(&rw->rwsem, lsc);
26111 +       AuDbgRcntInc(rw);
26112 +}
26113 +
26114 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
26115 +{
26116 +       AuRwMustReadLock(rw);
26117 +       AuDbgRcntDec(rw);
26118 +       up_read(&rw->rwsem);
26119 +}
26120 +
26121 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26122 +{
26123 +       AuRwMustWriteLock(rw);
26124 +       AuDbgRcntInc(rw);
26125 +       AuDbgWcntDec(rw);
26126 +       downgrade_write(&rw->rwsem);
26127 +}
26128 +
26129 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26130 +{
26131 +       down_write(&rw->rwsem);
26132 +       AuDbgWcntInc(rw);
26133 +}
26134 +
26135 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26136 +                                          unsigned int lsc)
26137 +{
26138 +       down_write_nested(&rw->rwsem, lsc);
26139 +       AuDbgWcntInc(rw);
26140 +}
26141 +
26142 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26143 +{
26144 +       AuRwMustWriteLock(rw);
26145 +       AuDbgWcntDec(rw);
26146 +       up_write(&rw->rwsem);
26147 +}
26148 +
26149 +/* why is not _nested version defined */
26150 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26151 +{
26152 +       int ret;
26153 +
26154 +       ret = down_read_trylock(&rw->rwsem);
26155 +       if (ret)
26156 +               AuDbgRcntInc(rw);
26157 +       return ret;
26158 +}
26159 +
26160 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26161 +{
26162 +       int ret;
26163 +
26164 +       ret = down_write_trylock(&rw->rwsem);
26165 +       if (ret)
26166 +               AuDbgWcntInc(rw);
26167 +       return ret;
26168 +}
26169 +
26170 +#undef AuDbgCntInit
26171 +#undef AuDbgRcntInc
26172 +#undef AuDbgRcntDec
26173 +#undef AuDbgWcntInc
26174 +#undef AuDbgWcntDec
26175 +
26176 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26177 +static inline void prefix##_read_lock(param) \
26178 +{ au_rw_read_lock(rwsem); } \
26179 +static inline void prefix##_write_lock(param) \
26180 +{ au_rw_write_lock(rwsem); } \
26181 +static inline int prefix##_read_trylock(param) \
26182 +{ return au_rw_read_trylock(rwsem); } \
26183 +static inline int prefix##_write_trylock(param) \
26184 +{ return au_rw_write_trylock(rwsem); }
26185 +/* why is not _nested version defined */
26186 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26187 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26188 +static inline void prefix##_write_trylock_nestd(param, lsc)
26189 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26190 +
26191 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26192 +static inline void prefix##_read_unlock(param) \
26193 +{ au_rw_read_unlock(rwsem); } \
26194 +static inline void prefix##_write_unlock(param) \
26195 +{ au_rw_write_unlock(rwsem); } \
26196 +static inline void prefix##_downgrade_lock(param) \
26197 +{ au_rw_dgrade_lock(rwsem); }
26198 +
26199 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26200 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26201 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26202 +
26203 +#endif /* __KERNEL__ */
26204 +#endif /* __AUFS_RWSEM_H__ */
26205 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26206 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26207 +++ linux/fs/aufs/sbinfo.c      2016-01-13 20:11:11.673093671 +0100
26208 @@ -0,0 +1,366 @@
26209 +/*
26210 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26211 + *
26212 + * This program, aufs is free software; you can redistribute it and/or modify
26213 + * it under the terms of the GNU General Public License as published by
26214 + * the Free Software Foundation; either version 2 of the License, or
26215 + * (at your option) any later version.
26216 + *
26217 + * This program is distributed in the hope that it will be useful,
26218 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26219 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26220 + * GNU General Public License for more details.
26221 + *
26222 + * You should have received a copy of the GNU General Public License
26223 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26224 + */
26225 +
26226 +/*
26227 + * superblock private data
26228 + */
26229 +
26230 +#include "aufs.h"
26231 +
26232 +/*
26233 + * they are necessary regardless sysfs is disabled.
26234 + */
26235 +void au_si_free(struct kobject *kobj)
26236 +{
26237 +       int i;
26238 +       struct au_sbinfo *sbinfo;
26239 +       char *locked __maybe_unused; /* debug only */
26240 +
26241 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26242 +       for (i = 0; i < AuPlink_NHASH; i++)
26243 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26244 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26245 +
26246 +       AuDebugOn(!hlist_empty(&sbinfo->si_symlink.head));
26247 +
26248 +       au_rw_write_lock(&sbinfo->si_rwsem);
26249 +       au_br_free(sbinfo);
26250 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26251 +
26252 +       AuDebugOn(radix_tree_gang_lookup
26253 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
26254 +                  /*first_index*/PID_MAX_DEFAULT - 1,
26255 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
26256 +
26257 +       kfree(sbinfo->si_branch);
26258 +       kfree(sbinfo->au_si_pid.bitmap);
26259 +       mutex_destroy(&sbinfo->si_xib_mtx);
26260 +       AuRwDestroy(&sbinfo->si_rwsem);
26261 +
26262 +       kfree(sbinfo);
26263 +}
26264 +
26265 +int au_si_alloc(struct super_block *sb)
26266 +{
26267 +       int err, i;
26268 +       struct au_sbinfo *sbinfo;
26269 +       static struct lock_class_key aufs_si;
26270 +
26271 +       err = -ENOMEM;
26272 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26273 +       if (unlikely(!sbinfo))
26274 +               goto out;
26275 +
26276 +       BUILD_BUG_ON(sizeof(unsigned long) !=
26277 +                    sizeof(*sbinfo->au_si_pid.bitmap));
26278 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
26279 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
26280 +                                       GFP_NOFS);
26281 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
26282 +               goto out_sbinfo;
26283 +
26284 +       /* will be reallocated separately */
26285 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26286 +       if (unlikely(!sbinfo->si_branch))
26287 +               goto out_pidmap;
26288 +
26289 +       err = sysaufs_si_init(sbinfo);
26290 +       if (unlikely(err))
26291 +               goto out_br;
26292 +
26293 +       au_nwt_init(&sbinfo->si_nowait);
26294 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26295 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
26296 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
26297 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
26298 +
26299 +       atomic_long_set(&sbinfo->si_ninodes, 0);
26300 +       atomic_long_set(&sbinfo->si_nfiles, 0);
26301 +
26302 +       sbinfo->si_bend = -1;
26303 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26304 +
26305 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26306 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26307 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26308 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26309 +
26310 +       au_fhsm_init(sbinfo);
26311 +
26312 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26313 +
26314 +       au_sphl_init(&sbinfo->si_symlink);
26315 +
26316 +       sbinfo->si_xino_jiffy = jiffies;
26317 +       sbinfo->si_xino_expire
26318 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26319 +       mutex_init(&sbinfo->si_xib_mtx);
26320 +       sbinfo->si_xino_brid = -1;
26321 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26322 +
26323 +       au_sphl_init(&sbinfo->si_aopen);
26324 +
26325 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26326 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26327 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26328 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26329 +
26330 +       for (i = 0; i < AuPlink_NHASH; i++)
26331 +               au_sphl_init(sbinfo->si_plink + i);
26332 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26333 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26334 +
26335 +       au_sphl_init(&sbinfo->si_files);
26336 +
26337 +       /* with getattr by default */
26338 +       sbinfo->si_iop_array = aufs_iop;
26339 +
26340 +       /* leave other members for sysaufs and si_mnt. */
26341 +       sbinfo->si_sb = sb;
26342 +       sb->s_fs_info = sbinfo;
26343 +       si_pid_set(sb);
26344 +       return 0; /* success */
26345 +
26346 +out_br:
26347 +       kfree(sbinfo->si_branch);
26348 +out_pidmap:
26349 +       kfree(sbinfo->au_si_pid.bitmap);
26350 +out_sbinfo:
26351 +       kfree(sbinfo);
26352 +out:
26353 +       return err;
26354 +}
26355 +
26356 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26357 +{
26358 +       int err, sz;
26359 +       struct au_branch **brp;
26360 +
26361 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26362 +
26363 +       err = -ENOMEM;
26364 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
26365 +       if (unlikely(!sz))
26366 +               sz = sizeof(*brp);
26367 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26368 +       if (brp) {
26369 +               sbinfo->si_branch = brp;
26370 +               err = 0;
26371 +       }
26372 +
26373 +       return err;
26374 +}
26375 +
26376 +/* ---------------------------------------------------------------------- */
26377 +
26378 +unsigned int au_sigen_inc(struct super_block *sb)
26379 +{
26380 +       unsigned int gen;
26381 +       struct inode *inode;
26382 +
26383 +       SiMustWriteLock(sb);
26384 +
26385 +       gen = ++au_sbi(sb)->si_generation;
26386 +       au_update_digen(sb->s_root);
26387 +       inode = d_inode(sb->s_root);
26388 +       au_update_iigen(inode, /*half*/0);
26389 +       inode->i_version++;
26390 +       return gen;
26391 +}
26392 +
26393 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26394 +{
26395 +       aufs_bindex_t br_id;
26396 +       int i;
26397 +       struct au_sbinfo *sbinfo;
26398 +
26399 +       SiMustWriteLock(sb);
26400 +
26401 +       sbinfo = au_sbi(sb);
26402 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26403 +               br_id = ++sbinfo->si_last_br_id;
26404 +               AuDebugOn(br_id < 0);
26405 +               if (br_id && au_br_index(sb, br_id) < 0)
26406 +                       return br_id;
26407 +       }
26408 +
26409 +       return -1;
26410 +}
26411 +
26412 +/* ---------------------------------------------------------------------- */
26413 +
26414 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26415 +int si_read_lock(struct super_block *sb, int flags)
26416 +{
26417 +       int err;
26418 +
26419 +       err = 0;
26420 +       if (au_ftest_lock(flags, FLUSH))
26421 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26422 +
26423 +       si_noflush_read_lock(sb);
26424 +       err = au_plink_maint(sb, flags);
26425 +       if (unlikely(err))
26426 +               si_read_unlock(sb);
26427 +
26428 +       return err;
26429 +}
26430 +
26431 +int si_write_lock(struct super_block *sb, int flags)
26432 +{
26433 +       int err;
26434 +
26435 +       if (au_ftest_lock(flags, FLUSH))
26436 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26437 +
26438 +       si_noflush_write_lock(sb);
26439 +       err = au_plink_maint(sb, flags);
26440 +       if (unlikely(err))
26441 +               si_write_unlock(sb);
26442 +
26443 +       return err;
26444 +}
26445 +
26446 +/* dentry and super_block lock. call at entry point */
26447 +int aufs_read_lock(struct dentry *dentry, int flags)
26448 +{
26449 +       int err;
26450 +       struct super_block *sb;
26451 +
26452 +       sb = dentry->d_sb;
26453 +       err = si_read_lock(sb, flags);
26454 +       if (unlikely(err))
26455 +               goto out;
26456 +
26457 +       if (au_ftest_lock(flags, DW))
26458 +               di_write_lock_child(dentry);
26459 +       else
26460 +               di_read_lock_child(dentry, flags);
26461 +
26462 +       if (au_ftest_lock(flags, GEN)) {
26463 +               err = au_digen_test(dentry, au_sigen(sb));
26464 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
26465 +                       AuDebugOn(!err && au_dbrange_test(dentry));
26466 +               else if (!err)
26467 +                       err = au_dbrange_test(dentry);
26468 +               if (unlikely(err))
26469 +                       aufs_read_unlock(dentry, flags);
26470 +       }
26471 +
26472 +out:
26473 +       return err;
26474 +}
26475 +
26476 +void aufs_read_unlock(struct dentry *dentry, int flags)
26477 +{
26478 +       if (au_ftest_lock(flags, DW))
26479 +               di_write_unlock(dentry);
26480 +       else
26481 +               di_read_unlock(dentry, flags);
26482 +       si_read_unlock(dentry->d_sb);
26483 +}
26484 +
26485 +void aufs_write_lock(struct dentry *dentry)
26486 +{
26487 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
26488 +       di_write_lock_child(dentry);
26489 +}
26490 +
26491 +void aufs_write_unlock(struct dentry *dentry)
26492 +{
26493 +       di_write_unlock(dentry);
26494 +       si_write_unlock(dentry->d_sb);
26495 +}
26496 +
26497 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
26498 +{
26499 +       int err;
26500 +       unsigned int sigen;
26501 +       struct super_block *sb;
26502 +
26503 +       sb = d1->d_sb;
26504 +       err = si_read_lock(sb, flags);
26505 +       if (unlikely(err))
26506 +               goto out;
26507 +
26508 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
26509 +
26510 +       if (au_ftest_lock(flags, GEN)) {
26511 +               sigen = au_sigen(sb);
26512 +               err = au_digen_test(d1, sigen);
26513 +               AuDebugOn(!err && au_dbrange_test(d1));
26514 +               if (!err) {
26515 +                       err = au_digen_test(d2, sigen);
26516 +                       AuDebugOn(!err && au_dbrange_test(d2));
26517 +               }
26518 +               if (unlikely(err))
26519 +                       aufs_read_and_write_unlock2(d1, d2);
26520 +       }
26521 +
26522 +out:
26523 +       return err;
26524 +}
26525 +
26526 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26527 +{
26528 +       di_write_unlock2(d1, d2);
26529 +       si_read_unlock(d1->d_sb);
26530 +}
26531 +
26532 +/* ---------------------------------------------------------------------- */
26533 +
26534 +int si_pid_test_slow(struct super_block *sb)
26535 +{
26536 +       void *p;
26537 +
26538 +       rcu_read_lock();
26539 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
26540 +       rcu_read_unlock();
26541 +
26542 +       return (long)!!p;
26543 +}
26544 +
26545 +void si_pid_set_slow(struct super_block *sb)
26546 +{
26547 +       int err;
26548 +       struct au_sbinfo *sbinfo;
26549 +
26550 +       AuDebugOn(si_pid_test_slow(sb));
26551 +
26552 +       sbinfo = au_sbi(sb);
26553 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
26554 +       AuDebugOn(err);
26555 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26556 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
26557 +                               /*any valid ptr*/sb);
26558 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26559 +       AuDebugOn(err);
26560 +       radix_tree_preload_end();
26561 +}
26562 +
26563 +void si_pid_clr_slow(struct super_block *sb)
26564 +{
26565 +       void *p;
26566 +       struct au_sbinfo *sbinfo;
26567 +
26568 +       AuDebugOn(!si_pid_test_slow(sb));
26569 +
26570 +       sbinfo = au_sbi(sb);
26571 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26572 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
26573 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26574 +}
26575 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26576 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
26577 +++ linux/fs/aufs/spl.h 2016-01-13 20:11:11.673093671 +0100
26578 @@ -0,0 +1,111 @@
26579 +/*
26580 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26581 + *
26582 + * This program, aufs is free software; you can redistribute it and/or modify
26583 + * it under the terms of the GNU General Public License as published by
26584 + * the Free Software Foundation; either version 2 of the License, or
26585 + * (at your option) any later version.
26586 + *
26587 + * This program is distributed in the hope that it will be useful,
26588 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26589 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26590 + * GNU General Public License for more details.
26591 + *
26592 + * You should have received a copy of the GNU General Public License
26593 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26594 + */
26595 +
26596 +/*
26597 + * simple list protected by a spinlock
26598 + */
26599 +
26600 +#ifndef __AUFS_SPL_H__
26601 +#define __AUFS_SPL_H__
26602 +
26603 +#ifdef __KERNEL__
26604 +
26605 +struct au_splhead {
26606 +       spinlock_t              spin;
26607 +       struct list_head        head;
26608 +};
26609 +
26610 +static inline void au_spl_init(struct au_splhead *spl)
26611 +{
26612 +       spin_lock_init(&spl->spin);
26613 +       INIT_LIST_HEAD(&spl->head);
26614 +}
26615 +
26616 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26617 +{
26618 +       spin_lock(&spl->spin);
26619 +       list_add(list, &spl->head);
26620 +       spin_unlock(&spl->spin);
26621 +}
26622 +
26623 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26624 +{
26625 +       spin_lock(&spl->spin);
26626 +       list_del(list);
26627 +       spin_unlock(&spl->spin);
26628 +}
26629 +
26630 +static inline void au_spl_del_rcu(struct list_head *list,
26631 +                                 struct au_splhead *spl)
26632 +{
26633 +       spin_lock(&spl->spin);
26634 +       list_del_rcu(list);
26635 +       spin_unlock(&spl->spin);
26636 +}
26637 +
26638 +/* ---------------------------------------------------------------------- */
26639 +
26640 +struct au_sphlhead {
26641 +       spinlock_t              spin;
26642 +       struct hlist_head       head;
26643 +};
26644 +
26645 +static inline void au_sphl_init(struct au_sphlhead *sphl)
26646 +{
26647 +       spin_lock_init(&sphl->spin);
26648 +       INIT_HLIST_HEAD(&sphl->head);
26649 +}
26650 +
26651 +static inline void au_sphl_add(struct hlist_node *hlist,
26652 +                              struct au_sphlhead *sphl)
26653 +{
26654 +       spin_lock(&sphl->spin);
26655 +       hlist_add_head(hlist, &sphl->head);
26656 +       spin_unlock(&sphl->spin);
26657 +}
26658 +
26659 +static inline void au_sphl_del(struct hlist_node *hlist,
26660 +                              struct au_sphlhead *sphl)
26661 +{
26662 +       spin_lock(&sphl->spin);
26663 +       hlist_del(hlist);
26664 +       spin_unlock(&sphl->spin);
26665 +}
26666 +
26667 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26668 +                                  struct au_sphlhead *sphl)
26669 +{
26670 +       spin_lock(&sphl->spin);
26671 +       hlist_del_rcu(hlist);
26672 +       spin_unlock(&sphl->spin);
26673 +}
26674 +
26675 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26676 +{
26677 +       unsigned long cnt;
26678 +       struct hlist_node *pos;
26679 +
26680 +       cnt = 0;
26681 +       spin_lock(&sphl->spin);
26682 +       hlist_for_each(pos, &sphl->head)
26683 +               cnt++;
26684 +       spin_unlock(&sphl->spin);
26685 +       return cnt;
26686 +}
26687 +
26688 +#endif /* __KERNEL__ */
26689 +#endif /* __AUFS_SPL_H__ */
26690 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26691 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
26692 +++ linux/fs/aufs/super.c       2016-01-13 20:11:11.673093671 +0100
26693 @@ -0,0 +1,1039 @@
26694 +/*
26695 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26696 + *
26697 + * This program, aufs is free software; you can redistribute it and/or modify
26698 + * it under the terms of the GNU General Public License as published by
26699 + * the Free Software Foundation; either version 2 of the License, or
26700 + * (at your option) any later version.
26701 + *
26702 + * This program is distributed in the hope that it will be useful,
26703 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26704 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26705 + * GNU General Public License for more details.
26706 + *
26707 + * You should have received a copy of the GNU General Public License
26708 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26709 + */
26710 +
26711 +/*
26712 + * mount and super_block operations
26713 + */
26714 +
26715 +#include <linux/mm.h>
26716 +#include <linux/seq_file.h>
26717 +#include <linux/statfs.h>
26718 +#include <linux/vmalloc.h>
26719 +#include "aufs.h"
26720 +
26721 +/*
26722 + * super_operations
26723 + */
26724 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26725 +{
26726 +       struct au_icntnr *c;
26727 +
26728 +       c = au_cache_alloc_icntnr();
26729 +       if (c) {
26730 +               au_icntnr_init(c);
26731 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
26732 +               c->iinfo.ii_hinode = NULL;
26733 +               return &c->vfs_inode;
26734 +       }
26735 +       return NULL;
26736 +}
26737 +
26738 +static void aufs_destroy_inode_cb(struct rcu_head *head)
26739 +{
26740 +       struct inode *inode = container_of(head, struct inode, i_rcu);
26741 +
26742 +       INIT_HLIST_HEAD(&inode->i_dentry);
26743 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26744 +}
26745 +
26746 +static void aufs_destroy_inode(struct inode *inode)
26747 +{
26748 +       au_iinfo_fin(inode);
26749 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
26750 +}
26751 +
26752 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26753 +{
26754 +       struct inode *inode;
26755 +       int err;
26756 +
26757 +       inode = iget_locked(sb, ino);
26758 +       if (unlikely(!inode)) {
26759 +               inode = ERR_PTR(-ENOMEM);
26760 +               goto out;
26761 +       }
26762 +       if (!(inode->i_state & I_NEW))
26763 +               goto out;
26764 +
26765 +       err = au_xigen_new(inode);
26766 +       if (!err)
26767 +               err = au_iinfo_init(inode);
26768 +       if (!err)
26769 +               inode->i_version++;
26770 +       else {
26771 +               iget_failed(inode);
26772 +               inode = ERR_PTR(err);
26773 +       }
26774 +
26775 +out:
26776 +       /* never return NULL */
26777 +       AuDebugOn(!inode);
26778 +       AuTraceErrPtr(inode);
26779 +       return inode;
26780 +}
26781 +
26782 +/* lock free root dinfo */
26783 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26784 +{
26785 +       int err;
26786 +       aufs_bindex_t bindex, bend;
26787 +       struct path path;
26788 +       struct au_hdentry *hdp;
26789 +       struct au_branch *br;
26790 +       au_br_perm_str_t perm;
26791 +
26792 +       err = 0;
26793 +       bend = au_sbend(sb);
26794 +       hdp = au_di(sb->s_root)->di_hdentry;
26795 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
26796 +               br = au_sbr(sb, bindex);
26797 +               path.mnt = au_br_mnt(br);
26798 +               path.dentry = hdp[bindex].hd_dentry;
26799 +               err = au_seq_path(seq, &path);
26800 +               if (!err) {
26801 +                       au_optstr_br_perm(&perm, br->br_perm);
26802 +                       seq_printf(seq, "=%s", perm.a);
26803 +                       if (bindex != bend)
26804 +                               seq_putc(seq, ':');
26805 +               }
26806 +       }
26807 +       if (unlikely(err || seq_has_overflowed(seq)))
26808 +               err = -E2BIG;
26809 +
26810 +       return err;
26811 +}
26812 +
26813 +static void au_show_wbr_create(struct seq_file *m, int v,
26814 +                              struct au_sbinfo *sbinfo)
26815 +{
26816 +       const char *pat;
26817 +
26818 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26819 +
26820 +       seq_puts(m, ",create=");
26821 +       pat = au_optstr_wbr_create(v);
26822 +       switch (v) {
26823 +       case AuWbrCreate_TDP:
26824 +       case AuWbrCreate_RR:
26825 +       case AuWbrCreate_MFS:
26826 +       case AuWbrCreate_PMFS:
26827 +               seq_puts(m, pat);
26828 +               break;
26829 +       case AuWbrCreate_MFSV:
26830 +               seq_printf(m, /*pat*/"mfs:%lu",
26831 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26832 +                          / MSEC_PER_SEC);
26833 +               break;
26834 +       case AuWbrCreate_PMFSV:
26835 +               seq_printf(m, /*pat*/"pmfs:%lu",
26836 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26837 +                          / MSEC_PER_SEC);
26838 +               break;
26839 +       case AuWbrCreate_MFSRR:
26840 +               seq_printf(m, /*pat*/"mfsrr:%llu",
26841 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26842 +               break;
26843 +       case AuWbrCreate_MFSRRV:
26844 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26845 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26846 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26847 +                          / MSEC_PER_SEC);
26848 +               break;
26849 +       case AuWbrCreate_PMFSRR:
26850 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
26851 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26852 +               break;
26853 +       case AuWbrCreate_PMFSRRV:
26854 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
26855 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26856 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26857 +                          / MSEC_PER_SEC);
26858 +               break;
26859 +       }
26860 +}
26861 +
26862 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
26863 +{
26864 +#ifdef CONFIG_SYSFS
26865 +       return 0;
26866 +#else
26867 +       int err;
26868 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
26869 +       aufs_bindex_t bindex, brid;
26870 +       struct qstr *name;
26871 +       struct file *f;
26872 +       struct dentry *d, *h_root;
26873 +       struct au_hdentry *hdp;
26874 +
26875 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26876 +
26877 +       err = 0;
26878 +       f = au_sbi(sb)->si_xib;
26879 +       if (!f)
26880 +               goto out;
26881 +
26882 +       /* stop printing the default xino path on the first writable branch */
26883 +       h_root = NULL;
26884 +       brid = au_xino_brid(sb);
26885 +       if (brid >= 0) {
26886 +               bindex = au_br_index(sb, brid);
26887 +               hdp = au_di(sb->s_root)->di_hdentry;
26888 +               h_root = hdp[0 + bindex].hd_dentry;
26889 +       }
26890 +       d = f->f_path.dentry;
26891 +       name = &d->d_name;
26892 +       /* safe ->d_parent because the file is unlinked */
26893 +       if (d->d_parent == h_root
26894 +           && name->len == len
26895 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
26896 +               goto out;
26897 +
26898 +       seq_puts(seq, ",xino=");
26899 +       err = au_xino_path(seq, f);
26900 +
26901 +out:
26902 +       return err;
26903 +#endif
26904 +}
26905 +
26906 +/* seq_file will re-call me in case of too long string */
26907 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
26908 +{
26909 +       int err;
26910 +       unsigned int mnt_flags, v;
26911 +       struct super_block *sb;
26912 +       struct au_sbinfo *sbinfo;
26913 +
26914 +#define AuBool(name, str) do { \
26915 +       v = au_opt_test(mnt_flags, name); \
26916 +       if (v != au_opt_test(AuOpt_Def, name)) \
26917 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
26918 +} while (0)
26919 +
26920 +#define AuStr(name, str) do { \
26921 +       v = mnt_flags & AuOptMask_##name; \
26922 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
26923 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
26924 +} while (0)
26925 +
26926 +#define AuUInt(name, str, val) do { \
26927 +       if (val != AUFS_##name##_DEF) \
26928 +               seq_printf(m, "," #str "=%u", val); \
26929 +} while (0)
26930 +
26931 +       sb = dentry->d_sb;
26932 +       if (sb->s_flags & MS_POSIXACL)
26933 +               seq_puts(m, ",acl");
26934 +
26935 +       /* lock free root dinfo */
26936 +       si_noflush_read_lock(sb);
26937 +       sbinfo = au_sbi(sb);
26938 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
26939 +
26940 +       mnt_flags = au_mntflags(sb);
26941 +       if (au_opt_test(mnt_flags, XINO)) {
26942 +               err = au_show_xino(m, sb);
26943 +               if (unlikely(err))
26944 +                       goto out;
26945 +       } else
26946 +               seq_puts(m, ",noxino");
26947 +
26948 +       AuBool(TRUNC_XINO, trunc_xino);
26949 +       AuStr(UDBA, udba);
26950 +       AuBool(SHWH, shwh);
26951 +       AuBool(PLINK, plink);
26952 +       AuBool(DIO, dio);
26953 +       AuBool(DIRPERM1, dirperm1);
26954 +
26955 +       v = sbinfo->si_wbr_create;
26956 +       if (v != AuWbrCreate_Def)
26957 +               au_show_wbr_create(m, v, sbinfo);
26958 +
26959 +       v = sbinfo->si_wbr_copyup;
26960 +       if (v != AuWbrCopyup_Def)
26961 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
26962 +
26963 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
26964 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
26965 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
26966 +
26967 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
26968 +
26969 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
26970 +       AuUInt(RDCACHE, rdcache, v);
26971 +
26972 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
26973 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
26974 +
26975 +       au_fhsm_show(m, sbinfo);
26976 +
26977 +       AuBool(SUM, sum);
26978 +       /* AuBool(SUM_W, wsum); */
26979 +       AuBool(WARN_PERM, warn_perm);
26980 +       AuBool(VERBOSE, verbose);
26981 +
26982 +out:
26983 +       /* be sure to print "br:" last */
26984 +       if (!sysaufs_brs) {
26985 +               seq_puts(m, ",br:");
26986 +               au_show_brs(m, sb);
26987 +       }
26988 +       si_read_unlock(sb);
26989 +       return 0;
26990 +
26991 +#undef AuBool
26992 +#undef AuStr
26993 +#undef AuUInt
26994 +}
26995 +
26996 +/* ---------------------------------------------------------------------- */
26997 +
26998 +/* sum mode which returns the summation for statfs(2) */
26999 +
27000 +static u64 au_add_till_max(u64 a, u64 b)
27001 +{
27002 +       u64 old;
27003 +
27004 +       old = a;
27005 +       a += b;
27006 +       if (old <= a)
27007 +               return a;
27008 +       return ULLONG_MAX;
27009 +}
27010 +
27011 +static u64 au_mul_till_max(u64 a, long mul)
27012 +{
27013 +       u64 old;
27014 +
27015 +       old = a;
27016 +       a *= mul;
27017 +       if (old <= a)
27018 +               return a;
27019 +       return ULLONG_MAX;
27020 +}
27021 +
27022 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27023 +{
27024 +       int err;
27025 +       long bsize, factor;
27026 +       u64 blocks, bfree, bavail, files, ffree;
27027 +       aufs_bindex_t bend, bindex, i;
27028 +       unsigned char shared;
27029 +       struct path h_path;
27030 +       struct super_block *h_sb;
27031 +
27032 +       err = 0;
27033 +       bsize = LONG_MAX;
27034 +       files = 0;
27035 +       ffree = 0;
27036 +       blocks = 0;
27037 +       bfree = 0;
27038 +       bavail = 0;
27039 +       bend = au_sbend(sb);
27040 +       for (bindex = 0; bindex <= bend; bindex++) {
27041 +               h_path.mnt = au_sbr_mnt(sb, bindex);
27042 +               h_sb = h_path.mnt->mnt_sb;
27043 +               shared = 0;
27044 +               for (i = 0; !shared && i < bindex; i++)
27045 +                       shared = (au_sbr_sb(sb, i) == h_sb);
27046 +               if (shared)
27047 +                       continue;
27048 +
27049 +               /* sb->s_root for NFS is unreliable */
27050 +               h_path.dentry = h_path.mnt->mnt_root;
27051 +               err = vfs_statfs(&h_path, buf);
27052 +               if (unlikely(err))
27053 +                       goto out;
27054 +
27055 +               if (bsize > buf->f_bsize) {
27056 +                       /*
27057 +                        * we will reduce bsize, so we have to expand blocks
27058 +                        * etc. to match them again
27059 +                        */
27060 +                       factor = (bsize / buf->f_bsize);
27061 +                       blocks = au_mul_till_max(blocks, factor);
27062 +                       bfree = au_mul_till_max(bfree, factor);
27063 +                       bavail = au_mul_till_max(bavail, factor);
27064 +                       bsize = buf->f_bsize;
27065 +               }
27066 +
27067 +               factor = (buf->f_bsize / bsize);
27068 +               blocks = au_add_till_max(blocks,
27069 +                               au_mul_till_max(buf->f_blocks, factor));
27070 +               bfree = au_add_till_max(bfree,
27071 +                               au_mul_till_max(buf->f_bfree, factor));
27072 +               bavail = au_add_till_max(bavail,
27073 +                               au_mul_till_max(buf->f_bavail, factor));
27074 +               files = au_add_till_max(files, buf->f_files);
27075 +               ffree = au_add_till_max(ffree, buf->f_ffree);
27076 +       }
27077 +
27078 +       buf->f_bsize = bsize;
27079 +       buf->f_blocks = blocks;
27080 +       buf->f_bfree = bfree;
27081 +       buf->f_bavail = bavail;
27082 +       buf->f_files = files;
27083 +       buf->f_ffree = ffree;
27084 +       buf->f_frsize = 0;
27085 +
27086 +out:
27087 +       return err;
27088 +}
27089 +
27090 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27091 +{
27092 +       int err;
27093 +       struct path h_path;
27094 +       struct super_block *sb;
27095 +
27096 +       /* lock free root dinfo */
27097 +       sb = dentry->d_sb;
27098 +       si_noflush_read_lock(sb);
27099 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
27100 +               /* sb->s_root for NFS is unreliable */
27101 +               h_path.mnt = au_sbr_mnt(sb, 0);
27102 +               h_path.dentry = h_path.mnt->mnt_root;
27103 +               err = vfs_statfs(&h_path, buf);
27104 +       } else
27105 +               err = au_statfs_sum(sb, buf);
27106 +       si_read_unlock(sb);
27107 +
27108 +       if (!err) {
27109 +               buf->f_type = AUFS_SUPER_MAGIC;
27110 +               buf->f_namelen = AUFS_MAX_NAMELEN;
27111 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27112 +       }
27113 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27114 +
27115 +       return err;
27116 +}
27117 +
27118 +/* ---------------------------------------------------------------------- */
27119 +
27120 +static int aufs_sync_fs(struct super_block *sb, int wait)
27121 +{
27122 +       int err, e;
27123 +       aufs_bindex_t bend, bindex;
27124 +       struct au_branch *br;
27125 +       struct super_block *h_sb;
27126 +
27127 +       err = 0;
27128 +       si_noflush_read_lock(sb);
27129 +       bend = au_sbend(sb);
27130 +       for (bindex = 0; bindex <= bend; bindex++) {
27131 +               br = au_sbr(sb, bindex);
27132 +               if (!au_br_writable(br->br_perm))
27133 +                       continue;
27134 +
27135 +               h_sb = au_sbr_sb(sb, bindex);
27136 +               if (h_sb->s_op->sync_fs) {
27137 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
27138 +                       if (unlikely(e && !err))
27139 +                               err = e;
27140 +                       /* go on even if an error happens */
27141 +               }
27142 +       }
27143 +       si_read_unlock(sb);
27144 +
27145 +       return err;
27146 +}
27147 +
27148 +/* ---------------------------------------------------------------------- */
27149 +
27150 +/* final actions when unmounting a file system */
27151 +static void aufs_put_super(struct super_block *sb)
27152 +{
27153 +       struct au_sbinfo *sbinfo;
27154 +
27155 +       sbinfo = au_sbi(sb);
27156 +       if (!sbinfo)
27157 +               return;
27158 +
27159 +       dbgaufs_si_fin(sbinfo);
27160 +       kobject_put(&sbinfo->si_kobj);
27161 +}
27162 +
27163 +/* ---------------------------------------------------------------------- */
27164 +
27165 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27166 +                    struct super_block *sb, void *arg)
27167 +{
27168 +       void *array;
27169 +       unsigned long long n, sz;
27170 +
27171 +       array = NULL;
27172 +       n = 0;
27173 +       if (!*hint)
27174 +               goto out;
27175 +
27176 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27177 +               array = ERR_PTR(-EMFILE);
27178 +               pr_err("hint %llu\n", *hint);
27179 +               goto out;
27180 +       }
27181 +
27182 +       sz = sizeof(array) * *hint;
27183 +       array = kzalloc(sz, GFP_NOFS);
27184 +       if (unlikely(!array))
27185 +               array = vzalloc(sz);
27186 +       if (unlikely(!array)) {
27187 +               array = ERR_PTR(-ENOMEM);
27188 +               goto out;
27189 +       }
27190 +
27191 +       n = cb(sb, array, *hint, arg);
27192 +       AuDebugOn(n > *hint);
27193 +
27194 +out:
27195 +       *hint = n;
27196 +       return array;
27197 +}
27198 +
27199 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
27200 +                                      unsigned long long max __maybe_unused,
27201 +                                      void *arg)
27202 +{
27203 +       unsigned long long n;
27204 +       struct inode **p, *inode;
27205 +       struct list_head *head;
27206 +
27207 +       n = 0;
27208 +       p = a;
27209 +       head = arg;
27210 +       spin_lock(&sb->s_inode_list_lock);
27211 +       list_for_each_entry(inode, head, i_sb_list) {
27212 +               if (!is_bad_inode(inode)
27213 +                   && au_ii(inode)->ii_bstart >= 0) {
27214 +                       spin_lock(&inode->i_lock);
27215 +                       if (atomic_read(&inode->i_count)) {
27216 +                               au_igrab(inode);
27217 +                               *p++ = inode;
27218 +                               n++;
27219 +                               AuDebugOn(n > max);
27220 +                       }
27221 +                       spin_unlock(&inode->i_lock);
27222 +               }
27223 +       }
27224 +       spin_unlock(&sb->s_inode_list_lock);
27225 +
27226 +       return n;
27227 +}
27228 +
27229 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27230 +{
27231 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
27232 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
27233 +}
27234 +
27235 +void au_iarray_free(struct inode **a, unsigned long long max)
27236 +{
27237 +       unsigned long long ull;
27238 +
27239 +       for (ull = 0; ull < max; ull++)
27240 +               iput(a[ull]);
27241 +       kvfree(a);
27242 +}
27243 +
27244 +/* ---------------------------------------------------------------------- */
27245 +
27246 +/*
27247 + * refresh dentry and inode at remount time.
27248 + */
27249 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27250 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27251 +                     struct dentry *parent)
27252 +{
27253 +       int err;
27254 +
27255 +       di_write_lock_child(dentry);
27256 +       di_read_lock_parent(parent, AuLock_IR);
27257 +       err = au_refresh_dentry(dentry, parent);
27258 +       if (!err && dir_flags)
27259 +               au_hn_reset(d_inode(dentry), dir_flags);
27260 +       di_read_unlock(parent, AuLock_IR);
27261 +       di_write_unlock(dentry);
27262 +
27263 +       return err;
27264 +}
27265 +
27266 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27267 +                          struct au_sbinfo *sbinfo,
27268 +                          const unsigned int dir_flags, unsigned int do_idop)
27269 +{
27270 +       int err;
27271 +       struct dentry *parent;
27272 +
27273 +       err = 0;
27274 +       parent = dget_parent(dentry);
27275 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27276 +               if (d_really_is_positive(dentry)) {
27277 +                       if (!d_is_dir(dentry))
27278 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27279 +                                                parent);
27280 +                       else {
27281 +                               err = au_do_refresh(dentry, dir_flags, parent);
27282 +                               if (unlikely(err))
27283 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27284 +                       }
27285 +               } else
27286 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27287 +               AuDbgDentry(dentry);
27288 +       }
27289 +       dput(parent);
27290 +
27291 +       if (!err) {
27292 +               if (do_idop)
27293 +                       au_refresh_dop(dentry, /*force_reval*/0);
27294 +       } else
27295 +               au_refresh_dop(dentry, /*force_reval*/1);
27296 +
27297 +       AuTraceErr(err);
27298 +       return err;
27299 +}
27300 +
27301 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
27302 +{
27303 +       int err, i, j, ndentry, e;
27304 +       unsigned int sigen;
27305 +       struct au_dcsub_pages dpages;
27306 +       struct au_dpage *dpage;
27307 +       struct dentry **dentries, *d;
27308 +       struct au_sbinfo *sbinfo;
27309 +       struct dentry *root = sb->s_root;
27310 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
27311 +
27312 +       if (do_idop)
27313 +               au_refresh_dop(root, /*force_reval*/0);
27314 +
27315 +       err = au_dpages_init(&dpages, GFP_NOFS);
27316 +       if (unlikely(err))
27317 +               goto out;
27318 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27319 +       if (unlikely(err))
27320 +               goto out_dpages;
27321 +
27322 +       sigen = au_sigen(sb);
27323 +       sbinfo = au_sbi(sb);
27324 +       for (i = 0; i < dpages.ndpage; i++) {
27325 +               dpage = dpages.dpages + i;
27326 +               dentries = dpage->dentries;
27327 +               ndentry = dpage->ndentry;
27328 +               for (j = 0; j < ndentry; j++) {
27329 +                       d = dentries[j];
27330 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
27331 +                                           do_idop);
27332 +                       if (unlikely(e && !err))
27333 +                               err = e;
27334 +                       /* go on even err */
27335 +               }
27336 +       }
27337 +
27338 +out_dpages:
27339 +       au_dpages_free(&dpages);
27340 +out:
27341 +       return err;
27342 +}
27343 +
27344 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
27345 +{
27346 +       int err, e;
27347 +       unsigned int sigen;
27348 +       unsigned long long max, ull;
27349 +       struct inode *inode, **array;
27350 +
27351 +       array = au_iarray_alloc(sb, &max);
27352 +       err = PTR_ERR(array);
27353 +       if (IS_ERR(array))
27354 +               goto out;
27355 +
27356 +       err = 0;
27357 +       sigen = au_sigen(sb);
27358 +       for (ull = 0; ull < max; ull++) {
27359 +               inode = array[ull];
27360 +               if (unlikely(!inode))
27361 +                       break;
27362 +
27363 +               e = 0;
27364 +               ii_write_lock_child(inode);
27365 +               if (au_iigen(inode, NULL) != sigen) {
27366 +                       e = au_refresh_hinode_self(inode);
27367 +                       if (unlikely(e)) {
27368 +                               au_refresh_iop(inode, /*force_getattr*/1);
27369 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27370 +                               if (!err)
27371 +                                       err = e;
27372 +                               /* go on even if err */
27373 +                       }
27374 +               }
27375 +               if (!e && do_idop)
27376 +                       au_refresh_iop(inode, /*force_getattr*/0);
27377 +               ii_write_unlock(inode);
27378 +       }
27379 +
27380 +       au_iarray_free(array, max);
27381 +
27382 +out:
27383 +       return err;
27384 +}
27385 +
27386 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
27387 +{
27388 +       int err, e;
27389 +       unsigned int udba;
27390 +       aufs_bindex_t bindex, bend;
27391 +       struct dentry *root;
27392 +       struct inode *inode;
27393 +       struct au_branch *br;
27394 +       struct au_sbinfo *sbi;
27395 +
27396 +       au_sigen_inc(sb);
27397 +       sbi = au_sbi(sb);
27398 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
27399 +
27400 +       root = sb->s_root;
27401 +       DiMustNoWaiters(root);
27402 +       inode = d_inode(root);
27403 +       IiMustNoWaiters(inode);
27404 +
27405 +       udba = au_opt_udba(sb);
27406 +       bend = au_sbend(sb);
27407 +       for (bindex = 0; bindex <= bend; bindex++) {
27408 +               br = au_sbr(sb, bindex);
27409 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27410 +               if (unlikely(err))
27411 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27412 +                               bindex, err);
27413 +               /* go on even if err */
27414 +       }
27415 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27416 +
27417 +       if (do_idop) {
27418 +               if (au_ftest_si(sbi, NO_DREVAL)) {
27419 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27420 +                       sb->s_d_op = &aufs_dop_noreval;
27421 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27422 +                       sbi->si_iop_array = aufs_iop_nogetattr;
27423 +               } else {
27424 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
27425 +                       sb->s_d_op = &aufs_dop;
27426 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
27427 +                       sbi->si_iop_array = aufs_iop;
27428 +               }
27429 +               pr_info("reset to %pf and %pf\n",
27430 +                       sb->s_d_op, sbi->si_iop_array);
27431 +       }
27432 +
27433 +       di_write_unlock(root);
27434 +       err = au_refresh_d(sb, do_idop);
27435 +       e = au_refresh_i(sb, do_idop);
27436 +       if (unlikely(e && !err))
27437 +               err = e;
27438 +       /* aufs_write_lock() calls ..._child() */
27439 +       di_write_lock_child(root);
27440 +
27441 +       au_cpup_attr_all(inode, /*force*/1);
27442 +
27443 +       if (unlikely(err))
27444 +               AuIOErr("refresh failed, ignored, %d\n", err);
27445 +}
27446 +
27447 +/* stop extra interpretation of errno in mount(8), and strange error messages */
27448 +static int cvt_err(int err)
27449 +{
27450 +       AuTraceErr(err);
27451 +
27452 +       switch (err) {
27453 +       case -ENOENT:
27454 +       case -ENOTDIR:
27455 +       case -EEXIST:
27456 +       case -EIO:
27457 +               err = -EINVAL;
27458 +       }
27459 +       return err;
27460 +}
27461 +
27462 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27463 +{
27464 +       int err, do_dx;
27465 +       unsigned int mntflags;
27466 +       struct au_opts opts = {
27467 +               .opt = NULL
27468 +       };
27469 +       struct dentry *root;
27470 +       struct inode *inode;
27471 +       struct au_sbinfo *sbinfo;
27472 +
27473 +       err = 0;
27474 +       root = sb->s_root;
27475 +       if (!data || !*data) {
27476 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27477 +               if (!err) {
27478 +                       di_write_lock_child(root);
27479 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
27480 +                       aufs_write_unlock(root);
27481 +               }
27482 +               goto out;
27483 +       }
27484 +
27485 +       err = -ENOMEM;
27486 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27487 +       if (unlikely(!opts.opt))
27488 +               goto out;
27489 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27490 +       opts.flags = AuOpts_REMOUNT;
27491 +       opts.sb_flags = *flags;
27492 +
27493 +       /* parse it before aufs lock */
27494 +       err = au_opts_parse(sb, data, &opts);
27495 +       if (unlikely(err))
27496 +               goto out_opts;
27497 +
27498 +       sbinfo = au_sbi(sb);
27499 +       inode = d_inode(root);
27500 +       mutex_lock(&inode->i_mutex);
27501 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27502 +       if (unlikely(err))
27503 +               goto out_mtx;
27504 +       di_write_lock_child(root);
27505 +
27506 +       /* au_opts_remount() may return an error */
27507 +       err = au_opts_remount(sb, &opts);
27508 +       au_opts_free(&opts);
27509 +
27510 +       if (au_ftest_opts(opts.flags, REFRESH))
27511 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
27512 +
27513 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27514 +               mntflags = au_mntflags(sb);
27515 +               do_dx = !!au_opt_test(mntflags, DIO);
27516 +               au_dy_arefresh(do_dx);
27517 +       }
27518 +
27519 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
27520 +       aufs_write_unlock(root);
27521 +
27522 +out_mtx:
27523 +       mutex_unlock(&inode->i_mutex);
27524 +out_opts:
27525 +       free_page((unsigned long)opts.opt);
27526 +out:
27527 +       err = cvt_err(err);
27528 +       AuTraceErr(err);
27529 +       return err;
27530 +}
27531 +
27532 +static const struct super_operations aufs_sop = {
27533 +       .alloc_inode    = aufs_alloc_inode,
27534 +       .destroy_inode  = aufs_destroy_inode,
27535 +       /* always deleting, no clearing */
27536 +       .drop_inode     = generic_delete_inode,
27537 +       .show_options   = aufs_show_options,
27538 +       .statfs         = aufs_statfs,
27539 +       .put_super      = aufs_put_super,
27540 +       .sync_fs        = aufs_sync_fs,
27541 +       .remount_fs     = aufs_remount_fs
27542 +};
27543 +
27544 +/* ---------------------------------------------------------------------- */
27545 +
27546 +static int alloc_root(struct super_block *sb)
27547 +{
27548 +       int err;
27549 +       struct inode *inode;
27550 +       struct dentry *root;
27551 +
27552 +       err = -ENOMEM;
27553 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
27554 +       err = PTR_ERR(inode);
27555 +       if (IS_ERR(inode))
27556 +               goto out;
27557 +
27558 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
27559 +       inode->i_fop = &aufs_dir_fop;
27560 +       inode->i_mode = S_IFDIR;
27561 +       set_nlink(inode, 2);
27562 +       unlock_new_inode(inode);
27563 +
27564 +       root = d_make_root(inode);
27565 +       if (unlikely(!root))
27566 +               goto out;
27567 +       err = PTR_ERR(root);
27568 +       if (IS_ERR(root))
27569 +               goto out;
27570 +
27571 +       err = au_di_init(root);
27572 +       if (!err) {
27573 +               sb->s_root = root;
27574 +               return 0; /* success */
27575 +       }
27576 +       dput(root);
27577 +
27578 +out:
27579 +       return err;
27580 +}
27581 +
27582 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
27583 +                          int silent __maybe_unused)
27584 +{
27585 +       int err;
27586 +       struct au_opts opts = {
27587 +               .opt = NULL
27588 +       };
27589 +       struct au_sbinfo *sbinfo;
27590 +       struct dentry *root;
27591 +       struct inode *inode;
27592 +       char *arg = raw_data;
27593 +
27594 +       if (unlikely(!arg || !*arg)) {
27595 +               err = -EINVAL;
27596 +               pr_err("no arg\n");
27597 +               goto out;
27598 +       }
27599 +
27600 +       err = -ENOMEM;
27601 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27602 +       if (unlikely(!opts.opt))
27603 +               goto out;
27604 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27605 +       opts.sb_flags = sb->s_flags;
27606 +
27607 +       err = au_si_alloc(sb);
27608 +       if (unlikely(err))
27609 +               goto out_opts;
27610 +       sbinfo = au_sbi(sb);
27611 +
27612 +       /* all timestamps always follow the ones on the branch */
27613 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27614 +       sb->s_op = &aufs_sop;
27615 +       sb->s_d_op = &aufs_dop;
27616 +       sb->s_magic = AUFS_SUPER_MAGIC;
27617 +       sb->s_maxbytes = 0;
27618 +       sb->s_stack_depth = 1;
27619 +       au_export_init(sb);
27620 +       /* au_xattr_init(sb); */
27621 +
27622 +       err = alloc_root(sb);
27623 +       if (unlikely(err)) {
27624 +               si_write_unlock(sb);
27625 +               goto out_info;
27626 +       }
27627 +       root = sb->s_root;
27628 +       inode = d_inode(root);
27629 +
27630 +       /*
27631 +        * actually we can parse options regardless aufs lock here.
27632 +        * but at remount time, parsing must be done before aufs lock.
27633 +        * so we follow the same rule.
27634 +        */
27635 +       ii_write_lock_parent(inode);
27636 +       aufs_write_unlock(root);
27637 +       err = au_opts_parse(sb, arg, &opts);
27638 +       if (unlikely(err))
27639 +               goto out_root;
27640 +
27641 +       /* lock vfs_inode first, then aufs. */
27642 +       mutex_lock(&inode->i_mutex);
27643 +       aufs_write_lock(root);
27644 +       err = au_opts_mount(sb, &opts);
27645 +       au_opts_free(&opts);
27646 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
27647 +               sb->s_d_op = &aufs_dop_noreval;
27648 +               pr_info("%pf\n", sb->s_d_op);
27649 +               au_refresh_dop(root, /*force_reval*/0);
27650 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
27651 +               au_refresh_iop(inode, /*force_getattr*/0);
27652 +       }
27653 +       aufs_write_unlock(root);
27654 +       mutex_unlock(&inode->i_mutex);
27655 +       if (!err)
27656 +               goto out_opts; /* success */
27657 +
27658 +out_root:
27659 +       dput(root);
27660 +       sb->s_root = NULL;
27661 +out_info:
27662 +       dbgaufs_si_fin(sbinfo);
27663 +       kobject_put(&sbinfo->si_kobj);
27664 +       sb->s_fs_info = NULL;
27665 +out_opts:
27666 +       free_page((unsigned long)opts.opt);
27667 +out:
27668 +       AuTraceErr(err);
27669 +       err = cvt_err(err);
27670 +       AuTraceErr(err);
27671 +       return err;
27672 +}
27673 +
27674 +/* ---------------------------------------------------------------------- */
27675 +
27676 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27677 +                                const char *dev_name __maybe_unused,
27678 +                                void *raw_data)
27679 +{
27680 +       struct dentry *root;
27681 +       struct super_block *sb;
27682 +
27683 +       /* all timestamps always follow the ones on the branch */
27684 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
27685 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27686 +       if (IS_ERR(root))
27687 +               goto out;
27688 +
27689 +       sb = root->d_sb;
27690 +       si_write_lock(sb, !AuLock_FLUSH);
27691 +       sysaufs_brs_add(sb, 0);
27692 +       si_write_unlock(sb);
27693 +       au_sbilist_add(sb);
27694 +
27695 +out:
27696 +       return root;
27697 +}
27698 +
27699 +static void aufs_kill_sb(struct super_block *sb)
27700 +{
27701 +       struct au_sbinfo *sbinfo;
27702 +
27703 +       sbinfo = au_sbi(sb);
27704 +       if (sbinfo) {
27705 +               au_sbilist_del(sb);
27706 +               aufs_write_lock(sb->s_root);
27707 +               au_fhsm_fin(sb);
27708 +               if (sbinfo->si_wbr_create_ops->fin)
27709 +                       sbinfo->si_wbr_create_ops->fin(sb);
27710 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27711 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
27712 +                       au_remount_refresh(sb, /*do_idop*/0);
27713 +               }
27714 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27715 +                       au_plink_put(sb, /*verbose*/1);
27716 +               au_xino_clr(sb);
27717 +               sbinfo->si_sb = NULL;
27718 +               aufs_write_unlock(sb->s_root);
27719 +               au_nwt_flush(&sbinfo->si_nowait);
27720 +       }
27721 +       kill_anon_super(sb);
27722 +}
27723 +
27724 +struct file_system_type aufs_fs_type = {
27725 +       .name           = AUFS_FSTYPE,
27726 +       /* a race between rename and others */
27727 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
27728 +       .mount          = aufs_mount,
27729 +       .kill_sb        = aufs_kill_sb,
27730 +       /* no need to __module_get() and module_put(). */
27731 +       .owner          = THIS_MODULE,
27732 +};
27733 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27734 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
27735 +++ linux/fs/aufs/super.h       2016-01-13 20:11:11.673093671 +0100
27736 @@ -0,0 +1,641 @@
27737 +/*
27738 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27739 + *
27740 + * This program, aufs is free software; you can redistribute it and/or modify
27741 + * it under the terms of the GNU General Public License as published by
27742 + * the Free Software Foundation; either version 2 of the License, or
27743 + * (at your option) any later version.
27744 + *
27745 + * This program is distributed in the hope that it will be useful,
27746 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27747 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27748 + * GNU General Public License for more details.
27749 + *
27750 + * You should have received a copy of the GNU General Public License
27751 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27752 + */
27753 +
27754 +/*
27755 + * super_block operations
27756 + */
27757 +
27758 +#ifndef __AUFS_SUPER_H__
27759 +#define __AUFS_SUPER_H__
27760 +
27761 +#ifdef __KERNEL__
27762 +
27763 +#include <linux/fs.h>
27764 +#include <linux/kobject.h>
27765 +#include "rwsem.h"
27766 +#include "spl.h"
27767 +#include "wkq.h"
27768 +
27769 +/* policies to select one among multiple writable branches */
27770 +struct au_wbr_copyup_operations {
27771 +       int (*copyup)(struct dentry *dentry);
27772 +};
27773 +
27774 +#define AuWbr_DIR      1               /* target is a dir */
27775 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
27776 +
27777 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
27778 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
27779 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
27780 +
27781 +struct au_wbr_create_operations {
27782 +       int (*create)(struct dentry *dentry, unsigned int flags);
27783 +       int (*init)(struct super_block *sb);
27784 +       int (*fin)(struct super_block *sb);
27785 +};
27786 +
27787 +struct au_wbr_mfs {
27788 +       struct mutex    mfs_lock; /* protect this structure */
27789 +       unsigned long   mfs_jiffy;
27790 +       unsigned long   mfs_expire;
27791 +       aufs_bindex_t   mfs_bindex;
27792 +
27793 +       unsigned long long      mfsrr_bytes;
27794 +       unsigned long long      mfsrr_watermark;
27795 +};
27796 +
27797 +struct pseudo_link {
27798 +       union {
27799 +               struct hlist_node hlist;
27800 +               struct rcu_head rcu;
27801 +       };
27802 +       struct inode *inode;
27803 +};
27804 +
27805 +#define AuPlink_NHASH 100
27806 +static inline int au_plink_hash(ino_t ino)
27807 +{
27808 +       return ino % AuPlink_NHASH;
27809 +}
27810 +
27811 +/* File-based Hierarchical Storage Management */
27812 +struct au_fhsm {
27813 +#ifdef CONFIG_AUFS_FHSM
27814 +       /* allow only one process who can receive the notification */
27815 +       spinlock_t              fhsm_spin;
27816 +       pid_t                   fhsm_pid;
27817 +       wait_queue_head_t       fhsm_wqh;
27818 +       atomic_t                fhsm_readable;
27819 +
27820 +       /* these are protected by si_rwsem */
27821 +       unsigned long           fhsm_expire;
27822 +       aufs_bindex_t           fhsm_bottom;
27823 +#endif
27824 +};
27825 +
27826 +struct au_branch;
27827 +struct au_sbinfo {
27828 +       /* nowait tasks in the system-wide workqueue */
27829 +       struct au_nowait_tasks  si_nowait;
27830 +
27831 +       /*
27832 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27833 +        * rwsem for au_sbinfo is necessary.
27834 +        */
27835 +       struct au_rwsem         si_rwsem;
27836 +
27837 +       /* prevent recursive locking in deleting inode */
27838 +       struct {
27839 +               unsigned long           *bitmap;
27840 +               spinlock_t              tree_lock;
27841 +               struct radix_tree_root  tree;
27842 +       } au_si_pid;
27843 +
27844 +       /*
27845 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27846 +        * remount.
27847 +        */
27848 +       atomic_long_t           si_ninodes, si_nfiles;
27849 +
27850 +       /* branch management */
27851 +       unsigned int            si_generation;
27852 +
27853 +       /* see AuSi_ flags */
27854 +       unsigned char           au_si_status;
27855 +
27856 +       aufs_bindex_t           si_bend;
27857 +
27858 +       /* dirty trick to keep br_id plus */
27859 +       unsigned int            si_last_br_id :
27860 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
27861 +       struct au_branch        **si_branch;
27862 +
27863 +       /* policy to select a writable branch */
27864 +       unsigned char           si_wbr_copyup;
27865 +       unsigned char           si_wbr_create;
27866 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
27867 +       struct au_wbr_create_operations *si_wbr_create_ops;
27868 +
27869 +       /* round robin */
27870 +       atomic_t                si_wbr_rr_next;
27871 +
27872 +       /* most free space */
27873 +       struct au_wbr_mfs       si_wbr_mfs;
27874 +
27875 +       /* File-based Hierarchical Storage Management */
27876 +       struct au_fhsm          si_fhsm;
27877 +
27878 +       /* mount flags */
27879 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
27880 +       unsigned int            si_mntflags;
27881 +
27882 +       /* symlink to follow_link() and put_link() */
27883 +       struct au_sphlhead      si_symlink;
27884 +
27885 +       /* external inode number (bitmap and translation table) */
27886 +       vfs_readf_t             si_xread;
27887 +       vfs_writef_t            si_xwrite;
27888 +       struct file             *si_xib;
27889 +       struct mutex            si_xib_mtx; /* protect xib members */
27890 +       unsigned long           *si_xib_buf;
27891 +       unsigned long           si_xib_last_pindex;
27892 +       int                     si_xib_next_bit;
27893 +       aufs_bindex_t           si_xino_brid;
27894 +       unsigned long           si_xino_jiffy;
27895 +       unsigned long           si_xino_expire;
27896 +       /* reserved for future use */
27897 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
27898 +
27899 +#ifdef CONFIG_AUFS_EXPORT
27900 +       /* i_generation */
27901 +       struct file             *si_xigen;
27902 +       atomic_t                si_xigen_next;
27903 +#endif
27904 +
27905 +       /* dirty trick to suppoer atomic_open */
27906 +       struct au_sphlhead      si_aopen;
27907 +
27908 +       /* vdir parameters */
27909 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
27910 +       unsigned int            si_rdblk;       /* deblk size */
27911 +       unsigned int            si_rdhash;      /* hash size */
27912 +
27913 +       /*
27914 +        * If the number of whiteouts are larger than si_dirwh, leave all of
27915 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
27916 +        * future fsck.aufs or kernel thread will remove them later.
27917 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
27918 +        */
27919 +       unsigned int            si_dirwh;
27920 +
27921 +       /* pseudo_link list */
27922 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
27923 +       wait_queue_head_t       si_plink_wq;
27924 +       spinlock_t              si_plink_maint_lock;
27925 +       pid_t                   si_plink_maint_pid;
27926 +
27927 +       /* file list */
27928 +       struct au_sphlhead      si_files;
27929 +
27930 +       /* with/without getattr, brother of sb->s_d_op */
27931 +       struct inode_operations *si_iop_array;
27932 +
27933 +       /*
27934 +        * sysfs and lifetime management.
27935 +        * this is not a small structure and it may be a waste of memory in case
27936 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
27937 +        * but using sysfs is majority.
27938 +        */
27939 +       struct kobject          si_kobj;
27940 +#ifdef CONFIG_DEBUG_FS
27941 +       struct dentry            *si_dbgaufs;
27942 +       struct dentry            *si_dbgaufs_plink;
27943 +       struct dentry            *si_dbgaufs_xib;
27944 +#ifdef CONFIG_AUFS_EXPORT
27945 +       struct dentry            *si_dbgaufs_xigen;
27946 +#endif
27947 +#endif
27948 +
27949 +#ifdef CONFIG_AUFS_SBILIST
27950 +       struct list_head        si_list;
27951 +#endif
27952 +
27953 +       /* dirty, necessary for unmounting, sysfs and sysrq */
27954 +       struct super_block      *si_sb;
27955 +};
27956 +
27957 +/* sbinfo status flags */
27958 +/*
27959 + * set true when refresh_dirs() failed at remount time.
27960 + * then try refreshing dirs at access time again.
27961 + * if it is false, refreshing dirs at access time is unnecesary
27962 + */
27963 +#define AuSi_FAILED_REFRESH_DIR        1
27964 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
27965 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
27966 +
27967 +#ifndef CONFIG_AUFS_FHSM
27968 +#undef AuSi_FHSM
27969 +#define AuSi_FHSM              0
27970 +#endif
27971 +
27972 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
27973 +                                          unsigned int flag)
27974 +{
27975 +       AuRwMustAnyLock(&sbi->si_rwsem);
27976 +       return sbi->au_si_status & flag;
27977 +}
27978 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
27979 +#define au_fset_si(sbinfo, name) do { \
27980 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27981 +       (sbinfo)->au_si_status |= AuSi_##name; \
27982 +} while (0)
27983 +#define au_fclr_si(sbinfo, name) do { \
27984 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27985 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
27986 +} while (0)
27987 +
27988 +/* ---------------------------------------------------------------------- */
27989 +
27990 +/* policy to select one among writable branches */
27991 +#define AuWbrCopyup(sbinfo, ...) \
27992 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
27993 +#define AuWbrCreate(sbinfo, ...) \
27994 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
27995 +
27996 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
27997 +#define AuLock_DW              1               /* write-lock dentry */
27998 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
27999 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
28000 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
28001 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
28002 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
28003 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
28004 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
28005 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
28006 +#define au_fset_lock(flags, name) \
28007 +       do { (flags) |= AuLock_##name; } while (0)
28008 +#define au_fclr_lock(flags, name) \
28009 +       do { (flags) &= ~AuLock_##name; } while (0)
28010 +
28011 +/* ---------------------------------------------------------------------- */
28012 +
28013 +/* super.c */
28014 +extern struct file_system_type aufs_fs_type;
28015 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
28016 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28017 +                                          unsigned long long max, void *arg);
28018 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28019 +                    struct super_block *sb, void *arg);
28020 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28021 +void au_iarray_free(struct inode **a, unsigned long long max);
28022 +
28023 +/* sbinfo.c */
28024 +void au_si_free(struct kobject *kobj);
28025 +int au_si_alloc(struct super_block *sb);
28026 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
28027 +
28028 +unsigned int au_sigen_inc(struct super_block *sb);
28029 +aufs_bindex_t au_new_br_id(struct super_block *sb);
28030 +
28031 +int si_read_lock(struct super_block *sb, int flags);
28032 +int si_write_lock(struct super_block *sb, int flags);
28033 +int aufs_read_lock(struct dentry *dentry, int flags);
28034 +void aufs_read_unlock(struct dentry *dentry, int flags);
28035 +void aufs_write_lock(struct dentry *dentry);
28036 +void aufs_write_unlock(struct dentry *dentry);
28037 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
28038 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28039 +
28040 +int si_pid_test_slow(struct super_block *sb);
28041 +void si_pid_set_slow(struct super_block *sb);
28042 +void si_pid_clr_slow(struct super_block *sb);
28043 +
28044 +/* wbr_policy.c */
28045 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28046 +extern struct au_wbr_create_operations au_wbr_create_ops[];
28047 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
28048 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
28049 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
28050 +
28051 +/* mvdown.c */
28052 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
28053 +
28054 +#ifdef CONFIG_AUFS_FHSM
28055 +/* fhsm.c */
28056 +
28057 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28058 +{
28059 +       pid_t pid;
28060 +
28061 +       spin_lock(&fhsm->fhsm_spin);
28062 +       pid = fhsm->fhsm_pid;
28063 +       spin_unlock(&fhsm->fhsm_spin);
28064 +
28065 +       return pid;
28066 +}
28067 +
28068 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28069 +void au_fhsm_wrote_all(struct super_block *sb, int force);
28070 +int au_fhsm_fd(struct super_block *sb, int oflags);
28071 +int au_fhsm_br_alloc(struct au_branch *br);
28072 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
28073 +void au_fhsm_fin(struct super_block *sb);
28074 +void au_fhsm_init(struct au_sbinfo *sbinfo);
28075 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28076 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28077 +#else
28078 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28079 +          int force)
28080 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28081 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
28082 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28083 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28084 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
28085 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
28086 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28087 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28088 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28089 +#endif
28090 +
28091 +/* ---------------------------------------------------------------------- */
28092 +
28093 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28094 +{
28095 +       return sb->s_fs_info;
28096 +}
28097 +
28098 +/* ---------------------------------------------------------------------- */
28099 +
28100 +#ifdef CONFIG_AUFS_EXPORT
28101 +int au_test_nfsd(void);
28102 +void au_export_init(struct super_block *sb);
28103 +void au_xigen_inc(struct inode *inode);
28104 +int au_xigen_new(struct inode *inode);
28105 +int au_xigen_set(struct super_block *sb, struct file *base);
28106 +void au_xigen_clr(struct super_block *sb);
28107 +
28108 +static inline int au_busy_or_stale(void)
28109 +{
28110 +       if (!au_test_nfsd())
28111 +               return -EBUSY;
28112 +       return -ESTALE;
28113 +}
28114 +#else
28115 +AuStubInt0(au_test_nfsd, void)
28116 +AuStubVoid(au_export_init, struct super_block *sb)
28117 +AuStubVoid(au_xigen_inc, struct inode *inode)
28118 +AuStubInt0(au_xigen_new, struct inode *inode)
28119 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28120 +AuStubVoid(au_xigen_clr, struct super_block *sb)
28121 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
28122 +#endif /* CONFIG_AUFS_EXPORT */
28123 +
28124 +/* ---------------------------------------------------------------------- */
28125 +
28126 +#ifdef CONFIG_AUFS_SBILIST
28127 +/* module.c */
28128 +extern struct au_splhead au_sbilist;
28129 +
28130 +static inline void au_sbilist_init(void)
28131 +{
28132 +       au_spl_init(&au_sbilist);
28133 +}
28134 +
28135 +static inline void au_sbilist_add(struct super_block *sb)
28136 +{
28137 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
28138 +}
28139 +
28140 +static inline void au_sbilist_del(struct super_block *sb)
28141 +{
28142 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
28143 +}
28144 +
28145 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28146 +static inline void au_sbilist_lock(void)
28147 +{
28148 +       spin_lock(&au_sbilist.spin);
28149 +}
28150 +
28151 +static inline void au_sbilist_unlock(void)
28152 +{
28153 +       spin_unlock(&au_sbilist.spin);
28154 +}
28155 +#define AuGFP_SBILIST  GFP_ATOMIC
28156 +#else
28157 +AuStubVoid(au_sbilist_lock, void)
28158 +AuStubVoid(au_sbilist_unlock, void)
28159 +#define AuGFP_SBILIST  GFP_NOFS
28160 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
28161 +#else
28162 +AuStubVoid(au_sbilist_init, void)
28163 +AuStubVoid(au_sbilist_add, struct super_block *sb)
28164 +AuStubVoid(au_sbilist_del, struct super_block *sb)
28165 +AuStubVoid(au_sbilist_lock, void)
28166 +AuStubVoid(au_sbilist_unlock, void)
28167 +#define AuGFP_SBILIST  GFP_NOFS
28168 +#endif
28169 +
28170 +/* ---------------------------------------------------------------------- */
28171 +
28172 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28173 +{
28174 +       /*
28175 +        * This function is a dynamic '__init' function actually,
28176 +        * so the tiny check for si_rwsem is unnecessary.
28177 +        */
28178 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28179 +#ifdef CONFIG_DEBUG_FS
28180 +       sbinfo->si_dbgaufs = NULL;
28181 +       sbinfo->si_dbgaufs_plink = NULL;
28182 +       sbinfo->si_dbgaufs_xib = NULL;
28183 +#ifdef CONFIG_AUFS_EXPORT
28184 +       sbinfo->si_dbgaufs_xigen = NULL;
28185 +#endif
28186 +#endif
28187 +}
28188 +
28189 +/* ---------------------------------------------------------------------- */
28190 +
28191 +static inline pid_t si_pid_bit(void)
28192 +{
28193 +       /* the origin of pid is 1, but the bitmap's is 0 */
28194 +       return current->pid - 1;
28195 +}
28196 +
28197 +static inline int si_pid_test(struct super_block *sb)
28198 +{
28199 +       pid_t bit;
28200 +
28201 +       bit = si_pid_bit();
28202 +       if (bit < PID_MAX_DEFAULT)
28203 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28204 +       return si_pid_test_slow(sb);
28205 +}
28206 +
28207 +static inline void si_pid_set(struct super_block *sb)
28208 +{
28209 +       pid_t bit;
28210 +
28211 +       bit = si_pid_bit();
28212 +       if (bit < PID_MAX_DEFAULT) {
28213 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28214 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28215 +               /* smp_mb(); */
28216 +       } else
28217 +               si_pid_set_slow(sb);
28218 +}
28219 +
28220 +static inline void si_pid_clr(struct super_block *sb)
28221 +{
28222 +       pid_t bit;
28223 +
28224 +       bit = si_pid_bit();
28225 +       if (bit < PID_MAX_DEFAULT) {
28226 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28227 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28228 +               /* smp_mb(); */
28229 +       } else
28230 +               si_pid_clr_slow(sb);
28231 +}
28232 +
28233 +/* ---------------------------------------------------------------------- */
28234 +
28235 +/* lock superblock. mainly for entry point functions */
28236 +/*
28237 + * __si_read_lock, __si_write_lock,
28238 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28239 + */
28240 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28241 +
28242 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28243 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28244 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28245 +
28246 +static inline void si_noflush_read_lock(struct super_block *sb)
28247 +{
28248 +       __si_read_lock(sb);
28249 +       si_pid_set(sb);
28250 +}
28251 +
28252 +static inline int si_noflush_read_trylock(struct super_block *sb)
28253 +{
28254 +       int locked;
28255 +
28256 +       locked = __si_read_trylock(sb);
28257 +       if (locked)
28258 +               si_pid_set(sb);
28259 +       return locked;
28260 +}
28261 +
28262 +static inline void si_noflush_write_lock(struct super_block *sb)
28263 +{
28264 +       __si_write_lock(sb);
28265 +       si_pid_set(sb);
28266 +}
28267 +
28268 +static inline int si_noflush_write_trylock(struct super_block *sb)
28269 +{
28270 +       int locked;
28271 +
28272 +       locked = __si_write_trylock(sb);
28273 +       if (locked)
28274 +               si_pid_set(sb);
28275 +       return locked;
28276 +}
28277 +
28278 +#if 0 /* reserved */
28279 +static inline int si_read_trylock(struct super_block *sb, int flags)
28280 +{
28281 +       if (au_ftest_lock(flags, FLUSH))
28282 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28283 +       return si_noflush_read_trylock(sb);
28284 +}
28285 +#endif
28286 +
28287 +static inline void si_read_unlock(struct super_block *sb)
28288 +{
28289 +       si_pid_clr(sb);
28290 +       __si_read_unlock(sb);
28291 +}
28292 +
28293 +#if 0 /* reserved */
28294 +static inline int si_write_trylock(struct super_block *sb, int flags)
28295 +{
28296 +       if (au_ftest_lock(flags, FLUSH))
28297 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28298 +       return si_noflush_write_trylock(sb);
28299 +}
28300 +#endif
28301 +
28302 +static inline void si_write_unlock(struct super_block *sb)
28303 +{
28304 +       si_pid_clr(sb);
28305 +       __si_write_unlock(sb);
28306 +}
28307 +
28308 +#if 0 /* reserved */
28309 +static inline void si_downgrade_lock(struct super_block *sb)
28310 +{
28311 +       __si_downgrade_lock(sb);
28312 +}
28313 +#endif
28314 +
28315 +/* ---------------------------------------------------------------------- */
28316 +
28317 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
28318 +{
28319 +       SiMustAnyLock(sb);
28320 +       return au_sbi(sb)->si_bend;
28321 +}
28322 +
28323 +static inline unsigned int au_mntflags(struct super_block *sb)
28324 +{
28325 +       SiMustAnyLock(sb);
28326 +       return au_sbi(sb)->si_mntflags;
28327 +}
28328 +
28329 +static inline unsigned int au_sigen(struct super_block *sb)
28330 +{
28331 +       SiMustAnyLock(sb);
28332 +       return au_sbi(sb)->si_generation;
28333 +}
28334 +
28335 +static inline void au_ninodes_inc(struct super_block *sb)
28336 +{
28337 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
28338 +}
28339 +
28340 +static inline void au_ninodes_dec(struct super_block *sb)
28341 +{
28342 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
28343 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
28344 +}
28345 +
28346 +static inline void au_nfiles_inc(struct super_block *sb)
28347 +{
28348 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
28349 +}
28350 +
28351 +static inline void au_nfiles_dec(struct super_block *sb)
28352 +{
28353 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
28354 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
28355 +}
28356 +
28357 +static inline struct au_branch *au_sbr(struct super_block *sb,
28358 +                                      aufs_bindex_t bindex)
28359 +{
28360 +       SiMustAnyLock(sb);
28361 +       return au_sbi(sb)->si_branch[0 + bindex];
28362 +}
28363 +
28364 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28365 +{
28366 +       SiMustWriteLock(sb);
28367 +       au_sbi(sb)->si_xino_brid = brid;
28368 +}
28369 +
28370 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28371 +{
28372 +       SiMustAnyLock(sb);
28373 +       return au_sbi(sb)->si_xino_brid;
28374 +}
28375 +
28376 +#endif /* __KERNEL__ */
28377 +#endif /* __AUFS_SUPER_H__ */
28378 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28379 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28380 +++ linux/fs/aufs/sysaufs.c     2016-01-13 20:11:11.673093671 +0100
28381 @@ -0,0 +1,104 @@
28382 +/*
28383 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28384 + *
28385 + * This program, aufs is free software; you can redistribute it and/or modify
28386 + * it under the terms of the GNU General Public License as published by
28387 + * the Free Software Foundation; either version 2 of the License, or
28388 + * (at your option) any later version.
28389 + *
28390 + * This program is distributed in the hope that it will be useful,
28391 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28392 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28393 + * GNU General Public License for more details.
28394 + *
28395 + * You should have received a copy of the GNU General Public License
28396 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28397 + */
28398 +
28399 +/*
28400 + * sysfs interface and lifetime management
28401 + * they are necessary regardless sysfs is disabled.
28402 + */
28403 +
28404 +#include <linux/random.h>
28405 +#include "aufs.h"
28406 +
28407 +unsigned long sysaufs_si_mask;
28408 +struct kset *sysaufs_kset;
28409 +
28410 +#define AuSiAttr(_name) { \
28411 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28412 +       .show   = sysaufs_si_##_name,                           \
28413 +}
28414 +
28415 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28416 +struct attribute *sysaufs_si_attrs[] = {
28417 +       &sysaufs_si_attr_xi_path.attr,
28418 +       NULL,
28419 +};
28420 +
28421 +static const struct sysfs_ops au_sbi_ops = {
28422 +       .show   = sysaufs_si_show
28423 +};
28424 +
28425 +static struct kobj_type au_sbi_ktype = {
28426 +       .release        = au_si_free,
28427 +       .sysfs_ops      = &au_sbi_ops,
28428 +       .default_attrs  = sysaufs_si_attrs
28429 +};
28430 +
28431 +/* ---------------------------------------------------------------------- */
28432 +
28433 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28434 +{
28435 +       int err;
28436 +
28437 +       sbinfo->si_kobj.kset = sysaufs_kset;
28438 +       /* cf. sysaufs_name() */
28439 +       err = kobject_init_and_add
28440 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28441 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28442 +
28443 +       dbgaufs_si_null(sbinfo);
28444 +       if (!err) {
28445 +               err = dbgaufs_si_init(sbinfo);
28446 +               if (unlikely(err))
28447 +                       kobject_put(&sbinfo->si_kobj);
28448 +       }
28449 +       return err;
28450 +}
28451 +
28452 +void sysaufs_fin(void)
28453 +{
28454 +       dbgaufs_fin();
28455 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28456 +       kset_unregister(sysaufs_kset);
28457 +}
28458 +
28459 +int __init sysaufs_init(void)
28460 +{
28461 +       int err;
28462 +
28463 +       do {
28464 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28465 +       } while (!sysaufs_si_mask);
28466 +
28467 +       err = -EINVAL;
28468 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28469 +       if (unlikely(!sysaufs_kset))
28470 +               goto out;
28471 +       err = PTR_ERR(sysaufs_kset);
28472 +       if (IS_ERR(sysaufs_kset))
28473 +               goto out;
28474 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28475 +       if (unlikely(err)) {
28476 +               kset_unregister(sysaufs_kset);
28477 +               goto out;
28478 +       }
28479 +
28480 +       err = dbgaufs_init();
28481 +       if (unlikely(err))
28482 +               sysaufs_fin();
28483 +out:
28484 +       return err;
28485 +}
28486 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28487 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
28488 +++ linux/fs/aufs/sysaufs.h     2016-01-13 20:11:11.673093671 +0100
28489 @@ -0,0 +1,101 @@
28490 +/*
28491 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28492 + *
28493 + * This program, aufs is free software; you can redistribute it and/or modify
28494 + * it under the terms of the GNU General Public License as published by
28495 + * the Free Software Foundation; either version 2 of the License, or
28496 + * (at your option) any later version.
28497 + *
28498 + * This program is distributed in the hope that it will be useful,
28499 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28500 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28501 + * GNU General Public License for more details.
28502 + *
28503 + * You should have received a copy of the GNU General Public License
28504 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28505 + */
28506 +
28507 +/*
28508 + * sysfs interface and mount lifetime management
28509 + */
28510 +
28511 +#ifndef __SYSAUFS_H__
28512 +#define __SYSAUFS_H__
28513 +
28514 +#ifdef __KERNEL__
28515 +
28516 +#include <linux/sysfs.h>
28517 +#include "module.h"
28518 +
28519 +struct super_block;
28520 +struct au_sbinfo;
28521 +
28522 +struct sysaufs_si_attr {
28523 +       struct attribute attr;
28524 +       int (*show)(struct seq_file *seq, struct super_block *sb);
28525 +};
28526 +
28527 +/* ---------------------------------------------------------------------- */
28528 +
28529 +/* sysaufs.c */
28530 +extern unsigned long sysaufs_si_mask;
28531 +extern struct kset *sysaufs_kset;
28532 +extern struct attribute *sysaufs_si_attrs[];
28533 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
28534 +int __init sysaufs_init(void);
28535 +void sysaufs_fin(void);
28536 +
28537 +/* ---------------------------------------------------------------------- */
28538 +
28539 +/* some people doesn't like to show a pointer in kernel */
28540 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28541 +{
28542 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
28543 +}
28544 +
28545 +#define SysaufsSiNamePrefix    "si_"
28546 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
28547 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28548 +{
28549 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28550 +                sysaufs_si_id(sbinfo));
28551 +}
28552 +
28553 +struct au_branch;
28554 +#ifdef CONFIG_SYSFS
28555 +/* sysfs.c */
28556 +extern struct attribute_group *sysaufs_attr_group;
28557 +
28558 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28559 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28560 +                        char *buf);
28561 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
28562 +#ifdef CONFIG_COMPAT
28563 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28564 +#endif
28565 +
28566 +void sysaufs_br_init(struct au_branch *br);
28567 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28568 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28569 +
28570 +#define sysaufs_brs_init()     do {} while (0)
28571 +
28572 +#else
28573 +#define sysaufs_attr_group     NULL
28574 +
28575 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
28576 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28577 +       struct attribute *attr, char *buf)
28578 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
28579 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28580 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
28581 +
28582 +static inline void sysaufs_brs_init(void)
28583 +{
28584 +       sysaufs_brs = 0;
28585 +}
28586 +
28587 +#endif /* CONFIG_SYSFS */
28588 +
28589 +#endif /* __KERNEL__ */
28590 +#endif /* __SYSAUFS_H__ */
28591 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28592 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
28593 +++ linux/fs/aufs/sysfs.c       2016-01-13 20:11:11.673093671 +0100
28594 @@ -0,0 +1,376 @@
28595 +/*
28596 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28597 + *
28598 + * This program, aufs is free software; you can redistribute it and/or modify
28599 + * it under the terms of the GNU General Public License as published by
28600 + * the Free Software Foundation; either version 2 of the License, or
28601 + * (at your option) any later version.
28602 + *
28603 + * This program is distributed in the hope that it will be useful,
28604 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28605 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28606 + * GNU General Public License for more details.
28607 + *
28608 + * You should have received a copy of the GNU General Public License
28609 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28610 + */
28611 +
28612 +/*
28613 + * sysfs interface
28614 + */
28615 +
28616 +#include <linux/compat.h>
28617 +#include <linux/seq_file.h>
28618 +#include "aufs.h"
28619 +
28620 +#ifdef CONFIG_AUFS_FS_MODULE
28621 +/* this entry violates the "one line per file" policy of sysfs */
28622 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28623 +                          char *buf)
28624 +{
28625 +       ssize_t err;
28626 +       static char *conf =
28627 +/* this file is generated at compiling */
28628 +#include "conf.str"
28629 +               ;
28630 +
28631 +       err = snprintf(buf, PAGE_SIZE, conf);
28632 +       if (unlikely(err >= PAGE_SIZE))
28633 +               err = -EFBIG;
28634 +       return err;
28635 +}
28636 +
28637 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28638 +#endif
28639 +
28640 +static struct attribute *au_attr[] = {
28641 +#ifdef CONFIG_AUFS_FS_MODULE
28642 +       &au_config_attr.attr,
28643 +#endif
28644 +       NULL,   /* need to NULL terminate the list of attributes */
28645 +};
28646 +
28647 +static struct attribute_group sysaufs_attr_group_body = {
28648 +       .attrs = au_attr
28649 +};
28650 +
28651 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28652 +
28653 +/* ---------------------------------------------------------------------- */
28654 +
28655 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28656 +{
28657 +       int err;
28658 +
28659 +       SiMustAnyLock(sb);
28660 +
28661 +       err = 0;
28662 +       if (au_opt_test(au_mntflags(sb), XINO)) {
28663 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
28664 +               seq_putc(seq, '\n');
28665 +       }
28666 +       return err;
28667 +}
28668 +
28669 +/*
28670 + * the lifetime of branch is independent from the entry under sysfs.
28671 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
28672 + * unlinked.
28673 + */
28674 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
28675 +                        aufs_bindex_t bindex, int idx)
28676 +{
28677 +       int err;
28678 +       struct path path;
28679 +       struct dentry *root;
28680 +       struct au_branch *br;
28681 +       au_br_perm_str_t perm;
28682 +
28683 +       AuDbg("b%d\n", bindex);
28684 +
28685 +       err = 0;
28686 +       root = sb->s_root;
28687 +       di_read_lock_parent(root, !AuLock_IR);
28688 +       br = au_sbr(sb, bindex);
28689 +
28690 +       switch (idx) {
28691 +       case AuBrSysfs_BR:
28692 +               path.mnt = au_br_mnt(br);
28693 +               path.dentry = au_h_dptr(root, bindex);
28694 +               err = au_seq_path(seq, &path);
28695 +               if (!err) {
28696 +                       au_optstr_br_perm(&perm, br->br_perm);
28697 +                       seq_printf(seq, "=%s\n", perm.a);
28698 +               }
28699 +               break;
28700 +       case AuBrSysfs_BRID:
28701 +               seq_printf(seq, "%d\n", br->br_id);
28702 +               break;
28703 +       }
28704 +       di_read_unlock(root, !AuLock_IR);
28705 +       if (unlikely(err || seq_has_overflowed(seq)))
28706 +               err = -E2BIG;
28707 +
28708 +       return err;
28709 +}
28710 +
28711 +/* ---------------------------------------------------------------------- */
28712 +
28713 +static struct seq_file *au_seq(char *p, ssize_t len)
28714 +{
28715 +       struct seq_file *seq;
28716 +
28717 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
28718 +       if (seq) {
28719 +               /* mutex_init(&seq.lock); */
28720 +               seq->buf = p;
28721 +               seq->size = len;
28722 +               return seq; /* success */
28723 +       }
28724 +
28725 +       seq = ERR_PTR(-ENOMEM);
28726 +       return seq;
28727 +}
28728 +
28729 +#define SysaufsBr_PREFIX       "br"
28730 +#define SysaufsBrid_PREFIX     "brid"
28731 +
28732 +/* todo: file size may exceed PAGE_SIZE */
28733 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28734 +                       char *buf)
28735 +{
28736 +       ssize_t err;
28737 +       int idx;
28738 +       long l;
28739 +       aufs_bindex_t bend;
28740 +       struct au_sbinfo *sbinfo;
28741 +       struct super_block *sb;
28742 +       struct seq_file *seq;
28743 +       char *name;
28744 +       struct attribute **cattr;
28745 +
28746 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28747 +       sb = sbinfo->si_sb;
28748 +
28749 +       /*
28750 +        * prevent a race condition between sysfs and aufs.
28751 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28752 +        * prohibits maintaining the sysfs entries.
28753 +        * hew we acquire read lock after sysfs_get_active_two().
28754 +        * on the other hand, the remount process may maintain the sysfs/aufs
28755 +        * entries after acquiring write lock.
28756 +        * it can cause a deadlock.
28757 +        * simply we gave up processing read here.
28758 +        */
28759 +       err = -EBUSY;
28760 +       if (unlikely(!si_noflush_read_trylock(sb)))
28761 +               goto out;
28762 +
28763 +       seq = au_seq(buf, PAGE_SIZE);
28764 +       err = PTR_ERR(seq);
28765 +       if (IS_ERR(seq))
28766 +               goto out_unlock;
28767 +
28768 +       name = (void *)attr->name;
28769 +       cattr = sysaufs_si_attrs;
28770 +       while (*cattr) {
28771 +               if (!strcmp(name, (*cattr)->name)) {
28772 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
28773 +                               ->show(seq, sb);
28774 +                       goto out_seq;
28775 +               }
28776 +               cattr++;
28777 +       }
28778 +
28779 +       if (!strncmp(name, SysaufsBrid_PREFIX,
28780 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
28781 +               idx = AuBrSysfs_BRID;
28782 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
28783 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
28784 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
28785 +               idx = AuBrSysfs_BR;
28786 +               name += sizeof(SysaufsBr_PREFIX) - 1;
28787 +       } else
28788 +                 BUG();
28789 +
28790 +       err = kstrtol(name, 10, &l);
28791 +       if (!err) {
28792 +               bend = au_sbend(sb);
28793 +               if (l <= bend)
28794 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28795 +               else
28796 +                       err = -ENOENT;
28797 +       }
28798 +
28799 +out_seq:
28800 +       if (!err) {
28801 +               err = seq->count;
28802 +               /* sysfs limit */
28803 +               if (unlikely(err == PAGE_SIZE))
28804 +                       err = -EFBIG;
28805 +       }
28806 +       kfree(seq);
28807 +out_unlock:
28808 +       si_read_unlock(sb);
28809 +out:
28810 +       return err;
28811 +}
28812 +
28813 +/* ---------------------------------------------------------------------- */
28814 +
28815 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28816 +{
28817 +       int err;
28818 +       int16_t brid;
28819 +       aufs_bindex_t bindex, bend;
28820 +       size_t sz;
28821 +       char *buf;
28822 +       struct seq_file *seq;
28823 +       struct au_branch *br;
28824 +
28825 +       si_read_lock(sb, AuLock_FLUSH);
28826 +       bend = au_sbend(sb);
28827 +       err = bend + 1;
28828 +       if (!arg)
28829 +               goto out;
28830 +
28831 +       err = -ENOMEM;
28832 +       buf = (void *)__get_free_page(GFP_NOFS);
28833 +       if (unlikely(!buf))
28834 +               goto out;
28835 +
28836 +       seq = au_seq(buf, PAGE_SIZE);
28837 +       err = PTR_ERR(seq);
28838 +       if (IS_ERR(seq))
28839 +               goto out_buf;
28840 +
28841 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
28842 +       for (bindex = 0; bindex <= bend; bindex++, arg++) {
28843 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28844 +               if (unlikely(err))
28845 +                       break;
28846 +
28847 +               br = au_sbr(sb, bindex);
28848 +               brid = br->br_id;
28849 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28850 +               err = __put_user(brid, &arg->id);
28851 +               if (unlikely(err))
28852 +                       break;
28853 +
28854 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28855 +               err = __put_user(br->br_perm, &arg->perm);
28856 +               if (unlikely(err))
28857 +                       break;
28858 +
28859 +               err = au_seq_path(seq, &br->br_path);
28860 +               if (unlikely(err))
28861 +                       break;
28862 +               seq_putc(seq, '\0');
28863 +               if (!seq_has_overflowed(seq)) {
28864 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
28865 +                       seq->count = 0;
28866 +                       if (unlikely(err))
28867 +                               break;
28868 +               } else {
28869 +                       err = -E2BIG;
28870 +                       goto out_seq;
28871 +               }
28872 +       }
28873 +       if (unlikely(err))
28874 +               err = -EFAULT;
28875 +
28876 +out_seq:
28877 +       kfree(seq);
28878 +out_buf:
28879 +       free_page((unsigned long)buf);
28880 +out:
28881 +       si_read_unlock(sb);
28882 +       return err;
28883 +}
28884 +
28885 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
28886 +{
28887 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
28888 +}
28889 +
28890 +#ifdef CONFIG_COMPAT
28891 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
28892 +{
28893 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
28894 +}
28895 +#endif
28896 +
28897 +/* ---------------------------------------------------------------------- */
28898 +
28899 +void sysaufs_br_init(struct au_branch *br)
28900 +{
28901 +       int i;
28902 +       struct au_brsysfs *br_sysfs;
28903 +       struct attribute *attr;
28904 +
28905 +       br_sysfs = br->br_sysfs;
28906 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28907 +               attr = &br_sysfs->attr;
28908 +               sysfs_attr_init(attr);
28909 +               attr->name = br_sysfs->name;
28910 +               attr->mode = S_IRUGO;
28911 +               br_sysfs++;
28912 +       }
28913 +}
28914 +
28915 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
28916 +{
28917 +       struct au_branch *br;
28918 +       struct kobject *kobj;
28919 +       struct au_brsysfs *br_sysfs;
28920 +       int i;
28921 +       aufs_bindex_t bend;
28922 +
28923 +       dbgaufs_brs_del(sb, bindex);
28924 +
28925 +       if (!sysaufs_brs)
28926 +               return;
28927 +
28928 +       kobj = &au_sbi(sb)->si_kobj;
28929 +       bend = au_sbend(sb);
28930 +       for (; bindex <= bend; bindex++) {
28931 +               br = au_sbr(sb, bindex);
28932 +               br_sysfs = br->br_sysfs;
28933 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28934 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
28935 +                       br_sysfs++;
28936 +               }
28937 +       }
28938 +}
28939 +
28940 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
28941 +{
28942 +       int err, i;
28943 +       aufs_bindex_t bend;
28944 +       struct kobject *kobj;
28945 +       struct au_branch *br;
28946 +       struct au_brsysfs *br_sysfs;
28947 +
28948 +       dbgaufs_brs_add(sb, bindex);
28949 +
28950 +       if (!sysaufs_brs)
28951 +               return;
28952 +
28953 +       kobj = &au_sbi(sb)->si_kobj;
28954 +       bend = au_sbend(sb);
28955 +       for (; bindex <= bend; bindex++) {
28956 +               br = au_sbr(sb, bindex);
28957 +               br_sysfs = br->br_sysfs;
28958 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
28959 +                        SysaufsBr_PREFIX "%d", bindex);
28960 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
28961 +                        SysaufsBrid_PREFIX "%d", bindex);
28962 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28963 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
28964 +                       if (unlikely(err))
28965 +                               pr_warn("failed %s under sysfs(%d)\n",
28966 +                                       br_sysfs->name, err);
28967 +                       br_sysfs++;
28968 +               }
28969 +       }
28970 +}
28971 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
28972 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
28973 +++ linux/fs/aufs/sysrq.c       2016-01-13 20:11:11.673093671 +0100
28974 @@ -0,0 +1,157 @@
28975 +/*
28976 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28977 + *
28978 + * This program, aufs is free software; you can redistribute it and/or modify
28979 + * it under the terms of the GNU General Public License as published by
28980 + * the Free Software Foundation; either version 2 of the License, or
28981 + * (at your option) any later version.
28982 + *
28983 + * This program is distributed in the hope that it will be useful,
28984 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28985 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28986 + * GNU General Public License for more details.
28987 + *
28988 + * You should have received a copy of the GNU General Public License
28989 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28990 + */
28991 +
28992 +/*
28993 + * magic sysrq hanlder
28994 + */
28995 +
28996 +/* #include <linux/sysrq.h> */
28997 +#include <linux/writeback.h>
28998 +#include "aufs.h"
28999 +
29000 +/* ---------------------------------------------------------------------- */
29001 +
29002 +static void sysrq_sb(struct super_block *sb)
29003 +{
29004 +       char *plevel;
29005 +       struct au_sbinfo *sbinfo;
29006 +       struct file *file;
29007 +       struct au_sphlhead *files;
29008 +       struct au_finfo *finfo;
29009 +
29010 +       plevel = au_plevel;
29011 +       au_plevel = KERN_WARNING;
29012 +
29013 +       /* since we define pr_fmt, call printk directly */
29014 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29015 +
29016 +       sbinfo = au_sbi(sb);
29017 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
29018 +       pr("superblock\n");
29019 +       au_dpri_sb(sb);
29020 +
29021 +#if 0
29022 +       pr("root dentry\n");
29023 +       au_dpri_dentry(sb->s_root);
29024 +       pr("root inode\n");
29025 +       au_dpri_inode(d_inode(sb->s_root));
29026 +#endif
29027 +
29028 +#if 0
29029 +       do {
29030 +               int err, i, j, ndentry;
29031 +               struct au_dcsub_pages dpages;
29032 +               struct au_dpage *dpage;
29033 +
29034 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
29035 +               if (unlikely(err))
29036 +                       break;
29037 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29038 +               if (!err)
29039 +                       for (i = 0; i < dpages.ndpage; i++) {
29040 +                               dpage = dpages.dpages + i;
29041 +                               ndentry = dpage->ndentry;
29042 +                               for (j = 0; j < ndentry; j++)
29043 +                                       au_dpri_dentry(dpage->dentries[j]);
29044 +                       }
29045 +               au_dpages_free(&dpages);
29046 +       } while (0);
29047 +#endif
29048 +
29049 +#if 1
29050 +       {
29051 +               struct inode *i;
29052 +
29053 +               pr("isolated inode\n");
29054 +               spin_lock(&sb->s_inode_list_lock);
29055 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29056 +                       spin_lock(&i->i_lock);
29057 +                       if (1 || hlist_empty(&i->i_dentry))
29058 +                               au_dpri_inode(i);
29059 +                       spin_unlock(&i->i_lock);
29060 +               }
29061 +               spin_unlock(&sb->s_inode_list_lock);
29062 +       }
29063 +#endif
29064 +       pr("files\n");
29065 +       files = &au_sbi(sb)->si_files;
29066 +       spin_lock(&files->spin);
29067 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
29068 +               umode_t mode;
29069 +
29070 +               file = finfo->fi_file;
29071 +               mode = file_inode(file)->i_mode;
29072 +               if (!special_file(mode))
29073 +                       au_dpri_file(file);
29074 +       }
29075 +       spin_unlock(&files->spin);
29076 +       pr("done\n");
29077 +
29078 +#undef pr
29079 +       au_plevel = plevel;
29080 +}
29081 +
29082 +/* ---------------------------------------------------------------------- */
29083 +
29084 +/* module parameter */
29085 +static char *aufs_sysrq_key = "a";
29086 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29087 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29088 +
29089 +static void au_sysrq(int key __maybe_unused)
29090 +{
29091 +       struct au_sbinfo *sbinfo;
29092 +
29093 +       lockdep_off();
29094 +       au_sbilist_lock();
29095 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
29096 +               sysrq_sb(sbinfo->si_sb);
29097 +       au_sbilist_unlock();
29098 +       lockdep_on();
29099 +}
29100 +
29101 +static struct sysrq_key_op au_sysrq_op = {
29102 +       .handler        = au_sysrq,
29103 +       .help_msg       = "Aufs",
29104 +       .action_msg     = "Aufs",
29105 +       .enable_mask    = SYSRQ_ENABLE_DUMP
29106 +};
29107 +
29108 +/* ---------------------------------------------------------------------- */
29109 +
29110 +int __init au_sysrq_init(void)
29111 +{
29112 +       int err;
29113 +       char key;
29114 +
29115 +       err = -1;
29116 +       key = *aufs_sysrq_key;
29117 +       if ('a' <= key && key <= 'z')
29118 +               err = register_sysrq_key(key, &au_sysrq_op);
29119 +       if (unlikely(err))
29120 +               pr_err("err %d, sysrq=%c\n", err, key);
29121 +       return err;
29122 +}
29123 +
29124 +void au_sysrq_fin(void)
29125 +{
29126 +       int err;
29127 +
29128 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29129 +       if (unlikely(err))
29130 +               pr_err("err %d (ignored)\n", err);
29131 +}
29132 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29133 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
29134 +++ linux/fs/aufs/vdir.c        2016-01-13 20:11:11.673093671 +0100
29135 @@ -0,0 +1,888 @@
29136 +/*
29137 + * Copyright (C) 2005-2015 Junjiro R. Okajima
29138 + *
29139 + * This program, aufs is free software; you can redistribute it and/or modify
29140 + * it under the terms of the GNU General Public License as published by
29141 + * the Free Software Foundation; either version 2 of the License, or
29142 + * (at your option) any later version.
29143 + *
29144 + * This program is distributed in the hope that it will be useful,
29145 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29146 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29147 + * GNU General Public License for more details.
29148 + *
29149 + * You should have received a copy of the GNU General Public License
29150 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29151 + */
29152 +
29153 +/*
29154 + * virtual or vertical directory
29155 + */
29156 +
29157 +#include "aufs.h"
29158 +
29159 +static unsigned int calc_size(int nlen)
29160 +{
29161 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
29162 +}
29163 +
29164 +static int set_deblk_end(union au_vdir_deblk_p *p,
29165 +                        union au_vdir_deblk_p *deblk_end)
29166 +{
29167 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29168 +               p->de->de_str.len = 0;
29169 +               /* smp_mb(); */
29170 +               return 0;
29171 +       }
29172 +       return -1; /* error */
29173 +}
29174 +
29175 +/* returns true or false */
29176 +static int is_deblk_end(union au_vdir_deblk_p *p,
29177 +                       union au_vdir_deblk_p *deblk_end)
29178 +{
29179 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29180 +               return !p->de->de_str.len;
29181 +       return 1;
29182 +}
29183 +
29184 +static unsigned char *last_deblk(struct au_vdir *vdir)
29185 +{
29186 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29187 +}
29188 +
29189 +/* ---------------------------------------------------------------------- */
29190 +
29191 +/* estimate the appropriate size for name hash table */
29192 +unsigned int au_rdhash_est(loff_t sz)
29193 +{
29194 +       unsigned int n;
29195 +
29196 +       n = UINT_MAX;
29197 +       sz >>= 10;
29198 +       if (sz < n)
29199 +               n = sz;
29200 +       if (sz < AUFS_RDHASH_DEF)
29201 +               n = AUFS_RDHASH_DEF;
29202 +       /* pr_info("n %u\n", n); */
29203 +       return n;
29204 +}
29205 +
29206 +/*
29207 + * the allocated memory has to be freed by
29208 + * au_nhash_wh_free() or au_nhash_de_free().
29209 + */
29210 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29211 +{
29212 +       struct hlist_head *head;
29213 +       unsigned int u;
29214 +       size_t sz;
29215 +
29216 +       sz = sizeof(*nhash->nh_head) * num_hash;
29217 +       head = kmalloc(sz, gfp);
29218 +       if (head) {
29219 +               nhash->nh_num = num_hash;
29220 +               nhash->nh_head = head;
29221 +               for (u = 0; u < num_hash; u++)
29222 +                       INIT_HLIST_HEAD(head++);
29223 +               return 0; /* success */
29224 +       }
29225 +
29226 +       return -ENOMEM;
29227 +}
29228 +
29229 +static void nhash_count(struct hlist_head *head)
29230 +{
29231 +#if 0
29232 +       unsigned long n;
29233 +       struct hlist_node *pos;
29234 +
29235 +       n = 0;
29236 +       hlist_for_each(pos, head)
29237 +               n++;
29238 +       pr_info("%lu\n", n);
29239 +#endif
29240 +}
29241 +
29242 +static void au_nhash_wh_do_free(struct hlist_head *head)
29243 +{
29244 +       struct au_vdir_wh *pos;
29245 +       struct hlist_node *node;
29246 +
29247 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29248 +               kfree(pos);
29249 +}
29250 +
29251 +static void au_nhash_de_do_free(struct hlist_head *head)
29252 +{
29253 +       struct au_vdir_dehstr *pos;
29254 +       struct hlist_node *node;
29255 +
29256 +       hlist_for_each_entry_safe(pos, node, head, hash)
29257 +               au_cache_free_vdir_dehstr(pos);
29258 +}
29259 +
29260 +static void au_nhash_do_free(struct au_nhash *nhash,
29261 +                            void (*free)(struct hlist_head *head))
29262 +{
29263 +       unsigned int n;
29264 +       struct hlist_head *head;
29265 +
29266 +       n = nhash->nh_num;
29267 +       if (!n)
29268 +               return;
29269 +
29270 +       head = nhash->nh_head;
29271 +       while (n-- > 0) {
29272 +               nhash_count(head);
29273 +               free(head++);
29274 +       }
29275 +       kfree(nhash->nh_head);
29276 +}
29277 +
29278 +void au_nhash_wh_free(struct au_nhash *whlist)
29279 +{
29280 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29281 +}
29282 +
29283 +static void au_nhash_de_free(struct au_nhash *delist)
29284 +{
29285 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29286 +}
29287 +
29288 +/* ---------------------------------------------------------------------- */
29289 +
29290 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29291 +                           int limit)
29292 +{
29293 +       int num;
29294 +       unsigned int u, n;
29295 +       struct hlist_head *head;
29296 +       struct au_vdir_wh *pos;
29297 +
29298 +       num = 0;
29299 +       n = whlist->nh_num;
29300 +       head = whlist->nh_head;
29301 +       for (u = 0; u < n; u++, head++)
29302 +               hlist_for_each_entry(pos, head, wh_hash)
29303 +                       if (pos->wh_bindex == btgt && ++num > limit)
29304 +                               return 1;
29305 +       return 0;
29306 +}
29307 +
29308 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29309 +                                      unsigned char *name,
29310 +                                      unsigned int len)
29311 +{
29312 +       unsigned int v;
29313 +       /* const unsigned int magic_bit = 12; */
29314 +
29315 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29316 +
29317 +       v = 0;
29318 +       while (len--)
29319 +               v += *name++;
29320 +       /* v = hash_long(v, magic_bit); */
29321 +       v %= nhash->nh_num;
29322 +       return nhash->nh_head + v;
29323 +}
29324 +
29325 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29326 +                             int nlen)
29327 +{
29328 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29329 +}
29330 +
29331 +/* returns found or not */
29332 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29333 +{
29334 +       struct hlist_head *head;
29335 +       struct au_vdir_wh *pos;
29336 +       struct au_vdir_destr *str;
29337 +
29338 +       head = au_name_hash(whlist, name, nlen);
29339 +       hlist_for_each_entry(pos, head, wh_hash) {
29340 +               str = &pos->wh_str;
29341 +               AuDbg("%.*s\n", str->len, str->name);
29342 +               if (au_nhash_test_name(str, name, nlen))
29343 +                       return 1;
29344 +       }
29345 +       return 0;
29346 +}
29347 +
29348 +/* returns found(true) or not */
29349 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29350 +{
29351 +       struct hlist_head *head;
29352 +       struct au_vdir_dehstr *pos;
29353 +       struct au_vdir_destr *str;
29354 +
29355 +       head = au_name_hash(delist, name, nlen);
29356 +       hlist_for_each_entry(pos, head, hash) {
29357 +               str = pos->str;
29358 +               AuDbg("%.*s\n", str->len, str->name);
29359 +               if (au_nhash_test_name(str, name, nlen))
29360 +                       return 1;
29361 +       }
29362 +       return 0;
29363 +}
29364 +
29365 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29366 +                           unsigned char d_type)
29367 +{
29368 +#ifdef CONFIG_AUFS_SHWH
29369 +       wh->wh_ino = ino;
29370 +       wh->wh_type = d_type;
29371 +#endif
29372 +}
29373 +
29374 +/* ---------------------------------------------------------------------- */
29375 +
29376 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29377 +                      unsigned int d_type, aufs_bindex_t bindex,
29378 +                      unsigned char shwh)
29379 +{
29380 +       int err;
29381 +       struct au_vdir_destr *str;
29382 +       struct au_vdir_wh *wh;
29383 +
29384 +       AuDbg("%.*s\n", nlen, name);
29385 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29386 +
29387 +       err = -ENOMEM;
29388 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29389 +       if (unlikely(!wh))
29390 +               goto out;
29391 +
29392 +       err = 0;
29393 +       wh->wh_bindex = bindex;
29394 +       if (shwh)
29395 +               au_shwh_init_wh(wh, ino, d_type);
29396 +       str = &wh->wh_str;
29397 +       str->len = nlen;
29398 +       memcpy(str->name, name, nlen);
29399 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29400 +       /* smp_mb(); */
29401 +
29402 +out:
29403 +       return err;
29404 +}
29405 +
29406 +static int append_deblk(struct au_vdir *vdir)
29407 +{
29408 +       int err;
29409 +       unsigned long ul;
29410 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29411 +       union au_vdir_deblk_p p, deblk_end;
29412 +       unsigned char **o;
29413 +
29414 +       err = -ENOMEM;
29415 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29416 +                    GFP_NOFS);
29417 +       if (unlikely(!o))
29418 +               goto out;
29419 +
29420 +       vdir->vd_deblk = o;
29421 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29422 +       if (p.deblk) {
29423 +               ul = vdir->vd_nblk++;
29424 +               vdir->vd_deblk[ul] = p.deblk;
29425 +               vdir->vd_last.ul = ul;
29426 +               vdir->vd_last.p.deblk = p.deblk;
29427 +               deblk_end.deblk = p.deblk + deblk_sz;
29428 +               err = set_deblk_end(&p, &deblk_end);
29429 +       }
29430 +
29431 +out:
29432 +       return err;
29433 +}
29434 +
29435 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29436 +                    unsigned int d_type, struct au_nhash *delist)
29437 +{
29438 +       int err;
29439 +       unsigned int sz;
29440 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29441 +       union au_vdir_deblk_p p, *room, deblk_end;
29442 +       struct au_vdir_dehstr *dehstr;
29443 +
29444 +       p.deblk = last_deblk(vdir);
29445 +       deblk_end.deblk = p.deblk + deblk_sz;
29446 +       room = &vdir->vd_last.p;
29447 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29448 +                 || !is_deblk_end(room, &deblk_end));
29449 +
29450 +       sz = calc_size(nlen);
29451 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29452 +               err = append_deblk(vdir);
29453 +               if (unlikely(err))
29454 +                       goto out;
29455 +
29456 +               p.deblk = last_deblk(vdir);
29457 +               deblk_end.deblk = p.deblk + deblk_sz;
29458 +               /* smp_mb(); */
29459 +               AuDebugOn(room->deblk != p.deblk);
29460 +       }
29461 +
29462 +       err = -ENOMEM;
29463 +       dehstr = au_cache_alloc_vdir_dehstr();
29464 +       if (unlikely(!dehstr))
29465 +               goto out;
29466 +
29467 +       dehstr->str = &room->de->de_str;
29468 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29469 +       room->de->de_ino = ino;
29470 +       room->de->de_type = d_type;
29471 +       room->de->de_str.len = nlen;
29472 +       memcpy(room->de->de_str.name, name, nlen);
29473 +
29474 +       err = 0;
29475 +       room->deblk += sz;
29476 +       if (unlikely(set_deblk_end(room, &deblk_end)))
29477 +               err = append_deblk(vdir);
29478 +       /* smp_mb(); */
29479 +
29480 +out:
29481 +       return err;
29482 +}
29483 +
29484 +/* ---------------------------------------------------------------------- */
29485 +
29486 +void au_vdir_free(struct au_vdir *vdir)
29487 +{
29488 +       unsigned char **deblk;
29489 +
29490 +       deblk = vdir->vd_deblk;
29491 +       while (vdir->vd_nblk--)
29492 +               kfree(*deblk++);
29493 +       kfree(vdir->vd_deblk);
29494 +       au_cache_free_vdir(vdir);
29495 +}
29496 +
29497 +static struct au_vdir *alloc_vdir(struct file *file)
29498 +{
29499 +       struct au_vdir *vdir;
29500 +       struct super_block *sb;
29501 +       int err;
29502 +
29503 +       sb = file->f_path.dentry->d_sb;
29504 +       SiMustAnyLock(sb);
29505 +
29506 +       err = -ENOMEM;
29507 +       vdir = au_cache_alloc_vdir();
29508 +       if (unlikely(!vdir))
29509 +               goto out;
29510 +
29511 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29512 +       if (unlikely(!vdir->vd_deblk))
29513 +               goto out_free;
29514 +
29515 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
29516 +       if (!vdir->vd_deblk_sz) {
29517 +               /* estimate the appropriate size for deblk */
29518 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
29519 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
29520 +       }
29521 +       vdir->vd_nblk = 0;
29522 +       vdir->vd_version = 0;
29523 +       vdir->vd_jiffy = 0;
29524 +       err = append_deblk(vdir);
29525 +       if (!err)
29526 +               return vdir; /* success */
29527 +
29528 +       kfree(vdir->vd_deblk);
29529 +
29530 +out_free:
29531 +       au_cache_free_vdir(vdir);
29532 +out:
29533 +       vdir = ERR_PTR(err);
29534 +       return vdir;
29535 +}
29536 +
29537 +static int reinit_vdir(struct au_vdir *vdir)
29538 +{
29539 +       int err;
29540 +       union au_vdir_deblk_p p, deblk_end;
29541 +
29542 +       while (vdir->vd_nblk > 1) {
29543 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
29544 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29545 +               vdir->vd_nblk--;
29546 +       }
29547 +       p.deblk = vdir->vd_deblk[0];
29548 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29549 +       err = set_deblk_end(&p, &deblk_end);
29550 +       /* keep vd_dblk_sz */
29551 +       vdir->vd_last.ul = 0;
29552 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29553 +       vdir->vd_version = 0;
29554 +       vdir->vd_jiffy = 0;
29555 +       /* smp_mb(); */
29556 +       return err;
29557 +}
29558 +
29559 +/* ---------------------------------------------------------------------- */
29560 +
29561 +#define AuFillVdir_CALLED      1
29562 +#define AuFillVdir_WHABLE      (1 << 1)
29563 +#define AuFillVdir_SHWH                (1 << 2)
29564 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
29565 +#define au_fset_fillvdir(flags, name) \
29566 +       do { (flags) |= AuFillVdir_##name; } while (0)
29567 +#define au_fclr_fillvdir(flags, name) \
29568 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
29569 +
29570 +#ifndef CONFIG_AUFS_SHWH
29571 +#undef AuFillVdir_SHWH
29572 +#define AuFillVdir_SHWH                0
29573 +#endif
29574 +
29575 +struct fillvdir_arg {
29576 +       struct dir_context      ctx;
29577 +       struct file             *file;
29578 +       struct au_vdir          *vdir;
29579 +       struct au_nhash         delist;
29580 +       struct au_nhash         whlist;
29581 +       aufs_bindex_t           bindex;
29582 +       unsigned int            flags;
29583 +       int                     err;
29584 +};
29585 +
29586 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
29587 +                   loff_t offset __maybe_unused, u64 h_ino,
29588 +                   unsigned int d_type)
29589 +{
29590 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
29591 +       char *name = (void *)__name;
29592 +       struct super_block *sb;
29593 +       ino_t ino;
29594 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
29595 +
29596 +       arg->err = 0;
29597 +       sb = arg->file->f_path.dentry->d_sb;
29598 +       au_fset_fillvdir(arg->flags, CALLED);
29599 +       /* smp_mb(); */
29600 +       if (nlen <= AUFS_WH_PFX_LEN
29601 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
29602 +               if (test_known(&arg->delist, name, nlen)
29603 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29604 +                       goto out; /* already exists or whiteouted */
29605 +
29606 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
29607 +               if (!arg->err) {
29608 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
29609 +                               d_type = DT_UNKNOWN;
29610 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
29611 +                                            d_type, &arg->delist);
29612 +               }
29613 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29614 +               name += AUFS_WH_PFX_LEN;
29615 +               nlen -= AUFS_WH_PFX_LEN;
29616 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29617 +                       goto out; /* already whiteouted */
29618 +
29619 +               if (shwh)
29620 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29621 +                                            &ino);
29622 +               if (!arg->err) {
29623 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29624 +                               d_type = DT_UNKNOWN;
29625 +                       arg->err = au_nhash_append_wh
29626 +                               (&arg->whlist, name, nlen, ino, d_type,
29627 +                                arg->bindex, shwh);
29628 +               }
29629 +       }
29630 +
29631 +out:
29632 +       if (!arg->err)
29633 +               arg->vdir->vd_jiffy = jiffies;
29634 +       /* smp_mb(); */
29635 +       AuTraceErr(arg->err);
29636 +       return arg->err;
29637 +}
29638 +
29639 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29640 +                         struct au_nhash *whlist, struct au_nhash *delist)
29641 +{
29642 +#ifdef CONFIG_AUFS_SHWH
29643 +       int err;
29644 +       unsigned int nh, u;
29645 +       struct hlist_head *head;
29646 +       struct au_vdir_wh *pos;
29647 +       struct hlist_node *n;
29648 +       char *p, *o;
29649 +       struct au_vdir_destr *destr;
29650 +
29651 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29652 +
29653 +       err = -ENOMEM;
29654 +       o = p = (void *)__get_free_page(GFP_NOFS);
29655 +       if (unlikely(!p))
29656 +               goto out;
29657 +
29658 +       err = 0;
29659 +       nh = whlist->nh_num;
29660 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29661 +       p += AUFS_WH_PFX_LEN;
29662 +       for (u = 0; u < nh; u++) {
29663 +               head = whlist->nh_head + u;
29664 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29665 +                       destr = &pos->wh_str;
29666 +                       memcpy(p, destr->name, destr->len);
29667 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
29668 +                                       pos->wh_ino, pos->wh_type, delist);
29669 +                       if (unlikely(err))
29670 +                               break;
29671 +               }
29672 +       }
29673 +
29674 +       free_page((unsigned long)o);
29675 +
29676 +out:
29677 +       AuTraceErr(err);
29678 +       return err;
29679 +#else
29680 +       return 0;
29681 +#endif
29682 +}
29683 +
29684 +static int au_do_read_vdir(struct fillvdir_arg *arg)
29685 +{
29686 +       int err;
29687 +       unsigned int rdhash;
29688 +       loff_t offset;
29689 +       aufs_bindex_t bend, bindex, bstart;
29690 +       unsigned char shwh;
29691 +       struct file *hf, *file;
29692 +       struct super_block *sb;
29693 +
29694 +       file = arg->file;
29695 +       sb = file->f_path.dentry->d_sb;
29696 +       SiMustAnyLock(sb);
29697 +
29698 +       rdhash = au_sbi(sb)->si_rdhash;
29699 +       if (!rdhash)
29700 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
29701 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29702 +       if (unlikely(err))
29703 +               goto out;
29704 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29705 +       if (unlikely(err))
29706 +               goto out_delist;
29707 +
29708 +       err = 0;
29709 +       arg->flags = 0;
29710 +       shwh = 0;
29711 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
29712 +               shwh = 1;
29713 +               au_fset_fillvdir(arg->flags, SHWH);
29714 +       }
29715 +       bstart = au_fbstart(file);
29716 +       bend = au_fbend_dir(file);
29717 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
29718 +               hf = au_hf_dir(file, bindex);
29719 +               if (!hf)
29720 +                       continue;
29721 +
29722 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
29723 +               err = offset;
29724 +               if (unlikely(offset))
29725 +                       break;
29726 +
29727 +               arg->bindex = bindex;
29728 +               au_fclr_fillvdir(arg->flags, WHABLE);
29729 +               if (shwh
29730 +                   || (bindex != bend
29731 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
29732 +                       au_fset_fillvdir(arg->flags, WHABLE);
29733 +               do {
29734 +                       arg->err = 0;
29735 +                       au_fclr_fillvdir(arg->flags, CALLED);
29736 +                       /* smp_mb(); */
29737 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
29738 +                       if (err >= 0)
29739 +                               err = arg->err;
29740 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
29741 +
29742 +               /*
29743 +                * dir_relax() may be good for concurrency, but aufs should not
29744 +                * use it since it will cause a lockdep problem.
29745 +                */
29746 +       }
29747 +
29748 +       if (!err && shwh)
29749 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29750 +
29751 +       au_nhash_wh_free(&arg->whlist);
29752 +
29753 +out_delist:
29754 +       au_nhash_de_free(&arg->delist);
29755 +out:
29756 +       return err;
29757 +}
29758 +
29759 +static int read_vdir(struct file *file, int may_read)
29760 +{
29761 +       int err;
29762 +       unsigned long expire;
29763 +       unsigned char do_read;
29764 +       struct fillvdir_arg arg = {
29765 +               .ctx = {
29766 +                       .actor = fillvdir
29767 +               }
29768 +       };
29769 +       struct inode *inode;
29770 +       struct au_vdir *vdir, *allocated;
29771 +
29772 +       err = 0;
29773 +       inode = file_inode(file);
29774 +       IMustLock(inode);
29775 +       SiMustAnyLock(inode->i_sb);
29776 +
29777 +       allocated = NULL;
29778 +       do_read = 0;
29779 +       expire = au_sbi(inode->i_sb)->si_rdcache;
29780 +       vdir = au_ivdir(inode);
29781 +       if (!vdir) {
29782 +               do_read = 1;
29783 +               vdir = alloc_vdir(file);
29784 +               err = PTR_ERR(vdir);
29785 +               if (IS_ERR(vdir))
29786 +                       goto out;
29787 +               err = 0;
29788 +               allocated = vdir;
29789 +       } else if (may_read
29790 +                  && (inode->i_version != vdir->vd_version
29791 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
29792 +               do_read = 1;
29793 +               err = reinit_vdir(vdir);
29794 +               if (unlikely(err))
29795 +                       goto out;
29796 +       }
29797 +
29798 +       if (!do_read)
29799 +               return 0; /* success */
29800 +
29801 +       arg.file = file;
29802 +       arg.vdir = vdir;
29803 +       err = au_do_read_vdir(&arg);
29804 +       if (!err) {
29805 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
29806 +               vdir->vd_version = inode->i_version;
29807 +               vdir->vd_last.ul = 0;
29808 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29809 +               if (allocated)
29810 +                       au_set_ivdir(inode, allocated);
29811 +       } else if (allocated)
29812 +               au_vdir_free(allocated);
29813 +
29814 +out:
29815 +       return err;
29816 +}
29817 +
29818 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29819 +{
29820 +       int err, rerr;
29821 +       unsigned long ul, n;
29822 +       const unsigned int deblk_sz = src->vd_deblk_sz;
29823 +
29824 +       AuDebugOn(tgt->vd_nblk != 1);
29825 +
29826 +       err = -ENOMEM;
29827 +       if (tgt->vd_nblk < src->vd_nblk) {
29828 +               unsigned char **p;
29829 +
29830 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29831 +                            GFP_NOFS);
29832 +               if (unlikely(!p))
29833 +                       goto out;
29834 +               tgt->vd_deblk = p;
29835 +       }
29836 +
29837 +       if (tgt->vd_deblk_sz != deblk_sz) {
29838 +               unsigned char *p;
29839 +
29840 +               tgt->vd_deblk_sz = deblk_sz;
29841 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29842 +               if (unlikely(!p))
29843 +                       goto out;
29844 +               tgt->vd_deblk[0] = p;
29845 +       }
29846 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
29847 +       tgt->vd_version = src->vd_version;
29848 +       tgt->vd_jiffy = src->vd_jiffy;
29849 +
29850 +       n = src->vd_nblk;
29851 +       for (ul = 1; ul < n; ul++) {
29852 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29853 +                                           GFP_NOFS);
29854 +               if (unlikely(!tgt->vd_deblk[ul]))
29855 +                       goto out;
29856 +               tgt->vd_nblk++;
29857 +       }
29858 +       tgt->vd_nblk = n;
29859 +       tgt->vd_last.ul = tgt->vd_last.ul;
29860 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
29861 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
29862 +               - src->vd_deblk[src->vd_last.ul];
29863 +       /* smp_mb(); */
29864 +       return 0; /* success */
29865 +
29866 +out:
29867 +       rerr = reinit_vdir(tgt);
29868 +       BUG_ON(rerr);
29869 +       return err;
29870 +}
29871 +
29872 +int au_vdir_init(struct file *file)
29873 +{
29874 +       int err;
29875 +       struct inode *inode;
29876 +       struct au_vdir *vdir_cache, *allocated;
29877 +
29878 +       /* test file->f_pos here instead of ctx->pos */
29879 +       err = read_vdir(file, !file->f_pos);
29880 +       if (unlikely(err))
29881 +               goto out;
29882 +
29883 +       allocated = NULL;
29884 +       vdir_cache = au_fvdir_cache(file);
29885 +       if (!vdir_cache) {
29886 +               vdir_cache = alloc_vdir(file);
29887 +               err = PTR_ERR(vdir_cache);
29888 +               if (IS_ERR(vdir_cache))
29889 +                       goto out;
29890 +               allocated = vdir_cache;
29891 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
29892 +               /* test file->f_pos here instead of ctx->pos */
29893 +               err = reinit_vdir(vdir_cache);
29894 +               if (unlikely(err))
29895 +                       goto out;
29896 +       } else
29897 +               return 0; /* success */
29898 +
29899 +       inode = file_inode(file);
29900 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
29901 +       if (!err) {
29902 +               file->f_version = inode->i_version;
29903 +               if (allocated)
29904 +                       au_set_fvdir_cache(file, allocated);
29905 +       } else if (allocated)
29906 +               au_vdir_free(allocated);
29907 +
29908 +out:
29909 +       return err;
29910 +}
29911 +
29912 +static loff_t calc_offset(struct au_vdir *vdir)
29913 +{
29914 +       loff_t offset;
29915 +       union au_vdir_deblk_p p;
29916 +
29917 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
29918 +       offset = vdir->vd_last.p.deblk - p.deblk;
29919 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
29920 +       return offset;
29921 +}
29922 +
29923 +/* returns true or false */
29924 +static int seek_vdir(struct file *file, struct dir_context *ctx)
29925 +{
29926 +       int valid;
29927 +       unsigned int deblk_sz;
29928 +       unsigned long ul, n;
29929 +       loff_t offset;
29930 +       union au_vdir_deblk_p p, deblk_end;
29931 +       struct au_vdir *vdir_cache;
29932 +
29933 +       valid = 1;
29934 +       vdir_cache = au_fvdir_cache(file);
29935 +       offset = calc_offset(vdir_cache);
29936 +       AuDbg("offset %lld\n", offset);
29937 +       if (ctx->pos == offset)
29938 +               goto out;
29939 +
29940 +       vdir_cache->vd_last.ul = 0;
29941 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
29942 +       if (!ctx->pos)
29943 +               goto out;
29944 +
29945 +       valid = 0;
29946 +       deblk_sz = vdir_cache->vd_deblk_sz;
29947 +       ul = div64_u64(ctx->pos, deblk_sz);
29948 +       AuDbg("ul %lu\n", ul);
29949 +       if (ul >= vdir_cache->vd_nblk)
29950 +               goto out;
29951 +
29952 +       n = vdir_cache->vd_nblk;
29953 +       for (; ul < n; ul++) {
29954 +               p.deblk = vdir_cache->vd_deblk[ul];
29955 +               deblk_end.deblk = p.deblk + deblk_sz;
29956 +               offset = ul;
29957 +               offset *= deblk_sz;
29958 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
29959 +                       unsigned int l;
29960 +
29961 +                       l = calc_size(p.de->de_str.len);
29962 +                       offset += l;
29963 +                       p.deblk += l;
29964 +               }
29965 +               if (!is_deblk_end(&p, &deblk_end)) {
29966 +                       valid = 1;
29967 +                       vdir_cache->vd_last.ul = ul;
29968 +                       vdir_cache->vd_last.p = p;
29969 +                       break;
29970 +               }
29971 +       }
29972 +
29973 +out:
29974 +       /* smp_mb(); */
29975 +       AuTraceErr(!valid);
29976 +       return valid;
29977 +}
29978 +
29979 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
29980 +{
29981 +       unsigned int l, deblk_sz;
29982 +       union au_vdir_deblk_p deblk_end;
29983 +       struct au_vdir *vdir_cache;
29984 +       struct au_vdir_de *de;
29985 +
29986 +       vdir_cache = au_fvdir_cache(file);
29987 +       if (!seek_vdir(file, ctx))
29988 +               return 0;
29989 +
29990 +       deblk_sz = vdir_cache->vd_deblk_sz;
29991 +       while (1) {
29992 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29993 +               deblk_end.deblk += deblk_sz;
29994 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
29995 +                       de = vdir_cache->vd_last.p.de;
29996 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
29997 +                             de->de_str.len, de->de_str.name, ctx->pos,
29998 +                             (unsigned long)de->de_ino, de->de_type);
29999 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
30000 +                                              de->de_str.len, de->de_ino,
30001 +                                              de->de_type))) {
30002 +                               /* todo: ignore the error caused by udba? */
30003 +                               /* return err; */
30004 +                               return 0;
30005 +                       }
30006 +
30007 +                       l = calc_size(de->de_str.len);
30008 +                       vdir_cache->vd_last.p.deblk += l;
30009 +                       ctx->pos += l;
30010 +               }
30011 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30012 +                       vdir_cache->vd_last.ul++;
30013 +                       vdir_cache->vd_last.p.deblk
30014 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30015 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
30016 +                       continue;
30017 +               }
30018 +               break;
30019 +       }
30020 +
30021 +       /* smp_mb(); */
30022 +       return 0;
30023 +}
30024 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30025 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
30026 +++ linux/fs/aufs/vfsub.c       2016-01-13 20:11:11.673093671 +0100
30027 @@ -0,0 +1,848 @@
30028 +/*
30029 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30030 + *
30031 + * This program, aufs is free software; you can redistribute it and/or modify
30032 + * it under the terms of the GNU General Public License as published by
30033 + * the Free Software Foundation; either version 2 of the License, or
30034 + * (at your option) any later version.
30035 + *
30036 + * This program is distributed in the hope that it will be useful,
30037 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30038 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30039 + * GNU General Public License for more details.
30040 + *
30041 + * You should have received a copy of the GNU General Public License
30042 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30043 + */
30044 +
30045 +/*
30046 + * sub-routines for VFS
30047 + */
30048 +
30049 +#include <linux/namei.h>
30050 +#include <linux/security.h>
30051 +#include <linux/splice.h>
30052 +#include "aufs.h"
30053 +
30054 +int vfsub_update_h_iattr(struct path *h_path, int *did)
30055 +{
30056 +       int err;
30057 +       struct kstat st;
30058 +       struct super_block *h_sb;
30059 +
30060 +       /* for remote fs, leave work for its getattr or d_revalidate */
30061 +       /* for bad i_attr fs, handle them in aufs_getattr() */
30062 +       /* still some fs may acquire i_mutex. we need to skip them */
30063 +       err = 0;
30064 +       if (!did)
30065 +               did = &err;
30066 +       h_sb = h_path->dentry->d_sb;
30067 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30068 +       if (*did)
30069 +               err = vfs_getattr(h_path, &st);
30070 +
30071 +       return err;
30072 +}
30073 +
30074 +/* ---------------------------------------------------------------------- */
30075 +
30076 +struct file *vfsub_dentry_open(struct path *path, int flags)
30077 +{
30078 +       struct file *file;
30079 +
30080 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
30081 +                          current_cred());
30082 +       if (!IS_ERR_OR_NULL(file)
30083 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
30084 +               i_readcount_inc(d_inode(path->dentry));
30085 +
30086 +       return file;
30087 +}
30088 +
30089 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30090 +{
30091 +       struct file *file;
30092 +
30093 +       lockdep_off();
30094 +       file = filp_open(path,
30095 +                        oflags /* | __FMODE_NONOTIFY */,
30096 +                        mode);
30097 +       lockdep_on();
30098 +       if (IS_ERR(file))
30099 +               goto out;
30100 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30101 +
30102 +out:
30103 +       return file;
30104 +}
30105 +
30106 +/*
30107 + * Ideally this function should call VFS:do_last() in order to keep all its
30108 + * checkings. But it is very hard for aufs to regenerate several VFS internal
30109 + * structure such as nameidata. This is a second (or third) best approach.
30110 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30111 + */
30112 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30113 +                     struct vfsub_aopen_args *args, struct au_branch *br)
30114 +{
30115 +       int err;
30116 +       struct file *file = args->file;
30117 +       /* copied from linux/fs/namei.c:atomic_open() */
30118 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30119 +
30120 +       IMustLock(dir);
30121 +       AuDebugOn(!dir->i_op->atomic_open);
30122 +
30123 +       err = au_br_test_oflag(args->open_flag, br);
30124 +       if (unlikely(err))
30125 +               goto out;
30126 +
30127 +       args->file->f_path.dentry = DENTRY_NOT_SET;
30128 +       args->file->f_path.mnt = au_br_mnt(br);
30129 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30130 +                                    args->create_mode, args->opened);
30131 +       if (err >= 0) {
30132 +               /* some filesystems don't set FILE_CREATED while succeeded? */
30133 +               if (*args->opened & FILE_CREATED)
30134 +                       fsnotify_create(dir, dentry);
30135 +       } else
30136 +               goto out;
30137 +
30138 +
30139 +       if (!err) {
30140 +               /* todo: call VFS:may_open() here */
30141 +               err = open_check_o_direct(file);
30142 +               /* todo: ima_file_check() too? */
30143 +               if (!err && (args->open_flag & __FMODE_EXEC))
30144 +                       err = deny_write_access(file);
30145 +               if (unlikely(err))
30146 +                       /* note that the file is created and still opened */
30147 +                       goto out;
30148 +       }
30149 +
30150 +       atomic_inc(&br->br_count);
30151 +       fsnotify_open(file);
30152 +
30153 +out:
30154 +       return err;
30155 +}
30156 +
30157 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30158 +{
30159 +       int err;
30160 +
30161 +       err = kern_path(name, flags, path);
30162 +       if (!err && d_is_positive(path->dentry))
30163 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30164 +       return err;
30165 +}
30166 +
30167 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30168 +                                   int len)
30169 +{
30170 +       struct path path = {
30171 +               .mnt = NULL
30172 +       };
30173 +
30174 +       /* VFS checks it too, but by WARN_ON_ONCE() */
30175 +       IMustLock(d_inode(parent));
30176 +
30177 +       path.dentry = lookup_one_len(name, parent, len);
30178 +       if (IS_ERR(path.dentry))
30179 +               goto out;
30180 +       if (d_is_positive(path.dentry))
30181 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30182 +
30183 +out:
30184 +       AuTraceErrPtr(path.dentry);
30185 +       return path.dentry;
30186 +}
30187 +
30188 +void vfsub_call_lkup_one(void *args)
30189 +{
30190 +       struct vfsub_lkup_one_args *a = args;
30191 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30192 +}
30193 +
30194 +/* ---------------------------------------------------------------------- */
30195 +
30196 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30197 +                                struct dentry *d2, struct au_hinode *hdir2)
30198 +{
30199 +       struct dentry *d;
30200 +
30201 +       lockdep_off();
30202 +       d = lock_rename(d1, d2);
30203 +       lockdep_on();
30204 +       au_hn_suspend(hdir1);
30205 +       if (hdir1 != hdir2)
30206 +               au_hn_suspend(hdir2);
30207 +
30208 +       return d;
30209 +}
30210 +
30211 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30212 +                        struct dentry *d2, struct au_hinode *hdir2)
30213 +{
30214 +       au_hn_resume(hdir1);
30215 +       if (hdir1 != hdir2)
30216 +               au_hn_resume(hdir2);
30217 +       lockdep_off();
30218 +       unlock_rename(d1, d2);
30219 +       lockdep_on();
30220 +}
30221 +
30222 +/* ---------------------------------------------------------------------- */
30223 +
30224 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30225 +{
30226 +       int err;
30227 +       struct dentry *d;
30228 +
30229 +       IMustLock(dir);
30230 +
30231 +       d = path->dentry;
30232 +       path->dentry = d->d_parent;
30233 +       err = security_path_mknod(path, d, mode, 0);
30234 +       path->dentry = d;
30235 +       if (unlikely(err))
30236 +               goto out;
30237 +
30238 +       lockdep_off();
30239 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30240 +       lockdep_on();
30241 +       if (!err) {
30242 +               struct path tmp = *path;
30243 +               int did;
30244 +
30245 +               vfsub_update_h_iattr(&tmp, &did);
30246 +               if (did) {
30247 +                       tmp.dentry = path->dentry->d_parent;
30248 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30249 +               }
30250 +               /*ignore*/
30251 +       }
30252 +
30253 +out:
30254 +       return err;
30255 +}
30256 +
30257 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30258 +{
30259 +       int err;
30260 +       struct dentry *d;
30261 +
30262 +       IMustLock(dir);
30263 +
30264 +       d = path->dentry;
30265 +       path->dentry = d->d_parent;
30266 +       err = security_path_symlink(path, d, symname);
30267 +       path->dentry = d;
30268 +       if (unlikely(err))
30269 +               goto out;
30270 +
30271 +       lockdep_off();
30272 +       err = vfs_symlink(dir, path->dentry, symname);
30273 +       lockdep_on();
30274 +       if (!err) {
30275 +               struct path tmp = *path;
30276 +               int did;
30277 +
30278 +               vfsub_update_h_iattr(&tmp, &did);
30279 +               if (did) {
30280 +                       tmp.dentry = path->dentry->d_parent;
30281 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30282 +               }
30283 +               /*ignore*/
30284 +       }
30285 +
30286 +out:
30287 +       return err;
30288 +}
30289 +
30290 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30291 +{
30292 +       int err;
30293 +       struct dentry *d;
30294 +
30295 +       IMustLock(dir);
30296 +
30297 +       d = path->dentry;
30298 +       path->dentry = d->d_parent;
30299 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30300 +       path->dentry = d;
30301 +       if (unlikely(err))
30302 +               goto out;
30303 +
30304 +       lockdep_off();
30305 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30306 +       lockdep_on();
30307 +       if (!err) {
30308 +               struct path tmp = *path;
30309 +               int did;
30310 +
30311 +               vfsub_update_h_iattr(&tmp, &did);
30312 +               if (did) {
30313 +                       tmp.dentry = path->dentry->d_parent;
30314 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30315 +               }
30316 +               /*ignore*/
30317 +       }
30318 +
30319 +out:
30320 +       return err;
30321 +}
30322 +
30323 +static int au_test_nlink(struct inode *inode)
30324 +{
30325 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30326 +
30327 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30328 +           || inode->i_nlink < link_max)
30329 +               return 0;
30330 +       return -EMLINK;
30331 +}
30332 +
30333 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30334 +              struct inode **delegated_inode)
30335 +{
30336 +       int err;
30337 +       struct dentry *d;
30338 +
30339 +       IMustLock(dir);
30340 +
30341 +       err = au_test_nlink(d_inode(src_dentry));
30342 +       if (unlikely(err))
30343 +               return err;
30344 +
30345 +       /* we don't call may_linkat() */
30346 +       d = path->dentry;
30347 +       path->dentry = d->d_parent;
30348 +       err = security_path_link(src_dentry, path, d);
30349 +       path->dentry = d;
30350 +       if (unlikely(err))
30351 +               goto out;
30352 +
30353 +       lockdep_off();
30354 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30355 +       lockdep_on();
30356 +       if (!err) {
30357 +               struct path tmp = *path;
30358 +               int did;
30359 +
30360 +               /* fuse has different memory inode for the same inumber */
30361 +               vfsub_update_h_iattr(&tmp, &did);
30362 +               if (did) {
30363 +                       tmp.dentry = path->dentry->d_parent;
30364 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30365 +                       tmp.dentry = src_dentry;
30366 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30367 +               }
30368 +               /*ignore*/
30369 +       }
30370 +
30371 +out:
30372 +       return err;
30373 +}
30374 +
30375 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30376 +                struct inode *dir, struct path *path,
30377 +                struct inode **delegated_inode)
30378 +{
30379 +       int err;
30380 +       struct path tmp = {
30381 +               .mnt    = path->mnt
30382 +       };
30383 +       struct dentry *d;
30384 +
30385 +       IMustLock(dir);
30386 +       IMustLock(src_dir);
30387 +
30388 +       d = path->dentry;
30389 +       path->dentry = d->d_parent;
30390 +       tmp.dentry = src_dentry->d_parent;
30391 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30392 +       path->dentry = d;
30393 +       if (unlikely(err))
30394 +               goto out;
30395 +
30396 +       lockdep_off();
30397 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30398 +                        delegated_inode, /*flags*/0);
30399 +       lockdep_on();
30400 +       if (!err) {
30401 +               int did;
30402 +
30403 +               tmp.dentry = d->d_parent;
30404 +               vfsub_update_h_iattr(&tmp, &did);
30405 +               if (did) {
30406 +                       tmp.dentry = src_dentry;
30407 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30408 +                       tmp.dentry = src_dentry->d_parent;
30409 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30410 +               }
30411 +               /*ignore*/
30412 +       }
30413 +
30414 +out:
30415 +       return err;
30416 +}
30417 +
30418 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30419 +{
30420 +       int err;
30421 +       struct dentry *d;
30422 +
30423 +       IMustLock(dir);
30424 +
30425 +       d = path->dentry;
30426 +       path->dentry = d->d_parent;
30427 +       err = security_path_mkdir(path, d, mode);
30428 +       path->dentry = d;
30429 +       if (unlikely(err))
30430 +               goto out;
30431 +
30432 +       lockdep_off();
30433 +       err = vfs_mkdir(dir, path->dentry, mode);
30434 +       lockdep_on();
30435 +       if (!err) {
30436 +               struct path tmp = *path;
30437 +               int did;
30438 +
30439 +               vfsub_update_h_iattr(&tmp, &did);
30440 +               if (did) {
30441 +                       tmp.dentry = path->dentry->d_parent;
30442 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30443 +               }
30444 +               /*ignore*/
30445 +       }
30446 +
30447 +out:
30448 +       return err;
30449 +}
30450 +
30451 +int vfsub_rmdir(struct inode *dir, struct path *path)
30452 +{
30453 +       int err;
30454 +       struct dentry *d;
30455 +
30456 +       IMustLock(dir);
30457 +
30458 +       d = path->dentry;
30459 +       path->dentry = d->d_parent;
30460 +       err = security_path_rmdir(path, d);
30461 +       path->dentry = d;
30462 +       if (unlikely(err))
30463 +               goto out;
30464 +
30465 +       lockdep_off();
30466 +       err = vfs_rmdir(dir, path->dentry);
30467 +       lockdep_on();
30468 +       if (!err) {
30469 +               struct path tmp = {
30470 +                       .dentry = path->dentry->d_parent,
30471 +                       .mnt    = path->mnt
30472 +               };
30473 +
30474 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30475 +       }
30476 +
30477 +out:
30478 +       return err;
30479 +}
30480 +
30481 +/* ---------------------------------------------------------------------- */
30482 +
30483 +/* todo: support mmap_sem? */
30484 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30485 +                    loff_t *ppos)
30486 +{
30487 +       ssize_t err;
30488 +
30489 +       lockdep_off();
30490 +       err = vfs_read(file, ubuf, count, ppos);
30491 +       lockdep_on();
30492 +       if (err >= 0)
30493 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30494 +       return err;
30495 +}
30496 +
30497 +/* todo: kernel_read()? */
30498 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30499 +                    loff_t *ppos)
30500 +{
30501 +       ssize_t err;
30502 +       mm_segment_t oldfs;
30503 +       union {
30504 +               void *k;
30505 +               char __user *u;
30506 +       } buf;
30507 +
30508 +       buf.k = kbuf;
30509 +       oldfs = get_fs();
30510 +       set_fs(KERNEL_DS);
30511 +       err = vfsub_read_u(file, buf.u, count, ppos);
30512 +       set_fs(oldfs);
30513 +       return err;
30514 +}
30515 +
30516 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30517 +                     loff_t *ppos)
30518 +{
30519 +       ssize_t err;
30520 +
30521 +       lockdep_off();
30522 +       err = vfs_write(file, ubuf, count, ppos);
30523 +       lockdep_on();
30524 +       if (err >= 0)
30525 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30526 +       return err;
30527 +}
30528 +
30529 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30530 +{
30531 +       ssize_t err;
30532 +       mm_segment_t oldfs;
30533 +       union {
30534 +               void *k;
30535 +               const char __user *u;
30536 +       } buf;
30537 +
30538 +       buf.k = kbuf;
30539 +       oldfs = get_fs();
30540 +       set_fs(KERNEL_DS);
30541 +       err = vfsub_write_u(file, buf.u, count, ppos);
30542 +       set_fs(oldfs);
30543 +       return err;
30544 +}
30545 +
30546 +int vfsub_flush(struct file *file, fl_owner_t id)
30547 +{
30548 +       int err;
30549 +
30550 +       err = 0;
30551 +       if (file->f_op->flush) {
30552 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
30553 +                       err = file->f_op->flush(file, id);
30554 +               else {
30555 +                       lockdep_off();
30556 +                       err = file->f_op->flush(file, id);
30557 +                       lockdep_on();
30558 +               }
30559 +               if (!err)
30560 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30561 +               /*ignore*/
30562 +       }
30563 +       return err;
30564 +}
30565 +
30566 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
30567 +{
30568 +       int err;
30569 +
30570 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
30571 +
30572 +       lockdep_off();
30573 +       err = iterate_dir(file, ctx);
30574 +       lockdep_on();
30575 +       if (err >= 0)
30576 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30577 +       return err;
30578 +}
30579 +
30580 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30581 +                    struct pipe_inode_info *pipe, size_t len,
30582 +                    unsigned int flags)
30583 +{
30584 +       long err;
30585 +
30586 +       lockdep_off();
30587 +       err = do_splice_to(in, ppos, pipe, len, flags);
30588 +       lockdep_on();
30589 +       file_accessed(in);
30590 +       if (err >= 0)
30591 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30592 +       return err;
30593 +}
30594 +
30595 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30596 +                      loff_t *ppos, size_t len, unsigned int flags)
30597 +{
30598 +       long err;
30599 +
30600 +       lockdep_off();
30601 +       err = do_splice_from(pipe, out, ppos, len, flags);
30602 +       lockdep_on();
30603 +       if (err >= 0)
30604 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30605 +       return err;
30606 +}
30607 +
30608 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
30609 +{
30610 +       int err;
30611 +
30612 +       /* file can be NULL */
30613 +       lockdep_off();
30614 +       err = vfs_fsync(file, datasync);
30615 +       lockdep_on();
30616 +       if (!err) {
30617 +               if (!path) {
30618 +                       AuDebugOn(!file);
30619 +                       path = &file->f_path;
30620 +               }
30621 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30622 +       }
30623 +       return err;
30624 +}
30625 +
30626 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30627 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30628 +               struct file *h_file)
30629 +{
30630 +       int err;
30631 +       struct inode *h_inode;
30632 +       struct super_block *h_sb;
30633 +
30634 +       if (!h_file) {
30635 +               err = vfsub_truncate(h_path, length);
30636 +               goto out;
30637 +       }
30638 +
30639 +       h_inode = d_inode(h_path->dentry);
30640 +       h_sb = h_inode->i_sb;
30641 +       lockdep_off();
30642 +       sb_start_write(h_sb);
30643 +       lockdep_on();
30644 +       err = locks_verify_truncate(h_inode, h_file, length);
30645 +       if (!err)
30646 +               err = security_path_truncate(h_path);
30647 +       if (!err) {
30648 +               lockdep_off();
30649 +               err = do_truncate(h_path->dentry, length, attr, h_file);
30650 +               lockdep_on();
30651 +       }
30652 +       lockdep_off();
30653 +       sb_end_write(h_sb);
30654 +       lockdep_on();
30655 +
30656 +out:
30657 +       return err;
30658 +}
30659 +
30660 +/* ---------------------------------------------------------------------- */
30661 +
30662 +struct au_vfsub_mkdir_args {
30663 +       int *errp;
30664 +       struct inode *dir;
30665 +       struct path *path;
30666 +       int mode;
30667 +};
30668 +
30669 +static void au_call_vfsub_mkdir(void *args)
30670 +{
30671 +       struct au_vfsub_mkdir_args *a = args;
30672 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30673 +}
30674 +
30675 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30676 +{
30677 +       int err, do_sio, wkq_err;
30678 +
30679 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30680 +       if (!do_sio) {
30681 +               lockdep_off();
30682 +               err = vfsub_mkdir(dir, path, mode);
30683 +               lockdep_on();
30684 +       } else {
30685 +               struct au_vfsub_mkdir_args args = {
30686 +                       .errp   = &err,
30687 +                       .dir    = dir,
30688 +                       .path   = path,
30689 +                       .mode   = mode
30690 +               };
30691 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30692 +               if (unlikely(wkq_err))
30693 +                       err = wkq_err;
30694 +       }
30695 +
30696 +       return err;
30697 +}
30698 +
30699 +struct au_vfsub_rmdir_args {
30700 +       int *errp;
30701 +       struct inode *dir;
30702 +       struct path *path;
30703 +};
30704 +
30705 +static void au_call_vfsub_rmdir(void *args)
30706 +{
30707 +       struct au_vfsub_rmdir_args *a = args;
30708 +       *a->errp = vfsub_rmdir(a->dir, a->path);
30709 +}
30710 +
30711 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30712 +{
30713 +       int err, do_sio, wkq_err;
30714 +
30715 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30716 +       if (!do_sio) {
30717 +               lockdep_off();
30718 +               err = vfsub_rmdir(dir, path);
30719 +               lockdep_on();
30720 +       } else {
30721 +               struct au_vfsub_rmdir_args args = {
30722 +                       .errp   = &err,
30723 +                       .dir    = dir,
30724 +                       .path   = path
30725 +               };
30726 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30727 +               if (unlikely(wkq_err))
30728 +                       err = wkq_err;
30729 +       }
30730 +
30731 +       return err;
30732 +}
30733 +
30734 +/* ---------------------------------------------------------------------- */
30735 +
30736 +struct notify_change_args {
30737 +       int *errp;
30738 +       struct path *path;
30739 +       struct iattr *ia;
30740 +       struct inode **delegated_inode;
30741 +};
30742 +
30743 +static void call_notify_change(void *args)
30744 +{
30745 +       struct notify_change_args *a = args;
30746 +       struct inode *h_inode;
30747 +
30748 +       h_inode = d_inode(a->path->dentry);
30749 +       IMustLock(h_inode);
30750 +
30751 +       *a->errp = -EPERM;
30752 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
30753 +               lockdep_off();
30754 +               *a->errp = notify_change(a->path->dentry, a->ia,
30755 +                                        a->delegated_inode);
30756 +               lockdep_on();
30757 +               if (!*a->errp)
30758 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30759 +       }
30760 +       AuTraceErr(*a->errp);
30761 +}
30762 +
30763 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30764 +                       struct inode **delegated_inode)
30765 +{
30766 +       int err;
30767 +       struct notify_change_args args = {
30768 +               .errp                   = &err,
30769 +               .path                   = path,
30770 +               .ia                     = ia,
30771 +               .delegated_inode        = delegated_inode
30772 +       };
30773 +
30774 +       call_notify_change(&args);
30775 +
30776 +       return err;
30777 +}
30778 +
30779 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30780 +                           struct inode **delegated_inode)
30781 +{
30782 +       int err, wkq_err;
30783 +       struct notify_change_args args = {
30784 +               .errp                   = &err,
30785 +               .path                   = path,
30786 +               .ia                     = ia,
30787 +               .delegated_inode        = delegated_inode
30788 +       };
30789 +
30790 +       wkq_err = au_wkq_wait(call_notify_change, &args);
30791 +       if (unlikely(wkq_err))
30792 +               err = wkq_err;
30793 +
30794 +       return err;
30795 +}
30796 +
30797 +/* ---------------------------------------------------------------------- */
30798 +
30799 +struct unlink_args {
30800 +       int *errp;
30801 +       struct inode *dir;
30802 +       struct path *path;
30803 +       struct inode **delegated_inode;
30804 +};
30805 +
30806 +static void call_unlink(void *args)
30807 +{
30808 +       struct unlink_args *a = args;
30809 +       struct dentry *d = a->path->dentry;
30810 +       struct inode *h_inode;
30811 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
30812 +                                     && au_dcount(d) == 1);
30813 +
30814 +       IMustLock(a->dir);
30815 +
30816 +       a->path->dentry = d->d_parent;
30817 +       *a->errp = security_path_unlink(a->path, d);
30818 +       a->path->dentry = d;
30819 +       if (unlikely(*a->errp))
30820 +               return;
30821 +
30822 +       if (!stop_sillyrename)
30823 +               dget(d);
30824 +       h_inode = NULL;
30825 +       if (d_is_positive(d)) {
30826 +               h_inode = d_inode(d);
30827 +               ihold(h_inode);
30828 +       }
30829 +
30830 +       lockdep_off();
30831 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
30832 +       lockdep_on();
30833 +       if (!*a->errp) {
30834 +               struct path tmp = {
30835 +                       .dentry = d->d_parent,
30836 +                       .mnt    = a->path->mnt
30837 +               };
30838 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30839 +       }
30840 +
30841 +       if (!stop_sillyrename)
30842 +               dput(d);
30843 +       if (h_inode)
30844 +               iput(h_inode);
30845 +
30846 +       AuTraceErr(*a->errp);
30847 +}
30848 +
30849 +/*
30850 + * @dir: must be locked.
30851 + * @dentry: target dentry.
30852 + */
30853 +int vfsub_unlink(struct inode *dir, struct path *path,
30854 +                struct inode **delegated_inode, int force)
30855 +{
30856 +       int err;
30857 +       struct unlink_args args = {
30858 +               .errp                   = &err,
30859 +               .dir                    = dir,
30860 +               .path                   = path,
30861 +               .delegated_inode        = delegated_inode
30862 +       };
30863 +
30864 +       if (!force)
30865 +               call_unlink(&args);
30866 +       else {
30867 +               int wkq_err;
30868 +
30869 +               wkq_err = au_wkq_wait(call_unlink, &args);
30870 +               if (unlikely(wkq_err))
30871 +                       err = wkq_err;
30872 +       }
30873 +
30874 +       return err;
30875 +}
30876 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
30877 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
30878 +++ linux/fs/aufs/vfsub.h       2016-01-13 20:11:11.673093671 +0100
30879 @@ -0,0 +1,287 @@
30880 +/*
30881 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30882 + *
30883 + * This program, aufs is free software; you can redistribute it and/or modify
30884 + * it under the terms of the GNU General Public License as published by
30885 + * the Free Software Foundation; either version 2 of the License, or
30886 + * (at your option) any later version.
30887 + *
30888 + * This program is distributed in the hope that it will be useful,
30889 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30890 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30891 + * GNU General Public License for more details.
30892 + *
30893 + * You should have received a copy of the GNU General Public License
30894 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30895 + */
30896 +
30897 +/*
30898 + * sub-routines for VFS
30899 + */
30900 +
30901 +#ifndef __AUFS_VFSUB_H__
30902 +#define __AUFS_VFSUB_H__
30903 +
30904 +#ifdef __KERNEL__
30905 +
30906 +#include <linux/fs.h>
30907 +#include <linux/mount.h>
30908 +#include <linux/xattr.h>
30909 +#include "debug.h"
30910 +
30911 +/* copied from linux/fs/internal.h */
30912 +/* todo: BAD approach!! */
30913 +extern void __mnt_drop_write(struct vfsmount *);
30914 +extern int open_check_o_direct(struct file *f);
30915 +
30916 +/* ---------------------------------------------------------------------- */
30917 +
30918 +/* lock subclass for lower inode */
30919 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
30920 +/* reduce? gave up. */
30921 +enum {
30922 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
30923 +       AuLsc_I_PARENT,         /* lower inode, parent first */
30924 +       AuLsc_I_PARENT2,        /* copyup dirs */
30925 +       AuLsc_I_PARENT3,        /* copyup wh */
30926 +       AuLsc_I_CHILD,
30927 +       AuLsc_I_CHILD2,
30928 +       AuLsc_I_End
30929 +};
30930 +
30931 +/* to debug easier, do not make them inlined functions */
30932 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
30933 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
30934 +
30935 +/* ---------------------------------------------------------------------- */
30936 +
30937 +static inline void vfsub_drop_nlink(struct inode *inode)
30938 +{
30939 +       AuDebugOn(!inode->i_nlink);
30940 +       drop_nlink(inode);
30941 +}
30942 +
30943 +static inline void vfsub_dead_dir(struct inode *inode)
30944 +{
30945 +       AuDebugOn(!S_ISDIR(inode->i_mode));
30946 +       inode->i_flags |= S_DEAD;
30947 +       clear_nlink(inode);
30948 +}
30949 +
30950 +static inline int vfsub_native_ro(struct inode *inode)
30951 +{
30952 +       return (inode->i_sb->s_flags & MS_RDONLY)
30953 +               || IS_RDONLY(inode)
30954 +               /* || IS_APPEND(inode) */
30955 +               || IS_IMMUTABLE(inode);
30956 +}
30957 +
30958 +/* ---------------------------------------------------------------------- */
30959 +
30960 +int vfsub_update_h_iattr(struct path *h_path, int *did);
30961 +struct file *vfsub_dentry_open(struct path *path, int flags);
30962 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
30963 +struct vfsub_aopen_args {
30964 +       struct file     *file;
30965 +       unsigned int    open_flag;
30966 +       umode_t         create_mode;
30967 +       int             *opened;
30968 +};
30969 +struct au_branch;
30970 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30971 +                     struct vfsub_aopen_args *args, struct au_branch *br);
30972 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
30973 +
30974 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30975 +                                   int len);
30976 +
30977 +struct vfsub_lkup_one_args {
30978 +       struct dentry **errp;
30979 +       struct qstr *name;
30980 +       struct dentry *parent;
30981 +};
30982 +
30983 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
30984 +                                           struct dentry *parent)
30985 +{
30986 +       return vfsub_lookup_one_len(name->name, parent, name->len);
30987 +}
30988 +
30989 +void vfsub_call_lkup_one(void *args);
30990 +
30991 +/* ---------------------------------------------------------------------- */
30992 +
30993 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
30994 +{
30995 +       int err;
30996 +
30997 +       lockdep_off();
30998 +       err = mnt_want_write(mnt);
30999 +       lockdep_on();
31000 +       return err;
31001 +}
31002 +
31003 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31004 +{
31005 +       lockdep_off();
31006 +       mnt_drop_write(mnt);
31007 +       lockdep_on();
31008 +}
31009 +
31010 +#if 0 /* reserved */
31011 +static inline void vfsub_mnt_drop_write_file(struct file *file)
31012 +{
31013 +       lockdep_off();
31014 +       mnt_drop_write_file(file);
31015 +       lockdep_on();
31016 +}
31017 +#endif
31018 +
31019 +/* ---------------------------------------------------------------------- */
31020 +
31021 +struct au_hinode;
31022 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31023 +                                struct dentry *d2, struct au_hinode *hdir2);
31024 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31025 +                        struct dentry *d2, struct au_hinode *hdir2);
31026 +
31027 +int vfsub_create(struct inode *dir, struct path *path, int mode,
31028 +                bool want_excl);
31029 +int vfsub_symlink(struct inode *dir, struct path *path,
31030 +                 const char *symname);
31031 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31032 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
31033 +              struct path *path, struct inode **delegated_inode);
31034 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
31035 +                struct inode *hdir, struct path *path,
31036 +                struct inode **delegated_inode);
31037 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31038 +int vfsub_rmdir(struct inode *dir, struct path *path);
31039 +
31040 +/* ---------------------------------------------------------------------- */
31041 +
31042 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31043 +                    loff_t *ppos);
31044 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31045 +                       loff_t *ppos);
31046 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31047 +                     loff_t *ppos);
31048 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31049 +                     loff_t *ppos);
31050 +int vfsub_flush(struct file *file, fl_owner_t id);
31051 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31052 +
31053 +static inline loff_t vfsub_f_size_read(struct file *file)
31054 +{
31055 +       return i_size_read(file_inode(file));
31056 +}
31057 +
31058 +static inline unsigned int vfsub_file_flags(struct file *file)
31059 +{
31060 +       unsigned int flags;
31061 +
31062 +       spin_lock(&file->f_lock);
31063 +       flags = file->f_flags;
31064 +       spin_unlock(&file->f_lock);
31065 +
31066 +       return flags;
31067 +}
31068 +
31069 +#if 0 /* reserved */
31070 +static inline void vfsub_file_accessed(struct file *h_file)
31071 +{
31072 +       file_accessed(h_file);
31073 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31074 +}
31075 +#endif
31076 +
31077 +#if 0 /* reserved */
31078 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31079 +                                    struct dentry *h_dentry)
31080 +{
31081 +       struct path h_path = {
31082 +               .dentry = h_dentry,
31083 +               .mnt    = h_mnt
31084 +       };
31085 +       touch_atime(&h_path);
31086 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31087 +}
31088 +#endif
31089 +
31090 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31091 +                                   int flags)
31092 +{
31093 +       return generic_update_time(h_inode, ts, flags);
31094 +       /* no vfsub_update_h_iattr() since we don't have struct path */
31095 +}
31096 +
31097 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31098 +                    struct pipe_inode_info *pipe, size_t len,
31099 +                    unsigned int flags);
31100 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31101 +                      loff_t *ppos, size_t len, unsigned int flags);
31102 +
31103 +static inline long vfsub_truncate(struct path *path, loff_t length)
31104 +{
31105 +       long err;
31106 +
31107 +       lockdep_off();
31108 +       err = vfs_truncate(path, length);
31109 +       lockdep_on();
31110 +       return err;
31111 +}
31112 +
31113 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31114 +               struct file *h_file);
31115 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
31116 +
31117 +/* ---------------------------------------------------------------------- */
31118 +
31119 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31120 +{
31121 +       loff_t err;
31122 +
31123 +       lockdep_off();
31124 +       err = vfs_llseek(file, offset, origin);
31125 +       lockdep_on();
31126 +       return err;
31127 +}
31128 +
31129 +/* ---------------------------------------------------------------------- */
31130 +
31131 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31132 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
31133 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31134 +                           struct inode **delegated_inode);
31135 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31136 +                       struct inode **delegated_inode);
31137 +int vfsub_unlink(struct inode *dir, struct path *path,
31138 +                struct inode **delegated_inode, int force);
31139 +
31140 +/* ---------------------------------------------------------------------- */
31141 +
31142 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31143 +                                const void *value, size_t size, int flags)
31144 +{
31145 +       int err;
31146 +
31147 +       lockdep_off();
31148 +       err = vfs_setxattr(dentry, name, value, size, flags);
31149 +       lockdep_on();
31150 +
31151 +       return err;
31152 +}
31153 +
31154 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31155 +{
31156 +       int err;
31157 +
31158 +       lockdep_off();
31159 +       err = vfs_removexattr(dentry, name);
31160 +       lockdep_on();
31161 +
31162 +       return err;
31163 +}
31164 +
31165 +#endif /* __KERNEL__ */
31166 +#endif /* __AUFS_VFSUB_H__ */
31167 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31168 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
31169 +++ linux/fs/aufs/wbr_policy.c  2016-01-13 20:11:11.673093671 +0100
31170 @@ -0,0 +1,765 @@
31171 +/*
31172 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31173 + *
31174 + * This program, aufs is free software; you can redistribute it and/or modify
31175 + * it under the terms of the GNU General Public License as published by
31176 + * the Free Software Foundation; either version 2 of the License, or
31177 + * (at your option) any later version.
31178 + *
31179 + * This program is distributed in the hope that it will be useful,
31180 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31181 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31182 + * GNU General Public License for more details.
31183 + *
31184 + * You should have received a copy of the GNU General Public License
31185 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31186 + */
31187 +
31188 +/*
31189 + * policies for selecting one among multiple writable branches
31190 + */
31191 +
31192 +#include <linux/statfs.h>
31193 +#include "aufs.h"
31194 +
31195 +/* subset of cpup_attr() */
31196 +static noinline_for_stack
31197 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31198 +{
31199 +       int err, sbits;
31200 +       struct iattr ia;
31201 +       struct inode *h_isrc;
31202 +
31203 +       h_isrc = d_inode(h_src);
31204 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31205 +       ia.ia_mode = h_isrc->i_mode;
31206 +       ia.ia_uid = h_isrc->i_uid;
31207 +       ia.ia_gid = h_isrc->i_gid;
31208 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31209 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
31210 +       /* no delegation since it is just created */
31211 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31212 +
31213 +       /* is this nfs only? */
31214 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31215 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31216 +               ia.ia_mode = h_isrc->i_mode;
31217 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31218 +       }
31219 +
31220 +       return err;
31221 +}
31222 +
31223 +#define AuCpdown_PARENT_OPQ    1
31224 +#define AuCpdown_WHED          (1 << 1)
31225 +#define AuCpdown_MADE_DIR      (1 << 2)
31226 +#define AuCpdown_DIROPQ                (1 << 3)
31227 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31228 +#define au_fset_cpdown(flags, name) \
31229 +       do { (flags) |= AuCpdown_##name; } while (0)
31230 +#define au_fclr_cpdown(flags, name) \
31231 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31232 +
31233 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31234 +                            unsigned int *flags)
31235 +{
31236 +       int err;
31237 +       struct dentry *opq_dentry;
31238 +
31239 +       opq_dentry = au_diropq_create(dentry, bdst);
31240 +       err = PTR_ERR(opq_dentry);
31241 +       if (IS_ERR(opq_dentry))
31242 +               goto out;
31243 +       dput(opq_dentry);
31244 +       au_fset_cpdown(*flags, DIROPQ);
31245 +
31246 +out:
31247 +       return err;
31248 +}
31249 +
31250 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31251 +                           struct inode *dir, aufs_bindex_t bdst)
31252 +{
31253 +       int err;
31254 +       struct path h_path;
31255 +       struct au_branch *br;
31256 +
31257 +       br = au_sbr(dentry->d_sb, bdst);
31258 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31259 +       err = PTR_ERR(h_path.dentry);
31260 +       if (IS_ERR(h_path.dentry))
31261 +               goto out;
31262 +
31263 +       err = 0;
31264 +       if (d_is_positive(h_path.dentry)) {
31265 +               h_path.mnt = au_br_mnt(br);
31266 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31267 +                                         dentry);
31268 +       }
31269 +       dput(h_path.dentry);
31270 +
31271 +out:
31272 +       return err;
31273 +}
31274 +
31275 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31276 +                        struct au_pin *pin,
31277 +                        struct dentry *h_parent, void *arg)
31278 +{
31279 +       int err, rerr;
31280 +       aufs_bindex_t bopq, bstart;
31281 +       struct path h_path;
31282 +       struct dentry *parent;
31283 +       struct inode *h_dir, *h_inode, *inode, *dir;
31284 +       unsigned int *flags = arg;
31285 +
31286 +       bstart = au_dbstart(dentry);
31287 +       /* dentry is di-locked */
31288 +       parent = dget_parent(dentry);
31289 +       dir = d_inode(parent);
31290 +       h_dir = d_inode(h_parent);
31291 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31292 +       IMustLock(h_dir);
31293 +
31294 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31295 +       if (unlikely(err < 0))
31296 +               goto out;
31297 +       h_path.dentry = au_h_dptr(dentry, bdst);
31298 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31299 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31300 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31301 +       if (unlikely(err))
31302 +               goto out_put;
31303 +       au_fset_cpdown(*flags, MADE_DIR);
31304 +
31305 +       bopq = au_dbdiropq(dentry);
31306 +       au_fclr_cpdown(*flags, WHED);
31307 +       au_fclr_cpdown(*flags, DIROPQ);
31308 +       if (au_dbwh(dentry) == bdst)
31309 +               au_fset_cpdown(*flags, WHED);
31310 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31311 +               au_fset_cpdown(*flags, PARENT_OPQ);
31312 +       h_inode = d_inode(h_path.dentry);
31313 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31314 +       if (au_ftest_cpdown(*flags, WHED)) {
31315 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31316 +               if (unlikely(err)) {
31317 +                       mutex_unlock(&h_inode->i_mutex);
31318 +                       goto out_dir;
31319 +               }
31320 +       }
31321 +
31322 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
31323 +       mutex_unlock(&h_inode->i_mutex);
31324 +       if (unlikely(err))
31325 +               goto out_opq;
31326 +
31327 +       if (au_ftest_cpdown(*flags, WHED)) {
31328 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31329 +               if (unlikely(err))
31330 +                       goto out_opq;
31331 +       }
31332 +
31333 +       inode = d_inode(dentry);
31334 +       if (au_ibend(inode) < bdst)
31335 +               au_set_ibend(inode, bdst);
31336 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31337 +                     au_hi_flags(inode, /*isdir*/1));
31338 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
31339 +       goto out; /* success */
31340 +
31341 +       /* revert */
31342 +out_opq:
31343 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
31344 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31345 +               rerr = au_diropq_remove(dentry, bdst);
31346 +               mutex_unlock(&h_inode->i_mutex);
31347 +               if (unlikely(rerr)) {
31348 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31349 +                               dentry, bdst, rerr);
31350 +                       err = -EIO;
31351 +                       goto out;
31352 +               }
31353 +       }
31354 +out_dir:
31355 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
31356 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31357 +               if (unlikely(rerr)) {
31358 +                       AuIOErr("failed removing %pd b%d (%d)\n",
31359 +                               dentry, bdst, rerr);
31360 +                       err = -EIO;
31361 +               }
31362 +       }
31363 +out_put:
31364 +       au_set_h_dptr(dentry, bdst, NULL);
31365 +       if (au_dbend(dentry) == bdst)
31366 +               au_update_dbend(dentry);
31367 +out:
31368 +       dput(parent);
31369 +       return err;
31370 +}
31371 +
31372 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31373 +{
31374 +       int err;
31375 +       unsigned int flags;
31376 +
31377 +       flags = 0;
31378 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
31379 +
31380 +       return err;
31381 +}
31382 +
31383 +/* ---------------------------------------------------------------------- */
31384 +
31385 +/* policies for create */
31386 +
31387 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
31388 +{
31389 +       int err, i, j, ndentry;
31390 +       aufs_bindex_t bopq;
31391 +       struct au_dcsub_pages dpages;
31392 +       struct au_dpage *dpage;
31393 +       struct dentry **dentries, *parent, *d;
31394 +
31395 +       err = au_dpages_init(&dpages, GFP_NOFS);
31396 +       if (unlikely(err))
31397 +               goto out;
31398 +       parent = dget_parent(dentry);
31399 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
31400 +       if (unlikely(err))
31401 +               goto out_free;
31402 +
31403 +       err = bindex;
31404 +       for (i = 0; i < dpages.ndpage; i++) {
31405 +               dpage = dpages.dpages + i;
31406 +               dentries = dpage->dentries;
31407 +               ndentry = dpage->ndentry;
31408 +               for (j = 0; j < ndentry; j++) {
31409 +                       d = dentries[j];
31410 +                       di_read_lock_parent2(d, !AuLock_IR);
31411 +                       bopq = au_dbdiropq(d);
31412 +                       di_read_unlock(d, !AuLock_IR);
31413 +                       if (bopq >= 0 && bopq < err)
31414 +                               err = bopq;
31415 +               }
31416 +       }
31417 +
31418 +out_free:
31419 +       dput(parent);
31420 +       au_dpages_free(&dpages);
31421 +out:
31422 +       return err;
31423 +}
31424 +
31425 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31426 +{
31427 +       for (; bindex >= 0; bindex--)
31428 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
31429 +                       return bindex;
31430 +       return -EROFS;
31431 +}
31432 +
31433 +/* top down parent */
31434 +static int au_wbr_create_tdp(struct dentry *dentry,
31435 +                            unsigned int flags __maybe_unused)
31436 +{
31437 +       int err;
31438 +       aufs_bindex_t bstart, bindex;
31439 +       struct super_block *sb;
31440 +       struct dentry *parent, *h_parent;
31441 +
31442 +       sb = dentry->d_sb;
31443 +       bstart = au_dbstart(dentry);
31444 +       err = bstart;
31445 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
31446 +               goto out;
31447 +
31448 +       err = -EROFS;
31449 +       parent = dget_parent(dentry);
31450 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
31451 +               h_parent = au_h_dptr(parent, bindex);
31452 +               if (!h_parent || d_is_negative(h_parent))
31453 +                       continue;
31454 +
31455 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31456 +                       err = bindex;
31457 +                       break;
31458 +               }
31459 +       }
31460 +       dput(parent);
31461 +
31462 +       /* bottom up here */
31463 +       if (unlikely(err < 0)) {
31464 +               err = au_wbr_bu(sb, bstart - 1);
31465 +               if (err >= 0)
31466 +                       err = au_wbr_nonopq(dentry, err);
31467 +       }
31468 +
31469 +out:
31470 +       AuDbg("b%d\n", err);
31471 +       return err;
31472 +}
31473 +
31474 +/* ---------------------------------------------------------------------- */
31475 +
31476 +/* an exception for the policy other than tdp */
31477 +static int au_wbr_create_exp(struct dentry *dentry)
31478 +{
31479 +       int err;
31480 +       aufs_bindex_t bwh, bdiropq;
31481 +       struct dentry *parent;
31482 +
31483 +       err = -1;
31484 +       bwh = au_dbwh(dentry);
31485 +       parent = dget_parent(dentry);
31486 +       bdiropq = au_dbdiropq(parent);
31487 +       if (bwh >= 0) {
31488 +               if (bdiropq >= 0)
31489 +                       err = min(bdiropq, bwh);
31490 +               else
31491 +                       err = bwh;
31492 +               AuDbg("%d\n", err);
31493 +       } else if (bdiropq >= 0) {
31494 +               err = bdiropq;
31495 +               AuDbg("%d\n", err);
31496 +       }
31497 +       dput(parent);
31498 +
31499 +       if (err >= 0)
31500 +               err = au_wbr_nonopq(dentry, err);
31501 +
31502 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31503 +               err = -1;
31504 +
31505 +       AuDbg("%d\n", err);
31506 +       return err;
31507 +}
31508 +
31509 +/* ---------------------------------------------------------------------- */
31510 +
31511 +/* round robin */
31512 +static int au_wbr_create_init_rr(struct super_block *sb)
31513 +{
31514 +       int err;
31515 +
31516 +       err = au_wbr_bu(sb, au_sbend(sb));
31517 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
31518 +       /* smp_mb(); */
31519 +
31520 +       AuDbg("b%d\n", err);
31521 +       return err;
31522 +}
31523 +
31524 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
31525 +{
31526 +       int err, nbr;
31527 +       unsigned int u;
31528 +       aufs_bindex_t bindex, bend;
31529 +       struct super_block *sb;
31530 +       atomic_t *next;
31531 +
31532 +       err = au_wbr_create_exp(dentry);
31533 +       if (err >= 0)
31534 +               goto out;
31535 +
31536 +       sb = dentry->d_sb;
31537 +       next = &au_sbi(sb)->si_wbr_rr_next;
31538 +       bend = au_sbend(sb);
31539 +       nbr = bend + 1;
31540 +       for (bindex = 0; bindex <= bend; bindex++) {
31541 +               if (!au_ftest_wbr(flags, DIR)) {
31542 +                       err = atomic_dec_return(next) + 1;
31543 +                       /* modulo for 0 is meaningless */
31544 +                       if (unlikely(!err))
31545 +                               err = atomic_dec_return(next) + 1;
31546 +               } else
31547 +                       err = atomic_read(next);
31548 +               AuDbg("%d\n", err);
31549 +               u = err;
31550 +               err = u % nbr;
31551 +               AuDbg("%d\n", err);
31552 +               if (!au_br_rdonly(au_sbr(sb, err)))
31553 +                       break;
31554 +               err = -EROFS;
31555 +       }
31556 +
31557 +       if (err >= 0)
31558 +               err = au_wbr_nonopq(dentry, err);
31559 +
31560 +out:
31561 +       AuDbg("%d\n", err);
31562 +       return err;
31563 +}
31564 +
31565 +/* ---------------------------------------------------------------------- */
31566 +
31567 +/* most free space */
31568 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
31569 +{
31570 +       struct super_block *sb;
31571 +       struct au_branch *br;
31572 +       struct au_wbr_mfs *mfs;
31573 +       struct dentry *h_parent;
31574 +       aufs_bindex_t bindex, bend;
31575 +       int err;
31576 +       unsigned long long b, bavail;
31577 +       struct path h_path;
31578 +       /* reduce the stack usage */
31579 +       struct kstatfs *st;
31580 +
31581 +       st = kmalloc(sizeof(*st), GFP_NOFS);
31582 +       if (unlikely(!st)) {
31583 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31584 +               return;
31585 +       }
31586 +
31587 +       bavail = 0;
31588 +       sb = dentry->d_sb;
31589 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31590 +       MtxMustLock(&mfs->mfs_lock);
31591 +       mfs->mfs_bindex = -EROFS;
31592 +       mfs->mfsrr_bytes = 0;
31593 +       if (!parent) {
31594 +               bindex = 0;
31595 +               bend = au_sbend(sb);
31596 +       } else {
31597 +               bindex = au_dbstart(parent);
31598 +               bend = au_dbtaildir(parent);
31599 +       }
31600 +
31601 +       for (; bindex <= bend; bindex++) {
31602 +               if (parent) {
31603 +                       h_parent = au_h_dptr(parent, bindex);
31604 +                       if (!h_parent || d_is_negative(h_parent))
31605 +                               continue;
31606 +               }
31607 +               br = au_sbr(sb, bindex);
31608 +               if (au_br_rdonly(br))
31609 +                       continue;
31610 +
31611 +               /* sb->s_root for NFS is unreliable */
31612 +               h_path.mnt = au_br_mnt(br);
31613 +               h_path.dentry = h_path.mnt->mnt_root;
31614 +               err = vfs_statfs(&h_path, st);
31615 +               if (unlikely(err)) {
31616 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
31617 +                       continue;
31618 +               }
31619 +
31620 +               /* when the available size is equal, select the lower one */
31621 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
31622 +                            || sizeof(b) < sizeof(st->f_bsize));
31623 +               b = st->f_bavail * st->f_bsize;
31624 +               br->br_wbr->wbr_bytes = b;
31625 +               if (b >= bavail) {
31626 +                       bavail = b;
31627 +                       mfs->mfs_bindex = bindex;
31628 +                       mfs->mfs_jiffy = jiffies;
31629 +               }
31630 +       }
31631 +
31632 +       mfs->mfsrr_bytes = bavail;
31633 +       AuDbg("b%d\n", mfs->mfs_bindex);
31634 +       kfree(st);
31635 +}
31636 +
31637 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
31638 +{
31639 +       int err;
31640 +       struct dentry *parent;
31641 +       struct super_block *sb;
31642 +       struct au_wbr_mfs *mfs;
31643 +
31644 +       err = au_wbr_create_exp(dentry);
31645 +       if (err >= 0)
31646 +               goto out;
31647 +
31648 +       sb = dentry->d_sb;
31649 +       parent = NULL;
31650 +       if (au_ftest_wbr(flags, PARENT))
31651 +               parent = dget_parent(dentry);
31652 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31653 +       mutex_lock(&mfs->mfs_lock);
31654 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
31655 +           || mfs->mfs_bindex < 0
31656 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
31657 +               au_mfs(dentry, parent);
31658 +       mutex_unlock(&mfs->mfs_lock);
31659 +       err = mfs->mfs_bindex;
31660 +       dput(parent);
31661 +
31662 +       if (err >= 0)
31663 +               err = au_wbr_nonopq(dentry, err);
31664 +
31665 +out:
31666 +       AuDbg("b%d\n", err);
31667 +       return err;
31668 +}
31669 +
31670 +static int au_wbr_create_init_mfs(struct super_block *sb)
31671 +{
31672 +       struct au_wbr_mfs *mfs;
31673 +
31674 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31675 +       mutex_init(&mfs->mfs_lock);
31676 +       mfs->mfs_jiffy = 0;
31677 +       mfs->mfs_bindex = -EROFS;
31678 +
31679 +       return 0;
31680 +}
31681 +
31682 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31683 +{
31684 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31685 +       return 0;
31686 +}
31687 +
31688 +/* ---------------------------------------------------------------------- */
31689 +
31690 +/* most free space and then round robin */
31691 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
31692 +{
31693 +       int err;
31694 +       struct au_wbr_mfs *mfs;
31695 +
31696 +       err = au_wbr_create_mfs(dentry, flags);
31697 +       if (err >= 0) {
31698 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
31699 +               mutex_lock(&mfs->mfs_lock);
31700 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
31701 +                       err = au_wbr_create_rr(dentry, flags);
31702 +               mutex_unlock(&mfs->mfs_lock);
31703 +       }
31704 +
31705 +       AuDbg("b%d\n", err);
31706 +       return err;
31707 +}
31708 +
31709 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
31710 +{
31711 +       int err;
31712 +
31713 +       au_wbr_create_init_mfs(sb); /* ignore */
31714 +       err = au_wbr_create_init_rr(sb);
31715 +
31716 +       return err;
31717 +}
31718 +
31719 +/* ---------------------------------------------------------------------- */
31720 +
31721 +/* top down parent and most free space */
31722 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
31723 +{
31724 +       int err, e2;
31725 +       unsigned long long b;
31726 +       aufs_bindex_t bindex, bstart, bend;
31727 +       struct super_block *sb;
31728 +       struct dentry *parent, *h_parent;
31729 +       struct au_branch *br;
31730 +
31731 +       err = au_wbr_create_tdp(dentry, flags);
31732 +       if (unlikely(err < 0))
31733 +               goto out;
31734 +       parent = dget_parent(dentry);
31735 +       bstart = au_dbstart(parent);
31736 +       bend = au_dbtaildir(parent);
31737 +       if (bstart == bend)
31738 +               goto out_parent; /* success */
31739 +
31740 +       e2 = au_wbr_create_mfs(dentry, flags);
31741 +       if (e2 < 0)
31742 +               goto out_parent; /* success */
31743 +
31744 +       /* when the available size is equal, select upper one */
31745 +       sb = dentry->d_sb;
31746 +       br = au_sbr(sb, err);
31747 +       b = br->br_wbr->wbr_bytes;
31748 +       AuDbg("b%d, %llu\n", err, b);
31749 +
31750 +       for (bindex = bstart; bindex <= bend; bindex++) {
31751 +               h_parent = au_h_dptr(parent, bindex);
31752 +               if (!h_parent || d_is_negative(h_parent))
31753 +                       continue;
31754 +
31755 +               br = au_sbr(sb, bindex);
31756 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31757 +                       b = br->br_wbr->wbr_bytes;
31758 +                       err = bindex;
31759 +                       AuDbg("b%d, %llu\n", err, b);
31760 +               }
31761 +       }
31762 +
31763 +       if (err >= 0)
31764 +               err = au_wbr_nonopq(dentry, err);
31765 +
31766 +out_parent:
31767 +       dput(parent);
31768 +out:
31769 +       AuDbg("b%d\n", err);
31770 +       return err;
31771 +}
31772 +
31773 +/* ---------------------------------------------------------------------- */
31774 +
31775 +/*
31776 + * - top down parent
31777 + * - most free space with parent
31778 + * - most free space round-robin regardless parent
31779 + */
31780 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31781 +{
31782 +       int err;
31783 +       unsigned long long watermark;
31784 +       struct super_block *sb;
31785 +       struct au_branch *br;
31786 +       struct au_wbr_mfs *mfs;
31787 +
31788 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31789 +       if (unlikely(err < 0))
31790 +               goto out;
31791 +
31792 +       sb = dentry->d_sb;
31793 +       br = au_sbr(sb, err);
31794 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31795 +       mutex_lock(&mfs->mfs_lock);
31796 +       watermark = mfs->mfsrr_watermark;
31797 +       mutex_unlock(&mfs->mfs_lock);
31798 +       if (br->br_wbr->wbr_bytes < watermark)
31799 +               /* regardless the parent dir */
31800 +               err = au_wbr_create_mfsrr(dentry, flags);
31801 +
31802 +out:
31803 +       AuDbg("b%d\n", err);
31804 +       return err;
31805 +}
31806 +
31807 +/* ---------------------------------------------------------------------- */
31808 +
31809 +/* policies for copyup */
31810 +
31811 +/* top down parent */
31812 +static int au_wbr_copyup_tdp(struct dentry *dentry)
31813 +{
31814 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
31815 +}
31816 +
31817 +/* bottom up parent */
31818 +static int au_wbr_copyup_bup(struct dentry *dentry)
31819 +{
31820 +       int err;
31821 +       aufs_bindex_t bindex, bstart;
31822 +       struct dentry *parent, *h_parent;
31823 +       struct super_block *sb;
31824 +
31825 +       err = -EROFS;
31826 +       sb = dentry->d_sb;
31827 +       parent = dget_parent(dentry);
31828 +       bstart = au_dbstart(parent);
31829 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
31830 +               h_parent = au_h_dptr(parent, bindex);
31831 +               if (!h_parent || d_is_negative(h_parent))
31832 +                       continue;
31833 +
31834 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31835 +                       err = bindex;
31836 +                       break;
31837 +               }
31838 +       }
31839 +       dput(parent);
31840 +
31841 +       /* bottom up here */
31842 +       if (unlikely(err < 0))
31843 +               err = au_wbr_bu(sb, bstart - 1);
31844 +
31845 +       AuDbg("b%d\n", err);
31846 +       return err;
31847 +}
31848 +
31849 +/* bottom up */
31850 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
31851 +{
31852 +       int err;
31853 +
31854 +       err = au_wbr_bu(dentry->d_sb, bstart);
31855 +       AuDbg("b%d\n", err);
31856 +       if (err > bstart)
31857 +               err = au_wbr_nonopq(dentry, err);
31858 +
31859 +       AuDbg("b%d\n", err);
31860 +       return err;
31861 +}
31862 +
31863 +static int au_wbr_copyup_bu(struct dentry *dentry)
31864 +{
31865 +       int err;
31866 +       aufs_bindex_t bstart;
31867 +
31868 +       bstart = au_dbstart(dentry);
31869 +       err = au_wbr_do_copyup_bu(dentry, bstart);
31870 +       return err;
31871 +}
31872 +
31873 +/* ---------------------------------------------------------------------- */
31874 +
31875 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
31876 +       [AuWbrCopyup_TDP] = {
31877 +               .copyup = au_wbr_copyup_tdp
31878 +       },
31879 +       [AuWbrCopyup_BUP] = {
31880 +               .copyup = au_wbr_copyup_bup
31881 +       },
31882 +       [AuWbrCopyup_BU] = {
31883 +               .copyup = au_wbr_copyup_bu
31884 +       }
31885 +};
31886 +
31887 +struct au_wbr_create_operations au_wbr_create_ops[] = {
31888 +       [AuWbrCreate_TDP] = {
31889 +               .create = au_wbr_create_tdp
31890 +       },
31891 +       [AuWbrCreate_RR] = {
31892 +               .create = au_wbr_create_rr,
31893 +               .init   = au_wbr_create_init_rr
31894 +       },
31895 +       [AuWbrCreate_MFS] = {
31896 +               .create = au_wbr_create_mfs,
31897 +               .init   = au_wbr_create_init_mfs,
31898 +               .fin    = au_wbr_create_fin_mfs
31899 +       },
31900 +       [AuWbrCreate_MFSV] = {
31901 +               .create = au_wbr_create_mfs,
31902 +               .init   = au_wbr_create_init_mfs,
31903 +               .fin    = au_wbr_create_fin_mfs
31904 +       },
31905 +       [AuWbrCreate_MFSRR] = {
31906 +               .create = au_wbr_create_mfsrr,
31907 +               .init   = au_wbr_create_init_mfsrr,
31908 +               .fin    = au_wbr_create_fin_mfs
31909 +       },
31910 +       [AuWbrCreate_MFSRRV] = {
31911 +               .create = au_wbr_create_mfsrr,
31912 +               .init   = au_wbr_create_init_mfsrr,
31913 +               .fin    = au_wbr_create_fin_mfs
31914 +       },
31915 +       [AuWbrCreate_PMFS] = {
31916 +               .create = au_wbr_create_pmfs,
31917 +               .init   = au_wbr_create_init_mfs,
31918 +               .fin    = au_wbr_create_fin_mfs
31919 +       },
31920 +       [AuWbrCreate_PMFSV] = {
31921 +               .create = au_wbr_create_pmfs,
31922 +               .init   = au_wbr_create_init_mfs,
31923 +               .fin    = au_wbr_create_fin_mfs
31924 +       },
31925 +       [AuWbrCreate_PMFSRR] = {
31926 +               .create = au_wbr_create_pmfsrr,
31927 +               .init   = au_wbr_create_init_mfsrr,
31928 +               .fin    = au_wbr_create_fin_mfs
31929 +       },
31930 +       [AuWbrCreate_PMFSRRV] = {
31931 +               .create = au_wbr_create_pmfsrr,
31932 +               .init   = au_wbr_create_init_mfsrr,
31933 +               .fin    = au_wbr_create_fin_mfs
31934 +       }
31935 +};
31936 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
31937 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
31938 +++ linux/fs/aufs/whout.c       2016-01-13 20:11:11.673093671 +0100
31939 @@ -0,0 +1,1060 @@
31940 +/*
31941 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31942 + *
31943 + * This program, aufs is free software; you can redistribute it and/or modify
31944 + * it under the terms of the GNU General Public License as published by
31945 + * the Free Software Foundation; either version 2 of the License, or
31946 + * (at your option) any later version.
31947 + *
31948 + * This program is distributed in the hope that it will be useful,
31949 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31950 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31951 + * GNU General Public License for more details.
31952 + *
31953 + * You should have received a copy of the GNU General Public License
31954 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31955 + */
31956 +
31957 +/*
31958 + * whiteout for logical deletion and opaque directory
31959 + */
31960 +
31961 +#include "aufs.h"
31962 +
31963 +#define WH_MASK                        S_IRUGO
31964 +
31965 +/*
31966 + * If a directory contains this file, then it is opaque.  We start with the
31967 + * .wh. flag so that it is blocked by lookup.
31968 + */
31969 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
31970 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
31971 +
31972 +/*
31973 + * generate whiteout name, which is NOT terminated by NULL.
31974 + * @name: original d_name.name
31975 + * @len: original d_name.len
31976 + * @wh: whiteout qstr
31977 + * returns zero when succeeds, otherwise error.
31978 + * succeeded value as wh->name should be freed by kfree().
31979 + */
31980 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
31981 +{
31982 +       char *p;
31983 +
31984 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
31985 +               return -ENAMETOOLONG;
31986 +
31987 +       wh->len = name->len + AUFS_WH_PFX_LEN;
31988 +       p = kmalloc(wh->len, GFP_NOFS);
31989 +       wh->name = p;
31990 +       if (p) {
31991 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
31992 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
31993 +               /* smp_mb(); */
31994 +               return 0;
31995 +       }
31996 +       return -ENOMEM;
31997 +}
31998 +
31999 +/* ---------------------------------------------------------------------- */
32000 +
32001 +/*
32002 + * test if the @wh_name exists under @h_parent.
32003 + * @try_sio specifies the necessary of super-io.
32004 + */
32005 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
32006 +{
32007 +       int err;
32008 +       struct dentry *wh_dentry;
32009 +
32010 +       if (!try_sio)
32011 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
32012 +       else
32013 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
32014 +       err = PTR_ERR(wh_dentry);
32015 +       if (IS_ERR(wh_dentry)) {
32016 +               if (err == -ENAMETOOLONG)
32017 +                       err = 0;
32018 +               goto out;
32019 +       }
32020 +
32021 +       err = 0;
32022 +       if (d_is_negative(wh_dentry))
32023 +               goto out_wh; /* success */
32024 +
32025 +       err = 1;
32026 +       if (d_is_reg(wh_dentry))
32027 +               goto out_wh; /* success */
32028 +
32029 +       err = -EIO;
32030 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
32031 +               wh_dentry, d_inode(wh_dentry)->i_mode);
32032 +
32033 +out_wh:
32034 +       dput(wh_dentry);
32035 +out:
32036 +       return err;
32037 +}
32038 +
32039 +/*
32040 + * test if the @h_dentry sets opaque or not.
32041 + */
32042 +int au_diropq_test(struct dentry *h_dentry)
32043 +{
32044 +       int err;
32045 +       struct inode *h_dir;
32046 +
32047 +       h_dir = d_inode(h_dentry);
32048 +       err = au_wh_test(h_dentry, &diropq_name,
32049 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
32050 +       return err;
32051 +}
32052 +
32053 +/*
32054 + * returns a negative dentry whose name is unique and temporary.
32055 + */
32056 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32057 +                            struct qstr *prefix)
32058 +{
32059 +       struct dentry *dentry;
32060 +       int i;
32061 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
32062 +               *name, *p;
32063 +       /* strict atomic_t is unnecessary here */
32064 +       static unsigned short cnt;
32065 +       struct qstr qs;
32066 +
32067 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32068 +
32069 +       name = defname;
32070 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32071 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
32072 +               dentry = ERR_PTR(-ENAMETOOLONG);
32073 +               if (unlikely(qs.len > NAME_MAX))
32074 +                       goto out;
32075 +               dentry = ERR_PTR(-ENOMEM);
32076 +               name = kmalloc(qs.len + 1, GFP_NOFS);
32077 +               if (unlikely(!name))
32078 +                       goto out;
32079 +       }
32080 +
32081 +       /* doubly whiteout-ed */
32082 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32083 +       p = name + AUFS_WH_PFX_LEN * 2;
32084 +       memcpy(p, prefix->name, prefix->len);
32085 +       p += prefix->len;
32086 +       *p++ = '.';
32087 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
32088 +
32089 +       qs.name = name;
32090 +       for (i = 0; i < 3; i++) {
32091 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
32092 +               dentry = au_sio_lkup_one(&qs, h_parent);
32093 +               if (IS_ERR(dentry) || d_is_negative(dentry))
32094 +                       goto out_name;
32095 +               dput(dentry);
32096 +       }
32097 +       /* pr_warn("could not get random name\n"); */
32098 +       dentry = ERR_PTR(-EEXIST);
32099 +       AuDbg("%.*s\n", AuLNPair(&qs));
32100 +       BUG();
32101 +
32102 +out_name:
32103 +       if (name != defname)
32104 +               kfree(name);
32105 +out:
32106 +       AuTraceErrPtr(dentry);
32107 +       return dentry;
32108 +}
32109 +
32110 +/*
32111 + * rename the @h_dentry on @br to the whiteouted temporary name.
32112 + */
32113 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32114 +{
32115 +       int err;
32116 +       struct path h_path = {
32117 +               .mnt = au_br_mnt(br)
32118 +       };
32119 +       struct inode *h_dir, *delegated;
32120 +       struct dentry *h_parent;
32121 +
32122 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
32123 +       h_dir = d_inode(h_parent);
32124 +       IMustLock(h_dir);
32125 +
32126 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32127 +       err = PTR_ERR(h_path.dentry);
32128 +       if (IS_ERR(h_path.dentry))
32129 +               goto out;
32130 +
32131 +       /* under the same dir, no need to lock_rename() */
32132 +       delegated = NULL;
32133 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
32134 +       AuTraceErr(err);
32135 +       if (unlikely(err == -EWOULDBLOCK)) {
32136 +               pr_warn("cannot retry for NFSv4 delegation"
32137 +                       " for an internal rename\n");
32138 +               iput(delegated);
32139 +       }
32140 +       dput(h_path.dentry);
32141 +
32142 +out:
32143 +       AuTraceErr(err);
32144 +       return err;
32145 +}
32146 +
32147 +/* ---------------------------------------------------------------------- */
32148 +/*
32149 + * functions for removing a whiteout
32150 + */
32151 +
32152 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32153 +{
32154 +       int err, force;
32155 +       struct inode *delegated;
32156 +
32157 +       /*
32158 +        * forces superio when the dir has a sticky bit.
32159 +        * this may be a violation of unix fs semantics.
32160 +        */
32161 +       force = (h_dir->i_mode & S_ISVTX)
32162 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
32163 +       delegated = NULL;
32164 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
32165 +       if (unlikely(err == -EWOULDBLOCK)) {
32166 +               pr_warn("cannot retry for NFSv4 delegation"
32167 +                       " for an internal unlink\n");
32168 +               iput(delegated);
32169 +       }
32170 +       return err;
32171 +}
32172 +
32173 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32174 +                       struct dentry *dentry)
32175 +{
32176 +       int err;
32177 +
32178 +       err = do_unlink_wh(h_dir, h_path);
32179 +       if (!err && dentry)
32180 +               au_set_dbwh(dentry, -1);
32181 +
32182 +       return err;
32183 +}
32184 +
32185 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32186 +                         struct au_branch *br)
32187 +{
32188 +       int err;
32189 +       struct path h_path = {
32190 +               .mnt = au_br_mnt(br)
32191 +       };
32192 +
32193 +       err = 0;
32194 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32195 +       if (IS_ERR(h_path.dentry))
32196 +               err = PTR_ERR(h_path.dentry);
32197 +       else {
32198 +               if (d_is_reg(h_path.dentry))
32199 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
32200 +               dput(h_path.dentry);
32201 +       }
32202 +
32203 +       return err;
32204 +}
32205 +
32206 +/* ---------------------------------------------------------------------- */
32207 +/*
32208 + * initialize/clean whiteout for a branch
32209 + */
32210 +
32211 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32212 +                       const int isdir)
32213 +{
32214 +       int err;
32215 +       struct inode *delegated;
32216 +
32217 +       if (d_is_negative(whpath->dentry))
32218 +               return;
32219 +
32220 +       if (isdir)
32221 +               err = vfsub_rmdir(h_dir, whpath);
32222 +       else {
32223 +               delegated = NULL;
32224 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32225 +               if (unlikely(err == -EWOULDBLOCK)) {
32226 +                       pr_warn("cannot retry for NFSv4 delegation"
32227 +                               " for an internal unlink\n");
32228 +                       iput(delegated);
32229 +               }
32230 +       }
32231 +       if (unlikely(err))
32232 +               pr_warn("failed removing %pd (%d), ignored.\n",
32233 +                       whpath->dentry, err);
32234 +}
32235 +
32236 +static int test_linkable(struct dentry *h_root)
32237 +{
32238 +       struct inode *h_dir = d_inode(h_root);
32239 +
32240 +       if (h_dir->i_op->link)
32241 +               return 0;
32242 +
32243 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32244 +              h_root, au_sbtype(h_root->d_sb));
32245 +       return -ENOSYS;
32246 +}
32247 +
32248 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32249 +static int au_whdir(struct inode *h_dir, struct path *path)
32250 +{
32251 +       int err;
32252 +
32253 +       err = -EEXIST;
32254 +       if (d_is_negative(path->dentry)) {
32255 +               int mode = S_IRWXU;
32256 +
32257 +               if (au_test_nfs(path->dentry->d_sb))
32258 +                       mode |= S_IXUGO;
32259 +               err = vfsub_mkdir(h_dir, path, mode);
32260 +       } else if (d_is_dir(path->dentry))
32261 +               err = 0;
32262 +       else
32263 +               pr_err("unknown %pd exists\n", path->dentry);
32264 +
32265 +       return err;
32266 +}
32267 +
32268 +struct au_wh_base {
32269 +       const struct qstr *name;
32270 +       struct dentry *dentry;
32271 +};
32272 +
32273 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32274 +                         struct path *h_path)
32275 +{
32276 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32277 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32278 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32279 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32280 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32281 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32282 +}
32283 +
32284 +/*
32285 + * returns tri-state,
32286 + * minus: error, caller should print the message
32287 + * zero: succuess
32288 + * plus: error, caller should NOT print the message
32289 + */
32290 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32291 +                               int do_plink, struct au_wh_base base[],
32292 +                               struct path *h_path)
32293 +{
32294 +       int err;
32295 +       struct inode *h_dir;
32296 +
32297 +       h_dir = d_inode(h_root);
32298 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32299 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32300 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32301 +       if (do_plink) {
32302 +               err = test_linkable(h_root);
32303 +               if (unlikely(err)) {
32304 +                       err = 1;
32305 +                       goto out;
32306 +               }
32307 +
32308 +               err = au_whdir(h_dir, h_path);
32309 +               if (unlikely(err))
32310 +                       goto out;
32311 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32312 +       } else
32313 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32314 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32315 +       err = au_whdir(h_dir, h_path);
32316 +       if (unlikely(err))
32317 +               goto out;
32318 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32319 +
32320 +out:
32321 +       return err;
32322 +}
32323 +
32324 +/*
32325 + * for the moment, aufs supports the branch filesystem which does not support
32326 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
32327 + * copyup failed. finally, such filesystem will not be used as the writable
32328 + * branch.
32329 + *
32330 + * returns tri-state, see above.
32331 + */
32332 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32333 +                        int do_plink, struct au_wh_base base[],
32334 +                        struct path *h_path)
32335 +{
32336 +       int err;
32337 +       struct inode *h_dir;
32338 +
32339 +       WbrWhMustWriteLock(wbr);
32340 +
32341 +       err = test_linkable(h_root);
32342 +       if (unlikely(err)) {
32343 +               err = 1;
32344 +               goto out;
32345 +       }
32346 +
32347 +       /*
32348 +        * todo: should this create be done in /sbin/mount.aufs helper?
32349 +        */
32350 +       err = -EEXIST;
32351 +       h_dir = d_inode(h_root);
32352 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
32353 +               h_path->dentry = base[AuBrWh_BASE].dentry;
32354 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
32355 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
32356 +               err = 0;
32357 +       else
32358 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
32359 +       if (unlikely(err))
32360 +               goto out;
32361 +
32362 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32363 +       if (do_plink) {
32364 +               err = au_whdir(h_dir, h_path);
32365 +               if (unlikely(err))
32366 +                       goto out;
32367 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32368 +       } else
32369 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32370 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32371 +
32372 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32373 +       err = au_whdir(h_dir, h_path);
32374 +       if (unlikely(err))
32375 +               goto out;
32376 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32377 +
32378 +out:
32379 +       return err;
32380 +}
32381 +
32382 +/*
32383 + * initialize the whiteout base file/dir for @br.
32384 + */
32385 +int au_wh_init(struct au_branch *br, struct super_block *sb)
32386 +{
32387 +       int err, i;
32388 +       const unsigned char do_plink
32389 +               = !!au_opt_test(au_mntflags(sb), PLINK);
32390 +       struct inode *h_dir;
32391 +       struct path path = br->br_path;
32392 +       struct dentry *h_root = path.dentry;
32393 +       struct au_wbr *wbr = br->br_wbr;
32394 +       static const struct qstr base_name[] = {
32395 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32396 +                                         sizeof(AUFS_BASE_NAME) - 1),
32397 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32398 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
32399 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32400 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
32401 +       };
32402 +       struct au_wh_base base[] = {
32403 +               [AuBrWh_BASE] = {
32404 +                       .name   = base_name + AuBrWh_BASE,
32405 +                       .dentry = NULL
32406 +               },
32407 +               [AuBrWh_PLINK] = {
32408 +                       .name   = base_name + AuBrWh_PLINK,
32409 +                       .dentry = NULL
32410 +               },
32411 +               [AuBrWh_ORPH] = {
32412 +                       .name   = base_name + AuBrWh_ORPH,
32413 +                       .dentry = NULL
32414 +               }
32415 +       };
32416 +
32417 +       if (wbr)
32418 +               WbrWhMustWriteLock(wbr);
32419 +
32420 +       for (i = 0; i < AuBrWh_Last; i++) {
32421 +               /* doubly whiteouted */
32422 +               struct dentry *d;
32423 +
32424 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
32425 +               err = PTR_ERR(d);
32426 +               if (IS_ERR(d))
32427 +                       goto out;
32428 +
32429 +               base[i].dentry = d;
32430 +               AuDebugOn(wbr
32431 +                         && wbr->wbr_wh[i]
32432 +                         && wbr->wbr_wh[i] != base[i].dentry);
32433 +       }
32434 +
32435 +       if (wbr)
32436 +               for (i = 0; i < AuBrWh_Last; i++) {
32437 +                       dput(wbr->wbr_wh[i]);
32438 +                       wbr->wbr_wh[i] = NULL;
32439 +               }
32440 +
32441 +       err = 0;
32442 +       if (!au_br_writable(br->br_perm)) {
32443 +               h_dir = d_inode(h_root);
32444 +               au_wh_init_ro(h_dir, base, &path);
32445 +       } else if (!au_br_wh_linkable(br->br_perm)) {
32446 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32447 +               if (err > 0)
32448 +                       goto out;
32449 +               else if (err)
32450 +                       goto out_err;
32451 +       } else {
32452 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32453 +               if (err > 0)
32454 +                       goto out;
32455 +               else if (err)
32456 +                       goto out_err;
32457 +       }
32458 +       goto out; /* success */
32459 +
32460 +out_err:
32461 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
32462 +              err, h_root, au_sbtype(h_root->d_sb));
32463 +out:
32464 +       for (i = 0; i < AuBrWh_Last; i++)
32465 +               dput(base[i].dentry);
32466 +       return err;
32467 +}
32468 +
32469 +/* ---------------------------------------------------------------------- */
32470 +/*
32471 + * whiteouts are all hard-linked usually.
32472 + * when its link count reaches a ceiling, we create a new whiteout base
32473 + * asynchronously.
32474 + */
32475 +
32476 +struct reinit_br_wh {
32477 +       struct super_block *sb;
32478 +       struct au_branch *br;
32479 +};
32480 +
32481 +static void reinit_br_wh(void *arg)
32482 +{
32483 +       int err;
32484 +       aufs_bindex_t bindex;
32485 +       struct path h_path;
32486 +       struct reinit_br_wh *a = arg;
32487 +       struct au_wbr *wbr;
32488 +       struct inode *dir, *delegated;
32489 +       struct dentry *h_root;
32490 +       struct au_hinode *hdir;
32491 +
32492 +       err = 0;
32493 +       wbr = a->br->br_wbr;
32494 +       /* big aufs lock */
32495 +       si_noflush_write_lock(a->sb);
32496 +       if (!au_br_writable(a->br->br_perm))
32497 +               goto out;
32498 +       bindex = au_br_index(a->sb, a->br->br_id);
32499 +       if (unlikely(bindex < 0))
32500 +               goto out;
32501 +
32502 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
32503 +       dir = d_inode(a->sb->s_root);
32504 +       hdir = au_hi(dir, bindex);
32505 +       h_root = au_h_dptr(a->sb->s_root, bindex);
32506 +       AuDebugOn(h_root != au_br_dentry(a->br));
32507 +
32508 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32509 +       wbr_wh_write_lock(wbr);
32510 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32511 +                         h_root, a->br);
32512 +       if (!err) {
32513 +               h_path.dentry = wbr->wbr_whbase;
32514 +               h_path.mnt = au_br_mnt(a->br);
32515 +               delegated = NULL;
32516 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32517 +                                  /*force*/0);
32518 +               if (unlikely(err == -EWOULDBLOCK)) {
32519 +                       pr_warn("cannot retry for NFSv4 delegation"
32520 +                               " for an internal unlink\n");
32521 +                       iput(delegated);
32522 +               }
32523 +       } else {
32524 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
32525 +               err = 0;
32526 +       }
32527 +       dput(wbr->wbr_whbase);
32528 +       wbr->wbr_whbase = NULL;
32529 +       if (!err)
32530 +               err = au_wh_init(a->br, a->sb);
32531 +       wbr_wh_write_unlock(wbr);
32532 +       au_hn_imtx_unlock(hdir);
32533 +       di_read_unlock(a->sb->s_root, AuLock_IR);
32534 +       if (!err)
32535 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
32536 +
32537 +out:
32538 +       if (wbr)
32539 +               atomic_dec(&wbr->wbr_wh_running);
32540 +       atomic_dec(&a->br->br_count);
32541 +       si_write_unlock(a->sb);
32542 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
32543 +       kfree(arg);
32544 +       if (unlikely(err))
32545 +               AuIOErr("err %d\n", err);
32546 +}
32547 +
32548 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32549 +{
32550 +       int do_dec, wkq_err;
32551 +       struct reinit_br_wh *arg;
32552 +
32553 +       do_dec = 1;
32554 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32555 +               goto out;
32556 +
32557 +       /* ignore ENOMEM */
32558 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
32559 +       if (arg) {
32560 +               /*
32561 +                * dec(wh_running), kfree(arg) and dec(br_count)
32562 +                * in reinit function
32563 +                */
32564 +               arg->sb = sb;
32565 +               arg->br = br;
32566 +               atomic_inc(&br->br_count);
32567 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
32568 +               if (unlikely(wkq_err)) {
32569 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
32570 +                       atomic_dec(&br->br_count);
32571 +                       kfree(arg);
32572 +               }
32573 +               do_dec = 0;
32574 +       }
32575 +
32576 +out:
32577 +       if (do_dec)
32578 +               atomic_dec(&br->br_wbr->wbr_wh_running);
32579 +}
32580 +
32581 +/* ---------------------------------------------------------------------- */
32582 +
32583 +/*
32584 + * create the whiteout @wh.
32585 + */
32586 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32587 +                            struct dentry *wh)
32588 +{
32589 +       int err;
32590 +       struct path h_path = {
32591 +               .dentry = wh
32592 +       };
32593 +       struct au_branch *br;
32594 +       struct au_wbr *wbr;
32595 +       struct dentry *h_parent;
32596 +       struct inode *h_dir, *delegated;
32597 +
32598 +       h_parent = wh->d_parent; /* dir inode is locked */
32599 +       h_dir = d_inode(h_parent);
32600 +       IMustLock(h_dir);
32601 +
32602 +       br = au_sbr(sb, bindex);
32603 +       h_path.mnt = au_br_mnt(br);
32604 +       wbr = br->br_wbr;
32605 +       wbr_wh_read_lock(wbr);
32606 +       if (wbr->wbr_whbase) {
32607 +               delegated = NULL;
32608 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
32609 +               if (unlikely(err == -EWOULDBLOCK)) {
32610 +                       pr_warn("cannot retry for NFSv4 delegation"
32611 +                               " for an internal link\n");
32612 +                       iput(delegated);
32613 +               }
32614 +               if (!err || err != -EMLINK)
32615 +                       goto out;
32616 +
32617 +               /* link count full. re-initialize br_whbase. */
32618 +               kick_reinit_br_wh(sb, br);
32619 +       }
32620 +
32621 +       /* return this error in this context */
32622 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
32623 +       if (!err)
32624 +               au_fhsm_wrote(sb, bindex, /*force*/0);
32625 +
32626 +out:
32627 +       wbr_wh_read_unlock(wbr);
32628 +       return err;
32629 +}
32630 +
32631 +/* ---------------------------------------------------------------------- */
32632 +
32633 +/*
32634 + * create or remove the diropq.
32635 + */
32636 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
32637 +                               unsigned int flags)
32638 +{
32639 +       struct dentry *opq_dentry, *h_dentry;
32640 +       struct super_block *sb;
32641 +       struct au_branch *br;
32642 +       int err;
32643 +
32644 +       sb = dentry->d_sb;
32645 +       br = au_sbr(sb, bindex);
32646 +       h_dentry = au_h_dptr(dentry, bindex);
32647 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
32648 +       if (IS_ERR(opq_dentry))
32649 +               goto out;
32650 +
32651 +       if (au_ftest_diropq(flags, CREATE)) {
32652 +               err = link_or_create_wh(sb, bindex, opq_dentry);
32653 +               if (!err) {
32654 +                       au_set_dbdiropq(dentry, bindex);
32655 +                       goto out; /* success */
32656 +               }
32657 +       } else {
32658 +               struct path tmp = {
32659 +                       .dentry = opq_dentry,
32660 +                       .mnt    = au_br_mnt(br)
32661 +               };
32662 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
32663 +               if (!err)
32664 +                       au_set_dbdiropq(dentry, -1);
32665 +       }
32666 +       dput(opq_dentry);
32667 +       opq_dentry = ERR_PTR(err);
32668 +
32669 +out:
32670 +       return opq_dentry;
32671 +}
32672 +
32673 +struct do_diropq_args {
32674 +       struct dentry **errp;
32675 +       struct dentry *dentry;
32676 +       aufs_bindex_t bindex;
32677 +       unsigned int flags;
32678 +};
32679 +
32680 +static void call_do_diropq(void *args)
32681 +{
32682 +       struct do_diropq_args *a = args;
32683 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32684 +}
32685 +
32686 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32687 +                            unsigned int flags)
32688 +{
32689 +       struct dentry *diropq, *h_dentry;
32690 +
32691 +       h_dentry = au_h_dptr(dentry, bindex);
32692 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
32693 +               diropq = do_diropq(dentry, bindex, flags);
32694 +       else {
32695 +               int wkq_err;
32696 +               struct do_diropq_args args = {
32697 +                       .errp           = &diropq,
32698 +                       .dentry         = dentry,
32699 +                       .bindex         = bindex,
32700 +                       .flags          = flags
32701 +               };
32702 +
32703 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
32704 +               if (unlikely(wkq_err))
32705 +                       diropq = ERR_PTR(wkq_err);
32706 +       }
32707 +
32708 +       return diropq;
32709 +}
32710 +
32711 +/* ---------------------------------------------------------------------- */
32712 +
32713 +/*
32714 + * lookup whiteout dentry.
32715 + * @h_parent: lower parent dentry which must exist and be locked
32716 + * @base_name: name of dentry which will be whiteouted
32717 + * returns dentry for whiteout.
32718 + */
32719 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32720 +                         struct au_branch *br)
32721 +{
32722 +       int err;
32723 +       struct qstr wh_name;
32724 +       struct dentry *wh_dentry;
32725 +
32726 +       err = au_wh_name_alloc(&wh_name, base_name);
32727 +       wh_dentry = ERR_PTR(err);
32728 +       if (!err) {
32729 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
32730 +               kfree(wh_name.name);
32731 +       }
32732 +       return wh_dentry;
32733 +}
32734 +
32735 +/*
32736 + * link/create a whiteout for @dentry on @bindex.
32737 + */
32738 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32739 +                           struct dentry *h_parent)
32740 +{
32741 +       struct dentry *wh_dentry;
32742 +       struct super_block *sb;
32743 +       int err;
32744 +
32745 +       sb = dentry->d_sb;
32746 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
32747 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
32748 +               err = link_or_create_wh(sb, bindex, wh_dentry);
32749 +               if (!err) {
32750 +                       au_set_dbwh(dentry, bindex);
32751 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
32752 +               } else {
32753 +                       dput(wh_dentry);
32754 +                       wh_dentry = ERR_PTR(err);
32755 +               }
32756 +       }
32757 +
32758 +       return wh_dentry;
32759 +}
32760 +
32761 +/* ---------------------------------------------------------------------- */
32762 +
32763 +/* Delete all whiteouts in this directory on branch bindex. */
32764 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32765 +                          aufs_bindex_t bindex, struct au_branch *br)
32766 +{
32767 +       int err;
32768 +       unsigned long ul, n;
32769 +       struct qstr wh_name;
32770 +       char *p;
32771 +       struct hlist_head *head;
32772 +       struct au_vdir_wh *pos;
32773 +       struct au_vdir_destr *str;
32774 +
32775 +       err = -ENOMEM;
32776 +       p = (void *)__get_free_page(GFP_NOFS);
32777 +       wh_name.name = p;
32778 +       if (unlikely(!wh_name.name))
32779 +               goto out;
32780 +
32781 +       err = 0;
32782 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32783 +       p += AUFS_WH_PFX_LEN;
32784 +       n = whlist->nh_num;
32785 +       head = whlist->nh_head;
32786 +       for (ul = 0; !err && ul < n; ul++, head++) {
32787 +               hlist_for_each_entry(pos, head, wh_hash) {
32788 +                       if (pos->wh_bindex != bindex)
32789 +                               continue;
32790 +
32791 +                       str = &pos->wh_str;
32792 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32793 +                               memcpy(p, str->name, str->len);
32794 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
32795 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
32796 +                               if (!err)
32797 +                                       continue;
32798 +                               break;
32799 +                       }
32800 +                       AuIOErr("whiteout name too long %.*s\n",
32801 +                               str->len, str->name);
32802 +                       err = -EIO;
32803 +                       break;
32804 +               }
32805 +       }
32806 +       free_page((unsigned long)wh_name.name);
32807 +
32808 +out:
32809 +       return err;
32810 +}
32811 +
32812 +struct del_wh_children_args {
32813 +       int *errp;
32814 +       struct dentry *h_dentry;
32815 +       struct au_nhash *whlist;
32816 +       aufs_bindex_t bindex;
32817 +       struct au_branch *br;
32818 +};
32819 +
32820 +static void call_del_wh_children(void *args)
32821 +{
32822 +       struct del_wh_children_args *a = args;
32823 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
32824 +}
32825 +
32826 +/* ---------------------------------------------------------------------- */
32827 +
32828 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
32829 +{
32830 +       struct au_whtmp_rmdir *whtmp;
32831 +       int err;
32832 +       unsigned int rdhash;
32833 +
32834 +       SiMustAnyLock(sb);
32835 +
32836 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
32837 +       if (unlikely(!whtmp)) {
32838 +               whtmp = ERR_PTR(-ENOMEM);
32839 +               goto out;
32840 +       }
32841 +
32842 +       /* no estimation for dir size */
32843 +       rdhash = au_sbi(sb)->si_rdhash;
32844 +       if (!rdhash)
32845 +               rdhash = AUFS_RDHASH_DEF;
32846 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
32847 +       if (unlikely(err)) {
32848 +               kfree(whtmp);
32849 +               whtmp = ERR_PTR(err);
32850 +       }
32851 +
32852 +out:
32853 +       return whtmp;
32854 +}
32855 +
32856 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
32857 +{
32858 +       if (whtmp->br)
32859 +               atomic_dec(&whtmp->br->br_count);
32860 +       dput(whtmp->wh_dentry);
32861 +       iput(whtmp->dir);
32862 +       au_nhash_wh_free(&whtmp->whlist);
32863 +       kfree(whtmp);
32864 +}
32865 +
32866 +/*
32867 + * rmdir the whiteouted temporary named dir @h_dentry.
32868 + * @whlist: whiteouted children.
32869 + */
32870 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32871 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
32872 +{
32873 +       int err;
32874 +       unsigned int h_nlink;
32875 +       struct path h_tmp;
32876 +       struct inode *wh_inode, *h_dir;
32877 +       struct au_branch *br;
32878 +
32879 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
32880 +       IMustLock(h_dir);
32881 +
32882 +       br = au_sbr(dir->i_sb, bindex);
32883 +       wh_inode = d_inode(wh_dentry);
32884 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
32885 +
32886 +       /*
32887 +        * someone else might change some whiteouts while we were sleeping.
32888 +        * it means this whlist may have an obsoleted entry.
32889 +        */
32890 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
32891 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
32892 +       else {
32893 +               int wkq_err;
32894 +               struct del_wh_children_args args = {
32895 +                       .errp           = &err,
32896 +                       .h_dentry       = wh_dentry,
32897 +                       .whlist         = whlist,
32898 +                       .bindex         = bindex,
32899 +                       .br             = br
32900 +               };
32901 +
32902 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
32903 +               if (unlikely(wkq_err))
32904 +                       err = wkq_err;
32905 +       }
32906 +       mutex_unlock(&wh_inode->i_mutex);
32907 +
32908 +       if (!err) {
32909 +               h_tmp.dentry = wh_dentry;
32910 +               h_tmp.mnt = au_br_mnt(br);
32911 +               h_nlink = h_dir->i_nlink;
32912 +               err = vfsub_rmdir(h_dir, &h_tmp);
32913 +               /* some fs doesn't change the parent nlink in some cases */
32914 +               h_nlink -= h_dir->i_nlink;
32915 +       }
32916 +
32917 +       if (!err) {
32918 +               if (au_ibstart(dir) == bindex) {
32919 +                       /* todo: dir->i_mutex is necessary */
32920 +                       au_cpup_attr_timesizes(dir);
32921 +                       if (h_nlink)
32922 +                               vfsub_drop_nlink(dir);
32923 +               }
32924 +               return 0; /* success */
32925 +       }
32926 +
32927 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
32928 +       return err;
32929 +}
32930 +
32931 +static void call_rmdir_whtmp(void *args)
32932 +{
32933 +       int err;
32934 +       aufs_bindex_t bindex;
32935 +       struct au_whtmp_rmdir *a = args;
32936 +       struct super_block *sb;
32937 +       struct dentry *h_parent;
32938 +       struct inode *h_dir;
32939 +       struct au_hinode *hdir;
32940 +
32941 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
32942 +       /* mutex_lock(&a->dir->i_mutex); */
32943 +       err = -EROFS;
32944 +       sb = a->dir->i_sb;
32945 +       si_read_lock(sb, !AuLock_FLUSH);
32946 +       if (!au_br_writable(a->br->br_perm))
32947 +               goto out;
32948 +       bindex = au_br_index(sb, a->br->br_id);
32949 +       if (unlikely(bindex < 0))
32950 +               goto out;
32951 +
32952 +       err = -EIO;
32953 +       ii_write_lock_parent(a->dir);
32954 +       h_parent = dget_parent(a->wh_dentry);
32955 +       h_dir = d_inode(h_parent);
32956 +       hdir = au_hi(a->dir, bindex);
32957 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
32958 +       if (unlikely(err))
32959 +               goto out_mnt;
32960 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32961 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
32962 +                         a->br);
32963 +       if (!err)
32964 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
32965 +       au_hn_imtx_unlock(hdir);
32966 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
32967 +
32968 +out_mnt:
32969 +       dput(h_parent);
32970 +       ii_write_unlock(a->dir);
32971 +out:
32972 +       /* mutex_unlock(&a->dir->i_mutex); */
32973 +       au_whtmp_rmdir_free(a);
32974 +       si_read_unlock(sb);
32975 +       au_nwt_done(&au_sbi(sb)->si_nowait);
32976 +       if (unlikely(err))
32977 +               AuIOErr("err %d\n", err);
32978 +}
32979 +
32980 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32981 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
32982 +{
32983 +       int wkq_err;
32984 +       struct super_block *sb;
32985 +
32986 +       IMustLock(dir);
32987 +
32988 +       /* all post-process will be done in do_rmdir_whtmp(). */
32989 +       sb = dir->i_sb;
32990 +       args->dir = au_igrab(dir);
32991 +       args->br = au_sbr(sb, bindex);
32992 +       atomic_inc(&args->br->br_count);
32993 +       args->wh_dentry = dget(wh_dentry);
32994 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
32995 +       if (unlikely(wkq_err)) {
32996 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
32997 +               au_whtmp_rmdir_free(args);
32998 +       }
32999 +}
33000 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33001 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
33002 +++ linux/fs/aufs/whout.h       2016-01-13 20:11:11.673093671 +0100
33003 @@ -0,0 +1,85 @@
33004 +/*
33005 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33006 + *
33007 + * This program, aufs is free software; you can redistribute it and/or modify
33008 + * it under the terms of the GNU General Public License as published by
33009 + * the Free Software Foundation; either version 2 of the License, or
33010 + * (at your option) any later version.
33011 + *
33012 + * This program is distributed in the hope that it will be useful,
33013 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33014 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33015 + * GNU General Public License for more details.
33016 + *
33017 + * You should have received a copy of the GNU General Public License
33018 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33019 + */
33020 +
33021 +/*
33022 + * whiteout for logical deletion and opaque directory
33023 + */
33024 +
33025 +#ifndef __AUFS_WHOUT_H__
33026 +#define __AUFS_WHOUT_H__
33027 +
33028 +#ifdef __KERNEL__
33029 +
33030 +#include "dir.h"
33031 +
33032 +/* whout.c */
33033 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
33034 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33035 +int au_diropq_test(struct dentry *h_dentry);
33036 +struct au_branch;
33037 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33038 +                            struct qstr *prefix);
33039 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33040 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33041 +                       struct dentry *dentry);
33042 +int au_wh_init(struct au_branch *br, struct super_block *sb);
33043 +
33044 +/* diropq flags */
33045 +#define AuDiropq_CREATE        1
33046 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
33047 +#define au_fset_diropq(flags, name) \
33048 +       do { (flags) |= AuDiropq_##name; } while (0)
33049 +#define au_fclr_diropq(flags, name) \
33050 +       do { (flags) &= ~AuDiropq_##name; } while (0)
33051 +
33052 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33053 +                            unsigned int flags);
33054 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33055 +                         struct au_branch *br);
33056 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33057 +                           struct dentry *h_parent);
33058 +
33059 +/* real rmdir for the whiteout-ed dir */
33060 +struct au_whtmp_rmdir {
33061 +       struct inode *dir;
33062 +       struct au_branch *br;
33063 +       struct dentry *wh_dentry;
33064 +       struct au_nhash whlist;
33065 +};
33066 +
33067 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33068 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33069 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33070 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
33071 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33072 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33073 +
33074 +/* ---------------------------------------------------------------------- */
33075 +
33076 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
33077 +                                             aufs_bindex_t bindex)
33078 +{
33079 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33080 +}
33081 +
33082 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33083 +{
33084 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33085 +}
33086 +
33087 +#endif /* __KERNEL__ */
33088 +#endif /* __AUFS_WHOUT_H__ */
33089 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33090 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
33091 +++ linux/fs/aufs/wkq.c 2016-01-13 20:11:11.673093671 +0100
33092 @@ -0,0 +1,213 @@
33093 +/*
33094 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33095 + *
33096 + * This program, aufs is free software; you can redistribute it and/or modify
33097 + * it under the terms of the GNU General Public License as published by
33098 + * the Free Software Foundation; either version 2 of the License, or
33099 + * (at your option) any later version.
33100 + *
33101 + * This program is distributed in the hope that it will be useful,
33102 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33103 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33104 + * GNU General Public License for more details.
33105 + *
33106 + * You should have received a copy of the GNU General Public License
33107 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33108 + */
33109 +
33110 +/*
33111 + * workqueue for asynchronous/super-io operations
33112 + * todo: try new dredential scheme
33113 + */
33114 +
33115 +#include <linux/module.h>
33116 +#include "aufs.h"
33117 +
33118 +/* internal workqueue named AUFS_WKQ_NAME */
33119 +
33120 +static struct workqueue_struct *au_wkq;
33121 +
33122 +struct au_wkinfo {
33123 +       struct work_struct wk;
33124 +       struct kobject *kobj;
33125 +
33126 +       unsigned int flags; /* see wkq.h */
33127 +
33128 +       au_wkq_func_t func;
33129 +       void *args;
33130 +
33131 +       struct completion *comp;
33132 +};
33133 +
33134 +/* ---------------------------------------------------------------------- */
33135 +
33136 +static void wkq_func(struct work_struct *wk)
33137 +{
33138 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33139 +
33140 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
33141 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33142 +
33143 +       wkinfo->func(wkinfo->args);
33144 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
33145 +               complete(wkinfo->comp);
33146 +       else {
33147 +               kobject_put(wkinfo->kobj);
33148 +               module_put(THIS_MODULE); /* todo: ?? */
33149 +               kfree(wkinfo);
33150 +       }
33151 +}
33152 +
33153 +/*
33154 + * Since struct completion is large, try allocating it dynamically.
33155 + */
33156 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
33157 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
33158 +
33159 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33160 +{
33161 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33162 +       if (*comp) {
33163 +               init_completion(*comp);
33164 +               wkinfo->comp = *comp;
33165 +               return 0;
33166 +       }
33167 +       return -ENOMEM;
33168 +}
33169 +
33170 +static void au_wkq_comp_free(struct completion *comp)
33171 +{
33172 +       kfree(comp);
33173 +}
33174 +
33175 +#else
33176 +
33177 +/* no braces */
33178 +#define AuWkqCompDeclare(name) \
33179 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33180 +       struct completion *comp = &_ ## name
33181 +
33182 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33183 +{
33184 +       wkinfo->comp = *comp;
33185 +       return 0;
33186 +}
33187 +
33188 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33189 +{
33190 +       /* empty */
33191 +}
33192 +#endif /* 4KSTACKS */
33193 +
33194 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33195 +{
33196 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33197 +               if (au_wkq_test()) {
33198 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33199 +                               " due to a dead dir by UDBA?\n");
33200 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33201 +               }
33202 +       } else
33203 +               au_dbg_verify_kthread();
33204 +
33205 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33206 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33207 +               queue_work(au_wkq, &wkinfo->wk);
33208 +       } else {
33209 +               INIT_WORK(&wkinfo->wk, wkq_func);
33210 +               schedule_work(&wkinfo->wk);
33211 +       }
33212 +}
33213 +
33214 +/*
33215 + * Be careful. It is easy to make deadlock happen.
33216 + * processA: lock, wkq and wait
33217 + * processB: wkq and wait, lock in wkq
33218 + * --> deadlock
33219 + */
33220 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33221 +{
33222 +       int err;
33223 +       AuWkqCompDeclare(comp);
33224 +       struct au_wkinfo wkinfo = {
33225 +               .flags  = flags,
33226 +               .func   = func,
33227 +               .args   = args
33228 +       };
33229 +
33230 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33231 +       if (!err) {
33232 +               au_wkq_run(&wkinfo);
33233 +               /* no timeout, no interrupt */
33234 +               wait_for_completion(wkinfo.comp);
33235 +               au_wkq_comp_free(comp);
33236 +               destroy_work_on_stack(&wkinfo.wk);
33237 +       }
33238 +
33239 +       return err;
33240 +
33241 +}
33242 +
33243 +/*
33244 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33245 + * problem in a concurrent umounting.
33246 + */
33247 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33248 +                 unsigned int flags)
33249 +{
33250 +       int err;
33251 +       struct au_wkinfo *wkinfo;
33252 +
33253 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33254 +
33255 +       /*
33256 +        * wkq_func() must free this wkinfo.
33257 +        * it highly depends upon the implementation of workqueue.
33258 +        */
33259 +       err = 0;
33260 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33261 +       if (wkinfo) {
33262 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33263 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33264 +               wkinfo->func = func;
33265 +               wkinfo->args = args;
33266 +               wkinfo->comp = NULL;
33267 +               kobject_get(wkinfo->kobj);
33268 +               __module_get(THIS_MODULE); /* todo: ?? */
33269 +
33270 +               au_wkq_run(wkinfo);
33271 +       } else {
33272 +               err = -ENOMEM;
33273 +               au_nwt_done(&au_sbi(sb)->si_nowait);
33274 +       }
33275 +
33276 +       return err;
33277 +}
33278 +
33279 +/* ---------------------------------------------------------------------- */
33280 +
33281 +void au_nwt_init(struct au_nowait_tasks *nwt)
33282 +{
33283 +       atomic_set(&nwt->nw_len, 0);
33284 +       /* smp_mb(); */ /* atomic_set */
33285 +       init_waitqueue_head(&nwt->nw_wq);
33286 +}
33287 +
33288 +void au_wkq_fin(void)
33289 +{
33290 +       destroy_workqueue(au_wkq);
33291 +}
33292 +
33293 +int __init au_wkq_init(void)
33294 +{
33295 +       int err;
33296 +
33297 +       err = 0;
33298 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
33299 +       if (IS_ERR(au_wkq))
33300 +               err = PTR_ERR(au_wkq);
33301 +       else if (!au_wkq)
33302 +               err = -ENOMEM;
33303 +
33304 +       return err;
33305 +}
33306 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33307 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
33308 +++ linux/fs/aufs/wkq.h 2016-01-13 20:11:11.673093671 +0100
33309 @@ -0,0 +1,91 @@
33310 +/*
33311 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33312 + *
33313 + * This program, aufs is free software; you can redistribute it and/or modify
33314 + * it under the terms of the GNU General Public License as published by
33315 + * the Free Software Foundation; either version 2 of the License, or
33316 + * (at your option) any later version.
33317 + *
33318 + * This program is distributed in the hope that it will be useful,
33319 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33320 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33321 + * GNU General Public License for more details.
33322 + *
33323 + * You should have received a copy of the GNU General Public License
33324 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33325 + */
33326 +
33327 +/*
33328 + * workqueue for asynchronous/super-io operations
33329 + * todo: try new credentials management scheme
33330 + */
33331 +
33332 +#ifndef __AUFS_WKQ_H__
33333 +#define __AUFS_WKQ_H__
33334 +
33335 +#ifdef __KERNEL__
33336 +
33337 +struct super_block;
33338 +
33339 +/* ---------------------------------------------------------------------- */
33340 +
33341 +/*
33342 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33343 + */
33344 +struct au_nowait_tasks {
33345 +       atomic_t                nw_len;
33346 +       wait_queue_head_t       nw_wq;
33347 +};
33348 +
33349 +/* ---------------------------------------------------------------------- */
33350 +
33351 +typedef void (*au_wkq_func_t)(void *args);
33352 +
33353 +/* wkq flags */
33354 +#define AuWkq_WAIT     1
33355 +#define AuWkq_NEST     (1 << 1)
33356 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
33357 +#define au_fset_wkq(flags, name) \
33358 +       do { (flags) |= AuWkq_##name; } while (0)
33359 +#define au_fclr_wkq(flags, name) \
33360 +       do { (flags) &= ~AuWkq_##name; } while (0)
33361 +
33362 +#ifndef CONFIG_AUFS_HNOTIFY
33363 +#undef AuWkq_NEST
33364 +#define AuWkq_NEST     0
33365 +#endif
33366 +
33367 +/* wkq.c */
33368 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
33369 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33370 +                 unsigned int flags);
33371 +void au_nwt_init(struct au_nowait_tasks *nwt);
33372 +int __init au_wkq_init(void);
33373 +void au_wkq_fin(void);
33374 +
33375 +/* ---------------------------------------------------------------------- */
33376 +
33377 +static inline int au_wkq_test(void)
33378 +{
33379 +       return current->flags & PF_WQ_WORKER;
33380 +}
33381 +
33382 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
33383 +{
33384 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
33385 +}
33386 +
33387 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33388 +{
33389 +       if (atomic_dec_and_test(&nwt->nw_len))
33390 +               wake_up_all(&nwt->nw_wq);
33391 +}
33392 +
33393 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33394 +{
33395 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
33396 +       return 0;
33397 +}
33398 +
33399 +#endif /* __KERNEL__ */
33400 +#endif /* __AUFS_WKQ_H__ */
33401 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33402 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
33403 +++ linux/fs/aufs/xattr.c       2016-01-13 20:11:11.673093671 +0100
33404 @@ -0,0 +1,344 @@
33405 +/*
33406 + * Copyright (C) 2014-2015 Junjiro R. Okajima
33407 + *
33408 + * This program, aufs is free software; you can redistribute it and/or modify
33409 + * it under the terms of the GNU General Public License as published by
33410 + * the Free Software Foundation; either version 2 of the License, or
33411 + * (at your option) any later version.
33412 + *
33413 + * This program is distributed in the hope that it will be useful,
33414 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33415 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33416 + * GNU General Public License for more details.
33417 + *
33418 + * You should have received a copy of the GNU General Public License
33419 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33420 + */
33421 +
33422 +/*
33423 + * handling xattr functions
33424 + */
33425 +
33426 +#include <linux/xattr.h>
33427 +#include "aufs.h"
33428 +
33429 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33430 +{
33431 +       if (!ignore_flags)
33432 +               goto out;
33433 +       switch (err) {
33434 +       case -ENOMEM:
33435 +       case -EDQUOT:
33436 +               goto out;
33437 +       }
33438 +
33439 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33440 +               err = 0;
33441 +               goto out;
33442 +       }
33443 +
33444 +#define cmp(brattr, prefix) do {                                       \
33445 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
33446 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
33447 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
33448 +                               err = 0;                                \
33449 +                       goto out;                                       \
33450 +               }                                                       \
33451 +       } while (0)
33452 +
33453 +       cmp(SEC, SECURITY);
33454 +       cmp(SYS, SYSTEM);
33455 +       cmp(TR, TRUSTED);
33456 +       cmp(USR, USER);
33457 +#undef cmp
33458 +
33459 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
33460 +               err = 0;
33461 +
33462 +out:
33463 +       return err;
33464 +}
33465 +
33466 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33467 +
33468 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
33469 +                           char *name, char **buf, unsigned int ignore_flags,
33470 +                           unsigned int verbose)
33471 +{
33472 +       int err;
33473 +       ssize_t ssz;
33474 +       struct inode *h_idst;
33475 +
33476 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33477 +       err = ssz;
33478 +       if (unlikely(err <= 0)) {
33479 +               if (err == -ENODATA
33480 +                   || (err == -EOPNOTSUPP
33481 +                       && ((ignore_flags & au_xattr_out_of_list)
33482 +                           || (au_test_nfs_noacl(d_inode(h_src))
33483 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33484 +                                   || !strcmp(name,
33485 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
33486 +                           ))
33487 +                       err = 0;
33488 +               if (err && (verbose || au_debug_test()))
33489 +                       pr_err("%s, err %d\n", name, err);
33490 +               goto out;
33491 +       }
33492 +
33493 +       /* unlock it temporary */
33494 +       h_idst = d_inode(h_dst);
33495 +       mutex_unlock(&h_idst->i_mutex);
33496 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
33497 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33498 +       if (unlikely(err)) {
33499 +               if (verbose || au_debug_test())
33500 +                       pr_err("%s, err %d\n", name, err);
33501 +               err = au_xattr_ignore(err, name, ignore_flags);
33502 +       }
33503 +
33504 +out:
33505 +       return err;
33506 +}
33507 +
33508 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33509 +                 unsigned int verbose)
33510 +{
33511 +       int err, unlocked, acl_access, acl_default;
33512 +       ssize_t ssz;
33513 +       struct inode *h_isrc, *h_idst;
33514 +       char *value, *p, *o, *e;
33515 +
33516 +       /* try stopping to update the source inode while we are referencing */
33517 +       /* there should not be the parent-child relationship between them */
33518 +       h_isrc = d_inode(h_src);
33519 +       h_idst = d_inode(h_dst);
33520 +       mutex_unlock(&h_idst->i_mutex);
33521 +       mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
33522 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33523 +       unlocked = 0;
33524 +
33525 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
33526 +       ssz = vfs_listxattr(h_src, NULL, 0);
33527 +       err = ssz;
33528 +       if (unlikely(err < 0)) {
33529 +               AuTraceErr(err);
33530 +               if (err == -ENODATA
33531 +                   || err == -EOPNOTSUPP)
33532 +                       err = 0;        /* ignore */
33533 +               goto out;
33534 +       }
33535 +
33536 +       err = 0;
33537 +       p = NULL;
33538 +       o = NULL;
33539 +       if (ssz) {
33540 +               err = -ENOMEM;
33541 +               p = kmalloc(ssz, GFP_NOFS);
33542 +               o = p;
33543 +               if (unlikely(!p))
33544 +                       goto out;
33545 +               err = vfs_listxattr(h_src, p, ssz);
33546 +       }
33547 +       mutex_unlock(&h_isrc->i_mutex);
33548 +       unlocked = 1;
33549 +       AuDbg("err %d, ssz %zd\n", err, ssz);
33550 +       if (unlikely(err < 0))
33551 +               goto out_free;
33552 +
33553 +       err = 0;
33554 +       e = p + ssz;
33555 +       value = NULL;
33556 +       acl_access = 0;
33557 +       acl_default = 0;
33558 +       while (!err && p < e) {
33559 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33560 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33561 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33562 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33563 +                                       - 1);
33564 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33565 +                                      verbose);
33566 +               p += strlen(p) + 1;
33567 +       }
33568 +       AuTraceErr(err);
33569 +       ignore_flags |= au_xattr_out_of_list;
33570 +       if (!err && !acl_access) {
33571 +               err = au_do_cpup_xattr(h_dst, h_src,
33572 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
33573 +                                      ignore_flags, verbose);
33574 +               AuTraceErr(err);
33575 +       }
33576 +       if (!err && !acl_default) {
33577 +               err = au_do_cpup_xattr(h_dst, h_src,
33578 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
33579 +                                      ignore_flags, verbose);
33580 +               AuTraceErr(err);
33581 +       }
33582 +
33583 +       kfree(value);
33584 +
33585 +out_free:
33586 +       kfree(o);
33587 +out:
33588 +       if (!unlocked)
33589 +               mutex_unlock(&h_isrc->i_mutex);
33590 +       AuTraceErr(err);
33591 +       return err;
33592 +}
33593 +
33594 +/* ---------------------------------------------------------------------- */
33595 +
33596 +enum {
33597 +       AU_XATTR_LIST,
33598 +       AU_XATTR_GET
33599 +};
33600 +
33601 +struct au_lgxattr {
33602 +       int type;
33603 +       union {
33604 +               struct {
33605 +                       char    *list;
33606 +                       size_t  size;
33607 +               } list;
33608 +               struct {
33609 +                       const char      *name;
33610 +                       void            *value;
33611 +                       size_t          size;
33612 +               } get;
33613 +       } u;
33614 +};
33615 +
33616 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
33617 +{
33618 +       ssize_t err;
33619 +       struct path h_path;
33620 +       struct super_block *sb;
33621 +
33622 +       sb = dentry->d_sb;
33623 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
33624 +       if (unlikely(err))
33625 +               goto out;
33626 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path);
33627 +       if (unlikely(err))
33628 +               goto out_si;
33629 +       if (unlikely(!h_path.dentry))
33630 +               /* illegally overlapped or something */
33631 +               goto out_di; /* pretending success */
33632 +
33633 +       /* always topmost entry only */
33634 +       switch (arg->type) {
33635 +       case AU_XATTR_LIST:
33636 +               err = vfs_listxattr(h_path.dentry,
33637 +                                   arg->u.list.list, arg->u.list.size);
33638 +               break;
33639 +       case AU_XATTR_GET:
33640 +               err = vfs_getxattr(h_path.dentry,
33641 +                                  arg->u.get.name, arg->u.get.value,
33642 +                                  arg->u.get.size);
33643 +               break;
33644 +       }
33645 +
33646 +out_di:
33647 +       di_read_unlock(dentry, AuLock_IR);
33648 +out_si:
33649 +       si_read_unlock(sb);
33650 +out:
33651 +       AuTraceErr(err);
33652 +       return err;
33653 +}
33654 +
33655 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
33656 +{
33657 +       struct au_lgxattr arg = {
33658 +               .type = AU_XATTR_LIST,
33659 +               .u.list = {
33660 +                       .list   = list,
33661 +                       .size   = size
33662 +               },
33663 +       };
33664 +
33665 +       return au_lgxattr(dentry, &arg);
33666 +}
33667 +
33668 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
33669 +                     size_t size)
33670 +{
33671 +       struct au_lgxattr arg = {
33672 +               .type = AU_XATTR_GET,
33673 +               .u.get = {
33674 +                       .name   = name,
33675 +                       .value  = value,
33676 +                       .size   = size
33677 +               },
33678 +       };
33679 +
33680 +       return au_lgxattr(dentry, &arg);
33681 +}
33682 +
33683 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
33684 +                 size_t size, int flags)
33685 +{
33686 +       struct au_srxattr arg = {
33687 +               .type = AU_XATTR_SET,
33688 +               .u.set = {
33689 +                       .name   = name,
33690 +                       .value  = value,
33691 +                       .size   = size,
33692 +                       .flags  = flags
33693 +               },
33694 +       };
33695 +
33696 +       return au_srxattr(dentry, &arg);
33697 +}
33698 +
33699 +int aufs_removexattr(struct dentry *dentry, const char *name)
33700 +{
33701 +       struct au_srxattr arg = {
33702 +               .type = AU_XATTR_REMOVE,
33703 +               .u.remove = {
33704 +                       .name   = name
33705 +               },
33706 +       };
33707 +
33708 +       return au_srxattr(dentry, &arg);
33709 +}
33710 +
33711 +/* ---------------------------------------------------------------------- */
33712 +
33713 +#if 0
33714 +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33715 +                           const char *name, size_t name_len, int type)
33716 +{
33717 +       return aufs_listxattr(dentry, list, list_size);
33718 +}
33719 +
33720 +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33721 +                       size_t size, int type)
33722 +{
33723 +       return aufs_getxattr(dentry, name, buffer, size);
33724 +}
33725 +
33726 +static int au_xattr_set(struct dentry *dentry, const char *name,
33727 +                       const void *value, size_t size, int flags, int type)
33728 +{
33729 +       return aufs_setxattr(dentry, name, value, size, flags);
33730 +}
33731 +
33732 +static const struct xattr_handler au_xattr_handler = {
33733 +       /* no prefix, no flags */
33734 +       .list   = au_xattr_list,
33735 +       .get    = au_xattr_get,
33736 +       .set    = au_xattr_set
33737 +       /* why no remove? */
33738 +};
33739 +
33740 +static const struct xattr_handler *au_xattr_handlers[] = {
33741 +       &au_xattr_handler
33742 +};
33743 +
33744 +void au_xattr_init(struct super_block *sb)
33745 +{
33746 +       /* sb->s_xattr = au_xattr_handlers; */
33747 +}
33748 +#endif
33749 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33750 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
33751 +++ linux/fs/aufs/xino.c        2016-01-13 20:11:11.673093671 +0100
33752 @@ -0,0 +1,1318 @@
33753 +/*
33754 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33755 + *
33756 + * This program, aufs is free software; you can redistribute it and/or modify
33757 + * it under the terms of the GNU General Public License as published by
33758 + * the Free Software Foundation; either version 2 of the License, or
33759 + * (at your option) any later version.
33760 + *
33761 + * This program is distributed in the hope that it will be useful,
33762 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33763 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33764 + * GNU General Public License for more details.
33765 + *
33766 + * You should have received a copy of the GNU General Public License
33767 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33768 + */
33769 +
33770 +/*
33771 + * external inode number translation table and bitmap
33772 + */
33773 +
33774 +#include <linux/seq_file.h>
33775 +#include <linux/statfs.h>
33776 +#include "aufs.h"
33777 +
33778 +/* todo: unnecessary to support mmap_sem since kernel-space? */
33779 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
33780 +                  loff_t *pos)
33781 +{
33782 +       ssize_t err;
33783 +       mm_segment_t oldfs;
33784 +       union {
33785 +               void *k;
33786 +               char __user *u;
33787 +       } buf;
33788 +
33789 +       buf.k = kbuf;
33790 +       oldfs = get_fs();
33791 +       set_fs(KERNEL_DS);
33792 +       do {
33793 +               /* todo: signal_pending? */
33794 +               err = func(file, buf.u, size, pos);
33795 +       } while (err == -EAGAIN || err == -EINTR);
33796 +       set_fs(oldfs);
33797 +
33798 +#if 0 /* reserved for future use */
33799 +       if (err > 0)
33800 +               fsnotify_access(file->f_path.dentry);
33801 +#endif
33802 +
33803 +       return err;
33804 +}
33805 +
33806 +/* ---------------------------------------------------------------------- */
33807 +
33808 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
33809 +                              size_t size, loff_t *pos);
33810 +
33811 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
33812 +                             size_t size, loff_t *pos)
33813 +{
33814 +       ssize_t err;
33815 +       mm_segment_t oldfs;
33816 +       union {
33817 +               void *k;
33818 +               const char __user *u;
33819 +       } buf;
33820 +       int i;
33821 +       const int prevent_endless = 10;
33822 +
33823 +       i = 0;
33824 +       buf.k = kbuf;
33825 +       oldfs = get_fs();
33826 +       set_fs(KERNEL_DS);
33827 +       do {
33828 +               err = func(file, buf.u, size, pos);
33829 +               if (err == -EINTR
33830 +                   && !au_wkq_test()
33831 +                   && fatal_signal_pending(current)) {
33832 +                       set_fs(oldfs);
33833 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
33834 +                       BUG_ON(err == -EINTR);
33835 +                       oldfs = get_fs();
33836 +                       set_fs(KERNEL_DS);
33837 +               }
33838 +       } while (i++ < prevent_endless
33839 +                && (err == -EAGAIN || err == -EINTR));
33840 +       set_fs(oldfs);
33841 +
33842 +#if 0 /* reserved for future use */
33843 +       if (err > 0)
33844 +               fsnotify_modify(file->f_path.dentry);
33845 +#endif
33846 +
33847 +       return err;
33848 +}
33849 +
33850 +struct do_xino_fwrite_args {
33851 +       ssize_t *errp;
33852 +       vfs_writef_t func;
33853 +       struct file *file;
33854 +       void *buf;
33855 +       size_t size;
33856 +       loff_t *pos;
33857 +};
33858 +
33859 +static void call_do_xino_fwrite(void *args)
33860 +{
33861 +       struct do_xino_fwrite_args *a = args;
33862 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
33863 +}
33864 +
33865 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
33866 +                              size_t size, loff_t *pos)
33867 +{
33868 +       ssize_t err;
33869 +       int wkq_err;
33870 +       struct do_xino_fwrite_args args = {
33871 +               .errp   = &err,
33872 +               .func   = func,
33873 +               .file   = file,
33874 +               .buf    = buf,
33875 +               .size   = size,
33876 +               .pos    = pos
33877 +       };
33878 +
33879 +       /*
33880 +        * it breaks RLIMIT_FSIZE and normal user's limit,
33881 +        * users should care about quota and real 'filesystem full.'
33882 +        */
33883 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
33884 +       if (unlikely(wkq_err))
33885 +               err = wkq_err;
33886 +
33887 +       return err;
33888 +}
33889 +
33890 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
33891 +                   size_t size, loff_t *pos)
33892 +{
33893 +       ssize_t err;
33894 +
33895 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
33896 +               lockdep_off();
33897 +               err = do_xino_fwrite(func, file, buf, size, pos);
33898 +               lockdep_on();
33899 +       } else
33900 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
33901 +
33902 +       return err;
33903 +}
33904 +
33905 +/* ---------------------------------------------------------------------- */
33906 +
33907 +/*
33908 + * create a new xinofile at the same place/path as @base_file.
33909 + */
33910 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
33911 +{
33912 +       struct file *file;
33913 +       struct dentry *base, *parent;
33914 +       struct inode *dir, *delegated;
33915 +       struct qstr *name;
33916 +       struct path path;
33917 +       int err;
33918 +
33919 +       base = base_file->f_path.dentry;
33920 +       parent = base->d_parent; /* dir inode is locked */
33921 +       dir = d_inode(parent);
33922 +       IMustLock(dir);
33923 +
33924 +       file = ERR_PTR(-EINVAL);
33925 +       name = &base->d_name;
33926 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
33927 +       if (IS_ERR(path.dentry)) {
33928 +               file = (void *)path.dentry;
33929 +               pr_err("%pd lookup err %ld\n",
33930 +                      base, PTR_ERR(path.dentry));
33931 +               goto out;
33932 +       }
33933 +
33934 +       /* no need to mnt_want_write() since we call dentry_open() later */
33935 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
33936 +       if (unlikely(err)) {
33937 +               file = ERR_PTR(err);
33938 +               pr_err("%pd create err %d\n", base, err);
33939 +               goto out_dput;
33940 +       }
33941 +
33942 +       path.mnt = base_file->f_path.mnt;
33943 +       file = vfsub_dentry_open(&path,
33944 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
33945 +                                /* | __FMODE_NONOTIFY */);
33946 +       if (IS_ERR(file)) {
33947 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
33948 +               goto out_dput;
33949 +       }
33950 +
33951 +       delegated = NULL;
33952 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
33953 +       if (unlikely(err == -EWOULDBLOCK)) {
33954 +               pr_warn("cannot retry for NFSv4 delegation"
33955 +                       " for an internal unlink\n");
33956 +               iput(delegated);
33957 +       }
33958 +       if (unlikely(err)) {
33959 +               pr_err("%pd unlink err %d\n", base, err);
33960 +               goto out_fput;
33961 +       }
33962 +
33963 +       if (copy_src) {
33964 +               /* no one can touch copy_src xino */
33965 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
33966 +               if (unlikely(err)) {
33967 +                       pr_err("%pd copy err %d\n", base, err);
33968 +                       goto out_fput;
33969 +               }
33970 +       }
33971 +       goto out_dput; /* success */
33972 +
33973 +out_fput:
33974 +       fput(file);
33975 +       file = ERR_PTR(err);
33976 +out_dput:
33977 +       dput(path.dentry);
33978 +out:
33979 +       return file;
33980 +}
33981 +
33982 +struct au_xino_lock_dir {
33983 +       struct au_hinode *hdir;
33984 +       struct dentry *parent;
33985 +       struct mutex *mtx;
33986 +};
33987 +
33988 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
33989 +                            struct au_xino_lock_dir *ldir)
33990 +{
33991 +       aufs_bindex_t brid, bindex;
33992 +
33993 +       ldir->hdir = NULL;
33994 +       bindex = -1;
33995 +       brid = au_xino_brid(sb);
33996 +       if (brid >= 0)
33997 +               bindex = au_br_index(sb, brid);
33998 +       if (bindex >= 0) {
33999 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
34000 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
34001 +       } else {
34002 +               ldir->parent = dget_parent(xino->f_path.dentry);
34003 +               ldir->mtx = &d_inode(ldir->parent)->i_mutex;
34004 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
34005 +       }
34006 +}
34007 +
34008 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34009 +{
34010 +       if (ldir->hdir)
34011 +               au_hn_imtx_unlock(ldir->hdir);
34012 +       else {
34013 +               mutex_unlock(ldir->mtx);
34014 +               dput(ldir->parent);
34015 +       }
34016 +}
34017 +
34018 +/* ---------------------------------------------------------------------- */
34019 +
34020 +/* trucate xino files asynchronously */
34021 +
34022 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34023 +{
34024 +       int err;
34025 +       unsigned long jiffy;
34026 +       blkcnt_t blocks;
34027 +       aufs_bindex_t bi, bend;
34028 +       struct kstatfs *st;
34029 +       struct au_branch *br;
34030 +       struct file *new_xino, *file;
34031 +       struct super_block *h_sb;
34032 +       struct au_xino_lock_dir ldir;
34033 +
34034 +       err = -ENOMEM;
34035 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34036 +       if (unlikely(!st))
34037 +               goto out;
34038 +
34039 +       err = -EINVAL;
34040 +       bend = au_sbend(sb);
34041 +       if (unlikely(bindex < 0 || bend < bindex))
34042 +               goto out_st;
34043 +       br = au_sbr(sb, bindex);
34044 +       file = br->br_xino.xi_file;
34045 +       if (!file)
34046 +               goto out_st;
34047 +
34048 +       err = vfs_statfs(&file->f_path, st);
34049 +       if (unlikely(err))
34050 +               AuErr1("statfs err %d, ignored\n", err);
34051 +       jiffy = jiffies;
34052 +       blocks = file_inode(file)->i_blocks;
34053 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34054 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
34055 +
34056 +       au_xino_lock_dir(sb, file, &ldir);
34057 +       /* mnt_want_write() is unnecessary here */
34058 +       new_xino = au_xino_create2(file, file);
34059 +       au_xino_unlock_dir(&ldir);
34060 +       err = PTR_ERR(new_xino);
34061 +       if (IS_ERR(new_xino)) {
34062 +               pr_err("err %d, ignored\n", err);
34063 +               goto out_st;
34064 +       }
34065 +       err = 0;
34066 +       fput(file);
34067 +       br->br_xino.xi_file = new_xino;
34068 +
34069 +       h_sb = au_br_sb(br);
34070 +       for (bi = 0; bi <= bend; bi++) {
34071 +               if (unlikely(bi == bindex))
34072 +                       continue;
34073 +               br = au_sbr(sb, bi);
34074 +               if (au_br_sb(br) != h_sb)
34075 +                       continue;
34076 +
34077 +               fput(br->br_xino.xi_file);
34078 +               br->br_xino.xi_file = new_xino;
34079 +               get_file(new_xino);
34080 +       }
34081 +
34082 +       err = vfs_statfs(&new_xino->f_path, st);
34083 +       if (!err) {
34084 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34085 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
34086 +                       st->f_bfree, st->f_blocks);
34087 +               if (file_inode(new_xino)->i_blocks < blocks)
34088 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
34089 +       } else
34090 +               AuErr1("statfs err %d, ignored\n", err);
34091 +
34092 +out_st:
34093 +       kfree(st);
34094 +out:
34095 +       return err;
34096 +}
34097 +
34098 +struct xino_do_trunc_args {
34099 +       struct super_block *sb;
34100 +       struct au_branch *br;
34101 +};
34102 +
34103 +static void xino_do_trunc(void *_args)
34104 +{
34105 +       struct xino_do_trunc_args *args = _args;
34106 +       struct super_block *sb;
34107 +       struct au_branch *br;
34108 +       struct inode *dir;
34109 +       int err;
34110 +       aufs_bindex_t bindex;
34111 +
34112 +       err = 0;
34113 +       sb = args->sb;
34114 +       dir = d_inode(sb->s_root);
34115 +       br = args->br;
34116 +
34117 +       si_noflush_write_lock(sb);
34118 +       ii_read_lock_parent(dir);
34119 +       bindex = au_br_index(sb, br->br_id);
34120 +       err = au_xino_trunc(sb, bindex);
34121 +       ii_read_unlock(dir);
34122 +       if (unlikely(err))
34123 +               pr_warn("err b%d, (%d)\n", bindex, err);
34124 +       atomic_dec(&br->br_xino_running);
34125 +       atomic_dec(&br->br_count);
34126 +       si_write_unlock(sb);
34127 +       au_nwt_done(&au_sbi(sb)->si_nowait);
34128 +       kfree(args);
34129 +}
34130 +
34131 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34132 +{
34133 +       int err;
34134 +       struct kstatfs st;
34135 +       struct au_sbinfo *sbinfo;
34136 +
34137 +       /* todo: si_xino_expire and the ratio should be customizable */
34138 +       sbinfo = au_sbi(sb);
34139 +       if (time_before(jiffies,
34140 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34141 +               return 0;
34142 +
34143 +       /* truncation border */
34144 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34145 +       if (unlikely(err)) {
34146 +               AuErr1("statfs err %d, ignored\n", err);
34147 +               return 0;
34148 +       }
34149 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34150 +               return 0;
34151 +
34152 +       return 1;
34153 +}
34154 +
34155 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34156 +{
34157 +       struct xino_do_trunc_args *args;
34158 +       int wkq_err;
34159 +
34160 +       if (!xino_trunc_test(sb, br))
34161 +               return;
34162 +
34163 +       if (atomic_inc_return(&br->br_xino_running) > 1)
34164 +               goto out;
34165 +
34166 +       /* lock and kfree() will be called in trunc_xino() */
34167 +       args = kmalloc(sizeof(*args), GFP_NOFS);
34168 +       if (unlikely(!args)) {
34169 +               AuErr1("no memory\n");
34170 +               goto out_args;
34171 +       }
34172 +
34173 +       atomic_inc(&br->br_count);
34174 +       args->sb = sb;
34175 +       args->br = br;
34176 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
34177 +       if (!wkq_err)
34178 +               return; /* success */
34179 +
34180 +       pr_err("wkq %d\n", wkq_err);
34181 +       atomic_dec(&br->br_count);
34182 +
34183 +out_args:
34184 +       kfree(args);
34185 +out:
34186 +       atomic_dec(&br->br_xino_running);
34187 +}
34188 +
34189 +/* ---------------------------------------------------------------------- */
34190 +
34191 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
34192 +                           ino_t h_ino, ino_t ino)
34193 +{
34194 +       loff_t pos;
34195 +       ssize_t sz;
34196 +
34197 +       pos = h_ino;
34198 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34199 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34200 +               return -EFBIG;
34201 +       }
34202 +       pos *= sizeof(ino);
34203 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34204 +       if (sz == sizeof(ino))
34205 +               return 0; /* success */
34206 +
34207 +       AuIOErr("write failed (%zd)\n", sz);
34208 +       return -EIO;
34209 +}
34210 +
34211 +/*
34212 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34213 + * at the position of @h_ino.
34214 + * even if @ino is zero, it is written to the xinofile and means no entry.
34215 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34216 + * try truncating it.
34217 + */
34218 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34219 +                 ino_t ino)
34220 +{
34221 +       int err;
34222 +       unsigned int mnt_flags;
34223 +       struct au_branch *br;
34224 +
34225 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34226 +                    || ((loff_t)-1) > 0);
34227 +       SiMustAnyLock(sb);
34228 +
34229 +       mnt_flags = au_mntflags(sb);
34230 +       if (!au_opt_test(mnt_flags, XINO))
34231 +               return 0;
34232 +
34233 +       br = au_sbr(sb, bindex);
34234 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34235 +                              h_ino, ino);
34236 +       if (!err) {
34237 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34238 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34239 +                       xino_try_trunc(sb, br);
34240 +               return 0; /* success */
34241 +       }
34242 +
34243 +       AuIOErr("write failed (%d)\n", err);
34244 +       return -EIO;
34245 +}
34246 +
34247 +/* ---------------------------------------------------------------------- */
34248 +
34249 +/* aufs inode number bitmap */
34250 +
34251 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34252 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34253 +{
34254 +       ino_t ino;
34255 +
34256 +       AuDebugOn(bit < 0 || page_bits <= bit);
34257 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34258 +       return ino;
34259 +}
34260 +
34261 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34262 +{
34263 +       AuDebugOn(ino < AUFS_FIRST_INO);
34264 +       ino -= AUFS_FIRST_INO;
34265 +       *pindex = ino / page_bits;
34266 +       *bit = ino % page_bits;
34267 +}
34268 +
34269 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
34270 +{
34271 +       int err;
34272 +       loff_t pos;
34273 +       ssize_t sz;
34274 +       struct au_sbinfo *sbinfo;
34275 +       struct file *xib;
34276 +       unsigned long *p;
34277 +
34278 +       sbinfo = au_sbi(sb);
34279 +       MtxMustLock(&sbinfo->si_xib_mtx);
34280 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34281 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
34282 +
34283 +       if (pindex == sbinfo->si_xib_last_pindex)
34284 +               return 0;
34285 +
34286 +       xib = sbinfo->si_xib;
34287 +       p = sbinfo->si_xib_buf;
34288 +       pos = sbinfo->si_xib_last_pindex;
34289 +       pos *= PAGE_SIZE;
34290 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34291 +       if (unlikely(sz != PAGE_SIZE))
34292 +               goto out;
34293 +
34294 +       pos = pindex;
34295 +       pos *= PAGE_SIZE;
34296 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
34297 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34298 +       else {
34299 +               memset(p, 0, PAGE_SIZE);
34300 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34301 +       }
34302 +       if (sz == PAGE_SIZE) {
34303 +               sbinfo->si_xib_last_pindex = pindex;
34304 +               return 0; /* success */
34305 +       }
34306 +
34307 +out:
34308 +       AuIOErr1("write failed (%zd)\n", sz);
34309 +       err = sz;
34310 +       if (sz >= 0)
34311 +               err = -EIO;
34312 +       return err;
34313 +}
34314 +
34315 +/* ---------------------------------------------------------------------- */
34316 +
34317 +static void au_xib_clear_bit(struct inode *inode)
34318 +{
34319 +       int err, bit;
34320 +       unsigned long pindex;
34321 +       struct super_block *sb;
34322 +       struct au_sbinfo *sbinfo;
34323 +
34324 +       AuDebugOn(inode->i_nlink);
34325 +
34326 +       sb = inode->i_sb;
34327 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
34328 +       AuDebugOn(page_bits <= bit);
34329 +       sbinfo = au_sbi(sb);
34330 +       mutex_lock(&sbinfo->si_xib_mtx);
34331 +       err = xib_pindex(sb, pindex);
34332 +       if (!err) {
34333 +               clear_bit(bit, sbinfo->si_xib_buf);
34334 +               sbinfo->si_xib_next_bit = bit;
34335 +       }
34336 +       mutex_unlock(&sbinfo->si_xib_mtx);
34337 +}
34338 +
34339 +/* for s_op->delete_inode() */
34340 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
34341 +{
34342 +       int err;
34343 +       unsigned int mnt_flags;
34344 +       aufs_bindex_t bindex, bend, bi;
34345 +       unsigned char try_trunc;
34346 +       struct au_iinfo *iinfo;
34347 +       struct super_block *sb;
34348 +       struct au_hinode *hi;
34349 +       struct inode *h_inode;
34350 +       struct au_branch *br;
34351 +       vfs_writef_t xwrite;
34352 +
34353 +       sb = inode->i_sb;
34354 +       mnt_flags = au_mntflags(sb);
34355 +       if (!au_opt_test(mnt_flags, XINO)
34356 +           || inode->i_ino == AUFS_ROOT_INO)
34357 +               return;
34358 +
34359 +       if (unlinked) {
34360 +               au_xigen_inc(inode);
34361 +               au_xib_clear_bit(inode);
34362 +       }
34363 +
34364 +       iinfo = au_ii(inode);
34365 +       if (!iinfo)
34366 +               return;
34367 +
34368 +       bindex = iinfo->ii_bstart;
34369 +       if (bindex < 0)
34370 +               return;
34371 +
34372 +       xwrite = au_sbi(sb)->si_xwrite;
34373 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
34374 +       hi = iinfo->ii_hinode + bindex;
34375 +       bend = iinfo->ii_bend;
34376 +       for (; bindex <= bend; bindex++, hi++) {
34377 +               h_inode = hi->hi_inode;
34378 +               if (!h_inode
34379 +                   || (!unlinked && h_inode->i_nlink))
34380 +                       continue;
34381 +
34382 +               /* inode may not be revalidated */
34383 +               bi = au_br_index(sb, hi->hi_id);
34384 +               if (bi < 0)
34385 +                       continue;
34386 +
34387 +               br = au_sbr(sb, bi);
34388 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34389 +                                      h_inode->i_ino, /*ino*/0);
34390 +               if (!err && try_trunc
34391 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34392 +                       xino_try_trunc(sb, br);
34393 +       }
34394 +}
34395 +
34396 +/* get an unused inode number from bitmap */
34397 +ino_t au_xino_new_ino(struct super_block *sb)
34398 +{
34399 +       ino_t ino;
34400 +       unsigned long *p, pindex, ul, pend;
34401 +       struct au_sbinfo *sbinfo;
34402 +       struct file *file;
34403 +       int free_bit, err;
34404 +
34405 +       if (!au_opt_test(au_mntflags(sb), XINO))
34406 +               return iunique(sb, AUFS_FIRST_INO);
34407 +
34408 +       sbinfo = au_sbi(sb);
34409 +       mutex_lock(&sbinfo->si_xib_mtx);
34410 +       p = sbinfo->si_xib_buf;
34411 +       free_bit = sbinfo->si_xib_next_bit;
34412 +       if (free_bit < page_bits && !test_bit(free_bit, p))
34413 +               goto out; /* success */
34414 +       free_bit = find_first_zero_bit(p, page_bits);
34415 +       if (free_bit < page_bits)
34416 +               goto out; /* success */
34417 +
34418 +       pindex = sbinfo->si_xib_last_pindex;
34419 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34420 +               err = xib_pindex(sb, ul);
34421 +               if (unlikely(err))
34422 +                       goto out_err;
34423 +               free_bit = find_first_zero_bit(p, page_bits);
34424 +               if (free_bit < page_bits)
34425 +                       goto out; /* success */
34426 +       }
34427 +
34428 +       file = sbinfo->si_xib;
34429 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
34430 +       for (ul = pindex + 1; ul <= pend; ul++) {
34431 +               err = xib_pindex(sb, ul);
34432 +               if (unlikely(err))
34433 +                       goto out_err;
34434 +               free_bit = find_first_zero_bit(p, page_bits);
34435 +               if (free_bit < page_bits)
34436 +                       goto out; /* success */
34437 +       }
34438 +       BUG();
34439 +
34440 +out:
34441 +       set_bit(free_bit, p);
34442 +       sbinfo->si_xib_next_bit = free_bit + 1;
34443 +       pindex = sbinfo->si_xib_last_pindex;
34444 +       mutex_unlock(&sbinfo->si_xib_mtx);
34445 +       ino = xib_calc_ino(pindex, free_bit);
34446 +       AuDbg("i%lu\n", (unsigned long)ino);
34447 +       return ino;
34448 +out_err:
34449 +       mutex_unlock(&sbinfo->si_xib_mtx);
34450 +       AuDbg("i0\n");
34451 +       return 0;
34452 +}
34453 +
34454 +/*
34455 + * read @ino from xinofile for the specified branch{@sb, @bindex}
34456 + * at the position of @h_ino.
34457 + * if @ino does not exist and @do_new is true, get new one.
34458 + */
34459 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34460 +                ino_t *ino)
34461 +{
34462 +       int err;
34463 +       ssize_t sz;
34464 +       loff_t pos;
34465 +       struct file *file;
34466 +       struct au_sbinfo *sbinfo;
34467 +
34468 +       *ino = 0;
34469 +       if (!au_opt_test(au_mntflags(sb), XINO))
34470 +               return 0; /* no xino */
34471 +
34472 +       err = 0;
34473 +       sbinfo = au_sbi(sb);
34474 +       pos = h_ino;
34475 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34476 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34477 +               return -EFBIG;
34478 +       }
34479 +       pos *= sizeof(*ino);
34480 +
34481 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
34482 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
34483 +               return 0; /* no ino */
34484 +
34485 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34486 +       if (sz == sizeof(*ino))
34487 +               return 0; /* success */
34488 +
34489 +       err = sz;
34490 +       if (unlikely(sz >= 0)) {
34491 +               err = -EIO;
34492 +               AuIOErr("xino read error (%zd)\n", sz);
34493 +       }
34494 +
34495 +       return err;
34496 +}
34497 +
34498 +/* ---------------------------------------------------------------------- */
34499 +
34500 +/* create and set a new xino file */
34501 +
34502 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34503 +{
34504 +       struct file *file;
34505 +       struct dentry *h_parent, *d;
34506 +       struct inode *h_dir, *inode;
34507 +       int err;
34508 +
34509 +       /*
34510 +        * at mount-time, and the xino file is the default path,
34511 +        * hnotify is disabled so we have no notify events to ignore.
34512 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
34513 +        */
34514 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34515 +                              /* | __FMODE_NONOTIFY */,
34516 +                              S_IRUGO | S_IWUGO);
34517 +       if (IS_ERR(file)) {
34518 +               if (!silent)
34519 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
34520 +               return file;
34521 +       }
34522 +
34523 +       /* keep file count */
34524 +       err = 0;
34525 +       inode = file_inode(file);
34526 +       h_parent = dget_parent(file->f_path.dentry);
34527 +       h_dir = d_inode(h_parent);
34528 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
34529 +       /* mnt_want_write() is unnecessary here */
34530 +       /* no delegation since it is just created */
34531 +       if (inode->i_nlink)
34532 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34533 +                                  /*force*/0);
34534 +       mutex_unlock(&h_dir->i_mutex);
34535 +       dput(h_parent);
34536 +       if (unlikely(err)) {
34537 +               if (!silent)
34538 +                       pr_err("unlink %s(%d)\n", fname, err);
34539 +               goto out;
34540 +       }
34541 +
34542 +       err = -EINVAL;
34543 +       d = file->f_path.dentry;
34544 +       if (unlikely(sb == d->d_sb)) {
34545 +               if (!silent)
34546 +                       pr_err("%s must be outside\n", fname);
34547 +               goto out;
34548 +       }
34549 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34550 +               if (!silent)
34551 +                       pr_err("xino doesn't support %s(%s)\n",
34552 +                              fname, au_sbtype(d->d_sb));
34553 +               goto out;
34554 +       }
34555 +       return file; /* success */
34556 +
34557 +out:
34558 +       fput(file);
34559 +       file = ERR_PTR(err);
34560 +       return file;
34561 +}
34562 +
34563 +/*
34564 + * find another branch who is on the same filesystem of the specified
34565 + * branch{@btgt}. search until @bend.
34566 + */
34567 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
34568 +                       aufs_bindex_t bend)
34569 +{
34570 +       aufs_bindex_t bindex;
34571 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34572 +
34573 +       for (bindex = 0; bindex < btgt; bindex++)
34574 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34575 +                       return bindex;
34576 +       for (bindex++; bindex <= bend; bindex++)
34577 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34578 +                       return bindex;
34579 +       return -1;
34580 +}
34581 +
34582 +/* ---------------------------------------------------------------------- */
34583 +
34584 +/*
34585 + * initialize the xinofile for the specified branch @br
34586 + * at the place/path where @base_file indicates.
34587 + * test whether another branch is on the same filesystem or not,
34588 + * if @do_test is true.
34589 + */
34590 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34591 +              struct file *base_file, int do_test)
34592 +{
34593 +       int err;
34594 +       ino_t ino;
34595 +       aufs_bindex_t bend, bindex;
34596 +       struct au_branch *shared_br, *b;
34597 +       struct file *file;
34598 +       struct super_block *tgt_sb;
34599 +
34600 +       shared_br = NULL;
34601 +       bend = au_sbend(sb);
34602 +       if (do_test) {
34603 +               tgt_sb = au_br_sb(br);
34604 +               for (bindex = 0; bindex <= bend; bindex++) {
34605 +                       b = au_sbr(sb, bindex);
34606 +                       if (tgt_sb == au_br_sb(b)) {
34607 +                               shared_br = b;
34608 +                               break;
34609 +                       }
34610 +               }
34611 +       }
34612 +
34613 +       if (!shared_br || !shared_br->br_xino.xi_file) {
34614 +               struct au_xino_lock_dir ldir;
34615 +
34616 +               au_xino_lock_dir(sb, base_file, &ldir);
34617 +               /* mnt_want_write() is unnecessary here */
34618 +               file = au_xino_create2(base_file, NULL);
34619 +               au_xino_unlock_dir(&ldir);
34620 +               err = PTR_ERR(file);
34621 +               if (IS_ERR(file))
34622 +                       goto out;
34623 +               br->br_xino.xi_file = file;
34624 +       } else {
34625 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
34626 +               get_file(br->br_xino.xi_file);
34627 +       }
34628 +
34629 +       ino = AUFS_ROOT_INO;
34630 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34631 +                              h_ino, ino);
34632 +       if (unlikely(err)) {
34633 +               fput(br->br_xino.xi_file);
34634 +               br->br_xino.xi_file = NULL;
34635 +       }
34636 +
34637 +out:
34638 +       return err;
34639 +}
34640 +
34641 +/* ---------------------------------------------------------------------- */
34642 +
34643 +/* trucate a xino bitmap file */
34644 +
34645 +/* todo: slow */
34646 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
34647 +{
34648 +       int err, bit;
34649 +       ssize_t sz;
34650 +       unsigned long pindex;
34651 +       loff_t pos, pend;
34652 +       struct au_sbinfo *sbinfo;
34653 +       vfs_readf_t func;
34654 +       ino_t *ino;
34655 +       unsigned long *p;
34656 +
34657 +       err = 0;
34658 +       sbinfo = au_sbi(sb);
34659 +       MtxMustLock(&sbinfo->si_xib_mtx);
34660 +       p = sbinfo->si_xib_buf;
34661 +       func = sbinfo->si_xread;
34662 +       pend = vfsub_f_size_read(file);
34663 +       pos = 0;
34664 +       while (pos < pend) {
34665 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
34666 +               err = sz;
34667 +               if (unlikely(sz <= 0))
34668 +                       goto out;
34669 +
34670 +               err = 0;
34671 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
34672 +                       if (unlikely(*ino < AUFS_FIRST_INO))
34673 +                               continue;
34674 +
34675 +                       xib_calc_bit(*ino, &pindex, &bit);
34676 +                       AuDebugOn(page_bits <= bit);
34677 +                       err = xib_pindex(sb, pindex);
34678 +                       if (!err)
34679 +                               set_bit(bit, p);
34680 +                       else
34681 +                               goto out;
34682 +               }
34683 +       }
34684 +
34685 +out:
34686 +       return err;
34687 +}
34688 +
34689 +static int xib_restore(struct super_block *sb)
34690 +{
34691 +       int err;
34692 +       aufs_bindex_t bindex, bend;
34693 +       void *page;
34694 +
34695 +       err = -ENOMEM;
34696 +       page = (void *)__get_free_page(GFP_NOFS);
34697 +       if (unlikely(!page))
34698 +               goto out;
34699 +
34700 +       err = 0;
34701 +       bend = au_sbend(sb);
34702 +       for (bindex = 0; !err && bindex <= bend; bindex++)
34703 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
34704 +                       err = do_xib_restore
34705 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
34706 +               else
34707 +                       AuDbg("b%d\n", bindex);
34708 +       free_page((unsigned long)page);
34709 +
34710 +out:
34711 +       return err;
34712 +}
34713 +
34714 +int au_xib_trunc(struct super_block *sb)
34715 +{
34716 +       int err;
34717 +       ssize_t sz;
34718 +       loff_t pos;
34719 +       struct au_xino_lock_dir ldir;
34720 +       struct au_sbinfo *sbinfo;
34721 +       unsigned long *p;
34722 +       struct file *file;
34723 +
34724 +       SiMustWriteLock(sb);
34725 +
34726 +       err = 0;
34727 +       sbinfo = au_sbi(sb);
34728 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
34729 +               goto out;
34730 +
34731 +       file = sbinfo->si_xib;
34732 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
34733 +               goto out;
34734 +
34735 +       au_xino_lock_dir(sb, file, &ldir);
34736 +       /* mnt_want_write() is unnecessary here */
34737 +       file = au_xino_create2(sbinfo->si_xib, NULL);
34738 +       au_xino_unlock_dir(&ldir);
34739 +       err = PTR_ERR(file);
34740 +       if (IS_ERR(file))
34741 +               goto out;
34742 +       fput(sbinfo->si_xib);
34743 +       sbinfo->si_xib = file;
34744 +
34745 +       p = sbinfo->si_xib_buf;
34746 +       memset(p, 0, PAGE_SIZE);
34747 +       pos = 0;
34748 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34749 +       if (unlikely(sz != PAGE_SIZE)) {
34750 +               err = sz;
34751 +               AuIOErr("err %d\n", err);
34752 +               if (sz >= 0)
34753 +                       err = -EIO;
34754 +               goto out;
34755 +       }
34756 +
34757 +       mutex_lock(&sbinfo->si_xib_mtx);
34758 +       /* mnt_want_write() is unnecessary here */
34759 +       err = xib_restore(sb);
34760 +       mutex_unlock(&sbinfo->si_xib_mtx);
34761 +
34762 +out:
34763 +       return err;
34764 +}
34765 +
34766 +/* ---------------------------------------------------------------------- */
34767 +
34768 +/*
34769 + * xino mount option handlers
34770 + */
34771 +
34772 +/* xino bitmap */
34773 +static void xino_clear_xib(struct super_block *sb)
34774 +{
34775 +       struct au_sbinfo *sbinfo;
34776 +
34777 +       SiMustWriteLock(sb);
34778 +
34779 +       sbinfo = au_sbi(sb);
34780 +       sbinfo->si_xread = NULL;
34781 +       sbinfo->si_xwrite = NULL;
34782 +       if (sbinfo->si_xib)
34783 +               fput(sbinfo->si_xib);
34784 +       sbinfo->si_xib = NULL;
34785 +       free_page((unsigned long)sbinfo->si_xib_buf);
34786 +       sbinfo->si_xib_buf = NULL;
34787 +}
34788 +
34789 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
34790 +{
34791 +       int err;
34792 +       loff_t pos;
34793 +       struct au_sbinfo *sbinfo;
34794 +       struct file *file;
34795 +
34796 +       SiMustWriteLock(sb);
34797 +
34798 +       sbinfo = au_sbi(sb);
34799 +       file = au_xino_create2(base, sbinfo->si_xib);
34800 +       err = PTR_ERR(file);
34801 +       if (IS_ERR(file))
34802 +               goto out;
34803 +       if (sbinfo->si_xib)
34804 +               fput(sbinfo->si_xib);
34805 +       sbinfo->si_xib = file;
34806 +       sbinfo->si_xread = vfs_readf(file);
34807 +       sbinfo->si_xwrite = vfs_writef(file);
34808 +
34809 +       err = -ENOMEM;
34810 +       if (!sbinfo->si_xib_buf)
34811 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
34812 +       if (unlikely(!sbinfo->si_xib_buf))
34813 +               goto out_unset;
34814 +
34815 +       sbinfo->si_xib_last_pindex = 0;
34816 +       sbinfo->si_xib_next_bit = 0;
34817 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
34818 +               pos = 0;
34819 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
34820 +                                 PAGE_SIZE, &pos);
34821 +               if (unlikely(err != PAGE_SIZE))
34822 +                       goto out_free;
34823 +       }
34824 +       err = 0;
34825 +       goto out; /* success */
34826 +
34827 +out_free:
34828 +       free_page((unsigned long)sbinfo->si_xib_buf);
34829 +       sbinfo->si_xib_buf = NULL;
34830 +       if (err >= 0)
34831 +               err = -EIO;
34832 +out_unset:
34833 +       fput(sbinfo->si_xib);
34834 +       sbinfo->si_xib = NULL;
34835 +       sbinfo->si_xread = NULL;
34836 +       sbinfo->si_xwrite = NULL;
34837 +out:
34838 +       return err;
34839 +}
34840 +
34841 +/* xino for each branch */
34842 +static void xino_clear_br(struct super_block *sb)
34843 +{
34844 +       aufs_bindex_t bindex, bend;
34845 +       struct au_branch *br;
34846 +
34847 +       bend = au_sbend(sb);
34848 +       for (bindex = 0; bindex <= bend; bindex++) {
34849 +               br = au_sbr(sb, bindex);
34850 +               if (!br || !br->br_xino.xi_file)
34851 +                       continue;
34852 +
34853 +               fput(br->br_xino.xi_file);
34854 +               br->br_xino.xi_file = NULL;
34855 +       }
34856 +}
34857 +
34858 +static int au_xino_set_br(struct super_block *sb, struct file *base)
34859 +{
34860 +       int err;
34861 +       ino_t ino;
34862 +       aufs_bindex_t bindex, bend, bshared;
34863 +       struct {
34864 +               struct file *old, *new;
34865 +       } *fpair, *p;
34866 +       struct au_branch *br;
34867 +       struct inode *inode;
34868 +       vfs_writef_t writef;
34869 +
34870 +       SiMustWriteLock(sb);
34871 +
34872 +       err = -ENOMEM;
34873 +       bend = au_sbend(sb);
34874 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
34875 +       if (unlikely(!fpair))
34876 +               goto out;
34877 +
34878 +       inode = d_inode(sb->s_root);
34879 +       ino = AUFS_ROOT_INO;
34880 +       writef = au_sbi(sb)->si_xwrite;
34881 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34882 +               br = au_sbr(sb, bindex);
34883 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
34884 +               if (bshared >= 0) {
34885 +                       /* shared xino */
34886 +                       *p = fpair[bshared];
34887 +                       get_file(p->new);
34888 +               }
34889 +
34890 +               if (!p->new) {
34891 +                       /* new xino */
34892 +                       p->old = br->br_xino.xi_file;
34893 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
34894 +                       err = PTR_ERR(p->new);
34895 +                       if (IS_ERR(p->new)) {
34896 +                               p->new = NULL;
34897 +                               goto out_pair;
34898 +                       }
34899 +               }
34900 +
34901 +               err = au_xino_do_write(writef, p->new,
34902 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
34903 +               if (unlikely(err))
34904 +                       goto out_pair;
34905 +       }
34906 +
34907 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34908 +               br = au_sbr(sb, bindex);
34909 +               if (br->br_xino.xi_file)
34910 +                       fput(br->br_xino.xi_file);
34911 +               get_file(p->new);
34912 +               br->br_xino.xi_file = p->new;
34913 +       }
34914 +
34915 +out_pair:
34916 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
34917 +               if (p->new)
34918 +                       fput(p->new);
34919 +               else
34920 +                       break;
34921 +       kfree(fpair);
34922 +out:
34923 +       return err;
34924 +}
34925 +
34926 +void au_xino_clr(struct super_block *sb)
34927 +{
34928 +       struct au_sbinfo *sbinfo;
34929 +
34930 +       au_xigen_clr(sb);
34931 +       xino_clear_xib(sb);
34932 +       xino_clear_br(sb);
34933 +       sbinfo = au_sbi(sb);
34934 +       /* lvalue, do not call au_mntflags() */
34935 +       au_opt_clr(sbinfo->si_mntflags, XINO);
34936 +}
34937 +
34938 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
34939 +{
34940 +       int err, skip;
34941 +       struct dentry *parent, *cur_parent;
34942 +       struct qstr *dname, *cur_name;
34943 +       struct file *cur_xino;
34944 +       struct inode *dir;
34945 +       struct au_sbinfo *sbinfo;
34946 +
34947 +       SiMustWriteLock(sb);
34948 +
34949 +       err = 0;
34950 +       sbinfo = au_sbi(sb);
34951 +       parent = dget_parent(xino->file->f_path.dentry);
34952 +       if (remount) {
34953 +               skip = 0;
34954 +               dname = &xino->file->f_path.dentry->d_name;
34955 +               cur_xino = sbinfo->si_xib;
34956 +               if (cur_xino) {
34957 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
34958 +                       cur_name = &cur_xino->f_path.dentry->d_name;
34959 +                       skip = (cur_parent == parent
34960 +                               && au_qstreq(dname, cur_name));
34961 +                       dput(cur_parent);
34962 +               }
34963 +               if (skip)
34964 +                       goto out;
34965 +       }
34966 +
34967 +       au_opt_set(sbinfo->si_mntflags, XINO);
34968 +       dir = d_inode(parent);
34969 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
34970 +       /* mnt_want_write() is unnecessary here */
34971 +       err = au_xino_set_xib(sb, xino->file);
34972 +       if (!err)
34973 +               err = au_xigen_set(sb, xino->file);
34974 +       if (!err)
34975 +               err = au_xino_set_br(sb, xino->file);
34976 +       mutex_unlock(&dir->i_mutex);
34977 +       if (!err)
34978 +               goto out; /* success */
34979 +
34980 +       /* reset all */
34981 +       AuIOErr("failed creating xino(%d).\n", err);
34982 +       au_xigen_clr(sb);
34983 +       xino_clear_xib(sb);
34984 +
34985 +out:
34986 +       dput(parent);
34987 +       return err;
34988 +}
34989 +
34990 +/* ---------------------------------------------------------------------- */
34991 +
34992 +/*
34993 + * create a xinofile at the default place/path.
34994 + */
34995 +struct file *au_xino_def(struct super_block *sb)
34996 +{
34997 +       struct file *file;
34998 +       char *page, *p;
34999 +       struct au_branch *br;
35000 +       struct super_block *h_sb;
35001 +       struct path path;
35002 +       aufs_bindex_t bend, bindex, bwr;
35003 +
35004 +       br = NULL;
35005 +       bend = au_sbend(sb);
35006 +       bwr = -1;
35007 +       for (bindex = 0; bindex <= bend; bindex++) {
35008 +               br = au_sbr(sb, bindex);
35009 +               if (au_br_writable(br->br_perm)
35010 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
35011 +                       bwr = bindex;
35012 +                       break;
35013 +               }
35014 +       }
35015 +
35016 +       if (bwr >= 0) {
35017 +               file = ERR_PTR(-ENOMEM);
35018 +               page = (void *)__get_free_page(GFP_NOFS);
35019 +               if (unlikely(!page))
35020 +                       goto out;
35021 +               path.mnt = au_br_mnt(br);
35022 +               path.dentry = au_h_dptr(sb->s_root, bwr);
35023 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35024 +               file = (void *)p;
35025 +               if (!IS_ERR(p)) {
35026 +                       strcat(p, "/" AUFS_XINO_FNAME);
35027 +                       AuDbg("%s\n", p);
35028 +                       file = au_xino_create(sb, p, /*silent*/0);
35029 +                       if (!IS_ERR(file))
35030 +                               au_xino_brid_set(sb, br->br_id);
35031 +               }
35032 +               free_page((unsigned long)page);
35033 +       } else {
35034 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35035 +               if (IS_ERR(file))
35036 +                       goto out;
35037 +               h_sb = file->f_path.dentry->d_sb;
35038 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
35039 +                       pr_err("xino doesn't support %s(%s)\n",
35040 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35041 +                       fput(file);
35042 +                       file = ERR_PTR(-EINVAL);
35043 +               }
35044 +               if (!IS_ERR(file))
35045 +                       au_xino_brid_set(sb, -1);
35046 +       }
35047 +
35048 +out:
35049 +       return file;
35050 +}
35051 +
35052 +/* ---------------------------------------------------------------------- */
35053 +
35054 +int au_xino_path(struct seq_file *seq, struct file *file)
35055 +{
35056 +       int err;
35057 +
35058 +       err = au_seq_path(seq, &file->f_path);
35059 +       if (unlikely(err))
35060 +               goto out;
35061 +
35062 +#define Deleted "\\040(deleted)"
35063 +       seq->count -= sizeof(Deleted) - 1;
35064 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35065 +                        sizeof(Deleted) - 1));
35066 +#undef Deleted
35067 +
35068 +out:
35069 +       return err;
35070 +}
35071 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35072 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
35073 +++ linux/include/uapi/linux/aufs_type.h        2016-01-24 20:34:09.159976795 +0100
35074 @@ -0,0 +1,419 @@
35075 +/*
35076 + * Copyright (C) 2005-2015 Junjiro R. Okajima
35077 + *
35078 + * This program, aufs is free software; you can redistribute it and/or modify
35079 + * it under the terms of the GNU General Public License as published by
35080 + * the Free Software Foundation; either version 2 of the License, or
35081 + * (at your option) any later version.
35082 + *
35083 + * This program is distributed in the hope that it will be useful,
35084 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35085 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35086 + * GNU General Public License for more details.
35087 + *
35088 + * You should have received a copy of the GNU General Public License
35089 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35090 + */
35091 +
35092 +#ifndef __AUFS_TYPE_H__
35093 +#define __AUFS_TYPE_H__
35094 +
35095 +#define AUFS_NAME      "aufs"
35096 +
35097 +#ifdef __KERNEL__
35098 +/*
35099 + * define it before including all other headers.
35100 + * sched.h may use pr_* macros before defining "current", so define the
35101 + * no-current version first, and re-define later.
35102 + */
35103 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35104 +#include <linux/sched.h>
35105 +#undef pr_fmt
35106 +#define pr_fmt(fmt) \
35107 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35108 +               (int)sizeof(current->comm), current->comm, current->pid
35109 +#else
35110 +#include <stdint.h>
35111 +#include <sys/types.h>
35112 +#endif /* __KERNEL__ */
35113 +
35114 +#include <linux/limits.h>
35115 +
35116 +#define AUFS_VERSION   "4.4-20160118"
35117 +
35118 +/* todo? move this to linux-2.6.19/include/magic.h */
35119 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35120 +
35121 +/* ---------------------------------------------------------------------- */
35122 +
35123 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
35124 +typedef int8_t aufs_bindex_t;
35125 +#define AUFS_BRANCH_MAX 127
35126 +#else
35127 +typedef int16_t aufs_bindex_t;
35128 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
35129 +#define AUFS_BRANCH_MAX 511
35130 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35131 +#define AUFS_BRANCH_MAX 1023
35132 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35133 +#define AUFS_BRANCH_MAX 32767
35134 +#endif
35135 +#endif
35136 +
35137 +#ifdef __KERNEL__
35138 +#ifndef AUFS_BRANCH_MAX
35139 +#error unknown CONFIG_AUFS_BRANCH_MAX value
35140 +#endif
35141 +#endif /* __KERNEL__ */
35142 +
35143 +/* ---------------------------------------------------------------------- */
35144 +
35145 +#define AUFS_FSTYPE            AUFS_NAME
35146 +
35147 +#define AUFS_ROOT_INO          2
35148 +#define AUFS_FIRST_INO         11
35149 +
35150 +#define AUFS_WH_PFX            ".wh."
35151 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
35152 +#define AUFS_WH_TMP_LEN                4
35153 +/* a limit for rmdir/rename a dir and copyup */
35154 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
35155 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
35156 +                               - 1                     /* dot */\
35157 +                               - AUFS_WH_TMP_LEN)      /* hex */
35158 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
35159 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
35160 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
35161 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
35162 +#define AUFS_DIRWH_DEF         3
35163 +#define AUFS_RDCACHE_DEF       10 /* seconds */
35164 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
35165 +#define AUFS_RDBLK_DEF         512 /* bytes */
35166 +#define AUFS_RDHASH_DEF                32
35167 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
35168 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
35169 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
35170 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
35171 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
35172 +
35173 +/* pseudo-link maintenace under /proc */
35174 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
35175 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
35176 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35177 +
35178 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
35179 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
35180 +
35181 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
35182 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
35183 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
35184 +
35185 +/* doubly whiteouted */
35186 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
35187 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
35188 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
35189 +
35190 +/* branch permissions and attributes */
35191 +#define AUFS_BRPERM_RW         "rw"
35192 +#define AUFS_BRPERM_RO         "ro"
35193 +#define AUFS_BRPERM_RR         "rr"
35194 +#define AUFS_BRATTR_COO_REG    "coo_reg"
35195 +#define AUFS_BRATTR_COO_ALL    "coo_all"
35196 +#define AUFS_BRATTR_FHSM       "fhsm"
35197 +#define AUFS_BRATTR_UNPIN      "unpin"
35198 +#define AUFS_BRATTR_ICEX       "icex"
35199 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
35200 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
35201 +#define AUFS_BRATTR_ICEX_TR    "icextr"
35202 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
35203 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
35204 +#define AUFS_BRRATTR_WH                "wh"
35205 +#define AUFS_BRWATTR_NLWH      "nolwh"
35206 +#define AUFS_BRWATTR_MOO       "moo"
35207 +
35208 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
35209 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
35210 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
35211 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35212 +
35213 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
35214 +#define AuBrAttr_COO_ALL       (1 << 4)
35215 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35216 +
35217 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
35218 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
35219 +                                                  branch. meaningless since
35220 +                                                  linux-3.18-rc1 */
35221 +
35222 +/* ignore error in copying XATTR */
35223 +#define AuBrAttr_ICEX_SEC      (1 << 7)
35224 +#define AuBrAttr_ICEX_SYS      (1 << 8)
35225 +#define AuBrAttr_ICEX_TR       (1 << 9)
35226 +#define AuBrAttr_ICEX_USR      (1 << 10)
35227 +#define AuBrAttr_ICEX_OTH      (1 << 11)
35228 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
35229 +                                | AuBrAttr_ICEX_SYS    \
35230 +                                | AuBrAttr_ICEX_TR     \
35231 +                                | AuBrAttr_ICEX_USR    \
35232 +                                | AuBrAttr_ICEX_OTH)
35233 +
35234 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
35235 +#define AuBrRAttr_Mask         AuBrRAttr_WH
35236 +
35237 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
35238 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
35239 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35240 +
35241 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35242 +
35243 +/* #warning test userspace */
35244 +#ifdef __KERNEL__
35245 +#ifndef CONFIG_AUFS_FHSM
35246 +#undef AuBrAttr_FHSM
35247 +#define AuBrAttr_FHSM          0
35248 +#endif
35249 +#ifndef CONFIG_AUFS_XATTR
35250 +#undef AuBrAttr_ICEX
35251 +#define AuBrAttr_ICEX          0
35252 +#undef AuBrAttr_ICEX_SEC
35253 +#define AuBrAttr_ICEX_SEC      0
35254 +#undef AuBrAttr_ICEX_SYS
35255 +#define AuBrAttr_ICEX_SYS      0
35256 +#undef AuBrAttr_ICEX_TR
35257 +#define AuBrAttr_ICEX_TR       0
35258 +#undef AuBrAttr_ICEX_USR
35259 +#define AuBrAttr_ICEX_USR      0
35260 +#undef AuBrAttr_ICEX_OTH
35261 +#define AuBrAttr_ICEX_OTH      0
35262 +#endif
35263 +#endif
35264 +
35265 +/* the longest combination */
35266 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35267 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
35268 +                              "+" AUFS_BRATTR_COO_REG          \
35269 +                              "+" AUFS_BRATTR_FHSM             \
35270 +                              "+" AUFS_BRATTR_UNPIN            \
35271 +                              "+" AUFS_BRATTR_ICEX_SEC         \
35272 +                              "+" AUFS_BRATTR_ICEX_SYS         \
35273 +                              "+" AUFS_BRATTR_ICEX_USR         \
35274 +                              "+" AUFS_BRATTR_ICEX_OTH         \
35275 +                              "+" AUFS_BRWATTR_NLWH)
35276 +
35277 +typedef struct {
35278 +       char a[AuBrPermStrSz];
35279 +} au_br_perm_str_t;
35280 +
35281 +static inline int au_br_writable(int brperm)
35282 +{
35283 +       return brperm & AuBrPerm_RW;
35284 +}
35285 +
35286 +static inline int au_br_whable(int brperm)
35287 +{
35288 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35289 +}
35290 +
35291 +static inline int au_br_wh_linkable(int brperm)
35292 +{
35293 +       return !(brperm & AuBrWAttr_NoLinkWH);
35294 +}
35295 +
35296 +static inline int au_br_cmoo(int brperm)
35297 +{
35298 +       return brperm & AuBrAttr_CMOO_Mask;
35299 +}
35300 +
35301 +static inline int au_br_fhsm(int brperm)
35302 +{
35303 +       return brperm & AuBrAttr_FHSM;
35304 +}
35305 +
35306 +/* ---------------------------------------------------------------------- */
35307 +
35308 +/* ioctl */
35309 +enum {
35310 +       /* readdir in userspace */
35311 +       AuCtl_RDU,
35312 +       AuCtl_RDU_INO,
35313 +
35314 +       AuCtl_WBR_FD,   /* pathconf wrapper */
35315 +       AuCtl_IBUSY,    /* busy inode */
35316 +       AuCtl_MVDOWN,   /* move-down */
35317 +       AuCtl_BR,       /* info about branches */
35318 +       AuCtl_FHSM_FD   /* connection for fhsm */
35319 +};
35320 +
35321 +/* borrowed from linux/include/linux/kernel.h */
35322 +#ifndef ALIGN
35323 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
35324 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
35325 +#endif
35326 +
35327 +/* borrowed from linux/include/linux/compiler-gcc3.h */
35328 +#ifndef __aligned
35329 +#define __aligned(x)                   __attribute__((aligned(x)))
35330 +#endif
35331 +
35332 +#ifdef __KERNEL__
35333 +#ifndef __packed
35334 +#define __packed                       __attribute__((packed))
35335 +#endif
35336 +#endif
35337 +
35338 +struct au_rdu_cookie {
35339 +       uint64_t        h_pos;
35340 +       int16_t         bindex;
35341 +       uint8_t         flags;
35342 +       uint8_t         pad;
35343 +       uint32_t        generation;
35344 +} __aligned(8);
35345 +
35346 +struct au_rdu_ent {
35347 +       uint64_t        ino;
35348 +       int16_t         bindex;
35349 +       uint8_t         type;
35350 +       uint8_t         nlen;
35351 +       uint8_t         wh;
35352 +       char            name[0];
35353 +} __aligned(8);
35354 +
35355 +static inline int au_rdu_len(int nlen)
35356 +{
35357 +       /* include the terminating NULL */
35358 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
35359 +                    sizeof(uint64_t));
35360 +}
35361 +
35362 +union au_rdu_ent_ul {
35363 +       struct au_rdu_ent __user        *e;
35364 +       uint64_t                        ul;
35365 +};
35366 +
35367 +enum {
35368 +       AufsCtlRduV_SZ,
35369 +       AufsCtlRduV_End
35370 +};
35371 +
35372 +struct aufs_rdu {
35373 +       /* input */
35374 +       union {
35375 +               uint64_t        sz;     /* AuCtl_RDU */
35376 +               uint64_t        nent;   /* AuCtl_RDU_INO */
35377 +       };
35378 +       union au_rdu_ent_ul     ent;
35379 +       uint16_t                verify[AufsCtlRduV_End];
35380 +
35381 +       /* input/output */
35382 +       uint32_t                blk;
35383 +
35384 +       /* output */
35385 +       union au_rdu_ent_ul     tail;
35386 +       /* number of entries which were added in a single call */
35387 +       uint64_t                rent;
35388 +       uint8_t                 full;
35389 +       uint8_t                 shwh;
35390 +
35391 +       struct au_rdu_cookie    cookie;
35392 +} __aligned(8);
35393 +
35394 +/* ---------------------------------------------------------------------- */
35395 +
35396 +struct aufs_wbr_fd {
35397 +       uint32_t        oflags;
35398 +       int16_t         brid;
35399 +} __aligned(8);
35400 +
35401 +/* ---------------------------------------------------------------------- */
35402 +
35403 +struct aufs_ibusy {
35404 +       uint64_t        ino, h_ino;
35405 +       int16_t         bindex;
35406 +} __aligned(8);
35407 +
35408 +/* ---------------------------------------------------------------------- */
35409 +
35410 +/* error code for move-down */
35411 +/* the actual message strings are implemented in aufs-util.git */
35412 +enum {
35413 +       EAU_MVDOWN_OPAQUE = 1,
35414 +       EAU_MVDOWN_WHITEOUT,
35415 +       EAU_MVDOWN_UPPER,
35416 +       EAU_MVDOWN_BOTTOM,
35417 +       EAU_MVDOWN_NOUPPER,
35418 +       EAU_MVDOWN_NOLOWERBR,
35419 +       EAU_Last
35420 +};
35421 +
35422 +/* flags for move-down */
35423 +#define AUFS_MVDOWN_DMSG       1
35424 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
35425 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
35426 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
35427 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
35428 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
35429 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
35430 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
35431 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
35432 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
35433 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
35434 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
35435 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
35436 +
35437 +/* index for move-down */
35438 +enum {
35439 +       AUFS_MVDOWN_UPPER,
35440 +       AUFS_MVDOWN_LOWER,
35441 +       AUFS_MVDOWN_NARRAY
35442 +};
35443 +
35444 +/*
35445 + * additional info of move-down
35446 + * number of free blocks and inodes.
35447 + * subset of struct kstatfs, but smaller and always 64bit.
35448 + */
35449 +struct aufs_stfs {
35450 +       uint64_t        f_blocks;
35451 +       uint64_t        f_bavail;
35452 +       uint64_t        f_files;
35453 +       uint64_t        f_ffree;
35454 +};
35455 +
35456 +struct aufs_stbr {
35457 +       int16_t                 brid;   /* optional input */
35458 +       int16_t                 bindex; /* output */
35459 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
35460 +} __aligned(8);
35461 +
35462 +struct aufs_mvdown {
35463 +       uint32_t                flags;                  /* input/output */
35464 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35465 +       int8_t                  au_errno;               /* output */
35466 +} __aligned(8);
35467 +
35468 +/* ---------------------------------------------------------------------- */
35469 +
35470 +union aufs_brinfo {
35471 +       /* PATH_MAX may differ between kernel-space and user-space */
35472 +       char    _spacer[4096];
35473 +       struct {
35474 +               int16_t id;
35475 +               int     perm;
35476 +               char    path[0];
35477 +       };
35478 +} __aligned(8);
35479 +
35480 +/* ---------------------------------------------------------------------- */
35481 +
35482 +#define AuCtlType              'A'
35483 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35484 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
35485 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
35486 +                                    struct aufs_wbr_fd)
35487 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
35488 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
35489 +                                     struct aufs_mvdown)
35490 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35491 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
35492 +
35493 +#endif /* __AUFS_TYPE_H__ */
35494 aufs4.4 loopback patch
35495
35496 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
35497 index abfdd2b..a2e3c43 100644
35498 --- a/drivers/block/loop.c
35499 +++ b/drivers/block/loop.c
35500 @@ -556,7 +556,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
35501  }
35502  
35503  struct switch_request {
35504 -       struct file *file;
35505 +       struct file *file, *virt_file;
35506         struct completion wait;
35507  };
35508  
35509 @@ -582,6 +582,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35510         mapping = file->f_mapping;
35511         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
35512         lo->lo_backing_file = file;
35513 +       lo->lo_backing_virt_file = p->virt_file;
35514         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
35515                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
35516         lo->old_gfp_mask = mapping_gfp_mask(mapping);
35517 @@ -594,11 +595,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35518   * First it needs to flush existing IO, it does this by sending a magic
35519   * BIO down the pipe. The completion of this BIO does the actual switch.
35520   */
35521 -static int loop_switch(struct loop_device *lo, struct file *file)
35522 +static int loop_switch(struct loop_device *lo, struct file *file,
35523 +                      struct file *virt_file)
35524  {
35525         struct switch_request w;
35526  
35527         w.file = file;
35528 +       w.virt_file = virt_file;
35529  
35530         /* freeze queue and wait for completion of scheduled requests */
35531         blk_mq_freeze_queue(lo->lo_queue);
35532 @@ -617,7 +620,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
35533   */
35534  static int loop_flush(struct loop_device *lo)
35535  {
35536 -       return loop_switch(lo, NULL);
35537 +       return loop_switch(lo, NULL, NULL);
35538 +}
35539 +
35540 +static struct file *loop_real_file(struct file *file)
35541 +{
35542 +       struct file *f = NULL;
35543 +
35544 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
35545 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
35546 +       return f;
35547  }
35548  
35549  static void loop_reread_partitions(struct loop_device *lo,
35550 @@ -654,6 +666,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35551                           unsigned int arg)
35552  {
35553         struct file     *file, *old_file;
35554 +       struct file     *f, *virt_file = NULL, *old_virt_file;
35555         struct inode    *inode;
35556         int             error;
35557  
35558 @@ -670,9 +683,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35559         file = fget(arg);
35560         if (!file)
35561                 goto out;
35562 +       f = loop_real_file(file);
35563 +       if (f) {
35564 +               virt_file = file;
35565 +               file = f;
35566 +               get_file(file);
35567 +       }
35568  
35569         inode = file->f_mapping->host;
35570         old_file = lo->lo_backing_file;
35571 +       old_virt_file = lo->lo_backing_virt_file;
35572  
35573         error = -EINVAL;
35574  
35575 @@ -684,17 +704,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35576                 goto out_putf;
35577  
35578         /* and ... switch */
35579 -       error = loop_switch(lo, file);
35580 +       error = loop_switch(lo, file, virt_file);
35581         if (error)
35582                 goto out_putf;
35583  
35584         fput(old_file);
35585 +       if (old_virt_file)
35586 +               fput(old_virt_file);
35587         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
35588                 loop_reread_partitions(lo, bdev);
35589         return 0;
35590  
35591   out_putf:
35592         fput(file);
35593 +       if (virt_file)
35594 +               fput(virt_file);
35595   out:
35596         return error;
35597  }
35598 @@ -881,7 +905,7 @@ static int loop_prepare_queue(struct loop_device *lo)
35599  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35600                        struct block_device *bdev, unsigned int arg)
35601  {
35602 -       struct file     *file, *f;
35603 +       struct file     *file, *f, *virt_file = NULL;
35604         struct inode    *inode;
35605         struct address_space *mapping;
35606         unsigned lo_blocksize;
35607 @@ -896,6 +920,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35608         file = fget(arg);
35609         if (!file)
35610                 goto out;
35611 +       f = loop_real_file(file);
35612 +       if (f) {
35613 +               virt_file = file;
35614 +               file = f;
35615 +               get_file(file);
35616 +       }
35617  
35618         error = -EBUSY;
35619         if (lo->lo_state != Lo_unbound)
35620 @@ -948,6 +978,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35621         lo->lo_device = bdev;
35622         lo->lo_flags = lo_flags;
35623         lo->lo_backing_file = file;
35624 +       lo->lo_backing_virt_file = virt_file;
35625         lo->transfer = NULL;
35626         lo->ioctl = NULL;
35627         lo->lo_sizelimit = 0;
35628 @@ -980,6 +1011,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35629  
35630   out_putf:
35631         fput(file);
35632 +       if (virt_file)
35633 +               fput(virt_file);
35634   out:
35635         /* This is safe: open() is still holding a reference. */
35636         module_put(THIS_MODULE);
35637 @@ -1026,6 +1059,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
35638  static int loop_clr_fd(struct loop_device *lo)
35639  {
35640         struct file *filp = lo->lo_backing_file;
35641 +       struct file *virt_filp = lo->lo_backing_virt_file;
35642         gfp_t gfp = lo->old_gfp_mask;
35643         struct block_device *bdev = lo->lo_device;
35644  
35645 @@ -1057,6 +1091,7 @@ static int loop_clr_fd(struct loop_device *lo)
35646         spin_lock_irq(&lo->lo_lock);
35647         lo->lo_state = Lo_rundown;
35648         lo->lo_backing_file = NULL;
35649 +       lo->lo_backing_virt_file = NULL;
35650         spin_unlock_irq(&lo->lo_lock);
35651  
35652         loop_release_xfer(lo);
35653 @@ -1101,6 +1136,8 @@ static int loop_clr_fd(struct loop_device *lo)
35654          * bd_mutex which is usually taken before lo_ctl_mutex.
35655          */
35656         fput(filp);
35657 +       if (virt_filp)
35658 +               fput(virt_filp);
35659         return 0;
35660  }
35661  
35662 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
35663 index fb2237c..c3888c5 100644
35664 --- a/drivers/block/loop.h
35665 +++ b/drivers/block/loop.h
35666 @@ -46,7 +46,7 @@ struct loop_device {
35667         int             (*ioctl)(struct loop_device *, int cmd, 
35668                                  unsigned long arg); 
35669  
35670 -       struct file *   lo_backing_file;
35671 +       struct file *   lo_backing_file, *lo_backing_virt_file;
35672         struct block_device *lo_device;
35673         unsigned        lo_blocksize;
35674         void            *key_data; 
35675 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
35676 index 91c2ce7..d4ee5a7 100644
35677 --- a/fs/aufs/f_op.c
35678 +++ b/fs/aufs/f_op.c
35679 @@ -389,7 +389,7 @@ static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
35680         if (IS_ERR(h_file))
35681                 goto out;
35682  
35683 -       if (au_test_loopback_kthread()) {
35684 +       if (0 && au_test_loopback_kthread()) {
35685                 au_warn_loopback(h_file->f_path.dentry->d_sb);
35686                 if (file->f_mapping != h_file->f_mapping) {
35687                         file->f_mapping = h_file->f_mapping;
35688 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
35689 index f324758..4555e7b 100644
35690 --- a/fs/aufs/loop.c
35691 +++ b/fs/aufs/loop.c
35692 @@ -131,3 +131,19 @@ void au_loopback_fin(void)
35693                 symbol_put(loop_backing_file);
35694         kfree(au_warn_loopback_array);
35695  }
35696 +
35697 +/* ---------------------------------------------------------------------- */
35698 +
35699 +/* support the loopback block device insude aufs */
35700 +
35701 +struct file *aufs_real_loop(struct file *file)
35702 +{
35703 +       struct file *f;
35704 +
35705 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
35706 +       fi_read_lock(file);
35707 +       f = au_hf_top(file);
35708 +       fi_read_unlock(file);
35709 +       AuDebugOn(!f);
35710 +       return f;
35711 +}
35712 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
35713 index 6d9864d..3322557 100644
35714 --- a/fs/aufs/loop.h
35715 +++ b/fs/aufs/loop.h
35716 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
35717  
35718  int au_loopback_init(void);
35719  void au_loopback_fin(void);
35720 +
35721 +struct file *aufs_real_loop(struct file *file);
35722  #else
35723 +AuStub(struct file *, loop_backing_file, return NULL)
35724 +
35725  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
35726            struct dentry *h_adding)
35727  AuStubInt0(au_test_loopback_kthread, void)
35728 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
35729  
35730  AuStubInt0(au_loopback_init, void)
35731  AuStubVoid(au_loopback_fin, void)
35732 +
35733 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
35734  #endif /* BLK_DEV_LOOP */
35735  
35736  #endif /* __KERNEL__ */
35737 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
35738 index 98cfd64..a705a0e 100644
35739 --- a/fs/aufs/super.c
35740 +++ b/fs/aufs/super.c
35741 @@ -832,7 +832,10 @@ static const struct super_operations aufs_sop = {
35742         .statfs         = aufs_statfs,
35743         .put_super      = aufs_put_super,
35744         .sync_fs        = aufs_sync_fs,
35745 -       .remount_fs     = aufs_remount_fs
35746 +       .remount_fs     = aufs_remount_fs,
35747 +#ifdef CONFIG_AUFS_BDEV_LOOP
35748 +       .real_loop      = aufs_real_loop
35749 +#endif
35750  };
35751  
35752  /* ---------------------------------------------------------------------- */
35753 diff --git a/include/linux/fs.h b/include/linux/fs.h
35754 index 8d48506..5246785 100644
35755 --- a/include/linux/fs.h
35756 +++ b/include/linux/fs.h
35757 @@ -1719,6 +1719,10 @@ struct super_operations {
35758                                   struct shrink_control *);
35759         long (*free_cached_objects)(struct super_block *,
35760                                     struct shrink_control *);
35761 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
35762 +       /* and aufs */
35763 +       struct file *(*real_loop)(struct file *);
35764 +#endif
35765  };
35766  
35767  /*
This page took 2.977158 seconds and 4 git commands to generate.