]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- fetch 4.12
[packages/kernel.git] / kernel-aufs4.patch
1 aufs4.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index b0e42b6..7fa4b68 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -249,6 +249,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 7bbaca9..a026491 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -128,3 +128,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 aufs4.x-rcN base patch
25
26 diff --git a/MAINTAINERS b/MAINTAINERS
27 index 09b5ab6..6640ab1 100644
28 --- a/MAINTAINERS
29 +++ b/MAINTAINERS
30 @@ -2348,6 +2348,19 @@ F:       include/linux/audit.h
31  F:     include/uapi/linux/audit.h
32  F:     kernel/audit*
33  
34 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
35 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
36 +L:     linux-unionfs@vger.kernel.org
37 +L:     aufs-users@lists.sourceforge.net (members only)
38 +W:     http://aufs.sourceforge.net
39 +T:     git://github.com/sfjro/aufs4-linux.git
40 +S:     Supported
41 +F:     Documentation/filesystems/aufs/
42 +F:     Documentation/ABI/testing/debugfs-aufs
43 +F:     Documentation/ABI/testing/sysfs-aufs
44 +F:     fs/aufs/
45 +F:     include/uapi/linux/aufs_type.h
46 +
47  AUXILIARY DISPLAY DRIVERS
48  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
49  W:     http://miguelojeda.es/auxdisplay.htm
50 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
51 index ebbd0c3..6b8a6a4c 100644
52 --- a/drivers/block/loop.c
53 +++ b/drivers/block/loop.c
54 @@ -700,6 +700,24 @@ static inline int is_loop_device(struct file *file)
55         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
56  }
57  
58 +/*
59 + * for AUFS
60 + * no get/put for file.
61 + */
62 +struct file *loop_backing_file(struct super_block *sb)
63 +{
64 +       struct file *ret;
65 +       struct loop_device *l;
66 +
67 +       ret = NULL;
68 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
69 +               l = sb->s_bdev->bd_disk->private_data;
70 +               ret = l->lo_backing_file;
71 +       }
72 +       return ret;
73 +}
74 +EXPORT_SYMBOL_GPL(loop_backing_file);
75 +
76  /* loop sysfs attributes */
77  
78  static ssize_t loop_attr_show(struct device *dev, char *page,
79 diff --git a/fs/dcache.c b/fs/dcache.c
80 index a9f995f..3458af5 100644
81 --- a/fs/dcache.c
82 +++ b/fs/dcache.c
83 @@ -1164,7 +1164,7 @@ enum d_walk_ret {
84   *
85   * The @enter() and @finish() callbacks are called with d_lock held.
86   */
87 -static void d_walk(struct dentry *parent, void *data,
88 +void d_walk(struct dentry *parent, void *data,
89                    enum d_walk_ret (*enter)(void *, struct dentry *),
90                    void (*finish)(void *))
91  {
92 diff --git a/fs/fcntl.c b/fs/fcntl.c
93 index f4e7267..d39404c 100644
94 --- a/fs/fcntl.c
95 +++ b/fs/fcntl.c
96 @@ -31,7 +31,7 @@
97  
98  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
99  
100 -static int setfl(int fd, struct file * filp, unsigned long arg)
101 +int setfl(int fd, struct file * filp, unsigned long arg)
102  {
103         struct inode * inode = file_inode(filp);
104         int error = 0;
105 @@ -62,6 +62,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
106  
107         if (filp->f_op->check_flags)
108                 error = filp->f_op->check_flags(arg);
109 +       if (!error && filp->f_op->setfl)
110 +               error = filp->f_op->setfl(filp, arg);
111         if (error)
112                 return error;
113  
114 diff --git a/fs/inode.c b/fs/inode.c
115 index db59147..69cbe9c 100644
116 --- a/fs/inode.c
117 +++ b/fs/inode.c
118 @@ -1640,7 +1640,7 @@ EXPORT_SYMBOL(generic_update_time);
119   * This does the actual work of updating an inodes time or version.  Must have
120   * had called mnt_want_write() before calling this.
121   */
122 -static int update_time(struct inode *inode, struct timespec *time, int flags)
123 +int update_time(struct inode *inode, struct timespec *time, int flags)
124  {
125         int (*update_time)(struct inode *, struct timespec *, int);
126  
127 diff --git a/fs/read_write.c b/fs/read_write.c
128 index 19d4d88..05033f7 100644
129 --- a/fs/read_write.c
130 +++ b/fs/read_write.c
131 @@ -513,6 +513,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
132  }
133  EXPORT_SYMBOL(__vfs_write);
134  
135 +vfs_readf_t vfs_readf(struct file *file)
136 +{
137 +       const struct file_operations *fop = file->f_op;
138 +
139 +       if (fop->read)
140 +               return fop->read;
141 +       if (fop->read_iter)
142 +               return new_sync_read;
143 +       return ERR_PTR(-ENOSYS);
144 +}
145 +
146 +vfs_writef_t vfs_writef(struct file *file)
147 +{
148 +       const struct file_operations *fop = file->f_op;
149 +
150 +       if (fop->write)
151 +               return fop->write;
152 +       if (fop->write_iter)
153 +               return new_sync_write;
154 +       return ERR_PTR(-ENOSYS);
155 +}
156 +
157  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
158  {
159         mm_segment_t old_fs;
160 diff --git a/fs/splice.c b/fs/splice.c
161 index 540c4a4..4a0bc62 100644
162 --- a/fs/splice.c
163 +++ b/fs/splice.c
164 @@ -853,8 +853,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
165  /*
166   * Attempt to initiate a splice from pipe to file.
167   */
168 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
169 -                          loff_t *ppos, size_t len, unsigned int flags)
170 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
171 +                   loff_t *ppos, size_t len, unsigned int flags)
172  {
173         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
174                                 loff_t *, size_t, unsigned int);
175 @@ -870,9 +870,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
176  /*
177   * Attempt to initiate a splice from a file to a pipe.
178   */
179 -static long do_splice_to(struct file *in, loff_t *ppos,
180 -                        struct pipe_inode_info *pipe, size_t len,
181 -                        unsigned int flags)
182 +long do_splice_to(struct file *in, loff_t *ppos,
183 +                 struct pipe_inode_info *pipe, size_t len,
184 +                 unsigned int flags)
185  {
186         ssize_t (*splice_read)(struct file *, loff_t *,
187                                struct pipe_inode_info *, size_t, unsigned int);
188 diff --git a/fs/sync.c b/fs/sync.c
189 index 11ba023..abf6a5d 100644
190 --- a/fs/sync.c
191 +++ b/fs/sync.c
192 @@ -27,7 +27,7 @@
193   * wait == 1 case since in that case write_inode() functions do
194   * sync_dirty_buffer() and thus effectively write one block at a time.
195   */
196 -static int __sync_filesystem(struct super_block *sb, int wait)
197 +int __sync_filesystem(struct super_block *sb, int wait)
198  {
199         if (wait)
200                 sync_inodes_sb(sb);
201 diff --git a/include/linux/file.h b/include/linux/file.h
202 index 61eb82c..e700888 100644
203 --- a/include/linux/file.h
204 +++ b/include/linux/file.h
205 @@ -19,6 +19,7 @@ struct dentry;
206  struct path;
207  extern struct file *alloc_file(const struct path *, fmode_t mode,
208         const struct file_operations *fop);
209 +extern struct file *get_empty_filp(void);
210  
211  static inline void fput_light(struct file *file, int fput_needed)
212  {
213 diff --git a/include/linux/fs.h b/include/linux/fs.h
214 index 803e5a9..22d2910 100644
215 --- a/include/linux/fs.h
216 +++ b/include/linux/fs.h
217 @@ -1248,6 +1248,7 @@ extern void fasync_free(struct fasync_struct *);
218  /* can be called from interrupts */
219  extern void kill_fasync(struct fasync_struct **, int, int);
220  
221 +extern int setfl(int fd, struct file * filp, unsigned long arg);
222  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
223  extern void f_setown(struct file *filp, unsigned long arg, int force);
224  extern void f_delown(struct file *filp);
225 @@ -1674,6 +1675,7 @@ struct file_operations {
226         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
227         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
228         int (*check_flags)(int);
229 +       int (*setfl)(struct file *, unsigned long);
230         int (*flock) (struct file *, int, struct file_lock *);
231         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
232         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
233 @@ -1750,6 +1752,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
234                               struct iovec *fast_pointer,
235                               struct iovec **ret_pointer);
236  
237 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
238 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
239 +                               loff_t *);
240 +vfs_readf_t vfs_readf(struct file *file);
241 +vfs_writef_t vfs_writef(struct file *file);
242 +
243  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
244  extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
245  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
246 @@ -2131,6 +2139,7 @@ extern int current_umask(void);
247  extern void ihold(struct inode * inode);
248  extern void iput(struct inode *);
249  extern int generic_update_time(struct inode *, struct timespec *, int);
250 +extern int update_time(struct inode *, struct timespec *, int);
251  
252  /* /sys/fs */
253  extern struct kobject *fs_kobj;
254 @@ -2411,6 +2420,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
255         return false;
256  }
257  #endif
258 +extern int __sync_filesystem(struct super_block *, int);
259  extern int sync_filesystem(struct super_block *);
260  extern const struct file_operations def_blk_fops;
261  extern const struct file_operations def_chr_fops;
262 diff --git a/include/linux/splice.h b/include/linux/splice.h
263 index db42746..12f3a5a 100644
264 --- a/include/linux/splice.h
265 +++ b/include/linux/splice.h
266 @@ -86,4 +86,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
267  
268  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
269  extern const struct pipe_buf_operations default_pipe_buf_ops;
270 +
271 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
272 +                          loff_t *ppos, size_t len, unsigned int flags);
273 +extern long do_splice_to(struct file *in, loff_t *ppos,
274 +                        struct pipe_inode_info *pipe, size_t len,
275 +                        unsigned int flags);
276  #endif
277 aufs4.x-rcN mmap patch
278
279 diff --git a/fs/proc/base.c b/fs/proc/base.c
280 index f1e1927..7b5af0c 100644
281 --- a/fs/proc/base.c
282 +++ b/fs/proc/base.c
283 @@ -1943,7 +1943,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
284         down_read(&mm->mmap_sem);
285         vma = find_exact_vma(mm, vm_start, vm_end);
286         if (vma && vma->vm_file) {
287 -               *path = vma->vm_file->f_path;
288 +               *path = vma_pr_or_file(vma)->f_path;
289                 path_get(path);
290                 rc = 0;
291         }
292 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
293 index 7563437..7c0dc0f 100644
294 --- a/fs/proc/nommu.c
295 +++ b/fs/proc/nommu.c
296 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
297         file = region->vm_file;
298  
299         if (file) {
300 -               struct inode *inode = file_inode(region->vm_file);
301 +               struct inode *inode;
302 +
303 +               file = vmr_pr_or_file(region);
304 +               inode = file_inode(file);
305                 dev = inode->i_sb->s_dev;
306                 ino = inode->i_ino;
307         }
308 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
309 index 520802d..61a037d 100644
310 --- a/fs/proc/task_mmu.c
311 +++ b/fs/proc/task_mmu.c
312 @@ -292,7 +292,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
313         const char *name = NULL;
314  
315         if (file) {
316 -               struct inode *inode = file_inode(vma->vm_file);
317 +               struct inode *inode;
318 +
319 +               file = vma_pr_or_file(vma);
320 +               inode = file_inode(file);
321                 dev = inode->i_sb->s_dev;
322                 ino = inode->i_ino;
323                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
324 @@ -1638,7 +1641,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
325         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
326         struct vm_area_struct *vma = v;
327         struct numa_maps *md = &numa_priv->md;
328 -       struct file *file = vma->vm_file;
329 +       struct file *file = vma_pr_or_file(vma);
330         struct mm_struct *mm = vma->vm_mm;
331         struct mm_walk walk = {
332                 .hugetlb_entry = gather_hugetlb_stats,
333 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
334 index 23266694..58e59b6 100644
335 --- a/fs/proc/task_nommu.c
336 +++ b/fs/proc/task_nommu.c
337 @@ -157,7 +157,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
338         file = vma->vm_file;
339  
340         if (file) {
341 -               struct inode *inode = file_inode(vma->vm_file);
342 +               struct inode *inode;
343 +
344 +               file = vma_pr_or_file(vma);
345 +               inode = file_inode(file);
346                 dev = inode->i_sb->s_dev;
347                 ino = inode->i_ino;
348                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
349 diff --git a/include/linux/mm.h b/include/linux/mm.h
350 index 6f543a4..696494b 100644
351 --- a/include/linux/mm.h
352 +++ b/include/linux/mm.h
353 @@ -1306,6 +1306,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
354  }
355  #endif
356  
357 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
358 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
359 +                                     int);
360 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
361 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
362 +
363 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
364 +                                                               __LINE__)
365 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
366 +                                                         __LINE__)
367 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
368 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
369 +
370 +#ifndef CONFIG_MMU
371 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
372 +extern void vmr_do_fput(struct vm_region *, const char[], int);
373 +
374 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
375 +                                                         __LINE__)
376 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
377 +#endif /* !CONFIG_MMU */
378 +
379  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
380                 unsigned int gup_flags);
381  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
382 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
383 index 45cdb27..1a40012 100644
384 --- a/include/linux/mm_types.h
385 +++ b/include/linux/mm_types.h
386 @@ -259,6 +259,7 @@ struct vm_region {
387         unsigned long   vm_top;         /* region allocated to here */
388         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
389         struct file     *vm_file;       /* the backing file or NULL */
390 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
391  
392         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
393         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
394 @@ -333,6 +334,7 @@ struct vm_area_struct {
395         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
396                                            units */
397         struct file * vm_file;          /* File we map to (can be NULL). */
398 +       struct file *vm_prfile;         /* shadow of vm_file */
399         void * vm_private_data;         /* was vm_pte (shared mem) */
400  
401  #ifndef CONFIG_MMU
402 diff --git a/kernel/fork.c b/kernel/fork.c
403 index e53770d..cdf7516 100644
404 --- a/kernel/fork.c
405 +++ b/kernel/fork.c
406 @@ -665,7 +665,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
407                         struct inode *inode = file_inode(file);
408                         struct address_space *mapping = file->f_mapping;
409  
410 -                       get_file(file);
411 +                       vma_get_file(tmp);
412                         if (tmp->vm_flags & VM_DENYWRITE)
413                                 atomic_dec(&inode->i_writecount);
414                         i_mmap_lock_write(mapping);
415 diff --git a/mm/Makefile b/mm/Makefile
416 index 026f6a8..723da17 100644
417 --- a/mm/Makefile
418 +++ b/mm/Makefile
419 @@ -39,7 +39,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
420                            mm_init.o mmu_context.o percpu.o slab_common.o \
421                            compaction.o vmacache.o swap_slots.o \
422                            interval_tree.o list_lru.o workingset.o \
423 -                          debug.o $(mmu-y)
424 +                          prfile.o debug.o $(mmu-y)
425  
426  obj-y += init-mm.o
427  
428 diff --git a/mm/filemap.c b/mm/filemap.c
429 index 6f1be57..69a8d94 100644
430 --- a/mm/filemap.c
431 +++ b/mm/filemap.c
432 @@ -2408,7 +2408,7 @@ int filemap_page_mkwrite(struct vm_fault *vmf)
433         int ret = VM_FAULT_LOCKED;
434  
435         sb_start_pagefault(inode->i_sb);
436 -       file_update_time(vmf->vma->vm_file);
437 +       vma_file_update_time(vmf->vma);
438         lock_page(page);
439         if (page->mapping != inode->i_mapping) {
440                 unlock_page(page);
441 diff --git a/mm/mmap.c b/mm/mmap.c
442 index a5e3dcd..a5d908c 100644
443 --- a/mm/mmap.c
444 +++ b/mm/mmap.c
445 @@ -170,7 +170,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
446         if (vma->vm_ops && vma->vm_ops->close)
447                 vma->vm_ops->close(vma);
448         if (vma->vm_file)
449 -               fput(vma->vm_file);
450 +               vma_fput(vma);
451         mpol_put(vma_policy(vma));
452         kmem_cache_free(vm_area_cachep, vma);
453         return next;
454 @@ -895,7 +895,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
455         if (remove_next) {
456                 if (file) {
457                         uprobe_munmap(next, next->vm_start, next->vm_end);
458 -                       fput(file);
459 +                       vma_fput(vma);
460                 }
461                 if (next->anon_vma)
462                         anon_vma_merge(vma, next);
463 @@ -1745,8 +1745,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
464         return addr;
465  
466  unmap_and_free_vma:
467 +       vma_fput(vma);
468         vma->vm_file = NULL;
469 -       fput(file);
470  
471         /* Undo any partial mapping done by a device driver. */
472         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
473 @@ -2571,7 +2571,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
474                 goto out_free_mpol;
475  
476         if (new->vm_file)
477 -               get_file(new->vm_file);
478 +               vma_get_file(new);
479  
480         if (new->vm_ops && new->vm_ops->open)
481                 new->vm_ops->open(new);
482 @@ -2590,7 +2590,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
483         if (new->vm_ops && new->vm_ops->close)
484                 new->vm_ops->close(new);
485         if (new->vm_file)
486 -               fput(new->vm_file);
487 +               vma_fput(new);
488         unlink_anon_vmas(new);
489   out_free_mpol:
490         mpol_put(vma_policy(new));
491 @@ -2744,7 +2744,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
492         struct vm_area_struct *vma;
493         unsigned long populate = 0;
494         unsigned long ret = -EINVAL;
495 -       struct file *file;
496 +       struct file *file, *prfile;
497  
498         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
499                      current->comm, current->pid);
500 @@ -2819,10 +2819,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
501                 }
502         }
503  
504 -       file = get_file(vma->vm_file);
505 +       vma_get_file(vma);
506 +       file = vma->vm_file;
507 +       prfile = vma->vm_prfile;
508         ret = do_mmap_pgoff(vma->vm_file, start, size,
509                         prot, flags, pgoff, &populate, NULL);
510 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
511 +               struct vm_area_struct *new_vma;
512 +
513 +               new_vma = find_vma(mm, ret);
514 +               if (!new_vma->vm_prfile)
515 +                       new_vma->vm_prfile = prfile;
516 +               if (new_vma != vma)
517 +                       get_file(prfile);
518 +       }
519 +       /*
520 +        * two fput()s instead of vma_fput(vma),
521 +        * coz vma may not be available anymore.
522 +        */
523         fput(file);
524 +       if (prfile)
525 +               fput(prfile);
526  out:
527         up_write(&mm->mmap_sem);
528         if (populate)
529 @@ -3113,7 +3130,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
530                 if (anon_vma_clone(new_vma, vma))
531                         goto out_free_mempol;
532                 if (new_vma->vm_file)
533 -                       get_file(new_vma->vm_file);
534 +                       vma_get_file(new_vma);
535                 if (new_vma->vm_ops && new_vma->vm_ops->open)
536                         new_vma->vm_ops->open(new_vma);
537                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
538 diff --git a/mm/nommu.c b/mm/nommu.c
539 index fc184f5..637ea81 100644
540 --- a/mm/nommu.c
541 +++ b/mm/nommu.c
542 @@ -641,7 +641,7 @@ static void __put_nommu_region(struct vm_region *region)
543                 up_write(&nommu_region_sem);
544  
545                 if (region->vm_file)
546 -                       fput(region->vm_file);
547 +                       vmr_fput(region);
548  
549                 /* IO memory and memory shared directly out of the pagecache
550                  * from ramfs/tmpfs mustn't be released here */
551 @@ -799,7 +799,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
552         if (vma->vm_ops && vma->vm_ops->close)
553                 vma->vm_ops->close(vma);
554         if (vma->vm_file)
555 -               fput(vma->vm_file);
556 +               vma_fput(vma);
557         put_nommu_region(vma->vm_region);
558         kmem_cache_free(vm_area_cachep, vma);
559  }
560 @@ -1326,7 +1326,7 @@ unsigned long do_mmap(struct file *file,
561                                         goto error_just_free;
562                                 }
563                         }
564 -                       fput(region->vm_file);
565 +                       vmr_fput(region);
566                         kmem_cache_free(vm_region_jar, region);
567                         region = pregion;
568                         result = start;
569 @@ -1401,10 +1401,10 @@ unsigned long do_mmap(struct file *file,
570         up_write(&nommu_region_sem);
571  error:
572         if (region->vm_file)
573 -               fput(region->vm_file);
574 +               vmr_fput(region);
575         kmem_cache_free(vm_region_jar, region);
576         if (vma->vm_file)
577 -               fput(vma->vm_file);
578 +               vma_fput(vma);
579         kmem_cache_free(vm_area_cachep, vma);
580         return ret;
581  
582 diff --git a/mm/prfile.c b/mm/prfile.c
583 new file mode 100644
584 index 0000000..1ef053b
585 --- /dev/null
586 +++ b/mm/prfile.c
587 @@ -0,0 +1,85 @@
588 +/*
589 + * Mainly for aufs which mmap(2) different file and wants to print different
590 + * path in /proc/PID/maps.
591 + * Call these functions via macros defined in linux/mm.h.
592 + *
593 + * See Documentation/filesystems/aufs/design/06mmap.txt
594 + *
595 + * Copyright (c) 2014-2017 Junjro R. Okajima
596 + * Copyright (c) 2014 Ian Campbell
597 + */
598 +
599 +#include <linux/mm.h>
600 +#include <linux/file.h>
601 +#include <linux/fs.h>
602 +
603 +/* #define PRFILE_TRACE */
604 +static inline void prfile_trace(struct file *f, struct file *pr,
605 +                             const char func[], int line, const char func2[])
606 +{
607 +#ifdef PRFILE_TRACE
608 +       if (pr)
609 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
610 +#endif
611 +}
612 +
613 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
614 +                            int line)
615 +{
616 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
617 +
618 +       prfile_trace(f, pr, func, line, __func__);
619 +       file_update_time(f);
620 +       if (f && pr)
621 +               file_update_time(pr);
622 +}
623 +
624 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
625 +                              int line)
626 +{
627 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
628 +
629 +       prfile_trace(f, pr, func, line, __func__);
630 +       return (f && pr) ? pr : f;
631 +}
632 +
633 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
634 +{
635 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
636 +
637 +       prfile_trace(f, pr, func, line, __func__);
638 +       get_file(f);
639 +       if (f && pr)
640 +               get_file(pr);
641 +}
642 +
643 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
644 +{
645 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
646 +
647 +       prfile_trace(f, pr, func, line, __func__);
648 +       fput(f);
649 +       if (f && pr)
650 +               fput(pr);
651 +}
652 +
653 +#ifndef CONFIG_MMU
654 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
655 +                              int line)
656 +{
657 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
658 +
659 +       prfile_trace(f, pr, func, line, __func__);
660 +       return (f && pr) ? pr : f;
661 +}
662 +
663 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
664 +{
665 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
666 +
667 +       prfile_trace(f, pr, func, line, __func__);
668 +       fput(f);
669 +       if (f && pr)
670 +               fput(pr);
671 +}
672 +#endif /* !CONFIG_MMU */
673 aufs4.x-rcN standalone patch
674
675 diff --git a/fs/dcache.c b/fs/dcache.c
676 index 3458af5..5fd25bb 100644
677 --- a/fs/dcache.c
678 +++ b/fs/dcache.c
679 @@ -1272,6 +1272,7 @@ void d_walk(struct dentry *parent, void *data,
680         seq = 1;
681         goto again;
682  }
683 +EXPORT_SYMBOL_GPL(d_walk);
684  
685  struct check_mount {
686         struct vfsmount *mnt;
687 @@ -2862,6 +2863,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
688  
689         write_sequnlock(&rename_lock);
690  }
691 +EXPORT_SYMBOL_GPL(d_exchange);
692  
693  /**
694   * d_ancestor - search for an ancestor
695 diff --git a/fs/exec.c b/fs/exec.c
696 index 9041990..31f14c6 100644
697 --- a/fs/exec.c
698 +++ b/fs/exec.c
699 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
700         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
701                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
702  }
703 +EXPORT_SYMBOL_GPL(path_noexec);
704  
705  #ifdef CONFIG_USELIB
706  /*
707 diff --git a/fs/fcntl.c b/fs/fcntl.c
708 index d39404c..ac1dc66 100644
709 --- a/fs/fcntl.c
710 +++ b/fs/fcntl.c
711 @@ -84,6 +84,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
712   out:
713         return error;
714  }
715 +EXPORT_SYMBOL_GPL(setfl);
716  
717  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
718                       int force)
719 diff --git a/fs/file_table.c b/fs/file_table.c
720 index 954d510..4fb5b10 100644
721 --- a/fs/file_table.c
722 +++ b/fs/file_table.c
723 @@ -148,6 +148,7 @@ struct file *get_empty_filp(void)
724         }
725         return ERR_PTR(-ENFILE);
726  }
727 +EXPORT_SYMBOL_GPL(get_empty_filp);
728  
729  /**
730   * alloc_file - allocate and initialize a 'struct file'
731 @@ -259,6 +260,7 @@ void flush_delayed_fput(void)
732  {
733         delayed_fput(NULL);
734  }
735 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
736  
737  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
738  
739 @@ -301,6 +303,7 @@ void __fput_sync(struct file *file)
740  }
741  
742  EXPORT_SYMBOL(fput);
743 +EXPORT_SYMBOL_GPL(__fput_sync);
744  
745  void put_filp(struct file *file)
746  {
747 @@ -309,6 +312,7 @@ void put_filp(struct file *file)
748                 file_free(file);
749         }
750  }
751 +EXPORT_SYMBOL_GPL(put_filp);
752  
753  void __init files_init(void)
754  { 
755 diff --git a/fs/inode.c b/fs/inode.c
756 index 69cbe9c..764566d 100644
757 --- a/fs/inode.c
758 +++ b/fs/inode.c
759 @@ -1649,6 +1649,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
760  
761         return update_time(inode, time, flags);
762  }
763 +EXPORT_SYMBOL_GPL(update_time);
764  
765  /**
766   *     touch_atime     -       update the access time
767 diff --git a/fs/namespace.c b/fs/namespace.c
768 index 5a44384..cc6f6fb 100644
769 --- a/fs/namespace.c
770 +++ b/fs/namespace.c
771 @@ -462,6 +462,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
772         mnt_dec_writers(real_mount(mnt));
773         preempt_enable();
774  }
775 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
776  
777  /**
778   * mnt_drop_write - give up write access to a mount
779 @@ -1881,6 +1882,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
780         }
781         return 0;
782  }
783 +EXPORT_SYMBOL_GPL(iterate_mounts);
784  
785  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
786  {
787 diff --git a/fs/notify/group.c b/fs/notify/group.c
788 index 3235753..14a2d48 100644
789 --- a/fs/notify/group.c
790 +++ b/fs/notify/group.c
791 @@ -22,6 +22,7 @@
792  #include <linux/srcu.h>
793  #include <linux/rculist.h>
794  #include <linux/wait.h>
795 +#include <linux/module.h>
796  
797  #include <linux/fsnotify_backend.h>
798  #include "fsnotify.h"
799 @@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
800  {
801         atomic_inc(&group->refcnt);
802  }
803 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
804  
805  /*
806   * Drop a reference to a group.  Free it if it's through.
807 @@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
808         if (atomic_dec_and_test(&group->refcnt))
809                 fsnotify_final_destroy_group(group);
810  }
811 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
812  
813  /*
814   * Create a new fsnotify_group and hold a reference for the group returned.
815 @@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
816  
817         return group;
818  }
819 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
820  
821  int fsnotify_fasync(int fd, struct file *file, int on)
822  {
823 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
824 index 9991f88..117042c 100644
825 --- a/fs/notify/mark.c
826 +++ b/fs/notify/mark.c
827 @@ -118,6 +118,7 @@ static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
828  {
829         return atomic_inc_not_zero(&mark->refcnt);
830  }
831 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
832  
833  static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
834  {
835 @@ -395,6 +396,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
836         mutex_unlock(&group->mark_mutex);
837         fsnotify_free_mark(mark);
838  }
839 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
840  
841  /*
842   * Sorting function for lists of fsnotify marks.
843 @@ -607,6 +609,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
844         fsnotify_put_mark(mark);
845         return ret;
846  }
847 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
848  
849  int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
850                       struct vfsmount *mnt, int allow_dups)
851 @@ -742,6 +745,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
852         fsnotify_get_group(group);
853         mark->group = group;
854  }
855 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
856  
857  /*
858   * Destroy all marks in destroy_list, waits for SRCU period to finish before
859 diff --git a/fs/open.c b/fs/open.c
860 index cd0c5be..491442a 100644
861 --- a/fs/open.c
862 +++ b/fs/open.c
863 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
864         inode_unlock(dentry->d_inode);
865         return ret;
866  }
867 +EXPORT_SYMBOL_GPL(do_truncate);
868  
869  long vfs_truncate(const struct path *path, loff_t length)
870  {
871 @@ -691,6 +692,7 @@ int open_check_o_direct(struct file *f)
872         }
873         return 0;
874  }
875 +EXPORT_SYMBOL_GPL(open_check_o_direct);
876  
877  static int do_dentry_open(struct file *f,
878                           struct inode *inode,
879 diff --git a/fs/read_write.c b/fs/read_write.c
880 index 05033f7..ce062e8 100644
881 --- a/fs/read_write.c
882 +++ b/fs/read_write.c
883 @@ -523,6 +523,7 @@ vfs_readf_t vfs_readf(struct file *file)
884                 return new_sync_read;
885         return ERR_PTR(-ENOSYS);
886  }
887 +EXPORT_SYMBOL_GPL(vfs_readf);
888  
889  vfs_writef_t vfs_writef(struct file *file)
890  {
891 @@ -534,6 +535,7 @@ vfs_writef_t vfs_writef(struct file *file)
892                 return new_sync_write;
893         return ERR_PTR(-ENOSYS);
894  }
895 +EXPORT_SYMBOL_GPL(vfs_writef);
896  
897  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
898  {
899 diff --git a/fs/splice.c b/fs/splice.c
900 index 4a0bc62..21d7893 100644
901 --- a/fs/splice.c
902 +++ b/fs/splice.c
903 @@ -866,6 +866,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
904  
905         return splice_write(pipe, out, ppos, len, flags);
906  }
907 +EXPORT_SYMBOL_GPL(do_splice_from);
908  
909  /*
910   * Attempt to initiate a splice from a file to a pipe.
911 @@ -895,6 +896,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
912  
913         return splice_read(in, ppos, pipe, len, flags);
914  }
915 +EXPORT_SYMBOL_GPL(do_splice_to);
916  
917  /**
918   * splice_direct_to_actor - splices data directly between two non-pipes
919 diff --git a/fs/sync.c b/fs/sync.c
920 index abf6a5d..c86fe9c 100644
921 --- a/fs/sync.c
922 +++ b/fs/sync.c
923 @@ -38,6 +38,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
924                 sb->s_op->sync_fs(sb, wait);
925         return __sync_blockdev(sb->s_bdev, wait);
926  }
927 +EXPORT_SYMBOL_GPL(__sync_filesystem);
928  
929  /*
930   * Write out and wait upon all dirty data associated with this
931 diff --git a/fs/xattr.c b/fs/xattr.c
932 index 464c94b..0234d49 100644
933 --- a/fs/xattr.c
934 +++ b/fs/xattr.c
935 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
936         *xattr_value = value;
937         return error;
938  }
939 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
940  
941  ssize_t
942  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
943 diff --git a/kernel/task_work.c b/kernel/task_work.c
944 index d513051..e056d54 100644
945 --- a/kernel/task_work.c
946 +++ b/kernel/task_work.c
947 @@ -119,3 +119,4 @@ void task_work_run(void)
948                 } while (work);
949         }
950  }
951 +EXPORT_SYMBOL_GPL(task_work_run);
952 diff --git a/security/commoncap.c b/security/commoncap.c
953 index 7abebd7..c079ce4 100644
954 --- a/security/commoncap.c
955 +++ b/security/commoncap.c
956 @@ -1062,12 +1062,14 @@ int cap_mmap_addr(unsigned long addr)
957         }
958         return ret;
959  }
960 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
961  
962  int cap_mmap_file(struct file *file, unsigned long reqprot,
963                   unsigned long prot, unsigned long flags)
964  {
965         return 0;
966  }
967 +EXPORT_SYMBOL_GPL(cap_mmap_file);
968  
969  #ifdef CONFIG_SECURITY
970  
971 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
972 index 03c1652..f88c84b 100644
973 --- a/security/device_cgroup.c
974 +++ b/security/device_cgroup.c
975 @@ -7,6 +7,7 @@
976  #include <linux/device_cgroup.h>
977  #include <linux/cgroup.h>
978  #include <linux/ctype.h>
979 +#include <linux/export.h>
980  #include <linux/list.h>
981  #include <linux/uaccess.h>
982  #include <linux/seq_file.h>
983 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
984         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
985                         access);
986  }
987 +EXPORT_SYMBOL_GPL(__devcgroup_inode_permission);
988  
989  int devcgroup_inode_mknod(int mode, dev_t dev)
990  {
991 diff --git a/security/security.c b/security/security.c
992 index b9fea39..afa97dd 100644
993 --- a/security/security.c
994 +++ b/security/security.c
995 @@ -492,6 +492,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
996                 return 0;
997         return call_int_hook(path_rmdir, 0, dir, dentry);
998  }
999 +EXPORT_SYMBOL_GPL(security_path_rmdir);
1000  
1001  int security_path_unlink(const struct path *dir, struct dentry *dentry)
1002  {
1003 @@ -508,6 +509,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
1004                 return 0;
1005         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1006  }
1007 +EXPORT_SYMBOL_GPL(security_path_symlink);
1008  
1009  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1010                        struct dentry *new_dentry)
1011 @@ -516,6 +518,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1012                 return 0;
1013         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1014  }
1015 +EXPORT_SYMBOL_GPL(security_path_link);
1016  
1017  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1018                          const struct path *new_dir, struct dentry *new_dentry,
1019 @@ -543,6 +546,7 @@ int security_path_truncate(const struct path *path)
1020                 return 0;
1021         return call_int_hook(path_truncate, 0, path);
1022  }
1023 +EXPORT_SYMBOL_GPL(security_path_truncate);
1024  
1025  int security_path_chmod(const struct path *path, umode_t mode)
1026  {
1027 @@ -550,6 +554,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1028                 return 0;
1029         return call_int_hook(path_chmod, 0, path, mode);
1030  }
1031 +EXPORT_SYMBOL_GPL(security_path_chmod);
1032  
1033  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1034  {
1035 @@ -557,6 +562,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1036                 return 0;
1037         return call_int_hook(path_chown, 0, path, uid, gid);
1038  }
1039 +EXPORT_SYMBOL_GPL(security_path_chown);
1040  
1041  int security_path_chroot(const struct path *path)
1042  {
1043 @@ -642,6 +648,7 @@ int security_inode_readlink(struct dentry *dentry)
1044                 return 0;
1045         return call_int_hook(inode_readlink, 0, dentry);
1046  }
1047 +EXPORT_SYMBOL_GPL(security_inode_readlink);
1048  
1049  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1050                                bool rcu)
1051 @@ -657,6 +664,7 @@ int security_inode_permission(struct inode *inode, int mask)
1052                 return 0;
1053         return call_int_hook(inode_permission, 0, inode, mask);
1054  }
1055 +EXPORT_SYMBOL_GPL(security_inode_permission);
1056  
1057  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1058  {
1059 @@ -828,6 +836,7 @@ int security_file_permission(struct file *file, int mask)
1060  
1061         return fsnotify_perm(file, mask);
1062  }
1063 +EXPORT_SYMBOL_GPL(security_file_permission);
1064  
1065  int security_file_alloc(struct file *file)
1066  {
1067 @@ -887,6 +896,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1068                 return ret;
1069         return ima_file_mmap(file, prot);
1070  }
1071 +EXPORT_SYMBOL_GPL(security_mmap_file);
1072  
1073  int security_mmap_addr(unsigned long addr)
1074  {
1075 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1076 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1077 +++ linux/Documentation/ABI/testing/debugfs-aufs        2017-07-29 12:14:25.893041746 +0200
1078 @@ -0,0 +1,50 @@
1079 +What:          /debug/aufs/si_<id>/
1080 +Date:          March 2009
1081 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1082 +Description:
1083 +               Under /debug/aufs, a directory named si_<id> is created
1084 +               per aufs mount, where <id> is a unique id generated
1085 +               internally.
1086 +
1087 +What:          /debug/aufs/si_<id>/plink
1088 +Date:          Apr 2013
1089 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1090 +Description:
1091 +               It has three lines and shows the information about the
1092 +               pseudo-link. The first line is a single number
1093 +               representing a number of buckets. The second line is a
1094 +               number of pseudo-links per buckets (separated by a
1095 +               blank). The last line is a single number representing a
1096 +               total number of psedo-links.
1097 +               When the aufs mount option 'noplink' is specified, it
1098 +               will show "1\n0\n0\n".
1099 +
1100 +What:          /debug/aufs/si_<id>/xib
1101 +Date:          March 2009
1102 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1103 +Description:
1104 +               It shows the consumed blocks by xib (External Inode Number
1105 +               Bitmap), its block size and file size.
1106 +               When the aufs mount option 'noxino' is specified, it
1107 +               will be empty. About XINO files, see the aufs manual.
1108 +
1109 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1110 +Date:          March 2009
1111 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1112 +Description:
1113 +               It shows the consumed blocks by xino (External Inode Number
1114 +               Translation Table), its link count, block size and file
1115 +               size.
1116 +               When the aufs mount option 'noxino' is specified, it
1117 +               will be empty. About XINO files, see the aufs manual.
1118 +
1119 +What:          /debug/aufs/si_<id>/xigen
1120 +Date:          March 2009
1121 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1122 +Description:
1123 +               It shows the consumed blocks by xigen (External Inode
1124 +               Generation Table), its block size and file size.
1125 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1126 +               be created.
1127 +               When the aufs mount option 'noxino' is specified, it
1128 +               will be empty. About XINO files, see the aufs manual.
1129 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1130 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1131 +++ linux/Documentation/ABI/testing/sysfs-aufs  2017-07-29 12:14:25.893041746 +0200
1132 @@ -0,0 +1,31 @@
1133 +What:          /sys/fs/aufs/si_<id>/
1134 +Date:          March 2009
1135 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1136 +Description:
1137 +               Under /sys/fs/aufs, a directory named si_<id> is created
1138 +               per aufs mount, where <id> is a unique id generated
1139 +               internally.
1140 +
1141 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1142 +Date:          March 2009
1143 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1144 +Description:
1145 +               It shows the abolute path of a member directory (which
1146 +               is called branch) in aufs, and its permission.
1147 +
1148 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1149 +Date:          July 2013
1150 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1151 +Description:
1152 +               It shows the id of a member directory (which is called
1153 +               branch) in aufs.
1154 +
1155 +What:          /sys/fs/aufs/si_<id>/xi_path
1156 +Date:          March 2009
1157 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1158 +Description:
1159 +               It shows the abolute path of XINO (External Inode Number
1160 +               Bitmap, Translation Table and Generation Table) file
1161 +               even if it is the default path.
1162 +               When the aufs mount option 'noxino' is specified, it
1163 +               will be empty. About XINO files, see the aufs manual.
1164 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1165 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1166 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2017-07-29 12:14:25.893041746 +0200
1167 @@ -0,0 +1,171 @@
1168 +
1169 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1170 +# 
1171 +# This program is free software; you can redistribute it and/or modify
1172 +# it under the terms of the GNU General Public License as published by
1173 +# the Free Software Foundation; either version 2 of the License, or
1174 +# (at your option) any later version.
1175 +# 
1176 +# This program is distributed in the hope that it will be useful,
1177 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1178 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1179 +# GNU General Public License for more details.
1180 +# 
1181 +# You should have received a copy of the GNU General Public License
1182 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1183 +
1184 +Introduction
1185 +----------------------------------------
1186 +
1187 +aufs [ei ju: ef es] | [a u f s]
1188 +1. abbrev. for "advanced multi-layered unification filesystem".
1189 +2. abbrev. for "another unionfs".
1190 +3. abbrev. for "auf das" in German which means "on the" in English.
1191 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1192 +   But "Filesystem aufs Filesystem" is hard to understand.
1193 +4. abbrev. for "African Urban Fashion Show".
1194 +
1195 +AUFS is a filesystem with features:
1196 +- multi layered stackable unification filesystem, the member directory
1197 +  is called as a branch.
1198 +- branch permission and attribute, 'readonly', 'real-readonly',
1199 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1200 +  combination.
1201 +- internal "file copy-on-write".
1202 +- logical deletion, whiteout.
1203 +- dynamic branch manipulation, adding, deleting and changing permission.
1204 +- allow bypassing aufs, user's direct branch access.
1205 +- external inode number translation table and bitmap which maintains the
1206 +  persistent aufs inode number.
1207 +- seekable directory, including NFS readdir.
1208 +- file mapping, mmap and sharing pages.
1209 +- pseudo-link, hardlink over branches.
1210 +- loopback mounted filesystem as a branch.
1211 +- several policies to select one among multiple writable branches.
1212 +- revert a single systemcall when an error occurs in aufs.
1213 +- and more...
1214 +
1215 +
1216 +Multi Layered Stackable Unification Filesystem
1217 +----------------------------------------------------------------------
1218 +Most people already knows what it is.
1219 +It is a filesystem which unifies several directories and provides a
1220 +merged single directory. When users access a file, the access will be
1221 +passed/re-directed/converted (sorry, I am not sure which English word is
1222 +correct) to the real file on the member filesystem. The member
1223 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1224 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1225 +readonly branch is handled by creating 'whiteout' on the upper writable
1226 +branch.
1227 +
1228 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1229 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1230 +different approaches to implement the merged-view.
1231 +The former tries putting it into VFS, and the latter implements as a
1232 +separate filesystem.
1233 +(If I misunderstand about these implementations, please let me know and
1234 +I shall correct it. Because it is a long time ago when I read their
1235 +source files last time).
1236 +
1237 +UnionMount's approach will be able to small, but may be hard to share
1238 +branches between several UnionMount since the whiteout in it is
1239 +implemented in the inode on branch filesystem and always
1240 +shared. According to Bharata's post, readdir does not seems to be
1241 +finished yet.
1242 +There are several missing features known in this implementations such as
1243 +- for users, the inode number may change silently. eg. copy-up.
1244 +- link(2) may break by copy-up.
1245 +- read(2) may get an obsoleted filedata (fstat(2) too).
1246 +- fcntl(F_SETLK) may be broken by copy-up.
1247 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1248 +  open(O_RDWR).
1249 +
1250 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1251 +merged into mainline. This is another implementation of UnionMount as a
1252 +separated filesystem. All the limitations and known problems which
1253 +UnionMount are equally inherited to "overlay" filesystem.
1254 +
1255 +Unionfs has a longer history. When I started implementing a stackable
1256 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1257 +inode, dentry and file objects and they have an array pointing lower
1258 +same kind objects. After contributing many patches for Unionfs, I
1259 +re-started my project AUFS (Jun 2006).
1260 +
1261 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1262 +implemented my own ideas, approaches and enhancements and it became
1263 +totally different one.
1264 +
1265 +Comparing DM snapshot and fs based implementation
1266 +- the number of bytes to be copied between devices is much smaller.
1267 +- the type of filesystem must be one and only.
1268 +- the fs must be writable, no readonly fs, even for the lower original
1269 +  device. so the compression fs will not be usable. but if we use
1270 +  loopback mount, we may address this issue.
1271 +  for instance,
1272 +       mount /cdrom/squashfs.img /sq
1273 +       losetup /sq/ext2.img
1274 +       losetup /somewhere/cow
1275 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1276 +- it will be difficult (or needs more operations) to extract the
1277 +  difference between the original device and COW.
1278 +- DM snapshot-merge may help a lot when users try merging. in the
1279 +  fs-layer union, users will use rsync(1).
1280 +
1281 +You may want to read my old paper "Filesystems in LiveCD"
1282 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1283 +
1284 +
1285 +Several characters/aspects/persona of aufs
1286 +----------------------------------------------------------------------
1287 +
1288 +Aufs has several characters, aspects or persona.
1289 +1. a filesystem, callee of VFS helper
1290 +2. sub-VFS, caller of VFS helper for branches
1291 +3. a virtual filesystem which maintains persistent inode number
1292 +4. reader/writer of files on branches such like an application
1293 +
1294 +1. Callee of VFS Helper
1295 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1296 +unlink(2) from an application reaches sys_unlink() kernel function and
1297 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1298 +calls filesystem specific unlink operation. Actually aufs implements the
1299 +unlink operation but it behaves like a redirector.
1300 +
1301 +2. Caller of VFS Helper for Branches
1302 +aufs_unlink() passes the unlink request to the branch filesystem as if
1303 +it were called from VFS. So the called unlink operation of the branch
1304 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1305 +every necessary pre/post operation for the branch filesystem.
1306 +- acquire the lock for the parent dir on a branch
1307 +- lookup in a branch
1308 +- revalidate dentry on a branch
1309 +- mnt_want_write() for a branch
1310 +- vfs_unlink() for a branch
1311 +- mnt_drop_write() for a branch
1312 +- release the lock on a branch
1313 +
1314 +3. Persistent Inode Number
1315 +One of the most important issue for a filesystem is to maintain inode
1316 +numbers. This is particularly important to support exporting a
1317 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1318 +backend block device for its own. But some storage is necessary to
1319 +keep and maintain the inode numbers. It may be a large space and may not
1320 +suit to keep in memory. Aufs rents some space from its first writable
1321 +branch filesystem (by default) and creates file(s) on it. These files
1322 +are created by aufs internally and removed soon (currently) keeping
1323 +opened.
1324 +Note: Because these files are removed, they are totally gone after
1325 +      unmounting aufs. It means the inode numbers are not persistent
1326 +      across unmount or reboot. I have a plan to make them really
1327 +      persistent which will be important for aufs on NFS server.
1328 +
1329 +4. Read/Write Files Internally (copy-on-write)
1330 +Because a branch can be readonly, when you write a file on it, aufs will
1331 +"copy-up" it to the upper writable branch internally. And then write the
1332 +originally requested thing to the file. Generally kernel doesn't
1333 +open/read/write file actively. In aufs, even a single write may cause a
1334 +internal "file copy". This behaviour is very similar to cp(1) command.
1335 +
1336 +Some people may think it is better to pass such work to user space
1337 +helper, instead of doing in kernel space. Actually I am still thinking
1338 +about it. But currently I have implemented it in kernel space.
1339 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1340 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1341 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2017-07-29 12:14:25.893041746 +0200
1342 @@ -0,0 +1,258 @@
1343 +
1344 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1345 +# 
1346 +# This program is free software; you can redistribute it and/or modify
1347 +# it under the terms of the GNU General Public License as published by
1348 +# the Free Software Foundation; either version 2 of the License, or
1349 +# (at your option) any later version.
1350 +# 
1351 +# This program is distributed in the hope that it will be useful,
1352 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1353 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1354 +# GNU General Public License for more details.
1355 +# 
1356 +# You should have received a copy of the GNU General Public License
1357 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1358 +
1359 +Basic Aufs Internal Structure
1360 +
1361 +Superblock/Inode/Dentry/File Objects
1362 +----------------------------------------------------------------------
1363 +As like an ordinary filesystem, aufs has its own
1364 +superblock/inode/dentry/file objects. All these objects have a
1365 +dynamically allocated array and store the same kind of pointers to the
1366 +lower filesystem, branch.
1367 +For example, when you build a union with one readwrite branch and one
1368 +readonly, mounted /au, /rw and /ro respectively.
1369 +- /au = /rw + /ro
1370 +- /ro/fileA exists but /rw/fileA
1371 +
1372 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1373 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1374 +- [0] = NULL (because /rw/fileA doesn't exist)
1375 +- [1] = /ro/fileA
1376 +
1377 +This style of an array is essentially same to the aufs
1378 +superblock/inode/dentry/file objects.
1379 +
1380 +Because aufs supports manipulating branches, ie. add/delete/change
1381 +branches dynamically, these objects has its own generation. When
1382 +branches are changed, the generation in aufs superblock is
1383 +incremented. And a generation in other object are compared when it is
1384 +accessed. When a generation in other objects are obsoleted, aufs
1385 +refreshes the internal array.
1386 +
1387 +
1388 +Superblock
1389 +----------------------------------------------------------------------
1390 +Additionally aufs superblock has some data for policies to select one
1391 +among multiple writable branches, XIB files, pseudo-links and kobject.
1392 +See below in detail.
1393 +About the policies which supports copy-down a directory, see
1394 +wbr_policy.txt too.
1395 +
1396 +
1397 +Branch and XINO(External Inode Number Translation Table)
1398 +----------------------------------------------------------------------
1399 +Every branch has its own xino (external inode number translation table)
1400 +file. The xino file is created and unlinked by aufs internally. When two
1401 +members of a union exist on the same filesystem, they share the single
1402 +xino file.
1403 +The struct of a xino file is simple, just a sequence of aufs inode
1404 +numbers which is indexed by the lower inode number.
1405 +In the above sample, assume the inode number of /ro/fileA is i111 and
1406 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1407 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1408 +
1409 +When the inode numbers are not contiguous, the xino file will be sparse
1410 +which has a hole in it and doesn't consume as much disk space as it
1411 +might appear. If your branch filesystem consumes disk space for such
1412 +holes, then you should specify 'xino=' option at mounting aufs.
1413 +
1414 +Aufs has a mount option to free the disk blocks for such holes in XINO
1415 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1416 +meet a problem of disk shortage due to XINO files, then you should try
1417 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1418 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1419 +the holes in XINO files.
1420 +
1421 +Also a writable branch has three kinds of "whiteout bases". All these
1422 +are existed when the branch is joined to aufs, and their names are
1423 +whiteout-ed doubly, so that users will never see their names in aufs
1424 +hierarchy.
1425 +1. a regular file which will be hardlinked to all whiteouts.
1426 +2. a directory to store a pseudo-link.
1427 +3. a directory to store an "orphan"-ed file temporary.
1428 +
1429 +1. Whiteout Base
1430 +   When you remove a file on a readonly branch, aufs handles it as a
1431 +   logical deletion and creates a whiteout on the upper writable branch
1432 +   as a hardlink of this file in order not to consume inode on the
1433 +   writable branch.
1434 +2. Pseudo-link Dir
1435 +   See below, Pseudo-link.
1436 +3. Step-Parent Dir
1437 +   When "fileC" exists on the lower readonly branch only and it is
1438 +   opened and removed with its parent dir, and then user writes
1439 +   something into it, then aufs copies-up fileC to this
1440 +   directory. Because there is no other dir to store fileC. After
1441 +   creating a file under this dir, the file is unlinked.
1442 +
1443 +Because aufs supports manipulating branches, ie. add/delete/change
1444 +dynamically, a branch has its own id. When the branch order changes,
1445 +aufs finds the new index by searching the branch id.
1446 +
1447 +
1448 +Pseudo-link
1449 +----------------------------------------------------------------------
1450 +Assume "fileA" exists on the lower readonly branch only and it is
1451 +hardlinked to "fileB" on the branch. When you write something to fileA,
1452 +aufs copies-up it to the upper writable branch. Additionally aufs
1453 +creates a hardlink under the Pseudo-link Directory of the writable
1454 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1455 +simple list. If fileB is read after unlinking fileA, aufs returns
1456 +filedata from the pseudo-link instead of the lower readonly
1457 +branch. Because the pseudo-link is based upon the inode, to keep the
1458 +inode number by xino (see above) is essentially necessary.
1459 +
1460 +All the hardlinks under the Pseudo-link Directory of the writable branch
1461 +should be restored in a proper location later. Aufs provides a utility
1462 +to do this. The userspace helpers executed at remounting and unmounting
1463 +aufs by default.
1464 +During this utility is running, it puts aufs into the pseudo-link
1465 +maintenance mode. In this mode, only the process which began the
1466 +maintenance mode (and its child processes) is allowed to operate in
1467 +aufs. Some other processes which are not related to the pseudo-link will
1468 +be allowed to run too, but the rest have to return an error or wait
1469 +until the maintenance mode ends. If a process already acquires an inode
1470 +mutex (in VFS), it has to return an error.
1471 +
1472 +
1473 +XIB(external inode number bitmap)
1474 +----------------------------------------------------------------------
1475 +Addition to the xino file per a branch, aufs has an external inode number
1476 +bitmap in a superblock object. It is also an internal file such like a
1477 +xino file.
1478 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1479 +not.
1480 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1481 +
1482 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1483 +reduce the number of consumed disk blocks for these files.
1484 +
1485 +
1486 +Virtual or Vertical Dir, and Readdir in Userspace
1487 +----------------------------------------------------------------------
1488 +In order to support multiple layers (branches), aufs readdir operation
1489 +constructs a virtual dir block on memory. For readdir, aufs calls
1490 +vfs_readdir() internally for each dir on branches, merges their entries
1491 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1492 +object. So the file object has its entry list until it is closed. The
1493 +entry list will be updated when the file position is zero and becomes
1494 +obsoleted. This decision is made in aufs automatically.
1495 +
1496 +The dynamically allocated memory block for the name of entries has a
1497 +unit of 512 bytes (by default) and stores the names contiguously (no
1498 +padding). Another block for each entry is handled by kmem_cache too.
1499 +During building dir blocks, aufs creates hash list and judging whether
1500 +the entry is whiteouted by its upper branch or already listed.
1501 +The merged result is cached in the corresponding inode object and
1502 +maintained by a customizable life-time option.
1503 +
1504 +Some people may call it can be a security hole or invite DoS attack
1505 +since the opened and once readdir-ed dir (file object) holds its entry
1506 +list and becomes a pressure for system memory. But I'd say it is similar
1507 +to files under /proc or /sys. The virtual files in them also holds a
1508 +memory page (generally) while they are opened. When an idea to reduce
1509 +memory for them is introduced, it will be applied to aufs too.
1510 +For those who really hate this situation, I've developed readdir(3)
1511 +library which operates this merging in userspace. You just need to set
1512 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1513 +kernel space for readdir(3).
1514 +
1515 +
1516 +Workqueue
1517 +----------------------------------------------------------------------
1518 +Aufs sometimes requires privilege access to a branch. For instance,
1519 +in copy-up/down operation. When a user process is going to make changes
1520 +to a file which exists in the lower readonly branch only, and the mode
1521 +of one of ancestor directories may not be writable by a user
1522 +process. Here aufs copy-up the file with its ancestors and they may
1523 +require privilege to set its owner/group/mode/etc.
1524 +This is a typical case of a application character of aufs (see
1525 +Introduction).
1526 +
1527 +Aufs uses workqueue synchronously for this case. It creates its own
1528 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1529 +passes the request to call mkdir or write (for example), and wait for
1530 +its completion. This approach solves a problem of a signal handler
1531 +simply.
1532 +If aufs didn't adopt the workqueue and changed the privilege of the
1533 +process, then the process may receive the unexpected SIGXFSZ or other
1534 +signals.
1535 +
1536 +Also aufs uses the system global workqueue ("events" kernel thread) too
1537 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1538 +whiteout base and etc. This is unrelated to a privilege.
1539 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1540 +superblock at the beginning, at the same time waits for the completion
1541 +of all queued asynchronous tasks.
1542 +
1543 +
1544 +Whiteout
1545 +----------------------------------------------------------------------
1546 +The whiteout in aufs is very similar to Unionfs's. That is represented
1547 +by its filename. UnionMount takes an approach of a file mode, but I am
1548 +afraid several utilities (find(1) or something) will have to support it.
1549 +
1550 +Basically the whiteout represents "logical deletion" which stops aufs to
1551 +lookup further, but also it represents "dir is opaque" which also stop
1552 +further lookup.
1553 +
1554 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1555 +In order to make several functions in a single systemcall to be
1556 +revertible, aufs adopts an approach to rename a directory to a temporary
1557 +unique whiteouted name.
1558 +For example, in rename(2) dir where the target dir already existed, aufs
1559 +renames the target dir to a temporary unique whiteouted name before the
1560 +actual rename on a branch, and then handles other actions (make it opaque,
1561 +update the attributes, etc). If an error happens in these actions, aufs
1562 +simply renames the whiteouted name back and returns an error. If all are
1563 +succeeded, aufs registers a function to remove the whiteouted unique
1564 +temporary name completely and asynchronously to the system global
1565 +workqueue.
1566 +
1567 +
1568 +Copy-up
1569 +----------------------------------------------------------------------
1570 +It is a well-known feature or concept.
1571 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1572 +internally and makes change to the new file on the upper writable branch.
1573 +When the trigger systemcall does not update the timestamps of the parent
1574 +dir, aufs reverts it after copy-up.
1575 +
1576 +
1577 +Move-down (aufs3.9 and later)
1578 +----------------------------------------------------------------------
1579 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1580 +the lower readonly branch to the upper writable branch when a user
1581 +changes something about the file.
1582 +"Move-down" is an opposite action of copy-up. Basically this action is
1583 +ran manually instead of automatically and internally.
1584 +For desgin and implementation, aufs has to consider these issues.
1585 +- whiteout for the file may exist on the lower branch.
1586 +- ancestor directories may not exist on the lower branch.
1587 +- diropq for the ancestor directories may exist on the upper branch.
1588 +- free space on the lower branch will reduce.
1589 +- another access to the file may happen during moving-down, including
1590 +  UDBA (see "Revalidate Dentry and UDBA").
1591 +- the file should not be hard-linked nor pseudo-linked. they should be
1592 +  handled by auplink utility later.
1593 +
1594 +Sometimes users want to move-down a file from the upper writable branch
1595 +to the lower readonly or writable branch. For instance,
1596 +- the free space of the upper writable branch is going to run out.
1597 +- create a new intermediate branch between the upper and lower branch.
1598 +- etc.
1599 +
1600 +For this purpose, use "aumvdown" command in aufs-util.git.
1601 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1602 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1603 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2017-07-29 12:14:25.893041746 +0200
1604 @@ -0,0 +1,85 @@
1605 +
1606 +# Copyright (C) 2015-2017 Junjiro R. Okajima
1607 +# 
1608 +# This program is free software; you can redistribute it and/or modify
1609 +# it under the terms of the GNU General Public License as published by
1610 +# the Free Software Foundation; either version 2 of the License, or
1611 +# (at your option) any later version.
1612 +# 
1613 +# This program is distributed in the hope that it will be useful,
1614 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1615 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1616 +# GNU General Public License for more details.
1617 +# 
1618 +# You should have received a copy of the GNU General Public License
1619 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1620 +
1621 +Support for a branch who has its ->atomic_open()
1622 +----------------------------------------------------------------------
1623 +The filesystems who implement its ->atomic_open() are not majority. For
1624 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1625 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1626 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1627 +sure whether all filesystems who have ->atomic_open() behave like this,
1628 +but NFSv4 surely returns the error.
1629 +
1630 +In order to support ->atomic_open() for aufs, there are a few
1631 +approaches.
1632 +
1633 +A. Introduce aufs_atomic_open()
1634 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1635 +     branch fs.
1636 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1637 +   an aufs user Pip Cet's approach
1638 +   - calls aufs_create(), VFS finish_open() and notify_change().
1639 +   - pass fake-mode to finish_open(), and then correct the mode by
1640 +     notify_change().
1641 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1642 +   - no aufs_atomic_open().
1643 +   - aufs_lookup() registers the TID to an aufs internal object.
1644 +   - aufs_create() does nothing when the matching TID is registered, but
1645 +     registers the mode.
1646 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1647 +     TID is registered.
1648 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1649 +   credential
1650 +   - no aufs_atomic_open().
1651 +   - aufs_create() registers the TID to an internal object. this info
1652 +     represents "this process created this file just now."
1653 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1654 +     registered TID and re-try open() with superuser's credential.
1655 +
1656 +Pros and cons for each approach.
1657 +
1658 +A.
1659 +   - straightforward but highly depends upon VFS internal.
1660 +   - the atomic behavaiour is kept.
1661 +   - some of parameters such as nameidata are hard to reproduce for
1662 +     branch fs.
1663 +   - large overhead.
1664 +B.
1665 +   - easy to implement.
1666 +   - the atomic behavaiour is lost.
1667 +C.
1668 +   - the atomic behavaiour is kept.
1669 +   - dirty and tricky.
1670 +   - VFS checks whether the file is created correctly after calling
1671 +     ->create(), which means this approach doesn't work.
1672 +D.
1673 +   - easy to implement.
1674 +   - the atomic behavaiour is lost.
1675 +   - to open a file with superuser's credential and give it to a user
1676 +     process is a bad idea, since the file object keeps the credential
1677 +     in it. It may affect LSM or something. This approach doesn't work
1678 +     either.
1679 +
1680 +The approach A is ideal, but it hard to implement. So here is a
1681 +variation of A, which is to be implemented.
1682 +
1683 +A-1. Introduce aufs_atomic_open()
1684 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1685 +       vfs_create() and finish_open().
1686 +     - the demerit is that the several checks after branch fs
1687 +       ->atomic_open() are lost. in the ordinary case, the checks are
1688 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1689 +       be implemented in aufs, but not all I am afraid.
1690 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1691 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1692 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2017-07-29 12:14:25.893041746 +0200
1693 @@ -0,0 +1,113 @@
1694 +
1695 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1696 +# 
1697 +# This program is free software; you can redistribute it and/or modify
1698 +# it under the terms of the GNU General Public License as published by
1699 +# the Free Software Foundation; either version 2 of the License, or
1700 +# (at your option) any later version.
1701 +# 
1702 +# This program is distributed in the hope that it will be useful,
1703 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1704 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1705 +# GNU General Public License for more details.
1706 +# 
1707 +# You should have received a copy of the GNU General Public License
1708 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1709 +
1710 +Lookup in a Branch
1711 +----------------------------------------------------------------------
1712 +Since aufs has a character of sub-VFS (see Introduction), it operates
1713 +lookup for branches as VFS does. It may be a heavy work. But almost all
1714 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1715 +directly connected to its parent. Digging down the directory hierarchy
1716 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1717 +aufs calls it.
1718 +
1719 +When a branch is a remote filesystem, aufs basically relies upon its
1720 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1721 +them.
1722 +For d_revalidate, aufs implements three levels of revalidate tests. See
1723 +"Revalidate Dentry and UDBA" in detail.
1724 +
1725 +
1726 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1727 +----------------------------------------------------------------------
1728 +Let's try case study.
1729 +- aufs has two branches, upper readwrite and lower readonly.
1730 +  /au = /rw + /ro
1731 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1732 +- user invoked "chmod a+rx /au/dirA"
1733 +- the internal copy-up is activated and "/rw/dirA" is created and its
1734 +  permission bits are set to world readable.
1735 +- then "/au/dirA" becomes world readable?
1736 +
1737 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1738 +or it may be a natively readonly filesystem. If aufs respects the lower
1739 +branch, it should not respond readdir request from other users. But user
1740 +allowed it by chmod. Should really aufs rejects showing the entries
1741 +under /ro/dirA?
1742 +
1743 +To be honest, I don't have a good solution for this case. So aufs
1744 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1745 +users.
1746 +When dirperm1 is specified, aufs checks only the highest one for the
1747 +directory permission, and shows the entries. Otherwise, as usual, checks
1748 +every dir existing on all branches and rejects the request.
1749 +
1750 +As a side effect, dirperm1 option improves the performance of aufs
1751 +because the number of permission check is reduced when the number of
1752 +branch is many.
1753 +
1754 +
1755 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1756 +----------------------------------------------------------------------
1757 +Generally VFS helpers re-validate a dentry as a part of lookup.
1758 +0. digging down the directory hierarchy.
1759 +1. lock the parent dir by its i_mutex.
1760 +2. lookup the final (child) entry.
1761 +3. revalidate it.
1762 +4. call the actual operation (create, unlink, etc.)
1763 +5. unlock the parent dir
1764 +
1765 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1766 +called. Actually aufs implements it and checks the dentry on a branch is
1767 +still valid.
1768 +But it is not enough. Because aufs has to release the lock for the
1769 +parent dir on a branch at the end of ->lookup() (step 2) and
1770 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1771 +held by VFS.
1772 +If the file on a branch is changed directly, eg. bypassing aufs, after
1773 +aufs released the lock, then the subsequent operation may cause
1774 +something unpleasant result.
1775 +
1776 +This situation is a result of VFS architecture, ->lookup() and
1777 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1778 +design from VFS's point of view. It is just not suitable for sub-VFS
1779 +character in aufs.
1780 +
1781 +Aufs supports such case by three level of revalidation which is
1782 +selectable by user.
1783 +1. Simple Revalidate
1784 +   Addition to the native flow in VFS's, confirm the child-parent
1785 +   relationship on the branch just after locking the parent dir on the
1786 +   branch in the "actual operation" (step 4). When this validation
1787 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1788 +   checks the validation of the dentry on branches.
1789 +2. Monitor Changes Internally by Inotify/Fsnotify
1790 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1791 +   the dentry on the branch, and returns EBUSY if it finds different
1792 +   dentry.
1793 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1794 +   during it is in cache. When the event is notified, aufs registers a
1795 +   function to kernel 'events' thread by schedule_work(). And the
1796 +   function sets some special status to the cached aufs dentry and inode
1797 +   private data. If they are not cached, then aufs has nothing to
1798 +   do. When the same file is accessed through aufs (step 0-3) later,
1799 +   aufs will detect the status and refresh all necessary data.
1800 +   In this mode, aufs has to ignore the event which is fired by aufs
1801 +   itself.
1802 +3. No Extra Validation
1803 +   This is the simplest test and doesn't add any additional revalidation
1804 +   test, and skip the revalidation in step 4. It is useful and improves
1805 +   aufs performance when system surely hide the aufs branches from user,
1806 +   by over-mounting something (or another method).
1807 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1808 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1809 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2017-07-29 12:14:25.893041746 +0200
1810 @@ -0,0 +1,74 @@
1811 +
1812 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1813 +# 
1814 +# This program is free software; you can redistribute it and/or modify
1815 +# it under the terms of the GNU General Public License as published by
1816 +# the Free Software Foundation; either version 2 of the License, or
1817 +# (at your option) any later version.
1818 +# 
1819 +# This program is distributed in the hope that it will be useful,
1820 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1821 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1822 +# GNU General Public License for more details.
1823 +# 
1824 +# You should have received a copy of the GNU General Public License
1825 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1826 +
1827 +Branch Manipulation
1828 +
1829 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1830 +and changing its permission/attribute, there are a lot of works to do.
1831 +
1832 +
1833 +Add a Branch
1834 +----------------------------------------------------------------------
1835 +o Confirm the adding dir exists outside of aufs, including loopback
1836 +  mount, and its various attributes.
1837 +o Initialize the xino file and whiteout bases if necessary.
1838 +  See struct.txt.
1839 +
1840 +o Check the owner/group/mode of the directory
1841 +  When the owner/group/mode of the adding directory differs from the
1842 +  existing branch, aufs issues a warning because it may impose a
1843 +  security risk.
1844 +  For example, when a upper writable branch has a world writable empty
1845 +  top directory, a malicious user can create any files on the writable
1846 +  branch directly, like copy-up and modify manually. If something like
1847 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1848 +  writable branch, and the writable branch is world-writable, then a
1849 +  malicious guy may create /etc/passwd on the writable branch directly
1850 +  and the infected file will be valid in aufs.
1851 +  I am afraid it can be a security issue, but aufs can do nothing except
1852 +  producing a warning.
1853 +
1854 +
1855 +Delete a Branch
1856 +----------------------------------------------------------------------
1857 +o Confirm the deleting branch is not busy
1858 +  To be general, there is one merit to adopt "remount" interface to
1859 +  manipulate branches. It is to discard caches. At deleting a branch,
1860 +  aufs checks the still cached (and connected) dentries and inodes. If
1861 +  there are any, then they are all in-use. An inode without its
1862 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1863 +
1864 +  For the cached one, aufs checks whether the same named entry exists on
1865 +  other branches.
1866 +  If the cached one is a directory, because aufs provides a merged view
1867 +  to users, as long as one dir is left on any branch aufs can show the
1868 +  dir to users. In this case, the branch can be removed from aufs.
1869 +  Otherwise aufs rejects deleting the branch.
1870 +
1871 +  If any file on the deleting branch is opened by aufs, then aufs
1872 +  rejects deleting.
1873 +
1874 +
1875 +Modify the Permission of a Branch
1876 +----------------------------------------------------------------------
1877 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1878 +  See struct.txt.
1879 +
1880 +o rw --> ro: Confirm the modifying branch is not busy
1881 +  Aufs rejects the request if any of these conditions are true.
1882 +  - a file on the branch is mmap-ed.
1883 +  - a regular file on the branch is opened for write and there is no
1884 +    same named entry on the upper branch.
1885 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1886 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1887 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2017-07-29 12:14:25.893041746 +0200
1888 @@ -0,0 +1,64 @@
1889 +
1890 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1891 +# 
1892 +# This program is free software; you can redistribute it and/or modify
1893 +# it under the terms of the GNU General Public License as published by
1894 +# the Free Software Foundation; either version 2 of the License, or
1895 +# (at your option) any later version.
1896 +# 
1897 +# This program is distributed in the hope that it will be useful,
1898 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1899 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1900 +# GNU General Public License for more details.
1901 +# 
1902 +# You should have received a copy of the GNU General Public License
1903 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1904 +
1905 +Policies to Select One among Multiple Writable Branches
1906 +----------------------------------------------------------------------
1907 +When the number of writable branch is more than one, aufs has to decide
1908 +the target branch for file creation or copy-up. By default, the highest
1909 +writable branch which has the parent (or ancestor) dir of the target
1910 +file is chosen (top-down-parent policy).
1911 +By user's request, aufs implements some other policies to select the
1912 +writable branch, for file creation several policies, round-robin,
1913 +most-free-space, and other policies. For copy-up, top-down-parent,
1914 +bottom-up-parent, bottom-up and others.
1915 +
1916 +As expected, the round-robin policy selects the branch in circular. When
1917 +you have two writable branches and creates 10 new files, 5 files will be
1918 +created for each branch. mkdir(2) systemcall is an exception. When you
1919 +create 10 new directories, all will be created on the same branch.
1920 +And the most-free-space policy selects the one which has most free
1921 +space among the writable branches. The amount of free space will be
1922 +checked by aufs internally, and users can specify its time interval.
1923 +
1924 +The policies for copy-up is more simple,
1925 +top-down-parent is equivalent to the same named on in create policy,
1926 +bottom-up-parent selects the writable branch where the parent dir
1927 +exists and the nearest upper one from the copyup-source,
1928 +bottom-up selects the nearest upper writable branch from the
1929 +copyup-source, regardless the existence of the parent dir.
1930 +
1931 +There are some rules or exceptions to apply these policies.
1932 +- If there is a readonly branch above the policy-selected branch and
1933 +  the parent dir is marked as opaque (a variation of whiteout), or the
1934 +  target (creating) file is whiteout-ed on the upper readonly branch,
1935 +  then the result of the policy is ignored and the target file will be
1936 +  created on the nearest upper writable branch than the readonly branch.
1937 +- If there is a writable branch above the policy-selected branch and
1938 +  the parent dir is marked as opaque or the target file is whiteouted
1939 +  on the branch, then the result of the policy is ignored and the target
1940 +  file will be created on the highest one among the upper writable
1941 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1942 +  it as usual.
1943 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1944 +  They try selecting the branch where the source exists as possible
1945 +  since copyup a large file will take long time. If it can't be,
1946 +  ie. the branch where the source exists is readonly, then they will
1947 +  follow the copyup policy.
1948 +- There is an exception for rename(2) when the target exists.
1949 +  If the rename target exists, aufs compares the index of the branches
1950 +  where the source and the target exists and selects the higher
1951 +  one. If the selected branch is readonly, then aufs follows the
1952 +  copyup policy.
1953 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1954 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1955 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2017-07-29 12:14:25.896375188 +0200
1956 @@ -0,0 +1,120 @@
1957 +
1958 +# Copyright (C) 2011-2017 Junjiro R. Okajima
1959 +# 
1960 +# This program is free software; you can redistribute it and/or modify
1961 +# it under the terms of the GNU General Public License as published by
1962 +# the Free Software Foundation; either version 2 of the License, or
1963 +# (at your option) any later version.
1964 +# 
1965 +# This program is distributed in the hope that it will be useful,
1966 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1967 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1968 +# GNU General Public License for more details.
1969 +# 
1970 +# You should have received a copy of the GNU General Public License
1971 +# along with this program; if not, write to the Free Software
1972 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1973 +
1974 +
1975 +File-based Hierarchical Storage Management (FHSM)
1976 +----------------------------------------------------------------------
1977 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1978 +storage world. Aufs provides this feature as file-based with multiple
1979 +writable branches, based upon the principle of "Colder, the Lower".
1980 +Here the word "colder" means that the less used files, and "lower" means
1981 +that the position in the order of the stacked branches vertically.
1982 +These multiple writable branches are prioritized, ie. the topmost one
1983 +should be the fastest drive and be used heavily.
1984 +
1985 +o Characters in aufs FHSM story
1986 +- aufs itself and a new branch attribute.
1987 +- a new ioctl interface to move-down and to establish a connection with
1988 +  the daemon ("move-down" is a converse of "copy-up").
1989 +- userspace tool and daemon.
1990 +
1991 +The userspace daemon establishes a connection with aufs and waits for
1992 +the notification. The notified information is very similar to struct
1993 +statfs containing the number of consumed blocks and inodes.
1994 +When the consumed blocks/inodes of a branch exceeds the user-specified
1995 +upper watermark, the daemon activates its move-down process until the
1996 +consumed blocks/inodes reaches the user-specified lower watermark.
1997 +
1998 +The actual move-down is done by aufs based upon the request from
1999 +user-space since we need to maintain the inode number and the internal
2000 +pointer arrays in aufs.
2001 +
2002 +Currently aufs FHSM handles the regular files only. Additionally they
2003 +must not be hard-linked nor pseudo-linked.
2004 +
2005 +
2006 +o Cowork of aufs and the user-space daemon
2007 +  During the userspace daemon established the connection, aufs sends a
2008 +  small notification to it whenever aufs writes something into the
2009 +  writable branch. But it may cost high since aufs issues statfs(2)
2010 +  internally. So user can specify a new option to cache the
2011 +  info. Actually the notification is controlled by these factors.
2012 +  + the specified cache time.
2013 +  + classified as "force" by aufs internally.
2014 +  Until the specified time expires, aufs doesn't send the info
2015 +  except the forced cases. When aufs decide forcing, the info is always
2016 +  notified to userspace.
2017 +  For example, the number of free inodes is generally large enough and
2018 +  the shortage of it happens rarely. So aufs doesn't force the
2019 +  notification when creating a new file, directory and others. This is
2020 +  the typical case which aufs doesn't force.
2021 +  When aufs writes the actual filedata and the files consumes any of new
2022 +  blocks, the aufs forces notifying.
2023 +
2024 +
2025 +o Interfaces in aufs
2026 +- New branch attribute.
2027 +  + fhsm
2028 +    Specifies that the branch is managed by FHSM feature. In other word,
2029 +    participant in the FHSM.
2030 +    When nofhsm is set to the branch, it will not be the source/target
2031 +    branch of the move-down operation. This attribute is set
2032 +    independently from coo and moo attributes, and if you want full
2033 +    FHSM, you should specify them as well.
2034 +- New mount option.
2035 +  + fhsm_sec
2036 +    Specifies a second to suppress many less important info to be
2037 +    notified.
2038 +- New ioctl.
2039 +  + AUFS_CTL_FHSM_FD
2040 +    create a new file descriptor which userspace can read the notification
2041 +    (a subset of struct statfs) from aufs.
2042 +- Module parameter 'brs'
2043 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2044 +  be set.
2045 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2046 +  When there are two or more branches with fhsm attributes,
2047 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2048 +  terminates it. As a result of remounting and branch-manipulation, the
2049 +  number of branches with fhsm attribute can be one. In this case,
2050 +  /sbin/mount.aufs will terminate the user-space daemon.
2051 +
2052 +
2053 +Finally the operation is done as these steps in kernel-space.
2054 +- make sure that,
2055 +  + no one else is using the file.
2056 +  + the file is not hard-linked.
2057 +  + the file is not pseudo-linked.
2058 +  + the file is a regular file.
2059 +  + the parent dir is not opaqued.
2060 +- find the target writable branch.
2061 +- make sure the file is not whiteout-ed by the upper (than the target)
2062 +  branch.
2063 +- make the parent dir on the target branch.
2064 +- mutex lock the inode on the branch.
2065 +- unlink the whiteout on the target branch (if exists).
2066 +- lookup and create the whiteout-ed temporary name on the target branch.
2067 +- copy the file as the whiteout-ed temporary name on the target branch.
2068 +- rename the whiteout-ed temporary name to the original name.
2069 +- unlink the file on the source branch.
2070 +- maintain the internal pointer array and the external inode number
2071 +  table (XINO).
2072 +- maintain the timestamps and other attributes of the parent dir and the
2073 +  file.
2074 +
2075 +And of course, in every step, an error may happen. So the operation
2076 +should restore the original file state after an error happens.
2077 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2078 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2079 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2017-07-29 12:14:25.896375188 +0200
2080 @@ -0,0 +1,72 @@
2081 +
2082 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2083 +# 
2084 +# This program is free software; you can redistribute it and/or modify
2085 +# it under the terms of the GNU General Public License as published by
2086 +# the Free Software Foundation; either version 2 of the License, or
2087 +# (at your option) any later version.
2088 +# 
2089 +# This program is distributed in the hope that it will be useful,
2090 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2091 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2092 +# GNU General Public License for more details.
2093 +# 
2094 +# You should have received a copy of the GNU General Public License
2095 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2096 +
2097 +mmap(2) -- File Memory Mapping
2098 +----------------------------------------------------------------------
2099 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2100 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2101 +->mmap().
2102 +This approach is simple and good, but there is one problem.
2103 +Under /proc, several entries show the mmapped files by its path (with
2104 +device and inode number), and the printed path will be the path on the
2105 +branch fs's instead of virtual aufs's.
2106 +This is not a problem in most cases, but some utilities lsof(1) (and its
2107 +user) may expect the path on aufs.
2108 +
2109 +To address this issue, aufs adds a new member called vm_prfile in struct
2110 +vm_area_struct (and struct vm_region). The original vm_file points to
2111 +the file on the branch fs in order to handle everything correctly as
2112 +usual. The new vm_prfile points to a virtual file in aufs, and the
2113 +show-functions in procfs refers to vm_prfile if it is set.
2114 +Also we need to maintain several other places where touching vm_file
2115 +such like
2116 +- fork()/clone() copies vma and the reference count of vm_file is
2117 +  incremented.
2118 +- merging vma maintains the ref count too.
2119 +
2120 +This is not a good approach. It just fakes the printed path. But it
2121 +leaves all behaviour around f_mapping unchanged. This is surely an
2122 +advantage.
2123 +Actually aufs had adopted another complicated approach which calls
2124 +generic_file_mmap() and handles struct vm_operations_struct. In this
2125 +approach, aufs met a hard problem and I could not solve it without
2126 +switching the approach.
2127 +
2128 +There may be one more another approach which is
2129 +- bind-mount the branch-root onto the aufs-root internally
2130 +- grab the new vfsmount (ie. struct mount)
2131 +- lazy-umount the branch-root internally
2132 +- in open(2) the aufs-file, open the branch-file with the hidden
2133 +  vfsmount (instead of the original branch's vfsmount)
2134 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2135 +  but it may be possible from userspace by the mount helper.
2136 +
2137 +Adding the internal hidden vfsmount and using it in opening a file, the
2138 +file path under /proc will be printed correctly. This approach looks
2139 +smarter, but is not possible I am afraid.
2140 +- aufs-root may be bind-mount later. when it happens, another hidden
2141 +  vfsmount will be required.
2142 +- it is hard to get the chance to bind-mount and lazy-umount
2143 +  + in kernel-space, FS can have vfsmount in open(2) via
2144 +    file->f_path, and aufs can know its vfsmount. But several locks are
2145 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2146 +    here, then it may cause a deadlock.
2147 +  + in user-space, bind-mount doesn't invoke the mount helper.
2148 +- since /proc shows dev and ino, aufs has to give vma these info. it
2149 +  means a new member vm_prinode will be necessary. this is essentially
2150 +  equivalent to vm_prfile described above.
2151 +
2152 +I have to give up this "looks-smater" approach.
2153 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2154 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2155 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2017-07-29 12:14:25.896375188 +0200
2156 @@ -0,0 +1,96 @@
2157 +
2158 +# Copyright (C) 2014-2017 Junjiro R. Okajima
2159 +#
2160 +# This program is free software; you can redistribute it and/or modify
2161 +# it under the terms of the GNU General Public License as published by
2162 +# the Free Software Foundation; either version 2 of the License, or
2163 +# (at your option) any later version.
2164 +#
2165 +# This program is distributed in the hope that it will be useful,
2166 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2167 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2168 +# GNU General Public License for more details.
2169 +#
2170 +# You should have received a copy of the GNU General Public License
2171 +# along with this program; if not, write to the Free Software
2172 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2173 +
2174 +
2175 +Listing XATTR/EA and getting the value
2176 +----------------------------------------------------------------------
2177 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2178 +shows the values from the topmost existing file. This behaviour is good
2179 +for the non-dir entries since the bahaviour exactly matches the shown
2180 +information. But for the directories, aufs considers all the same named
2181 +entries on the lower branches. Which means, if one of the lower entry
2182 +rejects readdir call, then aufs returns an error even if the topmost
2183 +entry allows it. This behaviour is necessary to respect the branch fs's
2184 +security, but can make users confused since the user-visible standard
2185 +attributes don't match the behaviour.
2186 +To address this issue, aufs has a mount option called dirperm1 which
2187 +checks the permission for the topmost entry only, and ignores the lower
2188 +entry's permission.
2189 +
2190 +A similar issue can happen around XATTR.
2191 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2192 +always set. Otherwise these very unpleasant situation would happen.
2193 +- listxattr(2) may return the duplicated entries.
2194 +- users may not be able to remove or reset the XATTR forever,
2195 +
2196 +
2197 +XATTR/EA support in the internal (copy,move)-(up,down)
2198 +----------------------------------------------------------------------
2199 +Generally the extended attributes of inode are categorized as these.
2200 +- "security" for LSM and capability.
2201 +- "system" for posix ACL, 'acl' mount option is required for the branch
2202 +  fs generally.
2203 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2204 +- "user" for userspace, 'user_xattr' mount option is required for the
2205 +  branch fs generally.
2206 +
2207 +Moreover there are some other categories. Aufs handles these rather
2208 +unpopular categories as the ordinary ones, ie. there is no special
2209 +condition nor exception.
2210 +
2211 +In copy-up, the support for XATTR on the dst branch may differ from the
2212 +src branch. In this case, the copy-up operation will get an error and
2213 +the original user operation which triggered the copy-up will fail. It
2214 +can happen that even all copy-up will fail.
2215 +When both of src and dst branches support XATTR and if an error occurs
2216 +during copying XATTR, then the copy-up should fail obviously. That is a
2217 +good reason and aufs should return an error to userspace. But when only
2218 +the src branch support that XATTR, aufs should not return an error.
2219 +For example, the src branch supports ACL but the dst branch doesn't
2220 +because the dst branch may natively un-support it or temporary
2221 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2222 +may NOT return an error even if the XATTR is not supported. It is
2223 +totally up to the branch fs.
2224 +
2225 +Anyway when the aufs internal copy-up gets an error from the dst branch
2226 +fs, then aufs tries removing the just copied entry and returns the error
2227 +to the userspace. The worst case of this situation will be all copy-up
2228 +will fail.
2229 +
2230 +For the copy-up operation, there two basic approaches.
2231 +- copy the specified XATTR only (by category above), and return the
2232 +  error unconditionally if it happens.
2233 +- copy all XATTR, and ignore the error on the specified category only.
2234 +
2235 +In order to support XATTR and to implement the correct behaviour, aufs
2236 +chooses the latter approach and introduces some new branch attributes,
2237 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2238 +They correspond to the XATTR namespaces (see above). Additionally, to be
2239 +convenient, "icex" is also provided which means all "icex*" attributes
2240 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2241 +
2242 +The meaning of these attributes is to ignore the error from setting
2243 +XATTR on that branch.
2244 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2245 +error from the dst branch according to the specified attributes.
2246 +
2247 +Some XATTR may have its default value. The default value may come from
2248 +the parent dir or the environment. If the default value is set at the
2249 +file creating-time, it will be overwritten by copy-up.
2250 +Some contradiction may happen I am afraid.
2251 +Do we need another attribute to stop copying XATTR? I am unsure. For
2252 +now, aufs implements the branch attributes to ignore the error.
2253 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2254 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2255 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2017-07-29 12:14:25.896375188 +0200
2256 @@ -0,0 +1,58 @@
2257 +
2258 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2259 +# 
2260 +# This program is free software; you can redistribute it and/or modify
2261 +# it under the terms of the GNU General Public License as published by
2262 +# the Free Software Foundation; either version 2 of the License, or
2263 +# (at your option) any later version.
2264 +# 
2265 +# This program is distributed in the hope that it will be useful,
2266 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2267 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2268 +# GNU General Public License for more details.
2269 +# 
2270 +# You should have received a copy of the GNU General Public License
2271 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2272 +
2273 +Export Aufs via NFS
2274 +----------------------------------------------------------------------
2275 +Here is an approach.
2276 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2277 +  generation.
2278 +- iget_locked(): initialize aufs inode generation for a new inode, and
2279 +  store it in xigen file.
2280 +- destroy_inode(): increment aufs inode generation and store it in xigen
2281 +  file. it is necessary even if it is not unlinked, because any data of
2282 +  inode may be changed by UDBA.
2283 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2284 +  build file handle by
2285 +  + branch id (4 bytes)
2286 +  + superblock generation (4 bytes)
2287 +  + inode number (4 or 8 bytes)
2288 +  + parent dir inode number (4 or 8 bytes)
2289 +  + inode generation (4 bytes))
2290 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2291 +    bytes)
2292 +  + file handle for a branch (by exportfs_encode_fh())
2293 +- fh_to_dentry():
2294 +  + find the index of a branch from its id in handle, and check it is
2295 +    still exist in aufs.
2296 +  + 1st level: get the inode number from handle and search it in cache.
2297 +  + 2nd level: if not found in cache, get the parent inode number from
2298 +    the handle and search it in cache. and then open the found parent
2299 +    dir, find the matching inode number by vfs_readdir() and get its
2300 +    name, and call lookup_one_len() for the target dentry.
2301 +  + 3rd level: if the parent dir is not cached, call
2302 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2303 +    build a pathname of it, convert it a pathname in aufs, call
2304 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2305 +    the 2nd level.
2306 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2307 +    for every branch, but not itself. to get this, (currently) aufs
2308 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2309 +    idea, but I didn't get other approach.
2310 +  + test the generation of the gotten inode.
2311 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2312 +  convert it into ESTALE for NFSD.
2313 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2314 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2315 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2316 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2317 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2017-07-29 12:14:25.896375188 +0200
2318 @@ -0,0 +1,52 @@
2319 +
2320 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2321 +# 
2322 +# This program is free software; you can redistribute it and/or modify
2323 +# it under the terms of the GNU General Public License as published by
2324 +# the Free Software Foundation; either version 2 of the License, or
2325 +# (at your option) any later version.
2326 +# 
2327 +# This program is distributed in the hope that it will be useful,
2328 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2329 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2330 +# GNU General Public License for more details.
2331 +# 
2332 +# You should have received a copy of the GNU General Public License
2333 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2334 +
2335 +Show Whiteout Mode (shwh)
2336 +----------------------------------------------------------------------
2337 +Generally aufs hides the name of whiteouts. But in some cases, to show
2338 +them is very useful for users. For instance, creating a new middle layer
2339 +(branch) by merging existing layers.
2340 +
2341 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2342 +When you have three branches,
2343 +- Bottom: 'system', squashfs (underlying base system), read-only
2344 +- Middle: 'mods', squashfs, read-only
2345 +- Top: 'overlay', ram (tmpfs), read-write
2346 +
2347 +The top layer is loaded at boot time and saved at shutdown, to preserve
2348 +the changes made to the system during the session.
2349 +When larger changes have been made, or smaller changes have accumulated,
2350 +the size of the saved top layer data grows. At this point, it would be
2351 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2352 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2353 +restoring save and load speed.
2354 +
2355 +This merging is simplified by the use of another aufs mount, of just the
2356 +two overlay branches using the 'shwh' option.
2357 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2358 +       aufs /livesys/merge_union
2359 +
2360 +A merged view of these two branches is then available at
2361 +/livesys/merge_union, and the new feature is that the whiteouts are
2362 +visible!
2363 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2364 +writing to all branches. Also the default mode for all branches is 'ro'.
2365 +It is now possible to save the combined contents of the two overlay
2366 +branches to a new squashfs, e.g.:
2367 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2368 +
2369 +This new squashfs archive can be stored on the boot device and the
2370 +initramfs will use it to replace the old one at the next boot.
2371 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2372 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2373 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2017-07-29 12:14:25.896375188 +0200
2374 @@ -0,0 +1,47 @@
2375 +
2376 +# Copyright (C) 2010-2017 Junjiro R. Okajima
2377 +#
2378 +# This program is free software; you can redistribute it and/or modify
2379 +# it under the terms of the GNU General Public License as published by
2380 +# the Free Software Foundation; either version 2 of the License, or
2381 +# (at your option) any later version.
2382 +#
2383 +# This program is distributed in the hope that it will be useful,
2384 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2385 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2386 +# GNU General Public License for more details.
2387 +#
2388 +# You should have received a copy of the GNU General Public License
2389 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2390 +
2391 +Dynamically customizable FS operations
2392 +----------------------------------------------------------------------
2393 +Generally FS operations (struct inode_operations, struct
2394 +address_space_operations, struct file_operations, etc.) are defined as
2395 +"static const", but it never means that FS have only one set of
2396 +operation. Some FS have multiple sets of them. For instance, ext2 has
2397 +three sets, one for XIP, for NOBH, and for normal.
2398 +Since aufs overrides and redirects these operations, sometimes aufs has
2399 +to change its behaviour according to the branch FS type. More importantly
2400 +VFS acts differently if a function (member in the struct) is set or
2401 +not. It means aufs should have several sets of operations and select one
2402 +among them according to the branch FS definition.
2403 +
2404 +In order to solve this problem and not to affect the behaviour of VFS,
2405 +aufs defines these operations dynamically. For instance, aufs defines
2406 +dummy direct_IO function for struct address_space_operations, but it may
2407 +not be set to the address_space_operations actually. When the branch FS
2408 +doesn't have it, aufs doesn't set it to its address_space_operations
2409 +while the function definition itself is still alive. So the behaviour
2410 +itself will not change, and it will return an error when direct_IO is
2411 +not set.
2412 +
2413 +The lifetime of these dynamically generated operation object is
2414 +maintained by aufs branch object. When the branch is removed from aufs,
2415 +the reference counter of the object is decremented. When it reaches
2416 +zero, the dynamically generated operation object will be freed.
2417 +
2418 +This approach is designed to support AIO (io_submit), Direct I/O and
2419 +XIP (DAX) mainly.
2420 +Currently this approach is applied to address_space_operations for
2421 +regular files only.
2422 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2423 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2424 +++ linux/Documentation/filesystems/aufs/README 2017-07-29 12:14:25.893041746 +0200
2425 @@ -0,0 +1,393 @@
2426 +
2427 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2428 +http://aufs.sf.net
2429 +Junjiro R. Okajima
2430 +
2431 +
2432 +0. Introduction
2433 +----------------------------------------
2434 +In the early days, aufs was entirely re-designed and re-implemented
2435 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2436 +improvements and implementations, it becomes totally different from
2437 +Unionfs while keeping the basic features.
2438 +Recently, Unionfs Version 2.x series begin taking some of the same
2439 +approaches to aufs1's.
2440 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2441 +University and his team.
2442 +
2443 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2444 +If you want older kernel version support, try aufs2-2.6.git or
2445 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2446 +
2447 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2448 +      According to Christoph Hellwig, linux rejects all union-type
2449 +      filesystems but UnionMount.
2450 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2451 +
2452 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2453 +    UnionMount, and he pointed out an issue around a directory mutex
2454 +    lock and aufs addressed it. But it is still unsure whether aufs will
2455 +    be merged (or any other union solution).
2456 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2457 +
2458 +
2459 +1. Features
2460 +----------------------------------------
2461 +- unite several directories into a single virtual filesystem. The member
2462 +  directory is called as a branch.
2463 +- you can specify the permission flags to the branch, which are 'readonly',
2464 +  'readwrite' and 'whiteout-able.'
2465 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2466 +  readonly branch are modifiable logically.
2467 +- dynamic branch manipulation, add, del.
2468 +- etc...
2469 +
2470 +Also there are many enhancements in aufs, such as:
2471 +- test only the highest one for the directory permission (dirperm1)
2472 +- copyup on open (coo=)
2473 +- 'move' policy for copy-up between two writable branches, after
2474 +  checking free space.
2475 +- xattr, acl
2476 +- readdir(3) in userspace.
2477 +- keep inode number by external inode number table
2478 +- keep the timestamps of file/dir in internal copyup operation
2479 +- seekable directory, supporting NFS readdir.
2480 +- whiteout is hardlinked in order to reduce the consumption of inodes
2481 +  on branch
2482 +- do not copyup, nor create a whiteout when it is unnecessary
2483 +- revert a single systemcall when an error occurs in aufs
2484 +- remount interface instead of ioctl
2485 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2486 +- loopback mounted filesystem as a branch
2487 +- kernel thread for removing the dir who has a plenty of whiteouts
2488 +- support copyup sparse file (a file which has a 'hole' in it)
2489 +- default permission flags for branches
2490 +- selectable permission flags for ro branch, whether whiteout can
2491 +  exist or not
2492 +- export via NFS.
2493 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2494 +- support multiple writable branches, some policies to select one
2495 +  among multiple writable branches.
2496 +- a new semantics for link(2) and rename(2) to support multiple
2497 +  writable branches.
2498 +- no glibc changes are required.
2499 +- pseudo hardlink (hardlink over branches)
2500 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2501 +  including NFS or remote filesystem branch.
2502 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2503 +- and more...
2504 +
2505 +Currently these features are dropped temporary from aufs4.
2506 +See design/08plan.txt in detail.
2507 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2508 +  (robr)
2509 +- statistics of aufs thread (/sys/fs/aufs/stat)
2510 +
2511 +Features or just an idea in the future (see also design/*.txt),
2512 +- reorder the branch index without del/re-add.
2513 +- permanent xino files for NFSD
2514 +- an option for refreshing the opened files after add/del branches
2515 +- light version, without branch manipulation. (unnecessary?)
2516 +- copyup in userspace
2517 +- inotify in userspace
2518 +- readv/writev
2519 +
2520 +
2521 +2. Download
2522 +----------------------------------------
2523 +There are three GIT trees for aufs4, aufs4-linux.git,
2524 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2525 +"aufs-util.git."
2526 +While the aufs-util is always necessary, you need either of aufs4-linux
2527 +or aufs4-standalone.
2528 +
2529 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2530 +git://git.kernel.org/.../torvalds/linux.git.
2531 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2532 +build aufs4 as an external kernel module.
2533 +Several extra patches are not included in this tree. Only
2534 +aufs4-standalone tree contains them. They are described in the later
2535 +section "Configuration and Compilation."
2536 +
2537 +On the other hand, the aufs4-standalone tree has only aufs source files
2538 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2539 +But you need to apply all aufs patches manually.
2540 +
2541 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2542 +represents the linux kernel version, "linux-4.x". For instance,
2543 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2544 +"aufs4.x-rcN" branch.
2545 +
2546 +o aufs4-linux tree
2547 +$ git clone --reference /your/linux/git/tree \
2548 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2549 +- if you don't have linux GIT tree, then remove "--reference ..."
2550 +$ cd aufs4-linux.git
2551 +$ git checkout origin/aufs4.0
2552 +
2553 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2554 +leave the patch-work to GIT.
2555 +$ cd /your/linux/git/tree
2556 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2557 +$ git fetch aufs4
2558 +$ git checkout -b my4.0 v4.0
2559 +$ (add your local change...)
2560 +$ git pull aufs4 aufs4.0
2561 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2562 +- you may need to solve some conflicts between your_changes and
2563 +  aufs4.0. in this case, git-rerere is recommended so that you can
2564 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2565 +  later in the future.
2566 +
2567 +o aufs4-standalone tree
2568 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2569 +$ cd aufs4-standalone.git
2570 +$ git checkout origin/aufs4.0
2571 +
2572 +o aufs-util tree
2573 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2574 +- note that the public aufs-util.git is on SourceForge instead of
2575 +  GitHUB.
2576 +$ cd aufs-util.git
2577 +$ git checkout origin/aufs4.0
2578 +
2579 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2580 +The minor version number, 'x' in '4.x', of aufs may not always
2581 +follow the minor version number of the kernel.
2582 +Because changes in the kernel that cause the use of a new
2583 +minor version number do not always require changes to aufs-util.
2584 +
2585 +Since aufs-util has its own minor version number, you may not be
2586 +able to find a GIT branch in aufs-util for your kernel's
2587 +exact minor version number.
2588 +In this case, you should git-checkout the branch for the
2589 +nearest lower number.
2590 +
2591 +For (an unreleased) example:
2592 +If you are using "linux-4.10" and the "aufs4.10" branch
2593 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2594 +or something numerically smaller is the branch for your kernel.
2595 +
2596 +Also you can view all branches by
2597 +       $ git branch -a
2598 +
2599 +
2600 +3. Configuration and Compilation
2601 +----------------------------------------
2602 +Make sure you have git-checkout'ed the correct branch.
2603 +
2604 +For aufs4-linux tree,
2605 +- enable CONFIG_AUFS_FS.
2606 +- set other aufs configurations if necessary.
2607 +
2608 +For aufs4-standalone tree,
2609 +There are several ways to build.
2610 +
2611 +1.
2612 +- apply ./aufs4-kbuild.patch to your kernel source files.
2613 +- apply ./aufs4-base.patch too.
2614 +- apply ./aufs4-mmap.patch too.
2615 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2616 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2617 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2618 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2619 +- enable CONFIG_AUFS_FS, you can select either
2620 +  =m or =y.
2621 +- and build your kernel as usual.
2622 +- install the built kernel.
2623 +  Note: Since linux-3.9, every filesystem module requires an alias
2624 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2625 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2626 +- install the header files too by "make headers_install" to the
2627 +  directory where you specify. By default, it is $PWD/usr.
2628 +  "make help" shows a brief note for headers_install.
2629 +- and reboot your system.
2630 +
2631 +2.
2632 +- module only (CONFIG_AUFS_FS=m).
2633 +- apply ./aufs4-base.patch to your kernel source files.
2634 +- apply ./aufs4-mmap.patch too.
2635 +- apply ./aufs4-standalone.patch too.
2636 +- build your kernel, don't forget "make headers_install", and reboot.
2637 +- edit ./config.mk and set other aufs configurations if necessary.
2638 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2639 +  every aufs configurations.
2640 +- build the module by simple "make".
2641 +  Note: Since linux-3.9, every filesystem module requires an alias
2642 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2643 +  modules.aliases file.
2644 +- you can specify ${KDIR} make variable which points to your kernel
2645 +  source tree.
2646 +- install the files
2647 +  + run "make install" to install the aufs module, or copy the built
2648 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2649 +  + run "make install_headers" (instead of headers_install) to install
2650 +    the modified aufs header file (you can specify DESTDIR which is
2651 +    available in aufs standalone version's Makefile only), or copy
2652 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2653 +    you like manually. By default, the target directory is $PWD/usr.
2654 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2655 +  kernel source tree.
2656 +
2657 +Note: The header file aufs_type.h is necessary to build aufs-util
2658 +      as well as "make headers_install" in the kernel source tree.
2659 +      headers_install is subject to be forgotten, but it is essentially
2660 +      necessary, not only for building aufs-util.
2661 +      You may not meet problems without headers_install in some older
2662 +      version though.
2663 +
2664 +And then,
2665 +- read README in aufs-util, build and install it
2666 +- note that your distribution may contain an obsoleted version of
2667 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2668 +  utilities, make sure that your compiler refers the correct aufs header
2669 +  file which is built by "make headers_install."
2670 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2671 +  then run "make install_ulib" too. And refer to the aufs manual in
2672 +  detail.
2673 +
2674 +There several other patches in aufs4-standalone.git. They are all
2675 +optional. When you meet some problems, they will help you.
2676 +- aufs4-loopback.patch
2677 +  Supports a nested loopback mount in a branch-fs. This patch is
2678 +  unnecessary until aufs produces a message like "you may want to try
2679 +  another patch for loopback file".
2680 +- vfs-ino.patch
2681 +  Modifies a system global kernel internal function get_next_ino() in
2682 +  order to stop assigning 0 for an inode-number. Not directly related to
2683 +  aufs, but recommended generally.
2684 +- tmpfs-idr.patch
2685 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2686 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2687 +  duplication of inode number, which is important for backup tools and
2688 +  other utilities. When you find aufs XINO files for tmpfs branch
2689 +  growing too much, try this patch.
2690 +- lockdep-debug.patch
2691 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2692 +  also a caller of VFS functions for branch filesystems, subclassing of
2693 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2694 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2695 +  need to apply this debug patch to expand several constant values.
2696 +  If don't know what LOCKDEP, then you don't have apply this patch.
2697 +
2698 +
2699 +4. Usage
2700 +----------------------------------------
2701 +At first, make sure aufs-util are installed, and please read the aufs
2702 +manual, aufs.5 in aufs-util.git tree.
2703 +$ man -l aufs.5
2704 +
2705 +And then,
2706 +$ mkdir /tmp/rw /tmp/aufs
2707 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2708 +
2709 +Here is another example. The result is equivalent.
2710 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2711 +  Or
2712 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2713 +# mount -o remount,append:${HOME} /tmp/aufs
2714 +
2715 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2716 +you modify a file under /tmp/aufs, the one on your home directory is
2717 +not affected, instead the same named file will be newly created under
2718 +/tmp/rw. And all of your modification to a file will be applied to
2719 +the one under /tmp/rw. This is called the file based Copy on Write
2720 +(COW) method.
2721 +Aufs mount options are described in aufs.5.
2722 +If you run chroot or something and make your aufs as a root directory,
2723 +then you need to customize the shutdown script. See the aufs manual in
2724 +detail.
2725 +
2726 +Additionally, there are some sample usages of aufs which are a
2727 +diskless system with network booting, and LiveCD over NFS.
2728 +See sample dir in CVS tree on SourceForge.
2729 +
2730 +
2731 +5. Contact
2732 +----------------------------------------
2733 +When you have any problems or strange behaviour in aufs, please let me
2734 +know with:
2735 +- /proc/mounts (instead of the output of mount(8))
2736 +- /sys/module/aufs/*
2737 +- /sys/fs/aufs/* (if you have them)
2738 +- /debug/aufs/* (if you have them)
2739 +- linux kernel version
2740 +  if your kernel is not plain, for example modified by distributor,
2741 +  the url where i can download its source is necessary too.
2742 +- aufs version which was printed at loading the module or booting the
2743 +  system, instead of the date you downloaded.
2744 +- configuration (define/undefine CONFIG_AUFS_xxx)
2745 +- kernel configuration or /proc/config.gz (if you have it)
2746 +- behaviour which you think to be incorrect
2747 +- actual operation, reproducible one is better
2748 +- mailto: aufs-users at lists.sourceforge.net
2749 +
2750 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2751 +and Feature Requests) on SourceForge. Please join and write to
2752 +aufs-users ML.
2753 +
2754 +
2755 +6. Acknowledgements
2756 +----------------------------------------
2757 +Thanks to everyone who have tried and are using aufs, whoever
2758 +have reported a bug or any feedback.
2759 +
2760 +Especially donators:
2761 +Tomas Matejicek(slax.org) made a donation (much more than once).
2762 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2763 +       scripts) is making "doubling" donations.
2764 +       Unfortunately I cannot list all of the donators, but I really
2765 +       appreciate.
2766 +       It ends Aug 2010, but the ordinary donation URL is still available.
2767 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2768 +Dai Itasaka made a donation (2007/8).
2769 +Chuck Smith made a donation (2008/4, 10 and 12).
2770 +Henk Schoneveld made a donation (2008/9).
2771 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2772 +Francois Dupoux made a donation (2008/11).
2773 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2774 +       aufs2 GIT tree (2009/2).
2775 +William Grant made a donation (2009/3).
2776 +Patrick Lane made a donation (2009/4).
2777 +The Mail Archive (mail-archive.com) made donations (2009/5).
2778 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2779 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2780 +Pavel Pronskiy made a donation (2011/2).
2781 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2782 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2783 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2784 +11).
2785 +Sam Liddicott made a donation (2011/9).
2786 +Era Scarecrow made a donation (2013/4).
2787 +Bor Ratajc made a donation (2013/4).
2788 +Alessandro Gorreta made a donation (2013/4).
2789 +POIRETTE Marc made a donation (2013/4).
2790 +Alessandro Gorreta made a donation (2013/4).
2791 +lauri kasvandik made a donation (2013/5).
2792 +"pemasu from Finland" made a donation (2013/7).
2793 +The Parted Magic Project made a donation (2013/9 and 11).
2794 +Pavel Barta made a donation (2013/10).
2795 +Nikolay Pertsev made a donation (2014/5).
2796 +James B made a donation (2014/7 and 2015/7).
2797 +Stefano Di Biase made a donation (2014/8).
2798 +Daniel Epellei made a donation (2015/1).
2799 +OmegaPhil made a donation (2016/1).
2800 +Tomasz Szewczyk made a donation (2016/4).
2801 +James Burry made a donation (2016/12).
2802 +
2803 +Thank you very much.
2804 +Donations are always, including future donations, very important and
2805 +helpful for me to keep on developing aufs.
2806 +
2807 +
2808 +7.
2809 +----------------------------------------
2810 +If you are an experienced user, no explanation is needed. Aufs is
2811 +just a linux filesystem.
2812 +
2813 +
2814 +Enjoy!
2815 +
2816 +# Local variables: ;
2817 +# mode: text;
2818 +# End: ;
2819 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2820 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2821 +++ linux/fs/aufs/aufs.h        2017-07-29 12:14:25.896375188 +0200
2822 @@ -0,0 +1,59 @@
2823 +/*
2824 + * Copyright (C) 2005-2017 Junjiro R. Okajima
2825 + *
2826 + * This program, aufs is free software; you can redistribute it and/or modify
2827 + * it under the terms of the GNU General Public License as published by
2828 + * the Free Software Foundation; either version 2 of the License, or
2829 + * (at your option) any later version.
2830 + *
2831 + * This program is distributed in the hope that it will be useful,
2832 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2833 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2834 + * GNU General Public License for more details.
2835 + *
2836 + * You should have received a copy of the GNU General Public License
2837 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2838 + */
2839 +
2840 +/*
2841 + * all header files
2842 + */
2843 +
2844 +#ifndef __AUFS_H__
2845 +#define __AUFS_H__
2846 +
2847 +#ifdef __KERNEL__
2848 +
2849 +#define AuStub(type, name, body, ...) \
2850 +       static inline type name(__VA_ARGS__) { body; }
2851 +
2852 +#define AuStubVoid(name, ...) \
2853 +       AuStub(void, name, , __VA_ARGS__)
2854 +#define AuStubInt0(name, ...) \
2855 +       AuStub(int, name, return 0, __VA_ARGS__)
2856 +
2857 +#include "debug.h"
2858 +
2859 +#include "branch.h"
2860 +#include "cpup.h"
2861 +#include "dcsub.h"
2862 +#include "dbgaufs.h"
2863 +#include "dentry.h"
2864 +#include "dir.h"
2865 +#include "dynop.h"
2866 +#include "file.h"
2867 +#include "fstype.h"
2868 +#include "inode.h"
2869 +#include "loop.h"
2870 +#include "module.h"
2871 +#include "opts.h"
2872 +#include "rwsem.h"
2873 +#include "spl.h"
2874 +#include "super.h"
2875 +#include "sysaufs.h"
2876 +#include "vfsub.h"
2877 +#include "whout.h"
2878 +#include "wkq.h"
2879 +
2880 +#endif /* __KERNEL__ */
2881 +#endif /* __AUFS_H__ */
2882 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2883 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2884 +++ linux/fs/aufs/branch.c      2017-07-29 12:14:25.896375188 +0200
2885 @@ -0,0 +1,1422 @@
2886 +/*
2887 + * Copyright (C) 2005-2017 Junjiro R. Okajima
2888 + *
2889 + * This program, aufs is free software; you can redistribute it and/or modify
2890 + * it under the terms of the GNU General Public License as published by
2891 + * the Free Software Foundation; either version 2 of the License, or
2892 + * (at your option) any later version.
2893 + *
2894 + * This program is distributed in the hope that it will be useful,
2895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2897 + * GNU General Public License for more details.
2898 + *
2899 + * You should have received a copy of the GNU General Public License
2900 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2901 + */
2902 +
2903 +/*
2904 + * branch management
2905 + */
2906 +
2907 +#include <linux/compat.h>
2908 +#include <linux/statfs.h>
2909 +#include "aufs.h"
2910 +
2911 +/*
2912 + * free a single branch
2913 + */
2914 +static void au_br_do_free(struct au_branch *br)
2915 +{
2916 +       int i;
2917 +       struct au_wbr *wbr;
2918 +       struct au_dykey **key;
2919 +
2920 +       au_hnotify_fin_br(br);
2921 +
2922 +       if (br->br_xino.xi_file)
2923 +               fput(br->br_xino.xi_file);
2924 +       for (i = br->br_xino.xi_nondir.total - 1; i >= 0; i--)
2925 +               AuDebugOn(br->br_xino.xi_nondir.array[i]);
2926 +       kfree(br->br_xino.xi_nondir.array);
2927 +
2928 +       AuDebugOn(au_br_count(br));
2929 +       au_br_count_fin(br);
2930 +
2931 +       wbr = br->br_wbr;
2932 +       if (wbr) {
2933 +               for (i = 0; i < AuBrWh_Last; i++)
2934 +                       dput(wbr->wbr_wh[i]);
2935 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2936 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2937 +       }
2938 +
2939 +       if (br->br_fhsm) {
2940 +               au_br_fhsm_fin(br->br_fhsm);
2941 +               kfree(br->br_fhsm);
2942 +       }
2943 +
2944 +       key = br->br_dykey;
2945 +       for (i = 0; i < AuBrDynOp; i++, key++)
2946 +               if (*key)
2947 +                       au_dy_put(*key);
2948 +               else
2949 +                       break;
2950 +
2951 +       /* recursive lock, s_umount of branch's */
2952 +       lockdep_off();
2953 +       path_put(&br->br_path);
2954 +       lockdep_on();
2955 +       kfree(wbr);
2956 +       kfree(br);
2957 +}
2958 +
2959 +/*
2960 + * frees all branches
2961 + */
2962 +void au_br_free(struct au_sbinfo *sbinfo)
2963 +{
2964 +       aufs_bindex_t bmax;
2965 +       struct au_branch **br;
2966 +
2967 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2968 +
2969 +       bmax = sbinfo->si_bbot + 1;
2970 +       br = sbinfo->si_branch;
2971 +       while (bmax--)
2972 +               au_br_do_free(*br++);
2973 +}
2974 +
2975 +/*
2976 + * find the index of a branch which is specified by @br_id.
2977 + */
2978 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2979 +{
2980 +       aufs_bindex_t bindex, bbot;
2981 +
2982 +       bbot = au_sbbot(sb);
2983 +       for (bindex = 0; bindex <= bbot; bindex++)
2984 +               if (au_sbr_id(sb, bindex) == br_id)
2985 +                       return bindex;
2986 +       return -1;
2987 +}
2988 +
2989 +/* ---------------------------------------------------------------------- */
2990 +
2991 +/*
2992 + * add a branch
2993 + */
2994 +
2995 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2996 +                       struct dentry *h_root)
2997 +{
2998 +       if (unlikely(h_adding == h_root
2999 +                    || au_test_loopback_overlap(sb, h_adding)))
3000 +               return 1;
3001 +       if (h_adding->d_sb != h_root->d_sb)
3002 +               return 0;
3003 +       return au_test_subdir(h_adding, h_root)
3004 +               || au_test_subdir(h_root, h_adding);
3005 +}
3006 +
3007 +/*
3008 + * returns a newly allocated branch. @new_nbranch is a number of branches
3009 + * after adding a branch.
3010 + */
3011 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3012 +                                    int perm)
3013 +{
3014 +       struct au_branch *add_branch;
3015 +       struct dentry *root;
3016 +       struct inode *inode;
3017 +       int err;
3018 +
3019 +       err = -ENOMEM;
3020 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3021 +       if (unlikely(!add_branch))
3022 +               goto out;
3023 +       add_branch->br_xino.xi_nondir.total = 8; /* initial size */
3024 +       add_branch->br_xino.xi_nondir.array
3025 +               = kzalloc(sizeof(ino_t) * add_branch->br_xino.xi_nondir.total,
3026 +                         GFP_NOFS);
3027 +       if (unlikely(!add_branch->br_xino.xi_nondir.array))
3028 +               goto out_br;
3029 +
3030 +       err = au_hnotify_init_br(add_branch, perm);
3031 +       if (unlikely(err))
3032 +               goto out_xinondir;
3033 +
3034 +       if (au_br_writable(perm)) {
3035 +               /* may be freed separately at changing the branch permission */
3036 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3037 +                                            GFP_NOFS);
3038 +               if (unlikely(!add_branch->br_wbr))
3039 +                       goto out_hnotify;
3040 +       }
3041 +
3042 +       if (au_br_fhsm(perm)) {
3043 +               err = au_fhsm_br_alloc(add_branch);
3044 +               if (unlikely(err))
3045 +                       goto out_wbr;
3046 +       }
3047 +
3048 +       root = sb->s_root;
3049 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3050 +       if (!err)
3051 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3052 +       if (!err) {
3053 +               inode = d_inode(root);
3054 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3055 +                                       /*may_shrink*/0);
3056 +       }
3057 +       if (!err)
3058 +               return add_branch; /* success */
3059 +
3060 +out_wbr:
3061 +       kfree(add_branch->br_wbr);
3062 +out_hnotify:
3063 +       au_hnotify_fin_br(add_branch);
3064 +out_xinondir:
3065 +       kfree(add_branch->br_xino.xi_nondir.array);
3066 +out_br:
3067 +       kfree(add_branch);
3068 +out:
3069 +       return ERR_PTR(err);
3070 +}
3071 +
3072 +/*
3073 + * test if the branch permission is legal or not.
3074 + */
3075 +static int test_br(struct inode *inode, int brperm, char *path)
3076 +{
3077 +       int err;
3078 +
3079 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3080 +       if (!err)
3081 +               goto out;
3082 +
3083 +       err = -EINVAL;
3084 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3085 +
3086 +out:
3087 +       return err;
3088 +}
3089 +
3090 +/*
3091 + * returns:
3092 + * 0: success, the caller will add it
3093 + * plus: success, it is already unified, the caller should ignore it
3094 + * minus: error
3095 + */
3096 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3097 +{
3098 +       int err;
3099 +       aufs_bindex_t bbot, bindex;
3100 +       struct dentry *root, *h_dentry;
3101 +       struct inode *inode, *h_inode;
3102 +
3103 +       root = sb->s_root;
3104 +       bbot = au_sbbot(sb);
3105 +       if (unlikely(bbot >= 0
3106 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3107 +               err = 1;
3108 +               if (!remount) {
3109 +                       err = -EINVAL;
3110 +                       pr_err("%s duplicated\n", add->pathname);
3111 +               }
3112 +               goto out;
3113 +       }
3114 +
3115 +       err = -ENOSPC; /* -E2BIG; */
3116 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3117 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3118 +               pr_err("number of branches exceeded %s\n", add->pathname);
3119 +               goto out;
3120 +       }
3121 +
3122 +       err = -EDOM;
3123 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3124 +               pr_err("bad index %d\n", add->bindex);
3125 +               goto out;
3126 +       }
3127 +
3128 +       inode = d_inode(add->path.dentry);
3129 +       err = -ENOENT;
3130 +       if (unlikely(!inode->i_nlink)) {
3131 +               pr_err("no existence %s\n", add->pathname);
3132 +               goto out;
3133 +       }
3134 +
3135 +       err = -EINVAL;
3136 +       if (unlikely(inode->i_sb == sb)) {
3137 +               pr_err("%s must be outside\n", add->pathname);
3138 +               goto out;
3139 +       }
3140 +
3141 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3142 +               pr_err("unsupported filesystem, %s (%s)\n",
3143 +                      add->pathname, au_sbtype(inode->i_sb));
3144 +               goto out;
3145 +       }
3146 +
3147 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3148 +               pr_err("already stacked, %s (%s)\n",
3149 +                      add->pathname, au_sbtype(inode->i_sb));
3150 +               goto out;
3151 +       }
3152 +
3153 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3154 +       if (unlikely(err))
3155 +               goto out;
3156 +
3157 +       if (bbot < 0)
3158 +               return 0; /* success */
3159 +
3160 +       err = -EINVAL;
3161 +       for (bindex = 0; bindex <= bbot; bindex++)
3162 +               if (unlikely(test_overlap(sb, add->path.dentry,
3163 +                                         au_h_dptr(root, bindex)))) {
3164 +                       pr_err("%s is overlapped\n", add->pathname);
3165 +                       goto out;
3166 +               }
3167 +
3168 +       err = 0;
3169 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3170 +               h_dentry = au_h_dptr(root, 0);
3171 +               h_inode = d_inode(h_dentry);
3172 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3173 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3174 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3175 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3176 +                               add->pathname,
3177 +                               i_uid_read(inode), i_gid_read(inode),
3178 +                               (inode->i_mode & S_IALLUGO),
3179 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3180 +                               (h_inode->i_mode & S_IALLUGO));
3181 +       }
3182 +
3183 +out:
3184 +       return err;
3185 +}
3186 +
3187 +/*
3188 + * initialize or clean the whiteouts for an adding branch
3189 + */
3190 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3191 +                        int new_perm)
3192 +{
3193 +       int err, old_perm;
3194 +       aufs_bindex_t bindex;
3195 +       struct inode *h_inode;
3196 +       struct au_wbr *wbr;
3197 +       struct au_hinode *hdir;
3198 +       struct dentry *h_dentry;
3199 +
3200 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3201 +       if (unlikely(err))
3202 +               goto out;
3203 +
3204 +       wbr = br->br_wbr;
3205 +       old_perm = br->br_perm;
3206 +       br->br_perm = new_perm;
3207 +       hdir = NULL;
3208 +       h_inode = NULL;
3209 +       bindex = au_br_index(sb, br->br_id);
3210 +       if (0 <= bindex) {
3211 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3212 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3213 +       } else {
3214 +               h_dentry = au_br_dentry(br);
3215 +               h_inode = d_inode(h_dentry);
3216 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3217 +       }
3218 +       if (!wbr)
3219 +               err = au_wh_init(br, sb);
3220 +       else {
3221 +               wbr_wh_write_lock(wbr);
3222 +               err = au_wh_init(br, sb);
3223 +               wbr_wh_write_unlock(wbr);
3224 +       }
3225 +       if (hdir)
3226 +               au_hn_inode_unlock(hdir);
3227 +       else
3228 +               inode_unlock(h_inode);
3229 +       vfsub_mnt_drop_write(au_br_mnt(br));
3230 +       br->br_perm = old_perm;
3231 +
3232 +       if (!err && wbr && !au_br_writable(new_perm)) {
3233 +               kfree(wbr);
3234 +               br->br_wbr = NULL;
3235 +       }
3236 +
3237 +out:
3238 +       return err;
3239 +}
3240 +
3241 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3242 +                      int perm)
3243 +{
3244 +       int err;
3245 +       struct kstatfs kst;
3246 +       struct au_wbr *wbr;
3247 +
3248 +       wbr = br->br_wbr;
3249 +       au_rw_init(&wbr->wbr_wh_rwsem);
3250 +       atomic_set(&wbr->wbr_wh_running, 0);
3251 +
3252 +       /*
3253 +        * a limit for rmdir/rename a dir
3254 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3255 +        */
3256 +       err = vfs_statfs(&br->br_path, &kst);
3257 +       if (unlikely(err))
3258 +               goto out;
3259 +       err = -EINVAL;
3260 +       if (kst.f_namelen >= NAME_MAX)
3261 +               err = au_br_init_wh(sb, br, perm);
3262 +       else
3263 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3264 +                      au_br_dentry(br),
3265 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3266 +
3267 +out:
3268 +       return err;
3269 +}
3270 +
3271 +/* initialize a new branch */
3272 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3273 +                     struct au_opt_add *add)
3274 +{
3275 +       int err;
3276 +       struct inode *h_inode;
3277 +
3278 +       err = 0;
3279 +       spin_lock_init(&br->br_xino.xi_nondir.spin);
3280 +       init_waitqueue_head(&br->br_xino.xi_nondir.wqh);
3281 +       br->br_perm = add->perm;
3282 +       br->br_path = add->path; /* set first, path_get() later */
3283 +       spin_lock_init(&br->br_dykey_lock);
3284 +       au_br_count_init(br);
3285 +       atomic_set(&br->br_xino_running, 0);
3286 +       br->br_id = au_new_br_id(sb);
3287 +       AuDebugOn(br->br_id < 0);
3288 +
3289 +       if (au_br_writable(add->perm)) {
3290 +               err = au_wbr_init(br, sb, add->perm);
3291 +               if (unlikely(err))
3292 +                       goto out_err;
3293 +       }
3294 +
3295 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3296 +               h_inode = d_inode(add->path.dentry);
3297 +               err = au_xino_br(sb, br, h_inode->i_ino,
3298 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3299 +               if (unlikely(err)) {
3300 +                       AuDebugOn(br->br_xino.xi_file);
3301 +                       goto out_err;
3302 +               }
3303 +       }
3304 +
3305 +       sysaufs_br_init(br);
3306 +       path_get(&br->br_path);
3307 +       goto out; /* success */
3308 +
3309 +out_err:
3310 +       memset(&br->br_path, 0, sizeof(br->br_path));
3311 +out:
3312 +       return err;
3313 +}
3314 +
3315 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3316 +                            struct au_branch *br, aufs_bindex_t bbot,
3317 +                            aufs_bindex_t amount)
3318 +{
3319 +       struct au_branch **brp;
3320 +
3321 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3322 +
3323 +       brp = sbinfo->si_branch + bindex;
3324 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3325 +       *brp = br;
3326 +       sbinfo->si_bbot++;
3327 +       if (unlikely(bbot < 0))
3328 +               sbinfo->si_bbot = 0;
3329 +}
3330 +
3331 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3332 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3333 +{
3334 +       struct au_hdentry *hdp;
3335 +
3336 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3337 +
3338 +       hdp = au_hdentry(dinfo, bindex);
3339 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3340 +       au_h_dentry_init(hdp);
3341 +       dinfo->di_bbot++;
3342 +       if (unlikely(bbot < 0))
3343 +               dinfo->di_btop = 0;
3344 +}
3345 +
3346 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3347 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3348 +{
3349 +       struct au_hinode *hip;
3350 +
3351 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3352 +
3353 +       hip = au_hinode(iinfo, bindex);
3354 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3355 +       au_hinode_init(hip);
3356 +       iinfo->ii_bbot++;
3357 +       if (unlikely(bbot < 0))
3358 +               iinfo->ii_btop = 0;
3359 +}
3360 +
3361 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3362 +                        aufs_bindex_t bindex)
3363 +{
3364 +       struct dentry *root, *h_dentry;
3365 +       struct inode *root_inode, *h_inode;
3366 +       aufs_bindex_t bbot, amount;
3367 +
3368 +       root = sb->s_root;
3369 +       root_inode = d_inode(root);
3370 +       bbot = au_sbbot(sb);
3371 +       amount = bbot + 1 - bindex;
3372 +       h_dentry = au_br_dentry(br);
3373 +       au_sbilist_lock();
3374 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3375 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3376 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3377 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3378 +       h_inode = d_inode(h_dentry);
3379 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3380 +       au_sbilist_unlock();
3381 +}
3382 +
3383 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3384 +{
3385 +       int err;
3386 +       aufs_bindex_t bbot, add_bindex;
3387 +       struct dentry *root, *h_dentry;
3388 +       struct inode *root_inode;
3389 +       struct au_branch *add_branch;
3390 +
3391 +       root = sb->s_root;
3392 +       root_inode = d_inode(root);
3393 +       IMustLock(root_inode);
3394 +       IiMustWriteLock(root_inode);
3395 +       err = test_add(sb, add, remount);
3396 +       if (unlikely(err < 0))
3397 +               goto out;
3398 +       if (err) {
3399 +               err = 0;
3400 +               goto out; /* success */
3401 +       }
3402 +
3403 +       bbot = au_sbbot(sb);
3404 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3405 +       err = PTR_ERR(add_branch);
3406 +       if (IS_ERR(add_branch))
3407 +               goto out;
3408 +
3409 +       err = au_br_init(add_branch, sb, add);
3410 +       if (unlikely(err)) {
3411 +               au_br_do_free(add_branch);
3412 +               goto out;
3413 +       }
3414 +
3415 +       add_bindex = add->bindex;
3416 +       if (!remount)
3417 +               au_br_do_add(sb, add_branch, add_bindex);
3418 +       else {
3419 +               sysaufs_brs_del(sb, add_bindex);
3420 +               au_br_do_add(sb, add_branch, add_bindex);
3421 +               sysaufs_brs_add(sb, add_bindex);
3422 +       }
3423 +
3424 +       h_dentry = add->path.dentry;
3425 +       if (!add_bindex) {
3426 +               au_cpup_attr_all(root_inode, /*force*/1);
3427 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3428 +       } else
3429 +               au_add_nlink(root_inode, d_inode(h_dentry));
3430 +
3431 +       /*
3432 +        * this test/set prevents aufs from handling unnecesary notify events
3433 +        * of xino files, in case of re-adding a writable branch which was
3434 +        * once detached from aufs.
3435 +        */
3436 +       if (au_xino_brid(sb) < 0
3437 +           && au_br_writable(add_branch->br_perm)
3438 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3439 +           && add_branch->br_xino.xi_file
3440 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3441 +               au_xino_brid_set(sb, add_branch->br_id);
3442 +
3443 +out:
3444 +       return err;
3445 +}
3446 +
3447 +/* ---------------------------------------------------------------------- */
3448 +
3449 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3450 +                                      unsigned long long max __maybe_unused,
3451 +                                      void *arg)
3452 +{
3453 +       unsigned long long n;
3454 +       struct file **p, *f;
3455 +       struct au_sphlhead *files;
3456 +       struct au_finfo *finfo;
3457 +
3458 +       n = 0;
3459 +       p = a;
3460 +       files = &au_sbi(sb)->si_files;
3461 +       spin_lock(&files->spin);
3462 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3463 +               f = finfo->fi_file;
3464 +               if (file_count(f)
3465 +                   && !special_file(file_inode(f)->i_mode)) {
3466 +                       get_file(f);
3467 +                       *p++ = f;
3468 +                       n++;
3469 +                       AuDebugOn(n > max);
3470 +               }
3471 +       }
3472 +       spin_unlock(&files->spin);
3473 +
3474 +       return n;
3475 +}
3476 +
3477 +static struct file **au_farray_alloc(struct super_block *sb,
3478 +                                    unsigned long long *max)
3479 +{
3480 +       *max = au_nfiles(sb);
3481 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3482 +}
3483 +
3484 +static void au_farray_free(struct file **a, unsigned long long max)
3485 +{
3486 +       unsigned long long ull;
3487 +
3488 +       for (ull = 0; ull < max; ull++)
3489 +               if (a[ull])
3490 +                       fput(a[ull]);
3491 +       kvfree(a);
3492 +}
3493 +
3494 +/* ---------------------------------------------------------------------- */
3495 +
3496 +/*
3497 + * delete a branch
3498 + */
3499 +
3500 +/* to show the line number, do not make it inlined function */
3501 +#define AuVerbose(do_info, fmt, ...) do { \
3502 +       if (do_info) \
3503 +               pr_info(fmt, ##__VA_ARGS__); \
3504 +} while (0)
3505 +
3506 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3507 +                        aufs_bindex_t bbot)
3508 +{
3509 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3510 +}
3511 +
3512 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3513 +                        aufs_bindex_t bbot)
3514 +{
3515 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3516 +}
3517 +
3518 +/*
3519 + * test if the branch is deletable or not.
3520 + */
3521 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3522 +                           unsigned int sigen, const unsigned int verbose)
3523 +{
3524 +       int err, i, j, ndentry;
3525 +       aufs_bindex_t btop, bbot;
3526 +       struct au_dcsub_pages dpages;
3527 +       struct au_dpage *dpage;
3528 +       struct dentry *d;
3529 +
3530 +       err = au_dpages_init(&dpages, GFP_NOFS);
3531 +       if (unlikely(err))
3532 +               goto out;
3533 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3534 +       if (unlikely(err))
3535 +               goto out_dpages;
3536 +
3537 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3538 +               dpage = dpages.dpages + i;
3539 +               ndentry = dpage->ndentry;
3540 +               for (j = 0; !err && j < ndentry; j++) {
3541 +                       d = dpage->dentries[j];
3542 +                       AuDebugOn(au_dcount(d) <= 0);
3543 +                       if (!au_digen_test(d, sigen)) {
3544 +                               di_read_lock_child(d, AuLock_IR);
3545 +                               if (unlikely(au_dbrange_test(d))) {
3546 +                                       di_read_unlock(d, AuLock_IR);
3547 +                                       continue;
3548 +                               }
3549 +                       } else {
3550 +                               di_write_lock_child(d);
3551 +                               if (unlikely(au_dbrange_test(d))) {
3552 +                                       di_write_unlock(d);
3553 +                                       continue;
3554 +                               }
3555 +                               err = au_reval_dpath(d, sigen);
3556 +                               if (!err)
3557 +                                       di_downgrade_lock(d, AuLock_IR);
3558 +                               else {
3559 +                                       di_write_unlock(d);
3560 +                                       break;
3561 +                               }
3562 +                       }
3563 +
3564 +                       /* AuDbgDentry(d); */
3565 +                       btop = au_dbtop(d);
3566 +                       bbot = au_dbbot(d);
3567 +                       if (btop <= bindex
3568 +                           && bindex <= bbot
3569 +                           && au_h_dptr(d, bindex)
3570 +                           && au_test_dbusy(d, btop, bbot)) {
3571 +                               err = -EBUSY;
3572 +                               AuVerbose(verbose, "busy %pd\n", d);
3573 +                               AuDbgDentry(d);
3574 +                       }
3575 +                       di_read_unlock(d, AuLock_IR);
3576 +               }
3577 +       }
3578 +
3579 +out_dpages:
3580 +       au_dpages_free(&dpages);
3581 +out:
3582 +       return err;
3583 +}
3584 +
3585 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3586 +                          unsigned int sigen, const unsigned int verbose)
3587 +{
3588 +       int err;
3589 +       unsigned long long max, ull;
3590 +       struct inode *i, **array;
3591 +       aufs_bindex_t btop, bbot;
3592 +
3593 +       array = au_iarray_alloc(sb, &max);
3594 +       err = PTR_ERR(array);
3595 +       if (IS_ERR(array))
3596 +               goto out;
3597 +
3598 +       err = 0;
3599 +       AuDbg("b%d\n", bindex);
3600 +       for (ull = 0; !err && ull < max; ull++) {
3601 +               i = array[ull];
3602 +               if (unlikely(!i))
3603 +                       break;
3604 +               if (i->i_ino == AUFS_ROOT_INO)
3605 +                       continue;
3606 +
3607 +               /* AuDbgInode(i); */
3608 +               if (au_iigen(i, NULL) == sigen)
3609 +                       ii_read_lock_child(i);
3610 +               else {
3611 +                       ii_write_lock_child(i);
3612 +                       err = au_refresh_hinode_self(i);
3613 +                       au_iigen_dec(i);
3614 +                       if (!err)
3615 +                               ii_downgrade_lock(i);
3616 +                       else {
3617 +                               ii_write_unlock(i);
3618 +                               break;
3619 +                       }
3620 +               }
3621 +
3622 +               btop = au_ibtop(i);
3623 +               bbot = au_ibbot(i);
3624 +               if (btop <= bindex
3625 +                   && bindex <= bbot
3626 +                   && au_h_iptr(i, bindex)
3627 +                   && au_test_ibusy(i, btop, bbot)) {
3628 +                       err = -EBUSY;
3629 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3630 +                       AuDbgInode(i);
3631 +               }
3632 +               ii_read_unlock(i);
3633 +       }
3634 +       au_iarray_free(array, max);
3635 +
3636 +out:
3637 +       return err;
3638 +}
3639 +
3640 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3641 +                             const unsigned int verbose)
3642 +{
3643 +       int err;
3644 +       unsigned int sigen;
3645 +
3646 +       sigen = au_sigen(root->d_sb);
3647 +       DiMustNoWaiters(root);
3648 +       IiMustNoWaiters(d_inode(root));
3649 +       di_write_unlock(root);
3650 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3651 +       if (!err)
3652 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3653 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3654 +
3655 +       return err;
3656 +}
3657 +
3658 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3659 +                        struct file **to_free, int *idx)
3660 +{
3661 +       int err;
3662 +       unsigned char matched, root;
3663 +       aufs_bindex_t bindex, bbot;
3664 +       struct au_fidir *fidir;
3665 +       struct au_hfile *hfile;
3666 +
3667 +       err = 0;
3668 +       root = IS_ROOT(file->f_path.dentry);
3669 +       if (root) {
3670 +               get_file(file);
3671 +               to_free[*idx] = file;
3672 +               (*idx)++;
3673 +               goto out;
3674 +       }
3675 +
3676 +       matched = 0;
3677 +       fidir = au_fi(file)->fi_hdir;
3678 +       AuDebugOn(!fidir);
3679 +       bbot = au_fbbot_dir(file);
3680 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3681 +               hfile = fidir->fd_hfile + bindex;
3682 +               if (!hfile->hf_file)
3683 +                       continue;
3684 +
3685 +               if (hfile->hf_br->br_id == br_id) {
3686 +                       matched = 1;
3687 +                       break;
3688 +               }
3689 +       }
3690 +       if (matched)
3691 +               err = -EBUSY;
3692 +
3693 +out:
3694 +       return err;
3695 +}
3696 +
3697 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3698 +                         struct file **to_free, int opened)
3699 +{
3700 +       int err, idx;
3701 +       unsigned long long ull, max;
3702 +       aufs_bindex_t btop;
3703 +       struct file *file, **array;
3704 +       struct dentry *root;
3705 +       struct au_hfile *hfile;
3706 +
3707 +       array = au_farray_alloc(sb, &max);
3708 +       err = PTR_ERR(array);
3709 +       if (IS_ERR(array))
3710 +               goto out;
3711 +
3712 +       err = 0;
3713 +       idx = 0;
3714 +       root = sb->s_root;
3715 +       di_write_unlock(root);
3716 +       for (ull = 0; ull < max; ull++) {
3717 +               file = array[ull];
3718 +               if (unlikely(!file))
3719 +                       break;
3720 +
3721 +               /* AuDbg("%pD\n", file); */
3722 +               fi_read_lock(file);
3723 +               btop = au_fbtop(file);
3724 +               if (!d_is_dir(file->f_path.dentry)) {
3725 +                       hfile = &au_fi(file)->fi_htop;
3726 +                       if (hfile->hf_br->br_id == br_id)
3727 +                               err = -EBUSY;
3728 +               } else
3729 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3730 +               fi_read_unlock(file);
3731 +               if (unlikely(err))
3732 +                       break;
3733 +       }
3734 +       di_write_lock_child(root);
3735 +       au_farray_free(array, max);
3736 +       AuDebugOn(idx > opened);
3737 +
3738 +out:
3739 +       return err;
3740 +}
3741 +
3742 +static void br_del_file(struct file **to_free, unsigned long long opened,
3743 +                         aufs_bindex_t br_id)
3744 +{
3745 +       unsigned long long ull;
3746 +       aufs_bindex_t bindex, btop, bbot, bfound;
3747 +       struct file *file;
3748 +       struct au_fidir *fidir;
3749 +       struct au_hfile *hfile;
3750 +
3751 +       for (ull = 0; ull < opened; ull++) {
3752 +               file = to_free[ull];
3753 +               if (unlikely(!file))
3754 +                       break;
3755 +
3756 +               /* AuDbg("%pD\n", file); */
3757 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3758 +               bfound = -1;
3759 +               fidir = au_fi(file)->fi_hdir;
3760 +               AuDebugOn(!fidir);
3761 +               fi_write_lock(file);
3762 +               btop = au_fbtop(file);
3763 +               bbot = au_fbbot_dir(file);
3764 +               for (bindex = btop; bindex <= bbot; bindex++) {
3765 +                       hfile = fidir->fd_hfile + bindex;
3766 +                       if (!hfile->hf_file)
3767 +                               continue;
3768 +
3769 +                       if (hfile->hf_br->br_id == br_id) {
3770 +                               bfound = bindex;
3771 +                               break;
3772 +                       }
3773 +               }
3774 +               AuDebugOn(bfound < 0);
3775 +               au_set_h_fptr(file, bfound, NULL);
3776 +               if (bfound == btop) {
3777 +                       for (btop++; btop <= bbot; btop++)
3778 +                               if (au_hf_dir(file, btop)) {
3779 +                                       au_set_fbtop(file, btop);
3780 +                                       break;
3781 +                               }
3782 +               }
3783 +               fi_write_unlock(file);
3784 +       }
3785 +}
3786 +
3787 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3788 +                            const aufs_bindex_t bindex,
3789 +                            const aufs_bindex_t bbot)
3790 +{
3791 +       struct au_branch **brp, **p;
3792 +
3793 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3794 +
3795 +       brp = sbinfo->si_branch + bindex;
3796 +       if (bindex < bbot)
3797 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3798 +       sbinfo->si_branch[0 + bbot] = NULL;
3799 +       sbinfo->si_bbot--;
3800 +
3801 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3802 +                       /*may_shrink*/1);
3803 +       if (p)
3804 +               sbinfo->si_branch = p;
3805 +       /* harmless error */
3806 +}
3807 +
3808 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3809 +                            const aufs_bindex_t bbot)
3810 +{
3811 +       struct au_hdentry *hdp, *p;
3812 +
3813 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3814 +
3815 +       hdp = au_hdentry(dinfo, bindex);
3816 +       if (bindex < bbot)
3817 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3818 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3819 +       dinfo->di_bbot--;
3820 +
3821 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3822 +                       /*may_shrink*/1);
3823 +       if (p)
3824 +               dinfo->di_hdentry = p;
3825 +       /* harmless error */
3826 +}
3827 +
3828 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3829 +                            const aufs_bindex_t bbot)
3830 +{
3831 +       struct au_hinode *hip, *p;
3832 +
3833 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3834 +
3835 +       hip = au_hinode(iinfo, bindex);
3836 +       if (bindex < bbot)
3837 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3838 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3839 +       iinfo->ii_bbot--;
3840 +
3841 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3842 +                       /*may_shrink*/1);
3843 +       if (p)
3844 +               iinfo->ii_hinode = p;
3845 +       /* harmless error */
3846 +}
3847 +
3848 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3849 +                        struct au_branch *br)
3850 +{
3851 +       aufs_bindex_t bbot;
3852 +       struct au_sbinfo *sbinfo;
3853 +       struct dentry *root, *h_root;
3854 +       struct inode *inode, *h_inode;
3855 +       struct au_hinode *hinode;
3856 +
3857 +       SiMustWriteLock(sb);
3858 +
3859 +       root = sb->s_root;
3860 +       inode = d_inode(root);
3861 +       sbinfo = au_sbi(sb);
3862 +       bbot = sbinfo->si_bbot;
3863 +
3864 +       h_root = au_h_dptr(root, bindex);
3865 +       hinode = au_hi(inode, bindex);
3866 +       h_inode = au_igrab(hinode->hi_inode);
3867 +       au_hiput(hinode);
3868 +
3869 +       au_sbilist_lock();
3870 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3871 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3872 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3873 +       au_sbilist_unlock();
3874 +
3875 +       dput(h_root);
3876 +       iput(h_inode);
3877 +       au_br_do_free(br);
3878 +}
3879 +
3880 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3881 +                                  unsigned long long max, void *arg)
3882 +{
3883 +       return max;
3884 +}
3885 +
3886 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3887 +{
3888 +       int err, rerr, i;
3889 +       unsigned long long opened;
3890 +       unsigned int mnt_flags;
3891 +       aufs_bindex_t bindex, bbot, br_id;
3892 +       unsigned char do_wh, verbose;
3893 +       struct au_branch *br;
3894 +       struct au_wbr *wbr;
3895 +       struct dentry *root;
3896 +       struct file **to_free;
3897 +
3898 +       err = 0;
3899 +       opened = 0;
3900 +       to_free = NULL;
3901 +       root = sb->s_root;
3902 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3903 +       if (bindex < 0) {
3904 +               if (remount)
3905 +                       goto out; /* success */
3906 +               err = -ENOENT;
3907 +               pr_err("%s no such branch\n", del->pathname);
3908 +               goto out;
3909 +       }
3910 +       AuDbg("bindex b%d\n", bindex);
3911 +
3912 +       err = -EBUSY;
3913 +       mnt_flags = au_mntflags(sb);
3914 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3915 +       bbot = au_sbbot(sb);
3916 +       if (unlikely(!bbot)) {
3917 +               AuVerbose(verbose, "no more branches left\n");
3918 +               goto out;
3919 +       }
3920 +       br = au_sbr(sb, bindex);
3921 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3922 +
3923 +       br_id = br->br_id;
3924 +       opened = au_br_count(br);
3925 +       if (unlikely(opened)) {
3926 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3927 +               err = PTR_ERR(to_free);
3928 +               if (IS_ERR(to_free))
3929 +                       goto out;
3930 +
3931 +               err = test_file_busy(sb, br_id, to_free, opened);
3932 +               if (unlikely(err)) {
3933 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3934 +                       goto out;
3935 +               }
3936 +       }
3937 +
3938 +       wbr = br->br_wbr;
3939 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3940 +       if (do_wh) {
3941 +               /* instead of WbrWhMustWriteLock(wbr) */
3942 +               SiMustWriteLock(sb);
3943 +               for (i = 0; i < AuBrWh_Last; i++) {
3944 +                       dput(wbr->wbr_wh[i]);
3945 +                       wbr->wbr_wh[i] = NULL;
3946 +               }
3947 +       }
3948 +
3949 +       err = test_children_busy(root, bindex, verbose);
3950 +       if (unlikely(err)) {
3951 +               if (do_wh)
3952 +                       goto out_wh;
3953 +               goto out;
3954 +       }
3955 +
3956 +       err = 0;
3957 +       if (to_free) {
3958 +               /*
3959 +                * now we confirmed the branch is deletable.
3960 +                * let's free the remaining opened dirs on the branch.
3961 +                */
3962 +               di_write_unlock(root);
3963 +               br_del_file(to_free, opened, br_id);
3964 +               di_write_lock_child(root);
3965 +       }
3966 +
3967 +       if (!remount)
3968 +               au_br_do_del(sb, bindex, br);
3969 +       else {
3970 +               sysaufs_brs_del(sb, bindex);
3971 +               au_br_do_del(sb, bindex, br);
3972 +               sysaufs_brs_add(sb, bindex);
3973 +       }
3974 +
3975 +       if (!bindex) {
3976 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3977 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3978 +       } else
3979 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3980 +       if (au_opt_test(mnt_flags, PLINK))
3981 +               au_plink_half_refresh(sb, br_id);
3982 +
3983 +       if (au_xino_brid(sb) == br_id)
3984 +               au_xino_brid_set(sb, -1);
3985 +       goto out; /* success */
3986 +
3987 +out_wh:
3988 +       /* revert */
3989 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3990 +       if (rerr)
3991 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3992 +                       del->pathname, rerr);
3993 +out:
3994 +       if (to_free)
3995 +               au_farray_free(to_free, opened);
3996 +       return err;
3997 +}
3998 +
3999 +/* ---------------------------------------------------------------------- */
4000 +
4001 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4002 +{
4003 +       int err;
4004 +       aufs_bindex_t btop, bbot;
4005 +       struct aufs_ibusy ibusy;
4006 +       struct inode *inode, *h_inode;
4007 +
4008 +       err = -EPERM;
4009 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4010 +               goto out;
4011 +
4012 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4013 +       if (!err)
4014 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4015 +       if (unlikely(err)) {
4016 +               err = -EFAULT;
4017 +               AuTraceErr(err);
4018 +               goto out;
4019 +       }
4020 +
4021 +       err = -EINVAL;
4022 +       si_read_lock(sb, AuLock_FLUSH);
4023 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4024 +               goto out_unlock;
4025 +
4026 +       err = 0;
4027 +       ibusy.h_ino = 0; /* invalid */
4028 +       inode = ilookup(sb, ibusy.ino);
4029 +       if (!inode
4030 +           || inode->i_ino == AUFS_ROOT_INO
4031 +           || au_is_bad_inode(inode))
4032 +               goto out_unlock;
4033 +
4034 +       ii_read_lock_child(inode);
4035 +       btop = au_ibtop(inode);
4036 +       bbot = au_ibbot(inode);
4037 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4038 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4039 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4040 +                       ibusy.h_ino = h_inode->i_ino;
4041 +       }
4042 +       ii_read_unlock(inode);
4043 +       iput(inode);
4044 +
4045 +out_unlock:
4046 +       si_read_unlock(sb);
4047 +       if (!err) {
4048 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4049 +               if (unlikely(err)) {
4050 +                       err = -EFAULT;
4051 +                       AuTraceErr(err);
4052 +               }
4053 +       }
4054 +out:
4055 +       return err;
4056 +}
4057 +
4058 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4059 +{
4060 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4061 +}
4062 +
4063 +#ifdef CONFIG_COMPAT
4064 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4065 +{
4066 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4067 +}
4068 +#endif
4069 +
4070 +/* ---------------------------------------------------------------------- */
4071 +
4072 +/*
4073 + * change a branch permission
4074 + */
4075 +
4076 +static void au_warn_ima(void)
4077 +{
4078 +#ifdef CONFIG_IMA
4079 +       /* since it doesn't support mark_files_ro() */
4080 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4081 +#endif
4082 +}
4083 +
4084 +static int do_need_sigen_inc(int a, int b)
4085 +{
4086 +       return au_br_whable(a) && !au_br_whable(b);
4087 +}
4088 +
4089 +static int need_sigen_inc(int old, int new)
4090 +{
4091 +       return do_need_sigen_inc(old, new)
4092 +               || do_need_sigen_inc(new, old);
4093 +}
4094 +
4095 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4096 +{
4097 +       int err, do_warn;
4098 +       unsigned int mnt_flags;
4099 +       unsigned long long ull, max;
4100 +       aufs_bindex_t br_id;
4101 +       unsigned char verbose, writer;
4102 +       struct file *file, *hf, **array;
4103 +       struct au_hfile *hfile;
4104 +
4105 +       mnt_flags = au_mntflags(sb);
4106 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4107 +
4108 +       array = au_farray_alloc(sb, &max);
4109 +       err = PTR_ERR(array);
4110 +       if (IS_ERR(array))
4111 +               goto out;
4112 +
4113 +       do_warn = 0;
4114 +       br_id = au_sbr_id(sb, bindex);
4115 +       for (ull = 0; ull < max; ull++) {
4116 +               file = array[ull];
4117 +               if (unlikely(!file))
4118 +                       break;
4119 +
4120 +               /* AuDbg("%pD\n", file); */
4121 +               fi_read_lock(file);
4122 +               if (unlikely(au_test_mmapped(file))) {
4123 +                       err = -EBUSY;
4124 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4125 +                       AuDbgFile(file);
4126 +                       FiMustNoWaiters(file);
4127 +                       fi_read_unlock(file);
4128 +                       goto out_array;
4129 +               }
4130 +
4131 +               hfile = &au_fi(file)->fi_htop;
4132 +               hf = hfile->hf_file;
4133 +               if (!d_is_reg(file->f_path.dentry)
4134 +                   || !(file->f_mode & FMODE_WRITE)
4135 +                   || hfile->hf_br->br_id != br_id
4136 +                   || !(hf->f_mode & FMODE_WRITE))
4137 +                       array[ull] = NULL;
4138 +               else {
4139 +                       do_warn = 1;
4140 +                       get_file(file);
4141 +               }
4142 +
4143 +               FiMustNoWaiters(file);
4144 +               fi_read_unlock(file);
4145 +               fput(file);
4146 +       }
4147 +
4148 +       err = 0;
4149 +       if (do_warn)
4150 +               au_warn_ima();
4151 +
4152 +       for (ull = 0; ull < max; ull++) {
4153 +               file = array[ull];
4154 +               if (!file)
4155 +                       continue;
4156 +
4157 +               /* todo: already flushed? */
4158 +               /*
4159 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4160 +                * approach which resets f_mode and calls mnt_drop_write() and
4161 +                * file_release_write() for each file, because the branch
4162 +                * attribute in aufs world is totally different from the native
4163 +                * fs rw/ro mode.
4164 +               */
4165 +               /* fi_read_lock(file); */
4166 +               hfile = &au_fi(file)->fi_htop;
4167 +               hf = hfile->hf_file;
4168 +               /* fi_read_unlock(file); */
4169 +               spin_lock(&hf->f_lock);
4170 +               writer = !!(hf->f_mode & FMODE_WRITER);
4171 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4172 +               spin_unlock(&hf->f_lock);
4173 +               if (writer) {
4174 +                       put_write_access(file_inode(hf));
4175 +                       __mnt_drop_write(hf->f_path.mnt);
4176 +               }
4177 +       }
4178 +
4179 +out_array:
4180 +       au_farray_free(array, max);
4181 +out:
4182 +       AuTraceErr(err);
4183 +       return err;
4184 +}
4185 +
4186 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4187 +             int *do_refresh)
4188 +{
4189 +       int err, rerr;
4190 +       aufs_bindex_t bindex;
4191 +       struct dentry *root;
4192 +       struct au_branch *br;
4193 +       struct au_br_fhsm *bf;
4194 +
4195 +       root = sb->s_root;
4196 +       bindex = au_find_dbindex(root, mod->h_root);
4197 +       if (bindex < 0) {
4198 +               if (remount)
4199 +                       return 0; /* success */
4200 +               err = -ENOENT;
4201 +               pr_err("%s no such branch\n", mod->path);
4202 +               goto out;
4203 +       }
4204 +       AuDbg("bindex b%d\n", bindex);
4205 +
4206 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4207 +       if (unlikely(err))
4208 +               goto out;
4209 +
4210 +       br = au_sbr(sb, bindex);
4211 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4212 +       if (br->br_perm == mod->perm)
4213 +               return 0; /* success */
4214 +
4215 +       /* pre-allocate for non-fhsm --> fhsm */
4216 +       bf = NULL;
4217 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4218 +               err = au_fhsm_br_alloc(br);
4219 +               if (unlikely(err))
4220 +                       goto out;
4221 +               bf = br->br_fhsm;
4222 +               br->br_fhsm = NULL;
4223 +       }
4224 +
4225 +       if (au_br_writable(br->br_perm)) {
4226 +               /* remove whiteout base */
4227 +               err = au_br_init_wh(sb, br, mod->perm);
4228 +               if (unlikely(err))
4229 +                       goto out_bf;
4230 +
4231 +               if (!au_br_writable(mod->perm)) {
4232 +                       /* rw --> ro, file might be mmapped */
4233 +                       DiMustNoWaiters(root);
4234 +                       IiMustNoWaiters(d_inode(root));
4235 +                       di_write_unlock(root);
4236 +                       err = au_br_mod_files_ro(sb, bindex);
4237 +                       /* aufs_write_lock() calls ..._child() */
4238 +                       di_write_lock_child(root);
4239 +
4240 +                       if (unlikely(err)) {
4241 +                               rerr = -ENOMEM;
4242 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4243 +                                                    GFP_NOFS);
4244 +                               if (br->br_wbr)
4245 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4246 +                               if (unlikely(rerr)) {
4247 +                                       AuIOErr("nested error %d (%d)\n",
4248 +                                               rerr, err);
4249 +                                       br->br_perm = mod->perm;
4250 +                               }
4251 +                       }
4252 +               }
4253 +       } else if (au_br_writable(mod->perm)) {
4254 +               /* ro --> rw */
4255 +               err = -ENOMEM;
4256 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4257 +               if (br->br_wbr) {
4258 +                       err = au_wbr_init(br, sb, mod->perm);
4259 +                       if (unlikely(err)) {
4260 +                               kfree(br->br_wbr);
4261 +                               br->br_wbr = NULL;
4262 +                       }
4263 +               }
4264 +       }
4265 +       if (unlikely(err))
4266 +               goto out_bf;
4267 +
4268 +       if (au_br_fhsm(br->br_perm)) {
4269 +               if (!au_br_fhsm(mod->perm)) {
4270 +                       /* fhsm --> non-fhsm */
4271 +                       au_br_fhsm_fin(br->br_fhsm);
4272 +                       kfree(br->br_fhsm);
4273 +                       br->br_fhsm = NULL;
4274 +               }
4275 +       } else if (au_br_fhsm(mod->perm))
4276 +               /* non-fhsm --> fhsm */
4277 +               br->br_fhsm = bf;
4278 +
4279 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4280 +       br->br_perm = mod->perm;
4281 +       goto out; /* success */
4282 +
4283 +out_bf:
4284 +       if (bf)
4285 +               kfree(bf);
4286 +out:
4287 +       AuTraceErr(err);
4288 +       return err;
4289 +}
4290 +
4291 +/* ---------------------------------------------------------------------- */
4292 +
4293 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4294 +{
4295 +       int err;
4296 +       struct kstatfs kstfs;
4297 +
4298 +       err = vfs_statfs(&br->br_path, &kstfs);
4299 +       if (!err) {
4300 +               stfs->f_blocks = kstfs.f_blocks;
4301 +               stfs->f_bavail = kstfs.f_bavail;
4302 +               stfs->f_files = kstfs.f_files;
4303 +               stfs->f_ffree = kstfs.f_ffree;
4304 +       }
4305 +
4306 +       return err;
4307 +}
4308 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4309 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4310 +++ linux/fs/aufs/branch.h      2017-07-29 12:14:25.896375188 +0200
4311 @@ -0,0 +1,321 @@
4312 +/*
4313 + * Copyright (C) 2005-2017 Junjiro R. Okajima
4314 + *
4315 + * This program, aufs is free software; you can redistribute it and/or modify
4316 + * it under the terms of the GNU General Public License as published by
4317 + * the Free Software Foundation; either version 2 of the License, or
4318 + * (at your option) any later version.
4319 + *
4320 + * This program is distributed in the hope that it will be useful,
4321 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4322 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4323 + * GNU General Public License for more details.
4324 + *
4325 + * You should have received a copy of the GNU General Public License
4326 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4327 + */
4328 +
4329 +/*
4330 + * branch filesystems and xino for them
4331 + */
4332 +
4333 +#ifndef __AUFS_BRANCH_H__
4334 +#define __AUFS_BRANCH_H__
4335 +
4336 +#ifdef __KERNEL__
4337 +
4338 +#include <linux/mount.h>
4339 +#include "dynop.h"
4340 +#include "rwsem.h"
4341 +#include "super.h"
4342 +
4343 +/* ---------------------------------------------------------------------- */
4344 +
4345 +/* a xino file */
4346 +struct au_xino_file {
4347 +       struct file             *xi_file;
4348 +       struct {
4349 +               spinlock_t              spin;
4350 +               ino_t                   *array;
4351 +               int                     total;
4352 +               /* reserved for future use */
4353 +               /* unsigned long        *bitmap; */
4354 +               wait_queue_head_t       wqh;
4355 +       } xi_nondir;
4356 +
4357 +       /* todo: make xino files an array to support huge inode number */
4358 +
4359 +#ifdef CONFIG_DEBUG_FS
4360 +       struct dentry            *xi_dbgaufs;
4361 +#endif
4362 +};
4363 +
4364 +/* File-based Hierarchical Storage Management */
4365 +struct au_br_fhsm {
4366 +#ifdef CONFIG_AUFS_FHSM
4367 +       struct mutex            bf_lock;
4368 +       unsigned long           bf_jiffy;
4369 +       struct aufs_stfs        bf_stfs;
4370 +       int                     bf_readable;
4371 +#endif
4372 +};
4373 +
4374 +/* members for writable branch only */
4375 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4376 +struct au_wbr {
4377 +       struct au_rwsem         wbr_wh_rwsem;
4378 +       struct dentry           *wbr_wh[AuBrWh_Last];
4379 +       atomic_t                wbr_wh_running;
4380 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4381 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4382 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4383 +
4384 +       /* mfs mode */
4385 +       unsigned long long      wbr_bytes;
4386 +};
4387 +
4388 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4389 +#define AuBrDynOp (AuDyLast * 4)
4390 +
4391 +#ifdef CONFIG_AUFS_HFSNOTIFY
4392 +/* support for asynchronous destruction */
4393 +struct au_br_hfsnotify {
4394 +       struct fsnotify_group   *hfsn_group;
4395 +};
4396 +#endif
4397 +
4398 +/* sysfs entries */
4399 +struct au_brsysfs {
4400 +       char                    name[16];
4401 +       struct attribute        attr;
4402 +};
4403 +
4404 +enum {
4405 +       AuBrSysfs_BR,
4406 +       AuBrSysfs_BRID,
4407 +       AuBrSysfs_Last
4408 +};
4409 +
4410 +/* protected by superblock rwsem */
4411 +struct au_branch {
4412 +       struct au_xino_file     br_xino;
4413 +
4414 +       aufs_bindex_t           br_id;
4415 +
4416 +       int                     br_perm;
4417 +       struct path             br_path;
4418 +       spinlock_t              br_dykey_lock;
4419 +       struct au_dykey         *br_dykey[AuBrDynOp];
4420 +       struct percpu_counter   br_count;
4421 +
4422 +       struct au_wbr           *br_wbr;
4423 +       struct au_br_fhsm       *br_fhsm;
4424 +
4425 +       /* xino truncation */
4426 +       atomic_t                br_xino_running;
4427 +
4428 +#ifdef CONFIG_AUFS_HFSNOTIFY
4429 +       struct au_br_hfsnotify  *br_hfsn;
4430 +#endif
4431 +
4432 +#ifdef CONFIG_SYSFS
4433 +       /* entries under sysfs per mount-point */
4434 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4435 +#endif
4436 +};
4437 +
4438 +/* ---------------------------------------------------------------------- */
4439 +
4440 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4441 +{
4442 +       return br->br_path.mnt;
4443 +}
4444 +
4445 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4446 +{
4447 +       return br->br_path.dentry;
4448 +}
4449 +
4450 +static inline struct super_block *au_br_sb(struct au_branch *br)
4451 +{
4452 +       return au_br_mnt(br)->mnt_sb;
4453 +}
4454 +
4455 +static inline void au_br_get(struct au_branch *br)
4456 +{
4457 +       percpu_counter_inc(&br->br_count);
4458 +}
4459 +
4460 +static inline void au_br_put(struct au_branch *br)
4461 +{
4462 +       percpu_counter_dec(&br->br_count);
4463 +}
4464 +
4465 +static inline s64 au_br_count(struct au_branch *br)
4466 +{
4467 +       return percpu_counter_sum(&br->br_count);
4468 +}
4469 +
4470 +static inline void au_br_count_init(struct au_branch *br)
4471 +{
4472 +       percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4473 +}
4474 +
4475 +static inline void au_br_count_fin(struct au_branch *br)
4476 +{
4477 +       percpu_counter_destroy(&br->br_count);
4478 +}
4479 +
4480 +static inline int au_br_rdonly(struct au_branch *br)
4481 +{
4482 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4483 +               || !au_br_writable(br->br_perm))
4484 +               ? -EROFS : 0;
4485 +}
4486 +
4487 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4488 +{
4489 +#ifdef CONFIG_AUFS_HNOTIFY
4490 +       return !(brperm & AuBrPerm_RR);
4491 +#else
4492 +       return 0;
4493 +#endif
4494 +}
4495 +
4496 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4497 +{
4498 +       int err, exec_flag;
4499 +
4500 +       err = 0;
4501 +       exec_flag = oflag & __FMODE_EXEC;
4502 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4503 +               err = -EACCES;
4504 +
4505 +       return err;
4506 +}
4507 +
4508 +/* ---------------------------------------------------------------------- */
4509 +
4510 +/* branch.c */
4511 +struct au_sbinfo;
4512 +void au_br_free(struct au_sbinfo *sinfo);
4513 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4514 +struct au_opt_add;
4515 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4516 +struct au_opt_del;
4517 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4518 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4519 +#ifdef CONFIG_COMPAT
4520 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4521 +#endif
4522 +struct au_opt_mod;
4523 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4524 +             int *do_refresh);
4525 +struct aufs_stfs;
4526 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4527 +
4528 +/* xino.c */
4529 +static const loff_t au_loff_max = LLONG_MAX;
4530 +
4531 +int au_xib_trunc(struct super_block *sb);
4532 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4533 +                  loff_t *pos);
4534 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4535 +                   size_t size, loff_t *pos);
4536 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4537 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4538 +ino_t au_xino_new_ino(struct super_block *sb);
4539 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4540 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4541 +                 ino_t ino);
4542 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4543 +                ino_t *ino);
4544 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4545 +              struct file *base_file, int do_test);
4546 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4547 +
4548 +struct au_opt_xino;
4549 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4550 +void au_xino_clr(struct super_block *sb);
4551 +struct file *au_xino_def(struct super_block *sb);
4552 +int au_xino_path(struct seq_file *seq, struct file *file);
4553 +
4554 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4555 +                      ino_t h_ino, int idx);
4556 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4557 +                     int *idx);
4558 +
4559 +/* ---------------------------------------------------------------------- */
4560 +
4561 +/* Superblock to branch */
4562 +static inline
4563 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4564 +{
4565 +       return au_sbr(sb, bindex)->br_id;
4566 +}
4567 +
4568 +static inline
4569 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4570 +{
4571 +       return au_br_mnt(au_sbr(sb, bindex));
4572 +}
4573 +
4574 +static inline
4575 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4576 +{
4577 +       return au_br_sb(au_sbr(sb, bindex));
4578 +}
4579 +
4580 +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4581 +{
4582 +       au_br_get(au_sbr(sb, bindex));
4583 +}
4584 +
4585 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4586 +{
4587 +       au_br_put(au_sbr(sb, bindex));
4588 +}
4589 +
4590 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4591 +{
4592 +       return au_sbr(sb, bindex)->br_perm;
4593 +}
4594 +
4595 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4596 +{
4597 +       return au_br_whable(au_sbr_perm(sb, bindex));
4598 +}
4599 +
4600 +/* ---------------------------------------------------------------------- */
4601 +
4602 +/*
4603 + * wbr_wh_read_lock, wbr_wh_write_lock
4604 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4605 + */
4606 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4607 +
4608 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4609 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4610 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4611 +
4612 +/* ---------------------------------------------------------------------- */
4613 +
4614 +#ifdef CONFIG_AUFS_FHSM
4615 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4616 +{
4617 +       mutex_init(&brfhsm->bf_lock);
4618 +       brfhsm->bf_jiffy = 0;
4619 +       brfhsm->bf_readable = 0;
4620 +}
4621 +
4622 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4623 +{
4624 +       mutex_destroy(&brfhsm->bf_lock);
4625 +}
4626 +#else
4627 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4628 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4629 +#endif
4630 +
4631 +#endif /* __KERNEL__ */
4632 +#endif /* __AUFS_BRANCH_H__ */
4633 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4634 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4635 +++ linux/fs/aufs/conf.mk       2017-07-29 12:14:25.899708630 +0200
4636 @@ -0,0 +1,38 @@
4637 +
4638 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4639 +
4640 +define AuConf
4641 +ifdef ${1}
4642 +AuConfStr += ${1}=${${1}}
4643 +endif
4644 +endef
4645 +
4646 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4647 +       SBILIST \
4648 +       HNOTIFY HFSNOTIFY \
4649 +       EXPORT INO_T_64 \
4650 +       XATTR \
4651 +       FHSM \
4652 +       RDU \
4653 +       SHWH \
4654 +       BR_RAMFS \
4655 +       BR_FUSE POLL \
4656 +       BR_HFSPLUS \
4657 +       BDEV_LOOP \
4658 +       DEBUG MAGIC_SYSRQ
4659 +$(foreach i, ${AuConfAll}, \
4660 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4661 +
4662 +AuConfName = ${obj}/conf.str
4663 +${AuConfName}.tmp: FORCE
4664 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4665 +${AuConfName}: ${AuConfName}.tmp
4666 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4667 +       echo '  GEN    ' $@; \
4668 +       cp -p $< $@; \
4669 +       }
4670 +FORCE:
4671 +clean-files += ${AuConfName} ${AuConfName}.tmp
4672 +${obj}/sysfs.o: ${AuConfName}
4673 +
4674 +-include ${srctree}/${src}/conf_priv.mk
4675 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4676 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4677 +++ linux/fs/aufs/cpup.c        2017-07-29 12:14:25.899708630 +0200
4678 @@ -0,0 +1,1442 @@
4679 +/*
4680 + * Copyright (C) 2005-2017 Junjiro R. Okajima
4681 + *
4682 + * This program, aufs is free software; you can redistribute it and/or modify
4683 + * it under the terms of the GNU General Public License as published by
4684 + * the Free Software Foundation; either version 2 of the License, or
4685 + * (at your option) any later version.
4686 + *
4687 + * This program is distributed in the hope that it will be useful,
4688 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4689 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4690 + * GNU General Public License for more details.
4691 + *
4692 + * You should have received a copy of the GNU General Public License
4693 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4694 + */
4695 +
4696 +/*
4697 + * copy-up functions, see wbr_policy.c for copy-down
4698 + */
4699 +
4700 +#include <linux/fs_stack.h>
4701 +#include <linux/mm.h>
4702 +#include <linux/task_work.h>
4703 +#include "aufs.h"
4704 +
4705 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4706 +{
4707 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4708 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4709 +
4710 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4711 +
4712 +       dst->i_flags |= iflags & ~mask;
4713 +       if (au_test_fs_notime(dst->i_sb))
4714 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4715 +}
4716 +
4717 +void au_cpup_attr_timesizes(struct inode *inode)
4718 +{
4719 +       struct inode *h_inode;
4720 +
4721 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4722 +       fsstack_copy_attr_times(inode, h_inode);
4723 +       fsstack_copy_inode_size(inode, h_inode);
4724 +}
4725 +
4726 +void au_cpup_attr_nlink(struct inode *inode, int force)
4727 +{
4728 +       struct inode *h_inode;
4729 +       struct super_block *sb;
4730 +       aufs_bindex_t bindex, bbot;
4731 +
4732 +       sb = inode->i_sb;
4733 +       bindex = au_ibtop(inode);
4734 +       h_inode = au_h_iptr(inode, bindex);
4735 +       if (!force
4736 +           && !S_ISDIR(h_inode->i_mode)
4737 +           && au_opt_test(au_mntflags(sb), PLINK)
4738 +           && au_plink_test(inode))
4739 +               return;
4740 +
4741 +       /*
4742 +        * 0 can happen in revalidating.
4743 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4744 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4745 +        * case.
4746 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4747 +        *       the incorrect link count.
4748 +        */
4749 +       set_nlink(inode, h_inode->i_nlink);
4750 +
4751 +       /*
4752 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4753 +        * it may includes whplink directory.
4754 +        */
4755 +       if (S_ISDIR(h_inode->i_mode)) {
4756 +               bbot = au_ibbot(inode);
4757 +               for (bindex++; bindex <= bbot; bindex++) {
4758 +                       h_inode = au_h_iptr(inode, bindex);
4759 +                       if (h_inode)
4760 +                               au_add_nlink(inode, h_inode);
4761 +               }
4762 +       }
4763 +}
4764 +
4765 +void au_cpup_attr_changeable(struct inode *inode)
4766 +{
4767 +       struct inode *h_inode;
4768 +
4769 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4770 +       inode->i_mode = h_inode->i_mode;
4771 +       inode->i_uid = h_inode->i_uid;
4772 +       inode->i_gid = h_inode->i_gid;
4773 +       au_cpup_attr_timesizes(inode);
4774 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4775 +}
4776 +
4777 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4778 +{
4779 +       struct au_iinfo *iinfo = au_ii(inode);
4780 +
4781 +       IiMustWriteLock(inode);
4782 +
4783 +       iinfo->ii_higen = h_inode->i_generation;
4784 +       iinfo->ii_hsb1 = h_inode->i_sb;
4785 +}
4786 +
4787 +void au_cpup_attr_all(struct inode *inode, int force)
4788 +{
4789 +       struct inode *h_inode;
4790 +
4791 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4792 +       au_cpup_attr_changeable(inode);
4793 +       if (inode->i_nlink > 0)
4794 +               au_cpup_attr_nlink(inode, force);
4795 +       inode->i_rdev = h_inode->i_rdev;
4796 +       inode->i_blkbits = h_inode->i_blkbits;
4797 +       au_cpup_igen(inode, h_inode);
4798 +}
4799 +
4800 +/* ---------------------------------------------------------------------- */
4801 +
4802 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4803 +
4804 +/* keep the timestamps of the parent dir when cpup */
4805 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4806 +                   struct path *h_path)
4807 +{
4808 +       struct inode *h_inode;
4809 +
4810 +       dt->dt_dentry = dentry;
4811 +       dt->dt_h_path = *h_path;
4812 +       h_inode = d_inode(h_path->dentry);
4813 +       dt->dt_atime = h_inode->i_atime;
4814 +       dt->dt_mtime = h_inode->i_mtime;
4815 +       /* smp_mb(); */
4816 +}
4817 +
4818 +void au_dtime_revert(struct au_dtime *dt)
4819 +{
4820 +       struct iattr attr;
4821 +       int err;
4822 +
4823 +       attr.ia_atime = dt->dt_atime;
4824 +       attr.ia_mtime = dt->dt_mtime;
4825 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4826 +               | ATTR_ATIME | ATTR_ATIME_SET;
4827 +
4828 +       /* no delegation since this is a directory */
4829 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4830 +       if (unlikely(err))
4831 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4832 +}
4833 +
4834 +/* ---------------------------------------------------------------------- */
4835 +
4836 +/* internal use only */
4837 +struct au_cpup_reg_attr {
4838 +       int             valid;
4839 +       struct kstat    st;
4840 +       unsigned int    iflags; /* inode->i_flags */
4841 +};
4842 +
4843 +static noinline_for_stack
4844 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4845 +              struct au_cpup_reg_attr *h_src_attr)
4846 +{
4847 +       int err, sbits, icex;
4848 +       unsigned int mnt_flags;
4849 +       unsigned char verbose;
4850 +       struct iattr ia;
4851 +       struct path h_path;
4852 +       struct inode *h_isrc, *h_idst;
4853 +       struct kstat *h_st;
4854 +       struct au_branch *br;
4855 +
4856 +       h_path.dentry = au_h_dptr(dst, bindex);
4857 +       h_idst = d_inode(h_path.dentry);
4858 +       br = au_sbr(dst->d_sb, bindex);
4859 +       h_path.mnt = au_br_mnt(br);
4860 +       h_isrc = d_inode(h_src);
4861 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4862 +               | ATTR_ATIME | ATTR_MTIME
4863 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4864 +       if (h_src_attr && h_src_attr->valid) {
4865 +               h_st = &h_src_attr->st;
4866 +               ia.ia_uid = h_st->uid;
4867 +               ia.ia_gid = h_st->gid;
4868 +               ia.ia_atime = h_st->atime;
4869 +               ia.ia_mtime = h_st->mtime;
4870 +               if (h_idst->i_mode != h_st->mode
4871 +                   && !S_ISLNK(h_idst->i_mode)) {
4872 +                       ia.ia_valid |= ATTR_MODE;
4873 +                       ia.ia_mode = h_st->mode;
4874 +               }
4875 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4876 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4877 +       } else {
4878 +               ia.ia_uid = h_isrc->i_uid;
4879 +               ia.ia_gid = h_isrc->i_gid;
4880 +               ia.ia_atime = h_isrc->i_atime;
4881 +               ia.ia_mtime = h_isrc->i_mtime;
4882 +               if (h_idst->i_mode != h_isrc->i_mode
4883 +                   && !S_ISLNK(h_idst->i_mode)) {
4884 +                       ia.ia_valid |= ATTR_MODE;
4885 +                       ia.ia_mode = h_isrc->i_mode;
4886 +               }
4887 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4888 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4889 +       }
4890 +       /* no delegation since it is just created */
4891 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4892 +
4893 +       /* is this nfs only? */
4894 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4895 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4896 +               ia.ia_mode = h_isrc->i_mode;
4897 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4898 +       }
4899 +
4900 +       icex = br->br_perm & AuBrAttr_ICEX;
4901 +       if (!err) {
4902 +               mnt_flags = au_mntflags(dst->d_sb);
4903 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4904 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4905 +       }
4906 +
4907 +       return err;
4908 +}
4909 +
4910 +/* ---------------------------------------------------------------------- */
4911 +
4912 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4913 +                          char *buf, unsigned long blksize)
4914 +{
4915 +       int err;
4916 +       size_t sz, rbytes, wbytes;
4917 +       unsigned char all_zero;
4918 +       char *p, *zp;
4919 +       struct inode *h_inode;
4920 +       /* reduce stack usage */
4921 +       struct iattr *ia;
4922 +
4923 +       zp = page_address(ZERO_PAGE(0));
4924 +       if (unlikely(!zp))
4925 +               return -ENOMEM; /* possible? */
4926 +
4927 +       err = 0;
4928 +       all_zero = 0;
4929 +       while (len) {
4930 +               AuDbg("len %lld\n", len);
4931 +               sz = blksize;
4932 +               if (len < blksize)
4933 +                       sz = len;
4934 +
4935 +               rbytes = 0;
4936 +               /* todo: signal_pending? */
4937 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4938 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4939 +                       err = rbytes;
4940 +               }
4941 +               if (unlikely(err < 0))
4942 +                       break;
4943 +
4944 +               all_zero = 0;
4945 +               if (len >= rbytes && rbytes == blksize)
4946 +                       all_zero = !memcmp(buf, zp, rbytes);
4947 +               if (!all_zero) {
4948 +                       wbytes = rbytes;
4949 +                       p = buf;
4950 +                       while (wbytes) {
4951 +                               size_t b;
4952 +
4953 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4954 +                               err = b;
4955 +                               /* todo: signal_pending? */
4956 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4957 +                                       continue;
4958 +                               if (unlikely(err < 0))
4959 +                                       break;
4960 +                               wbytes -= b;
4961 +                               p += b;
4962 +                       }
4963 +                       if (unlikely(err < 0))
4964 +                               break;
4965 +               } else {
4966 +                       loff_t res;
4967 +
4968 +                       AuLabel(hole);
4969 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4970 +                       err = res;
4971 +                       if (unlikely(res < 0))
4972 +                               break;
4973 +               }
4974 +               len -= rbytes;
4975 +               err = 0;
4976 +       }
4977 +
4978 +       /* the last block may be a hole */
4979 +       if (!err && all_zero) {
4980 +               AuLabel(last hole);
4981 +
4982 +               err = 1;
4983 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4984 +                       /* nfs requires this step to make last hole */
4985 +                       /* is this only nfs? */
4986 +                       do {
4987 +                               /* todo: signal_pending? */
4988 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4989 +                       } while (err == -EAGAIN || err == -EINTR);
4990 +                       if (err == 1)
4991 +                               dst->f_pos--;
4992 +               }
4993 +
4994 +               if (err == 1) {
4995 +                       ia = (void *)buf;
4996 +                       ia->ia_size = dst->f_pos;
4997 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4998 +                       ia->ia_file = dst;
4999 +                       h_inode = file_inode(dst);
5000 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5001 +                       /* no delegation since it is just created */
5002 +                       err = vfsub_notify_change(&dst->f_path, ia,
5003 +                                                 /*delegated*/NULL);
5004 +                       inode_unlock(h_inode);
5005 +               }
5006 +       }
5007 +
5008 +       return err;
5009 +}
5010 +
5011 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5012 +{
5013 +       int err;
5014 +       unsigned long blksize;
5015 +       unsigned char do_kfree;
5016 +       char *buf;
5017 +
5018 +       err = -ENOMEM;
5019 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
5020 +       if (!blksize || PAGE_SIZE < blksize)
5021 +               blksize = PAGE_SIZE;
5022 +       AuDbg("blksize %lu\n", blksize);
5023 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5024 +       if (do_kfree)
5025 +               buf = kmalloc(blksize, GFP_NOFS);
5026 +       else
5027 +               buf = (void *)__get_free_page(GFP_NOFS);
5028 +       if (unlikely(!buf))
5029 +               goto out;
5030 +
5031 +       if (len > (1 << 22))
5032 +               AuDbg("copying a large file %lld\n", (long long)len);
5033 +
5034 +       src->f_pos = 0;
5035 +       dst->f_pos = 0;
5036 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5037 +       if (do_kfree)
5038 +               kfree(buf);
5039 +       else
5040 +               free_page((unsigned long)buf);
5041 +
5042 +out:
5043 +       return err;
5044 +}
5045 +
5046 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5047 +{
5048 +       int err;
5049 +       struct super_block *h_src_sb;
5050 +       struct inode *h_src_inode;
5051 +
5052 +       h_src_inode = file_inode(src);
5053 +       h_src_sb = h_src_inode->i_sb;
5054 +
5055 +       /* XFS acquires inode_lock */
5056 +       if (!au_test_xfs(h_src_sb))
5057 +               err = au_copy_file(dst, src, len);
5058 +       else {
5059 +               inode_unlock(h_src_inode);
5060 +               err = au_copy_file(dst, src, len);
5061 +               inode_lock(h_src_inode);
5062 +       }
5063 +
5064 +       return err;
5065 +}
5066 +
5067 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5068 +{
5069 +       int err;
5070 +       struct super_block *h_src_sb;
5071 +       struct inode *h_src_inode;
5072 +
5073 +       h_src_inode = file_inode(src);
5074 +       h_src_sb = h_src_inode->i_sb;
5075 +       if (h_src_sb != file_inode(dst)->i_sb
5076 +           || !dst->f_op->clone_file_range) {
5077 +               err = au_do_copy(dst, src, len);
5078 +               goto out;
5079 +       }
5080 +
5081 +       if (!au_test_nfs(h_src_sb)) {
5082 +               inode_unlock(h_src_inode);
5083 +               err = vfsub_clone_file_range(src, dst, len);
5084 +               inode_lock(h_src_inode);
5085 +       } else
5086 +               err = vfsub_clone_file_range(src, dst, len);
5087 +       /* older XFS has a condition in cloning */
5088 +       if (unlikely(err != -EOPNOTSUPP))
5089 +               goto out;
5090 +
5091 +       /* the backend fs on NFS may not support cloning */
5092 +       err = au_do_copy(dst, src, len);
5093 +
5094 +out:
5095 +       AuTraceErr(err);
5096 +       return err;
5097 +}
5098 +
5099 +/*
5100 + * to support a sparse file which is opened with O_APPEND,
5101 + * we need to close the file.
5102 + */
5103 +static int au_cp_regular(struct au_cp_generic *cpg)
5104 +{
5105 +       int err, i;
5106 +       enum { SRC, DST };
5107 +       struct {
5108 +               aufs_bindex_t bindex;
5109 +               unsigned int flags;
5110 +               struct dentry *dentry;
5111 +               int force_wr;
5112 +               struct file *file;
5113 +               void *label;
5114 +       } *f, file[] = {
5115 +               {
5116 +                       .bindex = cpg->bsrc,
5117 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5118 +                       .label = &&out
5119 +               },
5120 +               {
5121 +                       .bindex = cpg->bdst,
5122 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5123 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5124 +                       .label = &&out_src
5125 +               }
5126 +       };
5127 +       struct super_block *sb, *h_src_sb;
5128 +       struct inode *h_src_inode;
5129 +       struct task_struct *tsk = current;
5130 +
5131 +       /* bsrc branch can be ro/rw. */
5132 +       sb = cpg->dentry->d_sb;
5133 +       f = file;
5134 +       for (i = 0; i < 2; i++, f++) {
5135 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5136 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5137 +                                   /*file*/NULL, f->force_wr);
5138 +               err = PTR_ERR(f->file);
5139 +               if (IS_ERR(f->file))
5140 +                       goto *f->label;
5141 +       }
5142 +
5143 +       /* try stopping to update while we copyup */
5144 +       h_src_inode = d_inode(file[SRC].dentry);
5145 +       h_src_sb = h_src_inode->i_sb;
5146 +       if (!au_test_nfs(h_src_sb))
5147 +               IMustLock(h_src_inode);
5148 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5149 +
5150 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5151 +       if (tsk->flags & PF_KTHREAD)
5152 +               __fput_sync(file[DST].file);
5153 +       else {
5154 +               WARN(1, "%pD\nPlease report this warning to aufs-users ML",
5155 +                    file[DST].file);
5156 +               fput(file[DST].file);
5157 +               /*
5158 +                * too bad.
5159 +                * we have to call both since we don't know which place the file
5160 +                * was added to.
5161 +                */
5162 +               task_work_run();
5163 +               flush_delayed_fput();
5164 +       }
5165 +       au_sbr_put(sb, file[DST].bindex);
5166 +
5167 +out_src:
5168 +       fput(file[SRC].file);
5169 +       au_sbr_put(sb, file[SRC].bindex);
5170 +out:
5171 +       return err;
5172 +}
5173 +
5174 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5175 +                             struct au_cpup_reg_attr *h_src_attr)
5176 +{
5177 +       int err, rerr;
5178 +       loff_t l;
5179 +       struct path h_path;
5180 +       struct inode *h_src_inode, *h_dst_inode;
5181 +
5182 +       err = 0;
5183 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5184 +       l = i_size_read(h_src_inode);
5185 +       if (cpg->len == -1 || l < cpg->len)
5186 +               cpg->len = l;
5187 +       if (cpg->len) {
5188 +               /* try stopping to update while we are referencing */
5189 +               inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5190 +               au_pin_hdir_unlock(cpg->pin);
5191 +
5192 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5193 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5194 +               h_src_attr->iflags = h_src_inode->i_flags;
5195 +               if (!au_test_nfs(h_src_inode->i_sb))
5196 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5197 +               else {
5198 +                       inode_unlock(h_src_inode);
5199 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5200 +                       inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5201 +               }
5202 +               if (unlikely(err)) {
5203 +                       inode_unlock(h_src_inode);
5204 +                       goto out;
5205 +               }
5206 +               h_src_attr->valid = 1;
5207 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5208 +                       err = au_cp_regular(cpg);
5209 +                       inode_unlock(h_src_inode);
5210 +               } else {
5211 +                       inode_unlock(h_src_inode);
5212 +                       err = au_cp_regular(cpg);
5213 +               }
5214 +               rerr = au_pin_hdir_relock(cpg->pin);
5215 +               if (!err && rerr)
5216 +                       err = rerr;
5217 +       }
5218 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5219 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5220 +               h_dst_inode = d_inode(h_path.dentry);
5221 +               spin_lock(&h_dst_inode->i_lock);
5222 +               h_dst_inode->i_state |= I_LINKABLE;
5223 +               spin_unlock(&h_dst_inode->i_lock);
5224 +       }
5225 +
5226 +out:
5227 +       return err;
5228 +}
5229 +
5230 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5231 +                             struct inode *h_dir)
5232 +{
5233 +       int err, symlen;
5234 +       mm_segment_t old_fs;
5235 +       union {
5236 +               char *k;
5237 +               char __user *u;
5238 +       } sym;
5239 +
5240 +       err = -ENOMEM;
5241 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5242 +       if (unlikely(!sym.k))
5243 +               goto out;
5244 +
5245 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5246 +       old_fs = get_fs();
5247 +       set_fs(KERNEL_DS);
5248 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5249 +       err = symlen;
5250 +       set_fs(old_fs);
5251 +
5252 +       if (symlen > 0) {
5253 +               sym.k[symlen] = 0;
5254 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5255 +       }
5256 +       free_page((unsigned long)sym.k);
5257 +
5258 +out:
5259 +       return err;
5260 +}
5261 +
5262 +/*
5263 + * regardless 'acl' option, reset all ACL.
5264 + * All ACL will be copied up later from the original entry on the lower branch.
5265 + */
5266 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5267 +{
5268 +       int err;
5269 +       struct dentry *h_dentry;
5270 +       struct inode *h_inode;
5271 +
5272 +       h_dentry = h_path->dentry;
5273 +       h_inode = d_inode(h_dentry);
5274 +       /* forget_all_cached_acls(h_inode)); */
5275 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5276 +       AuTraceErr(err);
5277 +       if (err == -EOPNOTSUPP)
5278 +               err = 0;
5279 +       if (!err)
5280 +               err = vfsub_acl_chmod(h_inode, mode);
5281 +
5282 +       AuTraceErr(err);
5283 +       return err;
5284 +}
5285 +
5286 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5287 +                         struct inode *h_dir, struct path *h_path)
5288 +{
5289 +       int err;
5290 +       struct inode *dir, *inode;
5291 +
5292 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5293 +       AuTraceErr(err);
5294 +       if (err == -EOPNOTSUPP)
5295 +               err = 0;
5296 +       if (unlikely(err))
5297 +               goto out;
5298 +
5299 +       /*
5300 +        * strange behaviour from the users view,
5301 +        * particularry setattr case
5302 +        */
5303 +       dir = d_inode(dst_parent);
5304 +       if (au_ibtop(dir) == cpg->bdst)
5305 +               au_cpup_attr_nlink(dir, /*force*/1);
5306 +       inode = d_inode(cpg->dentry);
5307 +       au_cpup_attr_nlink(inode, /*force*/1);
5308 +
5309 +out:
5310 +       return err;
5311 +}
5312 +
5313 +static noinline_for_stack
5314 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5315 +              struct au_cpup_reg_attr *h_src_attr)
5316 +{
5317 +       int err;
5318 +       umode_t mode;
5319 +       unsigned int mnt_flags;
5320 +       unsigned char isdir, isreg, force;
5321 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5322 +       struct au_dtime dt;
5323 +       struct path h_path;
5324 +       struct dentry *h_src, *h_dst, *h_parent;
5325 +       struct inode *h_inode, *h_dir;
5326 +       struct super_block *sb;
5327 +
5328 +       /* bsrc branch can be ro/rw. */
5329 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5330 +       h_inode = d_inode(h_src);
5331 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5332 +
5333 +       /* try stopping to be referenced while we are creating */
5334 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5335 +       if (au_ftest_cpup(cpg->flags, RENAME))
5336 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5337 +                                 AUFS_WH_PFX_LEN));
5338 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5339 +       h_dir = d_inode(h_parent);
5340 +       IMustLock(h_dir);
5341 +       AuDebugOn(h_parent != h_dst->d_parent);
5342 +
5343 +       sb = cpg->dentry->d_sb;
5344 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5345 +       if (do_dt) {
5346 +               h_path.dentry = h_parent;
5347 +               au_dtime_store(&dt, dst_parent, &h_path);
5348 +       }
5349 +       h_path.dentry = h_dst;
5350 +
5351 +       isreg = 0;
5352 +       isdir = 0;
5353 +       mode = h_inode->i_mode;
5354 +       switch (mode & S_IFMT) {
5355 +       case S_IFREG:
5356 +               isreg = 1;
5357 +               err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
5358 +                                  /*want_excl*/true);
5359 +               if (!err)
5360 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5361 +               break;
5362 +       case S_IFDIR:
5363 +               isdir = 1;
5364 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5365 +               if (!err)
5366 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5367 +               break;
5368 +       case S_IFLNK:
5369 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5370 +               break;
5371 +       case S_IFCHR:
5372 +       case S_IFBLK:
5373 +               AuDebugOn(!capable(CAP_MKNOD));
5374 +               /*FALLTHROUGH*/
5375 +       case S_IFIFO:
5376 +       case S_IFSOCK:
5377 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5378 +               break;
5379 +       default:
5380 +               AuIOErr("Unknown inode type 0%o\n", mode);
5381 +               err = -EIO;
5382 +       }
5383 +       if (!err)
5384 +               err = au_reset_acl(h_dir, &h_path, mode);
5385 +
5386 +       mnt_flags = au_mntflags(sb);
5387 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5388 +           && !isdir
5389 +           && au_opt_test(mnt_flags, XINO)
5390 +           && (h_inode->i_nlink == 1
5391 +               || (h_inode->i_state & I_LINKABLE))
5392 +           /* todo: unnecessary? */
5393 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5394 +           && cpg->bdst < cpg->bsrc
5395 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5396 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5397 +               /* ignore this error */
5398 +
5399 +       if (!err) {
5400 +               force = 0;
5401 +               if (isreg) {
5402 +                       force = !!cpg->len;
5403 +                       if (cpg->len == -1)
5404 +                               force = !!i_size_read(h_inode);
5405 +               }
5406 +               au_fhsm_wrote(sb, cpg->bdst, force);
5407 +       }
5408 +
5409 +       if (do_dt)
5410 +               au_dtime_revert(&dt);
5411 +       return err;
5412 +}
5413 +
5414 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5415 +{
5416 +       int err;
5417 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5418 +       struct inode *h_dir;
5419 +       aufs_bindex_t bdst;
5420 +
5421 +       dentry = cpg->dentry;
5422 +       bdst = cpg->bdst;
5423 +       h_dentry = au_h_dptr(dentry, bdst);
5424 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5425 +               dget(h_dentry);
5426 +               au_set_h_dptr(dentry, bdst, NULL);
5427 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5428 +               if (!err)
5429 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5430 +               au_set_h_dptr(dentry, bdst, h_dentry);
5431 +       } else {
5432 +               err = 0;
5433 +               parent = dget_parent(dentry);
5434 +               h_parent = au_h_dptr(parent, bdst);
5435 +               dput(parent);
5436 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5437 +               if (IS_ERR(h_path->dentry))
5438 +                       err = PTR_ERR(h_path->dentry);
5439 +       }
5440 +       if (unlikely(err))
5441 +               goto out;
5442 +
5443 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5444 +       h_dir = d_inode(h_parent);
5445 +       IMustLock(h_dir);
5446 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5447 +       /* no delegation since it is just created */
5448 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5449 +                          /*flags*/0);
5450 +       dput(h_path->dentry);
5451 +
5452 +out:
5453 +       return err;
5454 +}
5455 +
5456 +/*
5457 + * copyup the @dentry from @bsrc to @bdst.
5458 + * the caller must set the both of lower dentries.
5459 + * @len is for truncating when it is -1 copyup the entire file.
5460 + * in link/rename cases, @dst_parent may be different from the real one.
5461 + * basic->bsrc can be larger than basic->bdst.
5462 + * aufs doesn't touch the credential so
5463 + * security_inode_copy_up{,_xattr}() are unnecrssary.
5464 + */
5465 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5466 +{
5467 +       int err, rerr;
5468 +       aufs_bindex_t old_ibtop;
5469 +       unsigned char isdir, plink;
5470 +       struct dentry *h_src, *h_dst, *h_parent;
5471 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5472 +       struct super_block *sb;
5473 +       struct au_branch *br;
5474 +       /* to reuduce stack size */
5475 +       struct {
5476 +               struct au_dtime dt;
5477 +               struct path h_path;
5478 +               struct au_cpup_reg_attr h_src_attr;
5479 +       } *a;
5480 +
5481 +       err = -ENOMEM;
5482 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5483 +       if (unlikely(!a))
5484 +               goto out;
5485 +       a->h_src_attr.valid = 0;
5486 +
5487 +       sb = cpg->dentry->d_sb;
5488 +       br = au_sbr(sb, cpg->bdst);
5489 +       a->h_path.mnt = au_br_mnt(br);
5490 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5491 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5492 +       h_dir = d_inode(h_parent);
5493 +       IMustLock(h_dir);
5494 +
5495 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5496 +       inode = d_inode(cpg->dentry);
5497 +
5498 +       if (!dst_parent)
5499 +               dst_parent = dget_parent(cpg->dentry);
5500 +       else
5501 +               dget(dst_parent);
5502 +
5503 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5504 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5505 +       if (dst_inode) {
5506 +               if (unlikely(!plink)) {
5507 +                       err = -EIO;
5508 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5509 +                               "but plink is disabled\n",
5510 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5511 +                       goto out_parent;
5512 +               }
5513 +
5514 +               if (dst_inode->i_nlink) {
5515 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5516 +
5517 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5518 +                       err = PTR_ERR(h_src);
5519 +                       if (IS_ERR(h_src))
5520 +                               goto out_parent;
5521 +                       if (unlikely(d_is_negative(h_src))) {
5522 +                               err = -EIO;
5523 +                               AuIOErr("i%lu exists on b%d "
5524 +                                       "but not pseudo-linked\n",
5525 +                                       inode->i_ino, cpg->bdst);
5526 +                               dput(h_src);
5527 +                               goto out_parent;
5528 +                       }
5529 +
5530 +                       if (do_dt) {
5531 +                               a->h_path.dentry = h_parent;
5532 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5533 +                       }
5534 +
5535 +                       a->h_path.dentry = h_dst;
5536 +                       delegated = NULL;
5537 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5538 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5539 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5540 +                       if (do_dt)
5541 +                               au_dtime_revert(&a->dt);
5542 +                       if (unlikely(err == -EWOULDBLOCK)) {
5543 +                               pr_warn("cannot retry for NFSv4 delegation"
5544 +                                       " for an internal link\n");
5545 +                               iput(delegated);
5546 +                       }
5547 +                       dput(h_src);
5548 +                       goto out_parent;
5549 +               } else
5550 +                       /* todo: cpup_wh_file? */
5551 +                       /* udba work */
5552 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5553 +       }
5554 +
5555 +       isdir = S_ISDIR(inode->i_mode);
5556 +       old_ibtop = au_ibtop(inode);
5557 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5558 +       if (unlikely(err))
5559 +               goto out_rev;
5560 +       dst_inode = d_inode(h_dst);
5561 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5562 +       /* todo: necessary? */
5563 +       /* au_pin_hdir_unlock(cpg->pin); */
5564 +
5565 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5566 +       if (unlikely(err)) {
5567 +               /* todo: necessary? */
5568 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5569 +               inode_unlock(dst_inode);
5570 +               goto out_rev;
5571 +       }
5572 +
5573 +       if (cpg->bdst < old_ibtop) {
5574 +               if (S_ISREG(inode->i_mode)) {
5575 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5576 +                       if (unlikely(err)) {
5577 +                               /* ignore an error */
5578 +                               /* au_pin_hdir_relock(cpg->pin); */
5579 +                               inode_unlock(dst_inode);
5580 +                               goto out_rev;
5581 +                       }
5582 +               }
5583 +               au_set_ibtop(inode, cpg->bdst);
5584 +       } else
5585 +               au_set_ibbot(inode, cpg->bdst);
5586 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5587 +                     au_hi_flags(inode, isdir));
5588 +
5589 +       /* todo: necessary? */
5590 +       /* err = au_pin_hdir_relock(cpg->pin); */
5591 +       inode_unlock(dst_inode);
5592 +       if (unlikely(err))
5593 +               goto out_rev;
5594 +
5595 +       src_inode = d_inode(h_src);
5596 +       if (!isdir
5597 +           && (src_inode->i_nlink > 1
5598 +               || src_inode->i_state & I_LINKABLE)
5599 +           && plink)
5600 +               au_plink_append(inode, cpg->bdst, h_dst);
5601 +
5602 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5603 +               a->h_path.dentry = h_dst;
5604 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5605 +       }
5606 +       if (!err)
5607 +               goto out_parent; /* success */
5608 +
5609 +       /* revert */
5610 +out_rev:
5611 +       a->h_path.dentry = h_parent;
5612 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5613 +       a->h_path.dentry = h_dst;
5614 +       rerr = 0;
5615 +       if (d_is_positive(h_dst)) {
5616 +               if (!isdir) {
5617 +                       /* no delegation since it is just created */
5618 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5619 +                                           /*delegated*/NULL, /*force*/0);
5620 +               } else
5621 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5622 +       }
5623 +       au_dtime_revert(&a->dt);
5624 +       if (rerr) {
5625 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5626 +               err = -EIO;
5627 +       }
5628 +out_parent:
5629 +       dput(dst_parent);
5630 +       kfree(a);
5631 +out:
5632 +       return err;
5633 +}
5634 +
5635 +#if 0 /* reserved */
5636 +struct au_cpup_single_args {
5637 +       int *errp;
5638 +       struct au_cp_generic *cpg;
5639 +       struct dentry *dst_parent;
5640 +};
5641 +
5642 +static void au_call_cpup_single(void *args)
5643 +{
5644 +       struct au_cpup_single_args *a = args;
5645 +
5646 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5647 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5648 +       au_pin_hdir_release(a->cpg->pin);
5649 +}
5650 +#endif
5651 +
5652 +/*
5653 + * prevent SIGXFSZ in copy-up.
5654 + * testing CAP_MKNOD is for generic fs,
5655 + * but CAP_FSETID is for xfs only, currently.
5656 + */
5657 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5658 +{
5659 +       int do_sio;
5660 +       struct super_block *sb;
5661 +       struct inode *h_dir;
5662 +
5663 +       do_sio = 0;
5664 +       sb = au_pinned_parent(pin)->d_sb;
5665 +       if (!au_wkq_test()
5666 +           && (!au_sbi(sb)->si_plink_maint_pid
5667 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5668 +               switch (mode & S_IFMT) {
5669 +               case S_IFREG:
5670 +                       /* no condition about RLIMIT_FSIZE and the file size */
5671 +                       do_sio = 1;
5672 +                       break;
5673 +               case S_IFCHR:
5674 +               case S_IFBLK:
5675 +                       do_sio = !capable(CAP_MKNOD);
5676 +                       break;
5677 +               }
5678 +               if (!do_sio)
5679 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5680 +                                 && !capable(CAP_FSETID));
5681 +               /* this workaround may be removed in the future */
5682 +               if (!do_sio) {
5683 +                       h_dir = au_pinned_h_dir(pin);
5684 +                       do_sio = h_dir->i_mode & S_ISVTX;
5685 +               }
5686 +       }
5687 +
5688 +       return do_sio;
5689 +}
5690 +
5691 +#if 0 /* reserved */
5692 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5693 +{
5694 +       int err, wkq_err;
5695 +       struct dentry *h_dentry;
5696 +
5697 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5698 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5699 +               err = au_cpup_single(cpg, dst_parent);
5700 +       else {
5701 +               struct au_cpup_single_args args = {
5702 +                       .errp           = &err,
5703 +                       .cpg            = cpg,
5704 +                       .dst_parent     = dst_parent
5705 +               };
5706 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5707 +               if (unlikely(wkq_err))
5708 +                       err = wkq_err;
5709 +       }
5710 +
5711 +       return err;
5712 +}
5713 +#endif
5714 +
5715 +/*
5716 + * copyup the @dentry from the first active lower branch to @bdst,
5717 + * using au_cpup_single().
5718 + */
5719 +static int au_cpup_simple(struct au_cp_generic *cpg)
5720 +{
5721 +       int err;
5722 +       unsigned int flags_orig;
5723 +       struct dentry *dentry;
5724 +
5725 +       AuDebugOn(cpg->bsrc < 0);
5726 +
5727 +       dentry = cpg->dentry;
5728 +       DiMustWriteLock(dentry);
5729 +
5730 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5731 +       if (!err) {
5732 +               flags_orig = cpg->flags;
5733 +               au_fset_cpup(cpg->flags, RENAME);
5734 +               err = au_cpup_single(cpg, NULL);
5735 +               cpg->flags = flags_orig;
5736 +               if (!err)
5737 +                       return 0; /* success */
5738 +
5739 +               /* revert */
5740 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5741 +               au_set_dbtop(dentry, cpg->bsrc);
5742 +       }
5743 +
5744 +       return err;
5745 +}
5746 +
5747 +struct au_cpup_simple_args {
5748 +       int *errp;
5749 +       struct au_cp_generic *cpg;
5750 +};
5751 +
5752 +static void au_call_cpup_simple(void *args)
5753 +{
5754 +       struct au_cpup_simple_args *a = args;
5755 +
5756 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5757 +       *a->errp = au_cpup_simple(a->cpg);
5758 +       au_pin_hdir_release(a->cpg->pin);
5759 +}
5760 +
5761 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5762 +{
5763 +       int err, wkq_err;
5764 +       struct dentry *dentry, *parent;
5765 +       struct file *h_file;
5766 +       struct inode *h_dir;
5767 +
5768 +       dentry = cpg->dentry;
5769 +       h_file = NULL;
5770 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5771 +               AuDebugOn(cpg->bsrc < 0);
5772 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5773 +               err = PTR_ERR(h_file);
5774 +               if (IS_ERR(h_file))
5775 +                       goto out;
5776 +       }
5777 +
5778 +       parent = dget_parent(dentry);
5779 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5780 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5781 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5782 +               err = au_cpup_simple(cpg);
5783 +       else {
5784 +               struct au_cpup_simple_args args = {
5785 +                       .errp           = &err,
5786 +                       .cpg            = cpg
5787 +               };
5788 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5789 +               if (unlikely(wkq_err))
5790 +                       err = wkq_err;
5791 +       }
5792 +
5793 +       dput(parent);
5794 +       if (h_file)
5795 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5796 +
5797 +out:
5798 +       return err;
5799 +}
5800 +
5801 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5802 +{
5803 +       aufs_bindex_t bsrc, bbot;
5804 +       struct dentry *dentry, *h_dentry;
5805 +
5806 +       if (cpg->bsrc < 0) {
5807 +               dentry = cpg->dentry;
5808 +               bbot = au_dbbot(dentry);
5809 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5810 +                       h_dentry = au_h_dptr(dentry, bsrc);
5811 +                       if (h_dentry) {
5812 +                               AuDebugOn(d_is_negative(h_dentry));
5813 +                               break;
5814 +                       }
5815 +               }
5816 +               AuDebugOn(bsrc > bbot);
5817 +               cpg->bsrc = bsrc;
5818 +       }
5819 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5820 +       return au_do_sio_cpup_simple(cpg);
5821 +}
5822 +
5823 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5824 +{
5825 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5826 +       return au_do_sio_cpup_simple(cpg);
5827 +}
5828 +
5829 +/* ---------------------------------------------------------------------- */
5830 +
5831 +/*
5832 + * copyup the deleted file for writing.
5833 + */
5834 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5835 +                        struct file *file)
5836 +{
5837 +       int err;
5838 +       unsigned int flags_orig;
5839 +       aufs_bindex_t bsrc_orig;
5840 +       struct au_dinfo *dinfo;
5841 +       struct {
5842 +               struct au_hdentry *hd;
5843 +               struct dentry *h_dentry;
5844 +       } hdst, hsrc;
5845 +
5846 +       dinfo = au_di(cpg->dentry);
5847 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5848 +
5849 +       bsrc_orig = cpg->bsrc;
5850 +       cpg->bsrc = dinfo->di_btop;
5851 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5852 +       hdst.h_dentry = hdst.hd->hd_dentry;
5853 +       hdst.hd->hd_dentry = wh_dentry;
5854 +       dinfo->di_btop = cpg->bdst;
5855 +
5856 +       hsrc.h_dentry = NULL;
5857 +       if (file) {
5858 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5859 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5860 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5861 +       }
5862 +       flags_orig = cpg->flags;
5863 +       cpg->flags = !AuCpup_DTIME;
5864 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5865 +       cpg->flags = flags_orig;
5866 +       if (file) {
5867 +               if (!err)
5868 +                       err = au_reopen_nondir(file);
5869 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5870 +       }
5871 +       hdst.hd->hd_dentry = hdst.h_dentry;
5872 +       dinfo->di_btop = cpg->bsrc;
5873 +       cpg->bsrc = bsrc_orig;
5874 +
5875 +       return err;
5876 +}
5877 +
5878 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5879 +{
5880 +       int err;
5881 +       aufs_bindex_t bdst;
5882 +       struct au_dtime dt;
5883 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5884 +       struct au_branch *br;
5885 +       struct path h_path;
5886 +
5887 +       dentry = cpg->dentry;
5888 +       bdst = cpg->bdst;
5889 +       br = au_sbr(dentry->d_sb, bdst);
5890 +       parent = dget_parent(dentry);
5891 +       h_parent = au_h_dptr(parent, bdst);
5892 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5893 +       err = PTR_ERR(wh_dentry);
5894 +       if (IS_ERR(wh_dentry))
5895 +               goto out;
5896 +
5897 +       h_path.dentry = h_parent;
5898 +       h_path.mnt = au_br_mnt(br);
5899 +       au_dtime_store(&dt, parent, &h_path);
5900 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5901 +       if (unlikely(err))
5902 +               goto out_wh;
5903 +
5904 +       dget(wh_dentry);
5905 +       h_path.dentry = wh_dentry;
5906 +       if (!d_is_dir(wh_dentry)) {
5907 +               /* no delegation since it is just created */
5908 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5909 +                                  /*delegated*/NULL, /*force*/0);
5910 +       } else
5911 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5912 +       if (unlikely(err)) {
5913 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5914 +                       wh_dentry, err);
5915 +               err = -EIO;
5916 +       }
5917 +       au_dtime_revert(&dt);
5918 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5919 +
5920 +out_wh:
5921 +       dput(wh_dentry);
5922 +out:
5923 +       dput(parent);
5924 +       return err;
5925 +}
5926 +
5927 +struct au_cpup_wh_args {
5928 +       int *errp;
5929 +       struct au_cp_generic *cpg;
5930 +       struct file *file;
5931 +};
5932 +
5933 +static void au_call_cpup_wh(void *args)
5934 +{
5935 +       struct au_cpup_wh_args *a = args;
5936 +
5937 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5938 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5939 +       au_pin_hdir_release(a->cpg->pin);
5940 +}
5941 +
5942 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5943 +{
5944 +       int err, wkq_err;
5945 +       aufs_bindex_t bdst;
5946 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5947 +       struct inode *dir, *h_dir, *h_tmpdir;
5948 +       struct au_wbr *wbr;
5949 +       struct au_pin wh_pin, *pin_orig;
5950 +
5951 +       dentry = cpg->dentry;
5952 +       bdst = cpg->bdst;
5953 +       parent = dget_parent(dentry);
5954 +       dir = d_inode(parent);
5955 +       h_orph = NULL;
5956 +       h_parent = NULL;
5957 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5958 +       h_tmpdir = h_dir;
5959 +       pin_orig = NULL;
5960 +       if (!h_dir->i_nlink) {
5961 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5962 +               h_orph = wbr->wbr_orph;
5963 +
5964 +               h_parent = dget(au_h_dptr(parent, bdst));
5965 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5966 +               h_tmpdir = d_inode(h_orph);
5967 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5968 +
5969 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
5970 +               /* todo: au_h_open_pre()? */
5971 +
5972 +               pin_orig = cpg->pin;
5973 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5974 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5975 +               cpg->pin = &wh_pin;
5976 +       }
5977 +
5978 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5979 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5980 +               err = au_cpup_wh(cpg, file);
5981 +       else {
5982 +               struct au_cpup_wh_args args = {
5983 +                       .errp   = &err,
5984 +                       .cpg    = cpg,
5985 +                       .file   = file
5986 +               };
5987 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5988 +               if (unlikely(wkq_err))
5989 +                       err = wkq_err;
5990 +       }
5991 +
5992 +       if (h_orph) {
5993 +               inode_unlock(h_tmpdir);
5994 +               /* todo: au_h_open_post()? */
5995 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5996 +               au_set_h_dptr(parent, bdst, h_parent);
5997 +               AuDebugOn(!pin_orig);
5998 +               cpg->pin = pin_orig;
5999 +       }
6000 +       iput(h_dir);
6001 +       dput(parent);
6002 +
6003 +       return err;
6004 +}
6005 +
6006 +/* ---------------------------------------------------------------------- */
6007 +
6008 +/*
6009 + * generic routine for both of copy-up and copy-down.
6010 + */
6011 +/* cf. revalidate function in file.c */
6012 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6013 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6014 +                        struct au_pin *pin,
6015 +                        struct dentry *h_parent, void *arg),
6016 +              void *arg)
6017 +{
6018 +       int err;
6019 +       struct au_pin pin;
6020 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6021 +
6022 +       err = 0;
6023 +       parent = dget_parent(dentry);
6024 +       if (IS_ROOT(parent))
6025 +               goto out;
6026 +
6027 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6028 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6029 +
6030 +       /* do not use au_dpage */
6031 +       real_parent = parent;
6032 +       while (1) {
6033 +               dput(parent);
6034 +               parent = dget_parent(dentry);
6035 +               h_parent = au_h_dptr(parent, bdst);
6036 +               if (h_parent)
6037 +                       goto out; /* success */
6038 +
6039 +               /* find top dir which is necessary to cpup */
6040 +               do {
6041 +                       d = parent;
6042 +                       dput(parent);
6043 +                       parent = dget_parent(d);
6044 +                       di_read_lock_parent3(parent, !AuLock_IR);
6045 +                       h_parent = au_h_dptr(parent, bdst);
6046 +                       di_read_unlock(parent, !AuLock_IR);
6047 +               } while (!h_parent);
6048 +
6049 +               if (d != real_parent)
6050 +                       di_write_lock_child3(d);
6051 +
6052 +               /* somebody else might create while we were sleeping */
6053 +               h_dentry = au_h_dptr(d, bdst);
6054 +               if (!h_dentry || d_is_negative(h_dentry)) {
6055 +                       if (h_dentry)
6056 +                               au_update_dbtop(d);
6057 +
6058 +                       au_pin_set_dentry(&pin, d);
6059 +                       err = au_do_pin(&pin);
6060 +                       if (!err) {
6061 +                               err = cp(d, bdst, &pin, h_parent, arg);
6062 +                               au_unpin(&pin);
6063 +                       }
6064 +               }
6065 +
6066 +               if (d != real_parent)
6067 +                       di_write_unlock(d);
6068 +               if (unlikely(err))
6069 +                       break;
6070 +       }
6071 +
6072 +out:
6073 +       dput(parent);
6074 +       return err;
6075 +}
6076 +
6077 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6078 +                      struct au_pin *pin,
6079 +                      struct dentry *h_parent __maybe_unused,
6080 +                      void *arg __maybe_unused)
6081 +{
6082 +       struct au_cp_generic cpg = {
6083 +               .dentry = dentry,
6084 +               .bdst   = bdst,
6085 +               .bsrc   = -1,
6086 +               .len    = 0,
6087 +               .pin    = pin,
6088 +               .flags  = AuCpup_DTIME
6089 +       };
6090 +       return au_sio_cpup_simple(&cpg);
6091 +}
6092 +
6093 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6094 +{
6095 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6096 +}
6097 +
6098 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6099 +{
6100 +       int err;
6101 +       struct dentry *parent;
6102 +       struct inode *dir;
6103 +
6104 +       parent = dget_parent(dentry);
6105 +       dir = d_inode(parent);
6106 +       err = 0;
6107 +       if (au_h_iptr(dir, bdst))
6108 +               goto out;
6109 +
6110 +       di_read_unlock(parent, AuLock_IR);
6111 +       di_write_lock_parent(parent);
6112 +       /* someone else might change our inode while we were sleeping */
6113 +       if (!au_h_iptr(dir, bdst))
6114 +               err = au_cpup_dirs(dentry, bdst);
6115 +       di_downgrade_lock(parent, AuLock_IR);
6116 +
6117 +out:
6118 +       dput(parent);
6119 +       return err;
6120 +}
6121 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6122 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6123 +++ linux/fs/aufs/cpup.h        2017-07-29 12:14:25.899708630 +0200
6124 @@ -0,0 +1,94 @@
6125 +/*
6126 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6127 + *
6128 + * This program, aufs is free software; you can redistribute it and/or modify
6129 + * it under the terms of the GNU General Public License as published by
6130 + * the Free Software Foundation; either version 2 of the License, or
6131 + * (at your option) any later version.
6132 + *
6133 + * This program is distributed in the hope that it will be useful,
6134 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6135 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6136 + * GNU General Public License for more details.
6137 + *
6138 + * You should have received a copy of the GNU General Public License
6139 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6140 + */
6141 +
6142 +/*
6143 + * copy-up/down functions
6144 + */
6145 +
6146 +#ifndef __AUFS_CPUP_H__
6147 +#define __AUFS_CPUP_H__
6148 +
6149 +#ifdef __KERNEL__
6150 +
6151 +#include <linux/path.h>
6152 +
6153 +struct inode;
6154 +struct file;
6155 +struct au_pin;
6156 +
6157 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6158 +void au_cpup_attr_timesizes(struct inode *inode);
6159 +void au_cpup_attr_nlink(struct inode *inode, int force);
6160 +void au_cpup_attr_changeable(struct inode *inode);
6161 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6162 +void au_cpup_attr_all(struct inode *inode, int force);
6163 +
6164 +/* ---------------------------------------------------------------------- */
6165 +
6166 +struct au_cp_generic {
6167 +       struct dentry   *dentry;
6168 +       aufs_bindex_t   bdst, bsrc;
6169 +       loff_t          len;
6170 +       struct au_pin   *pin;
6171 +       unsigned int    flags;
6172 +};
6173 +
6174 +/* cpup flags */
6175 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6176 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6177 +                                                  for link(2) */
6178 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6179 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6180 +                                                  cpup */
6181 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6182 +                                                  existing entry */
6183 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6184 +                                                  the branch is marked as RO */
6185 +
6186 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6187 +#define au_fset_cpup(flags, name) \
6188 +       do { (flags) |= AuCpup_##name; } while (0)
6189 +#define au_fclr_cpup(flags, name) \
6190 +       do { (flags) &= ~AuCpup_##name; } while (0)
6191 +
6192 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6193 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6194 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6195 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6196 +
6197 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6198 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6199 +                        struct au_pin *pin,
6200 +                        struct dentry *h_parent, void *arg),
6201 +              void *arg);
6202 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6203 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6204 +
6205 +/* ---------------------------------------------------------------------- */
6206 +
6207 +/* keep timestamps when copyup */
6208 +struct au_dtime {
6209 +       struct dentry *dt_dentry;
6210 +       struct path dt_h_path;
6211 +       struct timespec dt_atime, dt_mtime;
6212 +};
6213 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6214 +                   struct path *h_path);
6215 +void au_dtime_revert(struct au_dtime *dt);
6216 +
6217 +#endif /* __KERNEL__ */
6218 +#endif /* __AUFS_CPUP_H__ */
6219 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6220 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6221 +++ linux/fs/aufs/dbgaufs.c     2017-07-29 12:14:25.899708630 +0200
6222 @@ -0,0 +1,438 @@
6223 +/*
6224 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6225 + *
6226 + * This program, aufs is free software; you can redistribute it and/or modify
6227 + * it under the terms of the GNU General Public License as published by
6228 + * the Free Software Foundation; either version 2 of the License, or
6229 + * (at your option) any later version.
6230 + *
6231 + * This program is distributed in the hope that it will be useful,
6232 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6233 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6234 + * GNU General Public License for more details.
6235 + *
6236 + * You should have received a copy of the GNU General Public License
6237 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6238 + */
6239 +
6240 +/*
6241 + * debugfs interface
6242 + */
6243 +
6244 +#include <linux/debugfs.h>
6245 +#include "aufs.h"
6246 +
6247 +#ifndef CONFIG_SYSFS
6248 +#error DEBUG_FS depends upon SYSFS
6249 +#endif
6250 +
6251 +static struct dentry *dbgaufs;
6252 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6253 +
6254 +/* 20 is max digits length of ulong 64 */
6255 +struct dbgaufs_arg {
6256 +       int n;
6257 +       char a[20 * 4];
6258 +};
6259 +
6260 +/*
6261 + * common function for all XINO files
6262 + */
6263 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6264 +                             struct file *file)
6265 +{
6266 +       kfree(file->private_data);
6267 +       return 0;
6268 +}
6269 +
6270 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6271 +{
6272 +       int err;
6273 +       struct kstat st;
6274 +       struct dbgaufs_arg *p;
6275 +
6276 +       err = -ENOMEM;
6277 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6278 +       if (unlikely(!p))
6279 +               goto out;
6280 +
6281 +       err = 0;
6282 +       p->n = 0;
6283 +       file->private_data = p;
6284 +       if (!xf)
6285 +               goto out;
6286 +
6287 +       err = vfsub_getattr(&xf->f_path, &st);
6288 +       if (!err) {
6289 +               if (do_fcnt)
6290 +                       p->n = snprintf
6291 +                               (p->a, sizeof(p->a), "%ld, %llux%u %lld\n",
6292 +                                (long)file_count(xf), st.blocks, st.blksize,
6293 +                                (long long)st.size);
6294 +               else
6295 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6296 +                                       st.blocks, st.blksize,
6297 +                                       (long long)st.size);
6298 +               AuDebugOn(p->n >= sizeof(p->a));
6299 +       } else {
6300 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6301 +               err = 0;
6302 +       }
6303 +
6304 +out:
6305 +       return err;
6306 +
6307 +}
6308 +
6309 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6310 +                              size_t count, loff_t *ppos)
6311 +{
6312 +       struct dbgaufs_arg *p;
6313 +
6314 +       p = file->private_data;
6315 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6316 +}
6317 +
6318 +/* ---------------------------------------------------------------------- */
6319 +
6320 +struct dbgaufs_plink_arg {
6321 +       int n;
6322 +       char a[];
6323 +};
6324 +
6325 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6326 +                                struct file *file)
6327 +{
6328 +       free_page((unsigned long)file->private_data);
6329 +       return 0;
6330 +}
6331 +
6332 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6333 +{
6334 +       int err, i, limit;
6335 +       unsigned long n, sum;
6336 +       struct dbgaufs_plink_arg *p;
6337 +       struct au_sbinfo *sbinfo;
6338 +       struct super_block *sb;
6339 +       struct au_sphlhead *sphl;
6340 +
6341 +       err = -ENOMEM;
6342 +       p = (void *)get_zeroed_page(GFP_NOFS);
6343 +       if (unlikely(!p))
6344 +               goto out;
6345 +
6346 +       err = -EFBIG;
6347 +       sbinfo = inode->i_private;
6348 +       sb = sbinfo->si_sb;
6349 +       si_noflush_read_lock(sb);
6350 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6351 +               limit = PAGE_SIZE - sizeof(p->n);
6352 +
6353 +               /* the number of buckets */
6354 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6355 +               p->n += n;
6356 +               limit -= n;
6357 +
6358 +               sum = 0;
6359 +               for (i = 0, sphl = sbinfo->si_plink;
6360 +                    i < AuPlink_NHASH;
6361 +                    i++, sphl++) {
6362 +                       n = au_sphl_count(sphl);
6363 +                       sum += n;
6364 +
6365 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6366 +                       p->n += n;
6367 +                       limit -= n;
6368 +                       if (unlikely(limit <= 0))
6369 +                               goto out_free;
6370 +               }
6371 +               p->a[p->n - 1] = '\n';
6372 +
6373 +               /* the sum of plinks */
6374 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6375 +               p->n += n;
6376 +               limit -= n;
6377 +               if (unlikely(limit <= 0))
6378 +                       goto out_free;
6379 +       } else {
6380 +#define str "1\n0\n0\n"
6381 +               p->n = sizeof(str) - 1;
6382 +               strcpy(p->a, str);
6383 +#undef str
6384 +       }
6385 +       si_read_unlock(sb);
6386 +
6387 +       err = 0;
6388 +       file->private_data = p;
6389 +       goto out; /* success */
6390 +
6391 +out_free:
6392 +       free_page((unsigned long)p);
6393 +out:
6394 +       return err;
6395 +}
6396 +
6397 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6398 +                                 size_t count, loff_t *ppos)
6399 +{
6400 +       struct dbgaufs_plink_arg *p;
6401 +
6402 +       p = file->private_data;
6403 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6404 +}
6405 +
6406 +static const struct file_operations dbgaufs_plink_fop = {
6407 +       .owner          = THIS_MODULE,
6408 +       .open           = dbgaufs_plink_open,
6409 +       .release        = dbgaufs_plink_release,
6410 +       .read           = dbgaufs_plink_read
6411 +};
6412 +
6413 +/* ---------------------------------------------------------------------- */
6414 +
6415 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6416 +{
6417 +       int err;
6418 +       struct au_sbinfo *sbinfo;
6419 +       struct super_block *sb;
6420 +
6421 +       sbinfo = inode->i_private;
6422 +       sb = sbinfo->si_sb;
6423 +       si_noflush_read_lock(sb);
6424 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6425 +       si_read_unlock(sb);
6426 +       return err;
6427 +}
6428 +
6429 +static const struct file_operations dbgaufs_xib_fop = {
6430 +       .owner          = THIS_MODULE,
6431 +       .open           = dbgaufs_xib_open,
6432 +       .release        = dbgaufs_xi_release,
6433 +       .read           = dbgaufs_xi_read
6434 +};
6435 +
6436 +/* ---------------------------------------------------------------------- */
6437 +
6438 +#define DbgaufsXi_PREFIX "xi"
6439 +
6440 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6441 +{
6442 +       int err;
6443 +       long l;
6444 +       struct au_sbinfo *sbinfo;
6445 +       struct super_block *sb;
6446 +       struct file *xf;
6447 +       struct qstr *name;
6448 +
6449 +       err = -ENOENT;
6450 +       xf = NULL;
6451 +       name = &file->f_path.dentry->d_name;
6452 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6453 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6454 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6455 +               goto out;
6456 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6457 +       if (unlikely(err))
6458 +               goto out;
6459 +
6460 +       sbinfo = inode->i_private;
6461 +       sb = sbinfo->si_sb;
6462 +       si_noflush_read_lock(sb);
6463 +       if (l <= au_sbbot(sb)) {
6464 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6465 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6466 +       } else
6467 +               err = -ENOENT;
6468 +       si_read_unlock(sb);
6469 +
6470 +out:
6471 +       return err;
6472 +}
6473 +
6474 +static const struct file_operations dbgaufs_xino_fop = {
6475 +       .owner          = THIS_MODULE,
6476 +       .open           = dbgaufs_xino_open,
6477 +       .release        = dbgaufs_xi_release,
6478 +       .read           = dbgaufs_xi_read
6479 +};
6480 +
6481 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6482 +{
6483 +       aufs_bindex_t bbot;
6484 +       struct au_branch *br;
6485 +       struct au_xino_file *xi;
6486 +
6487 +       if (!au_sbi(sb)->si_dbgaufs)
6488 +               return;
6489 +
6490 +       bbot = au_sbbot(sb);
6491 +       for (; bindex <= bbot; bindex++) {
6492 +               br = au_sbr(sb, bindex);
6493 +               xi = &br->br_xino;
6494 +               /* debugfs acquires the parent i_mutex */
6495 +               lockdep_off();
6496 +               debugfs_remove(xi->xi_dbgaufs);
6497 +               lockdep_on();
6498 +               xi->xi_dbgaufs = NULL;
6499 +       }
6500 +}
6501 +
6502 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6503 +{
6504 +       struct au_sbinfo *sbinfo;
6505 +       struct dentry *parent;
6506 +       struct au_branch *br;
6507 +       struct au_xino_file *xi;
6508 +       aufs_bindex_t bbot;
6509 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6510 +
6511 +       sbinfo = au_sbi(sb);
6512 +       parent = sbinfo->si_dbgaufs;
6513 +       if (!parent)
6514 +               return;
6515 +
6516 +       bbot = au_sbbot(sb);
6517 +       for (; bindex <= bbot; bindex++) {
6518 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6519 +               br = au_sbr(sb, bindex);
6520 +               xi = &br->br_xino;
6521 +               AuDebugOn(xi->xi_dbgaufs);
6522 +               /* debugfs acquires the parent i_mutex */
6523 +               lockdep_off();
6524 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6525 +                                                    sbinfo, &dbgaufs_xino_fop);
6526 +               lockdep_on();
6527 +               /* ignore an error */
6528 +               if (unlikely(!xi->xi_dbgaufs))
6529 +                       AuWarn1("failed %s under debugfs\n", name);
6530 +       }
6531 +}
6532 +
6533 +/* ---------------------------------------------------------------------- */
6534 +
6535 +#ifdef CONFIG_AUFS_EXPORT
6536 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6537 +{
6538 +       int err;
6539 +       struct au_sbinfo *sbinfo;
6540 +       struct super_block *sb;
6541 +
6542 +       sbinfo = inode->i_private;
6543 +       sb = sbinfo->si_sb;
6544 +       si_noflush_read_lock(sb);
6545 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6546 +       si_read_unlock(sb);
6547 +       return err;
6548 +}
6549 +
6550 +static const struct file_operations dbgaufs_xigen_fop = {
6551 +       .owner          = THIS_MODULE,
6552 +       .open           = dbgaufs_xigen_open,
6553 +       .release        = dbgaufs_xi_release,
6554 +       .read           = dbgaufs_xi_read
6555 +};
6556 +
6557 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6558 +{
6559 +       int err;
6560 +
6561 +       /*
6562 +        * This function is a dynamic '__init' function actually,
6563 +        * so the tiny check for si_rwsem is unnecessary.
6564 +        */
6565 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6566 +
6567 +       err = -EIO;
6568 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6569 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6570 +                &dbgaufs_xigen_fop);
6571 +       if (sbinfo->si_dbgaufs_xigen)
6572 +               err = 0;
6573 +
6574 +       return err;
6575 +}
6576 +#else
6577 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6578 +{
6579 +       return 0;
6580 +}
6581 +#endif /* CONFIG_AUFS_EXPORT */
6582 +
6583 +/* ---------------------------------------------------------------------- */
6584 +
6585 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6586 +{
6587 +       /*
6588 +        * This function is a dynamic '__fin' function actually,
6589 +        * so the tiny check for si_rwsem is unnecessary.
6590 +        */
6591 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6592 +
6593 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6594 +       sbinfo->si_dbgaufs = NULL;
6595 +       kobject_put(&sbinfo->si_kobj);
6596 +}
6597 +
6598 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6599 +{
6600 +       int err;
6601 +       char name[SysaufsSiNameLen];
6602 +
6603 +       /*
6604 +        * This function is a dynamic '__init' function actually,
6605 +        * so the tiny check for si_rwsem is unnecessary.
6606 +        */
6607 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6608 +
6609 +       err = -ENOENT;
6610 +       if (!dbgaufs) {
6611 +               AuErr1("/debug/aufs is uninitialized\n");
6612 +               goto out;
6613 +       }
6614 +
6615 +       err = -EIO;
6616 +       sysaufs_name(sbinfo, name);
6617 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6618 +       if (unlikely(!sbinfo->si_dbgaufs))
6619 +               goto out;
6620 +       kobject_get(&sbinfo->si_kobj);
6621 +
6622 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6623 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6624 +                &dbgaufs_xib_fop);
6625 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6626 +               goto out_dir;
6627 +
6628 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6629 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6630 +                &dbgaufs_plink_fop);
6631 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6632 +               goto out_dir;
6633 +
6634 +       err = dbgaufs_xigen_init(sbinfo);
6635 +       if (!err)
6636 +               goto out; /* success */
6637 +
6638 +out_dir:
6639 +       dbgaufs_si_fin(sbinfo);
6640 +out:
6641 +       return err;
6642 +}
6643 +
6644 +/* ---------------------------------------------------------------------- */
6645 +
6646 +void dbgaufs_fin(void)
6647 +{
6648 +       debugfs_remove(dbgaufs);
6649 +}
6650 +
6651 +int __init dbgaufs_init(void)
6652 +{
6653 +       int err;
6654 +
6655 +       err = -EIO;
6656 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6657 +       if (dbgaufs)
6658 +               err = 0;
6659 +       return err;
6660 +}
6661 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6662 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6663 +++ linux/fs/aufs/dbgaufs.h     2017-07-29 12:14:25.899708630 +0200
6664 @@ -0,0 +1,48 @@
6665 +/*
6666 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6667 + *
6668 + * This program, aufs is free software; you can redistribute it and/or modify
6669 + * it under the terms of the GNU General Public License as published by
6670 + * the Free Software Foundation; either version 2 of the License, or
6671 + * (at your option) any later version.
6672 + *
6673 + * This program is distributed in the hope that it will be useful,
6674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6676 + * GNU General Public License for more details.
6677 + *
6678 + * You should have received a copy of the GNU General Public License
6679 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6680 + */
6681 +
6682 +/*
6683 + * debugfs interface
6684 + */
6685 +
6686 +#ifndef __DBGAUFS_H__
6687 +#define __DBGAUFS_H__
6688 +
6689 +#ifdef __KERNEL__
6690 +
6691 +struct super_block;
6692 +struct au_sbinfo;
6693 +
6694 +#ifdef CONFIG_DEBUG_FS
6695 +/* dbgaufs.c */
6696 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6697 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6698 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6699 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6700 +void dbgaufs_fin(void);
6701 +int __init dbgaufs_init(void);
6702 +#else
6703 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6704 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6705 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6706 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6707 +AuStubVoid(dbgaufs_fin, void)
6708 +AuStubInt0(__init dbgaufs_init, void)
6709 +#endif /* CONFIG_DEBUG_FS */
6710 +
6711 +#endif /* __KERNEL__ */
6712 +#endif /* __DBGAUFS_H__ */
6713 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6714 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6715 +++ linux/fs/aufs/dcsub.c       2017-07-29 12:14:25.899708630 +0200
6716 @@ -0,0 +1,225 @@
6717 +/*
6718 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6719 + *
6720 + * This program, aufs is free software; you can redistribute it and/or modify
6721 + * it under the terms of the GNU General Public License as published by
6722 + * the Free Software Foundation; either version 2 of the License, or
6723 + * (at your option) any later version.
6724 + *
6725 + * This program is distributed in the hope that it will be useful,
6726 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6727 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6728 + * GNU General Public License for more details.
6729 + *
6730 + * You should have received a copy of the GNU General Public License
6731 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6732 + */
6733 +
6734 +/*
6735 + * sub-routines for dentry cache
6736 + */
6737 +
6738 +#include "aufs.h"
6739 +
6740 +static void au_dpage_free(struct au_dpage *dpage)
6741 +{
6742 +       int i;
6743 +       struct dentry **p;
6744 +
6745 +       p = dpage->dentries;
6746 +       for (i = 0; i < dpage->ndentry; i++)
6747 +               dput(*p++);
6748 +       free_page((unsigned long)dpage->dentries);
6749 +}
6750 +
6751 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6752 +{
6753 +       int err;
6754 +       void *p;
6755 +
6756 +       err = -ENOMEM;
6757 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6758 +       if (unlikely(!dpages->dpages))
6759 +               goto out;
6760 +
6761 +       p = (void *)__get_free_page(gfp);
6762 +       if (unlikely(!p))
6763 +               goto out_dpages;
6764 +
6765 +       dpages->dpages[0].ndentry = 0;
6766 +       dpages->dpages[0].dentries = p;
6767 +       dpages->ndpage = 1;
6768 +       return 0; /* success */
6769 +
6770 +out_dpages:
6771 +       kfree(dpages->dpages);
6772 +out:
6773 +       return err;
6774 +}
6775 +
6776 +void au_dpages_free(struct au_dcsub_pages *dpages)
6777 +{
6778 +       int i;
6779 +       struct au_dpage *p;
6780 +
6781 +       p = dpages->dpages;
6782 +       for (i = 0; i < dpages->ndpage; i++)
6783 +               au_dpage_free(p++);
6784 +       kfree(dpages->dpages);
6785 +}
6786 +
6787 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6788 +                           struct dentry *dentry, gfp_t gfp)
6789 +{
6790 +       int err, sz;
6791 +       struct au_dpage *dpage;
6792 +       void *p;
6793 +
6794 +       dpage = dpages->dpages + dpages->ndpage - 1;
6795 +       sz = PAGE_SIZE / sizeof(dentry);
6796 +       if (unlikely(dpage->ndentry >= sz)) {
6797 +               AuLabel(new dpage);
6798 +               err = -ENOMEM;
6799 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6800 +               p = au_kzrealloc(dpages->dpages, sz,
6801 +                                sz + sizeof(*dpages->dpages), gfp,
6802 +                                /*may_shrink*/0);
6803 +               if (unlikely(!p))
6804 +                       goto out;
6805 +
6806 +               dpages->dpages = p;
6807 +               dpage = dpages->dpages + dpages->ndpage;
6808 +               p = (void *)__get_free_page(gfp);
6809 +               if (unlikely(!p))
6810 +                       goto out;
6811 +
6812 +               dpage->ndentry = 0;
6813 +               dpage->dentries = p;
6814 +               dpages->ndpage++;
6815 +       }
6816 +
6817 +       AuDebugOn(au_dcount(dentry) <= 0);
6818 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6819 +       return 0; /* success */
6820 +
6821 +out:
6822 +       return err;
6823 +}
6824 +
6825 +/* todo: BAD approach */
6826 +/* copied from linux/fs/dcache.c */
6827 +enum d_walk_ret {
6828 +       D_WALK_CONTINUE,
6829 +       D_WALK_QUIT,
6830 +       D_WALK_NORETRY,
6831 +       D_WALK_SKIP,
6832 +};
6833 +
6834 +extern void d_walk(struct dentry *parent, void *data,
6835 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6836 +                  void (*finish)(void *));
6837 +
6838 +struct ac_dpages_arg {
6839 +       int err;
6840 +       struct au_dcsub_pages *dpages;
6841 +       struct super_block *sb;
6842 +       au_dpages_test test;
6843 +       void *arg;
6844 +};
6845 +
6846 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6847 +{
6848 +       enum d_walk_ret ret;
6849 +       struct ac_dpages_arg *arg = _arg;
6850 +
6851 +       ret = D_WALK_CONTINUE;
6852 +       if (dentry->d_sb == arg->sb
6853 +           && !IS_ROOT(dentry)
6854 +           && au_dcount(dentry) > 0
6855 +           && au_di(dentry)
6856 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6857 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6858 +               if (unlikely(arg->err))
6859 +                       ret = D_WALK_QUIT;
6860 +       }
6861 +
6862 +       return ret;
6863 +}
6864 +
6865 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6866 +                  au_dpages_test test, void *arg)
6867 +{
6868 +       struct ac_dpages_arg args = {
6869 +               .err    = 0,
6870 +               .dpages = dpages,
6871 +               .sb     = root->d_sb,
6872 +               .test   = test,
6873 +               .arg    = arg
6874 +       };
6875 +
6876 +       d_walk(root, &args, au_call_dpages_append, NULL);
6877 +
6878 +       return args.err;
6879 +}
6880 +
6881 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6882 +                      int do_include, au_dpages_test test, void *arg)
6883 +{
6884 +       int err;
6885 +
6886 +       err = 0;
6887 +       write_seqlock(&rename_lock);
6888 +       spin_lock(&dentry->d_lock);
6889 +       if (do_include
6890 +           && au_dcount(dentry) > 0
6891 +           && (!test || test(dentry, arg)))
6892 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6893 +       spin_unlock(&dentry->d_lock);
6894 +       if (unlikely(err))
6895 +               goto out;
6896 +
6897 +       /*
6898 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6899 +        * mount
6900 +        */
6901 +       while (!IS_ROOT(dentry)) {
6902 +               dentry = dentry->d_parent; /* rename_lock is locked */
6903 +               spin_lock(&dentry->d_lock);
6904 +               if (au_dcount(dentry) > 0
6905 +                   && (!test || test(dentry, arg)))
6906 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6907 +               spin_unlock(&dentry->d_lock);
6908 +               if (unlikely(err))
6909 +                       break;
6910 +       }
6911 +
6912 +out:
6913 +       write_sequnlock(&rename_lock);
6914 +       return err;
6915 +}
6916 +
6917 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6918 +{
6919 +       return au_di(dentry) && dentry->d_sb == arg;
6920 +}
6921 +
6922 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6923 +                           struct dentry *dentry, int do_include)
6924 +{
6925 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6926 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6927 +}
6928 +
6929 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6930 +{
6931 +       struct path path[2] = {
6932 +               {
6933 +                       .dentry = d1
6934 +               },
6935 +               {
6936 +                       .dentry = d2
6937 +               }
6938 +       };
6939 +
6940 +       return path_is_under(path + 0, path + 1);
6941 +}
6942 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6943 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6944 +++ linux/fs/aufs/dcsub.h       2017-07-29 12:14:25.899708630 +0200
6945 @@ -0,0 +1,136 @@
6946 +/*
6947 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6948 + *
6949 + * This program, aufs is free software; you can redistribute it and/or modify
6950 + * it under the terms of the GNU General Public License as published by
6951 + * the Free Software Foundation; either version 2 of the License, or
6952 + * (at your option) any later version.
6953 + *
6954 + * This program is distributed in the hope that it will be useful,
6955 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6956 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6957 + * GNU General Public License for more details.
6958 + *
6959 + * You should have received a copy of the GNU General Public License
6960 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6961 + */
6962 +
6963 +/*
6964 + * sub-routines for dentry cache
6965 + */
6966 +
6967 +#ifndef __AUFS_DCSUB_H__
6968 +#define __AUFS_DCSUB_H__
6969 +
6970 +#ifdef __KERNEL__
6971 +
6972 +#include <linux/dcache.h>
6973 +#include <linux/fs.h>
6974 +
6975 +struct au_dpage {
6976 +       int ndentry;
6977 +       struct dentry **dentries;
6978 +};
6979 +
6980 +struct au_dcsub_pages {
6981 +       int ndpage;
6982 +       struct au_dpage *dpages;
6983 +};
6984 +
6985 +/* ---------------------------------------------------------------------- */
6986 +
6987 +/* dcsub.c */
6988 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6989 +void au_dpages_free(struct au_dcsub_pages *dpages);
6990 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6991 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6992 +                  au_dpages_test test, void *arg);
6993 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6994 +                      int do_include, au_dpages_test test, void *arg);
6995 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6996 +                           struct dentry *dentry, int do_include);
6997 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6998 +
6999 +/* ---------------------------------------------------------------------- */
7000 +
7001 +/*
7002 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7003 + * include/linux/dcache.h. Try them (in the future).
7004 + */
7005 +
7006 +static inline int au_d_hashed_positive(struct dentry *d)
7007 +{
7008 +       int err;
7009 +       struct inode *inode = d_inode(d);
7010 +
7011 +       err = 0;
7012 +       if (unlikely(d_unhashed(d)
7013 +                    || d_is_negative(d)
7014 +                    || !inode->i_nlink))
7015 +               err = -ENOENT;
7016 +       return err;
7017 +}
7018 +
7019 +static inline int au_d_linkable(struct dentry *d)
7020 +{
7021 +       int err;
7022 +       struct inode *inode = d_inode(d);
7023 +
7024 +       err = au_d_hashed_positive(d);
7025 +       if (err
7026 +           && d_is_positive(d)
7027 +           && (inode->i_state & I_LINKABLE))
7028 +               err = 0;
7029 +       return err;
7030 +}
7031 +
7032 +static inline int au_d_alive(struct dentry *d)
7033 +{
7034 +       int err;
7035 +       struct inode *inode;
7036 +
7037 +       err = 0;
7038 +       if (!IS_ROOT(d))
7039 +               err = au_d_hashed_positive(d);
7040 +       else {
7041 +               inode = d_inode(d);
7042 +               if (unlikely(d_unlinked(d)
7043 +                            || d_is_negative(d)
7044 +                            || !inode->i_nlink))
7045 +                       err = -ENOENT;
7046 +       }
7047 +       return err;
7048 +}
7049 +
7050 +static inline int au_alive_dir(struct dentry *d)
7051 +{
7052 +       int err;
7053 +
7054 +       err = au_d_alive(d);
7055 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7056 +               err = -ENOENT;
7057 +       return err;
7058 +}
7059 +
7060 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7061 +{
7062 +       return a->len == b->len
7063 +               && !memcmp(a->name, b->name, a->len);
7064 +}
7065 +
7066 +/*
7067 + * by the commit
7068 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7069 + *                     taking d_lock
7070 + * the type of d_lockref.count became int, but the inlined function d_count()
7071 + * still returns unsigned int.
7072 + * I don't know why. Maybe it is for every d_count() users?
7073 + * Anyway au_dcount() lives on.
7074 + */
7075 +static inline int au_dcount(struct dentry *d)
7076 +{
7077 +       return (int)d_count(d);
7078 +}
7079 +
7080 +#endif /* __KERNEL__ */
7081 +#endif /* __AUFS_DCSUB_H__ */
7082 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7083 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7084 +++ linux/fs/aufs/debug.c       2017-07-29 12:14:25.899708630 +0200
7085 @@ -0,0 +1,440 @@
7086 +/*
7087 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7088 + *
7089 + * This program, aufs is free software; you can redistribute it and/or modify
7090 + * it under the terms of the GNU General Public License as published by
7091 + * the Free Software Foundation; either version 2 of the License, or
7092 + * (at your option) any later version.
7093 + *
7094 + * This program is distributed in the hope that it will be useful,
7095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7097 + * GNU General Public License for more details.
7098 + *
7099 + * You should have received a copy of the GNU General Public License
7100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7101 + */
7102 +
7103 +/*
7104 + * debug print functions
7105 + */
7106 +
7107 +#include "aufs.h"
7108 +
7109 +/* Returns 0, or -errno.  arg is in kp->arg. */
7110 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7111 +{
7112 +       int err, n;
7113 +
7114 +       err = kstrtoint(val, 0, &n);
7115 +       if (!err) {
7116 +               if (n > 0)
7117 +                       au_debug_on();
7118 +               else
7119 +                       au_debug_off();
7120 +       }
7121 +       return err;
7122 +}
7123 +
7124 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7125 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7126 +{
7127 +       atomic_t *a;
7128 +
7129 +       a = kp->arg;
7130 +       return sprintf(buffer, "%d", atomic_read(a));
7131 +}
7132 +
7133 +static struct kernel_param_ops param_ops_atomic_t = {
7134 +       .set = param_atomic_t_set,
7135 +       .get = param_atomic_t_get
7136 +       /* void (*free)(void *arg) */
7137 +};
7138 +
7139 +atomic_t aufs_debug = ATOMIC_INIT(0);
7140 +MODULE_PARM_DESC(debug, "debug print");
7141 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
7142 +
7143 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7144 +char *au_plevel = KERN_DEBUG;
7145 +#define dpri(fmt, ...) do {                                    \
7146 +       if ((au_plevel                                          \
7147 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7148 +           || au_debug_test())                                 \
7149 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7150 +} while (0)
7151 +
7152 +/* ---------------------------------------------------------------------- */
7153 +
7154 +void au_dpri_whlist(struct au_nhash *whlist)
7155 +{
7156 +       unsigned long ul, n;
7157 +       struct hlist_head *head;
7158 +       struct au_vdir_wh *pos;
7159 +
7160 +       n = whlist->nh_num;
7161 +       head = whlist->nh_head;
7162 +       for (ul = 0; ul < n; ul++) {
7163 +               hlist_for_each_entry(pos, head, wh_hash)
7164 +                       dpri("b%d, %.*s, %d\n",
7165 +                            pos->wh_bindex,
7166 +                            pos->wh_str.len, pos->wh_str.name,
7167 +                            pos->wh_str.len);
7168 +               head++;
7169 +       }
7170 +}
7171 +
7172 +void au_dpri_vdir(struct au_vdir *vdir)
7173 +{
7174 +       unsigned long ul;
7175 +       union au_vdir_deblk_p p;
7176 +       unsigned char *o;
7177 +
7178 +       if (!vdir || IS_ERR(vdir)) {
7179 +               dpri("err %ld\n", PTR_ERR(vdir));
7180 +               return;
7181 +       }
7182 +
7183 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
7184 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7185 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7186 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7187 +               p.deblk = vdir->vd_deblk[ul];
7188 +               o = p.deblk;
7189 +               dpri("[%lu]: %p\n", ul, o);
7190 +       }
7191 +}
7192 +
7193 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7194 +                       struct dentry *wh)
7195 +{
7196 +       char *n = NULL;
7197 +       int l = 0;
7198 +
7199 +       if (!inode || IS_ERR(inode)) {
7200 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7201 +               return -1;
7202 +       }
7203 +
7204 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7205 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7206 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7207 +       if (wh) {
7208 +               n = (void *)wh->d_name.name;
7209 +               l = wh->d_name.len;
7210 +       }
7211 +
7212 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7213 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7214 +            bindex, inode,
7215 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7216 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7217 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7218 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
7219 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7220 +            inode->i_state, inode->i_flags, inode->i_version,
7221 +            inode->i_generation,
7222 +            l ? ", wh " : "", l, n);
7223 +       return 0;
7224 +}
7225 +
7226 +void au_dpri_inode(struct inode *inode)
7227 +{
7228 +       struct au_iinfo *iinfo;
7229 +       struct au_hinode *hi;
7230 +       aufs_bindex_t bindex;
7231 +       int err, hn;
7232 +
7233 +       err = do_pri_inode(-1, inode, -1, NULL);
7234 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7235 +               return;
7236 +
7237 +       iinfo = au_ii(inode);
7238 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7239 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7240 +       if (iinfo->ii_btop < 0)
7241 +               return;
7242 +       hn = 0;
7243 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7244 +               hi = au_hinode(iinfo, bindex);
7245 +               hn = !!au_hn(hi);
7246 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7247 +       }
7248 +}
7249 +
7250 +void au_dpri_dalias(struct inode *inode)
7251 +{
7252 +       struct dentry *d;
7253 +
7254 +       spin_lock(&inode->i_lock);
7255 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7256 +               au_dpri_dentry(d);
7257 +       spin_unlock(&inode->i_lock);
7258 +}
7259 +
7260 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7261 +{
7262 +       struct dentry *wh = NULL;
7263 +       int hn;
7264 +       struct inode *inode;
7265 +       struct au_iinfo *iinfo;
7266 +       struct au_hinode *hi;
7267 +
7268 +       if (!dentry || IS_ERR(dentry)) {
7269 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7270 +               return -1;
7271 +       }
7272 +       /* do not call dget_parent() here */
7273 +       /* note: access d_xxx without d_lock */
7274 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7275 +            bindex, dentry, dentry,
7276 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7277 +            au_dcount(dentry), dentry->d_flags,
7278 +            d_unhashed(dentry) ? "un" : "");
7279 +       hn = -1;
7280 +       inode = NULL;
7281 +       if (d_is_positive(dentry))
7282 +               inode = d_inode(dentry);
7283 +       if (inode
7284 +           && au_test_aufs(dentry->d_sb)
7285 +           && bindex >= 0
7286 +           && !au_is_bad_inode(inode)) {
7287 +               iinfo = au_ii(inode);
7288 +               hi = au_hinode(iinfo, bindex);
7289 +               hn = !!au_hn(hi);
7290 +               wh = hi->hi_whdentry;
7291 +       }
7292 +       do_pri_inode(bindex, inode, hn, wh);
7293 +       return 0;
7294 +}
7295 +
7296 +void au_dpri_dentry(struct dentry *dentry)
7297 +{
7298 +       struct au_dinfo *dinfo;
7299 +       aufs_bindex_t bindex;
7300 +       int err;
7301 +
7302 +       err = do_pri_dentry(-1, dentry);
7303 +       if (err || !au_test_aufs(dentry->d_sb))
7304 +               return;
7305 +
7306 +       dinfo = au_di(dentry);
7307 +       if (!dinfo)
7308 +               return;
7309 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7310 +            dinfo->di_btop, dinfo->di_bbot,
7311 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7312 +            dinfo->di_tmpfile);
7313 +       if (dinfo->di_btop < 0)
7314 +               return;
7315 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7316 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7317 +}
7318 +
7319 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7320 +{
7321 +       char a[32];
7322 +
7323 +       if (!file || IS_ERR(file)) {
7324 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7325 +               return -1;
7326 +       }
7327 +       a[0] = 0;
7328 +       if (bindex < 0
7329 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7330 +           && au_test_aufs(file->f_path.dentry->d_sb)
7331 +           && au_fi(file))
7332 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7333 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7334 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7335 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7336 +            file->f_version, file->f_pos, a);
7337 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7338 +               do_pri_dentry(bindex, file->f_path.dentry);
7339 +       return 0;
7340 +}
7341 +
7342 +void au_dpri_file(struct file *file)
7343 +{
7344 +       struct au_finfo *finfo;
7345 +       struct au_fidir *fidir;
7346 +       struct au_hfile *hfile;
7347 +       aufs_bindex_t bindex;
7348 +       int err;
7349 +
7350 +       err = do_pri_file(-1, file);
7351 +       if (err
7352 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7353 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7354 +               return;
7355 +
7356 +       finfo = au_fi(file);
7357 +       if (!finfo)
7358 +               return;
7359 +       if (finfo->fi_btop < 0)
7360 +               return;
7361 +       fidir = finfo->fi_hdir;
7362 +       if (!fidir)
7363 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7364 +       else
7365 +               for (bindex = finfo->fi_btop;
7366 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7367 +                    bindex++) {
7368 +                       hfile = fidir->fd_hfile + bindex;
7369 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7370 +               }
7371 +}
7372 +
7373 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7374 +{
7375 +       struct vfsmount *mnt;
7376 +       struct super_block *sb;
7377 +
7378 +       if (!br || IS_ERR(br))
7379 +               goto out;
7380 +       mnt = au_br_mnt(br);
7381 +       if (!mnt || IS_ERR(mnt))
7382 +               goto out;
7383 +       sb = mnt->mnt_sb;
7384 +       if (!sb || IS_ERR(sb))
7385 +               goto out;
7386 +
7387 +       dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
7388 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7389 +            "xino %d\n",
7390 +            bindex, br->br_perm, br->br_id, au_br_count(br),
7391 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7392 +            sb->s_flags, sb->s_count,
7393 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7394 +       return 0;
7395 +
7396 +out:
7397 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7398 +       return -1;
7399 +}
7400 +
7401 +void au_dpri_sb(struct super_block *sb)
7402 +{
7403 +       struct au_sbinfo *sbinfo;
7404 +       aufs_bindex_t bindex;
7405 +       int err;
7406 +       /* to reuduce stack size */
7407 +       struct {
7408 +               struct vfsmount mnt;
7409 +               struct au_branch fake;
7410 +       } *a;
7411 +
7412 +       /* this function can be called from magic sysrq */
7413 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7414 +       if (unlikely(!a)) {
7415 +               dpri("no memory\n");
7416 +               return;
7417 +       }
7418 +
7419 +       a->mnt.mnt_sb = sb;
7420 +       a->fake.br_path.mnt = &a->mnt;
7421 +       au_br_count_init(&a->fake);
7422 +       err = do_pri_br(-1, &a->fake);
7423 +       au_br_count_fin(&a->fake);
7424 +       kfree(a);
7425 +       dpri("dev 0x%x\n", sb->s_dev);
7426 +       if (err || !au_test_aufs(sb))
7427 +               return;
7428 +
7429 +       sbinfo = au_sbi(sb);
7430 +       if (!sbinfo)
7431 +               return;
7432 +       dpri("nw %d, gen %u, kobj %d\n",
7433 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7434 +            kref_read(&sbinfo->si_kobj.kref));
7435 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7436 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7437 +}
7438 +
7439 +/* ---------------------------------------------------------------------- */
7440 +
7441 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7442 +{
7443 +       struct inode *h_inode, *inode = d_inode(dentry);
7444 +       struct dentry *h_dentry;
7445 +       aufs_bindex_t bindex, bbot, bi;
7446 +
7447 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7448 +               return;
7449 +
7450 +       bbot = au_dbbot(dentry);
7451 +       bi = au_ibbot(inode);
7452 +       if (bi < bbot)
7453 +               bbot = bi;
7454 +       bindex = au_dbtop(dentry);
7455 +       bi = au_ibtop(inode);
7456 +       if (bi > bindex)
7457 +               bindex = bi;
7458 +
7459 +       for (; bindex <= bbot; bindex++) {
7460 +               h_dentry = au_h_dptr(dentry, bindex);
7461 +               if (!h_dentry)
7462 +                       continue;
7463 +               h_inode = au_h_iptr(inode, bindex);
7464 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7465 +                       au_debug_on();
7466 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7467 +                       AuDbgDentry(dentry);
7468 +                       AuDbgInode(inode);
7469 +                       au_debug_off();
7470 +                       BUG();
7471 +               }
7472 +       }
7473 +}
7474 +
7475 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7476 +{
7477 +       int err, i, j;
7478 +       struct au_dcsub_pages dpages;
7479 +       struct au_dpage *dpage;
7480 +       struct dentry **dentries;
7481 +
7482 +       err = au_dpages_init(&dpages, GFP_NOFS);
7483 +       AuDebugOn(err);
7484 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7485 +       AuDebugOn(err);
7486 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7487 +               dpage = dpages.dpages + i;
7488 +               dentries = dpage->dentries;
7489 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7490 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7491 +       }
7492 +       au_dpages_free(&dpages);
7493 +}
7494 +
7495 +void au_dbg_verify_kthread(void)
7496 +{
7497 +       if (au_wkq_test()) {
7498 +               au_dbg_blocked();
7499 +               /*
7500 +                * It may be recursive, but udba=notify between two aufs mounts,
7501 +                * where a single ro branch is shared, is not a problem.
7502 +                */
7503 +               /* WARN_ON(1); */
7504 +       }
7505 +}
7506 +
7507 +/* ---------------------------------------------------------------------- */
7508 +
7509 +int __init au_debug_init(void)
7510 +{
7511 +       aufs_bindex_t bindex;
7512 +       struct au_vdir_destr destr;
7513 +
7514 +       bindex = -1;
7515 +       AuDebugOn(bindex >= 0);
7516 +
7517 +       destr.len = -1;
7518 +       AuDebugOn(destr.len < NAME_MAX);
7519 +
7520 +#ifdef CONFIG_4KSTACKS
7521 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7522 +#endif
7523 +
7524 +       return 0;
7525 +}
7526 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7527 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7528 +++ linux/fs/aufs/debug.h       2017-07-29 12:14:25.899708630 +0200
7529 @@ -0,0 +1,225 @@
7530 +/*
7531 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7532 + *
7533 + * This program, aufs is free software; you can redistribute it and/or modify
7534 + * it under the terms of the GNU General Public License as published by
7535 + * the Free Software Foundation; either version 2 of the License, or
7536 + * (at your option) any later version.
7537 + *
7538 + * This program is distributed in the hope that it will be useful,
7539 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7540 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7541 + * GNU General Public License for more details.
7542 + *
7543 + * You should have received a copy of the GNU General Public License
7544 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7545 + */
7546 +
7547 +/*
7548 + * debug print functions
7549 + */
7550 +
7551 +#ifndef __AUFS_DEBUG_H__
7552 +#define __AUFS_DEBUG_H__
7553 +
7554 +#ifdef __KERNEL__
7555 +
7556 +#include <linux/atomic.h>
7557 +#include <linux/module.h>
7558 +#include <linux/kallsyms.h>
7559 +#include <linux/sysrq.h>
7560 +
7561 +#ifdef CONFIG_AUFS_DEBUG
7562 +#define AuDebugOn(a)           BUG_ON(a)
7563 +
7564 +/* module parameter */
7565 +extern atomic_t aufs_debug;
7566 +static inline void au_debug_on(void)
7567 +{
7568 +       atomic_inc(&aufs_debug);
7569 +}
7570 +static inline void au_debug_off(void)
7571 +{
7572 +       atomic_dec_if_positive(&aufs_debug);
7573 +}
7574 +
7575 +static inline int au_debug_test(void)
7576 +{
7577 +       return atomic_read(&aufs_debug) > 0;
7578 +}
7579 +#else
7580 +#define AuDebugOn(a)           do {} while (0)
7581 +AuStubVoid(au_debug_on, void)
7582 +AuStubVoid(au_debug_off, void)
7583 +AuStubInt0(au_debug_test, void)
7584 +#endif /* CONFIG_AUFS_DEBUG */
7585 +
7586 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7587 +
7588 +/* ---------------------------------------------------------------------- */
7589 +
7590 +/* debug print */
7591 +
7592 +#define AuDbg(fmt, ...) do { \
7593 +       if (au_debug_test()) \
7594 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7595 +} while (0)
7596 +#define AuLabel(l)             AuDbg(#l "\n")
7597 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7598 +#define AuWarn1(fmt, ...) do { \
7599 +       static unsigned char _c; \
7600 +       if (!_c++) \
7601 +               pr_warn(fmt, ##__VA_ARGS__); \
7602 +} while (0)
7603 +
7604 +#define AuErr1(fmt, ...) do { \
7605 +       static unsigned char _c; \
7606 +       if (!_c++) \
7607 +               pr_err(fmt, ##__VA_ARGS__); \
7608 +} while (0)
7609 +
7610 +#define AuIOErr1(fmt, ...) do { \
7611 +       static unsigned char _c; \
7612 +       if (!_c++) \
7613 +               AuIOErr(fmt, ##__VA_ARGS__); \
7614 +} while (0)
7615 +
7616 +#define AuUnsupportMsg "This operation is not supported." \
7617 +                       " Please report this application to aufs-users ML."
7618 +#define AuUnsupport(fmt, ...) do { \
7619 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7620 +       dump_stack(); \
7621 +} while (0)
7622 +
7623 +#define AuTraceErr(e) do { \
7624 +       if (unlikely((e) < 0)) \
7625 +               AuDbg("err %d\n", (int)(e)); \
7626 +} while (0)
7627 +
7628 +#define AuTraceErrPtr(p) do { \
7629 +       if (IS_ERR(p)) \
7630 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7631 +} while (0)
7632 +
7633 +/* dirty macros for debug print, use with "%.*s" and caution */
7634 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7635 +
7636 +/* ---------------------------------------------------------------------- */
7637 +
7638 +struct dentry;
7639 +#ifdef CONFIG_AUFS_DEBUG
7640 +extern struct mutex au_dbg_mtx;
7641 +extern char *au_plevel;
7642 +struct au_nhash;
7643 +void au_dpri_whlist(struct au_nhash *whlist);
7644 +struct au_vdir;
7645 +void au_dpri_vdir(struct au_vdir *vdir);
7646 +struct inode;
7647 +void au_dpri_inode(struct inode *inode);
7648 +void au_dpri_dalias(struct inode *inode);
7649 +void au_dpri_dentry(struct dentry *dentry);
7650 +struct file;
7651 +void au_dpri_file(struct file *filp);
7652 +struct super_block;
7653 +void au_dpri_sb(struct super_block *sb);
7654 +
7655 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7656 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7657 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7658 +void au_dbg_verify_kthread(void);
7659 +
7660 +int __init au_debug_init(void);
7661 +
7662 +#define AuDbgWhlist(w) do { \
7663 +       mutex_lock(&au_dbg_mtx); \
7664 +       AuDbg(#w "\n"); \
7665 +       au_dpri_whlist(w); \
7666 +       mutex_unlock(&au_dbg_mtx); \
7667 +} while (0)
7668 +
7669 +#define AuDbgVdir(v) do { \
7670 +       mutex_lock(&au_dbg_mtx); \
7671 +       AuDbg(#v "\n"); \
7672 +       au_dpri_vdir(v); \
7673 +       mutex_unlock(&au_dbg_mtx); \
7674 +} while (0)
7675 +
7676 +#define AuDbgInode(i) do { \
7677 +       mutex_lock(&au_dbg_mtx); \
7678 +       AuDbg(#i "\n"); \
7679 +       au_dpri_inode(i); \
7680 +       mutex_unlock(&au_dbg_mtx); \
7681 +} while (0)
7682 +
7683 +#define AuDbgDAlias(i) do { \
7684 +       mutex_lock(&au_dbg_mtx); \
7685 +       AuDbg(#i "\n"); \
7686 +       au_dpri_dalias(i); \
7687 +       mutex_unlock(&au_dbg_mtx); \
7688 +} while (0)
7689 +
7690 +#define AuDbgDentry(d) do { \
7691 +       mutex_lock(&au_dbg_mtx); \
7692 +       AuDbg(#d "\n"); \
7693 +       au_dpri_dentry(d); \
7694 +       mutex_unlock(&au_dbg_mtx); \
7695 +} while (0)
7696 +
7697 +#define AuDbgFile(f) do { \
7698 +       mutex_lock(&au_dbg_mtx); \
7699 +       AuDbg(#f "\n"); \
7700 +       au_dpri_file(f); \
7701 +       mutex_unlock(&au_dbg_mtx); \
7702 +} while (0)
7703 +
7704 +#define AuDbgSb(sb) do { \
7705 +       mutex_lock(&au_dbg_mtx); \
7706 +       AuDbg(#sb "\n"); \
7707 +       au_dpri_sb(sb); \
7708 +       mutex_unlock(&au_dbg_mtx); \
7709 +} while (0)
7710 +
7711 +#define AuDbgSym(addr) do {                            \
7712 +       char sym[KSYM_SYMBOL_LEN];                      \
7713 +       sprint_symbol(sym, (unsigned long)addr);        \
7714 +       AuDbg("%s\n", sym);                             \
7715 +} while (0)
7716 +#else
7717 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7718 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7719 +AuStubVoid(au_dbg_verify_kthread, void)
7720 +AuStubInt0(__init au_debug_init, void)
7721 +
7722 +#define AuDbgWhlist(w)         do {} while (0)
7723 +#define AuDbgVdir(v)           do {} while (0)
7724 +#define AuDbgInode(i)          do {} while (0)
7725 +#define AuDbgDAlias(i)         do {} while (0)
7726 +#define AuDbgDentry(d)         do {} while (0)
7727 +#define AuDbgFile(f)           do {} while (0)
7728 +#define AuDbgSb(sb)            do {} while (0)
7729 +#define AuDbgSym(addr)         do {} while (0)
7730 +#endif /* CONFIG_AUFS_DEBUG */
7731 +
7732 +/* ---------------------------------------------------------------------- */
7733 +
7734 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7735 +int __init au_sysrq_init(void);
7736 +void au_sysrq_fin(void);
7737 +
7738 +#ifdef CONFIG_HW_CONSOLE
7739 +#define au_dbg_blocked() do { \
7740 +       WARN_ON(1); \
7741 +       handle_sysrq('w'); \
7742 +} while (0)
7743 +#else
7744 +AuStubVoid(au_dbg_blocked, void)
7745 +#endif
7746 +
7747 +#else
7748 +AuStubInt0(__init au_sysrq_init, void)
7749 +AuStubVoid(au_sysrq_fin, void)
7750 +AuStubVoid(au_dbg_blocked, void)
7751 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7752 +
7753 +#endif /* __KERNEL__ */
7754 +#endif /* __AUFS_DEBUG_H__ */
7755 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7756 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7757 +++ linux/fs/aufs/dentry.c      2017-07-29 12:14:25.899708630 +0200
7758 @@ -0,0 +1,1130 @@
7759 +/*
7760 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7761 + *
7762 + * This program, aufs is free software; you can redistribute it and/or modify
7763 + * it under the terms of the GNU General Public License as published by
7764 + * the Free Software Foundation; either version 2 of the License, or
7765 + * (at your option) any later version.
7766 + *
7767 + * This program is distributed in the hope that it will be useful,
7768 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7769 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7770 + * GNU General Public License for more details.
7771 + *
7772 + * You should have received a copy of the GNU General Public License
7773 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7774 + */
7775 +
7776 +/*
7777 + * lookup and dentry operations
7778 + */
7779 +
7780 +#include <linux/namei.h>
7781 +#include "aufs.h"
7782 +
7783 +struct au_do_lookup_args {
7784 +       unsigned int            flags;
7785 +       mode_t                  type;
7786 +};
7787 +
7788 +/*
7789 + * returns positive/negative dentry, NULL or an error.
7790 + * NULL means whiteout-ed or not-found.
7791 + */
7792 +static struct dentry*
7793 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7794 +            aufs_bindex_t bindex, struct qstr *wh_name,
7795 +            struct au_do_lookup_args *args)
7796 +{
7797 +       struct dentry *h_dentry;
7798 +       struct inode *h_inode;
7799 +       struct au_branch *br;
7800 +       int wh_found, opq;
7801 +       unsigned char wh_able;
7802 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7803 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7804 +                                                         IGNORE_PERM);
7805 +
7806 +       wh_found = 0;
7807 +       br = au_sbr(dentry->d_sb, bindex);
7808 +       wh_able = !!au_br_whable(br->br_perm);
7809 +       if (wh_able)
7810 +               wh_found = au_wh_test(h_parent, wh_name, ignore_perm);
7811 +       h_dentry = ERR_PTR(wh_found);
7812 +       if (!wh_found)
7813 +               goto real_lookup;
7814 +       if (unlikely(wh_found < 0))
7815 +               goto out;
7816 +
7817 +       /* We found a whiteout */
7818 +       /* au_set_dbbot(dentry, bindex); */
7819 +       au_set_dbwh(dentry, bindex);
7820 +       if (!allow_neg)
7821 +               return NULL; /* success */
7822 +
7823 +real_lookup:
7824 +       if (!ignore_perm)
7825 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7826 +       else
7827 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7828 +       if (IS_ERR(h_dentry)) {
7829 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7830 +                   && !allow_neg)
7831 +                       h_dentry = NULL;
7832 +               goto out;
7833 +       }
7834 +
7835 +       h_inode = d_inode(h_dentry);
7836 +       if (d_is_negative(h_dentry)) {
7837 +               if (!allow_neg)
7838 +                       goto out_neg;
7839 +       } else if (wh_found
7840 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7841 +               goto out_neg;
7842 +
7843 +       if (au_dbbot(dentry) <= bindex)
7844 +               au_set_dbbot(dentry, bindex);
7845 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
7846 +               au_set_dbtop(dentry, bindex);
7847 +       au_set_h_dptr(dentry, bindex, h_dentry);
7848 +
7849 +       if (!d_is_dir(h_dentry)
7850 +           || !wh_able
7851 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7852 +               goto out; /* success */
7853 +
7854 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
7855 +       opq = au_diropq_test(h_dentry);
7856 +       inode_unlock(h_inode);
7857 +       if (opq > 0)
7858 +               au_set_dbdiropq(dentry, bindex);
7859 +       else if (unlikely(opq < 0)) {
7860 +               au_set_h_dptr(dentry, bindex, NULL);
7861 +               h_dentry = ERR_PTR(opq);
7862 +       }
7863 +       goto out;
7864 +
7865 +out_neg:
7866 +       dput(h_dentry);
7867 +       h_dentry = NULL;
7868 +out:
7869 +       return h_dentry;
7870 +}
7871 +
7872 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7873 +{
7874 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7875 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7876 +               return -EPERM;
7877 +       return 0;
7878 +}
7879 +
7880 +/*
7881 + * returns the number of lower positive dentries,
7882 + * otherwise an error.
7883 + * can be called at unlinking with @type is zero.
7884 + */
7885 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
7886 +                  unsigned int flags)
7887 +{
7888 +       int npositive, err;
7889 +       aufs_bindex_t bindex, btail, bdiropq;
7890 +       unsigned char isdir, dirperm1;
7891 +       struct qstr whname;
7892 +       struct au_do_lookup_args args = {
7893 +               .flags          = flags
7894 +       };
7895 +       const struct qstr *name = &dentry->d_name;
7896 +       struct dentry *parent;
7897 +       struct super_block *sb;
7898 +
7899 +       sb = dentry->d_sb;
7900 +       err = au_test_shwh(sb, name);
7901 +       if (unlikely(err))
7902 +               goto out;
7903 +
7904 +       err = au_wh_name_alloc(&whname, name);
7905 +       if (unlikely(err))
7906 +               goto out;
7907 +
7908 +       isdir = !!d_is_dir(dentry);
7909 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7910 +
7911 +       npositive = 0;
7912 +       parent = dget_parent(dentry);
7913 +       btail = au_dbtaildir(parent);
7914 +       for (bindex = btop; bindex <= btail; bindex++) {
7915 +               struct dentry *h_parent, *h_dentry;
7916 +               struct inode *h_inode, *h_dir;
7917 +
7918 +               h_dentry = au_h_dptr(dentry, bindex);
7919 +               if (h_dentry) {
7920 +                       if (d_is_positive(h_dentry))
7921 +                               npositive++;
7922 +                       break;
7923 +               }
7924 +               h_parent = au_h_dptr(parent, bindex);
7925 +               if (!h_parent || !d_is_dir(h_parent))
7926 +                       continue;
7927 +
7928 +               h_dir = d_inode(h_parent);
7929 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
7930 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7931 +                                       &args);
7932 +               inode_unlock(h_dir);
7933 +               err = PTR_ERR(h_dentry);
7934 +               if (IS_ERR(h_dentry))
7935 +                       goto out_parent;
7936 +               if (h_dentry)
7937 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7938 +               if (dirperm1)
7939 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7940 +
7941 +               if (au_dbwh(dentry) == bindex)
7942 +                       break;
7943 +               if (!h_dentry)
7944 +                       continue;
7945 +               if (d_is_negative(h_dentry))
7946 +                       continue;
7947 +               h_inode = d_inode(h_dentry);
7948 +               npositive++;
7949 +               if (!args.type)
7950 +                       args.type = h_inode->i_mode & S_IFMT;
7951 +               if (args.type != S_IFDIR)
7952 +                       break;
7953 +               else if (isdir) {
7954 +                       /* the type of lower may be different */
7955 +                       bdiropq = au_dbdiropq(dentry);
7956 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7957 +                               break;
7958 +               }
7959 +       }
7960 +
7961 +       if (npositive) {
7962 +               AuLabel(positive);
7963 +               au_update_dbtop(dentry);
7964 +       }
7965 +       err = npositive;
7966 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7967 +                    && au_dbtop(dentry) < 0)) {
7968 +               err = -EIO;
7969 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7970 +                       dentry, err);
7971 +       }
7972 +
7973 +out_parent:
7974 +       dput(parent);
7975 +       kfree(whname.name);
7976 +out:
7977 +       return err;
7978 +}
7979 +
7980 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7981 +{
7982 +       struct dentry *dentry;
7983 +       int wkq_err;
7984 +
7985 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
7986 +               dentry = vfsub_lkup_one(name, parent);
7987 +       else {
7988 +               struct vfsub_lkup_one_args args = {
7989 +                       .errp   = &dentry,
7990 +                       .name   = name,
7991 +                       .parent = parent
7992 +               };
7993 +
7994 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7995 +               if (unlikely(wkq_err))
7996 +                       dentry = ERR_PTR(wkq_err);
7997 +       }
7998 +
7999 +       return dentry;
8000 +}
8001 +
8002 +/*
8003 + * lookup @dentry on @bindex which should be negative.
8004 + */
8005 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8006 +{
8007 +       int err;
8008 +       struct dentry *parent, *h_parent, *h_dentry;
8009 +       struct au_branch *br;
8010 +
8011 +       parent = dget_parent(dentry);
8012 +       h_parent = au_h_dptr(parent, bindex);
8013 +       br = au_sbr(dentry->d_sb, bindex);
8014 +       if (wh)
8015 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8016 +       else
8017 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8018 +       err = PTR_ERR(h_dentry);
8019 +       if (IS_ERR(h_dentry))
8020 +               goto out;
8021 +       if (unlikely(d_is_positive(h_dentry))) {
8022 +               err = -EIO;
8023 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8024 +               dput(h_dentry);
8025 +               goto out;
8026 +       }
8027 +
8028 +       err = 0;
8029 +       if (bindex < au_dbtop(dentry))
8030 +               au_set_dbtop(dentry, bindex);
8031 +       if (au_dbbot(dentry) < bindex)
8032 +               au_set_dbbot(dentry, bindex);
8033 +       au_set_h_dptr(dentry, bindex, h_dentry);
8034 +
8035 +out:
8036 +       dput(parent);
8037 +       return err;
8038 +}
8039 +
8040 +/* ---------------------------------------------------------------------- */
8041 +
8042 +/* subset of struct inode */
8043 +struct au_iattr {
8044 +       unsigned long           i_ino;
8045 +       /* unsigned int         i_nlink; */
8046 +       kuid_t                  i_uid;
8047 +       kgid_t                  i_gid;
8048 +       u64                     i_version;
8049 +/*
8050 +       loff_t                  i_size;
8051 +       blkcnt_t                i_blocks;
8052 +*/
8053 +       umode_t                 i_mode;
8054 +};
8055 +
8056 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8057 +{
8058 +       ia->i_ino = h_inode->i_ino;
8059 +       /* ia->i_nlink = h_inode->i_nlink; */
8060 +       ia->i_uid = h_inode->i_uid;
8061 +       ia->i_gid = h_inode->i_gid;
8062 +       ia->i_version = h_inode->i_version;
8063 +/*
8064 +       ia->i_size = h_inode->i_size;
8065 +       ia->i_blocks = h_inode->i_blocks;
8066 +*/
8067 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8068 +}
8069 +
8070 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8071 +{
8072 +       return ia->i_ino != h_inode->i_ino
8073 +               /* || ia->i_nlink != h_inode->i_nlink */
8074 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8075 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8076 +               || ia->i_version != h_inode->i_version
8077 +/*
8078 +               || ia->i_size != h_inode->i_size
8079 +               || ia->i_blocks != h_inode->i_blocks
8080 +*/
8081 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8082 +}
8083 +
8084 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8085 +                             struct au_branch *br)
8086 +{
8087 +       int err;
8088 +       struct au_iattr ia;
8089 +       struct inode *h_inode;
8090 +       struct dentry *h_d;
8091 +       struct super_block *h_sb;
8092 +
8093 +       err = 0;
8094 +       memset(&ia, -1, sizeof(ia));
8095 +       h_sb = h_dentry->d_sb;
8096 +       h_inode = NULL;
8097 +       if (d_is_positive(h_dentry)) {
8098 +               h_inode = d_inode(h_dentry);
8099 +               au_iattr_save(&ia, h_inode);
8100 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8101 +               /* nfs d_revalidate may return 0 for negative dentry */
8102 +               /* fuse d_revalidate always return 0 for negative dentry */
8103 +               goto out;
8104 +
8105 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8106 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8107 +       err = PTR_ERR(h_d);
8108 +       if (IS_ERR(h_d))
8109 +               goto out;
8110 +
8111 +       err = 0;
8112 +       if (unlikely(h_d != h_dentry
8113 +                    || d_inode(h_d) != h_inode
8114 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8115 +               err = au_busy_or_stale();
8116 +       dput(h_d);
8117 +
8118 +out:
8119 +       AuTraceErr(err);
8120 +       return err;
8121 +}
8122 +
8123 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8124 +               struct dentry *h_parent, struct au_branch *br)
8125 +{
8126 +       int err;
8127 +
8128 +       err = 0;
8129 +       if (udba == AuOpt_UDBA_REVAL
8130 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8131 +               IMustLock(h_dir);
8132 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8133 +       } else if (udba != AuOpt_UDBA_NONE)
8134 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8135 +
8136 +       return err;
8137 +}
8138 +
8139 +/* ---------------------------------------------------------------------- */
8140 +
8141 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8142 +{
8143 +       int err;
8144 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8145 +       struct au_hdentry tmp, *p, *q;
8146 +       struct au_dinfo *dinfo;
8147 +       struct super_block *sb;
8148 +
8149 +       DiMustWriteLock(dentry);
8150 +
8151 +       sb = dentry->d_sb;
8152 +       dinfo = au_di(dentry);
8153 +       bbot = dinfo->di_bbot;
8154 +       bwh = dinfo->di_bwh;
8155 +       bdiropq = dinfo->di_bdiropq;
8156 +       bindex = dinfo->di_btop;
8157 +       p = au_hdentry(dinfo, bindex);
8158 +       for (; bindex <= bbot; bindex++, p++) {
8159 +               if (!p->hd_dentry)
8160 +                       continue;
8161 +
8162 +               new_bindex = au_br_index(sb, p->hd_id);
8163 +               if (new_bindex == bindex)
8164 +                       continue;
8165 +
8166 +               if (dinfo->di_bwh == bindex)
8167 +                       bwh = new_bindex;
8168 +               if (dinfo->di_bdiropq == bindex)
8169 +                       bdiropq = new_bindex;
8170 +               if (new_bindex < 0) {
8171 +                       au_hdput(p);
8172 +                       p->hd_dentry = NULL;
8173 +                       continue;
8174 +               }
8175 +
8176 +               /* swap two lower dentries, and loop again */
8177 +               q = au_hdentry(dinfo, new_bindex);
8178 +               tmp = *q;
8179 +               *q = *p;
8180 +               *p = tmp;
8181 +               if (tmp.hd_dentry) {
8182 +                       bindex--;
8183 +                       p--;
8184 +               }
8185 +       }
8186 +
8187 +       dinfo->di_bwh = -1;
8188 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8189 +               dinfo->di_bwh = bwh;
8190 +
8191 +       dinfo->di_bdiropq = -1;
8192 +       if (bdiropq >= 0
8193 +           && bdiropq <= au_sbbot(sb)
8194 +           && au_sbr_whable(sb, bdiropq))
8195 +               dinfo->di_bdiropq = bdiropq;
8196 +
8197 +       err = -EIO;
8198 +       dinfo->di_btop = -1;
8199 +       dinfo->di_bbot = -1;
8200 +       bbot = au_dbbot(parent);
8201 +       bindex = 0;
8202 +       p = au_hdentry(dinfo, bindex);
8203 +       for (; bindex <= bbot; bindex++, p++)
8204 +               if (p->hd_dentry) {
8205 +                       dinfo->di_btop = bindex;
8206 +                       break;
8207 +               }
8208 +
8209 +       if (dinfo->di_btop >= 0) {
8210 +               bindex = bbot;
8211 +               p = au_hdentry(dinfo, bindex);
8212 +               for (; bindex >= 0; bindex--, p--)
8213 +                       if (p->hd_dentry) {
8214 +                               dinfo->di_bbot = bindex;
8215 +                               err = 0;
8216 +                               break;
8217 +                       }
8218 +       }
8219 +
8220 +       return err;
8221 +}
8222 +
8223 +static void au_do_hide(struct dentry *dentry)
8224 +{
8225 +       struct inode *inode;
8226 +
8227 +       if (d_really_is_positive(dentry)) {
8228 +               inode = d_inode(dentry);
8229 +               if (!d_is_dir(dentry)) {
8230 +                       if (inode->i_nlink && !d_unhashed(dentry))
8231 +                               drop_nlink(inode);
8232 +               } else {
8233 +                       clear_nlink(inode);
8234 +                       /* stop next lookup */
8235 +                       inode->i_flags |= S_DEAD;
8236 +               }
8237 +               smp_mb(); /* necessary? */
8238 +       }
8239 +       d_drop(dentry);
8240 +}
8241 +
8242 +static int au_hide_children(struct dentry *parent)
8243 +{
8244 +       int err, i, j, ndentry;
8245 +       struct au_dcsub_pages dpages;
8246 +       struct au_dpage *dpage;
8247 +       struct dentry *dentry;
8248 +
8249 +       err = au_dpages_init(&dpages, GFP_NOFS);
8250 +       if (unlikely(err))
8251 +               goto out;
8252 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8253 +       if (unlikely(err))
8254 +               goto out_dpages;
8255 +
8256 +       /* in reverse order */
8257 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8258 +               dpage = dpages.dpages + i;
8259 +               ndentry = dpage->ndentry;
8260 +               for (j = ndentry - 1; j >= 0; j--) {
8261 +                       dentry = dpage->dentries[j];
8262 +                       if (dentry != parent)
8263 +                               au_do_hide(dentry);
8264 +               }
8265 +       }
8266 +
8267 +out_dpages:
8268 +       au_dpages_free(&dpages);
8269 +out:
8270 +       return err;
8271 +}
8272 +
8273 +static void au_hide(struct dentry *dentry)
8274 +{
8275 +       int err;
8276 +
8277 +       AuDbgDentry(dentry);
8278 +       if (d_is_dir(dentry)) {
8279 +               /* shrink_dcache_parent(dentry); */
8280 +               err = au_hide_children(dentry);
8281 +               if (unlikely(err))
8282 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8283 +                               dentry, err);
8284 +       }
8285 +       au_do_hide(dentry);
8286 +}
8287 +
8288 +/*
8289 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8290 + *
8291 + * a dirty branch is added
8292 + * - on the top of layers
8293 + * - in the middle of layers
8294 + * - to the bottom of layers
8295 + *
8296 + * on the added branch there exists
8297 + * - a whiteout
8298 + * - a diropq
8299 + * - a same named entry
8300 + *   + exist
8301 + *     * negative --> positive
8302 + *     * positive --> positive
8303 + *      - type is unchanged
8304 + *      - type is changed
8305 + *   + doesn't exist
8306 + *     * negative --> negative
8307 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8308 + * - none
8309 + */
8310 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8311 +                              struct au_dinfo *tmp)
8312 +{
8313 +       int err;
8314 +       aufs_bindex_t bindex, bbot;
8315 +       struct {
8316 +               struct dentry *dentry;
8317 +               struct inode *inode;
8318 +               mode_t mode;
8319 +       } orig_h, tmp_h = {
8320 +               .dentry = NULL
8321 +       };
8322 +       struct au_hdentry *hd;
8323 +       struct inode *inode, *h_inode;
8324 +       struct dentry *h_dentry;
8325 +
8326 +       err = 0;
8327 +       AuDebugOn(dinfo->di_btop < 0);
8328 +       orig_h.mode = 0;
8329 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8330 +       orig_h.inode = NULL;
8331 +       if (d_is_positive(orig_h.dentry)) {
8332 +               orig_h.inode = d_inode(orig_h.dentry);
8333 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8334 +       }
8335 +       if (tmp->di_btop >= 0) {
8336 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8337 +               if (d_is_positive(tmp_h.dentry)) {
8338 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8339 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8340 +               }
8341 +       }
8342 +
8343 +       inode = NULL;
8344 +       if (d_really_is_positive(dentry))
8345 +               inode = d_inode(dentry);
8346 +       if (!orig_h.inode) {
8347 +               AuDbg("nagative originally\n");
8348 +               if (inode) {
8349 +                       au_hide(dentry);
8350 +                       goto out;
8351 +               }
8352 +               AuDebugOn(inode);
8353 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8354 +               AuDebugOn(dinfo->di_bdiropq != -1);
8355 +
8356 +               if (!tmp_h.inode) {
8357 +                       AuDbg("negative --> negative\n");
8358 +                       /* should have only one negative lower */
8359 +                       if (tmp->di_btop >= 0
8360 +                           && tmp->di_btop < dinfo->di_btop) {
8361 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8362 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8363 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8364 +                               au_di_cp(dinfo, tmp);
8365 +                               hd = au_hdentry(tmp, tmp->di_btop);
8366 +                               au_set_h_dptr(dentry, tmp->di_btop,
8367 +                                             dget(hd->hd_dentry));
8368 +                       }
8369 +                       au_dbg_verify_dinode(dentry);
8370 +               } else {
8371 +                       AuDbg("negative --> positive\n");
8372 +                       /*
8373 +                        * similar to the behaviour of creating with bypassing
8374 +                        * aufs.
8375 +                        * unhash it in order to force an error in the
8376 +                        * succeeding create operation.
8377 +                        * we should not set S_DEAD here.
8378 +                        */
8379 +                       d_drop(dentry);
8380 +                       /* au_di_swap(tmp, dinfo); */
8381 +                       au_dbg_verify_dinode(dentry);
8382 +               }
8383 +       } else {
8384 +               AuDbg("positive originally\n");
8385 +               /* inode may be NULL */
8386 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8387 +               if (!tmp_h.inode) {
8388 +                       AuDbg("positive --> negative\n");
8389 +                       /* or bypassing aufs */
8390 +                       au_hide(dentry);
8391 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8392 +                               dinfo->di_bwh = tmp->di_bwh;
8393 +                       if (inode)
8394 +                               err = au_refresh_hinode_self(inode);
8395 +                       au_dbg_verify_dinode(dentry);
8396 +               } else if (orig_h.mode == tmp_h.mode) {
8397 +                       AuDbg("positive --> positive, same type\n");
8398 +                       if (!S_ISDIR(orig_h.mode)
8399 +                           && dinfo->di_btop > tmp->di_btop) {
8400 +                               /*
8401 +                                * similar to the behaviour of removing and
8402 +                                * creating.
8403 +                                */
8404 +                               au_hide(dentry);
8405 +                               if (inode)
8406 +                                       err = au_refresh_hinode_self(inode);
8407 +                               au_dbg_verify_dinode(dentry);
8408 +                       } else {
8409 +                               /* fill empty slots */
8410 +                               if (dinfo->di_btop > tmp->di_btop)
8411 +                                       dinfo->di_btop = tmp->di_btop;
8412 +                               if (dinfo->di_bbot < tmp->di_bbot)
8413 +                                       dinfo->di_bbot = tmp->di_bbot;
8414 +                               dinfo->di_bwh = tmp->di_bwh;
8415 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8416 +                               bbot = dinfo->di_bbot;
8417 +                               bindex = tmp->di_btop;
8418 +                               hd = au_hdentry(tmp, bindex);
8419 +                               for (; bindex <= bbot; bindex++, hd++) {
8420 +                                       if (au_h_dptr(dentry, bindex))
8421 +                                               continue;
8422 +                                       h_dentry = hd->hd_dentry;
8423 +                                       if (!h_dentry)
8424 +                                               continue;
8425 +                                       AuDebugOn(d_is_negative(h_dentry));
8426 +                                       h_inode = d_inode(h_dentry);
8427 +                                       AuDebugOn(orig_h.mode
8428 +                                                 != (h_inode->i_mode
8429 +                                                     & S_IFMT));
8430 +                                       au_set_h_dptr(dentry, bindex,
8431 +                                                     dget(h_dentry));
8432 +                               }
8433 +                               if (inode)
8434 +                                       err = au_refresh_hinode(inode, dentry);
8435 +                               au_dbg_verify_dinode(dentry);
8436 +                       }
8437 +               } else {
8438 +                       AuDbg("positive --> positive, different type\n");
8439 +                       /* similar to the behaviour of removing and creating */
8440 +                       au_hide(dentry);
8441 +                       if (inode)
8442 +                               err = au_refresh_hinode_self(inode);
8443 +                       au_dbg_verify_dinode(dentry);
8444 +               }
8445 +       }
8446 +
8447 +out:
8448 +       return err;
8449 +}
8450 +
8451 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8452 +{
8453 +       const struct dentry_operations *dop
8454 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8455 +       static const unsigned int mask
8456 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8457 +
8458 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8459 +
8460 +       if (dentry->d_op == dop)
8461 +               return;
8462 +
8463 +       AuDbg("%pd\n", dentry);
8464 +       spin_lock(&dentry->d_lock);
8465 +       if (dop == &aufs_dop)
8466 +               dentry->d_flags |= mask;
8467 +       else
8468 +               dentry->d_flags &= ~mask;
8469 +       dentry->d_op = dop;
8470 +       spin_unlock(&dentry->d_lock);
8471 +}
8472 +
8473 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8474 +{
8475 +       int err, ebrange, nbr;
8476 +       unsigned int sigen;
8477 +       struct au_dinfo *dinfo, *tmp;
8478 +       struct super_block *sb;
8479 +       struct inode *inode;
8480 +
8481 +       DiMustWriteLock(dentry);
8482 +       AuDebugOn(IS_ROOT(dentry));
8483 +       AuDebugOn(d_really_is_negative(parent));
8484 +
8485 +       sb = dentry->d_sb;
8486 +       sigen = au_sigen(sb);
8487 +       err = au_digen_test(parent, sigen);
8488 +       if (unlikely(err))
8489 +               goto out;
8490 +
8491 +       nbr = au_sbbot(sb) + 1;
8492 +       dinfo = au_di(dentry);
8493 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8494 +       if (unlikely(err))
8495 +               goto out;
8496 +       ebrange = au_dbrange_test(dentry);
8497 +       if (!ebrange)
8498 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8499 +
8500 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8501 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8502 +               if (d_really_is_positive(dentry)) {
8503 +                       inode = d_inode(dentry);
8504 +                       err = au_refresh_hinode_self(inode);
8505 +               }
8506 +               au_dbg_verify_dinode(dentry);
8507 +               if (!err)
8508 +                       goto out_dgen; /* success */
8509 +               goto out;
8510 +       }
8511 +
8512 +       /* temporary dinfo */
8513 +       AuDbgDentry(dentry);
8514 +       err = -ENOMEM;
8515 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8516 +       if (unlikely(!tmp))
8517 +               goto out;
8518 +       au_di_swap(tmp, dinfo);
8519 +       /* returns the number of positive dentries */
8520 +       /*
8521 +        * if current working dir is removed, it returns an error.
8522 +        * but the dentry is legal.
8523 +        */
8524 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8525 +       AuDbgDentry(dentry);
8526 +       au_di_swap(tmp, dinfo);
8527 +       if (err == -ENOENT)
8528 +               err = 0;
8529 +       if (err >= 0) {
8530 +               /* compare/refresh by dinfo */
8531 +               AuDbgDentry(dentry);
8532 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8533 +               au_dbg_verify_dinode(dentry);
8534 +               AuTraceErr(err);
8535 +       }
8536 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8537 +       au_rw_write_unlock(&tmp->di_rwsem);
8538 +       au_di_free(tmp);
8539 +       if (unlikely(err))
8540 +               goto out;
8541 +
8542 +out_dgen:
8543 +       au_update_digen(dentry);
8544 +out:
8545 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8546 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8547 +               AuDbgDentry(dentry);
8548 +       }
8549 +       AuTraceErr(err);
8550 +       return err;
8551 +}
8552 +
8553 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8554 +                          struct dentry *dentry, aufs_bindex_t bindex)
8555 +{
8556 +       int err, valid;
8557 +
8558 +       err = 0;
8559 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8560 +               goto out;
8561 +
8562 +       AuDbg("b%d\n", bindex);
8563 +       /*
8564 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8565 +        * due to whiteout and branch permission.
8566 +        */
8567 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8568 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8569 +       /* it may return tri-state */
8570 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8571 +
8572 +       if (unlikely(valid < 0))
8573 +               err = valid;
8574 +       else if (!valid)
8575 +               err = -EINVAL;
8576 +
8577 +out:
8578 +       AuTraceErr(err);
8579 +       return err;
8580 +}
8581 +
8582 +/* todo: remove this */
8583 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8584 +                         unsigned int flags, int do_udba)
8585 +{
8586 +       int err;
8587 +       umode_t mode, h_mode;
8588 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8589 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8590 +       struct inode *h_inode, *h_cached_inode;
8591 +       struct dentry *h_dentry;
8592 +       struct qstr *name, *h_name;
8593 +
8594 +       err = 0;
8595 +       plus = 0;
8596 +       mode = 0;
8597 +       ibs = -1;
8598 +       ibe = -1;
8599 +       unhashed = !!d_unhashed(dentry);
8600 +       is_root = !!IS_ROOT(dentry);
8601 +       name = &dentry->d_name;
8602 +       tmpfile = au_di(dentry)->di_tmpfile;
8603 +
8604 +       /*
8605 +        * Theoretically, REVAL test should be unnecessary in case of
8606 +        * {FS,I}NOTIFY.
8607 +        * But {fs,i}notify doesn't fire some necessary events,
8608 +        *      IN_ATTRIB for atime/nlink/pageio
8609 +        * Let's do REVAL test too.
8610 +        */
8611 +       if (do_udba && inode) {
8612 +               mode = (inode->i_mode & S_IFMT);
8613 +               plus = (inode->i_nlink > 0);
8614 +               ibs = au_ibtop(inode);
8615 +               ibe = au_ibbot(inode);
8616 +       }
8617 +
8618 +       btop = au_dbtop(dentry);
8619 +       btail = btop;
8620 +       if (inode && S_ISDIR(inode->i_mode))
8621 +               btail = au_dbtaildir(dentry);
8622 +       for (bindex = btop; bindex <= btail; bindex++) {
8623 +               h_dentry = au_h_dptr(dentry, bindex);
8624 +               if (!h_dentry)
8625 +                       continue;
8626 +
8627 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8628 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8629 +               spin_lock(&h_dentry->d_lock);
8630 +               h_name = &h_dentry->d_name;
8631 +               if (unlikely(do_udba
8632 +                            && !is_root
8633 +                            && ((!h_nfs
8634 +                                 && (unhashed != !!d_unhashed(h_dentry)
8635 +                                     || (!tmpfile
8636 +                                         && !au_qstreq(name, h_name))
8637 +                                         ))
8638 +                                || (h_nfs
8639 +                                    && !(flags & LOOKUP_OPEN)
8640 +                                    && (h_dentry->d_flags
8641 +                                        & DCACHE_NFSFS_RENAMED)))
8642 +                           )) {
8643 +                       int h_unhashed;
8644 +
8645 +                       h_unhashed = d_unhashed(h_dentry);
8646 +                       spin_unlock(&h_dentry->d_lock);
8647 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8648 +                             unhashed, h_unhashed, dentry, h_dentry);
8649 +                       goto err;
8650 +               }
8651 +               spin_unlock(&h_dentry->d_lock);
8652 +
8653 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8654 +               if (unlikely(err))
8655 +                       /* do not goto err, to keep the errno */
8656 +                       break;
8657 +
8658 +               /* todo: plink too? */
8659 +               if (!do_udba)
8660 +                       continue;
8661 +
8662 +               /* UDBA tests */
8663 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8664 +                       goto err;
8665 +
8666 +               h_inode = NULL;
8667 +               if (d_is_positive(h_dentry))
8668 +                       h_inode = d_inode(h_dentry);
8669 +               h_plus = plus;
8670 +               h_mode = mode;
8671 +               h_cached_inode = h_inode;
8672 +               if (h_inode) {
8673 +                       h_mode = (h_inode->i_mode & S_IFMT);
8674 +                       h_plus = (h_inode->i_nlink > 0);
8675 +               }
8676 +               if (inode && ibs <= bindex && bindex <= ibe)
8677 +                       h_cached_inode = au_h_iptr(inode, bindex);
8678 +
8679 +               if (!h_nfs) {
8680 +                       if (unlikely(plus != h_plus && !tmpfile))
8681 +                               goto err;
8682 +               } else {
8683 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8684 +                                    && !is_root
8685 +                                    && !IS_ROOT(h_dentry)
8686 +                                    && unhashed != d_unhashed(h_dentry)))
8687 +                               goto err;
8688 +               }
8689 +               if (unlikely(mode != h_mode
8690 +                            || h_cached_inode != h_inode))
8691 +                       goto err;
8692 +               continue;
8693 +
8694 +err:
8695 +               err = -EINVAL;
8696 +               break;
8697 +       }
8698 +
8699 +       AuTraceErr(err);
8700 +       return err;
8701 +}
8702 +
8703 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8704 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8705 +{
8706 +       int err;
8707 +       struct dentry *parent;
8708 +
8709 +       if (!au_digen_test(dentry, sigen))
8710 +               return 0;
8711 +
8712 +       parent = dget_parent(dentry);
8713 +       di_read_lock_parent(parent, AuLock_IR);
8714 +       AuDebugOn(au_digen_test(parent, sigen));
8715 +       au_dbg_verify_gen(parent, sigen);
8716 +       err = au_refresh_dentry(dentry, parent);
8717 +       di_read_unlock(parent, AuLock_IR);
8718 +       dput(parent);
8719 +       AuTraceErr(err);
8720 +       return err;
8721 +}
8722 +
8723 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8724 +{
8725 +       int err;
8726 +       struct dentry *d, *parent;
8727 +
8728 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8729 +               return simple_reval_dpath(dentry, sigen);
8730 +
8731 +       /* slow loop, keep it simple and stupid */
8732 +       /* cf: au_cpup_dirs() */
8733 +       err = 0;
8734 +       parent = NULL;
8735 +       while (au_digen_test(dentry, sigen)) {
8736 +               d = dentry;
8737 +               while (1) {
8738 +                       dput(parent);
8739 +                       parent = dget_parent(d);
8740 +                       if (!au_digen_test(parent, sigen))
8741 +                               break;
8742 +                       d = parent;
8743 +               }
8744 +
8745 +               if (d != dentry)
8746 +                       di_write_lock_child2(d);
8747 +
8748 +               /* someone might update our dentry while we were sleeping */
8749 +               if (au_digen_test(d, sigen)) {
8750 +                       /*
8751 +                        * todo: consolidate with simple_reval_dpath(),
8752 +                        * do_refresh() and au_reval_for_attr().
8753 +                        */
8754 +                       di_read_lock_parent(parent, AuLock_IR);
8755 +                       err = au_refresh_dentry(d, parent);
8756 +                       di_read_unlock(parent, AuLock_IR);
8757 +               }
8758 +
8759 +               if (d != dentry)
8760 +                       di_write_unlock(d);
8761 +               dput(parent);
8762 +               if (unlikely(err))
8763 +                       break;
8764 +       }
8765 +
8766 +       return err;
8767 +}
8768 +
8769 +/*
8770 + * if valid returns 1, otherwise 0.
8771 + */
8772 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8773 +{
8774 +       int valid, err;
8775 +       unsigned int sigen;
8776 +       unsigned char do_udba;
8777 +       struct super_block *sb;
8778 +       struct inode *inode;
8779 +
8780 +       /* todo: support rcu-walk? */
8781 +       if (flags & LOOKUP_RCU)
8782 +               return -ECHILD;
8783 +
8784 +       valid = 0;
8785 +       if (unlikely(!au_di(dentry)))
8786 +               goto out;
8787 +
8788 +       valid = 1;
8789 +       sb = dentry->d_sb;
8790 +       /*
8791 +        * todo: very ugly
8792 +        * i_mutex of parent dir may be held,
8793 +        * but we should not return 'invalid' due to busy.
8794 +        */
8795 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8796 +       if (unlikely(err)) {
8797 +               valid = err;
8798 +               AuTraceErr(err);
8799 +               goto out;
8800 +       }
8801 +       inode = NULL;
8802 +       if (d_really_is_positive(dentry))
8803 +               inode = d_inode(dentry);
8804 +       if (unlikely(inode && au_is_bad_inode(inode))) {
8805 +               err = -EINVAL;
8806 +               AuTraceErr(err);
8807 +               goto out_dgrade;
8808 +       }
8809 +       if (unlikely(au_dbrange_test(dentry))) {
8810 +               err = -EINVAL;
8811 +               AuTraceErr(err);
8812 +               goto out_dgrade;
8813 +       }
8814 +
8815 +       sigen = au_sigen(sb);
8816 +       if (au_digen_test(dentry, sigen)) {
8817 +               AuDebugOn(IS_ROOT(dentry));
8818 +               err = au_reval_dpath(dentry, sigen);
8819 +               if (unlikely(err)) {
8820 +                       AuTraceErr(err);
8821 +                       goto out_dgrade;
8822 +               }
8823 +       }
8824 +       di_downgrade_lock(dentry, AuLock_IR);
8825 +
8826 +       err = -EINVAL;
8827 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8828 +           && inode
8829 +           && !(inode->i_state && I_LINKABLE)
8830 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8831 +               AuTraceErr(err);
8832 +               goto out_inval;
8833 +       }
8834 +
8835 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8836 +       if (do_udba && inode) {
8837 +               aufs_bindex_t btop = au_ibtop(inode);
8838 +               struct inode *h_inode;
8839 +
8840 +               if (btop >= 0) {
8841 +                       h_inode = au_h_iptr(inode, btop);
8842 +                       if (h_inode && au_test_higen(inode, h_inode)) {
8843 +                               AuTraceErr(err);
8844 +                               goto out_inval;
8845 +                       }
8846 +               }
8847 +       }
8848 +
8849 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8850 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
8851 +               err = -EIO;
8852 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8853 +                     dentry, err);
8854 +       }
8855 +       goto out_inval;
8856 +
8857 +out_dgrade:
8858 +       di_downgrade_lock(dentry, AuLock_IR);
8859 +out_inval:
8860 +       aufs_read_unlock(dentry, AuLock_IR);
8861 +       AuTraceErr(err);
8862 +       valid = !err;
8863 +out:
8864 +       if (!valid) {
8865 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8866 +               d_drop(dentry);
8867 +       }
8868 +       return valid;
8869 +}
8870 +
8871 +static void aufs_d_release(struct dentry *dentry)
8872 +{
8873 +       if (au_di(dentry)) {
8874 +               au_di_fin(dentry);
8875 +               au_hn_di_reinit(dentry);
8876 +       }
8877 +}
8878 +
8879 +const struct dentry_operations aufs_dop = {
8880 +       .d_revalidate           = aufs_d_revalidate,
8881 +       .d_weak_revalidate      = aufs_d_revalidate,
8882 +       .d_release              = aufs_d_release
8883 +};
8884 +
8885 +/* aufs_dop without d_revalidate */
8886 +const struct dentry_operations aufs_dop_noreval = {
8887 +       .d_release              = aufs_d_release
8888 +};
8889 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8890 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8891 +++ linux/fs/aufs/dentry.h      2017-07-29 12:14:25.899708630 +0200
8892 @@ -0,0 +1,252 @@
8893 +/*
8894 + * Copyright (C) 2005-2017 Junjiro R. Okajima
8895 + *
8896 + * This program, aufs is free software; you can redistribute it and/or modify
8897 + * it under the terms of the GNU General Public License as published by
8898 + * the Free Software Foundation; either version 2 of the License, or
8899 + * (at your option) any later version.
8900 + *
8901 + * This program is distributed in the hope that it will be useful,
8902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8904 + * GNU General Public License for more details.
8905 + *
8906 + * You should have received a copy of the GNU General Public License
8907 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8908 + */
8909 +
8910 +/*
8911 + * lookup and dentry operations
8912 + */
8913 +
8914 +#ifndef __AUFS_DENTRY_H__
8915 +#define __AUFS_DENTRY_H__
8916 +
8917 +#ifdef __KERNEL__
8918 +
8919 +#include <linux/dcache.h>
8920 +#include "rwsem.h"
8921 +
8922 +struct au_hdentry {
8923 +       struct dentry           *hd_dentry;
8924 +       aufs_bindex_t           hd_id;
8925 +};
8926 +
8927 +struct au_dinfo {
8928 +       atomic_t                di_generation;
8929 +
8930 +       struct au_rwsem         di_rwsem;
8931 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
8932 +       unsigned char           di_tmpfile; /* to allow the different name */
8933 +       struct au_hdentry       *di_hdentry;
8934 +} ____cacheline_aligned_in_smp;
8935 +
8936 +/* ---------------------------------------------------------------------- */
8937 +
8938 +/* flags for au_lkup_dentry() */
8939 +#define AuLkup_ALLOW_NEG       1
8940 +#define AuLkup_IGNORE_PERM     (1 << 1)
8941 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
8942 +#define au_fset_lkup(flags, name) \
8943 +       do { (flags) |= AuLkup_##name; } while (0)
8944 +#define au_fclr_lkup(flags, name) \
8945 +       do { (flags) &= ~AuLkup_##name; } while (0)
8946 +
8947 +/* ---------------------------------------------------------------------- */
8948 +
8949 +/* dentry.c */
8950 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
8951 +struct au_branch;
8952 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8953 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8954 +               struct dentry *h_parent, struct au_branch *br);
8955 +
8956 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8957 +                  unsigned int flags);
8958 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8959 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8960 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8961 +void au_refresh_dop(struct dentry *dentry, int force_reval);
8962 +
8963 +/* dinfo.c */
8964 +void au_di_init_once(void *_di);
8965 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8966 +void au_di_free(struct au_dinfo *dinfo);
8967 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8968 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8969 +int au_di_init(struct dentry *dentry);
8970 +void au_di_fin(struct dentry *dentry);
8971 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
8972 +
8973 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8974 +void di_read_unlock(struct dentry *d, int flags);
8975 +void di_downgrade_lock(struct dentry *d, int flags);
8976 +void di_write_lock(struct dentry *d, unsigned int lsc);
8977 +void di_write_unlock(struct dentry *d);
8978 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8979 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8980 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8981 +
8982 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8983 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8984 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8985 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8986 +
8987 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8988 +                  struct dentry *h_dentry);
8989 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8990 +int au_dbrange_test(struct dentry *dentry);
8991 +void au_update_digen(struct dentry *dentry);
8992 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8993 +void au_update_dbtop(struct dentry *dentry);
8994 +void au_update_dbbot(struct dentry *dentry);
8995 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8996 +
8997 +/* ---------------------------------------------------------------------- */
8998 +
8999 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9000 +{
9001 +       return dentry->d_fsdata;
9002 +}
9003 +
9004 +/* ---------------------------------------------------------------------- */
9005 +
9006 +/* lock subclass for dinfo */
9007 +enum {
9008 +       AuLsc_DI_CHILD,         /* child first */
9009 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9010 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9011 +       AuLsc_DI_PARENT,
9012 +       AuLsc_DI_PARENT2,
9013 +       AuLsc_DI_PARENT3,
9014 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9015 +};
9016 +
9017 +/*
9018 + * di_read_lock_child, di_write_lock_child,
9019 + * di_read_lock_child2, di_write_lock_child2,
9020 + * di_read_lock_child3, di_write_lock_child3,
9021 + * di_read_lock_parent, di_write_lock_parent,
9022 + * di_read_lock_parent2, di_write_lock_parent2,
9023 + * di_read_lock_parent3, di_write_lock_parent3,
9024 + */
9025 +#define AuReadLockFunc(name, lsc) \
9026 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9027 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9028 +
9029 +#define AuWriteLockFunc(name, lsc) \
9030 +static inline void di_write_lock_##name(struct dentry *d) \
9031 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9032 +
9033 +#define AuRWLockFuncs(name, lsc) \
9034 +       AuReadLockFunc(name, lsc) \
9035 +       AuWriteLockFunc(name, lsc)
9036 +
9037 +AuRWLockFuncs(child, CHILD);
9038 +AuRWLockFuncs(child2, CHILD2);
9039 +AuRWLockFuncs(child3, CHILD3);
9040 +AuRWLockFuncs(parent, PARENT);
9041 +AuRWLockFuncs(parent2, PARENT2);
9042 +AuRWLockFuncs(parent3, PARENT3);
9043 +
9044 +#undef AuReadLockFunc
9045 +#undef AuWriteLockFunc
9046 +#undef AuRWLockFuncs
9047 +
9048 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9049 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9050 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9051 +
9052 +/* ---------------------------------------------------------------------- */
9053 +
9054 +/* todo: memory barrier? */
9055 +static inline unsigned int au_digen(struct dentry *d)
9056 +{
9057 +       return atomic_read(&au_di(d)->di_generation);
9058 +}
9059 +
9060 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9061 +{
9062 +       hdentry->hd_dentry = NULL;
9063 +}
9064 +
9065 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9066 +                                           aufs_bindex_t bindex)
9067 +{
9068 +       return di->di_hdentry + bindex;
9069 +}
9070 +
9071 +static inline void au_hdput(struct au_hdentry *hd)
9072 +{
9073 +       if (hd)
9074 +               dput(hd->hd_dentry);
9075 +}
9076 +
9077 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9078 +{
9079 +       DiMustAnyLock(dentry);
9080 +       return au_di(dentry)->di_btop;
9081 +}
9082 +
9083 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9084 +{
9085 +       DiMustAnyLock(dentry);
9086 +       return au_di(dentry)->di_bbot;
9087 +}
9088 +
9089 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9090 +{
9091 +       DiMustAnyLock(dentry);
9092 +       return au_di(dentry)->di_bwh;
9093 +}
9094 +
9095 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9096 +{
9097 +       DiMustAnyLock(dentry);
9098 +       return au_di(dentry)->di_bdiropq;
9099 +}
9100 +
9101 +/* todo: hard/soft set? */
9102 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9103 +{
9104 +       DiMustWriteLock(dentry);
9105 +       au_di(dentry)->di_btop = bindex;
9106 +}
9107 +
9108 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9109 +{
9110 +       DiMustWriteLock(dentry);
9111 +       au_di(dentry)->di_bbot = bindex;
9112 +}
9113 +
9114 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9115 +{
9116 +       DiMustWriteLock(dentry);
9117 +       /* dbwh can be outside of btop - bbot range */
9118 +       au_di(dentry)->di_bwh = bindex;
9119 +}
9120 +
9121 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9122 +{
9123 +       DiMustWriteLock(dentry);
9124 +       au_di(dentry)->di_bdiropq = bindex;
9125 +}
9126 +
9127 +/* ---------------------------------------------------------------------- */
9128 +
9129 +#ifdef CONFIG_AUFS_HNOTIFY
9130 +static inline void au_digen_dec(struct dentry *d)
9131 +{
9132 +       atomic_dec(&au_di(d)->di_generation);
9133 +}
9134 +
9135 +static inline void au_hn_di_reinit(struct dentry *dentry)
9136 +{
9137 +       dentry->d_fsdata = NULL;
9138 +}
9139 +#else
9140 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9141 +#endif /* CONFIG_AUFS_HNOTIFY */
9142 +
9143 +#endif /* __KERNEL__ */
9144 +#endif /* __AUFS_DENTRY_H__ */
9145 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9146 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9147 +++ linux/fs/aufs/dinfo.c       2017-07-29 12:14:25.899708630 +0200
9148 @@ -0,0 +1,553 @@
9149 +/*
9150 + * Copyright (C) 2005-2017 Junjiro R. Okajima
9151 + *
9152 + * This program, aufs is free software; you can redistribute it and/or modify
9153 + * it under the terms of the GNU General Public License as published by
9154 + * the Free Software Foundation; either version 2 of the License, or
9155 + * (at your option) any later version.
9156 + *
9157 + * This program is distributed in the hope that it will be useful,
9158 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9159 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9160 + * GNU General Public License for more details.
9161 + *
9162 + * You should have received a copy of the GNU General Public License
9163 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9164 + */
9165 +
9166 +/*
9167 + * dentry private data
9168 + */
9169 +
9170 +#include "aufs.h"
9171 +
9172 +void au_di_init_once(void *_dinfo)
9173 +{
9174 +       struct au_dinfo *dinfo = _dinfo;
9175 +
9176 +       au_rw_init(&dinfo->di_rwsem);
9177 +}
9178 +
9179 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9180 +{
9181 +       struct au_dinfo *dinfo;
9182 +       int nbr, i;
9183 +
9184 +       dinfo = au_cache_alloc_dinfo();
9185 +       if (unlikely(!dinfo))
9186 +               goto out;
9187 +
9188 +       nbr = au_sbbot(sb) + 1;
9189 +       if (nbr <= 0)
9190 +               nbr = 1;
9191 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9192 +       if (dinfo->di_hdentry) {
9193 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9194 +               dinfo->di_btop = -1;
9195 +               dinfo->di_bbot = -1;
9196 +               dinfo->di_bwh = -1;
9197 +               dinfo->di_bdiropq = -1;
9198 +               dinfo->di_tmpfile = 0;
9199 +               for (i = 0; i < nbr; i++)
9200 +                       dinfo->di_hdentry[i].hd_id = -1;
9201 +               goto out;
9202 +       }
9203 +
9204 +       au_cache_free_dinfo(dinfo);
9205 +       dinfo = NULL;
9206 +
9207 +out:
9208 +       return dinfo;
9209 +}
9210 +
9211 +void au_di_free(struct au_dinfo *dinfo)
9212 +{
9213 +       struct au_hdentry *p;
9214 +       aufs_bindex_t bbot, bindex;
9215 +
9216 +       /* dentry may not be revalidated */
9217 +       bindex = dinfo->di_btop;
9218 +       if (bindex >= 0) {
9219 +               bbot = dinfo->di_bbot;
9220 +               p = au_hdentry(dinfo, bindex);
9221 +               while (bindex++ <= bbot)
9222 +                       au_hdput(p++);
9223 +       }
9224 +       kfree(dinfo->di_hdentry);
9225 +       au_cache_free_dinfo(dinfo);
9226 +}
9227 +
9228 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9229 +{
9230 +       struct au_hdentry *p;
9231 +       aufs_bindex_t bi;
9232 +
9233 +       AuRwMustWriteLock(&a->di_rwsem);
9234 +       AuRwMustWriteLock(&b->di_rwsem);
9235 +
9236 +#define DiSwap(v, name)                                \
9237 +       do {                                    \
9238 +               v = a->di_##name;               \
9239 +               a->di_##name = b->di_##name;    \
9240 +               b->di_##name = v;               \
9241 +       } while (0)
9242 +
9243 +       DiSwap(p, hdentry);
9244 +       DiSwap(bi, btop);
9245 +       DiSwap(bi, bbot);
9246 +       DiSwap(bi, bwh);
9247 +       DiSwap(bi, bdiropq);
9248 +       /* smp_mb(); */
9249 +
9250 +#undef DiSwap
9251 +}
9252 +
9253 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9254 +{
9255 +       AuRwMustWriteLock(&dst->di_rwsem);
9256 +       AuRwMustWriteLock(&src->di_rwsem);
9257 +
9258 +       dst->di_btop = src->di_btop;
9259 +       dst->di_bbot = src->di_bbot;
9260 +       dst->di_bwh = src->di_bwh;
9261 +       dst->di_bdiropq = src->di_bdiropq;
9262 +       /* smp_mb(); */
9263 +}
9264 +
9265 +int au_di_init(struct dentry *dentry)
9266 +{
9267 +       int err;
9268 +       struct super_block *sb;
9269 +       struct au_dinfo *dinfo;
9270 +
9271 +       err = 0;
9272 +       sb = dentry->d_sb;
9273 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9274 +       if (dinfo) {
9275 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9276 +               /* smp_mb(); */ /* atomic_set */
9277 +               dentry->d_fsdata = dinfo;
9278 +       } else
9279 +               err = -ENOMEM;
9280 +
9281 +       return err;
9282 +}
9283 +
9284 +void au_di_fin(struct dentry *dentry)
9285 +{
9286 +       struct au_dinfo *dinfo;
9287 +
9288 +       dinfo = au_di(dentry);
9289 +       AuRwDestroy(&dinfo->di_rwsem);
9290 +       au_di_free(dinfo);
9291 +}
9292 +
9293 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9294 +{
9295 +       int err, sz;
9296 +       struct au_hdentry *hdp;
9297 +
9298 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9299 +
9300 +       err = -ENOMEM;
9301 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9302 +       if (!sz)
9303 +               sz = sizeof(*hdp);
9304 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9305 +                          may_shrink);
9306 +       if (hdp) {
9307 +               dinfo->di_hdentry = hdp;
9308 +               err = 0;
9309 +       }
9310 +
9311 +       return err;
9312 +}
9313 +
9314 +/* ---------------------------------------------------------------------- */
9315 +
9316 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9317 +{
9318 +       switch (lsc) {
9319 +       case AuLsc_DI_CHILD:
9320 +               ii_write_lock_child(inode);
9321 +               break;
9322 +       case AuLsc_DI_CHILD2:
9323 +               ii_write_lock_child2(inode);
9324 +               break;
9325 +       case AuLsc_DI_CHILD3:
9326 +               ii_write_lock_child3(inode);
9327 +               break;
9328 +       case AuLsc_DI_PARENT:
9329 +               ii_write_lock_parent(inode);
9330 +               break;
9331 +       case AuLsc_DI_PARENT2:
9332 +               ii_write_lock_parent2(inode);
9333 +               break;
9334 +       case AuLsc_DI_PARENT3:
9335 +               ii_write_lock_parent3(inode);
9336 +               break;
9337 +       default:
9338 +               BUG();
9339 +       }
9340 +}
9341 +
9342 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9343 +{
9344 +       switch (lsc) {
9345 +       case AuLsc_DI_CHILD:
9346 +               ii_read_lock_child(inode);
9347 +               break;
9348 +       case AuLsc_DI_CHILD2:
9349 +               ii_read_lock_child2(inode);
9350 +               break;
9351 +       case AuLsc_DI_CHILD3:
9352 +               ii_read_lock_child3(inode);
9353 +               break;
9354 +       case AuLsc_DI_PARENT:
9355 +               ii_read_lock_parent(inode);
9356 +               break;
9357 +       case AuLsc_DI_PARENT2:
9358 +               ii_read_lock_parent2(inode);
9359 +               break;
9360 +       case AuLsc_DI_PARENT3:
9361 +               ii_read_lock_parent3(inode);
9362 +               break;
9363 +       default:
9364 +               BUG();
9365 +       }
9366 +}
9367 +
9368 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9369 +{
9370 +       struct inode *inode;
9371 +
9372 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9373 +       if (d_really_is_positive(d)) {
9374 +               inode = d_inode(d);
9375 +               if (au_ftest_lock(flags, IW))
9376 +                       do_ii_write_lock(inode, lsc);
9377 +               else if (au_ftest_lock(flags, IR))
9378 +                       do_ii_read_lock(inode, lsc);
9379 +       }
9380 +}
9381 +
9382 +void di_read_unlock(struct dentry *d, int flags)
9383 +{
9384 +       struct inode *inode;
9385 +
9386 +       if (d_really_is_positive(d)) {
9387 +               inode = d_inode(d);
9388 +               if (au_ftest_lock(flags, IW)) {
9389 +                       au_dbg_verify_dinode(d);
9390 +                       ii_write_unlock(inode);
9391 +               } else if (au_ftest_lock(flags, IR)) {
9392 +                       au_dbg_verify_dinode(d);
9393 +                       ii_read_unlock(inode);
9394 +               }
9395 +       }
9396 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9397 +}
9398 +
9399 +void di_downgrade_lock(struct dentry *d, int flags)
9400 +{
9401 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9402 +               ii_downgrade_lock(d_inode(d));
9403 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9404 +}
9405 +
9406 +void di_write_lock(struct dentry *d, unsigned int lsc)
9407 +{
9408 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9409 +       if (d_really_is_positive(d))
9410 +               do_ii_write_lock(d_inode(d), lsc);
9411 +}
9412 +
9413 +void di_write_unlock(struct dentry *d)
9414 +{
9415 +       au_dbg_verify_dinode(d);
9416 +       if (d_really_is_positive(d))
9417 +               ii_write_unlock(d_inode(d));
9418 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9419 +}
9420 +
9421 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9422 +{
9423 +       AuDebugOn(d1 == d2
9424 +                 || d_inode(d1) == d_inode(d2)
9425 +                 || d1->d_sb != d2->d_sb);
9426 +
9427 +       if ((isdir && au_test_subdir(d1, d2))
9428 +           || d1 < d2) {
9429 +               di_write_lock_child(d1);
9430 +               di_write_lock_child2(d2);
9431 +       } else {
9432 +               di_write_lock_child(d2);
9433 +               di_write_lock_child2(d1);
9434 +       }
9435 +}
9436 +
9437 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9438 +{
9439 +       AuDebugOn(d1 == d2
9440 +                 || d_inode(d1) == d_inode(d2)
9441 +                 || d1->d_sb != d2->d_sb);
9442 +
9443 +       if ((isdir && au_test_subdir(d1, d2))
9444 +           || d1 < d2) {
9445 +               di_write_lock_parent(d1);
9446 +               di_write_lock_parent2(d2);
9447 +       } else {
9448 +               di_write_lock_parent(d2);
9449 +               di_write_lock_parent2(d1);
9450 +       }
9451 +}
9452 +
9453 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9454 +{
9455 +       di_write_unlock(d1);
9456 +       if (d_inode(d1) == d_inode(d2))
9457 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9458 +       else
9459 +               di_write_unlock(d2);
9460 +}
9461 +
9462 +/* ---------------------------------------------------------------------- */
9463 +
9464 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9465 +{
9466 +       struct dentry *d;
9467 +
9468 +       DiMustAnyLock(dentry);
9469 +
9470 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9471 +               return NULL;
9472 +       AuDebugOn(bindex < 0);
9473 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9474 +       AuDebugOn(d && au_dcount(d) <= 0);
9475 +       return d;
9476 +}
9477 +
9478 +/*
9479 + * extended version of au_h_dptr().
9480 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9481 + * error.
9482 + */
9483 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9484 +{
9485 +       struct dentry *h_dentry;
9486 +       struct inode *inode, *h_inode;
9487 +
9488 +       AuDebugOn(d_really_is_negative(dentry));
9489 +
9490 +       h_dentry = NULL;
9491 +       if (au_dbtop(dentry) <= bindex
9492 +           && bindex <= au_dbbot(dentry))
9493 +               h_dentry = au_h_dptr(dentry, bindex);
9494 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9495 +               dget(h_dentry);
9496 +               goto out; /* success */
9497 +       }
9498 +
9499 +       inode = d_inode(dentry);
9500 +       AuDebugOn(bindex < au_ibtop(inode));
9501 +       AuDebugOn(au_ibbot(inode) < bindex);
9502 +       h_inode = au_h_iptr(inode, bindex);
9503 +       h_dentry = d_find_alias(h_inode);
9504 +       if (h_dentry) {
9505 +               if (!IS_ERR(h_dentry)) {
9506 +                       if (!au_d_linkable(h_dentry))
9507 +                               goto out; /* success */
9508 +                       dput(h_dentry);
9509 +               } else
9510 +                       goto out;
9511 +       }
9512 +
9513 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9514 +               h_dentry = au_plink_lkup(inode, bindex);
9515 +               AuDebugOn(!h_dentry);
9516 +               if (!IS_ERR(h_dentry)) {
9517 +                       if (!au_d_hashed_positive(h_dentry))
9518 +                               goto out; /* success */
9519 +                       dput(h_dentry);
9520 +                       h_dentry = NULL;
9521 +               }
9522 +       }
9523 +
9524 +out:
9525 +       AuDbgDentry(h_dentry);
9526 +       return h_dentry;
9527 +}
9528 +
9529 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9530 +{
9531 +       aufs_bindex_t bbot, bwh;
9532 +
9533 +       bbot = au_dbbot(dentry);
9534 +       if (0 <= bbot) {
9535 +               bwh = au_dbwh(dentry);
9536 +               if (!bwh)
9537 +                       return bwh;
9538 +               if (0 < bwh && bwh < bbot)
9539 +                       return bwh - 1;
9540 +       }
9541 +       return bbot;
9542 +}
9543 +
9544 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9545 +{
9546 +       aufs_bindex_t bbot, bopq;
9547 +
9548 +       bbot = au_dbtail(dentry);
9549 +       if (0 <= bbot) {
9550 +               bopq = au_dbdiropq(dentry);
9551 +               if (0 <= bopq && bopq < bbot)
9552 +                       bbot = bopq;
9553 +       }
9554 +       return bbot;
9555 +}
9556 +
9557 +/* ---------------------------------------------------------------------- */
9558 +
9559 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9560 +                  struct dentry *h_dentry)
9561 +{
9562 +       struct au_dinfo *dinfo;
9563 +       struct au_hdentry *hd;
9564 +       struct au_branch *br;
9565 +
9566 +       DiMustWriteLock(dentry);
9567 +
9568 +       dinfo = au_di(dentry);
9569 +       hd = au_hdentry(dinfo, bindex);
9570 +       au_hdput(hd);
9571 +       hd->hd_dentry = h_dentry;
9572 +       if (h_dentry) {
9573 +               br = au_sbr(dentry->d_sb, bindex);
9574 +               hd->hd_id = br->br_id;
9575 +       }
9576 +}
9577 +
9578 +int au_dbrange_test(struct dentry *dentry)
9579 +{
9580 +       int err;
9581 +       aufs_bindex_t btop, bbot;
9582 +
9583 +       err = 0;
9584 +       btop = au_dbtop(dentry);
9585 +       bbot = au_dbbot(dentry);
9586 +       if (btop >= 0)
9587 +               AuDebugOn(bbot < 0 && btop > bbot);
9588 +       else {
9589 +               err = -EIO;
9590 +               AuDebugOn(bbot >= 0);
9591 +       }
9592 +
9593 +       return err;
9594 +}
9595 +
9596 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9597 +{
9598 +       int err;
9599 +
9600 +       err = 0;
9601 +       if (unlikely(au_digen(dentry) != sigen
9602 +                    || au_iigen_test(d_inode(dentry), sigen)))
9603 +               err = -EIO;
9604 +
9605 +       return err;
9606 +}
9607 +
9608 +void au_update_digen(struct dentry *dentry)
9609 +{
9610 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9611 +       /* smp_mb(); */ /* atomic_set */
9612 +}
9613 +
9614 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9615 +{
9616 +       struct au_dinfo *dinfo;
9617 +       struct dentry *h_d;
9618 +       struct au_hdentry *hdp;
9619 +       aufs_bindex_t bindex, bbot;
9620 +
9621 +       DiMustWriteLock(dentry);
9622 +
9623 +       dinfo = au_di(dentry);
9624 +       if (!dinfo || dinfo->di_btop < 0)
9625 +               return;
9626 +
9627 +       if (do_put_zero) {
9628 +               bbot = dinfo->di_bbot;
9629 +               bindex = dinfo->di_btop;
9630 +               hdp = au_hdentry(dinfo, bindex);
9631 +               for (; bindex <= bbot; bindex++, hdp++) {
9632 +                       h_d = hdp->hd_dentry;
9633 +                       if (h_d && d_is_negative(h_d))
9634 +                               au_set_h_dptr(dentry, bindex, NULL);
9635 +               }
9636 +       }
9637 +
9638 +       dinfo->di_btop = 0;
9639 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9640 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9641 +               if (hdp->hd_dentry)
9642 +                       break;
9643 +       if (dinfo->di_btop > dinfo->di_bbot) {
9644 +               dinfo->di_btop = -1;
9645 +               dinfo->di_bbot = -1;
9646 +               return;
9647 +       }
9648 +
9649 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9650 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9651 +               if (hdp->hd_dentry)
9652 +                       break;
9653 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9654 +}
9655 +
9656 +void au_update_dbtop(struct dentry *dentry)
9657 +{
9658 +       aufs_bindex_t bindex, bbot;
9659 +       struct dentry *h_dentry;
9660 +
9661 +       bbot = au_dbbot(dentry);
9662 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9663 +               h_dentry = au_h_dptr(dentry, bindex);
9664 +               if (!h_dentry)
9665 +                       continue;
9666 +               if (d_is_positive(h_dentry)) {
9667 +                       au_set_dbtop(dentry, bindex);
9668 +                       return;
9669 +               }
9670 +               au_set_h_dptr(dentry, bindex, NULL);
9671 +       }
9672 +}
9673 +
9674 +void au_update_dbbot(struct dentry *dentry)
9675 +{
9676 +       aufs_bindex_t bindex, btop;
9677 +       struct dentry *h_dentry;
9678 +
9679 +       btop = au_dbtop(dentry);
9680 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9681 +               h_dentry = au_h_dptr(dentry, bindex);
9682 +               if (!h_dentry)
9683 +                       continue;
9684 +               if (d_is_positive(h_dentry)) {
9685 +                       au_set_dbbot(dentry, bindex);
9686 +                       return;
9687 +               }
9688 +               au_set_h_dptr(dentry, bindex, NULL);
9689 +       }
9690 +}
9691 +
9692 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9693 +{
9694 +       aufs_bindex_t bindex, bbot;
9695 +
9696 +       bbot = au_dbbot(dentry);
9697 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9698 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9699 +                       return bindex;
9700 +       return -1;
9701 +}
9702 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9703 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9704 +++ linux/fs/aufs/dir.c 2017-07-29 12:14:25.899708630 +0200
9705 @@ -0,0 +1,759 @@
9706 +/*
9707 + * Copyright (C) 2005-2017 Junjiro R. Okajima
9708 + *
9709 + * This program, aufs is free software; you can redistribute it and/or modify
9710 + * it under the terms of the GNU General Public License as published by
9711 + * the Free Software Foundation; either version 2 of the License, or
9712 + * (at your option) any later version.
9713 + *
9714 + * This program is distributed in the hope that it will be useful,
9715 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9716 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9717 + * GNU General Public License for more details.
9718 + *
9719 + * You should have received a copy of the GNU General Public License
9720 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9721 + */
9722 +
9723 +/*
9724 + * directory operations
9725 + */
9726 +
9727 +#include <linux/fs_stack.h>
9728 +#include "aufs.h"
9729 +
9730 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9731 +{
9732 +       unsigned int nlink;
9733 +
9734 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9735 +
9736 +       nlink = dir->i_nlink;
9737 +       nlink += h_dir->i_nlink - 2;
9738 +       if (h_dir->i_nlink < 2)
9739 +               nlink += 2;
9740 +       smp_mb(); /* for i_nlink */
9741 +       /* 0 can happen in revaliding */
9742 +       set_nlink(dir, nlink);
9743 +}
9744 +
9745 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9746 +{
9747 +       unsigned int nlink;
9748 +
9749 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9750 +
9751 +       nlink = dir->i_nlink;
9752 +       nlink -= h_dir->i_nlink - 2;
9753 +       if (h_dir->i_nlink < 2)
9754 +               nlink -= 2;
9755 +       smp_mb(); /* for i_nlink */
9756 +       /* nlink == 0 means the branch-fs is broken */
9757 +       set_nlink(dir, nlink);
9758 +}
9759 +
9760 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9761 +{
9762 +       loff_t sz;
9763 +       aufs_bindex_t bindex, bbot;
9764 +       struct file *h_file;
9765 +       struct dentry *h_dentry;
9766 +
9767 +       sz = 0;
9768 +       if (file) {
9769 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9770 +
9771 +               bbot = au_fbbot_dir(file);
9772 +               for (bindex = au_fbtop(file);
9773 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9774 +                    bindex++) {
9775 +                       h_file = au_hf_dir(file, bindex);
9776 +                       if (h_file && file_inode(h_file))
9777 +                               sz += vfsub_f_size_read(h_file);
9778 +               }
9779 +       } else {
9780 +               AuDebugOn(!dentry);
9781 +               AuDebugOn(!d_is_dir(dentry));
9782 +
9783 +               bbot = au_dbtaildir(dentry);
9784 +               for (bindex = au_dbtop(dentry);
9785 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9786 +                    bindex++) {
9787 +                       h_dentry = au_h_dptr(dentry, bindex);
9788 +                       if (h_dentry && d_is_positive(h_dentry))
9789 +                               sz += i_size_read(d_inode(h_dentry));
9790 +               }
9791 +       }
9792 +       if (sz < KMALLOC_MAX_SIZE)
9793 +               sz = roundup_pow_of_two(sz);
9794 +       if (sz > KMALLOC_MAX_SIZE)
9795 +               sz = KMALLOC_MAX_SIZE;
9796 +       else if (sz < NAME_MAX) {
9797 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9798 +               sz = AUFS_RDBLK_DEF;
9799 +       }
9800 +       return sz;
9801 +}
9802 +
9803 +struct au_dir_ts_arg {
9804 +       struct dentry *dentry;
9805 +       aufs_bindex_t brid;
9806 +};
9807 +
9808 +static void au_do_dir_ts(void *arg)
9809 +{
9810 +       struct au_dir_ts_arg *a = arg;
9811 +       struct au_dtime dt;
9812 +       struct path h_path;
9813 +       struct inode *dir, *h_dir;
9814 +       struct super_block *sb;
9815 +       struct au_branch *br;
9816 +       struct au_hinode *hdir;
9817 +       int err;
9818 +       aufs_bindex_t btop, bindex;
9819 +
9820 +       sb = a->dentry->d_sb;
9821 +       if (d_really_is_negative(a->dentry))
9822 +               goto out;
9823 +       /* no dir->i_mutex lock */
9824 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9825 +
9826 +       dir = d_inode(a->dentry);
9827 +       btop = au_ibtop(dir);
9828 +       bindex = au_br_index(sb, a->brid);
9829 +       if (bindex < btop)
9830 +               goto out_unlock;
9831 +
9832 +       br = au_sbr(sb, bindex);
9833 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9834 +       if (!h_path.dentry)
9835 +               goto out_unlock;
9836 +       h_path.mnt = au_br_mnt(br);
9837 +       au_dtime_store(&dt, a->dentry, &h_path);
9838 +
9839 +       br = au_sbr(sb, btop);
9840 +       if (!au_br_writable(br->br_perm))
9841 +               goto out_unlock;
9842 +       h_path.dentry = au_h_dptr(a->dentry, btop);
9843 +       h_path.mnt = au_br_mnt(br);
9844 +       err = vfsub_mnt_want_write(h_path.mnt);
9845 +       if (err)
9846 +               goto out_unlock;
9847 +       hdir = au_hi(dir, btop);
9848 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
9849 +       h_dir = au_h_iptr(dir, btop);
9850 +       if (h_dir->i_nlink
9851 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9852 +               dt.dt_h_path = h_path;
9853 +               au_dtime_revert(&dt);
9854 +       }
9855 +       au_hn_inode_unlock(hdir);
9856 +       vfsub_mnt_drop_write(h_path.mnt);
9857 +       au_cpup_attr_timesizes(dir);
9858 +
9859 +out_unlock:
9860 +       aufs_read_unlock(a->dentry, AuLock_DW);
9861 +out:
9862 +       dput(a->dentry);
9863 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9864 +       kfree(arg);
9865 +}
9866 +
9867 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9868 +{
9869 +       int perm, wkq_err;
9870 +       aufs_bindex_t btop;
9871 +       struct au_dir_ts_arg *arg;
9872 +       struct dentry *dentry;
9873 +       struct super_block *sb;
9874 +
9875 +       IMustLock(dir);
9876 +
9877 +       dentry = d_find_any_alias(dir);
9878 +       AuDebugOn(!dentry);
9879 +       sb = dentry->d_sb;
9880 +       btop = au_ibtop(dir);
9881 +       if (btop == bindex) {
9882 +               au_cpup_attr_timesizes(dir);
9883 +               goto out;
9884 +       }
9885 +
9886 +       perm = au_sbr_perm(sb, btop);
9887 +       if (!au_br_writable(perm))
9888 +               goto out;
9889 +
9890 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9891 +       if (!arg)
9892 +               goto out;
9893 +
9894 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9895 +       arg->brid = au_sbr_id(sb, bindex);
9896 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9897 +       if (unlikely(wkq_err)) {
9898 +               pr_err("wkq %d\n", wkq_err);
9899 +               dput(dentry);
9900 +               kfree(arg);
9901 +       }
9902 +
9903 +out:
9904 +       dput(dentry);
9905 +}
9906 +
9907 +/* ---------------------------------------------------------------------- */
9908 +
9909 +static int reopen_dir(struct file *file)
9910 +{
9911 +       int err;
9912 +       unsigned int flags;
9913 +       aufs_bindex_t bindex, btail, btop;
9914 +       struct dentry *dentry, *h_dentry;
9915 +       struct file *h_file;
9916 +
9917 +       /* open all lower dirs */
9918 +       dentry = file->f_path.dentry;
9919 +       btop = au_dbtop(dentry);
9920 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
9921 +               au_set_h_fptr(file, bindex, NULL);
9922 +       au_set_fbtop(file, btop);
9923 +
9924 +       btail = au_dbtaildir(dentry);
9925 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
9926 +               au_set_h_fptr(file, bindex, NULL);
9927 +       au_set_fbbot_dir(file, btail);
9928 +
9929 +       flags = vfsub_file_flags(file);
9930 +       for (bindex = btop; bindex <= btail; bindex++) {
9931 +               h_dentry = au_h_dptr(dentry, bindex);
9932 +               if (!h_dentry)
9933 +                       continue;
9934 +               h_file = au_hf_dir(file, bindex);
9935 +               if (h_file)
9936 +                       continue;
9937 +
9938 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9939 +               err = PTR_ERR(h_file);
9940 +               if (IS_ERR(h_file))
9941 +                       goto out; /* close all? */
9942 +               au_set_h_fptr(file, bindex, h_file);
9943 +       }
9944 +       au_update_figen(file);
9945 +       /* todo: necessary? */
9946 +       /* file->f_ra = h_file->f_ra; */
9947 +       err = 0;
9948 +
9949 +out:
9950 +       return err;
9951 +}
9952 +
9953 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9954 +{
9955 +       int err;
9956 +       aufs_bindex_t bindex, btail;
9957 +       struct dentry *dentry, *h_dentry;
9958 +       struct vfsmount *mnt;
9959 +
9960 +       FiMustWriteLock(file);
9961 +       AuDebugOn(h_file);
9962 +
9963 +       err = 0;
9964 +       mnt = file->f_path.mnt;
9965 +       dentry = file->f_path.dentry;
9966 +       file->f_version = d_inode(dentry)->i_version;
9967 +       bindex = au_dbtop(dentry);
9968 +       au_set_fbtop(file, bindex);
9969 +       btail = au_dbtaildir(dentry);
9970 +       au_set_fbbot_dir(file, btail);
9971 +       for (; !err && bindex <= btail; bindex++) {
9972 +               h_dentry = au_h_dptr(dentry, bindex);
9973 +               if (!h_dentry)
9974 +                       continue;
9975 +
9976 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9977 +               if (unlikely(err))
9978 +                       break;
9979 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9980 +               if (IS_ERR(h_file)) {
9981 +                       err = PTR_ERR(h_file);
9982 +                       break;
9983 +               }
9984 +               au_set_h_fptr(file, bindex, h_file);
9985 +       }
9986 +       au_update_figen(file);
9987 +       /* todo: necessary? */
9988 +       /* file->f_ra = h_file->f_ra; */
9989 +       if (!err)
9990 +               return 0; /* success */
9991 +
9992 +       /* close all */
9993 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
9994 +               au_set_h_fptr(file, bindex, NULL);
9995 +       au_set_fbtop(file, -1);
9996 +       au_set_fbbot_dir(file, -1);
9997 +
9998 +       return err;
9999 +}
10000 +
10001 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10002 +                        struct file *file)
10003 +{
10004 +       int err;
10005 +       struct super_block *sb;
10006 +       struct au_fidir *fidir;
10007 +
10008 +       err = -ENOMEM;
10009 +       sb = file->f_path.dentry->d_sb;
10010 +       si_read_lock(sb, AuLock_FLUSH);
10011 +       fidir = au_fidir_alloc(sb);
10012 +       if (fidir) {
10013 +               struct au_do_open_args args = {
10014 +                       .open   = do_open_dir,
10015 +                       .fidir  = fidir
10016 +               };
10017 +               err = au_do_open(file, &args);
10018 +               if (unlikely(err))
10019 +                       kfree(fidir);
10020 +       }
10021 +       si_read_unlock(sb);
10022 +       return err;
10023 +}
10024 +
10025 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10026 +                           struct file *file)
10027 +{
10028 +       struct au_vdir *vdir_cache;
10029 +       struct au_finfo *finfo;
10030 +       struct au_fidir *fidir;
10031 +       struct au_hfile *hf;
10032 +       aufs_bindex_t bindex, bbot;
10033 +
10034 +       finfo = au_fi(file);
10035 +       fidir = finfo->fi_hdir;
10036 +       if (fidir) {
10037 +               au_sphl_del(&finfo->fi_hlist,
10038 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
10039 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10040 +               if (vdir_cache)
10041 +                       au_vdir_free(vdir_cache);
10042 +
10043 +               bindex = finfo->fi_btop;
10044 +               if (bindex >= 0) {
10045 +                       hf = fidir->fd_hfile + bindex;
10046 +                       /*
10047 +                        * calls fput() instead of filp_close(),
10048 +                        * since no dnotify or lock for the lower file.
10049 +                        */
10050 +                       bbot = fidir->fd_bbot;
10051 +                       for (; bindex <= bbot; bindex++, hf++)
10052 +                               if (hf->hf_file)
10053 +                                       au_hfput(hf, /*execed*/0);
10054 +               }
10055 +               kfree(fidir);
10056 +               finfo->fi_hdir = NULL;
10057 +       }
10058 +       au_finfo_fin(file);
10059 +       return 0;
10060 +}
10061 +
10062 +/* ---------------------------------------------------------------------- */
10063 +
10064 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10065 +{
10066 +       int err;
10067 +       aufs_bindex_t bindex, bbot;
10068 +       struct file *h_file;
10069 +
10070 +       err = 0;
10071 +       bbot = au_fbbot_dir(file);
10072 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10073 +               h_file = au_hf_dir(file, bindex);
10074 +               if (h_file)
10075 +                       err = vfsub_flush(h_file, id);
10076 +       }
10077 +       return err;
10078 +}
10079 +
10080 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10081 +{
10082 +       return au_do_flush(file, id, au_do_flush_dir);
10083 +}
10084 +
10085 +/* ---------------------------------------------------------------------- */
10086 +
10087 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10088 +{
10089 +       int err;
10090 +       aufs_bindex_t bbot, bindex;
10091 +       struct inode *inode;
10092 +       struct super_block *sb;
10093 +
10094 +       err = 0;
10095 +       sb = dentry->d_sb;
10096 +       inode = d_inode(dentry);
10097 +       IMustLock(inode);
10098 +       bbot = au_dbbot(dentry);
10099 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10100 +               struct path h_path;
10101 +
10102 +               if (au_test_ro(sb, bindex, inode))
10103 +                       continue;
10104 +               h_path.dentry = au_h_dptr(dentry, bindex);
10105 +               if (!h_path.dentry)
10106 +                       continue;
10107 +
10108 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10109 +               err = vfsub_fsync(NULL, &h_path, datasync);
10110 +       }
10111 +
10112 +       return err;
10113 +}
10114 +
10115 +static int au_do_fsync_dir(struct file *file, int datasync)
10116 +{
10117 +       int err;
10118 +       aufs_bindex_t bbot, bindex;
10119 +       struct file *h_file;
10120 +       struct super_block *sb;
10121 +       struct inode *inode;
10122 +
10123 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10124 +       if (unlikely(err))
10125 +               goto out;
10126 +
10127 +       inode = file_inode(file);
10128 +       sb = inode->i_sb;
10129 +       bbot = au_fbbot_dir(file);
10130 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10131 +               h_file = au_hf_dir(file, bindex);
10132 +               if (!h_file || au_test_ro(sb, bindex, inode))
10133 +                       continue;
10134 +
10135 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10136 +       }
10137 +
10138 +out:
10139 +       return err;
10140 +}
10141 +
10142 +/*
10143 + * @file may be NULL
10144 + */
10145 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10146 +                         int datasync)
10147 +{
10148 +       int err;
10149 +       struct dentry *dentry;
10150 +       struct inode *inode;
10151 +       struct super_block *sb;
10152 +
10153 +       err = 0;
10154 +       dentry = file->f_path.dentry;
10155 +       inode = d_inode(dentry);
10156 +       inode_lock(inode);
10157 +       sb = dentry->d_sb;
10158 +       si_noflush_read_lock(sb);
10159 +       if (file)
10160 +               err = au_do_fsync_dir(file, datasync);
10161 +       else {
10162 +               di_write_lock_child(dentry);
10163 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10164 +       }
10165 +       au_cpup_attr_timesizes(inode);
10166 +       di_write_unlock(dentry);
10167 +       if (file)
10168 +               fi_write_unlock(file);
10169 +
10170 +       si_read_unlock(sb);
10171 +       inode_unlock(inode);
10172 +       return err;
10173 +}
10174 +
10175 +/* ---------------------------------------------------------------------- */
10176 +
10177 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10178 +{
10179 +       int err;
10180 +       struct dentry *dentry;
10181 +       struct inode *inode, *h_inode;
10182 +       struct super_block *sb;
10183 +
10184 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
10185 +
10186 +       dentry = file->f_path.dentry;
10187 +       inode = d_inode(dentry);
10188 +       IMustLock(inode);
10189 +
10190 +       sb = dentry->d_sb;
10191 +       si_read_lock(sb, AuLock_FLUSH);
10192 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10193 +       if (unlikely(err))
10194 +               goto out;
10195 +       err = au_alive_dir(dentry);
10196 +       if (!err)
10197 +               err = au_vdir_init(file);
10198 +       di_downgrade_lock(dentry, AuLock_IR);
10199 +       if (unlikely(err))
10200 +               goto out_unlock;
10201 +
10202 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10203 +       if (!au_test_nfsd()) {
10204 +               err = au_vdir_fill_de(file, ctx);
10205 +               fsstack_copy_attr_atime(inode, h_inode);
10206 +       } else {
10207 +               /*
10208 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10209 +                * encode_fh() and others.
10210 +                */
10211 +               atomic_inc(&h_inode->i_count);
10212 +               di_read_unlock(dentry, AuLock_IR);
10213 +               si_read_unlock(sb);
10214 +               err = au_vdir_fill_de(file, ctx);
10215 +               fsstack_copy_attr_atime(inode, h_inode);
10216 +               fi_write_unlock(file);
10217 +               iput(h_inode);
10218 +
10219 +               AuTraceErr(err);
10220 +               return err;
10221 +       }
10222 +
10223 +out_unlock:
10224 +       di_read_unlock(dentry, AuLock_IR);
10225 +       fi_write_unlock(file);
10226 +out:
10227 +       si_read_unlock(sb);
10228 +       return err;
10229 +}
10230 +
10231 +/* ---------------------------------------------------------------------- */
10232 +
10233 +#define AuTestEmpty_WHONLY     1
10234 +#define AuTestEmpty_CALLED     (1 << 1)
10235 +#define AuTestEmpty_SHWH       (1 << 2)
10236 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10237 +#define au_fset_testempty(flags, name) \
10238 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10239 +#define au_fclr_testempty(flags, name) \
10240 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10241 +
10242 +#ifndef CONFIG_AUFS_SHWH
10243 +#undef AuTestEmpty_SHWH
10244 +#define AuTestEmpty_SHWH       0
10245 +#endif
10246 +
10247 +struct test_empty_arg {
10248 +       struct dir_context ctx;
10249 +       struct au_nhash *whlist;
10250 +       unsigned int flags;
10251 +       int err;
10252 +       aufs_bindex_t bindex;
10253 +};
10254 +
10255 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10256 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10257 +                        unsigned int d_type)
10258 +{
10259 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10260 +                                                 ctx);
10261 +       char *name = (void *)__name;
10262 +
10263 +       arg->err = 0;
10264 +       au_fset_testempty(arg->flags, CALLED);
10265 +       /* smp_mb(); */
10266 +       if (name[0] == '.'
10267 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10268 +               goto out; /* success */
10269 +
10270 +       if (namelen <= AUFS_WH_PFX_LEN
10271 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10272 +               if (au_ftest_testempty(arg->flags, WHONLY)
10273 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10274 +                       arg->err = -ENOTEMPTY;
10275 +               goto out;
10276 +       }
10277 +
10278 +       name += AUFS_WH_PFX_LEN;
10279 +       namelen -= AUFS_WH_PFX_LEN;
10280 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10281 +               arg->err = au_nhash_append_wh
10282 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10283 +                        au_ftest_testempty(arg->flags, SHWH));
10284 +
10285 +out:
10286 +       /* smp_mb(); */
10287 +       AuTraceErr(arg->err);
10288 +       return arg->err;
10289 +}
10290 +
10291 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10292 +{
10293 +       int err;
10294 +       struct file *h_file;
10295 +
10296 +       h_file = au_h_open(dentry, arg->bindex,
10297 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10298 +                          /*file*/NULL, /*force_wr*/0);
10299 +       err = PTR_ERR(h_file);
10300 +       if (IS_ERR(h_file))
10301 +               goto out;
10302 +
10303 +       err = 0;
10304 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10305 +           && !file_inode(h_file)->i_nlink)
10306 +               goto out_put;
10307 +
10308 +       do {
10309 +               arg->err = 0;
10310 +               au_fclr_testempty(arg->flags, CALLED);
10311 +               /* smp_mb(); */
10312 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10313 +               if (err >= 0)
10314 +                       err = arg->err;
10315 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10316 +
10317 +out_put:
10318 +       fput(h_file);
10319 +       au_sbr_put(dentry->d_sb, arg->bindex);
10320 +out:
10321 +       return err;
10322 +}
10323 +
10324 +struct do_test_empty_args {
10325 +       int *errp;
10326 +       struct dentry *dentry;
10327 +       struct test_empty_arg *arg;
10328 +};
10329 +
10330 +static void call_do_test_empty(void *args)
10331 +{
10332 +       struct do_test_empty_args *a = args;
10333 +       *a->errp = do_test_empty(a->dentry, a->arg);
10334 +}
10335 +
10336 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10337 +{
10338 +       int err, wkq_err;
10339 +       struct dentry *h_dentry;
10340 +       struct inode *h_inode;
10341 +
10342 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10343 +       h_inode = d_inode(h_dentry);
10344 +       /* todo: i_mode changes anytime? */
10345 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
10346 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10347 +       inode_unlock(h_inode);
10348 +       if (!err)
10349 +               err = do_test_empty(dentry, arg);
10350 +       else {
10351 +               struct do_test_empty_args args = {
10352 +                       .errp   = &err,
10353 +                       .dentry = dentry,
10354 +                       .arg    = arg
10355 +               };
10356 +               unsigned int flags = arg->flags;
10357 +
10358 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10359 +               if (unlikely(wkq_err))
10360 +                       err = wkq_err;
10361 +               arg->flags = flags;
10362 +       }
10363 +
10364 +       return err;
10365 +}
10366 +
10367 +int au_test_empty_lower(struct dentry *dentry)
10368 +{
10369 +       int err;
10370 +       unsigned int rdhash;
10371 +       aufs_bindex_t bindex, btop, btail;
10372 +       struct au_nhash whlist;
10373 +       struct test_empty_arg arg = {
10374 +               .ctx = {
10375 +                       .actor = test_empty_cb
10376 +               }
10377 +       };
10378 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10379 +
10380 +       SiMustAnyLock(dentry->d_sb);
10381 +
10382 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10383 +       if (!rdhash)
10384 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10385 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10386 +       if (unlikely(err))
10387 +               goto out;
10388 +
10389 +       arg.flags = 0;
10390 +       arg.whlist = &whlist;
10391 +       btop = au_dbtop(dentry);
10392 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10393 +               au_fset_testempty(arg.flags, SHWH);
10394 +       test_empty = do_test_empty;
10395 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10396 +               test_empty = sio_test_empty;
10397 +       arg.bindex = btop;
10398 +       err = test_empty(dentry, &arg);
10399 +       if (unlikely(err))
10400 +               goto out_whlist;
10401 +
10402 +       au_fset_testempty(arg.flags, WHONLY);
10403 +       btail = au_dbtaildir(dentry);
10404 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10405 +               struct dentry *h_dentry;
10406 +
10407 +               h_dentry = au_h_dptr(dentry, bindex);
10408 +               if (h_dentry && d_is_positive(h_dentry)) {
10409 +                       arg.bindex = bindex;
10410 +                       err = test_empty(dentry, &arg);
10411 +               }
10412 +       }
10413 +
10414 +out_whlist:
10415 +       au_nhash_wh_free(&whlist);
10416 +out:
10417 +       return err;
10418 +}
10419 +
10420 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10421 +{
10422 +       int err;
10423 +       struct test_empty_arg arg = {
10424 +               .ctx = {
10425 +                       .actor = test_empty_cb
10426 +               }
10427 +       };
10428 +       aufs_bindex_t bindex, btail;
10429 +
10430 +       err = 0;
10431 +       arg.whlist = whlist;
10432 +       arg.flags = AuTestEmpty_WHONLY;
10433 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10434 +               au_fset_testempty(arg.flags, SHWH);
10435 +       btail = au_dbtaildir(dentry);
10436 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10437 +               struct dentry *h_dentry;
10438 +
10439 +               h_dentry = au_h_dptr(dentry, bindex);
10440 +               if (h_dentry && d_is_positive(h_dentry)) {
10441 +                       arg.bindex = bindex;
10442 +                       err = sio_test_empty(dentry, &arg);
10443 +               }
10444 +       }
10445 +
10446 +       return err;
10447 +}
10448 +
10449 +/* ---------------------------------------------------------------------- */
10450 +
10451 +const struct file_operations aufs_dir_fop = {
10452 +       .owner          = THIS_MODULE,
10453 +       .llseek         = default_llseek,
10454 +       .read           = generic_read_dir,
10455 +       .iterate_shared = aufs_iterate_shared,
10456 +       .unlocked_ioctl = aufs_ioctl_dir,
10457 +#ifdef CONFIG_COMPAT
10458 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10459 +#endif
10460 +       .open           = aufs_open_dir,
10461 +       .release        = aufs_release_dir,
10462 +       .flush          = aufs_flush_dir,
10463 +       .fsync          = aufs_fsync_dir
10464 +};
10465 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10466 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10467 +++ linux/fs/aufs/dir.h 2017-07-29 12:14:25.899708630 +0200
10468 @@ -0,0 +1,131 @@
10469 +/*
10470 + * Copyright (C) 2005-2017 Junjiro R. Okajima
10471 + *
10472 + * This program, aufs is free software; you can redistribute it and/or modify
10473 + * it under the terms of the GNU General Public License as published by
10474 + * the Free Software Foundation; either version 2 of the License, or
10475 + * (at your option) any later version.
10476 + *
10477 + * This program is distributed in the hope that it will be useful,
10478 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10479 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10480 + * GNU General Public License for more details.
10481 + *
10482 + * You should have received a copy of the GNU General Public License
10483 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10484 + */
10485 +
10486 +/*
10487 + * directory operations
10488 + */
10489 +
10490 +#ifndef __AUFS_DIR_H__
10491 +#define __AUFS_DIR_H__
10492 +
10493 +#ifdef __KERNEL__
10494 +
10495 +#include <linux/fs.h>
10496 +
10497 +/* ---------------------------------------------------------------------- */
10498 +
10499 +/* need to be faster and smaller */
10500 +
10501 +struct au_nhash {
10502 +       unsigned int            nh_num;
10503 +       struct hlist_head       *nh_head;
10504 +};
10505 +
10506 +struct au_vdir_destr {
10507 +       unsigned char   len;
10508 +       unsigned char   name[0];
10509 +} __packed;
10510 +
10511 +struct au_vdir_dehstr {
10512 +       struct hlist_node       hash;
10513 +       struct au_vdir_destr    *str;
10514 +} ____cacheline_aligned_in_smp;
10515 +
10516 +struct au_vdir_de {
10517 +       ino_t                   de_ino;
10518 +       unsigned char           de_type;
10519 +       /* caution: packed */
10520 +       struct au_vdir_destr    de_str;
10521 +} __packed;
10522 +
10523 +struct au_vdir_wh {
10524 +       struct hlist_node       wh_hash;
10525 +#ifdef CONFIG_AUFS_SHWH
10526 +       ino_t                   wh_ino;
10527 +       aufs_bindex_t           wh_bindex;
10528 +       unsigned char           wh_type;
10529 +#else
10530 +       aufs_bindex_t           wh_bindex;
10531 +#endif
10532 +       /* caution: packed */
10533 +       struct au_vdir_destr    wh_str;
10534 +} __packed;
10535 +
10536 +union au_vdir_deblk_p {
10537 +       unsigned char           *deblk;
10538 +       struct au_vdir_de       *de;
10539 +};
10540 +
10541 +struct au_vdir {
10542 +       unsigned char   **vd_deblk;
10543 +       unsigned long   vd_nblk;
10544 +       struct {
10545 +               unsigned long           ul;
10546 +               union au_vdir_deblk_p   p;
10547 +       } vd_last;
10548 +
10549 +       unsigned long   vd_version;
10550 +       unsigned int    vd_deblk_sz;
10551 +       unsigned long           vd_jiffy;
10552 +} ____cacheline_aligned_in_smp;
10553 +
10554 +/* ---------------------------------------------------------------------- */
10555 +
10556 +/* dir.c */
10557 +extern const struct file_operations aufs_dir_fop;
10558 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10559 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10560 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10561 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10562 +int au_test_empty_lower(struct dentry *dentry);
10563 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10564 +
10565 +/* vdir.c */
10566 +unsigned int au_rdhash_est(loff_t sz);
10567 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10568 +void au_nhash_wh_free(struct au_nhash *whlist);
10569 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10570 +                           int limit);
10571 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10572 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10573 +                      unsigned int d_type, aufs_bindex_t bindex,
10574 +                      unsigned char shwh);
10575 +void au_vdir_free(struct au_vdir *vdir);
10576 +int au_vdir_init(struct file *file);
10577 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10578 +
10579 +/* ioctl.c */
10580 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10581 +
10582 +#ifdef CONFIG_AUFS_RDU
10583 +/* rdu.c */
10584 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10585 +#ifdef CONFIG_COMPAT
10586 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10587 +                        unsigned long arg);
10588 +#endif
10589 +#else
10590 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10591 +       unsigned int cmd, unsigned long arg)
10592 +#ifdef CONFIG_COMPAT
10593 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10594 +       unsigned int cmd, unsigned long arg)
10595 +#endif
10596 +#endif
10597 +
10598 +#endif /* __KERNEL__ */
10599 +#endif /* __AUFS_DIR_H__ */
10600 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10601 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10602 +++ linux/fs/aufs/dynop.c       2017-07-29 12:14:25.899708630 +0200
10603 @@ -0,0 +1,371 @@
10604 +/*
10605 + * Copyright (C) 2010-2017 Junjiro R. Okajima
10606 + *
10607 + * This program, aufs is free software; you can redistribute it and/or modify
10608 + * it under the terms of the GNU General Public License as published by
10609 + * the Free Software Foundation; either version 2 of the License, or
10610 + * (at your option) any later version.
10611 + *
10612 + * This program is distributed in the hope that it will be useful,
10613 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10614 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10615 + * GNU General Public License for more details.
10616 + *
10617 + * You should have received a copy of the GNU General Public License
10618 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10619 + */
10620 +
10621 +/*
10622 + * dynamically customizable operations for regular files
10623 + */
10624 +
10625 +#include "aufs.h"
10626 +
10627 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10628 +
10629 +/*
10630 + * How large will these lists be?
10631 + * Usually just a few elements, 20-30 at most for each, I guess.
10632 + */
10633 +static struct au_sphlhead dynop[AuDyLast];
10634 +
10635 +static struct au_dykey *dy_gfind_get(struct au_sphlhead *sphl, const void *h_op)
10636 +{
10637 +       struct au_dykey *key, *tmp;
10638 +       struct hlist_head *head;
10639 +
10640 +       key = NULL;
10641 +       head = &sphl->head;
10642 +       rcu_read_lock();
10643 +       hlist_for_each_entry_rcu(tmp, head, dk_hnode)
10644 +               if (tmp->dk_op.dy_hop == h_op) {
10645 +                       key = tmp;
10646 +                       kref_get(&key->dk_kref);
10647 +                       break;
10648 +               }
10649 +       rcu_read_unlock();
10650 +
10651 +       return key;
10652 +}
10653 +
10654 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10655 +{
10656 +       struct au_dykey **k, *found;
10657 +       const void *h_op = key->dk_op.dy_hop;
10658 +       int i;
10659 +
10660 +       found = NULL;
10661 +       k = br->br_dykey;
10662 +       for (i = 0; i < AuBrDynOp; i++)
10663 +               if (k[i]) {
10664 +                       if (k[i]->dk_op.dy_hop == h_op) {
10665 +                               found = k[i];
10666 +                               break;
10667 +                       }
10668 +               } else
10669 +                       break;
10670 +       if (!found) {
10671 +               spin_lock(&br->br_dykey_lock);
10672 +               for (; i < AuBrDynOp; i++)
10673 +                       if (k[i]) {
10674 +                               if (k[i]->dk_op.dy_hop == h_op) {
10675 +                                       found = k[i];
10676 +                                       break;
10677 +                               }
10678 +                       } else {
10679 +                               k[i] = key;
10680 +                               break;
10681 +                       }
10682 +               spin_unlock(&br->br_dykey_lock);
10683 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10684 +       }
10685 +
10686 +       return found;
10687 +}
10688 +
10689 +/* kref_get() if @key is already added */
10690 +static struct au_dykey *dy_gadd(struct au_sphlhead *sphl, struct au_dykey *key)
10691 +{
10692 +       struct au_dykey *tmp, *found;
10693 +       struct hlist_head *head;
10694 +       const void *h_op = key->dk_op.dy_hop;
10695 +
10696 +       found = NULL;
10697 +       head = &sphl->head;
10698 +       spin_lock(&sphl->spin);
10699 +       hlist_for_each_entry(tmp, head, dk_hnode)
10700 +               if (tmp->dk_op.dy_hop == h_op) {
10701 +                       kref_get(&tmp->dk_kref);
10702 +                       found = tmp;
10703 +                       break;
10704 +               }
10705 +       if (!found)
10706 +               hlist_add_head_rcu(&key->dk_hnode, head);
10707 +       spin_unlock(&sphl->spin);
10708 +
10709 +       if (!found)
10710 +               DyPrSym(key);
10711 +       return found;
10712 +}
10713 +
10714 +static void dy_free_rcu(struct rcu_head *rcu)
10715 +{
10716 +       struct au_dykey *key;
10717 +
10718 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10719 +       DyPrSym(key);
10720 +       kfree(key);
10721 +}
10722 +
10723 +static void dy_free(struct kref *kref)
10724 +{
10725 +       struct au_dykey *key;
10726 +       struct au_sphlhead *sphl;
10727 +
10728 +       key = container_of(kref, struct au_dykey, dk_kref);
10729 +       sphl = dynop + key->dk_op.dy_type;
10730 +       au_sphl_del_rcu(&key->dk_hnode, sphl);
10731 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10732 +}
10733 +
10734 +void au_dy_put(struct au_dykey *key)
10735 +{
10736 +       kref_put(&key->dk_kref, dy_free);
10737 +}
10738 +
10739 +/* ---------------------------------------------------------------------- */
10740 +
10741 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10742 +
10743 +#ifdef CONFIG_AUFS_DEBUG
10744 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10745 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10746 +#else
10747 +#define DyDbgDeclare(cnt)      do {} while (0)
10748 +#define DyDbgInc(cnt)          do {} while (0)
10749 +#endif
10750 +
10751 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10752 +       DyDbgInc(cnt);                                                  \
10753 +       if (h_op->func) {                                               \
10754 +               if (src.func)                                           \
10755 +                       dst.func = src.func;                            \
10756 +               else                                                    \
10757 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10758 +       }                                                               \
10759 +} while (0)
10760 +
10761 +#define DySetForce(func, dst, src) do {                \
10762 +       AuDebugOn(!src.func);                   \
10763 +       DyDbgInc(cnt);                          \
10764 +       dst.func = src.func;                    \
10765 +} while (0)
10766 +
10767 +#define DySetAop(func) \
10768 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10769 +#define DySetAopForce(func) \
10770 +       DySetForce(func, dyaop->da_op, aufs_aop)
10771 +
10772 +static void dy_aop(struct au_dykey *key, const void *h_op,
10773 +                  struct super_block *h_sb __maybe_unused)
10774 +{
10775 +       struct au_dyaop *dyaop = (void *)key;
10776 +       const struct address_space_operations *h_aop = h_op;
10777 +       DyDbgDeclare(cnt);
10778 +
10779 +       AuDbg("%s\n", au_sbtype(h_sb));
10780 +
10781 +       DySetAop(writepage);
10782 +       DySetAopForce(readpage);        /* force */
10783 +       DySetAop(writepages);
10784 +       DySetAop(set_page_dirty);
10785 +       DySetAop(readpages);
10786 +       DySetAop(write_begin);
10787 +       DySetAop(write_end);
10788 +       DySetAop(bmap);
10789 +       DySetAop(invalidatepage);
10790 +       DySetAop(releasepage);
10791 +       DySetAop(freepage);
10792 +       /* this one will be changed according to an aufs mount option */
10793 +       DySetAop(direct_IO);
10794 +       DySetAop(migratepage);
10795 +       DySetAop(isolate_page);
10796 +       DySetAop(putback_page);
10797 +       DySetAop(launder_page);
10798 +       DySetAop(is_partially_uptodate);
10799 +       DySetAop(is_dirty_writeback);
10800 +       DySetAop(error_remove_page);
10801 +       DySetAop(swap_activate);
10802 +       DySetAop(swap_deactivate);
10803 +
10804 +       DyDbgSize(cnt, *h_aop);
10805 +}
10806 +
10807 +/* ---------------------------------------------------------------------- */
10808 +
10809 +static void dy_bug(struct kref *kref)
10810 +{
10811 +       BUG();
10812 +}
10813 +
10814 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10815 +{
10816 +       struct au_dykey *key, *old;
10817 +       struct au_sphlhead *sphl;
10818 +       struct op {
10819 +               unsigned int sz;
10820 +               void (*set)(struct au_dykey *key, const void *h_op,
10821 +                           struct super_block *h_sb __maybe_unused);
10822 +       };
10823 +       static const struct op a[] = {
10824 +               [AuDy_AOP] = {
10825 +                       .sz     = sizeof(struct au_dyaop),
10826 +                       .set    = dy_aop
10827 +               }
10828 +       };
10829 +       const struct op *p;
10830 +
10831 +       sphl = dynop + op->dy_type;
10832 +       key = dy_gfind_get(sphl, op->dy_hop);
10833 +       if (key)
10834 +               goto out_add; /* success */
10835 +
10836 +       p = a + op->dy_type;
10837 +       key = kzalloc(p->sz, GFP_NOFS);
10838 +       if (unlikely(!key)) {
10839 +               key = ERR_PTR(-ENOMEM);
10840 +               goto out;
10841 +       }
10842 +
10843 +       key->dk_op.dy_hop = op->dy_hop;
10844 +       kref_init(&key->dk_kref);
10845 +       p->set(key, op->dy_hop, au_br_sb(br));
10846 +       old = dy_gadd(sphl, key);
10847 +       if (old) {
10848 +               kfree(key);
10849 +               key = old;
10850 +       }
10851 +
10852 +out_add:
10853 +       old = dy_bradd(br, key);
10854 +       if (old)
10855 +               /* its ref-count should never be zero here */
10856 +               kref_put(&key->dk_kref, dy_bug);
10857 +out:
10858 +       return key;
10859 +}
10860 +
10861 +/* ---------------------------------------------------------------------- */
10862 +/*
10863 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10864 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10865 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10866 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10867 + * See the aufs manual in detail.
10868 + */
10869 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10870 +{
10871 +       if (!do_dx)
10872 +               dyaop->da_op.direct_IO = NULL;
10873 +       else
10874 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10875 +}
10876 +
10877 +static struct au_dyaop *dy_aget(struct au_branch *br,
10878 +                               const struct address_space_operations *h_aop,
10879 +                               int do_dx)
10880 +{
10881 +       struct au_dyaop *dyaop;
10882 +       struct au_dynop op;
10883 +
10884 +       op.dy_type = AuDy_AOP;
10885 +       op.dy_haop = h_aop;
10886 +       dyaop = (void *)dy_get(&op, br);
10887 +       if (IS_ERR(dyaop))
10888 +               goto out;
10889 +       dy_adx(dyaop, do_dx);
10890 +
10891 +out:
10892 +       return dyaop;
10893 +}
10894 +
10895 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10896 +               struct inode *h_inode)
10897 +{
10898 +       int err, do_dx;
10899 +       struct super_block *sb;
10900 +       struct au_branch *br;
10901 +       struct au_dyaop *dyaop;
10902 +
10903 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10904 +       IiMustWriteLock(inode);
10905 +
10906 +       sb = inode->i_sb;
10907 +       br = au_sbr(sb, bindex);
10908 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10909 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10910 +       err = PTR_ERR(dyaop);
10911 +       if (IS_ERR(dyaop))
10912 +               /* unnecessary to call dy_fput() */
10913 +               goto out;
10914 +
10915 +       err = 0;
10916 +       inode->i_mapping->a_ops = &dyaop->da_op;
10917 +
10918 +out:
10919 +       return err;
10920 +}
10921 +
10922 +/*
10923 + * Is it safe to replace a_ops during the inode/file is in operation?
10924 + * Yes, I hope so.
10925 + */
10926 +int au_dy_irefresh(struct inode *inode)
10927 +{
10928 +       int err;
10929 +       aufs_bindex_t btop;
10930 +       struct inode *h_inode;
10931 +
10932 +       err = 0;
10933 +       if (S_ISREG(inode->i_mode)) {
10934 +               btop = au_ibtop(inode);
10935 +               h_inode = au_h_iptr(inode, btop);
10936 +               err = au_dy_iaop(inode, btop, h_inode);
10937 +       }
10938 +       return err;
10939 +}
10940 +
10941 +void au_dy_arefresh(int do_dx)
10942 +{
10943 +       struct au_sphlhead *sphl;
10944 +       struct hlist_head *head;
10945 +       struct au_dykey *key;
10946 +
10947 +       sphl = dynop + AuDy_AOP;
10948 +       head = &sphl->head;
10949 +       spin_lock(&sphl->spin);
10950 +       hlist_for_each_entry(key, head, dk_hnode)
10951 +               dy_adx((void *)key, do_dx);
10952 +       spin_unlock(&sphl->spin);
10953 +}
10954 +
10955 +/* ---------------------------------------------------------------------- */
10956 +
10957 +void __init au_dy_init(void)
10958 +{
10959 +       int i;
10960 +
10961 +       /* make sure that 'struct au_dykey *' can be any type */
10962 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10963 +
10964 +       for (i = 0; i < AuDyLast; i++)
10965 +               au_sphl_init(dynop + i);
10966 +}
10967 +
10968 +void au_dy_fin(void)
10969 +{
10970 +       int i;
10971 +
10972 +       for (i = 0; i < AuDyLast; i++)
10973 +               WARN_ON(!hlist_empty(&dynop[i].head));
10974 +}
10975 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10976 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10977 +++ linux/fs/aufs/dynop.h       2017-07-29 12:14:25.899708630 +0200
10978 @@ -0,0 +1,74 @@
10979 +/*
10980 + * Copyright (C) 2010-2017 Junjiro R. Okajima
10981 + *
10982 + * This program, aufs is free software; you can redistribute it and/or modify
10983 + * it under the terms of the GNU General Public License as published by
10984 + * the Free Software Foundation; either version 2 of the License, or
10985 + * (at your option) any later version.
10986 + *
10987 + * This program is distributed in the hope that it will be useful,
10988 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10989 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10990 + * GNU General Public License for more details.
10991 + *
10992 + * You should have received a copy of the GNU General Public License
10993 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10994 + */
10995 +
10996 +/*
10997 + * dynamically customizable operations (for regular files only)
10998 + */
10999 +
11000 +#ifndef __AUFS_DYNOP_H__
11001 +#define __AUFS_DYNOP_H__
11002 +
11003 +#ifdef __KERNEL__
11004 +
11005 +#include <linux/fs.h>
11006 +#include <linux/kref.h>
11007 +
11008 +enum {AuDy_AOP, AuDyLast};
11009 +
11010 +struct au_dynop {
11011 +       int                                             dy_type;
11012 +       union {
11013 +               const void                              *dy_hop;
11014 +               const struct address_space_operations   *dy_haop;
11015 +       };
11016 +};
11017 +
11018 +struct au_dykey {
11019 +       union {
11020 +               struct hlist_node       dk_hnode;
11021 +               struct rcu_head         dk_rcu;
11022 +       };
11023 +       struct au_dynop         dk_op;
11024 +
11025 +       /*
11026 +        * during I am in the branch local array, kref is gotten. when the
11027 +        * branch is removed, kref is put.
11028 +        */
11029 +       struct kref             dk_kref;
11030 +};
11031 +
11032 +/* stop unioning since their sizes are very different from each other */
11033 +struct au_dyaop {
11034 +       struct au_dykey                 da_key;
11035 +       struct address_space_operations da_op; /* not const */
11036 +};
11037 +
11038 +/* ---------------------------------------------------------------------- */
11039 +
11040 +/* dynop.c */
11041 +struct au_branch;
11042 +void au_dy_put(struct au_dykey *key);
11043 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
11044 +               struct inode *h_inode);
11045 +int au_dy_irefresh(struct inode *inode);
11046 +void au_dy_arefresh(int do_dio);
11047 +
11048 +void __init au_dy_init(void);
11049 +void au_dy_fin(void);
11050 +
11051 +#endif /* __KERNEL__ */
11052 +#endif /* __AUFS_DYNOP_H__ */
11053 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
11054 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
11055 +++ linux/fs/aufs/export.c      2017-07-29 12:14:25.903042072 +0200
11056 @@ -0,0 +1,836 @@
11057 +/*
11058 + * Copyright (C) 2005-2017 Junjiro R. Okajima
11059 + *
11060 + * This program, aufs is free software; you can redistribute it and/or modify
11061 + * it under the terms of the GNU General Public License as published by
11062 + * the Free Software Foundation; either version 2 of the License, or
11063 + * (at your option) any later version.
11064 + *
11065 + * This program is distributed in the hope that it will be useful,
11066 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11067 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11068 + * GNU General Public License for more details.
11069 + *
11070 + * You should have received a copy of the GNU General Public License
11071 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11072 + */
11073 +
11074 +/*
11075 + * export via nfs
11076 + */
11077 +
11078 +#include <linux/exportfs.h>
11079 +#include <linux/fs_struct.h>
11080 +#include <linux/namei.h>
11081 +#include <linux/nsproxy.h>
11082 +#include <linux/random.h>
11083 +#include <linux/writeback.h>
11084 +#include "aufs.h"
11085 +
11086 +union conv {
11087 +#ifdef CONFIG_AUFS_INO_T_64
11088 +       __u32 a[2];
11089 +#else
11090 +       __u32 a[1];
11091 +#endif
11092 +       ino_t ino;
11093 +};
11094 +
11095 +static ino_t decode_ino(__u32 *a)
11096 +{
11097 +       union conv u;
11098 +
11099 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
11100 +       u.a[0] = a[0];
11101 +#ifdef CONFIG_AUFS_INO_T_64
11102 +       u.a[1] = a[1];
11103 +#endif
11104 +       return u.ino;
11105 +}
11106 +
11107 +static void encode_ino(__u32 *a, ino_t ino)
11108 +{
11109 +       union conv u;
11110 +
11111 +       u.ino = ino;
11112 +       a[0] = u.a[0];
11113 +#ifdef CONFIG_AUFS_INO_T_64
11114 +       a[1] = u.a[1];
11115 +#endif
11116 +}
11117 +
11118 +/* NFS file handle */
11119 +enum {
11120 +       Fh_br_id,
11121 +       Fh_sigen,
11122 +#ifdef CONFIG_AUFS_INO_T_64
11123 +       /* support 64bit inode number */
11124 +       Fh_ino1,
11125 +       Fh_ino2,
11126 +       Fh_dir_ino1,
11127 +       Fh_dir_ino2,
11128 +#else
11129 +       Fh_ino1,
11130 +       Fh_dir_ino1,
11131 +#endif
11132 +       Fh_igen,
11133 +       Fh_h_type,
11134 +       Fh_tail,
11135 +
11136 +       Fh_ino = Fh_ino1,
11137 +       Fh_dir_ino = Fh_dir_ino1
11138 +};
11139 +
11140 +static int au_test_anon(struct dentry *dentry)
11141 +{
11142 +       /* note: read d_flags without d_lock */
11143 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
11144 +}
11145 +
11146 +int au_test_nfsd(void)
11147 +{
11148 +       int ret;
11149 +       struct task_struct *tsk = current;
11150 +       char comm[sizeof(tsk->comm)];
11151 +
11152 +       ret = 0;
11153 +       if (tsk->flags & PF_KTHREAD) {
11154 +               get_task_comm(comm, tsk);
11155 +               ret = !strcmp(comm, "nfsd");
11156 +       }
11157 +
11158 +       return ret;
11159 +}
11160 +
11161 +/* ---------------------------------------------------------------------- */
11162 +/* inode generation external table */
11163 +
11164 +void au_xigen_inc(struct inode *inode)
11165 +{
11166 +       loff_t pos;
11167 +       ssize_t sz;
11168 +       __u32 igen;
11169 +       struct super_block *sb;
11170 +       struct au_sbinfo *sbinfo;
11171 +
11172 +       sb = inode->i_sb;
11173 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
11174 +
11175 +       sbinfo = au_sbi(sb);
11176 +       pos = inode->i_ino;
11177 +       pos *= sizeof(igen);
11178 +       igen = inode->i_generation + 1;
11179 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
11180 +                        sizeof(igen), &pos);
11181 +       if (sz == sizeof(igen))
11182 +               return; /* success */
11183 +
11184 +       if (unlikely(sz >= 0))
11185 +               AuIOErr("xigen error (%zd)\n", sz);
11186 +}
11187 +
11188 +int au_xigen_new(struct inode *inode)
11189 +{
11190 +       int err;
11191 +       loff_t pos;
11192 +       ssize_t sz;
11193 +       struct super_block *sb;
11194 +       struct au_sbinfo *sbinfo;
11195 +       struct file *file;
11196 +
11197 +       err = 0;
11198 +       /* todo: dirty, at mount time */
11199 +       if (inode->i_ino == AUFS_ROOT_INO)
11200 +               goto out;
11201 +       sb = inode->i_sb;
11202 +       SiMustAnyLock(sb);
11203 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11204 +               goto out;
11205 +
11206 +       err = -EFBIG;
11207 +       pos = inode->i_ino;
11208 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
11209 +               AuIOErr1("too large i%lld\n", pos);
11210 +               goto out;
11211 +       }
11212 +       pos *= sizeof(inode->i_generation);
11213 +
11214 +       err = 0;
11215 +       sbinfo = au_sbi(sb);
11216 +       file = sbinfo->si_xigen;
11217 +       BUG_ON(!file);
11218 +
11219 +       if (vfsub_f_size_read(file)
11220 +           < pos + sizeof(inode->i_generation)) {
11221 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
11222 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
11223 +                                sizeof(inode->i_generation), &pos);
11224 +       } else
11225 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
11226 +                               sizeof(inode->i_generation), &pos);
11227 +       if (sz == sizeof(inode->i_generation))
11228 +               goto out; /* success */
11229 +
11230 +       err = sz;
11231 +       if (unlikely(sz >= 0)) {
11232 +               err = -EIO;
11233 +               AuIOErr("xigen error (%zd)\n", sz);
11234 +       }
11235 +
11236 +out:
11237 +       return err;
11238 +}
11239 +
11240 +int au_xigen_set(struct super_block *sb, struct file *base)
11241 +{
11242 +       int err;
11243 +       struct au_sbinfo *sbinfo;
11244 +       struct file *file;
11245 +
11246 +       SiMustWriteLock(sb);
11247 +
11248 +       sbinfo = au_sbi(sb);
11249 +       file = au_xino_create2(base, sbinfo->si_xigen);
11250 +       err = PTR_ERR(file);
11251 +       if (IS_ERR(file))
11252 +               goto out;
11253 +       err = 0;
11254 +       if (sbinfo->si_xigen)
11255 +               fput(sbinfo->si_xigen);
11256 +       sbinfo->si_xigen = file;
11257 +
11258 +out:
11259 +       return err;
11260 +}
11261 +
11262 +void au_xigen_clr(struct super_block *sb)
11263 +{
11264 +       struct au_sbinfo *sbinfo;
11265 +
11266 +       SiMustWriteLock(sb);
11267 +
11268 +       sbinfo = au_sbi(sb);
11269 +       if (sbinfo->si_xigen) {
11270 +               fput(sbinfo->si_xigen);
11271 +               sbinfo->si_xigen = NULL;
11272 +       }
11273 +}
11274 +
11275 +/* ---------------------------------------------------------------------- */
11276 +
11277 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
11278 +                                   ino_t dir_ino)
11279 +{
11280 +       struct dentry *dentry, *d;
11281 +       struct inode *inode;
11282 +       unsigned int sigen;
11283 +
11284 +       dentry = NULL;
11285 +       inode = ilookup(sb, ino);
11286 +       if (!inode)
11287 +               goto out;
11288 +
11289 +       dentry = ERR_PTR(-ESTALE);
11290 +       sigen = au_sigen(sb);
11291 +       if (unlikely(au_is_bad_inode(inode)
11292 +                    || IS_DEADDIR(inode)
11293 +                    || sigen != au_iigen(inode, NULL)))
11294 +               goto out_iput;
11295 +
11296 +       dentry = NULL;
11297 +       if (!dir_ino || S_ISDIR(inode->i_mode))
11298 +               dentry = d_find_alias(inode);
11299 +       else {
11300 +               spin_lock(&inode->i_lock);
11301 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
11302 +                       spin_lock(&d->d_lock);
11303 +                       if (!au_test_anon(d)
11304 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
11305 +                               dentry = dget_dlock(d);
11306 +                               spin_unlock(&d->d_lock);
11307 +                               break;
11308 +                       }
11309 +                       spin_unlock(&d->d_lock);
11310 +               }
11311 +               spin_unlock(&inode->i_lock);
11312 +       }
11313 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
11314 +               /* need to refresh */
11315 +               dput(dentry);
11316 +               dentry = NULL;
11317 +       }
11318 +
11319 +out_iput:
11320 +       iput(inode);
11321 +out:
11322 +       AuTraceErrPtr(dentry);
11323 +       return dentry;
11324 +}
11325 +
11326 +/* ---------------------------------------------------------------------- */
11327 +
11328 +/* todo: dirty? */
11329 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
11330 +
11331 +struct au_compare_mnt_args {
11332 +       /* input */
11333 +       struct super_block *sb;
11334 +
11335 +       /* output */
11336 +       struct vfsmount *mnt;
11337 +};
11338 +
11339 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
11340 +{
11341 +       struct au_compare_mnt_args *a = arg;
11342 +
11343 +       if (mnt->mnt_sb != a->sb)
11344 +               return 0;
11345 +       a->mnt = mntget(mnt);
11346 +       return 1;
11347 +}
11348 +
11349 +static struct vfsmount *au_mnt_get(struct super_block *sb)
11350 +{
11351 +       int err;
11352 +       struct path root;
11353 +       struct au_compare_mnt_args args = {
11354 +               .sb = sb
11355 +       };
11356 +
11357 +       get_fs_root(current->fs, &root);
11358 +       rcu_read_lock();
11359 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
11360 +       rcu_read_unlock();
11361 +       path_put(&root);
11362 +       AuDebugOn(!err);
11363 +       AuDebugOn(!args.mnt);
11364 +       return args.mnt;
11365 +}
11366 +
11367 +struct au_nfsd_si_lock {
11368 +       unsigned int sigen;
11369 +       aufs_bindex_t bindex, br_id;
11370 +       unsigned char force_lock;
11371 +};
11372 +
11373 +static int si_nfsd_read_lock(struct super_block *sb,
11374 +                            struct au_nfsd_si_lock *nsi_lock)
11375 +{
11376 +       int err;
11377 +       aufs_bindex_t bindex;
11378 +
11379 +       si_read_lock(sb, AuLock_FLUSH);
11380 +
11381 +       /* branch id may be wrapped around */
11382 +       err = 0;
11383 +       bindex = au_br_index(sb, nsi_lock->br_id);
11384 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11385 +               goto out; /* success */
11386 +
11387 +       err = -ESTALE;
11388 +       bindex = -1;
11389 +       if (!nsi_lock->force_lock)
11390 +               si_read_unlock(sb);
11391 +
11392 +out:
11393 +       nsi_lock->bindex = bindex;
11394 +       return err;
11395 +}
11396 +
11397 +struct find_name_by_ino {
11398 +       struct dir_context ctx;
11399 +       int called, found;
11400 +       ino_t ino;
11401 +       char *name;
11402 +       int namelen;
11403 +};
11404 +
11405 +static int
11406 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11407 +                loff_t offset, u64 ino, unsigned int d_type)
11408 +{
11409 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11410 +                                                 ctx);
11411 +
11412 +       a->called++;
11413 +       if (a->ino != ino)
11414 +               return 0;
11415 +
11416 +       memcpy(a->name, name, namelen);
11417 +       a->namelen = namelen;
11418 +       a->found = 1;
11419 +       return 1;
11420 +}
11421 +
11422 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11423 +                                    struct au_nfsd_si_lock *nsi_lock)
11424 +{
11425 +       struct dentry *dentry, *parent;
11426 +       struct file *file;
11427 +       struct inode *dir;
11428 +       struct find_name_by_ino arg = {
11429 +               .ctx = {
11430 +                       .actor = find_name_by_ino
11431 +               }
11432 +       };
11433 +       int err;
11434 +
11435 +       parent = path->dentry;
11436 +       if (nsi_lock)
11437 +               si_read_unlock(parent->d_sb);
11438 +       file = vfsub_dentry_open(path, au_dir_roflags);
11439 +       dentry = (void *)file;
11440 +       if (IS_ERR(file))
11441 +               goto out;
11442 +
11443 +       dentry = ERR_PTR(-ENOMEM);
11444 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11445 +       if (unlikely(!arg.name))
11446 +               goto out_file;
11447 +       arg.ino = ino;
11448 +       arg.found = 0;
11449 +       do {
11450 +               arg.called = 0;
11451 +               /* smp_mb(); */
11452 +               err = vfsub_iterate_dir(file, &arg.ctx);
11453 +       } while (!err && !arg.found && arg.called);
11454 +       dentry = ERR_PTR(err);
11455 +       if (unlikely(err))
11456 +               goto out_name;
11457 +       /* instead of ENOENT */
11458 +       dentry = ERR_PTR(-ESTALE);
11459 +       if (!arg.found)
11460 +               goto out_name;
11461 +
11462 +       /* do not call vfsub_lkup_one() */
11463 +       dir = d_inode(parent);
11464 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
11465 +       AuTraceErrPtr(dentry);
11466 +       if (IS_ERR(dentry))
11467 +               goto out_name;
11468 +       AuDebugOn(au_test_anon(dentry));
11469 +       if (unlikely(d_really_is_negative(dentry))) {
11470 +               dput(dentry);
11471 +               dentry = ERR_PTR(-ENOENT);
11472 +       }
11473 +
11474 +out_name:
11475 +       free_page((unsigned long)arg.name);
11476 +out_file:
11477 +       fput(file);
11478 +out:
11479 +       if (unlikely(nsi_lock
11480 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11481 +               if (!IS_ERR(dentry)) {
11482 +                       dput(dentry);
11483 +                       dentry = ERR_PTR(-ESTALE);
11484 +               }
11485 +       AuTraceErrPtr(dentry);
11486 +       return dentry;
11487 +}
11488 +
11489 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11490 +                                       ino_t dir_ino,
11491 +                                       struct au_nfsd_si_lock *nsi_lock)
11492 +{
11493 +       struct dentry *dentry;
11494 +       struct path path;
11495 +
11496 +       if (dir_ino != AUFS_ROOT_INO) {
11497 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11498 +               dentry = path.dentry;
11499 +               if (!path.dentry || IS_ERR(path.dentry))
11500 +                       goto out;
11501 +               AuDebugOn(au_test_anon(path.dentry));
11502 +       } else
11503 +               path.dentry = dget(sb->s_root);
11504 +
11505 +       path.mnt = au_mnt_get(sb);
11506 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11507 +       path_put(&path);
11508 +
11509 +out:
11510 +       AuTraceErrPtr(dentry);
11511 +       return dentry;
11512 +}
11513 +
11514 +/* ---------------------------------------------------------------------- */
11515 +
11516 +static int h_acceptable(void *expv, struct dentry *dentry)
11517 +{
11518 +       return 1;
11519 +}
11520 +
11521 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11522 +                          char *buf, int len, struct super_block *sb)
11523 +{
11524 +       char *p;
11525 +       int n;
11526 +       struct path path;
11527 +
11528 +       p = d_path(h_rootpath, buf, len);
11529 +       if (IS_ERR(p))
11530 +               goto out;
11531 +       n = strlen(p);
11532 +
11533 +       path.mnt = h_rootpath->mnt;
11534 +       path.dentry = h_parent;
11535 +       p = d_path(&path, buf, len);
11536 +       if (IS_ERR(p))
11537 +               goto out;
11538 +       if (n != 1)
11539 +               p += n;
11540 +
11541 +       path.mnt = au_mnt_get(sb);
11542 +       path.dentry = sb->s_root;
11543 +       p = d_path(&path, buf, len - strlen(p));
11544 +       mntput(path.mnt);
11545 +       if (IS_ERR(p))
11546 +               goto out;
11547 +       if (n != 1)
11548 +               p[strlen(p)] = '/';
11549 +
11550 +out:
11551 +       AuTraceErrPtr(p);
11552 +       return p;
11553 +}
11554 +
11555 +static
11556 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11557 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11558 +{
11559 +       struct dentry *dentry, *h_parent, *root;
11560 +       struct super_block *h_sb;
11561 +       char *pathname, *p;
11562 +       struct vfsmount *h_mnt;
11563 +       struct au_branch *br;
11564 +       int err;
11565 +       struct path path;
11566 +
11567 +       br = au_sbr(sb, nsi_lock->bindex);
11568 +       h_mnt = au_br_mnt(br);
11569 +       h_sb = h_mnt->mnt_sb;
11570 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11571 +       lockdep_off();
11572 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11573 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11574 +                                     h_acceptable, /*context*/NULL);
11575 +       lockdep_on();
11576 +       dentry = h_parent;
11577 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11578 +               AuWarn1("%s decode_fh failed, %ld\n",
11579 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11580 +               goto out;
11581 +       }
11582 +       dentry = NULL;
11583 +       if (unlikely(au_test_anon(h_parent))) {
11584 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11585 +                       au_sbtype(h_sb));
11586 +               goto out_h_parent;
11587 +       }
11588 +
11589 +       dentry = ERR_PTR(-ENOMEM);
11590 +       pathname = (void *)__get_free_page(GFP_NOFS);
11591 +       if (unlikely(!pathname))
11592 +               goto out_h_parent;
11593 +
11594 +       root = sb->s_root;
11595 +       path.mnt = h_mnt;
11596 +       di_read_lock_parent(root, !AuLock_IR);
11597 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11598 +       di_read_unlock(root, !AuLock_IR);
11599 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11600 +       dentry = (void *)p;
11601 +       if (IS_ERR(p))
11602 +               goto out_pathname;
11603 +
11604 +       si_read_unlock(sb);
11605 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11606 +       dentry = ERR_PTR(err);
11607 +       if (unlikely(err))
11608 +               goto out_relock;
11609 +
11610 +       dentry = ERR_PTR(-ENOENT);
11611 +       AuDebugOn(au_test_anon(path.dentry));
11612 +       if (unlikely(d_really_is_negative(path.dentry)))
11613 +               goto out_path;
11614 +
11615 +       if (ino != d_inode(path.dentry)->i_ino)
11616 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11617 +       else
11618 +               dentry = dget(path.dentry);
11619 +
11620 +out_path:
11621 +       path_put(&path);
11622 +out_relock:
11623 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11624 +               if (!IS_ERR(dentry)) {
11625 +                       dput(dentry);
11626 +                       dentry = ERR_PTR(-ESTALE);
11627 +               }
11628 +out_pathname:
11629 +       free_page((unsigned long)pathname);
11630 +out_h_parent:
11631 +       dput(h_parent);
11632 +out:
11633 +       AuTraceErrPtr(dentry);
11634 +       return dentry;
11635 +}
11636 +
11637 +/* ---------------------------------------------------------------------- */
11638 +
11639 +static struct dentry *
11640 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11641 +                 int fh_type)
11642 +{
11643 +       struct dentry *dentry;
11644 +       __u32 *fh = fid->raw;
11645 +       struct au_branch *br;
11646 +       ino_t ino, dir_ino;
11647 +       struct au_nfsd_si_lock nsi_lock = {
11648 +               .force_lock     = 0
11649 +       };
11650 +
11651 +       dentry = ERR_PTR(-ESTALE);
11652 +       /* it should never happen, but the file handle is unreliable */
11653 +       if (unlikely(fh_len < Fh_tail))
11654 +               goto out;
11655 +       nsi_lock.sigen = fh[Fh_sigen];
11656 +       nsi_lock.br_id = fh[Fh_br_id];
11657 +
11658 +       /* branch id may be wrapped around */
11659 +       br = NULL;
11660 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11661 +               goto out;
11662 +       nsi_lock.force_lock = 1;
11663 +
11664 +       /* is this inode still cached? */
11665 +       ino = decode_ino(fh + Fh_ino);
11666 +       /* it should never happen */
11667 +       if (unlikely(ino == AUFS_ROOT_INO))
11668 +               goto out_unlock;
11669 +
11670 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11671 +       dentry = decode_by_ino(sb, ino, dir_ino);
11672 +       if (IS_ERR(dentry))
11673 +               goto out_unlock;
11674 +       if (dentry)
11675 +               goto accept;
11676 +
11677 +       /* is the parent dir cached? */
11678 +       br = au_sbr(sb, nsi_lock.bindex);
11679 +       au_br_get(br);
11680 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11681 +       if (IS_ERR(dentry))
11682 +               goto out_unlock;
11683 +       if (dentry)
11684 +               goto accept;
11685 +
11686 +       /* lookup path */
11687 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11688 +       if (IS_ERR(dentry))
11689 +               goto out_unlock;
11690 +       if (unlikely(!dentry))
11691 +               /* todo?: make it ESTALE */
11692 +               goto out_unlock;
11693 +
11694 +accept:
11695 +       if (!au_digen_test(dentry, au_sigen(sb))
11696 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
11697 +               goto out_unlock; /* success */
11698 +
11699 +       dput(dentry);
11700 +       dentry = ERR_PTR(-ESTALE);
11701 +out_unlock:
11702 +       if (br)
11703 +               au_br_put(br);
11704 +       si_read_unlock(sb);
11705 +out:
11706 +       AuTraceErrPtr(dentry);
11707 +       return dentry;
11708 +}
11709 +
11710 +#if 0 /* reserved for future use */
11711 +/* support subtreecheck option */
11712 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11713 +                                       int fh_len, int fh_type)
11714 +{
11715 +       struct dentry *parent;
11716 +       __u32 *fh = fid->raw;
11717 +       ino_t dir_ino;
11718 +
11719 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11720 +       parent = decode_by_ino(sb, dir_ino, 0);
11721 +       if (IS_ERR(parent))
11722 +               goto out;
11723 +       if (!parent)
11724 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11725 +                                       dir_ino, fh, fh_len);
11726 +
11727 +out:
11728 +       AuTraceErrPtr(parent);
11729 +       return parent;
11730 +}
11731 +#endif
11732 +
11733 +/* ---------------------------------------------------------------------- */
11734 +
11735 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11736 +                         struct inode *dir)
11737 +{
11738 +       int err;
11739 +       aufs_bindex_t bindex;
11740 +       struct super_block *sb, *h_sb;
11741 +       struct dentry *dentry, *parent, *h_parent;
11742 +       struct inode *h_dir;
11743 +       struct au_branch *br;
11744 +
11745 +       err = -ENOSPC;
11746 +       if (unlikely(*max_len <= Fh_tail)) {
11747 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11748 +               goto out;
11749 +       }
11750 +
11751 +       err = FILEID_ROOT;
11752 +       if (inode->i_ino == AUFS_ROOT_INO) {
11753 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11754 +               goto out;
11755 +       }
11756 +
11757 +       h_parent = NULL;
11758 +       sb = inode->i_sb;
11759 +       err = si_read_lock(sb, AuLock_FLUSH);
11760 +       if (unlikely(err))
11761 +               goto out;
11762 +
11763 +#ifdef CONFIG_AUFS_DEBUG
11764 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11765 +               AuWarn1("NFS-exporting requires xino\n");
11766 +#endif
11767 +       err = -EIO;
11768 +       parent = NULL;
11769 +       ii_read_lock_child(inode);
11770 +       bindex = au_ibtop(inode);
11771 +       if (!dir) {
11772 +               dentry = d_find_any_alias(inode);
11773 +               if (unlikely(!dentry))
11774 +                       goto out_unlock;
11775 +               AuDebugOn(au_test_anon(dentry));
11776 +               parent = dget_parent(dentry);
11777 +               dput(dentry);
11778 +               if (unlikely(!parent))
11779 +                       goto out_unlock;
11780 +               if (d_really_is_positive(parent))
11781 +                       dir = d_inode(parent);
11782 +       }
11783 +
11784 +       ii_read_lock_parent(dir);
11785 +       h_dir = au_h_iptr(dir, bindex);
11786 +       ii_read_unlock(dir);
11787 +       if (unlikely(!h_dir))
11788 +               goto out_parent;
11789 +       h_parent = d_find_any_alias(h_dir);
11790 +       if (unlikely(!h_parent))
11791 +               goto out_hparent;
11792 +
11793 +       err = -EPERM;
11794 +       br = au_sbr(sb, bindex);
11795 +       h_sb = au_br_sb(br);
11796 +       if (unlikely(!h_sb->s_export_op)) {
11797 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11798 +               goto out_hparent;
11799 +       }
11800 +
11801 +       fh[Fh_br_id] = br->br_id;
11802 +       fh[Fh_sigen] = au_sigen(sb);
11803 +       encode_ino(fh + Fh_ino, inode->i_ino);
11804 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11805 +       fh[Fh_igen] = inode->i_generation;
11806 +
11807 +       *max_len -= Fh_tail;
11808 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11809 +                                          max_len,
11810 +                                          /*connectable or subtreecheck*/0);
11811 +       err = fh[Fh_h_type];
11812 +       *max_len += Fh_tail;
11813 +       /* todo: macros? */
11814 +       if (err != FILEID_INVALID)
11815 +               err = 99;
11816 +       else
11817 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11818 +
11819 +out_hparent:
11820 +       dput(h_parent);
11821 +out_parent:
11822 +       dput(parent);
11823 +out_unlock:
11824 +       ii_read_unlock(inode);
11825 +       si_read_unlock(sb);
11826 +out:
11827 +       if (unlikely(err < 0))
11828 +               err = FILEID_INVALID;
11829 +       return err;
11830 +}
11831 +
11832 +/* ---------------------------------------------------------------------- */
11833 +
11834 +static int aufs_commit_metadata(struct inode *inode)
11835 +{
11836 +       int err;
11837 +       aufs_bindex_t bindex;
11838 +       struct super_block *sb;
11839 +       struct inode *h_inode;
11840 +       int (*f)(struct inode *inode);
11841 +
11842 +       sb = inode->i_sb;
11843 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11844 +       ii_write_lock_child(inode);
11845 +       bindex = au_ibtop(inode);
11846 +       AuDebugOn(bindex < 0);
11847 +       h_inode = au_h_iptr(inode, bindex);
11848 +
11849 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11850 +       if (f)
11851 +               err = f(h_inode);
11852 +       else {
11853 +               struct writeback_control wbc = {
11854 +                       .sync_mode      = WB_SYNC_ALL,
11855 +                       .nr_to_write    = 0 /* metadata only */
11856 +               };
11857 +
11858 +               err = sync_inode(h_inode, &wbc);
11859 +       }
11860 +
11861 +       au_cpup_attr_timesizes(inode);
11862 +       ii_write_unlock(inode);
11863 +       si_read_unlock(sb);
11864 +       return err;
11865 +}
11866 +
11867 +/* ---------------------------------------------------------------------- */
11868 +
11869 +static struct export_operations aufs_export_op = {
11870 +       .fh_to_dentry           = aufs_fh_to_dentry,
11871 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11872 +       .encode_fh              = aufs_encode_fh,
11873 +       .commit_metadata        = aufs_commit_metadata
11874 +};
11875 +
11876 +void au_export_init(struct super_block *sb)
11877 +{
11878 +       struct au_sbinfo *sbinfo;
11879 +       __u32 u;
11880 +
11881 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
11882 +                        && IS_MODULE(CONFIG_EXPORTFS),
11883 +                        AUFS_NAME ": unsupported configuration "
11884 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
11885 +
11886 +       sb->s_export_op = &aufs_export_op;
11887 +       sbinfo = au_sbi(sb);
11888 +       sbinfo->si_xigen = NULL;
11889 +       get_random_bytes(&u, sizeof(u));
11890 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11891 +       atomic_set(&sbinfo->si_xigen_next, u);
11892 +}
11893 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11894 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11895 +++ linux/fs/aufs/fhsm.c        2017-07-29 12:14:25.903042072 +0200
11896 @@ -0,0 +1,426 @@
11897 +/*
11898 + * Copyright (C) 2011-2017 Junjiro R. Okajima
11899 + *
11900 + * This program, aufs is free software; you can redistribute it and/or modify
11901 + * it under the terms of the GNU General Public License as published by
11902 + * the Free Software Foundation; either version 2 of the License, or
11903 + * (at your option) any later version.
11904 + *
11905 + * This program is distributed in the hope that it will be useful,
11906 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11907 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11908 + * GNU General Public License for more details.
11909 + *
11910 + * You should have received a copy of the GNU General Public License
11911 + * along with this program; if not, write to the Free Software
11912 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11913 + */
11914 +
11915 +/*
11916 + * File-based Hierarchy Storage Management
11917 + */
11918 +
11919 +#include <linux/anon_inodes.h>
11920 +#include <linux/poll.h>
11921 +#include <linux/seq_file.h>
11922 +#include <linux/statfs.h>
11923 +#include "aufs.h"
11924 +
11925 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11926 +{
11927 +       struct au_sbinfo *sbinfo;
11928 +       struct au_fhsm *fhsm;
11929 +
11930 +       SiMustAnyLock(sb);
11931 +
11932 +       sbinfo = au_sbi(sb);
11933 +       fhsm = &sbinfo->si_fhsm;
11934 +       AuDebugOn(!fhsm);
11935 +       return fhsm->fhsm_bottom;
11936 +}
11937 +
11938 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11939 +{
11940 +       struct au_sbinfo *sbinfo;
11941 +       struct au_fhsm *fhsm;
11942 +
11943 +       SiMustWriteLock(sb);
11944 +
11945 +       sbinfo = au_sbi(sb);
11946 +       fhsm = &sbinfo->si_fhsm;
11947 +       AuDebugOn(!fhsm);
11948 +       fhsm->fhsm_bottom = bindex;
11949 +}
11950 +
11951 +/* ---------------------------------------------------------------------- */
11952 +
11953 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11954 +{
11955 +       struct au_br_fhsm *bf;
11956 +
11957 +       bf = br->br_fhsm;
11958 +       MtxMustLock(&bf->bf_lock);
11959 +
11960 +       return !bf->bf_readable
11961 +               || time_after(jiffies,
11962 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11963 +}
11964 +
11965 +/* ---------------------------------------------------------------------- */
11966 +
11967 +static void au_fhsm_notify(struct super_block *sb, int val)
11968 +{
11969 +       struct au_sbinfo *sbinfo;
11970 +       struct au_fhsm *fhsm;
11971 +
11972 +       SiMustAnyLock(sb);
11973 +
11974 +       sbinfo = au_sbi(sb);
11975 +       fhsm = &sbinfo->si_fhsm;
11976 +       if (au_fhsm_pid(fhsm)
11977 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11978 +               atomic_set(&fhsm->fhsm_readable, val);
11979 +               if (val)
11980 +                       wake_up(&fhsm->fhsm_wqh);
11981 +       }
11982 +}
11983 +
11984 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11985 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11986 +{
11987 +       int err;
11988 +       struct au_branch *br;
11989 +       struct au_br_fhsm *bf;
11990 +
11991 +       br = au_sbr(sb, bindex);
11992 +       AuDebugOn(au_br_rdonly(br));
11993 +       bf = br->br_fhsm;
11994 +       AuDebugOn(!bf);
11995 +
11996 +       if (do_lock)
11997 +               mutex_lock(&bf->bf_lock);
11998 +       else
11999 +               MtxMustLock(&bf->bf_lock);
12000 +
12001 +       /* sb->s_root for NFS is unreliable */
12002 +       err = au_br_stfs(br, &bf->bf_stfs);
12003 +       if (unlikely(err)) {
12004 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
12005 +               goto out;
12006 +       }
12007 +
12008 +       bf->bf_jiffy = jiffies;
12009 +       bf->bf_readable = 1;
12010 +       if (do_notify)
12011 +               au_fhsm_notify(sb, /*val*/1);
12012 +       if (rstfs)
12013 +               *rstfs = bf->bf_stfs;
12014 +
12015 +out:
12016 +       if (do_lock)
12017 +               mutex_unlock(&bf->bf_lock);
12018 +       au_fhsm_notify(sb, /*val*/1);
12019 +
12020 +       return err;
12021 +}
12022 +
12023 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
12024 +{
12025 +       int err;
12026 +       struct au_sbinfo *sbinfo;
12027 +       struct au_fhsm *fhsm;
12028 +       struct au_branch *br;
12029 +       struct au_br_fhsm *bf;
12030 +
12031 +       AuDbg("b%d, force %d\n", bindex, force);
12032 +       SiMustAnyLock(sb);
12033 +
12034 +       sbinfo = au_sbi(sb);
12035 +       fhsm = &sbinfo->si_fhsm;
12036 +       if (!au_ftest_si(sbinfo, FHSM)
12037 +           || fhsm->fhsm_bottom == bindex)
12038 +               return;
12039 +
12040 +       br = au_sbr(sb, bindex);
12041 +       bf = br->br_fhsm;
12042 +       AuDebugOn(!bf);
12043 +       mutex_lock(&bf->bf_lock);
12044 +       if (force
12045 +           || au_fhsm_pid(fhsm)
12046 +           || au_fhsm_test_jiffy(sbinfo, br))
12047 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
12048 +                                 /*do_notify*/1);
12049 +       mutex_unlock(&bf->bf_lock);
12050 +}
12051 +
12052 +void au_fhsm_wrote_all(struct super_block *sb, int force)
12053 +{
12054 +       aufs_bindex_t bindex, bbot;
12055 +       struct au_branch *br;
12056 +
12057 +       /* exclude the bottom */
12058 +       bbot = au_fhsm_bottom(sb);
12059 +       for (bindex = 0; bindex < bbot; bindex++) {
12060 +               br = au_sbr(sb, bindex);
12061 +               if (au_br_fhsm(br->br_perm))
12062 +                       au_fhsm_wrote(sb, bindex, force);
12063 +       }
12064 +}
12065 +
12066 +/* ---------------------------------------------------------------------- */
12067 +
12068 +static unsigned int au_fhsm_poll(struct file *file,
12069 +                                struct poll_table_struct *wait)
12070 +{
12071 +       unsigned int mask;
12072 +       struct au_sbinfo *sbinfo;
12073 +       struct au_fhsm *fhsm;
12074 +
12075 +       mask = 0;
12076 +       sbinfo = file->private_data;
12077 +       fhsm = &sbinfo->si_fhsm;
12078 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
12079 +       if (atomic_read(&fhsm->fhsm_readable))
12080 +               mask = POLLIN /* | POLLRDNORM */;
12081 +
12082 +       AuTraceErr((int)mask);
12083 +       return mask;
12084 +}
12085 +
12086 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
12087 +                             struct aufs_stfs *stfs, __s16 brid)
12088 +{
12089 +       int err;
12090 +
12091 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
12092 +       if (!err)
12093 +               err = __put_user(brid, &stbr->brid);
12094 +       if (unlikely(err))
12095 +               err = -EFAULT;
12096 +
12097 +       return err;
12098 +}
12099 +
12100 +static ssize_t au_fhsm_do_read(struct super_block *sb,
12101 +                              struct aufs_stbr __user *stbr, size_t count)
12102 +{
12103 +       ssize_t err;
12104 +       int nstbr;
12105 +       aufs_bindex_t bindex, bbot;
12106 +       struct au_branch *br;
12107 +       struct au_br_fhsm *bf;
12108 +
12109 +       /* except the bottom branch */
12110 +       err = 0;
12111 +       nstbr = 0;
12112 +       bbot = au_fhsm_bottom(sb);
12113 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
12114 +               br = au_sbr(sb, bindex);
12115 +               if (!au_br_fhsm(br->br_perm))
12116 +                       continue;
12117 +
12118 +               bf = br->br_fhsm;
12119 +               mutex_lock(&bf->bf_lock);
12120 +               if (bf->bf_readable) {
12121 +                       err = -EFAULT;
12122 +                       if (count >= sizeof(*stbr))
12123 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
12124 +                                                         br->br_id);
12125 +                       if (!err) {
12126 +                               bf->bf_readable = 0;
12127 +                               count -= sizeof(*stbr);
12128 +                               nstbr++;
12129 +                       }
12130 +               }
12131 +               mutex_unlock(&bf->bf_lock);
12132 +       }
12133 +       if (!err)
12134 +               err = sizeof(*stbr) * nstbr;
12135 +
12136 +       return err;
12137 +}
12138 +
12139 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
12140 +                          loff_t *pos)
12141 +{
12142 +       ssize_t err;
12143 +       int readable;
12144 +       aufs_bindex_t nfhsm, bindex, bbot;
12145 +       struct au_sbinfo *sbinfo;
12146 +       struct au_fhsm *fhsm;
12147 +       struct au_branch *br;
12148 +       struct super_block *sb;
12149 +
12150 +       err = 0;
12151 +       sbinfo = file->private_data;
12152 +       fhsm = &sbinfo->si_fhsm;
12153 +need_data:
12154 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
12155 +       if (!atomic_read(&fhsm->fhsm_readable)) {
12156 +               if (vfsub_file_flags(file) & O_NONBLOCK)
12157 +                       err = -EAGAIN;
12158 +               else
12159 +                       err = wait_event_interruptible_locked_irq
12160 +                               (fhsm->fhsm_wqh,
12161 +                                atomic_read(&fhsm->fhsm_readable));
12162 +       }
12163 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
12164 +       if (unlikely(err))
12165 +               goto out;
12166 +
12167 +       /* sb may already be dead */
12168 +       au_rw_read_lock(&sbinfo->si_rwsem);
12169 +       readable = atomic_read(&fhsm->fhsm_readable);
12170 +       if (readable > 0) {
12171 +               sb = sbinfo->si_sb;
12172 +               AuDebugOn(!sb);
12173 +               /* exclude the bottom branch */
12174 +               nfhsm = 0;
12175 +               bbot = au_fhsm_bottom(sb);
12176 +               for (bindex = 0; bindex < bbot; bindex++) {
12177 +                       br = au_sbr(sb, bindex);
12178 +                       if (au_br_fhsm(br->br_perm))
12179 +                               nfhsm++;
12180 +               }
12181 +               err = -EMSGSIZE;
12182 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
12183 +                       atomic_set(&fhsm->fhsm_readable, 0);
12184 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
12185 +                                            count);
12186 +               }
12187 +       }
12188 +       au_rw_read_unlock(&sbinfo->si_rwsem);
12189 +       if (!readable)
12190 +               goto need_data;
12191 +
12192 +out:
12193 +       return err;
12194 +}
12195 +
12196 +static int au_fhsm_release(struct inode *inode, struct file *file)
12197 +{
12198 +       struct au_sbinfo *sbinfo;
12199 +       struct au_fhsm *fhsm;
12200 +
12201 +       /* sb may already be dead */
12202 +       sbinfo = file->private_data;
12203 +       fhsm = &sbinfo->si_fhsm;
12204 +       spin_lock(&fhsm->fhsm_spin);
12205 +       fhsm->fhsm_pid = 0;
12206 +       spin_unlock(&fhsm->fhsm_spin);
12207 +       kobject_put(&sbinfo->si_kobj);
12208 +
12209 +       return 0;
12210 +}
12211 +
12212 +static const struct file_operations au_fhsm_fops = {
12213 +       .owner          = THIS_MODULE,
12214 +       .llseek         = noop_llseek,
12215 +       .read           = au_fhsm_read,
12216 +       .poll           = au_fhsm_poll,
12217 +       .release        = au_fhsm_release
12218 +};
12219 +
12220 +int au_fhsm_fd(struct super_block *sb, int oflags)
12221 +{
12222 +       int err, fd;
12223 +       struct au_sbinfo *sbinfo;
12224 +       struct au_fhsm *fhsm;
12225 +
12226 +       err = -EPERM;
12227 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
12228 +               goto out;
12229 +
12230 +       err = -EINVAL;
12231 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
12232 +               goto out;
12233 +
12234 +       err = 0;
12235 +       sbinfo = au_sbi(sb);
12236 +       fhsm = &sbinfo->si_fhsm;
12237 +       spin_lock(&fhsm->fhsm_spin);
12238 +       if (!fhsm->fhsm_pid)
12239 +               fhsm->fhsm_pid = current->pid;
12240 +       else
12241 +               err = -EBUSY;
12242 +       spin_unlock(&fhsm->fhsm_spin);
12243 +       if (unlikely(err))
12244 +               goto out;
12245 +
12246 +       oflags |= O_RDONLY;
12247 +       /* oflags |= FMODE_NONOTIFY; */
12248 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
12249 +       err = fd;
12250 +       if (unlikely(fd < 0))
12251 +               goto out_pid;
12252 +
12253 +       /* succeed reglardless 'fhsm' status */
12254 +       kobject_get(&sbinfo->si_kobj);
12255 +       si_noflush_read_lock(sb);
12256 +       if (au_ftest_si(sbinfo, FHSM))
12257 +               au_fhsm_wrote_all(sb, /*force*/0);
12258 +       si_read_unlock(sb);
12259 +       goto out; /* success */
12260 +
12261 +out_pid:
12262 +       spin_lock(&fhsm->fhsm_spin);
12263 +       fhsm->fhsm_pid = 0;
12264 +       spin_unlock(&fhsm->fhsm_spin);
12265 +out:
12266 +       AuTraceErr(err);
12267 +       return err;
12268 +}
12269 +
12270 +/* ---------------------------------------------------------------------- */
12271 +
12272 +int au_fhsm_br_alloc(struct au_branch *br)
12273 +{
12274 +       int err;
12275 +
12276 +       err = 0;
12277 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
12278 +       if (br->br_fhsm)
12279 +               au_br_fhsm_init(br->br_fhsm);
12280 +       else
12281 +               err = -ENOMEM;
12282 +
12283 +       return err;
12284 +}
12285 +
12286 +/* ---------------------------------------------------------------------- */
12287 +
12288 +void au_fhsm_fin(struct super_block *sb)
12289 +{
12290 +       au_fhsm_notify(sb, /*val*/-1);
12291 +}
12292 +
12293 +void au_fhsm_init(struct au_sbinfo *sbinfo)
12294 +{
12295 +       struct au_fhsm *fhsm;
12296 +
12297 +       fhsm = &sbinfo->si_fhsm;
12298 +       spin_lock_init(&fhsm->fhsm_spin);
12299 +       init_waitqueue_head(&fhsm->fhsm_wqh);
12300 +       atomic_set(&fhsm->fhsm_readable, 0);
12301 +       fhsm->fhsm_expire
12302 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
12303 +       fhsm->fhsm_bottom = -1;
12304 +}
12305 +
12306 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
12307 +{
12308 +       sbinfo->si_fhsm.fhsm_expire
12309 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
12310 +}
12311 +
12312 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
12313 +{
12314 +       unsigned int u;
12315 +
12316 +       if (!au_ftest_si(sbinfo, FHSM))
12317 +               return;
12318 +
12319 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
12320 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
12321 +               seq_printf(seq, ",fhsm_sec=%u", u);
12322 +}
12323 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
12324 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
12325 +++ linux/fs/aufs/file.c        2017-07-29 12:14:25.903042072 +0200
12326 @@ -0,0 +1,858 @@
12327 +/*
12328 + * Copyright (C) 2005-2017 Junjiro R. Okajima
12329 + *
12330 + * This program, aufs is free software; you can redistribute it and/or modify
12331 + * it under the terms of the GNU General Public License as published by
12332 + * the Free Software Foundation; either version 2 of the License, or
12333 + * (at your option) any later version.
12334 + *
12335 + * This program is distributed in the hope that it will be useful,
12336 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12337 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12338 + * GNU General Public License for more details.
12339 + *
12340 + * You should have received a copy of the GNU General Public License
12341 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12342 + */
12343 +
12344 +/*
12345 + * handling file/dir, and address_space operation
12346 + */
12347 +
12348 +#ifdef CONFIG_AUFS_DEBUG
12349 +#include <linux/migrate.h>
12350 +#endif
12351 +#include <linux/pagemap.h>
12352 +#include "aufs.h"
12353 +
12354 +/* drop flags for writing */
12355 +unsigned int au_file_roflags(unsigned int flags)
12356 +{
12357 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
12358 +       flags |= O_RDONLY | O_NOATIME;
12359 +       return flags;
12360 +}
12361 +
12362 +/* common functions to regular file and dir */
12363 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12364 +                      struct file *file, int force_wr)
12365 +{
12366 +       struct file *h_file;
12367 +       struct dentry *h_dentry;
12368 +       struct inode *h_inode;
12369 +       struct super_block *sb;
12370 +       struct au_branch *br;
12371 +       struct path h_path;
12372 +       int err;
12373 +
12374 +       /* a race condition can happen between open and unlink/rmdir */
12375 +       h_file = ERR_PTR(-ENOENT);
12376 +       h_dentry = au_h_dptr(dentry, bindex);
12377 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
12378 +               goto out;
12379 +       h_inode = d_inode(h_dentry);
12380 +       spin_lock(&h_dentry->d_lock);
12381 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
12382 +               /* || !d_inode(dentry)->i_nlink */
12383 +               ;
12384 +       spin_unlock(&h_dentry->d_lock);
12385 +       if (unlikely(err))
12386 +               goto out;
12387 +
12388 +       sb = dentry->d_sb;
12389 +       br = au_sbr(sb, bindex);
12390 +       err = au_br_test_oflag(flags, br);
12391 +       h_file = ERR_PTR(err);
12392 +       if (unlikely(err))
12393 +               goto out;
12394 +
12395 +       /* drop flags for writing */
12396 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
12397 +               if (force_wr && !(flags & O_WRONLY))
12398 +                       force_wr = 0;
12399 +               flags = au_file_roflags(flags);
12400 +               if (force_wr) {
12401 +                       h_file = ERR_PTR(-EROFS);
12402 +                       flags = au_file_roflags(flags);
12403 +                       if (unlikely(vfsub_native_ro(h_inode)
12404 +                                    || IS_APPEND(h_inode)))
12405 +                               goto out;
12406 +                       flags &= ~O_ACCMODE;
12407 +                       flags |= O_WRONLY;
12408 +               }
12409 +       }
12410 +       flags &= ~O_CREAT;
12411 +       au_br_get(br);
12412 +       h_path.dentry = h_dentry;
12413 +       h_path.mnt = au_br_mnt(br);
12414 +       h_file = vfsub_dentry_open(&h_path, flags);
12415 +       if (IS_ERR(h_file))
12416 +               goto out_br;
12417 +
12418 +       if (flags & __FMODE_EXEC) {
12419 +               err = deny_write_access(h_file);
12420 +               if (unlikely(err)) {
12421 +                       fput(h_file);
12422 +                       h_file = ERR_PTR(err);
12423 +                       goto out_br;
12424 +               }
12425 +       }
12426 +       fsnotify_open(h_file);
12427 +       goto out; /* success */
12428 +
12429 +out_br:
12430 +       au_br_put(br);
12431 +out:
12432 +       return h_file;
12433 +}
12434 +
12435 +static int au_cmoo(struct dentry *dentry)
12436 +{
12437 +       int err, cmoo;
12438 +       unsigned int udba;
12439 +       struct path h_path;
12440 +       struct au_pin pin;
12441 +       struct au_cp_generic cpg = {
12442 +               .dentry = dentry,
12443 +               .bdst   = -1,
12444 +               .bsrc   = -1,
12445 +               .len    = -1,
12446 +               .pin    = &pin,
12447 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12448 +       };
12449 +       struct inode *delegated;
12450 +       struct super_block *sb;
12451 +       struct au_sbinfo *sbinfo;
12452 +       struct au_fhsm *fhsm;
12453 +       pid_t pid;
12454 +       struct au_branch *br;
12455 +       struct dentry *parent;
12456 +       struct au_hinode *hdir;
12457 +
12458 +       DiMustWriteLock(dentry);
12459 +       IiMustWriteLock(d_inode(dentry));
12460 +
12461 +       err = 0;
12462 +       if (IS_ROOT(dentry))
12463 +               goto out;
12464 +       cpg.bsrc = au_dbtop(dentry);
12465 +       if (!cpg.bsrc)
12466 +               goto out;
12467 +
12468 +       sb = dentry->d_sb;
12469 +       sbinfo = au_sbi(sb);
12470 +       fhsm = &sbinfo->si_fhsm;
12471 +       pid = au_fhsm_pid(fhsm);
12472 +       if (pid
12473 +           && (current->pid == pid
12474 +               || current->real_parent->pid == pid))
12475 +               goto out;
12476 +
12477 +       br = au_sbr(sb, cpg.bsrc);
12478 +       cmoo = au_br_cmoo(br->br_perm);
12479 +       if (!cmoo)
12480 +               goto out;
12481 +       if (!d_is_reg(dentry))
12482 +               cmoo &= AuBrAttr_COO_ALL;
12483 +       if (!cmoo)
12484 +               goto out;
12485 +
12486 +       parent = dget_parent(dentry);
12487 +       di_write_lock_parent(parent);
12488 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12489 +       cpg.bdst = err;
12490 +       if (unlikely(err < 0)) {
12491 +               err = 0;        /* there is no upper writable branch */
12492 +               goto out_dgrade;
12493 +       }
12494 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12495 +
12496 +       /* do not respect the coo attrib for the target branch */
12497 +       err = au_cpup_dirs(dentry, cpg.bdst);
12498 +       if (unlikely(err))
12499 +               goto out_dgrade;
12500 +
12501 +       di_downgrade_lock(parent, AuLock_IR);
12502 +       udba = au_opt_udba(sb);
12503 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12504 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12505 +       if (unlikely(err))
12506 +               goto out_parent;
12507 +
12508 +       err = au_sio_cpup_simple(&cpg);
12509 +       au_unpin(&pin);
12510 +       if (unlikely(err))
12511 +               goto out_parent;
12512 +       if (!(cmoo & AuBrWAttr_MOO))
12513 +               goto out_parent; /* success */
12514 +
12515 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12516 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12517 +       if (unlikely(err))
12518 +               goto out_parent;
12519 +
12520 +       h_path.mnt = au_br_mnt(br);
12521 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12522 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
12523 +       delegated = NULL;
12524 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12525 +       au_unpin(&pin);
12526 +       /* todo: keep h_dentry or not? */
12527 +       if (unlikely(err == -EWOULDBLOCK)) {
12528 +               pr_warn("cannot retry for NFSv4 delegation"
12529 +                       " for an internal unlink\n");
12530 +               iput(delegated);
12531 +       }
12532 +       if (unlikely(err)) {
12533 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12534 +                      dentry, err);
12535 +               err = 0;
12536 +       }
12537 +       goto out_parent; /* success */
12538 +
12539 +out_dgrade:
12540 +       di_downgrade_lock(parent, AuLock_IR);
12541 +out_parent:
12542 +       di_read_unlock(parent, AuLock_IR);
12543 +       dput(parent);
12544 +out:
12545 +       AuTraceErr(err);
12546 +       return err;
12547 +}
12548 +
12549 +int au_do_open(struct file *file, struct au_do_open_args *args)
12550 +{
12551 +       int err, no_lock = args->no_lock;
12552 +       struct dentry *dentry;
12553 +       struct au_finfo *finfo;
12554 +
12555 +       if (!no_lock)
12556 +               err = au_finfo_init(file, args->fidir);
12557 +       else {
12558 +               lockdep_off();
12559 +               err = au_finfo_init(file, args->fidir);
12560 +               lockdep_on();
12561 +       }
12562 +       if (unlikely(err))
12563 +               goto out;
12564 +
12565 +       dentry = file->f_path.dentry;
12566 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12567 +       if (!no_lock) {
12568 +               di_write_lock_child(dentry);
12569 +               err = au_cmoo(dentry);
12570 +               di_downgrade_lock(dentry, AuLock_IR);
12571 +               if (!err)
12572 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12573 +               di_read_unlock(dentry, AuLock_IR);
12574 +       } else {
12575 +               err = au_cmoo(dentry);
12576 +               if (!err)
12577 +                       err = args->open(file, vfsub_file_flags(file),
12578 +                                        args->h_file);
12579 +               if (!err && au_fbtop(file) != au_dbtop(dentry))
12580 +                       /*
12581 +                        * cmoo happens after h_file was opened.
12582 +                        * need to refresh file later.
12583 +                        */
12584 +                       atomic_dec(&au_fi(file)->fi_generation);
12585 +       }
12586 +
12587 +       finfo = au_fi(file);
12588 +       if (!err) {
12589 +               finfo->fi_file = file;
12590 +               au_sphl_add(&finfo->fi_hlist,
12591 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12592 +       }
12593 +       if (!no_lock)
12594 +               fi_write_unlock(file);
12595 +       else {
12596 +               lockdep_off();
12597 +               fi_write_unlock(file);
12598 +               lockdep_on();
12599 +       }
12600 +       if (unlikely(err)) {
12601 +               finfo->fi_hdir = NULL;
12602 +               au_finfo_fin(file);
12603 +       }
12604 +
12605 +out:
12606 +       return err;
12607 +}
12608 +
12609 +int au_reopen_nondir(struct file *file)
12610 +{
12611 +       int err;
12612 +       aufs_bindex_t btop;
12613 +       struct dentry *dentry;
12614 +       struct file *h_file, *h_file_tmp;
12615 +
12616 +       dentry = file->f_path.dentry;
12617 +       btop = au_dbtop(dentry);
12618 +       h_file_tmp = NULL;
12619 +       if (au_fbtop(file) == btop) {
12620 +               h_file = au_hf_top(file);
12621 +               if (file->f_mode == h_file->f_mode)
12622 +                       return 0; /* success */
12623 +               h_file_tmp = h_file;
12624 +               get_file(h_file_tmp);
12625 +               au_set_h_fptr(file, btop, NULL);
12626 +       }
12627 +       AuDebugOn(au_fi(file)->fi_hdir);
12628 +       /*
12629 +        * it can happen
12630 +        * file exists on both of rw and ro
12631 +        * open --> dbtop and fbtop are both 0
12632 +        * prepend a branch as rw, "rw" become ro
12633 +        * remove rw/file
12634 +        * delete the top branch, "rw" becomes rw again
12635 +        *      --> dbtop is 1, fbtop is still 0
12636 +        * write --> fbtop is 0 but dbtop is 1
12637 +        */
12638 +       /* AuDebugOn(au_fbtop(file) < btop); */
12639 +
12640 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
12641 +                          file, /*force_wr*/0);
12642 +       err = PTR_ERR(h_file);
12643 +       if (IS_ERR(h_file)) {
12644 +               if (h_file_tmp) {
12645 +                       au_sbr_get(dentry->d_sb, btop);
12646 +                       au_set_h_fptr(file, btop, h_file_tmp);
12647 +                       h_file_tmp = NULL;
12648 +               }
12649 +               goto out; /* todo: close all? */
12650 +       }
12651 +
12652 +       err = 0;
12653 +       au_set_fbtop(file, btop);
12654 +       au_set_h_fptr(file, btop, h_file);
12655 +       au_update_figen(file);
12656 +       /* todo: necessary? */
12657 +       /* file->f_ra = h_file->f_ra; */
12658 +
12659 +out:
12660 +       if (h_file_tmp)
12661 +               fput(h_file_tmp);
12662 +       return err;
12663 +}
12664 +
12665 +/* ---------------------------------------------------------------------- */
12666 +
12667 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12668 +                       struct dentry *hi_wh)
12669 +{
12670 +       int err;
12671 +       aufs_bindex_t btop;
12672 +       struct au_dinfo *dinfo;
12673 +       struct dentry *h_dentry;
12674 +       struct au_hdentry *hdp;
12675 +
12676 +       dinfo = au_di(file->f_path.dentry);
12677 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12678 +
12679 +       btop = dinfo->di_btop;
12680 +       dinfo->di_btop = btgt;
12681 +       hdp = au_hdentry(dinfo, btgt);
12682 +       h_dentry = hdp->hd_dentry;
12683 +       hdp->hd_dentry = hi_wh;
12684 +       err = au_reopen_nondir(file);
12685 +       hdp->hd_dentry = h_dentry;
12686 +       dinfo->di_btop = btop;
12687 +
12688 +       return err;
12689 +}
12690 +
12691 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12692 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12693 +{
12694 +       int err;
12695 +       struct inode *inode, *h_inode;
12696 +       struct dentry *h_dentry, *hi_wh;
12697 +       struct au_cp_generic cpg = {
12698 +               .dentry = file->f_path.dentry,
12699 +               .bdst   = bcpup,
12700 +               .bsrc   = -1,
12701 +               .len    = len,
12702 +               .pin    = pin
12703 +       };
12704 +
12705 +       au_update_dbtop(cpg.dentry);
12706 +       inode = d_inode(cpg.dentry);
12707 +       h_inode = NULL;
12708 +       if (au_dbtop(cpg.dentry) <= bcpup
12709 +           && au_dbbot(cpg.dentry) >= bcpup) {
12710 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12711 +               if (h_dentry && d_is_positive(h_dentry))
12712 +                       h_inode = d_inode(h_dentry);
12713 +       }
12714 +       hi_wh = au_hi_wh(inode, bcpup);
12715 +       if (!hi_wh && !h_inode)
12716 +               err = au_sio_cpup_wh(&cpg, file);
12717 +       else
12718 +               /* already copied-up after unlink */
12719 +               err = au_reopen_wh(file, bcpup, hi_wh);
12720 +
12721 +       if (!err
12722 +           && (inode->i_nlink > 1
12723 +               || (inode->i_state & I_LINKABLE))
12724 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12725 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12726 +
12727 +       return err;
12728 +}
12729 +
12730 +/*
12731 + * prepare the @file for writing.
12732 + */
12733 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12734 +{
12735 +       int err;
12736 +       aufs_bindex_t dbtop;
12737 +       struct dentry *parent;
12738 +       struct inode *inode;
12739 +       struct super_block *sb;
12740 +       struct file *h_file;
12741 +       struct au_cp_generic cpg = {
12742 +               .dentry = file->f_path.dentry,
12743 +               .bdst   = -1,
12744 +               .bsrc   = -1,
12745 +               .len    = len,
12746 +               .pin    = pin,
12747 +               .flags  = AuCpup_DTIME
12748 +       };
12749 +
12750 +       sb = cpg.dentry->d_sb;
12751 +       inode = d_inode(cpg.dentry);
12752 +       cpg.bsrc = au_fbtop(file);
12753 +       err = au_test_ro(sb, cpg.bsrc, inode);
12754 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12755 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12756 +                            /*flags*/0);
12757 +               goto out;
12758 +       }
12759 +
12760 +       /* need to cpup or reopen */
12761 +       parent = dget_parent(cpg.dentry);
12762 +       di_write_lock_parent(parent);
12763 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12764 +       cpg.bdst = err;
12765 +       if (unlikely(err < 0))
12766 +               goto out_dgrade;
12767 +       err = 0;
12768 +
12769 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12770 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12771 +               if (unlikely(err))
12772 +                       goto out_dgrade;
12773 +       }
12774 +
12775 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12776 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12777 +       if (unlikely(err))
12778 +               goto out_dgrade;
12779 +
12780 +       dbtop = au_dbtop(cpg.dentry);
12781 +       if (dbtop <= cpg.bdst)
12782 +               cpg.bsrc = cpg.bdst;
12783 +
12784 +       if (dbtop <= cpg.bdst           /* just reopen */
12785 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12786 +               ) {
12787 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12788 +               if (IS_ERR(h_file))
12789 +                       err = PTR_ERR(h_file);
12790 +               else {
12791 +                       di_downgrade_lock(parent, AuLock_IR);
12792 +                       if (dbtop > cpg.bdst)
12793 +                               err = au_sio_cpup_simple(&cpg);
12794 +                       if (!err)
12795 +                               err = au_reopen_nondir(file);
12796 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12797 +               }
12798 +       } else {                        /* copyup as wh and reopen */
12799 +               /*
12800 +                * since writable hfsplus branch is not supported,
12801 +                * h_open_pre/post() are unnecessary.
12802 +                */
12803 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12804 +               di_downgrade_lock(parent, AuLock_IR);
12805 +       }
12806 +
12807 +       if (!err) {
12808 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12809 +               goto out_dput; /* success */
12810 +       }
12811 +       au_unpin(pin);
12812 +       goto out_unlock;
12813 +
12814 +out_dgrade:
12815 +       di_downgrade_lock(parent, AuLock_IR);
12816 +out_unlock:
12817 +       di_read_unlock(parent, AuLock_IR);
12818 +out_dput:
12819 +       dput(parent);
12820 +out:
12821 +       return err;
12822 +}
12823 +
12824 +/* ---------------------------------------------------------------------- */
12825 +
12826 +int au_do_flush(struct file *file, fl_owner_t id,
12827 +               int (*flush)(struct file *file, fl_owner_t id))
12828 +{
12829 +       int err;
12830 +       struct super_block *sb;
12831 +       struct inode *inode;
12832 +
12833 +       inode = file_inode(file);
12834 +       sb = inode->i_sb;
12835 +       si_noflush_read_lock(sb);
12836 +       fi_read_lock(file);
12837 +       ii_read_lock_child(inode);
12838 +
12839 +       err = flush(file, id);
12840 +       au_cpup_attr_timesizes(inode);
12841 +
12842 +       ii_read_unlock(inode);
12843 +       fi_read_unlock(file);
12844 +       si_read_unlock(sb);
12845 +       return err;
12846 +}
12847 +
12848 +/* ---------------------------------------------------------------------- */
12849 +
12850 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12851 +{
12852 +       int err;
12853 +       struct au_pin pin;
12854 +       struct au_finfo *finfo;
12855 +       struct dentry *parent, *hi_wh;
12856 +       struct inode *inode;
12857 +       struct super_block *sb;
12858 +       struct au_cp_generic cpg = {
12859 +               .dentry = file->f_path.dentry,
12860 +               .bdst   = -1,
12861 +               .bsrc   = -1,
12862 +               .len    = -1,
12863 +               .pin    = &pin,
12864 +               .flags  = AuCpup_DTIME
12865 +       };
12866 +
12867 +       FiMustWriteLock(file);
12868 +
12869 +       err = 0;
12870 +       finfo = au_fi(file);
12871 +       sb = cpg.dentry->d_sb;
12872 +       inode = d_inode(cpg.dentry);
12873 +       cpg.bdst = au_ibtop(inode);
12874 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12875 +               goto out;
12876 +
12877 +       parent = dget_parent(cpg.dentry);
12878 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12879 +               di_read_lock_parent(parent, !AuLock_IR);
12880 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12881 +               cpg.bdst = err;
12882 +               di_read_unlock(parent, !AuLock_IR);
12883 +               if (unlikely(err < 0))
12884 +                       goto out_parent;
12885 +               err = 0;
12886 +       }
12887 +
12888 +       di_read_lock_parent(parent, AuLock_IR);
12889 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12890 +       if (!S_ISDIR(inode->i_mode)
12891 +           && au_opt_test(au_mntflags(sb), PLINK)
12892 +           && au_plink_test(inode)
12893 +           && !d_unhashed(cpg.dentry)
12894 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
12895 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12896 +               if (unlikely(err))
12897 +                       goto out_unlock;
12898 +
12899 +               /* always superio. */
12900 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12901 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12902 +               if (!err) {
12903 +                       err = au_sio_cpup_simple(&cpg);
12904 +                       au_unpin(&pin);
12905 +               }
12906 +       } else if (hi_wh) {
12907 +               /* already copied-up after unlink */
12908 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12909 +               *need_reopen = 0;
12910 +       }
12911 +
12912 +out_unlock:
12913 +       di_read_unlock(parent, AuLock_IR);
12914 +out_parent:
12915 +       dput(parent);
12916 +out:
12917 +       return err;
12918 +}
12919 +
12920 +static void au_do_refresh_dir(struct file *file)
12921 +{
12922 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
12923 +       struct au_hfile *p, tmp, *q;
12924 +       struct au_finfo *finfo;
12925 +       struct super_block *sb;
12926 +       struct au_fidir *fidir;
12927 +
12928 +       FiMustWriteLock(file);
12929 +
12930 +       sb = file->f_path.dentry->d_sb;
12931 +       finfo = au_fi(file);
12932 +       fidir = finfo->fi_hdir;
12933 +       AuDebugOn(!fidir);
12934 +       p = fidir->fd_hfile + finfo->fi_btop;
12935 +       brid = p->hf_br->br_id;
12936 +       bbot = fidir->fd_bbot;
12937 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
12938 +               if (!p->hf_file)
12939 +                       continue;
12940 +
12941 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12942 +               if (new_bindex == bindex)
12943 +                       continue;
12944 +               if (new_bindex < 0) {
12945 +                       au_set_h_fptr(file, bindex, NULL);
12946 +                       continue;
12947 +               }
12948 +
12949 +               /* swap two lower inode, and loop again */
12950 +               q = fidir->fd_hfile + new_bindex;
12951 +               tmp = *q;
12952 +               *q = *p;
12953 +               *p = tmp;
12954 +               if (tmp.hf_file) {
12955 +                       bindex--;
12956 +                       p--;
12957 +               }
12958 +       }
12959 +
12960 +       p = fidir->fd_hfile;
12961 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12962 +               bbot = au_sbbot(sb);
12963 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
12964 +                    finfo->fi_btop++, p++)
12965 +                       if (p->hf_file) {
12966 +                               if (file_inode(p->hf_file))
12967 +                                       break;
12968 +                               au_hfput(p, /*execed*/0);
12969 +                       }
12970 +       } else {
12971 +               bbot = au_br_index(sb, brid);
12972 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
12973 +                    finfo->fi_btop++, p++)
12974 +                       if (p->hf_file)
12975 +                               au_hfput(p, /*execed*/0);
12976 +               bbot = au_sbbot(sb);
12977 +       }
12978 +
12979 +       p = fidir->fd_hfile + bbot;
12980 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
12981 +            fidir->fd_bbot--, p--)
12982 +               if (p->hf_file) {
12983 +                       if (file_inode(p->hf_file))
12984 +                               break;
12985 +                       au_hfput(p, /*execed*/0);
12986 +               }
12987 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12988 +}
12989 +
12990 +/*
12991 + * after branch manipulating, refresh the file.
12992 + */
12993 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12994 +{
12995 +       int err, need_reopen, nbr;
12996 +       aufs_bindex_t bbot, bindex;
12997 +       struct dentry *dentry;
12998 +       struct super_block *sb;
12999 +       struct au_finfo *finfo;
13000 +       struct au_hfile *hfile;
13001 +
13002 +       dentry = file->f_path.dentry;
13003 +       sb = dentry->d_sb;
13004 +       nbr = au_sbbot(sb) + 1;
13005 +       finfo = au_fi(file);
13006 +       if (!finfo->fi_hdir) {
13007 +               hfile = &finfo->fi_htop;
13008 +               AuDebugOn(!hfile->hf_file);
13009 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
13010 +               AuDebugOn(bindex < 0);
13011 +               if (bindex != finfo->fi_btop)
13012 +                       au_set_fbtop(file, bindex);
13013 +       } else {
13014 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
13015 +               if (unlikely(err))
13016 +                       goto out;
13017 +               au_do_refresh_dir(file);
13018 +       }
13019 +
13020 +       err = 0;
13021 +       need_reopen = 1;
13022 +       if (!au_test_mmapped(file))
13023 +               err = au_file_refresh_by_inode(file, &need_reopen);
13024 +       if (finfo->fi_hdir)
13025 +               /* harmless if err */
13026 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
13027 +       if (!err && need_reopen && !d_unlinked(dentry))
13028 +               err = reopen(file);
13029 +       if (!err) {
13030 +               au_update_figen(file);
13031 +               goto out; /* success */
13032 +       }
13033 +
13034 +       /* error, close all lower files */
13035 +       if (finfo->fi_hdir) {
13036 +               bbot = au_fbbot_dir(file);
13037 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
13038 +                       au_set_h_fptr(file, bindex, NULL);
13039 +       }
13040 +
13041 +out:
13042 +       return err;
13043 +}
13044 +
13045 +/* common function to regular file and dir */
13046 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13047 +                         int wlock, unsigned int fi_lsc)
13048 +{
13049 +       int err;
13050 +       unsigned int sigen, figen;
13051 +       aufs_bindex_t btop;
13052 +       unsigned char pseudo_link;
13053 +       struct dentry *dentry;
13054 +       struct inode *inode;
13055 +
13056 +       err = 0;
13057 +       dentry = file->f_path.dentry;
13058 +       inode = d_inode(dentry);
13059 +       sigen = au_sigen(dentry->d_sb);
13060 +       fi_write_lock_nested(file, fi_lsc);
13061 +       figen = au_figen(file);
13062 +       if (!fi_lsc)
13063 +               di_write_lock_child(dentry);
13064 +       else
13065 +               di_write_lock_child2(dentry);
13066 +       btop = au_dbtop(dentry);
13067 +       pseudo_link = (btop != au_ibtop(inode));
13068 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
13069 +               if (!wlock) {
13070 +                       di_downgrade_lock(dentry, AuLock_IR);
13071 +                       fi_downgrade_lock(file);
13072 +               }
13073 +               goto out; /* success */
13074 +       }
13075 +
13076 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
13077 +       if (au_digen_test(dentry, sigen)) {
13078 +               err = au_reval_dpath(dentry, sigen);
13079 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
13080 +       }
13081 +
13082 +       if (!err)
13083 +               err = refresh_file(file, reopen);
13084 +       if (!err) {
13085 +               if (!wlock) {
13086 +                       di_downgrade_lock(dentry, AuLock_IR);
13087 +                       fi_downgrade_lock(file);
13088 +               }
13089 +       } else {
13090 +               di_write_unlock(dentry);
13091 +               fi_write_unlock(file);
13092 +       }
13093 +
13094 +out:
13095 +       return err;
13096 +}
13097 +
13098 +/* ---------------------------------------------------------------------- */
13099 +
13100 +/* cf. aufs_nopage() */
13101 +/* for madvise(2) */
13102 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
13103 +{
13104 +       unlock_page(page);
13105 +       return 0;
13106 +}
13107 +
13108 +/* it will never be called, but necessary to support O_DIRECT */
13109 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
13110 +{ BUG(); return 0; }
13111 +
13112 +/* they will never be called. */
13113 +#ifdef CONFIG_AUFS_DEBUG
13114 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
13115 +                           loff_t pos, unsigned len, unsigned flags,
13116 +                           struct page **pagep, void **fsdata)
13117 +{ AuUnsupport(); return 0; }
13118 +static int aufs_write_end(struct file *file, struct address_space *mapping,
13119 +                         loff_t pos, unsigned len, unsigned copied,
13120 +                         struct page *page, void *fsdata)
13121 +{ AuUnsupport(); return 0; }
13122 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
13123 +{ AuUnsupport(); return 0; }
13124 +
13125 +static int aufs_set_page_dirty(struct page *page)
13126 +{ AuUnsupport(); return 0; }
13127 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
13128 +                               unsigned int length)
13129 +{ AuUnsupport(); }
13130 +static int aufs_releasepage(struct page *page, gfp_t gfp)
13131 +{ AuUnsupport(); return 0; }
13132 +#if 0 /* called by memory compaction regardless file */
13133 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
13134 +                           struct page *page, enum migrate_mode mode)
13135 +{ AuUnsupport(); return 0; }
13136 +#endif
13137 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
13138 +{ AuUnsupport(); return true; }
13139 +static void aufs_putback_page(struct page *page)
13140 +{ AuUnsupport(); }
13141 +static int aufs_launder_page(struct page *page)
13142 +{ AuUnsupport(); return 0; }
13143 +static int aufs_is_partially_uptodate(struct page *page,
13144 +                                     unsigned long from,
13145 +                                     unsigned long count)
13146 +{ AuUnsupport(); return 0; }
13147 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
13148 +                                   bool *writeback)
13149 +{ AuUnsupport(); }
13150 +static int aufs_error_remove_page(struct address_space *mapping,
13151 +                                 struct page *page)
13152 +{ AuUnsupport(); return 0; }
13153 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
13154 +                             sector_t *span)
13155 +{ AuUnsupport(); return 0; }
13156 +static void aufs_swap_deactivate(struct file *file)
13157 +{ AuUnsupport(); }
13158 +#endif /* CONFIG_AUFS_DEBUG */
13159 +
13160 +const struct address_space_operations aufs_aop = {
13161 +       .readpage               = aufs_readpage,
13162 +       .direct_IO              = aufs_direct_IO,
13163 +#ifdef CONFIG_AUFS_DEBUG
13164 +       .writepage              = aufs_writepage,
13165 +       /* no writepages, because of writepage */
13166 +       .set_page_dirty         = aufs_set_page_dirty,
13167 +       /* no readpages, because of readpage */
13168 +       .write_begin            = aufs_write_begin,
13169 +       .write_end              = aufs_write_end,
13170 +       /* no bmap, no block device */
13171 +       .invalidatepage         = aufs_invalidatepage,
13172 +       .releasepage            = aufs_releasepage,
13173 +       /* is fallback_migrate_page ok? */
13174 +       /* .migratepage         = aufs_migratepage, */
13175 +       .isolate_page           = aufs_isolate_page,
13176 +       .putback_page           = aufs_putback_page,
13177 +       .launder_page           = aufs_launder_page,
13178 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
13179 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
13180 +       .error_remove_page      = aufs_error_remove_page,
13181 +       .swap_activate          = aufs_swap_activate,
13182 +       .swap_deactivate        = aufs_swap_deactivate
13183 +#endif /* CONFIG_AUFS_DEBUG */
13184 +};
13185 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
13186 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
13187 +++ linux/fs/aufs/file.h        2017-07-29 12:14:25.903042072 +0200
13188 @@ -0,0 +1,330 @@
13189 +/*
13190 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13191 + *
13192 + * This program, aufs is free software; you can redistribute it and/or modify
13193 + * it under the terms of the GNU General Public License as published by
13194 + * the Free Software Foundation; either version 2 of the License, or
13195 + * (at your option) any later version.
13196 + *
13197 + * This program is distributed in the hope that it will be useful,
13198 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13199 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13200 + * GNU General Public License for more details.
13201 + *
13202 + * You should have received a copy of the GNU General Public License
13203 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13204 + */
13205 +
13206 +/*
13207 + * file operations
13208 + */
13209 +
13210 +#ifndef __AUFS_FILE_H__
13211 +#define __AUFS_FILE_H__
13212 +
13213 +#ifdef __KERNEL__
13214 +
13215 +#include <linux/file.h>
13216 +#include <linux/fs.h>
13217 +#include <linux/poll.h>
13218 +#include "rwsem.h"
13219 +
13220 +struct au_branch;
13221 +struct au_hfile {
13222 +       struct file             *hf_file;
13223 +       struct au_branch        *hf_br;
13224 +};
13225 +
13226 +struct au_vdir;
13227 +struct au_fidir {
13228 +       aufs_bindex_t           fd_bbot;
13229 +       aufs_bindex_t           fd_nent;
13230 +       struct au_vdir          *fd_vdir_cache;
13231 +       struct au_hfile         fd_hfile[];
13232 +};
13233 +
13234 +static inline int au_fidir_sz(int nent)
13235 +{
13236 +       AuDebugOn(nent < 0);
13237 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
13238 +}
13239 +
13240 +struct au_finfo {
13241 +       atomic_t                fi_generation;
13242 +
13243 +       struct au_rwsem         fi_rwsem;
13244 +       aufs_bindex_t           fi_btop;
13245 +
13246 +       /* do not union them */
13247 +       struct {                                /* for non-dir */
13248 +               struct au_hfile                 fi_htop;
13249 +               atomic_t                        fi_mmapped;
13250 +       };
13251 +       struct au_fidir         *fi_hdir;       /* for dir only */
13252 +
13253 +       struct hlist_node       fi_hlist;
13254 +       struct file             *fi_file;       /* very ugly */
13255 +} ____cacheline_aligned_in_smp;
13256 +
13257 +/* ---------------------------------------------------------------------- */
13258 +
13259 +/* file.c */
13260 +extern const struct address_space_operations aufs_aop;
13261 +unsigned int au_file_roflags(unsigned int flags);
13262 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
13263 +                      struct file *file, int force_wr);
13264 +struct au_do_open_args {
13265 +       int             no_lock;
13266 +       int             (*open)(struct file *file, int flags,
13267 +                               struct file *h_file);
13268 +       struct au_fidir *fidir;
13269 +       struct file     *h_file;
13270 +};
13271 +int au_do_open(struct file *file, struct au_do_open_args *args);
13272 +int au_reopen_nondir(struct file *file);
13273 +struct au_pin;
13274 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
13275 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13276 +                         int wlock, unsigned int fi_lsc);
13277 +int au_do_flush(struct file *file, fl_owner_t id,
13278 +               int (*flush)(struct file *file, fl_owner_t id));
13279 +
13280 +/* poll.c */
13281 +#ifdef CONFIG_AUFS_POLL
13282 +unsigned int aufs_poll(struct file *file, poll_table *wait);
13283 +#endif
13284 +
13285 +#ifdef CONFIG_AUFS_BR_HFSPLUS
13286 +/* hfsplus.c */
13287 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13288 +                          int force_wr);
13289 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13290 +                   struct file *h_file);
13291 +#else
13292 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
13293 +       aufs_bindex_t bindex, int force_wr)
13294 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
13295 +          struct file *h_file);
13296 +#endif
13297 +
13298 +/* f_op.c */
13299 +extern const struct file_operations aufs_file_fop;
13300 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
13301 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
13302 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
13303 +
13304 +/* finfo.c */
13305 +void au_hfput(struct au_hfile *hf, int execed);
13306 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
13307 +                  struct file *h_file);
13308 +
13309 +void au_update_figen(struct file *file);
13310 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
13311 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
13312 +
13313 +void au_fi_init_once(void *_fi);
13314 +void au_finfo_fin(struct file *file);
13315 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
13316 +
13317 +/* ioctl.c */
13318 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
13319 +#ifdef CONFIG_COMPAT
13320 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
13321 +                          unsigned long arg);
13322 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
13323 +                             unsigned long arg);
13324 +#endif
13325 +
13326 +/* ---------------------------------------------------------------------- */
13327 +
13328 +static inline struct au_finfo *au_fi(struct file *file)
13329 +{
13330 +       return file->private_data;
13331 +}
13332 +
13333 +/* ---------------------------------------------------------------------- */
13334 +
13335 +/*
13336 + * fi_read_lock, fi_write_lock,
13337 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
13338 + */
13339 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
13340 +
13341 +/* lock subclass for finfo */
13342 +enum {
13343 +       AuLsc_FI_1,
13344 +       AuLsc_FI_2
13345 +};
13346 +
13347 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
13348 +{
13349 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
13350 +}
13351 +
13352 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
13353 +{
13354 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
13355 +}
13356 +
13357 +/*
13358 + * fi_read_lock_1, fi_write_lock_1,
13359 + * fi_read_lock_2, fi_write_lock_2
13360 + */
13361 +#define AuReadLockFunc(name) \
13362 +static inline void fi_read_lock_##name(struct file *f) \
13363 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
13364 +
13365 +#define AuWriteLockFunc(name) \
13366 +static inline void fi_write_lock_##name(struct file *f) \
13367 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
13368 +
13369 +#define AuRWLockFuncs(name) \
13370 +       AuReadLockFunc(name) \
13371 +       AuWriteLockFunc(name)
13372 +
13373 +AuRWLockFuncs(1);
13374 +AuRWLockFuncs(2);
13375 +
13376 +#undef AuReadLockFunc
13377 +#undef AuWriteLockFunc
13378 +#undef AuRWLockFuncs
13379 +
13380 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
13381 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
13382 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
13383 +
13384 +/* ---------------------------------------------------------------------- */
13385 +
13386 +/* todo: hard/soft set? */
13387 +static inline aufs_bindex_t au_fbtop(struct file *file)
13388 +{
13389 +       FiMustAnyLock(file);
13390 +       return au_fi(file)->fi_btop;
13391 +}
13392 +
13393 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
13394 +{
13395 +       FiMustAnyLock(file);
13396 +       AuDebugOn(!au_fi(file)->fi_hdir);
13397 +       return au_fi(file)->fi_hdir->fd_bbot;
13398 +}
13399 +
13400 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
13401 +{
13402 +       FiMustAnyLock(file);
13403 +       AuDebugOn(!au_fi(file)->fi_hdir);
13404 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
13405 +}
13406 +
13407 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
13408 +{
13409 +       FiMustWriteLock(file);
13410 +       au_fi(file)->fi_btop = bindex;
13411 +}
13412 +
13413 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
13414 +{
13415 +       FiMustWriteLock(file);
13416 +       AuDebugOn(!au_fi(file)->fi_hdir);
13417 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
13418 +}
13419 +
13420 +static inline void au_set_fvdir_cache(struct file *file,
13421 +                                     struct au_vdir *vdir_cache)
13422 +{
13423 +       FiMustWriteLock(file);
13424 +       AuDebugOn(!au_fi(file)->fi_hdir);
13425 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13426 +}
13427 +
13428 +static inline struct file *au_hf_top(struct file *file)
13429 +{
13430 +       FiMustAnyLock(file);
13431 +       AuDebugOn(au_fi(file)->fi_hdir);
13432 +       return au_fi(file)->fi_htop.hf_file;
13433 +}
13434 +
13435 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13436 +{
13437 +       FiMustAnyLock(file);
13438 +       AuDebugOn(!au_fi(file)->fi_hdir);
13439 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
13440 +}
13441 +
13442 +/* todo: memory barrier? */
13443 +static inline unsigned int au_figen(struct file *f)
13444 +{
13445 +       return atomic_read(&au_fi(f)->fi_generation);
13446 +}
13447 +
13448 +static inline void au_set_mmapped(struct file *f)
13449 +{
13450 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13451 +               return;
13452 +       pr_warn("fi_mmapped wrapped around\n");
13453 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13454 +               ;
13455 +}
13456 +
13457 +static inline void au_unset_mmapped(struct file *f)
13458 +{
13459 +       atomic_dec(&au_fi(f)->fi_mmapped);
13460 +}
13461 +
13462 +static inline int au_test_mmapped(struct file *f)
13463 +{
13464 +       return atomic_read(&au_fi(f)->fi_mmapped);
13465 +}
13466 +
13467 +/* customize vma->vm_file */
13468 +
13469 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13470 +                                      struct file *file)
13471 +{
13472 +       struct file *f;
13473 +
13474 +       f = vma->vm_file;
13475 +       get_file(file);
13476 +       vma->vm_file = file;
13477 +       fput(f);
13478 +}
13479 +
13480 +#ifdef CONFIG_MMU
13481 +#define AuDbgVmRegion(file, vma) do {} while (0)
13482 +
13483 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13484 +                                   struct file *file)
13485 +{
13486 +       au_do_vm_file_reset(vma, file);
13487 +}
13488 +#else
13489 +#define AuDbgVmRegion(file, vma) \
13490 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13491 +
13492 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13493 +                                   struct file *file)
13494 +{
13495 +       struct file *f;
13496 +
13497 +       au_do_vm_file_reset(vma, file);
13498 +       f = vma->vm_region->vm_file;
13499 +       get_file(file);
13500 +       vma->vm_region->vm_file = file;
13501 +       fput(f);
13502 +}
13503 +#endif /* CONFIG_MMU */
13504 +
13505 +/* handle vma->vm_prfile */
13506 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13507 +                                   struct file *file)
13508 +{
13509 +       get_file(file);
13510 +       vma->vm_prfile = file;
13511 +#ifndef CONFIG_MMU
13512 +       get_file(file);
13513 +       vma->vm_region->vm_prfile = file;
13514 +#endif
13515 +}
13516 +
13517 +#endif /* __KERNEL__ */
13518 +#endif /* __AUFS_FILE_H__ */
13519 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13520 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13521 +++ linux/fs/aufs/finfo.c       2017-07-29 12:14:25.903042072 +0200
13522 @@ -0,0 +1,148 @@
13523 +/*
13524 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13525 + *
13526 + * This program, aufs is free software; you can redistribute it and/or modify
13527 + * it under the terms of the GNU General Public License as published by
13528 + * the Free Software Foundation; either version 2 of the License, or
13529 + * (at your option) any later version.
13530 + *
13531 + * This program is distributed in the hope that it will be useful,
13532 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13533 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13534 + * GNU General Public License for more details.
13535 + *
13536 + * You should have received a copy of the GNU General Public License
13537 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13538 + */
13539 +
13540 +/*
13541 + * file private data
13542 + */
13543 +
13544 +#include "aufs.h"
13545 +
13546 +void au_hfput(struct au_hfile *hf, int execed)
13547 +{
13548 +       if (execed)
13549 +               allow_write_access(hf->hf_file);
13550 +       fput(hf->hf_file);
13551 +       hf->hf_file = NULL;
13552 +       au_br_put(hf->hf_br);
13553 +       hf->hf_br = NULL;
13554 +}
13555 +
13556 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13557 +{
13558 +       struct au_finfo *finfo = au_fi(file);
13559 +       struct au_hfile *hf;
13560 +       struct au_fidir *fidir;
13561 +
13562 +       fidir = finfo->fi_hdir;
13563 +       if (!fidir) {
13564 +               AuDebugOn(finfo->fi_btop != bindex);
13565 +               hf = &finfo->fi_htop;
13566 +       } else
13567 +               hf = fidir->fd_hfile + bindex;
13568 +
13569 +       if (hf && hf->hf_file)
13570 +               au_hfput(hf, vfsub_file_execed(file));
13571 +       if (val) {
13572 +               FiMustWriteLock(file);
13573 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13574 +               hf->hf_file = val;
13575 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13576 +       }
13577 +}
13578 +
13579 +void au_update_figen(struct file *file)
13580 +{
13581 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13582 +       /* smp_mb(); */ /* atomic_set */
13583 +}
13584 +
13585 +/* ---------------------------------------------------------------------- */
13586 +
13587 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13588 +{
13589 +       struct au_fidir *fidir;
13590 +       int nbr;
13591 +
13592 +       nbr = au_sbbot(sb) + 1;
13593 +       if (nbr < 2)
13594 +               nbr = 2; /* initial allocate for 2 branches */
13595 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13596 +       if (fidir) {
13597 +               fidir->fd_bbot = -1;
13598 +               fidir->fd_nent = nbr;
13599 +       }
13600 +
13601 +       return fidir;
13602 +}
13603 +
13604 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
13605 +{
13606 +       int err;
13607 +       struct au_fidir *fidir, *p;
13608 +
13609 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13610 +       fidir = finfo->fi_hdir;
13611 +       AuDebugOn(!fidir);
13612 +
13613 +       err = -ENOMEM;
13614 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13615 +                        GFP_NOFS, may_shrink);
13616 +       if (p) {
13617 +               p->fd_nent = nbr;
13618 +               finfo->fi_hdir = p;
13619 +               err = 0;
13620 +       }
13621 +
13622 +       return err;
13623 +}
13624 +
13625 +/* ---------------------------------------------------------------------- */
13626 +
13627 +void au_finfo_fin(struct file *file)
13628 +{
13629 +       struct au_finfo *finfo;
13630 +
13631 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13632 +
13633 +       finfo = au_fi(file);
13634 +       AuDebugOn(finfo->fi_hdir);
13635 +       AuRwDestroy(&finfo->fi_rwsem);
13636 +       au_cache_free_finfo(finfo);
13637 +}
13638 +
13639 +void au_fi_init_once(void *_finfo)
13640 +{
13641 +       struct au_finfo *finfo = _finfo;
13642 +
13643 +       au_rw_init(&finfo->fi_rwsem);
13644 +}
13645 +
13646 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13647 +{
13648 +       int err;
13649 +       struct au_finfo *finfo;
13650 +       struct dentry *dentry;
13651 +
13652 +       err = -ENOMEM;
13653 +       dentry = file->f_path.dentry;
13654 +       finfo = au_cache_alloc_finfo();
13655 +       if (unlikely(!finfo))
13656 +               goto out;
13657 +
13658 +       err = 0;
13659 +       au_nfiles_inc(dentry->d_sb);
13660 +       au_rw_write_lock(&finfo->fi_rwsem);
13661 +       finfo->fi_btop = -1;
13662 +       finfo->fi_hdir = fidir;
13663 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13664 +       /* smp_mb(); */ /* atomic_set */
13665 +
13666 +       file->private_data = finfo;
13667 +
13668 +out:
13669 +       return err;
13670 +}
13671 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13672 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13673 +++ linux/fs/aufs/f_op.c        2017-07-29 12:14:25.903042072 +0200
13674 @@ -0,0 +1,817 @@
13675 +/*
13676 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13677 + *
13678 + * This program, aufs is free software; you can redistribute it and/or modify
13679 + * it under the terms of the GNU General Public License as published by
13680 + * the Free Software Foundation; either version 2 of the License, or
13681 + * (at your option) any later version.
13682 + *
13683 + * This program is distributed in the hope that it will be useful,
13684 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13685 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13686 + * GNU General Public License for more details.
13687 + *
13688 + * You should have received a copy of the GNU General Public License
13689 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13690 + */
13691 +
13692 +/*
13693 + * file and vm operations
13694 + */
13695 +
13696 +#include <linux/aio.h>
13697 +#include <linux/fs_stack.h>
13698 +#include <linux/mman.h>
13699 +#include <linux/security.h>
13700 +#include "aufs.h"
13701 +
13702 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13703 +{
13704 +       int err;
13705 +       aufs_bindex_t bindex;
13706 +       struct dentry *dentry, *h_dentry;
13707 +       struct au_finfo *finfo;
13708 +       struct inode *h_inode;
13709 +
13710 +       FiMustWriteLock(file);
13711 +
13712 +       err = 0;
13713 +       dentry = file->f_path.dentry;
13714 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13715 +       finfo = au_fi(file);
13716 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13717 +       atomic_set(&finfo->fi_mmapped, 0);
13718 +       bindex = au_dbtop(dentry);
13719 +       if (!h_file) {
13720 +               h_dentry = au_h_dptr(dentry, bindex);
13721 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13722 +               if (unlikely(err))
13723 +                       goto out;
13724 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13725 +       } else {
13726 +               h_dentry = h_file->f_path.dentry;
13727 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13728 +               if (unlikely(err))
13729 +                       goto out;
13730 +               get_file(h_file);
13731 +       }
13732 +       if (IS_ERR(h_file))
13733 +               err = PTR_ERR(h_file);
13734 +       else {
13735 +               if ((flags & __O_TMPFILE)
13736 +                   && !(flags & O_EXCL)) {
13737 +                       h_inode = file_inode(h_file);
13738 +                       spin_lock(&h_inode->i_lock);
13739 +                       h_inode->i_state |= I_LINKABLE;
13740 +                       spin_unlock(&h_inode->i_lock);
13741 +               }
13742 +               au_set_fbtop(file, bindex);
13743 +               au_set_h_fptr(file, bindex, h_file);
13744 +               au_update_figen(file);
13745 +               /* todo: necessary? */
13746 +               /* file->f_ra = h_file->f_ra; */
13747 +       }
13748 +
13749 +out:
13750 +       return err;
13751 +}
13752 +
13753 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13754 +                           struct file *file)
13755 +{
13756 +       int err;
13757 +       struct super_block *sb;
13758 +       struct au_do_open_args args = {
13759 +               .open   = au_do_open_nondir
13760 +       };
13761 +
13762 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13763 +             file, vfsub_file_flags(file), file->f_mode);
13764 +
13765 +       sb = file->f_path.dentry->d_sb;
13766 +       si_read_lock(sb, AuLock_FLUSH);
13767 +       err = au_do_open(file, &args);
13768 +       si_read_unlock(sb);
13769 +       return err;
13770 +}
13771 +
13772 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13773 +{
13774 +       struct au_finfo *finfo;
13775 +       aufs_bindex_t bindex;
13776 +
13777 +       finfo = au_fi(file);
13778 +       au_sphl_del(&finfo->fi_hlist,
13779 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13780 +       bindex = finfo->fi_btop;
13781 +       if (bindex >= 0)
13782 +               au_set_h_fptr(file, bindex, NULL);
13783 +
13784 +       au_finfo_fin(file);
13785 +       return 0;
13786 +}
13787 +
13788 +/* ---------------------------------------------------------------------- */
13789 +
13790 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13791 +{
13792 +       int err;
13793 +       struct file *h_file;
13794 +
13795 +       err = 0;
13796 +       h_file = au_hf_top(file);
13797 +       if (h_file)
13798 +               err = vfsub_flush(h_file, id);
13799 +       return err;
13800 +}
13801 +
13802 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13803 +{
13804 +       return au_do_flush(file, id, au_do_flush_nondir);
13805 +}
13806 +
13807 +/* ---------------------------------------------------------------------- */
13808 +/*
13809 + * read and write functions acquire [fdi]_rwsem once, but release before
13810 + * mmap_sem. This is because to stop a race condition between mmap(2).
13811 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13812 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13813 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13814 + */
13815 +
13816 +/* Callers should call au_read_post() or fput() in the end */
13817 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
13818 +{
13819 +       struct file *h_file;
13820 +       int err;
13821 +
13822 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
13823 +       if (!err) {
13824 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13825 +               h_file = au_hf_top(file);
13826 +               get_file(h_file);
13827 +               if (!keep_fi)
13828 +                       fi_read_unlock(file);
13829 +       } else
13830 +               h_file = ERR_PTR(err);
13831 +
13832 +       return h_file;
13833 +}
13834 +
13835 +static void au_read_post(struct inode *inode, struct file *h_file)
13836 +{
13837 +       /* update without lock, I don't think it a problem */
13838 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13839 +       fput(h_file);
13840 +}
13841 +
13842 +struct au_write_pre {
13843 +       /* input */
13844 +       unsigned int lsc;
13845 +
13846 +       /* output */
13847 +       blkcnt_t blks;
13848 +       aufs_bindex_t btop;
13849 +};
13850 +
13851 +/*
13852 + * return with iinfo is write-locked
13853 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13854 + * end
13855 + */
13856 +static struct file *au_write_pre(struct file *file, int do_ready,
13857 +                                struct au_write_pre *wpre)
13858 +{
13859 +       struct file *h_file;
13860 +       struct dentry *dentry;
13861 +       int err;
13862 +       unsigned int lsc;
13863 +       struct au_pin pin;
13864 +
13865 +       lsc = 0;
13866 +       if (wpre)
13867 +               lsc = wpre->lsc;
13868 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
13869 +       h_file = ERR_PTR(err);
13870 +       if (unlikely(err))
13871 +               goto out;
13872 +
13873 +       dentry = file->f_path.dentry;
13874 +       if (do_ready) {
13875 +               err = au_ready_to_write(file, -1, &pin);
13876 +               if (unlikely(err)) {
13877 +                       h_file = ERR_PTR(err);
13878 +                       di_write_unlock(dentry);
13879 +                       goto out_fi;
13880 +               }
13881 +       }
13882 +
13883 +       di_downgrade_lock(dentry, /*flags*/0);
13884 +       if (wpre)
13885 +               wpre->btop = au_fbtop(file);
13886 +       h_file = au_hf_top(file);
13887 +       get_file(h_file);
13888 +       if (wpre)
13889 +               wpre->blks = file_inode(h_file)->i_blocks;
13890 +       if (do_ready)
13891 +               au_unpin(&pin);
13892 +       di_read_unlock(dentry, /*flags*/0);
13893 +
13894 +out_fi:
13895 +       fi_write_unlock(file);
13896 +out:
13897 +       return h_file;
13898 +}
13899 +
13900 +static void au_write_post(struct inode *inode, struct file *h_file,
13901 +                         struct au_write_pre *wpre, ssize_t written)
13902 +{
13903 +       struct inode *h_inode;
13904 +
13905 +       au_cpup_attr_timesizes(inode);
13906 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
13907 +       h_inode = file_inode(h_file);
13908 +       inode->i_mode = h_inode->i_mode;
13909 +       ii_write_unlock(inode);
13910 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13911 +       if (written > 0)
13912 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
13913 +                             /*force*/h_inode->i_blocks > wpre->blks);
13914 +       fput(h_file);
13915 +}
13916 +
13917 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13918 +                        loff_t *ppos)
13919 +{
13920 +       ssize_t err;
13921 +       struct inode *inode;
13922 +       struct file *h_file;
13923 +       struct super_block *sb;
13924 +
13925 +       inode = file_inode(file);
13926 +       sb = inode->i_sb;
13927 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13928 +
13929 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
13930 +       err = PTR_ERR(h_file);
13931 +       if (IS_ERR(h_file))
13932 +               goto out;
13933 +
13934 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13935 +       err = vfsub_read_u(h_file, buf, count, ppos);
13936 +       /* todo: necessary? */
13937 +       /* file->f_ra = h_file->f_ra; */
13938 +       au_read_post(inode, h_file);
13939 +
13940 +out:
13941 +       si_read_unlock(sb);
13942 +       return err;
13943 +}
13944 +
13945 +/*
13946 + * todo: very ugly
13947 + * it locks both of i_mutex and si_rwsem for read in safe.
13948 + * if the plink maintenance mode continues forever (that is the problem),
13949 + * may loop forever.
13950 + */
13951 +static void au_mtx_and_read_lock(struct inode *inode)
13952 +{
13953 +       int err;
13954 +       struct super_block *sb = inode->i_sb;
13955 +
13956 +       while (1) {
13957 +               inode_lock(inode);
13958 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13959 +               if (!err)
13960 +                       break;
13961 +               inode_unlock(inode);
13962 +               si_read_lock(sb, AuLock_NOPLMW);
13963 +               si_read_unlock(sb);
13964 +       }
13965 +}
13966 +
13967 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13968 +                         size_t count, loff_t *ppos)
13969 +{
13970 +       ssize_t err;
13971 +       struct au_write_pre wpre;
13972 +       struct inode *inode;
13973 +       struct file *h_file;
13974 +       char __user *buf = (char __user *)ubuf;
13975 +
13976 +       inode = file_inode(file);
13977 +       au_mtx_and_read_lock(inode);
13978 +
13979 +       wpre.lsc = 0;
13980 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13981 +       err = PTR_ERR(h_file);
13982 +       if (IS_ERR(h_file))
13983 +               goto out;
13984 +
13985 +       err = vfsub_write_u(h_file, buf, count, ppos);
13986 +       au_write_post(inode, h_file, &wpre, err);
13987 +
13988 +out:
13989 +       si_read_unlock(inode->i_sb);
13990 +       inode_unlock(inode);
13991 +       return err;
13992 +}
13993 +
13994 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13995 +                         struct iov_iter *iov_iter)
13996 +{
13997 +       ssize_t err;
13998 +       struct file *file;
13999 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
14000 +
14001 +       err = security_file_permission(h_file, rw);
14002 +       if (unlikely(err))
14003 +               goto out;
14004 +
14005 +       err = -ENOSYS;
14006 +       iter = NULL;
14007 +       if (rw == MAY_READ)
14008 +               iter = h_file->f_op->read_iter;
14009 +       else if (rw == MAY_WRITE)
14010 +               iter = h_file->f_op->write_iter;
14011 +
14012 +       file = kio->ki_filp;
14013 +       kio->ki_filp = h_file;
14014 +       if (iter) {
14015 +               lockdep_off();
14016 +               err = iter(kio, iov_iter);
14017 +               lockdep_on();
14018 +       } else
14019 +               /* currently there is no such fs */
14020 +               WARN_ON_ONCE(1);
14021 +       kio->ki_filp = file;
14022 +
14023 +out:
14024 +       return err;
14025 +}
14026 +
14027 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
14028 +{
14029 +       ssize_t err;
14030 +       struct file *file, *h_file;
14031 +       struct inode *inode;
14032 +       struct super_block *sb;
14033 +
14034 +       file = kio->ki_filp;
14035 +       inode = file_inode(file);
14036 +       sb = inode->i_sb;
14037 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14038 +
14039 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
14040 +       err = PTR_ERR(h_file);
14041 +       if (IS_ERR(h_file))
14042 +               goto out;
14043 +
14044 +       if (au_test_loopback_kthread()) {
14045 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
14046 +               if (file->f_mapping != h_file->f_mapping) {
14047 +                       file->f_mapping = h_file->f_mapping;
14048 +                       smp_mb(); /* unnecessary? */
14049 +               }
14050 +       }
14051 +       fi_read_unlock(file);
14052 +
14053 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
14054 +       /* todo: necessary? */
14055 +       /* file->f_ra = h_file->f_ra; */
14056 +       au_read_post(inode, h_file);
14057 +
14058 +out:
14059 +       si_read_unlock(sb);
14060 +       return err;
14061 +}
14062 +
14063 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
14064 +{
14065 +       ssize_t err;
14066 +       struct au_write_pre wpre;
14067 +       struct inode *inode;
14068 +       struct file *file, *h_file;
14069 +
14070 +       file = kio->ki_filp;
14071 +       inode = file_inode(file);
14072 +       au_mtx_and_read_lock(inode);
14073 +
14074 +       wpre.lsc = 0;
14075 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14076 +       err = PTR_ERR(h_file);
14077 +       if (IS_ERR(h_file))
14078 +               goto out;
14079 +
14080 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
14081 +       au_write_post(inode, h_file, &wpre, err);
14082 +
14083 +out:
14084 +       si_read_unlock(inode->i_sb);
14085 +       inode_unlock(inode);
14086 +       return err;
14087 +}
14088 +
14089 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
14090 +                               struct pipe_inode_info *pipe, size_t len,
14091 +                               unsigned int flags)
14092 +{
14093 +       ssize_t err;
14094 +       struct file *h_file;
14095 +       struct inode *inode;
14096 +       struct super_block *sb;
14097 +
14098 +       inode = file_inode(file);
14099 +       sb = inode->i_sb;
14100 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14101 +
14102 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
14103 +       err = PTR_ERR(h_file);
14104 +       if (IS_ERR(h_file))
14105 +               goto out;
14106 +
14107 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
14108 +       /* todo: necessasry? */
14109 +       /* file->f_ra = h_file->f_ra; */
14110 +       au_read_post(inode, h_file);
14111 +
14112 +out:
14113 +       si_read_unlock(sb);
14114 +       return err;
14115 +}
14116 +
14117 +static ssize_t
14118 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
14119 +                 size_t len, unsigned int flags)
14120 +{
14121 +       ssize_t err;
14122 +       struct au_write_pre wpre;
14123 +       struct inode *inode;
14124 +       struct file *h_file;
14125 +
14126 +       inode = file_inode(file);
14127 +       au_mtx_and_read_lock(inode);
14128 +
14129 +       wpre.lsc = 0;
14130 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14131 +       err = PTR_ERR(h_file);
14132 +       if (IS_ERR(h_file))
14133 +               goto out;
14134 +
14135 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
14136 +       au_write_post(inode, h_file, &wpre, err);
14137 +
14138 +out:
14139 +       si_read_unlock(inode->i_sb);
14140 +       inode_unlock(inode);
14141 +       return err;
14142 +}
14143 +
14144 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
14145 +                          loff_t len)
14146 +{
14147 +       long err;
14148 +       struct au_write_pre wpre;
14149 +       struct inode *inode;
14150 +       struct file *h_file;
14151 +
14152 +       inode = file_inode(file);
14153 +       au_mtx_and_read_lock(inode);
14154 +
14155 +       wpre.lsc = 0;
14156 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14157 +       err = PTR_ERR(h_file);
14158 +       if (IS_ERR(h_file))
14159 +               goto out;
14160 +
14161 +       lockdep_off();
14162 +       err = vfs_fallocate(h_file, mode, offset, len);
14163 +       lockdep_on();
14164 +       au_write_post(inode, h_file, &wpre, /*written*/1);
14165 +
14166 +out:
14167 +       si_read_unlock(inode->i_sb);
14168 +       inode_unlock(inode);
14169 +       return err;
14170 +}
14171 +
14172 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
14173 +                                   struct file *dst, loff_t dst_pos,
14174 +                                   size_t len, unsigned int flags)
14175 +{
14176 +       ssize_t err;
14177 +       struct au_write_pre wpre;
14178 +       enum { SRC, DST };
14179 +       struct {
14180 +               struct inode *inode;
14181 +               struct file *h_file;
14182 +               struct super_block *h_sb;
14183 +       } a[2];
14184 +#define a_src  a[SRC]
14185 +#define a_dst  a[DST]
14186 +
14187 +       err = -EINVAL;
14188 +       a_src.inode = file_inode(src);
14189 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
14190 +               goto out;
14191 +       a_dst.inode = file_inode(dst);
14192 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
14193 +               goto out;
14194 +
14195 +       au_mtx_and_read_lock(a_dst.inode);
14196 +       /*
14197 +        * in order to match the order in di_write_lock2_{child,parent}(),
14198 +        * use f_path.dentry for this comparision.
14199 +        */
14200 +       if (src->f_path.dentry < dst->f_path.dentry) {
14201 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
14202 +               err = PTR_ERR(a_src.h_file);
14203 +               if (IS_ERR(a_src.h_file))
14204 +                       goto out_si;
14205 +
14206 +               wpre.lsc = AuLsc_FI_2;
14207 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
14208 +               err = PTR_ERR(a_dst.h_file);
14209 +               if (IS_ERR(a_dst.h_file)) {
14210 +                       au_read_post(a_src.inode, a_src.h_file);
14211 +                       goto out_si;
14212 +               }
14213 +       } else {
14214 +               wpre.lsc = AuLsc_FI_1;
14215 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
14216 +               err = PTR_ERR(a_dst.h_file);
14217 +               if (IS_ERR(a_dst.h_file))
14218 +                       goto out_si;
14219 +
14220 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
14221 +               err = PTR_ERR(a_src.h_file);
14222 +               if (IS_ERR(a_src.h_file)) {
14223 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
14224 +                                     /*written*/0);
14225 +                       goto out_si;
14226 +               }
14227 +       }
14228 +
14229 +       err = -EXDEV;
14230 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
14231 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
14232 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
14233 +               AuDbgFile(src);
14234 +               AuDbgFile(dst);
14235 +               goto out_file;
14236 +       }
14237 +
14238 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
14239 +                                   dst_pos, len, flags);
14240 +
14241 +out_file:
14242 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
14243 +       fi_read_unlock(src);
14244 +       au_read_post(a_src.inode, a_src.h_file);
14245 +out_si:
14246 +       si_read_unlock(a_dst.inode->i_sb);
14247 +       inode_unlock(a_dst.inode);
14248 +out:
14249 +       return err;
14250 +#undef a_src
14251 +#undef a_dst
14252 +}
14253 +
14254 +/* ---------------------------------------------------------------------- */
14255 +
14256 +/*
14257 + * The locking order around current->mmap_sem.
14258 + * - in most and regular cases
14259 + *   file I/O syscall -- aufs_read() or something
14260 + *     -- si_rwsem for read -- mmap_sem
14261 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
14262 + * - in mmap case
14263 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
14264 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
14265 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
14266 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
14267 + * It means that when aufs acquires si_rwsem for write, the process should never
14268 + * acquire mmap_sem.
14269 + *
14270 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
14271 + * problem either since any directory is not able to be mmap-ed.
14272 + * The similar scenario is applied to aufs_readlink() too.
14273 + */
14274 +
14275 +#if 0 /* stop calling security_file_mmap() */
14276 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
14277 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
14278 +
14279 +static unsigned long au_arch_prot_conv(unsigned long flags)
14280 +{
14281 +       /* currently ppc64 only */
14282 +#ifdef CONFIG_PPC64
14283 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
14284 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
14285 +       return AuConv_VM_PROT(flags, SAO);
14286 +#else
14287 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
14288 +       return 0;
14289 +#endif
14290 +}
14291 +
14292 +static unsigned long au_prot_conv(unsigned long flags)
14293 +{
14294 +       return AuConv_VM_PROT(flags, READ)
14295 +               | AuConv_VM_PROT(flags, WRITE)
14296 +               | AuConv_VM_PROT(flags, EXEC)
14297 +               | au_arch_prot_conv(flags);
14298 +}
14299 +
14300 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
14301 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
14302 +
14303 +static unsigned long au_flag_conv(unsigned long flags)
14304 +{
14305 +       return AuConv_VM_MAP(flags, GROWSDOWN)
14306 +               | AuConv_VM_MAP(flags, DENYWRITE)
14307 +               | AuConv_VM_MAP(flags, LOCKED);
14308 +}
14309 +#endif
14310 +
14311 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
14312 +{
14313 +       int err;
14314 +       const unsigned char wlock
14315 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
14316 +       struct super_block *sb;
14317 +       struct file *h_file;
14318 +       struct inode *inode;
14319 +
14320 +       AuDbgVmRegion(file, vma);
14321 +
14322 +       inode = file_inode(file);
14323 +       sb = inode->i_sb;
14324 +       lockdep_off();
14325 +       si_read_lock(sb, AuLock_NOPLMW);
14326 +
14327 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
14328 +       lockdep_on();
14329 +       err = PTR_ERR(h_file);
14330 +       if (IS_ERR(h_file))
14331 +               goto out;
14332 +
14333 +       err = 0;
14334 +       au_set_mmapped(file);
14335 +       au_vm_file_reset(vma, h_file);
14336 +       /*
14337 +        * we cannot call security_mmap_file() here since it may acquire
14338 +        * mmap_sem or i_mutex.
14339 +        *
14340 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
14341 +        *                       au_flag_conv(vma->vm_flags));
14342 +        */
14343 +       if (!err)
14344 +               err = call_mmap(h_file, vma);
14345 +       if (!err) {
14346 +               au_vm_prfile_set(vma, file);
14347 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
14348 +               goto out_fput; /* success */
14349 +       }
14350 +       au_unset_mmapped(file);
14351 +       au_vm_file_reset(vma, file);
14352 +
14353 +out_fput:
14354 +       lockdep_off();
14355 +       ii_write_unlock(inode);
14356 +       lockdep_on();
14357 +       fput(h_file);
14358 +out:
14359 +       lockdep_off();
14360 +       si_read_unlock(sb);
14361 +       lockdep_on();
14362 +       AuTraceErr(err);
14363 +       return err;
14364 +}
14365 +
14366 +/* ---------------------------------------------------------------------- */
14367 +
14368 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14369 +                            int datasync)
14370 +{
14371 +       int err;
14372 +       struct au_write_pre wpre;
14373 +       struct inode *inode;
14374 +       struct file *h_file;
14375 +
14376 +       err = 0; /* -EBADF; */ /* posix? */
14377 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
14378 +               goto out;
14379 +
14380 +       inode = file_inode(file);
14381 +       au_mtx_and_read_lock(inode);
14382 +
14383 +       wpre.lsc = 0;
14384 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14385 +       err = PTR_ERR(h_file);
14386 +       if (IS_ERR(h_file))
14387 +               goto out_unlock;
14388 +
14389 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
14390 +       au_write_post(inode, h_file, &wpre, /*written*/0);
14391 +
14392 +out_unlock:
14393 +       si_read_unlock(inode->i_sb);
14394 +       inode_unlock(inode);
14395 +out:
14396 +       return err;
14397 +}
14398 +
14399 +static int aufs_fasync(int fd, struct file *file, int flag)
14400 +{
14401 +       int err;
14402 +       struct file *h_file;
14403 +       struct super_block *sb;
14404 +
14405 +       sb = file->f_path.dentry->d_sb;
14406 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14407 +
14408 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
14409 +       err = PTR_ERR(h_file);
14410 +       if (IS_ERR(h_file))
14411 +               goto out;
14412 +
14413 +       if (h_file->f_op->fasync)
14414 +               err = h_file->f_op->fasync(fd, h_file, flag);
14415 +       fput(h_file); /* instead of au_read_post() */
14416 +
14417 +out:
14418 +       si_read_unlock(sb);
14419 +       return err;
14420 +}
14421 +
14422 +static int aufs_setfl(struct file *file, unsigned long arg)
14423 +{
14424 +       int err;
14425 +       struct file *h_file;
14426 +       struct super_block *sb;
14427 +
14428 +       sb = file->f_path.dentry->d_sb;
14429 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14430 +
14431 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
14432 +       err = PTR_ERR(h_file);
14433 +       if (IS_ERR(h_file))
14434 +               goto out;
14435 +
14436 +       /* stop calling h_file->fasync */
14437 +       arg |= vfsub_file_flags(file) & FASYNC;
14438 +       err = setfl(/*unused fd*/-1, h_file, arg);
14439 +       fput(h_file); /* instead of au_read_post() */
14440 +
14441 +out:
14442 +       si_read_unlock(sb);
14443 +       return err;
14444 +}
14445 +
14446 +/* ---------------------------------------------------------------------- */
14447 +
14448 +/* no one supports this operation, currently */
14449 +#if 0
14450 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
14451 +                            size_t len, loff_t *pos, int more)
14452 +{
14453 +}
14454 +#endif
14455 +
14456 +/* ---------------------------------------------------------------------- */
14457 +
14458 +const struct file_operations aufs_file_fop = {
14459 +       .owner          = THIS_MODULE,
14460 +
14461 +       .llseek         = default_llseek,
14462 +
14463 +       .read           = aufs_read,
14464 +       .write          = aufs_write,
14465 +       .read_iter      = aufs_read_iter,
14466 +       .write_iter     = aufs_write_iter,
14467 +
14468 +#ifdef CONFIG_AUFS_POLL
14469 +       .poll           = aufs_poll,
14470 +#endif
14471 +       .unlocked_ioctl = aufs_ioctl_nondir,
14472 +#ifdef CONFIG_COMPAT
14473 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
14474 +#endif
14475 +       .mmap           = aufs_mmap,
14476 +       .open           = aufs_open_nondir,
14477 +       .flush          = aufs_flush_nondir,
14478 +       .release        = aufs_release_nondir,
14479 +       .fsync          = aufs_fsync_nondir,
14480 +       .fasync         = aufs_fasync,
14481 +       /* .sendpage    = aufs_sendpage, */
14482 +       .setfl          = aufs_setfl,
14483 +       .splice_write   = aufs_splice_write,
14484 +       .splice_read    = aufs_splice_read,
14485 +#if 0
14486 +       .aio_splice_write = aufs_aio_splice_write,
14487 +       .aio_splice_read  = aufs_aio_splice_read,
14488 +#endif
14489 +       .fallocate      = aufs_fallocate,
14490 +       .copy_file_range = aufs_copy_file_range
14491 +};
14492 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14493 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
14494 +++ linux/fs/aufs/fstype.h      2017-07-29 12:14:25.903042072 +0200
14495 @@ -0,0 +1,400 @@
14496 +/*
14497 + * Copyright (C) 2005-2017 Junjiro R. Okajima
14498 + *
14499 + * This program, aufs is free software; you can redistribute it and/or modify
14500 + * it under the terms of the GNU General Public License as published by
14501 + * the Free Software Foundation; either version 2 of the License, or
14502 + * (at your option) any later version.
14503 + *
14504 + * This program is distributed in the hope that it will be useful,
14505 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14506 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14507 + * GNU General Public License for more details.
14508 + *
14509 + * You should have received a copy of the GNU General Public License
14510 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14511 + */
14512 +
14513 +/*
14514 + * judging filesystem type
14515 + */
14516 +
14517 +#ifndef __AUFS_FSTYPE_H__
14518 +#define __AUFS_FSTYPE_H__
14519 +
14520 +#ifdef __KERNEL__
14521 +
14522 +#include <linux/fs.h>
14523 +#include <linux/magic.h>
14524 +#include <linux/nfs_fs.h>
14525 +#include <linux/romfs_fs.h>
14526 +
14527 +static inline int au_test_aufs(struct super_block *sb)
14528 +{
14529 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14530 +}
14531 +
14532 +static inline const char *au_sbtype(struct super_block *sb)
14533 +{
14534 +       return sb->s_type->name;
14535 +}
14536 +
14537 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14538 +{
14539 +#if IS_ENABLED(CONFIG_ISO9660_FS)
14540 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14541 +#else
14542 +       return 0;
14543 +#endif
14544 +}
14545 +
14546 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14547 +{
14548 +#if IS_ENABLED(CONFIG_ROMFS_FS)
14549 +       return sb->s_magic == ROMFS_MAGIC;
14550 +#else
14551 +       return 0;
14552 +#endif
14553 +}
14554 +
14555 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14556 +{
14557 +#if IS_ENABLED(CONFIG_CRAMFS)
14558 +       return sb->s_magic == CRAMFS_MAGIC;
14559 +#endif
14560 +       return 0;
14561 +}
14562 +
14563 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14564 +{
14565 +#if IS_ENABLED(CONFIG_NFS_FS)
14566 +       return sb->s_magic == NFS_SUPER_MAGIC;
14567 +#else
14568 +       return 0;
14569 +#endif
14570 +}
14571 +
14572 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14573 +{
14574 +#if IS_ENABLED(CONFIG_FUSE_FS)
14575 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14576 +#else
14577 +       return 0;
14578 +#endif
14579 +}
14580 +
14581 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14582 +{
14583 +#if IS_ENABLED(CONFIG_XFS_FS)
14584 +       return sb->s_magic == XFS_SB_MAGIC;
14585 +#else
14586 +       return 0;
14587 +#endif
14588 +}
14589 +
14590 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14591 +{
14592 +#ifdef CONFIG_TMPFS
14593 +       return sb->s_magic == TMPFS_MAGIC;
14594 +#else
14595 +       return 0;
14596 +#endif
14597 +}
14598 +
14599 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14600 +{
14601 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
14602 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14603 +#else
14604 +       return 0;
14605 +#endif
14606 +}
14607 +
14608 +static inline int au_test_ramfs(struct super_block *sb)
14609 +{
14610 +       return sb->s_magic == RAMFS_MAGIC;
14611 +}
14612 +
14613 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14614 +{
14615 +#if IS_ENABLED(CONFIG_UBIFS_FS)
14616 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14617 +#else
14618 +       return 0;
14619 +#endif
14620 +}
14621 +
14622 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14623 +{
14624 +#ifdef CONFIG_PROC_FS
14625 +       return sb->s_magic == PROC_SUPER_MAGIC;
14626 +#else
14627 +       return 0;
14628 +#endif
14629 +}
14630 +
14631 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14632 +{
14633 +#ifdef CONFIG_SYSFS
14634 +       return sb->s_magic == SYSFS_MAGIC;
14635 +#else
14636 +       return 0;
14637 +#endif
14638 +}
14639 +
14640 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14641 +{
14642 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
14643 +       return sb->s_magic == CONFIGFS_MAGIC;
14644 +#else
14645 +       return 0;
14646 +#endif
14647 +}
14648 +
14649 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14650 +{
14651 +#if IS_ENABLED(CONFIG_MINIX_FS)
14652 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14653 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14654 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14655 +               || sb->s_magic == MINIX_SUPER_MAGIC
14656 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14657 +#else
14658 +       return 0;
14659 +#endif
14660 +}
14661 +
14662 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14663 +{
14664 +#if IS_ENABLED(CONFIG_FAT_FS)
14665 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14666 +#else
14667 +       return 0;
14668 +#endif
14669 +}
14670 +
14671 +static inline int au_test_msdos(struct super_block *sb)
14672 +{
14673 +       return au_test_fat(sb);
14674 +}
14675 +
14676 +static inline int au_test_vfat(struct super_block *sb)
14677 +{
14678 +       return au_test_fat(sb);
14679 +}
14680 +
14681 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14682 +{
14683 +#ifdef CONFIG_SECURITYFS
14684 +       return sb->s_magic == SECURITYFS_MAGIC;
14685 +#else
14686 +       return 0;
14687 +#endif
14688 +}
14689 +
14690 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14691 +{
14692 +#if IS_ENABLED(CONFIG_SQUASHFS)
14693 +       return sb->s_magic == SQUASHFS_MAGIC;
14694 +#else
14695 +       return 0;
14696 +#endif
14697 +}
14698 +
14699 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14700 +{
14701 +#if IS_ENABLED(CONFIG_BTRFS_FS)
14702 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14703 +#else
14704 +       return 0;
14705 +#endif
14706 +}
14707 +
14708 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14709 +{
14710 +#if IS_ENABLED(CONFIG_XENFS)
14711 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14712 +#else
14713 +       return 0;
14714 +#endif
14715 +}
14716 +
14717 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14718 +{
14719 +#ifdef CONFIG_DEBUG_FS
14720 +       return sb->s_magic == DEBUGFS_MAGIC;
14721 +#else
14722 +       return 0;
14723 +#endif
14724 +}
14725 +
14726 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14727 +{
14728 +#if IS_ENABLED(CONFIG_NILFS)
14729 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14730 +#else
14731 +       return 0;
14732 +#endif
14733 +}
14734 +
14735 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14736 +{
14737 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
14738 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14739 +#else
14740 +       return 0;
14741 +#endif
14742 +}
14743 +
14744 +/* ---------------------------------------------------------------------- */
14745 +/*
14746 + * they can't be an aufs branch.
14747 + */
14748 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14749 +{
14750 +       return
14751 +#ifndef CONFIG_AUFS_BR_RAMFS
14752 +               au_test_ramfs(sb) ||
14753 +#endif
14754 +               au_test_procfs(sb)
14755 +               || au_test_sysfs(sb)
14756 +               || au_test_configfs(sb)
14757 +               || au_test_debugfs(sb)
14758 +               || au_test_securityfs(sb)
14759 +               || au_test_xenfs(sb)
14760 +               || au_test_ecryptfs(sb)
14761 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14762 +               || au_test_aufs(sb); /* will be supported in next version */
14763 +}
14764 +
14765 +static inline int au_test_fs_remote(struct super_block *sb)
14766 +{
14767 +       return !au_test_tmpfs(sb)
14768 +#ifdef CONFIG_AUFS_BR_RAMFS
14769 +               && !au_test_ramfs(sb)
14770 +#endif
14771 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14772 +}
14773 +
14774 +/* ---------------------------------------------------------------------- */
14775 +
14776 +/*
14777 + * Note: these functions (below) are created after reading ->getattr() in all
14778 + * filesystems under linux/fs. it means we have to do so in every update...
14779 + */
14780 +
14781 +/*
14782 + * some filesystems require getattr to refresh the inode attributes before
14783 + * referencing.
14784 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14785 + * and leave the work for d_revalidate()
14786 + */
14787 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14788 +{
14789 +       return au_test_nfs(sb)
14790 +               || au_test_fuse(sb)
14791 +               /* || au_test_btrfs(sb) */      /* untested */
14792 +               ;
14793 +}
14794 +
14795 +/*
14796 + * filesystems which don't maintain i_size or i_blocks.
14797 + */
14798 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14799 +{
14800 +       return au_test_xfs(sb)
14801 +               || au_test_btrfs(sb)
14802 +               || au_test_ubifs(sb)
14803 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14804 +               /* || au_test_minix(sb) */      /* untested */
14805 +               ;
14806 +}
14807 +
14808 +/*
14809 + * filesystems which don't store the correct value in some of their inode
14810 + * attributes.
14811 + */
14812 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14813 +{
14814 +       return au_test_fs_bad_iattr_size(sb)
14815 +               || au_test_fat(sb)
14816 +               || au_test_msdos(sb)
14817 +               || au_test_vfat(sb);
14818 +}
14819 +
14820 +/* they don't check i_nlink in link(2) */
14821 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14822 +{
14823 +       return au_test_tmpfs(sb)
14824 +#ifdef CONFIG_AUFS_BR_RAMFS
14825 +               || au_test_ramfs(sb)
14826 +#endif
14827 +               || au_test_ubifs(sb)
14828 +               || au_test_hfsplus(sb);
14829 +}
14830 +
14831 +/*
14832 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14833 + */
14834 +static inline int au_test_fs_notime(struct super_block *sb)
14835 +{
14836 +       return au_test_nfs(sb)
14837 +               || au_test_fuse(sb)
14838 +               || au_test_ubifs(sb)
14839 +               ;
14840 +}
14841 +
14842 +/* temporary support for i#1 in cramfs */
14843 +static inline int au_test_fs_unique_ino(struct inode *inode)
14844 +{
14845 +       if (au_test_cramfs(inode->i_sb))
14846 +               return inode->i_ino != 1;
14847 +       return 1;
14848 +}
14849 +
14850 +/* ---------------------------------------------------------------------- */
14851 +
14852 +/*
14853 + * the filesystem where the xino files placed must support i/o after unlink and
14854 + * maintain i_size and i_blocks.
14855 + */
14856 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14857 +{
14858 +       return au_test_fs_remote(sb)
14859 +               || au_test_fs_bad_iattr_size(sb)
14860 +               /* don't want unnecessary work for xino */
14861 +               || au_test_aufs(sb)
14862 +               || au_test_ecryptfs(sb)
14863 +               || au_test_nilfs(sb);
14864 +}
14865 +
14866 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14867 +{
14868 +       return au_test_tmpfs(sb)
14869 +               || au_test_ramfs(sb);
14870 +}
14871 +
14872 +/*
14873 + * test if the @sb is real-readonly.
14874 + */
14875 +static inline int au_test_fs_rr(struct super_block *sb)
14876 +{
14877 +       return au_test_squashfs(sb)
14878 +               || au_test_iso9660(sb)
14879 +               || au_test_cramfs(sb)
14880 +               || au_test_romfs(sb);
14881 +}
14882 +
14883 +/*
14884 + * test if the @inode is nfs with 'noacl' option
14885 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14886 + */
14887 +static inline int au_test_nfs_noacl(struct inode *inode)
14888 +{
14889 +       return au_test_nfs(inode->i_sb)
14890 +               /* && IS_POSIXACL(inode) */
14891 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14892 +}
14893 +
14894 +#endif /* __KERNEL__ */
14895 +#endif /* __AUFS_FSTYPE_H__ */
14896 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14897 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14898 +++ linux/fs/aufs/hfsnotify.c   2017-07-29 12:14:32.526591175 +0200
14899 @@ -0,0 +1,289 @@
14900 +/*
14901 + * Copyright (C) 2005-2017 Junjiro R. Okajima
14902 + *
14903 + * This program, aufs is free software; you can redistribute it and/or modify
14904 + * it under the terms of the GNU General Public License as published by
14905 + * the Free Software Foundation; either version 2 of the License, or
14906 + * (at your option) any later version.
14907 + *
14908 + * This program is distributed in the hope that it will be useful,
14909 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14910 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14911 + * GNU General Public License for more details.
14912 + *
14913 + * You should have received a copy of the GNU General Public License
14914 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14915 + */
14916 +
14917 +/*
14918 + * fsnotify for the lower directories
14919 + */
14920 +
14921 +#include "aufs.h"
14922 +
14923 +/* FS_IN_IGNORED is unnecessary */
14924 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14925 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14926 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14927 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14928 +
14929 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14930 +{
14931 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14932 +                                            hn_mark);
14933 +       /* AuDbg("here\n"); */
14934 +       au_cache_free_hnotify(hn);
14935 +       smp_mb__before_atomic();
14936 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14937 +               wake_up(&au_hfsn_wq);
14938 +}
14939 +
14940 +static int au_hfsn_alloc(struct au_hinode *hinode)
14941 +{
14942 +       int err;
14943 +       struct au_hnotify *hn;
14944 +       struct super_block *sb;
14945 +       struct au_branch *br;
14946 +       struct fsnotify_mark *mark;
14947 +       aufs_bindex_t bindex;
14948 +
14949 +       hn = hinode->hi_notify;
14950 +       sb = hn->hn_aufs_inode->i_sb;
14951 +       bindex = au_br_index(sb, hinode->hi_id);
14952 +       br = au_sbr(sb, bindex);
14953 +       AuDebugOn(!br->br_hfsn);
14954 +
14955 +       mark = &hn->hn_mark;
14956 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
14957 +       mark->mask = AuHfsnMask;
14958 +       /*
14959 +        * by udba rename or rmdir, aufs assign a new inode to the known
14960 +        * h_inode, so specify 1 to allow dups.
14961 +        */
14962 +       lockdep_off();
14963 +       err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
14964 +                               /*allow_dups*/1);
14965 +       lockdep_on();
14966 +
14967 +       return err;
14968 +}
14969 +
14970 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14971 +{
14972 +       struct fsnotify_mark *mark;
14973 +       unsigned long long ull;
14974 +       struct fsnotify_group *group;
14975 +
14976 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14977 +       BUG_ON(!ull);
14978 +
14979 +       mark = &hn->hn_mark;
14980 +       spin_lock(&mark->lock);
14981 +       group = mark->group;
14982 +       fsnotify_get_group(group);
14983 +       spin_unlock(&mark->lock);
14984 +       lockdep_off();
14985 +       fsnotify_destroy_mark(mark, group);
14986 +       fsnotify_put_mark(mark);
14987 +       fsnotify_put_group(group);
14988 +       lockdep_on();
14989 +
14990 +       /* free hn by myself */
14991 +       return 0;
14992 +}
14993 +
14994 +/* ---------------------------------------------------------------------- */
14995 +
14996 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14997 +{
14998 +       struct fsnotify_mark *mark;
14999 +
15000 +       mark = &hinode->hi_notify->hn_mark;
15001 +       spin_lock(&mark->lock);
15002 +       if (do_set) {
15003 +               AuDebugOn(mark->mask & AuHfsnMask);
15004 +               mark->mask |= AuHfsnMask;
15005 +       } else {
15006 +               AuDebugOn(!(mark->mask & AuHfsnMask));
15007 +               mark->mask &= ~AuHfsnMask;
15008 +       }
15009 +       spin_unlock(&mark->lock);
15010 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
15011 +}
15012 +
15013 +/* ---------------------------------------------------------------------- */
15014 +
15015 +/* #define AuDbgHnotify */
15016 +#ifdef AuDbgHnotify
15017 +static char *au_hfsn_name(u32 mask)
15018 +{
15019 +#ifdef CONFIG_AUFS_DEBUG
15020 +#define test_ret(flag)                         \
15021 +       do {                                    \
15022 +               if (mask & flag)                \
15023 +                       return #flag;           \
15024 +       } while (0)
15025 +       test_ret(FS_ACCESS);
15026 +       test_ret(FS_MODIFY);
15027 +       test_ret(FS_ATTRIB);
15028 +       test_ret(FS_CLOSE_WRITE);
15029 +       test_ret(FS_CLOSE_NOWRITE);
15030 +       test_ret(FS_OPEN);
15031 +       test_ret(FS_MOVED_FROM);
15032 +       test_ret(FS_MOVED_TO);
15033 +       test_ret(FS_CREATE);
15034 +       test_ret(FS_DELETE);
15035 +       test_ret(FS_DELETE_SELF);
15036 +       test_ret(FS_MOVE_SELF);
15037 +       test_ret(FS_UNMOUNT);
15038 +       test_ret(FS_Q_OVERFLOW);
15039 +       test_ret(FS_IN_IGNORED);
15040 +       test_ret(FS_ISDIR);
15041 +       test_ret(FS_IN_ONESHOT);
15042 +       test_ret(FS_EVENT_ON_CHILD);
15043 +       return "";
15044 +#undef test_ret
15045 +#else
15046 +       return "??";
15047 +#endif
15048 +}
15049 +#endif
15050 +
15051 +/* ---------------------------------------------------------------------- */
15052 +
15053 +static void au_hfsn_free_group(struct fsnotify_group *group)
15054 +{
15055 +       struct au_br_hfsnotify *hfsn = group->private;
15056 +
15057 +       /* AuDbg("here\n"); */
15058 +       kfree(hfsn);
15059 +}
15060 +
15061 +static int au_hfsn_handle_event(struct fsnotify_group *group,
15062 +                               struct inode *inode,
15063 +                               struct fsnotify_mark *inode_mark,
15064 +                               struct fsnotify_mark *vfsmount_mark,
15065 +                               u32 mask, const void *data, int data_type,
15066 +                               const unsigned char *file_name, u32 cookie,
15067 +                               struct fsnotify_iter_info *iter_info)
15068 +{
15069 +       int err;
15070 +       struct au_hnotify *hnotify;
15071 +       struct inode *h_dir, *h_inode;
15072 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
15073 +
15074 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
15075 +
15076 +       err = 0;
15077 +       /* if FS_UNMOUNT happens, there must be another bug */
15078 +       AuDebugOn(mask & FS_UNMOUNT);
15079 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
15080 +               goto out;
15081 +
15082 +       h_dir = inode;
15083 +       h_inode = NULL;
15084 +#ifdef AuDbgHnotify
15085 +       au_debug_on();
15086 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
15087 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
15088 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
15089 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
15090 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
15091 +               /* WARN_ON(1); */
15092 +       }
15093 +       au_debug_off();
15094 +#endif
15095 +
15096 +       AuDebugOn(!inode_mark);
15097 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
15098 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
15099 +
15100 +out:
15101 +       return err;
15102 +}
15103 +
15104 +static struct fsnotify_ops au_hfsn_ops = {
15105 +       .handle_event           = au_hfsn_handle_event,
15106 +       .free_group_priv        = au_hfsn_free_group,
15107 +       .free_mark              = au_hfsn_free_mark
15108 +};
15109 +
15110 +/* ---------------------------------------------------------------------- */
15111 +
15112 +static void au_hfsn_fin_br(struct au_branch *br)
15113 +{
15114 +       struct au_br_hfsnotify *hfsn;
15115 +
15116 +       hfsn = br->br_hfsn;
15117 +       if (hfsn) {
15118 +               lockdep_off();
15119 +               fsnotify_put_group(hfsn->hfsn_group);
15120 +               lockdep_on();
15121 +       }
15122 +}
15123 +
15124 +static int au_hfsn_init_br(struct au_branch *br, int perm)
15125 +{
15126 +       int err;
15127 +       struct fsnotify_group *group;
15128 +       struct au_br_hfsnotify *hfsn;
15129 +
15130 +       err = 0;
15131 +       br->br_hfsn = NULL;
15132 +       if (!au_br_hnotifyable(perm))
15133 +               goto out;
15134 +
15135 +       err = -ENOMEM;
15136 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
15137 +       if (unlikely(!hfsn))
15138 +               goto out;
15139 +
15140 +       err = 0;
15141 +       group = fsnotify_alloc_group(&au_hfsn_ops);
15142 +       if (IS_ERR(group)) {
15143 +               err = PTR_ERR(group);
15144 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
15145 +               goto out_hfsn;
15146 +       }
15147 +
15148 +       group->private = hfsn;
15149 +       hfsn->hfsn_group = group;
15150 +       br->br_hfsn = hfsn;
15151 +       goto out; /* success */
15152 +
15153 +out_hfsn:
15154 +       kfree(hfsn);
15155 +out:
15156 +       return err;
15157 +}
15158 +
15159 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
15160 +{
15161 +       int err;
15162 +
15163 +       err = 0;
15164 +       if (!br->br_hfsn)
15165 +               err = au_hfsn_init_br(br, perm);
15166 +
15167 +       return err;
15168 +}
15169 +
15170 +/* ---------------------------------------------------------------------- */
15171 +
15172 +static void au_hfsn_fin(void)
15173 +{
15174 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
15175 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
15176 +}
15177 +
15178 +const struct au_hnotify_op au_hnotify_op = {
15179 +       .ctl            = au_hfsn_ctl,
15180 +       .alloc          = au_hfsn_alloc,
15181 +       .free           = au_hfsn_free,
15182 +
15183 +       .fin            = au_hfsn_fin,
15184 +
15185 +       .reset_br       = au_hfsn_reset_br,
15186 +       .fin_br         = au_hfsn_fin_br,
15187 +       .init_br        = au_hfsn_init_br
15188 +};
15189 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
15190 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
15191 +++ linux/fs/aufs/hfsplus.c     2017-07-29 12:14:25.903042072 +0200
15192 @@ -0,0 +1,56 @@
15193 +/*
15194 + * Copyright (C) 2010-2017 Junjiro R. Okajima
15195 + *
15196 + * This program, aufs is free software; you can redistribute it and/or modify
15197 + * it under the terms of the GNU General Public License as published by
15198 + * the Free Software Foundation; either version 2 of the License, or
15199 + * (at your option) any later version.
15200 + *
15201 + * This program is distributed in the hope that it will be useful,
15202 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15203 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15204 + * GNU General Public License for more details.
15205 + *
15206 + * You should have received a copy of the GNU General Public License
15207 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15208 + */
15209 +
15210 +/*
15211 + * special support for filesystems which aqucires an inode mutex
15212 + * at final closing a file, eg, hfsplus.
15213 + *
15214 + * This trick is very simple and stupid, just to open the file before really
15215 + * neceeary open to tell hfsplus that this is not the final closing.
15216 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
15217 + * and au_h_open_post() after releasing it.
15218 + */
15219 +
15220 +#include "aufs.h"
15221 +
15222 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15223 +                          int force_wr)
15224 +{
15225 +       struct file *h_file;
15226 +       struct dentry *h_dentry;
15227 +
15228 +       h_dentry = au_h_dptr(dentry, bindex);
15229 +       AuDebugOn(!h_dentry);
15230 +       AuDebugOn(d_is_negative(h_dentry));
15231 +
15232 +       h_file = NULL;
15233 +       if (au_test_hfsplus(h_dentry->d_sb)
15234 +           && d_is_reg(h_dentry))
15235 +               h_file = au_h_open(dentry, bindex,
15236 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
15237 +                                  /*file*/NULL, force_wr);
15238 +       return h_file;
15239 +}
15240 +
15241 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15242 +                   struct file *h_file)
15243 +{
15244 +       if (h_file) {
15245 +               fput(h_file);
15246 +               au_sbr_put(dentry->d_sb, bindex);
15247 +       }
15248 +}
15249 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
15250 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
15251 +++ linux/fs/aufs/hnotify.c     2017-07-29 12:14:25.903042072 +0200
15252 @@ -0,0 +1,711 @@
15253 +/*
15254 + * Copyright (C) 2005-2017 Junjiro R. Okajima
15255 + *
15256 + * This program, aufs is free software; you can redistribute it and/or modify
15257 + * it under the terms of the GNU General Public License as published by
15258 + * the Free Software Foundation; either version 2 of the License, or
15259 + * (at your option) any later version.
15260 + *
15261 + * This program is distributed in the hope that it will be useful,
15262 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15263 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15264 + * GNU General Public License for more details.
15265 + *
15266 + * You should have received a copy of the GNU General Public License
15267 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15268 + */
15269 +
15270 +/*
15271 + * abstraction to notify the direct changes on lower directories
15272 + */
15273 +
15274 +#include "aufs.h"
15275 +
15276 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
15277 +{
15278 +       int err;
15279 +       struct au_hnotify *hn;
15280 +
15281 +       err = -ENOMEM;
15282 +       hn = au_cache_alloc_hnotify();
15283 +       if (hn) {
15284 +               hn->hn_aufs_inode = inode;
15285 +               hinode->hi_notify = hn;
15286 +               err = au_hnotify_op.alloc(hinode);
15287 +               AuTraceErr(err);
15288 +               if (unlikely(err)) {
15289 +                       hinode->hi_notify = NULL;
15290 +                       au_cache_free_hnotify(hn);
15291 +                       /*
15292 +                        * The upper dir was removed by udba, but the same named
15293 +                        * dir left. In this case, aufs assignes a new inode
15294 +                        * number and set the monitor again.
15295 +                        * For the lower dir, the old monitnor is still left.
15296 +                        */
15297 +                       if (err == -EEXIST)
15298 +                               err = 0;
15299 +               }
15300 +       }
15301 +
15302 +       AuTraceErr(err);
15303 +       return err;
15304 +}
15305 +
15306 +void au_hn_free(struct au_hinode *hinode)
15307 +{
15308 +       struct au_hnotify *hn;
15309 +
15310 +       hn = hinode->hi_notify;
15311 +       if (hn) {
15312 +               hinode->hi_notify = NULL;
15313 +               if (au_hnotify_op.free(hinode, hn))
15314 +                       au_cache_free_hnotify(hn);
15315 +       }
15316 +}
15317 +
15318 +/* ---------------------------------------------------------------------- */
15319 +
15320 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
15321 +{
15322 +       if (hinode->hi_notify)
15323 +               au_hnotify_op.ctl(hinode, do_set);
15324 +}
15325 +
15326 +void au_hn_reset(struct inode *inode, unsigned int flags)
15327 +{
15328 +       aufs_bindex_t bindex, bbot;
15329 +       struct inode *hi;
15330 +       struct dentry *iwhdentry;
15331 +
15332 +       bbot = au_ibbot(inode);
15333 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
15334 +               hi = au_h_iptr(inode, bindex);
15335 +               if (!hi)
15336 +                       continue;
15337 +
15338 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
15339 +               iwhdentry = au_hi_wh(inode, bindex);
15340 +               if (iwhdentry)
15341 +                       dget(iwhdentry);
15342 +               au_igrab(hi);
15343 +               au_set_h_iptr(inode, bindex, NULL, 0);
15344 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
15345 +                             flags & ~AuHi_XINO);
15346 +               iput(hi);
15347 +               dput(iwhdentry);
15348 +               /* inode_unlock(hi); */
15349 +       }
15350 +}
15351 +
15352 +/* ---------------------------------------------------------------------- */
15353 +
15354 +static int hn_xino(struct inode *inode, struct inode *h_inode)
15355 +{
15356 +       int err;
15357 +       aufs_bindex_t bindex, bbot, bfound, btop;
15358 +       struct inode *h_i;
15359 +
15360 +       err = 0;
15361 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15362 +               pr_warn("branch root dir was changed\n");
15363 +               goto out;
15364 +       }
15365 +
15366 +       bfound = -1;
15367 +       bbot = au_ibbot(inode);
15368 +       btop = au_ibtop(inode);
15369 +#if 0 /* reserved for future use */
15370 +       if (bindex == bbot) {
15371 +               /* keep this ino in rename case */
15372 +               goto out;
15373 +       }
15374 +#endif
15375 +       for (bindex = btop; bindex <= bbot; bindex++)
15376 +               if (au_h_iptr(inode, bindex) == h_inode) {
15377 +                       bfound = bindex;
15378 +                       break;
15379 +               }
15380 +       if (bfound < 0)
15381 +               goto out;
15382 +
15383 +       for (bindex = btop; bindex <= bbot; bindex++) {
15384 +               h_i = au_h_iptr(inode, bindex);
15385 +               if (!h_i)
15386 +                       continue;
15387 +
15388 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
15389 +               /* ignore this error */
15390 +               /* bad action? */
15391 +       }
15392 +
15393 +       /* children inode number will be broken */
15394 +
15395 +out:
15396 +       AuTraceErr(err);
15397 +       return err;
15398 +}
15399 +
15400 +static int hn_gen_tree(struct dentry *dentry)
15401 +{
15402 +       int err, i, j, ndentry;
15403 +       struct au_dcsub_pages dpages;
15404 +       struct au_dpage *dpage;
15405 +       struct dentry **dentries;
15406 +
15407 +       err = au_dpages_init(&dpages, GFP_NOFS);
15408 +       if (unlikely(err))
15409 +               goto out;
15410 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
15411 +       if (unlikely(err))
15412 +               goto out_dpages;
15413 +
15414 +       for (i = 0; i < dpages.ndpage; i++) {
15415 +               dpage = dpages.dpages + i;
15416 +               dentries = dpage->dentries;
15417 +               ndentry = dpage->ndentry;
15418 +               for (j = 0; j < ndentry; j++) {
15419 +                       struct dentry *d;
15420 +
15421 +                       d = dentries[j];
15422 +                       if (IS_ROOT(d))
15423 +                               continue;
15424 +
15425 +                       au_digen_dec(d);
15426 +                       if (d_really_is_positive(d))
15427 +                               /* todo: reset children xino?
15428 +                                  cached children only? */
15429 +                               au_iigen_dec(d_inode(d));
15430 +               }
15431 +       }
15432 +
15433 +out_dpages:
15434 +       au_dpages_free(&dpages);
15435 +
15436 +#if 0
15437 +       /* discard children */
15438 +       dentry_unhash(dentry);
15439 +       dput(dentry);
15440 +#endif
15441 +out:
15442 +       return err;
15443 +}
15444 +
15445 +/*
15446 + * return 0 if processed.
15447 + */
15448 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
15449 +                          const unsigned int isdir)
15450 +{
15451 +       int err;
15452 +       struct dentry *d;
15453 +       struct qstr *dname;
15454 +
15455 +       err = 1;
15456 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15457 +               pr_warn("branch root dir was changed\n");
15458 +               err = 0;
15459 +               goto out;
15460 +       }
15461 +
15462 +       if (!isdir) {
15463 +               AuDebugOn(!name);
15464 +               au_iigen_dec(inode);
15465 +               spin_lock(&inode->i_lock);
15466 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
15467 +                       spin_lock(&d->d_lock);
15468 +                       dname = &d->d_name;
15469 +                       if (dname->len != nlen
15470 +                           && memcmp(dname->name, name, nlen)) {
15471 +                               spin_unlock(&d->d_lock);
15472 +                               continue;
15473 +                       }
15474 +                       err = 0;
15475 +                       au_digen_dec(d);
15476 +                       spin_unlock(&d->d_lock);
15477 +                       break;
15478 +               }
15479 +               spin_unlock(&inode->i_lock);
15480 +       } else {
15481 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
15482 +               d = d_find_any_alias(inode);
15483 +               if (!d) {
15484 +                       au_iigen_dec(inode);
15485 +                       goto out;
15486 +               }
15487 +
15488 +               spin_lock(&d->d_lock);
15489 +               dname = &d->d_name;
15490 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15491 +                       spin_unlock(&d->d_lock);
15492 +                       err = hn_gen_tree(d);
15493 +                       spin_lock(&d->d_lock);
15494 +               }
15495 +               spin_unlock(&d->d_lock);
15496 +               dput(d);
15497 +       }
15498 +
15499 +out:
15500 +       AuTraceErr(err);
15501 +       return err;
15502 +}
15503 +
15504 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15505 +{
15506 +       int err;
15507 +
15508 +       if (IS_ROOT(dentry)) {
15509 +               pr_warn("branch root dir was changed\n");
15510 +               return 0;
15511 +       }
15512 +
15513 +       err = 0;
15514 +       if (!isdir) {
15515 +               au_digen_dec(dentry);
15516 +               if (d_really_is_positive(dentry))
15517 +                       au_iigen_dec(d_inode(dentry));
15518 +       } else {
15519 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15520 +               if (d_really_is_positive(dentry))
15521 +                       err = hn_gen_tree(dentry);
15522 +       }
15523 +
15524 +       AuTraceErr(err);
15525 +       return err;
15526 +}
15527 +
15528 +/* ---------------------------------------------------------------------- */
15529 +
15530 +/* hnotify job flags */
15531 +#define AuHnJob_XINO0          1
15532 +#define AuHnJob_GEN            (1 << 1)
15533 +#define AuHnJob_DIRENT         (1 << 2)
15534 +#define AuHnJob_ISDIR          (1 << 3)
15535 +#define AuHnJob_TRYXINO0       (1 << 4)
15536 +#define AuHnJob_MNTPNT         (1 << 5)
15537 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15538 +#define au_fset_hnjob(flags, name) \
15539 +       do { (flags) |= AuHnJob_##name; } while (0)
15540 +#define au_fclr_hnjob(flags, name) \
15541 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15542 +
15543 +enum {
15544 +       AuHn_CHILD,
15545 +       AuHn_PARENT,
15546 +       AuHnLast
15547 +};
15548 +
15549 +struct au_hnotify_args {
15550 +       struct inode *h_dir, *dir, *h_child_inode;
15551 +       u32 mask;
15552 +       unsigned int flags[AuHnLast];
15553 +       unsigned int h_child_nlen;
15554 +       char h_child_name[];
15555 +};
15556 +
15557 +struct hn_job_args {
15558 +       unsigned int flags;
15559 +       struct inode *inode, *h_inode, *dir, *h_dir;
15560 +       struct dentry *dentry;
15561 +       char *h_name;
15562 +       int h_nlen;
15563 +};
15564 +
15565 +static int hn_job(struct hn_job_args *a)
15566 +{
15567 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15568 +       int e;
15569 +
15570 +       /* reset xino */
15571 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15572 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15573 +
15574 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15575 +           && a->inode
15576 +           && a->h_inode) {
15577 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
15578 +               if (!a->h_inode->i_nlink
15579 +                   && !(a->h_inode->i_state & I_LINKABLE))
15580 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15581 +               inode_unlock(a->h_inode);
15582 +       }
15583 +
15584 +       /* make the generation obsolete */
15585 +       if (au_ftest_hnjob(a->flags, GEN)) {
15586 +               e = -1;
15587 +               if (a->inode)
15588 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15589 +                                             isdir);
15590 +               if (e && a->dentry)
15591 +                       hn_gen_by_name(a->dentry, isdir);
15592 +               /* ignore this error */
15593 +       }
15594 +
15595 +       /* make dir entries obsolete */
15596 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15597 +               struct au_vdir *vdir;
15598 +
15599 +               vdir = au_ivdir(a->inode);
15600 +               if (vdir)
15601 +                       vdir->vd_jiffy = 0;
15602 +               /* IMustLock(a->inode); */
15603 +               /* a->inode->i_version++; */
15604 +       }
15605 +
15606 +       /* can do nothing but warn */
15607 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15608 +           && a->dentry
15609 +           && d_mountpoint(a->dentry))
15610 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15611 +
15612 +       return 0;
15613 +}
15614 +
15615 +/* ---------------------------------------------------------------------- */
15616 +
15617 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15618 +                                          struct inode *dir)
15619 +{
15620 +       struct dentry *dentry, *d, *parent;
15621 +       struct qstr *dname;
15622 +
15623 +       parent = d_find_any_alias(dir);
15624 +       if (!parent)
15625 +               return NULL;
15626 +
15627 +       dentry = NULL;
15628 +       spin_lock(&parent->d_lock);
15629 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15630 +               /* AuDbg("%pd\n", d); */
15631 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15632 +               dname = &d->d_name;
15633 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15634 +                       goto cont_unlock;
15635 +               if (au_di(d))
15636 +                       au_digen_dec(d);
15637 +               else
15638 +                       goto cont_unlock;
15639 +               if (au_dcount(d) > 0) {
15640 +                       dentry = dget_dlock(d);
15641 +                       spin_unlock(&d->d_lock);
15642 +                       break;
15643 +               }
15644 +
15645 +cont_unlock:
15646 +               spin_unlock(&d->d_lock);
15647 +       }
15648 +       spin_unlock(&parent->d_lock);
15649 +       dput(parent);
15650 +
15651 +       if (dentry)
15652 +               di_write_lock_child(dentry);
15653 +
15654 +       return dentry;
15655 +}
15656 +
15657 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15658 +                                        aufs_bindex_t bindex, ino_t h_ino)
15659 +{
15660 +       struct inode *inode;
15661 +       ino_t ino;
15662 +       int err;
15663 +
15664 +       inode = NULL;
15665 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15666 +       if (!err && ino)
15667 +               inode = ilookup(sb, ino);
15668 +       if (!inode)
15669 +               goto out;
15670 +
15671 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15672 +               pr_warn("wrong root branch\n");
15673 +               iput(inode);
15674 +               inode = NULL;
15675 +               goto out;
15676 +       }
15677 +
15678 +       ii_write_lock_child(inode);
15679 +
15680 +out:
15681 +       return inode;
15682 +}
15683 +
15684 +static void au_hn_bh(void *_args)
15685 +{
15686 +       struct au_hnotify_args *a = _args;
15687 +       struct super_block *sb;
15688 +       aufs_bindex_t bindex, bbot, bfound;
15689 +       unsigned char xino, try_iput;
15690 +       int err;
15691 +       struct inode *inode;
15692 +       ino_t h_ino;
15693 +       struct hn_job_args args;
15694 +       struct dentry *dentry;
15695 +       struct au_sbinfo *sbinfo;
15696 +
15697 +       AuDebugOn(!_args);
15698 +       AuDebugOn(!a->h_dir);
15699 +       AuDebugOn(!a->dir);
15700 +       AuDebugOn(!a->mask);
15701 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15702 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15703 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15704 +
15705 +       inode = NULL;
15706 +       dentry = NULL;
15707 +       /*
15708 +        * do not lock a->dir->i_mutex here
15709 +        * because of d_revalidate() may cause a deadlock.
15710 +        */
15711 +       sb = a->dir->i_sb;
15712 +       AuDebugOn(!sb);
15713 +       sbinfo = au_sbi(sb);
15714 +       AuDebugOn(!sbinfo);
15715 +       si_write_lock(sb, AuLock_NOPLMW);
15716 +
15717 +       ii_read_lock_parent(a->dir);
15718 +       bfound = -1;
15719 +       bbot = au_ibbot(a->dir);
15720 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
15721 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15722 +                       bfound = bindex;
15723 +                       break;
15724 +               }
15725 +       ii_read_unlock(a->dir);
15726 +       if (unlikely(bfound < 0))
15727 +               goto out;
15728 +
15729 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15730 +       h_ino = 0;
15731 +       if (a->h_child_inode)
15732 +               h_ino = a->h_child_inode->i_ino;
15733 +
15734 +       if (a->h_child_nlen
15735 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15736 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15737 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15738 +                                             a->dir);
15739 +       try_iput = 0;
15740 +       if (dentry && d_really_is_positive(dentry))
15741 +               inode = d_inode(dentry);
15742 +       if (xino && !inode && h_ino
15743 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15744 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15745 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15746 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15747 +               try_iput = 1;
15748 +       }
15749 +
15750 +       args.flags = a->flags[AuHn_CHILD];
15751 +       args.dentry = dentry;
15752 +       args.inode = inode;
15753 +       args.h_inode = a->h_child_inode;
15754 +       args.dir = a->dir;
15755 +       args.h_dir = a->h_dir;
15756 +       args.h_name = a->h_child_name;
15757 +       args.h_nlen = a->h_child_nlen;
15758 +       err = hn_job(&args);
15759 +       if (dentry) {
15760 +               if (au_di(dentry))
15761 +                       di_write_unlock(dentry);
15762 +               dput(dentry);
15763 +       }
15764 +       if (inode && try_iput) {
15765 +               ii_write_unlock(inode);
15766 +               iput(inode);
15767 +       }
15768 +
15769 +       ii_write_lock_parent(a->dir);
15770 +       args.flags = a->flags[AuHn_PARENT];
15771 +       args.dentry = NULL;
15772 +       args.inode = a->dir;
15773 +       args.h_inode = a->h_dir;
15774 +       args.dir = NULL;
15775 +       args.h_dir = NULL;
15776 +       args.h_name = NULL;
15777 +       args.h_nlen = 0;
15778 +       err = hn_job(&args);
15779 +       ii_write_unlock(a->dir);
15780 +
15781 +out:
15782 +       iput(a->h_child_inode);
15783 +       iput(a->h_dir);
15784 +       iput(a->dir);
15785 +       si_write_unlock(sb);
15786 +       au_nwt_done(&sbinfo->si_nowait);
15787 +       kfree(a);
15788 +}
15789 +
15790 +/* ---------------------------------------------------------------------- */
15791 +
15792 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15793 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15794 +{
15795 +       int err, len;
15796 +       unsigned int flags[AuHnLast], f;
15797 +       unsigned char isdir, isroot, wh;
15798 +       struct inode *dir;
15799 +       struct au_hnotify_args *args;
15800 +       char *p, *h_child_name;
15801 +
15802 +       err = 0;
15803 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15804 +       dir = igrab(hnotify->hn_aufs_inode);
15805 +       if (!dir)
15806 +               goto out;
15807 +
15808 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15809 +       wh = 0;
15810 +       h_child_name = (void *)h_child_qstr->name;
15811 +       len = h_child_qstr->len;
15812 +       if (h_child_name) {
15813 +               if (len > AUFS_WH_PFX_LEN
15814 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15815 +                       h_child_name += AUFS_WH_PFX_LEN;
15816 +                       len -= AUFS_WH_PFX_LEN;
15817 +                       wh = 1;
15818 +               }
15819 +       }
15820 +
15821 +       isdir = 0;
15822 +       if (h_child_inode)
15823 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15824 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15825 +       flags[AuHn_CHILD] = 0;
15826 +       if (isdir)
15827 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15828 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15829 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15830 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15831 +       case FS_MOVED_FROM:
15832 +       case FS_MOVED_TO:
15833 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15834 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15835 +               /*FALLTHROUGH*/
15836 +       case FS_CREATE:
15837 +               AuDebugOn(!h_child_name);
15838 +               break;
15839 +
15840 +       case FS_DELETE:
15841 +               /*
15842 +                * aufs never be able to get this child inode.
15843 +                * revalidation should be in d_revalidate()
15844 +                * by checking i_nlink, i_generation or d_unhashed().
15845 +                */
15846 +               AuDebugOn(!h_child_name);
15847 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15848 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15849 +               break;
15850 +
15851 +       default:
15852 +               AuDebugOn(1);
15853 +       }
15854 +
15855 +       if (wh)
15856 +               h_child_inode = NULL;
15857 +
15858 +       err = -ENOMEM;
15859 +       /* iput() and kfree() will be called in au_hnotify() */
15860 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15861 +       if (unlikely(!args)) {
15862 +               AuErr1("no memory\n");
15863 +               iput(dir);
15864 +               goto out;
15865 +       }
15866 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15867 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15868 +       args->mask = mask;
15869 +       args->dir = dir;
15870 +       args->h_dir = igrab(h_dir);
15871 +       if (h_child_inode)
15872 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15873 +       args->h_child_inode = h_child_inode;
15874 +       args->h_child_nlen = len;
15875 +       if (len) {
15876 +               p = (void *)args;
15877 +               p += sizeof(*args);
15878 +               memcpy(p, h_child_name, len);
15879 +               p[len] = 0;
15880 +       }
15881 +
15882 +       /* NFS fires the event for silly-renamed one from kworker */
15883 +       f = 0;
15884 +       if (!dir->i_nlink
15885 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15886 +               f = AuWkq_NEST;
15887 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15888 +       if (unlikely(err)) {
15889 +               pr_err("wkq %d\n", err);
15890 +               iput(args->h_child_inode);
15891 +               iput(args->h_dir);
15892 +               iput(args->dir);
15893 +               kfree(args);
15894 +       }
15895 +
15896 +out:
15897 +       return err;
15898 +}
15899 +
15900 +/* ---------------------------------------------------------------------- */
15901 +
15902 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15903 +{
15904 +       int err;
15905 +
15906 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15907 +
15908 +       err = 0;
15909 +       if (au_hnotify_op.reset_br)
15910 +               err = au_hnotify_op.reset_br(udba, br, perm);
15911 +
15912 +       return err;
15913 +}
15914 +
15915 +int au_hnotify_init_br(struct au_branch *br, int perm)
15916 +{
15917 +       int err;
15918 +
15919 +       err = 0;
15920 +       if (au_hnotify_op.init_br)
15921 +               err = au_hnotify_op.init_br(br, perm);
15922 +
15923 +       return err;
15924 +}
15925 +
15926 +void au_hnotify_fin_br(struct au_branch *br)
15927 +{
15928 +       if (au_hnotify_op.fin_br)
15929 +               au_hnotify_op.fin_br(br);
15930 +}
15931 +
15932 +static void au_hn_destroy_cache(void)
15933 +{
15934 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
15935 +       au_cache[AuCache_HNOTIFY] = NULL;
15936 +}
15937 +
15938 +int __init au_hnotify_init(void)
15939 +{
15940 +       int err;
15941 +
15942 +       err = -ENOMEM;
15943 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
15944 +       if (au_cache[AuCache_HNOTIFY]) {
15945 +               err = 0;
15946 +               if (au_hnotify_op.init)
15947 +                       err = au_hnotify_op.init();
15948 +               if (unlikely(err))
15949 +                       au_hn_destroy_cache();
15950 +       }
15951 +       AuTraceErr(err);
15952 +       return err;
15953 +}
15954 +
15955 +void au_hnotify_fin(void)
15956 +{
15957 +       if (au_hnotify_op.fin)
15958 +               au_hnotify_op.fin();
15959 +
15960 +       /* cf. au_cache_fin() */
15961 +       if (au_cache[AuCache_HNOTIFY])
15962 +               au_hn_destroy_cache();
15963 +}
15964 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15965 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15966 +++ linux/fs/aufs/iinfo.c       2017-07-29 12:14:25.903042072 +0200
15967 @@ -0,0 +1,285 @@
15968 +/*
15969 + * Copyright (C) 2005-2017 Junjiro R. Okajima
15970 + *
15971 + * This program, aufs is free software; you can redistribute it and/or modify
15972 + * it under the terms of the GNU General Public License as published by
15973 + * the Free Software Foundation; either version 2 of the License, or
15974 + * (at your option) any later version.
15975 + *
15976 + * This program is distributed in the hope that it will be useful,
15977 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15978 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15979 + * GNU General Public License for more details.
15980 + *
15981 + * You should have received a copy of the GNU General Public License
15982 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15983 + */
15984 +
15985 +/*
15986 + * inode private data
15987 + */
15988 +
15989 +#include "aufs.h"
15990 +
15991 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15992 +{
15993 +       struct inode *h_inode;
15994 +       struct au_hinode *hinode;
15995 +
15996 +       IiMustAnyLock(inode);
15997 +
15998 +       hinode = au_hinode(au_ii(inode), bindex);
15999 +       h_inode = hinode->hi_inode;
16000 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
16001 +       return h_inode;
16002 +}
16003 +
16004 +/* todo: hard/soft set? */
16005 +void au_hiput(struct au_hinode *hinode)
16006 +{
16007 +       au_hn_free(hinode);
16008 +       dput(hinode->hi_whdentry);
16009 +       iput(hinode->hi_inode);
16010 +}
16011 +
16012 +unsigned int au_hi_flags(struct inode *inode, int isdir)
16013 +{
16014 +       unsigned int flags;
16015 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
16016 +
16017 +       flags = 0;
16018 +       if (au_opt_test(mnt_flags, XINO))
16019 +               au_fset_hi(flags, XINO);
16020 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
16021 +               au_fset_hi(flags, HNOTIFY);
16022 +       return flags;
16023 +}
16024 +
16025 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16026 +                  struct inode *h_inode, unsigned int flags)
16027 +{
16028 +       struct au_hinode *hinode;
16029 +       struct inode *hi;
16030 +       struct au_iinfo *iinfo = au_ii(inode);
16031 +
16032 +       IiMustWriteLock(inode);
16033 +
16034 +       hinode = au_hinode(iinfo, bindex);
16035 +       hi = hinode->hi_inode;
16036 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
16037 +
16038 +       if (hi)
16039 +               au_hiput(hinode);
16040 +       hinode->hi_inode = h_inode;
16041 +       if (h_inode) {
16042 +               int err;
16043 +               struct super_block *sb = inode->i_sb;
16044 +               struct au_branch *br;
16045 +
16046 +               AuDebugOn(inode->i_mode
16047 +                         && (h_inode->i_mode & S_IFMT)
16048 +                         != (inode->i_mode & S_IFMT));
16049 +               if (bindex == iinfo->ii_btop)
16050 +                       au_cpup_igen(inode, h_inode);
16051 +               br = au_sbr(sb, bindex);
16052 +               hinode->hi_id = br->br_id;
16053 +               if (au_ftest_hi(flags, XINO)) {
16054 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
16055 +                                           inode->i_ino);
16056 +                       if (unlikely(err))
16057 +                               AuIOErr1("failed au_xino_write() %d\n", err);
16058 +               }
16059 +
16060 +               if (au_ftest_hi(flags, HNOTIFY)
16061 +                   && au_br_hnotifyable(br->br_perm)) {
16062 +                       err = au_hn_alloc(hinode, inode);
16063 +                       if (unlikely(err))
16064 +                               AuIOErr1("au_hn_alloc() %d\n", err);
16065 +               }
16066 +       }
16067 +}
16068 +
16069 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16070 +                 struct dentry *h_wh)
16071 +{
16072 +       struct au_hinode *hinode;
16073 +
16074 +       IiMustWriteLock(inode);
16075 +
16076 +       hinode = au_hinode(au_ii(inode), bindex);
16077 +       AuDebugOn(hinode->hi_whdentry);
16078 +       hinode->hi_whdentry = h_wh;
16079 +}
16080 +
16081 +void au_update_iigen(struct inode *inode, int half)
16082 +{
16083 +       struct au_iinfo *iinfo;
16084 +       struct au_iigen *iigen;
16085 +       unsigned int sigen;
16086 +
16087 +       sigen = au_sigen(inode->i_sb);
16088 +       iinfo = au_ii(inode);
16089 +       iigen = &iinfo->ii_generation;
16090 +       spin_lock(&iigen->ig_spin);
16091 +       iigen->ig_generation = sigen;
16092 +       if (half)
16093 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
16094 +       else
16095 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
16096 +       spin_unlock(&iigen->ig_spin);
16097 +}
16098 +
16099 +/* it may be called at remount time, too */
16100 +void au_update_ibrange(struct inode *inode, int do_put_zero)
16101 +{
16102 +       struct au_iinfo *iinfo;
16103 +       aufs_bindex_t bindex, bbot;
16104 +
16105 +       AuDebugOn(au_is_bad_inode(inode));
16106 +       IiMustWriteLock(inode);
16107 +
16108 +       iinfo = au_ii(inode);
16109 +       if (do_put_zero && iinfo->ii_btop >= 0) {
16110 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
16111 +                    bindex++) {
16112 +                       struct inode *h_i;
16113 +
16114 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
16115 +                       if (h_i
16116 +                           && !h_i->i_nlink
16117 +                           && !(h_i->i_state & I_LINKABLE))
16118 +                               au_set_h_iptr(inode, bindex, NULL, 0);
16119 +               }
16120 +       }
16121 +
16122 +       iinfo->ii_btop = -1;
16123 +       iinfo->ii_bbot = -1;
16124 +       bbot = au_sbbot(inode->i_sb);
16125 +       for (bindex = 0; bindex <= bbot; bindex++)
16126 +               if (au_hinode(iinfo, bindex)->hi_inode) {
16127 +                       iinfo->ii_btop = bindex;
16128 +                       break;
16129 +               }
16130 +       if (iinfo->ii_btop >= 0)
16131 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
16132 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
16133 +                               iinfo->ii_bbot = bindex;
16134 +                               break;
16135 +                       }
16136 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
16137 +}
16138 +
16139 +/* ---------------------------------------------------------------------- */
16140 +
16141 +void au_icntnr_init_once(void *_c)
16142 +{
16143 +       struct au_icntnr *c = _c;
16144 +       struct au_iinfo *iinfo = &c->iinfo;
16145 +
16146 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
16147 +       au_rw_init(&iinfo->ii_rwsem);
16148 +       inode_init_once(&c->vfs_inode);
16149 +}
16150 +
16151 +void au_hinode_init(struct au_hinode *hinode)
16152 +{
16153 +       hinode->hi_inode = NULL;
16154 +       hinode->hi_id = -1;
16155 +       au_hn_init(hinode);
16156 +       hinode->hi_whdentry = NULL;
16157 +}
16158 +
16159 +int au_iinfo_init(struct inode *inode)
16160 +{
16161 +       struct au_iinfo *iinfo;
16162 +       struct super_block *sb;
16163 +       struct au_hinode *hi;
16164 +       int nbr, i;
16165 +
16166 +       sb = inode->i_sb;
16167 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16168 +       nbr = au_sbbot(sb) + 1;
16169 +       if (unlikely(nbr <= 0))
16170 +               nbr = 1;
16171 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
16172 +       if (hi) {
16173 +               au_ninodes_inc(sb);
16174 +
16175 +               iinfo->ii_hinode = hi;
16176 +               for (i = 0; i < nbr; i++, hi++)
16177 +                       au_hinode_init(hi);
16178 +
16179 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
16180 +               iinfo->ii_btop = -1;
16181 +               iinfo->ii_bbot = -1;
16182 +               iinfo->ii_vdir = NULL;
16183 +               return 0;
16184 +       }
16185 +       return -ENOMEM;
16186 +}
16187 +
16188 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
16189 +{
16190 +       int err, i;
16191 +       struct au_hinode *hip;
16192 +
16193 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
16194 +
16195 +       err = -ENOMEM;
16196 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
16197 +                         may_shrink);
16198 +       if (hip) {
16199 +               iinfo->ii_hinode = hip;
16200 +               i = iinfo->ii_bbot + 1;
16201 +               hip += i;
16202 +               for (; i < nbr; i++, hip++)
16203 +                       au_hinode_init(hip);
16204 +               err = 0;
16205 +       }
16206 +
16207 +       return err;
16208 +}
16209 +
16210 +void au_iinfo_fin(struct inode *inode)
16211 +{
16212 +       struct au_iinfo *iinfo;
16213 +       struct au_hinode *hi;
16214 +       struct super_block *sb;
16215 +       aufs_bindex_t bindex, bbot;
16216 +       const unsigned char unlinked = !inode->i_nlink;
16217 +
16218 +       AuDebugOn(au_is_bad_inode(inode));
16219 +
16220 +       sb = inode->i_sb;
16221 +       au_ninodes_dec(sb);
16222 +       if (si_pid_test(sb))
16223 +               au_xino_delete_inode(inode, unlinked);
16224 +       else {
16225 +               /*
16226 +                * it is safe to hide the dependency between sbinfo and
16227 +                * sb->s_umount.
16228 +                */
16229 +               lockdep_off();
16230 +               si_noflush_read_lock(sb);
16231 +               au_xino_delete_inode(inode, unlinked);
16232 +               si_read_unlock(sb);
16233 +               lockdep_on();
16234 +       }
16235 +
16236 +       iinfo = au_ii(inode);
16237 +       if (iinfo->ii_vdir)
16238 +               au_vdir_free(iinfo->ii_vdir);
16239 +
16240 +       bindex = iinfo->ii_btop;
16241 +       if (bindex >= 0) {
16242 +               hi = au_hinode(iinfo, bindex);
16243 +               bbot = iinfo->ii_bbot;
16244 +               while (bindex++ <= bbot) {
16245 +                       if (hi->hi_inode)
16246 +                               au_hiput(hi);
16247 +                       hi++;
16248 +               }
16249 +       }
16250 +       kfree(iinfo->ii_hinode);
16251 +       AuRwDestroy(&iinfo->ii_rwsem);
16252 +}
16253 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
16254 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
16255 +++ linux/fs/aufs/inode.c       2017-07-29 12:14:25.903042072 +0200
16256 @@ -0,0 +1,527 @@
16257 +/*
16258 + * Copyright (C) 2005-2017 Junjiro R. Okajima
16259 + *
16260 + * This program, aufs is free software; you can redistribute it and/or modify
16261 + * it under the terms of the GNU General Public License as published by
16262 + * the Free Software Foundation; either version 2 of the License, or
16263 + * (at your option) any later version.
16264 + *
16265 + * This program is distributed in the hope that it will be useful,
16266 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16267 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16268 + * GNU General Public License for more details.
16269 + *
16270 + * You should have received a copy of the GNU General Public License
16271 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16272 + */
16273 +
16274 +/*
16275 + * inode functions
16276 + */
16277 +
16278 +#include "aufs.h"
16279 +
16280 +struct inode *au_igrab(struct inode *inode)
16281 +{
16282 +       if (inode) {
16283 +               AuDebugOn(!atomic_read(&inode->i_count));
16284 +               ihold(inode);
16285 +       }
16286 +       return inode;
16287 +}
16288 +
16289 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
16290 +{
16291 +       au_cpup_attr_all(inode, /*force*/0);
16292 +       au_update_iigen(inode, /*half*/1);
16293 +       if (do_version)
16294 +               inode->i_version++;
16295 +}
16296 +
16297 +static int au_ii_refresh(struct inode *inode, int *update)
16298 +{
16299 +       int err, e, nbr;
16300 +       umode_t type;
16301 +       aufs_bindex_t bindex, new_bindex;
16302 +       struct super_block *sb;
16303 +       struct au_iinfo *iinfo;
16304 +       struct au_hinode *p, *q, tmp;
16305 +
16306 +       AuDebugOn(au_is_bad_inode(inode));
16307 +       IiMustWriteLock(inode);
16308 +
16309 +       *update = 0;
16310 +       sb = inode->i_sb;
16311 +       nbr = au_sbbot(sb) + 1;
16312 +       type = inode->i_mode & S_IFMT;
16313 +       iinfo = au_ii(inode);
16314 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
16315 +       if (unlikely(err))
16316 +               goto out;
16317 +
16318 +       AuDebugOn(iinfo->ii_btop < 0);
16319 +       p = au_hinode(iinfo, iinfo->ii_btop);
16320 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
16321 +            bindex++, p++) {
16322 +               if (!p->hi_inode)
16323 +                       continue;
16324 +
16325 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
16326 +               new_bindex = au_br_index(sb, p->hi_id);
16327 +               if (new_bindex == bindex)
16328 +                       continue;
16329 +
16330 +               if (new_bindex < 0) {
16331 +                       *update = 1;
16332 +                       au_hiput(p);
16333 +                       p->hi_inode = NULL;
16334 +                       continue;
16335 +               }
16336 +
16337 +               if (new_bindex < iinfo->ii_btop)
16338 +                       iinfo->ii_btop = new_bindex;
16339 +               if (iinfo->ii_bbot < new_bindex)
16340 +                       iinfo->ii_bbot = new_bindex;
16341 +               /* swap two lower inode, and loop again */
16342 +               q = au_hinode(iinfo, new_bindex);
16343 +               tmp = *q;
16344 +               *q = *p;
16345 +               *p = tmp;
16346 +               if (tmp.hi_inode) {
16347 +                       bindex--;
16348 +                       p--;
16349 +               }
16350 +       }
16351 +       au_update_ibrange(inode, /*do_put_zero*/0);
16352 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
16353 +       e = au_dy_irefresh(inode);
16354 +       if (unlikely(e && !err))
16355 +               err = e;
16356 +
16357 +out:
16358 +       AuTraceErr(err);
16359 +       return err;
16360 +}
16361 +
16362 +void au_refresh_iop(struct inode *inode, int force_getattr)
16363 +{
16364 +       int type;
16365 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
16366 +       const struct inode_operations *iop
16367 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
16368 +
16369 +       if (inode->i_op == iop)
16370 +               return;
16371 +
16372 +       switch (inode->i_mode & S_IFMT) {
16373 +       case S_IFDIR:
16374 +               type = AuIop_DIR;
16375 +               break;
16376 +       case S_IFLNK:
16377 +               type = AuIop_SYMLINK;
16378 +               break;
16379 +       default:
16380 +               type = AuIop_OTHER;
16381 +               break;
16382 +       }
16383 +
16384 +       inode->i_op = iop + type;
16385 +       /* unnecessary smp_wmb() */
16386 +}
16387 +
16388 +int au_refresh_hinode_self(struct inode *inode)
16389 +{
16390 +       int err, update;
16391 +
16392 +       err = au_ii_refresh(inode, &update);
16393 +       if (!err)
16394 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
16395 +
16396 +       AuTraceErr(err);
16397 +       return err;
16398 +}
16399 +
16400 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
16401 +{
16402 +       int err, e, update;
16403 +       unsigned int flags;
16404 +       umode_t mode;
16405 +       aufs_bindex_t bindex, bbot;
16406 +       unsigned char isdir;
16407 +       struct au_hinode *p;
16408 +       struct au_iinfo *iinfo;
16409 +
16410 +       err = au_ii_refresh(inode, &update);
16411 +       if (unlikely(err))
16412 +               goto out;
16413 +
16414 +       update = 0;
16415 +       iinfo = au_ii(inode);
16416 +       p = au_hinode(iinfo, iinfo->ii_btop);
16417 +       mode = (inode->i_mode & S_IFMT);
16418 +       isdir = S_ISDIR(mode);
16419 +       flags = au_hi_flags(inode, isdir);
16420 +       bbot = au_dbbot(dentry);
16421 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
16422 +               struct inode *h_i, *h_inode;
16423 +               struct dentry *h_d;
16424 +
16425 +               h_d = au_h_dptr(dentry, bindex);
16426 +               if (!h_d || d_is_negative(h_d))
16427 +                       continue;
16428 +
16429 +               h_inode = d_inode(h_d);
16430 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
16431 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
16432 +                       h_i = au_h_iptr(inode, bindex);
16433 +                       if (h_i) {
16434 +                               if (h_i == h_inode)
16435 +                                       continue;
16436 +                               err = -EIO;
16437 +                               break;
16438 +                       }
16439 +               }
16440 +               if (bindex < iinfo->ii_btop)
16441 +                       iinfo->ii_btop = bindex;
16442 +               if (iinfo->ii_bbot < bindex)
16443 +                       iinfo->ii_bbot = bindex;
16444 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
16445 +               update = 1;
16446 +       }
16447 +       au_update_ibrange(inode, /*do_put_zero*/0);
16448 +       e = au_dy_irefresh(inode);
16449 +       if (unlikely(e && !err))
16450 +               err = e;
16451 +       if (!err)
16452 +               au_refresh_hinode_attr(inode, update && isdir);
16453 +
16454 +out:
16455 +       AuTraceErr(err);
16456 +       return err;
16457 +}
16458 +
16459 +static int set_inode(struct inode *inode, struct dentry *dentry)
16460 +{
16461 +       int err;
16462 +       unsigned int flags;
16463 +       umode_t mode;
16464 +       aufs_bindex_t bindex, btop, btail;
16465 +       unsigned char isdir;
16466 +       struct dentry *h_dentry;
16467 +       struct inode *h_inode;
16468 +       struct au_iinfo *iinfo;
16469 +       struct inode_operations *iop;
16470 +
16471 +       IiMustWriteLock(inode);
16472 +
16473 +       err = 0;
16474 +       isdir = 0;
16475 +       iop = au_sbi(inode->i_sb)->si_iop_array;
16476 +       btop = au_dbtop(dentry);
16477 +       h_dentry = au_h_dptr(dentry, btop);
16478 +       h_inode = d_inode(h_dentry);
16479 +       mode = h_inode->i_mode;
16480 +       switch (mode & S_IFMT) {
16481 +       case S_IFREG:
16482 +               btail = au_dbtail(dentry);
16483 +               inode->i_op = iop + AuIop_OTHER;
16484 +               inode->i_fop = &aufs_file_fop;
16485 +               err = au_dy_iaop(inode, btop, h_inode);
16486 +               if (unlikely(err))
16487 +                       goto out;
16488 +               break;
16489 +       case S_IFDIR:
16490 +               isdir = 1;
16491 +               btail = au_dbtaildir(dentry);
16492 +               inode->i_op = iop + AuIop_DIR;
16493 +               inode->i_fop = &aufs_dir_fop;
16494 +               break;
16495 +       case S_IFLNK:
16496 +               btail = au_dbtail(dentry);
16497 +               inode->i_op = iop + AuIop_SYMLINK;
16498 +               break;
16499 +       case S_IFBLK:
16500 +       case S_IFCHR:
16501 +       case S_IFIFO:
16502 +       case S_IFSOCK:
16503 +               btail = au_dbtail(dentry);
16504 +               inode->i_op = iop + AuIop_OTHER;
16505 +               init_special_inode(inode, mode, h_inode->i_rdev);
16506 +               break;
16507 +       default:
16508 +               AuIOErr("Unknown file type 0%o\n", mode);
16509 +               err = -EIO;
16510 +               goto out;
16511 +       }
16512 +
16513 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
16514 +       flags = au_hi_flags(inode, isdir);
16515 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16516 +           && au_ftest_hi(flags, HNOTIFY)
16517 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
16518 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16519 +               au_fclr_hi(flags, HNOTIFY);
16520 +       iinfo = au_ii(inode);
16521 +       iinfo->ii_btop = btop;
16522 +       iinfo->ii_bbot = btail;
16523 +       for (bindex = btop; bindex <= btail; bindex++) {
16524 +               h_dentry = au_h_dptr(dentry, bindex);
16525 +               if (h_dentry)
16526 +                       au_set_h_iptr(inode, bindex,
16527 +                                     au_igrab(d_inode(h_dentry)), flags);
16528 +       }
16529 +       au_cpup_attr_all(inode, /*force*/1);
16530 +       /*
16531 +        * to force calling aufs_get_acl() every time,
16532 +        * do not call cache_no_acl() for aufs inode.
16533 +        */
16534 +
16535 +out:
16536 +       return err;
16537 +}
16538 +
16539 +/*
16540 + * successful returns with iinfo write_locked
16541 + * minus: errno
16542 + * zero: success, matched
16543 + * plus: no error, but unmatched
16544 + */
16545 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16546 +{
16547 +       int err;
16548 +       unsigned int gen, igflags;
16549 +       aufs_bindex_t bindex, bbot;
16550 +       struct inode *h_inode, *h_dinode;
16551 +       struct dentry *h_dentry;
16552 +
16553 +       /*
16554 +        * before this function, if aufs got any iinfo lock, it must be only
16555 +        * one, the parent dir.
16556 +        * it can happen by UDBA and the obsoleted inode number.
16557 +        */
16558 +       err = -EIO;
16559 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16560 +               goto out;
16561 +
16562 +       err = 1;
16563 +       ii_write_lock_new_child(inode);
16564 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
16565 +       h_dinode = d_inode(h_dentry);
16566 +       bbot = au_ibbot(inode);
16567 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
16568 +               h_inode = au_h_iptr(inode, bindex);
16569 +               if (!h_inode || h_inode != h_dinode)
16570 +                       continue;
16571 +
16572 +               err = 0;
16573 +               gen = au_iigen(inode, &igflags);
16574 +               if (gen == au_digen(dentry)
16575 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
16576 +                       break;
16577 +
16578 +               /* fully refresh inode using dentry */
16579 +               err = au_refresh_hinode(inode, dentry);
16580 +               if (!err)
16581 +                       au_update_iigen(inode, /*half*/0);
16582 +               break;
16583 +       }
16584 +
16585 +       if (unlikely(err))
16586 +               ii_write_unlock(inode);
16587 +out:
16588 +       return err;
16589 +}
16590 +
16591 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16592 +          unsigned int d_type, ino_t *ino)
16593 +{
16594 +       int err, idx;
16595 +       const int isnondir = d_type != DT_DIR;
16596 +
16597 +       /* prevent hardlinked inode number from race condition */
16598 +       if (isnondir) {
16599 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
16600 +               if (unlikely(err))
16601 +                       goto out;
16602 +       }
16603 +
16604 +       err = au_xino_read(sb, bindex, h_ino, ino);
16605 +       if (unlikely(err))
16606 +               goto out_xinondir;
16607 +
16608 +       if (!*ino) {
16609 +               err = -EIO;
16610 +               *ino = au_xino_new_ino(sb);
16611 +               if (unlikely(!*ino))
16612 +                       goto out_xinondir;
16613 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16614 +               if (unlikely(err))
16615 +                       goto out_xinondir;
16616 +       }
16617 +
16618 +out_xinondir:
16619 +       if (isnondir && idx >= 0)
16620 +               au_xinondir_leave(sb, bindex, h_ino, idx);
16621 +out:
16622 +       return err;
16623 +}
16624 +
16625 +/* successful returns with iinfo write_locked */
16626 +/* todo: return with unlocked? */
16627 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16628 +{
16629 +       struct inode *inode, *h_inode;
16630 +       struct dentry *h_dentry;
16631 +       struct super_block *sb;
16632 +       ino_t h_ino, ino;
16633 +       int err, idx, hlinked;
16634 +       aufs_bindex_t btop;
16635 +
16636 +       sb = dentry->d_sb;
16637 +       btop = au_dbtop(dentry);
16638 +       h_dentry = au_h_dptr(dentry, btop);
16639 +       h_inode = d_inode(h_dentry);
16640 +       h_ino = h_inode->i_ino;
16641 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
16642 +
16643 +new_ino:
16644 +       /*
16645 +        * stop 'race'-ing between hardlinks under different
16646 +        * parents.
16647 +        */
16648 +       if (hlinked) {
16649 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
16650 +               inode = ERR_PTR(err);
16651 +               if (unlikely(err))
16652 +                       goto out;
16653 +       }
16654 +
16655 +       err = au_xino_read(sb, btop, h_ino, &ino);
16656 +       inode = ERR_PTR(err);
16657 +       if (unlikely(err))
16658 +               goto out_xinondir;
16659 +
16660 +       if (!ino) {
16661 +               ino = au_xino_new_ino(sb);
16662 +               if (unlikely(!ino)) {
16663 +                       inode = ERR_PTR(-EIO);
16664 +                       goto out_xinondir;
16665 +               }
16666 +       }
16667 +
16668 +       AuDbg("i%lu\n", (unsigned long)ino);
16669 +       inode = au_iget_locked(sb, ino);
16670 +       err = PTR_ERR(inode);
16671 +       if (IS_ERR(inode))
16672 +               goto out_xinondir;
16673 +
16674 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16675 +       if (inode->i_state & I_NEW) {
16676 +               ii_write_lock_new_child(inode);
16677 +               err = set_inode(inode, dentry);
16678 +               if (!err) {
16679 +                       unlock_new_inode(inode);
16680 +                       goto out_xinondir; /* success */
16681 +               }
16682 +
16683 +               /*
16684 +                * iget_failed() calls iput(), but we need to call
16685 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16686 +                * i_count.
16687 +                */
16688 +               atomic_inc(&inode->i_count);
16689 +               iget_failed(inode);
16690 +               ii_write_unlock(inode);
16691 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
16692 +               /* ignore this error */
16693 +               goto out_iput;
16694 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16695 +               /*
16696 +                * horrible race condition between lookup, readdir and copyup
16697 +                * (or something).
16698 +                */
16699 +               if (hlinked && idx >= 0)
16700 +                       au_xinondir_leave(sb, btop, h_ino, idx);
16701 +               err = reval_inode(inode, dentry);
16702 +               if (unlikely(err < 0)) {
16703 +                       hlinked = 0;
16704 +                       goto out_iput;
16705 +               }
16706 +               if (!err)
16707 +                       goto out; /* success */
16708 +               else if (hlinked && idx >= 0) {
16709 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
16710 +                       if (unlikely(err)) {
16711 +                               iput(inode);
16712 +                               inode = ERR_PTR(err);
16713 +                               goto out;
16714 +                       }
16715 +               }
16716 +       }
16717 +
16718 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
16719 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16720 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16721 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
16722 +                       (unsigned long)h_ino, (unsigned long)ino);
16723 +       ino = 0;
16724 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
16725 +       if (!err) {
16726 +               iput(inode);
16727 +               if (hlinked && idx >= 0)
16728 +                       au_xinondir_leave(sb, btop, h_ino, idx);
16729 +               goto new_ino;
16730 +       }
16731 +
16732 +out_iput:
16733 +       iput(inode);
16734 +       inode = ERR_PTR(err);
16735 +out_xinondir:
16736 +       if (hlinked && idx >= 0)
16737 +               au_xinondir_leave(sb, btop, h_ino, idx);
16738 +out:
16739 +       return inode;
16740 +}
16741 +
16742 +/* ---------------------------------------------------------------------- */
16743 +
16744 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16745 +              struct inode *inode)
16746 +{
16747 +       int err;
16748 +       struct inode *hi;
16749 +
16750 +       err = au_br_rdonly(au_sbr(sb, bindex));
16751 +
16752 +       /* pseudo-link after flushed may happen out of bounds */
16753 +       if (!err
16754 +           && inode
16755 +           && au_ibtop(inode) <= bindex
16756 +           && bindex <= au_ibbot(inode)) {
16757 +               /*
16758 +                * permission check is unnecessary since vfsub routine
16759 +                * will be called later
16760 +                */
16761 +               hi = au_h_iptr(inode, bindex);
16762 +               if (hi)
16763 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16764 +       }
16765 +
16766 +       return err;
16767 +}
16768 +
16769 +int au_test_h_perm(struct inode *h_inode, int mask)
16770 +{
16771 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16772 +               return 0;
16773 +       return inode_permission(h_inode, mask);
16774 +}
16775 +
16776 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16777 +{
16778 +       if (au_test_nfs(h_inode->i_sb)
16779 +           && (mask & MAY_WRITE)
16780 +           && S_ISDIR(h_inode->i_mode))
16781 +               mask |= MAY_READ; /* force permission check */
16782 +       return au_test_h_perm(h_inode, mask);
16783 +}
16784 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16785 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16786 +++ linux/fs/aufs/inode.h       2017-07-29 12:14:25.903042072 +0200
16787 @@ -0,0 +1,686 @@
16788 +/*
16789 + * Copyright (C) 2005-2017 Junjiro R. Okajima
16790 + *
16791 + * This program, aufs is free software; you can redistribute it and/or modify
16792 + * it under the terms of the GNU General Public License as published by
16793 + * the Free Software Foundation; either version 2 of the License, or
16794 + * (at your option) any later version.
16795 + *
16796 + * This program is distributed in the hope that it will be useful,
16797 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16798 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16799 + * GNU General Public License for more details.
16800 + *
16801 + * You should have received a copy of the GNU General Public License
16802 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16803 + */
16804 +
16805 +/*
16806 + * inode operations
16807 + */
16808 +
16809 +#ifndef __AUFS_INODE_H__
16810 +#define __AUFS_INODE_H__
16811 +
16812 +#ifdef __KERNEL__
16813 +
16814 +#include <linux/fsnotify.h>
16815 +#include "rwsem.h"
16816 +
16817 +struct vfsmount;
16818 +
16819 +struct au_hnotify {
16820 +#ifdef CONFIG_AUFS_HNOTIFY
16821 +#ifdef CONFIG_AUFS_HFSNOTIFY
16822 +       /* never use fsnotify_add_vfsmount_mark() */
16823 +       struct fsnotify_mark            hn_mark;
16824 +#endif
16825 +       struct inode            *hn_aufs_inode; /* no get/put */
16826 +#endif
16827 +} ____cacheline_aligned_in_smp;
16828 +
16829 +struct au_hinode {
16830 +       struct inode            *hi_inode;
16831 +       aufs_bindex_t           hi_id;
16832 +#ifdef CONFIG_AUFS_HNOTIFY
16833 +       struct au_hnotify       *hi_notify;
16834 +#endif
16835 +
16836 +       /* reference to the copied-up whiteout with get/put */
16837 +       struct dentry           *hi_whdentry;
16838 +};
16839 +
16840 +/* ig_flags */
16841 +#define AuIG_HALF_REFRESHED            1
16842 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16843 +#define au_ig_fset(flags, name) \
16844 +       do { (flags) |= AuIG_##name; } while (0)
16845 +#define au_ig_fclr(flags, name) \
16846 +       do { (flags) &= ~AuIG_##name; } while (0)
16847 +
16848 +struct au_iigen {
16849 +       spinlock_t      ig_spin;
16850 +       __u32           ig_generation, ig_flags;
16851 +};
16852 +
16853 +struct au_vdir;
16854 +struct au_iinfo {
16855 +       struct au_iigen         ii_generation;
16856 +       struct super_block      *ii_hsb1;       /* no get/put */
16857 +
16858 +       struct au_rwsem         ii_rwsem;
16859 +       aufs_bindex_t           ii_btop, ii_bbot;
16860 +       __u32                   ii_higen;
16861 +       struct au_hinode        *ii_hinode;
16862 +       struct au_vdir          *ii_vdir;
16863 +};
16864 +
16865 +struct au_icntnr {
16866 +       struct au_iinfo iinfo;
16867 +       struct inode vfs_inode;
16868 +       struct hlist_node plink;
16869 +} ____cacheline_aligned_in_smp;
16870 +
16871 +/* au_pin flags */
16872 +#define AuPin_DI_LOCKED                1
16873 +#define AuPin_MNT_WRITE                (1 << 1)
16874 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16875 +#define au_fset_pin(flags, name) \
16876 +       do { (flags) |= AuPin_##name; } while (0)
16877 +#define au_fclr_pin(flags, name) \
16878 +       do { (flags) &= ~AuPin_##name; } while (0)
16879 +
16880 +struct au_pin {
16881 +       /* input */
16882 +       struct dentry *dentry;
16883 +       unsigned int udba;
16884 +       unsigned char lsc_di, lsc_hi, flags;
16885 +       aufs_bindex_t bindex;
16886 +
16887 +       /* output */
16888 +       struct dentry *parent;
16889 +       struct au_hinode *hdir;
16890 +       struct vfsmount *h_mnt;
16891 +
16892 +       /* temporary unlock/relock for copyup */
16893 +       struct dentry *h_dentry, *h_parent;
16894 +       struct au_branch *br;
16895 +       struct task_struct *task;
16896 +};
16897 +
16898 +void au_pin_hdir_unlock(struct au_pin *p);
16899 +int au_pin_hdir_lock(struct au_pin *p);
16900 +int au_pin_hdir_relock(struct au_pin *p);
16901 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16902 +void au_pin_hdir_release(struct au_pin *p);
16903 +
16904 +/* ---------------------------------------------------------------------- */
16905 +
16906 +static inline struct au_iinfo *au_ii(struct inode *inode)
16907 +{
16908 +       BUG_ON(is_bad_inode(inode));
16909 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16910 +}
16911 +
16912 +/* ---------------------------------------------------------------------- */
16913 +
16914 +/* inode.c */
16915 +struct inode *au_igrab(struct inode *inode);
16916 +void au_refresh_iop(struct inode *inode, int force_getattr);
16917 +int au_refresh_hinode_self(struct inode *inode);
16918 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16919 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16920 +          unsigned int d_type, ino_t *ino);
16921 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16922 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16923 +              struct inode *inode);
16924 +int au_test_h_perm(struct inode *h_inode, int mask);
16925 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16926 +
16927 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16928 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16929 +{
16930 +#ifdef CONFIG_AUFS_SHWH
16931 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16932 +#else
16933 +       return 0;
16934 +#endif
16935 +}
16936 +
16937 +/* i_op.c */
16938 +enum {
16939 +       AuIop_SYMLINK,
16940 +       AuIop_DIR,
16941 +       AuIop_OTHER,
16942 +       AuIop_Last
16943 +};
16944 +extern struct inode_operations aufs_iop[AuIop_Last],
16945 +       aufs_iop_nogetattr[AuIop_Last];
16946 +
16947 +/* au_wr_dir flags */
16948 +#define AuWrDir_ADD_ENTRY      1
16949 +#define AuWrDir_ISDIR          (1 << 1)
16950 +#define AuWrDir_TMPFILE                (1 << 2)
16951 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16952 +#define au_fset_wrdir(flags, name) \
16953 +       do { (flags) |= AuWrDir_##name; } while (0)
16954 +#define au_fclr_wrdir(flags, name) \
16955 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16956 +
16957 +struct au_wr_dir_args {
16958 +       aufs_bindex_t force_btgt;
16959 +       unsigned char flags;
16960 +};
16961 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16962 +             struct au_wr_dir_args *args);
16963 +
16964 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16965 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16966 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16967 +                unsigned int udba, unsigned char flags);
16968 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16969 +          unsigned int udba, unsigned char flags) __must_check;
16970 +int au_do_pin(struct au_pin *pin) __must_check;
16971 +void au_unpin(struct au_pin *pin);
16972 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16973 +
16974 +#define AuIcpup_DID_CPUP       1
16975 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16976 +#define au_fset_icpup(flags, name) \
16977 +       do { (flags) |= AuIcpup_##name; } while (0)
16978 +#define au_fclr_icpup(flags, name) \
16979 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16980 +
16981 +struct au_icpup_args {
16982 +       unsigned char flags;
16983 +       unsigned char pin_flags;
16984 +       aufs_bindex_t btgt;
16985 +       unsigned int udba;
16986 +       struct au_pin pin;
16987 +       struct path h_path;
16988 +       struct inode *h_inode;
16989 +};
16990 +
16991 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16992 +                    struct au_icpup_args *a);
16993 +
16994 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
16995 +                     int locked);
16996 +
16997 +/* i_op_add.c */
16998 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16999 +              struct dentry *h_parent, int isdir);
17000 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17001 +              dev_t dev);
17002 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
17003 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17004 +               bool want_excl);
17005 +struct vfsub_aopen_args;
17006 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17007 +                      struct vfsub_aopen_args *args);
17008 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
17009 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
17010 +             struct dentry *dentry);
17011 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
17012 +
17013 +/* i_op_del.c */
17014 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
17015 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
17016 +              struct dentry *h_parent, int isdir);
17017 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
17018 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
17019 +
17020 +/* i_op_ren.c */
17021 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
17022 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
17023 +               struct inode *dir, struct dentry *dentry,
17024 +               unsigned int flags);
17025 +
17026 +/* iinfo.c */
17027 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
17028 +void au_hiput(struct au_hinode *hinode);
17029 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17030 +                 struct dentry *h_wh);
17031 +unsigned int au_hi_flags(struct inode *inode, int isdir);
17032 +
17033 +/* hinode flags */
17034 +#define AuHi_XINO      1
17035 +#define AuHi_HNOTIFY   (1 << 1)
17036 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
17037 +#define au_fset_hi(flags, name) \
17038 +       do { (flags) |= AuHi_##name; } while (0)
17039 +#define au_fclr_hi(flags, name) \
17040 +       do { (flags) &= ~AuHi_##name; } while (0)
17041 +
17042 +#ifndef CONFIG_AUFS_HNOTIFY
17043 +#undef AuHi_HNOTIFY
17044 +#define AuHi_HNOTIFY   0
17045 +#endif
17046 +
17047 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17048 +                  struct inode *h_inode, unsigned int flags);
17049 +
17050 +void au_update_iigen(struct inode *inode, int half);
17051 +void au_update_ibrange(struct inode *inode, int do_put_zero);
17052 +
17053 +void au_icntnr_init_once(void *_c);
17054 +void au_hinode_init(struct au_hinode *hinode);
17055 +int au_iinfo_init(struct inode *inode);
17056 +void au_iinfo_fin(struct inode *inode);
17057 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
17058 +
17059 +#ifdef CONFIG_PROC_FS
17060 +/* plink.c */
17061 +int au_plink_maint(struct super_block *sb, int flags);
17062 +struct au_sbinfo;
17063 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
17064 +int au_plink_maint_enter(struct super_block *sb);
17065 +#ifdef CONFIG_AUFS_DEBUG
17066 +void au_plink_list(struct super_block *sb);
17067 +#else
17068 +AuStubVoid(au_plink_list, struct super_block *sb)
17069 +#endif
17070 +int au_plink_test(struct inode *inode);
17071 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
17072 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
17073 +                    struct dentry *h_dentry);
17074 +void au_plink_put(struct super_block *sb, int verbose);
17075 +void au_plink_clean(struct super_block *sb, int verbose);
17076 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
17077 +#else
17078 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
17079 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
17080 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
17081 +AuStubVoid(au_plink_list, struct super_block *sb);
17082 +AuStubInt0(au_plink_test, struct inode *inode);
17083 +AuStub(struct dentry *, au_plink_lkup, return NULL,
17084 +       struct inode *inode, aufs_bindex_t bindex);
17085 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
17086 +          struct dentry *h_dentry);
17087 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
17088 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
17089 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
17090 +#endif /* CONFIG_PROC_FS */
17091 +
17092 +#ifdef CONFIG_AUFS_XATTR
17093 +/* xattr.c */
17094 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
17095 +                 unsigned int verbose);
17096 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
17097 +void au_xattr_init(struct super_block *sb);
17098 +#else
17099 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
17100 +          int ignore_flags, unsigned int verbose);
17101 +AuStubVoid(au_xattr_init, struct super_block *sb);
17102 +#endif
17103 +
17104 +#ifdef CONFIG_FS_POSIX_ACL
17105 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
17106 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
17107 +#endif
17108 +
17109 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
17110 +enum {
17111 +       AU_XATTR_SET,
17112 +       AU_ACL_SET
17113 +};
17114 +
17115 +struct au_sxattr {
17116 +       int type;
17117 +       union {
17118 +               struct {
17119 +                       const char      *name;
17120 +                       const void      *value;
17121 +                       size_t          size;
17122 +                       int             flags;
17123 +               } set;
17124 +               struct {
17125 +                       struct posix_acl *acl;
17126 +                       int             type;
17127 +               } acl_set;
17128 +       } u;
17129 +};
17130 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
17131 +                 struct au_sxattr *arg);
17132 +#endif
17133 +
17134 +/* ---------------------------------------------------------------------- */
17135 +
17136 +/* lock subclass for iinfo */
17137 +enum {
17138 +       AuLsc_II_CHILD,         /* child first */
17139 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
17140 +       AuLsc_II_CHILD3,        /* copyup dirs */
17141 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
17142 +       AuLsc_II_PARENT2,
17143 +       AuLsc_II_PARENT3,       /* copyup dirs */
17144 +       AuLsc_II_NEW_CHILD
17145 +};
17146 +
17147 +/*
17148 + * ii_read_lock_child, ii_write_lock_child,
17149 + * ii_read_lock_child2, ii_write_lock_child2,
17150 + * ii_read_lock_child3, ii_write_lock_child3,
17151 + * ii_read_lock_parent, ii_write_lock_parent,
17152 + * ii_read_lock_parent2, ii_write_lock_parent2,
17153 + * ii_read_lock_parent3, ii_write_lock_parent3,
17154 + * ii_read_lock_new_child, ii_write_lock_new_child,
17155 + */
17156 +#define AuReadLockFunc(name, lsc) \
17157 +static inline void ii_read_lock_##name(struct inode *i) \
17158 +{ \
17159 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
17160 +}
17161 +
17162 +#define AuWriteLockFunc(name, lsc) \
17163 +static inline void ii_write_lock_##name(struct inode *i) \
17164 +{ \
17165 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
17166 +}
17167 +
17168 +#define AuRWLockFuncs(name, lsc) \
17169 +       AuReadLockFunc(name, lsc) \
17170 +       AuWriteLockFunc(name, lsc)
17171 +
17172 +AuRWLockFuncs(child, CHILD);
17173 +AuRWLockFuncs(child2, CHILD2);
17174 +AuRWLockFuncs(child3, CHILD3);
17175 +AuRWLockFuncs(parent, PARENT);
17176 +AuRWLockFuncs(parent2, PARENT2);
17177 +AuRWLockFuncs(parent3, PARENT3);
17178 +AuRWLockFuncs(new_child, NEW_CHILD);
17179 +
17180 +#undef AuReadLockFunc
17181 +#undef AuWriteLockFunc
17182 +#undef AuRWLockFuncs
17183 +
17184 +/*
17185 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
17186 + */
17187 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
17188 +
17189 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
17190 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
17191 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
17192 +
17193 +/* ---------------------------------------------------------------------- */
17194 +
17195 +static inline void au_icntnr_init(struct au_icntnr *c)
17196 +{
17197 +#ifdef CONFIG_AUFS_DEBUG
17198 +       c->vfs_inode.i_mode = 0;
17199 +#endif
17200 +}
17201 +
17202 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
17203 +{
17204 +       unsigned int gen;
17205 +       struct au_iinfo *iinfo;
17206 +       struct au_iigen *iigen;
17207 +
17208 +       iinfo = au_ii(inode);
17209 +       iigen = &iinfo->ii_generation;
17210 +       spin_lock(&iigen->ig_spin);
17211 +       if (igflags)
17212 +               *igflags = iigen->ig_flags;
17213 +       gen = iigen->ig_generation;
17214 +       spin_unlock(&iigen->ig_spin);
17215 +
17216 +       return gen;
17217 +}
17218 +
17219 +/* tiny test for inode number */
17220 +/* tmpfs generation is too rough */
17221 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
17222 +{
17223 +       struct au_iinfo *iinfo;
17224 +
17225 +       iinfo = au_ii(inode);
17226 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
17227 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
17228 +                && iinfo->ii_higen == h_inode->i_generation);
17229 +}
17230 +
17231 +static inline void au_iigen_dec(struct inode *inode)
17232 +{
17233 +       struct au_iinfo *iinfo;
17234 +       struct au_iigen *iigen;
17235 +
17236 +       iinfo = au_ii(inode);
17237 +       iigen = &iinfo->ii_generation;
17238 +       spin_lock(&iigen->ig_spin);
17239 +       iigen->ig_generation--;
17240 +       spin_unlock(&iigen->ig_spin);
17241 +}
17242 +
17243 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
17244 +{
17245 +       int err;
17246 +
17247 +       err = 0;
17248 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
17249 +               err = -EIO;
17250 +
17251 +       return err;
17252 +}
17253 +
17254 +/* ---------------------------------------------------------------------- */
17255 +
17256 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
17257 +                                         aufs_bindex_t bindex)
17258 +{
17259 +       return iinfo->ii_hinode + bindex;
17260 +}
17261 +
17262 +static inline int au_is_bad_inode(struct inode *inode)
17263 +{
17264 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
17265 +}
17266 +
17267 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
17268 +                                       aufs_bindex_t bindex)
17269 +{
17270 +       IiMustAnyLock(inode);
17271 +       return au_hinode(au_ii(inode), bindex)->hi_id;
17272 +}
17273 +
17274 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
17275 +{
17276 +       IiMustAnyLock(inode);
17277 +       return au_ii(inode)->ii_btop;
17278 +}
17279 +
17280 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
17281 +{
17282 +       IiMustAnyLock(inode);
17283 +       return au_ii(inode)->ii_bbot;
17284 +}
17285 +
17286 +static inline struct au_vdir *au_ivdir(struct inode *inode)
17287 +{
17288 +       IiMustAnyLock(inode);
17289 +       return au_ii(inode)->ii_vdir;
17290 +}
17291 +
17292 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
17293 +{
17294 +       IiMustAnyLock(inode);
17295 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
17296 +}
17297 +
17298 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
17299 +{
17300 +       IiMustWriteLock(inode);
17301 +       au_ii(inode)->ii_btop = bindex;
17302 +}
17303 +
17304 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
17305 +{
17306 +       IiMustWriteLock(inode);
17307 +       au_ii(inode)->ii_bbot = bindex;
17308 +}
17309 +
17310 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
17311 +{
17312 +       IiMustWriteLock(inode);
17313 +       au_ii(inode)->ii_vdir = vdir;
17314 +}
17315 +
17316 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
17317 +{
17318 +       IiMustAnyLock(inode);
17319 +       return au_hinode(au_ii(inode), bindex);
17320 +}
17321 +
17322 +/* ---------------------------------------------------------------------- */
17323 +
17324 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
17325 +{
17326 +       if (pin)
17327 +               return pin->parent;
17328 +       return NULL;
17329 +}
17330 +
17331 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
17332 +{
17333 +       if (pin && pin->hdir)
17334 +               return pin->hdir->hi_inode;
17335 +       return NULL;
17336 +}
17337 +
17338 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
17339 +{
17340 +       if (pin)
17341 +               return pin->hdir;
17342 +       return NULL;
17343 +}
17344 +
17345 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
17346 +{
17347 +       if (pin)
17348 +               pin->dentry = dentry;
17349 +}
17350 +
17351 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
17352 +                                          unsigned char lflag)
17353 +{
17354 +       if (pin) {
17355 +               if (lflag)
17356 +                       au_fset_pin(pin->flags, DI_LOCKED);
17357 +               else
17358 +                       au_fclr_pin(pin->flags, DI_LOCKED);
17359 +       }
17360 +}
17361 +
17362 +#if 0 /* reserved */
17363 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
17364 +{
17365 +       if (pin) {
17366 +               dput(pin->parent);
17367 +               pin->parent = dget(parent);
17368 +       }
17369 +}
17370 +#endif
17371 +
17372 +/* ---------------------------------------------------------------------- */
17373 +
17374 +struct au_branch;
17375 +#ifdef CONFIG_AUFS_HNOTIFY
17376 +struct au_hnotify_op {
17377 +       void (*ctl)(struct au_hinode *hinode, int do_set);
17378 +       int (*alloc)(struct au_hinode *hinode);
17379 +
17380 +       /*
17381 +        * if it returns true, the the caller should free hinode->hi_notify,
17382 +        * otherwise ->free() frees it.
17383 +        */
17384 +       int (*free)(struct au_hinode *hinode,
17385 +                   struct au_hnotify *hn) __must_check;
17386 +
17387 +       void (*fin)(void);
17388 +       int (*init)(void);
17389 +
17390 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
17391 +       void (*fin_br)(struct au_branch *br);
17392 +       int (*init_br)(struct au_branch *br, int perm);
17393 +};
17394 +
17395 +/* hnotify.c */
17396 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
17397 +void au_hn_free(struct au_hinode *hinode);
17398 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
17399 +void au_hn_reset(struct inode *inode, unsigned int flags);
17400 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17401 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
17402 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
17403 +int au_hnotify_init_br(struct au_branch *br, int perm);
17404 +void au_hnotify_fin_br(struct au_branch *br);
17405 +int __init au_hnotify_init(void);
17406 +void au_hnotify_fin(void);
17407 +
17408 +/* hfsnotify.c */
17409 +extern const struct au_hnotify_op au_hnotify_op;
17410 +
17411 +static inline
17412 +void au_hn_init(struct au_hinode *hinode)
17413 +{
17414 +       hinode->hi_notify = NULL;
17415 +}
17416 +
17417 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
17418 +{
17419 +       return hinode->hi_notify;
17420 +}
17421 +
17422 +#else
17423 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
17424 +       struct au_hinode *hinode __maybe_unused,
17425 +       struct inode *inode __maybe_unused)
17426 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
17427 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
17428 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
17429 +          int do_set __maybe_unused)
17430 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
17431 +          unsigned int flags __maybe_unused)
17432 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
17433 +          struct au_branch *br __maybe_unused,
17434 +          int perm __maybe_unused)
17435 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
17436 +          int perm __maybe_unused)
17437 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
17438 +AuStubInt0(__init au_hnotify_init, void)
17439 +AuStubVoid(au_hnotify_fin, void)
17440 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
17441 +#endif /* CONFIG_AUFS_HNOTIFY */
17442 +
17443 +static inline void au_hn_suspend(struct au_hinode *hdir)
17444 +{
17445 +       au_hn_ctl(hdir, /*do_set*/0);
17446 +}
17447 +
17448 +static inline void au_hn_resume(struct au_hinode *hdir)
17449 +{
17450 +       au_hn_ctl(hdir, /*do_set*/1);
17451 +}
17452 +
17453 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
17454 +{
17455 +       inode_lock(hdir->hi_inode);
17456 +       au_hn_suspend(hdir);
17457 +}
17458 +
17459 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
17460 +                                         unsigned int sc __maybe_unused)
17461 +{
17462 +       inode_lock_nested(hdir->hi_inode, sc);
17463 +       au_hn_suspend(hdir);
17464 +}
17465 +
17466 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
17467 +{
17468 +       au_hn_resume(hdir);
17469 +       inode_unlock(hdir->hi_inode);
17470 +}
17471 +
17472 +#endif /* __KERNEL__ */
17473 +#endif /* __AUFS_INODE_H__ */
17474 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
17475 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
17476 +++ linux/fs/aufs/ioctl.c       2017-07-29 12:14:25.903042072 +0200
17477 @@ -0,0 +1,219 @@
17478 +/*
17479 + * Copyright (C) 2005-2017 Junjiro R. Okajima
17480 + *
17481 + * This program, aufs is free software; you can redistribute it and/or modify
17482 + * it under the terms of the GNU General Public License as published by
17483 + * the Free Software Foundation; either version 2 of the License, or
17484 + * (at your option) any later version.
17485 + *
17486 + * This program is distributed in the hope that it will be useful,
17487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17489 + * GNU General Public License for more details.
17490 + *
17491 + * You should have received a copy of the GNU General Public License
17492 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17493 + */
17494 +
17495 +/*
17496 + * ioctl
17497 + * plink-management and readdir in userspace.
17498 + * assist the pathconf(3) wrapper library.
17499 + * move-down
17500 + * File-based Hierarchical Storage Management.
17501 + */
17502 +
17503 +#include <linux/compat.h>
17504 +#include <linux/file.h>
17505 +#include "aufs.h"
17506 +
17507 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
17508 +{
17509 +       int err, fd;
17510 +       aufs_bindex_t wbi, bindex, bbot;
17511 +       struct file *h_file;
17512 +       struct super_block *sb;
17513 +       struct dentry *root;
17514 +       struct au_branch *br;
17515 +       struct aufs_wbr_fd wbrfd = {
17516 +               .oflags = au_dir_roflags,
17517 +               .brid   = -1
17518 +       };
17519 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17520 +               | O_NOATIME | O_CLOEXEC;
17521 +
17522 +       AuDebugOn(wbrfd.oflags & ~valid);
17523 +
17524 +       if (arg) {
17525 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17526 +               if (unlikely(err)) {
17527 +                       err = -EFAULT;
17528 +                       goto out;
17529 +               }
17530 +
17531 +               err = -EINVAL;
17532 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17533 +               wbrfd.oflags |= au_dir_roflags;
17534 +               AuDbg("0%o\n", wbrfd.oflags);
17535 +               if (unlikely(wbrfd.oflags & ~valid))
17536 +                       goto out;
17537 +       }
17538 +
17539 +       fd = get_unused_fd_flags(0);
17540 +       err = fd;
17541 +       if (unlikely(fd < 0))
17542 +               goto out;
17543 +
17544 +       h_file = ERR_PTR(-EINVAL);
17545 +       wbi = 0;
17546 +       br = NULL;
17547 +       sb = path->dentry->d_sb;
17548 +       root = sb->s_root;
17549 +       aufs_read_lock(root, AuLock_IR);
17550 +       bbot = au_sbbot(sb);
17551 +       if (wbrfd.brid >= 0) {
17552 +               wbi = au_br_index(sb, wbrfd.brid);
17553 +               if (unlikely(wbi < 0 || wbi > bbot))
17554 +                       goto out_unlock;
17555 +       }
17556 +
17557 +       h_file = ERR_PTR(-ENOENT);
17558 +       br = au_sbr(sb, wbi);
17559 +       if (!au_br_writable(br->br_perm)) {
17560 +               if (arg)
17561 +                       goto out_unlock;
17562 +
17563 +               bindex = wbi + 1;
17564 +               wbi = -1;
17565 +               for (; bindex <= bbot; bindex++) {
17566 +                       br = au_sbr(sb, bindex);
17567 +                       if (au_br_writable(br->br_perm)) {
17568 +                               wbi = bindex;
17569 +                               br = au_sbr(sb, wbi);
17570 +                               break;
17571 +                       }
17572 +               }
17573 +       }
17574 +       AuDbg("wbi %d\n", wbi);
17575 +       if (wbi >= 0)
17576 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17577 +                                  /*force_wr*/0);
17578 +
17579 +out_unlock:
17580 +       aufs_read_unlock(root, AuLock_IR);
17581 +       err = PTR_ERR(h_file);
17582 +       if (IS_ERR(h_file))
17583 +               goto out_fd;
17584 +
17585 +       au_br_put(br); /* cf. au_h_open() */
17586 +       fd_install(fd, h_file);
17587 +       err = fd;
17588 +       goto out; /* success */
17589 +
17590 +out_fd:
17591 +       put_unused_fd(fd);
17592 +out:
17593 +       AuTraceErr(err);
17594 +       return err;
17595 +}
17596 +
17597 +/* ---------------------------------------------------------------------- */
17598 +
17599 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17600 +{
17601 +       long err;
17602 +       struct dentry *dentry;
17603 +
17604 +       switch (cmd) {
17605 +       case AUFS_CTL_RDU:
17606 +       case AUFS_CTL_RDU_INO:
17607 +               err = au_rdu_ioctl(file, cmd, arg);
17608 +               break;
17609 +
17610 +       case AUFS_CTL_WBR_FD:
17611 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17612 +               break;
17613 +
17614 +       case AUFS_CTL_IBUSY:
17615 +               err = au_ibusy_ioctl(file, arg);
17616 +               break;
17617 +
17618 +       case AUFS_CTL_BRINFO:
17619 +               err = au_brinfo_ioctl(file, arg);
17620 +               break;
17621 +
17622 +       case AUFS_CTL_FHSM_FD:
17623 +               dentry = file->f_path.dentry;
17624 +               if (IS_ROOT(dentry))
17625 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17626 +               else
17627 +                       err = -ENOTTY;
17628 +               break;
17629 +
17630 +       default:
17631 +               /* do not call the lower */
17632 +               AuDbg("0x%x\n", cmd);
17633 +               err = -ENOTTY;
17634 +       }
17635 +
17636 +       AuTraceErr(err);
17637 +       return err;
17638 +}
17639 +
17640 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17641 +{
17642 +       long err;
17643 +
17644 +       switch (cmd) {
17645 +       case AUFS_CTL_MVDOWN:
17646 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17647 +               break;
17648 +
17649 +       case AUFS_CTL_WBR_FD:
17650 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17651 +               break;
17652 +
17653 +       default:
17654 +               /* do not call the lower */
17655 +               AuDbg("0x%x\n", cmd);
17656 +               err = -ENOTTY;
17657 +       }
17658 +
17659 +       AuTraceErr(err);
17660 +       return err;
17661 +}
17662 +
17663 +#ifdef CONFIG_COMPAT
17664 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17665 +                          unsigned long arg)
17666 +{
17667 +       long err;
17668 +
17669 +       switch (cmd) {
17670 +       case AUFS_CTL_RDU:
17671 +       case AUFS_CTL_RDU_INO:
17672 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17673 +               break;
17674 +
17675 +       case AUFS_CTL_IBUSY:
17676 +               err = au_ibusy_compat_ioctl(file, arg);
17677 +               break;
17678 +
17679 +       case AUFS_CTL_BRINFO:
17680 +               err = au_brinfo_compat_ioctl(file, arg);
17681 +               break;
17682 +
17683 +       default:
17684 +               err = aufs_ioctl_dir(file, cmd, arg);
17685 +       }
17686 +
17687 +       AuTraceErr(err);
17688 +       return err;
17689 +}
17690 +
17691 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17692 +                             unsigned long arg)
17693 +{
17694 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17695 +}
17696 +#endif
17697 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17698 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17699 +++ linux/fs/aufs/i_op_add.c    2017-07-29 12:14:25.903042072 +0200
17700 @@ -0,0 +1,920 @@
17701 +/*
17702 + * Copyright (C) 2005-2017 Junjiro R. Okajima
17703 + *
17704 + * This program, aufs is free software; you can redistribute it and/or modify
17705 + * it under the terms of the GNU General Public License as published by
17706 + * the Free Software Foundation; either version 2 of the License, or
17707 + * (at your option) any later version.
17708 + *
17709 + * This program is distributed in the hope that it will be useful,
17710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17712 + * GNU General Public License for more details.
17713 + *
17714 + * You should have received a copy of the GNU General Public License
17715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17716 + */
17717 +
17718 +/*
17719 + * inode operations (add entry)
17720 + */
17721 +
17722 +#include "aufs.h"
17723 +
17724 +/*
17725 + * final procedure of adding a new entry, except link(2).
17726 + * remove whiteout, instantiate, copyup the parent dir's times and size
17727 + * and update version.
17728 + * if it failed, re-create the removed whiteout.
17729 + */
17730 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17731 +                 struct dentry *wh_dentry, struct dentry *dentry)
17732 +{
17733 +       int err, rerr;
17734 +       aufs_bindex_t bwh;
17735 +       struct path h_path;
17736 +       struct super_block *sb;
17737 +       struct inode *inode, *h_dir;
17738 +       struct dentry *wh;
17739 +
17740 +       bwh = -1;
17741 +       sb = dir->i_sb;
17742 +       if (wh_dentry) {
17743 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
17744 +               IMustLock(h_dir);
17745 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17746 +               bwh = au_dbwh(dentry);
17747 +               h_path.dentry = wh_dentry;
17748 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17749 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17750 +                                         dentry);
17751 +               if (unlikely(err))
17752 +                       goto out;
17753 +       }
17754 +
17755 +       inode = au_new_inode(dentry, /*must_new*/1);
17756 +       if (!IS_ERR(inode)) {
17757 +               d_instantiate(dentry, inode);
17758 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
17759 +               IMustLock(dir);
17760 +               au_dir_ts(dir, bindex);
17761 +               dir->i_version++;
17762 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17763 +               return 0; /* success */
17764 +       }
17765 +
17766 +       err = PTR_ERR(inode);
17767 +       if (!wh_dentry)
17768 +               goto out;
17769 +
17770 +       /* revert */
17771 +       /* dir inode is locked */
17772 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17773 +       rerr = PTR_ERR(wh);
17774 +       if (IS_ERR(wh)) {
17775 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17776 +                       dentry, err, rerr);
17777 +               err = -EIO;
17778 +       } else
17779 +               dput(wh);
17780 +
17781 +out:
17782 +       return err;
17783 +}
17784 +
17785 +static int au_d_may_add(struct dentry *dentry)
17786 +{
17787 +       int err;
17788 +
17789 +       err = 0;
17790 +       if (unlikely(d_unhashed(dentry)))
17791 +               err = -ENOENT;
17792 +       if (unlikely(d_really_is_positive(dentry)))
17793 +               err = -EEXIST;
17794 +       return err;
17795 +}
17796 +
17797 +/*
17798 + * simple tests for the adding inode operations.
17799 + * following the checks in vfs, plus the parent-child relationship.
17800 + */
17801 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17802 +              struct dentry *h_parent, int isdir)
17803 +{
17804 +       int err;
17805 +       umode_t h_mode;
17806 +       struct dentry *h_dentry;
17807 +       struct inode *h_inode;
17808 +
17809 +       err = -ENAMETOOLONG;
17810 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17811 +               goto out;
17812 +
17813 +       h_dentry = au_h_dptr(dentry, bindex);
17814 +       if (d_really_is_negative(dentry)) {
17815 +               err = -EEXIST;
17816 +               if (unlikely(d_is_positive(h_dentry)))
17817 +                       goto out;
17818 +       } else {
17819 +               /* rename(2) case */
17820 +               err = -EIO;
17821 +               if (unlikely(d_is_negative(h_dentry)))
17822 +                       goto out;
17823 +               h_inode = d_inode(h_dentry);
17824 +               if (unlikely(!h_inode->i_nlink))
17825 +                       goto out;
17826 +
17827 +               h_mode = h_inode->i_mode;
17828 +               if (!isdir) {
17829 +                       err = -EISDIR;
17830 +                       if (unlikely(S_ISDIR(h_mode)))
17831 +                               goto out;
17832 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17833 +                       err = -ENOTDIR;
17834 +                       goto out;
17835 +               }
17836 +       }
17837 +
17838 +       err = 0;
17839 +       /* expected parent dir is locked */
17840 +       if (unlikely(h_parent != h_dentry->d_parent))
17841 +               err = -EIO;
17842 +
17843 +out:
17844 +       AuTraceErr(err);
17845 +       return err;
17846 +}
17847 +
17848 +/*
17849 + * initial procedure of adding a new entry.
17850 + * prepare writable branch and the parent dir, lock it,
17851 + * and lookup whiteout for the new entry.
17852 + */
17853 +static struct dentry*
17854 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17855 +                 struct dentry *src_dentry, struct au_pin *pin,
17856 +                 struct au_wr_dir_args *wr_dir_args)
17857 +{
17858 +       struct dentry *wh_dentry, *h_parent;
17859 +       struct super_block *sb;
17860 +       struct au_branch *br;
17861 +       int err;
17862 +       unsigned int udba;
17863 +       aufs_bindex_t bcpup;
17864 +
17865 +       AuDbg("%pd\n", dentry);
17866 +
17867 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17868 +       bcpup = err;
17869 +       wh_dentry = ERR_PTR(err);
17870 +       if (unlikely(err < 0))
17871 +               goto out;
17872 +
17873 +       sb = dentry->d_sb;
17874 +       udba = au_opt_udba(sb);
17875 +       err = au_pin(pin, dentry, bcpup, udba,
17876 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17877 +       wh_dentry = ERR_PTR(err);
17878 +       if (unlikely(err))
17879 +               goto out;
17880 +
17881 +       h_parent = au_pinned_h_parent(pin);
17882 +       if (udba != AuOpt_UDBA_NONE
17883 +           && au_dbtop(dentry) == bcpup)
17884 +               err = au_may_add(dentry, bcpup, h_parent,
17885 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17886 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17887 +               err = -ENAMETOOLONG;
17888 +       wh_dentry = ERR_PTR(err);
17889 +       if (unlikely(err))
17890 +               goto out_unpin;
17891 +
17892 +       br = au_sbr(sb, bcpup);
17893 +       if (dt) {
17894 +               struct path tmp = {
17895 +                       .dentry = h_parent,
17896 +                       .mnt    = au_br_mnt(br)
17897 +               };
17898 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17899 +       }
17900 +
17901 +       wh_dentry = NULL;
17902 +       if (bcpup != au_dbwh(dentry))
17903 +               goto out; /* success */
17904 +
17905 +       /*
17906 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17907 +        * would not be able to removed in the future. So we don't allow such
17908 +        * name here and we don't handle ENAMETOOLONG differently here.
17909 +        */
17910 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17911 +
17912 +out_unpin:
17913 +       if (IS_ERR(wh_dentry))
17914 +               au_unpin(pin);
17915 +out:
17916 +       return wh_dentry;
17917 +}
17918 +
17919 +/* ---------------------------------------------------------------------- */
17920 +
17921 +enum { Mknod, Symlink, Creat };
17922 +struct simple_arg {
17923 +       int type;
17924 +       union {
17925 +               struct {
17926 +                       umode_t                 mode;
17927 +                       bool                    want_excl;
17928 +                       bool                    try_aopen;
17929 +                       struct vfsub_aopen_args *aopen;
17930 +               } c;
17931 +               struct {
17932 +                       const char *symname;
17933 +               } s;
17934 +               struct {
17935 +                       umode_t mode;
17936 +                       dev_t dev;
17937 +               } m;
17938 +       } u;
17939 +};
17940 +
17941 +static int add_simple(struct inode *dir, struct dentry *dentry,
17942 +                     struct simple_arg *arg)
17943 +{
17944 +       int err, rerr;
17945 +       aufs_bindex_t btop;
17946 +       unsigned char created;
17947 +       const unsigned char try_aopen
17948 +               = (arg->type == Creat && arg->u.c.try_aopen);
17949 +       struct dentry *wh_dentry, *parent;
17950 +       struct inode *h_dir;
17951 +       struct super_block *sb;
17952 +       struct au_branch *br;
17953 +       /* to reuduce stack size */
17954 +       struct {
17955 +               struct au_dtime dt;
17956 +               struct au_pin pin;
17957 +               struct path h_path;
17958 +               struct au_wr_dir_args wr_dir_args;
17959 +       } *a;
17960 +
17961 +       AuDbg("%pd\n", dentry);
17962 +       IMustLock(dir);
17963 +
17964 +       err = -ENOMEM;
17965 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17966 +       if (unlikely(!a))
17967 +               goto out;
17968 +       a->wr_dir_args.force_btgt = -1;
17969 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17970 +
17971 +       parent = dentry->d_parent; /* dir inode is locked */
17972 +       if (!try_aopen) {
17973 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17974 +               if (unlikely(err))
17975 +                       goto out_free;
17976 +       }
17977 +       err = au_d_may_add(dentry);
17978 +       if (unlikely(err))
17979 +               goto out_unlock;
17980 +       if (!try_aopen)
17981 +               di_write_lock_parent(parent);
17982 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17983 +                                     &a->pin, &a->wr_dir_args);
17984 +       err = PTR_ERR(wh_dentry);
17985 +       if (IS_ERR(wh_dentry))
17986 +               goto out_parent;
17987 +
17988 +       btop = au_dbtop(dentry);
17989 +       sb = dentry->d_sb;
17990 +       br = au_sbr(sb, btop);
17991 +       a->h_path.dentry = au_h_dptr(dentry, btop);
17992 +       a->h_path.mnt = au_br_mnt(br);
17993 +       h_dir = au_pinned_h_dir(&a->pin);
17994 +       switch (arg->type) {
17995 +       case Creat:
17996 +               err = 0;
17997 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17998 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17999 +                                          arg->u.c.want_excl);
18000 +               else
18001 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
18002 +                                               arg->u.c.aopen, br);
18003 +               break;
18004 +       case Symlink:
18005 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
18006 +               break;
18007 +       case Mknod:
18008 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
18009 +                                 arg->u.m.dev);
18010 +               break;
18011 +       default:
18012 +               BUG();
18013 +       }
18014 +       created = !err;
18015 +       if (!err)
18016 +               err = epilog(dir, btop, wh_dentry, dentry);
18017 +
18018 +       /* revert */
18019 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
18020 +               /* no delegation since it is just created */
18021 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
18022 +                                   /*force*/0);
18023 +               if (rerr) {
18024 +                       AuIOErr("%pd revert failure(%d, %d)\n",
18025 +                               dentry, err, rerr);
18026 +                       err = -EIO;
18027 +               }
18028 +               au_dtime_revert(&a->dt);
18029 +       }
18030 +
18031 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
18032 +               *arg->u.c.aopen->opened |= FILE_CREATED;
18033 +
18034 +       au_unpin(&a->pin);
18035 +       dput(wh_dentry);
18036 +
18037 +out_parent:
18038 +       if (!try_aopen)
18039 +               di_write_unlock(parent);
18040 +out_unlock:
18041 +       if (unlikely(err)) {
18042 +               au_update_dbtop(dentry);
18043 +               d_drop(dentry);
18044 +       }
18045 +       if (!try_aopen)
18046 +               aufs_read_unlock(dentry, AuLock_DW);
18047 +out_free:
18048 +       kfree(a);
18049 +out:
18050 +       return err;
18051 +}
18052 +
18053 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18054 +              dev_t dev)
18055 +{
18056 +       struct simple_arg arg = {
18057 +               .type = Mknod,
18058 +               .u.m = {
18059 +                       .mode   = mode,
18060 +                       .dev    = dev
18061 +               }
18062 +       };
18063 +       return add_simple(dir, dentry, &arg);
18064 +}
18065 +
18066 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
18067 +{
18068 +       struct simple_arg arg = {
18069 +               .type = Symlink,
18070 +               .u.s.symname = symname
18071 +       };
18072 +       return add_simple(dir, dentry, &arg);
18073 +}
18074 +
18075 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18076 +               bool want_excl)
18077 +{
18078 +       struct simple_arg arg = {
18079 +               .type = Creat,
18080 +               .u.c = {
18081 +                       .mode           = mode,
18082 +                       .want_excl      = want_excl
18083 +               }
18084 +       };
18085 +       return add_simple(dir, dentry, &arg);
18086 +}
18087 +
18088 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18089 +                      struct vfsub_aopen_args *aopen_args)
18090 +{
18091 +       struct simple_arg arg = {
18092 +               .type = Creat,
18093 +               .u.c = {
18094 +                       .mode           = aopen_args->create_mode,
18095 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
18096 +                       .try_aopen      = true,
18097 +                       .aopen          = aopen_args
18098 +               }
18099 +       };
18100 +       return add_simple(dir, dentry, &arg);
18101 +}
18102 +
18103 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
18104 +{
18105 +       int err;
18106 +       aufs_bindex_t bindex;
18107 +       struct super_block *sb;
18108 +       struct dentry *parent, *h_parent, *h_dentry;
18109 +       struct inode *h_dir, *inode;
18110 +       struct vfsmount *h_mnt;
18111 +       struct au_wr_dir_args wr_dir_args = {
18112 +               .force_btgt     = -1,
18113 +               .flags          = AuWrDir_TMPFILE
18114 +       };
18115 +
18116 +       /* copy-up may happen */
18117 +       inode_lock(dir);
18118 +
18119 +       sb = dir->i_sb;
18120 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18121 +       if (unlikely(err))
18122 +               goto out;
18123 +
18124 +       err = au_di_init(dentry);
18125 +       if (unlikely(err))
18126 +               goto out_si;
18127 +
18128 +       err = -EBUSY;
18129 +       parent = d_find_any_alias(dir);
18130 +       AuDebugOn(!parent);
18131 +       di_write_lock_parent(parent);
18132 +       if (unlikely(d_inode(parent) != dir))
18133 +               goto out_parent;
18134 +
18135 +       err = au_digen_test(parent, au_sigen(sb));
18136 +       if (unlikely(err))
18137 +               goto out_parent;
18138 +
18139 +       bindex = au_dbtop(parent);
18140 +       au_set_dbtop(dentry, bindex);
18141 +       au_set_dbbot(dentry, bindex);
18142 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
18143 +       bindex = err;
18144 +       if (unlikely(err < 0))
18145 +               goto out_parent;
18146 +
18147 +       err = -EOPNOTSUPP;
18148 +       h_dir = au_h_iptr(dir, bindex);
18149 +       if (unlikely(!h_dir->i_op->tmpfile))
18150 +               goto out_parent;
18151 +
18152 +       h_mnt = au_sbr_mnt(sb, bindex);
18153 +       err = vfsub_mnt_want_write(h_mnt);
18154 +       if (unlikely(err))
18155 +               goto out_parent;
18156 +
18157 +       h_parent = au_h_dptr(parent, bindex);
18158 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
18159 +       if (IS_ERR(h_dentry)) {
18160 +               err = PTR_ERR(h_dentry);
18161 +               goto out_mnt;
18162 +       }
18163 +
18164 +       au_set_dbtop(dentry, bindex);
18165 +       au_set_dbbot(dentry, bindex);
18166 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
18167 +       inode = au_new_inode(dentry, /*must_new*/1);
18168 +       if (IS_ERR(inode)) {
18169 +               err = PTR_ERR(inode);
18170 +               au_set_h_dptr(dentry, bindex, NULL);
18171 +               au_set_dbtop(dentry, -1);
18172 +               au_set_dbbot(dentry, -1);
18173 +       } else {
18174 +               if (!inode->i_nlink)
18175 +                       set_nlink(inode, 1);
18176 +               d_tmpfile(dentry, inode);
18177 +               au_di(dentry)->di_tmpfile = 1;
18178 +
18179 +               /* update without i_mutex */
18180 +               if (au_ibtop(dir) == au_dbtop(dentry))
18181 +                       au_cpup_attr_timesizes(dir);
18182 +       }
18183 +       dput(h_dentry);
18184 +
18185 +out_mnt:
18186 +       vfsub_mnt_drop_write(h_mnt);
18187 +out_parent:
18188 +       di_write_unlock(parent);
18189 +       dput(parent);
18190 +       di_write_unlock(dentry);
18191 +       if (unlikely(err)) {
18192 +               au_di_fin(dentry);
18193 +               dentry->d_fsdata = NULL;
18194 +       }
18195 +out_si:
18196 +       si_read_unlock(sb);
18197 +out:
18198 +       inode_unlock(dir);
18199 +       return err;
18200 +}
18201 +
18202 +/* ---------------------------------------------------------------------- */
18203 +
18204 +struct au_link_args {
18205 +       aufs_bindex_t bdst, bsrc;
18206 +       struct au_pin pin;
18207 +       struct path h_path;
18208 +       struct dentry *src_parent, *parent;
18209 +};
18210 +
18211 +static int au_cpup_before_link(struct dentry *src_dentry,
18212 +                              struct au_link_args *a)
18213 +{
18214 +       int err;
18215 +       struct dentry *h_src_dentry;
18216 +       struct au_cp_generic cpg = {
18217 +               .dentry = src_dentry,
18218 +               .bdst   = a->bdst,
18219 +               .bsrc   = a->bsrc,
18220 +               .len    = -1,
18221 +               .pin    = &a->pin,
18222 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
18223 +       };
18224 +
18225 +       di_read_lock_parent(a->src_parent, AuLock_IR);
18226 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
18227 +       if (unlikely(err))
18228 +               goto out;
18229 +
18230 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
18231 +       err = au_pin(&a->pin, src_dentry, a->bdst,
18232 +                    au_opt_udba(src_dentry->d_sb),
18233 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18234 +       if (unlikely(err))
18235 +               goto out;
18236 +
18237 +       err = au_sio_cpup_simple(&cpg);
18238 +       au_unpin(&a->pin);
18239 +
18240 +out:
18241 +       di_read_unlock(a->src_parent, AuLock_IR);
18242 +       return err;
18243 +}
18244 +
18245 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
18246 +                          struct au_link_args *a)
18247 +{
18248 +       int err;
18249 +       unsigned char plink;
18250 +       aufs_bindex_t bbot;
18251 +       struct dentry *h_src_dentry;
18252 +       struct inode *h_inode, *inode, *delegated;
18253 +       struct super_block *sb;
18254 +       struct file *h_file;
18255 +
18256 +       plink = 0;
18257 +       h_inode = NULL;
18258 +       sb = src_dentry->d_sb;
18259 +       inode = d_inode(src_dentry);
18260 +       if (au_ibtop(inode) <= a->bdst)
18261 +               h_inode = au_h_iptr(inode, a->bdst);
18262 +       if (!h_inode || !h_inode->i_nlink) {
18263 +               /* copyup src_dentry as the name of dentry. */
18264 +               bbot = au_dbbot(dentry);
18265 +               if (bbot < a->bsrc)
18266 +                       au_set_dbbot(dentry, a->bsrc);
18267 +               au_set_h_dptr(dentry, a->bsrc,
18268 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
18269 +               dget(a->h_path.dentry);
18270 +               au_set_h_dptr(dentry, a->bdst, NULL);
18271 +               AuDbg("temporary d_inode...\n");
18272 +               spin_lock(&dentry->d_lock);
18273 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
18274 +               spin_unlock(&dentry->d_lock);
18275 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
18276 +               if (IS_ERR(h_file))
18277 +                       err = PTR_ERR(h_file);
18278 +               else {
18279 +                       struct au_cp_generic cpg = {
18280 +                               .dentry = dentry,
18281 +                               .bdst   = a->bdst,
18282 +                               .bsrc   = -1,
18283 +                               .len    = -1,
18284 +                               .pin    = &a->pin,
18285 +                               .flags  = AuCpup_KEEPLINO
18286 +                       };
18287 +                       err = au_sio_cpup_simple(&cpg);
18288 +                       au_h_open_post(dentry, a->bsrc, h_file);
18289 +                       if (!err) {
18290 +                               dput(a->h_path.dentry);
18291 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18292 +                       } else
18293 +                               au_set_h_dptr(dentry, a->bdst,
18294 +                                             a->h_path.dentry);
18295 +               }
18296 +               spin_lock(&dentry->d_lock);
18297 +               dentry->d_inode = NULL; /* restore */
18298 +               spin_unlock(&dentry->d_lock);
18299 +               AuDbg("temporary d_inode...done\n");
18300 +               au_set_h_dptr(dentry, a->bsrc, NULL);
18301 +               au_set_dbbot(dentry, bbot);
18302 +       } else {
18303 +               /* the inode of src_dentry already exists on a.bdst branch */
18304 +               h_src_dentry = d_find_alias(h_inode);
18305 +               if (!h_src_dentry && au_plink_test(inode)) {
18306 +                       plink = 1;
18307 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
18308 +                       err = PTR_ERR(h_src_dentry);
18309 +                       if (IS_ERR(h_src_dentry))
18310 +                               goto out;
18311 +
18312 +                       if (unlikely(d_is_negative(h_src_dentry))) {
18313 +                               dput(h_src_dentry);
18314 +                               h_src_dentry = NULL;
18315 +                       }
18316 +
18317 +               }
18318 +               if (h_src_dentry) {
18319 +                       delegated = NULL;
18320 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18321 +                                        &a->h_path, &delegated);
18322 +                       if (unlikely(err == -EWOULDBLOCK)) {
18323 +                               pr_warn("cannot retry for NFSv4 delegation"
18324 +                                       " for an internal link\n");
18325 +                               iput(delegated);
18326 +                       }
18327 +                       dput(h_src_dentry);
18328 +               } else {
18329 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
18330 +                               h_inode->i_ino, a->bdst);
18331 +                       err = -EIO;
18332 +               }
18333 +       }
18334 +
18335 +       if (!err && !plink)
18336 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
18337 +
18338 +out:
18339 +       AuTraceErr(err);
18340 +       return err;
18341 +}
18342 +
18343 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18344 +             struct dentry *dentry)
18345 +{
18346 +       int err, rerr;
18347 +       struct au_dtime dt;
18348 +       struct au_link_args *a;
18349 +       struct dentry *wh_dentry, *h_src_dentry;
18350 +       struct inode *inode, *delegated;
18351 +       struct super_block *sb;
18352 +       struct au_wr_dir_args wr_dir_args = {
18353 +               /* .force_btgt  = -1, */
18354 +               .flags          = AuWrDir_ADD_ENTRY
18355 +       };
18356 +
18357 +       IMustLock(dir);
18358 +       inode = d_inode(src_dentry);
18359 +       IMustLock(inode);
18360 +
18361 +       err = -ENOMEM;
18362 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18363 +       if (unlikely(!a))
18364 +               goto out;
18365 +
18366 +       a->parent = dentry->d_parent; /* dir inode is locked */
18367 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
18368 +                                       AuLock_NOPLM | AuLock_GEN);
18369 +       if (unlikely(err))
18370 +               goto out_kfree;
18371 +       err = au_d_linkable(src_dentry);
18372 +       if (unlikely(err))
18373 +               goto out_unlock;
18374 +       err = au_d_may_add(dentry);
18375 +       if (unlikely(err))
18376 +               goto out_unlock;
18377 +
18378 +       a->src_parent = dget_parent(src_dentry);
18379 +       wr_dir_args.force_btgt = au_ibtop(inode);
18380 +
18381 +       di_write_lock_parent(a->parent);
18382 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
18383 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
18384 +                                     &wr_dir_args);
18385 +       err = PTR_ERR(wh_dentry);
18386 +       if (IS_ERR(wh_dentry))
18387 +               goto out_parent;
18388 +
18389 +       err = 0;
18390 +       sb = dentry->d_sb;
18391 +       a->bdst = au_dbtop(dentry);
18392 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18393 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
18394 +       a->bsrc = au_ibtop(inode);
18395 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18396 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
18397 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
18398 +       if (!h_src_dentry) {
18399 +               a->bsrc = au_dbtop(src_dentry);
18400 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18401 +               AuDebugOn(!h_src_dentry);
18402 +       } else if (IS_ERR(h_src_dentry)) {
18403 +               err = PTR_ERR(h_src_dentry);
18404 +               goto out_parent;
18405 +       }
18406 +
18407 +       /*
18408 +        * aufs doesn't touch the credential so
18409 +        * security_dentry_create_files_as() is unnecrssary.
18410 +        */
18411 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
18412 +               if (a->bdst < a->bsrc
18413 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
18414 +                       err = au_cpup_or_link(src_dentry, dentry, a);
18415 +               else {
18416 +                       delegated = NULL;
18417 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18418 +                                        &a->h_path, &delegated);
18419 +                       if (unlikely(err == -EWOULDBLOCK)) {
18420 +                               pr_warn("cannot retry for NFSv4 delegation"
18421 +                                       " for an internal link\n");
18422 +                               iput(delegated);
18423 +                       }
18424 +               }
18425 +               dput(h_src_dentry);
18426 +       } else {
18427 +               /*
18428 +                * copyup src_dentry to the branch we process,
18429 +                * and then link(2) to it.
18430 +                */
18431 +               dput(h_src_dentry);
18432 +               if (a->bdst < a->bsrc
18433 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
18434 +                       au_unpin(&a->pin);
18435 +                       di_write_unlock(a->parent);
18436 +                       err = au_cpup_before_link(src_dentry, a);
18437 +                       di_write_lock_parent(a->parent);
18438 +                       if (!err)
18439 +                               err = au_pin(&a->pin, dentry, a->bdst,
18440 +                                            au_opt_udba(sb),
18441 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18442 +                       if (unlikely(err))
18443 +                               goto out_wh;
18444 +               }
18445 +               if (!err) {
18446 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
18447 +                       err = -ENOENT;
18448 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
18449 +                               delegated = NULL;
18450 +                               err = vfsub_link(h_src_dentry,
18451 +                                                au_pinned_h_dir(&a->pin),
18452 +                                                &a->h_path, &delegated);
18453 +                               if (unlikely(err == -EWOULDBLOCK)) {
18454 +                                       pr_warn("cannot retry"
18455 +                                               " for NFSv4 delegation"
18456 +                                               " for an internal link\n");
18457 +                                       iput(delegated);
18458 +                               }
18459 +                       }
18460 +               }
18461 +       }
18462 +       if (unlikely(err))
18463 +               goto out_unpin;
18464 +
18465 +       if (wh_dentry) {
18466 +               a->h_path.dentry = wh_dentry;
18467 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
18468 +                                         dentry);
18469 +               if (unlikely(err))
18470 +                       goto out_revert;
18471 +       }
18472 +
18473 +       au_dir_ts(dir, a->bdst);
18474 +       dir->i_version++;
18475 +       inc_nlink(inode);
18476 +       inode->i_ctime = dir->i_ctime;
18477 +       d_instantiate(dentry, au_igrab(inode));
18478 +       if (d_unhashed(a->h_path.dentry))
18479 +               /* some filesystem calls d_drop() */
18480 +               d_drop(dentry);
18481 +       /* some filesystems consume an inode even hardlink */
18482 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
18483 +       goto out_unpin; /* success */
18484 +
18485 +out_revert:
18486 +       /* no delegation since it is just created */
18487 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18488 +                           /*delegated*/NULL, /*force*/0);
18489 +       if (unlikely(rerr)) {
18490 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
18491 +               err = -EIO;
18492 +       }
18493 +       au_dtime_revert(&dt);
18494 +out_unpin:
18495 +       au_unpin(&a->pin);
18496 +out_wh:
18497 +       dput(wh_dentry);
18498 +out_parent:
18499 +       di_write_unlock(a->parent);
18500 +       dput(a->src_parent);
18501 +out_unlock:
18502 +       if (unlikely(err)) {
18503 +               au_update_dbtop(dentry);
18504 +               d_drop(dentry);
18505 +       }
18506 +       aufs_read_and_write_unlock2(dentry, src_dentry);
18507 +out_kfree:
18508 +       kfree(a);
18509 +out:
18510 +       AuTraceErr(err);
18511 +       return err;
18512 +}
18513 +
18514 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18515 +{
18516 +       int err, rerr;
18517 +       aufs_bindex_t bindex;
18518 +       unsigned char diropq;
18519 +       struct path h_path;
18520 +       struct dentry *wh_dentry, *parent, *opq_dentry;
18521 +       struct inode *h_inode;
18522 +       struct super_block *sb;
18523 +       struct {
18524 +               struct au_pin pin;
18525 +               struct au_dtime dt;
18526 +       } *a; /* reduce the stack usage */
18527 +       struct au_wr_dir_args wr_dir_args = {
18528 +               .force_btgt     = -1,
18529 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18530 +       };
18531 +
18532 +       IMustLock(dir);
18533 +
18534 +       err = -ENOMEM;
18535 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18536 +       if (unlikely(!a))
18537 +               goto out;
18538 +
18539 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18540 +       if (unlikely(err))
18541 +               goto out_free;
18542 +       err = au_d_may_add(dentry);
18543 +       if (unlikely(err))
18544 +               goto out_unlock;
18545 +
18546 +       parent = dentry->d_parent; /* dir inode is locked */
18547 +       di_write_lock_parent(parent);
18548 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18549 +                                     &a->pin, &wr_dir_args);
18550 +       err = PTR_ERR(wh_dentry);
18551 +       if (IS_ERR(wh_dentry))
18552 +               goto out_parent;
18553 +
18554 +       sb = dentry->d_sb;
18555 +       bindex = au_dbtop(dentry);
18556 +       h_path.dentry = au_h_dptr(dentry, bindex);
18557 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18558 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18559 +       if (unlikely(err))
18560 +               goto out_unpin;
18561 +
18562 +       /* make the dir opaque */
18563 +       diropq = 0;
18564 +       h_inode = d_inode(h_path.dentry);
18565 +       if (wh_dentry
18566 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18567 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18568 +               opq_dentry = au_diropq_create(dentry, bindex);
18569 +               inode_unlock(h_inode);
18570 +               err = PTR_ERR(opq_dentry);
18571 +               if (IS_ERR(opq_dentry))
18572 +                       goto out_dir;
18573 +               dput(opq_dentry);
18574 +               diropq = 1;
18575 +       }
18576 +
18577 +       err = epilog(dir, bindex, wh_dentry, dentry);
18578 +       if (!err) {
18579 +               inc_nlink(dir);
18580 +               goto out_unpin; /* success */
18581 +       }
18582 +
18583 +       /* revert */
18584 +       if (diropq) {
18585 +               AuLabel(revert opq);
18586 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18587 +               rerr = au_diropq_remove(dentry, bindex);
18588 +               inode_unlock(h_inode);
18589 +               if (rerr) {
18590 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18591 +                               dentry, err, rerr);
18592 +                       err = -EIO;
18593 +               }
18594 +       }
18595 +
18596 +out_dir:
18597 +       AuLabel(revert dir);
18598 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18599 +       if (rerr) {
18600 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18601 +                       dentry, err, rerr);
18602 +               err = -EIO;
18603 +       }
18604 +       au_dtime_revert(&a->dt);
18605 +out_unpin:
18606 +       au_unpin(&a->pin);
18607 +       dput(wh_dentry);
18608 +out_parent:
18609 +       di_write_unlock(parent);
18610 +out_unlock:
18611 +       if (unlikely(err)) {
18612 +               au_update_dbtop(dentry);
18613 +               d_drop(dentry);
18614 +       }
18615 +       aufs_read_unlock(dentry, AuLock_DW);
18616 +out_free:
18617 +       kfree(a);
18618 +out:
18619 +       return err;
18620 +}
18621 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18622 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18623 +++ linux/fs/aufs/i_op.c        2017-07-29 12:14:25.903042072 +0200
18624 @@ -0,0 +1,1452 @@
18625 +/*
18626 + * Copyright (C) 2005-2017 Junjiro R. Okajima
18627 + *
18628 + * This program, aufs is free software; you can redistribute it and/or modify
18629 + * it under the terms of the GNU General Public License as published by
18630 + * the Free Software Foundation; either version 2 of the License, or
18631 + * (at your option) any later version.
18632 + *
18633 + * This program is distributed in the hope that it will be useful,
18634 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18635 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18636 + * GNU General Public License for more details.
18637 + *
18638 + * You should have received a copy of the GNU General Public License
18639 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18640 + */
18641 +
18642 +/*
18643 + * inode operations (except add/del/rename)
18644 + */
18645 +
18646 +#include <linux/device_cgroup.h>
18647 +#include <linux/fs_stack.h>
18648 +#include <linux/namei.h>
18649 +#include <linux/security.h>
18650 +#include "aufs.h"
18651 +
18652 +static int h_permission(struct inode *h_inode, int mask,
18653 +                       struct path *h_path, int brperm)
18654 +{
18655 +       int err;
18656 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18657 +
18658 +       err = -EPERM;
18659 +       if (write_mask && IS_IMMUTABLE(h_inode))
18660 +               goto out;
18661 +
18662 +       err = -EACCES;
18663 +       if (((mask & MAY_EXEC)
18664 +            && S_ISREG(h_inode->i_mode)
18665 +            && (path_noexec(h_path)
18666 +                || !(h_inode->i_mode & S_IXUGO))))
18667 +               goto out;
18668 +
18669 +       /*
18670 +        * - skip the lower fs test in the case of write to ro branch.
18671 +        * - nfs dir permission write check is optimized, but a policy for
18672 +        *   link/rename requires a real check.
18673 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18674 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18675 +        */
18676 +       if ((write_mask && !au_br_writable(brperm))
18677 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18678 +               && write_mask && !(mask & MAY_READ))
18679 +           || !h_inode->i_op->permission) {
18680 +               /* AuLabel(generic_permission); */
18681 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18682 +               err = generic_permission(h_inode, mask);
18683 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18684 +                       err = h_inode->i_op->permission(h_inode, mask);
18685 +               AuTraceErr(err);
18686 +       } else {
18687 +               /* AuLabel(h_inode->permission); */
18688 +               err = h_inode->i_op->permission(h_inode, mask);
18689 +               AuTraceErr(err);
18690 +       }
18691 +
18692 +       if (!err)
18693 +               err = devcgroup_inode_permission(h_inode, mask);
18694 +       if (!err)
18695 +               err = security_inode_permission(h_inode, mask);
18696 +
18697 +#if 0
18698 +       if (!err) {
18699 +               /* todo: do we need to call ima_path_check()? */
18700 +               struct path h_path = {
18701 +                       .dentry =
18702 +                       .mnt    = h_mnt
18703 +               };
18704 +               err = ima_path_check(&h_path,
18705 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18706 +                                    IMA_COUNT_LEAVE);
18707 +       }
18708 +#endif
18709 +
18710 +out:
18711 +       return err;
18712 +}
18713 +
18714 +static int aufs_permission(struct inode *inode, int mask)
18715 +{
18716 +       int err;
18717 +       aufs_bindex_t bindex, bbot;
18718 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18719 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18720 +       struct inode *h_inode;
18721 +       struct super_block *sb;
18722 +       struct au_branch *br;
18723 +
18724 +       /* todo: support rcu-walk? */
18725 +       if (mask & MAY_NOT_BLOCK)
18726 +               return -ECHILD;
18727 +
18728 +       sb = inode->i_sb;
18729 +       si_read_lock(sb, AuLock_FLUSH);
18730 +       ii_read_lock_child(inode);
18731 +#if 0
18732 +       err = au_iigen_test(inode, au_sigen(sb));
18733 +       if (unlikely(err))
18734 +               goto out;
18735 +#endif
18736 +
18737 +       if (!isdir
18738 +           || write_mask
18739 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18740 +               err = au_busy_or_stale();
18741 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
18742 +               if (unlikely(!h_inode
18743 +                            || (h_inode->i_mode & S_IFMT)
18744 +                            != (inode->i_mode & S_IFMT)))
18745 +                       goto out;
18746 +
18747 +               err = 0;
18748 +               bindex = au_ibtop(inode);
18749 +               br = au_sbr(sb, bindex);
18750 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
18751 +               if (write_mask
18752 +                   && !err
18753 +                   && !special_file(h_inode->i_mode)) {
18754 +                       /* test whether the upper writable branch exists */
18755 +                       err = -EROFS;
18756 +                       for (; bindex >= 0; bindex--)
18757 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18758 +                                       err = 0;
18759 +                                       break;
18760 +                               }
18761 +               }
18762 +               goto out;
18763 +       }
18764 +
18765 +       /* non-write to dir */
18766 +       err = 0;
18767 +       bbot = au_ibbot(inode);
18768 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
18769 +               h_inode = au_h_iptr(inode, bindex);
18770 +               if (h_inode) {
18771 +                       err = au_busy_or_stale();
18772 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18773 +                               break;
18774 +
18775 +                       br = au_sbr(sb, bindex);
18776 +                       err = h_permission(h_inode, mask, &br->br_path,
18777 +                                          br->br_perm);
18778 +               }
18779 +       }
18780 +
18781 +out:
18782 +       ii_read_unlock(inode);
18783 +       si_read_unlock(sb);
18784 +       return err;
18785 +}
18786 +
18787 +/* ---------------------------------------------------------------------- */
18788 +
18789 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18790 +                                 unsigned int flags)
18791 +{
18792 +       struct dentry *ret, *parent;
18793 +       struct inode *inode;
18794 +       struct super_block *sb;
18795 +       int err, npositive;
18796 +
18797 +       IMustLock(dir);
18798 +
18799 +       /* todo: support rcu-walk? */
18800 +       ret = ERR_PTR(-ECHILD);
18801 +       if (flags & LOOKUP_RCU)
18802 +               goto out;
18803 +
18804 +       ret = ERR_PTR(-ENAMETOOLONG);
18805 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18806 +               goto out;
18807 +
18808 +       sb = dir->i_sb;
18809 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18810 +       ret = ERR_PTR(err);
18811 +       if (unlikely(err))
18812 +               goto out;
18813 +
18814 +       err = au_di_init(dentry);
18815 +       ret = ERR_PTR(err);
18816 +       if (unlikely(err))
18817 +               goto out_si;
18818 +
18819 +       inode = NULL;
18820 +       npositive = 0; /* suppress a warning */
18821 +       parent = dentry->d_parent; /* dir inode is locked */
18822 +       di_read_lock_parent(parent, AuLock_IR);
18823 +       err = au_alive_dir(parent);
18824 +       if (!err)
18825 +               err = au_digen_test(parent, au_sigen(sb));
18826 +       if (!err) {
18827 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
18828 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
18829 +                                          AuLkup_ALLOW_NEG);
18830 +               err = npositive;
18831 +       }
18832 +       di_read_unlock(parent, AuLock_IR);
18833 +       ret = ERR_PTR(err);
18834 +       if (unlikely(err < 0))
18835 +               goto out_unlock;
18836 +
18837 +       if (npositive) {
18838 +               inode = au_new_inode(dentry, /*must_new*/0);
18839 +               if (IS_ERR(inode)) {
18840 +                       ret = (void *)inode;
18841 +                       inode = NULL;
18842 +                       goto out_unlock;
18843 +               }
18844 +       }
18845 +
18846 +       if (inode)
18847 +               atomic_inc(&inode->i_count);
18848 +       ret = d_splice_alias(inode, dentry);
18849 +#if 0
18850 +       if (unlikely(d_need_lookup(dentry))) {
18851 +               spin_lock(&dentry->d_lock);
18852 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18853 +               spin_unlock(&dentry->d_lock);
18854 +       } else
18855 +#endif
18856 +       if (inode) {
18857 +               if (!IS_ERR(ret)) {
18858 +                       iput(inode);
18859 +                       if (ret && ret != dentry)
18860 +                               ii_write_unlock(inode);
18861 +               } else {
18862 +                       ii_write_unlock(inode);
18863 +                       iput(inode);
18864 +                       inode = NULL;
18865 +               }
18866 +       }
18867 +
18868 +out_unlock:
18869 +       di_write_unlock(dentry);
18870 +out_si:
18871 +       si_read_unlock(sb);
18872 +out:
18873 +       return ret;
18874 +}
18875 +
18876 +/* ---------------------------------------------------------------------- */
18877 +
18878 +struct aopen_node {
18879 +       struct hlist_node hlist;
18880 +       struct file *file, *h_file;
18881 +};
18882 +
18883 +static int au_do_aopen(struct inode *inode, struct file *file)
18884 +{
18885 +       struct au_sphlhead *aopen;
18886 +       struct aopen_node *node;
18887 +       struct au_do_open_args args = {
18888 +               .no_lock        = 1,
18889 +               .open           = au_do_open_nondir
18890 +       };
18891 +
18892 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18893 +       spin_lock(&aopen->spin);
18894 +       hlist_for_each_entry(node, &aopen->head, hlist)
18895 +               if (node->file == file) {
18896 +                       args.h_file = node->h_file;
18897 +                       break;
18898 +               }
18899 +       spin_unlock(&aopen->spin);
18900 +       /* AuDebugOn(!args.h_file); */
18901 +
18902 +       return au_do_open(file, &args);
18903 +}
18904 +
18905 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18906 +                           struct file *file, unsigned int open_flag,
18907 +                           umode_t create_mode, int *opened)
18908 +{
18909 +       int err, h_opened = *opened;
18910 +       unsigned int lkup_flags;
18911 +       struct dentry *parent, *d;
18912 +       struct au_sphlhead *aopen;
18913 +       struct vfsub_aopen_args args = {
18914 +               .open_flag      = open_flag,
18915 +               .create_mode    = create_mode,
18916 +               .opened         = &h_opened
18917 +       };
18918 +       struct aopen_node aopen_node = {
18919 +               .file   = file
18920 +       };
18921 +
18922 +       IMustLock(dir);
18923 +       AuDbg("open_flag 0%o\n", open_flag);
18924 +       AuDbgDentry(dentry);
18925 +
18926 +       err = 0;
18927 +       if (!au_di(dentry)) {
18928 +               lkup_flags = LOOKUP_OPEN;
18929 +               if (open_flag & O_CREAT)
18930 +                       lkup_flags |= LOOKUP_CREATE;
18931 +               d = aufs_lookup(dir, dentry, lkup_flags);
18932 +               if (IS_ERR(d)) {
18933 +                       err = PTR_ERR(d);
18934 +                       AuTraceErr(err);
18935 +                       goto out;
18936 +               } else if (d) {
18937 +                       /*
18938 +                        * obsoleted dentry found.
18939 +                        * another error will be returned later.
18940 +                        */
18941 +                       d_drop(d);
18942 +                       AuDbgDentry(d);
18943 +                       dput(d);
18944 +               }
18945 +               AuDbgDentry(dentry);
18946 +       }
18947 +
18948 +       if (d_is_positive(dentry)
18949 +           || d_unhashed(dentry)
18950 +           || d_unlinked(dentry)
18951 +           || !(open_flag & O_CREAT))
18952 +               goto out_no_open;
18953 +
18954 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18955 +       if (unlikely(err))
18956 +               goto out;
18957 +
18958 +       parent = dentry->d_parent;      /* dir is locked */
18959 +       di_write_lock_parent(parent);
18960 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
18961 +       if (unlikely(err))
18962 +               goto out_unlock;
18963 +
18964 +       AuDbgDentry(dentry);
18965 +       if (d_is_positive(dentry))
18966 +               goto out_unlock;
18967 +
18968 +       args.file = get_empty_filp();
18969 +       err = PTR_ERR(args.file);
18970 +       if (IS_ERR(args.file))
18971 +               goto out_unlock;
18972 +
18973 +       args.file->f_flags = file->f_flags;
18974 +       err = au_aopen_or_create(dir, dentry, &args);
18975 +       AuTraceErr(err);
18976 +       AuDbgFile(args.file);
18977 +       if (unlikely(err < 0)) {
18978 +               if (h_opened & FILE_OPENED)
18979 +                       fput(args.file);
18980 +               else
18981 +                       put_filp(args.file);
18982 +               goto out_unlock;
18983 +       }
18984 +
18985 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18986 +       *opened |= FILE_CREATED;
18987 +       if (h_opened & FILE_OPENED)
18988 +               aopen_node.h_file = args.file;
18989 +       else {
18990 +               put_filp(args.file);
18991 +               args.file = NULL;
18992 +       }
18993 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18994 +       au_sphl_add(&aopen_node.hlist, aopen);
18995 +       err = finish_open(file, dentry, au_do_aopen, opened);
18996 +       au_sphl_del(&aopen_node.hlist, aopen);
18997 +       AuTraceErr(err);
18998 +       AuDbgFile(file);
18999 +       if (aopen_node.h_file)
19000 +               fput(aopen_node.h_file);
19001 +
19002 +out_unlock:
19003 +       di_write_unlock(parent);
19004 +       aufs_read_unlock(dentry, AuLock_DW);
19005 +       AuDbgDentry(dentry);
19006 +       if (unlikely(err < 0))
19007 +               goto out;
19008 +out_no_open:
19009 +       if (err >= 0 && !(*opened & FILE_CREATED)) {
19010 +               AuLabel(out_no_open);
19011 +               dget(dentry);
19012 +               err = finish_no_open(file, dentry);
19013 +       }
19014 +out:
19015 +       AuDbg("%pd%s%s\n", dentry,
19016 +             (*opened & FILE_CREATED) ? " created" : "",
19017 +             (*opened & FILE_OPENED) ? " opened" : "");
19018 +       AuTraceErr(err);
19019 +       return err;
19020 +}
19021 +
19022 +
19023 +/* ---------------------------------------------------------------------- */
19024 +
19025 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
19026 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
19027 +                         aufs_bindex_t btop)
19028 +{
19029 +       int err;
19030 +       struct dentry *h_parent;
19031 +       struct inode *h_dir;
19032 +
19033 +       if (add_entry)
19034 +               IMustLock(d_inode(parent));
19035 +       else
19036 +               di_write_lock_parent(parent);
19037 +
19038 +       err = 0;
19039 +       if (!au_h_dptr(parent, bcpup)) {
19040 +               if (btop > bcpup)
19041 +                       err = au_cpup_dirs(dentry, bcpup);
19042 +               else if (btop < bcpup)
19043 +                       err = au_cpdown_dirs(dentry, bcpup);
19044 +               else
19045 +                       BUG();
19046 +       }
19047 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
19048 +               h_parent = au_h_dptr(parent, bcpup);
19049 +               h_dir = d_inode(h_parent);
19050 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
19051 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
19052 +               /* todo: no unlock here */
19053 +               inode_unlock(h_dir);
19054 +
19055 +               AuDbg("bcpup %d\n", bcpup);
19056 +               if (!err) {
19057 +                       if (d_really_is_negative(dentry))
19058 +                               au_set_h_dptr(dentry, btop, NULL);
19059 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
19060 +               }
19061 +       }
19062 +
19063 +       if (!add_entry)
19064 +               di_write_unlock(parent);
19065 +       if (!err)
19066 +               err = bcpup; /* success */
19067 +
19068 +       AuTraceErr(err);
19069 +       return err;
19070 +}
19071 +
19072 +/*
19073 + * decide the branch and the parent dir where we will create a new entry.
19074 + * returns new bindex or an error.
19075 + * copyup the parent dir if needed.
19076 + */
19077 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19078 +             struct au_wr_dir_args *args)
19079 +{
19080 +       int err;
19081 +       unsigned int flags;
19082 +       aufs_bindex_t bcpup, btop, src_btop;
19083 +       const unsigned char add_entry
19084 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
19085 +               | au_ftest_wrdir(args->flags, TMPFILE);
19086 +       struct super_block *sb;
19087 +       struct dentry *parent;
19088 +       struct au_sbinfo *sbinfo;
19089 +
19090 +       sb = dentry->d_sb;
19091 +       sbinfo = au_sbi(sb);
19092 +       parent = dget_parent(dentry);
19093 +       btop = au_dbtop(dentry);
19094 +       bcpup = btop;
19095 +       if (args->force_btgt < 0) {
19096 +               if (src_dentry) {
19097 +                       src_btop = au_dbtop(src_dentry);
19098 +                       if (src_btop < btop)
19099 +                               bcpup = src_btop;
19100 +               } else if (add_entry) {
19101 +                       flags = 0;
19102 +                       if (au_ftest_wrdir(args->flags, ISDIR))
19103 +                               au_fset_wbr(flags, DIR);
19104 +                       err = AuWbrCreate(sbinfo, dentry, flags);
19105 +                       bcpup = err;
19106 +               }
19107 +
19108 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
19109 +                       if (add_entry)
19110 +                               err = AuWbrCopyup(sbinfo, dentry);
19111 +                       else {
19112 +                               if (!IS_ROOT(dentry)) {
19113 +                                       di_read_lock_parent(parent, !AuLock_IR);
19114 +                                       err = AuWbrCopyup(sbinfo, dentry);
19115 +                                       di_read_unlock(parent, !AuLock_IR);
19116 +                               } else
19117 +                                       err = AuWbrCopyup(sbinfo, dentry);
19118 +                       }
19119 +                       bcpup = err;
19120 +                       if (unlikely(err < 0))
19121 +                               goto out;
19122 +               }
19123 +       } else {
19124 +               bcpup = args->force_btgt;
19125 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
19126 +       }
19127 +
19128 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
19129 +       err = bcpup;
19130 +       if (bcpup == btop)
19131 +               goto out; /* success */
19132 +
19133 +       /* copyup the new parent into the branch we process */
19134 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
19135 +       if (err >= 0) {
19136 +               if (d_really_is_negative(dentry)) {
19137 +                       au_set_h_dptr(dentry, btop, NULL);
19138 +                       au_set_dbtop(dentry, bcpup);
19139 +                       au_set_dbbot(dentry, bcpup);
19140 +               }
19141 +               AuDebugOn(add_entry
19142 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
19143 +                         && !au_h_dptr(dentry, bcpup));
19144 +       }
19145 +
19146 +out:
19147 +       dput(parent);
19148 +       return err;
19149 +}
19150 +
19151 +/* ---------------------------------------------------------------------- */
19152 +
19153 +void au_pin_hdir_unlock(struct au_pin *p)
19154 +{
19155 +       if (p->hdir)
19156 +               au_hn_inode_unlock(p->hdir);
19157 +}
19158 +
19159 +int au_pin_hdir_lock(struct au_pin *p)
19160 +{
19161 +       int err;
19162 +
19163 +       err = 0;
19164 +       if (!p->hdir)
19165 +               goto out;
19166 +
19167 +       /* even if an error happens later, keep this lock */
19168 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
19169 +
19170 +       err = -EBUSY;
19171 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
19172 +               goto out;
19173 +
19174 +       err = 0;
19175 +       if (p->h_dentry)
19176 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
19177 +                                 p->h_parent, p->br);
19178 +
19179 +out:
19180 +       return err;
19181 +}
19182 +
19183 +int au_pin_hdir_relock(struct au_pin *p)
19184 +{
19185 +       int err, i;
19186 +       struct inode *h_i;
19187 +       struct dentry *h_d[] = {
19188 +               p->h_dentry,
19189 +               p->h_parent
19190 +       };
19191 +
19192 +       err = au_pin_hdir_lock(p);
19193 +       if (unlikely(err))
19194 +               goto out;
19195 +
19196 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
19197 +               if (!h_d[i])
19198 +                       continue;
19199 +               if (d_is_positive(h_d[i])) {
19200 +                       h_i = d_inode(h_d[i]);
19201 +                       err = !h_i->i_nlink;
19202 +               }
19203 +       }
19204 +
19205 +out:
19206 +       return err;
19207 +}
19208 +
19209 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
19210 +{
19211 +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
19212 +       p->hdir->hi_inode->i_rwsem.owner = task;
19213 +#endif
19214 +}
19215 +
19216 +void au_pin_hdir_acquire_nest(struct au_pin *p)
19217 +{
19218 +       if (p->hdir) {
19219 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
19220 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
19221 +               au_pin_hdir_set_owner(p, current);
19222 +       }
19223 +}
19224 +
19225 +void au_pin_hdir_release(struct au_pin *p)
19226 +{
19227 +       if (p->hdir) {
19228 +               au_pin_hdir_set_owner(p, p->task);
19229 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
19230 +       }
19231 +}
19232 +
19233 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
19234 +{
19235 +       if (pin && pin->parent)
19236 +               return au_h_dptr(pin->parent, pin->bindex);
19237 +       return NULL;
19238 +}
19239 +
19240 +void au_unpin(struct au_pin *p)
19241 +{
19242 +       if (p->hdir)
19243 +               au_pin_hdir_unlock(p);
19244 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
19245 +               vfsub_mnt_drop_write(p->h_mnt);
19246 +       if (!p->hdir)
19247 +               return;
19248 +
19249 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
19250 +               di_read_unlock(p->parent, AuLock_IR);
19251 +       iput(p->hdir->hi_inode);
19252 +       dput(p->parent);
19253 +       p->parent = NULL;
19254 +       p->hdir = NULL;
19255 +       p->h_mnt = NULL;
19256 +       /* do not clear p->task */
19257 +}
19258 +
19259 +int au_do_pin(struct au_pin *p)
19260 +{
19261 +       int err;
19262 +       struct super_block *sb;
19263 +       struct inode *h_dir;
19264 +
19265 +       err = 0;
19266 +       sb = p->dentry->d_sb;
19267 +       p->br = au_sbr(sb, p->bindex);
19268 +       if (IS_ROOT(p->dentry)) {
19269 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
19270 +                       p->h_mnt = au_br_mnt(p->br);
19271 +                       err = vfsub_mnt_want_write(p->h_mnt);
19272 +                       if (unlikely(err)) {
19273 +                               au_fclr_pin(p->flags, MNT_WRITE);
19274 +                               goto out_err;
19275 +                       }
19276 +               }
19277 +               goto out;
19278 +       }
19279 +
19280 +       p->h_dentry = NULL;
19281 +       if (p->bindex <= au_dbbot(p->dentry))
19282 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
19283 +
19284 +       p->parent = dget_parent(p->dentry);
19285 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
19286 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
19287 +
19288 +       h_dir = NULL;
19289 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
19290 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
19291 +       if (p->hdir)
19292 +               h_dir = p->hdir->hi_inode;
19293 +
19294 +       /*
19295 +        * udba case, or
19296 +        * if DI_LOCKED is not set, then p->parent may be different
19297 +        * and h_parent can be NULL.
19298 +        */
19299 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
19300 +               err = -EBUSY;
19301 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
19302 +                       di_read_unlock(p->parent, AuLock_IR);
19303 +               dput(p->parent);
19304 +               p->parent = NULL;
19305 +               goto out_err;
19306 +       }
19307 +
19308 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
19309 +               p->h_mnt = au_br_mnt(p->br);
19310 +               err = vfsub_mnt_want_write(p->h_mnt);
19311 +               if (unlikely(err)) {
19312 +                       au_fclr_pin(p->flags, MNT_WRITE);
19313 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
19314 +                               di_read_unlock(p->parent, AuLock_IR);
19315 +                       dput(p->parent);
19316 +                       p->parent = NULL;
19317 +                       goto out_err;
19318 +               }
19319 +       }
19320 +
19321 +       au_igrab(h_dir);
19322 +       err = au_pin_hdir_lock(p);
19323 +       if (!err)
19324 +               goto out; /* success */
19325 +
19326 +       au_unpin(p);
19327 +
19328 +out_err:
19329 +       pr_err("err %d\n", err);
19330 +       err = au_busy_or_stale();
19331 +out:
19332 +       return err;
19333 +}
19334 +
19335 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
19336 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19337 +                unsigned int udba, unsigned char flags)
19338 +{
19339 +       p->dentry = dentry;
19340 +       p->udba = udba;
19341 +       p->lsc_di = lsc_di;
19342 +       p->lsc_hi = lsc_hi;
19343 +       p->flags = flags;
19344 +       p->bindex = bindex;
19345 +
19346 +       p->parent = NULL;
19347 +       p->hdir = NULL;
19348 +       p->h_mnt = NULL;
19349 +
19350 +       p->h_dentry = NULL;
19351 +       p->h_parent = NULL;
19352 +       p->br = NULL;
19353 +       p->task = current;
19354 +}
19355 +
19356 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19357 +          unsigned int udba, unsigned char flags)
19358 +{
19359 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19360 +                   udba, flags);
19361 +       return au_do_pin(pin);
19362 +}
19363 +
19364 +/* ---------------------------------------------------------------------- */
19365 +
19366 +/*
19367 + * ->setattr() and ->getattr() are called in various cases.
19368 + * chmod, stat: dentry is revalidated.
19369 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19370 + *               unhashed.
19371 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
19372 + */
19373 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
19374 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
19375 +{
19376 +       int err;
19377 +       struct dentry *parent;
19378 +
19379 +       err = 0;
19380 +       if (au_digen_test(dentry, sigen)) {
19381 +               parent = dget_parent(dentry);
19382 +               di_read_lock_parent(parent, AuLock_IR);
19383 +               err = au_refresh_dentry(dentry, parent);
19384 +               di_read_unlock(parent, AuLock_IR);
19385 +               dput(parent);
19386 +       }
19387 +
19388 +       AuTraceErr(err);
19389 +       return err;
19390 +}
19391 +
19392 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19393 +                    struct au_icpup_args *a)
19394 +{
19395 +       int err;
19396 +       loff_t sz;
19397 +       aufs_bindex_t btop, ibtop;
19398 +       struct dentry *hi_wh, *parent;
19399 +       struct inode *inode;
19400 +       struct au_wr_dir_args wr_dir_args = {
19401 +               .force_btgt     = -1,
19402 +               .flags          = 0
19403 +       };
19404 +
19405 +       if (d_is_dir(dentry))
19406 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
19407 +       /* plink or hi_wh() case */
19408 +       btop = au_dbtop(dentry);
19409 +       inode = d_inode(dentry);
19410 +       ibtop = au_ibtop(inode);
19411 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
19412 +               wr_dir_args.force_btgt = ibtop;
19413 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19414 +       if (unlikely(err < 0))
19415 +               goto out;
19416 +       a->btgt = err;
19417 +       if (err != btop)
19418 +               au_fset_icpup(a->flags, DID_CPUP);
19419 +
19420 +       err = 0;
19421 +       a->pin_flags = AuPin_MNT_WRITE;
19422 +       parent = NULL;
19423 +       if (!IS_ROOT(dentry)) {
19424 +               au_fset_pin(a->pin_flags, DI_LOCKED);
19425 +               parent = dget_parent(dentry);
19426 +               di_write_lock_parent(parent);
19427 +       }
19428 +
19429 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19430 +       if (unlikely(err))
19431 +               goto out_parent;
19432 +
19433 +       sz = -1;
19434 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19435 +       a->h_inode = d_inode(a->h_path.dentry);
19436 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
19437 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19438 +               if (ia->ia_size < i_size_read(a->h_inode))
19439 +                       sz = ia->ia_size;
19440 +               inode_unlock(a->h_inode);
19441 +       }
19442 +
19443 +       hi_wh = NULL;
19444 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
19445 +               hi_wh = au_hi_wh(inode, a->btgt);
19446 +               if (!hi_wh) {
19447 +                       struct au_cp_generic cpg = {
19448 +                               .dentry = dentry,
19449 +                               .bdst   = a->btgt,
19450 +                               .bsrc   = -1,
19451 +                               .len    = sz,
19452 +                               .pin    = &a->pin
19453 +                       };
19454 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
19455 +                       if (unlikely(err))
19456 +                               goto out_unlock;
19457 +                       hi_wh = au_hi_wh(inode, a->btgt);
19458 +                       /* todo: revalidate hi_wh? */
19459 +               }
19460 +       }
19461 +
19462 +       if (parent) {
19463 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19464 +               di_downgrade_lock(parent, AuLock_IR);
19465 +               dput(parent);
19466 +               parent = NULL;
19467 +       }
19468 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
19469 +               goto out; /* success */
19470 +
19471 +       if (!d_unhashed(dentry)) {
19472 +               struct au_cp_generic cpg = {
19473 +                       .dentry = dentry,
19474 +                       .bdst   = a->btgt,
19475 +                       .bsrc   = btop,
19476 +                       .len    = sz,
19477 +                       .pin    = &a->pin,
19478 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
19479 +               };
19480 +               err = au_sio_cpup_simple(&cpg);
19481 +               if (!err)
19482 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19483 +       } else if (!hi_wh)
19484 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19485 +       else
19486 +               a->h_path.dentry = hi_wh; /* do not dget here */
19487 +
19488 +out_unlock:
19489 +       a->h_inode = d_inode(a->h_path.dentry);
19490 +       if (!err)
19491 +               goto out; /* success */
19492 +       au_unpin(&a->pin);
19493 +out_parent:
19494 +       if (parent) {
19495 +               di_write_unlock(parent);
19496 +               dput(parent);
19497 +       }
19498 +out:
19499 +       if (!err)
19500 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19501 +       return err;
19502 +}
19503 +
19504 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
19505 +{
19506 +       int err;
19507 +       struct inode *inode, *delegated;
19508 +       struct super_block *sb;
19509 +       struct file *file;
19510 +       struct au_icpup_args *a;
19511 +
19512 +       inode = d_inode(dentry);
19513 +       IMustLock(inode);
19514 +
19515 +       err = setattr_prepare(dentry, ia);
19516 +       if (unlikely(err))
19517 +               goto out;
19518 +
19519 +       err = -ENOMEM;
19520 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19521 +       if (unlikely(!a))
19522 +               goto out;
19523 +
19524 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19525 +               ia->ia_valid &= ~ATTR_MODE;
19526 +
19527 +       file = NULL;
19528 +       sb = dentry->d_sb;
19529 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19530 +       if (unlikely(err))
19531 +               goto out_kfree;
19532 +
19533 +       if (ia->ia_valid & ATTR_FILE) {
19534 +               /* currently ftruncate(2) only */
19535 +               AuDebugOn(!d_is_reg(dentry));
19536 +               file = ia->ia_file;
19537 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
19538 +                                           /*fi_lsc*/0);
19539 +               if (unlikely(err))
19540 +                       goto out_si;
19541 +               ia->ia_file = au_hf_top(file);
19542 +               a->udba = AuOpt_UDBA_NONE;
19543 +       } else {
19544 +               /* fchmod() doesn't pass ia_file */
19545 +               a->udba = au_opt_udba(sb);
19546 +               di_write_lock_child(dentry);
19547 +               /* no d_unlinked(), to set UDBA_NONE for root */
19548 +               if (d_unhashed(dentry))
19549 +                       a->udba = AuOpt_UDBA_NONE;
19550 +               if (a->udba != AuOpt_UDBA_NONE) {
19551 +                       AuDebugOn(IS_ROOT(dentry));
19552 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19553 +                       if (unlikely(err))
19554 +                               goto out_dentry;
19555 +               }
19556 +       }
19557 +
19558 +       err = au_pin_and_icpup(dentry, ia, a);
19559 +       if (unlikely(err < 0))
19560 +               goto out_dentry;
19561 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19562 +               ia->ia_file = NULL;
19563 +               ia->ia_valid &= ~ATTR_FILE;
19564 +       }
19565 +
19566 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19567 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19568 +           == (ATTR_MODE | ATTR_CTIME)) {
19569 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19570 +               if (unlikely(err))
19571 +                       goto out_unlock;
19572 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19573 +                  && (ia->ia_valid & ATTR_CTIME)) {
19574 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19575 +               if (unlikely(err))
19576 +                       goto out_unlock;
19577 +       }
19578 +
19579 +       if (ia->ia_valid & ATTR_SIZE) {
19580 +               struct file *f;
19581 +
19582 +               if (ia->ia_size < i_size_read(inode))
19583 +                       /* unmap only */
19584 +                       truncate_setsize(inode, ia->ia_size);
19585 +
19586 +               f = NULL;
19587 +               if (ia->ia_valid & ATTR_FILE)
19588 +                       f = ia->ia_file;
19589 +               inode_unlock(a->h_inode);
19590 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19591 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19592 +       } else {
19593 +               delegated = NULL;
19594 +               while (1) {
19595 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19596 +                       if (delegated) {
19597 +                               err = break_deleg_wait(&delegated);
19598 +                               if (!err)
19599 +                                       continue;
19600 +                       }
19601 +                       break;
19602 +               }
19603 +       }
19604 +       /*
19605 +        * regardless aufs 'acl' option setting.
19606 +        * why don't all acl-aware fs call this func from their ->setattr()?
19607 +        */
19608 +       if (!err && (ia->ia_valid & ATTR_MODE))
19609 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
19610 +       if (!err)
19611 +               au_cpup_attr_changeable(inode);
19612 +
19613 +out_unlock:
19614 +       inode_unlock(a->h_inode);
19615 +       au_unpin(&a->pin);
19616 +       if (unlikely(err))
19617 +               au_update_dbtop(dentry);
19618 +out_dentry:
19619 +       di_write_unlock(dentry);
19620 +       if (file) {
19621 +               fi_write_unlock(file);
19622 +               ia->ia_file = file;
19623 +               ia->ia_valid |= ATTR_FILE;
19624 +       }
19625 +out_si:
19626 +       si_read_unlock(sb);
19627 +out_kfree:
19628 +       kfree(a);
19629 +out:
19630 +       AuTraceErr(err);
19631 +       return err;
19632 +}
19633 +
19634 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19635 +static int au_h_path_to_set_attr(struct dentry *dentry,
19636 +                                struct au_icpup_args *a, struct path *h_path)
19637 +{
19638 +       int err;
19639 +       struct super_block *sb;
19640 +
19641 +       sb = dentry->d_sb;
19642 +       a->udba = au_opt_udba(sb);
19643 +       /* no d_unlinked(), to set UDBA_NONE for root */
19644 +       if (d_unhashed(dentry))
19645 +               a->udba = AuOpt_UDBA_NONE;
19646 +       if (a->udba != AuOpt_UDBA_NONE) {
19647 +               AuDebugOn(IS_ROOT(dentry));
19648 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19649 +               if (unlikely(err))
19650 +                       goto out;
19651 +       }
19652 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19653 +       if (unlikely(err < 0))
19654 +               goto out;
19655 +
19656 +       h_path->dentry = a->h_path.dentry;
19657 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19658 +
19659 +out:
19660 +       return err;
19661 +}
19662 +
19663 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19664 +                 struct au_sxattr *arg)
19665 +{
19666 +       int err;
19667 +       struct path h_path;
19668 +       struct super_block *sb;
19669 +       struct au_icpup_args *a;
19670 +       struct inode *h_inode;
19671 +
19672 +       IMustLock(inode);
19673 +
19674 +       err = -ENOMEM;
19675 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19676 +       if (unlikely(!a))
19677 +               goto out;
19678 +
19679 +       sb = dentry->d_sb;
19680 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19681 +       if (unlikely(err))
19682 +               goto out_kfree;
19683 +
19684 +       h_path.dentry = NULL;   /* silence gcc */
19685 +       di_write_lock_child(dentry);
19686 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19687 +       if (unlikely(err))
19688 +               goto out_di;
19689 +
19690 +       inode_unlock(a->h_inode);
19691 +       switch (arg->type) {
19692 +       case AU_XATTR_SET:
19693 +               AuDebugOn(d_is_negative(h_path.dentry));
19694 +               err = vfsub_setxattr(h_path.dentry,
19695 +                                    arg->u.set.name, arg->u.set.value,
19696 +                                    arg->u.set.size, arg->u.set.flags);
19697 +               break;
19698 +       case AU_ACL_SET:
19699 +               err = -EOPNOTSUPP;
19700 +               h_inode = d_inode(h_path.dentry);
19701 +               if (h_inode->i_op->set_acl)
19702 +                       /* this will call posix_acl_update_mode */
19703 +                       err = h_inode->i_op->set_acl(h_inode,
19704 +                                                    arg->u.acl_set.acl,
19705 +                                                    arg->u.acl_set.type);
19706 +               break;
19707 +       }
19708 +       if (!err)
19709 +               au_cpup_attr_timesizes(inode);
19710 +
19711 +       au_unpin(&a->pin);
19712 +       if (unlikely(err))
19713 +               au_update_dbtop(dentry);
19714 +
19715 +out_di:
19716 +       di_write_unlock(dentry);
19717 +       si_read_unlock(sb);
19718 +out_kfree:
19719 +       kfree(a);
19720 +out:
19721 +       AuTraceErr(err);
19722 +       return err;
19723 +}
19724 +#endif
19725 +
19726 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19727 +                            unsigned int nlink)
19728 +{
19729 +       unsigned int n;
19730 +
19731 +       inode->i_mode = st->mode;
19732 +       /* don't i_[ug]id_write() here */
19733 +       inode->i_uid = st->uid;
19734 +       inode->i_gid = st->gid;
19735 +       inode->i_atime = st->atime;
19736 +       inode->i_mtime = st->mtime;
19737 +       inode->i_ctime = st->ctime;
19738 +
19739 +       au_cpup_attr_nlink(inode, /*force*/0);
19740 +       if (S_ISDIR(inode->i_mode)) {
19741 +               n = inode->i_nlink;
19742 +               n -= nlink;
19743 +               n += st->nlink;
19744 +               smp_mb(); /* for i_nlink */
19745 +               /* 0 can happen */
19746 +               set_nlink(inode, n);
19747 +       }
19748 +
19749 +       spin_lock(&inode->i_lock);
19750 +       inode->i_blocks = st->blocks;
19751 +       i_size_write(inode, st->size);
19752 +       spin_unlock(&inode->i_lock);
19753 +}
19754 +
19755 +/*
19756 + * common routine for aufs_getattr() and au_getxattr().
19757 + * returns zero or negative (an error).
19758 + * @dentry will be read-locked in success.
19759 + */
19760 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19761 +                     int locked)
19762 +{
19763 +       int err;
19764 +       unsigned int mnt_flags, sigen;
19765 +       unsigned char udba_none;
19766 +       aufs_bindex_t bindex;
19767 +       struct super_block *sb, *h_sb;
19768 +       struct inode *inode;
19769 +
19770 +       h_path->mnt = NULL;
19771 +       h_path->dentry = NULL;
19772 +
19773 +       err = 0;
19774 +       sb = dentry->d_sb;
19775 +       mnt_flags = au_mntflags(sb);
19776 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19777 +
19778 +       if (unlikely(locked))
19779 +               goto body; /* skip locking dinfo */
19780 +
19781 +       /* support fstat(2) */
19782 +       if (!d_unlinked(dentry) && !udba_none) {
19783 +               sigen = au_sigen(sb);
19784 +               err = au_digen_test(dentry, sigen);
19785 +               if (!err) {
19786 +                       di_read_lock_child(dentry, AuLock_IR);
19787 +                       err = au_dbrange_test(dentry);
19788 +                       if (unlikely(err)) {
19789 +                               di_read_unlock(dentry, AuLock_IR);
19790 +                               goto out;
19791 +                       }
19792 +               } else {
19793 +                       AuDebugOn(IS_ROOT(dentry));
19794 +                       di_write_lock_child(dentry);
19795 +                       err = au_dbrange_test(dentry);
19796 +                       if (!err)
19797 +                               err = au_reval_for_attr(dentry, sigen);
19798 +                       if (!err)
19799 +                               di_downgrade_lock(dentry, AuLock_IR);
19800 +                       else {
19801 +                               di_write_unlock(dentry);
19802 +                               goto out;
19803 +                       }
19804 +               }
19805 +       } else
19806 +               di_read_lock_child(dentry, AuLock_IR);
19807 +
19808 +body:
19809 +       inode = d_inode(dentry);
19810 +       bindex = au_ibtop(inode);
19811 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19812 +       h_sb = h_path->mnt->mnt_sb;
19813 +       if (!force
19814 +           && !au_test_fs_bad_iattr(h_sb)
19815 +           && udba_none)
19816 +               goto out; /* success */
19817 +
19818 +       if (au_dbtop(dentry) == bindex)
19819 +               h_path->dentry = au_h_dptr(dentry, bindex);
19820 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19821 +               h_path->dentry = au_plink_lkup(inode, bindex);
19822 +               if (IS_ERR(h_path->dentry))
19823 +                       /* pretending success */
19824 +                       h_path->dentry = NULL;
19825 +               else
19826 +                       dput(h_path->dentry);
19827 +       }
19828 +
19829 +out:
19830 +       return err;
19831 +}
19832 +
19833 +static int aufs_getattr(const struct path *path, struct kstat *st,
19834 +                       u32 request, unsigned int query)
19835 +{
19836 +       int err;
19837 +       unsigned char positive;
19838 +       struct path h_path;
19839 +       struct dentry *dentry;
19840 +       struct inode *inode;
19841 +       struct super_block *sb;
19842 +
19843 +       dentry = path->dentry;
19844 +       inode = d_inode(dentry);
19845 +       sb = dentry->d_sb;
19846 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19847 +       if (unlikely(err))
19848 +               goto out;
19849 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
19850 +       if (unlikely(err))
19851 +               goto out_si;
19852 +       if (unlikely(!h_path.dentry))
19853 +               /* illegally overlapped or something */
19854 +               goto out_fill; /* pretending success */
19855 +
19856 +       positive = d_is_positive(h_path.dentry);
19857 +       if (positive)
19858 +               /* no vfsub version */
19859 +               err = vfs_getattr(&h_path, st, request, query);
19860 +       if (!err) {
19861 +               if (positive)
19862 +                       au_refresh_iattr(inode, st,
19863 +                                        d_inode(h_path.dentry)->i_nlink);
19864 +               goto out_fill; /* success */
19865 +       }
19866 +       AuTraceErr(err);
19867 +       goto out_di;
19868 +
19869 +out_fill:
19870 +       generic_fillattr(inode, st);
19871 +out_di:
19872 +       di_read_unlock(dentry, AuLock_IR);
19873 +out_si:
19874 +       si_read_unlock(sb);
19875 +out:
19876 +       AuTraceErr(err);
19877 +       return err;
19878 +}
19879 +
19880 +/* ---------------------------------------------------------------------- */
19881 +
19882 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
19883 +                                struct delayed_call *done)
19884 +{
19885 +       const char *ret;
19886 +       struct dentry *h_dentry;
19887 +       struct inode *h_inode;
19888 +       int err;
19889 +       aufs_bindex_t bindex;
19890 +
19891 +       ret = NULL; /* suppress a warning */
19892 +       err = -ECHILD;
19893 +       if (!dentry)
19894 +               goto out;
19895 +
19896 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19897 +       if (unlikely(err))
19898 +               goto out;
19899 +
19900 +       err = au_d_hashed_positive(dentry);
19901 +       if (unlikely(err))
19902 +               goto out_unlock;
19903 +
19904 +       err = -EINVAL;
19905 +       inode = d_inode(dentry);
19906 +       bindex = au_ibtop(inode);
19907 +       h_inode = au_h_iptr(inode, bindex);
19908 +       if (unlikely(!h_inode->i_op->get_link))
19909 +               goto out_unlock;
19910 +
19911 +       err = -EBUSY;
19912 +       h_dentry = NULL;
19913 +       if (au_dbtop(dentry) <= bindex) {
19914 +               h_dentry = au_h_dptr(dentry, bindex);
19915 +               if (h_dentry)
19916 +                       dget(h_dentry);
19917 +       }
19918 +       if (!h_dentry) {
19919 +               h_dentry = d_find_any_alias(h_inode);
19920 +               if (IS_ERR(h_dentry)) {
19921 +                       err = PTR_ERR(h_dentry);
19922 +                       goto out_unlock;
19923 +               }
19924 +       }
19925 +       if (unlikely(!h_dentry))
19926 +               goto out_unlock;
19927 +
19928 +       err = 0;
19929 +       AuDbg("%pf\n", h_inode->i_op->get_link);
19930 +       AuDbgDentry(h_dentry);
19931 +       ret = vfs_get_link(h_dentry, done);
19932 +       dput(h_dentry);
19933 +       if (IS_ERR(ret))
19934 +               err = PTR_ERR(ret);
19935 +
19936 +out_unlock:
19937 +       aufs_read_unlock(dentry, AuLock_IR);
19938 +out:
19939 +       if (unlikely(err))
19940 +               ret = ERR_PTR(err);
19941 +       AuTraceErrPtr(ret);
19942 +       return ret;
19943 +}
19944 +
19945 +/* ---------------------------------------------------------------------- */
19946 +
19947 +static int au_is_special(struct inode *inode)
19948 +{
19949 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
19950 +}
19951 +
19952 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19953 +{
19954 +       int err;
19955 +       aufs_bindex_t bindex;
19956 +       struct super_block *sb;
19957 +       struct inode *h_inode;
19958 +       struct vfsmount *h_mnt;
19959 +
19960 +       sb = inode->i_sb;
19961 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
19962 +                 "unexpected s_flags 0x%lx", sb->s_flags);
19963 +
19964 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19965 +       lockdep_off();
19966 +       si_read_lock(sb, AuLock_FLUSH);
19967 +       ii_write_lock_child(inode);
19968 +       lockdep_on();
19969 +
19970 +       err = 0;
19971 +       bindex = au_ibtop(inode);
19972 +       h_inode = au_h_iptr(inode, bindex);
19973 +       if (!au_test_ro(sb, bindex, inode)) {
19974 +               h_mnt = au_sbr_mnt(sb, bindex);
19975 +               err = vfsub_mnt_want_write(h_mnt);
19976 +               if (!err) {
19977 +                       err = vfsub_update_time(h_inode, ts, flags);
19978 +                       vfsub_mnt_drop_write(h_mnt);
19979 +               }
19980 +       } else if (au_is_special(h_inode)) {
19981 +               /*
19982 +                * Never copy-up here.
19983 +                * These special files may already be opened and used for
19984 +                * communicating. If we copied it up, then the communication
19985 +                * would be corrupted.
19986 +                */
19987 +               AuWarn1("timestamps for i%lu are ignored "
19988 +                       "since it is on readonly branch (hi%lu).\n",
19989 +                       inode->i_ino, h_inode->i_ino);
19990 +       } else if (flags & ~S_ATIME) {
19991 +               err = -EIO;
19992 +               AuIOErr1("unexpected flags 0x%x\n", flags);
19993 +               AuDebugOn(1);
19994 +       }
19995 +
19996 +       lockdep_off();
19997 +       if (!err)
19998 +               au_cpup_attr_timesizes(inode);
19999 +       ii_write_unlock(inode);
20000 +       si_read_unlock(sb);
20001 +       lockdep_on();
20002 +
20003 +       if (!err && (flags & S_VERSION))
20004 +               inode_inc_iversion(inode);
20005 +
20006 +       return err;
20007 +}
20008 +
20009 +/* ---------------------------------------------------------------------- */
20010 +
20011 +/* no getattr version will be set by module.c:aufs_init() */
20012 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
20013 +       aufs_iop[] = {
20014 +       [AuIop_SYMLINK] = {
20015 +               .permission     = aufs_permission,
20016 +#ifdef CONFIG_FS_POSIX_ACL
20017 +               .get_acl        = aufs_get_acl,
20018 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
20019 +#endif
20020 +
20021 +               .setattr        = aufs_setattr,
20022 +               .getattr        = aufs_getattr,
20023 +
20024 +#ifdef CONFIG_AUFS_XATTR
20025 +               .listxattr      = aufs_listxattr,
20026 +#endif
20027 +
20028 +               .get_link       = aufs_get_link,
20029 +
20030 +               /* .update_time = aufs_update_time */
20031 +       },
20032 +       [AuIop_DIR] = {
20033 +               .create         = aufs_create,
20034 +               .lookup         = aufs_lookup,
20035 +               .link           = aufs_link,
20036 +               .unlink         = aufs_unlink,
20037 +               .symlink        = aufs_symlink,
20038 +               .mkdir          = aufs_mkdir,
20039 +               .rmdir          = aufs_rmdir,
20040 +               .mknod          = aufs_mknod,
20041 +               .rename         = aufs_rename,
20042 +
20043 +               .permission     = aufs_permission,
20044 +#ifdef CONFIG_FS_POSIX_ACL
20045 +               .get_acl        = aufs_get_acl,
20046 +               .set_acl        = aufs_set_acl,
20047 +#endif
20048 +
20049 +               .setattr        = aufs_setattr,
20050 +               .getattr        = aufs_getattr,
20051 +
20052 +#ifdef CONFIG_AUFS_XATTR
20053 +               .listxattr      = aufs_listxattr,
20054 +#endif
20055 +
20056 +               .update_time    = aufs_update_time,
20057 +               .atomic_open    = aufs_atomic_open,
20058 +               .tmpfile        = aufs_tmpfile
20059 +       },
20060 +       [AuIop_OTHER] = {
20061 +               .permission     = aufs_permission,
20062 +#ifdef CONFIG_FS_POSIX_ACL
20063 +               .get_acl        = aufs_get_acl,
20064 +               .set_acl        = aufs_set_acl,
20065 +#endif
20066 +
20067 +               .setattr        = aufs_setattr,
20068 +               .getattr        = aufs_getattr,
20069 +
20070 +#ifdef CONFIG_AUFS_XATTR
20071 +               .listxattr      = aufs_listxattr,
20072 +#endif
20073 +
20074 +               .update_time    = aufs_update_time
20075 +       }
20076 +};
20077 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
20078 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
20079 +++ linux/fs/aufs/i_op_del.c    2017-07-29 12:14:25.903042072 +0200
20080 @@ -0,0 +1,511 @@
20081 +/*
20082 + * Copyright (C) 2005-2017 Junjiro R. Okajima
20083 + *
20084 + * This program, aufs is free software; you can redistribute it and/or modify
20085 + * it under the terms of the GNU General Public License as published by
20086 + * the Free Software Foundation; either version 2 of the License, or
20087 + * (at your option) any later version.
20088 + *
20089 + * This program is distributed in the hope that it will be useful,
20090 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20091 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20092 + * GNU General Public License for more details.
20093 + *
20094 + * You should have received a copy of the GNU General Public License
20095 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20096 + */
20097 +
20098 +/*
20099 + * inode operations (del entry)
20100 + */
20101 +
20102 +#include "aufs.h"
20103 +
20104 +/*
20105 + * decide if a new whiteout for @dentry is necessary or not.
20106 + * when it is necessary, prepare the parent dir for the upper branch whose
20107 + * branch index is @bcpup for creation. the actual creation of the whiteout will
20108 + * be done by caller.
20109 + * return value:
20110 + * 0: wh is unnecessary
20111 + * plus: wh is necessary
20112 + * minus: error
20113 + */
20114 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
20115 +{
20116 +       int need_wh, err;
20117 +       aufs_bindex_t btop;
20118 +       struct super_block *sb;
20119 +
20120 +       sb = dentry->d_sb;
20121 +       btop = au_dbtop(dentry);
20122 +       if (*bcpup < 0) {
20123 +               *bcpup = btop;
20124 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
20125 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
20126 +                       *bcpup = err;
20127 +                       if (unlikely(err < 0))
20128 +                               goto out;
20129 +               }
20130 +       } else
20131 +               AuDebugOn(btop < *bcpup
20132 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
20133 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
20134 +
20135 +       if (*bcpup != btop) {
20136 +               err = au_cpup_dirs(dentry, *bcpup);
20137 +               if (unlikely(err))
20138 +                       goto out;
20139 +               need_wh = 1;
20140 +       } else {
20141 +               struct au_dinfo *dinfo, *tmp;
20142 +
20143 +               need_wh = -ENOMEM;
20144 +               dinfo = au_di(dentry);
20145 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
20146 +               if (tmp) {
20147 +                       au_di_cp(tmp, dinfo);
20148 +                       au_di_swap(tmp, dinfo);
20149 +                       /* returns the number of positive dentries */
20150 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
20151 +                                                /* AuLkup_IGNORE_PERM */ 0);
20152 +                       au_di_swap(tmp, dinfo);
20153 +                       au_rw_write_unlock(&tmp->di_rwsem);
20154 +                       au_di_free(tmp);
20155 +               }
20156 +       }
20157 +       AuDbg("need_wh %d\n", need_wh);
20158 +       err = need_wh;
20159 +
20160 +out:
20161 +       return err;
20162 +}
20163 +
20164 +/*
20165 + * simple tests for the del-entry operations.
20166 + * following the checks in vfs, plus the parent-child relationship.
20167 + */
20168 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20169 +              struct dentry *h_parent, int isdir)
20170 +{
20171 +       int err;
20172 +       umode_t h_mode;
20173 +       struct dentry *h_dentry, *h_latest;
20174 +       struct inode *h_inode;
20175 +
20176 +       h_dentry = au_h_dptr(dentry, bindex);
20177 +       if (d_really_is_positive(dentry)) {
20178 +               err = -ENOENT;
20179 +               if (unlikely(d_is_negative(h_dentry)))
20180 +                       goto out;
20181 +               h_inode = d_inode(h_dentry);
20182 +               if (unlikely(!h_inode->i_nlink))
20183 +                       goto out;
20184 +
20185 +               h_mode = h_inode->i_mode;
20186 +               if (!isdir) {
20187 +                       err = -EISDIR;
20188 +                       if (unlikely(S_ISDIR(h_mode)))
20189 +                               goto out;
20190 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20191 +                       err = -ENOTDIR;
20192 +                       goto out;
20193 +               }
20194 +       } else {
20195 +               /* rename(2) case */
20196 +               err = -EIO;
20197 +               if (unlikely(d_is_positive(h_dentry)))
20198 +                       goto out;
20199 +       }
20200 +
20201 +       err = -ENOENT;
20202 +       /* expected parent dir is locked */
20203 +       if (unlikely(h_parent != h_dentry->d_parent))
20204 +               goto out;
20205 +       err = 0;
20206 +
20207 +       /*
20208 +        * rmdir a dir may break the consistency on some filesystem.
20209 +        * let's try heavy test.
20210 +        */
20211 +       err = -EACCES;
20212 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
20213 +                    && au_test_h_perm(d_inode(h_parent),
20214 +                                      MAY_EXEC | MAY_WRITE)))
20215 +               goto out;
20216 +
20217 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
20218 +       err = -EIO;
20219 +       if (IS_ERR(h_latest))
20220 +               goto out;
20221 +       if (h_latest == h_dentry)
20222 +               err = 0;
20223 +       dput(h_latest);
20224 +
20225 +out:
20226 +       return err;
20227 +}
20228 +
20229 +/*
20230 + * decide the branch where we operate for @dentry. the branch index will be set
20231 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
20232 + * dir for reverting.
20233 + * when a new whiteout is necessary, create it.
20234 + */
20235 +static struct dentry*
20236 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
20237 +                   struct au_dtime *dt, struct au_pin *pin)
20238 +{
20239 +       struct dentry *wh_dentry;
20240 +       struct super_block *sb;
20241 +       struct path h_path;
20242 +       int err, need_wh;
20243 +       unsigned int udba;
20244 +       aufs_bindex_t bcpup;
20245 +
20246 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
20247 +       wh_dentry = ERR_PTR(need_wh);
20248 +       if (unlikely(need_wh < 0))
20249 +               goto out;
20250 +
20251 +       sb = dentry->d_sb;
20252 +       udba = au_opt_udba(sb);
20253 +       bcpup = *rbcpup;
20254 +       err = au_pin(pin, dentry, bcpup, udba,
20255 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20256 +       wh_dentry = ERR_PTR(err);
20257 +       if (unlikely(err))
20258 +               goto out;
20259 +
20260 +       h_path.dentry = au_pinned_h_parent(pin);
20261 +       if (udba != AuOpt_UDBA_NONE
20262 +           && au_dbtop(dentry) == bcpup) {
20263 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
20264 +               wh_dentry = ERR_PTR(err);
20265 +               if (unlikely(err))
20266 +                       goto out_unpin;
20267 +       }
20268 +
20269 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
20270 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
20271 +       wh_dentry = NULL;
20272 +       if (!need_wh)
20273 +               goto out; /* success, no need to create whiteout */
20274 +
20275 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
20276 +       if (IS_ERR(wh_dentry))
20277 +               goto out_unpin;
20278 +
20279 +       /* returns with the parent is locked and wh_dentry is dget-ed */
20280 +       goto out; /* success */
20281 +
20282 +out_unpin:
20283 +       au_unpin(pin);
20284 +out:
20285 +       return wh_dentry;
20286 +}
20287 +
20288 +/*
20289 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
20290 + * in order to be revertible and save time for removing many child whiteouts
20291 + * under the dir.
20292 + * returns 1 when there are too many child whiteout and caller should remove
20293 + * them asynchronously. returns 0 when the number of children is enough small to
20294 + * remove now or the branch fs is a remote fs.
20295 + * otherwise return an error.
20296 + */
20297 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
20298 +                          struct au_nhash *whlist, struct inode *dir)
20299 +{
20300 +       int rmdir_later, err, dirwh;
20301 +       struct dentry *h_dentry;
20302 +       struct super_block *sb;
20303 +       struct inode *inode;
20304 +
20305 +       sb = dentry->d_sb;
20306 +       SiMustAnyLock(sb);
20307 +       h_dentry = au_h_dptr(dentry, bindex);
20308 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
20309 +       if (unlikely(err))
20310 +               goto out;
20311 +
20312 +       /* stop monitoring */
20313 +       inode = d_inode(dentry);
20314 +       au_hn_free(au_hi(inode, bindex));
20315 +
20316 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
20317 +               dirwh = au_sbi(sb)->si_dirwh;
20318 +               rmdir_later = (dirwh <= 1);
20319 +               if (!rmdir_later)
20320 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
20321 +                                                             dirwh);
20322 +               if (rmdir_later)
20323 +                       return rmdir_later;
20324 +       }
20325 +
20326 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
20327 +       if (unlikely(err)) {
20328 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
20329 +                       h_dentry, bindex, err);
20330 +               err = 0;
20331 +       }
20332 +
20333 +out:
20334 +       AuTraceErr(err);
20335 +       return err;
20336 +}
20337 +
20338 +/*
20339 + * final procedure for deleting a entry.
20340 + * maintain dentry and iattr.
20341 + */
20342 +static void epilog(struct inode *dir, struct dentry *dentry,
20343 +                  aufs_bindex_t bindex)
20344 +{
20345 +       struct inode *inode;
20346 +
20347 +       inode = d_inode(dentry);
20348 +       d_drop(dentry);
20349 +       inode->i_ctime = dir->i_ctime;
20350 +
20351 +       au_dir_ts(dir, bindex);
20352 +       dir->i_version++;
20353 +}
20354 +
20355 +/*
20356 + * when an error happened, remove the created whiteout and revert everything.
20357 + */
20358 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
20359 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
20360 +                    struct dentry *dentry, struct au_dtime *dt)
20361 +{
20362 +       int rerr;
20363 +       struct path h_path = {
20364 +               .dentry = wh_dentry,
20365 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
20366 +       };
20367 +
20368 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
20369 +       if (!rerr) {
20370 +               au_set_dbwh(dentry, bwh);
20371 +               au_dtime_revert(dt);
20372 +               return 0;
20373 +       }
20374 +
20375 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
20376 +       return -EIO;
20377 +}
20378 +
20379 +/* ---------------------------------------------------------------------- */
20380 +
20381 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
20382 +{
20383 +       int err;
20384 +       aufs_bindex_t bwh, bindex, btop;
20385 +       struct inode *inode, *h_dir, *delegated;
20386 +       struct dentry *parent, *wh_dentry;
20387 +       /* to reuduce stack size */
20388 +       struct {
20389 +               struct au_dtime dt;
20390 +               struct au_pin pin;
20391 +               struct path h_path;
20392 +       } *a;
20393 +
20394 +       IMustLock(dir);
20395 +
20396 +       err = -ENOMEM;
20397 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20398 +       if (unlikely(!a))
20399 +               goto out;
20400 +
20401 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20402 +       if (unlikely(err))
20403 +               goto out_free;
20404 +       err = au_d_hashed_positive(dentry);
20405 +       if (unlikely(err))
20406 +               goto out_unlock;
20407 +       inode = d_inode(dentry);
20408 +       IMustLock(inode);
20409 +       err = -EISDIR;
20410 +       if (unlikely(d_is_dir(dentry)))
20411 +               goto out_unlock; /* possible? */
20412 +
20413 +       btop = au_dbtop(dentry);
20414 +       bwh = au_dbwh(dentry);
20415 +       bindex = -1;
20416 +       parent = dentry->d_parent; /* dir inode is locked */
20417 +       di_write_lock_parent(parent);
20418 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
20419 +                                       &a->pin);
20420 +       err = PTR_ERR(wh_dentry);
20421 +       if (IS_ERR(wh_dentry))
20422 +               goto out_parent;
20423 +
20424 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
20425 +       a->h_path.dentry = au_h_dptr(dentry, btop);
20426 +       dget(a->h_path.dentry);
20427 +       if (bindex == btop) {
20428 +               h_dir = au_pinned_h_dir(&a->pin);
20429 +               delegated = NULL;
20430 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
20431 +               if (unlikely(err == -EWOULDBLOCK)) {
20432 +                       pr_warn("cannot retry for NFSv4 delegation"
20433 +                               " for an internal unlink\n");
20434 +                       iput(delegated);
20435 +               }
20436 +       } else {
20437 +               /* dir inode is locked */
20438 +               h_dir = d_inode(wh_dentry->d_parent);
20439 +               IMustLock(h_dir);
20440 +               err = 0;
20441 +       }
20442 +
20443 +       if (!err) {
20444 +               vfsub_drop_nlink(inode);
20445 +               epilog(dir, dentry, bindex);
20446 +
20447 +               /* update target timestamps */
20448 +               if (bindex == btop) {
20449 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
20450 +                       /*ignore*/
20451 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20452 +               } else
20453 +                       /* todo: this timestamp may be reverted later */
20454 +                       inode->i_ctime = h_dir->i_ctime;
20455 +               goto out_unpin; /* success */
20456 +       }
20457 +
20458 +       /* revert */
20459 +       if (wh_dentry) {
20460 +               int rerr;
20461 +
20462 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20463 +                                &a->dt);
20464 +               if (rerr)
20465 +                       err = rerr;
20466 +       }
20467 +
20468 +out_unpin:
20469 +       au_unpin(&a->pin);
20470 +       dput(wh_dentry);
20471 +       dput(a->h_path.dentry);
20472 +out_parent:
20473 +       di_write_unlock(parent);
20474 +out_unlock:
20475 +       aufs_read_unlock(dentry, AuLock_DW);
20476 +out_free:
20477 +       kfree(a);
20478 +out:
20479 +       return err;
20480 +}
20481 +
20482 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
20483 +{
20484 +       int err, rmdir_later;
20485 +       aufs_bindex_t bwh, bindex, btop;
20486 +       struct inode *inode;
20487 +       struct dentry *parent, *wh_dentry, *h_dentry;
20488 +       struct au_whtmp_rmdir *args;
20489 +       /* to reuduce stack size */
20490 +       struct {
20491 +               struct au_dtime dt;
20492 +               struct au_pin pin;
20493 +       } *a;
20494 +
20495 +       IMustLock(dir);
20496 +
20497 +       err = -ENOMEM;
20498 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20499 +       if (unlikely(!a))
20500 +               goto out;
20501 +
20502 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20503 +       if (unlikely(err))
20504 +               goto out_free;
20505 +       err = au_alive_dir(dentry);
20506 +       if (unlikely(err))
20507 +               goto out_unlock;
20508 +       inode = d_inode(dentry);
20509 +       IMustLock(inode);
20510 +       err = -ENOTDIR;
20511 +       if (unlikely(!d_is_dir(dentry)))
20512 +               goto out_unlock; /* possible? */
20513 +
20514 +       err = -ENOMEM;
20515 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20516 +       if (unlikely(!args))
20517 +               goto out_unlock;
20518 +
20519 +       parent = dentry->d_parent; /* dir inode is locked */
20520 +       di_write_lock_parent(parent);
20521 +       err = au_test_empty(dentry, &args->whlist);
20522 +       if (unlikely(err))
20523 +               goto out_parent;
20524 +
20525 +       btop = au_dbtop(dentry);
20526 +       bwh = au_dbwh(dentry);
20527 +       bindex = -1;
20528 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20529 +                                       &a->pin);
20530 +       err = PTR_ERR(wh_dentry);
20531 +       if (IS_ERR(wh_dentry))
20532 +               goto out_parent;
20533 +
20534 +       h_dentry = au_h_dptr(dentry, btop);
20535 +       dget(h_dentry);
20536 +       rmdir_later = 0;
20537 +       if (bindex == btop) {
20538 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
20539 +               if (err > 0) {
20540 +                       rmdir_later = err;
20541 +                       err = 0;
20542 +               }
20543 +       } else {
20544 +               /* stop monitoring */
20545 +               au_hn_free(au_hi(inode, btop));
20546 +
20547 +               /* dir inode is locked */
20548 +               IMustLock(d_inode(wh_dentry->d_parent));
20549 +               err = 0;
20550 +       }
20551 +
20552 +       if (!err) {
20553 +               vfsub_dead_dir(inode);
20554 +               au_set_dbdiropq(dentry, -1);
20555 +               epilog(dir, dentry, bindex);
20556 +
20557 +               if (rmdir_later) {
20558 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
20559 +                       args = NULL;
20560 +               }
20561 +
20562 +               goto out_unpin; /* success */
20563 +       }
20564 +
20565 +       /* revert */
20566 +       AuLabel(revert);
20567 +       if (wh_dentry) {
20568 +               int rerr;
20569 +
20570 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20571 +                                &a->dt);
20572 +               if (rerr)
20573 +                       err = rerr;
20574 +       }
20575 +
20576 +out_unpin:
20577 +       au_unpin(&a->pin);
20578 +       dput(wh_dentry);
20579 +       dput(h_dentry);
20580 +out_parent:
20581 +       di_write_unlock(parent);
20582 +       if (args)
20583 +               au_whtmp_rmdir_free(args);
20584 +out_unlock:
20585 +       aufs_read_unlock(dentry, AuLock_DW);
20586 +out_free:
20587 +       kfree(a);
20588 +out:
20589 +       AuTraceErr(err);
20590 +       return err;
20591 +}
20592 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20593 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20594 +++ linux/fs/aufs/i_op_ren.c    2017-07-29 12:14:25.903042072 +0200
20595 @@ -0,0 +1,1165 @@
20596 +/*
20597 + * Copyright (C) 2005-2017 Junjiro R. Okajima
20598 + *
20599 + * This program, aufs is free software; you can redistribute it and/or modify
20600 + * it under the terms of the GNU General Public License as published by
20601 + * the Free Software Foundation; either version 2 of the License, or
20602 + * (at your option) any later version.
20603 + *
20604 + * This program is distributed in the hope that it will be useful,
20605 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20606 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20607 + * GNU General Public License for more details.
20608 + *
20609 + * You should have received a copy of the GNU General Public License
20610 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20611 + */
20612 +
20613 +/*
20614 + * inode operation (rename entry)
20615 + * todo: this is crazy monster
20616 + */
20617 +
20618 +#include "aufs.h"
20619 +
20620 +enum { AuSRC, AuDST, AuSrcDst };
20621 +enum { AuPARENT, AuCHILD, AuParentChild };
20622 +
20623 +#define AuRen_ISDIR_SRC                1
20624 +#define AuRen_ISDIR_DST                (1 << 1)
20625 +#define AuRen_ISSAMEDIR                (1 << 2)
20626 +#define AuRen_WHSRC            (1 << 3)
20627 +#define AuRen_WHDST            (1 << 4)
20628 +#define AuRen_MNT_WRITE                (1 << 5)
20629 +#define AuRen_DT_DSTDIR                (1 << 6)
20630 +#define AuRen_DIROPQ_SRC       (1 << 7)
20631 +#define AuRen_DIROPQ_DST       (1 << 8)
20632 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20633 +#define au_fset_ren(flags, name) \
20634 +       do { (flags) |= AuRen_##name; } while (0)
20635 +#define au_fclr_ren(flags, name) \
20636 +       do { (flags) &= ~AuRen_##name; } while (0)
20637 +
20638 +struct au_ren_args {
20639 +       struct {
20640 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20641 +                       *wh_dentry;
20642 +               struct inode *dir, *inode;
20643 +               struct au_hinode *hdir, *hinode;
20644 +               struct au_dtime dt[AuParentChild];
20645 +               aufs_bindex_t btop, bdiropq;
20646 +       } sd[AuSrcDst];
20647 +
20648 +#define src_dentry     sd[AuSRC].dentry
20649 +#define src_dir                sd[AuSRC].dir
20650 +#define src_inode      sd[AuSRC].inode
20651 +#define src_h_dentry   sd[AuSRC].h_dentry
20652 +#define src_parent     sd[AuSRC].parent
20653 +#define src_h_parent   sd[AuSRC].h_parent
20654 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20655 +#define src_hdir       sd[AuSRC].hdir
20656 +#define src_hinode     sd[AuSRC].hinode
20657 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20658 +#define src_dt         sd[AuSRC].dt
20659 +#define src_btop       sd[AuSRC].btop
20660 +#define src_bdiropq    sd[AuSRC].bdiropq
20661 +
20662 +#define dst_dentry     sd[AuDST].dentry
20663 +#define dst_dir                sd[AuDST].dir
20664 +#define dst_inode      sd[AuDST].inode
20665 +#define dst_h_dentry   sd[AuDST].h_dentry
20666 +#define dst_parent     sd[AuDST].parent
20667 +#define dst_h_parent   sd[AuDST].h_parent
20668 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20669 +#define dst_hdir       sd[AuDST].hdir
20670 +#define dst_hinode     sd[AuDST].hinode
20671 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20672 +#define dst_dt         sd[AuDST].dt
20673 +#define dst_btop       sd[AuDST].btop
20674 +#define dst_bdiropq    sd[AuDST].bdiropq
20675 +
20676 +       struct dentry *h_trap;
20677 +       struct au_branch *br;
20678 +       struct path h_path;
20679 +       struct au_nhash whlist;
20680 +       aufs_bindex_t btgt, src_bwh;
20681 +
20682 +       struct {
20683 +               unsigned short auren_flags;
20684 +               unsigned char flags;    /* syscall parameter */
20685 +               unsigned char exchange;
20686 +       } __packed;
20687 +
20688 +       struct au_whtmp_rmdir *thargs;
20689 +       struct dentry *h_dst;
20690 +};
20691 +
20692 +/* ---------------------------------------------------------------------- */
20693 +
20694 +/*
20695 + * functions for reverting.
20696 + * when an error happened in a single rename systemcall, we should revert
20697 + * everything as if nothing happened.
20698 + * we don't need to revert the copied-up/down the parent dir since they are
20699 + * harmless.
20700 + */
20701 +
20702 +#define RevertFailure(fmt, ...) do { \
20703 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20704 +               ##__VA_ARGS__, err, rerr); \
20705 +       err = -EIO; \
20706 +} while (0)
20707 +
20708 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
20709 +{
20710 +       int rerr;
20711 +       struct dentry *d;
20712 +#define src_or_dst(member) a->sd[idx].member
20713 +
20714 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
20715 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
20716 +       rerr = au_diropq_remove(d, a->btgt);
20717 +       au_hn_inode_unlock(src_or_dst(hinode));
20718 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
20719 +       if (rerr)
20720 +               RevertFailure("remove diropq %pd", d);
20721 +
20722 +#undef src_or_dst_
20723 +}
20724 +
20725 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20726 +{
20727 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
20728 +               au_ren_do_rev_diropq(err, a, AuSRC);
20729 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
20730 +               au_ren_do_rev_diropq(err, a, AuDST);
20731 +}
20732 +
20733 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20734 +{
20735 +       int rerr;
20736 +       struct inode *delegated;
20737 +
20738 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20739 +                                         a->src_h_parent);
20740 +       rerr = PTR_ERR(a->h_path.dentry);
20741 +       if (IS_ERR(a->h_path.dentry)) {
20742 +               RevertFailure("lkup one %pd", a->src_dentry);
20743 +               return;
20744 +       }
20745 +
20746 +       delegated = NULL;
20747 +       rerr = vfsub_rename(a->dst_h_dir,
20748 +                           au_h_dptr(a->src_dentry, a->btgt),
20749 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
20750 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20751 +               pr_warn("cannot retry for NFSv4 delegation"
20752 +                       " for an internal rename\n");
20753 +               iput(delegated);
20754 +       }
20755 +       d_drop(a->h_path.dentry);
20756 +       dput(a->h_path.dentry);
20757 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20758 +       if (rerr)
20759 +               RevertFailure("rename %pd", a->src_dentry);
20760 +}
20761 +
20762 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20763 +{
20764 +       int rerr;
20765 +       struct inode *delegated;
20766 +
20767 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20768 +                                         a->dst_h_parent);
20769 +       rerr = PTR_ERR(a->h_path.dentry);
20770 +       if (IS_ERR(a->h_path.dentry)) {
20771 +               RevertFailure("lkup one %pd", a->dst_dentry);
20772 +               return;
20773 +       }
20774 +       if (d_is_positive(a->h_path.dentry)) {
20775 +               d_drop(a->h_path.dentry);
20776 +               dput(a->h_path.dentry);
20777 +               return;
20778 +       }
20779 +
20780 +       delegated = NULL;
20781 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20782 +                           &delegated, a->flags);
20783 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20784 +               pr_warn("cannot retry for NFSv4 delegation"
20785 +                       " for an internal rename\n");
20786 +               iput(delegated);
20787 +       }
20788 +       d_drop(a->h_path.dentry);
20789 +       dput(a->h_path.dentry);
20790 +       if (!rerr)
20791 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20792 +       else
20793 +               RevertFailure("rename %pd", a->h_dst);
20794 +}
20795 +
20796 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20797 +{
20798 +       int rerr;
20799 +
20800 +       a->h_path.dentry = a->src_wh_dentry;
20801 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20802 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20803 +       if (rerr)
20804 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20805 +}
20806 +#undef RevertFailure
20807 +
20808 +/* ---------------------------------------------------------------------- */
20809 +
20810 +/*
20811 + * when we have to copyup the renaming entry, do it with the rename-target name
20812 + * in order to minimize the cost (the later actual rename is unnecessary).
20813 + * otherwise rename it on the target branch.
20814 + */
20815 +static int au_ren_or_cpup(struct au_ren_args *a)
20816 +{
20817 +       int err;
20818 +       struct dentry *d;
20819 +       struct inode *delegated;
20820 +
20821 +       d = a->src_dentry;
20822 +       if (au_dbtop(d) == a->btgt) {
20823 +               a->h_path.dentry = a->dst_h_dentry;
20824 +               AuDebugOn(au_dbtop(d) != a->btgt);
20825 +               delegated = NULL;
20826 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20827 +                                  a->dst_h_dir, &a->h_path, &delegated,
20828 +                                  a->flags);
20829 +               if (unlikely(err == -EWOULDBLOCK)) {
20830 +                       pr_warn("cannot retry for NFSv4 delegation"
20831 +                               " for an internal rename\n");
20832 +                       iput(delegated);
20833 +               }
20834 +       } else
20835 +               BUG();
20836 +
20837 +       if (!err && a->h_dst)
20838 +               /* it will be set to dinfo later */
20839 +               dget(a->h_dst);
20840 +
20841 +       return err;
20842 +}
20843 +
20844 +/* cf. aufs_rmdir() */
20845 +static int au_ren_del_whtmp(struct au_ren_args *a)
20846 +{
20847 +       int err;
20848 +       struct inode *dir;
20849 +
20850 +       dir = a->dst_dir;
20851 +       SiMustAnyLock(dir->i_sb);
20852 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20853 +                                    au_sbi(dir->i_sb)->si_dirwh)
20854 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20855 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20856 +               if (unlikely(err))
20857 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20858 +                               "ignored.\n", a->h_dst, err);
20859 +       } else {
20860 +               au_nhash_wh_free(&a->thargs->whlist);
20861 +               a->thargs->whlist = a->whlist;
20862 +               a->whlist.nh_num = 0;
20863 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20864 +               dput(a->h_dst);
20865 +               a->thargs = NULL;
20866 +       }
20867 +
20868 +       return 0;
20869 +}
20870 +
20871 +/* make it 'opaque' dir. */
20872 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
20873 +{
20874 +       int err;
20875 +       struct dentry *d, *diropq;
20876 +#define src_or_dst(member) a->sd[idx].member
20877 +
20878 +       err = 0;
20879 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
20880 +       src_or_dst(bdiropq) = au_dbdiropq(d);
20881 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
20882 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
20883 +       diropq = au_diropq_create(d, a->btgt);
20884 +       au_hn_inode_unlock(src_or_dst(hinode));
20885 +       if (IS_ERR(diropq))
20886 +               err = PTR_ERR(diropq);
20887 +       else
20888 +               dput(diropq);
20889 +
20890 +#undef src_or_dst_
20891 +       return err;
20892 +}
20893 +
20894 +static int au_ren_diropq(struct au_ren_args *a)
20895 +{
20896 +       int err;
20897 +       unsigned char always;
20898 +       struct dentry *d;
20899 +
20900 +       err = 0;
20901 +       d = a->dst_dentry; /* already renamed on the branch */
20902 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
20903 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
20904 +           && a->btgt != au_dbdiropq(a->src_dentry)
20905 +           && (a->dst_wh_dentry
20906 +               || a->btgt <= au_dbdiropq(d)
20907 +               /* hide the lower to keep xino */
20908 +               /* the lowers may not be a dir, but we hide them anyway */
20909 +               || a->btgt < au_dbbot(d)
20910 +               || always)) {
20911 +               AuDbg("here\n");
20912 +               err = au_ren_do_diropq(a, AuSRC);
20913 +               if (unlikely(err))
20914 +                       goto out;
20915 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
20916 +       }
20917 +       if (!a->exchange)
20918 +               goto out; /* success */
20919 +
20920 +       d = a->src_dentry; /* already renamed on the branch */
20921 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
20922 +           && a->btgt != au_dbdiropq(a->dst_dentry)
20923 +           && (a->btgt < au_dbdiropq(d)
20924 +               || a->btgt < au_dbbot(d)
20925 +               || always)) {
20926 +               AuDbgDentry(a->src_dentry);
20927 +               AuDbgDentry(a->dst_dentry);
20928 +               err = au_ren_do_diropq(a, AuDST);
20929 +               if (unlikely(err))
20930 +                       goto out_rev_src;
20931 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
20932 +       }
20933 +       goto out; /* success */
20934 +
20935 +out_rev_src:
20936 +       AuDbg("err %d, reverting src\n", err);
20937 +       au_ren_rev_diropq(err, a);
20938 +out:
20939 +       return err;
20940 +}
20941 +
20942 +static int do_rename(struct au_ren_args *a)
20943 +{
20944 +       int err;
20945 +       struct dentry *d, *h_d;
20946 +
20947 +       if (!a->exchange) {
20948 +               /* prepare workqueue args for asynchronous rmdir */
20949 +               h_d = a->dst_h_dentry;
20950 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
20951 +                   && d_is_positive(h_d)) {
20952 +                       err = -ENOMEM;
20953 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
20954 +                                                        GFP_NOFS);
20955 +                       if (unlikely(!a->thargs))
20956 +                               goto out;
20957 +                       a->h_dst = dget(h_d);
20958 +               }
20959 +
20960 +               /* create whiteout for src_dentry */
20961 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
20962 +                       a->src_bwh = au_dbwh(a->src_dentry);
20963 +                       AuDebugOn(a->src_bwh >= 0);
20964 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
20965 +                                                       a->src_h_parent);
20966 +                       err = PTR_ERR(a->src_wh_dentry);
20967 +                       if (IS_ERR(a->src_wh_dentry))
20968 +                               goto out_thargs;
20969 +               }
20970 +
20971 +               /* lookup whiteout for dentry */
20972 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
20973 +                       h_d = au_wh_lkup(a->dst_h_parent,
20974 +                                        &a->dst_dentry->d_name, a->br);
20975 +                       err = PTR_ERR(h_d);
20976 +                       if (IS_ERR(h_d))
20977 +                               goto out_whsrc;
20978 +                       if (d_is_negative(h_d))
20979 +                               dput(h_d);
20980 +                       else
20981 +                               a->dst_wh_dentry = h_d;
20982 +               }
20983 +
20984 +               /* rename dentry to tmpwh */
20985 +               if (a->thargs) {
20986 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
20987 +                       if (unlikely(err))
20988 +                               goto out_whdst;
20989 +
20990 +                       d = a->dst_dentry;
20991 +                       au_set_h_dptr(d, a->btgt, NULL);
20992 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
20993 +                       if (unlikely(err))
20994 +                               goto out_whtmp;
20995 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
20996 +               }
20997 +       }
20998 +
20999 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
21000 +
21001 +       /* rename by vfs_rename or cpup */
21002 +       err = au_ren_or_cpup(a);
21003 +       if (unlikely(err))
21004 +               /* leave the copied-up one */
21005 +               goto out_whtmp;
21006 +
21007 +       /* make dir opaque */
21008 +       err = au_ren_diropq(a);
21009 +       if (unlikely(err))
21010 +               goto out_rename;
21011 +
21012 +       /* update target timestamps */
21013 +       if (a->exchange) {
21014 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
21015 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
21016 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
21017 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
21018 +       }
21019 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
21020 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
21021 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
21022 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
21023 +
21024 +       if (!a->exchange) {
21025 +               /* remove whiteout for dentry */
21026 +               if (a->dst_wh_dentry) {
21027 +                       a->h_path.dentry = a->dst_wh_dentry;
21028 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
21029 +                                                 a->dst_dentry);
21030 +                       if (unlikely(err))
21031 +                               goto out_diropq;
21032 +               }
21033 +
21034 +               /* remove whtmp */
21035 +               if (a->thargs)
21036 +                       au_ren_del_whtmp(a); /* ignore this error */
21037 +
21038 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
21039 +       }
21040 +       err = 0;
21041 +       goto out_success;
21042 +
21043 +out_diropq:
21044 +       au_ren_rev_diropq(err, a);
21045 +out_rename:
21046 +       au_ren_rev_rename(err, a);
21047 +       dput(a->h_dst);
21048 +out_whtmp:
21049 +       if (a->thargs)
21050 +               au_ren_rev_whtmp(err, a);
21051 +out_whdst:
21052 +       dput(a->dst_wh_dentry);
21053 +       a->dst_wh_dentry = NULL;
21054 +out_whsrc:
21055 +       if (a->src_wh_dentry)
21056 +               au_ren_rev_whsrc(err, a);
21057 +out_success:
21058 +       dput(a->src_wh_dentry);
21059 +       dput(a->dst_wh_dentry);
21060 +out_thargs:
21061 +       if (a->thargs) {
21062 +               dput(a->h_dst);
21063 +               au_whtmp_rmdir_free(a->thargs);
21064 +               a->thargs = NULL;
21065 +       }
21066 +out:
21067 +       return err;
21068 +}
21069 +
21070 +/* ---------------------------------------------------------------------- */
21071 +
21072 +/*
21073 + * test if @dentry dir can be rename destination or not.
21074 + * success means, it is a logically empty dir.
21075 + */
21076 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
21077 +{
21078 +       return au_test_empty(dentry, whlist);
21079 +}
21080 +
21081 +/*
21082 + * test if @dentry dir can be rename source or not.
21083 + * if it can, return 0 and @children is filled.
21084 + * success means,
21085 + * - it is a logically empty dir.
21086 + * - or, it exists on writable branch and has no children including whiteouts
21087 + *       on the lower branch.
21088 + */
21089 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
21090 +{
21091 +       int err;
21092 +       unsigned int rdhash;
21093 +       aufs_bindex_t btop;
21094 +
21095 +       btop = au_dbtop(dentry);
21096 +       if (btop != btgt) {
21097 +               struct au_nhash whlist;
21098 +
21099 +               SiMustAnyLock(dentry->d_sb);
21100 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
21101 +               if (!rdhash)
21102 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
21103 +                                                          dentry));
21104 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
21105 +               if (unlikely(err))
21106 +                       goto out;
21107 +               err = au_test_empty(dentry, &whlist);
21108 +               au_nhash_wh_free(&whlist);
21109 +               goto out;
21110 +       }
21111 +
21112 +       if (btop == au_dbtaildir(dentry))
21113 +               return 0; /* success */
21114 +
21115 +       err = au_test_empty_lower(dentry);
21116 +
21117 +out:
21118 +       if (err == -ENOTEMPTY) {
21119 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
21120 +                       " is not supported\n");
21121 +               err = -EXDEV;
21122 +       }
21123 +       return err;
21124 +}
21125 +
21126 +/* side effect: sets whlist and h_dentry */
21127 +static int au_ren_may_dir(struct au_ren_args *a)
21128 +{
21129 +       int err;
21130 +       unsigned int rdhash;
21131 +       struct dentry *d;
21132 +
21133 +       d = a->dst_dentry;
21134 +       SiMustAnyLock(d->d_sb);
21135 +
21136 +       err = 0;
21137 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
21138 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
21139 +               if (!rdhash)
21140 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
21141 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
21142 +               if (unlikely(err))
21143 +                       goto out;
21144 +
21145 +               if (!a->exchange) {
21146 +                       au_set_dbtop(d, a->dst_btop);
21147 +                       err = may_rename_dstdir(d, &a->whlist);
21148 +                       au_set_dbtop(d, a->btgt);
21149 +               } else
21150 +                       err = may_rename_srcdir(d, a->btgt);
21151 +       }
21152 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
21153 +       if (unlikely(err))
21154 +               goto out;
21155 +
21156 +       d = a->src_dentry;
21157 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
21158 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
21159 +               err = may_rename_srcdir(d, a->btgt);
21160 +               if (unlikely(err)) {
21161 +                       au_nhash_wh_free(&a->whlist);
21162 +                       a->whlist.nh_num = 0;
21163 +               }
21164 +       }
21165 +out:
21166 +       return err;
21167 +}
21168 +
21169 +/* ---------------------------------------------------------------------- */
21170 +
21171 +/*
21172 + * simple tests for rename.
21173 + * following the checks in vfs, plus the parent-child relationship.
21174 + */
21175 +static int au_may_ren(struct au_ren_args *a)
21176 +{
21177 +       int err, isdir;
21178 +       struct inode *h_inode;
21179 +
21180 +       if (a->src_btop == a->btgt) {
21181 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
21182 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
21183 +               if (unlikely(err))
21184 +                       goto out;
21185 +               err = -EINVAL;
21186 +               if (unlikely(a->src_h_dentry == a->h_trap))
21187 +                       goto out;
21188 +       }
21189 +
21190 +       err = 0;
21191 +       if (a->dst_btop != a->btgt)
21192 +               goto out;
21193 +
21194 +       err = -ENOTEMPTY;
21195 +       if (unlikely(a->dst_h_dentry == a->h_trap))
21196 +               goto out;
21197 +
21198 +       err = -EIO;
21199 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
21200 +       if (d_really_is_negative(a->dst_dentry)) {
21201 +               if (d_is_negative(a->dst_h_dentry))
21202 +                       err = au_may_add(a->dst_dentry, a->btgt,
21203 +                                        a->dst_h_parent, isdir);
21204 +       } else {
21205 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
21206 +                       goto out;
21207 +               h_inode = d_inode(a->dst_h_dentry);
21208 +               if (h_inode->i_nlink)
21209 +                       err = au_may_del(a->dst_dentry, a->btgt,
21210 +                                        a->dst_h_parent, isdir);
21211 +       }
21212 +
21213 +out:
21214 +       if (unlikely(err == -ENOENT || err == -EEXIST))
21215 +               err = -EIO;
21216 +       AuTraceErr(err);
21217 +       return err;
21218 +}
21219 +
21220 +/* ---------------------------------------------------------------------- */
21221 +
21222 +/*
21223 + * locking order
21224 + * (VFS)
21225 + * - src_dir and dir by lock_rename()
21226 + * - inode if exitsts
21227 + * (aufs)
21228 + * - lock all
21229 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
21230 + *     + si_read_lock
21231 + *     + di_write_lock2_child()
21232 + *       + di_write_lock_child()
21233 + *        + ii_write_lock_child()
21234 + *       + di_write_lock_child2()
21235 + *        + ii_write_lock_child2()
21236 + *     + src_parent and parent
21237 + *       + di_write_lock_parent()
21238 + *        + ii_write_lock_parent()
21239 + *       + di_write_lock_parent2()
21240 + *        + ii_write_lock_parent2()
21241 + *   + lower src_dir and dir by vfsub_lock_rename()
21242 + *   + verify the every relationships between child and parent. if any
21243 + *     of them failed, unlock all and return -EBUSY.
21244 + */
21245 +static void au_ren_unlock(struct au_ren_args *a)
21246 +{
21247 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
21248 +                           a->dst_h_parent, a->dst_hdir);
21249 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
21250 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
21251 +}
21252 +
21253 +static int au_ren_lock(struct au_ren_args *a)
21254 +{
21255 +       int err;
21256 +       unsigned int udba;
21257 +
21258 +       err = 0;
21259 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
21260 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
21261 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
21262 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
21263 +
21264 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
21265 +       if (unlikely(err))
21266 +               goto out;
21267 +       au_fset_ren(a->auren_flags, MNT_WRITE);
21268 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
21269 +                                     a->dst_h_parent, a->dst_hdir);
21270 +       udba = au_opt_udba(a->src_dentry->d_sb);
21271 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
21272 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
21273 +               err = au_busy_or_stale();
21274 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
21275 +               err = au_h_verify(a->src_h_dentry, udba,
21276 +                                 d_inode(a->src_h_parent), a->src_h_parent,
21277 +                                 a->br);
21278 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
21279 +               err = au_h_verify(a->dst_h_dentry, udba,
21280 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
21281 +                                 a->br);
21282 +       if (!err)
21283 +               goto out; /* success */
21284 +
21285 +       err = au_busy_or_stale();
21286 +       au_ren_unlock(a);
21287 +
21288 +out:
21289 +       return err;
21290 +}
21291 +
21292 +/* ---------------------------------------------------------------------- */
21293 +
21294 +static void au_ren_refresh_dir(struct au_ren_args *a)
21295 +{
21296 +       struct inode *dir;
21297 +
21298 +       dir = a->dst_dir;
21299 +       dir->i_version++;
21300 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
21301 +               /* is this updating defined in POSIX? */
21302 +               au_cpup_attr_timesizes(a->src_inode);
21303 +               au_cpup_attr_nlink(dir, /*force*/1);
21304 +       }
21305 +       au_dir_ts(dir, a->btgt);
21306 +
21307 +       if (a->exchange) {
21308 +               dir = a->src_dir;
21309 +               dir->i_version++;
21310 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
21311 +                       /* is this updating defined in POSIX? */
21312 +                       au_cpup_attr_timesizes(a->dst_inode);
21313 +                       au_cpup_attr_nlink(dir, /*force*/1);
21314 +               }
21315 +               au_dir_ts(dir, a->btgt);
21316 +       }
21317 +
21318 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
21319 +               return;
21320 +
21321 +       dir = a->src_dir;
21322 +       dir->i_version++;
21323 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
21324 +               au_cpup_attr_nlink(dir, /*force*/1);
21325 +       au_dir_ts(dir, a->btgt);
21326 +}
21327 +
21328 +static void au_ren_refresh(struct au_ren_args *a)
21329 +{
21330 +       aufs_bindex_t bbot, bindex;
21331 +       struct dentry *d, *h_d;
21332 +       struct inode *i, *h_i;
21333 +       struct super_block *sb;
21334 +
21335 +       d = a->dst_dentry;
21336 +       d_drop(d);
21337 +       if (a->h_dst)
21338 +               /* already dget-ed by au_ren_or_cpup() */
21339 +               au_set_h_dptr(d, a->btgt, a->h_dst);
21340 +
21341 +       i = a->dst_inode;
21342 +       if (i) {
21343 +               if (!a->exchange) {
21344 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
21345 +                               vfsub_drop_nlink(i);
21346 +                       else {
21347 +                               vfsub_dead_dir(i);
21348 +                               au_cpup_attr_timesizes(i);
21349 +                       }
21350 +                       au_update_dbrange(d, /*do_put_zero*/1);
21351 +               } else
21352 +                       au_cpup_attr_nlink(i, /*force*/1);
21353 +       } else {
21354 +               bbot = a->btgt;
21355 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
21356 +                       au_set_h_dptr(d, bindex, NULL);
21357 +               bbot = au_dbbot(d);
21358 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
21359 +                       au_set_h_dptr(d, bindex, NULL);
21360 +               au_update_dbrange(d, /*do_put_zero*/0);
21361 +       }
21362 +
21363 +       d = a->src_dentry;
21364 +       if (!a->exchange) {
21365 +               au_set_dbwh(d, -1);
21366 +               bbot = au_dbbot(d);
21367 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
21368 +                       h_d = au_h_dptr(d, bindex);
21369 +                       if (h_d)
21370 +                               au_set_h_dptr(d, bindex, NULL);
21371 +               }
21372 +               au_set_dbbot(d, a->btgt);
21373 +
21374 +               sb = d->d_sb;
21375 +               i = a->src_inode;
21376 +               if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
21377 +                       return; /* success */
21378 +
21379 +               bbot = au_ibbot(i);
21380 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
21381 +                       h_i = au_h_iptr(i, bindex);
21382 +                       if (h_i) {
21383 +                               au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
21384 +                               /* ignore this error */
21385 +                               au_set_h_iptr(i, bindex, NULL, 0);
21386 +                       }
21387 +               }
21388 +               au_set_ibbot(i, a->btgt);
21389 +       }
21390 +               d_drop(a->src_dentry);
21391 +}
21392 +
21393 +/* ---------------------------------------------------------------------- */
21394 +
21395 +/* mainly for link(2) and rename(2) */
21396 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
21397 +{
21398 +       aufs_bindex_t bdiropq, bwh;
21399 +       struct dentry *parent;
21400 +       struct au_branch *br;
21401 +
21402 +       parent = dentry->d_parent;
21403 +       IMustLock(d_inode(parent)); /* dir is locked */
21404 +
21405 +       bdiropq = au_dbdiropq(parent);
21406 +       bwh = au_dbwh(dentry);
21407 +       br = au_sbr(dentry->d_sb, btgt);
21408 +       if (au_br_rdonly(br)
21409 +           || (0 <= bdiropq && bdiropq < btgt)
21410 +           || (0 <= bwh && bwh < btgt))
21411 +               btgt = -1;
21412 +
21413 +       AuDbg("btgt %d\n", btgt);
21414 +       return btgt;
21415 +}
21416 +
21417 +/* sets src_btop, dst_btop and btgt */
21418 +static int au_ren_wbr(struct au_ren_args *a)
21419 +{
21420 +       int err;
21421 +       struct au_wr_dir_args wr_dir_args = {
21422 +               /* .force_btgt  = -1, */
21423 +               .flags          = AuWrDir_ADD_ENTRY
21424 +       };
21425 +
21426 +       a->src_btop = au_dbtop(a->src_dentry);
21427 +       a->dst_btop = au_dbtop(a->dst_dentry);
21428 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
21429 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
21430 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21431 +       wr_dir_args.force_btgt = a->src_btop;
21432 +       if (a->dst_inode && a->dst_btop < a->src_btop)
21433 +               wr_dir_args.force_btgt = a->dst_btop;
21434 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
21435 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
21436 +       a->btgt = err;
21437 +       if (a->exchange)
21438 +               au_update_dbtop(a->dst_dentry);
21439 +
21440 +       return err;
21441 +}
21442 +
21443 +static void au_ren_dt(struct au_ren_args *a)
21444 +{
21445 +       a->h_path.dentry = a->src_h_parent;
21446 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
21447 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
21448 +               a->h_path.dentry = a->dst_h_parent;
21449 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
21450 +       }
21451 +
21452 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
21453 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
21454 +           && !a->exchange)
21455 +               return;
21456 +
21457 +       a->h_path.dentry = a->src_h_dentry;
21458 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
21459 +       if (d_is_positive(a->dst_h_dentry)) {
21460 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
21461 +               a->h_path.dentry = a->dst_h_dentry;
21462 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
21463 +       }
21464 +}
21465 +
21466 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
21467 +{
21468 +       struct dentry *h_d;
21469 +       struct inode *h_inode;
21470 +
21471 +       au_dtime_revert(a->src_dt + AuPARENT);
21472 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
21473 +               au_dtime_revert(a->dst_dt + AuPARENT);
21474 +
21475 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
21476 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
21477 +               h_inode = d_inode(h_d);
21478 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21479 +               au_dtime_revert(a->src_dt + AuCHILD);
21480 +               inode_unlock(h_inode);
21481 +
21482 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
21483 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
21484 +                       h_inode = d_inode(h_d);
21485 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
21486 +                       au_dtime_revert(a->dst_dt + AuCHILD);
21487 +                       inode_unlock(h_inode);
21488 +               }
21489 +       }
21490 +}
21491 +
21492 +/* ---------------------------------------------------------------------- */
21493 +
21494 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
21495 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
21496 +               unsigned int _flags)
21497 +{
21498 +       int err, lock_flags;
21499 +       /* reduce stack space */
21500 +       struct au_ren_args *a;
21501 +       struct au_pin pin;
21502 +
21503 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
21504 +       IMustLock(_src_dir);
21505 +       IMustLock(_dst_dir);
21506 +
21507 +       err = -EINVAL;
21508 +       if (unlikely(_flags & RENAME_WHITEOUT))
21509 +               goto out;
21510 +
21511 +       err = -ENOMEM;
21512 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
21513 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21514 +       if (unlikely(!a))
21515 +               goto out;
21516 +
21517 +       a->flags = _flags;
21518 +       a->exchange = _flags & RENAME_EXCHANGE;
21519 +       a->src_dir = _src_dir;
21520 +       a->src_dentry = _src_dentry;
21521 +       a->src_inode = NULL;
21522 +       if (d_really_is_positive(a->src_dentry))
21523 +               a->src_inode = d_inode(a->src_dentry);
21524 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
21525 +       a->dst_dir = _dst_dir;
21526 +       a->dst_dentry = _dst_dentry;
21527 +       a->dst_inode = NULL;
21528 +       if (d_really_is_positive(a->dst_dentry))
21529 +               a->dst_inode = d_inode(a->dst_dentry);
21530 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
21531 +       if (a->dst_inode) {
21532 +               /*
21533 +                * if EXCHANGE && src is non-dir && dst is dir,
21534 +                * dst is not locked.
21535 +                */
21536 +               /* IMustLock(a->dst_inode); */
21537 +               au_igrab(a->dst_inode);
21538 +       }
21539 +
21540 +       err = -ENOTDIR;
21541 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
21542 +       if (d_is_dir(a->src_dentry)) {
21543 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
21544 +               if (unlikely(!a->exchange
21545 +                            && d_really_is_positive(a->dst_dentry)
21546 +                            && !d_is_dir(a->dst_dentry)))
21547 +                       goto out_free;
21548 +               lock_flags |= AuLock_DIRS;
21549 +       }
21550 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
21551 +               au_fset_ren(a->auren_flags, ISDIR_DST);
21552 +               if (unlikely(!a->exchange
21553 +                            && d_really_is_positive(a->src_dentry)
21554 +                            && !d_is_dir(a->src_dentry)))
21555 +                       goto out_free;
21556 +               lock_flags |= AuLock_DIRS;
21557 +       }
21558 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, lock_flags);
21559 +       if (unlikely(err))
21560 +               goto out_free;
21561 +
21562 +       err = au_d_hashed_positive(a->src_dentry);
21563 +       if (unlikely(err))
21564 +               goto out_unlock;
21565 +       err = -ENOENT;
21566 +       if (a->dst_inode) {
21567 +               /*
21568 +                * If it is a dir, VFS unhash it before this
21569 +                * function. It means we cannot rely upon d_unhashed().
21570 +                */
21571 +               if (unlikely(!a->dst_inode->i_nlink))
21572 +                       goto out_unlock;
21573 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
21574 +                       err = au_d_hashed_positive(a->dst_dentry);
21575 +                       if (unlikely(err && !a->exchange))
21576 +                               goto out_unlock;
21577 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21578 +                       goto out_unlock;
21579 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
21580 +               goto out_unlock;
21581 +
21582 +       /*
21583 +        * is it possible?
21584 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
21585 +        * there may exist a problem somewhere else.
21586 +        */
21587 +       err = -EINVAL;
21588 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
21589 +               goto out_unlock;
21590 +
21591 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
21592 +       di_write_lock_parent(a->dst_parent);
21593 +
21594 +       /* which branch we process */
21595 +       err = au_ren_wbr(a);
21596 +       if (unlikely(err < 0))
21597 +               goto out_parent;
21598 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
21599 +       a->h_path.mnt = au_br_mnt(a->br);
21600 +
21601 +       /* are they available to be renamed */
21602 +       err = au_ren_may_dir(a);
21603 +       if (unlikely(err))
21604 +               goto out_children;
21605 +
21606 +       /* prepare the writable parent dir on the same branch */
21607 +       if (a->dst_btop == a->btgt) {
21608 +               au_fset_ren(a->auren_flags, WHDST);
21609 +       } else {
21610 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
21611 +               if (unlikely(err))
21612 +                       goto out_children;
21613 +       }
21614 +
21615 +       err = 0;
21616 +       if (!a->exchange) {
21617 +               if (a->src_dir != a->dst_dir) {
21618 +                       /*
21619 +                        * this temporary unlock is safe,
21620 +                        * because both dir->i_mutex are locked.
21621 +                        */
21622 +                       di_write_unlock(a->dst_parent);
21623 +                       di_write_lock_parent(a->src_parent);
21624 +                       err = au_wr_dir_need_wh(a->src_dentry,
21625 +                                               au_ftest_ren(a->auren_flags,
21626 +                                                            ISDIR_SRC),
21627 +                                               &a->btgt);
21628 +                       di_write_unlock(a->src_parent);
21629 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
21630 +                                             /*isdir*/1);
21631 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
21632 +               } else
21633 +                       err = au_wr_dir_need_wh(a->src_dentry,
21634 +                                               au_ftest_ren(a->auren_flags,
21635 +                                                            ISDIR_SRC),
21636 +                                               &a->btgt);
21637 +       }
21638 +       if (unlikely(err < 0))
21639 +               goto out_children;
21640 +       if (err)
21641 +               au_fset_ren(a->auren_flags, WHSRC);
21642 +
21643 +       /* cpup src */
21644 +       if (a->src_btop != a->btgt) {
21645 +               err = au_pin(&pin, a->src_dentry, a->btgt,
21646 +                            au_opt_udba(a->src_dentry->d_sb),
21647 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21648 +               if (!err) {
21649 +                       struct au_cp_generic cpg = {
21650 +                               .dentry = a->src_dentry,
21651 +                               .bdst   = a->btgt,
21652 +                               .bsrc   = a->src_btop,
21653 +                               .len    = -1,
21654 +                               .pin    = &pin,
21655 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21656 +                       };
21657 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
21658 +                       err = au_sio_cpup_simple(&cpg);
21659 +                       au_unpin(&pin);
21660 +               }
21661 +               if (unlikely(err))
21662 +                       goto out_children;
21663 +               a->src_btop = a->btgt;
21664 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21665 +               if (!a->exchange)
21666 +                       au_fset_ren(a->auren_flags, WHSRC);
21667 +       }
21668 +
21669 +       /* cpup dst */
21670 +       if (a->exchange && a->dst_inode
21671 +           && a->dst_btop != a->btgt) {
21672 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
21673 +                            au_opt_udba(a->dst_dentry->d_sb),
21674 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21675 +               if (!err) {
21676 +                       struct au_cp_generic cpg = {
21677 +                               .dentry = a->dst_dentry,
21678 +                               .bdst   = a->btgt,
21679 +                               .bsrc   = a->dst_btop,
21680 +                               .len    = -1,
21681 +                               .pin    = &pin,
21682 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21683 +                       };
21684 +                       err = au_sio_cpup_simple(&cpg);
21685 +                       au_unpin(&pin);
21686 +               }
21687 +               if (unlikely(err))
21688 +                       goto out_children;
21689 +               a->dst_btop = a->btgt;
21690 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
21691 +       }
21692 +
21693 +       /* lock them all */
21694 +       err = au_ren_lock(a);
21695 +       if (unlikely(err))
21696 +               /* leave the copied-up one */
21697 +               goto out_children;
21698 +
21699 +       if (!a->exchange) {
21700 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21701 +                       err = au_may_ren(a);
21702 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21703 +                       err = -ENAMETOOLONG;
21704 +               if (unlikely(err))
21705 +                       goto out_hdir;
21706 +       }
21707 +
21708 +       /* store timestamps to be revertible */
21709 +       au_ren_dt(a);
21710 +
21711 +       /* here we go */
21712 +       err = do_rename(a);
21713 +       if (unlikely(err))
21714 +               goto out_dt;
21715 +
21716 +       /* update dir attributes */
21717 +       au_ren_refresh_dir(a);
21718 +
21719 +       /* dput/iput all lower dentries */
21720 +       au_ren_refresh(a);
21721 +
21722 +       goto out_hdir; /* success */
21723 +
21724 +out_dt:
21725 +       au_ren_rev_dt(err, a);
21726 +out_hdir:
21727 +       au_ren_unlock(a);
21728 +out_children:
21729 +       au_nhash_wh_free(&a->whlist);
21730 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
21731 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
21732 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21733 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
21734 +       }
21735 +out_parent:
21736 +       if (!err) {
21737 +               if (!a->exchange)
21738 +                       d_move(a->src_dentry, a->dst_dentry);
21739 +               else
21740 +                       d_exchange(a->src_dentry, a->dst_dentry);
21741 +       } else {
21742 +               au_update_dbtop(a->dst_dentry);
21743 +               if (!a->dst_inode)
21744 +                       d_drop(a->dst_dentry);
21745 +       }
21746 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
21747 +               di_write_unlock(a->dst_parent);
21748 +       else
21749 +               di_write_unlock2(a->src_parent, a->dst_parent);
21750 +out_unlock:
21751 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21752 +out_free:
21753 +       iput(a->dst_inode);
21754 +       if (a->thargs)
21755 +               au_whtmp_rmdir_free(a->thargs);
21756 +       kfree(a);
21757 +out:
21758 +       AuTraceErr(err);
21759 +       return err;
21760 +}
21761 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21762 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21763 +++ linux/fs/aufs/Kconfig       2017-07-29 12:14:25.896375188 +0200
21764 @@ -0,0 +1,185 @@
21765 +config AUFS_FS
21766 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21767 +       help
21768 +       Aufs is a stackable unification filesystem such as Unionfs,
21769 +       which unifies several directories and provides a merged single
21770 +       directory.
21771 +       In the early days, aufs was entirely re-designed and
21772 +       re-implemented Unionfs Version 1.x series. Introducing many
21773 +       original ideas, approaches and improvements, it becomes totally
21774 +       different from Unionfs while keeping the basic features.
21775 +
21776 +if AUFS_FS
21777 +choice
21778 +       prompt "Maximum number of branches"
21779 +       default AUFS_BRANCH_MAX_127
21780 +       help
21781 +       Specifies the maximum number of branches (or member directories)
21782 +       in a single aufs. The larger value consumes more system
21783 +       resources and has a minor impact to performance.
21784 +config AUFS_BRANCH_MAX_127
21785 +       bool "127"
21786 +       help
21787 +       Specifies the maximum number of branches (or member directories)
21788 +       in a single aufs. The larger value consumes more system
21789 +       resources and has a minor impact to performance.
21790 +config AUFS_BRANCH_MAX_511
21791 +       bool "511"
21792 +       help
21793 +       Specifies the maximum number of branches (or member directories)
21794 +       in a single aufs. The larger value consumes more system
21795 +       resources and has a minor impact to performance.
21796 +config AUFS_BRANCH_MAX_1023
21797 +       bool "1023"
21798 +       help
21799 +       Specifies the maximum number of branches (or member directories)
21800 +       in a single aufs. The larger value consumes more system
21801 +       resources and has a minor impact to performance.
21802 +config AUFS_BRANCH_MAX_32767
21803 +       bool "32767"
21804 +       help
21805 +       Specifies the maximum number of branches (or member directories)
21806 +       in a single aufs. The larger value consumes more system
21807 +       resources and has a minor impact to performance.
21808 +endchoice
21809 +
21810 +config AUFS_SBILIST
21811 +       bool
21812 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21813 +       default y
21814 +       help
21815 +       Automatic configuration for internal use.
21816 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21817 +
21818 +config AUFS_HNOTIFY
21819 +       bool "Detect direct branch access (bypassing aufs)"
21820 +       help
21821 +       If you want to modify files on branches directly, eg. bypassing aufs,
21822 +       and want aufs to detect the changes of them fully, then enable this
21823 +       option and use 'udba=notify' mount option.
21824 +       Currently there is only one available configuration, "fsnotify".
21825 +       It will have a negative impact to the performance.
21826 +       See detail in aufs.5.
21827 +
21828 +choice
21829 +       prompt "method" if AUFS_HNOTIFY
21830 +       default AUFS_HFSNOTIFY
21831 +config AUFS_HFSNOTIFY
21832 +       bool "fsnotify"
21833 +       select FSNOTIFY
21834 +endchoice
21835 +
21836 +config AUFS_EXPORT
21837 +       bool "NFS-exportable aufs"
21838 +       depends on EXPORTFS
21839 +       help
21840 +       If you want to export your mounted aufs via NFS, then enable this
21841 +       option. There are several requirements for this configuration.
21842 +       See detail in aufs.5.
21843 +
21844 +config AUFS_INO_T_64
21845 +       bool
21846 +       depends on AUFS_EXPORT
21847 +       depends on 64BIT && !(ALPHA || S390)
21848 +       default y
21849 +       help
21850 +       Automatic configuration for internal use.
21851 +       /* typedef unsigned long/int __kernel_ino_t */
21852 +       /* alpha and s390x are int */
21853 +
21854 +config AUFS_XATTR
21855 +       bool "support for XATTR/EA (including Security Labels)"
21856 +       help
21857 +       If your branch fs supports XATTR/EA and you want to make them
21858 +       available in aufs too, then enable this opsion and specify the
21859 +       branch attributes for EA.
21860 +       See detail in aufs.5.
21861 +
21862 +config AUFS_FHSM
21863 +       bool "File-based Hierarchical Storage Management"
21864 +       help
21865 +       Hierarchical Storage Management (or HSM) is a well-known feature
21866 +       in the storage world. Aufs provides this feature as file-based.
21867 +       with multiple branches.
21868 +       These multiple branches are prioritized, ie. the topmost one
21869 +       should be the fastest drive and be used heavily.
21870 +
21871 +config AUFS_RDU
21872 +       bool "Readdir in userspace"
21873 +       help
21874 +       Aufs has two methods to provide a merged view for a directory,
21875 +       by a user-space library and by kernel-space natively. The latter
21876 +       is always enabled but sometimes large and slow.
21877 +       If you enable this option, install the library in aufs2-util
21878 +       package, and set some environment variables for your readdir(3),
21879 +       then the work will be handled in user-space which generally
21880 +       shows better performance in most cases.
21881 +       See detail in aufs.5.
21882 +
21883 +config AUFS_SHWH
21884 +       bool "Show whiteouts"
21885 +       help
21886 +       If you want to make the whiteouts in aufs visible, then enable
21887 +       this option and specify 'shwh' mount option. Although it may
21888 +       sounds like philosophy or something, but in technically it
21889 +       simply shows the name of whiteout with keeping its behaviour.
21890 +
21891 +config AUFS_BR_RAMFS
21892 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21893 +       help
21894 +       If you want to use ramfs as an aufs branch fs, then enable this
21895 +       option. Generally tmpfs is recommended.
21896 +       Aufs prohibited them to be a branch fs by default, because
21897 +       initramfs becomes unusable after switch_root or something
21898 +       generally. If you sets initramfs as an aufs branch and boot your
21899 +       system by switch_root, you will meet a problem easily since the
21900 +       files in initramfs may be inaccessible.
21901 +       Unless you are going to use ramfs as an aufs branch fs without
21902 +       switch_root or something, leave it N.
21903 +
21904 +config AUFS_BR_FUSE
21905 +       bool "Fuse fs as an aufs branch"
21906 +       depends on FUSE_FS
21907 +       select AUFS_POLL
21908 +       help
21909 +       If you want to use fuse-based userspace filesystem as an aufs
21910 +       branch fs, then enable this option.
21911 +       It implements the internal poll(2) operation which is
21912 +       implemented by fuse only (curretnly).
21913 +
21914 +config AUFS_POLL
21915 +       bool
21916 +       help
21917 +       Automatic configuration for internal use.
21918 +
21919 +config AUFS_BR_HFSPLUS
21920 +       bool "Hfsplus as an aufs branch"
21921 +       depends on HFSPLUS_FS
21922 +       default y
21923 +       help
21924 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21925 +       this option. This option introduces a small overhead at
21926 +       copying-up a file on hfsplus.
21927 +
21928 +config AUFS_BDEV_LOOP
21929 +       bool
21930 +       depends on BLK_DEV_LOOP
21931 +       default y
21932 +       help
21933 +       Automatic configuration for internal use.
21934 +       Convert =[ym] into =y.
21935 +
21936 +config AUFS_DEBUG
21937 +       bool "Debug aufs"
21938 +       help
21939 +       Enable this to compile aufs internal debug code.
21940 +       It will have a negative impact to the performance.
21941 +
21942 +config AUFS_MAGIC_SYSRQ
21943 +       bool
21944 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21945 +       default y
21946 +       help
21947 +       Automatic configuration for internal use.
21948 +       When aufs supports Magic SysRq, enabled automatically.
21949 +endif
21950 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21951 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21952 +++ linux/fs/aufs/loop.c        2017-07-29 12:14:25.903042072 +0200
21953 @@ -0,0 +1,147 @@
21954 +/*
21955 + * Copyright (C) 2005-2017 Junjiro R. Okajima
21956 + *
21957 + * This program, aufs is free software; you can redistribute it and/or modify
21958 + * it under the terms of the GNU General Public License as published by
21959 + * the Free Software Foundation; either version 2 of the License, or
21960 + * (at your option) any later version.
21961 + *
21962 + * This program is distributed in the hope that it will be useful,
21963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21965 + * GNU General Public License for more details.
21966 + *
21967 + * You should have received a copy of the GNU General Public License
21968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21969 + */
21970 +
21971 +/*
21972 + * support for loopback block device as a branch
21973 + */
21974 +
21975 +#include "aufs.h"
21976 +
21977 +/* added into drivers/block/loop.c */
21978 +static struct file *(*backing_file_func)(struct super_block *sb);
21979 +
21980 +/*
21981 + * test if two lower dentries have overlapping branches.
21982 + */
21983 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21984 +{
21985 +       struct super_block *h_sb;
21986 +       struct file *backing_file;
21987 +
21988 +       if (unlikely(!backing_file_func)) {
21989 +               /* don't load "loop" module here */
21990 +               backing_file_func = symbol_get(loop_backing_file);
21991 +               if (unlikely(!backing_file_func))
21992 +                       /* "loop" module is not loaded */
21993 +                       return 0;
21994 +       }
21995 +
21996 +       h_sb = h_adding->d_sb;
21997 +       backing_file = backing_file_func(h_sb);
21998 +       if (!backing_file)
21999 +               return 0;
22000 +
22001 +       h_adding = backing_file->f_path.dentry;
22002 +       /*
22003 +        * h_adding can be local NFS.
22004 +        * in this case aufs cannot detect the loop.
22005 +        */
22006 +       if (unlikely(h_adding->d_sb == sb))
22007 +               return 1;
22008 +       return !!au_test_subdir(h_adding, sb->s_root);
22009 +}
22010 +
22011 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
22012 +int au_test_loopback_kthread(void)
22013 +{
22014 +       int ret;
22015 +       struct task_struct *tsk = current;
22016 +       char c, comm[sizeof(tsk->comm)];
22017 +
22018 +       ret = 0;
22019 +       if (tsk->flags & PF_KTHREAD) {
22020 +               get_task_comm(comm, tsk);
22021 +               c = comm[4];
22022 +               ret = ('0' <= c && c <= '9'
22023 +                      && !strncmp(comm, "loop", 4));
22024 +       }
22025 +
22026 +       return ret;
22027 +}
22028 +
22029 +/* ---------------------------------------------------------------------- */
22030 +
22031 +#define au_warn_loopback_step  16
22032 +static int au_warn_loopback_nelem = au_warn_loopback_step;
22033 +static unsigned long *au_warn_loopback_array;
22034 +
22035 +void au_warn_loopback(struct super_block *h_sb)
22036 +{
22037 +       int i, new_nelem;
22038 +       unsigned long *a, magic;
22039 +       static DEFINE_SPINLOCK(spin);
22040 +
22041 +       magic = h_sb->s_magic;
22042 +       spin_lock(&spin);
22043 +       a = au_warn_loopback_array;
22044 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
22045 +               if (a[i] == magic) {
22046 +                       spin_unlock(&spin);
22047 +                       return;
22048 +               }
22049 +
22050 +       /* h_sb is new to us, print it */
22051 +       if (i < au_warn_loopback_nelem) {
22052 +               a[i] = magic;
22053 +               goto pr;
22054 +       }
22055 +
22056 +       /* expand the array */
22057 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
22058 +       a = au_kzrealloc(au_warn_loopback_array,
22059 +                        au_warn_loopback_nelem * sizeof(unsigned long),
22060 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
22061 +                        /*may_shrink*/0);
22062 +       if (a) {
22063 +               au_warn_loopback_nelem = new_nelem;
22064 +               au_warn_loopback_array = a;
22065 +               a[i] = magic;
22066 +               goto pr;
22067 +       }
22068 +
22069 +       spin_unlock(&spin);
22070 +       AuWarn1("realloc failed, ignored\n");
22071 +       return;
22072 +
22073 +pr:
22074 +       spin_unlock(&spin);
22075 +       pr_warn("you may want to try another patch for loopback file "
22076 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
22077 +}
22078 +
22079 +int au_loopback_init(void)
22080 +{
22081 +       int err;
22082 +       struct super_block *sb __maybe_unused;
22083 +
22084 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
22085 +
22086 +       err = 0;
22087 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
22088 +                                        sizeof(unsigned long), GFP_NOFS);
22089 +       if (unlikely(!au_warn_loopback_array))
22090 +               err = -ENOMEM;
22091 +
22092 +       return err;
22093 +}
22094 +
22095 +void au_loopback_fin(void)
22096 +{
22097 +       if (backing_file_func)
22098 +               symbol_put(loop_backing_file);
22099 +       kfree(au_warn_loopback_array);
22100 +}
22101 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
22102 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
22103 +++ linux/fs/aufs/loop.h        2017-07-29 12:14:25.903042072 +0200
22104 @@ -0,0 +1,52 @@
22105 +/*
22106 + * Copyright (C) 2005-2017 Junjiro R. Okajima
22107 + *
22108 + * This program, aufs is free software; you can redistribute it and/or modify
22109 + * it under the terms of the GNU General Public License as published by
22110 + * the Free Software Foundation; either version 2 of the License, or
22111 + * (at your option) any later version.
22112 + *
22113 + * This program is distributed in the hope that it will be useful,
22114 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22115 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22116 + * GNU General Public License for more details.
22117 + *
22118 + * You should have received a copy of the GNU General Public License
22119 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22120 + */
22121 +
22122 +/*
22123 + * support for loopback mount as a branch
22124 + */
22125 +
22126 +#ifndef __AUFS_LOOP_H__
22127 +#define __AUFS_LOOP_H__
22128 +
22129 +#ifdef __KERNEL__
22130 +
22131 +struct dentry;
22132 +struct super_block;
22133 +
22134 +#ifdef CONFIG_AUFS_BDEV_LOOP
22135 +/* drivers/block/loop.c */
22136 +struct file *loop_backing_file(struct super_block *sb);
22137 +
22138 +/* loop.c */
22139 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
22140 +int au_test_loopback_kthread(void);
22141 +void au_warn_loopback(struct super_block *h_sb);
22142 +
22143 +int au_loopback_init(void);
22144 +void au_loopback_fin(void);
22145 +#else
22146 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
22147 +          struct dentry *h_adding)
22148 +AuStubInt0(au_test_loopback_kthread, void)
22149 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
22150 +
22151 +AuStubInt0(au_loopback_init, void)
22152 +AuStubVoid(au_loopback_fin, void)
22153 +#endif /* BLK_DEV_LOOP */
22154 +
22155 +#endif /* __KERNEL__ */
22156 +#endif /* __AUFS_LOOP_H__ */
22157 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
22158 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
22159 +++ linux/fs/aufs/magic.mk      2017-07-29 12:14:25.903042072 +0200
22160 @@ -0,0 +1,30 @@
22161 +
22162 +# defined in ${srctree}/fs/fuse/inode.c
22163 +# tristate
22164 +ifdef CONFIG_FUSE_FS
22165 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
22166 +endif
22167 +
22168 +# defined in ${srctree}/fs/xfs/xfs_sb.h
22169 +# tristate
22170 +ifdef CONFIG_XFS_FS
22171 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
22172 +endif
22173 +
22174 +# defined in ${srctree}/fs/configfs/mount.c
22175 +# tristate
22176 +ifdef CONFIG_CONFIGFS_FS
22177 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
22178 +endif
22179 +
22180 +# defined in ${srctree}/fs/ubifs/ubifs.h
22181 +# tristate
22182 +ifdef CONFIG_UBIFS_FS
22183 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
22184 +endif
22185 +
22186 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
22187 +# tristate
22188 +ifdef CONFIG_HFSPLUS_FS
22189 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
22190 +endif
22191 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
22192 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
22193 +++ linux/fs/aufs/Makefile      2017-07-29 12:14:25.896375188 +0200
22194 @@ -0,0 +1,44 @@
22195 +
22196 +include ${src}/magic.mk
22197 +ifeq (${CONFIG_AUFS_FS},m)
22198 +include ${src}/conf.mk
22199 +endif
22200 +-include ${src}/priv_def.mk
22201 +
22202 +# cf. include/linux/kernel.h
22203 +# enable pr_debug
22204 +ccflags-y += -DDEBUG
22205 +# sparse requires the full pathname
22206 +ifdef M
22207 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
22208 +else
22209 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
22210 +endif
22211 +
22212 +obj-$(CONFIG_AUFS_FS) += aufs.o
22213 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
22214 +       wkq.o vfsub.o dcsub.o \
22215 +       cpup.o whout.o wbr_policy.o \
22216 +       dinfo.o dentry.o \
22217 +       dynop.o \
22218 +       finfo.o file.o f_op.o \
22219 +       dir.o vdir.o \
22220 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
22221 +       mvdown.o ioctl.o
22222 +
22223 +# all are boolean
22224 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
22225 +aufs-$(CONFIG_SYSFS) += sysfs.o
22226 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
22227 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
22228 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
22229 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
22230 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
22231 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
22232 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
22233 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
22234 +aufs-$(CONFIG_AUFS_POLL) += poll.o
22235 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
22236 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
22237 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
22238 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
22239 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
22240 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
22241 +++ linux/fs/aufs/module.c      2017-07-29 12:14:25.903042072 +0200
22242 @@ -0,0 +1,266 @@
22243 +/*
22244 + * Copyright (C) 2005-2017 Junjiro R. Okajima
22245 + *
22246 + * This program, aufs is free software; you can redistribute it and/or modify
22247 + * it under the terms of the GNU General Public License as published by
22248 + * the Free Software Foundation; either version 2 of the License, or
22249 + * (at your option) any later version.
22250 + *
22251 + * This program is distributed in the hope that it will be useful,
22252 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22253 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22254 + * GNU General Public License for more details.
22255 + *
22256 + * You should have received a copy of the GNU General Public License
22257 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22258 + */
22259 +
22260 +/*
22261 + * module global variables and operations
22262 + */
22263 +
22264 +#include <linux/module.h>
22265 +#include <linux/seq_file.h>
22266 +#include "aufs.h"
22267 +
22268 +/* shrinkable realloc */
22269 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
22270 +{
22271 +       size_t sz;
22272 +       int diff;
22273 +
22274 +       sz = 0;
22275 +       diff = -1;
22276 +       if (p) {
22277 +#if 0 /* unused */
22278 +               if (!new_sz) {
22279 +                       kfree(p);
22280 +                       p = NULL;
22281 +                       goto out;
22282 +               }
22283 +#else
22284 +               AuDebugOn(!new_sz);
22285 +#endif
22286 +               sz = ksize(p);
22287 +               diff = au_kmidx_sub(sz, new_sz);
22288 +       }
22289 +       if (sz && !diff)
22290 +               goto out;
22291 +
22292 +       if (sz < new_sz)
22293 +               /* expand or SLOB */
22294 +               p = krealloc(p, new_sz, gfp);
22295 +       else if (new_sz < sz && may_shrink) {
22296 +               /* shrink */
22297 +               void *q;
22298 +
22299 +               q = kmalloc(new_sz, gfp);
22300 +               if (q) {
22301 +                       if (p) {
22302 +                               memcpy(q, p, new_sz);
22303 +                               kfree(p);
22304 +                       }
22305 +                       p = q;
22306 +               } else
22307 +                       p = NULL;
22308 +       }
22309 +
22310 +out:
22311 +       return p;
22312 +}
22313 +
22314 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
22315 +                  int may_shrink)
22316 +{
22317 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
22318 +       if (p && new_sz > nused)
22319 +               memset(p + nused, 0, new_sz - nused);
22320 +       return p;
22321 +}
22322 +
22323 +/* ---------------------------------------------------------------------- */
22324 +/*
22325 + * aufs caches
22326 + */
22327 +struct kmem_cache *au_cache[AuCache_Last];
22328 +
22329 +static void au_cache_fin(void)
22330 +{
22331 +       int i;
22332 +
22333 +       /*
22334 +        * Make sure all delayed rcu free inodes are flushed before we
22335 +        * destroy cache.
22336 +        */
22337 +       rcu_barrier();
22338 +
22339 +       /* excluding AuCache_HNOTIFY */
22340 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
22341 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
22342 +               kmem_cache_destroy(au_cache[i]);
22343 +               au_cache[i] = NULL;
22344 +       }
22345 +}
22346 +
22347 +static int __init au_cache_init(void)
22348 +{
22349 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
22350 +       if (au_cache[AuCache_DINFO])
22351 +               /* SLAB_DESTROY_BY_RCU */
22352 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
22353 +                                                      au_icntnr_init_once);
22354 +       if (au_cache[AuCache_ICNTNR])
22355 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
22356 +                                                     au_fi_init_once);
22357 +       if (au_cache[AuCache_FINFO])
22358 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
22359 +       if (au_cache[AuCache_VDIR])
22360 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
22361 +       if (au_cache[AuCache_DEHSTR])
22362 +               return 0;
22363 +
22364 +       au_cache_fin();
22365 +       return -ENOMEM;
22366 +}
22367 +
22368 +/* ---------------------------------------------------------------------- */
22369 +
22370 +int au_dir_roflags;
22371 +
22372 +#ifdef CONFIG_AUFS_SBILIST
22373 +/*
22374 + * iterate_supers_type() doesn't protect us from
22375 + * remounting (branch management)
22376 + */
22377 +struct au_sphlhead au_sbilist;
22378 +#endif
22379 +
22380 +/*
22381 + * functions for module interface.
22382 + */
22383 +MODULE_LICENSE("GPL");
22384 +/* MODULE_LICENSE("GPL v2"); */
22385 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
22386 +MODULE_DESCRIPTION(AUFS_NAME
22387 +       " -- Advanced multi layered unification filesystem");
22388 +MODULE_VERSION(AUFS_VERSION);
22389 +MODULE_ALIAS_FS(AUFS_NAME);
22390 +
22391 +/* this module parameter has no meaning when SYSFS is disabled */
22392 +int sysaufs_brs = 1;
22393 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
22394 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
22395 +
22396 +/* this module parameter has no meaning when USER_NS is disabled */
22397 +bool au_userns;
22398 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
22399 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
22400 +
22401 +/* ---------------------------------------------------------------------- */
22402 +
22403 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
22404 +
22405 +int au_seq_path(struct seq_file *seq, struct path *path)
22406 +{
22407 +       int err;
22408 +
22409 +       err = seq_path(seq, path, au_esc_chars);
22410 +       if (err >= 0)
22411 +               err = 0;
22412 +       else
22413 +               err = -ENOMEM;
22414 +
22415 +       return err;
22416 +}
22417 +
22418 +/* ---------------------------------------------------------------------- */
22419 +
22420 +static int __init aufs_init(void)
22421 +{
22422 +       int err, i;
22423 +       char *p;
22424 +
22425 +       p = au_esc_chars;
22426 +       for (i = 1; i <= ' '; i++)
22427 +               *p++ = i;
22428 +       *p++ = '\\';
22429 +       *p++ = '\x7f';
22430 +       *p = 0;
22431 +
22432 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
22433 +
22434 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
22435 +       for (i = 0; i < AuIop_Last; i++)
22436 +               aufs_iop_nogetattr[i].getattr = NULL;
22437 +
22438 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
22439 +
22440 +       au_sbilist_init();
22441 +       sysaufs_brs_init();
22442 +       au_debug_init();
22443 +       au_dy_init();
22444 +       err = sysaufs_init();
22445 +       if (unlikely(err))
22446 +               goto out;
22447 +       err = au_procfs_init();
22448 +       if (unlikely(err))
22449 +               goto out_sysaufs;
22450 +       err = au_wkq_init();
22451 +       if (unlikely(err))
22452 +               goto out_procfs;
22453 +       err = au_loopback_init();
22454 +       if (unlikely(err))
22455 +               goto out_wkq;
22456 +       err = au_hnotify_init();
22457 +       if (unlikely(err))
22458 +               goto out_loopback;
22459 +       err = au_sysrq_init();
22460 +       if (unlikely(err))
22461 +               goto out_hin;
22462 +       err = au_cache_init();
22463 +       if (unlikely(err))
22464 +               goto out_sysrq;
22465 +
22466 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
22467 +       err = register_filesystem(&aufs_fs_type);
22468 +       if (unlikely(err))
22469 +               goto out_cache;
22470 +
22471 +       /* since we define pr_fmt, call printk directly */
22472 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
22473 +       goto out; /* success */
22474 +
22475 +out_cache:
22476 +       au_cache_fin();
22477 +out_sysrq:
22478 +       au_sysrq_fin();
22479 +out_hin:
22480 +       au_hnotify_fin();
22481 +out_loopback:
22482 +       au_loopback_fin();
22483 +out_wkq:
22484 +       au_wkq_fin();
22485 +out_procfs:
22486 +       au_procfs_fin();
22487 +out_sysaufs:
22488 +       sysaufs_fin();
22489 +       au_dy_fin();
22490 +out:
22491 +       return err;
22492 +}
22493 +
22494 +static void __exit aufs_exit(void)
22495 +{
22496 +       unregister_filesystem(&aufs_fs_type);
22497 +       au_cache_fin();
22498 +       au_sysrq_fin();
22499 +       au_hnotify_fin();
22500 +       au_loopback_fin();
22501 +       au_wkq_fin();
22502 +       au_procfs_fin();
22503 +       sysaufs_fin();
22504 +       au_dy_fin();
22505 +}
22506 +
22507 +module_init(aufs_init);
22508 +module_exit(aufs_exit);
22509 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
22510 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
22511 +++ linux/fs/aufs/module.h      2017-07-29 12:14:25.903042072 +0200
22512 @@ -0,0 +1,101 @@
22513 +/*
22514 + * Copyright (C) 2005-2017 Junjiro R. Okajima
22515 + *
22516 + * This program, aufs is free software; you can redistribute it and/or modify
22517 + * it under the terms of the GNU General Public License as published by
22518 + * the Free Software Foundation; either version 2 of the License, or
22519 + * (at your option) any later version.
22520 + *
22521 + * This program is distributed in the hope that it will be useful,
22522 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22523 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22524 + * GNU General Public License for more details.
22525 + *
22526 + * You should have received a copy of the GNU General Public License
22527 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22528 + */
22529 +
22530 +/*
22531 + * module initialization and module-global
22532 + */
22533 +
22534 +#ifndef __AUFS_MODULE_H__
22535 +#define __AUFS_MODULE_H__
22536 +
22537 +#ifdef __KERNEL__
22538 +
22539 +#include <linux/slab.h>
22540 +
22541 +struct path;
22542 +struct seq_file;
22543 +
22544 +/* module parameters */
22545 +extern int sysaufs_brs;
22546 +extern bool au_userns;
22547 +
22548 +/* ---------------------------------------------------------------------- */
22549 +
22550 +extern int au_dir_roflags;
22551 +
22552 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
22553 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
22554 +                  int may_shrink);
22555 +
22556 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
22557 +{
22558 +#ifndef CONFIG_SLOB
22559 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
22560 +#else
22561 +       return -1; /* SLOB is untested */
22562 +#endif
22563 +}
22564 +
22565 +int au_seq_path(struct seq_file *seq, struct path *path);
22566 +
22567 +#ifdef CONFIG_PROC_FS
22568 +/* procfs.c */
22569 +int __init au_procfs_init(void);
22570 +void au_procfs_fin(void);
22571 +#else
22572 +AuStubInt0(au_procfs_init, void);
22573 +AuStubVoid(au_procfs_fin, void);
22574 +#endif
22575 +
22576 +/* ---------------------------------------------------------------------- */
22577 +
22578 +/* kmem cache */
22579 +enum {
22580 +       AuCache_DINFO,
22581 +       AuCache_ICNTNR,
22582 +       AuCache_FINFO,
22583 +       AuCache_VDIR,
22584 +       AuCache_DEHSTR,
22585 +       AuCache_HNOTIFY, /* must be last */
22586 +       AuCache_Last
22587 +};
22588 +
22589 +extern struct kmem_cache *au_cache[AuCache_Last];
22590 +
22591 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
22592 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
22593 +#define AuCacheCtor(type, ctor)        \
22594 +       kmem_cache_create(#type, sizeof(struct type), \
22595 +                         __alignof__(struct type), AuCacheFlags, ctor)
22596 +
22597 +#define AuCacheFuncs(name, index) \
22598 +static inline struct au_##name *au_cache_alloc_##name(void) \
22599 +{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
22600 +static inline void au_cache_free_##name(struct au_##name *p) \
22601 +{ kmem_cache_free(au_cache[AuCache_##index], p); }
22602 +
22603 +AuCacheFuncs(dinfo, DINFO);
22604 +AuCacheFuncs(icntnr, ICNTNR);
22605 +AuCacheFuncs(finfo, FINFO);
22606 +AuCacheFuncs(vdir, VDIR);
22607 +AuCacheFuncs(vdir_dehstr, DEHSTR);
22608 +#ifdef CONFIG_AUFS_HNOTIFY
22609 +AuCacheFuncs(hnotify, HNOTIFY);
22610 +#endif
22611 +
22612 +#endif /* __KERNEL__ */
22613 +#endif /* __AUFS_MODULE_H__ */
22614 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
22615 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
22616 +++ linux/fs/aufs/mvdown.c      2017-07-29 12:14:25.903042072 +0200
22617 @@ -0,0 +1,704 @@
22618 +/*
22619 + * Copyright (C) 2011-2017 Junjiro R. Okajima
22620 + *
22621 + * This program, aufs is free software; you can redistribute it and/or modify
22622 + * it under the terms of the GNU General Public License as published by
22623 + * the Free Software Foundation; either version 2 of the License, or
22624 + * (at your option) any later version.
22625 + *
22626 + * This program is distributed in the hope that it will be useful,
22627 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22628 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22629 + * GNU General Public License for more details.
22630 + *
22631 + * You should have received a copy of the GNU General Public License
22632 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22633 + */
22634 +
22635 +/*
22636 + * move-down, opposite of copy-up
22637 + */
22638 +
22639 +#include "aufs.h"
22640 +
22641 +struct au_mvd_args {
22642 +       struct {
22643 +               struct super_block *h_sb;
22644 +               struct dentry *h_parent;
22645 +               struct au_hinode *hdir;
22646 +               struct inode *h_dir, *h_inode;
22647 +               struct au_pin pin;
22648 +       } info[AUFS_MVDOWN_NARRAY];
22649 +
22650 +       struct aufs_mvdown mvdown;
22651 +       struct dentry *dentry, *parent;
22652 +       struct inode *inode, *dir;
22653 +       struct super_block *sb;
22654 +       aufs_bindex_t bopq, bwh, bfound;
22655 +       unsigned char rename_lock;
22656 +};
22657 +
22658 +#define mvd_errno              mvdown.au_errno
22659 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22660 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22661 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22662 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
22663 +
22664 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
22665 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
22666 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
22667 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
22668 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
22669 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
22670 +
22671 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
22672 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
22673 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
22674 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
22675 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
22676 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
22677 +
22678 +#define AU_MVD_PR(flag, ...) do {                      \
22679 +               if (flag)                               \
22680 +                       pr_err(__VA_ARGS__);            \
22681 +       } while (0)
22682 +
22683 +static int find_lower_writable(struct au_mvd_args *a)
22684 +{
22685 +       struct super_block *sb;
22686 +       aufs_bindex_t bindex, bbot;
22687 +       struct au_branch *br;
22688 +
22689 +       sb = a->sb;
22690 +       bindex = a->mvd_bsrc;
22691 +       bbot = au_sbbot(sb);
22692 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
22693 +               for (bindex++; bindex <= bbot; bindex++) {
22694 +                       br = au_sbr(sb, bindex);
22695 +                       if (au_br_fhsm(br->br_perm)
22696 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22697 +                               return bindex;
22698 +               }
22699 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
22700 +               for (bindex++; bindex <= bbot; bindex++) {
22701 +                       br = au_sbr(sb, bindex);
22702 +                       if (!au_br_rdonly(br))
22703 +                               return bindex;
22704 +               }
22705 +       else
22706 +               for (bindex++; bindex <= bbot; bindex++) {
22707 +                       br = au_sbr(sb, bindex);
22708 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22709 +                               if (au_br_rdonly(br))
22710 +                                       a->mvdown.flags
22711 +                                               |= AUFS_MVDOWN_ROLOWER_R;
22712 +                               return bindex;
22713 +                       }
22714 +               }
22715 +
22716 +       return -1;
22717 +}
22718 +
22719 +/* make the parent dir on bdst */
22720 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
22721 +{
22722 +       int err;
22723 +
22724 +       err = 0;
22725 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22726 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22727 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22728 +       a->mvd_h_dst_parent = NULL;
22729 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
22730 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22731 +       if (!a->mvd_h_dst_parent) {
22732 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22733 +               if (unlikely(err)) {
22734 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
22735 +                       goto out;
22736 +               }
22737 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22738 +       }
22739 +
22740 +out:
22741 +       AuTraceErr(err);
22742 +       return err;
22743 +}
22744 +
22745 +/* lock them all */
22746 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
22747 +{
22748 +       int err;
22749 +       struct dentry *h_trap;
22750 +
22751 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22752 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
22753 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22754 +                    au_opt_udba(a->sb),
22755 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22756 +       AuTraceErr(err);
22757 +       if (unlikely(err)) {
22758 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
22759 +               goto out;
22760 +       }
22761 +
22762 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22763 +               a->rename_lock = 0;
22764 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22765 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22766 +                           au_opt_udba(a->sb),
22767 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22768 +               err = au_do_pin(&a->mvd_pin_src);
22769 +               AuTraceErr(err);
22770 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22771 +               if (unlikely(err)) {
22772 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22773 +                       goto out_dst;
22774 +               }
22775 +               goto out; /* success */
22776 +       }
22777 +
22778 +       a->rename_lock = 1;
22779 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22780 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22781 +                    au_opt_udba(a->sb),
22782 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22783 +       AuTraceErr(err);
22784 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22785 +       if (unlikely(err)) {
22786 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22787 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22788 +               goto out_dst;
22789 +       }
22790 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22791 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22792 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22793 +       if (h_trap) {
22794 +               err = (h_trap != a->mvd_h_src_parent);
22795 +               if (err)
22796 +                       err = (h_trap != a->mvd_h_dst_parent);
22797 +       }
22798 +       BUG_ON(err); /* it should never happen */
22799 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22800 +               err = -EBUSY;
22801 +               AuTraceErr(err);
22802 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22803 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22804 +               au_pin_hdir_lock(&a->mvd_pin_src);
22805 +               au_unpin(&a->mvd_pin_src);
22806 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22807 +               goto out_dst;
22808 +       }
22809 +       goto out; /* success */
22810 +
22811 +out_dst:
22812 +       au_unpin(&a->mvd_pin_dst);
22813 +out:
22814 +       AuTraceErr(err);
22815 +       return err;
22816 +}
22817 +
22818 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22819 +{
22820 +       if (!a->rename_lock)
22821 +               au_unpin(&a->mvd_pin_src);
22822 +       else {
22823 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22824 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22825 +               au_pin_hdir_lock(&a->mvd_pin_src);
22826 +               au_unpin(&a->mvd_pin_src);
22827 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22828 +       }
22829 +       au_unpin(&a->mvd_pin_dst);
22830 +}
22831 +
22832 +/* copy-down the file */
22833 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22834 +{
22835 +       int err;
22836 +       struct au_cp_generic cpg = {
22837 +               .dentry = a->dentry,
22838 +               .bdst   = a->mvd_bdst,
22839 +               .bsrc   = a->mvd_bsrc,
22840 +               .len    = -1,
22841 +               .pin    = &a->mvd_pin_dst,
22842 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22843 +       };
22844 +
22845 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22846 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22847 +               au_fset_cpup(cpg.flags, OVERWRITE);
22848 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22849 +               au_fset_cpup(cpg.flags, RWDST);
22850 +       err = au_sio_cpdown_simple(&cpg);
22851 +       if (unlikely(err))
22852 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22853 +
22854 +       AuTraceErr(err);
22855 +       return err;
22856 +}
22857 +
22858 +/*
22859 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22860 + * were sleeping
22861 + */
22862 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22863 +{
22864 +       int err;
22865 +       struct path h_path;
22866 +       struct au_branch *br;
22867 +       struct inode *delegated;
22868 +
22869 +       br = au_sbr(a->sb, a->mvd_bdst);
22870 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22871 +       err = PTR_ERR(h_path.dentry);
22872 +       if (IS_ERR(h_path.dentry)) {
22873 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22874 +               goto out;
22875 +       }
22876 +
22877 +       err = 0;
22878 +       if (d_is_positive(h_path.dentry)) {
22879 +               h_path.mnt = au_br_mnt(br);
22880 +               delegated = NULL;
22881 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
22882 +                                  &delegated, /*force*/0);
22883 +               if (unlikely(err == -EWOULDBLOCK)) {
22884 +                       pr_warn("cannot retry for NFSv4 delegation"
22885 +                               " for an internal unlink\n");
22886 +                       iput(delegated);
22887 +               }
22888 +               if (unlikely(err))
22889 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22890 +       }
22891 +       dput(h_path.dentry);
22892 +
22893 +out:
22894 +       AuTraceErr(err);
22895 +       return err;
22896 +}
22897 +
22898 +/*
22899 + * unlink the topmost h_dentry
22900 + */
22901 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22902 +{
22903 +       int err;
22904 +       struct path h_path;
22905 +       struct inode *delegated;
22906 +
22907 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22908 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22909 +       delegated = NULL;
22910 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22911 +       if (unlikely(err == -EWOULDBLOCK)) {
22912 +               pr_warn("cannot retry for NFSv4 delegation"
22913 +                       " for an internal unlink\n");
22914 +               iput(delegated);
22915 +       }
22916 +       if (unlikely(err))
22917 +               AU_MVD_PR(dmsg, "unlink failed\n");
22918 +
22919 +       AuTraceErr(err);
22920 +       return err;
22921 +}
22922 +
22923 +/* Since mvdown succeeded, we ignore an error of this function */
22924 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22925 +{
22926 +       int err;
22927 +       struct au_branch *br;
22928 +
22929 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22930 +       br = au_sbr(a->sb, a->mvd_bsrc);
22931 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22932 +       if (!err) {
22933 +               br = au_sbr(a->sb, a->mvd_bdst);
22934 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22935 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22936 +       }
22937 +       if (!err)
22938 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22939 +       else
22940 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22941 +}
22942 +
22943 +/*
22944 + * copy-down the file and unlink the bsrc file.
22945 + * - unlink the bdst whout if exist
22946 + * - copy-down the file (with whtmp name and rename)
22947 + * - unlink the bsrc file
22948 + */
22949 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22950 +{
22951 +       int err;
22952 +
22953 +       err = au_do_mkdir(dmsg, a);
22954 +       if (!err)
22955 +               err = au_do_lock(dmsg, a);
22956 +       if (unlikely(err))
22957 +               goto out;
22958 +
22959 +       /*
22960 +        * do not revert the activities we made on bdst since they should be
22961 +        * harmless in aufs.
22962 +        */
22963 +
22964 +       err = au_do_cpdown(dmsg, a);
22965 +       if (!err)
22966 +               err = au_do_unlink_wh(dmsg, a);
22967 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22968 +               err = au_do_unlink(dmsg, a);
22969 +       if (unlikely(err))
22970 +               goto out_unlock;
22971 +
22972 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22973 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22974 +       if (find_lower_writable(a) < 0)
22975 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22976 +
22977 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22978 +               au_do_stfs(dmsg, a);
22979 +
22980 +       /* maintain internal array */
22981 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22982 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22983 +               au_set_dbtop(a->dentry, a->mvd_bdst);
22984 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22985 +               au_set_ibtop(a->inode, a->mvd_bdst);
22986 +       } else {
22987 +               /* hide the lower */
22988 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
22989 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
22990 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
22991 +               au_set_ibbot(a->inode, a->mvd_bsrc);
22992 +       }
22993 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
22994 +               au_set_dbbot(a->dentry, a->mvd_bdst);
22995 +       if (au_ibbot(a->inode) < a->mvd_bdst)
22996 +               au_set_ibbot(a->inode, a->mvd_bdst);
22997 +
22998 +out_unlock:
22999 +       au_do_unlock(dmsg, a);
23000 +out:
23001 +       AuTraceErr(err);
23002 +       return err;
23003 +}
23004 +
23005 +/* ---------------------------------------------------------------------- */
23006 +
23007 +/* make sure the file is idle */
23008 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
23009 +{
23010 +       int err, plinked;
23011 +
23012 +       err = 0;
23013 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
23014 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
23015 +           && au_dcount(a->dentry) == 1
23016 +           && atomic_read(&a->inode->i_count) == 1
23017 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
23018 +           && (!plinked || !au_plink_test(a->inode))
23019 +           && a->inode->i_nlink == 1)
23020 +               goto out;
23021 +
23022 +       err = -EBUSY;
23023 +       AU_MVD_PR(dmsg,
23024 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
23025 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
23026 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
23027 +                 a->mvd_h_src_inode->i_nlink,
23028 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
23029 +
23030 +out:
23031 +       AuTraceErr(err);
23032 +       return err;
23033 +}
23034 +
23035 +/* make sure the parent dir is fine */
23036 +static int au_mvd_args_parent(const unsigned char dmsg,
23037 +                             struct au_mvd_args *a)
23038 +{
23039 +       int err;
23040 +       aufs_bindex_t bindex;
23041 +
23042 +       err = 0;
23043 +       if (unlikely(au_alive_dir(a->parent))) {
23044 +               err = -ENOENT;
23045 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
23046 +               goto out;
23047 +       }
23048 +
23049 +       a->bopq = au_dbdiropq(a->parent);
23050 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
23051 +       AuDbg("b%d\n", bindex);
23052 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
23053 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
23054 +               err = -EINVAL;
23055 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
23056 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
23057 +                         a->bopq, a->mvd_bdst);
23058 +       }
23059 +
23060 +out:
23061 +       AuTraceErr(err);
23062 +       return err;
23063 +}
23064 +
23065 +static int au_mvd_args_intermediate(const unsigned char dmsg,
23066 +                                   struct au_mvd_args *a)
23067 +{
23068 +       int err;
23069 +       struct au_dinfo *dinfo, *tmp;
23070 +
23071 +       /* lookup the next lower positive entry */
23072 +       err = -ENOMEM;
23073 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
23074 +       if (unlikely(!tmp))
23075 +               goto out;
23076 +
23077 +       a->bfound = -1;
23078 +       a->bwh = -1;
23079 +       dinfo = au_di(a->dentry);
23080 +       au_di_cp(tmp, dinfo);
23081 +       au_di_swap(tmp, dinfo);
23082 +
23083 +       /* returns the number of positive dentries */
23084 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
23085 +                            /* AuLkup_IGNORE_PERM */ 0);
23086 +       if (!err)
23087 +               a->bwh = au_dbwh(a->dentry);
23088 +       else if (err > 0)
23089 +               a->bfound = au_dbtop(a->dentry);
23090 +
23091 +       au_di_swap(tmp, dinfo);
23092 +       au_rw_write_unlock(&tmp->di_rwsem);
23093 +       au_di_free(tmp);
23094 +       if (unlikely(err < 0))
23095 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
23096 +
23097 +       /*
23098 +        * here, we have these cases.
23099 +        * bfound == -1
23100 +        *      no positive dentry under bsrc. there are more sub-cases.
23101 +        *      bwh < 0
23102 +        *              there no whiteout, we can safely move-down.
23103 +        *      bwh <= bsrc
23104 +        *              impossible
23105 +        *      bsrc < bwh && bwh < bdst
23106 +        *              there is a whiteout on RO branch. cannot proceed.
23107 +        *      bwh == bdst
23108 +        *              there is a whiteout on the RW target branch. it should
23109 +        *              be removed.
23110 +        *      bdst < bwh
23111 +        *              there is a whiteout somewhere unrelated branch.
23112 +        * -1 < bfound && bfound <= bsrc
23113 +        *      impossible.
23114 +        * bfound < bdst
23115 +        *      found, but it is on RO branch between bsrc and bdst. cannot
23116 +        *      proceed.
23117 +        * bfound == bdst
23118 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
23119 +        *      error.
23120 +        * bdst < bfound
23121 +        *      found, after we create the file on bdst, it will be hidden.
23122 +        */
23123 +
23124 +       AuDebugOn(a->bfound == -1
23125 +                 && a->bwh != -1
23126 +                 && a->bwh <= a->mvd_bsrc);
23127 +       AuDebugOn(-1 < a->bfound
23128 +                 && a->bfound <= a->mvd_bsrc);
23129 +
23130 +       err = -EINVAL;
23131 +       if (a->bfound == -1
23132 +           && a->mvd_bsrc < a->bwh
23133 +           && a->bwh != -1
23134 +           && a->bwh < a->mvd_bdst) {
23135 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
23136 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
23137 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
23138 +               goto out;
23139 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
23140 +               a->mvd_errno = EAU_MVDOWN_UPPER;
23141 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
23142 +                         a->mvd_bdst, a->bfound);
23143 +               goto out;
23144 +       }
23145 +
23146 +       err = 0; /* success */
23147 +
23148 +out:
23149 +       AuTraceErr(err);
23150 +       return err;
23151 +}
23152 +
23153 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
23154 +{
23155 +       int err;
23156 +
23157 +       err = 0;
23158 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
23159 +           && a->bfound == a->mvd_bdst)
23160 +               err = -EEXIST;
23161 +       AuTraceErr(err);
23162 +       return err;
23163 +}
23164 +
23165 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
23166 +{
23167 +       int err;
23168 +       struct au_branch *br;
23169 +
23170 +       err = -EISDIR;
23171 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
23172 +               goto out;
23173 +
23174 +       err = -EINVAL;
23175 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
23176 +               a->mvd_bsrc = au_ibtop(a->inode);
23177 +       else {
23178 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
23179 +               if (unlikely(a->mvd_bsrc < 0
23180 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
23181 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
23182 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
23183 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
23184 +                                || au_ibbot(a->inode) < a->mvd_bsrc
23185 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
23186 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
23187 +                       AU_MVD_PR(dmsg, "no upper\n");
23188 +                       goto out;
23189 +               }
23190 +       }
23191 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
23192 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
23193 +               AU_MVD_PR(dmsg, "on the bottom\n");
23194 +               goto out;
23195 +       }
23196 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
23197 +       br = au_sbr(a->sb, a->mvd_bsrc);
23198 +       err = au_br_rdonly(br);
23199 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
23200 +               if (unlikely(err))
23201 +                       goto out;
23202 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
23203 +                    || IS_APPEND(a->mvd_h_src_inode))) {
23204 +               if (err)
23205 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
23206 +               /* go on */
23207 +       } else
23208 +               goto out;
23209 +
23210 +       err = -EINVAL;
23211 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
23212 +               a->mvd_bdst = find_lower_writable(a);
23213 +               if (unlikely(a->mvd_bdst < 0)) {
23214 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
23215 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
23216 +                       goto out;
23217 +               }
23218 +       } else {
23219 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
23220 +               if (unlikely(a->mvd_bdst < 0
23221 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
23222 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
23223 +                       AU_MVD_PR(dmsg, "no lower brid\n");
23224 +                       goto out;
23225 +               }
23226 +       }
23227 +
23228 +       err = au_mvd_args_busy(dmsg, a);
23229 +       if (!err)
23230 +               err = au_mvd_args_parent(dmsg, a);
23231 +       if (!err)
23232 +               err = au_mvd_args_intermediate(dmsg, a);
23233 +       if (!err)
23234 +               err = au_mvd_args_exist(dmsg, a);
23235 +       if (!err)
23236 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
23237 +
23238 +out:
23239 +       AuTraceErr(err);
23240 +       return err;
23241 +}
23242 +
23243 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
23244 +{
23245 +       int err, e;
23246 +       unsigned char dmsg;
23247 +       struct au_mvd_args *args;
23248 +       struct inode *inode;
23249 +
23250 +       inode = d_inode(dentry);
23251 +       err = -EPERM;
23252 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
23253 +               goto out;
23254 +
23255 +       err = -ENOMEM;
23256 +       args = kmalloc(sizeof(*args), GFP_NOFS);
23257 +       if (unlikely(!args))
23258 +               goto out;
23259 +
23260 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
23261 +       if (!err)
23262 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
23263 +       if (unlikely(err)) {
23264 +               err = -EFAULT;
23265 +               AuTraceErr(err);
23266 +               goto out_free;
23267 +       }
23268 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
23269 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
23270 +       args->mvdown.au_errno = 0;
23271 +       args->dentry = dentry;
23272 +       args->inode = inode;
23273 +       args->sb = dentry->d_sb;
23274 +
23275 +       err = -ENOENT;
23276 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
23277 +       args->parent = dget_parent(dentry);
23278 +       args->dir = d_inode(args->parent);
23279 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
23280 +       dput(args->parent);
23281 +       if (unlikely(args->parent != dentry->d_parent)) {
23282 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
23283 +               goto out_dir;
23284 +       }
23285 +
23286 +       inode_lock_nested(inode, I_MUTEX_CHILD);
23287 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
23288 +       if (unlikely(err))
23289 +               goto out_inode;
23290 +
23291 +       di_write_lock_parent(args->parent);
23292 +       err = au_mvd_args(dmsg, args);
23293 +       if (unlikely(err))
23294 +               goto out_parent;
23295 +
23296 +       err = au_do_mvdown(dmsg, args);
23297 +       if (unlikely(err))
23298 +               goto out_parent;
23299 +
23300 +       au_cpup_attr_timesizes(args->dir);
23301 +       au_cpup_attr_timesizes(inode);
23302 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
23303 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
23304 +       /* au_digen_dec(dentry); */
23305 +
23306 +out_parent:
23307 +       di_write_unlock(args->parent);
23308 +       aufs_read_unlock(dentry, AuLock_DW);
23309 +out_inode:
23310 +       inode_unlock(inode);
23311 +out_dir:
23312 +       inode_unlock(args->dir);
23313 +out_free:
23314 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
23315 +       if (unlikely(e))
23316 +               err = -EFAULT;
23317 +       kfree(args);
23318 +out:
23319 +       AuTraceErr(err);
23320 +       return err;
23321 +}
23322 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
23323 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
23324 +++ linux/fs/aufs/opts.c        2017-07-29 12:14:25.903042072 +0200
23325 @@ -0,0 +1,1846 @@
23326 +/*
23327 + * Copyright (C) 2005-2017 Junjiro R. Okajima
23328 + *
23329 + * This program, aufs is free software; you can redistribute it and/or modify
23330 + * it under the terms of the GNU General Public License as published by
23331 + * the Free Software Foundation; either version 2 of the License, or
23332 + * (at your option) any later version.
23333 + *
23334 + * This program is distributed in the hope that it will be useful,
23335 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23336 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23337 + * GNU General Public License for more details.
23338 + *
23339 + * You should have received a copy of the GNU General Public License
23340 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23341 + */
23342 +
23343 +/*
23344 + * mount options/flags
23345 + */
23346 +
23347 +#include <linux/namei.h>
23348 +#include <linux/types.h> /* a distribution requires */
23349 +#include <linux/parser.h>
23350 +#include "aufs.h"
23351 +
23352 +/* ---------------------------------------------------------------------- */
23353 +
23354 +enum {
23355 +       Opt_br,
23356 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
23357 +       Opt_idel, Opt_imod,
23358 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
23359 +       Opt_rdblk_def, Opt_rdhash_def,
23360 +       Opt_xino, Opt_noxino,
23361 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
23362 +       Opt_trunc_xino_path, Opt_itrunc_xino,
23363 +       Opt_trunc_xib, Opt_notrunc_xib,
23364 +       Opt_shwh, Opt_noshwh,
23365 +       Opt_plink, Opt_noplink, Opt_list_plink,
23366 +       Opt_udba,
23367 +       Opt_dio, Opt_nodio,
23368 +       Opt_diropq_a, Opt_diropq_w,
23369 +       Opt_warn_perm, Opt_nowarn_perm,
23370 +       Opt_wbr_copyup, Opt_wbr_create,
23371 +       Opt_fhsm_sec,
23372 +       Opt_verbose, Opt_noverbose,
23373 +       Opt_sum, Opt_nosum, Opt_wsum,
23374 +       Opt_dirperm1, Opt_nodirperm1,
23375 +       Opt_acl, Opt_noacl,
23376 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
23377 +};
23378 +
23379 +static match_table_t options = {
23380 +       {Opt_br, "br=%s"},
23381 +       {Opt_br, "br:%s"},
23382 +
23383 +       {Opt_add, "add=%d:%s"},
23384 +       {Opt_add, "add:%d:%s"},
23385 +       {Opt_add, "ins=%d:%s"},
23386 +       {Opt_add, "ins:%d:%s"},
23387 +       {Opt_append, "append=%s"},
23388 +       {Opt_append, "append:%s"},
23389 +       {Opt_prepend, "prepend=%s"},
23390 +       {Opt_prepend, "prepend:%s"},
23391 +
23392 +       {Opt_del, "del=%s"},
23393 +       {Opt_del, "del:%s"},
23394 +       /* {Opt_idel, "idel:%d"}, */
23395 +       {Opt_mod, "mod=%s"},
23396 +       {Opt_mod, "mod:%s"},
23397 +       /* {Opt_imod, "imod:%d:%s"}, */
23398 +
23399 +       {Opt_dirwh, "dirwh=%d"},
23400 +
23401 +       {Opt_xino, "xino=%s"},
23402 +       {Opt_noxino, "noxino"},
23403 +       {Opt_trunc_xino, "trunc_xino"},
23404 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
23405 +       {Opt_notrunc_xino, "notrunc_xino"},
23406 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
23407 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
23408 +       /* {Opt_zxino, "zxino=%s"}, */
23409 +       {Opt_trunc_xib, "trunc_xib"},
23410 +       {Opt_notrunc_xib, "notrunc_xib"},
23411 +
23412 +#ifdef CONFIG_PROC_FS
23413 +       {Opt_plink, "plink"},
23414 +#else
23415 +       {Opt_ignore_silent, "plink"},
23416 +#endif
23417 +
23418 +       {Opt_noplink, "noplink"},
23419 +
23420 +#ifdef CONFIG_AUFS_DEBUG
23421 +       {Opt_list_plink, "list_plink"},
23422 +#endif
23423 +
23424 +       {Opt_udba, "udba=%s"},
23425 +
23426 +       {Opt_dio, "dio"},
23427 +       {Opt_nodio, "nodio"},
23428 +
23429 +#ifdef CONFIG_AUFS_FHSM
23430 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
23431 +#else
23432 +       {Opt_ignore_silent, "fhsm_sec=%d"},
23433 +#endif
23434 +
23435 +       {Opt_diropq_a, "diropq=always"},
23436 +       {Opt_diropq_a, "diropq=a"},
23437 +       {Opt_diropq_w, "diropq=whiteouted"},
23438 +       {Opt_diropq_w, "diropq=w"},
23439 +
23440 +       {Opt_warn_perm, "warn_perm"},
23441 +       {Opt_nowarn_perm, "nowarn_perm"},
23442 +
23443 +       /* keep them temporary */
23444 +       {Opt_ignore_silent, "nodlgt"},
23445 +       {Opt_ignore_silent, "clean_plink"},
23446 +
23447 +#ifdef CONFIG_AUFS_SHWH
23448 +       {Opt_shwh, "shwh"},
23449 +#endif
23450 +       {Opt_noshwh, "noshwh"},
23451 +
23452 +       {Opt_dirperm1, "dirperm1"},
23453 +       {Opt_nodirperm1, "nodirperm1"},
23454 +
23455 +       {Opt_verbose, "verbose"},
23456 +       {Opt_verbose, "v"},
23457 +       {Opt_noverbose, "noverbose"},
23458 +       {Opt_noverbose, "quiet"},
23459 +       {Opt_noverbose, "q"},
23460 +       {Opt_noverbose, "silent"},
23461 +
23462 +       {Opt_sum, "sum"},
23463 +       {Opt_nosum, "nosum"},
23464 +       {Opt_wsum, "wsum"},
23465 +
23466 +       {Opt_rdcache, "rdcache=%d"},
23467 +       {Opt_rdblk, "rdblk=%d"},
23468 +       {Opt_rdblk_def, "rdblk=def"},
23469 +       {Opt_rdhash, "rdhash=%d"},
23470 +       {Opt_rdhash_def, "rdhash=def"},
23471 +
23472 +       {Opt_wbr_create, "create=%s"},
23473 +       {Opt_wbr_create, "create_policy=%s"},
23474 +       {Opt_wbr_copyup, "cpup=%s"},
23475 +       {Opt_wbr_copyup, "copyup=%s"},
23476 +       {Opt_wbr_copyup, "copyup_policy=%s"},
23477 +
23478 +       /* generic VFS flag */
23479 +#ifdef CONFIG_FS_POSIX_ACL
23480 +       {Opt_acl, "acl"},
23481 +       {Opt_noacl, "noacl"},
23482 +#else
23483 +       {Opt_ignore_silent, "acl"},
23484 +       {Opt_ignore_silent, "noacl"},
23485 +#endif
23486 +
23487 +       /* internal use for the scripts */
23488 +       {Opt_ignore_silent, "si=%s"},
23489 +
23490 +       {Opt_br, "dirs=%s"},
23491 +       {Opt_ignore, "debug=%d"},
23492 +       {Opt_ignore, "delete=whiteout"},
23493 +       {Opt_ignore, "delete=all"},
23494 +       {Opt_ignore, "imap=%s"},
23495 +
23496 +       /* temporary workaround, due to old mount(8)? */
23497 +       {Opt_ignore_silent, "relatime"},
23498 +
23499 +       {Opt_err, NULL}
23500 +};
23501 +
23502 +/* ---------------------------------------------------------------------- */
23503 +
23504 +static const char *au_parser_pattern(int val, match_table_t tbl)
23505 +{
23506 +       struct match_token *p;
23507 +
23508 +       p = tbl;
23509 +       while (p->pattern) {
23510 +               if (p->token == val)
23511 +                       return p->pattern;
23512 +               p++;
23513 +       }
23514 +       BUG();
23515 +       return "??";
23516 +}
23517 +
23518 +static const char *au_optstr(int *val, match_table_t tbl)
23519 +{
23520 +       struct match_token *p;
23521 +       int v;
23522 +
23523 +       v = *val;
23524 +       if (!v)
23525 +               goto out;
23526 +       p = tbl;
23527 +       while (p->pattern) {
23528 +               if (p->token
23529 +                   && (v & p->token) == p->token) {
23530 +                       *val &= ~p->token;
23531 +                       return p->pattern;
23532 +               }
23533 +               p++;
23534 +       }
23535 +
23536 +out:
23537 +       return NULL;
23538 +}
23539 +
23540 +/* ---------------------------------------------------------------------- */
23541 +
23542 +static match_table_t brperm = {
23543 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
23544 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
23545 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
23546 +       {0, NULL}
23547 +};
23548 +
23549 +static match_table_t brattr = {
23550 +       /* general */
23551 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
23552 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
23553 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
23554 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
23555 +#ifdef CONFIG_AUFS_FHSM
23556 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
23557 +#endif
23558 +#ifdef CONFIG_AUFS_XATTR
23559 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
23560 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
23561 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
23562 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
23563 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
23564 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
23565 +#endif
23566 +
23567 +       /* ro/rr branch */
23568 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
23569 +
23570 +       /* rw branch */
23571 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
23572 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
23573 +
23574 +       {0, NULL}
23575 +};
23576 +
23577 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
23578 +{
23579 +       int attr, v;
23580 +       char *p;
23581 +
23582 +       attr = 0;
23583 +       do {
23584 +               p = strchr(str, '+');
23585 +               if (p)
23586 +                       *p = 0;
23587 +               v = match_token(str, table, args);
23588 +               if (v) {
23589 +                       if (v & AuBrAttr_CMOO_Mask)
23590 +                               attr &= ~AuBrAttr_CMOO_Mask;
23591 +                       attr |= v;
23592 +               } else {
23593 +                       if (p)
23594 +                               *p = '+';
23595 +                       pr_warn("ignored branch attribute %s\n", str);
23596 +                       break;
23597 +               }
23598 +               if (p)
23599 +                       str = p + 1;
23600 +       } while (p);
23601 +
23602 +       return attr;
23603 +}
23604 +
23605 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
23606 +{
23607 +       int sz;
23608 +       const char *p;
23609 +       char *q;
23610 +
23611 +       q = str->a;
23612 +       *q = 0;
23613 +       p = au_optstr(&perm, brattr);
23614 +       if (p) {
23615 +               sz = strlen(p);
23616 +               memcpy(q, p, sz + 1);
23617 +               q += sz;
23618 +       } else
23619 +               goto out;
23620 +
23621 +       do {
23622 +               p = au_optstr(&perm, brattr);
23623 +               if (p) {
23624 +                       *q++ = '+';
23625 +                       sz = strlen(p);
23626 +                       memcpy(q, p, sz + 1);
23627 +                       q += sz;
23628 +               }
23629 +       } while (p);
23630 +
23631 +out:
23632 +       return q - str->a;
23633 +}
23634 +
23635 +static int noinline_for_stack br_perm_val(char *perm)
23636 +{
23637 +       int val, bad, sz;
23638 +       char *p;
23639 +       substring_t args[MAX_OPT_ARGS];
23640 +       au_br_perm_str_t attr;
23641 +
23642 +       p = strchr(perm, '+');
23643 +       if (p)
23644 +               *p = 0;
23645 +       val = match_token(perm, brperm, args);
23646 +       if (!val) {
23647 +               if (p)
23648 +                       *p = '+';
23649 +               pr_warn("ignored branch permission %s\n", perm);
23650 +               val = AuBrPerm_RO;
23651 +               goto out;
23652 +       }
23653 +       if (!p)
23654 +               goto out;
23655 +
23656 +       val |= br_attr_val(p + 1, brattr, args);
23657 +
23658 +       bad = 0;
23659 +       switch (val & AuBrPerm_Mask) {
23660 +       case AuBrPerm_RO:
23661 +       case AuBrPerm_RR:
23662 +               bad = val & AuBrWAttr_Mask;
23663 +               val &= ~AuBrWAttr_Mask;
23664 +               break;
23665 +       case AuBrPerm_RW:
23666 +               bad = val & AuBrRAttr_Mask;
23667 +               val &= ~AuBrRAttr_Mask;
23668 +               break;
23669 +       }
23670 +
23671 +       /*
23672 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23673 +        * does not treat it as an error, just warning.
23674 +        * this is a tiny guard for the user operation.
23675 +        */
23676 +       if (val & AuBrAttr_UNPIN) {
23677 +               bad |= AuBrAttr_UNPIN;
23678 +               val &= ~AuBrAttr_UNPIN;
23679 +       }
23680 +
23681 +       if (unlikely(bad)) {
23682 +               sz = au_do_optstr_br_attr(&attr, bad);
23683 +               AuDebugOn(!sz);
23684 +               pr_warn("ignored branch attribute %s\n", attr.a);
23685 +       }
23686 +
23687 +out:
23688 +       return val;
23689 +}
23690 +
23691 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
23692 +{
23693 +       au_br_perm_str_t attr;
23694 +       const char *p;
23695 +       char *q;
23696 +       int sz;
23697 +
23698 +       q = str->a;
23699 +       p = au_optstr(&perm, brperm);
23700 +       AuDebugOn(!p || !*p);
23701 +       sz = strlen(p);
23702 +       memcpy(q, p, sz + 1);
23703 +       q += sz;
23704 +
23705 +       sz = au_do_optstr_br_attr(&attr, perm);
23706 +       if (sz) {
23707 +               *q++ = '+';
23708 +               memcpy(q, attr.a, sz + 1);
23709 +       }
23710 +
23711 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
23712 +}
23713 +
23714 +/* ---------------------------------------------------------------------- */
23715 +
23716 +static match_table_t udbalevel = {
23717 +       {AuOpt_UDBA_REVAL, "reval"},
23718 +       {AuOpt_UDBA_NONE, "none"},
23719 +#ifdef CONFIG_AUFS_HNOTIFY
23720 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23721 +#ifdef CONFIG_AUFS_HFSNOTIFY
23722 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
23723 +#endif
23724 +#endif
23725 +       {-1, NULL}
23726 +};
23727 +
23728 +static int noinline_for_stack udba_val(char *str)
23729 +{
23730 +       substring_t args[MAX_OPT_ARGS];
23731 +
23732 +       return match_token(str, udbalevel, args);
23733 +}
23734 +
23735 +const char *au_optstr_udba(int udba)
23736 +{
23737 +       return au_parser_pattern(udba, udbalevel);
23738 +}
23739 +
23740 +/* ---------------------------------------------------------------------- */
23741 +
23742 +static match_table_t au_wbr_create_policy = {
23743 +       {AuWbrCreate_TDP, "tdp"},
23744 +       {AuWbrCreate_TDP, "top-down-parent"},
23745 +       {AuWbrCreate_RR, "rr"},
23746 +       {AuWbrCreate_RR, "round-robin"},
23747 +       {AuWbrCreate_MFS, "mfs"},
23748 +       {AuWbrCreate_MFS, "most-free-space"},
23749 +       {AuWbrCreate_MFSV, "mfs:%d"},
23750 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
23751 +
23752 +       /* top-down regardless the parent, and then mfs */
23753 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
23754 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
23755 +
23756 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
23757 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23758 +       {AuWbrCreate_PMFS, "pmfs"},
23759 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
23760 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23761 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
23762 +
23763 +       {-1, NULL}
23764 +};
23765 +
23766 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23767 +                           struct au_opt_wbr_create *create)
23768 +{
23769 +       int err;
23770 +       unsigned long long ull;
23771 +
23772 +       err = 0;
23773 +       if (!match_u64(arg, &ull))
23774 +               create->mfsrr_watermark = ull;
23775 +       else {
23776 +               pr_err("bad integer in %s\n", str);
23777 +               err = -EINVAL;
23778 +       }
23779 +
23780 +       return err;
23781 +}
23782 +
23783 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23784 +                         struct au_opt_wbr_create *create)
23785 +{
23786 +       int n, err;
23787 +
23788 +       err = 0;
23789 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23790 +               create->mfs_second = n;
23791 +       else {
23792 +               pr_err("bad integer in %s\n", str);
23793 +               err = -EINVAL;
23794 +       }
23795 +
23796 +       return err;
23797 +}
23798 +
23799 +static int noinline_for_stack
23800 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23801 +{
23802 +       int err, e;
23803 +       substring_t args[MAX_OPT_ARGS];
23804 +
23805 +       err = match_token(str, au_wbr_create_policy, args);
23806 +       create->wbr_create = err;
23807 +       switch (err) {
23808 +       case AuWbrCreate_MFSRRV:
23809 +       case AuWbrCreate_TDMFSV:
23810 +       case AuWbrCreate_PMFSRRV:
23811 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23812 +               if (!e)
23813 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23814 +               if (unlikely(e))
23815 +                       err = e;
23816 +               break;
23817 +       case AuWbrCreate_MFSRR:
23818 +       case AuWbrCreate_TDMFS:
23819 +       case AuWbrCreate_PMFSRR:
23820 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23821 +               if (unlikely(e)) {
23822 +                       err = e;
23823 +                       break;
23824 +               }
23825 +               /*FALLTHROUGH*/
23826 +       case AuWbrCreate_MFS:
23827 +       case AuWbrCreate_PMFS:
23828 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23829 +               break;
23830 +       case AuWbrCreate_MFSV:
23831 +       case AuWbrCreate_PMFSV:
23832 +               e = au_wbr_mfs_sec(&args[0], str, create);
23833 +               if (unlikely(e))
23834 +                       err = e;
23835 +               break;
23836 +       }
23837 +
23838 +       return err;
23839 +}
23840 +
23841 +const char *au_optstr_wbr_create(int wbr_create)
23842 +{
23843 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23844 +}
23845 +
23846 +static match_table_t au_wbr_copyup_policy = {
23847 +       {AuWbrCopyup_TDP, "tdp"},
23848 +       {AuWbrCopyup_TDP, "top-down-parent"},
23849 +       {AuWbrCopyup_BUP, "bup"},
23850 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23851 +       {AuWbrCopyup_BU, "bu"},
23852 +       {AuWbrCopyup_BU, "bottom-up"},
23853 +       {-1, NULL}
23854 +};
23855 +
23856 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23857 +{
23858 +       substring_t args[MAX_OPT_ARGS];
23859 +
23860 +       return match_token(str, au_wbr_copyup_policy, args);
23861 +}
23862 +
23863 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23864 +{
23865 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23866 +}
23867 +
23868 +/* ---------------------------------------------------------------------- */
23869 +
23870 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23871 +
23872 +static void dump_opts(struct au_opts *opts)
23873 +{
23874 +#ifdef CONFIG_AUFS_DEBUG
23875 +       /* reduce stack space */
23876 +       union {
23877 +               struct au_opt_add *add;
23878 +               struct au_opt_del *del;
23879 +               struct au_opt_mod *mod;
23880 +               struct au_opt_xino *xino;
23881 +               struct au_opt_xino_itrunc *xino_itrunc;
23882 +               struct au_opt_wbr_create *create;
23883 +       } u;
23884 +       struct au_opt *opt;
23885 +
23886 +       opt = opts->opt;
23887 +       while (opt->type != Opt_tail) {
23888 +               switch (opt->type) {
23889 +               case Opt_add:
23890 +                       u.add = &opt->add;
23891 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23892 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23893 +                                 u.add->path.dentry);
23894 +                       break;
23895 +               case Opt_del:
23896 +               case Opt_idel:
23897 +                       u.del = &opt->del;
23898 +                       AuDbg("del {%s, %p}\n",
23899 +                             u.del->pathname, u.del->h_path.dentry);
23900 +                       break;
23901 +               case Opt_mod:
23902 +               case Opt_imod:
23903 +                       u.mod = &opt->mod;
23904 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23905 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23906 +                       break;
23907 +               case Opt_append:
23908 +                       u.add = &opt->add;
23909 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23910 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23911 +                                 u.add->path.dentry);
23912 +                       break;
23913 +               case Opt_prepend:
23914 +                       u.add = &opt->add;
23915 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23916 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23917 +                                 u.add->path.dentry);
23918 +                       break;
23919 +               case Opt_dirwh:
23920 +                       AuDbg("dirwh %d\n", opt->dirwh);
23921 +                       break;
23922 +               case Opt_rdcache:
23923 +                       AuDbg("rdcache %d\n", opt->rdcache);
23924 +                       break;
23925 +               case Opt_rdblk:
23926 +                       AuDbg("rdblk %u\n", opt->rdblk);
23927 +                       break;
23928 +               case Opt_rdblk_def:
23929 +                       AuDbg("rdblk_def\n");
23930 +                       break;
23931 +               case Opt_rdhash:
23932 +                       AuDbg("rdhash %u\n", opt->rdhash);
23933 +                       break;
23934 +               case Opt_rdhash_def:
23935 +                       AuDbg("rdhash_def\n");
23936 +                       break;
23937 +               case Opt_xino:
23938 +                       u.xino = &opt->xino;
23939 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23940 +                       break;
23941 +               case Opt_trunc_xino:
23942 +                       AuLabel(trunc_xino);
23943 +                       break;
23944 +               case Opt_notrunc_xino:
23945 +                       AuLabel(notrunc_xino);
23946 +                       break;
23947 +               case Opt_trunc_xino_path:
23948 +               case Opt_itrunc_xino:
23949 +                       u.xino_itrunc = &opt->xino_itrunc;
23950 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23951 +                       break;
23952 +               case Opt_noxino:
23953 +                       AuLabel(noxino);
23954 +                       break;
23955 +               case Opt_trunc_xib:
23956 +                       AuLabel(trunc_xib);
23957 +                       break;
23958 +               case Opt_notrunc_xib:
23959 +                       AuLabel(notrunc_xib);
23960 +                       break;
23961 +               case Opt_shwh:
23962 +                       AuLabel(shwh);
23963 +                       break;
23964 +               case Opt_noshwh:
23965 +                       AuLabel(noshwh);
23966 +                       break;
23967 +               case Opt_dirperm1:
23968 +                       AuLabel(dirperm1);
23969 +                       break;
23970 +               case Opt_nodirperm1:
23971 +                       AuLabel(nodirperm1);
23972 +                       break;
23973 +               case Opt_plink:
23974 +                       AuLabel(plink);
23975 +                       break;
23976 +               case Opt_noplink:
23977 +                       AuLabel(noplink);
23978 +                       break;
23979 +               case Opt_list_plink:
23980 +                       AuLabel(list_plink);
23981 +                       break;
23982 +               case Opt_udba:
23983 +                       AuDbg("udba %d, %s\n",
23984 +                                 opt->udba, au_optstr_udba(opt->udba));
23985 +                       break;
23986 +               case Opt_dio:
23987 +                       AuLabel(dio);
23988 +                       break;
23989 +               case Opt_nodio:
23990 +                       AuLabel(nodio);
23991 +                       break;
23992 +               case Opt_diropq_a:
23993 +                       AuLabel(diropq_a);
23994 +                       break;
23995 +               case Opt_diropq_w:
23996 +                       AuLabel(diropq_w);
23997 +                       break;
23998 +               case Opt_warn_perm:
23999 +                       AuLabel(warn_perm);
24000 +                       break;
24001 +               case Opt_nowarn_perm:
24002 +                       AuLabel(nowarn_perm);
24003 +                       break;
24004 +               case Opt_verbose:
24005 +                       AuLabel(verbose);
24006 +                       break;
24007 +               case Opt_noverbose:
24008 +                       AuLabel(noverbose);
24009 +                       break;
24010 +               case Opt_sum:
24011 +                       AuLabel(sum);
24012 +                       break;
24013 +               case Opt_nosum:
24014 +                       AuLabel(nosum);
24015 +                       break;
24016 +               case Opt_wsum:
24017 +                       AuLabel(wsum);
24018 +                       break;
24019 +               case Opt_wbr_create:
24020 +                       u.create = &opt->wbr_create;
24021 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
24022 +                                 au_optstr_wbr_create(u.create->wbr_create));
24023 +                       switch (u.create->wbr_create) {
24024 +                       case AuWbrCreate_MFSV:
24025 +                       case AuWbrCreate_PMFSV:
24026 +                               AuDbg("%d sec\n", u.create->mfs_second);
24027 +                               break;
24028 +                       case AuWbrCreate_MFSRR:
24029 +                       case AuWbrCreate_TDMFS:
24030 +                               AuDbg("%llu watermark\n",
24031 +                                         u.create->mfsrr_watermark);
24032 +                               break;
24033 +                       case AuWbrCreate_MFSRRV:
24034 +                       case AuWbrCreate_TDMFSV:
24035 +                       case AuWbrCreate_PMFSRRV:
24036 +                               AuDbg("%llu watermark, %d sec\n",
24037 +                                         u.create->mfsrr_watermark,
24038 +                                         u.create->mfs_second);
24039 +                               break;
24040 +                       }
24041 +                       break;
24042 +               case Opt_wbr_copyup:
24043 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
24044 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
24045 +                       break;
24046 +               case Opt_fhsm_sec:
24047 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
24048 +                       break;
24049 +               case Opt_acl:
24050 +                       AuLabel(acl);
24051 +                       break;
24052 +               case Opt_noacl:
24053 +                       AuLabel(noacl);
24054 +                       break;
24055 +               default:
24056 +                       BUG();
24057 +               }
24058 +               opt++;
24059 +       }
24060 +#endif
24061 +}
24062 +
24063 +void au_opts_free(struct au_opts *opts)
24064 +{
24065 +       struct au_opt *opt;
24066 +
24067 +       opt = opts->opt;
24068 +       while (opt->type != Opt_tail) {
24069 +               switch (opt->type) {
24070 +               case Opt_add:
24071 +               case Opt_append:
24072 +               case Opt_prepend:
24073 +                       path_put(&opt->add.path);
24074 +                       break;
24075 +               case Opt_del:
24076 +               case Opt_idel:
24077 +                       path_put(&opt->del.h_path);
24078 +                       break;
24079 +               case Opt_mod:
24080 +               case Opt_imod:
24081 +                       dput(opt->mod.h_root);
24082 +                       break;
24083 +               case Opt_xino:
24084 +                       fput(opt->xino.file);
24085 +                       break;
24086 +               }
24087 +               opt++;
24088 +       }
24089 +}
24090 +
24091 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
24092 +                  aufs_bindex_t bindex)
24093 +{
24094 +       int err;
24095 +       struct au_opt_add *add = &opt->add;
24096 +       char *p;
24097 +
24098 +       add->bindex = bindex;
24099 +       add->perm = AuBrPerm_RO;
24100 +       add->pathname = opt_str;
24101 +       p = strchr(opt_str, '=');
24102 +       if (p) {
24103 +               *p++ = 0;
24104 +               if (*p)
24105 +                       add->perm = br_perm_val(p);
24106 +       }
24107 +
24108 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
24109 +       if (!err) {
24110 +               if (!p) {
24111 +                       add->perm = AuBrPerm_RO;
24112 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
24113 +                               add->perm = AuBrPerm_RR;
24114 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
24115 +                               add->perm = AuBrPerm_RW;
24116 +               }
24117 +               opt->type = Opt_add;
24118 +               goto out;
24119 +       }
24120 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
24121 +       err = -EINVAL;
24122 +
24123 +out:
24124 +       return err;
24125 +}
24126 +
24127 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
24128 +{
24129 +       int err;
24130 +
24131 +       del->pathname = args[0].from;
24132 +       AuDbg("del path %s\n", del->pathname);
24133 +
24134 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
24135 +       if (unlikely(err))
24136 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
24137 +
24138 +       return err;
24139 +}
24140 +
24141 +#if 0 /* reserved for future use */
24142 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
24143 +                             struct au_opt_del *del, substring_t args[])
24144 +{
24145 +       int err;
24146 +       struct dentry *root;
24147 +
24148 +       err = -EINVAL;
24149 +       root = sb->s_root;
24150 +       aufs_read_lock(root, AuLock_FLUSH);
24151 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
24152 +               pr_err("out of bounds, %d\n", bindex);
24153 +               goto out;
24154 +       }
24155 +
24156 +       err = 0;
24157 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
24158 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
24159 +
24160 +out:
24161 +       aufs_read_unlock(root, !AuLock_IR);
24162 +       return err;
24163 +}
24164 +#endif
24165 +
24166 +static int noinline_for_stack
24167 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
24168 +{
24169 +       int err;
24170 +       struct path path;
24171 +       char *p;
24172 +
24173 +       err = -EINVAL;
24174 +       mod->path = args[0].from;
24175 +       p = strchr(mod->path, '=');
24176 +       if (unlikely(!p)) {
24177 +               pr_err("no permssion %s\n", args[0].from);
24178 +               goto out;
24179 +       }
24180 +
24181 +       *p++ = 0;
24182 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
24183 +       if (unlikely(err)) {
24184 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
24185 +               goto out;
24186 +       }
24187 +
24188 +       mod->perm = br_perm_val(p);
24189 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
24190 +       mod->h_root = dget(path.dentry);
24191 +       path_put(&path);
24192 +
24193 +out:
24194 +       return err;
24195 +}
24196 +
24197 +#if 0 /* reserved for future use */
24198 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
24199 +                             struct au_opt_mod *mod, substring_t args[])
24200 +{
24201 +       int err;
24202 +       struct dentry *root;
24203 +
24204 +       err = -EINVAL;
24205 +       root = sb->s_root;
24206 +       aufs_read_lock(root, AuLock_FLUSH);
24207 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
24208 +               pr_err("out of bounds, %d\n", bindex);
24209 +               goto out;
24210 +       }
24211 +
24212 +       err = 0;
24213 +       mod->perm = br_perm_val(args[1].from);
24214 +       AuDbg("mod path %s, perm 0x%x, %s\n",
24215 +             mod->path, mod->perm, args[1].from);
24216 +       mod->h_root = dget(au_h_dptr(root, bindex));
24217 +
24218 +out:
24219 +       aufs_read_unlock(root, !AuLock_IR);
24220 +       return err;
24221 +}
24222 +#endif
24223 +
24224 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
24225 +                             substring_t args[])
24226 +{
24227 +       int err;
24228 +       struct file *file;
24229 +
24230 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
24231 +       err = PTR_ERR(file);
24232 +       if (IS_ERR(file))
24233 +               goto out;
24234 +
24235 +       err = -EINVAL;
24236 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
24237 +               fput(file);
24238 +               pr_err("%s must be outside\n", args[0].from);
24239 +               goto out;
24240 +       }
24241 +
24242 +       err = 0;
24243 +       xino->file = file;
24244 +       xino->path = args[0].from;
24245 +
24246 +out:
24247 +       return err;
24248 +}
24249 +
24250 +static int noinline_for_stack
24251 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
24252 +                              struct au_opt_xino_itrunc *xino_itrunc,
24253 +                              substring_t args[])
24254 +{
24255 +       int err;
24256 +       aufs_bindex_t bbot, bindex;
24257 +       struct path path;
24258 +       struct dentry *root;
24259 +
24260 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
24261 +       if (unlikely(err)) {
24262 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
24263 +               goto out;
24264 +       }
24265 +
24266 +       xino_itrunc->bindex = -1;
24267 +       root = sb->s_root;
24268 +       aufs_read_lock(root, AuLock_FLUSH);
24269 +       bbot = au_sbbot(sb);
24270 +       for (bindex = 0; bindex <= bbot; bindex++) {
24271 +               if (au_h_dptr(root, bindex) == path.dentry) {
24272 +                       xino_itrunc->bindex = bindex;
24273 +                       break;
24274 +               }
24275 +       }
24276 +       aufs_read_unlock(root, !AuLock_IR);
24277 +       path_put(&path);
24278 +
24279 +       if (unlikely(xino_itrunc->bindex < 0)) {
24280 +               pr_err("no such branch %s\n", args[0].from);
24281 +               err = -EINVAL;
24282 +       }
24283 +
24284 +out:
24285 +       return err;
24286 +}
24287 +
24288 +/* called without aufs lock */
24289 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
24290 +{
24291 +       int err, n, token;
24292 +       aufs_bindex_t bindex;
24293 +       unsigned char skipped;
24294 +       struct dentry *root;
24295 +       struct au_opt *opt, *opt_tail;
24296 +       char *opt_str;
24297 +       /* reduce the stack space */
24298 +       union {
24299 +               struct au_opt_xino_itrunc *xino_itrunc;
24300 +               struct au_opt_wbr_create *create;
24301 +       } u;
24302 +       struct {
24303 +               substring_t args[MAX_OPT_ARGS];
24304 +       } *a;
24305 +
24306 +       err = -ENOMEM;
24307 +       a = kmalloc(sizeof(*a), GFP_NOFS);
24308 +       if (unlikely(!a))
24309 +               goto out;
24310 +
24311 +       root = sb->s_root;
24312 +       err = 0;
24313 +       bindex = 0;
24314 +       opt = opts->opt;
24315 +       opt_tail = opt + opts->max_opt - 1;
24316 +       opt->type = Opt_tail;
24317 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
24318 +               err = -EINVAL;
24319 +               skipped = 0;
24320 +               token = match_token(opt_str, options, a->args);
24321 +               switch (token) {
24322 +               case Opt_br:
24323 +                       err = 0;
24324 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
24325 +                              && *opt_str) {
24326 +                               err = opt_add(opt, opt_str, opts->sb_flags,
24327 +                                             bindex++);
24328 +                               if (unlikely(!err && ++opt > opt_tail)) {
24329 +                                       err = -E2BIG;
24330 +                                       break;
24331 +                               }
24332 +                               opt->type = Opt_tail;
24333 +                               skipped = 1;
24334 +                       }
24335 +                       break;
24336 +               case Opt_add:
24337 +                       if (unlikely(match_int(&a->args[0], &n))) {
24338 +                               pr_err("bad integer in %s\n", opt_str);
24339 +                               break;
24340 +                       }
24341 +                       bindex = n;
24342 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
24343 +                                     bindex);
24344 +                       if (!err)
24345 +                               opt->type = token;
24346 +                       break;
24347 +               case Opt_append:
24348 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
24349 +                                     /*dummy bindex*/1);
24350 +                       if (!err)
24351 +                               opt->type = token;
24352 +                       break;
24353 +               case Opt_prepend:
24354 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
24355 +                                     /*bindex*/0);
24356 +                       if (!err)
24357 +                               opt->type = token;
24358 +                       break;
24359 +               case Opt_del:
24360 +                       err = au_opts_parse_del(&opt->del, a->args);
24361 +                       if (!err)
24362 +                               opt->type = token;
24363 +                       break;
24364 +#if 0 /* reserved for future use */
24365 +               case Opt_idel:
24366 +                       del->pathname = "(indexed)";
24367 +                       if (unlikely(match_int(&args[0], &n))) {
24368 +                               pr_err("bad integer in %s\n", opt_str);
24369 +                               break;
24370 +                       }
24371 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
24372 +                       if (!err)
24373 +                               opt->type = token;
24374 +                       break;
24375 +#endif
24376 +               case Opt_mod:
24377 +                       err = au_opts_parse_mod(&opt->mod, a->args);
24378 +                       if (!err)
24379 +                               opt->type = token;
24380 +                       break;
24381 +#ifdef IMOD /* reserved for future use */
24382 +               case Opt_imod:
24383 +                       u.mod->path = "(indexed)";
24384 +                       if (unlikely(match_int(&a->args[0], &n))) {
24385 +                               pr_err("bad integer in %s\n", opt_str);
24386 +                               break;
24387 +                       }
24388 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
24389 +                       if (!err)
24390 +                               opt->type = token;
24391 +                       break;
24392 +#endif
24393 +               case Opt_xino:
24394 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
24395 +                       if (!err)
24396 +                               opt->type = token;
24397 +                       break;
24398 +
24399 +               case Opt_trunc_xino_path:
24400 +                       err = au_opts_parse_xino_itrunc_path
24401 +                               (sb, &opt->xino_itrunc, a->args);
24402 +                       if (!err)
24403 +                               opt->type = token;
24404 +                       break;
24405 +
24406 +               case Opt_itrunc_xino:
24407 +                       u.xino_itrunc = &opt->xino_itrunc;
24408 +                       if (unlikely(match_int(&a->args[0], &n))) {
24409 +                               pr_err("bad integer in %s\n", opt_str);
24410 +                               break;
24411 +                       }
24412 +                       u.xino_itrunc->bindex = n;
24413 +                       aufs_read_lock(root, AuLock_FLUSH);
24414 +                       if (n < 0 || au_sbbot(sb) < n) {
24415 +                               pr_err("out of bounds, %d\n", n);
24416 +                               aufs_read_unlock(root, !AuLock_IR);
24417 +                               break;
24418 +                       }
24419 +                       aufs_read_unlock(root, !AuLock_IR);
24420 +                       err = 0;
24421 +                       opt->type = token;
24422 +                       break;
24423 +
24424 +               case Opt_dirwh:
24425 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
24426 +                               break;
24427 +                       err = 0;
24428 +                       opt->type = token;
24429 +                       break;
24430 +
24431 +               case Opt_rdcache:
24432 +                       if (unlikely(match_int(&a->args[0], &n))) {
24433 +                               pr_err("bad integer in %s\n", opt_str);
24434 +                               break;
24435 +                       }
24436 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
24437 +                               pr_err("rdcache must be smaller than %d\n",
24438 +                                      AUFS_RDCACHE_MAX);
24439 +                               break;
24440 +                       }
24441 +                       opt->rdcache = n;
24442 +                       err = 0;
24443 +                       opt->type = token;
24444 +                       break;
24445 +               case Opt_rdblk:
24446 +                       if (unlikely(match_int(&a->args[0], &n)
24447 +                                    || n < 0
24448 +                                    || n > KMALLOC_MAX_SIZE)) {
24449 +                               pr_err("bad integer in %s\n", opt_str);
24450 +                               break;
24451 +                       }
24452 +                       if (unlikely(n && n < NAME_MAX)) {
24453 +                               pr_err("rdblk must be larger than %d\n",
24454 +                                      NAME_MAX);
24455 +                               break;
24456 +                       }
24457 +                       opt->rdblk = n;
24458 +                       err = 0;
24459 +                       opt->type = token;
24460 +                       break;
24461 +               case Opt_rdhash:
24462 +                       if (unlikely(match_int(&a->args[0], &n)
24463 +                                    || n < 0
24464 +                                    || n * sizeof(struct hlist_head)
24465 +                                    > KMALLOC_MAX_SIZE)) {
24466 +                               pr_err("bad integer in %s\n", opt_str);
24467 +                               break;
24468 +                       }
24469 +                       opt->rdhash = n;
24470 +                       err = 0;
24471 +                       opt->type = token;
24472 +                       break;
24473 +
24474 +               case Opt_trunc_xino:
24475 +               case Opt_notrunc_xino:
24476 +               case Opt_noxino:
24477 +               case Opt_trunc_xib:
24478 +               case Opt_notrunc_xib:
24479 +               case Opt_shwh:
24480 +               case Opt_noshwh:
24481 +               case Opt_dirperm1:
24482 +               case Opt_nodirperm1:
24483 +               case Opt_plink:
24484 +               case Opt_noplink:
24485 +               case Opt_list_plink:
24486 +               case Opt_dio:
24487 +               case Opt_nodio:
24488 +               case Opt_diropq_a:
24489 +               case Opt_diropq_w:
24490 +               case Opt_warn_perm:
24491 +               case Opt_nowarn_perm:
24492 +               case Opt_verbose:
24493 +               case Opt_noverbose:
24494 +               case Opt_sum:
24495 +               case Opt_nosum:
24496 +               case Opt_wsum:
24497 +               case Opt_rdblk_def:
24498 +               case Opt_rdhash_def:
24499 +               case Opt_acl:
24500 +               case Opt_noacl:
24501 +                       err = 0;
24502 +                       opt->type = token;
24503 +                       break;
24504 +
24505 +               case Opt_udba:
24506 +                       opt->udba = udba_val(a->args[0].from);
24507 +                       if (opt->udba >= 0) {
24508 +                               err = 0;
24509 +                               opt->type = token;
24510 +                       } else
24511 +                               pr_err("wrong value, %s\n", opt_str);
24512 +                       break;
24513 +
24514 +               case Opt_wbr_create:
24515 +                       u.create = &opt->wbr_create;
24516 +                       u.create->wbr_create
24517 +                               = au_wbr_create_val(a->args[0].from, u.create);
24518 +                       if (u.create->wbr_create >= 0) {
24519 +                               err = 0;
24520 +                               opt->type = token;
24521 +                       } else
24522 +                               pr_err("wrong value, %s\n", opt_str);
24523 +                       break;
24524 +               case Opt_wbr_copyup:
24525 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
24526 +                       if (opt->wbr_copyup >= 0) {
24527 +                               err = 0;
24528 +                               opt->type = token;
24529 +                       } else
24530 +                               pr_err("wrong value, %s\n", opt_str);
24531 +                       break;
24532 +
24533 +               case Opt_fhsm_sec:
24534 +                       if (unlikely(match_int(&a->args[0], &n)
24535 +                                    || n < 0)) {
24536 +                               pr_err("bad integer in %s\n", opt_str);
24537 +                               break;
24538 +                       }
24539 +                       if (sysaufs_brs) {
24540 +                               opt->fhsm_second = n;
24541 +                               opt->type = token;
24542 +                       } else
24543 +                               pr_warn("ignored %s\n", opt_str);
24544 +                       err = 0;
24545 +                       break;
24546 +
24547 +               case Opt_ignore:
24548 +                       pr_warn("ignored %s\n", opt_str);
24549 +                       /*FALLTHROUGH*/
24550 +               case Opt_ignore_silent:
24551 +                       skipped = 1;
24552 +                       err = 0;
24553 +                       break;
24554 +               case Opt_err:
24555 +                       pr_err("unknown option %s\n", opt_str);
24556 +                       break;
24557 +               }
24558 +
24559 +               if (!err && !skipped) {
24560 +                       if (unlikely(++opt > opt_tail)) {
24561 +                               err = -E2BIG;
24562 +                               opt--;
24563 +                               opt->type = Opt_tail;
24564 +                               break;
24565 +                       }
24566 +                       opt->type = Opt_tail;
24567 +               }
24568 +       }
24569 +
24570 +       kfree(a);
24571 +       dump_opts(opts);
24572 +       if (unlikely(err))
24573 +               au_opts_free(opts);
24574 +
24575 +out:
24576 +       return err;
24577 +}
24578 +
24579 +static int au_opt_wbr_create(struct super_block *sb,
24580 +                            struct au_opt_wbr_create *create)
24581 +{
24582 +       int err;
24583 +       struct au_sbinfo *sbinfo;
24584 +
24585 +       SiMustWriteLock(sb);
24586 +
24587 +       err = 1; /* handled */
24588 +       sbinfo = au_sbi(sb);
24589 +       if (sbinfo->si_wbr_create_ops->fin) {
24590 +               err = sbinfo->si_wbr_create_ops->fin(sb);
24591 +               if (!err)
24592 +                       err = 1;
24593 +       }
24594 +
24595 +       sbinfo->si_wbr_create = create->wbr_create;
24596 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
24597 +       switch (create->wbr_create) {
24598 +       case AuWbrCreate_MFSRRV:
24599 +       case AuWbrCreate_MFSRR:
24600 +       case AuWbrCreate_TDMFS:
24601 +       case AuWbrCreate_TDMFSV:
24602 +       case AuWbrCreate_PMFSRR:
24603 +       case AuWbrCreate_PMFSRRV:
24604 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
24605 +               /*FALLTHROUGH*/
24606 +       case AuWbrCreate_MFS:
24607 +       case AuWbrCreate_MFSV:
24608 +       case AuWbrCreate_PMFS:
24609 +       case AuWbrCreate_PMFSV:
24610 +               sbinfo->si_wbr_mfs.mfs_expire
24611 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
24612 +               break;
24613 +       }
24614 +
24615 +       if (sbinfo->si_wbr_create_ops->init)
24616 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
24617 +
24618 +       return err;
24619 +}
24620 +
24621 +/*
24622 + * returns,
24623 + * plus: processed without an error
24624 + * zero: unprocessed
24625 + */
24626 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
24627 +                        struct au_opts *opts)
24628 +{
24629 +       int err;
24630 +       struct au_sbinfo *sbinfo;
24631 +
24632 +       SiMustWriteLock(sb);
24633 +
24634 +       err = 1; /* handled */
24635 +       sbinfo = au_sbi(sb);
24636 +       switch (opt->type) {
24637 +       case Opt_udba:
24638 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24639 +               sbinfo->si_mntflags |= opt->udba;
24640 +               opts->given_udba |= opt->udba;
24641 +               break;
24642 +
24643 +       case Opt_plink:
24644 +               au_opt_set(sbinfo->si_mntflags, PLINK);
24645 +               break;
24646 +       case Opt_noplink:
24647 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24648 +                       au_plink_put(sb, /*verbose*/1);
24649 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
24650 +               break;
24651 +       case Opt_list_plink:
24652 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24653 +                       au_plink_list(sb);
24654 +               break;
24655 +
24656 +       case Opt_dio:
24657 +               au_opt_set(sbinfo->si_mntflags, DIO);
24658 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24659 +               break;
24660 +       case Opt_nodio:
24661 +               au_opt_clr(sbinfo->si_mntflags, DIO);
24662 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24663 +               break;
24664 +
24665 +       case Opt_fhsm_sec:
24666 +               au_fhsm_set(sbinfo, opt->fhsm_second);
24667 +               break;
24668 +
24669 +       case Opt_diropq_a:
24670 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24671 +               break;
24672 +       case Opt_diropq_w:
24673 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24674 +               break;
24675 +
24676 +       case Opt_warn_perm:
24677 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24678 +               break;
24679 +       case Opt_nowarn_perm:
24680 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24681 +               break;
24682 +
24683 +       case Opt_verbose:
24684 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
24685 +               break;
24686 +       case Opt_noverbose:
24687 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24688 +               break;
24689 +
24690 +       case Opt_sum:
24691 +               au_opt_set(sbinfo->si_mntflags, SUM);
24692 +               break;
24693 +       case Opt_wsum:
24694 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24695 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
24696 +       case Opt_nosum:
24697 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24698 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
24699 +               break;
24700 +
24701 +       case Opt_wbr_create:
24702 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
24703 +               break;
24704 +       case Opt_wbr_copyup:
24705 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
24706 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24707 +               break;
24708 +
24709 +       case Opt_dirwh:
24710 +               sbinfo->si_dirwh = opt->dirwh;
24711 +               break;
24712 +
24713 +       case Opt_rdcache:
24714 +               sbinfo->si_rdcache
24715 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
24716 +               break;
24717 +       case Opt_rdblk:
24718 +               sbinfo->si_rdblk = opt->rdblk;
24719 +               break;
24720 +       case Opt_rdblk_def:
24721 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24722 +               break;
24723 +       case Opt_rdhash:
24724 +               sbinfo->si_rdhash = opt->rdhash;
24725 +               break;
24726 +       case Opt_rdhash_def:
24727 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24728 +               break;
24729 +
24730 +       case Opt_shwh:
24731 +               au_opt_set(sbinfo->si_mntflags, SHWH);
24732 +               break;
24733 +       case Opt_noshwh:
24734 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
24735 +               break;
24736 +
24737 +       case Opt_dirperm1:
24738 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24739 +               break;
24740 +       case Opt_nodirperm1:
24741 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24742 +               break;
24743 +
24744 +       case Opt_trunc_xino:
24745 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24746 +               break;
24747 +       case Opt_notrunc_xino:
24748 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24749 +               break;
24750 +
24751 +       case Opt_trunc_xino_path:
24752 +       case Opt_itrunc_xino:
24753 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24754 +               if (!err)
24755 +                       err = 1;
24756 +               break;
24757 +
24758 +       case Opt_trunc_xib:
24759 +               au_fset_opts(opts->flags, TRUNC_XIB);
24760 +               break;
24761 +       case Opt_notrunc_xib:
24762 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24763 +               break;
24764 +
24765 +       case Opt_acl:
24766 +               sb->s_flags |= MS_POSIXACL;
24767 +               break;
24768 +       case Opt_noacl:
24769 +               sb->s_flags &= ~MS_POSIXACL;
24770 +               break;
24771 +
24772 +       default:
24773 +               err = 0;
24774 +               break;
24775 +       }
24776 +
24777 +       return err;
24778 +}
24779 +
24780 +/*
24781 + * returns tri-state.
24782 + * plus: processed without an error
24783 + * zero: unprocessed
24784 + * minus: error
24785 + */
24786 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24787 +                    struct au_opts *opts)
24788 +{
24789 +       int err, do_refresh;
24790 +
24791 +       err = 0;
24792 +       switch (opt->type) {
24793 +       case Opt_append:
24794 +               opt->add.bindex = au_sbbot(sb) + 1;
24795 +               if (opt->add.bindex < 0)
24796 +                       opt->add.bindex = 0;
24797 +               goto add;
24798 +       case Opt_prepend:
24799 +               opt->add.bindex = 0;
24800 +       add: /* indented label */
24801 +       case Opt_add:
24802 +               err = au_br_add(sb, &opt->add,
24803 +                               au_ftest_opts(opts->flags, REMOUNT));
24804 +               if (!err) {
24805 +                       err = 1;
24806 +                       au_fset_opts(opts->flags, REFRESH);
24807 +               }
24808 +               break;
24809 +
24810 +       case Opt_del:
24811 +       case Opt_idel:
24812 +               err = au_br_del(sb, &opt->del,
24813 +                               au_ftest_opts(opts->flags, REMOUNT));
24814 +               if (!err) {
24815 +                       err = 1;
24816 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24817 +                       au_fset_opts(opts->flags, REFRESH);
24818 +               }
24819 +               break;
24820 +
24821 +       case Opt_mod:
24822 +       case Opt_imod:
24823 +               err = au_br_mod(sb, &opt->mod,
24824 +                               au_ftest_opts(opts->flags, REMOUNT),
24825 +                               &do_refresh);
24826 +               if (!err) {
24827 +                       err = 1;
24828 +                       if (do_refresh)
24829 +                               au_fset_opts(opts->flags, REFRESH);
24830 +               }
24831 +               break;
24832 +       }
24833 +       return err;
24834 +}
24835 +
24836 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24837 +                      struct au_opt_xino **opt_xino,
24838 +                      struct au_opts *opts)
24839 +{
24840 +       int err;
24841 +       aufs_bindex_t bbot, bindex;
24842 +       struct dentry *root, *parent, *h_root;
24843 +
24844 +       err = 0;
24845 +       switch (opt->type) {
24846 +       case Opt_xino:
24847 +               err = au_xino_set(sb, &opt->xino,
24848 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24849 +               if (unlikely(err))
24850 +                       break;
24851 +
24852 +               *opt_xino = &opt->xino;
24853 +               au_xino_brid_set(sb, -1);
24854 +
24855 +               /* safe d_parent access */
24856 +               parent = opt->xino.file->f_path.dentry->d_parent;
24857 +               root = sb->s_root;
24858 +               bbot = au_sbbot(sb);
24859 +               for (bindex = 0; bindex <= bbot; bindex++) {
24860 +                       h_root = au_h_dptr(root, bindex);
24861 +                       if (h_root == parent) {
24862 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24863 +                               break;
24864 +                       }
24865 +               }
24866 +               break;
24867 +
24868 +       case Opt_noxino:
24869 +               au_xino_clr(sb);
24870 +               au_xino_brid_set(sb, -1);
24871 +               *opt_xino = (void *)-1;
24872 +               break;
24873 +       }
24874 +
24875 +       return err;
24876 +}
24877 +
24878 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24879 +                  unsigned int pending)
24880 +{
24881 +       int err, fhsm;
24882 +       aufs_bindex_t bindex, bbot;
24883 +       unsigned char do_plink, skip, do_free, can_no_dreval;
24884 +       struct au_branch *br;
24885 +       struct au_wbr *wbr;
24886 +       struct dentry *root, *dentry;
24887 +       struct inode *dir, *h_dir;
24888 +       struct au_sbinfo *sbinfo;
24889 +       struct au_hinode *hdir;
24890 +
24891 +       SiMustAnyLock(sb);
24892 +
24893 +       sbinfo = au_sbi(sb);
24894 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24895 +
24896 +       if (!(sb_flags & MS_RDONLY)) {
24897 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24898 +                       pr_warn("first branch should be rw\n");
24899 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24900 +                       pr_warn_once("shwh should be used with ro\n");
24901 +       }
24902 +
24903 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24904 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24905 +               pr_warn_once("udba=*notify requires xino\n");
24906 +
24907 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24908 +               pr_warn_once("dirperm1 breaks the protection"
24909 +                            " by the permission bits on the lower branch\n");
24910 +
24911 +       err = 0;
24912 +       fhsm = 0;
24913 +       root = sb->s_root;
24914 +       dir = d_inode(root);
24915 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24916 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24917 +                                     UDBA_NONE);
24918 +       bbot = au_sbbot(sb);
24919 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
24920 +               skip = 0;
24921 +               h_dir = au_h_iptr(dir, bindex);
24922 +               br = au_sbr(sb, bindex);
24923 +
24924 +               if ((br->br_perm & AuBrAttr_ICEX)
24925 +                   && !h_dir->i_op->listxattr)
24926 +                       br->br_perm &= ~AuBrAttr_ICEX;
24927 +#if 0
24928 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24929 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24930 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24931 +#endif
24932 +
24933 +               do_free = 0;
24934 +               wbr = br->br_wbr;
24935 +               if (wbr)
24936 +                       wbr_wh_read_lock(wbr);
24937 +
24938 +               if (!au_br_writable(br->br_perm)) {
24939 +                       do_free = !!wbr;
24940 +                       skip = (!wbr
24941 +                               || (!wbr->wbr_whbase
24942 +                                   && !wbr->wbr_plink
24943 +                                   && !wbr->wbr_orph));
24944 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24945 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24946 +                       skip = (!wbr || !wbr->wbr_whbase);
24947 +                       if (skip && wbr) {
24948 +                               if (do_plink)
24949 +                                       skip = !!wbr->wbr_plink;
24950 +                               else
24951 +                                       skip = !wbr->wbr_plink;
24952 +                       }
24953 +               } else {
24954 +                       /* skip = (br->br_whbase && br->br_ohph); */
24955 +                       skip = (wbr && wbr->wbr_whbase);
24956 +                       if (skip) {
24957 +                               if (do_plink)
24958 +                                       skip = !!wbr->wbr_plink;
24959 +                               else
24960 +                                       skip = !wbr->wbr_plink;
24961 +                       }
24962 +               }
24963 +               if (wbr)
24964 +                       wbr_wh_read_unlock(wbr);
24965 +
24966 +               if (can_no_dreval) {
24967 +                       dentry = br->br_path.dentry;
24968 +                       spin_lock(&dentry->d_lock);
24969 +                       if (dentry->d_flags &
24970 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24971 +                               can_no_dreval = 0;
24972 +                       spin_unlock(&dentry->d_lock);
24973 +               }
24974 +
24975 +               if (au_br_fhsm(br->br_perm)) {
24976 +                       fhsm++;
24977 +                       AuDebugOn(!br->br_fhsm);
24978 +               }
24979 +
24980 +               if (skip)
24981 +                       continue;
24982 +
24983 +               hdir = au_hi(dir, bindex);
24984 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
24985 +               if (wbr)
24986 +                       wbr_wh_write_lock(wbr);
24987 +               err = au_wh_init(br, sb);
24988 +               if (wbr)
24989 +                       wbr_wh_write_unlock(wbr);
24990 +               au_hn_inode_unlock(hdir);
24991 +
24992 +               if (!err && do_free) {
24993 +                       kfree(wbr);
24994 +                       br->br_wbr = NULL;
24995 +               }
24996 +       }
24997 +
24998 +       if (can_no_dreval)
24999 +               au_fset_si(sbinfo, NO_DREVAL);
25000 +       else
25001 +               au_fclr_si(sbinfo, NO_DREVAL);
25002 +
25003 +       if (fhsm >= 2) {
25004 +               au_fset_si(sbinfo, FHSM);
25005 +               for (bindex = bbot; bindex >= 0; bindex--) {
25006 +                       br = au_sbr(sb, bindex);
25007 +                       if (au_br_fhsm(br->br_perm)) {
25008 +                               au_fhsm_set_bottom(sb, bindex);
25009 +                               break;
25010 +                       }
25011 +               }
25012 +       } else {
25013 +               au_fclr_si(sbinfo, FHSM);
25014 +               au_fhsm_set_bottom(sb, -1);
25015 +       }
25016 +
25017 +       return err;
25018 +}
25019 +
25020 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
25021 +{
25022 +       int err;
25023 +       unsigned int tmp;
25024 +       aufs_bindex_t bindex, bbot;
25025 +       struct au_opt *opt;
25026 +       struct au_opt_xino *opt_xino, xino;
25027 +       struct au_sbinfo *sbinfo;
25028 +       struct au_branch *br;
25029 +       struct inode *dir;
25030 +
25031 +       SiMustWriteLock(sb);
25032 +
25033 +       err = 0;
25034 +       opt_xino = NULL;
25035 +       opt = opts->opt;
25036 +       while (err >= 0 && opt->type != Opt_tail)
25037 +               err = au_opt_simple(sb, opt++, opts);
25038 +       if (err > 0)
25039 +               err = 0;
25040 +       else if (unlikely(err < 0))
25041 +               goto out;
25042 +
25043 +       /* disable xino and udba temporary */
25044 +       sbinfo = au_sbi(sb);
25045 +       tmp = sbinfo->si_mntflags;
25046 +       au_opt_clr(sbinfo->si_mntflags, XINO);
25047 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
25048 +
25049 +       opt = opts->opt;
25050 +       while (err >= 0 && opt->type != Opt_tail)
25051 +               err = au_opt_br(sb, opt++, opts);
25052 +       if (err > 0)
25053 +               err = 0;
25054 +       else if (unlikely(err < 0))
25055 +               goto out;
25056 +
25057 +       bbot = au_sbbot(sb);
25058 +       if (unlikely(bbot < 0)) {
25059 +               err = -EINVAL;
25060 +               pr_err("no branches\n");
25061 +               goto out;
25062 +       }
25063 +
25064 +       if (au_opt_test(tmp, XINO))
25065 +               au_opt_set(sbinfo->si_mntflags, XINO);
25066 +       opt = opts->opt;
25067 +       while (!err && opt->type != Opt_tail)
25068 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
25069 +       if (unlikely(err))
25070 +               goto out;
25071 +
25072 +       err = au_opts_verify(sb, sb->s_flags, tmp);
25073 +       if (unlikely(err))
25074 +               goto out;
25075 +
25076 +       /* restore xino */
25077 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
25078 +               xino.file = au_xino_def(sb);
25079 +               err = PTR_ERR(xino.file);
25080 +               if (IS_ERR(xino.file))
25081 +                       goto out;
25082 +
25083 +               err = au_xino_set(sb, &xino, /*remount*/0);
25084 +               fput(xino.file);
25085 +               if (unlikely(err))
25086 +                       goto out;
25087 +       }
25088 +
25089 +       /* restore udba */
25090 +       tmp &= AuOptMask_UDBA;
25091 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
25092 +       sbinfo->si_mntflags |= tmp;
25093 +       bbot = au_sbbot(sb);
25094 +       for (bindex = 0; bindex <= bbot; bindex++) {
25095 +               br = au_sbr(sb, bindex);
25096 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
25097 +               if (unlikely(err))
25098 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
25099 +                               bindex, err);
25100 +               /* go on even if err */
25101 +       }
25102 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
25103 +               dir = d_inode(sb->s_root);
25104 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
25105 +       }
25106 +
25107 +out:
25108 +       return err;
25109 +}
25110 +
25111 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
25112 +{
25113 +       int err, rerr;
25114 +       unsigned char no_dreval;
25115 +       struct inode *dir;
25116 +       struct au_opt_xino *opt_xino;
25117 +       struct au_opt *opt;
25118 +       struct au_sbinfo *sbinfo;
25119 +
25120 +       SiMustWriteLock(sb);
25121 +
25122 +       err = 0;
25123 +       dir = d_inode(sb->s_root);
25124 +       sbinfo = au_sbi(sb);
25125 +       opt_xino = NULL;
25126 +       opt = opts->opt;
25127 +       while (err >= 0 && opt->type != Opt_tail) {
25128 +               err = au_opt_simple(sb, opt, opts);
25129 +               if (!err)
25130 +                       err = au_opt_br(sb, opt, opts);
25131 +               if (!err)
25132 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
25133 +               opt++;
25134 +       }
25135 +       if (err > 0)
25136 +               err = 0;
25137 +       AuTraceErr(err);
25138 +       /* go on even err */
25139 +
25140 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
25141 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
25142 +       if (unlikely(rerr && !err))
25143 +               err = rerr;
25144 +
25145 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
25146 +               au_fset_opts(opts->flags, REFRESH_IDOP);
25147 +
25148 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
25149 +               rerr = au_xib_trunc(sb);
25150 +               if (unlikely(rerr && !err))
25151 +                       err = rerr;
25152 +       }
25153 +
25154 +       /* will be handled by the caller */
25155 +       if (!au_ftest_opts(opts->flags, REFRESH)
25156 +           && (opts->given_udba
25157 +               || au_opt_test(sbinfo->si_mntflags, XINO)
25158 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
25159 +                   ))
25160 +               au_fset_opts(opts->flags, REFRESH);
25161 +
25162 +       AuDbg("status 0x%x\n", opts->flags);
25163 +       return err;
25164 +}
25165 +
25166 +/* ---------------------------------------------------------------------- */
25167 +
25168 +unsigned int au_opt_udba(struct super_block *sb)
25169 +{
25170 +       return au_mntflags(sb) & AuOptMask_UDBA;
25171 +}
25172 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
25173 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
25174 +++ linux/fs/aufs/opts.h        2017-07-29 12:14:25.903042072 +0200
25175 @@ -0,0 +1,212 @@
25176 +/*
25177 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25178 + *
25179 + * This program, aufs is free software; you can redistribute it and/or modify
25180 + * it under the terms of the GNU General Public License as published by
25181 + * the Free Software Foundation; either version 2 of the License, or
25182 + * (at your option) any later version.
25183 + *
25184 + * This program is distributed in the hope that it will be useful,
25185 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25186 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25187 + * GNU General Public License for more details.
25188 + *
25189 + * You should have received a copy of the GNU General Public License
25190 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25191 + */
25192 +
25193 +/*
25194 + * mount options/flags
25195 + */
25196 +
25197 +#ifndef __AUFS_OPTS_H__
25198 +#define __AUFS_OPTS_H__
25199 +
25200 +#ifdef __KERNEL__
25201 +
25202 +#include <linux/path.h>
25203 +
25204 +struct file;
25205 +
25206 +/* ---------------------------------------------------------------------- */
25207 +
25208 +/* mount flags */
25209 +#define AuOpt_XINO             1               /* external inode number bitmap
25210 +                                                  and translation table */
25211 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
25212 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
25213 +#define AuOpt_UDBA_REVAL       (1 << 3)
25214 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
25215 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
25216 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
25217 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
25218 +                                                  bits */
25219 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
25220 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
25221 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
25222 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
25223 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
25224 +#define AuOpt_DIO              (1 << 14)       /* direct io */
25225 +
25226 +#ifndef CONFIG_AUFS_HNOTIFY
25227 +#undef AuOpt_UDBA_HNOTIFY
25228 +#define AuOpt_UDBA_HNOTIFY     0
25229 +#endif
25230 +#ifndef CONFIG_AUFS_SHWH
25231 +#undef AuOpt_SHWH
25232 +#define AuOpt_SHWH             0
25233 +#endif
25234 +
25235 +#define AuOpt_Def      (AuOpt_XINO \
25236 +                        | AuOpt_UDBA_REVAL \
25237 +                        | AuOpt_PLINK \
25238 +                        /* | AuOpt_DIRPERM1 */ \
25239 +                        | AuOpt_WARN_PERM)
25240 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
25241 +                        | AuOpt_UDBA_REVAL \
25242 +                        | AuOpt_UDBA_HNOTIFY)
25243 +
25244 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
25245 +#define au_opt_set(flags, name) do { \
25246 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
25247 +       ((flags) |= AuOpt_##name); \
25248 +} while (0)
25249 +#define au_opt_set_udba(flags, name) do { \
25250 +       (flags) &= ~AuOptMask_UDBA; \
25251 +       ((flags) |= AuOpt_##name); \
25252 +} while (0)
25253 +#define au_opt_clr(flags, name) do { \
25254 +       ((flags) &= ~AuOpt_##name); \
25255 +} while (0)
25256 +
25257 +static inline unsigned int au_opts_plink(unsigned int mntflags)
25258 +{
25259 +#ifdef CONFIG_PROC_FS
25260 +       return mntflags;
25261 +#else
25262 +       return mntflags & ~AuOpt_PLINK;
25263 +#endif
25264 +}
25265 +
25266 +/* ---------------------------------------------------------------------- */
25267 +
25268 +/* policies to select one among multiple writable branches */
25269 +enum {
25270 +       AuWbrCreate_TDP,        /* top down parent */
25271 +       AuWbrCreate_RR,         /* round robin */
25272 +       AuWbrCreate_MFS,        /* most free space */
25273 +       AuWbrCreate_MFSV,       /* mfs with seconds */
25274 +       AuWbrCreate_MFSRR,      /* mfs then rr */
25275 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
25276 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
25277 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
25278 +       AuWbrCreate_PMFS,       /* parent and mfs */
25279 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
25280 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
25281 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
25282 +
25283 +       AuWbrCreate_Def = AuWbrCreate_TDP
25284 +};
25285 +
25286 +enum {
25287 +       AuWbrCopyup_TDP,        /* top down parent */
25288 +       AuWbrCopyup_BUP,        /* bottom up parent */
25289 +       AuWbrCopyup_BU,         /* bottom up */
25290 +
25291 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
25292 +};
25293 +
25294 +/* ---------------------------------------------------------------------- */
25295 +
25296 +struct au_opt_add {
25297 +       aufs_bindex_t   bindex;
25298 +       char            *pathname;
25299 +       int             perm;
25300 +       struct path     path;
25301 +};
25302 +
25303 +struct au_opt_del {
25304 +       char            *pathname;
25305 +       struct path     h_path;
25306 +};
25307 +
25308 +struct au_opt_mod {
25309 +       char            *path;
25310 +       int             perm;
25311 +       struct dentry   *h_root;
25312 +};
25313 +
25314 +struct au_opt_xino {
25315 +       char            *path;
25316 +       struct file     *file;
25317 +};
25318 +
25319 +struct au_opt_xino_itrunc {
25320 +       aufs_bindex_t   bindex;
25321 +};
25322 +
25323 +struct au_opt_wbr_create {
25324 +       int                     wbr_create;
25325 +       int                     mfs_second;
25326 +       unsigned long long      mfsrr_watermark;
25327 +};
25328 +
25329 +struct au_opt {
25330 +       int type;
25331 +       union {
25332 +               struct au_opt_xino      xino;
25333 +               struct au_opt_xino_itrunc xino_itrunc;
25334 +               struct au_opt_add       add;
25335 +               struct au_opt_del       del;
25336 +               struct au_opt_mod       mod;
25337 +               int                     dirwh;
25338 +               int                     rdcache;
25339 +               unsigned int            rdblk;
25340 +               unsigned int            rdhash;
25341 +               int                     udba;
25342 +               struct au_opt_wbr_create wbr_create;
25343 +               int                     wbr_copyup;
25344 +               unsigned int            fhsm_second;
25345 +       };
25346 +};
25347 +
25348 +/* opts flags */
25349 +#define AuOpts_REMOUNT         1
25350 +#define AuOpts_REFRESH         (1 << 1)
25351 +#define AuOpts_TRUNC_XIB       (1 << 2)
25352 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
25353 +#define AuOpts_REFRESH_IDOP    (1 << 4)
25354 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
25355 +#define au_fset_opts(flags, name) \
25356 +       do { (flags) |= AuOpts_##name; } while (0)
25357 +#define au_fclr_opts(flags, name) \
25358 +       do { (flags) &= ~AuOpts_##name; } while (0)
25359 +
25360 +struct au_opts {
25361 +       struct au_opt   *opt;
25362 +       int             max_opt;
25363 +
25364 +       unsigned int    given_udba;
25365 +       unsigned int    flags;
25366 +       unsigned long   sb_flags;
25367 +};
25368 +
25369 +/* ---------------------------------------------------------------------- */
25370 +
25371 +/* opts.c */
25372 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
25373 +const char *au_optstr_udba(int udba);
25374 +const char *au_optstr_wbr_copyup(int wbr_copyup);
25375 +const char *au_optstr_wbr_create(int wbr_create);
25376 +
25377 +void au_opts_free(struct au_opts *opts);
25378 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
25379 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
25380 +                  unsigned int pending);
25381 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
25382 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
25383 +
25384 +unsigned int au_opt_udba(struct super_block *sb);
25385 +
25386 +#endif /* __KERNEL__ */
25387 +#endif /* __AUFS_OPTS_H__ */
25388 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
25389 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
25390 +++ linux/fs/aufs/plink.c       2017-07-29 12:14:25.903042072 +0200
25391 @@ -0,0 +1,514 @@
25392 +/*
25393 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25394 + *
25395 + * This program, aufs is free software; you can redistribute it and/or modify
25396 + * it under the terms of the GNU General Public License as published by
25397 + * the Free Software Foundation; either version 2 of the License, or
25398 + * (at your option) any later version.
25399 + *
25400 + * This program is distributed in the hope that it will be useful,
25401 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25402 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25403 + * GNU General Public License for more details.
25404 + *
25405 + * You should have received a copy of the GNU General Public License
25406 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25407 + */
25408 +
25409 +/*
25410 + * pseudo-link
25411 + */
25412 +
25413 +#include "aufs.h"
25414 +
25415 +/*
25416 + * the pseudo-link maintenance mode.
25417 + * during a user process maintains the pseudo-links,
25418 + * prohibit adding a new plink and branch manipulation.
25419 + *
25420 + * Flags
25421 + * NOPLM:
25422 + *     For entry functions which will handle plink, and i_mutex is already held
25423 + *     in VFS.
25424 + *     They cannot wait and should return an error at once.
25425 + *     Callers has to check the error.
25426 + * NOPLMW:
25427 + *     For entry functions which will handle plink, but i_mutex is not held
25428 + *     in VFS.
25429 + *     They can wait the plink maintenance mode to finish.
25430 + *
25431 + * They behave like F_SETLK and F_SETLKW.
25432 + * If the caller never handle plink, then both flags are unnecessary.
25433 + */
25434 +
25435 +int au_plink_maint(struct super_block *sb, int flags)
25436 +{
25437 +       int err;
25438 +       pid_t pid, ppid;
25439 +       struct task_struct *parent, *prev;
25440 +       struct au_sbinfo *sbi;
25441 +
25442 +       SiMustAnyLock(sb);
25443 +
25444 +       err = 0;
25445 +       if (!au_opt_test(au_mntflags(sb), PLINK))
25446 +               goto out;
25447 +
25448 +       sbi = au_sbi(sb);
25449 +       pid = sbi->si_plink_maint_pid;
25450 +       if (!pid || pid == current->pid)
25451 +               goto out;
25452 +
25453 +       /* todo: it highly depends upon /sbin/mount.aufs */
25454 +       prev = NULL;
25455 +       parent = current;
25456 +       ppid = 0;
25457 +       rcu_read_lock();
25458 +       while (1) {
25459 +               parent = rcu_dereference(parent->real_parent);
25460 +               if (parent == prev)
25461 +                       break;
25462 +               ppid = task_pid_vnr(parent);
25463 +               if (pid == ppid) {
25464 +                       rcu_read_unlock();
25465 +                       goto out;
25466 +               }
25467 +               prev = parent;
25468 +       }
25469 +       rcu_read_unlock();
25470 +
25471 +       if (au_ftest_lock(flags, NOPLMW)) {
25472 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
25473 +               /* AuDebugOn(!lockdep_depth(current)); */
25474 +               while (sbi->si_plink_maint_pid) {
25475 +                       si_read_unlock(sb);
25476 +                       /* gave up wake_up_bit() */
25477 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
25478 +
25479 +                       if (au_ftest_lock(flags, FLUSH))
25480 +                               au_nwt_flush(&sbi->si_nowait);
25481 +                       si_noflush_read_lock(sb);
25482 +               }
25483 +       } else if (au_ftest_lock(flags, NOPLM)) {
25484 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
25485 +               err = -EAGAIN;
25486 +       }
25487 +
25488 +out:
25489 +       return err;
25490 +}
25491 +
25492 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
25493 +{
25494 +       spin_lock(&sbinfo->si_plink_maint_lock);
25495 +       sbinfo->si_plink_maint_pid = 0;
25496 +       spin_unlock(&sbinfo->si_plink_maint_lock);
25497 +       wake_up_all(&sbinfo->si_plink_wq);
25498 +}
25499 +
25500 +int au_plink_maint_enter(struct super_block *sb)
25501 +{
25502 +       int err;
25503 +       struct au_sbinfo *sbinfo;
25504 +
25505 +       err = 0;
25506 +       sbinfo = au_sbi(sb);
25507 +       /* make sure i am the only one in this fs */
25508 +       si_write_lock(sb, AuLock_FLUSH);
25509 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
25510 +               spin_lock(&sbinfo->si_plink_maint_lock);
25511 +               if (!sbinfo->si_plink_maint_pid)
25512 +                       sbinfo->si_plink_maint_pid = current->pid;
25513 +               else
25514 +                       err = -EBUSY;
25515 +               spin_unlock(&sbinfo->si_plink_maint_lock);
25516 +       }
25517 +       si_write_unlock(sb);
25518 +
25519 +       return err;
25520 +}
25521 +
25522 +/* ---------------------------------------------------------------------- */
25523 +
25524 +#ifdef CONFIG_AUFS_DEBUG
25525 +void au_plink_list(struct super_block *sb)
25526 +{
25527 +       int i;
25528 +       struct au_sbinfo *sbinfo;
25529 +       struct hlist_head *plink_hlist;
25530 +       struct au_icntnr *icntnr;
25531 +
25532 +       SiMustAnyLock(sb);
25533 +
25534 +       sbinfo = au_sbi(sb);
25535 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25536 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25537 +
25538 +       for (i = 0; i < AuPlink_NHASH; i++) {
25539 +               plink_hlist = &sbinfo->si_plink[i].head;
25540 +               rcu_read_lock();
25541 +               hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25542 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
25543 +               rcu_read_unlock();
25544 +       }
25545 +}
25546 +#endif
25547 +
25548 +/* is the inode pseudo-linked? */
25549 +int au_plink_test(struct inode *inode)
25550 +{
25551 +       int found, i;
25552 +       struct au_sbinfo *sbinfo;
25553 +       struct hlist_head *plink_hlist;
25554 +       struct au_icntnr *icntnr;
25555 +
25556 +       sbinfo = au_sbi(inode->i_sb);
25557 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
25558 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
25559 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25560 +
25561 +       found = 0;
25562 +       i = au_plink_hash(inode->i_ino);
25563 +       plink_hlist = &sbinfo->si_plink[i].head;
25564 +       rcu_read_lock();
25565 +       hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25566 +               if (&icntnr->vfs_inode == inode) {
25567 +                       found = 1;
25568 +                       break;
25569 +               }
25570 +       rcu_read_unlock();
25571 +       return found;
25572 +}
25573 +
25574 +/* ---------------------------------------------------------------------- */
25575 +
25576 +/*
25577 + * generate a name for plink.
25578 + * the file will be stored under AUFS_WH_PLINKDIR.
25579 + */
25580 +/* 20 is max digits length of ulong 64 */
25581 +#define PLINK_NAME_LEN ((20 + 1) * 2)
25582 +
25583 +static int plink_name(char *name, int len, struct inode *inode,
25584 +                     aufs_bindex_t bindex)
25585 +{
25586 +       int rlen;
25587 +       struct inode *h_inode;
25588 +
25589 +       h_inode = au_h_iptr(inode, bindex);
25590 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
25591 +       return rlen;
25592 +}
25593 +
25594 +struct au_do_plink_lkup_args {
25595 +       struct dentry **errp;
25596 +       struct qstr *tgtname;
25597 +       struct dentry *h_parent;
25598 +       struct au_branch *br;
25599 +};
25600 +
25601 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
25602 +                                      struct dentry *h_parent,
25603 +                                      struct au_branch *br)
25604 +{
25605 +       struct dentry *h_dentry;
25606 +       struct inode *h_inode;
25607 +
25608 +       h_inode = d_inode(h_parent);
25609 +       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
25610 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
25611 +       inode_unlock(h_inode);
25612 +       return h_dentry;
25613 +}
25614 +
25615 +static void au_call_do_plink_lkup(void *args)
25616 +{
25617 +       struct au_do_plink_lkup_args *a = args;
25618 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
25619 +}
25620 +
25621 +/* lookup the plink-ed @inode under the branch at @bindex */
25622 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
25623 +{
25624 +       struct dentry *h_dentry, *h_parent;
25625 +       struct au_branch *br;
25626 +       int wkq_err;
25627 +       char a[PLINK_NAME_LEN];
25628 +       struct qstr tgtname = QSTR_INIT(a, 0);
25629 +
25630 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25631 +
25632 +       br = au_sbr(inode->i_sb, bindex);
25633 +       h_parent = br->br_wbr->wbr_plink;
25634 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25635 +
25636 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25637 +               struct au_do_plink_lkup_args args = {
25638 +                       .errp           = &h_dentry,
25639 +                       .tgtname        = &tgtname,
25640 +                       .h_parent       = h_parent,
25641 +                       .br             = br
25642 +               };
25643 +
25644 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
25645 +               if (unlikely(wkq_err))
25646 +                       h_dentry = ERR_PTR(wkq_err);
25647 +       } else
25648 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25649 +
25650 +       return h_dentry;
25651 +}
25652 +
25653 +/* create a pseudo-link */
25654 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25655 +                     struct dentry *h_dentry, struct au_branch *br)
25656 +{
25657 +       int err;
25658 +       struct path h_path = {
25659 +               .mnt = au_br_mnt(br)
25660 +       };
25661 +       struct inode *h_dir, *delegated;
25662 +
25663 +       h_dir = d_inode(h_parent);
25664 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
25665 +again:
25666 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
25667 +       err = PTR_ERR(h_path.dentry);
25668 +       if (IS_ERR(h_path.dentry))
25669 +               goto out;
25670 +
25671 +       err = 0;
25672 +       /* wh.plink dir is not monitored */
25673 +       /* todo: is it really safe? */
25674 +       if (d_is_positive(h_path.dentry)
25675 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
25676 +               delegated = NULL;
25677 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25678 +               if (unlikely(err == -EWOULDBLOCK)) {
25679 +                       pr_warn("cannot retry for NFSv4 delegation"
25680 +                               " for an internal unlink\n");
25681 +                       iput(delegated);
25682 +               }
25683 +               dput(h_path.dentry);
25684 +               h_path.dentry = NULL;
25685 +               if (!err)
25686 +                       goto again;
25687 +       }
25688 +       if (!err && d_is_negative(h_path.dentry)) {
25689 +               delegated = NULL;
25690 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25691 +               if (unlikely(err == -EWOULDBLOCK)) {
25692 +                       pr_warn("cannot retry for NFSv4 delegation"
25693 +                               " for an internal link\n");
25694 +                       iput(delegated);
25695 +               }
25696 +       }
25697 +       dput(h_path.dentry);
25698 +
25699 +out:
25700 +       inode_unlock(h_dir);
25701 +       return err;
25702 +}
25703 +
25704 +struct do_whplink_args {
25705 +       int *errp;
25706 +       struct qstr *tgt;
25707 +       struct dentry *h_parent;
25708 +       struct dentry *h_dentry;
25709 +       struct au_branch *br;
25710 +};
25711 +
25712 +static void call_do_whplink(void *args)
25713 +{
25714 +       struct do_whplink_args *a = args;
25715 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25716 +}
25717 +
25718 +static int whplink(struct dentry *h_dentry, struct inode *inode,
25719 +                  aufs_bindex_t bindex, struct au_branch *br)
25720 +{
25721 +       int err, wkq_err;
25722 +       struct au_wbr *wbr;
25723 +       struct dentry *h_parent;
25724 +       char a[PLINK_NAME_LEN];
25725 +       struct qstr tgtname = QSTR_INIT(a, 0);
25726 +
25727 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25728 +       h_parent = wbr->wbr_plink;
25729 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25730 +
25731 +       /* always superio. */
25732 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25733 +               struct do_whplink_args args = {
25734 +                       .errp           = &err,
25735 +                       .tgt            = &tgtname,
25736 +                       .h_parent       = h_parent,
25737 +                       .h_dentry       = h_dentry,
25738 +                       .br             = br
25739 +               };
25740 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
25741 +               if (unlikely(wkq_err))
25742 +                       err = wkq_err;
25743 +       } else
25744 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
25745 +
25746 +       return err;
25747 +}
25748 +
25749 +/*
25750 + * create a new pseudo-link for @h_dentry on @bindex.
25751 + * the linked inode is held in aufs @inode.
25752 + */
25753 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25754 +                    struct dentry *h_dentry)
25755 +{
25756 +       struct super_block *sb;
25757 +       struct au_sbinfo *sbinfo;
25758 +       struct hlist_head *plink_hlist;
25759 +       struct au_icntnr *icntnr;
25760 +       struct au_sphlhead *sphl;
25761 +       int found, err, cnt, i;
25762 +
25763 +       sb = inode->i_sb;
25764 +       sbinfo = au_sbi(sb);
25765 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25766 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25767 +
25768 +       found = au_plink_test(inode);
25769 +       if (found)
25770 +               return;
25771 +
25772 +       i = au_plink_hash(inode->i_ino);
25773 +       sphl = sbinfo->si_plink + i;
25774 +       plink_hlist = &sphl->head;
25775 +       au_igrab(inode);
25776 +
25777 +       spin_lock(&sphl->spin);
25778 +       hlist_for_each_entry(icntnr, plink_hlist, plink) {
25779 +               if (&icntnr->vfs_inode == inode) {
25780 +                       found = 1;
25781 +                       break;
25782 +               }
25783 +       }
25784 +       if (!found) {
25785 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
25786 +               hlist_add_head_rcu(&icntnr->plink, plink_hlist);
25787 +       }
25788 +       spin_unlock(&sphl->spin);
25789 +       if (!found) {
25790 +               cnt = au_sphl_count(sphl);
25791 +#define msg "unexpectedly unblanced or too many pseudo-links"
25792 +               if (cnt > AUFS_PLINK_WARN)
25793 +                       AuWarn1(msg ", %d\n", cnt);
25794 +#undef msg
25795 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25796 +               if (unlikely(err)) {
25797 +                       pr_warn("err %d, damaged pseudo link.\n", err);
25798 +                       au_sphl_del_rcu(&icntnr->plink, sphl);
25799 +                       iput(&icntnr->vfs_inode);
25800 +               }
25801 +       } else
25802 +               iput(&icntnr->vfs_inode);
25803 +}
25804 +
25805 +/* free all plinks */
25806 +void au_plink_put(struct super_block *sb, int verbose)
25807 +{
25808 +       int i, warned;
25809 +       struct au_sbinfo *sbinfo;
25810 +       struct hlist_head *plink_hlist;
25811 +       struct hlist_node *tmp;
25812 +       struct au_icntnr *icntnr;
25813 +
25814 +       SiMustWriteLock(sb);
25815 +
25816 +       sbinfo = au_sbi(sb);
25817 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25818 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25819 +
25820 +       /* no spin_lock since sbinfo is write-locked */
25821 +       warned = 0;
25822 +       for (i = 0; i < AuPlink_NHASH; i++) {
25823 +               plink_hlist = &sbinfo->si_plink[i].head;
25824 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25825 +                       pr_warn("pseudo-link is not flushed");
25826 +                       warned = 1;
25827 +               }
25828 +               hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
25829 +                       iput(&icntnr->vfs_inode);
25830 +               INIT_HLIST_HEAD(plink_hlist);
25831 +       }
25832 +}
25833 +
25834 +void au_plink_clean(struct super_block *sb, int verbose)
25835 +{
25836 +       struct dentry *root;
25837 +
25838 +       root = sb->s_root;
25839 +       aufs_write_lock(root);
25840 +       if (au_opt_test(au_mntflags(sb), PLINK))
25841 +               au_plink_put(sb, verbose);
25842 +       aufs_write_unlock(root);
25843 +}
25844 +
25845 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25846 +{
25847 +       int do_put;
25848 +       aufs_bindex_t btop, bbot, bindex;
25849 +
25850 +       do_put = 0;
25851 +       btop = au_ibtop(inode);
25852 +       bbot = au_ibbot(inode);
25853 +       if (btop >= 0) {
25854 +               for (bindex = btop; bindex <= bbot; bindex++) {
25855 +                       if (!au_h_iptr(inode, bindex)
25856 +                           || au_ii_br_id(inode, bindex) != br_id)
25857 +                               continue;
25858 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25859 +                       do_put = 1;
25860 +                       break;
25861 +               }
25862 +               if (do_put)
25863 +                       for (bindex = btop; bindex <= bbot; bindex++)
25864 +                               if (au_h_iptr(inode, bindex)) {
25865 +                                       do_put = 0;
25866 +                                       break;
25867 +                               }
25868 +       } else
25869 +               do_put = 1;
25870 +
25871 +       return do_put;
25872 +}
25873 +
25874 +/* free the plinks on a branch specified by @br_id */
25875 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25876 +{
25877 +       struct au_sbinfo *sbinfo;
25878 +       struct hlist_head *plink_hlist;
25879 +       struct hlist_node *tmp;
25880 +       struct au_icntnr *icntnr;
25881 +       struct inode *inode;
25882 +       int i, do_put;
25883 +
25884 +       SiMustWriteLock(sb);
25885 +
25886 +       sbinfo = au_sbi(sb);
25887 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25888 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25889 +
25890 +       /* no spin_lock since sbinfo is write-locked */
25891 +       for (i = 0; i < AuPlink_NHASH; i++) {
25892 +               plink_hlist = &sbinfo->si_plink[i].head;
25893 +               hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
25894 +                       inode = au_igrab(&icntnr->vfs_inode);
25895 +                       ii_write_lock_child(inode);
25896 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25897 +                       if (do_put) {
25898 +                               hlist_del(&icntnr->plink);
25899 +                               iput(inode);
25900 +                       }
25901 +                       ii_write_unlock(inode);
25902 +                       iput(inode);
25903 +               }
25904 +       }
25905 +}
25906 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25907 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25908 +++ linux/fs/aufs/poll.c        2017-07-29 12:14:25.906375514 +0200
25909 @@ -0,0 +1,52 @@
25910 +/*
25911 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25912 + *
25913 + * This program, aufs is free software; you can redistribute it and/or modify
25914 + * it under the terms of the GNU General Public License as published by
25915 + * the Free Software Foundation; either version 2 of the License, or
25916 + * (at your option) any later version.
25917 + *
25918 + * This program is distributed in the hope that it will be useful,
25919 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25920 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25921 + * GNU General Public License for more details.
25922 + *
25923 + * You should have received a copy of the GNU General Public License
25924 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25925 + */
25926 +
25927 +/*
25928 + * poll operation
25929 + * There is only one filesystem which implements ->poll operation, currently.
25930 + */
25931 +
25932 +#include "aufs.h"
25933 +
25934 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25935 +{
25936 +       unsigned int mask;
25937 +       int err;
25938 +       struct file *h_file;
25939 +       struct super_block *sb;
25940 +
25941 +       /* We should pretend an error happened. */
25942 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25943 +       sb = file->f_path.dentry->d_sb;
25944 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25945 +
25946 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
25947 +       err = PTR_ERR(h_file);
25948 +       if (IS_ERR(h_file))
25949 +               goto out;
25950 +
25951 +       /* it is not an error if h_file has no operation */
25952 +       mask = DEFAULT_POLLMASK;
25953 +       if (h_file->f_op->poll)
25954 +               mask = h_file->f_op->poll(h_file, wait);
25955 +       fput(h_file); /* instead of au_read_post() */
25956 +
25957 +out:
25958 +       si_read_unlock(sb);
25959 +       AuTraceErr((int)mask);
25960 +       return mask;
25961 +}
25962 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25963 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25964 +++ linux/fs/aufs/posix_acl.c   2017-07-29 12:14:25.906375514 +0200
25965 @@ -0,0 +1,102 @@
25966 +/*
25967 + * Copyright (C) 2014-2017 Junjiro R. Okajima
25968 + *
25969 + * This program, aufs is free software; you can redistribute it and/or modify
25970 + * it under the terms of the GNU General Public License as published by
25971 + * the Free Software Foundation; either version 2 of the License, or
25972 + * (at your option) any later version.
25973 + *
25974 + * This program is distributed in the hope that it will be useful,
25975 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25976 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25977 + * GNU General Public License for more details.
25978 + *
25979 + * You should have received a copy of the GNU General Public License
25980 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25981 + */
25982 +
25983 +/*
25984 + * posix acl operations
25985 + */
25986 +
25987 +#include <linux/fs.h>
25988 +#include "aufs.h"
25989 +
25990 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25991 +{
25992 +       struct posix_acl *acl;
25993 +       int err;
25994 +       aufs_bindex_t bindex;
25995 +       struct inode *h_inode;
25996 +       struct super_block *sb;
25997 +
25998 +       acl = NULL;
25999 +       sb = inode->i_sb;
26000 +       si_read_lock(sb, AuLock_FLUSH);
26001 +       ii_read_lock_child(inode);
26002 +       if (!(sb->s_flags & MS_POSIXACL))
26003 +               goto out;
26004 +
26005 +       bindex = au_ibtop(inode);
26006 +       h_inode = au_h_iptr(inode, bindex);
26007 +       if (unlikely(!h_inode
26008 +                    || ((h_inode->i_mode & S_IFMT)
26009 +                        != (inode->i_mode & S_IFMT)))) {
26010 +               err = au_busy_or_stale();
26011 +               acl = ERR_PTR(err);
26012 +               goto out;
26013 +       }
26014 +
26015 +       /* always topmost only */
26016 +       acl = get_acl(h_inode, type);
26017 +       if (!IS_ERR_OR_NULL(acl))
26018 +               set_cached_acl(inode, type, acl);
26019 +
26020 +out:
26021 +       ii_read_unlock(inode);
26022 +       si_read_unlock(sb);
26023 +
26024 +       AuTraceErrPtr(acl);
26025 +       return acl;
26026 +}
26027 +
26028 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
26029 +{
26030 +       int err;
26031 +       ssize_t ssz;
26032 +       struct dentry *dentry;
26033 +       struct au_sxattr arg = {
26034 +               .type = AU_ACL_SET,
26035 +               .u.acl_set = {
26036 +                       .acl    = acl,
26037 +                       .type   = type
26038 +               },
26039 +       };
26040 +
26041 +       IMustLock(inode);
26042 +
26043 +       if (inode->i_ino == AUFS_ROOT_INO)
26044 +               dentry = dget(inode->i_sb->s_root);
26045 +       else {
26046 +               dentry = d_find_alias(inode);
26047 +               if (!dentry)
26048 +                       dentry = d_find_any_alias(inode);
26049 +               if (!dentry) {
26050 +                       pr_warn("cannot handle this inode, "
26051 +                               "please report to aufs-users ML\n");
26052 +                       err = -ENOENT;
26053 +                       goto out;
26054 +               }
26055 +       }
26056 +
26057 +       ssz = au_sxattr(dentry, inode, &arg);
26058 +       dput(dentry);
26059 +       err = ssz;
26060 +       if (ssz >= 0) {
26061 +               err = 0;
26062 +               set_cached_acl(inode, type, acl);
26063 +       }
26064 +
26065 +out:
26066 +       return err;
26067 +}
26068 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
26069 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
26070 +++ linux/fs/aufs/procfs.c      2017-07-29 12:14:25.906375514 +0200
26071 @@ -0,0 +1,169 @@
26072 +/*
26073 + * Copyright (C) 2010-2017 Junjiro R. Okajima
26074 + *
26075 + * This program, aufs is free software; you can redistribute it and/or modify
26076 + * it under the terms of the GNU General Public License as published by
26077 + * the Free Software Foundation; either version 2 of the License, or
26078 + * (at your option) any later version.
26079 + *
26080 + * This program is distributed in the hope that it will be useful,
26081 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26082 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26083 + * GNU General Public License for more details.
26084 + *
26085 + * You should have received a copy of the GNU General Public License
26086 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26087 + */
26088 +
26089 +/*
26090 + * procfs interfaces
26091 + */
26092 +
26093 +#include <linux/proc_fs.h>
26094 +#include "aufs.h"
26095 +
26096 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
26097 +{
26098 +       struct au_sbinfo *sbinfo;
26099 +
26100 +       sbinfo = file->private_data;
26101 +       if (sbinfo) {
26102 +               au_plink_maint_leave(sbinfo);
26103 +               kobject_put(&sbinfo->si_kobj);
26104 +       }
26105 +
26106 +       return 0;
26107 +}
26108 +
26109 +static void au_procfs_plm_write_clean(struct file *file)
26110 +{
26111 +       struct au_sbinfo *sbinfo;
26112 +
26113 +       sbinfo = file->private_data;
26114 +       if (sbinfo)
26115 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
26116 +}
26117 +
26118 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
26119 +{
26120 +       int err;
26121 +       struct super_block *sb;
26122 +       struct au_sbinfo *sbinfo;
26123 +
26124 +       err = -EBUSY;
26125 +       if (unlikely(file->private_data))
26126 +               goto out;
26127 +
26128 +       sb = NULL;
26129 +       /* don't use au_sbilist_lock() here */
26130 +       spin_lock(&au_sbilist.spin);
26131 +       hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
26132 +               if (id == sysaufs_si_id(sbinfo)) {
26133 +                       kobject_get(&sbinfo->si_kobj);
26134 +                       sb = sbinfo->si_sb;
26135 +                       break;
26136 +               }
26137 +       spin_unlock(&au_sbilist.spin);
26138 +
26139 +       err = -EINVAL;
26140 +       if (unlikely(!sb))
26141 +               goto out;
26142 +
26143 +       err = au_plink_maint_enter(sb);
26144 +       if (!err)
26145 +               /* keep kobject_get() */
26146 +               file->private_data = sbinfo;
26147 +       else
26148 +               kobject_put(&sbinfo->si_kobj);
26149 +out:
26150 +       return err;
26151 +}
26152 +
26153 +/*
26154 + * Accept a valid "si=xxxx" only.
26155 + * Once it is accepted successfully, accept "clean" too.
26156 + */
26157 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
26158 +                                  size_t count, loff_t *ppos)
26159 +{
26160 +       ssize_t err;
26161 +       unsigned long id;
26162 +       /* last newline is allowed */
26163 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
26164 +
26165 +       err = -EACCES;
26166 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26167 +               goto out;
26168 +
26169 +       err = -EINVAL;
26170 +       if (unlikely(count > sizeof(buf)))
26171 +               goto out;
26172 +
26173 +       err = copy_from_user(buf, ubuf, count);
26174 +       if (unlikely(err)) {
26175 +               err = -EFAULT;
26176 +               goto out;
26177 +       }
26178 +       buf[count] = 0;
26179 +
26180 +       err = -EINVAL;
26181 +       if (!strcmp("clean", buf)) {
26182 +               au_procfs_plm_write_clean(file);
26183 +               goto out_success;
26184 +       } else if (unlikely(strncmp("si=", buf, 3)))
26185 +               goto out;
26186 +
26187 +       err = kstrtoul(buf + 3, 16, &id);
26188 +       if (unlikely(err))
26189 +               goto out;
26190 +
26191 +       err = au_procfs_plm_write_si(file, id);
26192 +       if (unlikely(err))
26193 +               goto out;
26194 +
26195 +out_success:
26196 +       err = count; /* success */
26197 +out:
26198 +       return err;
26199 +}
26200 +
26201 +static const struct file_operations au_procfs_plm_fop = {
26202 +       .write          = au_procfs_plm_write,
26203 +       .release        = au_procfs_plm_release,
26204 +       .owner          = THIS_MODULE
26205 +};
26206 +
26207 +/* ---------------------------------------------------------------------- */
26208 +
26209 +static struct proc_dir_entry *au_procfs_dir;
26210 +
26211 +void au_procfs_fin(void)
26212 +{
26213 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
26214 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
26215 +}
26216 +
26217 +int __init au_procfs_init(void)
26218 +{
26219 +       int err;
26220 +       struct proc_dir_entry *entry;
26221 +
26222 +       err = -ENOMEM;
26223 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
26224 +       if (unlikely(!au_procfs_dir))
26225 +               goto out;
26226 +
26227 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
26228 +                           au_procfs_dir, &au_procfs_plm_fop);
26229 +       if (unlikely(!entry))
26230 +               goto out_dir;
26231 +
26232 +       err = 0;
26233 +       goto out; /* success */
26234 +
26235 +
26236 +out_dir:
26237 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
26238 +out:
26239 +       return err;
26240 +}
26241 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
26242 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
26243 +++ linux/fs/aufs/rdu.c 2017-07-29 12:14:25.906375514 +0200
26244 @@ -0,0 +1,381 @@
26245 +/*
26246 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26247 + *
26248 + * This program, aufs is free software; you can redistribute it and/or modify
26249 + * it under the terms of the GNU General Public License as published by
26250 + * the Free Software Foundation; either version 2 of the License, or
26251 + * (at your option) any later version.
26252 + *
26253 + * This program is distributed in the hope that it will be useful,
26254 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26255 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26256 + * GNU General Public License for more details.
26257 + *
26258 + * You should have received a copy of the GNU General Public License
26259 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26260 + */
26261 +
26262 +/*
26263 + * readdir in userspace.
26264 + */
26265 +
26266 +#include <linux/compat.h>
26267 +#include <linux/fs_stack.h>
26268 +#include <linux/security.h>
26269 +#include "aufs.h"
26270 +
26271 +/* bits for struct aufs_rdu.flags */
26272 +#define        AuRdu_CALLED    1
26273 +#define        AuRdu_CONT      (1 << 1)
26274 +#define        AuRdu_FULL      (1 << 2)
26275 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
26276 +#define au_fset_rdu(flags, name) \
26277 +       do { (flags) |= AuRdu_##name; } while (0)
26278 +#define au_fclr_rdu(flags, name) \
26279 +       do { (flags) &= ~AuRdu_##name; } while (0)
26280 +
26281 +struct au_rdu_arg {
26282 +       struct dir_context              ctx;
26283 +       struct aufs_rdu                 *rdu;
26284 +       union au_rdu_ent_ul             ent;
26285 +       unsigned long                   end;
26286 +
26287 +       struct super_block              *sb;
26288 +       int                             err;
26289 +};
26290 +
26291 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
26292 +                      loff_t offset, u64 h_ino, unsigned int d_type)
26293 +{
26294 +       int err, len;
26295 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
26296 +       struct aufs_rdu *rdu = arg->rdu;
26297 +       struct au_rdu_ent ent;
26298 +
26299 +       err = 0;
26300 +       arg->err = 0;
26301 +       au_fset_rdu(rdu->cookie.flags, CALLED);
26302 +       len = au_rdu_len(nlen);
26303 +       if (arg->ent.ul + len  < arg->end) {
26304 +               ent.ino = h_ino;
26305 +               ent.bindex = rdu->cookie.bindex;
26306 +               ent.type = d_type;
26307 +               ent.nlen = nlen;
26308 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
26309 +                       ent.type = DT_UNKNOWN;
26310 +
26311 +               /* unnecessary to support mmap_sem since this is a dir */
26312 +               err = -EFAULT;
26313 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
26314 +                       goto out;
26315 +               if (copy_to_user(arg->ent.e->name, name, nlen))
26316 +                       goto out;
26317 +               /* the terminating NULL */
26318 +               if (__put_user(0, arg->ent.e->name + nlen))
26319 +                       goto out;
26320 +               err = 0;
26321 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
26322 +               arg->ent.ul += len;
26323 +               rdu->rent++;
26324 +       } else {
26325 +               err = -EFAULT;
26326 +               au_fset_rdu(rdu->cookie.flags, FULL);
26327 +               rdu->full = 1;
26328 +               rdu->tail = arg->ent;
26329 +       }
26330 +
26331 +out:
26332 +       /* AuTraceErr(err); */
26333 +       return err;
26334 +}
26335 +
26336 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
26337 +{
26338 +       int err;
26339 +       loff_t offset;
26340 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
26341 +
26342 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
26343 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
26344 +       err = offset;
26345 +       if (unlikely(offset != cookie->h_pos))
26346 +               goto out;
26347 +
26348 +       err = 0;
26349 +       do {
26350 +               arg->err = 0;
26351 +               au_fclr_rdu(cookie->flags, CALLED);
26352 +               /* smp_mb(); */
26353 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
26354 +               if (err >= 0)
26355 +                       err = arg->err;
26356 +       } while (!err
26357 +                && au_ftest_rdu(cookie->flags, CALLED)
26358 +                && !au_ftest_rdu(cookie->flags, FULL));
26359 +       cookie->h_pos = h_file->f_pos;
26360 +
26361 +out:
26362 +       AuTraceErr(err);
26363 +       return err;
26364 +}
26365 +
26366 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
26367 +{
26368 +       int err;
26369 +       aufs_bindex_t bbot;
26370 +       struct au_rdu_arg arg = {
26371 +               .ctx = {
26372 +                       .actor = au_rdu_fill
26373 +               }
26374 +       };
26375 +       struct dentry *dentry;
26376 +       struct inode *inode;
26377 +       struct file *h_file;
26378 +       struct au_rdu_cookie *cookie = &rdu->cookie;
26379 +
26380 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
26381 +       if (unlikely(err)) {
26382 +               err = -EFAULT;
26383 +               AuTraceErr(err);
26384 +               goto out;
26385 +       }
26386 +       rdu->rent = 0;
26387 +       rdu->tail = rdu->ent;
26388 +       rdu->full = 0;
26389 +       arg.rdu = rdu;
26390 +       arg.ent = rdu->ent;
26391 +       arg.end = arg.ent.ul;
26392 +       arg.end += rdu->sz;
26393 +
26394 +       err = -ENOTDIR;
26395 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
26396 +               goto out;
26397 +
26398 +       err = security_file_permission(file, MAY_READ);
26399 +       AuTraceErr(err);
26400 +       if (unlikely(err))
26401 +               goto out;
26402 +
26403 +       dentry = file->f_path.dentry;
26404 +       inode = d_inode(dentry);
26405 +       inode_lock_shared(inode);
26406 +
26407 +       arg.sb = inode->i_sb;
26408 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
26409 +       if (unlikely(err))
26410 +               goto out_mtx;
26411 +       err = au_alive_dir(dentry);
26412 +       if (unlikely(err))
26413 +               goto out_si;
26414 +       /* todo: reval? */
26415 +       fi_read_lock(file);
26416 +
26417 +       err = -EAGAIN;
26418 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
26419 +                    && cookie->generation != au_figen(file)))
26420 +               goto out_unlock;
26421 +
26422 +       err = 0;
26423 +       if (!rdu->blk) {
26424 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
26425 +               if (!rdu->blk)
26426 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
26427 +       }
26428 +       bbot = au_fbtop(file);
26429 +       if (cookie->bindex < bbot)
26430 +               cookie->bindex = bbot;
26431 +       bbot = au_fbbot_dir(file);
26432 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
26433 +       for (; !err && cookie->bindex <= bbot;
26434 +            cookie->bindex++, cookie->h_pos = 0) {
26435 +               h_file = au_hf_dir(file, cookie->bindex);
26436 +               if (!h_file)
26437 +                       continue;
26438 +
26439 +               au_fclr_rdu(cookie->flags, FULL);
26440 +               err = au_rdu_do(h_file, &arg);
26441 +               AuTraceErr(err);
26442 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
26443 +                       break;
26444 +       }
26445 +       AuDbg("rent %llu\n", rdu->rent);
26446 +
26447 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
26448 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
26449 +               au_fset_rdu(cookie->flags, CONT);
26450 +               cookie->generation = au_figen(file);
26451 +       }
26452 +
26453 +       ii_read_lock_child(inode);
26454 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
26455 +       ii_read_unlock(inode);
26456 +
26457 +out_unlock:
26458 +       fi_read_unlock(file);
26459 +out_si:
26460 +       si_read_unlock(arg.sb);
26461 +out_mtx:
26462 +       inode_unlock_shared(inode);
26463 +out:
26464 +       AuTraceErr(err);
26465 +       return err;
26466 +}
26467 +
26468 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
26469 +{
26470 +       int err;
26471 +       ino_t ino;
26472 +       unsigned long long nent;
26473 +       union au_rdu_ent_ul *u;
26474 +       struct au_rdu_ent ent;
26475 +       struct super_block *sb;
26476 +
26477 +       err = 0;
26478 +       nent = rdu->nent;
26479 +       u = &rdu->ent;
26480 +       sb = file->f_path.dentry->d_sb;
26481 +       si_read_lock(sb, AuLock_FLUSH);
26482 +       while (nent-- > 0) {
26483 +               /* unnecessary to support mmap_sem since this is a dir */
26484 +               err = copy_from_user(&ent, u->e, sizeof(ent));
26485 +               if (!err)
26486 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
26487 +               if (unlikely(err)) {
26488 +                       err = -EFAULT;
26489 +                       AuTraceErr(err);
26490 +                       break;
26491 +               }
26492 +
26493 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
26494 +               if (!ent.wh)
26495 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
26496 +               else
26497 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
26498 +                                       &ino);
26499 +               if (unlikely(err)) {
26500 +                       AuTraceErr(err);
26501 +                       break;
26502 +               }
26503 +
26504 +               err = __put_user(ino, &u->e->ino);
26505 +               if (unlikely(err)) {
26506 +                       err = -EFAULT;
26507 +                       AuTraceErr(err);
26508 +                       break;
26509 +               }
26510 +               u->ul += au_rdu_len(ent.nlen);
26511 +       }
26512 +       si_read_unlock(sb);
26513 +
26514 +       return err;
26515 +}
26516 +
26517 +/* ---------------------------------------------------------------------- */
26518 +
26519 +static int au_rdu_verify(struct aufs_rdu *rdu)
26520 +{
26521 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
26522 +             "%llu, b%d, 0x%x, g%u}\n",
26523 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
26524 +             rdu->blk,
26525 +             rdu->rent, rdu->shwh, rdu->full,
26526 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
26527 +             rdu->cookie.generation);
26528 +
26529 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
26530 +               return 0;
26531 +
26532 +       AuDbg("%u:%u\n",
26533 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
26534 +       return -EINVAL;
26535 +}
26536 +
26537 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26538 +{
26539 +       long err, e;
26540 +       struct aufs_rdu rdu;
26541 +       void __user *p = (void __user *)arg;
26542 +
26543 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26544 +       if (unlikely(err)) {
26545 +               err = -EFAULT;
26546 +               AuTraceErr(err);
26547 +               goto out;
26548 +       }
26549 +       err = au_rdu_verify(&rdu);
26550 +       if (unlikely(err))
26551 +               goto out;
26552 +
26553 +       switch (cmd) {
26554 +       case AUFS_CTL_RDU:
26555 +               err = au_rdu(file, &rdu);
26556 +               if (unlikely(err))
26557 +                       break;
26558 +
26559 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26560 +               if (unlikely(e)) {
26561 +                       err = -EFAULT;
26562 +                       AuTraceErr(err);
26563 +               }
26564 +               break;
26565 +       case AUFS_CTL_RDU_INO:
26566 +               err = au_rdu_ino(file, &rdu);
26567 +               break;
26568 +
26569 +       default:
26570 +               /* err = -ENOTTY; */
26571 +               err = -EINVAL;
26572 +       }
26573 +
26574 +out:
26575 +       AuTraceErr(err);
26576 +       return err;
26577 +}
26578 +
26579 +#ifdef CONFIG_COMPAT
26580 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26581 +{
26582 +       long err, e;
26583 +       struct aufs_rdu rdu;
26584 +       void __user *p = compat_ptr(arg);
26585 +
26586 +       /* todo: get_user()? */
26587 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26588 +       if (unlikely(err)) {
26589 +               err = -EFAULT;
26590 +               AuTraceErr(err);
26591 +               goto out;
26592 +       }
26593 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
26594 +       err = au_rdu_verify(&rdu);
26595 +       if (unlikely(err))
26596 +               goto out;
26597 +
26598 +       switch (cmd) {
26599 +       case AUFS_CTL_RDU:
26600 +               err = au_rdu(file, &rdu);
26601 +               if (unlikely(err))
26602 +                       break;
26603 +
26604 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
26605 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
26606 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26607 +               if (unlikely(e)) {
26608 +                       err = -EFAULT;
26609 +                       AuTraceErr(err);
26610 +               }
26611 +               break;
26612 +       case AUFS_CTL_RDU_INO:
26613 +               err = au_rdu_ino(file, &rdu);
26614 +               break;
26615 +
26616 +       default:
26617 +               /* err = -ENOTTY; */
26618 +               err = -EINVAL;
26619 +       }
26620 +
26621 +out:
26622 +       AuTraceErr(err);
26623 +       return err;
26624 +}
26625 +#endif
26626 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26627 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
26628 +++ linux/fs/aufs/rwsem.h       2017-07-29 12:14:25.906375514 +0200
26629 @@ -0,0 +1,198 @@
26630 +/*
26631 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26632 + *
26633 + * This program, aufs is free software; you can redistribute it and/or modify
26634 + * it under the terms of the GNU General Public License as published by
26635 + * the Free Software Foundation; either version 2 of the License, or
26636 + * (at your option) any later version.
26637 + *
26638 + * This program is distributed in the hope that it will be useful,
26639 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26640 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26641 + * GNU General Public License for more details.
26642 + *
26643 + * You should have received a copy of the GNU General Public License
26644 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26645 + */
26646 +
26647 +/*
26648 + * simple read-write semaphore wrappers
26649 + */
26650 +
26651 +#ifndef __AUFS_RWSEM_H__
26652 +#define __AUFS_RWSEM_H__
26653 +
26654 +#ifdef __KERNEL__
26655 +
26656 +#include "debug.h"
26657 +
26658 +struct au_rwsem {
26659 +       struct rw_semaphore     rwsem;
26660 +#ifdef CONFIG_AUFS_DEBUG
26661 +       /* just for debugging, not almighty counter */
26662 +       atomic_t                rcnt, wcnt;
26663 +#endif
26664 +};
26665 +
26666 +#ifdef CONFIG_LOCKDEP
26667 +#define au_lockdep_set_name(rw)                                                \
26668 +       lockdep_set_class_and_name(&(rw)->rwsem,                        \
26669 +                                  /*original key*/(rw)->rwsem.dep_map.key, \
26670 +                                  /*name*/#rw)
26671 +#else
26672 +#define au_lockdep_set_name(rw) do {} while (0)
26673 +#endif
26674 +
26675 +#ifdef CONFIG_AUFS_DEBUG
26676 +#define AuDbgCntInit(rw) do { \
26677 +       atomic_set(&(rw)->rcnt, 0); \
26678 +       atomic_set(&(rw)->wcnt, 0); \
26679 +       smp_mb(); /* atomic set */ \
26680 +} while (0)
26681 +
26682 +#define AuDbgCnt(rw, cnt)      atomic_read(&(rw)->cnt)
26683 +#define AuDbgCntInc(rw, cnt)   atomic_inc(&(rw)->cnt)
26684 +#define AuDbgCntDec(rw, cnt)   WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
26685 +#define AuDbgRcntInc(rw)       AuDbgCntInc(rw, rcnt)
26686 +#define AuDbgRcntDec(rw)       AuDbgCntDec(rw, rcnt)
26687 +#define AuDbgWcntInc(rw)       AuDbgCntInc(rw, wcnt)
26688 +#define AuDbgWcntDec(rw)       AuDbgCntDec(rw, wcnt)
26689 +#else
26690 +#define AuDbgCnt(rw, cnt)      0
26691 +#define AuDbgCntInit(rw)       do {} while (0)
26692 +#define AuDbgRcntInc(rw)       do {} while (0)
26693 +#define AuDbgRcntDec(rw)       do {} while (0)
26694 +#define AuDbgWcntInc(rw)       do {} while (0)
26695 +#define AuDbgWcntDec(rw)       do {} while (0)
26696 +#endif /* CONFIG_AUFS_DEBUG */
26697 +
26698 +/* to debug easier, do not make them inlined functions */
26699 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
26700 +/* rwsem_is_locked() is unusable */
26701 +#define AuRwMustReadLock(rw)   AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
26702 +#define AuRwMustWriteLock(rw)  AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
26703 +#define AuRwMustAnyLock(rw)    AuDebugOn(AuDbgCnt(rw, rcnt) <= 0       \
26704 +                                         && AuDbgCnt(rw, wcnt) <= 0)
26705 +#define AuRwDestroy(rw)                AuDebugOn(AuDbgCnt(rw, rcnt)            \
26706 +                                         || AuDbgCnt(rw, wcnt))
26707 +
26708 +#define au_rw_init(rw) do {                    \
26709 +               AuDbgCntInit(rw);               \
26710 +               init_rwsem(&(rw)->rwsem);       \
26711 +               au_lockdep_set_name(rw);        \
26712 +       } while (0)
26713 +
26714 +#define au_rw_init_wlock(rw) do {              \
26715 +               au_rw_init(rw);                 \
26716 +               down_write(&(rw)->rwsem);       \
26717 +               AuDbgWcntInc(rw);               \
26718 +       } while (0)
26719 +
26720 +#define au_rw_init_wlock_nested(rw, lsc) do { \
26721 +               au_rw_init(rw);                         \
26722 +               down_write_nested(&(rw)->rwsem, lsc);   \
26723 +               AuDbgWcntInc(rw);                       \
26724 +       } while (0)
26725 +
26726 +static inline void au_rw_read_lock(struct au_rwsem *rw)
26727 +{
26728 +       down_read(&rw->rwsem);
26729 +       AuDbgRcntInc(rw);
26730 +}
26731 +
26732 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26733 +{
26734 +       down_read_nested(&rw->rwsem, lsc);
26735 +       AuDbgRcntInc(rw);
26736 +}
26737 +
26738 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
26739 +{
26740 +       AuRwMustReadLock(rw);
26741 +       AuDbgRcntDec(rw);
26742 +       up_read(&rw->rwsem);
26743 +}
26744 +
26745 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26746 +{
26747 +       AuRwMustWriteLock(rw);
26748 +       AuDbgRcntInc(rw);
26749 +       AuDbgWcntDec(rw);
26750 +       downgrade_write(&rw->rwsem);
26751 +}
26752 +
26753 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26754 +{
26755 +       down_write(&rw->rwsem);
26756 +       AuDbgWcntInc(rw);
26757 +}
26758 +
26759 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26760 +                                          unsigned int lsc)
26761 +{
26762 +       down_write_nested(&rw->rwsem, lsc);
26763 +       AuDbgWcntInc(rw);
26764 +}
26765 +
26766 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26767 +{
26768 +       AuRwMustWriteLock(rw);
26769 +       AuDbgWcntDec(rw);
26770 +       up_write(&rw->rwsem);
26771 +}
26772 +
26773 +/* why is not _nested version defined */
26774 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26775 +{
26776 +       int ret;
26777 +
26778 +       ret = down_read_trylock(&rw->rwsem);
26779 +       if (ret)
26780 +               AuDbgRcntInc(rw);
26781 +       return ret;
26782 +}
26783 +
26784 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26785 +{
26786 +       int ret;
26787 +
26788 +       ret = down_write_trylock(&rw->rwsem);
26789 +       if (ret)
26790 +               AuDbgWcntInc(rw);
26791 +       return ret;
26792 +}
26793 +
26794 +#undef AuDbgCntDec
26795 +#undef AuDbgRcntInc
26796 +#undef AuDbgRcntDec
26797 +#undef AuDbgWcntDec
26798 +
26799 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26800 +static inline void prefix##_read_lock(param) \
26801 +{ au_rw_read_lock(rwsem); } \
26802 +static inline void prefix##_write_lock(param) \
26803 +{ au_rw_write_lock(rwsem); } \
26804 +static inline int prefix##_read_trylock(param) \
26805 +{ return au_rw_read_trylock(rwsem); } \
26806 +static inline int prefix##_write_trylock(param) \
26807 +{ return au_rw_write_trylock(rwsem); }
26808 +/* why is not _nested version defined */
26809 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26810 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26811 +static inline void prefix##_write_trylock_nestd(param, lsc)
26812 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26813 +
26814 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26815 +static inline void prefix##_read_unlock(param) \
26816 +{ au_rw_read_unlock(rwsem); } \
26817 +static inline void prefix##_write_unlock(param) \
26818 +{ au_rw_write_unlock(rwsem); } \
26819 +static inline void prefix##_downgrade_lock(param) \
26820 +{ au_rw_dgrade_lock(rwsem); }
26821 +
26822 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26823 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26824 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26825 +
26826 +#endif /* __KERNEL__ */
26827 +#endif /* __AUFS_RWSEM_H__ */
26828 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26829 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26830 +++ linux/fs/aufs/sbinfo.c      2017-07-29 12:14:25.906375514 +0200
26831 @@ -0,0 +1,304 @@
26832 +/*
26833 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26834 + *
26835 + * This program, aufs is free software; you can redistribute it and/or modify
26836 + * it under the terms of the GNU General Public License as published by
26837 + * the Free Software Foundation; either version 2 of the License, or
26838 + * (at your option) any later version.
26839 + *
26840 + * This program is distributed in the hope that it will be useful,
26841 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26842 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26843 + * GNU General Public License for more details.
26844 + *
26845 + * You should have received a copy of the GNU General Public License
26846 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26847 + */
26848 +
26849 +/*
26850 + * superblock private data
26851 + */
26852 +
26853 +#include "aufs.h"
26854 +
26855 +/*
26856 + * they are necessary regardless sysfs is disabled.
26857 + */
26858 +void au_si_free(struct kobject *kobj)
26859 +{
26860 +       int i;
26861 +       struct au_sbinfo *sbinfo;
26862 +       char *locked __maybe_unused; /* debug only */
26863 +
26864 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26865 +       for (i = 0; i < AuPlink_NHASH; i++)
26866 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26867 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26868 +
26869 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
26870 +       percpu_counter_destroy(&sbinfo->si_ninodes);
26871 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
26872 +       percpu_counter_destroy(&sbinfo->si_nfiles);
26873 +
26874 +       au_rw_write_lock(&sbinfo->si_rwsem);
26875 +       au_br_free(sbinfo);
26876 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26877 +
26878 +       kfree(sbinfo->si_branch);
26879 +       mutex_destroy(&sbinfo->si_xib_mtx);
26880 +       AuRwDestroy(&sbinfo->si_rwsem);
26881 +
26882 +       kfree(sbinfo);
26883 +}
26884 +
26885 +int au_si_alloc(struct super_block *sb)
26886 +{
26887 +       int err, i;
26888 +       struct au_sbinfo *sbinfo;
26889 +
26890 +       err = -ENOMEM;
26891 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26892 +       if (unlikely(!sbinfo))
26893 +               goto out;
26894 +
26895 +       /* will be reallocated separately */
26896 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26897 +       if (unlikely(!sbinfo->si_branch))
26898 +               goto out_sbinfo;
26899 +
26900 +       err = sysaufs_si_init(sbinfo);
26901 +       if (unlikely(err))
26902 +               goto out_br;
26903 +
26904 +       au_nwt_init(&sbinfo->si_nowait);
26905 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26906 +
26907 +       percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
26908 +       percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
26909 +
26910 +       sbinfo->si_bbot = -1;
26911 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26912 +
26913 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26914 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26915 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26916 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26917 +
26918 +       au_fhsm_init(sbinfo);
26919 +
26920 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26921 +
26922 +       sbinfo->si_xino_jiffy = jiffies;
26923 +       sbinfo->si_xino_expire
26924 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26925 +       mutex_init(&sbinfo->si_xib_mtx);
26926 +       sbinfo->si_xino_brid = -1;
26927 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26928 +
26929 +       au_sphl_init(&sbinfo->si_aopen);
26930 +
26931 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26932 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26933 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26934 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26935 +
26936 +       for (i = 0; i < AuPlink_NHASH; i++)
26937 +               au_sphl_init(sbinfo->si_plink + i);
26938 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26939 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26940 +
26941 +       au_sphl_init(&sbinfo->si_files);
26942 +
26943 +       /* with getattr by default */
26944 +       sbinfo->si_iop_array = aufs_iop;
26945 +
26946 +       /* leave other members for sysaufs and si_mnt. */
26947 +       sbinfo->si_sb = sb;
26948 +       sb->s_fs_info = sbinfo;
26949 +       si_pid_set(sb);
26950 +       return 0; /* success */
26951 +
26952 +out_br:
26953 +       kfree(sbinfo->si_branch);
26954 +out_sbinfo:
26955 +       kfree(sbinfo);
26956 +out:
26957 +       return err;
26958 +}
26959 +
26960 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
26961 +{
26962 +       int err, sz;
26963 +       struct au_branch **brp;
26964 +
26965 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26966 +
26967 +       err = -ENOMEM;
26968 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
26969 +       if (unlikely(!sz))
26970 +               sz = sizeof(*brp);
26971 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
26972 +                          may_shrink);
26973 +       if (brp) {
26974 +               sbinfo->si_branch = brp;
26975 +               err = 0;
26976 +       }
26977 +
26978 +       return err;
26979 +}
26980 +
26981 +/* ---------------------------------------------------------------------- */
26982 +
26983 +unsigned int au_sigen_inc(struct super_block *sb)
26984 +{
26985 +       unsigned int gen;
26986 +       struct inode *inode;
26987 +
26988 +       SiMustWriteLock(sb);
26989 +
26990 +       gen = ++au_sbi(sb)->si_generation;
26991 +       au_update_digen(sb->s_root);
26992 +       inode = d_inode(sb->s_root);
26993 +       au_update_iigen(inode, /*half*/0);
26994 +       inode->i_version++;
26995 +       return gen;
26996 +}
26997 +
26998 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26999 +{
27000 +       aufs_bindex_t br_id;
27001 +       int i;
27002 +       struct au_sbinfo *sbinfo;
27003 +
27004 +       SiMustWriteLock(sb);
27005 +
27006 +       sbinfo = au_sbi(sb);
27007 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
27008 +               br_id = ++sbinfo->si_last_br_id;
27009 +               AuDebugOn(br_id < 0);
27010 +               if (br_id && au_br_index(sb, br_id) < 0)
27011 +                       return br_id;
27012 +       }
27013 +
27014 +       return -1;
27015 +}
27016 +
27017 +/* ---------------------------------------------------------------------- */
27018 +
27019 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
27020 +int si_read_lock(struct super_block *sb, int flags)
27021 +{
27022 +       int err;
27023 +
27024 +       err = 0;
27025 +       if (au_ftest_lock(flags, FLUSH))
27026 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
27027 +
27028 +       si_noflush_read_lock(sb);
27029 +       err = au_plink_maint(sb, flags);
27030 +       if (unlikely(err))
27031 +               si_read_unlock(sb);
27032 +
27033 +       return err;
27034 +}
27035 +
27036 +int si_write_lock(struct super_block *sb, int flags)
27037 +{
27038 +       int err;
27039 +
27040 +       if (au_ftest_lock(flags, FLUSH))
27041 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
27042 +
27043 +       si_noflush_write_lock(sb);
27044 +       err = au_plink_maint(sb, flags);
27045 +       if (unlikely(err))
27046 +               si_write_unlock(sb);
27047 +
27048 +       return err;
27049 +}
27050 +
27051 +/* dentry and super_block lock. call at entry point */
27052 +int aufs_read_lock(struct dentry *dentry, int flags)
27053 +{
27054 +       int err;
27055 +       struct super_block *sb;
27056 +
27057 +       sb = dentry->d_sb;
27058 +       err = si_read_lock(sb, flags);
27059 +       if (unlikely(err))
27060 +               goto out;
27061 +
27062 +       if (au_ftest_lock(flags, DW))
27063 +               di_write_lock_child(dentry);
27064 +       else
27065 +               di_read_lock_child(dentry, flags);
27066 +
27067 +       if (au_ftest_lock(flags, GEN)) {
27068 +               err = au_digen_test(dentry, au_sigen(sb));
27069 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
27070 +                       AuDebugOn(!err && au_dbrange_test(dentry));
27071 +               else if (!err)
27072 +                       err = au_dbrange_test(dentry);
27073 +               if (unlikely(err))
27074 +                       aufs_read_unlock(dentry, flags);
27075 +       }
27076 +
27077 +out:
27078 +       return err;
27079 +}
27080 +
27081 +void aufs_read_unlock(struct dentry *dentry, int flags)
27082 +{
27083 +       if (au_ftest_lock(flags, DW))
27084 +               di_write_unlock(dentry);
27085 +       else
27086 +               di_read_unlock(dentry, flags);
27087 +       si_read_unlock(dentry->d_sb);
27088 +}
27089 +
27090 +void aufs_write_lock(struct dentry *dentry)
27091 +{
27092 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
27093 +       di_write_lock_child(dentry);
27094 +}
27095 +
27096 +void aufs_write_unlock(struct dentry *dentry)
27097 +{
27098 +       di_write_unlock(dentry);
27099 +       si_write_unlock(dentry->d_sb);
27100 +}
27101 +
27102 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
27103 +{
27104 +       int err;
27105 +       unsigned int sigen;
27106 +       struct super_block *sb;
27107 +
27108 +       sb = d1->d_sb;
27109 +       err = si_read_lock(sb, flags);
27110 +       if (unlikely(err))
27111 +               goto out;
27112 +
27113 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
27114 +
27115 +       if (au_ftest_lock(flags, GEN)) {
27116 +               sigen = au_sigen(sb);
27117 +               err = au_digen_test(d1, sigen);
27118 +               AuDebugOn(!err && au_dbrange_test(d1));
27119 +               if (!err) {
27120 +                       err = au_digen_test(d2, sigen);
27121 +                       AuDebugOn(!err && au_dbrange_test(d2));
27122 +               }
27123 +               if (unlikely(err))
27124 +                       aufs_read_and_write_unlock2(d1, d2);
27125 +       }
27126 +
27127 +out:
27128 +       return err;
27129 +}
27130 +
27131 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
27132 +{
27133 +       di_write_unlock2(d1, d2);
27134 +       si_read_unlock(d1->d_sb);
27135 +}
27136 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
27137 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
27138 +++ linux/fs/aufs/spl.h 2017-07-29 12:14:25.906375514 +0200
27139 @@ -0,0 +1,113 @@
27140 +/*
27141 + * Copyright (C) 2005-2017 Junjiro R. Okajima
27142 + *
27143 + * This program, aufs is free software; you can redistribute it and/or modify
27144 + * it under the terms of the GNU General Public License as published by
27145 + * the Free Software Foundation; either version 2 of the License, or
27146 + * (at your option) any later version.
27147 + *
27148 + * This program is distributed in the hope that it will be useful,
27149 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27150 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27151 + * GNU General Public License for more details.
27152 + *
27153 + * You should have received a copy of the GNU General Public License
27154 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27155 + */
27156 +
27157 +/*
27158 + * simple list protected by a spinlock
27159 + */
27160 +
27161 +#ifndef __AUFS_SPL_H__
27162 +#define __AUFS_SPL_H__
27163 +
27164 +#ifdef __KERNEL__
27165 +
27166 +#if 0
27167 +struct au_splhead {
27168 +       spinlock_t              spin;
27169 +       struct list_head        head;
27170 +};
27171 +
27172 +static inline void au_spl_init(struct au_splhead *spl)
27173 +{
27174 +       spin_lock_init(&spl->spin);
27175 +       INIT_LIST_HEAD(&spl->head);
27176 +}
27177 +
27178 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
27179 +{
27180 +       spin_lock(&spl->spin);
27181 +       list_add(list, &spl->head);
27182 +       spin_unlock(&spl->spin);
27183 +}
27184 +
27185 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
27186 +{
27187 +       spin_lock(&spl->spin);
27188 +       list_del(list);
27189 +       spin_unlock(&spl->spin);
27190 +}
27191 +
27192 +static inline void au_spl_del_rcu(struct list_head *list,
27193 +                                 struct au_splhead *spl)
27194 +{
27195 +       spin_lock(&spl->spin);
27196 +       list_del_rcu(list);
27197 +       spin_unlock(&spl->spin);
27198 +}
27199 +#endif
27200 +
27201 +/* ---------------------------------------------------------------------- */
27202 +
27203 +struct au_sphlhead {
27204 +       spinlock_t              spin;
27205 +       struct hlist_head       head;
27206 +};
27207 +
27208 +static inline void au_sphl_init(struct au_sphlhead *sphl)
27209 +{
27210 +       spin_lock_init(&sphl->spin);
27211 +       INIT_HLIST_HEAD(&sphl->head);
27212 +}
27213 +
27214 +static inline void au_sphl_add(struct hlist_node *hlist,
27215 +                              struct au_sphlhead *sphl)
27216 +{
27217 +       spin_lock(&sphl->spin);
27218 +       hlist_add_head(hlist, &sphl->head);
27219 +       spin_unlock(&sphl->spin);
27220 +}
27221 +
27222 +static inline void au_sphl_del(struct hlist_node *hlist,
27223 +                              struct au_sphlhead *sphl)
27224 +{
27225 +       spin_lock(&sphl->spin);
27226 +       hlist_del(hlist);
27227 +       spin_unlock(&sphl->spin);
27228 +}
27229 +
27230 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
27231 +                                  struct au_sphlhead *sphl)
27232 +{
27233 +       spin_lock(&sphl->spin);
27234 +       hlist_del_rcu(hlist);
27235 +       spin_unlock(&sphl->spin);
27236 +}
27237 +
27238 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
27239 +{
27240 +       unsigned long cnt;
27241 +       struct hlist_node *pos;
27242 +
27243 +       cnt = 0;
27244 +       spin_lock(&sphl->spin);
27245 +       hlist_for_each(pos, &sphl->head)
27246 +               cnt++;
27247 +       spin_unlock(&sphl->spin);
27248 +       return cnt;
27249 +}
27250 +
27251 +#endif /* __KERNEL__ */
27252 +#endif /* __AUFS_SPL_H__ */
27253 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
27254 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
27255 +++ linux/fs/aufs/super.c       2017-07-29 12:14:25.906375514 +0200
27256 @@ -0,0 +1,1044 @@
27257 +/*
27258 + * Copyright (C) 2005-2017 Junjiro R. Okajima
27259 + *
27260 + * This program, aufs is free software; you can redistribute it and/or modify
27261 + * it under the terms of the GNU General Public License as published by
27262 + * the Free Software Foundation; either version 2 of the License, or
27263 + * (at your option) any later version.
27264 + *
27265 + * This program is distributed in the hope that it will be useful,
27266 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27267 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27268 + * GNU General Public License for more details.
27269 + *
27270 + * You should have received a copy of the GNU General Public License
27271 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27272 + */
27273 +
27274 +/*
27275 + * mount and super_block operations
27276 + */
27277 +
27278 +#include <linux/mm.h>
27279 +#include <linux/seq_file.h>
27280 +#include <linux/statfs.h>
27281 +#include <linux/vmalloc.h>
27282 +#include "aufs.h"
27283 +
27284 +/*
27285 + * super_operations
27286 + */
27287 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
27288 +{
27289 +       struct au_icntnr *c;
27290 +
27291 +       c = au_cache_alloc_icntnr();
27292 +       if (c) {
27293 +               au_icntnr_init(c);
27294 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
27295 +               c->iinfo.ii_hinode = NULL;
27296 +               return &c->vfs_inode;
27297 +       }
27298 +       return NULL;
27299 +}
27300 +
27301 +static void aufs_destroy_inode_cb(struct rcu_head *head)
27302 +{
27303 +       struct inode *inode = container_of(head, struct inode, i_rcu);
27304 +
27305 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
27306 +}
27307 +
27308 +static void aufs_destroy_inode(struct inode *inode)
27309 +{
27310 +       if (!au_is_bad_inode(inode))
27311 +               au_iinfo_fin(inode);
27312 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
27313 +}
27314 +
27315 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
27316 +{
27317 +       struct inode *inode;
27318 +       int err;
27319 +
27320 +       inode = iget_locked(sb, ino);
27321 +       if (unlikely(!inode)) {
27322 +               inode = ERR_PTR(-ENOMEM);
27323 +               goto out;
27324 +       }
27325 +       if (!(inode->i_state & I_NEW))
27326 +               goto out;
27327 +
27328 +       err = au_xigen_new(inode);
27329 +       if (!err)
27330 +               err = au_iinfo_init(inode);
27331 +       if (!err)
27332 +               inode->i_version++;
27333 +       else {
27334 +               iget_failed(inode);
27335 +               inode = ERR_PTR(err);
27336 +       }
27337 +
27338 +out:
27339 +       /* never return NULL */
27340 +       AuDebugOn(!inode);
27341 +       AuTraceErrPtr(inode);
27342 +       return inode;
27343 +}
27344 +
27345 +/* lock free root dinfo */
27346 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
27347 +{
27348 +       int err;
27349 +       aufs_bindex_t bindex, bbot;
27350 +       struct path path;
27351 +       struct au_hdentry *hdp;
27352 +       struct au_branch *br;
27353 +       au_br_perm_str_t perm;
27354 +
27355 +       err = 0;
27356 +       bbot = au_sbbot(sb);
27357 +       bindex = 0;
27358 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
27359 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
27360 +               br = au_sbr(sb, bindex);
27361 +               path.mnt = au_br_mnt(br);
27362 +               path.dentry = hdp->hd_dentry;
27363 +               err = au_seq_path(seq, &path);
27364 +               if (!err) {
27365 +                       au_optstr_br_perm(&perm, br->br_perm);
27366 +                       seq_printf(seq, "=%s", perm.a);
27367 +                       if (bindex != bbot)
27368 +                               seq_putc(seq, ':');
27369 +               }
27370 +       }
27371 +       if (unlikely(err || seq_has_overflowed(seq)))
27372 +               err = -E2BIG;
27373 +
27374 +       return err;
27375 +}
27376 +
27377 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
27378 +                      const char *append)
27379 +{
27380 +       char *p;
27381 +
27382 +       p = fmt;
27383 +       while (*pat != ':')
27384 +               *p++ = *pat++;
27385 +       *p++ = *pat++;
27386 +       strcpy(p, append);
27387 +       AuDebugOn(strlen(fmt) >= len);
27388 +}
27389 +
27390 +static void au_show_wbr_create(struct seq_file *m, int v,
27391 +                              struct au_sbinfo *sbinfo)
27392 +{
27393 +       const char *pat;
27394 +       char fmt[32];
27395 +       struct au_wbr_mfs *mfs;
27396 +
27397 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27398 +
27399 +       seq_puts(m, ",create=");
27400 +       pat = au_optstr_wbr_create(v);
27401 +       mfs = &sbinfo->si_wbr_mfs;
27402 +       switch (v) {
27403 +       case AuWbrCreate_TDP:
27404 +       case AuWbrCreate_RR:
27405 +       case AuWbrCreate_MFS:
27406 +       case AuWbrCreate_PMFS:
27407 +               seq_puts(m, pat);
27408 +               break;
27409 +       case AuWbrCreate_MFSRR:
27410 +       case AuWbrCreate_TDMFS:
27411 +       case AuWbrCreate_PMFSRR:
27412 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
27413 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
27414 +               break;
27415 +       case AuWbrCreate_MFSV:
27416 +       case AuWbrCreate_PMFSV:
27417 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
27418 +               seq_printf(m, fmt,
27419 +                          jiffies_to_msecs(mfs->mfs_expire)
27420 +                          / MSEC_PER_SEC);
27421 +               break;
27422 +       case AuWbrCreate_MFSRRV:
27423 +       case AuWbrCreate_TDMFSV:
27424 +       case AuWbrCreate_PMFSRRV:
27425 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
27426 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
27427 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
27428 +               break;
27429 +       default:
27430 +               BUG();
27431 +       }
27432 +}
27433 +
27434 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
27435 +{
27436 +#ifdef CONFIG_SYSFS
27437 +       return 0;
27438 +#else
27439 +       int err;
27440 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
27441 +       aufs_bindex_t bindex, brid;
27442 +       struct qstr *name;
27443 +       struct file *f;
27444 +       struct dentry *d, *h_root;
27445 +
27446 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27447 +
27448 +       err = 0;
27449 +       f = au_sbi(sb)->si_xib;
27450 +       if (!f)
27451 +               goto out;
27452 +
27453 +       /* stop printing the default xino path on the first writable branch */
27454 +       h_root = NULL;
27455 +       brid = au_xino_brid(sb);
27456 +       if (brid >= 0) {
27457 +               bindex = au_br_index(sb, brid);
27458 +               h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
27459 +       }
27460 +       d = f->f_path.dentry;
27461 +       name = &d->d_name;
27462 +       /* safe ->d_parent because the file is unlinked */
27463 +       if (d->d_parent == h_root
27464 +           && name->len == len
27465 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
27466 +               goto out;
27467 +
27468 +       seq_puts(seq, ",xino=");
27469 +       err = au_xino_path(seq, f);
27470 +
27471 +out:
27472 +       return err;
27473 +#endif
27474 +}
27475 +
27476 +/* seq_file will re-call me in case of too long string */
27477 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
27478 +{
27479 +       int err;
27480 +       unsigned int mnt_flags, v;
27481 +       struct super_block *sb;
27482 +       struct au_sbinfo *sbinfo;
27483 +
27484 +#define AuBool(name, str) do { \
27485 +       v = au_opt_test(mnt_flags, name); \
27486 +       if (v != au_opt_test(AuOpt_Def, name)) \
27487 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
27488 +} while (0)
27489 +
27490 +#define AuStr(name, str) do { \
27491 +       v = mnt_flags & AuOptMask_##name; \
27492 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
27493 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
27494 +} while (0)
27495 +
27496 +#define AuUInt(name, str, val) do { \
27497 +       if (val != AUFS_##name##_DEF) \
27498 +               seq_printf(m, "," #str "=%u", val); \
27499 +} while (0)
27500 +
27501 +       sb = dentry->d_sb;
27502 +       if (sb->s_flags & MS_POSIXACL)
27503 +               seq_puts(m, ",acl");
27504 +
27505 +       /* lock free root dinfo */
27506 +       si_noflush_read_lock(sb);
27507 +       sbinfo = au_sbi(sb);
27508 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
27509 +
27510 +       mnt_flags = au_mntflags(sb);
27511 +       if (au_opt_test(mnt_flags, XINO)) {
27512 +               err = au_show_xino(m, sb);
27513 +               if (unlikely(err))
27514 +                       goto out;
27515 +       } else
27516 +               seq_puts(m, ",noxino");
27517 +
27518 +       AuBool(TRUNC_XINO, trunc_xino);
27519 +       AuStr(UDBA, udba);
27520 +       AuBool(SHWH, shwh);
27521 +       AuBool(PLINK, plink);
27522 +       AuBool(DIO, dio);
27523 +       AuBool(DIRPERM1, dirperm1);
27524 +
27525 +       v = sbinfo->si_wbr_create;
27526 +       if (v != AuWbrCreate_Def)
27527 +               au_show_wbr_create(m, v, sbinfo);
27528 +
27529 +       v = sbinfo->si_wbr_copyup;
27530 +       if (v != AuWbrCopyup_Def)
27531 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
27532 +
27533 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
27534 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
27535 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
27536 +
27537 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
27538 +
27539 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
27540 +       AuUInt(RDCACHE, rdcache, v);
27541 +
27542 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
27543 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
27544 +
27545 +       au_fhsm_show(m, sbinfo);
27546 +
27547 +       AuBool(SUM, sum);
27548 +       /* AuBool(SUM_W, wsum); */
27549 +       AuBool(WARN_PERM, warn_perm);
27550 +       AuBool(VERBOSE, verbose);
27551 +
27552 +out:
27553 +       /* be sure to print "br:" last */
27554 +       if (!sysaufs_brs) {
27555 +               seq_puts(m, ",br:");
27556 +               au_show_brs(m, sb);
27557 +       }
27558 +       si_read_unlock(sb);
27559 +       return 0;
27560 +
27561 +#undef AuBool
27562 +#undef AuStr
27563 +#undef AuUInt
27564 +}
27565 +
27566 +/* ---------------------------------------------------------------------- */
27567 +
27568 +/* sum mode which returns the summation for statfs(2) */
27569 +
27570 +static u64 au_add_till_max(u64 a, u64 b)
27571 +{
27572 +       u64 old;
27573 +
27574 +       old = a;
27575 +       a += b;
27576 +       if (old <= a)
27577 +               return a;
27578 +       return ULLONG_MAX;
27579 +}
27580 +
27581 +static u64 au_mul_till_max(u64 a, long mul)
27582 +{
27583 +       u64 old;
27584 +
27585 +       old = a;
27586 +       a *= mul;
27587 +       if (old <= a)
27588 +               return a;
27589 +       return ULLONG_MAX;
27590 +}
27591 +
27592 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27593 +{
27594 +       int err;
27595 +       long bsize, factor;
27596 +       u64 blocks, bfree, bavail, files, ffree;
27597 +       aufs_bindex_t bbot, bindex, i;
27598 +       unsigned char shared;
27599 +       struct path h_path;
27600 +       struct super_block *h_sb;
27601 +
27602 +       err = 0;
27603 +       bsize = LONG_MAX;
27604 +       files = 0;
27605 +       ffree = 0;
27606 +       blocks = 0;
27607 +       bfree = 0;
27608 +       bavail = 0;
27609 +       bbot = au_sbbot(sb);
27610 +       for (bindex = 0; bindex <= bbot; bindex++) {
27611 +               h_path.mnt = au_sbr_mnt(sb, bindex);
27612 +               h_sb = h_path.mnt->mnt_sb;
27613 +               shared = 0;
27614 +               for (i = 0; !shared && i < bindex; i++)
27615 +                       shared = (au_sbr_sb(sb, i) == h_sb);
27616 +               if (shared)
27617 +                       continue;
27618 +
27619 +               /* sb->s_root for NFS is unreliable */
27620 +               h_path.dentry = h_path.mnt->mnt_root;
27621 +               err = vfs_statfs(&h_path, buf);
27622 +               if (unlikely(err))
27623 +                       goto out;
27624 +
27625 +               if (bsize > buf->f_bsize) {
27626 +                       /*
27627 +                        * we will reduce bsize, so we have to expand blocks
27628 +                        * etc. to match them again
27629 +                        */
27630 +                       factor = (bsize / buf->f_bsize);
27631 +                       blocks = au_mul_till_max(blocks, factor);
27632 +                       bfree = au_mul_till_max(bfree, factor);
27633 +                       bavail = au_mul_till_max(bavail, factor);
27634 +                       bsize = buf->f_bsize;
27635 +               }
27636 +
27637 +               factor = (buf->f_bsize / bsize);
27638 +               blocks = au_add_till_max(blocks,
27639 +                               au_mul_till_max(buf->f_blocks, factor));
27640 +               bfree = au_add_till_max(bfree,
27641 +                               au_mul_till_max(buf->f_bfree, factor));
27642 +               bavail = au_add_till_max(bavail,
27643 +                               au_mul_till_max(buf->f_bavail, factor));
27644 +               files = au_add_till_max(files, buf->f_files);
27645 +               ffree = au_add_till_max(ffree, buf->f_ffree);
27646 +       }
27647 +
27648 +       buf->f_bsize = bsize;
27649 +       buf->f_blocks = blocks;
27650 +       buf->f_bfree = bfree;
27651 +       buf->f_bavail = bavail;
27652 +       buf->f_files = files;
27653 +       buf->f_ffree = ffree;
27654 +       buf->f_frsize = 0;
27655 +
27656 +out:
27657 +       return err;
27658 +}
27659 +
27660 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27661 +{
27662 +       int err;
27663 +       struct path h_path;
27664 +       struct super_block *sb;
27665 +
27666 +       /* lock free root dinfo */
27667 +       sb = dentry->d_sb;
27668 +       si_noflush_read_lock(sb);
27669 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
27670 +               /* sb->s_root for NFS is unreliable */
27671 +               h_path.mnt = au_sbr_mnt(sb, 0);
27672 +               h_path.dentry = h_path.mnt->mnt_root;
27673 +               err = vfs_statfs(&h_path, buf);
27674 +       } else
27675 +               err = au_statfs_sum(sb, buf);
27676 +       si_read_unlock(sb);
27677 +
27678 +       if (!err) {
27679 +               buf->f_type = AUFS_SUPER_MAGIC;
27680 +               buf->f_namelen = AUFS_MAX_NAMELEN;
27681 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27682 +       }
27683 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27684 +
27685 +       return err;
27686 +}
27687 +
27688 +/* ---------------------------------------------------------------------- */
27689 +
27690 +static int aufs_sync_fs(struct super_block *sb, int wait)
27691 +{
27692 +       int err, e;
27693 +       aufs_bindex_t bbot, bindex;
27694 +       struct au_branch *br;
27695 +       struct super_block *h_sb;
27696 +
27697 +       err = 0;
27698 +       si_noflush_read_lock(sb);
27699 +       bbot = au_sbbot(sb);
27700 +       for (bindex = 0; bindex <= bbot; bindex++) {
27701 +               br = au_sbr(sb, bindex);
27702 +               if (!au_br_writable(br->br_perm))
27703 +                       continue;
27704 +
27705 +               h_sb = au_sbr_sb(sb, bindex);
27706 +               e = vfsub_sync_filesystem(h_sb, wait);
27707 +               if (unlikely(e && !err))
27708 +                       err = e;
27709 +               /* go on even if an error happens */
27710 +       }
27711 +       si_read_unlock(sb);
27712 +
27713 +       return err;
27714 +}
27715 +
27716 +/* ---------------------------------------------------------------------- */
27717 +
27718 +/* final actions when unmounting a file system */
27719 +static void aufs_put_super(struct super_block *sb)
27720 +{
27721 +       struct au_sbinfo *sbinfo;
27722 +
27723 +       sbinfo = au_sbi(sb);
27724 +       if (!sbinfo)
27725 +               return;
27726 +
27727 +       dbgaufs_si_fin(sbinfo);
27728 +       kobject_put(&sbinfo->si_kobj);
27729 +}
27730 +
27731 +/* ---------------------------------------------------------------------- */
27732 +
27733 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27734 +                    struct super_block *sb, void *arg)
27735 +{
27736 +       void *array;
27737 +       unsigned long long n, sz;
27738 +
27739 +       array = NULL;
27740 +       n = 0;
27741 +       if (!*hint)
27742 +               goto out;
27743 +
27744 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27745 +               array = ERR_PTR(-EMFILE);
27746 +               pr_err("hint %llu\n", *hint);
27747 +               goto out;
27748 +       }
27749 +
27750 +       sz = sizeof(array) * *hint;
27751 +       array = kzalloc(sz, GFP_NOFS);
27752 +       if (unlikely(!array))
27753 +               array = vzalloc(sz);
27754 +       if (unlikely(!array)) {
27755 +               array = ERR_PTR(-ENOMEM);
27756 +               goto out;
27757 +       }
27758 +
27759 +       n = cb(sb, array, *hint, arg);
27760 +       AuDebugOn(n > *hint);
27761 +
27762 +out:
27763 +       *hint = n;
27764 +       return array;
27765 +}
27766 +
27767 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
27768 +                                      unsigned long long max __maybe_unused,
27769 +                                      void *arg)
27770 +{
27771 +       unsigned long long n;
27772 +       struct inode **p, *inode;
27773 +       struct list_head *head;
27774 +
27775 +       n = 0;
27776 +       p = a;
27777 +       head = arg;
27778 +       spin_lock(&sb->s_inode_list_lock);
27779 +       list_for_each_entry(inode, head, i_sb_list) {
27780 +               if (!au_is_bad_inode(inode)
27781 +                   && au_ii(inode)->ii_btop >= 0) {
27782 +                       spin_lock(&inode->i_lock);
27783 +                       if (atomic_read(&inode->i_count)) {
27784 +                               au_igrab(inode);
27785 +                               *p++ = inode;
27786 +                               n++;
27787 +                               AuDebugOn(n > max);
27788 +                       }
27789 +                       spin_unlock(&inode->i_lock);
27790 +               }
27791 +       }
27792 +       spin_unlock(&sb->s_inode_list_lock);
27793 +
27794 +       return n;
27795 +}
27796 +
27797 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27798 +{
27799 +       *max = au_ninodes(sb);
27800 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
27801 +}
27802 +
27803 +void au_iarray_free(struct inode **a, unsigned long long max)
27804 +{
27805 +       unsigned long long ull;
27806 +
27807 +       for (ull = 0; ull < max; ull++)
27808 +               iput(a[ull]);
27809 +       kvfree(a);
27810 +}
27811 +
27812 +/* ---------------------------------------------------------------------- */
27813 +
27814 +/*
27815 + * refresh dentry and inode at remount time.
27816 + */
27817 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27818 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27819 +                     struct dentry *parent)
27820 +{
27821 +       int err;
27822 +
27823 +       di_write_lock_child(dentry);
27824 +       di_read_lock_parent(parent, AuLock_IR);
27825 +       err = au_refresh_dentry(dentry, parent);
27826 +       if (!err && dir_flags)
27827 +               au_hn_reset(d_inode(dentry), dir_flags);
27828 +       di_read_unlock(parent, AuLock_IR);
27829 +       di_write_unlock(dentry);
27830 +
27831 +       return err;
27832 +}
27833 +
27834 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27835 +                          struct au_sbinfo *sbinfo,
27836 +                          const unsigned int dir_flags, unsigned int do_idop)
27837 +{
27838 +       int err;
27839 +       struct dentry *parent;
27840 +
27841 +       err = 0;
27842 +       parent = dget_parent(dentry);
27843 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27844 +               if (d_really_is_positive(dentry)) {
27845 +                       if (!d_is_dir(dentry))
27846 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27847 +                                                parent);
27848 +                       else {
27849 +                               err = au_do_refresh(dentry, dir_flags, parent);
27850 +                               if (unlikely(err))
27851 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27852 +                       }
27853 +               } else
27854 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27855 +               AuDbgDentry(dentry);
27856 +       }
27857 +       dput(parent);
27858 +
27859 +       if (!err) {
27860 +               if (do_idop)
27861 +                       au_refresh_dop(dentry, /*force_reval*/0);
27862 +       } else
27863 +               au_refresh_dop(dentry, /*force_reval*/1);
27864 +
27865 +       AuTraceErr(err);
27866 +       return err;
27867 +}
27868 +
27869 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
27870 +{
27871 +       int err, i, j, ndentry, e;
27872 +       unsigned int sigen;
27873 +       struct au_dcsub_pages dpages;
27874 +       struct au_dpage *dpage;
27875 +       struct dentry **dentries, *d;
27876 +       struct au_sbinfo *sbinfo;
27877 +       struct dentry *root = sb->s_root;
27878 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
27879 +
27880 +       if (do_idop)
27881 +               au_refresh_dop(root, /*force_reval*/0);
27882 +
27883 +       err = au_dpages_init(&dpages, GFP_NOFS);
27884 +       if (unlikely(err))
27885 +               goto out;
27886 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27887 +       if (unlikely(err))
27888 +               goto out_dpages;
27889 +
27890 +       sigen = au_sigen(sb);
27891 +       sbinfo = au_sbi(sb);
27892 +       for (i = 0; i < dpages.ndpage; i++) {
27893 +               dpage = dpages.dpages + i;
27894 +               dentries = dpage->dentries;
27895 +               ndentry = dpage->ndentry;
27896 +               for (j = 0; j < ndentry; j++) {
27897 +                       d = dentries[j];
27898 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
27899 +                                           do_idop);
27900 +                       if (unlikely(e && !err))
27901 +                               err = e;
27902 +                       /* go on even err */
27903 +               }
27904 +       }
27905 +
27906 +out_dpages:
27907 +       au_dpages_free(&dpages);
27908 +out:
27909 +       return err;
27910 +}
27911 +
27912 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
27913 +{
27914 +       int err, e;
27915 +       unsigned int sigen;
27916 +       unsigned long long max, ull;
27917 +       struct inode *inode, **array;
27918 +
27919 +       array = au_iarray_alloc(sb, &max);
27920 +       err = PTR_ERR(array);
27921 +       if (IS_ERR(array))
27922 +               goto out;
27923 +
27924 +       err = 0;
27925 +       sigen = au_sigen(sb);
27926 +       for (ull = 0; ull < max; ull++) {
27927 +               inode = array[ull];
27928 +               if (unlikely(!inode))
27929 +                       break;
27930 +
27931 +               e = 0;
27932 +               ii_write_lock_child(inode);
27933 +               if (au_iigen(inode, NULL) != sigen) {
27934 +                       e = au_refresh_hinode_self(inode);
27935 +                       if (unlikely(e)) {
27936 +                               au_refresh_iop(inode, /*force_getattr*/1);
27937 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27938 +                               if (!err)
27939 +                                       err = e;
27940 +                               /* go on even if err */
27941 +                       }
27942 +               }
27943 +               if (!e && do_idop)
27944 +                       au_refresh_iop(inode, /*force_getattr*/0);
27945 +               ii_write_unlock(inode);
27946 +       }
27947 +
27948 +       au_iarray_free(array, max);
27949 +
27950 +out:
27951 +       return err;
27952 +}
27953 +
27954 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
27955 +{
27956 +       int err, e;
27957 +       unsigned int udba;
27958 +       aufs_bindex_t bindex, bbot;
27959 +       struct dentry *root;
27960 +       struct inode *inode;
27961 +       struct au_branch *br;
27962 +       struct au_sbinfo *sbi;
27963 +
27964 +       au_sigen_inc(sb);
27965 +       sbi = au_sbi(sb);
27966 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
27967 +
27968 +       root = sb->s_root;
27969 +       DiMustNoWaiters(root);
27970 +       inode = d_inode(root);
27971 +       IiMustNoWaiters(inode);
27972 +
27973 +       udba = au_opt_udba(sb);
27974 +       bbot = au_sbbot(sb);
27975 +       for (bindex = 0; bindex <= bbot; bindex++) {
27976 +               br = au_sbr(sb, bindex);
27977 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27978 +               if (unlikely(err))
27979 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27980 +                               bindex, err);
27981 +               /* go on even if err */
27982 +       }
27983 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27984 +
27985 +       if (do_idop) {
27986 +               if (au_ftest_si(sbi, NO_DREVAL)) {
27987 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27988 +                       sb->s_d_op = &aufs_dop_noreval;
27989 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27990 +                       sbi->si_iop_array = aufs_iop_nogetattr;
27991 +               } else {
27992 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
27993 +                       sb->s_d_op = &aufs_dop;
27994 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
27995 +                       sbi->si_iop_array = aufs_iop;
27996 +               }
27997 +               pr_info("reset to %pf and %pf\n",
27998 +                       sb->s_d_op, sbi->si_iop_array);
27999 +       }
28000 +
28001 +       di_write_unlock(root);
28002 +       err = au_refresh_d(sb, do_idop);
28003 +       e = au_refresh_i(sb, do_idop);
28004 +       if (unlikely(e && !err))
28005 +               err = e;
28006 +       /* aufs_write_lock() calls ..._child() */
28007 +       di_write_lock_child(root);
28008 +
28009 +       au_cpup_attr_all(inode, /*force*/1);
28010 +
28011 +       if (unlikely(err))
28012 +               AuIOErr("refresh failed, ignored, %d\n", err);
28013 +}
28014 +
28015 +/* stop extra interpretation of errno in mount(8), and strange error messages */
28016 +static int cvt_err(int err)
28017 +{
28018 +       AuTraceErr(err);
28019 +
28020 +       switch (err) {
28021 +       case -ENOENT:
28022 +       case -ENOTDIR:
28023 +       case -EEXIST:
28024 +       case -EIO:
28025 +               err = -EINVAL;
28026 +       }
28027 +       return err;
28028 +}
28029 +
28030 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
28031 +{
28032 +       int err, do_dx;
28033 +       unsigned int mntflags;
28034 +       struct au_opts opts = {
28035 +               .opt = NULL
28036 +       };
28037 +       struct dentry *root;
28038 +       struct inode *inode;
28039 +       struct au_sbinfo *sbinfo;
28040 +
28041 +       err = 0;
28042 +       root = sb->s_root;
28043 +       if (!data || !*data) {
28044 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
28045 +               if (!err) {
28046 +                       di_write_lock_child(root);
28047 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
28048 +                       aufs_write_unlock(root);
28049 +               }
28050 +               goto out;
28051 +       }
28052 +
28053 +       err = -ENOMEM;
28054 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
28055 +       if (unlikely(!opts.opt))
28056 +               goto out;
28057 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
28058 +       opts.flags = AuOpts_REMOUNT;
28059 +       opts.sb_flags = *flags;
28060 +
28061 +       /* parse it before aufs lock */
28062 +       err = au_opts_parse(sb, data, &opts);
28063 +       if (unlikely(err))
28064 +               goto out_opts;
28065 +
28066 +       sbinfo = au_sbi(sb);
28067 +       inode = d_inode(root);
28068 +       inode_lock(inode);
28069 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
28070 +       if (unlikely(err))
28071 +               goto out_mtx;
28072 +       di_write_lock_child(root);
28073 +
28074 +       /* au_opts_remount() may return an error */
28075 +       err = au_opts_remount(sb, &opts);
28076 +       au_opts_free(&opts);
28077 +
28078 +       if (au_ftest_opts(opts.flags, REFRESH))
28079 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
28080 +
28081 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
28082 +               mntflags = au_mntflags(sb);
28083 +               do_dx = !!au_opt_test(mntflags, DIO);
28084 +               au_dy_arefresh(do_dx);
28085 +       }
28086 +
28087 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
28088 +       aufs_write_unlock(root);
28089 +
28090 +out_mtx:
28091 +       inode_unlock(inode);
28092 +out_opts:
28093 +       free_page((unsigned long)opts.opt);
28094 +out:
28095 +       err = cvt_err(err);
28096 +       AuTraceErr(err);
28097 +       return err;
28098 +}
28099 +
28100 +static const struct super_operations aufs_sop = {
28101 +       .alloc_inode    = aufs_alloc_inode,
28102 +       .destroy_inode  = aufs_destroy_inode,
28103 +       /* always deleting, no clearing */
28104 +       .drop_inode     = generic_delete_inode,
28105 +       .show_options   = aufs_show_options,
28106 +       .statfs         = aufs_statfs,
28107 +       .put_super      = aufs_put_super,
28108 +       .sync_fs        = aufs_sync_fs,
28109 +       .remount_fs     = aufs_remount_fs
28110 +};
28111 +
28112 +/* ---------------------------------------------------------------------- */
28113 +
28114 +static int alloc_root(struct super_block *sb)
28115 +{
28116 +       int err;
28117 +       struct inode *inode;
28118 +       struct dentry *root;
28119 +
28120 +       err = -ENOMEM;
28121 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
28122 +       err = PTR_ERR(inode);
28123 +       if (IS_ERR(inode))
28124 +               goto out;
28125 +
28126 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
28127 +       inode->i_fop = &aufs_dir_fop;
28128 +       inode->i_mode = S_IFDIR;
28129 +       set_nlink(inode, 2);
28130 +       unlock_new_inode(inode);
28131 +
28132 +       root = d_make_root(inode);
28133 +       if (unlikely(!root))
28134 +               goto out;
28135 +       err = PTR_ERR(root);
28136 +       if (IS_ERR(root))
28137 +               goto out;
28138 +
28139 +       err = au_di_init(root);
28140 +       if (!err) {
28141 +               sb->s_root = root;
28142 +               return 0; /* success */
28143 +       }
28144 +       dput(root);
28145 +
28146 +out:
28147 +       return err;
28148 +}
28149 +
28150 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
28151 +                          int silent __maybe_unused)
28152 +{
28153 +       int err;
28154 +       struct au_opts opts = {
28155 +               .opt = NULL
28156 +       };
28157 +       struct au_sbinfo *sbinfo;
28158 +       struct dentry *root;
28159 +       struct inode *inode;
28160 +       char *arg = raw_data;
28161 +
28162 +       if (unlikely(!arg || !*arg)) {
28163 +               err = -EINVAL;
28164 +               pr_err("no arg\n");
28165 +               goto out;
28166 +       }
28167 +
28168 +       err = -ENOMEM;
28169 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
28170 +       if (unlikely(!opts.opt))
28171 +               goto out;
28172 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
28173 +       opts.sb_flags = sb->s_flags;
28174 +
28175 +       err = au_si_alloc(sb);
28176 +       if (unlikely(err))
28177 +               goto out_opts;
28178 +       sbinfo = au_sbi(sb);
28179 +
28180 +       /* all timestamps always follow the ones on the branch */
28181 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
28182 +       sb->s_op = &aufs_sop;
28183 +       sb->s_d_op = &aufs_dop;
28184 +       sb->s_magic = AUFS_SUPER_MAGIC;
28185 +       sb->s_maxbytes = 0;
28186 +       sb->s_stack_depth = 1;
28187 +       au_export_init(sb);
28188 +       au_xattr_init(sb);
28189 +
28190 +       err = alloc_root(sb);
28191 +       if (unlikely(err)) {
28192 +               si_write_unlock(sb);
28193 +               goto out_info;
28194 +       }
28195 +       root = sb->s_root;
28196 +       inode = d_inode(root);
28197 +
28198 +       /*
28199 +        * actually we can parse options regardless aufs lock here.
28200 +        * but at remount time, parsing must be done before aufs lock.
28201 +        * so we follow the same rule.
28202 +        */
28203 +       ii_write_lock_parent(inode);
28204 +       aufs_write_unlock(root);
28205 +       err = au_opts_parse(sb, arg, &opts);
28206 +       if (unlikely(err))
28207 +               goto out_root;
28208 +
28209 +       /* lock vfs_inode first, then aufs. */
28210 +       inode_lock(inode);
28211 +       aufs_write_lock(root);
28212 +       err = au_opts_mount(sb, &opts);
28213 +       au_opts_free(&opts);
28214 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
28215 +               sb->s_d_op = &aufs_dop_noreval;
28216 +               pr_info("%pf\n", sb->s_d_op);
28217 +               au_refresh_dop(root, /*force_reval*/0);
28218 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
28219 +               au_refresh_iop(inode, /*force_getattr*/0);
28220 +       }
28221 +       aufs_write_unlock(root);
28222 +       inode_unlock(inode);
28223 +       if (!err)
28224 +               goto out_opts; /* success */
28225 +
28226 +out_root:
28227 +       dput(root);
28228 +       sb->s_root = NULL;
28229 +out_info:
28230 +       dbgaufs_si_fin(sbinfo);
28231 +       kobject_put(&sbinfo->si_kobj);
28232 +       sb->s_fs_info = NULL;
28233 +out_opts:
28234 +       free_page((unsigned long)opts.opt);
28235 +out:
28236 +       AuTraceErr(err);
28237 +       err = cvt_err(err);
28238 +       AuTraceErr(err);
28239 +       return err;
28240 +}
28241 +
28242 +/* ---------------------------------------------------------------------- */
28243 +
28244 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
28245 +                                const char *dev_name __maybe_unused,
28246 +                                void *raw_data)
28247 +{
28248 +       struct dentry *root;
28249 +       struct super_block *sb;
28250 +
28251 +       /* all timestamps always follow the ones on the branch */
28252 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
28253 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
28254 +       if (IS_ERR(root))
28255 +               goto out;
28256 +
28257 +       sb = root->d_sb;
28258 +       si_write_lock(sb, !AuLock_FLUSH);
28259 +       sysaufs_brs_add(sb, 0);
28260 +       si_write_unlock(sb);
28261 +       au_sbilist_add(sb);
28262 +
28263 +out:
28264 +       return root;
28265 +}
28266 +
28267 +static void aufs_kill_sb(struct super_block *sb)
28268 +{
28269 +       struct au_sbinfo *sbinfo;
28270 +
28271 +       sbinfo = au_sbi(sb);
28272 +       if (sbinfo) {
28273 +               au_sbilist_del(sb);
28274 +               aufs_write_lock(sb->s_root);
28275 +               au_fhsm_fin(sb);
28276 +               if (sbinfo->si_wbr_create_ops->fin)
28277 +                       sbinfo->si_wbr_create_ops->fin(sb);
28278 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
28279 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
28280 +                       au_remount_refresh(sb, /*do_idop*/0);
28281 +               }
28282 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
28283 +                       au_plink_put(sb, /*verbose*/1);
28284 +               au_xino_clr(sb);
28285 +               sbinfo->si_sb = NULL;
28286 +               aufs_write_unlock(sb->s_root);
28287 +               au_nwt_flush(&sbinfo->si_nowait);
28288 +       }
28289 +       kill_anon_super(sb);
28290 +}
28291 +
28292 +struct file_system_type aufs_fs_type = {
28293 +       .name           = AUFS_FSTYPE,
28294 +       /* a race between rename and others */
28295 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
28296 +       .mount          = aufs_mount,
28297 +       .kill_sb        = aufs_kill_sb,
28298 +       /* no need to __module_get() and module_put(). */
28299 +       .owner          = THIS_MODULE,
28300 +};
28301 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
28302 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
28303 +++ linux/fs/aufs/super.h       2017-07-29 12:14:25.906375514 +0200
28304 @@ -0,0 +1,617 @@
28305 +/*
28306 + * Copyright (C) 2005-2017 Junjiro R. Okajima
28307 + *
28308 + * This program, aufs is free software; you can redistribute it and/or modify
28309 + * it under the terms of the GNU General Public License as published by
28310 + * the Free Software Foundation; either version 2 of the License, or
28311 + * (at your option) any later version.
28312 + *
28313 + * This program is distributed in the hope that it will be useful,
28314 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28315 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28316 + * GNU General Public License for more details.
28317 + *
28318 + * You should have received a copy of the GNU General Public License
28319 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28320 + */
28321 +
28322 +/*
28323 + * super_block operations
28324 + */
28325 +
28326 +#ifndef __AUFS_SUPER_H__
28327 +#define __AUFS_SUPER_H__
28328 +
28329 +#ifdef __KERNEL__
28330 +
28331 +#include <linux/fs.h>
28332 +#include <linux/kobject.h>
28333 +#include "rwsem.h"
28334 +#include "spl.h"
28335 +#include "wkq.h"
28336 +
28337 +/* policies to select one among multiple writable branches */
28338 +struct au_wbr_copyup_operations {
28339 +       int (*copyup)(struct dentry *dentry);
28340 +};
28341 +
28342 +#define AuWbr_DIR      1               /* target is a dir */
28343 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
28344 +
28345 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
28346 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
28347 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
28348 +
28349 +struct au_wbr_create_operations {
28350 +       int (*create)(struct dentry *dentry, unsigned int flags);
28351 +       int (*init)(struct super_block *sb);
28352 +       int (*fin)(struct super_block *sb);
28353 +};
28354 +
28355 +struct au_wbr_mfs {
28356 +       struct mutex    mfs_lock; /* protect this structure */
28357 +       unsigned long   mfs_jiffy;
28358 +       unsigned long   mfs_expire;
28359 +       aufs_bindex_t   mfs_bindex;
28360 +
28361 +       unsigned long long      mfsrr_bytes;
28362 +       unsigned long long      mfsrr_watermark;
28363 +};
28364 +
28365 +#define AuPlink_NHASH 100
28366 +static inline int au_plink_hash(ino_t ino)
28367 +{
28368 +       return ino % AuPlink_NHASH;
28369 +}
28370 +
28371 +/* File-based Hierarchical Storage Management */
28372 +struct au_fhsm {
28373 +#ifdef CONFIG_AUFS_FHSM
28374 +       /* allow only one process who can receive the notification */
28375 +       spinlock_t              fhsm_spin;
28376 +       pid_t                   fhsm_pid;
28377 +       wait_queue_head_t       fhsm_wqh;
28378 +       atomic_t                fhsm_readable;
28379 +
28380 +       /* these are protected by si_rwsem */
28381 +       unsigned long           fhsm_expire;
28382 +       aufs_bindex_t           fhsm_bottom;
28383 +#endif
28384 +};
28385 +
28386 +struct au_branch;
28387 +struct au_sbinfo {
28388 +       /* nowait tasks in the system-wide workqueue */
28389 +       struct au_nowait_tasks  si_nowait;
28390 +
28391 +       /*
28392 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
28393 +        * rwsem for au_sbinfo is necessary.
28394 +        */
28395 +       struct au_rwsem         si_rwsem;
28396 +
28397 +       /*
28398 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
28399 +        * remount.
28400 +        */
28401 +       struct percpu_counter   si_ninodes, si_nfiles;
28402 +
28403 +       /* branch management */
28404 +       unsigned int            si_generation;
28405 +
28406 +       /* see AuSi_ flags */
28407 +       unsigned char           au_si_status;
28408 +
28409 +       aufs_bindex_t           si_bbot;
28410 +
28411 +       /* dirty trick to keep br_id plus */
28412 +       unsigned int            si_last_br_id :
28413 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
28414 +       struct au_branch        **si_branch;
28415 +
28416 +       /* policy to select a writable branch */
28417 +       unsigned char           si_wbr_copyup;
28418 +       unsigned char           si_wbr_create;
28419 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
28420 +       struct au_wbr_create_operations *si_wbr_create_ops;
28421 +
28422 +       /* round robin */
28423 +       atomic_t                si_wbr_rr_next;
28424 +
28425 +       /* most free space */
28426 +       struct au_wbr_mfs       si_wbr_mfs;
28427 +
28428 +       /* File-based Hierarchical Storage Management */
28429 +       struct au_fhsm          si_fhsm;
28430 +
28431 +       /* mount flags */
28432 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
28433 +       unsigned int            si_mntflags;
28434 +
28435 +       /* external inode number (bitmap and translation table) */
28436 +       vfs_readf_t             si_xread;
28437 +       vfs_writef_t            si_xwrite;
28438 +       struct file             *si_xib;
28439 +       struct mutex            si_xib_mtx; /* protect xib members */
28440 +       unsigned long           *si_xib_buf;
28441 +       unsigned long           si_xib_last_pindex;
28442 +       int                     si_xib_next_bit;
28443 +       aufs_bindex_t           si_xino_brid;
28444 +       unsigned long           si_xino_jiffy;
28445 +       unsigned long           si_xino_expire;
28446 +       /* reserved for future use */
28447 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
28448 +
28449 +#ifdef CONFIG_AUFS_EXPORT
28450 +       /* i_generation */
28451 +       struct file             *si_xigen;
28452 +       atomic_t                si_xigen_next;
28453 +#endif
28454 +
28455 +       /* dirty trick to suppoer atomic_open */
28456 +       struct au_sphlhead      si_aopen;
28457 +
28458 +       /* vdir parameters */
28459 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
28460 +       unsigned int            si_rdblk;       /* deblk size */
28461 +       unsigned int            si_rdhash;      /* hash size */
28462 +
28463 +       /*
28464 +        * If the number of whiteouts are larger than si_dirwh, leave all of
28465 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
28466 +        * future fsck.aufs or kernel thread will remove them later.
28467 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
28468 +        */
28469 +       unsigned int            si_dirwh;
28470 +
28471 +       /* pseudo_link list */
28472 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
28473 +       wait_queue_head_t       si_plink_wq;
28474 +       spinlock_t              si_plink_maint_lock;
28475 +       pid_t                   si_plink_maint_pid;
28476 +
28477 +       /* file list */
28478 +       struct au_sphlhead      si_files;
28479 +
28480 +       /* with/without getattr, brother of sb->s_d_op */
28481 +       struct inode_operations *si_iop_array;
28482 +
28483 +       /*
28484 +        * sysfs and lifetime management.
28485 +        * this is not a small structure and it may be a waste of memory in case
28486 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
28487 +        * but using sysfs is majority.
28488 +        */
28489 +       struct kobject          si_kobj;
28490 +#ifdef CONFIG_DEBUG_FS
28491 +       struct dentry            *si_dbgaufs;
28492 +       struct dentry            *si_dbgaufs_plink;
28493 +       struct dentry            *si_dbgaufs_xib;
28494 +#ifdef CONFIG_AUFS_EXPORT
28495 +       struct dentry            *si_dbgaufs_xigen;
28496 +#endif
28497 +#endif
28498 +
28499 +#ifdef CONFIG_AUFS_SBILIST
28500 +       struct hlist_node       si_list;
28501 +#endif
28502 +
28503 +       /* dirty, necessary for unmounting, sysfs and sysrq */
28504 +       struct super_block      *si_sb;
28505 +};
28506 +
28507 +/* sbinfo status flags */
28508 +/*
28509 + * set true when refresh_dirs() failed at remount time.
28510 + * then try refreshing dirs at access time again.
28511 + * if it is false, refreshing dirs at access time is unnecesary
28512 + */
28513 +#define AuSi_FAILED_REFRESH_DIR        1
28514 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
28515 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
28516 +
28517 +#ifndef CONFIG_AUFS_FHSM
28518 +#undef AuSi_FHSM
28519 +#define AuSi_FHSM              0
28520 +#endif
28521 +
28522 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
28523 +                                          unsigned int flag)
28524 +{
28525 +       AuRwMustAnyLock(&sbi->si_rwsem);
28526 +       return sbi->au_si_status & flag;
28527 +}
28528 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
28529 +#define au_fset_si(sbinfo, name) do { \
28530 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28531 +       (sbinfo)->au_si_status |= AuSi_##name; \
28532 +} while (0)
28533 +#define au_fclr_si(sbinfo, name) do { \
28534 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28535 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
28536 +} while (0)
28537 +
28538 +/* ---------------------------------------------------------------------- */
28539 +
28540 +/* policy to select one among writable branches */
28541 +#define AuWbrCopyup(sbinfo, ...) \
28542 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
28543 +#define AuWbrCreate(sbinfo, ...) \
28544 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
28545 +
28546 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
28547 +#define AuLock_DW              1               /* write-lock dentry */
28548 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
28549 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
28550 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
28551 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
28552 +                                               /* except RENAME_EXCHANGE */
28553 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
28554 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
28555 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
28556 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
28557 +#define au_fset_lock(flags, name) \
28558 +       do { (flags) |= AuLock_##name; } while (0)
28559 +#define au_fclr_lock(flags, name) \
28560 +       do { (flags) &= ~AuLock_##name; } while (0)
28561 +
28562 +/* ---------------------------------------------------------------------- */
28563 +
28564 +/* super.c */
28565 +extern struct file_system_type aufs_fs_type;
28566 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
28567 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28568 +                                          unsigned long long max, void *arg);
28569 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28570 +                    struct super_block *sb, void *arg);
28571 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28572 +void au_iarray_free(struct inode **a, unsigned long long max);
28573 +
28574 +/* sbinfo.c */
28575 +void au_si_free(struct kobject *kobj);
28576 +int au_si_alloc(struct super_block *sb);
28577 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
28578 +
28579 +unsigned int au_sigen_inc(struct super_block *sb);
28580 +aufs_bindex_t au_new_br_id(struct super_block *sb);
28581 +
28582 +int si_read_lock(struct super_block *sb, int flags);
28583 +int si_write_lock(struct super_block *sb, int flags);
28584 +int aufs_read_lock(struct dentry *dentry, int flags);
28585 +void aufs_read_unlock(struct dentry *dentry, int flags);
28586 +void aufs_write_lock(struct dentry *dentry);
28587 +void aufs_write_unlock(struct dentry *dentry);
28588 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
28589 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28590 +
28591 +/* wbr_policy.c */
28592 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28593 +extern struct au_wbr_create_operations au_wbr_create_ops[];
28594 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
28595 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
28596 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
28597 +
28598 +/* mvdown.c */
28599 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
28600 +
28601 +#ifdef CONFIG_AUFS_FHSM
28602 +/* fhsm.c */
28603 +
28604 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28605 +{
28606 +       pid_t pid;
28607 +
28608 +       spin_lock(&fhsm->fhsm_spin);
28609 +       pid = fhsm->fhsm_pid;
28610 +       spin_unlock(&fhsm->fhsm_spin);
28611 +
28612 +       return pid;
28613 +}
28614 +
28615 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28616 +void au_fhsm_wrote_all(struct super_block *sb, int force);
28617 +int au_fhsm_fd(struct super_block *sb, int oflags);
28618 +int au_fhsm_br_alloc(struct au_branch *br);
28619 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
28620 +void au_fhsm_fin(struct super_block *sb);
28621 +void au_fhsm_init(struct au_sbinfo *sbinfo);
28622 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28623 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28624 +#else
28625 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28626 +          int force)
28627 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28628 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
28629 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28630 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28631 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
28632 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
28633 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28634 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28635 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28636 +#endif
28637 +
28638 +/* ---------------------------------------------------------------------- */
28639 +
28640 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28641 +{
28642 +       return sb->s_fs_info;
28643 +}
28644 +
28645 +/* ---------------------------------------------------------------------- */
28646 +
28647 +#ifdef CONFIG_AUFS_EXPORT
28648 +int au_test_nfsd(void);
28649 +void au_export_init(struct super_block *sb);
28650 +void au_xigen_inc(struct inode *inode);
28651 +int au_xigen_new(struct inode *inode);
28652 +int au_xigen_set(struct super_block *sb, struct file *base);
28653 +void au_xigen_clr(struct super_block *sb);
28654 +
28655 +static inline int au_busy_or_stale(void)
28656 +{
28657 +       if (!au_test_nfsd())
28658 +               return -EBUSY;
28659 +       return -ESTALE;
28660 +}
28661 +#else
28662 +AuStubInt0(au_test_nfsd, void)
28663 +AuStubVoid(au_export_init, struct super_block *sb)
28664 +AuStubVoid(au_xigen_inc, struct inode *inode)
28665 +AuStubInt0(au_xigen_new, struct inode *inode)
28666 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28667 +AuStubVoid(au_xigen_clr, struct super_block *sb)
28668 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
28669 +#endif /* CONFIG_AUFS_EXPORT */
28670 +
28671 +/* ---------------------------------------------------------------------- */
28672 +
28673 +#ifdef CONFIG_AUFS_SBILIST
28674 +/* module.c */
28675 +extern struct au_sphlhead au_sbilist;
28676 +
28677 +static inline void au_sbilist_init(void)
28678 +{
28679 +       au_sphl_init(&au_sbilist);
28680 +}
28681 +
28682 +static inline void au_sbilist_add(struct super_block *sb)
28683 +{
28684 +       au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
28685 +}
28686 +
28687 +static inline void au_sbilist_del(struct super_block *sb)
28688 +{
28689 +       au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
28690 +}
28691 +
28692 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28693 +static inline void au_sbilist_lock(void)
28694 +{
28695 +       spin_lock(&au_sbilist.spin);
28696 +}
28697 +
28698 +static inline void au_sbilist_unlock(void)
28699 +{
28700 +       spin_unlock(&au_sbilist.spin);
28701 +}
28702 +#define AuGFP_SBILIST  GFP_ATOMIC
28703 +#else
28704 +AuStubVoid(au_sbilist_lock, void)
28705 +AuStubVoid(au_sbilist_unlock, void)
28706 +#define AuGFP_SBILIST  GFP_NOFS
28707 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
28708 +#else
28709 +AuStubVoid(au_sbilist_init, void)
28710 +AuStubVoid(au_sbilist_add, struct super_block *sb)
28711 +AuStubVoid(au_sbilist_del, struct super_block *sb)
28712 +AuStubVoid(au_sbilist_lock, void)
28713 +AuStubVoid(au_sbilist_unlock, void)
28714 +#define AuGFP_SBILIST  GFP_NOFS
28715 +#endif
28716 +
28717 +/* ---------------------------------------------------------------------- */
28718 +
28719 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28720 +{
28721 +       /*
28722 +        * This function is a dynamic '__init' function actually,
28723 +        * so the tiny check for si_rwsem is unnecessary.
28724 +        */
28725 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28726 +#ifdef CONFIG_DEBUG_FS
28727 +       sbinfo->si_dbgaufs = NULL;
28728 +       sbinfo->si_dbgaufs_plink = NULL;
28729 +       sbinfo->si_dbgaufs_xib = NULL;
28730 +#ifdef CONFIG_AUFS_EXPORT
28731 +       sbinfo->si_dbgaufs_xigen = NULL;
28732 +#endif
28733 +#endif
28734 +}
28735 +
28736 +/* ---------------------------------------------------------------------- */
28737 +
28738 +/* current->atomic_flags */
28739 +/* this value should never corrupt the ones defined in linux/sched.h */
28740 +#define PFA_AUFS       7
28741 +
28742 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
28743 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
28744 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
28745 +
28746 +static inline int si_pid_test(struct super_block *sb)
28747 +{
28748 +       return !!task_test_aufs(current);
28749 +}
28750 +
28751 +static inline void si_pid_clr(struct super_block *sb)
28752 +{
28753 +       AuDebugOn(!task_test_aufs(current));
28754 +       task_clear_aufs(current);
28755 +}
28756 +
28757 +static inline void si_pid_set(struct super_block *sb)
28758 +{
28759 +       AuDebugOn(task_test_aufs(current));
28760 +       task_set_aufs(current);
28761 +}
28762 +
28763 +/* ---------------------------------------------------------------------- */
28764 +
28765 +/* lock superblock. mainly for entry point functions */
28766 +/*
28767 + * __si_read_lock, __si_write_lock,
28768 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28769 + */
28770 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28771 +
28772 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28773 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28774 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28775 +
28776 +static inline void si_noflush_read_lock(struct super_block *sb)
28777 +{
28778 +       __si_read_lock(sb);
28779 +       si_pid_set(sb);
28780 +}
28781 +
28782 +static inline int si_noflush_read_trylock(struct super_block *sb)
28783 +{
28784 +       int locked;
28785 +
28786 +       locked = __si_read_trylock(sb);
28787 +       if (locked)
28788 +               si_pid_set(sb);
28789 +       return locked;
28790 +}
28791 +
28792 +static inline void si_noflush_write_lock(struct super_block *sb)
28793 +{
28794 +       __si_write_lock(sb);
28795 +       si_pid_set(sb);
28796 +}
28797 +
28798 +static inline int si_noflush_write_trylock(struct super_block *sb)
28799 +{
28800 +       int locked;
28801 +
28802 +       locked = __si_write_trylock(sb);
28803 +       if (locked)
28804 +               si_pid_set(sb);
28805 +       return locked;
28806 +}
28807 +
28808 +#if 0 /* reserved */
28809 +static inline int si_read_trylock(struct super_block *sb, int flags)
28810 +{
28811 +       if (au_ftest_lock(flags, FLUSH))
28812 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28813 +       return si_noflush_read_trylock(sb);
28814 +}
28815 +#endif
28816 +
28817 +static inline void si_read_unlock(struct super_block *sb)
28818 +{
28819 +       si_pid_clr(sb);
28820 +       __si_read_unlock(sb);
28821 +}
28822 +
28823 +#if 0 /* reserved */
28824 +static inline int si_write_trylock(struct super_block *sb, int flags)
28825 +{
28826 +       if (au_ftest_lock(flags, FLUSH))
28827 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28828 +       return si_noflush_write_trylock(sb);
28829 +}
28830 +#endif
28831 +
28832 +static inline void si_write_unlock(struct super_block *sb)
28833 +{
28834 +       si_pid_clr(sb);
28835 +       __si_write_unlock(sb);
28836 +}
28837 +
28838 +#if 0 /* reserved */
28839 +static inline void si_downgrade_lock(struct super_block *sb)
28840 +{
28841 +       __si_downgrade_lock(sb);
28842 +}
28843 +#endif
28844 +
28845 +/* ---------------------------------------------------------------------- */
28846 +
28847 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
28848 +{
28849 +       SiMustAnyLock(sb);
28850 +       return au_sbi(sb)->si_bbot;
28851 +}
28852 +
28853 +static inline unsigned int au_mntflags(struct super_block *sb)
28854 +{
28855 +       SiMustAnyLock(sb);
28856 +       return au_sbi(sb)->si_mntflags;
28857 +}
28858 +
28859 +static inline unsigned int au_sigen(struct super_block *sb)
28860 +{
28861 +       SiMustAnyLock(sb);
28862 +       return au_sbi(sb)->si_generation;
28863 +}
28864 +
28865 +static inline unsigned long long au_ninodes(struct super_block *sb)
28866 +{
28867 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
28868 +
28869 +       BUG_ON(n < 0);
28870 +       return n;
28871 +}
28872 +
28873 +static inline void au_ninodes_inc(struct super_block *sb)
28874 +{
28875 +       percpu_counter_inc(&au_sbi(sb)->si_ninodes);
28876 +}
28877 +
28878 +static inline void au_ninodes_dec(struct super_block *sb)
28879 +{
28880 +       percpu_counter_dec(&au_sbi(sb)->si_ninodes);
28881 +}
28882 +
28883 +static inline unsigned long long au_nfiles(struct super_block *sb)
28884 +{
28885 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
28886 +
28887 +       BUG_ON(n < 0);
28888 +       return n;
28889 +}
28890 +
28891 +static inline void au_nfiles_inc(struct super_block *sb)
28892 +{
28893 +       percpu_counter_inc(&au_sbi(sb)->si_nfiles);
28894 +}
28895 +
28896 +static inline void au_nfiles_dec(struct super_block *sb)
28897 +{
28898 +       percpu_counter_dec(&au_sbi(sb)->si_nfiles);
28899 +}
28900 +
28901 +static inline struct au_branch *au_sbr(struct super_block *sb,
28902 +                                      aufs_bindex_t bindex)
28903 +{
28904 +       SiMustAnyLock(sb);
28905 +       return au_sbi(sb)->si_branch[0 + bindex];
28906 +}
28907 +
28908 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28909 +{
28910 +       SiMustWriteLock(sb);
28911 +       au_sbi(sb)->si_xino_brid = brid;
28912 +}
28913 +
28914 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28915 +{
28916 +       SiMustAnyLock(sb);
28917 +       return au_sbi(sb)->si_xino_brid;
28918 +}
28919 +
28920 +#endif /* __KERNEL__ */
28921 +#endif /* __AUFS_SUPER_H__ */
28922 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28923 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28924 +++ linux/fs/aufs/sysaufs.c     2017-07-29 12:14:25.906375514 +0200
28925 @@ -0,0 +1,104 @@
28926 +/*
28927 + * Copyright (C) 2005-2017 Junjiro R. Okajima
28928 + *
28929 + * This program, aufs is free software; you can redistribute it and/or modify
28930 + * it under the terms of the GNU General Public License as published by
28931 + * the Free Software Foundation; either version 2 of the License, or
28932 + * (at your option) any later version.
28933 + *
28934 + * This program is distributed in the hope that it will be useful,
28935 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28936 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28937 + * GNU General Public License for more details.
28938 + *
28939 + * You should have received a copy of the GNU General Public License
28940 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28941 + */
28942 +
28943 +/*
28944 + * sysfs interface and lifetime management
28945 + * they are necessary regardless sysfs is disabled.
28946 + */
28947 +
28948 +#include <linux/random.h>
28949 +#include "aufs.h"
28950 +
28951 +unsigned long sysaufs_si_mask;
28952 +struct kset *sysaufs_kset;
28953 +
28954 +#define AuSiAttr(_name) { \
28955 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28956 +       .show   = sysaufs_si_##_name,                           \
28957 +}
28958 +
28959 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28960 +struct attribute *sysaufs_si_attrs[] = {
28961 +       &sysaufs_si_attr_xi_path.attr,
28962 +       NULL,
28963 +};
28964 +
28965 +static const struct sysfs_ops au_sbi_ops = {
28966 +       .show   = sysaufs_si_show
28967 +};
28968 +
28969 +static struct kobj_type au_sbi_ktype = {
28970 +       .release        = au_si_free,
28971 +       .sysfs_ops      = &au_sbi_ops,
28972 +       .default_attrs  = sysaufs_si_attrs
28973 +};
28974 +
28975 +/* ---------------------------------------------------------------------- */
28976 +
28977 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28978 +{
28979 +       int err;
28980 +
28981 +       sbinfo->si_kobj.kset = sysaufs_kset;
28982 +       /* cf. sysaufs_name() */
28983 +       err = kobject_init_and_add
28984 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28985 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28986 +
28987 +       dbgaufs_si_null(sbinfo);
28988 +       if (!err) {
28989 +               err = dbgaufs_si_init(sbinfo);
28990 +               if (unlikely(err))
28991 +                       kobject_put(&sbinfo->si_kobj);
28992 +       }
28993 +       return err;
28994 +}
28995 +
28996 +void sysaufs_fin(void)
28997 +{
28998 +       dbgaufs_fin();
28999 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
29000 +       kset_unregister(sysaufs_kset);
29001 +}
29002 +
29003 +int __init sysaufs_init(void)
29004 +{
29005 +       int err;
29006 +
29007 +       do {
29008 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
29009 +       } while (!sysaufs_si_mask);
29010 +
29011 +       err = -EINVAL;
29012 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
29013 +       if (unlikely(!sysaufs_kset))
29014 +               goto out;
29015 +       err = PTR_ERR(sysaufs_kset);
29016 +       if (IS_ERR(sysaufs_kset))
29017 +               goto out;
29018 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
29019 +       if (unlikely(err)) {
29020 +               kset_unregister(sysaufs_kset);
29021 +               goto out;
29022 +       }
29023 +
29024 +       err = dbgaufs_init();
29025 +       if (unlikely(err))
29026 +               sysaufs_fin();
29027 +out:
29028 +       return err;
29029 +}
29030 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
29031 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
29032 +++ linux/fs/aufs/sysaufs.h     2017-07-29 12:14:25.906375514 +0200
29033 @@ -0,0 +1,101 @@
29034 +/*
29035 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29036 + *
29037 + * This program, aufs is free software; you can redistribute it and/or modify
29038 + * it under the terms of the GNU General Public License as published by
29039 + * the Free Software Foundation; either version 2 of the License, or
29040 + * (at your option) any later version.
29041 + *
29042 + * This program is distributed in the hope that it will be useful,
29043 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29044 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29045 + * GNU General Public License for more details.
29046 + *
29047 + * You should have received a copy of the GNU General Public License
29048 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29049 + */
29050 +
29051 +/*
29052 + * sysfs interface and mount lifetime management
29053 + */
29054 +
29055 +#ifndef __SYSAUFS_H__
29056 +#define __SYSAUFS_H__
29057 +
29058 +#ifdef __KERNEL__
29059 +
29060 +#include <linux/sysfs.h>
29061 +#include "module.h"
29062 +
29063 +struct super_block;
29064 +struct au_sbinfo;
29065 +
29066 +struct sysaufs_si_attr {
29067 +       struct attribute attr;
29068 +       int (*show)(struct seq_file *seq, struct super_block *sb);
29069 +};
29070 +
29071 +/* ---------------------------------------------------------------------- */
29072 +
29073 +/* sysaufs.c */
29074 +extern unsigned long sysaufs_si_mask;
29075 +extern struct kset *sysaufs_kset;
29076 +extern struct attribute *sysaufs_si_attrs[];
29077 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
29078 +int __init sysaufs_init(void);
29079 +void sysaufs_fin(void);
29080 +
29081 +/* ---------------------------------------------------------------------- */
29082 +
29083 +/* some people doesn't like to show a pointer in kernel */
29084 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
29085 +{
29086 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
29087 +}
29088 +
29089 +#define SysaufsSiNamePrefix    "si_"
29090 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
29091 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
29092 +{
29093 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
29094 +                sysaufs_si_id(sbinfo));
29095 +}
29096 +
29097 +struct au_branch;
29098 +#ifdef CONFIG_SYSFS
29099 +/* sysfs.c */
29100 +extern struct attribute_group *sysaufs_attr_group;
29101 +
29102 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
29103 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
29104 +                        char *buf);
29105 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
29106 +#ifdef CONFIG_COMPAT
29107 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
29108 +#endif
29109 +
29110 +void sysaufs_br_init(struct au_branch *br);
29111 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
29112 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
29113 +
29114 +#define sysaufs_brs_init()     do {} while (0)
29115 +
29116 +#else
29117 +#define sysaufs_attr_group     NULL
29118 +
29119 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
29120 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
29121 +       struct attribute *attr, char *buf)
29122 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
29123 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
29124 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
29125 +
29126 +static inline void sysaufs_brs_init(void)
29127 +{
29128 +       sysaufs_brs = 0;
29129 +}
29130 +
29131 +#endif /* CONFIG_SYSFS */
29132 +
29133 +#endif /* __KERNEL__ */
29134 +#endif /* __SYSAUFS_H__ */
29135 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
29136 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
29137 +++ linux/fs/aufs/sysfs.c       2017-07-29 12:14:25.906375514 +0200
29138 @@ -0,0 +1,376 @@
29139 +/*
29140 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29141 + *
29142 + * This program, aufs is free software; you can redistribute it and/or modify
29143 + * it under the terms of the GNU General Public License as published by
29144 + * the Free Software Foundation; either version 2 of the License, or
29145 + * (at your option) any later version.
29146 + *
29147 + * This program is distributed in the hope that it will be useful,
29148 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29149 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29150 + * GNU General Public License for more details.
29151 + *
29152 + * You should have received a copy of the GNU General Public License
29153 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29154 + */
29155 +
29156 +/*
29157 + * sysfs interface
29158 + */
29159 +
29160 +#include <linux/compat.h>
29161 +#include <linux/seq_file.h>
29162 +#include "aufs.h"
29163 +
29164 +#ifdef CONFIG_AUFS_FS_MODULE
29165 +/* this entry violates the "one line per file" policy of sysfs */
29166 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
29167 +                          char *buf)
29168 +{
29169 +       ssize_t err;
29170 +       static char *conf =
29171 +/* this file is generated at compiling */
29172 +#include "conf.str"
29173 +               ;
29174 +
29175 +       err = snprintf(buf, PAGE_SIZE, conf);
29176 +       if (unlikely(err >= PAGE_SIZE))
29177 +               err = -EFBIG;
29178 +       return err;
29179 +}
29180 +
29181 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
29182 +#endif
29183 +
29184 +static struct attribute *au_attr[] = {
29185 +#ifdef CONFIG_AUFS_FS_MODULE
29186 +       &au_config_attr.attr,
29187 +#endif
29188 +       NULL,   /* need to NULL terminate the list of attributes */
29189 +};
29190 +
29191 +static struct attribute_group sysaufs_attr_group_body = {
29192 +       .attrs = au_attr
29193 +};
29194 +
29195 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
29196 +
29197 +/* ---------------------------------------------------------------------- */
29198 +
29199 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
29200 +{
29201 +       int err;
29202 +
29203 +       SiMustAnyLock(sb);
29204 +
29205 +       err = 0;
29206 +       if (au_opt_test(au_mntflags(sb), XINO)) {
29207 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
29208 +               seq_putc(seq, '\n');
29209 +       }
29210 +       return err;
29211 +}
29212 +
29213 +/*
29214 + * the lifetime of branch is independent from the entry under sysfs.
29215 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
29216 + * unlinked.
29217 + */
29218 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
29219 +                        aufs_bindex_t bindex, int idx)
29220 +{
29221 +       int err;
29222 +       struct path path;
29223 +       struct dentry *root;
29224 +       struct au_branch *br;
29225 +       au_br_perm_str_t perm;
29226 +
29227 +       AuDbg("b%d\n", bindex);
29228 +
29229 +       err = 0;
29230 +       root = sb->s_root;
29231 +       di_read_lock_parent(root, !AuLock_IR);
29232 +       br = au_sbr(sb, bindex);
29233 +
29234 +       switch (idx) {
29235 +       case AuBrSysfs_BR:
29236 +               path.mnt = au_br_mnt(br);
29237 +               path.dentry = au_h_dptr(root, bindex);
29238 +               err = au_seq_path(seq, &path);
29239 +               if (!err) {
29240 +                       au_optstr_br_perm(&perm, br->br_perm);
29241 +                       seq_printf(seq, "=%s\n", perm.a);
29242 +               }
29243 +               break;
29244 +       case AuBrSysfs_BRID:
29245 +               seq_printf(seq, "%d\n", br->br_id);
29246 +               break;
29247 +       }
29248 +       di_read_unlock(root, !AuLock_IR);
29249 +       if (unlikely(err || seq_has_overflowed(seq)))
29250 +               err = -E2BIG;
29251 +
29252 +       return err;
29253 +}
29254 +
29255 +/* ---------------------------------------------------------------------- */
29256 +
29257 +static struct seq_file *au_seq(char *p, ssize_t len)
29258 +{
29259 +       struct seq_file *seq;
29260 +
29261 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
29262 +       if (seq) {
29263 +               /* mutex_init(&seq.lock); */
29264 +               seq->buf = p;
29265 +               seq->size = len;
29266 +               return seq; /* success */
29267 +       }
29268 +
29269 +       seq = ERR_PTR(-ENOMEM);
29270 +       return seq;
29271 +}
29272 +
29273 +#define SysaufsBr_PREFIX       "br"
29274 +#define SysaufsBrid_PREFIX     "brid"
29275 +
29276 +/* todo: file size may exceed PAGE_SIZE */
29277 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
29278 +                       char *buf)
29279 +{
29280 +       ssize_t err;
29281 +       int idx;
29282 +       long l;
29283 +       aufs_bindex_t bbot;
29284 +       struct au_sbinfo *sbinfo;
29285 +       struct super_block *sb;
29286 +       struct seq_file *seq;
29287 +       char *name;
29288 +       struct attribute **cattr;
29289 +
29290 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29291 +       sb = sbinfo->si_sb;
29292 +
29293 +       /*
29294 +        * prevent a race condition between sysfs and aufs.
29295 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
29296 +        * prohibits maintaining the sysfs entries.
29297 +        * hew we acquire read lock after sysfs_get_active_two().
29298 +        * on the other hand, the remount process may maintain the sysfs/aufs
29299 +        * entries after acquiring write lock.
29300 +        * it can cause a deadlock.
29301 +        * simply we gave up processing read here.
29302 +        */
29303 +       err = -EBUSY;
29304 +       if (unlikely(!si_noflush_read_trylock(sb)))
29305 +               goto out;
29306 +
29307 +       seq = au_seq(buf, PAGE_SIZE);
29308 +       err = PTR_ERR(seq);
29309 +       if (IS_ERR(seq))
29310 +               goto out_unlock;
29311 +
29312 +       name = (void *)attr->name;
29313 +       cattr = sysaufs_si_attrs;
29314 +       while (*cattr) {
29315 +               if (!strcmp(name, (*cattr)->name)) {
29316 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
29317 +                               ->show(seq, sb);
29318 +                       goto out_seq;
29319 +               }
29320 +               cattr++;
29321 +       }
29322 +
29323 +       if (!strncmp(name, SysaufsBrid_PREFIX,
29324 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
29325 +               idx = AuBrSysfs_BRID;
29326 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
29327 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
29328 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
29329 +               idx = AuBrSysfs_BR;
29330 +               name += sizeof(SysaufsBr_PREFIX) - 1;
29331 +       } else
29332 +                 BUG();
29333 +
29334 +       err = kstrtol(name, 10, &l);
29335 +       if (!err) {
29336 +               bbot = au_sbbot(sb);
29337 +               if (l <= bbot)
29338 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
29339 +               else
29340 +                       err = -ENOENT;
29341 +       }
29342 +
29343 +out_seq:
29344 +       if (!err) {
29345 +               err = seq->count;
29346 +               /* sysfs limit */
29347 +               if (unlikely(err == PAGE_SIZE))
29348 +                       err = -EFBIG;
29349 +       }
29350 +       kfree(seq);
29351 +out_unlock:
29352 +       si_read_unlock(sb);
29353 +out:
29354 +       return err;
29355 +}
29356 +
29357 +/* ---------------------------------------------------------------------- */
29358 +
29359 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
29360 +{
29361 +       int err;
29362 +       int16_t brid;
29363 +       aufs_bindex_t bindex, bbot;
29364 +       size_t sz;
29365 +       char *buf;
29366 +       struct seq_file *seq;
29367 +       struct au_branch *br;
29368 +
29369 +       si_read_lock(sb, AuLock_FLUSH);
29370 +       bbot = au_sbbot(sb);
29371 +       err = bbot + 1;
29372 +       if (!arg)
29373 +               goto out;
29374 +
29375 +       err = -ENOMEM;
29376 +       buf = (void *)__get_free_page(GFP_NOFS);
29377 +       if (unlikely(!buf))
29378 +               goto out;
29379 +
29380 +       seq = au_seq(buf, PAGE_SIZE);
29381 +       err = PTR_ERR(seq);
29382 +       if (IS_ERR(seq))
29383 +               goto out_buf;
29384 +
29385 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
29386 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
29387 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
29388 +               if (unlikely(err))
29389 +                       break;
29390 +
29391 +               br = au_sbr(sb, bindex);
29392 +               brid = br->br_id;
29393 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
29394 +               err = __put_user(brid, &arg->id);
29395 +               if (unlikely(err))
29396 +                       break;
29397 +
29398 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
29399 +               err = __put_user(br->br_perm, &arg->perm);
29400 +               if (unlikely(err))
29401 +                       break;
29402 +
29403 +               err = au_seq_path(seq, &br->br_path);
29404 +               if (unlikely(err))
29405 +                       break;
29406 +               seq_putc(seq, '\0');
29407 +               if (!seq_has_overflowed(seq)) {
29408 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
29409 +                       seq->count = 0;
29410 +                       if (unlikely(err))
29411 +                               break;
29412 +               } else {
29413 +                       err = -E2BIG;
29414 +                       goto out_seq;
29415 +               }
29416 +       }
29417 +       if (unlikely(err))
29418 +               err = -EFAULT;
29419 +
29420 +out_seq:
29421 +       kfree(seq);
29422 +out_buf:
29423 +       free_page((unsigned long)buf);
29424 +out:
29425 +       si_read_unlock(sb);
29426 +       return err;
29427 +}
29428 +
29429 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
29430 +{
29431 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
29432 +}
29433 +
29434 +#ifdef CONFIG_COMPAT
29435 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
29436 +{
29437 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
29438 +}
29439 +#endif
29440 +
29441 +/* ---------------------------------------------------------------------- */
29442 +
29443 +void sysaufs_br_init(struct au_branch *br)
29444 +{
29445 +       int i;
29446 +       struct au_brsysfs *br_sysfs;
29447 +       struct attribute *attr;
29448 +
29449 +       br_sysfs = br->br_sysfs;
29450 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29451 +               attr = &br_sysfs->attr;
29452 +               sysfs_attr_init(attr);
29453 +               attr->name = br_sysfs->name;
29454 +               attr->mode = S_IRUGO;
29455 +               br_sysfs++;
29456 +       }
29457 +}
29458 +
29459 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
29460 +{
29461 +       struct au_branch *br;
29462 +       struct kobject *kobj;
29463 +       struct au_brsysfs *br_sysfs;
29464 +       int i;
29465 +       aufs_bindex_t bbot;
29466 +
29467 +       dbgaufs_brs_del(sb, bindex);
29468 +
29469 +       if (!sysaufs_brs)
29470 +               return;
29471 +
29472 +       kobj = &au_sbi(sb)->si_kobj;
29473 +       bbot = au_sbbot(sb);
29474 +       for (; bindex <= bbot; bindex++) {
29475 +               br = au_sbr(sb, bindex);
29476 +               br_sysfs = br->br_sysfs;
29477 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29478 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
29479 +                       br_sysfs++;
29480 +               }
29481 +       }
29482 +}
29483 +
29484 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
29485 +{
29486 +       int err, i;
29487 +       aufs_bindex_t bbot;
29488 +       struct kobject *kobj;
29489 +       struct au_branch *br;
29490 +       struct au_brsysfs *br_sysfs;
29491 +
29492 +       dbgaufs_brs_add(sb, bindex);
29493 +
29494 +       if (!sysaufs_brs)
29495 +               return;
29496 +
29497 +       kobj = &au_sbi(sb)->si_kobj;
29498 +       bbot = au_sbbot(sb);
29499 +       for (; bindex <= bbot; bindex++) {
29500 +               br = au_sbr(sb, bindex);
29501 +               br_sysfs = br->br_sysfs;
29502 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
29503 +                        SysaufsBr_PREFIX "%d", bindex);
29504 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
29505 +                        SysaufsBrid_PREFIX "%d", bindex);
29506 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29507 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
29508 +                       if (unlikely(err))
29509 +                               pr_warn("failed %s under sysfs(%d)\n",
29510 +                                       br_sysfs->name, err);
29511 +                       br_sysfs++;
29512 +               }
29513 +       }
29514 +}
29515 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
29516 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
29517 +++ linux/fs/aufs/sysrq.c       2017-07-29 12:14:25.906375514 +0200
29518 @@ -0,0 +1,157 @@
29519 +/*
29520 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29521 + *
29522 + * This program, aufs is free software; you can redistribute it and/or modify
29523 + * it under the terms of the GNU General Public License as published by
29524 + * the Free Software Foundation; either version 2 of the License, or
29525 + * (at your option) any later version.
29526 + *
29527 + * This program is distributed in the hope that it will be useful,
29528 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29529 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29530 + * GNU General Public License for more details.
29531 + *
29532 + * You should have received a copy of the GNU General Public License
29533 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29534 + */
29535 +
29536 +/*
29537 + * magic sysrq hanlder
29538 + */
29539 +
29540 +/* #include <linux/sysrq.h> */
29541 +#include <linux/writeback.h>
29542 +#include "aufs.h"
29543 +
29544 +/* ---------------------------------------------------------------------- */
29545 +
29546 +static void sysrq_sb(struct super_block *sb)
29547 +{
29548 +       char *plevel;
29549 +       struct au_sbinfo *sbinfo;
29550 +       struct file *file;
29551 +       struct au_sphlhead *files;
29552 +       struct au_finfo *finfo;
29553 +
29554 +       plevel = au_plevel;
29555 +       au_plevel = KERN_WARNING;
29556 +
29557 +       /* since we define pr_fmt, call printk directly */
29558 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29559 +
29560 +       sbinfo = au_sbi(sb);
29561 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
29562 +       pr("superblock\n");
29563 +       au_dpri_sb(sb);
29564 +
29565 +#if 0
29566 +       pr("root dentry\n");
29567 +       au_dpri_dentry(sb->s_root);
29568 +       pr("root inode\n");
29569 +       au_dpri_inode(d_inode(sb->s_root));
29570 +#endif
29571 +
29572 +#if 0
29573 +       do {
29574 +               int err, i, j, ndentry;
29575 +               struct au_dcsub_pages dpages;
29576 +               struct au_dpage *dpage;
29577 +
29578 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
29579 +               if (unlikely(err))
29580 +                       break;
29581 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29582 +               if (!err)
29583 +                       for (i = 0; i < dpages.ndpage; i++) {
29584 +                               dpage = dpages.dpages + i;
29585 +                               ndentry = dpage->ndentry;
29586 +                               for (j = 0; j < ndentry; j++)
29587 +                                       au_dpri_dentry(dpage->dentries[j]);
29588 +                       }
29589 +               au_dpages_free(&dpages);
29590 +       } while (0);
29591 +#endif
29592 +
29593 +#if 1
29594 +       {
29595 +               struct inode *i;
29596 +
29597 +               pr("isolated inode\n");
29598 +               spin_lock(&sb->s_inode_list_lock);
29599 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29600 +                       spin_lock(&i->i_lock);
29601 +                       if (1 || hlist_empty(&i->i_dentry))
29602 +                               au_dpri_inode(i);
29603 +                       spin_unlock(&i->i_lock);
29604 +               }
29605 +               spin_unlock(&sb->s_inode_list_lock);
29606 +       }
29607 +#endif
29608 +       pr("files\n");
29609 +       files = &au_sbi(sb)->si_files;
29610 +       spin_lock(&files->spin);
29611 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
29612 +               umode_t mode;
29613 +
29614 +               file = finfo->fi_file;
29615 +               mode = file_inode(file)->i_mode;
29616 +               if (!special_file(mode))
29617 +                       au_dpri_file(file);
29618 +       }
29619 +       spin_unlock(&files->spin);
29620 +       pr("done\n");
29621 +
29622 +#undef pr
29623 +       au_plevel = plevel;
29624 +}
29625 +
29626 +/* ---------------------------------------------------------------------- */
29627 +
29628 +/* module parameter */
29629 +static char *aufs_sysrq_key = "a";
29630 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29631 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29632 +
29633 +static void au_sysrq(int key __maybe_unused)
29634 +{
29635 +       struct au_sbinfo *sbinfo;
29636 +
29637 +       lockdep_off();
29638 +       au_sbilist_lock();
29639 +       hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
29640 +               sysrq_sb(sbinfo->si_sb);
29641 +       au_sbilist_unlock();
29642 +       lockdep_on();
29643 +}
29644 +
29645 +static struct sysrq_key_op au_sysrq_op = {
29646 +       .handler        = au_sysrq,
29647 +       .help_msg       = "Aufs",
29648 +       .action_msg     = "Aufs",
29649 +       .enable_mask    = SYSRQ_ENABLE_DUMP
29650 +};
29651 +
29652 +/* ---------------------------------------------------------------------- */
29653 +
29654 +int __init au_sysrq_init(void)
29655 +{
29656 +       int err;
29657 +       char key;
29658 +
29659 +       err = -1;
29660 +       key = *aufs_sysrq_key;
29661 +       if ('a' <= key && key <= 'z')
29662 +               err = register_sysrq_key(key, &au_sysrq_op);
29663 +       if (unlikely(err))
29664 +               pr_err("err %d, sysrq=%c\n", err, key);
29665 +       return err;
29666 +}
29667 +
29668 +void au_sysrq_fin(void)
29669 +{
29670 +       int err;
29671 +
29672 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29673 +       if (unlikely(err))
29674 +               pr_err("err %d (ignored)\n", err);
29675 +}
29676 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29677 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
29678 +++ linux/fs/aufs/vdir.c        2017-07-29 12:14:25.906375514 +0200
29679 @@ -0,0 +1,892 @@
29680 +/*
29681 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29682 + *
29683 + * This program, aufs is free software; you can redistribute it and/or modify
29684 + * it under the terms of the GNU General Public License as published by
29685 + * the Free Software Foundation; either version 2 of the License, or
29686 + * (at your option) any later version.
29687 + *
29688 + * This program is distributed in the hope that it will be useful,
29689 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29690 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29691 + * GNU General Public License for more details.
29692 + *
29693 + * You should have received a copy of the GNU General Public License
29694 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29695 + */
29696 +
29697 +/*
29698 + * virtual or vertical directory
29699 + */
29700 +
29701 +#include "aufs.h"
29702 +
29703 +static unsigned int calc_size(int nlen)
29704 +{
29705 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
29706 +}
29707 +
29708 +static int set_deblk_end(union au_vdir_deblk_p *p,
29709 +                        union au_vdir_deblk_p *deblk_end)
29710 +{
29711 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29712 +               p->de->de_str.len = 0;
29713 +               /* smp_mb(); */
29714 +               return 0;
29715 +       }
29716 +       return -1; /* error */
29717 +}
29718 +
29719 +/* returns true or false */
29720 +static int is_deblk_end(union au_vdir_deblk_p *p,
29721 +                       union au_vdir_deblk_p *deblk_end)
29722 +{
29723 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29724 +               return !p->de->de_str.len;
29725 +       return 1;
29726 +}
29727 +
29728 +static unsigned char *last_deblk(struct au_vdir *vdir)
29729 +{
29730 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29731 +}
29732 +
29733 +/* ---------------------------------------------------------------------- */
29734 +
29735 +/* estimate the appropriate size for name hash table */
29736 +unsigned int au_rdhash_est(loff_t sz)
29737 +{
29738 +       unsigned int n;
29739 +
29740 +       n = UINT_MAX;
29741 +       sz >>= 10;
29742 +       if (sz < n)
29743 +               n = sz;
29744 +       if (sz < AUFS_RDHASH_DEF)
29745 +               n = AUFS_RDHASH_DEF;
29746 +       /* pr_info("n %u\n", n); */
29747 +       return n;
29748 +}
29749 +
29750 +/*
29751 + * the allocated memory has to be freed by
29752 + * au_nhash_wh_free() or au_nhash_de_free().
29753 + */
29754 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29755 +{
29756 +       struct hlist_head *head;
29757 +       unsigned int u;
29758 +       size_t sz;
29759 +
29760 +       sz = sizeof(*nhash->nh_head) * num_hash;
29761 +       head = kmalloc(sz, gfp);
29762 +       if (head) {
29763 +               nhash->nh_num = num_hash;
29764 +               nhash->nh_head = head;
29765 +               for (u = 0; u < num_hash; u++)
29766 +                       INIT_HLIST_HEAD(head++);
29767 +               return 0; /* success */
29768 +       }
29769 +
29770 +       return -ENOMEM;
29771 +}
29772 +
29773 +static void nhash_count(struct hlist_head *head)
29774 +{
29775 +#if 0
29776 +       unsigned long n;
29777 +       struct hlist_node *pos;
29778 +
29779 +       n = 0;
29780 +       hlist_for_each(pos, head)
29781 +               n++;
29782 +       pr_info("%lu\n", n);
29783 +#endif
29784 +}
29785 +
29786 +static void au_nhash_wh_do_free(struct hlist_head *head)
29787 +{
29788 +       struct au_vdir_wh *pos;
29789 +       struct hlist_node *node;
29790 +
29791 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29792 +               kfree(pos);
29793 +}
29794 +
29795 +static void au_nhash_de_do_free(struct hlist_head *head)
29796 +{
29797 +       struct au_vdir_dehstr *pos;
29798 +       struct hlist_node *node;
29799 +
29800 +       hlist_for_each_entry_safe(pos, node, head, hash)
29801 +               au_cache_free_vdir_dehstr(pos);
29802 +}
29803 +
29804 +static void au_nhash_do_free(struct au_nhash *nhash,
29805 +                            void (*free)(struct hlist_head *head))
29806 +{
29807 +       unsigned int n;
29808 +       struct hlist_head *head;
29809 +
29810 +       n = nhash->nh_num;
29811 +       if (!n)
29812 +               return;
29813 +
29814 +       head = nhash->nh_head;
29815 +       while (n-- > 0) {
29816 +               nhash_count(head);
29817 +               free(head++);
29818 +       }
29819 +       kfree(nhash->nh_head);
29820 +}
29821 +
29822 +void au_nhash_wh_free(struct au_nhash *whlist)
29823 +{
29824 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29825 +}
29826 +
29827 +static void au_nhash_de_free(struct au_nhash *delist)
29828 +{
29829 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29830 +}
29831 +
29832 +/* ---------------------------------------------------------------------- */
29833 +
29834 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29835 +                           int limit)
29836 +{
29837 +       int num;
29838 +       unsigned int u, n;
29839 +       struct hlist_head *head;
29840 +       struct au_vdir_wh *pos;
29841 +
29842 +       num = 0;
29843 +       n = whlist->nh_num;
29844 +       head = whlist->nh_head;
29845 +       for (u = 0; u < n; u++, head++)
29846 +               hlist_for_each_entry(pos, head, wh_hash)
29847 +                       if (pos->wh_bindex == btgt && ++num > limit)
29848 +                               return 1;
29849 +       return 0;
29850 +}
29851 +
29852 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29853 +                                      unsigned char *name,
29854 +                                      unsigned int len)
29855 +{
29856 +       unsigned int v;
29857 +       /* const unsigned int magic_bit = 12; */
29858 +
29859 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29860 +
29861 +       v = 0;
29862 +       if (len > 8)
29863 +               len = 8;
29864 +       while (len--)
29865 +               v += *name++;
29866 +       /* v = hash_long(v, magic_bit); */
29867 +       v %= nhash->nh_num;
29868 +       return nhash->nh_head + v;
29869 +}
29870 +
29871 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29872 +                             int nlen)
29873 +{
29874 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29875 +}
29876 +
29877 +/* returns found or not */
29878 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29879 +{
29880 +       struct hlist_head *head;
29881 +       struct au_vdir_wh *pos;
29882 +       struct au_vdir_destr *str;
29883 +
29884 +       head = au_name_hash(whlist, name, nlen);
29885 +       hlist_for_each_entry(pos, head, wh_hash) {
29886 +               str = &pos->wh_str;
29887 +               AuDbg("%.*s\n", str->len, str->name);
29888 +               if (au_nhash_test_name(str, name, nlen))
29889 +                       return 1;
29890 +       }
29891 +       return 0;
29892 +}
29893 +
29894 +/* returns found(true) or not */
29895 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29896 +{
29897 +       struct hlist_head *head;
29898 +       struct au_vdir_dehstr *pos;
29899 +       struct au_vdir_destr *str;
29900 +
29901 +       head = au_name_hash(delist, name, nlen);
29902 +       hlist_for_each_entry(pos, head, hash) {
29903 +               str = pos->str;
29904 +               AuDbg("%.*s\n", str->len, str->name);
29905 +               if (au_nhash_test_name(str, name, nlen))
29906 +                       return 1;
29907 +       }
29908 +       return 0;
29909 +}
29910 +
29911 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29912 +                           unsigned char d_type)
29913 +{
29914 +#ifdef CONFIG_AUFS_SHWH
29915 +       wh->wh_ino = ino;
29916 +       wh->wh_type = d_type;
29917 +#endif
29918 +}
29919 +
29920 +/* ---------------------------------------------------------------------- */
29921 +
29922 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29923 +                      unsigned int d_type, aufs_bindex_t bindex,
29924 +                      unsigned char shwh)
29925 +{
29926 +       int err;
29927 +       struct au_vdir_destr *str;
29928 +       struct au_vdir_wh *wh;
29929 +
29930 +       AuDbg("%.*s\n", nlen, name);
29931 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29932 +
29933 +       err = -ENOMEM;
29934 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29935 +       if (unlikely(!wh))
29936 +               goto out;
29937 +
29938 +       err = 0;
29939 +       wh->wh_bindex = bindex;
29940 +       if (shwh)
29941 +               au_shwh_init_wh(wh, ino, d_type);
29942 +       str = &wh->wh_str;
29943 +       str->len = nlen;
29944 +       memcpy(str->name, name, nlen);
29945 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29946 +       /* smp_mb(); */
29947 +
29948 +out:
29949 +       return err;
29950 +}
29951 +
29952 +static int append_deblk(struct au_vdir *vdir)
29953 +{
29954 +       int err;
29955 +       unsigned long ul;
29956 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29957 +       union au_vdir_deblk_p p, deblk_end;
29958 +       unsigned char **o;
29959 +
29960 +       err = -ENOMEM;
29961 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29962 +                       GFP_NOFS, /*may_shrink*/0);
29963 +       if (unlikely(!o))
29964 +               goto out;
29965 +
29966 +       vdir->vd_deblk = o;
29967 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29968 +       if (p.deblk) {
29969 +               ul = vdir->vd_nblk++;
29970 +               vdir->vd_deblk[ul] = p.deblk;
29971 +               vdir->vd_last.ul = ul;
29972 +               vdir->vd_last.p.deblk = p.deblk;
29973 +               deblk_end.deblk = p.deblk + deblk_sz;
29974 +               err = set_deblk_end(&p, &deblk_end);
29975 +       }
29976 +
29977 +out:
29978 +       return err;
29979 +}
29980 +
29981 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29982 +                    unsigned int d_type, struct au_nhash *delist)
29983 +{
29984 +       int err;
29985 +       unsigned int sz;
29986 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29987 +       union au_vdir_deblk_p p, *room, deblk_end;
29988 +       struct au_vdir_dehstr *dehstr;
29989 +
29990 +       p.deblk = last_deblk(vdir);
29991 +       deblk_end.deblk = p.deblk + deblk_sz;
29992 +       room = &vdir->vd_last.p;
29993 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29994 +                 || !is_deblk_end(room, &deblk_end));
29995 +
29996 +       sz = calc_size(nlen);
29997 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29998 +               err = append_deblk(vdir);
29999 +               if (unlikely(err))
30000 +                       goto out;
30001 +
30002 +               p.deblk = last_deblk(vdir);
30003 +               deblk_end.deblk = p.deblk + deblk_sz;
30004 +               /* smp_mb(); */
30005 +               AuDebugOn(room->deblk != p.deblk);
30006 +       }
30007 +
30008 +       err = -ENOMEM;
30009 +       dehstr = au_cache_alloc_vdir_dehstr();
30010 +       if (unlikely(!dehstr))
30011 +               goto out;
30012 +
30013 +       dehstr->str = &room->de->de_str;
30014 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
30015 +       room->de->de_ino = ino;
30016 +       room->de->de_type = d_type;
30017 +       room->de->de_str.len = nlen;
30018 +       memcpy(room->de->de_str.name, name, nlen);
30019 +
30020 +       err = 0;
30021 +       room->deblk += sz;
30022 +       if (unlikely(set_deblk_end(room, &deblk_end)))
30023 +               err = append_deblk(vdir);
30024 +       /* smp_mb(); */
30025 +
30026 +out:
30027 +       return err;
30028 +}
30029 +
30030 +/* ---------------------------------------------------------------------- */
30031 +
30032 +void au_vdir_free(struct au_vdir *vdir)
30033 +{
30034 +       unsigned char **deblk;
30035 +
30036 +       deblk = vdir->vd_deblk;
30037 +       while (vdir->vd_nblk--)
30038 +               kfree(*deblk++);
30039 +       kfree(vdir->vd_deblk);
30040 +       au_cache_free_vdir(vdir);
30041 +}
30042 +
30043 +static struct au_vdir *alloc_vdir(struct file *file)
30044 +{
30045 +       struct au_vdir *vdir;
30046 +       struct super_block *sb;
30047 +       int err;
30048 +
30049 +       sb = file->f_path.dentry->d_sb;
30050 +       SiMustAnyLock(sb);
30051 +
30052 +       err = -ENOMEM;
30053 +       vdir = au_cache_alloc_vdir();
30054 +       if (unlikely(!vdir))
30055 +               goto out;
30056 +
30057 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
30058 +       if (unlikely(!vdir->vd_deblk))
30059 +               goto out_free;
30060 +
30061 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
30062 +       if (!vdir->vd_deblk_sz) {
30063 +               /* estimate the appropriate size for deblk */
30064 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
30065 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
30066 +       }
30067 +       vdir->vd_nblk = 0;
30068 +       vdir->vd_version = 0;
30069 +       vdir->vd_jiffy = 0;
30070 +       err = append_deblk(vdir);
30071 +       if (!err)
30072 +               return vdir; /* success */
30073 +
30074 +       kfree(vdir->vd_deblk);
30075 +
30076 +out_free:
30077 +       au_cache_free_vdir(vdir);
30078 +out:
30079 +       vdir = ERR_PTR(err);
30080 +       return vdir;
30081 +}
30082 +
30083 +static int reinit_vdir(struct au_vdir *vdir)
30084 +{
30085 +       int err;
30086 +       union au_vdir_deblk_p p, deblk_end;
30087 +
30088 +       while (vdir->vd_nblk > 1) {
30089 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
30090 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
30091 +               vdir->vd_nblk--;
30092 +       }
30093 +       p.deblk = vdir->vd_deblk[0];
30094 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
30095 +       err = set_deblk_end(&p, &deblk_end);
30096 +       /* keep vd_dblk_sz */
30097 +       vdir->vd_last.ul = 0;
30098 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
30099 +       vdir->vd_version = 0;
30100 +       vdir->vd_jiffy = 0;
30101 +       /* smp_mb(); */
30102 +       return err;
30103 +}
30104 +
30105 +/* ---------------------------------------------------------------------- */
30106 +
30107 +#define AuFillVdir_CALLED      1
30108 +#define AuFillVdir_WHABLE      (1 << 1)
30109 +#define AuFillVdir_SHWH                (1 << 2)
30110 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
30111 +#define au_fset_fillvdir(flags, name) \
30112 +       do { (flags) |= AuFillVdir_##name; } while (0)
30113 +#define au_fclr_fillvdir(flags, name) \
30114 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
30115 +
30116 +#ifndef CONFIG_AUFS_SHWH
30117 +#undef AuFillVdir_SHWH
30118 +#define AuFillVdir_SHWH                0
30119 +#endif
30120 +
30121 +struct fillvdir_arg {
30122 +       struct dir_context      ctx;
30123 +       struct file             *file;
30124 +       struct au_vdir          *vdir;
30125 +       struct au_nhash         delist;
30126 +       struct au_nhash         whlist;
30127 +       aufs_bindex_t           bindex;
30128 +       unsigned int            flags;
30129 +       int                     err;
30130 +};
30131 +
30132 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
30133 +                   loff_t offset __maybe_unused, u64 h_ino,
30134 +                   unsigned int d_type)
30135 +{
30136 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
30137 +       char *name = (void *)__name;
30138 +       struct super_block *sb;
30139 +       ino_t ino;
30140 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
30141 +
30142 +       arg->err = 0;
30143 +       sb = arg->file->f_path.dentry->d_sb;
30144 +       au_fset_fillvdir(arg->flags, CALLED);
30145 +       /* smp_mb(); */
30146 +       if (nlen <= AUFS_WH_PFX_LEN
30147 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
30148 +               if (test_known(&arg->delist, name, nlen)
30149 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
30150 +                       goto out; /* already exists or whiteouted */
30151 +
30152 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
30153 +               if (!arg->err) {
30154 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
30155 +                               d_type = DT_UNKNOWN;
30156 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
30157 +                                            d_type, &arg->delist);
30158 +               }
30159 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
30160 +               name += AUFS_WH_PFX_LEN;
30161 +               nlen -= AUFS_WH_PFX_LEN;
30162 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
30163 +                       goto out; /* already whiteouted */
30164 +
30165 +               if (shwh)
30166 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
30167 +                                            &ino);
30168 +               if (!arg->err) {
30169 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
30170 +                               d_type = DT_UNKNOWN;
30171 +                       arg->err = au_nhash_append_wh
30172 +                               (&arg->whlist, name, nlen, ino, d_type,
30173 +                                arg->bindex, shwh);
30174 +               }
30175 +       }
30176 +
30177 +out:
30178 +       if (!arg->err)
30179 +               arg->vdir->vd_jiffy = jiffies;
30180 +       /* smp_mb(); */
30181 +       AuTraceErr(arg->err);
30182 +       return arg->err;
30183 +}
30184 +
30185 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
30186 +                         struct au_nhash *whlist, struct au_nhash *delist)
30187 +{
30188 +#ifdef CONFIG_AUFS_SHWH
30189 +       int err;
30190 +       unsigned int nh, u;
30191 +       struct hlist_head *head;
30192 +       struct au_vdir_wh *pos;
30193 +       struct hlist_node *n;
30194 +       char *p, *o;
30195 +       struct au_vdir_destr *destr;
30196 +
30197 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
30198 +
30199 +       err = -ENOMEM;
30200 +       o = p = (void *)__get_free_page(GFP_NOFS);
30201 +       if (unlikely(!p))
30202 +               goto out;
30203 +
30204 +       err = 0;
30205 +       nh = whlist->nh_num;
30206 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
30207 +       p += AUFS_WH_PFX_LEN;
30208 +       for (u = 0; u < nh; u++) {
30209 +               head = whlist->nh_head + u;
30210 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
30211 +                       destr = &pos->wh_str;
30212 +                       memcpy(p, destr->name, destr->len);
30213 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
30214 +                                       pos->wh_ino, pos->wh_type, delist);
30215 +                       if (unlikely(err))
30216 +                               break;
30217 +               }
30218 +       }
30219 +
30220 +       free_page((unsigned long)o);
30221 +
30222 +out:
30223 +       AuTraceErr(err);
30224 +       return err;
30225 +#else
30226 +       return 0;
30227 +#endif
30228 +}
30229 +
30230 +static int au_do_read_vdir(struct fillvdir_arg *arg)
30231 +{
30232 +       int err;
30233 +       unsigned int rdhash;
30234 +       loff_t offset;
30235 +       aufs_bindex_t bbot, bindex, btop;
30236 +       unsigned char shwh;
30237 +       struct file *hf, *file;
30238 +       struct super_block *sb;
30239 +
30240 +       file = arg->file;
30241 +       sb = file->f_path.dentry->d_sb;
30242 +       SiMustAnyLock(sb);
30243 +
30244 +       rdhash = au_sbi(sb)->si_rdhash;
30245 +       if (!rdhash)
30246 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
30247 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
30248 +       if (unlikely(err))
30249 +               goto out;
30250 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
30251 +       if (unlikely(err))
30252 +               goto out_delist;
30253 +
30254 +       err = 0;
30255 +       arg->flags = 0;
30256 +       shwh = 0;
30257 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
30258 +               shwh = 1;
30259 +               au_fset_fillvdir(arg->flags, SHWH);
30260 +       }
30261 +       btop = au_fbtop(file);
30262 +       bbot = au_fbbot_dir(file);
30263 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
30264 +               hf = au_hf_dir(file, bindex);
30265 +               if (!hf)
30266 +                       continue;
30267 +
30268 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
30269 +               err = offset;
30270 +               if (unlikely(offset))
30271 +                       break;
30272 +
30273 +               arg->bindex = bindex;
30274 +               au_fclr_fillvdir(arg->flags, WHABLE);
30275 +               if (shwh
30276 +                   || (bindex != bbot
30277 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
30278 +                       au_fset_fillvdir(arg->flags, WHABLE);
30279 +               do {
30280 +                       arg->err = 0;
30281 +                       au_fclr_fillvdir(arg->flags, CALLED);
30282 +                       /* smp_mb(); */
30283 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
30284 +                       if (err >= 0)
30285 +                               err = arg->err;
30286 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
30287 +
30288 +               /*
30289 +                * dir_relax() may be good for concurrency, but aufs should not
30290 +                * use it since it will cause a lockdep problem.
30291 +                */
30292 +       }
30293 +
30294 +       if (!err && shwh)
30295 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
30296 +
30297 +       au_nhash_wh_free(&arg->whlist);
30298 +
30299 +out_delist:
30300 +       au_nhash_de_free(&arg->delist);
30301 +out:
30302 +       return err;
30303 +}
30304 +
30305 +static int read_vdir(struct file *file, int may_read)
30306 +{
30307 +       int err;
30308 +       unsigned long expire;
30309 +       unsigned char do_read;
30310 +       struct fillvdir_arg arg = {
30311 +               .ctx = {
30312 +                       .actor = fillvdir
30313 +               }
30314 +       };
30315 +       struct inode *inode;
30316 +       struct au_vdir *vdir, *allocated;
30317 +
30318 +       err = 0;
30319 +       inode = file_inode(file);
30320 +       IMustLock(inode);
30321 +       IiMustWriteLock(inode);
30322 +       SiMustAnyLock(inode->i_sb);
30323 +
30324 +       allocated = NULL;
30325 +       do_read = 0;
30326 +       expire = au_sbi(inode->i_sb)->si_rdcache;
30327 +       vdir = au_ivdir(inode);
30328 +       if (!vdir) {
30329 +               do_read = 1;
30330 +               vdir = alloc_vdir(file);
30331 +               err = PTR_ERR(vdir);
30332 +               if (IS_ERR(vdir))
30333 +                       goto out;
30334 +               err = 0;
30335 +               allocated = vdir;
30336 +       } else if (may_read
30337 +                  && (inode->i_version != vdir->vd_version
30338 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
30339 +               do_read = 1;
30340 +               err = reinit_vdir(vdir);
30341 +               if (unlikely(err))
30342 +                       goto out;
30343 +       }
30344 +
30345 +       if (!do_read)
30346 +               return 0; /* success */
30347 +
30348 +       arg.file = file;
30349 +       arg.vdir = vdir;
30350 +       err = au_do_read_vdir(&arg);
30351 +       if (!err) {
30352 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
30353 +               vdir->vd_version = inode->i_version;
30354 +               vdir->vd_last.ul = 0;
30355 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
30356 +               if (allocated)
30357 +                       au_set_ivdir(inode, allocated);
30358 +       } else if (allocated)
30359 +               au_vdir_free(allocated);
30360 +
30361 +out:
30362 +       return err;
30363 +}
30364 +
30365 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
30366 +{
30367 +       int err, rerr;
30368 +       unsigned long ul, n;
30369 +       const unsigned int deblk_sz = src->vd_deblk_sz;
30370 +
30371 +       AuDebugOn(tgt->vd_nblk != 1);
30372 +
30373 +       err = -ENOMEM;
30374 +       if (tgt->vd_nblk < src->vd_nblk) {
30375 +               unsigned char **p;
30376 +
30377 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
30378 +                               GFP_NOFS, /*may_shrink*/0);
30379 +               if (unlikely(!p))
30380 +                       goto out;
30381 +               tgt->vd_deblk = p;
30382 +       }
30383 +
30384 +       if (tgt->vd_deblk_sz != deblk_sz) {
30385 +               unsigned char *p;
30386 +
30387 +               tgt->vd_deblk_sz = deblk_sz;
30388 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
30389 +                               /*may_shrink*/1);
30390 +               if (unlikely(!p))
30391 +                       goto out;
30392 +               tgt->vd_deblk[0] = p;
30393 +       }
30394 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
30395 +       tgt->vd_version = src->vd_version;
30396 +       tgt->vd_jiffy = src->vd_jiffy;
30397 +
30398 +       n = src->vd_nblk;
30399 +       for (ul = 1; ul < n; ul++) {
30400 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
30401 +                                           GFP_NOFS);
30402 +               if (unlikely(!tgt->vd_deblk[ul]))
30403 +                       goto out;
30404 +               tgt->vd_nblk++;
30405 +       }
30406 +       tgt->vd_nblk = n;
30407 +       tgt->vd_last.ul = tgt->vd_last.ul;
30408 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
30409 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
30410 +               - src->vd_deblk[src->vd_last.ul];
30411 +       /* smp_mb(); */
30412 +       return 0; /* success */
30413 +
30414 +out:
30415 +       rerr = reinit_vdir(tgt);
30416 +       BUG_ON(rerr);
30417 +       return err;
30418 +}
30419 +
30420 +int au_vdir_init(struct file *file)
30421 +{
30422 +       int err;
30423 +       struct inode *inode;
30424 +       struct au_vdir *vdir_cache, *allocated;
30425 +
30426 +       /* test file->f_pos here instead of ctx->pos */
30427 +       err = read_vdir(file, !file->f_pos);
30428 +       if (unlikely(err))
30429 +               goto out;
30430 +
30431 +       allocated = NULL;
30432 +       vdir_cache = au_fvdir_cache(file);
30433 +       if (!vdir_cache) {
30434 +               vdir_cache = alloc_vdir(file);
30435 +               err = PTR_ERR(vdir_cache);
30436 +               if (IS_ERR(vdir_cache))
30437 +                       goto out;
30438 +               allocated = vdir_cache;
30439 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
30440 +               /* test file->f_pos here instead of ctx->pos */
30441 +               err = reinit_vdir(vdir_cache);
30442 +               if (unlikely(err))
30443 +                       goto out;
30444 +       } else
30445 +               return 0; /* success */
30446 +
30447 +       inode = file_inode(file);
30448 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
30449 +       if (!err) {
30450 +               file->f_version = inode->i_version;
30451 +               if (allocated)
30452 +                       au_set_fvdir_cache(file, allocated);
30453 +       } else if (allocated)
30454 +               au_vdir_free(allocated);
30455 +
30456 +out:
30457 +       return err;
30458 +}
30459 +
30460 +static loff_t calc_offset(struct au_vdir *vdir)
30461 +{
30462 +       loff_t offset;
30463 +       union au_vdir_deblk_p p;
30464 +
30465 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
30466 +       offset = vdir->vd_last.p.deblk - p.deblk;
30467 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
30468 +       return offset;
30469 +}
30470 +
30471 +/* returns true or false */
30472 +static int seek_vdir(struct file *file, struct dir_context *ctx)
30473 +{
30474 +       int valid;
30475 +       unsigned int deblk_sz;
30476 +       unsigned long ul, n;
30477 +       loff_t offset;
30478 +       union au_vdir_deblk_p p, deblk_end;
30479 +       struct au_vdir *vdir_cache;
30480 +
30481 +       valid = 1;
30482 +       vdir_cache = au_fvdir_cache(file);
30483 +       offset = calc_offset(vdir_cache);
30484 +       AuDbg("offset %lld\n", offset);
30485 +       if (ctx->pos == offset)
30486 +               goto out;
30487 +
30488 +       vdir_cache->vd_last.ul = 0;
30489 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
30490 +       if (!ctx->pos)
30491 +               goto out;
30492 +
30493 +       valid = 0;
30494 +       deblk_sz = vdir_cache->vd_deblk_sz;
30495 +       ul = div64_u64(ctx->pos, deblk_sz);
30496 +       AuDbg("ul %lu\n", ul);
30497 +       if (ul >= vdir_cache->vd_nblk)
30498 +               goto out;
30499 +
30500 +       n = vdir_cache->vd_nblk;
30501 +       for (; ul < n; ul++) {
30502 +               p.deblk = vdir_cache->vd_deblk[ul];
30503 +               deblk_end.deblk = p.deblk + deblk_sz;
30504 +               offset = ul;
30505 +               offset *= deblk_sz;
30506 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
30507 +                       unsigned int l;
30508 +
30509 +                       l = calc_size(p.de->de_str.len);
30510 +                       offset += l;
30511 +                       p.deblk += l;
30512 +               }
30513 +               if (!is_deblk_end(&p, &deblk_end)) {
30514 +                       valid = 1;
30515 +                       vdir_cache->vd_last.ul = ul;
30516 +                       vdir_cache->vd_last.p = p;
30517 +                       break;
30518 +               }
30519 +       }
30520 +
30521 +out:
30522 +       /* smp_mb(); */
30523 +       AuTraceErr(!valid);
30524 +       return valid;
30525 +}
30526 +
30527 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
30528 +{
30529 +       unsigned int l, deblk_sz;
30530 +       union au_vdir_deblk_p deblk_end;
30531 +       struct au_vdir *vdir_cache;
30532 +       struct au_vdir_de *de;
30533 +
30534 +       vdir_cache = au_fvdir_cache(file);
30535 +       if (!seek_vdir(file, ctx))
30536 +               return 0;
30537 +
30538 +       deblk_sz = vdir_cache->vd_deblk_sz;
30539 +       while (1) {
30540 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30541 +               deblk_end.deblk += deblk_sz;
30542 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
30543 +                       de = vdir_cache->vd_last.p.de;
30544 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
30545 +                             de->de_str.len, de->de_str.name, ctx->pos,
30546 +                             (unsigned long)de->de_ino, de->de_type);
30547 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
30548 +                                              de->de_str.len, de->de_ino,
30549 +                                              de->de_type))) {
30550 +                               /* todo: ignore the error caused by udba? */
30551 +                               /* return err; */
30552 +                               return 0;
30553 +                       }
30554 +
30555 +                       l = calc_size(de->de_str.len);
30556 +                       vdir_cache->vd_last.p.deblk += l;
30557 +                       ctx->pos += l;
30558 +               }
30559 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30560 +                       vdir_cache->vd_last.ul++;
30561 +                       vdir_cache->vd_last.p.deblk
30562 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30563 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
30564 +                       continue;
30565 +               }
30566 +               break;
30567 +       }
30568 +
30569 +       /* smp_mb(); */
30570 +       return 0;
30571 +}
30572 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30573 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
30574 +++ linux/fs/aufs/vfsub.c       2017-07-29 12:14:25.906375514 +0200
30575 @@ -0,0 +1,900 @@
30576 +/*
30577 + * Copyright (C) 2005-2017 Junjiro R. Okajima
30578 + *
30579 + * This program, aufs is free software; you can redistribute it and/or modify
30580 + * it under the terms of the GNU General Public License as published by
30581 + * the Free Software Foundation; either version 2 of the License, or
30582 + * (at your option) any later version.
30583 + *
30584 + * This program is distributed in the hope that it will be useful,
30585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30587 + * GNU General Public License for more details.
30588 + *
30589 + * You should have received a copy of the GNU General Public License
30590 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30591 + */
30592 +
30593 +/*
30594 + * sub-routines for VFS
30595 + */
30596 +
30597 +#include <linux/namei.h>
30598 +#include <linux/nsproxy.h>
30599 +#include <linux/security.h>
30600 +#include <linux/splice.h>
30601 +#ifdef CONFIG_AUFS_BR_FUSE
30602 +#include "../fs/mount.h"
30603 +#endif
30604 +#include "aufs.h"
30605 +
30606 +#ifdef CONFIG_AUFS_BR_FUSE
30607 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
30608 +{
30609 +       struct nsproxy *ns;
30610 +
30611 +       if (!au_test_fuse(h_sb) || !au_userns)
30612 +               return 0;
30613 +
30614 +       ns = current->nsproxy;
30615 +       /* no {get,put}_nsproxy(ns) */
30616 +       return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
30617 +}
30618 +#endif
30619 +
30620 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
30621 +{
30622 +       int err;
30623 +
30624 +       lockdep_off();
30625 +       down_read(&h_sb->s_umount);
30626 +       err = __sync_filesystem(h_sb, wait);
30627 +       up_read(&h_sb->s_umount);
30628 +       lockdep_on();
30629 +
30630 +       return err;
30631 +}
30632 +
30633 +/* ---------------------------------------------------------------------- */
30634 +
30635 +int vfsub_update_h_iattr(struct path *h_path, int *did)
30636 +{
30637 +       int err;
30638 +       struct kstat st;
30639 +       struct super_block *h_sb;
30640 +
30641 +       /* for remote fs, leave work for its getattr or d_revalidate */
30642 +       /* for bad i_attr fs, handle them in aufs_getattr() */
30643 +       /* still some fs may acquire i_mutex. we need to skip them */
30644 +       err = 0;
30645 +       if (!did)
30646 +               did = &err;
30647 +       h_sb = h_path->dentry->d_sb;
30648 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30649 +       if (*did)
30650 +               err = vfsub_getattr(h_path, &st);
30651 +
30652 +       return err;
30653 +}
30654 +
30655 +/* ---------------------------------------------------------------------- */
30656 +
30657 +struct file *vfsub_dentry_open(struct path *path, int flags)
30658 +{
30659 +       struct file *file;
30660 +
30661 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
30662 +                          current_cred());
30663 +       if (!IS_ERR_OR_NULL(file)
30664 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
30665 +               i_readcount_inc(d_inode(path->dentry));
30666 +
30667 +       return file;
30668 +}
30669 +
30670 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30671 +{
30672 +       struct file *file;
30673 +
30674 +       lockdep_off();
30675 +       file = filp_open(path,
30676 +                        oflags /* | __FMODE_NONOTIFY */,
30677 +                        mode);
30678 +       lockdep_on();
30679 +       if (IS_ERR(file))
30680 +               goto out;
30681 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30682 +
30683 +out:
30684 +       return file;
30685 +}
30686 +
30687 +/*
30688 + * Ideally this function should call VFS:do_last() in order to keep all its
30689 + * checkings. But it is very hard for aufs to regenerate several VFS internal
30690 + * structure such as nameidata. This is a second (or third) best approach.
30691 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30692 + */
30693 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30694 +                     struct vfsub_aopen_args *args, struct au_branch *br)
30695 +{
30696 +       int err;
30697 +       struct file *file = args->file;
30698 +       /* copied from linux/fs/namei.c:atomic_open() */
30699 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30700 +
30701 +       IMustLock(dir);
30702 +       AuDebugOn(!dir->i_op->atomic_open);
30703 +
30704 +       err = au_br_test_oflag(args->open_flag, br);
30705 +       if (unlikely(err))
30706 +               goto out;
30707 +
30708 +       args->file->f_path.dentry = DENTRY_NOT_SET;
30709 +       args->file->f_path.mnt = au_br_mnt(br);
30710 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30711 +                                    args->create_mode, args->opened);
30712 +       if (err >= 0) {
30713 +               /* some filesystems don't set FILE_CREATED while succeeded? */
30714 +               if (*args->opened & FILE_CREATED)
30715 +                       fsnotify_create(dir, dentry);
30716 +       } else
30717 +               goto out;
30718 +
30719 +
30720 +       if (!err) {
30721 +               /* todo: call VFS:may_open() here */
30722 +               err = open_check_o_direct(file);
30723 +               /* todo: ima_file_check() too? */
30724 +               if (!err && (args->open_flag & __FMODE_EXEC))
30725 +                       err = deny_write_access(file);
30726 +               if (unlikely(err))
30727 +                       /* note that the file is created and still opened */
30728 +                       goto out;
30729 +       }
30730 +
30731 +       au_br_get(br);
30732 +       fsnotify_open(file);
30733 +
30734 +out:
30735 +       return err;
30736 +}
30737 +
30738 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30739 +{
30740 +       int err;
30741 +
30742 +       err = kern_path(name, flags, path);
30743 +       if (!err && d_is_positive(path->dentry))
30744 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30745 +       return err;
30746 +}
30747 +
30748 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
30749 +                                            struct dentry *parent, int len)
30750 +{
30751 +       struct path path = {
30752 +               .mnt = NULL
30753 +       };
30754 +
30755 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
30756 +       if (IS_ERR(path.dentry))
30757 +               goto out;
30758 +       if (d_is_positive(path.dentry))
30759 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30760 +
30761 +out:
30762 +       AuTraceErrPtr(path.dentry);
30763 +       return path.dentry;
30764 +}
30765 +
30766 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30767 +                                   int len)
30768 +{
30769 +       struct path path = {
30770 +               .mnt = NULL
30771 +       };
30772 +
30773 +       /* VFS checks it too, but by WARN_ON_ONCE() */
30774 +       IMustLock(d_inode(parent));
30775 +
30776 +       path.dentry = lookup_one_len(name, parent, len);
30777 +       if (IS_ERR(path.dentry))
30778 +               goto out;
30779 +       if (d_is_positive(path.dentry))
30780 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30781 +
30782 +out:
30783 +       AuTraceErrPtr(path.dentry);
30784 +       return path.dentry;
30785 +}
30786 +
30787 +void vfsub_call_lkup_one(void *args)
30788 +{
30789 +       struct vfsub_lkup_one_args *a = args;
30790 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30791 +}
30792 +
30793 +/* ---------------------------------------------------------------------- */
30794 +
30795 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30796 +                                struct dentry *d2, struct au_hinode *hdir2)
30797 +{
30798 +       struct dentry *d;
30799 +
30800 +       lockdep_off();
30801 +       d = lock_rename(d1, d2);
30802 +       lockdep_on();
30803 +       au_hn_suspend(hdir1);
30804 +       if (hdir1 != hdir2)
30805 +               au_hn_suspend(hdir2);
30806 +
30807 +       return d;
30808 +}
30809 +
30810 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30811 +                        struct dentry *d2, struct au_hinode *hdir2)
30812 +{
30813 +       au_hn_resume(hdir1);
30814 +       if (hdir1 != hdir2)
30815 +               au_hn_resume(hdir2);
30816 +       lockdep_off();
30817 +       unlock_rename(d1, d2);
30818 +       lockdep_on();
30819 +}
30820 +
30821 +/* ---------------------------------------------------------------------- */
30822 +
30823 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30824 +{
30825 +       int err;
30826 +       struct dentry *d;
30827 +
30828 +       IMustLock(dir);
30829 +
30830 +       d = path->dentry;
30831 +       path->dentry = d->d_parent;
30832 +       err = security_path_mknod(path, d, mode, 0);
30833 +       path->dentry = d;
30834 +       if (unlikely(err))
30835 +               goto out;
30836 +
30837 +       lockdep_off();
30838 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30839 +       lockdep_on();
30840 +       if (!err) {
30841 +               struct path tmp = *path;
30842 +               int did;
30843 +
30844 +               vfsub_update_h_iattr(&tmp, &did);
30845 +               if (did) {
30846 +                       tmp.dentry = path->dentry->d_parent;
30847 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30848 +               }
30849 +               /*ignore*/
30850 +       }
30851 +
30852 +out:
30853 +       return err;
30854 +}
30855 +
30856 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30857 +{
30858 +       int err;
30859 +       struct dentry *d;
30860 +
30861 +       IMustLock(dir);
30862 +
30863 +       d = path->dentry;
30864 +       path->dentry = d->d_parent;
30865 +       err = security_path_symlink(path, d, symname);
30866 +       path->dentry = d;
30867 +       if (unlikely(err))
30868 +               goto out;
30869 +
30870 +       lockdep_off();
30871 +       err = vfs_symlink(dir, path->dentry, symname);
30872 +       lockdep_on();
30873 +       if (!err) {
30874 +               struct path tmp = *path;
30875 +               int did;
30876 +
30877 +               vfsub_update_h_iattr(&tmp, &did);
30878 +               if (did) {
30879 +                       tmp.dentry = path->dentry->d_parent;
30880 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30881 +               }
30882 +               /*ignore*/
30883 +       }
30884 +
30885 +out:
30886 +       return err;
30887 +}
30888 +
30889 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30890 +{
30891 +       int err;
30892 +       struct dentry *d;
30893 +
30894 +       IMustLock(dir);
30895 +
30896 +       d = path->dentry;
30897 +       path->dentry = d->d_parent;
30898 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30899 +       path->dentry = d;
30900 +       if (unlikely(err))
30901 +               goto out;
30902 +
30903 +       lockdep_off();
30904 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30905 +       lockdep_on();
30906 +       if (!err) {
30907 +               struct path tmp = *path;
30908 +               int did;
30909 +
30910 +               vfsub_update_h_iattr(&tmp, &did);
30911 +               if (did) {
30912 +                       tmp.dentry = path->dentry->d_parent;
30913 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30914 +               }
30915 +               /*ignore*/
30916 +       }
30917 +
30918 +out:
30919 +       return err;
30920 +}
30921 +
30922 +static int au_test_nlink(struct inode *inode)
30923 +{
30924 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30925 +
30926 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30927 +           || inode->i_nlink < link_max)
30928 +               return 0;
30929 +       return -EMLINK;
30930 +}
30931 +
30932 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30933 +              struct inode **delegated_inode)
30934 +{
30935 +       int err;
30936 +       struct dentry *d;
30937 +
30938 +       IMustLock(dir);
30939 +
30940 +       err = au_test_nlink(d_inode(src_dentry));
30941 +       if (unlikely(err))
30942 +               return err;
30943 +
30944 +       /* we don't call may_linkat() */
30945 +       d = path->dentry;
30946 +       path->dentry = d->d_parent;
30947 +       err = security_path_link(src_dentry, path, d);
30948 +       path->dentry = d;
30949 +       if (unlikely(err))
30950 +               goto out;
30951 +
30952 +       lockdep_off();
30953 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30954 +       lockdep_on();
30955 +       if (!err) {
30956 +               struct path tmp = *path;
30957 +               int did;
30958 +
30959 +               /* fuse has different memory inode for the same inumber */
30960 +               vfsub_update_h_iattr(&tmp, &did);
30961 +               if (did) {
30962 +                       tmp.dentry = path->dentry->d_parent;
30963 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30964 +                       tmp.dentry = src_dentry;
30965 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30966 +               }
30967 +               /*ignore*/
30968 +       }
30969 +
30970 +out:
30971 +       return err;
30972 +}
30973 +
30974 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30975 +                struct inode *dir, struct path *path,
30976 +                struct inode **delegated_inode, unsigned int flags)
30977 +{
30978 +       int err;
30979 +       struct path tmp = {
30980 +               .mnt    = path->mnt
30981 +       };
30982 +       struct dentry *d;
30983 +
30984 +       IMustLock(dir);
30985 +       IMustLock(src_dir);
30986 +
30987 +       d = path->dentry;
30988 +       path->dentry = d->d_parent;
30989 +       tmp.dentry = src_dentry->d_parent;
30990 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30991 +       path->dentry = d;
30992 +       if (unlikely(err))
30993 +               goto out;
30994 +
30995 +       lockdep_off();
30996 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30997 +                        delegated_inode, flags);
30998 +       lockdep_on();
30999 +       if (!err) {
31000 +               int did;
31001 +
31002 +               tmp.dentry = d->d_parent;
31003 +               vfsub_update_h_iattr(&tmp, &did);
31004 +               if (did) {
31005 +                       tmp.dentry = src_dentry;
31006 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
31007 +                       tmp.dentry = src_dentry->d_parent;
31008 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
31009 +               }
31010 +               /*ignore*/
31011 +       }
31012 +
31013 +out:
31014 +       return err;
31015 +}
31016 +
31017 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
31018 +{
31019 +       int err;
31020 +       struct dentry *d;
31021 +
31022 +       IMustLock(dir);
31023 +
31024 +       d = path->dentry;
31025 +       path->dentry = d->d_parent;
31026 +       err = security_path_mkdir(path, d, mode);
31027 +       path->dentry = d;
31028 +       if (unlikely(err))
31029 +               goto out;
31030 +
31031 +       lockdep_off();
31032 +       err = vfs_mkdir(dir, path->dentry, mode);
31033 +       lockdep_on();
31034 +       if (!err) {
31035 +               struct path tmp = *path;
31036 +               int did;
31037 +
31038 +               vfsub_update_h_iattr(&tmp, &did);
31039 +               if (did) {
31040 +                       tmp.dentry = path->dentry->d_parent;
31041 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
31042 +               }
31043 +               /*ignore*/
31044 +       }
31045 +
31046 +out:
31047 +       return err;
31048 +}
31049 +
31050 +int vfsub_rmdir(struct inode *dir, struct path *path)
31051 +{
31052 +       int err;
31053 +       struct dentry *d;
31054 +
31055 +       IMustLock(dir);
31056 +
31057 +       d = path->dentry;
31058 +       path->dentry = d->d_parent;
31059 +       err = security_path_rmdir(path, d);
31060 +       path->dentry = d;
31061 +       if (unlikely(err))
31062 +               goto out;
31063 +
31064 +       lockdep_off();
31065 +       err = vfs_rmdir(dir, path->dentry);
31066 +       lockdep_on();
31067 +       if (!err) {
31068 +               struct path tmp = {
31069 +                       .dentry = path->dentry->d_parent,
31070 +                       .mnt    = path->mnt
31071 +               };
31072 +
31073 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31074 +       }
31075 +
31076 +out:
31077 +       return err;
31078 +}
31079 +
31080 +/* ---------------------------------------------------------------------- */
31081 +
31082 +/* todo: support mmap_sem? */
31083 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31084 +                    loff_t *ppos)
31085 +{
31086 +       ssize_t err;
31087 +
31088 +       lockdep_off();
31089 +       err = vfs_read(file, ubuf, count, ppos);
31090 +       lockdep_on();
31091 +       if (err >= 0)
31092 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31093 +       return err;
31094 +}
31095 +
31096 +/* todo: kernel_read()? */
31097 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31098 +                    loff_t *ppos)
31099 +{
31100 +       ssize_t err;
31101 +       mm_segment_t oldfs;
31102 +       union {
31103 +               void *k;
31104 +               char __user *u;
31105 +       } buf;
31106 +
31107 +       buf.k = kbuf;
31108 +       oldfs = get_fs();
31109 +       set_fs(KERNEL_DS);
31110 +       err = vfsub_read_u(file, buf.u, count, ppos);
31111 +       set_fs(oldfs);
31112 +       return err;
31113 +}
31114 +
31115 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31116 +                     loff_t *ppos)
31117 +{
31118 +       ssize_t err;
31119 +
31120 +       lockdep_off();
31121 +       err = vfs_write(file, ubuf, count, ppos);
31122 +       lockdep_on();
31123 +       if (err >= 0)
31124 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31125 +       return err;
31126 +}
31127 +
31128 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
31129 +{
31130 +       ssize_t err;
31131 +       mm_segment_t oldfs;
31132 +       union {
31133 +               void *k;
31134 +               const char __user *u;
31135 +       } buf;
31136 +
31137 +       buf.k = kbuf;
31138 +       oldfs = get_fs();
31139 +       set_fs(KERNEL_DS);
31140 +       err = vfsub_write_u(file, buf.u, count, ppos);
31141 +       set_fs(oldfs);
31142 +       return err;
31143 +}
31144 +
31145 +int vfsub_flush(struct file *file, fl_owner_t id)
31146 +{
31147 +       int err;
31148 +
31149 +       err = 0;
31150 +       if (file->f_op->flush) {
31151 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
31152 +                       err = file->f_op->flush(file, id);
31153 +               else {
31154 +                       lockdep_off();
31155 +                       err = file->f_op->flush(file, id);
31156 +                       lockdep_on();
31157 +               }
31158 +               if (!err)
31159 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
31160 +               /*ignore*/
31161 +       }
31162 +       return err;
31163 +}
31164 +
31165 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
31166 +{
31167 +       int err;
31168 +
31169 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
31170 +
31171 +       lockdep_off();
31172 +       err = iterate_dir(file, ctx);
31173 +       lockdep_on();
31174 +       if (err >= 0)
31175 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31176 +
31177 +       return err;
31178 +}
31179 +
31180 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31181 +                    struct pipe_inode_info *pipe, size_t len,
31182 +                    unsigned int flags)
31183 +{
31184 +       long err;
31185 +
31186 +       lockdep_off();
31187 +       err = do_splice_to(in, ppos, pipe, len, flags);
31188 +       lockdep_on();
31189 +       file_accessed(in);
31190 +       if (err >= 0)
31191 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
31192 +       return err;
31193 +}
31194 +
31195 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31196 +                      loff_t *ppos, size_t len, unsigned int flags)
31197 +{
31198 +       long err;
31199 +
31200 +       lockdep_off();
31201 +       err = do_splice_from(pipe, out, ppos, len, flags);
31202 +       lockdep_on();
31203 +       if (err >= 0)
31204 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
31205 +       return err;
31206 +}
31207 +
31208 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
31209 +{
31210 +       int err;
31211 +
31212 +       /* file can be NULL */
31213 +       lockdep_off();
31214 +       err = vfs_fsync(file, datasync);
31215 +       lockdep_on();
31216 +       if (!err) {
31217 +               if (!path) {
31218 +                       AuDebugOn(!file);
31219 +                       path = &file->f_path;
31220 +               }
31221 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
31222 +       }
31223 +       return err;
31224 +}
31225 +
31226 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
31227 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31228 +               struct file *h_file)
31229 +{
31230 +       int err;
31231 +       struct inode *h_inode;
31232 +       struct super_block *h_sb;
31233 +
31234 +       if (!h_file) {
31235 +               err = vfsub_truncate(h_path, length);
31236 +               goto out;
31237 +       }
31238 +
31239 +       h_inode = d_inode(h_path->dentry);
31240 +       h_sb = h_inode->i_sb;
31241 +       lockdep_off();
31242 +       sb_start_write(h_sb);
31243 +       lockdep_on();
31244 +       err = locks_verify_truncate(h_inode, h_file, length);
31245 +       if (!err)
31246 +               err = security_path_truncate(h_path);
31247 +       if (!err) {
31248 +               lockdep_off();
31249 +               err = do_truncate(h_path->dentry, length, attr, h_file);
31250 +               lockdep_on();
31251 +       }
31252 +       lockdep_off();
31253 +       sb_end_write(h_sb);
31254 +       lockdep_on();
31255 +
31256 +out:
31257 +       return err;
31258 +}
31259 +
31260 +/* ---------------------------------------------------------------------- */
31261 +
31262 +struct au_vfsub_mkdir_args {
31263 +       int *errp;
31264 +       struct inode *dir;
31265 +       struct path *path;
31266 +       int mode;
31267 +};
31268 +
31269 +static void au_call_vfsub_mkdir(void *args)
31270 +{
31271 +       struct au_vfsub_mkdir_args *a = args;
31272 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
31273 +}
31274 +
31275 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
31276 +{
31277 +       int err, do_sio, wkq_err;
31278 +
31279 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
31280 +       if (!do_sio) {
31281 +               lockdep_off();
31282 +               err = vfsub_mkdir(dir, path, mode);
31283 +               lockdep_on();
31284 +       } else {
31285 +               struct au_vfsub_mkdir_args args = {
31286 +                       .errp   = &err,
31287 +                       .dir    = dir,
31288 +                       .path   = path,
31289 +                       .mode   = mode
31290 +               };
31291 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
31292 +               if (unlikely(wkq_err))
31293 +                       err = wkq_err;
31294 +       }
31295 +
31296 +       return err;
31297 +}
31298 +
31299 +struct au_vfsub_rmdir_args {
31300 +       int *errp;
31301 +       struct inode *dir;
31302 +       struct path *path;
31303 +};
31304 +
31305 +static void au_call_vfsub_rmdir(void *args)
31306 +{
31307 +       struct au_vfsub_rmdir_args *a = args;
31308 +       *a->errp = vfsub_rmdir(a->dir, a->path);
31309 +}
31310 +
31311 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
31312 +{
31313 +       int err, do_sio, wkq_err;
31314 +
31315 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
31316 +       if (!do_sio) {
31317 +               lockdep_off();
31318 +               err = vfsub_rmdir(dir, path);
31319 +               lockdep_on();
31320 +       } else {
31321 +               struct au_vfsub_rmdir_args args = {
31322 +                       .errp   = &err,
31323 +                       .dir    = dir,
31324 +                       .path   = path
31325 +               };
31326 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
31327 +               if (unlikely(wkq_err))
31328 +                       err = wkq_err;
31329 +       }
31330 +
31331 +       return err;
31332 +}
31333 +
31334 +/* ---------------------------------------------------------------------- */
31335 +
31336 +struct notify_change_args {
31337 +       int *errp;
31338 +       struct path *path;
31339 +       struct iattr *ia;
31340 +       struct inode **delegated_inode;
31341 +};
31342 +
31343 +static void call_notify_change(void *args)
31344 +{
31345 +       struct notify_change_args *a = args;
31346 +       struct inode *h_inode;
31347 +
31348 +       h_inode = d_inode(a->path->dentry);
31349 +       IMustLock(h_inode);
31350 +
31351 +       *a->errp = -EPERM;
31352 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
31353 +               lockdep_off();
31354 +               *a->errp = notify_change(a->path->dentry, a->ia,
31355 +                                        a->delegated_inode);
31356 +               lockdep_on();
31357 +               if (!*a->errp)
31358 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
31359 +       }
31360 +       AuTraceErr(*a->errp);
31361 +}
31362 +
31363 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31364 +                       struct inode **delegated_inode)
31365 +{
31366 +       int err;
31367 +       struct notify_change_args args = {
31368 +               .errp                   = &err,
31369 +               .path                   = path,
31370 +               .ia                     = ia,
31371 +               .delegated_inode        = delegated_inode
31372 +       };
31373 +
31374 +       call_notify_change(&args);
31375 +
31376 +       return err;
31377 +}
31378 +
31379 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31380 +                           struct inode **delegated_inode)
31381 +{
31382 +       int err, wkq_err;
31383 +       struct notify_change_args args = {
31384 +               .errp                   = &err,
31385 +               .path                   = path,
31386 +               .ia                     = ia,
31387 +               .delegated_inode        = delegated_inode
31388 +       };
31389 +
31390 +       wkq_err = au_wkq_wait(call_notify_change, &args);
31391 +       if (unlikely(wkq_err))
31392 +               err = wkq_err;
31393 +
31394 +       return err;
31395 +}
31396 +
31397 +/* ---------------------------------------------------------------------- */
31398 +
31399 +struct unlink_args {
31400 +       int *errp;
31401 +       struct inode *dir;
31402 +       struct path *path;
31403 +       struct inode **delegated_inode;
31404 +};
31405 +
31406 +static void call_unlink(void *args)
31407 +{
31408 +       struct unlink_args *a = args;
31409 +       struct dentry *d = a->path->dentry;
31410 +       struct inode *h_inode;
31411 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
31412 +                                     && au_dcount(d) == 1);
31413 +
31414 +       IMustLock(a->dir);
31415 +
31416 +       a->path->dentry = d->d_parent;
31417 +       *a->errp = security_path_unlink(a->path, d);
31418 +       a->path->dentry = d;
31419 +       if (unlikely(*a->errp))
31420 +               return;
31421 +
31422 +       if (!stop_sillyrename)
31423 +               dget(d);
31424 +       h_inode = NULL;
31425 +       if (d_is_positive(d)) {
31426 +               h_inode = d_inode(d);
31427 +               ihold(h_inode);
31428 +       }
31429 +
31430 +       lockdep_off();
31431 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
31432 +       lockdep_on();
31433 +       if (!*a->errp) {
31434 +               struct path tmp = {
31435 +                       .dentry = d->d_parent,
31436 +                       .mnt    = a->path->mnt
31437 +               };
31438 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31439 +       }
31440 +
31441 +       if (!stop_sillyrename)
31442 +               dput(d);
31443 +       if (h_inode)
31444 +               iput(h_inode);
31445 +
31446 +       AuTraceErr(*a->errp);
31447 +}
31448 +
31449 +/*
31450 + * @dir: must be locked.
31451 + * @dentry: target dentry.
31452 + */
31453 +int vfsub_unlink(struct inode *dir, struct path *path,
31454 +                struct inode **delegated_inode, int force)
31455 +{
31456 +       int err;
31457 +       struct unlink_args args = {
31458 +               .errp                   = &err,
31459 +               .dir                    = dir,
31460 +               .path                   = path,
31461 +               .delegated_inode        = delegated_inode
31462 +       };
31463 +
31464 +       if (!force)
31465 +               call_unlink(&args);
31466 +       else {
31467 +               int wkq_err;
31468 +
31469 +               wkq_err = au_wkq_wait(call_unlink, &args);
31470 +               if (unlikely(wkq_err))
31471 +                       err = wkq_err;
31472 +       }
31473 +
31474 +       return err;
31475 +}
31476 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
31477 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
31478 +++ linux/fs/aufs/vfsub.h       2017-07-29 12:14:25.906375514 +0200
31479 @@ -0,0 +1,353 @@
31480 +/*
31481 + * Copyright (C) 2005-2017 Junjiro R. Okajima
31482 + *
31483 + * This program, aufs is free software; you can redistribute it and/or modify
31484 + * it under the terms of the GNU General Public License as published by
31485 + * the Free Software Foundation; either version 2 of the License, or
31486 + * (at your option) any later version.
31487 + *
31488 + * This program is distributed in the hope that it will be useful,
31489 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31490 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31491 + * GNU General Public License for more details.
31492 + *
31493 + * You should have received a copy of the GNU General Public License
31494 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31495 + */
31496 +
31497 +/*
31498 + * sub-routines for VFS
31499 + */
31500 +
31501 +#ifndef __AUFS_VFSUB_H__
31502 +#define __AUFS_VFSUB_H__
31503 +
31504 +#ifdef __KERNEL__
31505 +
31506 +#include <linux/fs.h>
31507 +#include <linux/mount.h>
31508 +#include <linux/posix_acl.h>
31509 +#include <linux/xattr.h>
31510 +#include "debug.h"
31511 +
31512 +/* copied from linux/fs/internal.h */
31513 +/* todo: BAD approach!! */
31514 +extern void __mnt_drop_write(struct vfsmount *);
31515 +extern int open_check_o_direct(struct file *f);
31516 +
31517 +/* ---------------------------------------------------------------------- */
31518 +
31519 +/* lock subclass for lower inode */
31520 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
31521 +/* reduce? gave up. */
31522 +enum {
31523 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
31524 +       AuLsc_I_PARENT,         /* lower inode, parent first */
31525 +       AuLsc_I_PARENT2,        /* copyup dirs */
31526 +       AuLsc_I_PARENT3,        /* copyup wh */
31527 +       AuLsc_I_CHILD,
31528 +       AuLsc_I_CHILD2,
31529 +       AuLsc_I_End
31530 +};
31531 +
31532 +/* to debug easier, do not make them inlined functions */
31533 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
31534 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
31535 +
31536 +/* ---------------------------------------------------------------------- */
31537 +
31538 +static inline void vfsub_drop_nlink(struct inode *inode)
31539 +{
31540 +       AuDebugOn(!inode->i_nlink);
31541 +       drop_nlink(inode);
31542 +}
31543 +
31544 +static inline void vfsub_dead_dir(struct inode *inode)
31545 +{
31546 +       AuDebugOn(!S_ISDIR(inode->i_mode));
31547 +       inode->i_flags |= S_DEAD;
31548 +       clear_nlink(inode);
31549 +}
31550 +
31551 +static inline int vfsub_native_ro(struct inode *inode)
31552 +{
31553 +       return (inode->i_sb->s_flags & MS_RDONLY)
31554 +               || IS_RDONLY(inode)
31555 +               /* || IS_APPEND(inode) */
31556 +               || IS_IMMUTABLE(inode);
31557 +}
31558 +
31559 +#ifdef CONFIG_AUFS_BR_FUSE
31560 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
31561 +#else
31562 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
31563 +#endif
31564 +
31565 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
31566 +
31567 +/* ---------------------------------------------------------------------- */
31568 +
31569 +int vfsub_update_h_iattr(struct path *h_path, int *did);
31570 +struct file *vfsub_dentry_open(struct path *path, int flags);
31571 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
31572 +struct vfsub_aopen_args {
31573 +       struct file     *file;
31574 +       unsigned int    open_flag;
31575 +       umode_t         create_mode;
31576 +       int             *opened;
31577 +};
31578 +struct au_branch;
31579 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
31580 +                     struct vfsub_aopen_args *args, struct au_branch *br);
31581 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
31582 +
31583 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
31584 +                                            struct dentry *parent, int len);
31585 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
31586 +                                   int len);
31587 +
31588 +struct vfsub_lkup_one_args {
31589 +       struct dentry **errp;
31590 +       struct qstr *name;
31591 +       struct dentry *parent;
31592 +};
31593 +
31594 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
31595 +                                           struct dentry *parent)
31596 +{
31597 +       return vfsub_lookup_one_len(name->name, parent, name->len);
31598 +}
31599 +
31600 +void vfsub_call_lkup_one(void *args);
31601 +
31602 +/* ---------------------------------------------------------------------- */
31603 +
31604 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
31605 +{
31606 +       int err;
31607 +
31608 +       lockdep_off();
31609 +       err = mnt_want_write(mnt);
31610 +       lockdep_on();
31611 +       return err;
31612 +}
31613 +
31614 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31615 +{
31616 +       lockdep_off();
31617 +       mnt_drop_write(mnt);
31618 +       lockdep_on();
31619 +}
31620 +
31621 +#if 0 /* reserved */
31622 +static inline void vfsub_mnt_drop_write_file(struct file *file)
31623 +{
31624 +       lockdep_off();
31625 +       mnt_drop_write_file(file);
31626 +       lockdep_on();
31627 +}
31628 +#endif
31629 +
31630 +/* ---------------------------------------------------------------------- */
31631 +
31632 +struct au_hinode;
31633 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31634 +                                struct dentry *d2, struct au_hinode *hdir2);
31635 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31636 +                        struct dentry *d2, struct au_hinode *hdir2);
31637 +
31638 +int vfsub_create(struct inode *dir, struct path *path, int mode,
31639 +                bool want_excl);
31640 +int vfsub_symlink(struct inode *dir, struct path *path,
31641 +                 const char *symname);
31642 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31643 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
31644 +              struct path *path, struct inode **delegated_inode);
31645 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
31646 +                struct inode *hdir, struct path *path,
31647 +                struct inode **delegated_inode, unsigned int flags);
31648 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31649 +int vfsub_rmdir(struct inode *dir, struct path *path);
31650 +
31651 +/* ---------------------------------------------------------------------- */
31652 +
31653 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31654 +                    loff_t *ppos);
31655 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31656 +                       loff_t *ppos);
31657 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31658 +                     loff_t *ppos);
31659 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31660 +                     loff_t *ppos);
31661 +int vfsub_flush(struct file *file, fl_owner_t id);
31662 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31663 +
31664 +static inline loff_t vfsub_f_size_read(struct file *file)
31665 +{
31666 +       return i_size_read(file_inode(file));
31667 +}
31668 +
31669 +static inline unsigned int vfsub_file_flags(struct file *file)
31670 +{
31671 +       unsigned int flags;
31672 +
31673 +       spin_lock(&file->f_lock);
31674 +       flags = file->f_flags;
31675 +       spin_unlock(&file->f_lock);
31676 +
31677 +       return flags;
31678 +}
31679 +
31680 +static inline int vfsub_file_execed(struct file *file)
31681 +{
31682 +       /* todo: direct access f_flags */
31683 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
31684 +}
31685 +
31686 +#if 0 /* reserved */
31687 +static inline void vfsub_file_accessed(struct file *h_file)
31688 +{
31689 +       file_accessed(h_file);
31690 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31691 +}
31692 +#endif
31693 +
31694 +#if 0 /* reserved */
31695 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31696 +                                    struct dentry *h_dentry)
31697 +{
31698 +       struct path h_path = {
31699 +               .dentry = h_dentry,
31700 +               .mnt    = h_mnt
31701 +       };
31702 +       touch_atime(&h_path);
31703 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31704 +}
31705 +#endif
31706 +
31707 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31708 +                                   int flags)
31709 +{
31710 +       return update_time(h_inode, ts, flags);
31711 +       /* no vfsub_update_h_iattr() since we don't have struct path */
31712 +}
31713 +
31714 +#ifdef CONFIG_FS_POSIX_ACL
31715 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
31716 +{
31717 +       int err;
31718 +
31719 +       err = posix_acl_chmod(h_inode, h_mode);
31720 +       if (err == -EOPNOTSUPP)
31721 +               err = 0;
31722 +       return err;
31723 +}
31724 +#else
31725 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
31726 +#endif
31727 +
31728 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31729 +                    struct pipe_inode_info *pipe, size_t len,
31730 +                    unsigned int flags);
31731 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31732 +                      loff_t *ppos, size_t len, unsigned int flags);
31733 +
31734 +static inline long vfsub_truncate(struct path *path, loff_t length)
31735 +{
31736 +       long err;
31737 +
31738 +       lockdep_off();
31739 +       err = vfs_truncate(path, length);
31740 +       lockdep_on();
31741 +       return err;
31742 +}
31743 +
31744 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31745 +               struct file *h_file);
31746 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
31747 +
31748 +/*
31749 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
31750 + * ioctl.
31751 + */
31752 +static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
31753 +                                        u64 len)
31754 +{
31755 +       int err;
31756 +
31757 +       lockdep_off();
31758 +       err = vfs_clone_file_range(src, 0, dst, 0, len);
31759 +       lockdep_on();
31760 +
31761 +       return err;
31762 +}
31763 +
31764 +/* copy_file_range(2) is a systemcall */
31765 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
31766 +                                           struct file *dst, loff_t dst_pos,
31767 +                                           size_t len, unsigned int flags)
31768 +{
31769 +       ssize_t ssz;
31770 +
31771 +       lockdep_off();
31772 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
31773 +       lockdep_on();
31774 +
31775 +       return ssz;
31776 +}
31777 +
31778 +/* ---------------------------------------------------------------------- */
31779 +
31780 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31781 +{
31782 +       loff_t err;
31783 +
31784 +       lockdep_off();
31785 +       err = vfs_llseek(file, offset, origin);
31786 +       lockdep_on();
31787 +       return err;
31788 +}
31789 +
31790 +/* ---------------------------------------------------------------------- */
31791 +
31792 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31793 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
31794 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31795 +                           struct inode **delegated_inode);
31796 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31797 +                       struct inode **delegated_inode);
31798 +int vfsub_unlink(struct inode *dir, struct path *path,
31799 +                struct inode **delegated_inode, int force);
31800 +
31801 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
31802 +{
31803 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
31804 +}
31805 +
31806 +/* ---------------------------------------------------------------------- */
31807 +
31808 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31809 +                                const void *value, size_t size, int flags)
31810 +{
31811 +       int err;
31812 +
31813 +       lockdep_off();
31814 +       err = vfs_setxattr(dentry, name, value, size, flags);
31815 +       lockdep_on();
31816 +
31817 +       return err;
31818 +}
31819 +
31820 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31821 +{
31822 +       int err;
31823 +
31824 +       lockdep_off();
31825 +       err = vfs_removexattr(dentry, name);
31826 +       lockdep_on();
31827 +
31828 +       return err;
31829 +}
31830 +
31831 +#endif /* __KERNEL__ */
31832 +#endif /* __AUFS_VFSUB_H__ */
31833 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31834 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
31835 +++ linux/fs/aufs/wbr_policy.c  2017-07-29 12:14:25.906375514 +0200
31836 @@ -0,0 +1,830 @@
31837 +/*
31838 + * Copyright (C) 2005-2017 Junjiro R. Okajima
31839 + *
31840 + * This program, aufs is free software; you can redistribute it and/or modify
31841 + * it under the terms of the GNU General Public License as published by
31842 + * the Free Software Foundation; either version 2 of the License, or
31843 + * (at your option) any later version.
31844 + *
31845 + * This program is distributed in the hope that it will be useful,
31846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31848 + * GNU General Public License for more details.
31849 + *
31850 + * You should have received a copy of the GNU General Public License
31851 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31852 + */
31853 +
31854 +/*
31855 + * policies for selecting one among multiple writable branches
31856 + */
31857 +
31858 +#include <linux/statfs.h>
31859 +#include "aufs.h"
31860 +
31861 +/* subset of cpup_attr() */
31862 +static noinline_for_stack
31863 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31864 +{
31865 +       int err, sbits;
31866 +       struct iattr ia;
31867 +       struct inode *h_isrc;
31868 +
31869 +       h_isrc = d_inode(h_src);
31870 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31871 +       ia.ia_mode = h_isrc->i_mode;
31872 +       ia.ia_uid = h_isrc->i_uid;
31873 +       ia.ia_gid = h_isrc->i_gid;
31874 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31875 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
31876 +       /* no delegation since it is just created */
31877 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31878 +
31879 +       /* is this nfs only? */
31880 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31881 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31882 +               ia.ia_mode = h_isrc->i_mode;
31883 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31884 +       }
31885 +
31886 +       return err;
31887 +}
31888 +
31889 +#define AuCpdown_PARENT_OPQ    1
31890 +#define AuCpdown_WHED          (1 << 1)
31891 +#define AuCpdown_MADE_DIR      (1 << 2)
31892 +#define AuCpdown_DIROPQ                (1 << 3)
31893 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31894 +#define au_fset_cpdown(flags, name) \
31895 +       do { (flags) |= AuCpdown_##name; } while (0)
31896 +#define au_fclr_cpdown(flags, name) \
31897 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31898 +
31899 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31900 +                            unsigned int *flags)
31901 +{
31902 +       int err;
31903 +       struct dentry *opq_dentry;
31904 +
31905 +       opq_dentry = au_diropq_create(dentry, bdst);
31906 +       err = PTR_ERR(opq_dentry);
31907 +       if (IS_ERR(opq_dentry))
31908 +               goto out;
31909 +       dput(opq_dentry);
31910 +       au_fset_cpdown(*flags, DIROPQ);
31911 +
31912 +out:
31913 +       return err;
31914 +}
31915 +
31916 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31917 +                           struct inode *dir, aufs_bindex_t bdst)
31918 +{
31919 +       int err;
31920 +       struct path h_path;
31921 +       struct au_branch *br;
31922 +
31923 +       br = au_sbr(dentry->d_sb, bdst);
31924 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31925 +       err = PTR_ERR(h_path.dentry);
31926 +       if (IS_ERR(h_path.dentry))
31927 +               goto out;
31928 +
31929 +       err = 0;
31930 +       if (d_is_positive(h_path.dentry)) {
31931 +               h_path.mnt = au_br_mnt(br);
31932 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31933 +                                         dentry);
31934 +       }
31935 +       dput(h_path.dentry);
31936 +
31937 +out:
31938 +       return err;
31939 +}
31940 +
31941 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31942 +                        struct au_pin *pin,
31943 +                        struct dentry *h_parent, void *arg)
31944 +{
31945 +       int err, rerr;
31946 +       aufs_bindex_t bopq, btop;
31947 +       struct path h_path;
31948 +       struct dentry *parent;
31949 +       struct inode *h_dir, *h_inode, *inode, *dir;
31950 +       unsigned int *flags = arg;
31951 +
31952 +       btop = au_dbtop(dentry);
31953 +       /* dentry is di-locked */
31954 +       parent = dget_parent(dentry);
31955 +       dir = d_inode(parent);
31956 +       h_dir = d_inode(h_parent);
31957 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31958 +       IMustLock(h_dir);
31959 +
31960 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31961 +       if (unlikely(err < 0))
31962 +               goto out;
31963 +       h_path.dentry = au_h_dptr(dentry, bdst);
31964 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31965 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31966 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31967 +       if (unlikely(err))
31968 +               goto out_put;
31969 +       au_fset_cpdown(*flags, MADE_DIR);
31970 +
31971 +       bopq = au_dbdiropq(dentry);
31972 +       au_fclr_cpdown(*flags, WHED);
31973 +       au_fclr_cpdown(*flags, DIROPQ);
31974 +       if (au_dbwh(dentry) == bdst)
31975 +               au_fset_cpdown(*flags, WHED);
31976 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31977 +               au_fset_cpdown(*flags, PARENT_OPQ);
31978 +       h_inode = d_inode(h_path.dentry);
31979 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
31980 +       if (au_ftest_cpdown(*flags, WHED)) {
31981 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31982 +               if (unlikely(err)) {
31983 +                       inode_unlock(h_inode);
31984 +                       goto out_dir;
31985 +               }
31986 +       }
31987 +
31988 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
31989 +       inode_unlock(h_inode);
31990 +       if (unlikely(err))
31991 +               goto out_opq;
31992 +
31993 +       if (au_ftest_cpdown(*flags, WHED)) {
31994 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31995 +               if (unlikely(err))
31996 +                       goto out_opq;
31997 +       }
31998 +
31999 +       inode = d_inode(dentry);
32000 +       if (au_ibbot(inode) < bdst)
32001 +               au_set_ibbot(inode, bdst);
32002 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
32003 +                     au_hi_flags(inode, /*isdir*/1));
32004 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
32005 +       goto out; /* success */
32006 +
32007 +       /* revert */
32008 +out_opq:
32009 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
32010 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
32011 +               rerr = au_diropq_remove(dentry, bdst);
32012 +               inode_unlock(h_inode);
32013 +               if (unlikely(rerr)) {
32014 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
32015 +                               dentry, bdst, rerr);
32016 +                       err = -EIO;
32017 +                       goto out;
32018 +               }
32019 +       }
32020 +out_dir:
32021 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
32022 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
32023 +               if (unlikely(rerr)) {
32024 +                       AuIOErr("failed removing %pd b%d (%d)\n",
32025 +                               dentry, bdst, rerr);
32026 +                       err = -EIO;
32027 +               }
32028 +       }
32029 +out_put:
32030 +       au_set_h_dptr(dentry, bdst, NULL);
32031 +       if (au_dbbot(dentry) == bdst)
32032 +               au_update_dbbot(dentry);
32033 +out:
32034 +       dput(parent);
32035 +       return err;
32036 +}
32037 +
32038 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
32039 +{
32040 +       int err;
32041 +       unsigned int flags;
32042 +
32043 +       flags = 0;
32044 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
32045 +
32046 +       return err;
32047 +}
32048 +
32049 +/* ---------------------------------------------------------------------- */
32050 +
32051 +/* policies for create */
32052 +
32053 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
32054 +{
32055 +       int err, i, j, ndentry;
32056 +       aufs_bindex_t bopq;
32057 +       struct au_dcsub_pages dpages;
32058 +       struct au_dpage *dpage;
32059 +       struct dentry **dentries, *parent, *d;
32060 +
32061 +       err = au_dpages_init(&dpages, GFP_NOFS);
32062 +       if (unlikely(err))
32063 +               goto out;
32064 +       parent = dget_parent(dentry);
32065 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
32066 +       if (unlikely(err))
32067 +               goto out_free;
32068 +
32069 +       err = bindex;
32070 +       for (i = 0; i < dpages.ndpage; i++) {
32071 +               dpage = dpages.dpages + i;
32072 +               dentries = dpage->dentries;
32073 +               ndentry = dpage->ndentry;
32074 +               for (j = 0; j < ndentry; j++) {
32075 +                       d = dentries[j];
32076 +                       di_read_lock_parent2(d, !AuLock_IR);
32077 +                       bopq = au_dbdiropq(d);
32078 +                       di_read_unlock(d, !AuLock_IR);
32079 +                       if (bopq >= 0 && bopq < err)
32080 +                               err = bopq;
32081 +               }
32082 +       }
32083 +
32084 +out_free:
32085 +       dput(parent);
32086 +       au_dpages_free(&dpages);
32087 +out:
32088 +       return err;
32089 +}
32090 +
32091 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
32092 +{
32093 +       for (; bindex >= 0; bindex--)
32094 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
32095 +                       return bindex;
32096 +       return -EROFS;
32097 +}
32098 +
32099 +/* top down parent */
32100 +static int au_wbr_create_tdp(struct dentry *dentry,
32101 +                            unsigned int flags __maybe_unused)
32102 +{
32103 +       int err;
32104 +       aufs_bindex_t btop, bindex;
32105 +       struct super_block *sb;
32106 +       struct dentry *parent, *h_parent;
32107 +
32108 +       sb = dentry->d_sb;
32109 +       btop = au_dbtop(dentry);
32110 +       err = btop;
32111 +       if (!au_br_rdonly(au_sbr(sb, btop)))
32112 +               goto out;
32113 +
32114 +       err = -EROFS;
32115 +       parent = dget_parent(dentry);
32116 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
32117 +               h_parent = au_h_dptr(parent, bindex);
32118 +               if (!h_parent || d_is_negative(h_parent))
32119 +                       continue;
32120 +
32121 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
32122 +                       err = bindex;
32123 +                       break;
32124 +               }
32125 +       }
32126 +       dput(parent);
32127 +
32128 +       /* bottom up here */
32129 +       if (unlikely(err < 0)) {
32130 +               err = au_wbr_bu(sb, btop - 1);
32131 +               if (err >= 0)
32132 +                       err = au_wbr_nonopq(dentry, err);
32133 +       }
32134 +
32135 +out:
32136 +       AuDbg("b%d\n", err);
32137 +       return err;
32138 +}
32139 +
32140 +/* ---------------------------------------------------------------------- */
32141 +
32142 +/* an exception for the policy other than tdp */
32143 +static int au_wbr_create_exp(struct dentry *dentry)
32144 +{
32145 +       int err;
32146 +       aufs_bindex_t bwh, bdiropq;
32147 +       struct dentry *parent;
32148 +
32149 +       err = -1;
32150 +       bwh = au_dbwh(dentry);
32151 +       parent = dget_parent(dentry);
32152 +       bdiropq = au_dbdiropq(parent);
32153 +       if (bwh >= 0) {
32154 +               if (bdiropq >= 0)
32155 +                       err = min(bdiropq, bwh);
32156 +               else
32157 +                       err = bwh;
32158 +               AuDbg("%d\n", err);
32159 +       } else if (bdiropq >= 0) {
32160 +               err = bdiropq;
32161 +               AuDbg("%d\n", err);
32162 +       }
32163 +       dput(parent);
32164 +
32165 +       if (err >= 0)
32166 +               err = au_wbr_nonopq(dentry, err);
32167 +
32168 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
32169 +               err = -1;
32170 +
32171 +       AuDbg("%d\n", err);
32172 +       return err;
32173 +}
32174 +
32175 +/* ---------------------------------------------------------------------- */
32176 +
32177 +/* round robin */
32178 +static int au_wbr_create_init_rr(struct super_block *sb)
32179 +{
32180 +       int err;
32181 +
32182 +       err = au_wbr_bu(sb, au_sbbot(sb));
32183 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
32184 +       /* smp_mb(); */
32185 +
32186 +       AuDbg("b%d\n", err);
32187 +       return err;
32188 +}
32189 +
32190 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
32191 +{
32192 +       int err, nbr;
32193 +       unsigned int u;
32194 +       aufs_bindex_t bindex, bbot;
32195 +       struct super_block *sb;
32196 +       atomic_t *next;
32197 +
32198 +       err = au_wbr_create_exp(dentry);
32199 +       if (err >= 0)
32200 +               goto out;
32201 +
32202 +       sb = dentry->d_sb;
32203 +       next = &au_sbi(sb)->si_wbr_rr_next;
32204 +       bbot = au_sbbot(sb);
32205 +       nbr = bbot + 1;
32206 +       for (bindex = 0; bindex <= bbot; bindex++) {
32207 +               if (!au_ftest_wbr(flags, DIR)) {
32208 +                       err = atomic_dec_return(next) + 1;
32209 +                       /* modulo for 0 is meaningless */
32210 +                       if (unlikely(!err))
32211 +                               err = atomic_dec_return(next) + 1;
32212 +               } else
32213 +                       err = atomic_read(next);
32214 +               AuDbg("%d\n", err);
32215 +               u = err;
32216 +               err = u % nbr;
32217 +               AuDbg("%d\n", err);
32218 +               if (!au_br_rdonly(au_sbr(sb, err)))
32219 +                       break;
32220 +               err = -EROFS;
32221 +       }
32222 +
32223 +       if (err >= 0)
32224 +               err = au_wbr_nonopq(dentry, err);
32225 +
32226 +out:
32227 +       AuDbg("%d\n", err);
32228 +       return err;
32229 +}
32230 +
32231 +/* ---------------------------------------------------------------------- */
32232 +
32233 +/* most free space */
32234 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
32235 +{
32236 +       struct super_block *sb;
32237 +       struct au_branch *br;
32238 +       struct au_wbr_mfs *mfs;
32239 +       struct dentry *h_parent;
32240 +       aufs_bindex_t bindex, bbot;
32241 +       int err;
32242 +       unsigned long long b, bavail;
32243 +       struct path h_path;
32244 +       /* reduce the stack usage */
32245 +       struct kstatfs *st;
32246 +
32247 +       st = kmalloc(sizeof(*st), GFP_NOFS);
32248 +       if (unlikely(!st)) {
32249 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
32250 +               return;
32251 +       }
32252 +
32253 +       bavail = 0;
32254 +       sb = dentry->d_sb;
32255 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32256 +       MtxMustLock(&mfs->mfs_lock);
32257 +       mfs->mfs_bindex = -EROFS;
32258 +       mfs->mfsrr_bytes = 0;
32259 +       if (!parent) {
32260 +               bindex = 0;
32261 +               bbot = au_sbbot(sb);
32262 +       } else {
32263 +               bindex = au_dbtop(parent);
32264 +               bbot = au_dbtaildir(parent);
32265 +       }
32266 +
32267 +       for (; bindex <= bbot; bindex++) {
32268 +               if (parent) {
32269 +                       h_parent = au_h_dptr(parent, bindex);
32270 +                       if (!h_parent || d_is_negative(h_parent))
32271 +                               continue;
32272 +               }
32273 +               br = au_sbr(sb, bindex);
32274 +               if (au_br_rdonly(br))
32275 +                       continue;
32276 +
32277 +               /* sb->s_root for NFS is unreliable */
32278 +               h_path.mnt = au_br_mnt(br);
32279 +               h_path.dentry = h_path.mnt->mnt_root;
32280 +               err = vfs_statfs(&h_path, st);
32281 +               if (unlikely(err)) {
32282 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
32283 +                       continue;
32284 +               }
32285 +
32286 +               /* when the available size is equal, select the lower one */
32287 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
32288 +                            || sizeof(b) < sizeof(st->f_bsize));
32289 +               b = st->f_bavail * st->f_bsize;
32290 +               br->br_wbr->wbr_bytes = b;
32291 +               if (b >= bavail) {
32292 +                       bavail = b;
32293 +                       mfs->mfs_bindex = bindex;
32294 +                       mfs->mfs_jiffy = jiffies;
32295 +               }
32296 +       }
32297 +
32298 +       mfs->mfsrr_bytes = bavail;
32299 +       AuDbg("b%d\n", mfs->mfs_bindex);
32300 +       kfree(st);
32301 +}
32302 +
32303 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
32304 +{
32305 +       int err;
32306 +       struct dentry *parent;
32307 +       struct super_block *sb;
32308 +       struct au_wbr_mfs *mfs;
32309 +
32310 +       err = au_wbr_create_exp(dentry);
32311 +       if (err >= 0)
32312 +               goto out;
32313 +
32314 +       sb = dentry->d_sb;
32315 +       parent = NULL;
32316 +       if (au_ftest_wbr(flags, PARENT))
32317 +               parent = dget_parent(dentry);
32318 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32319 +       mutex_lock(&mfs->mfs_lock);
32320 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
32321 +           || mfs->mfs_bindex < 0
32322 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
32323 +               au_mfs(dentry, parent);
32324 +       mutex_unlock(&mfs->mfs_lock);
32325 +       err = mfs->mfs_bindex;
32326 +       dput(parent);
32327 +
32328 +       if (err >= 0)
32329 +               err = au_wbr_nonopq(dentry, err);
32330 +
32331 +out:
32332 +       AuDbg("b%d\n", err);
32333 +       return err;
32334 +}
32335 +
32336 +static int au_wbr_create_init_mfs(struct super_block *sb)
32337 +{
32338 +       struct au_wbr_mfs *mfs;
32339 +
32340 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32341 +       mutex_init(&mfs->mfs_lock);
32342 +       mfs->mfs_jiffy = 0;
32343 +       mfs->mfs_bindex = -EROFS;
32344 +
32345 +       return 0;
32346 +}
32347 +
32348 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
32349 +{
32350 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
32351 +       return 0;
32352 +}
32353 +
32354 +/* ---------------------------------------------------------------------- */
32355 +
32356 +/* top down regardless parent, and then mfs */
32357 +static int au_wbr_create_tdmfs(struct dentry *dentry,
32358 +                              unsigned int flags __maybe_unused)
32359 +{
32360 +       int err;
32361 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
32362 +       unsigned long long watermark;
32363 +       struct super_block *sb;
32364 +       struct au_wbr_mfs *mfs;
32365 +       struct au_branch *br;
32366 +       struct dentry *parent;
32367 +
32368 +       sb = dentry->d_sb;
32369 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32370 +       mutex_lock(&mfs->mfs_lock);
32371 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
32372 +           || mfs->mfs_bindex < 0)
32373 +               au_mfs(dentry, /*parent*/NULL);
32374 +       watermark = mfs->mfsrr_watermark;
32375 +       bmfs = mfs->mfs_bindex;
32376 +       mutex_unlock(&mfs->mfs_lock);
32377 +
32378 +       /* another style of au_wbr_create_exp() */
32379 +       bwh = au_dbwh(dentry);
32380 +       parent = dget_parent(dentry);
32381 +       btail = au_dbtaildir(parent);
32382 +       if (bwh >= 0 && bwh < btail)
32383 +               btail = bwh;
32384 +
32385 +       err = au_wbr_nonopq(dentry, btail);
32386 +       if (unlikely(err < 0))
32387 +               goto out;
32388 +       btail = err;
32389 +       bfound = -1;
32390 +       for (bindex = 0; bindex <= btail; bindex++) {
32391 +               br = au_sbr(sb, bindex);
32392 +               if (au_br_rdonly(br))
32393 +                       continue;
32394 +               if (br->br_wbr->wbr_bytes > watermark) {
32395 +                       bfound = bindex;
32396 +                       break;
32397 +               }
32398 +       }
32399 +       err = bfound;
32400 +       if (err < 0)
32401 +               err = bmfs;
32402 +
32403 +out:
32404 +       dput(parent);
32405 +       AuDbg("b%d\n", err);
32406 +       return err;
32407 +}
32408 +
32409 +/* ---------------------------------------------------------------------- */
32410 +
32411 +/* most free space and then round robin */
32412 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
32413 +{
32414 +       int err;
32415 +       struct au_wbr_mfs *mfs;
32416 +
32417 +       err = au_wbr_create_mfs(dentry, flags);
32418 +       if (err >= 0) {
32419 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
32420 +               mutex_lock(&mfs->mfs_lock);
32421 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
32422 +                       err = au_wbr_create_rr(dentry, flags);
32423 +               mutex_unlock(&mfs->mfs_lock);
32424 +       }
32425 +
32426 +       AuDbg("b%d\n", err);
32427 +       return err;
32428 +}
32429 +
32430 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
32431 +{
32432 +       int err;
32433 +
32434 +       au_wbr_create_init_mfs(sb); /* ignore */
32435 +       err = au_wbr_create_init_rr(sb);
32436 +
32437 +       return err;
32438 +}
32439 +
32440 +/* ---------------------------------------------------------------------- */
32441 +
32442 +/* top down parent and most free space */
32443 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
32444 +{
32445 +       int err, e2;
32446 +       unsigned long long b;
32447 +       aufs_bindex_t bindex, btop, bbot;
32448 +       struct super_block *sb;
32449 +       struct dentry *parent, *h_parent;
32450 +       struct au_branch *br;
32451 +
32452 +       err = au_wbr_create_tdp(dentry, flags);
32453 +       if (unlikely(err < 0))
32454 +               goto out;
32455 +       parent = dget_parent(dentry);
32456 +       btop = au_dbtop(parent);
32457 +       bbot = au_dbtaildir(parent);
32458 +       if (btop == bbot)
32459 +               goto out_parent; /* success */
32460 +
32461 +       e2 = au_wbr_create_mfs(dentry, flags);
32462 +       if (e2 < 0)
32463 +               goto out_parent; /* success */
32464 +
32465 +       /* when the available size is equal, select upper one */
32466 +       sb = dentry->d_sb;
32467 +       br = au_sbr(sb, err);
32468 +       b = br->br_wbr->wbr_bytes;
32469 +       AuDbg("b%d, %llu\n", err, b);
32470 +
32471 +       for (bindex = btop; bindex <= bbot; bindex++) {
32472 +               h_parent = au_h_dptr(parent, bindex);
32473 +               if (!h_parent || d_is_negative(h_parent))
32474 +                       continue;
32475 +
32476 +               br = au_sbr(sb, bindex);
32477 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
32478 +                       b = br->br_wbr->wbr_bytes;
32479 +                       err = bindex;
32480 +                       AuDbg("b%d, %llu\n", err, b);
32481 +               }
32482 +       }
32483 +
32484 +       if (err >= 0)
32485 +               err = au_wbr_nonopq(dentry, err);
32486 +
32487 +out_parent:
32488 +       dput(parent);
32489 +out:
32490 +       AuDbg("b%d\n", err);
32491 +       return err;
32492 +}
32493 +
32494 +/* ---------------------------------------------------------------------- */
32495 +
32496 +/*
32497 + * - top down parent
32498 + * - most free space with parent
32499 + * - most free space round-robin regardless parent
32500 + */
32501 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
32502 +{
32503 +       int err;
32504 +       unsigned long long watermark;
32505 +       struct super_block *sb;
32506 +       struct au_branch *br;
32507 +       struct au_wbr_mfs *mfs;
32508 +
32509 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
32510 +       if (unlikely(err < 0))
32511 +               goto out;
32512 +
32513 +       sb = dentry->d_sb;
32514 +       br = au_sbr(sb, err);
32515 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32516 +       mutex_lock(&mfs->mfs_lock);
32517 +       watermark = mfs->mfsrr_watermark;
32518 +       mutex_unlock(&mfs->mfs_lock);
32519 +       if (br->br_wbr->wbr_bytes < watermark)
32520 +               /* regardless the parent dir */
32521 +               err = au_wbr_create_mfsrr(dentry, flags);
32522 +
32523 +out:
32524 +       AuDbg("b%d\n", err);
32525 +       return err;
32526 +}
32527 +
32528 +/* ---------------------------------------------------------------------- */
32529 +
32530 +/* policies for copyup */
32531 +
32532 +/* top down parent */
32533 +static int au_wbr_copyup_tdp(struct dentry *dentry)
32534 +{
32535 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
32536 +}
32537 +
32538 +/* bottom up parent */
32539 +static int au_wbr_copyup_bup(struct dentry *dentry)
32540 +{
32541 +       int err;
32542 +       aufs_bindex_t bindex, btop;
32543 +       struct dentry *parent, *h_parent;
32544 +       struct super_block *sb;
32545 +
32546 +       err = -EROFS;
32547 +       sb = dentry->d_sb;
32548 +       parent = dget_parent(dentry);
32549 +       btop = au_dbtop(parent);
32550 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
32551 +               h_parent = au_h_dptr(parent, bindex);
32552 +               if (!h_parent || d_is_negative(h_parent))
32553 +                       continue;
32554 +
32555 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
32556 +                       err = bindex;
32557 +                       break;
32558 +               }
32559 +       }
32560 +       dput(parent);
32561 +
32562 +       /* bottom up here */
32563 +       if (unlikely(err < 0))
32564 +               err = au_wbr_bu(sb, btop - 1);
32565 +
32566 +       AuDbg("b%d\n", err);
32567 +       return err;
32568 +}
32569 +
32570 +/* bottom up */
32571 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
32572 +{
32573 +       int err;
32574 +
32575 +       err = au_wbr_bu(dentry->d_sb, btop);
32576 +       AuDbg("b%d\n", err);
32577 +       if (err > btop)
32578 +               err = au_wbr_nonopq(dentry, err);
32579 +
32580 +       AuDbg("b%d\n", err);
32581 +       return err;
32582 +}
32583 +
32584 +static int au_wbr_copyup_bu(struct dentry *dentry)
32585 +{
32586 +       int err;
32587 +       aufs_bindex_t btop;
32588 +
32589 +       btop = au_dbtop(dentry);
32590 +       err = au_wbr_do_copyup_bu(dentry, btop);
32591 +       return err;
32592 +}
32593 +
32594 +/* ---------------------------------------------------------------------- */
32595 +
32596 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
32597 +       [AuWbrCopyup_TDP] = {
32598 +               .copyup = au_wbr_copyup_tdp
32599 +       },
32600 +       [AuWbrCopyup_BUP] = {
32601 +               .copyup = au_wbr_copyup_bup
32602 +       },
32603 +       [AuWbrCopyup_BU] = {
32604 +               .copyup = au_wbr_copyup_bu
32605 +       }
32606 +};
32607 +
32608 +struct au_wbr_create_operations au_wbr_create_ops[] = {
32609 +       [AuWbrCreate_TDP] = {
32610 +               .create = au_wbr_create_tdp
32611 +       },
32612 +       [AuWbrCreate_RR] = {
32613 +               .create = au_wbr_create_rr,
32614 +               .init   = au_wbr_create_init_rr
32615 +       },
32616 +       [AuWbrCreate_MFS] = {
32617 +               .create = au_wbr_create_mfs,
32618 +               .init   = au_wbr_create_init_mfs,
32619 +               .fin    = au_wbr_create_fin_mfs
32620 +       },
32621 +       [AuWbrCreate_MFSV] = {
32622 +               .create = au_wbr_create_mfs,
32623 +               .init   = au_wbr_create_init_mfs,
32624 +               .fin    = au_wbr_create_fin_mfs
32625 +       },
32626 +       [AuWbrCreate_MFSRR] = {
32627 +               .create = au_wbr_create_mfsrr,
32628 +               .init   = au_wbr_create_init_mfsrr,
32629 +               .fin    = au_wbr_create_fin_mfs
32630 +       },
32631 +       [AuWbrCreate_MFSRRV] = {
32632 +               .create = au_wbr_create_mfsrr,
32633 +               .init   = au_wbr_create_init_mfsrr,
32634 +               .fin    = au_wbr_create_fin_mfs
32635 +       },
32636 +       [AuWbrCreate_TDMFS] = {
32637 +               .create = au_wbr_create_tdmfs,
32638 +               .init   = au_wbr_create_init_mfs,
32639 +               .fin    = au_wbr_create_fin_mfs
32640 +       },
32641 +       [AuWbrCreate_TDMFSV] = {
32642 +               .create = au_wbr_create_tdmfs,
32643 +               .init   = au_wbr_create_init_mfs,
32644 +               .fin    = au_wbr_create_fin_mfs
32645 +       },
32646 +       [AuWbrCreate_PMFS] = {
32647 +               .create = au_wbr_create_pmfs,
32648 +               .init   = au_wbr_create_init_mfs,
32649 +               .fin    = au_wbr_create_fin_mfs
32650 +       },
32651 +       [AuWbrCreate_PMFSV] = {
32652 +               .create = au_wbr_create_pmfs,
32653 +               .init   = au_wbr_create_init_mfs,
32654 +               .fin    = au_wbr_create_fin_mfs
32655 +       },
32656 +       [AuWbrCreate_PMFSRR] = {
32657 +               .create = au_wbr_create_pmfsrr,
32658 +               .init   = au_wbr_create_init_mfsrr,
32659 +               .fin    = au_wbr_create_fin_mfs
32660 +       },
32661 +       [AuWbrCreate_PMFSRRV] = {
32662 +               .create = au_wbr_create_pmfsrr,
32663 +               .init   = au_wbr_create_init_mfsrr,
32664 +               .fin    = au_wbr_create_fin_mfs
32665 +       }
32666 +};
32667 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
32668 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
32669 +++ linux/fs/aufs/whout.c       2017-07-29 12:14:25.906375514 +0200
32670 @@ -0,0 +1,1061 @@
32671 +/*
32672 + * Copyright (C) 2005-2017 Junjiro R. Okajima
32673 + *
32674 + * This program, aufs is free software; you can redistribute it and/or modify
32675 + * it under the terms of the GNU General Public License as published by
32676 + * the Free Software Foundation; either version 2 of the License, or
32677 + * (at your option) any later version.
32678 + *
32679 + * This program is distributed in the hope that it will be useful,
32680 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32681 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32682 + * GNU General Public License for more details.
32683 + *
32684 + * You should have received a copy of the GNU General Public License
32685 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32686 + */
32687 +
32688 +/*
32689 + * whiteout for logical deletion and opaque directory
32690 + */
32691 +
32692 +#include "aufs.h"
32693 +
32694 +#define WH_MASK                        S_IRUGO
32695 +
32696 +/*
32697 + * If a directory contains this file, then it is opaque.  We start with the
32698 + * .wh. flag so that it is blocked by lookup.
32699 + */
32700 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
32701 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
32702 +
32703 +/*
32704 + * generate whiteout name, which is NOT terminated by NULL.
32705 + * @name: original d_name.name
32706 + * @len: original d_name.len
32707 + * @wh: whiteout qstr
32708 + * returns zero when succeeds, otherwise error.
32709 + * succeeded value as wh->name should be freed by kfree().
32710 + */
32711 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
32712 +{
32713 +       char *p;
32714 +
32715 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
32716 +               return -ENAMETOOLONG;
32717 +
32718 +       wh->len = name->len + AUFS_WH_PFX_LEN;
32719 +       p = kmalloc(wh->len, GFP_NOFS);
32720 +       wh->name = p;
32721 +       if (p) {
32722 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32723 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
32724 +               /* smp_mb(); */
32725 +               return 0;
32726 +       }
32727 +       return -ENOMEM;
32728 +}
32729 +
32730 +/* ---------------------------------------------------------------------- */
32731 +
32732 +/*
32733 + * test if the @wh_name exists under @h_parent.
32734 + * @try_sio specifies the necessary of super-io.
32735 + */
32736 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
32737 +{
32738 +       int err;
32739 +       struct dentry *wh_dentry;
32740 +
32741 +       if (!try_sio)
32742 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
32743 +       else
32744 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
32745 +       err = PTR_ERR(wh_dentry);
32746 +       if (IS_ERR(wh_dentry)) {
32747 +               if (err == -ENAMETOOLONG)
32748 +                       err = 0;
32749 +               goto out;
32750 +       }
32751 +
32752 +       err = 0;
32753 +       if (d_is_negative(wh_dentry))
32754 +               goto out_wh; /* success */
32755 +
32756 +       err = 1;
32757 +       if (d_is_reg(wh_dentry))
32758 +               goto out_wh; /* success */
32759 +
32760 +       err = -EIO;
32761 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
32762 +               wh_dentry, d_inode(wh_dentry)->i_mode);
32763 +
32764 +out_wh:
32765 +       dput(wh_dentry);
32766 +out:
32767 +       return err;
32768 +}
32769 +
32770 +/*
32771 + * test if the @h_dentry sets opaque or not.
32772 + */
32773 +int au_diropq_test(struct dentry *h_dentry)
32774 +{
32775 +       int err;
32776 +       struct inode *h_dir;
32777 +
32778 +       h_dir = d_inode(h_dentry);
32779 +       err = au_wh_test(h_dentry, &diropq_name,
32780 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
32781 +       return err;
32782 +}
32783 +
32784 +/*
32785 + * returns a negative dentry whose name is unique and temporary.
32786 + */
32787 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32788 +                            struct qstr *prefix)
32789 +{
32790 +       struct dentry *dentry;
32791 +       int i;
32792 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
32793 +               *name, *p;
32794 +       /* strict atomic_t is unnecessary here */
32795 +       static unsigned short cnt;
32796 +       struct qstr qs;
32797 +
32798 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32799 +
32800 +       name = defname;
32801 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32802 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
32803 +               dentry = ERR_PTR(-ENAMETOOLONG);
32804 +               if (unlikely(qs.len > NAME_MAX))
32805 +                       goto out;
32806 +               dentry = ERR_PTR(-ENOMEM);
32807 +               name = kmalloc(qs.len + 1, GFP_NOFS);
32808 +               if (unlikely(!name))
32809 +                       goto out;
32810 +       }
32811 +
32812 +       /* doubly whiteout-ed */
32813 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32814 +       p = name + AUFS_WH_PFX_LEN * 2;
32815 +       memcpy(p, prefix->name, prefix->len);
32816 +       p += prefix->len;
32817 +       *p++ = '.';
32818 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
32819 +
32820 +       qs.name = name;
32821 +       for (i = 0; i < 3; i++) {
32822 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
32823 +               dentry = au_sio_lkup_one(&qs, h_parent);
32824 +               if (IS_ERR(dentry) || d_is_negative(dentry))
32825 +                       goto out_name;
32826 +               dput(dentry);
32827 +       }
32828 +       /* pr_warn("could not get random name\n"); */
32829 +       dentry = ERR_PTR(-EEXIST);
32830 +       AuDbg("%.*s\n", AuLNPair(&qs));
32831 +       BUG();
32832 +
32833 +out_name:
32834 +       if (name != defname)
32835 +               kfree(name);
32836 +out:
32837 +       AuTraceErrPtr(dentry);
32838 +       return dentry;
32839 +}
32840 +
32841 +/*
32842 + * rename the @h_dentry on @br to the whiteouted temporary name.
32843 + */
32844 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32845 +{
32846 +       int err;
32847 +       struct path h_path = {
32848 +               .mnt = au_br_mnt(br)
32849 +       };
32850 +       struct inode *h_dir, *delegated;
32851 +       struct dentry *h_parent;
32852 +
32853 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
32854 +       h_dir = d_inode(h_parent);
32855 +       IMustLock(h_dir);
32856 +
32857 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32858 +       err = PTR_ERR(h_path.dentry);
32859 +       if (IS_ERR(h_path.dentry))
32860 +               goto out;
32861 +
32862 +       /* under the same dir, no need to lock_rename() */
32863 +       delegated = NULL;
32864 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
32865 +                          /*flags*/0);
32866 +       AuTraceErr(err);
32867 +       if (unlikely(err == -EWOULDBLOCK)) {
32868 +               pr_warn("cannot retry for NFSv4 delegation"
32869 +                       " for an internal rename\n");
32870 +               iput(delegated);
32871 +       }
32872 +       dput(h_path.dentry);
32873 +
32874 +out:
32875 +       AuTraceErr(err);
32876 +       return err;
32877 +}
32878 +
32879 +/* ---------------------------------------------------------------------- */
32880 +/*
32881 + * functions for removing a whiteout
32882 + */
32883 +
32884 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32885 +{
32886 +       int err, force;
32887 +       struct inode *delegated;
32888 +
32889 +       /*
32890 +        * forces superio when the dir has a sticky bit.
32891 +        * this may be a violation of unix fs semantics.
32892 +        */
32893 +       force = (h_dir->i_mode & S_ISVTX)
32894 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
32895 +       delegated = NULL;
32896 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
32897 +       if (unlikely(err == -EWOULDBLOCK)) {
32898 +               pr_warn("cannot retry for NFSv4 delegation"
32899 +                       " for an internal unlink\n");
32900 +               iput(delegated);
32901 +       }
32902 +       return err;
32903 +}
32904 +
32905 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32906 +                       struct dentry *dentry)
32907 +{
32908 +       int err;
32909 +
32910 +       err = do_unlink_wh(h_dir, h_path);
32911 +       if (!err && dentry)
32912 +               au_set_dbwh(dentry, -1);
32913 +
32914 +       return err;
32915 +}
32916 +
32917 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32918 +                         struct au_branch *br)
32919 +{
32920 +       int err;
32921 +       struct path h_path = {
32922 +               .mnt = au_br_mnt(br)
32923 +       };
32924 +
32925 +       err = 0;
32926 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32927 +       if (IS_ERR(h_path.dentry))
32928 +               err = PTR_ERR(h_path.dentry);
32929 +       else {
32930 +               if (d_is_reg(h_path.dentry))
32931 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
32932 +               dput(h_path.dentry);
32933 +       }
32934 +
32935 +       return err;
32936 +}
32937 +
32938 +/* ---------------------------------------------------------------------- */
32939 +/*
32940 + * initialize/clean whiteout for a branch
32941 + */
32942 +
32943 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32944 +                       const int isdir)
32945 +{
32946 +       int err;
32947 +       struct inode *delegated;
32948 +
32949 +       if (d_is_negative(whpath->dentry))
32950 +               return;
32951 +
32952 +       if (isdir)
32953 +               err = vfsub_rmdir(h_dir, whpath);
32954 +       else {
32955 +               delegated = NULL;
32956 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32957 +               if (unlikely(err == -EWOULDBLOCK)) {
32958 +                       pr_warn("cannot retry for NFSv4 delegation"
32959 +                               " for an internal unlink\n");
32960 +                       iput(delegated);
32961 +               }
32962 +       }
32963 +       if (unlikely(err))
32964 +               pr_warn("failed removing %pd (%d), ignored.\n",
32965 +                       whpath->dentry, err);
32966 +}
32967 +
32968 +static int test_linkable(struct dentry *h_root)
32969 +{
32970 +       struct inode *h_dir = d_inode(h_root);
32971 +
32972 +       if (h_dir->i_op->link)
32973 +               return 0;
32974 +
32975 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32976 +              h_root, au_sbtype(h_root->d_sb));
32977 +       return -ENOSYS;
32978 +}
32979 +
32980 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32981 +static int au_whdir(struct inode *h_dir, struct path *path)
32982 +{
32983 +       int err;
32984 +
32985 +       err = -EEXIST;
32986 +       if (d_is_negative(path->dentry)) {
32987 +               int mode = S_IRWXU;
32988 +
32989 +               if (au_test_nfs(path->dentry->d_sb))
32990 +                       mode |= S_IXUGO;
32991 +               err = vfsub_mkdir(h_dir, path, mode);
32992 +       } else if (d_is_dir(path->dentry))
32993 +               err = 0;
32994 +       else
32995 +               pr_err("unknown %pd exists\n", path->dentry);
32996 +
32997 +       return err;
32998 +}
32999 +
33000 +struct au_wh_base {
33001 +       const struct qstr *name;
33002 +       struct dentry *dentry;
33003 +};
33004 +
33005 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
33006 +                         struct path *h_path)
33007 +{
33008 +       h_path->dentry = base[AuBrWh_BASE].dentry;
33009 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
33010 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
33011 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
33012 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
33013 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
33014 +}
33015 +
33016 +/*
33017 + * returns tri-state,
33018 + * minus: error, caller should print the message
33019 + * zero: succuess
33020 + * plus: error, caller should NOT print the message
33021 + */
33022 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
33023 +                               int do_plink, struct au_wh_base base[],
33024 +                               struct path *h_path)
33025 +{
33026 +       int err;
33027 +       struct inode *h_dir;
33028 +
33029 +       h_dir = d_inode(h_root);
33030 +       h_path->dentry = base[AuBrWh_BASE].dentry;
33031 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
33032 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
33033 +       if (do_plink) {
33034 +               err = test_linkable(h_root);
33035 +               if (unlikely(err)) {
33036 +                       err = 1;
33037 +                       goto out;
33038 +               }
33039 +
33040 +               err = au_whdir(h_dir, h_path);
33041 +               if (unlikely(err))
33042 +                       goto out;
33043 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
33044 +       } else
33045 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
33046 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
33047 +       err = au_whdir(h_dir, h_path);
33048 +       if (unlikely(err))
33049 +               goto out;
33050 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
33051 +
33052 +out:
33053 +       return err;
33054 +}
33055 +
33056 +/*
33057 + * for the moment, aufs supports the branch filesystem which does not support
33058 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
33059 + * copyup failed. finally, such filesystem will not be used as the writable
33060 + * branch.
33061 + *
33062 + * returns tri-state, see above.
33063 + */
33064 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
33065 +                        int do_plink, struct au_wh_base base[],
33066 +                        struct path *h_path)
33067 +{
33068 +       int err;
33069 +       struct inode *h_dir;
33070 +
33071 +       WbrWhMustWriteLock(wbr);
33072 +
33073 +       err = test_linkable(h_root);
33074 +       if (unlikely(err)) {
33075 +               err = 1;
33076 +               goto out;
33077 +       }
33078 +
33079 +       /*
33080 +        * todo: should this create be done in /sbin/mount.aufs helper?
33081 +        */
33082 +       err = -EEXIST;
33083 +       h_dir = d_inode(h_root);
33084 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
33085 +               h_path->dentry = base[AuBrWh_BASE].dentry;
33086 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
33087 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
33088 +               err = 0;
33089 +       else
33090 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
33091 +       if (unlikely(err))
33092 +               goto out;
33093 +
33094 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
33095 +       if (do_plink) {
33096 +               err = au_whdir(h_dir, h_path);
33097 +               if (unlikely(err))
33098 +                       goto out;
33099 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
33100 +       } else
33101 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
33102 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
33103 +
33104 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
33105 +       err = au_whdir(h_dir, h_path);
33106 +       if (unlikely(err))
33107 +               goto out;
33108 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
33109 +
33110 +out:
33111 +       return err;
33112 +}
33113 +
33114 +/*
33115 + * initialize the whiteout base file/dir for @br.
33116 + */
33117 +int au_wh_init(struct au_branch *br, struct super_block *sb)
33118 +{
33119 +       int err, i;
33120 +       const unsigned char do_plink
33121 +               = !!au_opt_test(au_mntflags(sb), PLINK);
33122 +       struct inode *h_dir;
33123 +       struct path path = br->br_path;
33124 +       struct dentry *h_root = path.dentry;
33125 +       struct au_wbr *wbr = br->br_wbr;
33126 +       static const struct qstr base_name[] = {
33127 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
33128 +                                         sizeof(AUFS_BASE_NAME) - 1),
33129 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
33130 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
33131 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
33132 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
33133 +       };
33134 +       struct au_wh_base base[] = {
33135 +               [AuBrWh_BASE] = {
33136 +                       .name   = base_name + AuBrWh_BASE,
33137 +                       .dentry = NULL
33138 +               },
33139 +               [AuBrWh_PLINK] = {
33140 +                       .name   = base_name + AuBrWh_PLINK,
33141 +                       .dentry = NULL
33142 +               },
33143 +               [AuBrWh_ORPH] = {
33144 +                       .name   = base_name + AuBrWh_ORPH,
33145 +                       .dentry = NULL
33146 +               }
33147 +       };
33148 +
33149 +       if (wbr)
33150 +               WbrWhMustWriteLock(wbr);
33151 +
33152 +       for (i = 0; i < AuBrWh_Last; i++) {
33153 +               /* doubly whiteouted */
33154 +               struct dentry *d;
33155 +
33156 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
33157 +               err = PTR_ERR(d);
33158 +               if (IS_ERR(d))
33159 +                       goto out;
33160 +
33161 +               base[i].dentry = d;
33162 +               AuDebugOn(wbr
33163 +                         && wbr->wbr_wh[i]
33164 +                         && wbr->wbr_wh[i] != base[i].dentry);
33165 +       }
33166 +
33167 +       if (wbr)
33168 +               for (i = 0; i < AuBrWh_Last; i++) {
33169 +                       dput(wbr->wbr_wh[i]);
33170 +                       wbr->wbr_wh[i] = NULL;
33171 +               }
33172 +
33173 +       err = 0;
33174 +       if (!au_br_writable(br->br_perm)) {
33175 +               h_dir = d_inode(h_root);
33176 +               au_wh_init_ro(h_dir, base, &path);
33177 +       } else if (!au_br_wh_linkable(br->br_perm)) {
33178 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
33179 +               if (err > 0)
33180 +                       goto out;
33181 +               else if (err)
33182 +                       goto out_err;
33183 +       } else {
33184 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
33185 +               if (err > 0)
33186 +                       goto out;
33187 +               else if (err)
33188 +                       goto out_err;
33189 +       }
33190 +       goto out; /* success */
33191 +
33192 +out_err:
33193 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
33194 +              err, h_root, au_sbtype(h_root->d_sb));
33195 +out:
33196 +       for (i = 0; i < AuBrWh_Last; i++)
33197 +               dput(base[i].dentry);
33198 +       return err;
33199 +}
33200 +
33201 +/* ---------------------------------------------------------------------- */
33202 +/*
33203 + * whiteouts are all hard-linked usually.
33204 + * when its link count reaches a ceiling, we create a new whiteout base
33205 + * asynchronously.
33206 + */
33207 +
33208 +struct reinit_br_wh {
33209 +       struct super_block *sb;
33210 +       struct au_branch *br;
33211 +};
33212 +
33213 +static void reinit_br_wh(void *arg)
33214 +{
33215 +       int err;
33216 +       aufs_bindex_t bindex;
33217 +       struct path h_path;
33218 +       struct reinit_br_wh *a = arg;
33219 +       struct au_wbr *wbr;
33220 +       struct inode *dir, *delegated;
33221 +       struct dentry *h_root;
33222 +       struct au_hinode *hdir;
33223 +
33224 +       err = 0;
33225 +       wbr = a->br->br_wbr;
33226 +       /* big aufs lock */
33227 +       si_noflush_write_lock(a->sb);
33228 +       if (!au_br_writable(a->br->br_perm))
33229 +               goto out;
33230 +       bindex = au_br_index(a->sb, a->br->br_id);
33231 +       if (unlikely(bindex < 0))
33232 +               goto out;
33233 +
33234 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
33235 +       dir = d_inode(a->sb->s_root);
33236 +       hdir = au_hi(dir, bindex);
33237 +       h_root = au_h_dptr(a->sb->s_root, bindex);
33238 +       AuDebugOn(h_root != au_br_dentry(a->br));
33239 +
33240 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
33241 +       wbr_wh_write_lock(wbr);
33242 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
33243 +                         h_root, a->br);
33244 +       if (!err) {
33245 +               h_path.dentry = wbr->wbr_whbase;
33246 +               h_path.mnt = au_br_mnt(a->br);
33247 +               delegated = NULL;
33248 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
33249 +                                  /*force*/0);
33250 +               if (unlikely(err == -EWOULDBLOCK)) {
33251 +                       pr_warn("cannot retry for NFSv4 delegation"
33252 +                               " for an internal unlink\n");
33253 +                       iput(delegated);
33254 +               }
33255 +       } else {
33256 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
33257 +               err = 0;
33258 +       }
33259 +       dput(wbr->wbr_whbase);
33260 +       wbr->wbr_whbase = NULL;
33261 +       if (!err)
33262 +               err = au_wh_init(a->br, a->sb);
33263 +       wbr_wh_write_unlock(wbr);
33264 +       au_hn_inode_unlock(hdir);
33265 +       di_read_unlock(a->sb->s_root, AuLock_IR);
33266 +       if (!err)
33267 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
33268 +
33269 +out:
33270 +       if (wbr)
33271 +               atomic_dec(&wbr->wbr_wh_running);
33272 +       au_br_put(a->br);
33273 +       si_write_unlock(a->sb);
33274 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
33275 +       kfree(arg);
33276 +       if (unlikely(err))
33277 +               AuIOErr("err %d\n", err);
33278 +}
33279 +
33280 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
33281 +{
33282 +       int do_dec, wkq_err;
33283 +       struct reinit_br_wh *arg;
33284 +
33285 +       do_dec = 1;
33286 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
33287 +               goto out;
33288 +
33289 +       /* ignore ENOMEM */
33290 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
33291 +       if (arg) {
33292 +               /*
33293 +                * dec(wh_running), kfree(arg) and dec(br_count)
33294 +                * in reinit function
33295 +                */
33296 +               arg->sb = sb;
33297 +               arg->br = br;
33298 +               au_br_get(br);
33299 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
33300 +               if (unlikely(wkq_err)) {
33301 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
33302 +                       au_br_put(br);
33303 +                       kfree(arg);
33304 +               }
33305 +               do_dec = 0;
33306 +       }
33307 +
33308 +out:
33309 +       if (do_dec)
33310 +               atomic_dec(&br->br_wbr->wbr_wh_running);
33311 +}
33312 +
33313 +/* ---------------------------------------------------------------------- */
33314 +
33315 +/*
33316 + * create the whiteout @wh.
33317 + */
33318 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
33319 +                            struct dentry *wh)
33320 +{
33321 +       int err;
33322 +       struct path h_path = {
33323 +               .dentry = wh
33324 +       };
33325 +       struct au_branch *br;
33326 +       struct au_wbr *wbr;
33327 +       struct dentry *h_parent;
33328 +       struct inode *h_dir, *delegated;
33329 +
33330 +       h_parent = wh->d_parent; /* dir inode is locked */
33331 +       h_dir = d_inode(h_parent);
33332 +       IMustLock(h_dir);
33333 +
33334 +       br = au_sbr(sb, bindex);
33335 +       h_path.mnt = au_br_mnt(br);
33336 +       wbr = br->br_wbr;
33337 +       wbr_wh_read_lock(wbr);
33338 +       if (wbr->wbr_whbase) {
33339 +               delegated = NULL;
33340 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
33341 +               if (unlikely(err == -EWOULDBLOCK)) {
33342 +                       pr_warn("cannot retry for NFSv4 delegation"
33343 +                               " for an internal link\n");
33344 +                       iput(delegated);
33345 +               }
33346 +               if (!err || err != -EMLINK)
33347 +                       goto out;
33348 +
33349 +               /* link count full. re-initialize br_whbase. */
33350 +               kick_reinit_br_wh(sb, br);
33351 +       }
33352 +
33353 +       /* return this error in this context */
33354 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
33355 +       if (!err)
33356 +               au_fhsm_wrote(sb, bindex, /*force*/0);
33357 +
33358 +out:
33359 +       wbr_wh_read_unlock(wbr);
33360 +       return err;
33361 +}
33362 +
33363 +/* ---------------------------------------------------------------------- */
33364 +
33365 +/*
33366 + * create or remove the diropq.
33367 + */
33368 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
33369 +                               unsigned int flags)
33370 +{
33371 +       struct dentry *opq_dentry, *h_dentry;
33372 +       struct super_block *sb;
33373 +       struct au_branch *br;
33374 +       int err;
33375 +
33376 +       sb = dentry->d_sb;
33377 +       br = au_sbr(sb, bindex);
33378 +       h_dentry = au_h_dptr(dentry, bindex);
33379 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
33380 +       if (IS_ERR(opq_dentry))
33381 +               goto out;
33382 +
33383 +       if (au_ftest_diropq(flags, CREATE)) {
33384 +               err = link_or_create_wh(sb, bindex, opq_dentry);
33385 +               if (!err) {
33386 +                       au_set_dbdiropq(dentry, bindex);
33387 +                       goto out; /* success */
33388 +               }
33389 +       } else {
33390 +               struct path tmp = {
33391 +                       .dentry = opq_dentry,
33392 +                       .mnt    = au_br_mnt(br)
33393 +               };
33394 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
33395 +               if (!err)
33396 +                       au_set_dbdiropq(dentry, -1);
33397 +       }
33398 +       dput(opq_dentry);
33399 +       opq_dentry = ERR_PTR(err);
33400 +
33401 +out:
33402 +       return opq_dentry;
33403 +}
33404 +
33405 +struct do_diropq_args {
33406 +       struct dentry **errp;
33407 +       struct dentry *dentry;
33408 +       aufs_bindex_t bindex;
33409 +       unsigned int flags;
33410 +};
33411 +
33412 +static void call_do_diropq(void *args)
33413 +{
33414 +       struct do_diropq_args *a = args;
33415 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
33416 +}
33417 +
33418 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33419 +                            unsigned int flags)
33420 +{
33421 +       struct dentry *diropq, *h_dentry;
33422 +
33423 +       h_dentry = au_h_dptr(dentry, bindex);
33424 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
33425 +               diropq = do_diropq(dentry, bindex, flags);
33426 +       else {
33427 +               int wkq_err;
33428 +               struct do_diropq_args args = {
33429 +                       .errp           = &diropq,
33430 +                       .dentry         = dentry,
33431 +                       .bindex         = bindex,
33432 +                       .flags          = flags
33433 +               };
33434 +
33435 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
33436 +               if (unlikely(wkq_err))
33437 +                       diropq = ERR_PTR(wkq_err);
33438 +       }
33439 +
33440 +       return diropq;
33441 +}
33442 +
33443 +/* ---------------------------------------------------------------------- */
33444 +
33445 +/*
33446 + * lookup whiteout dentry.
33447 + * @h_parent: lower parent dentry which must exist and be locked
33448 + * @base_name: name of dentry which will be whiteouted
33449 + * returns dentry for whiteout.
33450 + */
33451 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33452 +                         struct au_branch *br)
33453 +{
33454 +       int err;
33455 +       struct qstr wh_name;
33456 +       struct dentry *wh_dentry;
33457 +
33458 +       err = au_wh_name_alloc(&wh_name, base_name);
33459 +       wh_dentry = ERR_PTR(err);
33460 +       if (!err) {
33461 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
33462 +               kfree(wh_name.name);
33463 +       }
33464 +       return wh_dentry;
33465 +}
33466 +
33467 +/*
33468 + * link/create a whiteout for @dentry on @bindex.
33469 + */
33470 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33471 +                           struct dentry *h_parent)
33472 +{
33473 +       struct dentry *wh_dentry;
33474 +       struct super_block *sb;
33475 +       int err;
33476 +
33477 +       sb = dentry->d_sb;
33478 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
33479 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
33480 +               err = link_or_create_wh(sb, bindex, wh_dentry);
33481 +               if (!err) {
33482 +                       au_set_dbwh(dentry, bindex);
33483 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
33484 +               } else {
33485 +                       dput(wh_dentry);
33486 +                       wh_dentry = ERR_PTR(err);
33487 +               }
33488 +       }
33489 +
33490 +       return wh_dentry;
33491 +}
33492 +
33493 +/* ---------------------------------------------------------------------- */
33494 +
33495 +/* Delete all whiteouts in this directory on branch bindex. */
33496 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
33497 +                          aufs_bindex_t bindex, struct au_branch *br)
33498 +{
33499 +       int err;
33500 +       unsigned long ul, n;
33501 +       struct qstr wh_name;
33502 +       char *p;
33503 +       struct hlist_head *head;
33504 +       struct au_vdir_wh *pos;
33505 +       struct au_vdir_destr *str;
33506 +
33507 +       err = -ENOMEM;
33508 +       p = (void *)__get_free_page(GFP_NOFS);
33509 +       wh_name.name = p;
33510 +       if (unlikely(!wh_name.name))
33511 +               goto out;
33512 +
33513 +       err = 0;
33514 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
33515 +       p += AUFS_WH_PFX_LEN;
33516 +       n = whlist->nh_num;
33517 +       head = whlist->nh_head;
33518 +       for (ul = 0; !err && ul < n; ul++, head++) {
33519 +               hlist_for_each_entry(pos, head, wh_hash) {
33520 +                       if (pos->wh_bindex != bindex)
33521 +                               continue;
33522 +
33523 +                       str = &pos->wh_str;
33524 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
33525 +                               memcpy(p, str->name, str->len);
33526 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
33527 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
33528 +                               if (!err)
33529 +                                       continue;
33530 +                               break;
33531 +                       }
33532 +                       AuIOErr("whiteout name too long %.*s\n",
33533 +                               str->len, str->name);
33534 +                       err = -EIO;
33535 +                       break;
33536 +               }
33537 +       }
33538 +       free_page((unsigned long)wh_name.name);
33539 +
33540 +out:
33541 +       return err;
33542 +}
33543 +
33544 +struct del_wh_children_args {
33545 +       int *errp;
33546 +       struct dentry *h_dentry;
33547 +       struct au_nhash *whlist;
33548 +       aufs_bindex_t bindex;
33549 +       struct au_branch *br;
33550 +};
33551 +
33552 +static void call_del_wh_children(void *args)
33553 +{
33554 +       struct del_wh_children_args *a = args;
33555 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
33556 +}
33557 +
33558 +/* ---------------------------------------------------------------------- */
33559 +
33560 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
33561 +{
33562 +       struct au_whtmp_rmdir *whtmp;
33563 +       int err;
33564 +       unsigned int rdhash;
33565 +
33566 +       SiMustAnyLock(sb);
33567 +
33568 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
33569 +       if (unlikely(!whtmp)) {
33570 +               whtmp = ERR_PTR(-ENOMEM);
33571 +               goto out;
33572 +       }
33573 +
33574 +       /* no estimation for dir size */
33575 +       rdhash = au_sbi(sb)->si_rdhash;
33576 +       if (!rdhash)
33577 +               rdhash = AUFS_RDHASH_DEF;
33578 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
33579 +       if (unlikely(err)) {
33580 +               kfree(whtmp);
33581 +               whtmp = ERR_PTR(err);
33582 +       }
33583 +
33584 +out:
33585 +       return whtmp;
33586 +}
33587 +
33588 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
33589 +{
33590 +       if (whtmp->br)
33591 +               au_br_put(whtmp->br);
33592 +       dput(whtmp->wh_dentry);
33593 +       iput(whtmp->dir);
33594 +       au_nhash_wh_free(&whtmp->whlist);
33595 +       kfree(whtmp);
33596 +}
33597 +
33598 +/*
33599 + * rmdir the whiteouted temporary named dir @h_dentry.
33600 + * @whlist: whiteouted children.
33601 + */
33602 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33603 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
33604 +{
33605 +       int err;
33606 +       unsigned int h_nlink;
33607 +       struct path h_tmp;
33608 +       struct inode *wh_inode, *h_dir;
33609 +       struct au_branch *br;
33610 +
33611 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
33612 +       IMustLock(h_dir);
33613 +
33614 +       br = au_sbr(dir->i_sb, bindex);
33615 +       wh_inode = d_inode(wh_dentry);
33616 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
33617 +
33618 +       /*
33619 +        * someone else might change some whiteouts while we were sleeping.
33620 +        * it means this whlist may have an obsoleted entry.
33621 +        */
33622 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
33623 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
33624 +       else {
33625 +               int wkq_err;
33626 +               struct del_wh_children_args args = {
33627 +                       .errp           = &err,
33628 +                       .h_dentry       = wh_dentry,
33629 +                       .whlist         = whlist,
33630 +                       .bindex         = bindex,
33631 +                       .br             = br
33632 +               };
33633 +
33634 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
33635 +               if (unlikely(wkq_err))
33636 +                       err = wkq_err;
33637 +       }
33638 +       inode_unlock(wh_inode);
33639 +
33640 +       if (!err) {
33641 +               h_tmp.dentry = wh_dentry;
33642 +               h_tmp.mnt = au_br_mnt(br);
33643 +               h_nlink = h_dir->i_nlink;
33644 +               err = vfsub_rmdir(h_dir, &h_tmp);
33645 +               /* some fs doesn't change the parent nlink in some cases */
33646 +               h_nlink -= h_dir->i_nlink;
33647 +       }
33648 +
33649 +       if (!err) {
33650 +               if (au_ibtop(dir) == bindex) {
33651 +                       /* todo: dir->i_mutex is necessary */
33652 +                       au_cpup_attr_timesizes(dir);
33653 +                       if (h_nlink)
33654 +                               vfsub_drop_nlink(dir);
33655 +               }
33656 +               return 0; /* success */
33657 +       }
33658 +
33659 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
33660 +       return err;
33661 +}
33662 +
33663 +static void call_rmdir_whtmp(void *args)
33664 +{
33665 +       int err;
33666 +       aufs_bindex_t bindex;
33667 +       struct au_whtmp_rmdir *a = args;
33668 +       struct super_block *sb;
33669 +       struct dentry *h_parent;
33670 +       struct inode *h_dir;
33671 +       struct au_hinode *hdir;
33672 +
33673 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
33674 +       /* inode_lock(a->dir); */
33675 +       err = -EROFS;
33676 +       sb = a->dir->i_sb;
33677 +       si_read_lock(sb, !AuLock_FLUSH);
33678 +       if (!au_br_writable(a->br->br_perm))
33679 +               goto out;
33680 +       bindex = au_br_index(sb, a->br->br_id);
33681 +       if (unlikely(bindex < 0))
33682 +               goto out;
33683 +
33684 +       err = -EIO;
33685 +       ii_write_lock_parent(a->dir);
33686 +       h_parent = dget_parent(a->wh_dentry);
33687 +       h_dir = d_inode(h_parent);
33688 +       hdir = au_hi(a->dir, bindex);
33689 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
33690 +       if (unlikely(err))
33691 +               goto out_mnt;
33692 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
33693 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
33694 +                         a->br);
33695 +       if (!err)
33696 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
33697 +       au_hn_inode_unlock(hdir);
33698 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
33699 +
33700 +out_mnt:
33701 +       dput(h_parent);
33702 +       ii_write_unlock(a->dir);
33703 +out:
33704 +       /* inode_unlock(a->dir); */
33705 +       au_whtmp_rmdir_free(a);
33706 +       si_read_unlock(sb);
33707 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33708 +       if (unlikely(err))
33709 +               AuIOErr("err %d\n", err);
33710 +}
33711 +
33712 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33713 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
33714 +{
33715 +       int wkq_err;
33716 +       struct super_block *sb;
33717 +
33718 +       IMustLock(dir);
33719 +
33720 +       /* all post-process will be done in do_rmdir_whtmp(). */
33721 +       sb = dir->i_sb;
33722 +       args->dir = au_igrab(dir);
33723 +       args->br = au_sbr(sb, bindex);
33724 +       au_br_get(args->br);
33725 +       args->wh_dentry = dget(wh_dentry);
33726 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
33727 +       if (unlikely(wkq_err)) {
33728 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
33729 +               au_whtmp_rmdir_free(args);
33730 +       }
33731 +}
33732 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33733 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
33734 +++ linux/fs/aufs/whout.h       2017-07-29 12:14:25.906375514 +0200
33735 @@ -0,0 +1,84 @@
33736 +/*
33737 + * Copyright (C) 2005-2017 Junjiro R. Okajima
33738 + *
33739 + * This program, aufs is free software; you can redistribute it and/or modify
33740 + * it under the terms of the GNU General Public License as published by
33741 + * the Free Software Foundation; either version 2 of the License, or
33742 + * (at your option) any later version.
33743 + *
33744 + * This program is distributed in the hope that it will be useful,
33745 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33746 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33747 + * GNU General Public License for more details.
33748 + *
33749 + * You should have received a copy of the GNU General Public License
33750 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33751 + */
33752 +
33753 +/*
33754 + * whiteout for logical deletion and opaque directory
33755 + */
33756 +
33757 +#ifndef __AUFS_WHOUT_H__
33758 +#define __AUFS_WHOUT_H__
33759 +
33760 +#ifdef __KERNEL__
33761 +
33762 +#include "dir.h"
33763 +
33764 +/* whout.c */
33765 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
33766 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33767 +int au_diropq_test(struct dentry *h_dentry);
33768 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33769 +                            struct qstr *prefix);
33770 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33771 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33772 +                       struct dentry *dentry);
33773 +int au_wh_init(struct au_branch *br, struct super_block *sb);
33774 +
33775 +/* diropq flags */
33776 +#define AuDiropq_CREATE        1
33777 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
33778 +#define au_fset_diropq(flags, name) \
33779 +       do { (flags) |= AuDiropq_##name; } while (0)
33780 +#define au_fclr_diropq(flags, name) \
33781 +       do { (flags) &= ~AuDiropq_##name; } while (0)
33782 +
33783 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33784 +                            unsigned int flags);
33785 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33786 +                         struct au_branch *br);
33787 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33788 +                           struct dentry *h_parent);
33789 +
33790 +/* real rmdir for the whiteout-ed dir */
33791 +struct au_whtmp_rmdir {
33792 +       struct inode *dir;
33793 +       struct au_branch *br;
33794 +       struct dentry *wh_dentry;
33795 +       struct au_nhash whlist;
33796 +};
33797 +
33798 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33799 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33800 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33801 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
33802 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33803 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33804 +
33805 +/* ---------------------------------------------------------------------- */
33806 +
33807 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
33808 +                                             aufs_bindex_t bindex)
33809 +{
33810 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33811 +}
33812 +
33813 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33814 +{
33815 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33816 +}
33817 +
33818 +#endif /* __KERNEL__ */
33819 +#endif /* __AUFS_WHOUT_H__ */
33820 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33821 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
33822 +++ linux/fs/aufs/wkq.c 2017-07-29 12:14:25.906375514 +0200
33823 @@ -0,0 +1,213 @@
33824 +/*
33825 + * Copyright (C) 2005-2017 Junjiro R. Okajima
33826 + *
33827 + * This program, aufs is free software; you can redistribute it and/or modify
33828 + * it under the terms of the GNU General Public License as published by
33829 + * the Free Software Foundation; either version 2 of the License, or
33830 + * (at your option) any later version.
33831 + *
33832 + * This program is distributed in the hope that it will be useful,
33833 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33834 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33835 + * GNU General Public License for more details.
33836 + *
33837 + * You should have received a copy of the GNU General Public License
33838 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33839 + */
33840 +
33841 +/*
33842 + * workqueue for asynchronous/super-io operations
33843 + * todo: try new dredential scheme
33844 + */
33845 +
33846 +#include <linux/module.h>
33847 +#include "aufs.h"
33848 +
33849 +/* internal workqueue named AUFS_WKQ_NAME */
33850 +
33851 +static struct workqueue_struct *au_wkq;
33852 +
33853 +struct au_wkinfo {
33854 +       struct work_struct wk;
33855 +       struct kobject *kobj;
33856 +
33857 +       unsigned int flags; /* see wkq.h */
33858 +
33859 +       au_wkq_func_t func;
33860 +       void *args;
33861 +
33862 +       struct completion *comp;
33863 +};
33864 +
33865 +/* ---------------------------------------------------------------------- */
33866 +
33867 +static void wkq_func(struct work_struct *wk)
33868 +{
33869 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33870 +
33871 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
33872 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33873 +
33874 +       wkinfo->func(wkinfo->args);
33875 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
33876 +               complete(wkinfo->comp);
33877 +       else {
33878 +               kobject_put(wkinfo->kobj);
33879 +               module_put(THIS_MODULE); /* todo: ?? */
33880 +               kfree(wkinfo);
33881 +       }
33882 +}
33883 +
33884 +/*
33885 + * Since struct completion is large, try allocating it dynamically.
33886 + */
33887 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
33888 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
33889 +
33890 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33891 +{
33892 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33893 +       if (*comp) {
33894 +               init_completion(*comp);
33895 +               wkinfo->comp = *comp;
33896 +               return 0;
33897 +       }
33898 +       return -ENOMEM;
33899 +}
33900 +
33901 +static void au_wkq_comp_free(struct completion *comp)
33902 +{
33903 +       kfree(comp);
33904 +}
33905 +
33906 +#else
33907 +
33908 +/* no braces */
33909 +#define AuWkqCompDeclare(name) \
33910 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33911 +       struct completion *comp = &_ ## name
33912 +
33913 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33914 +{
33915 +       wkinfo->comp = *comp;
33916 +       return 0;
33917 +}
33918 +
33919 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33920 +{
33921 +       /* empty */
33922 +}
33923 +#endif /* 4KSTACKS */
33924 +
33925 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33926 +{
33927 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33928 +               if (au_wkq_test()) {
33929 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33930 +                               " due to a dead dir by UDBA?\n");
33931 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33932 +               }
33933 +       } else
33934 +               au_dbg_verify_kthread();
33935 +
33936 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33937 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33938 +               queue_work(au_wkq, &wkinfo->wk);
33939 +       } else {
33940 +               INIT_WORK(&wkinfo->wk, wkq_func);
33941 +               schedule_work(&wkinfo->wk);
33942 +       }
33943 +}
33944 +
33945 +/*
33946 + * Be careful. It is easy to make deadlock happen.
33947 + * processA: lock, wkq and wait
33948 + * processB: wkq and wait, lock in wkq
33949 + * --> deadlock
33950 + */
33951 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33952 +{
33953 +       int err;
33954 +       AuWkqCompDeclare(comp);
33955 +       struct au_wkinfo wkinfo = {
33956 +               .flags  = flags,
33957 +               .func   = func,
33958 +               .args   = args
33959 +       };
33960 +
33961 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33962 +       if (!err) {
33963 +               au_wkq_run(&wkinfo);
33964 +               /* no timeout, no interrupt */
33965 +               wait_for_completion(wkinfo.comp);
33966 +               au_wkq_comp_free(comp);
33967 +               destroy_work_on_stack(&wkinfo.wk);
33968 +       }
33969 +
33970 +       return err;
33971 +
33972 +}
33973 +
33974 +/*
33975 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33976 + * problem in a concurrent umounting.
33977 + */
33978 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33979 +                 unsigned int flags)
33980 +{
33981 +       int err;
33982 +       struct au_wkinfo *wkinfo;
33983 +
33984 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33985 +
33986 +       /*
33987 +        * wkq_func() must free this wkinfo.
33988 +        * it highly depends upon the implementation of workqueue.
33989 +        */
33990 +       err = 0;
33991 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33992 +       if (wkinfo) {
33993 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33994 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33995 +               wkinfo->func = func;
33996 +               wkinfo->args = args;
33997 +               wkinfo->comp = NULL;
33998 +               kobject_get(wkinfo->kobj);
33999 +               __module_get(THIS_MODULE); /* todo: ?? */
34000 +
34001 +               au_wkq_run(wkinfo);
34002 +       } else {
34003 +               err = -ENOMEM;
34004 +               au_nwt_done(&au_sbi(sb)->si_nowait);
34005 +       }
34006 +
34007 +       return err;
34008 +}
34009 +
34010 +/* ---------------------------------------------------------------------- */
34011 +
34012 +void au_nwt_init(struct au_nowait_tasks *nwt)
34013 +{
34014 +       atomic_set(&nwt->nw_len, 0);
34015 +       /* smp_mb(); */ /* atomic_set */
34016 +       init_waitqueue_head(&nwt->nw_wq);
34017 +}
34018 +
34019 +void au_wkq_fin(void)
34020 +{
34021 +       destroy_workqueue(au_wkq);
34022 +}
34023 +
34024 +int __init au_wkq_init(void)
34025 +{
34026 +       int err;
34027 +
34028 +       err = 0;
34029 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
34030 +       if (IS_ERR(au_wkq))
34031 +               err = PTR_ERR(au_wkq);
34032 +       else if (!au_wkq)
34033 +               err = -ENOMEM;
34034 +
34035 +       return err;
34036 +}
34037 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
34038 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
34039 +++ linux/fs/aufs/wkq.h 2017-07-29 12:14:25.906375514 +0200
34040 @@ -0,0 +1,93 @@
34041 +/*
34042 + * Copyright (C) 2005-2017 Junjiro R. Okajima
34043 + *
34044 + * This program, aufs is free software; you can redistribute it and/or modify
34045 + * it under the terms of the GNU General Public License as published by
34046 + * the Free Software Foundation; either version 2 of the License, or
34047 + * (at your option) any later version.
34048 + *
34049 + * This program is distributed in the hope that it will be useful,
34050 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34051 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34052 + * GNU General Public License for more details.
34053 + *
34054 + * You should have received a copy of the GNU General Public License
34055 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34056 + */
34057 +
34058 +/*
34059 + * workqueue for asynchronous/super-io operations
34060 + * todo: try new credentials management scheme
34061 + */
34062 +
34063 +#ifndef __AUFS_WKQ_H__
34064 +#define __AUFS_WKQ_H__
34065 +
34066 +#ifdef __KERNEL__
34067 +
34068 +#include <linux/percpu_counter.h>
34069 +
34070 +struct super_block;
34071 +
34072 +/* ---------------------------------------------------------------------- */
34073 +
34074 +/*
34075 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
34076 + */
34077 +struct au_nowait_tasks {
34078 +       atomic_t                nw_len;
34079 +       wait_queue_head_t       nw_wq;
34080 +};
34081 +
34082 +/* ---------------------------------------------------------------------- */
34083 +
34084 +typedef void (*au_wkq_func_t)(void *args);
34085 +
34086 +/* wkq flags */
34087 +#define AuWkq_WAIT     1
34088 +#define AuWkq_NEST     (1 << 1)
34089 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
34090 +#define au_fset_wkq(flags, name) \
34091 +       do { (flags) |= AuWkq_##name; } while (0)
34092 +#define au_fclr_wkq(flags, name) \
34093 +       do { (flags) &= ~AuWkq_##name; } while (0)
34094 +
34095 +#ifndef CONFIG_AUFS_HNOTIFY
34096 +#undef AuWkq_NEST
34097 +#define AuWkq_NEST     0
34098 +#endif
34099 +
34100 +/* wkq.c */
34101 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
34102 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
34103 +                 unsigned int flags);
34104 +void au_nwt_init(struct au_nowait_tasks *nwt);
34105 +int __init au_wkq_init(void);
34106 +void au_wkq_fin(void);
34107 +
34108 +/* ---------------------------------------------------------------------- */
34109 +
34110 +static inline int au_wkq_test(void)
34111 +{
34112 +       return current->flags & PF_WQ_WORKER;
34113 +}
34114 +
34115 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
34116 +{
34117 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
34118 +}
34119 +
34120 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
34121 +{
34122 +       if (atomic_dec_and_test(&nwt->nw_len))
34123 +               wake_up_all(&nwt->nw_wq);
34124 +}
34125 +
34126 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
34127 +{
34128 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
34129 +       return 0;
34130 +}
34131 +
34132 +#endif /* __KERNEL__ */
34133 +#endif /* __AUFS_WKQ_H__ */
34134 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
34135 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
34136 +++ linux/fs/aufs/xattr.c       2017-07-29 12:14:25.906375514 +0200
34137 @@ -0,0 +1,357 @@
34138 +/*
34139 + * Copyright (C) 2014-2017 Junjiro R. Okajima
34140 + *
34141 + * This program, aufs is free software; you can redistribute it and/or modify
34142 + * it under the terms of the GNU General Public License as published by
34143 + * the Free Software Foundation; either version 2 of the License, or
34144 + * (at your option) any later version.
34145 + *
34146 + * This program is distributed in the hope that it will be useful,
34147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34149 + * GNU General Public License for more details.
34150 + *
34151 + * You should have received a copy of the GNU General Public License
34152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34153 + */
34154 +
34155 +/*
34156 + * handling xattr functions
34157 + */
34158 +
34159 +#include <linux/fs.h>
34160 +#include <linux/posix_acl_xattr.h>
34161 +#include <linux/xattr.h>
34162 +#include "aufs.h"
34163 +
34164 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
34165 +{
34166 +       if (!ignore_flags)
34167 +               goto out;
34168 +       switch (err) {
34169 +       case -ENOMEM:
34170 +       case -EDQUOT:
34171 +               goto out;
34172 +       }
34173 +
34174 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
34175 +               err = 0;
34176 +               goto out;
34177 +       }
34178 +
34179 +#define cmp(brattr, prefix) do {                                       \
34180 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
34181 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
34182 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
34183 +                               err = 0;                                \
34184 +                       goto out;                                       \
34185 +               }                                                       \
34186 +       } while (0)
34187 +
34188 +       cmp(SEC, SECURITY);
34189 +       cmp(SYS, SYSTEM);
34190 +       cmp(TR, TRUSTED);
34191 +       cmp(USR, USER);
34192 +#undef cmp
34193 +
34194 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
34195 +               err = 0;
34196 +
34197 +out:
34198 +       return err;
34199 +}
34200 +
34201 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
34202 +
34203 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
34204 +                           char *name, char **buf, unsigned int ignore_flags,
34205 +                           unsigned int verbose)
34206 +{
34207 +       int err;
34208 +       ssize_t ssz;
34209 +       struct inode *h_idst;
34210 +
34211 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
34212 +       err = ssz;
34213 +       if (unlikely(err <= 0)) {
34214 +               if (err == -ENODATA
34215 +                   || (err == -EOPNOTSUPP
34216 +                       && ((ignore_flags & au_xattr_out_of_list)
34217 +                           || (au_test_nfs_noacl(d_inode(h_src))
34218 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
34219 +                                   || !strcmp(name,
34220 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
34221 +                           ))
34222 +                       err = 0;
34223 +               if (err && (verbose || au_debug_test()))
34224 +                       pr_err("%s, err %d\n", name, err);
34225 +               goto out;
34226 +       }
34227 +
34228 +       /* unlock it temporary */
34229 +       h_idst = d_inode(h_dst);
34230 +       inode_unlock(h_idst);
34231 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
34232 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
34233 +       if (unlikely(err)) {
34234 +               if (verbose || au_debug_test())
34235 +                       pr_err("%s, err %d\n", name, err);
34236 +               err = au_xattr_ignore(err, name, ignore_flags);
34237 +       }
34238 +
34239 +out:
34240 +       return err;
34241 +}
34242 +
34243 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
34244 +                 unsigned int verbose)
34245 +{
34246 +       int err, unlocked, acl_access, acl_default;
34247 +       ssize_t ssz;
34248 +       struct inode *h_isrc, *h_idst;
34249 +       char *value, *p, *o, *e;
34250 +
34251 +       /* try stopping to update the source inode while we are referencing */
34252 +       /* there should not be the parent-child relationship between them */
34253 +       h_isrc = d_inode(h_src);
34254 +       h_idst = d_inode(h_dst);
34255 +       inode_unlock(h_idst);
34256 +       inode_lock_nested(h_isrc, AuLsc_I_CHILD);
34257 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
34258 +       unlocked = 0;
34259 +
34260 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
34261 +       ssz = vfs_listxattr(h_src, NULL, 0);
34262 +       err = ssz;
34263 +       if (unlikely(err < 0)) {
34264 +               AuTraceErr(err);
34265 +               if (err == -ENODATA
34266 +                   || err == -EOPNOTSUPP)
34267 +                       err = 0;        /* ignore */
34268 +               goto out;
34269 +       }
34270 +
34271 +       err = 0;
34272 +       p = NULL;
34273 +       o = NULL;
34274 +       if (ssz) {
34275 +               err = -ENOMEM;
34276 +               p = kmalloc(ssz, GFP_NOFS);
34277 +               o = p;
34278 +               if (unlikely(!p))
34279 +                       goto out;
34280 +               err = vfs_listxattr(h_src, p, ssz);
34281 +       }
34282 +       inode_unlock(h_isrc);
34283 +       unlocked = 1;
34284 +       AuDbg("err %d, ssz %zd\n", err, ssz);
34285 +       if (unlikely(err < 0))
34286 +               goto out_free;
34287 +
34288 +       err = 0;
34289 +       e = p + ssz;
34290 +       value = NULL;
34291 +       acl_access = 0;
34292 +       acl_default = 0;
34293 +       while (!err && p < e) {
34294 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
34295 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
34296 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
34297 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
34298 +                                       - 1);
34299 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
34300 +                                      verbose);
34301 +               p += strlen(p) + 1;
34302 +       }
34303 +       AuTraceErr(err);
34304 +       ignore_flags |= au_xattr_out_of_list;
34305 +       if (!err && !acl_access) {
34306 +               err = au_do_cpup_xattr(h_dst, h_src,
34307 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
34308 +                                      ignore_flags, verbose);
34309 +               AuTraceErr(err);
34310 +       }
34311 +       if (!err && !acl_default) {
34312 +               err = au_do_cpup_xattr(h_dst, h_src,
34313 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
34314 +                                      ignore_flags, verbose);
34315 +               AuTraceErr(err);
34316 +       }
34317 +
34318 +       if (value)
34319 +               kfree(value);
34320 +
34321 +out_free:
34322 +       if (o)
34323 +               kfree(o);
34324 +out:
34325 +       if (!unlocked)
34326 +               inode_unlock(h_isrc);
34327 +       AuTraceErr(err);
34328 +       return err;
34329 +}
34330 +
34331 +/* ---------------------------------------------------------------------- */
34332 +
34333 +static int au_smack_reentering(struct super_block *sb)
34334 +{
34335 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
34336 +       /*
34337 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
34338 +        * i_op->getxattr(). ouch.
34339 +        */
34340 +       return si_pid_test(sb);
34341 +#else
34342 +       return 0;
34343 +#endif
34344 +}
34345 +
34346 +enum {
34347 +       AU_XATTR_LIST,
34348 +       AU_XATTR_GET
34349 +};
34350 +
34351 +struct au_lgxattr {
34352 +       int type;
34353 +       union {
34354 +               struct {
34355 +                       char    *list;
34356 +                       size_t  size;
34357 +               } list;
34358 +               struct {
34359 +                       const char      *name;
34360 +                       void            *value;
34361 +                       size_t          size;
34362 +               } get;
34363 +       } u;
34364 +};
34365 +
34366 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
34367 +{
34368 +       ssize_t err;
34369 +       int reenter;
34370 +       struct path h_path;
34371 +       struct super_block *sb;
34372 +
34373 +       sb = dentry->d_sb;
34374 +       reenter = au_smack_reentering(sb);
34375 +       if (!reenter) {
34376 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
34377 +               if (unlikely(err))
34378 +                       goto out;
34379 +       }
34380 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
34381 +       if (unlikely(err))
34382 +               goto out_si;
34383 +       if (unlikely(!h_path.dentry))
34384 +               /* illegally overlapped or something */
34385 +               goto out_di; /* pretending success */
34386 +
34387 +       /* always topmost entry only */
34388 +       switch (arg->type) {
34389 +       case AU_XATTR_LIST:
34390 +               err = vfs_listxattr(h_path.dentry,
34391 +                                   arg->u.list.list, arg->u.list.size);
34392 +               break;
34393 +       case AU_XATTR_GET:
34394 +               AuDebugOn(d_is_negative(h_path.dentry));
34395 +               err = vfs_getxattr(h_path.dentry,
34396 +                                  arg->u.get.name, arg->u.get.value,
34397 +                                  arg->u.get.size);
34398 +               break;
34399 +       }
34400 +
34401 +out_di:
34402 +       if (!reenter)
34403 +               di_read_unlock(dentry, AuLock_IR);
34404 +out_si:
34405 +       if (!reenter)
34406 +               si_read_unlock(sb);
34407 +out:
34408 +       AuTraceErr(err);
34409 +       return err;
34410 +}
34411 +
34412 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
34413 +{
34414 +       struct au_lgxattr arg = {
34415 +               .type = AU_XATTR_LIST,
34416 +               .u.list = {
34417 +                       .list   = list,
34418 +                       .size   = size
34419 +               },
34420 +       };
34421 +
34422 +       return au_lgxattr(dentry, &arg);
34423 +}
34424 +
34425 +static ssize_t au_getxattr(struct dentry *dentry,
34426 +                          struct inode *inode __maybe_unused,
34427 +                          const char *name, void *value, size_t size)
34428 +{
34429 +       struct au_lgxattr arg = {
34430 +               .type = AU_XATTR_GET,
34431 +               .u.get = {
34432 +                       .name   = name,
34433 +                       .value  = value,
34434 +                       .size   = size
34435 +               },
34436 +       };
34437 +
34438 +       return au_lgxattr(dentry, &arg);
34439 +}
34440 +
34441 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
34442 +                      const char *name, const void *value, size_t size,
34443 +                      int flags)
34444 +{
34445 +       struct au_sxattr arg = {
34446 +               .type = AU_XATTR_SET,
34447 +               .u.set = {
34448 +                       .name   = name,
34449 +                       .value  = value,
34450 +                       .size   = size,
34451 +                       .flags  = flags
34452 +               },
34453 +       };
34454 +
34455 +       return au_sxattr(dentry, inode, &arg);
34456 +}
34457 +
34458 +/* ---------------------------------------------------------------------- */
34459 +
34460 +static int au_xattr_get(const struct xattr_handler *handler,
34461 +                       struct dentry *dentry, struct inode *inode,
34462 +                       const char *name, void *buffer, size_t size)
34463 +{
34464 +       return au_getxattr(dentry, inode, name, buffer, size);
34465 +}
34466 +
34467 +static int au_xattr_set(const struct xattr_handler *handler,
34468 +                       struct dentry *dentry, struct inode *inode,
34469 +                       const char *name, const void *value, size_t size,
34470 +                       int flags)
34471 +{
34472 +       return au_setxattr(dentry, inode, name, value, size, flags);
34473 +}
34474 +
34475 +static const struct xattr_handler au_xattr_handler = {
34476 +       .name   = "",
34477 +       .prefix = "",
34478 +       .get    = au_xattr_get,
34479 +       .set    = au_xattr_set
34480 +};
34481 +
34482 +static const struct xattr_handler *au_xattr_handlers[] = {
34483 +#ifdef CONFIG_FS_POSIX_ACL
34484 +       &posix_acl_access_xattr_handler,
34485 +       &posix_acl_default_xattr_handler,
34486 +#endif
34487 +       &au_xattr_handler, /* must be last */
34488 +       NULL
34489 +};
34490 +
34491 +void au_xattr_init(struct super_block *sb)
34492 +{
34493 +       sb->s_xattr = au_xattr_handlers;
34494 +}
34495 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
34496 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
34497 +++ linux/fs/aufs/xino.c        2017-07-29 12:14:25.906375514 +0200
34498 @@ -0,0 +1,1415 @@
34499 +/*
34500 + * Copyright (C) 2005-2017 Junjiro R. Okajima
34501 + *
34502 + * This program, aufs is free software; you can redistribute it and/or modify
34503 + * it under the terms of the GNU General Public License as published by
34504 + * the Free Software Foundation; either version 2 of the License, or
34505 + * (at your option) any later version.
34506 + *
34507 + * This program is distributed in the hope that it will be useful,
34508 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34509 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34510 + * GNU General Public License for more details.
34511 + *
34512 + * You should have received a copy of the GNU General Public License
34513 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34514 + */
34515 +
34516 +/*
34517 + * external inode number translation table and bitmap
34518 + */
34519 +
34520 +#include <linux/seq_file.h>
34521 +#include <linux/statfs.h>
34522 +#include "aufs.h"
34523 +
34524 +/* todo: unnecessary to support mmap_sem since kernel-space? */
34525 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
34526 +                  loff_t *pos)
34527 +{
34528 +       ssize_t err;
34529 +       mm_segment_t oldfs;
34530 +       union {
34531 +               void *k;
34532 +               char __user *u;
34533 +       } buf;
34534 +
34535 +       buf.k = kbuf;
34536 +       oldfs = get_fs();
34537 +       set_fs(KERNEL_DS);
34538 +       do {
34539 +               /* todo: signal_pending? */
34540 +               err = func(file, buf.u, size, pos);
34541 +       } while (err == -EAGAIN || err == -EINTR);
34542 +       set_fs(oldfs);
34543 +
34544 +#if 0 /* reserved for future use */
34545 +       if (err > 0)
34546 +               fsnotify_access(file->f_path.dentry);
34547 +#endif
34548 +
34549 +       return err;
34550 +}
34551 +
34552 +/* ---------------------------------------------------------------------- */
34553 +
34554 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34555 +                              size_t size, loff_t *pos);
34556 +
34557 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
34558 +                             size_t size, loff_t *pos)
34559 +{
34560 +       ssize_t err;
34561 +       mm_segment_t oldfs;
34562 +       union {
34563 +               void *k;
34564 +               const char __user *u;
34565 +       } buf;
34566 +       int i;
34567 +       const int prevent_endless = 10;
34568 +
34569 +       i = 0;
34570 +       buf.k = kbuf;
34571 +       oldfs = get_fs();
34572 +       set_fs(KERNEL_DS);
34573 +       do {
34574 +               err = func(file, buf.u, size, pos);
34575 +               if (err == -EINTR
34576 +                   && !au_wkq_test()
34577 +                   && fatal_signal_pending(current)) {
34578 +                       set_fs(oldfs);
34579 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
34580 +                       BUG_ON(err == -EINTR);
34581 +                       oldfs = get_fs();
34582 +                       set_fs(KERNEL_DS);
34583 +               }
34584 +       } while (i++ < prevent_endless
34585 +                && (err == -EAGAIN || err == -EINTR));
34586 +       set_fs(oldfs);
34587 +
34588 +#if 0 /* reserved for future use */
34589 +       if (err > 0)
34590 +               fsnotify_modify(file->f_path.dentry);
34591 +#endif
34592 +
34593 +       return err;
34594 +}
34595 +
34596 +struct do_xino_fwrite_args {
34597 +       ssize_t *errp;
34598 +       vfs_writef_t func;
34599 +       struct file *file;
34600 +       void *buf;
34601 +       size_t size;
34602 +       loff_t *pos;
34603 +};
34604 +
34605 +static void call_do_xino_fwrite(void *args)
34606 +{
34607 +       struct do_xino_fwrite_args *a = args;
34608 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
34609 +}
34610 +
34611 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34612 +                              size_t size, loff_t *pos)
34613 +{
34614 +       ssize_t err;
34615 +       int wkq_err;
34616 +       struct do_xino_fwrite_args args = {
34617 +               .errp   = &err,
34618 +               .func   = func,
34619 +               .file   = file,
34620 +               .buf    = buf,
34621 +               .size   = size,
34622 +               .pos    = pos
34623 +       };
34624 +
34625 +       /*
34626 +        * it breaks RLIMIT_FSIZE and normal user's limit,
34627 +        * users should care about quota and real 'filesystem full.'
34628 +        */
34629 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
34630 +       if (unlikely(wkq_err))
34631 +               err = wkq_err;
34632 +
34633 +       return err;
34634 +}
34635 +
34636 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
34637 +                   size_t size, loff_t *pos)
34638 +{
34639 +       ssize_t err;
34640 +
34641 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
34642 +               lockdep_off();
34643 +               err = do_xino_fwrite(func, file, buf, size, pos);
34644 +               lockdep_on();
34645 +       } else
34646 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
34647 +
34648 +       return err;
34649 +}
34650 +
34651 +/* ---------------------------------------------------------------------- */
34652 +
34653 +/*
34654 + * create a new xinofile at the same place/path as @base_file.
34655 + */
34656 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
34657 +{
34658 +       struct file *file;
34659 +       struct dentry *base, *parent;
34660 +       struct inode *dir, *delegated;
34661 +       struct qstr *name;
34662 +       struct path path;
34663 +       int err;
34664 +
34665 +       base = base_file->f_path.dentry;
34666 +       parent = base->d_parent; /* dir inode is locked */
34667 +       dir = d_inode(parent);
34668 +       IMustLock(dir);
34669 +
34670 +       file = ERR_PTR(-EINVAL);
34671 +       name = &base->d_name;
34672 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
34673 +       if (IS_ERR(path.dentry)) {
34674 +               file = (void *)path.dentry;
34675 +               pr_err("%pd lookup err %ld\n",
34676 +                      base, PTR_ERR(path.dentry));
34677 +               goto out;
34678 +       }
34679 +
34680 +       /* no need to mnt_want_write() since we call dentry_open() later */
34681 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
34682 +       if (unlikely(err)) {
34683 +               file = ERR_PTR(err);
34684 +               pr_err("%pd create err %d\n", base, err);
34685 +               goto out_dput;
34686 +       }
34687 +
34688 +       path.mnt = base_file->f_path.mnt;
34689 +       file = vfsub_dentry_open(&path,
34690 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34691 +                                /* | __FMODE_NONOTIFY */);
34692 +       if (IS_ERR(file)) {
34693 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
34694 +               goto out_dput;
34695 +       }
34696 +
34697 +       delegated = NULL;
34698 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
34699 +       if (unlikely(err == -EWOULDBLOCK)) {
34700 +               pr_warn("cannot retry for NFSv4 delegation"
34701 +                       " for an internal unlink\n");
34702 +               iput(delegated);
34703 +       }
34704 +       if (unlikely(err)) {
34705 +               pr_err("%pd unlink err %d\n", base, err);
34706 +               goto out_fput;
34707 +       }
34708 +
34709 +       if (copy_src) {
34710 +               /* no one can touch copy_src xino */
34711 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
34712 +               if (unlikely(err)) {
34713 +                       pr_err("%pd copy err %d\n", base, err);
34714 +                       goto out_fput;
34715 +               }
34716 +       }
34717 +       goto out_dput; /* success */
34718 +
34719 +out_fput:
34720 +       fput(file);
34721 +       file = ERR_PTR(err);
34722 +out_dput:
34723 +       dput(path.dentry);
34724 +out:
34725 +       return file;
34726 +}
34727 +
34728 +struct au_xino_lock_dir {
34729 +       struct au_hinode *hdir;
34730 +       struct dentry *parent;
34731 +       struct inode *dir;
34732 +};
34733 +
34734 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
34735 +                            struct au_xino_lock_dir *ldir)
34736 +{
34737 +       aufs_bindex_t brid, bindex;
34738 +
34739 +       ldir->hdir = NULL;
34740 +       bindex = -1;
34741 +       brid = au_xino_brid(sb);
34742 +       if (brid >= 0)
34743 +               bindex = au_br_index(sb, brid);
34744 +       if (bindex >= 0) {
34745 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
34746 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
34747 +       } else {
34748 +               ldir->parent = dget_parent(xino->f_path.dentry);
34749 +               ldir->dir = d_inode(ldir->parent);
34750 +               inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
34751 +       }
34752 +}
34753 +
34754 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34755 +{
34756 +       if (ldir->hdir)
34757 +               au_hn_inode_unlock(ldir->hdir);
34758 +       else {
34759 +               inode_unlock(ldir->dir);
34760 +               dput(ldir->parent);
34761 +       }
34762 +}
34763 +
34764 +/* ---------------------------------------------------------------------- */
34765 +
34766 +/* trucate xino files asynchronously */
34767 +
34768 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34769 +{
34770 +       int err;
34771 +       unsigned long jiffy;
34772 +       blkcnt_t blocks;
34773 +       aufs_bindex_t bi, bbot;
34774 +       struct kstatfs *st;
34775 +       struct au_branch *br;
34776 +       struct file *new_xino, *file;
34777 +       struct super_block *h_sb;
34778 +       struct au_xino_lock_dir ldir;
34779 +
34780 +       err = -ENOMEM;
34781 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34782 +       if (unlikely(!st))
34783 +               goto out;
34784 +
34785 +       err = -EINVAL;
34786 +       bbot = au_sbbot(sb);
34787 +       if (unlikely(bindex < 0 || bbot < bindex))
34788 +               goto out_st;
34789 +       br = au_sbr(sb, bindex);
34790 +       file = br->br_xino.xi_file;
34791 +       if (!file)
34792 +               goto out_st;
34793 +
34794 +       err = vfs_statfs(&file->f_path, st);
34795 +       if (unlikely(err))
34796 +               AuErr1("statfs err %d, ignored\n", err);
34797 +       jiffy = jiffies;
34798 +       blocks = file_inode(file)->i_blocks;
34799 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34800 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
34801 +
34802 +       au_xino_lock_dir(sb, file, &ldir);
34803 +       /* mnt_want_write() is unnecessary here */
34804 +       new_xino = au_xino_create2(file, file);
34805 +       au_xino_unlock_dir(&ldir);
34806 +       err = PTR_ERR(new_xino);
34807 +       if (IS_ERR(new_xino)) {
34808 +               pr_err("err %d, ignored\n", err);
34809 +               goto out_st;
34810 +       }
34811 +       err = 0;
34812 +       fput(file);
34813 +       br->br_xino.xi_file = new_xino;
34814 +
34815 +       h_sb = au_br_sb(br);
34816 +       for (bi = 0; bi <= bbot; bi++) {
34817 +               if (unlikely(bi == bindex))
34818 +                       continue;
34819 +               br = au_sbr(sb, bi);
34820 +               if (au_br_sb(br) != h_sb)
34821 +                       continue;
34822 +
34823 +               fput(br->br_xino.xi_file);
34824 +               br->br_xino.xi_file = new_xino;
34825 +               get_file(new_xino);
34826 +       }
34827 +
34828 +       err = vfs_statfs(&new_xino->f_path, st);
34829 +       if (!err) {
34830 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34831 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
34832 +                       st->f_bfree, st->f_blocks);
34833 +               if (file_inode(new_xino)->i_blocks < blocks)
34834 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
34835 +       } else
34836 +               AuErr1("statfs err %d, ignored\n", err);
34837 +
34838 +out_st:
34839 +       kfree(st);
34840 +out:
34841 +       return err;
34842 +}
34843 +
34844 +struct xino_do_trunc_args {
34845 +       struct super_block *sb;
34846 +       struct au_branch *br;
34847 +};
34848 +
34849 +static void xino_do_trunc(void *_args)
34850 +{
34851 +       struct xino_do_trunc_args *args = _args;
34852 +       struct super_block *sb;
34853 +       struct au_branch *br;
34854 +       struct inode *dir;
34855 +       int err;
34856 +       aufs_bindex_t bindex;
34857 +
34858 +       err = 0;
34859 +       sb = args->sb;
34860 +       dir = d_inode(sb->s_root);
34861 +       br = args->br;
34862 +
34863 +       si_noflush_write_lock(sb);
34864 +       ii_read_lock_parent(dir);
34865 +       bindex = au_br_index(sb, br->br_id);
34866 +       err = au_xino_trunc(sb, bindex);
34867 +       ii_read_unlock(dir);
34868 +       if (unlikely(err))
34869 +               pr_warn("err b%d, (%d)\n", bindex, err);
34870 +       atomic_dec(&br->br_xino_running);
34871 +       au_br_put(br);
34872 +       si_write_unlock(sb);
34873 +       au_nwt_done(&au_sbi(sb)->si_nowait);
34874 +       kfree(args);
34875 +}
34876 +
34877 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34878 +{
34879 +       int err;
34880 +       struct kstatfs st;
34881 +       struct au_sbinfo *sbinfo;
34882 +
34883 +       /* todo: si_xino_expire and the ratio should be customizable */
34884 +       sbinfo = au_sbi(sb);
34885 +       if (time_before(jiffies,
34886 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34887 +               return 0;
34888 +
34889 +       /* truncation border */
34890 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34891 +       if (unlikely(err)) {
34892 +               AuErr1("statfs err %d, ignored\n", err);
34893 +               return 0;
34894 +       }
34895 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34896 +               return 0;
34897 +
34898 +       return 1;
34899 +}
34900 +
34901 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34902 +{
34903 +       struct xino_do_trunc_args *args;
34904 +       int wkq_err;
34905 +
34906 +       if (!xino_trunc_test(sb, br))
34907 +               return;
34908 +
34909 +       if (atomic_inc_return(&br->br_xino_running) > 1)
34910 +               goto out;
34911 +
34912 +       /* lock and kfree() will be called in trunc_xino() */
34913 +       args = kmalloc(sizeof(*args), GFP_NOFS);
34914 +       if (unlikely(!args)) {
34915 +               AuErr1("no memory\n");
34916 +               goto out;
34917 +       }
34918 +
34919 +       au_br_get(br);
34920 +       args->sb = sb;
34921 +       args->br = br;
34922 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
34923 +       if (!wkq_err)
34924 +               return; /* success */
34925 +
34926 +       pr_err("wkq %d\n", wkq_err);
34927 +       au_br_put(br);
34928 +       kfree(args);
34929 +
34930 +out:
34931 +       atomic_dec(&br->br_xino_running);
34932 +}
34933 +
34934 +/* ---------------------------------------------------------------------- */
34935 +
34936 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
34937 +                           ino_t h_ino, ino_t ino)
34938 +{
34939 +       loff_t pos;
34940 +       ssize_t sz;
34941 +
34942 +       pos = h_ino;
34943 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34944 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34945 +               return -EFBIG;
34946 +       }
34947 +       pos *= sizeof(ino);
34948 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34949 +       if (sz == sizeof(ino))
34950 +               return 0; /* success */
34951 +
34952 +       AuIOErr("write failed (%zd)\n", sz);
34953 +       return -EIO;
34954 +}
34955 +
34956 +/*
34957 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34958 + * at the position of @h_ino.
34959 + * even if @ino is zero, it is written to the xinofile and means no entry.
34960 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34961 + * try truncating it.
34962 + */
34963 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34964 +                 ino_t ino)
34965 +{
34966 +       int err;
34967 +       unsigned int mnt_flags;
34968 +       struct au_branch *br;
34969 +
34970 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34971 +                    || ((loff_t)-1) > 0);
34972 +       SiMustAnyLock(sb);
34973 +
34974 +       mnt_flags = au_mntflags(sb);
34975 +       if (!au_opt_test(mnt_flags, XINO))
34976 +               return 0;
34977 +
34978 +       br = au_sbr(sb, bindex);
34979 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34980 +                              h_ino, ino);
34981 +       if (!err) {
34982 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34983 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34984 +                       xino_try_trunc(sb, br);
34985 +               return 0; /* success */
34986 +       }
34987 +
34988 +       AuIOErr("write failed (%d)\n", err);
34989 +       return -EIO;
34990 +}
34991 +
34992 +/* ---------------------------------------------------------------------- */
34993 +
34994 +/* aufs inode number bitmap */
34995 +
34996 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34997 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34998 +{
34999 +       ino_t ino;
35000 +
35001 +       AuDebugOn(bit < 0 || page_bits <= bit);
35002 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
35003 +       return ino;
35004 +}
35005 +
35006 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
35007 +{
35008 +       AuDebugOn(ino < AUFS_FIRST_INO);
35009 +       ino -= AUFS_FIRST_INO;
35010 +       *pindex = ino / page_bits;
35011 +       *bit = ino % page_bits;
35012 +}
35013 +
35014 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
35015 +{
35016 +       int err;
35017 +       loff_t pos;
35018 +       ssize_t sz;
35019 +       struct au_sbinfo *sbinfo;
35020 +       struct file *xib;
35021 +       unsigned long *p;
35022 +
35023 +       sbinfo = au_sbi(sb);
35024 +       MtxMustLock(&sbinfo->si_xib_mtx);
35025 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
35026 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
35027 +
35028 +       if (pindex == sbinfo->si_xib_last_pindex)
35029 +               return 0;
35030 +
35031 +       xib = sbinfo->si_xib;
35032 +       p = sbinfo->si_xib_buf;
35033 +       pos = sbinfo->si_xib_last_pindex;
35034 +       pos *= PAGE_SIZE;
35035 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
35036 +       if (unlikely(sz != PAGE_SIZE))
35037 +               goto out;
35038 +
35039 +       pos = pindex;
35040 +       pos *= PAGE_SIZE;
35041 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
35042 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
35043 +       else {
35044 +               memset(p, 0, PAGE_SIZE);
35045 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
35046 +       }
35047 +       if (sz == PAGE_SIZE) {
35048 +               sbinfo->si_xib_last_pindex = pindex;
35049 +               return 0; /* success */
35050 +       }
35051 +
35052 +out:
35053 +       AuIOErr1("write failed (%zd)\n", sz);
35054 +       err = sz;
35055 +       if (sz >= 0)
35056 +               err = -EIO;
35057 +       return err;
35058 +}
35059 +
35060 +/* ---------------------------------------------------------------------- */
35061 +
35062 +static void au_xib_clear_bit(struct inode *inode)
35063 +{
35064 +       int err, bit;
35065 +       unsigned long pindex;
35066 +       struct super_block *sb;
35067 +       struct au_sbinfo *sbinfo;
35068 +
35069 +       AuDebugOn(inode->i_nlink);
35070 +
35071 +       sb = inode->i_sb;
35072 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
35073 +       AuDebugOn(page_bits <= bit);
35074 +       sbinfo = au_sbi(sb);
35075 +       mutex_lock(&sbinfo->si_xib_mtx);
35076 +       err = xib_pindex(sb, pindex);
35077 +       if (!err) {
35078 +               clear_bit(bit, sbinfo->si_xib_buf);
35079 +               sbinfo->si_xib_next_bit = bit;
35080 +       }
35081 +       mutex_unlock(&sbinfo->si_xib_mtx);
35082 +}
35083 +
35084 +/* for s_op->delete_inode() */
35085 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
35086 +{
35087 +       int err;
35088 +       unsigned int mnt_flags;
35089 +       aufs_bindex_t bindex, bbot, bi;
35090 +       unsigned char try_trunc;
35091 +       struct au_iinfo *iinfo;
35092 +       struct super_block *sb;
35093 +       struct au_hinode *hi;
35094 +       struct inode *h_inode;
35095 +       struct au_branch *br;
35096 +       vfs_writef_t xwrite;
35097 +
35098 +       AuDebugOn(au_is_bad_inode(inode));
35099 +
35100 +       sb = inode->i_sb;
35101 +       mnt_flags = au_mntflags(sb);
35102 +       if (!au_opt_test(mnt_flags, XINO)
35103 +           || inode->i_ino == AUFS_ROOT_INO)
35104 +               return;
35105 +
35106 +       if (unlinked) {
35107 +               au_xigen_inc(inode);
35108 +               au_xib_clear_bit(inode);
35109 +       }
35110 +
35111 +       iinfo = au_ii(inode);
35112 +       bindex = iinfo->ii_btop;
35113 +       if (bindex < 0)
35114 +               return;
35115 +
35116 +       xwrite = au_sbi(sb)->si_xwrite;
35117 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
35118 +       hi = au_hinode(iinfo, bindex);
35119 +       bbot = iinfo->ii_bbot;
35120 +       for (; bindex <= bbot; bindex++, hi++) {
35121 +               h_inode = hi->hi_inode;
35122 +               if (!h_inode
35123 +                   || (!unlinked && h_inode->i_nlink))
35124 +                       continue;
35125 +
35126 +               /* inode may not be revalidated */
35127 +               bi = au_br_index(sb, hi->hi_id);
35128 +               if (bi < 0)
35129 +                       continue;
35130 +
35131 +               br = au_sbr(sb, bi);
35132 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
35133 +                                      h_inode->i_ino, /*ino*/0);
35134 +               if (!err && try_trunc
35135 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
35136 +                       xino_try_trunc(sb, br);
35137 +       }
35138 +}
35139 +
35140 +/* get an unused inode number from bitmap */
35141 +ino_t au_xino_new_ino(struct super_block *sb)
35142 +{
35143 +       ino_t ino;
35144 +       unsigned long *p, pindex, ul, pend;
35145 +       struct au_sbinfo *sbinfo;
35146 +       struct file *file;
35147 +       int free_bit, err;
35148 +
35149 +       if (!au_opt_test(au_mntflags(sb), XINO))
35150 +               return iunique(sb, AUFS_FIRST_INO);
35151 +
35152 +       sbinfo = au_sbi(sb);
35153 +       mutex_lock(&sbinfo->si_xib_mtx);
35154 +       p = sbinfo->si_xib_buf;
35155 +       free_bit = sbinfo->si_xib_next_bit;
35156 +       if (free_bit < page_bits && !test_bit(free_bit, p))
35157 +               goto out; /* success */
35158 +       free_bit = find_first_zero_bit(p, page_bits);
35159 +       if (free_bit < page_bits)
35160 +               goto out; /* success */
35161 +
35162 +       pindex = sbinfo->si_xib_last_pindex;
35163 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
35164 +               err = xib_pindex(sb, ul);
35165 +               if (unlikely(err))
35166 +                       goto out_err;
35167 +               free_bit = find_first_zero_bit(p, page_bits);
35168 +               if (free_bit < page_bits)
35169 +                       goto out; /* success */
35170 +       }
35171 +
35172 +       file = sbinfo->si_xib;
35173 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
35174 +       for (ul = pindex + 1; ul <= pend; ul++) {
35175 +               err = xib_pindex(sb, ul);
35176 +               if (unlikely(err))
35177 +                       goto out_err;
35178 +               free_bit = find_first_zero_bit(p, page_bits);
35179 +               if (free_bit < page_bits)
35180 +                       goto out; /* success */
35181 +       }
35182 +       BUG();
35183 +
35184 +out:
35185 +       set_bit(free_bit, p);
35186 +       sbinfo->si_xib_next_bit = free_bit + 1;
35187 +       pindex = sbinfo->si_xib_last_pindex;
35188 +       mutex_unlock(&sbinfo->si_xib_mtx);
35189 +       ino = xib_calc_ino(pindex, free_bit);
35190 +       AuDbg("i%lu\n", (unsigned long)ino);
35191 +       return ino;
35192 +out_err:
35193 +       mutex_unlock(&sbinfo->si_xib_mtx);
35194 +       AuDbg("i0\n");
35195 +       return 0;
35196 +}
35197 +
35198 +/*
35199 + * read @ino from xinofile for the specified branch{@sb, @bindex}
35200 + * at the position of @h_ino.
35201 + * if @ino does not exist and @do_new is true, get new one.
35202 + */
35203 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
35204 +                ino_t *ino)
35205 +{
35206 +       int err;
35207 +       ssize_t sz;
35208 +       loff_t pos;
35209 +       struct file *file;
35210 +       struct au_sbinfo *sbinfo;
35211 +
35212 +       *ino = 0;
35213 +       if (!au_opt_test(au_mntflags(sb), XINO))
35214 +               return 0; /* no xino */
35215 +
35216 +       err = 0;
35217 +       sbinfo = au_sbi(sb);
35218 +       pos = h_ino;
35219 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
35220 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
35221 +               return -EFBIG;
35222 +       }
35223 +       pos *= sizeof(*ino);
35224 +
35225 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
35226 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
35227 +               return 0; /* no ino */
35228 +
35229 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
35230 +       if (sz == sizeof(*ino))
35231 +               return 0; /* success */
35232 +
35233 +       err = sz;
35234 +       if (unlikely(sz >= 0)) {
35235 +               err = -EIO;
35236 +               AuIOErr("xino read error (%zd)\n", sz);
35237 +       }
35238 +
35239 +       return err;
35240 +}
35241 +
35242 +/* ---------------------------------------------------------------------- */
35243 +
35244 +/* create and set a new xino file */
35245 +
35246 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
35247 +{
35248 +       struct file *file;
35249 +       struct dentry *h_parent, *d;
35250 +       struct inode *h_dir, *inode;
35251 +       int err;
35252 +
35253 +       /*
35254 +        * at mount-time, and the xino file is the default path,
35255 +        * hnotify is disabled so we have no notify events to ignore.
35256 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
35257 +        */
35258 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
35259 +                              /* | __FMODE_NONOTIFY */,
35260 +                              S_IRUGO | S_IWUGO);
35261 +       if (IS_ERR(file)) {
35262 +               if (!silent)
35263 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
35264 +               return file;
35265 +       }
35266 +
35267 +       /* keep file count */
35268 +       err = 0;
35269 +       inode = file_inode(file);
35270 +       h_parent = dget_parent(file->f_path.dentry);
35271 +       h_dir = d_inode(h_parent);
35272 +       inode_lock_nested(h_dir, AuLsc_I_PARENT);
35273 +       /* mnt_want_write() is unnecessary here */
35274 +       /* no delegation since it is just created */
35275 +       if (inode->i_nlink)
35276 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
35277 +                                  /*force*/0);
35278 +       inode_unlock(h_dir);
35279 +       dput(h_parent);
35280 +       if (unlikely(err)) {
35281 +               if (!silent)
35282 +                       pr_err("unlink %s(%d)\n", fname, err);
35283 +               goto out;
35284 +       }
35285 +
35286 +       err = -EINVAL;
35287 +       d = file->f_path.dentry;
35288 +       if (unlikely(sb == d->d_sb)) {
35289 +               if (!silent)
35290 +                       pr_err("%s must be outside\n", fname);
35291 +               goto out;
35292 +       }
35293 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
35294 +               if (!silent)
35295 +                       pr_err("xino doesn't support %s(%s)\n",
35296 +                              fname, au_sbtype(d->d_sb));
35297 +               goto out;
35298 +       }
35299 +       return file; /* success */
35300 +
35301 +out:
35302 +       fput(file);
35303 +       file = ERR_PTR(err);
35304 +       return file;
35305 +}
35306 +
35307 +/*
35308 + * find another branch who is on the same filesystem of the specified
35309 + * branch{@btgt}. search until @bbot.
35310 + */
35311 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
35312 +                       aufs_bindex_t bbot)
35313 +{
35314 +       aufs_bindex_t bindex;
35315 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
35316 +
35317 +       for (bindex = 0; bindex < btgt; bindex++)
35318 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
35319 +                       return bindex;
35320 +       for (bindex++; bindex <= bbot; bindex++)
35321 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
35322 +                       return bindex;
35323 +       return -1;
35324 +}
35325 +
35326 +/* ---------------------------------------------------------------------- */
35327 +
35328 +/*
35329 + * initialize the xinofile for the specified branch @br
35330 + * at the place/path where @base_file indicates.
35331 + * test whether another branch is on the same filesystem or not,
35332 + * if @do_test is true.
35333 + */
35334 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
35335 +              struct file *base_file, int do_test)
35336 +{
35337 +       int err;
35338 +       ino_t ino;
35339 +       aufs_bindex_t bbot, bindex;
35340 +       struct au_branch *shared_br, *b;
35341 +       struct file *file;
35342 +       struct super_block *tgt_sb;
35343 +
35344 +       shared_br = NULL;
35345 +       bbot = au_sbbot(sb);
35346 +       if (do_test) {
35347 +               tgt_sb = au_br_sb(br);
35348 +               for (bindex = 0; bindex <= bbot; bindex++) {
35349 +                       b = au_sbr(sb, bindex);
35350 +                       if (tgt_sb == au_br_sb(b)) {
35351 +                               shared_br = b;
35352 +                               break;
35353 +                       }
35354 +               }
35355 +       }
35356 +
35357 +       if (!shared_br || !shared_br->br_xino.xi_file) {
35358 +               struct au_xino_lock_dir ldir;
35359 +
35360 +               au_xino_lock_dir(sb, base_file, &ldir);
35361 +               /* mnt_want_write() is unnecessary here */
35362 +               file = au_xino_create2(base_file, NULL);
35363 +               au_xino_unlock_dir(&ldir);
35364 +               err = PTR_ERR(file);
35365 +               if (IS_ERR(file))
35366 +                       goto out;
35367 +               br->br_xino.xi_file = file;
35368 +       } else {
35369 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
35370 +               get_file(br->br_xino.xi_file);
35371 +       }
35372 +
35373 +       ino = AUFS_ROOT_INO;
35374 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
35375 +                              h_ino, ino);
35376 +       if (unlikely(err)) {
35377 +               fput(br->br_xino.xi_file);
35378 +               br->br_xino.xi_file = NULL;
35379 +       }
35380 +
35381 +out:
35382 +       return err;
35383 +}
35384 +
35385 +/* ---------------------------------------------------------------------- */
35386 +
35387 +/* trucate a xino bitmap file */
35388 +
35389 +/* todo: slow */
35390 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
35391 +{
35392 +       int err, bit;
35393 +       ssize_t sz;
35394 +       unsigned long pindex;
35395 +       loff_t pos, pend;
35396 +       struct au_sbinfo *sbinfo;
35397 +       vfs_readf_t func;
35398 +       ino_t *ino;
35399 +       unsigned long *p;
35400 +
35401 +       err = 0;
35402 +       sbinfo = au_sbi(sb);
35403 +       MtxMustLock(&sbinfo->si_xib_mtx);
35404 +       p = sbinfo->si_xib_buf;
35405 +       func = sbinfo->si_xread;
35406 +       pend = vfsub_f_size_read(file);
35407 +       pos = 0;
35408 +       while (pos < pend) {
35409 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
35410 +               err = sz;
35411 +               if (unlikely(sz <= 0))
35412 +                       goto out;
35413 +
35414 +               err = 0;
35415 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
35416 +                       if (unlikely(*ino < AUFS_FIRST_INO))
35417 +                               continue;
35418 +
35419 +                       xib_calc_bit(*ino, &pindex, &bit);
35420 +                       AuDebugOn(page_bits <= bit);
35421 +                       err = xib_pindex(sb, pindex);
35422 +                       if (!err)
35423 +                               set_bit(bit, p);
35424 +                       else
35425 +                               goto out;
35426 +               }
35427 +       }
35428 +
35429 +out:
35430 +       return err;
35431 +}
35432 +
35433 +static int xib_restore(struct super_block *sb)
35434 +{
35435 +       int err;
35436 +       aufs_bindex_t bindex, bbot;
35437 +       void *page;
35438 +
35439 +       err = -ENOMEM;
35440 +       page = (void *)__get_free_page(GFP_NOFS);
35441 +       if (unlikely(!page))
35442 +               goto out;
35443 +
35444 +       err = 0;
35445 +       bbot = au_sbbot(sb);
35446 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
35447 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
35448 +                       err = do_xib_restore
35449 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
35450 +               else
35451 +                       AuDbg("b%d\n", bindex);
35452 +       free_page((unsigned long)page);
35453 +
35454 +out:
35455 +       return err;
35456 +}
35457 +
35458 +int au_xib_trunc(struct super_block *sb)
35459 +{
35460 +       int err;
35461 +       ssize_t sz;
35462 +       loff_t pos;
35463 +       struct au_xino_lock_dir ldir;
35464 +       struct au_sbinfo *sbinfo;
35465 +       unsigned long *p;
35466 +       struct file *file;
35467 +
35468 +       SiMustWriteLock(sb);
35469 +
35470 +       err = 0;
35471 +       sbinfo = au_sbi(sb);
35472 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
35473 +               goto out;
35474 +
35475 +       file = sbinfo->si_xib;
35476 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
35477 +               goto out;
35478 +
35479 +       au_xino_lock_dir(sb, file, &ldir);
35480 +       /* mnt_want_write() is unnecessary here */
35481 +       file = au_xino_create2(sbinfo->si_xib, NULL);
35482 +       au_xino_unlock_dir(&ldir);
35483 +       err = PTR_ERR(file);
35484 +       if (IS_ERR(file))
35485 +               goto out;
35486 +       fput(sbinfo->si_xib);
35487 +       sbinfo->si_xib = file;
35488 +
35489 +       p = sbinfo->si_xib_buf;
35490 +       memset(p, 0, PAGE_SIZE);
35491 +       pos = 0;
35492 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
35493 +       if (unlikely(sz != PAGE_SIZE)) {
35494 +               err = sz;
35495 +               AuIOErr("err %d\n", err);
35496 +               if (sz >= 0)
35497 +                       err = -EIO;
35498 +               goto out;
35499 +       }
35500 +
35501 +       mutex_lock(&sbinfo->si_xib_mtx);
35502 +       /* mnt_want_write() is unnecessary here */
35503 +       err = xib_restore(sb);
35504 +       mutex_unlock(&sbinfo->si_xib_mtx);
35505 +
35506 +out:
35507 +       return err;
35508 +}
35509 +
35510 +/* ---------------------------------------------------------------------- */
35511 +
35512 +/*
35513 + * xino mount option handlers
35514 + */
35515 +
35516 +/* xino bitmap */
35517 +static void xino_clear_xib(struct super_block *sb)
35518 +{
35519 +       struct au_sbinfo *sbinfo;
35520 +
35521 +       SiMustWriteLock(sb);
35522 +
35523 +       sbinfo = au_sbi(sb);
35524 +       sbinfo->si_xread = NULL;
35525 +       sbinfo->si_xwrite = NULL;
35526 +       if (sbinfo->si_xib)
35527 +               fput(sbinfo->si_xib);
35528 +       sbinfo->si_xib = NULL;
35529 +       if (sbinfo->si_xib_buf)
35530 +               free_page((unsigned long)sbinfo->si_xib_buf);
35531 +       sbinfo->si_xib_buf = NULL;
35532 +}
35533 +
35534 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
35535 +{
35536 +       int err;
35537 +       loff_t pos;
35538 +       struct au_sbinfo *sbinfo;
35539 +       struct file *file;
35540 +
35541 +       SiMustWriteLock(sb);
35542 +
35543 +       sbinfo = au_sbi(sb);
35544 +       file = au_xino_create2(base, sbinfo->si_xib);
35545 +       err = PTR_ERR(file);
35546 +       if (IS_ERR(file))
35547 +               goto out;
35548 +       if (sbinfo->si_xib)
35549 +               fput(sbinfo->si_xib);
35550 +       sbinfo->si_xib = file;
35551 +       sbinfo->si_xread = vfs_readf(file);
35552 +       sbinfo->si_xwrite = vfs_writef(file);
35553 +
35554 +       err = -ENOMEM;
35555 +       if (!sbinfo->si_xib_buf)
35556 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
35557 +       if (unlikely(!sbinfo->si_xib_buf))
35558 +               goto out_unset;
35559 +
35560 +       sbinfo->si_xib_last_pindex = 0;
35561 +       sbinfo->si_xib_next_bit = 0;
35562 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
35563 +               pos = 0;
35564 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
35565 +                                 PAGE_SIZE, &pos);
35566 +               if (unlikely(err != PAGE_SIZE))
35567 +                       goto out_free;
35568 +       }
35569 +       err = 0;
35570 +       goto out; /* success */
35571 +
35572 +out_free:
35573 +       if (sbinfo->si_xib_buf)
35574 +               free_page((unsigned long)sbinfo->si_xib_buf);
35575 +       sbinfo->si_xib_buf = NULL;
35576 +       if (err >= 0)
35577 +               err = -EIO;
35578 +out_unset:
35579 +       fput(sbinfo->si_xib);
35580 +       sbinfo->si_xib = NULL;
35581 +       sbinfo->si_xread = NULL;
35582 +       sbinfo->si_xwrite = NULL;
35583 +out:
35584 +       return err;
35585 +}
35586 +
35587 +/* xino for each branch */
35588 +static void xino_clear_br(struct super_block *sb)
35589 +{
35590 +       aufs_bindex_t bindex, bbot;
35591 +       struct au_branch *br;
35592 +
35593 +       bbot = au_sbbot(sb);
35594 +       for (bindex = 0; bindex <= bbot; bindex++) {
35595 +               br = au_sbr(sb, bindex);
35596 +               if (!br || !br->br_xino.xi_file)
35597 +                       continue;
35598 +
35599 +               fput(br->br_xino.xi_file);
35600 +               br->br_xino.xi_file = NULL;
35601 +       }
35602 +}
35603 +
35604 +static int au_xino_set_br(struct super_block *sb, struct file *base)
35605 +{
35606 +       int err;
35607 +       ino_t ino;
35608 +       aufs_bindex_t bindex, bbot, bshared;
35609 +       struct {
35610 +               struct file *old, *new;
35611 +       } *fpair, *p;
35612 +       struct au_branch *br;
35613 +       struct inode *inode;
35614 +       vfs_writef_t writef;
35615 +
35616 +       SiMustWriteLock(sb);
35617 +
35618 +       err = -ENOMEM;
35619 +       bbot = au_sbbot(sb);
35620 +       fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
35621 +       if (unlikely(!fpair))
35622 +               goto out;
35623 +
35624 +       inode = d_inode(sb->s_root);
35625 +       ino = AUFS_ROOT_INO;
35626 +       writef = au_sbi(sb)->si_xwrite;
35627 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
35628 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
35629 +               if (bshared >= 0) {
35630 +                       /* shared xino */
35631 +                       *p = fpair[bshared];
35632 +                       get_file(p->new);
35633 +               }
35634 +
35635 +               if (!p->new) {
35636 +                       /* new xino */
35637 +                       br = au_sbr(sb, bindex);
35638 +                       p->old = br->br_xino.xi_file;
35639 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
35640 +                       err = PTR_ERR(p->new);
35641 +                       if (IS_ERR(p->new)) {
35642 +                               p->new = NULL;
35643 +                               goto out_pair;
35644 +                       }
35645 +               }
35646 +
35647 +               err = au_xino_do_write(writef, p->new,
35648 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
35649 +               if (unlikely(err))
35650 +                       goto out_pair;
35651 +       }
35652 +
35653 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
35654 +               br = au_sbr(sb, bindex);
35655 +               if (br->br_xino.xi_file)
35656 +                       fput(br->br_xino.xi_file);
35657 +               get_file(p->new);
35658 +               br->br_xino.xi_file = p->new;
35659 +       }
35660 +
35661 +out_pair:
35662 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
35663 +               if (p->new)
35664 +                       fput(p->new);
35665 +               else
35666 +                       break;
35667 +       kfree(fpair);
35668 +out:
35669 +       return err;
35670 +}
35671 +
35672 +void au_xino_clr(struct super_block *sb)
35673 +{
35674 +       struct au_sbinfo *sbinfo;
35675 +
35676 +       au_xigen_clr(sb);
35677 +       xino_clear_xib(sb);
35678 +       xino_clear_br(sb);
35679 +       sbinfo = au_sbi(sb);
35680 +       /* lvalue, do not call au_mntflags() */
35681 +       au_opt_clr(sbinfo->si_mntflags, XINO);
35682 +}
35683 +
35684 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
35685 +{
35686 +       int err, skip;
35687 +       struct dentry *parent, *cur_parent;
35688 +       struct qstr *dname, *cur_name;
35689 +       struct file *cur_xino;
35690 +       struct inode *dir;
35691 +       struct au_sbinfo *sbinfo;
35692 +
35693 +       SiMustWriteLock(sb);
35694 +
35695 +       err = 0;
35696 +       sbinfo = au_sbi(sb);
35697 +       parent = dget_parent(xino->file->f_path.dentry);
35698 +       if (remount) {
35699 +               skip = 0;
35700 +               dname = &xino->file->f_path.dentry->d_name;
35701 +               cur_xino = sbinfo->si_xib;
35702 +               if (cur_xino) {
35703 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
35704 +                       cur_name = &cur_xino->f_path.dentry->d_name;
35705 +                       skip = (cur_parent == parent
35706 +                               && au_qstreq(dname, cur_name));
35707 +                       dput(cur_parent);
35708 +               }
35709 +               if (skip)
35710 +                       goto out;
35711 +       }
35712 +
35713 +       au_opt_set(sbinfo->si_mntflags, XINO);
35714 +       dir = d_inode(parent);
35715 +       inode_lock_nested(dir, AuLsc_I_PARENT);
35716 +       /* mnt_want_write() is unnecessary here */
35717 +       err = au_xino_set_xib(sb, xino->file);
35718 +       if (!err)
35719 +               err = au_xigen_set(sb, xino->file);
35720 +       if (!err)
35721 +               err = au_xino_set_br(sb, xino->file);
35722 +       inode_unlock(dir);
35723 +       if (!err)
35724 +               goto out; /* success */
35725 +
35726 +       /* reset all */
35727 +       AuIOErr("failed creating xino(%d).\n", err);
35728 +       au_xigen_clr(sb);
35729 +       xino_clear_xib(sb);
35730 +
35731 +out:
35732 +       dput(parent);
35733 +       return err;
35734 +}
35735 +
35736 +/* ---------------------------------------------------------------------- */
35737 +
35738 +/*
35739 + * create a xinofile at the default place/path.
35740 + */
35741 +struct file *au_xino_def(struct super_block *sb)
35742 +{
35743 +       struct file *file;
35744 +       char *page, *p;
35745 +       struct au_branch *br;
35746 +       struct super_block *h_sb;
35747 +       struct path path;
35748 +       aufs_bindex_t bbot, bindex, bwr;
35749 +
35750 +       br = NULL;
35751 +       bbot = au_sbbot(sb);
35752 +       bwr = -1;
35753 +       for (bindex = 0; bindex <= bbot; bindex++) {
35754 +               br = au_sbr(sb, bindex);
35755 +               if (au_br_writable(br->br_perm)
35756 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
35757 +                       bwr = bindex;
35758 +                       break;
35759 +               }
35760 +       }
35761 +
35762 +       if (bwr >= 0) {
35763 +               file = ERR_PTR(-ENOMEM);
35764 +               page = (void *)__get_free_page(GFP_NOFS);
35765 +               if (unlikely(!page))
35766 +                       goto out;
35767 +               path.mnt = au_br_mnt(br);
35768 +               path.dentry = au_h_dptr(sb->s_root, bwr);
35769 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35770 +               file = (void *)p;
35771 +               if (!IS_ERR(p)) {
35772 +                       strcat(p, "/" AUFS_XINO_FNAME);
35773 +                       AuDbg("%s\n", p);
35774 +                       file = au_xino_create(sb, p, /*silent*/0);
35775 +                       if (!IS_ERR(file))
35776 +                               au_xino_brid_set(sb, br->br_id);
35777 +               }
35778 +               free_page((unsigned long)page);
35779 +       } else {
35780 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35781 +               if (IS_ERR(file))
35782 +                       goto out;
35783 +               h_sb = file->f_path.dentry->d_sb;
35784 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
35785 +                       pr_err("xino doesn't support %s(%s)\n",
35786 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35787 +                       fput(file);
35788 +                       file = ERR_PTR(-EINVAL);
35789 +               }
35790 +               if (!IS_ERR(file))
35791 +                       au_xino_brid_set(sb, -1);
35792 +       }
35793 +
35794 +out:
35795 +       return file;
35796 +}
35797 +
35798 +/* ---------------------------------------------------------------------- */
35799 +
35800 +int au_xino_path(struct seq_file *seq, struct file *file)
35801 +{
35802 +       int err;
35803 +
35804 +       err = au_seq_path(seq, &file->f_path);
35805 +       if (unlikely(err))
35806 +               goto out;
35807 +
35808 +#define Deleted "\\040(deleted)"
35809 +       seq->count -= sizeof(Deleted) - 1;
35810 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35811 +                        sizeof(Deleted) - 1));
35812 +#undef Deleted
35813 +
35814 +out:
35815 +       return err;
35816 +}
35817 +
35818 +/* ---------------------------------------------------------------------- */
35819 +
35820 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
35821 +                      ino_t h_ino, int idx)
35822 +{
35823 +       struct au_xino_file *xino;
35824 +
35825 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
35826 +       xino = &au_sbr(sb, bindex)->br_xino;
35827 +       AuDebugOn(idx < 0 || xino->xi_nondir.total <= idx);
35828 +
35829 +       spin_lock(&xino->xi_nondir.spin);
35830 +       AuDebugOn(xino->xi_nondir.array[idx] != h_ino);
35831 +       xino->xi_nondir.array[idx] = 0;
35832 +       spin_unlock(&xino->xi_nondir.spin);
35833 +       wake_up_all(&xino->xi_nondir.wqh);
35834 +}
35835 +
35836 +static int au_xinondir_find(struct au_xino_file *xino, ino_t h_ino)
35837 +{
35838 +       int found, total, i;
35839 +
35840 +       found = -1;
35841 +       total = xino->xi_nondir.total;
35842 +       for (i = 0; i < total; i++) {
35843 +               if (xino->xi_nondir.array[i] != h_ino)
35844 +                       continue;
35845 +               found = i;
35846 +               break;
35847 +       }
35848 +
35849 +       return found;
35850 +}
35851 +
35852 +static int au_xinondir_expand(struct au_xino_file *xino)
35853 +{
35854 +       int err, sz;
35855 +       ino_t *p;
35856 +
35857 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
35858 +
35859 +       err = -ENOMEM;
35860 +       sz = xino->xi_nondir.total * sizeof(ino_t);
35861 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
35862 +               goto out;
35863 +       p = au_kzrealloc(xino->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
35864 +                        /*may_shrink*/0);
35865 +       if (p) {
35866 +               xino->xi_nondir.array = p;
35867 +               xino->xi_nondir.total <<= 1;
35868 +               AuDbg("xi_nondir.total %d\n", xino->xi_nondir.total);
35869 +               err = 0;
35870 +       }
35871 +
35872 +out:
35873 +       return err;
35874 +}
35875 +
35876 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
35877 +                     int *idx)
35878 +{
35879 +       int err, found, empty;
35880 +       struct au_xino_file *xino;
35881 +
35882 +       err = 0;
35883 +       *idx = -1;
35884 +       if (!au_opt_test(au_mntflags(sb), XINO))
35885 +               goto out; /* no xino */
35886 +
35887 +       xino = &au_sbr(sb, bindex)->br_xino;
35888 +
35889 +again:
35890 +       spin_lock(&xino->xi_nondir.spin);
35891 +       found = au_xinondir_find(xino, h_ino);
35892 +       if (found == -1) {
35893 +               empty = au_xinondir_find(xino, /*h_ino*/0);
35894 +               if (empty == -1) {
35895 +                       empty = xino->xi_nondir.total;
35896 +                       err = au_xinondir_expand(xino);
35897 +                       if (unlikely(err))
35898 +                               goto out_unlock;
35899 +               }
35900 +               xino->xi_nondir.array[empty] = h_ino;
35901 +               *idx = empty;
35902 +       } else {
35903 +               spin_unlock(&xino->xi_nondir.spin);
35904 +               wait_event(xino->xi_nondir.wqh,
35905 +                          xino->xi_nondir.array[found] != h_ino);
35906 +               goto again;
35907 +       }
35908 +
35909 +out_unlock:
35910 +       spin_unlock(&xino->xi_nondir.spin);
35911 +out:
35912 +       return err;
35913 +}
35914 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35915 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
35916 +++ linux/include/uapi/linux/aufs_type.h        2017-07-29 12:14:32.526591175 +0200
35917 @@ -0,0 +1,419 @@
35918 +/*
35919 + * Copyright (C) 2005-2017 Junjiro R. Okajima
35920 + *
35921 + * This program, aufs is free software; you can redistribute it and/or modify
35922 + * it under the terms of the GNU General Public License as published by
35923 + * the Free Software Foundation; either version 2 of the License, or
35924 + * (at your option) any later version.
35925 + *
35926 + * This program is distributed in the hope that it will be useful,
35927 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35928 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35929 + * GNU General Public License for more details.
35930 + *
35931 + * You should have received a copy of the GNU General Public License
35932 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35933 + */
35934 +
35935 +#ifndef __AUFS_TYPE_H__
35936 +#define __AUFS_TYPE_H__
35937 +
35938 +#define AUFS_NAME      "aufs"
35939 +
35940 +#ifdef __KERNEL__
35941 +/*
35942 + * define it before including all other headers.
35943 + * sched.h may use pr_* macros before defining "current", so define the
35944 + * no-current version first, and re-define later.
35945 + */
35946 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35947 +#include <linux/sched.h>
35948 +#undef pr_fmt
35949 +#define pr_fmt(fmt) \
35950 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35951 +               (int)sizeof(current->comm), current->comm, current->pid
35952 +#else
35953 +#include <stdint.h>
35954 +#include <sys/types.h>
35955 +#endif /* __KERNEL__ */
35956 +
35957 +#include <linux/limits.h>
35958 +
35959 +#define AUFS_VERSION   "4.x-rcN-20170703"
35960 +
35961 +/* todo? move this to linux-2.6.19/include/magic.h */
35962 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35963 +
35964 +/* ---------------------------------------------------------------------- */
35965 +
35966 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
35967 +typedef int8_t aufs_bindex_t;
35968 +#define AUFS_BRANCH_MAX 127
35969 +#else
35970 +typedef int16_t aufs_bindex_t;
35971 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
35972 +#define AUFS_BRANCH_MAX 511
35973 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35974 +#define AUFS_BRANCH_MAX 1023
35975 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35976 +#define AUFS_BRANCH_MAX 32767
35977 +#endif
35978 +#endif
35979 +
35980 +#ifdef __KERNEL__
35981 +#ifndef AUFS_BRANCH_MAX
35982 +#error unknown CONFIG_AUFS_BRANCH_MAX value
35983 +#endif
35984 +#endif /* __KERNEL__ */
35985 +
35986 +/* ---------------------------------------------------------------------- */
35987 +
35988 +#define AUFS_FSTYPE            AUFS_NAME
35989 +
35990 +#define AUFS_ROOT_INO          2
35991 +#define AUFS_FIRST_INO         11
35992 +
35993 +#define AUFS_WH_PFX            ".wh."
35994 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
35995 +#define AUFS_WH_TMP_LEN                4
35996 +/* a limit for rmdir/rename a dir and copyup */
35997 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
35998 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
35999 +                               - 1                     /* dot */\
36000 +                               - AUFS_WH_TMP_LEN)      /* hex */
36001 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
36002 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
36003 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
36004 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
36005 +#define AUFS_DIRWH_DEF         3
36006 +#define AUFS_RDCACHE_DEF       10 /* seconds */
36007 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
36008 +#define AUFS_RDBLK_DEF         512 /* bytes */
36009 +#define AUFS_RDHASH_DEF                32
36010 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
36011 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
36012 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
36013 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
36014 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
36015 +
36016 +/* pseudo-link maintenace under /proc */
36017 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
36018 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
36019 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
36020 +
36021 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
36022 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
36023 +
36024 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
36025 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
36026 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
36027 +
36028 +/* doubly whiteouted */
36029 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
36030 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
36031 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
36032 +
36033 +/* branch permissions and attributes */
36034 +#define AUFS_BRPERM_RW         "rw"
36035 +#define AUFS_BRPERM_RO         "ro"
36036 +#define AUFS_BRPERM_RR         "rr"
36037 +#define AUFS_BRATTR_COO_REG    "coo_reg"
36038 +#define AUFS_BRATTR_COO_ALL    "coo_all"
36039 +#define AUFS_BRATTR_FHSM       "fhsm"
36040 +#define AUFS_BRATTR_UNPIN      "unpin"
36041 +#define AUFS_BRATTR_ICEX       "icex"
36042 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
36043 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
36044 +#define AUFS_BRATTR_ICEX_TR    "icextr"
36045 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
36046 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
36047 +#define AUFS_BRRATTR_WH                "wh"
36048 +#define AUFS_BRWATTR_NLWH      "nolwh"
36049 +#define AUFS_BRWATTR_MOO       "moo"
36050 +
36051 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
36052 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
36053 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
36054 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
36055 +
36056 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
36057 +#define AuBrAttr_COO_ALL       (1 << 4)
36058 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
36059 +
36060 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
36061 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
36062 +                                                  branch. meaningless since
36063 +                                                  linux-3.18-rc1 */
36064 +
36065 +/* ignore error in copying XATTR */
36066 +#define AuBrAttr_ICEX_SEC      (1 << 7)
36067 +#define AuBrAttr_ICEX_SYS      (1 << 8)
36068 +#define AuBrAttr_ICEX_TR       (1 << 9)
36069 +#define AuBrAttr_ICEX_USR      (1 << 10)
36070 +#define AuBrAttr_ICEX_OTH      (1 << 11)
36071 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
36072 +                                | AuBrAttr_ICEX_SYS    \
36073 +                                | AuBrAttr_ICEX_TR     \
36074 +                                | AuBrAttr_ICEX_USR    \
36075 +                                | AuBrAttr_ICEX_OTH)
36076 +
36077 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
36078 +#define AuBrRAttr_Mask         AuBrRAttr_WH
36079 +
36080 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
36081 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
36082 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
36083 +
36084 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
36085 +
36086 +/* #warning test userspace */
36087 +#ifdef __KERNEL__
36088 +#ifndef CONFIG_AUFS_FHSM
36089 +#undef AuBrAttr_FHSM
36090 +#define AuBrAttr_FHSM          0
36091 +#endif
36092 +#ifndef CONFIG_AUFS_XATTR
36093 +#undef AuBrAttr_ICEX
36094 +#define AuBrAttr_ICEX          0
36095 +#undef AuBrAttr_ICEX_SEC
36096 +#define AuBrAttr_ICEX_SEC      0
36097 +#undef AuBrAttr_ICEX_SYS
36098 +#define AuBrAttr_ICEX_SYS      0
36099 +#undef AuBrAttr_ICEX_TR
36100 +#define AuBrAttr_ICEX_TR       0
36101 +#undef AuBrAttr_ICEX_USR
36102 +#define AuBrAttr_ICEX_USR      0
36103 +#undef AuBrAttr_ICEX_OTH
36104 +#define AuBrAttr_ICEX_OTH      0
36105 +#endif
36106 +#endif
36107 +
36108 +/* the longest combination */
36109 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
36110 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
36111 +                              "+" AUFS_BRATTR_COO_REG          \
36112 +                              "+" AUFS_BRATTR_FHSM             \
36113 +                              "+" AUFS_BRATTR_UNPIN            \
36114 +                              "+" AUFS_BRATTR_ICEX_SEC         \
36115 +                              "+" AUFS_BRATTR_ICEX_SYS         \
36116 +                              "+" AUFS_BRATTR_ICEX_USR         \
36117 +                              "+" AUFS_BRATTR_ICEX_OTH         \
36118 +                              "+" AUFS_BRWATTR_NLWH)
36119 +
36120 +typedef struct {
36121 +       char a[AuBrPermStrSz];
36122 +} au_br_perm_str_t;
36123 +
36124 +static inline int au_br_writable(int brperm)
36125 +{
36126 +       return brperm & AuBrPerm_RW;
36127 +}
36128 +
36129 +static inline int au_br_whable(int brperm)
36130 +{
36131 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
36132 +}
36133 +
36134 +static inline int au_br_wh_linkable(int brperm)
36135 +{
36136 +       return !(brperm & AuBrWAttr_NoLinkWH);
36137 +}
36138 +
36139 +static inline int au_br_cmoo(int brperm)
36140 +{
36141 +       return brperm & AuBrAttr_CMOO_Mask;
36142 +}
36143 +
36144 +static inline int au_br_fhsm(int brperm)
36145 +{
36146 +       return brperm & AuBrAttr_FHSM;
36147 +}
36148 +
36149 +/* ---------------------------------------------------------------------- */
36150 +
36151 +/* ioctl */
36152 +enum {
36153 +       /* readdir in userspace */
36154 +       AuCtl_RDU,
36155 +       AuCtl_RDU_INO,
36156 +
36157 +       AuCtl_WBR_FD,   /* pathconf wrapper */
36158 +       AuCtl_IBUSY,    /* busy inode */
36159 +       AuCtl_MVDOWN,   /* move-down */
36160 +       AuCtl_BR,       /* info about branches */
36161 +       AuCtl_FHSM_FD   /* connection for fhsm */
36162 +};
36163 +
36164 +/* borrowed from linux/include/linux/kernel.h */
36165 +#ifndef ALIGN
36166 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
36167 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
36168 +#endif
36169 +
36170 +/* borrowed from linux/include/linux/compiler-gcc3.h */
36171 +#ifndef __aligned
36172 +#define __aligned(x)                   __attribute__((aligned(x)))
36173 +#endif
36174 +
36175 +#ifdef __KERNEL__
36176 +#ifndef __packed
36177 +#define __packed                       __attribute__((packed))
36178 +#endif
36179 +#endif
36180 +
36181 +struct au_rdu_cookie {
36182 +       uint64_t        h_pos;
36183 +       int16_t         bindex;
36184 +       uint8_t         flags;
36185 +       uint8_t         pad;
36186 +       uint32_t        generation;
36187 +} __aligned(8);
36188 +
36189 +struct au_rdu_ent {
36190 +       uint64_t        ino;
36191 +       int16_t         bindex;
36192 +       uint8_t         type;
36193 +       uint8_t         nlen;
36194 +       uint8_t         wh;
36195 +       char            name[0];
36196 +} __aligned(8);
36197 +
36198 +static inline int au_rdu_len(int nlen)
36199 +{
36200 +       /* include the terminating NULL */
36201 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
36202 +                    sizeof(uint64_t));
36203 +}
36204 +
36205 +union au_rdu_ent_ul {
36206 +       struct au_rdu_ent __user        *e;
36207 +       uint64_t                        ul;
36208 +};
36209 +
36210 +enum {
36211 +       AufsCtlRduV_SZ,
36212 +       AufsCtlRduV_End
36213 +};
36214 +
36215 +struct aufs_rdu {
36216 +       /* input */
36217 +       union {
36218 +               uint64_t        sz;     /* AuCtl_RDU */
36219 +               uint64_t        nent;   /* AuCtl_RDU_INO */
36220 +       };
36221 +       union au_rdu_ent_ul     ent;
36222 +       uint16_t                verify[AufsCtlRduV_End];
36223 +
36224 +       /* input/output */
36225 +       uint32_t                blk;
36226 +
36227 +       /* output */
36228 +       union au_rdu_ent_ul     tail;
36229 +       /* number of entries which were added in a single call */
36230 +       uint64_t                rent;
36231 +       uint8_t                 full;
36232 +       uint8_t                 shwh;
36233 +
36234 +       struct au_rdu_cookie    cookie;
36235 +} __aligned(8);
36236 +
36237 +/* ---------------------------------------------------------------------- */
36238 +
36239 +struct aufs_wbr_fd {
36240 +       uint32_t        oflags;
36241 +       int16_t         brid;
36242 +} __aligned(8);
36243 +
36244 +/* ---------------------------------------------------------------------- */
36245 +
36246 +struct aufs_ibusy {
36247 +       uint64_t        ino, h_ino;
36248 +       int16_t         bindex;
36249 +} __aligned(8);
36250 +
36251 +/* ---------------------------------------------------------------------- */
36252 +
36253 +/* error code for move-down */
36254 +/* the actual message strings are implemented in aufs-util.git */
36255 +enum {
36256 +       EAU_MVDOWN_OPAQUE = 1,
36257 +       EAU_MVDOWN_WHITEOUT,
36258 +       EAU_MVDOWN_UPPER,
36259 +       EAU_MVDOWN_BOTTOM,
36260 +       EAU_MVDOWN_NOUPPER,
36261 +       EAU_MVDOWN_NOLOWERBR,
36262 +       EAU_Last
36263 +};
36264 +
36265 +/* flags for move-down */
36266 +#define AUFS_MVDOWN_DMSG       1
36267 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
36268 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
36269 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
36270 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
36271 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
36272 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
36273 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
36274 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
36275 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
36276 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
36277 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
36278 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
36279 +
36280 +/* index for move-down */
36281 +enum {
36282 +       AUFS_MVDOWN_UPPER,
36283 +       AUFS_MVDOWN_LOWER,
36284 +       AUFS_MVDOWN_NARRAY
36285 +};
36286 +
36287 +/*
36288 + * additional info of move-down
36289 + * number of free blocks and inodes.
36290 + * subset of struct kstatfs, but smaller and always 64bit.
36291 + */
36292 +struct aufs_stfs {
36293 +       uint64_t        f_blocks;
36294 +       uint64_t        f_bavail;
36295 +       uint64_t        f_files;
36296 +       uint64_t        f_ffree;
36297 +};
36298 +
36299 +struct aufs_stbr {
36300 +       int16_t                 brid;   /* optional input */
36301 +       int16_t                 bindex; /* output */
36302 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
36303 +} __aligned(8);
36304 +
36305 +struct aufs_mvdown {
36306 +       uint32_t                flags;                  /* input/output */
36307 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
36308 +       int8_t                  au_errno;               /* output */
36309 +} __aligned(8);
36310 +
36311 +/* ---------------------------------------------------------------------- */
36312 +
36313 +union aufs_brinfo {
36314 +       /* PATH_MAX may differ between kernel-space and user-space */
36315 +       char    _spacer[4096];
36316 +       struct {
36317 +               int16_t id;
36318 +               int     perm;
36319 +               char    path[0];
36320 +       };
36321 +} __aligned(8);
36322 +
36323 +/* ---------------------------------------------------------------------- */
36324 +
36325 +#define AuCtlType              'A'
36326 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
36327 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
36328 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
36329 +                                    struct aufs_wbr_fd)
36330 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
36331 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
36332 +                                     struct aufs_mvdown)
36333 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
36334 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
36335 +
36336 +#endif /* __AUFS_TYPE_H__ */
36337 aufs4.x-rcN loopback patch
36338
36339 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
36340 index 6b8a6a4c..a80416e 100644
36341 --- a/drivers/block/loop.c
36342 +++ b/drivers/block/loop.c
36343 @@ -547,7 +547,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
36344  }
36345  
36346  struct switch_request {
36347 -       struct file *file;
36348 +       struct file *file, *virt_file;
36349         struct completion wait;
36350  };
36351  
36352 @@ -573,6 +573,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
36353         mapping = file->f_mapping;
36354         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
36355         lo->lo_backing_file = file;
36356 +       lo->lo_backing_virt_file = p->virt_file;
36357         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
36358                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
36359         lo->old_gfp_mask = mapping_gfp_mask(mapping);
36360 @@ -585,11 +586,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
36361   * First it needs to flush existing IO, it does this by sending a magic
36362   * BIO down the pipe. The completion of this BIO does the actual switch.
36363   */
36364 -static int loop_switch(struct loop_device *lo, struct file *file)
36365 +static int loop_switch(struct loop_device *lo, struct file *file,
36366 +                      struct file *virt_file)
36367  {
36368         struct switch_request w;
36369  
36370         w.file = file;
36371 +       w.virt_file = virt_file;
36372  
36373         /* freeze queue and wait for completion of scheduled requests */
36374         blk_mq_freeze_queue(lo->lo_queue);
36375 @@ -614,6 +617,15 @@ static int loop_flush(struct loop_device *lo)
36376         return loop_switch(lo, NULL);
36377  }
36378  
36379 +static struct file *loop_real_file(struct file *file)
36380 +{
36381 +       struct file *f = NULL;
36382 +
36383 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
36384 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
36385 +       return f;
36386 +}
36387 +
36388  static void loop_reread_partitions(struct loop_device *lo,
36389                                    struct block_device *bdev)
36390  {
36391 @@ -648,6 +660,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36392                           unsigned int arg)
36393  {
36394         struct file     *file, *old_file;
36395 +       struct file     *f, *virt_file = NULL, *old_virt_file;
36396         struct inode    *inode;
36397         int             error;
36398  
36399 @@ -664,9 +677,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36400         file = fget(arg);
36401         if (!file)
36402                 goto out;
36403 +       f = loop_real_file(file);
36404 +       if (f) {
36405 +               virt_file = file;
36406 +               file = f;
36407 +               get_file(file);
36408 +       }
36409  
36410         inode = file->f_mapping->host;
36411         old_file = lo->lo_backing_file;
36412 +       old_virt_file = lo->lo_backing_virt_file;
36413  
36414         error = -EINVAL;
36415  
36416 @@ -678,17 +698,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36417                 goto out_putf;
36418  
36419         /* and ... switch */
36420 -       error = loop_switch(lo, file);
36421 +       error = loop_switch(lo, file, virt_file);
36422         if (error)
36423                 goto out_putf;
36424  
36425         fput(old_file);
36426 +       if (old_virt_file)
36427 +               fput(old_virt_file);
36428         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
36429                 loop_reread_partitions(lo, bdev);
36430         return 0;
36431  
36432   out_putf:
36433         fput(file);
36434 +       if (virt_file)
36435 +               fput(virt_file);
36436   out:
36437         return error;
36438  }
36439 @@ -875,7 +899,7 @@ static int loop_prepare_queue(struct loop_device *lo)
36440  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36441                        struct block_device *bdev, unsigned int arg)
36442  {
36443 -       struct file     *file, *f;
36444 +       struct file     *file, *f, *virt_file = NULL;
36445         struct inode    *inode;
36446         struct address_space *mapping;
36447         unsigned lo_blocksize;
36448 @@ -890,6 +914,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36449         file = fget(arg);
36450         if (!file)
36451                 goto out;
36452 +       f = loop_real_file(file);
36453 +       if (f) {
36454 +               virt_file = file;
36455 +               file = f;
36456 +               get_file(file);
36457 +       }
36458  
36459         error = -EBUSY;
36460         if (lo->lo_state != Lo_unbound)
36461 @@ -942,6 +972,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36462         lo->lo_device = bdev;
36463         lo->lo_flags = lo_flags;
36464         lo->lo_backing_file = file;
36465 +       lo->lo_backing_virt_file = virt_file;
36466         lo->transfer = NULL;
36467         lo->ioctl = NULL;
36468         lo->lo_sizelimit = 0;
36469 @@ -974,6 +1005,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36470  
36471   out_putf:
36472         fput(file);
36473 +       if (virt_file)
36474 +               fput(virt_file);
36475   out:
36476         /* This is safe: open() is still holding a reference. */
36477         module_put(THIS_MODULE);
36478 @@ -1020,6 +1053,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
36479  static int loop_clr_fd(struct loop_device *lo)
36480  {
36481         struct file *filp = lo->lo_backing_file;
36482 +       struct file *virt_filp = lo->lo_backing_virt_file;
36483         gfp_t gfp = lo->old_gfp_mask;
36484         struct block_device *bdev = lo->lo_device;
36485  
36486 @@ -1051,6 +1085,7 @@ static int loop_clr_fd(struct loop_device *lo)
36487         spin_lock_irq(&lo->lo_lock);
36488         lo->lo_state = Lo_rundown;
36489         lo->lo_backing_file = NULL;
36490 +       lo->lo_backing_virt_file = NULL;
36491         spin_unlock_irq(&lo->lo_lock);
36492  
36493         loop_release_xfer(lo);
36494 @@ -1095,6 +1130,8 @@ static int loop_clr_fd(struct loop_device *lo)
36495          * bd_mutex which is usually taken before lo_ctl_mutex.
36496          */
36497         fput(filp);
36498 +       if (virt_filp)
36499 +               fput(virt_filp);
36500         return 0;
36501  }
36502  
36503 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
36504 index fecd3f9..6b3a7c9 100644
36505 --- a/drivers/block/loop.h
36506 +++ b/drivers/block/loop.h
36507 @@ -46,7 +46,7 @@ struct loop_device {
36508         int             (*ioctl)(struct loop_device *, int cmd, 
36509                                  unsigned long arg); 
36510  
36511 -       struct file *   lo_backing_file;
36512 +       struct file *   lo_backing_file, *lo_backing_virt_file;
36513         struct block_device *lo_device;
36514         unsigned        lo_blocksize;
36515         void            *key_data; 
36516 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
36517 index 870717e..ea7fee0 100644
36518 --- a/fs/aufs/f_op.c
36519 +++ b/fs/aufs/f_op.c
36520 @@ -357,7 +357,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
36521         if (IS_ERR(h_file))
36522                 goto out;
36523  
36524 -       if (au_test_loopback_kthread()) {
36525 +       if (0 && au_test_loopback_kthread()) {
36526                 au_warn_loopback(h_file->f_path.dentry->d_sb);
36527                 if (file->f_mapping != h_file->f_mapping) {
36528                         file->f_mapping = h_file->f_mapping;
36529 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
36530 index e35f015..b37f1ae 100644
36531 --- a/fs/aufs/loop.c
36532 +++ b/fs/aufs/loop.c
36533 @@ -132,3 +132,19 @@ void au_loopback_fin(void)
36534                 symbol_put(loop_backing_file);
36535         kfree(au_warn_loopback_array);
36536  }
36537 +
36538 +/* ---------------------------------------------------------------------- */
36539 +
36540 +/* support the loopback block device insude aufs */
36541 +
36542 +struct file *aufs_real_loop(struct file *file)
36543 +{
36544 +       struct file *f;
36545 +
36546 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
36547 +       fi_read_lock(file);
36548 +       f = au_hf_top(file);
36549 +       fi_read_unlock(file);
36550 +       AuDebugOn(!f);
36551 +       return f;
36552 +}
36553 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
36554 index e2df495..36e5052 100644
36555 --- a/fs/aufs/loop.h
36556 +++ b/fs/aufs/loop.h
36557 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
36558  
36559  int au_loopback_init(void);
36560  void au_loopback_fin(void);
36561 +
36562 +struct file *aufs_real_loop(struct file *file);
36563  #else
36564 +AuStub(struct file *, loop_backing_file, return NULL)
36565 +
36566  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
36567            struct dentry *h_adding)
36568  AuStubInt0(au_test_loopback_kthread, void)
36569 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
36570  
36571  AuStubInt0(au_loopback_init, void)
36572  AuStubVoid(au_loopback_fin, void)
36573 +
36574 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
36575  #endif /* BLK_DEV_LOOP */
36576  
36577  #endif /* __KERNEL__ */
36578 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
36579 index 5455fb1..8b9df60 100644
36580 --- a/fs/aufs/super.c
36581 +++ b/fs/aufs/super.c
36582 @@ -837,7 +837,10 @@ static const struct super_operations aufs_sop = {
36583         .statfs         = aufs_statfs,
36584         .put_super      = aufs_put_super,
36585         .sync_fs        = aufs_sync_fs,
36586 -       .remount_fs     = aufs_remount_fs
36587 +       .remount_fs     = aufs_remount_fs,
36588 +#ifdef CONFIG_AUFS_BDEV_LOOP
36589 +       .real_loop      = aufs_real_loop
36590 +#endif
36591  };
36592  
36593  /* ---------------------------------------------------------------------- */
36594 diff --git a/include/linux/fs.h b/include/linux/fs.h
36595 index 22d2910..d8db3fb 100644
36596 --- a/include/linux/fs.h
36597 +++ b/include/linux/fs.h
36598 @@ -1811,6 +1811,10 @@ struct super_operations {
36599                                   struct shrink_control *);
36600         long (*free_cached_objects)(struct super_block *,
36601                                     struct shrink_control *);
36602 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
36603 +       /* and aufs */
36604 +       struct file *(*real_loop)(struct file *);
36605 +#endif
36606  };
36607  
36608  /*
This page took 3.303121 seconds and 3 git commands to generate.