]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- updated aufs patch
[packages/kernel.git] / kernel-aufs4.patch
1 aufs4.5 kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 9adee0d..5e9cfb2 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -232,6 +232,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 ebd10e6..32152e7 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.5 base patch
37
38 diff --git a/MAINTAINERS b/MAINTAINERS
39 index 6ee06ea..3147250 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -2082,6 +2082,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..0b816b2 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_GPL(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 2398f9f9..318c329 100644
93 --- a/fs/dcache.c
94 +++ b/fs/dcache.c
95 @@ -1156,7 +1156,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/fcntl.c b/fs/fcntl.c
105 index 350a2c8..6f42279 100644
106 --- a/fs/fcntl.c
107 +++ b/fs/fcntl.c
108 @@ -29,7 +29,7 @@
109  
110  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
111  
112 -static int setfl(int fd, struct file * filp, unsigned long arg)
113 +int setfl(int fd, struct file * filp, unsigned long arg)
114  {
115         struct inode * inode = file_inode(filp);
116         int error = 0;
117 @@ -60,6 +60,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
118  
119         if (filp->f_op->check_flags)
120                 error = filp->f_op->check_flags(arg);
121 +       if (!error && filp->f_op->setfl)
122 +               error = filp->f_op->setfl(filp, arg);
123         if (error)
124                 return error;
125  
126 diff --git a/fs/read_write.c b/fs/read_write.c
127 index dadf24e..c8b5b7a 100644
128 --- a/fs/read_write.c
129 +++ b/fs/read_write.c
130 @@ -534,6 +534,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
131  }
132  EXPORT_SYMBOL(__vfs_write);
133  
134 +vfs_readf_t vfs_readf(struct file *file)
135 +{
136 +       const struct file_operations *fop = file->f_op;
137 +
138 +       if (fop->read)
139 +               return fop->read;
140 +       if (fop->read_iter)
141 +               return new_sync_read;
142 +       return ERR_PTR(-ENOSYS);
143 +}
144 +
145 +vfs_writef_t vfs_writef(struct file *file)
146 +{
147 +       const struct file_operations *fop = file->f_op;
148 +
149 +       if (fop->write)
150 +               return fop->write;
151 +       if (fop->write_iter)
152 +               return new_sync_write;
153 +       return ERR_PTR(-ENOSYS);
154 +}
155 +
156  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
157  {
158         mm_segment_t old_fs;
159 diff --git a/fs/splice.c b/fs/splice.c
160 index 82bc0d6..93aee51 100644
161 --- a/fs/splice.c
162 +++ b/fs/splice.c
163 @@ -1108,8 +1108,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
164  /*
165   * Attempt to initiate a splice from pipe to file.
166   */
167 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
168 -                          loff_t *ppos, size_t len, unsigned int flags)
169 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
170 +                   loff_t *ppos, size_t len, unsigned int flags)
171  {
172         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
173                                 loff_t *, size_t, unsigned int);
174 @@ -1125,9 +1125,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
175  /*
176   * Attempt to initiate a splice from a file to a pipe.
177   */
178 -static long do_splice_to(struct file *in, loff_t *ppos,
179 -                        struct pipe_inode_info *pipe, size_t len,
180 -                        unsigned int flags)
181 +long do_splice_to(struct file *in, loff_t *ppos,
182 +                 struct pipe_inode_info *pipe, size_t len,
183 +                 unsigned int flags)
184  {
185         ssize_t (*splice_read)(struct file *, loff_t *,
186                                struct pipe_inode_info *, size_t, unsigned int);
187 diff --git a/include/linux/file.h b/include/linux/file.h
188 index f87d308..9a290b3 100644
189 --- a/include/linux/file.h
190 +++ b/include/linux/file.h
191 @@ -19,6 +19,7 @@ struct dentry;
192  struct path;
193  extern struct file *alloc_file(struct path *, fmode_t mode,
194         const struct file_operations *fop);
195 +extern struct file *get_empty_filp(void);
196  
197  static inline void fput_light(struct file *file, int fput_needed)
198  {
199 diff --git a/include/linux/fs.h b/include/linux/fs.h
200 index ae68100..99fc2bd 100644
201 --- a/include/linux/fs.h
202 +++ b/include/linux/fs.h
203 @@ -1260,6 +1260,7 @@ extern void fasync_free(struct fasync_struct *);
204  /* can be called from interrupts */
205  extern void kill_fasync(struct fasync_struct **, int, int);
206  
207 +extern int setfl(int fd, struct file * filp, unsigned long arg);
208  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
209  extern void f_setown(struct file *filp, unsigned long arg, int force);
210  extern void f_delown(struct file *filp);
211 @@ -1646,6 +1647,7 @@ struct file_operations {
212         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
213         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
214         int (*check_flags)(int);
215 +       int (*setfl)(struct file *, unsigned long);
216         int (*flock) (struct file *, int, struct file_lock *);
217         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
218         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
219 @@ -1704,6 +1706,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
220                               struct iovec *fast_pointer,
221                               struct iovec **ret_pointer);
222  
223 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
224 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
225 +                               loff_t *);
226 +vfs_readf_t vfs_readf(struct file *file);
227 +vfs_writef_t vfs_writef(struct file *file);
228 +
229  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
230  extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
231  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
232 diff --git a/include/linux/splice.h b/include/linux/splice.h
233 index da2751d..2e0fca6 100644
234 --- a/include/linux/splice.h
235 +++ b/include/linux/splice.h
236 @@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
237  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
238  
239  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
240 +
241 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
242 +                          loff_t *ppos, size_t len, unsigned int flags);
243 +extern long do_splice_to(struct file *in, loff_t *ppos,
244 +                        struct pipe_inode_info *pipe, size_t len,
245 +                        unsigned int flags);
246  #endif
247 aufs4.5 mmap patch
248
249 diff --git a/fs/proc/base.c b/fs/proc/base.c
250 index 4f764c2..229de5e 100644
251 --- a/fs/proc/base.c
252 +++ b/fs/proc/base.c
253 @@ -1933,7 +1933,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
254         down_read(&mm->mmap_sem);
255         vma = find_exact_vma(mm, vm_start, vm_end);
256         if (vma && vma->vm_file) {
257 -               *path = vma->vm_file->f_path;
258 +               *path = vma_pr_or_file(vma)->f_path;
259                 path_get(path);
260                 rc = 0;
261         }
262 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
263 index f8595e8..cb8eda0 100644
264 --- a/fs/proc/nommu.c
265 +++ b/fs/proc/nommu.c
266 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
267         file = region->vm_file;
268  
269         if (file) {
270 -               struct inode *inode = file_inode(region->vm_file);
271 +               struct inode *inode;
272 +
273 +               file = vmr_pr_or_file(region);
274 +               inode = file_inode(file);
275                 dev = inode->i_sb->s_dev;
276                 ino = inode->i_ino;
277         }
278 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
279 index fa95ab2..d440354 100644
280 --- a/fs/proc/task_mmu.c
281 +++ b/fs/proc/task_mmu.c
282 @@ -298,7 +298,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
283         const char *name = NULL;
284  
285         if (file) {
286 -               struct inode *inode = file_inode(vma->vm_file);
287 +               struct inode *inode;
288 +
289 +               file = vma_pr_or_file(vma);
290 +               inode = file_inode(file);
291                 dev = inode->i_sb->s_dev;
292                 ino = inode->i_ino;
293                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
294 @@ -1576,7 +1579,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
295         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
296         struct vm_area_struct *vma = v;
297         struct numa_maps *md = &numa_priv->md;
298 -       struct file *file = vma->vm_file;
299 +       struct file *file = vma_pr_or_file(vma);
300         struct mm_struct *mm = vma->vm_mm;
301         struct mm_walk walk = {
302                 .hugetlb_entry = gather_hugetlb_stats,
303 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
304 index faacb0c..17b43be 100644
305 --- a/fs/proc/task_nommu.c
306 +++ b/fs/proc/task_nommu.c
307 @@ -163,7 +163,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
308         file = vma->vm_file;
309  
310         if (file) {
311 -               struct inode *inode = file_inode(vma->vm_file);
312 +               struct inode *inode;
313 +
314 +               file = vma_pr_or_file(vma);
315 +               inode = file_inode(file);
316                 dev = inode->i_sb->s_dev;
317                 ino = inode->i_ino;
318                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
319 diff --git a/include/linux/mm.h b/include/linux/mm.h
320 index 516e149..ddd5454 100644
321 --- a/include/linux/mm.h
322 +++ b/include/linux/mm.h
323 @@ -1217,6 +1217,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
324  }
325  #endif
326  
327 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
328 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
329 +                                     int);
330 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
331 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
332 +
333 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
334 +                                                               __LINE__)
335 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
336 +                                                         __LINE__)
337 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
338 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
339 +
340 +#ifndef CONFIG_MMU
341 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
342 +extern void vmr_do_fput(struct vm_region *, const char[], int);
343 +
344 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
345 +                                                         __LINE__)
346 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
347 +#endif /* !CONFIG_MMU */
348 +
349  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
350  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
351                 void *buf, int len, int write);
352 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
353 index 624b78b..1be91c5 100644
354 --- a/include/linux/mm_types.h
355 +++ b/include/linux/mm_types.h
356 @@ -269,6 +269,7 @@ struct vm_region {
357         unsigned long   vm_top;         /* region allocated to here */
358         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
359         struct file     *vm_file;       /* the backing file or NULL */
360 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
361  
362         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
363         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
364 @@ -343,6 +344,7 @@ struct vm_area_struct {
365         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
366                                            units, *not* PAGE_CACHE_SIZE */
367         struct file * vm_file;          /* File we map to (can be NULL). */
368 +       struct file *vm_prfile;         /* shadow of vm_file */
369         void * vm_private_data;         /* was vm_pte (shared mem) */
370  
371  #ifndef CONFIG_MMU
372 diff --git a/kernel/fork.c b/kernel/fork.c
373 index 2e391c7..6c4215c 100644
374 --- a/kernel/fork.c
375 +++ b/kernel/fork.c
376 @@ -464,7 +464,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
377                         struct inode *inode = file_inode(file);
378                         struct address_space *mapping = file->f_mapping;
379  
380 -                       get_file(file);
381 +                       vma_get_file(tmp);
382                         if (tmp->vm_flags & VM_DENYWRITE)
383                                 atomic_dec(&inode->i_writecount);
384                         i_mmap_lock_write(mapping);
385 diff --git a/mm/Makefile b/mm/Makefile
386 index 2ed4319..e3a53f5 100644
387 --- a/mm/Makefile
388 +++ b/mm/Makefile
389 @@ -21,7 +21,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
390                            mm_init.o mmu_context.o percpu.o slab_common.o \
391                            compaction.o vmacache.o \
392                            interval_tree.o list_lru.o workingset.o \
393 -                          debug.o $(mmu-y)
394 +                          prfile.o debug.o $(mmu-y)
395  
396  obj-y += init-mm.o
397  
398 diff --git a/mm/filemap.c b/mm/filemap.c
399 index da7a35d..f800f87 100644
400 --- a/mm/filemap.c
401 +++ b/mm/filemap.c
402 @@ -2229,7 +2229,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
403         int ret = VM_FAULT_LOCKED;
404  
405         sb_start_pagefault(inode->i_sb);
406 -       file_update_time(vma->vm_file);
407 +       vma_file_update_time(vma);
408         lock_page(page);
409         if (page->mapping != inode->i_mapping) {
410                 unlock_page(page);
411 diff --git a/mm/memory.c b/mm/memory.c
412 index 8132787..3f7de66 100644
413 --- a/mm/memory.c
414 +++ b/mm/memory.c
415 @@ -2042,7 +2042,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
416                 }
417  
418                 if (!page_mkwrite)
419 -                       file_update_time(vma->vm_file);
420 +                       vma_file_update_time(vma);
421         }
422  
423         return VM_FAULT_WRITE;
424 diff --git a/mm/mmap.c b/mm/mmap.c
425 index 76d1ec2..fdd163e 100644
426 --- a/mm/mmap.c
427 +++ b/mm/mmap.c
428 @@ -290,7 +290,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
429         if (vma->vm_ops && vma->vm_ops->close)
430                 vma->vm_ops->close(vma);
431         if (vma->vm_file)
432 -               fput(vma->vm_file);
433 +               vma_fput(vma);
434         mpol_put(vma_policy(vma));
435         kmem_cache_free(vm_area_cachep, vma);
436         return next;
437 @@ -909,7 +909,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
438         if (remove_next) {
439                 if (file) {
440                         uprobe_munmap(next, next->vm_start, next->vm_end);
441 -                       fput(file);
442 +                       vma_fput(vma);
443                 }
444                 if (next->anon_vma)
445                         anon_vma_merge(vma, next);
446 @@ -1683,8 +1683,8 @@ out:
447         return addr;
448  
449  unmap_and_free_vma:
450 +       vma_fput(vma);
451         vma->vm_file = NULL;
452 -       fput(file);
453  
454         /* Undo any partial mapping done by a device driver. */
455         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
456 @@ -2479,7 +2479,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
457                 goto out_free_mpol;
458  
459         if (new->vm_file)
460 -               get_file(new->vm_file);
461 +               vma_get_file(new);
462  
463         if (new->vm_ops && new->vm_ops->open)
464                 new->vm_ops->open(new);
465 @@ -2498,7 +2498,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
466         if (new->vm_ops && new->vm_ops->close)
467                 new->vm_ops->close(new);
468         if (new->vm_file)
469 -               fput(new->vm_file);
470 +               vma_fput(new);
471         unlink_anon_vmas(new);
472   out_free_mpol:
473         mpol_put(vma_policy(new));
474 @@ -2640,7 +2640,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
475         struct vm_area_struct *vma;
476         unsigned long populate = 0;
477         unsigned long ret = -EINVAL;
478 -       struct file *file;
479  
480         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
481                         "See Documentation/vm/remap_file_pages.txt.\n",
482 @@ -2708,10 +2707,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
483                 }
484         }
485  
486 -       file = get_file(vma->vm_file);
487 +       vma_get_file(vma);
488         ret = do_mmap_pgoff(vma->vm_file, start, size,
489                         prot, flags, pgoff, &populate);
490 -       fput(file);
491 +       vma_fput(vma);
492  out:
493         up_write(&mm->mmap_sem);
494         if (populate)
495 @@ -2982,7 +2981,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
496                 if (anon_vma_clone(new_vma, vma))
497                         goto out_free_mempol;
498                 if (new_vma->vm_file)
499 -                       get_file(new_vma->vm_file);
500 +                       vma_get_file(new_vma);
501                 if (new_vma->vm_ops && new_vma->vm_ops->open)
502                         new_vma->vm_ops->open(new_vma);
503                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
504 diff --git a/mm/nommu.c b/mm/nommu.c
505 index fbf6f0f1..1a4a06d 100644
506 --- a/mm/nommu.c
507 +++ b/mm/nommu.c
508 @@ -671,7 +671,7 @@ static void __put_nommu_region(struct vm_region *region)
509                 up_write(&nommu_region_sem);
510  
511                 if (region->vm_file)
512 -                       fput(region->vm_file);
513 +                       vmr_fput(region);
514  
515                 /* IO memory and memory shared directly out of the pagecache
516                  * from ramfs/tmpfs mustn't be released here */
517 @@ -829,7 +829,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
518         if (vma->vm_ops && vma->vm_ops->close)
519                 vma->vm_ops->close(vma);
520         if (vma->vm_file)
521 -               fput(vma->vm_file);
522 +               vma_fput(vma);
523         put_nommu_region(vma->vm_region);
524         kmem_cache_free(vm_area_cachep, vma);
525  }
526 @@ -1355,7 +1355,7 @@ unsigned long do_mmap(struct file *file,
527                                         goto error_just_free;
528                                 }
529                         }
530 -                       fput(region->vm_file);
531 +                       vmr_fput(region);
532                         kmem_cache_free(vm_region_jar, region);
533                         region = pregion;
534                         result = start;
535 @@ -1430,10 +1430,10 @@ error_just_free:
536         up_write(&nommu_region_sem);
537  error:
538         if (region->vm_file)
539 -               fput(region->vm_file);
540 +               vmr_fput(region);
541         kmem_cache_free(vm_region_jar, region);
542         if (vma->vm_file)
543 -               fput(vma->vm_file);
544 +               vma_fput(vma);
545         kmem_cache_free(vm_area_cachep, vma);
546         return ret;
547  
548 diff --git a/mm/prfile.c b/mm/prfile.c
549 new file mode 100644
550 index 0000000..b323b8a
551 --- /dev/null
552 +++ b/mm/prfile.c
553 @@ -0,0 +1,86 @@
554 +/*
555 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
556 + * in /proc/PID/maps.
557 + * Call these functions via macros defined in linux/mm.h.
558 + *
559 + * See Documentation/filesystems/aufs/design/06mmap.txt
560 + *
561 + * Copyright (c) 2014 Junjro R. Okajima
562 + * Copyright (c) 2014 Ian Campbell
563 + */
564 +
565 +#include <linux/mm.h>
566 +#include <linux/file.h>
567 +#include <linux/fs.h>
568 +
569 +/* #define PRFILE_TRACE */
570 +static inline void prfile_trace(struct file *f, struct file *pr,
571 +                             const char func[], int line, const char func2[])
572 +{
573 +#ifdef PRFILE_TRACE
574 +       if (pr)
575 +               pr_info("%s:%d: %s, %s\n", func, line, func2,
576 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
577 +#endif
578 +}
579 +
580 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
581 +                            int line)
582 +{
583 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
584 +
585 +       prfile_trace(f, pr, func, line, __func__);
586 +       file_update_time(f);
587 +       if (f && pr)
588 +               file_update_time(pr);
589 +}
590 +
591 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
592 +                              int line)
593 +{
594 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
595 +
596 +       prfile_trace(f, pr, func, line, __func__);
597 +       return (f && pr) ? pr : f;
598 +}
599 +
600 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
601 +{
602 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
603 +
604 +       prfile_trace(f, pr, func, line, __func__);
605 +       get_file(f);
606 +       if (f && pr)
607 +               get_file(pr);
608 +}
609 +
610 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
611 +{
612 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
613 +
614 +       prfile_trace(f, pr, func, line, __func__);
615 +       fput(f);
616 +       if (f && pr)
617 +               fput(pr);
618 +}
619 +
620 +#ifndef CONFIG_MMU
621 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
622 +                              int line)
623 +{
624 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
625 +
626 +       prfile_trace(f, pr, func, line, __func__);
627 +       return (f && pr) ? pr : f;
628 +}
629 +
630 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
631 +{
632 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       fput(f);
636 +       if (f && pr)
637 +               fput(pr);
638 +}
639 +#endif /* !CONFIG_MMU */
640 aufs4.5 standalone patch
641
642 diff --git a/fs/dcache.c b/fs/dcache.c
643 index 318c329..72d3cc8 100644
644 --- a/fs/dcache.c
645 +++ b/fs/dcache.c
646 @@ -1261,6 +1261,7 @@ rename_retry:
647         seq = 1;
648         goto again;
649  }
650 +EXPORT_SYMBOL_GPL(d_walk);
651  
652  /*
653   * Search for at least 1 mount point in the dentry's subdirs.
654 diff --git a/fs/exec.c b/fs/exec.c
655 index dcd4ac7..09a7818 100644
656 --- a/fs/exec.c
657 +++ b/fs/exec.c
658 @@ -103,6 +103,7 @@ bool path_noexec(const struct path *path)
659         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
660                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
661  }
662 +EXPORT_SYMBOL_GPL(path_noexec);
663  
664  #ifdef CONFIG_USELIB
665  /*
666 diff --git a/fs/fcntl.c b/fs/fcntl.c
667 index 6f42279..04fd33c 100644
668 --- a/fs/fcntl.c
669 +++ b/fs/fcntl.c
670 @@ -82,6 +82,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
671   out:
672         return error;
673  }
674 +EXPORT_SYMBOL_GPL(setfl);
675  
676  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
677                       int force)
678 diff --git a/fs/file_table.c b/fs/file_table.c
679 index ad17e05..ae9f267 100644
680 --- a/fs/file_table.c
681 +++ b/fs/file_table.c
682 @@ -147,6 +147,7 @@ over:
683         }
684         return ERR_PTR(-ENFILE);
685  }
686 +EXPORT_SYMBOL_GPL(get_empty_filp);
687  
688  /**
689   * alloc_file - allocate and initialize a 'struct file'
690 @@ -258,6 +259,7 @@ void flush_delayed_fput(void)
691  {
692         delayed_fput(NULL);
693  }
694 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
695  
696  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
697  
698 @@ -300,6 +302,7 @@ void __fput_sync(struct file *file)
699  }
700  
701  EXPORT_SYMBOL(fput);
702 +EXPORT_SYMBOL_GPL(__fput_sync);
703  
704  void put_filp(struct file *file)
705  {
706 @@ -308,6 +311,7 @@ void put_filp(struct file *file)
707                 file_free(file);
708         }
709  }
710 +EXPORT_SYMBOL_GPL(put_filp);
711  
712  void __init files_init(void)
713  { 
714 diff --git a/fs/namespace.c b/fs/namespace.c
715 index 4fb1691..97654d2 100644
716 --- a/fs/namespace.c
717 +++ b/fs/namespace.c
718 @@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
719         mnt_dec_writers(real_mount(mnt));
720         preempt_enable();
721  }
722 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
723  
724  /**
725   * mnt_drop_write - give up write access to a mount
726 @@ -1811,6 +1812,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
727         }
728         return 0;
729  }
730 +EXPORT_SYMBOL_GPL(iterate_mounts);
731  
732  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
733  {
734 diff --git a/fs/notify/group.c b/fs/notify/group.c
735 index d16b62c..53e45b6 100644
736 --- a/fs/notify/group.c
737 +++ b/fs/notify/group.c
738 @@ -22,6 +22,7 @@
739  #include <linux/srcu.h>
740  #include <linux/rculist.h>
741  #include <linux/wait.h>
742 +#include <linux/module.h>
743  
744  #include <linux/fsnotify_backend.h>
745  #include "fsnotify.h"
746 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
747  {
748         atomic_inc(&group->refcnt);
749  }
750 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
751  
752  /*
753   * Drop a reference to a group.  Free it if it's through.
754 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
755         if (atomic_dec_and_test(&group->refcnt))
756                 fsnotify_final_destroy_group(group);
757  }
758 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
759  
760  /*
761   * Create a new fsnotify_group and hold a reference for the group returned.
762 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
763  
764         return group;
765  }
766 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
767  
768  int fsnotify_fasync(int fd, struct file *file, int on)
769  {
770 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
771 index 7115c5d..ac2bd69 100644
772 --- a/fs/notify/mark.c
773 +++ b/fs/notify/mark.c
774 @@ -113,6 +113,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
775                 mark->free_mark(mark);
776         }
777  }
778 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
779  
780  /* Calculate mask of events for a list of marks */
781  u32 fsnotify_recalc_mask(struct hlist_head *head)
782 @@ -213,6 +214,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
783         mutex_unlock(&group->mark_mutex);
784         fsnotify_free_mark(mark);
785  }
786 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
787  
788  void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
789  {
790 @@ -398,6 +400,7 @@ err:
791  
792         return ret;
793  }
794 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
795  
796  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
797                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
798 @@ -498,6 +501,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
799         atomic_set(&mark->refcnt, 1);
800         mark->free_mark = free_mark;
801  }
802 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
803  
804  static void fsnotify_mark_destroy(struct work_struct *work)
805  {
806 diff --git a/fs/open.c b/fs/open.c
807 index 55bdc75..fd1df73 100644
808 --- a/fs/open.c
809 +++ b/fs/open.c
810 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
811         inode_unlock(dentry->d_inode);
812         return ret;
813  }
814 +EXPORT_SYMBOL_GPL(do_truncate);
815  
816  long vfs_truncate(struct path *path, loff_t length)
817  {
818 @@ -678,6 +679,7 @@ int open_check_o_direct(struct file *f)
819         }
820         return 0;
821  }
822 +EXPORT_SYMBOL_GPL(open_check_o_direct);
823  
824  static int do_dentry_open(struct file *f,
825                           struct inode *inode,
826 diff --git a/fs/read_write.c b/fs/read_write.c
827 index c8b5b7a..9461d12 100644
828 --- a/fs/read_write.c
829 +++ b/fs/read_write.c
830 @@ -544,6 +544,7 @@ vfs_readf_t vfs_readf(struct file *file)
831                 return new_sync_read;
832         return ERR_PTR(-ENOSYS);
833  }
834 +EXPORT_SYMBOL_GPL(vfs_readf);
835  
836  vfs_writef_t vfs_writef(struct file *file)
837  {
838 @@ -555,6 +556,7 @@ vfs_writef_t vfs_writef(struct file *file)
839                 return new_sync_write;
840         return ERR_PTR(-ENOSYS);
841  }
842 +EXPORT_SYMBOL_GPL(vfs_writef);
843  
844  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
845  {
846 diff --git a/fs/splice.c b/fs/splice.c
847 index 93aee51..52d2f5b 100644
848 --- a/fs/splice.c
849 +++ b/fs/splice.c
850 @@ -1121,6 +1121,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
851  
852         return splice_write(pipe, out, ppos, len, flags);
853  }
854 +EXPORT_SYMBOL_GPL(do_splice_from);
855  
856  /*
857   * Attempt to initiate a splice from a file to a pipe.
858 @@ -1147,6 +1148,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
859  
860         return splice_read(in, ppos, pipe, len, flags);
861  }
862 +EXPORT_SYMBOL_GPL(do_splice_to);
863  
864  /**
865   * splice_direct_to_actor - splices data directly between two non-pipes
866 diff --git a/fs/xattr.c b/fs/xattr.c
867 index 4861322..c4bb039 100644
868 --- a/fs/xattr.c
869 +++ b/fs/xattr.c
870 @@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
871         *xattr_value = value;
872         return error;
873  }
874 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
875  
876  ssize_t
877  vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
878 diff --git a/kernel/task_work.c b/kernel/task_work.c
879 index 53fa971..bce3211 100644
880 --- a/kernel/task_work.c
881 +++ b/kernel/task_work.c
882 @@ -118,3 +118,4 @@ void task_work_run(void)
883                 } while (work);
884         }
885  }
886 +EXPORT_SYMBOL_GPL(task_work_run);
887 diff --git a/security/commoncap.c b/security/commoncap.c
888 index 48071ed..50a1a40 100644
889 --- a/security/commoncap.c
890 +++ b/security/commoncap.c
891 @@ -1058,12 +1058,14 @@ int cap_mmap_addr(unsigned long addr)
892         }
893         return ret;
894  }
895 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
896  
897  int cap_mmap_file(struct file *file, unsigned long reqprot,
898                   unsigned long prot, unsigned long flags)
899  {
900         return 0;
901  }
902 +EXPORT_SYMBOL_GPL(cap_mmap_file);
903  
904  #ifdef CONFIG_SECURITY
905  
906 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
907 index 03c1652..f88c84b 100644
908 --- a/security/device_cgroup.c
909 +++ b/security/device_cgroup.c
910 @@ -7,6 +7,7 @@
911  #include <linux/device_cgroup.h>
912  #include <linux/cgroup.h>
913  #include <linux/ctype.h>
914 +#include <linux/export.h>
915  #include <linux/list.h>
916  #include <linux/uaccess.h>
917  #include <linux/seq_file.h>
918 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
919         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
920                         access);
921  }
922 +EXPORT_SYMBOL_GPL(__devcgroup_inode_permission);
923  
924  int devcgroup_inode_mknod(int mode, dev_t dev)
925  {
926 diff --git a/security/security.c b/security/security.c
927 index e8ffd92..51fa026 100644
928 --- a/security/security.c
929 +++ b/security/security.c
930 @@ -433,6 +433,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
931                 return 0;
932         return call_int_hook(path_rmdir, 0, dir, dentry);
933  }
934 +EXPORT_SYMBOL_GPL(security_path_rmdir);
935  
936  int security_path_unlink(struct path *dir, struct dentry *dentry)
937  {
938 @@ -449,6 +450,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
939                 return 0;
940         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
941  }
942 +EXPORT_SYMBOL_GPL(security_path_symlink);
943  
944  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
945                        struct dentry *new_dentry)
946 @@ -457,6 +459,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
947                 return 0;
948         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
949  }
950 +EXPORT_SYMBOL_GPL(security_path_link);
951  
952  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
953                          struct path *new_dir, struct dentry *new_dentry,
954 @@ -484,6 +487,7 @@ int security_path_truncate(struct path *path)
955                 return 0;
956         return call_int_hook(path_truncate, 0, path);
957  }
958 +EXPORT_SYMBOL_GPL(security_path_truncate);
959  
960  int security_path_chmod(struct path *path, umode_t mode)
961  {
962 @@ -491,6 +495,7 @@ int security_path_chmod(struct path *path, umode_t mode)
963                 return 0;
964         return call_int_hook(path_chmod, 0, path, mode);
965  }
966 +EXPORT_SYMBOL_GPL(security_path_chmod);
967  
968  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
969  {
970 @@ -498,6 +503,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
971                 return 0;
972         return call_int_hook(path_chown, 0, path, uid, gid);
973  }
974 +EXPORT_SYMBOL_GPL(security_path_chown);
975  
976  int security_path_chroot(struct path *path)
977  {
978 @@ -583,6 +589,7 @@ int security_inode_readlink(struct dentry *dentry)
979                 return 0;
980         return call_int_hook(inode_readlink, 0, dentry);
981  }
982 +EXPORT_SYMBOL_GPL(security_inode_readlink);
983  
984  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
985                                bool rcu)
986 @@ -598,6 +605,7 @@ int security_inode_permission(struct inode *inode, int mask)
987                 return 0;
988         return call_int_hook(inode_permission, 0, inode, mask);
989  }
990 +EXPORT_SYMBOL_GPL(security_inode_permission);
991  
992  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
993  {
994 @@ -736,6 +744,7 @@ int security_file_permission(struct file *file, int mask)
995  
996         return fsnotify_perm(file, mask);
997  }
998 +EXPORT_SYMBOL_GPL(security_file_permission);
999  
1000  int security_file_alloc(struct file *file)
1001  {
1002 @@ -795,6 +804,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1003                 return ret;
1004         return ima_file_mmap(file, prot);
1005  }
1006 +EXPORT_SYMBOL_GPL(security_mmap_file);
1007  
1008  int security_mmap_addr(unsigned long addr)
1009  {
1010 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1011 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1012 +++ linux/Documentation/ABI/testing/debugfs-aufs        2016-04-24 18:32:51.390353525 +0200
1013 @@ -0,0 +1,50 @@
1014 +What:          /debug/aufs/si_<id>/
1015 +Date:          March 2009
1016 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1017 +Description:
1018 +               Under /debug/aufs, a directory named si_<id> is created
1019 +               per aufs mount, where <id> is a unique id generated
1020 +               internally.
1021 +
1022 +What:          /debug/aufs/si_<id>/plink
1023 +Date:          Apr 2013
1024 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1025 +Description:
1026 +               It has three lines and shows the information about the
1027 +               pseudo-link. The first line is a single number
1028 +               representing a number of buckets. The second line is a
1029 +               number of pseudo-links per buckets (separated by a
1030 +               blank). The last line is a single number representing a
1031 +               total number of psedo-links.
1032 +               When the aufs mount option 'noplink' is specified, it
1033 +               will show "1\n0\n0\n".
1034 +
1035 +What:          /debug/aufs/si_<id>/xib
1036 +Date:          March 2009
1037 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1038 +Description:
1039 +               It shows the consumed blocks by xib (External Inode Number
1040 +               Bitmap), its block size and file size.
1041 +               When the aufs mount option 'noxino' is specified, it
1042 +               will be empty. About XINO files, see the aufs manual.
1043 +
1044 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1045 +Date:          March 2009
1046 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1047 +Description:
1048 +               It shows the consumed blocks by xino (External Inode Number
1049 +               Translation Table), its link count, block size and file
1050 +               size.
1051 +               When the aufs mount option 'noxino' is specified, it
1052 +               will be empty. About XINO files, see the aufs manual.
1053 +
1054 +What:          /debug/aufs/si_<id>/xigen
1055 +Date:          March 2009
1056 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1057 +Description:
1058 +               It shows the consumed blocks by xigen (External Inode
1059 +               Generation Table), its block size and file size.
1060 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1061 +               be created.
1062 +               When the aufs mount option 'noxino' is specified, it
1063 +               will be empty. About XINO files, see the aufs manual.
1064 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1065 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1066 +++ linux/Documentation/ABI/testing/sysfs-aufs  2016-04-24 18:32:51.390353525 +0200
1067 @@ -0,0 +1,31 @@
1068 +What:          /sys/fs/aufs/si_<id>/
1069 +Date:          March 2009
1070 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1071 +Description:
1072 +               Under /sys/fs/aufs, a directory named si_<id> is created
1073 +               per aufs mount, where <id> is a unique id generated
1074 +               internally.
1075 +
1076 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1077 +Date:          March 2009
1078 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1079 +Description:
1080 +               It shows the abolute path of a member directory (which
1081 +               is called branch) in aufs, and its permission.
1082 +
1083 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1084 +Date:          July 2013
1085 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1086 +Description:
1087 +               It shows the id of a member directory (which is called
1088 +               branch) in aufs.
1089 +
1090 +What:          /sys/fs/aufs/si_<id>/xi_path
1091 +Date:          March 2009
1092 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1093 +Description:
1094 +               It shows the abolute path of XINO (External Inode Number
1095 +               Bitmap, Translation Table and Generation Table) file
1096 +               even if it is the default path.
1097 +               When the aufs mount option 'noxino' is specified, it
1098 +               will be empty. About XINO files, see the aufs manual.
1099 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1100 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1101 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2016-04-24 18:32:51.390353525 +0200
1102 @@ -0,0 +1,170 @@
1103 +
1104 +# Copyright (C) 2005-2016 Junjiro R. Okajima
1105 +# 
1106 +# This program is free software; you can redistribute it and/or modify
1107 +# it under the terms of the GNU General Public License as published by
1108 +# the Free Software Foundation; either version 2 of the License, or
1109 +# (at your option) any later version.
1110 +# 
1111 +# This program is distributed in the hope that it will be useful,
1112 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1113 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1114 +# GNU General Public License for more details.
1115 +# 
1116 +# You should have received a copy of the GNU General Public License
1117 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1118 +
1119 +Introduction
1120 +----------------------------------------
1121 +
1122 +aufs [ei ju: ef es] | [a u f s]
1123 +1. abbrev. for "advanced multi-layered unification filesystem".
1124 +2. abbrev. for "another unionfs".
1125 +3. abbrev. for "auf das" in German which means "on the" in English.
1126 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1127 +   But "Filesystem aufs Filesystem" is hard to understand.
1128 +
1129 +AUFS is a filesystem with features:
1130 +- multi layered stackable unification filesystem, the member directory
1131 +  is called as a branch.
1132 +- branch permission and attribute, 'readonly', 'real-readonly',
1133 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1134 +  combination.
1135 +- internal "file copy-on-write".
1136 +- logical deletion, whiteout.
1137 +- dynamic branch manipulation, adding, deleting and changing permission.
1138 +- allow bypassing aufs, user's direct branch access.
1139 +- external inode number translation table and bitmap which maintains the
1140 +  persistent aufs inode number.
1141 +- seekable directory, including NFS readdir.
1142 +- file mapping, mmap and sharing pages.
1143 +- pseudo-link, hardlink over branches.
1144 +- loopback mounted filesystem as a branch.
1145 +- several policies to select one among multiple writable branches.
1146 +- revert a single systemcall when an error occurs in aufs.
1147 +- and more...
1148 +
1149 +
1150 +Multi Layered Stackable Unification Filesystem
1151 +----------------------------------------------------------------------
1152 +Most people already knows what it is.
1153 +It is a filesystem which unifies several directories and provides a
1154 +merged single directory. When users access a file, the access will be
1155 +passed/re-directed/converted (sorry, I am not sure which English word is
1156 +correct) to the real file on the member filesystem. The member
1157 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1158 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1159 +readonly branch is handled by creating 'whiteout' on the upper writable
1160 +branch.
1161 +
1162 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1163 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1164 +different approaches to implement the merged-view.
1165 +The former tries putting it into VFS, and the latter implements as a
1166 +separate filesystem.
1167 +(If I misunderstand about these implementations, please let me know and
1168 +I shall correct it. Because it is a long time ago when I read their
1169 +source files last time).
1170 +
1171 +UnionMount's approach will be able to small, but may be hard to share
1172 +branches between several UnionMount since the whiteout in it is
1173 +implemented in the inode on branch filesystem and always
1174 +shared. According to Bharata's post, readdir does not seems to be
1175 +finished yet.
1176 +There are several missing features known in this implementations such as
1177 +- for users, the inode number may change silently. eg. copy-up.
1178 +- link(2) may break by copy-up.
1179 +- read(2) may get an obsoleted filedata (fstat(2) too).
1180 +- fcntl(F_SETLK) may be broken by copy-up.
1181 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1182 +  open(O_RDWR).
1183 +
1184 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1185 +merged into mainline. This is another implementation of UnionMount as a
1186 +separated filesystem. All the limitations and known problems which
1187 +UnionMount are equally inherited to "overlay" filesystem.
1188 +
1189 +Unionfs has a longer history. When I started implementing a stackable
1190 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1191 +inode, dentry and file objects and they have an array pointing lower
1192 +same kind objects. After contributing many patches for Unionfs, I
1193 +re-started my project AUFS (Jun 2006).
1194 +
1195 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1196 +implemented my own ideas, approaches and enhancements and it became
1197 +totally different one.
1198 +
1199 +Comparing DM snapshot and fs based implementation
1200 +- the number of bytes to be copied between devices is much smaller.
1201 +- the type of filesystem must be one and only.
1202 +- the fs must be writable, no readonly fs, even for the lower original
1203 +  device. so the compression fs will not be usable. but if we use
1204 +  loopback mount, we may address this issue.
1205 +  for instance,
1206 +       mount /cdrom/squashfs.img /sq
1207 +       losetup /sq/ext2.img
1208 +       losetup /somewhere/cow
1209 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1210 +- it will be difficult (or needs more operations) to extract the
1211 +  difference between the original device and COW.
1212 +- DM snapshot-merge may help a lot when users try merging. in the
1213 +  fs-layer union, users will use rsync(1).
1214 +
1215 +You may want to read my old paper "Filesystems in LiveCD"
1216 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1217 +
1218 +
1219 +Several characters/aspects/persona of aufs
1220 +----------------------------------------------------------------------
1221 +
1222 +Aufs has several characters, aspects or persona.
1223 +1. a filesystem, callee of VFS helper
1224 +2. sub-VFS, caller of VFS helper for branches
1225 +3. a virtual filesystem which maintains persistent inode number
1226 +4. reader/writer of files on branches such like an application
1227 +
1228 +1. Callee of VFS Helper
1229 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1230 +unlink(2) from an application reaches sys_unlink() kernel function and
1231 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1232 +calls filesystem specific unlink operation. Actually aufs implements the
1233 +unlink operation but it behaves like a redirector.
1234 +
1235 +2. Caller of VFS Helper for Branches
1236 +aufs_unlink() passes the unlink request to the branch filesystem as if
1237 +it were called from VFS. So the called unlink operation of the branch
1238 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1239 +every necessary pre/post operation for the branch filesystem.
1240 +- acquire the lock for the parent dir on a branch
1241 +- lookup in a branch
1242 +- revalidate dentry on a branch
1243 +- mnt_want_write() for a branch
1244 +- vfs_unlink() for a branch
1245 +- mnt_drop_write() for a branch
1246 +- release the lock on a branch
1247 +
1248 +3. Persistent Inode Number
1249 +One of the most important issue for a filesystem is to maintain inode
1250 +numbers. This is particularly important to support exporting a
1251 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1252 +backend block device for its own. But some storage is necessary to
1253 +keep and maintain the inode numbers. It may be a large space and may not
1254 +suit to keep in memory. Aufs rents some space from its first writable
1255 +branch filesystem (by default) and creates file(s) on it. These files
1256 +are created by aufs internally and removed soon (currently) keeping
1257 +opened.
1258 +Note: Because these files are removed, they are totally gone after
1259 +      unmounting aufs. It means the inode numbers are not persistent
1260 +      across unmount or reboot. I have a plan to make them really
1261 +      persistent which will be important for aufs on NFS server.
1262 +
1263 +4. Read/Write Files Internally (copy-on-write)
1264 +Because a branch can be readonly, when you write a file on it, aufs will
1265 +"copy-up" it to the upper writable branch internally. And then write the
1266 +originally requested thing to the file. Generally kernel doesn't
1267 +open/read/write file actively. In aufs, even a single write may cause a
1268 +internal "file copy". This behaviour is very similar to cp(1) command.
1269 +
1270 +Some people may think it is better to pass such work to user space
1271 +helper, instead of doing in kernel space. Actually I am still thinking
1272 +about it. But currently I have implemented it in kernel space.
1273 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1274 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1275 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2016-04-24 18:32:51.390353525 +0200
1276 @@ -0,0 +1,258 @@
1277 +
1278 +# Copyright (C) 2005-2016 Junjiro R. Okajima
1279 +# 
1280 +# This program is free software; you can redistribute it and/or modify
1281 +# it under the terms of the GNU General Public License as published by
1282 +# the Free Software Foundation; either version 2 of the License, or
1283 +# (at your option) any later version.
1284 +# 
1285 +# This program is distributed in the hope that it will be useful,
1286 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1287 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1288 +# GNU General Public License for more details.
1289 +# 
1290 +# You should have received a copy of the GNU General Public License
1291 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1292 +
1293 +Basic Aufs Internal Structure
1294 +
1295 +Superblock/Inode/Dentry/File Objects
1296 +----------------------------------------------------------------------
1297 +As like an ordinary filesystem, aufs has its own
1298 +superblock/inode/dentry/file objects. All these objects have a
1299 +dynamically allocated array and store the same kind of pointers to the
1300 +lower filesystem, branch.
1301 +For example, when you build a union with one readwrite branch and one
1302 +readonly, mounted /au, /rw and /ro respectively.
1303 +- /au = /rw + /ro
1304 +- /ro/fileA exists but /rw/fileA
1305 +
1306 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1307 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1308 +- [0] = NULL (because /rw/fileA doesn't exist)
1309 +- [1] = /ro/fileA
1310 +
1311 +This style of an array is essentially same to the aufs
1312 +superblock/inode/dentry/file objects.
1313 +
1314 +Because aufs supports manipulating branches, ie. add/delete/change
1315 +branches dynamically, these objects has its own generation. When
1316 +branches are changed, the generation in aufs superblock is
1317 +incremented. And a generation in other object are compared when it is
1318 +accessed. When a generation in other objects are obsoleted, aufs
1319 +refreshes the internal array.
1320 +
1321 +
1322 +Superblock
1323 +----------------------------------------------------------------------
1324 +Additionally aufs superblock has some data for policies to select one
1325 +among multiple writable branches, XIB files, pseudo-links and kobject.
1326 +See below in detail.
1327 +About the policies which supports copy-down a directory, see
1328 +wbr_policy.txt too.
1329 +
1330 +
1331 +Branch and XINO(External Inode Number Translation Table)
1332 +----------------------------------------------------------------------
1333 +Every branch has its own xino (external inode number translation table)
1334 +file. The xino file is created and unlinked by aufs internally. When two
1335 +members of a union exist on the same filesystem, they share the single
1336 +xino file.
1337 +The struct of a xino file is simple, just a sequence of aufs inode
1338 +numbers which is indexed by the lower inode number.
1339 +In the above sample, assume the inode number of /ro/fileA is i111 and
1340 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1341 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1342 +
1343 +When the inode numbers are not contiguous, the xino file will be sparse
1344 +which has a hole in it and doesn't consume as much disk space as it
1345 +might appear. If your branch filesystem consumes disk space for such
1346 +holes, then you should specify 'xino=' option at mounting aufs.
1347 +
1348 +Aufs has a mount option to free the disk blocks for such holes in XINO
1349 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1350 +meet a problem of disk shortage due to XINO files, then you should try
1351 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1352 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1353 +the holes in XINO files.
1354 +
1355 +Also a writable branch has three kinds of "whiteout bases". All these
1356 +are existed when the branch is joined to aufs, and their names are
1357 +whiteout-ed doubly, so that users will never see their names in aufs
1358 +hierarchy.
1359 +1. a regular file which will be hardlinked to all whiteouts.
1360 +2. a directory to store a pseudo-link.
1361 +3. a directory to store an "orphan"-ed file temporary.
1362 +
1363 +1. Whiteout Base
1364 +   When you remove a file on a readonly branch, aufs handles it as a
1365 +   logical deletion and creates a whiteout on the upper writable branch
1366 +   as a hardlink of this file in order not to consume inode on the
1367 +   writable branch.
1368 +2. Pseudo-link Dir
1369 +   See below, Pseudo-link.
1370 +3. Step-Parent Dir
1371 +   When "fileC" exists on the lower readonly branch only and it is
1372 +   opened and removed with its parent dir, and then user writes
1373 +   something into it, then aufs copies-up fileC to this
1374 +   directory. Because there is no other dir to store fileC. After
1375 +   creating a file under this dir, the file is unlinked.
1376 +
1377 +Because aufs supports manipulating branches, ie. add/delete/change
1378 +dynamically, a branch has its own id. When the branch order changes,
1379 +aufs finds the new index by searching the branch id.
1380 +
1381 +
1382 +Pseudo-link
1383 +----------------------------------------------------------------------
1384 +Assume "fileA" exists on the lower readonly branch only and it is
1385 +hardlinked to "fileB" on the branch. When you write something to fileA,
1386 +aufs copies-up it to the upper writable branch. Additionally aufs
1387 +creates a hardlink under the Pseudo-link Directory of the writable
1388 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1389 +simple list. If fileB is read after unlinking fileA, aufs returns
1390 +filedata from the pseudo-link instead of the lower readonly
1391 +branch. Because the pseudo-link is based upon the inode, to keep the
1392 +inode number by xino (see above) is essentially necessary.
1393 +
1394 +All the hardlinks under the Pseudo-link Directory of the writable branch
1395 +should be restored in a proper location later. Aufs provides a utility
1396 +to do this. The userspace helpers executed at remounting and unmounting
1397 +aufs by default.
1398 +During this utility is running, it puts aufs into the pseudo-link
1399 +maintenance mode. In this mode, only the process which began the
1400 +maintenance mode (and its child processes) is allowed to operate in
1401 +aufs. Some other processes which are not related to the pseudo-link will
1402 +be allowed to run too, but the rest have to return an error or wait
1403 +until the maintenance mode ends. If a process already acquires an inode
1404 +mutex (in VFS), it has to return an error.
1405 +
1406 +
1407 +XIB(external inode number bitmap)
1408 +----------------------------------------------------------------------
1409 +Addition to the xino file per a branch, aufs has an external inode number
1410 +bitmap in a superblock object. It is also an internal file such like a
1411 +xino file.
1412 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1413 +not.
1414 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1415 +
1416 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1417 +reduce the number of consumed disk blocks for these files.
1418 +
1419 +
1420 +Virtual or Vertical Dir, and Readdir in Userspace
1421 +----------------------------------------------------------------------
1422 +In order to support multiple layers (branches), aufs readdir operation
1423 +constructs a virtual dir block on memory. For readdir, aufs calls
1424 +vfs_readdir() internally for each dir on branches, merges their entries
1425 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1426 +object. So the file object has its entry list until it is closed. The
1427 +entry list will be updated when the file position is zero and becomes
1428 +obsoleted. This decision is made in aufs automatically.
1429 +
1430 +The dynamically allocated memory block for the name of entries has a
1431 +unit of 512 bytes (by default) and stores the names contiguously (no
1432 +padding). Another block for each entry is handled by kmem_cache too.
1433 +During building dir blocks, aufs creates hash list and judging whether
1434 +the entry is whiteouted by its upper branch or already listed.
1435 +The merged result is cached in the corresponding inode object and
1436 +maintained by a customizable life-time option.
1437 +
1438 +Some people may call it can be a security hole or invite DoS attack
1439 +since the opened and once readdir-ed dir (file object) holds its entry
1440 +list and becomes a pressure for system memory. But I'd say it is similar
1441 +to files under /proc or /sys. The virtual files in them also holds a
1442 +memory page (generally) while they are opened. When an idea to reduce
1443 +memory for them is introduced, it will be applied to aufs too.
1444 +For those who really hate this situation, I've developed readdir(3)
1445 +library which operates this merging in userspace. You just need to set
1446 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1447 +kernel space for readdir(3).
1448 +
1449 +
1450 +Workqueue
1451 +----------------------------------------------------------------------
1452 +Aufs sometimes requires privilege access to a branch. For instance,
1453 +in copy-up/down operation. When a user process is going to make changes
1454 +to a file which exists in the lower readonly branch only, and the mode
1455 +of one of ancestor directories may not be writable by a user
1456 +process. Here aufs copy-up the file with its ancestors and they may
1457 +require privilege to set its owner/group/mode/etc.
1458 +This is a typical case of a application character of aufs (see
1459 +Introduction).
1460 +
1461 +Aufs uses workqueue synchronously for this case. It creates its own
1462 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1463 +passes the request to call mkdir or write (for example), and wait for
1464 +its completion. This approach solves a problem of a signal handler
1465 +simply.
1466 +If aufs didn't adopt the workqueue and changed the privilege of the
1467 +process, then the process may receive the unexpected SIGXFSZ or other
1468 +signals.
1469 +
1470 +Also aufs uses the system global workqueue ("events" kernel thread) too
1471 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1472 +whiteout base and etc. This is unrelated to a privilege.
1473 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1474 +superblock at the beginning, at the same time waits for the completion
1475 +of all queued asynchronous tasks.
1476 +
1477 +
1478 +Whiteout
1479 +----------------------------------------------------------------------
1480 +The whiteout in aufs is very similar to Unionfs's. That is represented
1481 +by its filename. UnionMount takes an approach of a file mode, but I am
1482 +afraid several utilities (find(1) or something) will have to support it.
1483 +
1484 +Basically the whiteout represents "logical deletion" which stops aufs to
1485 +lookup further, but also it represents "dir is opaque" which also stop
1486 +further lookup.
1487 +
1488 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1489 +In order to make several functions in a single systemcall to be
1490 +revertible, aufs adopts an approach to rename a directory to a temporary
1491 +unique whiteouted name.
1492 +For example, in rename(2) dir where the target dir already existed, aufs
1493 +renames the target dir to a temporary unique whiteouted name before the
1494 +actual rename on a branch, and then handles other actions (make it opaque,
1495 +update the attributes, etc). If an error happens in these actions, aufs
1496 +simply renames the whiteouted name back and returns an error. If all are
1497 +succeeded, aufs registers a function to remove the whiteouted unique
1498 +temporary name completely and asynchronously to the system global
1499 +workqueue.
1500 +
1501 +
1502 +Copy-up
1503 +----------------------------------------------------------------------
1504 +It is a well-known feature or concept.
1505 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1506 +internally and makes change to the new file on the upper writable branch.
1507 +When the trigger systemcall does not update the timestamps of the parent
1508 +dir, aufs reverts it after copy-up.
1509 +
1510 +
1511 +Move-down (aufs3.9 and later)
1512 +----------------------------------------------------------------------
1513 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1514 +the lower readonly branch to the upper writable branch when a user
1515 +changes something about the file.
1516 +"Move-down" is an opposite action of copy-up. Basically this action is
1517 +ran manually instead of automatically and internally.
1518 +For desgin and implementation, aufs has to consider these issues.
1519 +- whiteout for the file may exist on the lower branch.
1520 +- ancestor directories may not exist on the lower branch.
1521 +- diropq for the ancestor directories may exist on the upper branch.
1522 +- free space on the lower branch will reduce.
1523 +- another access to the file may happen during moving-down, including
1524 +  UDBA (see "Revalidate Dentry and UDBA").
1525 +- the file should not be hard-linked nor pseudo-linked. they should be
1526 +  handled by auplink utility later.
1527 +
1528 +Sometimes users want to move-down a file from the upper writable branch
1529 +to the lower readonly or writable branch. For instance,
1530 +- the free space of the upper writable branch is going to run out.
1531 +- create a new intermediate branch between the upper and lower branch.
1532 +- etc.
1533 +
1534 +For this purpose, use "aumvdown" command in aufs-util.git.
1535 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1536 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1537 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2016-04-24 18:32:51.390353525 +0200
1538 @@ -0,0 +1,85 @@
1539 +
1540 +# Copyright (C) 2015-2016 Junjiro R. Okajima
1541 +# 
1542 +# This program is free software; you can redistribute it and/or modify
1543 +# it under the terms of the GNU General Public License as published by
1544 +# the Free Software Foundation; either version 2 of the License, or
1545 +# (at your option) any later version.
1546 +# 
1547 +# This program is distributed in the hope that it will be useful,
1548 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1549 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1550 +# GNU General Public License for more details.
1551 +# 
1552 +# You should have received a copy of the GNU General Public License
1553 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1554 +
1555 +Support for a branch who has its ->atomic_open()
1556 +----------------------------------------------------------------------
1557 +The filesystems who implement its ->atomic_open() are not majority. For
1558 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1559 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1560 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1561 +sure whether all filesystems who have ->atomic_open() behave like this,
1562 +but NFSv4 surely returns the error.
1563 +
1564 +In order to support ->atomic_open() for aufs, there are a few
1565 +approaches.
1566 +
1567 +A. Introduce aufs_atomic_open()
1568 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1569 +     branch fs.
1570 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1571 +   an aufs user Pip Cet's approach
1572 +   - calls aufs_create(), VFS finish_open() and notify_change().
1573 +   - pass fake-mode to finish_open(), and then correct the mode by
1574 +     notify_change().
1575 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1576 +   - no aufs_atomic_open().
1577 +   - aufs_lookup() registers the TID to an aufs internal object.
1578 +   - aufs_create() does nothing when the matching TID is registered, but
1579 +     registers the mode.
1580 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1581 +     TID is registered.
1582 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1583 +   credential
1584 +   - no aufs_atomic_open().
1585 +   - aufs_create() registers the TID to an internal object. this info
1586 +     represents "this process created this file just now."
1587 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1588 +     registered TID and re-try open() with superuser's credential.
1589 +
1590 +Pros and cons for each approach.
1591 +
1592 +A.
1593 +   - straightforward but highly depends upon VFS internal.
1594 +   - the atomic behavaiour is kept.
1595 +   - some of parameters such as nameidata are hard to reproduce for
1596 +     branch fs.
1597 +   - large overhead.
1598 +B.
1599 +   - easy to implement.
1600 +   - the atomic behavaiour is lost.
1601 +C.
1602 +   - the atomic behavaiour is kept.
1603 +   - dirty and tricky.
1604 +   - VFS checks whether the file is created correctly after calling
1605 +     ->create(), which means this approach doesn't work.
1606 +D.
1607 +   - easy to implement.
1608 +   - the atomic behavaiour is lost.
1609 +   - to open a file with superuser's credential and give it to a user
1610 +     process is a bad idea, since the file object keeps the credential
1611 +     in it. It may affect LSM or something. This approach doesn't work
1612 +     either.
1613 +
1614 +The approach A is ideal, but it hard to implement. So here is a
1615 +variation of A, which is to be implemented.
1616 +
1617 +A-1. Introduce aufs_atomic_open()
1618 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1619 +       vfs_create() and finish_open().
1620 +     - the demerit is that the several checks after branch fs
1621 +       ->atomic_open() are lost. in the ordinary case, the checks are
1622 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1623 +       be implemented in aufs, but not all I am afraid.
1624 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1625 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1626 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2016-04-24 18:32:51.390353525 +0200
1627 @@ -0,0 +1,113 @@
1628 +
1629 +# Copyright (C) 2005-2016 Junjiro R. Okajima
1630 +# 
1631 +# This program is free software; you can redistribute it and/or modify
1632 +# it under the terms of the GNU General Public License as published by
1633 +# the Free Software Foundation; either version 2 of the License, or
1634 +# (at your option) any later version.
1635 +# 
1636 +# This program is distributed in the hope that it will be useful,
1637 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1638 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1639 +# GNU General Public License for more details.
1640 +# 
1641 +# You should have received a copy of the GNU General Public License
1642 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1643 +
1644 +Lookup in a Branch
1645 +----------------------------------------------------------------------
1646 +Since aufs has a character of sub-VFS (see Introduction), it operates
1647 +lookup for branches as VFS does. It may be a heavy work. But almost all
1648 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1649 +directly connected to its parent. Digging down the directory hierarchy
1650 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1651 +aufs calls it.
1652 +
1653 +When a branch is a remote filesystem, aufs basically relies upon its
1654 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1655 +them.
1656 +For d_revalidate, aufs implements three levels of revalidate tests. See
1657 +"Revalidate Dentry and UDBA" in detail.
1658 +
1659 +
1660 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1661 +----------------------------------------------------------------------
1662 +Let's try case study.
1663 +- aufs has two branches, upper readwrite and lower readonly.
1664 +  /au = /rw + /ro
1665 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1666 +- user invoked "chmod a+rx /au/dirA"
1667 +- the internal copy-up is activated and "/rw/dirA" is created and its
1668 +  permission bits are set to world readable.
1669 +- then "/au/dirA" becomes world readable?
1670 +
1671 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1672 +or it may be a natively readonly filesystem. If aufs respects the lower
1673 +branch, it should not respond readdir request from other users. But user
1674 +allowed it by chmod. Should really aufs rejects showing the entries
1675 +under /ro/dirA?
1676 +
1677 +To be honest, I don't have a good solution for this case. So aufs
1678 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1679 +users.
1680 +When dirperm1 is specified, aufs checks only the highest one for the
1681 +directory permission, and shows the entries. Otherwise, as usual, checks
1682 +every dir existing on all branches and rejects the request.
1683 +
1684 +As a side effect, dirperm1 option improves the performance of aufs
1685 +because the number of permission check is reduced when the number of
1686 +branch is many.
1687 +
1688 +
1689 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1690 +----------------------------------------------------------------------
1691 +Generally VFS helpers re-validate a dentry as a part of lookup.
1692 +0. digging down the directory hierarchy.
1693 +1. lock the parent dir by its i_mutex.
1694 +2. lookup the final (child) entry.
1695 +3. revalidate it.
1696 +4. call the actual operation (create, unlink, etc.)
1697 +5. unlock the parent dir
1698 +
1699 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1700 +called. Actually aufs implements it and checks the dentry on a branch is
1701 +still valid.
1702 +But it is not enough. Because aufs has to release the lock for the
1703 +parent dir on a branch at the end of ->lookup() (step 2) and
1704 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1705 +held by VFS.
1706 +If the file on a branch is changed directly, eg. bypassing aufs, after
1707 +aufs released the lock, then the subsequent operation may cause
1708 +something unpleasant result.
1709 +
1710 +This situation is a result of VFS architecture, ->lookup() and
1711 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1712 +design from VFS's point of view. It is just not suitable for sub-VFS
1713 +character in aufs.
1714 +
1715 +Aufs supports such case by three level of revalidation which is
1716 +selectable by user.
1717 +1. Simple Revalidate
1718 +   Addition to the native flow in VFS's, confirm the child-parent
1719 +   relationship on the branch just after locking the parent dir on the
1720 +   branch in the "actual operation" (step 4). When this validation
1721 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1722 +   checks the validation of the dentry on branches.
1723 +2. Monitor Changes Internally by Inotify/Fsnotify
1724 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1725 +   the dentry on the branch, and returns EBUSY if it finds different
1726 +   dentry.
1727 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1728 +   during it is in cache. When the event is notified, aufs registers a
1729 +   function to kernel 'events' thread by schedule_work(). And the
1730 +   function sets some special status to the cached aufs dentry and inode
1731 +   private data. If they are not cached, then aufs has nothing to
1732 +   do. When the same file is accessed through aufs (step 0-3) later,
1733 +   aufs will detect the status and refresh all necessary data.
1734 +   In this mode, aufs has to ignore the event which is fired by aufs
1735 +   itself.
1736 +3. No Extra Validation
1737 +   This is the simplest test and doesn't add any additional revalidation
1738 +   test, and skip the revalidation in step 4. It is useful and improves
1739 +   aufs performance when system surely hide the aufs branches from user,
1740 +   by over-mounting something (or another method).
1741 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1742 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1743 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2016-04-24 18:32:51.390353525 +0200
1744 @@ -0,0 +1,74 @@
1745 +
1746 +# Copyright (C) 2005-2016 Junjiro R. Okajima
1747 +# 
1748 +# This program is free software; you can redistribute it and/or modify
1749 +# it under the terms of the GNU General Public License as published by
1750 +# the Free Software Foundation; either version 2 of the License, or
1751 +# (at your option) any later version.
1752 +# 
1753 +# This program is distributed in the hope that it will be useful,
1754 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1755 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1756 +# GNU General Public License for more details.
1757 +# 
1758 +# You should have received a copy of the GNU General Public License
1759 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1760 +
1761 +Branch Manipulation
1762 +
1763 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1764 +and changing its permission/attribute, there are a lot of works to do.
1765 +
1766 +
1767 +Add a Branch
1768 +----------------------------------------------------------------------
1769 +o Confirm the adding dir exists outside of aufs, including loopback
1770 +  mount, and its various attributes.
1771 +o Initialize the xino file and whiteout bases if necessary.
1772 +  See struct.txt.
1773 +
1774 +o Check the owner/group/mode of the directory
1775 +  When the owner/group/mode of the adding directory differs from the
1776 +  existing branch, aufs issues a warning because it may impose a
1777 +  security risk.
1778 +  For example, when a upper writable branch has a world writable empty
1779 +  top directory, a malicious user can create any files on the writable
1780 +  branch directly, like copy-up and modify manually. If something like
1781 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1782 +  writable branch, and the writable branch is world-writable, then a
1783 +  malicious guy may create /etc/passwd on the writable branch directly
1784 +  and the infected file will be valid in aufs.
1785 +  I am afraid it can be a security issue, but aufs can do nothing except
1786 +  producing a warning.
1787 +
1788 +
1789 +Delete a Branch
1790 +----------------------------------------------------------------------
1791 +o Confirm the deleting branch is not busy
1792 +  To be general, there is one merit to adopt "remount" interface to
1793 +  manipulate branches. It is to discard caches. At deleting a branch,
1794 +  aufs checks the still cached (and connected) dentries and inodes. If
1795 +  there are any, then they are all in-use. An inode without its
1796 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1797 +
1798 +  For the cached one, aufs checks whether the same named entry exists on
1799 +  other branches.
1800 +  If the cached one is a directory, because aufs provides a merged view
1801 +  to users, as long as one dir is left on any branch aufs can show the
1802 +  dir to users. In this case, the branch can be removed from aufs.
1803 +  Otherwise aufs rejects deleting the branch.
1804 +
1805 +  If any file on the deleting branch is opened by aufs, then aufs
1806 +  rejects deleting.
1807 +
1808 +
1809 +Modify the Permission of a Branch
1810 +----------------------------------------------------------------------
1811 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1812 +  See struct.txt.
1813 +
1814 +o rw --> ro: Confirm the modifying branch is not busy
1815 +  Aufs rejects the request if any of these conditions are true.
1816 +  - a file on the branch is mmap-ed.
1817 +  - a regular file on the branch is opened for write and there is no
1818 +    same named entry on the upper branch.
1819 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1820 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1821 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2016-04-24 18:32:51.390353525 +0200
1822 @@ -0,0 +1,64 @@
1823 +
1824 +# Copyright (C) 2005-2016 Junjiro R. Okajima
1825 +# 
1826 +# This program is free software; you can redistribute it and/or modify
1827 +# it under the terms of the GNU General Public License as published by
1828 +# the Free Software Foundation; either version 2 of the License, or
1829 +# (at your option) any later version.
1830 +# 
1831 +# This program is distributed in the hope that it will be useful,
1832 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1833 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1834 +# GNU General Public License for more details.
1835 +# 
1836 +# You should have received a copy of the GNU General Public License
1837 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1838 +
1839 +Policies to Select One among Multiple Writable Branches
1840 +----------------------------------------------------------------------
1841 +When the number of writable branch is more than one, aufs has to decide
1842 +the target branch for file creation or copy-up. By default, the highest
1843 +writable branch which has the parent (or ancestor) dir of the target
1844 +file is chosen (top-down-parent policy).
1845 +By user's request, aufs implements some other policies to select the
1846 +writable branch, for file creation several policies, round-robin,
1847 +most-free-space, and other policies. For copy-up, top-down-parent,
1848 +bottom-up-parent, bottom-up and others.
1849 +
1850 +As expected, the round-robin policy selects the branch in circular. When
1851 +you have two writable branches and creates 10 new files, 5 files will be
1852 +created for each branch. mkdir(2) systemcall is an exception. When you
1853 +create 10 new directories, all will be created on the same branch.
1854 +And the most-free-space policy selects the one which has most free
1855 +space among the writable branches. The amount of free space will be
1856 +checked by aufs internally, and users can specify its time interval.
1857 +
1858 +The policies for copy-up is more simple,
1859 +top-down-parent is equivalent to the same named on in create policy,
1860 +bottom-up-parent selects the writable branch where the parent dir
1861 +exists and the nearest upper one from the copyup-source,
1862 +bottom-up selects the nearest upper writable branch from the
1863 +copyup-source, regardless the existence of the parent dir.
1864 +
1865 +There are some rules or exceptions to apply these policies.
1866 +- If there is a readonly branch above the policy-selected branch and
1867 +  the parent dir is marked as opaque (a variation of whiteout), or the
1868 +  target (creating) file is whiteout-ed on the upper readonly branch,
1869 +  then the result of the policy is ignored and the target file will be
1870 +  created on the nearest upper writable branch than the readonly branch.
1871 +- If there is a writable branch above the policy-selected branch and
1872 +  the parent dir is marked as opaque or the target file is whiteouted
1873 +  on the branch, then the result of the policy is ignored and the target
1874 +  file will be created on the highest one among the upper writable
1875 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1876 +  it as usual.
1877 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1878 +  They try selecting the branch where the source exists as possible
1879 +  since copyup a large file will take long time. If it can't be,
1880 +  ie. the branch where the source exists is readonly, then they will
1881 +  follow the copyup policy.
1882 +- There is an exception for rename(2) when the target exists.
1883 +  If the rename target exists, aufs compares the index of the branches
1884 +  where the source and the target exists and selects the higher
1885 +  one. If the selected branch is readonly, then aufs follows the
1886 +  copyup policy.
1887 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1888 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1889 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2016-04-24 18:32:51.390353525 +0200
1890 @@ -0,0 +1,120 @@
1891 +
1892 +# Copyright (C) 2011-2016 Junjiro R. Okajima
1893 +# 
1894 +# This program is free software; you can redistribute it and/or modify
1895 +# it under the terms of the GNU General Public License as published by
1896 +# the Free Software Foundation; either version 2 of the License, or
1897 +# (at your option) any later version.
1898 +# 
1899 +# This program is distributed in the hope that it will be useful,
1900 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1901 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1902 +# GNU General Public License for more details.
1903 +# 
1904 +# You should have received a copy of the GNU General Public License
1905 +# along with this program; if not, write to the Free Software
1906 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1907 +
1908 +
1909 +File-based Hierarchical Storage Management (FHSM)
1910 +----------------------------------------------------------------------
1911 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1912 +storage world. Aufs provides this feature as file-based with multiple
1913 +writable branches, based upon the principle of "Colder, the Lower".
1914 +Here the word "colder" means that the less used files, and "lower" means
1915 +that the position in the order of the stacked branches vertically.
1916 +These multiple writable branches are prioritized, ie. the topmost one
1917 +should be the fastest drive and be used heavily.
1918 +
1919 +o Characters in aufs FHSM story
1920 +- aufs itself and a new branch attribute.
1921 +- a new ioctl interface to move-down and to establish a connection with
1922 +  the daemon ("move-down" is a converse of "copy-up").
1923 +- userspace tool and daemon.
1924 +
1925 +The userspace daemon establishes a connection with aufs and waits for
1926 +the notification. The notified information is very similar to struct
1927 +statfs containing the number of consumed blocks and inodes.
1928 +When the consumed blocks/inodes of a branch exceeds the user-specified
1929 +upper watermark, the daemon activates its move-down process until the
1930 +consumed blocks/inodes reaches the user-specified lower watermark.
1931 +
1932 +The actual move-down is done by aufs based upon the request from
1933 +user-space since we need to maintain the inode number and the internal
1934 +pointer arrays in aufs.
1935 +
1936 +Currently aufs FHSM handles the regular files only. Additionally they
1937 +must not be hard-linked nor pseudo-linked.
1938 +
1939 +
1940 +o Cowork of aufs and the user-space daemon
1941 +  During the userspace daemon established the connection, aufs sends a
1942 +  small notification to it whenever aufs writes something into the
1943 +  writable branch. But it may cost high since aufs issues statfs(2)
1944 +  internally. So user can specify a new option to cache the
1945 +  info. Actually the notification is controlled by these factors.
1946 +  + the specified cache time.
1947 +  + classified as "force" by aufs internally.
1948 +  Until the specified time expires, aufs doesn't send the info
1949 +  except the forced cases. When aufs decide forcing, the info is always
1950 +  notified to userspace.
1951 +  For example, the number of free inodes is generally large enough and
1952 +  the shortage of it happens rarely. So aufs doesn't force the
1953 +  notification when creating a new file, directory and others. This is
1954 +  the typical case which aufs doesn't force.
1955 +  When aufs writes the actual filedata and the files consumes any of new
1956 +  blocks, the aufs forces notifying.
1957 +
1958 +
1959 +o Interfaces in aufs
1960 +- New branch attribute.
1961 +  + fhsm
1962 +    Specifies that the branch is managed by FHSM feature. In other word,
1963 +    participant in the FHSM.
1964 +    When nofhsm is set to the branch, it will not be the source/target
1965 +    branch of the move-down operation. This attribute is set
1966 +    independently from coo and moo attributes, and if you want full
1967 +    FHSM, you should specify them as well.
1968 +- New mount option.
1969 +  + fhsm_sec
1970 +    Specifies a second to suppress many less important info to be
1971 +    notified.
1972 +- New ioctl.
1973 +  + AUFS_CTL_FHSM_FD
1974 +    create a new file descriptor which userspace can read the notification
1975 +    (a subset of struct statfs) from aufs.
1976 +- Module parameter 'brs'
1977 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1978 +  be set.
1979 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1980 +  When there are two or more branches with fhsm attributes,
1981 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1982 +  terminates it. As a result of remounting and branch-manipulation, the
1983 +  number of branches with fhsm attribute can be one. In this case,
1984 +  /sbin/mount.aufs will terminate the user-space daemon.
1985 +
1986 +
1987 +Finally the operation is done as these steps in kernel-space.
1988 +- make sure that,
1989 +  + no one else is using the file.
1990 +  + the file is not hard-linked.
1991 +  + the file is not pseudo-linked.
1992 +  + the file is a regular file.
1993 +  + the parent dir is not opaqued.
1994 +- find the target writable branch.
1995 +- make sure the file is not whiteout-ed by the upper (than the target)
1996 +  branch.
1997 +- make the parent dir on the target branch.
1998 +- mutex lock the inode on the branch.
1999 +- unlink the whiteout on the target branch (if exists).
2000 +- lookup and create the whiteout-ed temporary name on the target branch.
2001 +- copy the file as the whiteout-ed temporary name on the target branch.
2002 +- rename the whiteout-ed temporary name to the original name.
2003 +- unlink the file on the source branch.
2004 +- maintain the internal pointer array and the external inode number
2005 +  table (XINO).
2006 +- maintain the timestamps and other attributes of the parent dir and the
2007 +  file.
2008 +
2009 +And of course, in every step, an error may happen. So the operation
2010 +should restore the original file state after an error happens.
2011 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2012 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2013 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2016-04-24 18:32:51.390353525 +0200
2014 @@ -0,0 +1,72 @@
2015 +
2016 +# Copyright (C) 2005-2016 Junjiro R. Okajima
2017 +# 
2018 +# This program is free software; you can redistribute it and/or modify
2019 +# it under the terms of the GNU General Public License as published by
2020 +# the Free Software Foundation; either version 2 of the License, or
2021 +# (at your option) any later version.
2022 +# 
2023 +# This program is distributed in the hope that it will be useful,
2024 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2025 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2026 +# GNU General Public License for more details.
2027 +# 
2028 +# You should have received a copy of the GNU General Public License
2029 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2030 +
2031 +mmap(2) -- File Memory Mapping
2032 +----------------------------------------------------------------------
2033 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2034 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2035 +->mmap().
2036 +This approach is simple and good, but there is one problem.
2037 +Under /proc, several entries show the mmapped files by its path (with
2038 +device and inode number), and the printed path will be the path on the
2039 +branch fs's instead of virtual aufs's.
2040 +This is not a problem in most cases, but some utilities lsof(1) (and its
2041 +user) may expect the path on aufs.
2042 +
2043 +To address this issue, aufs adds a new member called vm_prfile in struct
2044 +vm_area_struct (and struct vm_region). The original vm_file points to
2045 +the file on the branch fs in order to handle everything correctly as
2046 +usual. The new vm_prfile points to a virtual file in aufs, and the
2047 +show-functions in procfs refers to vm_prfile if it is set.
2048 +Also we need to maintain several other places where touching vm_file
2049 +such like
2050 +- fork()/clone() copies vma and the reference count of vm_file is
2051 +  incremented.
2052 +- merging vma maintains the ref count too.
2053 +
2054 +This is not a good approach. It just fakes the printed path. But it
2055 +leaves all behaviour around f_mapping unchanged. This is surely an
2056 +advantage.
2057 +Actually aufs had adopted another complicated approach which calls
2058 +generic_file_mmap() and handles struct vm_operations_struct. In this
2059 +approach, aufs met a hard problem and I could not solve it without
2060 +switching the approach.
2061 +
2062 +There may be one more another approach which is
2063 +- bind-mount the branch-root onto the aufs-root internally
2064 +- grab the new vfsmount (ie. struct mount)
2065 +- lazy-umount the branch-root internally
2066 +- in open(2) the aufs-file, open the branch-file with the hidden
2067 +  vfsmount (instead of the original branch's vfsmount)
2068 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2069 +  but it may be possible from userspace by the mount helper.
2070 +
2071 +Adding the internal hidden vfsmount and using it in opening a file, the
2072 +file path under /proc will be printed correctly. This approach looks
2073 +smarter, but is not possible I am afraid.
2074 +- aufs-root may be bind-mount later. when it happens, another hidden
2075 +  vfsmount will be required.
2076 +- it is hard to get the chance to bind-mount and lazy-umount
2077 +  + in kernel-space, FS can have vfsmount in open(2) via
2078 +    file->f_path, and aufs can know its vfsmount. But several locks are
2079 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2080 +    here, then it may cause a deadlock.
2081 +  + in user-space, bind-mount doesn't invoke the mount helper.
2082 +- since /proc shows dev and ino, aufs has to give vma these info. it
2083 +  means a new member vm_prinode will be necessary. this is essentially
2084 +  equivalent to vm_prfile described above.
2085 +
2086 +I have to give up this "looks-smater" approach.
2087 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2088 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2089 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2016-04-24 18:32:51.390353525 +0200
2090 @@ -0,0 +1,96 @@
2091 +
2092 +# Copyright (C) 2014-2016 Junjiro R. Okajima
2093 +#
2094 +# This program is free software; you can redistribute it and/or modify
2095 +# it under the terms of the GNU General Public License as published by
2096 +# the Free Software Foundation; either version 2 of the License, or
2097 +# (at your option) any later version.
2098 +#
2099 +# This program is distributed in the hope that it will be useful,
2100 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2101 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2102 +# GNU General Public License for more details.
2103 +#
2104 +# You should have received a copy of the GNU General Public License
2105 +# along with this program; if not, write to the Free Software
2106 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2107 +
2108 +
2109 +Listing XATTR/EA and getting the value
2110 +----------------------------------------------------------------------
2111 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2112 +shows the values from the topmost existing file. This behaviour is good
2113 +for the non-dir entries since the bahaviour exactly matches the shown
2114 +information. But for the directories, aufs considers all the same named
2115 +entries on the lower branches. Which means, if one of the lower entry
2116 +rejects readdir call, then aufs returns an error even if the topmost
2117 +entry allows it. This behaviour is necessary to respect the branch fs's
2118 +security, but can make users confused since the user-visible standard
2119 +attributes don't match the behaviour.
2120 +To address this issue, aufs has a mount option called dirperm1 which
2121 +checks the permission for the topmost entry only, and ignores the lower
2122 +entry's permission.
2123 +
2124 +A similar issue can happen around XATTR.
2125 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2126 +always set. Otherwise these very unpleasant situation would happen.
2127 +- listxattr(2) may return the duplicated entries.
2128 +- users may not be able to remove or reset the XATTR forever,
2129 +
2130 +
2131 +XATTR/EA support in the internal (copy,move)-(up,down)
2132 +----------------------------------------------------------------------
2133 +Generally the extended attributes of inode are categorized as these.
2134 +- "security" for LSM and capability.
2135 +- "system" for posix ACL, 'acl' mount option is required for the branch
2136 +  fs generally.
2137 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2138 +- "user" for userspace, 'user_xattr' mount option is required for the
2139 +  branch fs generally.
2140 +
2141 +Moreover there are some other categories. Aufs handles these rather
2142 +unpopular categories as the ordinary ones, ie. there is no special
2143 +condition nor exception.
2144 +
2145 +In copy-up, the support for XATTR on the dst branch may differ from the
2146 +src branch. In this case, the copy-up operation will get an error and
2147 +the original user operation which triggered the copy-up will fail. It
2148 +can happen that even all copy-up will fail.
2149 +When both of src and dst branches support XATTR and if an error occurs
2150 +during copying XATTR, then the copy-up should fail obviously. That is a
2151 +good reason and aufs should return an error to userspace. But when only
2152 +the src branch support that XATTR, aufs should not return an error.
2153 +For example, the src branch supports ACL but the dst branch doesn't
2154 +because the dst branch may natively un-support it or temporary
2155 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2156 +may NOT return an error even if the XATTR is not supported. It is
2157 +totally up to the branch fs.
2158 +
2159 +Anyway when the aufs internal copy-up gets an error from the dst branch
2160 +fs, then aufs tries removing the just copied entry and returns the error
2161 +to the userspace. The worst case of this situation will be all copy-up
2162 +will fail.
2163 +
2164 +For the copy-up operation, there two basic approaches.
2165 +- copy the specified XATTR only (by category above), and return the
2166 +  error unconditionally if it happens.
2167 +- copy all XATTR, and ignore the error on the specified category only.
2168 +
2169 +In order to support XATTR and to implement the correct behaviour, aufs
2170 +chooses the latter approach and introduces some new branch attributes,
2171 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2172 +They correspond to the XATTR namespaces (see above). Additionally, to be
2173 +convenient, "icex" is also provided which means all "icex*" attributes
2174 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2175 +
2176 +The meaning of these attributes is to ignore the error from setting
2177 +XATTR on that branch.
2178 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2179 +error from the dst branch according to the specified attributes.
2180 +
2181 +Some XATTR may have its default value. The default value may come from
2182 +the parent dir or the environment. If the default value is set at the
2183 +file creating-time, it will be overwritten by copy-up.
2184 +Some contradiction may happen I am afraid.
2185 +Do we need another attribute to stop copying XATTR? I am unsure. For
2186 +now, aufs implements the branch attributes to ignore the error.
2187 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2188 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2189 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2016-04-24 18:32:51.390353525 +0200
2190 @@ -0,0 +1,58 @@
2191 +
2192 +# Copyright (C) 2005-2016 Junjiro R. Okajima
2193 +# 
2194 +# This program is free software; you can redistribute it and/or modify
2195 +# it under the terms of the GNU General Public License as published by
2196 +# the Free Software Foundation; either version 2 of the License, or
2197 +# (at your option) any later version.
2198 +# 
2199 +# This program is distributed in the hope that it will be useful,
2200 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2201 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2202 +# GNU General Public License for more details.
2203 +# 
2204 +# You should have received a copy of the GNU General Public License
2205 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2206 +
2207 +Export Aufs via NFS
2208 +----------------------------------------------------------------------
2209 +Here is an approach.
2210 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2211 +  generation.
2212 +- iget_locked(): initialize aufs inode generation for a new inode, and
2213 +  store it in xigen file.
2214 +- destroy_inode(): increment aufs inode generation and store it in xigen
2215 +  file. it is necessary even if it is not unlinked, because any data of
2216 +  inode may be changed by UDBA.
2217 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2218 +  build file handle by
2219 +  + branch id (4 bytes)
2220 +  + superblock generation (4 bytes)
2221 +  + inode number (4 or 8 bytes)
2222 +  + parent dir inode number (4 or 8 bytes)
2223 +  + inode generation (4 bytes))
2224 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2225 +    bytes)
2226 +  + file handle for a branch (by exportfs_encode_fh())
2227 +- fh_to_dentry():
2228 +  + find the index of a branch from its id in handle, and check it is
2229 +    still exist in aufs.
2230 +  + 1st level: get the inode number from handle and search it in cache.
2231 +  + 2nd level: if not found in cache, get the parent inode number from
2232 +    the handle and search it in cache. and then open the found parent
2233 +    dir, find the matching inode number by vfs_readdir() and get its
2234 +    name, and call lookup_one_len() for the target dentry.
2235 +  + 3rd level: if the parent dir is not cached, call
2236 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2237 +    build a pathname of it, convert it a pathname in aufs, call
2238 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2239 +    the 2nd level.
2240 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2241 +    for every branch, but not itself. to get this, (currently) aufs
2242 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2243 +    idea, but I didn't get other approach.
2244 +  + test the generation of the gotten inode.
2245 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2246 +  convert it into ESTALE for NFSD.
2247 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2248 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2249 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2250 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2251 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2016-04-24 18:32:51.390353525 +0200
2252 @@ -0,0 +1,52 @@
2253 +
2254 +# Copyright (C) 2005-2016 Junjiro R. Okajima
2255 +# 
2256 +# This program is free software; you can redistribute it and/or modify
2257 +# it under the terms of the GNU General Public License as published by
2258 +# the Free Software Foundation; either version 2 of the License, or
2259 +# (at your option) any later version.
2260 +# 
2261 +# This program is distributed in the hope that it will be useful,
2262 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2263 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2264 +# GNU General Public License for more details.
2265 +# 
2266 +# You should have received a copy of the GNU General Public License
2267 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2268 +
2269 +Show Whiteout Mode (shwh)
2270 +----------------------------------------------------------------------
2271 +Generally aufs hides the name of whiteouts. But in some cases, to show
2272 +them is very useful for users. For instance, creating a new middle layer
2273 +(branch) by merging existing layers.
2274 +
2275 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2276 +When you have three branches,
2277 +- Bottom: 'system', squashfs (underlying base system), read-only
2278 +- Middle: 'mods', squashfs, read-only
2279 +- Top: 'overlay', ram (tmpfs), read-write
2280 +
2281 +The top layer is loaded at boot time and saved at shutdown, to preserve
2282 +the changes made to the system during the session.
2283 +When larger changes have been made, or smaller changes have accumulated,
2284 +the size of the saved top layer data grows. At this point, it would be
2285 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2286 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2287 +restoring save and load speed.
2288 +
2289 +This merging is simplified by the use of another aufs mount, of just the
2290 +two overlay branches using the 'shwh' option.
2291 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2292 +       aufs /livesys/merge_union
2293 +
2294 +A merged view of these two branches is then available at
2295 +/livesys/merge_union, and the new feature is that the whiteouts are
2296 +visible!
2297 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2298 +writing to all branches. Also the default mode for all branches is 'ro'.
2299 +It is now possible to save the combined contents of the two overlay
2300 +branches to a new squashfs, e.g.:
2301 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2302 +
2303 +This new squashfs archive can be stored on the boot device and the
2304 +initramfs will use it to replace the old one at the next boot.
2305 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2306 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2307 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2016-04-24 18:32:51.390353525 +0200
2308 @@ -0,0 +1,47 @@
2309 +
2310 +# Copyright (C) 2010-2016 Junjiro R. Okajima
2311 +#
2312 +# This program is free software; you can redistribute it and/or modify
2313 +# it under the terms of the GNU General Public License as published by
2314 +# the Free Software Foundation; either version 2 of the License, or
2315 +# (at your option) any later version.
2316 +#
2317 +# This program is distributed in the hope that it will be useful,
2318 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2319 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2320 +# GNU General Public License for more details.
2321 +#
2322 +# You should have received a copy of the GNU General Public License
2323 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2324 +
2325 +Dynamically customizable FS operations
2326 +----------------------------------------------------------------------
2327 +Generally FS operations (struct inode_operations, struct
2328 +address_space_operations, struct file_operations, etc.) are defined as
2329 +"static const", but it never means that FS have only one set of
2330 +operation. Some FS have multiple sets of them. For instance, ext2 has
2331 +three sets, one for XIP, for NOBH, and for normal.
2332 +Since aufs overrides and redirects these operations, sometimes aufs has
2333 +to change its behaviour according to the branch FS type. More importantly
2334 +VFS acts differently if a function (member in the struct) is set or
2335 +not. It means aufs should have several sets of operations and select one
2336 +among them according to the branch FS definition.
2337 +
2338 +In order to solve this problem and not to affect the behaviour of VFS,
2339 +aufs defines these operations dynamically. For instance, aufs defines
2340 +dummy direct_IO function for struct address_space_operations, but it may
2341 +not be set to the address_space_operations actually. When the branch FS
2342 +doesn't have it, aufs doesn't set it to its address_space_operations
2343 +while the function definition itself is still alive. So the behaviour
2344 +itself will not change, and it will return an error when direct_IO is
2345 +not set.
2346 +
2347 +The lifetime of these dynamically generated operation object is
2348 +maintained by aufs branch object. When the branch is removed from aufs,
2349 +the reference counter of the object is decremented. When it reaches
2350 +zero, the dynamically generated operation object will be freed.
2351 +
2352 +This approach is designed to support AIO (io_submit), Direct I/O and
2353 +XIP (DAX) mainly.
2354 +Currently this approach is applied to address_space_operations for
2355 +regular files only.
2356 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2357 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2358 +++ linux/Documentation/filesystems/aufs/README 2016-04-24 18:32:51.390353525 +0200
2359 @@ -0,0 +1,391 @@
2360 +
2361 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2362 +http://aufs.sf.net
2363 +Junjiro R. Okajima
2364 +
2365 +
2366 +0. Introduction
2367 +----------------------------------------
2368 +In the early days, aufs was entirely re-designed and re-implemented
2369 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2370 +improvements and implementations, it becomes totally different from
2371 +Unionfs while keeping the basic features.
2372 +Recently, Unionfs Version 2.x series begin taking some of the same
2373 +approaches to aufs1's.
2374 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2375 +University and his team.
2376 +
2377 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2378 +If you want older kernel version support, try aufs2-2.6.git or
2379 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2380 +
2381 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2382 +      According to Christoph Hellwig, linux rejects all union-type
2383 +      filesystems but UnionMount.
2384 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2385 +
2386 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2387 +    UnionMount, and he pointed out an issue around a directory mutex
2388 +    lock and aufs addressed it. But it is still unsure whether aufs will
2389 +    be merged (or any other union solution).
2390 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2391 +
2392 +
2393 +1. Features
2394 +----------------------------------------
2395 +- unite several directories into a single virtual filesystem. The member
2396 +  directory is called as a branch.
2397 +- you can specify the permission flags to the branch, which are 'readonly',
2398 +  'readwrite' and 'whiteout-able.'
2399 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2400 +  readonly branch are modifiable logically.
2401 +- dynamic branch manipulation, add, del.
2402 +- etc...
2403 +
2404 +Also there are many enhancements in aufs, such as:
2405 +- test only the highest one for the directory permission (dirperm1)
2406 +- copyup on open (coo=)
2407 +- 'move' policy for copy-up between two writable branches, after
2408 +  checking free space.
2409 +- xattr, acl
2410 +- readdir(3) in userspace.
2411 +- keep inode number by external inode number table
2412 +- keep the timestamps of file/dir in internal copyup operation
2413 +- seekable directory, supporting NFS readdir.
2414 +- whiteout is hardlinked in order to reduce the consumption of inodes
2415 +  on branch
2416 +- do not copyup, nor create a whiteout when it is unnecessary
2417 +- revert a single systemcall when an error occurs in aufs
2418 +- remount interface instead of ioctl
2419 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2420 +- loopback mounted filesystem as a branch
2421 +- kernel thread for removing the dir who has a plenty of whiteouts
2422 +- support copyup sparse file (a file which has a 'hole' in it)
2423 +- default permission flags for branches
2424 +- selectable permission flags for ro branch, whether whiteout can
2425 +  exist or not
2426 +- export via NFS.
2427 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2428 +- support multiple writable branches, some policies to select one
2429 +  among multiple writable branches.
2430 +- a new semantics for link(2) and rename(2) to support multiple
2431 +  writable branches.
2432 +- no glibc changes are required.
2433 +- pseudo hardlink (hardlink over branches)
2434 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2435 +  including NFS or remote filesystem branch.
2436 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2437 +- and more...
2438 +
2439 +Currently these features are dropped temporary from aufs4.
2440 +See design/08plan.txt in detail.
2441 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2442 +  (robr)
2443 +- statistics of aufs thread (/sys/fs/aufs/stat)
2444 +
2445 +Features or just an idea in the future (see also design/*.txt),
2446 +- reorder the branch index without del/re-add.
2447 +- permanent xino files for NFSD
2448 +- an option for refreshing the opened files after add/del branches
2449 +- light version, without branch manipulation. (unnecessary?)
2450 +- copyup in userspace
2451 +- inotify in userspace
2452 +- readv/writev
2453 +
2454 +
2455 +2. Download
2456 +----------------------------------------
2457 +There are three GIT trees for aufs4, aufs4-linux.git,
2458 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2459 +"aufs-util.git."
2460 +While the aufs-util is always necessary, you need either of aufs4-linux
2461 +or aufs4-standalone.
2462 +
2463 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2464 +git://git.kernel.org/.../torvalds/linux.git.
2465 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2466 +build aufs4 as an external kernel module.
2467 +Several extra patches are not included in this tree. Only
2468 +aufs4-standalone tree contains them. They are described in the later
2469 +section "Configuration and Compilation."
2470 +
2471 +On the other hand, the aufs4-standalone tree has only aufs source files
2472 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2473 +But you need to apply all aufs patches manually.
2474 +
2475 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2476 +represents the linux kernel version, "linux-4.x". For instance,
2477 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2478 +"aufs4.x-rcN" branch.
2479 +
2480 +o aufs4-linux tree
2481 +$ git clone --reference /your/linux/git/tree \
2482 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2483 +- if you don't have linux GIT tree, then remove "--reference ..."
2484 +$ cd aufs4-linux.git
2485 +$ git checkout origin/aufs4.0
2486 +
2487 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2488 +leave the patch-work to GIT.
2489 +$ cd /your/linux/git/tree
2490 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2491 +$ git fetch aufs4
2492 +$ git checkout -b my4.0 v4.0
2493 +$ (add your local change...)
2494 +$ git pull aufs4 aufs4.0
2495 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2496 +- you may need to solve some conflicts between your_changes and
2497 +  aufs4.0. in this case, git-rerere is recommended so that you can
2498 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2499 +  later in the future.
2500 +
2501 +o aufs4-standalone tree
2502 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2503 +$ cd aufs4-standalone.git
2504 +$ git checkout origin/aufs4.0
2505 +
2506 +o aufs-util tree
2507 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2508 +- note that the public aufs-util.git is on SourceForge instead of
2509 +  GitHUB.
2510 +$ cd aufs-util.git
2511 +$ git checkout origin/aufs4.0
2512 +
2513 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2514 +The minor version number, 'x' in '4.x', of aufs may not always
2515 +follow the minor version number of the kernel.
2516 +Because changes in the kernel that cause the use of a new
2517 +minor version number do not always require changes to aufs-util.
2518 +
2519 +Since aufs-util has its own minor version number, you may not be
2520 +able to find a GIT branch in aufs-util for your kernel's
2521 +exact minor version number.
2522 +In this case, you should git-checkout the branch for the
2523 +nearest lower number.
2524 +
2525 +For (an unreleased) example:
2526 +If you are using "linux-4.10" and the "aufs4.10" branch
2527 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2528 +or something numerically smaller is the branch for your kernel.
2529 +
2530 +Also you can view all branches by
2531 +       $ git branch -a
2532 +
2533 +
2534 +3. Configuration and Compilation
2535 +----------------------------------------
2536 +Make sure you have git-checkout'ed the correct branch.
2537 +
2538 +For aufs4-linux tree,
2539 +- enable CONFIG_AUFS_FS.
2540 +- set other aufs configurations if necessary.
2541 +
2542 +For aufs4-standalone tree,
2543 +There are several ways to build.
2544 +
2545 +1.
2546 +- apply ./aufs4-kbuild.patch to your kernel source files.
2547 +- apply ./aufs4-base.patch too.
2548 +- apply ./aufs4-mmap.patch too.
2549 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2550 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2551 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2552 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2553 +- enable CONFIG_AUFS_FS, you can select either
2554 +  =m or =y.
2555 +- and build your kernel as usual.
2556 +- install the built kernel.
2557 +  Note: Since linux-3.9, every filesystem module requires an alias
2558 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2559 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2560 +- install the header files too by "make headers_install" to the
2561 +  directory where you specify. By default, it is $PWD/usr.
2562 +  "make help" shows a brief note for headers_install.
2563 +- and reboot your system.
2564 +
2565 +2.
2566 +- module only (CONFIG_AUFS_FS=m).
2567 +- apply ./aufs4-base.patch to your kernel source files.
2568 +- apply ./aufs4-mmap.patch too.
2569 +- apply ./aufs4-standalone.patch too.
2570 +- build your kernel, don't forget "make headers_install", and reboot.
2571 +- edit ./config.mk and set other aufs configurations if necessary.
2572 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2573 +  every aufs configurations.
2574 +- build the module by simple "make".
2575 +  Note: Since linux-3.9, every filesystem module requires an alias
2576 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2577 +  modules.aliases file.
2578 +- you can specify ${KDIR} make variable which points to your kernel
2579 +  source tree.
2580 +- install the files
2581 +  + run "make install" to install the aufs module, or copy the built
2582 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2583 +  + run "make install_headers" (instead of headers_install) to install
2584 +    the modified aufs header file (you can specify DESTDIR which is
2585 +    available in aufs standalone version's Makefile only), or copy
2586 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2587 +    you like manually. By default, the target directory is $PWD/usr.
2588 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2589 +  kernel source tree.
2590 +
2591 +Note: The header file aufs_type.h is necessary to build aufs-util
2592 +      as well as "make headers_install" in the kernel source tree.
2593 +      headers_install is subject to be forgotten, but it is essentially
2594 +      necessary, not only for building aufs-util.
2595 +      You may not meet problems without headers_install in some older
2596 +      version though.
2597 +
2598 +And then,
2599 +- read README in aufs-util, build and install it
2600 +- note that your distribution may contain an obsoleted version of
2601 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2602 +  utilities, make sure that your compiler refers the correct aufs header
2603 +  file which is built by "make headers_install."
2604 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2605 +  then run "make install_ulib" too. And refer to the aufs manual in
2606 +  detail.
2607 +
2608 +There several other patches in aufs4-standalone.git. They are all
2609 +optional. When you meet some problems, they will help you.
2610 +- aufs4-loopback.patch
2611 +  Supports a nested loopback mount in a branch-fs. This patch is
2612 +  unnecessary until aufs produces a message like "you may want to try
2613 +  another patch for loopback file".
2614 +- vfs-ino.patch
2615 +  Modifies a system global kernel internal function get_next_ino() in
2616 +  order to stop assigning 0 for an inode-number. Not directly related to
2617 +  aufs, but recommended generally.
2618 +- tmpfs-idr.patch
2619 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2620 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2621 +  duplication of inode number, which is important for backup tools and
2622 +  other utilities. When you find aufs XINO files for tmpfs branch
2623 +  growing too much, try this patch.
2624 +- lockdep-debug.patch
2625 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2626 +  also a caller of VFS functions for branch filesystems, subclassing of
2627 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2628 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2629 +  need to apply this debug patch to expand several constant values.
2630 +  If don't know what LOCKDEP, then you don't have apply this patch.
2631 +
2632 +
2633 +4. Usage
2634 +----------------------------------------
2635 +At first, make sure aufs-util are installed, and please read the aufs
2636 +manual, aufs.5 in aufs-util.git tree.
2637 +$ man -l aufs.5
2638 +
2639 +And then,
2640 +$ mkdir /tmp/rw /tmp/aufs
2641 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2642 +
2643 +Here is another example. The result is equivalent.
2644 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2645 +  Or
2646 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2647 +# mount -o remount,append:${HOME} /tmp/aufs
2648 +
2649 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2650 +you modify a file under /tmp/aufs, the one on your home directory is
2651 +not affected, instead the same named file will be newly created under
2652 +/tmp/rw. And all of your modification to a file will be applied to
2653 +the one under /tmp/rw. This is called the file based Copy on Write
2654 +(COW) method.
2655 +Aufs mount options are described in aufs.5.
2656 +If you run chroot or something and make your aufs as a root directory,
2657 +then you need to customize the shutdown script. See the aufs manual in
2658 +detail.
2659 +
2660 +Additionally, there are some sample usages of aufs which are a
2661 +diskless system with network booting, and LiveCD over NFS.
2662 +See sample dir in CVS tree on SourceForge.
2663 +
2664 +
2665 +5. Contact
2666 +----------------------------------------
2667 +When you have any problems or strange behaviour in aufs, please let me
2668 +know with:
2669 +- /proc/mounts (instead of the output of mount(8))
2670 +- /sys/module/aufs/*
2671 +- /sys/fs/aufs/* (if you have them)
2672 +- /debug/aufs/* (if you have them)
2673 +- linux kernel version
2674 +  if your kernel is not plain, for example modified by distributor,
2675 +  the url where i can download its source is necessary too.
2676 +- aufs version which was printed at loading the module or booting the
2677 +  system, instead of the date you downloaded.
2678 +- configuration (define/undefine CONFIG_AUFS_xxx)
2679 +- kernel configuration or /proc/config.gz (if you have it)
2680 +- behaviour which you think to be incorrect
2681 +- actual operation, reproducible one is better
2682 +- mailto: aufs-users at lists.sourceforge.net
2683 +
2684 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2685 +and Feature Requests) on SourceForge. Please join and write to
2686 +aufs-users ML.
2687 +
2688 +
2689 +6. Acknowledgements
2690 +----------------------------------------
2691 +Thanks to everyone who have tried and are using aufs, whoever
2692 +have reported a bug or any feedback.
2693 +
2694 +Especially donators:
2695 +Tomas Matejicek(slax.org) made a donation (much more than once).
2696 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2697 +       scripts) is making "doubling" donations.
2698 +       Unfortunately I cannot list all of the donators, but I really
2699 +       appreciate.
2700 +       It ends Aug 2010, but the ordinary donation URL is still available.
2701 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2702 +Dai Itasaka made a donation (2007/8).
2703 +Chuck Smith made a donation (2008/4, 10 and 12).
2704 +Henk Schoneveld made a donation (2008/9).
2705 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2706 +Francois Dupoux made a donation (2008/11).
2707 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2708 +       aufs2 GIT tree (2009/2).
2709 +William Grant made a donation (2009/3).
2710 +Patrick Lane made a donation (2009/4).
2711 +The Mail Archive (mail-archive.com) made donations (2009/5).
2712 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2713 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2714 +Pavel Pronskiy made a donation (2011/2).
2715 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2716 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2717 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2718 +11).
2719 +Sam Liddicott made a donation (2011/9).
2720 +Era Scarecrow made a donation (2013/4).
2721 +Bor Ratajc made a donation (2013/4).
2722 +Alessandro Gorreta made a donation (2013/4).
2723 +POIRETTE Marc made a donation (2013/4).
2724 +Alessandro Gorreta made a donation (2013/4).
2725 +lauri kasvandik made a donation (2013/5).
2726 +"pemasu from Finland" made a donation (2013/7).
2727 +The Parted Magic Project made a donation (2013/9 and 11).
2728 +Pavel Barta made a donation (2013/10).
2729 +Nikolay Pertsev made a donation (2014/5).
2730 +James B made a donation (2014/7 and 2015/7).
2731 +Stefano Di Biase made a donation (2014/8).
2732 +Daniel Epellei made a donation (2015/1).
2733 +OmegaPhil made a donation (2016/1).
2734 +
2735 +Thank you very much.
2736 +Donations are always, including future donations, very important and
2737 +helpful for me to keep on developing aufs.
2738 +
2739 +
2740 +7.
2741 +----------------------------------------
2742 +If you are an experienced user, no explanation is needed. Aufs is
2743 +just a linux filesystem.
2744 +
2745 +
2746 +Enjoy!
2747 +
2748 +# Local variables: ;
2749 +# mode: text;
2750 +# End: ;
2751 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2752 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2753 +++ linux/fs/aufs/aufs.h        2016-04-24 18:32:51.390353525 +0200
2754 @@ -0,0 +1,59 @@
2755 +/*
2756 + * Copyright (C) 2005-2016 Junjiro R. Okajima
2757 + *
2758 + * This program, aufs is free software; you can redistribute it and/or modify
2759 + * it under the terms of the GNU General Public License as published by
2760 + * the Free Software Foundation; either version 2 of the License, or
2761 + * (at your option) any later version.
2762 + *
2763 + * This program is distributed in the hope that it will be useful,
2764 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2765 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2766 + * GNU General Public License for more details.
2767 + *
2768 + * You should have received a copy of the GNU General Public License
2769 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2770 + */
2771 +
2772 +/*
2773 + * all header files
2774 + */
2775 +
2776 +#ifndef __AUFS_H__
2777 +#define __AUFS_H__
2778 +
2779 +#ifdef __KERNEL__
2780 +
2781 +#define AuStub(type, name, body, ...) \
2782 +       static inline type name(__VA_ARGS__) { body; }
2783 +
2784 +#define AuStubVoid(name, ...) \
2785 +       AuStub(void, name, , __VA_ARGS__)
2786 +#define AuStubInt0(name, ...) \
2787 +       AuStub(int, name, return 0, __VA_ARGS__)
2788 +
2789 +#include "debug.h"
2790 +
2791 +#include "branch.h"
2792 +#include "cpup.h"
2793 +#include "dcsub.h"
2794 +#include "dbgaufs.h"
2795 +#include "dentry.h"
2796 +#include "dir.h"
2797 +#include "dynop.h"
2798 +#include "file.h"
2799 +#include "fstype.h"
2800 +#include "inode.h"
2801 +#include "loop.h"
2802 +#include "module.h"
2803 +#include "opts.h"
2804 +#include "rwsem.h"
2805 +#include "spl.h"
2806 +#include "super.h"
2807 +#include "sysaufs.h"
2808 +#include "vfsub.h"
2809 +#include "whout.h"
2810 +#include "wkq.h"
2811 +
2812 +#endif /* __KERNEL__ */
2813 +#endif /* __AUFS_H__ */
2814 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2815 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2816 +++ linux/fs/aufs/branch.c      2016-04-24 18:32:51.390353525 +0200
2817 @@ -0,0 +1,1407 @@
2818 +/*
2819 + * Copyright (C) 2005-2016 Junjiro R. Okajima
2820 + *
2821 + * This program, aufs is free software; you can redistribute it and/or modify
2822 + * it under the terms of the GNU General Public License as published by
2823 + * the Free Software Foundation; either version 2 of the License, or
2824 + * (at your option) any later version.
2825 + *
2826 + * This program is distributed in the hope that it will be useful,
2827 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2828 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2829 + * GNU General Public License for more details.
2830 + *
2831 + * You should have received a copy of the GNU General Public License
2832 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2833 + */
2834 +
2835 +/*
2836 + * branch management
2837 + */
2838 +
2839 +#include <linux/compat.h>
2840 +#include <linux/statfs.h>
2841 +#include "aufs.h"
2842 +
2843 +/*
2844 + * free a single branch
2845 + */
2846 +static void au_br_do_free(struct au_branch *br)
2847 +{
2848 +       int i;
2849 +       struct au_wbr *wbr;
2850 +       struct au_dykey **key;
2851 +
2852 +       au_hnotify_fin_br(br);
2853 +
2854 +       if (br->br_xino.xi_file)
2855 +               fput(br->br_xino.xi_file);
2856 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2857 +
2858 +       AuDebugOn(atomic_read(&br->br_count));
2859 +
2860 +       wbr = br->br_wbr;
2861 +       if (wbr) {
2862 +               for (i = 0; i < AuBrWh_Last; i++)
2863 +                       dput(wbr->wbr_wh[i]);
2864 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2865 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2866 +       }
2867 +
2868 +       if (br->br_fhsm) {
2869 +               au_br_fhsm_fin(br->br_fhsm);
2870 +               kfree(br->br_fhsm);
2871 +       }
2872 +
2873 +       key = br->br_dykey;
2874 +       for (i = 0; i < AuBrDynOp; i++, key++)
2875 +               if (*key)
2876 +                       au_dy_put(*key);
2877 +               else
2878 +                       break;
2879 +
2880 +       /* recursive lock, s_umount of branch's */
2881 +       lockdep_off();
2882 +       path_put(&br->br_path);
2883 +       lockdep_on();
2884 +       kfree(wbr);
2885 +       kfree(br);
2886 +}
2887 +
2888 +/*
2889 + * frees all branches
2890 + */
2891 +void au_br_free(struct au_sbinfo *sbinfo)
2892 +{
2893 +       aufs_bindex_t bmax;
2894 +       struct au_branch **br;
2895 +
2896 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2897 +
2898 +       bmax = sbinfo->si_bend + 1;
2899 +       br = sbinfo->si_branch;
2900 +       while (bmax--)
2901 +               au_br_do_free(*br++);
2902 +}
2903 +
2904 +/*
2905 + * find the index of a branch which is specified by @br_id.
2906 + */
2907 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2908 +{
2909 +       aufs_bindex_t bindex, bend;
2910 +
2911 +       bend = au_sbend(sb);
2912 +       for (bindex = 0; bindex <= bend; bindex++)
2913 +               if (au_sbr_id(sb, bindex) == br_id)
2914 +                       return bindex;
2915 +       return -1;
2916 +}
2917 +
2918 +/* ---------------------------------------------------------------------- */
2919 +
2920 +/*
2921 + * add a branch
2922 + */
2923 +
2924 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2925 +                       struct dentry *h_root)
2926 +{
2927 +       if (unlikely(h_adding == h_root
2928 +                    || au_test_loopback_overlap(sb, h_adding)))
2929 +               return 1;
2930 +       if (h_adding->d_sb != h_root->d_sb)
2931 +               return 0;
2932 +       return au_test_subdir(h_adding, h_root)
2933 +               || au_test_subdir(h_root, h_adding);
2934 +}
2935 +
2936 +/*
2937 + * returns a newly allocated branch. @new_nbranch is a number of branches
2938 + * after adding a branch.
2939 + */
2940 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2941 +                                    int perm)
2942 +{
2943 +       struct au_branch *add_branch;
2944 +       struct dentry *root;
2945 +       struct inode *inode;
2946 +       int err;
2947 +
2948 +       err = -ENOMEM;
2949 +       root = sb->s_root;
2950 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
2951 +       if (unlikely(!add_branch))
2952 +               goto out;
2953 +
2954 +       err = au_hnotify_init_br(add_branch, perm);
2955 +       if (unlikely(err))
2956 +               goto out_br;
2957 +
2958 +       if (au_br_writable(perm)) {
2959 +               /* may be freed separately at changing the branch permission */
2960 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
2961 +                                            GFP_NOFS);
2962 +               if (unlikely(!add_branch->br_wbr))
2963 +                       goto out_hnotify;
2964 +       }
2965 +
2966 +       if (au_br_fhsm(perm)) {
2967 +               err = au_fhsm_br_alloc(add_branch);
2968 +               if (unlikely(err))
2969 +                       goto out_wbr;
2970 +       }
2971 +
2972 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2973 +       if (!err)
2974 +               err = au_di_realloc(au_di(root), new_nbranch);
2975 +       if (!err) {
2976 +               inode = d_inode(root);
2977 +               err = au_ii_realloc(au_ii(inode), new_nbranch);
2978 +       }
2979 +       if (!err)
2980 +               return add_branch; /* success */
2981 +
2982 +out_wbr:
2983 +       kfree(add_branch->br_wbr);
2984 +out_hnotify:
2985 +       au_hnotify_fin_br(add_branch);
2986 +out_br:
2987 +       kfree(add_branch);
2988 +out:
2989 +       return ERR_PTR(err);
2990 +}
2991 +
2992 +/*
2993 + * test if the branch permission is legal or not.
2994 + */
2995 +static int test_br(struct inode *inode, int brperm, char *path)
2996 +{
2997 +       int err;
2998 +
2999 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3000 +       if (!err)
3001 +               goto out;
3002 +
3003 +       err = -EINVAL;
3004 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3005 +
3006 +out:
3007 +       return err;
3008 +}
3009 +
3010 +/*
3011 + * returns:
3012 + * 0: success, the caller will add it
3013 + * plus: success, it is already unified, the caller should ignore it
3014 + * minus: error
3015 + */
3016 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3017 +{
3018 +       int err;
3019 +       aufs_bindex_t bend, bindex;
3020 +       struct dentry *root, *h_dentry;
3021 +       struct inode *inode, *h_inode;
3022 +
3023 +       root = sb->s_root;
3024 +       bend = au_sbend(sb);
3025 +       if (unlikely(bend >= 0
3026 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3027 +               err = 1;
3028 +               if (!remount) {
3029 +                       err = -EINVAL;
3030 +                       pr_err("%s duplicated\n", add->pathname);
3031 +               }
3032 +               goto out;
3033 +       }
3034 +
3035 +       err = -ENOSPC; /* -E2BIG; */
3036 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3037 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
3038 +               pr_err("number of branches exceeded %s\n", add->pathname);
3039 +               goto out;
3040 +       }
3041 +
3042 +       err = -EDOM;
3043 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
3044 +               pr_err("bad index %d\n", add->bindex);
3045 +               goto out;
3046 +       }
3047 +
3048 +       inode = d_inode(add->path.dentry);
3049 +       err = -ENOENT;
3050 +       if (unlikely(!inode->i_nlink)) {
3051 +               pr_err("no existence %s\n", add->pathname);
3052 +               goto out;
3053 +       }
3054 +
3055 +       err = -EINVAL;
3056 +       if (unlikely(inode->i_sb == sb)) {
3057 +               pr_err("%s must be outside\n", add->pathname);
3058 +               goto out;
3059 +       }
3060 +
3061 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3062 +               pr_err("unsupported filesystem, %s (%s)\n",
3063 +                      add->pathname, au_sbtype(inode->i_sb));
3064 +               goto out;
3065 +       }
3066 +
3067 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3068 +               pr_err("already stacked, %s (%s)\n",
3069 +                      add->pathname, au_sbtype(inode->i_sb));
3070 +               goto out;
3071 +       }
3072 +
3073 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3074 +       if (unlikely(err))
3075 +               goto out;
3076 +
3077 +       if (bend < 0)
3078 +               return 0; /* success */
3079 +
3080 +       err = -EINVAL;
3081 +       for (bindex = 0; bindex <= bend; bindex++)
3082 +               if (unlikely(test_overlap(sb, add->path.dentry,
3083 +                                         au_h_dptr(root, bindex)))) {
3084 +                       pr_err("%s is overlapped\n", add->pathname);
3085 +                       goto out;
3086 +               }
3087 +
3088 +       err = 0;
3089 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3090 +               h_dentry = au_h_dptr(root, 0);
3091 +               h_inode = d_inode(h_dentry);
3092 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3093 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3094 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3095 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3096 +                               add->pathname,
3097 +                               i_uid_read(inode), i_gid_read(inode),
3098 +                               (inode->i_mode & S_IALLUGO),
3099 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3100 +                               (h_inode->i_mode & S_IALLUGO));
3101 +       }
3102 +
3103 +out:
3104 +       return err;
3105 +}
3106 +
3107 +/*
3108 + * initialize or clean the whiteouts for an adding branch
3109 + */
3110 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3111 +                        int new_perm)
3112 +{
3113 +       int err, old_perm;
3114 +       aufs_bindex_t bindex;
3115 +       struct inode *h_inode;
3116 +       struct au_wbr *wbr;
3117 +       struct au_hinode *hdir;
3118 +       struct dentry *h_dentry;
3119 +
3120 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3121 +       if (unlikely(err))
3122 +               goto out;
3123 +
3124 +       wbr = br->br_wbr;
3125 +       old_perm = br->br_perm;
3126 +       br->br_perm = new_perm;
3127 +       hdir = NULL;
3128 +       h_inode = NULL;
3129 +       bindex = au_br_index(sb, br->br_id);
3130 +       if (0 <= bindex) {
3131 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3132 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
3133 +       } else {
3134 +               h_dentry = au_br_dentry(br);
3135 +               h_inode = d_inode(h_dentry);
3136 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3137 +       }
3138 +       if (!wbr)
3139 +               err = au_wh_init(br, sb);
3140 +       else {
3141 +               wbr_wh_write_lock(wbr);
3142 +               err = au_wh_init(br, sb);
3143 +               wbr_wh_write_unlock(wbr);
3144 +       }
3145 +       if (hdir)
3146 +               au_hn_imtx_unlock(hdir);
3147 +       else
3148 +               inode_unlock(h_inode);
3149 +       vfsub_mnt_drop_write(au_br_mnt(br));
3150 +       br->br_perm = old_perm;
3151 +
3152 +       if (!err && wbr && !au_br_writable(new_perm)) {
3153 +               kfree(wbr);
3154 +               br->br_wbr = NULL;
3155 +       }
3156 +
3157 +out:
3158 +       return err;
3159 +}
3160 +
3161 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3162 +                      int perm)
3163 +{
3164 +       int err;
3165 +       struct kstatfs kst;
3166 +       struct au_wbr *wbr;
3167 +
3168 +       wbr = br->br_wbr;
3169 +       au_rw_init(&wbr->wbr_wh_rwsem);
3170 +       atomic_set(&wbr->wbr_wh_running, 0);
3171 +
3172 +       /*
3173 +        * a limit for rmdir/rename a dir
3174 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3175 +        */
3176 +       err = vfs_statfs(&br->br_path, &kst);
3177 +       if (unlikely(err))
3178 +               goto out;
3179 +       err = -EINVAL;
3180 +       if (kst.f_namelen >= NAME_MAX)
3181 +               err = au_br_init_wh(sb, br, perm);
3182 +       else
3183 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3184 +                      au_br_dentry(br),
3185 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3186 +
3187 +out:
3188 +       return err;
3189 +}
3190 +
3191 +/* initialize a new branch */
3192 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3193 +                     struct au_opt_add *add)
3194 +{
3195 +       int err;
3196 +       struct inode *h_inode;
3197 +
3198 +       err = 0;
3199 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3200 +       br->br_perm = add->perm;
3201 +       br->br_path = add->path; /* set first, path_get() later */
3202 +       spin_lock_init(&br->br_dykey_lock);
3203 +       atomic_set(&br->br_count, 0);
3204 +       atomic_set(&br->br_xino_running, 0);
3205 +       br->br_id = au_new_br_id(sb);
3206 +       AuDebugOn(br->br_id < 0);
3207 +
3208 +       if (au_br_writable(add->perm)) {
3209 +               err = au_wbr_init(br, sb, add->perm);
3210 +               if (unlikely(err))
3211 +                       goto out_err;
3212 +       }
3213 +
3214 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3215 +               h_inode = d_inode(add->path.dentry);
3216 +               err = au_xino_br(sb, br, h_inode->i_ino,
3217 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3218 +               if (unlikely(err)) {
3219 +                       AuDebugOn(br->br_xino.xi_file);
3220 +                       goto out_err;
3221 +               }
3222 +       }
3223 +
3224 +       sysaufs_br_init(br);
3225 +       path_get(&br->br_path);
3226 +       goto out; /* success */
3227 +
3228 +out_err:
3229 +       memset(&br->br_path, 0, sizeof(br->br_path));
3230 +out:
3231 +       return err;
3232 +}
3233 +
3234 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3235 +                            struct au_branch *br, aufs_bindex_t bend,
3236 +                            aufs_bindex_t amount)
3237 +{
3238 +       struct au_branch **brp;
3239 +
3240 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3241 +
3242 +       brp = sbinfo->si_branch + bindex;
3243 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3244 +       *brp = br;
3245 +       sbinfo->si_bend++;
3246 +       if (unlikely(bend < 0))
3247 +               sbinfo->si_bend = 0;
3248 +}
3249 +
3250 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3251 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3252 +{
3253 +       struct au_hdentry *hdp;
3254 +
3255 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3256 +
3257 +       hdp = dinfo->di_hdentry + bindex;
3258 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3259 +       au_h_dentry_init(hdp);
3260 +       dinfo->di_bend++;
3261 +       if (unlikely(bend < 0))
3262 +               dinfo->di_bstart = 0;
3263 +}
3264 +
3265 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3266 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3267 +{
3268 +       struct au_hinode *hip;
3269 +
3270 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3271 +
3272 +       hip = iinfo->ii_hinode + bindex;
3273 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3274 +       hip->hi_inode = NULL;
3275 +       au_hn_init(hip);
3276 +       iinfo->ii_bend++;
3277 +       if (unlikely(bend < 0))
3278 +               iinfo->ii_bstart = 0;
3279 +}
3280 +
3281 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3282 +                        aufs_bindex_t bindex)
3283 +{
3284 +       struct dentry *root, *h_dentry;
3285 +       struct inode *root_inode, *h_inode;
3286 +       aufs_bindex_t bend, amount;
3287 +
3288 +       root = sb->s_root;
3289 +       root_inode = d_inode(root);
3290 +       bend = au_sbend(sb);
3291 +       amount = bend + 1 - bindex;
3292 +       h_dentry = au_br_dentry(br);
3293 +       au_sbilist_lock();
3294 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3295 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3296 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3297 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3298 +       h_inode = d_inode(h_dentry);
3299 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3300 +       au_sbilist_unlock();
3301 +}
3302 +
3303 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3304 +{
3305 +       int err;
3306 +       aufs_bindex_t bend, add_bindex;
3307 +       struct dentry *root, *h_dentry;
3308 +       struct inode *root_inode;
3309 +       struct au_branch *add_branch;
3310 +
3311 +       root = sb->s_root;
3312 +       root_inode = d_inode(root);
3313 +       IMustLock(root_inode);
3314 +       err = test_add(sb, add, remount);
3315 +       if (unlikely(err < 0))
3316 +               goto out;
3317 +       if (err) {
3318 +               err = 0;
3319 +               goto out; /* success */
3320 +       }
3321 +
3322 +       bend = au_sbend(sb);
3323 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3324 +       err = PTR_ERR(add_branch);
3325 +       if (IS_ERR(add_branch))
3326 +               goto out;
3327 +
3328 +       err = au_br_init(add_branch, sb, add);
3329 +       if (unlikely(err)) {
3330 +               au_br_do_free(add_branch);
3331 +               goto out;
3332 +       }
3333 +
3334 +       add_bindex = add->bindex;
3335 +       if (!remount)
3336 +               au_br_do_add(sb, add_branch, add_bindex);
3337 +       else {
3338 +               sysaufs_brs_del(sb, add_bindex);
3339 +               au_br_do_add(sb, add_branch, add_bindex);
3340 +               sysaufs_brs_add(sb, add_bindex);
3341 +       }
3342 +
3343 +       h_dentry = add->path.dentry;
3344 +       if (!add_bindex) {
3345 +               au_cpup_attr_all(root_inode, /*force*/1);
3346 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3347 +       } else
3348 +               au_add_nlink(root_inode, d_inode(h_dentry));
3349 +
3350 +       /*
3351 +        * this test/set prevents aufs from handling unnecesary notify events
3352 +        * of xino files, in case of re-adding a writable branch which was
3353 +        * once detached from aufs.
3354 +        */
3355 +       if (au_xino_brid(sb) < 0
3356 +           && au_br_writable(add_branch->br_perm)
3357 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3358 +           && add_branch->br_xino.xi_file
3359 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3360 +               au_xino_brid_set(sb, add_branch->br_id);
3361 +
3362 +out:
3363 +       return err;
3364 +}
3365 +
3366 +/* ---------------------------------------------------------------------- */
3367 +
3368 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3369 +                                      unsigned long long max __maybe_unused,
3370 +                                      void *arg)
3371 +{
3372 +       unsigned long long n;
3373 +       struct file **p, *f;
3374 +       struct au_sphlhead *files;
3375 +       struct au_finfo *finfo;
3376 +
3377 +       n = 0;
3378 +       p = a;
3379 +       files = &au_sbi(sb)->si_files;
3380 +       spin_lock(&files->spin);
3381 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3382 +               f = finfo->fi_file;
3383 +               if (file_count(f)
3384 +                   && !special_file(file_inode(f)->i_mode)) {
3385 +                       get_file(f);
3386 +                       *p++ = f;
3387 +                       n++;
3388 +                       AuDebugOn(n > max);
3389 +               }
3390 +       }
3391 +       spin_unlock(&files->spin);
3392 +
3393 +       return n;
3394 +}
3395 +
3396 +static struct file **au_farray_alloc(struct super_block *sb,
3397 +                                    unsigned long long *max)
3398 +{
3399 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
3400 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3401 +}
3402 +
3403 +static void au_farray_free(struct file **a, unsigned long long max)
3404 +{
3405 +       unsigned long long ull;
3406 +
3407 +       for (ull = 0; ull < max; ull++)
3408 +               if (a[ull])
3409 +                       fput(a[ull]);
3410 +       kvfree(a);
3411 +}
3412 +
3413 +/* ---------------------------------------------------------------------- */
3414 +
3415 +/*
3416 + * delete a branch
3417 + */
3418 +
3419 +/* to show the line number, do not make it inlined function */
3420 +#define AuVerbose(do_info, fmt, ...) do { \
3421 +       if (do_info) \
3422 +               pr_info(fmt, ##__VA_ARGS__); \
3423 +} while (0)
3424 +
3425 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
3426 +                        aufs_bindex_t bend)
3427 +{
3428 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
3429 +}
3430 +
3431 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
3432 +                        aufs_bindex_t bend)
3433 +{
3434 +       return au_test_ibusy(d_inode(dentry), bstart, bend);
3435 +}
3436 +
3437 +/*
3438 + * test if the branch is deletable or not.
3439 + */
3440 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3441 +                           unsigned int sigen, const unsigned int verbose)
3442 +{
3443 +       int err, i, j, ndentry;
3444 +       aufs_bindex_t bstart, bend;
3445 +       struct au_dcsub_pages dpages;
3446 +       struct au_dpage *dpage;
3447 +       struct dentry *d;
3448 +
3449 +       err = au_dpages_init(&dpages, GFP_NOFS);
3450 +       if (unlikely(err))
3451 +               goto out;
3452 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3453 +       if (unlikely(err))
3454 +               goto out_dpages;
3455 +
3456 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3457 +               dpage = dpages.dpages + i;
3458 +               ndentry = dpage->ndentry;
3459 +               for (j = 0; !err && j < ndentry; j++) {
3460 +                       d = dpage->dentries[j];
3461 +                       AuDebugOn(au_dcount(d) <= 0);
3462 +                       if (!au_digen_test(d, sigen)) {
3463 +                               di_read_lock_child(d, AuLock_IR);
3464 +                               if (unlikely(au_dbrange_test(d))) {
3465 +                                       di_read_unlock(d, AuLock_IR);
3466 +                                       continue;
3467 +                               }
3468 +                       } else {
3469 +                               di_write_lock_child(d);
3470 +                               if (unlikely(au_dbrange_test(d))) {
3471 +                                       di_write_unlock(d);
3472 +                                       continue;
3473 +                               }
3474 +                               err = au_reval_dpath(d, sigen);
3475 +                               if (!err)
3476 +                                       di_downgrade_lock(d, AuLock_IR);
3477 +                               else {
3478 +                                       di_write_unlock(d);
3479 +                                       break;
3480 +                               }
3481 +                       }
3482 +
3483 +                       /* AuDbgDentry(d); */
3484 +                       bstart = au_dbstart(d);
3485 +                       bend = au_dbend(d);
3486 +                       if (bstart <= bindex
3487 +                           && bindex <= bend
3488 +                           && au_h_dptr(d, bindex)
3489 +                           && au_test_dbusy(d, bstart, bend)) {
3490 +                               err = -EBUSY;
3491 +                               AuVerbose(verbose, "busy %pd\n", d);
3492 +                               AuDbgDentry(d);
3493 +                       }
3494 +                       di_read_unlock(d, AuLock_IR);
3495 +               }
3496 +       }
3497 +
3498 +out_dpages:
3499 +       au_dpages_free(&dpages);
3500 +out:
3501 +       return err;
3502 +}
3503 +
3504 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3505 +                          unsigned int sigen, const unsigned int verbose)
3506 +{
3507 +       int err;
3508 +       unsigned long long max, ull;
3509 +       struct inode *i, **array;
3510 +       aufs_bindex_t bstart, bend;
3511 +
3512 +       array = au_iarray_alloc(sb, &max);
3513 +       err = PTR_ERR(array);
3514 +       if (IS_ERR(array))
3515 +               goto out;
3516 +
3517 +       err = 0;
3518 +       AuDbg("b%d\n", bindex);
3519 +       for (ull = 0; !err && ull < max; ull++) {
3520 +               i = array[ull];
3521 +               if (unlikely(!i))
3522 +                       break;
3523 +               if (i->i_ino == AUFS_ROOT_INO)
3524 +                       continue;
3525 +
3526 +               /* AuDbgInode(i); */
3527 +               if (au_iigen(i, NULL) == sigen)
3528 +                       ii_read_lock_child(i);
3529 +               else {
3530 +                       ii_write_lock_child(i);
3531 +                       err = au_refresh_hinode_self(i);
3532 +                       au_iigen_dec(i);
3533 +                       if (!err)
3534 +                               ii_downgrade_lock(i);
3535 +                       else {
3536 +                               ii_write_unlock(i);
3537 +                               break;
3538 +                       }
3539 +               }
3540 +
3541 +               bstart = au_ibstart(i);
3542 +               bend = au_ibend(i);
3543 +               if (bstart <= bindex
3544 +                   && bindex <= bend
3545 +                   && au_h_iptr(i, bindex)
3546 +                   && au_test_ibusy(i, bstart, bend)) {
3547 +                       err = -EBUSY;
3548 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3549 +                       AuDbgInode(i);
3550 +               }
3551 +               ii_read_unlock(i);
3552 +       }
3553 +       au_iarray_free(array, max);
3554 +
3555 +out:
3556 +       return err;
3557 +}
3558 +
3559 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3560 +                             const unsigned int verbose)
3561 +{
3562 +       int err;
3563 +       unsigned int sigen;
3564 +
3565 +       sigen = au_sigen(root->d_sb);
3566 +       DiMustNoWaiters(root);
3567 +       IiMustNoWaiters(d_inode(root));
3568 +       di_write_unlock(root);
3569 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3570 +       if (!err)
3571 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3572 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3573 +
3574 +       return err;
3575 +}
3576 +
3577 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3578 +                        struct file **to_free, int *idx)
3579 +{
3580 +       int err;
3581 +       unsigned char matched, root;
3582 +       aufs_bindex_t bindex, bend;
3583 +       struct au_fidir *fidir;
3584 +       struct au_hfile *hfile;
3585 +
3586 +       err = 0;
3587 +       root = IS_ROOT(file->f_path.dentry);
3588 +       if (root) {
3589 +               get_file(file);
3590 +               to_free[*idx] = file;
3591 +               (*idx)++;
3592 +               goto out;
3593 +       }
3594 +
3595 +       matched = 0;
3596 +       fidir = au_fi(file)->fi_hdir;
3597 +       AuDebugOn(!fidir);
3598 +       bend = au_fbend_dir(file);
3599 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
3600 +               hfile = fidir->fd_hfile + bindex;
3601 +               if (!hfile->hf_file)
3602 +                       continue;
3603 +
3604 +               if (hfile->hf_br->br_id == br_id) {
3605 +                       matched = 1;
3606 +                       break;
3607 +               }
3608 +       }
3609 +       if (matched)
3610 +               err = -EBUSY;
3611 +
3612 +out:
3613 +       return err;
3614 +}
3615 +
3616 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3617 +                         struct file **to_free, int opened)
3618 +{
3619 +       int err, idx;
3620 +       unsigned long long ull, max;
3621 +       aufs_bindex_t bstart;
3622 +       struct file *file, **array;
3623 +       struct dentry *root;
3624 +       struct au_hfile *hfile;
3625 +
3626 +       array = au_farray_alloc(sb, &max);
3627 +       err = PTR_ERR(array);
3628 +       if (IS_ERR(array))
3629 +               goto out;
3630 +
3631 +       err = 0;
3632 +       idx = 0;
3633 +       root = sb->s_root;
3634 +       di_write_unlock(root);
3635 +       for (ull = 0; ull < max; ull++) {
3636 +               file = array[ull];
3637 +               if (unlikely(!file))
3638 +                       break;
3639 +
3640 +               /* AuDbg("%pD\n", file); */
3641 +               fi_read_lock(file);
3642 +               bstart = au_fbstart(file);
3643 +               if (!d_is_dir(file->f_path.dentry)) {
3644 +                       hfile = &au_fi(file)->fi_htop;
3645 +                       if (hfile->hf_br->br_id == br_id)
3646 +                               err = -EBUSY;
3647 +               } else
3648 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3649 +               fi_read_unlock(file);
3650 +               if (unlikely(err))
3651 +                       break;
3652 +       }
3653 +       di_write_lock_child(root);
3654 +       au_farray_free(array, max);
3655 +       AuDebugOn(idx > opened);
3656 +
3657 +out:
3658 +       return err;
3659 +}
3660 +
3661 +static void br_del_file(struct file **to_free, unsigned long long opened,
3662 +                         aufs_bindex_t br_id)
3663 +{
3664 +       unsigned long long ull;
3665 +       aufs_bindex_t bindex, bstart, bend, bfound;
3666 +       struct file *file;
3667 +       struct au_fidir *fidir;
3668 +       struct au_hfile *hfile;
3669 +
3670 +       for (ull = 0; ull < opened; ull++) {
3671 +               file = to_free[ull];
3672 +               if (unlikely(!file))
3673 +                       break;
3674 +
3675 +               /* AuDbg("%pD\n", file); */
3676 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3677 +               bfound = -1;
3678 +               fidir = au_fi(file)->fi_hdir;
3679 +               AuDebugOn(!fidir);
3680 +               fi_write_lock(file);
3681 +               bstart = au_fbstart(file);
3682 +               bend = au_fbend_dir(file);
3683 +               for (bindex = bstart; bindex <= bend; bindex++) {
3684 +                       hfile = fidir->fd_hfile + bindex;
3685 +                       if (!hfile->hf_file)
3686 +                               continue;
3687 +
3688 +                       if (hfile->hf_br->br_id == br_id) {
3689 +                               bfound = bindex;
3690 +                               break;
3691 +                       }
3692 +               }
3693 +               AuDebugOn(bfound < 0);
3694 +               au_set_h_fptr(file, bfound, NULL);
3695 +               if (bfound == bstart) {
3696 +                       for (bstart++; bstart <= bend; bstart++)
3697 +                               if (au_hf_dir(file, bstart)) {
3698 +                                       au_set_fbstart(file, bstart);
3699 +                                       break;
3700 +                               }
3701 +               }
3702 +               fi_write_unlock(file);
3703 +       }
3704 +}
3705 +
3706 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3707 +                            const aufs_bindex_t bindex,
3708 +                            const aufs_bindex_t bend)
3709 +{
3710 +       struct au_branch **brp, **p;
3711 +
3712 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3713 +
3714 +       brp = sbinfo->si_branch + bindex;
3715 +       if (bindex < bend)
3716 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3717 +       sbinfo->si_branch[0 + bend] = NULL;
3718 +       sbinfo->si_bend--;
3719 +
3720 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3721 +       if (p)
3722 +               sbinfo->si_branch = p;
3723 +       /* harmless error */
3724 +}
3725 +
3726 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3727 +                            const aufs_bindex_t bend)
3728 +{
3729 +       struct au_hdentry *hdp, *p;
3730 +
3731 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3732 +
3733 +       hdp = dinfo->di_hdentry;
3734 +       if (bindex < bend)
3735 +               memmove(hdp + bindex, hdp + bindex + 1,
3736 +                       sizeof(*hdp) * (bend - bindex));
3737 +       hdp[0 + bend].hd_dentry = NULL;
3738 +       dinfo->di_bend--;
3739 +
3740 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3741 +       if (p)
3742 +               dinfo->di_hdentry = p;
3743 +       /* harmless error */
3744 +}
3745 +
3746 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3747 +                            const aufs_bindex_t bend)
3748 +{
3749 +       struct au_hinode *hip, *p;
3750 +
3751 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3752 +
3753 +       hip = iinfo->ii_hinode + bindex;
3754 +       if (bindex < bend)
3755 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3756 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3757 +       au_hn_init(iinfo->ii_hinode + bend);
3758 +       iinfo->ii_bend--;
3759 +
3760 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3761 +       if (p)
3762 +               iinfo->ii_hinode = p;
3763 +       /* harmless error */
3764 +}
3765 +
3766 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3767 +                        struct au_branch *br)
3768 +{
3769 +       aufs_bindex_t bend;
3770 +       struct au_sbinfo *sbinfo;
3771 +       struct dentry *root, *h_root;
3772 +       struct inode *inode, *h_inode;
3773 +       struct au_hinode *hinode;
3774 +
3775 +       SiMustWriteLock(sb);
3776 +
3777 +       root = sb->s_root;
3778 +       inode = d_inode(root);
3779 +       sbinfo = au_sbi(sb);
3780 +       bend = sbinfo->si_bend;
3781 +
3782 +       h_root = au_h_dptr(root, bindex);
3783 +       hinode = au_hi(inode, bindex);
3784 +       h_inode = au_igrab(hinode->hi_inode);
3785 +       au_hiput(hinode);
3786 +
3787 +       au_sbilist_lock();
3788 +       au_br_do_del_brp(sbinfo, bindex, bend);
3789 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3790 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3791 +       au_sbilist_unlock();
3792 +
3793 +       dput(h_root);
3794 +       iput(h_inode);
3795 +       au_br_do_free(br);
3796 +}
3797 +
3798 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3799 +                                  unsigned long long max, void *arg)
3800 +{
3801 +       return max;
3802 +}
3803 +
3804 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3805 +{
3806 +       int err, rerr, i;
3807 +       unsigned long long opened;
3808 +       unsigned int mnt_flags;
3809 +       aufs_bindex_t bindex, bend, br_id;
3810 +       unsigned char do_wh, verbose;
3811 +       struct au_branch *br;
3812 +       struct au_wbr *wbr;
3813 +       struct dentry *root;
3814 +       struct file **to_free;
3815 +
3816 +       err = 0;
3817 +       opened = 0;
3818 +       to_free = NULL;
3819 +       root = sb->s_root;
3820 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3821 +       if (bindex < 0) {
3822 +               if (remount)
3823 +                       goto out; /* success */
3824 +               err = -ENOENT;
3825 +               pr_err("%s no such branch\n", del->pathname);
3826 +               goto out;
3827 +       }
3828 +       AuDbg("bindex b%d\n", bindex);
3829 +
3830 +       err = -EBUSY;
3831 +       mnt_flags = au_mntflags(sb);
3832 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3833 +       bend = au_sbend(sb);
3834 +       if (unlikely(!bend)) {
3835 +               AuVerbose(verbose, "no more branches left\n");
3836 +               goto out;
3837 +       }
3838 +       br = au_sbr(sb, bindex);
3839 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3840 +
3841 +       br_id = br->br_id;
3842 +       opened = atomic_read(&br->br_count);
3843 +       if (unlikely(opened)) {
3844 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3845 +               err = PTR_ERR(to_free);
3846 +               if (IS_ERR(to_free))
3847 +                       goto out;
3848 +
3849 +               err = test_file_busy(sb, br_id, to_free, opened);
3850 +               if (unlikely(err)) {
3851 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3852 +                       goto out;
3853 +               }
3854 +       }
3855 +
3856 +       wbr = br->br_wbr;
3857 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3858 +       if (do_wh) {
3859 +               /* instead of WbrWhMustWriteLock(wbr) */
3860 +               SiMustWriteLock(sb);
3861 +               for (i = 0; i < AuBrWh_Last; i++) {
3862 +                       dput(wbr->wbr_wh[i]);
3863 +                       wbr->wbr_wh[i] = NULL;
3864 +               }
3865 +       }
3866 +
3867 +       err = test_children_busy(root, bindex, verbose);
3868 +       if (unlikely(err)) {
3869 +               if (do_wh)
3870 +                       goto out_wh;
3871 +               goto out;
3872 +       }
3873 +
3874 +       err = 0;
3875 +       if (to_free) {
3876 +               /*
3877 +                * now we confirmed the branch is deletable.
3878 +                * let's free the remaining opened dirs on the branch.
3879 +                */
3880 +               di_write_unlock(root);
3881 +               br_del_file(to_free, opened, br_id);
3882 +               di_write_lock_child(root);
3883 +       }
3884 +
3885 +       if (!remount)
3886 +               au_br_do_del(sb, bindex, br);
3887 +       else {
3888 +               sysaufs_brs_del(sb, bindex);
3889 +               au_br_do_del(sb, bindex, br);
3890 +               sysaufs_brs_add(sb, bindex);
3891 +       }
3892 +
3893 +       if (!bindex) {
3894 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3895 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3896 +       } else
3897 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3898 +       if (au_opt_test(mnt_flags, PLINK))
3899 +               au_plink_half_refresh(sb, br_id);
3900 +
3901 +       if (au_xino_brid(sb) == br_id)
3902 +               au_xino_brid_set(sb, -1);
3903 +       goto out; /* success */
3904 +
3905 +out_wh:
3906 +       /* revert */
3907 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3908 +       if (rerr)
3909 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3910 +                       del->pathname, rerr);
3911 +out:
3912 +       if (to_free)
3913 +               au_farray_free(to_free, opened);
3914 +       return err;
3915 +}
3916 +
3917 +/* ---------------------------------------------------------------------- */
3918 +
3919 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3920 +{
3921 +       int err;
3922 +       aufs_bindex_t bstart, bend;
3923 +       struct aufs_ibusy ibusy;
3924 +       struct inode *inode, *h_inode;
3925 +
3926 +       err = -EPERM;
3927 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3928 +               goto out;
3929 +
3930 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3931 +       if (!err)
3932 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3933 +       if (unlikely(err)) {
3934 +               err = -EFAULT;
3935 +               AuTraceErr(err);
3936 +               goto out;
3937 +       }
3938 +
3939 +       err = -EINVAL;
3940 +       si_read_lock(sb, AuLock_FLUSH);
3941 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3942 +               goto out_unlock;
3943 +
3944 +       err = 0;
3945 +       ibusy.h_ino = 0; /* invalid */
3946 +       inode = ilookup(sb, ibusy.ino);
3947 +       if (!inode
3948 +           || inode->i_ino == AUFS_ROOT_INO
3949 +           || is_bad_inode(inode))
3950 +               goto out_unlock;
3951 +
3952 +       ii_read_lock_child(inode);
3953 +       bstart = au_ibstart(inode);
3954 +       bend = au_ibend(inode);
3955 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3956 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3957 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3958 +                       ibusy.h_ino = h_inode->i_ino;
3959 +       }
3960 +       ii_read_unlock(inode);
3961 +       iput(inode);
3962 +
3963 +out_unlock:
3964 +       si_read_unlock(sb);
3965 +       if (!err) {
3966 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3967 +               if (unlikely(err)) {
3968 +                       err = -EFAULT;
3969 +                       AuTraceErr(err);
3970 +               }
3971 +       }
3972 +out:
3973 +       return err;
3974 +}
3975 +
3976 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3977 +{
3978 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3979 +}
3980 +
3981 +#ifdef CONFIG_COMPAT
3982 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3983 +{
3984 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3985 +}
3986 +#endif
3987 +
3988 +/* ---------------------------------------------------------------------- */
3989 +
3990 +/*
3991 + * change a branch permission
3992 + */
3993 +
3994 +static void au_warn_ima(void)
3995 +{
3996 +#ifdef CONFIG_IMA
3997 +       /* since it doesn't support mark_files_ro() */
3998 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3999 +#endif
4000 +}
4001 +
4002 +static int do_need_sigen_inc(int a, int b)
4003 +{
4004 +       return au_br_whable(a) && !au_br_whable(b);
4005 +}
4006 +
4007 +static int need_sigen_inc(int old, int new)
4008 +{
4009 +       return do_need_sigen_inc(old, new)
4010 +               || do_need_sigen_inc(new, old);
4011 +}
4012 +
4013 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4014 +{
4015 +       int err, do_warn;
4016 +       unsigned int mnt_flags;
4017 +       unsigned long long ull, max;
4018 +       aufs_bindex_t br_id;
4019 +       unsigned char verbose, writer;
4020 +       struct file *file, *hf, **array;
4021 +       struct au_hfile *hfile;
4022 +
4023 +       mnt_flags = au_mntflags(sb);
4024 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4025 +
4026 +       array = au_farray_alloc(sb, &max);
4027 +       err = PTR_ERR(array);
4028 +       if (IS_ERR(array))
4029 +               goto out;
4030 +
4031 +       do_warn = 0;
4032 +       br_id = au_sbr_id(sb, bindex);
4033 +       for (ull = 0; ull < max; ull++) {
4034 +               file = array[ull];
4035 +               if (unlikely(!file))
4036 +                       break;
4037 +
4038 +               /* AuDbg("%pD\n", file); */
4039 +               fi_read_lock(file);
4040 +               if (unlikely(au_test_mmapped(file))) {
4041 +                       err = -EBUSY;
4042 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4043 +                       AuDbgFile(file);
4044 +                       FiMustNoWaiters(file);
4045 +                       fi_read_unlock(file);
4046 +                       goto out_array;
4047 +               }
4048 +
4049 +               hfile = &au_fi(file)->fi_htop;
4050 +               hf = hfile->hf_file;
4051 +               if (!d_is_reg(file->f_path.dentry)
4052 +                   || !(file->f_mode & FMODE_WRITE)
4053 +                   || hfile->hf_br->br_id != br_id
4054 +                   || !(hf->f_mode & FMODE_WRITE))
4055 +                       array[ull] = NULL;
4056 +               else {
4057 +                       do_warn = 1;
4058 +                       get_file(file);
4059 +               }
4060 +
4061 +               FiMustNoWaiters(file);
4062 +               fi_read_unlock(file);
4063 +               fput(file);
4064 +       }
4065 +
4066 +       err = 0;
4067 +       if (do_warn)
4068 +               au_warn_ima();
4069 +
4070 +       for (ull = 0; ull < max; ull++) {
4071 +               file = array[ull];
4072 +               if (!file)
4073 +                       continue;
4074 +
4075 +               /* todo: already flushed? */
4076 +               /*
4077 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4078 +                * approach which resets f_mode and calls mnt_drop_write() and
4079 +                * file_release_write() for each file, because the branch
4080 +                * attribute in aufs world is totally different from the native
4081 +                * fs rw/ro mode.
4082 +               */
4083 +               /* fi_read_lock(file); */
4084 +               hfile = &au_fi(file)->fi_htop;
4085 +               hf = hfile->hf_file;
4086 +               /* fi_read_unlock(file); */
4087 +               spin_lock(&hf->f_lock);
4088 +               writer = !!(hf->f_mode & FMODE_WRITER);
4089 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4090 +               spin_unlock(&hf->f_lock);
4091 +               if (writer) {
4092 +                       put_write_access(file_inode(hf));
4093 +                       __mnt_drop_write(hf->f_path.mnt);
4094 +               }
4095 +       }
4096 +
4097 +out_array:
4098 +       au_farray_free(array, max);
4099 +out:
4100 +       AuTraceErr(err);
4101 +       return err;
4102 +}
4103 +
4104 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4105 +             int *do_refresh)
4106 +{
4107 +       int err, rerr;
4108 +       aufs_bindex_t bindex;
4109 +       struct dentry *root;
4110 +       struct au_branch *br;
4111 +       struct au_br_fhsm *bf;
4112 +
4113 +       root = sb->s_root;
4114 +       bindex = au_find_dbindex(root, mod->h_root);
4115 +       if (bindex < 0) {
4116 +               if (remount)
4117 +                       return 0; /* success */
4118 +               err = -ENOENT;
4119 +               pr_err("%s no such branch\n", mod->path);
4120 +               goto out;
4121 +       }
4122 +       AuDbg("bindex b%d\n", bindex);
4123 +
4124 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4125 +       if (unlikely(err))
4126 +               goto out;
4127 +
4128 +       br = au_sbr(sb, bindex);
4129 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4130 +       if (br->br_perm == mod->perm)
4131 +               return 0; /* success */
4132 +
4133 +       /* pre-allocate for non-fhsm --> fhsm */
4134 +       bf = NULL;
4135 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4136 +               err = au_fhsm_br_alloc(br);
4137 +               if (unlikely(err))
4138 +                       goto out;
4139 +               bf = br->br_fhsm;
4140 +               br->br_fhsm = NULL;
4141 +       }
4142 +
4143 +       if (au_br_writable(br->br_perm)) {
4144 +               /* remove whiteout base */
4145 +               err = au_br_init_wh(sb, br, mod->perm);
4146 +               if (unlikely(err))
4147 +                       goto out_bf;
4148 +
4149 +               if (!au_br_writable(mod->perm)) {
4150 +                       /* rw --> ro, file might be mmapped */
4151 +                       DiMustNoWaiters(root);
4152 +                       IiMustNoWaiters(d_inode(root));
4153 +                       di_write_unlock(root);
4154 +                       err = au_br_mod_files_ro(sb, bindex);
4155 +                       /* aufs_write_lock() calls ..._child() */
4156 +                       di_write_lock_child(root);
4157 +
4158 +                       if (unlikely(err)) {
4159 +                               rerr = -ENOMEM;
4160 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4161 +                                                    GFP_NOFS);
4162 +                               if (br->br_wbr)
4163 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4164 +                               if (unlikely(rerr)) {
4165 +                                       AuIOErr("nested error %d (%d)\n",
4166 +                                               rerr, err);
4167 +                                       br->br_perm = mod->perm;
4168 +                               }
4169 +                       }
4170 +               }
4171 +       } else if (au_br_writable(mod->perm)) {
4172 +               /* ro --> rw */
4173 +               err = -ENOMEM;
4174 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4175 +               if (br->br_wbr) {
4176 +                       err = au_wbr_init(br, sb, mod->perm);
4177 +                       if (unlikely(err)) {
4178 +                               kfree(br->br_wbr);
4179 +                               br->br_wbr = NULL;
4180 +                       }
4181 +               }
4182 +       }
4183 +       if (unlikely(err))
4184 +               goto out_bf;
4185 +
4186 +       if (au_br_fhsm(br->br_perm)) {
4187 +               if (!au_br_fhsm(mod->perm)) {
4188 +                       /* fhsm --> non-fhsm */
4189 +                       au_br_fhsm_fin(br->br_fhsm);
4190 +                       kfree(br->br_fhsm);
4191 +                       br->br_fhsm = NULL;
4192 +               }
4193 +       } else if (au_br_fhsm(mod->perm))
4194 +               /* non-fhsm --> fhsm */
4195 +               br->br_fhsm = bf;
4196 +
4197 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4198 +       br->br_perm = mod->perm;
4199 +       goto out; /* success */
4200 +
4201 +out_bf:
4202 +       kfree(bf);
4203 +out:
4204 +       AuTraceErr(err);
4205 +       return err;
4206 +}
4207 +
4208 +/* ---------------------------------------------------------------------- */
4209 +
4210 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4211 +{
4212 +       int err;
4213 +       struct kstatfs kstfs;
4214 +
4215 +       err = vfs_statfs(&br->br_path, &kstfs);
4216 +       if (!err) {
4217 +               stfs->f_blocks = kstfs.f_blocks;
4218 +               stfs->f_bavail = kstfs.f_bavail;
4219 +               stfs->f_files = kstfs.f_files;
4220 +               stfs->f_ffree = kstfs.f_ffree;
4221 +       }
4222 +
4223 +       return err;
4224 +}
4225 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4226 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4227 +++ linux/fs/aufs/branch.h      2016-04-24 18:32:51.390353525 +0200
4228 @@ -0,0 +1,279 @@
4229 +/*
4230 + * Copyright (C) 2005-2016 Junjiro R. Okajima
4231 + *
4232 + * This program, aufs is free software; you can redistribute it and/or modify
4233 + * it under the terms of the GNU General Public License as published by
4234 + * the Free Software Foundation; either version 2 of the License, or
4235 + * (at your option) any later version.
4236 + *
4237 + * This program is distributed in the hope that it will be useful,
4238 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4239 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4240 + * GNU General Public License for more details.
4241 + *
4242 + * You should have received a copy of the GNU General Public License
4243 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4244 + */
4245 +
4246 +/*
4247 + * branch filesystems and xino for them
4248 + */
4249 +
4250 +#ifndef __AUFS_BRANCH_H__
4251 +#define __AUFS_BRANCH_H__
4252 +
4253 +#ifdef __KERNEL__
4254 +
4255 +#include <linux/mount.h>
4256 +#include "dynop.h"
4257 +#include "rwsem.h"
4258 +#include "super.h"
4259 +
4260 +/* ---------------------------------------------------------------------- */
4261 +
4262 +/* a xino file */
4263 +struct au_xino_file {
4264 +       struct file             *xi_file;
4265 +       struct mutex            xi_nondir_mtx;
4266 +
4267 +       /* todo: make xino files an array to support huge inode number */
4268 +
4269 +#ifdef CONFIG_DEBUG_FS
4270 +       struct dentry            *xi_dbgaufs;
4271 +#endif
4272 +};
4273 +
4274 +/* File-based Hierarchical Storage Management */
4275 +struct au_br_fhsm {
4276 +#ifdef CONFIG_AUFS_FHSM
4277 +       struct mutex            bf_lock;
4278 +       unsigned long           bf_jiffy;
4279 +       struct aufs_stfs        bf_stfs;
4280 +       int                     bf_readable;
4281 +#endif
4282 +};
4283 +
4284 +/* members for writable branch only */
4285 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4286 +struct au_wbr {
4287 +       struct au_rwsem         wbr_wh_rwsem;
4288 +       struct dentry           *wbr_wh[AuBrWh_Last];
4289 +       atomic_t                wbr_wh_running;
4290 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4291 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4292 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4293 +
4294 +       /* mfs mode */
4295 +       unsigned long long      wbr_bytes;
4296 +};
4297 +
4298 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4299 +#define AuBrDynOp (AuDyLast * 4)
4300 +
4301 +#ifdef CONFIG_AUFS_HFSNOTIFY
4302 +/* support for asynchronous destruction */
4303 +struct au_br_hfsnotify {
4304 +       struct fsnotify_group   *hfsn_group;
4305 +};
4306 +#endif
4307 +
4308 +/* sysfs entries */
4309 +struct au_brsysfs {
4310 +       char                    name[16];
4311 +       struct attribute        attr;
4312 +};
4313 +
4314 +enum {
4315 +       AuBrSysfs_BR,
4316 +       AuBrSysfs_BRID,
4317 +       AuBrSysfs_Last
4318 +};
4319 +
4320 +/* protected by superblock rwsem */
4321 +struct au_branch {
4322 +       struct au_xino_file     br_xino;
4323 +
4324 +       aufs_bindex_t           br_id;
4325 +
4326 +       int                     br_perm;
4327 +       struct path             br_path;
4328 +       spinlock_t              br_dykey_lock;
4329 +       struct au_dykey         *br_dykey[AuBrDynOp];
4330 +       atomic_t                br_count;
4331 +
4332 +       struct au_wbr           *br_wbr;
4333 +       struct au_br_fhsm       *br_fhsm;
4334 +
4335 +       /* xino truncation */
4336 +       atomic_t                br_xino_running;
4337 +
4338 +#ifdef CONFIG_AUFS_HFSNOTIFY
4339 +       struct au_br_hfsnotify  *br_hfsn;
4340 +#endif
4341 +
4342 +#ifdef CONFIG_SYSFS
4343 +       /* entries under sysfs per mount-point */
4344 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4345 +#endif
4346 +};
4347 +
4348 +/* ---------------------------------------------------------------------- */
4349 +
4350 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4351 +{
4352 +       return br->br_path.mnt;
4353 +}
4354 +
4355 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4356 +{
4357 +       return br->br_path.dentry;
4358 +}
4359 +
4360 +static inline struct super_block *au_br_sb(struct au_branch *br)
4361 +{
4362 +       return au_br_mnt(br)->mnt_sb;
4363 +}
4364 +
4365 +static inline int au_br_rdonly(struct au_branch *br)
4366 +{
4367 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4368 +               || !au_br_writable(br->br_perm))
4369 +               ? -EROFS : 0;
4370 +}
4371 +
4372 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4373 +{
4374 +#ifdef CONFIG_AUFS_HNOTIFY
4375 +       return !(brperm & AuBrPerm_RR);
4376 +#else
4377 +       return 0;
4378 +#endif
4379 +}
4380 +
4381 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4382 +{
4383 +       int err, exec_flag;
4384 +
4385 +       err = 0;
4386 +       exec_flag = oflag & __FMODE_EXEC;
4387 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4388 +               err = -EACCES;
4389 +
4390 +       return err;
4391 +}
4392 +
4393 +/* ---------------------------------------------------------------------- */
4394 +
4395 +/* branch.c */
4396 +struct au_sbinfo;
4397 +void au_br_free(struct au_sbinfo *sinfo);
4398 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4399 +struct au_opt_add;
4400 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4401 +struct au_opt_del;
4402 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4403 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4404 +#ifdef CONFIG_COMPAT
4405 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4406 +#endif
4407 +struct au_opt_mod;
4408 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4409 +             int *do_refresh);
4410 +struct aufs_stfs;
4411 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4412 +
4413 +/* xino.c */
4414 +static const loff_t au_loff_max = LLONG_MAX;
4415 +
4416 +int au_xib_trunc(struct super_block *sb);
4417 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4418 +                  loff_t *pos);
4419 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4420 +                   size_t size, loff_t *pos);
4421 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4422 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4423 +ino_t au_xino_new_ino(struct super_block *sb);
4424 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4425 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4426 +                 ino_t ino);
4427 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4428 +                ino_t *ino);
4429 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4430 +              struct file *base_file, int do_test);
4431 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4432 +
4433 +struct au_opt_xino;
4434 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4435 +void au_xino_clr(struct super_block *sb);
4436 +struct file *au_xino_def(struct super_block *sb);
4437 +int au_xino_path(struct seq_file *seq, struct file *file);
4438 +
4439 +/* ---------------------------------------------------------------------- */
4440 +
4441 +/* Superblock to branch */
4442 +static inline
4443 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4444 +{
4445 +       return au_sbr(sb, bindex)->br_id;
4446 +}
4447 +
4448 +static inline
4449 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4450 +{
4451 +       return au_br_mnt(au_sbr(sb, bindex));
4452 +}
4453 +
4454 +static inline
4455 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4456 +{
4457 +       return au_br_sb(au_sbr(sb, bindex));
4458 +}
4459 +
4460 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4461 +{
4462 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4463 +}
4464 +
4465 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4466 +{
4467 +       return au_sbr(sb, bindex)->br_perm;
4468 +}
4469 +
4470 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4471 +{
4472 +       return au_br_whable(au_sbr_perm(sb, bindex));
4473 +}
4474 +
4475 +/* ---------------------------------------------------------------------- */
4476 +
4477 +/*
4478 + * wbr_wh_read_lock, wbr_wh_write_lock
4479 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4480 + */
4481 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4482 +
4483 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4484 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4485 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4486 +
4487 +/* ---------------------------------------------------------------------- */
4488 +
4489 +#ifdef CONFIG_AUFS_FHSM
4490 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4491 +{
4492 +       mutex_init(&brfhsm->bf_lock);
4493 +       brfhsm->bf_jiffy = 0;
4494 +       brfhsm->bf_readable = 0;
4495 +}
4496 +
4497 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4498 +{
4499 +       mutex_destroy(&brfhsm->bf_lock);
4500 +}
4501 +#else
4502 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4503 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4504 +#endif
4505 +
4506 +#endif /* __KERNEL__ */
4507 +#endif /* __AUFS_BRANCH_H__ */
4508 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4509 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4510 +++ linux/fs/aufs/conf.mk       2016-04-24 18:32:51.390353525 +0200
4511 @@ -0,0 +1,38 @@
4512 +
4513 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4514 +
4515 +define AuConf
4516 +ifdef ${1}
4517 +AuConfStr += ${1}=${${1}}
4518 +endif
4519 +endef
4520 +
4521 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4522 +       SBILIST \
4523 +       HNOTIFY HFSNOTIFY \
4524 +       EXPORT INO_T_64 \
4525 +       XATTR \
4526 +       FHSM \
4527 +       RDU \
4528 +       SHWH \
4529 +       BR_RAMFS \
4530 +       BR_FUSE POLL \
4531 +       BR_HFSPLUS \
4532 +       BDEV_LOOP \
4533 +       DEBUG MAGIC_SYSRQ
4534 +$(foreach i, ${AuConfAll}, \
4535 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4536 +
4537 +AuConfName = ${obj}/conf.str
4538 +${AuConfName}.tmp: FORCE
4539 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4540 +${AuConfName}: ${AuConfName}.tmp
4541 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4542 +       echo '  GEN    ' $@; \
4543 +       cp -p $< $@; \
4544 +       }
4545 +FORCE:
4546 +clean-files += ${AuConfName} ${AuConfName}.tmp
4547 +${obj}/sysfs.o: ${AuConfName}
4548 +
4549 +-include ${srctree}/${src}/conf_priv.mk
4550 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4551 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4552 +++ linux/fs/aufs/cpup.c        2016-04-24 18:32:51.390353525 +0200
4553 @@ -0,0 +1,1379 @@
4554 +/*
4555 + * Copyright (C) 2005-2016 Junjiro R. Okajima
4556 + *
4557 + * This program, aufs is free software; you can redistribute it and/or modify
4558 + * it under the terms of the GNU General Public License as published by
4559 + * the Free Software Foundation; either version 2 of the License, or
4560 + * (at your option) any later version.
4561 + *
4562 + * This program is distributed in the hope that it will be useful,
4563 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4564 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4565 + * GNU General Public License for more details.
4566 + *
4567 + * You should have received a copy of the GNU General Public License
4568 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4569 + */
4570 +
4571 +/*
4572 + * copy-up functions, see wbr_policy.c for copy-down
4573 + */
4574 +
4575 +#include <linux/fs_stack.h>
4576 +#include <linux/mm.h>
4577 +#include <linux/task_work.h>
4578 +#include "aufs.h"
4579 +
4580 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4581 +{
4582 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4583 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4584 +
4585 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4586 +
4587 +       dst->i_flags |= iflags & ~mask;
4588 +       if (au_test_fs_notime(dst->i_sb))
4589 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4590 +}
4591 +
4592 +void au_cpup_attr_timesizes(struct inode *inode)
4593 +{
4594 +       struct inode *h_inode;
4595 +
4596 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4597 +       fsstack_copy_attr_times(inode, h_inode);
4598 +       fsstack_copy_inode_size(inode, h_inode);
4599 +}
4600 +
4601 +void au_cpup_attr_nlink(struct inode *inode, int force)
4602 +{
4603 +       struct inode *h_inode;
4604 +       struct super_block *sb;
4605 +       aufs_bindex_t bindex, bend;
4606 +
4607 +       sb = inode->i_sb;
4608 +       bindex = au_ibstart(inode);
4609 +       h_inode = au_h_iptr(inode, bindex);
4610 +       if (!force
4611 +           && !S_ISDIR(h_inode->i_mode)
4612 +           && au_opt_test(au_mntflags(sb), PLINK)
4613 +           && au_plink_test(inode))
4614 +               return;
4615 +
4616 +       /*
4617 +        * 0 can happen in revalidating.
4618 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4619 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4620 +        * case.
4621 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4622 +        *       the incorrect link count.
4623 +        */
4624 +       set_nlink(inode, h_inode->i_nlink);
4625 +
4626 +       /*
4627 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4628 +        * it may includes whplink directory.
4629 +        */
4630 +       if (S_ISDIR(h_inode->i_mode)) {
4631 +               bend = au_ibend(inode);
4632 +               for (bindex++; bindex <= bend; bindex++) {
4633 +                       h_inode = au_h_iptr(inode, bindex);
4634 +                       if (h_inode)
4635 +                               au_add_nlink(inode, h_inode);
4636 +               }
4637 +       }
4638 +}
4639 +
4640 +void au_cpup_attr_changeable(struct inode *inode)
4641 +{
4642 +       struct inode *h_inode;
4643 +
4644 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4645 +       inode->i_mode = h_inode->i_mode;
4646 +       inode->i_uid = h_inode->i_uid;
4647 +       inode->i_gid = h_inode->i_gid;
4648 +       au_cpup_attr_timesizes(inode);
4649 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4650 +}
4651 +
4652 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4653 +{
4654 +       struct au_iinfo *iinfo = au_ii(inode);
4655 +
4656 +       IiMustWriteLock(inode);
4657 +
4658 +       iinfo->ii_higen = h_inode->i_generation;
4659 +       iinfo->ii_hsb1 = h_inode->i_sb;
4660 +}
4661 +
4662 +void au_cpup_attr_all(struct inode *inode, int force)
4663 +{
4664 +       struct inode *h_inode;
4665 +
4666 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4667 +       au_cpup_attr_changeable(inode);
4668 +       if (inode->i_nlink > 0)
4669 +               au_cpup_attr_nlink(inode, force);
4670 +       inode->i_rdev = h_inode->i_rdev;
4671 +       inode->i_blkbits = h_inode->i_blkbits;
4672 +       au_cpup_igen(inode, h_inode);
4673 +}
4674 +
4675 +/* ---------------------------------------------------------------------- */
4676 +
4677 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4678 +
4679 +/* keep the timestamps of the parent dir when cpup */
4680 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4681 +                   struct path *h_path)
4682 +{
4683 +       struct inode *h_inode;
4684 +
4685 +       dt->dt_dentry = dentry;
4686 +       dt->dt_h_path = *h_path;
4687 +       h_inode = d_inode(h_path->dentry);
4688 +       dt->dt_atime = h_inode->i_atime;
4689 +       dt->dt_mtime = h_inode->i_mtime;
4690 +       /* smp_mb(); */
4691 +}
4692 +
4693 +void au_dtime_revert(struct au_dtime *dt)
4694 +{
4695 +       struct iattr attr;
4696 +       int err;
4697 +
4698 +       attr.ia_atime = dt->dt_atime;
4699 +       attr.ia_mtime = dt->dt_mtime;
4700 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4701 +               | ATTR_ATIME | ATTR_ATIME_SET;
4702 +
4703 +       /* no delegation since this is a directory */
4704 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4705 +       if (unlikely(err))
4706 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4707 +}
4708 +
4709 +/* ---------------------------------------------------------------------- */
4710 +
4711 +/* internal use only */
4712 +struct au_cpup_reg_attr {
4713 +       int             valid;
4714 +       struct kstat    st;
4715 +       unsigned int    iflags; /* inode->i_flags */
4716 +};
4717 +
4718 +static noinline_for_stack
4719 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4720 +              struct au_cpup_reg_attr *h_src_attr)
4721 +{
4722 +       int err, sbits, icex;
4723 +       unsigned int mnt_flags;
4724 +       unsigned char verbose;
4725 +       struct iattr ia;
4726 +       struct path h_path;
4727 +       struct inode *h_isrc, *h_idst;
4728 +       struct kstat *h_st;
4729 +       struct au_branch *br;
4730 +
4731 +       h_path.dentry = au_h_dptr(dst, bindex);
4732 +       h_idst = d_inode(h_path.dentry);
4733 +       br = au_sbr(dst->d_sb, bindex);
4734 +       h_path.mnt = au_br_mnt(br);
4735 +       h_isrc = d_inode(h_src);
4736 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4737 +               | ATTR_ATIME | ATTR_MTIME
4738 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4739 +       if (h_src_attr && h_src_attr->valid) {
4740 +               h_st = &h_src_attr->st;
4741 +               ia.ia_uid = h_st->uid;
4742 +               ia.ia_gid = h_st->gid;
4743 +               ia.ia_atime = h_st->atime;
4744 +               ia.ia_mtime = h_st->mtime;
4745 +               if (h_idst->i_mode != h_st->mode
4746 +                   && !S_ISLNK(h_idst->i_mode)) {
4747 +                       ia.ia_valid |= ATTR_MODE;
4748 +                       ia.ia_mode = h_st->mode;
4749 +               }
4750 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4751 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4752 +       } else {
4753 +               ia.ia_uid = h_isrc->i_uid;
4754 +               ia.ia_gid = h_isrc->i_gid;
4755 +               ia.ia_atime = h_isrc->i_atime;
4756 +               ia.ia_mtime = h_isrc->i_mtime;
4757 +               if (h_idst->i_mode != h_isrc->i_mode
4758 +                   && !S_ISLNK(h_idst->i_mode)) {
4759 +                       ia.ia_valid |= ATTR_MODE;
4760 +                       ia.ia_mode = h_isrc->i_mode;
4761 +               }
4762 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4763 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4764 +       }
4765 +       /* no delegation since it is just created */
4766 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4767 +
4768 +       /* is this nfs only? */
4769 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4770 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4771 +               ia.ia_mode = h_isrc->i_mode;
4772 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4773 +       }
4774 +
4775 +       icex = br->br_perm & AuBrAttr_ICEX;
4776 +       if (!err) {
4777 +               mnt_flags = au_mntflags(dst->d_sb);
4778 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4779 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4780 +       }
4781 +
4782 +       return err;
4783 +}
4784 +
4785 +/* ---------------------------------------------------------------------- */
4786 +
4787 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4788 +                          char *buf, unsigned long blksize)
4789 +{
4790 +       int err;
4791 +       size_t sz, rbytes, wbytes;
4792 +       unsigned char all_zero;
4793 +       char *p, *zp;
4794 +       struct inode *h_inode;
4795 +       /* reduce stack usage */
4796 +       struct iattr *ia;
4797 +
4798 +       zp = page_address(ZERO_PAGE(0));
4799 +       if (unlikely(!zp))
4800 +               return -ENOMEM; /* possible? */
4801 +
4802 +       err = 0;
4803 +       all_zero = 0;
4804 +       while (len) {
4805 +               AuDbg("len %lld\n", len);
4806 +               sz = blksize;
4807 +               if (len < blksize)
4808 +                       sz = len;
4809 +
4810 +               rbytes = 0;
4811 +               /* todo: signal_pending? */
4812 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4813 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4814 +                       err = rbytes;
4815 +               }
4816 +               if (unlikely(err < 0))
4817 +                       break;
4818 +
4819 +               all_zero = 0;
4820 +               if (len >= rbytes && rbytes == blksize)
4821 +                       all_zero = !memcmp(buf, zp, rbytes);
4822 +               if (!all_zero) {
4823 +                       wbytes = rbytes;
4824 +                       p = buf;
4825 +                       while (wbytes) {
4826 +                               size_t b;
4827 +
4828 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4829 +                               err = b;
4830 +                               /* todo: signal_pending? */
4831 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4832 +                                       continue;
4833 +                               if (unlikely(err < 0))
4834 +                                       break;
4835 +                               wbytes -= b;
4836 +                               p += b;
4837 +                       }
4838 +                       if (unlikely(err < 0))
4839 +                               break;
4840 +               } else {
4841 +                       loff_t res;
4842 +
4843 +                       AuLabel(hole);
4844 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4845 +                       err = res;
4846 +                       if (unlikely(res < 0))
4847 +                               break;
4848 +               }
4849 +               len -= rbytes;
4850 +               err = 0;
4851 +       }
4852 +
4853 +       /* the last block may be a hole */
4854 +       if (!err && all_zero) {
4855 +               AuLabel(last hole);
4856 +
4857 +               err = 1;
4858 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4859 +                       /* nfs requires this step to make last hole */
4860 +                       /* is this only nfs? */
4861 +                       do {
4862 +                               /* todo: signal_pending? */
4863 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4864 +                       } while (err == -EAGAIN || err == -EINTR);
4865 +                       if (err == 1)
4866 +                               dst->f_pos--;
4867 +               }
4868 +
4869 +               if (err == 1) {
4870 +                       ia = (void *)buf;
4871 +                       ia->ia_size = dst->f_pos;
4872 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4873 +                       ia->ia_file = dst;
4874 +                       h_inode = file_inode(dst);
4875 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
4876 +                       /* no delegation since it is just created */
4877 +                       err = vfsub_notify_change(&dst->f_path, ia,
4878 +                                                 /*delegated*/NULL);
4879 +                       inode_unlock(h_inode);
4880 +               }
4881 +       }
4882 +
4883 +       return err;
4884 +}
4885 +
4886 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4887 +{
4888 +       int err;
4889 +       unsigned long blksize;
4890 +       unsigned char do_kfree;
4891 +       char *buf;
4892 +
4893 +       err = -ENOMEM;
4894 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
4895 +       if (!blksize || PAGE_SIZE < blksize)
4896 +               blksize = PAGE_SIZE;
4897 +       AuDbg("blksize %lu\n", blksize);
4898 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4899 +       if (do_kfree)
4900 +               buf = kmalloc(blksize, GFP_NOFS);
4901 +       else
4902 +               buf = (void *)__get_free_page(GFP_NOFS);
4903 +       if (unlikely(!buf))
4904 +               goto out;
4905 +
4906 +       if (len > (1 << 22))
4907 +               AuDbg("copying a large file %lld\n", (long long)len);
4908 +
4909 +       src->f_pos = 0;
4910 +       dst->f_pos = 0;
4911 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4912 +       if (do_kfree)
4913 +               kfree(buf);
4914 +       else
4915 +               free_page((unsigned long)buf);
4916 +
4917 +out:
4918 +       return err;
4919 +}
4920 +
4921 +/*
4922 + * to support a sparse file which is opened with O_APPEND,
4923 + * we need to close the file.
4924 + */
4925 +static int au_cp_regular(struct au_cp_generic *cpg)
4926 +{
4927 +       int err, i;
4928 +       enum { SRC, DST };
4929 +       struct {
4930 +               aufs_bindex_t bindex;
4931 +               unsigned int flags;
4932 +               struct dentry *dentry;
4933 +               int force_wr;
4934 +               struct file *file;
4935 +               void *label;
4936 +       } *f, file[] = {
4937 +               {
4938 +                       .bindex = cpg->bsrc,
4939 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4940 +                       .label = &&out
4941 +               },
4942 +               {
4943 +                       .bindex = cpg->bdst,
4944 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4945 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4946 +                       .label = &&out_src
4947 +               }
4948 +       };
4949 +       struct super_block *sb;
4950 +       struct task_struct *tsk = current;
4951 +
4952 +       /* bsrc branch can be ro/rw. */
4953 +       sb = cpg->dentry->d_sb;
4954 +       f = file;
4955 +       for (i = 0; i < 2; i++, f++) {
4956 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4957 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4958 +                                   /*file*/NULL, f->force_wr);
4959 +               err = PTR_ERR(f->file);
4960 +               if (IS_ERR(f->file))
4961 +                       goto *f->label;
4962 +       }
4963 +
4964 +       /* try stopping to update while we copyup */
4965 +       IMustLock(d_inode(file[SRC].dentry));
4966 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4967 +
4968 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
4969 +       if (tsk->flags & PF_KTHREAD)
4970 +               __fput_sync(file[DST].file);
4971 +       else {
4972 +               WARN(1, "%pD\nPlease report this warning to aufs-users ML",
4973 +                    file[DST].file);
4974 +               fput(file[DST].file);
4975 +               /*
4976 +                * too bad.
4977 +                * we have to call both since we don't know which place the file
4978 +                * was added to.
4979 +                */
4980 +               task_work_run();
4981 +               flush_delayed_fput();
4982 +       }
4983 +       au_sbr_put(sb, file[DST].bindex);
4984 +
4985 +out_src:
4986 +       fput(file[SRC].file);
4987 +       au_sbr_put(sb, file[SRC].bindex);
4988 +out:
4989 +       return err;
4990 +}
4991 +
4992 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4993 +                             struct au_cpup_reg_attr *h_src_attr)
4994 +{
4995 +       int err, rerr;
4996 +       loff_t l;
4997 +       struct path h_path;
4998 +       struct inode *h_src_inode, *h_dst_inode;
4999 +
5000 +       err = 0;
5001 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5002 +       l = i_size_read(h_src_inode);
5003 +       if (cpg->len == -1 || l < cpg->len)
5004 +               cpg->len = l;
5005 +       if (cpg->len) {
5006 +               /* try stopping to update while we are referencing */
5007 +               inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5008 +               au_pin_hdir_unlock(cpg->pin);
5009 +
5010 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5011 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5012 +               h_src_attr->iflags = h_src_inode->i_flags;
5013 +               if (!au_test_nfs(h_src_inode->i_sb))
5014 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
5015 +               else {
5016 +                       inode_unlock(h_src_inode);
5017 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
5018 +                       inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5019 +               }
5020 +               if (unlikely(err)) {
5021 +                       inode_unlock(h_src_inode);
5022 +                       goto out;
5023 +               }
5024 +               h_src_attr->valid = 1;
5025 +               err = au_cp_regular(cpg);
5026 +               inode_unlock(h_src_inode);
5027 +               rerr = au_pin_hdir_relock(cpg->pin);
5028 +               if (!err && rerr)
5029 +                       err = rerr;
5030 +       }
5031 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5032 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5033 +               h_dst_inode = d_inode(h_path.dentry);
5034 +               spin_lock(&h_dst_inode->i_lock);
5035 +               h_dst_inode->i_state |= I_LINKABLE;
5036 +               spin_unlock(&h_dst_inode->i_lock);
5037 +       }
5038 +
5039 +out:
5040 +       return err;
5041 +}
5042 +
5043 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5044 +                             struct inode *h_dir)
5045 +{
5046 +       int err, symlen;
5047 +       mm_segment_t old_fs;
5048 +       union {
5049 +               char *k;
5050 +               char __user *u;
5051 +       } sym;
5052 +       struct inode *h_inode = d_inode(h_src);
5053 +       const struct inode_operations *h_iop = h_inode->i_op;
5054 +
5055 +       err = -ENOSYS;
5056 +       if (unlikely(!h_iop->readlink))
5057 +               goto out;
5058 +
5059 +       err = -ENOMEM;
5060 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5061 +       if (unlikely(!sym.k))
5062 +               goto out;
5063 +
5064 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5065 +       old_fs = get_fs();
5066 +       set_fs(KERNEL_DS);
5067 +       symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
5068 +       err = symlen;
5069 +       set_fs(old_fs);
5070 +
5071 +       if (symlen > 0) {
5072 +               sym.k[symlen] = 0;
5073 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5074 +       }
5075 +       free_page((unsigned long)sym.k);
5076 +
5077 +out:
5078 +       return err;
5079 +}
5080 +
5081 +/*
5082 + * regardless 'acl' option, reset all ACL.
5083 + * All ACL will be copied up later from the original entry on the lower branch.
5084 + */
5085 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5086 +{
5087 +       int err;
5088 +       struct dentry *h_dentry;
5089 +       struct inode *h_inode;
5090 +
5091 +       h_dentry = h_path->dentry;
5092 +       h_inode = d_inode(h_dentry);
5093 +       /* forget_all_cached_acls(h_inode)); */
5094 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5095 +       AuTraceErr(err);
5096 +       if (err == -EOPNOTSUPP)
5097 +               err = 0;
5098 +       if (!err)
5099 +               err = vfsub_acl_chmod(h_inode, mode);
5100 +
5101 +       AuTraceErr(err);
5102 +       return err;
5103 +}
5104 +
5105 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5106 +                         struct inode *h_dir, struct path *h_path)
5107 +{
5108 +       int err;
5109 +       struct inode *dir, *inode;
5110 +
5111 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5112 +       AuTraceErr(err);
5113 +       if (err == -EOPNOTSUPP)
5114 +               err = 0;
5115 +       if (unlikely(err))
5116 +               goto out;
5117 +
5118 +       /*
5119 +        * strange behaviour from the users view,
5120 +        * particularry setattr case
5121 +        */
5122 +       dir = d_inode(dst_parent);
5123 +       if (au_ibstart(dir) == cpg->bdst)
5124 +               au_cpup_attr_nlink(dir, /*force*/1);
5125 +       inode = d_inode(cpg->dentry);
5126 +       au_cpup_attr_nlink(inode, /*force*/1);
5127 +
5128 +out:
5129 +       return err;
5130 +}
5131 +
5132 +static noinline_for_stack
5133 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5134 +              struct au_cpup_reg_attr *h_src_attr)
5135 +{
5136 +       int err;
5137 +       umode_t mode;
5138 +       unsigned int mnt_flags;
5139 +       unsigned char isdir, isreg, force;
5140 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5141 +       struct au_dtime dt;
5142 +       struct path h_path;
5143 +       struct dentry *h_src, *h_dst, *h_parent;
5144 +       struct inode *h_inode, *h_dir;
5145 +       struct super_block *sb;
5146 +
5147 +       /* bsrc branch can be ro/rw. */
5148 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5149 +       h_inode = d_inode(h_src);
5150 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5151 +
5152 +       /* try stopping to be referenced while we are creating */
5153 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5154 +       if (au_ftest_cpup(cpg->flags, RENAME))
5155 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5156 +                                 AUFS_WH_PFX_LEN));
5157 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5158 +       h_dir = d_inode(h_parent);
5159 +       IMustLock(h_dir);
5160 +       AuDebugOn(h_parent != h_dst->d_parent);
5161 +
5162 +       sb = cpg->dentry->d_sb;
5163 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5164 +       if (do_dt) {
5165 +               h_path.dentry = h_parent;
5166 +               au_dtime_store(&dt, dst_parent, &h_path);
5167 +       }
5168 +       h_path.dentry = h_dst;
5169 +
5170 +       isreg = 0;
5171 +       isdir = 0;
5172 +       mode = h_inode->i_mode;
5173 +       switch (mode & S_IFMT) {
5174 +       case S_IFREG:
5175 +               isreg = 1;
5176 +               err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
5177 +                                  /*want_excl*/true);
5178 +               if (!err)
5179 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5180 +               break;
5181 +       case S_IFDIR:
5182 +               isdir = 1;
5183 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5184 +               if (!err)
5185 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5186 +               break;
5187 +       case S_IFLNK:
5188 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5189 +               break;
5190 +       case S_IFCHR:
5191 +       case S_IFBLK:
5192 +               AuDebugOn(!capable(CAP_MKNOD));
5193 +               /*FALLTHROUGH*/
5194 +       case S_IFIFO:
5195 +       case S_IFSOCK:
5196 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5197 +               break;
5198 +       default:
5199 +               AuIOErr("Unknown inode type 0%o\n", mode);
5200 +               err = -EIO;
5201 +       }
5202 +       if (!err)
5203 +               err = au_reset_acl(h_dir, &h_path, mode);
5204 +
5205 +       mnt_flags = au_mntflags(sb);
5206 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5207 +           && !isdir
5208 +           && au_opt_test(mnt_flags, XINO)
5209 +           && (h_inode->i_nlink == 1
5210 +               || (h_inode->i_state & I_LINKABLE))
5211 +           /* todo: unnecessary? */
5212 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5213 +           && cpg->bdst < cpg->bsrc
5214 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5215 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5216 +               /* ignore this error */
5217 +
5218 +       if (!err) {
5219 +               force = 0;
5220 +               if (isreg) {
5221 +                       force = !!cpg->len;
5222 +                       if (cpg->len == -1)
5223 +                               force = !!i_size_read(h_inode);
5224 +               }
5225 +               au_fhsm_wrote(sb, cpg->bdst, force);
5226 +       }
5227 +
5228 +       if (do_dt)
5229 +               au_dtime_revert(&dt);
5230 +       return err;
5231 +}
5232 +
5233 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5234 +{
5235 +       int err;
5236 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5237 +       struct inode *h_dir;
5238 +       aufs_bindex_t bdst;
5239 +
5240 +       dentry = cpg->dentry;
5241 +       bdst = cpg->bdst;
5242 +       h_dentry = au_h_dptr(dentry, bdst);
5243 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5244 +               dget(h_dentry);
5245 +               au_set_h_dptr(dentry, bdst, NULL);
5246 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5247 +               if (!err)
5248 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5249 +               au_set_h_dptr(dentry, bdst, h_dentry);
5250 +       } else {
5251 +               err = 0;
5252 +               parent = dget_parent(dentry);
5253 +               h_parent = au_h_dptr(parent, bdst);
5254 +               dput(parent);
5255 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5256 +               if (IS_ERR(h_path->dentry))
5257 +                       err = PTR_ERR(h_path->dentry);
5258 +       }
5259 +       if (unlikely(err))
5260 +               goto out;
5261 +
5262 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5263 +       h_dir = d_inode(h_parent);
5264 +       IMustLock(h_dir);
5265 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5266 +       /* no delegation since it is just created */
5267 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
5268 +       dput(h_path->dentry);
5269 +
5270 +out:
5271 +       return err;
5272 +}
5273 +
5274 +/*
5275 + * copyup the @dentry from @bsrc to @bdst.
5276 + * the caller must set the both of lower dentries.
5277 + * @len is for truncating when it is -1 copyup the entire file.
5278 + * in link/rename cases, @dst_parent may be different from the real one.
5279 + * basic->bsrc can be larger than basic->bdst.
5280 + */
5281 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5282 +{
5283 +       int err, rerr;
5284 +       aufs_bindex_t old_ibstart;
5285 +       unsigned char isdir, plink;
5286 +       struct dentry *h_src, *h_dst, *h_parent;
5287 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5288 +       struct super_block *sb;
5289 +       struct au_branch *br;
5290 +       /* to reuduce stack size */
5291 +       struct {
5292 +               struct au_dtime dt;
5293 +               struct path h_path;
5294 +               struct au_cpup_reg_attr h_src_attr;
5295 +       } *a;
5296 +
5297 +       err = -ENOMEM;
5298 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5299 +       if (unlikely(!a))
5300 +               goto out;
5301 +       a->h_src_attr.valid = 0;
5302 +
5303 +       sb = cpg->dentry->d_sb;
5304 +       br = au_sbr(sb, cpg->bdst);
5305 +       a->h_path.mnt = au_br_mnt(br);
5306 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5307 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5308 +       h_dir = d_inode(h_parent);
5309 +       IMustLock(h_dir);
5310 +
5311 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5312 +       inode = d_inode(cpg->dentry);
5313 +
5314 +       if (!dst_parent)
5315 +               dst_parent = dget_parent(cpg->dentry);
5316 +       else
5317 +               dget(dst_parent);
5318 +
5319 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5320 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5321 +       if (dst_inode) {
5322 +               if (unlikely(!plink)) {
5323 +                       err = -EIO;
5324 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5325 +                               "but plink is disabled\n",
5326 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5327 +                       goto out_parent;
5328 +               }
5329 +
5330 +               if (dst_inode->i_nlink) {
5331 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5332 +
5333 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5334 +                       err = PTR_ERR(h_src);
5335 +                       if (IS_ERR(h_src))
5336 +                               goto out_parent;
5337 +                       if (unlikely(d_is_negative(h_src))) {
5338 +                               err = -EIO;
5339 +                               AuIOErr("i%lu exists on b%d "
5340 +                                       "but not pseudo-linked\n",
5341 +                                       inode->i_ino, cpg->bdst);
5342 +                               dput(h_src);
5343 +                               goto out_parent;
5344 +                       }
5345 +
5346 +                       if (do_dt) {
5347 +                               a->h_path.dentry = h_parent;
5348 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5349 +                       }
5350 +
5351 +                       a->h_path.dentry = h_dst;
5352 +                       delegated = NULL;
5353 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5354 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5355 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5356 +                       if (do_dt)
5357 +                               au_dtime_revert(&a->dt);
5358 +                       if (unlikely(err == -EWOULDBLOCK)) {
5359 +                               pr_warn("cannot retry for NFSv4 delegation"
5360 +                                       " for an internal link\n");
5361 +                               iput(delegated);
5362 +                       }
5363 +                       dput(h_src);
5364 +                       goto out_parent;
5365 +               } else
5366 +                       /* todo: cpup_wh_file? */
5367 +                       /* udba work */
5368 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5369 +       }
5370 +
5371 +       isdir = S_ISDIR(inode->i_mode);
5372 +       old_ibstart = au_ibstart(inode);
5373 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5374 +       if (unlikely(err))
5375 +               goto out_rev;
5376 +       dst_inode = d_inode(h_dst);
5377 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5378 +       /* todo: necessary? */
5379 +       /* au_pin_hdir_unlock(cpg->pin); */
5380 +
5381 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5382 +       if (unlikely(err)) {
5383 +               /* todo: necessary? */
5384 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5385 +               inode_unlock(dst_inode);
5386 +               goto out_rev;
5387 +       }
5388 +
5389 +       if (cpg->bdst < old_ibstart) {
5390 +               if (S_ISREG(inode->i_mode)) {
5391 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5392 +                       if (unlikely(err)) {
5393 +                               /* ignore an error */
5394 +                               /* au_pin_hdir_relock(cpg->pin); */
5395 +                               inode_unlock(dst_inode);
5396 +                               goto out_rev;
5397 +                       }
5398 +               }
5399 +               au_set_ibstart(inode, cpg->bdst);
5400 +       } else
5401 +               au_set_ibend(inode, cpg->bdst);
5402 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5403 +                     au_hi_flags(inode, isdir));
5404 +
5405 +       /* todo: necessary? */
5406 +       /* err = au_pin_hdir_relock(cpg->pin); */
5407 +       inode_unlock(dst_inode);
5408 +       if (unlikely(err))
5409 +               goto out_rev;
5410 +
5411 +       src_inode = d_inode(h_src);
5412 +       if (!isdir
5413 +           && (src_inode->i_nlink > 1
5414 +               || src_inode->i_state & I_LINKABLE)
5415 +           && plink)
5416 +               au_plink_append(inode, cpg->bdst, h_dst);
5417 +
5418 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5419 +               a->h_path.dentry = h_dst;
5420 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5421 +       }
5422 +       if (!err)
5423 +               goto out_parent; /* success */
5424 +
5425 +       /* revert */
5426 +out_rev:
5427 +       a->h_path.dentry = h_parent;
5428 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5429 +       a->h_path.dentry = h_dst;
5430 +       rerr = 0;
5431 +       if (d_is_positive(h_dst)) {
5432 +               if (!isdir) {
5433 +                       /* no delegation since it is just created */
5434 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5435 +                                           /*delegated*/NULL, /*force*/0);
5436 +               } else
5437 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5438 +       }
5439 +       au_dtime_revert(&a->dt);
5440 +       if (rerr) {
5441 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5442 +               err = -EIO;
5443 +       }
5444 +out_parent:
5445 +       dput(dst_parent);
5446 +       kfree(a);
5447 +out:
5448 +       return err;
5449 +}
5450 +
5451 +#if 0 /* reserved */
5452 +struct au_cpup_single_args {
5453 +       int *errp;
5454 +       struct au_cp_generic *cpg;
5455 +       struct dentry *dst_parent;
5456 +};
5457 +
5458 +static void au_call_cpup_single(void *args)
5459 +{
5460 +       struct au_cpup_single_args *a = args;
5461 +
5462 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5463 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5464 +       au_pin_hdir_release(a->cpg->pin);
5465 +}
5466 +#endif
5467 +
5468 +/*
5469 + * prevent SIGXFSZ in copy-up.
5470 + * testing CAP_MKNOD is for generic fs,
5471 + * but CAP_FSETID is for xfs only, currently.
5472 + */
5473 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5474 +{
5475 +       int do_sio;
5476 +       struct super_block *sb;
5477 +       struct inode *h_dir;
5478 +
5479 +       do_sio = 0;
5480 +       sb = au_pinned_parent(pin)->d_sb;
5481 +       if (!au_wkq_test()
5482 +           && (!au_sbi(sb)->si_plink_maint_pid
5483 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5484 +               switch (mode & S_IFMT) {
5485 +               case S_IFREG:
5486 +                       /* no condition about RLIMIT_FSIZE and the file size */
5487 +                       do_sio = 1;
5488 +                       break;
5489 +               case S_IFCHR:
5490 +               case S_IFBLK:
5491 +                       do_sio = !capable(CAP_MKNOD);
5492 +                       break;
5493 +               }
5494 +               if (!do_sio)
5495 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5496 +                                 && !capable(CAP_FSETID));
5497 +               /* this workaround may be removed in the future */
5498 +               if (!do_sio) {
5499 +                       h_dir = au_pinned_h_dir(pin);
5500 +                       do_sio = h_dir->i_mode & S_ISVTX;
5501 +               }
5502 +       }
5503 +
5504 +       return do_sio;
5505 +}
5506 +
5507 +#if 0 /* reserved */
5508 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5509 +{
5510 +       int err, wkq_err;
5511 +       struct dentry *h_dentry;
5512 +
5513 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5514 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5515 +               err = au_cpup_single(cpg, dst_parent);
5516 +       else {
5517 +               struct au_cpup_single_args args = {
5518 +                       .errp           = &err,
5519 +                       .cpg            = cpg,
5520 +                       .dst_parent     = dst_parent
5521 +               };
5522 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5523 +               if (unlikely(wkq_err))
5524 +                       err = wkq_err;
5525 +       }
5526 +
5527 +       return err;
5528 +}
5529 +#endif
5530 +
5531 +/*
5532 + * copyup the @dentry from the first active lower branch to @bdst,
5533 + * using au_cpup_single().
5534 + */
5535 +static int au_cpup_simple(struct au_cp_generic *cpg)
5536 +{
5537 +       int err;
5538 +       unsigned int flags_orig;
5539 +       struct dentry *dentry;
5540 +
5541 +       AuDebugOn(cpg->bsrc < 0);
5542 +
5543 +       dentry = cpg->dentry;
5544 +       DiMustWriteLock(dentry);
5545 +
5546 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5547 +       if (!err) {
5548 +               flags_orig = cpg->flags;
5549 +               au_fset_cpup(cpg->flags, RENAME);
5550 +               err = au_cpup_single(cpg, NULL);
5551 +               cpg->flags = flags_orig;
5552 +               if (!err)
5553 +                       return 0; /* success */
5554 +
5555 +               /* revert */
5556 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5557 +               au_set_dbstart(dentry, cpg->bsrc);
5558 +       }
5559 +
5560 +       return err;
5561 +}
5562 +
5563 +struct au_cpup_simple_args {
5564 +       int *errp;
5565 +       struct au_cp_generic *cpg;
5566 +};
5567 +
5568 +static void au_call_cpup_simple(void *args)
5569 +{
5570 +       struct au_cpup_simple_args *a = args;
5571 +
5572 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5573 +       *a->errp = au_cpup_simple(a->cpg);
5574 +       au_pin_hdir_release(a->cpg->pin);
5575 +}
5576 +
5577 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5578 +{
5579 +       int err, wkq_err;
5580 +       struct dentry *dentry, *parent;
5581 +       struct file *h_file;
5582 +       struct inode *h_dir;
5583 +
5584 +       dentry = cpg->dentry;
5585 +       h_file = NULL;
5586 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5587 +               AuDebugOn(cpg->bsrc < 0);
5588 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5589 +               err = PTR_ERR(h_file);
5590 +               if (IS_ERR(h_file))
5591 +                       goto out;
5592 +       }
5593 +
5594 +       parent = dget_parent(dentry);
5595 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5596 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5597 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5598 +               err = au_cpup_simple(cpg);
5599 +       else {
5600 +               struct au_cpup_simple_args args = {
5601 +                       .errp           = &err,
5602 +                       .cpg            = cpg
5603 +               };
5604 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5605 +               if (unlikely(wkq_err))
5606 +                       err = wkq_err;
5607 +       }
5608 +
5609 +       dput(parent);
5610 +       if (h_file)
5611 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5612 +
5613 +out:
5614 +       return err;
5615 +}
5616 +
5617 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5618 +{
5619 +       aufs_bindex_t bsrc, bend;
5620 +       struct dentry *dentry, *h_dentry;
5621 +
5622 +       if (cpg->bsrc < 0) {
5623 +               dentry = cpg->dentry;
5624 +               bend = au_dbend(dentry);
5625 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
5626 +                       h_dentry = au_h_dptr(dentry, bsrc);
5627 +                       if (h_dentry) {
5628 +                               AuDebugOn(d_is_negative(h_dentry));
5629 +                               break;
5630 +                       }
5631 +               }
5632 +               AuDebugOn(bsrc > bend);
5633 +               cpg->bsrc = bsrc;
5634 +       }
5635 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5636 +       return au_do_sio_cpup_simple(cpg);
5637 +}
5638 +
5639 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5640 +{
5641 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5642 +       return au_do_sio_cpup_simple(cpg);
5643 +}
5644 +
5645 +/* ---------------------------------------------------------------------- */
5646 +
5647 +/*
5648 + * copyup the deleted file for writing.
5649 + */
5650 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5651 +                        struct file *file)
5652 +{
5653 +       int err;
5654 +       unsigned int flags_orig;
5655 +       aufs_bindex_t bsrc_orig;
5656 +       struct dentry *h_d_dst, *h_d_start;
5657 +       struct au_dinfo *dinfo;
5658 +       struct au_hdentry *hdp;
5659 +
5660 +       dinfo = au_di(cpg->dentry);
5661 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5662 +
5663 +       bsrc_orig = cpg->bsrc;
5664 +       cpg->bsrc = dinfo->di_bstart;
5665 +       hdp = dinfo->di_hdentry;
5666 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
5667 +       dinfo->di_bstart = cpg->bdst;
5668 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
5669 +       h_d_start = NULL;
5670 +       if (file) {
5671 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
5672 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
5673 +       }
5674 +       flags_orig = cpg->flags;
5675 +       cpg->flags = !AuCpup_DTIME;
5676 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5677 +       cpg->flags = flags_orig;
5678 +       if (file) {
5679 +               if (!err)
5680 +                       err = au_reopen_nondir(file);
5681 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
5682 +       }
5683 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
5684 +       dinfo->di_bstart = cpg->bsrc;
5685 +       cpg->bsrc = bsrc_orig;
5686 +
5687 +       return err;
5688 +}
5689 +
5690 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5691 +{
5692 +       int err;
5693 +       aufs_bindex_t bdst;
5694 +       struct au_dtime dt;
5695 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5696 +       struct au_branch *br;
5697 +       struct path h_path;
5698 +
5699 +       dentry = cpg->dentry;
5700 +       bdst = cpg->bdst;
5701 +       br = au_sbr(dentry->d_sb, bdst);
5702 +       parent = dget_parent(dentry);
5703 +       h_parent = au_h_dptr(parent, bdst);
5704 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5705 +       err = PTR_ERR(wh_dentry);
5706 +       if (IS_ERR(wh_dentry))
5707 +               goto out;
5708 +
5709 +       h_path.dentry = h_parent;
5710 +       h_path.mnt = au_br_mnt(br);
5711 +       au_dtime_store(&dt, parent, &h_path);
5712 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5713 +       if (unlikely(err))
5714 +               goto out_wh;
5715 +
5716 +       dget(wh_dentry);
5717 +       h_path.dentry = wh_dentry;
5718 +       if (!d_is_dir(wh_dentry)) {
5719 +               /* no delegation since it is just created */
5720 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5721 +                                  /*delegated*/NULL, /*force*/0);
5722 +       } else
5723 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5724 +       if (unlikely(err)) {
5725 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5726 +                       wh_dentry, err);
5727 +               err = -EIO;
5728 +       }
5729 +       au_dtime_revert(&dt);
5730 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5731 +
5732 +out_wh:
5733 +       dput(wh_dentry);
5734 +out:
5735 +       dput(parent);
5736 +       return err;
5737 +}
5738 +
5739 +struct au_cpup_wh_args {
5740 +       int *errp;
5741 +       struct au_cp_generic *cpg;
5742 +       struct file *file;
5743 +};
5744 +
5745 +static void au_call_cpup_wh(void *args)
5746 +{
5747 +       struct au_cpup_wh_args *a = args;
5748 +
5749 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5750 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5751 +       au_pin_hdir_release(a->cpg->pin);
5752 +}
5753 +
5754 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5755 +{
5756 +       int err, wkq_err;
5757 +       aufs_bindex_t bdst;
5758 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5759 +       struct inode *dir, *h_dir, *h_tmpdir;
5760 +       struct au_wbr *wbr;
5761 +       struct au_pin wh_pin, *pin_orig;
5762 +
5763 +       dentry = cpg->dentry;
5764 +       bdst = cpg->bdst;
5765 +       parent = dget_parent(dentry);
5766 +       dir = d_inode(parent);
5767 +       h_orph = NULL;
5768 +       h_parent = NULL;
5769 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5770 +       h_tmpdir = h_dir;
5771 +       pin_orig = NULL;
5772 +       if (!h_dir->i_nlink) {
5773 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5774 +               h_orph = wbr->wbr_orph;
5775 +
5776 +               h_parent = dget(au_h_dptr(parent, bdst));
5777 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5778 +               h_tmpdir = d_inode(h_orph);
5779 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5780 +
5781 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
5782 +               /* todo: au_h_open_pre()? */
5783 +
5784 +               pin_orig = cpg->pin;
5785 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5786 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5787 +               cpg->pin = &wh_pin;
5788 +       }
5789 +
5790 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5791 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5792 +               err = au_cpup_wh(cpg, file);
5793 +       else {
5794 +               struct au_cpup_wh_args args = {
5795 +                       .errp   = &err,
5796 +                       .cpg    = cpg,
5797 +                       .file   = file
5798 +               };
5799 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5800 +               if (unlikely(wkq_err))
5801 +                       err = wkq_err;
5802 +       }
5803 +
5804 +       if (h_orph) {
5805 +               inode_unlock(h_tmpdir);
5806 +               /* todo: au_h_open_post()? */
5807 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5808 +               au_set_h_dptr(parent, bdst, h_parent);
5809 +               AuDebugOn(!pin_orig);
5810 +               cpg->pin = pin_orig;
5811 +       }
5812 +       iput(h_dir);
5813 +       dput(parent);
5814 +
5815 +       return err;
5816 +}
5817 +
5818 +/* ---------------------------------------------------------------------- */
5819 +
5820 +/*
5821 + * generic routine for both of copy-up and copy-down.
5822 + */
5823 +/* cf. revalidate function in file.c */
5824 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5825 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5826 +                        struct au_pin *pin,
5827 +                        struct dentry *h_parent, void *arg),
5828 +              void *arg)
5829 +{
5830 +       int err;
5831 +       struct au_pin pin;
5832 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
5833 +
5834 +       err = 0;
5835 +       parent = dget_parent(dentry);
5836 +       if (IS_ROOT(parent))
5837 +               goto out;
5838 +
5839 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5840 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5841 +
5842 +       /* do not use au_dpage */
5843 +       real_parent = parent;
5844 +       while (1) {
5845 +               dput(parent);
5846 +               parent = dget_parent(dentry);
5847 +               h_parent = au_h_dptr(parent, bdst);
5848 +               if (h_parent)
5849 +                       goto out; /* success */
5850 +
5851 +               /* find top dir which is necessary to cpup */
5852 +               do {
5853 +                       d = parent;
5854 +                       dput(parent);
5855 +                       parent = dget_parent(d);
5856 +                       di_read_lock_parent3(parent, !AuLock_IR);
5857 +                       h_parent = au_h_dptr(parent, bdst);
5858 +                       di_read_unlock(parent, !AuLock_IR);
5859 +               } while (!h_parent);
5860 +
5861 +               if (d != real_parent)
5862 +                       di_write_lock_child3(d);
5863 +
5864 +               /* somebody else might create while we were sleeping */
5865 +               h_dentry = au_h_dptr(d, bdst);
5866 +               if (!h_dentry || d_is_negative(h_dentry)) {
5867 +                       if (h_dentry)
5868 +                               au_update_dbstart(d);
5869 +
5870 +                       au_pin_set_dentry(&pin, d);
5871 +                       err = au_do_pin(&pin);
5872 +                       if (!err) {
5873 +                               err = cp(d, bdst, &pin, h_parent, arg);
5874 +                               au_unpin(&pin);
5875 +                       }
5876 +               }
5877 +
5878 +               if (d != real_parent)
5879 +                       di_write_unlock(d);
5880 +               if (unlikely(err))
5881 +                       break;
5882 +       }
5883 +
5884 +out:
5885 +       dput(parent);
5886 +       return err;
5887 +}
5888 +
5889 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5890 +                      struct au_pin *pin,
5891 +                      struct dentry *h_parent __maybe_unused,
5892 +                      void *arg __maybe_unused)
5893 +{
5894 +       struct au_cp_generic cpg = {
5895 +               .dentry = dentry,
5896 +               .bdst   = bdst,
5897 +               .bsrc   = -1,
5898 +               .len    = 0,
5899 +               .pin    = pin,
5900 +               .flags  = AuCpup_DTIME
5901 +       };
5902 +       return au_sio_cpup_simple(&cpg);
5903 +}
5904 +
5905 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5906 +{
5907 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5908 +}
5909 +
5910 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5911 +{
5912 +       int err;
5913 +       struct dentry *parent;
5914 +       struct inode *dir;
5915 +
5916 +       parent = dget_parent(dentry);
5917 +       dir = d_inode(parent);
5918 +       err = 0;
5919 +       if (au_h_iptr(dir, bdst))
5920 +               goto out;
5921 +
5922 +       di_read_unlock(parent, AuLock_IR);
5923 +       di_write_lock_parent(parent);
5924 +       /* someone else might change our inode while we were sleeping */
5925 +       if (!au_h_iptr(dir, bdst))
5926 +               err = au_cpup_dirs(dentry, bdst);
5927 +       di_downgrade_lock(parent, AuLock_IR);
5928 +
5929 +out:
5930 +       dput(parent);
5931 +       return err;
5932 +}
5933 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5934 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5935 +++ linux/fs/aufs/cpup.h        2016-04-24 18:32:51.390353525 +0200
5936 @@ -0,0 +1,94 @@
5937 +/*
5938 + * Copyright (C) 2005-2016 Junjiro R. Okajima
5939 + *
5940 + * This program, aufs is free software; you can redistribute it and/or modify
5941 + * it under the terms of the GNU General Public License as published by
5942 + * the Free Software Foundation; either version 2 of the License, or
5943 + * (at your option) any later version.
5944 + *
5945 + * This program is distributed in the hope that it will be useful,
5946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5948 + * GNU General Public License for more details.
5949 + *
5950 + * You should have received a copy of the GNU General Public License
5951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5952 + */
5953 +
5954 +/*
5955 + * copy-up/down functions
5956 + */
5957 +
5958 +#ifndef __AUFS_CPUP_H__
5959 +#define __AUFS_CPUP_H__
5960 +
5961 +#ifdef __KERNEL__
5962 +
5963 +#include <linux/path.h>
5964 +
5965 +struct inode;
5966 +struct file;
5967 +struct au_pin;
5968 +
5969 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5970 +void au_cpup_attr_timesizes(struct inode *inode);
5971 +void au_cpup_attr_nlink(struct inode *inode, int force);
5972 +void au_cpup_attr_changeable(struct inode *inode);
5973 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5974 +void au_cpup_attr_all(struct inode *inode, int force);
5975 +
5976 +/* ---------------------------------------------------------------------- */
5977 +
5978 +struct au_cp_generic {
5979 +       struct dentry   *dentry;
5980 +       aufs_bindex_t   bdst, bsrc;
5981 +       loff_t          len;
5982 +       struct au_pin   *pin;
5983 +       unsigned int    flags;
5984 +};
5985 +
5986 +/* cpup flags */
5987 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5988 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5989 +                                                  for link(2) */
5990 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5991 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5992 +                                                  cpup */
5993 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5994 +                                                  existing entry */
5995 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5996 +                                                  the branch is marked as RO */
5997 +
5998 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5999 +#define au_fset_cpup(flags, name) \
6000 +       do { (flags) |= AuCpup_##name; } while (0)
6001 +#define au_fclr_cpup(flags, name) \
6002 +       do { (flags) &= ~AuCpup_##name; } while (0)
6003 +
6004 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6005 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6006 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6007 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6008 +
6009 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6010 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6011 +                        struct au_pin *pin,
6012 +                        struct dentry *h_parent, void *arg),
6013 +              void *arg);
6014 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6015 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6016 +
6017 +/* ---------------------------------------------------------------------- */
6018 +
6019 +/* keep timestamps when copyup */
6020 +struct au_dtime {
6021 +       struct dentry *dt_dentry;
6022 +       struct path dt_h_path;
6023 +       struct timespec dt_atime, dt_mtime;
6024 +};
6025 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6026 +                   struct path *h_path);
6027 +void au_dtime_revert(struct au_dtime *dt);
6028 +
6029 +#endif /* __KERNEL__ */
6030 +#endif /* __AUFS_CPUP_H__ */
6031 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6032 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6033 +++ linux/fs/aufs/dbgaufs.c     2016-04-24 18:32:51.390353525 +0200
6034 @@ -0,0 +1,432 @@
6035 +/*
6036 + * Copyright (C) 2005-2016 Junjiro R. Okajima
6037 + *
6038 + * This program, aufs is free software; you can redistribute it and/or modify
6039 + * it under the terms of the GNU General Public License as published by
6040 + * the Free Software Foundation; either version 2 of the License, or
6041 + * (at your option) any later version.
6042 + *
6043 + * This program is distributed in the hope that it will be useful,
6044 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6045 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6046 + * GNU General Public License for more details.
6047 + *
6048 + * You should have received a copy of the GNU General Public License
6049 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6050 + */
6051 +
6052 +/*
6053 + * debugfs interface
6054 + */
6055 +
6056 +#include <linux/debugfs.h>
6057 +#include "aufs.h"
6058 +
6059 +#ifndef CONFIG_SYSFS
6060 +#error DEBUG_FS depends upon SYSFS
6061 +#endif
6062 +
6063 +static struct dentry *dbgaufs;
6064 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6065 +
6066 +/* 20 is max digits length of ulong 64 */
6067 +struct dbgaufs_arg {
6068 +       int n;
6069 +       char a[20 * 4];
6070 +};
6071 +
6072 +/*
6073 + * common function for all XINO files
6074 + */
6075 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6076 +                             struct file *file)
6077 +{
6078 +       kfree(file->private_data);
6079 +       return 0;
6080 +}
6081 +
6082 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6083 +{
6084 +       int err;
6085 +       struct kstat st;
6086 +       struct dbgaufs_arg *p;
6087 +
6088 +       err = -ENOMEM;
6089 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6090 +       if (unlikely(!p))
6091 +               goto out;
6092 +
6093 +       err = 0;
6094 +       p->n = 0;
6095 +       file->private_data = p;
6096 +       if (!xf)
6097 +               goto out;
6098 +
6099 +       err = vfs_getattr(&xf->f_path, &st);
6100 +       if (!err) {
6101 +               if (do_fcnt)
6102 +                       p->n = snprintf
6103 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
6104 +                                (long)file_count(xf), st.blocks, st.blksize,
6105 +                                (long long)st.size);
6106 +               else
6107 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
6108 +                                       st.blocks, st.blksize,
6109 +                                       (long long)st.size);
6110 +               AuDebugOn(p->n >= sizeof(p->a));
6111 +       } else {
6112 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6113 +               err = 0;
6114 +       }
6115 +
6116 +out:
6117 +       return err;
6118 +
6119 +}
6120 +
6121 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6122 +                              size_t count, loff_t *ppos)
6123 +{
6124 +       struct dbgaufs_arg *p;
6125 +
6126 +       p = file->private_data;
6127 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6128 +}
6129 +
6130 +/* ---------------------------------------------------------------------- */
6131 +
6132 +struct dbgaufs_plink_arg {
6133 +       int n;
6134 +       char a[];
6135 +};
6136 +
6137 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6138 +                                struct file *file)
6139 +{
6140 +       free_page((unsigned long)file->private_data);
6141 +       return 0;
6142 +}
6143 +
6144 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6145 +{
6146 +       int err, i, limit;
6147 +       unsigned long n, sum;
6148 +       struct dbgaufs_plink_arg *p;
6149 +       struct au_sbinfo *sbinfo;
6150 +       struct super_block *sb;
6151 +       struct au_sphlhead *sphl;
6152 +
6153 +       err = -ENOMEM;
6154 +       p = (void *)get_zeroed_page(GFP_NOFS);
6155 +       if (unlikely(!p))
6156 +               goto out;
6157 +
6158 +       err = -EFBIG;
6159 +       sbinfo = inode->i_private;
6160 +       sb = sbinfo->si_sb;
6161 +       si_noflush_read_lock(sb);
6162 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6163 +               limit = PAGE_SIZE - sizeof(p->n);
6164 +
6165 +               /* the number of buckets */
6166 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6167 +               p->n += n;
6168 +               limit -= n;
6169 +
6170 +               sum = 0;
6171 +               for (i = 0, sphl = sbinfo->si_plink;
6172 +                    i < AuPlink_NHASH;
6173 +                    i++, sphl++) {
6174 +                       n = au_sphl_count(sphl);
6175 +                       sum += n;
6176 +
6177 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6178 +                       p->n += n;
6179 +                       limit -= n;
6180 +                       if (unlikely(limit <= 0))
6181 +                               goto out_free;
6182 +               }
6183 +               p->a[p->n - 1] = '\n';
6184 +
6185 +               /* the sum of plinks */
6186 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6187 +               p->n += n;
6188 +               limit -= n;
6189 +               if (unlikely(limit <= 0))
6190 +                       goto out_free;
6191 +       } else {
6192 +#define str "1\n0\n0\n"
6193 +               p->n = sizeof(str) - 1;
6194 +               strcpy(p->a, str);
6195 +#undef str
6196 +       }
6197 +       si_read_unlock(sb);
6198 +
6199 +       err = 0;
6200 +       file->private_data = p;
6201 +       goto out; /* success */
6202 +
6203 +out_free:
6204 +       free_page((unsigned long)p);
6205 +out:
6206 +       return err;
6207 +}
6208 +
6209 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6210 +                                 size_t count, loff_t *ppos)
6211 +{
6212 +       struct dbgaufs_plink_arg *p;
6213 +
6214 +       p = file->private_data;
6215 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6216 +}
6217 +
6218 +static const struct file_operations dbgaufs_plink_fop = {
6219 +       .owner          = THIS_MODULE,
6220 +       .open           = dbgaufs_plink_open,
6221 +       .release        = dbgaufs_plink_release,
6222 +       .read           = dbgaufs_plink_read
6223 +};
6224 +
6225 +/* ---------------------------------------------------------------------- */
6226 +
6227 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6228 +{
6229 +       int err;
6230 +       struct au_sbinfo *sbinfo;
6231 +       struct super_block *sb;
6232 +
6233 +       sbinfo = inode->i_private;
6234 +       sb = sbinfo->si_sb;
6235 +       si_noflush_read_lock(sb);
6236 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6237 +       si_read_unlock(sb);
6238 +       return err;
6239 +}
6240 +
6241 +static const struct file_operations dbgaufs_xib_fop = {
6242 +       .owner          = THIS_MODULE,
6243 +       .open           = dbgaufs_xib_open,
6244 +       .release        = dbgaufs_xi_release,
6245 +       .read           = dbgaufs_xi_read
6246 +};
6247 +
6248 +/* ---------------------------------------------------------------------- */
6249 +
6250 +#define DbgaufsXi_PREFIX "xi"
6251 +
6252 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6253 +{
6254 +       int err;
6255 +       long l;
6256 +       struct au_sbinfo *sbinfo;
6257 +       struct super_block *sb;
6258 +       struct file *xf;
6259 +       struct qstr *name;
6260 +
6261 +       err = -ENOENT;
6262 +       xf = NULL;
6263 +       name = &file->f_path.dentry->d_name;
6264 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6265 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6266 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6267 +               goto out;
6268 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6269 +       if (unlikely(err))
6270 +               goto out;
6271 +
6272 +       sbinfo = inode->i_private;
6273 +       sb = sbinfo->si_sb;
6274 +       si_noflush_read_lock(sb);
6275 +       if (l <= au_sbend(sb)) {
6276 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6277 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6278 +       } else
6279 +               err = -ENOENT;
6280 +       si_read_unlock(sb);
6281 +
6282 +out:
6283 +       return err;
6284 +}
6285 +
6286 +static const struct file_operations dbgaufs_xino_fop = {
6287 +       .owner          = THIS_MODULE,
6288 +       .open           = dbgaufs_xino_open,
6289 +       .release        = dbgaufs_xi_release,
6290 +       .read           = dbgaufs_xi_read
6291 +};
6292 +
6293 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6294 +{
6295 +       aufs_bindex_t bend;
6296 +       struct au_branch *br;
6297 +       struct au_xino_file *xi;
6298 +
6299 +       if (!au_sbi(sb)->si_dbgaufs)
6300 +               return;
6301 +
6302 +       bend = au_sbend(sb);
6303 +       for (; bindex <= bend; bindex++) {
6304 +               br = au_sbr(sb, bindex);
6305 +               xi = &br->br_xino;
6306 +               debugfs_remove(xi->xi_dbgaufs);
6307 +               xi->xi_dbgaufs = NULL;
6308 +       }
6309 +}
6310 +
6311 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6312 +{
6313 +       struct au_sbinfo *sbinfo;
6314 +       struct dentry *parent;
6315 +       struct au_branch *br;
6316 +       struct au_xino_file *xi;
6317 +       aufs_bindex_t bend;
6318 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6319 +
6320 +       sbinfo = au_sbi(sb);
6321 +       parent = sbinfo->si_dbgaufs;
6322 +       if (!parent)
6323 +               return;
6324 +
6325 +       bend = au_sbend(sb);
6326 +       for (; bindex <= bend; bindex++) {
6327 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6328 +               br = au_sbr(sb, bindex);
6329 +               xi = &br->br_xino;
6330 +               AuDebugOn(xi->xi_dbgaufs);
6331 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6332 +                                                    sbinfo, &dbgaufs_xino_fop);
6333 +               /* ignore an error */
6334 +               if (unlikely(!xi->xi_dbgaufs))
6335 +                       AuWarn1("failed %s under debugfs\n", name);
6336 +       }
6337 +}
6338 +
6339 +/* ---------------------------------------------------------------------- */
6340 +
6341 +#ifdef CONFIG_AUFS_EXPORT
6342 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6343 +{
6344 +       int err;
6345 +       struct au_sbinfo *sbinfo;
6346 +       struct super_block *sb;
6347 +
6348 +       sbinfo = inode->i_private;
6349 +       sb = sbinfo->si_sb;
6350 +       si_noflush_read_lock(sb);
6351 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6352 +       si_read_unlock(sb);
6353 +       return err;
6354 +}
6355 +
6356 +static const struct file_operations dbgaufs_xigen_fop = {
6357 +       .owner          = THIS_MODULE,
6358 +       .open           = dbgaufs_xigen_open,
6359 +       .release        = dbgaufs_xi_release,
6360 +       .read           = dbgaufs_xi_read
6361 +};
6362 +
6363 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6364 +{
6365 +       int err;
6366 +
6367 +       /*
6368 +        * This function is a dynamic '__init' function actually,
6369 +        * so the tiny check for si_rwsem is unnecessary.
6370 +        */
6371 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6372 +
6373 +       err = -EIO;
6374 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6375 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6376 +                &dbgaufs_xigen_fop);
6377 +       if (sbinfo->si_dbgaufs_xigen)
6378 +               err = 0;
6379 +
6380 +       return err;
6381 +}
6382 +#else
6383 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6384 +{
6385 +       return 0;
6386 +}
6387 +#endif /* CONFIG_AUFS_EXPORT */
6388 +
6389 +/* ---------------------------------------------------------------------- */
6390 +
6391 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6392 +{
6393 +       /*
6394 +        * This function is a dynamic '__fin' function actually,
6395 +        * so the tiny check for si_rwsem is unnecessary.
6396 +        */
6397 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6398 +
6399 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6400 +       sbinfo->si_dbgaufs = NULL;
6401 +       kobject_put(&sbinfo->si_kobj);
6402 +}
6403 +
6404 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6405 +{
6406 +       int err;
6407 +       char name[SysaufsSiNameLen];
6408 +
6409 +       /*
6410 +        * This function is a dynamic '__init' function actually,
6411 +        * so the tiny check for si_rwsem is unnecessary.
6412 +        */
6413 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6414 +
6415 +       err = -ENOENT;
6416 +       if (!dbgaufs) {
6417 +               AuErr1("/debug/aufs is uninitialized\n");
6418 +               goto out;
6419 +       }
6420 +
6421 +       err = -EIO;
6422 +       sysaufs_name(sbinfo, name);
6423 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6424 +       if (unlikely(!sbinfo->si_dbgaufs))
6425 +               goto out;
6426 +       kobject_get(&sbinfo->si_kobj);
6427 +
6428 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6429 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6430 +                &dbgaufs_xib_fop);
6431 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6432 +               goto out_dir;
6433 +
6434 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6435 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6436 +                &dbgaufs_plink_fop);
6437 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6438 +               goto out_dir;
6439 +
6440 +       err = dbgaufs_xigen_init(sbinfo);
6441 +       if (!err)
6442 +               goto out; /* success */
6443 +
6444 +out_dir:
6445 +       dbgaufs_si_fin(sbinfo);
6446 +out:
6447 +       return err;
6448 +}
6449 +
6450 +/* ---------------------------------------------------------------------- */
6451 +
6452 +void dbgaufs_fin(void)
6453 +{
6454 +       debugfs_remove(dbgaufs);
6455 +}
6456 +
6457 +int __init dbgaufs_init(void)
6458 +{
6459 +       int err;
6460 +
6461 +       err = -EIO;
6462 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6463 +       if (dbgaufs)
6464 +               err = 0;
6465 +       return err;
6466 +}
6467 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6468 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6469 +++ linux/fs/aufs/dbgaufs.h     2016-04-24 18:32:51.390353525 +0200
6470 @@ -0,0 +1,48 @@
6471 +/*
6472 + * Copyright (C) 2005-2016 Junjiro R. Okajima
6473 + *
6474 + * This program, aufs is free software; you can redistribute it and/or modify
6475 + * it under the terms of the GNU General Public License as published by
6476 + * the Free Software Foundation; either version 2 of the License, or
6477 + * (at your option) any later version.
6478 + *
6479 + * This program is distributed in the hope that it will be useful,
6480 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6481 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6482 + * GNU General Public License for more details.
6483 + *
6484 + * You should have received a copy of the GNU General Public License
6485 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6486 + */
6487 +
6488 +/*
6489 + * debugfs interface
6490 + */
6491 +
6492 +#ifndef __DBGAUFS_H__
6493 +#define __DBGAUFS_H__
6494 +
6495 +#ifdef __KERNEL__
6496 +
6497 +struct super_block;
6498 +struct au_sbinfo;
6499 +
6500 +#ifdef CONFIG_DEBUG_FS
6501 +/* dbgaufs.c */
6502 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6503 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6504 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6505 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6506 +void dbgaufs_fin(void);
6507 +int __init dbgaufs_init(void);
6508 +#else
6509 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6510 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6511 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6512 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6513 +AuStubVoid(dbgaufs_fin, void)
6514 +AuStubInt0(__init dbgaufs_init, void)
6515 +#endif /* CONFIG_DEBUG_FS */
6516 +
6517 +#endif /* __KERNEL__ */
6518 +#endif /* __DBGAUFS_H__ */
6519 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6520 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6521 +++ linux/fs/aufs/dcsub.c       2016-04-24 18:32:51.390353525 +0200
6522 @@ -0,0 +1,224 @@
6523 +/*
6524 + * Copyright (C) 2005-2016 Junjiro R. Okajima
6525 + *
6526 + * This program, aufs is free software; you can redistribute it and/or modify
6527 + * it under the terms of the GNU General Public License as published by
6528 + * the Free Software Foundation; either version 2 of the License, or
6529 + * (at your option) any later version.
6530 + *
6531 + * This program is distributed in the hope that it will be useful,
6532 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6533 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6534 + * GNU General Public License for more details.
6535 + *
6536 + * You should have received a copy of the GNU General Public License
6537 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6538 + */
6539 +
6540 +/*
6541 + * sub-routines for dentry cache
6542 + */
6543 +
6544 +#include "aufs.h"
6545 +
6546 +static void au_dpage_free(struct au_dpage *dpage)
6547 +{
6548 +       int i;
6549 +       struct dentry **p;
6550 +
6551 +       p = dpage->dentries;
6552 +       for (i = 0; i < dpage->ndentry; i++)
6553 +               dput(*p++);
6554 +       free_page((unsigned long)dpage->dentries);
6555 +}
6556 +
6557 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6558 +{
6559 +       int err;
6560 +       void *p;
6561 +
6562 +       err = -ENOMEM;
6563 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6564 +       if (unlikely(!dpages->dpages))
6565 +               goto out;
6566 +
6567 +       p = (void *)__get_free_page(gfp);
6568 +       if (unlikely(!p))
6569 +               goto out_dpages;
6570 +
6571 +       dpages->dpages[0].ndentry = 0;
6572 +       dpages->dpages[0].dentries = p;
6573 +       dpages->ndpage = 1;
6574 +       return 0; /* success */
6575 +
6576 +out_dpages:
6577 +       kfree(dpages->dpages);
6578 +out:
6579 +       return err;
6580 +}
6581 +
6582 +void au_dpages_free(struct au_dcsub_pages *dpages)
6583 +{
6584 +       int i;
6585 +       struct au_dpage *p;
6586 +
6587 +       p = dpages->dpages;
6588 +       for (i = 0; i < dpages->ndpage; i++)
6589 +               au_dpage_free(p++);
6590 +       kfree(dpages->dpages);
6591 +}
6592 +
6593 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6594 +                           struct dentry *dentry, gfp_t gfp)
6595 +{
6596 +       int err, sz;
6597 +       struct au_dpage *dpage;
6598 +       void *p;
6599 +
6600 +       dpage = dpages->dpages + dpages->ndpage - 1;
6601 +       sz = PAGE_SIZE / sizeof(dentry);
6602 +       if (unlikely(dpage->ndentry >= sz)) {
6603 +               AuLabel(new dpage);
6604 +               err = -ENOMEM;
6605 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6606 +               p = au_kzrealloc(dpages->dpages, sz,
6607 +                                sz + sizeof(*dpages->dpages), gfp);
6608 +               if (unlikely(!p))
6609 +                       goto out;
6610 +
6611 +               dpages->dpages = p;
6612 +               dpage = dpages->dpages + dpages->ndpage;
6613 +               p = (void *)__get_free_page(gfp);
6614 +               if (unlikely(!p))
6615 +                       goto out;
6616 +
6617 +               dpage->ndentry = 0;
6618 +               dpage->dentries = p;
6619 +               dpages->ndpage++;
6620 +       }
6621 +
6622 +       AuDebugOn(au_dcount(dentry) <= 0);
6623 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6624 +       return 0; /* success */
6625 +
6626 +out:
6627 +       return err;
6628 +}
6629 +
6630 +/* todo: BAD approach */
6631 +/* copied from linux/fs/dcache.c */
6632 +enum d_walk_ret {
6633 +       D_WALK_CONTINUE,
6634 +       D_WALK_QUIT,
6635 +       D_WALK_NORETRY,
6636 +       D_WALK_SKIP,
6637 +};
6638 +
6639 +extern void d_walk(struct dentry *parent, void *data,
6640 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6641 +                  void (*finish)(void *));
6642 +
6643 +struct ac_dpages_arg {
6644 +       int err;
6645 +       struct au_dcsub_pages *dpages;
6646 +       struct super_block *sb;
6647 +       au_dpages_test test;
6648 +       void *arg;
6649 +};
6650 +
6651 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6652 +{
6653 +       enum d_walk_ret ret;
6654 +       struct ac_dpages_arg *arg = _arg;
6655 +
6656 +       ret = D_WALK_CONTINUE;
6657 +       if (dentry->d_sb == arg->sb
6658 +           && !IS_ROOT(dentry)
6659 +           && au_dcount(dentry) > 0
6660 +           && au_di(dentry)
6661 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6662 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6663 +               if (unlikely(arg->err))
6664 +                       ret = D_WALK_QUIT;
6665 +       }
6666 +
6667 +       return ret;
6668 +}
6669 +
6670 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6671 +                  au_dpages_test test, void *arg)
6672 +{
6673 +       struct ac_dpages_arg args = {
6674 +               .err    = 0,
6675 +               .dpages = dpages,
6676 +               .sb     = root->d_sb,
6677 +               .test   = test,
6678 +               .arg    = arg
6679 +       };
6680 +
6681 +       d_walk(root, &args, au_call_dpages_append, NULL);
6682 +
6683 +       return args.err;
6684 +}
6685 +
6686 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6687 +                      int do_include, au_dpages_test test, void *arg)
6688 +{
6689 +       int err;
6690 +
6691 +       err = 0;
6692 +       write_seqlock(&rename_lock);
6693 +       spin_lock(&dentry->d_lock);
6694 +       if (do_include
6695 +           && au_dcount(dentry) > 0
6696 +           && (!test || test(dentry, arg)))
6697 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6698 +       spin_unlock(&dentry->d_lock);
6699 +       if (unlikely(err))
6700 +               goto out;
6701 +
6702 +       /*
6703 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6704 +        * mount
6705 +        */
6706 +       while (!IS_ROOT(dentry)) {
6707 +               dentry = dentry->d_parent; /* rename_lock is locked */
6708 +               spin_lock(&dentry->d_lock);
6709 +               if (au_dcount(dentry) > 0
6710 +                   && (!test || test(dentry, arg)))
6711 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6712 +               spin_unlock(&dentry->d_lock);
6713 +               if (unlikely(err))
6714 +                       break;
6715 +       }
6716 +
6717 +out:
6718 +       write_sequnlock(&rename_lock);
6719 +       return err;
6720 +}
6721 +
6722 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6723 +{
6724 +       return au_di(dentry) && dentry->d_sb == arg;
6725 +}
6726 +
6727 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6728 +                           struct dentry *dentry, int do_include)
6729 +{
6730 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6731 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6732 +}
6733 +
6734 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6735 +{
6736 +       struct path path[2] = {
6737 +               {
6738 +                       .dentry = d1
6739 +               },
6740 +               {
6741 +                       .dentry = d2
6742 +               }
6743 +       };
6744 +
6745 +       return path_is_under(path + 0, path + 1);
6746 +}
6747 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6748 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6749 +++ linux/fs/aufs/dcsub.h       2016-04-24 18:32:51.390353525 +0200
6750 @@ -0,0 +1,136 @@
6751 +/*
6752 + * Copyright (C) 2005-2016 Junjiro R. Okajima
6753 + *
6754 + * This program, aufs is free software; you can redistribute it and/or modify
6755 + * it under the terms of the GNU General Public License as published by
6756 + * the Free Software Foundation; either version 2 of the License, or
6757 + * (at your option) any later version.
6758 + *
6759 + * This program is distributed in the hope that it will be useful,
6760 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6761 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6762 + * GNU General Public License for more details.
6763 + *
6764 + * You should have received a copy of the GNU General Public License
6765 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6766 + */
6767 +
6768 +/*
6769 + * sub-routines for dentry cache
6770 + */
6771 +
6772 +#ifndef __AUFS_DCSUB_H__
6773 +#define __AUFS_DCSUB_H__
6774 +
6775 +#ifdef __KERNEL__
6776 +
6777 +#include <linux/dcache.h>
6778 +#include <linux/fs.h>
6779 +
6780 +struct au_dpage {
6781 +       int ndentry;
6782 +       struct dentry **dentries;
6783 +};
6784 +
6785 +struct au_dcsub_pages {
6786 +       int ndpage;
6787 +       struct au_dpage *dpages;
6788 +};
6789 +
6790 +/* ---------------------------------------------------------------------- */
6791 +
6792 +/* dcsub.c */
6793 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6794 +void au_dpages_free(struct au_dcsub_pages *dpages);
6795 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6796 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6797 +                  au_dpages_test test, void *arg);
6798 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6799 +                      int do_include, au_dpages_test test, void *arg);
6800 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6801 +                           struct dentry *dentry, int do_include);
6802 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6803 +
6804 +/* ---------------------------------------------------------------------- */
6805 +
6806 +/*
6807 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6808 + * include/linux/dcache.h. Try them (in the future).
6809 + */
6810 +
6811 +static inline int au_d_hashed_positive(struct dentry *d)
6812 +{
6813 +       int err;
6814 +       struct inode *inode = d_inode(d);
6815 +
6816 +       err = 0;
6817 +       if (unlikely(d_unhashed(d)
6818 +                    || d_is_negative(d)
6819 +                    || !inode->i_nlink))
6820 +               err = -ENOENT;
6821 +       return err;
6822 +}
6823 +
6824 +static inline int au_d_linkable(struct dentry *d)
6825 +{
6826 +       int err;
6827 +       struct inode *inode = d_inode(d);
6828 +
6829 +       err = au_d_hashed_positive(d);
6830 +       if (err
6831 +           && d_is_positive(d)
6832 +           && (inode->i_state & I_LINKABLE))
6833 +               err = 0;
6834 +       return err;
6835 +}
6836 +
6837 +static inline int au_d_alive(struct dentry *d)
6838 +{
6839 +       int err;
6840 +       struct inode *inode;
6841 +
6842 +       err = 0;
6843 +       if (!IS_ROOT(d))
6844 +               err = au_d_hashed_positive(d);
6845 +       else {
6846 +               inode = d_inode(d);
6847 +               if (unlikely(d_unlinked(d)
6848 +                            || d_is_negative(d)
6849 +                            || !inode->i_nlink))
6850 +                       err = -ENOENT;
6851 +       }
6852 +       return err;
6853 +}
6854 +
6855 +static inline int au_alive_dir(struct dentry *d)
6856 +{
6857 +       int err;
6858 +
6859 +       err = au_d_alive(d);
6860 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
6861 +               err = -ENOENT;
6862 +       return err;
6863 +}
6864 +
6865 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6866 +{
6867 +       return a->len == b->len
6868 +               && !memcmp(a->name, b->name, a->len);
6869 +}
6870 +
6871 +/*
6872 + * by the commit
6873 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6874 + *                     taking d_lock
6875 + * the type of d_lockref.count became int, but the inlined function d_count()
6876 + * still returns unsigned int.
6877 + * I don't know why. Maybe it is for every d_count() users?
6878 + * Anyway au_dcount() lives on.
6879 + */
6880 +static inline int au_dcount(struct dentry *d)
6881 +{
6882 +       return (int)d_count(d);
6883 +}
6884 +
6885 +#endif /* __KERNEL__ */
6886 +#endif /* __AUFS_DCSUB_H__ */
6887 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6888 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6889 +++ linux/fs/aufs/debug.c       2016-04-24 18:32:51.390353525 +0200
6890 @@ -0,0 +1,438 @@
6891 +/*
6892 + * Copyright (C) 2005-2016 Junjiro R. Okajima
6893 + *
6894 + * This program, aufs is free software; you can redistribute it and/or modify
6895 + * it under the terms of the GNU General Public License as published by
6896 + * the Free Software Foundation; either version 2 of the License, or
6897 + * (at your option) any later version.
6898 + *
6899 + * This program is distributed in the hope that it will be useful,
6900 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6901 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6902 + * GNU General Public License for more details.
6903 + *
6904 + * You should have received a copy of the GNU General Public License
6905 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6906 + */
6907 +
6908 +/*
6909 + * debug print functions
6910 + */
6911 +
6912 +#include "aufs.h"
6913 +
6914 +/* Returns 0, or -errno.  arg is in kp->arg. */
6915 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6916 +{
6917 +       int err, n;
6918 +
6919 +       err = kstrtoint(val, 0, &n);
6920 +       if (!err) {
6921 +               if (n > 0)
6922 +                       au_debug_on();
6923 +               else
6924 +                       au_debug_off();
6925 +       }
6926 +       return err;
6927 +}
6928 +
6929 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6930 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6931 +{
6932 +       atomic_t *a;
6933 +
6934 +       a = kp->arg;
6935 +       return sprintf(buffer, "%d", atomic_read(a));
6936 +}
6937 +
6938 +static struct kernel_param_ops param_ops_atomic_t = {
6939 +       .set = param_atomic_t_set,
6940 +       .get = param_atomic_t_get
6941 +       /* void (*free)(void *arg) */
6942 +};
6943 +
6944 +atomic_t aufs_debug = ATOMIC_INIT(0);
6945 +MODULE_PARM_DESC(debug, "debug print");
6946 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6947 +
6948 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
6949 +char *au_plevel = KERN_DEBUG;
6950 +#define dpri(fmt, ...) do {                                    \
6951 +       if ((au_plevel                                          \
6952 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6953 +           || au_debug_test())                                 \
6954 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6955 +} while (0)
6956 +
6957 +/* ---------------------------------------------------------------------- */
6958 +
6959 +void au_dpri_whlist(struct au_nhash *whlist)
6960 +{
6961 +       unsigned long ul, n;
6962 +       struct hlist_head *head;
6963 +       struct au_vdir_wh *pos;
6964 +
6965 +       n = whlist->nh_num;
6966 +       head = whlist->nh_head;
6967 +       for (ul = 0; ul < n; ul++) {
6968 +               hlist_for_each_entry(pos, head, wh_hash)
6969 +                       dpri("b%d, %.*s, %d\n",
6970 +                            pos->wh_bindex,
6971 +                            pos->wh_str.len, pos->wh_str.name,
6972 +                            pos->wh_str.len);
6973 +               head++;
6974 +       }
6975 +}
6976 +
6977 +void au_dpri_vdir(struct au_vdir *vdir)
6978 +{
6979 +       unsigned long ul;
6980 +       union au_vdir_deblk_p p;
6981 +       unsigned char *o;
6982 +
6983 +       if (!vdir || IS_ERR(vdir)) {
6984 +               dpri("err %ld\n", PTR_ERR(vdir));
6985 +               return;
6986 +       }
6987 +
6988 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6989 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6990 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6991 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6992 +               p.deblk = vdir->vd_deblk[ul];
6993 +               o = p.deblk;
6994 +               dpri("[%lu]: %p\n", ul, o);
6995 +       }
6996 +}
6997 +
6998 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6999 +                       struct dentry *wh)
7000 +{
7001 +       char *n = NULL;
7002 +       int l = 0;
7003 +
7004 +       if (!inode || IS_ERR(inode)) {
7005 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7006 +               return -1;
7007 +       }
7008 +
7009 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7010 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7011 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7012 +       if (wh) {
7013 +               n = (void *)wh->d_name.name;
7014 +               l = wh->d_name.len;
7015 +       }
7016 +
7017 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7018 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7019 +            bindex, inode,
7020 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7021 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7022 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7023 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
7024 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7025 +            inode->i_state, inode->i_flags, inode->i_version,
7026 +            inode->i_generation,
7027 +            l ? ", wh " : "", l, n);
7028 +       return 0;
7029 +}
7030 +
7031 +void au_dpri_inode(struct inode *inode)
7032 +{
7033 +       struct au_iinfo *iinfo;
7034 +       aufs_bindex_t bindex;
7035 +       int err, hn;
7036 +
7037 +       err = do_pri_inode(-1, inode, -1, NULL);
7038 +       if (err || !au_test_aufs(inode->i_sb))
7039 +               return;
7040 +
7041 +       iinfo = au_ii(inode);
7042 +       if (!iinfo)
7043 +               return;
7044 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
7045 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
7046 +       if (iinfo->ii_bstart < 0)
7047 +               return;
7048 +       hn = 0;
7049 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
7050 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
7051 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
7052 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
7053 +       }
7054 +}
7055 +
7056 +void au_dpri_dalias(struct inode *inode)
7057 +{
7058 +       struct dentry *d;
7059 +
7060 +       spin_lock(&inode->i_lock);
7061 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7062 +               au_dpri_dentry(d);
7063 +       spin_unlock(&inode->i_lock);
7064 +}
7065 +
7066 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7067 +{
7068 +       struct dentry *wh = NULL;
7069 +       int hn;
7070 +       struct au_iinfo *iinfo;
7071 +
7072 +       if (!dentry || IS_ERR(dentry)) {
7073 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7074 +               return -1;
7075 +       }
7076 +       /* do not call dget_parent() here */
7077 +       /* note: access d_xxx without d_lock */
7078 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7079 +            bindex, dentry, dentry,
7080 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7081 +            au_dcount(dentry), dentry->d_flags,
7082 +            d_unhashed(dentry) ? "un" : "");
7083 +       hn = -1;
7084 +       if (bindex >= 0
7085 +           && d_is_positive(dentry)
7086 +           && au_test_aufs(dentry->d_sb)) {
7087 +               iinfo = au_ii(d_inode(dentry));
7088 +               if (iinfo) {
7089 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
7090 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
7091 +               }
7092 +       }
7093 +       do_pri_inode(bindex, d_inode(dentry), hn, wh);
7094 +       return 0;
7095 +}
7096 +
7097 +void au_dpri_dentry(struct dentry *dentry)
7098 +{
7099 +       struct au_dinfo *dinfo;
7100 +       aufs_bindex_t bindex;
7101 +       int err;
7102 +       struct au_hdentry *hdp;
7103 +
7104 +       err = do_pri_dentry(-1, dentry);
7105 +       if (err || !au_test_aufs(dentry->d_sb))
7106 +               return;
7107 +
7108 +       dinfo = au_di(dentry);
7109 +       if (!dinfo)
7110 +               return;
7111 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7112 +            dinfo->di_bstart, dinfo->di_bend,
7113 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7114 +            dinfo->di_tmpfile);
7115 +       if (dinfo->di_bstart < 0)
7116 +               return;
7117 +       hdp = dinfo->di_hdentry;
7118 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
7119 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
7120 +}
7121 +
7122 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7123 +{
7124 +       char a[32];
7125 +
7126 +       if (!file || IS_ERR(file)) {
7127 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7128 +               return -1;
7129 +       }
7130 +       a[0] = 0;
7131 +       if (bindex < 0
7132 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7133 +           && au_test_aufs(file->f_path.dentry->d_sb)
7134 +           && au_fi(file))
7135 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7136 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7137 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7138 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7139 +            file->f_version, file->f_pos, a);
7140 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7141 +               do_pri_dentry(bindex, file->f_path.dentry);
7142 +       return 0;
7143 +}
7144 +
7145 +void au_dpri_file(struct file *file)
7146 +{
7147 +       struct au_finfo *finfo;
7148 +       struct au_fidir *fidir;
7149 +       struct au_hfile *hfile;
7150 +       aufs_bindex_t bindex;
7151 +       int err;
7152 +
7153 +       err = do_pri_file(-1, file);
7154 +       if (err
7155 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7156 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7157 +               return;
7158 +
7159 +       finfo = au_fi(file);
7160 +       if (!finfo)
7161 +               return;
7162 +       if (finfo->fi_btop < 0)
7163 +               return;
7164 +       fidir = finfo->fi_hdir;
7165 +       if (!fidir)
7166 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7167 +       else
7168 +               for (bindex = finfo->fi_btop;
7169 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7170 +                    bindex++) {
7171 +                       hfile = fidir->fd_hfile + bindex;
7172 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7173 +               }
7174 +}
7175 +
7176 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7177 +{
7178 +       struct vfsmount *mnt;
7179 +       struct super_block *sb;
7180 +
7181 +       if (!br || IS_ERR(br))
7182 +               goto out;
7183 +       mnt = au_br_mnt(br);
7184 +       if (!mnt || IS_ERR(mnt))
7185 +               goto out;
7186 +       sb = mnt->mnt_sb;
7187 +       if (!sb || IS_ERR(sb))
7188 +               goto out;
7189 +
7190 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
7191 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7192 +            "xino %d\n",
7193 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
7194 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7195 +            sb->s_flags, sb->s_count,
7196 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7197 +       return 0;
7198 +
7199 +out:
7200 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7201 +       return -1;
7202 +}
7203 +
7204 +void au_dpri_sb(struct super_block *sb)
7205 +{
7206 +       struct au_sbinfo *sbinfo;
7207 +       aufs_bindex_t bindex;
7208 +       int err;
7209 +       /* to reuduce stack size */
7210 +       struct {
7211 +               struct vfsmount mnt;
7212 +               struct au_branch fake;
7213 +       } *a;
7214 +
7215 +       /* this function can be called from magic sysrq */
7216 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7217 +       if (unlikely(!a)) {
7218 +               dpri("no memory\n");
7219 +               return;
7220 +       }
7221 +
7222 +       a->mnt.mnt_sb = sb;
7223 +       a->fake.br_path.mnt = &a->mnt;
7224 +       atomic_set(&a->fake.br_count, 0);
7225 +       smp_mb(); /* atomic_set */
7226 +       err = do_pri_br(-1, &a->fake);
7227 +       kfree(a);
7228 +       dpri("dev 0x%x\n", sb->s_dev);
7229 +       if (err || !au_test_aufs(sb))
7230 +               return;
7231 +
7232 +       sbinfo = au_sbi(sb);
7233 +       if (!sbinfo)
7234 +               return;
7235 +       dpri("nw %d, gen %u, kobj %d\n",
7236 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7237 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
7238 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
7239 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7240 +}
7241 +
7242 +/* ---------------------------------------------------------------------- */
7243 +
7244 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7245 +{
7246 +       struct inode *h_inode, *inode = d_inode(dentry);
7247 +       struct dentry *h_dentry;
7248 +       aufs_bindex_t bindex, bend, bi;
7249 +
7250 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7251 +               return;
7252 +
7253 +       bend = au_dbend(dentry);
7254 +       bi = au_ibend(inode);
7255 +       if (bi < bend)
7256 +               bend = bi;
7257 +       bindex = au_dbstart(dentry);
7258 +       bi = au_ibstart(inode);
7259 +       if (bi > bindex)
7260 +               bindex = bi;
7261 +
7262 +       for (; bindex <= bend; bindex++) {
7263 +               h_dentry = au_h_dptr(dentry, bindex);
7264 +               if (!h_dentry)
7265 +                       continue;
7266 +               h_inode = au_h_iptr(inode, bindex);
7267 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7268 +                       au_debug_on();
7269 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7270 +                       AuDbgDentry(dentry);
7271 +                       AuDbgInode(inode);
7272 +                       au_debug_off();
7273 +                       BUG();
7274 +               }
7275 +       }
7276 +}
7277 +
7278 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7279 +{
7280 +       int err, i, j;
7281 +       struct au_dcsub_pages dpages;
7282 +       struct au_dpage *dpage;
7283 +       struct dentry **dentries;
7284 +
7285 +       err = au_dpages_init(&dpages, GFP_NOFS);
7286 +       AuDebugOn(err);
7287 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7288 +       AuDebugOn(err);
7289 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7290 +               dpage = dpages.dpages + i;
7291 +               dentries = dpage->dentries;
7292 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7293 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7294 +       }
7295 +       au_dpages_free(&dpages);
7296 +}
7297 +
7298 +void au_dbg_verify_kthread(void)
7299 +{
7300 +       if (au_wkq_test()) {
7301 +               au_dbg_blocked();
7302 +               /*
7303 +                * It may be recursive, but udba=notify between two aufs mounts,
7304 +                * where a single ro branch is shared, is not a problem.
7305 +                */
7306 +               /* WARN_ON(1); */
7307 +       }
7308 +}
7309 +
7310 +/* ---------------------------------------------------------------------- */
7311 +
7312 +int __init au_debug_init(void)
7313 +{
7314 +       aufs_bindex_t bindex;
7315 +       struct au_vdir_destr destr;
7316 +
7317 +       bindex = -1;
7318 +       AuDebugOn(bindex >= 0);
7319 +
7320 +       destr.len = -1;
7321 +       AuDebugOn(destr.len < NAME_MAX);
7322 +
7323 +#ifdef CONFIG_4KSTACKS
7324 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7325 +#endif
7326 +
7327 +       return 0;
7328 +}
7329 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7330 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7331 +++ linux/fs/aufs/debug.h       2016-04-24 18:32:51.390353525 +0200
7332 @@ -0,0 +1,225 @@
7333 +/*
7334 + * Copyright (C) 2005-2016 Junjiro R. Okajima
7335 + *
7336 + * This program, aufs is free software; you can redistribute it and/or modify
7337 + * it under the terms of the GNU General Public License as published by
7338 + * the Free Software Foundation; either version 2 of the License, or
7339 + * (at your option) any later version.
7340 + *
7341 + * This program is distributed in the hope that it will be useful,
7342 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7343 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7344 + * GNU General Public License for more details.
7345 + *
7346 + * You should have received a copy of the GNU General Public License
7347 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7348 + */
7349 +
7350 +/*
7351 + * debug print functions
7352 + */
7353 +
7354 +#ifndef __AUFS_DEBUG_H__
7355 +#define __AUFS_DEBUG_H__
7356 +
7357 +#ifdef __KERNEL__
7358 +
7359 +#include <linux/atomic.h>
7360 +#include <linux/module.h>
7361 +#include <linux/kallsyms.h>
7362 +#include <linux/sysrq.h>
7363 +
7364 +#ifdef CONFIG_AUFS_DEBUG
7365 +#define AuDebugOn(a)           BUG_ON(a)
7366 +
7367 +/* module parameter */
7368 +extern atomic_t aufs_debug;
7369 +static inline void au_debug_on(void)
7370 +{
7371 +       atomic_inc(&aufs_debug);
7372 +}
7373 +static inline void au_debug_off(void)
7374 +{
7375 +       atomic_dec_if_positive(&aufs_debug);
7376 +}
7377 +
7378 +static inline int au_debug_test(void)
7379 +{
7380 +       return atomic_read(&aufs_debug) > 0;
7381 +}
7382 +#else
7383 +#define AuDebugOn(a)           do {} while (0)
7384 +AuStubVoid(au_debug_on, void)
7385 +AuStubVoid(au_debug_off, void)
7386 +AuStubInt0(au_debug_test, void)
7387 +#endif /* CONFIG_AUFS_DEBUG */
7388 +
7389 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7390 +
7391 +/* ---------------------------------------------------------------------- */
7392 +
7393 +/* debug print */
7394 +
7395 +#define AuDbg(fmt, ...) do { \
7396 +       if (au_debug_test()) \
7397 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7398 +} while (0)
7399 +#define AuLabel(l)             AuDbg(#l "\n")
7400 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7401 +#define AuWarn1(fmt, ...) do { \
7402 +       static unsigned char _c; \
7403 +       if (!_c++) \
7404 +               pr_warn(fmt, ##__VA_ARGS__); \
7405 +} while (0)
7406 +
7407 +#define AuErr1(fmt, ...) do { \
7408 +       static unsigned char _c; \
7409 +       if (!_c++) \
7410 +               pr_err(fmt, ##__VA_ARGS__); \
7411 +} while (0)
7412 +
7413 +#define AuIOErr1(fmt, ...) do { \
7414 +       static unsigned char _c; \
7415 +       if (!_c++) \
7416 +               AuIOErr(fmt, ##__VA_ARGS__); \
7417 +} while (0)
7418 +
7419 +#define AuUnsupportMsg "This operation is not supported." \
7420 +                       " Please report this application to aufs-users ML."
7421 +#define AuUnsupport(fmt, ...) do { \
7422 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7423 +       dump_stack(); \
7424 +} while (0)
7425 +
7426 +#define AuTraceErr(e) do { \
7427 +       if (unlikely((e) < 0)) \
7428 +               AuDbg("err %d\n", (int)(e)); \
7429 +} while (0)
7430 +
7431 +#define AuTraceErrPtr(p) do { \
7432 +       if (IS_ERR(p)) \
7433 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7434 +} while (0)
7435 +
7436 +/* dirty macros for debug print, use with "%.*s" and caution */
7437 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7438 +
7439 +/* ---------------------------------------------------------------------- */
7440 +
7441 +struct dentry;
7442 +#ifdef CONFIG_AUFS_DEBUG
7443 +extern struct mutex au_dbg_mtx;
7444 +extern char *au_plevel;
7445 +struct au_nhash;
7446 +void au_dpri_whlist(struct au_nhash *whlist);
7447 +struct au_vdir;
7448 +void au_dpri_vdir(struct au_vdir *vdir);
7449 +struct inode;
7450 +void au_dpri_inode(struct inode *inode);
7451 +void au_dpri_dalias(struct inode *inode);
7452 +void au_dpri_dentry(struct dentry *dentry);
7453 +struct file;
7454 +void au_dpri_file(struct file *filp);
7455 +struct super_block;
7456 +void au_dpri_sb(struct super_block *sb);
7457 +
7458 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7459 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7460 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7461 +void au_dbg_verify_kthread(void);
7462 +
7463 +int __init au_debug_init(void);
7464 +
7465 +#define AuDbgWhlist(w) do { \
7466 +       mutex_lock(&au_dbg_mtx); \
7467 +       AuDbg(#w "\n"); \
7468 +       au_dpri_whlist(w); \
7469 +       mutex_unlock(&au_dbg_mtx); \
7470 +} while (0)
7471 +
7472 +#define AuDbgVdir(v) do { \
7473 +       mutex_lock(&au_dbg_mtx); \
7474 +       AuDbg(#v "\n"); \
7475 +       au_dpri_vdir(v); \
7476 +       mutex_unlock(&au_dbg_mtx); \
7477 +} while (0)
7478 +
7479 +#define AuDbgInode(i) do { \
7480 +       mutex_lock(&au_dbg_mtx); \
7481 +       AuDbg(#i "\n"); \
7482 +       au_dpri_inode(i); \
7483 +       mutex_unlock(&au_dbg_mtx); \
7484 +} while (0)
7485 +
7486 +#define AuDbgDAlias(i) do { \
7487 +       mutex_lock(&au_dbg_mtx); \
7488 +       AuDbg(#i "\n"); \
7489 +       au_dpri_dalias(i); \
7490 +       mutex_unlock(&au_dbg_mtx); \
7491 +} while (0)
7492 +
7493 +#define AuDbgDentry(d) do { \
7494 +       mutex_lock(&au_dbg_mtx); \
7495 +       AuDbg(#d "\n"); \
7496 +       au_dpri_dentry(d); \
7497 +       mutex_unlock(&au_dbg_mtx); \
7498 +} while (0)
7499 +
7500 +#define AuDbgFile(f) do { \
7501 +       mutex_lock(&au_dbg_mtx); \
7502 +       AuDbg(#f "\n"); \
7503 +       au_dpri_file(f); \
7504 +       mutex_unlock(&au_dbg_mtx); \
7505 +} while (0)
7506 +
7507 +#define AuDbgSb(sb) do { \
7508 +       mutex_lock(&au_dbg_mtx); \
7509 +       AuDbg(#sb "\n"); \
7510 +       au_dpri_sb(sb); \
7511 +       mutex_unlock(&au_dbg_mtx); \
7512 +} while (0)
7513 +
7514 +#define AuDbgSym(addr) do {                            \
7515 +       char sym[KSYM_SYMBOL_LEN];                      \
7516 +       sprint_symbol(sym, (unsigned long)addr);        \
7517 +       AuDbg("%s\n", sym);                             \
7518 +} while (0)
7519 +#else
7520 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7521 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7522 +AuStubVoid(au_dbg_verify_kthread, void)
7523 +AuStubInt0(__init au_debug_init, void)
7524 +
7525 +#define AuDbgWhlist(w)         do {} while (0)
7526 +#define AuDbgVdir(v)           do {} while (0)
7527 +#define AuDbgInode(i)          do {} while (0)
7528 +#define AuDbgDAlias(i)         do {} while (0)
7529 +#define AuDbgDentry(d)         do {} while (0)
7530 +#define AuDbgFile(f)           do {} while (0)
7531 +#define AuDbgSb(sb)            do {} while (0)
7532 +#define AuDbgSym(addr)         do {} while (0)
7533 +#endif /* CONFIG_AUFS_DEBUG */
7534 +
7535 +/* ---------------------------------------------------------------------- */
7536 +
7537 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7538 +int __init au_sysrq_init(void);
7539 +void au_sysrq_fin(void);
7540 +
7541 +#ifdef CONFIG_HW_CONSOLE
7542 +#define au_dbg_blocked() do { \
7543 +       WARN_ON(1); \
7544 +       handle_sysrq('w'); \
7545 +} while (0)
7546 +#else
7547 +AuStubVoid(au_dbg_blocked, void)
7548 +#endif
7549 +
7550 +#else
7551 +AuStubInt0(__init au_sysrq_init, void)
7552 +AuStubVoid(au_sysrq_fin, void)
7553 +AuStubVoid(au_dbg_blocked, void)
7554 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7555 +
7556 +#endif /* __KERNEL__ */
7557 +#endif /* __AUFS_DEBUG_H__ */
7558 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7559 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7560 +++ linux/fs/aufs/dentry.c      2016-04-24 18:32:51.390353525 +0200
7561 @@ -0,0 +1,1136 @@
7562 +/*
7563 + * Copyright (C) 2005-2016 Junjiro R. Okajima
7564 + *
7565 + * This program, aufs is free software; you can redistribute it and/or modify
7566 + * it under the terms of the GNU General Public License as published by
7567 + * the Free Software Foundation; either version 2 of the License, or
7568 + * (at your option) any later version.
7569 + *
7570 + * This program is distributed in the hope that it will be useful,
7571 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7572 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7573 + * GNU General Public License for more details.
7574 + *
7575 + * You should have received a copy of the GNU General Public License
7576 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7577 + */
7578 +
7579 +/*
7580 + * lookup and dentry operations
7581 + */
7582 +
7583 +#include <linux/namei.h>
7584 +#include "aufs.h"
7585 +
7586 +#define AuLkup_ALLOW_NEG       1
7587 +#define AuLkup_IGNORE_PERM     (1 << 1)
7588 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
7589 +#define au_fset_lkup(flags, name) \
7590 +       do { (flags) |= AuLkup_##name; } while (0)
7591 +#define au_fclr_lkup(flags, name) \
7592 +       do { (flags) &= ~AuLkup_##name; } while (0)
7593 +
7594 +struct au_do_lookup_args {
7595 +       unsigned int            flags;
7596 +       mode_t                  type;
7597 +};
7598 +
7599 +/*
7600 + * returns positive/negative dentry, NULL or an error.
7601 + * NULL means whiteout-ed or not-found.
7602 + */
7603 +static struct dentry*
7604 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7605 +            aufs_bindex_t bindex, struct qstr *wh_name,
7606 +            struct au_do_lookup_args *args)
7607 +{
7608 +       struct dentry *h_dentry;
7609 +       struct inode *h_inode;
7610 +       struct au_branch *br;
7611 +       int wh_found, opq;
7612 +       unsigned char wh_able;
7613 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7614 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7615 +                                                         IGNORE_PERM);
7616 +
7617 +       wh_found = 0;
7618 +       br = au_sbr(dentry->d_sb, bindex);
7619 +       wh_able = !!au_br_whable(br->br_perm);
7620 +       if (wh_able)
7621 +               wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
7622 +       h_dentry = ERR_PTR(wh_found);
7623 +       if (!wh_found)
7624 +               goto real_lookup;
7625 +       if (unlikely(wh_found < 0))
7626 +               goto out;
7627 +
7628 +       /* We found a whiteout */
7629 +       /* au_set_dbend(dentry, bindex); */
7630 +       au_set_dbwh(dentry, bindex);
7631 +       if (!allow_neg)
7632 +               return NULL; /* success */
7633 +
7634 +real_lookup:
7635 +       if (!ignore_perm)
7636 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7637 +       else
7638 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7639 +       if (IS_ERR(h_dentry)) {
7640 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7641 +                   && !allow_neg)
7642 +                       h_dentry = NULL;
7643 +               goto out;
7644 +       }
7645 +
7646 +       h_inode = d_inode(h_dentry);
7647 +       if (d_is_negative(h_dentry)) {
7648 +               if (!allow_neg)
7649 +                       goto out_neg;
7650 +       } else if (wh_found
7651 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7652 +               goto out_neg;
7653 +
7654 +       if (au_dbend(dentry) <= bindex)
7655 +               au_set_dbend(dentry, bindex);
7656 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7657 +               au_set_dbstart(dentry, bindex);
7658 +       au_set_h_dptr(dentry, bindex, h_dentry);
7659 +
7660 +       if (!d_is_dir(h_dentry)
7661 +           || !wh_able
7662 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7663 +               goto out; /* success */
7664 +
7665 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
7666 +       opq = au_diropq_test(h_dentry);
7667 +       inode_unlock(h_inode);
7668 +       if (opq > 0)
7669 +               au_set_dbdiropq(dentry, bindex);
7670 +       else if (unlikely(opq < 0)) {
7671 +               au_set_h_dptr(dentry, bindex, NULL);
7672 +               h_dentry = ERR_PTR(opq);
7673 +       }
7674 +       goto out;
7675 +
7676 +out_neg:
7677 +       dput(h_dentry);
7678 +       h_dentry = NULL;
7679 +out:
7680 +       return h_dentry;
7681 +}
7682 +
7683 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7684 +{
7685 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7686 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7687 +               return -EPERM;
7688 +       return 0;
7689 +}
7690 +
7691 +/*
7692 + * returns the number of lower positive dentries,
7693 + * otherwise an error.
7694 + * can be called at unlinking with @type is zero.
7695 + */
7696 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
7697 +{
7698 +       int npositive, err;
7699 +       aufs_bindex_t bindex, btail, bdiropq;
7700 +       unsigned char isdir, dirperm1;
7701 +       struct qstr whname;
7702 +       struct au_do_lookup_args args = {
7703 +               .flags          = 0,
7704 +               .type           = type
7705 +       };
7706 +       const struct qstr *name = &dentry->d_name;
7707 +       struct dentry *parent;
7708 +       struct super_block *sb;
7709 +
7710 +       sb = dentry->d_sb;
7711 +       err = au_test_shwh(sb, name);
7712 +       if (unlikely(err))
7713 +               goto out;
7714 +
7715 +       err = au_wh_name_alloc(&whname, name);
7716 +       if (unlikely(err))
7717 +               goto out;
7718 +
7719 +       isdir = !!d_is_dir(dentry);
7720 +       if (!type)
7721 +               au_fset_lkup(args.flags, ALLOW_NEG);
7722 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7723 +
7724 +       npositive = 0;
7725 +       parent = dget_parent(dentry);
7726 +       btail = au_dbtaildir(parent);
7727 +       for (bindex = bstart; bindex <= btail; bindex++) {
7728 +               struct dentry *h_parent, *h_dentry;
7729 +               struct inode *h_inode, *h_dir;
7730 +
7731 +               h_dentry = au_h_dptr(dentry, bindex);
7732 +               if (h_dentry) {
7733 +                       if (d_is_positive(h_dentry))
7734 +                               npositive++;
7735 +                       if (type != S_IFDIR)
7736 +                               break;
7737 +                       continue;
7738 +               }
7739 +               h_parent = au_h_dptr(parent, bindex);
7740 +               if (!h_parent || !d_is_dir(h_parent))
7741 +                       continue;
7742 +
7743 +               h_dir = d_inode(h_parent);
7744 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
7745 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7746 +                                       &args);
7747 +               inode_unlock(h_dir);
7748 +               err = PTR_ERR(h_dentry);
7749 +               if (IS_ERR(h_dentry))
7750 +                       goto out_parent;
7751 +               if (h_dentry)
7752 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7753 +               if (dirperm1)
7754 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7755 +
7756 +               if (au_dbwh(dentry) == bindex)
7757 +                       break;
7758 +               if (!h_dentry)
7759 +                       continue;
7760 +               if (d_is_negative(h_dentry))
7761 +                       continue;
7762 +               h_inode = d_inode(h_dentry);
7763 +               npositive++;
7764 +               if (!args.type)
7765 +                       args.type = h_inode->i_mode & S_IFMT;
7766 +               if (args.type != S_IFDIR)
7767 +                       break;
7768 +               else if (isdir) {
7769 +                       /* the type of lower may be different */
7770 +                       bdiropq = au_dbdiropq(dentry);
7771 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7772 +                               break;
7773 +               }
7774 +       }
7775 +
7776 +       if (npositive) {
7777 +               AuLabel(positive);
7778 +               au_update_dbstart(dentry);
7779 +       }
7780 +       err = npositive;
7781 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7782 +                    && au_dbstart(dentry) < 0)) {
7783 +               err = -EIO;
7784 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7785 +                       dentry, err);
7786 +       }
7787 +
7788 +out_parent:
7789 +       dput(parent);
7790 +       kfree(whname.name);
7791 +out:
7792 +       return err;
7793 +}
7794 +
7795 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7796 +{
7797 +       struct dentry *dentry;
7798 +       int wkq_err;
7799 +
7800 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
7801 +               dentry = vfsub_lkup_one(name, parent);
7802 +       else {
7803 +               struct vfsub_lkup_one_args args = {
7804 +                       .errp   = &dentry,
7805 +                       .name   = name,
7806 +                       .parent = parent
7807 +               };
7808 +
7809 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7810 +               if (unlikely(wkq_err))
7811 +                       dentry = ERR_PTR(wkq_err);
7812 +       }
7813 +
7814 +       return dentry;
7815 +}
7816 +
7817 +/*
7818 + * lookup @dentry on @bindex which should be negative.
7819 + */
7820 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7821 +{
7822 +       int err;
7823 +       struct dentry *parent, *h_parent, *h_dentry;
7824 +       struct au_branch *br;
7825 +
7826 +       parent = dget_parent(dentry);
7827 +       h_parent = au_h_dptr(parent, bindex);
7828 +       br = au_sbr(dentry->d_sb, bindex);
7829 +       if (wh)
7830 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7831 +       else
7832 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7833 +       err = PTR_ERR(h_dentry);
7834 +       if (IS_ERR(h_dentry))
7835 +               goto out;
7836 +       if (unlikely(d_is_positive(h_dentry))) {
7837 +               err = -EIO;
7838 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7839 +               dput(h_dentry);
7840 +               goto out;
7841 +       }
7842 +
7843 +       err = 0;
7844 +       if (bindex < au_dbstart(dentry))
7845 +               au_set_dbstart(dentry, bindex);
7846 +       if (au_dbend(dentry) < bindex)
7847 +               au_set_dbend(dentry, bindex);
7848 +       au_set_h_dptr(dentry, bindex, h_dentry);
7849 +
7850 +out:
7851 +       dput(parent);
7852 +       return err;
7853 +}
7854 +
7855 +/* ---------------------------------------------------------------------- */
7856 +
7857 +/* subset of struct inode */
7858 +struct au_iattr {
7859 +       unsigned long           i_ino;
7860 +       /* unsigned int         i_nlink; */
7861 +       kuid_t                  i_uid;
7862 +       kgid_t                  i_gid;
7863 +       u64                     i_version;
7864 +/*
7865 +       loff_t                  i_size;
7866 +       blkcnt_t                i_blocks;
7867 +*/
7868 +       umode_t                 i_mode;
7869 +};
7870 +
7871 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7872 +{
7873 +       ia->i_ino = h_inode->i_ino;
7874 +       /* ia->i_nlink = h_inode->i_nlink; */
7875 +       ia->i_uid = h_inode->i_uid;
7876 +       ia->i_gid = h_inode->i_gid;
7877 +       ia->i_version = h_inode->i_version;
7878 +/*
7879 +       ia->i_size = h_inode->i_size;
7880 +       ia->i_blocks = h_inode->i_blocks;
7881 +*/
7882 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7883 +}
7884 +
7885 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7886 +{
7887 +       return ia->i_ino != h_inode->i_ino
7888 +               /* || ia->i_nlink != h_inode->i_nlink */
7889 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7890 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7891 +               || ia->i_version != h_inode->i_version
7892 +/*
7893 +               || ia->i_size != h_inode->i_size
7894 +               || ia->i_blocks != h_inode->i_blocks
7895 +*/
7896 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7897 +}
7898 +
7899 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7900 +                             struct au_branch *br)
7901 +{
7902 +       int err;
7903 +       struct au_iattr ia;
7904 +       struct inode *h_inode;
7905 +       struct dentry *h_d;
7906 +       struct super_block *h_sb;
7907 +
7908 +       err = 0;
7909 +       memset(&ia, -1, sizeof(ia));
7910 +       h_sb = h_dentry->d_sb;
7911 +       h_inode = NULL;
7912 +       if (d_is_positive(h_dentry)) {
7913 +               h_inode = d_inode(h_dentry);
7914 +               au_iattr_save(&ia, h_inode);
7915 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7916 +               /* nfs d_revalidate may return 0 for negative dentry */
7917 +               /* fuse d_revalidate always return 0 for negative dentry */
7918 +               goto out;
7919 +
7920 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7921 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7922 +       err = PTR_ERR(h_d);
7923 +       if (IS_ERR(h_d))
7924 +               goto out;
7925 +
7926 +       err = 0;
7927 +       if (unlikely(h_d != h_dentry
7928 +                    || d_inode(h_d) != h_inode
7929 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7930 +               err = au_busy_or_stale();
7931 +       dput(h_d);
7932 +
7933 +out:
7934 +       AuTraceErr(err);
7935 +       return err;
7936 +}
7937 +
7938 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7939 +               struct dentry *h_parent, struct au_branch *br)
7940 +{
7941 +       int err;
7942 +
7943 +       err = 0;
7944 +       if (udba == AuOpt_UDBA_REVAL
7945 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7946 +               IMustLock(h_dir);
7947 +               err = (d_inode(h_dentry->d_parent) != h_dir);
7948 +       } else if (udba != AuOpt_UDBA_NONE)
7949 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7950 +
7951 +       return err;
7952 +}
7953 +
7954 +/* ---------------------------------------------------------------------- */
7955 +
7956 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7957 +{
7958 +       int err;
7959 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7960 +       struct au_hdentry tmp, *p, *q;
7961 +       struct au_dinfo *dinfo;
7962 +       struct super_block *sb;
7963 +
7964 +       DiMustWriteLock(dentry);
7965 +
7966 +       sb = dentry->d_sb;
7967 +       dinfo = au_di(dentry);
7968 +       bend = dinfo->di_bend;
7969 +       bwh = dinfo->di_bwh;
7970 +       bdiropq = dinfo->di_bdiropq;
7971 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7972 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7973 +               if (!p->hd_dentry)
7974 +                       continue;
7975 +
7976 +               new_bindex = au_br_index(sb, p->hd_id);
7977 +               if (new_bindex == bindex)
7978 +                       continue;
7979 +
7980 +               if (dinfo->di_bwh == bindex)
7981 +                       bwh = new_bindex;
7982 +               if (dinfo->di_bdiropq == bindex)
7983 +                       bdiropq = new_bindex;
7984 +               if (new_bindex < 0) {
7985 +                       au_hdput(p);
7986 +                       p->hd_dentry = NULL;
7987 +                       continue;
7988 +               }
7989 +
7990 +               /* swap two lower dentries, and loop again */
7991 +               q = dinfo->di_hdentry + new_bindex;
7992 +               tmp = *q;
7993 +               *q = *p;
7994 +               *p = tmp;
7995 +               if (tmp.hd_dentry) {
7996 +                       bindex--;
7997 +                       p--;
7998 +               }
7999 +       }
8000 +
8001 +       dinfo->di_bwh = -1;
8002 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
8003 +               dinfo->di_bwh = bwh;
8004 +
8005 +       dinfo->di_bdiropq = -1;
8006 +       if (bdiropq >= 0
8007 +           && bdiropq <= au_sbend(sb)
8008 +           && au_sbr_whable(sb, bdiropq))
8009 +               dinfo->di_bdiropq = bdiropq;
8010 +
8011 +       err = -EIO;
8012 +       dinfo->di_bstart = -1;
8013 +       dinfo->di_bend = -1;
8014 +       bend = au_dbend(parent);
8015 +       p = dinfo->di_hdentry;
8016 +       for (bindex = 0; bindex <= bend; bindex++, p++)
8017 +               if (p->hd_dentry) {
8018 +                       dinfo->di_bstart = bindex;
8019 +                       break;
8020 +               }
8021 +
8022 +       if (dinfo->di_bstart >= 0) {
8023 +               p = dinfo->di_hdentry + bend;
8024 +               for (bindex = bend; bindex >= 0; bindex--, p--)
8025 +                       if (p->hd_dentry) {
8026 +                               dinfo->di_bend = bindex;
8027 +                               err = 0;
8028 +                               break;
8029 +                       }
8030 +       }
8031 +
8032 +       return err;
8033 +}
8034 +
8035 +static void au_do_hide(struct dentry *dentry)
8036 +{
8037 +       struct inode *inode;
8038 +
8039 +       if (d_really_is_positive(dentry)) {
8040 +               inode = d_inode(dentry);
8041 +               if (!d_is_dir(dentry)) {
8042 +                       if (inode->i_nlink && !d_unhashed(dentry))
8043 +                               drop_nlink(inode);
8044 +               } else {
8045 +                       clear_nlink(inode);
8046 +                       /* stop next lookup */
8047 +                       inode->i_flags |= S_DEAD;
8048 +               }
8049 +               smp_mb(); /* necessary? */
8050 +       }
8051 +       d_drop(dentry);
8052 +}
8053 +
8054 +static int au_hide_children(struct dentry *parent)
8055 +{
8056 +       int err, i, j, ndentry;
8057 +       struct au_dcsub_pages dpages;
8058 +       struct au_dpage *dpage;
8059 +       struct dentry *dentry;
8060 +
8061 +       err = au_dpages_init(&dpages, GFP_NOFS);
8062 +       if (unlikely(err))
8063 +               goto out;
8064 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8065 +       if (unlikely(err))
8066 +               goto out_dpages;
8067 +
8068 +       /* in reverse order */
8069 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8070 +               dpage = dpages.dpages + i;
8071 +               ndentry = dpage->ndentry;
8072 +               for (j = ndentry - 1; j >= 0; j--) {
8073 +                       dentry = dpage->dentries[j];
8074 +                       if (dentry != parent)
8075 +                               au_do_hide(dentry);
8076 +               }
8077 +       }
8078 +
8079 +out_dpages:
8080 +       au_dpages_free(&dpages);
8081 +out:
8082 +       return err;
8083 +}
8084 +
8085 +static void au_hide(struct dentry *dentry)
8086 +{
8087 +       int err;
8088 +
8089 +       AuDbgDentry(dentry);
8090 +       if (d_is_dir(dentry)) {
8091 +               /* shrink_dcache_parent(dentry); */
8092 +               err = au_hide_children(dentry);
8093 +               if (unlikely(err))
8094 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8095 +                               dentry, err);
8096 +       }
8097 +       au_do_hide(dentry);
8098 +}
8099 +
8100 +/*
8101 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8102 + *
8103 + * a dirty branch is added
8104 + * - on the top of layers
8105 + * - in the middle of layers
8106 + * - to the bottom of layers
8107 + *
8108 + * on the added branch there exists
8109 + * - a whiteout
8110 + * - a diropq
8111 + * - a same named entry
8112 + *   + exist
8113 + *     * negative --> positive
8114 + *     * positive --> positive
8115 + *      - type is unchanged
8116 + *      - type is changed
8117 + *   + doesn't exist
8118 + *     * negative --> negative
8119 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8120 + * - none
8121 + */
8122 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8123 +                              struct au_dinfo *tmp)
8124 +{
8125 +       int err;
8126 +       aufs_bindex_t bindex, bend;
8127 +       struct {
8128 +               struct dentry *dentry;
8129 +               struct inode *inode;
8130 +               mode_t mode;
8131 +       } orig_h, tmp_h = {
8132 +               .dentry = NULL
8133 +       };
8134 +       struct au_hdentry *hd;
8135 +       struct inode *inode, *h_inode;
8136 +       struct dentry *h_dentry;
8137 +
8138 +       err = 0;
8139 +       AuDebugOn(dinfo->di_bstart < 0);
8140 +       orig_h.mode = 0;
8141 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
8142 +       orig_h.inode = NULL;
8143 +       if (d_is_positive(orig_h.dentry)) {
8144 +               orig_h.inode = d_inode(orig_h.dentry);
8145 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8146 +       }
8147 +       if (tmp->di_bstart >= 0) {
8148 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
8149 +               if (d_is_positive(tmp_h.dentry)) {
8150 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8151 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8152 +               }
8153 +       }
8154 +
8155 +       inode = NULL;
8156 +       if (d_really_is_positive(dentry))
8157 +               inode = d_inode(dentry);
8158 +       if (!orig_h.inode) {
8159 +               AuDbg("nagative originally\n");
8160 +               if (inode) {
8161 +                       au_hide(dentry);
8162 +                       goto out;
8163 +               }
8164 +               AuDebugOn(inode);
8165 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8166 +               AuDebugOn(dinfo->di_bdiropq != -1);
8167 +
8168 +               if (!tmp_h.inode) {
8169 +                       AuDbg("negative --> negative\n");
8170 +                       /* should have only one negative lower */
8171 +                       if (tmp->di_bstart >= 0
8172 +                           && tmp->di_bstart < dinfo->di_bstart) {
8173 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
8174 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8175 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
8176 +                               au_di_cp(dinfo, tmp);
8177 +                               hd = tmp->di_hdentry + tmp->di_bstart;
8178 +                               au_set_h_dptr(dentry, tmp->di_bstart,
8179 +                                             dget(hd->hd_dentry));
8180 +                       }
8181 +                       au_dbg_verify_dinode(dentry);
8182 +               } else {
8183 +                       AuDbg("negative --> positive\n");
8184 +                       /*
8185 +                        * similar to the behaviour of creating with bypassing
8186 +                        * aufs.
8187 +                        * unhash it in order to force an error in the
8188 +                        * succeeding create operation.
8189 +                        * we should not set S_DEAD here.
8190 +                        */
8191 +                       d_drop(dentry);
8192 +                       /* au_di_swap(tmp, dinfo); */
8193 +                       au_dbg_verify_dinode(dentry);
8194 +               }
8195 +       } else {
8196 +               AuDbg("positive originally\n");
8197 +               /* inode may be NULL */
8198 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8199 +               if (!tmp_h.inode) {
8200 +                       AuDbg("positive --> negative\n");
8201 +                       /* or bypassing aufs */
8202 +                       au_hide(dentry);
8203 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
8204 +                               dinfo->di_bwh = tmp->di_bwh;
8205 +                       if (inode)
8206 +                               err = au_refresh_hinode_self(inode);
8207 +                       au_dbg_verify_dinode(dentry);
8208 +               } else if (orig_h.mode == tmp_h.mode) {
8209 +                       AuDbg("positive --> positive, same type\n");
8210 +                       if (!S_ISDIR(orig_h.mode)
8211 +                           && dinfo->di_bstart > tmp->di_bstart) {
8212 +                               /*
8213 +                                * similar to the behaviour of removing and
8214 +                                * creating.
8215 +                                */
8216 +                               au_hide(dentry);
8217 +                               if (inode)
8218 +                                       err = au_refresh_hinode_self(inode);
8219 +                               au_dbg_verify_dinode(dentry);
8220 +                       } else {
8221 +                               /* fill empty slots */
8222 +                               if (dinfo->di_bstart > tmp->di_bstart)
8223 +                                       dinfo->di_bstart = tmp->di_bstart;
8224 +                               if (dinfo->di_bend < tmp->di_bend)
8225 +                                       dinfo->di_bend = tmp->di_bend;
8226 +                               dinfo->di_bwh = tmp->di_bwh;
8227 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8228 +                               hd = tmp->di_hdentry;
8229 +                               bend = dinfo->di_bend;
8230 +                               for (bindex = tmp->di_bstart; bindex <= bend;
8231 +                                    bindex++) {
8232 +                                       if (au_h_dptr(dentry, bindex))
8233 +                                               continue;
8234 +                                       h_dentry = hd[bindex].hd_dentry;
8235 +                                       if (!h_dentry)
8236 +                                               continue;
8237 +                                       AuDebugOn(d_is_negative(h_dentry));
8238 +                                       h_inode = d_inode(h_dentry);
8239 +                                       AuDebugOn(orig_h.mode
8240 +                                                 != (h_inode->i_mode
8241 +                                                     & S_IFMT));
8242 +                                       au_set_h_dptr(dentry, bindex,
8243 +                                                     dget(h_dentry));
8244 +                               }
8245 +                               err = au_refresh_hinode(inode, dentry);
8246 +                               au_dbg_verify_dinode(dentry);
8247 +                       }
8248 +               } else {
8249 +                       AuDbg("positive --> positive, different type\n");
8250 +                       /* similar to the behaviour of removing and creating */
8251 +                       au_hide(dentry);
8252 +                       if (inode)
8253 +                               err = au_refresh_hinode_self(inode);
8254 +                       au_dbg_verify_dinode(dentry);
8255 +               }
8256 +       }
8257 +
8258 +out:
8259 +       return err;
8260 +}
8261 +
8262 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8263 +{
8264 +       const struct dentry_operations *dop
8265 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8266 +       static const unsigned int mask
8267 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8268 +
8269 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8270 +
8271 +       if (dentry->d_op == dop)
8272 +               return;
8273 +
8274 +       AuDbg("%pd\n", dentry);
8275 +       spin_lock(&dentry->d_lock);
8276 +       if (dop == &aufs_dop)
8277 +               dentry->d_flags |= mask;
8278 +       else
8279 +               dentry->d_flags &= ~mask;
8280 +       dentry->d_op = dop;
8281 +       spin_unlock(&dentry->d_lock);
8282 +}
8283 +
8284 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8285 +{
8286 +       int err, ebrange;
8287 +       unsigned int sigen;
8288 +       struct au_dinfo *dinfo, *tmp;
8289 +       struct super_block *sb;
8290 +       struct inode *inode;
8291 +
8292 +       DiMustWriteLock(dentry);
8293 +       AuDebugOn(IS_ROOT(dentry));
8294 +       AuDebugOn(d_really_is_negative(parent));
8295 +
8296 +       sb = dentry->d_sb;
8297 +       sigen = au_sigen(sb);
8298 +       err = au_digen_test(parent, sigen);
8299 +       if (unlikely(err))
8300 +               goto out;
8301 +
8302 +       dinfo = au_di(dentry);
8303 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
8304 +       if (unlikely(err))
8305 +               goto out;
8306 +       ebrange = au_dbrange_test(dentry);
8307 +       if (!ebrange)
8308 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8309 +
8310 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8311 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
8312 +               if (d_really_is_positive(dentry)) {
8313 +                       inode = d_inode(dentry);
8314 +                       err = au_refresh_hinode_self(inode);
8315 +               }
8316 +               au_dbg_verify_dinode(dentry);
8317 +               if (!err)
8318 +                       goto out_dgen; /* success */
8319 +               goto out;
8320 +       }
8321 +
8322 +       /* temporary dinfo */
8323 +       AuDbgDentry(dentry);
8324 +       err = -ENOMEM;
8325 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8326 +       if (unlikely(!tmp))
8327 +               goto out;
8328 +       au_di_swap(tmp, dinfo);
8329 +       /* returns the number of positive dentries */
8330 +       /*
8331 +        * if current working dir is removed, it returns an error.
8332 +        * but the dentry is legal.
8333 +        */
8334 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
8335 +       AuDbgDentry(dentry);
8336 +       au_di_swap(tmp, dinfo);
8337 +       if (err == -ENOENT)
8338 +               err = 0;
8339 +       if (err >= 0) {
8340 +               /* compare/refresh by dinfo */
8341 +               AuDbgDentry(dentry);
8342 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8343 +               au_dbg_verify_dinode(dentry);
8344 +               AuTraceErr(err);
8345 +       }
8346 +       au_rw_write_unlock(&tmp->di_rwsem);
8347 +       au_di_free(tmp);
8348 +       if (unlikely(err))
8349 +               goto out;
8350 +
8351 +out_dgen:
8352 +       au_update_digen(dentry);
8353 +out:
8354 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8355 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8356 +               AuDbgDentry(dentry);
8357 +       }
8358 +       AuTraceErr(err);
8359 +       return err;
8360 +}
8361 +
8362 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8363 +                          struct dentry *dentry, aufs_bindex_t bindex)
8364 +{
8365 +       int err, valid;
8366 +
8367 +       err = 0;
8368 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8369 +               goto out;
8370 +
8371 +       AuDbg("b%d\n", bindex);
8372 +       /*
8373 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8374 +        * due to whiteout and branch permission.
8375 +        */
8376 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8377 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8378 +       /* it may return tri-state */
8379 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8380 +
8381 +       if (unlikely(valid < 0))
8382 +               err = valid;
8383 +       else if (!valid)
8384 +               err = -EINVAL;
8385 +
8386 +out:
8387 +       AuTraceErr(err);
8388 +       return err;
8389 +}
8390 +
8391 +/* todo: remove this */
8392 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8393 +                         unsigned int flags, int do_udba)
8394 +{
8395 +       int err;
8396 +       umode_t mode, h_mode;
8397 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
8398 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8399 +       struct inode *h_inode, *h_cached_inode;
8400 +       struct dentry *h_dentry;
8401 +       struct qstr *name, *h_name;
8402 +
8403 +       err = 0;
8404 +       plus = 0;
8405 +       mode = 0;
8406 +       ibs = -1;
8407 +       ibe = -1;
8408 +       unhashed = !!d_unhashed(dentry);
8409 +       is_root = !!IS_ROOT(dentry);
8410 +       name = &dentry->d_name;
8411 +       tmpfile = au_di(dentry)->di_tmpfile;
8412 +
8413 +       /*
8414 +        * Theoretically, REVAL test should be unnecessary in case of
8415 +        * {FS,I}NOTIFY.
8416 +        * But {fs,i}notify doesn't fire some necessary events,
8417 +        *      IN_ATTRIB for atime/nlink/pageio
8418 +        * Let's do REVAL test too.
8419 +        */
8420 +       if (do_udba && inode) {
8421 +               mode = (inode->i_mode & S_IFMT);
8422 +               plus = (inode->i_nlink > 0);
8423 +               ibs = au_ibstart(inode);
8424 +               ibe = au_ibend(inode);
8425 +       }
8426 +
8427 +       bstart = au_dbstart(dentry);
8428 +       btail = bstart;
8429 +       if (inode && S_ISDIR(inode->i_mode))
8430 +               btail = au_dbtaildir(dentry);
8431 +       for (bindex = bstart; bindex <= btail; bindex++) {
8432 +               h_dentry = au_h_dptr(dentry, bindex);
8433 +               if (!h_dentry)
8434 +                       continue;
8435 +
8436 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8437 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8438 +               spin_lock(&h_dentry->d_lock);
8439 +               h_name = &h_dentry->d_name;
8440 +               if (unlikely(do_udba
8441 +                            && !is_root
8442 +                            && ((!h_nfs
8443 +                                 && (unhashed != !!d_unhashed(h_dentry)
8444 +                                     || (!tmpfile
8445 +                                         && !au_qstreq(name, h_name))
8446 +                                         ))
8447 +                                || (h_nfs
8448 +                                    && !(flags & LOOKUP_OPEN)
8449 +                                    && (h_dentry->d_flags
8450 +                                        & DCACHE_NFSFS_RENAMED)))
8451 +                           )) {
8452 +                       int h_unhashed;
8453 +
8454 +                       h_unhashed = d_unhashed(h_dentry);
8455 +                       spin_unlock(&h_dentry->d_lock);
8456 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8457 +                             unhashed, h_unhashed, dentry, h_dentry);
8458 +                       goto err;
8459 +               }
8460 +               spin_unlock(&h_dentry->d_lock);
8461 +
8462 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8463 +               if (unlikely(err))
8464 +                       /* do not goto err, to keep the errno */
8465 +                       break;
8466 +
8467 +               /* todo: plink too? */
8468 +               if (!do_udba)
8469 +                       continue;
8470 +
8471 +               /* UDBA tests */
8472 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8473 +                       goto err;
8474 +
8475 +               h_inode = NULL;
8476 +               if (d_is_positive(h_dentry))
8477 +                       h_inode = d_inode(h_dentry);
8478 +               h_plus = plus;
8479 +               h_mode = mode;
8480 +               h_cached_inode = h_inode;
8481 +               if (h_inode) {
8482 +                       h_mode = (h_inode->i_mode & S_IFMT);
8483 +                       h_plus = (h_inode->i_nlink > 0);
8484 +               }
8485 +               if (inode && ibs <= bindex && bindex <= ibe)
8486 +                       h_cached_inode = au_h_iptr(inode, bindex);
8487 +
8488 +               if (!h_nfs) {
8489 +                       if (unlikely(plus != h_plus && !tmpfile))
8490 +                               goto err;
8491 +               } else {
8492 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8493 +                                    && !is_root
8494 +                                    && !IS_ROOT(h_dentry)
8495 +                                    && unhashed != d_unhashed(h_dentry)))
8496 +                               goto err;
8497 +               }
8498 +               if (unlikely(mode != h_mode
8499 +                            || h_cached_inode != h_inode))
8500 +                       goto err;
8501 +               continue;
8502 +
8503 +err:
8504 +               err = -EINVAL;
8505 +               break;
8506 +       }
8507 +
8508 +       AuTraceErr(err);
8509 +       return err;
8510 +}
8511 +
8512 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8513 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8514 +{
8515 +       int err;
8516 +       struct dentry *parent;
8517 +
8518 +       if (!au_digen_test(dentry, sigen))
8519 +               return 0;
8520 +
8521 +       parent = dget_parent(dentry);
8522 +       di_read_lock_parent(parent, AuLock_IR);
8523 +       AuDebugOn(au_digen_test(parent, sigen));
8524 +       au_dbg_verify_gen(parent, sigen);
8525 +       err = au_refresh_dentry(dentry, parent);
8526 +       di_read_unlock(parent, AuLock_IR);
8527 +       dput(parent);
8528 +       AuTraceErr(err);
8529 +       return err;
8530 +}
8531 +
8532 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8533 +{
8534 +       int err;
8535 +       struct dentry *d, *parent;
8536 +
8537 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8538 +               return simple_reval_dpath(dentry, sigen);
8539 +
8540 +       /* slow loop, keep it simple and stupid */
8541 +       /* cf: au_cpup_dirs() */
8542 +       err = 0;
8543 +       parent = NULL;
8544 +       while (au_digen_test(dentry, sigen)) {
8545 +               d = dentry;
8546 +               while (1) {
8547 +                       dput(parent);
8548 +                       parent = dget_parent(d);
8549 +                       if (!au_digen_test(parent, sigen))
8550 +                               break;
8551 +                       d = parent;
8552 +               }
8553 +
8554 +               if (d != dentry)
8555 +                       di_write_lock_child2(d);
8556 +
8557 +               /* someone might update our dentry while we were sleeping */
8558 +               if (au_digen_test(d, sigen)) {
8559 +                       /*
8560 +                        * todo: consolidate with simple_reval_dpath(),
8561 +                        * do_refresh() and au_reval_for_attr().
8562 +                        */
8563 +                       di_read_lock_parent(parent, AuLock_IR);
8564 +                       err = au_refresh_dentry(d, parent);
8565 +                       di_read_unlock(parent, AuLock_IR);
8566 +               }
8567 +
8568 +               if (d != dentry)
8569 +                       di_write_unlock(d);
8570 +               dput(parent);
8571 +               if (unlikely(err))
8572 +                       break;
8573 +       }
8574 +
8575 +       return err;
8576 +}
8577 +
8578 +/*
8579 + * if valid returns 1, otherwise 0.
8580 + */
8581 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8582 +{
8583 +       int valid, err;
8584 +       unsigned int sigen;
8585 +       unsigned char do_udba;
8586 +       struct super_block *sb;
8587 +       struct inode *inode;
8588 +
8589 +       /* todo: support rcu-walk? */
8590 +       if (flags & LOOKUP_RCU)
8591 +               return -ECHILD;
8592 +
8593 +       valid = 0;
8594 +       if (unlikely(!au_di(dentry)))
8595 +               goto out;
8596 +
8597 +       valid = 1;
8598 +       sb = dentry->d_sb;
8599 +       /*
8600 +        * todo: very ugly
8601 +        * i_mutex of parent dir may be held,
8602 +        * but we should not return 'invalid' due to busy.
8603 +        */
8604 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8605 +       if (unlikely(err)) {
8606 +               valid = err;
8607 +               AuTraceErr(err);
8608 +               goto out;
8609 +       }
8610 +       inode = NULL;
8611 +       if (d_really_is_positive(dentry))
8612 +               inode = d_inode(dentry);
8613 +       if (unlikely(inode && is_bad_inode(inode))) {
8614 +               err = -EINVAL;
8615 +               AuTraceErr(err);
8616 +               goto out_dgrade;
8617 +       }
8618 +       if (unlikely(au_dbrange_test(dentry))) {
8619 +               err = -EINVAL;
8620 +               AuTraceErr(err);
8621 +               goto out_dgrade;
8622 +       }
8623 +
8624 +       sigen = au_sigen(sb);
8625 +       if (au_digen_test(dentry, sigen)) {
8626 +               AuDebugOn(IS_ROOT(dentry));
8627 +               err = au_reval_dpath(dentry, sigen);
8628 +               if (unlikely(err)) {
8629 +                       AuTraceErr(err);
8630 +                       goto out_dgrade;
8631 +               }
8632 +       }
8633 +       di_downgrade_lock(dentry, AuLock_IR);
8634 +
8635 +       err = -EINVAL;
8636 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8637 +           && inode
8638 +           && !(inode->i_state && I_LINKABLE)
8639 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8640 +               AuTraceErr(err);
8641 +               goto out_inval;
8642 +       }
8643 +
8644 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8645 +       if (do_udba && inode) {
8646 +               aufs_bindex_t bstart = au_ibstart(inode);
8647 +               struct inode *h_inode;
8648 +
8649 +               if (bstart >= 0) {
8650 +                       h_inode = au_h_iptr(inode, bstart);
8651 +                       if (h_inode && au_test_higen(inode, h_inode)) {
8652 +                               AuTraceErr(err);
8653 +                               goto out_inval;
8654 +                       }
8655 +               }
8656 +       }
8657 +
8658 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8659 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
8660 +               err = -EIO;
8661 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8662 +                     dentry, err);
8663 +       }
8664 +       goto out_inval;
8665 +
8666 +out_dgrade:
8667 +       di_downgrade_lock(dentry, AuLock_IR);
8668 +out_inval:
8669 +       aufs_read_unlock(dentry, AuLock_IR);
8670 +       AuTraceErr(err);
8671 +       valid = !err;
8672 +out:
8673 +       if (!valid) {
8674 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8675 +               d_drop(dentry);
8676 +       }
8677 +       return valid;
8678 +}
8679 +
8680 +static void aufs_d_release(struct dentry *dentry)
8681 +{
8682 +       if (au_di(dentry)) {
8683 +               au_di_fin(dentry);
8684 +               au_hn_di_reinit(dentry);
8685 +       }
8686 +}
8687 +
8688 +const struct dentry_operations aufs_dop = {
8689 +       .d_revalidate           = aufs_d_revalidate,
8690 +       .d_weak_revalidate      = aufs_d_revalidate,
8691 +       .d_release              = aufs_d_release
8692 +};
8693 +
8694 +/* aufs_dop without d_revalidate */
8695 +const struct dentry_operations aufs_dop_noreval = {
8696 +       .d_release              = aufs_d_release
8697 +};
8698 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8699 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8700 +++ linux/fs/aufs/dentry.h      2016-04-24 18:32:51.390353525 +0200
8701 @@ -0,0 +1,234 @@
8702 +/*
8703 + * Copyright (C) 2005-2016 Junjiro R. Okajima
8704 + *
8705 + * This program, aufs is free software; you can redistribute it and/or modify
8706 + * it under the terms of the GNU General Public License as published by
8707 + * the Free Software Foundation; either version 2 of the License, or
8708 + * (at your option) any later version.
8709 + *
8710 + * This program is distributed in the hope that it will be useful,
8711 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8712 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8713 + * GNU General Public License for more details.
8714 + *
8715 + * You should have received a copy of the GNU General Public License
8716 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8717 + */
8718 +
8719 +/*
8720 + * lookup and dentry operations
8721 + */
8722 +
8723 +#ifndef __AUFS_DENTRY_H__
8724 +#define __AUFS_DENTRY_H__
8725 +
8726 +#ifdef __KERNEL__
8727 +
8728 +#include <linux/dcache.h>
8729 +#include "rwsem.h"
8730 +
8731 +struct au_hdentry {
8732 +       struct dentry           *hd_dentry;
8733 +       aufs_bindex_t           hd_id;
8734 +};
8735 +
8736 +struct au_dinfo {
8737 +       atomic_t                di_generation;
8738 +
8739 +       struct au_rwsem         di_rwsem;
8740 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8741 +       unsigned char           di_tmpfile; /* to allow the different name */
8742 +       struct au_hdentry       *di_hdentry;
8743 +} ____cacheline_aligned_in_smp;
8744 +
8745 +/* ---------------------------------------------------------------------- */
8746 +
8747 +/* dentry.c */
8748 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
8749 +struct au_branch;
8750 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8751 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8752 +               struct dentry *h_parent, struct au_branch *br);
8753 +
8754 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8755 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8756 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8757 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8758 +void au_refresh_dop(struct dentry *dentry, int force_reval);
8759 +
8760 +/* dinfo.c */
8761 +void au_di_init_once(void *_di);
8762 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8763 +void au_di_free(struct au_dinfo *dinfo);
8764 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8765 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8766 +int au_di_init(struct dentry *dentry);
8767 +void au_di_fin(struct dentry *dentry);
8768 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8769 +
8770 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8771 +void di_read_unlock(struct dentry *d, int flags);
8772 +void di_downgrade_lock(struct dentry *d, int flags);
8773 +void di_write_lock(struct dentry *d, unsigned int lsc);
8774 +void di_write_unlock(struct dentry *d);
8775 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8776 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8777 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8778 +
8779 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8780 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8781 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8782 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8783 +
8784 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8785 +                  struct dentry *h_dentry);
8786 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8787 +int au_dbrange_test(struct dentry *dentry);
8788 +void au_update_digen(struct dentry *dentry);
8789 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8790 +void au_update_dbstart(struct dentry *dentry);
8791 +void au_update_dbend(struct dentry *dentry);
8792 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8793 +
8794 +/* ---------------------------------------------------------------------- */
8795 +
8796 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8797 +{
8798 +       return dentry->d_fsdata;
8799 +}
8800 +
8801 +/* ---------------------------------------------------------------------- */
8802 +
8803 +/* lock subclass for dinfo */
8804 +enum {
8805 +       AuLsc_DI_CHILD,         /* child first */
8806 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8807 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8808 +       AuLsc_DI_PARENT,
8809 +       AuLsc_DI_PARENT2,
8810 +       AuLsc_DI_PARENT3,
8811 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8812 +};
8813 +
8814 +/*
8815 + * di_read_lock_child, di_write_lock_child,
8816 + * di_read_lock_child2, di_write_lock_child2,
8817 + * di_read_lock_child3, di_write_lock_child3,
8818 + * di_read_lock_parent, di_write_lock_parent,
8819 + * di_read_lock_parent2, di_write_lock_parent2,
8820 + * di_read_lock_parent3, di_write_lock_parent3,
8821 + */
8822 +#define AuReadLockFunc(name, lsc) \
8823 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8824 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8825 +
8826 +#define AuWriteLockFunc(name, lsc) \
8827 +static inline void di_write_lock_##name(struct dentry *d) \
8828 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8829 +
8830 +#define AuRWLockFuncs(name, lsc) \
8831 +       AuReadLockFunc(name, lsc) \
8832 +       AuWriteLockFunc(name, lsc)
8833 +
8834 +AuRWLockFuncs(child, CHILD);
8835 +AuRWLockFuncs(child2, CHILD2);
8836 +AuRWLockFuncs(child3, CHILD3);
8837 +AuRWLockFuncs(parent, PARENT);
8838 +AuRWLockFuncs(parent2, PARENT2);
8839 +AuRWLockFuncs(parent3, PARENT3);
8840 +
8841 +#undef AuReadLockFunc
8842 +#undef AuWriteLockFunc
8843 +#undef AuRWLockFuncs
8844 +
8845 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8846 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8847 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8848 +
8849 +/* ---------------------------------------------------------------------- */
8850 +
8851 +/* todo: memory barrier? */
8852 +static inline unsigned int au_digen(struct dentry *d)
8853 +{
8854 +       return atomic_read(&au_di(d)->di_generation);
8855 +}
8856 +
8857 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8858 +{
8859 +       hdentry->hd_dentry = NULL;
8860 +}
8861 +
8862 +static inline void au_hdput(struct au_hdentry *hd)
8863 +{
8864 +       if (hd)
8865 +               dput(hd->hd_dentry);
8866 +}
8867 +
8868 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8869 +{
8870 +       DiMustAnyLock(dentry);
8871 +       return au_di(dentry)->di_bstart;
8872 +}
8873 +
8874 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8875 +{
8876 +       DiMustAnyLock(dentry);
8877 +       return au_di(dentry)->di_bend;
8878 +}
8879 +
8880 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8881 +{
8882 +       DiMustAnyLock(dentry);
8883 +       return au_di(dentry)->di_bwh;
8884 +}
8885 +
8886 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8887 +{
8888 +       DiMustAnyLock(dentry);
8889 +       return au_di(dentry)->di_bdiropq;
8890 +}
8891 +
8892 +/* todo: hard/soft set? */
8893 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8894 +{
8895 +       DiMustWriteLock(dentry);
8896 +       au_di(dentry)->di_bstart = bindex;
8897 +}
8898 +
8899 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8900 +{
8901 +       DiMustWriteLock(dentry);
8902 +       au_di(dentry)->di_bend = bindex;
8903 +}
8904 +
8905 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8906 +{
8907 +       DiMustWriteLock(dentry);
8908 +       /* dbwh can be outside of bstart - bend range */
8909 +       au_di(dentry)->di_bwh = bindex;
8910 +}
8911 +
8912 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8913 +{
8914 +       DiMustWriteLock(dentry);
8915 +       au_di(dentry)->di_bdiropq = bindex;
8916 +}
8917 +
8918 +/* ---------------------------------------------------------------------- */
8919 +
8920 +#ifdef CONFIG_AUFS_HNOTIFY
8921 +static inline void au_digen_dec(struct dentry *d)
8922 +{
8923 +       atomic_dec(&au_di(d)->di_generation);
8924 +}
8925 +
8926 +static inline void au_hn_di_reinit(struct dentry *dentry)
8927 +{
8928 +       dentry->d_fsdata = NULL;
8929 +}
8930 +#else
8931 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8932 +#endif /* CONFIG_AUFS_HNOTIFY */
8933 +
8934 +#endif /* __KERNEL__ */
8935 +#endif /* __AUFS_DENTRY_H__ */
8936 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8937 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8938 +++ linux/fs/aufs/dinfo.c       2016-04-24 18:32:51.390353525 +0200
8939 @@ -0,0 +1,550 @@
8940 +/*
8941 + * Copyright (C) 2005-2016 Junjiro R. Okajima
8942 + *
8943 + * This program, aufs is free software; you can redistribute it and/or modify
8944 + * it under the terms of the GNU General Public License as published by
8945 + * the Free Software Foundation; either version 2 of the License, or
8946 + * (at your option) any later version.
8947 + *
8948 + * This program is distributed in the hope that it will be useful,
8949 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8950 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8951 + * GNU General Public License for more details.
8952 + *
8953 + * You should have received a copy of the GNU General Public License
8954 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8955 + */
8956 +
8957 +/*
8958 + * dentry private data
8959 + */
8960 +
8961 +#include "aufs.h"
8962 +
8963 +void au_di_init_once(void *_dinfo)
8964 +{
8965 +       struct au_dinfo *dinfo = _dinfo;
8966 +       static struct lock_class_key aufs_di;
8967 +
8968 +       au_rw_init(&dinfo->di_rwsem);
8969 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8970 +}
8971 +
8972 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8973 +{
8974 +       struct au_dinfo *dinfo;
8975 +       int nbr, i;
8976 +
8977 +       dinfo = au_cache_alloc_dinfo();
8978 +       if (unlikely(!dinfo))
8979 +               goto out;
8980 +
8981 +       nbr = au_sbend(sb) + 1;
8982 +       if (nbr <= 0)
8983 +               nbr = 1;
8984 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8985 +       if (dinfo->di_hdentry) {
8986 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8987 +               dinfo->di_bstart = -1;
8988 +               dinfo->di_bend = -1;
8989 +               dinfo->di_bwh = -1;
8990 +               dinfo->di_bdiropq = -1;
8991 +               dinfo->di_tmpfile = 0;
8992 +               for (i = 0; i < nbr; i++)
8993 +                       dinfo->di_hdentry[i].hd_id = -1;
8994 +               goto out;
8995 +       }
8996 +
8997 +       au_cache_free_dinfo(dinfo);
8998 +       dinfo = NULL;
8999 +
9000 +out:
9001 +       return dinfo;
9002 +}
9003 +
9004 +void au_di_free(struct au_dinfo *dinfo)
9005 +{
9006 +       struct au_hdentry *p;
9007 +       aufs_bindex_t bend, bindex;
9008 +
9009 +       /* dentry may not be revalidated */
9010 +       bindex = dinfo->di_bstart;
9011 +       if (bindex >= 0) {
9012 +               bend = dinfo->di_bend;
9013 +               p = dinfo->di_hdentry + bindex;
9014 +               while (bindex++ <= bend)
9015 +                       au_hdput(p++);
9016 +       }
9017 +       kfree(dinfo->di_hdentry);
9018 +       au_cache_free_dinfo(dinfo);
9019 +}
9020 +
9021 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9022 +{
9023 +       struct au_hdentry *p;
9024 +       aufs_bindex_t bi;
9025 +
9026 +       AuRwMustWriteLock(&a->di_rwsem);
9027 +       AuRwMustWriteLock(&b->di_rwsem);
9028 +
9029 +#define DiSwap(v, name)                                \
9030 +       do {                                    \
9031 +               v = a->di_##name;               \
9032 +               a->di_##name = b->di_##name;    \
9033 +               b->di_##name = v;               \
9034 +       } while (0)
9035 +
9036 +       DiSwap(p, hdentry);
9037 +       DiSwap(bi, bstart);
9038 +       DiSwap(bi, bend);
9039 +       DiSwap(bi, bwh);
9040 +       DiSwap(bi, bdiropq);
9041 +       /* smp_mb(); */
9042 +
9043 +#undef DiSwap
9044 +}
9045 +
9046 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9047 +{
9048 +       AuRwMustWriteLock(&dst->di_rwsem);
9049 +       AuRwMustWriteLock(&src->di_rwsem);
9050 +
9051 +       dst->di_bstart = src->di_bstart;
9052 +       dst->di_bend = src->di_bend;
9053 +       dst->di_bwh = src->di_bwh;
9054 +       dst->di_bdiropq = src->di_bdiropq;
9055 +       /* smp_mb(); */
9056 +}
9057 +
9058 +int au_di_init(struct dentry *dentry)
9059 +{
9060 +       int err;
9061 +       struct super_block *sb;
9062 +       struct au_dinfo *dinfo;
9063 +
9064 +       err = 0;
9065 +       sb = dentry->d_sb;
9066 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9067 +       if (dinfo) {
9068 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9069 +               /* smp_mb(); */ /* atomic_set */
9070 +               dentry->d_fsdata = dinfo;
9071 +       } else
9072 +               err = -ENOMEM;
9073 +
9074 +       return err;
9075 +}
9076 +
9077 +void au_di_fin(struct dentry *dentry)
9078 +{
9079 +       struct au_dinfo *dinfo;
9080 +
9081 +       dinfo = au_di(dentry);
9082 +       AuRwDestroy(&dinfo->di_rwsem);
9083 +       au_di_free(dinfo);
9084 +}
9085 +
9086 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
9087 +{
9088 +       int err, sz;
9089 +       struct au_hdentry *hdp;
9090 +
9091 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9092 +
9093 +       err = -ENOMEM;
9094 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
9095 +       if (!sz)
9096 +               sz = sizeof(*hdp);
9097 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
9098 +       if (hdp) {
9099 +               dinfo->di_hdentry = hdp;
9100 +               err = 0;
9101 +       }
9102 +
9103 +       return err;
9104 +}
9105 +
9106 +/* ---------------------------------------------------------------------- */
9107 +
9108 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9109 +{
9110 +       switch (lsc) {
9111 +       case AuLsc_DI_CHILD:
9112 +               ii_write_lock_child(inode);
9113 +               break;
9114 +       case AuLsc_DI_CHILD2:
9115 +               ii_write_lock_child2(inode);
9116 +               break;
9117 +       case AuLsc_DI_CHILD3:
9118 +               ii_write_lock_child3(inode);
9119 +               break;
9120 +       case AuLsc_DI_PARENT:
9121 +               ii_write_lock_parent(inode);
9122 +               break;
9123 +       case AuLsc_DI_PARENT2:
9124 +               ii_write_lock_parent2(inode);
9125 +               break;
9126 +       case AuLsc_DI_PARENT3:
9127 +               ii_write_lock_parent3(inode);
9128 +               break;
9129 +       default:
9130 +               BUG();
9131 +       }
9132 +}
9133 +
9134 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9135 +{
9136 +       switch (lsc) {
9137 +       case AuLsc_DI_CHILD:
9138 +               ii_read_lock_child(inode);
9139 +               break;
9140 +       case AuLsc_DI_CHILD2:
9141 +               ii_read_lock_child2(inode);
9142 +               break;
9143 +       case AuLsc_DI_CHILD3:
9144 +               ii_read_lock_child3(inode);
9145 +               break;
9146 +       case AuLsc_DI_PARENT:
9147 +               ii_read_lock_parent(inode);
9148 +               break;
9149 +       case AuLsc_DI_PARENT2:
9150 +               ii_read_lock_parent2(inode);
9151 +               break;
9152 +       case AuLsc_DI_PARENT3:
9153 +               ii_read_lock_parent3(inode);
9154 +               break;
9155 +       default:
9156 +               BUG();
9157 +       }
9158 +}
9159 +
9160 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9161 +{
9162 +       struct inode *inode;
9163 +
9164 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9165 +       if (d_really_is_positive(d)) {
9166 +               inode = d_inode(d);
9167 +               if (au_ftest_lock(flags, IW))
9168 +                       do_ii_write_lock(inode, lsc);
9169 +               else if (au_ftest_lock(flags, IR))
9170 +                       do_ii_read_lock(inode, lsc);
9171 +       }
9172 +}
9173 +
9174 +void di_read_unlock(struct dentry *d, int flags)
9175 +{
9176 +       struct inode *inode;
9177 +
9178 +       if (d_really_is_positive(d)) {
9179 +               inode = d_inode(d);
9180 +               if (au_ftest_lock(flags, IW)) {
9181 +                       au_dbg_verify_dinode(d);
9182 +                       ii_write_unlock(inode);
9183 +               } else if (au_ftest_lock(flags, IR)) {
9184 +                       au_dbg_verify_dinode(d);
9185 +                       ii_read_unlock(inode);
9186 +               }
9187 +       }
9188 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9189 +}
9190 +
9191 +void di_downgrade_lock(struct dentry *d, int flags)
9192 +{
9193 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9194 +               ii_downgrade_lock(d_inode(d));
9195 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9196 +}
9197 +
9198 +void di_write_lock(struct dentry *d, unsigned int lsc)
9199 +{
9200 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9201 +       if (d_really_is_positive(d))
9202 +               do_ii_write_lock(d_inode(d), lsc);
9203 +}
9204 +
9205 +void di_write_unlock(struct dentry *d)
9206 +{
9207 +       au_dbg_verify_dinode(d);
9208 +       if (d_really_is_positive(d))
9209 +               ii_write_unlock(d_inode(d));
9210 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9211 +}
9212 +
9213 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9214 +{
9215 +       AuDebugOn(d1 == d2
9216 +                 || d_inode(d1) == d_inode(d2)
9217 +                 || d1->d_sb != d2->d_sb);
9218 +
9219 +       if (isdir && au_test_subdir(d1, d2)) {
9220 +               di_write_lock_child(d1);
9221 +               di_write_lock_child2(d2);
9222 +       } else {
9223 +               /* there should be no races */
9224 +               di_write_lock_child(d2);
9225 +               di_write_lock_child2(d1);
9226 +       }
9227 +}
9228 +
9229 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9230 +{
9231 +       AuDebugOn(d1 == d2
9232 +                 || d_inode(d1) == d_inode(d2)
9233 +                 || d1->d_sb != d2->d_sb);
9234 +
9235 +       if (isdir && au_test_subdir(d1, d2)) {
9236 +               di_write_lock_parent(d1);
9237 +               di_write_lock_parent2(d2);
9238 +       } else {
9239 +               /* there should be no races */
9240 +               di_write_lock_parent(d2);
9241 +               di_write_lock_parent2(d1);
9242 +       }
9243 +}
9244 +
9245 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9246 +{
9247 +       di_write_unlock(d1);
9248 +       if (d_inode(d1) == d_inode(d2))
9249 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9250 +       else
9251 +               di_write_unlock(d2);
9252 +}
9253 +
9254 +/* ---------------------------------------------------------------------- */
9255 +
9256 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9257 +{
9258 +       struct dentry *d;
9259 +
9260 +       DiMustAnyLock(dentry);
9261 +
9262 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
9263 +               return NULL;
9264 +       AuDebugOn(bindex < 0);
9265 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
9266 +       AuDebugOn(d && au_dcount(d) <= 0);
9267 +       return d;
9268 +}
9269 +
9270 +/*
9271 + * extended version of au_h_dptr().
9272 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9273 + * error.
9274 + */
9275 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9276 +{
9277 +       struct dentry *h_dentry;
9278 +       struct inode *inode, *h_inode;
9279 +
9280 +       AuDebugOn(d_really_is_negative(dentry));
9281 +
9282 +       h_dentry = NULL;
9283 +       if (au_dbstart(dentry) <= bindex
9284 +           && bindex <= au_dbend(dentry))
9285 +               h_dentry = au_h_dptr(dentry, bindex);
9286 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9287 +               dget(h_dentry);
9288 +               goto out; /* success */
9289 +       }
9290 +
9291 +       inode = d_inode(dentry);
9292 +       AuDebugOn(bindex < au_ibstart(inode));
9293 +       AuDebugOn(au_ibend(inode) < bindex);
9294 +       h_inode = au_h_iptr(inode, bindex);
9295 +       h_dentry = d_find_alias(h_inode);
9296 +       if (h_dentry) {
9297 +               if (!IS_ERR(h_dentry)) {
9298 +                       if (!au_d_linkable(h_dentry))
9299 +                               goto out; /* success */
9300 +                       dput(h_dentry);
9301 +               } else
9302 +                       goto out;
9303 +       }
9304 +
9305 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9306 +               h_dentry = au_plink_lkup(inode, bindex);
9307 +               AuDebugOn(!h_dentry);
9308 +               if (!IS_ERR(h_dentry)) {
9309 +                       if (!au_d_hashed_positive(h_dentry))
9310 +                               goto out; /* success */
9311 +                       dput(h_dentry);
9312 +                       h_dentry = NULL;
9313 +               }
9314 +       }
9315 +
9316 +out:
9317 +       AuDbgDentry(h_dentry);
9318 +       return h_dentry;
9319 +}
9320 +
9321 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9322 +{
9323 +       aufs_bindex_t bend, bwh;
9324 +
9325 +       bend = au_dbend(dentry);
9326 +       if (0 <= bend) {
9327 +               bwh = au_dbwh(dentry);
9328 +               if (!bwh)
9329 +                       return bwh;
9330 +               if (0 < bwh && bwh < bend)
9331 +                       return bwh - 1;
9332 +       }
9333 +       return bend;
9334 +}
9335 +
9336 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9337 +{
9338 +       aufs_bindex_t bend, bopq;
9339 +
9340 +       bend = au_dbtail(dentry);
9341 +       if (0 <= bend) {
9342 +               bopq = au_dbdiropq(dentry);
9343 +               if (0 <= bopq && bopq < bend)
9344 +                       bend = bopq;
9345 +       }
9346 +       return bend;
9347 +}
9348 +
9349 +/* ---------------------------------------------------------------------- */
9350 +
9351 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9352 +                  struct dentry *h_dentry)
9353 +{
9354 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
9355 +       struct au_branch *br;
9356 +
9357 +       DiMustWriteLock(dentry);
9358 +
9359 +       au_hdput(hd);
9360 +       hd->hd_dentry = h_dentry;
9361 +       if (h_dentry) {
9362 +               br = au_sbr(dentry->d_sb, bindex);
9363 +               hd->hd_id = br->br_id;
9364 +       }
9365 +}
9366 +
9367 +int au_dbrange_test(struct dentry *dentry)
9368 +{
9369 +       int err;
9370 +       aufs_bindex_t bstart, bend;
9371 +
9372 +       err = 0;
9373 +       bstart = au_dbstart(dentry);
9374 +       bend = au_dbend(dentry);
9375 +       if (bstart >= 0)
9376 +               AuDebugOn(bend < 0 && bstart > bend);
9377 +       else {
9378 +               err = -EIO;
9379 +               AuDebugOn(bend >= 0);
9380 +       }
9381 +
9382 +       return err;
9383 +}
9384 +
9385 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9386 +{
9387 +       int err;
9388 +
9389 +       err = 0;
9390 +       if (unlikely(au_digen(dentry) != sigen
9391 +                    || au_iigen_test(d_inode(dentry), sigen)))
9392 +               err = -EIO;
9393 +
9394 +       return err;
9395 +}
9396 +
9397 +void au_update_digen(struct dentry *dentry)
9398 +{
9399 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9400 +       /* smp_mb(); */ /* atomic_set */
9401 +}
9402 +
9403 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9404 +{
9405 +       struct au_dinfo *dinfo;
9406 +       struct dentry *h_d;
9407 +       struct au_hdentry *hdp;
9408 +
9409 +       DiMustWriteLock(dentry);
9410 +
9411 +       dinfo = au_di(dentry);
9412 +       if (!dinfo || dinfo->di_bstart < 0)
9413 +               return;
9414 +
9415 +       hdp = dinfo->di_hdentry;
9416 +       if (do_put_zero) {
9417 +               aufs_bindex_t bindex, bend;
9418 +
9419 +               bend = dinfo->di_bend;
9420 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
9421 +                       h_d = hdp[0 + bindex].hd_dentry;
9422 +                       if (h_d && d_is_negative(h_d))
9423 +                               au_set_h_dptr(dentry, bindex, NULL);
9424 +               }
9425 +       }
9426 +
9427 +       dinfo->di_bstart = -1;
9428 +       while (++dinfo->di_bstart <= dinfo->di_bend)
9429 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
9430 +                       break;
9431 +       if (dinfo->di_bstart > dinfo->di_bend) {
9432 +               dinfo->di_bstart = -1;
9433 +               dinfo->di_bend = -1;
9434 +               return;
9435 +       }
9436 +
9437 +       dinfo->di_bend++;
9438 +       while (0 <= --dinfo->di_bend)
9439 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
9440 +                       break;
9441 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
9442 +}
9443 +
9444 +void au_update_dbstart(struct dentry *dentry)
9445 +{
9446 +       aufs_bindex_t bindex, bend;
9447 +       struct dentry *h_dentry;
9448 +
9449 +       bend = au_dbend(dentry);
9450 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
9451 +               h_dentry = au_h_dptr(dentry, bindex);
9452 +               if (!h_dentry)
9453 +                       continue;
9454 +               if (d_is_positive(h_dentry)) {
9455 +                       au_set_dbstart(dentry, bindex);
9456 +                       return;
9457 +               }
9458 +               au_set_h_dptr(dentry, bindex, NULL);
9459 +       }
9460 +}
9461 +
9462 +void au_update_dbend(struct dentry *dentry)
9463 +{
9464 +       aufs_bindex_t bindex, bstart;
9465 +       struct dentry *h_dentry;
9466 +
9467 +       bstart = au_dbstart(dentry);
9468 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
9469 +               h_dentry = au_h_dptr(dentry, bindex);
9470 +               if (!h_dentry)
9471 +                       continue;
9472 +               if (d_is_positive(h_dentry)) {
9473 +                       au_set_dbend(dentry, bindex);
9474 +                       return;
9475 +               }
9476 +               au_set_h_dptr(dentry, bindex, NULL);
9477 +       }
9478 +}
9479 +
9480 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9481 +{
9482 +       aufs_bindex_t bindex, bend;
9483 +
9484 +       bend = au_dbend(dentry);
9485 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
9486 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9487 +                       return bindex;
9488 +       return -1;
9489 +}
9490 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9491 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9492 +++ linux/fs/aufs/dir.c 2016-04-24 18:32:51.390353525 +0200
9493 @@ -0,0 +1,756 @@
9494 +/*
9495 + * Copyright (C) 2005-2016 Junjiro R. Okajima
9496 + *
9497 + * This program, aufs is free software; you can redistribute it and/or modify
9498 + * it under the terms of the GNU General Public License as published by
9499 + * the Free Software Foundation; either version 2 of the License, or
9500 + * (at your option) any later version.
9501 + *
9502 + * This program is distributed in the hope that it will be useful,
9503 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9504 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9505 + * GNU General Public License for more details.
9506 + *
9507 + * You should have received a copy of the GNU General Public License
9508 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9509 + */
9510 +
9511 +/*
9512 + * directory operations
9513 + */
9514 +
9515 +#include <linux/fs_stack.h>
9516 +#include "aufs.h"
9517 +
9518 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9519 +{
9520 +       unsigned int nlink;
9521 +
9522 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9523 +
9524 +       nlink = dir->i_nlink;
9525 +       nlink += h_dir->i_nlink - 2;
9526 +       if (h_dir->i_nlink < 2)
9527 +               nlink += 2;
9528 +       smp_mb(); /* for i_nlink */
9529 +       /* 0 can happen in revaliding */
9530 +       set_nlink(dir, nlink);
9531 +}
9532 +
9533 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9534 +{
9535 +       unsigned int nlink;
9536 +
9537 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9538 +
9539 +       nlink = dir->i_nlink;
9540 +       nlink -= h_dir->i_nlink - 2;
9541 +       if (h_dir->i_nlink < 2)
9542 +               nlink -= 2;
9543 +       smp_mb(); /* for i_nlink */
9544 +       /* nlink == 0 means the branch-fs is broken */
9545 +       set_nlink(dir, nlink);
9546 +}
9547 +
9548 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9549 +{
9550 +       loff_t sz;
9551 +       aufs_bindex_t bindex, bend;
9552 +       struct file *h_file;
9553 +       struct dentry *h_dentry;
9554 +
9555 +       sz = 0;
9556 +       if (file) {
9557 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9558 +
9559 +               bend = au_fbend_dir(file);
9560 +               for (bindex = au_fbstart(file);
9561 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9562 +                    bindex++) {
9563 +                       h_file = au_hf_dir(file, bindex);
9564 +                       if (h_file && file_inode(h_file))
9565 +                               sz += vfsub_f_size_read(h_file);
9566 +               }
9567 +       } else {
9568 +               AuDebugOn(!dentry);
9569 +               AuDebugOn(!d_is_dir(dentry));
9570 +
9571 +               bend = au_dbtaildir(dentry);
9572 +               for (bindex = au_dbstart(dentry);
9573 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9574 +                    bindex++) {
9575 +                       h_dentry = au_h_dptr(dentry, bindex);
9576 +                       if (h_dentry && d_is_positive(h_dentry))
9577 +                               sz += i_size_read(d_inode(h_dentry));
9578 +               }
9579 +       }
9580 +       if (sz < KMALLOC_MAX_SIZE)
9581 +               sz = roundup_pow_of_two(sz);
9582 +       if (sz > KMALLOC_MAX_SIZE)
9583 +               sz = KMALLOC_MAX_SIZE;
9584 +       else if (sz < NAME_MAX) {
9585 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9586 +               sz = AUFS_RDBLK_DEF;
9587 +       }
9588 +       return sz;
9589 +}
9590 +
9591 +struct au_dir_ts_arg {
9592 +       struct dentry *dentry;
9593 +       aufs_bindex_t brid;
9594 +};
9595 +
9596 +static void au_do_dir_ts(void *arg)
9597 +{
9598 +       struct au_dir_ts_arg *a = arg;
9599 +       struct au_dtime dt;
9600 +       struct path h_path;
9601 +       struct inode *dir, *h_dir;
9602 +       struct super_block *sb;
9603 +       struct au_branch *br;
9604 +       struct au_hinode *hdir;
9605 +       int err;
9606 +       aufs_bindex_t bstart, bindex;
9607 +
9608 +       sb = a->dentry->d_sb;
9609 +       if (d_really_is_negative(a->dentry))
9610 +               goto out;
9611 +       /* no dir->i_mutex lock */
9612 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9613 +
9614 +       dir = d_inode(a->dentry);
9615 +       bstart = au_ibstart(dir);
9616 +       bindex = au_br_index(sb, a->brid);
9617 +       if (bindex < bstart)
9618 +               goto out_unlock;
9619 +
9620 +       br = au_sbr(sb, bindex);
9621 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9622 +       if (!h_path.dentry)
9623 +               goto out_unlock;
9624 +       h_path.mnt = au_br_mnt(br);
9625 +       au_dtime_store(&dt, a->dentry, &h_path);
9626 +
9627 +       br = au_sbr(sb, bstart);
9628 +       if (!au_br_writable(br->br_perm))
9629 +               goto out_unlock;
9630 +       h_path.dentry = au_h_dptr(a->dentry, bstart);
9631 +       h_path.mnt = au_br_mnt(br);
9632 +       err = vfsub_mnt_want_write(h_path.mnt);
9633 +       if (err)
9634 +               goto out_unlock;
9635 +       hdir = au_hi(dir, bstart);
9636 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
9637 +       h_dir = au_h_iptr(dir, bstart);
9638 +       if (h_dir->i_nlink
9639 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9640 +               dt.dt_h_path = h_path;
9641 +               au_dtime_revert(&dt);
9642 +       }
9643 +       au_hn_imtx_unlock(hdir);
9644 +       vfsub_mnt_drop_write(h_path.mnt);
9645 +       au_cpup_attr_timesizes(dir);
9646 +
9647 +out_unlock:
9648 +       aufs_read_unlock(a->dentry, AuLock_DW);
9649 +out:
9650 +       dput(a->dentry);
9651 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9652 +       kfree(arg);
9653 +}
9654 +
9655 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9656 +{
9657 +       int perm, wkq_err;
9658 +       aufs_bindex_t bstart;
9659 +       struct au_dir_ts_arg *arg;
9660 +       struct dentry *dentry;
9661 +       struct super_block *sb;
9662 +
9663 +       IMustLock(dir);
9664 +
9665 +       dentry = d_find_any_alias(dir);
9666 +       AuDebugOn(!dentry);
9667 +       sb = dentry->d_sb;
9668 +       bstart = au_ibstart(dir);
9669 +       if (bstart == bindex) {
9670 +               au_cpup_attr_timesizes(dir);
9671 +               goto out;
9672 +       }
9673 +
9674 +       perm = au_sbr_perm(sb, bstart);
9675 +       if (!au_br_writable(perm))
9676 +               goto out;
9677 +
9678 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9679 +       if (!arg)
9680 +               goto out;
9681 +
9682 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9683 +       arg->brid = au_sbr_id(sb, bindex);
9684 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9685 +       if (unlikely(wkq_err)) {
9686 +               pr_err("wkq %d\n", wkq_err);
9687 +               dput(dentry);
9688 +               kfree(arg);
9689 +       }
9690 +
9691 +out:
9692 +       dput(dentry);
9693 +}
9694 +
9695 +/* ---------------------------------------------------------------------- */
9696 +
9697 +static int reopen_dir(struct file *file)
9698 +{
9699 +       int err;
9700 +       unsigned int flags;
9701 +       aufs_bindex_t bindex, btail, bstart;
9702 +       struct dentry *dentry, *h_dentry;
9703 +       struct file *h_file;
9704 +
9705 +       /* open all lower dirs */
9706 +       dentry = file->f_path.dentry;
9707 +       bstart = au_dbstart(dentry);
9708 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
9709 +               au_set_h_fptr(file, bindex, NULL);
9710 +       au_set_fbstart(file, bstart);
9711 +
9712 +       btail = au_dbtaildir(dentry);
9713 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
9714 +               au_set_h_fptr(file, bindex, NULL);
9715 +       au_set_fbend_dir(file, btail);
9716 +
9717 +       flags = vfsub_file_flags(file);
9718 +       for (bindex = bstart; bindex <= btail; bindex++) {
9719 +               h_dentry = au_h_dptr(dentry, bindex);
9720 +               if (!h_dentry)
9721 +                       continue;
9722 +               h_file = au_hf_dir(file, bindex);
9723 +               if (h_file)
9724 +                       continue;
9725 +
9726 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9727 +               err = PTR_ERR(h_file);
9728 +               if (IS_ERR(h_file))
9729 +                       goto out; /* close all? */
9730 +               au_set_h_fptr(file, bindex, h_file);
9731 +       }
9732 +       au_update_figen(file);
9733 +       /* todo: necessary? */
9734 +       /* file->f_ra = h_file->f_ra; */
9735 +       err = 0;
9736 +
9737 +out:
9738 +       return err;
9739 +}
9740 +
9741 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9742 +{
9743 +       int err;
9744 +       aufs_bindex_t bindex, btail;
9745 +       struct dentry *dentry, *h_dentry;
9746 +       struct vfsmount *mnt;
9747 +
9748 +       FiMustWriteLock(file);
9749 +       AuDebugOn(h_file);
9750 +
9751 +       err = 0;
9752 +       mnt = file->f_path.mnt;
9753 +       dentry = file->f_path.dentry;
9754 +       file->f_version = d_inode(dentry)->i_version;
9755 +       bindex = au_dbstart(dentry);
9756 +       au_set_fbstart(file, bindex);
9757 +       btail = au_dbtaildir(dentry);
9758 +       au_set_fbend_dir(file, btail);
9759 +       for (; !err && bindex <= btail; bindex++) {
9760 +               h_dentry = au_h_dptr(dentry, bindex);
9761 +               if (!h_dentry)
9762 +                       continue;
9763 +
9764 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9765 +               if (unlikely(err))
9766 +                       break;
9767 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9768 +               if (IS_ERR(h_file)) {
9769 +                       err = PTR_ERR(h_file);
9770 +                       break;
9771 +               }
9772 +               au_set_h_fptr(file, bindex, h_file);
9773 +       }
9774 +       au_update_figen(file);
9775 +       /* todo: necessary? */
9776 +       /* file->f_ra = h_file->f_ra; */
9777 +       if (!err)
9778 +               return 0; /* success */
9779 +
9780 +       /* close all */
9781 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
9782 +               au_set_h_fptr(file, bindex, NULL);
9783 +       au_set_fbstart(file, -1);
9784 +       au_set_fbend_dir(file, -1);
9785 +
9786 +       return err;
9787 +}
9788 +
9789 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9790 +                        struct file *file)
9791 +{
9792 +       int err;
9793 +       struct super_block *sb;
9794 +       struct au_fidir *fidir;
9795 +
9796 +       err = -ENOMEM;
9797 +       sb = file->f_path.dentry->d_sb;
9798 +       si_read_lock(sb, AuLock_FLUSH);
9799 +       fidir = au_fidir_alloc(sb);
9800 +       if (fidir) {
9801 +               struct au_do_open_args args = {
9802 +                       .open   = do_open_dir,
9803 +                       .fidir  = fidir
9804 +               };
9805 +               err = au_do_open(file, &args);
9806 +               if (unlikely(err))
9807 +                       kfree(fidir);
9808 +       }
9809 +       si_read_unlock(sb);
9810 +       return err;
9811 +}
9812 +
9813 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9814 +                           struct file *file)
9815 +{
9816 +       struct au_vdir *vdir_cache;
9817 +       struct au_finfo *finfo;
9818 +       struct au_fidir *fidir;
9819 +       aufs_bindex_t bindex, bend;
9820 +
9821 +       finfo = au_fi(file);
9822 +       fidir = finfo->fi_hdir;
9823 +       if (fidir) {
9824 +               au_sphl_del(&finfo->fi_hlist,
9825 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9826 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9827 +               if (vdir_cache)
9828 +                       au_vdir_free(vdir_cache);
9829 +
9830 +               bindex = finfo->fi_btop;
9831 +               if (bindex >= 0) {
9832 +                       /*
9833 +                        * calls fput() instead of filp_close(),
9834 +                        * since no dnotify or lock for the lower file.
9835 +                        */
9836 +                       bend = fidir->fd_bbot;
9837 +                       for (; bindex <= bend; bindex++)
9838 +                               au_set_h_fptr(file, bindex, NULL);
9839 +               }
9840 +               kfree(fidir);
9841 +               finfo->fi_hdir = NULL;
9842 +       }
9843 +       au_finfo_fin(file);
9844 +       return 0;
9845 +}
9846 +
9847 +/* ---------------------------------------------------------------------- */
9848 +
9849 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
9850 +{
9851 +       int err;
9852 +       aufs_bindex_t bindex, bend;
9853 +       struct file *h_file;
9854 +
9855 +       err = 0;
9856 +       bend = au_fbend_dir(file);
9857 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9858 +               h_file = au_hf_dir(file, bindex);
9859 +               if (h_file)
9860 +                       err = vfsub_flush(h_file, id);
9861 +       }
9862 +       return err;
9863 +}
9864 +
9865 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9866 +{
9867 +       return au_do_flush(file, id, au_do_flush_dir);
9868 +}
9869 +
9870 +/* ---------------------------------------------------------------------- */
9871 +
9872 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9873 +{
9874 +       int err;
9875 +       aufs_bindex_t bend, bindex;
9876 +       struct inode *inode;
9877 +       struct super_block *sb;
9878 +
9879 +       err = 0;
9880 +       sb = dentry->d_sb;
9881 +       inode = d_inode(dentry);
9882 +       IMustLock(inode);
9883 +       bend = au_dbend(dentry);
9884 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9885 +               struct path h_path;
9886 +
9887 +               if (au_test_ro(sb, bindex, inode))
9888 +                       continue;
9889 +               h_path.dentry = au_h_dptr(dentry, bindex);
9890 +               if (!h_path.dentry)
9891 +                       continue;
9892 +
9893 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9894 +               err = vfsub_fsync(NULL, &h_path, datasync);
9895 +       }
9896 +
9897 +       return err;
9898 +}
9899 +
9900 +static int au_do_fsync_dir(struct file *file, int datasync)
9901 +{
9902 +       int err;
9903 +       aufs_bindex_t bend, bindex;
9904 +       struct file *h_file;
9905 +       struct super_block *sb;
9906 +       struct inode *inode;
9907 +
9908 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9909 +       if (unlikely(err))
9910 +               goto out;
9911 +
9912 +       inode = file_inode(file);
9913 +       sb = inode->i_sb;
9914 +       bend = au_fbend_dir(file);
9915 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9916 +               h_file = au_hf_dir(file, bindex);
9917 +               if (!h_file || au_test_ro(sb, bindex, inode))
9918 +                       continue;
9919 +
9920 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9921 +       }
9922 +
9923 +out:
9924 +       return err;
9925 +}
9926 +
9927 +/*
9928 + * @file may be NULL
9929 + */
9930 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9931 +                         int datasync)
9932 +{
9933 +       int err;
9934 +       struct dentry *dentry;
9935 +       struct inode *inode;
9936 +       struct super_block *sb;
9937 +
9938 +       err = 0;
9939 +       dentry = file->f_path.dentry;
9940 +       inode = d_inode(dentry);
9941 +       inode_lock(inode);
9942 +       sb = dentry->d_sb;
9943 +       si_noflush_read_lock(sb);
9944 +       if (file)
9945 +               err = au_do_fsync_dir(file, datasync);
9946 +       else {
9947 +               di_write_lock_child(dentry);
9948 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9949 +       }
9950 +       au_cpup_attr_timesizes(inode);
9951 +       di_write_unlock(dentry);
9952 +       if (file)
9953 +               fi_write_unlock(file);
9954 +
9955 +       si_read_unlock(sb);
9956 +       inode_unlock(inode);
9957 +       return err;
9958 +}
9959 +
9960 +/* ---------------------------------------------------------------------- */
9961 +
9962 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9963 +{
9964 +       int err;
9965 +       struct dentry *dentry;
9966 +       struct inode *inode, *h_inode;
9967 +       struct super_block *sb;
9968 +
9969 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9970 +
9971 +       dentry = file->f_path.dentry;
9972 +       inode = d_inode(dentry);
9973 +       IMustLock(inode);
9974 +
9975 +       sb = dentry->d_sb;
9976 +       si_read_lock(sb, AuLock_FLUSH);
9977 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9978 +       if (unlikely(err))
9979 +               goto out;
9980 +       err = au_alive_dir(dentry);
9981 +       if (!err)
9982 +               err = au_vdir_init(file);
9983 +       di_downgrade_lock(dentry, AuLock_IR);
9984 +       if (unlikely(err))
9985 +               goto out_unlock;
9986 +
9987 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9988 +       if (!au_test_nfsd()) {
9989 +               err = au_vdir_fill_de(file, ctx);
9990 +               fsstack_copy_attr_atime(inode, h_inode);
9991 +       } else {
9992 +               /*
9993 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9994 +                * encode_fh() and others.
9995 +                */
9996 +               atomic_inc(&h_inode->i_count);
9997 +               di_read_unlock(dentry, AuLock_IR);
9998 +               si_read_unlock(sb);
9999 +               err = au_vdir_fill_de(file, ctx);
10000 +               fsstack_copy_attr_atime(inode, h_inode);
10001 +               fi_write_unlock(file);
10002 +               iput(h_inode);
10003 +
10004 +               AuTraceErr(err);
10005 +               return err;
10006 +       }
10007 +
10008 +out_unlock:
10009 +       di_read_unlock(dentry, AuLock_IR);
10010 +       fi_write_unlock(file);
10011 +out:
10012 +       si_read_unlock(sb);
10013 +       return err;
10014 +}
10015 +
10016 +/* ---------------------------------------------------------------------- */
10017 +
10018 +#define AuTestEmpty_WHONLY     1
10019 +#define AuTestEmpty_CALLED     (1 << 1)
10020 +#define AuTestEmpty_SHWH       (1 << 2)
10021 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10022 +#define au_fset_testempty(flags, name) \
10023 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10024 +#define au_fclr_testempty(flags, name) \
10025 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10026 +
10027 +#ifndef CONFIG_AUFS_SHWH
10028 +#undef AuTestEmpty_SHWH
10029 +#define AuTestEmpty_SHWH       0
10030 +#endif
10031 +
10032 +struct test_empty_arg {
10033 +       struct dir_context ctx;
10034 +       struct au_nhash *whlist;
10035 +       unsigned int flags;
10036 +       int err;
10037 +       aufs_bindex_t bindex;
10038 +};
10039 +
10040 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10041 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10042 +                        unsigned int d_type)
10043 +{
10044 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10045 +                                                 ctx);
10046 +       char *name = (void *)__name;
10047 +
10048 +       arg->err = 0;
10049 +       au_fset_testempty(arg->flags, CALLED);
10050 +       /* smp_mb(); */
10051 +       if (name[0] == '.'
10052 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10053 +               goto out; /* success */
10054 +
10055 +       if (namelen <= AUFS_WH_PFX_LEN
10056 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10057 +               if (au_ftest_testempty(arg->flags, WHONLY)
10058 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10059 +                       arg->err = -ENOTEMPTY;
10060 +               goto out;
10061 +       }
10062 +
10063 +       name += AUFS_WH_PFX_LEN;
10064 +       namelen -= AUFS_WH_PFX_LEN;
10065 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10066 +               arg->err = au_nhash_append_wh
10067 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10068 +                        au_ftest_testempty(arg->flags, SHWH));
10069 +
10070 +out:
10071 +       /* smp_mb(); */
10072 +       AuTraceErr(arg->err);
10073 +       return arg->err;
10074 +}
10075 +
10076 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10077 +{
10078 +       int err;
10079 +       struct file *h_file;
10080 +
10081 +       h_file = au_h_open(dentry, arg->bindex,
10082 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10083 +                          /*file*/NULL, /*force_wr*/0);
10084 +       err = PTR_ERR(h_file);
10085 +       if (IS_ERR(h_file))
10086 +               goto out;
10087 +
10088 +       err = 0;
10089 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10090 +           && !file_inode(h_file)->i_nlink)
10091 +               goto out_put;
10092 +
10093 +       do {
10094 +               arg->err = 0;
10095 +               au_fclr_testempty(arg->flags, CALLED);
10096 +               /* smp_mb(); */
10097 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10098 +               if (err >= 0)
10099 +                       err = arg->err;
10100 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10101 +
10102 +out_put:
10103 +       fput(h_file);
10104 +       au_sbr_put(dentry->d_sb, arg->bindex);
10105 +out:
10106 +       return err;
10107 +}
10108 +
10109 +struct do_test_empty_args {
10110 +       int *errp;
10111 +       struct dentry *dentry;
10112 +       struct test_empty_arg *arg;
10113 +};
10114 +
10115 +static void call_do_test_empty(void *args)
10116 +{
10117 +       struct do_test_empty_args *a = args;
10118 +       *a->errp = do_test_empty(a->dentry, a->arg);
10119 +}
10120 +
10121 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10122 +{
10123 +       int err, wkq_err;
10124 +       struct dentry *h_dentry;
10125 +       struct inode *h_inode;
10126 +
10127 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10128 +       h_inode = d_inode(h_dentry);
10129 +       /* todo: i_mode changes anytime? */
10130 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
10131 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10132 +       inode_unlock(h_inode);
10133 +       if (!err)
10134 +               err = do_test_empty(dentry, arg);
10135 +       else {
10136 +               struct do_test_empty_args args = {
10137 +                       .errp   = &err,
10138 +                       .dentry = dentry,
10139 +                       .arg    = arg
10140 +               };
10141 +               unsigned int flags = arg->flags;
10142 +
10143 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10144 +               if (unlikely(wkq_err))
10145 +                       err = wkq_err;
10146 +               arg->flags = flags;
10147 +       }
10148 +
10149 +       return err;
10150 +}
10151 +
10152 +int au_test_empty_lower(struct dentry *dentry)
10153 +{
10154 +       int err;
10155 +       unsigned int rdhash;
10156 +       aufs_bindex_t bindex, bstart, btail;
10157 +       struct au_nhash whlist;
10158 +       struct test_empty_arg arg = {
10159 +               .ctx = {
10160 +                       .actor = test_empty_cb
10161 +               }
10162 +       };
10163 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10164 +
10165 +       SiMustAnyLock(dentry->d_sb);
10166 +
10167 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10168 +       if (!rdhash)
10169 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10170 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10171 +       if (unlikely(err))
10172 +               goto out;
10173 +
10174 +       arg.flags = 0;
10175 +       arg.whlist = &whlist;
10176 +       bstart = au_dbstart(dentry);
10177 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10178 +               au_fset_testempty(arg.flags, SHWH);
10179 +       test_empty = do_test_empty;
10180 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10181 +               test_empty = sio_test_empty;
10182 +       arg.bindex = bstart;
10183 +       err = test_empty(dentry, &arg);
10184 +       if (unlikely(err))
10185 +               goto out_whlist;
10186 +
10187 +       au_fset_testempty(arg.flags, WHONLY);
10188 +       btail = au_dbtaildir(dentry);
10189 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
10190 +               struct dentry *h_dentry;
10191 +
10192 +               h_dentry = au_h_dptr(dentry, bindex);
10193 +               if (h_dentry && d_is_positive(h_dentry)) {
10194 +                       arg.bindex = bindex;
10195 +                       err = test_empty(dentry, &arg);
10196 +               }
10197 +       }
10198 +
10199 +out_whlist:
10200 +       au_nhash_wh_free(&whlist);
10201 +out:
10202 +       return err;
10203 +}
10204 +
10205 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10206 +{
10207 +       int err;
10208 +       struct test_empty_arg arg = {
10209 +               .ctx = {
10210 +                       .actor = test_empty_cb
10211 +               }
10212 +       };
10213 +       aufs_bindex_t bindex, btail;
10214 +
10215 +       err = 0;
10216 +       arg.whlist = whlist;
10217 +       arg.flags = AuTestEmpty_WHONLY;
10218 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10219 +               au_fset_testempty(arg.flags, SHWH);
10220 +       btail = au_dbtaildir(dentry);
10221 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
10222 +               struct dentry *h_dentry;
10223 +
10224 +               h_dentry = au_h_dptr(dentry, bindex);
10225 +               if (h_dentry && d_is_positive(h_dentry)) {
10226 +                       arg.bindex = bindex;
10227 +                       err = sio_test_empty(dentry, &arg);
10228 +               }
10229 +       }
10230 +
10231 +       return err;
10232 +}
10233 +
10234 +/* ---------------------------------------------------------------------- */
10235 +
10236 +const struct file_operations aufs_dir_fop = {
10237 +       .owner          = THIS_MODULE,
10238 +       .llseek         = default_llseek,
10239 +       .read           = generic_read_dir,
10240 +       .iterate        = aufs_iterate,
10241 +       .unlocked_ioctl = aufs_ioctl_dir,
10242 +#ifdef CONFIG_COMPAT
10243 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10244 +#endif
10245 +       .open           = aufs_open_dir,
10246 +       .release        = aufs_release_dir,
10247 +       .flush          = aufs_flush_dir,
10248 +       .fsync          = aufs_fsync_dir
10249 +};
10250 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10251 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10252 +++ linux/fs/aufs/dir.h 2016-04-24 18:32:51.390353525 +0200
10253 @@ -0,0 +1,131 @@
10254 +/*
10255 + * Copyright (C) 2005-2016 Junjiro R. Okajima
10256 + *
10257 + * This program, aufs is free software; you can redistribute it and/or modify
10258 + * it under the terms of the GNU General Public License as published by
10259 + * the Free Software Foundation; either version 2 of the License, or
10260 + * (at your option) any later version.
10261 + *
10262 + * This program is distributed in the hope that it will be useful,
10263 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10264 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10265 + * GNU General Public License for more details.
10266 + *
10267 + * You should have received a copy of the GNU General Public License
10268 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10269 + */
10270 +
10271 +/*
10272 + * directory operations
10273 + */
10274 +
10275 +#ifndef __AUFS_DIR_H__
10276 +#define __AUFS_DIR_H__
10277 +
10278 +#ifdef __KERNEL__
10279 +
10280 +#include <linux/fs.h>
10281 +
10282 +/* ---------------------------------------------------------------------- */
10283 +
10284 +/* need to be faster and smaller */
10285 +
10286 +struct au_nhash {
10287 +       unsigned int            nh_num;
10288 +       struct hlist_head       *nh_head;
10289 +};
10290 +
10291 +struct au_vdir_destr {
10292 +       unsigned char   len;
10293 +       unsigned char   name[0];
10294 +} __packed;
10295 +
10296 +struct au_vdir_dehstr {
10297 +       struct hlist_node       hash;
10298 +       struct au_vdir_destr    *str;
10299 +} ____cacheline_aligned_in_smp;
10300 +
10301 +struct au_vdir_de {
10302 +       ino_t                   de_ino;
10303 +       unsigned char           de_type;
10304 +       /* caution: packed */
10305 +       struct au_vdir_destr    de_str;
10306 +} __packed;
10307 +
10308 +struct au_vdir_wh {
10309 +       struct hlist_node       wh_hash;
10310 +#ifdef CONFIG_AUFS_SHWH
10311 +       ino_t                   wh_ino;
10312 +       aufs_bindex_t           wh_bindex;
10313 +       unsigned char           wh_type;
10314 +#else
10315 +       aufs_bindex_t           wh_bindex;
10316 +#endif
10317 +       /* caution: packed */
10318 +       struct au_vdir_destr    wh_str;
10319 +} __packed;
10320 +
10321 +union au_vdir_deblk_p {
10322 +       unsigned char           *deblk;
10323 +       struct au_vdir_de       *de;
10324 +};
10325 +
10326 +struct au_vdir {
10327 +       unsigned char   **vd_deblk;
10328 +       unsigned long   vd_nblk;
10329 +       struct {
10330 +               unsigned long           ul;
10331 +               union au_vdir_deblk_p   p;
10332 +       } vd_last;
10333 +
10334 +       unsigned long   vd_version;
10335 +       unsigned int    vd_deblk_sz;
10336 +       unsigned long   vd_jiffy;
10337 +} ____cacheline_aligned_in_smp;
10338 +
10339 +/* ---------------------------------------------------------------------- */
10340 +
10341 +/* dir.c */
10342 +extern const struct file_operations aufs_dir_fop;
10343 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10344 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10345 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10346 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10347 +int au_test_empty_lower(struct dentry *dentry);
10348 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10349 +
10350 +/* vdir.c */
10351 +unsigned int au_rdhash_est(loff_t sz);
10352 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10353 +void au_nhash_wh_free(struct au_nhash *whlist);
10354 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10355 +                           int limit);
10356 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10357 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10358 +                      unsigned int d_type, aufs_bindex_t bindex,
10359 +                      unsigned char shwh);
10360 +void au_vdir_free(struct au_vdir *vdir);
10361 +int au_vdir_init(struct file *file);
10362 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10363 +
10364 +/* ioctl.c */
10365 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10366 +
10367 +#ifdef CONFIG_AUFS_RDU
10368 +/* rdu.c */
10369 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10370 +#ifdef CONFIG_COMPAT
10371 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10372 +                        unsigned long arg);
10373 +#endif
10374 +#else
10375 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10376 +       unsigned int cmd, unsigned long arg)
10377 +#ifdef CONFIG_COMPAT
10378 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10379 +       unsigned int cmd, unsigned long arg)
10380 +#endif
10381 +#endif
10382 +
10383 +#endif /* __KERNEL__ */
10384 +#endif /* __AUFS_DIR_H__ */
10385 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10386 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10387 +++ linux/fs/aufs/dynop.c       2016-04-24 18:32:51.390353525 +0200
10388 @@ -0,0 +1,369 @@
10389 +/*
10390 + * Copyright (C) 2010-2016 Junjiro R. Okajima
10391 + *
10392 + * This program, aufs is free software; you can redistribute it and/or modify
10393 + * it under the terms of the GNU General Public License as published by
10394 + * the Free Software Foundation; either version 2 of the License, or
10395 + * (at your option) any later version.
10396 + *
10397 + * This program is distributed in the hope that it will be useful,
10398 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10399 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10400 + * GNU General Public License for more details.
10401 + *
10402 + * You should have received a copy of the GNU General Public License
10403 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10404 + */
10405 +
10406 +/*
10407 + * dynamically customizable operations for regular files
10408 + */
10409 +
10410 +#include "aufs.h"
10411 +
10412 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10413 +
10414 +/*
10415 + * How large will these lists be?
10416 + * Usually just a few elements, 20-30 at most for each, I guess.
10417 + */
10418 +static struct au_splhead dynop[AuDyLast];
10419 +
10420 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
10421 +{
10422 +       struct au_dykey *key, *tmp;
10423 +       struct list_head *head;
10424 +
10425 +       key = NULL;
10426 +       head = &spl->head;
10427 +       rcu_read_lock();
10428 +       list_for_each_entry_rcu(tmp, head, dk_list)
10429 +               if (tmp->dk_op.dy_hop == h_op) {
10430 +                       key = tmp;
10431 +                       kref_get(&key->dk_kref);
10432 +                       break;
10433 +               }
10434 +       rcu_read_unlock();
10435 +
10436 +       return key;
10437 +}
10438 +
10439 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10440 +{
10441 +       struct au_dykey **k, *found;
10442 +       const void *h_op = key->dk_op.dy_hop;
10443 +       int i;
10444 +
10445 +       found = NULL;
10446 +       k = br->br_dykey;
10447 +       for (i = 0; i < AuBrDynOp; i++)
10448 +               if (k[i]) {
10449 +                       if (k[i]->dk_op.dy_hop == h_op) {
10450 +                               found = k[i];
10451 +                               break;
10452 +                       }
10453 +               } else
10454 +                       break;
10455 +       if (!found) {
10456 +               spin_lock(&br->br_dykey_lock);
10457 +               for (; i < AuBrDynOp; i++)
10458 +                       if (k[i]) {
10459 +                               if (k[i]->dk_op.dy_hop == h_op) {
10460 +                                       found = k[i];
10461 +                                       break;
10462 +                               }
10463 +                       } else {
10464 +                               k[i] = key;
10465 +                               break;
10466 +                       }
10467 +               spin_unlock(&br->br_dykey_lock);
10468 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10469 +       }
10470 +
10471 +       return found;
10472 +}
10473 +
10474 +/* kref_get() if @key is already added */
10475 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10476 +{
10477 +       struct au_dykey *tmp, *found;
10478 +       struct list_head *head;
10479 +       const void *h_op = key->dk_op.dy_hop;
10480 +
10481 +       found = NULL;
10482 +       head = &spl->head;
10483 +       spin_lock(&spl->spin);
10484 +       list_for_each_entry(tmp, head, dk_list)
10485 +               if (tmp->dk_op.dy_hop == h_op) {
10486 +                       kref_get(&tmp->dk_kref);
10487 +                       found = tmp;
10488 +                       break;
10489 +               }
10490 +       if (!found)
10491 +               list_add_rcu(&key->dk_list, head);
10492 +       spin_unlock(&spl->spin);
10493 +
10494 +       if (!found)
10495 +               DyPrSym(key);
10496 +       return found;
10497 +}
10498 +
10499 +static void dy_free_rcu(struct rcu_head *rcu)
10500 +{
10501 +       struct au_dykey *key;
10502 +
10503 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10504 +       DyPrSym(key);
10505 +       kfree(key);
10506 +}
10507 +
10508 +static void dy_free(struct kref *kref)
10509 +{
10510 +       struct au_dykey *key;
10511 +       struct au_splhead *spl;
10512 +
10513 +       key = container_of(kref, struct au_dykey, dk_kref);
10514 +       spl = dynop + key->dk_op.dy_type;
10515 +       au_spl_del_rcu(&key->dk_list, spl);
10516 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10517 +}
10518 +
10519 +void au_dy_put(struct au_dykey *key)
10520 +{
10521 +       kref_put(&key->dk_kref, dy_free);
10522 +}
10523 +
10524 +/* ---------------------------------------------------------------------- */
10525 +
10526 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10527 +
10528 +#ifdef CONFIG_AUFS_DEBUG
10529 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10530 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10531 +#else
10532 +#define DyDbgDeclare(cnt)      do {} while (0)
10533 +#define DyDbgInc(cnt)          do {} while (0)
10534 +#endif
10535 +
10536 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10537 +       DyDbgInc(cnt);                                                  \
10538 +       if (h_op->func) {                                               \
10539 +               if (src.func)                                           \
10540 +                       dst.func = src.func;                            \
10541 +               else                                                    \
10542 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10543 +       }                                                               \
10544 +} while (0)
10545 +
10546 +#define DySetForce(func, dst, src) do {                \
10547 +       AuDebugOn(!src.func);                   \
10548 +       DyDbgInc(cnt);                          \
10549 +       dst.func = src.func;                    \
10550 +} while (0)
10551 +
10552 +#define DySetAop(func) \
10553 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10554 +#define DySetAopForce(func) \
10555 +       DySetForce(func, dyaop->da_op, aufs_aop)
10556 +
10557 +static void dy_aop(struct au_dykey *key, const void *h_op,
10558 +                  struct super_block *h_sb __maybe_unused)
10559 +{
10560 +       struct au_dyaop *dyaop = (void *)key;
10561 +       const struct address_space_operations *h_aop = h_op;
10562 +       DyDbgDeclare(cnt);
10563 +
10564 +       AuDbg("%s\n", au_sbtype(h_sb));
10565 +
10566 +       DySetAop(writepage);
10567 +       DySetAopForce(readpage);        /* force */
10568 +       DySetAop(writepages);
10569 +       DySetAop(set_page_dirty);
10570 +       DySetAop(readpages);
10571 +       DySetAop(write_begin);
10572 +       DySetAop(write_end);
10573 +       DySetAop(bmap);
10574 +       DySetAop(invalidatepage);
10575 +       DySetAop(releasepage);
10576 +       DySetAop(freepage);
10577 +       /* this one will be changed according to an aufs mount option */
10578 +       DySetAop(direct_IO);
10579 +       DySetAop(migratepage);
10580 +       DySetAop(launder_page);
10581 +       DySetAop(is_partially_uptodate);
10582 +       DySetAop(is_dirty_writeback);
10583 +       DySetAop(error_remove_page);
10584 +       DySetAop(swap_activate);
10585 +       DySetAop(swap_deactivate);
10586 +
10587 +       DyDbgSize(cnt, *h_aop);
10588 +}
10589 +
10590 +/* ---------------------------------------------------------------------- */
10591 +
10592 +static void dy_bug(struct kref *kref)
10593 +{
10594 +       BUG();
10595 +}
10596 +
10597 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10598 +{
10599 +       struct au_dykey *key, *old;
10600 +       struct au_splhead *spl;
10601 +       struct op {
10602 +               unsigned int sz;
10603 +               void (*set)(struct au_dykey *key, const void *h_op,
10604 +                           struct super_block *h_sb __maybe_unused);
10605 +       };
10606 +       static const struct op a[] = {
10607 +               [AuDy_AOP] = {
10608 +                       .sz     = sizeof(struct au_dyaop),
10609 +                       .set    = dy_aop
10610 +               }
10611 +       };
10612 +       const struct op *p;
10613 +
10614 +       spl = dynop + op->dy_type;
10615 +       key = dy_gfind_get(spl, op->dy_hop);
10616 +       if (key)
10617 +               goto out_add; /* success */
10618 +
10619 +       p = a + op->dy_type;
10620 +       key = kzalloc(p->sz, GFP_NOFS);
10621 +       if (unlikely(!key)) {
10622 +               key = ERR_PTR(-ENOMEM);
10623 +               goto out;
10624 +       }
10625 +
10626 +       key->dk_op.dy_hop = op->dy_hop;
10627 +       kref_init(&key->dk_kref);
10628 +       p->set(key, op->dy_hop, au_br_sb(br));
10629 +       old = dy_gadd(spl, key);
10630 +       if (old) {
10631 +               kfree(key);
10632 +               key = old;
10633 +       }
10634 +
10635 +out_add:
10636 +       old = dy_bradd(br, key);
10637 +       if (old)
10638 +               /* its ref-count should never be zero here */
10639 +               kref_put(&key->dk_kref, dy_bug);
10640 +out:
10641 +       return key;
10642 +}
10643 +
10644 +/* ---------------------------------------------------------------------- */
10645 +/*
10646 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10647 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10648 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10649 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10650 + * See the aufs manual in detail.
10651 + */
10652 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10653 +{
10654 +       if (!do_dx)
10655 +               dyaop->da_op.direct_IO = NULL;
10656 +       else
10657 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10658 +}
10659 +
10660 +static struct au_dyaop *dy_aget(struct au_branch *br,
10661 +                               const struct address_space_operations *h_aop,
10662 +                               int do_dx)
10663 +{
10664 +       struct au_dyaop *dyaop;
10665 +       struct au_dynop op;
10666 +
10667 +       op.dy_type = AuDy_AOP;
10668 +       op.dy_haop = h_aop;
10669 +       dyaop = (void *)dy_get(&op, br);
10670 +       if (IS_ERR(dyaop))
10671 +               goto out;
10672 +       dy_adx(dyaop, do_dx);
10673 +
10674 +out:
10675 +       return dyaop;
10676 +}
10677 +
10678 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10679 +               struct inode *h_inode)
10680 +{
10681 +       int err, do_dx;
10682 +       struct super_block *sb;
10683 +       struct au_branch *br;
10684 +       struct au_dyaop *dyaop;
10685 +
10686 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10687 +       IiMustWriteLock(inode);
10688 +
10689 +       sb = inode->i_sb;
10690 +       br = au_sbr(sb, bindex);
10691 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10692 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10693 +       err = PTR_ERR(dyaop);
10694 +       if (IS_ERR(dyaop))
10695 +               /* unnecessary to call dy_fput() */
10696 +               goto out;
10697 +
10698 +       err = 0;
10699 +       inode->i_mapping->a_ops = &dyaop->da_op;
10700 +
10701 +out:
10702 +       return err;
10703 +}
10704 +
10705 +/*
10706 + * Is it safe to replace a_ops during the inode/file is in operation?
10707 + * Yes, I hope so.
10708 + */
10709 +int au_dy_irefresh(struct inode *inode)
10710 +{
10711 +       int err;
10712 +       aufs_bindex_t bstart;
10713 +       struct inode *h_inode;
10714 +
10715 +       err = 0;
10716 +       if (S_ISREG(inode->i_mode)) {
10717 +               bstart = au_ibstart(inode);
10718 +               h_inode = au_h_iptr(inode, bstart);
10719 +               err = au_dy_iaop(inode, bstart, h_inode);
10720 +       }
10721 +       return err;
10722 +}
10723 +
10724 +void au_dy_arefresh(int do_dx)
10725 +{
10726 +       struct au_splhead *spl;
10727 +       struct list_head *head;
10728 +       struct au_dykey *key;
10729 +
10730 +       spl = dynop + AuDy_AOP;
10731 +       head = &spl->head;
10732 +       spin_lock(&spl->spin);
10733 +       list_for_each_entry(key, head, dk_list)
10734 +               dy_adx((void *)key, do_dx);
10735 +       spin_unlock(&spl->spin);
10736 +}
10737 +
10738 +/* ---------------------------------------------------------------------- */
10739 +
10740 +void __init au_dy_init(void)
10741 +{
10742 +       int i;
10743 +
10744 +       /* make sure that 'struct au_dykey *' can be any type */
10745 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10746 +
10747 +       for (i = 0; i < AuDyLast; i++)
10748 +               au_spl_init(dynop + i);
10749 +}
10750 +
10751 +void au_dy_fin(void)
10752 +{
10753 +       int i;
10754 +
10755 +       for (i = 0; i < AuDyLast; i++)
10756 +               WARN_ON(!list_empty(&dynop[i].head));
10757 +}
10758 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10759 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10760 +++ linux/fs/aufs/dynop.h       2016-04-24 18:32:51.393686895 +0200
10761 @@ -0,0 +1,74 @@
10762 +/*
10763 + * Copyright (C) 2010-2016 Junjiro R. Okajima
10764 + *
10765 + * This program, aufs is free software; you can redistribute it and/or modify
10766 + * it under the terms of the GNU General Public License as published by
10767 + * the Free Software Foundation; either version 2 of the License, or
10768 + * (at your option) any later version.
10769 + *
10770 + * This program is distributed in the hope that it will be useful,
10771 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10772 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10773 + * GNU General Public License for more details.
10774 + *
10775 + * You should have received a copy of the GNU General Public License
10776 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10777 + */
10778 +
10779 +/*
10780 + * dynamically customizable operations (for regular files only)
10781 + */
10782 +
10783 +#ifndef __AUFS_DYNOP_H__
10784 +#define __AUFS_DYNOP_H__
10785 +
10786 +#ifdef __KERNEL__
10787 +
10788 +#include <linux/fs.h>
10789 +#include <linux/kref.h>
10790 +
10791 +enum {AuDy_AOP, AuDyLast};
10792 +
10793 +struct au_dynop {
10794 +       int                                             dy_type;
10795 +       union {
10796 +               const void                              *dy_hop;
10797 +               const struct address_space_operations   *dy_haop;
10798 +       };
10799 +};
10800 +
10801 +struct au_dykey {
10802 +       union {
10803 +               struct list_head        dk_list;
10804 +               struct rcu_head         dk_rcu;
10805 +       };
10806 +       struct au_dynop         dk_op;
10807 +
10808 +       /*
10809 +        * during I am in the branch local array, kref is gotten. when the
10810 +        * branch is removed, kref is put.
10811 +        */
10812 +       struct kref             dk_kref;
10813 +};
10814 +
10815 +/* stop unioning since their sizes are very different from each other */
10816 +struct au_dyaop {
10817 +       struct au_dykey                 da_key;
10818 +       struct address_space_operations da_op; /* not const */
10819 +};
10820 +
10821 +/* ---------------------------------------------------------------------- */
10822 +
10823 +/* dynop.c */
10824 +struct au_branch;
10825 +void au_dy_put(struct au_dykey *key);
10826 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10827 +               struct inode *h_inode);
10828 +int au_dy_irefresh(struct inode *inode);
10829 +void au_dy_arefresh(int do_dio);
10830 +
10831 +void __init au_dy_init(void);
10832 +void au_dy_fin(void);
10833 +
10834 +#endif /* __KERNEL__ */
10835 +#endif /* __AUFS_DYNOP_H__ */
10836 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10837 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
10838 +++ linux/fs/aufs/export.c      2016-04-24 18:32:51.393686895 +0200
10839 @@ -0,0 +1,830 @@
10840 +/*
10841 + * Copyright (C) 2005-2016 Junjiro R. Okajima
10842 + *
10843 + * This program, aufs is free software; you can redistribute it and/or modify
10844 + * it under the terms of the GNU General Public License as published by
10845 + * the Free Software Foundation; either version 2 of the License, or
10846 + * (at your option) any later version.
10847 + *
10848 + * This program is distributed in the hope that it will be useful,
10849 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10850 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10851 + * GNU General Public License for more details.
10852 + *
10853 + * You should have received a copy of the GNU General Public License
10854 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10855 + */
10856 +
10857 +/*
10858 + * export via nfs
10859 + */
10860 +
10861 +#include <linux/exportfs.h>
10862 +#include <linux/fs_struct.h>
10863 +#include <linux/namei.h>
10864 +#include <linux/nsproxy.h>
10865 +#include <linux/random.h>
10866 +#include <linux/writeback.h>
10867 +#include "../fs/mount.h"
10868 +#include "aufs.h"
10869 +
10870 +union conv {
10871 +#ifdef CONFIG_AUFS_INO_T_64
10872 +       __u32 a[2];
10873 +#else
10874 +       __u32 a[1];
10875 +#endif
10876 +       ino_t ino;
10877 +};
10878 +
10879 +static ino_t decode_ino(__u32 *a)
10880 +{
10881 +       union conv u;
10882 +
10883 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10884 +       u.a[0] = a[0];
10885 +#ifdef CONFIG_AUFS_INO_T_64
10886 +       u.a[1] = a[1];
10887 +#endif
10888 +       return u.ino;
10889 +}
10890 +
10891 +static void encode_ino(__u32 *a, ino_t ino)
10892 +{
10893 +       union conv u;
10894 +
10895 +       u.ino = ino;
10896 +       a[0] = u.a[0];
10897 +#ifdef CONFIG_AUFS_INO_T_64
10898 +       a[1] = u.a[1];
10899 +#endif
10900 +}
10901 +
10902 +/* NFS file handle */
10903 +enum {
10904 +       Fh_br_id,
10905 +       Fh_sigen,
10906 +#ifdef CONFIG_AUFS_INO_T_64
10907 +       /* support 64bit inode number */
10908 +       Fh_ino1,
10909 +       Fh_ino2,
10910 +       Fh_dir_ino1,
10911 +       Fh_dir_ino2,
10912 +#else
10913 +       Fh_ino1,
10914 +       Fh_dir_ino1,
10915 +#endif
10916 +       Fh_igen,
10917 +       Fh_h_type,
10918 +       Fh_tail,
10919 +
10920 +       Fh_ino = Fh_ino1,
10921 +       Fh_dir_ino = Fh_dir_ino1
10922 +};
10923 +
10924 +static int au_test_anon(struct dentry *dentry)
10925 +{
10926 +       /* note: read d_flags without d_lock */
10927 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10928 +}
10929 +
10930 +int au_test_nfsd(void)
10931 +{
10932 +       int ret;
10933 +       struct task_struct *tsk = current;
10934 +       char comm[sizeof(tsk->comm)];
10935 +
10936 +       ret = 0;
10937 +       if (tsk->flags & PF_KTHREAD) {
10938 +               get_task_comm(comm, tsk);
10939 +               ret = !strcmp(comm, "nfsd");
10940 +       }
10941 +
10942 +       return ret;
10943 +}
10944 +
10945 +/* ---------------------------------------------------------------------- */
10946 +/* inode generation external table */
10947 +
10948 +void au_xigen_inc(struct inode *inode)
10949 +{
10950 +       loff_t pos;
10951 +       ssize_t sz;
10952 +       __u32 igen;
10953 +       struct super_block *sb;
10954 +       struct au_sbinfo *sbinfo;
10955 +
10956 +       sb = inode->i_sb;
10957 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10958 +
10959 +       sbinfo = au_sbi(sb);
10960 +       pos = inode->i_ino;
10961 +       pos *= sizeof(igen);
10962 +       igen = inode->i_generation + 1;
10963 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10964 +                        sizeof(igen), &pos);
10965 +       if (sz == sizeof(igen))
10966 +               return; /* success */
10967 +
10968 +       if (unlikely(sz >= 0))
10969 +               AuIOErr("xigen error (%zd)\n", sz);
10970 +}
10971 +
10972 +int au_xigen_new(struct inode *inode)
10973 +{
10974 +       int err;
10975 +       loff_t pos;
10976 +       ssize_t sz;
10977 +       struct super_block *sb;
10978 +       struct au_sbinfo *sbinfo;
10979 +       struct file *file;
10980 +
10981 +       err = 0;
10982 +       /* todo: dirty, at mount time */
10983 +       if (inode->i_ino == AUFS_ROOT_INO)
10984 +               goto out;
10985 +       sb = inode->i_sb;
10986 +       SiMustAnyLock(sb);
10987 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10988 +               goto out;
10989 +
10990 +       err = -EFBIG;
10991 +       pos = inode->i_ino;
10992 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10993 +               AuIOErr1("too large i%lld\n", pos);
10994 +               goto out;
10995 +       }
10996 +       pos *= sizeof(inode->i_generation);
10997 +
10998 +       err = 0;
10999 +       sbinfo = au_sbi(sb);
11000 +       file = sbinfo->si_xigen;
11001 +       BUG_ON(!file);
11002 +
11003 +       if (vfsub_f_size_read(file)
11004 +           < pos + sizeof(inode->i_generation)) {
11005 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
11006 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
11007 +                                sizeof(inode->i_generation), &pos);
11008 +       } else
11009 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
11010 +                               sizeof(inode->i_generation), &pos);
11011 +       if (sz == sizeof(inode->i_generation))
11012 +               goto out; /* success */
11013 +
11014 +       err = sz;
11015 +       if (unlikely(sz >= 0)) {
11016 +               err = -EIO;
11017 +               AuIOErr("xigen error (%zd)\n", sz);
11018 +       }
11019 +
11020 +out:
11021 +       return err;
11022 +}
11023 +
11024 +int au_xigen_set(struct super_block *sb, struct file *base)
11025 +{
11026 +       int err;
11027 +       struct au_sbinfo *sbinfo;
11028 +       struct file *file;
11029 +
11030 +       SiMustWriteLock(sb);
11031 +
11032 +       sbinfo = au_sbi(sb);
11033 +       file = au_xino_create2(base, sbinfo->si_xigen);
11034 +       err = PTR_ERR(file);
11035 +       if (IS_ERR(file))
11036 +               goto out;
11037 +       err = 0;
11038 +       if (sbinfo->si_xigen)
11039 +               fput(sbinfo->si_xigen);
11040 +       sbinfo->si_xigen = file;
11041 +
11042 +out:
11043 +       return err;
11044 +}
11045 +
11046 +void au_xigen_clr(struct super_block *sb)
11047 +{
11048 +       struct au_sbinfo *sbinfo;
11049 +
11050 +       SiMustWriteLock(sb);
11051 +
11052 +       sbinfo = au_sbi(sb);
11053 +       if (sbinfo->si_xigen) {
11054 +               fput(sbinfo->si_xigen);
11055 +               sbinfo->si_xigen = NULL;
11056 +       }
11057 +}
11058 +
11059 +/* ---------------------------------------------------------------------- */
11060 +
11061 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
11062 +                                   ino_t dir_ino)
11063 +{
11064 +       struct dentry *dentry, *d;
11065 +       struct inode *inode;
11066 +       unsigned int sigen;
11067 +
11068 +       dentry = NULL;
11069 +       inode = ilookup(sb, ino);
11070 +       if (!inode)
11071 +               goto out;
11072 +
11073 +       dentry = ERR_PTR(-ESTALE);
11074 +       sigen = au_sigen(sb);
11075 +       if (unlikely(is_bad_inode(inode)
11076 +                    || IS_DEADDIR(inode)
11077 +                    || sigen != au_iigen(inode, NULL)))
11078 +               goto out_iput;
11079 +
11080 +       dentry = NULL;
11081 +       if (!dir_ino || S_ISDIR(inode->i_mode))
11082 +               dentry = d_find_alias(inode);
11083 +       else {
11084 +               spin_lock(&inode->i_lock);
11085 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
11086 +                       spin_lock(&d->d_lock);
11087 +                       if (!au_test_anon(d)
11088 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
11089 +                               dentry = dget_dlock(d);
11090 +                               spin_unlock(&d->d_lock);
11091 +                               break;
11092 +                       }
11093 +                       spin_unlock(&d->d_lock);
11094 +               }
11095 +               spin_unlock(&inode->i_lock);
11096 +       }
11097 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
11098 +               /* need to refresh */
11099 +               dput(dentry);
11100 +               dentry = NULL;
11101 +       }
11102 +
11103 +out_iput:
11104 +       iput(inode);
11105 +out:
11106 +       AuTraceErrPtr(dentry);
11107 +       return dentry;
11108 +}
11109 +
11110 +/* ---------------------------------------------------------------------- */
11111 +
11112 +/* todo: dirty? */
11113 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
11114 +
11115 +struct au_compare_mnt_args {
11116 +       /* input */
11117 +       struct super_block *sb;
11118 +
11119 +       /* output */
11120 +       struct vfsmount *mnt;
11121 +};
11122 +
11123 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
11124 +{
11125 +       struct au_compare_mnt_args *a = arg;
11126 +
11127 +       if (mnt->mnt_sb != a->sb)
11128 +               return 0;
11129 +       a->mnt = mntget(mnt);
11130 +       return 1;
11131 +}
11132 +
11133 +static struct vfsmount *au_mnt_get(struct super_block *sb)
11134 +{
11135 +       int err;
11136 +       struct path root;
11137 +       struct au_compare_mnt_args args = {
11138 +               .sb = sb
11139 +       };
11140 +
11141 +       get_fs_root(current->fs, &root);
11142 +       rcu_read_lock();
11143 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
11144 +       rcu_read_unlock();
11145 +       path_put(&root);
11146 +       AuDebugOn(!err);
11147 +       AuDebugOn(!args.mnt);
11148 +       return args.mnt;
11149 +}
11150 +
11151 +struct au_nfsd_si_lock {
11152 +       unsigned int sigen;
11153 +       aufs_bindex_t bindex, br_id;
11154 +       unsigned char force_lock;
11155 +};
11156 +
11157 +static int si_nfsd_read_lock(struct super_block *sb,
11158 +                            struct au_nfsd_si_lock *nsi_lock)
11159 +{
11160 +       int err;
11161 +       aufs_bindex_t bindex;
11162 +
11163 +       si_read_lock(sb, AuLock_FLUSH);
11164 +
11165 +       /* branch id may be wrapped around */
11166 +       err = 0;
11167 +       bindex = au_br_index(sb, nsi_lock->br_id);
11168 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11169 +               goto out; /* success */
11170 +
11171 +       err = -ESTALE;
11172 +       bindex = -1;
11173 +       if (!nsi_lock->force_lock)
11174 +               si_read_unlock(sb);
11175 +
11176 +out:
11177 +       nsi_lock->bindex = bindex;
11178 +       return err;
11179 +}
11180 +
11181 +struct find_name_by_ino {
11182 +       struct dir_context ctx;
11183 +       int called, found;
11184 +       ino_t ino;
11185 +       char *name;
11186 +       int namelen;
11187 +};
11188 +
11189 +static int
11190 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11191 +                loff_t offset, u64 ino, unsigned int d_type)
11192 +{
11193 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11194 +                                                 ctx);
11195 +
11196 +       a->called++;
11197 +       if (a->ino != ino)
11198 +               return 0;
11199 +
11200 +       memcpy(a->name, name, namelen);
11201 +       a->namelen = namelen;
11202 +       a->found = 1;
11203 +       return 1;
11204 +}
11205 +
11206 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11207 +                                    struct au_nfsd_si_lock *nsi_lock)
11208 +{
11209 +       struct dentry *dentry, *parent;
11210 +       struct file *file;
11211 +       struct inode *dir;
11212 +       struct find_name_by_ino arg = {
11213 +               .ctx = {
11214 +                       .actor = find_name_by_ino
11215 +               }
11216 +       };
11217 +       int err;
11218 +
11219 +       parent = path->dentry;
11220 +       if (nsi_lock)
11221 +               si_read_unlock(parent->d_sb);
11222 +       file = vfsub_dentry_open(path, au_dir_roflags);
11223 +       dentry = (void *)file;
11224 +       if (IS_ERR(file))
11225 +               goto out;
11226 +
11227 +       dentry = ERR_PTR(-ENOMEM);
11228 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11229 +       if (unlikely(!arg.name))
11230 +               goto out_file;
11231 +       arg.ino = ino;
11232 +       arg.found = 0;
11233 +       do {
11234 +               arg.called = 0;
11235 +               /* smp_mb(); */
11236 +               err = vfsub_iterate_dir(file, &arg.ctx);
11237 +       } while (!err && !arg.found && arg.called);
11238 +       dentry = ERR_PTR(err);
11239 +       if (unlikely(err))
11240 +               goto out_name;
11241 +       /* instead of ENOENT */
11242 +       dentry = ERR_PTR(-ESTALE);
11243 +       if (!arg.found)
11244 +               goto out_name;
11245 +
11246 +       /* do not call vfsub_lkup_one() */
11247 +       dir = d_inode(parent);
11248 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
11249 +       AuTraceErrPtr(dentry);
11250 +       if (IS_ERR(dentry))
11251 +               goto out_name;
11252 +       AuDebugOn(au_test_anon(dentry));
11253 +       if (unlikely(d_really_is_negative(dentry))) {
11254 +               dput(dentry);
11255 +               dentry = ERR_PTR(-ENOENT);
11256 +       }
11257 +
11258 +out_name:
11259 +       free_page((unsigned long)arg.name);
11260 +out_file:
11261 +       fput(file);
11262 +out:
11263 +       if (unlikely(nsi_lock
11264 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11265 +               if (!IS_ERR(dentry)) {
11266 +                       dput(dentry);
11267 +                       dentry = ERR_PTR(-ESTALE);
11268 +               }
11269 +       AuTraceErrPtr(dentry);
11270 +       return dentry;
11271 +}
11272 +
11273 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11274 +                                       ino_t dir_ino,
11275 +                                       struct au_nfsd_si_lock *nsi_lock)
11276 +{
11277 +       struct dentry *dentry;
11278 +       struct path path;
11279 +
11280 +       if (dir_ino != AUFS_ROOT_INO) {
11281 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11282 +               dentry = path.dentry;
11283 +               if (!path.dentry || IS_ERR(path.dentry))
11284 +                       goto out;
11285 +               AuDebugOn(au_test_anon(path.dentry));
11286 +       } else
11287 +               path.dentry = dget(sb->s_root);
11288 +
11289 +       path.mnt = au_mnt_get(sb);
11290 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11291 +       path_put(&path);
11292 +
11293 +out:
11294 +       AuTraceErrPtr(dentry);
11295 +       return dentry;
11296 +}
11297 +
11298 +/* ---------------------------------------------------------------------- */
11299 +
11300 +static int h_acceptable(void *expv, struct dentry *dentry)
11301 +{
11302 +       return 1;
11303 +}
11304 +
11305 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11306 +                          char *buf, int len, struct super_block *sb)
11307 +{
11308 +       char *p;
11309 +       int n;
11310 +       struct path path;
11311 +
11312 +       p = d_path(h_rootpath, buf, len);
11313 +       if (IS_ERR(p))
11314 +               goto out;
11315 +       n = strlen(p);
11316 +
11317 +       path.mnt = h_rootpath->mnt;
11318 +       path.dentry = h_parent;
11319 +       p = d_path(&path, buf, len);
11320 +       if (IS_ERR(p))
11321 +               goto out;
11322 +       if (n != 1)
11323 +               p += n;
11324 +
11325 +       path.mnt = au_mnt_get(sb);
11326 +       path.dentry = sb->s_root;
11327 +       p = d_path(&path, buf, len - strlen(p));
11328 +       mntput(path.mnt);
11329 +       if (IS_ERR(p))
11330 +               goto out;
11331 +       if (n != 1)
11332 +               p[strlen(p)] = '/';
11333 +
11334 +out:
11335 +       AuTraceErrPtr(p);
11336 +       return p;
11337 +}
11338 +
11339 +static
11340 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11341 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11342 +{
11343 +       struct dentry *dentry, *h_parent, *root;
11344 +       struct super_block *h_sb;
11345 +       char *pathname, *p;
11346 +       struct vfsmount *h_mnt;
11347 +       struct au_branch *br;
11348 +       int err;
11349 +       struct path path;
11350 +
11351 +       br = au_sbr(sb, nsi_lock->bindex);
11352 +       h_mnt = au_br_mnt(br);
11353 +       h_sb = h_mnt->mnt_sb;
11354 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11355 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11356 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11357 +                                     h_acceptable, /*context*/NULL);
11358 +       dentry = h_parent;
11359 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11360 +               AuWarn1("%s decode_fh failed, %ld\n",
11361 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11362 +               goto out;
11363 +       }
11364 +       dentry = NULL;
11365 +       if (unlikely(au_test_anon(h_parent))) {
11366 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11367 +                       au_sbtype(h_sb));
11368 +               goto out_h_parent;
11369 +       }
11370 +
11371 +       dentry = ERR_PTR(-ENOMEM);
11372 +       pathname = (void *)__get_free_page(GFP_NOFS);
11373 +       if (unlikely(!pathname))
11374 +               goto out_h_parent;
11375 +
11376 +       root = sb->s_root;
11377 +       path.mnt = h_mnt;
11378 +       di_read_lock_parent(root, !AuLock_IR);
11379 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11380 +       di_read_unlock(root, !AuLock_IR);
11381 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11382 +       dentry = (void *)p;
11383 +       if (IS_ERR(p))
11384 +               goto out_pathname;
11385 +
11386 +       si_read_unlock(sb);
11387 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11388 +       dentry = ERR_PTR(err);
11389 +       if (unlikely(err))
11390 +               goto out_relock;
11391 +
11392 +       dentry = ERR_PTR(-ENOENT);
11393 +       AuDebugOn(au_test_anon(path.dentry));
11394 +       if (unlikely(d_really_is_negative(path.dentry)))
11395 +               goto out_path;
11396 +
11397 +       if (ino != d_inode(path.dentry)->i_ino)
11398 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11399 +       else
11400 +               dentry = dget(path.dentry);
11401 +
11402 +out_path:
11403 +       path_put(&path);
11404 +out_relock:
11405 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11406 +               if (!IS_ERR(dentry)) {
11407 +                       dput(dentry);
11408 +                       dentry = ERR_PTR(-ESTALE);
11409 +               }
11410 +out_pathname:
11411 +       free_page((unsigned long)pathname);
11412 +out_h_parent:
11413 +       dput(h_parent);
11414 +out:
11415 +       AuTraceErrPtr(dentry);
11416 +       return dentry;
11417 +}
11418 +
11419 +/* ---------------------------------------------------------------------- */
11420 +
11421 +static struct dentry *
11422 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11423 +                 int fh_type)
11424 +{
11425 +       struct dentry *dentry;
11426 +       __u32 *fh = fid->raw;
11427 +       struct au_branch *br;
11428 +       ino_t ino, dir_ino;
11429 +       struct au_nfsd_si_lock nsi_lock = {
11430 +               .force_lock     = 0
11431 +       };
11432 +
11433 +       dentry = ERR_PTR(-ESTALE);
11434 +       /* it should never happen, but the file handle is unreliable */
11435 +       if (unlikely(fh_len < Fh_tail))
11436 +               goto out;
11437 +       nsi_lock.sigen = fh[Fh_sigen];
11438 +       nsi_lock.br_id = fh[Fh_br_id];
11439 +
11440 +       /* branch id may be wrapped around */
11441 +       br = NULL;
11442 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11443 +               goto out;
11444 +       nsi_lock.force_lock = 1;
11445 +
11446 +       /* is this inode still cached? */
11447 +       ino = decode_ino(fh + Fh_ino);
11448 +       /* it should never happen */
11449 +       if (unlikely(ino == AUFS_ROOT_INO))
11450 +               goto out_unlock;
11451 +
11452 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11453 +       dentry = decode_by_ino(sb, ino, dir_ino);
11454 +       if (IS_ERR(dentry))
11455 +               goto out_unlock;
11456 +       if (dentry)
11457 +               goto accept;
11458 +
11459 +       /* is the parent dir cached? */
11460 +       br = au_sbr(sb, nsi_lock.bindex);
11461 +       atomic_inc(&br->br_count);
11462 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11463 +       if (IS_ERR(dentry))
11464 +               goto out_unlock;
11465 +       if (dentry)
11466 +               goto accept;
11467 +
11468 +       /* lookup path */
11469 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11470 +       if (IS_ERR(dentry))
11471 +               goto out_unlock;
11472 +       if (unlikely(!dentry))
11473 +               /* todo?: make it ESTALE */
11474 +               goto out_unlock;
11475 +
11476 +accept:
11477 +       if (!au_digen_test(dentry, au_sigen(sb))
11478 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
11479 +               goto out_unlock; /* success */
11480 +
11481 +       dput(dentry);
11482 +       dentry = ERR_PTR(-ESTALE);
11483 +out_unlock:
11484 +       if (br)
11485 +               atomic_dec(&br->br_count);
11486 +       si_read_unlock(sb);
11487 +out:
11488 +       AuTraceErrPtr(dentry);
11489 +       return dentry;
11490 +}
11491 +
11492 +#if 0 /* reserved for future use */
11493 +/* support subtreecheck option */
11494 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11495 +                                       int fh_len, int fh_type)
11496 +{
11497 +       struct dentry *parent;
11498 +       __u32 *fh = fid->raw;
11499 +       ino_t dir_ino;
11500 +
11501 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11502 +       parent = decode_by_ino(sb, dir_ino, 0);
11503 +       if (IS_ERR(parent))
11504 +               goto out;
11505 +       if (!parent)
11506 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11507 +                                       dir_ino, fh, fh_len);
11508 +
11509 +out:
11510 +       AuTraceErrPtr(parent);
11511 +       return parent;
11512 +}
11513 +#endif
11514 +
11515 +/* ---------------------------------------------------------------------- */
11516 +
11517 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11518 +                         struct inode *dir)
11519 +{
11520 +       int err;
11521 +       aufs_bindex_t bindex;
11522 +       struct super_block *sb, *h_sb;
11523 +       struct dentry *dentry, *parent, *h_parent;
11524 +       struct inode *h_dir;
11525 +       struct au_branch *br;
11526 +
11527 +       err = -ENOSPC;
11528 +       if (unlikely(*max_len <= Fh_tail)) {
11529 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11530 +               goto out;
11531 +       }
11532 +
11533 +       err = FILEID_ROOT;
11534 +       if (inode->i_ino == AUFS_ROOT_INO) {
11535 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11536 +               goto out;
11537 +       }
11538 +
11539 +       h_parent = NULL;
11540 +       sb = inode->i_sb;
11541 +       err = si_read_lock(sb, AuLock_FLUSH);
11542 +       if (unlikely(err))
11543 +               goto out;
11544 +
11545 +#ifdef CONFIG_AUFS_DEBUG
11546 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11547 +               AuWarn1("NFS-exporting requires xino\n");
11548 +#endif
11549 +       err = -EIO;
11550 +       parent = NULL;
11551 +       ii_read_lock_child(inode);
11552 +       bindex = au_ibstart(inode);
11553 +       if (!dir) {
11554 +               dentry = d_find_any_alias(inode);
11555 +               if (unlikely(!dentry))
11556 +                       goto out_unlock;
11557 +               AuDebugOn(au_test_anon(dentry));
11558 +               parent = dget_parent(dentry);
11559 +               dput(dentry);
11560 +               if (unlikely(!parent))
11561 +                       goto out_unlock;
11562 +               if (d_really_is_positive(parent))
11563 +                       dir = d_inode(parent);
11564 +       }
11565 +
11566 +       ii_read_lock_parent(dir);
11567 +       h_dir = au_h_iptr(dir, bindex);
11568 +       ii_read_unlock(dir);
11569 +       if (unlikely(!h_dir))
11570 +               goto out_parent;
11571 +       h_parent = d_find_any_alias(h_dir);
11572 +       if (unlikely(!h_parent))
11573 +               goto out_hparent;
11574 +
11575 +       err = -EPERM;
11576 +       br = au_sbr(sb, bindex);
11577 +       h_sb = au_br_sb(br);
11578 +       if (unlikely(!h_sb->s_export_op)) {
11579 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11580 +               goto out_hparent;
11581 +       }
11582 +
11583 +       fh[Fh_br_id] = br->br_id;
11584 +       fh[Fh_sigen] = au_sigen(sb);
11585 +       encode_ino(fh + Fh_ino, inode->i_ino);
11586 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11587 +       fh[Fh_igen] = inode->i_generation;
11588 +
11589 +       *max_len -= Fh_tail;
11590 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11591 +                                          max_len,
11592 +                                          /*connectable or subtreecheck*/0);
11593 +       err = fh[Fh_h_type];
11594 +       *max_len += Fh_tail;
11595 +       /* todo: macros? */
11596 +       if (err != FILEID_INVALID)
11597 +               err = 99;
11598 +       else
11599 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11600 +
11601 +out_hparent:
11602 +       dput(h_parent);
11603 +out_parent:
11604 +       dput(parent);
11605 +out_unlock:
11606 +       ii_read_unlock(inode);
11607 +       si_read_unlock(sb);
11608 +out:
11609 +       if (unlikely(err < 0))
11610 +               err = FILEID_INVALID;
11611 +       return err;
11612 +}
11613 +
11614 +/* ---------------------------------------------------------------------- */
11615 +
11616 +static int aufs_commit_metadata(struct inode *inode)
11617 +{
11618 +       int err;
11619 +       aufs_bindex_t bindex;
11620 +       struct super_block *sb;
11621 +       struct inode *h_inode;
11622 +       int (*f)(struct inode *inode);
11623 +
11624 +       sb = inode->i_sb;
11625 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11626 +       ii_write_lock_child(inode);
11627 +       bindex = au_ibstart(inode);
11628 +       AuDebugOn(bindex < 0);
11629 +       h_inode = au_h_iptr(inode, bindex);
11630 +
11631 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11632 +       if (f)
11633 +               err = f(h_inode);
11634 +       else {
11635 +               struct writeback_control wbc = {
11636 +                       .sync_mode      = WB_SYNC_ALL,
11637 +                       .nr_to_write    = 0 /* metadata only */
11638 +               };
11639 +
11640 +               err = sync_inode(h_inode, &wbc);
11641 +       }
11642 +
11643 +       au_cpup_attr_timesizes(inode);
11644 +       ii_write_unlock(inode);
11645 +       si_read_unlock(sb);
11646 +       return err;
11647 +}
11648 +
11649 +/* ---------------------------------------------------------------------- */
11650 +
11651 +static struct export_operations aufs_export_op = {
11652 +       .fh_to_dentry           = aufs_fh_to_dentry,
11653 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11654 +       .encode_fh              = aufs_encode_fh,
11655 +       .commit_metadata        = aufs_commit_metadata
11656 +};
11657 +
11658 +void au_export_init(struct super_block *sb)
11659 +{
11660 +       struct au_sbinfo *sbinfo;
11661 +       __u32 u;
11662 +
11663 +       sb->s_export_op = &aufs_export_op;
11664 +       sbinfo = au_sbi(sb);
11665 +       sbinfo->si_xigen = NULL;
11666 +       get_random_bytes(&u, sizeof(u));
11667 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11668 +       atomic_set(&sbinfo->si_xigen_next, u);
11669 +}
11670 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11671 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11672 +++ linux/fs/aufs/fhsm.c        2016-04-24 18:32:51.393686895 +0200
11673 @@ -0,0 +1,426 @@
11674 +/*
11675 + * Copyright (C) 2011-2016 Junjiro R. Okajima
11676 + *
11677 + * This program, aufs is free software; you can redistribute it and/or modify
11678 + * it under the terms of the GNU General Public License as published by
11679 + * the Free Software Foundation; either version 2 of the License, or
11680 + * (at your option) any later version.
11681 + *
11682 + * This program is distributed in the hope that it will be useful,
11683 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11684 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11685 + * GNU General Public License for more details.
11686 + *
11687 + * You should have received a copy of the GNU General Public License
11688 + * along with this program; if not, write to the Free Software
11689 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11690 + */
11691 +
11692 +/*
11693 + * File-based Hierarchy Storage Management
11694 + */
11695 +
11696 +#include <linux/anon_inodes.h>
11697 +#include <linux/poll.h>
11698 +#include <linux/seq_file.h>
11699 +#include <linux/statfs.h>
11700 +#include "aufs.h"
11701 +
11702 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11703 +{
11704 +       struct au_sbinfo *sbinfo;
11705 +       struct au_fhsm *fhsm;
11706 +
11707 +       SiMustAnyLock(sb);
11708 +
11709 +       sbinfo = au_sbi(sb);
11710 +       fhsm = &sbinfo->si_fhsm;
11711 +       AuDebugOn(!fhsm);
11712 +       return fhsm->fhsm_bottom;
11713 +}
11714 +
11715 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11716 +{
11717 +       struct au_sbinfo *sbinfo;
11718 +       struct au_fhsm *fhsm;
11719 +
11720 +       SiMustWriteLock(sb);
11721 +
11722 +       sbinfo = au_sbi(sb);
11723 +       fhsm = &sbinfo->si_fhsm;
11724 +       AuDebugOn(!fhsm);
11725 +       fhsm->fhsm_bottom = bindex;
11726 +}
11727 +
11728 +/* ---------------------------------------------------------------------- */
11729 +
11730 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11731 +{
11732 +       struct au_br_fhsm *bf;
11733 +
11734 +       bf = br->br_fhsm;
11735 +       MtxMustLock(&bf->bf_lock);
11736 +
11737 +       return !bf->bf_readable
11738 +               || time_after(jiffies,
11739 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11740 +}
11741 +
11742 +/* ---------------------------------------------------------------------- */
11743 +
11744 +static void au_fhsm_notify(struct super_block *sb, int val)
11745 +{
11746 +       struct au_sbinfo *sbinfo;
11747 +       struct au_fhsm *fhsm;
11748 +
11749 +       SiMustAnyLock(sb);
11750 +
11751 +       sbinfo = au_sbi(sb);
11752 +       fhsm = &sbinfo->si_fhsm;
11753 +       if (au_fhsm_pid(fhsm)
11754 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11755 +               atomic_set(&fhsm->fhsm_readable, val);
11756 +               if (val)
11757 +                       wake_up(&fhsm->fhsm_wqh);
11758 +       }
11759 +}
11760 +
11761 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11762 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11763 +{
11764 +       int err;
11765 +       struct au_branch *br;
11766 +       struct au_br_fhsm *bf;
11767 +
11768 +       br = au_sbr(sb, bindex);
11769 +       AuDebugOn(au_br_rdonly(br));
11770 +       bf = br->br_fhsm;
11771 +       AuDebugOn(!bf);
11772 +
11773 +       if (do_lock)
11774 +               mutex_lock(&bf->bf_lock);
11775 +       else
11776 +               MtxMustLock(&bf->bf_lock);
11777 +
11778 +       /* sb->s_root for NFS is unreliable */
11779 +       err = au_br_stfs(br, &bf->bf_stfs);
11780 +       if (unlikely(err)) {
11781 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11782 +               goto out;
11783 +       }
11784 +
11785 +       bf->bf_jiffy = jiffies;
11786 +       bf->bf_readable = 1;
11787 +       if (do_notify)
11788 +               au_fhsm_notify(sb, /*val*/1);
11789 +       if (rstfs)
11790 +               *rstfs = bf->bf_stfs;
11791 +
11792 +out:
11793 +       if (do_lock)
11794 +               mutex_unlock(&bf->bf_lock);
11795 +       au_fhsm_notify(sb, /*val*/1);
11796 +
11797 +       return err;
11798 +}
11799 +
11800 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11801 +{
11802 +       int err;
11803 +       struct au_sbinfo *sbinfo;
11804 +       struct au_fhsm *fhsm;
11805 +       struct au_branch *br;
11806 +       struct au_br_fhsm *bf;
11807 +
11808 +       AuDbg("b%d, force %d\n", bindex, force);
11809 +       SiMustAnyLock(sb);
11810 +
11811 +       sbinfo = au_sbi(sb);
11812 +       fhsm = &sbinfo->si_fhsm;
11813 +       if (!au_ftest_si(sbinfo, FHSM)
11814 +           || fhsm->fhsm_bottom == bindex)
11815 +               return;
11816 +
11817 +       br = au_sbr(sb, bindex);
11818 +       bf = br->br_fhsm;
11819 +       AuDebugOn(!bf);
11820 +       mutex_lock(&bf->bf_lock);
11821 +       if (force
11822 +           || au_fhsm_pid(fhsm)
11823 +           || au_fhsm_test_jiffy(sbinfo, br))
11824 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11825 +                                 /*do_notify*/1);
11826 +       mutex_unlock(&bf->bf_lock);
11827 +}
11828 +
11829 +void au_fhsm_wrote_all(struct super_block *sb, int force)
11830 +{
11831 +       aufs_bindex_t bindex, bend;
11832 +       struct au_branch *br;
11833 +
11834 +       /* exclude the bottom */
11835 +       bend = au_fhsm_bottom(sb);
11836 +       for (bindex = 0; bindex < bend; bindex++) {
11837 +               br = au_sbr(sb, bindex);
11838 +               if (au_br_fhsm(br->br_perm))
11839 +                       au_fhsm_wrote(sb, bindex, force);
11840 +       }
11841 +}
11842 +
11843 +/* ---------------------------------------------------------------------- */
11844 +
11845 +static unsigned int au_fhsm_poll(struct file *file,
11846 +                                struct poll_table_struct *wait)
11847 +{
11848 +       unsigned int mask;
11849 +       struct au_sbinfo *sbinfo;
11850 +       struct au_fhsm *fhsm;
11851 +
11852 +       mask = 0;
11853 +       sbinfo = file->private_data;
11854 +       fhsm = &sbinfo->si_fhsm;
11855 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
11856 +       if (atomic_read(&fhsm->fhsm_readable))
11857 +               mask = POLLIN /* | POLLRDNORM */;
11858 +
11859 +       AuTraceErr((int)mask);
11860 +       return mask;
11861 +}
11862 +
11863 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11864 +                             struct aufs_stfs *stfs, __s16 brid)
11865 +{
11866 +       int err;
11867 +
11868 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11869 +       if (!err)
11870 +               err = __put_user(brid, &stbr->brid);
11871 +       if (unlikely(err))
11872 +               err = -EFAULT;
11873 +
11874 +       return err;
11875 +}
11876 +
11877 +static ssize_t au_fhsm_do_read(struct super_block *sb,
11878 +                              struct aufs_stbr __user *stbr, size_t count)
11879 +{
11880 +       ssize_t err;
11881 +       int nstbr;
11882 +       aufs_bindex_t bindex, bend;
11883 +       struct au_branch *br;
11884 +       struct au_br_fhsm *bf;
11885 +
11886 +       /* except the bottom branch */
11887 +       err = 0;
11888 +       nstbr = 0;
11889 +       bend = au_fhsm_bottom(sb);
11890 +       for (bindex = 0; !err && bindex < bend; bindex++) {
11891 +               br = au_sbr(sb, bindex);
11892 +               if (!au_br_fhsm(br->br_perm))
11893 +                       continue;
11894 +
11895 +               bf = br->br_fhsm;
11896 +               mutex_lock(&bf->bf_lock);
11897 +               if (bf->bf_readable) {
11898 +                       err = -EFAULT;
11899 +                       if (count >= sizeof(*stbr))
11900 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
11901 +                                                         br->br_id);
11902 +                       if (!err) {
11903 +                               bf->bf_readable = 0;
11904 +                               count -= sizeof(*stbr);
11905 +                               nstbr++;
11906 +                       }
11907 +               }
11908 +               mutex_unlock(&bf->bf_lock);
11909 +       }
11910 +       if (!err)
11911 +               err = sizeof(*stbr) * nstbr;
11912 +
11913 +       return err;
11914 +}
11915 +
11916 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
11917 +                          loff_t *pos)
11918 +{
11919 +       ssize_t err;
11920 +       int readable;
11921 +       aufs_bindex_t nfhsm, bindex, bend;
11922 +       struct au_sbinfo *sbinfo;
11923 +       struct au_fhsm *fhsm;
11924 +       struct au_branch *br;
11925 +       struct super_block *sb;
11926 +
11927 +       err = 0;
11928 +       sbinfo = file->private_data;
11929 +       fhsm = &sbinfo->si_fhsm;
11930 +need_data:
11931 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
11932 +       if (!atomic_read(&fhsm->fhsm_readable)) {
11933 +               if (vfsub_file_flags(file) & O_NONBLOCK)
11934 +                       err = -EAGAIN;
11935 +               else
11936 +                       err = wait_event_interruptible_locked_irq
11937 +                               (fhsm->fhsm_wqh,
11938 +                                atomic_read(&fhsm->fhsm_readable));
11939 +       }
11940 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
11941 +       if (unlikely(err))
11942 +               goto out;
11943 +
11944 +       /* sb may already be dead */
11945 +       au_rw_read_lock(&sbinfo->si_rwsem);
11946 +       readable = atomic_read(&fhsm->fhsm_readable);
11947 +       if (readable > 0) {
11948 +               sb = sbinfo->si_sb;
11949 +               AuDebugOn(!sb);
11950 +               /* exclude the bottom branch */
11951 +               nfhsm = 0;
11952 +               bend = au_fhsm_bottom(sb);
11953 +               for (bindex = 0; bindex < bend; bindex++) {
11954 +                       br = au_sbr(sb, bindex);
11955 +                       if (au_br_fhsm(br->br_perm))
11956 +                               nfhsm++;
11957 +               }
11958 +               err = -EMSGSIZE;
11959 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
11960 +                       atomic_set(&fhsm->fhsm_readable, 0);
11961 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
11962 +                                            count);
11963 +               }
11964 +       }
11965 +       au_rw_read_unlock(&sbinfo->si_rwsem);
11966 +       if (!readable)
11967 +               goto need_data;
11968 +
11969 +out:
11970 +       return err;
11971 +}
11972 +
11973 +static int au_fhsm_release(struct inode *inode, struct file *file)
11974 +{
11975 +       struct au_sbinfo *sbinfo;
11976 +       struct au_fhsm *fhsm;
11977 +
11978 +       /* sb may already be dead */
11979 +       sbinfo = file->private_data;
11980 +       fhsm = &sbinfo->si_fhsm;
11981 +       spin_lock(&fhsm->fhsm_spin);
11982 +       fhsm->fhsm_pid = 0;
11983 +       spin_unlock(&fhsm->fhsm_spin);
11984 +       kobject_put(&sbinfo->si_kobj);
11985 +
11986 +       return 0;
11987 +}
11988 +
11989 +static const struct file_operations au_fhsm_fops = {
11990 +       .owner          = THIS_MODULE,
11991 +       .llseek         = noop_llseek,
11992 +       .read           = au_fhsm_read,
11993 +       .poll           = au_fhsm_poll,
11994 +       .release        = au_fhsm_release
11995 +};
11996 +
11997 +int au_fhsm_fd(struct super_block *sb, int oflags)
11998 +{
11999 +       int err, fd;
12000 +       struct au_sbinfo *sbinfo;
12001 +       struct au_fhsm *fhsm;
12002 +
12003 +       err = -EPERM;
12004 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
12005 +               goto out;
12006 +
12007 +       err = -EINVAL;
12008 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
12009 +               goto out;
12010 +
12011 +       err = 0;
12012 +       sbinfo = au_sbi(sb);
12013 +       fhsm = &sbinfo->si_fhsm;
12014 +       spin_lock(&fhsm->fhsm_spin);
12015 +       if (!fhsm->fhsm_pid)
12016 +               fhsm->fhsm_pid = current->pid;
12017 +       else
12018 +               err = -EBUSY;
12019 +       spin_unlock(&fhsm->fhsm_spin);
12020 +       if (unlikely(err))
12021 +               goto out;
12022 +
12023 +       oflags |= O_RDONLY;
12024 +       /* oflags |= FMODE_NONOTIFY; */
12025 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
12026 +       err = fd;
12027 +       if (unlikely(fd < 0))
12028 +               goto out_pid;
12029 +
12030 +       /* succeed reglardless 'fhsm' status */
12031 +       kobject_get(&sbinfo->si_kobj);
12032 +       si_noflush_read_lock(sb);
12033 +       if (au_ftest_si(sbinfo, FHSM))
12034 +               au_fhsm_wrote_all(sb, /*force*/0);
12035 +       si_read_unlock(sb);
12036 +       goto out; /* success */
12037 +
12038 +out_pid:
12039 +       spin_lock(&fhsm->fhsm_spin);
12040 +       fhsm->fhsm_pid = 0;
12041 +       spin_unlock(&fhsm->fhsm_spin);
12042 +out:
12043 +       AuTraceErr(err);
12044 +       return err;
12045 +}
12046 +
12047 +/* ---------------------------------------------------------------------- */
12048 +
12049 +int au_fhsm_br_alloc(struct au_branch *br)
12050 +{
12051 +       int err;
12052 +
12053 +       err = 0;
12054 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
12055 +       if (br->br_fhsm)
12056 +               au_br_fhsm_init(br->br_fhsm);
12057 +       else
12058 +               err = -ENOMEM;
12059 +
12060 +       return err;
12061 +}
12062 +
12063 +/* ---------------------------------------------------------------------- */
12064 +
12065 +void au_fhsm_fin(struct super_block *sb)
12066 +{
12067 +       au_fhsm_notify(sb, /*val*/-1);
12068 +}
12069 +
12070 +void au_fhsm_init(struct au_sbinfo *sbinfo)
12071 +{
12072 +       struct au_fhsm *fhsm;
12073 +
12074 +       fhsm = &sbinfo->si_fhsm;
12075 +       spin_lock_init(&fhsm->fhsm_spin);
12076 +       init_waitqueue_head(&fhsm->fhsm_wqh);
12077 +       atomic_set(&fhsm->fhsm_readable, 0);
12078 +       fhsm->fhsm_expire
12079 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
12080 +       fhsm->fhsm_bottom = -1;
12081 +}
12082 +
12083 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
12084 +{
12085 +       sbinfo->si_fhsm.fhsm_expire
12086 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
12087 +}
12088 +
12089 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
12090 +{
12091 +       unsigned int u;
12092 +
12093 +       if (!au_ftest_si(sbinfo, FHSM))
12094 +               return;
12095 +
12096 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
12097 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
12098 +               seq_printf(seq, ",fhsm_sec=%u", u);
12099 +}
12100 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
12101 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
12102 +++ linux/fs/aufs/file.c        2016-04-24 18:32:51.393686895 +0200
12103 @@ -0,0 +1,844 @@
12104 +/*
12105 + * Copyright (C) 2005-2016 Junjiro R. Okajima
12106 + *
12107 + * This program, aufs is free software; you can redistribute it and/or modify
12108 + * it under the terms of the GNU General Public License as published by
12109 + * the Free Software Foundation; either version 2 of the License, or
12110 + * (at your option) any later version.
12111 + *
12112 + * This program is distributed in the hope that it will be useful,
12113 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12114 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12115 + * GNU General Public License for more details.
12116 + *
12117 + * You should have received a copy of the GNU General Public License
12118 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12119 + */
12120 +
12121 +/*
12122 + * handling file/dir, and address_space operation
12123 + */
12124 +
12125 +#ifdef CONFIG_AUFS_DEBUG
12126 +#include <linux/migrate.h>
12127 +#endif
12128 +#include <linux/pagemap.h>
12129 +#include "aufs.h"
12130 +
12131 +/* drop flags for writing */
12132 +unsigned int au_file_roflags(unsigned int flags)
12133 +{
12134 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
12135 +       flags |= O_RDONLY | O_NOATIME;
12136 +       return flags;
12137 +}
12138 +
12139 +/* common functions to regular file and dir */
12140 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12141 +                      struct file *file, int force_wr)
12142 +{
12143 +       struct file *h_file;
12144 +       struct dentry *h_dentry;
12145 +       struct inode *h_inode;
12146 +       struct super_block *sb;
12147 +       struct au_branch *br;
12148 +       struct path h_path;
12149 +       int err;
12150 +
12151 +       /* a race condition can happen between open and unlink/rmdir */
12152 +       h_file = ERR_PTR(-ENOENT);
12153 +       h_dentry = au_h_dptr(dentry, bindex);
12154 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
12155 +               goto out;
12156 +       h_inode = d_inode(h_dentry);
12157 +       spin_lock(&h_dentry->d_lock);
12158 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
12159 +               /* || !d_inode(dentry)->i_nlink */
12160 +               ;
12161 +       spin_unlock(&h_dentry->d_lock);
12162 +       if (unlikely(err))
12163 +               goto out;
12164 +
12165 +       sb = dentry->d_sb;
12166 +       br = au_sbr(sb, bindex);
12167 +       err = au_br_test_oflag(flags, br);
12168 +       h_file = ERR_PTR(err);
12169 +       if (unlikely(err))
12170 +               goto out;
12171 +
12172 +       /* drop flags for writing */
12173 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
12174 +               if (force_wr && !(flags & O_WRONLY))
12175 +                       force_wr = 0;
12176 +               flags = au_file_roflags(flags);
12177 +               if (force_wr) {
12178 +                       h_file = ERR_PTR(-EROFS);
12179 +                       flags = au_file_roflags(flags);
12180 +                       if (unlikely(vfsub_native_ro(h_inode)
12181 +                                    || IS_APPEND(h_inode)))
12182 +                               goto out;
12183 +                       flags &= ~O_ACCMODE;
12184 +                       flags |= O_WRONLY;
12185 +               }
12186 +       }
12187 +       flags &= ~O_CREAT;
12188 +       atomic_inc(&br->br_count);
12189 +       h_path.dentry = h_dentry;
12190 +       h_path.mnt = au_br_mnt(br);
12191 +       h_file = vfsub_dentry_open(&h_path, flags);
12192 +       if (IS_ERR(h_file))
12193 +               goto out_br;
12194 +
12195 +       if (flags & __FMODE_EXEC) {
12196 +               err = deny_write_access(h_file);
12197 +               if (unlikely(err)) {
12198 +                       fput(h_file);
12199 +                       h_file = ERR_PTR(err);
12200 +                       goto out_br;
12201 +               }
12202 +       }
12203 +       fsnotify_open(h_file);
12204 +       goto out; /* success */
12205 +
12206 +out_br:
12207 +       atomic_dec(&br->br_count);
12208 +out:
12209 +       return h_file;
12210 +}
12211 +
12212 +static int au_cmoo(struct dentry *dentry)
12213 +{
12214 +       int err, cmoo;
12215 +       unsigned int udba;
12216 +       struct path h_path;
12217 +       struct au_pin pin;
12218 +       struct au_cp_generic cpg = {
12219 +               .dentry = dentry,
12220 +               .bdst   = -1,
12221 +               .bsrc   = -1,
12222 +               .len    = -1,
12223 +               .pin    = &pin,
12224 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12225 +       };
12226 +       struct inode *delegated;
12227 +       struct super_block *sb;
12228 +       struct au_sbinfo *sbinfo;
12229 +       struct au_fhsm *fhsm;
12230 +       pid_t pid;
12231 +       struct au_branch *br;
12232 +       struct dentry *parent;
12233 +       struct au_hinode *hdir;
12234 +
12235 +       DiMustWriteLock(dentry);
12236 +       IiMustWriteLock(d_inode(dentry));
12237 +
12238 +       err = 0;
12239 +       if (IS_ROOT(dentry))
12240 +               goto out;
12241 +       cpg.bsrc = au_dbstart(dentry);
12242 +       if (!cpg.bsrc)
12243 +               goto out;
12244 +
12245 +       sb = dentry->d_sb;
12246 +       sbinfo = au_sbi(sb);
12247 +       fhsm = &sbinfo->si_fhsm;
12248 +       pid = au_fhsm_pid(fhsm);
12249 +       if (pid
12250 +           && (current->pid == pid
12251 +               || current->real_parent->pid == pid))
12252 +               goto out;
12253 +
12254 +       br = au_sbr(sb, cpg.bsrc);
12255 +       cmoo = au_br_cmoo(br->br_perm);
12256 +       if (!cmoo)
12257 +               goto out;
12258 +       if (!d_is_reg(dentry))
12259 +               cmoo &= AuBrAttr_COO_ALL;
12260 +       if (!cmoo)
12261 +               goto out;
12262 +
12263 +       parent = dget_parent(dentry);
12264 +       di_write_lock_parent(parent);
12265 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12266 +       cpg.bdst = err;
12267 +       if (unlikely(err < 0)) {
12268 +               err = 0;        /* there is no upper writable branch */
12269 +               goto out_dgrade;
12270 +       }
12271 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12272 +
12273 +       /* do not respect the coo attrib for the target branch */
12274 +       err = au_cpup_dirs(dentry, cpg.bdst);
12275 +       if (unlikely(err))
12276 +               goto out_dgrade;
12277 +
12278 +       di_downgrade_lock(parent, AuLock_IR);
12279 +       udba = au_opt_udba(sb);
12280 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12281 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12282 +       if (unlikely(err))
12283 +               goto out_parent;
12284 +
12285 +       err = au_sio_cpup_simple(&cpg);
12286 +       au_unpin(&pin);
12287 +       if (unlikely(err))
12288 +               goto out_parent;
12289 +       if (!(cmoo & AuBrWAttr_MOO))
12290 +               goto out_parent; /* success */
12291 +
12292 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12293 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12294 +       if (unlikely(err))
12295 +               goto out_parent;
12296 +
12297 +       h_path.mnt = au_br_mnt(br);
12298 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12299 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
12300 +       delegated = NULL;
12301 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12302 +       au_unpin(&pin);
12303 +       /* todo: keep h_dentry or not? */
12304 +       if (unlikely(err == -EWOULDBLOCK)) {
12305 +               pr_warn("cannot retry for NFSv4 delegation"
12306 +                       " for an internal unlink\n");
12307 +               iput(delegated);
12308 +       }
12309 +       if (unlikely(err)) {
12310 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12311 +                      dentry, err);
12312 +               err = 0;
12313 +       }
12314 +       goto out_parent; /* success */
12315 +
12316 +out_dgrade:
12317 +       di_downgrade_lock(parent, AuLock_IR);
12318 +out_parent:
12319 +       di_read_unlock(parent, AuLock_IR);
12320 +       dput(parent);
12321 +out:
12322 +       AuTraceErr(err);
12323 +       return err;
12324 +}
12325 +
12326 +int au_do_open(struct file *file, struct au_do_open_args *args)
12327 +{
12328 +       int err, no_lock = args->no_lock;
12329 +       struct dentry *dentry;
12330 +       struct au_finfo *finfo;
12331 +
12332 +       if (!no_lock)
12333 +               err = au_finfo_init(file, args->fidir);
12334 +       else {
12335 +               lockdep_off();
12336 +               err = au_finfo_init(file, args->fidir);
12337 +               lockdep_on();
12338 +       }
12339 +       if (unlikely(err))
12340 +               goto out;
12341 +
12342 +       dentry = file->f_path.dentry;
12343 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12344 +       if (!no_lock) {
12345 +               di_write_lock_child(dentry);
12346 +               err = au_cmoo(dentry);
12347 +               di_downgrade_lock(dentry, AuLock_IR);
12348 +               if (!err)
12349 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12350 +               di_read_unlock(dentry, AuLock_IR);
12351 +       } else {
12352 +               err = au_cmoo(dentry);
12353 +               if (!err)
12354 +                       err = args->open(file, vfsub_file_flags(file),
12355 +                                        args->h_file);
12356 +               if (!err && au_fbstart(file) != au_dbstart(dentry))
12357 +                       /*
12358 +                        * cmoo happens after h_file was opened.
12359 +                        * need to refresh file later.
12360 +                        */
12361 +                       atomic_dec(&au_fi(file)->fi_generation);
12362 +       }
12363 +
12364 +       finfo = au_fi(file);
12365 +       if (!err) {
12366 +               finfo->fi_file = file;
12367 +               au_sphl_add(&finfo->fi_hlist,
12368 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12369 +       }
12370 +       if (!no_lock)
12371 +               fi_write_unlock(file);
12372 +       else {
12373 +               lockdep_off();
12374 +               fi_write_unlock(file);
12375 +               lockdep_on();
12376 +       }
12377 +       if (unlikely(err)) {
12378 +               finfo->fi_hdir = NULL;
12379 +               au_finfo_fin(file);
12380 +       }
12381 +
12382 +out:
12383 +       return err;
12384 +}
12385 +
12386 +int au_reopen_nondir(struct file *file)
12387 +{
12388 +       int err;
12389 +       aufs_bindex_t bstart;
12390 +       struct dentry *dentry;
12391 +       struct file *h_file, *h_file_tmp;
12392 +
12393 +       dentry = file->f_path.dentry;
12394 +       bstart = au_dbstart(dentry);
12395 +       h_file_tmp = NULL;
12396 +       if (au_fbstart(file) == bstart) {
12397 +               h_file = au_hf_top(file);
12398 +               if (file->f_mode == h_file->f_mode)
12399 +                       return 0; /* success */
12400 +               h_file_tmp = h_file;
12401 +               get_file(h_file_tmp);
12402 +               au_set_h_fptr(file, bstart, NULL);
12403 +       }
12404 +       AuDebugOn(au_fi(file)->fi_hdir);
12405 +       /*
12406 +        * it can happen
12407 +        * file exists on both of rw and ro
12408 +        * open --> dbstart and fbstart are both 0
12409 +        * prepend a branch as rw, "rw" become ro
12410 +        * remove rw/file
12411 +        * delete the top branch, "rw" becomes rw again
12412 +        *      --> dbstart is 1, fbstart is still 0
12413 +        * write --> fbstart is 0 but dbstart is 1
12414 +        */
12415 +       /* AuDebugOn(au_fbstart(file) < bstart); */
12416 +
12417 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
12418 +                          file, /*force_wr*/0);
12419 +       err = PTR_ERR(h_file);
12420 +       if (IS_ERR(h_file)) {
12421 +               if (h_file_tmp) {
12422 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
12423 +                       au_set_h_fptr(file, bstart, h_file_tmp);
12424 +                       h_file_tmp = NULL;
12425 +               }
12426 +               goto out; /* todo: close all? */
12427 +       }
12428 +
12429 +       err = 0;
12430 +       au_set_fbstart(file, bstart);
12431 +       au_set_h_fptr(file, bstart, h_file);
12432 +       au_update_figen(file);
12433 +       /* todo: necessary? */
12434 +       /* file->f_ra = h_file->f_ra; */
12435 +
12436 +out:
12437 +       if (h_file_tmp)
12438 +               fput(h_file_tmp);
12439 +       return err;
12440 +}
12441 +
12442 +/* ---------------------------------------------------------------------- */
12443 +
12444 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12445 +                       struct dentry *hi_wh)
12446 +{
12447 +       int err;
12448 +       aufs_bindex_t bstart;
12449 +       struct au_dinfo *dinfo;
12450 +       struct dentry *h_dentry;
12451 +       struct au_hdentry *hdp;
12452 +
12453 +       dinfo = au_di(file->f_path.dentry);
12454 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12455 +
12456 +       bstart = dinfo->di_bstart;
12457 +       dinfo->di_bstart = btgt;
12458 +       hdp = dinfo->di_hdentry;
12459 +       h_dentry = hdp[0 + btgt].hd_dentry;
12460 +       hdp[0 + btgt].hd_dentry = hi_wh;
12461 +       err = au_reopen_nondir(file);
12462 +       hdp[0 + btgt].hd_dentry = h_dentry;
12463 +       dinfo->di_bstart = bstart;
12464 +
12465 +       return err;
12466 +}
12467 +
12468 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12469 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12470 +{
12471 +       int err;
12472 +       struct inode *inode, *h_inode;
12473 +       struct dentry *h_dentry, *hi_wh;
12474 +       struct au_cp_generic cpg = {
12475 +               .dentry = file->f_path.dentry,
12476 +               .bdst   = bcpup,
12477 +               .bsrc   = -1,
12478 +               .len    = len,
12479 +               .pin    = pin
12480 +       };
12481 +
12482 +       au_update_dbstart(cpg.dentry);
12483 +       inode = d_inode(cpg.dentry);
12484 +       h_inode = NULL;
12485 +       if (au_dbstart(cpg.dentry) <= bcpup
12486 +           && au_dbend(cpg.dentry) >= bcpup) {
12487 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12488 +               if (h_dentry && d_is_positive(h_dentry))
12489 +                       h_inode = d_inode(h_dentry);
12490 +       }
12491 +       hi_wh = au_hi_wh(inode, bcpup);
12492 +       if (!hi_wh && !h_inode)
12493 +               err = au_sio_cpup_wh(&cpg, file);
12494 +       else
12495 +               /* already copied-up after unlink */
12496 +               err = au_reopen_wh(file, bcpup, hi_wh);
12497 +
12498 +       if (!err
12499 +           && (inode->i_nlink > 1
12500 +               || (inode->i_state & I_LINKABLE))
12501 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12502 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12503 +
12504 +       return err;
12505 +}
12506 +
12507 +/*
12508 + * prepare the @file for writing.
12509 + */
12510 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12511 +{
12512 +       int err;
12513 +       aufs_bindex_t dbstart;
12514 +       struct dentry *parent;
12515 +       struct inode *inode;
12516 +       struct super_block *sb;
12517 +       struct file *h_file;
12518 +       struct au_cp_generic cpg = {
12519 +               .dentry = file->f_path.dentry,
12520 +               .bdst   = -1,
12521 +               .bsrc   = -1,
12522 +               .len    = len,
12523 +               .pin    = pin,
12524 +               .flags  = AuCpup_DTIME
12525 +       };
12526 +
12527 +       sb = cpg.dentry->d_sb;
12528 +       inode = d_inode(cpg.dentry);
12529 +       cpg.bsrc = au_fbstart(file);
12530 +       err = au_test_ro(sb, cpg.bsrc, inode);
12531 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12532 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12533 +                            /*flags*/0);
12534 +               goto out;
12535 +       }
12536 +
12537 +       /* need to cpup or reopen */
12538 +       parent = dget_parent(cpg.dentry);
12539 +       di_write_lock_parent(parent);
12540 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12541 +       cpg.bdst = err;
12542 +       if (unlikely(err < 0))
12543 +               goto out_dgrade;
12544 +       err = 0;
12545 +
12546 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12547 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12548 +               if (unlikely(err))
12549 +                       goto out_dgrade;
12550 +       }
12551 +
12552 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12553 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12554 +       if (unlikely(err))
12555 +               goto out_dgrade;
12556 +
12557 +       dbstart = au_dbstart(cpg.dentry);
12558 +       if (dbstart <= cpg.bdst)
12559 +               cpg.bsrc = cpg.bdst;
12560 +
12561 +       if (dbstart <= cpg.bdst         /* just reopen */
12562 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12563 +               ) {
12564 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12565 +               if (IS_ERR(h_file))
12566 +                       err = PTR_ERR(h_file);
12567 +               else {
12568 +                       di_downgrade_lock(parent, AuLock_IR);
12569 +                       if (dbstart > cpg.bdst)
12570 +                               err = au_sio_cpup_simple(&cpg);
12571 +                       if (!err)
12572 +                               err = au_reopen_nondir(file);
12573 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12574 +               }
12575 +       } else {                        /* copyup as wh and reopen */
12576 +               /*
12577 +                * since writable hfsplus branch is not supported,
12578 +                * h_open_pre/post() are unnecessary.
12579 +                */
12580 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12581 +               di_downgrade_lock(parent, AuLock_IR);
12582 +       }
12583 +
12584 +       if (!err) {
12585 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12586 +               goto out_dput; /* success */
12587 +       }
12588 +       au_unpin(pin);
12589 +       goto out_unlock;
12590 +
12591 +out_dgrade:
12592 +       di_downgrade_lock(parent, AuLock_IR);
12593 +out_unlock:
12594 +       di_read_unlock(parent, AuLock_IR);
12595 +out_dput:
12596 +       dput(parent);
12597 +out:
12598 +       return err;
12599 +}
12600 +
12601 +/* ---------------------------------------------------------------------- */
12602 +
12603 +int au_do_flush(struct file *file, fl_owner_t id,
12604 +               int (*flush)(struct file *file, fl_owner_t id))
12605 +{
12606 +       int err;
12607 +       struct super_block *sb;
12608 +       struct inode *inode;
12609 +
12610 +       inode = file_inode(file);
12611 +       sb = inode->i_sb;
12612 +       si_noflush_read_lock(sb);
12613 +       fi_read_lock(file);
12614 +       ii_read_lock_child(inode);
12615 +
12616 +       err = flush(file, id);
12617 +       au_cpup_attr_timesizes(inode);
12618 +
12619 +       ii_read_unlock(inode);
12620 +       fi_read_unlock(file);
12621 +       si_read_unlock(sb);
12622 +       return err;
12623 +}
12624 +
12625 +/* ---------------------------------------------------------------------- */
12626 +
12627 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12628 +{
12629 +       int err;
12630 +       struct au_pin pin;
12631 +       struct au_finfo *finfo;
12632 +       struct dentry *parent, *hi_wh;
12633 +       struct inode *inode;
12634 +       struct super_block *sb;
12635 +       struct au_cp_generic cpg = {
12636 +               .dentry = file->f_path.dentry,
12637 +               .bdst   = -1,
12638 +               .bsrc   = -1,
12639 +               .len    = -1,
12640 +               .pin    = &pin,
12641 +               .flags  = AuCpup_DTIME
12642 +       };
12643 +
12644 +       FiMustWriteLock(file);
12645 +
12646 +       err = 0;
12647 +       finfo = au_fi(file);
12648 +       sb = cpg.dentry->d_sb;
12649 +       inode = d_inode(cpg.dentry);
12650 +       cpg.bdst = au_ibstart(inode);
12651 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12652 +               goto out;
12653 +
12654 +       parent = dget_parent(cpg.dentry);
12655 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12656 +               di_read_lock_parent(parent, !AuLock_IR);
12657 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12658 +               cpg.bdst = err;
12659 +               di_read_unlock(parent, !AuLock_IR);
12660 +               if (unlikely(err < 0))
12661 +                       goto out_parent;
12662 +               err = 0;
12663 +       }
12664 +
12665 +       di_read_lock_parent(parent, AuLock_IR);
12666 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12667 +       if (!S_ISDIR(inode->i_mode)
12668 +           && au_opt_test(au_mntflags(sb), PLINK)
12669 +           && au_plink_test(inode)
12670 +           && !d_unhashed(cpg.dentry)
12671 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
12672 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12673 +               if (unlikely(err))
12674 +                       goto out_unlock;
12675 +
12676 +               /* always superio. */
12677 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12678 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12679 +               if (!err) {
12680 +                       err = au_sio_cpup_simple(&cpg);
12681 +                       au_unpin(&pin);
12682 +               }
12683 +       } else if (hi_wh) {
12684 +               /* already copied-up after unlink */
12685 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12686 +               *need_reopen = 0;
12687 +       }
12688 +
12689 +out_unlock:
12690 +       di_read_unlock(parent, AuLock_IR);
12691 +out_parent:
12692 +       dput(parent);
12693 +out:
12694 +       return err;
12695 +}
12696 +
12697 +static void au_do_refresh_dir(struct file *file)
12698 +{
12699 +       aufs_bindex_t bindex, bend, new_bindex, brid;
12700 +       struct au_hfile *p, tmp, *q;
12701 +       struct au_finfo *finfo;
12702 +       struct super_block *sb;
12703 +       struct au_fidir *fidir;
12704 +
12705 +       FiMustWriteLock(file);
12706 +
12707 +       sb = file->f_path.dentry->d_sb;
12708 +       finfo = au_fi(file);
12709 +       fidir = finfo->fi_hdir;
12710 +       AuDebugOn(!fidir);
12711 +       p = fidir->fd_hfile + finfo->fi_btop;
12712 +       brid = p->hf_br->br_id;
12713 +       bend = fidir->fd_bbot;
12714 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
12715 +               if (!p->hf_file)
12716 +                       continue;
12717 +
12718 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12719 +               if (new_bindex == bindex)
12720 +                       continue;
12721 +               if (new_bindex < 0) {
12722 +                       au_set_h_fptr(file, bindex, NULL);
12723 +                       continue;
12724 +               }
12725 +
12726 +               /* swap two lower inode, and loop again */
12727 +               q = fidir->fd_hfile + new_bindex;
12728 +               tmp = *q;
12729 +               *q = *p;
12730 +               *p = tmp;
12731 +               if (tmp.hf_file) {
12732 +                       bindex--;
12733 +                       p--;
12734 +               }
12735 +       }
12736 +
12737 +       p = fidir->fd_hfile;
12738 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12739 +               bend = au_sbend(sb);
12740 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
12741 +                    finfo->fi_btop++, p++)
12742 +                       if (p->hf_file) {
12743 +                               if (file_inode(p->hf_file))
12744 +                                       break;
12745 +                               au_hfput(p, file);
12746 +                       }
12747 +       } else {
12748 +               bend = au_br_index(sb, brid);
12749 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
12750 +                    finfo->fi_btop++, p++)
12751 +                       if (p->hf_file)
12752 +                               au_hfput(p, file);
12753 +               bend = au_sbend(sb);
12754 +       }
12755 +
12756 +       p = fidir->fd_hfile + bend;
12757 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
12758 +            fidir->fd_bbot--, p--)
12759 +               if (p->hf_file) {
12760 +                       if (file_inode(p->hf_file))
12761 +                               break;
12762 +                       au_hfput(p, file);
12763 +               }
12764 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12765 +}
12766 +
12767 +/*
12768 + * after branch manipulating, refresh the file.
12769 + */
12770 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12771 +{
12772 +       int err, need_reopen;
12773 +       aufs_bindex_t bend, bindex;
12774 +       struct dentry *dentry;
12775 +       struct au_finfo *finfo;
12776 +       struct au_hfile *hfile;
12777 +
12778 +       dentry = file->f_path.dentry;
12779 +       finfo = au_fi(file);
12780 +       if (!finfo->fi_hdir) {
12781 +               hfile = &finfo->fi_htop;
12782 +               AuDebugOn(!hfile->hf_file);
12783 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12784 +               AuDebugOn(bindex < 0);
12785 +               if (bindex != finfo->fi_btop)
12786 +                       au_set_fbstart(file, bindex);
12787 +       } else {
12788 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
12789 +               if (unlikely(err))
12790 +                       goto out;
12791 +               au_do_refresh_dir(file);
12792 +       }
12793 +
12794 +       err = 0;
12795 +       need_reopen = 1;
12796 +       if (!au_test_mmapped(file))
12797 +               err = au_file_refresh_by_inode(file, &need_reopen);
12798 +       if (!err && need_reopen && !d_unlinked(dentry))
12799 +               err = reopen(file);
12800 +       if (!err) {
12801 +               au_update_figen(file);
12802 +               goto out; /* success */
12803 +       }
12804 +
12805 +       /* error, close all lower files */
12806 +       if (finfo->fi_hdir) {
12807 +               bend = au_fbend_dir(file);
12808 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
12809 +                       au_set_h_fptr(file, bindex, NULL);
12810 +       }
12811 +
12812 +out:
12813 +       return err;
12814 +}
12815 +
12816 +/* common function to regular file and dir */
12817 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12818 +                         int wlock)
12819 +{
12820 +       int err;
12821 +       unsigned int sigen, figen;
12822 +       aufs_bindex_t bstart;
12823 +       unsigned char pseudo_link;
12824 +       struct dentry *dentry;
12825 +       struct inode *inode;
12826 +
12827 +       err = 0;
12828 +       dentry = file->f_path.dentry;
12829 +       inode = d_inode(dentry);
12830 +       sigen = au_sigen(dentry->d_sb);
12831 +       fi_write_lock(file);
12832 +       figen = au_figen(file);
12833 +       di_write_lock_child(dentry);
12834 +       bstart = au_dbstart(dentry);
12835 +       pseudo_link = (bstart != au_ibstart(inode));
12836 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
12837 +               if (!wlock) {
12838 +                       di_downgrade_lock(dentry, AuLock_IR);
12839 +                       fi_downgrade_lock(file);
12840 +               }
12841 +               goto out; /* success */
12842 +       }
12843 +
12844 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
12845 +       if (au_digen_test(dentry, sigen)) {
12846 +               err = au_reval_dpath(dentry, sigen);
12847 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
12848 +       }
12849 +
12850 +       if (!err)
12851 +               err = refresh_file(file, reopen);
12852 +       if (!err) {
12853 +               if (!wlock) {
12854 +                       di_downgrade_lock(dentry, AuLock_IR);
12855 +                       fi_downgrade_lock(file);
12856 +               }
12857 +       } else {
12858 +               di_write_unlock(dentry);
12859 +               fi_write_unlock(file);
12860 +       }
12861 +
12862 +out:
12863 +       return err;
12864 +}
12865 +
12866 +/* ---------------------------------------------------------------------- */
12867 +
12868 +/* cf. aufs_nopage() */
12869 +/* for madvise(2) */
12870 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
12871 +{
12872 +       unlock_page(page);
12873 +       return 0;
12874 +}
12875 +
12876 +/* it will never be called, but necessary to support O_DIRECT */
12877 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
12878 +                             loff_t offset)
12879 +{ BUG(); return 0; }
12880 +
12881 +/* they will never be called. */
12882 +#ifdef CONFIG_AUFS_DEBUG
12883 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
12884 +                           loff_t pos, unsigned len, unsigned flags,
12885 +                           struct page **pagep, void **fsdata)
12886 +{ AuUnsupport(); return 0; }
12887 +static int aufs_write_end(struct file *file, struct address_space *mapping,
12888 +                         loff_t pos, unsigned len, unsigned copied,
12889 +                         struct page *page, void *fsdata)
12890 +{ AuUnsupport(); return 0; }
12891 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12892 +{ AuUnsupport(); return 0; }
12893 +
12894 +static int aufs_set_page_dirty(struct page *page)
12895 +{ AuUnsupport(); return 0; }
12896 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
12897 +                               unsigned int length)
12898 +{ AuUnsupport(); }
12899 +static int aufs_releasepage(struct page *page, gfp_t gfp)
12900 +{ AuUnsupport(); return 0; }
12901 +#if 0 /* called by memory compaction regardless file */
12902 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
12903 +                           struct page *page, enum migrate_mode mode)
12904 +{ AuUnsupport(); return 0; }
12905 +#endif
12906 +static int aufs_launder_page(struct page *page)
12907 +{ AuUnsupport(); return 0; }
12908 +static int aufs_is_partially_uptodate(struct page *page,
12909 +                                     unsigned long from,
12910 +                                     unsigned long count)
12911 +{ AuUnsupport(); return 0; }
12912 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
12913 +                                   bool *writeback)
12914 +{ AuUnsupport(); }
12915 +static int aufs_error_remove_page(struct address_space *mapping,
12916 +                                 struct page *page)
12917 +{ AuUnsupport(); return 0; }
12918 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
12919 +                             sector_t *span)
12920 +{ AuUnsupport(); return 0; }
12921 +static void aufs_swap_deactivate(struct file *file)
12922 +{ AuUnsupport(); }
12923 +#endif /* CONFIG_AUFS_DEBUG */
12924 +
12925 +const struct address_space_operations aufs_aop = {
12926 +       .readpage               = aufs_readpage,
12927 +       .direct_IO              = aufs_direct_IO,
12928 +#ifdef CONFIG_AUFS_DEBUG
12929 +       .writepage              = aufs_writepage,
12930 +       /* no writepages, because of writepage */
12931 +       .set_page_dirty         = aufs_set_page_dirty,
12932 +       /* no readpages, because of readpage */
12933 +       .write_begin            = aufs_write_begin,
12934 +       .write_end              = aufs_write_end,
12935 +       /* no bmap, no block device */
12936 +       .invalidatepage         = aufs_invalidatepage,
12937 +       .releasepage            = aufs_releasepage,
12938 +       /* is fallback_migrate_page ok? */
12939 +       /* .migratepage         = aufs_migratepage, */
12940 +       .launder_page           = aufs_launder_page,
12941 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
12942 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
12943 +       .error_remove_page      = aufs_error_remove_page,
12944 +       .swap_activate          = aufs_swap_activate,
12945 +       .swap_deactivate        = aufs_swap_deactivate
12946 +#endif /* CONFIG_AUFS_DEBUG */
12947 +};
12948 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
12949 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
12950 +++ linux/fs/aufs/file.h        2016-04-24 18:32:51.393686895 +0200
12951 @@ -0,0 +1,291 @@
12952 +/*
12953 + * Copyright (C) 2005-2016 Junjiro R. Okajima
12954 + *
12955 + * This program, aufs is free software; you can redistribute it and/or modify
12956 + * it under the terms of the GNU General Public License as published by
12957 + * the Free Software Foundation; either version 2 of the License, or
12958 + * (at your option) any later version.
12959 + *
12960 + * This program is distributed in the hope that it will be useful,
12961 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12962 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12963 + * GNU General Public License for more details.
12964 + *
12965 + * You should have received a copy of the GNU General Public License
12966 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12967 + */
12968 +
12969 +/*
12970 + * file operations
12971 + */
12972 +
12973 +#ifndef __AUFS_FILE_H__
12974 +#define __AUFS_FILE_H__
12975 +
12976 +#ifdef __KERNEL__
12977 +
12978 +#include <linux/file.h>
12979 +#include <linux/fs.h>
12980 +#include <linux/poll.h>
12981 +#include "rwsem.h"
12982 +
12983 +struct au_branch;
12984 +struct au_hfile {
12985 +       struct file             *hf_file;
12986 +       struct au_branch        *hf_br;
12987 +};
12988 +
12989 +struct au_vdir;
12990 +struct au_fidir {
12991 +       aufs_bindex_t           fd_bbot;
12992 +       aufs_bindex_t           fd_nent;
12993 +       struct au_vdir          *fd_vdir_cache;
12994 +       struct au_hfile         fd_hfile[];
12995 +};
12996 +
12997 +static inline int au_fidir_sz(int nent)
12998 +{
12999 +       AuDebugOn(nent < 0);
13000 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
13001 +}
13002 +
13003 +struct au_finfo {
13004 +       atomic_t                fi_generation;
13005 +
13006 +       struct au_rwsem         fi_rwsem;
13007 +       aufs_bindex_t           fi_btop;
13008 +
13009 +       /* do not union them */
13010 +       struct {                                /* for non-dir */
13011 +               struct au_hfile                 fi_htop;
13012 +               atomic_t                        fi_mmapped;
13013 +       };
13014 +       struct au_fidir         *fi_hdir;       /* for dir only */
13015 +
13016 +       struct hlist_node       fi_hlist;
13017 +       struct file             *fi_file;       /* very ugly */
13018 +} ____cacheline_aligned_in_smp;
13019 +
13020 +/* ---------------------------------------------------------------------- */
13021 +
13022 +/* file.c */
13023 +extern const struct address_space_operations aufs_aop;
13024 +unsigned int au_file_roflags(unsigned int flags);
13025 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
13026 +                      struct file *file, int force_wr);
13027 +struct au_do_open_args {
13028 +       int             no_lock;
13029 +       int             (*open)(struct file *file, int flags,
13030 +                               struct file *h_file);
13031 +       struct au_fidir *fidir;
13032 +       struct file     *h_file;
13033 +};
13034 +int au_do_open(struct file *file, struct au_do_open_args *args);
13035 +int au_reopen_nondir(struct file *file);
13036 +struct au_pin;
13037 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
13038 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13039 +                         int wlock);
13040 +int au_do_flush(struct file *file, fl_owner_t id,
13041 +               int (*flush)(struct file *file, fl_owner_t id));
13042 +
13043 +/* poll.c */
13044 +#ifdef CONFIG_AUFS_POLL
13045 +unsigned int aufs_poll(struct file *file, poll_table *wait);
13046 +#endif
13047 +
13048 +#ifdef CONFIG_AUFS_BR_HFSPLUS
13049 +/* hfsplus.c */
13050 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13051 +                          int force_wr);
13052 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13053 +                   struct file *h_file);
13054 +#else
13055 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
13056 +       aufs_bindex_t bindex, int force_wr)
13057 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
13058 +          struct file *h_file);
13059 +#endif
13060 +
13061 +/* f_op.c */
13062 +extern const struct file_operations aufs_file_fop;
13063 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
13064 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
13065 +struct file *au_read_pre(struct file *file, int keep_fi);
13066 +
13067 +/* finfo.c */
13068 +void au_hfput(struct au_hfile *hf, struct file *file);
13069 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
13070 +                  struct file *h_file);
13071 +
13072 +void au_update_figen(struct file *file);
13073 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
13074 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
13075 +
13076 +void au_fi_init_once(void *_fi);
13077 +void au_finfo_fin(struct file *file);
13078 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
13079 +
13080 +/* ioctl.c */
13081 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
13082 +#ifdef CONFIG_COMPAT
13083 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
13084 +                          unsigned long arg);
13085 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
13086 +                             unsigned long arg);
13087 +#endif
13088 +
13089 +/* ---------------------------------------------------------------------- */
13090 +
13091 +static inline struct au_finfo *au_fi(struct file *file)
13092 +{
13093 +       return file->private_data;
13094 +}
13095 +
13096 +/* ---------------------------------------------------------------------- */
13097 +
13098 +/*
13099 + * fi_read_lock, fi_write_lock,
13100 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
13101 + */
13102 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
13103 +
13104 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
13105 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
13106 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
13107 +
13108 +/* ---------------------------------------------------------------------- */
13109 +
13110 +/* todo: hard/soft set? */
13111 +static inline aufs_bindex_t au_fbstart(struct file *file)
13112 +{
13113 +       FiMustAnyLock(file);
13114 +       return au_fi(file)->fi_btop;
13115 +}
13116 +
13117 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
13118 +{
13119 +       FiMustAnyLock(file);
13120 +       AuDebugOn(!au_fi(file)->fi_hdir);
13121 +       return au_fi(file)->fi_hdir->fd_bbot;
13122 +}
13123 +
13124 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
13125 +{
13126 +       FiMustAnyLock(file);
13127 +       AuDebugOn(!au_fi(file)->fi_hdir);
13128 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
13129 +}
13130 +
13131 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
13132 +{
13133 +       FiMustWriteLock(file);
13134 +       au_fi(file)->fi_btop = bindex;
13135 +}
13136 +
13137 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
13138 +{
13139 +       FiMustWriteLock(file);
13140 +       AuDebugOn(!au_fi(file)->fi_hdir);
13141 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
13142 +}
13143 +
13144 +static inline void au_set_fvdir_cache(struct file *file,
13145 +                                     struct au_vdir *vdir_cache)
13146 +{
13147 +       FiMustWriteLock(file);
13148 +       AuDebugOn(!au_fi(file)->fi_hdir);
13149 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13150 +}
13151 +
13152 +static inline struct file *au_hf_top(struct file *file)
13153 +{
13154 +       FiMustAnyLock(file);
13155 +       AuDebugOn(au_fi(file)->fi_hdir);
13156 +       return au_fi(file)->fi_htop.hf_file;
13157 +}
13158 +
13159 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13160 +{
13161 +       FiMustAnyLock(file);
13162 +       AuDebugOn(!au_fi(file)->fi_hdir);
13163 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
13164 +}
13165 +
13166 +/* todo: memory barrier? */
13167 +static inline unsigned int au_figen(struct file *f)
13168 +{
13169 +       return atomic_read(&au_fi(f)->fi_generation);
13170 +}
13171 +
13172 +static inline void au_set_mmapped(struct file *f)
13173 +{
13174 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13175 +               return;
13176 +       pr_warn("fi_mmapped wrapped around\n");
13177 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13178 +               ;
13179 +}
13180 +
13181 +static inline void au_unset_mmapped(struct file *f)
13182 +{
13183 +       atomic_dec(&au_fi(f)->fi_mmapped);
13184 +}
13185 +
13186 +static inline int au_test_mmapped(struct file *f)
13187 +{
13188 +       return atomic_read(&au_fi(f)->fi_mmapped);
13189 +}
13190 +
13191 +/* customize vma->vm_file */
13192 +
13193 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13194 +                                      struct file *file)
13195 +{
13196 +       struct file *f;
13197 +
13198 +       f = vma->vm_file;
13199 +       get_file(file);
13200 +       vma->vm_file = file;
13201 +       fput(f);
13202 +}
13203 +
13204 +#ifdef CONFIG_MMU
13205 +#define AuDbgVmRegion(file, vma) do {} while (0)
13206 +
13207 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13208 +                                   struct file *file)
13209 +{
13210 +       au_do_vm_file_reset(vma, file);
13211 +}
13212 +#else
13213 +#define AuDbgVmRegion(file, vma) \
13214 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13215 +
13216 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13217 +                                   struct file *file)
13218 +{
13219 +       struct file *f;
13220 +
13221 +       au_do_vm_file_reset(vma, file);
13222 +       f = vma->vm_region->vm_file;
13223 +       get_file(file);
13224 +       vma->vm_region->vm_file = file;
13225 +       fput(f);
13226 +}
13227 +#endif /* CONFIG_MMU */
13228 +
13229 +/* handle vma->vm_prfile */
13230 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13231 +                                   struct file *file)
13232 +{
13233 +       get_file(file);
13234 +       vma->vm_prfile = file;
13235 +#ifndef CONFIG_MMU
13236 +       get_file(file);
13237 +       vma->vm_region->vm_prfile = file;
13238 +#endif
13239 +}
13240 +
13241 +#endif /* __KERNEL__ */
13242 +#endif /* __AUFS_FILE_H__ */
13243 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13244 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13245 +++ linux/fs/aufs/finfo.c       2016-04-24 18:32:51.393686895 +0200
13246 @@ -0,0 +1,156 @@
13247 +/*
13248 + * Copyright (C) 2005-2016 Junjiro R. Okajima
13249 + *
13250 + * This program, aufs is free software; you can redistribute it and/or modify
13251 + * it under the terms of the GNU General Public License as published by
13252 + * the Free Software Foundation; either version 2 of the License, or
13253 + * (at your option) any later version.
13254 + *
13255 + * This program is distributed in the hope that it will be useful,
13256 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13257 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13258 + * GNU General Public License for more details.
13259 + *
13260 + * You should have received a copy of the GNU General Public License
13261 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13262 + */
13263 +
13264 +/*
13265 + * file private data
13266 + */
13267 +
13268 +#include "aufs.h"
13269 +
13270 +void au_hfput(struct au_hfile *hf, struct file *file)
13271 +{
13272 +       /* todo: direct access f_flags */
13273 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
13274 +               allow_write_access(hf->hf_file);
13275 +       fput(hf->hf_file);
13276 +       hf->hf_file = NULL;
13277 +       atomic_dec(&hf->hf_br->br_count);
13278 +       hf->hf_br = NULL;
13279 +}
13280 +
13281 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13282 +{
13283 +       struct au_finfo *finfo = au_fi(file);
13284 +       struct au_hfile *hf;
13285 +       struct au_fidir *fidir;
13286 +
13287 +       fidir = finfo->fi_hdir;
13288 +       if (!fidir) {
13289 +               AuDebugOn(finfo->fi_btop != bindex);
13290 +               hf = &finfo->fi_htop;
13291 +       } else
13292 +               hf = fidir->fd_hfile + bindex;
13293 +
13294 +       if (hf && hf->hf_file)
13295 +               au_hfput(hf, file);
13296 +       if (val) {
13297 +               FiMustWriteLock(file);
13298 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13299 +               hf->hf_file = val;
13300 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13301 +       }
13302 +}
13303 +
13304 +void au_update_figen(struct file *file)
13305 +{
13306 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13307 +       /* smp_mb(); */ /* atomic_set */
13308 +}
13309 +
13310 +/* ---------------------------------------------------------------------- */
13311 +
13312 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13313 +{
13314 +       struct au_fidir *fidir;
13315 +       int nbr;
13316 +
13317 +       nbr = au_sbend(sb) + 1;
13318 +       if (nbr < 2)
13319 +               nbr = 2; /* initial allocate for 2 branches */
13320 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13321 +       if (fidir) {
13322 +               fidir->fd_bbot = -1;
13323 +               fidir->fd_nent = nbr;
13324 +       }
13325 +
13326 +       return fidir;
13327 +}
13328 +
13329 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13330 +{
13331 +       int err;
13332 +       struct au_fidir *fidir, *p;
13333 +
13334 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13335 +       fidir = finfo->fi_hdir;
13336 +       AuDebugOn(!fidir);
13337 +
13338 +       err = -ENOMEM;
13339 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13340 +                        GFP_NOFS);
13341 +       if (p) {
13342 +               p->fd_nent = nbr;
13343 +               finfo->fi_hdir = p;
13344 +               err = 0;
13345 +       }
13346 +
13347 +       return err;
13348 +}
13349 +
13350 +/* ---------------------------------------------------------------------- */
13351 +
13352 +void au_finfo_fin(struct file *file)
13353 +{
13354 +       struct au_finfo *finfo;
13355 +
13356 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13357 +
13358 +       finfo = au_fi(file);
13359 +       AuDebugOn(finfo->fi_hdir);
13360 +       AuRwDestroy(&finfo->fi_rwsem);
13361 +       au_cache_free_finfo(finfo);
13362 +}
13363 +
13364 +void au_fi_init_once(void *_finfo)
13365 +{
13366 +       struct au_finfo *finfo = _finfo;
13367 +       static struct lock_class_key aufs_fi;
13368 +
13369 +       au_rw_init(&finfo->fi_rwsem);
13370 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
13371 +}
13372 +
13373 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13374 +{
13375 +       int err;
13376 +       struct au_finfo *finfo;
13377 +       struct dentry *dentry;
13378 +
13379 +       err = -ENOMEM;
13380 +       dentry = file->f_path.dentry;
13381 +       finfo = au_cache_alloc_finfo();
13382 +       if (unlikely(!finfo))
13383 +               goto out;
13384 +
13385 +       err = 0;
13386 +       au_nfiles_inc(dentry->d_sb);
13387 +       /* verbose coding for lock class name */
13388 +       if (!fidir)
13389 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
13390 +       else
13391 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
13392 +       au_rw_write_lock(&finfo->fi_rwsem);
13393 +       finfo->fi_btop = -1;
13394 +       finfo->fi_hdir = fidir;
13395 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13396 +       /* smp_mb(); */ /* atomic_set */
13397 +
13398 +       file->private_data = finfo;
13399 +
13400 +out:
13401 +       return err;
13402 +}
13403 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13404 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13405 +++ linux/fs/aufs/f_op.c        2016-04-24 18:32:51.393686895 +0200
13406 @@ -0,0 +1,770 @@
13407 +/*
13408 + * Copyright (C) 2005-2016 Junjiro R. Okajima
13409 + *
13410 + * This program, aufs is free software; you can redistribute it and/or modify
13411 + * it under the terms of the GNU General Public License as published by
13412 + * the Free Software Foundation; either version 2 of the License, or
13413 + * (at your option) any later version.
13414 + *
13415 + * This program is distributed in the hope that it will be useful,
13416 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13417 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13418 + * GNU General Public License for more details.
13419 + *
13420 + * You should have received a copy of the GNU General Public License
13421 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13422 + */
13423 +
13424 +/*
13425 + * file and vm operations
13426 + */
13427 +
13428 +#include <linux/aio.h>
13429 +#include <linux/fs_stack.h>
13430 +#include <linux/mman.h>
13431 +#include <linux/security.h>
13432 +#include "aufs.h"
13433 +
13434 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13435 +{
13436 +       int err;
13437 +       aufs_bindex_t bindex;
13438 +       struct dentry *dentry, *h_dentry;
13439 +       struct au_finfo *finfo;
13440 +       struct inode *h_inode;
13441 +
13442 +       FiMustWriteLock(file);
13443 +
13444 +       err = 0;
13445 +       dentry = file->f_path.dentry;
13446 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13447 +       finfo = au_fi(file);
13448 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13449 +       atomic_set(&finfo->fi_mmapped, 0);
13450 +       bindex = au_dbstart(dentry);
13451 +       if (!h_file) {
13452 +               h_dentry = au_h_dptr(dentry, bindex);
13453 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13454 +               if (unlikely(err))
13455 +                       goto out;
13456 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13457 +       } else {
13458 +               h_dentry = h_file->f_path.dentry;
13459 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13460 +               if (unlikely(err))
13461 +                       goto out;
13462 +               get_file(h_file);
13463 +       }
13464 +       if (IS_ERR(h_file))
13465 +               err = PTR_ERR(h_file);
13466 +       else {
13467 +               if ((flags & __O_TMPFILE)
13468 +                   && !(flags & O_EXCL)) {
13469 +                       h_inode = file_inode(h_file);
13470 +                       spin_lock(&h_inode->i_lock);
13471 +                       h_inode->i_state |= I_LINKABLE;
13472 +                       spin_unlock(&h_inode->i_lock);
13473 +               }
13474 +               au_set_fbstart(file, bindex);
13475 +               au_set_h_fptr(file, bindex, h_file);
13476 +               au_update_figen(file);
13477 +               /* todo: necessary? */
13478 +               /* file->f_ra = h_file->f_ra; */
13479 +       }
13480 +
13481 +out:
13482 +       return err;
13483 +}
13484 +
13485 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13486 +                           struct file *file)
13487 +{
13488 +       int err;
13489 +       struct super_block *sb;
13490 +       struct au_do_open_args args = {
13491 +               .open   = au_do_open_nondir
13492 +       };
13493 +
13494 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13495 +             file, vfsub_file_flags(file), file->f_mode);
13496 +
13497 +       sb = file->f_path.dentry->d_sb;
13498 +       si_read_lock(sb, AuLock_FLUSH);
13499 +       err = au_do_open(file, &args);
13500 +       si_read_unlock(sb);
13501 +       return err;
13502 +}
13503 +
13504 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13505 +{
13506 +       struct au_finfo *finfo;
13507 +       aufs_bindex_t bindex;
13508 +
13509 +       finfo = au_fi(file);
13510 +       au_sphl_del(&finfo->fi_hlist,
13511 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13512 +       bindex = finfo->fi_btop;
13513 +       if (bindex >= 0)
13514 +               au_set_h_fptr(file, bindex, NULL);
13515 +
13516 +       au_finfo_fin(file);
13517 +       return 0;
13518 +}
13519 +
13520 +/* ---------------------------------------------------------------------- */
13521 +
13522 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13523 +{
13524 +       int err;
13525 +       struct file *h_file;
13526 +
13527 +       err = 0;
13528 +       h_file = au_hf_top(file);
13529 +       if (h_file)
13530 +               err = vfsub_flush(h_file, id);
13531 +       return err;
13532 +}
13533 +
13534 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13535 +{
13536 +       return au_do_flush(file, id, au_do_flush_nondir);
13537 +}
13538 +
13539 +/* ---------------------------------------------------------------------- */
13540 +/*
13541 + * read and write functions acquire [fdi]_rwsem once, but release before
13542 + * mmap_sem. This is because to stop a race condition between mmap(2).
13543 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13544 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13545 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13546 + */
13547 +
13548 +/* Callers should call au_read_post() or fput() in the end */
13549 +struct file *au_read_pre(struct file *file, int keep_fi)
13550 +{
13551 +       struct file *h_file;
13552 +       int err;
13553 +
13554 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13555 +       if (!err) {
13556 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13557 +               h_file = au_hf_top(file);
13558 +               get_file(h_file);
13559 +               if (!keep_fi)
13560 +                       fi_read_unlock(file);
13561 +       } else
13562 +               h_file = ERR_PTR(err);
13563 +
13564 +       return h_file;
13565 +}
13566 +
13567 +static void au_read_post(struct inode *inode, struct file *h_file)
13568 +{
13569 +       /* update without lock, I don't think it a problem */
13570 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13571 +       fput(h_file);
13572 +}
13573 +
13574 +struct au_write_pre {
13575 +       blkcnt_t blks;
13576 +       aufs_bindex_t bstart;
13577 +};
13578 +
13579 +/*
13580 + * return with iinfo is write-locked
13581 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13582 + * end
13583 + */
13584 +static struct file *au_write_pre(struct file *file, int do_ready,
13585 +                                struct au_write_pre *wpre)
13586 +{
13587 +       struct file *h_file;
13588 +       struct dentry *dentry;
13589 +       int err;
13590 +       struct au_pin pin;
13591 +
13592 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13593 +       h_file = ERR_PTR(err);
13594 +       if (unlikely(err))
13595 +               goto out;
13596 +
13597 +       dentry = file->f_path.dentry;
13598 +       if (do_ready) {
13599 +               err = au_ready_to_write(file, -1, &pin);
13600 +               if (unlikely(err)) {
13601 +                       h_file = ERR_PTR(err);
13602 +                       di_write_unlock(dentry);
13603 +                       goto out_fi;
13604 +               }
13605 +       }
13606 +
13607 +       di_downgrade_lock(dentry, /*flags*/0);
13608 +       if (wpre)
13609 +               wpre->bstart = au_fbstart(file);
13610 +       h_file = au_hf_top(file);
13611 +       get_file(h_file);
13612 +       if (wpre)
13613 +               wpre->blks = file_inode(h_file)->i_blocks;
13614 +       if (do_ready)
13615 +               au_unpin(&pin);
13616 +       di_read_unlock(dentry, /*flags*/0);
13617 +
13618 +out_fi:
13619 +       fi_write_unlock(file);
13620 +out:
13621 +       return h_file;
13622 +}
13623 +
13624 +static void au_write_post(struct inode *inode, struct file *h_file,
13625 +                         struct au_write_pre *wpre, ssize_t written)
13626 +{
13627 +       struct inode *h_inode;
13628 +
13629 +       au_cpup_attr_timesizes(inode);
13630 +       AuDebugOn(au_ibstart(inode) != wpre->bstart);
13631 +       h_inode = file_inode(h_file);
13632 +       inode->i_mode = h_inode->i_mode;
13633 +       ii_write_unlock(inode);
13634 +       fput(h_file);
13635 +
13636 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13637 +       if (written > 0)
13638 +               au_fhsm_wrote(inode->i_sb, wpre->bstart,
13639 +                             /*force*/h_inode->i_blocks > wpre->blks);
13640 +}
13641 +
13642 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13643 +                        loff_t *ppos)
13644 +{
13645 +       ssize_t err;
13646 +       struct inode *inode;
13647 +       struct file *h_file;
13648 +       struct super_block *sb;
13649 +
13650 +       inode = file_inode(file);
13651 +       sb = inode->i_sb;
13652 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13653 +
13654 +       h_file = au_read_pre(file, /*keep_fi*/0);
13655 +       err = PTR_ERR(h_file);
13656 +       if (IS_ERR(h_file))
13657 +               goto out;
13658 +
13659 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13660 +       err = vfsub_read_u(h_file, buf, count, ppos);
13661 +       /* todo: necessary? */
13662 +       /* file->f_ra = h_file->f_ra; */
13663 +       au_read_post(inode, h_file);
13664 +
13665 +out:
13666 +       si_read_unlock(sb);
13667 +       return err;
13668 +}
13669 +
13670 +/*
13671 + * todo: very ugly
13672 + * it locks both of i_mutex and si_rwsem for read in safe.
13673 + * if the plink maintenance mode continues forever (that is the problem),
13674 + * may loop forever.
13675 + */
13676 +static void au_mtx_and_read_lock(struct inode *inode)
13677 +{
13678 +       int err;
13679 +       struct super_block *sb = inode->i_sb;
13680 +
13681 +       while (1) {
13682 +               inode_lock(inode);
13683 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13684 +               if (!err)
13685 +                       break;
13686 +               inode_unlock(inode);
13687 +               si_read_lock(sb, AuLock_NOPLMW);
13688 +               si_read_unlock(sb);
13689 +       }
13690 +}
13691 +
13692 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13693 +                         size_t count, loff_t *ppos)
13694 +{
13695 +       ssize_t err;
13696 +       struct au_write_pre wpre;
13697 +       struct inode *inode;
13698 +       struct file *h_file;
13699 +       char __user *buf = (char __user *)ubuf;
13700 +
13701 +       inode = file_inode(file);
13702 +       au_mtx_and_read_lock(inode);
13703 +
13704 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13705 +       err = PTR_ERR(h_file);
13706 +       if (IS_ERR(h_file))
13707 +               goto out;
13708 +
13709 +       err = vfsub_write_u(h_file, buf, count, ppos);
13710 +       au_write_post(inode, h_file, &wpre, err);
13711 +
13712 +out:
13713 +       si_read_unlock(inode->i_sb);
13714 +       inode_unlock(inode);
13715 +       return err;
13716 +}
13717 +
13718 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13719 +                         struct iov_iter *iov_iter)
13720 +{
13721 +       ssize_t err;
13722 +       struct file *file;
13723 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13724 +
13725 +       err = security_file_permission(h_file, rw);
13726 +       if (unlikely(err))
13727 +               goto out;
13728 +
13729 +       err = -ENOSYS;
13730 +       iter = NULL;
13731 +       if (rw == MAY_READ)
13732 +               iter = h_file->f_op->read_iter;
13733 +       else if (rw == MAY_WRITE)
13734 +               iter = h_file->f_op->write_iter;
13735 +
13736 +       file = kio->ki_filp;
13737 +       kio->ki_filp = h_file;
13738 +       if (iter) {
13739 +               lockdep_off();
13740 +               err = iter(kio, iov_iter);
13741 +               lockdep_on();
13742 +       } else
13743 +               /* currently there is no such fs */
13744 +               WARN_ON_ONCE(1);
13745 +       kio->ki_filp = file;
13746 +
13747 +out:
13748 +       return err;
13749 +}
13750 +
13751 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13752 +{
13753 +       ssize_t err;
13754 +       struct file *file, *h_file;
13755 +       struct inode *inode;
13756 +       struct super_block *sb;
13757 +
13758 +       file = kio->ki_filp;
13759 +       inode = file_inode(file);
13760 +       sb = inode->i_sb;
13761 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13762 +
13763 +       h_file = au_read_pre(file, /*keep_fi*/0);
13764 +       err = PTR_ERR(h_file);
13765 +       if (IS_ERR(h_file))
13766 +               goto out;
13767 +
13768 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13769 +       /* todo: necessary? */
13770 +       /* file->f_ra = h_file->f_ra; */
13771 +       au_read_post(inode, h_file);
13772 +
13773 +out:
13774 +       si_read_unlock(sb);
13775 +       return err;
13776 +}
13777 +
13778 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13779 +{
13780 +       ssize_t err;
13781 +       struct au_write_pre wpre;
13782 +       struct inode *inode;
13783 +       struct file *file, *h_file;
13784 +
13785 +       file = kio->ki_filp;
13786 +       inode = file_inode(file);
13787 +       au_mtx_and_read_lock(inode);
13788 +
13789 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13790 +       err = PTR_ERR(h_file);
13791 +       if (IS_ERR(h_file))
13792 +               goto out;
13793 +
13794 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13795 +       au_write_post(inode, h_file, &wpre, err);
13796 +
13797 +out:
13798 +       si_read_unlock(inode->i_sb);
13799 +       inode_unlock(inode);
13800 +       return err;
13801 +}
13802 +
13803 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13804 +                               struct pipe_inode_info *pipe, size_t len,
13805 +                               unsigned int flags)
13806 +{
13807 +       ssize_t err;
13808 +       struct file *h_file;
13809 +       struct inode *inode;
13810 +       struct super_block *sb;
13811 +
13812 +       inode = file_inode(file);
13813 +       sb = inode->i_sb;
13814 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13815 +
13816 +       h_file = au_read_pre(file, /*keep_fi*/1);
13817 +       err = PTR_ERR(h_file);
13818 +       if (IS_ERR(h_file))
13819 +               goto out;
13820 +
13821 +       if (au_test_loopback_kthread()) {
13822 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13823 +               if (file->f_mapping != h_file->f_mapping) {
13824 +                       file->f_mapping = h_file->f_mapping;
13825 +                       smp_mb(); /* unnecessary? */
13826 +               }
13827 +       }
13828 +       fi_read_unlock(file);
13829 +
13830 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13831 +       /* todo: necessasry? */
13832 +       /* file->f_ra = h_file->f_ra; */
13833 +       au_read_post(inode, h_file);
13834 +
13835 +out:
13836 +       si_read_unlock(sb);
13837 +       return err;
13838 +}
13839 +
13840 +static ssize_t
13841 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13842 +                 size_t len, unsigned int flags)
13843 +{
13844 +       ssize_t err;
13845 +       struct au_write_pre wpre;
13846 +       struct inode *inode;
13847 +       struct file *h_file;
13848 +
13849 +       inode = file_inode(file);
13850 +       au_mtx_and_read_lock(inode);
13851 +
13852 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13853 +       err = PTR_ERR(h_file);
13854 +       if (IS_ERR(h_file))
13855 +               goto out;
13856 +
13857 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13858 +       au_write_post(inode, h_file, &wpre, err);
13859 +
13860 +out:
13861 +       si_read_unlock(inode->i_sb);
13862 +       inode_unlock(inode);
13863 +       return err;
13864 +}
13865 +
13866 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13867 +                          loff_t len)
13868 +{
13869 +       long err;
13870 +       struct au_write_pre wpre;
13871 +       struct inode *inode;
13872 +       struct file *h_file;
13873 +
13874 +       inode = file_inode(file);
13875 +       au_mtx_and_read_lock(inode);
13876 +
13877 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13878 +       err = PTR_ERR(h_file);
13879 +       if (IS_ERR(h_file))
13880 +               goto out;
13881 +
13882 +       lockdep_off();
13883 +       err = vfs_fallocate(h_file, mode, offset, len);
13884 +       lockdep_on();
13885 +       au_write_post(inode, h_file, &wpre, /*written*/1);
13886 +
13887 +out:
13888 +       si_read_unlock(inode->i_sb);
13889 +       inode_unlock(inode);
13890 +       return err;
13891 +}
13892 +
13893 +/* ---------------------------------------------------------------------- */
13894 +
13895 +/*
13896 + * The locking order around current->mmap_sem.
13897 + * - in most and regular cases
13898 + *   file I/O syscall -- aufs_read() or something
13899 + *     -- si_rwsem for read -- mmap_sem
13900 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
13901 + * - in mmap case
13902 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13903 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
13904 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
13905 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13906 + * It means that when aufs acquires si_rwsem for write, the process should never
13907 + * acquire mmap_sem.
13908 + *
13909 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13910 + * problem either since any directory is not able to be mmap-ed.
13911 + * The similar scenario is applied to aufs_readlink() too.
13912 + */
13913 +
13914 +#if 0 /* stop calling security_file_mmap() */
13915 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13916 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
13917 +
13918 +static unsigned long au_arch_prot_conv(unsigned long flags)
13919 +{
13920 +       /* currently ppc64 only */
13921 +#ifdef CONFIG_PPC64
13922 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
13923 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13924 +       return AuConv_VM_PROT(flags, SAO);
13925 +#else
13926 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
13927 +       return 0;
13928 +#endif
13929 +}
13930 +
13931 +static unsigned long au_prot_conv(unsigned long flags)
13932 +{
13933 +       return AuConv_VM_PROT(flags, READ)
13934 +               | AuConv_VM_PROT(flags, WRITE)
13935 +               | AuConv_VM_PROT(flags, EXEC)
13936 +               | au_arch_prot_conv(flags);
13937 +}
13938 +
13939 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13940 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
13941 +
13942 +static unsigned long au_flag_conv(unsigned long flags)
13943 +{
13944 +       return AuConv_VM_MAP(flags, GROWSDOWN)
13945 +               | AuConv_VM_MAP(flags, DENYWRITE)
13946 +               | AuConv_VM_MAP(flags, LOCKED);
13947 +}
13948 +#endif
13949 +
13950 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13951 +{
13952 +       int err;
13953 +       const unsigned char wlock
13954 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13955 +       struct super_block *sb;
13956 +       struct file *h_file;
13957 +       struct inode *inode;
13958 +
13959 +       AuDbgVmRegion(file, vma);
13960 +
13961 +       inode = file_inode(file);
13962 +       sb = inode->i_sb;
13963 +       lockdep_off();
13964 +       si_read_lock(sb, AuLock_NOPLMW);
13965 +
13966 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
13967 +       lockdep_on();
13968 +       err = PTR_ERR(h_file);
13969 +       if (IS_ERR(h_file))
13970 +               goto out;
13971 +
13972 +       err = 0;
13973 +       au_set_mmapped(file);
13974 +       au_vm_file_reset(vma, h_file);
13975 +       /*
13976 +        * we cannot call security_mmap_file() here since it may acquire
13977 +        * mmap_sem or i_mutex.
13978 +        *
13979 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13980 +        *                       au_flag_conv(vma->vm_flags));
13981 +        */
13982 +       if (!err)
13983 +               err = h_file->f_op->mmap(h_file, vma);
13984 +       if (!err) {
13985 +               au_vm_prfile_set(vma, file);
13986 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
13987 +               goto out_fput; /* success */
13988 +       }
13989 +       au_unset_mmapped(file);
13990 +       au_vm_file_reset(vma, file);
13991 +
13992 +out_fput:
13993 +       lockdep_off();
13994 +       ii_write_unlock(inode);
13995 +       lockdep_on();
13996 +       fput(h_file);
13997 +out:
13998 +       lockdep_off();
13999 +       si_read_unlock(sb);
14000 +       lockdep_on();
14001 +       AuTraceErr(err);
14002 +       return err;
14003 +}
14004 +
14005 +/* ---------------------------------------------------------------------- */
14006 +
14007 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14008 +                            int datasync)
14009 +{
14010 +       int err;
14011 +       struct au_write_pre wpre;
14012 +       struct inode *inode;
14013 +       struct file *h_file;
14014 +
14015 +       err = 0; /* -EBADF; */ /* posix? */
14016 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
14017 +               goto out;
14018 +
14019 +       inode = file_inode(file);
14020 +       au_mtx_and_read_lock(inode);
14021 +
14022 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14023 +       err = PTR_ERR(h_file);
14024 +       if (IS_ERR(h_file))
14025 +               goto out_unlock;
14026 +
14027 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
14028 +       au_write_post(inode, h_file, &wpre, /*written*/0);
14029 +
14030 +out_unlock:
14031 +       si_read_unlock(inode->i_sb);
14032 +       inode_unlock(inode);
14033 +out:
14034 +       return err;
14035 +}
14036 +
14037 +/* no one supports this operation, currently */
14038 +#if 0
14039 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
14040 +{
14041 +       int err;
14042 +       struct au_write_pre wpre;
14043 +       struct inode *inode, *h_inode;
14044 +       struct file *file, *h_file;
14045 +
14046 +       err = 0; /* -EBADF; */ /* posix? */
14047 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
14048 +               goto out;
14049 +
14050 +       file = kio->ki_filp;
14051 +       inode = file_inode(file);
14052 +       au_mtx_and_read_lock(inode);
14053 +
14054 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14055 +       err = PTR_ERR(h_file);
14056 +       if (IS_ERR(h_file))
14057 +               goto out_unlock;
14058 +
14059 +       err = -ENOSYS;
14060 +       h_file = au_hf_top(file);
14061 +       if (h_file->f_op->aio_fsync) {
14062 +               h_inode = file_inode(h_file);
14063 +               if (!is_sync_kiocb(kio)) {
14064 +                       get_file(h_file);
14065 +                       fput(file);
14066 +               }
14067 +               kio->ki_filp = h_file;
14068 +               err = h_file->f_op->aio_fsync(kio, datasync);
14069 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
14070 +               if (!err)
14071 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
14072 +               /*ignore*/
14073 +               inode_unlock(h_inode);
14074 +       }
14075 +       au_write_post(inode, h_file, &wpre, /*written*/0);
14076 +
14077 +out_unlock:
14078 +       si_read_unlock(inode->sb);
14079 +       inode_unlock(inode);
14080 +out:
14081 +       return err;
14082 +}
14083 +#endif
14084 +
14085 +static int aufs_fasync(int fd, struct file *file, int flag)
14086 +{
14087 +       int err;
14088 +       struct file *h_file;
14089 +       struct super_block *sb;
14090 +
14091 +       sb = file->f_path.dentry->d_sb;
14092 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14093 +
14094 +       h_file = au_read_pre(file, /*keep_fi*/0);
14095 +       err = PTR_ERR(h_file);
14096 +       if (IS_ERR(h_file))
14097 +               goto out;
14098 +
14099 +       if (h_file->f_op->fasync)
14100 +               err = h_file->f_op->fasync(fd, h_file, flag);
14101 +       fput(h_file); /* instead of au_read_post() */
14102 +
14103 +out:
14104 +       si_read_unlock(sb);
14105 +       return err;
14106 +}
14107 +
14108 +static int aufs_setfl(struct file *file, unsigned long arg)
14109 +{
14110 +       int err;
14111 +       struct file *h_file;
14112 +       struct super_block *sb;
14113 +
14114 +       sb = file->f_path.dentry->d_sb;
14115 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14116 +
14117 +       h_file = au_read_pre(file, /*keep_fi*/0);
14118 +       err = PTR_ERR(h_file);
14119 +       if (IS_ERR(h_file))
14120 +               goto out;
14121 +
14122 +       arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
14123 +       err = setfl(/*unused fd*/-1, h_file, arg);
14124 +       fput(h_file); /* instead of au_read_post() */
14125 +
14126 +out:
14127 +       si_read_unlock(sb);
14128 +       return err;
14129 +}
14130 +
14131 +/* ---------------------------------------------------------------------- */
14132 +
14133 +/* no one supports this operation, currently */
14134 +#if 0
14135 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
14136 +                            size_t len, loff_t *pos, int more)
14137 +{
14138 +}
14139 +#endif
14140 +
14141 +/* ---------------------------------------------------------------------- */
14142 +
14143 +const struct file_operations aufs_file_fop = {
14144 +       .owner          = THIS_MODULE,
14145 +
14146 +       .llseek         = default_llseek,
14147 +
14148 +       .read           = aufs_read,
14149 +       .write          = aufs_write,
14150 +       .read_iter      = aufs_read_iter,
14151 +       .write_iter     = aufs_write_iter,
14152 +
14153 +#ifdef CONFIG_AUFS_POLL
14154 +       .poll           = aufs_poll,
14155 +#endif
14156 +       .unlocked_ioctl = aufs_ioctl_nondir,
14157 +#ifdef CONFIG_COMPAT
14158 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
14159 +#endif
14160 +       .mmap           = aufs_mmap,
14161 +       .open           = aufs_open_nondir,
14162 +       .flush          = aufs_flush_nondir,
14163 +       .release        = aufs_release_nondir,
14164 +       .fsync          = aufs_fsync_nondir,
14165 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
14166 +       .fasync         = aufs_fasync,
14167 +       /* .sendpage    = aufs_sendpage, */
14168 +       .setfl          = aufs_setfl,
14169 +       .splice_write   = aufs_splice_write,
14170 +       .splice_read    = aufs_splice_read,
14171 +#if 0
14172 +       .aio_splice_write = aufs_aio_splice_write,
14173 +       .aio_splice_read  = aufs_aio_splice_read,
14174 +#endif
14175 +       .fallocate      = aufs_fallocate
14176 +};
14177 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14178 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
14179 +++ linux/fs/aufs/fstype.h      2016-04-24 18:32:51.393686895 +0200
14180 @@ -0,0 +1,400 @@
14181 +/*
14182 + * Copyright (C) 2005-2016 Junjiro R. Okajima
14183 + *
14184 + * This program, aufs is free software; you can redistribute it and/or modify
14185 + * it under the terms of the GNU General Public License as published by
14186 + * the Free Software Foundation; either version 2 of the License, or
14187 + * (at your option) any later version.
14188 + *
14189 + * This program is distributed in the hope that it will be useful,
14190 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14191 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14192 + * GNU General Public License for more details.
14193 + *
14194 + * You should have received a copy of the GNU General Public License
14195 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14196 + */
14197 +
14198 +/*
14199 + * judging filesystem type
14200 + */
14201 +
14202 +#ifndef __AUFS_FSTYPE_H__
14203 +#define __AUFS_FSTYPE_H__
14204 +
14205 +#ifdef __KERNEL__
14206 +
14207 +#include <linux/fs.h>
14208 +#include <linux/magic.h>
14209 +#include <linux/nfs_fs.h>
14210 +#include <linux/romfs_fs.h>
14211 +
14212 +static inline int au_test_aufs(struct super_block *sb)
14213 +{
14214 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14215 +}
14216 +
14217 +static inline const char *au_sbtype(struct super_block *sb)
14218 +{
14219 +       return sb->s_type->name;
14220 +}
14221 +
14222 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14223 +{
14224 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
14225 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14226 +#else
14227 +       return 0;
14228 +#endif
14229 +}
14230 +
14231 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14232 +{
14233 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
14234 +       return sb->s_magic == ROMFS_MAGIC;
14235 +#else
14236 +       return 0;
14237 +#endif
14238 +}
14239 +
14240 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14241 +{
14242 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
14243 +       return sb->s_magic == CRAMFS_MAGIC;
14244 +#endif
14245 +       return 0;
14246 +}
14247 +
14248 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14249 +{
14250 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
14251 +       return sb->s_magic == NFS_SUPER_MAGIC;
14252 +#else
14253 +       return 0;
14254 +#endif
14255 +}
14256 +
14257 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14258 +{
14259 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
14260 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14261 +#else
14262 +       return 0;
14263 +#endif
14264 +}
14265 +
14266 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14267 +{
14268 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
14269 +       return sb->s_magic == XFS_SB_MAGIC;
14270 +#else
14271 +       return 0;
14272 +#endif
14273 +}
14274 +
14275 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14276 +{
14277 +#ifdef CONFIG_TMPFS
14278 +       return sb->s_magic == TMPFS_MAGIC;
14279 +#else
14280 +       return 0;
14281 +#endif
14282 +}
14283 +
14284 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14285 +{
14286 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
14287 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14288 +#else
14289 +       return 0;
14290 +#endif
14291 +}
14292 +
14293 +static inline int au_test_ramfs(struct super_block *sb)
14294 +{
14295 +       return sb->s_magic == RAMFS_MAGIC;
14296 +}
14297 +
14298 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14299 +{
14300 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
14301 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14302 +#else
14303 +       return 0;
14304 +#endif
14305 +}
14306 +
14307 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14308 +{
14309 +#ifdef CONFIG_PROC_FS
14310 +       return sb->s_magic == PROC_SUPER_MAGIC;
14311 +#else
14312 +       return 0;
14313 +#endif
14314 +}
14315 +
14316 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14317 +{
14318 +#ifdef CONFIG_SYSFS
14319 +       return sb->s_magic == SYSFS_MAGIC;
14320 +#else
14321 +       return 0;
14322 +#endif
14323 +}
14324 +
14325 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14326 +{
14327 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
14328 +       return sb->s_magic == CONFIGFS_MAGIC;
14329 +#else
14330 +       return 0;
14331 +#endif
14332 +}
14333 +
14334 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14335 +{
14336 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
14337 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14338 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14339 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14340 +               || sb->s_magic == MINIX_SUPER_MAGIC
14341 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14342 +#else
14343 +       return 0;
14344 +#endif
14345 +}
14346 +
14347 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14348 +{
14349 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
14350 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14351 +#else
14352 +       return 0;
14353 +#endif
14354 +}
14355 +
14356 +static inline int au_test_msdos(struct super_block *sb)
14357 +{
14358 +       return au_test_fat(sb);
14359 +}
14360 +
14361 +static inline int au_test_vfat(struct super_block *sb)
14362 +{
14363 +       return au_test_fat(sb);
14364 +}
14365 +
14366 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14367 +{
14368 +#ifdef CONFIG_SECURITYFS
14369 +       return sb->s_magic == SECURITYFS_MAGIC;
14370 +#else
14371 +       return 0;
14372 +#endif
14373 +}
14374 +
14375 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14376 +{
14377 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
14378 +       return sb->s_magic == SQUASHFS_MAGIC;
14379 +#else
14380 +       return 0;
14381 +#endif
14382 +}
14383 +
14384 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14385 +{
14386 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
14387 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14388 +#else
14389 +       return 0;
14390 +#endif
14391 +}
14392 +
14393 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14394 +{
14395 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
14396 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14397 +#else
14398 +       return 0;
14399 +#endif
14400 +}
14401 +
14402 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14403 +{
14404 +#ifdef CONFIG_DEBUG_FS
14405 +       return sb->s_magic == DEBUGFS_MAGIC;
14406 +#else
14407 +       return 0;
14408 +#endif
14409 +}
14410 +
14411 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14412 +{
14413 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
14414 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14415 +#else
14416 +       return 0;
14417 +#endif
14418 +}
14419 +
14420 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14421 +{
14422 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
14423 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14424 +#else
14425 +       return 0;
14426 +#endif
14427 +}
14428 +
14429 +/* ---------------------------------------------------------------------- */
14430 +/*
14431 + * they can't be an aufs branch.
14432 + */
14433 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14434 +{
14435 +       return
14436 +#ifndef CONFIG_AUFS_BR_RAMFS
14437 +               au_test_ramfs(sb) ||
14438 +#endif
14439 +               au_test_procfs(sb)
14440 +               || au_test_sysfs(sb)
14441 +               || au_test_configfs(sb)
14442 +               || au_test_debugfs(sb)
14443 +               || au_test_securityfs(sb)
14444 +               || au_test_xenfs(sb)
14445 +               || au_test_ecryptfs(sb)
14446 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14447 +               || au_test_aufs(sb); /* will be supported in next version */
14448 +}
14449 +
14450 +static inline int au_test_fs_remote(struct super_block *sb)
14451 +{
14452 +       return !au_test_tmpfs(sb)
14453 +#ifdef CONFIG_AUFS_BR_RAMFS
14454 +               && !au_test_ramfs(sb)
14455 +#endif
14456 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14457 +}
14458 +
14459 +/* ---------------------------------------------------------------------- */
14460 +
14461 +/*
14462 + * Note: these functions (below) are created after reading ->getattr() in all
14463 + * filesystems under linux/fs. it means we have to do so in every update...
14464 + */
14465 +
14466 +/*
14467 + * some filesystems require getattr to refresh the inode attributes before
14468 + * referencing.
14469 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14470 + * and leave the work for d_revalidate()
14471 + */
14472 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14473 +{
14474 +       return au_test_nfs(sb)
14475 +               || au_test_fuse(sb)
14476 +               /* || au_test_btrfs(sb) */      /* untested */
14477 +               ;
14478 +}
14479 +
14480 +/*
14481 + * filesystems which don't maintain i_size or i_blocks.
14482 + */
14483 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14484 +{
14485 +       return au_test_xfs(sb)
14486 +               || au_test_btrfs(sb)
14487 +               || au_test_ubifs(sb)
14488 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14489 +               /* || au_test_minix(sb) */      /* untested */
14490 +               ;
14491 +}
14492 +
14493 +/*
14494 + * filesystems which don't store the correct value in some of their inode
14495 + * attributes.
14496 + */
14497 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14498 +{
14499 +       return au_test_fs_bad_iattr_size(sb)
14500 +               || au_test_fat(sb)
14501 +               || au_test_msdos(sb)
14502 +               || au_test_vfat(sb);
14503 +}
14504 +
14505 +/* they don't check i_nlink in link(2) */
14506 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14507 +{
14508 +       return au_test_tmpfs(sb)
14509 +#ifdef CONFIG_AUFS_BR_RAMFS
14510 +               || au_test_ramfs(sb)
14511 +#endif
14512 +               || au_test_ubifs(sb)
14513 +               || au_test_hfsplus(sb);
14514 +}
14515 +
14516 +/*
14517 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14518 + */
14519 +static inline int au_test_fs_notime(struct super_block *sb)
14520 +{
14521 +       return au_test_nfs(sb)
14522 +               || au_test_fuse(sb)
14523 +               || au_test_ubifs(sb)
14524 +               ;
14525 +}
14526 +
14527 +/* temporary support for i#1 in cramfs */
14528 +static inline int au_test_fs_unique_ino(struct inode *inode)
14529 +{
14530 +       if (au_test_cramfs(inode->i_sb))
14531 +               return inode->i_ino != 1;
14532 +       return 1;
14533 +}
14534 +
14535 +/* ---------------------------------------------------------------------- */
14536 +
14537 +/*
14538 + * the filesystem where the xino files placed must support i/o after unlink and
14539 + * maintain i_size and i_blocks.
14540 + */
14541 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14542 +{
14543 +       return au_test_fs_remote(sb)
14544 +               || au_test_fs_bad_iattr_size(sb)
14545 +               /* don't want unnecessary work for xino */
14546 +               || au_test_aufs(sb)
14547 +               || au_test_ecryptfs(sb)
14548 +               || au_test_nilfs(sb);
14549 +}
14550 +
14551 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14552 +{
14553 +       return au_test_tmpfs(sb)
14554 +               || au_test_ramfs(sb);
14555 +}
14556 +
14557 +/*
14558 + * test if the @sb is real-readonly.
14559 + */
14560 +static inline int au_test_fs_rr(struct super_block *sb)
14561 +{
14562 +       return au_test_squashfs(sb)
14563 +               || au_test_iso9660(sb)
14564 +               || au_test_cramfs(sb)
14565 +               || au_test_romfs(sb);
14566 +}
14567 +
14568 +/*
14569 + * test if the @inode is nfs with 'noacl' option
14570 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14571 + */
14572 +static inline int au_test_nfs_noacl(struct inode *inode)
14573 +{
14574 +       return au_test_nfs(inode->i_sb)
14575 +               /* && IS_POSIXACL(inode) */
14576 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14577 +}
14578 +
14579 +#endif /* __KERNEL__ */
14580 +#endif /* __AUFS_FSTYPE_H__ */
14581 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14582 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14583 +++ linux/fs/aufs/hfsnotify.c   2016-04-24 18:32:51.393686895 +0200
14584 @@ -0,0 +1,288 @@
14585 +/*
14586 + * Copyright (C) 2005-2016 Junjiro R. Okajima
14587 + *
14588 + * This program, aufs is free software; you can redistribute it and/or modify
14589 + * it under the terms of the GNU General Public License as published by
14590 + * the Free Software Foundation; either version 2 of the License, or
14591 + * (at your option) any later version.
14592 + *
14593 + * This program is distributed in the hope that it will be useful,
14594 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14595 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14596 + * GNU General Public License for more details.
14597 + *
14598 + * You should have received a copy of the GNU General Public License
14599 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14600 + */
14601 +
14602 +/*
14603 + * fsnotify for the lower directories
14604 + */
14605 +
14606 +#include "aufs.h"
14607 +
14608 +/* FS_IN_IGNORED is unnecessary */
14609 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14610 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14611 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14612 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14613 +
14614 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14615 +{
14616 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14617 +                                            hn_mark);
14618 +       AuDbg("here\n");
14619 +       au_cache_free_hnotify(hn);
14620 +       smp_mb__before_atomic();
14621 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14622 +               wake_up(&au_hfsn_wq);
14623 +}
14624 +
14625 +static int au_hfsn_alloc(struct au_hinode *hinode)
14626 +{
14627 +       int err;
14628 +       struct au_hnotify *hn;
14629 +       struct super_block *sb;
14630 +       struct au_branch *br;
14631 +       struct fsnotify_mark *mark;
14632 +       aufs_bindex_t bindex;
14633 +
14634 +       hn = hinode->hi_notify;
14635 +       sb = hn->hn_aufs_inode->i_sb;
14636 +       bindex = au_br_index(sb, hinode->hi_id);
14637 +       br = au_sbr(sb, bindex);
14638 +       AuDebugOn(!br->br_hfsn);
14639 +
14640 +       mark = &hn->hn_mark;
14641 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14642 +       mark->mask = AuHfsnMask;
14643 +       /*
14644 +        * by udba rename or rmdir, aufs assign a new inode to the known
14645 +        * h_inode, so specify 1 to allow dups.
14646 +        */
14647 +       lockdep_off();
14648 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14649 +                                /*mnt*/NULL, /*allow_dups*/1);
14650 +       /* even if err */
14651 +       fsnotify_put_mark(mark);
14652 +       lockdep_on();
14653 +
14654 +       return err;
14655 +}
14656 +
14657 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14658 +{
14659 +       struct fsnotify_mark *mark;
14660 +       unsigned long long ull;
14661 +       struct fsnotify_group *group;
14662 +
14663 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14664 +       BUG_ON(!ull);
14665 +
14666 +       mark = &hn->hn_mark;
14667 +       spin_lock(&mark->lock);
14668 +       group = mark->group;
14669 +       fsnotify_get_group(group);
14670 +       spin_unlock(&mark->lock);
14671 +       lockdep_off();
14672 +       fsnotify_destroy_mark(mark, group);
14673 +       fsnotify_put_group(group);
14674 +       lockdep_on();
14675 +
14676 +       /* free hn by myself */
14677 +       return 0;
14678 +}
14679 +
14680 +/* ---------------------------------------------------------------------- */
14681 +
14682 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14683 +{
14684 +       struct fsnotify_mark *mark;
14685 +
14686 +       mark = &hinode->hi_notify->hn_mark;
14687 +       spin_lock(&mark->lock);
14688 +       if (do_set) {
14689 +               AuDebugOn(mark->mask & AuHfsnMask);
14690 +               mark->mask |= AuHfsnMask;
14691 +       } else {
14692 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14693 +               mark->mask &= ~AuHfsnMask;
14694 +       }
14695 +       spin_unlock(&mark->lock);
14696 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14697 +}
14698 +
14699 +/* ---------------------------------------------------------------------- */
14700 +
14701 +/* #define AuDbgHnotify */
14702 +#ifdef AuDbgHnotify
14703 +static char *au_hfsn_name(u32 mask)
14704 +{
14705 +#ifdef CONFIG_AUFS_DEBUG
14706 +#define test_ret(flag)                         \
14707 +       do {                                    \
14708 +               if (mask & flag)                \
14709 +                       return #flag;           \
14710 +       } while (0)
14711 +       test_ret(FS_ACCESS);
14712 +       test_ret(FS_MODIFY);
14713 +       test_ret(FS_ATTRIB);
14714 +       test_ret(FS_CLOSE_WRITE);
14715 +       test_ret(FS_CLOSE_NOWRITE);
14716 +       test_ret(FS_OPEN);
14717 +       test_ret(FS_MOVED_FROM);
14718 +       test_ret(FS_MOVED_TO);
14719 +       test_ret(FS_CREATE);
14720 +       test_ret(FS_DELETE);
14721 +       test_ret(FS_DELETE_SELF);
14722 +       test_ret(FS_MOVE_SELF);
14723 +       test_ret(FS_UNMOUNT);
14724 +       test_ret(FS_Q_OVERFLOW);
14725 +       test_ret(FS_IN_IGNORED);
14726 +       test_ret(FS_ISDIR);
14727 +       test_ret(FS_IN_ONESHOT);
14728 +       test_ret(FS_EVENT_ON_CHILD);
14729 +       return "";
14730 +#undef test_ret
14731 +#else
14732 +       return "??";
14733 +#endif
14734 +}
14735 +#endif
14736 +
14737 +/* ---------------------------------------------------------------------- */
14738 +
14739 +static void au_hfsn_free_group(struct fsnotify_group *group)
14740 +{
14741 +       struct au_br_hfsnotify *hfsn = group->private;
14742 +
14743 +       AuDbg("here\n");
14744 +       kfree(hfsn);
14745 +}
14746 +
14747 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14748 +                               struct inode *inode,
14749 +                               struct fsnotify_mark *inode_mark,
14750 +                               struct fsnotify_mark *vfsmount_mark,
14751 +                               u32 mask, void *data, int data_type,
14752 +                               const unsigned char *file_name, u32 cookie)
14753 +{
14754 +       int err;
14755 +       struct au_hnotify *hnotify;
14756 +       struct inode *h_dir, *h_inode;
14757 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14758 +
14759 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14760 +
14761 +       err = 0;
14762 +       /* if FS_UNMOUNT happens, there must be another bug */
14763 +       AuDebugOn(mask & FS_UNMOUNT);
14764 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14765 +               goto out;
14766 +
14767 +       h_dir = inode;
14768 +       h_inode = NULL;
14769 +#ifdef AuDbgHnotify
14770 +       au_debug_on();
14771 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14772 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14773 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14774 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14775 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14776 +               /* WARN_ON(1); */
14777 +       }
14778 +       au_debug_off();
14779 +#endif
14780 +
14781 +       AuDebugOn(!inode_mark);
14782 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14783 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14784 +
14785 +out:
14786 +       return err;
14787 +}
14788 +
14789 +static struct fsnotify_ops au_hfsn_ops = {
14790 +       .handle_event           = au_hfsn_handle_event,
14791 +       .free_group_priv        = au_hfsn_free_group
14792 +};
14793 +
14794 +/* ---------------------------------------------------------------------- */
14795 +
14796 +static void au_hfsn_fin_br(struct au_branch *br)
14797 +{
14798 +       struct au_br_hfsnotify *hfsn;
14799 +
14800 +       hfsn = br->br_hfsn;
14801 +       if (hfsn) {
14802 +               lockdep_off();
14803 +               fsnotify_put_group(hfsn->hfsn_group);
14804 +               lockdep_on();
14805 +       }
14806 +}
14807 +
14808 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14809 +{
14810 +       int err;
14811 +       struct fsnotify_group *group;
14812 +       struct au_br_hfsnotify *hfsn;
14813 +
14814 +       err = 0;
14815 +       br->br_hfsn = NULL;
14816 +       if (!au_br_hnotifyable(perm))
14817 +               goto out;
14818 +
14819 +       err = -ENOMEM;
14820 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14821 +       if (unlikely(!hfsn))
14822 +               goto out;
14823 +
14824 +       err = 0;
14825 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14826 +       if (IS_ERR(group)) {
14827 +               err = PTR_ERR(group);
14828 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14829 +               goto out_hfsn;
14830 +       }
14831 +
14832 +       group->private = hfsn;
14833 +       hfsn->hfsn_group = group;
14834 +       br->br_hfsn = hfsn;
14835 +       goto out; /* success */
14836 +
14837 +out_hfsn:
14838 +       kfree(hfsn);
14839 +out:
14840 +       return err;
14841 +}
14842 +
14843 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14844 +{
14845 +       int err;
14846 +
14847 +       err = 0;
14848 +       if (!br->br_hfsn)
14849 +               err = au_hfsn_init_br(br, perm);
14850 +
14851 +       return err;
14852 +}
14853 +
14854 +/* ---------------------------------------------------------------------- */
14855 +
14856 +static void au_hfsn_fin(void)
14857 +{
14858 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14859 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14860 +}
14861 +
14862 +const struct au_hnotify_op au_hnotify_op = {
14863 +       .ctl            = au_hfsn_ctl,
14864 +       .alloc          = au_hfsn_alloc,
14865 +       .free           = au_hfsn_free,
14866 +
14867 +       .fin            = au_hfsn_fin,
14868 +
14869 +       .reset_br       = au_hfsn_reset_br,
14870 +       .fin_br         = au_hfsn_fin_br,
14871 +       .init_br        = au_hfsn_init_br
14872 +};
14873 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14874 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
14875 +++ linux/fs/aufs/hfsplus.c     2016-04-24 18:32:51.393686895 +0200
14876 @@ -0,0 +1,56 @@
14877 +/*
14878 + * Copyright (C) 2010-2016 Junjiro R. Okajima
14879 + *
14880 + * This program, aufs is free software; you can redistribute it and/or modify
14881 + * it under the terms of the GNU General Public License as published by
14882 + * the Free Software Foundation; either version 2 of the License, or
14883 + * (at your option) any later version.
14884 + *
14885 + * This program is distributed in the hope that it will be useful,
14886 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14887 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14888 + * GNU General Public License for more details.
14889 + *
14890 + * You should have received a copy of the GNU General Public License
14891 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14892 + */
14893 +
14894 +/*
14895 + * special support for filesystems which aqucires an inode mutex
14896 + * at final closing a file, eg, hfsplus.
14897 + *
14898 + * This trick is very simple and stupid, just to open the file before really
14899 + * neceeary open to tell hfsplus that this is not the final closing.
14900 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
14901 + * and au_h_open_post() after releasing it.
14902 + */
14903 +
14904 +#include "aufs.h"
14905 +
14906 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14907 +                          int force_wr)
14908 +{
14909 +       struct file *h_file;
14910 +       struct dentry *h_dentry;
14911 +
14912 +       h_dentry = au_h_dptr(dentry, bindex);
14913 +       AuDebugOn(!h_dentry);
14914 +       AuDebugOn(d_is_negative(h_dentry));
14915 +
14916 +       h_file = NULL;
14917 +       if (au_test_hfsplus(h_dentry->d_sb)
14918 +           && d_is_reg(h_dentry))
14919 +               h_file = au_h_open(dentry, bindex,
14920 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
14921 +                                  /*file*/NULL, force_wr);
14922 +       return h_file;
14923 +}
14924 +
14925 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14926 +                   struct file *h_file)
14927 +{
14928 +       if (h_file) {
14929 +               fput(h_file);
14930 +               au_sbr_put(dentry->d_sb, bindex);
14931 +       }
14932 +}
14933 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
14934 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
14935 +++ linux/fs/aufs/hnotify.c     2016-04-24 18:32:51.393686895 +0200
14936 @@ -0,0 +1,710 @@
14937 +/*
14938 + * Copyright (C) 2005-2016 Junjiro R. Okajima
14939 + *
14940 + * This program, aufs is free software; you can redistribute it and/or modify
14941 + * it under the terms of the GNU General Public License as published by
14942 + * the Free Software Foundation; either version 2 of the License, or
14943 + * (at your option) any later version.
14944 + *
14945 + * This program is distributed in the hope that it will be useful,
14946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14948 + * GNU General Public License for more details.
14949 + *
14950 + * You should have received a copy of the GNU General Public License
14951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14952 + */
14953 +
14954 +/*
14955 + * abstraction to notify the direct changes on lower directories
14956 + */
14957 +
14958 +#include "aufs.h"
14959 +
14960 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
14961 +{
14962 +       int err;
14963 +       struct au_hnotify *hn;
14964 +
14965 +       err = -ENOMEM;
14966 +       hn = au_cache_alloc_hnotify();
14967 +       if (hn) {
14968 +               hn->hn_aufs_inode = inode;
14969 +               hinode->hi_notify = hn;
14970 +               err = au_hnotify_op.alloc(hinode);
14971 +               AuTraceErr(err);
14972 +               if (unlikely(err)) {
14973 +                       hinode->hi_notify = NULL;
14974 +                       au_cache_free_hnotify(hn);
14975 +                       /*
14976 +                        * The upper dir was removed by udba, but the same named
14977 +                        * dir left. In this case, aufs assignes a new inode
14978 +                        * number and set the monitor again.
14979 +                        * For the lower dir, the old monitnor is still left.
14980 +                        */
14981 +                       if (err == -EEXIST)
14982 +                               err = 0;
14983 +               }
14984 +       }
14985 +
14986 +       AuTraceErr(err);
14987 +       return err;
14988 +}
14989 +
14990 +void au_hn_free(struct au_hinode *hinode)
14991 +{
14992 +       struct au_hnotify *hn;
14993 +
14994 +       hn = hinode->hi_notify;
14995 +       if (hn) {
14996 +               hinode->hi_notify = NULL;
14997 +               if (au_hnotify_op.free(hinode, hn))
14998 +                       au_cache_free_hnotify(hn);
14999 +       }
15000 +}
15001 +
15002 +/* ---------------------------------------------------------------------- */
15003 +
15004 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
15005 +{
15006 +       if (hinode->hi_notify)
15007 +               au_hnotify_op.ctl(hinode, do_set);
15008 +}
15009 +
15010 +void au_hn_reset(struct inode *inode, unsigned int flags)
15011 +{
15012 +       aufs_bindex_t bindex, bend;
15013 +       struct inode *hi;
15014 +       struct dentry *iwhdentry;
15015 +
15016 +       bend = au_ibend(inode);
15017 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
15018 +               hi = au_h_iptr(inode, bindex);
15019 +               if (!hi)
15020 +                       continue;
15021 +
15022 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
15023 +               iwhdentry = au_hi_wh(inode, bindex);
15024 +               if (iwhdentry)
15025 +                       dget(iwhdentry);
15026 +               au_igrab(hi);
15027 +               au_set_h_iptr(inode, bindex, NULL, 0);
15028 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
15029 +                             flags & ~AuHi_XINO);
15030 +               iput(hi);
15031 +               dput(iwhdentry);
15032 +               /* inode_unlock(hi); */
15033 +       }
15034 +}
15035 +
15036 +/* ---------------------------------------------------------------------- */
15037 +
15038 +static int hn_xino(struct inode *inode, struct inode *h_inode)
15039 +{
15040 +       int err;
15041 +       aufs_bindex_t bindex, bend, bfound, bstart;
15042 +       struct inode *h_i;
15043 +
15044 +       err = 0;
15045 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15046 +               pr_warn("branch root dir was changed\n");
15047 +               goto out;
15048 +       }
15049 +
15050 +       bfound = -1;
15051 +       bend = au_ibend(inode);
15052 +       bstart = au_ibstart(inode);
15053 +#if 0 /* reserved for future use */
15054 +       if (bindex == bend) {
15055 +               /* keep this ino in rename case */
15056 +               goto out;
15057 +       }
15058 +#endif
15059 +       for (bindex = bstart; bindex <= bend; bindex++)
15060 +               if (au_h_iptr(inode, bindex) == h_inode) {
15061 +                       bfound = bindex;
15062 +                       break;
15063 +               }
15064 +       if (bfound < 0)
15065 +               goto out;
15066 +
15067 +       for (bindex = bstart; bindex <= bend; bindex++) {
15068 +               h_i = au_h_iptr(inode, bindex);
15069 +               if (!h_i)
15070 +                       continue;
15071 +
15072 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
15073 +               /* ignore this error */
15074 +               /* bad action? */
15075 +       }
15076 +
15077 +       /* children inode number will be broken */
15078 +
15079 +out:
15080 +       AuTraceErr(err);
15081 +       return err;
15082 +}
15083 +
15084 +static int hn_gen_tree(struct dentry *dentry)
15085 +{
15086 +       int err, i, j, ndentry;
15087 +       struct au_dcsub_pages dpages;
15088 +       struct au_dpage *dpage;
15089 +       struct dentry **dentries;
15090 +
15091 +       err = au_dpages_init(&dpages, GFP_NOFS);
15092 +       if (unlikely(err))
15093 +               goto out;
15094 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
15095 +       if (unlikely(err))
15096 +               goto out_dpages;
15097 +
15098 +       for (i = 0; i < dpages.ndpage; i++) {
15099 +               dpage = dpages.dpages + i;
15100 +               dentries = dpage->dentries;
15101 +               ndentry = dpage->ndentry;
15102 +               for (j = 0; j < ndentry; j++) {
15103 +                       struct dentry *d;
15104 +
15105 +                       d = dentries[j];
15106 +                       if (IS_ROOT(d))
15107 +                               continue;
15108 +
15109 +                       au_digen_dec(d);
15110 +                       if (d_really_is_positive(d))
15111 +                               /* todo: reset children xino?
15112 +                                  cached children only? */
15113 +                               au_iigen_dec(d_inode(d));
15114 +               }
15115 +       }
15116 +
15117 +out_dpages:
15118 +       au_dpages_free(&dpages);
15119 +
15120 +#if 0
15121 +       /* discard children */
15122 +       dentry_unhash(dentry);
15123 +       dput(dentry);
15124 +#endif
15125 +out:
15126 +       return err;
15127 +}
15128 +
15129 +/*
15130 + * return 0 if processed.
15131 + */
15132 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
15133 +                          const unsigned int isdir)
15134 +{
15135 +       int err;
15136 +       struct dentry *d;
15137 +       struct qstr *dname;
15138 +
15139 +       err = 1;
15140 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15141 +               pr_warn("branch root dir was changed\n");
15142 +               err = 0;
15143 +               goto out;
15144 +       }
15145 +
15146 +       if (!isdir) {
15147 +               AuDebugOn(!name);
15148 +               au_iigen_dec(inode);
15149 +               spin_lock(&inode->i_lock);
15150 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
15151 +                       spin_lock(&d->d_lock);
15152 +                       dname = &d->d_name;
15153 +                       if (dname->len != nlen
15154 +                           && memcmp(dname->name, name, nlen)) {
15155 +                               spin_unlock(&d->d_lock);
15156 +                               continue;
15157 +                       }
15158 +                       err = 0;
15159 +                       au_digen_dec(d);
15160 +                       spin_unlock(&d->d_lock);
15161 +                       break;
15162 +               }
15163 +               spin_unlock(&inode->i_lock);
15164 +       } else {
15165 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
15166 +               d = d_find_any_alias(inode);
15167 +               if (!d) {
15168 +                       au_iigen_dec(inode);
15169 +                       goto out;
15170 +               }
15171 +
15172 +               spin_lock(&d->d_lock);
15173 +               dname = &d->d_name;
15174 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15175 +                       spin_unlock(&d->d_lock);
15176 +                       err = hn_gen_tree(d);
15177 +                       spin_lock(&d->d_lock);
15178 +               }
15179 +               spin_unlock(&d->d_lock);
15180 +               dput(d);
15181 +       }
15182 +
15183 +out:
15184 +       AuTraceErr(err);
15185 +       return err;
15186 +}
15187 +
15188 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15189 +{
15190 +       int err;
15191 +
15192 +       if (IS_ROOT(dentry)) {
15193 +               pr_warn("branch root dir was changed\n");
15194 +               return 0;
15195 +       }
15196 +
15197 +       err = 0;
15198 +       if (!isdir) {
15199 +               au_digen_dec(dentry);
15200 +               if (d_really_is_positive(dentry))
15201 +                       au_iigen_dec(d_inode(dentry));
15202 +       } else {
15203 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15204 +               if (d_really_is_positive(dentry))
15205 +                       err = hn_gen_tree(dentry);
15206 +       }
15207 +
15208 +       AuTraceErr(err);
15209 +       return err;
15210 +}
15211 +
15212 +/* ---------------------------------------------------------------------- */
15213 +
15214 +/* hnotify job flags */
15215 +#define AuHnJob_XINO0          1
15216 +#define AuHnJob_GEN            (1 << 1)
15217 +#define AuHnJob_DIRENT         (1 << 2)
15218 +#define AuHnJob_ISDIR          (1 << 3)
15219 +#define AuHnJob_TRYXINO0       (1 << 4)
15220 +#define AuHnJob_MNTPNT         (1 << 5)
15221 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15222 +#define au_fset_hnjob(flags, name) \
15223 +       do { (flags) |= AuHnJob_##name; } while (0)
15224 +#define au_fclr_hnjob(flags, name) \
15225 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15226 +
15227 +enum {
15228 +       AuHn_CHILD,
15229 +       AuHn_PARENT,
15230 +       AuHnLast
15231 +};
15232 +
15233 +struct au_hnotify_args {
15234 +       struct inode *h_dir, *dir, *h_child_inode;
15235 +       u32 mask;
15236 +       unsigned int flags[AuHnLast];
15237 +       unsigned int h_child_nlen;
15238 +       char h_child_name[];
15239 +};
15240 +
15241 +struct hn_job_args {
15242 +       unsigned int flags;
15243 +       struct inode *inode, *h_inode, *dir, *h_dir;
15244 +       struct dentry *dentry;
15245 +       char *h_name;
15246 +       int h_nlen;
15247 +};
15248 +
15249 +static int hn_job(struct hn_job_args *a)
15250 +{
15251 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15252 +       int e;
15253 +
15254 +       /* reset xino */
15255 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15256 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15257 +
15258 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15259 +           && a->inode
15260 +           && a->h_inode) {
15261 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
15262 +               if (!a->h_inode->i_nlink
15263 +                   && !(a->h_inode->i_state & I_LINKABLE))
15264 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15265 +               inode_unlock(a->h_inode);
15266 +       }
15267 +
15268 +       /* make the generation obsolete */
15269 +       if (au_ftest_hnjob(a->flags, GEN)) {
15270 +               e = -1;
15271 +               if (a->inode)
15272 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15273 +                                             isdir);
15274 +               if (e && a->dentry)
15275 +                       hn_gen_by_name(a->dentry, isdir);
15276 +               /* ignore this error */
15277 +       }
15278 +
15279 +       /* make dir entries obsolete */
15280 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15281 +               struct au_vdir *vdir;
15282 +
15283 +               vdir = au_ivdir(a->inode);
15284 +               if (vdir)
15285 +                       vdir->vd_jiffy = 0;
15286 +               /* IMustLock(a->inode); */
15287 +               /* a->inode->i_version++; */
15288 +       }
15289 +
15290 +       /* can do nothing but warn */
15291 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15292 +           && a->dentry
15293 +           && d_mountpoint(a->dentry))
15294 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15295 +
15296 +       return 0;
15297 +}
15298 +
15299 +/* ---------------------------------------------------------------------- */
15300 +
15301 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15302 +                                          struct inode *dir)
15303 +{
15304 +       struct dentry *dentry, *d, *parent;
15305 +       struct qstr *dname;
15306 +
15307 +       parent = d_find_any_alias(dir);
15308 +       if (!parent)
15309 +               return NULL;
15310 +
15311 +       dentry = NULL;
15312 +       spin_lock(&parent->d_lock);
15313 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15314 +               /* AuDbg("%pd\n", d); */
15315 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15316 +               dname = &d->d_name;
15317 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15318 +                       goto cont_unlock;
15319 +               if (au_di(d))
15320 +                       au_digen_dec(d);
15321 +               else
15322 +                       goto cont_unlock;
15323 +               if (au_dcount(d) > 0) {
15324 +                       dentry = dget_dlock(d);
15325 +                       spin_unlock(&d->d_lock);
15326 +                       break;
15327 +               }
15328 +
15329 +cont_unlock:
15330 +               spin_unlock(&d->d_lock);
15331 +       }
15332 +       spin_unlock(&parent->d_lock);
15333 +       dput(parent);
15334 +
15335 +       if (dentry)
15336 +               di_write_lock_child(dentry);
15337 +
15338 +       return dentry;
15339 +}
15340 +
15341 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15342 +                                        aufs_bindex_t bindex, ino_t h_ino)
15343 +{
15344 +       struct inode *inode;
15345 +       ino_t ino;
15346 +       int err;
15347 +
15348 +       inode = NULL;
15349 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15350 +       if (!err && ino)
15351 +               inode = ilookup(sb, ino);
15352 +       if (!inode)
15353 +               goto out;
15354 +
15355 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15356 +               pr_warn("wrong root branch\n");
15357 +               iput(inode);
15358 +               inode = NULL;
15359 +               goto out;
15360 +       }
15361 +
15362 +       ii_write_lock_child(inode);
15363 +
15364 +out:
15365 +       return inode;
15366 +}
15367 +
15368 +static void au_hn_bh(void *_args)
15369 +{
15370 +       struct au_hnotify_args *a = _args;
15371 +       struct super_block *sb;
15372 +       aufs_bindex_t bindex, bend, bfound;
15373 +       unsigned char xino, try_iput;
15374 +       int err;
15375 +       struct inode *inode;
15376 +       ino_t h_ino;
15377 +       struct hn_job_args args;
15378 +       struct dentry *dentry;
15379 +       struct au_sbinfo *sbinfo;
15380 +
15381 +       AuDebugOn(!_args);
15382 +       AuDebugOn(!a->h_dir);
15383 +       AuDebugOn(!a->dir);
15384 +       AuDebugOn(!a->mask);
15385 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15386 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15387 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15388 +
15389 +       inode = NULL;
15390 +       dentry = NULL;
15391 +       /*
15392 +        * do not lock a->dir->i_mutex here
15393 +        * because of d_revalidate() may cause a deadlock.
15394 +        */
15395 +       sb = a->dir->i_sb;
15396 +       AuDebugOn(!sb);
15397 +       sbinfo = au_sbi(sb);
15398 +       AuDebugOn(!sbinfo);
15399 +       si_write_lock(sb, AuLock_NOPLMW);
15400 +
15401 +       ii_read_lock_parent(a->dir);
15402 +       bfound = -1;
15403 +       bend = au_ibend(a->dir);
15404 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
15405 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15406 +                       bfound = bindex;
15407 +                       break;
15408 +               }
15409 +       ii_read_unlock(a->dir);
15410 +       if (unlikely(bfound < 0))
15411 +               goto out;
15412 +
15413 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15414 +       h_ino = 0;
15415 +       if (a->h_child_inode)
15416 +               h_ino = a->h_child_inode->i_ino;
15417 +
15418 +       if (a->h_child_nlen
15419 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15420 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15421 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15422 +                                             a->dir);
15423 +       try_iput = 0;
15424 +       if (dentry && d_really_is_positive(dentry))
15425 +               inode = d_inode(dentry);
15426 +       if (xino && !inode && h_ino
15427 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15428 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15429 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15430 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15431 +               try_iput = 1;
15432 +           }
15433 +
15434 +       args.flags = a->flags[AuHn_CHILD];
15435 +       args.dentry = dentry;
15436 +       args.inode = inode;
15437 +       args.h_inode = a->h_child_inode;
15438 +       args.dir = a->dir;
15439 +       args.h_dir = a->h_dir;
15440 +       args.h_name = a->h_child_name;
15441 +       args.h_nlen = a->h_child_nlen;
15442 +       err = hn_job(&args);
15443 +       if (dentry) {
15444 +               if (au_di(dentry))
15445 +                       di_write_unlock(dentry);
15446 +               dput(dentry);
15447 +       }
15448 +       if (inode && try_iput) {
15449 +               ii_write_unlock(inode);
15450 +               iput(inode);
15451 +       }
15452 +
15453 +       ii_write_lock_parent(a->dir);
15454 +       args.flags = a->flags[AuHn_PARENT];
15455 +       args.dentry = NULL;
15456 +       args.inode = a->dir;
15457 +       args.h_inode = a->h_dir;
15458 +       args.dir = NULL;
15459 +       args.h_dir = NULL;
15460 +       args.h_name = NULL;
15461 +       args.h_nlen = 0;
15462 +       err = hn_job(&args);
15463 +       ii_write_unlock(a->dir);
15464 +
15465 +out:
15466 +       iput(a->h_child_inode);
15467 +       iput(a->h_dir);
15468 +       iput(a->dir);
15469 +       si_write_unlock(sb);
15470 +       au_nwt_done(&sbinfo->si_nowait);
15471 +       kfree(a);
15472 +}
15473 +
15474 +/* ---------------------------------------------------------------------- */
15475 +
15476 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15477 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15478 +{
15479 +       int err, len;
15480 +       unsigned int flags[AuHnLast], f;
15481 +       unsigned char isdir, isroot, wh;
15482 +       struct inode *dir;
15483 +       struct au_hnotify_args *args;
15484 +       char *p, *h_child_name;
15485 +
15486 +       err = 0;
15487 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15488 +       dir = igrab(hnotify->hn_aufs_inode);
15489 +       if (!dir)
15490 +               goto out;
15491 +
15492 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15493 +       wh = 0;
15494 +       h_child_name = (void *)h_child_qstr->name;
15495 +       len = h_child_qstr->len;
15496 +       if (h_child_name) {
15497 +               if (len > AUFS_WH_PFX_LEN
15498 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15499 +                       h_child_name += AUFS_WH_PFX_LEN;
15500 +                       len -= AUFS_WH_PFX_LEN;
15501 +                       wh = 1;
15502 +               }
15503 +       }
15504 +
15505 +       isdir = 0;
15506 +       if (h_child_inode)
15507 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15508 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15509 +       flags[AuHn_CHILD] = 0;
15510 +       if (isdir)
15511 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15512 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15513 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15514 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15515 +       case FS_MOVED_FROM:
15516 +       case FS_MOVED_TO:
15517 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15518 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15519 +               /*FALLTHROUGH*/
15520 +       case FS_CREATE:
15521 +               AuDebugOn(!h_child_name);
15522 +               break;
15523 +
15524 +       case FS_DELETE:
15525 +               /*
15526 +                * aufs never be able to get this child inode.
15527 +                * revalidation should be in d_revalidate()
15528 +                * by checking i_nlink, i_generation or d_unhashed().
15529 +                */
15530 +               AuDebugOn(!h_child_name);
15531 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15532 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15533 +               break;
15534 +
15535 +       default:
15536 +               AuDebugOn(1);
15537 +       }
15538 +
15539 +       if (wh)
15540 +               h_child_inode = NULL;
15541 +
15542 +       err = -ENOMEM;
15543 +       /* iput() and kfree() will be called in au_hnotify() */
15544 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15545 +       if (unlikely(!args)) {
15546 +               AuErr1("no memory\n");
15547 +               iput(dir);
15548 +               goto out;
15549 +       }
15550 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15551 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15552 +       args->mask = mask;
15553 +       args->dir = dir;
15554 +       args->h_dir = igrab(h_dir);
15555 +       if (h_child_inode)
15556 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15557 +       args->h_child_inode = h_child_inode;
15558 +       args->h_child_nlen = len;
15559 +       if (len) {
15560 +               p = (void *)args;
15561 +               p += sizeof(*args);
15562 +               memcpy(p, h_child_name, len);
15563 +               p[len] = 0;
15564 +       }
15565 +
15566 +       /* NFS fires the event for silly-renamed one from kworker */
15567 +       f = 0;
15568 +       if (!dir->i_nlink
15569 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15570 +               f = AuWkq_NEST;
15571 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15572 +       if (unlikely(err)) {
15573 +               pr_err("wkq %d\n", err);
15574 +               iput(args->h_child_inode);
15575 +               iput(args->h_dir);
15576 +               iput(args->dir);
15577 +               kfree(args);
15578 +       }
15579 +
15580 +out:
15581 +       return err;
15582 +}
15583 +
15584 +/* ---------------------------------------------------------------------- */
15585 +
15586 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15587 +{
15588 +       int err;
15589 +
15590 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15591 +
15592 +       err = 0;
15593 +       if (au_hnotify_op.reset_br)
15594 +               err = au_hnotify_op.reset_br(udba, br, perm);
15595 +
15596 +       return err;
15597 +}
15598 +
15599 +int au_hnotify_init_br(struct au_branch *br, int perm)
15600 +{
15601 +       int err;
15602 +
15603 +       err = 0;
15604 +       if (au_hnotify_op.init_br)
15605 +               err = au_hnotify_op.init_br(br, perm);
15606 +
15607 +       return err;
15608 +}
15609 +
15610 +void au_hnotify_fin_br(struct au_branch *br)
15611 +{
15612 +       if (au_hnotify_op.fin_br)
15613 +               au_hnotify_op.fin_br(br);
15614 +}
15615 +
15616 +static void au_hn_destroy_cache(void)
15617 +{
15618 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15619 +       au_cachep[AuCache_HNOTIFY] = NULL;
15620 +}
15621 +
15622 +int __init au_hnotify_init(void)
15623 +{
15624 +       int err;
15625 +
15626 +       err = -ENOMEM;
15627 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15628 +       if (au_cachep[AuCache_HNOTIFY]) {
15629 +               err = 0;
15630 +               if (au_hnotify_op.init)
15631 +                       err = au_hnotify_op.init();
15632 +               if (unlikely(err))
15633 +                       au_hn_destroy_cache();
15634 +       }
15635 +       AuTraceErr(err);
15636 +       return err;
15637 +}
15638 +
15639 +void au_hnotify_fin(void)
15640 +{
15641 +       if (au_hnotify_op.fin)
15642 +               au_hnotify_op.fin();
15643 +       /* cf. au_cache_fin() */
15644 +       if (au_cachep[AuCache_HNOTIFY])
15645 +               au_hn_destroy_cache();
15646 +}
15647 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15648 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15649 +++ linux/fs/aufs/iinfo.c       2016-04-24 18:32:51.393686895 +0200
15650 @@ -0,0 +1,277 @@
15651 +/*
15652 + * Copyright (C) 2005-2016 Junjiro R. Okajima
15653 + *
15654 + * This program, aufs is free software; you can redistribute it and/or modify
15655 + * it under the terms of the GNU General Public License as published by
15656 + * the Free Software Foundation; either version 2 of the License, or
15657 + * (at your option) any later version.
15658 + *
15659 + * This program is distributed in the hope that it will be useful,
15660 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15661 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15662 + * GNU General Public License for more details.
15663 + *
15664 + * You should have received a copy of the GNU General Public License
15665 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15666 + */
15667 +
15668 +/*
15669 + * inode private data
15670 + */
15671 +
15672 +#include "aufs.h"
15673 +
15674 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15675 +{
15676 +       struct inode *h_inode;
15677 +
15678 +       IiMustAnyLock(inode);
15679 +
15680 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15681 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15682 +       return h_inode;
15683 +}
15684 +
15685 +/* todo: hard/soft set? */
15686 +void au_hiput(struct au_hinode *hinode)
15687 +{
15688 +       au_hn_free(hinode);
15689 +       dput(hinode->hi_whdentry);
15690 +       iput(hinode->hi_inode);
15691 +}
15692 +
15693 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15694 +{
15695 +       unsigned int flags;
15696 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15697 +
15698 +       flags = 0;
15699 +       if (au_opt_test(mnt_flags, XINO))
15700 +               au_fset_hi(flags, XINO);
15701 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15702 +               au_fset_hi(flags, HNOTIFY);
15703 +       return flags;
15704 +}
15705 +
15706 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15707 +                  struct inode *h_inode, unsigned int flags)
15708 +{
15709 +       struct au_hinode *hinode;
15710 +       struct inode *hi;
15711 +       struct au_iinfo *iinfo = au_ii(inode);
15712 +
15713 +       IiMustWriteLock(inode);
15714 +
15715 +       hinode = iinfo->ii_hinode + bindex;
15716 +       hi = hinode->hi_inode;
15717 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15718 +
15719 +       if (hi)
15720 +               au_hiput(hinode);
15721 +       hinode->hi_inode = h_inode;
15722 +       if (h_inode) {
15723 +               int err;
15724 +               struct super_block *sb = inode->i_sb;
15725 +               struct au_branch *br;
15726 +
15727 +               AuDebugOn(inode->i_mode
15728 +                         && (h_inode->i_mode & S_IFMT)
15729 +                         != (inode->i_mode & S_IFMT));
15730 +               if (bindex == iinfo->ii_bstart)
15731 +                       au_cpup_igen(inode, h_inode);
15732 +               br = au_sbr(sb, bindex);
15733 +               hinode->hi_id = br->br_id;
15734 +               if (au_ftest_hi(flags, XINO)) {
15735 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15736 +                                           inode->i_ino);
15737 +                       if (unlikely(err))
15738 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15739 +               }
15740 +
15741 +               if (au_ftest_hi(flags, HNOTIFY)
15742 +                   && au_br_hnotifyable(br->br_perm)) {
15743 +                       err = au_hn_alloc(hinode, inode);
15744 +                       if (unlikely(err))
15745 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15746 +               }
15747 +       }
15748 +}
15749 +
15750 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15751 +                 struct dentry *h_wh)
15752 +{
15753 +       struct au_hinode *hinode;
15754 +
15755 +       IiMustWriteLock(inode);
15756 +
15757 +       hinode = au_ii(inode)->ii_hinode + bindex;
15758 +       AuDebugOn(hinode->hi_whdentry);
15759 +       hinode->hi_whdentry = h_wh;
15760 +}
15761 +
15762 +void au_update_iigen(struct inode *inode, int half)
15763 +{
15764 +       struct au_iinfo *iinfo;
15765 +       struct au_iigen *iigen;
15766 +       unsigned int sigen;
15767 +
15768 +       sigen = au_sigen(inode->i_sb);
15769 +       iinfo = au_ii(inode);
15770 +       iigen = &iinfo->ii_generation;
15771 +       spin_lock(&iigen->ig_spin);
15772 +       iigen->ig_generation = sigen;
15773 +       if (half)
15774 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15775 +       else
15776 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15777 +       spin_unlock(&iigen->ig_spin);
15778 +}
15779 +
15780 +/* it may be called at remount time, too */
15781 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15782 +{
15783 +       struct au_iinfo *iinfo;
15784 +       aufs_bindex_t bindex, bend;
15785 +
15786 +       iinfo = au_ii(inode);
15787 +       if (!iinfo)
15788 +               return;
15789 +
15790 +       IiMustWriteLock(inode);
15791 +
15792 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
15793 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15794 +                    bindex++) {
15795 +                       struct inode *h_i;
15796 +
15797 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15798 +                       if (h_i
15799 +                           && !h_i->i_nlink
15800 +                           && !(h_i->i_state & I_LINKABLE))
15801 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15802 +               }
15803 +       }
15804 +
15805 +       iinfo->ii_bstart = -1;
15806 +       iinfo->ii_bend = -1;
15807 +       bend = au_sbend(inode->i_sb);
15808 +       for (bindex = 0; bindex <= bend; bindex++)
15809 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15810 +                       iinfo->ii_bstart = bindex;
15811 +                       break;
15812 +               }
15813 +       if (iinfo->ii_bstart >= 0)
15814 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
15815 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15816 +                               iinfo->ii_bend = bindex;
15817 +                               break;
15818 +                       }
15819 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
15820 +}
15821 +
15822 +/* ---------------------------------------------------------------------- */
15823 +
15824 +void au_icntnr_init_once(void *_c)
15825 +{
15826 +       struct au_icntnr *c = _c;
15827 +       struct au_iinfo *iinfo = &c->iinfo;
15828 +       static struct lock_class_key aufs_ii;
15829 +
15830 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
15831 +       au_rw_init(&iinfo->ii_rwsem);
15832 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
15833 +       inode_init_once(&c->vfs_inode);
15834 +}
15835 +
15836 +int au_iinfo_init(struct inode *inode)
15837 +{
15838 +       struct au_iinfo *iinfo;
15839 +       struct super_block *sb;
15840 +       int nbr, i;
15841 +
15842 +       sb = inode->i_sb;
15843 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15844 +       nbr = au_sbend(sb) + 1;
15845 +       if (unlikely(nbr <= 0))
15846 +               nbr = 1;
15847 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15848 +       if (iinfo->ii_hinode) {
15849 +               au_ninodes_inc(sb);
15850 +               for (i = 0; i < nbr; i++)
15851 +                       iinfo->ii_hinode[i].hi_id = -1;
15852 +
15853 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
15854 +               iinfo->ii_bstart = -1;
15855 +               iinfo->ii_bend = -1;
15856 +               iinfo->ii_vdir = NULL;
15857 +               return 0;
15858 +       }
15859 +       return -ENOMEM;
15860 +}
15861 +
15862 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15863 +{
15864 +       int err, sz;
15865 +       struct au_hinode *hip;
15866 +
15867 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
15868 +
15869 +       err = -ENOMEM;
15870 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15871 +       if (!sz)
15872 +               sz = sizeof(*hip);
15873 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15874 +       if (hip) {
15875 +               iinfo->ii_hinode = hip;
15876 +               err = 0;
15877 +       }
15878 +
15879 +       return err;
15880 +}
15881 +
15882 +void au_iinfo_fin(struct inode *inode)
15883 +{
15884 +       struct au_iinfo *iinfo;
15885 +       struct au_hinode *hi;
15886 +       struct super_block *sb;
15887 +       aufs_bindex_t bindex, bend;
15888 +       const unsigned char unlinked = !inode->i_nlink;
15889 +
15890 +       iinfo = au_ii(inode);
15891 +       /* bad_inode case */
15892 +       if (!iinfo)
15893 +               return;
15894 +
15895 +       sb = inode->i_sb;
15896 +       au_ninodes_dec(sb);
15897 +       if (si_pid_test(sb))
15898 +               au_xino_delete_inode(inode, unlinked);
15899 +       else {
15900 +               /*
15901 +                * it is safe to hide the dependency between sbinfo and
15902 +                * sb->s_umount.
15903 +                */
15904 +               lockdep_off();
15905 +               si_noflush_read_lock(sb);
15906 +               au_xino_delete_inode(inode, unlinked);
15907 +               si_read_unlock(sb);
15908 +               lockdep_on();
15909 +       }
15910 +
15911 +       if (iinfo->ii_vdir)
15912 +               au_vdir_free(iinfo->ii_vdir);
15913 +
15914 +       bindex = iinfo->ii_bstart;
15915 +       if (bindex >= 0) {
15916 +               hi = iinfo->ii_hinode + bindex;
15917 +               bend = iinfo->ii_bend;
15918 +               while (bindex++ <= bend) {
15919 +                       if (hi->hi_inode)
15920 +                               au_hiput(hi);
15921 +                       hi++;
15922 +               }
15923 +       }
15924 +       kfree(iinfo->ii_hinode);
15925 +       iinfo->ii_hinode = NULL;
15926 +       AuRwDestroy(&iinfo->ii_rwsem);
15927 +}
15928 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
15929 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
15930 +++ linux/fs/aufs/inode.c       2016-04-24 18:32:51.393686895 +0200
15931 @@ -0,0 +1,527 @@
15932 +/*
15933 + * Copyright (C) 2005-2016 Junjiro R. Okajima
15934 + *
15935 + * This program, aufs is free software; you can redistribute it and/or modify
15936 + * it under the terms of the GNU General Public License as published by
15937 + * the Free Software Foundation; either version 2 of the License, or
15938 + * (at your option) any later version.
15939 + *
15940 + * This program is distributed in the hope that it will be useful,
15941 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15942 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15943 + * GNU General Public License for more details.
15944 + *
15945 + * You should have received a copy of the GNU General Public License
15946 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15947 + */
15948 +
15949 +/*
15950 + * inode functions
15951 + */
15952 +
15953 +#include "aufs.h"
15954 +
15955 +struct inode *au_igrab(struct inode *inode)
15956 +{
15957 +       if (inode) {
15958 +               AuDebugOn(!atomic_read(&inode->i_count));
15959 +               ihold(inode);
15960 +       }
15961 +       return inode;
15962 +}
15963 +
15964 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15965 +{
15966 +       au_cpup_attr_all(inode, /*force*/0);
15967 +       au_update_iigen(inode, /*half*/1);
15968 +       if (do_version)
15969 +               inode->i_version++;
15970 +}
15971 +
15972 +static int au_ii_refresh(struct inode *inode, int *update)
15973 +{
15974 +       int err, e;
15975 +       umode_t type;
15976 +       aufs_bindex_t bindex, new_bindex;
15977 +       struct super_block *sb;
15978 +       struct au_iinfo *iinfo;
15979 +       struct au_hinode *p, *q, tmp;
15980 +
15981 +       IiMustWriteLock(inode);
15982 +
15983 +       *update = 0;
15984 +       sb = inode->i_sb;
15985 +       type = inode->i_mode & S_IFMT;
15986 +       iinfo = au_ii(inode);
15987 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15988 +       if (unlikely(err))
15989 +               goto out;
15990 +
15991 +       AuDebugOn(iinfo->ii_bstart < 0);
15992 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15993 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15994 +            bindex++, p++) {
15995 +               if (!p->hi_inode)
15996 +                       continue;
15997 +
15998 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
15999 +               new_bindex = au_br_index(sb, p->hi_id);
16000 +               if (new_bindex == bindex)
16001 +                       continue;
16002 +
16003 +               if (new_bindex < 0) {
16004 +                       *update = 1;
16005 +                       au_hiput(p);
16006 +                       p->hi_inode = NULL;
16007 +                       continue;
16008 +               }
16009 +
16010 +               if (new_bindex < iinfo->ii_bstart)
16011 +                       iinfo->ii_bstart = new_bindex;
16012 +               if (iinfo->ii_bend < new_bindex)
16013 +                       iinfo->ii_bend = new_bindex;
16014 +               /* swap two lower inode, and loop again */
16015 +               q = iinfo->ii_hinode + new_bindex;
16016 +               tmp = *q;
16017 +               *q = *p;
16018 +               *p = tmp;
16019 +               if (tmp.hi_inode) {
16020 +                       bindex--;
16021 +                       p--;
16022 +               }
16023 +       }
16024 +       au_update_ibrange(inode, /*do_put_zero*/0);
16025 +       e = au_dy_irefresh(inode);
16026 +       if (unlikely(e && !err))
16027 +               err = e;
16028 +
16029 +out:
16030 +       AuTraceErr(err);
16031 +       return err;
16032 +}
16033 +
16034 +void au_refresh_iop(struct inode *inode, int force_getattr)
16035 +{
16036 +       int type;
16037 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
16038 +       const struct inode_operations *iop
16039 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
16040 +
16041 +       if (inode->i_op == iop)
16042 +               return;
16043 +
16044 +       switch (inode->i_mode & S_IFMT) {
16045 +       case S_IFDIR:
16046 +               type = AuIop_DIR;
16047 +               break;
16048 +       case S_IFLNK:
16049 +               type = AuIop_SYMLINK;
16050 +               break;
16051 +       default:
16052 +               type = AuIop_OTHER;
16053 +               break;
16054 +       }
16055 +
16056 +       inode->i_op = iop + type;
16057 +       /* unnecessary smp_wmb() */
16058 +}
16059 +
16060 +int au_refresh_hinode_self(struct inode *inode)
16061 +{
16062 +       int err, update;
16063 +
16064 +       err = au_ii_refresh(inode, &update);
16065 +       if (!err)
16066 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
16067 +
16068 +       AuTraceErr(err);
16069 +       return err;
16070 +}
16071 +
16072 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
16073 +{
16074 +       int err, e, update;
16075 +       unsigned int flags;
16076 +       umode_t mode;
16077 +       aufs_bindex_t bindex, bend;
16078 +       unsigned char isdir;
16079 +       struct au_hinode *p;
16080 +       struct au_iinfo *iinfo;
16081 +
16082 +       err = au_ii_refresh(inode, &update);
16083 +       if (unlikely(err))
16084 +               goto out;
16085 +
16086 +       update = 0;
16087 +       iinfo = au_ii(inode);
16088 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
16089 +       mode = (inode->i_mode & S_IFMT);
16090 +       isdir = S_ISDIR(mode);
16091 +       flags = au_hi_flags(inode, isdir);
16092 +       bend = au_dbend(dentry);
16093 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
16094 +               struct inode *h_i, *h_inode;
16095 +               struct dentry *h_d;
16096 +
16097 +               h_d = au_h_dptr(dentry, bindex);
16098 +               if (!h_d || d_is_negative(h_d))
16099 +                       continue;
16100 +
16101 +               h_inode = d_inode(h_d);
16102 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
16103 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
16104 +                       h_i = au_h_iptr(inode, bindex);
16105 +                       if (h_i) {
16106 +                               if (h_i == h_inode)
16107 +                                       continue;
16108 +                               err = -EIO;
16109 +                               break;
16110 +                       }
16111 +               }
16112 +               if (bindex < iinfo->ii_bstart)
16113 +                       iinfo->ii_bstart = bindex;
16114 +               if (iinfo->ii_bend < bindex)
16115 +                       iinfo->ii_bend = bindex;
16116 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
16117 +               update = 1;
16118 +       }
16119 +       au_update_ibrange(inode, /*do_put_zero*/0);
16120 +       e = au_dy_irefresh(inode);
16121 +       if (unlikely(e && !err))
16122 +               err = e;
16123 +       if (!err)
16124 +               au_refresh_hinode_attr(inode, update && isdir);
16125 +
16126 +out:
16127 +       AuTraceErr(err);
16128 +       return err;
16129 +}
16130 +
16131 +static int set_inode(struct inode *inode, struct dentry *dentry)
16132 +{
16133 +       int err;
16134 +       unsigned int flags;
16135 +       umode_t mode;
16136 +       aufs_bindex_t bindex, bstart, btail;
16137 +       unsigned char isdir;
16138 +       struct dentry *h_dentry;
16139 +       struct inode *h_inode;
16140 +       struct au_iinfo *iinfo;
16141 +       struct inode_operations *iop;
16142 +
16143 +       IiMustWriteLock(inode);
16144 +
16145 +       err = 0;
16146 +       isdir = 0;
16147 +       iop = au_sbi(inode->i_sb)->si_iop_array;
16148 +       bstart = au_dbstart(dentry);
16149 +       h_dentry = au_h_dptr(dentry, bstart);
16150 +       h_inode = d_inode(h_dentry);
16151 +       mode = h_inode->i_mode;
16152 +       switch (mode & S_IFMT) {
16153 +       case S_IFREG:
16154 +               btail = au_dbtail(dentry);
16155 +               inode->i_op = iop + AuIop_OTHER;
16156 +               inode->i_fop = &aufs_file_fop;
16157 +               err = au_dy_iaop(inode, bstart, h_inode);
16158 +               if (unlikely(err))
16159 +                       goto out;
16160 +               break;
16161 +       case S_IFDIR:
16162 +               isdir = 1;
16163 +               btail = au_dbtaildir(dentry);
16164 +               inode->i_op = iop + AuIop_DIR;
16165 +               inode->i_fop = &aufs_dir_fop;
16166 +               break;
16167 +       case S_IFLNK:
16168 +               btail = au_dbtail(dentry);
16169 +               inode->i_op = iop + AuIop_SYMLINK;
16170 +               break;
16171 +       case S_IFBLK:
16172 +       case S_IFCHR:
16173 +       case S_IFIFO:
16174 +       case S_IFSOCK:
16175 +               btail = au_dbtail(dentry);
16176 +               inode->i_op = iop + AuIop_OTHER;
16177 +               init_special_inode(inode, mode, h_inode->i_rdev);
16178 +               break;
16179 +       default:
16180 +               AuIOErr("Unknown file type 0%o\n", mode);
16181 +               err = -EIO;
16182 +               goto out;
16183 +       }
16184 +
16185 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
16186 +       flags = au_hi_flags(inode, isdir);
16187 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16188 +           && au_ftest_hi(flags, HNOTIFY)
16189 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
16190 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16191 +               au_fclr_hi(flags, HNOTIFY);
16192 +       iinfo = au_ii(inode);
16193 +       iinfo->ii_bstart = bstart;
16194 +       iinfo->ii_bend = btail;
16195 +       for (bindex = bstart; bindex <= btail; bindex++) {
16196 +               h_dentry = au_h_dptr(dentry, bindex);
16197 +               if (h_dentry)
16198 +                       au_set_h_iptr(inode, bindex,
16199 +                                     au_igrab(d_inode(h_dentry)), flags);
16200 +       }
16201 +       au_cpup_attr_all(inode, /*force*/1);
16202 +       /*
16203 +        * to force calling aufs_get_acl() every time,
16204 +        * do not call cache_no_acl() for aufs inode.
16205 +        */
16206 +
16207 +out:
16208 +       return err;
16209 +}
16210 +
16211 +/*
16212 + * successful returns with iinfo write_locked
16213 + * minus: errno
16214 + * zero: success, matched
16215 + * plus: no error, but unmatched
16216 + */
16217 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16218 +{
16219 +       int err;
16220 +       unsigned int gen, igflags;
16221 +       aufs_bindex_t bindex, bend;
16222 +       struct inode *h_inode, *h_dinode;
16223 +       struct dentry *h_dentry;
16224 +
16225 +       /*
16226 +        * before this function, if aufs got any iinfo lock, it must be only
16227 +        * one, the parent dir.
16228 +        * it can happen by UDBA and the obsoleted inode number.
16229 +        */
16230 +       err = -EIO;
16231 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16232 +               goto out;
16233 +
16234 +       err = 1;
16235 +       ii_write_lock_new_child(inode);
16236 +       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
16237 +       h_dinode = d_inode(h_dentry);
16238 +       bend = au_ibend(inode);
16239 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
16240 +               h_inode = au_h_iptr(inode, bindex);
16241 +               if (!h_inode || h_inode != h_dinode)
16242 +                       continue;
16243 +
16244 +               err = 0;
16245 +               gen = au_iigen(inode, &igflags);
16246 +               if (gen == au_digen(dentry)
16247 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
16248 +                       break;
16249 +
16250 +               /* fully refresh inode using dentry */
16251 +               err = au_refresh_hinode(inode, dentry);
16252 +               if (!err)
16253 +                       au_update_iigen(inode, /*half*/0);
16254 +               break;
16255 +       }
16256 +
16257 +       if (unlikely(err))
16258 +               ii_write_unlock(inode);
16259 +out:
16260 +       return err;
16261 +}
16262 +
16263 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16264 +          unsigned int d_type, ino_t *ino)
16265 +{
16266 +       int err;
16267 +       struct mutex *mtx;
16268 +
16269 +       /* prevent hardlinked inode number from race condition */
16270 +       mtx = NULL;
16271 +       if (d_type != DT_DIR) {
16272 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16273 +               mutex_lock(mtx);
16274 +       }
16275 +       err = au_xino_read(sb, bindex, h_ino, ino);
16276 +       if (unlikely(err))
16277 +               goto out;
16278 +
16279 +       if (!*ino) {
16280 +               err = -EIO;
16281 +               *ino = au_xino_new_ino(sb);
16282 +               if (unlikely(!*ino))
16283 +                       goto out;
16284 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16285 +               if (unlikely(err))
16286 +                       goto out;
16287 +       }
16288 +
16289 +out:
16290 +       if (mtx)
16291 +               mutex_unlock(mtx);
16292 +       return err;
16293 +}
16294 +
16295 +/* successful returns with iinfo write_locked */
16296 +/* todo: return with unlocked? */
16297 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16298 +{
16299 +       struct inode *inode, *h_inode;
16300 +       struct dentry *h_dentry;
16301 +       struct super_block *sb;
16302 +       struct mutex *mtx;
16303 +       ino_t h_ino, ino;
16304 +       int err;
16305 +       aufs_bindex_t bstart;
16306 +
16307 +       sb = dentry->d_sb;
16308 +       bstart = au_dbstart(dentry);
16309 +       h_dentry = au_h_dptr(dentry, bstart);
16310 +       h_inode = d_inode(h_dentry);
16311 +       h_ino = h_inode->i_ino;
16312 +
16313 +       /*
16314 +        * stop 'race'-ing between hardlinks under different
16315 +        * parents.
16316 +        */
16317 +       mtx = NULL;
16318 +       if (!d_is_dir(h_dentry))
16319 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
16320 +
16321 +new_ino:
16322 +       if (mtx)
16323 +               mutex_lock(mtx);
16324 +       err = au_xino_read(sb, bstart, h_ino, &ino);
16325 +       inode = ERR_PTR(err);
16326 +       if (unlikely(err))
16327 +               goto out;
16328 +
16329 +       if (!ino) {
16330 +               ino = au_xino_new_ino(sb);
16331 +               if (unlikely(!ino)) {
16332 +                       inode = ERR_PTR(-EIO);
16333 +                       goto out;
16334 +               }
16335 +       }
16336 +
16337 +       AuDbg("i%lu\n", (unsigned long)ino);
16338 +       inode = au_iget_locked(sb, ino);
16339 +       err = PTR_ERR(inode);
16340 +       if (IS_ERR(inode))
16341 +               goto out;
16342 +
16343 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16344 +       if (inode->i_state & I_NEW) {
16345 +               /* verbose coding for lock class name */
16346 +               if (unlikely(d_is_symlink(h_dentry)))
16347 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16348 +                                   au_lc_key + AuLcSymlink_IIINFO);
16349 +               else if (unlikely(d_is_dir(h_dentry)))
16350 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16351 +                                   au_lc_key + AuLcDir_IIINFO);
16352 +               else /* likely */
16353 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16354 +                                   au_lc_key + AuLcNonDir_IIINFO);
16355 +
16356 +               ii_write_lock_new_child(inode);
16357 +               err = set_inode(inode, dentry);
16358 +               if (!err) {
16359 +                       unlock_new_inode(inode);
16360 +                       goto out; /* success */
16361 +               }
16362 +
16363 +               /*
16364 +                * iget_failed() calls iput(), but we need to call
16365 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16366 +                * i_count.
16367 +                */
16368 +               atomic_inc(&inode->i_count);
16369 +               iget_failed(inode);
16370 +               ii_write_unlock(inode);
16371 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
16372 +               /* ignore this error */
16373 +               goto out_iput;
16374 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16375 +               /*
16376 +                * horrible race condition between lookup, readdir and copyup
16377 +                * (or something).
16378 +                */
16379 +               if (mtx)
16380 +                       mutex_unlock(mtx);
16381 +               err = reval_inode(inode, dentry);
16382 +               if (unlikely(err < 0)) {
16383 +                       mtx = NULL;
16384 +                       goto out_iput;
16385 +               }
16386 +
16387 +               if (!err) {
16388 +                       mtx = NULL;
16389 +                       goto out; /* success */
16390 +               } else if (mtx)
16391 +                       mutex_lock(mtx);
16392 +       }
16393 +
16394 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
16395 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16396 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16397 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
16398 +                       (unsigned long)h_ino, (unsigned long)ino);
16399 +       ino = 0;
16400 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
16401 +       if (!err) {
16402 +               iput(inode);
16403 +               if (mtx)
16404 +                       mutex_unlock(mtx);
16405 +               goto new_ino;
16406 +       }
16407 +
16408 +out_iput:
16409 +       iput(inode);
16410 +       inode = ERR_PTR(err);
16411 +out:
16412 +       if (mtx)
16413 +               mutex_unlock(mtx);
16414 +       return inode;
16415 +}
16416 +
16417 +/* ---------------------------------------------------------------------- */
16418 +
16419 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16420 +              struct inode *inode)
16421 +{
16422 +       int err;
16423 +       struct inode *hi;
16424 +
16425 +       err = au_br_rdonly(au_sbr(sb, bindex));
16426 +
16427 +       /* pseudo-link after flushed may happen out of bounds */
16428 +       if (!err
16429 +           && inode
16430 +           && au_ibstart(inode) <= bindex
16431 +           && bindex <= au_ibend(inode)) {
16432 +               /*
16433 +                * permission check is unnecessary since vfsub routine
16434 +                * will be called later
16435 +                */
16436 +               hi = au_h_iptr(inode, bindex);
16437 +               if (hi)
16438 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16439 +       }
16440 +
16441 +       return err;
16442 +}
16443 +
16444 +int au_test_h_perm(struct inode *h_inode, int mask)
16445 +{
16446 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16447 +               return 0;
16448 +       return inode_permission(h_inode, mask);
16449 +}
16450 +
16451 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16452 +{
16453 +       if (au_test_nfs(h_inode->i_sb)
16454 +           && (mask & MAY_WRITE)
16455 +           && S_ISDIR(h_inode->i_mode))
16456 +               mask |= MAY_READ; /* force permission check */
16457 +       return au_test_h_perm(h_inode, mask);
16458 +}
16459 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16460 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16461 +++ linux/fs/aufs/inode.h       2016-04-24 18:32:51.393686895 +0200
16462 @@ -0,0 +1,685 @@
16463 +/*
16464 + * Copyright (C) 2005-2016 Junjiro R. Okajima
16465 + *
16466 + * This program, aufs is free software; you can redistribute it and/or modify
16467 + * it under the terms of the GNU General Public License as published by
16468 + * the Free Software Foundation; either version 2 of the License, or
16469 + * (at your option) any later version.
16470 + *
16471 + * This program is distributed in the hope that it will be useful,
16472 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16473 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16474 + * GNU General Public License for more details.
16475 + *
16476 + * You should have received a copy of the GNU General Public License
16477 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16478 + */
16479 +
16480 +/*
16481 + * inode operations
16482 + */
16483 +
16484 +#ifndef __AUFS_INODE_H__
16485 +#define __AUFS_INODE_H__
16486 +
16487 +#ifdef __KERNEL__
16488 +
16489 +#include <linux/fsnotify.h>
16490 +#include "rwsem.h"
16491 +
16492 +struct vfsmount;
16493 +
16494 +struct au_hnotify {
16495 +#ifdef CONFIG_AUFS_HNOTIFY
16496 +#ifdef CONFIG_AUFS_HFSNOTIFY
16497 +       /* never use fsnotify_add_vfsmount_mark() */
16498 +       struct fsnotify_mark            hn_mark;
16499 +#endif
16500 +       struct inode                    *hn_aufs_inode; /* no get/put */
16501 +#endif
16502 +} ____cacheline_aligned_in_smp;
16503 +
16504 +struct au_hinode {
16505 +       struct inode            *hi_inode;
16506 +       aufs_bindex_t           hi_id;
16507 +#ifdef CONFIG_AUFS_HNOTIFY
16508 +       struct au_hnotify       *hi_notify;
16509 +#endif
16510 +
16511 +       /* reference to the copied-up whiteout with get/put */
16512 +       struct dentry           *hi_whdentry;
16513 +};
16514 +
16515 +/* ig_flags */
16516 +#define AuIG_HALF_REFRESHED            1
16517 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16518 +#define au_ig_fset(flags, name) \
16519 +       do { (flags) |= AuIG_##name; } while (0)
16520 +#define au_ig_fclr(flags, name) \
16521 +       do { (flags) &= ~AuIG_##name; } while (0)
16522 +
16523 +struct au_iigen {
16524 +       spinlock_t      ig_spin;
16525 +       __u32           ig_generation, ig_flags;
16526 +};
16527 +
16528 +struct au_vdir;
16529 +struct au_iinfo {
16530 +       struct au_iigen         ii_generation;
16531 +       struct super_block      *ii_hsb1;       /* no get/put */
16532 +
16533 +       struct au_rwsem         ii_rwsem;
16534 +       aufs_bindex_t           ii_bstart, ii_bend;
16535 +       __u32                   ii_higen;
16536 +       struct au_hinode        *ii_hinode;
16537 +       struct au_vdir          *ii_vdir;
16538 +};
16539 +
16540 +struct au_icntnr {
16541 +       struct au_iinfo iinfo;
16542 +       struct inode vfs_inode;
16543 +} ____cacheline_aligned_in_smp;
16544 +
16545 +/* au_pin flags */
16546 +#define AuPin_DI_LOCKED                1
16547 +#define AuPin_MNT_WRITE                (1 << 1)
16548 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16549 +#define au_fset_pin(flags, name) \
16550 +       do { (flags) |= AuPin_##name; } while (0)
16551 +#define au_fclr_pin(flags, name) \
16552 +       do { (flags) &= ~AuPin_##name; } while (0)
16553 +
16554 +struct au_pin {
16555 +       /* input */
16556 +       struct dentry *dentry;
16557 +       unsigned int udba;
16558 +       unsigned char lsc_di, lsc_hi, flags;
16559 +       aufs_bindex_t bindex;
16560 +
16561 +       /* output */
16562 +       struct dentry *parent;
16563 +       struct au_hinode *hdir;
16564 +       struct vfsmount *h_mnt;
16565 +
16566 +       /* temporary unlock/relock for copyup */
16567 +       struct dentry *h_dentry, *h_parent;
16568 +       struct au_branch *br;
16569 +       struct task_struct *task;
16570 +};
16571 +
16572 +void au_pin_hdir_unlock(struct au_pin *p);
16573 +int au_pin_hdir_lock(struct au_pin *p);
16574 +int au_pin_hdir_relock(struct au_pin *p);
16575 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
16576 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16577 +void au_pin_hdir_release(struct au_pin *p);
16578 +
16579 +/* ---------------------------------------------------------------------- */
16580 +
16581 +static inline struct au_iinfo *au_ii(struct inode *inode)
16582 +{
16583 +       struct au_iinfo *iinfo;
16584 +
16585 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16586 +       if (iinfo->ii_hinode)
16587 +               return iinfo;
16588 +       return NULL; /* debugging bad_inode case */
16589 +}
16590 +
16591 +/* ---------------------------------------------------------------------- */
16592 +
16593 +/* inode.c */
16594 +struct inode *au_igrab(struct inode *inode);
16595 +void au_refresh_iop(struct inode *inode, int force_getattr);
16596 +int au_refresh_hinode_self(struct inode *inode);
16597 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16598 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16599 +          unsigned int d_type, ino_t *ino);
16600 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16601 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16602 +              struct inode *inode);
16603 +int au_test_h_perm(struct inode *h_inode, int mask);
16604 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16605 +
16606 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16607 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16608 +{
16609 +#ifdef CONFIG_AUFS_SHWH
16610 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16611 +#else
16612 +       return 0;
16613 +#endif
16614 +}
16615 +
16616 +/* i_op.c */
16617 +enum {
16618 +       AuIop_SYMLINK,
16619 +       AuIop_DIR,
16620 +       AuIop_OTHER,
16621 +       AuIop_Last
16622 +};
16623 +extern struct inode_operations aufs_iop[AuIop_Last],
16624 +       aufs_iop_nogetattr[AuIop_Last];
16625 +
16626 +/* au_wr_dir flags */
16627 +#define AuWrDir_ADD_ENTRY      1
16628 +#define AuWrDir_ISDIR          (1 << 1)
16629 +#define AuWrDir_TMPFILE                (1 << 2)
16630 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16631 +#define au_fset_wrdir(flags, name) \
16632 +       do { (flags) |= AuWrDir_##name; } while (0)
16633 +#define au_fclr_wrdir(flags, name) \
16634 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16635 +
16636 +struct au_wr_dir_args {
16637 +       aufs_bindex_t force_btgt;
16638 +       unsigned char flags;
16639 +};
16640 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16641 +             struct au_wr_dir_args *args);
16642 +
16643 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16644 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16645 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16646 +                unsigned int udba, unsigned char flags);
16647 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16648 +          unsigned int udba, unsigned char flags) __must_check;
16649 +int au_do_pin(struct au_pin *pin) __must_check;
16650 +void au_unpin(struct au_pin *pin);
16651 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16652 +
16653 +#define AuIcpup_DID_CPUP       1
16654 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16655 +#define au_fset_icpup(flags, name) \
16656 +       do { (flags) |= AuIcpup_##name; } while (0)
16657 +#define au_fclr_icpup(flags, name) \
16658 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16659 +
16660 +struct au_icpup_args {
16661 +       unsigned char flags;
16662 +       unsigned char pin_flags;
16663 +       aufs_bindex_t btgt;
16664 +       unsigned int udba;
16665 +       struct au_pin pin;
16666 +       struct path h_path;
16667 +       struct inode *h_inode;
16668 +};
16669 +
16670 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16671 +                    struct au_icpup_args *a);
16672 +
16673 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
16674 +
16675 +/* i_op_add.c */
16676 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16677 +              struct dentry *h_parent, int isdir);
16678 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16679 +              dev_t dev);
16680 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16681 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16682 +               bool want_excl);
16683 +struct vfsub_aopen_args;
16684 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16685 +                      struct vfsub_aopen_args *args);
16686 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16687 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16688 +             struct dentry *dentry);
16689 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16690 +
16691 +/* i_op_del.c */
16692 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16693 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16694 +              struct dentry *h_parent, int isdir);
16695 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16696 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16697 +
16698 +/* i_op_ren.c */
16699 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16700 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16701 +               struct inode *dir, struct dentry *dentry);
16702 +
16703 +/* iinfo.c */
16704 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16705 +void au_hiput(struct au_hinode *hinode);
16706 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16707 +                 struct dentry *h_wh);
16708 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16709 +
16710 +/* hinode flags */
16711 +#define AuHi_XINO      1
16712 +#define AuHi_HNOTIFY   (1 << 1)
16713 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16714 +#define au_fset_hi(flags, name) \
16715 +       do { (flags) |= AuHi_##name; } while (0)
16716 +#define au_fclr_hi(flags, name) \
16717 +       do { (flags) &= ~AuHi_##name; } while (0)
16718 +
16719 +#ifndef CONFIG_AUFS_HNOTIFY
16720 +#undef AuHi_HNOTIFY
16721 +#define AuHi_HNOTIFY   0
16722 +#endif
16723 +
16724 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16725 +                  struct inode *h_inode, unsigned int flags);
16726 +
16727 +void au_update_iigen(struct inode *inode, int half);
16728 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16729 +
16730 +void au_icntnr_init_once(void *_c);
16731 +int au_iinfo_init(struct inode *inode);
16732 +void au_iinfo_fin(struct inode *inode);
16733 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
16734 +
16735 +#ifdef CONFIG_PROC_FS
16736 +/* plink.c */
16737 +int au_plink_maint(struct super_block *sb, int flags);
16738 +struct au_sbinfo;
16739 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16740 +int au_plink_maint_enter(struct super_block *sb);
16741 +#ifdef CONFIG_AUFS_DEBUG
16742 +void au_plink_list(struct super_block *sb);
16743 +#else
16744 +AuStubVoid(au_plink_list, struct super_block *sb)
16745 +#endif
16746 +int au_plink_test(struct inode *inode);
16747 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16748 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16749 +                    struct dentry *h_dentry);
16750 +void au_plink_put(struct super_block *sb, int verbose);
16751 +void au_plink_clean(struct super_block *sb, int verbose);
16752 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16753 +#else
16754 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16755 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16756 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16757 +AuStubVoid(au_plink_list, struct super_block *sb);
16758 +AuStubInt0(au_plink_test, struct inode *inode);
16759 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16760 +       struct inode *inode, aufs_bindex_t bindex);
16761 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16762 +          struct dentry *h_dentry);
16763 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16764 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16765 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16766 +#endif /* CONFIG_PROC_FS */
16767 +
16768 +#ifdef CONFIG_AUFS_XATTR
16769 +/* xattr.c */
16770 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16771 +                 unsigned int verbose);
16772 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16773 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
16774 +                     size_t size);
16775 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
16776 +                 size_t size, int flags);
16777 +int aufs_removexattr(struct dentry *dentry, const char *name);
16778 +
16779 +/* void au_xattr_init(struct super_block *sb); */
16780 +#else
16781 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16782 +          int ignore_flags, unsigned int verbose);
16783 +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16784 +#endif
16785 +
16786 +#ifdef CONFIG_FS_POSIX_ACL
16787 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16788 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16789 +#endif
16790 +
16791 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16792 +enum {
16793 +       AU_XATTR_SET,
16794 +       AU_XATTR_REMOVE,
16795 +       AU_ACL_SET
16796 +};
16797 +
16798 +struct au_srxattr {
16799 +       int type;
16800 +       union {
16801 +               struct {
16802 +                       const char      *name;
16803 +                       const void      *value;
16804 +                       size_t          size;
16805 +                       int             flags;
16806 +               } set;
16807 +               struct {
16808 +                       const char      *name;
16809 +               } remove;
16810 +               struct {
16811 +                       struct posix_acl *acl;
16812 +                       int             type;
16813 +               } acl_set;
16814 +       } u;
16815 +};
16816 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
16817 +#endif
16818 +
16819 +/* ---------------------------------------------------------------------- */
16820 +
16821 +/* lock subclass for iinfo */
16822 +enum {
16823 +       AuLsc_II_CHILD,         /* child first */
16824 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16825 +       AuLsc_II_CHILD3,        /* copyup dirs */
16826 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16827 +       AuLsc_II_PARENT2,
16828 +       AuLsc_II_PARENT3,       /* copyup dirs */
16829 +       AuLsc_II_NEW_CHILD
16830 +};
16831 +
16832 +/*
16833 + * ii_read_lock_child, ii_write_lock_child,
16834 + * ii_read_lock_child2, ii_write_lock_child2,
16835 + * ii_read_lock_child3, ii_write_lock_child3,
16836 + * ii_read_lock_parent, ii_write_lock_parent,
16837 + * ii_read_lock_parent2, ii_write_lock_parent2,
16838 + * ii_read_lock_parent3, ii_write_lock_parent3,
16839 + * ii_read_lock_new_child, ii_write_lock_new_child,
16840 + */
16841 +#define AuReadLockFunc(name, lsc) \
16842 +static inline void ii_read_lock_##name(struct inode *i) \
16843 +{ \
16844 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16845 +}
16846 +
16847 +#define AuWriteLockFunc(name, lsc) \
16848 +static inline void ii_write_lock_##name(struct inode *i) \
16849 +{ \
16850 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16851 +}
16852 +
16853 +#define AuRWLockFuncs(name, lsc) \
16854 +       AuReadLockFunc(name, lsc) \
16855 +       AuWriteLockFunc(name, lsc)
16856 +
16857 +AuRWLockFuncs(child, CHILD);
16858 +AuRWLockFuncs(child2, CHILD2);
16859 +AuRWLockFuncs(child3, CHILD3);
16860 +AuRWLockFuncs(parent, PARENT);
16861 +AuRWLockFuncs(parent2, PARENT2);
16862 +AuRWLockFuncs(parent3, PARENT3);
16863 +AuRWLockFuncs(new_child, NEW_CHILD);
16864 +
16865 +#undef AuReadLockFunc
16866 +#undef AuWriteLockFunc
16867 +#undef AuRWLockFuncs
16868 +
16869 +/*
16870 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
16871 + */
16872 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
16873 +
16874 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16875 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16876 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
16877 +
16878 +/* ---------------------------------------------------------------------- */
16879 +
16880 +static inline void au_icntnr_init(struct au_icntnr *c)
16881 +{
16882 +#ifdef CONFIG_AUFS_DEBUG
16883 +       c->vfs_inode.i_mode = 0;
16884 +#endif
16885 +}
16886 +
16887 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
16888 +{
16889 +       unsigned int gen;
16890 +       struct au_iinfo *iinfo;
16891 +       struct au_iigen *iigen;
16892 +
16893 +       iinfo = au_ii(inode);
16894 +       iigen = &iinfo->ii_generation;
16895 +       spin_lock(&iigen->ig_spin);
16896 +       if (igflags)
16897 +               *igflags = iigen->ig_flags;
16898 +       gen = iigen->ig_generation;
16899 +       spin_unlock(&iigen->ig_spin);
16900 +
16901 +       return gen;
16902 +}
16903 +
16904 +/* tiny test for inode number */
16905 +/* tmpfs generation is too rough */
16906 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16907 +{
16908 +       struct au_iinfo *iinfo;
16909 +
16910 +       iinfo = au_ii(inode);
16911 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
16912 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
16913 +                && iinfo->ii_higen == h_inode->i_generation);
16914 +}
16915 +
16916 +static inline void au_iigen_dec(struct inode *inode)
16917 +{
16918 +       struct au_iinfo *iinfo;
16919 +       struct au_iigen *iigen;
16920 +
16921 +       iinfo = au_ii(inode);
16922 +       iigen = &iinfo->ii_generation;
16923 +       spin_lock(&iigen->ig_spin);
16924 +       iigen->ig_generation--;
16925 +       spin_unlock(&iigen->ig_spin);
16926 +}
16927 +
16928 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
16929 +{
16930 +       int err;
16931 +
16932 +       err = 0;
16933 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
16934 +               err = -EIO;
16935 +
16936 +       return err;
16937 +}
16938 +
16939 +/* ---------------------------------------------------------------------- */
16940 +
16941 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
16942 +                                       aufs_bindex_t bindex)
16943 +{
16944 +       IiMustAnyLock(inode);
16945 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
16946 +}
16947 +
16948 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
16949 +{
16950 +       IiMustAnyLock(inode);
16951 +       return au_ii(inode)->ii_bstart;
16952 +}
16953 +
16954 +static inline aufs_bindex_t au_ibend(struct inode *inode)
16955 +{
16956 +       IiMustAnyLock(inode);
16957 +       return au_ii(inode)->ii_bend;
16958 +}
16959 +
16960 +static inline struct au_vdir *au_ivdir(struct inode *inode)
16961 +{
16962 +       IiMustAnyLock(inode);
16963 +       return au_ii(inode)->ii_vdir;
16964 +}
16965 +
16966 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
16967 +{
16968 +       IiMustAnyLock(inode);
16969 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
16970 +}
16971 +
16972 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
16973 +{
16974 +       IiMustWriteLock(inode);
16975 +       au_ii(inode)->ii_bstart = bindex;
16976 +}
16977 +
16978 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
16979 +{
16980 +       IiMustWriteLock(inode);
16981 +       au_ii(inode)->ii_bend = bindex;
16982 +}
16983 +
16984 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
16985 +{
16986 +       IiMustWriteLock(inode);
16987 +       au_ii(inode)->ii_vdir = vdir;
16988 +}
16989 +
16990 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
16991 +{
16992 +       IiMustAnyLock(inode);
16993 +       return au_ii(inode)->ii_hinode + bindex;
16994 +}
16995 +
16996 +/* ---------------------------------------------------------------------- */
16997 +
16998 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
16999 +{
17000 +       if (pin)
17001 +               return pin->parent;
17002 +       return NULL;
17003 +}
17004 +
17005 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
17006 +{
17007 +       if (pin && pin->hdir)
17008 +               return pin->hdir->hi_inode;
17009 +       return NULL;
17010 +}
17011 +
17012 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
17013 +{
17014 +       if (pin)
17015 +               return pin->hdir;
17016 +       return NULL;
17017 +}
17018 +
17019 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
17020 +{
17021 +       if (pin)
17022 +               pin->dentry = dentry;
17023 +}
17024 +
17025 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
17026 +                                          unsigned char lflag)
17027 +{
17028 +       if (pin) {
17029 +               if (lflag)
17030 +                       au_fset_pin(pin->flags, DI_LOCKED);
17031 +               else
17032 +                       au_fclr_pin(pin->flags, DI_LOCKED);
17033 +       }
17034 +}
17035 +
17036 +#if 0 /* reserved */
17037 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
17038 +{
17039 +       if (pin) {
17040 +               dput(pin->parent);
17041 +               pin->parent = dget(parent);
17042 +       }
17043 +}
17044 +#endif
17045 +
17046 +/* ---------------------------------------------------------------------- */
17047 +
17048 +struct au_branch;
17049 +#ifdef CONFIG_AUFS_HNOTIFY
17050 +struct au_hnotify_op {
17051 +       void (*ctl)(struct au_hinode *hinode, int do_set);
17052 +       int (*alloc)(struct au_hinode *hinode);
17053 +
17054 +       /*
17055 +        * if it returns true, the the caller should free hinode->hi_notify,
17056 +        * otherwise ->free() frees it.
17057 +        */
17058 +       int (*free)(struct au_hinode *hinode,
17059 +                   struct au_hnotify *hn) __must_check;
17060 +
17061 +       void (*fin)(void);
17062 +       int (*init)(void);
17063 +
17064 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
17065 +       void (*fin_br)(struct au_branch *br);
17066 +       int (*init_br)(struct au_branch *br, int perm);
17067 +};
17068 +
17069 +/* hnotify.c */
17070 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
17071 +void au_hn_free(struct au_hinode *hinode);
17072 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
17073 +void au_hn_reset(struct inode *inode, unsigned int flags);
17074 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17075 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
17076 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
17077 +int au_hnotify_init_br(struct au_branch *br, int perm);
17078 +void au_hnotify_fin_br(struct au_branch *br);
17079 +int __init au_hnotify_init(void);
17080 +void au_hnotify_fin(void);
17081 +
17082 +/* hfsnotify.c */
17083 +extern const struct au_hnotify_op au_hnotify_op;
17084 +
17085 +static inline
17086 +void au_hn_init(struct au_hinode *hinode)
17087 +{
17088 +       hinode->hi_notify = NULL;
17089 +}
17090 +
17091 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
17092 +{
17093 +       return hinode->hi_notify;
17094 +}
17095 +
17096 +#else
17097 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
17098 +       struct au_hinode *hinode __maybe_unused,
17099 +       struct inode *inode __maybe_unused)
17100 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
17101 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
17102 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
17103 +          int do_set __maybe_unused)
17104 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
17105 +          unsigned int flags __maybe_unused)
17106 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
17107 +          struct au_branch *br __maybe_unused,
17108 +          int perm __maybe_unused)
17109 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
17110 +          int perm __maybe_unused)
17111 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
17112 +AuStubInt0(__init au_hnotify_init, void)
17113 +AuStubVoid(au_hnotify_fin, void)
17114 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
17115 +#endif /* CONFIG_AUFS_HNOTIFY */
17116 +
17117 +static inline void au_hn_suspend(struct au_hinode *hdir)
17118 +{
17119 +       au_hn_ctl(hdir, /*do_set*/0);
17120 +}
17121 +
17122 +static inline void au_hn_resume(struct au_hinode *hdir)
17123 +{
17124 +       au_hn_ctl(hdir, /*do_set*/1);
17125 +}
17126 +
17127 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
17128 +{
17129 +       inode_lock(hdir->hi_inode);
17130 +       au_hn_suspend(hdir);
17131 +}
17132 +
17133 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
17134 +                                         unsigned int sc __maybe_unused)
17135 +{
17136 +       inode_lock_nested(hdir->hi_inode, sc);
17137 +       au_hn_suspend(hdir);
17138 +}
17139 +
17140 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
17141 +{
17142 +       au_hn_resume(hdir);
17143 +       inode_unlock(hdir->hi_inode);
17144 +}
17145 +
17146 +#endif /* __KERNEL__ */
17147 +#endif /* __AUFS_INODE_H__ */
17148 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
17149 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
17150 +++ linux/fs/aufs/ioctl.c       2016-04-24 18:32:51.393686895 +0200
17151 @@ -0,0 +1,219 @@
17152 +/*
17153 + * Copyright (C) 2005-2016 Junjiro R. Okajima
17154 + *
17155 + * This program, aufs is free software; you can redistribute it and/or modify
17156 + * it under the terms of the GNU General Public License as published by
17157 + * the Free Software Foundation; either version 2 of the License, or
17158 + * (at your option) any later version.
17159 + *
17160 + * This program is distributed in the hope that it will be useful,
17161 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17162 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17163 + * GNU General Public License for more details.
17164 + *
17165 + * You should have received a copy of the GNU General Public License
17166 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17167 + */
17168 +
17169 +/*
17170 + * ioctl
17171 + * plink-management and readdir in userspace.
17172 + * assist the pathconf(3) wrapper library.
17173 + * move-down
17174 + * File-based Hierarchical Storage Management.
17175 + */
17176 +
17177 +#include <linux/compat.h>
17178 +#include <linux/file.h>
17179 +#include "aufs.h"
17180 +
17181 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
17182 +{
17183 +       int err, fd;
17184 +       aufs_bindex_t wbi, bindex, bend;
17185 +       struct file *h_file;
17186 +       struct super_block *sb;
17187 +       struct dentry *root;
17188 +       struct au_branch *br;
17189 +       struct aufs_wbr_fd wbrfd = {
17190 +               .oflags = au_dir_roflags,
17191 +               .brid   = -1
17192 +       };
17193 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17194 +               | O_NOATIME | O_CLOEXEC;
17195 +
17196 +       AuDebugOn(wbrfd.oflags & ~valid);
17197 +
17198 +       if (arg) {
17199 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17200 +               if (unlikely(err)) {
17201 +                       err = -EFAULT;
17202 +                       goto out;
17203 +               }
17204 +
17205 +               err = -EINVAL;
17206 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17207 +               wbrfd.oflags |= au_dir_roflags;
17208 +               AuDbg("0%o\n", wbrfd.oflags);
17209 +               if (unlikely(wbrfd.oflags & ~valid))
17210 +                       goto out;
17211 +       }
17212 +
17213 +       fd = get_unused_fd_flags(0);
17214 +       err = fd;
17215 +       if (unlikely(fd < 0))
17216 +               goto out;
17217 +
17218 +       h_file = ERR_PTR(-EINVAL);
17219 +       wbi = 0;
17220 +       br = NULL;
17221 +       sb = path->dentry->d_sb;
17222 +       root = sb->s_root;
17223 +       aufs_read_lock(root, AuLock_IR);
17224 +       bend = au_sbend(sb);
17225 +       if (wbrfd.brid >= 0) {
17226 +               wbi = au_br_index(sb, wbrfd.brid);
17227 +               if (unlikely(wbi < 0 || wbi > bend))
17228 +                       goto out_unlock;
17229 +       }
17230 +
17231 +       h_file = ERR_PTR(-ENOENT);
17232 +       br = au_sbr(sb, wbi);
17233 +       if (!au_br_writable(br->br_perm)) {
17234 +               if (arg)
17235 +                       goto out_unlock;
17236 +
17237 +               bindex = wbi + 1;
17238 +               wbi = -1;
17239 +               for (; bindex <= bend; bindex++) {
17240 +                       br = au_sbr(sb, bindex);
17241 +                       if (au_br_writable(br->br_perm)) {
17242 +                               wbi = bindex;
17243 +                               br = au_sbr(sb, wbi);
17244 +                               break;
17245 +                       }
17246 +               }
17247 +       }
17248 +       AuDbg("wbi %d\n", wbi);
17249 +       if (wbi >= 0)
17250 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17251 +                                  /*force_wr*/0);
17252 +
17253 +out_unlock:
17254 +       aufs_read_unlock(root, AuLock_IR);
17255 +       err = PTR_ERR(h_file);
17256 +       if (IS_ERR(h_file))
17257 +               goto out_fd;
17258 +
17259 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
17260 +       fd_install(fd, h_file);
17261 +       err = fd;
17262 +       goto out; /* success */
17263 +
17264 +out_fd:
17265 +       put_unused_fd(fd);
17266 +out:
17267 +       AuTraceErr(err);
17268 +       return err;
17269 +}
17270 +
17271 +/* ---------------------------------------------------------------------- */
17272 +
17273 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17274 +{
17275 +       long err;
17276 +       struct dentry *dentry;
17277 +
17278 +       switch (cmd) {
17279 +       case AUFS_CTL_RDU:
17280 +       case AUFS_CTL_RDU_INO:
17281 +               err = au_rdu_ioctl(file, cmd, arg);
17282 +               break;
17283 +
17284 +       case AUFS_CTL_WBR_FD:
17285 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17286 +               break;
17287 +
17288 +       case AUFS_CTL_IBUSY:
17289 +               err = au_ibusy_ioctl(file, arg);
17290 +               break;
17291 +
17292 +       case AUFS_CTL_BRINFO:
17293 +               err = au_brinfo_ioctl(file, arg);
17294 +               break;
17295 +
17296 +       case AUFS_CTL_FHSM_FD:
17297 +               dentry = file->f_path.dentry;
17298 +               if (IS_ROOT(dentry))
17299 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17300 +               else
17301 +                       err = -ENOTTY;
17302 +               break;
17303 +
17304 +       default:
17305 +               /* do not call the lower */
17306 +               AuDbg("0x%x\n", cmd);
17307 +               err = -ENOTTY;
17308 +       }
17309 +
17310 +       AuTraceErr(err);
17311 +       return err;
17312 +}
17313 +
17314 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17315 +{
17316 +       long err;
17317 +
17318 +       switch (cmd) {
17319 +       case AUFS_CTL_MVDOWN:
17320 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17321 +               break;
17322 +
17323 +       case AUFS_CTL_WBR_FD:
17324 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17325 +               break;
17326 +
17327 +       default:
17328 +               /* do not call the lower */
17329 +               AuDbg("0x%x\n", cmd);
17330 +               err = -ENOTTY;
17331 +       }
17332 +
17333 +       AuTraceErr(err);
17334 +       return err;
17335 +}
17336 +
17337 +#ifdef CONFIG_COMPAT
17338 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17339 +                          unsigned long arg)
17340 +{
17341 +       long err;
17342 +
17343 +       switch (cmd) {
17344 +       case AUFS_CTL_RDU:
17345 +       case AUFS_CTL_RDU_INO:
17346 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17347 +               break;
17348 +
17349 +       case AUFS_CTL_IBUSY:
17350 +               err = au_ibusy_compat_ioctl(file, arg);
17351 +               break;
17352 +
17353 +       case AUFS_CTL_BRINFO:
17354 +               err = au_brinfo_compat_ioctl(file, arg);
17355 +               break;
17356 +
17357 +       default:
17358 +               err = aufs_ioctl_dir(file, cmd, arg);
17359 +       }
17360 +
17361 +       AuTraceErr(err);
17362 +       return err;
17363 +}
17364 +
17365 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17366 +                             unsigned long arg)
17367 +{
17368 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17369 +}
17370 +#endif
17371 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17372 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17373 +++ linux/fs/aufs/i_op_add.c    2016-04-24 18:32:51.393686895 +0200
17374 @@ -0,0 +1,932 @@
17375 +/*
17376 + * Copyright (C) 2005-2016 Junjiro R. Okajima
17377 + *
17378 + * This program, aufs is free software; you can redistribute it and/or modify
17379 + * it under the terms of the GNU General Public License as published by
17380 + * the Free Software Foundation; either version 2 of the License, or
17381 + * (at your option) any later version.
17382 + *
17383 + * This program is distributed in the hope that it will be useful,
17384 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17385 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17386 + * GNU General Public License for more details.
17387 + *
17388 + * You should have received a copy of the GNU General Public License
17389 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17390 + */
17391 +
17392 +/*
17393 + * inode operations (add entry)
17394 + */
17395 +
17396 +#include "aufs.h"
17397 +
17398 +/*
17399 + * final procedure of adding a new entry, except link(2).
17400 + * remove whiteout, instantiate, copyup the parent dir's times and size
17401 + * and update version.
17402 + * if it failed, re-create the removed whiteout.
17403 + */
17404 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17405 +                 struct dentry *wh_dentry, struct dentry *dentry)
17406 +{
17407 +       int err, rerr;
17408 +       aufs_bindex_t bwh;
17409 +       struct path h_path;
17410 +       struct super_block *sb;
17411 +       struct inode *inode, *h_dir;
17412 +       struct dentry *wh;
17413 +
17414 +       bwh = -1;
17415 +       sb = dir->i_sb;
17416 +       if (wh_dentry) {
17417 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
17418 +               IMustLock(h_dir);
17419 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17420 +               bwh = au_dbwh(dentry);
17421 +               h_path.dentry = wh_dentry;
17422 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17423 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17424 +                                         dentry);
17425 +               if (unlikely(err))
17426 +                       goto out;
17427 +       }
17428 +
17429 +       inode = au_new_inode(dentry, /*must_new*/1);
17430 +       if (!IS_ERR(inode)) {
17431 +               d_instantiate(dentry, inode);
17432 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
17433 +               IMustLock(dir);
17434 +               au_dir_ts(dir, bindex);
17435 +               dir->i_version++;
17436 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17437 +               return 0; /* success */
17438 +       }
17439 +
17440 +       err = PTR_ERR(inode);
17441 +       if (!wh_dentry)
17442 +               goto out;
17443 +
17444 +       /* revert */
17445 +       /* dir inode is locked */
17446 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17447 +       rerr = PTR_ERR(wh);
17448 +       if (IS_ERR(wh)) {
17449 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17450 +                       dentry, err, rerr);
17451 +               err = -EIO;
17452 +       } else
17453 +               dput(wh);
17454 +
17455 +out:
17456 +       return err;
17457 +}
17458 +
17459 +static int au_d_may_add(struct dentry *dentry)
17460 +{
17461 +       int err;
17462 +
17463 +       err = 0;
17464 +       if (unlikely(d_unhashed(dentry)))
17465 +               err = -ENOENT;
17466 +       if (unlikely(d_really_is_positive(dentry)))
17467 +               err = -EEXIST;
17468 +       return err;
17469 +}
17470 +
17471 +/*
17472 + * simple tests for the adding inode operations.
17473 + * following the checks in vfs, plus the parent-child relationship.
17474 + */
17475 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17476 +              struct dentry *h_parent, int isdir)
17477 +{
17478 +       int err;
17479 +       umode_t h_mode;
17480 +       struct dentry *h_dentry;
17481 +       struct inode *h_inode;
17482 +
17483 +       err = -ENAMETOOLONG;
17484 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17485 +               goto out;
17486 +
17487 +       h_dentry = au_h_dptr(dentry, bindex);
17488 +       if (d_really_is_negative(dentry)) {
17489 +               err = -EEXIST;
17490 +               if (unlikely(d_is_positive(h_dentry)))
17491 +                       goto out;
17492 +       } else {
17493 +               /* rename(2) case */
17494 +               err = -EIO;
17495 +               if (unlikely(d_is_negative(h_dentry)))
17496 +                       goto out;
17497 +               h_inode = d_inode(h_dentry);
17498 +               if (unlikely(!h_inode->i_nlink))
17499 +                       goto out;
17500 +
17501 +               h_mode = h_inode->i_mode;
17502 +               if (!isdir) {
17503 +                       err = -EISDIR;
17504 +                       if (unlikely(S_ISDIR(h_mode)))
17505 +                               goto out;
17506 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17507 +                       err = -ENOTDIR;
17508 +                       goto out;
17509 +               }
17510 +       }
17511 +
17512 +       err = 0;
17513 +       /* expected parent dir is locked */
17514 +       if (unlikely(h_parent != h_dentry->d_parent))
17515 +               err = -EIO;
17516 +
17517 +out:
17518 +       AuTraceErr(err);
17519 +       return err;
17520 +}
17521 +
17522 +/*
17523 + * initial procedure of adding a new entry.
17524 + * prepare writable branch and the parent dir, lock it,
17525 + * and lookup whiteout for the new entry.
17526 + */
17527 +static struct dentry*
17528 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17529 +                 struct dentry *src_dentry, struct au_pin *pin,
17530 +                 struct au_wr_dir_args *wr_dir_args)
17531 +{
17532 +       struct dentry *wh_dentry, *h_parent;
17533 +       struct super_block *sb;
17534 +       struct au_branch *br;
17535 +       int err;
17536 +       unsigned int udba;
17537 +       aufs_bindex_t bcpup;
17538 +
17539 +       AuDbg("%pd\n", dentry);
17540 +
17541 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17542 +       bcpup = err;
17543 +       wh_dentry = ERR_PTR(err);
17544 +       if (unlikely(err < 0))
17545 +               goto out;
17546 +
17547 +       sb = dentry->d_sb;
17548 +       udba = au_opt_udba(sb);
17549 +       err = au_pin(pin, dentry, bcpup, udba,
17550 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17551 +       wh_dentry = ERR_PTR(err);
17552 +       if (unlikely(err))
17553 +               goto out;
17554 +
17555 +       h_parent = au_pinned_h_parent(pin);
17556 +       if (udba != AuOpt_UDBA_NONE
17557 +           && au_dbstart(dentry) == bcpup)
17558 +               err = au_may_add(dentry, bcpup, h_parent,
17559 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17560 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17561 +               err = -ENAMETOOLONG;
17562 +       wh_dentry = ERR_PTR(err);
17563 +       if (unlikely(err))
17564 +               goto out_unpin;
17565 +
17566 +       br = au_sbr(sb, bcpup);
17567 +       if (dt) {
17568 +               struct path tmp = {
17569 +                       .dentry = h_parent,
17570 +                       .mnt    = au_br_mnt(br)
17571 +               };
17572 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17573 +       }
17574 +
17575 +       wh_dentry = NULL;
17576 +       if (bcpup != au_dbwh(dentry))
17577 +               goto out; /* success */
17578 +
17579 +       /*
17580 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17581 +        * would not be able to removed in the future. So we don't allow such
17582 +        * name here and we don't handle ENAMETOOLONG differently here.
17583 +        */
17584 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17585 +
17586 +out_unpin:
17587 +       if (IS_ERR(wh_dentry))
17588 +               au_unpin(pin);
17589 +out:
17590 +       return wh_dentry;
17591 +}
17592 +
17593 +/* ---------------------------------------------------------------------- */
17594 +
17595 +enum { Mknod, Symlink, Creat };
17596 +struct simple_arg {
17597 +       int type;
17598 +       union {
17599 +               struct {
17600 +                       umode_t                 mode;
17601 +                       bool                    want_excl;
17602 +                       bool                    try_aopen;
17603 +                       struct vfsub_aopen_args *aopen;
17604 +               } c;
17605 +               struct {
17606 +                       const char *symname;
17607 +               } s;
17608 +               struct {
17609 +                       umode_t mode;
17610 +                       dev_t dev;
17611 +               } m;
17612 +       } u;
17613 +};
17614 +
17615 +static int add_simple(struct inode *dir, struct dentry *dentry,
17616 +                     struct simple_arg *arg)
17617 +{
17618 +       int err, rerr;
17619 +       aufs_bindex_t bstart;
17620 +       unsigned char created;
17621 +       const unsigned char try_aopen
17622 +               = (arg->type == Creat && arg->u.c.try_aopen);
17623 +       struct dentry *wh_dentry, *parent;
17624 +       struct inode *h_dir;
17625 +       struct super_block *sb;
17626 +       struct au_branch *br;
17627 +       /* to reuduce stack size */
17628 +       struct {
17629 +               struct au_dtime dt;
17630 +               struct au_pin pin;
17631 +               struct path h_path;
17632 +               struct au_wr_dir_args wr_dir_args;
17633 +       } *a;
17634 +
17635 +       AuDbg("%pd\n", dentry);
17636 +       IMustLock(dir);
17637 +
17638 +       err = -ENOMEM;
17639 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17640 +       if (unlikely(!a))
17641 +               goto out;
17642 +       a->wr_dir_args.force_btgt = -1;
17643 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17644 +
17645 +       parent = dentry->d_parent; /* dir inode is locked */
17646 +       if (!try_aopen) {
17647 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17648 +               if (unlikely(err))
17649 +                       goto out_free;
17650 +       }
17651 +       err = au_d_may_add(dentry);
17652 +       if (unlikely(err))
17653 +               goto out_unlock;
17654 +       if (!try_aopen)
17655 +               di_write_lock_parent(parent);
17656 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17657 +                                     &a->pin, &a->wr_dir_args);
17658 +       err = PTR_ERR(wh_dentry);
17659 +       if (IS_ERR(wh_dentry))
17660 +               goto out_parent;
17661 +
17662 +       bstart = au_dbstart(dentry);
17663 +       sb = dentry->d_sb;
17664 +       br = au_sbr(sb, bstart);
17665 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17666 +       a->h_path.mnt = au_br_mnt(br);
17667 +       h_dir = au_pinned_h_dir(&a->pin);
17668 +       switch (arg->type) {
17669 +       case Creat:
17670 +               err = 0;
17671 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17672 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17673 +                                          arg->u.c.want_excl);
17674 +               else
17675 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17676 +                                               arg->u.c.aopen, br);
17677 +               break;
17678 +       case Symlink:
17679 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17680 +               break;
17681 +       case Mknod:
17682 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17683 +                                 arg->u.m.dev);
17684 +               break;
17685 +       default:
17686 +               BUG();
17687 +       }
17688 +       created = !err;
17689 +       if (!err)
17690 +               err = epilog(dir, bstart, wh_dentry, dentry);
17691 +
17692 +       /* revert */
17693 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
17694 +               /* no delegation since it is just created */
17695 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17696 +                                   /*force*/0);
17697 +               if (rerr) {
17698 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17699 +                               dentry, err, rerr);
17700 +                       err = -EIO;
17701 +               }
17702 +               au_dtime_revert(&a->dt);
17703 +       }
17704 +
17705 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
17706 +               *arg->u.c.aopen->opened |= FILE_CREATED;
17707 +
17708 +       au_unpin(&a->pin);
17709 +       dput(wh_dentry);
17710 +
17711 +out_parent:
17712 +       if (!try_aopen)
17713 +               di_write_unlock(parent);
17714 +out_unlock:
17715 +       if (unlikely(err)) {
17716 +               au_update_dbstart(dentry);
17717 +               d_drop(dentry);
17718 +       }
17719 +       if (!try_aopen)
17720 +               aufs_read_unlock(dentry, AuLock_DW);
17721 +out_free:
17722 +       kfree(a);
17723 +out:
17724 +       return err;
17725 +}
17726 +
17727 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17728 +              dev_t dev)
17729 +{
17730 +       struct simple_arg arg = {
17731 +               .type = Mknod,
17732 +               .u.m = {
17733 +                       .mode   = mode,
17734 +                       .dev    = dev
17735 +               }
17736 +       };
17737 +       return add_simple(dir, dentry, &arg);
17738 +}
17739 +
17740 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17741 +{
17742 +       struct simple_arg arg = {
17743 +               .type = Symlink,
17744 +               .u.s.symname = symname
17745 +       };
17746 +       return add_simple(dir, dentry, &arg);
17747 +}
17748 +
17749 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17750 +               bool want_excl)
17751 +{
17752 +       struct simple_arg arg = {
17753 +               .type = Creat,
17754 +               .u.c = {
17755 +                       .mode           = mode,
17756 +                       .want_excl      = want_excl
17757 +               }
17758 +       };
17759 +       return add_simple(dir, dentry, &arg);
17760 +}
17761 +
17762 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17763 +                      struct vfsub_aopen_args *aopen_args)
17764 +{
17765 +       struct simple_arg arg = {
17766 +               .type = Creat,
17767 +               .u.c = {
17768 +                       .mode           = aopen_args->create_mode,
17769 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
17770 +                       .try_aopen      = true,
17771 +                       .aopen          = aopen_args
17772 +               }
17773 +       };
17774 +       return add_simple(dir, dentry, &arg);
17775 +}
17776 +
17777 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17778 +{
17779 +       int err;
17780 +       aufs_bindex_t bindex;
17781 +       struct super_block *sb;
17782 +       struct dentry *parent, *h_parent, *h_dentry;
17783 +       struct inode *h_dir, *inode;
17784 +       struct vfsmount *h_mnt;
17785 +       struct au_wr_dir_args wr_dir_args = {
17786 +               .force_btgt     = -1,
17787 +               .flags          = AuWrDir_TMPFILE
17788 +       };
17789 +
17790 +       /* copy-up may happen */
17791 +       inode_lock(dir);
17792 +
17793 +       sb = dir->i_sb;
17794 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17795 +       if (unlikely(err))
17796 +               goto out;
17797 +
17798 +       err = au_di_init(dentry);
17799 +       if (unlikely(err))
17800 +               goto out_si;
17801 +
17802 +       err = -EBUSY;
17803 +       parent = d_find_any_alias(dir);
17804 +       AuDebugOn(!parent);
17805 +       di_write_lock_parent(parent);
17806 +       if (unlikely(d_inode(parent) != dir))
17807 +               goto out_parent;
17808 +
17809 +       err = au_digen_test(parent, au_sigen(sb));
17810 +       if (unlikely(err))
17811 +               goto out_parent;
17812 +
17813 +       bindex = au_dbstart(parent);
17814 +       au_set_dbstart(dentry, bindex);
17815 +       au_set_dbend(dentry, bindex);
17816 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17817 +       bindex = err;
17818 +       if (unlikely(err < 0))
17819 +               goto out_parent;
17820 +
17821 +       err = -EOPNOTSUPP;
17822 +       h_dir = au_h_iptr(dir, bindex);
17823 +       if (unlikely(!h_dir->i_op->tmpfile))
17824 +               goto out_parent;
17825 +
17826 +       h_mnt = au_sbr_mnt(sb, bindex);
17827 +       err = vfsub_mnt_want_write(h_mnt);
17828 +       if (unlikely(err))
17829 +               goto out_parent;
17830 +
17831 +       h_parent = au_h_dptr(parent, bindex);
17832 +       err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
17833 +       if (unlikely(err))
17834 +               goto out_mnt;
17835 +
17836 +       err = -ENOMEM;
17837 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17838 +       if (unlikely(!h_dentry))
17839 +               goto out_mnt;
17840 +
17841 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17842 +       if (unlikely(err))
17843 +               goto out_dentry;
17844 +
17845 +       au_set_dbstart(dentry, bindex);
17846 +       au_set_dbend(dentry, bindex);
17847 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
17848 +       inode = au_new_inode(dentry, /*must_new*/1);
17849 +       if (IS_ERR(inode)) {
17850 +               err = PTR_ERR(inode);
17851 +               au_set_h_dptr(dentry, bindex, NULL);
17852 +               au_set_dbstart(dentry, -1);
17853 +               au_set_dbend(dentry, -1);
17854 +       } else {
17855 +               if (!inode->i_nlink)
17856 +                       set_nlink(inode, 1);
17857 +               d_tmpfile(dentry, inode);
17858 +               au_di(dentry)->di_tmpfile = 1;
17859 +
17860 +               /* update without i_mutex */
17861 +               if (au_ibstart(dir) == au_dbstart(dentry))
17862 +                       au_cpup_attr_timesizes(dir);
17863 +       }
17864 +
17865 +out_dentry:
17866 +       dput(h_dentry);
17867 +out_mnt:
17868 +       vfsub_mnt_drop_write(h_mnt);
17869 +out_parent:
17870 +       di_write_unlock(parent);
17871 +       dput(parent);
17872 +       di_write_unlock(dentry);
17873 +       if (!err)
17874 +#if 0
17875 +               /* verbose coding for lock class name */
17876 +               au_rw_class(&au_di(dentry)->di_rwsem,
17877 +                           au_lc_key + AuLcNonDir_DIINFO);
17878 +#else
17879 +               ;
17880 +#endif
17881 +       else {
17882 +               au_di_fin(dentry);
17883 +               dentry->d_fsdata = NULL;
17884 +       }
17885 +out_si:
17886 +       si_read_unlock(sb);
17887 +out:
17888 +       inode_unlock(dir);
17889 +       return err;
17890 +}
17891 +
17892 +/* ---------------------------------------------------------------------- */
17893 +
17894 +struct au_link_args {
17895 +       aufs_bindex_t bdst, bsrc;
17896 +       struct au_pin pin;
17897 +       struct path h_path;
17898 +       struct dentry *src_parent, *parent;
17899 +};
17900 +
17901 +static int au_cpup_before_link(struct dentry *src_dentry,
17902 +                              struct au_link_args *a)
17903 +{
17904 +       int err;
17905 +       struct dentry *h_src_dentry;
17906 +       struct au_cp_generic cpg = {
17907 +               .dentry = src_dentry,
17908 +               .bdst   = a->bdst,
17909 +               .bsrc   = a->bsrc,
17910 +               .len    = -1,
17911 +               .pin    = &a->pin,
17912 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
17913 +       };
17914 +
17915 +       di_read_lock_parent(a->src_parent, AuLock_IR);
17916 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
17917 +       if (unlikely(err))
17918 +               goto out;
17919 +
17920 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
17921 +       err = au_pin(&a->pin, src_dentry, a->bdst,
17922 +                    au_opt_udba(src_dentry->d_sb),
17923 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17924 +       if (unlikely(err))
17925 +               goto out;
17926 +
17927 +       err = au_sio_cpup_simple(&cpg);
17928 +       au_unpin(&a->pin);
17929 +
17930 +out:
17931 +       di_read_unlock(a->src_parent, AuLock_IR);
17932 +       return err;
17933 +}
17934 +
17935 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
17936 +                          struct au_link_args *a)
17937 +{
17938 +       int err;
17939 +       unsigned char plink;
17940 +       aufs_bindex_t bend;
17941 +       struct dentry *h_src_dentry;
17942 +       struct inode *h_inode, *inode, *delegated;
17943 +       struct super_block *sb;
17944 +       struct file *h_file;
17945 +
17946 +       plink = 0;
17947 +       h_inode = NULL;
17948 +       sb = src_dentry->d_sb;
17949 +       inode = d_inode(src_dentry);
17950 +       if (au_ibstart(inode) <= a->bdst)
17951 +               h_inode = au_h_iptr(inode, a->bdst);
17952 +       if (!h_inode || !h_inode->i_nlink) {
17953 +               /* copyup src_dentry as the name of dentry. */
17954 +               bend = au_dbend(dentry);
17955 +               if (bend < a->bsrc)
17956 +                       au_set_dbend(dentry, a->bsrc);
17957 +               au_set_h_dptr(dentry, a->bsrc,
17958 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
17959 +               dget(a->h_path.dentry);
17960 +               au_set_h_dptr(dentry, a->bdst, NULL);
17961 +               AuDbg("temporary d_inode...\n");
17962 +               spin_lock(&dentry->d_lock);
17963 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
17964 +               spin_unlock(&dentry->d_lock);
17965 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
17966 +               if (IS_ERR(h_file))
17967 +                       err = PTR_ERR(h_file);
17968 +               else {
17969 +                       struct au_cp_generic cpg = {
17970 +                               .dentry = dentry,
17971 +                               .bdst   = a->bdst,
17972 +                               .bsrc   = -1,
17973 +                               .len    = -1,
17974 +                               .pin    = &a->pin,
17975 +                               .flags  = AuCpup_KEEPLINO
17976 +                       };
17977 +                       err = au_sio_cpup_simple(&cpg);
17978 +                       au_h_open_post(dentry, a->bsrc, h_file);
17979 +                       if (!err) {
17980 +                               dput(a->h_path.dentry);
17981 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17982 +                       } else
17983 +                               au_set_h_dptr(dentry, a->bdst,
17984 +                                             a->h_path.dentry);
17985 +               }
17986 +               spin_lock(&dentry->d_lock);
17987 +               dentry->d_inode = NULL; /* restore */
17988 +               spin_unlock(&dentry->d_lock);
17989 +               AuDbg("temporary d_inode...done\n");
17990 +               au_set_h_dptr(dentry, a->bsrc, NULL);
17991 +               au_set_dbend(dentry, bend);
17992 +       } else {
17993 +               /* the inode of src_dentry already exists on a.bdst branch */
17994 +               h_src_dentry = d_find_alias(h_inode);
17995 +               if (!h_src_dentry && au_plink_test(inode)) {
17996 +                       plink = 1;
17997 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
17998 +                       err = PTR_ERR(h_src_dentry);
17999 +                       if (IS_ERR(h_src_dentry))
18000 +                               goto out;
18001 +
18002 +                       if (unlikely(d_is_negative(h_src_dentry))) {
18003 +                               dput(h_src_dentry);
18004 +                               h_src_dentry = NULL;
18005 +                       }
18006 +
18007 +               }
18008 +               if (h_src_dentry) {
18009 +                       delegated = NULL;
18010 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18011 +                                        &a->h_path, &delegated);
18012 +                       if (unlikely(err == -EWOULDBLOCK)) {
18013 +                               pr_warn("cannot retry for NFSv4 delegation"
18014 +                                       " for an internal link\n");
18015 +                               iput(delegated);
18016 +                       }
18017 +                       dput(h_src_dentry);
18018 +               } else {
18019 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
18020 +                               h_inode->i_ino, a->bdst);
18021 +                       err = -EIO;
18022 +               }
18023 +       }
18024 +
18025 +       if (!err && !plink)
18026 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
18027 +
18028 +out:
18029 +       AuTraceErr(err);
18030 +       return err;
18031 +}
18032 +
18033 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18034 +             struct dentry *dentry)
18035 +{
18036 +       int err, rerr;
18037 +       struct au_dtime dt;
18038 +       struct au_link_args *a;
18039 +       struct dentry *wh_dentry, *h_src_dentry;
18040 +       struct inode *inode, *delegated;
18041 +       struct super_block *sb;
18042 +       struct au_wr_dir_args wr_dir_args = {
18043 +               /* .force_btgt  = -1, */
18044 +               .flags          = AuWrDir_ADD_ENTRY
18045 +       };
18046 +
18047 +       IMustLock(dir);
18048 +       inode = d_inode(src_dentry);
18049 +       IMustLock(inode);
18050 +
18051 +       err = -ENOMEM;
18052 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18053 +       if (unlikely(!a))
18054 +               goto out;
18055 +
18056 +       a->parent = dentry->d_parent; /* dir inode is locked */
18057 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
18058 +                                       AuLock_NOPLM | AuLock_GEN);
18059 +       if (unlikely(err))
18060 +               goto out_kfree;
18061 +       err = au_d_linkable(src_dentry);
18062 +       if (unlikely(err))
18063 +               goto out_unlock;
18064 +       err = au_d_may_add(dentry);
18065 +       if (unlikely(err))
18066 +               goto out_unlock;
18067 +
18068 +       a->src_parent = dget_parent(src_dentry);
18069 +       wr_dir_args.force_btgt = au_ibstart(inode);
18070 +
18071 +       di_write_lock_parent(a->parent);
18072 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
18073 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
18074 +                                     &wr_dir_args);
18075 +       err = PTR_ERR(wh_dentry);
18076 +       if (IS_ERR(wh_dentry))
18077 +               goto out_parent;
18078 +
18079 +       err = 0;
18080 +       sb = dentry->d_sb;
18081 +       a->bdst = au_dbstart(dentry);
18082 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18083 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
18084 +       a->bsrc = au_ibstart(inode);
18085 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18086 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
18087 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
18088 +       if (!h_src_dentry) {
18089 +               a->bsrc = au_dbstart(src_dentry);
18090 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18091 +               AuDebugOn(!h_src_dentry);
18092 +       } else if (IS_ERR(h_src_dentry)) {
18093 +               err = PTR_ERR(h_src_dentry);
18094 +               goto out_parent;
18095 +       }
18096 +
18097 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
18098 +               if (a->bdst < a->bsrc
18099 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
18100 +                       err = au_cpup_or_link(src_dentry, dentry, a);
18101 +               else {
18102 +                       delegated = NULL;
18103 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18104 +                                        &a->h_path, &delegated);
18105 +                       if (unlikely(err == -EWOULDBLOCK)) {
18106 +                               pr_warn("cannot retry for NFSv4 delegation"
18107 +                                       " for an internal link\n");
18108 +                               iput(delegated);
18109 +                       }
18110 +               }
18111 +               dput(h_src_dentry);
18112 +       } else {
18113 +               /*
18114 +                * copyup src_dentry to the branch we process,
18115 +                * and then link(2) to it.
18116 +                */
18117 +               dput(h_src_dentry);
18118 +               if (a->bdst < a->bsrc
18119 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
18120 +                       au_unpin(&a->pin);
18121 +                       di_write_unlock(a->parent);
18122 +                       err = au_cpup_before_link(src_dentry, a);
18123 +                       di_write_lock_parent(a->parent);
18124 +                       if (!err)
18125 +                               err = au_pin(&a->pin, dentry, a->bdst,
18126 +                                            au_opt_udba(sb),
18127 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18128 +                       if (unlikely(err))
18129 +                               goto out_wh;
18130 +               }
18131 +               if (!err) {
18132 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
18133 +                       err = -ENOENT;
18134 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
18135 +                               delegated = NULL;
18136 +                               err = vfsub_link(h_src_dentry,
18137 +                                                au_pinned_h_dir(&a->pin),
18138 +                                                &a->h_path, &delegated);
18139 +                               if (unlikely(err == -EWOULDBLOCK)) {
18140 +                                       pr_warn("cannot retry"
18141 +                                               " for NFSv4 delegation"
18142 +                                               " for an internal link\n");
18143 +                                       iput(delegated);
18144 +                               }
18145 +                       }
18146 +               }
18147 +       }
18148 +       if (unlikely(err))
18149 +               goto out_unpin;
18150 +
18151 +       if (wh_dentry) {
18152 +               a->h_path.dentry = wh_dentry;
18153 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
18154 +                                         dentry);
18155 +               if (unlikely(err))
18156 +                       goto out_revert;
18157 +       }
18158 +
18159 +       au_dir_ts(dir, a->bdst);
18160 +       dir->i_version++;
18161 +       inc_nlink(inode);
18162 +       inode->i_ctime = dir->i_ctime;
18163 +       d_instantiate(dentry, au_igrab(inode));
18164 +       if (d_unhashed(a->h_path.dentry))
18165 +               /* some filesystem calls d_drop() */
18166 +               d_drop(dentry);
18167 +       /* some filesystems consume an inode even hardlink */
18168 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
18169 +       goto out_unpin; /* success */
18170 +
18171 +out_revert:
18172 +       /* no delegation since it is just created */
18173 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18174 +                           /*delegated*/NULL, /*force*/0);
18175 +       if (unlikely(rerr)) {
18176 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
18177 +               err = -EIO;
18178 +       }
18179 +       au_dtime_revert(&dt);
18180 +out_unpin:
18181 +       au_unpin(&a->pin);
18182 +out_wh:
18183 +       dput(wh_dentry);
18184 +out_parent:
18185 +       di_write_unlock(a->parent);
18186 +       dput(a->src_parent);
18187 +out_unlock:
18188 +       if (unlikely(err)) {
18189 +               au_update_dbstart(dentry);
18190 +               d_drop(dentry);
18191 +       }
18192 +       aufs_read_and_write_unlock2(dentry, src_dentry);
18193 +out_kfree:
18194 +       kfree(a);
18195 +out:
18196 +       AuTraceErr(err);
18197 +       return err;
18198 +}
18199 +
18200 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18201 +{
18202 +       int err, rerr;
18203 +       aufs_bindex_t bindex;
18204 +       unsigned char diropq;
18205 +       struct path h_path;
18206 +       struct dentry *wh_dentry, *parent, *opq_dentry;
18207 +       struct inode *h_inode;
18208 +       struct super_block *sb;
18209 +       struct {
18210 +               struct au_pin pin;
18211 +               struct au_dtime dt;
18212 +       } *a; /* reduce the stack usage */
18213 +       struct au_wr_dir_args wr_dir_args = {
18214 +               .force_btgt     = -1,
18215 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18216 +       };
18217 +
18218 +       IMustLock(dir);
18219 +
18220 +       err = -ENOMEM;
18221 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18222 +       if (unlikely(!a))
18223 +               goto out;
18224 +
18225 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18226 +       if (unlikely(err))
18227 +               goto out_free;
18228 +       err = au_d_may_add(dentry);
18229 +       if (unlikely(err))
18230 +               goto out_unlock;
18231 +
18232 +       parent = dentry->d_parent; /* dir inode is locked */
18233 +       di_write_lock_parent(parent);
18234 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18235 +                                     &a->pin, &wr_dir_args);
18236 +       err = PTR_ERR(wh_dentry);
18237 +       if (IS_ERR(wh_dentry))
18238 +               goto out_parent;
18239 +
18240 +       sb = dentry->d_sb;
18241 +       bindex = au_dbstart(dentry);
18242 +       h_path.dentry = au_h_dptr(dentry, bindex);
18243 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18244 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18245 +       if (unlikely(err))
18246 +               goto out_unpin;
18247 +
18248 +       /* make the dir opaque */
18249 +       diropq = 0;
18250 +       h_inode = d_inode(h_path.dentry);
18251 +       if (wh_dentry
18252 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18253 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18254 +               opq_dentry = au_diropq_create(dentry, bindex);
18255 +               inode_unlock(h_inode);
18256 +               err = PTR_ERR(opq_dentry);
18257 +               if (IS_ERR(opq_dentry))
18258 +                       goto out_dir;
18259 +               dput(opq_dentry);
18260 +               diropq = 1;
18261 +       }
18262 +
18263 +       err = epilog(dir, bindex, wh_dentry, dentry);
18264 +       if (!err) {
18265 +               inc_nlink(dir);
18266 +               goto out_unpin; /* success */
18267 +       }
18268 +
18269 +       /* revert */
18270 +       if (diropq) {
18271 +               AuLabel(revert opq);
18272 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18273 +               rerr = au_diropq_remove(dentry, bindex);
18274 +               inode_unlock(h_inode);
18275 +               if (rerr) {
18276 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18277 +                               dentry, err, rerr);
18278 +                       err = -EIO;
18279 +               }
18280 +       }
18281 +
18282 +out_dir:
18283 +       AuLabel(revert dir);
18284 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18285 +       if (rerr) {
18286 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18287 +                       dentry, err, rerr);
18288 +               err = -EIO;
18289 +       }
18290 +       au_dtime_revert(&a->dt);
18291 +out_unpin:
18292 +       au_unpin(&a->pin);
18293 +       dput(wh_dentry);
18294 +out_parent:
18295 +       di_write_unlock(parent);
18296 +out_unlock:
18297 +       if (unlikely(err)) {
18298 +               au_update_dbstart(dentry);
18299 +               d_drop(dentry);
18300 +       }
18301 +       aufs_read_unlock(dentry, AuLock_DW);
18302 +out_free:
18303 +       kfree(a);
18304 +out:
18305 +       return err;
18306 +}
18307 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18308 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18309 +++ linux/fs/aufs/i_op.c        2016-04-24 18:32:51.393686895 +0200
18310 @@ -0,0 +1,1419 @@
18311 +/*
18312 + * Copyright (C) 2005-2016 Junjiro R. Okajima
18313 + *
18314 + * This program, aufs is free software; you can redistribute it and/or modify
18315 + * it under the terms of the GNU General Public License as published by
18316 + * the Free Software Foundation; either version 2 of the License, or
18317 + * (at your option) any later version.
18318 + *
18319 + * This program is distributed in the hope that it will be useful,
18320 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18321 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18322 + * GNU General Public License for more details.
18323 + *
18324 + * You should have received a copy of the GNU General Public License
18325 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18326 + */
18327 +
18328 +/*
18329 + * inode operations (except add/del/rename)
18330 + */
18331 +
18332 +#include <linux/device_cgroup.h>
18333 +#include <linux/fs_stack.h>
18334 +#include <linux/namei.h>
18335 +#include <linux/security.h>
18336 +#include "aufs.h"
18337 +
18338 +static int h_permission(struct inode *h_inode, int mask,
18339 +                       struct path *h_path, int brperm)
18340 +{
18341 +       int err;
18342 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18343 +
18344 +       err = -EACCES;
18345 +       if ((write_mask && IS_IMMUTABLE(h_inode))
18346 +           || ((mask & MAY_EXEC)
18347 +               && S_ISREG(h_inode->i_mode)
18348 +               && (path_noexec(h_path)
18349 +                   || !(h_inode->i_mode & S_IXUGO))))
18350 +               goto out;
18351 +
18352 +       /*
18353 +        * - skip the lower fs test in the case of write to ro branch.
18354 +        * - nfs dir permission write check is optimized, but a policy for
18355 +        *   link/rename requires a real check.
18356 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18357 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18358 +        */
18359 +       if ((write_mask && !au_br_writable(brperm))
18360 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18361 +               && write_mask && !(mask & MAY_READ))
18362 +           || !h_inode->i_op->permission) {
18363 +               /* AuLabel(generic_permission); */
18364 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18365 +               err = generic_permission(h_inode, mask);
18366 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18367 +                       err = h_inode->i_op->permission(h_inode, mask);
18368 +               AuTraceErr(err);
18369 +       } else {
18370 +               /* AuLabel(h_inode->permission); */
18371 +               err = h_inode->i_op->permission(h_inode, mask);
18372 +               AuTraceErr(err);
18373 +       }
18374 +
18375 +       if (!err)
18376 +               err = devcgroup_inode_permission(h_inode, mask);
18377 +       if (!err)
18378 +               err = security_inode_permission(h_inode, mask);
18379 +
18380 +#if 0
18381 +       if (!err) {
18382 +               /* todo: do we need to call ima_path_check()? */
18383 +               struct path h_path = {
18384 +                       .dentry =
18385 +                       .mnt    = h_mnt
18386 +               };
18387 +               err = ima_path_check(&h_path,
18388 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18389 +                                    IMA_COUNT_LEAVE);
18390 +       }
18391 +#endif
18392 +
18393 +out:
18394 +       return err;
18395 +}
18396 +
18397 +static int aufs_permission(struct inode *inode, int mask)
18398 +{
18399 +       int err;
18400 +       aufs_bindex_t bindex, bend;
18401 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18402 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18403 +       struct inode *h_inode;
18404 +       struct super_block *sb;
18405 +       struct au_branch *br;
18406 +
18407 +       /* todo: support rcu-walk? */
18408 +       if (mask & MAY_NOT_BLOCK)
18409 +               return -ECHILD;
18410 +
18411 +       sb = inode->i_sb;
18412 +       si_read_lock(sb, AuLock_FLUSH);
18413 +       ii_read_lock_child(inode);
18414 +#if 0
18415 +       err = au_iigen_test(inode, au_sigen(sb));
18416 +       if (unlikely(err))
18417 +               goto out;
18418 +#endif
18419 +
18420 +       if (!isdir
18421 +           || write_mask
18422 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18423 +               err = au_busy_or_stale();
18424 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
18425 +               if (unlikely(!h_inode
18426 +                            || (h_inode->i_mode & S_IFMT)
18427 +                            != (inode->i_mode & S_IFMT)))
18428 +                       goto out;
18429 +
18430 +               err = 0;
18431 +               bindex = au_ibstart(inode);
18432 +               br = au_sbr(sb, bindex);
18433 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
18434 +               if (write_mask
18435 +                   && !err
18436 +                   && !special_file(h_inode->i_mode)) {
18437 +                       /* test whether the upper writable branch exists */
18438 +                       err = -EROFS;
18439 +                       for (; bindex >= 0; bindex--)
18440 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18441 +                                       err = 0;
18442 +                                       break;
18443 +                               }
18444 +               }
18445 +               goto out;
18446 +       }
18447 +
18448 +       /* non-write to dir */
18449 +       err = 0;
18450 +       bend = au_ibend(inode);
18451 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
18452 +               h_inode = au_h_iptr(inode, bindex);
18453 +               if (h_inode) {
18454 +                       err = au_busy_or_stale();
18455 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18456 +                               break;
18457 +
18458 +                       br = au_sbr(sb, bindex);
18459 +                       err = h_permission(h_inode, mask, &br->br_path,
18460 +                                          br->br_perm);
18461 +               }
18462 +       }
18463 +
18464 +out:
18465 +       ii_read_unlock(inode);
18466 +       si_read_unlock(sb);
18467 +       return err;
18468 +}
18469 +
18470 +/* ---------------------------------------------------------------------- */
18471 +
18472 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18473 +                                 unsigned int flags)
18474 +{
18475 +       struct dentry *ret, *parent;
18476 +       struct inode *inode;
18477 +       struct super_block *sb;
18478 +       int err, npositive;
18479 +
18480 +       IMustLock(dir);
18481 +
18482 +       /* todo: support rcu-walk? */
18483 +       ret = ERR_PTR(-ECHILD);
18484 +       if (flags & LOOKUP_RCU)
18485 +               goto out;
18486 +
18487 +       ret = ERR_PTR(-ENAMETOOLONG);
18488 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18489 +               goto out;
18490 +
18491 +       sb = dir->i_sb;
18492 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18493 +       ret = ERR_PTR(err);
18494 +       if (unlikely(err))
18495 +               goto out;
18496 +
18497 +       err = au_di_init(dentry);
18498 +       ret = ERR_PTR(err);
18499 +       if (unlikely(err))
18500 +               goto out_si;
18501 +
18502 +       inode = NULL;
18503 +       npositive = 0; /* suppress a warning */
18504 +       parent = dentry->d_parent; /* dir inode is locked */
18505 +       di_read_lock_parent(parent, AuLock_IR);
18506 +       err = au_alive_dir(parent);
18507 +       if (!err)
18508 +               err = au_digen_test(parent, au_sigen(sb));
18509 +       if (!err) {
18510 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
18511 +                                          /*type*/0);
18512 +               err = npositive;
18513 +       }
18514 +       di_read_unlock(parent, AuLock_IR);
18515 +       ret = ERR_PTR(err);
18516 +       if (unlikely(err < 0))
18517 +               goto out_unlock;
18518 +
18519 +       if (npositive) {
18520 +               inode = au_new_inode(dentry, /*must_new*/0);
18521 +               if (IS_ERR(inode)) {
18522 +                       ret = (void *)inode;
18523 +                       inode = NULL;
18524 +                       goto out_unlock;
18525 +               }
18526 +       }
18527 +
18528 +       if (inode)
18529 +               atomic_inc(&inode->i_count);
18530 +       ret = d_splice_alias(inode, dentry);
18531 +#if 0
18532 +       if (unlikely(d_need_lookup(dentry))) {
18533 +               spin_lock(&dentry->d_lock);
18534 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18535 +               spin_unlock(&dentry->d_lock);
18536 +       } else
18537 +#endif
18538 +       if (inode) {
18539 +               if (!IS_ERR(ret)) {
18540 +                       iput(inode);
18541 +                       if (ret && ret != dentry)
18542 +                               ii_write_unlock(inode);
18543 +               } else {
18544 +                       ii_write_unlock(inode);
18545 +                       iput(inode);
18546 +                       inode = NULL;
18547 +               }
18548 +       }
18549 +
18550 +out_unlock:
18551 +       di_write_unlock(dentry);
18552 +       if (inode) {
18553 +               /* verbose coding for lock class name */
18554 +               if (unlikely(S_ISLNK(inode->i_mode)))
18555 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18556 +                                   au_lc_key + AuLcSymlink_DIINFO);
18557 +               else if (unlikely(S_ISDIR(inode->i_mode)))
18558 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18559 +                                   au_lc_key + AuLcDir_DIINFO);
18560 +               else /* likely */
18561 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18562 +                                   au_lc_key + AuLcNonDir_DIINFO);
18563 +       }
18564 +out_si:
18565 +       si_read_unlock(sb);
18566 +out:
18567 +       return ret;
18568 +}
18569 +
18570 +/* ---------------------------------------------------------------------- */
18571 +
18572 +struct aopen_node {
18573 +       struct hlist_node hlist;
18574 +       struct file *file, *h_file;
18575 +};
18576 +
18577 +static int au_do_aopen(struct inode *inode, struct file *file)
18578 +{
18579 +       struct au_sphlhead *aopen;
18580 +       struct aopen_node *node;
18581 +       struct au_do_open_args args = {
18582 +               .no_lock        = 1,
18583 +               .open           = au_do_open_nondir
18584 +       };
18585 +
18586 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18587 +       spin_lock(&aopen->spin);
18588 +       hlist_for_each_entry(node, &aopen->head, hlist)
18589 +               if (node->file == file) {
18590 +                       args.h_file = node->h_file;
18591 +                       break;
18592 +               }
18593 +       spin_unlock(&aopen->spin);
18594 +       /* AuDebugOn(!args.h_file); */
18595 +
18596 +       return au_do_open(file, &args);
18597 +}
18598 +
18599 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18600 +                           struct file *file, unsigned int open_flag,
18601 +                           umode_t create_mode, int *opened)
18602 +{
18603 +       int err, h_opened = *opened;
18604 +       struct dentry *parent;
18605 +       struct dentry *d;
18606 +       struct au_sphlhead *aopen;
18607 +       struct vfsub_aopen_args args = {
18608 +               .open_flag      = open_flag,
18609 +               .create_mode    = create_mode,
18610 +               .opened         = &h_opened
18611 +       };
18612 +       struct aopen_node aopen_node = {
18613 +               .file   = file
18614 +       };
18615 +
18616 +       IMustLock(dir);
18617 +       AuDbg("open_flag 0x%x\n", open_flag);
18618 +       AuDbgDentry(dentry);
18619 +
18620 +       err = 0;
18621 +       if (!au_di(dentry)) {
18622 +               d = aufs_lookup(dir, dentry, /*flags*/0);
18623 +               if (IS_ERR(d)) {
18624 +                       err = PTR_ERR(d);
18625 +                       goto out;
18626 +               } else if (d) {
18627 +                       /*
18628 +                        * obsoleted dentry found.
18629 +                        * another error will be returned later.
18630 +                        */
18631 +                       d_drop(d);
18632 +                       dput(d);
18633 +                       AuDbgDentry(d);
18634 +               }
18635 +               AuDbgDentry(dentry);
18636 +       }
18637 +
18638 +       if (d_is_positive(dentry)
18639 +           || d_unhashed(dentry)
18640 +           || d_unlinked(dentry)
18641 +           || !(open_flag & O_CREAT))
18642 +               goto out_no_open;
18643 +
18644 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18645 +       if (unlikely(err))
18646 +               goto out;
18647 +
18648 +       parent = dentry->d_parent;      /* dir is locked */
18649 +       di_write_lock_parent(parent);
18650 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
18651 +       if (unlikely(err))
18652 +               goto out_unlock;
18653 +
18654 +       AuDbgDentry(dentry);
18655 +       if (d_is_positive(dentry))
18656 +               goto out_unlock;
18657 +
18658 +       args.file = get_empty_filp();
18659 +       err = PTR_ERR(args.file);
18660 +       if (IS_ERR(args.file))
18661 +               goto out_unlock;
18662 +
18663 +       args.file->f_flags = file->f_flags;
18664 +       err = au_aopen_or_create(dir, dentry, &args);
18665 +       AuTraceErr(err);
18666 +       AuDbgFile(args.file);
18667 +       if (unlikely(err < 0)) {
18668 +               if (h_opened & FILE_OPENED)
18669 +                       fput(args.file);
18670 +               else
18671 +                       put_filp(args.file);
18672 +               goto out_unlock;
18673 +       }
18674 +
18675 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18676 +       *opened |= FILE_CREATED;
18677 +       if (h_opened & FILE_OPENED)
18678 +               aopen_node.h_file = args.file;
18679 +       else {
18680 +               put_filp(args.file);
18681 +               args.file = NULL;
18682 +       }
18683 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18684 +       au_sphl_add(&aopen_node.hlist, aopen);
18685 +       err = finish_open(file, dentry, au_do_aopen, opened);
18686 +       au_sphl_del(&aopen_node.hlist, aopen);
18687 +       AuTraceErr(err);
18688 +       AuDbgFile(file);
18689 +       if (aopen_node.h_file)
18690 +               fput(aopen_node.h_file);
18691 +
18692 +out_unlock:
18693 +       di_write_unlock(parent);
18694 +       aufs_read_unlock(dentry, AuLock_DW);
18695 +       AuDbgDentry(dentry);
18696 +       if (unlikely(err))
18697 +               goto out;
18698 +out_no_open:
18699 +       if (!err && !(*opened & FILE_CREATED)) {
18700 +               AuLabel(out_no_open);
18701 +               dget(dentry);
18702 +               err = finish_no_open(file, dentry);
18703 +       }
18704 +out:
18705 +       AuDbg("%pd%s%s\n", dentry,
18706 +             (*opened & FILE_CREATED) ? " created" : "",
18707 +             (*opened & FILE_OPENED) ? " opened" : "");
18708 +       AuTraceErr(err);
18709 +       return err;
18710 +}
18711 +
18712 +
18713 +/* ---------------------------------------------------------------------- */
18714 +
18715 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18716 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18717 +                         aufs_bindex_t bstart)
18718 +{
18719 +       int err;
18720 +       struct dentry *h_parent;
18721 +       struct inode *h_dir;
18722 +
18723 +       if (add_entry)
18724 +               IMustLock(d_inode(parent));
18725 +       else
18726 +               di_write_lock_parent(parent);
18727 +
18728 +       err = 0;
18729 +       if (!au_h_dptr(parent, bcpup)) {
18730 +               if (bstart > bcpup)
18731 +                       err = au_cpup_dirs(dentry, bcpup);
18732 +               else if (bstart < bcpup)
18733 +                       err = au_cpdown_dirs(dentry, bcpup);
18734 +               else
18735 +                       BUG();
18736 +       }
18737 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18738 +               h_parent = au_h_dptr(parent, bcpup);
18739 +               h_dir = d_inode(h_parent);
18740 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
18741 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18742 +               /* todo: no unlock here */
18743 +               inode_unlock(h_dir);
18744 +
18745 +               AuDbg("bcpup %d\n", bcpup);
18746 +               if (!err) {
18747 +                       if (d_really_is_negative(dentry))
18748 +                               au_set_h_dptr(dentry, bstart, NULL);
18749 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18750 +               }
18751 +       }
18752 +
18753 +       if (!add_entry)
18754 +               di_write_unlock(parent);
18755 +       if (!err)
18756 +               err = bcpup; /* success */
18757 +
18758 +       AuTraceErr(err);
18759 +       return err;
18760 +}
18761 +
18762 +/*
18763 + * decide the branch and the parent dir where we will create a new entry.
18764 + * returns new bindex or an error.
18765 + * copyup the parent dir if needed.
18766 + */
18767 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18768 +             struct au_wr_dir_args *args)
18769 +{
18770 +       int err;
18771 +       unsigned int flags;
18772 +       aufs_bindex_t bcpup, bstart, src_bstart;
18773 +       const unsigned char add_entry
18774 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18775 +               | au_ftest_wrdir(args->flags, TMPFILE);
18776 +       struct super_block *sb;
18777 +       struct dentry *parent;
18778 +       struct au_sbinfo *sbinfo;
18779 +
18780 +       sb = dentry->d_sb;
18781 +       sbinfo = au_sbi(sb);
18782 +       parent = dget_parent(dentry);
18783 +       bstart = au_dbstart(dentry);
18784 +       bcpup = bstart;
18785 +       if (args->force_btgt < 0) {
18786 +               if (src_dentry) {
18787 +                       src_bstart = au_dbstart(src_dentry);
18788 +                       if (src_bstart < bstart)
18789 +                               bcpup = src_bstart;
18790 +               } else if (add_entry) {
18791 +                       flags = 0;
18792 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18793 +                               au_fset_wbr(flags, DIR);
18794 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18795 +                       bcpup = err;
18796 +               }
18797 +
18798 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
18799 +                       if (add_entry)
18800 +                               err = AuWbrCopyup(sbinfo, dentry);
18801 +                       else {
18802 +                               if (!IS_ROOT(dentry)) {
18803 +                                       di_read_lock_parent(parent, !AuLock_IR);
18804 +                                       err = AuWbrCopyup(sbinfo, dentry);
18805 +                                       di_read_unlock(parent, !AuLock_IR);
18806 +                               } else
18807 +                                       err = AuWbrCopyup(sbinfo, dentry);
18808 +                       }
18809 +                       bcpup = err;
18810 +                       if (unlikely(err < 0))
18811 +                               goto out;
18812 +               }
18813 +       } else {
18814 +               bcpup = args->force_btgt;
18815 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
18816 +       }
18817 +
18818 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
18819 +       err = bcpup;
18820 +       if (bcpup == bstart)
18821 +               goto out; /* success */
18822 +
18823 +       /* copyup the new parent into the branch we process */
18824 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
18825 +       if (err >= 0) {
18826 +               if (d_really_is_negative(dentry)) {
18827 +                       au_set_h_dptr(dentry, bstart, NULL);
18828 +                       au_set_dbstart(dentry, bcpup);
18829 +                       au_set_dbend(dentry, bcpup);
18830 +               }
18831 +               AuDebugOn(add_entry
18832 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18833 +                         && !au_h_dptr(dentry, bcpup));
18834 +       }
18835 +
18836 +out:
18837 +       dput(parent);
18838 +       return err;
18839 +}
18840 +
18841 +/* ---------------------------------------------------------------------- */
18842 +
18843 +void au_pin_hdir_unlock(struct au_pin *p)
18844 +{
18845 +       if (p->hdir)
18846 +               au_hn_imtx_unlock(p->hdir);
18847 +}
18848 +
18849 +int au_pin_hdir_lock(struct au_pin *p)
18850 +{
18851 +       int err;
18852 +
18853 +       err = 0;
18854 +       if (!p->hdir)
18855 +               goto out;
18856 +
18857 +       /* even if an error happens later, keep this lock */
18858 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
18859 +
18860 +       err = -EBUSY;
18861 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
18862 +               goto out;
18863 +
18864 +       err = 0;
18865 +       if (p->h_dentry)
18866 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18867 +                                 p->h_parent, p->br);
18868 +
18869 +out:
18870 +       return err;
18871 +}
18872 +
18873 +int au_pin_hdir_relock(struct au_pin *p)
18874 +{
18875 +       int err, i;
18876 +       struct inode *h_i;
18877 +       struct dentry *h_d[] = {
18878 +               p->h_dentry,
18879 +               p->h_parent
18880 +       };
18881 +
18882 +       err = au_pin_hdir_lock(p);
18883 +       if (unlikely(err))
18884 +               goto out;
18885 +
18886 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18887 +               if (!h_d[i])
18888 +                       continue;
18889 +               if (d_is_positive(h_d[i])) {
18890 +                       h_i = d_inode(h_d[i]);
18891 +                       err = !h_i->i_nlink;
18892 +               }
18893 +       }
18894 +
18895 +out:
18896 +       return err;
18897 +}
18898 +
18899 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
18900 +{
18901 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
18902 +       p->hdir->hi_inode->i_mutex.owner = task;
18903 +#endif
18904 +}
18905 +
18906 +void au_pin_hdir_acquire_nest(struct au_pin *p)
18907 +{
18908 +       if (p->hdir) {
18909 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
18910 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
18911 +               au_pin_hdir_set_owner(p, current);
18912 +       }
18913 +}
18914 +
18915 +void au_pin_hdir_release(struct au_pin *p)
18916 +{
18917 +       if (p->hdir) {
18918 +               au_pin_hdir_set_owner(p, p->task);
18919 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
18920 +       }
18921 +}
18922 +
18923 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
18924 +{
18925 +       if (pin && pin->parent)
18926 +               return au_h_dptr(pin->parent, pin->bindex);
18927 +       return NULL;
18928 +}
18929 +
18930 +void au_unpin(struct au_pin *p)
18931 +{
18932 +       if (p->hdir)
18933 +               au_pin_hdir_unlock(p);
18934 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
18935 +               vfsub_mnt_drop_write(p->h_mnt);
18936 +       if (!p->hdir)
18937 +               return;
18938 +
18939 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18940 +               di_read_unlock(p->parent, AuLock_IR);
18941 +       iput(p->hdir->hi_inode);
18942 +       dput(p->parent);
18943 +       p->parent = NULL;
18944 +       p->hdir = NULL;
18945 +       p->h_mnt = NULL;
18946 +       /* do not clear p->task */
18947 +}
18948 +
18949 +int au_do_pin(struct au_pin *p)
18950 +{
18951 +       int err;
18952 +       struct super_block *sb;
18953 +       struct inode *h_dir;
18954 +
18955 +       err = 0;
18956 +       sb = p->dentry->d_sb;
18957 +       p->br = au_sbr(sb, p->bindex);
18958 +       if (IS_ROOT(p->dentry)) {
18959 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
18960 +                       p->h_mnt = au_br_mnt(p->br);
18961 +                       err = vfsub_mnt_want_write(p->h_mnt);
18962 +                       if (unlikely(err)) {
18963 +                               au_fclr_pin(p->flags, MNT_WRITE);
18964 +                               goto out_err;
18965 +                       }
18966 +               }
18967 +               goto out;
18968 +       }
18969 +
18970 +       p->h_dentry = NULL;
18971 +       if (p->bindex <= au_dbend(p->dentry))
18972 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
18973 +
18974 +       p->parent = dget_parent(p->dentry);
18975 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18976 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
18977 +
18978 +       h_dir = NULL;
18979 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
18980 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
18981 +       if (p->hdir)
18982 +               h_dir = p->hdir->hi_inode;
18983 +
18984 +       /*
18985 +        * udba case, or
18986 +        * if DI_LOCKED is not set, then p->parent may be different
18987 +        * and h_parent can be NULL.
18988 +        */
18989 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
18990 +               err = -EBUSY;
18991 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
18992 +                       di_read_unlock(p->parent, AuLock_IR);
18993 +               dput(p->parent);
18994 +               p->parent = NULL;
18995 +               goto out_err;
18996 +       }
18997 +
18998 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
18999 +               p->h_mnt = au_br_mnt(p->br);
19000 +               err = vfsub_mnt_want_write(p->h_mnt);
19001 +               if (unlikely(err)) {
19002 +                       au_fclr_pin(p->flags, MNT_WRITE);
19003 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
19004 +                               di_read_unlock(p->parent, AuLock_IR);
19005 +                       dput(p->parent);
19006 +                       p->parent = NULL;
19007 +                       goto out_err;
19008 +               }
19009 +       }
19010 +
19011 +       au_igrab(h_dir);
19012 +       err = au_pin_hdir_lock(p);
19013 +       if (!err)
19014 +               goto out; /* success */
19015 +
19016 +       au_unpin(p);
19017 +
19018 +out_err:
19019 +       pr_err("err %d\n", err);
19020 +       err = au_busy_or_stale();
19021 +out:
19022 +       return err;
19023 +}
19024 +
19025 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
19026 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19027 +                unsigned int udba, unsigned char flags)
19028 +{
19029 +       p->dentry = dentry;
19030 +       p->udba = udba;
19031 +       p->lsc_di = lsc_di;
19032 +       p->lsc_hi = lsc_hi;
19033 +       p->flags = flags;
19034 +       p->bindex = bindex;
19035 +
19036 +       p->parent = NULL;
19037 +       p->hdir = NULL;
19038 +       p->h_mnt = NULL;
19039 +
19040 +       p->h_dentry = NULL;
19041 +       p->h_parent = NULL;
19042 +       p->br = NULL;
19043 +       p->task = current;
19044 +}
19045 +
19046 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19047 +          unsigned int udba, unsigned char flags)
19048 +{
19049 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19050 +                   udba, flags);
19051 +       return au_do_pin(pin);
19052 +}
19053 +
19054 +/* ---------------------------------------------------------------------- */
19055 +
19056 +/*
19057 + * ->setattr() and ->getattr() are called in various cases.
19058 + * chmod, stat: dentry is revalidated.
19059 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19060 + *               unhashed.
19061 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
19062 + */
19063 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
19064 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
19065 +{
19066 +       int err;
19067 +       struct dentry *parent;
19068 +
19069 +       err = 0;
19070 +       if (au_digen_test(dentry, sigen)) {
19071 +               parent = dget_parent(dentry);
19072 +               di_read_lock_parent(parent, AuLock_IR);
19073 +               err = au_refresh_dentry(dentry, parent);
19074 +               di_read_unlock(parent, AuLock_IR);
19075 +               dput(parent);
19076 +       }
19077 +
19078 +       AuTraceErr(err);
19079 +       return err;
19080 +}
19081 +
19082 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19083 +                    struct au_icpup_args *a)
19084 +{
19085 +       int err;
19086 +       loff_t sz;
19087 +       aufs_bindex_t bstart, ibstart;
19088 +       struct dentry *hi_wh, *parent;
19089 +       struct inode *inode;
19090 +       struct au_wr_dir_args wr_dir_args = {
19091 +               .force_btgt     = -1,
19092 +               .flags          = 0
19093 +       };
19094 +
19095 +       if (d_is_dir(dentry))
19096 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
19097 +       /* plink or hi_wh() case */
19098 +       bstart = au_dbstart(dentry);
19099 +       inode = d_inode(dentry);
19100 +       ibstart = au_ibstart(inode);
19101 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
19102 +               wr_dir_args.force_btgt = ibstart;
19103 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19104 +       if (unlikely(err < 0))
19105 +               goto out;
19106 +       a->btgt = err;
19107 +       if (err != bstart)
19108 +               au_fset_icpup(a->flags, DID_CPUP);
19109 +
19110 +       err = 0;
19111 +       a->pin_flags = AuPin_MNT_WRITE;
19112 +       parent = NULL;
19113 +       if (!IS_ROOT(dentry)) {
19114 +               au_fset_pin(a->pin_flags, DI_LOCKED);
19115 +               parent = dget_parent(dentry);
19116 +               di_write_lock_parent(parent);
19117 +       }
19118 +
19119 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19120 +       if (unlikely(err))
19121 +               goto out_parent;
19122 +
19123 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
19124 +       sz = -1;
19125 +       a->h_inode = d_inode(a->h_path.dentry);
19126 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
19127 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19128 +               if (ia->ia_size < i_size_read(a->h_inode))
19129 +                       sz = ia->ia_size;
19130 +               inode_unlock(a->h_inode);
19131 +       }
19132 +
19133 +       hi_wh = NULL;
19134 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
19135 +               hi_wh = au_hi_wh(inode, a->btgt);
19136 +               if (!hi_wh) {
19137 +                       struct au_cp_generic cpg = {
19138 +                               .dentry = dentry,
19139 +                               .bdst   = a->btgt,
19140 +                               .bsrc   = -1,
19141 +                               .len    = sz,
19142 +                               .pin    = &a->pin
19143 +                       };
19144 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
19145 +                       if (unlikely(err))
19146 +                               goto out_unlock;
19147 +                       hi_wh = au_hi_wh(inode, a->btgt);
19148 +                       /* todo: revalidate hi_wh? */
19149 +               }
19150 +       }
19151 +
19152 +       if (parent) {
19153 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19154 +               di_downgrade_lock(parent, AuLock_IR);
19155 +               dput(parent);
19156 +               parent = NULL;
19157 +       }
19158 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
19159 +               goto out; /* success */
19160 +
19161 +       if (!d_unhashed(dentry)) {
19162 +               struct au_cp_generic cpg = {
19163 +                       .dentry = dentry,
19164 +                       .bdst   = a->btgt,
19165 +                       .bsrc   = bstart,
19166 +                       .len    = sz,
19167 +                       .pin    = &a->pin,
19168 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
19169 +               };
19170 +               err = au_sio_cpup_simple(&cpg);
19171 +               if (!err)
19172 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19173 +       } else if (!hi_wh)
19174 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19175 +       else
19176 +               a->h_path.dentry = hi_wh; /* do not dget here */
19177 +
19178 +out_unlock:
19179 +       a->h_inode = d_inode(a->h_path.dentry);
19180 +       if (!err)
19181 +               goto out; /* success */
19182 +       au_unpin(&a->pin);
19183 +out_parent:
19184 +       if (parent) {
19185 +               di_write_unlock(parent);
19186 +               dput(parent);
19187 +       }
19188 +out:
19189 +       if (!err)
19190 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19191 +       return err;
19192 +}
19193 +
19194 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
19195 +{
19196 +       int err;
19197 +       struct inode *inode, *delegated;
19198 +       struct super_block *sb;
19199 +       struct file *file;
19200 +       struct au_icpup_args *a;
19201 +
19202 +       inode = d_inode(dentry);
19203 +       IMustLock(inode);
19204 +
19205 +       err = -ENOMEM;
19206 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19207 +       if (unlikely(!a))
19208 +               goto out;
19209 +
19210 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19211 +               ia->ia_valid &= ~ATTR_MODE;
19212 +
19213 +       file = NULL;
19214 +       sb = dentry->d_sb;
19215 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19216 +       if (unlikely(err))
19217 +               goto out_kfree;
19218 +
19219 +       if (ia->ia_valid & ATTR_FILE) {
19220 +               /* currently ftruncate(2) only */
19221 +               AuDebugOn(!d_is_reg(dentry));
19222 +               file = ia->ia_file;
19223 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
19224 +               if (unlikely(err))
19225 +                       goto out_si;
19226 +               ia->ia_file = au_hf_top(file);
19227 +               a->udba = AuOpt_UDBA_NONE;
19228 +       } else {
19229 +               /* fchmod() doesn't pass ia_file */
19230 +               a->udba = au_opt_udba(sb);
19231 +               di_write_lock_child(dentry);
19232 +               /* no d_unlinked(), to set UDBA_NONE for root */
19233 +               if (d_unhashed(dentry))
19234 +                       a->udba = AuOpt_UDBA_NONE;
19235 +               if (a->udba != AuOpt_UDBA_NONE) {
19236 +                       AuDebugOn(IS_ROOT(dentry));
19237 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19238 +                       if (unlikely(err))
19239 +                               goto out_dentry;
19240 +               }
19241 +       }
19242 +
19243 +       err = au_pin_and_icpup(dentry, ia, a);
19244 +       if (unlikely(err < 0))
19245 +               goto out_dentry;
19246 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19247 +               ia->ia_file = NULL;
19248 +               ia->ia_valid &= ~ATTR_FILE;
19249 +       }
19250 +
19251 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19252 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19253 +           == (ATTR_MODE | ATTR_CTIME)) {
19254 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19255 +               if (unlikely(err))
19256 +                       goto out_unlock;
19257 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19258 +                  && (ia->ia_valid & ATTR_CTIME)) {
19259 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19260 +               if (unlikely(err))
19261 +                       goto out_unlock;
19262 +       }
19263 +
19264 +       if (ia->ia_valid & ATTR_SIZE) {
19265 +               struct file *f;
19266 +
19267 +               if (ia->ia_size < i_size_read(inode))
19268 +                       /* unmap only */
19269 +                       truncate_setsize(inode, ia->ia_size);
19270 +
19271 +               f = NULL;
19272 +               if (ia->ia_valid & ATTR_FILE)
19273 +                       f = ia->ia_file;
19274 +               inode_unlock(a->h_inode);
19275 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19276 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19277 +       } else {
19278 +               delegated = NULL;
19279 +               while (1) {
19280 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19281 +                       if (delegated) {
19282 +                               err = break_deleg_wait(&delegated);
19283 +                               if (!err)
19284 +                                       continue;
19285 +                       }
19286 +                       break;
19287 +               }
19288 +       }
19289 +       /*
19290 +        * regardless aufs 'acl' option setting.
19291 +        * why don't all acl-aware fs call this func from their ->setattr()?
19292 +        */
19293 +       if (!err && (ia->ia_valid & ATTR_MODE))
19294 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
19295 +       if (!err)
19296 +               au_cpup_attr_changeable(inode);
19297 +
19298 +out_unlock:
19299 +       inode_unlock(a->h_inode);
19300 +       au_unpin(&a->pin);
19301 +       if (unlikely(err))
19302 +               au_update_dbstart(dentry);
19303 +out_dentry:
19304 +       di_write_unlock(dentry);
19305 +       if (file) {
19306 +               fi_write_unlock(file);
19307 +               ia->ia_file = file;
19308 +               ia->ia_valid |= ATTR_FILE;
19309 +       }
19310 +out_si:
19311 +       si_read_unlock(sb);
19312 +out_kfree:
19313 +       kfree(a);
19314 +out:
19315 +       AuTraceErr(err);
19316 +       return err;
19317 +}
19318 +
19319 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19320 +static int au_h_path_to_set_attr(struct dentry *dentry,
19321 +                                struct au_icpup_args *a, struct path *h_path)
19322 +{
19323 +       int err;
19324 +       struct super_block *sb;
19325 +
19326 +       sb = dentry->d_sb;
19327 +       a->udba = au_opt_udba(sb);
19328 +       /* no d_unlinked(), to set UDBA_NONE for root */
19329 +       if (d_unhashed(dentry))
19330 +               a->udba = AuOpt_UDBA_NONE;
19331 +       if (a->udba != AuOpt_UDBA_NONE) {
19332 +               AuDebugOn(IS_ROOT(dentry));
19333 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19334 +               if (unlikely(err))
19335 +                       goto out;
19336 +       }
19337 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19338 +       if (unlikely(err < 0))
19339 +               goto out;
19340 +
19341 +       h_path->dentry = a->h_path.dentry;
19342 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19343 +
19344 +out:
19345 +       return err;
19346 +}
19347 +
19348 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
19349 +{
19350 +       int err;
19351 +       struct path h_path;
19352 +       struct super_block *sb;
19353 +       struct au_icpup_args *a;
19354 +       struct inode *inode, *h_inode;
19355 +
19356 +       inode = d_inode(dentry);
19357 +       IMustLock(inode);
19358 +
19359 +       err = -ENOMEM;
19360 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19361 +       if (unlikely(!a))
19362 +               goto out;
19363 +
19364 +       sb = dentry->d_sb;
19365 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19366 +       if (unlikely(err))
19367 +               goto out_kfree;
19368 +
19369 +       h_path.dentry = NULL;   /* silence gcc */
19370 +       di_write_lock_child(dentry);
19371 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19372 +       if (unlikely(err))
19373 +               goto out_di;
19374 +
19375 +       inode_unlock(a->h_inode);
19376 +       switch (arg->type) {
19377 +       case AU_XATTR_SET:
19378 +               err = vfsub_setxattr(h_path.dentry,
19379 +                                    arg->u.set.name, arg->u.set.value,
19380 +                                    arg->u.set.size, arg->u.set.flags);
19381 +               break;
19382 +       case AU_XATTR_REMOVE:
19383 +               err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19384 +               break;
19385 +       case AU_ACL_SET:
19386 +               err = -EOPNOTSUPP;
19387 +               h_inode = d_inode(h_path.dentry);
19388 +               if (h_inode->i_op->set_acl)
19389 +                       err = h_inode->i_op->set_acl(h_inode,
19390 +                                                    arg->u.acl_set.acl,
19391 +                                                    arg->u.acl_set.type);
19392 +               break;
19393 +       }
19394 +       if (!err)
19395 +               au_cpup_attr_timesizes(inode);
19396 +
19397 +       au_unpin(&a->pin);
19398 +       if (unlikely(err))
19399 +               au_update_dbstart(dentry);
19400 +
19401 +out_di:
19402 +       di_write_unlock(dentry);
19403 +       si_read_unlock(sb);
19404 +out_kfree:
19405 +       kfree(a);
19406 +out:
19407 +       AuTraceErr(err);
19408 +       return err;
19409 +}
19410 +#endif
19411 +
19412 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19413 +                            unsigned int nlink)
19414 +{
19415 +       unsigned int n;
19416 +
19417 +       inode->i_mode = st->mode;
19418 +       /* don't i_[ug]id_write() here */
19419 +       inode->i_uid = st->uid;
19420 +       inode->i_gid = st->gid;
19421 +       inode->i_atime = st->atime;
19422 +       inode->i_mtime = st->mtime;
19423 +       inode->i_ctime = st->ctime;
19424 +
19425 +       au_cpup_attr_nlink(inode, /*force*/0);
19426 +       if (S_ISDIR(inode->i_mode)) {
19427 +               n = inode->i_nlink;
19428 +               n -= nlink;
19429 +               n += st->nlink;
19430 +               smp_mb(); /* for i_nlink */
19431 +               /* 0 can happen */
19432 +               set_nlink(inode, n);
19433 +       }
19434 +
19435 +       spin_lock(&inode->i_lock);
19436 +       inode->i_blocks = st->blocks;
19437 +       i_size_write(inode, st->size);
19438 +       spin_unlock(&inode->i_lock);
19439 +}
19440 +
19441 +/*
19442 + * common routine for aufs_getattr() and aufs_getxattr().
19443 + * returns zero or negative (an error).
19444 + * @dentry will be read-locked in success.
19445 + */
19446 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
19447 +{
19448 +       int err;
19449 +       unsigned int mnt_flags, sigen;
19450 +       unsigned char udba_none;
19451 +       aufs_bindex_t bindex;
19452 +       struct super_block *sb, *h_sb;
19453 +       struct inode *inode;
19454 +
19455 +       h_path->mnt = NULL;
19456 +       h_path->dentry = NULL;
19457 +
19458 +       err = 0;
19459 +       sb = dentry->d_sb;
19460 +       mnt_flags = au_mntflags(sb);
19461 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19462 +
19463 +       /* support fstat(2) */
19464 +       if (!d_unlinked(dentry) && !udba_none) {
19465 +               sigen = au_sigen(sb);
19466 +               err = au_digen_test(dentry, sigen);
19467 +               if (!err) {
19468 +                       di_read_lock_child(dentry, AuLock_IR);
19469 +                       err = au_dbrange_test(dentry);
19470 +                       if (unlikely(err)) {
19471 +                               di_read_unlock(dentry, AuLock_IR);
19472 +                               goto out;
19473 +                       }
19474 +               } else {
19475 +                       AuDebugOn(IS_ROOT(dentry));
19476 +                       di_write_lock_child(dentry);
19477 +                       err = au_dbrange_test(dentry);
19478 +                       if (!err)
19479 +                               err = au_reval_for_attr(dentry, sigen);
19480 +                       if (!err)
19481 +                               di_downgrade_lock(dentry, AuLock_IR);
19482 +                       else {
19483 +                               di_write_unlock(dentry);
19484 +                               goto out;
19485 +                       }
19486 +               }
19487 +       } else
19488 +               di_read_lock_child(dentry, AuLock_IR);
19489 +
19490 +       inode = d_inode(dentry);
19491 +       bindex = au_ibstart(inode);
19492 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19493 +       h_sb = h_path->mnt->mnt_sb;
19494 +       if (!force
19495 +           && !au_test_fs_bad_iattr(h_sb)
19496 +           && udba_none)
19497 +               goto out; /* success */
19498 +
19499 +       if (au_dbstart(dentry) == bindex)
19500 +               h_path->dentry = au_h_dptr(dentry, bindex);
19501 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19502 +               h_path->dentry = au_plink_lkup(inode, bindex);
19503 +               if (IS_ERR(h_path->dentry))
19504 +                       /* pretending success */
19505 +                       h_path->dentry = NULL;
19506 +               else
19507 +                       dput(h_path->dentry);
19508 +       }
19509 +
19510 +out:
19511 +       return err;
19512 +}
19513 +
19514 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19515 +                       struct dentry *dentry, struct kstat *st)
19516 +{
19517 +       int err;
19518 +       unsigned char positive;
19519 +       struct path h_path;
19520 +       struct inode *inode;
19521 +       struct super_block *sb;
19522 +
19523 +       inode = d_inode(dentry);
19524 +       sb = dentry->d_sb;
19525 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19526 +       if (unlikely(err))
19527 +               goto out;
19528 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19529 +       if (unlikely(err))
19530 +               goto out_si;
19531 +       if (unlikely(!h_path.dentry))
19532 +               /* illegally overlapped or something */
19533 +               goto out_fill; /* pretending success */
19534 +
19535 +       positive = d_is_positive(h_path.dentry);
19536 +       if (positive)
19537 +               err = vfs_getattr(&h_path, st);
19538 +       if (!err) {
19539 +               if (positive)
19540 +                       au_refresh_iattr(inode, st,
19541 +                                        d_inode(h_path.dentry)->i_nlink);
19542 +               goto out_fill; /* success */
19543 +       }
19544 +       AuTraceErr(err);
19545 +       goto out_di;
19546 +
19547 +out_fill:
19548 +       generic_fillattr(inode, st);
19549 +out_di:
19550 +       di_read_unlock(dentry, AuLock_IR);
19551 +out_si:
19552 +       si_read_unlock(sb);
19553 +out:
19554 +       AuTraceErr(err);
19555 +       return err;
19556 +}
19557 +
19558 +/* ---------------------------------------------------------------------- */
19559 +
19560 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
19561 +                                struct delayed_call *done)
19562 +{
19563 +       const char *ret;
19564 +       struct dentry *h_dentry;
19565 +       struct inode *h_inode;
19566 +       int err;
19567 +       aufs_bindex_t bindex;
19568 +
19569 +       ret = NULL; /* suppress a warning */
19570 +       err = -ECHILD;
19571 +       if (!dentry)
19572 +               goto out;
19573 +
19574 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19575 +       if (unlikely(err))
19576 +               goto out;
19577 +
19578 +       err = au_d_hashed_positive(dentry);
19579 +       if (unlikely(err))
19580 +               goto out_unlock;
19581 +
19582 +       err = -EINVAL;
19583 +       inode = d_inode(dentry);
19584 +       bindex = au_ibstart(inode);
19585 +       h_inode = au_h_iptr(inode, bindex);
19586 +       if (unlikely(!h_inode->i_op->get_link))
19587 +               goto out_unlock;
19588 +
19589 +       err = -EBUSY;
19590 +       h_dentry = NULL;
19591 +       if (au_dbstart(dentry) <= bindex) {
19592 +               h_dentry = au_h_dptr(dentry, bindex);
19593 +               if (h_dentry)
19594 +                       dget(h_dentry);
19595 +       }
19596 +       if (!h_dentry) {
19597 +               h_dentry = d_find_any_alias(h_inode);
19598 +               if (IS_ERR(h_dentry)) {
19599 +                       err = PTR_ERR(h_dentry);
19600 +                       goto out_unlock;
19601 +               }
19602 +       }
19603 +       if (unlikely(!h_dentry))
19604 +               goto out_unlock;
19605 +
19606 +       err = 0;
19607 +       AuDbg("%pf\n", h_inode->i_op->get_link);
19608 +       AuDbgDentry(h_dentry);
19609 +       ret = h_inode->i_op->get_link(h_dentry, h_inode, done);
19610 +       dput(h_dentry);
19611 +       if (IS_ERR(ret))
19612 +               err = PTR_ERR(ret);
19613 +
19614 +out_unlock:
19615 +       aufs_read_unlock(dentry, AuLock_IR);
19616 +out:
19617 +       if (unlikely(err))
19618 +               ret = ERR_PTR(err);
19619 +       AuTraceErrPtr(ret);
19620 +       return ret;
19621 +}
19622 +
19623 +/* ---------------------------------------------------------------------- */
19624 +
19625 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19626 +{
19627 +       int err;
19628 +       struct super_block *sb;
19629 +       struct inode *h_inode;
19630 +
19631 +       sb = inode->i_sb;
19632 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19633 +       lockdep_off();
19634 +       si_read_lock(sb, AuLock_FLUSH);
19635 +       ii_write_lock_child(inode);
19636 +       lockdep_on();
19637 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
19638 +       err = vfsub_update_time(h_inode, ts, flags);
19639 +       lockdep_off();
19640 +       if (!err)
19641 +               au_cpup_attr_timesizes(inode);
19642 +       ii_write_unlock(inode);
19643 +       si_read_unlock(sb);
19644 +       lockdep_on();
19645 +
19646 +       if (!err && (flags & S_VERSION))
19647 +               inode_inc_iversion(inode);
19648 +
19649 +       return err;
19650 +}
19651 +
19652 +/* ---------------------------------------------------------------------- */
19653 +
19654 +/* no getattr version will be set by module.c:aufs_init() */
19655 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
19656 +       aufs_iop[] = {
19657 +       [AuIop_SYMLINK] = {
19658 +               .permission     = aufs_permission,
19659 +#ifdef CONFIG_FS_POSIX_ACL
19660 +               .get_acl        = aufs_get_acl,
19661 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19662 +#endif
19663 +
19664 +               .setattr        = aufs_setattr,
19665 +               .getattr        = aufs_getattr,
19666 +
19667 +#ifdef CONFIG_AUFS_XATTR
19668 +               .setxattr       = aufs_setxattr,
19669 +               .getxattr       = aufs_getxattr,
19670 +               .listxattr      = aufs_listxattr,
19671 +               .removexattr    = aufs_removexattr,
19672 +#endif
19673 +
19674 +               .readlink       = generic_readlink,
19675 +               .get_link       = aufs_get_link,
19676 +
19677 +               /* .update_time = aufs_update_time */
19678 +       },
19679 +       [AuIop_DIR] = {
19680 +               .create         = aufs_create,
19681 +               .lookup         = aufs_lookup,
19682 +               .link           = aufs_link,
19683 +               .unlink         = aufs_unlink,
19684 +               .symlink        = aufs_symlink,
19685 +               .mkdir          = aufs_mkdir,
19686 +               .rmdir          = aufs_rmdir,
19687 +               .mknod          = aufs_mknod,
19688 +               .rename         = aufs_rename,
19689 +
19690 +               .permission     = aufs_permission,
19691 +#ifdef CONFIG_FS_POSIX_ACL
19692 +               .get_acl        = aufs_get_acl,
19693 +               .set_acl        = aufs_set_acl,
19694 +#endif
19695 +
19696 +               .setattr        = aufs_setattr,
19697 +               .getattr        = aufs_getattr,
19698 +
19699 +#ifdef CONFIG_AUFS_XATTR
19700 +               .setxattr       = aufs_setxattr,
19701 +               .getxattr       = aufs_getxattr,
19702 +               .listxattr      = aufs_listxattr,
19703 +               .removexattr    = aufs_removexattr,
19704 +#endif
19705 +
19706 +               .update_time    = aufs_update_time,
19707 +               .atomic_open    = aufs_atomic_open,
19708 +               .tmpfile        = aufs_tmpfile
19709 +       },
19710 +       [AuIop_OTHER] = {
19711 +               .permission     = aufs_permission,
19712 +#ifdef CONFIG_FS_POSIX_ACL
19713 +               .get_acl        = aufs_get_acl,
19714 +               .set_acl        = aufs_set_acl,
19715 +#endif
19716 +
19717 +               .setattr        = aufs_setattr,
19718 +               .getattr        = aufs_getattr,
19719 +
19720 +#ifdef CONFIG_AUFS_XATTR
19721 +               .setxattr       = aufs_setxattr,
19722 +               .getxattr       = aufs_getxattr,
19723 +               .listxattr      = aufs_listxattr,
19724 +               .removexattr    = aufs_removexattr,
19725 +#endif
19726 +
19727 +               .update_time    = aufs_update_time
19728 +       }
19729 +};
19730 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19731 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19732 +++ linux/fs/aufs/i_op_del.c    2016-04-24 18:32:51.393686895 +0200
19733 @@ -0,0 +1,510 @@
19734 +/*
19735 + * Copyright (C) 2005-2016 Junjiro R. Okajima
19736 + *
19737 + * This program, aufs is free software; you can redistribute it and/or modify
19738 + * it under the terms of the GNU General Public License as published by
19739 + * the Free Software Foundation; either version 2 of the License, or
19740 + * (at your option) any later version.
19741 + *
19742 + * This program is distributed in the hope that it will be useful,
19743 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19744 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19745 + * GNU General Public License for more details.
19746 + *
19747 + * You should have received a copy of the GNU General Public License
19748 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19749 + */
19750 +
19751 +/*
19752 + * inode operations (del entry)
19753 + */
19754 +
19755 +#include "aufs.h"
19756 +
19757 +/*
19758 + * decide if a new whiteout for @dentry is necessary or not.
19759 + * when it is necessary, prepare the parent dir for the upper branch whose
19760 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19761 + * be done by caller.
19762 + * return value:
19763 + * 0: wh is unnecessary
19764 + * plus: wh is necessary
19765 + * minus: error
19766 + */
19767 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19768 +{
19769 +       int need_wh, err;
19770 +       aufs_bindex_t bstart;
19771 +       struct super_block *sb;
19772 +
19773 +       sb = dentry->d_sb;
19774 +       bstart = au_dbstart(dentry);
19775 +       if (*bcpup < 0) {
19776 +               *bcpup = bstart;
19777 +               if (au_test_ro(sb, bstart, d_inode(dentry))) {
19778 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19779 +                       *bcpup = err;
19780 +                       if (unlikely(err < 0))
19781 +                               goto out;
19782 +               }
19783 +       } else
19784 +               AuDebugOn(bstart < *bcpup
19785 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
19786 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
19787 +
19788 +       if (*bcpup != bstart) {
19789 +               err = au_cpup_dirs(dentry, *bcpup);
19790 +               if (unlikely(err))
19791 +                       goto out;
19792 +               need_wh = 1;
19793 +       } else {
19794 +               struct au_dinfo *dinfo, *tmp;
19795 +
19796 +               need_wh = -ENOMEM;
19797 +               dinfo = au_di(dentry);
19798 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19799 +               if (tmp) {
19800 +                       au_di_cp(tmp, dinfo);
19801 +                       au_di_swap(tmp, dinfo);
19802 +                       /* returns the number of positive dentries */
19803 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
19804 +                       au_di_swap(tmp, dinfo);
19805 +                       au_rw_write_unlock(&tmp->di_rwsem);
19806 +                       au_di_free(tmp);
19807 +               }
19808 +       }
19809 +       AuDbg("need_wh %d\n", need_wh);
19810 +       err = need_wh;
19811 +
19812 +out:
19813 +       return err;
19814 +}
19815 +
19816 +/*
19817 + * simple tests for the del-entry operations.
19818 + * following the checks in vfs, plus the parent-child relationship.
19819 + */
19820 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19821 +              struct dentry *h_parent, int isdir)
19822 +{
19823 +       int err;
19824 +       umode_t h_mode;
19825 +       struct dentry *h_dentry, *h_latest;
19826 +       struct inode *h_inode;
19827 +
19828 +       h_dentry = au_h_dptr(dentry, bindex);
19829 +       if (d_really_is_positive(dentry)) {
19830 +               err = -ENOENT;
19831 +               if (unlikely(d_is_negative(h_dentry)))
19832 +                       goto out;
19833 +               h_inode = d_inode(h_dentry);
19834 +               if (unlikely(!h_inode->i_nlink))
19835 +                       goto out;
19836 +
19837 +               h_mode = h_inode->i_mode;
19838 +               if (!isdir) {
19839 +                       err = -EISDIR;
19840 +                       if (unlikely(S_ISDIR(h_mode)))
19841 +                               goto out;
19842 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19843 +                       err = -ENOTDIR;
19844 +                       goto out;
19845 +               }
19846 +       } else {
19847 +               /* rename(2) case */
19848 +               err = -EIO;
19849 +               if (unlikely(d_is_positive(h_dentry)))
19850 +                       goto out;
19851 +       }
19852 +
19853 +       err = -ENOENT;
19854 +       /* expected parent dir is locked */
19855 +       if (unlikely(h_parent != h_dentry->d_parent))
19856 +               goto out;
19857 +       err = 0;
19858 +
19859 +       /*
19860 +        * rmdir a dir may break the consistency on some filesystem.
19861 +        * let's try heavy test.
19862 +        */
19863 +       err = -EACCES;
19864 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
19865 +                    && au_test_h_perm(d_inode(h_parent),
19866 +                                      MAY_EXEC | MAY_WRITE)))
19867 +               goto out;
19868 +
19869 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
19870 +       err = -EIO;
19871 +       if (IS_ERR(h_latest))
19872 +               goto out;
19873 +       if (h_latest == h_dentry)
19874 +               err = 0;
19875 +       dput(h_latest);
19876 +
19877 +out:
19878 +       return err;
19879 +}
19880 +
19881 +/*
19882 + * decide the branch where we operate for @dentry. the branch index will be set
19883 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19884 + * dir for reverting.
19885 + * when a new whiteout is necessary, create it.
19886 + */
19887 +static struct dentry*
19888 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19889 +                   struct au_dtime *dt, struct au_pin *pin)
19890 +{
19891 +       struct dentry *wh_dentry;
19892 +       struct super_block *sb;
19893 +       struct path h_path;
19894 +       int err, need_wh;
19895 +       unsigned int udba;
19896 +       aufs_bindex_t bcpup;
19897 +
19898 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19899 +       wh_dentry = ERR_PTR(need_wh);
19900 +       if (unlikely(need_wh < 0))
19901 +               goto out;
19902 +
19903 +       sb = dentry->d_sb;
19904 +       udba = au_opt_udba(sb);
19905 +       bcpup = *rbcpup;
19906 +       err = au_pin(pin, dentry, bcpup, udba,
19907 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19908 +       wh_dentry = ERR_PTR(err);
19909 +       if (unlikely(err))
19910 +               goto out;
19911 +
19912 +       h_path.dentry = au_pinned_h_parent(pin);
19913 +       if (udba != AuOpt_UDBA_NONE
19914 +           && au_dbstart(dentry) == bcpup) {
19915 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
19916 +               wh_dentry = ERR_PTR(err);
19917 +               if (unlikely(err))
19918 +                       goto out_unpin;
19919 +       }
19920 +
19921 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
19922 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
19923 +       wh_dentry = NULL;
19924 +       if (!need_wh)
19925 +               goto out; /* success, no need to create whiteout */
19926 +
19927 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
19928 +       if (IS_ERR(wh_dentry))
19929 +               goto out_unpin;
19930 +
19931 +       /* returns with the parent is locked and wh_dentry is dget-ed */
19932 +       goto out; /* success */
19933 +
19934 +out_unpin:
19935 +       au_unpin(pin);
19936 +out:
19937 +       return wh_dentry;
19938 +}
19939 +
19940 +/*
19941 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
19942 + * in order to be revertible and save time for removing many child whiteouts
19943 + * under the dir.
19944 + * returns 1 when there are too many child whiteout and caller should remove
19945 + * them asynchronously. returns 0 when the number of children is enough small to
19946 + * remove now or the branch fs is a remote fs.
19947 + * otherwise return an error.
19948 + */
19949 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
19950 +                          struct au_nhash *whlist, struct inode *dir)
19951 +{
19952 +       int rmdir_later, err, dirwh;
19953 +       struct dentry *h_dentry;
19954 +       struct super_block *sb;
19955 +       struct inode *inode;
19956 +
19957 +       sb = dentry->d_sb;
19958 +       SiMustAnyLock(sb);
19959 +       h_dentry = au_h_dptr(dentry, bindex);
19960 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
19961 +       if (unlikely(err))
19962 +               goto out;
19963 +
19964 +       /* stop monitoring */
19965 +       inode = d_inode(dentry);
19966 +       au_hn_free(au_hi(inode, bindex));
19967 +
19968 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
19969 +               dirwh = au_sbi(sb)->si_dirwh;
19970 +               rmdir_later = (dirwh <= 1);
19971 +               if (!rmdir_later)
19972 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
19973 +                                                             dirwh);
19974 +               if (rmdir_later)
19975 +                       return rmdir_later;
19976 +       }
19977 +
19978 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
19979 +       if (unlikely(err)) {
19980 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
19981 +                       h_dentry, bindex, err);
19982 +               err = 0;
19983 +       }
19984 +
19985 +out:
19986 +       AuTraceErr(err);
19987 +       return err;
19988 +}
19989 +
19990 +/*
19991 + * final procedure for deleting a entry.
19992 + * maintain dentry and iattr.
19993 + */
19994 +static void epilog(struct inode *dir, struct dentry *dentry,
19995 +                  aufs_bindex_t bindex)
19996 +{
19997 +       struct inode *inode;
19998 +
19999 +       inode = d_inode(dentry);
20000 +       d_drop(dentry);
20001 +       inode->i_ctime = dir->i_ctime;
20002 +
20003 +       au_dir_ts(dir, bindex);
20004 +       dir->i_version++;
20005 +}
20006 +
20007 +/*
20008 + * when an error happened, remove the created whiteout and revert everything.
20009 + */
20010 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
20011 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
20012 +                    struct dentry *dentry, struct au_dtime *dt)
20013 +{
20014 +       int rerr;
20015 +       struct path h_path = {
20016 +               .dentry = wh_dentry,
20017 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
20018 +       };
20019 +
20020 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
20021 +       if (!rerr) {
20022 +               au_set_dbwh(dentry, bwh);
20023 +               au_dtime_revert(dt);
20024 +               return 0;
20025 +       }
20026 +
20027 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
20028 +       return -EIO;
20029 +}
20030 +
20031 +/* ---------------------------------------------------------------------- */
20032 +
20033 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
20034 +{
20035 +       int err;
20036 +       aufs_bindex_t bwh, bindex, bstart;
20037 +       struct inode *inode, *h_dir, *delegated;
20038 +       struct dentry *parent, *wh_dentry;
20039 +       /* to reuduce stack size */
20040 +       struct {
20041 +               struct au_dtime dt;
20042 +               struct au_pin pin;
20043 +               struct path h_path;
20044 +       } *a;
20045 +
20046 +       IMustLock(dir);
20047 +
20048 +       err = -ENOMEM;
20049 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20050 +       if (unlikely(!a))
20051 +               goto out;
20052 +
20053 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20054 +       if (unlikely(err))
20055 +               goto out_free;
20056 +       err = au_d_hashed_positive(dentry);
20057 +       if (unlikely(err))
20058 +               goto out_unlock;
20059 +       inode = d_inode(dentry);
20060 +       IMustLock(inode);
20061 +       err = -EISDIR;
20062 +       if (unlikely(d_is_dir(dentry)))
20063 +               goto out_unlock; /* possible? */
20064 +
20065 +       bstart = au_dbstart(dentry);
20066 +       bwh = au_dbwh(dentry);
20067 +       bindex = -1;
20068 +       parent = dentry->d_parent; /* dir inode is locked */
20069 +       di_write_lock_parent(parent);
20070 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
20071 +                                       &a->pin);
20072 +       err = PTR_ERR(wh_dentry);
20073 +       if (IS_ERR(wh_dentry))
20074 +               goto out_parent;
20075 +
20076 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
20077 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
20078 +       dget(a->h_path.dentry);
20079 +       if (bindex == bstart) {
20080 +               h_dir = au_pinned_h_dir(&a->pin);
20081 +               delegated = NULL;
20082 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
20083 +               if (unlikely(err == -EWOULDBLOCK)) {
20084 +                       pr_warn("cannot retry for NFSv4 delegation"
20085 +                               " for an internal unlink\n");
20086 +                       iput(delegated);
20087 +               }
20088 +       } else {
20089 +               /* dir inode is locked */
20090 +               h_dir = d_inode(wh_dentry->d_parent);
20091 +               IMustLock(h_dir);
20092 +               err = 0;
20093 +       }
20094 +
20095 +       if (!err) {
20096 +               vfsub_drop_nlink(inode);
20097 +               epilog(dir, dentry, bindex);
20098 +
20099 +               /* update target timestamps */
20100 +               if (bindex == bstart) {
20101 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
20102 +                       /*ignore*/
20103 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20104 +               } else
20105 +                       /* todo: this timestamp may be reverted later */
20106 +                       inode->i_ctime = h_dir->i_ctime;
20107 +               goto out_unpin; /* success */
20108 +       }
20109 +
20110 +       /* revert */
20111 +       if (wh_dentry) {
20112 +               int rerr;
20113 +
20114 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20115 +                                &a->dt);
20116 +               if (rerr)
20117 +                       err = rerr;
20118 +       }
20119 +
20120 +out_unpin:
20121 +       au_unpin(&a->pin);
20122 +       dput(wh_dentry);
20123 +       dput(a->h_path.dentry);
20124 +out_parent:
20125 +       di_write_unlock(parent);
20126 +out_unlock:
20127 +       aufs_read_unlock(dentry, AuLock_DW);
20128 +out_free:
20129 +       kfree(a);
20130 +out:
20131 +       return err;
20132 +}
20133 +
20134 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
20135 +{
20136 +       int err, rmdir_later;
20137 +       aufs_bindex_t bwh, bindex, bstart;
20138 +       struct inode *inode;
20139 +       struct dentry *parent, *wh_dentry, *h_dentry;
20140 +       struct au_whtmp_rmdir *args;
20141 +       /* to reuduce stack size */
20142 +       struct {
20143 +               struct au_dtime dt;
20144 +               struct au_pin pin;
20145 +       } *a;
20146 +
20147 +       IMustLock(dir);
20148 +
20149 +       err = -ENOMEM;
20150 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20151 +       if (unlikely(!a))
20152 +               goto out;
20153 +
20154 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20155 +       if (unlikely(err))
20156 +               goto out_free;
20157 +       err = au_alive_dir(dentry);
20158 +       if (unlikely(err))
20159 +               goto out_unlock;
20160 +       inode = d_inode(dentry);
20161 +       IMustLock(inode);
20162 +       err = -ENOTDIR;
20163 +       if (unlikely(!d_is_dir(dentry)))
20164 +               goto out_unlock; /* possible? */
20165 +
20166 +       err = -ENOMEM;
20167 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20168 +       if (unlikely(!args))
20169 +               goto out_unlock;
20170 +
20171 +       parent = dentry->d_parent; /* dir inode is locked */
20172 +       di_write_lock_parent(parent);
20173 +       err = au_test_empty(dentry, &args->whlist);
20174 +       if (unlikely(err))
20175 +               goto out_parent;
20176 +
20177 +       bstart = au_dbstart(dentry);
20178 +       bwh = au_dbwh(dentry);
20179 +       bindex = -1;
20180 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20181 +                                       &a->pin);
20182 +       err = PTR_ERR(wh_dentry);
20183 +       if (IS_ERR(wh_dentry))
20184 +               goto out_parent;
20185 +
20186 +       h_dentry = au_h_dptr(dentry, bstart);
20187 +       dget(h_dentry);
20188 +       rmdir_later = 0;
20189 +       if (bindex == bstart) {
20190 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
20191 +               if (err > 0) {
20192 +                       rmdir_later = err;
20193 +                       err = 0;
20194 +               }
20195 +       } else {
20196 +               /* stop monitoring */
20197 +               au_hn_free(au_hi(inode, bstart));
20198 +
20199 +               /* dir inode is locked */
20200 +               IMustLock(d_inode(wh_dentry->d_parent));
20201 +               err = 0;
20202 +       }
20203 +
20204 +       if (!err) {
20205 +               vfsub_dead_dir(inode);
20206 +               au_set_dbdiropq(dentry, -1);
20207 +               epilog(dir, dentry, bindex);
20208 +
20209 +               if (rmdir_later) {
20210 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
20211 +                       args = NULL;
20212 +               }
20213 +
20214 +               goto out_unpin; /* success */
20215 +       }
20216 +
20217 +       /* revert */
20218 +       AuLabel(revert);
20219 +       if (wh_dentry) {
20220 +               int rerr;
20221 +
20222 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20223 +                                &a->dt);
20224 +               if (rerr)
20225 +                       err = rerr;
20226 +       }
20227 +
20228 +out_unpin:
20229 +       au_unpin(&a->pin);
20230 +       dput(wh_dentry);
20231 +       dput(h_dentry);
20232 +out_parent:
20233 +       di_write_unlock(parent);
20234 +       if (args)
20235 +               au_whtmp_rmdir_free(args);
20236 +out_unlock:
20237 +       aufs_read_unlock(dentry, AuLock_DW);
20238 +out_free:
20239 +       kfree(a);
20240 +out:
20241 +       AuTraceErr(err);
20242 +       return err;
20243 +}
20244 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20245 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20246 +++ linux/fs/aufs/i_op_ren.c    2016-04-24 18:32:51.393686895 +0200
20247 @@ -0,0 +1,1015 @@
20248 +/*
20249 + * Copyright (C) 2005-2016 Junjiro R. Okajima
20250 + *
20251 + * This program, aufs is free software; you can redistribute it and/or modify
20252 + * it under the terms of the GNU General Public License as published by
20253 + * the Free Software Foundation; either version 2 of the License, or
20254 + * (at your option) any later version.
20255 + *
20256 + * This program is distributed in the hope that it will be useful,
20257 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20258 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20259 + * GNU General Public License for more details.
20260 + *
20261 + * You should have received a copy of the GNU General Public License
20262 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20263 + */
20264 +
20265 +/*
20266 + * inode operation (rename entry)
20267 + * todo: this is crazy monster
20268 + */
20269 +
20270 +#include "aufs.h"
20271 +
20272 +enum { AuSRC, AuDST, AuSrcDst };
20273 +enum { AuPARENT, AuCHILD, AuParentChild };
20274 +
20275 +#define AuRen_ISDIR    1
20276 +#define AuRen_ISSAMEDIR        (1 << 1)
20277 +#define AuRen_WHSRC    (1 << 2)
20278 +#define AuRen_WHDST    (1 << 3)
20279 +#define AuRen_MNT_WRITE        (1 << 4)
20280 +#define AuRen_DT_DSTDIR        (1 << 5)
20281 +#define AuRen_DIROPQ   (1 << 6)
20282 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20283 +#define au_fset_ren(flags, name) \
20284 +       do { (flags) |= AuRen_##name; } while (0)
20285 +#define au_fclr_ren(flags, name) \
20286 +       do { (flags) &= ~AuRen_##name; } while (0)
20287 +
20288 +struct au_ren_args {
20289 +       struct {
20290 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20291 +                       *wh_dentry;
20292 +               struct inode *dir, *inode;
20293 +               struct au_hinode *hdir;
20294 +               struct au_dtime dt[AuParentChild];
20295 +               aufs_bindex_t bstart;
20296 +       } sd[AuSrcDst];
20297 +
20298 +#define src_dentry     sd[AuSRC].dentry
20299 +#define src_dir                sd[AuSRC].dir
20300 +#define src_inode      sd[AuSRC].inode
20301 +#define src_h_dentry   sd[AuSRC].h_dentry
20302 +#define src_parent     sd[AuSRC].parent
20303 +#define src_h_parent   sd[AuSRC].h_parent
20304 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20305 +#define src_hdir       sd[AuSRC].hdir
20306 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20307 +#define src_dt         sd[AuSRC].dt
20308 +#define src_bstart     sd[AuSRC].bstart
20309 +
20310 +#define dst_dentry     sd[AuDST].dentry
20311 +#define dst_dir                sd[AuDST].dir
20312 +#define dst_inode      sd[AuDST].inode
20313 +#define dst_h_dentry   sd[AuDST].h_dentry
20314 +#define dst_parent     sd[AuDST].parent
20315 +#define dst_h_parent   sd[AuDST].h_parent
20316 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20317 +#define dst_hdir       sd[AuDST].hdir
20318 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20319 +#define dst_dt         sd[AuDST].dt
20320 +#define dst_bstart     sd[AuDST].bstart
20321 +
20322 +       struct dentry *h_trap;
20323 +       struct au_branch *br;
20324 +       struct au_hinode *src_hinode;
20325 +       struct path h_path;
20326 +       struct au_nhash whlist;
20327 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
20328 +
20329 +       unsigned int flags;
20330 +
20331 +       struct au_whtmp_rmdir *thargs;
20332 +       struct dentry *h_dst;
20333 +};
20334 +
20335 +/* ---------------------------------------------------------------------- */
20336 +
20337 +/*
20338 + * functions for reverting.
20339 + * when an error happened in a single rename systemcall, we should revert
20340 + * everything as if nothing happened.
20341 + * we don't need to revert the copied-up/down the parent dir since they are
20342 + * harmless.
20343 + */
20344 +
20345 +#define RevertFailure(fmt, ...) do { \
20346 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20347 +               ##__VA_ARGS__, err, rerr); \
20348 +       err = -EIO; \
20349 +} while (0)
20350 +
20351 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20352 +{
20353 +       int rerr;
20354 +
20355 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20356 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
20357 +       au_hn_imtx_unlock(a->src_hinode);
20358 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
20359 +       if (rerr)
20360 +               RevertFailure("remove diropq %pd", a->src_dentry);
20361 +}
20362 +
20363 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20364 +{
20365 +       int rerr;
20366 +       struct inode *delegated;
20367 +
20368 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20369 +                                         a->src_h_parent);
20370 +       rerr = PTR_ERR(a->h_path.dentry);
20371 +       if (IS_ERR(a->h_path.dentry)) {
20372 +               RevertFailure("lkup one %pd", a->src_dentry);
20373 +               return;
20374 +       }
20375 +
20376 +       delegated = NULL;
20377 +       rerr = vfsub_rename(a->dst_h_dir,
20378 +                           au_h_dptr(a->src_dentry, a->btgt),
20379 +                           a->src_h_dir, &a->h_path, &delegated);
20380 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20381 +               pr_warn("cannot retry for NFSv4 delegation"
20382 +                       " for an internal rename\n");
20383 +               iput(delegated);
20384 +       }
20385 +       d_drop(a->h_path.dentry);
20386 +       dput(a->h_path.dentry);
20387 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20388 +       if (rerr)
20389 +               RevertFailure("rename %pd", a->src_dentry);
20390 +}
20391 +
20392 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20393 +{
20394 +       int rerr;
20395 +       struct inode *delegated;
20396 +
20397 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20398 +                                         a->dst_h_parent);
20399 +       rerr = PTR_ERR(a->h_path.dentry);
20400 +       if (IS_ERR(a->h_path.dentry)) {
20401 +               RevertFailure("lkup one %pd", a->dst_dentry);
20402 +               return;
20403 +       }
20404 +       if (d_is_positive(a->h_path.dentry)) {
20405 +               d_drop(a->h_path.dentry);
20406 +               dput(a->h_path.dentry);
20407 +               return;
20408 +       }
20409 +
20410 +       delegated = NULL;
20411 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20412 +                           &delegated);
20413 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20414 +               pr_warn("cannot retry for NFSv4 delegation"
20415 +                       " for an internal rename\n");
20416 +               iput(delegated);
20417 +       }
20418 +       d_drop(a->h_path.dentry);
20419 +       dput(a->h_path.dentry);
20420 +       if (!rerr)
20421 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20422 +       else
20423 +               RevertFailure("rename %pd", a->h_dst);
20424 +}
20425 +
20426 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20427 +{
20428 +       int rerr;
20429 +
20430 +       a->h_path.dentry = a->src_wh_dentry;
20431 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20432 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20433 +       if (rerr)
20434 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20435 +}
20436 +#undef RevertFailure
20437 +
20438 +/* ---------------------------------------------------------------------- */
20439 +
20440 +/*
20441 + * when we have to copyup the renaming entry, do it with the rename-target name
20442 + * in order to minimize the cost (the later actual rename is unnecessary).
20443 + * otherwise rename it on the target branch.
20444 + */
20445 +static int au_ren_or_cpup(struct au_ren_args *a)
20446 +{
20447 +       int err;
20448 +       struct dentry *d;
20449 +       struct inode *delegated;
20450 +
20451 +       d = a->src_dentry;
20452 +       if (au_dbstart(d) == a->btgt) {
20453 +               a->h_path.dentry = a->dst_h_dentry;
20454 +               if (au_ftest_ren(a->flags, DIROPQ)
20455 +                   && au_dbdiropq(d) == a->btgt)
20456 +                       au_fclr_ren(a->flags, DIROPQ);
20457 +               AuDebugOn(au_dbstart(d) != a->btgt);
20458 +               delegated = NULL;
20459 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20460 +                                  a->dst_h_dir, &a->h_path, &delegated);
20461 +               if (unlikely(err == -EWOULDBLOCK)) {
20462 +                       pr_warn("cannot retry for NFSv4 delegation"
20463 +                               " for an internal rename\n");
20464 +                       iput(delegated);
20465 +               }
20466 +       } else
20467 +               BUG();
20468 +
20469 +       if (!err && a->h_dst)
20470 +               /* it will be set to dinfo later */
20471 +               dget(a->h_dst);
20472 +
20473 +       return err;
20474 +}
20475 +
20476 +/* cf. aufs_rmdir() */
20477 +static int au_ren_del_whtmp(struct au_ren_args *a)
20478 +{
20479 +       int err;
20480 +       struct inode *dir;
20481 +
20482 +       dir = a->dst_dir;
20483 +       SiMustAnyLock(dir->i_sb);
20484 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20485 +                                    au_sbi(dir->i_sb)->si_dirwh)
20486 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20487 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20488 +               if (unlikely(err))
20489 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20490 +                               "ignored.\n", a->h_dst, err);
20491 +       } else {
20492 +               au_nhash_wh_free(&a->thargs->whlist);
20493 +               a->thargs->whlist = a->whlist;
20494 +               a->whlist.nh_num = 0;
20495 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20496 +               dput(a->h_dst);
20497 +               a->thargs = NULL;
20498 +       }
20499 +
20500 +       return 0;
20501 +}
20502 +
20503 +/* make it 'opaque' dir. */
20504 +static int au_ren_diropq(struct au_ren_args *a)
20505 +{
20506 +       int err;
20507 +       struct dentry *diropq;
20508 +
20509 +       err = 0;
20510 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
20511 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
20512 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20513 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
20514 +       au_hn_imtx_unlock(a->src_hinode);
20515 +       if (IS_ERR(diropq))
20516 +               err = PTR_ERR(diropq);
20517 +       else
20518 +               dput(diropq);
20519 +
20520 +       return err;
20521 +}
20522 +
20523 +static int do_rename(struct au_ren_args *a)
20524 +{
20525 +       int err;
20526 +       struct dentry *d, *h_d;
20527 +
20528 +       /* prepare workqueue args for asynchronous rmdir */
20529 +       h_d = a->dst_h_dentry;
20530 +       if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
20531 +               err = -ENOMEM;
20532 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20533 +               if (unlikely(!a->thargs))
20534 +                       goto out;
20535 +               a->h_dst = dget(h_d);
20536 +       }
20537 +
20538 +       /* create whiteout for src_dentry */
20539 +       if (au_ftest_ren(a->flags, WHSRC)) {
20540 +               a->src_bwh = au_dbwh(a->src_dentry);
20541 +               AuDebugOn(a->src_bwh >= 0);
20542 +               a->src_wh_dentry
20543 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20544 +               err = PTR_ERR(a->src_wh_dentry);
20545 +               if (IS_ERR(a->src_wh_dentry))
20546 +                       goto out_thargs;
20547 +       }
20548 +
20549 +       /* lookup whiteout for dentry */
20550 +       if (au_ftest_ren(a->flags, WHDST)) {
20551 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20552 +                                a->br);
20553 +               err = PTR_ERR(h_d);
20554 +               if (IS_ERR(h_d))
20555 +                       goto out_whsrc;
20556 +               if (d_is_negative(h_d))
20557 +                       dput(h_d);
20558 +               else
20559 +                       a->dst_wh_dentry = h_d;
20560 +       }
20561 +
20562 +       /* rename dentry to tmpwh */
20563 +       if (a->thargs) {
20564 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
20565 +               if (unlikely(err))
20566 +                       goto out_whdst;
20567 +
20568 +               d = a->dst_dentry;
20569 +               au_set_h_dptr(d, a->btgt, NULL);
20570 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
20571 +               if (unlikely(err))
20572 +                       goto out_whtmp;
20573 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
20574 +       }
20575 +
20576 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_bstart != a->btgt);
20577 +
20578 +       /* rename by vfs_rename or cpup */
20579 +       d = a->dst_dentry;
20580 +       if (au_ftest_ren(a->flags, ISDIR)
20581 +           && (a->dst_wh_dentry
20582 +               || au_dbdiropq(d) == a->btgt
20583 +               /* hide the lower to keep xino */
20584 +               || a->btgt < au_dbend(d)
20585 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20586 +               au_fset_ren(a->flags, DIROPQ);
20587 +       err = au_ren_or_cpup(a);
20588 +       if (unlikely(err))
20589 +               /* leave the copied-up one */
20590 +               goto out_whtmp;
20591 +
20592 +       /* make dir opaque */
20593 +       if (au_ftest_ren(a->flags, DIROPQ)) {
20594 +               err = au_ren_diropq(a);
20595 +               if (unlikely(err))
20596 +                       goto out_rename;
20597 +       }
20598 +
20599 +       /* update target timestamps */
20600 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
20601 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20602 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20603 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20604 +
20605 +       /* remove whiteout for dentry */
20606 +       if (a->dst_wh_dentry) {
20607 +               a->h_path.dentry = a->dst_wh_dentry;
20608 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20609 +                                         a->dst_dentry);
20610 +               if (unlikely(err))
20611 +                       goto out_diropq;
20612 +       }
20613 +
20614 +       /* remove whtmp */
20615 +       if (a->thargs)
20616 +               au_ren_del_whtmp(a); /* ignore this error */
20617 +
20618 +       au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
20619 +       err = 0;
20620 +       goto out_success;
20621 +
20622 +out_diropq:
20623 +       if (au_ftest_ren(a->flags, DIROPQ))
20624 +               au_ren_rev_diropq(err, a);
20625 +out_rename:
20626 +       au_ren_rev_rename(err, a);
20627 +       dput(a->h_dst);
20628 +out_whtmp:
20629 +       if (a->thargs)
20630 +               au_ren_rev_whtmp(err, a);
20631 +out_whdst:
20632 +       dput(a->dst_wh_dentry);
20633 +       a->dst_wh_dentry = NULL;
20634 +out_whsrc:
20635 +       if (a->src_wh_dentry)
20636 +               au_ren_rev_whsrc(err, a);
20637 +out_success:
20638 +       dput(a->src_wh_dentry);
20639 +       dput(a->dst_wh_dentry);
20640 +out_thargs:
20641 +       if (a->thargs) {
20642 +               dput(a->h_dst);
20643 +               au_whtmp_rmdir_free(a->thargs);
20644 +               a->thargs = NULL;
20645 +       }
20646 +out:
20647 +       return err;
20648 +}
20649 +
20650 +/* ---------------------------------------------------------------------- */
20651 +
20652 +/*
20653 + * test if @dentry dir can be rename destination or not.
20654 + * success means, it is a logically empty dir.
20655 + */
20656 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20657 +{
20658 +       return au_test_empty(dentry, whlist);
20659 +}
20660 +
20661 +/*
20662 + * test if @dentry dir can be rename source or not.
20663 + * if it can, return 0 and @children is filled.
20664 + * success means,
20665 + * - it is a logically empty dir.
20666 + * - or, it exists on writable branch and has no children including whiteouts
20667 + *       on the lower branch.
20668 + */
20669 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20670 +{
20671 +       int err;
20672 +       unsigned int rdhash;
20673 +       aufs_bindex_t bstart;
20674 +
20675 +       bstart = au_dbstart(dentry);
20676 +       if (bstart != btgt) {
20677 +               struct au_nhash whlist;
20678 +
20679 +               SiMustAnyLock(dentry->d_sb);
20680 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20681 +               if (!rdhash)
20682 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20683 +                                                          dentry));
20684 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20685 +               if (unlikely(err))
20686 +                       goto out;
20687 +               err = au_test_empty(dentry, &whlist);
20688 +               au_nhash_wh_free(&whlist);
20689 +               goto out;
20690 +       }
20691 +
20692 +       if (bstart == au_dbtaildir(dentry))
20693 +               return 0; /* success */
20694 +
20695 +       err = au_test_empty_lower(dentry);
20696 +
20697 +out:
20698 +       if (err == -ENOTEMPTY) {
20699 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20700 +                       " is not supported\n");
20701 +               err = -EXDEV;
20702 +       }
20703 +       return err;
20704 +}
20705 +
20706 +/* side effect: sets whlist and h_dentry */
20707 +static int au_ren_may_dir(struct au_ren_args *a)
20708 +{
20709 +       int err;
20710 +       unsigned int rdhash;
20711 +       struct dentry *d;
20712 +
20713 +       d = a->dst_dentry;
20714 +       SiMustAnyLock(d->d_sb);
20715 +
20716 +       err = 0;
20717 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20718 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20719 +               if (!rdhash)
20720 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20721 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20722 +               if (unlikely(err))
20723 +                       goto out;
20724 +
20725 +               au_set_dbstart(d, a->dst_bstart);
20726 +               err = may_rename_dstdir(d, &a->whlist);
20727 +               au_set_dbstart(d, a->btgt);
20728 +       }
20729 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
20730 +       if (unlikely(err))
20731 +               goto out;
20732 +
20733 +       d = a->src_dentry;
20734 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
20735 +       if (au_ftest_ren(a->flags, ISDIR)) {
20736 +               err = may_rename_srcdir(d, a->btgt);
20737 +               if (unlikely(err)) {
20738 +                       au_nhash_wh_free(&a->whlist);
20739 +                       a->whlist.nh_num = 0;
20740 +               }
20741 +       }
20742 +out:
20743 +       return err;
20744 +}
20745 +
20746 +/* ---------------------------------------------------------------------- */
20747 +
20748 +/*
20749 + * simple tests for rename.
20750 + * following the checks in vfs, plus the parent-child relationship.
20751 + */
20752 +static int au_may_ren(struct au_ren_args *a)
20753 +{
20754 +       int err, isdir;
20755 +       struct inode *h_inode;
20756 +
20757 +       if (a->src_bstart == a->btgt) {
20758 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20759 +                                au_ftest_ren(a->flags, ISDIR));
20760 +               if (unlikely(err))
20761 +                       goto out;
20762 +               err = -EINVAL;
20763 +               if (unlikely(a->src_h_dentry == a->h_trap))
20764 +                       goto out;
20765 +       }
20766 +
20767 +       err = 0;
20768 +       if (a->dst_bstart != a->btgt)
20769 +               goto out;
20770 +
20771 +       err = -ENOTEMPTY;
20772 +       if (unlikely(a->dst_h_dentry == a->h_trap))
20773 +               goto out;
20774 +
20775 +       err = -EIO;
20776 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
20777 +       if (d_really_is_negative(a->dst_dentry)) {
20778 +               if (d_is_negative(a->dst_h_dentry))
20779 +                       err = au_may_add(a->dst_dentry, a->btgt,
20780 +                                        a->dst_h_parent, isdir);
20781 +       } else {
20782 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
20783 +                       goto out;
20784 +               h_inode = d_inode(a->dst_h_dentry);
20785 +               if (h_inode->i_nlink)
20786 +                       err = au_may_del(a->dst_dentry, a->btgt,
20787 +                                        a->dst_h_parent, isdir);
20788 +       }
20789 +
20790 +out:
20791 +       if (unlikely(err == -ENOENT || err == -EEXIST))
20792 +               err = -EIO;
20793 +       AuTraceErr(err);
20794 +       return err;
20795 +}
20796 +
20797 +/* ---------------------------------------------------------------------- */
20798 +
20799 +/*
20800 + * locking order
20801 + * (VFS)
20802 + * - src_dir and dir by lock_rename()
20803 + * - inode if exitsts
20804 + * (aufs)
20805 + * - lock all
20806 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20807 + *     + si_read_lock
20808 + *     + di_write_lock2_child()
20809 + *       + di_write_lock_child()
20810 + *        + ii_write_lock_child()
20811 + *       + di_write_lock_child2()
20812 + *        + ii_write_lock_child2()
20813 + *     + src_parent and parent
20814 + *       + di_write_lock_parent()
20815 + *        + ii_write_lock_parent()
20816 + *       + di_write_lock_parent2()
20817 + *        + ii_write_lock_parent2()
20818 + *   + lower src_dir and dir by vfsub_lock_rename()
20819 + *   + verify the every relationships between child and parent. if any
20820 + *     of them failed, unlock all and return -EBUSY.
20821 + */
20822 +static void au_ren_unlock(struct au_ren_args *a)
20823 +{
20824 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20825 +                           a->dst_h_parent, a->dst_hdir);
20826 +       if (au_ftest_ren(a->flags, MNT_WRITE))
20827 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
20828 +}
20829 +
20830 +static int au_ren_lock(struct au_ren_args *a)
20831 +{
20832 +       int err;
20833 +       unsigned int udba;
20834 +
20835 +       err = 0;
20836 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20837 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
20838 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20839 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
20840 +
20841 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
20842 +       if (unlikely(err))
20843 +               goto out;
20844 +       au_fset_ren(a->flags, MNT_WRITE);
20845 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20846 +                                     a->dst_h_parent, a->dst_hdir);
20847 +       udba = au_opt_udba(a->src_dentry->d_sb);
20848 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
20849 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
20850 +               err = au_busy_or_stale();
20851 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
20852 +               err = au_h_verify(a->src_h_dentry, udba,
20853 +                                 d_inode(a->src_h_parent), a->src_h_parent,
20854 +                                 a->br);
20855 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
20856 +               err = au_h_verify(a->dst_h_dentry, udba,
20857 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
20858 +                                 a->br);
20859 +       if (!err)
20860 +               goto out; /* success */
20861 +
20862 +       err = au_busy_or_stale();
20863 +       au_ren_unlock(a);
20864 +
20865 +out:
20866 +       return err;
20867 +}
20868 +
20869 +/* ---------------------------------------------------------------------- */
20870 +
20871 +static void au_ren_refresh_dir(struct au_ren_args *a)
20872 +{
20873 +       struct inode *dir;
20874 +
20875 +       dir = a->dst_dir;
20876 +       dir->i_version++;
20877 +       if (au_ftest_ren(a->flags, ISDIR)) {
20878 +               /* is this updating defined in POSIX? */
20879 +               au_cpup_attr_timesizes(a->src_inode);
20880 +               au_cpup_attr_nlink(dir, /*force*/1);
20881 +       }
20882 +
20883 +       au_dir_ts(dir, a->btgt);
20884 +
20885 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20886 +               return;
20887 +
20888 +       dir = a->src_dir;
20889 +       dir->i_version++;
20890 +       if (au_ftest_ren(a->flags, ISDIR))
20891 +               au_cpup_attr_nlink(dir, /*force*/1);
20892 +       au_dir_ts(dir, a->btgt);
20893 +}
20894 +
20895 +static void au_ren_refresh(struct au_ren_args *a)
20896 +{
20897 +       aufs_bindex_t bend, bindex;
20898 +       struct dentry *d, *h_d;
20899 +       struct inode *i, *h_i;
20900 +       struct super_block *sb;
20901 +
20902 +       d = a->dst_dentry;
20903 +       d_drop(d);
20904 +       if (a->h_dst)
20905 +               /* already dget-ed by au_ren_or_cpup() */
20906 +               au_set_h_dptr(d, a->btgt, a->h_dst);
20907 +
20908 +       i = a->dst_inode;
20909 +       if (i) {
20910 +               if (!au_ftest_ren(a->flags, ISDIR))
20911 +                       vfsub_drop_nlink(i);
20912 +               else {
20913 +                       vfsub_dead_dir(i);
20914 +                       au_cpup_attr_timesizes(i);
20915 +               }
20916 +               au_update_dbrange(d, /*do_put_zero*/1);
20917 +       } else {
20918 +               bend = a->btgt;
20919 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
20920 +                       au_set_h_dptr(d, bindex, NULL);
20921 +               bend = au_dbend(d);
20922 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
20923 +                       au_set_h_dptr(d, bindex, NULL);
20924 +               au_update_dbrange(d, /*do_put_zero*/0);
20925 +       }
20926 +
20927 +       d = a->src_dentry;
20928 +       au_set_dbwh(d, -1);
20929 +       bend = au_dbend(d);
20930 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20931 +               h_d = au_h_dptr(d, bindex);
20932 +               if (h_d)
20933 +                       au_set_h_dptr(d, bindex, NULL);
20934 +       }
20935 +       au_set_dbend(d, a->btgt);
20936 +
20937 +       sb = d->d_sb;
20938 +       i = a->src_inode;
20939 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
20940 +               return; /* success */
20941 +
20942 +       bend = au_ibend(i);
20943 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20944 +               h_i = au_h_iptr(i, bindex);
20945 +               if (h_i) {
20946 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
20947 +                       /* ignore this error */
20948 +                       au_set_h_iptr(i, bindex, NULL, 0);
20949 +               }
20950 +       }
20951 +       au_set_ibend(i, a->btgt);
20952 +}
20953 +
20954 +/* ---------------------------------------------------------------------- */
20955 +
20956 +/* mainly for link(2) and rename(2) */
20957 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
20958 +{
20959 +       aufs_bindex_t bdiropq, bwh;
20960 +       struct dentry *parent;
20961 +       struct au_branch *br;
20962 +
20963 +       parent = dentry->d_parent;
20964 +       IMustLock(d_inode(parent)); /* dir is locked */
20965 +
20966 +       bdiropq = au_dbdiropq(parent);
20967 +       bwh = au_dbwh(dentry);
20968 +       br = au_sbr(dentry->d_sb, btgt);
20969 +       if (au_br_rdonly(br)
20970 +           || (0 <= bdiropq && bdiropq < btgt)
20971 +           || (0 <= bwh && bwh < btgt))
20972 +               btgt = -1;
20973 +
20974 +       AuDbg("btgt %d\n", btgt);
20975 +       return btgt;
20976 +}
20977 +
20978 +/* sets src_bstart, dst_bstart and btgt */
20979 +static int au_ren_wbr(struct au_ren_args *a)
20980 +{
20981 +       int err;
20982 +       struct au_wr_dir_args wr_dir_args = {
20983 +               /* .force_btgt  = -1, */
20984 +               .flags          = AuWrDir_ADD_ENTRY
20985 +       };
20986 +
20987 +       a->src_bstart = au_dbstart(a->src_dentry);
20988 +       a->dst_bstart = au_dbstart(a->dst_dentry);
20989 +       if (au_ftest_ren(a->flags, ISDIR))
20990 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
20991 +       wr_dir_args.force_btgt = a->src_bstart;
20992 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
20993 +               wr_dir_args.force_btgt = a->dst_bstart;
20994 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
20995 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
20996 +       a->btgt = err;
20997 +
20998 +       return err;
20999 +}
21000 +
21001 +static void au_ren_dt(struct au_ren_args *a)
21002 +{
21003 +       a->h_path.dentry = a->src_h_parent;
21004 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
21005 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
21006 +               a->h_path.dentry = a->dst_h_parent;
21007 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
21008 +       }
21009 +
21010 +       au_fclr_ren(a->flags, DT_DSTDIR);
21011 +       if (!au_ftest_ren(a->flags, ISDIR))
21012 +               return;
21013 +
21014 +       a->h_path.dentry = a->src_h_dentry;
21015 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
21016 +       if (d_is_positive(a->dst_h_dentry)) {
21017 +               au_fset_ren(a->flags, DT_DSTDIR);
21018 +               a->h_path.dentry = a->dst_h_dentry;
21019 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
21020 +       }
21021 +}
21022 +
21023 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
21024 +{
21025 +       struct dentry *h_d;
21026 +       struct inode *h_inode;
21027 +
21028 +       au_dtime_revert(a->src_dt + AuPARENT);
21029 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
21030 +               au_dtime_revert(a->dst_dt + AuPARENT);
21031 +
21032 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
21033 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
21034 +               h_inode = d_inode(h_d);
21035 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21036 +               au_dtime_revert(a->src_dt + AuCHILD);
21037 +               inode_unlock(h_inode);
21038 +
21039 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
21040 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
21041 +                       h_inode = d_inode(h_d);
21042 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
21043 +                       au_dtime_revert(a->dst_dt + AuCHILD);
21044 +                       inode_unlock(h_inode);
21045 +               }
21046 +       }
21047 +}
21048 +
21049 +/* ---------------------------------------------------------------------- */
21050 +
21051 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
21052 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
21053 +{
21054 +       int err, flags;
21055 +       /* reduce stack space */
21056 +       struct au_ren_args *a;
21057 +
21058 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
21059 +       IMustLock(_src_dir);
21060 +       IMustLock(_dst_dir);
21061 +
21062 +       err = -ENOMEM;
21063 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
21064 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21065 +       if (unlikely(!a))
21066 +               goto out;
21067 +
21068 +       a->src_dir = _src_dir;
21069 +       a->src_dentry = _src_dentry;
21070 +       a->src_inode = NULL;
21071 +       if (d_really_is_positive(a->src_dentry))
21072 +               a->src_inode = d_inode(a->src_dentry);
21073 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
21074 +       a->dst_dir = _dst_dir;
21075 +       a->dst_dentry = _dst_dentry;
21076 +       a->dst_inode = NULL;
21077 +       if (d_really_is_positive(a->dst_dentry))
21078 +               a->dst_inode = d_inode(a->dst_dentry);
21079 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
21080 +       if (a->dst_inode) {
21081 +               IMustLock(a->dst_inode);
21082 +               au_igrab(a->dst_inode);
21083 +       }
21084 +
21085 +       err = -ENOTDIR;
21086 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
21087 +       if (d_is_dir(a->src_dentry)) {
21088 +               au_fset_ren(a->flags, ISDIR);
21089 +               if (unlikely(d_really_is_positive(a->dst_dentry)
21090 +                            && !d_is_dir(a->dst_dentry)))
21091 +                       goto out_free;
21092 +               flags |= AuLock_DIRS;
21093 +       }
21094 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
21095 +       if (unlikely(err))
21096 +               goto out_free;
21097 +
21098 +       err = au_d_hashed_positive(a->src_dentry);
21099 +       if (unlikely(err))
21100 +               goto out_unlock;
21101 +       err = -ENOENT;
21102 +       if (a->dst_inode) {
21103 +               /*
21104 +                * If it is a dir, VFS unhash dst_dentry before this
21105 +                * function. It means we cannot rely upon d_unhashed().
21106 +                */
21107 +               if (unlikely(!a->dst_inode->i_nlink))
21108 +                       goto out_unlock;
21109 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
21110 +                       err = au_d_hashed_positive(a->dst_dentry);
21111 +                       if (unlikely(err))
21112 +                               goto out_unlock;
21113 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21114 +                       goto out_unlock;
21115 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
21116 +               goto out_unlock;
21117 +
21118 +       /*
21119 +        * is it possible?
21120 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
21121 +        * there may exist a problem somewhere else.
21122 +        */
21123 +       err = -EINVAL;
21124 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
21125 +               goto out_unlock;
21126 +
21127 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
21128 +       di_write_lock_parent(a->dst_parent);
21129 +
21130 +       /* which branch we process */
21131 +       err = au_ren_wbr(a);
21132 +       if (unlikely(err < 0))
21133 +               goto out_parent;
21134 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
21135 +       a->h_path.mnt = au_br_mnt(a->br);
21136 +
21137 +       /* are they available to be renamed */
21138 +       err = au_ren_may_dir(a);
21139 +       if (unlikely(err))
21140 +               goto out_children;
21141 +
21142 +       /* prepare the writable parent dir on the same branch */
21143 +       if (a->dst_bstart == a->btgt) {
21144 +               au_fset_ren(a->flags, WHDST);
21145 +       } else {
21146 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
21147 +               if (unlikely(err))
21148 +                       goto out_children;
21149 +       }
21150 +
21151 +       if (a->src_dir != a->dst_dir) {
21152 +               /*
21153 +                * this temporary unlock is safe,
21154 +                * because both dir->i_mutex are locked.
21155 +                */
21156 +               di_write_unlock(a->dst_parent);
21157 +               di_write_lock_parent(a->src_parent);
21158 +               err = au_wr_dir_need_wh(a->src_dentry,
21159 +                                       au_ftest_ren(a->flags, ISDIR),
21160 +                                       &a->btgt);
21161 +               di_write_unlock(a->src_parent);
21162 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
21163 +               au_fclr_ren(a->flags, ISSAMEDIR);
21164 +       } else
21165 +               err = au_wr_dir_need_wh(a->src_dentry,
21166 +                                       au_ftest_ren(a->flags, ISDIR),
21167 +                                       &a->btgt);
21168 +       if (unlikely(err < 0))
21169 +               goto out_children;
21170 +       if (err)
21171 +               au_fset_ren(a->flags, WHSRC);
21172 +
21173 +       /* cpup src */
21174 +       if (a->src_bstart != a->btgt) {
21175 +               struct au_pin pin;
21176 +
21177 +               err = au_pin(&pin, a->src_dentry, a->btgt,
21178 +                            au_opt_udba(a->src_dentry->d_sb),
21179 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21180 +               if (!err) {
21181 +                       struct au_cp_generic cpg = {
21182 +                               .dentry = a->src_dentry,
21183 +                               .bdst   = a->btgt,
21184 +                               .bsrc   = a->src_bstart,
21185 +                               .len    = -1,
21186 +                               .pin    = &pin,
21187 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21188 +                       };
21189 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
21190 +                       err = au_sio_cpup_simple(&cpg);
21191 +                       au_unpin(&pin);
21192 +               }
21193 +               if (unlikely(err))
21194 +                       goto out_children;
21195 +               a->src_bstart = a->btgt;
21196 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21197 +               au_fset_ren(a->flags, WHSRC);
21198 +       }
21199 +
21200 +       /* lock them all */
21201 +       err = au_ren_lock(a);
21202 +       if (unlikely(err))
21203 +               /* leave the copied-up one */
21204 +               goto out_children;
21205 +
21206 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21207 +               err = au_may_ren(a);
21208 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21209 +               err = -ENAMETOOLONG;
21210 +       if (unlikely(err))
21211 +               goto out_hdir;
21212 +
21213 +       /* store timestamps to be revertible */
21214 +       au_ren_dt(a);
21215 +
21216 +       /* here we go */
21217 +       err = do_rename(a);
21218 +       if (unlikely(err))
21219 +               goto out_dt;
21220 +
21221 +       /* update dir attributes */
21222 +       au_ren_refresh_dir(a);
21223 +
21224 +       /* dput/iput all lower dentries */
21225 +       au_ren_refresh(a);
21226 +
21227 +       goto out_hdir; /* success */
21228 +
21229 +out_dt:
21230 +       au_ren_rev_dt(err, a);
21231 +out_hdir:
21232 +       au_ren_unlock(a);
21233 +out_children:
21234 +       au_nhash_wh_free(&a->whlist);
21235 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
21236 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
21237 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21238 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
21239 +       }
21240 +out_parent:
21241 +       if (!err)
21242 +               d_move(a->src_dentry, a->dst_dentry);
21243 +       else {
21244 +               au_update_dbstart(a->dst_dentry);
21245 +               if (!a->dst_inode)
21246 +                       d_drop(a->dst_dentry);
21247 +       }
21248 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
21249 +               di_write_unlock(a->dst_parent);
21250 +       else
21251 +               di_write_unlock2(a->src_parent, a->dst_parent);
21252 +out_unlock:
21253 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21254 +out_free:
21255 +       iput(a->dst_inode);
21256 +       if (a->thargs)
21257 +               au_whtmp_rmdir_free(a->thargs);
21258 +       kfree(a);
21259 +out:
21260 +       AuTraceErr(err);
21261 +       return err;
21262 +}
21263 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21264 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21265 +++ linux/fs/aufs/Kconfig       2016-04-24 18:32:51.390353525 +0200
21266 @@ -0,0 +1,185 @@
21267 +config AUFS_FS
21268 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21269 +       help
21270 +       Aufs is a stackable unification filesystem such as Unionfs,
21271 +       which unifies several directories and provides a merged single
21272 +       directory.
21273 +       In the early days, aufs was entirely re-designed and
21274 +       re-implemented Unionfs Version 1.x series. Introducing many
21275 +       original ideas, approaches and improvements, it becomes totally
21276 +       different from Unionfs while keeping the basic features.
21277 +
21278 +if AUFS_FS
21279 +choice
21280 +       prompt "Maximum number of branches"
21281 +       default AUFS_BRANCH_MAX_127
21282 +       help
21283 +       Specifies the maximum number of branches (or member directories)
21284 +       in a single aufs. The larger value consumes more system
21285 +       resources and has a minor impact to performance.
21286 +config AUFS_BRANCH_MAX_127
21287 +       bool "127"
21288 +       help
21289 +       Specifies the maximum number of branches (or member directories)
21290 +       in a single aufs. The larger value consumes more system
21291 +       resources and has a minor impact to performance.
21292 +config AUFS_BRANCH_MAX_511
21293 +       bool "511"
21294 +       help
21295 +       Specifies the maximum number of branches (or member directories)
21296 +       in a single aufs. The larger value consumes more system
21297 +       resources and has a minor impact to performance.
21298 +config AUFS_BRANCH_MAX_1023
21299 +       bool "1023"
21300 +       help
21301 +       Specifies the maximum number of branches (or member directories)
21302 +       in a single aufs. The larger value consumes more system
21303 +       resources and has a minor impact to performance.
21304 +config AUFS_BRANCH_MAX_32767
21305 +       bool "32767"
21306 +       help
21307 +       Specifies the maximum number of branches (or member directories)
21308 +       in a single aufs. The larger value consumes more system
21309 +       resources and has a minor impact to performance.
21310 +endchoice
21311 +
21312 +config AUFS_SBILIST
21313 +       bool
21314 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21315 +       default y
21316 +       help
21317 +       Automatic configuration for internal use.
21318 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21319 +
21320 +config AUFS_HNOTIFY
21321 +       bool "Detect direct branch access (bypassing aufs)"
21322 +       help
21323 +       If you want to modify files on branches directly, eg. bypassing aufs,
21324 +       and want aufs to detect the changes of them fully, then enable this
21325 +       option and use 'udba=notify' mount option.
21326 +       Currently there is only one available configuration, "fsnotify".
21327 +       It will have a negative impact to the performance.
21328 +       See detail in aufs.5.
21329 +
21330 +choice
21331 +       prompt "method" if AUFS_HNOTIFY
21332 +       default AUFS_HFSNOTIFY
21333 +config AUFS_HFSNOTIFY
21334 +       bool "fsnotify"
21335 +       select FSNOTIFY
21336 +endchoice
21337 +
21338 +config AUFS_EXPORT
21339 +       bool "NFS-exportable aufs"
21340 +       depends on EXPORTFS
21341 +       help
21342 +       If you want to export your mounted aufs via NFS, then enable this
21343 +       option. There are several requirements for this configuration.
21344 +       See detail in aufs.5.
21345 +
21346 +config AUFS_INO_T_64
21347 +       bool
21348 +       depends on AUFS_EXPORT
21349 +       depends on 64BIT && !(ALPHA || S390)
21350 +       default y
21351 +       help
21352 +       Automatic configuration for internal use.
21353 +       /* typedef unsigned long/int __kernel_ino_t */
21354 +       /* alpha and s390x are int */
21355 +
21356 +config AUFS_XATTR
21357 +       bool "support for XATTR/EA (including Security Labels)"
21358 +       help
21359 +       If your branch fs supports XATTR/EA and you want to make them
21360 +       available in aufs too, then enable this opsion and specify the
21361 +       branch attributes for EA.
21362 +       See detail in aufs.5.
21363 +
21364 +config AUFS_FHSM
21365 +       bool "File-based Hierarchical Storage Management"
21366 +       help
21367 +       Hierarchical Storage Management (or HSM) is a well-known feature
21368 +       in the storage world. Aufs provides this feature as file-based.
21369 +       with multiple branches.
21370 +       These multiple branches are prioritized, ie. the topmost one
21371 +       should be the fastest drive and be used heavily.
21372 +
21373 +config AUFS_RDU
21374 +       bool "Readdir in userspace"
21375 +       help
21376 +       Aufs has two methods to provide a merged view for a directory,
21377 +       by a user-space library and by kernel-space natively. The latter
21378 +       is always enabled but sometimes large and slow.
21379 +       If you enable this option, install the library in aufs2-util
21380 +       package, and set some environment variables for your readdir(3),
21381 +       then the work will be handled in user-space which generally
21382 +       shows better performance in most cases.
21383 +       See detail in aufs.5.
21384 +
21385 +config AUFS_SHWH
21386 +       bool "Show whiteouts"
21387 +       help
21388 +       If you want to make the whiteouts in aufs visible, then enable
21389 +       this option and specify 'shwh' mount option. Although it may
21390 +       sounds like philosophy or something, but in technically it
21391 +       simply shows the name of whiteout with keeping its behaviour.
21392 +
21393 +config AUFS_BR_RAMFS
21394 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21395 +       help
21396 +       If you want to use ramfs as an aufs branch fs, then enable this
21397 +       option. Generally tmpfs is recommended.
21398 +       Aufs prohibited them to be a branch fs by default, because
21399 +       initramfs becomes unusable after switch_root or something
21400 +       generally. If you sets initramfs as an aufs branch and boot your
21401 +       system by switch_root, you will meet a problem easily since the
21402 +       files in initramfs may be inaccessible.
21403 +       Unless you are going to use ramfs as an aufs branch fs without
21404 +       switch_root or something, leave it N.
21405 +
21406 +config AUFS_BR_FUSE
21407 +       bool "Fuse fs as an aufs branch"
21408 +       depends on FUSE_FS
21409 +       select AUFS_POLL
21410 +       help
21411 +       If you want to use fuse-based userspace filesystem as an aufs
21412 +       branch fs, then enable this option.
21413 +       It implements the internal poll(2) operation which is
21414 +       implemented by fuse only (curretnly).
21415 +
21416 +config AUFS_POLL
21417 +       bool
21418 +       help
21419 +       Automatic configuration for internal use.
21420 +
21421 +config AUFS_BR_HFSPLUS
21422 +       bool "Hfsplus as an aufs branch"
21423 +       depends on HFSPLUS_FS
21424 +       default y
21425 +       help
21426 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21427 +       this option. This option introduces a small overhead at
21428 +       copying-up a file on hfsplus.
21429 +
21430 +config AUFS_BDEV_LOOP
21431 +       bool
21432 +       depends on BLK_DEV_LOOP
21433 +       default y
21434 +       help
21435 +       Automatic configuration for internal use.
21436 +       Convert =[ym] into =y.
21437 +
21438 +config AUFS_DEBUG
21439 +       bool "Debug aufs"
21440 +       help
21441 +       Enable this to compile aufs internal debug code.
21442 +       It will have a negative impact to the performance.
21443 +
21444 +config AUFS_MAGIC_SYSRQ
21445 +       bool
21446 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21447 +       default y
21448 +       help
21449 +       Automatic configuration for internal use.
21450 +       When aufs supports Magic SysRq, enabled automatically.
21451 +endif
21452 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21453 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21454 +++ linux/fs/aufs/loop.c        2016-04-24 18:32:51.393686895 +0200
21455 @@ -0,0 +1,146 @@
21456 +/*
21457 + * Copyright (C) 2005-2016 Junjiro R. Okajima
21458 + *
21459 + * This program, aufs is free software; you can redistribute it and/or modify
21460 + * it under the terms of the GNU General Public License as published by
21461 + * the Free Software Foundation; either version 2 of the License, or
21462 + * (at your option) any later version.
21463 + *
21464 + * This program is distributed in the hope that it will be useful,
21465 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21466 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21467 + * GNU General Public License for more details.
21468 + *
21469 + * You should have received a copy of the GNU General Public License
21470 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21471 + */
21472 +
21473 +/*
21474 + * support for loopback block device as a branch
21475 + */
21476 +
21477 +#include "aufs.h"
21478 +
21479 +/* added into drivers/block/loop.c */
21480 +static struct file *(*backing_file_func)(struct super_block *sb);
21481 +
21482 +/*
21483 + * test if two lower dentries have overlapping branches.
21484 + */
21485 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21486 +{
21487 +       struct super_block *h_sb;
21488 +       struct file *backing_file;
21489 +
21490 +       if (unlikely(!backing_file_func)) {
21491 +               /* don't load "loop" module here */
21492 +               backing_file_func = symbol_get(loop_backing_file);
21493 +               if (unlikely(!backing_file_func))
21494 +                       /* "loop" module is not loaded */
21495 +                       return 0;
21496 +       }
21497 +
21498 +       h_sb = h_adding->d_sb;
21499 +       backing_file = backing_file_func(h_sb);
21500 +       if (!backing_file)
21501 +               return 0;
21502 +
21503 +       h_adding = backing_file->f_path.dentry;
21504 +       /*
21505 +        * h_adding can be local NFS.
21506 +        * in this case aufs cannot detect the loop.
21507 +        */
21508 +       if (unlikely(h_adding->d_sb == sb))
21509 +               return 1;
21510 +       return !!au_test_subdir(h_adding, sb->s_root);
21511 +}
21512 +
21513 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21514 +int au_test_loopback_kthread(void)
21515 +{
21516 +       int ret;
21517 +       struct task_struct *tsk = current;
21518 +       char c, comm[sizeof(tsk->comm)];
21519 +
21520 +       ret = 0;
21521 +       if (tsk->flags & PF_KTHREAD) {
21522 +               get_task_comm(comm, tsk);
21523 +               c = comm[4];
21524 +               ret = ('0' <= c && c <= '9'
21525 +                      && !strncmp(comm, "loop", 4));
21526 +       }
21527 +
21528 +       return ret;
21529 +}
21530 +
21531 +/* ---------------------------------------------------------------------- */
21532 +
21533 +#define au_warn_loopback_step  16
21534 +static int au_warn_loopback_nelem = au_warn_loopback_step;
21535 +static unsigned long *au_warn_loopback_array;
21536 +
21537 +void au_warn_loopback(struct super_block *h_sb)
21538 +{
21539 +       int i, new_nelem;
21540 +       unsigned long *a, magic;
21541 +       static DEFINE_SPINLOCK(spin);
21542 +
21543 +       magic = h_sb->s_magic;
21544 +       spin_lock(&spin);
21545 +       a = au_warn_loopback_array;
21546 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21547 +               if (a[i] == magic) {
21548 +                       spin_unlock(&spin);
21549 +                       return;
21550 +               }
21551 +
21552 +       /* h_sb is new to us, print it */
21553 +       if (i < au_warn_loopback_nelem) {
21554 +               a[i] = magic;
21555 +               goto pr;
21556 +       }
21557 +
21558 +       /* expand the array */
21559 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21560 +       a = au_kzrealloc(au_warn_loopback_array,
21561 +                        au_warn_loopback_nelem * sizeof(unsigned long),
21562 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21563 +       if (a) {
21564 +               au_warn_loopback_nelem = new_nelem;
21565 +               au_warn_loopback_array = a;
21566 +               a[i] = magic;
21567 +               goto pr;
21568 +       }
21569 +
21570 +       spin_unlock(&spin);
21571 +       AuWarn1("realloc failed, ignored\n");
21572 +       return;
21573 +
21574 +pr:
21575 +       spin_unlock(&spin);
21576 +       pr_warn("you may want to try another patch for loopback file "
21577 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
21578 +}
21579 +
21580 +int au_loopback_init(void)
21581 +{
21582 +       int err;
21583 +       struct super_block *sb __maybe_unused;
21584 +
21585 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
21586 +
21587 +       err = 0;
21588 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21589 +                                        sizeof(unsigned long), GFP_NOFS);
21590 +       if (unlikely(!au_warn_loopback_array))
21591 +               err = -ENOMEM;
21592 +
21593 +       return err;
21594 +}
21595 +
21596 +void au_loopback_fin(void)
21597 +{
21598 +       if (backing_file_func)
21599 +               symbol_put(loop_backing_file);
21600 +       kfree(au_warn_loopback_array);
21601 +}
21602 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21603 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
21604 +++ linux/fs/aufs/loop.h        2016-04-24 18:32:51.393686895 +0200
21605 @@ -0,0 +1,52 @@
21606 +/*
21607 + * Copyright (C) 2005-2016 Junjiro R. Okajima
21608 + *
21609 + * This program, aufs is free software; you can redistribute it and/or modify
21610 + * it under the terms of the GNU General Public License as published by
21611 + * the Free Software Foundation; either version 2 of the License, or
21612 + * (at your option) any later version.
21613 + *
21614 + * This program is distributed in the hope that it will be useful,
21615 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21616 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21617 + * GNU General Public License for more details.
21618 + *
21619 + * You should have received a copy of the GNU General Public License
21620 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21621 + */
21622 +
21623 +/*
21624 + * support for loopback mount as a branch
21625 + */
21626 +
21627 +#ifndef __AUFS_LOOP_H__
21628 +#define __AUFS_LOOP_H__
21629 +
21630 +#ifdef __KERNEL__
21631 +
21632 +struct dentry;
21633 +struct super_block;
21634 +
21635 +#ifdef CONFIG_AUFS_BDEV_LOOP
21636 +/* drivers/block/loop.c */
21637 +struct file *loop_backing_file(struct super_block *sb);
21638 +
21639 +/* loop.c */
21640 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
21641 +int au_test_loopback_kthread(void);
21642 +void au_warn_loopback(struct super_block *h_sb);
21643 +
21644 +int au_loopback_init(void);
21645 +void au_loopback_fin(void);
21646 +#else
21647 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21648 +          struct dentry *h_adding)
21649 +AuStubInt0(au_test_loopback_kthread, void)
21650 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21651 +
21652 +AuStubInt0(au_loopback_init, void)
21653 +AuStubVoid(au_loopback_fin, void)
21654 +#endif /* BLK_DEV_LOOP */
21655 +
21656 +#endif /* __KERNEL__ */
21657 +#endif /* __AUFS_LOOP_H__ */
21658 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21659 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21660 +++ linux/fs/aufs/magic.mk      2016-04-24 18:32:51.393686895 +0200
21661 @@ -0,0 +1,30 @@
21662 +
21663 +# defined in ${srctree}/fs/fuse/inode.c
21664 +# tristate
21665 +ifdef CONFIG_FUSE_FS
21666 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21667 +endif
21668 +
21669 +# defined in ${srctree}/fs/xfs/xfs_sb.h
21670 +# tristate
21671 +ifdef CONFIG_XFS_FS
21672 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
21673 +endif
21674 +
21675 +# defined in ${srctree}/fs/configfs/mount.c
21676 +# tristate
21677 +ifdef CONFIG_CONFIGFS_FS
21678 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21679 +endif
21680 +
21681 +# defined in ${srctree}/fs/ubifs/ubifs.h
21682 +# tristate
21683 +ifdef CONFIG_UBIFS_FS
21684 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21685 +endif
21686 +
21687 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21688 +# tristate
21689 +ifdef CONFIG_HFSPLUS_FS
21690 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21691 +endif
21692 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21693 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
21694 +++ linux/fs/aufs/Makefile      2016-04-24 18:32:51.390353525 +0200
21695 @@ -0,0 +1,44 @@
21696 +
21697 +include ${src}/magic.mk
21698 +ifeq (${CONFIG_AUFS_FS},m)
21699 +include ${src}/conf.mk
21700 +endif
21701 +-include ${src}/priv_def.mk
21702 +
21703 +# cf. include/linux/kernel.h
21704 +# enable pr_debug
21705 +ccflags-y += -DDEBUG
21706 +# sparse requires the full pathname
21707 +ifdef M
21708 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
21709 +else
21710 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
21711 +endif
21712 +
21713 +obj-$(CONFIG_AUFS_FS) += aufs.o
21714 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21715 +       wkq.o vfsub.o dcsub.o \
21716 +       cpup.o whout.o wbr_policy.o \
21717 +       dinfo.o dentry.o \
21718 +       dynop.o \
21719 +       finfo.o file.o f_op.o \
21720 +       dir.o vdir.o \
21721 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
21722 +       mvdown.o ioctl.o
21723 +
21724 +# all are boolean
21725 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
21726 +aufs-$(CONFIG_SYSFS) += sysfs.o
21727 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21728 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21729 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21730 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
21731 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
21732 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21733 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
21734 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
21735 +aufs-$(CONFIG_AUFS_POLL) += poll.o
21736 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
21737 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21738 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21739 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
21740 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21741 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
21742 +++ linux/fs/aufs/module.c      2016-04-24 18:32:51.393686895 +0200
21743 @@ -0,0 +1,221 @@
21744 +/*
21745 + * Copyright (C) 2005-2016 Junjiro R. Okajima
21746 + *
21747 + * This program, aufs is free software; you can redistribute it and/or modify
21748 + * it under the terms of the GNU General Public License as published by
21749 + * the Free Software Foundation; either version 2 of the License, or
21750 + * (at your option) any later version.
21751 + *
21752 + * This program is distributed in the hope that it will be useful,
21753 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21754 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21755 + * GNU General Public License for more details.
21756 + *
21757 + * You should have received a copy of the GNU General Public License
21758 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21759 + */
21760 +
21761 +/*
21762 + * module global variables and operations
21763 + */
21764 +
21765 +#include <linux/module.h>
21766 +#include <linux/seq_file.h>
21767 +#include "aufs.h"
21768 +
21769 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21770 +{
21771 +       if (new_sz <= nused)
21772 +               return p;
21773 +
21774 +       p = krealloc(p, new_sz, gfp);
21775 +       if (p)
21776 +               memset(p + nused, 0, new_sz - nused);
21777 +       return p;
21778 +}
21779 +
21780 +/* ---------------------------------------------------------------------- */
21781 +
21782 +/*
21783 + * aufs caches
21784 + */
21785 +struct kmem_cache *au_cachep[AuCache_Last];
21786 +static int __init au_cache_init(void)
21787 +{
21788 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
21789 +       if (au_cachep[AuCache_DINFO])
21790 +               /* SLAB_DESTROY_BY_RCU */
21791 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21792 +                                                       au_icntnr_init_once);
21793 +       if (au_cachep[AuCache_ICNTNR])
21794 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21795 +                                                      au_fi_init_once);
21796 +       if (au_cachep[AuCache_FINFO])
21797 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21798 +       if (au_cachep[AuCache_VDIR])
21799 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21800 +       if (au_cachep[AuCache_DEHSTR])
21801 +               return 0;
21802 +
21803 +       return -ENOMEM;
21804 +}
21805 +
21806 +static void au_cache_fin(void)
21807 +{
21808 +       int i;
21809 +
21810 +       /*
21811 +        * Make sure all delayed rcu free inodes are flushed before we
21812 +        * destroy cache.
21813 +        */
21814 +       rcu_barrier();
21815 +
21816 +       /* excluding AuCache_HNOTIFY */
21817 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21818 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
21819 +               kmem_cache_destroy(au_cachep[i]);
21820 +               au_cachep[i] = NULL;
21821 +       }
21822 +}
21823 +
21824 +/* ---------------------------------------------------------------------- */
21825 +
21826 +int au_dir_roflags;
21827 +
21828 +#ifdef CONFIG_AUFS_SBILIST
21829 +/*
21830 + * iterate_supers_type() doesn't protect us from
21831 + * remounting (branch management)
21832 + */
21833 +struct au_splhead au_sbilist;
21834 +#endif
21835 +
21836 +struct lock_class_key au_lc_key[AuLcKey_Last];
21837 +
21838 +/*
21839 + * functions for module interface.
21840 + */
21841 +MODULE_LICENSE("GPL");
21842 +/* MODULE_LICENSE("GPL v2"); */
21843 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
21844 +MODULE_DESCRIPTION(AUFS_NAME
21845 +       " -- Advanced multi layered unification filesystem");
21846 +MODULE_VERSION(AUFS_VERSION);
21847 +MODULE_ALIAS_FS(AUFS_NAME);
21848 +
21849 +/* this module parameter has no meaning when SYSFS is disabled */
21850 +int sysaufs_brs = 1;
21851 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21852 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21853 +
21854 +/* this module parameter has no meaning when USER_NS is disabled */
21855 +bool au_userns;
21856 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21857 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21858 +
21859 +/* ---------------------------------------------------------------------- */
21860 +
21861 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21862 +
21863 +int au_seq_path(struct seq_file *seq, struct path *path)
21864 +{
21865 +       int err;
21866 +
21867 +       err = seq_path(seq, path, au_esc_chars);
21868 +       if (err > 0)
21869 +               err = 0;
21870 +       else if (err < 0)
21871 +               err = -ENOMEM;
21872 +
21873 +       return err;
21874 +}
21875 +
21876 +/* ---------------------------------------------------------------------- */
21877 +
21878 +static int __init aufs_init(void)
21879 +{
21880 +       int err, i;
21881 +       char *p;
21882 +
21883 +       p = au_esc_chars;
21884 +       for (i = 1; i <= ' '; i++)
21885 +               *p++ = i;
21886 +       *p++ = '\\';
21887 +       *p++ = '\x7f';
21888 +       *p = 0;
21889 +
21890 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21891 +
21892 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
21893 +       for (i = 0; i < AuIop_Last; i++)
21894 +               aufs_iop_nogetattr[i].getattr = NULL;
21895 +
21896 +       au_sbilist_init();
21897 +       sysaufs_brs_init();
21898 +       au_debug_init();
21899 +       au_dy_init();
21900 +       err = sysaufs_init();
21901 +       if (unlikely(err))
21902 +               goto out;
21903 +       err = au_procfs_init();
21904 +       if (unlikely(err))
21905 +               goto out_sysaufs;
21906 +       err = au_wkq_init();
21907 +       if (unlikely(err))
21908 +               goto out_procfs;
21909 +       err = au_loopback_init();
21910 +       if (unlikely(err))
21911 +               goto out_wkq;
21912 +       err = au_hnotify_init();
21913 +       if (unlikely(err))
21914 +               goto out_loopback;
21915 +       err = au_sysrq_init();
21916 +       if (unlikely(err))
21917 +               goto out_hin;
21918 +       err = au_cache_init();
21919 +       if (unlikely(err))
21920 +               goto out_sysrq;
21921 +
21922 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
21923 +       err = register_filesystem(&aufs_fs_type);
21924 +       if (unlikely(err))
21925 +               goto out_cache;
21926 +
21927 +       /* since we define pr_fmt, call printk directly */
21928 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
21929 +       goto out; /* success */
21930 +
21931 +out_cache:
21932 +       au_cache_fin();
21933 +out_sysrq:
21934 +       au_sysrq_fin();
21935 +out_hin:
21936 +       au_hnotify_fin();
21937 +out_loopback:
21938 +       au_loopback_fin();
21939 +out_wkq:
21940 +       au_wkq_fin();
21941 +out_procfs:
21942 +       au_procfs_fin();
21943 +out_sysaufs:
21944 +       sysaufs_fin();
21945 +       au_dy_fin();
21946 +out:
21947 +       return err;
21948 +}
21949 +
21950 +static void __exit aufs_exit(void)
21951 +{
21952 +       unregister_filesystem(&aufs_fs_type);
21953 +       au_cache_fin();
21954 +       au_sysrq_fin();
21955 +       au_hnotify_fin();
21956 +       au_loopback_fin();
21957 +       au_wkq_fin();
21958 +       au_procfs_fin();
21959 +       sysaufs_fin();
21960 +       au_dy_fin();
21961 +}
21962 +
21963 +module_init(aufs_init);
21964 +module_exit(aufs_exit);
21965 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
21966 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
21967 +++ linux/fs/aufs/module.h      2016-04-24 18:32:51.393686895 +0200
21968 @@ -0,0 +1,105 @@
21969 +/*
21970 + * Copyright (C) 2005-2016 Junjiro R. Okajima
21971 + *
21972 + * This program, aufs is free software; you can redistribute it and/or modify
21973 + * it under the terms of the GNU General Public License as published by
21974 + * the Free Software Foundation; either version 2 of the License, or
21975 + * (at your option) any later version.
21976 + *
21977 + * This program is distributed in the hope that it will be useful,
21978 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21979 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21980 + * GNU General Public License for more details.
21981 + *
21982 + * You should have received a copy of the GNU General Public License
21983 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21984 + */
21985 +
21986 +/*
21987 + * module initialization and module-global
21988 + */
21989 +
21990 +#ifndef __AUFS_MODULE_H__
21991 +#define __AUFS_MODULE_H__
21992 +
21993 +#ifdef __KERNEL__
21994 +
21995 +#include <linux/slab.h>
21996 +
21997 +struct path;
21998 +struct seq_file;
21999 +
22000 +/* module parameters */
22001 +extern int sysaufs_brs;
22002 +extern bool au_userns;
22003 +
22004 +/* ---------------------------------------------------------------------- */
22005 +
22006 +extern int au_dir_roflags;
22007 +
22008 +enum {
22009 +       AuLcNonDir_FIINFO,
22010 +       AuLcNonDir_DIINFO,
22011 +       AuLcNonDir_IIINFO,
22012 +
22013 +       AuLcDir_FIINFO,
22014 +       AuLcDir_DIINFO,
22015 +       AuLcDir_IIINFO,
22016 +
22017 +       AuLcSymlink_DIINFO,
22018 +       AuLcSymlink_IIINFO,
22019 +
22020 +       AuLcKey_Last
22021 +};
22022 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
22023 +
22024 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
22025 +int au_seq_path(struct seq_file *seq, struct path *path);
22026 +
22027 +#ifdef CONFIG_PROC_FS
22028 +/* procfs.c */
22029 +int __init au_procfs_init(void);
22030 +void au_procfs_fin(void);
22031 +#else
22032 +AuStubInt0(au_procfs_init, void);
22033 +AuStubVoid(au_procfs_fin, void);
22034 +#endif
22035 +
22036 +/* ---------------------------------------------------------------------- */
22037 +
22038 +/* kmem cache */
22039 +enum {
22040 +       AuCache_DINFO,
22041 +       AuCache_ICNTNR,
22042 +       AuCache_FINFO,
22043 +       AuCache_VDIR,
22044 +       AuCache_DEHSTR,
22045 +       AuCache_HNOTIFY, /* must be last */
22046 +       AuCache_Last
22047 +};
22048 +
22049 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
22050 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
22051 +#define AuCacheCtor(type, ctor)        \
22052 +       kmem_cache_create(#type, sizeof(struct type), \
22053 +                         __alignof__(struct type), AuCacheFlags, ctor)
22054 +
22055 +extern struct kmem_cache *au_cachep[];
22056 +
22057 +#define AuCacheFuncs(name, index) \
22058 +static inline struct au_##name *au_cache_alloc_##name(void) \
22059 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
22060 +static inline void au_cache_free_##name(struct au_##name *p) \
22061 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
22062 +
22063 +AuCacheFuncs(dinfo, DINFO);
22064 +AuCacheFuncs(icntnr, ICNTNR);
22065 +AuCacheFuncs(finfo, FINFO);
22066 +AuCacheFuncs(vdir, VDIR);
22067 +AuCacheFuncs(vdir_dehstr, DEHSTR);
22068 +#ifdef CONFIG_AUFS_HNOTIFY
22069 +AuCacheFuncs(hnotify, HNOTIFY);
22070 +#endif
22071 +
22072 +#endif /* __KERNEL__ */
22073 +#endif /* __AUFS_MODULE_H__ */
22074 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
22075 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
22076 +++ linux/fs/aufs/mvdown.c      2016-04-24 18:32:51.393686895 +0200
22077 @@ -0,0 +1,703 @@
22078 +/*
22079 + * Copyright (C) 2011-2016 Junjiro R. Okajima
22080 + *
22081 + * This program, aufs is free software; you can redistribute it and/or modify
22082 + * it under the terms of the GNU General Public License as published by
22083 + * the Free Software Foundation; either version 2 of the License, or
22084 + * (at your option) any later version.
22085 + *
22086 + * This program is distributed in the hope that it will be useful,
22087 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22088 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22089 + * GNU General Public License for more details.
22090 + *
22091 + * You should have received a copy of the GNU General Public License
22092 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22093 + */
22094 +
22095 +/*
22096 + * move-down, opposite of copy-up
22097 + */
22098 +
22099 +#include "aufs.h"
22100 +
22101 +struct au_mvd_args {
22102 +       struct {
22103 +               struct super_block *h_sb;
22104 +               struct dentry *h_parent;
22105 +               struct au_hinode *hdir;
22106 +               struct inode *h_dir, *h_inode;
22107 +               struct au_pin pin;
22108 +       } info[AUFS_MVDOWN_NARRAY];
22109 +
22110 +       struct aufs_mvdown mvdown;
22111 +       struct dentry *dentry, *parent;
22112 +       struct inode *inode, *dir;
22113 +       struct super_block *sb;
22114 +       aufs_bindex_t bopq, bwh, bfound;
22115 +       unsigned char rename_lock;
22116 +};
22117 +
22118 +#define mvd_errno              mvdown.au_errno
22119 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22120 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22121 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22122 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
22123 +
22124 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
22125 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
22126 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
22127 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
22128 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
22129 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
22130 +
22131 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
22132 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
22133 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
22134 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
22135 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
22136 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
22137 +
22138 +#define AU_MVD_PR(flag, ...) do {                      \
22139 +               if (flag)                               \
22140 +                       pr_err(__VA_ARGS__);            \
22141 +       } while (0)
22142 +
22143 +static int find_lower_writable(struct au_mvd_args *a)
22144 +{
22145 +       struct super_block *sb;
22146 +       aufs_bindex_t bindex, bend;
22147 +       struct au_branch *br;
22148 +
22149 +       sb = a->sb;
22150 +       bindex = a->mvd_bsrc;
22151 +       bend = au_sbend(sb);
22152 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
22153 +               for (bindex++; bindex <= bend; bindex++) {
22154 +                       br = au_sbr(sb, bindex);
22155 +                       if (au_br_fhsm(br->br_perm)
22156 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22157 +                               return bindex;
22158 +               }
22159 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
22160 +               for (bindex++; bindex <= bend; bindex++) {
22161 +                       br = au_sbr(sb, bindex);
22162 +                       if (!au_br_rdonly(br))
22163 +                               return bindex;
22164 +               }
22165 +       else
22166 +               for (bindex++; bindex <= bend; bindex++) {
22167 +                       br = au_sbr(sb, bindex);
22168 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22169 +                               if (au_br_rdonly(br))
22170 +                                       a->mvdown.flags
22171 +                                               |= AUFS_MVDOWN_ROLOWER_R;
22172 +                               return bindex;
22173 +                       }
22174 +               }
22175 +
22176 +       return -1;
22177 +}
22178 +
22179 +/* make the parent dir on bdst */
22180 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
22181 +{
22182 +       int err;
22183 +
22184 +       err = 0;
22185 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22186 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22187 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22188 +       a->mvd_h_dst_parent = NULL;
22189 +       if (au_dbend(a->parent) >= a->mvd_bdst)
22190 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22191 +       if (!a->mvd_h_dst_parent) {
22192 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22193 +               if (unlikely(err)) {
22194 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
22195 +                       goto out;
22196 +               }
22197 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22198 +       }
22199 +
22200 +out:
22201 +       AuTraceErr(err);
22202 +       return err;
22203 +}
22204 +
22205 +/* lock them all */
22206 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
22207 +{
22208 +       int err;
22209 +       struct dentry *h_trap;
22210 +
22211 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22212 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
22213 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22214 +                    au_opt_udba(a->sb),
22215 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22216 +       AuTraceErr(err);
22217 +       if (unlikely(err)) {
22218 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
22219 +               goto out;
22220 +       }
22221 +
22222 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22223 +               a->rename_lock = 0;
22224 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22225 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22226 +                           au_opt_udba(a->sb),
22227 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22228 +               err = au_do_pin(&a->mvd_pin_src);
22229 +               AuTraceErr(err);
22230 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22231 +               if (unlikely(err)) {
22232 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22233 +                       goto out_dst;
22234 +               }
22235 +               goto out; /* success */
22236 +       }
22237 +
22238 +       a->rename_lock = 1;
22239 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22240 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22241 +                    au_opt_udba(a->sb),
22242 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22243 +       AuTraceErr(err);
22244 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22245 +       if (unlikely(err)) {
22246 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22247 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22248 +               goto out_dst;
22249 +       }
22250 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22251 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22252 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22253 +       if (h_trap) {
22254 +               err = (h_trap != a->mvd_h_src_parent);
22255 +               if (err)
22256 +                       err = (h_trap != a->mvd_h_dst_parent);
22257 +       }
22258 +       BUG_ON(err); /* it should never happen */
22259 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22260 +               err = -EBUSY;
22261 +               AuTraceErr(err);
22262 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22263 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22264 +               au_pin_hdir_lock(&a->mvd_pin_src);
22265 +               au_unpin(&a->mvd_pin_src);
22266 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22267 +               goto out_dst;
22268 +       }
22269 +       goto out; /* success */
22270 +
22271 +out_dst:
22272 +       au_unpin(&a->mvd_pin_dst);
22273 +out:
22274 +       AuTraceErr(err);
22275 +       return err;
22276 +}
22277 +
22278 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22279 +{
22280 +       if (!a->rename_lock)
22281 +               au_unpin(&a->mvd_pin_src);
22282 +       else {
22283 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22284 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22285 +               au_pin_hdir_lock(&a->mvd_pin_src);
22286 +               au_unpin(&a->mvd_pin_src);
22287 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22288 +       }
22289 +       au_unpin(&a->mvd_pin_dst);
22290 +}
22291 +
22292 +/* copy-down the file */
22293 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22294 +{
22295 +       int err;
22296 +       struct au_cp_generic cpg = {
22297 +               .dentry = a->dentry,
22298 +               .bdst   = a->mvd_bdst,
22299 +               .bsrc   = a->mvd_bsrc,
22300 +               .len    = -1,
22301 +               .pin    = &a->mvd_pin_dst,
22302 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22303 +       };
22304 +
22305 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22306 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22307 +               au_fset_cpup(cpg.flags, OVERWRITE);
22308 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22309 +               au_fset_cpup(cpg.flags, RWDST);
22310 +       err = au_sio_cpdown_simple(&cpg);
22311 +       if (unlikely(err))
22312 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22313 +
22314 +       AuTraceErr(err);
22315 +       return err;
22316 +}
22317 +
22318 +/*
22319 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22320 + * were sleeping
22321 + */
22322 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22323 +{
22324 +       int err;
22325 +       struct path h_path;
22326 +       struct au_branch *br;
22327 +       struct inode *delegated;
22328 +
22329 +       br = au_sbr(a->sb, a->mvd_bdst);
22330 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22331 +       err = PTR_ERR(h_path.dentry);
22332 +       if (IS_ERR(h_path.dentry)) {
22333 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22334 +               goto out;
22335 +       }
22336 +
22337 +       err = 0;
22338 +       if (d_is_positive(h_path.dentry)) {
22339 +               h_path.mnt = au_br_mnt(br);
22340 +               delegated = NULL;
22341 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
22342 +                                  &delegated, /*force*/0);
22343 +               if (unlikely(err == -EWOULDBLOCK)) {
22344 +                       pr_warn("cannot retry for NFSv4 delegation"
22345 +                               " for an internal unlink\n");
22346 +                       iput(delegated);
22347 +               }
22348 +               if (unlikely(err))
22349 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22350 +       }
22351 +       dput(h_path.dentry);
22352 +
22353 +out:
22354 +       AuTraceErr(err);
22355 +       return err;
22356 +}
22357 +
22358 +/*
22359 + * unlink the topmost h_dentry
22360 + */
22361 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22362 +{
22363 +       int err;
22364 +       struct path h_path;
22365 +       struct inode *delegated;
22366 +
22367 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22368 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22369 +       delegated = NULL;
22370 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22371 +       if (unlikely(err == -EWOULDBLOCK)) {
22372 +               pr_warn("cannot retry for NFSv4 delegation"
22373 +                       " for an internal unlink\n");
22374 +               iput(delegated);
22375 +       }
22376 +       if (unlikely(err))
22377 +               AU_MVD_PR(dmsg, "unlink failed\n");
22378 +
22379 +       AuTraceErr(err);
22380 +       return err;
22381 +}
22382 +
22383 +/* Since mvdown succeeded, we ignore an error of this function */
22384 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22385 +{
22386 +       int err;
22387 +       struct au_branch *br;
22388 +
22389 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22390 +       br = au_sbr(a->sb, a->mvd_bsrc);
22391 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22392 +       if (!err) {
22393 +               br = au_sbr(a->sb, a->mvd_bdst);
22394 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22395 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22396 +       }
22397 +       if (!err)
22398 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22399 +       else
22400 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22401 +}
22402 +
22403 +/*
22404 + * copy-down the file and unlink the bsrc file.
22405 + * - unlink the bdst whout if exist
22406 + * - copy-down the file (with whtmp name and rename)
22407 + * - unlink the bsrc file
22408 + */
22409 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22410 +{
22411 +       int err;
22412 +
22413 +       err = au_do_mkdir(dmsg, a);
22414 +       if (!err)
22415 +               err = au_do_lock(dmsg, a);
22416 +       if (unlikely(err))
22417 +               goto out;
22418 +
22419 +       /*
22420 +        * do not revert the activities we made on bdst since they should be
22421 +        * harmless in aufs.
22422 +        */
22423 +
22424 +       err = au_do_cpdown(dmsg, a);
22425 +       if (!err)
22426 +               err = au_do_unlink_wh(dmsg, a);
22427 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22428 +               err = au_do_unlink(dmsg, a);
22429 +       if (unlikely(err))
22430 +               goto out_unlock;
22431 +
22432 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22433 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22434 +       if (find_lower_writable(a) < 0)
22435 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22436 +
22437 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22438 +               au_do_stfs(dmsg, a);
22439 +
22440 +       /* maintain internal array */
22441 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22442 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22443 +               au_set_dbstart(a->dentry, a->mvd_bdst);
22444 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22445 +               au_set_ibstart(a->inode, a->mvd_bdst);
22446 +       } else {
22447 +               /* hide the lower */
22448 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
22449 +               au_set_dbend(a->dentry, a->mvd_bsrc);
22450 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
22451 +               au_set_ibend(a->inode, a->mvd_bsrc);
22452 +       }
22453 +       if (au_dbend(a->dentry) < a->mvd_bdst)
22454 +               au_set_dbend(a->dentry, a->mvd_bdst);
22455 +       if (au_ibend(a->inode) < a->mvd_bdst)
22456 +               au_set_ibend(a->inode, a->mvd_bdst);
22457 +
22458 +out_unlock:
22459 +       au_do_unlock(dmsg, a);
22460 +out:
22461 +       AuTraceErr(err);
22462 +       return err;
22463 +}
22464 +
22465 +/* ---------------------------------------------------------------------- */
22466 +
22467 +/* make sure the file is idle */
22468 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
22469 +{
22470 +       int err, plinked;
22471 +
22472 +       err = 0;
22473 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
22474 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
22475 +           && au_dcount(a->dentry) == 1
22476 +           && atomic_read(&a->inode->i_count) == 1
22477 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
22478 +           && (!plinked || !au_plink_test(a->inode))
22479 +           && a->inode->i_nlink == 1)
22480 +               goto out;
22481 +
22482 +       err = -EBUSY;
22483 +       AU_MVD_PR(dmsg,
22484 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
22485 +                 a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
22486 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
22487 +                 a->mvd_h_src_inode->i_nlink,
22488 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
22489 +
22490 +out:
22491 +       AuTraceErr(err);
22492 +       return err;
22493 +}
22494 +
22495 +/* make sure the parent dir is fine */
22496 +static int au_mvd_args_parent(const unsigned char dmsg,
22497 +                             struct au_mvd_args *a)
22498 +{
22499 +       int err;
22500 +       aufs_bindex_t bindex;
22501 +
22502 +       err = 0;
22503 +       if (unlikely(au_alive_dir(a->parent))) {
22504 +               err = -ENOENT;
22505 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
22506 +               goto out;
22507 +       }
22508 +
22509 +       a->bopq = au_dbdiropq(a->parent);
22510 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22511 +       AuDbg("b%d\n", bindex);
22512 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22513 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22514 +               err = -EINVAL;
22515 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
22516 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
22517 +                         a->bopq, a->mvd_bdst);
22518 +       }
22519 +
22520 +out:
22521 +       AuTraceErr(err);
22522 +       return err;
22523 +}
22524 +
22525 +static int au_mvd_args_intermediate(const unsigned char dmsg,
22526 +                                   struct au_mvd_args *a)
22527 +{
22528 +       int err;
22529 +       struct au_dinfo *dinfo, *tmp;
22530 +
22531 +       /* lookup the next lower positive entry */
22532 +       err = -ENOMEM;
22533 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22534 +       if (unlikely(!tmp))
22535 +               goto out;
22536 +
22537 +       a->bfound = -1;
22538 +       a->bwh = -1;
22539 +       dinfo = au_di(a->dentry);
22540 +       au_di_cp(tmp, dinfo);
22541 +       au_di_swap(tmp, dinfo);
22542 +
22543 +       /* returns the number of positive dentries */
22544 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
22545 +       if (!err)
22546 +               a->bwh = au_dbwh(a->dentry);
22547 +       else if (err > 0)
22548 +               a->bfound = au_dbstart(a->dentry);
22549 +
22550 +       au_di_swap(tmp, dinfo);
22551 +       au_rw_write_unlock(&tmp->di_rwsem);
22552 +       au_di_free(tmp);
22553 +       if (unlikely(err < 0))
22554 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
22555 +
22556 +       /*
22557 +        * here, we have these cases.
22558 +        * bfound == -1
22559 +        *      no positive dentry under bsrc. there are more sub-cases.
22560 +        *      bwh < 0
22561 +        *              there no whiteout, we can safely move-down.
22562 +        *      bwh <= bsrc
22563 +        *              impossible
22564 +        *      bsrc < bwh && bwh < bdst
22565 +        *              there is a whiteout on RO branch. cannot proceed.
22566 +        *      bwh == bdst
22567 +        *              there is a whiteout on the RW target branch. it should
22568 +        *              be removed.
22569 +        *      bdst < bwh
22570 +        *              there is a whiteout somewhere unrelated branch.
22571 +        * -1 < bfound && bfound <= bsrc
22572 +        *      impossible.
22573 +        * bfound < bdst
22574 +        *      found, but it is on RO branch between bsrc and bdst. cannot
22575 +        *      proceed.
22576 +        * bfound == bdst
22577 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22578 +        *      error.
22579 +        * bdst < bfound
22580 +        *      found, after we create the file on bdst, it will be hidden.
22581 +        */
22582 +
22583 +       AuDebugOn(a->bfound == -1
22584 +                 && a->bwh != -1
22585 +                 && a->bwh <= a->mvd_bsrc);
22586 +       AuDebugOn(-1 < a->bfound
22587 +                 && a->bfound <= a->mvd_bsrc);
22588 +
22589 +       err = -EINVAL;
22590 +       if (a->bfound == -1
22591 +           && a->mvd_bsrc < a->bwh
22592 +           && a->bwh != -1
22593 +           && a->bwh < a->mvd_bdst) {
22594 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22595 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
22596 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22597 +               goto out;
22598 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
22599 +               a->mvd_errno = EAU_MVDOWN_UPPER;
22600 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
22601 +                         a->mvd_bdst, a->bfound);
22602 +               goto out;
22603 +       }
22604 +
22605 +       err = 0; /* success */
22606 +
22607 +out:
22608 +       AuTraceErr(err);
22609 +       return err;
22610 +}
22611 +
22612 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
22613 +{
22614 +       int err;
22615 +
22616 +       err = 0;
22617 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22618 +           && a->bfound == a->mvd_bdst)
22619 +               err = -EEXIST;
22620 +       AuTraceErr(err);
22621 +       return err;
22622 +}
22623 +
22624 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
22625 +{
22626 +       int err;
22627 +       struct au_branch *br;
22628 +
22629 +       err = -EISDIR;
22630 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
22631 +               goto out;
22632 +
22633 +       err = -EINVAL;
22634 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
22635 +               a->mvd_bsrc = au_ibstart(a->inode);
22636 +       else {
22637 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22638 +               if (unlikely(a->mvd_bsrc < 0
22639 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
22640 +                                || au_dbend(a->dentry) < a->mvd_bsrc
22641 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
22642 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
22643 +                                || au_ibend(a->inode) < a->mvd_bsrc
22644 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22645 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
22646 +                       AU_MVD_PR(dmsg, "no upper\n");
22647 +                       goto out;
22648 +               }
22649 +       }
22650 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
22651 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
22652 +               AU_MVD_PR(dmsg, "on the bottom\n");
22653 +               goto out;
22654 +       }
22655 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
22656 +       br = au_sbr(a->sb, a->mvd_bsrc);
22657 +       err = au_br_rdonly(br);
22658 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22659 +               if (unlikely(err))
22660 +                       goto out;
22661 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22662 +                    || IS_APPEND(a->mvd_h_src_inode))) {
22663 +               if (err)
22664 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22665 +               /* go on */
22666 +       } else
22667 +               goto out;
22668 +
22669 +       err = -EINVAL;
22670 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22671 +               a->mvd_bdst = find_lower_writable(a);
22672 +               if (unlikely(a->mvd_bdst < 0)) {
22673 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
22674 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
22675 +                       goto out;
22676 +               }
22677 +       } else {
22678 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22679 +               if (unlikely(a->mvd_bdst < 0
22680 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
22681 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22682 +                       AU_MVD_PR(dmsg, "no lower brid\n");
22683 +                       goto out;
22684 +               }
22685 +       }
22686 +
22687 +       err = au_mvd_args_busy(dmsg, a);
22688 +       if (!err)
22689 +               err = au_mvd_args_parent(dmsg, a);
22690 +       if (!err)
22691 +               err = au_mvd_args_intermediate(dmsg, a);
22692 +       if (!err)
22693 +               err = au_mvd_args_exist(dmsg, a);
22694 +       if (!err)
22695 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22696 +
22697 +out:
22698 +       AuTraceErr(err);
22699 +       return err;
22700 +}
22701 +
22702 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22703 +{
22704 +       int err, e;
22705 +       unsigned char dmsg;
22706 +       struct au_mvd_args *args;
22707 +       struct inode *inode;
22708 +
22709 +       inode = d_inode(dentry);
22710 +       err = -EPERM;
22711 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
22712 +               goto out;
22713 +
22714 +       err = -ENOMEM;
22715 +       args = kmalloc(sizeof(*args), GFP_NOFS);
22716 +       if (unlikely(!args))
22717 +               goto out;
22718 +
22719 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22720 +       if (!err)
22721 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
22722 +       if (unlikely(err)) {
22723 +               err = -EFAULT;
22724 +               AuTraceErr(err);
22725 +               goto out_free;
22726 +       }
22727 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
22728 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22729 +       args->mvdown.au_errno = 0;
22730 +       args->dentry = dentry;
22731 +       args->inode = inode;
22732 +       args->sb = dentry->d_sb;
22733 +
22734 +       err = -ENOENT;
22735 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22736 +       args->parent = dget_parent(dentry);
22737 +       args->dir = d_inode(args->parent);
22738 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
22739 +       dput(args->parent);
22740 +       if (unlikely(args->parent != dentry->d_parent)) {
22741 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
22742 +               goto out_dir;
22743 +       }
22744 +
22745 +       inode_lock_nested(inode, I_MUTEX_CHILD);
22746 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
22747 +       if (unlikely(err))
22748 +               goto out_inode;
22749 +
22750 +       di_write_lock_parent(args->parent);
22751 +       err = au_mvd_args(dmsg, args);
22752 +       if (unlikely(err))
22753 +               goto out_parent;
22754 +
22755 +       err = au_do_mvdown(dmsg, args);
22756 +       if (unlikely(err))
22757 +               goto out_parent;
22758 +
22759 +       au_cpup_attr_timesizes(args->dir);
22760 +       au_cpup_attr_timesizes(inode);
22761 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
22762 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
22763 +       /* au_digen_dec(dentry); */
22764 +
22765 +out_parent:
22766 +       di_write_unlock(args->parent);
22767 +       aufs_read_unlock(dentry, AuLock_DW);
22768 +out_inode:
22769 +       inode_unlock(inode);
22770 +out_dir:
22771 +       inode_unlock(args->dir);
22772 +out_free:
22773 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22774 +       if (unlikely(e))
22775 +               err = -EFAULT;
22776 +       kfree(args);
22777 +out:
22778 +       AuTraceErr(err);
22779 +       return err;
22780 +}
22781 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22782 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
22783 +++ linux/fs/aufs/opts.c        2016-04-24 18:32:51.393686895 +0200
22784 @@ -0,0 +1,1859 @@
22785 +/*
22786 + * Copyright (C) 2005-2016 Junjiro R. Okajima
22787 + *
22788 + * This program, aufs is free software; you can redistribute it and/or modify
22789 + * it under the terms of the GNU General Public License as published by
22790 + * the Free Software Foundation; either version 2 of the License, or
22791 + * (at your option) any later version.
22792 + *
22793 + * This program is distributed in the hope that it will be useful,
22794 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22795 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22796 + * GNU General Public License for more details.
22797 + *
22798 + * You should have received a copy of the GNU General Public License
22799 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22800 + */
22801 +
22802 +/*
22803 + * mount options/flags
22804 + */
22805 +
22806 +#include <linux/namei.h>
22807 +#include <linux/types.h> /* a distribution requires */
22808 +#include <linux/parser.h>
22809 +#include "aufs.h"
22810 +
22811 +/* ---------------------------------------------------------------------- */
22812 +
22813 +enum {
22814 +       Opt_br,
22815 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22816 +       Opt_idel, Opt_imod,
22817 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22818 +       Opt_rdblk_def, Opt_rdhash_def,
22819 +       Opt_xino, Opt_noxino,
22820 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22821 +       Opt_trunc_xino_path, Opt_itrunc_xino,
22822 +       Opt_trunc_xib, Opt_notrunc_xib,
22823 +       Opt_shwh, Opt_noshwh,
22824 +       Opt_plink, Opt_noplink, Opt_list_plink,
22825 +       Opt_udba,
22826 +       Opt_dio, Opt_nodio,
22827 +       Opt_diropq_a, Opt_diropq_w,
22828 +       Opt_warn_perm, Opt_nowarn_perm,
22829 +       Opt_wbr_copyup, Opt_wbr_create,
22830 +       Opt_fhsm_sec,
22831 +       Opt_verbose, Opt_noverbose,
22832 +       Opt_sum, Opt_nosum, Opt_wsum,
22833 +       Opt_dirperm1, Opt_nodirperm1,
22834 +       Opt_acl, Opt_noacl,
22835 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22836 +};
22837 +
22838 +static match_table_t options = {
22839 +       {Opt_br, "br=%s"},
22840 +       {Opt_br, "br:%s"},
22841 +
22842 +       {Opt_add, "add=%d:%s"},
22843 +       {Opt_add, "add:%d:%s"},
22844 +       {Opt_add, "ins=%d:%s"},
22845 +       {Opt_add, "ins:%d:%s"},
22846 +       {Opt_append, "append=%s"},
22847 +       {Opt_append, "append:%s"},
22848 +       {Opt_prepend, "prepend=%s"},
22849 +       {Opt_prepend, "prepend:%s"},
22850 +
22851 +       {Opt_del, "del=%s"},
22852 +       {Opt_del, "del:%s"},
22853 +       /* {Opt_idel, "idel:%d"}, */
22854 +       {Opt_mod, "mod=%s"},
22855 +       {Opt_mod, "mod:%s"},
22856 +       /* {Opt_imod, "imod:%d:%s"}, */
22857 +
22858 +       {Opt_dirwh, "dirwh=%d"},
22859 +
22860 +       {Opt_xino, "xino=%s"},
22861 +       {Opt_noxino, "noxino"},
22862 +       {Opt_trunc_xino, "trunc_xino"},
22863 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22864 +       {Opt_notrunc_xino, "notrunc_xino"},
22865 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
22866 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
22867 +       /* {Opt_zxino, "zxino=%s"}, */
22868 +       {Opt_trunc_xib, "trunc_xib"},
22869 +       {Opt_notrunc_xib, "notrunc_xib"},
22870 +
22871 +#ifdef CONFIG_PROC_FS
22872 +       {Opt_plink, "plink"},
22873 +#else
22874 +       {Opt_ignore_silent, "plink"},
22875 +#endif
22876 +
22877 +       {Opt_noplink, "noplink"},
22878 +
22879 +#ifdef CONFIG_AUFS_DEBUG
22880 +       {Opt_list_plink, "list_plink"},
22881 +#endif
22882 +
22883 +       {Opt_udba, "udba=%s"},
22884 +
22885 +       {Opt_dio, "dio"},
22886 +       {Opt_nodio, "nodio"},
22887 +
22888 +#ifdef CONFIG_AUFS_FHSM
22889 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
22890 +#else
22891 +       {Opt_ignore_silent, "fhsm_sec=%d"},
22892 +#endif
22893 +
22894 +       {Opt_diropq_a, "diropq=always"},
22895 +       {Opt_diropq_a, "diropq=a"},
22896 +       {Opt_diropq_w, "diropq=whiteouted"},
22897 +       {Opt_diropq_w, "diropq=w"},
22898 +
22899 +       {Opt_warn_perm, "warn_perm"},
22900 +       {Opt_nowarn_perm, "nowarn_perm"},
22901 +
22902 +       /* keep them temporary */
22903 +       {Opt_ignore_silent, "nodlgt"},
22904 +       {Opt_ignore_silent, "clean_plink"},
22905 +
22906 +#ifdef CONFIG_AUFS_SHWH
22907 +       {Opt_shwh, "shwh"},
22908 +#endif
22909 +       {Opt_noshwh, "noshwh"},
22910 +
22911 +       {Opt_dirperm1, "dirperm1"},
22912 +       {Opt_nodirperm1, "nodirperm1"},
22913 +
22914 +       {Opt_verbose, "verbose"},
22915 +       {Opt_verbose, "v"},
22916 +       {Opt_noverbose, "noverbose"},
22917 +       {Opt_noverbose, "quiet"},
22918 +       {Opt_noverbose, "q"},
22919 +       {Opt_noverbose, "silent"},
22920 +
22921 +       {Opt_sum, "sum"},
22922 +       {Opt_nosum, "nosum"},
22923 +       {Opt_wsum, "wsum"},
22924 +
22925 +       {Opt_rdcache, "rdcache=%d"},
22926 +       {Opt_rdblk, "rdblk=%d"},
22927 +       {Opt_rdblk_def, "rdblk=def"},
22928 +       {Opt_rdhash, "rdhash=%d"},
22929 +       {Opt_rdhash_def, "rdhash=def"},
22930 +
22931 +       {Opt_wbr_create, "create=%s"},
22932 +       {Opt_wbr_create, "create_policy=%s"},
22933 +       {Opt_wbr_copyup, "cpup=%s"},
22934 +       {Opt_wbr_copyup, "copyup=%s"},
22935 +       {Opt_wbr_copyup, "copyup_policy=%s"},
22936 +
22937 +       /* generic VFS flag */
22938 +#ifdef CONFIG_FS_POSIX_ACL
22939 +       {Opt_acl, "acl"},
22940 +       {Opt_noacl, "noacl"},
22941 +#else
22942 +       {Opt_ignore_silent, "acl"},
22943 +       {Opt_ignore_silent, "noacl"},
22944 +#endif
22945 +
22946 +       /* internal use for the scripts */
22947 +       {Opt_ignore_silent, "si=%s"},
22948 +
22949 +       {Opt_br, "dirs=%s"},
22950 +       {Opt_ignore, "debug=%d"},
22951 +       {Opt_ignore, "delete=whiteout"},
22952 +       {Opt_ignore, "delete=all"},
22953 +       {Opt_ignore, "imap=%s"},
22954 +
22955 +       /* temporary workaround, due to old mount(8)? */
22956 +       {Opt_ignore_silent, "relatime"},
22957 +
22958 +       {Opt_err, NULL}
22959 +};
22960 +
22961 +/* ---------------------------------------------------------------------- */
22962 +
22963 +static const char *au_parser_pattern(int val, match_table_t tbl)
22964 +{
22965 +       struct match_token *p;
22966 +
22967 +       p = tbl;
22968 +       while (p->pattern) {
22969 +               if (p->token == val)
22970 +                       return p->pattern;
22971 +               p++;
22972 +       }
22973 +       BUG();
22974 +       return "??";
22975 +}
22976 +
22977 +static const char *au_optstr(int *val, match_table_t tbl)
22978 +{
22979 +       struct match_token *p;
22980 +       int v;
22981 +
22982 +       v = *val;
22983 +       if (!v)
22984 +               goto out;
22985 +       p = tbl;
22986 +       while (p->pattern) {
22987 +               if (p->token
22988 +                   && (v & p->token) == p->token) {
22989 +                       *val &= ~p->token;
22990 +                       return p->pattern;
22991 +               }
22992 +               p++;
22993 +       }
22994 +
22995 +out:
22996 +       return NULL;
22997 +}
22998 +
22999 +/* ---------------------------------------------------------------------- */
23000 +
23001 +static match_table_t brperm = {
23002 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
23003 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
23004 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
23005 +       {0, NULL}
23006 +};
23007 +
23008 +static match_table_t brattr = {
23009 +       /* general */
23010 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
23011 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
23012 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
23013 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
23014 +#ifdef CONFIG_AUFS_FHSM
23015 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
23016 +#endif
23017 +#ifdef CONFIG_AUFS_XATTR
23018 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
23019 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
23020 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
23021 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
23022 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
23023 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
23024 +#endif
23025 +
23026 +       /* ro/rr branch */
23027 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
23028 +
23029 +       /* rw branch */
23030 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
23031 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
23032 +
23033 +       {0, NULL}
23034 +};
23035 +
23036 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
23037 +{
23038 +       int attr, v;
23039 +       char *p;
23040 +
23041 +       attr = 0;
23042 +       do {
23043 +               p = strchr(str, '+');
23044 +               if (p)
23045 +                       *p = 0;
23046 +               v = match_token(str, table, args);
23047 +               if (v) {
23048 +                       if (v & AuBrAttr_CMOO_Mask)
23049 +                               attr &= ~AuBrAttr_CMOO_Mask;
23050 +                       attr |= v;
23051 +               } else {
23052 +                       if (p)
23053 +                               *p = '+';
23054 +                       pr_warn("ignored branch attribute %s\n", str);
23055 +                       break;
23056 +               }
23057 +               if (p)
23058 +                       str = p + 1;
23059 +       } while (p);
23060 +
23061 +       return attr;
23062 +}
23063 +
23064 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
23065 +{
23066 +       int sz;
23067 +       const char *p;
23068 +       char *q;
23069 +
23070 +       q = str->a;
23071 +       *q = 0;
23072 +       p = au_optstr(&perm, brattr);
23073 +       if (p) {
23074 +               sz = strlen(p);
23075 +               memcpy(q, p, sz + 1);
23076 +               q += sz;
23077 +       } else
23078 +               goto out;
23079 +
23080 +       do {
23081 +               p = au_optstr(&perm, brattr);
23082 +               if (p) {
23083 +                       *q++ = '+';
23084 +                       sz = strlen(p);
23085 +                       memcpy(q, p, sz + 1);
23086 +                       q += sz;
23087 +               }
23088 +       } while (p);
23089 +
23090 +out:
23091 +       return q - str->a;
23092 +}
23093 +
23094 +static int noinline_for_stack br_perm_val(char *perm)
23095 +{
23096 +       int val, bad, sz;
23097 +       char *p;
23098 +       substring_t args[MAX_OPT_ARGS];
23099 +       au_br_perm_str_t attr;
23100 +
23101 +       p = strchr(perm, '+');
23102 +       if (p)
23103 +               *p = 0;
23104 +       val = match_token(perm, brperm, args);
23105 +       if (!val) {
23106 +               if (p)
23107 +                       *p = '+';
23108 +               pr_warn("ignored branch permission %s\n", perm);
23109 +               val = AuBrPerm_RO;
23110 +               goto out;
23111 +       }
23112 +       if (!p)
23113 +               goto out;
23114 +
23115 +       val |= br_attr_val(p + 1, brattr, args);
23116 +
23117 +       bad = 0;
23118 +       switch (val & AuBrPerm_Mask) {
23119 +       case AuBrPerm_RO:
23120 +       case AuBrPerm_RR:
23121 +               bad = val & AuBrWAttr_Mask;
23122 +               val &= ~AuBrWAttr_Mask;
23123 +               break;
23124 +       case AuBrPerm_RW:
23125 +               bad = val & AuBrRAttr_Mask;
23126 +               val &= ~AuBrRAttr_Mask;
23127 +               break;
23128 +       }
23129 +
23130 +       /*
23131 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23132 +        * does not treat it as an error, just warning.
23133 +        * this is a tiny guard for the user operation.
23134 +        */
23135 +       if (val & AuBrAttr_UNPIN) {
23136 +               bad |= AuBrAttr_UNPIN;
23137 +               val &= ~AuBrAttr_UNPIN;
23138 +       }
23139 +
23140 +       if (unlikely(bad)) {
23141 +               sz = au_do_optstr_br_attr(&attr, bad);
23142 +               AuDebugOn(!sz);
23143 +               pr_warn("ignored branch attribute %s\n", attr.a);
23144 +       }
23145 +
23146 +out:
23147 +       return val;
23148 +}
23149 +
23150 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
23151 +{
23152 +       au_br_perm_str_t attr;
23153 +       const char *p;
23154 +       char *q;
23155 +       int sz;
23156 +
23157 +       q = str->a;
23158 +       p = au_optstr(&perm, brperm);
23159 +       AuDebugOn(!p || !*p);
23160 +       sz = strlen(p);
23161 +       memcpy(q, p, sz + 1);
23162 +       q += sz;
23163 +
23164 +       sz = au_do_optstr_br_attr(&attr, perm);
23165 +       if (sz) {
23166 +               *q++ = '+';
23167 +               memcpy(q, attr.a, sz + 1);
23168 +       }
23169 +
23170 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
23171 +}
23172 +
23173 +/* ---------------------------------------------------------------------- */
23174 +
23175 +static match_table_t udbalevel = {
23176 +       {AuOpt_UDBA_REVAL, "reval"},
23177 +       {AuOpt_UDBA_NONE, "none"},
23178 +#ifdef CONFIG_AUFS_HNOTIFY
23179 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23180 +#ifdef CONFIG_AUFS_HFSNOTIFY
23181 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
23182 +#endif
23183 +#endif
23184 +       {-1, NULL}
23185 +};
23186 +
23187 +static int noinline_for_stack udba_val(char *str)
23188 +{
23189 +       substring_t args[MAX_OPT_ARGS];
23190 +
23191 +       return match_token(str, udbalevel, args);
23192 +}
23193 +
23194 +const char *au_optstr_udba(int udba)
23195 +{
23196 +       return au_parser_pattern(udba, udbalevel);
23197 +}
23198 +
23199 +/* ---------------------------------------------------------------------- */
23200 +
23201 +static match_table_t au_wbr_create_policy = {
23202 +       {AuWbrCreate_TDP, "tdp"},
23203 +       {AuWbrCreate_TDP, "top-down-parent"},
23204 +       {AuWbrCreate_RR, "rr"},
23205 +       {AuWbrCreate_RR, "round-robin"},
23206 +       {AuWbrCreate_MFS, "mfs"},
23207 +       {AuWbrCreate_MFS, "most-free-space"},
23208 +       {AuWbrCreate_MFSV, "mfs:%d"},
23209 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
23210 +
23211 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
23212 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23213 +       {AuWbrCreate_PMFS, "pmfs"},
23214 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
23215 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23216 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
23217 +
23218 +       {-1, NULL}
23219 +};
23220 +
23221 +/*
23222 + * cf. linux/lib/parser.c and cmdline.c
23223 + * gave up calling memparse() since it uses simple_strtoull() instead of
23224 + * kstrto...().
23225 + */
23226 +static int noinline_for_stack
23227 +au_match_ull(substring_t *s, unsigned long long *result)
23228 +{
23229 +       int err;
23230 +       unsigned int len;
23231 +       char a[32];
23232 +
23233 +       err = -ERANGE;
23234 +       len = s->to - s->from;
23235 +       if (len + 1 <= sizeof(a)) {
23236 +               memcpy(a, s->from, len);
23237 +               a[len] = '\0';
23238 +               err = kstrtoull(a, 0, result);
23239 +       }
23240 +       return err;
23241 +}
23242 +
23243 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23244 +                           struct au_opt_wbr_create *create)
23245 +{
23246 +       int err;
23247 +       unsigned long long ull;
23248 +
23249 +       err = 0;
23250 +       if (!au_match_ull(arg, &ull))
23251 +               create->mfsrr_watermark = ull;
23252 +       else {
23253 +               pr_err("bad integer in %s\n", str);
23254 +               err = -EINVAL;
23255 +       }
23256 +
23257 +       return err;
23258 +}
23259 +
23260 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23261 +                         struct au_opt_wbr_create *create)
23262 +{
23263 +       int n, err;
23264 +
23265 +       err = 0;
23266 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23267 +               create->mfs_second = n;
23268 +       else {
23269 +               pr_err("bad integer in %s\n", str);
23270 +               err = -EINVAL;
23271 +       }
23272 +
23273 +       return err;
23274 +}
23275 +
23276 +static int noinline_for_stack
23277 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23278 +{
23279 +       int err, e;
23280 +       substring_t args[MAX_OPT_ARGS];
23281 +
23282 +       err = match_token(str, au_wbr_create_policy, args);
23283 +       create->wbr_create = err;
23284 +       switch (err) {
23285 +       case AuWbrCreate_MFSRRV:
23286 +       case AuWbrCreate_PMFSRRV:
23287 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23288 +               if (!e)
23289 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23290 +               if (unlikely(e))
23291 +                       err = e;
23292 +               break;
23293 +       case AuWbrCreate_MFSRR:
23294 +       case AuWbrCreate_PMFSRR:
23295 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23296 +               if (unlikely(e)) {
23297 +                       err = e;
23298 +                       break;
23299 +               }
23300 +               /*FALLTHROUGH*/
23301 +       case AuWbrCreate_MFS:
23302 +       case AuWbrCreate_PMFS:
23303 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23304 +               break;
23305 +       case AuWbrCreate_MFSV:
23306 +       case AuWbrCreate_PMFSV:
23307 +               e = au_wbr_mfs_sec(&args[0], str, create);
23308 +               if (unlikely(e))
23309 +                       err = e;
23310 +               break;
23311 +       }
23312 +
23313 +       return err;
23314 +}
23315 +
23316 +const char *au_optstr_wbr_create(int wbr_create)
23317 +{
23318 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23319 +}
23320 +
23321 +static match_table_t au_wbr_copyup_policy = {
23322 +       {AuWbrCopyup_TDP, "tdp"},
23323 +       {AuWbrCopyup_TDP, "top-down-parent"},
23324 +       {AuWbrCopyup_BUP, "bup"},
23325 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23326 +       {AuWbrCopyup_BU, "bu"},
23327 +       {AuWbrCopyup_BU, "bottom-up"},
23328 +       {-1, NULL}
23329 +};
23330 +
23331 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23332 +{
23333 +       substring_t args[MAX_OPT_ARGS];
23334 +
23335 +       return match_token(str, au_wbr_copyup_policy, args);
23336 +}
23337 +
23338 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23339 +{
23340 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23341 +}
23342 +
23343 +/* ---------------------------------------------------------------------- */
23344 +
23345 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23346 +
23347 +static void dump_opts(struct au_opts *opts)
23348 +{
23349 +#ifdef CONFIG_AUFS_DEBUG
23350 +       /* reduce stack space */
23351 +       union {
23352 +               struct au_opt_add *add;
23353 +               struct au_opt_del *del;
23354 +               struct au_opt_mod *mod;
23355 +               struct au_opt_xino *xino;
23356 +               struct au_opt_xino_itrunc *xino_itrunc;
23357 +               struct au_opt_wbr_create *create;
23358 +       } u;
23359 +       struct au_opt *opt;
23360 +
23361 +       opt = opts->opt;
23362 +       while (opt->type != Opt_tail) {
23363 +               switch (opt->type) {
23364 +               case Opt_add:
23365 +                       u.add = &opt->add;
23366 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23367 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23368 +                                 u.add->path.dentry);
23369 +                       break;
23370 +               case Opt_del:
23371 +               case Opt_idel:
23372 +                       u.del = &opt->del;
23373 +                       AuDbg("del {%s, %p}\n",
23374 +                             u.del->pathname, u.del->h_path.dentry);
23375 +                       break;
23376 +               case Opt_mod:
23377 +               case Opt_imod:
23378 +                       u.mod = &opt->mod;
23379 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23380 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23381 +                       break;
23382 +               case Opt_append:
23383 +                       u.add = &opt->add;
23384 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23385 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23386 +                                 u.add->path.dentry);
23387 +                       break;
23388 +               case Opt_prepend:
23389 +                       u.add = &opt->add;
23390 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23391 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23392 +                                 u.add->path.dentry);
23393 +                       break;
23394 +               case Opt_dirwh:
23395 +                       AuDbg("dirwh %d\n", opt->dirwh);
23396 +                       break;
23397 +               case Opt_rdcache:
23398 +                       AuDbg("rdcache %d\n", opt->rdcache);
23399 +                       break;
23400 +               case Opt_rdblk:
23401 +                       AuDbg("rdblk %u\n", opt->rdblk);
23402 +                       break;
23403 +               case Opt_rdblk_def:
23404 +                       AuDbg("rdblk_def\n");
23405 +                       break;
23406 +               case Opt_rdhash:
23407 +                       AuDbg("rdhash %u\n", opt->rdhash);
23408 +                       break;
23409 +               case Opt_rdhash_def:
23410 +                       AuDbg("rdhash_def\n");
23411 +                       break;
23412 +               case Opt_xino:
23413 +                       u.xino = &opt->xino;
23414 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23415 +                       break;
23416 +               case Opt_trunc_xino:
23417 +                       AuLabel(trunc_xino);
23418 +                       break;
23419 +               case Opt_notrunc_xino:
23420 +                       AuLabel(notrunc_xino);
23421 +                       break;
23422 +               case Opt_trunc_xino_path:
23423 +               case Opt_itrunc_xino:
23424 +                       u.xino_itrunc = &opt->xino_itrunc;
23425 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23426 +                       break;
23427 +               case Opt_noxino:
23428 +                       AuLabel(noxino);
23429 +                       break;
23430 +               case Opt_trunc_xib:
23431 +                       AuLabel(trunc_xib);
23432 +                       break;
23433 +               case Opt_notrunc_xib:
23434 +                       AuLabel(notrunc_xib);
23435 +                       break;
23436 +               case Opt_shwh:
23437 +                       AuLabel(shwh);
23438 +                       break;
23439 +               case Opt_noshwh:
23440 +                       AuLabel(noshwh);
23441 +                       break;
23442 +               case Opt_dirperm1:
23443 +                       AuLabel(dirperm1);
23444 +                       break;
23445 +               case Opt_nodirperm1:
23446 +                       AuLabel(nodirperm1);
23447 +                       break;
23448 +               case Opt_plink:
23449 +                       AuLabel(plink);
23450 +                       break;
23451 +               case Opt_noplink:
23452 +                       AuLabel(noplink);
23453 +                       break;
23454 +               case Opt_list_plink:
23455 +                       AuLabel(list_plink);
23456 +                       break;
23457 +               case Opt_udba:
23458 +                       AuDbg("udba %d, %s\n",
23459 +                                 opt->udba, au_optstr_udba(opt->udba));
23460 +                       break;
23461 +               case Opt_dio:
23462 +                       AuLabel(dio);
23463 +                       break;
23464 +               case Opt_nodio:
23465 +                       AuLabel(nodio);
23466 +                       break;
23467 +               case Opt_diropq_a:
23468 +                       AuLabel(diropq_a);
23469 +                       break;
23470 +               case Opt_diropq_w:
23471 +                       AuLabel(diropq_w);
23472 +                       break;
23473 +               case Opt_warn_perm:
23474 +                       AuLabel(warn_perm);
23475 +                       break;
23476 +               case Opt_nowarn_perm:
23477 +                       AuLabel(nowarn_perm);
23478 +                       break;
23479 +               case Opt_verbose:
23480 +                       AuLabel(verbose);
23481 +                       break;
23482 +               case Opt_noverbose:
23483 +                       AuLabel(noverbose);
23484 +                       break;
23485 +               case Opt_sum:
23486 +                       AuLabel(sum);
23487 +                       break;
23488 +               case Opt_nosum:
23489 +                       AuLabel(nosum);
23490 +                       break;
23491 +               case Opt_wsum:
23492 +                       AuLabel(wsum);
23493 +                       break;
23494 +               case Opt_wbr_create:
23495 +                       u.create = &opt->wbr_create;
23496 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
23497 +                                 au_optstr_wbr_create(u.create->wbr_create));
23498 +                       switch (u.create->wbr_create) {
23499 +                       case AuWbrCreate_MFSV:
23500 +                       case AuWbrCreate_PMFSV:
23501 +                               AuDbg("%d sec\n", u.create->mfs_second);
23502 +                               break;
23503 +                       case AuWbrCreate_MFSRR:
23504 +                               AuDbg("%llu watermark\n",
23505 +                                         u.create->mfsrr_watermark);
23506 +                               break;
23507 +                       case AuWbrCreate_MFSRRV:
23508 +                       case AuWbrCreate_PMFSRRV:
23509 +                               AuDbg("%llu watermark, %d sec\n",
23510 +                                         u.create->mfsrr_watermark,
23511 +                                         u.create->mfs_second);
23512 +                               break;
23513 +                       }
23514 +                       break;
23515 +               case Opt_wbr_copyup:
23516 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23517 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
23518 +                       break;
23519 +               case Opt_fhsm_sec:
23520 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23521 +                       break;
23522 +               case Opt_acl:
23523 +                       AuLabel(acl);
23524 +                       break;
23525 +               case Opt_noacl:
23526 +                       AuLabel(noacl);
23527 +                       break;
23528 +               default:
23529 +                       BUG();
23530 +               }
23531 +               opt++;
23532 +       }
23533 +#endif
23534 +}
23535 +
23536 +void au_opts_free(struct au_opts *opts)
23537 +{
23538 +       struct au_opt *opt;
23539 +
23540 +       opt = opts->opt;
23541 +       while (opt->type != Opt_tail) {
23542 +               switch (opt->type) {
23543 +               case Opt_add:
23544 +               case Opt_append:
23545 +               case Opt_prepend:
23546 +                       path_put(&opt->add.path);
23547 +                       break;
23548 +               case Opt_del:
23549 +               case Opt_idel:
23550 +                       path_put(&opt->del.h_path);
23551 +                       break;
23552 +               case Opt_mod:
23553 +               case Opt_imod:
23554 +                       dput(opt->mod.h_root);
23555 +                       break;
23556 +               case Opt_xino:
23557 +                       fput(opt->xino.file);
23558 +                       break;
23559 +               }
23560 +               opt++;
23561 +       }
23562 +}
23563 +
23564 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23565 +                  aufs_bindex_t bindex)
23566 +{
23567 +       int err;
23568 +       struct au_opt_add *add = &opt->add;
23569 +       char *p;
23570 +
23571 +       add->bindex = bindex;
23572 +       add->perm = AuBrPerm_RO;
23573 +       add->pathname = opt_str;
23574 +       p = strchr(opt_str, '=');
23575 +       if (p) {
23576 +               *p++ = 0;
23577 +               if (*p)
23578 +                       add->perm = br_perm_val(p);
23579 +       }
23580 +
23581 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23582 +       if (!err) {
23583 +               if (!p) {
23584 +                       add->perm = AuBrPerm_RO;
23585 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
23586 +                               add->perm = AuBrPerm_RR;
23587 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
23588 +                               add->perm = AuBrPerm_RW;
23589 +               }
23590 +               opt->type = Opt_add;
23591 +               goto out;
23592 +       }
23593 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
23594 +       err = -EINVAL;
23595 +
23596 +out:
23597 +       return err;
23598 +}
23599 +
23600 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23601 +{
23602 +       int err;
23603 +
23604 +       del->pathname = args[0].from;
23605 +       AuDbg("del path %s\n", del->pathname);
23606 +
23607 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23608 +       if (unlikely(err))
23609 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
23610 +
23611 +       return err;
23612 +}
23613 +
23614 +#if 0 /* reserved for future use */
23615 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23616 +                             struct au_opt_del *del, substring_t args[])
23617 +{
23618 +       int err;
23619 +       struct dentry *root;
23620 +
23621 +       err = -EINVAL;
23622 +       root = sb->s_root;
23623 +       aufs_read_lock(root, AuLock_FLUSH);
23624 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23625 +               pr_err("out of bounds, %d\n", bindex);
23626 +               goto out;
23627 +       }
23628 +
23629 +       err = 0;
23630 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
23631 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23632 +
23633 +out:
23634 +       aufs_read_unlock(root, !AuLock_IR);
23635 +       return err;
23636 +}
23637 +#endif
23638 +
23639 +static int noinline_for_stack
23640 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
23641 +{
23642 +       int err;
23643 +       struct path path;
23644 +       char *p;
23645 +
23646 +       err = -EINVAL;
23647 +       mod->path = args[0].from;
23648 +       p = strchr(mod->path, '=');
23649 +       if (unlikely(!p)) {
23650 +               pr_err("no permssion %s\n", args[0].from);
23651 +               goto out;
23652 +       }
23653 +
23654 +       *p++ = 0;
23655 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23656 +       if (unlikely(err)) {
23657 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
23658 +               goto out;
23659 +       }
23660 +
23661 +       mod->perm = br_perm_val(p);
23662 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23663 +       mod->h_root = dget(path.dentry);
23664 +       path_put(&path);
23665 +
23666 +out:
23667 +       return err;
23668 +}
23669 +
23670 +#if 0 /* reserved for future use */
23671 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23672 +                             struct au_opt_mod *mod, substring_t args[])
23673 +{
23674 +       int err;
23675 +       struct dentry *root;
23676 +
23677 +       err = -EINVAL;
23678 +       root = sb->s_root;
23679 +       aufs_read_lock(root, AuLock_FLUSH);
23680 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23681 +               pr_err("out of bounds, %d\n", bindex);
23682 +               goto out;
23683 +       }
23684 +
23685 +       err = 0;
23686 +       mod->perm = br_perm_val(args[1].from);
23687 +       AuDbg("mod path %s, perm 0x%x, %s\n",
23688 +             mod->path, mod->perm, args[1].from);
23689 +       mod->h_root = dget(au_h_dptr(root, bindex));
23690 +
23691 +out:
23692 +       aufs_read_unlock(root, !AuLock_IR);
23693 +       return err;
23694 +}
23695 +#endif
23696 +
23697 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23698 +                             substring_t args[])
23699 +{
23700 +       int err;
23701 +       struct file *file;
23702 +
23703 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
23704 +       err = PTR_ERR(file);
23705 +       if (IS_ERR(file))
23706 +               goto out;
23707 +
23708 +       err = -EINVAL;
23709 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
23710 +               fput(file);
23711 +               pr_err("%s must be outside\n", args[0].from);
23712 +               goto out;
23713 +       }
23714 +
23715 +       err = 0;
23716 +       xino->file = file;
23717 +       xino->path = args[0].from;
23718 +
23719 +out:
23720 +       return err;
23721 +}
23722 +
23723 +static int noinline_for_stack
23724 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
23725 +                              struct au_opt_xino_itrunc *xino_itrunc,
23726 +                              substring_t args[])
23727 +{
23728 +       int err;
23729 +       aufs_bindex_t bend, bindex;
23730 +       struct path path;
23731 +       struct dentry *root;
23732 +
23733 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23734 +       if (unlikely(err)) {
23735 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
23736 +               goto out;
23737 +       }
23738 +
23739 +       xino_itrunc->bindex = -1;
23740 +       root = sb->s_root;
23741 +       aufs_read_lock(root, AuLock_FLUSH);
23742 +       bend = au_sbend(sb);
23743 +       for (bindex = 0; bindex <= bend; bindex++) {
23744 +               if (au_h_dptr(root, bindex) == path.dentry) {
23745 +                       xino_itrunc->bindex = bindex;
23746 +                       break;
23747 +               }
23748 +       }
23749 +       aufs_read_unlock(root, !AuLock_IR);
23750 +       path_put(&path);
23751 +
23752 +       if (unlikely(xino_itrunc->bindex < 0)) {
23753 +               pr_err("no such branch %s\n", args[0].from);
23754 +               err = -EINVAL;
23755 +       }
23756 +
23757 +out:
23758 +       return err;
23759 +}
23760 +
23761 +/* called without aufs lock */
23762 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23763 +{
23764 +       int err, n, token;
23765 +       aufs_bindex_t bindex;
23766 +       unsigned char skipped;
23767 +       struct dentry *root;
23768 +       struct au_opt *opt, *opt_tail;
23769 +       char *opt_str;
23770 +       /* reduce the stack space */
23771 +       union {
23772 +               struct au_opt_xino_itrunc *xino_itrunc;
23773 +               struct au_opt_wbr_create *create;
23774 +       } u;
23775 +       struct {
23776 +               substring_t args[MAX_OPT_ARGS];
23777 +       } *a;
23778 +
23779 +       err = -ENOMEM;
23780 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23781 +       if (unlikely(!a))
23782 +               goto out;
23783 +
23784 +       root = sb->s_root;
23785 +       err = 0;
23786 +       bindex = 0;
23787 +       opt = opts->opt;
23788 +       opt_tail = opt + opts->max_opt - 1;
23789 +       opt->type = Opt_tail;
23790 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23791 +               err = -EINVAL;
23792 +               skipped = 0;
23793 +               token = match_token(opt_str, options, a->args);
23794 +               switch (token) {
23795 +               case Opt_br:
23796 +                       err = 0;
23797 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23798 +                              && *opt_str) {
23799 +                               err = opt_add(opt, opt_str, opts->sb_flags,
23800 +                                             bindex++);
23801 +                               if (unlikely(!err && ++opt > opt_tail)) {
23802 +                                       err = -E2BIG;
23803 +                                       break;
23804 +                               }
23805 +                               opt->type = Opt_tail;
23806 +                               skipped = 1;
23807 +                       }
23808 +                       break;
23809 +               case Opt_add:
23810 +                       if (unlikely(match_int(&a->args[0], &n))) {
23811 +                               pr_err("bad integer in %s\n", opt_str);
23812 +                               break;
23813 +                       }
23814 +                       bindex = n;
23815 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
23816 +                                     bindex);
23817 +                       if (!err)
23818 +                               opt->type = token;
23819 +                       break;
23820 +               case Opt_append:
23821 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23822 +                                     /*dummy bindex*/1);
23823 +                       if (!err)
23824 +                               opt->type = token;
23825 +                       break;
23826 +               case Opt_prepend:
23827 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23828 +                                     /*bindex*/0);
23829 +                       if (!err)
23830 +                               opt->type = token;
23831 +                       break;
23832 +               case Opt_del:
23833 +                       err = au_opts_parse_del(&opt->del, a->args);
23834 +                       if (!err)
23835 +                               opt->type = token;
23836 +                       break;
23837 +#if 0 /* reserved for future use */
23838 +               case Opt_idel:
23839 +                       del->pathname = "(indexed)";
23840 +                       if (unlikely(match_int(&args[0], &n))) {
23841 +                               pr_err("bad integer in %s\n", opt_str);
23842 +                               break;
23843 +                       }
23844 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23845 +                       if (!err)
23846 +                               opt->type = token;
23847 +                       break;
23848 +#endif
23849 +               case Opt_mod:
23850 +                       err = au_opts_parse_mod(&opt->mod, a->args);
23851 +                       if (!err)
23852 +                               opt->type = token;
23853 +                       break;
23854 +#ifdef IMOD /* reserved for future use */
23855 +               case Opt_imod:
23856 +                       u.mod->path = "(indexed)";
23857 +                       if (unlikely(match_int(&a->args[0], &n))) {
23858 +                               pr_err("bad integer in %s\n", opt_str);
23859 +                               break;
23860 +                       }
23861 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23862 +                       if (!err)
23863 +                               opt->type = token;
23864 +                       break;
23865 +#endif
23866 +               case Opt_xino:
23867 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
23868 +                       if (!err)
23869 +                               opt->type = token;
23870 +                       break;
23871 +
23872 +               case Opt_trunc_xino_path:
23873 +                       err = au_opts_parse_xino_itrunc_path
23874 +                               (sb, &opt->xino_itrunc, a->args);
23875 +                       if (!err)
23876 +                               opt->type = token;
23877 +                       break;
23878 +
23879 +               case Opt_itrunc_xino:
23880 +                       u.xino_itrunc = &opt->xino_itrunc;
23881 +                       if (unlikely(match_int(&a->args[0], &n))) {
23882 +                               pr_err("bad integer in %s\n", opt_str);
23883 +                               break;
23884 +                       }
23885 +                       u.xino_itrunc->bindex = n;
23886 +                       aufs_read_lock(root, AuLock_FLUSH);
23887 +                       if (n < 0 || au_sbend(sb) < n) {
23888 +                               pr_err("out of bounds, %d\n", n);
23889 +                               aufs_read_unlock(root, !AuLock_IR);
23890 +                               break;
23891 +                       }
23892 +                       aufs_read_unlock(root, !AuLock_IR);
23893 +                       err = 0;
23894 +                       opt->type = token;
23895 +                       break;
23896 +
23897 +               case Opt_dirwh:
23898 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23899 +                               break;
23900 +                       err = 0;
23901 +                       opt->type = token;
23902 +                       break;
23903 +
23904 +               case Opt_rdcache:
23905 +                       if (unlikely(match_int(&a->args[0], &n))) {
23906 +                               pr_err("bad integer in %s\n", opt_str);
23907 +                               break;
23908 +                       }
23909 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
23910 +                               pr_err("rdcache must be smaller than %d\n",
23911 +                                      AUFS_RDCACHE_MAX);
23912 +                               break;
23913 +                       }
23914 +                       opt->rdcache = n;
23915 +                       err = 0;
23916 +                       opt->type = token;
23917 +                       break;
23918 +               case Opt_rdblk:
23919 +                       if (unlikely(match_int(&a->args[0], &n)
23920 +                                    || n < 0
23921 +                                    || n > KMALLOC_MAX_SIZE)) {
23922 +                               pr_err("bad integer in %s\n", opt_str);
23923 +                               break;
23924 +                       }
23925 +                       if (unlikely(n && n < NAME_MAX)) {
23926 +                               pr_err("rdblk must be larger than %d\n",
23927 +                                      NAME_MAX);
23928 +                               break;
23929 +                       }
23930 +                       opt->rdblk = n;
23931 +                       err = 0;
23932 +                       opt->type = token;
23933 +                       break;
23934 +               case Opt_rdhash:
23935 +                       if (unlikely(match_int(&a->args[0], &n)
23936 +                                    || n < 0
23937 +                                    || n * sizeof(struct hlist_head)
23938 +                                    > KMALLOC_MAX_SIZE)) {
23939 +                               pr_err("bad integer in %s\n", opt_str);
23940 +                               break;
23941 +                       }
23942 +                       opt->rdhash = n;
23943 +                       err = 0;
23944 +                       opt->type = token;
23945 +                       break;
23946 +
23947 +               case Opt_trunc_xino:
23948 +               case Opt_notrunc_xino:
23949 +               case Opt_noxino:
23950 +               case Opt_trunc_xib:
23951 +               case Opt_notrunc_xib:
23952 +               case Opt_shwh:
23953 +               case Opt_noshwh:
23954 +               case Opt_dirperm1:
23955 +               case Opt_nodirperm1:
23956 +               case Opt_plink:
23957 +               case Opt_noplink:
23958 +               case Opt_list_plink:
23959 +               case Opt_dio:
23960 +               case Opt_nodio:
23961 +               case Opt_diropq_a:
23962 +               case Opt_diropq_w:
23963 +               case Opt_warn_perm:
23964 +               case Opt_nowarn_perm:
23965 +               case Opt_verbose:
23966 +               case Opt_noverbose:
23967 +               case Opt_sum:
23968 +               case Opt_nosum:
23969 +               case Opt_wsum:
23970 +               case Opt_rdblk_def:
23971 +               case Opt_rdhash_def:
23972 +               case Opt_acl:
23973 +               case Opt_noacl:
23974 +                       err = 0;
23975 +                       opt->type = token;
23976 +                       break;
23977 +
23978 +               case Opt_udba:
23979 +                       opt->udba = udba_val(a->args[0].from);
23980 +                       if (opt->udba >= 0) {
23981 +                               err = 0;
23982 +                               opt->type = token;
23983 +                       } else
23984 +                               pr_err("wrong value, %s\n", opt_str);
23985 +                       break;
23986 +
23987 +               case Opt_wbr_create:
23988 +                       u.create = &opt->wbr_create;
23989 +                       u.create->wbr_create
23990 +                               = au_wbr_create_val(a->args[0].from, u.create);
23991 +                       if (u.create->wbr_create >= 0) {
23992 +                               err = 0;
23993 +                               opt->type = token;
23994 +                       } else
23995 +                               pr_err("wrong value, %s\n", opt_str);
23996 +                       break;
23997 +               case Opt_wbr_copyup:
23998 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
23999 +                       if (opt->wbr_copyup >= 0) {
24000 +                               err = 0;
24001 +                               opt->type = token;
24002 +                       } else
24003 +                               pr_err("wrong value, %s\n", opt_str);
24004 +                       break;
24005 +
24006 +               case Opt_fhsm_sec:
24007 +                       if (unlikely(match_int(&a->args[0], &n)
24008 +                                    || n < 0)) {
24009 +                               pr_err("bad integer in %s\n", opt_str);
24010 +                               break;
24011 +                       }
24012 +                       if (sysaufs_brs) {
24013 +                               opt->fhsm_second = n;
24014 +                               opt->type = token;
24015 +                       } else
24016 +                               pr_warn("ignored %s\n", opt_str);
24017 +                       err = 0;
24018 +                       break;
24019 +
24020 +               case Opt_ignore:
24021 +                       pr_warn("ignored %s\n", opt_str);
24022 +                       /*FALLTHROUGH*/
24023 +               case Opt_ignore_silent:
24024 +                       skipped = 1;
24025 +                       err = 0;
24026 +                       break;
24027 +               case Opt_err:
24028 +                       pr_err("unknown option %s\n", opt_str);
24029 +                       break;
24030 +               }
24031 +
24032 +               if (!err && !skipped) {
24033 +                       if (unlikely(++opt > opt_tail)) {
24034 +                               err = -E2BIG;
24035 +                               opt--;
24036 +                               opt->type = Opt_tail;
24037 +                               break;
24038 +                       }
24039 +                       opt->type = Opt_tail;
24040 +               }
24041 +       }
24042 +
24043 +       kfree(a);
24044 +       dump_opts(opts);
24045 +       if (unlikely(err))
24046 +               au_opts_free(opts);
24047 +
24048 +out:
24049 +       return err;
24050 +}
24051 +
24052 +static int au_opt_wbr_create(struct super_block *sb,
24053 +                            struct au_opt_wbr_create *create)
24054 +{
24055 +       int err;
24056 +       struct au_sbinfo *sbinfo;
24057 +
24058 +       SiMustWriteLock(sb);
24059 +
24060 +       err = 1; /* handled */
24061 +       sbinfo = au_sbi(sb);
24062 +       if (sbinfo->si_wbr_create_ops->fin) {
24063 +               err = sbinfo->si_wbr_create_ops->fin(sb);
24064 +               if (!err)
24065 +                       err = 1;
24066 +       }
24067 +
24068 +       sbinfo->si_wbr_create = create->wbr_create;
24069 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
24070 +       switch (create->wbr_create) {
24071 +       case AuWbrCreate_MFSRRV:
24072 +       case AuWbrCreate_MFSRR:
24073 +       case AuWbrCreate_PMFSRR:
24074 +       case AuWbrCreate_PMFSRRV:
24075 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
24076 +               /*FALLTHROUGH*/
24077 +       case AuWbrCreate_MFS:
24078 +       case AuWbrCreate_MFSV:
24079 +       case AuWbrCreate_PMFS:
24080 +       case AuWbrCreate_PMFSV:
24081 +               sbinfo->si_wbr_mfs.mfs_expire
24082 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
24083 +               break;
24084 +       }
24085 +
24086 +       if (sbinfo->si_wbr_create_ops->init)
24087 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
24088 +
24089 +       return err;
24090 +}
24091 +
24092 +/*
24093 + * returns,
24094 + * plus: processed without an error
24095 + * zero: unprocessed
24096 + */
24097 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
24098 +                        struct au_opts *opts)
24099 +{
24100 +       int err;
24101 +       struct au_sbinfo *sbinfo;
24102 +
24103 +       SiMustWriteLock(sb);
24104 +
24105 +       err = 1; /* handled */
24106 +       sbinfo = au_sbi(sb);
24107 +       switch (opt->type) {
24108 +       case Opt_udba:
24109 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24110 +               sbinfo->si_mntflags |= opt->udba;
24111 +               opts->given_udba |= opt->udba;
24112 +               break;
24113 +
24114 +       case Opt_plink:
24115 +               au_opt_set(sbinfo->si_mntflags, PLINK);
24116 +               break;
24117 +       case Opt_noplink:
24118 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24119 +                       au_plink_put(sb, /*verbose*/1);
24120 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
24121 +               break;
24122 +       case Opt_list_plink:
24123 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24124 +                       au_plink_list(sb);
24125 +               break;
24126 +
24127 +       case Opt_dio:
24128 +               au_opt_set(sbinfo->si_mntflags, DIO);
24129 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24130 +               break;
24131 +       case Opt_nodio:
24132 +               au_opt_clr(sbinfo->si_mntflags, DIO);
24133 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24134 +               break;
24135 +
24136 +       case Opt_fhsm_sec:
24137 +               au_fhsm_set(sbinfo, opt->fhsm_second);
24138 +               break;
24139 +
24140 +       case Opt_diropq_a:
24141 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24142 +               break;
24143 +       case Opt_diropq_w:
24144 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24145 +               break;
24146 +
24147 +       case Opt_warn_perm:
24148 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24149 +               break;
24150 +       case Opt_nowarn_perm:
24151 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24152 +               break;
24153 +
24154 +       case Opt_verbose:
24155 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
24156 +               break;
24157 +       case Opt_noverbose:
24158 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24159 +               break;
24160 +
24161 +       case Opt_sum:
24162 +               au_opt_set(sbinfo->si_mntflags, SUM);
24163 +               break;
24164 +       case Opt_wsum:
24165 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24166 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
24167 +       case Opt_nosum:
24168 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24169 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
24170 +               break;
24171 +
24172 +       case Opt_wbr_create:
24173 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
24174 +               break;
24175 +       case Opt_wbr_copyup:
24176 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
24177 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24178 +               break;
24179 +
24180 +       case Opt_dirwh:
24181 +               sbinfo->si_dirwh = opt->dirwh;
24182 +               break;
24183 +
24184 +       case Opt_rdcache:
24185 +               sbinfo->si_rdcache
24186 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
24187 +               break;
24188 +       case Opt_rdblk:
24189 +               sbinfo->si_rdblk = opt->rdblk;
24190 +               break;
24191 +       case Opt_rdblk_def:
24192 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24193 +               break;
24194 +       case Opt_rdhash:
24195 +               sbinfo->si_rdhash = opt->rdhash;
24196 +               break;
24197 +       case Opt_rdhash_def:
24198 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24199 +               break;
24200 +
24201 +       case Opt_shwh:
24202 +               au_opt_set(sbinfo->si_mntflags, SHWH);
24203 +               break;
24204 +       case Opt_noshwh:
24205 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
24206 +               break;
24207 +
24208 +       case Opt_dirperm1:
24209 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24210 +               break;
24211 +       case Opt_nodirperm1:
24212 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24213 +               break;
24214 +
24215 +       case Opt_trunc_xino:
24216 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24217 +               break;
24218 +       case Opt_notrunc_xino:
24219 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24220 +               break;
24221 +
24222 +       case Opt_trunc_xino_path:
24223 +       case Opt_itrunc_xino:
24224 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24225 +               if (!err)
24226 +                       err = 1;
24227 +               break;
24228 +
24229 +       case Opt_trunc_xib:
24230 +               au_fset_opts(opts->flags, TRUNC_XIB);
24231 +               break;
24232 +       case Opt_notrunc_xib:
24233 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24234 +               break;
24235 +
24236 +       case Opt_acl:
24237 +               sb->s_flags |= MS_POSIXACL;
24238 +               break;
24239 +       case Opt_noacl:
24240 +               sb->s_flags &= ~MS_POSIXACL;
24241 +               break;
24242 +
24243 +       default:
24244 +               err = 0;
24245 +               break;
24246 +       }
24247 +
24248 +       return err;
24249 +}
24250 +
24251 +/*
24252 + * returns tri-state.
24253 + * plus: processed without an error
24254 + * zero: unprocessed
24255 + * minus: error
24256 + */
24257 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24258 +                    struct au_opts *opts)
24259 +{
24260 +       int err, do_refresh;
24261 +
24262 +       err = 0;
24263 +       switch (opt->type) {
24264 +       case Opt_append:
24265 +               opt->add.bindex = au_sbend(sb) + 1;
24266 +               if (opt->add.bindex < 0)
24267 +                       opt->add.bindex = 0;
24268 +               goto add;
24269 +       case Opt_prepend:
24270 +               opt->add.bindex = 0;
24271 +       add: /* indented label */
24272 +       case Opt_add:
24273 +               err = au_br_add(sb, &opt->add,
24274 +                               au_ftest_opts(opts->flags, REMOUNT));
24275 +               if (!err) {
24276 +                       err = 1;
24277 +                       au_fset_opts(opts->flags, REFRESH);
24278 +               }
24279 +               break;
24280 +
24281 +       case Opt_del:
24282 +       case Opt_idel:
24283 +               err = au_br_del(sb, &opt->del,
24284 +                               au_ftest_opts(opts->flags, REMOUNT));
24285 +               if (!err) {
24286 +                       err = 1;
24287 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24288 +                       au_fset_opts(opts->flags, REFRESH);
24289 +               }
24290 +               break;
24291 +
24292 +       case Opt_mod:
24293 +       case Opt_imod:
24294 +               err = au_br_mod(sb, &opt->mod,
24295 +                               au_ftest_opts(opts->flags, REMOUNT),
24296 +                               &do_refresh);
24297 +               if (!err) {
24298 +                       err = 1;
24299 +                       if (do_refresh)
24300 +                               au_fset_opts(opts->flags, REFRESH);
24301 +               }
24302 +               break;
24303 +       }
24304 +
24305 +       return err;
24306 +}
24307 +
24308 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24309 +                      struct au_opt_xino **opt_xino,
24310 +                      struct au_opts *opts)
24311 +{
24312 +       int err;
24313 +       aufs_bindex_t bend, bindex;
24314 +       struct dentry *root, *parent, *h_root;
24315 +
24316 +       err = 0;
24317 +       switch (opt->type) {
24318 +       case Opt_xino:
24319 +               err = au_xino_set(sb, &opt->xino,
24320 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24321 +               if (unlikely(err))
24322 +                       break;
24323 +
24324 +               *opt_xino = &opt->xino;
24325 +               au_xino_brid_set(sb, -1);
24326 +
24327 +               /* safe d_parent access */
24328 +               parent = opt->xino.file->f_path.dentry->d_parent;
24329 +               root = sb->s_root;
24330 +               bend = au_sbend(sb);
24331 +               for (bindex = 0; bindex <= bend; bindex++) {
24332 +                       h_root = au_h_dptr(root, bindex);
24333 +                       if (h_root == parent) {
24334 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24335 +                               break;
24336 +                       }
24337 +               }
24338 +               break;
24339 +
24340 +       case Opt_noxino:
24341 +               au_xino_clr(sb);
24342 +               au_xino_brid_set(sb, -1);
24343 +               *opt_xino = (void *)-1;
24344 +               break;
24345 +       }
24346 +
24347 +       return err;
24348 +}
24349 +
24350 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24351 +                  unsigned int pending)
24352 +{
24353 +       int err, fhsm;
24354 +       aufs_bindex_t bindex, bend;
24355 +       unsigned char do_plink, skip, do_free, can_no_dreval;
24356 +       struct au_branch *br;
24357 +       struct au_wbr *wbr;
24358 +       struct dentry *root, *dentry;
24359 +       struct inode *dir, *h_dir;
24360 +       struct au_sbinfo *sbinfo;
24361 +       struct au_hinode *hdir;
24362 +
24363 +       SiMustAnyLock(sb);
24364 +
24365 +       sbinfo = au_sbi(sb);
24366 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24367 +
24368 +       if (!(sb_flags & MS_RDONLY)) {
24369 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24370 +                       pr_warn("first branch should be rw\n");
24371 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24372 +                       pr_warn_once("shwh should be used with ro\n");
24373 +       }
24374 +
24375 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24376 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24377 +               pr_warn_once("udba=*notify requires xino\n");
24378 +
24379 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24380 +               pr_warn_once("dirperm1 breaks the protection"
24381 +                            " by the permission bits on the lower branch\n");
24382 +
24383 +       err = 0;
24384 +       fhsm = 0;
24385 +       root = sb->s_root;
24386 +       dir = d_inode(root);
24387 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24388 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24389 +                                     UDBA_NONE);
24390 +       bend = au_sbend(sb);
24391 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24392 +               skip = 0;
24393 +               h_dir = au_h_iptr(dir, bindex);
24394 +               br = au_sbr(sb, bindex);
24395 +
24396 +               if ((br->br_perm & AuBrAttr_ICEX)
24397 +                   && !h_dir->i_op->listxattr)
24398 +                       br->br_perm &= ~AuBrAttr_ICEX;
24399 +#if 0
24400 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24401 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24402 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24403 +#endif
24404 +
24405 +               do_free = 0;
24406 +               wbr = br->br_wbr;
24407 +               if (wbr)
24408 +                       wbr_wh_read_lock(wbr);
24409 +
24410 +               if (!au_br_writable(br->br_perm)) {
24411 +                       do_free = !!wbr;
24412 +                       skip = (!wbr
24413 +                               || (!wbr->wbr_whbase
24414 +                                   && !wbr->wbr_plink
24415 +                                   && !wbr->wbr_orph));
24416 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24417 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24418 +                       skip = (!wbr || !wbr->wbr_whbase);
24419 +                       if (skip && wbr) {
24420 +                               if (do_plink)
24421 +                                       skip = !!wbr->wbr_plink;
24422 +                               else
24423 +                                       skip = !wbr->wbr_plink;
24424 +                       }
24425 +               } else {
24426 +                       /* skip = (br->br_whbase && br->br_ohph); */
24427 +                       skip = (wbr && wbr->wbr_whbase);
24428 +                       if (skip) {
24429 +                               if (do_plink)
24430 +                                       skip = !!wbr->wbr_plink;
24431 +                               else
24432 +                                       skip = !wbr->wbr_plink;
24433 +                       }
24434 +               }
24435 +               if (wbr)
24436 +                       wbr_wh_read_unlock(wbr);
24437 +
24438 +               if (can_no_dreval) {
24439 +                       dentry = br->br_path.dentry;
24440 +                       spin_lock(&dentry->d_lock);
24441 +                       if (dentry->d_flags &
24442 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24443 +                               can_no_dreval = 0;
24444 +                       spin_unlock(&dentry->d_lock);
24445 +               }
24446 +
24447 +               if (au_br_fhsm(br->br_perm)) {
24448 +                       fhsm++;
24449 +                       AuDebugOn(!br->br_fhsm);
24450 +               }
24451 +
24452 +               if (skip)
24453 +                       continue;
24454 +
24455 +               hdir = au_hi(dir, bindex);
24456 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
24457 +               if (wbr)
24458 +                       wbr_wh_write_lock(wbr);
24459 +               err = au_wh_init(br, sb);
24460 +               if (wbr)
24461 +                       wbr_wh_write_unlock(wbr);
24462 +               au_hn_imtx_unlock(hdir);
24463 +
24464 +               if (!err && do_free) {
24465 +                       kfree(wbr);
24466 +                       br->br_wbr = NULL;
24467 +               }
24468 +       }
24469 +
24470 +       if (can_no_dreval)
24471 +               au_fset_si(sbinfo, NO_DREVAL);
24472 +       else
24473 +               au_fclr_si(sbinfo, NO_DREVAL);
24474 +
24475 +       if (fhsm >= 2) {
24476 +               au_fset_si(sbinfo, FHSM);
24477 +               for (bindex = bend; bindex >= 0; bindex--) {
24478 +                       br = au_sbr(sb, bindex);
24479 +                       if (au_br_fhsm(br->br_perm)) {
24480 +                               au_fhsm_set_bottom(sb, bindex);
24481 +                               break;
24482 +                       }
24483 +               }
24484 +       } else {
24485 +               au_fclr_si(sbinfo, FHSM);
24486 +               au_fhsm_set_bottom(sb, -1);
24487 +       }
24488 +
24489 +       return err;
24490 +}
24491 +
24492 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24493 +{
24494 +       int err;
24495 +       unsigned int tmp;
24496 +       aufs_bindex_t bindex, bend;
24497 +       struct au_opt *opt;
24498 +       struct au_opt_xino *opt_xino, xino;
24499 +       struct au_sbinfo *sbinfo;
24500 +       struct au_branch *br;
24501 +       struct inode *dir;
24502 +
24503 +       SiMustWriteLock(sb);
24504 +
24505 +       err = 0;
24506 +       opt_xino = NULL;
24507 +       opt = opts->opt;
24508 +       while (err >= 0 && opt->type != Opt_tail)
24509 +               err = au_opt_simple(sb, opt++, opts);
24510 +       if (err > 0)
24511 +               err = 0;
24512 +       else if (unlikely(err < 0))
24513 +               goto out;
24514 +
24515 +       /* disable xino and udba temporary */
24516 +       sbinfo = au_sbi(sb);
24517 +       tmp = sbinfo->si_mntflags;
24518 +       au_opt_clr(sbinfo->si_mntflags, XINO);
24519 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24520 +
24521 +       opt = opts->opt;
24522 +       while (err >= 0 && opt->type != Opt_tail)
24523 +               err = au_opt_br(sb, opt++, opts);
24524 +       if (err > 0)
24525 +               err = 0;
24526 +       else if (unlikely(err < 0))
24527 +               goto out;
24528 +
24529 +       bend = au_sbend(sb);
24530 +       if (unlikely(bend < 0)) {
24531 +               err = -EINVAL;
24532 +               pr_err("no branches\n");
24533 +               goto out;
24534 +       }
24535 +
24536 +       if (au_opt_test(tmp, XINO))
24537 +               au_opt_set(sbinfo->si_mntflags, XINO);
24538 +       opt = opts->opt;
24539 +       while (!err && opt->type != Opt_tail)
24540 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
24541 +       if (unlikely(err))
24542 +               goto out;
24543 +
24544 +       err = au_opts_verify(sb, sb->s_flags, tmp);
24545 +       if (unlikely(err))
24546 +               goto out;
24547 +
24548 +       /* restore xino */
24549 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
24550 +               xino.file = au_xino_def(sb);
24551 +               err = PTR_ERR(xino.file);
24552 +               if (IS_ERR(xino.file))
24553 +                       goto out;
24554 +
24555 +               err = au_xino_set(sb, &xino, /*remount*/0);
24556 +               fput(xino.file);
24557 +               if (unlikely(err))
24558 +                       goto out;
24559 +       }
24560 +
24561 +       /* restore udba */
24562 +       tmp &= AuOptMask_UDBA;
24563 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24564 +       sbinfo->si_mntflags |= tmp;
24565 +       bend = au_sbend(sb);
24566 +       for (bindex = 0; bindex <= bend; bindex++) {
24567 +               br = au_sbr(sb, bindex);
24568 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
24569 +               if (unlikely(err))
24570 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24571 +                               bindex, err);
24572 +               /* go on even if err */
24573 +       }
24574 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
24575 +               dir = d_inode(sb->s_root);
24576 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
24577 +       }
24578 +
24579 +out:
24580 +       return err;
24581 +}
24582 +
24583 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24584 +{
24585 +       int err, rerr;
24586 +       unsigned char no_dreval;
24587 +       struct inode *dir;
24588 +       struct au_opt_xino *opt_xino;
24589 +       struct au_opt *opt;
24590 +       struct au_sbinfo *sbinfo;
24591 +
24592 +       SiMustWriteLock(sb);
24593 +
24594 +       err = 0;
24595 +       dir = d_inode(sb->s_root);
24596 +       sbinfo = au_sbi(sb);
24597 +       opt_xino = NULL;
24598 +       opt = opts->opt;
24599 +       while (err >= 0 && opt->type != Opt_tail) {
24600 +               err = au_opt_simple(sb, opt, opts);
24601 +               if (!err)
24602 +                       err = au_opt_br(sb, opt, opts);
24603 +               if (!err)
24604 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
24605 +               opt++;
24606 +       }
24607 +       if (err > 0)
24608 +               err = 0;
24609 +       AuTraceErr(err);
24610 +       /* go on even err */
24611 +
24612 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
24613 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24614 +       if (unlikely(rerr && !err))
24615 +               err = rerr;
24616 +
24617 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
24618 +               au_fset_opts(opts->flags, REFRESH_IDOP);
24619 +
24620 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24621 +               rerr = au_xib_trunc(sb);
24622 +               if (unlikely(rerr && !err))
24623 +                       err = rerr;
24624 +       }
24625 +
24626 +       /* will be handled by the caller */
24627 +       if (!au_ftest_opts(opts->flags, REFRESH)
24628 +           && (opts->given_udba
24629 +               || au_opt_test(sbinfo->si_mntflags, XINO)
24630 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
24631 +                   ))
24632 +               au_fset_opts(opts->flags, REFRESH);
24633 +
24634 +       AuDbg("status 0x%x\n", opts->flags);
24635 +       return err;
24636 +}
24637 +
24638 +/* ---------------------------------------------------------------------- */
24639 +
24640 +unsigned int au_opt_udba(struct super_block *sb)
24641 +{
24642 +       return au_mntflags(sb) & AuOptMask_UDBA;
24643 +}
24644 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24645 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
24646 +++ linux/fs/aufs/opts.h        2016-04-24 18:32:51.393686895 +0200
24647 @@ -0,0 +1,211 @@
24648 +/*
24649 + * Copyright (C) 2005-2016 Junjiro R. Okajima
24650 + *
24651 + * This program, aufs is free software; you can redistribute it and/or modify
24652 + * it under the terms of the GNU General Public License as published by
24653 + * the Free Software Foundation; either version 2 of the License, or
24654 + * (at your option) any later version.
24655 + *
24656 + * This program is distributed in the hope that it will be useful,
24657 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24658 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24659 + * GNU General Public License for more details.
24660 + *
24661 + * You should have received a copy of the GNU General Public License
24662 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24663 + */
24664 +
24665 +/*
24666 + * mount options/flags
24667 + */
24668 +
24669 +#ifndef __AUFS_OPTS_H__
24670 +#define __AUFS_OPTS_H__
24671 +
24672 +#ifdef __KERNEL__
24673 +
24674 +#include <linux/path.h>
24675 +
24676 +struct file;
24677 +struct super_block;
24678 +
24679 +/* ---------------------------------------------------------------------- */
24680 +
24681 +/* mount flags */
24682 +#define AuOpt_XINO             1               /* external inode number bitmap
24683 +                                                  and translation table */
24684 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
24685 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
24686 +#define AuOpt_UDBA_REVAL       (1 << 3)
24687 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
24688 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
24689 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
24690 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
24691 +                                                  bits */
24692 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
24693 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
24694 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
24695 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
24696 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
24697 +#define AuOpt_DIO              (1 << 14)       /* direct io */
24698 +
24699 +#ifndef CONFIG_AUFS_HNOTIFY
24700 +#undef AuOpt_UDBA_HNOTIFY
24701 +#define AuOpt_UDBA_HNOTIFY     0
24702 +#endif
24703 +#ifndef CONFIG_AUFS_SHWH
24704 +#undef AuOpt_SHWH
24705 +#define AuOpt_SHWH             0
24706 +#endif
24707 +
24708 +#define AuOpt_Def      (AuOpt_XINO \
24709 +                        | AuOpt_UDBA_REVAL \
24710 +                        | AuOpt_PLINK \
24711 +                        /* | AuOpt_DIRPERM1 */ \
24712 +                        | AuOpt_WARN_PERM)
24713 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24714 +                        | AuOpt_UDBA_REVAL \
24715 +                        | AuOpt_UDBA_HNOTIFY)
24716 +
24717 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
24718 +#define au_opt_set(flags, name) do { \
24719 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24720 +       ((flags) |= AuOpt_##name); \
24721 +} while (0)
24722 +#define au_opt_set_udba(flags, name) do { \
24723 +       (flags) &= ~AuOptMask_UDBA; \
24724 +       ((flags) |= AuOpt_##name); \
24725 +} while (0)
24726 +#define au_opt_clr(flags, name) do { \
24727 +       ((flags) &= ~AuOpt_##name); \
24728 +} while (0)
24729 +
24730 +static inline unsigned int au_opts_plink(unsigned int mntflags)
24731 +{
24732 +#ifdef CONFIG_PROC_FS
24733 +       return mntflags;
24734 +#else
24735 +       return mntflags & ~AuOpt_PLINK;
24736 +#endif
24737 +}
24738 +
24739 +/* ---------------------------------------------------------------------- */
24740 +
24741 +/* policies to select one among multiple writable branches */
24742 +enum {
24743 +       AuWbrCreate_TDP,        /* top down parent */
24744 +       AuWbrCreate_RR,         /* round robin */
24745 +       AuWbrCreate_MFS,        /* most free space */
24746 +       AuWbrCreate_MFSV,       /* mfs with seconds */
24747 +       AuWbrCreate_MFSRR,      /* mfs then rr */
24748 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
24749 +       AuWbrCreate_PMFS,       /* parent and mfs */
24750 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
24751 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
24752 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
24753 +
24754 +       AuWbrCreate_Def = AuWbrCreate_TDP
24755 +};
24756 +
24757 +enum {
24758 +       AuWbrCopyup_TDP,        /* top down parent */
24759 +       AuWbrCopyup_BUP,        /* bottom up parent */
24760 +       AuWbrCopyup_BU,         /* bottom up */
24761 +
24762 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
24763 +};
24764 +
24765 +/* ---------------------------------------------------------------------- */
24766 +
24767 +struct au_opt_add {
24768 +       aufs_bindex_t   bindex;
24769 +       char            *pathname;
24770 +       int             perm;
24771 +       struct path     path;
24772 +};
24773 +
24774 +struct au_opt_del {
24775 +       char            *pathname;
24776 +       struct path     h_path;
24777 +};
24778 +
24779 +struct au_opt_mod {
24780 +       char            *path;
24781 +       int             perm;
24782 +       struct dentry   *h_root;
24783 +};
24784 +
24785 +struct au_opt_xino {
24786 +       char            *path;
24787 +       struct file     *file;
24788 +};
24789 +
24790 +struct au_opt_xino_itrunc {
24791 +       aufs_bindex_t   bindex;
24792 +};
24793 +
24794 +struct au_opt_wbr_create {
24795 +       int                     wbr_create;
24796 +       int                     mfs_second;
24797 +       unsigned long long      mfsrr_watermark;
24798 +};
24799 +
24800 +struct au_opt {
24801 +       int type;
24802 +       union {
24803 +               struct au_opt_xino      xino;
24804 +               struct au_opt_xino_itrunc xino_itrunc;
24805 +               struct au_opt_add       add;
24806 +               struct au_opt_del       del;
24807 +               struct au_opt_mod       mod;
24808 +               int                     dirwh;
24809 +               int                     rdcache;
24810 +               unsigned int            rdblk;
24811 +               unsigned int            rdhash;
24812 +               int                     udba;
24813 +               struct au_opt_wbr_create wbr_create;
24814 +               int                     wbr_copyup;
24815 +               unsigned int            fhsm_second;
24816 +       };
24817 +};
24818 +
24819 +/* opts flags */
24820 +#define AuOpts_REMOUNT         1
24821 +#define AuOpts_REFRESH         (1 << 1)
24822 +#define AuOpts_TRUNC_XIB       (1 << 2)
24823 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
24824 +#define AuOpts_REFRESH_IDOP    (1 << 4)
24825 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
24826 +#define au_fset_opts(flags, name) \
24827 +       do { (flags) |= AuOpts_##name; } while (0)
24828 +#define au_fclr_opts(flags, name) \
24829 +       do { (flags) &= ~AuOpts_##name; } while (0)
24830 +
24831 +struct au_opts {
24832 +       struct au_opt   *opt;
24833 +       int             max_opt;
24834 +
24835 +       unsigned int    given_udba;
24836 +       unsigned int    flags;
24837 +       unsigned long   sb_flags;
24838 +};
24839 +
24840 +/* ---------------------------------------------------------------------- */
24841 +
24842 +/* opts.c */
24843 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
24844 +const char *au_optstr_udba(int udba);
24845 +const char *au_optstr_wbr_copyup(int wbr_copyup);
24846 +const char *au_optstr_wbr_create(int wbr_create);
24847 +
24848 +void au_opts_free(struct au_opts *opts);
24849 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24850 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24851 +                  unsigned int pending);
24852 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24853 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24854 +
24855 +unsigned int au_opt_udba(struct super_block *sb);
24856 +
24857 +#endif /* __KERNEL__ */
24858 +#endif /* __AUFS_OPTS_H__ */
24859 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24860 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
24861 +++ linux/fs/aufs/plink.c       2016-04-24 18:32:51.393686895 +0200
24862 @@ -0,0 +1,528 @@
24863 +/*
24864 + * Copyright (C) 2005-2016 Junjiro R. Okajima
24865 + *
24866 + * This program, aufs is free software; you can redistribute it and/or modify
24867 + * it under the terms of the GNU General Public License as published by
24868 + * the Free Software Foundation; either version 2 of the License, or
24869 + * (at your option) any later version.
24870 + *
24871 + * This program is distributed in the hope that it will be useful,
24872 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24873 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24874 + * GNU General Public License for more details.
24875 + *
24876 + * You should have received a copy of the GNU General Public License
24877 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24878 + */
24879 +
24880 +/*
24881 + * pseudo-link
24882 + */
24883 +
24884 +#include "aufs.h"
24885 +
24886 +/*
24887 + * the pseudo-link maintenance mode.
24888 + * during a user process maintains the pseudo-links,
24889 + * prohibit adding a new plink and branch manipulation.
24890 + *
24891 + * Flags
24892 + * NOPLM:
24893 + *     For entry functions which will handle plink, and i_mutex is already held
24894 + *     in VFS.
24895 + *     They cannot wait and should return an error at once.
24896 + *     Callers has to check the error.
24897 + * NOPLMW:
24898 + *     For entry functions which will handle plink, but i_mutex is not held
24899 + *     in VFS.
24900 + *     They can wait the plink maintenance mode to finish.
24901 + *
24902 + * They behave like F_SETLK and F_SETLKW.
24903 + * If the caller never handle plink, then both flags are unnecessary.
24904 + */
24905 +
24906 +int au_plink_maint(struct super_block *sb, int flags)
24907 +{
24908 +       int err;
24909 +       pid_t pid, ppid;
24910 +       struct au_sbinfo *sbi;
24911 +
24912 +       SiMustAnyLock(sb);
24913 +
24914 +       err = 0;
24915 +       if (!au_opt_test(au_mntflags(sb), PLINK))
24916 +               goto out;
24917 +
24918 +       sbi = au_sbi(sb);
24919 +       pid = sbi->si_plink_maint_pid;
24920 +       if (!pid || pid == current->pid)
24921 +               goto out;
24922 +
24923 +       /* todo: it highly depends upon /sbin/mount.aufs */
24924 +       rcu_read_lock();
24925 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
24926 +       rcu_read_unlock();
24927 +       if (pid == ppid)
24928 +               goto out;
24929 +
24930 +       if (au_ftest_lock(flags, NOPLMW)) {
24931 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
24932 +               /* AuDebugOn(!lockdep_depth(current)); */
24933 +               while (sbi->si_plink_maint_pid) {
24934 +                       si_read_unlock(sb);
24935 +                       /* gave up wake_up_bit() */
24936 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
24937 +
24938 +                       if (au_ftest_lock(flags, FLUSH))
24939 +                               au_nwt_flush(&sbi->si_nowait);
24940 +                       si_noflush_read_lock(sb);
24941 +               }
24942 +       } else if (au_ftest_lock(flags, NOPLM)) {
24943 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
24944 +               err = -EAGAIN;
24945 +       }
24946 +
24947 +out:
24948 +       return err;
24949 +}
24950 +
24951 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
24952 +{
24953 +       spin_lock(&sbinfo->si_plink_maint_lock);
24954 +       sbinfo->si_plink_maint_pid = 0;
24955 +       spin_unlock(&sbinfo->si_plink_maint_lock);
24956 +       wake_up_all(&sbinfo->si_plink_wq);
24957 +}
24958 +
24959 +int au_plink_maint_enter(struct super_block *sb)
24960 +{
24961 +       int err;
24962 +       struct au_sbinfo *sbinfo;
24963 +
24964 +       err = 0;
24965 +       sbinfo = au_sbi(sb);
24966 +       /* make sure i am the only one in this fs */
24967 +       si_write_lock(sb, AuLock_FLUSH);
24968 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
24969 +               spin_lock(&sbinfo->si_plink_maint_lock);
24970 +               if (!sbinfo->si_plink_maint_pid)
24971 +                       sbinfo->si_plink_maint_pid = current->pid;
24972 +               else
24973 +                       err = -EBUSY;
24974 +               spin_unlock(&sbinfo->si_plink_maint_lock);
24975 +       }
24976 +       si_write_unlock(sb);
24977 +
24978 +       return err;
24979 +}
24980 +
24981 +/* ---------------------------------------------------------------------- */
24982 +
24983 +#ifdef CONFIG_AUFS_DEBUG
24984 +void au_plink_list(struct super_block *sb)
24985 +{
24986 +       int i;
24987 +       struct au_sbinfo *sbinfo;
24988 +       struct hlist_head *plink_hlist;
24989 +       struct pseudo_link *plink;
24990 +
24991 +       SiMustAnyLock(sb);
24992 +
24993 +       sbinfo = au_sbi(sb);
24994 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24995 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24996 +
24997 +       for (i = 0; i < AuPlink_NHASH; i++) {
24998 +               plink_hlist = &sbinfo->si_plink[i].head;
24999 +               rcu_read_lock();
25000 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
25001 +                       AuDbg("%lu\n", plink->inode->i_ino);
25002 +               rcu_read_unlock();
25003 +       }
25004 +}
25005 +#endif
25006 +
25007 +/* is the inode pseudo-linked? */
25008 +int au_plink_test(struct inode *inode)
25009 +{
25010 +       int found, i;
25011 +       struct au_sbinfo *sbinfo;
25012 +       struct hlist_head *plink_hlist;
25013 +       struct pseudo_link *plink;
25014 +
25015 +       sbinfo = au_sbi(inode->i_sb);
25016 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
25017 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
25018 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25019 +
25020 +       found = 0;
25021 +       i = au_plink_hash(inode->i_ino);
25022 +       plink_hlist = &sbinfo->si_plink[i].head;
25023 +       rcu_read_lock();
25024 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
25025 +               if (plink->inode == inode) {
25026 +                       found = 1;
25027 +                       break;
25028 +               }
25029 +       rcu_read_unlock();
25030 +       return found;
25031 +}
25032 +
25033 +/* ---------------------------------------------------------------------- */
25034 +
25035 +/*
25036 + * generate a name for plink.
25037 + * the file will be stored under AUFS_WH_PLINKDIR.
25038 + */
25039 +/* 20 is max digits length of ulong 64 */
25040 +#define PLINK_NAME_LEN ((20 + 1) * 2)
25041 +
25042 +static int plink_name(char *name, int len, struct inode *inode,
25043 +                     aufs_bindex_t bindex)
25044 +{
25045 +       int rlen;
25046 +       struct inode *h_inode;
25047 +
25048 +       h_inode = au_h_iptr(inode, bindex);
25049 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
25050 +       return rlen;
25051 +}
25052 +
25053 +struct au_do_plink_lkup_args {
25054 +       struct dentry **errp;
25055 +       struct qstr *tgtname;
25056 +       struct dentry *h_parent;
25057 +       struct au_branch *br;
25058 +};
25059 +
25060 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
25061 +                                      struct dentry *h_parent,
25062 +                                      struct au_branch *br)
25063 +{
25064 +       struct dentry *h_dentry;
25065 +       struct inode *h_inode;
25066 +
25067 +       h_inode = d_inode(h_parent);
25068 +       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
25069 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
25070 +       inode_unlock(h_inode);
25071 +       return h_dentry;
25072 +}
25073 +
25074 +static void au_call_do_plink_lkup(void *args)
25075 +{
25076 +       struct au_do_plink_lkup_args *a = args;
25077 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
25078 +}
25079 +
25080 +/* lookup the plink-ed @inode under the branch at @bindex */
25081 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
25082 +{
25083 +       struct dentry *h_dentry, *h_parent;
25084 +       struct au_branch *br;
25085 +       int wkq_err;
25086 +       char a[PLINK_NAME_LEN];
25087 +       struct qstr tgtname = QSTR_INIT(a, 0);
25088 +
25089 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25090 +
25091 +       br = au_sbr(inode->i_sb, bindex);
25092 +       h_parent = br->br_wbr->wbr_plink;
25093 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25094 +
25095 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25096 +               struct au_do_plink_lkup_args args = {
25097 +                       .errp           = &h_dentry,
25098 +                       .tgtname        = &tgtname,
25099 +                       .h_parent       = h_parent,
25100 +                       .br             = br
25101 +               };
25102 +
25103 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
25104 +               if (unlikely(wkq_err))
25105 +                       h_dentry = ERR_PTR(wkq_err);
25106 +       } else
25107 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25108 +
25109 +       return h_dentry;
25110 +}
25111 +
25112 +/* create a pseudo-link */
25113 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25114 +                     struct dentry *h_dentry, struct au_branch *br)
25115 +{
25116 +       int err;
25117 +       struct path h_path = {
25118 +               .mnt = au_br_mnt(br)
25119 +       };
25120 +       struct inode *h_dir, *delegated;
25121 +
25122 +       h_dir = d_inode(h_parent);
25123 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
25124 +again:
25125 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
25126 +       err = PTR_ERR(h_path.dentry);
25127 +       if (IS_ERR(h_path.dentry))
25128 +               goto out;
25129 +
25130 +       err = 0;
25131 +       /* wh.plink dir is not monitored */
25132 +       /* todo: is it really safe? */
25133 +       if (d_is_positive(h_path.dentry)
25134 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
25135 +               delegated = NULL;
25136 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25137 +               if (unlikely(err == -EWOULDBLOCK)) {
25138 +                       pr_warn("cannot retry for NFSv4 delegation"
25139 +                               " for an internal unlink\n");
25140 +                       iput(delegated);
25141 +               }
25142 +               dput(h_path.dentry);
25143 +               h_path.dentry = NULL;
25144 +               if (!err)
25145 +                       goto again;
25146 +       }
25147 +       if (!err && d_is_negative(h_path.dentry)) {
25148 +               delegated = NULL;
25149 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25150 +               if (unlikely(err == -EWOULDBLOCK)) {
25151 +                       pr_warn("cannot retry for NFSv4 delegation"
25152 +                               " for an internal link\n");
25153 +                       iput(delegated);
25154 +               }
25155 +       }
25156 +       dput(h_path.dentry);
25157 +
25158 +out:
25159 +       inode_unlock(h_dir);
25160 +       return err;
25161 +}
25162 +
25163 +struct do_whplink_args {
25164 +       int *errp;
25165 +       struct qstr *tgt;
25166 +       struct dentry *h_parent;
25167 +       struct dentry *h_dentry;
25168 +       struct au_branch *br;
25169 +};
25170 +
25171 +static void call_do_whplink(void *args)
25172 +{
25173 +       struct do_whplink_args *a = args;
25174 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25175 +}
25176 +
25177 +static int whplink(struct dentry *h_dentry, struct inode *inode,
25178 +                  aufs_bindex_t bindex, struct au_branch *br)
25179 +{
25180 +       int err, wkq_err;
25181 +       struct au_wbr *wbr;
25182 +       struct dentry *h_parent;
25183 +       char a[PLINK_NAME_LEN];
25184 +       struct qstr tgtname = QSTR_INIT(a, 0);
25185 +
25186 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25187 +       h_parent = wbr->wbr_plink;
25188 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25189 +
25190 +       /* always superio. */
25191 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25192 +               struct do_whplink_args args = {
25193 +                       .errp           = &err,
25194 +                       .tgt            = &tgtname,
25195 +                       .h_parent       = h_parent,
25196 +                       .h_dentry       = h_dentry,
25197 +                       .br             = br
25198 +               };
25199 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
25200 +               if (unlikely(wkq_err))
25201 +                       err = wkq_err;
25202 +       } else
25203 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
25204 +
25205 +       return err;
25206 +}
25207 +
25208 +/* free a single plink */
25209 +static void do_put_plink(struct pseudo_link *plink, int do_del)
25210 +{
25211 +       if (do_del)
25212 +               hlist_del(&plink->hlist);
25213 +       iput(plink->inode);
25214 +       kfree(plink);
25215 +}
25216 +
25217 +static void do_put_plink_rcu(struct rcu_head *rcu)
25218 +{
25219 +       struct pseudo_link *plink;
25220 +
25221 +       plink = container_of(rcu, struct pseudo_link, rcu);
25222 +       iput(plink->inode);
25223 +       kfree(plink);
25224 +}
25225 +
25226 +/*
25227 + * create a new pseudo-link for @h_dentry on @bindex.
25228 + * the linked inode is held in aufs @inode.
25229 + */
25230 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25231 +                    struct dentry *h_dentry)
25232 +{
25233 +       struct super_block *sb;
25234 +       struct au_sbinfo *sbinfo;
25235 +       struct hlist_head *plink_hlist;
25236 +       struct pseudo_link *plink, *tmp;
25237 +       struct au_sphlhead *sphl;
25238 +       int found, err, cnt, i;
25239 +
25240 +       sb = inode->i_sb;
25241 +       sbinfo = au_sbi(sb);
25242 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25243 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25244 +
25245 +       found = au_plink_test(inode);
25246 +       if (found)
25247 +               return;
25248 +
25249 +       i = au_plink_hash(inode->i_ino);
25250 +       sphl = sbinfo->si_plink + i;
25251 +       plink_hlist = &sphl->head;
25252 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
25253 +       if (tmp)
25254 +               tmp->inode = au_igrab(inode);
25255 +       else {
25256 +               err = -ENOMEM;
25257 +               goto out;
25258 +       }
25259 +
25260 +       spin_lock(&sphl->spin);
25261 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
25262 +               if (plink->inode == inode) {
25263 +                       found = 1;
25264 +                       break;
25265 +               }
25266 +       }
25267 +       if (!found)
25268 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
25269 +       spin_unlock(&sphl->spin);
25270 +       if (!found) {
25271 +               cnt = au_sphl_count(sphl);
25272 +#define msg "unexpectedly unblanced or too many pseudo-links"
25273 +               if (cnt > AUFS_PLINK_WARN)
25274 +                       AuWarn1(msg ", %d\n", cnt);
25275 +#undef msg
25276 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25277 +       } else {
25278 +               do_put_plink(tmp, 0);
25279 +               return;
25280 +       }
25281 +
25282 +out:
25283 +       if (unlikely(err)) {
25284 +               pr_warn("err %d, damaged pseudo link.\n", err);
25285 +               if (tmp) {
25286 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
25287 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
25288 +               }
25289 +       }
25290 +}
25291 +
25292 +/* free all plinks */
25293 +void au_plink_put(struct super_block *sb, int verbose)
25294 +{
25295 +       int i, warned;
25296 +       struct au_sbinfo *sbinfo;
25297 +       struct hlist_head *plink_hlist;
25298 +       struct hlist_node *tmp;
25299 +       struct pseudo_link *plink;
25300 +
25301 +       SiMustWriteLock(sb);
25302 +
25303 +       sbinfo = au_sbi(sb);
25304 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25305 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25306 +
25307 +       /* no spin_lock since sbinfo is write-locked */
25308 +       warned = 0;
25309 +       for (i = 0; i < AuPlink_NHASH; i++) {
25310 +               plink_hlist = &sbinfo->si_plink[i].head;
25311 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25312 +                       pr_warn("pseudo-link is not flushed");
25313 +                       warned = 1;
25314 +               }
25315 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
25316 +                       do_put_plink(plink, 0);
25317 +               INIT_HLIST_HEAD(plink_hlist);
25318 +       }
25319 +}
25320 +
25321 +void au_plink_clean(struct super_block *sb, int verbose)
25322 +{
25323 +       struct dentry *root;
25324 +
25325 +       root = sb->s_root;
25326 +       aufs_write_lock(root);
25327 +       if (au_opt_test(au_mntflags(sb), PLINK))
25328 +               au_plink_put(sb, verbose);
25329 +       aufs_write_unlock(root);
25330 +}
25331 +
25332 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25333 +{
25334 +       int do_put;
25335 +       aufs_bindex_t bstart, bend, bindex;
25336 +
25337 +       do_put = 0;
25338 +       bstart = au_ibstart(inode);
25339 +       bend = au_ibend(inode);
25340 +       if (bstart >= 0) {
25341 +               for (bindex = bstart; bindex <= bend; bindex++) {
25342 +                       if (!au_h_iptr(inode, bindex)
25343 +                           || au_ii_br_id(inode, bindex) != br_id)
25344 +                               continue;
25345 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25346 +                       do_put = 1;
25347 +                       break;
25348 +               }
25349 +               if (do_put)
25350 +                       for (bindex = bstart; bindex <= bend; bindex++)
25351 +                               if (au_h_iptr(inode, bindex)) {
25352 +                                       do_put = 0;
25353 +                                       break;
25354 +                               }
25355 +       } else
25356 +               do_put = 1;
25357 +
25358 +       return do_put;
25359 +}
25360 +
25361 +/* free the plinks on a branch specified by @br_id */
25362 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25363 +{
25364 +       struct au_sbinfo *sbinfo;
25365 +       struct hlist_head *plink_hlist;
25366 +       struct hlist_node *tmp;
25367 +       struct pseudo_link *plink;
25368 +       struct inode *inode;
25369 +       int i, do_put;
25370 +
25371 +       SiMustWriteLock(sb);
25372 +
25373 +       sbinfo = au_sbi(sb);
25374 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25375 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25376 +
25377 +       /* no spin_lock since sbinfo is write-locked */
25378 +       for (i = 0; i < AuPlink_NHASH; i++) {
25379 +               plink_hlist = &sbinfo->si_plink[i].head;
25380 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
25381 +                       inode = au_igrab(plink->inode);
25382 +                       ii_write_lock_child(inode);
25383 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25384 +                       if (do_put)
25385 +                               do_put_plink(plink, 1);
25386 +                       ii_write_unlock(inode);
25387 +                       iput(inode);
25388 +               }
25389 +       }
25390 +}
25391 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25392 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25393 +++ linux/fs/aufs/poll.c        2016-04-24 18:32:51.393686895 +0200
25394 @@ -0,0 +1,52 @@
25395 +/*
25396 + * Copyright (C) 2005-2016 Junjiro R. Okajima
25397 + *
25398 + * This program, aufs is free software; you can redistribute it and/or modify
25399 + * it under the terms of the GNU General Public License as published by
25400 + * the Free Software Foundation; either version 2 of the License, or
25401 + * (at your option) any later version.
25402 + *
25403 + * This program is distributed in the hope that it will be useful,
25404 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25405 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25406 + * GNU General Public License for more details.
25407 + *
25408 + * You should have received a copy of the GNU General Public License
25409 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25410 + */
25411 +
25412 +/*
25413 + * poll operation
25414 + * There is only one filesystem which implements ->poll operation, currently.
25415 + */
25416 +
25417 +#include "aufs.h"
25418 +
25419 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25420 +{
25421 +       unsigned int mask;
25422 +       int err;
25423 +       struct file *h_file;
25424 +       struct super_block *sb;
25425 +
25426 +       /* We should pretend an error happened. */
25427 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25428 +       sb = file->f_path.dentry->d_sb;
25429 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25430 +
25431 +       h_file = au_read_pre(file, /*keep_fi*/0);
25432 +       err = PTR_ERR(h_file);
25433 +       if (IS_ERR(h_file))
25434 +               goto out;
25435 +
25436 +       /* it is not an error if h_file has no operation */
25437 +       mask = DEFAULT_POLLMASK;
25438 +       if (h_file->f_op->poll)
25439 +               mask = h_file->f_op->poll(h_file, wait);
25440 +       fput(h_file); /* instead of au_read_post() */
25441 +
25442 +out:
25443 +       si_read_unlock(sb);
25444 +       AuTraceErr((int)mask);
25445 +       return mask;
25446 +}
25447 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25448 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25449 +++ linux/fs/aufs/posix_acl.c   2016-04-24 18:32:51.393686895 +0200
25450 @@ -0,0 +1,98 @@
25451 +/*
25452 + * Copyright (C) 2014-2016 Junjiro R. Okajima
25453 + *
25454 + * This program, aufs is free software; you can redistribute it and/or modify
25455 + * it under the terms of the GNU General Public License as published by
25456 + * the Free Software Foundation; either version 2 of the License, or
25457 + * (at your option) any later version.
25458 + *
25459 + * This program is distributed in the hope that it will be useful,
25460 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25461 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25462 + * GNU General Public License for more details.
25463 + *
25464 + * You should have received a copy of the GNU General Public License
25465 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25466 + */
25467 +
25468 +/*
25469 + * posix acl operations
25470 + */
25471 +
25472 +#include <linux/fs.h>
25473 +#include "aufs.h"
25474 +
25475 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25476 +{
25477 +       struct posix_acl *acl;
25478 +       int err;
25479 +       aufs_bindex_t bindex;
25480 +       struct inode *h_inode;
25481 +       struct super_block *sb;
25482 +
25483 +       acl = NULL;
25484 +       sb = inode->i_sb;
25485 +       si_read_lock(sb, AuLock_FLUSH);
25486 +       ii_read_lock_child(inode);
25487 +       if (!(sb->s_flags & MS_POSIXACL))
25488 +               goto out;
25489 +
25490 +       bindex = au_ibstart(inode);
25491 +       h_inode = au_h_iptr(inode, bindex);
25492 +       if (unlikely(!h_inode
25493 +                    || ((h_inode->i_mode & S_IFMT)
25494 +                        != (inode->i_mode & S_IFMT)))) {
25495 +               err = au_busy_or_stale();
25496 +               acl = ERR_PTR(err);
25497 +               goto out;
25498 +       }
25499 +
25500 +       /* always topmost only */
25501 +       acl = get_acl(h_inode, type);
25502 +
25503 +out:
25504 +       ii_read_unlock(inode);
25505 +       si_read_unlock(sb);
25506 +
25507 +       AuTraceErrPtr(acl);
25508 +       return acl;
25509 +}
25510 +
25511 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25512 +{
25513 +       int err;
25514 +       ssize_t ssz;
25515 +       struct dentry *dentry;
25516 +       struct au_srxattr arg = {
25517 +               .type = AU_ACL_SET,
25518 +               .u.acl_set = {
25519 +                       .acl    = acl,
25520 +                       .type   = type
25521 +               },
25522 +       };
25523 +
25524 +       inode_lock(inode);
25525 +       if (inode->i_ino == AUFS_ROOT_INO)
25526 +               dentry = dget(inode->i_sb->s_root);
25527 +       else {
25528 +               dentry = d_find_alias(inode);
25529 +               if (!dentry)
25530 +                       dentry = d_find_any_alias(inode);
25531 +               if (!dentry) {
25532 +                       pr_warn("cannot handle this inode, "
25533 +                               "please report to aufs-users ML\n");
25534 +                       err = -ENOENT;
25535 +                       goto out;
25536 +               }
25537 +       }
25538 +
25539 +       ssz = au_srxattr(dentry, &arg);
25540 +       dput(dentry);
25541 +       err = ssz;
25542 +       if (ssz >= 0)
25543 +               err = 0;
25544 +
25545 +out:
25546 +       inode_unlock(inode);
25547 +       return err;
25548 +}
25549 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25550 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
25551 +++ linux/fs/aufs/procfs.c      2016-04-24 18:32:51.393686895 +0200
25552 @@ -0,0 +1,169 @@
25553 +/*
25554 + * Copyright (C) 2010-2016 Junjiro R. Okajima
25555 + *
25556 + * This program, aufs is free software; you can redistribute it and/or modify
25557 + * it under the terms of the GNU General Public License as published by
25558 + * the Free Software Foundation; either version 2 of the License, or
25559 + * (at your option) any later version.
25560 + *
25561 + * This program is distributed in the hope that it will be useful,
25562 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25563 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25564 + * GNU General Public License for more details.
25565 + *
25566 + * You should have received a copy of the GNU General Public License
25567 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25568 + */
25569 +
25570 +/*
25571 + * procfs interfaces
25572 + */
25573 +
25574 +#include <linux/proc_fs.h>
25575 +#include "aufs.h"
25576 +
25577 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
25578 +{
25579 +       struct au_sbinfo *sbinfo;
25580 +
25581 +       sbinfo = file->private_data;
25582 +       if (sbinfo) {
25583 +               au_plink_maint_leave(sbinfo);
25584 +               kobject_put(&sbinfo->si_kobj);
25585 +       }
25586 +
25587 +       return 0;
25588 +}
25589 +
25590 +static void au_procfs_plm_write_clean(struct file *file)
25591 +{
25592 +       struct au_sbinfo *sbinfo;
25593 +
25594 +       sbinfo = file->private_data;
25595 +       if (sbinfo)
25596 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25597 +}
25598 +
25599 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25600 +{
25601 +       int err;
25602 +       struct super_block *sb;
25603 +       struct au_sbinfo *sbinfo;
25604 +
25605 +       err = -EBUSY;
25606 +       if (unlikely(file->private_data))
25607 +               goto out;
25608 +
25609 +       sb = NULL;
25610 +       /* don't use au_sbilist_lock() here */
25611 +       spin_lock(&au_sbilist.spin);
25612 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
25613 +               if (id == sysaufs_si_id(sbinfo)) {
25614 +                       kobject_get(&sbinfo->si_kobj);
25615 +                       sb = sbinfo->si_sb;
25616 +                       break;
25617 +               }
25618 +       spin_unlock(&au_sbilist.spin);
25619 +
25620 +       err = -EINVAL;
25621 +       if (unlikely(!sb))
25622 +               goto out;
25623 +
25624 +       err = au_plink_maint_enter(sb);
25625 +       if (!err)
25626 +               /* keep kobject_get() */
25627 +               file->private_data = sbinfo;
25628 +       else
25629 +               kobject_put(&sbinfo->si_kobj);
25630 +out:
25631 +       return err;
25632 +}
25633 +
25634 +/*
25635 + * Accept a valid "si=xxxx" only.
25636 + * Once it is accepted successfully, accept "clean" too.
25637 + */
25638 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25639 +                                  size_t count, loff_t *ppos)
25640 +{
25641 +       ssize_t err;
25642 +       unsigned long id;
25643 +       /* last newline is allowed */
25644 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
25645 +
25646 +       err = -EACCES;
25647 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25648 +               goto out;
25649 +
25650 +       err = -EINVAL;
25651 +       if (unlikely(count > sizeof(buf)))
25652 +               goto out;
25653 +
25654 +       err = copy_from_user(buf, ubuf, count);
25655 +       if (unlikely(err)) {
25656 +               err = -EFAULT;
25657 +               goto out;
25658 +       }
25659 +       buf[count] = 0;
25660 +
25661 +       err = -EINVAL;
25662 +       if (!strcmp("clean", buf)) {
25663 +               au_procfs_plm_write_clean(file);
25664 +               goto out_success;
25665 +       } else if (unlikely(strncmp("si=", buf, 3)))
25666 +               goto out;
25667 +
25668 +       err = kstrtoul(buf + 3, 16, &id);
25669 +       if (unlikely(err))
25670 +               goto out;
25671 +
25672 +       err = au_procfs_plm_write_si(file, id);
25673 +       if (unlikely(err))
25674 +               goto out;
25675 +
25676 +out_success:
25677 +       err = count; /* success */
25678 +out:
25679 +       return err;
25680 +}
25681 +
25682 +static const struct file_operations au_procfs_plm_fop = {
25683 +       .write          = au_procfs_plm_write,
25684 +       .release        = au_procfs_plm_release,
25685 +       .owner          = THIS_MODULE
25686 +};
25687 +
25688 +/* ---------------------------------------------------------------------- */
25689 +
25690 +static struct proc_dir_entry *au_procfs_dir;
25691 +
25692 +void au_procfs_fin(void)
25693 +{
25694 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25695 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25696 +}
25697 +
25698 +int __init au_procfs_init(void)
25699 +{
25700 +       int err;
25701 +       struct proc_dir_entry *entry;
25702 +
25703 +       err = -ENOMEM;
25704 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25705 +       if (unlikely(!au_procfs_dir))
25706 +               goto out;
25707 +
25708 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25709 +                           au_procfs_dir, &au_procfs_plm_fop);
25710 +       if (unlikely(!entry))
25711 +               goto out_dir;
25712 +
25713 +       err = 0;
25714 +       goto out; /* success */
25715 +
25716 +
25717 +out_dir:
25718 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25719 +out:
25720 +       return err;
25721 +}
25722 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25723 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
25724 +++ linux/fs/aufs/rdu.c 2016-04-24 18:32:51.393686895 +0200
25725 @@ -0,0 +1,389 @@
25726 +/*
25727 + * Copyright (C) 2005-2016 Junjiro R. Okajima
25728 + *
25729 + * This program, aufs is free software; you can redistribute it and/or modify
25730 + * it under the terms of the GNU General Public License as published by
25731 + * the Free Software Foundation; either version 2 of the License, or
25732 + * (at your option) any later version.
25733 + *
25734 + * This program is distributed in the hope that it will be useful,
25735 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25736 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25737 + * GNU General Public License for more details.
25738 + *
25739 + * You should have received a copy of the GNU General Public License
25740 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25741 + */
25742 +
25743 +/*
25744 + * readdir in userspace.
25745 + */
25746 +
25747 +#include <linux/compat.h>
25748 +#include <linux/fs_stack.h>
25749 +#include <linux/security.h>
25750 +#include "aufs.h"
25751 +
25752 +/* bits for struct aufs_rdu.flags */
25753 +#define        AuRdu_CALLED    1
25754 +#define        AuRdu_CONT      (1 << 1)
25755 +#define        AuRdu_FULL      (1 << 2)
25756 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
25757 +#define au_fset_rdu(flags, name) \
25758 +       do { (flags) |= AuRdu_##name; } while (0)
25759 +#define au_fclr_rdu(flags, name) \
25760 +       do { (flags) &= ~AuRdu_##name; } while (0)
25761 +
25762 +struct au_rdu_arg {
25763 +       struct dir_context              ctx;
25764 +       struct aufs_rdu                 *rdu;
25765 +       union au_rdu_ent_ul             ent;
25766 +       unsigned long                   end;
25767 +
25768 +       struct super_block              *sb;
25769 +       int                             err;
25770 +};
25771 +
25772 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
25773 +                      loff_t offset, u64 h_ino, unsigned int d_type)
25774 +{
25775 +       int err, len;
25776 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
25777 +       struct aufs_rdu *rdu = arg->rdu;
25778 +       struct au_rdu_ent ent;
25779 +
25780 +       err = 0;
25781 +       arg->err = 0;
25782 +       au_fset_rdu(rdu->cookie.flags, CALLED);
25783 +       len = au_rdu_len(nlen);
25784 +       if (arg->ent.ul + len  < arg->end) {
25785 +               ent.ino = h_ino;
25786 +               ent.bindex = rdu->cookie.bindex;
25787 +               ent.type = d_type;
25788 +               ent.nlen = nlen;
25789 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
25790 +                       ent.type = DT_UNKNOWN;
25791 +
25792 +               /* unnecessary to support mmap_sem since this is a dir */
25793 +               err = -EFAULT;
25794 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25795 +                       goto out;
25796 +               if (copy_to_user(arg->ent.e->name, name, nlen))
25797 +                       goto out;
25798 +               /* the terminating NULL */
25799 +               if (__put_user(0, arg->ent.e->name + nlen))
25800 +                       goto out;
25801 +               err = 0;
25802 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25803 +               arg->ent.ul += len;
25804 +               rdu->rent++;
25805 +       } else {
25806 +               err = -EFAULT;
25807 +               au_fset_rdu(rdu->cookie.flags, FULL);
25808 +               rdu->full = 1;
25809 +               rdu->tail = arg->ent;
25810 +       }
25811 +
25812 +out:
25813 +       /* AuTraceErr(err); */
25814 +       return err;
25815 +}
25816 +
25817 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25818 +{
25819 +       int err;
25820 +       loff_t offset;
25821 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25822 +
25823 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
25824 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25825 +       err = offset;
25826 +       if (unlikely(offset != cookie->h_pos))
25827 +               goto out;
25828 +
25829 +       err = 0;
25830 +       do {
25831 +               arg->err = 0;
25832 +               au_fclr_rdu(cookie->flags, CALLED);
25833 +               /* smp_mb(); */
25834 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
25835 +               if (err >= 0)
25836 +                       err = arg->err;
25837 +       } while (!err
25838 +                && au_ftest_rdu(cookie->flags, CALLED)
25839 +                && !au_ftest_rdu(cookie->flags, FULL));
25840 +       cookie->h_pos = h_file->f_pos;
25841 +
25842 +out:
25843 +       AuTraceErr(err);
25844 +       return err;
25845 +}
25846 +
25847 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25848 +{
25849 +       int err;
25850 +       aufs_bindex_t bend;
25851 +       struct au_rdu_arg arg = {
25852 +               .ctx = {
25853 +                       .actor = au_rdu_fill
25854 +               }
25855 +       };
25856 +       struct dentry *dentry;
25857 +       struct inode *inode;
25858 +       struct file *h_file;
25859 +       struct au_rdu_cookie *cookie = &rdu->cookie;
25860 +
25861 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25862 +       if (unlikely(err)) {
25863 +               err = -EFAULT;
25864 +               AuTraceErr(err);
25865 +               goto out;
25866 +       }
25867 +       rdu->rent = 0;
25868 +       rdu->tail = rdu->ent;
25869 +       rdu->full = 0;
25870 +       arg.rdu = rdu;
25871 +       arg.ent = rdu->ent;
25872 +       arg.end = arg.ent.ul;
25873 +       arg.end += rdu->sz;
25874 +
25875 +       err = -ENOTDIR;
25876 +       if (unlikely(!file->f_op->iterate))
25877 +               goto out;
25878 +
25879 +       err = security_file_permission(file, MAY_READ);
25880 +       AuTraceErr(err);
25881 +       if (unlikely(err))
25882 +               goto out;
25883 +
25884 +       dentry = file->f_path.dentry;
25885 +       inode = d_inode(dentry);
25886 +#if 1
25887 +       inode_lock(inode);
25888 +#else
25889 +       /* todo: create a new inline func inode_lock_killable() */
25890 +       err = mutex_lock_killable(&inode->i_mutex);
25891 +       AuTraceErr(err);
25892 +       if (unlikely(err))
25893 +               goto out;
25894 +#endif
25895 +
25896 +       arg.sb = inode->i_sb;
25897 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25898 +       if (unlikely(err))
25899 +               goto out_mtx;
25900 +       err = au_alive_dir(dentry);
25901 +       if (unlikely(err))
25902 +               goto out_si;
25903 +       /* todo: reval? */
25904 +       fi_read_lock(file);
25905 +
25906 +       err = -EAGAIN;
25907 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25908 +                    && cookie->generation != au_figen(file)))
25909 +               goto out_unlock;
25910 +
25911 +       err = 0;
25912 +       if (!rdu->blk) {
25913 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
25914 +               if (!rdu->blk)
25915 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
25916 +       }
25917 +       bend = au_fbstart(file);
25918 +       if (cookie->bindex < bend)
25919 +               cookie->bindex = bend;
25920 +       bend = au_fbend_dir(file);
25921 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
25922 +       for (; !err && cookie->bindex <= bend;
25923 +            cookie->bindex++, cookie->h_pos = 0) {
25924 +               h_file = au_hf_dir(file, cookie->bindex);
25925 +               if (!h_file)
25926 +                       continue;
25927 +
25928 +               au_fclr_rdu(cookie->flags, FULL);
25929 +               err = au_rdu_do(h_file, &arg);
25930 +               AuTraceErr(err);
25931 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25932 +                       break;
25933 +       }
25934 +       AuDbg("rent %llu\n", rdu->rent);
25935 +
25936 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25937 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25938 +               au_fset_rdu(cookie->flags, CONT);
25939 +               cookie->generation = au_figen(file);
25940 +       }
25941 +
25942 +       ii_read_lock_child(inode);
25943 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
25944 +       ii_read_unlock(inode);
25945 +
25946 +out_unlock:
25947 +       fi_read_unlock(file);
25948 +out_si:
25949 +       si_read_unlock(arg.sb);
25950 +out_mtx:
25951 +       inode_unlock(inode);
25952 +out:
25953 +       AuTraceErr(err);
25954 +       return err;
25955 +}
25956 +
25957 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
25958 +{
25959 +       int err;
25960 +       ino_t ino;
25961 +       unsigned long long nent;
25962 +       union au_rdu_ent_ul *u;
25963 +       struct au_rdu_ent ent;
25964 +       struct super_block *sb;
25965 +
25966 +       err = 0;
25967 +       nent = rdu->nent;
25968 +       u = &rdu->ent;
25969 +       sb = file->f_path.dentry->d_sb;
25970 +       si_read_lock(sb, AuLock_FLUSH);
25971 +       while (nent-- > 0) {
25972 +               /* unnecessary to support mmap_sem since this is a dir */
25973 +               err = copy_from_user(&ent, u->e, sizeof(ent));
25974 +               if (!err)
25975 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
25976 +               if (unlikely(err)) {
25977 +                       err = -EFAULT;
25978 +                       AuTraceErr(err);
25979 +                       break;
25980 +               }
25981 +
25982 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
25983 +               if (!ent.wh)
25984 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
25985 +               else
25986 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
25987 +                                       &ino);
25988 +               if (unlikely(err)) {
25989 +                       AuTraceErr(err);
25990 +                       break;
25991 +               }
25992 +
25993 +               err = __put_user(ino, &u->e->ino);
25994 +               if (unlikely(err)) {
25995 +                       err = -EFAULT;
25996 +                       AuTraceErr(err);
25997 +                       break;
25998 +               }
25999 +               u->ul += au_rdu_len(ent.nlen);
26000 +       }
26001 +       si_read_unlock(sb);
26002 +
26003 +       return err;
26004 +}
26005 +
26006 +/* ---------------------------------------------------------------------- */
26007 +
26008 +static int au_rdu_verify(struct aufs_rdu *rdu)
26009 +{
26010 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
26011 +             "%llu, b%d, 0x%x, g%u}\n",
26012 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
26013 +             rdu->blk,
26014 +             rdu->rent, rdu->shwh, rdu->full,
26015 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
26016 +             rdu->cookie.generation);
26017 +
26018 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
26019 +               return 0;
26020 +
26021 +       AuDbg("%u:%u\n",
26022 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
26023 +       return -EINVAL;
26024 +}
26025 +
26026 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26027 +{
26028 +       long err, e;
26029 +       struct aufs_rdu rdu;
26030 +       void __user *p = (void __user *)arg;
26031 +
26032 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26033 +       if (unlikely(err)) {
26034 +               err = -EFAULT;
26035 +               AuTraceErr(err);
26036 +               goto out;
26037 +       }
26038 +       err = au_rdu_verify(&rdu);
26039 +       if (unlikely(err))
26040 +               goto out;
26041 +
26042 +       switch (cmd) {
26043 +       case AUFS_CTL_RDU:
26044 +               err = au_rdu(file, &rdu);
26045 +               if (unlikely(err))
26046 +                       break;
26047 +
26048 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26049 +               if (unlikely(e)) {
26050 +                       err = -EFAULT;
26051 +                       AuTraceErr(err);
26052 +               }
26053 +               break;
26054 +       case AUFS_CTL_RDU_INO:
26055 +               err = au_rdu_ino(file, &rdu);
26056 +               break;
26057 +
26058 +       default:
26059 +               /* err = -ENOTTY; */
26060 +               err = -EINVAL;
26061 +       }
26062 +
26063 +out:
26064 +       AuTraceErr(err);
26065 +       return err;
26066 +}
26067 +
26068 +#ifdef CONFIG_COMPAT
26069 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26070 +{
26071 +       long err, e;
26072 +       struct aufs_rdu rdu;
26073 +       void __user *p = compat_ptr(arg);
26074 +
26075 +       /* todo: get_user()? */
26076 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26077 +       if (unlikely(err)) {
26078 +               err = -EFAULT;
26079 +               AuTraceErr(err);
26080 +               goto out;
26081 +       }
26082 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
26083 +       err = au_rdu_verify(&rdu);
26084 +       if (unlikely(err))
26085 +               goto out;
26086 +
26087 +       switch (cmd) {
26088 +       case AUFS_CTL_RDU:
26089 +               err = au_rdu(file, &rdu);
26090 +               if (unlikely(err))
26091 +                       break;
26092 +
26093 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
26094 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
26095 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26096 +               if (unlikely(e)) {
26097 +                       err = -EFAULT;
26098 +                       AuTraceErr(err);
26099 +               }
26100 +               break;
26101 +       case AUFS_CTL_RDU_INO:
26102 +               err = au_rdu_ino(file, &rdu);
26103 +               break;
26104 +
26105 +       default:
26106 +               /* err = -ENOTTY; */
26107 +               err = -EINVAL;
26108 +       }
26109 +
26110 +out:
26111 +       AuTraceErr(err);
26112 +       return err;
26113 +}
26114 +#endif
26115 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26116 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
26117 +++ linux/fs/aufs/rwsem.h       2016-04-24 18:32:51.393686895 +0200
26118 @@ -0,0 +1,191 @@
26119 +/*
26120 + * Copyright (C) 2005-2016 Junjiro R. Okajima
26121 + *
26122 + * This program, aufs is free software; you can redistribute it and/or modify
26123 + * it under the terms of the GNU General Public License as published by
26124 + * the Free Software Foundation; either version 2 of the License, or
26125 + * (at your option) any later version.
26126 + *
26127 + * This program is distributed in the hope that it will be useful,
26128 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26129 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26130 + * GNU General Public License for more details.
26131 + *
26132 + * You should have received a copy of the GNU General Public License
26133 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26134 + */
26135 +
26136 +/*
26137 + * simple read-write semaphore wrappers
26138 + */
26139 +
26140 +#ifndef __AUFS_RWSEM_H__
26141 +#define __AUFS_RWSEM_H__
26142 +
26143 +#ifdef __KERNEL__
26144 +
26145 +#include "debug.h"
26146 +
26147 +struct au_rwsem {
26148 +       struct rw_semaphore     rwsem;
26149 +#ifdef CONFIG_AUFS_DEBUG
26150 +       /* just for debugging, not almighty counter */
26151 +       atomic_t                rcnt, wcnt;
26152 +#endif
26153 +};
26154 +
26155 +#ifdef CONFIG_AUFS_DEBUG
26156 +#define AuDbgCntInit(rw) do { \
26157 +       atomic_set(&(rw)->rcnt, 0); \
26158 +       atomic_set(&(rw)->wcnt, 0); \
26159 +       smp_mb(); /* atomic set */ \
26160 +} while (0)
26161 +
26162 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
26163 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
26164 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
26165 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
26166 +#else
26167 +#define AuDbgCntInit(rw)       do {} while (0)
26168 +#define AuDbgRcntInc(rw)       do {} while (0)
26169 +#define AuDbgRcntDec(rw)       do {} while (0)
26170 +#define AuDbgWcntInc(rw)       do {} while (0)
26171 +#define AuDbgWcntDec(rw)       do {} while (0)
26172 +#endif /* CONFIG_AUFS_DEBUG */
26173 +
26174 +/* to debug easier, do not make them inlined functions */
26175 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
26176 +/* rwsem_is_locked() is unusable */
26177 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
26178 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
26179 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
26180 +                                       && atomic_read(&(rw)->wcnt) <= 0)
26181 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
26182 +                                       || atomic_read(&(rw)->wcnt))
26183 +
26184 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
26185 +
26186 +static inline void au_rw_init(struct au_rwsem *rw)
26187 +{
26188 +       AuDbgCntInit(rw);
26189 +       init_rwsem(&rw->rwsem);
26190 +}
26191 +
26192 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
26193 +{
26194 +       au_rw_init(rw);
26195 +       down_write(&rw->rwsem);
26196 +       AuDbgWcntInc(rw);
26197 +}
26198 +
26199 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
26200 +                                          unsigned int lsc)
26201 +{
26202 +       au_rw_init(rw);
26203 +       down_write_nested(&rw->rwsem, lsc);
26204 +       AuDbgWcntInc(rw);
26205 +}
26206 +
26207 +static inline void au_rw_read_lock(struct au_rwsem *rw)
26208 +{
26209 +       down_read(&rw->rwsem);
26210 +       AuDbgRcntInc(rw);
26211 +}
26212 +
26213 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26214 +{
26215 +       down_read_nested(&rw->rwsem, lsc);
26216 +       AuDbgRcntInc(rw);
26217 +}
26218 +
26219 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
26220 +{
26221 +       AuRwMustReadLock(rw);
26222 +       AuDbgRcntDec(rw);
26223 +       up_read(&rw->rwsem);
26224 +}
26225 +
26226 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26227 +{
26228 +       AuRwMustWriteLock(rw);
26229 +       AuDbgRcntInc(rw);
26230 +       AuDbgWcntDec(rw);
26231 +       downgrade_write(&rw->rwsem);
26232 +}
26233 +
26234 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26235 +{
26236 +       down_write(&rw->rwsem);
26237 +       AuDbgWcntInc(rw);
26238 +}
26239 +
26240 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26241 +                                          unsigned int lsc)
26242 +{
26243 +       down_write_nested(&rw->rwsem, lsc);
26244 +       AuDbgWcntInc(rw);
26245 +}
26246 +
26247 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26248 +{
26249 +       AuRwMustWriteLock(rw);
26250 +       AuDbgWcntDec(rw);
26251 +       up_write(&rw->rwsem);
26252 +}
26253 +
26254 +/* why is not _nested version defined */
26255 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26256 +{
26257 +       int ret;
26258 +
26259 +       ret = down_read_trylock(&rw->rwsem);
26260 +       if (ret)
26261 +               AuDbgRcntInc(rw);
26262 +       return ret;
26263 +}
26264 +
26265 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26266 +{
26267 +       int ret;
26268 +
26269 +       ret = down_write_trylock(&rw->rwsem);
26270 +       if (ret)
26271 +               AuDbgWcntInc(rw);
26272 +       return ret;
26273 +}
26274 +
26275 +#undef AuDbgCntInit
26276 +#undef AuDbgRcntInc
26277 +#undef AuDbgRcntDec
26278 +#undef AuDbgWcntInc
26279 +#undef AuDbgWcntDec
26280 +
26281 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26282 +static inline void prefix##_read_lock(param) \
26283 +{ au_rw_read_lock(rwsem); } \
26284 +static inline void prefix##_write_lock(param) \
26285 +{ au_rw_write_lock(rwsem); } \
26286 +static inline int prefix##_read_trylock(param) \
26287 +{ return au_rw_read_trylock(rwsem); } \
26288 +static inline int prefix##_write_trylock(param) \
26289 +{ return au_rw_write_trylock(rwsem); }
26290 +/* why is not _nested version defined */
26291 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26292 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26293 +static inline void prefix##_write_trylock_nestd(param, lsc)
26294 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26295 +
26296 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26297 +static inline void prefix##_read_unlock(param) \
26298 +{ au_rw_read_unlock(rwsem); } \
26299 +static inline void prefix##_write_unlock(param) \
26300 +{ au_rw_write_unlock(rwsem); } \
26301 +static inline void prefix##_downgrade_lock(param) \
26302 +{ au_rw_dgrade_lock(rwsem); }
26303 +
26304 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26305 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26306 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26307 +
26308 +#endif /* __KERNEL__ */
26309 +#endif /* __AUFS_RWSEM_H__ */
26310 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26311 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26312 +++ linux/fs/aufs/sbinfo.c      2016-04-24 18:32:51.393686895 +0200
26313 @@ -0,0 +1,350 @@
26314 +/*
26315 + * Copyright (C) 2005-2016 Junjiro R. Okajima
26316 + *
26317 + * This program, aufs is free software; you can redistribute it and/or modify
26318 + * it under the terms of the GNU General Public License as published by
26319 + * the Free Software Foundation; either version 2 of the License, or
26320 + * (at your option) any later version.
26321 + *
26322 + * This program is distributed in the hope that it will be useful,
26323 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26324 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26325 + * GNU General Public License for more details.
26326 + *
26327 + * You should have received a copy of the GNU General Public License
26328 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26329 + */
26330 +
26331 +/*
26332 + * superblock private data
26333 + */
26334 +
26335 +#include "aufs.h"
26336 +
26337 +/*
26338 + * they are necessary regardless sysfs is disabled.
26339 + */
26340 +void au_si_free(struct kobject *kobj)
26341 +{
26342 +       int i;
26343 +       struct au_sbinfo *sbinfo;
26344 +       char *locked __maybe_unused; /* debug only */
26345 +
26346 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26347 +       for (i = 0; i < AuPlink_NHASH; i++)
26348 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26349 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26350 +
26351 +       au_rw_write_lock(&sbinfo->si_rwsem);
26352 +       au_br_free(sbinfo);
26353 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26354 +
26355 +       kfree(sbinfo->si_branch);
26356 +       for (i = 0; i < AU_NPIDMAP; i++)
26357 +               kfree(sbinfo->au_si_pid.pid_bitmap[i]);
26358 +       mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
26359 +       mutex_destroy(&sbinfo->si_xib_mtx);
26360 +       AuRwDestroy(&sbinfo->si_rwsem);
26361 +
26362 +       kfree(sbinfo);
26363 +}
26364 +
26365 +int au_si_alloc(struct super_block *sb)
26366 +{
26367 +       int err, i;
26368 +       struct au_sbinfo *sbinfo;
26369 +       static struct lock_class_key aufs_si;
26370 +
26371 +       err = -ENOMEM;
26372 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26373 +       if (unlikely(!sbinfo))
26374 +               goto out;
26375 +
26376 +       /* will be reallocated separately */
26377 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26378 +       if (unlikely(!sbinfo->si_branch))
26379 +               goto out_sbinfo;
26380 +
26381 +       err = sysaufs_si_init(sbinfo);
26382 +       if (unlikely(err))
26383 +               goto out_br;
26384 +
26385 +       au_nwt_init(&sbinfo->si_nowait);
26386 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26387 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
26388 +       mutex_init(&sbinfo->au_si_pid.pid_mtx);
26389 +
26390 +       atomic_long_set(&sbinfo->si_ninodes, 0);
26391 +       atomic_long_set(&sbinfo->si_nfiles, 0);
26392 +
26393 +       sbinfo->si_bend = -1;
26394 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26395 +
26396 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26397 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26398 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26399 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26400 +
26401 +       au_fhsm_init(sbinfo);
26402 +
26403 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26404 +
26405 +       sbinfo->si_xino_jiffy = jiffies;
26406 +       sbinfo->si_xino_expire
26407 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26408 +       mutex_init(&sbinfo->si_xib_mtx);
26409 +       sbinfo->si_xino_brid = -1;
26410 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26411 +
26412 +       au_sphl_init(&sbinfo->si_aopen);
26413 +
26414 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26415 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26416 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26417 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26418 +
26419 +       for (i = 0; i < AuPlink_NHASH; i++)
26420 +               au_sphl_init(sbinfo->si_plink + i);
26421 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26422 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26423 +
26424 +       au_sphl_init(&sbinfo->si_files);
26425 +
26426 +       /* with getattr by default */
26427 +       sbinfo->si_iop_array = aufs_iop;
26428 +
26429 +       /* leave other members for sysaufs and si_mnt. */
26430 +       sbinfo->si_sb = sb;
26431 +       sb->s_fs_info = sbinfo;
26432 +       si_pid_set(sb);
26433 +       return 0; /* success */
26434 +
26435 +out_br:
26436 +       kfree(sbinfo->si_branch);
26437 +out_sbinfo:
26438 +       kfree(sbinfo);
26439 +out:
26440 +       return err;
26441 +}
26442 +
26443 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26444 +{
26445 +       int err, sz;
26446 +       struct au_branch **brp;
26447 +
26448 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26449 +
26450 +       err = -ENOMEM;
26451 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
26452 +       if (unlikely(!sz))
26453 +               sz = sizeof(*brp);
26454 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26455 +       if (brp) {
26456 +               sbinfo->si_branch = brp;
26457 +               err = 0;
26458 +       }
26459 +
26460 +       return err;
26461 +}
26462 +
26463 +/* ---------------------------------------------------------------------- */
26464 +
26465 +unsigned int au_sigen_inc(struct super_block *sb)
26466 +{
26467 +       unsigned int gen;
26468 +       struct inode *inode;
26469 +
26470 +       SiMustWriteLock(sb);
26471 +
26472 +       gen = ++au_sbi(sb)->si_generation;
26473 +       au_update_digen(sb->s_root);
26474 +       inode = d_inode(sb->s_root);
26475 +       au_update_iigen(inode, /*half*/0);
26476 +       inode->i_version++;
26477 +       return gen;
26478 +}
26479 +
26480 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26481 +{
26482 +       aufs_bindex_t br_id;
26483 +       int i;
26484 +       struct au_sbinfo *sbinfo;
26485 +
26486 +       SiMustWriteLock(sb);
26487 +
26488 +       sbinfo = au_sbi(sb);
26489 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26490 +               br_id = ++sbinfo->si_last_br_id;
26491 +               AuDebugOn(br_id < 0);
26492 +               if (br_id && au_br_index(sb, br_id) < 0)
26493 +                       return br_id;
26494 +       }
26495 +
26496 +       return -1;
26497 +}
26498 +
26499 +/* ---------------------------------------------------------------------- */
26500 +
26501 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26502 +int si_read_lock(struct super_block *sb, int flags)
26503 +{
26504 +       int err;
26505 +
26506 +       err = 0;
26507 +       if (au_ftest_lock(flags, FLUSH))
26508 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26509 +
26510 +       si_noflush_read_lock(sb);
26511 +       err = au_plink_maint(sb, flags);
26512 +       if (unlikely(err))
26513 +               si_read_unlock(sb);
26514 +
26515 +       return err;
26516 +}
26517 +
26518 +int si_write_lock(struct super_block *sb, int flags)
26519 +{
26520 +       int err;
26521 +
26522 +       if (au_ftest_lock(flags, FLUSH))
26523 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26524 +
26525 +       si_noflush_write_lock(sb);
26526 +       err = au_plink_maint(sb, flags);
26527 +       if (unlikely(err))
26528 +               si_write_unlock(sb);
26529 +
26530 +       return err;
26531 +}
26532 +
26533 +/* dentry and super_block lock. call at entry point */
26534 +int aufs_read_lock(struct dentry *dentry, int flags)
26535 +{
26536 +       int err;
26537 +       struct super_block *sb;
26538 +
26539 +       sb = dentry->d_sb;
26540 +       err = si_read_lock(sb, flags);
26541 +       if (unlikely(err))
26542 +               goto out;
26543 +
26544 +       if (au_ftest_lock(flags, DW))
26545 +               di_write_lock_child(dentry);
26546 +       else
26547 +               di_read_lock_child(dentry, flags);
26548 +
26549 +       if (au_ftest_lock(flags, GEN)) {
26550 +               err = au_digen_test(dentry, au_sigen(sb));
26551 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
26552 +                       AuDebugOn(!err && au_dbrange_test(dentry));
26553 +               else if (!err)
26554 +                       err = au_dbrange_test(dentry);
26555 +               if (unlikely(err))
26556 +                       aufs_read_unlock(dentry, flags);
26557 +       }
26558 +
26559 +out:
26560 +       return err;
26561 +}
26562 +
26563 +void aufs_read_unlock(struct dentry *dentry, int flags)
26564 +{
26565 +       if (au_ftest_lock(flags, DW))
26566 +               di_write_unlock(dentry);
26567 +       else
26568 +               di_read_unlock(dentry, flags);
26569 +       si_read_unlock(dentry->d_sb);
26570 +}
26571 +
26572 +void aufs_write_lock(struct dentry *dentry)
26573 +{
26574 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
26575 +       di_write_lock_child(dentry);
26576 +}
26577 +
26578 +void aufs_write_unlock(struct dentry *dentry)
26579 +{
26580 +       di_write_unlock(dentry);
26581 +       si_write_unlock(dentry->d_sb);
26582 +}
26583 +
26584 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
26585 +{
26586 +       int err;
26587 +       unsigned int sigen;
26588 +       struct super_block *sb;
26589 +
26590 +       sb = d1->d_sb;
26591 +       err = si_read_lock(sb, flags);
26592 +       if (unlikely(err))
26593 +               goto out;
26594 +
26595 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
26596 +
26597 +       if (au_ftest_lock(flags, GEN)) {
26598 +               sigen = au_sigen(sb);
26599 +               err = au_digen_test(d1, sigen);
26600 +               AuDebugOn(!err && au_dbrange_test(d1));
26601 +               if (!err) {
26602 +                       err = au_digen_test(d2, sigen);
26603 +                       AuDebugOn(!err && au_dbrange_test(d2));
26604 +               }
26605 +               if (unlikely(err))
26606 +                       aufs_read_and_write_unlock2(d1, d2);
26607 +       }
26608 +
26609 +out:
26610 +       return err;
26611 +}
26612 +
26613 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26614 +{
26615 +       di_write_unlock2(d1, d2);
26616 +       si_read_unlock(d1->d_sb);
26617 +}
26618 +
26619 +/* ---------------------------------------------------------------------- */
26620 +
26621 +static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
26622 +{
26623 +       unsigned long *p;
26624 +
26625 +       BUILD_BUG_ON(sizeof(unsigned long) !=
26626 +                    sizeof(*au_si_pid->pid_bitmap));
26627 +
26628 +       mutex_lock(&au_si_pid->pid_mtx);
26629 +       p = au_si_pid->pid_bitmap[idx];
26630 +       while (!p) {
26631 +               /*
26632 +                * bad approach.
26633 +                * but keeping 'si_pid_set()' void is more important.
26634 +                */
26635 +               p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
26636 +                           sizeof(*au_si_pid->pid_bitmap),
26637 +                           GFP_NOFS);
26638 +               if (p)
26639 +                       break;
26640 +               cond_resched();
26641 +       }
26642 +       au_si_pid->pid_bitmap[idx] = p;
26643 +       mutex_unlock(&au_si_pid->pid_mtx);
26644 +}
26645 +
26646 +void si_pid_set(struct super_block *sb)
26647 +{
26648 +       pid_t bit;
26649 +       int idx;
26650 +       unsigned long *bitmap;
26651 +       struct au_si_pid *au_si_pid;
26652 +
26653 +       si_pid_idx_bit(&idx, &bit);
26654 +       au_si_pid = &au_sbi(sb)->au_si_pid;
26655 +       bitmap = au_si_pid->pid_bitmap[idx];
26656 +       if (!bitmap) {
26657 +               si_pid_alloc(au_si_pid, idx);
26658 +               bitmap = au_si_pid->pid_bitmap[idx];
26659 +       }
26660 +       AuDebugOn(test_bit(bit, bitmap));
26661 +       set_bit(bit, bitmap);
26662 +       /* smp_mb(); */
26663 +}
26664 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26665 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
26666 +++ linux/fs/aufs/spl.h 2016-04-24 18:32:51.393686895 +0200
26667 @@ -0,0 +1,111 @@
26668 +/*
26669 + * Copyright (C) 2005-2016 Junjiro R. Okajima
26670 + *
26671 + * This program, aufs is free software; you can redistribute it and/or modify
26672 + * it under the terms of the GNU General Public License as published by
26673 + * the Free Software Foundation; either version 2 of the License, or
26674 + * (at your option) any later version.
26675 + *
26676 + * This program is distributed in the hope that it will be useful,
26677 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26678 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26679 + * GNU General Public License for more details.
26680 + *
26681 + * You should have received a copy of the GNU General Public License
26682 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26683 + */
26684 +
26685 +/*
26686 + * simple list protected by a spinlock
26687 + */
26688 +
26689 +#ifndef __AUFS_SPL_H__
26690 +#define __AUFS_SPL_H__
26691 +
26692 +#ifdef __KERNEL__
26693 +
26694 +struct au_splhead {
26695 +       spinlock_t              spin;
26696 +       struct list_head        head;
26697 +};
26698 +
26699 +static inline void au_spl_init(struct au_splhead *spl)
26700 +{
26701 +       spin_lock_init(&spl->spin);
26702 +       INIT_LIST_HEAD(&spl->head);
26703 +}
26704 +
26705 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26706 +{
26707 +       spin_lock(&spl->spin);
26708 +       list_add(list, &spl->head);
26709 +       spin_unlock(&spl->spin);
26710 +}
26711 +
26712 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26713 +{
26714 +       spin_lock(&spl->spin);
26715 +       list_del(list);
26716 +       spin_unlock(&spl->spin);
26717 +}
26718 +
26719 +static inline void au_spl_del_rcu(struct list_head *list,
26720 +                                 struct au_splhead *spl)
26721 +{
26722 +       spin_lock(&spl->spin);
26723 +       list_del_rcu(list);
26724 +       spin_unlock(&spl->spin);
26725 +}
26726 +
26727 +/* ---------------------------------------------------------------------- */
26728 +
26729 +struct au_sphlhead {
26730 +       spinlock_t              spin;
26731 +       struct hlist_head       head;
26732 +};
26733 +
26734 +static inline void au_sphl_init(struct au_sphlhead *sphl)
26735 +{
26736 +       spin_lock_init(&sphl->spin);
26737 +       INIT_HLIST_HEAD(&sphl->head);
26738 +}
26739 +
26740 +static inline void au_sphl_add(struct hlist_node *hlist,
26741 +                              struct au_sphlhead *sphl)
26742 +{
26743 +       spin_lock(&sphl->spin);
26744 +       hlist_add_head(hlist, &sphl->head);
26745 +       spin_unlock(&sphl->spin);
26746 +}
26747 +
26748 +static inline void au_sphl_del(struct hlist_node *hlist,
26749 +                              struct au_sphlhead *sphl)
26750 +{
26751 +       spin_lock(&sphl->spin);
26752 +       hlist_del(hlist);
26753 +       spin_unlock(&sphl->spin);
26754 +}
26755 +
26756 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26757 +                                  struct au_sphlhead *sphl)
26758 +{
26759 +       spin_lock(&sphl->spin);
26760 +       hlist_del_rcu(hlist);
26761 +       spin_unlock(&sphl->spin);
26762 +}
26763 +
26764 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26765 +{
26766 +       unsigned long cnt;
26767 +       struct hlist_node *pos;
26768 +
26769 +       cnt = 0;
26770 +       spin_lock(&sphl->spin);
26771 +       hlist_for_each(pos, &sphl->head)
26772 +               cnt++;
26773 +       spin_unlock(&sphl->spin);
26774 +       return cnt;
26775 +}
26776 +
26777 +#endif /* __KERNEL__ */
26778 +#endif /* __AUFS_SPL_H__ */
26779 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26780 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
26781 +++ linux/fs/aufs/super.c       2016-04-24 18:32:51.393686895 +0200
26782 @@ -0,0 +1,1039 @@
26783 +/*
26784 + * Copyright (C) 2005-2016 Junjiro R. Okajima
26785 + *
26786 + * This program, aufs is free software; you can redistribute it and/or modify
26787 + * it under the terms of the GNU General Public License as published by
26788 + * the Free Software Foundation; either version 2 of the License, or
26789 + * (at your option) any later version.
26790 + *
26791 + * This program is distributed in the hope that it will be useful,
26792 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26793 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26794 + * GNU General Public License for more details.
26795 + *
26796 + * You should have received a copy of the GNU General Public License
26797 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26798 + */
26799 +
26800 +/*
26801 + * mount and super_block operations
26802 + */
26803 +
26804 +#include <linux/mm.h>
26805 +#include <linux/seq_file.h>
26806 +#include <linux/statfs.h>
26807 +#include <linux/vmalloc.h>
26808 +#include "aufs.h"
26809 +
26810 +/*
26811 + * super_operations
26812 + */
26813 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26814 +{
26815 +       struct au_icntnr *c;
26816 +
26817 +       c = au_cache_alloc_icntnr();
26818 +       if (c) {
26819 +               au_icntnr_init(c);
26820 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
26821 +               c->iinfo.ii_hinode = NULL;
26822 +               return &c->vfs_inode;
26823 +       }
26824 +       return NULL;
26825 +}
26826 +
26827 +static void aufs_destroy_inode_cb(struct rcu_head *head)
26828 +{
26829 +       struct inode *inode = container_of(head, struct inode, i_rcu);
26830 +
26831 +       INIT_HLIST_HEAD(&inode->i_dentry);
26832 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26833 +}
26834 +
26835 +static void aufs_destroy_inode(struct inode *inode)
26836 +{
26837 +       au_iinfo_fin(inode);
26838 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
26839 +}
26840 +
26841 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26842 +{
26843 +       struct inode *inode;
26844 +       int err;
26845 +
26846 +       inode = iget_locked(sb, ino);
26847 +       if (unlikely(!inode)) {
26848 +               inode = ERR_PTR(-ENOMEM);
26849 +               goto out;
26850 +       }
26851 +       if (!(inode->i_state & I_NEW))
26852 +               goto out;
26853 +
26854 +       err = au_xigen_new(inode);
26855 +       if (!err)
26856 +               err = au_iinfo_init(inode);
26857 +       if (!err)
26858 +               inode->i_version++;
26859 +       else {
26860 +               iget_failed(inode);
26861 +               inode = ERR_PTR(err);
26862 +       }
26863 +
26864 +out:
26865 +       /* never return NULL */
26866 +       AuDebugOn(!inode);
26867 +       AuTraceErrPtr(inode);
26868 +       return inode;
26869 +}
26870 +
26871 +/* lock free root dinfo */
26872 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26873 +{
26874 +       int err;
26875 +       aufs_bindex_t bindex, bend;
26876 +       struct path path;
26877 +       struct au_hdentry *hdp;
26878 +       struct au_branch *br;
26879 +       au_br_perm_str_t perm;
26880 +
26881 +       err = 0;
26882 +       bend = au_sbend(sb);
26883 +       hdp = au_di(sb->s_root)->di_hdentry;
26884 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
26885 +               br = au_sbr(sb, bindex);
26886 +               path.mnt = au_br_mnt(br);
26887 +               path.dentry = hdp[bindex].hd_dentry;
26888 +               err = au_seq_path(seq, &path);
26889 +               if (!err) {
26890 +                       au_optstr_br_perm(&perm, br->br_perm);
26891 +                       seq_printf(seq, "=%s", perm.a);
26892 +                       if (bindex != bend)
26893 +                               seq_putc(seq, ':');
26894 +               }
26895 +       }
26896 +       if (unlikely(err || seq_has_overflowed(seq)))
26897 +               err = -E2BIG;
26898 +
26899 +       return err;
26900 +}
26901 +
26902 +static void au_show_wbr_create(struct seq_file *m, int v,
26903 +                              struct au_sbinfo *sbinfo)
26904 +{
26905 +       const char *pat;
26906 +
26907 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26908 +
26909 +       seq_puts(m, ",create=");
26910 +       pat = au_optstr_wbr_create(v);
26911 +       switch (v) {
26912 +       case AuWbrCreate_TDP:
26913 +       case AuWbrCreate_RR:
26914 +       case AuWbrCreate_MFS:
26915 +       case AuWbrCreate_PMFS:
26916 +               seq_puts(m, pat);
26917 +               break;
26918 +       case AuWbrCreate_MFSV:
26919 +               seq_printf(m, /*pat*/"mfs:%lu",
26920 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26921 +                          / MSEC_PER_SEC);
26922 +               break;
26923 +       case AuWbrCreate_PMFSV:
26924 +               seq_printf(m, /*pat*/"pmfs:%lu",
26925 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26926 +                          / MSEC_PER_SEC);
26927 +               break;
26928 +       case AuWbrCreate_MFSRR:
26929 +               seq_printf(m, /*pat*/"mfsrr:%llu",
26930 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26931 +               break;
26932 +       case AuWbrCreate_MFSRRV:
26933 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26934 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26935 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26936 +                          / MSEC_PER_SEC);
26937 +               break;
26938 +       case AuWbrCreate_PMFSRR:
26939 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
26940 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26941 +               break;
26942 +       case AuWbrCreate_PMFSRRV:
26943 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
26944 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26945 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26946 +                          / MSEC_PER_SEC);
26947 +               break;
26948 +       }
26949 +}
26950 +
26951 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
26952 +{
26953 +#ifdef CONFIG_SYSFS
26954 +       return 0;
26955 +#else
26956 +       int err;
26957 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
26958 +       aufs_bindex_t bindex, brid;
26959 +       struct qstr *name;
26960 +       struct file *f;
26961 +       struct dentry *d, *h_root;
26962 +       struct au_hdentry *hdp;
26963 +
26964 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26965 +
26966 +       err = 0;
26967 +       f = au_sbi(sb)->si_xib;
26968 +       if (!f)
26969 +               goto out;
26970 +
26971 +       /* stop printing the default xino path on the first writable branch */
26972 +       h_root = NULL;
26973 +       brid = au_xino_brid(sb);
26974 +       if (brid >= 0) {
26975 +               bindex = au_br_index(sb, brid);
26976 +               hdp = au_di(sb->s_root)->di_hdentry;
26977 +               h_root = hdp[0 + bindex].hd_dentry;
26978 +       }
26979 +       d = f->f_path.dentry;
26980 +       name = &d->d_name;
26981 +       /* safe ->d_parent because the file is unlinked */
26982 +       if (d->d_parent == h_root
26983 +           && name->len == len
26984 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
26985 +               goto out;
26986 +
26987 +       seq_puts(seq, ",xino=");
26988 +       err = au_xino_path(seq, f);
26989 +
26990 +out:
26991 +       return err;
26992 +#endif
26993 +}
26994 +
26995 +/* seq_file will re-call me in case of too long string */
26996 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
26997 +{
26998 +       int err;
26999 +       unsigned int mnt_flags, v;
27000 +       struct super_block *sb;
27001 +       struct au_sbinfo *sbinfo;
27002 +
27003 +#define AuBool(name, str) do { \
27004 +       v = au_opt_test(mnt_flags, name); \
27005 +       if (v != au_opt_test(AuOpt_Def, name)) \
27006 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
27007 +} while (0)
27008 +
27009 +#define AuStr(name, str) do { \
27010 +       v = mnt_flags & AuOptMask_##name; \
27011 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
27012 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
27013 +} while (0)
27014 +
27015 +#define AuUInt(name, str, val) do { \
27016 +       if (val != AUFS_##name##_DEF) \
27017 +               seq_printf(m, "," #str "=%u", val); \
27018 +} while (0)
27019 +
27020 +       sb = dentry->d_sb;
27021 +       if (sb->s_flags & MS_POSIXACL)
27022 +               seq_puts(m, ",acl");
27023 +
27024 +       /* lock free root dinfo */
27025 +       si_noflush_read_lock(sb);
27026 +       sbinfo = au_sbi(sb);
27027 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
27028 +
27029 +       mnt_flags = au_mntflags(sb);
27030 +       if (au_opt_test(mnt_flags, XINO)) {
27031 +               err = au_show_xino(m, sb);
27032 +               if (unlikely(err))
27033 +                       goto out;
27034 +       } else
27035 +               seq_puts(m, ",noxino");
27036 +
27037 +       AuBool(TRUNC_XINO, trunc_xino);
27038 +       AuStr(UDBA, udba);
27039 +       AuBool(SHWH, shwh);
27040 +       AuBool(PLINK, plink);
27041 +       AuBool(DIO, dio);
27042 +       AuBool(DIRPERM1, dirperm1);
27043 +
27044 +       v = sbinfo->si_wbr_create;
27045 +       if (v != AuWbrCreate_Def)
27046 +               au_show_wbr_create(m, v, sbinfo);
27047 +
27048 +       v = sbinfo->si_wbr_copyup;
27049 +       if (v != AuWbrCopyup_Def)
27050 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
27051 +
27052 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
27053 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
27054 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
27055 +
27056 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
27057 +
27058 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
27059 +       AuUInt(RDCACHE, rdcache, v);
27060 +
27061 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
27062 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
27063 +
27064 +       au_fhsm_show(m, sbinfo);
27065 +
27066 +       AuBool(SUM, sum);
27067 +       /* AuBool(SUM_W, wsum); */
27068 +       AuBool(WARN_PERM, warn_perm);
27069 +       AuBool(VERBOSE, verbose);
27070 +
27071 +out:
27072 +       /* be sure to print "br:" last */
27073 +       if (!sysaufs_brs) {
27074 +               seq_puts(m, ",br:");
27075 +               au_show_brs(m, sb);
27076 +       }
27077 +       si_read_unlock(sb);
27078 +       return 0;
27079 +
27080 +#undef AuBool
27081 +#undef AuStr
27082 +#undef AuUInt
27083 +}
27084 +
27085 +/* ---------------------------------------------------------------------- */
27086 +
27087 +/* sum mode which returns the summation for statfs(2) */
27088 +
27089 +static u64 au_add_till_max(u64 a, u64 b)
27090 +{
27091 +       u64 old;
27092 +
27093 +       old = a;
27094 +       a += b;
27095 +       if (old <= a)
27096 +               return a;
27097 +       return ULLONG_MAX;
27098 +}
27099 +
27100 +static u64 au_mul_till_max(u64 a, long mul)
27101 +{
27102 +       u64 old;
27103 +
27104 +       old = a;
27105 +       a *= mul;
27106 +       if (old <= a)
27107 +               return a;
27108 +       return ULLONG_MAX;
27109 +}
27110 +
27111 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27112 +{
27113 +       int err;
27114 +       long bsize, factor;
27115 +       u64 blocks, bfree, bavail, files, ffree;
27116 +       aufs_bindex_t bend, bindex, i;
27117 +       unsigned char shared;
27118 +       struct path h_path;
27119 +       struct super_block *h_sb;
27120 +
27121 +       err = 0;
27122 +       bsize = LONG_MAX;
27123 +       files = 0;
27124 +       ffree = 0;
27125 +       blocks = 0;
27126 +       bfree = 0;
27127 +       bavail = 0;
27128 +       bend = au_sbend(sb);
27129 +       for (bindex = 0; bindex <= bend; bindex++) {
27130 +               h_path.mnt = au_sbr_mnt(sb, bindex);
27131 +               h_sb = h_path.mnt->mnt_sb;
27132 +               shared = 0;
27133 +               for (i = 0; !shared && i < bindex; i++)
27134 +                       shared = (au_sbr_sb(sb, i) == h_sb);
27135 +               if (shared)
27136 +                       continue;
27137 +
27138 +               /* sb->s_root for NFS is unreliable */
27139 +               h_path.dentry = h_path.mnt->mnt_root;
27140 +               err = vfs_statfs(&h_path, buf);
27141 +               if (unlikely(err))
27142 +                       goto out;
27143 +
27144 +               if (bsize > buf->f_bsize) {
27145 +                       /*
27146 +                        * we will reduce bsize, so we have to expand blocks
27147 +                        * etc. to match them again
27148 +                        */
27149 +                       factor = (bsize / buf->f_bsize);
27150 +                       blocks = au_mul_till_max(blocks, factor);
27151 +                       bfree = au_mul_till_max(bfree, factor);
27152 +                       bavail = au_mul_till_max(bavail, factor);
27153 +                       bsize = buf->f_bsize;
27154 +               }
27155 +
27156 +               factor = (buf->f_bsize / bsize);
27157 +               blocks = au_add_till_max(blocks,
27158 +                               au_mul_till_max(buf->f_blocks, factor));
27159 +               bfree = au_add_till_max(bfree,
27160 +                               au_mul_till_max(buf->f_bfree, factor));
27161 +               bavail = au_add_till_max(bavail,
27162 +                               au_mul_till_max(buf->f_bavail, factor));
27163 +               files = au_add_till_max(files, buf->f_files);
27164 +               ffree = au_add_till_max(ffree, buf->f_ffree);
27165 +       }
27166 +
27167 +       buf->f_bsize = bsize;
27168 +       buf->f_blocks = blocks;
27169 +       buf->f_bfree = bfree;
27170 +       buf->f_bavail = bavail;
27171 +       buf->f_files = files;
27172 +       buf->f_ffree = ffree;
27173 +       buf->f_frsize = 0;
27174 +
27175 +out:
27176 +       return err;
27177 +}
27178 +
27179 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27180 +{
27181 +       int err;
27182 +       struct path h_path;
27183 +       struct super_block *sb;
27184 +
27185 +       /* lock free root dinfo */
27186 +       sb = dentry->d_sb;
27187 +       si_noflush_read_lock(sb);
27188 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
27189 +               /* sb->s_root for NFS is unreliable */
27190 +               h_path.mnt = au_sbr_mnt(sb, 0);
27191 +               h_path.dentry = h_path.mnt->mnt_root;
27192 +               err = vfs_statfs(&h_path, buf);
27193 +       } else
27194 +               err = au_statfs_sum(sb, buf);
27195 +       si_read_unlock(sb);
27196 +
27197 +       if (!err) {
27198 +               buf->f_type = AUFS_SUPER_MAGIC;
27199 +               buf->f_namelen = AUFS_MAX_NAMELEN;
27200 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27201 +       }
27202 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27203 +
27204 +       return err;
27205 +}
27206 +
27207 +/* ---------------------------------------------------------------------- */
27208 +
27209 +static int aufs_sync_fs(struct super_block *sb, int wait)
27210 +{
27211 +       int err, e;
27212 +       aufs_bindex_t bend, bindex;
27213 +       struct au_branch *br;
27214 +       struct super_block *h_sb;
27215 +
27216 +       err = 0;
27217 +       si_noflush_read_lock(sb);
27218 +       bend = au_sbend(sb);
27219 +       for (bindex = 0; bindex <= bend; bindex++) {
27220 +               br = au_sbr(sb, bindex);
27221 +               if (!au_br_writable(br->br_perm))
27222 +                       continue;
27223 +
27224 +               h_sb = au_sbr_sb(sb, bindex);
27225 +               if (h_sb->s_op->sync_fs) {
27226 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
27227 +                       if (unlikely(e && !err))
27228 +                               err = e;
27229 +                       /* go on even if an error happens */
27230 +               }
27231 +       }
27232 +       si_read_unlock(sb);
27233 +
27234 +       return err;
27235 +}
27236 +
27237 +/* ---------------------------------------------------------------------- */
27238 +
27239 +/* final actions when unmounting a file system */
27240 +static void aufs_put_super(struct super_block *sb)
27241 +{
27242 +       struct au_sbinfo *sbinfo;
27243 +
27244 +       sbinfo = au_sbi(sb);
27245 +       if (!sbinfo)
27246 +               return;
27247 +
27248 +       dbgaufs_si_fin(sbinfo);
27249 +       kobject_put(&sbinfo->si_kobj);
27250 +}
27251 +
27252 +/* ---------------------------------------------------------------------- */
27253 +
27254 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27255 +                    struct super_block *sb, void *arg)
27256 +{
27257 +       void *array;
27258 +       unsigned long long n, sz;
27259 +
27260 +       array = NULL;
27261 +       n = 0;
27262 +       if (!*hint)
27263 +               goto out;
27264 +
27265 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27266 +               array = ERR_PTR(-EMFILE);
27267 +               pr_err("hint %llu\n", *hint);
27268 +               goto out;
27269 +       }
27270 +
27271 +       sz = sizeof(array) * *hint;
27272 +       array = kzalloc(sz, GFP_NOFS);
27273 +       if (unlikely(!array))
27274 +               array = vzalloc(sz);
27275 +       if (unlikely(!array)) {
27276 +               array = ERR_PTR(-ENOMEM);
27277 +               goto out;
27278 +       }
27279 +
27280 +       n = cb(sb, array, *hint, arg);
27281 +       AuDebugOn(n > *hint);
27282 +
27283 +out:
27284 +       *hint = n;
27285 +       return array;
27286 +}
27287 +
27288 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
27289 +                                      unsigned long long max __maybe_unused,
27290 +                                      void *arg)
27291 +{
27292 +       unsigned long long n;
27293 +       struct inode **p, *inode;
27294 +       struct list_head *head;
27295 +
27296 +       n = 0;
27297 +       p = a;
27298 +       head = arg;
27299 +       spin_lock(&sb->s_inode_list_lock);
27300 +       list_for_each_entry(inode, head, i_sb_list) {
27301 +               if (!is_bad_inode(inode)
27302 +                   && au_ii(inode)->ii_bstart >= 0) {
27303 +                       spin_lock(&inode->i_lock);
27304 +                       if (atomic_read(&inode->i_count)) {
27305 +                               au_igrab(inode);
27306 +                               *p++ = inode;
27307 +                               n++;
27308 +                               AuDebugOn(n > max);
27309 +                       }
27310 +                       spin_unlock(&inode->i_lock);
27311 +               }
27312 +       }
27313 +       spin_unlock(&sb->s_inode_list_lock);
27314 +
27315 +       return n;
27316 +}
27317 +
27318 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27319 +{
27320 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
27321 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
27322 +}
27323 +
27324 +void au_iarray_free(struct inode **a, unsigned long long max)
27325 +{
27326 +       unsigned long long ull;
27327 +
27328 +       for (ull = 0; ull < max; ull++)
27329 +               iput(a[ull]);
27330 +       kvfree(a);
27331 +}
27332 +
27333 +/* ---------------------------------------------------------------------- */
27334 +
27335 +/*
27336 + * refresh dentry and inode at remount time.
27337 + */
27338 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27339 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27340 +                     struct dentry *parent)
27341 +{
27342 +       int err;
27343 +
27344 +       di_write_lock_child(dentry);
27345 +       di_read_lock_parent(parent, AuLock_IR);
27346 +       err = au_refresh_dentry(dentry, parent);
27347 +       if (!err && dir_flags)
27348 +               au_hn_reset(d_inode(dentry), dir_flags);
27349 +       di_read_unlock(parent, AuLock_IR);
27350 +       di_write_unlock(dentry);
27351 +
27352 +       return err;
27353 +}
27354 +
27355 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27356 +                          struct au_sbinfo *sbinfo,
27357 +                          const unsigned int dir_flags, unsigned int do_idop)
27358 +{
27359 +       int err;
27360 +       struct dentry *parent;
27361 +
27362 +       err = 0;
27363 +       parent = dget_parent(dentry);
27364 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27365 +               if (d_really_is_positive(dentry)) {
27366 +                       if (!d_is_dir(dentry))
27367 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27368 +                                                parent);
27369 +                       else {
27370 +                               err = au_do_refresh(dentry, dir_flags, parent);
27371 +                               if (unlikely(err))
27372 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27373 +                       }
27374 +               } else
27375 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27376 +               AuDbgDentry(dentry);
27377 +       }
27378 +       dput(parent);
27379 +
27380 +       if (!err) {
27381 +               if (do_idop)
27382 +                       au_refresh_dop(dentry, /*force_reval*/0);
27383 +       } else
27384 +               au_refresh_dop(dentry, /*force_reval*/1);
27385 +
27386 +       AuTraceErr(err);
27387 +       return err;
27388 +}
27389 +
27390 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
27391 +{
27392 +       int err, i, j, ndentry, e;
27393 +       unsigned int sigen;
27394 +       struct au_dcsub_pages dpages;
27395 +       struct au_dpage *dpage;
27396 +       struct dentry **dentries, *d;
27397 +       struct au_sbinfo *sbinfo;
27398 +       struct dentry *root = sb->s_root;
27399 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
27400 +
27401 +       if (do_idop)
27402 +               au_refresh_dop(root, /*force_reval*/0);
27403 +
27404 +       err = au_dpages_init(&dpages, GFP_NOFS);
27405 +       if (unlikely(err))
27406 +               goto out;
27407 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27408 +       if (unlikely(err))
27409 +               goto out_dpages;
27410 +
27411 +       sigen = au_sigen(sb);
27412 +       sbinfo = au_sbi(sb);
27413 +       for (i = 0; i < dpages.ndpage; i++) {
27414 +               dpage = dpages.dpages + i;
27415 +               dentries = dpage->dentries;
27416 +               ndentry = dpage->ndentry;
27417 +               for (j = 0; j < ndentry; j++) {
27418 +                       d = dentries[j];
27419 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
27420 +                                           do_idop);
27421 +                       if (unlikely(e && !err))
27422 +                               err = e;
27423 +                       /* go on even err */
27424 +               }
27425 +       }
27426 +
27427 +out_dpages:
27428 +       au_dpages_free(&dpages);
27429 +out:
27430 +       return err;
27431 +}
27432 +
27433 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
27434 +{
27435 +       int err, e;
27436 +       unsigned int sigen;
27437 +       unsigned long long max, ull;
27438 +       struct inode *inode, **array;
27439 +
27440 +       array = au_iarray_alloc(sb, &max);
27441 +       err = PTR_ERR(array);
27442 +       if (IS_ERR(array))
27443 +               goto out;
27444 +
27445 +       err = 0;
27446 +       sigen = au_sigen(sb);
27447 +       for (ull = 0; ull < max; ull++) {
27448 +               inode = array[ull];
27449 +               if (unlikely(!inode))
27450 +                       break;
27451 +
27452 +               e = 0;
27453 +               ii_write_lock_child(inode);
27454 +               if (au_iigen(inode, NULL) != sigen) {
27455 +                       e = au_refresh_hinode_self(inode);
27456 +                       if (unlikely(e)) {
27457 +                               au_refresh_iop(inode, /*force_getattr*/1);
27458 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27459 +                               if (!err)
27460 +                                       err = e;
27461 +                               /* go on even if err */
27462 +                       }
27463 +               }
27464 +               if (!e && do_idop)
27465 +                       au_refresh_iop(inode, /*force_getattr*/0);
27466 +               ii_write_unlock(inode);
27467 +       }
27468 +
27469 +       au_iarray_free(array, max);
27470 +
27471 +out:
27472 +       return err;
27473 +}
27474 +
27475 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
27476 +{
27477 +       int err, e;
27478 +       unsigned int udba;
27479 +       aufs_bindex_t bindex, bend;
27480 +       struct dentry *root;
27481 +       struct inode *inode;
27482 +       struct au_branch *br;
27483 +       struct au_sbinfo *sbi;
27484 +
27485 +       au_sigen_inc(sb);
27486 +       sbi = au_sbi(sb);
27487 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
27488 +
27489 +       root = sb->s_root;
27490 +       DiMustNoWaiters(root);
27491 +       inode = d_inode(root);
27492 +       IiMustNoWaiters(inode);
27493 +
27494 +       udba = au_opt_udba(sb);
27495 +       bend = au_sbend(sb);
27496 +       for (bindex = 0; bindex <= bend; bindex++) {
27497 +               br = au_sbr(sb, bindex);
27498 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27499 +               if (unlikely(err))
27500 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27501 +                               bindex, err);
27502 +               /* go on even if err */
27503 +       }
27504 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27505 +
27506 +       if (do_idop) {
27507 +               if (au_ftest_si(sbi, NO_DREVAL)) {
27508 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27509 +                       sb->s_d_op = &aufs_dop_noreval;
27510 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27511 +                       sbi->si_iop_array = aufs_iop_nogetattr;
27512 +               } else {
27513 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
27514 +                       sb->s_d_op = &aufs_dop;
27515 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
27516 +                       sbi->si_iop_array = aufs_iop;
27517 +               }
27518 +               pr_info("reset to %pf and %pf\n",
27519 +                       sb->s_d_op, sbi->si_iop_array);
27520 +       }
27521 +
27522 +       di_write_unlock(root);
27523 +       err = au_refresh_d(sb, do_idop);
27524 +       e = au_refresh_i(sb, do_idop);
27525 +       if (unlikely(e && !err))
27526 +               err = e;
27527 +       /* aufs_write_lock() calls ..._child() */
27528 +       di_write_lock_child(root);
27529 +
27530 +       au_cpup_attr_all(inode, /*force*/1);
27531 +
27532 +       if (unlikely(err))
27533 +               AuIOErr("refresh failed, ignored, %d\n", err);
27534 +}
27535 +
27536 +/* stop extra interpretation of errno in mount(8), and strange error messages */
27537 +static int cvt_err(int err)
27538 +{
27539 +       AuTraceErr(err);
27540 +
27541 +       switch (err) {
27542 +       case -ENOENT:
27543 +       case -ENOTDIR:
27544 +       case -EEXIST:
27545 +       case -EIO:
27546 +               err = -EINVAL;
27547 +       }
27548 +       return err;
27549 +}
27550 +
27551 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27552 +{
27553 +       int err, do_dx;
27554 +       unsigned int mntflags;
27555 +       struct au_opts opts = {
27556 +               .opt = NULL
27557 +       };
27558 +       struct dentry *root;
27559 +       struct inode *inode;
27560 +       struct au_sbinfo *sbinfo;
27561 +
27562 +       err = 0;
27563 +       root = sb->s_root;
27564 +       if (!data || !*data) {
27565 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27566 +               if (!err) {
27567 +                       di_write_lock_child(root);
27568 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
27569 +                       aufs_write_unlock(root);
27570 +               }
27571 +               goto out;
27572 +       }
27573 +
27574 +       err = -ENOMEM;
27575 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27576 +       if (unlikely(!opts.opt))
27577 +               goto out;
27578 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27579 +       opts.flags = AuOpts_REMOUNT;
27580 +       opts.sb_flags = *flags;
27581 +
27582 +       /* parse it before aufs lock */
27583 +       err = au_opts_parse(sb, data, &opts);
27584 +       if (unlikely(err))
27585 +               goto out_opts;
27586 +
27587 +       sbinfo = au_sbi(sb);
27588 +       inode = d_inode(root);
27589 +       inode_lock(inode);
27590 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27591 +       if (unlikely(err))
27592 +               goto out_mtx;
27593 +       di_write_lock_child(root);
27594 +
27595 +       /* au_opts_remount() may return an error */
27596 +       err = au_opts_remount(sb, &opts);
27597 +       au_opts_free(&opts);
27598 +
27599 +       if (au_ftest_opts(opts.flags, REFRESH))
27600 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
27601 +
27602 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27603 +               mntflags = au_mntflags(sb);
27604 +               do_dx = !!au_opt_test(mntflags, DIO);
27605 +               au_dy_arefresh(do_dx);
27606 +       }
27607 +
27608 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
27609 +       aufs_write_unlock(root);
27610 +
27611 +out_mtx:
27612 +       inode_unlock(inode);
27613 +out_opts:
27614 +       free_page((unsigned long)opts.opt);
27615 +out:
27616 +       err = cvt_err(err);
27617 +       AuTraceErr(err);
27618 +       return err;
27619 +}
27620 +
27621 +static const struct super_operations aufs_sop = {
27622 +       .alloc_inode    = aufs_alloc_inode,
27623 +       .destroy_inode  = aufs_destroy_inode,
27624 +       /* always deleting, no clearing */
27625 +       .drop_inode     = generic_delete_inode,
27626 +       .show_options   = aufs_show_options,
27627 +       .statfs         = aufs_statfs,
27628 +       .put_super      = aufs_put_super,
27629 +       .sync_fs        = aufs_sync_fs,
27630 +       .remount_fs     = aufs_remount_fs
27631 +};
27632 +
27633 +/* ---------------------------------------------------------------------- */
27634 +
27635 +static int alloc_root(struct super_block *sb)
27636 +{
27637 +       int err;
27638 +       struct inode *inode;
27639 +       struct dentry *root;
27640 +
27641 +       err = -ENOMEM;
27642 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
27643 +       err = PTR_ERR(inode);
27644 +       if (IS_ERR(inode))
27645 +               goto out;
27646 +
27647 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
27648 +       inode->i_fop = &aufs_dir_fop;
27649 +       inode->i_mode = S_IFDIR;
27650 +       set_nlink(inode, 2);
27651 +       unlock_new_inode(inode);
27652 +
27653 +       root = d_make_root(inode);
27654 +       if (unlikely(!root))
27655 +               goto out;
27656 +       err = PTR_ERR(root);
27657 +       if (IS_ERR(root))
27658 +               goto out;
27659 +
27660 +       err = au_di_init(root);
27661 +       if (!err) {
27662 +               sb->s_root = root;
27663 +               return 0; /* success */
27664 +       }
27665 +       dput(root);
27666 +
27667 +out:
27668 +       return err;
27669 +}
27670 +
27671 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
27672 +                          int silent __maybe_unused)
27673 +{
27674 +       int err;
27675 +       struct au_opts opts = {
27676 +               .opt = NULL
27677 +       };
27678 +       struct au_sbinfo *sbinfo;
27679 +       struct dentry *root;
27680 +       struct inode *inode;
27681 +       char *arg = raw_data;
27682 +
27683 +       if (unlikely(!arg || !*arg)) {
27684 +               err = -EINVAL;
27685 +               pr_err("no arg\n");
27686 +               goto out;
27687 +       }
27688 +
27689 +       err = -ENOMEM;
27690 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27691 +       if (unlikely(!opts.opt))
27692 +               goto out;
27693 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27694 +       opts.sb_flags = sb->s_flags;
27695 +
27696 +       err = au_si_alloc(sb);
27697 +       if (unlikely(err))
27698 +               goto out_opts;
27699 +       sbinfo = au_sbi(sb);
27700 +
27701 +       /* all timestamps always follow the ones on the branch */
27702 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27703 +       sb->s_op = &aufs_sop;
27704 +       sb->s_d_op = &aufs_dop;
27705 +       sb->s_magic = AUFS_SUPER_MAGIC;
27706 +       sb->s_maxbytes = 0;
27707 +       sb->s_stack_depth = 1;
27708 +       au_export_init(sb);
27709 +       /* au_xattr_init(sb); */
27710 +
27711 +       err = alloc_root(sb);
27712 +       if (unlikely(err)) {
27713 +               si_write_unlock(sb);
27714 +               goto out_info;
27715 +       }
27716 +       root = sb->s_root;
27717 +       inode = d_inode(root);
27718 +
27719 +       /*
27720 +        * actually we can parse options regardless aufs lock here.
27721 +        * but at remount time, parsing must be done before aufs lock.
27722 +        * so we follow the same rule.
27723 +        */
27724 +       ii_write_lock_parent(inode);
27725 +       aufs_write_unlock(root);
27726 +       err = au_opts_parse(sb, arg, &opts);
27727 +       if (unlikely(err))
27728 +               goto out_root;
27729 +
27730 +       /* lock vfs_inode first, then aufs. */
27731 +       inode_lock(inode);
27732 +       aufs_write_lock(root);
27733 +       err = au_opts_mount(sb, &opts);
27734 +       au_opts_free(&opts);
27735 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
27736 +               sb->s_d_op = &aufs_dop_noreval;
27737 +               pr_info("%pf\n", sb->s_d_op);
27738 +               au_refresh_dop(root, /*force_reval*/0);
27739 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
27740 +               au_refresh_iop(inode, /*force_getattr*/0);
27741 +       }
27742 +       aufs_write_unlock(root);
27743 +       inode_unlock(inode);
27744 +       if (!err)
27745 +               goto out_opts; /* success */
27746 +
27747 +out_root:
27748 +       dput(root);
27749 +       sb->s_root = NULL;
27750 +out_info:
27751 +       dbgaufs_si_fin(sbinfo);
27752 +       kobject_put(&sbinfo->si_kobj);
27753 +       sb->s_fs_info = NULL;
27754 +out_opts:
27755 +       free_page((unsigned long)opts.opt);
27756 +out:
27757 +       AuTraceErr(err);
27758 +       err = cvt_err(err);
27759 +       AuTraceErr(err);
27760 +       return err;
27761 +}
27762 +
27763 +/* ---------------------------------------------------------------------- */
27764 +
27765 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27766 +                                const char *dev_name __maybe_unused,
27767 +                                void *raw_data)
27768 +{
27769 +       struct dentry *root;
27770 +       struct super_block *sb;
27771 +
27772 +       /* all timestamps always follow the ones on the branch */
27773 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
27774 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27775 +       if (IS_ERR(root))
27776 +               goto out;
27777 +
27778 +       sb = root->d_sb;
27779 +       si_write_lock(sb, !AuLock_FLUSH);
27780 +       sysaufs_brs_add(sb, 0);
27781 +       si_write_unlock(sb);
27782 +       au_sbilist_add(sb);
27783 +
27784 +out:
27785 +       return root;
27786 +}
27787 +
27788 +static void aufs_kill_sb(struct super_block *sb)
27789 +{
27790 +       struct au_sbinfo *sbinfo;
27791 +
27792 +       sbinfo = au_sbi(sb);
27793 +       if (sbinfo) {
27794 +               au_sbilist_del(sb);
27795 +               aufs_write_lock(sb->s_root);
27796 +               au_fhsm_fin(sb);
27797 +               if (sbinfo->si_wbr_create_ops->fin)
27798 +                       sbinfo->si_wbr_create_ops->fin(sb);
27799 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27800 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
27801 +                       au_remount_refresh(sb, /*do_idop*/0);
27802 +               }
27803 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27804 +                       au_plink_put(sb, /*verbose*/1);
27805 +               au_xino_clr(sb);
27806 +               sbinfo->si_sb = NULL;
27807 +               aufs_write_unlock(sb->s_root);
27808 +               au_nwt_flush(&sbinfo->si_nowait);
27809 +       }
27810 +       kill_anon_super(sb);
27811 +}
27812 +
27813 +struct file_system_type aufs_fs_type = {
27814 +       .name           = AUFS_FSTYPE,
27815 +       /* a race between rename and others */
27816 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
27817 +       .mount          = aufs_mount,
27818 +       .kill_sb        = aufs_kill_sb,
27819 +       /* no need to __module_get() and module_put(). */
27820 +       .owner          = THIS_MODULE,
27821 +};
27822 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27823 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
27824 +++ linux/fs/aufs/super.h       2016-04-24 18:32:51.393686895 +0200
27825 @@ -0,0 +1,632 @@
27826 +/*
27827 + * Copyright (C) 2005-2016 Junjiro R. Okajima
27828 + *
27829 + * This program, aufs is free software; you can redistribute it and/or modify
27830 + * it under the terms of the GNU General Public License as published by
27831 + * the Free Software Foundation; either version 2 of the License, or
27832 + * (at your option) any later version.
27833 + *
27834 + * This program is distributed in the hope that it will be useful,
27835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27837 + * GNU General Public License for more details.
27838 + *
27839 + * You should have received a copy of the GNU General Public License
27840 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27841 + */
27842 +
27843 +/*
27844 + * super_block operations
27845 + */
27846 +
27847 +#ifndef __AUFS_SUPER_H__
27848 +#define __AUFS_SUPER_H__
27849 +
27850 +#ifdef __KERNEL__
27851 +
27852 +#include <linux/fs.h>
27853 +#include <linux/kobject.h>
27854 +#include "rwsem.h"
27855 +#include "spl.h"
27856 +#include "wkq.h"
27857 +
27858 +/* policies to select one among multiple writable branches */
27859 +struct au_wbr_copyup_operations {
27860 +       int (*copyup)(struct dentry *dentry);
27861 +};
27862 +
27863 +#define AuWbr_DIR      1               /* target is a dir */
27864 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
27865 +
27866 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
27867 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
27868 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
27869 +
27870 +struct au_wbr_create_operations {
27871 +       int (*create)(struct dentry *dentry, unsigned int flags);
27872 +       int (*init)(struct super_block *sb);
27873 +       int (*fin)(struct super_block *sb);
27874 +};
27875 +
27876 +struct au_wbr_mfs {
27877 +       struct mutex    mfs_lock; /* protect this structure */
27878 +       unsigned long   mfs_jiffy;
27879 +       unsigned long   mfs_expire;
27880 +       aufs_bindex_t   mfs_bindex;
27881 +
27882 +       unsigned long long      mfsrr_bytes;
27883 +       unsigned long long      mfsrr_watermark;
27884 +};
27885 +
27886 +struct pseudo_link {
27887 +       union {
27888 +               struct hlist_node hlist;
27889 +               struct rcu_head rcu;
27890 +       };
27891 +       struct inode *inode;
27892 +};
27893 +
27894 +#define AuPlink_NHASH 100
27895 +static inline int au_plink_hash(ino_t ino)
27896 +{
27897 +       return ino % AuPlink_NHASH;
27898 +}
27899 +
27900 +/* File-based Hierarchical Storage Management */
27901 +struct au_fhsm {
27902 +#ifdef CONFIG_AUFS_FHSM
27903 +       /* allow only one process who can receive the notification */
27904 +       spinlock_t              fhsm_spin;
27905 +       pid_t                   fhsm_pid;
27906 +       wait_queue_head_t       fhsm_wqh;
27907 +       atomic_t                fhsm_readable;
27908 +
27909 +       /* these are protected by si_rwsem */
27910 +       unsigned long           fhsm_expire;
27911 +       aufs_bindex_t           fhsm_bottom;
27912 +#endif
27913 +};
27914 +
27915 +#define AU_PIDSTEP     (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
27916 +#define AU_NPIDMAP     (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
27917 +struct au_si_pid {
27918 +       unsigned long   *pid_bitmap[AU_NPIDMAP];
27919 +       struct mutex    pid_mtx;
27920 +};
27921 +
27922 +struct au_branch;
27923 +struct au_sbinfo {
27924 +       /* nowait tasks in the system-wide workqueue */
27925 +       struct au_nowait_tasks  si_nowait;
27926 +
27927 +       /*
27928 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27929 +        * rwsem for au_sbinfo is necessary.
27930 +        */
27931 +       struct au_rwsem         si_rwsem;
27932 +
27933 +       /* prevent recursive locking in deleting inode */
27934 +       struct au_si_pid        au_si_pid;
27935 +
27936 +       /*
27937 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27938 +        * remount.
27939 +        */
27940 +       atomic_long_t           si_ninodes, si_nfiles;
27941 +
27942 +       /* branch management */
27943 +       unsigned int            si_generation;
27944 +
27945 +       /* see AuSi_ flags */
27946 +       unsigned char           au_si_status;
27947 +
27948 +       aufs_bindex_t           si_bend;
27949 +
27950 +       /* dirty trick to keep br_id plus */
27951 +       unsigned int            si_last_br_id :
27952 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
27953 +       struct au_branch        **si_branch;
27954 +
27955 +       /* policy to select a writable branch */
27956 +       unsigned char           si_wbr_copyup;
27957 +       unsigned char           si_wbr_create;
27958 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
27959 +       struct au_wbr_create_operations *si_wbr_create_ops;
27960 +
27961 +       /* round robin */
27962 +       atomic_t                si_wbr_rr_next;
27963 +
27964 +       /* most free space */
27965 +       struct au_wbr_mfs       si_wbr_mfs;
27966 +
27967 +       /* File-based Hierarchical Storage Management */
27968 +       struct au_fhsm          si_fhsm;
27969 +
27970 +       /* mount flags */
27971 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
27972 +       unsigned int            si_mntflags;
27973 +
27974 +       /* external inode number (bitmap and translation table) */
27975 +       vfs_readf_t             si_xread;
27976 +       vfs_writef_t            si_xwrite;
27977 +       struct file             *si_xib;
27978 +       struct mutex            si_xib_mtx; /* protect xib members */
27979 +       unsigned long           *si_xib_buf;
27980 +       unsigned long           si_xib_last_pindex;
27981 +       int                     si_xib_next_bit;
27982 +       aufs_bindex_t           si_xino_brid;
27983 +       unsigned long           si_xino_jiffy;
27984 +       unsigned long           si_xino_expire;
27985 +       /* reserved for future use */
27986 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
27987 +
27988 +#ifdef CONFIG_AUFS_EXPORT
27989 +       /* i_generation */
27990 +       struct file             *si_xigen;
27991 +       atomic_t                si_xigen_next;
27992 +#endif
27993 +
27994 +       /* dirty trick to suppoer atomic_open */
27995 +       struct au_sphlhead      si_aopen;
27996 +
27997 +       /* vdir parameters */
27998 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
27999 +       unsigned int            si_rdblk;       /* deblk size */
28000 +       unsigned int            si_rdhash;      /* hash size */
28001 +
28002 +       /*
28003 +        * If the number of whiteouts are larger than si_dirwh, leave all of
28004 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
28005 +        * future fsck.aufs or kernel thread will remove them later.
28006 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
28007 +        */
28008 +       unsigned int            si_dirwh;
28009 +
28010 +       /* pseudo_link list */
28011 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
28012 +       wait_queue_head_t       si_plink_wq;
28013 +       spinlock_t              si_plink_maint_lock;
28014 +       pid_t                   si_plink_maint_pid;
28015 +
28016 +       /* file list */
28017 +       struct au_sphlhead      si_files;
28018 +
28019 +       /* with/without getattr, brother of sb->s_d_op */
28020 +       struct inode_operations *si_iop_array;
28021 +
28022 +       /*
28023 +        * sysfs and lifetime management.
28024 +        * this is not a small structure and it may be a waste of memory in case
28025 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
28026 +        * but using sysfs is majority.
28027 +        */
28028 +       struct kobject          si_kobj;
28029 +#ifdef CONFIG_DEBUG_FS
28030 +       struct dentry            *si_dbgaufs;
28031 +       struct dentry            *si_dbgaufs_plink;
28032 +       struct dentry            *si_dbgaufs_xib;
28033 +#ifdef CONFIG_AUFS_EXPORT
28034 +       struct dentry            *si_dbgaufs_xigen;
28035 +#endif
28036 +#endif
28037 +
28038 +#ifdef CONFIG_AUFS_SBILIST
28039 +       struct list_head        si_list;
28040 +#endif
28041 +
28042 +       /* dirty, necessary for unmounting, sysfs and sysrq */
28043 +       struct super_block      *si_sb;
28044 +};
28045 +
28046 +/* sbinfo status flags */
28047 +/*
28048 + * set true when refresh_dirs() failed at remount time.
28049 + * then try refreshing dirs at access time again.
28050 + * if it is false, refreshing dirs at access time is unnecesary
28051 + */
28052 +#define AuSi_FAILED_REFRESH_DIR        1
28053 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
28054 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
28055 +
28056 +#ifndef CONFIG_AUFS_FHSM
28057 +#undef AuSi_FHSM
28058 +#define AuSi_FHSM              0
28059 +#endif
28060 +
28061 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
28062 +                                          unsigned int flag)
28063 +{
28064 +       AuRwMustAnyLock(&sbi->si_rwsem);
28065 +       return sbi->au_si_status & flag;
28066 +}
28067 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
28068 +#define au_fset_si(sbinfo, name) do { \
28069 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28070 +       (sbinfo)->au_si_status |= AuSi_##name; \
28071 +} while (0)
28072 +#define au_fclr_si(sbinfo, name) do { \
28073 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28074 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
28075 +} while (0)
28076 +
28077 +/* ---------------------------------------------------------------------- */
28078 +
28079 +/* policy to select one among writable branches */
28080 +#define AuWbrCopyup(sbinfo, ...) \
28081 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
28082 +#define AuWbrCreate(sbinfo, ...) \
28083 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
28084 +
28085 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
28086 +#define AuLock_DW              1               /* write-lock dentry */
28087 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
28088 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
28089 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
28090 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
28091 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
28092 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
28093 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
28094 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
28095 +#define au_fset_lock(flags, name) \
28096 +       do { (flags) |= AuLock_##name; } while (0)
28097 +#define au_fclr_lock(flags, name) \
28098 +       do { (flags) &= ~AuLock_##name; } while (0)
28099 +
28100 +/* ---------------------------------------------------------------------- */
28101 +
28102 +/* super.c */
28103 +extern struct file_system_type aufs_fs_type;
28104 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
28105 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28106 +                                          unsigned long long max, void *arg);
28107 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28108 +                    struct super_block *sb, void *arg);
28109 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28110 +void au_iarray_free(struct inode **a, unsigned long long max);
28111 +
28112 +/* sbinfo.c */
28113 +void au_si_free(struct kobject *kobj);
28114 +int au_si_alloc(struct super_block *sb);
28115 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
28116 +
28117 +unsigned int au_sigen_inc(struct super_block *sb);
28118 +aufs_bindex_t au_new_br_id(struct super_block *sb);
28119 +
28120 +int si_read_lock(struct super_block *sb, int flags);
28121 +int si_write_lock(struct super_block *sb, int flags);
28122 +int aufs_read_lock(struct dentry *dentry, int flags);
28123 +void aufs_read_unlock(struct dentry *dentry, int flags);
28124 +void aufs_write_lock(struct dentry *dentry);
28125 +void aufs_write_unlock(struct dentry *dentry);
28126 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
28127 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28128 +
28129 +/* wbr_policy.c */
28130 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28131 +extern struct au_wbr_create_operations au_wbr_create_ops[];
28132 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
28133 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
28134 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
28135 +
28136 +/* mvdown.c */
28137 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
28138 +
28139 +#ifdef CONFIG_AUFS_FHSM
28140 +/* fhsm.c */
28141 +
28142 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28143 +{
28144 +       pid_t pid;
28145 +
28146 +       spin_lock(&fhsm->fhsm_spin);
28147 +       pid = fhsm->fhsm_pid;
28148 +       spin_unlock(&fhsm->fhsm_spin);
28149 +
28150 +       return pid;
28151 +}
28152 +
28153 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28154 +void au_fhsm_wrote_all(struct super_block *sb, int force);
28155 +int au_fhsm_fd(struct super_block *sb, int oflags);
28156 +int au_fhsm_br_alloc(struct au_branch *br);
28157 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
28158 +void au_fhsm_fin(struct super_block *sb);
28159 +void au_fhsm_init(struct au_sbinfo *sbinfo);
28160 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28161 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28162 +#else
28163 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28164 +          int force)
28165 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28166 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
28167 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28168 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28169 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
28170 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
28171 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28172 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28173 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28174 +#endif
28175 +
28176 +/* ---------------------------------------------------------------------- */
28177 +
28178 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28179 +{
28180 +       return sb->s_fs_info;
28181 +}
28182 +
28183 +/* ---------------------------------------------------------------------- */
28184 +
28185 +#ifdef CONFIG_AUFS_EXPORT
28186 +int au_test_nfsd(void);
28187 +void au_export_init(struct super_block *sb);
28188 +void au_xigen_inc(struct inode *inode);
28189 +int au_xigen_new(struct inode *inode);
28190 +int au_xigen_set(struct super_block *sb, struct file *base);
28191 +void au_xigen_clr(struct super_block *sb);
28192 +
28193 +static inline int au_busy_or_stale(void)
28194 +{
28195 +       if (!au_test_nfsd())
28196 +               return -EBUSY;
28197 +       return -ESTALE;
28198 +}
28199 +#else
28200 +AuStubInt0(au_test_nfsd, void)
28201 +AuStubVoid(au_export_init, struct super_block *sb)
28202 +AuStubVoid(au_xigen_inc, struct inode *inode)
28203 +AuStubInt0(au_xigen_new, struct inode *inode)
28204 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28205 +AuStubVoid(au_xigen_clr, struct super_block *sb)
28206 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
28207 +#endif /* CONFIG_AUFS_EXPORT */
28208 +
28209 +/* ---------------------------------------------------------------------- */
28210 +
28211 +#ifdef CONFIG_AUFS_SBILIST
28212 +/* module.c */
28213 +extern struct au_splhead au_sbilist;
28214 +
28215 +static inline void au_sbilist_init(void)
28216 +{
28217 +       au_spl_init(&au_sbilist);
28218 +}
28219 +
28220 +static inline void au_sbilist_add(struct super_block *sb)
28221 +{
28222 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
28223 +}
28224 +
28225 +static inline void au_sbilist_del(struct super_block *sb)
28226 +{
28227 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
28228 +}
28229 +
28230 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28231 +static inline void au_sbilist_lock(void)
28232 +{
28233 +       spin_lock(&au_sbilist.spin);
28234 +}
28235 +
28236 +static inline void au_sbilist_unlock(void)
28237 +{
28238 +       spin_unlock(&au_sbilist.spin);
28239 +}
28240 +#define AuGFP_SBILIST  GFP_ATOMIC
28241 +#else
28242 +AuStubVoid(au_sbilist_lock, void)
28243 +AuStubVoid(au_sbilist_unlock, void)
28244 +#define AuGFP_SBILIST  GFP_NOFS
28245 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
28246 +#else
28247 +AuStubVoid(au_sbilist_init, void)
28248 +AuStubVoid(au_sbilist_add, struct super_block *sb)
28249 +AuStubVoid(au_sbilist_del, struct super_block *sb)
28250 +AuStubVoid(au_sbilist_lock, void)
28251 +AuStubVoid(au_sbilist_unlock, void)
28252 +#define AuGFP_SBILIST  GFP_NOFS
28253 +#endif
28254 +
28255 +/* ---------------------------------------------------------------------- */
28256 +
28257 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28258 +{
28259 +       /*
28260 +        * This function is a dynamic '__init' function actually,
28261 +        * so the tiny check for si_rwsem is unnecessary.
28262 +        */
28263 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28264 +#ifdef CONFIG_DEBUG_FS
28265 +       sbinfo->si_dbgaufs = NULL;
28266 +       sbinfo->si_dbgaufs_plink = NULL;
28267 +       sbinfo->si_dbgaufs_xib = NULL;
28268 +#ifdef CONFIG_AUFS_EXPORT
28269 +       sbinfo->si_dbgaufs_xigen = NULL;
28270 +#endif
28271 +#endif
28272 +}
28273 +
28274 +/* ---------------------------------------------------------------------- */
28275 +
28276 +static inline void si_pid_idx_bit(int *idx, pid_t *bit)
28277 +{
28278 +       /* the origin of pid is 1, but the bitmap's is 0 */
28279 +       *bit = current->pid - 1;
28280 +       *idx = *bit / AU_PIDSTEP;
28281 +       *bit %= AU_PIDSTEP;
28282 +}
28283 +
28284 +static inline int si_pid_test(struct super_block *sb)
28285 +{
28286 +       pid_t bit;
28287 +       int idx;
28288 +       unsigned long *bitmap;
28289 +
28290 +       si_pid_idx_bit(&idx, &bit);
28291 +       bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28292 +       if (bitmap)
28293 +               return test_bit(bit, bitmap);
28294 +       return 0;
28295 +}
28296 +
28297 +static inline void si_pid_clr(struct super_block *sb)
28298 +{
28299 +       pid_t bit;
28300 +       int idx;
28301 +       unsigned long *bitmap;
28302 +
28303 +       si_pid_idx_bit(&idx, &bit);
28304 +       bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28305 +       BUG_ON(!bitmap);
28306 +       AuDebugOn(!test_bit(bit, bitmap));
28307 +       clear_bit(bit, bitmap);
28308 +       /* smp_mb(); */
28309 +}
28310 +
28311 +void si_pid_set(struct super_block *sb);
28312 +
28313 +/* ---------------------------------------------------------------------- */
28314 +
28315 +/* lock superblock. mainly for entry point functions */
28316 +/*
28317 + * __si_read_lock, __si_write_lock,
28318 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28319 + */
28320 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28321 +
28322 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28323 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28324 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28325 +
28326 +static inline void si_noflush_read_lock(struct super_block *sb)
28327 +{
28328 +       __si_read_lock(sb);
28329 +       si_pid_set(sb);
28330 +}
28331 +
28332 +static inline int si_noflush_read_trylock(struct super_block *sb)
28333 +{
28334 +       int locked;
28335 +
28336 +       locked = __si_read_trylock(sb);
28337 +       if (locked)
28338 +               si_pid_set(sb);
28339 +       return locked;
28340 +}
28341 +
28342 +static inline void si_noflush_write_lock(struct super_block *sb)
28343 +{
28344 +       __si_write_lock(sb);
28345 +       si_pid_set(sb);
28346 +}
28347 +
28348 +static inline int si_noflush_write_trylock(struct super_block *sb)
28349 +{
28350 +       int locked;
28351 +
28352 +       locked = __si_write_trylock(sb);
28353 +       if (locked)
28354 +               si_pid_set(sb);
28355 +       return locked;
28356 +}
28357 +
28358 +#if 0 /* reserved */
28359 +static inline int si_read_trylock(struct super_block *sb, int flags)
28360 +{
28361 +       if (au_ftest_lock(flags, FLUSH))
28362 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28363 +       return si_noflush_read_trylock(sb);
28364 +}
28365 +#endif
28366 +
28367 +static inline void si_read_unlock(struct super_block *sb)
28368 +{
28369 +       si_pid_clr(sb);
28370 +       __si_read_unlock(sb);
28371 +}
28372 +
28373 +#if 0 /* reserved */
28374 +static inline int si_write_trylock(struct super_block *sb, int flags)
28375 +{
28376 +       if (au_ftest_lock(flags, FLUSH))
28377 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28378 +       return si_noflush_write_trylock(sb);
28379 +}
28380 +#endif
28381 +
28382 +static inline void si_write_unlock(struct super_block *sb)
28383 +{
28384 +       si_pid_clr(sb);
28385 +       __si_write_unlock(sb);
28386 +}
28387 +
28388 +#if 0 /* reserved */
28389 +static inline void si_downgrade_lock(struct super_block *sb)
28390 +{
28391 +       __si_downgrade_lock(sb);
28392 +}
28393 +#endif
28394 +
28395 +/* ---------------------------------------------------------------------- */
28396 +
28397 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
28398 +{
28399 +       SiMustAnyLock(sb);
28400 +       return au_sbi(sb)->si_bend;
28401 +}
28402 +
28403 +static inline unsigned int au_mntflags(struct super_block *sb)
28404 +{
28405 +       SiMustAnyLock(sb);
28406 +       return au_sbi(sb)->si_mntflags;
28407 +}
28408 +
28409 +static inline unsigned int au_sigen(struct super_block *sb)
28410 +{
28411 +       SiMustAnyLock(sb);
28412 +       return au_sbi(sb)->si_generation;
28413 +}
28414 +
28415 +static inline void au_ninodes_inc(struct super_block *sb)
28416 +{
28417 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
28418 +}
28419 +
28420 +static inline void au_ninodes_dec(struct super_block *sb)
28421 +{
28422 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
28423 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
28424 +}
28425 +
28426 +static inline void au_nfiles_inc(struct super_block *sb)
28427 +{
28428 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
28429 +}
28430 +
28431 +static inline void au_nfiles_dec(struct super_block *sb)
28432 +{
28433 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
28434 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
28435 +}
28436 +
28437 +static inline struct au_branch *au_sbr(struct super_block *sb,
28438 +                                      aufs_bindex_t bindex)
28439 +{
28440 +       SiMustAnyLock(sb);
28441 +       return au_sbi(sb)->si_branch[0 + bindex];
28442 +}
28443 +
28444 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28445 +{
28446 +       SiMustWriteLock(sb);
28447 +       au_sbi(sb)->si_xino_brid = brid;
28448 +}
28449 +
28450 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28451 +{
28452 +       SiMustAnyLock(sb);
28453 +       return au_sbi(sb)->si_xino_brid;
28454 +}
28455 +
28456 +#endif /* __KERNEL__ */
28457 +#endif /* __AUFS_SUPER_H__ */
28458 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28459 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28460 +++ linux/fs/aufs/sysaufs.c     2016-04-24 18:32:51.393686895 +0200
28461 @@ -0,0 +1,104 @@
28462 +/*
28463 + * Copyright (C) 2005-2016 Junjiro R. Okajima
28464 + *
28465 + * This program, aufs is free software; you can redistribute it and/or modify
28466 + * it under the terms of the GNU General Public License as published by
28467 + * the Free Software Foundation; either version 2 of the License, or
28468 + * (at your option) any later version.
28469 + *
28470 + * This program is distributed in the hope that it will be useful,
28471 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28472 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28473 + * GNU General Public License for more details.
28474 + *
28475 + * You should have received a copy of the GNU General Public License
28476 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28477 + */
28478 +
28479 +/*
28480 + * sysfs interface and lifetime management
28481 + * they are necessary regardless sysfs is disabled.
28482 + */
28483 +
28484 +#include <linux/random.h>
28485 +#include "aufs.h"
28486 +
28487 +unsigned long sysaufs_si_mask;
28488 +struct kset *sysaufs_kset;
28489 +
28490 +#define AuSiAttr(_name) { \
28491 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28492 +       .show   = sysaufs_si_##_name,                           \
28493 +}
28494 +
28495 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28496 +struct attribute *sysaufs_si_attrs[] = {
28497 +       &sysaufs_si_attr_xi_path.attr,
28498 +       NULL,
28499 +};
28500 +
28501 +static const struct sysfs_ops au_sbi_ops = {
28502 +       .show   = sysaufs_si_show
28503 +};
28504 +
28505 +static struct kobj_type au_sbi_ktype = {
28506 +       .release        = au_si_free,
28507 +       .sysfs_ops      = &au_sbi_ops,
28508 +       .default_attrs  = sysaufs_si_attrs
28509 +};
28510 +
28511 +/* ---------------------------------------------------------------------- */
28512 +
28513 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28514 +{
28515 +       int err;
28516 +
28517 +       sbinfo->si_kobj.kset = sysaufs_kset;
28518 +       /* cf. sysaufs_name() */
28519 +       err = kobject_init_and_add
28520 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28521 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28522 +
28523 +       dbgaufs_si_null(sbinfo);
28524 +       if (!err) {
28525 +               err = dbgaufs_si_init(sbinfo);
28526 +               if (unlikely(err))
28527 +                       kobject_put(&sbinfo->si_kobj);
28528 +       }
28529 +       return err;
28530 +}
28531 +
28532 +void sysaufs_fin(void)
28533 +{
28534 +       dbgaufs_fin();
28535 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28536 +       kset_unregister(sysaufs_kset);
28537 +}
28538 +
28539 +int __init sysaufs_init(void)
28540 +{
28541 +       int err;
28542 +
28543 +       do {
28544 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28545 +       } while (!sysaufs_si_mask);
28546 +
28547 +       err = -EINVAL;
28548 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28549 +       if (unlikely(!sysaufs_kset))
28550 +               goto out;
28551 +       err = PTR_ERR(sysaufs_kset);
28552 +       if (IS_ERR(sysaufs_kset))
28553 +               goto out;
28554 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28555 +       if (unlikely(err)) {
28556 +               kset_unregister(sysaufs_kset);
28557 +               goto out;
28558 +       }
28559 +
28560 +       err = dbgaufs_init();
28561 +       if (unlikely(err))
28562 +               sysaufs_fin();
28563 +out:
28564 +       return err;
28565 +}
28566 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28567 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
28568 +++ linux/fs/aufs/sysaufs.h     2016-04-24 18:32:51.393686895 +0200
28569 @@ -0,0 +1,101 @@
28570 +/*
28571 + * Copyright (C) 2005-2016 Junjiro R. Okajima
28572 + *
28573 + * This program, aufs is free software; you can redistribute it and/or modify
28574 + * it under the terms of the GNU General Public License as published by
28575 + * the Free Software Foundation; either version 2 of the License, or
28576 + * (at your option) any later version.
28577 + *
28578 + * This program is distributed in the hope that it will be useful,
28579 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28580 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28581 + * GNU General Public License for more details.
28582 + *
28583 + * You should have received a copy of the GNU General Public License
28584 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28585 + */
28586 +
28587 +/*
28588 + * sysfs interface and mount lifetime management
28589 + */
28590 +
28591 +#ifndef __SYSAUFS_H__
28592 +#define __SYSAUFS_H__
28593 +
28594 +#ifdef __KERNEL__
28595 +
28596 +#include <linux/sysfs.h>
28597 +#include "module.h"
28598 +
28599 +struct super_block;
28600 +struct au_sbinfo;
28601 +
28602 +struct sysaufs_si_attr {
28603 +       struct attribute attr;
28604 +       int (*show)(struct seq_file *seq, struct super_block *sb);
28605 +};
28606 +
28607 +/* ---------------------------------------------------------------------- */
28608 +
28609 +/* sysaufs.c */
28610 +extern unsigned long sysaufs_si_mask;
28611 +extern struct kset *sysaufs_kset;
28612 +extern struct attribute *sysaufs_si_attrs[];
28613 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
28614 +int __init sysaufs_init(void);
28615 +void sysaufs_fin(void);
28616 +
28617 +/* ---------------------------------------------------------------------- */
28618 +
28619 +/* some people doesn't like to show a pointer in kernel */
28620 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28621 +{
28622 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
28623 +}
28624 +
28625 +#define SysaufsSiNamePrefix    "si_"
28626 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
28627 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28628 +{
28629 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28630 +                sysaufs_si_id(sbinfo));
28631 +}
28632 +
28633 +struct au_branch;
28634 +#ifdef CONFIG_SYSFS
28635 +/* sysfs.c */
28636 +extern struct attribute_group *sysaufs_attr_group;
28637 +
28638 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28639 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28640 +                        char *buf);
28641 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
28642 +#ifdef CONFIG_COMPAT
28643 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28644 +#endif
28645 +
28646 +void sysaufs_br_init(struct au_branch *br);
28647 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28648 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28649 +
28650 +#define sysaufs_brs_init()     do {} while (0)
28651 +
28652 +#else
28653 +#define sysaufs_attr_group     NULL
28654 +
28655 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
28656 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28657 +       struct attribute *attr, char *buf)
28658 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
28659 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28660 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
28661 +
28662 +static inline void sysaufs_brs_init(void)
28663 +{
28664 +       sysaufs_brs = 0;
28665 +}
28666 +
28667 +#endif /* CONFIG_SYSFS */
28668 +
28669 +#endif /* __KERNEL__ */
28670 +#endif /* __SYSAUFS_H__ */
28671 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28672 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
28673 +++ linux/fs/aufs/sysfs.c       2016-04-24 18:32:51.393686895 +0200
28674 @@ -0,0 +1,376 @@
28675 +/*
28676 + * Copyright (C) 2005-2016 Junjiro R. Okajima
28677 + *
28678 + * This program, aufs is free software; you can redistribute it and/or modify
28679 + * it under the terms of the GNU General Public License as published by
28680 + * the Free Software Foundation; either version 2 of the License, or
28681 + * (at your option) any later version.
28682 + *
28683 + * This program is distributed in the hope that it will be useful,
28684 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28685 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28686 + * GNU General Public License for more details.
28687 + *
28688 + * You should have received a copy of the GNU General Public License
28689 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28690 + */
28691 +
28692 +/*
28693 + * sysfs interface
28694 + */
28695 +
28696 +#include <linux/compat.h>
28697 +#include <linux/seq_file.h>
28698 +#include "aufs.h"
28699 +
28700 +#ifdef CONFIG_AUFS_FS_MODULE
28701 +/* this entry violates the "one line per file" policy of sysfs */
28702 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28703 +                          char *buf)
28704 +{
28705 +       ssize_t err;
28706 +       static char *conf =
28707 +/* this file is generated at compiling */
28708 +#include "conf.str"
28709 +               ;
28710 +
28711 +       err = snprintf(buf, PAGE_SIZE, conf);
28712 +       if (unlikely(err >= PAGE_SIZE))
28713 +               err = -EFBIG;
28714 +       return err;
28715 +}
28716 +
28717 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28718 +#endif
28719 +
28720 +static struct attribute *au_attr[] = {
28721 +#ifdef CONFIG_AUFS_FS_MODULE
28722 +       &au_config_attr.attr,
28723 +#endif
28724 +       NULL,   /* need to NULL terminate the list of attributes */
28725 +};
28726 +
28727 +static struct attribute_group sysaufs_attr_group_body = {
28728 +       .attrs = au_attr
28729 +};
28730 +
28731 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28732 +
28733 +/* ---------------------------------------------------------------------- */
28734 +
28735 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28736 +{
28737 +       int err;
28738 +
28739 +       SiMustAnyLock(sb);
28740 +
28741 +       err = 0;
28742 +       if (au_opt_test(au_mntflags(sb), XINO)) {
28743 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
28744 +               seq_putc(seq, '\n');
28745 +       }
28746 +       return err;
28747 +}
28748 +
28749 +/*
28750 + * the lifetime of branch is independent from the entry under sysfs.
28751 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
28752 + * unlinked.
28753 + */
28754 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
28755 +                        aufs_bindex_t bindex, int idx)
28756 +{
28757 +       int err;
28758 +       struct path path;
28759 +       struct dentry *root;
28760 +       struct au_branch *br;
28761 +       au_br_perm_str_t perm;
28762 +
28763 +       AuDbg("b%d\n", bindex);
28764 +
28765 +       err = 0;
28766 +       root = sb->s_root;
28767 +       di_read_lock_parent(root, !AuLock_IR);
28768 +       br = au_sbr(sb, bindex);
28769 +
28770 +       switch (idx) {
28771 +       case AuBrSysfs_BR:
28772 +               path.mnt = au_br_mnt(br);
28773 +               path.dentry = au_h_dptr(root, bindex);
28774 +               err = au_seq_path(seq, &path);
28775 +               if (!err) {
28776 +                       au_optstr_br_perm(&perm, br->br_perm);
28777 +                       seq_printf(seq, "=%s\n", perm.a);
28778 +               }
28779 +               break;
28780 +       case AuBrSysfs_BRID:
28781 +               seq_printf(seq, "%d\n", br->br_id);
28782 +               break;
28783 +       }
28784 +       di_read_unlock(root, !AuLock_IR);
28785 +       if (unlikely(err || seq_has_overflowed(seq)))
28786 +               err = -E2BIG;
28787 +
28788 +       return err;
28789 +}
28790 +
28791 +/* ---------------------------------------------------------------------- */
28792 +
28793 +static struct seq_file *au_seq(char *p, ssize_t len)
28794 +{
28795 +       struct seq_file *seq;
28796 +
28797 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
28798 +       if (seq) {
28799 +               /* mutex_init(&seq.lock); */
28800 +               seq->buf = p;
28801 +               seq->size = len;
28802 +               return seq; /* success */
28803 +       }
28804 +
28805 +       seq = ERR_PTR(-ENOMEM);
28806 +       return seq;
28807 +}
28808 +
28809 +#define SysaufsBr_PREFIX       "br"
28810 +#define SysaufsBrid_PREFIX     "brid"
28811 +
28812 +/* todo: file size may exceed PAGE_SIZE */
28813 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28814 +                       char *buf)
28815 +{
28816 +       ssize_t err;
28817 +       int idx;
28818 +       long l;
28819 +       aufs_bindex_t bend;
28820 +       struct au_sbinfo *sbinfo;
28821 +       struct super_block *sb;
28822 +       struct seq_file *seq;
28823 +       char *name;
28824 +       struct attribute **cattr;
28825 +
28826 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28827 +       sb = sbinfo->si_sb;
28828 +
28829 +       /*
28830 +        * prevent a race condition between sysfs and aufs.
28831 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28832 +        * prohibits maintaining the sysfs entries.
28833 +        * hew we acquire read lock after sysfs_get_active_two().
28834 +        * on the other hand, the remount process may maintain the sysfs/aufs
28835 +        * entries after acquiring write lock.
28836 +        * it can cause a deadlock.
28837 +        * simply we gave up processing read here.
28838 +        */
28839 +       err = -EBUSY;
28840 +       if (unlikely(!si_noflush_read_trylock(sb)))
28841 +               goto out;
28842 +
28843 +       seq = au_seq(buf, PAGE_SIZE);
28844 +       err = PTR_ERR(seq);
28845 +       if (IS_ERR(seq))
28846 +               goto out_unlock;
28847 +
28848 +       name = (void *)attr->name;
28849 +       cattr = sysaufs_si_attrs;
28850 +       while (*cattr) {
28851 +               if (!strcmp(name, (*cattr)->name)) {
28852 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
28853 +                               ->show(seq, sb);
28854 +                       goto out_seq;
28855 +               }
28856 +               cattr++;
28857 +       }
28858 +
28859 +       if (!strncmp(name, SysaufsBrid_PREFIX,
28860 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
28861 +               idx = AuBrSysfs_BRID;
28862 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
28863 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
28864 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
28865 +               idx = AuBrSysfs_BR;
28866 +               name += sizeof(SysaufsBr_PREFIX) - 1;
28867 +       } else
28868 +                 BUG();
28869 +
28870 +       err = kstrtol(name, 10, &l);
28871 +       if (!err) {
28872 +               bend = au_sbend(sb);
28873 +               if (l <= bend)
28874 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28875 +               else
28876 +                       err = -ENOENT;
28877 +       }
28878 +
28879 +out_seq:
28880 +       if (!err) {
28881 +               err = seq->count;
28882 +               /* sysfs limit */
28883 +               if (unlikely(err == PAGE_SIZE))
28884 +                       err = -EFBIG;
28885 +       }
28886 +       kfree(seq);
28887 +out_unlock:
28888 +       si_read_unlock(sb);
28889 +out:
28890 +       return err;
28891 +}
28892 +
28893 +/* ---------------------------------------------------------------------- */
28894 +
28895 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28896 +{
28897 +       int err;
28898 +       int16_t brid;
28899 +       aufs_bindex_t bindex, bend;
28900 +       size_t sz;
28901 +       char *buf;
28902 +       struct seq_file *seq;
28903 +       struct au_branch *br;
28904 +
28905 +       si_read_lock(sb, AuLock_FLUSH);
28906 +       bend = au_sbend(sb);
28907 +       err = bend + 1;
28908 +       if (!arg)
28909 +               goto out;
28910 +
28911 +       err = -ENOMEM;
28912 +       buf = (void *)__get_free_page(GFP_NOFS);
28913 +       if (unlikely(!buf))
28914 +               goto out;
28915 +
28916 +       seq = au_seq(buf, PAGE_SIZE);
28917 +       err = PTR_ERR(seq);
28918 +       if (IS_ERR(seq))
28919 +               goto out_buf;
28920 +
28921 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
28922 +       for (bindex = 0; bindex <= bend; bindex++, arg++) {
28923 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28924 +               if (unlikely(err))
28925 +                       break;
28926 +
28927 +               br = au_sbr(sb, bindex);
28928 +               brid = br->br_id;
28929 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28930 +               err = __put_user(brid, &arg->id);
28931 +               if (unlikely(err))
28932 +                       break;
28933 +
28934 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28935 +               err = __put_user(br->br_perm, &arg->perm);
28936 +               if (unlikely(err))
28937 +                       break;
28938 +
28939 +               err = au_seq_path(seq, &br->br_path);
28940 +               if (unlikely(err))
28941 +                       break;
28942 +               seq_putc(seq, '\0');
28943 +               if (!seq_has_overflowed(seq)) {
28944 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
28945 +                       seq->count = 0;
28946 +                       if (unlikely(err))
28947 +                               break;
28948 +               } else {
28949 +                       err = -E2BIG;
28950 +                       goto out_seq;
28951 +               }
28952 +       }
28953 +       if (unlikely(err))
28954 +               err = -EFAULT;
28955 +
28956 +out_seq:
28957 +       kfree(seq);
28958 +out_buf:
28959 +       free_page((unsigned long)buf);
28960 +out:
28961 +       si_read_unlock(sb);
28962 +       return err;
28963 +}
28964 +
28965 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
28966 +{
28967 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
28968 +}
28969 +
28970 +#ifdef CONFIG_COMPAT
28971 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
28972 +{
28973 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
28974 +}
28975 +#endif
28976 +
28977 +/* ---------------------------------------------------------------------- */
28978 +
28979 +void sysaufs_br_init(struct au_branch *br)
28980 +{
28981 +       int i;
28982 +       struct au_brsysfs *br_sysfs;
28983 +       struct attribute *attr;
28984 +
28985 +       br_sysfs = br->br_sysfs;
28986 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28987 +               attr = &br_sysfs->attr;
28988 +               sysfs_attr_init(attr);
28989 +               attr->name = br_sysfs->name;
28990 +               attr->mode = S_IRUGO;
28991 +               br_sysfs++;
28992 +       }
28993 +}
28994 +
28995 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
28996 +{
28997 +       struct au_branch *br;
28998 +       struct kobject *kobj;
28999 +       struct au_brsysfs *br_sysfs;
29000 +       int i;
29001 +       aufs_bindex_t bend;
29002 +
29003 +       dbgaufs_brs_del(sb, bindex);
29004 +
29005 +       if (!sysaufs_brs)
29006 +               return;
29007 +
29008 +       kobj = &au_sbi(sb)->si_kobj;
29009 +       bend = au_sbend(sb);
29010 +       for (; bindex <= bend; bindex++) {
29011 +               br = au_sbr(sb, bindex);
29012 +               br_sysfs = br->br_sysfs;
29013 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29014 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
29015 +                       br_sysfs++;
29016 +               }
29017 +       }
29018 +}
29019 +
29020 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
29021 +{
29022 +       int err, i;
29023 +       aufs_bindex_t bend;
29024 +       struct kobject *kobj;
29025 +       struct au_branch *br;
29026 +       struct au_brsysfs *br_sysfs;
29027 +
29028 +       dbgaufs_brs_add(sb, bindex);
29029 +
29030 +       if (!sysaufs_brs)
29031 +               return;
29032 +
29033 +       kobj = &au_sbi(sb)->si_kobj;
29034 +       bend = au_sbend(sb);
29035 +       for (; bindex <= bend; bindex++) {
29036 +               br = au_sbr(sb, bindex);
29037 +               br_sysfs = br->br_sysfs;
29038 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
29039 +                        SysaufsBr_PREFIX "%d", bindex);
29040 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
29041 +                        SysaufsBrid_PREFIX "%d", bindex);
29042 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29043 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
29044 +                       if (unlikely(err))
29045 +                               pr_warn("failed %s under sysfs(%d)\n",
29046 +                                       br_sysfs->name, err);
29047 +                       br_sysfs++;
29048 +               }
29049 +       }
29050 +}
29051 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
29052 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
29053 +++ linux/fs/aufs/sysrq.c       2016-04-24 18:32:51.397020264 +0200
29054 @@ -0,0 +1,157 @@
29055 +/*
29056 + * Copyright (C) 2005-2016 Junjiro R. Okajima
29057 + *
29058 + * This program, aufs is free software; you can redistribute it and/or modify
29059 + * it under the terms of the GNU General Public License as published by
29060 + * the Free Software Foundation; either version 2 of the License, or
29061 + * (at your option) any later version.
29062 + *
29063 + * This program is distributed in the hope that it will be useful,
29064 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29065 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29066 + * GNU General Public License for more details.
29067 + *
29068 + * You should have received a copy of the GNU General Public License
29069 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29070 + */
29071 +
29072 +/*
29073 + * magic sysrq hanlder
29074 + */
29075 +
29076 +/* #include <linux/sysrq.h> */
29077 +#include <linux/writeback.h>
29078 +#include "aufs.h"
29079 +
29080 +/* ---------------------------------------------------------------------- */
29081 +
29082 +static void sysrq_sb(struct super_block *sb)
29083 +{
29084 +       char *plevel;
29085 +       struct au_sbinfo *sbinfo;
29086 +       struct file *file;
29087 +       struct au_sphlhead *files;
29088 +       struct au_finfo *finfo;
29089 +
29090 +       plevel = au_plevel;
29091 +       au_plevel = KERN_WARNING;
29092 +
29093 +       /* since we define pr_fmt, call printk directly */
29094 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29095 +
29096 +       sbinfo = au_sbi(sb);
29097 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
29098 +       pr("superblock\n");
29099 +       au_dpri_sb(sb);
29100 +
29101 +#if 0
29102 +       pr("root dentry\n");
29103 +       au_dpri_dentry(sb->s_root);
29104 +       pr("root inode\n");
29105 +       au_dpri_inode(d_inode(sb->s_root));
29106 +#endif
29107 +
29108 +#if 0
29109 +       do {
29110 +               int err, i, j, ndentry;
29111 +               struct au_dcsub_pages dpages;
29112 +               struct au_dpage *dpage;
29113 +
29114 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
29115 +               if (unlikely(err))
29116 +                       break;
29117 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29118 +               if (!err)
29119 +                       for (i = 0; i < dpages.ndpage; i++) {
29120 +                               dpage = dpages.dpages + i;
29121 +                               ndentry = dpage->ndentry;
29122 +                               for (j = 0; j < ndentry; j++)
29123 +                                       au_dpri_dentry(dpage->dentries[j]);
29124 +                       }
29125 +               au_dpages_free(&dpages);
29126 +       } while (0);
29127 +#endif
29128 +
29129 +#if 1
29130 +       {
29131 +               struct inode *i;
29132 +
29133 +               pr("isolated inode\n");
29134 +               spin_lock(&sb->s_inode_list_lock);
29135 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29136 +                       spin_lock(&i->i_lock);
29137 +                       if (1 || hlist_empty(&i->i_dentry))
29138 +                               au_dpri_inode(i);
29139 +                       spin_unlock(&i->i_lock);
29140 +               }
29141 +               spin_unlock(&sb->s_inode_list_lock);
29142 +       }
29143 +#endif
29144 +       pr("files\n");
29145 +       files = &au_sbi(sb)->si_files;
29146 +       spin_lock(&files->spin);
29147 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
29148 +               umode_t mode;
29149 +
29150 +               file = finfo->fi_file;
29151 +               mode = file_inode(file)->i_mode;
29152 +               if (!special_file(mode))
29153 +                       au_dpri_file(file);
29154 +       }
29155 +       spin_unlock(&files->spin);
29156 +       pr("done\n");
29157 +
29158 +#undef pr
29159 +       au_plevel = plevel;
29160 +}
29161 +
29162 +/* ---------------------------------------------------------------------- */
29163 +
29164 +/* module parameter */
29165 +static char *aufs_sysrq_key = "a";
29166 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29167 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29168 +
29169 +static void au_sysrq(int key __maybe_unused)
29170 +{
29171 +       struct au_sbinfo *sbinfo;
29172 +
29173 +       lockdep_off();
29174 +       au_sbilist_lock();
29175 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
29176 +               sysrq_sb(sbinfo->si_sb);
29177 +       au_sbilist_unlock();
29178 +       lockdep_on();
29179 +}
29180 +
29181 +static struct sysrq_key_op au_sysrq_op = {
29182 +       .handler        = au_sysrq,
29183 +       .help_msg       = "Aufs",
29184 +       .action_msg     = "Aufs",
29185 +       .enable_mask    = SYSRQ_ENABLE_DUMP
29186 +};
29187 +
29188 +/* ---------------------------------------------------------------------- */
29189 +
29190 +int __init au_sysrq_init(void)
29191 +{
29192 +       int err;
29193 +       char key;
29194 +
29195 +       err = -1;
29196 +       key = *aufs_sysrq_key;
29197 +       if ('a' <= key && key <= 'z')
29198 +               err = register_sysrq_key(key, &au_sysrq_op);
29199 +       if (unlikely(err))
29200 +               pr_err("err %d, sysrq=%c\n", err, key);
29201 +       return err;
29202 +}
29203 +
29204 +void au_sysrq_fin(void)
29205 +{
29206 +       int err;
29207 +
29208 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29209 +       if (unlikely(err))
29210 +               pr_err("err %d (ignored)\n", err);
29211 +}
29212 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29213 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
29214 +++ linux/fs/aufs/vdir.c        2016-04-24 18:32:51.397020264 +0200
29215 @@ -0,0 +1,888 @@
29216 +/*
29217 + * Copyright (C) 2005-2016 Junjiro R. Okajima
29218 + *
29219 + * This program, aufs is free software; you can redistribute it and/or modify
29220 + * it under the terms of the GNU General Public License as published by
29221 + * the Free Software Foundation; either version 2 of the License, or
29222 + * (at your option) any later version.
29223 + *
29224 + * This program is distributed in the hope that it will be useful,
29225 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29226 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29227 + * GNU General Public License for more details.
29228 + *
29229 + * You should have received a copy of the GNU General Public License
29230 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29231 + */
29232 +
29233 +/*
29234 + * virtual or vertical directory
29235 + */
29236 +
29237 +#include "aufs.h"
29238 +
29239 +static unsigned int calc_size(int nlen)
29240 +{
29241 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
29242 +}
29243 +
29244 +static int set_deblk_end(union au_vdir_deblk_p *p,
29245 +                        union au_vdir_deblk_p *deblk_end)
29246 +{
29247 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29248 +               p->de->de_str.len = 0;
29249 +               /* smp_mb(); */
29250 +               return 0;
29251 +       }
29252 +       return -1; /* error */
29253 +}
29254 +
29255 +/* returns true or false */
29256 +static int is_deblk_end(union au_vdir_deblk_p *p,
29257 +                       union au_vdir_deblk_p *deblk_end)
29258 +{
29259 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29260 +               return !p->de->de_str.len;
29261 +       return 1;
29262 +}
29263 +
29264 +static unsigned char *last_deblk(struct au_vdir *vdir)
29265 +{
29266 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29267 +}
29268 +
29269 +/* ---------------------------------------------------------------------- */
29270 +
29271 +/* estimate the appropriate size for name hash table */
29272 +unsigned int au_rdhash_est(loff_t sz)
29273 +{
29274 +       unsigned int n;
29275 +
29276 +       n = UINT_MAX;
29277 +       sz >>= 10;
29278 +       if (sz < n)
29279 +               n = sz;
29280 +       if (sz < AUFS_RDHASH_DEF)
29281 +               n = AUFS_RDHASH_DEF;
29282 +       /* pr_info("n %u\n", n); */
29283 +       return n;
29284 +}
29285 +
29286 +/*
29287 + * the allocated memory has to be freed by
29288 + * au_nhash_wh_free() or au_nhash_de_free().
29289 + */
29290 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29291 +{
29292 +       struct hlist_head *head;
29293 +       unsigned int u;
29294 +       size_t sz;
29295 +
29296 +       sz = sizeof(*nhash->nh_head) * num_hash;
29297 +       head = kmalloc(sz, gfp);
29298 +       if (head) {
29299 +               nhash->nh_num = num_hash;
29300 +               nhash->nh_head = head;
29301 +               for (u = 0; u < num_hash; u++)
29302 +                       INIT_HLIST_HEAD(head++);
29303 +               return 0; /* success */
29304 +       }
29305 +
29306 +       return -ENOMEM;
29307 +}
29308 +
29309 +static void nhash_count(struct hlist_head *head)
29310 +{
29311 +#if 0
29312 +       unsigned long n;
29313 +       struct hlist_node *pos;
29314 +
29315 +       n = 0;
29316 +       hlist_for_each(pos, head)
29317 +               n++;
29318 +       pr_info("%lu\n", n);
29319 +#endif
29320 +}
29321 +
29322 +static void au_nhash_wh_do_free(struct hlist_head *head)
29323 +{
29324 +       struct au_vdir_wh *pos;
29325 +       struct hlist_node *node;
29326 +
29327 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29328 +               kfree(pos);
29329 +}
29330 +
29331 +static void au_nhash_de_do_free(struct hlist_head *head)
29332 +{
29333 +       struct au_vdir_dehstr *pos;
29334 +       struct hlist_node *node;
29335 +
29336 +       hlist_for_each_entry_safe(pos, node, head, hash)
29337 +               au_cache_free_vdir_dehstr(pos);
29338 +}
29339 +
29340 +static void au_nhash_do_free(struct au_nhash *nhash,
29341 +                            void (*free)(struct hlist_head *head))
29342 +{
29343 +       unsigned int n;
29344 +       struct hlist_head *head;
29345 +
29346 +       n = nhash->nh_num;
29347 +       if (!n)
29348 +               return;
29349 +
29350 +       head = nhash->nh_head;
29351 +       while (n-- > 0) {
29352 +               nhash_count(head);
29353 +               free(head++);
29354 +       }
29355 +       kfree(nhash->nh_head);
29356 +}
29357 +
29358 +void au_nhash_wh_free(struct au_nhash *whlist)
29359 +{
29360 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29361 +}
29362 +
29363 +static void au_nhash_de_free(struct au_nhash *delist)
29364 +{
29365 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29366 +}
29367 +
29368 +/* ---------------------------------------------------------------------- */
29369 +
29370 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29371 +                           int limit)
29372 +{
29373 +       int num;
29374 +       unsigned int u, n;
29375 +       struct hlist_head *head;
29376 +       struct au_vdir_wh *pos;
29377 +
29378 +       num = 0;
29379 +       n = whlist->nh_num;
29380 +       head = whlist->nh_head;
29381 +       for (u = 0; u < n; u++, head++)
29382 +               hlist_for_each_entry(pos, head, wh_hash)
29383 +                       if (pos->wh_bindex == btgt && ++num > limit)
29384 +                               return 1;
29385 +       return 0;
29386 +}
29387 +
29388 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29389 +                                      unsigned char *name,
29390 +                                      unsigned int len)
29391 +{
29392 +       unsigned int v;
29393 +       /* const unsigned int magic_bit = 12; */
29394 +
29395 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29396 +
29397 +       v = 0;
29398 +       while (len--)
29399 +               v += *name++;
29400 +       /* v = hash_long(v, magic_bit); */
29401 +       v %= nhash->nh_num;
29402 +       return nhash->nh_head + v;
29403 +}
29404 +
29405 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29406 +                             int nlen)
29407 +{
29408 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29409 +}
29410 +
29411 +/* returns found or not */
29412 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29413 +{
29414 +       struct hlist_head *head;
29415 +       struct au_vdir_wh *pos;
29416 +       struct au_vdir_destr *str;
29417 +
29418 +       head = au_name_hash(whlist, name, nlen);
29419 +       hlist_for_each_entry(pos, head, wh_hash) {
29420 +               str = &pos->wh_str;
29421 +               AuDbg("%.*s\n", str->len, str->name);
29422 +               if (au_nhash_test_name(str, name, nlen))
29423 +                       return 1;
29424 +       }
29425 +       return 0;
29426 +}
29427 +
29428 +/* returns found(true) or not */
29429 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29430 +{
29431 +       struct hlist_head *head;
29432 +       struct au_vdir_dehstr *pos;
29433 +       struct au_vdir_destr *str;
29434 +
29435 +       head = au_name_hash(delist, name, nlen);
29436 +       hlist_for_each_entry(pos, head, hash) {
29437 +               str = pos->str;
29438 +               AuDbg("%.*s\n", str->len, str->name);
29439 +               if (au_nhash_test_name(str, name, nlen))
29440 +                       return 1;
29441 +       }
29442 +       return 0;
29443 +}
29444 +
29445 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29446 +                           unsigned char d_type)
29447 +{
29448 +#ifdef CONFIG_AUFS_SHWH
29449 +       wh->wh_ino = ino;
29450 +       wh->wh_type = d_type;
29451 +#endif
29452 +}
29453 +
29454 +/* ---------------------------------------------------------------------- */
29455 +
29456 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29457 +                      unsigned int d_type, aufs_bindex_t bindex,
29458 +                      unsigned char shwh)
29459 +{
29460 +       int err;
29461 +       struct au_vdir_destr *str;
29462 +       struct au_vdir_wh *wh;
29463 +
29464 +       AuDbg("%.*s\n", nlen, name);
29465 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29466 +
29467 +       err = -ENOMEM;
29468 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29469 +       if (unlikely(!wh))
29470 +               goto out;
29471 +
29472 +       err = 0;
29473 +       wh->wh_bindex = bindex;
29474 +       if (shwh)
29475 +               au_shwh_init_wh(wh, ino, d_type);
29476 +       str = &wh->wh_str;
29477 +       str->len = nlen;
29478 +       memcpy(str->name, name, nlen);
29479 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29480 +       /* smp_mb(); */
29481 +
29482 +out:
29483 +       return err;
29484 +}
29485 +
29486 +static int append_deblk(struct au_vdir *vdir)
29487 +{
29488 +       int err;
29489 +       unsigned long ul;
29490 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29491 +       union au_vdir_deblk_p p, deblk_end;
29492 +       unsigned char **o;
29493 +
29494 +       err = -ENOMEM;
29495 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29496 +                    GFP_NOFS);
29497 +       if (unlikely(!o))
29498 +               goto out;
29499 +
29500 +       vdir->vd_deblk = o;
29501 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29502 +       if (p.deblk) {
29503 +               ul = vdir->vd_nblk++;
29504 +               vdir->vd_deblk[ul] = p.deblk;
29505 +               vdir->vd_last.ul = ul;
29506 +               vdir->vd_last.p.deblk = p.deblk;
29507 +               deblk_end.deblk = p.deblk + deblk_sz;
29508 +               err = set_deblk_end(&p, &deblk_end);
29509 +       }
29510 +
29511 +out:
29512 +       return err;
29513 +}
29514 +
29515 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29516 +                    unsigned int d_type, struct au_nhash *delist)
29517 +{
29518 +       int err;
29519 +       unsigned int sz;
29520 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29521 +       union au_vdir_deblk_p p, *room, deblk_end;
29522 +       struct au_vdir_dehstr *dehstr;
29523 +
29524 +       p.deblk = last_deblk(vdir);
29525 +       deblk_end.deblk = p.deblk + deblk_sz;
29526 +       room = &vdir->vd_last.p;
29527 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29528 +                 || !is_deblk_end(room, &deblk_end));
29529 +
29530 +       sz = calc_size(nlen);
29531 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29532 +               err = append_deblk(vdir);
29533 +               if (unlikely(err))
29534 +                       goto out;
29535 +
29536 +               p.deblk = last_deblk(vdir);
29537 +               deblk_end.deblk = p.deblk + deblk_sz;
29538 +               /* smp_mb(); */
29539 +               AuDebugOn(room->deblk != p.deblk);
29540 +       }
29541 +
29542 +       err = -ENOMEM;
29543 +       dehstr = au_cache_alloc_vdir_dehstr();
29544 +       if (unlikely(!dehstr))
29545 +               goto out;
29546 +
29547 +       dehstr->str = &room->de->de_str;
29548 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29549 +       room->de->de_ino = ino;
29550 +       room->de->de_type = d_type;
29551 +       room->de->de_str.len = nlen;
29552 +       memcpy(room->de->de_str.name, name, nlen);
29553 +
29554 +       err = 0;
29555 +       room->deblk += sz;
29556 +       if (unlikely(set_deblk_end(room, &deblk_end)))
29557 +               err = append_deblk(vdir);
29558 +       /* smp_mb(); */
29559 +
29560 +out:
29561 +       return err;
29562 +}
29563 +
29564 +/* ---------------------------------------------------------------------- */
29565 +
29566 +void au_vdir_free(struct au_vdir *vdir)
29567 +{
29568 +       unsigned char **deblk;
29569 +
29570 +       deblk = vdir->vd_deblk;
29571 +       while (vdir->vd_nblk--)
29572 +               kfree(*deblk++);
29573 +       kfree(vdir->vd_deblk);
29574 +       au_cache_free_vdir(vdir);
29575 +}
29576 +
29577 +static struct au_vdir *alloc_vdir(struct file *file)
29578 +{
29579 +       struct au_vdir *vdir;
29580 +       struct super_block *sb;
29581 +       int err;
29582 +
29583 +       sb = file->f_path.dentry->d_sb;
29584 +       SiMustAnyLock(sb);
29585 +
29586 +       err = -ENOMEM;
29587 +       vdir = au_cache_alloc_vdir();
29588 +       if (unlikely(!vdir))
29589 +               goto out;
29590 +
29591 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29592 +       if (unlikely(!vdir->vd_deblk))
29593 +               goto out_free;
29594 +
29595 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
29596 +       if (!vdir->vd_deblk_sz) {
29597 +               /* estimate the appropriate size for deblk */
29598 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
29599 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
29600 +       }
29601 +       vdir->vd_nblk = 0;
29602 +       vdir->vd_version = 0;
29603 +       vdir->vd_jiffy = 0;
29604 +       err = append_deblk(vdir);
29605 +       if (!err)
29606 +               return vdir; /* success */
29607 +
29608 +       kfree(vdir->vd_deblk);
29609 +
29610 +out_free:
29611 +       au_cache_free_vdir(vdir);
29612 +out:
29613 +       vdir = ERR_PTR(err);
29614 +       return vdir;
29615 +}
29616 +
29617 +static int reinit_vdir(struct au_vdir *vdir)
29618 +{
29619 +       int err;
29620 +       union au_vdir_deblk_p p, deblk_end;
29621 +
29622 +       while (vdir->vd_nblk > 1) {
29623 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
29624 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29625 +               vdir->vd_nblk--;
29626 +       }
29627 +       p.deblk = vdir->vd_deblk[0];
29628 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29629 +       err = set_deblk_end(&p, &deblk_end);
29630 +       /* keep vd_dblk_sz */
29631 +       vdir->vd_last.ul = 0;
29632 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29633 +       vdir->vd_version = 0;
29634 +       vdir->vd_jiffy = 0;
29635 +       /* smp_mb(); */
29636 +       return err;
29637 +}
29638 +
29639 +/* ---------------------------------------------------------------------- */
29640 +
29641 +#define AuFillVdir_CALLED      1
29642 +#define AuFillVdir_WHABLE      (1 << 1)
29643 +#define AuFillVdir_SHWH                (1 << 2)
29644 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
29645 +#define au_fset_fillvdir(flags, name) \
29646 +       do { (flags) |= AuFillVdir_##name; } while (0)
29647 +#define au_fclr_fillvdir(flags, name) \
29648 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
29649 +
29650 +#ifndef CONFIG_AUFS_SHWH
29651 +#undef AuFillVdir_SHWH
29652 +#define AuFillVdir_SHWH                0
29653 +#endif
29654 +
29655 +struct fillvdir_arg {
29656 +       struct dir_context      ctx;
29657 +       struct file             *file;
29658 +       struct au_vdir          *vdir;
29659 +       struct au_nhash         delist;
29660 +       struct au_nhash         whlist;
29661 +       aufs_bindex_t           bindex;
29662 +       unsigned int            flags;
29663 +       int                     err;
29664 +};
29665 +
29666 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
29667 +                   loff_t offset __maybe_unused, u64 h_ino,
29668 +                   unsigned int d_type)
29669 +{
29670 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
29671 +       char *name = (void *)__name;
29672 +       struct super_block *sb;
29673 +       ino_t ino;
29674 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
29675 +
29676 +       arg->err = 0;
29677 +       sb = arg->file->f_path.dentry->d_sb;
29678 +       au_fset_fillvdir(arg->flags, CALLED);
29679 +       /* smp_mb(); */
29680 +       if (nlen <= AUFS_WH_PFX_LEN
29681 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
29682 +               if (test_known(&arg->delist, name, nlen)
29683 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29684 +                       goto out; /* already exists or whiteouted */
29685 +
29686 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
29687 +               if (!arg->err) {
29688 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
29689 +                               d_type = DT_UNKNOWN;
29690 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
29691 +                                            d_type, &arg->delist);
29692 +               }
29693 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29694 +               name += AUFS_WH_PFX_LEN;
29695 +               nlen -= AUFS_WH_PFX_LEN;
29696 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29697 +                       goto out; /* already whiteouted */
29698 +
29699 +               if (shwh)
29700 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29701 +                                            &ino);
29702 +               if (!arg->err) {
29703 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29704 +                               d_type = DT_UNKNOWN;
29705 +                       arg->err = au_nhash_append_wh
29706 +                               (&arg->whlist, name, nlen, ino, d_type,
29707 +                                arg->bindex, shwh);
29708 +               }
29709 +       }
29710 +
29711 +out:
29712 +       if (!arg->err)
29713 +               arg->vdir->vd_jiffy = jiffies;
29714 +       /* smp_mb(); */
29715 +       AuTraceErr(arg->err);
29716 +       return arg->err;
29717 +}
29718 +
29719 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29720 +                         struct au_nhash *whlist, struct au_nhash *delist)
29721 +{
29722 +#ifdef CONFIG_AUFS_SHWH
29723 +       int err;
29724 +       unsigned int nh, u;
29725 +       struct hlist_head *head;
29726 +       struct au_vdir_wh *pos;
29727 +       struct hlist_node *n;
29728 +       char *p, *o;
29729 +       struct au_vdir_destr *destr;
29730 +
29731 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29732 +
29733 +       err = -ENOMEM;
29734 +       o = p = (void *)__get_free_page(GFP_NOFS);
29735 +       if (unlikely(!p))
29736 +               goto out;
29737 +
29738 +       err = 0;
29739 +       nh = whlist->nh_num;
29740 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29741 +       p += AUFS_WH_PFX_LEN;
29742 +       for (u = 0; u < nh; u++) {
29743 +               head = whlist->nh_head + u;
29744 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29745 +                       destr = &pos->wh_str;
29746 +                       memcpy(p, destr->name, destr->len);
29747 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
29748 +                                       pos->wh_ino, pos->wh_type, delist);
29749 +                       if (unlikely(err))
29750 +                               break;
29751 +               }
29752 +       }
29753 +
29754 +       free_page((unsigned long)o);
29755 +
29756 +out:
29757 +       AuTraceErr(err);
29758 +       return err;
29759 +#else
29760 +       return 0;
29761 +#endif
29762 +}
29763 +
29764 +static int au_do_read_vdir(struct fillvdir_arg *arg)
29765 +{
29766 +       int err;
29767 +       unsigned int rdhash;
29768 +       loff_t offset;
29769 +       aufs_bindex_t bend, bindex, bstart;
29770 +       unsigned char shwh;
29771 +       struct file *hf, *file;
29772 +       struct super_block *sb;
29773 +
29774 +       file = arg->file;
29775 +       sb = file->f_path.dentry->d_sb;
29776 +       SiMustAnyLock(sb);
29777 +
29778 +       rdhash = au_sbi(sb)->si_rdhash;
29779 +       if (!rdhash)
29780 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
29781 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29782 +       if (unlikely(err))
29783 +               goto out;
29784 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29785 +       if (unlikely(err))
29786 +               goto out_delist;
29787 +
29788 +       err = 0;
29789 +       arg->flags = 0;
29790 +       shwh = 0;
29791 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
29792 +               shwh = 1;
29793 +               au_fset_fillvdir(arg->flags, SHWH);
29794 +       }
29795 +       bstart = au_fbstart(file);
29796 +       bend = au_fbend_dir(file);
29797 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
29798 +               hf = au_hf_dir(file, bindex);
29799 +               if (!hf)
29800 +                       continue;
29801 +
29802 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
29803 +               err = offset;
29804 +               if (unlikely(offset))
29805 +                       break;
29806 +
29807 +               arg->bindex = bindex;
29808 +               au_fclr_fillvdir(arg->flags, WHABLE);
29809 +               if (shwh
29810 +                   || (bindex != bend
29811 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
29812 +                       au_fset_fillvdir(arg->flags, WHABLE);
29813 +               do {
29814 +                       arg->err = 0;
29815 +                       au_fclr_fillvdir(arg->flags, CALLED);
29816 +                       /* smp_mb(); */
29817 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
29818 +                       if (err >= 0)
29819 +                               err = arg->err;
29820 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
29821 +
29822 +               /*
29823 +                * dir_relax() may be good for concurrency, but aufs should not
29824 +                * use it since it will cause a lockdep problem.
29825 +                */
29826 +       }
29827 +
29828 +       if (!err && shwh)
29829 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29830 +
29831 +       au_nhash_wh_free(&arg->whlist);
29832 +
29833 +out_delist:
29834 +       au_nhash_de_free(&arg->delist);
29835 +out:
29836 +       return err;
29837 +}
29838 +
29839 +static int read_vdir(struct file *file, int may_read)
29840 +{
29841 +       int err;
29842 +       unsigned long expire;
29843 +       unsigned char do_read;
29844 +       struct fillvdir_arg arg = {
29845 +               .ctx = {
29846 +                       .actor = fillvdir
29847 +               }
29848 +       };
29849 +       struct inode *inode;
29850 +       struct au_vdir *vdir, *allocated;
29851 +
29852 +       err = 0;
29853 +       inode = file_inode(file);
29854 +       IMustLock(inode);
29855 +       SiMustAnyLock(inode->i_sb);
29856 +
29857 +       allocated = NULL;
29858 +       do_read = 0;
29859 +       expire = au_sbi(inode->i_sb)->si_rdcache;
29860 +       vdir = au_ivdir(inode);
29861 +       if (!vdir) {
29862 +               do_read = 1;
29863 +               vdir = alloc_vdir(file);
29864 +               err = PTR_ERR(vdir);
29865 +               if (IS_ERR(vdir))
29866 +                       goto out;
29867 +               err = 0;
29868 +               allocated = vdir;
29869 +       } else if (may_read
29870 +                  && (inode->i_version != vdir->vd_version
29871 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
29872 +               do_read = 1;
29873 +               err = reinit_vdir(vdir);
29874 +               if (unlikely(err))
29875 +                       goto out;
29876 +       }
29877 +
29878 +       if (!do_read)
29879 +               return 0; /* success */
29880 +
29881 +       arg.file = file;
29882 +       arg.vdir = vdir;
29883 +       err = au_do_read_vdir(&arg);
29884 +       if (!err) {
29885 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
29886 +               vdir->vd_version = inode->i_version;
29887 +               vdir->vd_last.ul = 0;
29888 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29889 +               if (allocated)
29890 +                       au_set_ivdir(inode, allocated);
29891 +       } else if (allocated)
29892 +               au_vdir_free(allocated);
29893 +
29894 +out:
29895 +       return err;
29896 +}
29897 +
29898 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29899 +{
29900 +       int err, rerr;
29901 +       unsigned long ul, n;
29902 +       const unsigned int deblk_sz = src->vd_deblk_sz;
29903 +
29904 +       AuDebugOn(tgt->vd_nblk != 1);
29905 +
29906 +       err = -ENOMEM;
29907 +       if (tgt->vd_nblk < src->vd_nblk) {
29908 +               unsigned char **p;
29909 +
29910 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29911 +                            GFP_NOFS);
29912 +               if (unlikely(!p))
29913 +                       goto out;
29914 +               tgt->vd_deblk = p;
29915 +       }
29916 +
29917 +       if (tgt->vd_deblk_sz != deblk_sz) {
29918 +               unsigned char *p;
29919 +
29920 +               tgt->vd_deblk_sz = deblk_sz;
29921 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29922 +               if (unlikely(!p))
29923 +                       goto out;
29924 +               tgt->vd_deblk[0] = p;
29925 +       }
29926 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
29927 +       tgt->vd_version = src->vd_version;
29928 +       tgt->vd_jiffy = src->vd_jiffy;
29929 +
29930 +       n = src->vd_nblk;
29931 +       for (ul = 1; ul < n; ul++) {
29932 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29933 +                                           GFP_NOFS);
29934 +               if (unlikely(!tgt->vd_deblk[ul]))
29935 +                       goto out;
29936 +               tgt->vd_nblk++;
29937 +       }
29938 +       tgt->vd_nblk = n;
29939 +       tgt->vd_last.ul = tgt->vd_last.ul;
29940 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
29941 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
29942 +               - src->vd_deblk[src->vd_last.ul];
29943 +       /* smp_mb(); */
29944 +       return 0; /* success */
29945 +
29946 +out:
29947 +       rerr = reinit_vdir(tgt);
29948 +       BUG_ON(rerr);
29949 +       return err;
29950 +}
29951 +
29952 +int au_vdir_init(struct file *file)
29953 +{
29954 +       int err;
29955 +       struct inode *inode;
29956 +       struct au_vdir *vdir_cache, *allocated;
29957 +
29958 +       /* test file->f_pos here instead of ctx->pos */
29959 +       err = read_vdir(file, !file->f_pos);
29960 +       if (unlikely(err))
29961 +               goto out;
29962 +
29963 +       allocated = NULL;
29964 +       vdir_cache = au_fvdir_cache(file);
29965 +       if (!vdir_cache) {
29966 +               vdir_cache = alloc_vdir(file);
29967 +               err = PTR_ERR(vdir_cache);
29968 +               if (IS_ERR(vdir_cache))
29969 +                       goto out;
29970 +               allocated = vdir_cache;
29971 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
29972 +               /* test file->f_pos here instead of ctx->pos */
29973 +               err = reinit_vdir(vdir_cache);
29974 +               if (unlikely(err))
29975 +                       goto out;
29976 +       } else
29977 +               return 0; /* success */
29978 +
29979 +       inode = file_inode(file);
29980 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
29981 +       if (!err) {
29982 +               file->f_version = inode->i_version;
29983 +               if (allocated)
29984 +                       au_set_fvdir_cache(file, allocated);
29985 +       } else if (allocated)
29986 +               au_vdir_free(allocated);
29987 +
29988 +out:
29989 +       return err;
29990 +}
29991 +
29992 +static loff_t calc_offset(struct au_vdir *vdir)
29993 +{
29994 +       loff_t offset;
29995 +       union au_vdir_deblk_p p;
29996 +
29997 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
29998 +       offset = vdir->vd_last.p.deblk - p.deblk;
29999 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
30000 +       return offset;
30001 +}
30002 +
30003 +/* returns true or false */
30004 +static int seek_vdir(struct file *file, struct dir_context *ctx)
30005 +{
30006 +       int valid;
30007 +       unsigned int deblk_sz;
30008 +       unsigned long ul, n;
30009 +       loff_t offset;
30010 +       union au_vdir_deblk_p p, deblk_end;
30011 +       struct au_vdir *vdir_cache;
30012 +
30013 +       valid = 1;
30014 +       vdir_cache = au_fvdir_cache(file);
30015 +       offset = calc_offset(vdir_cache);
30016 +       AuDbg("offset %lld\n", offset);
30017 +       if (ctx->pos == offset)
30018 +               goto out;
30019 +
30020 +       vdir_cache->vd_last.ul = 0;
30021 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
30022 +       if (!ctx->pos)
30023 +               goto out;
30024 +
30025 +       valid = 0;
30026 +       deblk_sz = vdir_cache->vd_deblk_sz;
30027 +       ul = div64_u64(ctx->pos, deblk_sz);
30028 +       AuDbg("ul %lu\n", ul);
30029 +       if (ul >= vdir_cache->vd_nblk)
30030 +               goto out;
30031 +
30032 +       n = vdir_cache->vd_nblk;
30033 +       for (; ul < n; ul++) {
30034 +               p.deblk = vdir_cache->vd_deblk[ul];
30035 +               deblk_end.deblk = p.deblk + deblk_sz;
30036 +               offset = ul;
30037 +               offset *= deblk_sz;
30038 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
30039 +                       unsigned int l;
30040 +
30041 +                       l = calc_size(p.de->de_str.len);
30042 +                       offset += l;
30043 +                       p.deblk += l;
30044 +               }
30045 +               if (!is_deblk_end(&p, &deblk_end)) {
30046 +                       valid = 1;
30047 +                       vdir_cache->vd_last.ul = ul;
30048 +                       vdir_cache->vd_last.p = p;
30049 +                       break;
30050 +               }
30051 +       }
30052 +
30053 +out:
30054 +       /* smp_mb(); */
30055 +       AuTraceErr(!valid);
30056 +       return valid;
30057 +}
30058 +
30059 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
30060 +{
30061 +       unsigned int l, deblk_sz;
30062 +       union au_vdir_deblk_p deblk_end;
30063 +       struct au_vdir *vdir_cache;
30064 +       struct au_vdir_de *de;
30065 +
30066 +       vdir_cache = au_fvdir_cache(file);
30067 +       if (!seek_vdir(file, ctx))
30068 +               return 0;
30069 +
30070 +       deblk_sz = vdir_cache->vd_deblk_sz;
30071 +       while (1) {
30072 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30073 +               deblk_end.deblk += deblk_sz;
30074 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
30075 +                       de = vdir_cache->vd_last.p.de;
30076 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
30077 +                             de->de_str.len, de->de_str.name, ctx->pos,
30078 +                             (unsigned long)de->de_ino, de->de_type);
30079 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
30080 +                                              de->de_str.len, de->de_ino,
30081 +                                              de->de_type))) {
30082 +                               /* todo: ignore the error caused by udba? */
30083 +                               /* return err; */
30084 +                               return 0;
30085 +                       }
30086 +
30087 +                       l = calc_size(de->de_str.len);
30088 +                       vdir_cache->vd_last.p.deblk += l;
30089 +                       ctx->pos += l;
30090 +               }
30091 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30092 +                       vdir_cache->vd_last.ul++;
30093 +                       vdir_cache->vd_last.p.deblk
30094 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30095 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
30096 +                       continue;
30097 +               }
30098 +               break;
30099 +       }
30100 +
30101 +       /* smp_mb(); */
30102 +       return 0;
30103 +}
30104 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30105 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
30106 +++ linux/fs/aufs/vfsub.c       2016-04-24 18:32:51.397020264 +0200
30107 @@ -0,0 +1,884 @@
30108 +/*
30109 + * Copyright (C) 2005-2016 Junjiro R. Okajima
30110 + *
30111 + * This program, aufs is free software; you can redistribute it and/or modify
30112 + * it under the terms of the GNU General Public License as published by
30113 + * the Free Software Foundation; either version 2 of the License, or
30114 + * (at your option) any later version.
30115 + *
30116 + * This program is distributed in the hope that it will be useful,
30117 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30118 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30119 + * GNU General Public License for more details.
30120 + *
30121 + * You should have received a copy of the GNU General Public License
30122 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30123 + */
30124 +
30125 +/*
30126 + * sub-routines for VFS
30127 + */
30128 +
30129 +#include <linux/namei.h>
30130 +#include <linux/nsproxy.h>
30131 +#include <linux/security.h>
30132 +#include <linux/splice.h>
30133 +#include "../fs/mount.h"
30134 +#include "aufs.h"
30135 +
30136 +#ifdef CONFIG_AUFS_BR_FUSE
30137 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
30138 +{
30139 +       struct nsproxy *ns;
30140 +
30141 +       if (!au_test_fuse(h_sb) || !au_userns)
30142 +               return 0;
30143 +
30144 +       ns = current->nsproxy;
30145 +       /* no {get,put}_nsproxy(ns) */
30146 +       return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
30147 +}
30148 +#endif
30149 +
30150 +/* ---------------------------------------------------------------------- */
30151 +
30152 +int vfsub_update_h_iattr(struct path *h_path, int *did)
30153 +{
30154 +       int err;
30155 +       struct kstat st;
30156 +       struct super_block *h_sb;
30157 +
30158 +       /* for remote fs, leave work for its getattr or d_revalidate */
30159 +       /* for bad i_attr fs, handle them in aufs_getattr() */
30160 +       /* still some fs may acquire i_mutex. we need to skip them */
30161 +       err = 0;
30162 +       if (!did)
30163 +               did = &err;
30164 +       h_sb = h_path->dentry->d_sb;
30165 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30166 +       if (*did)
30167 +               err = vfs_getattr(h_path, &st);
30168 +
30169 +       return err;
30170 +}
30171 +
30172 +/* ---------------------------------------------------------------------- */
30173 +
30174 +struct file *vfsub_dentry_open(struct path *path, int flags)
30175 +{
30176 +       struct file *file;
30177 +
30178 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
30179 +                          current_cred());
30180 +       if (!IS_ERR_OR_NULL(file)
30181 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
30182 +               i_readcount_inc(d_inode(path->dentry));
30183 +
30184 +       return file;
30185 +}
30186 +
30187 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30188 +{
30189 +       struct file *file;
30190 +
30191 +       lockdep_off();
30192 +       file = filp_open(path,
30193 +                        oflags /* | __FMODE_NONOTIFY */,
30194 +                        mode);
30195 +       lockdep_on();
30196 +       if (IS_ERR(file))
30197 +               goto out;
30198 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30199 +
30200 +out:
30201 +       return file;
30202 +}
30203 +
30204 +/*
30205 + * Ideally this function should call VFS:do_last() in order to keep all its
30206 + * checkings. But it is very hard for aufs to regenerate several VFS internal
30207 + * structure such as nameidata. This is a second (or third) best approach.
30208 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30209 + */
30210 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30211 +                     struct vfsub_aopen_args *args, struct au_branch *br)
30212 +{
30213 +       int err;
30214 +       struct file *file = args->file;
30215 +       /* copied from linux/fs/namei.c:atomic_open() */
30216 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30217 +
30218 +       IMustLock(dir);
30219 +       AuDebugOn(!dir->i_op->atomic_open);
30220 +
30221 +       err = au_br_test_oflag(args->open_flag, br);
30222 +       if (unlikely(err))
30223 +               goto out;
30224 +
30225 +       args->file->f_path.dentry = DENTRY_NOT_SET;
30226 +       args->file->f_path.mnt = au_br_mnt(br);
30227 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30228 +                                    args->create_mode, args->opened);
30229 +       if (err >= 0) {
30230 +               /* some filesystems don't set FILE_CREATED while succeeded? */
30231 +               if (*args->opened & FILE_CREATED)
30232 +                       fsnotify_create(dir, dentry);
30233 +       } else
30234 +               goto out;
30235 +
30236 +
30237 +       if (!err) {
30238 +               /* todo: call VFS:may_open() here */
30239 +               err = open_check_o_direct(file);
30240 +               /* todo: ima_file_check() too? */
30241 +               if (!err && (args->open_flag & __FMODE_EXEC))
30242 +                       err = deny_write_access(file);
30243 +               if (unlikely(err))
30244 +                       /* note that the file is created and still opened */
30245 +                       goto out;
30246 +       }
30247 +
30248 +       atomic_inc(&br->br_count);
30249 +       fsnotify_open(file);
30250 +
30251 +out:
30252 +       return err;
30253 +}
30254 +
30255 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30256 +{
30257 +       int err;
30258 +
30259 +       err = kern_path(name, flags, path);
30260 +       if (!err && d_is_positive(path->dentry))
30261 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30262 +       return err;
30263 +}
30264 +
30265 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
30266 +                                            struct dentry *parent, int len)
30267 +{
30268 +       struct path path = {
30269 +               .mnt = NULL
30270 +       };
30271 +
30272 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
30273 +       if (IS_ERR(path.dentry))
30274 +               goto out;
30275 +       if (d_is_positive(path.dentry))
30276 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30277 +
30278 +out:
30279 +       AuTraceErrPtr(path.dentry);
30280 +       return path.dentry;
30281 +}
30282 +
30283 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30284 +                                   int len)
30285 +{
30286 +       struct path path = {
30287 +               .mnt = NULL
30288 +       };
30289 +
30290 +       /* VFS checks it too, but by WARN_ON_ONCE() */
30291 +       IMustLock(d_inode(parent));
30292 +
30293 +       path.dentry = lookup_one_len(name, parent, len);
30294 +       if (IS_ERR(path.dentry))
30295 +               goto out;
30296 +       if (d_is_positive(path.dentry))
30297 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30298 +
30299 +out:
30300 +       AuTraceErrPtr(path.dentry);
30301 +       return path.dentry;
30302 +}
30303 +
30304 +void vfsub_call_lkup_one(void *args)
30305 +{
30306 +       struct vfsub_lkup_one_args *a = args;
30307 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30308 +}
30309 +
30310 +/* ---------------------------------------------------------------------- */
30311 +
30312 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30313 +                                struct dentry *d2, struct au_hinode *hdir2)
30314 +{
30315 +       struct dentry *d;
30316 +
30317 +       lockdep_off();
30318 +       d = lock_rename(d1, d2);
30319 +       lockdep_on();
30320 +       au_hn_suspend(hdir1);
30321 +       if (hdir1 != hdir2)
30322 +               au_hn_suspend(hdir2);
30323 +
30324 +       return d;
30325 +}
30326 +
30327 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30328 +                        struct dentry *d2, struct au_hinode *hdir2)
30329 +{
30330 +       au_hn_resume(hdir1);
30331 +       if (hdir1 != hdir2)
30332 +               au_hn_resume(hdir2);
30333 +       lockdep_off();
30334 +       unlock_rename(d1, d2);
30335 +       lockdep_on();
30336 +}
30337 +
30338 +/* ---------------------------------------------------------------------- */
30339 +
30340 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30341 +{
30342 +       int err;
30343 +       struct dentry *d;
30344 +
30345 +       IMustLock(dir);
30346 +
30347 +       d = path->dentry;
30348 +       path->dentry = d->d_parent;
30349 +       err = security_path_mknod(path, d, mode, 0);
30350 +       path->dentry = d;
30351 +       if (unlikely(err))
30352 +               goto out;
30353 +
30354 +       lockdep_off();
30355 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30356 +       lockdep_on();
30357 +       if (!err) {
30358 +               struct path tmp = *path;
30359 +               int did;
30360 +
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 +               }
30366 +               /*ignore*/
30367 +       }
30368 +
30369 +out:
30370 +       return err;
30371 +}
30372 +
30373 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30374 +{
30375 +       int err;
30376 +       struct dentry *d;
30377 +
30378 +       IMustLock(dir);
30379 +
30380 +       d = path->dentry;
30381 +       path->dentry = d->d_parent;
30382 +       err = security_path_symlink(path, d, symname);
30383 +       path->dentry = d;
30384 +       if (unlikely(err))
30385 +               goto out;
30386 +
30387 +       lockdep_off();
30388 +       err = vfs_symlink(dir, path->dentry, symname);
30389 +       lockdep_on();
30390 +       if (!err) {
30391 +               struct path tmp = *path;
30392 +               int did;
30393 +
30394 +               vfsub_update_h_iattr(&tmp, &did);
30395 +               if (did) {
30396 +                       tmp.dentry = path->dentry->d_parent;
30397 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30398 +               }
30399 +               /*ignore*/
30400 +       }
30401 +
30402 +out:
30403 +       return err;
30404 +}
30405 +
30406 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30407 +{
30408 +       int err;
30409 +       struct dentry *d;
30410 +
30411 +       IMustLock(dir);
30412 +
30413 +       d = path->dentry;
30414 +       path->dentry = d->d_parent;
30415 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30416 +       path->dentry = d;
30417 +       if (unlikely(err))
30418 +               goto out;
30419 +
30420 +       lockdep_off();
30421 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30422 +       lockdep_on();
30423 +       if (!err) {
30424 +               struct path tmp = *path;
30425 +               int did;
30426 +
30427 +               vfsub_update_h_iattr(&tmp, &did);
30428 +               if (did) {
30429 +                       tmp.dentry = path->dentry->d_parent;
30430 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30431 +               }
30432 +               /*ignore*/
30433 +       }
30434 +
30435 +out:
30436 +       return err;
30437 +}
30438 +
30439 +static int au_test_nlink(struct inode *inode)
30440 +{
30441 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30442 +
30443 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30444 +           || inode->i_nlink < link_max)
30445 +               return 0;
30446 +       return -EMLINK;
30447 +}
30448 +
30449 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30450 +              struct inode **delegated_inode)
30451 +{
30452 +       int err;
30453 +       struct dentry *d;
30454 +
30455 +       IMustLock(dir);
30456 +
30457 +       err = au_test_nlink(d_inode(src_dentry));
30458 +       if (unlikely(err))
30459 +               return err;
30460 +
30461 +       /* we don't call may_linkat() */
30462 +       d = path->dentry;
30463 +       path->dentry = d->d_parent;
30464 +       err = security_path_link(src_dentry, path, d);
30465 +       path->dentry = d;
30466 +       if (unlikely(err))
30467 +               goto out;
30468 +
30469 +       lockdep_off();
30470 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30471 +       lockdep_on();
30472 +       if (!err) {
30473 +               struct path tmp = *path;
30474 +               int did;
30475 +
30476 +               /* fuse has different memory inode for the same inumber */
30477 +               vfsub_update_h_iattr(&tmp, &did);
30478 +               if (did) {
30479 +                       tmp.dentry = path->dentry->d_parent;
30480 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30481 +                       tmp.dentry = src_dentry;
30482 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30483 +               }
30484 +               /*ignore*/
30485 +       }
30486 +
30487 +out:
30488 +       return err;
30489 +}
30490 +
30491 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30492 +                struct inode *dir, struct path *path,
30493 +                struct inode **delegated_inode)
30494 +{
30495 +       int err;
30496 +       struct path tmp = {
30497 +               .mnt    = path->mnt
30498 +       };
30499 +       struct dentry *d;
30500 +
30501 +       IMustLock(dir);
30502 +       IMustLock(src_dir);
30503 +
30504 +       d = path->dentry;
30505 +       path->dentry = d->d_parent;
30506 +       tmp.dentry = src_dentry->d_parent;
30507 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30508 +       path->dentry = d;
30509 +       if (unlikely(err))
30510 +               goto out;
30511 +
30512 +       lockdep_off();
30513 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30514 +                        delegated_inode, /*flags*/0);
30515 +       lockdep_on();
30516 +       if (!err) {
30517 +               int did;
30518 +
30519 +               tmp.dentry = d->d_parent;
30520 +               vfsub_update_h_iattr(&tmp, &did);
30521 +               if (did) {
30522 +                       tmp.dentry = src_dentry;
30523 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30524 +                       tmp.dentry = src_dentry->d_parent;
30525 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30526 +               }
30527 +               /*ignore*/
30528 +       }
30529 +
30530 +out:
30531 +       return err;
30532 +}
30533 +
30534 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30535 +{
30536 +       int err;
30537 +       struct dentry *d;
30538 +
30539 +       IMustLock(dir);
30540 +
30541 +       d = path->dentry;
30542 +       path->dentry = d->d_parent;
30543 +       err = security_path_mkdir(path, d, mode);
30544 +       path->dentry = d;
30545 +       if (unlikely(err))
30546 +               goto out;
30547 +
30548 +       lockdep_off();
30549 +       err = vfs_mkdir(dir, path->dentry, mode);
30550 +       lockdep_on();
30551 +       if (!err) {
30552 +               struct path tmp = *path;
30553 +               int did;
30554 +
30555 +               vfsub_update_h_iattr(&tmp, &did);
30556 +               if (did) {
30557 +                       tmp.dentry = path->dentry->d_parent;
30558 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30559 +               }
30560 +               /*ignore*/
30561 +       }
30562 +
30563 +out:
30564 +       return err;
30565 +}
30566 +
30567 +int vfsub_rmdir(struct inode *dir, struct path *path)
30568 +{
30569 +       int err;
30570 +       struct dentry *d;
30571 +
30572 +       IMustLock(dir);
30573 +
30574 +       d = path->dentry;
30575 +       path->dentry = d->d_parent;
30576 +       err = security_path_rmdir(path, d);
30577 +       path->dentry = d;
30578 +       if (unlikely(err))
30579 +               goto out;
30580 +
30581 +       lockdep_off();
30582 +       err = vfs_rmdir(dir, path->dentry);
30583 +       lockdep_on();
30584 +       if (!err) {
30585 +               struct path tmp = {
30586 +                       .dentry = path->dentry->d_parent,
30587 +                       .mnt    = path->mnt
30588 +               };
30589 +
30590 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30591 +       }
30592 +
30593 +out:
30594 +       return err;
30595 +}
30596 +
30597 +/* ---------------------------------------------------------------------- */
30598 +
30599 +/* todo: support mmap_sem? */
30600 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30601 +                    loff_t *ppos)
30602 +{
30603 +       ssize_t err;
30604 +
30605 +       lockdep_off();
30606 +       err = vfs_read(file, ubuf, count, ppos);
30607 +       lockdep_on();
30608 +       if (err >= 0)
30609 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30610 +       return err;
30611 +}
30612 +
30613 +/* todo: kernel_read()? */
30614 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30615 +                    loff_t *ppos)
30616 +{
30617 +       ssize_t err;
30618 +       mm_segment_t oldfs;
30619 +       union {
30620 +               void *k;
30621 +               char __user *u;
30622 +       } buf;
30623 +
30624 +       buf.k = kbuf;
30625 +       oldfs = get_fs();
30626 +       set_fs(KERNEL_DS);
30627 +       err = vfsub_read_u(file, buf.u, count, ppos);
30628 +       set_fs(oldfs);
30629 +       return err;
30630 +}
30631 +
30632 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30633 +                     loff_t *ppos)
30634 +{
30635 +       ssize_t err;
30636 +
30637 +       lockdep_off();
30638 +       err = vfs_write(file, ubuf, count, ppos);
30639 +       lockdep_on();
30640 +       if (err >= 0)
30641 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30642 +       return err;
30643 +}
30644 +
30645 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30646 +{
30647 +       ssize_t err;
30648 +       mm_segment_t oldfs;
30649 +       union {
30650 +               void *k;
30651 +               const char __user *u;
30652 +       } buf;
30653 +
30654 +       buf.k = kbuf;
30655 +       oldfs = get_fs();
30656 +       set_fs(KERNEL_DS);
30657 +       err = vfsub_write_u(file, buf.u, count, ppos);
30658 +       set_fs(oldfs);
30659 +       return err;
30660 +}
30661 +
30662 +int vfsub_flush(struct file *file, fl_owner_t id)
30663 +{
30664 +       int err;
30665 +
30666 +       err = 0;
30667 +       if (file->f_op->flush) {
30668 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
30669 +                       err = file->f_op->flush(file, id);
30670 +               else {
30671 +                       lockdep_off();
30672 +                       err = file->f_op->flush(file, id);
30673 +                       lockdep_on();
30674 +               }
30675 +               if (!err)
30676 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30677 +               /*ignore*/
30678 +       }
30679 +       return err;
30680 +}
30681 +
30682 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
30683 +{
30684 +       int err;
30685 +
30686 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
30687 +
30688 +       lockdep_off();
30689 +       err = iterate_dir(file, ctx);
30690 +       lockdep_on();
30691 +       if (err >= 0)
30692 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30693 +       return err;
30694 +}
30695 +
30696 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30697 +                    struct pipe_inode_info *pipe, size_t len,
30698 +                    unsigned int flags)
30699 +{
30700 +       long err;
30701 +
30702 +       lockdep_off();
30703 +       err = do_splice_to(in, ppos, pipe, len, flags);
30704 +       lockdep_on();
30705 +       file_accessed(in);
30706 +       if (err >= 0)
30707 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30708 +       return err;
30709 +}
30710 +
30711 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30712 +                      loff_t *ppos, size_t len, unsigned int flags)
30713 +{
30714 +       long err;
30715 +
30716 +       lockdep_off();
30717 +       err = do_splice_from(pipe, out, ppos, len, flags);
30718 +       lockdep_on();
30719 +       if (err >= 0)
30720 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30721 +       return err;
30722 +}
30723 +
30724 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
30725 +{
30726 +       int err;
30727 +
30728 +       /* file can be NULL */
30729 +       lockdep_off();
30730 +       err = vfs_fsync(file, datasync);
30731 +       lockdep_on();
30732 +       if (!err) {
30733 +               if (!path) {
30734 +                       AuDebugOn(!file);
30735 +                       path = &file->f_path;
30736 +               }
30737 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30738 +       }
30739 +       return err;
30740 +}
30741 +
30742 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30743 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30744 +               struct file *h_file)
30745 +{
30746 +       int err;
30747 +       struct inode *h_inode;
30748 +       struct super_block *h_sb;
30749 +
30750 +       if (!h_file) {
30751 +               err = vfsub_truncate(h_path, length);
30752 +               goto out;
30753 +       }
30754 +
30755 +       h_inode = d_inode(h_path->dentry);
30756 +       h_sb = h_inode->i_sb;
30757 +       lockdep_off();
30758 +       sb_start_write(h_sb);
30759 +       lockdep_on();
30760 +       err = locks_verify_truncate(h_inode, h_file, length);
30761 +       if (!err)
30762 +               err = security_path_truncate(h_path);
30763 +       if (!err) {
30764 +               lockdep_off();
30765 +               err = do_truncate(h_path->dentry, length, attr, h_file);
30766 +               lockdep_on();
30767 +       }
30768 +       lockdep_off();
30769 +       sb_end_write(h_sb);
30770 +       lockdep_on();
30771 +
30772 +out:
30773 +       return err;
30774 +}
30775 +
30776 +/* ---------------------------------------------------------------------- */
30777 +
30778 +struct au_vfsub_mkdir_args {
30779 +       int *errp;
30780 +       struct inode *dir;
30781 +       struct path *path;
30782 +       int mode;
30783 +};
30784 +
30785 +static void au_call_vfsub_mkdir(void *args)
30786 +{
30787 +       struct au_vfsub_mkdir_args *a = args;
30788 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30789 +}
30790 +
30791 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30792 +{
30793 +       int err, do_sio, wkq_err;
30794 +
30795 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30796 +       if (!do_sio) {
30797 +               lockdep_off();
30798 +               err = vfsub_mkdir(dir, path, mode);
30799 +               lockdep_on();
30800 +       } else {
30801 +               struct au_vfsub_mkdir_args args = {
30802 +                       .errp   = &err,
30803 +                       .dir    = dir,
30804 +                       .path   = path,
30805 +                       .mode   = mode
30806 +               };
30807 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30808 +               if (unlikely(wkq_err))
30809 +                       err = wkq_err;
30810 +       }
30811 +
30812 +       return err;
30813 +}
30814 +
30815 +struct au_vfsub_rmdir_args {
30816 +       int *errp;
30817 +       struct inode *dir;
30818 +       struct path *path;
30819 +};
30820 +
30821 +static void au_call_vfsub_rmdir(void *args)
30822 +{
30823 +       struct au_vfsub_rmdir_args *a = args;
30824 +       *a->errp = vfsub_rmdir(a->dir, a->path);
30825 +}
30826 +
30827 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30828 +{
30829 +       int err, do_sio, wkq_err;
30830 +
30831 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30832 +       if (!do_sio) {
30833 +               lockdep_off();
30834 +               err = vfsub_rmdir(dir, path);
30835 +               lockdep_on();
30836 +       } else {
30837 +               struct au_vfsub_rmdir_args args = {
30838 +                       .errp   = &err,
30839 +                       .dir    = dir,
30840 +                       .path   = path
30841 +               };
30842 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30843 +               if (unlikely(wkq_err))
30844 +                       err = wkq_err;
30845 +       }
30846 +
30847 +       return err;
30848 +}
30849 +
30850 +/* ---------------------------------------------------------------------- */
30851 +
30852 +struct notify_change_args {
30853 +       int *errp;
30854 +       struct path *path;
30855 +       struct iattr *ia;
30856 +       struct inode **delegated_inode;
30857 +};
30858 +
30859 +static void call_notify_change(void *args)
30860 +{
30861 +       struct notify_change_args *a = args;
30862 +       struct inode *h_inode;
30863 +
30864 +       h_inode = d_inode(a->path->dentry);
30865 +       IMustLock(h_inode);
30866 +
30867 +       *a->errp = -EPERM;
30868 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
30869 +               lockdep_off();
30870 +               *a->errp = notify_change(a->path->dentry, a->ia,
30871 +                                        a->delegated_inode);
30872 +               lockdep_on();
30873 +               if (!*a->errp)
30874 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30875 +       }
30876 +       AuTraceErr(*a->errp);
30877 +}
30878 +
30879 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30880 +                       struct inode **delegated_inode)
30881 +{
30882 +       int err;
30883 +       struct notify_change_args args = {
30884 +               .errp                   = &err,
30885 +               .path                   = path,
30886 +               .ia                     = ia,
30887 +               .delegated_inode        = delegated_inode
30888 +       };
30889 +
30890 +       call_notify_change(&args);
30891 +
30892 +       return err;
30893 +}
30894 +
30895 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30896 +                           struct inode **delegated_inode)
30897 +{
30898 +       int err, wkq_err;
30899 +       struct notify_change_args args = {
30900 +               .errp                   = &err,
30901 +               .path                   = path,
30902 +               .ia                     = ia,
30903 +               .delegated_inode        = delegated_inode
30904 +       };
30905 +
30906 +       wkq_err = au_wkq_wait(call_notify_change, &args);
30907 +       if (unlikely(wkq_err))
30908 +               err = wkq_err;
30909 +
30910 +       return err;
30911 +}
30912 +
30913 +/* ---------------------------------------------------------------------- */
30914 +
30915 +struct unlink_args {
30916 +       int *errp;
30917 +       struct inode *dir;
30918 +       struct path *path;
30919 +       struct inode **delegated_inode;
30920 +};
30921 +
30922 +static void call_unlink(void *args)
30923 +{
30924 +       struct unlink_args *a = args;
30925 +       struct dentry *d = a->path->dentry;
30926 +       struct inode *h_inode;
30927 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
30928 +                                     && au_dcount(d) == 1);
30929 +
30930 +       IMustLock(a->dir);
30931 +
30932 +       a->path->dentry = d->d_parent;
30933 +       *a->errp = security_path_unlink(a->path, d);
30934 +       a->path->dentry = d;
30935 +       if (unlikely(*a->errp))
30936 +               return;
30937 +
30938 +       if (!stop_sillyrename)
30939 +               dget(d);
30940 +       h_inode = NULL;
30941 +       if (d_is_positive(d)) {
30942 +               h_inode = d_inode(d);
30943 +               ihold(h_inode);
30944 +       }
30945 +
30946 +       lockdep_off();
30947 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
30948 +       lockdep_on();
30949 +       if (!*a->errp) {
30950 +               struct path tmp = {
30951 +                       .dentry = d->d_parent,
30952 +                       .mnt    = a->path->mnt
30953 +               };
30954 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30955 +       }
30956 +
30957 +       if (!stop_sillyrename)
30958 +               dput(d);
30959 +       if (h_inode)
30960 +               iput(h_inode);
30961 +
30962 +       AuTraceErr(*a->errp);
30963 +}
30964 +
30965 +/*
30966 + * @dir: must be locked.
30967 + * @dentry: target dentry.
30968 + */
30969 +int vfsub_unlink(struct inode *dir, struct path *path,
30970 +                struct inode **delegated_inode, int force)
30971 +{
30972 +       int err;
30973 +       struct unlink_args args = {
30974 +               .errp                   = &err,
30975 +               .dir                    = dir,
30976 +               .path                   = path,
30977 +               .delegated_inode        = delegated_inode
30978 +       };
30979 +
30980 +       if (!force)
30981 +               call_unlink(&args);
30982 +       else {
30983 +               int wkq_err;
30984 +
30985 +               wkq_err = au_wkq_wait(call_unlink, &args);
30986 +               if (unlikely(wkq_err))
30987 +                       err = wkq_err;
30988 +       }
30989 +
30990 +       return err;
30991 +}
30992 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
30993 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
30994 +++ linux/fs/aufs/vfsub.h       2016-04-24 18:32:51.397020264 +0200
30995 @@ -0,0 +1,310 @@
30996 +/*
30997 + * Copyright (C) 2005-2016 Junjiro R. Okajima
30998 + *
30999 + * This program, aufs is free software; you can redistribute it and/or modify
31000 + * it under the terms of the GNU General Public License as published by
31001 + * the Free Software Foundation; either version 2 of the License, or
31002 + * (at your option) any later version.
31003 + *
31004 + * This program is distributed in the hope that it will be useful,
31005 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31006 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31007 + * GNU General Public License for more details.
31008 + *
31009 + * You should have received a copy of the GNU General Public License
31010 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31011 + */
31012 +
31013 +/*
31014 + * sub-routines for VFS
31015 + */
31016 +
31017 +#ifndef __AUFS_VFSUB_H__
31018 +#define __AUFS_VFSUB_H__
31019 +
31020 +#ifdef __KERNEL__
31021 +
31022 +#include <linux/fs.h>
31023 +#include <linux/mount.h>
31024 +#include <linux/posix_acl.h>
31025 +#include <linux/xattr.h>
31026 +#include "debug.h"
31027 +
31028 +/* copied from linux/fs/internal.h */
31029 +/* todo: BAD approach!! */
31030 +extern void __mnt_drop_write(struct vfsmount *);
31031 +extern int open_check_o_direct(struct file *f);
31032 +
31033 +/* ---------------------------------------------------------------------- */
31034 +
31035 +/* lock subclass for lower inode */
31036 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
31037 +/* reduce? gave up. */
31038 +enum {
31039 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
31040 +       AuLsc_I_PARENT,         /* lower inode, parent first */
31041 +       AuLsc_I_PARENT2,        /* copyup dirs */
31042 +       AuLsc_I_PARENT3,        /* copyup wh */
31043 +       AuLsc_I_CHILD,
31044 +       AuLsc_I_CHILD2,
31045 +       AuLsc_I_End
31046 +};
31047 +
31048 +/* to debug easier, do not make them inlined functions */
31049 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
31050 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
31051 +
31052 +/* ---------------------------------------------------------------------- */
31053 +
31054 +static inline void vfsub_drop_nlink(struct inode *inode)
31055 +{
31056 +       AuDebugOn(!inode->i_nlink);
31057 +       drop_nlink(inode);
31058 +}
31059 +
31060 +static inline void vfsub_dead_dir(struct inode *inode)
31061 +{
31062 +       AuDebugOn(!S_ISDIR(inode->i_mode));
31063 +       inode->i_flags |= S_DEAD;
31064 +       clear_nlink(inode);
31065 +}
31066 +
31067 +static inline int vfsub_native_ro(struct inode *inode)
31068 +{
31069 +       return (inode->i_sb->s_flags & MS_RDONLY)
31070 +               || IS_RDONLY(inode)
31071 +               /* || IS_APPEND(inode) */
31072 +               || IS_IMMUTABLE(inode);
31073 +}
31074 +
31075 +#ifdef CONFIG_AUFS_BR_FUSE
31076 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
31077 +#else
31078 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
31079 +#endif
31080 +
31081 +/* ---------------------------------------------------------------------- */
31082 +
31083 +int vfsub_update_h_iattr(struct path *h_path, int *did);
31084 +struct file *vfsub_dentry_open(struct path *path, int flags);
31085 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
31086 +struct vfsub_aopen_args {
31087 +       struct file     *file;
31088 +       unsigned int    open_flag;
31089 +       umode_t         create_mode;
31090 +       int             *opened;
31091 +};
31092 +struct au_branch;
31093 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
31094 +                     struct vfsub_aopen_args *args, struct au_branch *br);
31095 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
31096 +
31097 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
31098 +                                            struct dentry *parent, int len);
31099 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
31100 +                                   int len);
31101 +
31102 +struct vfsub_lkup_one_args {
31103 +       struct dentry **errp;
31104 +       struct qstr *name;
31105 +       struct dentry *parent;
31106 +};
31107 +
31108 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
31109 +                                           struct dentry *parent)
31110 +{
31111 +       return vfsub_lookup_one_len(name->name, parent, name->len);
31112 +}
31113 +
31114 +void vfsub_call_lkup_one(void *args);
31115 +
31116 +/* ---------------------------------------------------------------------- */
31117 +
31118 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
31119 +{
31120 +       int err;
31121 +
31122 +       lockdep_off();
31123 +       err = mnt_want_write(mnt);
31124 +       lockdep_on();
31125 +       return err;
31126 +}
31127 +
31128 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31129 +{
31130 +       lockdep_off();
31131 +       mnt_drop_write(mnt);
31132 +       lockdep_on();
31133 +}
31134 +
31135 +#if 0 /* reserved */
31136 +static inline void vfsub_mnt_drop_write_file(struct file *file)
31137 +{
31138 +       lockdep_off();
31139 +       mnt_drop_write_file(file);
31140 +       lockdep_on();
31141 +}
31142 +#endif
31143 +
31144 +/* ---------------------------------------------------------------------- */
31145 +
31146 +struct au_hinode;
31147 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31148 +                                struct dentry *d2, struct au_hinode *hdir2);
31149 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31150 +                        struct dentry *d2, struct au_hinode *hdir2);
31151 +
31152 +int vfsub_create(struct inode *dir, struct path *path, int mode,
31153 +                bool want_excl);
31154 +int vfsub_symlink(struct inode *dir, struct path *path,
31155 +                 const char *symname);
31156 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31157 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
31158 +              struct path *path, struct inode **delegated_inode);
31159 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
31160 +                struct inode *hdir, struct path *path,
31161 +                struct inode **delegated_inode);
31162 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31163 +int vfsub_rmdir(struct inode *dir, struct path *path);
31164 +
31165 +/* ---------------------------------------------------------------------- */
31166 +
31167 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31168 +                    loff_t *ppos);
31169 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31170 +                       loff_t *ppos);
31171 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31172 +                     loff_t *ppos);
31173 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31174 +                     loff_t *ppos);
31175 +int vfsub_flush(struct file *file, fl_owner_t id);
31176 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31177 +
31178 +static inline loff_t vfsub_f_size_read(struct file *file)
31179 +{
31180 +       return i_size_read(file_inode(file));
31181 +}
31182 +
31183 +static inline unsigned int vfsub_file_flags(struct file *file)
31184 +{
31185 +       unsigned int flags;
31186 +
31187 +       spin_lock(&file->f_lock);
31188 +       flags = file->f_flags;
31189 +       spin_unlock(&file->f_lock);
31190 +
31191 +       return flags;
31192 +}
31193 +
31194 +#if 0 /* reserved */
31195 +static inline void vfsub_file_accessed(struct file *h_file)
31196 +{
31197 +       file_accessed(h_file);
31198 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31199 +}
31200 +#endif
31201 +
31202 +#if 0 /* reserved */
31203 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31204 +                                    struct dentry *h_dentry)
31205 +{
31206 +       struct path h_path = {
31207 +               .dentry = h_dentry,
31208 +               .mnt    = h_mnt
31209 +       };
31210 +       touch_atime(&h_path);
31211 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31212 +}
31213 +#endif
31214 +
31215 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31216 +                                   int flags)
31217 +{
31218 +       return generic_update_time(h_inode, ts, flags);
31219 +       /* no vfsub_update_h_iattr() since we don't have struct path */
31220 +}
31221 +
31222 +#ifdef CONFIG_FS_POSIX_ACL
31223 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
31224 +{
31225 +       int err;
31226 +
31227 +       err = posix_acl_chmod(h_inode, h_mode);
31228 +       if (err == -EOPNOTSUPP)
31229 +               err = 0;
31230 +       return err;
31231 +}
31232 +#else
31233 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
31234 +#endif
31235 +
31236 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31237 +                    struct pipe_inode_info *pipe, size_t len,
31238 +                    unsigned int flags);
31239 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31240 +                      loff_t *ppos, size_t len, unsigned int flags);
31241 +
31242 +static inline long vfsub_truncate(struct path *path, loff_t length)
31243 +{
31244 +       long err;
31245 +
31246 +       lockdep_off();
31247 +       err = vfs_truncate(path, length);
31248 +       lockdep_on();
31249 +       return err;
31250 +}
31251 +
31252 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31253 +               struct file *h_file);
31254 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
31255 +
31256 +/* ---------------------------------------------------------------------- */
31257 +
31258 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31259 +{
31260 +       loff_t err;
31261 +
31262 +       lockdep_off();
31263 +       err = vfs_llseek(file, offset, origin);
31264 +       lockdep_on();
31265 +       return err;
31266 +}
31267 +
31268 +/* ---------------------------------------------------------------------- */
31269 +
31270 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31271 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
31272 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31273 +                           struct inode **delegated_inode);
31274 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31275 +                       struct inode **delegated_inode);
31276 +int vfsub_unlink(struct inode *dir, struct path *path,
31277 +                struct inode **delegated_inode, int force);
31278 +
31279 +/* ---------------------------------------------------------------------- */
31280 +
31281 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31282 +                                const void *value, size_t size, int flags)
31283 +{
31284 +       int err;
31285 +
31286 +       lockdep_off();
31287 +       err = vfs_setxattr(dentry, name, value, size, flags);
31288 +       lockdep_on();
31289 +
31290 +       return err;
31291 +}
31292 +
31293 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31294 +{
31295 +       int err;
31296 +
31297 +       lockdep_off();
31298 +       err = vfs_removexattr(dentry, name);
31299 +       lockdep_on();
31300 +
31301 +       return err;
31302 +}
31303 +
31304 +#endif /* __KERNEL__ */
31305 +#endif /* __AUFS_VFSUB_H__ */
31306 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31307 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
31308 +++ linux/fs/aufs/wbr_policy.c  2016-04-24 18:32:51.397020264 +0200
31309 @@ -0,0 +1,765 @@
31310 +/*
31311 + * Copyright (C) 2005-2016 Junjiro R. Okajima
31312 + *
31313 + * This program, aufs is free software; you can redistribute it and/or modify
31314 + * it under the terms of the GNU General Public License as published by
31315 + * the Free Software Foundation; either version 2 of the License, or
31316 + * (at your option) any later version.
31317 + *
31318 + * This program is distributed in the hope that it will be useful,
31319 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31320 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31321 + * GNU General Public License for more details.
31322 + *
31323 + * You should have received a copy of the GNU General Public License
31324 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31325 + */
31326 +
31327 +/*
31328 + * policies for selecting one among multiple writable branches
31329 + */
31330 +
31331 +#include <linux/statfs.h>
31332 +#include "aufs.h"
31333 +
31334 +/* subset of cpup_attr() */
31335 +static noinline_for_stack
31336 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31337 +{
31338 +       int err, sbits;
31339 +       struct iattr ia;
31340 +       struct inode *h_isrc;
31341 +
31342 +       h_isrc = d_inode(h_src);
31343 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31344 +       ia.ia_mode = h_isrc->i_mode;
31345 +       ia.ia_uid = h_isrc->i_uid;
31346 +       ia.ia_gid = h_isrc->i_gid;
31347 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31348 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
31349 +       /* no delegation since it is just created */
31350 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31351 +
31352 +       /* is this nfs only? */
31353 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31354 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31355 +               ia.ia_mode = h_isrc->i_mode;
31356 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31357 +       }
31358 +
31359 +       return err;
31360 +}
31361 +
31362 +#define AuCpdown_PARENT_OPQ    1
31363 +#define AuCpdown_WHED          (1 << 1)
31364 +#define AuCpdown_MADE_DIR      (1 << 2)
31365 +#define AuCpdown_DIROPQ                (1 << 3)
31366 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31367 +#define au_fset_cpdown(flags, name) \
31368 +       do { (flags) |= AuCpdown_##name; } while (0)
31369 +#define au_fclr_cpdown(flags, name) \
31370 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31371 +
31372 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31373 +                            unsigned int *flags)
31374 +{
31375 +       int err;
31376 +       struct dentry *opq_dentry;
31377 +
31378 +       opq_dentry = au_diropq_create(dentry, bdst);
31379 +       err = PTR_ERR(opq_dentry);
31380 +       if (IS_ERR(opq_dentry))
31381 +               goto out;
31382 +       dput(opq_dentry);
31383 +       au_fset_cpdown(*flags, DIROPQ);
31384 +
31385 +out:
31386 +       return err;
31387 +}
31388 +
31389 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31390 +                           struct inode *dir, aufs_bindex_t bdst)
31391 +{
31392 +       int err;
31393 +       struct path h_path;
31394 +       struct au_branch *br;
31395 +
31396 +       br = au_sbr(dentry->d_sb, bdst);
31397 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31398 +       err = PTR_ERR(h_path.dentry);
31399 +       if (IS_ERR(h_path.dentry))
31400 +               goto out;
31401 +
31402 +       err = 0;
31403 +       if (d_is_positive(h_path.dentry)) {
31404 +               h_path.mnt = au_br_mnt(br);
31405 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31406 +                                         dentry);
31407 +       }
31408 +       dput(h_path.dentry);
31409 +
31410 +out:
31411 +       return err;
31412 +}
31413 +
31414 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31415 +                        struct au_pin *pin,
31416 +                        struct dentry *h_parent, void *arg)
31417 +{
31418 +       int err, rerr;
31419 +       aufs_bindex_t bopq, bstart;
31420 +       struct path h_path;
31421 +       struct dentry *parent;
31422 +       struct inode *h_dir, *h_inode, *inode, *dir;
31423 +       unsigned int *flags = arg;
31424 +
31425 +       bstart = au_dbstart(dentry);
31426 +       /* dentry is di-locked */
31427 +       parent = dget_parent(dentry);
31428 +       dir = d_inode(parent);
31429 +       h_dir = d_inode(h_parent);
31430 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31431 +       IMustLock(h_dir);
31432 +
31433 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31434 +       if (unlikely(err < 0))
31435 +               goto out;
31436 +       h_path.dentry = au_h_dptr(dentry, bdst);
31437 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31438 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31439 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31440 +       if (unlikely(err))
31441 +               goto out_put;
31442 +       au_fset_cpdown(*flags, MADE_DIR);
31443 +
31444 +       bopq = au_dbdiropq(dentry);
31445 +       au_fclr_cpdown(*flags, WHED);
31446 +       au_fclr_cpdown(*flags, DIROPQ);
31447 +       if (au_dbwh(dentry) == bdst)
31448 +               au_fset_cpdown(*flags, WHED);
31449 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31450 +               au_fset_cpdown(*flags, PARENT_OPQ);
31451 +       h_inode = d_inode(h_path.dentry);
31452 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
31453 +       if (au_ftest_cpdown(*flags, WHED)) {
31454 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31455 +               if (unlikely(err)) {
31456 +                       inode_unlock(h_inode);
31457 +                       goto out_dir;
31458 +               }
31459 +       }
31460 +
31461 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
31462 +       inode_unlock(h_inode);
31463 +       if (unlikely(err))
31464 +               goto out_opq;
31465 +
31466 +       if (au_ftest_cpdown(*flags, WHED)) {
31467 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31468 +               if (unlikely(err))
31469 +                       goto out_opq;
31470 +       }
31471 +
31472 +       inode = d_inode(dentry);
31473 +       if (au_ibend(inode) < bdst)
31474 +               au_set_ibend(inode, bdst);
31475 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31476 +                     au_hi_flags(inode, /*isdir*/1));
31477 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
31478 +       goto out; /* success */
31479 +
31480 +       /* revert */
31481 +out_opq:
31482 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
31483 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
31484 +               rerr = au_diropq_remove(dentry, bdst);
31485 +               inode_unlock(h_inode);
31486 +               if (unlikely(rerr)) {
31487 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31488 +                               dentry, bdst, rerr);
31489 +                       err = -EIO;
31490 +                       goto out;
31491 +               }
31492 +       }
31493 +out_dir:
31494 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
31495 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31496 +               if (unlikely(rerr)) {
31497 +                       AuIOErr("failed removing %pd b%d (%d)\n",
31498 +                               dentry, bdst, rerr);
31499 +                       err = -EIO;
31500 +               }
31501 +       }
31502 +out_put:
31503 +       au_set_h_dptr(dentry, bdst, NULL);
31504 +       if (au_dbend(dentry) == bdst)
31505 +               au_update_dbend(dentry);
31506 +out:
31507 +       dput(parent);
31508 +       return err;
31509 +}
31510 +
31511 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31512 +{
31513 +       int err;
31514 +       unsigned int flags;
31515 +
31516 +       flags = 0;
31517 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
31518 +
31519 +       return err;
31520 +}
31521 +
31522 +/* ---------------------------------------------------------------------- */
31523 +
31524 +/* policies for create */
31525 +
31526 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
31527 +{
31528 +       int err, i, j, ndentry;
31529 +       aufs_bindex_t bopq;
31530 +       struct au_dcsub_pages dpages;
31531 +       struct au_dpage *dpage;
31532 +       struct dentry **dentries, *parent, *d;
31533 +
31534 +       err = au_dpages_init(&dpages, GFP_NOFS);
31535 +       if (unlikely(err))
31536 +               goto out;
31537 +       parent = dget_parent(dentry);
31538 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
31539 +       if (unlikely(err))
31540 +               goto out_free;
31541 +
31542 +       err = bindex;
31543 +       for (i = 0; i < dpages.ndpage; i++) {
31544 +               dpage = dpages.dpages + i;
31545 +               dentries = dpage->dentries;
31546 +               ndentry = dpage->ndentry;
31547 +               for (j = 0; j < ndentry; j++) {
31548 +                       d = dentries[j];
31549 +                       di_read_lock_parent2(d, !AuLock_IR);
31550 +                       bopq = au_dbdiropq(d);
31551 +                       di_read_unlock(d, !AuLock_IR);
31552 +                       if (bopq >= 0 && bopq < err)
31553 +                               err = bopq;
31554 +               }
31555 +       }
31556 +
31557 +out_free:
31558 +       dput(parent);
31559 +       au_dpages_free(&dpages);
31560 +out:
31561 +       return err;
31562 +}
31563 +
31564 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31565 +{
31566 +       for (; bindex >= 0; bindex--)
31567 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
31568 +                       return bindex;
31569 +       return -EROFS;
31570 +}
31571 +
31572 +/* top down parent */
31573 +static int au_wbr_create_tdp(struct dentry *dentry,
31574 +                            unsigned int flags __maybe_unused)
31575 +{
31576 +       int err;
31577 +       aufs_bindex_t bstart, bindex;
31578 +       struct super_block *sb;
31579 +       struct dentry *parent, *h_parent;
31580 +
31581 +       sb = dentry->d_sb;
31582 +       bstart = au_dbstart(dentry);
31583 +       err = bstart;
31584 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
31585 +               goto out;
31586 +
31587 +       err = -EROFS;
31588 +       parent = dget_parent(dentry);
31589 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
31590 +               h_parent = au_h_dptr(parent, bindex);
31591 +               if (!h_parent || d_is_negative(h_parent))
31592 +                       continue;
31593 +
31594 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31595 +                       err = bindex;
31596 +                       break;
31597 +               }
31598 +       }
31599 +       dput(parent);
31600 +
31601 +       /* bottom up here */
31602 +       if (unlikely(err < 0)) {
31603 +               err = au_wbr_bu(sb, bstart - 1);
31604 +               if (err >= 0)
31605 +                       err = au_wbr_nonopq(dentry, err);
31606 +       }
31607 +
31608 +out:
31609 +       AuDbg("b%d\n", err);
31610 +       return err;
31611 +}
31612 +
31613 +/* ---------------------------------------------------------------------- */
31614 +
31615 +/* an exception for the policy other than tdp */
31616 +static int au_wbr_create_exp(struct dentry *dentry)
31617 +{
31618 +       int err;
31619 +       aufs_bindex_t bwh, bdiropq;
31620 +       struct dentry *parent;
31621 +
31622 +       err = -1;
31623 +       bwh = au_dbwh(dentry);
31624 +       parent = dget_parent(dentry);
31625 +       bdiropq = au_dbdiropq(parent);
31626 +       if (bwh >= 0) {
31627 +               if (bdiropq >= 0)
31628 +                       err = min(bdiropq, bwh);
31629 +               else
31630 +                       err = bwh;
31631 +               AuDbg("%d\n", err);
31632 +       } else if (bdiropq >= 0) {
31633 +               err = bdiropq;
31634 +               AuDbg("%d\n", err);
31635 +       }
31636 +       dput(parent);
31637 +
31638 +       if (err >= 0)
31639 +               err = au_wbr_nonopq(dentry, err);
31640 +
31641 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31642 +               err = -1;
31643 +
31644 +       AuDbg("%d\n", err);
31645 +       return err;
31646 +}
31647 +
31648 +/* ---------------------------------------------------------------------- */
31649 +
31650 +/* round robin */
31651 +static int au_wbr_create_init_rr(struct super_block *sb)
31652 +{
31653 +       int err;
31654 +
31655 +       err = au_wbr_bu(sb, au_sbend(sb));
31656 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
31657 +       /* smp_mb(); */
31658 +
31659 +       AuDbg("b%d\n", err);
31660 +       return err;
31661 +}
31662 +
31663 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
31664 +{
31665 +       int err, nbr;
31666 +       unsigned int u;
31667 +       aufs_bindex_t bindex, bend;
31668 +       struct super_block *sb;
31669 +       atomic_t *next;
31670 +
31671 +       err = au_wbr_create_exp(dentry);
31672 +       if (err >= 0)
31673 +               goto out;
31674 +
31675 +       sb = dentry->d_sb;
31676 +       next = &au_sbi(sb)->si_wbr_rr_next;
31677 +       bend = au_sbend(sb);
31678 +       nbr = bend + 1;
31679 +       for (bindex = 0; bindex <= bend; bindex++) {
31680 +               if (!au_ftest_wbr(flags, DIR)) {
31681 +                       err = atomic_dec_return(next) + 1;
31682 +                       /* modulo for 0 is meaningless */
31683 +                       if (unlikely(!err))
31684 +                               err = atomic_dec_return(next) + 1;
31685 +               } else
31686 +                       err = atomic_read(next);
31687 +               AuDbg("%d\n", err);
31688 +               u = err;
31689 +               err = u % nbr;
31690 +               AuDbg("%d\n", err);
31691 +               if (!au_br_rdonly(au_sbr(sb, err)))
31692 +                       break;
31693 +               err = -EROFS;
31694 +       }
31695 +
31696 +       if (err >= 0)
31697 +               err = au_wbr_nonopq(dentry, err);
31698 +
31699 +out:
31700 +       AuDbg("%d\n", err);
31701 +       return err;
31702 +}
31703 +
31704 +/* ---------------------------------------------------------------------- */
31705 +
31706 +/* most free space */
31707 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
31708 +{
31709 +       struct super_block *sb;
31710 +       struct au_branch *br;
31711 +       struct au_wbr_mfs *mfs;
31712 +       struct dentry *h_parent;
31713 +       aufs_bindex_t bindex, bend;
31714 +       int err;
31715 +       unsigned long long b, bavail;
31716 +       struct path h_path;
31717 +       /* reduce the stack usage */
31718 +       struct kstatfs *st;
31719 +
31720 +       st = kmalloc(sizeof(*st), GFP_NOFS);
31721 +       if (unlikely(!st)) {
31722 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31723 +               return;
31724 +       }
31725 +
31726 +       bavail = 0;
31727 +       sb = dentry->d_sb;
31728 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31729 +       MtxMustLock(&mfs->mfs_lock);
31730 +       mfs->mfs_bindex = -EROFS;
31731 +       mfs->mfsrr_bytes = 0;
31732 +       if (!parent) {
31733 +               bindex = 0;
31734 +               bend = au_sbend(sb);
31735 +       } else {
31736 +               bindex = au_dbstart(parent);
31737 +               bend = au_dbtaildir(parent);
31738 +       }
31739 +
31740 +       for (; bindex <= bend; bindex++) {
31741 +               if (parent) {
31742 +                       h_parent = au_h_dptr(parent, bindex);
31743 +                       if (!h_parent || d_is_negative(h_parent))
31744 +                               continue;
31745 +               }
31746 +               br = au_sbr(sb, bindex);
31747 +               if (au_br_rdonly(br))
31748 +                       continue;
31749 +
31750 +               /* sb->s_root for NFS is unreliable */
31751 +               h_path.mnt = au_br_mnt(br);
31752 +               h_path.dentry = h_path.mnt->mnt_root;
31753 +               err = vfs_statfs(&h_path, st);
31754 +               if (unlikely(err)) {
31755 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
31756 +                       continue;
31757 +               }
31758 +
31759 +               /* when the available size is equal, select the lower one */
31760 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
31761 +                            || sizeof(b) < sizeof(st->f_bsize));
31762 +               b = st->f_bavail * st->f_bsize;
31763 +               br->br_wbr->wbr_bytes = b;
31764 +               if (b >= bavail) {
31765 +                       bavail = b;
31766 +                       mfs->mfs_bindex = bindex;
31767 +                       mfs->mfs_jiffy = jiffies;
31768 +               }
31769 +       }
31770 +
31771 +       mfs->mfsrr_bytes = bavail;
31772 +       AuDbg("b%d\n", mfs->mfs_bindex);
31773 +       kfree(st);
31774 +}
31775 +
31776 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
31777 +{
31778 +       int err;
31779 +       struct dentry *parent;
31780 +       struct super_block *sb;
31781 +       struct au_wbr_mfs *mfs;
31782 +
31783 +       err = au_wbr_create_exp(dentry);
31784 +       if (err >= 0)
31785 +               goto out;
31786 +
31787 +       sb = dentry->d_sb;
31788 +       parent = NULL;
31789 +       if (au_ftest_wbr(flags, PARENT))
31790 +               parent = dget_parent(dentry);
31791 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31792 +       mutex_lock(&mfs->mfs_lock);
31793 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
31794 +           || mfs->mfs_bindex < 0
31795 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
31796 +               au_mfs(dentry, parent);
31797 +       mutex_unlock(&mfs->mfs_lock);
31798 +       err = mfs->mfs_bindex;
31799 +       dput(parent);
31800 +
31801 +       if (err >= 0)
31802 +               err = au_wbr_nonopq(dentry, err);
31803 +
31804 +out:
31805 +       AuDbg("b%d\n", err);
31806 +       return err;
31807 +}
31808 +
31809 +static int au_wbr_create_init_mfs(struct super_block *sb)
31810 +{
31811 +       struct au_wbr_mfs *mfs;
31812 +
31813 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31814 +       mutex_init(&mfs->mfs_lock);
31815 +       mfs->mfs_jiffy = 0;
31816 +       mfs->mfs_bindex = -EROFS;
31817 +
31818 +       return 0;
31819 +}
31820 +
31821 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31822 +{
31823 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31824 +       return 0;
31825 +}
31826 +
31827 +/* ---------------------------------------------------------------------- */
31828 +
31829 +/* most free space and then round robin */
31830 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
31831 +{
31832 +       int err;
31833 +       struct au_wbr_mfs *mfs;
31834 +
31835 +       err = au_wbr_create_mfs(dentry, flags);
31836 +       if (err >= 0) {
31837 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
31838 +               mutex_lock(&mfs->mfs_lock);
31839 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
31840 +                       err = au_wbr_create_rr(dentry, flags);
31841 +               mutex_unlock(&mfs->mfs_lock);
31842 +       }
31843 +
31844 +       AuDbg("b%d\n", err);
31845 +       return err;
31846 +}
31847 +
31848 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
31849 +{
31850 +       int err;
31851 +
31852 +       au_wbr_create_init_mfs(sb); /* ignore */
31853 +       err = au_wbr_create_init_rr(sb);
31854 +
31855 +       return err;
31856 +}
31857 +
31858 +/* ---------------------------------------------------------------------- */
31859 +
31860 +/* top down parent and most free space */
31861 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
31862 +{
31863 +       int err, e2;
31864 +       unsigned long long b;
31865 +       aufs_bindex_t bindex, bstart, bend;
31866 +       struct super_block *sb;
31867 +       struct dentry *parent, *h_parent;
31868 +       struct au_branch *br;
31869 +
31870 +       err = au_wbr_create_tdp(dentry, flags);
31871 +       if (unlikely(err < 0))
31872 +               goto out;
31873 +       parent = dget_parent(dentry);
31874 +       bstart = au_dbstart(parent);
31875 +       bend = au_dbtaildir(parent);
31876 +       if (bstart == bend)
31877 +               goto out_parent; /* success */
31878 +
31879 +       e2 = au_wbr_create_mfs(dentry, flags);
31880 +       if (e2 < 0)
31881 +               goto out_parent; /* success */
31882 +
31883 +       /* when the available size is equal, select upper one */
31884 +       sb = dentry->d_sb;
31885 +       br = au_sbr(sb, err);
31886 +       b = br->br_wbr->wbr_bytes;
31887 +       AuDbg("b%d, %llu\n", err, b);
31888 +
31889 +       for (bindex = bstart; bindex <= bend; bindex++) {
31890 +               h_parent = au_h_dptr(parent, bindex);
31891 +               if (!h_parent || d_is_negative(h_parent))
31892 +                       continue;
31893 +
31894 +               br = au_sbr(sb, bindex);
31895 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31896 +                       b = br->br_wbr->wbr_bytes;
31897 +                       err = bindex;
31898 +                       AuDbg("b%d, %llu\n", err, b);
31899 +               }
31900 +       }
31901 +
31902 +       if (err >= 0)
31903 +               err = au_wbr_nonopq(dentry, err);
31904 +
31905 +out_parent:
31906 +       dput(parent);
31907 +out:
31908 +       AuDbg("b%d\n", err);
31909 +       return err;
31910 +}
31911 +
31912 +/* ---------------------------------------------------------------------- */
31913 +
31914 +/*
31915 + * - top down parent
31916 + * - most free space with parent
31917 + * - most free space round-robin regardless parent
31918 + */
31919 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31920 +{
31921 +       int err;
31922 +       unsigned long long watermark;
31923 +       struct super_block *sb;
31924 +       struct au_branch *br;
31925 +       struct au_wbr_mfs *mfs;
31926 +
31927 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31928 +       if (unlikely(err < 0))
31929 +               goto out;
31930 +
31931 +       sb = dentry->d_sb;
31932 +       br = au_sbr(sb, err);
31933 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31934 +       mutex_lock(&mfs->mfs_lock);
31935 +       watermark = mfs->mfsrr_watermark;
31936 +       mutex_unlock(&mfs->mfs_lock);
31937 +       if (br->br_wbr->wbr_bytes < watermark)
31938 +               /* regardless the parent dir */
31939 +               err = au_wbr_create_mfsrr(dentry, flags);
31940 +
31941 +out:
31942 +       AuDbg("b%d\n", err);
31943 +       return err;
31944 +}
31945 +
31946 +/* ---------------------------------------------------------------------- */
31947 +
31948 +/* policies for copyup */
31949 +
31950 +/* top down parent */
31951 +static int au_wbr_copyup_tdp(struct dentry *dentry)
31952 +{
31953 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
31954 +}
31955 +
31956 +/* bottom up parent */
31957 +static int au_wbr_copyup_bup(struct dentry *dentry)
31958 +{
31959 +       int err;
31960 +       aufs_bindex_t bindex, bstart;
31961 +       struct dentry *parent, *h_parent;
31962 +       struct super_block *sb;
31963 +
31964 +       err = -EROFS;
31965 +       sb = dentry->d_sb;
31966 +       parent = dget_parent(dentry);
31967 +       bstart = au_dbstart(parent);
31968 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
31969 +               h_parent = au_h_dptr(parent, bindex);
31970 +               if (!h_parent || d_is_negative(h_parent))
31971 +                       continue;
31972 +
31973 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31974 +                       err = bindex;
31975 +                       break;
31976 +               }
31977 +       }
31978 +       dput(parent);
31979 +
31980 +       /* bottom up here */
31981 +       if (unlikely(err < 0))
31982 +               err = au_wbr_bu(sb, bstart - 1);
31983 +
31984 +       AuDbg("b%d\n", err);
31985 +       return err;
31986 +}
31987 +
31988 +/* bottom up */
31989 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
31990 +{
31991 +       int err;
31992 +
31993 +       err = au_wbr_bu(dentry->d_sb, bstart);
31994 +       AuDbg("b%d\n", err);
31995 +       if (err > bstart)
31996 +               err = au_wbr_nonopq(dentry, err);
31997 +
31998 +       AuDbg("b%d\n", err);
31999 +       return err;
32000 +}
32001 +
32002 +static int au_wbr_copyup_bu(struct dentry *dentry)
32003 +{
32004 +       int err;
32005 +       aufs_bindex_t bstart;
32006 +
32007 +       bstart = au_dbstart(dentry);
32008 +       err = au_wbr_do_copyup_bu(dentry, bstart);
32009 +       return err;
32010 +}
32011 +
32012 +/* ---------------------------------------------------------------------- */
32013 +
32014 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
32015 +       [AuWbrCopyup_TDP] = {
32016 +               .copyup = au_wbr_copyup_tdp
32017 +       },
32018 +       [AuWbrCopyup_BUP] = {
32019 +               .copyup = au_wbr_copyup_bup
32020 +       },
32021 +       [AuWbrCopyup_BU] = {
32022 +               .copyup = au_wbr_copyup_bu
32023 +       }
32024 +};
32025 +
32026 +struct au_wbr_create_operations au_wbr_create_ops[] = {
32027 +       [AuWbrCreate_TDP] = {
32028 +               .create = au_wbr_create_tdp
32029 +       },
32030 +       [AuWbrCreate_RR] = {
32031 +               .create = au_wbr_create_rr,
32032 +               .init   = au_wbr_create_init_rr
32033 +       },
32034 +       [AuWbrCreate_MFS] = {
32035 +               .create = au_wbr_create_mfs,
32036 +               .init   = au_wbr_create_init_mfs,
32037 +               .fin    = au_wbr_create_fin_mfs
32038 +       },
32039 +       [AuWbrCreate_MFSV] = {
32040 +               .create = au_wbr_create_mfs,
32041 +               .init   = au_wbr_create_init_mfs,
32042 +               .fin    = au_wbr_create_fin_mfs
32043 +       },
32044 +       [AuWbrCreate_MFSRR] = {
32045 +               .create = au_wbr_create_mfsrr,
32046 +               .init   = au_wbr_create_init_mfsrr,
32047 +               .fin    = au_wbr_create_fin_mfs
32048 +       },
32049 +       [AuWbrCreate_MFSRRV] = {
32050 +               .create = au_wbr_create_mfsrr,
32051 +               .init   = au_wbr_create_init_mfsrr,
32052 +               .fin    = au_wbr_create_fin_mfs
32053 +       },
32054 +       [AuWbrCreate_PMFS] = {
32055 +               .create = au_wbr_create_pmfs,
32056 +               .init   = au_wbr_create_init_mfs,
32057 +               .fin    = au_wbr_create_fin_mfs
32058 +       },
32059 +       [AuWbrCreate_PMFSV] = {
32060 +               .create = au_wbr_create_pmfs,
32061 +               .init   = au_wbr_create_init_mfs,
32062 +               .fin    = au_wbr_create_fin_mfs
32063 +       },
32064 +       [AuWbrCreate_PMFSRR] = {
32065 +               .create = au_wbr_create_pmfsrr,
32066 +               .init   = au_wbr_create_init_mfsrr,
32067 +               .fin    = au_wbr_create_fin_mfs
32068 +       },
32069 +       [AuWbrCreate_PMFSRRV] = {
32070 +               .create = au_wbr_create_pmfsrr,
32071 +               .init   = au_wbr_create_init_mfsrr,
32072 +               .fin    = au_wbr_create_fin_mfs
32073 +       }
32074 +};
32075 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
32076 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
32077 +++ linux/fs/aufs/whout.c       2016-04-24 18:32:51.397020264 +0200
32078 @@ -0,0 +1,1060 @@
32079 +/*
32080 + * Copyright (C) 2005-2016 Junjiro R. Okajima
32081 + *
32082 + * This program, aufs is free software; you can redistribute it and/or modify
32083 + * it under the terms of the GNU General Public License as published by
32084 + * the Free Software Foundation; either version 2 of the License, or
32085 + * (at your option) any later version.
32086 + *
32087 + * This program is distributed in the hope that it will be useful,
32088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32090 + * GNU General Public License for more details.
32091 + *
32092 + * You should have received a copy of the GNU General Public License
32093 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32094 + */
32095 +
32096 +/*
32097 + * whiteout for logical deletion and opaque directory
32098 + */
32099 +
32100 +#include "aufs.h"
32101 +
32102 +#define WH_MASK                        S_IRUGO
32103 +
32104 +/*
32105 + * If a directory contains this file, then it is opaque.  We start with the
32106 + * .wh. flag so that it is blocked by lookup.
32107 + */
32108 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
32109 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
32110 +
32111 +/*
32112 + * generate whiteout name, which is NOT terminated by NULL.
32113 + * @name: original d_name.name
32114 + * @len: original d_name.len
32115 + * @wh: whiteout qstr
32116 + * returns zero when succeeds, otherwise error.
32117 + * succeeded value as wh->name should be freed by kfree().
32118 + */
32119 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
32120 +{
32121 +       char *p;
32122 +
32123 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
32124 +               return -ENAMETOOLONG;
32125 +
32126 +       wh->len = name->len + AUFS_WH_PFX_LEN;
32127 +       p = kmalloc(wh->len, GFP_NOFS);
32128 +       wh->name = p;
32129 +       if (p) {
32130 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32131 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
32132 +               /* smp_mb(); */
32133 +               return 0;
32134 +       }
32135 +       return -ENOMEM;
32136 +}
32137 +
32138 +/* ---------------------------------------------------------------------- */
32139 +
32140 +/*
32141 + * test if the @wh_name exists under @h_parent.
32142 + * @try_sio specifies the necessary of super-io.
32143 + */
32144 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
32145 +{
32146 +       int err;
32147 +       struct dentry *wh_dentry;
32148 +
32149 +       if (!try_sio)
32150 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
32151 +       else
32152 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
32153 +       err = PTR_ERR(wh_dentry);
32154 +       if (IS_ERR(wh_dentry)) {
32155 +               if (err == -ENAMETOOLONG)
32156 +                       err = 0;
32157 +               goto out;
32158 +       }
32159 +
32160 +       err = 0;
32161 +       if (d_is_negative(wh_dentry))
32162 +               goto out_wh; /* success */
32163 +
32164 +       err = 1;
32165 +       if (d_is_reg(wh_dentry))
32166 +               goto out_wh; /* success */
32167 +
32168 +       err = -EIO;
32169 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
32170 +               wh_dentry, d_inode(wh_dentry)->i_mode);
32171 +
32172 +out_wh:
32173 +       dput(wh_dentry);
32174 +out:
32175 +       return err;
32176 +}
32177 +
32178 +/*
32179 + * test if the @h_dentry sets opaque or not.
32180 + */
32181 +int au_diropq_test(struct dentry *h_dentry)
32182 +{
32183 +       int err;
32184 +       struct inode *h_dir;
32185 +
32186 +       h_dir = d_inode(h_dentry);
32187 +       err = au_wh_test(h_dentry, &diropq_name,
32188 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
32189 +       return err;
32190 +}
32191 +
32192 +/*
32193 + * returns a negative dentry whose name is unique and temporary.
32194 + */
32195 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32196 +                            struct qstr *prefix)
32197 +{
32198 +       struct dentry *dentry;
32199 +       int i;
32200 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
32201 +               *name, *p;
32202 +       /* strict atomic_t is unnecessary here */
32203 +       static unsigned short cnt;
32204 +       struct qstr qs;
32205 +
32206 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32207 +
32208 +       name = defname;
32209 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32210 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
32211 +               dentry = ERR_PTR(-ENAMETOOLONG);
32212 +               if (unlikely(qs.len > NAME_MAX))
32213 +                       goto out;
32214 +               dentry = ERR_PTR(-ENOMEM);
32215 +               name = kmalloc(qs.len + 1, GFP_NOFS);
32216 +               if (unlikely(!name))
32217 +                       goto out;
32218 +       }
32219 +
32220 +       /* doubly whiteout-ed */
32221 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32222 +       p = name + AUFS_WH_PFX_LEN * 2;
32223 +       memcpy(p, prefix->name, prefix->len);
32224 +       p += prefix->len;
32225 +       *p++ = '.';
32226 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
32227 +
32228 +       qs.name = name;
32229 +       for (i = 0; i < 3; i++) {
32230 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
32231 +               dentry = au_sio_lkup_one(&qs, h_parent);
32232 +               if (IS_ERR(dentry) || d_is_negative(dentry))
32233 +                       goto out_name;
32234 +               dput(dentry);
32235 +       }
32236 +       /* pr_warn("could not get random name\n"); */
32237 +       dentry = ERR_PTR(-EEXIST);
32238 +       AuDbg("%.*s\n", AuLNPair(&qs));
32239 +       BUG();
32240 +
32241 +out_name:
32242 +       if (name != defname)
32243 +               kfree(name);
32244 +out:
32245 +       AuTraceErrPtr(dentry);
32246 +       return dentry;
32247 +}
32248 +
32249 +/*
32250 + * rename the @h_dentry on @br to the whiteouted temporary name.
32251 + */
32252 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32253 +{
32254 +       int err;
32255 +       struct path h_path = {
32256 +               .mnt = au_br_mnt(br)
32257 +       };
32258 +       struct inode *h_dir, *delegated;
32259 +       struct dentry *h_parent;
32260 +
32261 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
32262 +       h_dir = d_inode(h_parent);
32263 +       IMustLock(h_dir);
32264 +
32265 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32266 +       err = PTR_ERR(h_path.dentry);
32267 +       if (IS_ERR(h_path.dentry))
32268 +               goto out;
32269 +
32270 +       /* under the same dir, no need to lock_rename() */
32271 +       delegated = NULL;
32272 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
32273 +       AuTraceErr(err);
32274 +       if (unlikely(err == -EWOULDBLOCK)) {
32275 +               pr_warn("cannot retry for NFSv4 delegation"
32276 +                       " for an internal rename\n");
32277 +               iput(delegated);
32278 +       }
32279 +       dput(h_path.dentry);
32280 +
32281 +out:
32282 +       AuTraceErr(err);
32283 +       return err;
32284 +}
32285 +
32286 +/* ---------------------------------------------------------------------- */
32287 +/*
32288 + * functions for removing a whiteout
32289 + */
32290 +
32291 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32292 +{
32293 +       int err, force;
32294 +       struct inode *delegated;
32295 +
32296 +       /*
32297 +        * forces superio when the dir has a sticky bit.
32298 +        * this may be a violation of unix fs semantics.
32299 +        */
32300 +       force = (h_dir->i_mode & S_ISVTX)
32301 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
32302 +       delegated = NULL;
32303 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
32304 +       if (unlikely(err == -EWOULDBLOCK)) {
32305 +               pr_warn("cannot retry for NFSv4 delegation"
32306 +                       " for an internal unlink\n");
32307 +               iput(delegated);
32308 +       }
32309 +       return err;
32310 +}
32311 +
32312 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32313 +                       struct dentry *dentry)
32314 +{
32315 +       int err;
32316 +
32317 +       err = do_unlink_wh(h_dir, h_path);
32318 +       if (!err && dentry)
32319 +               au_set_dbwh(dentry, -1);
32320 +
32321 +       return err;
32322 +}
32323 +
32324 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32325 +                         struct au_branch *br)
32326 +{
32327 +       int err;
32328 +       struct path h_path = {
32329 +               .mnt = au_br_mnt(br)
32330 +       };
32331 +
32332 +       err = 0;
32333 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32334 +       if (IS_ERR(h_path.dentry))
32335 +               err = PTR_ERR(h_path.dentry);
32336 +       else {
32337 +               if (d_is_reg(h_path.dentry))
32338 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
32339 +               dput(h_path.dentry);
32340 +       }
32341 +
32342 +       return err;
32343 +}
32344 +
32345 +/* ---------------------------------------------------------------------- */
32346 +/*
32347 + * initialize/clean whiteout for a branch
32348 + */
32349 +
32350 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32351 +                       const int isdir)
32352 +{
32353 +       int err;
32354 +       struct inode *delegated;
32355 +
32356 +       if (d_is_negative(whpath->dentry))
32357 +               return;
32358 +
32359 +       if (isdir)
32360 +               err = vfsub_rmdir(h_dir, whpath);
32361 +       else {
32362 +               delegated = NULL;
32363 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32364 +               if (unlikely(err == -EWOULDBLOCK)) {
32365 +                       pr_warn("cannot retry for NFSv4 delegation"
32366 +                               " for an internal unlink\n");
32367 +                       iput(delegated);
32368 +               }
32369 +       }
32370 +       if (unlikely(err))
32371 +               pr_warn("failed removing %pd (%d), ignored.\n",
32372 +                       whpath->dentry, err);
32373 +}
32374 +
32375 +static int test_linkable(struct dentry *h_root)
32376 +{
32377 +       struct inode *h_dir = d_inode(h_root);
32378 +
32379 +       if (h_dir->i_op->link)
32380 +               return 0;
32381 +
32382 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32383 +              h_root, au_sbtype(h_root->d_sb));
32384 +       return -ENOSYS;
32385 +}
32386 +
32387 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32388 +static int au_whdir(struct inode *h_dir, struct path *path)
32389 +{
32390 +       int err;
32391 +
32392 +       err = -EEXIST;
32393 +       if (d_is_negative(path->dentry)) {
32394 +               int mode = S_IRWXU;
32395 +
32396 +               if (au_test_nfs(path->dentry->d_sb))
32397 +                       mode |= S_IXUGO;
32398 +               err = vfsub_mkdir(h_dir, path, mode);
32399 +       } else if (d_is_dir(path->dentry))
32400 +               err = 0;
32401 +       else
32402 +               pr_err("unknown %pd exists\n", path->dentry);
32403 +
32404 +       return err;
32405 +}
32406 +
32407 +struct au_wh_base {
32408 +       const struct qstr *name;
32409 +       struct dentry *dentry;
32410 +};
32411 +
32412 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32413 +                         struct path *h_path)
32414 +{
32415 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32416 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32417 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32418 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32419 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32420 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32421 +}
32422 +
32423 +/*
32424 + * returns tri-state,
32425 + * minus: error, caller should print the message
32426 + * zero: succuess
32427 + * plus: error, caller should NOT print the message
32428 + */
32429 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32430 +                               int do_plink, struct au_wh_base base[],
32431 +                               struct path *h_path)
32432 +{
32433 +       int err;
32434 +       struct inode *h_dir;
32435 +
32436 +       h_dir = d_inode(h_root);
32437 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32438 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32439 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32440 +       if (do_plink) {
32441 +               err = test_linkable(h_root);
32442 +               if (unlikely(err)) {
32443 +                       err = 1;
32444 +                       goto out;
32445 +               }
32446 +
32447 +               err = au_whdir(h_dir, h_path);
32448 +               if (unlikely(err))
32449 +                       goto out;
32450 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32451 +       } else
32452 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32453 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32454 +       err = au_whdir(h_dir, h_path);
32455 +       if (unlikely(err))
32456 +               goto out;
32457 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32458 +
32459 +out:
32460 +       return err;
32461 +}
32462 +
32463 +/*
32464 + * for the moment, aufs supports the branch filesystem which does not support
32465 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
32466 + * copyup failed. finally, such filesystem will not be used as the writable
32467 + * branch.
32468 + *
32469 + * returns tri-state, see above.
32470 + */
32471 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32472 +                        int do_plink, struct au_wh_base base[],
32473 +                        struct path *h_path)
32474 +{
32475 +       int err;
32476 +       struct inode *h_dir;
32477 +
32478 +       WbrWhMustWriteLock(wbr);
32479 +
32480 +       err = test_linkable(h_root);
32481 +       if (unlikely(err)) {
32482 +               err = 1;
32483 +               goto out;
32484 +       }
32485 +
32486 +       /*
32487 +        * todo: should this create be done in /sbin/mount.aufs helper?
32488 +        */
32489 +       err = -EEXIST;
32490 +       h_dir = d_inode(h_root);
32491 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
32492 +               h_path->dentry = base[AuBrWh_BASE].dentry;
32493 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
32494 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
32495 +               err = 0;
32496 +       else
32497 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
32498 +       if (unlikely(err))
32499 +               goto out;
32500 +
32501 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32502 +       if (do_plink) {
32503 +               err = au_whdir(h_dir, h_path);
32504 +               if (unlikely(err))
32505 +                       goto out;
32506 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32507 +       } else
32508 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32509 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32510 +
32511 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32512 +       err = au_whdir(h_dir, h_path);
32513 +       if (unlikely(err))
32514 +               goto out;
32515 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32516 +
32517 +out:
32518 +       return err;
32519 +}
32520 +
32521 +/*
32522 + * initialize the whiteout base file/dir for @br.
32523 + */
32524 +int au_wh_init(struct au_branch *br, struct super_block *sb)
32525 +{
32526 +       int err, i;
32527 +       const unsigned char do_plink
32528 +               = !!au_opt_test(au_mntflags(sb), PLINK);
32529 +       struct inode *h_dir;
32530 +       struct path path = br->br_path;
32531 +       struct dentry *h_root = path.dentry;
32532 +       struct au_wbr *wbr = br->br_wbr;
32533 +       static const struct qstr base_name[] = {
32534 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32535 +                                         sizeof(AUFS_BASE_NAME) - 1),
32536 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32537 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
32538 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32539 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
32540 +       };
32541 +       struct au_wh_base base[] = {
32542 +               [AuBrWh_BASE] = {
32543 +                       .name   = base_name + AuBrWh_BASE,
32544 +                       .dentry = NULL
32545 +               },
32546 +               [AuBrWh_PLINK] = {
32547 +                       .name   = base_name + AuBrWh_PLINK,
32548 +                       .dentry = NULL
32549 +               },
32550 +               [AuBrWh_ORPH] = {
32551 +                       .name   = base_name + AuBrWh_ORPH,
32552 +                       .dentry = NULL
32553 +               }
32554 +       };
32555 +
32556 +       if (wbr)
32557 +               WbrWhMustWriteLock(wbr);
32558 +
32559 +       for (i = 0; i < AuBrWh_Last; i++) {
32560 +               /* doubly whiteouted */
32561 +               struct dentry *d;
32562 +
32563 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
32564 +               err = PTR_ERR(d);
32565 +               if (IS_ERR(d))
32566 +                       goto out;
32567 +
32568 +               base[i].dentry = d;
32569 +               AuDebugOn(wbr
32570 +                         && wbr->wbr_wh[i]
32571 +                         && wbr->wbr_wh[i] != base[i].dentry);
32572 +       }
32573 +
32574 +       if (wbr)
32575 +               for (i = 0; i < AuBrWh_Last; i++) {
32576 +                       dput(wbr->wbr_wh[i]);
32577 +                       wbr->wbr_wh[i] = NULL;
32578 +               }
32579 +
32580 +       err = 0;
32581 +       if (!au_br_writable(br->br_perm)) {
32582 +               h_dir = d_inode(h_root);
32583 +               au_wh_init_ro(h_dir, base, &path);
32584 +       } else if (!au_br_wh_linkable(br->br_perm)) {
32585 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32586 +               if (err > 0)
32587 +                       goto out;
32588 +               else if (err)
32589 +                       goto out_err;
32590 +       } else {
32591 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32592 +               if (err > 0)
32593 +                       goto out;
32594 +               else if (err)
32595 +                       goto out_err;
32596 +       }
32597 +       goto out; /* success */
32598 +
32599 +out_err:
32600 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
32601 +              err, h_root, au_sbtype(h_root->d_sb));
32602 +out:
32603 +       for (i = 0; i < AuBrWh_Last; i++)
32604 +               dput(base[i].dentry);
32605 +       return err;
32606 +}
32607 +
32608 +/* ---------------------------------------------------------------------- */
32609 +/*
32610 + * whiteouts are all hard-linked usually.
32611 + * when its link count reaches a ceiling, we create a new whiteout base
32612 + * asynchronously.
32613 + */
32614 +
32615 +struct reinit_br_wh {
32616 +       struct super_block *sb;
32617 +       struct au_branch *br;
32618 +};
32619 +
32620 +static void reinit_br_wh(void *arg)
32621 +{
32622 +       int err;
32623 +       aufs_bindex_t bindex;
32624 +       struct path h_path;
32625 +       struct reinit_br_wh *a = arg;
32626 +       struct au_wbr *wbr;
32627 +       struct inode *dir, *delegated;
32628 +       struct dentry *h_root;
32629 +       struct au_hinode *hdir;
32630 +
32631 +       err = 0;
32632 +       wbr = a->br->br_wbr;
32633 +       /* big aufs lock */
32634 +       si_noflush_write_lock(a->sb);
32635 +       if (!au_br_writable(a->br->br_perm))
32636 +               goto out;
32637 +       bindex = au_br_index(a->sb, a->br->br_id);
32638 +       if (unlikely(bindex < 0))
32639 +               goto out;
32640 +
32641 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
32642 +       dir = d_inode(a->sb->s_root);
32643 +       hdir = au_hi(dir, bindex);
32644 +       h_root = au_h_dptr(a->sb->s_root, bindex);
32645 +       AuDebugOn(h_root != au_br_dentry(a->br));
32646 +
32647 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32648 +       wbr_wh_write_lock(wbr);
32649 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32650 +                         h_root, a->br);
32651 +       if (!err) {
32652 +               h_path.dentry = wbr->wbr_whbase;
32653 +               h_path.mnt = au_br_mnt(a->br);
32654 +               delegated = NULL;
32655 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32656 +                                  /*force*/0);
32657 +               if (unlikely(err == -EWOULDBLOCK)) {
32658 +                       pr_warn("cannot retry for NFSv4 delegation"
32659 +                               " for an internal unlink\n");
32660 +                       iput(delegated);
32661 +               }
32662 +       } else {
32663 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
32664 +               err = 0;
32665 +       }
32666 +       dput(wbr->wbr_whbase);
32667 +       wbr->wbr_whbase = NULL;
32668 +       if (!err)
32669 +               err = au_wh_init(a->br, a->sb);
32670 +       wbr_wh_write_unlock(wbr);
32671 +       au_hn_imtx_unlock(hdir);
32672 +       di_read_unlock(a->sb->s_root, AuLock_IR);
32673 +       if (!err)
32674 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
32675 +
32676 +out:
32677 +       if (wbr)
32678 +               atomic_dec(&wbr->wbr_wh_running);
32679 +       atomic_dec(&a->br->br_count);
32680 +       si_write_unlock(a->sb);
32681 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
32682 +       kfree(arg);
32683 +       if (unlikely(err))
32684 +               AuIOErr("err %d\n", err);
32685 +}
32686 +
32687 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32688 +{
32689 +       int do_dec, wkq_err;
32690 +       struct reinit_br_wh *arg;
32691 +
32692 +       do_dec = 1;
32693 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32694 +               goto out;
32695 +
32696 +       /* ignore ENOMEM */
32697 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
32698 +       if (arg) {
32699 +               /*
32700 +                * dec(wh_running), kfree(arg) and dec(br_count)
32701 +                * in reinit function
32702 +                */
32703 +               arg->sb = sb;
32704 +               arg->br = br;
32705 +               atomic_inc(&br->br_count);
32706 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
32707 +               if (unlikely(wkq_err)) {
32708 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
32709 +                       atomic_dec(&br->br_count);
32710 +                       kfree(arg);
32711 +               }
32712 +               do_dec = 0;
32713 +       }
32714 +
32715 +out:
32716 +       if (do_dec)
32717 +               atomic_dec(&br->br_wbr->wbr_wh_running);
32718 +}
32719 +
32720 +/* ---------------------------------------------------------------------- */
32721 +
32722 +/*
32723 + * create the whiteout @wh.
32724 + */
32725 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32726 +                            struct dentry *wh)
32727 +{
32728 +       int err;
32729 +       struct path h_path = {
32730 +               .dentry = wh
32731 +       };
32732 +       struct au_branch *br;
32733 +       struct au_wbr *wbr;
32734 +       struct dentry *h_parent;
32735 +       struct inode *h_dir, *delegated;
32736 +
32737 +       h_parent = wh->d_parent; /* dir inode is locked */
32738 +       h_dir = d_inode(h_parent);
32739 +       IMustLock(h_dir);
32740 +
32741 +       br = au_sbr(sb, bindex);
32742 +       h_path.mnt = au_br_mnt(br);
32743 +       wbr = br->br_wbr;
32744 +       wbr_wh_read_lock(wbr);
32745 +       if (wbr->wbr_whbase) {
32746 +               delegated = NULL;
32747 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
32748 +               if (unlikely(err == -EWOULDBLOCK)) {
32749 +                       pr_warn("cannot retry for NFSv4 delegation"
32750 +                               " for an internal link\n");
32751 +                       iput(delegated);
32752 +               }
32753 +               if (!err || err != -EMLINK)
32754 +                       goto out;
32755 +
32756 +               /* link count full. re-initialize br_whbase. */
32757 +               kick_reinit_br_wh(sb, br);
32758 +       }
32759 +
32760 +       /* return this error in this context */
32761 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
32762 +       if (!err)
32763 +               au_fhsm_wrote(sb, bindex, /*force*/0);
32764 +
32765 +out:
32766 +       wbr_wh_read_unlock(wbr);
32767 +       return err;
32768 +}
32769 +
32770 +/* ---------------------------------------------------------------------- */
32771 +
32772 +/*
32773 + * create or remove the diropq.
32774 + */
32775 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
32776 +                               unsigned int flags)
32777 +{
32778 +       struct dentry *opq_dentry, *h_dentry;
32779 +       struct super_block *sb;
32780 +       struct au_branch *br;
32781 +       int err;
32782 +
32783 +       sb = dentry->d_sb;
32784 +       br = au_sbr(sb, bindex);
32785 +       h_dentry = au_h_dptr(dentry, bindex);
32786 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
32787 +       if (IS_ERR(opq_dentry))
32788 +               goto out;
32789 +
32790 +       if (au_ftest_diropq(flags, CREATE)) {
32791 +               err = link_or_create_wh(sb, bindex, opq_dentry);
32792 +               if (!err) {
32793 +                       au_set_dbdiropq(dentry, bindex);
32794 +                       goto out; /* success */
32795 +               }
32796 +       } else {
32797 +               struct path tmp = {
32798 +                       .dentry = opq_dentry,
32799 +                       .mnt    = au_br_mnt(br)
32800 +               };
32801 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
32802 +               if (!err)
32803 +                       au_set_dbdiropq(dentry, -1);
32804 +       }
32805 +       dput(opq_dentry);
32806 +       opq_dentry = ERR_PTR(err);
32807 +
32808 +out:
32809 +       return opq_dentry;
32810 +}
32811 +
32812 +struct do_diropq_args {
32813 +       struct dentry **errp;
32814 +       struct dentry *dentry;
32815 +       aufs_bindex_t bindex;
32816 +       unsigned int flags;
32817 +};
32818 +
32819 +static void call_do_diropq(void *args)
32820 +{
32821 +       struct do_diropq_args *a = args;
32822 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32823 +}
32824 +
32825 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32826 +                            unsigned int flags)
32827 +{
32828 +       struct dentry *diropq, *h_dentry;
32829 +
32830 +       h_dentry = au_h_dptr(dentry, bindex);
32831 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
32832 +               diropq = do_diropq(dentry, bindex, flags);
32833 +       else {
32834 +               int wkq_err;
32835 +               struct do_diropq_args args = {
32836 +                       .errp           = &diropq,
32837 +                       .dentry         = dentry,
32838 +                       .bindex         = bindex,
32839 +                       .flags          = flags
32840 +               };
32841 +
32842 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
32843 +               if (unlikely(wkq_err))
32844 +                       diropq = ERR_PTR(wkq_err);
32845 +       }
32846 +
32847 +       return diropq;
32848 +}
32849 +
32850 +/* ---------------------------------------------------------------------- */
32851 +
32852 +/*
32853 + * lookup whiteout dentry.
32854 + * @h_parent: lower parent dentry which must exist and be locked
32855 + * @base_name: name of dentry which will be whiteouted
32856 + * returns dentry for whiteout.
32857 + */
32858 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32859 +                         struct au_branch *br)
32860 +{
32861 +       int err;
32862 +       struct qstr wh_name;
32863 +       struct dentry *wh_dentry;
32864 +
32865 +       err = au_wh_name_alloc(&wh_name, base_name);
32866 +       wh_dentry = ERR_PTR(err);
32867 +       if (!err) {
32868 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
32869 +               kfree(wh_name.name);
32870 +       }
32871 +       return wh_dentry;
32872 +}
32873 +
32874 +/*
32875 + * link/create a whiteout for @dentry on @bindex.
32876 + */
32877 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32878 +                           struct dentry *h_parent)
32879 +{
32880 +       struct dentry *wh_dentry;
32881 +       struct super_block *sb;
32882 +       int err;
32883 +
32884 +       sb = dentry->d_sb;
32885 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
32886 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
32887 +               err = link_or_create_wh(sb, bindex, wh_dentry);
32888 +               if (!err) {
32889 +                       au_set_dbwh(dentry, bindex);
32890 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
32891 +               } else {
32892 +                       dput(wh_dentry);
32893 +                       wh_dentry = ERR_PTR(err);
32894 +               }
32895 +       }
32896 +
32897 +       return wh_dentry;
32898 +}
32899 +
32900 +/* ---------------------------------------------------------------------- */
32901 +
32902 +/* Delete all whiteouts in this directory on branch bindex. */
32903 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32904 +                          aufs_bindex_t bindex, struct au_branch *br)
32905 +{
32906 +       int err;
32907 +       unsigned long ul, n;
32908 +       struct qstr wh_name;
32909 +       char *p;
32910 +       struct hlist_head *head;
32911 +       struct au_vdir_wh *pos;
32912 +       struct au_vdir_destr *str;
32913 +
32914 +       err = -ENOMEM;
32915 +       p = (void *)__get_free_page(GFP_NOFS);
32916 +       wh_name.name = p;
32917 +       if (unlikely(!wh_name.name))
32918 +               goto out;
32919 +
32920 +       err = 0;
32921 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32922 +       p += AUFS_WH_PFX_LEN;
32923 +       n = whlist->nh_num;
32924 +       head = whlist->nh_head;
32925 +       for (ul = 0; !err && ul < n; ul++, head++) {
32926 +               hlist_for_each_entry(pos, head, wh_hash) {
32927 +                       if (pos->wh_bindex != bindex)
32928 +                               continue;
32929 +
32930 +                       str = &pos->wh_str;
32931 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32932 +                               memcpy(p, str->name, str->len);
32933 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
32934 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
32935 +                               if (!err)
32936 +                                       continue;
32937 +                               break;
32938 +                       }
32939 +                       AuIOErr("whiteout name too long %.*s\n",
32940 +                               str->len, str->name);
32941 +                       err = -EIO;
32942 +                       break;
32943 +               }
32944 +       }
32945 +       free_page((unsigned long)wh_name.name);
32946 +
32947 +out:
32948 +       return err;
32949 +}
32950 +
32951 +struct del_wh_children_args {
32952 +       int *errp;
32953 +       struct dentry *h_dentry;
32954 +       struct au_nhash *whlist;
32955 +       aufs_bindex_t bindex;
32956 +       struct au_branch *br;
32957 +};
32958 +
32959 +static void call_del_wh_children(void *args)
32960 +{
32961 +       struct del_wh_children_args *a = args;
32962 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
32963 +}
32964 +
32965 +/* ---------------------------------------------------------------------- */
32966 +
32967 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
32968 +{
32969 +       struct au_whtmp_rmdir *whtmp;
32970 +       int err;
32971 +       unsigned int rdhash;
32972 +
32973 +       SiMustAnyLock(sb);
32974 +
32975 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
32976 +       if (unlikely(!whtmp)) {
32977 +               whtmp = ERR_PTR(-ENOMEM);
32978 +               goto out;
32979 +       }
32980 +
32981 +       /* no estimation for dir size */
32982 +       rdhash = au_sbi(sb)->si_rdhash;
32983 +       if (!rdhash)
32984 +               rdhash = AUFS_RDHASH_DEF;
32985 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
32986 +       if (unlikely(err)) {
32987 +               kfree(whtmp);
32988 +               whtmp = ERR_PTR(err);
32989 +       }
32990 +
32991 +out:
32992 +       return whtmp;
32993 +}
32994 +
32995 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
32996 +{
32997 +       if (whtmp->br)
32998 +               atomic_dec(&whtmp->br->br_count);
32999 +       dput(whtmp->wh_dentry);
33000 +       iput(whtmp->dir);
33001 +       au_nhash_wh_free(&whtmp->whlist);
33002 +       kfree(whtmp);
33003 +}
33004 +
33005 +/*
33006 + * rmdir the whiteouted temporary named dir @h_dentry.
33007 + * @whlist: whiteouted children.
33008 + */
33009 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33010 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
33011 +{
33012 +       int err;
33013 +       unsigned int h_nlink;
33014 +       struct path h_tmp;
33015 +       struct inode *wh_inode, *h_dir;
33016 +       struct au_branch *br;
33017 +
33018 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
33019 +       IMustLock(h_dir);
33020 +
33021 +       br = au_sbr(dir->i_sb, bindex);
33022 +       wh_inode = d_inode(wh_dentry);
33023 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
33024 +
33025 +       /*
33026 +        * someone else might change some whiteouts while we were sleeping.
33027 +        * it means this whlist may have an obsoleted entry.
33028 +        */
33029 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
33030 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
33031 +       else {
33032 +               int wkq_err;
33033 +               struct del_wh_children_args args = {
33034 +                       .errp           = &err,
33035 +                       .h_dentry       = wh_dentry,
33036 +                       .whlist         = whlist,
33037 +                       .bindex         = bindex,
33038 +                       .br             = br
33039 +               };
33040 +
33041 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
33042 +               if (unlikely(wkq_err))
33043 +                       err = wkq_err;
33044 +       }
33045 +       inode_unlock(wh_inode);
33046 +
33047 +       if (!err) {
33048 +               h_tmp.dentry = wh_dentry;
33049 +               h_tmp.mnt = au_br_mnt(br);
33050 +               h_nlink = h_dir->i_nlink;
33051 +               err = vfsub_rmdir(h_dir, &h_tmp);
33052 +               /* some fs doesn't change the parent nlink in some cases */
33053 +               h_nlink -= h_dir->i_nlink;
33054 +       }
33055 +
33056 +       if (!err) {
33057 +               if (au_ibstart(dir) == bindex) {
33058 +                       /* todo: dir->i_mutex is necessary */
33059 +                       au_cpup_attr_timesizes(dir);
33060 +                       if (h_nlink)
33061 +                               vfsub_drop_nlink(dir);
33062 +               }
33063 +               return 0; /* success */
33064 +       }
33065 +
33066 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
33067 +       return err;
33068 +}
33069 +
33070 +static void call_rmdir_whtmp(void *args)
33071 +{
33072 +       int err;
33073 +       aufs_bindex_t bindex;
33074 +       struct au_whtmp_rmdir *a = args;
33075 +       struct super_block *sb;
33076 +       struct dentry *h_parent;
33077 +       struct inode *h_dir;
33078 +       struct au_hinode *hdir;
33079 +
33080 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
33081 +       /* inode_lock(a->dir); */
33082 +       err = -EROFS;
33083 +       sb = a->dir->i_sb;
33084 +       si_read_lock(sb, !AuLock_FLUSH);
33085 +       if (!au_br_writable(a->br->br_perm))
33086 +               goto out;
33087 +       bindex = au_br_index(sb, a->br->br_id);
33088 +       if (unlikely(bindex < 0))
33089 +               goto out;
33090 +
33091 +       err = -EIO;
33092 +       ii_write_lock_parent(a->dir);
33093 +       h_parent = dget_parent(a->wh_dentry);
33094 +       h_dir = d_inode(h_parent);
33095 +       hdir = au_hi(a->dir, bindex);
33096 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
33097 +       if (unlikely(err))
33098 +               goto out_mnt;
33099 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
33100 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
33101 +                         a->br);
33102 +       if (!err)
33103 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
33104 +       au_hn_imtx_unlock(hdir);
33105 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
33106 +
33107 +out_mnt:
33108 +       dput(h_parent);
33109 +       ii_write_unlock(a->dir);
33110 +out:
33111 +       /* inode_unlock(a->dir); */
33112 +       au_whtmp_rmdir_free(a);
33113 +       si_read_unlock(sb);
33114 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33115 +       if (unlikely(err))
33116 +               AuIOErr("err %d\n", err);
33117 +}
33118 +
33119 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33120 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
33121 +{
33122 +       int wkq_err;
33123 +       struct super_block *sb;
33124 +
33125 +       IMustLock(dir);
33126 +
33127 +       /* all post-process will be done in do_rmdir_whtmp(). */
33128 +       sb = dir->i_sb;
33129 +       args->dir = au_igrab(dir);
33130 +       args->br = au_sbr(sb, bindex);
33131 +       atomic_inc(&args->br->br_count);
33132 +       args->wh_dentry = dget(wh_dentry);
33133 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
33134 +       if (unlikely(wkq_err)) {
33135 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
33136 +               au_whtmp_rmdir_free(args);
33137 +       }
33138 +}
33139 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33140 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
33141 +++ linux/fs/aufs/whout.h       2016-04-24 18:32:51.397020264 +0200
33142 @@ -0,0 +1,85 @@
33143 +/*
33144 + * Copyright (C) 2005-2016 Junjiro R. Okajima
33145 + *
33146 + * This program, aufs is free software; you can redistribute it and/or modify
33147 + * it under the terms of the GNU General Public License as published by
33148 + * the Free Software Foundation; either version 2 of the License, or
33149 + * (at your option) any later version.
33150 + *
33151 + * This program is distributed in the hope that it will be useful,
33152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33154 + * GNU General Public License for more details.
33155 + *
33156 + * You should have received a copy of the GNU General Public License
33157 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33158 + */
33159 +
33160 +/*
33161 + * whiteout for logical deletion and opaque directory
33162 + */
33163 +
33164 +#ifndef __AUFS_WHOUT_H__
33165 +#define __AUFS_WHOUT_H__
33166 +
33167 +#ifdef __KERNEL__
33168 +
33169 +#include "dir.h"
33170 +
33171 +/* whout.c */
33172 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
33173 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33174 +int au_diropq_test(struct dentry *h_dentry);
33175 +struct au_branch;
33176 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33177 +                            struct qstr *prefix);
33178 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33179 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33180 +                       struct dentry *dentry);
33181 +int au_wh_init(struct au_branch *br, struct super_block *sb);
33182 +
33183 +/* diropq flags */
33184 +#define AuDiropq_CREATE        1
33185 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
33186 +#define au_fset_diropq(flags, name) \
33187 +       do { (flags) |= AuDiropq_##name; } while (0)
33188 +#define au_fclr_diropq(flags, name) \
33189 +       do { (flags) &= ~AuDiropq_##name; } while (0)
33190 +
33191 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33192 +                            unsigned int flags);
33193 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33194 +                         struct au_branch *br);
33195 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33196 +                           struct dentry *h_parent);
33197 +
33198 +/* real rmdir for the whiteout-ed dir */
33199 +struct au_whtmp_rmdir {
33200 +       struct inode *dir;
33201 +       struct au_branch *br;
33202 +       struct dentry *wh_dentry;
33203 +       struct au_nhash whlist;
33204 +};
33205 +
33206 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33207 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33208 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33209 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
33210 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33211 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33212 +
33213 +/* ---------------------------------------------------------------------- */
33214 +
33215 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
33216 +                                             aufs_bindex_t bindex)
33217 +{
33218 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33219 +}
33220 +
33221 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33222 +{
33223 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33224 +}
33225 +
33226 +#endif /* __KERNEL__ */
33227 +#endif /* __AUFS_WHOUT_H__ */
33228 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33229 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
33230 +++ linux/fs/aufs/wkq.c 2016-04-24 18:32:51.397020264 +0200
33231 @@ -0,0 +1,213 @@
33232 +/*
33233 + * Copyright (C) 2005-2016 Junjiro R. Okajima
33234 + *
33235 + * This program, aufs is free software; you can redistribute it and/or modify
33236 + * it under the terms of the GNU General Public License as published by
33237 + * the Free Software Foundation; either version 2 of the License, or
33238 + * (at your option) any later version.
33239 + *
33240 + * This program is distributed in the hope that it will be useful,
33241 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33242 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33243 + * GNU General Public License for more details.
33244 + *
33245 + * You should have received a copy of the GNU General Public License
33246 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33247 + */
33248 +
33249 +/*
33250 + * workqueue for asynchronous/super-io operations
33251 + * todo: try new dredential scheme
33252 + */
33253 +
33254 +#include <linux/module.h>
33255 +#include "aufs.h"
33256 +
33257 +/* internal workqueue named AUFS_WKQ_NAME */
33258 +
33259 +static struct workqueue_struct *au_wkq;
33260 +
33261 +struct au_wkinfo {
33262 +       struct work_struct wk;
33263 +       struct kobject *kobj;
33264 +
33265 +       unsigned int flags; /* see wkq.h */
33266 +
33267 +       au_wkq_func_t func;
33268 +       void *args;
33269 +
33270 +       struct completion *comp;
33271 +};
33272 +
33273 +/* ---------------------------------------------------------------------- */
33274 +
33275 +static void wkq_func(struct work_struct *wk)
33276 +{
33277 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33278 +
33279 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
33280 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33281 +
33282 +       wkinfo->func(wkinfo->args);
33283 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
33284 +               complete(wkinfo->comp);
33285 +       else {
33286 +               kobject_put(wkinfo->kobj);
33287 +               module_put(THIS_MODULE); /* todo: ?? */
33288 +               kfree(wkinfo);
33289 +       }
33290 +}
33291 +
33292 +/*
33293 + * Since struct completion is large, try allocating it dynamically.
33294 + */
33295 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
33296 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
33297 +
33298 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33299 +{
33300 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33301 +       if (*comp) {
33302 +               init_completion(*comp);
33303 +               wkinfo->comp = *comp;
33304 +               return 0;
33305 +       }
33306 +       return -ENOMEM;
33307 +}
33308 +
33309 +static void au_wkq_comp_free(struct completion *comp)
33310 +{
33311 +       kfree(comp);
33312 +}
33313 +
33314 +#else
33315 +
33316 +/* no braces */
33317 +#define AuWkqCompDeclare(name) \
33318 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33319 +       struct completion *comp = &_ ## name
33320 +
33321 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33322 +{
33323 +       wkinfo->comp = *comp;
33324 +       return 0;
33325 +}
33326 +
33327 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33328 +{
33329 +       /* empty */
33330 +}
33331 +#endif /* 4KSTACKS */
33332 +
33333 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33334 +{
33335 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33336 +               if (au_wkq_test()) {
33337 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33338 +                               " due to a dead dir by UDBA?\n");
33339 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33340 +               }
33341 +       } else
33342 +               au_dbg_verify_kthread();
33343 +
33344 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33345 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33346 +               queue_work(au_wkq, &wkinfo->wk);
33347 +       } else {
33348 +               INIT_WORK(&wkinfo->wk, wkq_func);
33349 +               schedule_work(&wkinfo->wk);
33350 +       }
33351 +}
33352 +
33353 +/*
33354 + * Be careful. It is easy to make deadlock happen.
33355 + * processA: lock, wkq and wait
33356 + * processB: wkq and wait, lock in wkq
33357 + * --> deadlock
33358 + */
33359 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33360 +{
33361 +       int err;
33362 +       AuWkqCompDeclare(comp);
33363 +       struct au_wkinfo wkinfo = {
33364 +               .flags  = flags,
33365 +               .func   = func,
33366 +               .args   = args
33367 +       };
33368 +
33369 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33370 +       if (!err) {
33371 +               au_wkq_run(&wkinfo);
33372 +               /* no timeout, no interrupt */
33373 +               wait_for_completion(wkinfo.comp);
33374 +               au_wkq_comp_free(comp);
33375 +               destroy_work_on_stack(&wkinfo.wk);
33376 +       }
33377 +
33378 +       return err;
33379 +
33380 +}
33381 +
33382 +/*
33383 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33384 + * problem in a concurrent umounting.
33385 + */
33386 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33387 +                 unsigned int flags)
33388 +{
33389 +       int err;
33390 +       struct au_wkinfo *wkinfo;
33391 +
33392 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33393 +
33394 +       /*
33395 +        * wkq_func() must free this wkinfo.
33396 +        * it highly depends upon the implementation of workqueue.
33397 +        */
33398 +       err = 0;
33399 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33400 +       if (wkinfo) {
33401 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33402 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33403 +               wkinfo->func = func;
33404 +               wkinfo->args = args;
33405 +               wkinfo->comp = NULL;
33406 +               kobject_get(wkinfo->kobj);
33407 +               __module_get(THIS_MODULE); /* todo: ?? */
33408 +
33409 +               au_wkq_run(wkinfo);
33410 +       } else {
33411 +               err = -ENOMEM;
33412 +               au_nwt_done(&au_sbi(sb)->si_nowait);
33413 +       }
33414 +
33415 +       return err;
33416 +}
33417 +
33418 +/* ---------------------------------------------------------------------- */
33419 +
33420 +void au_nwt_init(struct au_nowait_tasks *nwt)
33421 +{
33422 +       atomic_set(&nwt->nw_len, 0);
33423 +       /* smp_mb(); */ /* atomic_set */
33424 +       init_waitqueue_head(&nwt->nw_wq);
33425 +}
33426 +
33427 +void au_wkq_fin(void)
33428 +{
33429 +       destroy_workqueue(au_wkq);
33430 +}
33431 +
33432 +int __init au_wkq_init(void)
33433 +{
33434 +       int err;
33435 +
33436 +       err = 0;
33437 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
33438 +       if (IS_ERR(au_wkq))
33439 +               err = PTR_ERR(au_wkq);
33440 +       else if (!au_wkq)
33441 +               err = -ENOMEM;
33442 +
33443 +       return err;
33444 +}
33445 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33446 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
33447 +++ linux/fs/aufs/wkq.h 2016-04-24 18:32:51.397020264 +0200
33448 @@ -0,0 +1,91 @@
33449 +/*
33450 + * Copyright (C) 2005-2016 Junjiro R. Okajima
33451 + *
33452 + * This program, aufs is free software; you can redistribute it and/or modify
33453 + * it under the terms of the GNU General Public License as published by
33454 + * the Free Software Foundation; either version 2 of the License, or
33455 + * (at your option) any later version.
33456 + *
33457 + * This program is distributed in the hope that it will be useful,
33458 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33459 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33460 + * GNU General Public License for more details.
33461 + *
33462 + * You should have received a copy of the GNU General Public License
33463 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33464 + */
33465 +
33466 +/*
33467 + * workqueue for asynchronous/super-io operations
33468 + * todo: try new credentials management scheme
33469 + */
33470 +
33471 +#ifndef __AUFS_WKQ_H__
33472 +#define __AUFS_WKQ_H__
33473 +
33474 +#ifdef __KERNEL__
33475 +
33476 +struct super_block;
33477 +
33478 +/* ---------------------------------------------------------------------- */
33479 +
33480 +/*
33481 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33482 + */
33483 +struct au_nowait_tasks {
33484 +       atomic_t                nw_len;
33485 +       wait_queue_head_t       nw_wq;
33486 +};
33487 +
33488 +/* ---------------------------------------------------------------------- */
33489 +
33490 +typedef void (*au_wkq_func_t)(void *args);
33491 +
33492 +/* wkq flags */
33493 +#define AuWkq_WAIT     1
33494 +#define AuWkq_NEST     (1 << 1)
33495 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
33496 +#define au_fset_wkq(flags, name) \
33497 +       do { (flags) |= AuWkq_##name; } while (0)
33498 +#define au_fclr_wkq(flags, name) \
33499 +       do { (flags) &= ~AuWkq_##name; } while (0)
33500 +
33501 +#ifndef CONFIG_AUFS_HNOTIFY
33502 +#undef AuWkq_NEST
33503 +#define AuWkq_NEST     0
33504 +#endif
33505 +
33506 +/* wkq.c */
33507 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
33508 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33509 +                 unsigned int flags);
33510 +void au_nwt_init(struct au_nowait_tasks *nwt);
33511 +int __init au_wkq_init(void);
33512 +void au_wkq_fin(void);
33513 +
33514 +/* ---------------------------------------------------------------------- */
33515 +
33516 +static inline int au_wkq_test(void)
33517 +{
33518 +       return current->flags & PF_WQ_WORKER;
33519 +}
33520 +
33521 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
33522 +{
33523 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
33524 +}
33525 +
33526 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33527 +{
33528 +       if (atomic_dec_and_test(&nwt->nw_len))
33529 +               wake_up_all(&nwt->nw_wq);
33530 +}
33531 +
33532 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33533 +{
33534 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
33535 +       return 0;
33536 +}
33537 +
33538 +#endif /* __KERNEL__ */
33539 +#endif /* __AUFS_WKQ_H__ */
33540 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33541 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
33542 +++ linux/fs/aufs/xattr.c       2016-04-24 18:32:51.397020264 +0200
33543 @@ -0,0 +1,344 @@
33544 +/*
33545 + * Copyright (C) 2014-2016 Junjiro R. Okajima
33546 + *
33547 + * This program, aufs is free software; you can redistribute it and/or modify
33548 + * it under the terms of the GNU General Public License as published by
33549 + * the Free Software Foundation; either version 2 of the License, or
33550 + * (at your option) any later version.
33551 + *
33552 + * This program is distributed in the hope that it will be useful,
33553 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33554 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33555 + * GNU General Public License for more details.
33556 + *
33557 + * You should have received a copy of the GNU General Public License
33558 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33559 + */
33560 +
33561 +/*
33562 + * handling xattr functions
33563 + */
33564 +
33565 +#include <linux/xattr.h>
33566 +#include "aufs.h"
33567 +
33568 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33569 +{
33570 +       if (!ignore_flags)
33571 +               goto out;
33572 +       switch (err) {
33573 +       case -ENOMEM:
33574 +       case -EDQUOT:
33575 +               goto out;
33576 +       }
33577 +
33578 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33579 +               err = 0;
33580 +               goto out;
33581 +       }
33582 +
33583 +#define cmp(brattr, prefix) do {                                       \
33584 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
33585 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
33586 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
33587 +                               err = 0;                                \
33588 +                       goto out;                                       \
33589 +               }                                                       \
33590 +       } while (0)
33591 +
33592 +       cmp(SEC, SECURITY);
33593 +       cmp(SYS, SYSTEM);
33594 +       cmp(TR, TRUSTED);
33595 +       cmp(USR, USER);
33596 +#undef cmp
33597 +
33598 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
33599 +               err = 0;
33600 +
33601 +out:
33602 +       return err;
33603 +}
33604 +
33605 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33606 +
33607 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
33608 +                           char *name, char **buf, unsigned int ignore_flags,
33609 +                           unsigned int verbose)
33610 +{
33611 +       int err;
33612 +       ssize_t ssz;
33613 +       struct inode *h_idst;
33614 +
33615 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33616 +       err = ssz;
33617 +       if (unlikely(err <= 0)) {
33618 +               if (err == -ENODATA
33619 +                   || (err == -EOPNOTSUPP
33620 +                       && ((ignore_flags & au_xattr_out_of_list)
33621 +                           || (au_test_nfs_noacl(d_inode(h_src))
33622 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33623 +                                   || !strcmp(name,
33624 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
33625 +                           ))
33626 +                       err = 0;
33627 +               if (err && (verbose || au_debug_test()))
33628 +                       pr_err("%s, err %d\n", name, err);
33629 +               goto out;
33630 +       }
33631 +
33632 +       /* unlock it temporary */
33633 +       h_idst = d_inode(h_dst);
33634 +       inode_unlock(h_idst);
33635 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
33636 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
33637 +       if (unlikely(err)) {
33638 +               if (verbose || au_debug_test())
33639 +                       pr_err("%s, err %d\n", name, err);
33640 +               err = au_xattr_ignore(err, name, ignore_flags);
33641 +       }
33642 +
33643 +out:
33644 +       return err;
33645 +}
33646 +
33647 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33648 +                 unsigned int verbose)
33649 +{
33650 +       int err, unlocked, acl_access, acl_default;
33651 +       ssize_t ssz;
33652 +       struct inode *h_isrc, *h_idst;
33653 +       char *value, *p, *o, *e;
33654 +
33655 +       /* try stopping to update the source inode while we are referencing */
33656 +       /* there should not be the parent-child relationship between them */
33657 +       h_isrc = d_inode(h_src);
33658 +       h_idst = d_inode(h_dst);
33659 +       inode_unlock(h_idst);
33660 +       inode_lock_nested(h_isrc, AuLsc_I_CHILD);
33661 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
33662 +       unlocked = 0;
33663 +
33664 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
33665 +       ssz = vfs_listxattr(h_src, NULL, 0);
33666 +       err = ssz;
33667 +       if (unlikely(err < 0)) {
33668 +               AuTraceErr(err);
33669 +               if (err == -ENODATA
33670 +                   || err == -EOPNOTSUPP)
33671 +                       err = 0;        /* ignore */
33672 +               goto out;
33673 +       }
33674 +
33675 +       err = 0;
33676 +       p = NULL;
33677 +       o = NULL;
33678 +       if (ssz) {
33679 +               err = -ENOMEM;
33680 +               p = kmalloc(ssz, GFP_NOFS);
33681 +               o = p;
33682 +               if (unlikely(!p))
33683 +                       goto out;
33684 +               err = vfs_listxattr(h_src, p, ssz);
33685 +       }
33686 +       inode_unlock(h_isrc);
33687 +       unlocked = 1;
33688 +       AuDbg("err %d, ssz %zd\n", err, ssz);
33689 +       if (unlikely(err < 0))
33690 +               goto out_free;
33691 +
33692 +       err = 0;
33693 +       e = p + ssz;
33694 +       value = NULL;
33695 +       acl_access = 0;
33696 +       acl_default = 0;
33697 +       while (!err && p < e) {
33698 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33699 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33700 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33701 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33702 +                                       - 1);
33703 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33704 +                                      verbose);
33705 +               p += strlen(p) + 1;
33706 +       }
33707 +       AuTraceErr(err);
33708 +       ignore_flags |= au_xattr_out_of_list;
33709 +       if (!err && !acl_access) {
33710 +               err = au_do_cpup_xattr(h_dst, h_src,
33711 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
33712 +                                      ignore_flags, verbose);
33713 +               AuTraceErr(err);
33714 +       }
33715 +       if (!err && !acl_default) {
33716 +               err = au_do_cpup_xattr(h_dst, h_src,
33717 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
33718 +                                      ignore_flags, verbose);
33719 +               AuTraceErr(err);
33720 +       }
33721 +
33722 +       kfree(value);
33723 +
33724 +out_free:
33725 +       kfree(o);
33726 +out:
33727 +       if (!unlocked)
33728 +               inode_unlock(h_isrc);
33729 +       AuTraceErr(err);
33730 +       return err;
33731 +}
33732 +
33733 +/* ---------------------------------------------------------------------- */
33734 +
33735 +enum {
33736 +       AU_XATTR_LIST,
33737 +       AU_XATTR_GET
33738 +};
33739 +
33740 +struct au_lgxattr {
33741 +       int type;
33742 +       union {
33743 +               struct {
33744 +                       char    *list;
33745 +                       size_t  size;
33746 +               } list;
33747 +               struct {
33748 +                       const char      *name;
33749 +                       void            *value;
33750 +                       size_t          size;
33751 +               } get;
33752 +       } u;
33753 +};
33754 +
33755 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
33756 +{
33757 +       ssize_t err;
33758 +       struct path h_path;
33759 +       struct super_block *sb;
33760 +
33761 +       sb = dentry->d_sb;
33762 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
33763 +       if (unlikely(err))
33764 +               goto out;
33765 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path);
33766 +       if (unlikely(err))
33767 +               goto out_si;
33768 +       if (unlikely(!h_path.dentry))
33769 +               /* illegally overlapped or something */
33770 +               goto out_di; /* pretending success */
33771 +
33772 +       /* always topmost entry only */
33773 +       switch (arg->type) {
33774 +       case AU_XATTR_LIST:
33775 +               err = vfs_listxattr(h_path.dentry,
33776 +                                   arg->u.list.list, arg->u.list.size);
33777 +               break;
33778 +       case AU_XATTR_GET:
33779 +               err = vfs_getxattr(h_path.dentry,
33780 +                                  arg->u.get.name, arg->u.get.value,
33781 +                                  arg->u.get.size);
33782 +               break;
33783 +       }
33784 +
33785 +out_di:
33786 +       di_read_unlock(dentry, AuLock_IR);
33787 +out_si:
33788 +       si_read_unlock(sb);
33789 +out:
33790 +       AuTraceErr(err);
33791 +       return err;
33792 +}
33793 +
33794 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
33795 +{
33796 +       struct au_lgxattr arg = {
33797 +               .type = AU_XATTR_LIST,
33798 +               .u.list = {
33799 +                       .list   = list,
33800 +                       .size   = size
33801 +               },
33802 +       };
33803 +
33804 +       return au_lgxattr(dentry, &arg);
33805 +}
33806 +
33807 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
33808 +                     size_t size)
33809 +{
33810 +       struct au_lgxattr arg = {
33811 +               .type = AU_XATTR_GET,
33812 +               .u.get = {
33813 +                       .name   = name,
33814 +                       .value  = value,
33815 +                       .size   = size
33816 +               },
33817 +       };
33818 +
33819 +       return au_lgxattr(dentry, &arg);
33820 +}
33821 +
33822 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
33823 +                 size_t size, int flags)
33824 +{
33825 +       struct au_srxattr arg = {
33826 +               .type = AU_XATTR_SET,
33827 +               .u.set = {
33828 +                       .name   = name,
33829 +                       .value  = value,
33830 +                       .size   = size,
33831 +                       .flags  = flags
33832 +               },
33833 +       };
33834 +
33835 +       return au_srxattr(dentry, &arg);
33836 +}
33837 +
33838 +int aufs_removexattr(struct dentry *dentry, const char *name)
33839 +{
33840 +       struct au_srxattr arg = {
33841 +               .type = AU_XATTR_REMOVE,
33842 +               .u.remove = {
33843 +                       .name   = name
33844 +               },
33845 +       };
33846 +
33847 +       return au_srxattr(dentry, &arg);
33848 +}
33849 +
33850 +/* ---------------------------------------------------------------------- */
33851 +
33852 +#if 0
33853 +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33854 +                           const char *name, size_t name_len, int type)
33855 +{
33856 +       return aufs_listxattr(dentry, list, list_size);
33857 +}
33858 +
33859 +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33860 +                       size_t size, int type)
33861 +{
33862 +       return aufs_getxattr(dentry, name, buffer, size);
33863 +}
33864 +
33865 +static int au_xattr_set(struct dentry *dentry, const char *name,
33866 +                       const void *value, size_t size, int flags, int type)
33867 +{
33868 +       return aufs_setxattr(dentry, name, value, size, flags);
33869 +}
33870 +
33871 +static const struct xattr_handler au_xattr_handler = {
33872 +       /* no prefix, no flags */
33873 +       .list   = au_xattr_list,
33874 +       .get    = au_xattr_get,
33875 +       .set    = au_xattr_set
33876 +       /* why no remove? */
33877 +};
33878 +
33879 +static const struct xattr_handler *au_xattr_handlers[] = {
33880 +       &au_xattr_handler
33881 +};
33882 +
33883 +void au_xattr_init(struct super_block *sb)
33884 +{
33885 +       /* sb->s_xattr = au_xattr_handlers; */
33886 +}
33887 +#endif
33888 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33889 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
33890 +++ linux/fs/aufs/xino.c        2016-04-24 18:32:51.397020264 +0200
33891 @@ -0,0 +1,1318 @@
33892 +/*
33893 + * Copyright (C) 2005-2016 Junjiro R. Okajima
33894 + *
33895 + * This program, aufs is free software; you can redistribute it and/or modify
33896 + * it under the terms of the GNU General Public License as published by
33897 + * the Free Software Foundation; either version 2 of the License, or
33898 + * (at your option) any later version.
33899 + *
33900 + * This program is distributed in the hope that it will be useful,
33901 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33902 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33903 + * GNU General Public License for more details.
33904 + *
33905 + * You should have received a copy of the GNU General Public License
33906 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33907 + */
33908 +
33909 +/*
33910 + * external inode number translation table and bitmap
33911 + */
33912 +
33913 +#include <linux/seq_file.h>
33914 +#include <linux/statfs.h>
33915 +#include "aufs.h"
33916 +
33917 +/* todo: unnecessary to support mmap_sem since kernel-space? */
33918 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
33919 +                  loff_t *pos)
33920 +{
33921 +       ssize_t err;
33922 +       mm_segment_t oldfs;
33923 +       union {
33924 +               void *k;
33925 +               char __user *u;
33926 +       } buf;
33927 +
33928 +       buf.k = kbuf;
33929 +       oldfs = get_fs();
33930 +       set_fs(KERNEL_DS);
33931 +       do {
33932 +               /* todo: signal_pending? */
33933 +               err = func(file, buf.u, size, pos);
33934 +       } while (err == -EAGAIN || err == -EINTR);
33935 +       set_fs(oldfs);
33936 +
33937 +#if 0 /* reserved for future use */
33938 +       if (err > 0)
33939 +               fsnotify_access(file->f_path.dentry);
33940 +#endif
33941 +
33942 +       return err;
33943 +}
33944 +
33945 +/* ---------------------------------------------------------------------- */
33946 +
33947 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
33948 +                              size_t size, loff_t *pos);
33949 +
33950 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
33951 +                             size_t size, loff_t *pos)
33952 +{
33953 +       ssize_t err;
33954 +       mm_segment_t oldfs;
33955 +       union {
33956 +               void *k;
33957 +               const char __user *u;
33958 +       } buf;
33959 +       int i;
33960 +       const int prevent_endless = 10;
33961 +
33962 +       i = 0;
33963 +       buf.k = kbuf;
33964 +       oldfs = get_fs();
33965 +       set_fs(KERNEL_DS);
33966 +       do {
33967 +               err = func(file, buf.u, size, pos);
33968 +               if (err == -EINTR
33969 +                   && !au_wkq_test()
33970 +                   && fatal_signal_pending(current)) {
33971 +                       set_fs(oldfs);
33972 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
33973 +                       BUG_ON(err == -EINTR);
33974 +                       oldfs = get_fs();
33975 +                       set_fs(KERNEL_DS);
33976 +               }
33977 +       } while (i++ < prevent_endless
33978 +                && (err == -EAGAIN || err == -EINTR));
33979 +       set_fs(oldfs);
33980 +
33981 +#if 0 /* reserved for future use */
33982 +       if (err > 0)
33983 +               fsnotify_modify(file->f_path.dentry);
33984 +#endif
33985 +
33986 +       return err;
33987 +}
33988 +
33989 +struct do_xino_fwrite_args {
33990 +       ssize_t *errp;
33991 +       vfs_writef_t func;
33992 +       struct file *file;
33993 +       void *buf;
33994 +       size_t size;
33995 +       loff_t *pos;
33996 +};
33997 +
33998 +static void call_do_xino_fwrite(void *args)
33999 +{
34000 +       struct do_xino_fwrite_args *a = args;
34001 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
34002 +}
34003 +
34004 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34005 +                              size_t size, loff_t *pos)
34006 +{
34007 +       ssize_t err;
34008 +       int wkq_err;
34009 +       struct do_xino_fwrite_args args = {
34010 +               .errp   = &err,
34011 +               .func   = func,
34012 +               .file   = file,
34013 +               .buf    = buf,
34014 +               .size   = size,
34015 +               .pos    = pos
34016 +       };
34017 +
34018 +       /*
34019 +        * it breaks RLIMIT_FSIZE and normal user's limit,
34020 +        * users should care about quota and real 'filesystem full.'
34021 +        */
34022 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
34023 +       if (unlikely(wkq_err))
34024 +               err = wkq_err;
34025 +
34026 +       return err;
34027 +}
34028 +
34029 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
34030 +                   size_t size, loff_t *pos)
34031 +{
34032 +       ssize_t err;
34033 +
34034 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
34035 +               lockdep_off();
34036 +               err = do_xino_fwrite(func, file, buf, size, pos);
34037 +               lockdep_on();
34038 +       } else
34039 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
34040 +
34041 +       return err;
34042 +}
34043 +
34044 +/* ---------------------------------------------------------------------- */
34045 +
34046 +/*
34047 + * create a new xinofile at the same place/path as @base_file.
34048 + */
34049 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
34050 +{
34051 +       struct file *file;
34052 +       struct dentry *base, *parent;
34053 +       struct inode *dir, *delegated;
34054 +       struct qstr *name;
34055 +       struct path path;
34056 +       int err;
34057 +
34058 +       base = base_file->f_path.dentry;
34059 +       parent = base->d_parent; /* dir inode is locked */
34060 +       dir = d_inode(parent);
34061 +       IMustLock(dir);
34062 +
34063 +       file = ERR_PTR(-EINVAL);
34064 +       name = &base->d_name;
34065 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
34066 +       if (IS_ERR(path.dentry)) {
34067 +               file = (void *)path.dentry;
34068 +               pr_err("%pd lookup err %ld\n",
34069 +                      base, PTR_ERR(path.dentry));
34070 +               goto out;
34071 +       }
34072 +
34073 +       /* no need to mnt_want_write() since we call dentry_open() later */
34074 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
34075 +       if (unlikely(err)) {
34076 +               file = ERR_PTR(err);
34077 +               pr_err("%pd create err %d\n", base, err);
34078 +               goto out_dput;
34079 +       }
34080 +
34081 +       path.mnt = base_file->f_path.mnt;
34082 +       file = vfsub_dentry_open(&path,
34083 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34084 +                                /* | __FMODE_NONOTIFY */);
34085 +       if (IS_ERR(file)) {
34086 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
34087 +               goto out_dput;
34088 +       }
34089 +
34090 +       delegated = NULL;
34091 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
34092 +       if (unlikely(err == -EWOULDBLOCK)) {
34093 +               pr_warn("cannot retry for NFSv4 delegation"
34094 +                       " for an internal unlink\n");
34095 +               iput(delegated);
34096 +       }
34097 +       if (unlikely(err)) {
34098 +               pr_err("%pd unlink err %d\n", base, err);
34099 +               goto out_fput;
34100 +       }
34101 +
34102 +       if (copy_src) {
34103 +               /* no one can touch copy_src xino */
34104 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
34105 +               if (unlikely(err)) {
34106 +                       pr_err("%pd copy err %d\n", base, err);
34107 +                       goto out_fput;
34108 +               }
34109 +       }
34110 +       goto out_dput; /* success */
34111 +
34112 +out_fput:
34113 +       fput(file);
34114 +       file = ERR_PTR(err);
34115 +out_dput:
34116 +       dput(path.dentry);
34117 +out:
34118 +       return file;
34119 +}
34120 +
34121 +struct au_xino_lock_dir {
34122 +       struct au_hinode *hdir;
34123 +       struct dentry *parent;
34124 +       struct inode *dir;
34125 +};
34126 +
34127 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
34128 +                            struct au_xino_lock_dir *ldir)
34129 +{
34130 +       aufs_bindex_t brid, bindex;
34131 +
34132 +       ldir->hdir = NULL;
34133 +       bindex = -1;
34134 +       brid = au_xino_brid(sb);
34135 +       if (brid >= 0)
34136 +               bindex = au_br_index(sb, brid);
34137 +       if (bindex >= 0) {
34138 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
34139 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
34140 +       } else {
34141 +               ldir->parent = dget_parent(xino->f_path.dentry);
34142 +               ldir->dir = d_inode(ldir->parent);
34143 +               inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
34144 +       }
34145 +}
34146 +
34147 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34148 +{
34149 +       if (ldir->hdir)
34150 +               au_hn_imtx_unlock(ldir->hdir);
34151 +       else {
34152 +               inode_unlock(ldir->dir);
34153 +               dput(ldir->parent);
34154 +       }
34155 +}
34156 +
34157 +/* ---------------------------------------------------------------------- */
34158 +
34159 +/* trucate xino files asynchronously */
34160 +
34161 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34162 +{
34163 +       int err;
34164 +       unsigned long jiffy;
34165 +       blkcnt_t blocks;
34166 +       aufs_bindex_t bi, bend;
34167 +       struct kstatfs *st;
34168 +       struct au_branch *br;
34169 +       struct file *new_xino, *file;
34170 +       struct super_block *h_sb;
34171 +       struct au_xino_lock_dir ldir;
34172 +
34173 +       err = -ENOMEM;
34174 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34175 +       if (unlikely(!st))
34176 +               goto out;
34177 +
34178 +       err = -EINVAL;
34179 +       bend = au_sbend(sb);
34180 +       if (unlikely(bindex < 0 || bend < bindex))
34181 +               goto out_st;
34182 +       br = au_sbr(sb, bindex);
34183 +       file = br->br_xino.xi_file;
34184 +       if (!file)
34185 +               goto out_st;
34186 +
34187 +       err = vfs_statfs(&file->f_path, st);
34188 +       if (unlikely(err))
34189 +               AuErr1("statfs err %d, ignored\n", err);
34190 +       jiffy = jiffies;
34191 +       blocks = file_inode(file)->i_blocks;
34192 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34193 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
34194 +
34195 +       au_xino_lock_dir(sb, file, &ldir);
34196 +       /* mnt_want_write() is unnecessary here */
34197 +       new_xino = au_xino_create2(file, file);
34198 +       au_xino_unlock_dir(&ldir);
34199 +       err = PTR_ERR(new_xino);
34200 +       if (IS_ERR(new_xino)) {
34201 +               pr_err("err %d, ignored\n", err);
34202 +               goto out_st;
34203 +       }
34204 +       err = 0;
34205 +       fput(file);
34206 +       br->br_xino.xi_file = new_xino;
34207 +
34208 +       h_sb = au_br_sb(br);
34209 +       for (bi = 0; bi <= bend; bi++) {
34210 +               if (unlikely(bi == bindex))
34211 +                       continue;
34212 +               br = au_sbr(sb, bi);
34213 +               if (au_br_sb(br) != h_sb)
34214 +                       continue;
34215 +
34216 +               fput(br->br_xino.xi_file);
34217 +               br->br_xino.xi_file = new_xino;
34218 +               get_file(new_xino);
34219 +       }
34220 +
34221 +       err = vfs_statfs(&new_xino->f_path, st);
34222 +       if (!err) {
34223 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34224 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
34225 +                       st->f_bfree, st->f_blocks);
34226 +               if (file_inode(new_xino)->i_blocks < blocks)
34227 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
34228 +       } else
34229 +               AuErr1("statfs err %d, ignored\n", err);
34230 +
34231 +out_st:
34232 +       kfree(st);
34233 +out:
34234 +       return err;
34235 +}
34236 +
34237 +struct xino_do_trunc_args {
34238 +       struct super_block *sb;
34239 +       struct au_branch *br;
34240 +};
34241 +
34242 +static void xino_do_trunc(void *_args)
34243 +{
34244 +       struct xino_do_trunc_args *args = _args;
34245 +       struct super_block *sb;
34246 +       struct au_branch *br;
34247 +       struct inode *dir;
34248 +       int err;
34249 +       aufs_bindex_t bindex;
34250 +
34251 +       err = 0;
34252 +       sb = args->sb;
34253 +       dir = d_inode(sb->s_root);
34254 +       br = args->br;
34255 +
34256 +       si_noflush_write_lock(sb);
34257 +       ii_read_lock_parent(dir);
34258 +       bindex = au_br_index(sb, br->br_id);
34259 +       err = au_xino_trunc(sb, bindex);
34260 +       ii_read_unlock(dir);
34261 +       if (unlikely(err))
34262 +               pr_warn("err b%d, (%d)\n", bindex, err);
34263 +       atomic_dec(&br->br_xino_running);
34264 +       atomic_dec(&br->br_count);
34265 +       si_write_unlock(sb);
34266 +       au_nwt_done(&au_sbi(sb)->si_nowait);
34267 +       kfree(args);
34268 +}
34269 +
34270 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34271 +{
34272 +       int err;
34273 +       struct kstatfs st;
34274 +       struct au_sbinfo *sbinfo;
34275 +
34276 +       /* todo: si_xino_expire and the ratio should be customizable */
34277 +       sbinfo = au_sbi(sb);
34278 +       if (time_before(jiffies,
34279 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34280 +               return 0;
34281 +
34282 +       /* truncation border */
34283 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34284 +       if (unlikely(err)) {
34285 +               AuErr1("statfs err %d, ignored\n", err);
34286 +               return 0;
34287 +       }
34288 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34289 +               return 0;
34290 +
34291 +       return 1;
34292 +}
34293 +
34294 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34295 +{
34296 +       struct xino_do_trunc_args *args;
34297 +       int wkq_err;
34298 +
34299 +       if (!xino_trunc_test(sb, br))
34300 +               return;
34301 +
34302 +       if (atomic_inc_return(&br->br_xino_running) > 1)
34303 +               goto out;
34304 +
34305 +       /* lock and kfree() will be called in trunc_xino() */
34306 +       args = kmalloc(sizeof(*args), GFP_NOFS);
34307 +       if (unlikely(!args)) {
34308 +               AuErr1("no memory\n");
34309 +               goto out_args;
34310 +       }
34311 +
34312 +       atomic_inc(&br->br_count);
34313 +       args->sb = sb;
34314 +       args->br = br;
34315 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
34316 +       if (!wkq_err)
34317 +               return; /* success */
34318 +
34319 +       pr_err("wkq %d\n", wkq_err);
34320 +       atomic_dec(&br->br_count);
34321 +
34322 +out_args:
34323 +       kfree(args);
34324 +out:
34325 +       atomic_dec(&br->br_xino_running);
34326 +}
34327 +
34328 +/* ---------------------------------------------------------------------- */
34329 +
34330 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
34331 +                           ino_t h_ino, ino_t ino)
34332 +{
34333 +       loff_t pos;
34334 +       ssize_t sz;
34335 +
34336 +       pos = h_ino;
34337 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34338 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34339 +               return -EFBIG;
34340 +       }
34341 +       pos *= sizeof(ino);
34342 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34343 +       if (sz == sizeof(ino))
34344 +               return 0; /* success */
34345 +
34346 +       AuIOErr("write failed (%zd)\n", sz);
34347 +       return -EIO;
34348 +}
34349 +
34350 +/*
34351 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34352 + * at the position of @h_ino.
34353 + * even if @ino is zero, it is written to the xinofile and means no entry.
34354 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34355 + * try truncating it.
34356 + */
34357 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34358 +                 ino_t ino)
34359 +{
34360 +       int err;
34361 +       unsigned int mnt_flags;
34362 +       struct au_branch *br;
34363 +
34364 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34365 +                    || ((loff_t)-1) > 0);
34366 +       SiMustAnyLock(sb);
34367 +
34368 +       mnt_flags = au_mntflags(sb);
34369 +       if (!au_opt_test(mnt_flags, XINO))
34370 +               return 0;
34371 +
34372 +       br = au_sbr(sb, bindex);
34373 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34374 +                              h_ino, ino);
34375 +       if (!err) {
34376 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34377 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34378 +                       xino_try_trunc(sb, br);
34379 +               return 0; /* success */
34380 +       }
34381 +
34382 +       AuIOErr("write failed (%d)\n", err);
34383 +       return -EIO;
34384 +}
34385 +
34386 +/* ---------------------------------------------------------------------- */
34387 +
34388 +/* aufs inode number bitmap */
34389 +
34390 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34391 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34392 +{
34393 +       ino_t ino;
34394 +
34395 +       AuDebugOn(bit < 0 || page_bits <= bit);
34396 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34397 +       return ino;
34398 +}
34399 +
34400 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34401 +{
34402 +       AuDebugOn(ino < AUFS_FIRST_INO);
34403 +       ino -= AUFS_FIRST_INO;
34404 +       *pindex = ino / page_bits;
34405 +       *bit = ino % page_bits;
34406 +}
34407 +
34408 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
34409 +{
34410 +       int err;
34411 +       loff_t pos;
34412 +       ssize_t sz;
34413 +       struct au_sbinfo *sbinfo;
34414 +       struct file *xib;
34415 +       unsigned long *p;
34416 +
34417 +       sbinfo = au_sbi(sb);
34418 +       MtxMustLock(&sbinfo->si_xib_mtx);
34419 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34420 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
34421 +
34422 +       if (pindex == sbinfo->si_xib_last_pindex)
34423 +               return 0;
34424 +
34425 +       xib = sbinfo->si_xib;
34426 +       p = sbinfo->si_xib_buf;
34427 +       pos = sbinfo->si_xib_last_pindex;
34428 +       pos *= PAGE_SIZE;
34429 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34430 +       if (unlikely(sz != PAGE_SIZE))
34431 +               goto out;
34432 +
34433 +       pos = pindex;
34434 +       pos *= PAGE_SIZE;
34435 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
34436 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34437 +       else {
34438 +               memset(p, 0, PAGE_SIZE);
34439 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34440 +       }
34441 +       if (sz == PAGE_SIZE) {
34442 +               sbinfo->si_xib_last_pindex = pindex;
34443 +               return 0; /* success */
34444 +       }
34445 +
34446 +out:
34447 +       AuIOErr1("write failed (%zd)\n", sz);
34448 +       err = sz;
34449 +       if (sz >= 0)
34450 +               err = -EIO;
34451 +       return err;
34452 +}
34453 +
34454 +/* ---------------------------------------------------------------------- */
34455 +
34456 +static void au_xib_clear_bit(struct inode *inode)
34457 +{
34458 +       int err, bit;
34459 +       unsigned long pindex;
34460 +       struct super_block *sb;
34461 +       struct au_sbinfo *sbinfo;
34462 +
34463 +       AuDebugOn(inode->i_nlink);
34464 +
34465 +       sb = inode->i_sb;
34466 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
34467 +       AuDebugOn(page_bits <= bit);
34468 +       sbinfo = au_sbi(sb);
34469 +       mutex_lock(&sbinfo->si_xib_mtx);
34470 +       err = xib_pindex(sb, pindex);
34471 +       if (!err) {
34472 +               clear_bit(bit, sbinfo->si_xib_buf);
34473 +               sbinfo->si_xib_next_bit = bit;
34474 +       }
34475 +       mutex_unlock(&sbinfo->si_xib_mtx);
34476 +}
34477 +
34478 +/* for s_op->delete_inode() */
34479 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
34480 +{
34481 +       int err;
34482 +       unsigned int mnt_flags;
34483 +       aufs_bindex_t bindex, bend, bi;
34484 +       unsigned char try_trunc;
34485 +       struct au_iinfo *iinfo;
34486 +       struct super_block *sb;
34487 +       struct au_hinode *hi;
34488 +       struct inode *h_inode;
34489 +       struct au_branch *br;
34490 +       vfs_writef_t xwrite;
34491 +
34492 +       sb = inode->i_sb;
34493 +       mnt_flags = au_mntflags(sb);
34494 +       if (!au_opt_test(mnt_flags, XINO)
34495 +           || inode->i_ino == AUFS_ROOT_INO)
34496 +               return;
34497 +
34498 +       if (unlinked) {
34499 +               au_xigen_inc(inode);
34500 +               au_xib_clear_bit(inode);
34501 +       }
34502 +
34503 +       iinfo = au_ii(inode);
34504 +       if (!iinfo)
34505 +               return;
34506 +
34507 +       bindex = iinfo->ii_bstart;
34508 +       if (bindex < 0)
34509 +               return;
34510 +
34511 +       xwrite = au_sbi(sb)->si_xwrite;
34512 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
34513 +       hi = iinfo->ii_hinode + bindex;
34514 +       bend = iinfo->ii_bend;
34515 +       for (; bindex <= bend; bindex++, hi++) {
34516 +               h_inode = hi->hi_inode;
34517 +               if (!h_inode
34518 +                   || (!unlinked && h_inode->i_nlink))
34519 +                       continue;
34520 +
34521 +               /* inode may not be revalidated */
34522 +               bi = au_br_index(sb, hi->hi_id);
34523 +               if (bi < 0)
34524 +                       continue;
34525 +
34526 +               br = au_sbr(sb, bi);
34527 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34528 +                                      h_inode->i_ino, /*ino*/0);
34529 +               if (!err && try_trunc
34530 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34531 +                       xino_try_trunc(sb, br);
34532 +       }
34533 +}
34534 +
34535 +/* get an unused inode number from bitmap */
34536 +ino_t au_xino_new_ino(struct super_block *sb)
34537 +{
34538 +       ino_t ino;
34539 +       unsigned long *p, pindex, ul, pend;
34540 +       struct au_sbinfo *sbinfo;
34541 +       struct file *file;
34542 +       int free_bit, err;
34543 +
34544 +       if (!au_opt_test(au_mntflags(sb), XINO))
34545 +               return iunique(sb, AUFS_FIRST_INO);
34546 +
34547 +       sbinfo = au_sbi(sb);
34548 +       mutex_lock(&sbinfo->si_xib_mtx);
34549 +       p = sbinfo->si_xib_buf;
34550 +       free_bit = sbinfo->si_xib_next_bit;
34551 +       if (free_bit < page_bits && !test_bit(free_bit, p))
34552 +               goto out; /* success */
34553 +       free_bit = find_first_zero_bit(p, page_bits);
34554 +       if (free_bit < page_bits)
34555 +               goto out; /* success */
34556 +
34557 +       pindex = sbinfo->si_xib_last_pindex;
34558 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34559 +               err = xib_pindex(sb, ul);
34560 +               if (unlikely(err))
34561 +                       goto out_err;
34562 +               free_bit = find_first_zero_bit(p, page_bits);
34563 +               if (free_bit < page_bits)
34564 +                       goto out; /* success */
34565 +       }
34566 +
34567 +       file = sbinfo->si_xib;
34568 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
34569 +       for (ul = pindex + 1; ul <= pend; ul++) {
34570 +               err = xib_pindex(sb, ul);
34571 +               if (unlikely(err))
34572 +                       goto out_err;
34573 +               free_bit = find_first_zero_bit(p, page_bits);
34574 +               if (free_bit < page_bits)
34575 +                       goto out; /* success */
34576 +       }
34577 +       BUG();
34578 +
34579 +out:
34580 +       set_bit(free_bit, p);
34581 +       sbinfo->si_xib_next_bit = free_bit + 1;
34582 +       pindex = sbinfo->si_xib_last_pindex;
34583 +       mutex_unlock(&sbinfo->si_xib_mtx);
34584 +       ino = xib_calc_ino(pindex, free_bit);
34585 +       AuDbg("i%lu\n", (unsigned long)ino);
34586 +       return ino;
34587 +out_err:
34588 +       mutex_unlock(&sbinfo->si_xib_mtx);
34589 +       AuDbg("i0\n");
34590 +       return 0;
34591 +}
34592 +
34593 +/*
34594 + * read @ino from xinofile for the specified branch{@sb, @bindex}
34595 + * at the position of @h_ino.
34596 + * if @ino does not exist and @do_new is true, get new one.
34597 + */
34598 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34599 +                ino_t *ino)
34600 +{
34601 +       int err;
34602 +       ssize_t sz;
34603 +       loff_t pos;
34604 +       struct file *file;
34605 +       struct au_sbinfo *sbinfo;
34606 +
34607 +       *ino = 0;
34608 +       if (!au_opt_test(au_mntflags(sb), XINO))
34609 +               return 0; /* no xino */
34610 +
34611 +       err = 0;
34612 +       sbinfo = au_sbi(sb);
34613 +       pos = h_ino;
34614 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34615 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34616 +               return -EFBIG;
34617 +       }
34618 +       pos *= sizeof(*ino);
34619 +
34620 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
34621 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
34622 +               return 0; /* no ino */
34623 +
34624 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34625 +       if (sz == sizeof(*ino))
34626 +               return 0; /* success */
34627 +
34628 +       err = sz;
34629 +       if (unlikely(sz >= 0)) {
34630 +               err = -EIO;
34631 +               AuIOErr("xino read error (%zd)\n", sz);
34632 +       }
34633 +
34634 +       return err;
34635 +}
34636 +
34637 +/* ---------------------------------------------------------------------- */
34638 +
34639 +/* create and set a new xino file */
34640 +
34641 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34642 +{
34643 +       struct file *file;
34644 +       struct dentry *h_parent, *d;
34645 +       struct inode *h_dir, *inode;
34646 +       int err;
34647 +
34648 +       /*
34649 +        * at mount-time, and the xino file is the default path,
34650 +        * hnotify is disabled so we have no notify events to ignore.
34651 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
34652 +        */
34653 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34654 +                              /* | __FMODE_NONOTIFY */,
34655 +                              S_IRUGO | S_IWUGO);
34656 +       if (IS_ERR(file)) {
34657 +               if (!silent)
34658 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
34659 +               return file;
34660 +       }
34661 +
34662 +       /* keep file count */
34663 +       err = 0;
34664 +       inode = file_inode(file);
34665 +       h_parent = dget_parent(file->f_path.dentry);
34666 +       h_dir = d_inode(h_parent);
34667 +       inode_lock_nested(h_dir, AuLsc_I_PARENT);
34668 +       /* mnt_want_write() is unnecessary here */
34669 +       /* no delegation since it is just created */
34670 +       if (inode->i_nlink)
34671 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34672 +                                  /*force*/0);
34673 +       inode_unlock(h_dir);
34674 +       dput(h_parent);
34675 +       if (unlikely(err)) {
34676 +               if (!silent)
34677 +                       pr_err("unlink %s(%d)\n", fname, err);
34678 +               goto out;
34679 +       }
34680 +
34681 +       err = -EINVAL;
34682 +       d = file->f_path.dentry;
34683 +       if (unlikely(sb == d->d_sb)) {
34684 +               if (!silent)
34685 +                       pr_err("%s must be outside\n", fname);
34686 +               goto out;
34687 +       }
34688 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34689 +               if (!silent)
34690 +                       pr_err("xino doesn't support %s(%s)\n",
34691 +                              fname, au_sbtype(d->d_sb));
34692 +               goto out;
34693 +       }
34694 +       return file; /* success */
34695 +
34696 +out:
34697 +       fput(file);
34698 +       file = ERR_PTR(err);
34699 +       return file;
34700 +}
34701 +
34702 +/*
34703 + * find another branch who is on the same filesystem of the specified
34704 + * branch{@btgt}. search until @bend.
34705 + */
34706 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
34707 +                       aufs_bindex_t bend)
34708 +{
34709 +       aufs_bindex_t bindex;
34710 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34711 +
34712 +       for (bindex = 0; bindex < btgt; bindex++)
34713 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34714 +                       return bindex;
34715 +       for (bindex++; bindex <= bend; bindex++)
34716 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34717 +                       return bindex;
34718 +       return -1;
34719 +}
34720 +
34721 +/* ---------------------------------------------------------------------- */
34722 +
34723 +/*
34724 + * initialize the xinofile for the specified branch @br
34725 + * at the place/path where @base_file indicates.
34726 + * test whether another branch is on the same filesystem or not,
34727 + * if @do_test is true.
34728 + */
34729 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34730 +              struct file *base_file, int do_test)
34731 +{
34732 +       int err;
34733 +       ino_t ino;
34734 +       aufs_bindex_t bend, bindex;
34735 +       struct au_branch *shared_br, *b;
34736 +       struct file *file;
34737 +       struct super_block *tgt_sb;
34738 +
34739 +       shared_br = NULL;
34740 +       bend = au_sbend(sb);
34741 +       if (do_test) {
34742 +               tgt_sb = au_br_sb(br);
34743 +               for (bindex = 0; bindex <= bend; bindex++) {
34744 +                       b = au_sbr(sb, bindex);
34745 +                       if (tgt_sb == au_br_sb(b)) {
34746 +                               shared_br = b;
34747 +                               break;
34748 +                       }
34749 +               }
34750 +       }
34751 +
34752 +       if (!shared_br || !shared_br->br_xino.xi_file) {
34753 +               struct au_xino_lock_dir ldir;
34754 +
34755 +               au_xino_lock_dir(sb, base_file, &ldir);
34756 +               /* mnt_want_write() is unnecessary here */
34757 +               file = au_xino_create2(base_file, NULL);
34758 +               au_xino_unlock_dir(&ldir);
34759 +               err = PTR_ERR(file);
34760 +               if (IS_ERR(file))
34761 +                       goto out;
34762 +               br->br_xino.xi_file = file;
34763 +       } else {
34764 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
34765 +               get_file(br->br_xino.xi_file);
34766 +       }
34767 +
34768 +       ino = AUFS_ROOT_INO;
34769 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34770 +                              h_ino, ino);
34771 +       if (unlikely(err)) {
34772 +               fput(br->br_xino.xi_file);
34773 +               br->br_xino.xi_file = NULL;
34774 +       }
34775 +
34776 +out:
34777 +       return err;
34778 +}
34779 +
34780 +/* ---------------------------------------------------------------------- */
34781 +
34782 +/* trucate a xino bitmap file */
34783 +
34784 +/* todo: slow */
34785 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
34786 +{
34787 +       int err, bit;
34788 +       ssize_t sz;
34789 +       unsigned long pindex;
34790 +       loff_t pos, pend;
34791 +       struct au_sbinfo *sbinfo;
34792 +       vfs_readf_t func;
34793 +       ino_t *ino;
34794 +       unsigned long *p;
34795 +
34796 +       err = 0;
34797 +       sbinfo = au_sbi(sb);
34798 +       MtxMustLock(&sbinfo->si_xib_mtx);
34799 +       p = sbinfo->si_xib_buf;
34800 +       func = sbinfo->si_xread;
34801 +       pend = vfsub_f_size_read(file);
34802 +       pos = 0;
34803 +       while (pos < pend) {
34804 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
34805 +               err = sz;
34806 +               if (unlikely(sz <= 0))
34807 +                       goto out;
34808 +
34809 +               err = 0;
34810 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
34811 +                       if (unlikely(*ino < AUFS_FIRST_INO))
34812 +                               continue;
34813 +
34814 +                       xib_calc_bit(*ino, &pindex, &bit);
34815 +                       AuDebugOn(page_bits <= bit);
34816 +                       err = xib_pindex(sb, pindex);
34817 +                       if (!err)
34818 +                               set_bit(bit, p);
34819 +                       else
34820 +                               goto out;
34821 +               }
34822 +       }
34823 +
34824 +out:
34825 +       return err;
34826 +}
34827 +
34828 +static int xib_restore(struct super_block *sb)
34829 +{
34830 +       int err;
34831 +       aufs_bindex_t bindex, bend;
34832 +       void *page;
34833 +
34834 +       err = -ENOMEM;
34835 +       page = (void *)__get_free_page(GFP_NOFS);
34836 +       if (unlikely(!page))
34837 +               goto out;
34838 +
34839 +       err = 0;
34840 +       bend = au_sbend(sb);
34841 +       for (bindex = 0; !err && bindex <= bend; bindex++)
34842 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
34843 +                       err = do_xib_restore
34844 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
34845 +               else
34846 +                       AuDbg("b%d\n", bindex);
34847 +       free_page((unsigned long)page);
34848 +
34849 +out:
34850 +       return err;
34851 +}
34852 +
34853 +int au_xib_trunc(struct super_block *sb)
34854 +{
34855 +       int err;
34856 +       ssize_t sz;
34857 +       loff_t pos;
34858 +       struct au_xino_lock_dir ldir;
34859 +       struct au_sbinfo *sbinfo;
34860 +       unsigned long *p;
34861 +       struct file *file;
34862 +
34863 +       SiMustWriteLock(sb);
34864 +
34865 +       err = 0;
34866 +       sbinfo = au_sbi(sb);
34867 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
34868 +               goto out;
34869 +
34870 +       file = sbinfo->si_xib;
34871 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
34872 +               goto out;
34873 +
34874 +       au_xino_lock_dir(sb, file, &ldir);
34875 +       /* mnt_want_write() is unnecessary here */
34876 +       file = au_xino_create2(sbinfo->si_xib, NULL);
34877 +       au_xino_unlock_dir(&ldir);
34878 +       err = PTR_ERR(file);
34879 +       if (IS_ERR(file))
34880 +               goto out;
34881 +       fput(sbinfo->si_xib);
34882 +       sbinfo->si_xib = file;
34883 +
34884 +       p = sbinfo->si_xib_buf;
34885 +       memset(p, 0, PAGE_SIZE);
34886 +       pos = 0;
34887 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34888 +       if (unlikely(sz != PAGE_SIZE)) {
34889 +               err = sz;
34890 +               AuIOErr("err %d\n", err);
34891 +               if (sz >= 0)
34892 +                       err = -EIO;
34893 +               goto out;
34894 +       }
34895 +
34896 +       mutex_lock(&sbinfo->si_xib_mtx);
34897 +       /* mnt_want_write() is unnecessary here */
34898 +       err = xib_restore(sb);
34899 +       mutex_unlock(&sbinfo->si_xib_mtx);
34900 +
34901 +out:
34902 +       return err;
34903 +}
34904 +
34905 +/* ---------------------------------------------------------------------- */
34906 +
34907 +/*
34908 + * xino mount option handlers
34909 + */
34910 +
34911 +/* xino bitmap */
34912 +static void xino_clear_xib(struct super_block *sb)
34913 +{
34914 +       struct au_sbinfo *sbinfo;
34915 +
34916 +       SiMustWriteLock(sb);
34917 +
34918 +       sbinfo = au_sbi(sb);
34919 +       sbinfo->si_xread = NULL;
34920 +       sbinfo->si_xwrite = NULL;
34921 +       if (sbinfo->si_xib)
34922 +               fput(sbinfo->si_xib);
34923 +       sbinfo->si_xib = NULL;
34924 +       free_page((unsigned long)sbinfo->si_xib_buf);
34925 +       sbinfo->si_xib_buf = NULL;
34926 +}
34927 +
34928 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
34929 +{
34930 +       int err;
34931 +       loff_t pos;
34932 +       struct au_sbinfo *sbinfo;
34933 +       struct file *file;
34934 +
34935 +       SiMustWriteLock(sb);
34936 +
34937 +       sbinfo = au_sbi(sb);
34938 +       file = au_xino_create2(base, sbinfo->si_xib);
34939 +       err = PTR_ERR(file);
34940 +       if (IS_ERR(file))
34941 +               goto out;
34942 +       if (sbinfo->si_xib)
34943 +               fput(sbinfo->si_xib);
34944 +       sbinfo->si_xib = file;
34945 +       sbinfo->si_xread = vfs_readf(file);
34946 +       sbinfo->si_xwrite = vfs_writef(file);
34947 +
34948 +       err = -ENOMEM;
34949 +       if (!sbinfo->si_xib_buf)
34950 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
34951 +       if (unlikely(!sbinfo->si_xib_buf))
34952 +               goto out_unset;
34953 +
34954 +       sbinfo->si_xib_last_pindex = 0;
34955 +       sbinfo->si_xib_next_bit = 0;
34956 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
34957 +               pos = 0;
34958 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
34959 +                                 PAGE_SIZE, &pos);
34960 +               if (unlikely(err != PAGE_SIZE))
34961 +                       goto out_free;
34962 +       }
34963 +       err = 0;
34964 +       goto out; /* success */
34965 +
34966 +out_free:
34967 +       free_page((unsigned long)sbinfo->si_xib_buf);
34968 +       sbinfo->si_xib_buf = NULL;
34969 +       if (err >= 0)
34970 +               err = -EIO;
34971 +out_unset:
34972 +       fput(sbinfo->si_xib);
34973 +       sbinfo->si_xib = NULL;
34974 +       sbinfo->si_xread = NULL;
34975 +       sbinfo->si_xwrite = NULL;
34976 +out:
34977 +       return err;
34978 +}
34979 +
34980 +/* xino for each branch */
34981 +static void xino_clear_br(struct super_block *sb)
34982 +{
34983 +       aufs_bindex_t bindex, bend;
34984 +       struct au_branch *br;
34985 +
34986 +       bend = au_sbend(sb);
34987 +       for (bindex = 0; bindex <= bend; bindex++) {
34988 +               br = au_sbr(sb, bindex);
34989 +               if (!br || !br->br_xino.xi_file)
34990 +                       continue;
34991 +
34992 +               fput(br->br_xino.xi_file);
34993 +               br->br_xino.xi_file = NULL;
34994 +       }
34995 +}
34996 +
34997 +static int au_xino_set_br(struct super_block *sb, struct file *base)
34998 +{
34999 +       int err;
35000 +       ino_t ino;
35001 +       aufs_bindex_t bindex, bend, bshared;
35002 +       struct {
35003 +               struct file *old, *new;
35004 +       } *fpair, *p;
35005 +       struct au_branch *br;
35006 +       struct inode *inode;
35007 +       vfs_writef_t writef;
35008 +
35009 +       SiMustWriteLock(sb);
35010 +
35011 +       err = -ENOMEM;
35012 +       bend = au_sbend(sb);
35013 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
35014 +       if (unlikely(!fpair))
35015 +               goto out;
35016 +
35017 +       inode = d_inode(sb->s_root);
35018 +       ino = AUFS_ROOT_INO;
35019 +       writef = au_sbi(sb)->si_xwrite;
35020 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
35021 +               br = au_sbr(sb, bindex);
35022 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
35023 +               if (bshared >= 0) {
35024 +                       /* shared xino */
35025 +                       *p = fpair[bshared];
35026 +                       get_file(p->new);
35027 +               }
35028 +
35029 +               if (!p->new) {
35030 +                       /* new xino */
35031 +                       p->old = br->br_xino.xi_file;
35032 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
35033 +                       err = PTR_ERR(p->new);
35034 +                       if (IS_ERR(p->new)) {
35035 +                               p->new = NULL;
35036 +                               goto out_pair;
35037 +                       }
35038 +               }
35039 +
35040 +               err = au_xino_do_write(writef, p->new,
35041 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
35042 +               if (unlikely(err))
35043 +                       goto out_pair;
35044 +       }
35045 +
35046 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
35047 +               br = au_sbr(sb, bindex);
35048 +               if (br->br_xino.xi_file)
35049 +                       fput(br->br_xino.xi_file);
35050 +               get_file(p->new);
35051 +               br->br_xino.xi_file = p->new;
35052 +       }
35053 +
35054 +out_pair:
35055 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
35056 +               if (p->new)
35057 +                       fput(p->new);
35058 +               else
35059 +                       break;
35060 +       kfree(fpair);
35061 +out:
35062 +       return err;
35063 +}
35064 +
35065 +void au_xino_clr(struct super_block *sb)
35066 +{
35067 +       struct au_sbinfo *sbinfo;
35068 +
35069 +       au_xigen_clr(sb);
35070 +       xino_clear_xib(sb);
35071 +       xino_clear_br(sb);
35072 +       sbinfo = au_sbi(sb);
35073 +       /* lvalue, do not call au_mntflags() */
35074 +       au_opt_clr(sbinfo->si_mntflags, XINO);
35075 +}
35076 +
35077 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
35078 +{
35079 +       int err, skip;
35080 +       struct dentry *parent, *cur_parent;
35081 +       struct qstr *dname, *cur_name;
35082 +       struct file *cur_xino;
35083 +       struct inode *dir;
35084 +       struct au_sbinfo *sbinfo;
35085 +
35086 +       SiMustWriteLock(sb);
35087 +
35088 +       err = 0;
35089 +       sbinfo = au_sbi(sb);
35090 +       parent = dget_parent(xino->file->f_path.dentry);
35091 +       if (remount) {
35092 +               skip = 0;
35093 +               dname = &xino->file->f_path.dentry->d_name;
35094 +               cur_xino = sbinfo->si_xib;
35095 +               if (cur_xino) {
35096 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
35097 +                       cur_name = &cur_xino->f_path.dentry->d_name;
35098 +                       skip = (cur_parent == parent
35099 +                               && au_qstreq(dname, cur_name));
35100 +                       dput(cur_parent);
35101 +               }
35102 +               if (skip)
35103 +                       goto out;
35104 +       }
35105 +
35106 +       au_opt_set(sbinfo->si_mntflags, XINO);
35107 +       dir = d_inode(parent);
35108 +       inode_lock_nested(dir, AuLsc_I_PARENT);
35109 +       /* mnt_want_write() is unnecessary here */
35110 +       err = au_xino_set_xib(sb, xino->file);
35111 +       if (!err)
35112 +               err = au_xigen_set(sb, xino->file);
35113 +       if (!err)
35114 +               err = au_xino_set_br(sb, xino->file);
35115 +       inode_unlock(dir);
35116 +       if (!err)
35117 +               goto out; /* success */
35118 +
35119 +       /* reset all */
35120 +       AuIOErr("failed creating xino(%d).\n", err);
35121 +       au_xigen_clr(sb);
35122 +       xino_clear_xib(sb);
35123 +
35124 +out:
35125 +       dput(parent);
35126 +       return err;
35127 +}
35128 +
35129 +/* ---------------------------------------------------------------------- */
35130 +
35131 +/*
35132 + * create a xinofile at the default place/path.
35133 + */
35134 +struct file *au_xino_def(struct super_block *sb)
35135 +{
35136 +       struct file *file;
35137 +       char *page, *p;
35138 +       struct au_branch *br;
35139 +       struct super_block *h_sb;
35140 +       struct path path;
35141 +       aufs_bindex_t bend, bindex, bwr;
35142 +
35143 +       br = NULL;
35144 +       bend = au_sbend(sb);
35145 +       bwr = -1;
35146 +       for (bindex = 0; bindex <= bend; bindex++) {
35147 +               br = au_sbr(sb, bindex);
35148 +               if (au_br_writable(br->br_perm)
35149 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
35150 +                       bwr = bindex;
35151 +                       break;
35152 +               }
35153 +       }
35154 +
35155 +       if (bwr >= 0) {
35156 +               file = ERR_PTR(-ENOMEM);
35157 +               page = (void *)__get_free_page(GFP_NOFS);
35158 +               if (unlikely(!page))
35159 +                       goto out;
35160 +               path.mnt = au_br_mnt(br);
35161 +               path.dentry = au_h_dptr(sb->s_root, bwr);
35162 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35163 +               file = (void *)p;
35164 +               if (!IS_ERR(p)) {
35165 +                       strcat(p, "/" AUFS_XINO_FNAME);
35166 +                       AuDbg("%s\n", p);
35167 +                       file = au_xino_create(sb, p, /*silent*/0);
35168 +                       if (!IS_ERR(file))
35169 +                               au_xino_brid_set(sb, br->br_id);
35170 +               }
35171 +               free_page((unsigned long)page);
35172 +       } else {
35173 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35174 +               if (IS_ERR(file))
35175 +                       goto out;
35176 +               h_sb = file->f_path.dentry->d_sb;
35177 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
35178 +                       pr_err("xino doesn't support %s(%s)\n",
35179 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35180 +                       fput(file);
35181 +                       file = ERR_PTR(-EINVAL);
35182 +               }
35183 +               if (!IS_ERR(file))
35184 +                       au_xino_brid_set(sb, -1);
35185 +       }
35186 +
35187 +out:
35188 +       return file;
35189 +}
35190 +
35191 +/* ---------------------------------------------------------------------- */
35192 +
35193 +int au_xino_path(struct seq_file *seq, struct file *file)
35194 +{
35195 +       int err;
35196 +
35197 +       err = au_seq_path(seq, &file->f_path);
35198 +       if (unlikely(err))
35199 +               goto out;
35200 +
35201 +#define Deleted "\\040(deleted)"
35202 +       seq->count -= sizeof(Deleted) - 1;
35203 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35204 +                        sizeof(Deleted) - 1));
35205 +#undef Deleted
35206 +
35207 +out:
35208 +       return err;
35209 +}
35210 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35211 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
35212 +++ linux/include/uapi/linux/aufs_type.h        2016-04-24 18:32:51.397020264 +0200
35213 @@ -0,0 +1,419 @@
35214 +/*
35215 + * Copyright (C) 2005-2016 Junjiro R. Okajima
35216 + *
35217 + * This program, aufs is free software; you can redistribute it and/or modify
35218 + * it under the terms of the GNU General Public License as published by
35219 + * the Free Software Foundation; either version 2 of the License, or
35220 + * (at your option) any later version.
35221 + *
35222 + * This program is distributed in the hope that it will be useful,
35223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35225 + * GNU General Public License for more details.
35226 + *
35227 + * You should have received a copy of the GNU General Public License
35228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35229 + */
35230 +
35231 +#ifndef __AUFS_TYPE_H__
35232 +#define __AUFS_TYPE_H__
35233 +
35234 +#define AUFS_NAME      "aufs"
35235 +
35236 +#ifdef __KERNEL__
35237 +/*
35238 + * define it before including all other headers.
35239 + * sched.h may use pr_* macros before defining "current", so define the
35240 + * no-current version first, and re-define later.
35241 + */
35242 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35243 +#include <linux/sched.h>
35244 +#undef pr_fmt
35245 +#define pr_fmt(fmt) \
35246 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35247 +               (int)sizeof(current->comm), current->comm, current->pid
35248 +#else
35249 +#include <stdint.h>
35250 +#include <sys/types.h>
35251 +#endif /* __KERNEL__ */
35252 +
35253 +#include <linux/limits.h>
35254 +
35255 +#define AUFS_VERSION   "4.5-20160328"
35256 +
35257 +/* todo? move this to linux-2.6.19/include/magic.h */
35258 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35259 +
35260 +/* ---------------------------------------------------------------------- */
35261 +
35262 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
35263 +typedef int8_t aufs_bindex_t;
35264 +#define AUFS_BRANCH_MAX 127
35265 +#else
35266 +typedef int16_t aufs_bindex_t;
35267 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
35268 +#define AUFS_BRANCH_MAX 511
35269 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35270 +#define AUFS_BRANCH_MAX 1023
35271 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35272 +#define AUFS_BRANCH_MAX 32767
35273 +#endif
35274 +#endif
35275 +
35276 +#ifdef __KERNEL__
35277 +#ifndef AUFS_BRANCH_MAX
35278 +#error unknown CONFIG_AUFS_BRANCH_MAX value
35279 +#endif
35280 +#endif /* __KERNEL__ */
35281 +
35282 +/* ---------------------------------------------------------------------- */
35283 +
35284 +#define AUFS_FSTYPE            AUFS_NAME
35285 +
35286 +#define AUFS_ROOT_INO          2
35287 +#define AUFS_FIRST_INO         11
35288 +
35289 +#define AUFS_WH_PFX            ".wh."
35290 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
35291 +#define AUFS_WH_TMP_LEN                4
35292 +/* a limit for rmdir/rename a dir and copyup */
35293 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
35294 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
35295 +                               - 1                     /* dot */\
35296 +                               - AUFS_WH_TMP_LEN)      /* hex */
35297 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
35298 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
35299 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
35300 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
35301 +#define AUFS_DIRWH_DEF         3
35302 +#define AUFS_RDCACHE_DEF       10 /* seconds */
35303 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
35304 +#define AUFS_RDBLK_DEF         512 /* bytes */
35305 +#define AUFS_RDHASH_DEF                32
35306 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
35307 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
35308 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
35309 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
35310 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
35311 +
35312 +/* pseudo-link maintenace under /proc */
35313 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
35314 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
35315 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35316 +
35317 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
35318 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
35319 +
35320 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
35321 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
35322 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
35323 +
35324 +/* doubly whiteouted */
35325 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
35326 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
35327 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
35328 +
35329 +/* branch permissions and attributes */
35330 +#define AUFS_BRPERM_RW         "rw"
35331 +#define AUFS_BRPERM_RO         "ro"
35332 +#define AUFS_BRPERM_RR         "rr"
35333 +#define AUFS_BRATTR_COO_REG    "coo_reg"
35334 +#define AUFS_BRATTR_COO_ALL    "coo_all"
35335 +#define AUFS_BRATTR_FHSM       "fhsm"
35336 +#define AUFS_BRATTR_UNPIN      "unpin"
35337 +#define AUFS_BRATTR_ICEX       "icex"
35338 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
35339 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
35340 +#define AUFS_BRATTR_ICEX_TR    "icextr"
35341 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
35342 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
35343 +#define AUFS_BRRATTR_WH                "wh"
35344 +#define AUFS_BRWATTR_NLWH      "nolwh"
35345 +#define AUFS_BRWATTR_MOO       "moo"
35346 +
35347 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
35348 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
35349 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
35350 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35351 +
35352 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
35353 +#define AuBrAttr_COO_ALL       (1 << 4)
35354 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35355 +
35356 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
35357 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
35358 +                                                  branch. meaningless since
35359 +                                                  linux-3.18-rc1 */
35360 +
35361 +/* ignore error in copying XATTR */
35362 +#define AuBrAttr_ICEX_SEC      (1 << 7)
35363 +#define AuBrAttr_ICEX_SYS      (1 << 8)
35364 +#define AuBrAttr_ICEX_TR       (1 << 9)
35365 +#define AuBrAttr_ICEX_USR      (1 << 10)
35366 +#define AuBrAttr_ICEX_OTH      (1 << 11)
35367 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
35368 +                                | AuBrAttr_ICEX_SYS    \
35369 +                                | AuBrAttr_ICEX_TR     \
35370 +                                | AuBrAttr_ICEX_USR    \
35371 +                                | AuBrAttr_ICEX_OTH)
35372 +
35373 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
35374 +#define AuBrRAttr_Mask         AuBrRAttr_WH
35375 +
35376 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
35377 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
35378 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35379 +
35380 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35381 +
35382 +/* #warning test userspace */
35383 +#ifdef __KERNEL__
35384 +#ifndef CONFIG_AUFS_FHSM
35385 +#undef AuBrAttr_FHSM
35386 +#define AuBrAttr_FHSM          0
35387 +#endif
35388 +#ifndef CONFIG_AUFS_XATTR
35389 +#undef AuBrAttr_ICEX
35390 +#define AuBrAttr_ICEX          0
35391 +#undef AuBrAttr_ICEX_SEC
35392 +#define AuBrAttr_ICEX_SEC      0
35393 +#undef AuBrAttr_ICEX_SYS
35394 +#define AuBrAttr_ICEX_SYS      0
35395 +#undef AuBrAttr_ICEX_TR
35396 +#define AuBrAttr_ICEX_TR       0
35397 +#undef AuBrAttr_ICEX_USR
35398 +#define AuBrAttr_ICEX_USR      0
35399 +#undef AuBrAttr_ICEX_OTH
35400 +#define AuBrAttr_ICEX_OTH      0
35401 +#endif
35402 +#endif
35403 +
35404 +/* the longest combination */
35405 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35406 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
35407 +                              "+" AUFS_BRATTR_COO_REG          \
35408 +                              "+" AUFS_BRATTR_FHSM             \
35409 +                              "+" AUFS_BRATTR_UNPIN            \
35410 +                              "+" AUFS_BRATTR_ICEX_SEC         \
35411 +                              "+" AUFS_BRATTR_ICEX_SYS         \
35412 +                              "+" AUFS_BRATTR_ICEX_USR         \
35413 +                              "+" AUFS_BRATTR_ICEX_OTH         \
35414 +                              "+" AUFS_BRWATTR_NLWH)
35415 +
35416 +typedef struct {
35417 +       char a[AuBrPermStrSz];
35418 +} au_br_perm_str_t;
35419 +
35420 +static inline int au_br_writable(int brperm)
35421 +{
35422 +       return brperm & AuBrPerm_RW;
35423 +}
35424 +
35425 +static inline int au_br_whable(int brperm)
35426 +{
35427 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35428 +}
35429 +
35430 +static inline int au_br_wh_linkable(int brperm)
35431 +{
35432 +       return !(brperm & AuBrWAttr_NoLinkWH);
35433 +}
35434 +
35435 +static inline int au_br_cmoo(int brperm)
35436 +{
35437 +       return brperm & AuBrAttr_CMOO_Mask;
35438 +}
35439 +
35440 +static inline int au_br_fhsm(int brperm)
35441 +{
35442 +       return brperm & AuBrAttr_FHSM;
35443 +}
35444 +
35445 +/* ---------------------------------------------------------------------- */
35446 +
35447 +/* ioctl */
35448 +enum {
35449 +       /* readdir in userspace */
35450 +       AuCtl_RDU,
35451 +       AuCtl_RDU_INO,
35452 +
35453 +       AuCtl_WBR_FD,   /* pathconf wrapper */
35454 +       AuCtl_IBUSY,    /* busy inode */
35455 +       AuCtl_MVDOWN,   /* move-down */
35456 +       AuCtl_BR,       /* info about branches */
35457 +       AuCtl_FHSM_FD   /* connection for fhsm */
35458 +};
35459 +
35460 +/* borrowed from linux/include/linux/kernel.h */
35461 +#ifndef ALIGN
35462 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
35463 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
35464 +#endif
35465 +
35466 +/* borrowed from linux/include/linux/compiler-gcc3.h */
35467 +#ifndef __aligned
35468 +#define __aligned(x)                   __attribute__((aligned(x)))
35469 +#endif
35470 +
35471 +#ifdef __KERNEL__
35472 +#ifndef __packed
35473 +#define __packed                       __attribute__((packed))
35474 +#endif
35475 +#endif
35476 +
35477 +struct au_rdu_cookie {
35478 +       uint64_t        h_pos;
35479 +       int16_t         bindex;
35480 +       uint8_t         flags;
35481 +       uint8_t         pad;
35482 +       uint32_t        generation;
35483 +} __aligned(8);
35484 +
35485 +struct au_rdu_ent {
35486 +       uint64_t        ino;
35487 +       int16_t         bindex;
35488 +       uint8_t         type;
35489 +       uint8_t         nlen;
35490 +       uint8_t         wh;
35491 +       char            name[0];
35492 +} __aligned(8);
35493 +
35494 +static inline int au_rdu_len(int nlen)
35495 +{
35496 +       /* include the terminating NULL */
35497 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
35498 +                    sizeof(uint64_t));
35499 +}
35500 +
35501 +union au_rdu_ent_ul {
35502 +       struct au_rdu_ent __user        *e;
35503 +       uint64_t                        ul;
35504 +};
35505 +
35506 +enum {
35507 +       AufsCtlRduV_SZ,
35508 +       AufsCtlRduV_End
35509 +};
35510 +
35511 +struct aufs_rdu {
35512 +       /* input */
35513 +       union {
35514 +               uint64_t        sz;     /* AuCtl_RDU */
35515 +               uint64_t        nent;   /* AuCtl_RDU_INO */
35516 +       };
35517 +       union au_rdu_ent_ul     ent;
35518 +       uint16_t                verify[AufsCtlRduV_End];
35519 +
35520 +       /* input/output */
35521 +       uint32_t                blk;
35522 +
35523 +       /* output */
35524 +       union au_rdu_ent_ul     tail;
35525 +       /* number of entries which were added in a single call */
35526 +       uint64_t                rent;
35527 +       uint8_t                 full;
35528 +       uint8_t                 shwh;
35529 +
35530 +       struct au_rdu_cookie    cookie;
35531 +} __aligned(8);
35532 +
35533 +/* ---------------------------------------------------------------------- */
35534 +
35535 +struct aufs_wbr_fd {
35536 +       uint32_t        oflags;
35537 +       int16_t         brid;
35538 +} __aligned(8);
35539 +
35540 +/* ---------------------------------------------------------------------- */
35541 +
35542 +struct aufs_ibusy {
35543 +       uint64_t        ino, h_ino;
35544 +       int16_t         bindex;
35545 +} __aligned(8);
35546 +
35547 +/* ---------------------------------------------------------------------- */
35548 +
35549 +/* error code for move-down */
35550 +/* the actual message strings are implemented in aufs-util.git */
35551 +enum {
35552 +       EAU_MVDOWN_OPAQUE = 1,
35553 +       EAU_MVDOWN_WHITEOUT,
35554 +       EAU_MVDOWN_UPPER,
35555 +       EAU_MVDOWN_BOTTOM,
35556 +       EAU_MVDOWN_NOUPPER,
35557 +       EAU_MVDOWN_NOLOWERBR,
35558 +       EAU_Last
35559 +};
35560 +
35561 +/* flags for move-down */
35562 +#define AUFS_MVDOWN_DMSG       1
35563 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
35564 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
35565 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
35566 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
35567 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
35568 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
35569 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
35570 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
35571 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
35572 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
35573 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
35574 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
35575 +
35576 +/* index for move-down */
35577 +enum {
35578 +       AUFS_MVDOWN_UPPER,
35579 +       AUFS_MVDOWN_LOWER,
35580 +       AUFS_MVDOWN_NARRAY
35581 +};
35582 +
35583 +/*
35584 + * additional info of move-down
35585 + * number of free blocks and inodes.
35586 + * subset of struct kstatfs, but smaller and always 64bit.
35587 + */
35588 +struct aufs_stfs {
35589 +       uint64_t        f_blocks;
35590 +       uint64_t        f_bavail;
35591 +       uint64_t        f_files;
35592 +       uint64_t        f_ffree;
35593 +};
35594 +
35595 +struct aufs_stbr {
35596 +       int16_t                 brid;   /* optional input */
35597 +       int16_t                 bindex; /* output */
35598 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
35599 +} __aligned(8);
35600 +
35601 +struct aufs_mvdown {
35602 +       uint32_t                flags;                  /* input/output */
35603 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35604 +       int8_t                  au_errno;               /* output */
35605 +} __aligned(8);
35606 +
35607 +/* ---------------------------------------------------------------------- */
35608 +
35609 +union aufs_brinfo {
35610 +       /* PATH_MAX may differ between kernel-space and user-space */
35611 +       char    _spacer[4096];
35612 +       struct {
35613 +               int16_t id;
35614 +               int     perm;
35615 +               char    path[0];
35616 +       };
35617 +} __aligned(8);
35618 +
35619 +/* ---------------------------------------------------------------------- */
35620 +
35621 +#define AuCtlType              'A'
35622 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35623 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
35624 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
35625 +                                    struct aufs_wbr_fd)
35626 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
35627 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
35628 +                                     struct aufs_mvdown)
35629 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35630 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
35631 +
35632 +#endif /* __AUFS_TYPE_H__ */
35633 aufs4.5 loopback patch
35634
35635 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
35636 index 0b816b2..86dd454 100644
35637 --- a/drivers/block/loop.c
35638 +++ b/drivers/block/loop.c
35639 @@ -556,7 +556,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
35640  }
35641  
35642  struct switch_request {
35643 -       struct file *file;
35644 +       struct file *file, *virt_file;
35645         struct completion wait;
35646  };
35647  
35648 @@ -582,6 +582,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35649         mapping = file->f_mapping;
35650         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
35651         lo->lo_backing_file = file;
35652 +       lo->lo_backing_virt_file = p->virt_file;
35653         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
35654                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
35655         lo->old_gfp_mask = mapping_gfp_mask(mapping);
35656 @@ -594,11 +595,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35657   * First it needs to flush existing IO, it does this by sending a magic
35658   * BIO down the pipe. The completion of this BIO does the actual switch.
35659   */
35660 -static int loop_switch(struct loop_device *lo, struct file *file)
35661 +static int loop_switch(struct loop_device *lo, struct file *file,
35662 +                      struct file *virt_file)
35663  {
35664         struct switch_request w;
35665  
35666         w.file = file;
35667 +       w.virt_file = virt_file;
35668  
35669         /* freeze queue and wait for completion of scheduled requests */
35670         blk_mq_freeze_queue(lo->lo_queue);
35671 @@ -617,7 +620,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
35672   */
35673  static int loop_flush(struct loop_device *lo)
35674  {
35675 -       return loop_switch(lo, NULL);
35676 +       return loop_switch(lo, NULL, NULL);
35677 +}
35678 +
35679 +static struct file *loop_real_file(struct file *file)
35680 +{
35681 +       struct file *f = NULL;
35682 +
35683 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
35684 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
35685 +       return f;
35686  }
35687  
35688  static void loop_reread_partitions(struct loop_device *lo,
35689 @@ -654,6 +666,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35690                           unsigned int arg)
35691  {
35692         struct file     *file, *old_file;
35693 +       struct file     *f, *virt_file = NULL, *old_virt_file;
35694         struct inode    *inode;
35695         int             error;
35696  
35697 @@ -670,9 +683,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35698         file = fget(arg);
35699         if (!file)
35700                 goto out;
35701 +       f = loop_real_file(file);
35702 +       if (f) {
35703 +               virt_file = file;
35704 +               file = f;
35705 +               get_file(file);
35706 +       }
35707  
35708         inode = file->f_mapping->host;
35709         old_file = lo->lo_backing_file;
35710 +       old_virt_file = lo->lo_backing_virt_file;
35711  
35712         error = -EINVAL;
35713  
35714 @@ -684,17 +704,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35715                 goto out_putf;
35716  
35717         /* and ... switch */
35718 -       error = loop_switch(lo, file);
35719 +       error = loop_switch(lo, file, virt_file);
35720         if (error)
35721                 goto out_putf;
35722  
35723         fput(old_file);
35724 +       if (old_virt_file)
35725 +               fput(old_virt_file);
35726         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
35727                 loop_reread_partitions(lo, bdev);
35728         return 0;
35729  
35730   out_putf:
35731         fput(file);
35732 +       if (virt_file)
35733 +               fput(virt_file);
35734   out:
35735         return error;
35736  }
35737 @@ -881,7 +905,7 @@ static int loop_prepare_queue(struct loop_device *lo)
35738  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35739                        struct block_device *bdev, unsigned int arg)
35740  {
35741 -       struct file     *file, *f;
35742 +       struct file     *file, *f, *virt_file = NULL;
35743         struct inode    *inode;
35744         struct address_space *mapping;
35745         unsigned lo_blocksize;
35746 @@ -896,6 +920,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35747         file = fget(arg);
35748         if (!file)
35749                 goto out;
35750 +       f = loop_real_file(file);
35751 +       if (f) {
35752 +               virt_file = file;
35753 +               file = f;
35754 +               get_file(file);
35755 +       }
35756  
35757         error = -EBUSY;
35758         if (lo->lo_state != Lo_unbound)
35759 @@ -948,6 +978,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35760         lo->lo_device = bdev;
35761         lo->lo_flags = lo_flags;
35762         lo->lo_backing_file = file;
35763 +       lo->lo_backing_virt_file = virt_file;
35764         lo->transfer = NULL;
35765         lo->ioctl = NULL;
35766         lo->lo_sizelimit = 0;
35767 @@ -980,6 +1011,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35768  
35769   out_putf:
35770         fput(file);
35771 +       if (virt_file)
35772 +               fput(virt_file);
35773   out:
35774         /* This is safe: open() is still holding a reference. */
35775         module_put(THIS_MODULE);
35776 @@ -1026,6 +1059,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
35777  static int loop_clr_fd(struct loop_device *lo)
35778  {
35779         struct file *filp = lo->lo_backing_file;
35780 +       struct file *virt_filp = lo->lo_backing_virt_file;
35781         gfp_t gfp = lo->old_gfp_mask;
35782         struct block_device *bdev = lo->lo_device;
35783  
35784 @@ -1057,6 +1091,7 @@ static int loop_clr_fd(struct loop_device *lo)
35785         spin_lock_irq(&lo->lo_lock);
35786         lo->lo_state = Lo_rundown;
35787         lo->lo_backing_file = NULL;
35788 +       lo->lo_backing_virt_file = NULL;
35789         spin_unlock_irq(&lo->lo_lock);
35790  
35791         loop_release_xfer(lo);
35792 @@ -1101,6 +1136,8 @@ static int loop_clr_fd(struct loop_device *lo)
35793          * bd_mutex which is usually taken before lo_ctl_mutex.
35794          */
35795         fput(filp);
35796 +       if (virt_filp)
35797 +               fput(virt_filp);
35798         return 0;
35799  }
35800  
35801 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
35802 index fb2237c..c3888c5 100644
35803 --- a/drivers/block/loop.h
35804 +++ b/drivers/block/loop.h
35805 @@ -46,7 +46,7 @@ struct loop_device {
35806         int             (*ioctl)(struct loop_device *, int cmd, 
35807                                  unsigned long arg); 
35808  
35809 -       struct file *   lo_backing_file;
35810 +       struct file *   lo_backing_file, *lo_backing_virt_file;
35811         struct block_device *lo_device;
35812         unsigned        lo_blocksize;
35813         void            *key_data; 
35814 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
35815 index ac1304a..0cefd8a 100644
35816 --- a/fs/aufs/f_op.c
35817 +++ b/fs/aufs/f_op.c
35818 @@ -399,7 +399,7 @@ static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
35819         if (IS_ERR(h_file))
35820                 goto out;
35821  
35822 -       if (au_test_loopback_kthread()) {
35823 +       if (0 && au_test_loopback_kthread()) {
35824                 au_warn_loopback(h_file->f_path.dentry->d_sb);
35825                 if (file->f_mapping != h_file->f_mapping) {
35826                         file->f_mapping = h_file->f_mapping;
35827 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
35828 index 5711e7a..9df5d16 100644
35829 --- a/fs/aufs/loop.c
35830 +++ b/fs/aufs/loop.c
35831 @@ -131,3 +131,19 @@ void au_loopback_fin(void)
35832                 symbol_put(loop_backing_file);
35833         kfree(au_warn_loopback_array);
35834  }
35835 +
35836 +/* ---------------------------------------------------------------------- */
35837 +
35838 +/* support the loopback block device insude aufs */
35839 +
35840 +struct file *aufs_real_loop(struct file *file)
35841 +{
35842 +       struct file *f;
35843 +
35844 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
35845 +       fi_read_lock(file);
35846 +       f = au_hf_top(file);
35847 +       fi_read_unlock(file);
35848 +       AuDebugOn(!f);
35849 +       return f;
35850 +}
35851 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
35852 index 48bf070..66afec7 100644
35853 --- a/fs/aufs/loop.h
35854 +++ b/fs/aufs/loop.h
35855 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
35856  
35857  int au_loopback_init(void);
35858  void au_loopback_fin(void);
35859 +
35860 +struct file *aufs_real_loop(struct file *file);
35861  #else
35862 +AuStub(struct file *, loop_backing_file, return NULL)
35863 +
35864  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
35865            struct dentry *h_adding)
35866  AuStubInt0(au_test_loopback_kthread, void)
35867 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
35868  
35869  AuStubInt0(au_loopback_init, void)
35870  AuStubVoid(au_loopback_fin, void)
35871 +
35872 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
35873  #endif /* BLK_DEV_LOOP */
35874  
35875  #endif /* __KERNEL__ */
35876 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
35877 index 7928a50..9466cc9 100644
35878 --- a/fs/aufs/super.c
35879 +++ b/fs/aufs/super.c
35880 @@ -832,7 +832,10 @@ static const struct super_operations aufs_sop = {
35881         .statfs         = aufs_statfs,
35882         .put_super      = aufs_put_super,
35883         .sync_fs        = aufs_sync_fs,
35884 -       .remount_fs     = aufs_remount_fs
35885 +       .remount_fs     = aufs_remount_fs,
35886 +#ifdef CONFIG_AUFS_BDEV_LOOP
35887 +       .real_loop      = aufs_real_loop
35888 +#endif
35889  };
35890  
35891  /* ---------------------------------------------------------------------- */
35892 diff --git a/include/linux/fs.h b/include/linux/fs.h
35893 index 99fc2bd..842a2e0 100644
35894 --- a/include/linux/fs.h
35895 +++ b/include/linux/fs.h
35896 @@ -1759,6 +1759,10 @@ struct super_operations {
35897                                   struct shrink_control *);
35898         long (*free_cached_objects)(struct super_block *,
35899                                     struct shrink_control *);
35900 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
35901 +       /* and aufs */
35902 +       struct file *(*real_loop)(struct file *);
35903 +#endif
35904  };
35905  
35906  /*
This page took 2.517312 seconds and 4 git commands to generate.