]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- 4.17.11
[packages/kernel.git] / kernel-aufs4.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs4.17 kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index bc821a8..7ae814c 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -251,6 +251,7 @@ source "fs/pstore/Kconfig"
9  source "fs/sysv/Kconfig"
10  source "fs/ufs/Kconfig"
11  source "fs/exofs/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index c9375fd..8af5671 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -128,3 +128,4 @@ obj-y                               += exofs/ # Multiple modules
21  obj-$(CONFIG_CEPH_FS)          += ceph/
22  obj-$(CONFIG_PSTORE)           += pstore/
23  obj-$(CONFIG_EFIVAR_FS)                += efivarfs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs4.17 base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 9c125f7..4616bbf 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2519,6 +2519,19 @@ F:       include/linux/audit.h
33  F:     include/uapi/linux/audit.h
34  F:     kernel/audit*
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     linux-unionfs@vger.kernel.org
39 +L:     aufs-users@lists.sourceforge.net (members only)
40 +W:     http://aufs.sourceforge.net
41 +T:     git://github.com/sfjro/aufs4-linux.git
42 +S:     Supported
43 +F:     Documentation/filesystems/aufs/
44 +F:     Documentation/ABI/testing/debugfs-aufs
45 +F:     Documentation/ABI/testing/sysfs-aufs
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY DISPLAY DRIVERS
50  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
51  W:     http://miguelojeda.es/auxdisplay.htm
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index 55cf554..bc965e5 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -713,6 +713,24 @@ static inline int is_loop_device(struct file *file)
57         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
58  }
59  
60 +/*
61 + * for AUFS
62 + * no get/put for file.
63 + */
64 +struct file *loop_backing_file(struct super_block *sb)
65 +{
66 +       struct file *ret;
67 +       struct loop_device *l;
68 +
69 +       ret = NULL;
70 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71 +               l = sb->s_bdev->bd_disk->private_data;
72 +               ret = l->lo_backing_file;
73 +       }
74 +       return ret;
75 +}
76 +EXPORT_SYMBOL_GPL(loop_backing_file);
77 +
78  /* loop sysfs attributes */
79  
80  static ssize_t loop_attr_show(struct device *dev, char *page,
81 diff --git a/fs/dcache.c b/fs/dcache.c
82 index 2acfc69..ff338e2 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1234,7 +1234,7 @@ enum d_walk_ret {
86   *
87   * The @enter() and @finish() callbacks are called with d_lock held.
88   */
89 -static void d_walk(struct dentry *parent, void *data,
90 +void d_walk(struct dentry *parent, void *data,
91                    enum d_walk_ret (*enter)(void *, struct dentry *),
92                    void (*finish)(void *))
93  {
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index d737ff0..7550799 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -32,7 +32,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned long arg)
103 +int setfl(int fd, struct file * filp, unsigned long arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/inode.c b/fs/inode.c
117 index 3b55391..e0c5255 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1663,7 +1663,7 @@ EXPORT_SYMBOL(generic_update_time);
121   * This does the actual work of updating an inodes time or version.  Must have
122   * had called mnt_want_write() before calling this.
123   */
124 -static int update_time(struct inode *inode, struct timespec *time, int flags)
125 +int update_time(struct inode *inode, struct timespec *time, int flags)
126  {
127         int (*update_time)(struct inode *, struct timespec *, int);
128  
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index 5f75969..61129ff 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -846,6 +846,12 @@ static inline int check_mnt(struct mount *mnt)
134         return mnt->mnt_ns == current->nsproxy->mnt_ns;
135  }
136  
137 +/* for aufs, CONFIG_AUFS_BR_FUSE */
138 +int is_current_mnt_ns(struct vfsmount *mnt)
139 +{
140 +       return check_mnt(real_mount(mnt));
141 +}
142 +
143  /*
144   * vfsmount lock must be held for write
145   */
146 diff --git a/fs/read_write.c b/fs/read_write.c
147 index c4eabbf..ddd6e67 100644
148 --- a/fs/read_write.c
149 +++ b/fs/read_write.c
150 @@ -489,6 +489,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
151                 return -EINVAL;
152  }
153  
154 +vfs_readf_t vfs_readf(struct file *file)
155 +{
156 +       const struct file_operations *fop = file->f_op;
157 +
158 +       if (fop->read)
159 +               return fop->read;
160 +       if (fop->read_iter)
161 +               return new_sync_read;
162 +       return ERR_PTR(-ENOSYS);
163 +}
164 +
165 +vfs_writef_t vfs_writef(struct file *file)
166 +{
167 +       const struct file_operations *fop = file->f_op;
168 +
169 +       if (fop->write)
170 +               return fop->write;
171 +       if (fop->write_iter)
172 +               return new_sync_write;
173 +       return ERR_PTR(-ENOSYS);
174 +}
175 +
176  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
177  {
178         mm_segment_t old_fs;
179 diff --git a/fs/splice.c b/fs/splice.c
180 index 005d09c..f617ab0 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -837,8 +837,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
184  /*
185   * Attempt to initiate a splice from pipe to file.
186   */
187 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188 -                          loff_t *ppos, size_t len, unsigned int flags)
189 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190 +                   loff_t *ppos, size_t len, unsigned int flags)
191  {
192         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193                                 loff_t *, size_t, unsigned int);
194 @@ -854,9 +854,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
195  /*
196   * Attempt to initiate a splice from a file to a pipe.
197   */
198 -static long do_splice_to(struct file *in, loff_t *ppos,
199 -                        struct pipe_inode_info *pipe, size_t len,
200 -                        unsigned int flags)
201 +long do_splice_to(struct file *in, loff_t *ppos,
202 +                 struct pipe_inode_info *pipe, size_t len,
203 +                 unsigned int flags)
204  {
205         ssize_t (*splice_read)(struct file *, loff_t *,
206                                struct pipe_inode_info *, size_t, unsigned int);
207 diff --git a/fs/sync.c b/fs/sync.c
208 index b54e054..2860782 100644
209 --- a/fs/sync.c
210 +++ b/fs/sync.c
211 @@ -28,7 +28,7 @@
212   * wait == 1 case since in that case write_inode() functions do
213   * sync_dirty_buffer() and thus effectively write one block at a time.
214   */
215 -static int __sync_filesystem(struct super_block *sb, int wait)
216 +int __sync_filesystem(struct super_block *sb, int wait)
217  {
218         if (wait)
219                 sync_inodes_sb(sb);
220 diff --git a/include/linux/file.h b/include/linux/file.h
221 index 279720d..76e38ea 100644
222 --- a/include/linux/file.h
223 +++ b/include/linux/file.h
224 @@ -20,6 +20,7 @@ struct dentry;
225  struct path;
226  extern struct file *alloc_file(const struct path *, fmode_t mode,
227         const struct file_operations *fop);
228 +extern struct file *get_empty_filp(void);
229  
230  static inline void fput_light(struct file *file, int fput_needed)
231  {
232 diff --git a/include/linux/fs.h b/include/linux/fs.h
233 index 760d8da..09a2542 100644
234 --- a/include/linux/fs.h
235 +++ b/include/linux/fs.h
236 @@ -1270,6 +1270,7 @@ extern void fasync_free(struct fasync_struct *);
237  /* can be called from interrupts */
238  extern void kill_fasync(struct fasync_struct **, int, int);
239  
240 +extern int setfl(int fd, struct file * filp, unsigned long arg);
241  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
242  extern int f_setown(struct file *filp, unsigned long arg, int force);
243  extern void f_delown(struct file *filp);
244 @@ -1724,6 +1725,7 @@ struct file_operations {
245         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
246         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
247         int (*check_flags)(int);
248 +       int (*setfl)(struct file *, unsigned long);
249         int (*flock) (struct file *, int, struct file_lock *);
250         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
251         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
252 @@ -1794,6 +1796,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
253                               struct iovec *fast_pointer,
254                               struct iovec **ret_pointer);
255  
256 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
257 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
258 +                               loff_t *);
259 +vfs_readf_t vfs_readf(struct file *file);
260 +vfs_writef_t vfs_writef(struct file *file);
261 +
262  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
263  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
264  extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
265 @@ -2199,6 +2207,7 @@ extern int current_umask(void);
266  extern void ihold(struct inode * inode);
267  extern void iput(struct inode *);
268  extern int generic_update_time(struct inode *, struct timespec *, int);
269 +extern int update_time(struct inode *, struct timespec *, int);
270  
271  /* /sys/fs */
272  extern struct kobject *fs_kobj;
273 @@ -2485,6 +2494,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
274         return false;
275  }
276  #endif
277 +extern int __sync_filesystem(struct super_block *, int);
278  extern int sync_filesystem(struct super_block *);
279  extern const struct file_operations def_blk_fops;
280  extern const struct file_operations def_chr_fops;
281 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
282 index 6fc77d4..27e76f0 100644
283 --- a/include/linux/lockdep.h
284 +++ b/include/linux/lockdep.h
285 @@ -313,6 +313,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
286         return lock->key == key;
287  }
288  
289 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
290 +
291  /*
292   * Acquire a lock.
293   *
294 @@ -439,6 +441,7 @@ struct lockdep_map { };
295  
296  #define lockdep_depth(tsk)     (0)
297  
298 +#define lockdep_is_held(lock)                  (1)
299  #define lockdep_is_held_type(l, r)             (1)
300  
301  #define lockdep_assert_held(l)                 do { (void)(l); } while (0)
302 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
303 index 3594208..24f5fd1 100644
304 --- a/include/linux/mnt_namespace.h
305 +++ b/include/linux/mnt_namespace.h
306 @@ -6,11 +6,14 @@
307  struct mnt_namespace;
308  struct fs_struct;
309  struct user_namespace;
310 +struct vfsmount;
311  
312  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
313                 struct user_namespace *, struct fs_struct *);
314  extern void put_mnt_ns(struct mnt_namespace *ns);
315  
316 +extern int is_current_mnt_ns(struct vfsmount *mnt);
317 +
318  extern const struct file_operations proc_mounts_operations;
319  extern const struct file_operations proc_mountinfo_operations;
320  extern const struct file_operations proc_mountstats_operations;
321 diff --git a/include/linux/splice.h b/include/linux/splice.h
322 index 74b4911..19789fb 100644
323 --- a/include/linux/splice.h
324 +++ b/include/linux/splice.h
325 @@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
326  
327  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
328  extern const struct pipe_buf_operations default_pipe_buf_ops;
329 +
330 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
331 +                          loff_t *ppos, size_t len, unsigned int flags);
332 +extern long do_splice_to(struct file *in, loff_t *ppos,
333 +                        struct pipe_inode_info *pipe, size_t len,
334 +                        unsigned int flags);
335  #endif
336 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
337 index 0233863..06e0d7a 100644
338 --- a/kernel/locking/lockdep.c
339 +++ b/kernel/locking/lockdep.c
340 @@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
341  unsigned long nr_lock_classes;
342  static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
343  
344 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
345 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
346  {
347         if (!hlock->class_idx) {
348                 /*
349 @@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
350         }
351         return lock_classes + hlock->class_idx - 1;
352  }
353 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
354  
355  #ifdef CONFIG_LOCK_STAT
356  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
357 SPDX-License-Identifier: GPL-2.0
358 aufs4.17 mmap patch
359
360 diff --git a/fs/proc/base.c b/fs/proc/base.c
361 index 1a76d75..77f698e 100644
362 --- a/fs/proc/base.c
363 +++ b/fs/proc/base.c
364 @@ -2024,7 +2024,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
365         down_read(&mm->mmap_sem);
366         vma = find_exact_vma(mm, vm_start, vm_end);
367         if (vma && vma->vm_file) {
368 -               *path = vma->vm_file->f_path;
369 +               *path = vma_pr_or_file(vma)->f_path;
370                 path_get(path);
371                 rc = 0;
372         }
373 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
374 index 7563437..7c0dc0f 100644
375 --- a/fs/proc/nommu.c
376 +++ b/fs/proc/nommu.c
377 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
378         file = region->vm_file;
379  
380         if (file) {
381 -               struct inode *inode = file_inode(region->vm_file);
382 +               struct inode *inode;
383 +
384 +               file = vmr_pr_or_file(region);
385 +               inode = file_inode(file);
386                 dev = inode->i_sb->s_dev;
387                 ino = inode->i_ino;
388         }
389 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
390 index c486ad4..76b71f8 100644
391 --- a/fs/proc/task_mmu.c
392 +++ b/fs/proc/task_mmu.c
393 @@ -305,7 +305,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
394         const char *name = NULL;
395  
396         if (file) {
397 -               struct inode *inode = file_inode(vma->vm_file);
398 +               struct inode *inode;
399 +
400 +               file = vma_pr_or_file(vma);
401 +               inode = file_inode(file);
402                 dev = inode->i_sb->s_dev;
403                 ino = inode->i_ino;
404                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
405 @@ -1726,7 +1729,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
406         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
407         struct vm_area_struct *vma = v;
408         struct numa_maps *md = &numa_priv->md;
409 -       struct file *file = vma->vm_file;
410 +       struct file *file = vma_pr_or_file(vma);
411         struct mm_struct *mm = vma->vm_mm;
412         struct mm_walk walk = {
413                 .hugetlb_entry = gather_hugetlb_stats,
414 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
415 index 5b62f57..dfb4a3b 100644
416 --- a/fs/proc/task_nommu.c
417 +++ b/fs/proc/task_nommu.c
418 @@ -156,7 +156,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
419         file = vma->vm_file;
420  
421         if (file) {
422 -               struct inode *inode = file_inode(vma->vm_file);
423 +               struct inode *inode;
424 +
425 +               file = vma_pr_or_file(vma);
426 +               inode = file_inode(file);
427                 dev = inode->i_sb->s_dev;
428                 ino = inode->i_ino;
429                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
430 diff --git a/include/linux/mm.h b/include/linux/mm.h
431 index 02a616e..01b3bb9 100644
432 --- a/include/linux/mm.h
433 +++ b/include/linux/mm.h
434 @@ -1380,6 +1380,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
435         unmap_mapping_range(mapping, holebegin, holelen, 0);
436  }
437  
438 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
439 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
440 +                                     int);
441 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
442 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
443 +
444 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
445 +                                                               __LINE__)
446 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
447 +                                                         __LINE__)
448 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
449 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
450 +
451 +#ifndef CONFIG_MMU
452 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
453 +extern void vmr_do_fput(struct vm_region *, const char[], int);
454 +
455 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
456 +                                                         __LINE__)
457 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
458 +#endif /* !CONFIG_MMU */
459 +
460  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
461                 void *buf, int len, unsigned int gup_flags);
462  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
463 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
464 index 2161234..78aa367 100644
465 --- a/include/linux/mm_types.h
466 +++ b/include/linux/mm_types.h
467 @@ -251,6 +251,7 @@ struct vm_region {
468         unsigned long   vm_top;         /* region allocated to here */
469         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
470         struct file     *vm_file;       /* the backing file or NULL */
471 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
472  
473         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
474         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
475 @@ -325,6 +326,7 @@ struct vm_area_struct {
476         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
477                                            units */
478         struct file * vm_file;          /* File we map to (can be NULL). */
479 +       struct file *vm_prfile;         /* shadow of vm_file */
480         void * vm_private_data;         /* was vm_pte (shared mem) */
481  
482         atomic_long_t swap_readahead_info;
483 diff --git a/kernel/fork.c b/kernel/fork.c
484 index a5d21c4..e965e09 100644
485 --- a/kernel/fork.c
486 +++ b/kernel/fork.c
487 @@ -473,7 +473,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
488                         struct inode *inode = file_inode(file);
489                         struct address_space *mapping = file->f_mapping;
490  
491 -                       get_file(file);
492 +                       vma_get_file(tmp);
493                         if (tmp->vm_flags & VM_DENYWRITE)
494                                 atomic_dec(&inode->i_writecount);
495                         i_mmap_lock_write(mapping);
496 diff --git a/mm/Makefile b/mm/Makefile
497 index b4e54a9a..77892ae 100644
498 --- a/mm/Makefile
499 +++ b/mm/Makefile
500 @@ -39,7 +39,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
501                            mm_init.o mmu_context.o percpu.o slab_common.o \
502                            compaction.o vmacache.o \
503                            interval_tree.o list_lru.o workingset.o \
504 -                          debug.o $(mmu-y)
505 +                          prfile.o debug.o $(mmu-y)
506  
507  obj-y += init-mm.o
508  
509 diff --git a/mm/filemap.c b/mm/filemap.c
510 index 0604cb0..45d2369 100644
511 --- a/mm/filemap.c
512 +++ b/mm/filemap.c
513 @@ -2700,7 +2700,7 @@ int filemap_page_mkwrite(struct vm_fault *vmf)
514         int ret = VM_FAULT_LOCKED;
515  
516         sb_start_pagefault(inode->i_sb);
517 -       file_update_time(vmf->vma->vm_file);
518 +       vma_file_update_time(vmf->vma);
519         lock_page(page);
520         if (page->mapping != inode->i_mapping) {
521                 unlock_page(page);
522 diff --git a/mm/mmap.c b/mm/mmap.c
523 index fc41c05..e376869 100644
524 --- a/mm/mmap.c
525 +++ b/mm/mmap.c
526 @@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
527         if (vma->vm_ops && vma->vm_ops->close)
528                 vma->vm_ops->close(vma);
529         if (vma->vm_file)
530 -               fput(vma->vm_file);
531 +               vma_fput(vma);
532         mpol_put(vma_policy(vma));
533         kmem_cache_free(vm_area_cachep, vma);
534         return next;
535 @@ -905,7 +905,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
536         if (remove_next) {
537                 if (file) {
538                         uprobe_munmap(next, next->vm_start, next->vm_end);
539 -                       fput(file);
540 +                       vma_fput(vma);
541                 }
542                 if (next->anon_vma)
543                         anon_vma_merge(vma, next);
544 @@ -1820,8 +1820,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
545         return addr;
546  
547  unmap_and_free_vma:
548 +       vma_fput(vma);
549         vma->vm_file = NULL;
550 -       fput(file);
551  
552         /* Undo any partial mapping done by a device driver. */
553         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
554 @@ -2645,7 +2645,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
555                 goto out_free_mpol;
556  
557         if (new->vm_file)
558 -               get_file(new->vm_file);
559 +               vma_get_file(new);
560  
561         if (new->vm_ops && new->vm_ops->open)
562                 new->vm_ops->open(new);
563 @@ -2664,7 +2664,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
564         if (new->vm_ops && new->vm_ops->close)
565                 new->vm_ops->close(new);
566         if (new->vm_file)
567 -               fput(new->vm_file);
568 +               vma_fput(new);
569         unlink_anon_vmas(new);
570   out_free_mpol:
571         mpol_put(vma_policy(new));
572 @@ -2826,7 +2826,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
573         struct vm_area_struct *vma;
574         unsigned long populate = 0;
575         unsigned long ret = -EINVAL;
576 -       struct file *file;
577 +       struct file *file, *prfile;
578  
579         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
580                      current->comm, current->pid);
581 @@ -2901,10 +2901,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
582                 }
583         }
584  
585 -       file = get_file(vma->vm_file);
586 +       vma_get_file(vma);
587 +       file = vma->vm_file;
588 +       prfile = vma->vm_prfile;
589         ret = do_mmap_pgoff(vma->vm_file, start, size,
590                         prot, flags, pgoff, &populate, NULL);
591 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
592 +               struct vm_area_struct *new_vma;
593 +
594 +               new_vma = find_vma(mm, ret);
595 +               if (!new_vma->vm_prfile)
596 +                       new_vma->vm_prfile = prfile;
597 +               if (new_vma != vma)
598 +                       get_file(prfile);
599 +       }
600 +       /*
601 +        * two fput()s instead of vma_fput(vma),
602 +        * coz vma may not be available anymore.
603 +        */
604         fput(file);
605 +       if (prfile)
606 +               fput(prfile);
607  out:
608         up_write(&mm->mmap_sem);
609         if (populate)
610 @@ -3220,7 +3237,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
611                 if (anon_vma_clone(new_vma, vma))
612                         goto out_free_mempol;
613                 if (new_vma->vm_file)
614 -                       get_file(new_vma->vm_file);
615 +                       vma_get_file(new_vma);
616                 if (new_vma->vm_ops && new_vma->vm_ops->open)
617                         new_vma->vm_ops->open(new_vma);
618                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
619 diff --git a/mm/nommu.c b/mm/nommu.c
620 index 1372373..6362dde 100644
621 --- a/mm/nommu.c
622 +++ b/mm/nommu.c
623 @@ -629,7 +629,7 @@ static void __put_nommu_region(struct vm_region *region)
624                 up_write(&nommu_region_sem);
625  
626                 if (region->vm_file)
627 -                       fput(region->vm_file);
628 +                       vmr_fput(region);
629  
630                 /* IO memory and memory shared directly out of the pagecache
631                  * from ramfs/tmpfs mustn't be released here */
632 @@ -767,7 +767,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
633         if (vma->vm_ops && vma->vm_ops->close)
634                 vma->vm_ops->close(vma);
635         if (vma->vm_file)
636 -               fput(vma->vm_file);
637 +               vma_fput(vma);
638         put_nommu_region(vma->vm_region);
639         kmem_cache_free(vm_area_cachep, vma);
640  }
641 @@ -1289,7 +1289,7 @@ unsigned long do_mmap(struct file *file,
642                                         goto error_just_free;
643                                 }
644                         }
645 -                       fput(region->vm_file);
646 +                       vmr_fput(region);
647                         kmem_cache_free(vm_region_jar, region);
648                         region = pregion;
649                         result = start;
650 @@ -1364,10 +1364,10 @@ unsigned long do_mmap(struct file *file,
651         up_write(&nommu_region_sem);
652  error:
653         if (region->vm_file)
654 -               fput(region->vm_file);
655 +               vmr_fput(region);
656         kmem_cache_free(vm_region_jar, region);
657         if (vma->vm_file)
658 -               fput(vma->vm_file);
659 +               vma_fput(vma);
660         kmem_cache_free(vm_area_cachep, vma);
661         return ret;
662  
663 diff --git a/mm/prfile.c b/mm/prfile.c
664 new file mode 100644
665 index 0000000..37e95e1
666 --- /dev/null
667 +++ b/mm/prfile.c
668 @@ -0,0 +1,86 @@
669 +/* SPDX-License-Identifier: GPL-2.0 */
670 +/*
671 + * Mainly for aufs which mmap(2) different file and wants to print different
672 + * path in /proc/PID/maps.
673 + * Call these functions via macros defined in linux/mm.h.
674 + *
675 + * See Documentation/filesystems/aufs/design/06mmap.txt
676 + *
677 + * Copyright (c) 2014-2018 Junjro R. Okajima
678 + * Copyright (c) 2014 Ian Campbell
679 + */
680 +
681 +#include <linux/mm.h>
682 +#include <linux/file.h>
683 +#include <linux/fs.h>
684 +
685 +/* #define PRFILE_TRACE */
686 +static inline void prfile_trace(struct file *f, struct file *pr,
687 +                             const char func[], int line, const char func2[])
688 +{
689 +#ifdef PRFILE_TRACE
690 +       if (pr)
691 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
692 +#endif
693 +}
694 +
695 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
696 +                            int line)
697 +{
698 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
699 +
700 +       prfile_trace(f, pr, func, line, __func__);
701 +       file_update_time(f);
702 +       if (f && pr)
703 +               file_update_time(pr);
704 +}
705 +
706 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
707 +                              int line)
708 +{
709 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
710 +
711 +       prfile_trace(f, pr, func, line, __func__);
712 +       return (f && pr) ? pr : f;
713 +}
714 +
715 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
716 +{
717 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
718 +
719 +       prfile_trace(f, pr, func, line, __func__);
720 +       get_file(f);
721 +       if (f && pr)
722 +               get_file(pr);
723 +}
724 +
725 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
726 +{
727 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
728 +
729 +       prfile_trace(f, pr, func, line, __func__);
730 +       fput(f);
731 +       if (f && pr)
732 +               fput(pr);
733 +}
734 +
735 +#ifndef CONFIG_MMU
736 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
737 +                              int line)
738 +{
739 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
740 +
741 +       prfile_trace(f, pr, func, line, __func__);
742 +       return (f && pr) ? pr : f;
743 +}
744 +
745 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
746 +{
747 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
748 +
749 +       prfile_trace(f, pr, func, line, __func__);
750 +       fput(f);
751 +       if (f && pr)
752 +               fput(pr);
753 +}
754 +#endif /* !CONFIG_MMU */
755 SPDX-License-Identifier: GPL-2.0
756 aufs4.17 standalone patch
757
758 diff --git a/fs/dcache.c b/fs/dcache.c
759 index ff338e2..3e2bae8 100644
760 --- a/fs/dcache.c
761 +++ b/fs/dcache.c
762 @@ -1342,6 +1342,7 @@ void d_walk(struct dentry *parent, void *data,
763         seq = 1;
764         goto again;
765  }
766 +EXPORT_SYMBOL_GPL(d_walk);
767  
768  struct check_mount {
769         struct vfsmount *mnt;
770 @@ -2942,6 +2943,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
771  
772         write_sequnlock(&rename_lock);
773  }
774 +EXPORT_SYMBOL_GPL(d_exchange);
775  
776  /**
777   * d_ancestor - search for an ancestor
778 diff --git a/fs/exec.c b/fs/exec.c
779 index 183059c..35adee4 100644
780 --- a/fs/exec.c
781 +++ b/fs/exec.c
782 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
783         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
784                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
785  }
786 +EXPORT_SYMBOL_GPL(path_noexec);
787  
788  #ifdef CONFIG_USELIB
789  /*
790 diff --git a/fs/fcntl.c b/fs/fcntl.c
791 index 7550799..d403576 100644
792 --- a/fs/fcntl.c
793 +++ b/fs/fcntl.c
794 @@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
795   out:
796         return error;
797  }
798 +EXPORT_SYMBOL_GPL(setfl);
799  
800  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
801                       int force)
802 diff --git a/fs/file_table.c b/fs/file_table.c
803 index 7ec0b3e..819ee07 100644
804 --- a/fs/file_table.c
805 +++ b/fs/file_table.c
806 @@ -147,6 +147,7 @@ struct file *get_empty_filp(void)
807         }
808         return ERR_PTR(-ENFILE);
809  }
810 +EXPORT_SYMBOL_GPL(get_empty_filp);
811  
812  /**
813   * alloc_file - allocate and initialize a 'struct file'
814 @@ -257,6 +258,7 @@ void flush_delayed_fput(void)
815  {
816         delayed_fput(NULL);
817  }
818 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
819  
820  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
821  
822 @@ -299,6 +301,7 @@ void __fput_sync(struct file *file)
823  }
824  
825  EXPORT_SYMBOL(fput);
826 +EXPORT_SYMBOL_GPL(__fput_sync);
827  
828  void put_filp(struct file *file)
829  {
830 @@ -307,6 +310,7 @@ void put_filp(struct file *file)
831                 file_free(file);
832         }
833  }
834 +EXPORT_SYMBOL_GPL(put_filp);
835  
836  void __init files_init(void)
837  {
838 diff --git a/fs/inode.c b/fs/inode.c
839 index e0c5255..ff36056 100644
840 --- a/fs/inode.c
841 +++ b/fs/inode.c
842 @@ -1672,6 +1672,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
843  
844         return update_time(inode, time, flags);
845  }
846 +EXPORT_SYMBOL_GPL(update_time);
847  
848  /**
849   *     touch_atime     -       update the access time
850 diff --git a/fs/namespace.c b/fs/namespace.c
851 index 61129ff..5d3e0382 100644
852 --- a/fs/namespace.c
853 +++ b/fs/namespace.c
854 @@ -517,6 +517,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
855         mnt_dec_writers(real_mount(mnt));
856         preempt_enable();
857  }
858 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
859  
860  /**
861   * mnt_drop_write - give up write access to a mount
862 @@ -851,6 +852,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
863  {
864         return check_mnt(real_mount(mnt));
865  }
866 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
867  
868  /*
869   * vfsmount lock must be held for write
870 @@ -1893,6 +1895,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
871         }
872         return 0;
873  }
874 +EXPORT_SYMBOL_GPL(iterate_mounts);
875  
876  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
877  {
878 diff --git a/fs/notify/group.c b/fs/notify/group.c
879 index b7a4b6a..5a69d60 100644
880 --- a/fs/notify/group.c
881 +++ b/fs/notify/group.c
882 @@ -22,6 +22,7 @@
883  #include <linux/srcu.h>
884  #include <linux/rculist.h>
885  #include <linux/wait.h>
886 +#include <linux/module.h>
887  
888  #include <linux/fsnotify_backend.h>
889  #include "fsnotify.h"
890 @@ -109,6 +110,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
891  {
892         refcount_inc(&group->refcnt);
893  }
894 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
895  
896  /*
897   * Drop a reference to a group.  Free it if it's through.
898 @@ -118,6 +120,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
899         if (refcount_dec_and_test(&group->refcnt))
900                 fsnotify_final_destroy_group(group);
901  }
902 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
903  
904  /*
905   * Create a new fsnotify_group and hold a reference for the group returned.
906 @@ -147,6 +150,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
907  
908         return group;
909  }
910 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
911  
912  int fsnotify_fasync(int fd, struct file *file, int on)
913  {
914 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
915 index e9191b4..1f8ccfa 100644
916 --- a/fs/notify/mark.c
917 +++ b/fs/notify/mark.c
918 @@ -108,6 +108,7 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
919         WARN_ON_ONCE(!refcount_read(&mark->refcnt));
920         refcount_inc(&mark->refcnt);
921  }
922 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
923  
924  static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
925  {
926 @@ -392,6 +393,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
927         mutex_unlock(&group->mark_mutex);
928         fsnotify_free_mark(mark);
929  }
930 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
931  
932  /*
933   * Sorting function for lists of fsnotify marks.
934 @@ -606,6 +608,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
935         fsnotify_put_mark(mark);
936         return ret;
937  }
938 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
939  
940  int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
941                       struct vfsmount *mnt, int allow_dups)
942 @@ -741,6 +744,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
943         fsnotify_get_group(group);
944         mark->group = group;
945  }
946 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
947  
948  /*
949   * Destroy all marks in destroy_list, waits for SRCU period to finish before
950 diff --git a/fs/open.c b/fs/open.c
951 index d0e955b..527bc1a 100644
952 --- a/fs/open.c
953 +++ b/fs/open.c
954 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
955         inode_unlock(dentry->d_inode);
956         return ret;
957  }
958 +EXPORT_SYMBOL_GPL(do_truncate);
959  
960  long vfs_truncate(const struct path *path, loff_t length)
961  {
962 @@ -723,6 +724,7 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
963  {
964         return ksys_fchown(fd, user, group);
965  }
966 +EXPORT_SYMBOL_GPL(open_check_o_direct);
967  
968  int open_check_o_direct(struct file *f)
969  {
970 diff --git a/fs/read_write.c b/fs/read_write.c
971 index ddd6e67..aabf92d 100644
972 --- a/fs/read_write.c
973 +++ b/fs/read_write.c
974 @@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
975  
976         return ret;
977  }
978 +EXPORT_SYMBOL_GPL(vfs_read);
979  
980  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
981  {
982 @@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
983                 return new_sync_read;
984         return ERR_PTR(-ENOSYS);
985  }
986 +EXPORT_SYMBOL_GPL(vfs_readf);
987  
988  vfs_writef_t vfs_writef(struct file *file)
989  {
990 @@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
991                 return new_sync_write;
992         return ERR_PTR(-ENOSYS);
993  }
994 +EXPORT_SYMBOL_GPL(vfs_writef);
995  
996  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
997  {
998 @@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
999  
1000         return ret;
1001  }
1002 +EXPORT_SYMBOL_GPL(vfs_write);
1003  
1004  static inline loff_t file_pos_read(struct file *file)
1005  {
1006 diff --git a/fs/splice.c b/fs/splice.c
1007 index f617ab0..ec0ad02 100644
1008 --- a/fs/splice.c
1009 +++ b/fs/splice.c
1010 @@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
1011  
1012         return splice_write(pipe, out, ppos, len, flags);
1013  }
1014 +EXPORT_SYMBOL_GPL(do_splice_from);
1015  
1016  /*
1017   * Attempt to initiate a splice from a file to a pipe.
1018 @@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
1019  
1020         return splice_read(in, ppos, pipe, len, flags);
1021  }
1022 +EXPORT_SYMBOL_GPL(do_splice_to);
1023  
1024  /**
1025   * splice_direct_to_actor - splices data directly between two non-pipes
1026 diff --git a/fs/sync.c b/fs/sync.c
1027 index 2860782..ffd7ea4 100644
1028 --- a/fs/sync.c
1029 +++ b/fs/sync.c
1030 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
1031                 sb->s_op->sync_fs(sb, wait);
1032         return __sync_blockdev(sb->s_bdev, wait);
1033  }
1034 +EXPORT_SYMBOL_GPL(__sync_filesystem);
1035  
1036  /*
1037   * Write out and wait upon all dirty data associated with this
1038 diff --git a/fs/xattr.c b/fs/xattr.c
1039 index 61cd28b..35570cd 100644
1040 --- a/fs/xattr.c
1041 +++ b/fs/xattr.c
1042 @@ -297,6 +297,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
1043         *xattr_value = value;
1044         return error;
1045  }
1046 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
1047  
1048  ssize_t
1049  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
1050 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
1051 index 06e0d7a..6af91bd 100644
1052 --- a/kernel/locking/lockdep.c
1053 +++ b/kernel/locking/lockdep.c
1054 @@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
1055         }
1056         return lock_classes + hlock->class_idx - 1;
1057  }
1058 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1059  #define hlock_class(hlock) lockdep_hlock_class(hlock)
1060  
1061  #ifdef CONFIG_LOCK_STAT
1062 diff --git a/kernel/task_work.c b/kernel/task_work.c
1063 index 0fef395..83fb1ec 100644
1064 --- a/kernel/task_work.c
1065 +++ b/kernel/task_work.c
1066 @@ -116,3 +116,4 @@ void task_work_run(void)
1067                 } while (work);
1068         }
1069  }
1070 +EXPORT_SYMBOL_GPL(task_work_run);
1071 diff --git a/security/commoncap.c b/security/commoncap.c
1072 index 1ce701f..a0d106e 100644
1073 --- a/security/commoncap.c
1074 +++ b/security/commoncap.c
1075 @@ -1332,12 +1332,14 @@ int cap_mmap_addr(unsigned long addr)
1076         }
1077         return ret;
1078  }
1079 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
1080  
1081  int cap_mmap_file(struct file *file, unsigned long reqprot,
1082                   unsigned long prot, unsigned long flags)
1083  {
1084         return 0;
1085  }
1086 +EXPORT_SYMBOL_GPL(cap_mmap_file);
1087  
1088  #ifdef CONFIG_SECURITY
1089  
1090 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
1091 index c65b39b..e363d22 100644
1092 --- a/security/device_cgroup.c
1093 +++ b/security/device_cgroup.c
1094 @@ -8,6 +8,7 @@
1095  #include <linux/device_cgroup.h>
1096  #include <linux/cgroup.h>
1097  #include <linux/ctype.h>
1098 +#include <linux/export.h>
1099  #include <linux/list.h>
1100  #include <linux/uaccess.h>
1101  #include <linux/seq_file.h>
1102 @@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
1103  
1104         return 0;
1105  }
1106 +EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
1107 diff --git a/security/security.c b/security/security.c
1108 index 7bc2fde..6bd0468 100644
1109 --- a/security/security.c
1110 +++ b/security/security.c
1111 @@ -537,6 +537,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1112                 return 0;
1113         return call_int_hook(path_rmdir, 0, dir, dentry);
1114  }
1115 +EXPORT_SYMBOL_GPL(security_path_rmdir);
1116  
1117  int security_path_unlink(const struct path *dir, struct dentry *dentry)
1118  {
1119 @@ -553,6 +554,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
1120                 return 0;
1121         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1122  }
1123 +EXPORT_SYMBOL_GPL(security_path_symlink);
1124  
1125  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1126                        struct dentry *new_dentry)
1127 @@ -561,6 +563,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1128                 return 0;
1129         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1130  }
1131 +EXPORT_SYMBOL_GPL(security_path_link);
1132  
1133  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1134                          const struct path *new_dir, struct dentry *new_dentry,
1135 @@ -588,6 +591,7 @@ int security_path_truncate(const struct path *path)
1136                 return 0;
1137         return call_int_hook(path_truncate, 0, path);
1138  }
1139 +EXPORT_SYMBOL_GPL(security_path_truncate);
1140  
1141  int security_path_chmod(const struct path *path, umode_t mode)
1142  {
1143 @@ -595,6 +599,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1144                 return 0;
1145         return call_int_hook(path_chmod, 0, path, mode);
1146  }
1147 +EXPORT_SYMBOL_GPL(security_path_chmod);
1148  
1149  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1150  {
1151 @@ -602,6 +607,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1152                 return 0;
1153         return call_int_hook(path_chown, 0, path, uid, gid);
1154  }
1155 +EXPORT_SYMBOL_GPL(security_path_chown);
1156  
1157  int security_path_chroot(const struct path *path)
1158  {
1159 @@ -687,6 +693,7 @@ int security_inode_readlink(struct dentry *dentry)
1160                 return 0;
1161         return call_int_hook(inode_readlink, 0, dentry);
1162  }
1163 +EXPORT_SYMBOL_GPL(security_inode_readlink);
1164  
1165  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1166                                bool rcu)
1167 @@ -702,6 +709,7 @@ int security_inode_permission(struct inode *inode, int mask)
1168                 return 0;
1169         return call_int_hook(inode_permission, 0, inode, mask);
1170  }
1171 +EXPORT_SYMBOL_GPL(security_inode_permission);
1172  
1173  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1174  {
1175 @@ -873,6 +881,7 @@ int security_file_permission(struct file *file, int mask)
1176  
1177         return fsnotify_perm(file, mask);
1178  }
1179 +EXPORT_SYMBOL_GPL(security_file_permission);
1180  
1181  int security_file_alloc(struct file *file)
1182  {
1183 @@ -932,6 +941,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1184                 return ret;
1185         return ima_file_mmap(file, prot);
1186  }
1187 +EXPORT_SYMBOL_GPL(security_mmap_file);
1188  
1189  int security_mmap_addr(unsigned long addr)
1190  {
1191 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1192 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1193 +++ linux/Documentation/ABI/testing/debugfs-aufs        2018-07-19 09:46:13.053314374 +0200
1194 @@ -0,0 +1,55 @@
1195 +What:          /debug/aufs/si_<id>/
1196 +Date:          March 2009
1197 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1198 +Description:
1199 +               Under /debug/aufs, a directory named si_<id> is created
1200 +               per aufs mount, where <id> is a unique id generated
1201 +               internally.
1202 +
1203 +What:          /debug/aufs/si_<id>/plink
1204 +Date:          Apr 2013
1205 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1206 +Description:
1207 +               It has three lines and shows the information about the
1208 +               pseudo-link. The first line is a single number
1209 +               representing a number of buckets. The second line is a
1210 +               number of pseudo-links per buckets (separated by a
1211 +               blank). The last line is a single number representing a
1212 +               total number of psedo-links.
1213 +               When the aufs mount option 'noplink' is specified, it
1214 +               will show "1\n0\n0\n".
1215 +
1216 +What:          /debug/aufs/si_<id>/xib
1217 +Date:          March 2009
1218 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1219 +Description:
1220 +               It shows the consumed blocks by xib (External Inode Number
1221 +               Bitmap), its block size and file size.
1222 +               When the aufs mount option 'noxino' is specified, it
1223 +               will be empty. About XINO files, see the aufs manual.
1224 +
1225 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1226 +Date:          March 2009
1227 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1228 +Description:
1229 +               It shows the consumed blocks by xino (External Inode Number
1230 +               Translation Table), its link count, block size and file
1231 +               size.
1232 +               Due to the file size limit, there may exist multiple
1233 +               xino files per branch.  In this case, "-N" is added to
1234 +               the filename and it corresponds to the index of the
1235 +               internal xino array.  "-0" is omitted.
1236 +               When the aufs mount option 'noxino' is specified, Those
1237 +               entries won't exist.  About XINO files, see the aufs
1238 +               manual.
1239 +
1240 +What:          /debug/aufs/si_<id>/xigen
1241 +Date:          March 2009
1242 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1243 +Description:
1244 +               It shows the consumed blocks by xigen (External Inode
1245 +               Generation Table), its block size and file size.
1246 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1247 +               be created.
1248 +               When the aufs mount option 'noxino' is specified, it
1249 +               will be empty. About XINO files, see the aufs manual.
1250 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1251 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1252 +++ linux/Documentation/ABI/testing/sysfs-aufs  2017-07-29 12:14:25.893041746 +0200
1253 @@ -0,0 +1,31 @@
1254 +What:          /sys/fs/aufs/si_<id>/
1255 +Date:          March 2009
1256 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1257 +Description:
1258 +               Under /sys/fs/aufs, a directory named si_<id> is created
1259 +               per aufs mount, where <id> is a unique id generated
1260 +               internally.
1261 +
1262 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1263 +Date:          March 2009
1264 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1265 +Description:
1266 +               It shows the abolute path of a member directory (which
1267 +               is called branch) in aufs, and its permission.
1268 +
1269 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1270 +Date:          July 2013
1271 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1272 +Description:
1273 +               It shows the id of a member directory (which is called
1274 +               branch) in aufs.
1275 +
1276 +What:          /sys/fs/aufs/si_<id>/xi_path
1277 +Date:          March 2009
1278 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1279 +Description:
1280 +               It shows the abolute path of XINO (External Inode Number
1281 +               Bitmap, Translation Table and Generation Table) file
1282 +               even if it is the default path.
1283 +               When the aufs mount option 'noxino' is specified, it
1284 +               will be empty. About XINO files, see the aufs manual.
1285 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1286 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1287 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2018-04-15 08:49:13.394483860 +0200
1288 @@ -0,0 +1,171 @@
1289 +
1290 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1291 +# 
1292 +# This program is free software; you can redistribute it and/or modify
1293 +# it under the terms of the GNU General Public License as published by
1294 +# the Free Software Foundation; either version 2 of the License, or
1295 +# (at your option) any later version.
1296 +# 
1297 +# This program is distributed in the hope that it will be useful,
1298 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1299 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1300 +# GNU General Public License for more details.
1301 +# 
1302 +# You should have received a copy of the GNU General Public License
1303 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1304 +
1305 +Introduction
1306 +----------------------------------------
1307 +
1308 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1309 +1. abbrev. for "advanced multi-layered unification filesystem".
1310 +2. abbrev. for "another unionfs".
1311 +3. abbrev. for "auf das" in German which means "on the" in English.
1312 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1313 +   But "Filesystem aufs Filesystem" is hard to understand.
1314 +4. abbrev. for "African Urban Fashion Show".
1315 +
1316 +AUFS is a filesystem with features:
1317 +- multi layered stackable unification filesystem, the member directory
1318 +  is called as a branch.
1319 +- branch permission and attribute, 'readonly', 'real-readonly',
1320 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1321 +  combination.
1322 +- internal "file copy-on-write".
1323 +- logical deletion, whiteout.
1324 +- dynamic branch manipulation, adding, deleting and changing permission.
1325 +- allow bypassing aufs, user's direct branch access.
1326 +- external inode number translation table and bitmap which maintains the
1327 +  persistent aufs inode number.
1328 +- seekable directory, including NFS readdir.
1329 +- file mapping, mmap and sharing pages.
1330 +- pseudo-link, hardlink over branches.
1331 +- loopback mounted filesystem as a branch.
1332 +- several policies to select one among multiple writable branches.
1333 +- revert a single systemcall when an error occurs in aufs.
1334 +- and more...
1335 +
1336 +
1337 +Multi Layered Stackable Unification Filesystem
1338 +----------------------------------------------------------------------
1339 +Most people already knows what it is.
1340 +It is a filesystem which unifies several directories and provides a
1341 +merged single directory. When users access a file, the access will be
1342 +passed/re-directed/converted (sorry, I am not sure which English word is
1343 +correct) to the real file on the member filesystem. The member
1344 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1345 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1346 +readonly branch is handled by creating 'whiteout' on the upper writable
1347 +branch.
1348 +
1349 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1350 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1351 +different approaches to implement the merged-view.
1352 +The former tries putting it into VFS, and the latter implements as a
1353 +separate filesystem.
1354 +(If I misunderstand about these implementations, please let me know and
1355 +I shall correct it. Because it is a long time ago when I read their
1356 +source files last time).
1357 +
1358 +UnionMount's approach will be able to small, but may be hard to share
1359 +branches between several UnionMount since the whiteout in it is
1360 +implemented in the inode on branch filesystem and always
1361 +shared. According to Bharata's post, readdir does not seems to be
1362 +finished yet.
1363 +There are several missing features known in this implementations such as
1364 +- for users, the inode number may change silently. eg. copy-up.
1365 +- link(2) may break by copy-up.
1366 +- read(2) may get an obsoleted filedata (fstat(2) too).
1367 +- fcntl(F_SETLK) may be broken by copy-up.
1368 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1369 +  open(O_RDWR).
1370 +
1371 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1372 +merged into mainline. This is another implementation of UnionMount as a
1373 +separated filesystem. All the limitations and known problems which
1374 +UnionMount are equally inherited to "overlay" filesystem.
1375 +
1376 +Unionfs has a longer history. When I started implementing a stackable
1377 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1378 +inode, dentry and file objects and they have an array pointing lower
1379 +same kind objects. After contributing many patches for Unionfs, I
1380 +re-started my project AUFS (Jun 2006).
1381 +
1382 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1383 +implemented my own ideas, approaches and enhancements and it became
1384 +totally different one.
1385 +
1386 +Comparing DM snapshot and fs based implementation
1387 +- the number of bytes to be copied between devices is much smaller.
1388 +- the type of filesystem must be one and only.
1389 +- the fs must be writable, no readonly fs, even for the lower original
1390 +  device. so the compression fs will not be usable. but if we use
1391 +  loopback mount, we may address this issue.
1392 +  for instance,
1393 +       mount /cdrom/squashfs.img /sq
1394 +       losetup /sq/ext2.img
1395 +       losetup /somewhere/cow
1396 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1397 +- it will be difficult (or needs more operations) to extract the
1398 +  difference between the original device and COW.
1399 +- DM snapshot-merge may help a lot when users try merging. in the
1400 +  fs-layer union, users will use rsync(1).
1401 +
1402 +You may want to read my old paper "Filesystems in LiveCD"
1403 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1404 +
1405 +
1406 +Several characters/aspects/persona of aufs
1407 +----------------------------------------------------------------------
1408 +
1409 +Aufs has several characters, aspects or persona.
1410 +1. a filesystem, callee of VFS helper
1411 +2. sub-VFS, caller of VFS helper for branches
1412 +3. a virtual filesystem which maintains persistent inode number
1413 +4. reader/writer of files on branches such like an application
1414 +
1415 +1. Callee of VFS Helper
1416 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1417 +unlink(2) from an application reaches sys_unlink() kernel function and
1418 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1419 +calls filesystem specific unlink operation. Actually aufs implements the
1420 +unlink operation but it behaves like a redirector.
1421 +
1422 +2. Caller of VFS Helper for Branches
1423 +aufs_unlink() passes the unlink request to the branch filesystem as if
1424 +it were called from VFS. So the called unlink operation of the branch
1425 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1426 +every necessary pre/post operation for the branch filesystem.
1427 +- acquire the lock for the parent dir on a branch
1428 +- lookup in a branch
1429 +- revalidate dentry on a branch
1430 +- mnt_want_write() for a branch
1431 +- vfs_unlink() for a branch
1432 +- mnt_drop_write() for a branch
1433 +- release the lock on a branch
1434 +
1435 +3. Persistent Inode Number
1436 +One of the most important issue for a filesystem is to maintain inode
1437 +numbers. This is particularly important to support exporting a
1438 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1439 +backend block device for its own. But some storage is necessary to
1440 +keep and maintain the inode numbers. It may be a large space and may not
1441 +suit to keep in memory. Aufs rents some space from its first writable
1442 +branch filesystem (by default) and creates file(s) on it. These files
1443 +are created by aufs internally and removed soon (currently) keeping
1444 +opened.
1445 +Note: Because these files are removed, they are totally gone after
1446 +      unmounting aufs. It means the inode numbers are not persistent
1447 +      across unmount or reboot. I have a plan to make them really
1448 +      persistent which will be important for aufs on NFS server.
1449 +
1450 +4. Read/Write Files Internally (copy-on-write)
1451 +Because a branch can be readonly, when you write a file on it, aufs will
1452 +"copy-up" it to the upper writable branch internally. And then write the
1453 +originally requested thing to the file. Generally kernel doesn't
1454 +open/read/write file actively. In aufs, even a single write may cause a
1455 +internal "file copy". This behaviour is very similar to cp(1) command.
1456 +
1457 +Some people may think it is better to pass such work to user space
1458 +helper, instead of doing in kernel space. Actually I am still thinking
1459 +about it. But currently I have implemented it in kernel space.
1460 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1461 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1462 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2018-04-15 08:49:13.394483860 +0200
1463 @@ -0,0 +1,258 @@
1464 +
1465 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1466 +# 
1467 +# This program is free software; you can redistribute it and/or modify
1468 +# it under the terms of the GNU General Public License as published by
1469 +# the Free Software Foundation; either version 2 of the License, or
1470 +# (at your option) any later version.
1471 +# 
1472 +# This program is distributed in the hope that it will be useful,
1473 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1474 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1475 +# GNU General Public License for more details.
1476 +# 
1477 +# You should have received a copy of the GNU General Public License
1478 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1479 +
1480 +Basic Aufs Internal Structure
1481 +
1482 +Superblock/Inode/Dentry/File Objects
1483 +----------------------------------------------------------------------
1484 +As like an ordinary filesystem, aufs has its own
1485 +superblock/inode/dentry/file objects. All these objects have a
1486 +dynamically allocated array and store the same kind of pointers to the
1487 +lower filesystem, branch.
1488 +For example, when you build a union with one readwrite branch and one
1489 +readonly, mounted /au, /rw and /ro respectively.
1490 +- /au = /rw + /ro
1491 +- /ro/fileA exists but /rw/fileA
1492 +
1493 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1494 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1495 +- [0] = NULL (because /rw/fileA doesn't exist)
1496 +- [1] = /ro/fileA
1497 +
1498 +This style of an array is essentially same to the aufs
1499 +superblock/inode/dentry/file objects.
1500 +
1501 +Because aufs supports manipulating branches, ie. add/delete/change
1502 +branches dynamically, these objects has its own generation. When
1503 +branches are changed, the generation in aufs superblock is
1504 +incremented. And a generation in other object are compared when it is
1505 +accessed. When a generation in other objects are obsoleted, aufs
1506 +refreshes the internal array.
1507 +
1508 +
1509 +Superblock
1510 +----------------------------------------------------------------------
1511 +Additionally aufs superblock has some data for policies to select one
1512 +among multiple writable branches, XIB files, pseudo-links and kobject.
1513 +See below in detail.
1514 +About the policies which supports copy-down a directory, see
1515 +wbr_policy.txt too.
1516 +
1517 +
1518 +Branch and XINO(External Inode Number Translation Table)
1519 +----------------------------------------------------------------------
1520 +Every branch has its own xino (external inode number translation table)
1521 +file. The xino file is created and unlinked by aufs internally. When two
1522 +members of a union exist on the same filesystem, they share the single
1523 +xino file.
1524 +The struct of a xino file is simple, just a sequence of aufs inode
1525 +numbers which is indexed by the lower inode number.
1526 +In the above sample, assume the inode number of /ro/fileA is i111 and
1527 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1528 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1529 +
1530 +When the inode numbers are not contiguous, the xino file will be sparse
1531 +which has a hole in it and doesn't consume as much disk space as it
1532 +might appear. If your branch filesystem consumes disk space for such
1533 +holes, then you should specify 'xino=' option at mounting aufs.
1534 +
1535 +Aufs has a mount option to free the disk blocks for such holes in XINO
1536 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1537 +meet a problem of disk shortage due to XINO files, then you should try
1538 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1539 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1540 +the holes in XINO files.
1541 +
1542 +Also a writable branch has three kinds of "whiteout bases". All these
1543 +are existed when the branch is joined to aufs, and their names are
1544 +whiteout-ed doubly, so that users will never see their names in aufs
1545 +hierarchy.
1546 +1. a regular file which will be hardlinked to all whiteouts.
1547 +2. a directory to store a pseudo-link.
1548 +3. a directory to store an "orphan"-ed file temporary.
1549 +
1550 +1. Whiteout Base
1551 +   When you remove a file on a readonly branch, aufs handles it as a
1552 +   logical deletion and creates a whiteout on the upper writable branch
1553 +   as a hardlink of this file in order not to consume inode on the
1554 +   writable branch.
1555 +2. Pseudo-link Dir
1556 +   See below, Pseudo-link.
1557 +3. Step-Parent Dir
1558 +   When "fileC" exists on the lower readonly branch only and it is
1559 +   opened and removed with its parent dir, and then user writes
1560 +   something into it, then aufs copies-up fileC to this
1561 +   directory. Because there is no other dir to store fileC. After
1562 +   creating a file under this dir, the file is unlinked.
1563 +
1564 +Because aufs supports manipulating branches, ie. add/delete/change
1565 +dynamically, a branch has its own id. When the branch order changes,
1566 +aufs finds the new index by searching the branch id.
1567 +
1568 +
1569 +Pseudo-link
1570 +----------------------------------------------------------------------
1571 +Assume "fileA" exists on the lower readonly branch only and it is
1572 +hardlinked to "fileB" on the branch. When you write something to fileA,
1573 +aufs copies-up it to the upper writable branch. Additionally aufs
1574 +creates a hardlink under the Pseudo-link Directory of the writable
1575 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1576 +simple list. If fileB is read after unlinking fileA, aufs returns
1577 +filedata from the pseudo-link instead of the lower readonly
1578 +branch. Because the pseudo-link is based upon the inode, to keep the
1579 +inode number by xino (see above) is essentially necessary.
1580 +
1581 +All the hardlinks under the Pseudo-link Directory of the writable branch
1582 +should be restored in a proper location later. Aufs provides a utility
1583 +to do this. The userspace helpers executed at remounting and unmounting
1584 +aufs by default.
1585 +During this utility is running, it puts aufs into the pseudo-link
1586 +maintenance mode. In this mode, only the process which began the
1587 +maintenance mode (and its child processes) is allowed to operate in
1588 +aufs. Some other processes which are not related to the pseudo-link will
1589 +be allowed to run too, but the rest have to return an error or wait
1590 +until the maintenance mode ends. If a process already acquires an inode
1591 +mutex (in VFS), it has to return an error.
1592 +
1593 +
1594 +XIB(external inode number bitmap)
1595 +----------------------------------------------------------------------
1596 +Addition to the xino file per a branch, aufs has an external inode number
1597 +bitmap in a superblock object. It is also an internal file such like a
1598 +xino file.
1599 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1600 +not.
1601 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1602 +
1603 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1604 +reduce the number of consumed disk blocks for these files.
1605 +
1606 +
1607 +Virtual or Vertical Dir, and Readdir in Userspace
1608 +----------------------------------------------------------------------
1609 +In order to support multiple layers (branches), aufs readdir operation
1610 +constructs a virtual dir block on memory. For readdir, aufs calls
1611 +vfs_readdir() internally for each dir on branches, merges their entries
1612 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1613 +object. So the file object has its entry list until it is closed. The
1614 +entry list will be updated when the file position is zero and becomes
1615 +obsoleted. This decision is made in aufs automatically.
1616 +
1617 +The dynamically allocated memory block for the name of entries has a
1618 +unit of 512 bytes (by default) and stores the names contiguously (no
1619 +padding). Another block for each entry is handled by kmem_cache too.
1620 +During building dir blocks, aufs creates hash list and judging whether
1621 +the entry is whiteouted by its upper branch or already listed.
1622 +The merged result is cached in the corresponding inode object and
1623 +maintained by a customizable life-time option.
1624 +
1625 +Some people may call it can be a security hole or invite DoS attack
1626 +since the opened and once readdir-ed dir (file object) holds its entry
1627 +list and becomes a pressure for system memory. But I'd say it is similar
1628 +to files under /proc or /sys. The virtual files in them also holds a
1629 +memory page (generally) while they are opened. When an idea to reduce
1630 +memory for them is introduced, it will be applied to aufs too.
1631 +For those who really hate this situation, I've developed readdir(3)
1632 +library which operates this merging in userspace. You just need to set
1633 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1634 +kernel space for readdir(3).
1635 +
1636 +
1637 +Workqueue
1638 +----------------------------------------------------------------------
1639 +Aufs sometimes requires privilege access to a branch. For instance,
1640 +in copy-up/down operation. When a user process is going to make changes
1641 +to a file which exists in the lower readonly branch only, and the mode
1642 +of one of ancestor directories may not be writable by a user
1643 +process. Here aufs copy-up the file with its ancestors and they may
1644 +require privilege to set its owner/group/mode/etc.
1645 +This is a typical case of a application character of aufs (see
1646 +Introduction).
1647 +
1648 +Aufs uses workqueue synchronously for this case. It creates its own
1649 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1650 +passes the request to call mkdir or write (for example), and wait for
1651 +its completion. This approach solves a problem of a signal handler
1652 +simply.
1653 +If aufs didn't adopt the workqueue and changed the privilege of the
1654 +process, then the process may receive the unexpected SIGXFSZ or other
1655 +signals.
1656 +
1657 +Also aufs uses the system global workqueue ("events" kernel thread) too
1658 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1659 +whiteout base and etc. This is unrelated to a privilege.
1660 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1661 +superblock at the beginning, at the same time waits for the completion
1662 +of all queued asynchronous tasks.
1663 +
1664 +
1665 +Whiteout
1666 +----------------------------------------------------------------------
1667 +The whiteout in aufs is very similar to Unionfs's. That is represented
1668 +by its filename. UnionMount takes an approach of a file mode, but I am
1669 +afraid several utilities (find(1) or something) will have to support it.
1670 +
1671 +Basically the whiteout represents "logical deletion" which stops aufs to
1672 +lookup further, but also it represents "dir is opaque" which also stop
1673 +further lookup.
1674 +
1675 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1676 +In order to make several functions in a single systemcall to be
1677 +revertible, aufs adopts an approach to rename a directory to a temporary
1678 +unique whiteouted name.
1679 +For example, in rename(2) dir where the target dir already existed, aufs
1680 +renames the target dir to a temporary unique whiteouted name before the
1681 +actual rename on a branch, and then handles other actions (make it opaque,
1682 +update the attributes, etc). If an error happens in these actions, aufs
1683 +simply renames the whiteouted name back and returns an error. If all are
1684 +succeeded, aufs registers a function to remove the whiteouted unique
1685 +temporary name completely and asynchronously to the system global
1686 +workqueue.
1687 +
1688 +
1689 +Copy-up
1690 +----------------------------------------------------------------------
1691 +It is a well-known feature or concept.
1692 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1693 +internally and makes change to the new file on the upper writable branch.
1694 +When the trigger systemcall does not update the timestamps of the parent
1695 +dir, aufs reverts it after copy-up.
1696 +
1697 +
1698 +Move-down (aufs3.9 and later)
1699 +----------------------------------------------------------------------
1700 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1701 +the lower readonly branch to the upper writable branch when a user
1702 +changes something about the file.
1703 +"Move-down" is an opposite action of copy-up. Basically this action is
1704 +ran manually instead of automatically and internally.
1705 +For desgin and implementation, aufs has to consider these issues.
1706 +- whiteout for the file may exist on the lower branch.
1707 +- ancestor directories may not exist on the lower branch.
1708 +- diropq for the ancestor directories may exist on the upper branch.
1709 +- free space on the lower branch will reduce.
1710 +- another access to the file may happen during moving-down, including
1711 +  UDBA (see "Revalidate Dentry and UDBA").
1712 +- the file should not be hard-linked nor pseudo-linked. they should be
1713 +  handled by auplink utility later.
1714 +
1715 +Sometimes users want to move-down a file from the upper writable branch
1716 +to the lower readonly or writable branch. For instance,
1717 +- the free space of the upper writable branch is going to run out.
1718 +- create a new intermediate branch between the upper and lower branch.
1719 +- etc.
1720 +
1721 +For this purpose, use "aumvdown" command in aufs-util.git.
1722 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1723 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1724 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2018-04-15 08:49:13.394483860 +0200
1725 @@ -0,0 +1,85 @@
1726 +
1727 +# Copyright (C) 2015-2018 Junjiro R. Okajima
1728 +# 
1729 +# This program is free software; you can redistribute it and/or modify
1730 +# it under the terms of the GNU General Public License as published by
1731 +# the Free Software Foundation; either version 2 of the License, or
1732 +# (at your option) any later version.
1733 +# 
1734 +# This program is distributed in the hope that it will be useful,
1735 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1736 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1737 +# GNU General Public License for more details.
1738 +# 
1739 +# You should have received a copy of the GNU General Public License
1740 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1741 +
1742 +Support for a branch who has its ->atomic_open()
1743 +----------------------------------------------------------------------
1744 +The filesystems who implement its ->atomic_open() are not majority. For
1745 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1746 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1747 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1748 +sure whether all filesystems who have ->atomic_open() behave like this,
1749 +but NFSv4 surely returns the error.
1750 +
1751 +In order to support ->atomic_open() for aufs, there are a few
1752 +approaches.
1753 +
1754 +A. Introduce aufs_atomic_open()
1755 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1756 +     branch fs.
1757 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1758 +   an aufs user Pip Cet's approach
1759 +   - calls aufs_create(), VFS finish_open() and notify_change().
1760 +   - pass fake-mode to finish_open(), and then correct the mode by
1761 +     notify_change().
1762 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1763 +   - no aufs_atomic_open().
1764 +   - aufs_lookup() registers the TID to an aufs internal object.
1765 +   - aufs_create() does nothing when the matching TID is registered, but
1766 +     registers the mode.
1767 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1768 +     TID is registered.
1769 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1770 +   credential
1771 +   - no aufs_atomic_open().
1772 +   - aufs_create() registers the TID to an internal object. this info
1773 +     represents "this process created this file just now."
1774 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1775 +     registered TID and re-try open() with superuser's credential.
1776 +
1777 +Pros and cons for each approach.
1778 +
1779 +A.
1780 +   - straightforward but highly depends upon VFS internal.
1781 +   - the atomic behavaiour is kept.
1782 +   - some of parameters such as nameidata are hard to reproduce for
1783 +     branch fs.
1784 +   - large overhead.
1785 +B.
1786 +   - easy to implement.
1787 +   - the atomic behavaiour is lost.
1788 +C.
1789 +   - the atomic behavaiour is kept.
1790 +   - dirty and tricky.
1791 +   - VFS checks whether the file is created correctly after calling
1792 +     ->create(), which means this approach doesn't work.
1793 +D.
1794 +   - easy to implement.
1795 +   - the atomic behavaiour is lost.
1796 +   - to open a file with superuser's credential and give it to a user
1797 +     process is a bad idea, since the file object keeps the credential
1798 +     in it. It may affect LSM or something. This approach doesn't work
1799 +     either.
1800 +
1801 +The approach A is ideal, but it hard to implement. So here is a
1802 +variation of A, which is to be implemented.
1803 +
1804 +A-1. Introduce aufs_atomic_open()
1805 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1806 +       vfs_create() and finish_open().
1807 +     - the demerit is that the several checks after branch fs
1808 +       ->atomic_open() are lost. in the ordinary case, the checks are
1809 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1810 +       be implemented in aufs, but not all I am afraid.
1811 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1812 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1813 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2018-04-15 08:49:13.394483860 +0200
1814 @@ -0,0 +1,113 @@
1815 +
1816 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1817 +# 
1818 +# This program is free software; you can redistribute it and/or modify
1819 +# it under the terms of the GNU General Public License as published by
1820 +# the Free Software Foundation; either version 2 of the License, or
1821 +# (at your option) any later version.
1822 +# 
1823 +# This program is distributed in the hope that it will be useful,
1824 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1825 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1826 +# GNU General Public License for more details.
1827 +# 
1828 +# You should have received a copy of the GNU General Public License
1829 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1830 +
1831 +Lookup in a Branch
1832 +----------------------------------------------------------------------
1833 +Since aufs has a character of sub-VFS (see Introduction), it operates
1834 +lookup for branches as VFS does. It may be a heavy work. But almost all
1835 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1836 +directly connected to its parent. Digging down the directory hierarchy
1837 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1838 +aufs calls it.
1839 +
1840 +When a branch is a remote filesystem, aufs basically relies upon its
1841 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1842 +them.
1843 +For d_revalidate, aufs implements three levels of revalidate tests. See
1844 +"Revalidate Dentry and UDBA" in detail.
1845 +
1846 +
1847 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1848 +----------------------------------------------------------------------
1849 +Let's try case study.
1850 +- aufs has two branches, upper readwrite and lower readonly.
1851 +  /au = /rw + /ro
1852 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1853 +- user invoked "chmod a+rx /au/dirA"
1854 +- the internal copy-up is activated and "/rw/dirA" is created and its
1855 +  permission bits are set to world readable.
1856 +- then "/au/dirA" becomes world readable?
1857 +
1858 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1859 +or it may be a natively readonly filesystem. If aufs respects the lower
1860 +branch, it should not respond readdir request from other users. But user
1861 +allowed it by chmod. Should really aufs rejects showing the entries
1862 +under /ro/dirA?
1863 +
1864 +To be honest, I don't have a good solution for this case. So aufs
1865 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1866 +users.
1867 +When dirperm1 is specified, aufs checks only the highest one for the
1868 +directory permission, and shows the entries. Otherwise, as usual, checks
1869 +every dir existing on all branches and rejects the request.
1870 +
1871 +As a side effect, dirperm1 option improves the performance of aufs
1872 +because the number of permission check is reduced when the number of
1873 +branch is many.
1874 +
1875 +
1876 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1877 +----------------------------------------------------------------------
1878 +Generally VFS helpers re-validate a dentry as a part of lookup.
1879 +0. digging down the directory hierarchy.
1880 +1. lock the parent dir by its i_mutex.
1881 +2. lookup the final (child) entry.
1882 +3. revalidate it.
1883 +4. call the actual operation (create, unlink, etc.)
1884 +5. unlock the parent dir
1885 +
1886 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1887 +called. Actually aufs implements it and checks the dentry on a branch is
1888 +still valid.
1889 +But it is not enough. Because aufs has to release the lock for the
1890 +parent dir on a branch at the end of ->lookup() (step 2) and
1891 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1892 +held by VFS.
1893 +If the file on a branch is changed directly, eg. bypassing aufs, after
1894 +aufs released the lock, then the subsequent operation may cause
1895 +something unpleasant result.
1896 +
1897 +This situation is a result of VFS architecture, ->lookup() and
1898 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1899 +design from VFS's point of view. It is just not suitable for sub-VFS
1900 +character in aufs.
1901 +
1902 +Aufs supports such case by three level of revalidation which is
1903 +selectable by user.
1904 +1. Simple Revalidate
1905 +   Addition to the native flow in VFS's, confirm the child-parent
1906 +   relationship on the branch just after locking the parent dir on the
1907 +   branch in the "actual operation" (step 4). When this validation
1908 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1909 +   checks the validation of the dentry on branches.
1910 +2. Monitor Changes Internally by Inotify/Fsnotify
1911 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1912 +   the dentry on the branch, and returns EBUSY if it finds different
1913 +   dentry.
1914 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1915 +   during it is in cache. When the event is notified, aufs registers a
1916 +   function to kernel 'events' thread by schedule_work(). And the
1917 +   function sets some special status to the cached aufs dentry and inode
1918 +   private data. If they are not cached, then aufs has nothing to
1919 +   do. When the same file is accessed through aufs (step 0-3) later,
1920 +   aufs will detect the status and refresh all necessary data.
1921 +   In this mode, aufs has to ignore the event which is fired by aufs
1922 +   itself.
1923 +3. No Extra Validation
1924 +   This is the simplest test and doesn't add any additional revalidation
1925 +   test, and skip the revalidation in step 4. It is useful and improves
1926 +   aufs performance when system surely hide the aufs branches from user,
1927 +   by over-mounting something (or another method).
1928 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1929 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1930 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2018-04-15 08:49:13.394483860 +0200
1931 @@ -0,0 +1,74 @@
1932 +
1933 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1934 +# 
1935 +# This program is free software; you can redistribute it and/or modify
1936 +# it under the terms of the GNU General Public License as published by
1937 +# the Free Software Foundation; either version 2 of the License, or
1938 +# (at your option) any later version.
1939 +# 
1940 +# This program is distributed in the hope that it will be useful,
1941 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1942 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1943 +# GNU General Public License for more details.
1944 +# 
1945 +# You should have received a copy of the GNU General Public License
1946 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1947 +
1948 +Branch Manipulation
1949 +
1950 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1951 +and changing its permission/attribute, there are a lot of works to do.
1952 +
1953 +
1954 +Add a Branch
1955 +----------------------------------------------------------------------
1956 +o Confirm the adding dir exists outside of aufs, including loopback
1957 +  mount, and its various attributes.
1958 +o Initialize the xino file and whiteout bases if necessary.
1959 +  See struct.txt.
1960 +
1961 +o Check the owner/group/mode of the directory
1962 +  When the owner/group/mode of the adding directory differs from the
1963 +  existing branch, aufs issues a warning because it may impose a
1964 +  security risk.
1965 +  For example, when a upper writable branch has a world writable empty
1966 +  top directory, a malicious user can create any files on the writable
1967 +  branch directly, like copy-up and modify manually. If something like
1968 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1969 +  writable branch, and the writable branch is world-writable, then a
1970 +  malicious guy may create /etc/passwd on the writable branch directly
1971 +  and the infected file will be valid in aufs.
1972 +  I am afraid it can be a security issue, but aufs can do nothing except
1973 +  producing a warning.
1974 +
1975 +
1976 +Delete a Branch
1977 +----------------------------------------------------------------------
1978 +o Confirm the deleting branch is not busy
1979 +  To be general, there is one merit to adopt "remount" interface to
1980 +  manipulate branches. It is to discard caches. At deleting a branch,
1981 +  aufs checks the still cached (and connected) dentries and inodes. If
1982 +  there are any, then they are all in-use. An inode without its
1983 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1984 +
1985 +  For the cached one, aufs checks whether the same named entry exists on
1986 +  other branches.
1987 +  If the cached one is a directory, because aufs provides a merged view
1988 +  to users, as long as one dir is left on any branch aufs can show the
1989 +  dir to users. In this case, the branch can be removed from aufs.
1990 +  Otherwise aufs rejects deleting the branch.
1991 +
1992 +  If any file on the deleting branch is opened by aufs, then aufs
1993 +  rejects deleting.
1994 +
1995 +
1996 +Modify the Permission of a Branch
1997 +----------------------------------------------------------------------
1998 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1999 +  See struct.txt.
2000 +
2001 +o rw --> ro: Confirm the modifying branch is not busy
2002 +  Aufs rejects the request if any of these conditions are true.
2003 +  - a file on the branch is mmap-ed.
2004 +  - a regular file on the branch is opened for write and there is no
2005 +    same named entry on the upper branch.
2006 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
2007 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
2008 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2018-04-15 08:49:13.394483860 +0200
2009 @@ -0,0 +1,64 @@
2010 +
2011 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2012 +# 
2013 +# This program is free software; you can redistribute it and/or modify
2014 +# it under the terms of the GNU General Public License as published by
2015 +# the Free Software Foundation; either version 2 of the License, or
2016 +# (at your option) any later version.
2017 +# 
2018 +# This program is distributed in the hope that it will be useful,
2019 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2020 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2021 +# GNU General Public License for more details.
2022 +# 
2023 +# You should have received a copy of the GNU General Public License
2024 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2025 +
2026 +Policies to Select One among Multiple Writable Branches
2027 +----------------------------------------------------------------------
2028 +When the number of writable branch is more than one, aufs has to decide
2029 +the target branch for file creation or copy-up. By default, the highest
2030 +writable branch which has the parent (or ancestor) dir of the target
2031 +file is chosen (top-down-parent policy).
2032 +By user's request, aufs implements some other policies to select the
2033 +writable branch, for file creation several policies, round-robin,
2034 +most-free-space, and other policies. For copy-up, top-down-parent,
2035 +bottom-up-parent, bottom-up and others.
2036 +
2037 +As expected, the round-robin policy selects the branch in circular. When
2038 +you have two writable branches and creates 10 new files, 5 files will be
2039 +created for each branch. mkdir(2) systemcall is an exception. When you
2040 +create 10 new directories, all will be created on the same branch.
2041 +And the most-free-space policy selects the one which has most free
2042 +space among the writable branches. The amount of free space will be
2043 +checked by aufs internally, and users can specify its time interval.
2044 +
2045 +The policies for copy-up is more simple,
2046 +top-down-parent is equivalent to the same named on in create policy,
2047 +bottom-up-parent selects the writable branch where the parent dir
2048 +exists and the nearest upper one from the copyup-source,
2049 +bottom-up selects the nearest upper writable branch from the
2050 +copyup-source, regardless the existence of the parent dir.
2051 +
2052 +There are some rules or exceptions to apply these policies.
2053 +- If there is a readonly branch above the policy-selected branch and
2054 +  the parent dir is marked as opaque (a variation of whiteout), or the
2055 +  target (creating) file is whiteout-ed on the upper readonly branch,
2056 +  then the result of the policy is ignored and the target file will be
2057 +  created on the nearest upper writable branch than the readonly branch.
2058 +- If there is a writable branch above the policy-selected branch and
2059 +  the parent dir is marked as opaque or the target file is whiteouted
2060 +  on the branch, then the result of the policy is ignored and the target
2061 +  file will be created on the highest one among the upper writable
2062 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
2063 +  it as usual.
2064 +- link(2) and rename(2) systemcalls are exceptions in every policy.
2065 +  They try selecting the branch where the source exists as possible
2066 +  since copyup a large file will take long time. If it can't be,
2067 +  ie. the branch where the source exists is readonly, then they will
2068 +  follow the copyup policy.
2069 +- There is an exception for rename(2) when the target exists.
2070 +  If the rename target exists, aufs compares the index of the branches
2071 +  where the source and the target exists and selects the higher
2072 +  one. If the selected branch is readonly, then aufs follows the
2073 +  copyup policy.
2074 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2075 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
2076 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2018-04-15 08:49:13.394483860 +0200
2077 @@ -0,0 +1,31 @@
2078 +
2079 +// to view this graph, run dot(1) command in GRAPHVIZ.
2080 +
2081 +digraph G {
2082 +node [shape=box];
2083 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2084 +
2085 +node [shape=oval];
2086 +
2087 +aufs_rename -> whinfo [label="store/remove"];
2088 +
2089 +node [shape=oval];
2090 +inode_list [label="h_inum list in branch\ncache"];
2091 +
2092 +node [shape=box];
2093 +whinode [label="h_inum list file"];
2094 +
2095 +node [shape=oval];
2096 +brmgmt [label="br_add/del/mod/umount"];
2097 +
2098 +brmgmt -> inode_list [label="create/remove"];
2099 +brmgmt -> whinode [label="load/store"];
2100 +
2101 +inode_list -> whinode [style=dashed,dir=both];
2102 +
2103 +aufs_rename -> inode_list [label="add/del"];
2104 +
2105 +aufs_lookup -> inode_list [label="search"];
2106 +
2107 +aufs_lookup -> whinfo [label="load/remove"];
2108 +}
2109 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2110 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
2111 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2018-04-15 08:49:13.394483860 +0200
2112 @@ -0,0 +1,102 @@
2113 +
2114 +# Copyright (C) 2017-2018 Junjiro R. Okajima
2115 +#
2116 +# This program is free software; you can redistribute it and/or modify
2117 +# it under the terms of the GNU General Public License as published by
2118 +# the Free Software Foundation; either version 2 of the License, or
2119 +# (at your option) any later version.
2120 +#
2121 +# This program is distributed in the hope that it will be useful,
2122 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2123 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2124 +# GNU General Public License for more details.
2125 +#
2126 +# You should have received a copy of the GNU General Public License
2127 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2128 +
2129 +Special handling for renaming a directory (DIRREN)
2130 +----------------------------------------------------------------------
2131 +First, let's assume we have a simple usecase.
2132 +
2133 +- /u = /rw + /ro
2134 +- /rw/dirA exists
2135 +- /ro/dirA and /ro/dirA/file exist too
2136 +- there is no dirB on both branches
2137 +- a user issues rename("dirA", "dirB")
2138 +
2139 +Now, what should aufs behave against this rename(2)?
2140 +There are a few possible cases.
2141 +
2142 +A. returns EROFS.
2143 +   since dirA exists on a readonly branch which cannot be renamed.
2144 +B. returns EXDEV.
2145 +   it is possible to copy-up dirA (only the dir itself), but the child
2146 +   entries ("file" in this case) should not be. it must be a bad
2147 +   approach to copy-up recursively.
2148 +C. returns a success.
2149 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2150 +   is a violation of aufs' policy.
2151 +D. construct an extra information which indicates that /ro/dirA should
2152 +   be handled as the name of dirB.
2153 +   overlayfs has a similar feature called REDIRECT.
2154 +
2155 +Until now, aufs implements the case B only which returns EXDEV, and
2156 +expects the userspace application behaves like mv(1) which tries
2157 +issueing rename(2) recursively.
2158 +
2159 +A new aufs feature called DIRREN is introduced which implements the case
2160 +D. There are several "extra information" added.
2161 +
2162 +1. detailed info per renamed directory
2163 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2164 +2. the inode-number list of directories on a branch
2165 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2166 +
2167 +The filename of "detailed info per directory" represents the lower
2168 +branch, and its format is
2169 +- a type of the branch id
2170 +  one of these.
2171 +  + uuid (not implemented yet)
2172 +  + fsid
2173 +  + dev
2174 +- the inode-number of the branch root dir
2175 +
2176 +And it contains these info in a single regular file.
2177 +- magic number
2178 +- branch's inode-number of the logically renamed dir
2179 +- the name of the before-renamed dir
2180 +
2181 +The "detailed info per directory" file is created in aufs rename(2), and
2182 +loaded in any lookup.
2183 +The info is considered in lookup for the matching case only. Here
2184 +"matching" means that the root of branch (in the info filename) is same
2185 +to the current looking-up branch. After looking-up the before-renamed
2186 +name, the inode-number is compared. And the matched dentry is used.
2187 +
2188 +The "inode-number list of directories" is a regular file which contains
2189 +simply the inode-numbers on the branch. The file is created or updated
2190 +in removing the branch, and loaded in adding the branch. Its lifetime is
2191 +equal to the branch.
2192 +The list is refered in lookup, and when the current target inode is
2193 +found in the list, the aufs tries loading the "detailed info per
2194 +directory" and get the changed and valid name of the dir.
2195 +
2196 +Theoretically these "extra informaiton" may be able to be put into XATTR
2197 +in the dir inode. But aufs doesn't choose this way because
2198 +1. XATTR may not be supported by the branch (or its configuration)
2199 +2. XATTR may have its size limit.
2200 +3. XATTR may be less easy to convert than a regular file, when the
2201 +   format of the info is changed in the future.
2202 +At the same time, I agree that the regular file approach is much slower
2203 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2204 +better approach.
2205 +
2206 +This DIRREN feature is enabled by aufs configuration, and is activated
2207 +by a new mount option.
2208 +
2209 +For the more complicated case, there is a work with UDBA option, which
2210 +is to dected the direct access to the branches (by-passing aufs) and to
2211 +maintain the cashes in aufs. Since a single cached aufs dentry may
2212 +contains two names, before- and after-rename, the name comparision in
2213 +UDBA handler may not work correctly. In this case, the behaviour will be
2214 +equivalen to udba=reval case.
2215 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2216 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2217 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2018-04-15 08:49:13.394483860 +0200
2218 @@ -0,0 +1,120 @@
2219 +
2220 +# Copyright (C) 2011-2018 Junjiro R. Okajima
2221 +# 
2222 +# This program is free software; you can redistribute it and/or modify
2223 +# it under the terms of the GNU General Public License as published by
2224 +# the Free Software Foundation; either version 2 of the License, or
2225 +# (at your option) any later version.
2226 +# 
2227 +# This program is distributed in the hope that it will be useful,
2228 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2229 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2230 +# GNU General Public License for more details.
2231 +# 
2232 +# You should have received a copy of the GNU General Public License
2233 +# along with this program; if not, write to the Free Software
2234 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2235 +
2236 +
2237 +File-based Hierarchical Storage Management (FHSM)
2238 +----------------------------------------------------------------------
2239 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2240 +storage world. Aufs provides this feature as file-based with multiple
2241 +writable branches, based upon the principle of "Colder, the Lower".
2242 +Here the word "colder" means that the less used files, and "lower" means
2243 +that the position in the order of the stacked branches vertically.
2244 +These multiple writable branches are prioritized, ie. the topmost one
2245 +should be the fastest drive and be used heavily.
2246 +
2247 +o Characters in aufs FHSM story
2248 +- aufs itself and a new branch attribute.
2249 +- a new ioctl interface to move-down and to establish a connection with
2250 +  the daemon ("move-down" is a converse of "copy-up").
2251 +- userspace tool and daemon.
2252 +
2253 +The userspace daemon establishes a connection with aufs and waits for
2254 +the notification. The notified information is very similar to struct
2255 +statfs containing the number of consumed blocks and inodes.
2256 +When the consumed blocks/inodes of a branch exceeds the user-specified
2257 +upper watermark, the daemon activates its move-down process until the
2258 +consumed blocks/inodes reaches the user-specified lower watermark.
2259 +
2260 +The actual move-down is done by aufs based upon the request from
2261 +user-space since we need to maintain the inode number and the internal
2262 +pointer arrays in aufs.
2263 +
2264 +Currently aufs FHSM handles the regular files only. Additionally they
2265 +must not be hard-linked nor pseudo-linked.
2266 +
2267 +
2268 +o Cowork of aufs and the user-space daemon
2269 +  During the userspace daemon established the connection, aufs sends a
2270 +  small notification to it whenever aufs writes something into the
2271 +  writable branch. But it may cost high since aufs issues statfs(2)
2272 +  internally. So user can specify a new option to cache the
2273 +  info. Actually the notification is controlled by these factors.
2274 +  + the specified cache time.
2275 +  + classified as "force" by aufs internally.
2276 +  Until the specified time expires, aufs doesn't send the info
2277 +  except the forced cases. When aufs decide forcing, the info is always
2278 +  notified to userspace.
2279 +  For example, the number of free inodes is generally large enough and
2280 +  the shortage of it happens rarely. So aufs doesn't force the
2281 +  notification when creating a new file, directory and others. This is
2282 +  the typical case which aufs doesn't force.
2283 +  When aufs writes the actual filedata and the files consumes any of new
2284 +  blocks, the aufs forces notifying.
2285 +
2286 +
2287 +o Interfaces in aufs
2288 +- New branch attribute.
2289 +  + fhsm
2290 +    Specifies that the branch is managed by FHSM feature. In other word,
2291 +    participant in the FHSM.
2292 +    When nofhsm is set to the branch, it will not be the source/target
2293 +    branch of the move-down operation. This attribute is set
2294 +    independently from coo and moo attributes, and if you want full
2295 +    FHSM, you should specify them as well.
2296 +- New mount option.
2297 +  + fhsm_sec
2298 +    Specifies a second to suppress many less important info to be
2299 +    notified.
2300 +- New ioctl.
2301 +  + AUFS_CTL_FHSM_FD
2302 +    create a new file descriptor which userspace can read the notification
2303 +    (a subset of struct statfs) from aufs.
2304 +- Module parameter 'brs'
2305 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2306 +  be set.
2307 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2308 +  When there are two or more branches with fhsm attributes,
2309 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2310 +  terminates it. As a result of remounting and branch-manipulation, the
2311 +  number of branches with fhsm attribute can be one. In this case,
2312 +  /sbin/mount.aufs will terminate the user-space daemon.
2313 +
2314 +
2315 +Finally the operation is done as these steps in kernel-space.
2316 +- make sure that,
2317 +  + no one else is using the file.
2318 +  + the file is not hard-linked.
2319 +  + the file is not pseudo-linked.
2320 +  + the file is a regular file.
2321 +  + the parent dir is not opaqued.
2322 +- find the target writable branch.
2323 +- make sure the file is not whiteout-ed by the upper (than the target)
2324 +  branch.
2325 +- make the parent dir on the target branch.
2326 +- mutex lock the inode on the branch.
2327 +- unlink the whiteout on the target branch (if exists).
2328 +- lookup and create the whiteout-ed temporary name on the target branch.
2329 +- copy the file as the whiteout-ed temporary name on the target branch.
2330 +- rename the whiteout-ed temporary name to the original name.
2331 +- unlink the file on the source branch.
2332 +- maintain the internal pointer array and the external inode number
2333 +  table (XINO).
2334 +- maintain the timestamps and other attributes of the parent dir and the
2335 +  file.
2336 +
2337 +And of course, in every step, an error may happen. So the operation
2338 +should restore the original file state after an error happens.
2339 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2340 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2341 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2018-04-15 08:49:13.394483860 +0200
2342 @@ -0,0 +1,72 @@
2343 +
2344 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2345 +# 
2346 +# This program is free software; you can redistribute it and/or modify
2347 +# it under the terms of the GNU General Public License as published by
2348 +# the Free Software Foundation; either version 2 of the License, or
2349 +# (at your option) any later version.
2350 +# 
2351 +# This program is distributed in the hope that it will be useful,
2352 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2353 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2354 +# GNU General Public License for more details.
2355 +# 
2356 +# You should have received a copy of the GNU General Public License
2357 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2358 +
2359 +mmap(2) -- File Memory Mapping
2360 +----------------------------------------------------------------------
2361 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2362 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2363 +->mmap().
2364 +This approach is simple and good, but there is one problem.
2365 +Under /proc, several entries show the mmapped files by its path (with
2366 +device and inode number), and the printed path will be the path on the
2367 +branch fs's instead of virtual aufs's.
2368 +This is not a problem in most cases, but some utilities lsof(1) (and its
2369 +user) may expect the path on aufs.
2370 +
2371 +To address this issue, aufs adds a new member called vm_prfile in struct
2372 +vm_area_struct (and struct vm_region). The original vm_file points to
2373 +the file on the branch fs in order to handle everything correctly as
2374 +usual. The new vm_prfile points to a virtual file in aufs, and the
2375 +show-functions in procfs refers to vm_prfile if it is set.
2376 +Also we need to maintain several other places where touching vm_file
2377 +such like
2378 +- fork()/clone() copies vma and the reference count of vm_file is
2379 +  incremented.
2380 +- merging vma maintains the ref count too.
2381 +
2382 +This is not a good approach. It just fakes the printed path. But it
2383 +leaves all behaviour around f_mapping unchanged. This is surely an
2384 +advantage.
2385 +Actually aufs had adopted another complicated approach which calls
2386 +generic_file_mmap() and handles struct vm_operations_struct. In this
2387 +approach, aufs met a hard problem and I could not solve it without
2388 +switching the approach.
2389 +
2390 +There may be one more another approach which is
2391 +- bind-mount the branch-root onto the aufs-root internally
2392 +- grab the new vfsmount (ie. struct mount)
2393 +- lazy-umount the branch-root internally
2394 +- in open(2) the aufs-file, open the branch-file with the hidden
2395 +  vfsmount (instead of the original branch's vfsmount)
2396 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2397 +  but it may be possible from userspace by the mount helper.
2398 +
2399 +Adding the internal hidden vfsmount and using it in opening a file, the
2400 +file path under /proc will be printed correctly. This approach looks
2401 +smarter, but is not possible I am afraid.
2402 +- aufs-root may be bind-mount later. when it happens, another hidden
2403 +  vfsmount will be required.
2404 +- it is hard to get the chance to bind-mount and lazy-umount
2405 +  + in kernel-space, FS can have vfsmount in open(2) via
2406 +    file->f_path, and aufs can know its vfsmount. But several locks are
2407 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2408 +    here, then it may cause a deadlock.
2409 +  + in user-space, bind-mount doesn't invoke the mount helper.
2410 +- since /proc shows dev and ino, aufs has to give vma these info. it
2411 +  means a new member vm_prinode will be necessary. this is essentially
2412 +  equivalent to vm_prfile described above.
2413 +
2414 +I have to give up this "looks-smater" approach.
2415 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2416 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2417 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2018-04-15 08:49:13.394483860 +0200
2418 @@ -0,0 +1,96 @@
2419 +
2420 +# Copyright (C) 2014-2018 Junjiro R. Okajima
2421 +#
2422 +# This program is free software; you can redistribute it and/or modify
2423 +# it under the terms of the GNU General Public License as published by
2424 +# the Free Software Foundation; either version 2 of the License, or
2425 +# (at your option) any later version.
2426 +#
2427 +# This program is distributed in the hope that it will be useful,
2428 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2430 +# GNU General Public License for more details.
2431 +#
2432 +# You should have received a copy of the GNU General Public License
2433 +# along with this program; if not, write to the Free Software
2434 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2435 +
2436 +
2437 +Listing XATTR/EA and getting the value
2438 +----------------------------------------------------------------------
2439 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2440 +shows the values from the topmost existing file. This behaviour is good
2441 +for the non-dir entries since the bahaviour exactly matches the shown
2442 +information. But for the directories, aufs considers all the same named
2443 +entries on the lower branches. Which means, if one of the lower entry
2444 +rejects readdir call, then aufs returns an error even if the topmost
2445 +entry allows it. This behaviour is necessary to respect the branch fs's
2446 +security, but can make users confused since the user-visible standard
2447 +attributes don't match the behaviour.
2448 +To address this issue, aufs has a mount option called dirperm1 which
2449 +checks the permission for the topmost entry only, and ignores the lower
2450 +entry's permission.
2451 +
2452 +A similar issue can happen around XATTR.
2453 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2454 +always set. Otherwise these very unpleasant situation would happen.
2455 +- listxattr(2) may return the duplicated entries.
2456 +- users may not be able to remove or reset the XATTR forever,
2457 +
2458 +
2459 +XATTR/EA support in the internal (copy,move)-(up,down)
2460 +----------------------------------------------------------------------
2461 +Generally the extended attributes of inode are categorized as these.
2462 +- "security" for LSM and capability.
2463 +- "system" for posix ACL, 'acl' mount option is required for the branch
2464 +  fs generally.
2465 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2466 +- "user" for userspace, 'user_xattr' mount option is required for the
2467 +  branch fs generally.
2468 +
2469 +Moreover there are some other categories. Aufs handles these rather
2470 +unpopular categories as the ordinary ones, ie. there is no special
2471 +condition nor exception.
2472 +
2473 +In copy-up, the support for XATTR on the dst branch may differ from the
2474 +src branch. In this case, the copy-up operation will get an error and
2475 +the original user operation which triggered the copy-up will fail. It
2476 +can happen that even all copy-up will fail.
2477 +When both of src and dst branches support XATTR and if an error occurs
2478 +during copying XATTR, then the copy-up should fail obviously. That is a
2479 +good reason and aufs should return an error to userspace. But when only
2480 +the src branch support that XATTR, aufs should not return an error.
2481 +For example, the src branch supports ACL but the dst branch doesn't
2482 +because the dst branch may natively un-support it or temporary
2483 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2484 +may NOT return an error even if the XATTR is not supported. It is
2485 +totally up to the branch fs.
2486 +
2487 +Anyway when the aufs internal copy-up gets an error from the dst branch
2488 +fs, then aufs tries removing the just copied entry and returns the error
2489 +to the userspace. The worst case of this situation will be all copy-up
2490 +will fail.
2491 +
2492 +For the copy-up operation, there two basic approaches.
2493 +- copy the specified XATTR only (by category above), and return the
2494 +  error unconditionally if it happens.
2495 +- copy all XATTR, and ignore the error on the specified category only.
2496 +
2497 +In order to support XATTR and to implement the correct behaviour, aufs
2498 +chooses the latter approach and introduces some new branch attributes,
2499 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2500 +They correspond to the XATTR namespaces (see above). Additionally, to be
2501 +convenient, "icex" is also provided which means all "icex*" attributes
2502 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2503 +
2504 +The meaning of these attributes is to ignore the error from setting
2505 +XATTR on that branch.
2506 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2507 +error from the dst branch according to the specified attributes.
2508 +
2509 +Some XATTR may have its default value. The default value may come from
2510 +the parent dir or the environment. If the default value is set at the
2511 +file creating-time, it will be overwritten by copy-up.
2512 +Some contradiction may happen I am afraid.
2513 +Do we need another attribute to stop copying XATTR? I am unsure. For
2514 +now, aufs implements the branch attributes to ignore the error.
2515 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2516 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2517 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2018-04-15 08:49:13.394483860 +0200
2518 @@ -0,0 +1,58 @@
2519 +
2520 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2521 +# 
2522 +# This program is free software; you can redistribute it and/or modify
2523 +# it under the terms of the GNU General Public License as published by
2524 +# the Free Software Foundation; either version 2 of the License, or
2525 +# (at your option) any later version.
2526 +# 
2527 +# This program is distributed in the hope that it will be useful,
2528 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2529 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2530 +# GNU General Public License for more details.
2531 +# 
2532 +# You should have received a copy of the GNU General Public License
2533 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2534 +
2535 +Export Aufs via NFS
2536 +----------------------------------------------------------------------
2537 +Here is an approach.
2538 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2539 +  generation.
2540 +- iget_locked(): initialize aufs inode generation for a new inode, and
2541 +  store it in xigen file.
2542 +- destroy_inode(): increment aufs inode generation and store it in xigen
2543 +  file. it is necessary even if it is not unlinked, because any data of
2544 +  inode may be changed by UDBA.
2545 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2546 +  build file handle by
2547 +  + branch id (4 bytes)
2548 +  + superblock generation (4 bytes)
2549 +  + inode number (4 or 8 bytes)
2550 +  + parent dir inode number (4 or 8 bytes)
2551 +  + inode generation (4 bytes))
2552 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2553 +    bytes)
2554 +  + file handle for a branch (by exportfs_encode_fh())
2555 +- fh_to_dentry():
2556 +  + find the index of a branch from its id in handle, and check it is
2557 +    still exist in aufs.
2558 +  + 1st level: get the inode number from handle and search it in cache.
2559 +  + 2nd level: if not found in cache, get the parent inode number from
2560 +    the handle and search it in cache. and then open the found parent
2561 +    dir, find the matching inode number by vfs_readdir() and get its
2562 +    name, and call lookup_one_len() for the target dentry.
2563 +  + 3rd level: if the parent dir is not cached, call
2564 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2565 +    build a pathname of it, convert it a pathname in aufs, call
2566 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2567 +    the 2nd level.
2568 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2569 +    for every branch, but not itself. to get this, (currently) aufs
2570 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2571 +    idea, but I didn't get other approach.
2572 +  + test the generation of the gotten inode.
2573 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2574 +  convert it into ESTALE for NFSD.
2575 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2576 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2577 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2578 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2579 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2018-04-15 08:49:13.394483860 +0200
2580 @@ -0,0 +1,52 @@
2581 +
2582 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2583 +# 
2584 +# This program is free software; you can redistribute it and/or modify
2585 +# it under the terms of the GNU General Public License as published by
2586 +# the Free Software Foundation; either version 2 of the License, or
2587 +# (at your option) any later version.
2588 +# 
2589 +# This program is distributed in the hope that it will be useful,
2590 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2591 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2592 +# GNU General Public License for more details.
2593 +# 
2594 +# You should have received a copy of the GNU General Public License
2595 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2596 +
2597 +Show Whiteout Mode (shwh)
2598 +----------------------------------------------------------------------
2599 +Generally aufs hides the name of whiteouts. But in some cases, to show
2600 +them is very useful for users. For instance, creating a new middle layer
2601 +(branch) by merging existing layers.
2602 +
2603 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2604 +When you have three branches,
2605 +- Bottom: 'system', squashfs (underlying base system), read-only
2606 +- Middle: 'mods', squashfs, read-only
2607 +- Top: 'overlay', ram (tmpfs), read-write
2608 +
2609 +The top layer is loaded at boot time and saved at shutdown, to preserve
2610 +the changes made to the system during the session.
2611 +When larger changes have been made, or smaller changes have accumulated,
2612 +the size of the saved top layer data grows. At this point, it would be
2613 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2614 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2615 +restoring save and load speed.
2616 +
2617 +This merging is simplified by the use of another aufs mount, of just the
2618 +two overlay branches using the 'shwh' option.
2619 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2620 +       aufs /livesys/merge_union
2621 +
2622 +A merged view of these two branches is then available at
2623 +/livesys/merge_union, and the new feature is that the whiteouts are
2624 +visible!
2625 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2626 +writing to all branches. Also the default mode for all branches is 'ro'.
2627 +It is now possible to save the combined contents of the two overlay
2628 +branches to a new squashfs, e.g.:
2629 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2630 +
2631 +This new squashfs archive can be stored on the boot device and the
2632 +initramfs will use it to replace the old one at the next boot.
2633 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2634 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2635 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2018-04-15 08:49:13.394483860 +0200
2636 @@ -0,0 +1,47 @@
2637 +
2638 +# Copyright (C) 2010-2018 Junjiro R. Okajima
2639 +#
2640 +# This program is free software; you can redistribute it and/or modify
2641 +# it under the terms of the GNU General Public License as published by
2642 +# the Free Software Foundation; either version 2 of the License, or
2643 +# (at your option) any later version.
2644 +#
2645 +# This program is distributed in the hope that it will be useful,
2646 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2647 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2648 +# GNU General Public License for more details.
2649 +#
2650 +# You should have received a copy of the GNU General Public License
2651 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2652 +
2653 +Dynamically customizable FS operations
2654 +----------------------------------------------------------------------
2655 +Generally FS operations (struct inode_operations, struct
2656 +address_space_operations, struct file_operations, etc.) are defined as
2657 +"static const", but it never means that FS have only one set of
2658 +operation. Some FS have multiple sets of them. For instance, ext2 has
2659 +three sets, one for XIP, for NOBH, and for normal.
2660 +Since aufs overrides and redirects these operations, sometimes aufs has
2661 +to change its behaviour according to the branch FS type. More importantly
2662 +VFS acts differently if a function (member in the struct) is set or
2663 +not. It means aufs should have several sets of operations and select one
2664 +among them according to the branch FS definition.
2665 +
2666 +In order to solve this problem and not to affect the behaviour of VFS,
2667 +aufs defines these operations dynamically. For instance, aufs defines
2668 +dummy direct_IO function for struct address_space_operations, but it may
2669 +not be set to the address_space_operations actually. When the branch FS
2670 +doesn't have it, aufs doesn't set it to its address_space_operations
2671 +while the function definition itself is still alive. So the behaviour
2672 +itself will not change, and it will return an error when direct_IO is
2673 +not set.
2674 +
2675 +The lifetime of these dynamically generated operation object is
2676 +maintained by aufs branch object. When the branch is removed from aufs,
2677 +the reference counter of the object is decremented. When it reaches
2678 +zero, the dynamically generated operation object will be freed.
2679 +
2680 +This approach is designed to support AIO (io_submit), Direct I/O and
2681 +XIP (DAX) mainly.
2682 +Currently this approach is applied to address_space_operations for
2683 +regular files only.
2684 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2685 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2686 +++ linux/Documentation/filesystems/aufs/README 2018-06-15 11:15:15.400449109 +0200
2687 @@ -0,0 +1,393 @@
2688 +
2689 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2690 +http://aufs.sf.net
2691 +Junjiro R. Okajima
2692 +
2693 +
2694 +0. Introduction
2695 +----------------------------------------
2696 +In the early days, aufs was entirely re-designed and re-implemented
2697 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2698 +improvements and implementations, it becomes totally different from
2699 +Unionfs while keeping the basic features.
2700 +Recently, Unionfs Version 2.x series begin taking some of the same
2701 +approaches to aufs1's.
2702 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2703 +University and his team.
2704 +
2705 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2706 +If you want older kernel version support, try aufs2-2.6.git or
2707 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2708 +
2709 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2710 +      According to Christoph Hellwig, linux rejects all union-type
2711 +      filesystems but UnionMount.
2712 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2713 +
2714 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2715 +    UnionMount, and he pointed out an issue around a directory mutex
2716 +    lock and aufs addressed it. But it is still unsure whether aufs will
2717 +    be merged (or any other union solution).
2718 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2719 +
2720 +
2721 +1. Features
2722 +----------------------------------------
2723 +- unite several directories into a single virtual filesystem. The member
2724 +  directory is called as a branch.
2725 +- you can specify the permission flags to the branch, which are 'readonly',
2726 +  'readwrite' and 'whiteout-able.'
2727 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2728 +  readonly branch are modifiable logically.
2729 +- dynamic branch manipulation, add, del.
2730 +- etc...
2731 +
2732 +Also there are many enhancements in aufs, such as:
2733 +- test only the highest one for the directory permission (dirperm1)
2734 +- copyup on open (coo=)
2735 +- 'move' policy for copy-up between two writable branches, after
2736 +  checking free space.
2737 +- xattr, acl
2738 +- readdir(3) in userspace.
2739 +- keep inode number by external inode number table
2740 +- keep the timestamps of file/dir in internal copyup operation
2741 +- seekable directory, supporting NFS readdir.
2742 +- whiteout is hardlinked in order to reduce the consumption of inodes
2743 +  on branch
2744 +- do not copyup, nor create a whiteout when it is unnecessary
2745 +- revert a single systemcall when an error occurs in aufs
2746 +- remount interface instead of ioctl
2747 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2748 +- loopback mounted filesystem as a branch
2749 +- kernel thread for removing the dir who has a plenty of whiteouts
2750 +- support copyup sparse file (a file which has a 'hole' in it)
2751 +- default permission flags for branches
2752 +- selectable permission flags for ro branch, whether whiteout can
2753 +  exist or not
2754 +- export via NFS.
2755 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2756 +- support multiple writable branches, some policies to select one
2757 +  among multiple writable branches.
2758 +- a new semantics for link(2) and rename(2) to support multiple
2759 +  writable branches.
2760 +- no glibc changes are required.
2761 +- pseudo hardlink (hardlink over branches)
2762 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2763 +  including NFS or remote filesystem branch.
2764 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2765 +- and more...
2766 +
2767 +Currently these features are dropped temporary from aufs4.
2768 +See design/08plan.txt in detail.
2769 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2770 +  (robr)
2771 +- statistics of aufs thread (/sys/fs/aufs/stat)
2772 +
2773 +Features or just an idea in the future (see also design/*.txt),
2774 +- reorder the branch index without del/re-add.
2775 +- permanent xino files for NFSD
2776 +- an option for refreshing the opened files after add/del branches
2777 +- light version, without branch manipulation. (unnecessary?)
2778 +- copyup in userspace
2779 +- inotify in userspace
2780 +- readv/writev
2781 +
2782 +
2783 +2. Download
2784 +----------------------------------------
2785 +There are three GIT trees for aufs4, aufs4-linux.git,
2786 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2787 +"aufs-util.git."
2788 +While the aufs-util is always necessary, you need either of aufs4-linux
2789 +or aufs4-standalone.
2790 +
2791 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2792 +git://git.kernel.org/.../torvalds/linux.git.
2793 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2794 +build aufs4 as an external kernel module.
2795 +Several extra patches are not included in this tree. Only
2796 +aufs4-standalone tree contains them. They are described in the later
2797 +section "Configuration and Compilation."
2798 +
2799 +On the other hand, the aufs4-standalone tree has only aufs source files
2800 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2801 +But you need to apply all aufs patches manually.
2802 +
2803 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2804 +represents the linux kernel version, "linux-4.x". For instance,
2805 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2806 +"aufs4.x-rcN" branch.
2807 +
2808 +o aufs4-linux tree
2809 +$ git clone --reference /your/linux/git/tree \
2810 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2811 +- if you don't have linux GIT tree, then remove "--reference ..."
2812 +$ cd aufs4-linux.git
2813 +$ git checkout origin/aufs4.0
2814 +
2815 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2816 +leave the patch-work to GIT.
2817 +$ cd /your/linux/git/tree
2818 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2819 +$ git fetch aufs4
2820 +$ git checkout -b my4.0 v4.0
2821 +$ (add your local change...)
2822 +$ git pull aufs4 aufs4.0
2823 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2824 +- you may need to solve some conflicts between your_changes and
2825 +  aufs4.0. in this case, git-rerere is recommended so that you can
2826 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2827 +  later in the future.
2828 +
2829 +o aufs4-standalone tree
2830 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2831 +$ cd aufs4-standalone.git
2832 +$ git checkout origin/aufs4.0
2833 +
2834 +o aufs-util tree
2835 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2836 +- note that the public aufs-util.git is on SourceForge instead of
2837 +  GitHUB.
2838 +$ cd aufs-util.git
2839 +$ git checkout origin/aufs4.0
2840 +
2841 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2842 +The minor version number, 'x' in '4.x', of aufs may not always
2843 +follow the minor version number of the kernel.
2844 +Because changes in the kernel that cause the use of a new
2845 +minor version number do not always require changes to aufs-util.
2846 +
2847 +Since aufs-util has its own minor version number, you may not be
2848 +able to find a GIT branch in aufs-util for your kernel's
2849 +exact minor version number.
2850 +In this case, you should git-checkout the branch for the
2851 +nearest lower number.
2852 +
2853 +For (an unreleased) example:
2854 +If you are using "linux-4.10" and the "aufs4.10" branch
2855 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2856 +or something numerically smaller is the branch for your kernel.
2857 +
2858 +Also you can view all branches by
2859 +       $ git branch -a
2860 +
2861 +
2862 +3. Configuration and Compilation
2863 +----------------------------------------
2864 +Make sure you have git-checkout'ed the correct branch.
2865 +
2866 +For aufs4-linux tree,
2867 +- enable CONFIG_AUFS_FS.
2868 +- set other aufs configurations if necessary.
2869 +
2870 +For aufs4-standalone tree,
2871 +There are several ways to build.
2872 +
2873 +1.
2874 +- apply ./aufs4-kbuild.patch to your kernel source files.
2875 +- apply ./aufs4-base.patch too.
2876 +- apply ./aufs4-mmap.patch too.
2877 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2878 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2879 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2880 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2881 +- enable CONFIG_AUFS_FS, you can select either
2882 +  =m or =y.
2883 +- and build your kernel as usual.
2884 +- install the built kernel.
2885 +  Note: Since linux-3.9, every filesystem module requires an alias
2886 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2887 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2888 +- install the header files too by "make headers_install" to the
2889 +  directory where you specify. By default, it is $PWD/usr.
2890 +  "make help" shows a brief note for headers_install.
2891 +- and reboot your system.
2892 +
2893 +2.
2894 +- module only (CONFIG_AUFS_FS=m).
2895 +- apply ./aufs4-base.patch to your kernel source files.
2896 +- apply ./aufs4-mmap.patch too.
2897 +- apply ./aufs4-standalone.patch too.
2898 +- build your kernel, don't forget "make headers_install", and reboot.
2899 +- edit ./config.mk and set other aufs configurations if necessary.
2900 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2901 +  every aufs configurations.
2902 +- build the module by simple "make".
2903 +  Note: Since linux-3.9, every filesystem module requires an alias
2904 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2905 +  modules.aliases file.
2906 +- you can specify ${KDIR} make variable which points to your kernel
2907 +  source tree.
2908 +- install the files
2909 +  + run "make install" to install the aufs module, or copy the built
2910 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2911 +  + run "make install_headers" (instead of headers_install) to install
2912 +    the modified aufs header file (you can specify DESTDIR which is
2913 +    available in aufs standalone version's Makefile only), or copy
2914 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2915 +    you like manually. By default, the target directory is $PWD/usr.
2916 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2917 +  kernel source tree.
2918 +
2919 +Note: The header file aufs_type.h is necessary to build aufs-util
2920 +      as well as "make headers_install" in the kernel source tree.
2921 +      headers_install is subject to be forgotten, but it is essentially
2922 +      necessary, not only for building aufs-util.
2923 +      You may not meet problems without headers_install in some older
2924 +      version though.
2925 +
2926 +And then,
2927 +- read README in aufs-util, build and install it
2928 +- note that your distribution may contain an obsoleted version of
2929 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2930 +  utilities, make sure that your compiler refers the correct aufs header
2931 +  file which is built by "make headers_install."
2932 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2933 +  then run "make install_ulib" too. And refer to the aufs manual in
2934 +  detail.
2935 +
2936 +There several other patches in aufs4-standalone.git. They are all
2937 +optional. When you meet some problems, they will help you.
2938 +- aufs4-loopback.patch
2939 +  Supports a nested loopback mount in a branch-fs. This patch is
2940 +  unnecessary until aufs produces a message like "you may want to try
2941 +  another patch for loopback file".
2942 +- vfs-ino.patch
2943 +  Modifies a system global kernel internal function get_next_ino() in
2944 +  order to stop assigning 0 for an inode-number. Not directly related to
2945 +  aufs, but recommended generally.
2946 +- tmpfs-idr.patch
2947 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2948 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2949 +  duplication of inode number, which is important for backup tools and
2950 +  other utilities. When you find aufs XINO files for tmpfs branch
2951 +  growing too much, try this patch.
2952 +- lockdep-debug.patch
2953 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2954 +  also a caller of VFS functions for branch filesystems, subclassing of
2955 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2956 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2957 +  need to apply this debug patch to expand several constant values.
2958 +  If don't know what LOCKDEP, then you don't have apply this patch.
2959 +
2960 +
2961 +4. Usage
2962 +----------------------------------------
2963 +At first, make sure aufs-util are installed, and please read the aufs
2964 +manual, aufs.5 in aufs-util.git tree.
2965 +$ man -l aufs.5
2966 +
2967 +And then,
2968 +$ mkdir /tmp/rw /tmp/aufs
2969 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2970 +
2971 +Here is another example. The result is equivalent.
2972 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2973 +  Or
2974 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2975 +# mount -o remount,append:${HOME} /tmp/aufs
2976 +
2977 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2978 +you modify a file under /tmp/aufs, the one on your home directory is
2979 +not affected, instead the same named file will be newly created under
2980 +/tmp/rw. And all of your modification to a file will be applied to
2981 +the one under /tmp/rw. This is called the file based Copy on Write
2982 +(COW) method.
2983 +Aufs mount options are described in aufs.5.
2984 +If you run chroot or something and make your aufs as a root directory,
2985 +then you need to customize the shutdown script. See the aufs manual in
2986 +detail.
2987 +
2988 +Additionally, there are some sample usages of aufs which are a
2989 +diskless system with network booting, and LiveCD over NFS.
2990 +See sample dir in CVS tree on SourceForge.
2991 +
2992 +
2993 +5. Contact
2994 +----------------------------------------
2995 +When you have any problems or strange behaviour in aufs, please let me
2996 +know with:
2997 +- /proc/mounts (instead of the output of mount(8))
2998 +- /sys/module/aufs/*
2999 +- /sys/fs/aufs/* (if you have them)
3000 +- /debug/aufs/* (if you have them)
3001 +- linux kernel version
3002 +  if your kernel is not plain, for example modified by distributor,
3003 +  the url where i can download its source is necessary too.
3004 +- aufs version which was printed at loading the module or booting the
3005 +  system, instead of the date you downloaded.
3006 +- configuration (define/undefine CONFIG_AUFS_xxx)
3007 +- kernel configuration or /proc/config.gz (if you have it)
3008 +- behaviour which you think to be incorrect
3009 +- actual operation, reproducible one is better
3010 +- mailto: aufs-users at lists.sourceforge.net
3011 +
3012 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
3013 +and Feature Requests) on SourceForge. Please join and write to
3014 +aufs-users ML.
3015 +
3016 +
3017 +6. Acknowledgements
3018 +----------------------------------------
3019 +Thanks to everyone who have tried and are using aufs, whoever
3020 +have reported a bug or any feedback.
3021 +
3022 +Especially donators:
3023 +Tomas Matejicek(slax.org) made a donation (much more than once).
3024 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
3025 +       scripts) is making "doubling" donations.
3026 +       Unfortunately I cannot list all of the donators, but I really
3027 +       appreciate.
3028 +       It ends Aug 2010, but the ordinary donation URL is still available.
3029 +       <http://sourceforge.net/donate/index.php?group_id=167503>
3030 +Dai Itasaka made a donation (2007/8).
3031 +Chuck Smith made a donation (2008/4, 10 and 12).
3032 +Henk Schoneveld made a donation (2008/9).
3033 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3034 +Francois Dupoux made a donation (2008/11).
3035 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3036 +       aufs2 GIT tree (2009/2).
3037 +William Grant made a donation (2009/3).
3038 +Patrick Lane made a donation (2009/4).
3039 +The Mail Archive (mail-archive.com) made donations (2009/5).
3040 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3041 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3042 +Pavel Pronskiy made a donation (2011/2).
3043 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3044 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
3045 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3046 +11).
3047 +Sam Liddicott made a donation (2011/9).
3048 +Era Scarecrow made a donation (2013/4).
3049 +Bor Ratajc made a donation (2013/4).
3050 +Alessandro Gorreta made a donation (2013/4).
3051 +POIRETTE Marc made a donation (2013/4).
3052 +Alessandro Gorreta made a donation (2013/4).
3053 +lauri kasvandik made a donation (2013/5).
3054 +"pemasu from Finland" made a donation (2013/7).
3055 +The Parted Magic Project made a donation (2013/9 and 11).
3056 +Pavel Barta made a donation (2013/10).
3057 +Nikolay Pertsev made a donation (2014/5).
3058 +James B made a donation (2014/7 and 2015/7).
3059 +Stefano Di Biase made a donation (2014/8).
3060 +Daniel Epellei made a donation (2015/1).
3061 +OmegaPhil made a donation (2016/1, 2018/4).
3062 +Tomasz Szewczyk made a donation (2016/4).
3063 +James Burry made a donation (2016/12).
3064 +
3065 +Thank you very much.
3066 +Donations are always, including future donations, very important and
3067 +helpful for me to keep on developing aufs.
3068 +
3069 +
3070 +7.
3071 +----------------------------------------
3072 +If you are an experienced user, no explanation is needed. Aufs is
3073 +just a linux filesystem.
3074 +
3075 +
3076 +Enjoy!
3077 +
3078 +# Local variables: ;
3079 +# mode: text;
3080 +# End: ;
3081 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3082 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
3083 +++ linux/fs/aufs/aufs.h        2018-07-19 09:46:13.053314374 +0200
3084 @@ -0,0 +1,61 @@
3085 +/* SPDX-License-Identifier: GPL-2.0 */
3086 +/*
3087 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3088 + *
3089 + * This program, aufs is free software; you can redistribute it and/or modify
3090 + * it under the terms of the GNU General Public License as published by
3091 + * the Free Software Foundation; either version 2 of the License, or
3092 + * (at your option) any later version.
3093 + *
3094 + * This program is distributed in the hope that it will be useful,
3095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3097 + * GNU General Public License for more details.
3098 + *
3099 + * You should have received a copy of the GNU General Public License
3100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3101 + */
3102 +
3103 +/*
3104 + * all header files
3105 + */
3106 +
3107 +#ifndef __AUFS_H__
3108 +#define __AUFS_H__
3109 +
3110 +#ifdef __KERNEL__
3111 +
3112 +#define AuStub(type, name, body, ...) \
3113 +       static inline type name(__VA_ARGS__) { body; }
3114 +
3115 +#define AuStubVoid(name, ...) \
3116 +       AuStub(void, name, , __VA_ARGS__)
3117 +#define AuStubInt0(name, ...) \
3118 +       AuStub(int, name, return 0, __VA_ARGS__)
3119 +
3120 +#include "debug.h"
3121 +
3122 +#include "branch.h"
3123 +#include "cpup.h"
3124 +#include "dcsub.h"
3125 +#include "dbgaufs.h"
3126 +#include "dentry.h"
3127 +#include "dir.h"
3128 +#include "dirren.h"
3129 +#include "dynop.h"
3130 +#include "file.h"
3131 +#include "fstype.h"
3132 +#include "hbl.h"
3133 +#include "inode.h"
3134 +#include "loop.h"
3135 +#include "module.h"
3136 +#include "opts.h"
3137 +#include "rwsem.h"
3138 +#include "super.h"
3139 +#include "sysaufs.h"
3140 +#include "vfsub.h"
3141 +#include "whout.h"
3142 +#include "wkq.h"
3143 +
3144 +#endif /* __KERNEL__ */
3145 +#endif /* __AUFS_H__ */
3146 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3147 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3148 +++ linux/fs/aufs/branch.c      2018-07-19 09:46:13.053314374 +0200
3149 @@ -0,0 +1,1422 @@
3150 +/* SPDX-License-Identifier: GPL-2.0 */
3151 +/*
3152 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3153 + *
3154 + * This program, aufs is free software; you can redistribute it and/or modify
3155 + * it under the terms of the GNU General Public License as published by
3156 + * the Free Software Foundation; either version 2 of the License, or
3157 + * (at your option) any later version.
3158 + *
3159 + * This program is distributed in the hope that it will be useful,
3160 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3161 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3162 + * GNU General Public License for more details.
3163 + *
3164 + * You should have received a copy of the GNU General Public License
3165 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3166 + */
3167 +
3168 +/*
3169 + * branch management
3170 + */
3171 +
3172 +#include <linux/compat.h>
3173 +#include <linux/statfs.h>
3174 +#include "aufs.h"
3175 +
3176 +/*
3177 + * free a single branch
3178 + */
3179 +static void au_br_do_free(struct au_branch *br)
3180 +{
3181 +       int i;
3182 +       struct au_wbr *wbr;
3183 +       struct au_dykey **key;
3184 +
3185 +       au_hnotify_fin_br(br);
3186 +       /* always, regardless the mount option */
3187 +       au_dr_hino_free(&br->br_dirren);
3188 +       au_xino_put(br);
3189 +
3190 +       AuDebugOn(au_br_count(br));
3191 +       au_br_count_fin(br);
3192 +
3193 +       wbr = br->br_wbr;
3194 +       if (wbr) {
3195 +               for (i = 0; i < AuBrWh_Last; i++)
3196 +                       dput(wbr->wbr_wh[i]);
3197 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3198 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3199 +       }
3200 +
3201 +       if (br->br_fhsm) {
3202 +               au_br_fhsm_fin(br->br_fhsm);
3203 +               kfree(br->br_fhsm);
3204 +       }
3205 +
3206 +       key = br->br_dykey;
3207 +       for (i = 0; i < AuBrDynOp; i++, key++)
3208 +               if (*key)
3209 +                       au_dy_put(*key);
3210 +               else
3211 +                       break;
3212 +
3213 +       /* recursive lock, s_umount of branch's */
3214 +       lockdep_off();
3215 +       path_put(&br->br_path);
3216 +       lockdep_on();
3217 +       kfree(wbr);
3218 +       kfree(br);
3219 +}
3220 +
3221 +/*
3222 + * frees all branches
3223 + */
3224 +void au_br_free(struct au_sbinfo *sbinfo)
3225 +{
3226 +       aufs_bindex_t bmax;
3227 +       struct au_branch **br;
3228 +
3229 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3230 +
3231 +       bmax = sbinfo->si_bbot + 1;
3232 +       br = sbinfo->si_branch;
3233 +       while (bmax--)
3234 +               au_br_do_free(*br++);
3235 +}
3236 +
3237 +/*
3238 + * find the index of a branch which is specified by @br_id.
3239 + */
3240 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3241 +{
3242 +       aufs_bindex_t bindex, bbot;
3243 +
3244 +       bbot = au_sbbot(sb);
3245 +       for (bindex = 0; bindex <= bbot; bindex++)
3246 +               if (au_sbr_id(sb, bindex) == br_id)
3247 +                       return bindex;
3248 +       return -1;
3249 +}
3250 +
3251 +/* ---------------------------------------------------------------------- */
3252 +
3253 +/*
3254 + * add a branch
3255 + */
3256 +
3257 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3258 +                       struct dentry *h_root)
3259 +{
3260 +       if (unlikely(h_adding == h_root
3261 +                    || au_test_loopback_overlap(sb, h_adding)))
3262 +               return 1;
3263 +       if (h_adding->d_sb != h_root->d_sb)
3264 +               return 0;
3265 +       return au_test_subdir(h_adding, h_root)
3266 +               || au_test_subdir(h_root, h_adding);
3267 +}
3268 +
3269 +/*
3270 + * returns a newly allocated branch. @new_nbranch is a number of branches
3271 + * after adding a branch.
3272 + */
3273 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3274 +                                    int perm)
3275 +{
3276 +       struct au_branch *add_branch;
3277 +       struct dentry *root;
3278 +       struct inode *inode;
3279 +       int err;
3280 +
3281 +       err = -ENOMEM;
3282 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3283 +       if (unlikely(!add_branch))
3284 +               goto out;
3285 +       add_branch->br_xino = au_xino_alloc();
3286 +       if (unlikely(!add_branch->br_xino))
3287 +               goto out_br;
3288 +       err = au_hnotify_init_br(add_branch, perm);
3289 +       if (unlikely(err))
3290 +               goto out_xino;
3291 +
3292 +       if (au_br_writable(perm)) {
3293 +               /* may be freed separately at changing the branch permission */
3294 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3295 +                                            GFP_NOFS);
3296 +               if (unlikely(!add_branch->br_wbr))
3297 +                       goto out_hnotify;
3298 +       }
3299 +
3300 +       if (au_br_fhsm(perm)) {
3301 +               err = au_fhsm_br_alloc(add_branch);
3302 +               if (unlikely(err))
3303 +                       goto out_wbr;
3304 +       }
3305 +
3306 +       root = sb->s_root;
3307 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3308 +       if (!err)
3309 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3310 +       if (!err) {
3311 +               inode = d_inode(root);
3312 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3313 +                                       /*may_shrink*/0);
3314 +       }
3315 +       if (!err)
3316 +               return add_branch; /* success */
3317 +
3318 +out_wbr:
3319 +       kfree(add_branch->br_wbr);
3320 +out_hnotify:
3321 +       au_hnotify_fin_br(add_branch);
3322 +out_xino:
3323 +       au_xino_put(add_branch);
3324 +out_br:
3325 +       kfree(add_branch);
3326 +out:
3327 +       return ERR_PTR(err);
3328 +}
3329 +
3330 +/*
3331 + * test if the branch permission is legal or not.
3332 + */
3333 +static int test_br(struct inode *inode, int brperm, char *path)
3334 +{
3335 +       int err;
3336 +
3337 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3338 +       if (!err)
3339 +               goto out;
3340 +
3341 +       err = -EINVAL;
3342 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3343 +
3344 +out:
3345 +       return err;
3346 +}
3347 +
3348 +/*
3349 + * returns:
3350 + * 0: success, the caller will add it
3351 + * plus: success, it is already unified, the caller should ignore it
3352 + * minus: error
3353 + */
3354 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3355 +{
3356 +       int err;
3357 +       aufs_bindex_t bbot, bindex;
3358 +       struct dentry *root, *h_dentry;
3359 +       struct inode *inode, *h_inode;
3360 +
3361 +       root = sb->s_root;
3362 +       bbot = au_sbbot(sb);
3363 +       if (unlikely(bbot >= 0
3364 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3365 +               err = 1;
3366 +               if (!remount) {
3367 +                       err = -EINVAL;
3368 +                       pr_err("%s duplicated\n", add->pathname);
3369 +               }
3370 +               goto out;
3371 +       }
3372 +
3373 +       err = -ENOSPC; /* -E2BIG; */
3374 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3375 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3376 +               pr_err("number of branches exceeded %s\n", add->pathname);
3377 +               goto out;
3378 +       }
3379 +
3380 +       err = -EDOM;
3381 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3382 +               pr_err("bad index %d\n", add->bindex);
3383 +               goto out;
3384 +       }
3385 +
3386 +       inode = d_inode(add->path.dentry);
3387 +       err = -ENOENT;
3388 +       if (unlikely(!inode->i_nlink)) {
3389 +               pr_err("no existence %s\n", add->pathname);
3390 +               goto out;
3391 +       }
3392 +
3393 +       err = -EINVAL;
3394 +       if (unlikely(inode->i_sb == sb)) {
3395 +               pr_err("%s must be outside\n", add->pathname);
3396 +               goto out;
3397 +       }
3398 +
3399 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3400 +               pr_err("unsupported filesystem, %s (%s)\n",
3401 +                      add->pathname, au_sbtype(inode->i_sb));
3402 +               goto out;
3403 +       }
3404 +
3405 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3406 +               pr_err("already stacked, %s (%s)\n",
3407 +                      add->pathname, au_sbtype(inode->i_sb));
3408 +               goto out;
3409 +       }
3410 +
3411 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3412 +       if (unlikely(err))
3413 +               goto out;
3414 +
3415 +       if (bbot < 0)
3416 +               return 0; /* success */
3417 +
3418 +       err = -EINVAL;
3419 +       for (bindex = 0; bindex <= bbot; bindex++)
3420 +               if (unlikely(test_overlap(sb, add->path.dentry,
3421 +                                         au_h_dptr(root, bindex)))) {
3422 +                       pr_err("%s is overlapped\n", add->pathname);
3423 +                       goto out;
3424 +               }
3425 +
3426 +       err = 0;
3427 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3428 +               h_dentry = au_h_dptr(root, 0);
3429 +               h_inode = d_inode(h_dentry);
3430 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3431 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3432 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3433 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3434 +                               add->pathname,
3435 +                               i_uid_read(inode), i_gid_read(inode),
3436 +                               (inode->i_mode & S_IALLUGO),
3437 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3438 +                               (h_inode->i_mode & S_IALLUGO));
3439 +       }
3440 +
3441 +out:
3442 +       return err;
3443 +}
3444 +
3445 +/*
3446 + * initialize or clean the whiteouts for an adding branch
3447 + */
3448 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3449 +                        int new_perm)
3450 +{
3451 +       int err, old_perm;
3452 +       aufs_bindex_t bindex;
3453 +       struct inode *h_inode;
3454 +       struct au_wbr *wbr;
3455 +       struct au_hinode *hdir;
3456 +       struct dentry *h_dentry;
3457 +
3458 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3459 +       if (unlikely(err))
3460 +               goto out;
3461 +
3462 +       wbr = br->br_wbr;
3463 +       old_perm = br->br_perm;
3464 +       br->br_perm = new_perm;
3465 +       hdir = NULL;
3466 +       h_inode = NULL;
3467 +       bindex = au_br_index(sb, br->br_id);
3468 +       if (0 <= bindex) {
3469 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3470 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3471 +       } else {
3472 +               h_dentry = au_br_dentry(br);
3473 +               h_inode = d_inode(h_dentry);
3474 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3475 +       }
3476 +       if (!wbr)
3477 +               err = au_wh_init(br, sb);
3478 +       else {
3479 +               wbr_wh_write_lock(wbr);
3480 +               err = au_wh_init(br, sb);
3481 +               wbr_wh_write_unlock(wbr);
3482 +       }
3483 +       if (hdir)
3484 +               au_hn_inode_unlock(hdir);
3485 +       else
3486 +               inode_unlock(h_inode);
3487 +       vfsub_mnt_drop_write(au_br_mnt(br));
3488 +       br->br_perm = old_perm;
3489 +
3490 +       if (!err && wbr && !au_br_writable(new_perm)) {
3491 +               kfree(wbr);
3492 +               br->br_wbr = NULL;
3493 +       }
3494 +
3495 +out:
3496 +       return err;
3497 +}
3498 +
3499 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3500 +                      int perm)
3501 +{
3502 +       int err;
3503 +       struct kstatfs kst;
3504 +       struct au_wbr *wbr;
3505 +
3506 +       wbr = br->br_wbr;
3507 +       au_rw_init(&wbr->wbr_wh_rwsem);
3508 +       atomic_set(&wbr->wbr_wh_running, 0);
3509 +
3510 +       /*
3511 +        * a limit for rmdir/rename a dir
3512 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3513 +        */
3514 +       err = vfs_statfs(&br->br_path, &kst);
3515 +       if (unlikely(err))
3516 +               goto out;
3517 +       err = -EINVAL;
3518 +       if (kst.f_namelen >= NAME_MAX)
3519 +               err = au_br_init_wh(sb, br, perm);
3520 +       else
3521 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3522 +                      au_br_dentry(br),
3523 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3524 +
3525 +out:
3526 +       return err;
3527 +}
3528 +
3529 +/* initialize a new branch */
3530 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3531 +                     struct au_opt_add *add)
3532 +{
3533 +       int err;
3534 +       struct au_branch *brbase;
3535 +       struct file *xf;
3536 +       struct inode *h_inode;
3537 +
3538 +       err = 0;
3539 +       br->br_perm = add->perm;
3540 +       br->br_path = add->path; /* set first, path_get() later */
3541 +       spin_lock_init(&br->br_dykey_lock);
3542 +       au_br_count_init(br);
3543 +       br->br_id = au_new_br_id(sb);
3544 +       AuDebugOn(br->br_id < 0);
3545 +
3546 +       /* always, regardless the given option */
3547 +       err = au_dr_br_init(sb, br, &add->path);
3548 +       if (unlikely(err))
3549 +               goto out_err;
3550 +
3551 +       if (au_br_writable(add->perm)) {
3552 +               err = au_wbr_init(br, sb, add->perm);
3553 +               if (unlikely(err))
3554 +                       goto out_err;
3555 +       }
3556 +
3557 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3558 +               brbase = au_sbr(sb, 0);
3559 +               xf = au_xino_file(brbase);
3560 +               AuDebugOn(!xf);
3561 +               h_inode = d_inode(add->path.dentry);
3562 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3563 +               if (unlikely(err)) {
3564 +                       AuDebugOn(au_xino_file(br));
3565 +                       goto out_err;
3566 +               }
3567 +       }
3568 +
3569 +       sysaufs_br_init(br);
3570 +       path_get(&br->br_path);
3571 +       goto out; /* success */
3572 +
3573 +out_err:
3574 +       memset(&br->br_path, 0, sizeof(br->br_path));
3575 +out:
3576 +       return err;
3577 +}
3578 +
3579 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3580 +                            struct au_branch *br, aufs_bindex_t bbot,
3581 +                            aufs_bindex_t amount)
3582 +{
3583 +       struct au_branch **brp;
3584 +
3585 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3586 +
3587 +       brp = sbinfo->si_branch + bindex;
3588 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3589 +       *brp = br;
3590 +       sbinfo->si_bbot++;
3591 +       if (unlikely(bbot < 0))
3592 +               sbinfo->si_bbot = 0;
3593 +}
3594 +
3595 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3596 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3597 +{
3598 +       struct au_hdentry *hdp;
3599 +
3600 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3601 +
3602 +       hdp = au_hdentry(dinfo, bindex);
3603 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3604 +       au_h_dentry_init(hdp);
3605 +       dinfo->di_bbot++;
3606 +       if (unlikely(bbot < 0))
3607 +               dinfo->di_btop = 0;
3608 +}
3609 +
3610 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3611 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3612 +{
3613 +       struct au_hinode *hip;
3614 +
3615 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3616 +
3617 +       hip = au_hinode(iinfo, bindex);
3618 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3619 +       au_hinode_init(hip);
3620 +       iinfo->ii_bbot++;
3621 +       if (unlikely(bbot < 0))
3622 +               iinfo->ii_btop = 0;
3623 +}
3624 +
3625 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3626 +                        aufs_bindex_t bindex)
3627 +{
3628 +       struct dentry *root, *h_dentry;
3629 +       struct inode *root_inode, *h_inode;
3630 +       aufs_bindex_t bbot, amount;
3631 +
3632 +       root = sb->s_root;
3633 +       root_inode = d_inode(root);
3634 +       bbot = au_sbbot(sb);
3635 +       amount = bbot + 1 - bindex;
3636 +       h_dentry = au_br_dentry(br);
3637 +       au_sbilist_lock();
3638 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3639 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3640 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3641 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3642 +       h_inode = d_inode(h_dentry);
3643 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3644 +       au_sbilist_unlock();
3645 +}
3646 +
3647 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3648 +{
3649 +       int err;
3650 +       aufs_bindex_t bbot, add_bindex;
3651 +       struct dentry *root, *h_dentry;
3652 +       struct inode *root_inode;
3653 +       struct au_branch *add_branch;
3654 +       struct file *xf;
3655 +
3656 +       root = sb->s_root;
3657 +       root_inode = d_inode(root);
3658 +       IMustLock(root_inode);
3659 +       IiMustWriteLock(root_inode);
3660 +       err = test_add(sb, add, remount);
3661 +       if (unlikely(err < 0))
3662 +               goto out;
3663 +       if (err) {
3664 +               err = 0;
3665 +               goto out; /* success */
3666 +       }
3667 +
3668 +       bbot = au_sbbot(sb);
3669 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3670 +       err = PTR_ERR(add_branch);
3671 +       if (IS_ERR(add_branch))
3672 +               goto out;
3673 +
3674 +       err = au_br_init(add_branch, sb, add);
3675 +       if (unlikely(err)) {
3676 +               au_br_do_free(add_branch);
3677 +               goto out;
3678 +       }
3679 +
3680 +       add_bindex = add->bindex;
3681 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3682 +       au_br_do_add(sb, add_branch, add_bindex);
3683 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3684 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3685 +
3686 +       h_dentry = add->path.dentry;
3687 +       if (!add_bindex) {
3688 +               au_cpup_attr_all(root_inode, /*force*/1);
3689 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3690 +       } else
3691 +               au_add_nlink(root_inode, d_inode(h_dentry));
3692 +
3693 +       /*
3694 +        * this test/set prevents aufs from handling unnecessary notify events
3695 +        * of xino files, in case of re-adding a writable branch which was
3696 +        * once detached from aufs.
3697 +        */
3698 +       if (au_xino_brid(sb) < 0
3699 +           && au_br_writable(add_branch->br_perm)
3700 +           && !au_test_fs_bad_xino(h_dentry->d_sb)) {
3701 +               xf = au_xino_file(add_branch);
3702 +               if (xf && xf->f_path.dentry->d_parent == h_dentry)
3703 +                       au_xino_brid_set(sb, add_branch->br_id);
3704 +       }
3705 +
3706 +out:
3707 +       return err;
3708 +}
3709 +
3710 +/* ---------------------------------------------------------------------- */
3711 +
3712 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3713 +                                      unsigned long long max __maybe_unused,
3714 +                                      void *arg)
3715 +{
3716 +       unsigned long long n;
3717 +       struct file **p, *f;
3718 +       struct hlist_bl_head *files;
3719 +       struct hlist_bl_node *pos;
3720 +       struct au_finfo *finfo;
3721 +
3722 +       n = 0;
3723 +       p = a;
3724 +       files = &au_sbi(sb)->si_files;
3725 +       hlist_bl_lock(files);
3726 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3727 +               f = finfo->fi_file;
3728 +               if (file_count(f)
3729 +                   && !special_file(file_inode(f)->i_mode)) {
3730 +                       get_file(f);
3731 +                       *p++ = f;
3732 +                       n++;
3733 +                       AuDebugOn(n > max);
3734 +               }
3735 +       }
3736 +       hlist_bl_unlock(files);
3737 +
3738 +       return n;
3739 +}
3740 +
3741 +static struct file **au_farray_alloc(struct super_block *sb,
3742 +                                    unsigned long long *max)
3743 +{
3744 +       *max = au_nfiles(sb);
3745 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3746 +}
3747 +
3748 +static void au_farray_free(struct file **a, unsigned long long max)
3749 +{
3750 +       unsigned long long ull;
3751 +
3752 +       for (ull = 0; ull < max; ull++)
3753 +               if (a[ull])
3754 +                       fput(a[ull]);
3755 +       kvfree(a);
3756 +}
3757 +
3758 +/* ---------------------------------------------------------------------- */
3759 +
3760 +/*
3761 + * delete a branch
3762 + */
3763 +
3764 +/* to show the line number, do not make it inlined function */
3765 +#define AuVerbose(do_info, fmt, ...) do { \
3766 +       if (do_info) \
3767 +               pr_info(fmt, ##__VA_ARGS__); \
3768 +} while (0)
3769 +
3770 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3771 +                        aufs_bindex_t bbot)
3772 +{
3773 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3774 +}
3775 +
3776 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3777 +                        aufs_bindex_t bbot)
3778 +{
3779 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3780 +}
3781 +
3782 +/*
3783 + * test if the branch is deletable or not.
3784 + */
3785 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3786 +                           unsigned int sigen, const unsigned int verbose)
3787 +{
3788 +       int err, i, j, ndentry;
3789 +       aufs_bindex_t btop, bbot;
3790 +       struct au_dcsub_pages dpages;
3791 +       struct au_dpage *dpage;
3792 +       struct dentry *d;
3793 +
3794 +       err = au_dpages_init(&dpages, GFP_NOFS);
3795 +       if (unlikely(err))
3796 +               goto out;
3797 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3798 +       if (unlikely(err))
3799 +               goto out_dpages;
3800 +
3801 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3802 +               dpage = dpages.dpages + i;
3803 +               ndentry = dpage->ndentry;
3804 +               for (j = 0; !err && j < ndentry; j++) {
3805 +                       d = dpage->dentries[j];
3806 +                       AuDebugOn(au_dcount(d) <= 0);
3807 +                       if (!au_digen_test(d, sigen)) {
3808 +                               di_read_lock_child(d, AuLock_IR);
3809 +                               if (unlikely(au_dbrange_test(d))) {
3810 +                                       di_read_unlock(d, AuLock_IR);
3811 +                                       continue;
3812 +                               }
3813 +                       } else {
3814 +                               di_write_lock_child(d);
3815 +                               if (unlikely(au_dbrange_test(d))) {
3816 +                                       di_write_unlock(d);
3817 +                                       continue;
3818 +                               }
3819 +                               err = au_reval_dpath(d, sigen);
3820 +                               if (!err)
3821 +                                       di_downgrade_lock(d, AuLock_IR);
3822 +                               else {
3823 +                                       di_write_unlock(d);
3824 +                                       break;
3825 +                               }
3826 +                       }
3827 +
3828 +                       /* AuDbgDentry(d); */
3829 +                       btop = au_dbtop(d);
3830 +                       bbot = au_dbbot(d);
3831 +                       if (btop <= bindex
3832 +                           && bindex <= bbot
3833 +                           && au_h_dptr(d, bindex)
3834 +                           && au_test_dbusy(d, btop, bbot)) {
3835 +                               err = -EBUSY;
3836 +                               AuVerbose(verbose, "busy %pd\n", d);
3837 +                               AuDbgDentry(d);
3838 +                       }
3839 +                       di_read_unlock(d, AuLock_IR);
3840 +               }
3841 +       }
3842 +
3843 +out_dpages:
3844 +       au_dpages_free(&dpages);
3845 +out:
3846 +       return err;
3847 +}
3848 +
3849 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3850 +                          unsigned int sigen, const unsigned int verbose)
3851 +{
3852 +       int err;
3853 +       unsigned long long max, ull;
3854 +       struct inode *i, **array;
3855 +       aufs_bindex_t btop, bbot;
3856 +
3857 +       array = au_iarray_alloc(sb, &max);
3858 +       err = PTR_ERR(array);
3859 +       if (IS_ERR(array))
3860 +               goto out;
3861 +
3862 +       err = 0;
3863 +       AuDbg("b%d\n", bindex);
3864 +       for (ull = 0; !err && ull < max; ull++) {
3865 +               i = array[ull];
3866 +               if (unlikely(!i))
3867 +                       break;
3868 +               if (i->i_ino == AUFS_ROOT_INO)
3869 +                       continue;
3870 +
3871 +               /* AuDbgInode(i); */
3872 +               if (au_iigen(i, NULL) == sigen)
3873 +                       ii_read_lock_child(i);
3874 +               else {
3875 +                       ii_write_lock_child(i);
3876 +                       err = au_refresh_hinode_self(i);
3877 +                       au_iigen_dec(i);
3878 +                       if (!err)
3879 +                               ii_downgrade_lock(i);
3880 +                       else {
3881 +                               ii_write_unlock(i);
3882 +                               break;
3883 +                       }
3884 +               }
3885 +
3886 +               btop = au_ibtop(i);
3887 +               bbot = au_ibbot(i);
3888 +               if (btop <= bindex
3889 +                   && bindex <= bbot
3890 +                   && au_h_iptr(i, bindex)
3891 +                   && au_test_ibusy(i, btop, bbot)) {
3892 +                       err = -EBUSY;
3893 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3894 +                       AuDbgInode(i);
3895 +               }
3896 +               ii_read_unlock(i);
3897 +       }
3898 +       au_iarray_free(array, max);
3899 +
3900 +out:
3901 +       return err;
3902 +}
3903 +
3904 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3905 +                             const unsigned int verbose)
3906 +{
3907 +       int err;
3908 +       unsigned int sigen;
3909 +
3910 +       sigen = au_sigen(root->d_sb);
3911 +       DiMustNoWaiters(root);
3912 +       IiMustNoWaiters(d_inode(root));
3913 +       di_write_unlock(root);
3914 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3915 +       if (!err)
3916 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3917 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3918 +
3919 +       return err;
3920 +}
3921 +
3922 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3923 +                        struct file **to_free, int *idx)
3924 +{
3925 +       int err;
3926 +       unsigned char matched, root;
3927 +       aufs_bindex_t bindex, bbot;
3928 +       struct au_fidir *fidir;
3929 +       struct au_hfile *hfile;
3930 +
3931 +       err = 0;
3932 +       root = IS_ROOT(file->f_path.dentry);
3933 +       if (root) {
3934 +               get_file(file);
3935 +               to_free[*idx] = file;
3936 +               (*idx)++;
3937 +               goto out;
3938 +       }
3939 +
3940 +       matched = 0;
3941 +       fidir = au_fi(file)->fi_hdir;
3942 +       AuDebugOn(!fidir);
3943 +       bbot = au_fbbot_dir(file);
3944 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3945 +               hfile = fidir->fd_hfile + bindex;
3946 +               if (!hfile->hf_file)
3947 +                       continue;
3948 +
3949 +               if (hfile->hf_br->br_id == br_id) {
3950 +                       matched = 1;
3951 +                       break;
3952 +               }
3953 +       }
3954 +       if (matched)
3955 +               err = -EBUSY;
3956 +
3957 +out:
3958 +       return err;
3959 +}
3960 +
3961 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3962 +                         struct file **to_free, int opened)
3963 +{
3964 +       int err, idx;
3965 +       unsigned long long ull, max;
3966 +       aufs_bindex_t btop;
3967 +       struct file *file, **array;
3968 +       struct dentry *root;
3969 +       struct au_hfile *hfile;
3970 +
3971 +       array = au_farray_alloc(sb, &max);
3972 +       err = PTR_ERR(array);
3973 +       if (IS_ERR(array))
3974 +               goto out;
3975 +
3976 +       err = 0;
3977 +       idx = 0;
3978 +       root = sb->s_root;
3979 +       di_write_unlock(root);
3980 +       for (ull = 0; ull < max; ull++) {
3981 +               file = array[ull];
3982 +               if (unlikely(!file))
3983 +                       break;
3984 +
3985 +               /* AuDbg("%pD\n", file); */
3986 +               fi_read_lock(file);
3987 +               btop = au_fbtop(file);
3988 +               if (!d_is_dir(file->f_path.dentry)) {
3989 +                       hfile = &au_fi(file)->fi_htop;
3990 +                       if (hfile->hf_br->br_id == br_id)
3991 +                               err = -EBUSY;
3992 +               } else
3993 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3994 +               fi_read_unlock(file);
3995 +               if (unlikely(err))
3996 +                       break;
3997 +       }
3998 +       di_write_lock_child(root);
3999 +       au_farray_free(array, max);
4000 +       AuDebugOn(idx > opened);
4001 +
4002 +out:
4003 +       return err;
4004 +}
4005 +
4006 +static void br_del_file(struct file **to_free, unsigned long long opened,
4007 +                       aufs_bindex_t br_id)
4008 +{
4009 +       unsigned long long ull;
4010 +       aufs_bindex_t bindex, btop, bbot, bfound;
4011 +       struct file *file;
4012 +       struct au_fidir *fidir;
4013 +       struct au_hfile *hfile;
4014 +
4015 +       for (ull = 0; ull < opened; ull++) {
4016 +               file = to_free[ull];
4017 +               if (unlikely(!file))
4018 +                       break;
4019 +
4020 +               /* AuDbg("%pD\n", file); */
4021 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
4022 +               bfound = -1;
4023 +               fidir = au_fi(file)->fi_hdir;
4024 +               AuDebugOn(!fidir);
4025 +               fi_write_lock(file);
4026 +               btop = au_fbtop(file);
4027 +               bbot = au_fbbot_dir(file);
4028 +               for (bindex = btop; bindex <= bbot; bindex++) {
4029 +                       hfile = fidir->fd_hfile + bindex;
4030 +                       if (!hfile->hf_file)
4031 +                               continue;
4032 +
4033 +                       if (hfile->hf_br->br_id == br_id) {
4034 +                               bfound = bindex;
4035 +                               break;
4036 +                       }
4037 +               }
4038 +               AuDebugOn(bfound < 0);
4039 +               au_set_h_fptr(file, bfound, NULL);
4040 +               if (bfound == btop) {
4041 +                       for (btop++; btop <= bbot; btop++)
4042 +                               if (au_hf_dir(file, btop)) {
4043 +                                       au_set_fbtop(file, btop);
4044 +                                       break;
4045 +                               }
4046 +               }
4047 +               fi_write_unlock(file);
4048 +       }
4049 +}
4050 +
4051 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4052 +                            const aufs_bindex_t bindex,
4053 +                            const aufs_bindex_t bbot)
4054 +{
4055 +       struct au_branch **brp, **p;
4056 +
4057 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
4058 +
4059 +       brp = sbinfo->si_branch + bindex;
4060 +       if (bindex < bbot)
4061 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4062 +       sbinfo->si_branch[0 + bbot] = NULL;
4063 +       sbinfo->si_bbot--;
4064 +
4065 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4066 +                       /*may_shrink*/1);
4067 +       if (p)
4068 +               sbinfo->si_branch = p;
4069 +       /* harmless error */
4070 +}
4071 +
4072 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
4073 +                            const aufs_bindex_t bbot)
4074 +{
4075 +       struct au_hdentry *hdp, *p;
4076 +
4077 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4078 +
4079 +       hdp = au_hdentry(dinfo, bindex);
4080 +       if (bindex < bbot)
4081 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4082 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4083 +       dinfo->di_bbot--;
4084 +
4085 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4086 +                       /*may_shrink*/1);
4087 +       if (p)
4088 +               dinfo->di_hdentry = p;
4089 +       /* harmless error */
4090 +}
4091 +
4092 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
4093 +                            const aufs_bindex_t bbot)
4094 +{
4095 +       struct au_hinode *hip, *p;
4096 +
4097 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
4098 +
4099 +       hip = au_hinode(iinfo, bindex);
4100 +       if (bindex < bbot)
4101 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4102 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
4103 +       iinfo->ii_bbot--;
4104 +
4105 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4106 +                       /*may_shrink*/1);
4107 +       if (p)
4108 +               iinfo->ii_hinode = p;
4109 +       /* harmless error */
4110 +}
4111 +
4112 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4113 +                        struct au_branch *br)
4114 +{
4115 +       aufs_bindex_t bbot;
4116 +       struct au_sbinfo *sbinfo;
4117 +       struct dentry *root, *h_root;
4118 +       struct inode *inode, *h_inode;
4119 +       struct au_hinode *hinode;
4120 +
4121 +       SiMustWriteLock(sb);
4122 +
4123 +       root = sb->s_root;
4124 +       inode = d_inode(root);
4125 +       sbinfo = au_sbi(sb);
4126 +       bbot = sbinfo->si_bbot;
4127 +
4128 +       h_root = au_h_dptr(root, bindex);
4129 +       hinode = au_hi(inode, bindex);
4130 +       h_inode = au_igrab(hinode->hi_inode);
4131 +       au_hiput(hinode);
4132 +
4133 +       au_sbilist_lock();
4134 +       au_br_do_del_brp(sbinfo, bindex, bbot);
4135 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
4136 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
4137 +       au_sbilist_unlock();
4138 +
4139 +       /* ignore an error */
4140 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
4141 +
4142 +       dput(h_root);
4143 +       iput(h_inode);
4144 +       au_br_do_free(br);
4145 +}
4146 +
4147 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4148 +                                  unsigned long long max, void *arg)
4149 +{
4150 +       return max;
4151 +}
4152 +
4153 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4154 +{
4155 +       int err, rerr, i;
4156 +       unsigned long long opened;
4157 +       unsigned int mnt_flags;
4158 +       aufs_bindex_t bindex, bbot, br_id;
4159 +       unsigned char do_wh, verbose;
4160 +       struct au_branch *br;
4161 +       struct au_wbr *wbr;
4162 +       struct dentry *root;
4163 +       struct file **to_free;
4164 +
4165 +       err = 0;
4166 +       opened = 0;
4167 +       to_free = NULL;
4168 +       root = sb->s_root;
4169 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4170 +       if (bindex < 0) {
4171 +               if (remount)
4172 +                       goto out; /* success */
4173 +               err = -ENOENT;
4174 +               pr_err("%s no such branch\n", del->pathname);
4175 +               goto out;
4176 +       }
4177 +       AuDbg("bindex b%d\n", bindex);
4178 +
4179 +       err = -EBUSY;
4180 +       mnt_flags = au_mntflags(sb);
4181 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4182 +       bbot = au_sbbot(sb);
4183 +       if (unlikely(!bbot)) {
4184 +               AuVerbose(verbose, "no more branches left\n");
4185 +               goto out;
4186 +       }
4187 +       br = au_sbr(sb, bindex);
4188 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4189 +
4190 +       br_id = br->br_id;
4191 +       opened = au_br_count(br);
4192 +       if (unlikely(opened)) {
4193 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4194 +               err = PTR_ERR(to_free);
4195 +               if (IS_ERR(to_free))
4196 +                       goto out;
4197 +
4198 +               err = test_file_busy(sb, br_id, to_free, opened);
4199 +               if (unlikely(err)) {
4200 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4201 +                       goto out;
4202 +               }
4203 +       }
4204 +
4205 +       wbr = br->br_wbr;
4206 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4207 +       if (do_wh) {
4208 +               /* instead of WbrWhMustWriteLock(wbr) */
4209 +               SiMustWriteLock(sb);
4210 +               for (i = 0; i < AuBrWh_Last; i++) {
4211 +                       dput(wbr->wbr_wh[i]);
4212 +                       wbr->wbr_wh[i] = NULL;
4213 +               }
4214 +       }
4215 +
4216 +       err = test_children_busy(root, bindex, verbose);
4217 +       if (unlikely(err)) {
4218 +               if (do_wh)
4219 +                       goto out_wh;
4220 +               goto out;
4221 +       }
4222 +
4223 +       err = 0;
4224 +       if (to_free) {
4225 +               /*
4226 +                * now we confirmed the branch is deletable.
4227 +                * let's free the remaining opened dirs on the branch.
4228 +                */
4229 +               di_write_unlock(root);
4230 +               br_del_file(to_free, opened, br_id);
4231 +               di_write_lock_child(root);
4232 +       }
4233 +
4234 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4235 +       dbgaufs_xino_del(br);           /* remove one */
4236 +       au_br_do_del(sb, bindex, br);
4237 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4238 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4239 +
4240 +       if (!bindex) {
4241 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4242 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4243 +       } else
4244 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4245 +       if (au_opt_test(mnt_flags, PLINK))
4246 +               au_plink_half_refresh(sb, br_id);
4247 +
4248 +       if (au_xino_brid(sb) == br_id)
4249 +               au_xino_brid_set(sb, -1);
4250 +       goto out; /* success */
4251 +
4252 +out_wh:
4253 +       /* revert */
4254 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4255 +       if (rerr)
4256 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4257 +                       del->pathname, rerr);
4258 +out:
4259 +       if (to_free)
4260 +               au_farray_free(to_free, opened);
4261 +       return err;
4262 +}
4263 +
4264 +/* ---------------------------------------------------------------------- */
4265 +
4266 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4267 +{
4268 +       int err;
4269 +       aufs_bindex_t btop, bbot;
4270 +       struct aufs_ibusy ibusy;
4271 +       struct inode *inode, *h_inode;
4272 +
4273 +       err = -EPERM;
4274 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4275 +               goto out;
4276 +
4277 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4278 +       if (!err)
4279 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4280 +       if (unlikely(err)) {
4281 +               err = -EFAULT;
4282 +               AuTraceErr(err);
4283 +               goto out;
4284 +       }
4285 +
4286 +       err = -EINVAL;
4287 +       si_read_lock(sb, AuLock_FLUSH);
4288 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4289 +               goto out_unlock;
4290 +
4291 +       err = 0;
4292 +       ibusy.h_ino = 0; /* invalid */
4293 +       inode = ilookup(sb, ibusy.ino);
4294 +       if (!inode
4295 +           || inode->i_ino == AUFS_ROOT_INO
4296 +           || au_is_bad_inode(inode))
4297 +               goto out_unlock;
4298 +
4299 +       ii_read_lock_child(inode);
4300 +       btop = au_ibtop(inode);
4301 +       bbot = au_ibbot(inode);
4302 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4303 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4304 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4305 +                       ibusy.h_ino = h_inode->i_ino;
4306 +       }
4307 +       ii_read_unlock(inode);
4308 +       iput(inode);
4309 +
4310 +out_unlock:
4311 +       si_read_unlock(sb);
4312 +       if (!err) {
4313 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4314 +               if (unlikely(err)) {
4315 +                       err = -EFAULT;
4316 +                       AuTraceErr(err);
4317 +               }
4318 +       }
4319 +out:
4320 +       return err;
4321 +}
4322 +
4323 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4324 +{
4325 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4326 +}
4327 +
4328 +#ifdef CONFIG_COMPAT
4329 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4330 +{
4331 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4332 +}
4333 +#endif
4334 +
4335 +/* ---------------------------------------------------------------------- */
4336 +
4337 +/*
4338 + * change a branch permission
4339 + */
4340 +
4341 +static void au_warn_ima(void)
4342 +{
4343 +#ifdef CONFIG_IMA
4344 +       /* since it doesn't support mark_files_ro() */
4345 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4346 +#endif
4347 +}
4348 +
4349 +static int do_need_sigen_inc(int a, int b)
4350 +{
4351 +       return au_br_whable(a) && !au_br_whable(b);
4352 +}
4353 +
4354 +static int need_sigen_inc(int old, int new)
4355 +{
4356 +       return do_need_sigen_inc(old, new)
4357 +               || do_need_sigen_inc(new, old);
4358 +}
4359 +
4360 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4361 +{
4362 +       int err, do_warn;
4363 +       unsigned int mnt_flags;
4364 +       unsigned long long ull, max;
4365 +       aufs_bindex_t br_id;
4366 +       unsigned char verbose, writer;
4367 +       struct file *file, *hf, **array;
4368 +       struct au_hfile *hfile;
4369 +
4370 +       mnt_flags = au_mntflags(sb);
4371 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4372 +
4373 +       array = au_farray_alloc(sb, &max);
4374 +       err = PTR_ERR(array);
4375 +       if (IS_ERR(array))
4376 +               goto out;
4377 +
4378 +       do_warn = 0;
4379 +       br_id = au_sbr_id(sb, bindex);
4380 +       for (ull = 0; ull < max; ull++) {
4381 +               file = array[ull];
4382 +               if (unlikely(!file))
4383 +                       break;
4384 +
4385 +               /* AuDbg("%pD\n", file); */
4386 +               fi_read_lock(file);
4387 +               if (unlikely(au_test_mmapped(file))) {
4388 +                       err = -EBUSY;
4389 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4390 +                       AuDbgFile(file);
4391 +                       FiMustNoWaiters(file);
4392 +                       fi_read_unlock(file);
4393 +                       goto out_array;
4394 +               }
4395 +
4396 +               hfile = &au_fi(file)->fi_htop;
4397 +               hf = hfile->hf_file;
4398 +               if (!d_is_reg(file->f_path.dentry)
4399 +                   || !(file->f_mode & FMODE_WRITE)
4400 +                   || hfile->hf_br->br_id != br_id
4401 +                   || !(hf->f_mode & FMODE_WRITE))
4402 +                       array[ull] = NULL;
4403 +               else {
4404 +                       do_warn = 1;
4405 +                       get_file(file);
4406 +               }
4407 +
4408 +               FiMustNoWaiters(file);
4409 +               fi_read_unlock(file);
4410 +               fput(file);
4411 +       }
4412 +
4413 +       err = 0;
4414 +       if (do_warn)
4415 +               au_warn_ima();
4416 +
4417 +       for (ull = 0; ull < max; ull++) {
4418 +               file = array[ull];
4419 +               if (!file)
4420 +                       continue;
4421 +
4422 +               /* todo: already flushed? */
4423 +               /*
4424 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4425 +                * approach which resets f_mode and calls mnt_drop_write() and
4426 +                * file_release_write() for each file, because the branch
4427 +                * attribute in aufs world is totally different from the native
4428 +                * fs rw/ro mode.
4429 +               */
4430 +               /* fi_read_lock(file); */
4431 +               hfile = &au_fi(file)->fi_htop;
4432 +               hf = hfile->hf_file;
4433 +               /* fi_read_unlock(file); */
4434 +               spin_lock(&hf->f_lock);
4435 +               writer = !!(hf->f_mode & FMODE_WRITER);
4436 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4437 +               spin_unlock(&hf->f_lock);
4438 +               if (writer) {
4439 +                       put_write_access(file_inode(hf));
4440 +                       __mnt_drop_write(hf->f_path.mnt);
4441 +               }
4442 +       }
4443 +
4444 +out_array:
4445 +       au_farray_free(array, max);
4446 +out:
4447 +       AuTraceErr(err);
4448 +       return err;
4449 +}
4450 +
4451 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4452 +             int *do_refresh)
4453 +{
4454 +       int err, rerr;
4455 +       aufs_bindex_t bindex;
4456 +       struct dentry *root;
4457 +       struct au_branch *br;
4458 +       struct au_br_fhsm *bf;
4459 +
4460 +       root = sb->s_root;
4461 +       bindex = au_find_dbindex(root, mod->h_root);
4462 +       if (bindex < 0) {
4463 +               if (remount)
4464 +                       return 0; /* success */
4465 +               err = -ENOENT;
4466 +               pr_err("%s no such branch\n", mod->path);
4467 +               goto out;
4468 +       }
4469 +       AuDbg("bindex b%d\n", bindex);
4470 +
4471 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4472 +       if (unlikely(err))
4473 +               goto out;
4474 +
4475 +       br = au_sbr(sb, bindex);
4476 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4477 +       if (br->br_perm == mod->perm)
4478 +               return 0; /* success */
4479 +
4480 +       /* pre-allocate for non-fhsm --> fhsm */
4481 +       bf = NULL;
4482 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4483 +               err = au_fhsm_br_alloc(br);
4484 +               if (unlikely(err))
4485 +                       goto out;
4486 +               bf = br->br_fhsm;
4487 +               br->br_fhsm = NULL;
4488 +       }
4489 +
4490 +       if (au_br_writable(br->br_perm)) {
4491 +               /* remove whiteout base */
4492 +               err = au_br_init_wh(sb, br, mod->perm);
4493 +               if (unlikely(err))
4494 +                       goto out_bf;
4495 +
4496 +               if (!au_br_writable(mod->perm)) {
4497 +                       /* rw --> ro, file might be mmapped */
4498 +                       DiMustNoWaiters(root);
4499 +                       IiMustNoWaiters(d_inode(root));
4500 +                       di_write_unlock(root);
4501 +                       err = au_br_mod_files_ro(sb, bindex);
4502 +                       /* aufs_write_lock() calls ..._child() */
4503 +                       di_write_lock_child(root);
4504 +
4505 +                       if (unlikely(err)) {
4506 +                               rerr = -ENOMEM;
4507 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4508 +                                                    GFP_NOFS);
4509 +                               if (br->br_wbr)
4510 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4511 +                               if (unlikely(rerr)) {
4512 +                                       AuIOErr("nested error %d (%d)\n",
4513 +                                               rerr, err);
4514 +                                       br->br_perm = mod->perm;
4515 +                               }
4516 +                       }
4517 +               }
4518 +       } else if (au_br_writable(mod->perm)) {
4519 +               /* ro --> rw */
4520 +               err = -ENOMEM;
4521 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4522 +               if (br->br_wbr) {
4523 +                       err = au_wbr_init(br, sb, mod->perm);
4524 +                       if (unlikely(err)) {
4525 +                               kfree(br->br_wbr);
4526 +                               br->br_wbr = NULL;
4527 +                       }
4528 +               }
4529 +       }
4530 +       if (unlikely(err))
4531 +               goto out_bf;
4532 +
4533 +       if (au_br_fhsm(br->br_perm)) {
4534 +               if (!au_br_fhsm(mod->perm)) {
4535 +                       /* fhsm --> non-fhsm */
4536 +                       au_br_fhsm_fin(br->br_fhsm);
4537 +                       kfree(br->br_fhsm);
4538 +                       br->br_fhsm = NULL;
4539 +               }
4540 +       } else if (au_br_fhsm(mod->perm))
4541 +               /* non-fhsm --> fhsm */
4542 +               br->br_fhsm = bf;
4543 +
4544 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4545 +       br->br_perm = mod->perm;
4546 +       goto out; /* success */
4547 +
4548 +out_bf:
4549 +       kfree(bf);
4550 +out:
4551 +       AuTraceErr(err);
4552 +       return err;
4553 +}
4554 +
4555 +/* ---------------------------------------------------------------------- */
4556 +
4557 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4558 +{
4559 +       int err;
4560 +       struct kstatfs kstfs;
4561 +
4562 +       err = vfs_statfs(&br->br_path, &kstfs);
4563 +       if (!err) {
4564 +               stfs->f_blocks = kstfs.f_blocks;
4565 +               stfs->f_bavail = kstfs.f_bavail;
4566 +               stfs->f_files = kstfs.f_files;
4567 +               stfs->f_ffree = kstfs.f_ffree;
4568 +       }
4569 +
4570 +       return err;
4571 +}
4572 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4573 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4574 +++ linux/fs/aufs/branch.h      2018-07-19 09:46:13.053314374 +0200
4575 @@ -0,0 +1,374 @@
4576 +/* SPDX-License-Identifier: GPL-2.0 */
4577 +/*
4578 + * Copyright (C) 2005-2018 Junjiro R. Okajima
4579 + *
4580 + * This program, aufs is free software; you can redistribute it and/or modify
4581 + * it under the terms of the GNU General Public License as published by
4582 + * the Free Software Foundation; either version 2 of the License, or
4583 + * (at your option) any later version.
4584 + *
4585 + * This program is distributed in the hope that it will be useful,
4586 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4587 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4588 + * GNU General Public License for more details.
4589 + *
4590 + * You should have received a copy of the GNU General Public License
4591 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4592 + */
4593 +
4594 +/*
4595 + * branch filesystems and xino for them
4596 + */
4597 +
4598 +#ifndef __AUFS_BRANCH_H__
4599 +#define __AUFS_BRANCH_H__
4600 +
4601 +#ifdef __KERNEL__
4602 +
4603 +#include <linux/mount.h>
4604 +#include "dirren.h"
4605 +#include "dynop.h"
4606 +#include "rwsem.h"
4607 +#include "super.h"
4608 +
4609 +/* ---------------------------------------------------------------------- */
4610 +
4611 +/* a xino file */
4612 +struct au_xino {
4613 +       struct file             *xi_file;
4614 +       struct {
4615 +               spinlock_t              spin;
4616 +               ino_t                   *array;
4617 +               int                     total;
4618 +               /* reserved for future use */
4619 +               /* unsigned long        *bitmap; */
4620 +               wait_queue_head_t       wqh;
4621 +       } xi_nondir;
4622 +
4623 +       atomic_t                xi_truncating;
4624 +
4625 +       struct kref             xi_kref;
4626 +
4627 +       /* todo: make xino files an array to support huge inode number */
4628 +};
4629 +
4630 +/* File-based Hierarchical Storage Management */
4631 +struct au_br_fhsm {
4632 +#ifdef CONFIG_AUFS_FHSM
4633 +       struct mutex            bf_lock;
4634 +       unsigned long           bf_jiffy;
4635 +       struct aufs_stfs        bf_stfs;
4636 +       int                     bf_readable;
4637 +#endif
4638 +};
4639 +
4640 +/* members for writable branch only */
4641 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4642 +struct au_wbr {
4643 +       struct au_rwsem         wbr_wh_rwsem;
4644 +       struct dentry           *wbr_wh[AuBrWh_Last];
4645 +       atomic_t                wbr_wh_running;
4646 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4647 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4648 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4649 +
4650 +       /* mfs mode */
4651 +       unsigned long long      wbr_bytes;
4652 +};
4653 +
4654 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4655 +#define AuBrDynOp (AuDyLast * 4)
4656 +
4657 +#ifdef CONFIG_AUFS_HFSNOTIFY
4658 +/* support for asynchronous destruction */
4659 +struct au_br_hfsnotify {
4660 +       struct fsnotify_group   *hfsn_group;
4661 +};
4662 +#endif
4663 +
4664 +/* sysfs entries */
4665 +struct au_brsysfs {
4666 +       char                    name[16];
4667 +       struct attribute        attr;
4668 +};
4669 +
4670 +enum {
4671 +       AuBrSysfs_BR,
4672 +       AuBrSysfs_BRID,
4673 +       AuBrSysfs_Last
4674 +};
4675 +
4676 +/* protected by superblock rwsem */
4677 +struct au_branch {
4678 +       struct au_xino          *br_xino;
4679 +
4680 +       aufs_bindex_t           br_id;
4681 +
4682 +       int                     br_perm;
4683 +       struct path             br_path;
4684 +       spinlock_t              br_dykey_lock;
4685 +       struct au_dykey         *br_dykey[AuBrDynOp];
4686 +       struct percpu_counter   br_count;
4687 +
4688 +       struct au_wbr           *br_wbr;
4689 +       struct au_br_fhsm       *br_fhsm;
4690 +
4691 +#ifdef CONFIG_AUFS_HFSNOTIFY
4692 +       struct au_br_hfsnotify  *br_hfsn;
4693 +#endif
4694 +
4695 +#ifdef CONFIG_SYSFS
4696 +       /* entries under sysfs per mount-point */
4697 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4698 +#endif
4699 +
4700 +#ifdef CONFIG_DEBUG_FS
4701 +       struct dentry            *br_dbgaufs; /* xino */
4702 +#endif
4703 +
4704 +       struct au_dr_br         br_dirren;
4705 +};
4706 +
4707 +/* ---------------------------------------------------------------------- */
4708 +
4709 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4710 +{
4711 +       return br->br_path.mnt;
4712 +}
4713 +
4714 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4715 +{
4716 +       return br->br_path.dentry;
4717 +}
4718 +
4719 +static inline struct super_block *au_br_sb(struct au_branch *br)
4720 +{
4721 +       return au_br_mnt(br)->mnt_sb;
4722 +}
4723 +
4724 +static inline void au_br_get(struct au_branch *br)
4725 +{
4726 +       percpu_counter_inc(&br->br_count);
4727 +}
4728 +
4729 +static inline void au_br_put(struct au_branch *br)
4730 +{
4731 +       percpu_counter_dec(&br->br_count);
4732 +}
4733 +
4734 +static inline s64 au_br_count(struct au_branch *br)
4735 +{
4736 +       return percpu_counter_sum(&br->br_count);
4737 +}
4738 +
4739 +static inline void au_br_count_init(struct au_branch *br)
4740 +{
4741 +       percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4742 +}
4743 +
4744 +static inline void au_br_count_fin(struct au_branch *br)
4745 +{
4746 +       percpu_counter_destroy(&br->br_count);
4747 +}
4748 +
4749 +static inline int au_br_rdonly(struct au_branch *br)
4750 +{
4751 +       return (sb_rdonly(au_br_sb(br))
4752 +               || !au_br_writable(br->br_perm))
4753 +               ? -EROFS : 0;
4754 +}
4755 +
4756 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4757 +{
4758 +#ifdef CONFIG_AUFS_HNOTIFY
4759 +       return !(brperm & AuBrPerm_RR);
4760 +#else
4761 +       return 0;
4762 +#endif
4763 +}
4764 +
4765 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4766 +{
4767 +       int err, exec_flag;
4768 +
4769 +       err = 0;
4770 +       exec_flag = oflag & __FMODE_EXEC;
4771 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4772 +               err = -EACCES;
4773 +
4774 +       return err;
4775 +}
4776 +
4777 +static inline void au_xino_get(struct au_branch *br)
4778 +{
4779 +       struct au_xino *xi;
4780 +
4781 +       xi = br->br_xino;
4782 +       if (xi)
4783 +               kref_get(&xi->xi_kref);
4784 +}
4785 +
4786 +static inline int au_xino_count(struct au_branch *br)
4787 +{
4788 +       int v;
4789 +       struct au_xino *xi;
4790 +
4791 +       v = 0;
4792 +       xi = br->br_xino;
4793 +       if (xi)
4794 +               v = kref_read(&xi->xi_kref);
4795 +
4796 +       return v;
4797 +}
4798 +
4799 +static inline struct file *au_xino_file(struct au_branch *br)
4800 +{
4801 +       struct au_xino *xi;
4802 +
4803 +       xi = br->br_xino;
4804 +       return xi ? xi->xi_file : NULL;
4805 +}
4806 +
4807 +/* ---------------------------------------------------------------------- */
4808 +
4809 +/* branch.c */
4810 +struct au_sbinfo;
4811 +void au_br_free(struct au_sbinfo *sinfo);
4812 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4813 +struct au_opt_add;
4814 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4815 +struct au_opt_del;
4816 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4817 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4818 +#ifdef CONFIG_COMPAT
4819 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4820 +#endif
4821 +struct au_opt_mod;
4822 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4823 +             int *do_refresh);
4824 +struct aufs_stfs;
4825 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4826 +
4827 +/* xino.c */
4828 +static const loff_t au_loff_max = LLONG_MAX;
4829 +
4830 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4831 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4832 +                            struct file *copy_src);
4833 +
4834 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4835 +                ino_t *ino);
4836 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4837 +                 ino_t ino);
4838 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4839 +                  loff_t *pos);
4840 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4841 +                   size_t size, loff_t *pos);
4842 +
4843 +int au_xib_trunc(struct super_block *sb);
4844 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4845 +
4846 +struct au_xino *au_xino_alloc(void);
4847 +int au_xino_put(struct au_branch *br);
4848 +void au_xino_file_set(struct au_branch *br, struct file *file);
4849 +
4850 +struct au_opt_xino;
4851 +void au_xino_clr(struct super_block *sb);
4852 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4853 +struct file *au_xino_def(struct super_block *sb);
4854 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4855 +                   struct path *base);
4856 +
4857 +ino_t au_xino_new_ino(struct super_block *sb);
4858 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4859 +
4860 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4861 +                      ino_t h_ino, int idx);
4862 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4863 +                     int *idx);
4864 +
4865 +int au_xino_path(struct seq_file *seq, struct file *file);
4866 +
4867 +/* ---------------------------------------------------------------------- */
4868 +
4869 +/* Superblock to branch */
4870 +static inline
4871 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4872 +{
4873 +       return au_sbr(sb, bindex)->br_id;
4874 +}
4875 +
4876 +static inline
4877 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4878 +{
4879 +       return au_br_mnt(au_sbr(sb, bindex));
4880 +}
4881 +
4882 +static inline
4883 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4884 +{
4885 +       return au_br_sb(au_sbr(sb, bindex));
4886 +}
4887 +
4888 +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4889 +{
4890 +       au_br_get(au_sbr(sb, bindex));
4891 +}
4892 +
4893 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4894 +{
4895 +       au_br_put(au_sbr(sb, bindex));
4896 +}
4897 +
4898 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4899 +{
4900 +       return au_sbr(sb, bindex)->br_perm;
4901 +}
4902 +
4903 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4904 +{
4905 +       return au_br_whable(au_sbr_perm(sb, bindex));
4906 +}
4907 +
4908 +/* ---------------------------------------------------------------------- */
4909 +
4910 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4911 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4912 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4913 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4914 +/*
4915 +#define wbr_wh_read_trylock_nested(wbr) \
4916 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4917 +#define wbr_wh_write_trylock_nested(wbr) \
4918 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4919 +*/
4920 +
4921 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4922 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4923 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4924 +
4925 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4926 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4927 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4928 +
4929 +/* ---------------------------------------------------------------------- */
4930 +
4931 +#ifdef CONFIG_AUFS_FHSM
4932 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4933 +{
4934 +       mutex_init(&brfhsm->bf_lock);
4935 +       brfhsm->bf_jiffy = 0;
4936 +       brfhsm->bf_readable = 0;
4937 +}
4938 +
4939 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4940 +{
4941 +       mutex_destroy(&brfhsm->bf_lock);
4942 +}
4943 +#else
4944 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4945 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4946 +#endif
4947 +
4948 +#endif /* __KERNEL__ */
4949 +#endif /* __AUFS_BRANCH_H__ */
4950 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4951 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4952 +++ linux/fs/aufs/conf.mk       2018-06-04 09:08:09.181412645 +0200
4953 @@ -0,0 +1,40 @@
4954 +# SPDX-License-Identifier: GPL-2.0
4955 +
4956 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4957 +
4958 +define AuConf
4959 +ifdef ${1}
4960 +AuConfStr += ${1}=${${1}}
4961 +endif
4962 +endef
4963 +
4964 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4965 +       SBILIST \
4966 +       HNOTIFY HFSNOTIFY \
4967 +       EXPORT INO_T_64 \
4968 +       XATTR \
4969 +       FHSM \
4970 +       RDU \
4971 +       DIRREN \
4972 +       SHWH \
4973 +       BR_RAMFS \
4974 +       BR_FUSE POLL \
4975 +       BR_HFSPLUS \
4976 +       BDEV_LOOP \
4977 +       DEBUG MAGIC_SYSRQ
4978 +$(foreach i, ${AuConfAll}, \
4979 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4980 +
4981 +AuConfName = ${obj}/conf.str
4982 +${AuConfName}.tmp: FORCE
4983 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4984 +${AuConfName}: ${AuConfName}.tmp
4985 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4986 +       echo '  GEN    ' $@; \
4987 +       cp -p $< $@; \
4988 +       }
4989 +FORCE:
4990 +clean-files += ${AuConfName} ${AuConfName}.tmp
4991 +${obj}/sysfs.o: ${AuConfName}
4992 +
4993 +-include ${srctree}/${src}/conf_priv.mk
4994 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4995 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4996 +++ linux/fs/aufs/cpup.c        2018-07-19 09:46:13.056647808 +0200
4997 @@ -0,0 +1,1442 @@
4998 +/* SPDX-License-Identifier: GPL-2.0 */
4999 +/*
5000 + * Copyright (C) 2005-2018 Junjiro R. Okajima
5001 + *
5002 + * This program, aufs is free software; you can redistribute it and/or modify
5003 + * it under the terms of the GNU General Public License as published by
5004 + * the Free Software Foundation; either version 2 of the License, or
5005 + * (at your option) any later version.
5006 + *
5007 + * This program is distributed in the hope that it will be useful,
5008 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5009 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5010 + * GNU General Public License for more details.
5011 + *
5012 + * You should have received a copy of the GNU General Public License
5013 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5014 + */
5015 +
5016 +/*
5017 + * copy-up functions, see wbr_policy.c for copy-down
5018 + */
5019 +
5020 +#include <linux/fs_stack.h>
5021 +#include <linux/mm.h>
5022 +#include <linux/task_work.h>
5023 +#include "aufs.h"
5024 +
5025 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
5026 +{
5027 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
5028 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
5029 +
5030 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
5031 +
5032 +       dst->i_flags |= iflags & ~mask;
5033 +       if (au_test_fs_notime(dst->i_sb))
5034 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
5035 +}
5036 +
5037 +void au_cpup_attr_timesizes(struct inode *inode)
5038 +{
5039 +       struct inode *h_inode;
5040 +
5041 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5042 +       fsstack_copy_attr_times(inode, h_inode);
5043 +       fsstack_copy_inode_size(inode, h_inode);
5044 +}
5045 +
5046 +void au_cpup_attr_nlink(struct inode *inode, int force)
5047 +{
5048 +       struct inode *h_inode;
5049 +       struct super_block *sb;
5050 +       aufs_bindex_t bindex, bbot;
5051 +
5052 +       sb = inode->i_sb;
5053 +       bindex = au_ibtop(inode);
5054 +       h_inode = au_h_iptr(inode, bindex);
5055 +       if (!force
5056 +           && !S_ISDIR(h_inode->i_mode)
5057 +           && au_opt_test(au_mntflags(sb), PLINK)
5058 +           && au_plink_test(inode))
5059 +               return;
5060 +
5061 +       /*
5062 +        * 0 can happen in revalidating.
5063 +        * h_inode->i_mutex may not be held here, but it is harmless since once
5064 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
5065 +        * case.
5066 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5067 +        *       the incorrect link count.
5068 +        */
5069 +       set_nlink(inode, h_inode->i_nlink);
5070 +
5071 +       /*
5072 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5073 +        * it may includes whplink directory.
5074 +        */
5075 +       if (S_ISDIR(h_inode->i_mode)) {
5076 +               bbot = au_ibbot(inode);
5077 +               for (bindex++; bindex <= bbot; bindex++) {
5078 +                       h_inode = au_h_iptr(inode, bindex);
5079 +                       if (h_inode)
5080 +                               au_add_nlink(inode, h_inode);
5081 +               }
5082 +       }
5083 +}
5084 +
5085 +void au_cpup_attr_changeable(struct inode *inode)
5086 +{
5087 +       struct inode *h_inode;
5088 +
5089 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5090 +       inode->i_mode = h_inode->i_mode;
5091 +       inode->i_uid = h_inode->i_uid;
5092 +       inode->i_gid = h_inode->i_gid;
5093 +       au_cpup_attr_timesizes(inode);
5094 +       au_cpup_attr_flags(inode, h_inode->i_flags);
5095 +}
5096 +
5097 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5098 +{
5099 +       struct au_iinfo *iinfo = au_ii(inode);
5100 +
5101 +       IiMustWriteLock(inode);
5102 +
5103 +       iinfo->ii_higen = h_inode->i_generation;
5104 +       iinfo->ii_hsb1 = h_inode->i_sb;
5105 +}
5106 +
5107 +void au_cpup_attr_all(struct inode *inode, int force)
5108 +{
5109 +       struct inode *h_inode;
5110 +
5111 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5112 +       au_cpup_attr_changeable(inode);
5113 +       if (inode->i_nlink > 0)
5114 +               au_cpup_attr_nlink(inode, force);
5115 +       inode->i_rdev = h_inode->i_rdev;
5116 +       inode->i_blkbits = h_inode->i_blkbits;
5117 +       au_cpup_igen(inode, h_inode);
5118 +}
5119 +
5120 +/* ---------------------------------------------------------------------- */
5121 +
5122 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5123 +
5124 +/* keep the timestamps of the parent dir when cpup */
5125 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5126 +                   struct path *h_path)
5127 +{
5128 +       struct inode *h_inode;
5129 +
5130 +       dt->dt_dentry = dentry;
5131 +       dt->dt_h_path = *h_path;
5132 +       h_inode = d_inode(h_path->dentry);
5133 +       dt->dt_atime = h_inode->i_atime;
5134 +       dt->dt_mtime = h_inode->i_mtime;
5135 +       /* smp_mb(); */
5136 +}
5137 +
5138 +void au_dtime_revert(struct au_dtime *dt)
5139 +{
5140 +       struct iattr attr;
5141 +       int err;
5142 +
5143 +       attr.ia_atime = dt->dt_atime;
5144 +       attr.ia_mtime = dt->dt_mtime;
5145 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5146 +               | ATTR_ATIME | ATTR_ATIME_SET;
5147 +
5148 +       /* no delegation since this is a directory */
5149 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5150 +       if (unlikely(err))
5151 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5152 +}
5153 +
5154 +/* ---------------------------------------------------------------------- */
5155 +
5156 +/* internal use only */
5157 +struct au_cpup_reg_attr {
5158 +       int             valid;
5159 +       struct kstat    st;
5160 +       unsigned int    iflags; /* inode->i_flags */
5161 +};
5162 +
5163 +static noinline_for_stack
5164 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5165 +              struct au_cpup_reg_attr *h_src_attr)
5166 +{
5167 +       int err, sbits, icex;
5168 +       unsigned int mnt_flags;
5169 +       unsigned char verbose;
5170 +       struct iattr ia;
5171 +       struct path h_path;
5172 +       struct inode *h_isrc, *h_idst;
5173 +       struct kstat *h_st;
5174 +       struct au_branch *br;
5175 +
5176 +       h_path.dentry = au_h_dptr(dst, bindex);
5177 +       h_idst = d_inode(h_path.dentry);
5178 +       br = au_sbr(dst->d_sb, bindex);
5179 +       h_path.mnt = au_br_mnt(br);
5180 +       h_isrc = d_inode(h_src);
5181 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5182 +               | ATTR_ATIME | ATTR_MTIME
5183 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5184 +       if (h_src_attr && h_src_attr->valid) {
5185 +               h_st = &h_src_attr->st;
5186 +               ia.ia_uid = h_st->uid;
5187 +               ia.ia_gid = h_st->gid;
5188 +               ia.ia_atime = h_st->atime;
5189 +               ia.ia_mtime = h_st->mtime;
5190 +               if (h_idst->i_mode != h_st->mode
5191 +                   && !S_ISLNK(h_idst->i_mode)) {
5192 +                       ia.ia_valid |= ATTR_MODE;
5193 +                       ia.ia_mode = h_st->mode;
5194 +               }
5195 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5196 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5197 +       } else {
5198 +               ia.ia_uid = h_isrc->i_uid;
5199 +               ia.ia_gid = h_isrc->i_gid;
5200 +               ia.ia_atime = h_isrc->i_atime;
5201 +               ia.ia_mtime = h_isrc->i_mtime;
5202 +               if (h_idst->i_mode != h_isrc->i_mode
5203 +                   && !S_ISLNK(h_idst->i_mode)) {
5204 +                       ia.ia_valid |= ATTR_MODE;
5205 +                       ia.ia_mode = h_isrc->i_mode;
5206 +               }
5207 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5208 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5209 +       }
5210 +       /* no delegation since it is just created */
5211 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5212 +
5213 +       /* is this nfs only? */
5214 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5215 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5216 +               ia.ia_mode = h_isrc->i_mode;
5217 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5218 +       }
5219 +
5220 +       icex = br->br_perm & AuBrAttr_ICEX;
5221 +       if (!err) {
5222 +               mnt_flags = au_mntflags(dst->d_sb);
5223 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5224 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5225 +       }
5226 +
5227 +       return err;
5228 +}
5229 +
5230 +/* ---------------------------------------------------------------------- */
5231 +
5232 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5233 +                          char *buf, unsigned long blksize)
5234 +{
5235 +       int err;
5236 +       size_t sz, rbytes, wbytes;
5237 +       unsigned char all_zero;
5238 +       char *p, *zp;
5239 +       struct inode *h_inode;
5240 +       /* reduce stack usage */
5241 +       struct iattr *ia;
5242 +
5243 +       zp = page_address(ZERO_PAGE(0));
5244 +       if (unlikely(!zp))
5245 +               return -ENOMEM; /* possible? */
5246 +
5247 +       err = 0;
5248 +       all_zero = 0;
5249 +       while (len) {
5250 +               AuDbg("len %lld\n", len);
5251 +               sz = blksize;
5252 +               if (len < blksize)
5253 +                       sz = len;
5254 +
5255 +               rbytes = 0;
5256 +               /* todo: signal_pending? */
5257 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5258 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5259 +                       err = rbytes;
5260 +               }
5261 +               if (unlikely(err < 0))
5262 +                       break;
5263 +
5264 +               all_zero = 0;
5265 +               if (len >= rbytes && rbytes == blksize)
5266 +                       all_zero = !memcmp(buf, zp, rbytes);
5267 +               if (!all_zero) {
5268 +                       wbytes = rbytes;
5269 +                       p = buf;
5270 +                       while (wbytes) {
5271 +                               size_t b;
5272 +
5273 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5274 +                               err = b;
5275 +                               /* todo: signal_pending? */
5276 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5277 +                                       continue;
5278 +                               if (unlikely(err < 0))
5279 +                                       break;
5280 +                               wbytes -= b;
5281 +                               p += b;
5282 +                       }
5283 +                       if (unlikely(err < 0))
5284 +                               break;
5285 +               } else {
5286 +                       loff_t res;
5287 +
5288 +                       AuLabel(hole);
5289 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5290 +                       err = res;
5291 +                       if (unlikely(res < 0))
5292 +                               break;
5293 +               }
5294 +               len -= rbytes;
5295 +               err = 0;
5296 +       }
5297 +
5298 +       /* the last block may be a hole */
5299 +       if (!err && all_zero) {
5300 +               AuLabel(last hole);
5301 +
5302 +               err = 1;
5303 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5304 +                       /* nfs requires this step to make last hole */
5305 +                       /* is this only nfs? */
5306 +                       do {
5307 +                               /* todo: signal_pending? */
5308 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5309 +                       } while (err == -EAGAIN || err == -EINTR);
5310 +                       if (err == 1)
5311 +                               dst->f_pos--;
5312 +               }
5313 +
5314 +               if (err == 1) {
5315 +                       ia = (void *)buf;
5316 +                       ia->ia_size = dst->f_pos;
5317 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5318 +                       ia->ia_file = dst;
5319 +                       h_inode = file_inode(dst);
5320 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5321 +                       /* no delegation since it is just created */
5322 +                       err = vfsub_notify_change(&dst->f_path, ia,
5323 +                                                 /*delegated*/NULL);
5324 +                       inode_unlock(h_inode);
5325 +               }
5326 +       }
5327 +
5328 +       return err;
5329 +}
5330 +
5331 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5332 +{
5333 +       int err;
5334 +       unsigned long blksize;
5335 +       unsigned char do_kfree;
5336 +       char *buf;
5337 +
5338 +       err = -ENOMEM;
5339 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
5340 +       if (!blksize || PAGE_SIZE < blksize)
5341 +               blksize = PAGE_SIZE;
5342 +       AuDbg("blksize %lu\n", blksize);
5343 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5344 +       if (do_kfree)
5345 +               buf = kmalloc(blksize, GFP_NOFS);
5346 +       else
5347 +               buf = (void *)__get_free_page(GFP_NOFS);
5348 +       if (unlikely(!buf))
5349 +               goto out;
5350 +
5351 +       if (len > (1 << 22))
5352 +               AuDbg("copying a large file %lld\n", (long long)len);
5353 +
5354 +       src->f_pos = 0;
5355 +       dst->f_pos = 0;
5356 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5357 +       if (do_kfree)
5358 +               kfree(buf);
5359 +       else
5360 +               free_page((unsigned long)buf);
5361 +
5362 +out:
5363 +       return err;
5364 +}
5365 +
5366 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5367 +{
5368 +       int err;
5369 +       struct super_block *h_src_sb;
5370 +       struct inode *h_src_inode;
5371 +
5372 +       h_src_inode = file_inode(src);
5373 +       h_src_sb = h_src_inode->i_sb;
5374 +
5375 +       /* XFS acquires inode_lock */
5376 +       if (!au_test_xfs(h_src_sb))
5377 +               err = au_copy_file(dst, src, len);
5378 +       else {
5379 +               inode_unlock_shared(h_src_inode);
5380 +               err = au_copy_file(dst, src, len);
5381 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5382 +       }
5383 +
5384 +       return err;
5385 +}
5386 +
5387 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5388 +{
5389 +       int err;
5390 +       struct super_block *h_src_sb;
5391 +       struct inode *h_src_inode;
5392 +
5393 +       h_src_inode = file_inode(src);
5394 +       h_src_sb = h_src_inode->i_sb;
5395 +       if (h_src_sb != file_inode(dst)->i_sb
5396 +           || !dst->f_op->clone_file_range) {
5397 +               err = au_do_copy(dst, src, len);
5398 +               goto out;
5399 +       }
5400 +
5401 +       if (!au_test_nfs(h_src_sb)) {
5402 +               inode_unlock_shared(h_src_inode);
5403 +               err = vfsub_clone_file_range(src, dst, len);
5404 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5405 +       } else
5406 +               err = vfsub_clone_file_range(src, dst, len);
5407 +       /* older XFS has a condition in cloning */
5408 +       if (unlikely(err != -EOPNOTSUPP))
5409 +               goto out;
5410 +
5411 +       /* the backend fs on NFS may not support cloning */
5412 +       err = au_do_copy(dst, src, len);
5413 +
5414 +out:
5415 +       AuTraceErr(err);
5416 +       return err;
5417 +}
5418 +
5419 +/*
5420 + * to support a sparse file which is opened with O_APPEND,
5421 + * we need to close the file.
5422 + */
5423 +static int au_cp_regular(struct au_cp_generic *cpg)
5424 +{
5425 +       int err, i;
5426 +       enum { SRC, DST };
5427 +       struct {
5428 +               aufs_bindex_t bindex;
5429 +               unsigned int flags;
5430 +               struct dentry *dentry;
5431 +               int force_wr;
5432 +               struct file *file;
5433 +               void *label;
5434 +       } *f, file[] = {
5435 +               {
5436 +                       .bindex = cpg->bsrc,
5437 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5438 +                       .label = &&out
5439 +               },
5440 +               {
5441 +                       .bindex = cpg->bdst,
5442 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5443 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5444 +                       .label = &&out_src
5445 +               }
5446 +       };
5447 +       struct super_block *sb, *h_src_sb;
5448 +       struct inode *h_src_inode;
5449 +       struct task_struct *tsk = current;
5450 +
5451 +       /* bsrc branch can be ro/rw. */
5452 +       sb = cpg->dentry->d_sb;
5453 +       f = file;
5454 +       for (i = 0; i < 2; i++, f++) {
5455 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5456 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5457 +                                   /*file*/NULL, f->force_wr);
5458 +               err = PTR_ERR(f->file);
5459 +               if (IS_ERR(f->file))
5460 +                       goto *f->label;
5461 +       }
5462 +
5463 +       /* try stopping to update while we copyup */
5464 +       h_src_inode = d_inode(file[SRC].dentry);
5465 +       h_src_sb = h_src_inode->i_sb;
5466 +       if (!au_test_nfs(h_src_sb))
5467 +               IMustLock(h_src_inode);
5468 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5469 +
5470 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5471 +       if (tsk->flags & PF_KTHREAD)
5472 +               __fput_sync(file[DST].file);
5473 +       else {
5474 +               /* it happened actually */
5475 +               fput(file[DST].file);
5476 +               /*
5477 +                * too bad.
5478 +                * we have to call both since we don't know which place the file
5479 +                * was added to.
5480 +                */
5481 +               task_work_run();
5482 +               flush_delayed_fput();
5483 +       }
5484 +       au_sbr_put(sb, file[DST].bindex);
5485 +
5486 +out_src:
5487 +       fput(file[SRC].file);
5488 +       au_sbr_put(sb, file[SRC].bindex);
5489 +out:
5490 +       return err;
5491 +}
5492 +
5493 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5494 +                             struct au_cpup_reg_attr *h_src_attr)
5495 +{
5496 +       int err, rerr;
5497 +       loff_t l;
5498 +       struct path h_path;
5499 +       struct inode *h_src_inode, *h_dst_inode;
5500 +
5501 +       err = 0;
5502 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5503 +       l = i_size_read(h_src_inode);
5504 +       if (cpg->len == -1 || l < cpg->len)
5505 +               cpg->len = l;
5506 +       if (cpg->len) {
5507 +               /* try stopping to update while we are referencing */
5508 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5509 +               au_pin_hdir_unlock(cpg->pin);
5510 +
5511 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5512 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5513 +               h_src_attr->iflags = h_src_inode->i_flags;
5514 +               if (!au_test_nfs(h_src_inode->i_sb))
5515 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5516 +               else {
5517 +                       inode_unlock_shared(h_src_inode);
5518 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5519 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5520 +               }
5521 +               if (unlikely(err)) {
5522 +                       inode_unlock_shared(h_src_inode);
5523 +                       goto out;
5524 +               }
5525 +               h_src_attr->valid = 1;
5526 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5527 +                       err = au_cp_regular(cpg);
5528 +                       inode_unlock_shared(h_src_inode);
5529 +               } else {
5530 +                       inode_unlock_shared(h_src_inode);
5531 +                       err = au_cp_regular(cpg);
5532 +               }
5533 +               rerr = au_pin_hdir_relock(cpg->pin);
5534 +               if (!err && rerr)
5535 +                       err = rerr;
5536 +       }
5537 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5538 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5539 +               h_dst_inode = d_inode(h_path.dentry);
5540 +               spin_lock(&h_dst_inode->i_lock);
5541 +               h_dst_inode->i_state |= I_LINKABLE;
5542 +               spin_unlock(&h_dst_inode->i_lock);
5543 +       }
5544 +
5545 +out:
5546 +       return err;
5547 +}
5548 +
5549 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5550 +                             struct inode *h_dir)
5551 +{
5552 +       int err, symlen;
5553 +       mm_segment_t old_fs;
5554 +       union {
5555 +               char *k;
5556 +               char __user *u;
5557 +       } sym;
5558 +
5559 +       err = -ENOMEM;
5560 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5561 +       if (unlikely(!sym.k))
5562 +               goto out;
5563 +
5564 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5565 +       old_fs = get_fs();
5566 +       set_fs(KERNEL_DS);
5567 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5568 +       err = symlen;
5569 +       set_fs(old_fs);
5570 +
5571 +       if (symlen > 0) {
5572 +               sym.k[symlen] = 0;
5573 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5574 +       }
5575 +       free_page((unsigned long)sym.k);
5576 +
5577 +out:
5578 +       return err;
5579 +}
5580 +
5581 +/*
5582 + * regardless 'acl' option, reset all ACL.
5583 + * All ACL will be copied up later from the original entry on the lower branch.
5584 + */
5585 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5586 +{
5587 +       int err;
5588 +       struct dentry *h_dentry;
5589 +       struct inode *h_inode;
5590 +
5591 +       h_dentry = h_path->dentry;
5592 +       h_inode = d_inode(h_dentry);
5593 +       /* forget_all_cached_acls(h_inode)); */
5594 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5595 +       AuTraceErr(err);
5596 +       if (err == -EOPNOTSUPP)
5597 +               err = 0;
5598 +       if (!err)
5599 +               err = vfsub_acl_chmod(h_inode, mode);
5600 +
5601 +       AuTraceErr(err);
5602 +       return err;
5603 +}
5604 +
5605 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5606 +                         struct inode *h_dir, struct path *h_path)
5607 +{
5608 +       int err;
5609 +       struct inode *dir, *inode;
5610 +
5611 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5612 +       AuTraceErr(err);
5613 +       if (err == -EOPNOTSUPP)
5614 +               err = 0;
5615 +       if (unlikely(err))
5616 +               goto out;
5617 +
5618 +       /*
5619 +        * strange behaviour from the users view,
5620 +        * particularry setattr case
5621 +        */
5622 +       dir = d_inode(dst_parent);
5623 +       if (au_ibtop(dir) == cpg->bdst)
5624 +               au_cpup_attr_nlink(dir, /*force*/1);
5625 +       inode = d_inode(cpg->dentry);
5626 +       au_cpup_attr_nlink(inode, /*force*/1);
5627 +
5628 +out:
5629 +       return err;
5630 +}
5631 +
5632 +static noinline_for_stack
5633 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5634 +              struct au_cpup_reg_attr *h_src_attr)
5635 +{
5636 +       int err;
5637 +       umode_t mode;
5638 +       unsigned int mnt_flags;
5639 +       unsigned char isdir, isreg, force;
5640 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5641 +       struct au_dtime dt;
5642 +       struct path h_path;
5643 +       struct dentry *h_src, *h_dst, *h_parent;
5644 +       struct inode *h_inode, *h_dir;
5645 +       struct super_block *sb;
5646 +
5647 +       /* bsrc branch can be ro/rw. */
5648 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5649 +       h_inode = d_inode(h_src);
5650 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5651 +
5652 +       /* try stopping to be referenced while we are creating */
5653 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5654 +       if (au_ftest_cpup(cpg->flags, RENAME))
5655 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5656 +                                 AUFS_WH_PFX_LEN));
5657 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5658 +       h_dir = d_inode(h_parent);
5659 +       IMustLock(h_dir);
5660 +       AuDebugOn(h_parent != h_dst->d_parent);
5661 +
5662 +       sb = cpg->dentry->d_sb;
5663 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5664 +       if (do_dt) {
5665 +               h_path.dentry = h_parent;
5666 +               au_dtime_store(&dt, dst_parent, &h_path);
5667 +       }
5668 +       h_path.dentry = h_dst;
5669 +
5670 +       isreg = 0;
5671 +       isdir = 0;
5672 +       mode = h_inode->i_mode;
5673 +       switch (mode & S_IFMT) {
5674 +       case S_IFREG:
5675 +               isreg = 1;
5676 +               err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
5677 +                                  /*want_excl*/true);
5678 +               if (!err)
5679 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5680 +               break;
5681 +       case S_IFDIR:
5682 +               isdir = 1;
5683 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5684 +               if (!err)
5685 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5686 +               break;
5687 +       case S_IFLNK:
5688 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5689 +               break;
5690 +       case S_IFCHR:
5691 +       case S_IFBLK:
5692 +               AuDebugOn(!capable(CAP_MKNOD));
5693 +               /*FALLTHROUGH*/
5694 +       case S_IFIFO:
5695 +       case S_IFSOCK:
5696 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5697 +               break;
5698 +       default:
5699 +               AuIOErr("Unknown inode type 0%o\n", mode);
5700 +               err = -EIO;
5701 +       }
5702 +       if (!err)
5703 +               err = au_reset_acl(h_dir, &h_path, mode);
5704 +
5705 +       mnt_flags = au_mntflags(sb);
5706 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5707 +           && !isdir
5708 +           && au_opt_test(mnt_flags, XINO)
5709 +           && (h_inode->i_nlink == 1
5710 +               || (h_inode->i_state & I_LINKABLE))
5711 +           /* todo: unnecessary? */
5712 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5713 +           && cpg->bdst < cpg->bsrc
5714 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5715 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5716 +               /* ignore this error */
5717 +
5718 +       if (!err) {
5719 +               force = 0;
5720 +               if (isreg) {
5721 +                       force = !!cpg->len;
5722 +                       if (cpg->len == -1)
5723 +                               force = !!i_size_read(h_inode);
5724 +               }
5725 +               au_fhsm_wrote(sb, cpg->bdst, force);
5726 +       }
5727 +
5728 +       if (do_dt)
5729 +               au_dtime_revert(&dt);
5730 +       return err;
5731 +}
5732 +
5733 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5734 +{
5735 +       int err;
5736 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5737 +       struct inode *h_dir;
5738 +       aufs_bindex_t bdst;
5739 +
5740 +       dentry = cpg->dentry;
5741 +       bdst = cpg->bdst;
5742 +       h_dentry = au_h_dptr(dentry, bdst);
5743 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5744 +               dget(h_dentry);
5745 +               au_set_h_dptr(dentry, bdst, NULL);
5746 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5747 +               if (!err)
5748 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5749 +               au_set_h_dptr(dentry, bdst, h_dentry);
5750 +       } else {
5751 +               err = 0;
5752 +               parent = dget_parent(dentry);
5753 +               h_parent = au_h_dptr(parent, bdst);
5754 +               dput(parent);
5755 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5756 +               if (IS_ERR(h_path->dentry))
5757 +                       err = PTR_ERR(h_path->dentry);
5758 +       }
5759 +       if (unlikely(err))
5760 +               goto out;
5761 +
5762 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5763 +       h_dir = d_inode(h_parent);
5764 +       IMustLock(h_dir);
5765 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5766 +       /* no delegation since it is just created */
5767 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5768 +                          /*flags*/0);
5769 +       dput(h_path->dentry);
5770 +
5771 +out:
5772 +       return err;
5773 +}
5774 +
5775 +/*
5776 + * copyup the @dentry from @bsrc to @bdst.
5777 + * the caller must set the both of lower dentries.
5778 + * @len is for truncating when it is -1 copyup the entire file.
5779 + * in link/rename cases, @dst_parent may be different from the real one.
5780 + * basic->bsrc can be larger than basic->bdst.
5781 + * aufs doesn't touch the credential so
5782 + * security_inode_copy_up{,_xattr}() are unnecrssary.
5783 + */
5784 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5785 +{
5786 +       int err, rerr;
5787 +       aufs_bindex_t old_ibtop;
5788 +       unsigned char isdir, plink;
5789 +       struct dentry *h_src, *h_dst, *h_parent;
5790 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5791 +       struct super_block *sb;
5792 +       struct au_branch *br;
5793 +       /* to reuduce stack size */
5794 +       struct {
5795 +               struct au_dtime dt;
5796 +               struct path h_path;
5797 +               struct au_cpup_reg_attr h_src_attr;
5798 +       } *a;
5799 +
5800 +       err = -ENOMEM;
5801 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5802 +       if (unlikely(!a))
5803 +               goto out;
5804 +       a->h_src_attr.valid = 0;
5805 +
5806 +       sb = cpg->dentry->d_sb;
5807 +       br = au_sbr(sb, cpg->bdst);
5808 +       a->h_path.mnt = au_br_mnt(br);
5809 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5810 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5811 +       h_dir = d_inode(h_parent);
5812 +       IMustLock(h_dir);
5813 +
5814 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5815 +       inode = d_inode(cpg->dentry);
5816 +
5817 +       if (!dst_parent)
5818 +               dst_parent = dget_parent(cpg->dentry);
5819 +       else
5820 +               dget(dst_parent);
5821 +
5822 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5823 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5824 +       if (dst_inode) {
5825 +               if (unlikely(!plink)) {
5826 +                       err = -EIO;
5827 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5828 +                               "but plink is disabled\n",
5829 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5830 +                       goto out_parent;
5831 +               }
5832 +
5833 +               if (dst_inode->i_nlink) {
5834 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5835 +
5836 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5837 +                       err = PTR_ERR(h_src);
5838 +                       if (IS_ERR(h_src))
5839 +                               goto out_parent;
5840 +                       if (unlikely(d_is_negative(h_src))) {
5841 +                               err = -EIO;
5842 +                               AuIOErr("i%lu exists on b%d "
5843 +                                       "but not pseudo-linked\n",
5844 +                                       inode->i_ino, cpg->bdst);
5845 +                               dput(h_src);
5846 +                               goto out_parent;
5847 +                       }
5848 +
5849 +                       if (do_dt) {
5850 +                               a->h_path.dentry = h_parent;
5851 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5852 +                       }
5853 +
5854 +                       a->h_path.dentry = h_dst;
5855 +                       delegated = NULL;
5856 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5857 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5858 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5859 +                       if (do_dt)
5860 +                               au_dtime_revert(&a->dt);
5861 +                       if (unlikely(err == -EWOULDBLOCK)) {
5862 +                               pr_warn("cannot retry for NFSv4 delegation"
5863 +                                       " for an internal link\n");
5864 +                               iput(delegated);
5865 +                       }
5866 +                       dput(h_src);
5867 +                       goto out_parent;
5868 +               } else
5869 +                       /* todo: cpup_wh_file? */
5870 +                       /* udba work */
5871 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5872 +       }
5873 +
5874 +       isdir = S_ISDIR(inode->i_mode);
5875 +       old_ibtop = au_ibtop(inode);
5876 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5877 +       if (unlikely(err))
5878 +               goto out_rev;
5879 +       dst_inode = d_inode(h_dst);
5880 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5881 +       /* todo: necessary? */
5882 +       /* au_pin_hdir_unlock(cpg->pin); */
5883 +
5884 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5885 +       if (unlikely(err)) {
5886 +               /* todo: necessary? */
5887 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5888 +               inode_unlock(dst_inode);
5889 +               goto out_rev;
5890 +       }
5891 +
5892 +       if (cpg->bdst < old_ibtop) {
5893 +               if (S_ISREG(inode->i_mode)) {
5894 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5895 +                       if (unlikely(err)) {
5896 +                               /* ignore an error */
5897 +                               /* au_pin_hdir_relock(cpg->pin); */
5898 +                               inode_unlock(dst_inode);
5899 +                               goto out_rev;
5900 +                       }
5901 +               }
5902 +               au_set_ibtop(inode, cpg->bdst);
5903 +       } else
5904 +               au_set_ibbot(inode, cpg->bdst);
5905 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5906 +                     au_hi_flags(inode, isdir));
5907 +
5908 +       /* todo: necessary? */
5909 +       /* err = au_pin_hdir_relock(cpg->pin); */
5910 +       inode_unlock(dst_inode);
5911 +       if (unlikely(err))
5912 +               goto out_rev;
5913 +
5914 +       src_inode = d_inode(h_src);
5915 +       if (!isdir
5916 +           && (src_inode->i_nlink > 1
5917 +               || src_inode->i_state & I_LINKABLE)
5918 +           && plink)
5919 +               au_plink_append(inode, cpg->bdst, h_dst);
5920 +
5921 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5922 +               a->h_path.dentry = h_dst;
5923 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5924 +       }
5925 +       if (!err)
5926 +               goto out_parent; /* success */
5927 +
5928 +       /* revert */
5929 +out_rev:
5930 +       a->h_path.dentry = h_parent;
5931 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5932 +       a->h_path.dentry = h_dst;
5933 +       rerr = 0;
5934 +       if (d_is_positive(h_dst)) {
5935 +               if (!isdir) {
5936 +                       /* no delegation since it is just created */
5937 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5938 +                                           /*delegated*/NULL, /*force*/0);
5939 +               } else
5940 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5941 +       }
5942 +       au_dtime_revert(&a->dt);
5943 +       if (rerr) {
5944 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5945 +               err = -EIO;
5946 +       }
5947 +out_parent:
5948 +       dput(dst_parent);
5949 +       kfree(a);
5950 +out:
5951 +       return err;
5952 +}
5953 +
5954 +#if 0 /* reserved */
5955 +struct au_cpup_single_args {
5956 +       int *errp;
5957 +       struct au_cp_generic *cpg;
5958 +       struct dentry *dst_parent;
5959 +};
5960 +
5961 +static void au_call_cpup_single(void *args)
5962 +{
5963 +       struct au_cpup_single_args *a = args;
5964 +
5965 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5966 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5967 +       au_pin_hdir_release(a->cpg->pin);
5968 +}
5969 +#endif
5970 +
5971 +/*
5972 + * prevent SIGXFSZ in copy-up.
5973 + * testing CAP_MKNOD is for generic fs,
5974 + * but CAP_FSETID is for xfs only, currently.
5975 + */
5976 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5977 +{
5978 +       int do_sio;
5979 +       struct super_block *sb;
5980 +       struct inode *h_dir;
5981 +
5982 +       do_sio = 0;
5983 +       sb = au_pinned_parent(pin)->d_sb;
5984 +       if (!au_wkq_test()
5985 +           && (!au_sbi(sb)->si_plink_maint_pid
5986 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5987 +               switch (mode & S_IFMT) {
5988 +               case S_IFREG:
5989 +                       /* no condition about RLIMIT_FSIZE and the file size */
5990 +                       do_sio = 1;
5991 +                       break;
5992 +               case S_IFCHR:
5993 +               case S_IFBLK:
5994 +                       do_sio = !capable(CAP_MKNOD);
5995 +                       break;
5996 +               }
5997 +               if (!do_sio)
5998 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5999 +                                 && !capable(CAP_FSETID));
6000 +               /* this workaround may be removed in the future */
6001 +               if (!do_sio) {
6002 +                       h_dir = au_pinned_h_dir(pin);
6003 +                       do_sio = h_dir->i_mode & S_ISVTX;
6004 +               }
6005 +       }
6006 +
6007 +       return do_sio;
6008 +}
6009 +
6010 +#if 0 /* reserved */
6011 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
6012 +{
6013 +       int err, wkq_err;
6014 +       struct dentry *h_dentry;
6015 +
6016 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
6017 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
6018 +               err = au_cpup_single(cpg, dst_parent);
6019 +       else {
6020 +               struct au_cpup_single_args args = {
6021 +                       .errp           = &err,
6022 +                       .cpg            = cpg,
6023 +                       .dst_parent     = dst_parent
6024 +               };
6025 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6026 +               if (unlikely(wkq_err))
6027 +                       err = wkq_err;
6028 +       }
6029 +
6030 +       return err;
6031 +}
6032 +#endif
6033 +
6034 +/*
6035 + * copyup the @dentry from the first active lower branch to @bdst,
6036 + * using au_cpup_single().
6037 + */
6038 +static int au_cpup_simple(struct au_cp_generic *cpg)
6039 +{
6040 +       int err;
6041 +       unsigned int flags_orig;
6042 +       struct dentry *dentry;
6043 +
6044 +       AuDebugOn(cpg->bsrc < 0);
6045 +
6046 +       dentry = cpg->dentry;
6047 +       DiMustWriteLock(dentry);
6048 +
6049 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
6050 +       if (!err) {
6051 +               flags_orig = cpg->flags;
6052 +               au_fset_cpup(cpg->flags, RENAME);
6053 +               err = au_cpup_single(cpg, NULL);
6054 +               cpg->flags = flags_orig;
6055 +               if (!err)
6056 +                       return 0; /* success */
6057 +
6058 +               /* revert */
6059 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
6060 +               au_set_dbtop(dentry, cpg->bsrc);
6061 +       }
6062 +
6063 +       return err;
6064 +}
6065 +
6066 +struct au_cpup_simple_args {
6067 +       int *errp;
6068 +       struct au_cp_generic *cpg;
6069 +};
6070 +
6071 +static void au_call_cpup_simple(void *args)
6072 +{
6073 +       struct au_cpup_simple_args *a = args;
6074 +
6075 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6076 +       *a->errp = au_cpup_simple(a->cpg);
6077 +       au_pin_hdir_release(a->cpg->pin);
6078 +}
6079 +
6080 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
6081 +{
6082 +       int err, wkq_err;
6083 +       struct dentry *dentry, *parent;
6084 +       struct file *h_file;
6085 +       struct inode *h_dir;
6086 +
6087 +       dentry = cpg->dentry;
6088 +       h_file = NULL;
6089 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
6090 +               AuDebugOn(cpg->bsrc < 0);
6091 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
6092 +               err = PTR_ERR(h_file);
6093 +               if (IS_ERR(h_file))
6094 +                       goto out;
6095 +       }
6096 +
6097 +       parent = dget_parent(dentry);
6098 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
6099 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
6100 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6101 +               err = au_cpup_simple(cpg);
6102 +       else {
6103 +               struct au_cpup_simple_args args = {
6104 +                       .errp           = &err,
6105 +                       .cpg            = cpg
6106 +               };
6107 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6108 +               if (unlikely(wkq_err))
6109 +                       err = wkq_err;
6110 +       }
6111 +
6112 +       dput(parent);
6113 +       if (h_file)
6114 +               au_h_open_post(dentry, cpg->bsrc, h_file);
6115 +
6116 +out:
6117 +       return err;
6118 +}
6119 +
6120 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
6121 +{
6122 +       aufs_bindex_t bsrc, bbot;
6123 +       struct dentry *dentry, *h_dentry;
6124 +
6125 +       if (cpg->bsrc < 0) {
6126 +               dentry = cpg->dentry;
6127 +               bbot = au_dbbot(dentry);
6128 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6129 +                       h_dentry = au_h_dptr(dentry, bsrc);
6130 +                       if (h_dentry) {
6131 +                               AuDebugOn(d_is_negative(h_dentry));
6132 +                               break;
6133 +                       }
6134 +               }
6135 +               AuDebugOn(bsrc > bbot);
6136 +               cpg->bsrc = bsrc;
6137 +       }
6138 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6139 +       return au_do_sio_cpup_simple(cpg);
6140 +}
6141 +
6142 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6143 +{
6144 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6145 +       return au_do_sio_cpup_simple(cpg);
6146 +}
6147 +
6148 +/* ---------------------------------------------------------------------- */
6149 +
6150 +/*
6151 + * copyup the deleted file for writing.
6152 + */
6153 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6154 +                        struct file *file)
6155 +{
6156 +       int err;
6157 +       unsigned int flags_orig;
6158 +       aufs_bindex_t bsrc_orig;
6159 +       struct au_dinfo *dinfo;
6160 +       struct {
6161 +               struct au_hdentry *hd;
6162 +               struct dentry *h_dentry;
6163 +       } hdst, hsrc;
6164 +
6165 +       dinfo = au_di(cpg->dentry);
6166 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6167 +
6168 +       bsrc_orig = cpg->bsrc;
6169 +       cpg->bsrc = dinfo->di_btop;
6170 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6171 +       hdst.h_dentry = hdst.hd->hd_dentry;
6172 +       hdst.hd->hd_dentry = wh_dentry;
6173 +       dinfo->di_btop = cpg->bdst;
6174 +
6175 +       hsrc.h_dentry = NULL;
6176 +       if (file) {
6177 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6178 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6179 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6180 +       }
6181 +       flags_orig = cpg->flags;
6182 +       cpg->flags = !AuCpup_DTIME;
6183 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6184 +       cpg->flags = flags_orig;
6185 +       if (file) {
6186 +               if (!err)
6187 +                       err = au_reopen_nondir(file);
6188 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6189 +       }
6190 +       hdst.hd->hd_dentry = hdst.h_dentry;
6191 +       dinfo->di_btop = cpg->bsrc;
6192 +       cpg->bsrc = bsrc_orig;
6193 +
6194 +       return err;
6195 +}
6196 +
6197 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6198 +{
6199 +       int err;
6200 +       aufs_bindex_t bdst;
6201 +       struct au_dtime dt;
6202 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6203 +       struct au_branch *br;
6204 +       struct path h_path;
6205 +
6206 +       dentry = cpg->dentry;
6207 +       bdst = cpg->bdst;
6208 +       br = au_sbr(dentry->d_sb, bdst);
6209 +       parent = dget_parent(dentry);
6210 +       h_parent = au_h_dptr(parent, bdst);
6211 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6212 +       err = PTR_ERR(wh_dentry);
6213 +       if (IS_ERR(wh_dentry))
6214 +               goto out;
6215 +
6216 +       h_path.dentry = h_parent;
6217 +       h_path.mnt = au_br_mnt(br);
6218 +       au_dtime_store(&dt, parent, &h_path);
6219 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6220 +       if (unlikely(err))
6221 +               goto out_wh;
6222 +
6223 +       dget(wh_dentry);
6224 +       h_path.dentry = wh_dentry;
6225 +       if (!d_is_dir(wh_dentry)) {
6226 +               /* no delegation since it is just created */
6227 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6228 +                                  /*delegated*/NULL, /*force*/0);
6229 +       } else
6230 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6231 +       if (unlikely(err)) {
6232 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6233 +                       wh_dentry, err);
6234 +               err = -EIO;
6235 +       }
6236 +       au_dtime_revert(&dt);
6237 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6238 +
6239 +out_wh:
6240 +       dput(wh_dentry);
6241 +out:
6242 +       dput(parent);
6243 +       return err;
6244 +}
6245 +
6246 +struct au_cpup_wh_args {
6247 +       int *errp;
6248 +       struct au_cp_generic *cpg;
6249 +       struct file *file;
6250 +};
6251 +
6252 +static void au_call_cpup_wh(void *args)
6253 +{
6254 +       struct au_cpup_wh_args *a = args;
6255 +
6256 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6257 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6258 +       au_pin_hdir_release(a->cpg->pin);
6259 +}
6260 +
6261 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6262 +{
6263 +       int err, wkq_err;
6264 +       aufs_bindex_t bdst;
6265 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6266 +       struct inode *dir, *h_dir, *h_tmpdir;
6267 +       struct au_wbr *wbr;
6268 +       struct au_pin wh_pin, *pin_orig;
6269 +
6270 +       dentry = cpg->dentry;
6271 +       bdst = cpg->bdst;
6272 +       parent = dget_parent(dentry);
6273 +       dir = d_inode(parent);
6274 +       h_orph = NULL;
6275 +       h_parent = NULL;
6276 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6277 +       h_tmpdir = h_dir;
6278 +       pin_orig = NULL;
6279 +       if (!h_dir->i_nlink) {
6280 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6281 +               h_orph = wbr->wbr_orph;
6282 +
6283 +               h_parent = dget(au_h_dptr(parent, bdst));
6284 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6285 +               h_tmpdir = d_inode(h_orph);
6286 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6287 +
6288 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6289 +               /* todo: au_h_open_pre()? */
6290 +
6291 +               pin_orig = cpg->pin;
6292 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6293 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6294 +               cpg->pin = &wh_pin;
6295 +       }
6296 +
6297 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6298 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6299 +               err = au_cpup_wh(cpg, file);
6300 +       else {
6301 +               struct au_cpup_wh_args args = {
6302 +                       .errp   = &err,
6303 +                       .cpg    = cpg,
6304 +                       .file   = file
6305 +               };
6306 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6307 +               if (unlikely(wkq_err))
6308 +                       err = wkq_err;
6309 +       }
6310 +
6311 +       if (h_orph) {
6312 +               inode_unlock(h_tmpdir);
6313 +               /* todo: au_h_open_post()? */
6314 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6315 +               au_set_h_dptr(parent, bdst, h_parent);
6316 +               AuDebugOn(!pin_orig);
6317 +               cpg->pin = pin_orig;
6318 +       }
6319 +       iput(h_dir);
6320 +       dput(parent);
6321 +
6322 +       return err;
6323 +}
6324 +
6325 +/* ---------------------------------------------------------------------- */
6326 +
6327 +/*
6328 + * generic routine for both of copy-up and copy-down.
6329 + */
6330 +/* cf. revalidate function in file.c */
6331 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6332 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6333 +                        struct au_pin *pin,
6334 +                        struct dentry *h_parent, void *arg),
6335 +              void *arg)
6336 +{
6337 +       int err;
6338 +       struct au_pin pin;
6339 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6340 +
6341 +       err = 0;
6342 +       parent = dget_parent(dentry);
6343 +       if (IS_ROOT(parent))
6344 +               goto out;
6345 +
6346 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6347 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6348 +
6349 +       /* do not use au_dpage */
6350 +       real_parent = parent;
6351 +       while (1) {
6352 +               dput(parent);
6353 +               parent = dget_parent(dentry);
6354 +               h_parent = au_h_dptr(parent, bdst);
6355 +               if (h_parent)
6356 +                       goto out; /* success */
6357 +
6358 +               /* find top dir which is necessary to cpup */
6359 +               do {
6360 +                       d = parent;
6361 +                       dput(parent);
6362 +                       parent = dget_parent(d);
6363 +                       di_read_lock_parent3(parent, !AuLock_IR);
6364 +                       h_parent = au_h_dptr(parent, bdst);
6365 +                       di_read_unlock(parent, !AuLock_IR);
6366 +               } while (!h_parent);
6367 +
6368 +               if (d != real_parent)
6369 +                       di_write_lock_child3(d);
6370 +
6371 +               /* somebody else might create while we were sleeping */
6372 +               h_dentry = au_h_dptr(d, bdst);
6373 +               if (!h_dentry || d_is_negative(h_dentry)) {
6374 +                       if (h_dentry)
6375 +                               au_update_dbtop(d);
6376 +
6377 +                       au_pin_set_dentry(&pin, d);
6378 +                       err = au_do_pin(&pin);
6379 +                       if (!err) {
6380 +                               err = cp(d, bdst, &pin, h_parent, arg);
6381 +                               au_unpin(&pin);
6382 +                       }
6383 +               }
6384 +
6385 +               if (d != real_parent)
6386 +                       di_write_unlock(d);
6387 +               if (unlikely(err))
6388 +                       break;
6389 +       }
6390 +
6391 +out:
6392 +       dput(parent);
6393 +       return err;
6394 +}
6395 +
6396 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6397 +                      struct au_pin *pin,
6398 +                      struct dentry *h_parent __maybe_unused,
6399 +                      void *arg __maybe_unused)
6400 +{
6401 +       struct au_cp_generic cpg = {
6402 +               .dentry = dentry,
6403 +               .bdst   = bdst,
6404 +               .bsrc   = -1,
6405 +               .len    = 0,
6406 +               .pin    = pin,
6407 +               .flags  = AuCpup_DTIME
6408 +       };
6409 +       return au_sio_cpup_simple(&cpg);
6410 +}
6411 +
6412 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6413 +{
6414 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6415 +}
6416 +
6417 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6418 +{
6419 +       int err;
6420 +       struct dentry *parent;
6421 +       struct inode *dir;
6422 +
6423 +       parent = dget_parent(dentry);
6424 +       dir = d_inode(parent);
6425 +       err = 0;
6426 +       if (au_h_iptr(dir, bdst))
6427 +               goto out;
6428 +
6429 +       di_read_unlock(parent, AuLock_IR);
6430 +       di_write_lock_parent(parent);
6431 +       /* someone else might change our inode while we were sleeping */
6432 +       if (!au_h_iptr(dir, bdst))
6433 +               err = au_cpup_dirs(dentry, bdst);
6434 +       di_downgrade_lock(parent, AuLock_IR);
6435 +
6436 +out:
6437 +       dput(parent);
6438 +       return err;
6439 +}
6440 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6441 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6442 +++ linux/fs/aufs/cpup.h        2018-07-19 09:46:13.056647808 +0200
6443 @@ -0,0 +1,100 @@
6444 +/* SPDX-License-Identifier: GPL-2.0 */
6445 +/*
6446 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6447 + *
6448 + * This program, aufs is free software; you can redistribute it and/or modify
6449 + * it under the terms of the GNU General Public License as published by
6450 + * the Free Software Foundation; either version 2 of the License, or
6451 + * (at your option) any later version.
6452 + *
6453 + * This program is distributed in the hope that it will be useful,
6454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6456 + * GNU General Public License for more details.
6457 + *
6458 + * You should have received a copy of the GNU General Public License
6459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6460 + */
6461 +
6462 +/*
6463 + * copy-up/down functions
6464 + */
6465 +
6466 +#ifndef __AUFS_CPUP_H__
6467 +#define __AUFS_CPUP_H__
6468 +
6469 +#ifdef __KERNEL__
6470 +
6471 +#include <linux/path.h>
6472 +
6473 +struct inode;
6474 +struct file;
6475 +struct au_pin;
6476 +
6477 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6478 +void au_cpup_attr_timesizes(struct inode *inode);
6479 +void au_cpup_attr_nlink(struct inode *inode, int force);
6480 +void au_cpup_attr_changeable(struct inode *inode);
6481 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6482 +void au_cpup_attr_all(struct inode *inode, int force);
6483 +
6484 +/* ---------------------------------------------------------------------- */
6485 +
6486 +struct au_cp_generic {
6487 +       struct dentry   *dentry;
6488 +       aufs_bindex_t   bdst, bsrc;
6489 +       loff_t          len;
6490 +       struct au_pin   *pin;
6491 +       unsigned int    flags;
6492 +};
6493 +
6494 +/* cpup flags */
6495 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6496 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6497 +                                                  for link(2) */
6498 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6499 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6500 +                                                  cpup */
6501 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6502 +                                                  existing entry */
6503 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6504 +                                                  the branch is marked as RO */
6505 +
6506 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6507 +#undef AuCpup_HOPEN
6508 +#define AuCpup_HOPEN           0
6509 +#endif
6510 +
6511 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6512 +#define au_fset_cpup(flags, name) \
6513 +       do { (flags) |= AuCpup_##name; } while (0)
6514 +#define au_fclr_cpup(flags, name) \
6515 +       do { (flags) &= ~AuCpup_##name; } while (0)
6516 +
6517 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6518 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6519 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6520 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6521 +
6522 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6523 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6524 +                        struct au_pin *pin,
6525 +                        struct dentry *h_parent, void *arg),
6526 +              void *arg);
6527 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6528 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6529 +
6530 +/* ---------------------------------------------------------------------- */
6531 +
6532 +/* keep timestamps when copyup */
6533 +struct au_dtime {
6534 +       struct dentry *dt_dentry;
6535 +       struct path dt_h_path;
6536 +       struct timespec dt_atime, dt_mtime;
6537 +};
6538 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6539 +                   struct path *h_path);
6540 +void au_dtime_revert(struct au_dtime *dt);
6541 +
6542 +#endif /* __KERNEL__ */
6543 +#endif /* __AUFS_CPUP_H__ */
6544 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6545 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6546 +++ linux/fs/aufs/dbgaufs.c     2018-07-19 09:46:13.056647808 +0200
6547 @@ -0,0 +1,478 @@
6548 +/* SPDX-License-Identifier: GPL-2.0 */
6549 +/*
6550 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6551 + *
6552 + * This program, aufs is free software; you can redistribute it and/or modify
6553 + * it under the terms of the GNU General Public License as published by
6554 + * the Free Software Foundation; either version 2 of the License, or
6555 + * (at your option) any later version.
6556 + *
6557 + * This program is distributed in the hope that it will be useful,
6558 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6559 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6560 + * GNU General Public License for more details.
6561 + *
6562 + * You should have received a copy of the GNU General Public License
6563 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6564 + */
6565 +
6566 +/*
6567 + * debugfs interface
6568 + */
6569 +
6570 +#include <linux/debugfs.h>
6571 +#include "aufs.h"
6572 +
6573 +#ifndef CONFIG_SYSFS
6574 +#error DEBUG_FS depends upon SYSFS
6575 +#endif
6576 +
6577 +static struct dentry *dbgaufs;
6578 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6579 +
6580 +/* 20 is max digits length of ulong 64 */
6581 +struct dbgaufs_arg {
6582 +       int n;
6583 +       char a[20 * 4];
6584 +};
6585 +
6586 +/*
6587 + * common function for all XINO files
6588 + */
6589 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6590 +                             struct file *file)
6591 +{
6592 +       kfree(file->private_data);
6593 +       return 0;
6594 +}
6595 +
6596 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6597 +                          int cnt)
6598 +{
6599 +       int err;
6600 +       struct kstat st;
6601 +       struct dbgaufs_arg *p;
6602 +
6603 +       err = -ENOMEM;
6604 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6605 +       if (unlikely(!p))
6606 +               goto out;
6607 +
6608 +       err = 0;
6609 +       p->n = 0;
6610 +       file->private_data = p;
6611 +       if (!xf)
6612 +               goto out;
6613 +
6614 +       err = vfsub_getattr(&xf->f_path, &st);
6615 +       if (!err) {
6616 +               if (do_fcnt)
6617 +                       p->n = snprintf
6618 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6619 +                                cnt, st.blocks, st.blksize,
6620 +                                (long long)st.size);
6621 +               else
6622 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6623 +                                       st.blocks, st.blksize,
6624 +                                       (long long)st.size);
6625 +               AuDebugOn(p->n >= sizeof(p->a));
6626 +       } else {
6627 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6628 +               err = 0;
6629 +       }
6630 +
6631 +out:
6632 +       return err;
6633 +
6634 +}
6635 +
6636 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6637 +                              size_t count, loff_t *ppos)
6638 +{
6639 +       struct dbgaufs_arg *p;
6640 +
6641 +       p = file->private_data;
6642 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6643 +}
6644 +
6645 +/* ---------------------------------------------------------------------- */
6646 +
6647 +struct dbgaufs_plink_arg {
6648 +       int n;
6649 +       char a[];
6650 +};
6651 +
6652 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6653 +                                struct file *file)
6654 +{
6655 +       free_page((unsigned long)file->private_data);
6656 +       return 0;
6657 +}
6658 +
6659 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6660 +{
6661 +       int err, i, limit;
6662 +       unsigned long n, sum;
6663 +       struct dbgaufs_plink_arg *p;
6664 +       struct au_sbinfo *sbinfo;
6665 +       struct super_block *sb;
6666 +       struct hlist_bl_head *hbl;
6667 +
6668 +       err = -ENOMEM;
6669 +       p = (void *)get_zeroed_page(GFP_NOFS);
6670 +       if (unlikely(!p))
6671 +               goto out;
6672 +
6673 +       err = -EFBIG;
6674 +       sbinfo = inode->i_private;
6675 +       sb = sbinfo->si_sb;
6676 +       si_noflush_read_lock(sb);
6677 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6678 +               limit = PAGE_SIZE - sizeof(p->n);
6679 +
6680 +               /* the number of buckets */
6681 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6682 +               p->n += n;
6683 +               limit -= n;
6684 +
6685 +               sum = 0;
6686 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6687 +                    i++, hbl++) {
6688 +                       n = au_hbl_count(hbl);
6689 +                       sum += n;
6690 +
6691 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6692 +                       p->n += n;
6693 +                       limit -= n;
6694 +                       if (unlikely(limit <= 0))
6695 +                               goto out_free;
6696 +               }
6697 +               p->a[p->n - 1] = '\n';
6698 +
6699 +               /* the sum of plinks */
6700 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6701 +               p->n += n;
6702 +               limit -= n;
6703 +               if (unlikely(limit <= 0))
6704 +                       goto out_free;
6705 +       } else {
6706 +#define str "1\n0\n0\n"
6707 +               p->n = sizeof(str) - 1;
6708 +               strcpy(p->a, str);
6709 +#undef str
6710 +       }
6711 +       si_read_unlock(sb);
6712 +
6713 +       err = 0;
6714 +       file->private_data = p;
6715 +       goto out; /* success */
6716 +
6717 +out_free:
6718 +       free_page((unsigned long)p);
6719 +out:
6720 +       return err;
6721 +}
6722 +
6723 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6724 +                                 size_t count, loff_t *ppos)
6725 +{
6726 +       struct dbgaufs_plink_arg *p;
6727 +
6728 +       p = file->private_data;
6729 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6730 +}
6731 +
6732 +static const struct file_operations dbgaufs_plink_fop = {
6733 +       .owner          = THIS_MODULE,
6734 +       .open           = dbgaufs_plink_open,
6735 +       .release        = dbgaufs_plink_release,
6736 +       .read           = dbgaufs_plink_read
6737 +};
6738 +
6739 +/* ---------------------------------------------------------------------- */
6740 +
6741 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6742 +{
6743 +       int err;
6744 +       struct au_sbinfo *sbinfo;
6745 +       struct super_block *sb;
6746 +
6747 +       sbinfo = inode->i_private;
6748 +       sb = sbinfo->si_sb;
6749 +       si_noflush_read_lock(sb);
6750 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6751 +       si_read_unlock(sb);
6752 +       return err;
6753 +}
6754 +
6755 +static const struct file_operations dbgaufs_xib_fop = {
6756 +       .owner          = THIS_MODULE,
6757 +       .open           = dbgaufs_xib_open,
6758 +       .release        = dbgaufs_xi_release,
6759 +       .read           = dbgaufs_xi_read
6760 +};
6761 +
6762 +/* ---------------------------------------------------------------------- */
6763 +
6764 +#define DbgaufsXi_PREFIX "xi"
6765 +
6766 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6767 +{
6768 +       int err;
6769 +       long l;
6770 +       struct au_sbinfo *sbinfo;
6771 +       struct super_block *sb;
6772 +       struct file *xf;
6773 +       struct qstr *name;
6774 +       struct au_branch *br;
6775 +
6776 +       err = -ENOENT;
6777 +       xf = NULL;
6778 +       name = &file->f_path.dentry->d_name;
6779 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6780 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6781 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6782 +               goto out;
6783 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6784 +       if (unlikely(err))
6785 +               goto out;
6786 +
6787 +       sbinfo = inode->i_private;
6788 +       sb = sbinfo->si_sb;
6789 +       si_noflush_read_lock(sb);
6790 +       if (l <= au_sbbot(sb)) {
6791 +               br = au_sbr(sb, (aufs_bindex_t)l);
6792 +               xf = au_xino_file(br);
6793 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6794 +                                     au_xino_count(br));
6795 +       } else
6796 +               err = -ENOENT;
6797 +       si_read_unlock(sb);
6798 +
6799 +out:
6800 +       return err;
6801 +}
6802 +
6803 +static const struct file_operations dbgaufs_xino_fop = {
6804 +       .owner          = THIS_MODULE,
6805 +       .open           = dbgaufs_xino_open,
6806 +       .release        = dbgaufs_xi_release,
6807 +       .read           = dbgaufs_xi_read
6808 +};
6809 +
6810 +void dbgaufs_xino_del(struct au_branch *br)
6811 +{
6812 +       struct dentry *dbgaufs;
6813 +
6814 +       dbgaufs = br->br_dbgaufs;
6815 +       if (!dbgaufs)
6816 +               return;
6817 +
6818 +       br->br_dbgaufs = NULL;
6819 +       /* debugfs acquires the parent i_mutex */
6820 +       lockdep_off();
6821 +       debugfs_remove(dbgaufs);
6822 +       lockdep_on();
6823 +}
6824 +
6825 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6826 +{
6827 +       aufs_bindex_t bbot;
6828 +       struct au_branch *br;
6829 +
6830 +       if (!au_sbi(sb)->si_dbgaufs)
6831 +               return;
6832 +
6833 +       bbot = au_sbbot(sb);
6834 +       for (; bindex <= bbot; bindex++) {
6835 +               br = au_sbr(sb, bindex);
6836 +               dbgaufs_xino_del(br);
6837 +       }
6838 +}
6839 +
6840 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6841 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6842 +{
6843 +       struct au_branch *br;
6844 +       struct dentry *d;
6845 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6846 +
6847 +       snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6848 +       br = au_sbr(sb, bindex);
6849 +       if (br->br_dbgaufs) {
6850 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6851 +
6852 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6853 +                       /* debugfs acquires the parent i_mutex */
6854 +                       lockdep_off();
6855 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6856 +                                          name);
6857 +                       lockdep_on();
6858 +                       if (unlikely(!d))
6859 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6860 +                                       parent, name);
6861 +               }
6862 +       } else {
6863 +               lockdep_off();
6864 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6865 +                                                    sbinfo, &dbgaufs_xino_fop);
6866 +               lockdep_on();
6867 +               if (unlikely(!br->br_dbgaufs))
6868 +                       pr_warn("failed creaiting %pd/%s, ignored.\n",
6869 +                               parent, name);
6870 +       }
6871 +}
6872 +
6873 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6874 +{
6875 +       struct au_sbinfo *sbinfo;
6876 +       struct dentry *parent;
6877 +       aufs_bindex_t bbot;
6878 +
6879 +       if (!au_opt_test(au_mntflags(sb), XINO))
6880 +               return;
6881 +
6882 +       sbinfo = au_sbi(sb);
6883 +       parent = sbinfo->si_dbgaufs;
6884 +       if (!parent)
6885 +               return;
6886 +
6887 +       bbot = au_sbbot(sb);
6888 +       if (topdown)
6889 +               for (; bindex <= bbot; bindex++)
6890 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6891 +       else
6892 +               for (; bbot >= bindex; bbot--)
6893 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6894 +}
6895 +
6896 +/* ---------------------------------------------------------------------- */
6897 +
6898 +#ifdef CONFIG_AUFS_EXPORT
6899 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6900 +{
6901 +       int err;
6902 +       struct au_sbinfo *sbinfo;
6903 +       struct super_block *sb;
6904 +
6905 +       sbinfo = inode->i_private;
6906 +       sb = sbinfo->si_sb;
6907 +       si_noflush_read_lock(sb);
6908 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6909 +       si_read_unlock(sb);
6910 +       return err;
6911 +}
6912 +
6913 +static const struct file_operations dbgaufs_xigen_fop = {
6914 +       .owner          = THIS_MODULE,
6915 +       .open           = dbgaufs_xigen_open,
6916 +       .release        = dbgaufs_xi_release,
6917 +       .read           = dbgaufs_xi_read
6918 +};
6919 +
6920 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6921 +{
6922 +       int err;
6923 +
6924 +       /*
6925 +        * This function is a dynamic '__init' function actually,
6926 +        * so the tiny check for si_rwsem is unnecessary.
6927 +        */
6928 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6929 +
6930 +       err = -EIO;
6931 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6932 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6933 +                &dbgaufs_xigen_fop);
6934 +       if (sbinfo->si_dbgaufs_xigen)
6935 +               err = 0;
6936 +
6937 +       return err;
6938 +}
6939 +#else
6940 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6941 +{
6942 +       return 0;
6943 +}
6944 +#endif /* CONFIG_AUFS_EXPORT */
6945 +
6946 +/* ---------------------------------------------------------------------- */
6947 +
6948 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6949 +{
6950 +       /*
6951 +        * This function is a dynamic '__fin' function actually,
6952 +        * so the tiny check for si_rwsem is unnecessary.
6953 +        */
6954 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6955 +
6956 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6957 +       sbinfo->si_dbgaufs = NULL;
6958 +}
6959 +
6960 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6961 +{
6962 +       int err;
6963 +       char name[SysaufsSiNameLen];
6964 +
6965 +       /*
6966 +        * This function is a dynamic '__init' function actually,
6967 +        * so the tiny check for si_rwsem is unnecessary.
6968 +        */
6969 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6970 +
6971 +       err = -ENOENT;
6972 +       if (!dbgaufs) {
6973 +               AuErr1("/debug/aufs is uninitialized\n");
6974 +               goto out;
6975 +       }
6976 +
6977 +       err = -EIO;
6978 +       sysaufs_name(sbinfo, name);
6979 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6980 +       if (unlikely(!sbinfo->si_dbgaufs))
6981 +               goto out;
6982 +
6983 +       /* regardless plink/noplink option */
6984 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6985 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6986 +                &dbgaufs_plink_fop);
6987 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6988 +               goto out_dir;
6989 +
6990 +       /* regardless xino/noxino option */
6991 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6992 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6993 +                &dbgaufs_xib_fop);
6994 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6995 +               goto out_dir;
6996 +
6997 +       err = dbgaufs_xigen_init(sbinfo);
6998 +       if (!err)
6999 +               goto out; /* success */
7000 +
7001 +out_dir:
7002 +       dbgaufs_si_fin(sbinfo);
7003 +out:
7004 +       if (unlikely(err))
7005 +               pr_err("debugfs/aufs failed\n");
7006 +       return err;
7007 +}
7008 +
7009 +/* ---------------------------------------------------------------------- */
7010 +
7011 +void dbgaufs_fin(void)
7012 +{
7013 +       debugfs_remove(dbgaufs);
7014 +}
7015 +
7016 +int __init dbgaufs_init(void)
7017 +{
7018 +       int err;
7019 +
7020 +       err = -EIO;
7021 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7022 +       if (dbgaufs)
7023 +               err = 0;
7024 +       return err;
7025 +}
7026 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7027 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
7028 +++ linux/fs/aufs/dbgaufs.h     2018-07-19 09:46:13.056647808 +0200
7029 @@ -0,0 +1,53 @@
7030 +/* SPDX-License-Identifier: GPL-2.0 */
7031 +/*
7032 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7033 + *
7034 + * This program, aufs is free software; you can redistribute it and/or modify
7035 + * it under the terms of the GNU General Public License as published by
7036 + * the Free Software Foundation; either version 2 of the License, or
7037 + * (at your option) any later version.
7038 + *
7039 + * This program is distributed in the hope that it will be useful,
7040 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7041 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7042 + * GNU General Public License for more details.
7043 + *
7044 + * You should have received a copy of the GNU General Public License
7045 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7046 + */
7047 +
7048 +/*
7049 + * debugfs interface
7050 + */
7051 +
7052 +#ifndef __DBGAUFS_H__
7053 +#define __DBGAUFS_H__
7054 +
7055 +#ifdef __KERNEL__
7056 +
7057 +struct super_block;
7058 +struct au_sbinfo;
7059 +struct au_branch;
7060 +
7061 +#ifdef CONFIG_DEBUG_FS
7062 +/* dbgaufs.c */
7063 +void dbgaufs_xino_del(struct au_branch *br);
7064 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
7065 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
7066 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7067 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7068 +void dbgaufs_fin(void);
7069 +int __init dbgaufs_init(void);
7070 +#else
7071 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
7072 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
7073 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7074 +          int topdown)
7075 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7076 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7077 +AuStubVoid(dbgaufs_fin, void)
7078 +AuStubInt0(__init dbgaufs_init, void)
7079 +#endif /* CONFIG_DEBUG_FS */
7080 +
7081 +#endif /* __KERNEL__ */
7082 +#endif /* __DBGAUFS_H__ */
7083 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7084 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7085 +++ linux/fs/aufs/dcsub.c       2018-07-19 09:46:13.056647808 +0200
7086 @@ -0,0 +1,226 @@
7087 +/* SPDX-License-Identifier: GPL-2.0 */
7088 +/*
7089 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7090 + *
7091 + * This program, aufs is free software; you can redistribute it and/or modify
7092 + * it under the terms of the GNU General Public License as published by
7093 + * the Free Software Foundation; either version 2 of the License, or
7094 + * (at your option) any later version.
7095 + *
7096 + * This program is distributed in the hope that it will be useful,
7097 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7098 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7099 + * GNU General Public License for more details.
7100 + *
7101 + * You should have received a copy of the GNU General Public License
7102 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7103 + */
7104 +
7105 +/*
7106 + * sub-routines for dentry cache
7107 + */
7108 +
7109 +#include "aufs.h"
7110 +
7111 +static void au_dpage_free(struct au_dpage *dpage)
7112 +{
7113 +       int i;
7114 +       struct dentry **p;
7115 +
7116 +       p = dpage->dentries;
7117 +       for (i = 0; i < dpage->ndentry; i++)
7118 +               dput(*p++);
7119 +       free_page((unsigned long)dpage->dentries);
7120 +}
7121 +
7122 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7123 +{
7124 +       int err;
7125 +       void *p;
7126 +
7127 +       err = -ENOMEM;
7128 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7129 +       if (unlikely(!dpages->dpages))
7130 +               goto out;
7131 +
7132 +       p = (void *)__get_free_page(gfp);
7133 +       if (unlikely(!p))
7134 +               goto out_dpages;
7135 +
7136 +       dpages->dpages[0].ndentry = 0;
7137 +       dpages->dpages[0].dentries = p;
7138 +       dpages->ndpage = 1;
7139 +       return 0; /* success */
7140 +
7141 +out_dpages:
7142 +       kfree(dpages->dpages);
7143 +out:
7144 +       return err;
7145 +}
7146 +
7147 +void au_dpages_free(struct au_dcsub_pages *dpages)
7148 +{
7149 +       int i;
7150 +       struct au_dpage *p;
7151 +
7152 +       p = dpages->dpages;
7153 +       for (i = 0; i < dpages->ndpage; i++)
7154 +               au_dpage_free(p++);
7155 +       kfree(dpages->dpages);
7156 +}
7157 +
7158 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7159 +                           struct dentry *dentry, gfp_t gfp)
7160 +{
7161 +       int err, sz;
7162 +       struct au_dpage *dpage;
7163 +       void *p;
7164 +
7165 +       dpage = dpages->dpages + dpages->ndpage - 1;
7166 +       sz = PAGE_SIZE / sizeof(dentry);
7167 +       if (unlikely(dpage->ndentry >= sz)) {
7168 +               AuLabel(new dpage);
7169 +               err = -ENOMEM;
7170 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7171 +               p = au_kzrealloc(dpages->dpages, sz,
7172 +                                sz + sizeof(*dpages->dpages), gfp,
7173 +                                /*may_shrink*/0);
7174 +               if (unlikely(!p))
7175 +                       goto out;
7176 +
7177 +               dpages->dpages = p;
7178 +               dpage = dpages->dpages + dpages->ndpage;
7179 +               p = (void *)__get_free_page(gfp);
7180 +               if (unlikely(!p))
7181 +                       goto out;
7182 +
7183 +               dpage->ndentry = 0;
7184 +               dpage->dentries = p;
7185 +               dpages->ndpage++;
7186 +       }
7187 +
7188 +       AuDebugOn(au_dcount(dentry) <= 0);
7189 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7190 +       return 0; /* success */
7191 +
7192 +out:
7193 +       return err;
7194 +}
7195 +
7196 +/* todo: BAD approach */
7197 +/* copied from linux/fs/dcache.c */
7198 +enum d_walk_ret {
7199 +       D_WALK_CONTINUE,
7200 +       D_WALK_QUIT,
7201 +       D_WALK_NORETRY,
7202 +       D_WALK_SKIP,
7203 +};
7204 +
7205 +extern void d_walk(struct dentry *parent, void *data,
7206 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
7207 +                  void (*finish)(void *));
7208 +
7209 +struct ac_dpages_arg {
7210 +       int err;
7211 +       struct au_dcsub_pages *dpages;
7212 +       struct super_block *sb;
7213 +       au_dpages_test test;
7214 +       void *arg;
7215 +};
7216 +
7217 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7218 +{
7219 +       enum d_walk_ret ret;
7220 +       struct ac_dpages_arg *arg = _arg;
7221 +
7222 +       ret = D_WALK_CONTINUE;
7223 +       if (dentry->d_sb == arg->sb
7224 +           && !IS_ROOT(dentry)
7225 +           && au_dcount(dentry) > 0
7226 +           && au_di(dentry)
7227 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7228 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7229 +               if (unlikely(arg->err))
7230 +                       ret = D_WALK_QUIT;
7231 +       }
7232 +
7233 +       return ret;
7234 +}
7235 +
7236 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7237 +                  au_dpages_test test, void *arg)
7238 +{
7239 +       struct ac_dpages_arg args = {
7240 +               .err    = 0,
7241 +               .dpages = dpages,
7242 +               .sb     = root->d_sb,
7243 +               .test   = test,
7244 +               .arg    = arg
7245 +       };
7246 +
7247 +       d_walk(root, &args, au_call_dpages_append, NULL);
7248 +
7249 +       return args.err;
7250 +}
7251 +
7252 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7253 +                      int do_include, au_dpages_test test, void *arg)
7254 +{
7255 +       int err;
7256 +
7257 +       err = 0;
7258 +       write_seqlock(&rename_lock);
7259 +       spin_lock(&dentry->d_lock);
7260 +       if (do_include
7261 +           && au_dcount(dentry) > 0
7262 +           && (!test || test(dentry, arg)))
7263 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7264 +       spin_unlock(&dentry->d_lock);
7265 +       if (unlikely(err))
7266 +               goto out;
7267 +
7268 +       /*
7269 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7270 +        * mount
7271 +        */
7272 +       while (!IS_ROOT(dentry)) {
7273 +               dentry = dentry->d_parent; /* rename_lock is locked */
7274 +               spin_lock(&dentry->d_lock);
7275 +               if (au_dcount(dentry) > 0
7276 +                   && (!test || test(dentry, arg)))
7277 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7278 +               spin_unlock(&dentry->d_lock);
7279 +               if (unlikely(err))
7280 +                       break;
7281 +       }
7282 +
7283 +out:
7284 +       write_sequnlock(&rename_lock);
7285 +       return err;
7286 +}
7287 +
7288 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7289 +{
7290 +       return au_di(dentry) && dentry->d_sb == arg;
7291 +}
7292 +
7293 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7294 +                           struct dentry *dentry, int do_include)
7295 +{
7296 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7297 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7298 +}
7299 +
7300 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7301 +{
7302 +       struct path path[2] = {
7303 +               {
7304 +                       .dentry = d1
7305 +               },
7306 +               {
7307 +                       .dentry = d2
7308 +               }
7309 +       };
7310 +
7311 +       return path_is_under(path + 0, path + 1);
7312 +}
7313 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7314 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7315 +++ linux/fs/aufs/dcsub.h       2018-07-19 09:46:13.056647808 +0200
7316 @@ -0,0 +1,137 @@
7317 +/* SPDX-License-Identifier: GPL-2.0 */
7318 +/*
7319 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7320 + *
7321 + * This program, aufs is free software; you can redistribute it and/or modify
7322 + * it under the terms of the GNU General Public License as published by
7323 + * the Free Software Foundation; either version 2 of the License, or
7324 + * (at your option) any later version.
7325 + *
7326 + * This program is distributed in the hope that it will be useful,
7327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7329 + * GNU General Public License for more details.
7330 + *
7331 + * You should have received a copy of the GNU General Public License
7332 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7333 + */
7334 +
7335 +/*
7336 + * sub-routines for dentry cache
7337 + */
7338 +
7339 +#ifndef __AUFS_DCSUB_H__
7340 +#define __AUFS_DCSUB_H__
7341 +
7342 +#ifdef __KERNEL__
7343 +
7344 +#include <linux/dcache.h>
7345 +#include <linux/fs.h>
7346 +
7347 +struct au_dpage {
7348 +       int ndentry;
7349 +       struct dentry **dentries;
7350 +};
7351 +
7352 +struct au_dcsub_pages {
7353 +       int ndpage;
7354 +       struct au_dpage *dpages;
7355 +};
7356 +
7357 +/* ---------------------------------------------------------------------- */
7358 +
7359 +/* dcsub.c */
7360 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7361 +void au_dpages_free(struct au_dcsub_pages *dpages);
7362 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7363 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7364 +                  au_dpages_test test, void *arg);
7365 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7366 +                      int do_include, au_dpages_test test, void *arg);
7367 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7368 +                           struct dentry *dentry, int do_include);
7369 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7370 +
7371 +/* ---------------------------------------------------------------------- */
7372 +
7373 +/*
7374 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7375 + * include/linux/dcache.h. Try them (in the future).
7376 + */
7377 +
7378 +static inline int au_d_hashed_positive(struct dentry *d)
7379 +{
7380 +       int err;
7381 +       struct inode *inode = d_inode(d);
7382 +
7383 +       err = 0;
7384 +       if (unlikely(d_unhashed(d)
7385 +                    || d_is_negative(d)
7386 +                    || !inode->i_nlink))
7387 +               err = -ENOENT;
7388 +       return err;
7389 +}
7390 +
7391 +static inline int au_d_linkable(struct dentry *d)
7392 +{
7393 +       int err;
7394 +       struct inode *inode = d_inode(d);
7395 +
7396 +       err = au_d_hashed_positive(d);
7397 +       if (err
7398 +           && d_is_positive(d)
7399 +           && (inode->i_state & I_LINKABLE))
7400 +               err = 0;
7401 +       return err;
7402 +}
7403 +
7404 +static inline int au_d_alive(struct dentry *d)
7405 +{
7406 +       int err;
7407 +       struct inode *inode;
7408 +
7409 +       err = 0;
7410 +       if (!IS_ROOT(d))
7411 +               err = au_d_hashed_positive(d);
7412 +       else {
7413 +               inode = d_inode(d);
7414 +               if (unlikely(d_unlinked(d)
7415 +                            || d_is_negative(d)
7416 +                            || !inode->i_nlink))
7417 +                       err = -ENOENT;
7418 +       }
7419 +       return err;
7420 +}
7421 +
7422 +static inline int au_alive_dir(struct dentry *d)
7423 +{
7424 +       int err;
7425 +
7426 +       err = au_d_alive(d);
7427 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7428 +               err = -ENOENT;
7429 +       return err;
7430 +}
7431 +
7432 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7433 +{
7434 +       return a->len == b->len
7435 +               && !memcmp(a->name, b->name, a->len);
7436 +}
7437 +
7438 +/*
7439 + * by the commit
7440 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7441 + *                     taking d_lock
7442 + * the type of d_lockref.count became int, but the inlined function d_count()
7443 + * still returns unsigned int.
7444 + * I don't know why. Maybe it is for every d_count() users?
7445 + * Anyway au_dcount() lives on.
7446 + */
7447 +static inline int au_dcount(struct dentry *d)
7448 +{
7449 +       return (int)d_count(d);
7450 +}
7451 +
7452 +#endif /* __KERNEL__ */
7453 +#endif /* __AUFS_DCSUB_H__ */
7454 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7455 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7456 +++ linux/fs/aufs/debug.c       2018-07-19 09:46:13.056647808 +0200
7457 @@ -0,0 +1,441 @@
7458 +/* SPDX-License-Identifier: GPL-2.0 */
7459 +/*
7460 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7461 + *
7462 + * This program, aufs is free software; you can redistribute it and/or modify
7463 + * it under the terms of the GNU General Public License as published by
7464 + * the Free Software Foundation; either version 2 of the License, or
7465 + * (at your option) any later version.
7466 + *
7467 + * This program is distributed in the hope that it will be useful,
7468 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7469 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7470 + * GNU General Public License for more details.
7471 + *
7472 + * You should have received a copy of the GNU General Public License
7473 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7474 + */
7475 +
7476 +/*
7477 + * debug print functions
7478 + */
7479 +
7480 +#include "aufs.h"
7481 +
7482 +/* Returns 0, or -errno.  arg is in kp->arg. */
7483 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7484 +{
7485 +       int err, n;
7486 +
7487 +       err = kstrtoint(val, 0, &n);
7488 +       if (!err) {
7489 +               if (n > 0)
7490 +                       au_debug_on();
7491 +               else
7492 +                       au_debug_off();
7493 +       }
7494 +       return err;
7495 +}
7496 +
7497 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7498 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7499 +{
7500 +       atomic_t *a;
7501 +
7502 +       a = kp->arg;
7503 +       return sprintf(buffer, "%d", atomic_read(a));
7504 +}
7505 +
7506 +static struct kernel_param_ops param_ops_atomic_t = {
7507 +       .set = param_atomic_t_set,
7508 +       .get = param_atomic_t_get
7509 +       /* void (*free)(void *arg) */
7510 +};
7511 +
7512 +atomic_t aufs_debug = ATOMIC_INIT(0);
7513 +MODULE_PARM_DESC(debug, "debug print");
7514 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
7515 +
7516 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7517 +char *au_plevel = KERN_DEBUG;
7518 +#define dpri(fmt, ...) do {                                    \
7519 +       if ((au_plevel                                          \
7520 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7521 +           || au_debug_test())                                 \
7522 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7523 +} while (0)
7524 +
7525 +/* ---------------------------------------------------------------------- */
7526 +
7527 +void au_dpri_whlist(struct au_nhash *whlist)
7528 +{
7529 +       unsigned long ul, n;
7530 +       struct hlist_head *head;
7531 +       struct au_vdir_wh *pos;
7532 +
7533 +       n = whlist->nh_num;
7534 +       head = whlist->nh_head;
7535 +       for (ul = 0; ul < n; ul++) {
7536 +               hlist_for_each_entry(pos, head, wh_hash)
7537 +                       dpri("b%d, %.*s, %d\n",
7538 +                            pos->wh_bindex,
7539 +                            pos->wh_str.len, pos->wh_str.name,
7540 +                            pos->wh_str.len);
7541 +               head++;
7542 +       }
7543 +}
7544 +
7545 +void au_dpri_vdir(struct au_vdir *vdir)
7546 +{
7547 +       unsigned long ul;
7548 +       union au_vdir_deblk_p p;
7549 +       unsigned char *o;
7550 +
7551 +       if (!vdir || IS_ERR(vdir)) {
7552 +               dpri("err %ld\n", PTR_ERR(vdir));
7553 +               return;
7554 +       }
7555 +
7556 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7557 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7558 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7559 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7560 +               p.deblk = vdir->vd_deblk[ul];
7561 +               o = p.deblk;
7562 +               dpri("[%lu]: %p\n", ul, o);
7563 +       }
7564 +}
7565 +
7566 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7567 +                       struct dentry *wh)
7568 +{
7569 +       char *n = NULL;
7570 +       int l = 0;
7571 +
7572 +       if (!inode || IS_ERR(inode)) {
7573 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7574 +               return -1;
7575 +       }
7576 +
7577 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7578 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7579 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7580 +       if (wh) {
7581 +               n = (void *)wh->d_name.name;
7582 +               l = wh->d_name.len;
7583 +       }
7584 +
7585 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7586 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7587 +            bindex, inode,
7588 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7589 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7590 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7591 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
7592 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7593 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7594 +            inode->i_generation,
7595 +            l ? ", wh " : "", l, n);
7596 +       return 0;
7597 +}
7598 +
7599 +void au_dpri_inode(struct inode *inode)
7600 +{
7601 +       struct au_iinfo *iinfo;
7602 +       struct au_hinode *hi;
7603 +       aufs_bindex_t bindex;
7604 +       int err, hn;
7605 +
7606 +       err = do_pri_inode(-1, inode, -1, NULL);
7607 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7608 +               return;
7609 +
7610 +       iinfo = au_ii(inode);
7611 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7612 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7613 +       if (iinfo->ii_btop < 0)
7614 +               return;
7615 +       hn = 0;
7616 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7617 +               hi = au_hinode(iinfo, bindex);
7618 +               hn = !!au_hn(hi);
7619 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7620 +       }
7621 +}
7622 +
7623 +void au_dpri_dalias(struct inode *inode)
7624 +{
7625 +       struct dentry *d;
7626 +
7627 +       spin_lock(&inode->i_lock);
7628 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7629 +               au_dpri_dentry(d);
7630 +       spin_unlock(&inode->i_lock);
7631 +}
7632 +
7633 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7634 +{
7635 +       struct dentry *wh = NULL;
7636 +       int hn;
7637 +       struct inode *inode;
7638 +       struct au_iinfo *iinfo;
7639 +       struct au_hinode *hi;
7640 +
7641 +       if (!dentry || IS_ERR(dentry)) {
7642 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7643 +               return -1;
7644 +       }
7645 +       /* do not call dget_parent() here */
7646 +       /* note: access d_xxx without d_lock */
7647 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7648 +            bindex, dentry, dentry,
7649 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7650 +            au_dcount(dentry), dentry->d_flags,
7651 +            d_unhashed(dentry) ? "un" : "");
7652 +       hn = -1;
7653 +       inode = NULL;
7654 +       if (d_is_positive(dentry))
7655 +               inode = d_inode(dentry);
7656 +       if (inode
7657 +           && au_test_aufs(dentry->d_sb)
7658 +           && bindex >= 0
7659 +           && !au_is_bad_inode(inode)) {
7660 +               iinfo = au_ii(inode);
7661 +               hi = au_hinode(iinfo, bindex);
7662 +               hn = !!au_hn(hi);
7663 +               wh = hi->hi_whdentry;
7664 +       }
7665 +       do_pri_inode(bindex, inode, hn, wh);
7666 +       return 0;
7667 +}
7668 +
7669 +void au_dpri_dentry(struct dentry *dentry)
7670 +{
7671 +       struct au_dinfo *dinfo;
7672 +       aufs_bindex_t bindex;
7673 +       int err;
7674 +
7675 +       err = do_pri_dentry(-1, dentry);
7676 +       if (err || !au_test_aufs(dentry->d_sb))
7677 +               return;
7678 +
7679 +       dinfo = au_di(dentry);
7680 +       if (!dinfo)
7681 +               return;
7682 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7683 +            dinfo->di_btop, dinfo->di_bbot,
7684 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7685 +            dinfo->di_tmpfile);
7686 +       if (dinfo->di_btop < 0)
7687 +               return;
7688 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7689 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7690 +}
7691 +
7692 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7693 +{
7694 +       char a[32];
7695 +
7696 +       if (!file || IS_ERR(file)) {
7697 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7698 +               return -1;
7699 +       }
7700 +       a[0] = 0;
7701 +       if (bindex < 0
7702 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7703 +           && au_test_aufs(file->f_path.dentry->d_sb)
7704 +           && au_fi(file))
7705 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7706 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7707 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7708 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7709 +            file->f_version, file->f_pos, a);
7710 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7711 +               do_pri_dentry(bindex, file->f_path.dentry);
7712 +       return 0;
7713 +}
7714 +
7715 +void au_dpri_file(struct file *file)
7716 +{
7717 +       struct au_finfo *finfo;
7718 +       struct au_fidir *fidir;
7719 +       struct au_hfile *hfile;
7720 +       aufs_bindex_t bindex;
7721 +       int err;
7722 +
7723 +       err = do_pri_file(-1, file);
7724 +       if (err
7725 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7726 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7727 +               return;
7728 +
7729 +       finfo = au_fi(file);
7730 +       if (!finfo)
7731 +               return;
7732 +       if (finfo->fi_btop < 0)
7733 +               return;
7734 +       fidir = finfo->fi_hdir;
7735 +       if (!fidir)
7736 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7737 +       else
7738 +               for (bindex = finfo->fi_btop;
7739 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7740 +                    bindex++) {
7741 +                       hfile = fidir->fd_hfile + bindex;
7742 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7743 +               }
7744 +}
7745 +
7746 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7747 +{
7748 +       struct vfsmount *mnt;
7749 +       struct super_block *sb;
7750 +
7751 +       if (!br || IS_ERR(br))
7752 +               goto out;
7753 +       mnt = au_br_mnt(br);
7754 +       if (!mnt || IS_ERR(mnt))
7755 +               goto out;
7756 +       sb = mnt->mnt_sb;
7757 +       if (!sb || IS_ERR(sb))
7758 +               goto out;
7759 +
7760 +       dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
7761 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7762 +            "xino %d\n",
7763 +            bindex, br->br_perm, br->br_id, au_br_count(br),
7764 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7765 +            sb->s_flags, sb->s_count,
7766 +            atomic_read(&sb->s_active), !!au_xino_file(br));
7767 +       return 0;
7768 +
7769 +out:
7770 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7771 +       return -1;
7772 +}
7773 +
7774 +void au_dpri_sb(struct super_block *sb)
7775 +{
7776 +       struct au_sbinfo *sbinfo;
7777 +       aufs_bindex_t bindex;
7778 +       int err;
7779 +       /* to reuduce stack size */
7780 +       struct {
7781 +               struct vfsmount mnt;
7782 +               struct au_branch fake;
7783 +       } *a;
7784 +
7785 +       /* this function can be called from magic sysrq */
7786 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7787 +       if (unlikely(!a)) {
7788 +               dpri("no memory\n");
7789 +               return;
7790 +       }
7791 +
7792 +       a->mnt.mnt_sb = sb;
7793 +       a->fake.br_path.mnt = &a->mnt;
7794 +       au_br_count_init(&a->fake);
7795 +       err = do_pri_br(-1, &a->fake);
7796 +       au_br_count_fin(&a->fake);
7797 +       kfree(a);
7798 +       dpri("dev 0x%x\n", sb->s_dev);
7799 +       if (err || !au_test_aufs(sb))
7800 +               return;
7801 +
7802 +       sbinfo = au_sbi(sb);
7803 +       if (!sbinfo)
7804 +               return;
7805 +       dpri("nw %d, gen %u, kobj %d\n",
7806 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7807 +            kref_read(&sbinfo->si_kobj.kref));
7808 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7809 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7810 +}
7811 +
7812 +/* ---------------------------------------------------------------------- */
7813 +
7814 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7815 +{
7816 +       struct inode *h_inode, *inode = d_inode(dentry);
7817 +       struct dentry *h_dentry;
7818 +       aufs_bindex_t bindex, bbot, bi;
7819 +
7820 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7821 +               return;
7822 +
7823 +       bbot = au_dbbot(dentry);
7824 +       bi = au_ibbot(inode);
7825 +       if (bi < bbot)
7826 +               bbot = bi;
7827 +       bindex = au_dbtop(dentry);
7828 +       bi = au_ibtop(inode);
7829 +       if (bi > bindex)
7830 +               bindex = bi;
7831 +
7832 +       for (; bindex <= bbot; bindex++) {
7833 +               h_dentry = au_h_dptr(dentry, bindex);
7834 +               if (!h_dentry)
7835 +                       continue;
7836 +               h_inode = au_h_iptr(inode, bindex);
7837 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7838 +                       au_debug_on();
7839 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7840 +                       AuDbgDentry(dentry);
7841 +                       AuDbgInode(inode);
7842 +                       au_debug_off();
7843 +                       BUG();
7844 +               }
7845 +       }
7846 +}
7847 +
7848 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7849 +{
7850 +       int err, i, j;
7851 +       struct au_dcsub_pages dpages;
7852 +       struct au_dpage *dpage;
7853 +       struct dentry **dentries;
7854 +
7855 +       err = au_dpages_init(&dpages, GFP_NOFS);
7856 +       AuDebugOn(err);
7857 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7858 +       AuDebugOn(err);
7859 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7860 +               dpage = dpages.dpages + i;
7861 +               dentries = dpage->dentries;
7862 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7863 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7864 +       }
7865 +       au_dpages_free(&dpages);
7866 +}
7867 +
7868 +void au_dbg_verify_kthread(void)
7869 +{
7870 +       if (au_wkq_test()) {
7871 +               au_dbg_blocked();
7872 +               /*
7873 +                * It may be recursive, but udba=notify between two aufs mounts,
7874 +                * where a single ro branch is shared, is not a problem.
7875 +                */
7876 +               /* WARN_ON(1); */
7877 +       }
7878 +}
7879 +
7880 +/* ---------------------------------------------------------------------- */
7881 +
7882 +int __init au_debug_init(void)
7883 +{
7884 +       aufs_bindex_t bindex;
7885 +       struct au_vdir_destr destr;
7886 +
7887 +       bindex = -1;
7888 +       AuDebugOn(bindex >= 0);
7889 +
7890 +       destr.len = -1;
7891 +       AuDebugOn(destr.len < NAME_MAX);
7892 +
7893 +#ifdef CONFIG_4KSTACKS
7894 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7895 +#endif
7896 +
7897 +       return 0;
7898 +}
7899 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7900 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7901 +++ linux/fs/aufs/debug.h       2018-07-19 09:46:13.056647808 +0200
7902 @@ -0,0 +1,226 @@
7903 +/* SPDX-License-Identifier: GPL-2.0 */
7904 +/*
7905 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7906 + *
7907 + * This program, aufs is free software; you can redistribute it and/or modify
7908 + * it under the terms of the GNU General Public License as published by
7909 + * the Free Software Foundation; either version 2 of the License, or
7910 + * (at your option) any later version.
7911 + *
7912 + * This program is distributed in the hope that it will be useful,
7913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7915 + * GNU General Public License for more details.
7916 + *
7917 + * You should have received a copy of the GNU General Public License
7918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7919 + */
7920 +
7921 +/*
7922 + * debug print functions
7923 + */
7924 +
7925 +#ifndef __AUFS_DEBUG_H__
7926 +#define __AUFS_DEBUG_H__
7927 +
7928 +#ifdef __KERNEL__
7929 +
7930 +#include <linux/atomic.h>
7931 +#include <linux/module.h>
7932 +#include <linux/kallsyms.h>
7933 +#include <linux/sysrq.h>
7934 +
7935 +#ifdef CONFIG_AUFS_DEBUG
7936 +#define AuDebugOn(a)           BUG_ON(a)
7937 +
7938 +/* module parameter */
7939 +extern atomic_t aufs_debug;
7940 +static inline void au_debug_on(void)
7941 +{
7942 +       atomic_inc(&aufs_debug);
7943 +}
7944 +static inline void au_debug_off(void)
7945 +{
7946 +       atomic_dec_if_positive(&aufs_debug);
7947 +}
7948 +
7949 +static inline int au_debug_test(void)
7950 +{
7951 +       return atomic_read(&aufs_debug) > 0;
7952 +}
7953 +#else
7954 +#define AuDebugOn(a)           do {} while (0)
7955 +AuStubVoid(au_debug_on, void)
7956 +AuStubVoid(au_debug_off, void)
7957 +AuStubInt0(au_debug_test, void)
7958 +#endif /* CONFIG_AUFS_DEBUG */
7959 +
7960 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7961 +
7962 +/* ---------------------------------------------------------------------- */
7963 +
7964 +/* debug print */
7965 +
7966 +#define AuDbg(fmt, ...) do { \
7967 +       if (au_debug_test()) \
7968 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7969 +} while (0)
7970 +#define AuLabel(l)             AuDbg(#l "\n")
7971 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7972 +#define AuWarn1(fmt, ...) do { \
7973 +       static unsigned char _c; \
7974 +       if (!_c++) \
7975 +               pr_warn(fmt, ##__VA_ARGS__); \
7976 +} while (0)
7977 +
7978 +#define AuErr1(fmt, ...) do { \
7979 +       static unsigned char _c; \
7980 +       if (!_c++) \
7981 +               pr_err(fmt, ##__VA_ARGS__); \
7982 +} while (0)
7983 +
7984 +#define AuIOErr1(fmt, ...) do { \
7985 +       static unsigned char _c; \
7986 +       if (!_c++) \
7987 +               AuIOErr(fmt, ##__VA_ARGS__); \
7988 +} while (0)
7989 +
7990 +#define AuUnsupportMsg "This operation is not supported." \
7991 +                       " Please report this application to aufs-users ML."
7992 +#define AuUnsupport(fmt, ...) do { \
7993 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7994 +       dump_stack(); \
7995 +} while (0)
7996 +
7997 +#define AuTraceErr(e) do { \
7998 +       if (unlikely((e) < 0)) \
7999 +               AuDbg("err %d\n", (int)(e)); \
8000 +} while (0)
8001 +
8002 +#define AuTraceErrPtr(p) do { \
8003 +       if (IS_ERR(p)) \
8004 +               AuDbg("err %ld\n", PTR_ERR(p)); \
8005 +} while (0)
8006 +
8007 +/* dirty macros for debug print, use with "%.*s" and caution */
8008 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
8009 +
8010 +/* ---------------------------------------------------------------------- */
8011 +
8012 +struct dentry;
8013 +#ifdef CONFIG_AUFS_DEBUG
8014 +extern struct mutex au_dbg_mtx;
8015 +extern char *au_plevel;
8016 +struct au_nhash;
8017 +void au_dpri_whlist(struct au_nhash *whlist);
8018 +struct au_vdir;
8019 +void au_dpri_vdir(struct au_vdir *vdir);
8020 +struct inode;
8021 +void au_dpri_inode(struct inode *inode);
8022 +void au_dpri_dalias(struct inode *inode);
8023 +void au_dpri_dentry(struct dentry *dentry);
8024 +struct file;
8025 +void au_dpri_file(struct file *filp);
8026 +struct super_block;
8027 +void au_dpri_sb(struct super_block *sb);
8028 +
8029 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8030 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
8031 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
8032 +void au_dbg_verify_kthread(void);
8033 +
8034 +int __init au_debug_init(void);
8035 +
8036 +#define AuDbgWhlist(w) do { \
8037 +       mutex_lock(&au_dbg_mtx); \
8038 +       AuDbg(#w "\n"); \
8039 +       au_dpri_whlist(w); \
8040 +       mutex_unlock(&au_dbg_mtx); \
8041 +} while (0)
8042 +
8043 +#define AuDbgVdir(v) do { \
8044 +       mutex_lock(&au_dbg_mtx); \
8045 +       AuDbg(#v "\n"); \
8046 +       au_dpri_vdir(v); \
8047 +       mutex_unlock(&au_dbg_mtx); \
8048 +} while (0)
8049 +
8050 +#define AuDbgInode(i) do { \
8051 +       mutex_lock(&au_dbg_mtx); \
8052 +       AuDbg(#i "\n"); \
8053 +       au_dpri_inode(i); \
8054 +       mutex_unlock(&au_dbg_mtx); \
8055 +} while (0)
8056 +
8057 +#define AuDbgDAlias(i) do { \
8058 +       mutex_lock(&au_dbg_mtx); \
8059 +       AuDbg(#i "\n"); \
8060 +       au_dpri_dalias(i); \
8061 +       mutex_unlock(&au_dbg_mtx); \
8062 +} while (0)
8063 +
8064 +#define AuDbgDentry(d) do { \
8065 +       mutex_lock(&au_dbg_mtx); \
8066 +       AuDbg(#d "\n"); \
8067 +       au_dpri_dentry(d); \
8068 +       mutex_unlock(&au_dbg_mtx); \
8069 +} while (0)
8070 +
8071 +#define AuDbgFile(f) do { \
8072 +       mutex_lock(&au_dbg_mtx); \
8073 +       AuDbg(#f "\n"); \
8074 +       au_dpri_file(f); \
8075 +       mutex_unlock(&au_dbg_mtx); \
8076 +} while (0)
8077 +
8078 +#define AuDbgSb(sb) do { \
8079 +       mutex_lock(&au_dbg_mtx); \
8080 +       AuDbg(#sb "\n"); \
8081 +       au_dpri_sb(sb); \
8082 +       mutex_unlock(&au_dbg_mtx); \
8083 +} while (0)
8084 +
8085 +#define AuDbgSym(addr) do {                            \
8086 +       char sym[KSYM_SYMBOL_LEN];                      \
8087 +       sprint_symbol(sym, (unsigned long)addr);        \
8088 +       AuDbg("%s\n", sym);                             \
8089 +} while (0)
8090 +#else
8091 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8092 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8093 +AuStubVoid(au_dbg_verify_kthread, void)
8094 +AuStubInt0(__init au_debug_init, void)
8095 +
8096 +#define AuDbgWhlist(w)         do {} while (0)
8097 +#define AuDbgVdir(v)           do {} while (0)
8098 +#define AuDbgInode(i)          do {} while (0)
8099 +#define AuDbgDAlias(i)         do {} while (0)
8100 +#define AuDbgDentry(d)         do {} while (0)
8101 +#define AuDbgFile(f)           do {} while (0)
8102 +#define AuDbgSb(sb)            do {} while (0)
8103 +#define AuDbgSym(addr)         do {} while (0)
8104 +#endif /* CONFIG_AUFS_DEBUG */
8105 +
8106 +/* ---------------------------------------------------------------------- */
8107 +
8108 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8109 +int __init au_sysrq_init(void);
8110 +void au_sysrq_fin(void);
8111 +
8112 +#ifdef CONFIG_HW_CONSOLE
8113 +#define au_dbg_blocked() do { \
8114 +       WARN_ON(1); \
8115 +       handle_sysrq('w'); \
8116 +} while (0)
8117 +#else
8118 +AuStubVoid(au_dbg_blocked, void)
8119 +#endif
8120 +
8121 +#else
8122 +AuStubInt0(__init au_sysrq_init, void)
8123 +AuStubVoid(au_sysrq_fin, void)
8124 +AuStubVoid(au_dbg_blocked, void)
8125 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8126 +
8127 +#endif /* __KERNEL__ */
8128 +#endif /* __AUFS_DEBUG_H__ */
8129 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8130 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8131 +++ linux/fs/aufs/dentry.c      2018-07-19 09:46:13.056647808 +0200
8132 @@ -0,0 +1,1153 @@
8133 +/* SPDX-License-Identifier: GPL-2.0 */
8134 +/*
8135 + * Copyright (C) 2005-2018 Junjiro R. Okajima
8136 + *
8137 + * This program, aufs is free software; you can redistribute it and/or modify
8138 + * it under the terms of the GNU General Public License as published by
8139 + * the Free Software Foundation; either version 2 of the License, or
8140 + * (at your option) any later version.
8141 + *
8142 + * This program is distributed in the hope that it will be useful,
8143 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8144 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8145 + * GNU General Public License for more details.
8146 + *
8147 + * You should have received a copy of the GNU General Public License
8148 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8149 + */
8150 +
8151 +/*
8152 + * lookup and dentry operations
8153 + */
8154 +
8155 +#include <linux/namei.h>
8156 +#include "aufs.h"
8157 +
8158 +/*
8159 + * returns positive/negative dentry, NULL or an error.
8160 + * NULL means whiteout-ed or not-found.
8161 + */
8162 +static struct dentry*
8163 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8164 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8165 +{
8166 +       struct dentry *h_dentry;
8167 +       struct inode *h_inode;
8168 +       struct au_branch *br;
8169 +       int wh_found, opq;
8170 +       unsigned char wh_able;
8171 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8172 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8173 +                                                         IGNORE_PERM);
8174 +
8175 +       wh_found = 0;
8176 +       br = au_sbr(dentry->d_sb, bindex);
8177 +       wh_able = !!au_br_whable(br->br_perm);
8178 +       if (wh_able)
8179 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8180 +       h_dentry = ERR_PTR(wh_found);
8181 +       if (!wh_found)
8182 +               goto real_lookup;
8183 +       if (unlikely(wh_found < 0))
8184 +               goto out;
8185 +
8186 +       /* We found a whiteout */
8187 +       /* au_set_dbbot(dentry, bindex); */
8188 +       au_set_dbwh(dentry, bindex);
8189 +       if (!allow_neg)
8190 +               return NULL; /* success */
8191 +
8192 +real_lookup:
8193 +       if (!ignore_perm)
8194 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8195 +       else
8196 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8197 +       if (IS_ERR(h_dentry)) {
8198 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8199 +                   && !allow_neg)
8200 +                       h_dentry = NULL;
8201 +               goto out;
8202 +       }
8203 +
8204 +       h_inode = d_inode(h_dentry);
8205 +       if (d_is_negative(h_dentry)) {
8206 +               if (!allow_neg)
8207 +                       goto out_neg;
8208 +       } else if (wh_found
8209 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8210 +               goto out_neg;
8211 +       else if (au_ftest_lkup(args->flags, DIRREN)
8212 +                /* && h_inode */
8213 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8214 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8215 +                     (unsigned long long)h_inode->i_ino);
8216 +               goto out_neg;
8217 +       }
8218 +
8219 +       if (au_dbbot(dentry) <= bindex)
8220 +               au_set_dbbot(dentry, bindex);
8221 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8222 +               au_set_dbtop(dentry, bindex);
8223 +       au_set_h_dptr(dentry, bindex, h_dentry);
8224 +
8225 +       if (!d_is_dir(h_dentry)
8226 +           || !wh_able
8227 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8228 +               goto out; /* success */
8229 +
8230 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8231 +       opq = au_diropq_test(h_dentry);
8232 +       inode_unlock_shared(h_inode);
8233 +       if (opq > 0)
8234 +               au_set_dbdiropq(dentry, bindex);
8235 +       else if (unlikely(opq < 0)) {
8236 +               au_set_h_dptr(dentry, bindex, NULL);
8237 +               h_dentry = ERR_PTR(opq);
8238 +       }
8239 +       goto out;
8240 +
8241 +out_neg:
8242 +       dput(h_dentry);
8243 +       h_dentry = NULL;
8244 +out:
8245 +       return h_dentry;
8246 +}
8247 +
8248 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8249 +{
8250 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8251 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8252 +               return -EPERM;
8253 +       return 0;
8254 +}
8255 +
8256 +/*
8257 + * returns the number of lower positive dentries,
8258 + * otherwise an error.
8259 + * can be called at unlinking with @type is zero.
8260 + */
8261 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8262 +                  unsigned int flags)
8263 +{
8264 +       int npositive, err;
8265 +       aufs_bindex_t bindex, btail, bdiropq;
8266 +       unsigned char isdir, dirperm1, dirren;
8267 +       struct au_do_lookup_args args = {
8268 +               .flags          = flags,
8269 +               .name           = &dentry->d_name
8270 +       };
8271 +       struct dentry *parent;
8272 +       struct super_block *sb;
8273 +
8274 +       sb = dentry->d_sb;
8275 +       err = au_test_shwh(sb, args.name);
8276 +       if (unlikely(err))
8277 +               goto out;
8278 +
8279 +       err = au_wh_name_alloc(&args.whname, args.name);
8280 +       if (unlikely(err))
8281 +               goto out;
8282 +
8283 +       isdir = !!d_is_dir(dentry);
8284 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8285 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8286 +       if (dirren)
8287 +               au_fset_lkup(args.flags, DIRREN);
8288 +
8289 +       npositive = 0;
8290 +       parent = dget_parent(dentry);
8291 +       btail = au_dbtaildir(parent);
8292 +       for (bindex = btop; bindex <= btail; bindex++) {
8293 +               struct dentry *h_parent, *h_dentry;
8294 +               struct inode *h_inode, *h_dir;
8295 +               struct au_branch *br;
8296 +
8297 +               h_dentry = au_h_dptr(dentry, bindex);
8298 +               if (h_dentry) {
8299 +                       if (d_is_positive(h_dentry))
8300 +                               npositive++;
8301 +                       break;
8302 +               }
8303 +               h_parent = au_h_dptr(parent, bindex);
8304 +               if (!h_parent || !d_is_dir(h_parent))
8305 +                       continue;
8306 +
8307 +               if (dirren) {
8308 +                       /* if the inum matches, then use the prepared name */
8309 +                       err = au_dr_lkup_name(&args, bindex);
8310 +                       if (unlikely(err))
8311 +                               goto out_parent;
8312 +               }
8313 +
8314 +               h_dir = d_inode(h_parent);
8315 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8316 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8317 +               inode_unlock_shared(h_dir);
8318 +               err = PTR_ERR(h_dentry);
8319 +               if (IS_ERR(h_dentry))
8320 +                       goto out_parent;
8321 +               if (h_dentry)
8322 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8323 +               if (dirperm1)
8324 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8325 +
8326 +               if (au_dbwh(dentry) == bindex)
8327 +                       break;
8328 +               if (!h_dentry)
8329 +                       continue;
8330 +               if (d_is_negative(h_dentry))
8331 +                       continue;
8332 +               h_inode = d_inode(h_dentry);
8333 +               npositive++;
8334 +               if (!args.type)
8335 +                       args.type = h_inode->i_mode & S_IFMT;
8336 +               if (args.type != S_IFDIR)
8337 +                       break;
8338 +               else if (isdir) {
8339 +                       /* the type of lower may be different */
8340 +                       bdiropq = au_dbdiropq(dentry);
8341 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8342 +                               break;
8343 +               }
8344 +               br = au_sbr(sb, bindex);
8345 +               if (dirren
8346 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8347 +                                          /*add_ent*/NULL)) {
8348 +                       /* prepare next name to lookup */
8349 +                       err = au_dr_lkup(&args, dentry, bindex);
8350 +                       if (unlikely(err))
8351 +                               goto out_parent;
8352 +               }
8353 +       }
8354 +
8355 +       if (npositive) {
8356 +               AuLabel(positive);
8357 +               au_update_dbtop(dentry);
8358 +       }
8359 +       err = npositive;
8360 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8361 +                    && au_dbtop(dentry) < 0)) {
8362 +               err = -EIO;
8363 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8364 +                       dentry, err);
8365 +       }
8366 +
8367 +out_parent:
8368 +       dput(parent);
8369 +       kfree(args.whname.name);
8370 +       if (dirren)
8371 +               au_dr_lkup_fin(&args);
8372 +out:
8373 +       return err;
8374 +}
8375 +
8376 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8377 +{
8378 +       struct dentry *dentry;
8379 +       int wkq_err;
8380 +
8381 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8382 +               dentry = vfsub_lkup_one(name, parent);
8383 +       else {
8384 +               struct vfsub_lkup_one_args args = {
8385 +                       .errp   = &dentry,
8386 +                       .name   = name,
8387 +                       .parent = parent
8388 +               };
8389 +
8390 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8391 +               if (unlikely(wkq_err))
8392 +                       dentry = ERR_PTR(wkq_err);
8393 +       }
8394 +
8395 +       return dentry;
8396 +}
8397 +
8398 +/*
8399 + * lookup @dentry on @bindex which should be negative.
8400 + */
8401 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8402 +{
8403 +       int err;
8404 +       struct dentry *parent, *h_parent, *h_dentry;
8405 +       struct au_branch *br;
8406 +
8407 +       parent = dget_parent(dentry);
8408 +       h_parent = au_h_dptr(parent, bindex);
8409 +       br = au_sbr(dentry->d_sb, bindex);
8410 +       if (wh)
8411 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8412 +       else
8413 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8414 +       err = PTR_ERR(h_dentry);
8415 +       if (IS_ERR(h_dentry))
8416 +               goto out;
8417 +       if (unlikely(d_is_positive(h_dentry))) {
8418 +               err = -EIO;
8419 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8420 +               dput(h_dentry);
8421 +               goto out;
8422 +       }
8423 +
8424 +       err = 0;
8425 +       if (bindex < au_dbtop(dentry))
8426 +               au_set_dbtop(dentry, bindex);
8427 +       if (au_dbbot(dentry) < bindex)
8428 +               au_set_dbbot(dentry, bindex);
8429 +       au_set_h_dptr(dentry, bindex, h_dentry);
8430 +
8431 +out:
8432 +       dput(parent);
8433 +       return err;
8434 +}
8435 +
8436 +/* ---------------------------------------------------------------------- */
8437 +
8438 +/* subset of struct inode */
8439 +struct au_iattr {
8440 +       unsigned long           i_ino;
8441 +       /* unsigned int         i_nlink; */
8442 +       kuid_t                  i_uid;
8443 +       kgid_t                  i_gid;
8444 +       u64                     i_version;
8445 +/*
8446 +       loff_t                  i_size;
8447 +       blkcnt_t                i_blocks;
8448 +*/
8449 +       umode_t                 i_mode;
8450 +};
8451 +
8452 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8453 +{
8454 +       ia->i_ino = h_inode->i_ino;
8455 +       /* ia->i_nlink = h_inode->i_nlink; */
8456 +       ia->i_uid = h_inode->i_uid;
8457 +       ia->i_gid = h_inode->i_gid;
8458 +       ia->i_version = inode_query_iversion(h_inode);
8459 +/*
8460 +       ia->i_size = h_inode->i_size;
8461 +       ia->i_blocks = h_inode->i_blocks;
8462 +*/
8463 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8464 +}
8465 +
8466 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8467 +{
8468 +       return ia->i_ino != h_inode->i_ino
8469 +               /* || ia->i_nlink != h_inode->i_nlink */
8470 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8471 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8472 +               || !inode_eq_iversion(h_inode, ia->i_version)
8473 +/*
8474 +               || ia->i_size != h_inode->i_size
8475 +               || ia->i_blocks != h_inode->i_blocks
8476 +*/
8477 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8478 +}
8479 +
8480 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8481 +                             struct au_branch *br)
8482 +{
8483 +       int err;
8484 +       struct au_iattr ia;
8485 +       struct inode *h_inode;
8486 +       struct dentry *h_d;
8487 +       struct super_block *h_sb;
8488 +
8489 +       err = 0;
8490 +       memset(&ia, -1, sizeof(ia));
8491 +       h_sb = h_dentry->d_sb;
8492 +       h_inode = NULL;
8493 +       if (d_is_positive(h_dentry)) {
8494 +               h_inode = d_inode(h_dentry);
8495 +               au_iattr_save(&ia, h_inode);
8496 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8497 +               /* nfs d_revalidate may return 0 for negative dentry */
8498 +               /* fuse d_revalidate always return 0 for negative dentry */
8499 +               goto out;
8500 +
8501 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8502 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8503 +       err = PTR_ERR(h_d);
8504 +       if (IS_ERR(h_d))
8505 +               goto out;
8506 +
8507 +       err = 0;
8508 +       if (unlikely(h_d != h_dentry
8509 +                    || d_inode(h_d) != h_inode
8510 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8511 +               err = au_busy_or_stale();
8512 +       dput(h_d);
8513 +
8514 +out:
8515 +       AuTraceErr(err);
8516 +       return err;
8517 +}
8518 +
8519 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8520 +               struct dentry *h_parent, struct au_branch *br)
8521 +{
8522 +       int err;
8523 +
8524 +       err = 0;
8525 +       if (udba == AuOpt_UDBA_REVAL
8526 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8527 +               IMustLock(h_dir);
8528 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8529 +       } else if (udba != AuOpt_UDBA_NONE)
8530 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8531 +
8532 +       return err;
8533 +}
8534 +
8535 +/* ---------------------------------------------------------------------- */
8536 +
8537 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8538 +{
8539 +       int err;
8540 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8541 +       struct au_hdentry tmp, *p, *q;
8542 +       struct au_dinfo *dinfo;
8543 +       struct super_block *sb;
8544 +
8545 +       DiMustWriteLock(dentry);
8546 +
8547 +       sb = dentry->d_sb;
8548 +       dinfo = au_di(dentry);
8549 +       bbot = dinfo->di_bbot;
8550 +       bwh = dinfo->di_bwh;
8551 +       bdiropq = dinfo->di_bdiropq;
8552 +       bindex = dinfo->di_btop;
8553 +       p = au_hdentry(dinfo, bindex);
8554 +       for (; bindex <= bbot; bindex++, p++) {
8555 +               if (!p->hd_dentry)
8556 +                       continue;
8557 +
8558 +               new_bindex = au_br_index(sb, p->hd_id);
8559 +               if (new_bindex == bindex)
8560 +                       continue;
8561 +
8562 +               if (dinfo->di_bwh == bindex)
8563 +                       bwh = new_bindex;
8564 +               if (dinfo->di_bdiropq == bindex)
8565 +                       bdiropq = new_bindex;
8566 +               if (new_bindex < 0) {
8567 +                       au_hdput(p);
8568 +                       p->hd_dentry = NULL;
8569 +                       continue;
8570 +               }
8571 +
8572 +               /* swap two lower dentries, and loop again */
8573 +               q = au_hdentry(dinfo, new_bindex);
8574 +               tmp = *q;
8575 +               *q = *p;
8576 +               *p = tmp;
8577 +               if (tmp.hd_dentry) {
8578 +                       bindex--;
8579 +                       p--;
8580 +               }
8581 +       }
8582 +
8583 +       dinfo->di_bwh = -1;
8584 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8585 +               dinfo->di_bwh = bwh;
8586 +
8587 +       dinfo->di_bdiropq = -1;
8588 +       if (bdiropq >= 0
8589 +           && bdiropq <= au_sbbot(sb)
8590 +           && au_sbr_whable(sb, bdiropq))
8591 +               dinfo->di_bdiropq = bdiropq;
8592 +
8593 +       err = -EIO;
8594 +       dinfo->di_btop = -1;
8595 +       dinfo->di_bbot = -1;
8596 +       bbot = au_dbbot(parent);
8597 +       bindex = 0;
8598 +       p = au_hdentry(dinfo, bindex);
8599 +       for (; bindex <= bbot; bindex++, p++)
8600 +               if (p->hd_dentry) {
8601 +                       dinfo->di_btop = bindex;
8602 +                       break;
8603 +               }
8604 +
8605 +       if (dinfo->di_btop >= 0) {
8606 +               bindex = bbot;
8607 +               p = au_hdentry(dinfo, bindex);
8608 +               for (; bindex >= 0; bindex--, p--)
8609 +                       if (p->hd_dentry) {
8610 +                               dinfo->di_bbot = bindex;
8611 +                               err = 0;
8612 +                               break;
8613 +                       }
8614 +       }
8615 +
8616 +       return err;
8617 +}
8618 +
8619 +static void au_do_hide(struct dentry *dentry)
8620 +{
8621 +       struct inode *inode;
8622 +
8623 +       if (d_really_is_positive(dentry)) {
8624 +               inode = d_inode(dentry);
8625 +               if (!d_is_dir(dentry)) {
8626 +                       if (inode->i_nlink && !d_unhashed(dentry))
8627 +                               drop_nlink(inode);
8628 +               } else {
8629 +                       clear_nlink(inode);
8630 +                       /* stop next lookup */
8631 +                       inode->i_flags |= S_DEAD;
8632 +               }
8633 +               smp_mb(); /* necessary? */
8634 +       }
8635 +       d_drop(dentry);
8636 +}
8637 +
8638 +static int au_hide_children(struct dentry *parent)
8639 +{
8640 +       int err, i, j, ndentry;
8641 +       struct au_dcsub_pages dpages;
8642 +       struct au_dpage *dpage;
8643 +       struct dentry *dentry;
8644 +
8645 +       err = au_dpages_init(&dpages, GFP_NOFS);
8646 +       if (unlikely(err))
8647 +               goto out;
8648 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8649 +       if (unlikely(err))
8650 +               goto out_dpages;
8651 +
8652 +       /* in reverse order */
8653 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8654 +               dpage = dpages.dpages + i;
8655 +               ndentry = dpage->ndentry;
8656 +               for (j = ndentry - 1; j >= 0; j--) {
8657 +                       dentry = dpage->dentries[j];
8658 +                       if (dentry != parent)
8659 +                               au_do_hide(dentry);
8660 +               }
8661 +       }
8662 +
8663 +out_dpages:
8664 +       au_dpages_free(&dpages);
8665 +out:
8666 +       return err;
8667 +}
8668 +
8669 +static void au_hide(struct dentry *dentry)
8670 +{
8671 +       int err;
8672 +
8673 +       AuDbgDentry(dentry);
8674 +       if (d_is_dir(dentry)) {
8675 +               /* shrink_dcache_parent(dentry); */
8676 +               err = au_hide_children(dentry);
8677 +               if (unlikely(err))
8678 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8679 +                               dentry, err);
8680 +       }
8681 +       au_do_hide(dentry);
8682 +}
8683 +
8684 +/*
8685 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8686 + *
8687 + * a dirty branch is added
8688 + * - on the top of layers
8689 + * - in the middle of layers
8690 + * - to the bottom of layers
8691 + *
8692 + * on the added branch there exists
8693 + * - a whiteout
8694 + * - a diropq
8695 + * - a same named entry
8696 + *   + exist
8697 + *     * negative --> positive
8698 + *     * positive --> positive
8699 + *      - type is unchanged
8700 + *      - type is changed
8701 + *   + doesn't exist
8702 + *     * negative --> negative
8703 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8704 + * - none
8705 + */
8706 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8707 +                              struct au_dinfo *tmp)
8708 +{
8709 +       int err;
8710 +       aufs_bindex_t bindex, bbot;
8711 +       struct {
8712 +               struct dentry *dentry;
8713 +               struct inode *inode;
8714 +               mode_t mode;
8715 +       } orig_h, tmp_h = {
8716 +               .dentry = NULL
8717 +       };
8718 +       struct au_hdentry *hd;
8719 +       struct inode *inode, *h_inode;
8720 +       struct dentry *h_dentry;
8721 +
8722 +       err = 0;
8723 +       AuDebugOn(dinfo->di_btop < 0);
8724 +       orig_h.mode = 0;
8725 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8726 +       orig_h.inode = NULL;
8727 +       if (d_is_positive(orig_h.dentry)) {
8728 +               orig_h.inode = d_inode(orig_h.dentry);
8729 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8730 +       }
8731 +       if (tmp->di_btop >= 0) {
8732 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8733 +               if (d_is_positive(tmp_h.dentry)) {
8734 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8735 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8736 +               }
8737 +       }
8738 +
8739 +       inode = NULL;
8740 +       if (d_really_is_positive(dentry))
8741 +               inode = d_inode(dentry);
8742 +       if (!orig_h.inode) {
8743 +               AuDbg("nagative originally\n");
8744 +               if (inode) {
8745 +                       au_hide(dentry);
8746 +                       goto out;
8747 +               }
8748 +               AuDebugOn(inode);
8749 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8750 +               AuDebugOn(dinfo->di_bdiropq != -1);
8751 +
8752 +               if (!tmp_h.inode) {
8753 +                       AuDbg("negative --> negative\n");
8754 +                       /* should have only one negative lower */
8755 +                       if (tmp->di_btop >= 0
8756 +                           && tmp->di_btop < dinfo->di_btop) {
8757 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8758 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8759 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8760 +                               au_di_cp(dinfo, tmp);
8761 +                               hd = au_hdentry(tmp, tmp->di_btop);
8762 +                               au_set_h_dptr(dentry, tmp->di_btop,
8763 +                                             dget(hd->hd_dentry));
8764 +                       }
8765 +                       au_dbg_verify_dinode(dentry);
8766 +               } else {
8767 +                       AuDbg("negative --> positive\n");
8768 +                       /*
8769 +                        * similar to the behaviour of creating with bypassing
8770 +                        * aufs.
8771 +                        * unhash it in order to force an error in the
8772 +                        * succeeding create operation.
8773 +                        * we should not set S_DEAD here.
8774 +                        */
8775 +                       d_drop(dentry);
8776 +                       /* au_di_swap(tmp, dinfo); */
8777 +                       au_dbg_verify_dinode(dentry);
8778 +               }
8779 +       } else {
8780 +               AuDbg("positive originally\n");
8781 +               /* inode may be NULL */
8782 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8783 +               if (!tmp_h.inode) {
8784 +                       AuDbg("positive --> negative\n");
8785 +                       /* or bypassing aufs */
8786 +                       au_hide(dentry);
8787 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8788 +                               dinfo->di_bwh = tmp->di_bwh;
8789 +                       if (inode)
8790 +                               err = au_refresh_hinode_self(inode);
8791 +                       au_dbg_verify_dinode(dentry);
8792 +               } else if (orig_h.mode == tmp_h.mode) {
8793 +                       AuDbg("positive --> positive, same type\n");
8794 +                       if (!S_ISDIR(orig_h.mode)
8795 +                           && dinfo->di_btop > tmp->di_btop) {
8796 +                               /*
8797 +                                * similar to the behaviour of removing and
8798 +                                * creating.
8799 +                                */
8800 +                               au_hide(dentry);
8801 +                               if (inode)
8802 +                                       err = au_refresh_hinode_self(inode);
8803 +                               au_dbg_verify_dinode(dentry);
8804 +                       } else {
8805 +                               /* fill empty slots */
8806 +                               if (dinfo->di_btop > tmp->di_btop)
8807 +                                       dinfo->di_btop = tmp->di_btop;
8808 +                               if (dinfo->di_bbot < tmp->di_bbot)
8809 +                                       dinfo->di_bbot = tmp->di_bbot;
8810 +                               dinfo->di_bwh = tmp->di_bwh;
8811 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8812 +                               bbot = dinfo->di_bbot;
8813 +                               bindex = tmp->di_btop;
8814 +                               hd = au_hdentry(tmp, bindex);
8815 +                               for (; bindex <= bbot; bindex++, hd++) {
8816 +                                       if (au_h_dptr(dentry, bindex))
8817 +                                               continue;
8818 +                                       h_dentry = hd->hd_dentry;
8819 +                                       if (!h_dentry)
8820 +                                               continue;
8821 +                                       AuDebugOn(d_is_negative(h_dentry));
8822 +                                       h_inode = d_inode(h_dentry);
8823 +                                       AuDebugOn(orig_h.mode
8824 +                                                 != (h_inode->i_mode
8825 +                                                     & S_IFMT));
8826 +                                       au_set_h_dptr(dentry, bindex,
8827 +                                                     dget(h_dentry));
8828 +                               }
8829 +                               if (inode)
8830 +                                       err = au_refresh_hinode(inode, dentry);
8831 +                               au_dbg_verify_dinode(dentry);
8832 +                       }
8833 +               } else {
8834 +                       AuDbg("positive --> positive, different type\n");
8835 +                       /* similar to the behaviour of removing and creating */
8836 +                       au_hide(dentry);
8837 +                       if (inode)
8838 +                               err = au_refresh_hinode_self(inode);
8839 +                       au_dbg_verify_dinode(dentry);
8840 +               }
8841 +       }
8842 +
8843 +out:
8844 +       return err;
8845 +}
8846 +
8847 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8848 +{
8849 +       const struct dentry_operations *dop
8850 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8851 +       static const unsigned int mask
8852 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8853 +
8854 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8855 +
8856 +       if (dentry->d_op == dop)
8857 +               return;
8858 +
8859 +       AuDbg("%pd\n", dentry);
8860 +       spin_lock(&dentry->d_lock);
8861 +       if (dop == &aufs_dop)
8862 +               dentry->d_flags |= mask;
8863 +       else
8864 +               dentry->d_flags &= ~mask;
8865 +       dentry->d_op = dop;
8866 +       spin_unlock(&dentry->d_lock);
8867 +}
8868 +
8869 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8870 +{
8871 +       int err, ebrange, nbr;
8872 +       unsigned int sigen;
8873 +       struct au_dinfo *dinfo, *tmp;
8874 +       struct super_block *sb;
8875 +       struct inode *inode;
8876 +
8877 +       DiMustWriteLock(dentry);
8878 +       AuDebugOn(IS_ROOT(dentry));
8879 +       AuDebugOn(d_really_is_negative(parent));
8880 +
8881 +       sb = dentry->d_sb;
8882 +       sigen = au_sigen(sb);
8883 +       err = au_digen_test(parent, sigen);
8884 +       if (unlikely(err))
8885 +               goto out;
8886 +
8887 +       nbr = au_sbbot(sb) + 1;
8888 +       dinfo = au_di(dentry);
8889 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8890 +       if (unlikely(err))
8891 +               goto out;
8892 +       ebrange = au_dbrange_test(dentry);
8893 +       if (!ebrange)
8894 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8895 +
8896 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8897 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8898 +               if (d_really_is_positive(dentry)) {
8899 +                       inode = d_inode(dentry);
8900 +                       err = au_refresh_hinode_self(inode);
8901 +               }
8902 +               au_dbg_verify_dinode(dentry);
8903 +               if (!err)
8904 +                       goto out_dgen; /* success */
8905 +               goto out;
8906 +       }
8907 +
8908 +       /* temporary dinfo */
8909 +       AuDbgDentry(dentry);
8910 +       err = -ENOMEM;
8911 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8912 +       if (unlikely(!tmp))
8913 +               goto out;
8914 +       au_di_swap(tmp, dinfo);
8915 +       /* returns the number of positive dentries */
8916 +       /*
8917 +        * if current working dir is removed, it returns an error.
8918 +        * but the dentry is legal.
8919 +        */
8920 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8921 +       AuDbgDentry(dentry);
8922 +       au_di_swap(tmp, dinfo);
8923 +       if (err == -ENOENT)
8924 +               err = 0;
8925 +       if (err >= 0) {
8926 +               /* compare/refresh by dinfo */
8927 +               AuDbgDentry(dentry);
8928 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8929 +               au_dbg_verify_dinode(dentry);
8930 +               AuTraceErr(err);
8931 +       }
8932 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8933 +       au_rw_write_unlock(&tmp->di_rwsem);
8934 +       au_di_free(tmp);
8935 +       if (unlikely(err))
8936 +               goto out;
8937 +
8938 +out_dgen:
8939 +       au_update_digen(dentry);
8940 +out:
8941 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8942 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8943 +               AuDbgDentry(dentry);
8944 +       }
8945 +       AuTraceErr(err);
8946 +       return err;
8947 +}
8948 +
8949 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8950 +                          struct dentry *dentry, aufs_bindex_t bindex)
8951 +{
8952 +       int err, valid;
8953 +
8954 +       err = 0;
8955 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8956 +               goto out;
8957 +
8958 +       AuDbg("b%d\n", bindex);
8959 +       /*
8960 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8961 +        * due to whiteout and branch permission.
8962 +        */
8963 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8964 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8965 +       /* it may return tri-state */
8966 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8967 +
8968 +       if (unlikely(valid < 0))
8969 +               err = valid;
8970 +       else if (!valid)
8971 +               err = -EINVAL;
8972 +
8973 +out:
8974 +       AuTraceErr(err);
8975 +       return err;
8976 +}
8977 +
8978 +/* todo: remove this */
8979 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8980 +                         unsigned int flags, int do_udba, int dirren)
8981 +{
8982 +       int err;
8983 +       umode_t mode, h_mode;
8984 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8985 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8986 +       struct inode *h_inode, *h_cached_inode;
8987 +       struct dentry *h_dentry;
8988 +       struct qstr *name, *h_name;
8989 +
8990 +       err = 0;
8991 +       plus = 0;
8992 +       mode = 0;
8993 +       ibs = -1;
8994 +       ibe = -1;
8995 +       unhashed = !!d_unhashed(dentry);
8996 +       is_root = !!IS_ROOT(dentry);
8997 +       name = &dentry->d_name;
8998 +       tmpfile = au_di(dentry)->di_tmpfile;
8999 +
9000 +       /*
9001 +        * Theoretically, REVAL test should be unnecessary in case of
9002 +        * {FS,I}NOTIFY.
9003 +        * But {fs,i}notify doesn't fire some necessary events,
9004 +        *      IN_ATTRIB for atime/nlink/pageio
9005 +        * Let's do REVAL test too.
9006 +        */
9007 +       if (do_udba && inode) {
9008 +               mode = (inode->i_mode & S_IFMT);
9009 +               plus = (inode->i_nlink > 0);
9010 +               ibs = au_ibtop(inode);
9011 +               ibe = au_ibbot(inode);
9012 +       }
9013 +
9014 +       btop = au_dbtop(dentry);
9015 +       btail = btop;
9016 +       if (inode && S_ISDIR(inode->i_mode))
9017 +               btail = au_dbtaildir(dentry);
9018 +       for (bindex = btop; bindex <= btail; bindex++) {
9019 +               h_dentry = au_h_dptr(dentry, bindex);
9020 +               if (!h_dentry)
9021 +                       continue;
9022 +
9023 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
9024 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
9025 +               spin_lock(&h_dentry->d_lock);
9026 +               h_name = &h_dentry->d_name;
9027 +               if (unlikely(do_udba
9028 +                            && !is_root
9029 +                            && ((!h_nfs
9030 +                                 && (unhashed != !!d_unhashed(h_dentry)
9031 +                                     || (!tmpfile && !dirren
9032 +                                         && !au_qstreq(name, h_name))
9033 +                                         ))
9034 +                                || (h_nfs
9035 +                                    && !(flags & LOOKUP_OPEN)
9036 +                                    && (h_dentry->d_flags
9037 +                                        & DCACHE_NFSFS_RENAMED)))
9038 +                           )) {
9039 +                       int h_unhashed;
9040 +
9041 +                       h_unhashed = d_unhashed(h_dentry);
9042 +                       spin_unlock(&h_dentry->d_lock);
9043 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9044 +                             unhashed, h_unhashed, dentry, h_dentry);
9045 +                       goto err;
9046 +               }
9047 +               spin_unlock(&h_dentry->d_lock);
9048 +
9049 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
9050 +               if (unlikely(err))
9051 +                       /* do not goto err, to keep the errno */
9052 +                       break;
9053 +
9054 +               /* todo: plink too? */
9055 +               if (!do_udba)
9056 +                       continue;
9057 +
9058 +               /* UDBA tests */
9059 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
9060 +                       goto err;
9061 +
9062 +               h_inode = NULL;
9063 +               if (d_is_positive(h_dentry))
9064 +                       h_inode = d_inode(h_dentry);
9065 +               h_plus = plus;
9066 +               h_mode = mode;
9067 +               h_cached_inode = h_inode;
9068 +               if (h_inode) {
9069 +                       h_mode = (h_inode->i_mode & S_IFMT);
9070 +                       h_plus = (h_inode->i_nlink > 0);
9071 +               }
9072 +               if (inode && ibs <= bindex && bindex <= ibe)
9073 +                       h_cached_inode = au_h_iptr(inode, bindex);
9074 +
9075 +               if (!h_nfs) {
9076 +                       if (unlikely(plus != h_plus && !tmpfile))
9077 +                               goto err;
9078 +               } else {
9079 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9080 +                                    && !is_root
9081 +                                    && !IS_ROOT(h_dentry)
9082 +                                    && unhashed != d_unhashed(h_dentry)))
9083 +                               goto err;
9084 +               }
9085 +               if (unlikely(mode != h_mode
9086 +                            || h_cached_inode != h_inode))
9087 +                       goto err;
9088 +               continue;
9089 +
9090 +err:
9091 +               err = -EINVAL;
9092 +               break;
9093 +       }
9094 +
9095 +       AuTraceErr(err);
9096 +       return err;
9097 +}
9098 +
9099 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9100 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9101 +{
9102 +       int err;
9103 +       struct dentry *parent;
9104 +
9105 +       if (!au_digen_test(dentry, sigen))
9106 +               return 0;
9107 +
9108 +       parent = dget_parent(dentry);
9109 +       di_read_lock_parent(parent, AuLock_IR);
9110 +       AuDebugOn(au_digen_test(parent, sigen));
9111 +       au_dbg_verify_gen(parent, sigen);
9112 +       err = au_refresh_dentry(dentry, parent);
9113 +       di_read_unlock(parent, AuLock_IR);
9114 +       dput(parent);
9115 +       AuTraceErr(err);
9116 +       return err;
9117 +}
9118 +
9119 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9120 +{
9121 +       int err;
9122 +       struct dentry *d, *parent;
9123 +
9124 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9125 +               return simple_reval_dpath(dentry, sigen);
9126 +
9127 +       /* slow loop, keep it simple and stupid */
9128 +       /* cf: au_cpup_dirs() */
9129 +       err = 0;
9130 +       parent = NULL;
9131 +       while (au_digen_test(dentry, sigen)) {
9132 +               d = dentry;
9133 +               while (1) {
9134 +                       dput(parent);
9135 +                       parent = dget_parent(d);
9136 +                       if (!au_digen_test(parent, sigen))
9137 +                               break;
9138 +                       d = parent;
9139 +               }
9140 +
9141 +               if (d != dentry)
9142 +                       di_write_lock_child2(d);
9143 +
9144 +               /* someone might update our dentry while we were sleeping */
9145 +               if (au_digen_test(d, sigen)) {
9146 +                       /*
9147 +                        * todo: consolidate with simple_reval_dpath(),
9148 +                        * do_refresh() and au_reval_for_attr().
9149 +                        */
9150 +                       di_read_lock_parent(parent, AuLock_IR);
9151 +                       err = au_refresh_dentry(d, parent);
9152 +                       di_read_unlock(parent, AuLock_IR);
9153 +               }
9154 +
9155 +               if (d != dentry)
9156 +                       di_write_unlock(d);
9157 +               dput(parent);
9158 +               if (unlikely(err))
9159 +                       break;
9160 +       }
9161 +
9162 +       return err;
9163 +}
9164 +
9165 +/*
9166 + * if valid returns 1, otherwise 0.
9167 + */
9168 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9169 +{
9170 +       int valid, err;
9171 +       unsigned int sigen;
9172 +       unsigned char do_udba, dirren;
9173 +       struct super_block *sb;
9174 +       struct inode *inode;
9175 +
9176 +       /* todo: support rcu-walk? */
9177 +       if (flags & LOOKUP_RCU)
9178 +               return -ECHILD;
9179 +
9180 +       valid = 0;
9181 +       if (unlikely(!au_di(dentry)))
9182 +               goto out;
9183 +
9184 +       valid = 1;
9185 +       sb = dentry->d_sb;
9186 +       /*
9187 +        * todo: very ugly
9188 +        * i_mutex of parent dir may be held,
9189 +        * but we should not return 'invalid' due to busy.
9190 +        */
9191 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9192 +       if (unlikely(err)) {
9193 +               valid = err;
9194 +               AuTraceErr(err);
9195 +               goto out;
9196 +       }
9197 +       inode = NULL;
9198 +       if (d_really_is_positive(dentry))
9199 +               inode = d_inode(dentry);
9200 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9201 +               err = -EINVAL;
9202 +               AuTraceErr(err);
9203 +               goto out_dgrade;
9204 +       }
9205 +       if (unlikely(au_dbrange_test(dentry))) {
9206 +               err = -EINVAL;
9207 +               AuTraceErr(err);
9208 +               goto out_dgrade;
9209 +       }
9210 +
9211 +       sigen = au_sigen(sb);
9212 +       if (au_digen_test(dentry, sigen)) {
9213 +               AuDebugOn(IS_ROOT(dentry));
9214 +               err = au_reval_dpath(dentry, sigen);
9215 +               if (unlikely(err)) {
9216 +                       AuTraceErr(err);
9217 +                       goto out_dgrade;
9218 +               }
9219 +       }
9220 +       di_downgrade_lock(dentry, AuLock_IR);
9221 +
9222 +       err = -EINVAL;
9223 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9224 +           && inode
9225 +           && !(inode->i_state && I_LINKABLE)
9226 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9227 +               AuTraceErr(err);
9228 +               goto out_inval;
9229 +       }
9230 +
9231 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9232 +       if (do_udba && inode) {
9233 +               aufs_bindex_t btop = au_ibtop(inode);
9234 +               struct inode *h_inode;
9235 +
9236 +               if (btop >= 0) {
9237 +                       h_inode = au_h_iptr(inode, btop);
9238 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9239 +                               AuTraceErr(err);
9240 +                               goto out_inval;
9241 +                       }
9242 +               }
9243 +       }
9244 +
9245 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9246 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9247 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9248 +               err = -EIO;
9249 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9250 +                     dentry, err);
9251 +       }
9252 +       goto out_inval;
9253 +
9254 +out_dgrade:
9255 +       di_downgrade_lock(dentry, AuLock_IR);
9256 +out_inval:
9257 +       aufs_read_unlock(dentry, AuLock_IR);
9258 +       AuTraceErr(err);
9259 +       valid = !err;
9260 +out:
9261 +       if (!valid) {
9262 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9263 +               d_drop(dentry);
9264 +       }
9265 +       return valid;
9266 +}
9267 +
9268 +static void aufs_d_release(struct dentry *dentry)
9269 +{
9270 +       if (au_di(dentry)) {
9271 +               au_di_fin(dentry);
9272 +               au_hn_di_reinit(dentry);
9273 +       }
9274 +}
9275 +
9276 +const struct dentry_operations aufs_dop = {
9277 +       .d_revalidate           = aufs_d_revalidate,
9278 +       .d_weak_revalidate      = aufs_d_revalidate,
9279 +       .d_release              = aufs_d_release
9280 +};
9281 +
9282 +/* aufs_dop without d_revalidate */
9283 +const struct dentry_operations aufs_dop_noreval = {
9284 +       .d_release              = aufs_d_release
9285 +};
9286 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9287 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9288 +++ linux/fs/aufs/dentry.h      2018-07-19 09:46:13.056647808 +0200
9289 @@ -0,0 +1,267 @@
9290 +/* SPDX-License-Identifier: GPL-2.0 */
9291 +/*
9292 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9293 + *
9294 + * This program, aufs is free software; you can redistribute it and/or modify
9295 + * it under the terms of the GNU General Public License as published by
9296 + * the Free Software Foundation; either version 2 of the License, or
9297 + * (at your option) any later version.
9298 + *
9299 + * This program is distributed in the hope that it will be useful,
9300 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9301 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9302 + * GNU General Public License for more details.
9303 + *
9304 + * You should have received a copy of the GNU General Public License
9305 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9306 + */
9307 +
9308 +/*
9309 + * lookup and dentry operations
9310 + */
9311 +
9312 +#ifndef __AUFS_DENTRY_H__
9313 +#define __AUFS_DENTRY_H__
9314 +
9315 +#ifdef __KERNEL__
9316 +
9317 +#include <linux/dcache.h>
9318 +#include "dirren.h"
9319 +#include "rwsem.h"
9320 +
9321 +struct au_hdentry {
9322 +       struct dentry           *hd_dentry;
9323 +       aufs_bindex_t           hd_id;
9324 +};
9325 +
9326 +struct au_dinfo {
9327 +       atomic_t                di_generation;
9328 +
9329 +       struct au_rwsem         di_rwsem;
9330 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9331 +       unsigned char           di_tmpfile; /* to allow the different name */
9332 +       struct au_hdentry       *di_hdentry;
9333 +} ____cacheline_aligned_in_smp;
9334 +
9335 +/* ---------------------------------------------------------------------- */
9336 +
9337 +/* flags for au_lkup_dentry() */
9338 +#define AuLkup_ALLOW_NEG       1
9339 +#define AuLkup_IGNORE_PERM     (1 << 1)
9340 +#define AuLkup_DIRREN          (1 << 2)
9341 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9342 +#define au_fset_lkup(flags, name) \
9343 +       do { (flags) |= AuLkup_##name; } while (0)
9344 +#define au_fclr_lkup(flags, name) \
9345 +       do { (flags) &= ~AuLkup_##name; } while (0)
9346 +
9347 +#ifndef CONFIG_AUFS_DIRREN
9348 +#undef AuLkup_DIRREN
9349 +#define AuLkup_DIRREN 0
9350 +#endif
9351 +
9352 +struct au_do_lookup_args {
9353 +       unsigned int            flags;
9354 +       mode_t                  type;
9355 +       struct qstr             whname, *name;
9356 +       struct au_dr_lookup     dirren;
9357 +};
9358 +
9359 +/* ---------------------------------------------------------------------- */
9360 +
9361 +/* dentry.c */
9362 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9363 +struct au_branch;
9364 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9365 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9366 +               struct dentry *h_parent, struct au_branch *br);
9367 +
9368 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9369 +                  unsigned int flags);
9370 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9371 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9372 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9373 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9374 +
9375 +/* dinfo.c */
9376 +void au_di_init_once(void *_di);
9377 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9378 +void au_di_free(struct au_dinfo *dinfo);
9379 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9380 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9381 +int au_di_init(struct dentry *dentry);
9382 +void au_di_fin(struct dentry *dentry);
9383 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9384 +
9385 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9386 +void di_read_unlock(struct dentry *d, int flags);
9387 +void di_downgrade_lock(struct dentry *d, int flags);
9388 +void di_write_lock(struct dentry *d, unsigned int lsc);
9389 +void di_write_unlock(struct dentry *d);
9390 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9391 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9392 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9393 +
9394 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9395 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9396 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9397 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9398 +
9399 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9400 +                  struct dentry *h_dentry);
9401 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9402 +int au_dbrange_test(struct dentry *dentry);
9403 +void au_update_digen(struct dentry *dentry);
9404 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9405 +void au_update_dbtop(struct dentry *dentry);
9406 +void au_update_dbbot(struct dentry *dentry);
9407 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9408 +
9409 +/* ---------------------------------------------------------------------- */
9410 +
9411 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9412 +{
9413 +       return dentry->d_fsdata;
9414 +}
9415 +
9416 +/* ---------------------------------------------------------------------- */
9417 +
9418 +/* lock subclass for dinfo */
9419 +enum {
9420 +       AuLsc_DI_CHILD,         /* child first */
9421 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9422 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9423 +       AuLsc_DI_PARENT,
9424 +       AuLsc_DI_PARENT2,
9425 +       AuLsc_DI_PARENT3,
9426 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9427 +};
9428 +
9429 +/*
9430 + * di_read_lock_child, di_write_lock_child,
9431 + * di_read_lock_child2, di_write_lock_child2,
9432 + * di_read_lock_child3, di_write_lock_child3,
9433 + * di_read_lock_parent, di_write_lock_parent,
9434 + * di_read_lock_parent2, di_write_lock_parent2,
9435 + * di_read_lock_parent3, di_write_lock_parent3,
9436 + */
9437 +#define AuReadLockFunc(name, lsc) \
9438 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9439 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9440 +
9441 +#define AuWriteLockFunc(name, lsc) \
9442 +static inline void di_write_lock_##name(struct dentry *d) \
9443 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9444 +
9445 +#define AuRWLockFuncs(name, lsc) \
9446 +       AuReadLockFunc(name, lsc) \
9447 +       AuWriteLockFunc(name, lsc)
9448 +
9449 +AuRWLockFuncs(child, CHILD);
9450 +AuRWLockFuncs(child2, CHILD2);
9451 +AuRWLockFuncs(child3, CHILD3);
9452 +AuRWLockFuncs(parent, PARENT);
9453 +AuRWLockFuncs(parent2, PARENT2);
9454 +AuRWLockFuncs(parent3, PARENT3);
9455 +
9456 +#undef AuReadLockFunc
9457 +#undef AuWriteLockFunc
9458 +#undef AuRWLockFuncs
9459 +
9460 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9461 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9462 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9463 +
9464 +/* ---------------------------------------------------------------------- */
9465 +
9466 +/* todo: memory barrier? */
9467 +static inline unsigned int au_digen(struct dentry *d)
9468 +{
9469 +       return atomic_read(&au_di(d)->di_generation);
9470 +}
9471 +
9472 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9473 +{
9474 +       hdentry->hd_dentry = NULL;
9475 +}
9476 +
9477 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9478 +                                           aufs_bindex_t bindex)
9479 +{
9480 +       return di->di_hdentry + bindex;
9481 +}
9482 +
9483 +static inline void au_hdput(struct au_hdentry *hd)
9484 +{
9485 +       if (hd)
9486 +               dput(hd->hd_dentry);
9487 +}
9488 +
9489 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9490 +{
9491 +       DiMustAnyLock(dentry);
9492 +       return au_di(dentry)->di_btop;
9493 +}
9494 +
9495 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9496 +{
9497 +       DiMustAnyLock(dentry);
9498 +       return au_di(dentry)->di_bbot;
9499 +}
9500 +
9501 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9502 +{
9503 +       DiMustAnyLock(dentry);
9504 +       return au_di(dentry)->di_bwh;
9505 +}
9506 +
9507 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9508 +{
9509 +       DiMustAnyLock(dentry);
9510 +       return au_di(dentry)->di_bdiropq;
9511 +}
9512 +
9513 +/* todo: hard/soft set? */
9514 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9515 +{
9516 +       DiMustWriteLock(dentry);
9517 +       au_di(dentry)->di_btop = bindex;
9518 +}
9519 +
9520 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9521 +{
9522 +       DiMustWriteLock(dentry);
9523 +       au_di(dentry)->di_bbot = bindex;
9524 +}
9525 +
9526 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9527 +{
9528 +       DiMustWriteLock(dentry);
9529 +       /* dbwh can be outside of btop - bbot range */
9530 +       au_di(dentry)->di_bwh = bindex;
9531 +}
9532 +
9533 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9534 +{
9535 +       DiMustWriteLock(dentry);
9536 +       au_di(dentry)->di_bdiropq = bindex;
9537 +}
9538 +
9539 +/* ---------------------------------------------------------------------- */
9540 +
9541 +#ifdef CONFIG_AUFS_HNOTIFY
9542 +static inline void au_digen_dec(struct dentry *d)
9543 +{
9544 +       atomic_dec(&au_di(d)->di_generation);
9545 +}
9546 +
9547 +static inline void au_hn_di_reinit(struct dentry *dentry)
9548 +{
9549 +       dentry->d_fsdata = NULL;
9550 +}
9551 +#else
9552 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9553 +#endif /* CONFIG_AUFS_HNOTIFY */
9554 +
9555 +#endif /* __KERNEL__ */
9556 +#endif /* __AUFS_DENTRY_H__ */
9557 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9558 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9559 +++ linux/fs/aufs/dinfo.c       2018-07-19 09:46:13.056647808 +0200
9560 @@ -0,0 +1,554 @@
9561 +/* SPDX-License-Identifier: GPL-2.0 */
9562 +/*
9563 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9564 + *
9565 + * This program, aufs is free software; you can redistribute it and/or modify
9566 + * it under the terms of the GNU General Public License as published by
9567 + * the Free Software Foundation; either version 2 of the License, or
9568 + * (at your option) any later version.
9569 + *
9570 + * This program is distributed in the hope that it will be useful,
9571 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9572 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9573 + * GNU General Public License for more details.
9574 + *
9575 + * You should have received a copy of the GNU General Public License
9576 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9577 + */
9578 +
9579 +/*
9580 + * dentry private data
9581 + */
9582 +
9583 +#include "aufs.h"
9584 +
9585 +void au_di_init_once(void *_dinfo)
9586 +{
9587 +       struct au_dinfo *dinfo = _dinfo;
9588 +
9589 +       au_rw_init(&dinfo->di_rwsem);
9590 +}
9591 +
9592 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9593 +{
9594 +       struct au_dinfo *dinfo;
9595 +       int nbr, i;
9596 +
9597 +       dinfo = au_cache_alloc_dinfo();
9598 +       if (unlikely(!dinfo))
9599 +               goto out;
9600 +
9601 +       nbr = au_sbbot(sb) + 1;
9602 +       if (nbr <= 0)
9603 +               nbr = 1;
9604 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9605 +       if (dinfo->di_hdentry) {
9606 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9607 +               dinfo->di_btop = -1;
9608 +               dinfo->di_bbot = -1;
9609 +               dinfo->di_bwh = -1;
9610 +               dinfo->di_bdiropq = -1;
9611 +               dinfo->di_tmpfile = 0;
9612 +               for (i = 0; i < nbr; i++)
9613 +                       dinfo->di_hdentry[i].hd_id = -1;
9614 +               goto out;
9615 +       }
9616 +
9617 +       au_cache_free_dinfo(dinfo);
9618 +       dinfo = NULL;
9619 +
9620 +out:
9621 +       return dinfo;
9622 +}
9623 +
9624 +void au_di_free(struct au_dinfo *dinfo)
9625 +{
9626 +       struct au_hdentry *p;
9627 +       aufs_bindex_t bbot, bindex;
9628 +
9629 +       /* dentry may not be revalidated */
9630 +       bindex = dinfo->di_btop;
9631 +       if (bindex >= 0) {
9632 +               bbot = dinfo->di_bbot;
9633 +               p = au_hdentry(dinfo, bindex);
9634 +               while (bindex++ <= bbot)
9635 +                       au_hdput(p++);
9636 +       }
9637 +       kfree(dinfo->di_hdentry);
9638 +       au_cache_free_dinfo(dinfo);
9639 +}
9640 +
9641 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9642 +{
9643 +       struct au_hdentry *p;
9644 +       aufs_bindex_t bi;
9645 +
9646 +       AuRwMustWriteLock(&a->di_rwsem);
9647 +       AuRwMustWriteLock(&b->di_rwsem);
9648 +
9649 +#define DiSwap(v, name)                                \
9650 +       do {                                    \
9651 +               v = a->di_##name;               \
9652 +               a->di_##name = b->di_##name;    \
9653 +               b->di_##name = v;               \
9654 +       } while (0)
9655 +
9656 +       DiSwap(p, hdentry);
9657 +       DiSwap(bi, btop);
9658 +       DiSwap(bi, bbot);
9659 +       DiSwap(bi, bwh);
9660 +       DiSwap(bi, bdiropq);
9661 +       /* smp_mb(); */
9662 +
9663 +#undef DiSwap
9664 +}
9665 +
9666 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9667 +{
9668 +       AuRwMustWriteLock(&dst->di_rwsem);
9669 +       AuRwMustWriteLock(&src->di_rwsem);
9670 +
9671 +       dst->di_btop = src->di_btop;
9672 +       dst->di_bbot = src->di_bbot;
9673 +       dst->di_bwh = src->di_bwh;
9674 +       dst->di_bdiropq = src->di_bdiropq;
9675 +       /* smp_mb(); */
9676 +}
9677 +
9678 +int au_di_init(struct dentry *dentry)
9679 +{
9680 +       int err;
9681 +       struct super_block *sb;
9682 +       struct au_dinfo *dinfo;
9683 +
9684 +       err = 0;
9685 +       sb = dentry->d_sb;
9686 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9687 +       if (dinfo) {
9688 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9689 +               /* smp_mb(); */ /* atomic_set */
9690 +               dentry->d_fsdata = dinfo;
9691 +       } else
9692 +               err = -ENOMEM;
9693 +
9694 +       return err;
9695 +}
9696 +
9697 +void au_di_fin(struct dentry *dentry)
9698 +{
9699 +       struct au_dinfo *dinfo;
9700 +
9701 +       dinfo = au_di(dentry);
9702 +       AuRwDestroy(&dinfo->di_rwsem);
9703 +       au_di_free(dinfo);
9704 +}
9705 +
9706 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9707 +{
9708 +       int err, sz;
9709 +       struct au_hdentry *hdp;
9710 +
9711 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9712 +
9713 +       err = -ENOMEM;
9714 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9715 +       if (!sz)
9716 +               sz = sizeof(*hdp);
9717 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9718 +                          may_shrink);
9719 +       if (hdp) {
9720 +               dinfo->di_hdentry = hdp;
9721 +               err = 0;
9722 +       }
9723 +
9724 +       return err;
9725 +}
9726 +
9727 +/* ---------------------------------------------------------------------- */
9728 +
9729 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9730 +{
9731 +       switch (lsc) {
9732 +       case AuLsc_DI_CHILD:
9733 +               ii_write_lock_child(inode);
9734 +               break;
9735 +       case AuLsc_DI_CHILD2:
9736 +               ii_write_lock_child2(inode);
9737 +               break;
9738 +       case AuLsc_DI_CHILD3:
9739 +               ii_write_lock_child3(inode);
9740 +               break;
9741 +       case AuLsc_DI_PARENT:
9742 +               ii_write_lock_parent(inode);
9743 +               break;
9744 +       case AuLsc_DI_PARENT2:
9745 +               ii_write_lock_parent2(inode);
9746 +               break;
9747 +       case AuLsc_DI_PARENT3:
9748 +               ii_write_lock_parent3(inode);
9749 +               break;
9750 +       default:
9751 +               BUG();
9752 +       }
9753 +}
9754 +
9755 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9756 +{
9757 +       switch (lsc) {
9758 +       case AuLsc_DI_CHILD:
9759 +               ii_read_lock_child(inode);
9760 +               break;
9761 +       case AuLsc_DI_CHILD2:
9762 +               ii_read_lock_child2(inode);
9763 +               break;
9764 +       case AuLsc_DI_CHILD3:
9765 +               ii_read_lock_child3(inode);
9766 +               break;
9767 +       case AuLsc_DI_PARENT:
9768 +               ii_read_lock_parent(inode);
9769 +               break;
9770 +       case AuLsc_DI_PARENT2:
9771 +               ii_read_lock_parent2(inode);
9772 +               break;
9773 +       case AuLsc_DI_PARENT3:
9774 +               ii_read_lock_parent3(inode);
9775 +               break;
9776 +       default:
9777 +               BUG();
9778 +       }
9779 +}
9780 +
9781 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9782 +{
9783 +       struct inode *inode;
9784 +
9785 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9786 +       if (d_really_is_positive(d)) {
9787 +               inode = d_inode(d);
9788 +               if (au_ftest_lock(flags, IW))
9789 +                       do_ii_write_lock(inode, lsc);
9790 +               else if (au_ftest_lock(flags, IR))
9791 +                       do_ii_read_lock(inode, lsc);
9792 +       }
9793 +}
9794 +
9795 +void di_read_unlock(struct dentry *d, int flags)
9796 +{
9797 +       struct inode *inode;
9798 +
9799 +       if (d_really_is_positive(d)) {
9800 +               inode = d_inode(d);
9801 +               if (au_ftest_lock(flags, IW)) {
9802 +                       au_dbg_verify_dinode(d);
9803 +                       ii_write_unlock(inode);
9804 +               } else if (au_ftest_lock(flags, IR)) {
9805 +                       au_dbg_verify_dinode(d);
9806 +                       ii_read_unlock(inode);
9807 +               }
9808 +       }
9809 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9810 +}
9811 +
9812 +void di_downgrade_lock(struct dentry *d, int flags)
9813 +{
9814 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9815 +               ii_downgrade_lock(d_inode(d));
9816 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9817 +}
9818 +
9819 +void di_write_lock(struct dentry *d, unsigned int lsc)
9820 +{
9821 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9822 +       if (d_really_is_positive(d))
9823 +               do_ii_write_lock(d_inode(d), lsc);
9824 +}
9825 +
9826 +void di_write_unlock(struct dentry *d)
9827 +{
9828 +       au_dbg_verify_dinode(d);
9829 +       if (d_really_is_positive(d))
9830 +               ii_write_unlock(d_inode(d));
9831 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9832 +}
9833 +
9834 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9835 +{
9836 +       AuDebugOn(d1 == d2
9837 +                 || d_inode(d1) == d_inode(d2)
9838 +                 || d1->d_sb != d2->d_sb);
9839 +
9840 +       if ((isdir && au_test_subdir(d1, d2))
9841 +           || d1 < d2) {
9842 +               di_write_lock_child(d1);
9843 +               di_write_lock_child2(d2);
9844 +       } else {
9845 +               di_write_lock_child(d2);
9846 +               di_write_lock_child2(d1);
9847 +       }
9848 +}
9849 +
9850 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9851 +{
9852 +       AuDebugOn(d1 == d2
9853 +                 || d_inode(d1) == d_inode(d2)
9854 +                 || d1->d_sb != d2->d_sb);
9855 +
9856 +       if ((isdir && au_test_subdir(d1, d2))
9857 +           || d1 < d2) {
9858 +               di_write_lock_parent(d1);
9859 +               di_write_lock_parent2(d2);
9860 +       } else {
9861 +               di_write_lock_parent(d2);
9862 +               di_write_lock_parent2(d1);
9863 +       }
9864 +}
9865 +
9866 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9867 +{
9868 +       di_write_unlock(d1);
9869 +       if (d_inode(d1) == d_inode(d2))
9870 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9871 +       else
9872 +               di_write_unlock(d2);
9873 +}
9874 +
9875 +/* ---------------------------------------------------------------------- */
9876 +
9877 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9878 +{
9879 +       struct dentry *d;
9880 +
9881 +       DiMustAnyLock(dentry);
9882 +
9883 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9884 +               return NULL;
9885 +       AuDebugOn(bindex < 0);
9886 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9887 +       AuDebugOn(d && au_dcount(d) <= 0);
9888 +       return d;
9889 +}
9890 +
9891 +/*
9892 + * extended version of au_h_dptr().
9893 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9894 + * error.
9895 + */
9896 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9897 +{
9898 +       struct dentry *h_dentry;
9899 +       struct inode *inode, *h_inode;
9900 +
9901 +       AuDebugOn(d_really_is_negative(dentry));
9902 +
9903 +       h_dentry = NULL;
9904 +       if (au_dbtop(dentry) <= bindex
9905 +           && bindex <= au_dbbot(dentry))
9906 +               h_dentry = au_h_dptr(dentry, bindex);
9907 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9908 +               dget(h_dentry);
9909 +               goto out; /* success */
9910 +       }
9911 +
9912 +       inode = d_inode(dentry);
9913 +       AuDebugOn(bindex < au_ibtop(inode));
9914 +       AuDebugOn(au_ibbot(inode) < bindex);
9915 +       h_inode = au_h_iptr(inode, bindex);
9916 +       h_dentry = d_find_alias(h_inode);
9917 +       if (h_dentry) {
9918 +               if (!IS_ERR(h_dentry)) {
9919 +                       if (!au_d_linkable(h_dentry))
9920 +                               goto out; /* success */
9921 +                       dput(h_dentry);
9922 +               } else
9923 +                       goto out;
9924 +       }
9925 +
9926 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9927 +               h_dentry = au_plink_lkup(inode, bindex);
9928 +               AuDebugOn(!h_dentry);
9929 +               if (!IS_ERR(h_dentry)) {
9930 +                       if (!au_d_hashed_positive(h_dentry))
9931 +                               goto out; /* success */
9932 +                       dput(h_dentry);
9933 +                       h_dentry = NULL;
9934 +               }
9935 +       }
9936 +
9937 +out:
9938 +       AuDbgDentry(h_dentry);
9939 +       return h_dentry;
9940 +}
9941 +
9942 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9943 +{
9944 +       aufs_bindex_t bbot, bwh;
9945 +
9946 +       bbot = au_dbbot(dentry);
9947 +       if (0 <= bbot) {
9948 +               bwh = au_dbwh(dentry);
9949 +               if (!bwh)
9950 +                       return bwh;
9951 +               if (0 < bwh && bwh < bbot)
9952 +                       return bwh - 1;
9953 +       }
9954 +       return bbot;
9955 +}
9956 +
9957 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9958 +{
9959 +       aufs_bindex_t bbot, bopq;
9960 +
9961 +       bbot = au_dbtail(dentry);
9962 +       if (0 <= bbot) {
9963 +               bopq = au_dbdiropq(dentry);
9964 +               if (0 <= bopq && bopq < bbot)
9965 +                       bbot = bopq;
9966 +       }
9967 +       return bbot;
9968 +}
9969 +
9970 +/* ---------------------------------------------------------------------- */
9971 +
9972 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9973 +                  struct dentry *h_dentry)
9974 +{
9975 +       struct au_dinfo *dinfo;
9976 +       struct au_hdentry *hd;
9977 +       struct au_branch *br;
9978 +
9979 +       DiMustWriteLock(dentry);
9980 +
9981 +       dinfo = au_di(dentry);
9982 +       hd = au_hdentry(dinfo, bindex);
9983 +       au_hdput(hd);
9984 +       hd->hd_dentry = h_dentry;
9985 +       if (h_dentry) {
9986 +               br = au_sbr(dentry->d_sb, bindex);
9987 +               hd->hd_id = br->br_id;
9988 +       }
9989 +}
9990 +
9991 +int au_dbrange_test(struct dentry *dentry)
9992 +{
9993 +       int err;
9994 +       aufs_bindex_t btop, bbot;
9995 +
9996 +       err = 0;
9997 +       btop = au_dbtop(dentry);
9998 +       bbot = au_dbbot(dentry);
9999 +       if (btop >= 0)
10000 +               AuDebugOn(bbot < 0 && btop > bbot);
10001 +       else {
10002 +               err = -EIO;
10003 +               AuDebugOn(bbot >= 0);
10004 +       }
10005 +
10006 +       return err;
10007 +}
10008 +
10009 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
10010 +{
10011 +       int err;
10012 +
10013 +       err = 0;
10014 +       if (unlikely(au_digen(dentry) != sigen
10015 +                    || au_iigen_test(d_inode(dentry), sigen)))
10016 +               err = -EIO;
10017 +
10018 +       return err;
10019 +}
10020 +
10021 +void au_update_digen(struct dentry *dentry)
10022 +{
10023 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10024 +       /* smp_mb(); */ /* atomic_set */
10025 +}
10026 +
10027 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10028 +{
10029 +       struct au_dinfo *dinfo;
10030 +       struct dentry *h_d;
10031 +       struct au_hdentry *hdp;
10032 +       aufs_bindex_t bindex, bbot;
10033 +
10034 +       DiMustWriteLock(dentry);
10035 +
10036 +       dinfo = au_di(dentry);
10037 +       if (!dinfo || dinfo->di_btop < 0)
10038 +               return;
10039 +
10040 +       if (do_put_zero) {
10041 +               bbot = dinfo->di_bbot;
10042 +               bindex = dinfo->di_btop;
10043 +               hdp = au_hdentry(dinfo, bindex);
10044 +               for (; bindex <= bbot; bindex++, hdp++) {
10045 +                       h_d = hdp->hd_dentry;
10046 +                       if (h_d && d_is_negative(h_d))
10047 +                               au_set_h_dptr(dentry, bindex, NULL);
10048 +               }
10049 +       }
10050 +
10051 +       dinfo->di_btop = 0;
10052 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
10053 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10054 +               if (hdp->hd_dentry)
10055 +                       break;
10056 +       if (dinfo->di_btop > dinfo->di_bbot) {
10057 +               dinfo->di_btop = -1;
10058 +               dinfo->di_bbot = -1;
10059 +               return;
10060 +       }
10061 +
10062 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
10063 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10064 +               if (hdp->hd_dentry)
10065 +                       break;
10066 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
10067 +}
10068 +
10069 +void au_update_dbtop(struct dentry *dentry)
10070 +{
10071 +       aufs_bindex_t bindex, bbot;
10072 +       struct dentry *h_dentry;
10073 +
10074 +       bbot = au_dbbot(dentry);
10075 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10076 +               h_dentry = au_h_dptr(dentry, bindex);
10077 +               if (!h_dentry)
10078 +                       continue;
10079 +               if (d_is_positive(h_dentry)) {
10080 +                       au_set_dbtop(dentry, bindex);
10081 +                       return;
10082 +               }
10083 +               au_set_h_dptr(dentry, bindex, NULL);
10084 +       }
10085 +}
10086 +
10087 +void au_update_dbbot(struct dentry *dentry)
10088 +{
10089 +       aufs_bindex_t bindex, btop;
10090 +       struct dentry *h_dentry;
10091 +
10092 +       btop = au_dbtop(dentry);
10093 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10094 +               h_dentry = au_h_dptr(dentry, bindex);
10095 +               if (!h_dentry)
10096 +                       continue;
10097 +               if (d_is_positive(h_dentry)) {
10098 +                       au_set_dbbot(dentry, bindex);
10099 +                       return;
10100 +               }
10101 +               au_set_h_dptr(dentry, bindex, NULL);
10102 +       }
10103 +}
10104 +
10105 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10106 +{
10107 +       aufs_bindex_t bindex, bbot;
10108 +
10109 +       bbot = au_dbbot(dentry);
10110 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10111 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10112 +                       return bindex;
10113 +       return -1;
10114 +}
10115 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10116 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10117 +++ linux/fs/aufs/dir.c 2018-07-19 09:46:13.056647808 +0200
10118 @@ -0,0 +1,760 @@
10119 +/* SPDX-License-Identifier: GPL-2.0 */
10120 +/*
10121 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10122 + *
10123 + * This program, aufs is free software; you can redistribute it and/or modify
10124 + * it under the terms of the GNU General Public License as published by
10125 + * the Free Software Foundation; either version 2 of the License, or
10126 + * (at your option) any later version.
10127 + *
10128 + * This program is distributed in the hope that it will be useful,
10129 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10130 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10131 + * GNU General Public License for more details.
10132 + *
10133 + * You should have received a copy of the GNU General Public License
10134 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10135 + */
10136 +
10137 +/*
10138 + * directory operations
10139 + */
10140 +
10141 +#include <linux/fs_stack.h>
10142 +#include "aufs.h"
10143 +
10144 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10145 +{
10146 +       unsigned int nlink;
10147 +
10148 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10149 +
10150 +       nlink = dir->i_nlink;
10151 +       nlink += h_dir->i_nlink - 2;
10152 +       if (h_dir->i_nlink < 2)
10153 +               nlink += 2;
10154 +       smp_mb(); /* for i_nlink */
10155 +       /* 0 can happen in revaliding */
10156 +       set_nlink(dir, nlink);
10157 +}
10158 +
10159 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10160 +{
10161 +       unsigned int nlink;
10162 +
10163 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10164 +
10165 +       nlink = dir->i_nlink;
10166 +       nlink -= h_dir->i_nlink - 2;
10167 +       if (h_dir->i_nlink < 2)
10168 +               nlink -= 2;
10169 +       smp_mb(); /* for i_nlink */
10170 +       /* nlink == 0 means the branch-fs is broken */
10171 +       set_nlink(dir, nlink);
10172 +}
10173 +
10174 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10175 +{
10176 +       loff_t sz;
10177 +       aufs_bindex_t bindex, bbot;
10178 +       struct file *h_file;
10179 +       struct dentry *h_dentry;
10180 +
10181 +       sz = 0;
10182 +       if (file) {
10183 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10184 +
10185 +               bbot = au_fbbot_dir(file);
10186 +               for (bindex = au_fbtop(file);
10187 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10188 +                    bindex++) {
10189 +                       h_file = au_hf_dir(file, bindex);
10190 +                       if (h_file && file_inode(h_file))
10191 +                               sz += vfsub_f_size_read(h_file);
10192 +               }
10193 +       } else {
10194 +               AuDebugOn(!dentry);
10195 +               AuDebugOn(!d_is_dir(dentry));
10196 +
10197 +               bbot = au_dbtaildir(dentry);
10198 +               for (bindex = au_dbtop(dentry);
10199 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10200 +                    bindex++) {
10201 +                       h_dentry = au_h_dptr(dentry, bindex);
10202 +                       if (h_dentry && d_is_positive(h_dentry))
10203 +                               sz += i_size_read(d_inode(h_dentry));
10204 +               }
10205 +       }
10206 +       if (sz < KMALLOC_MAX_SIZE)
10207 +               sz = roundup_pow_of_two(sz);
10208 +       if (sz > KMALLOC_MAX_SIZE)
10209 +               sz = KMALLOC_MAX_SIZE;
10210 +       else if (sz < NAME_MAX) {
10211 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10212 +               sz = AUFS_RDBLK_DEF;
10213 +       }
10214 +       return sz;
10215 +}
10216 +
10217 +struct au_dir_ts_arg {
10218 +       struct dentry *dentry;
10219 +       aufs_bindex_t brid;
10220 +};
10221 +
10222 +static void au_do_dir_ts(void *arg)
10223 +{
10224 +       struct au_dir_ts_arg *a = arg;
10225 +       struct au_dtime dt;
10226 +       struct path h_path;
10227 +       struct inode *dir, *h_dir;
10228 +       struct super_block *sb;
10229 +       struct au_branch *br;
10230 +       struct au_hinode *hdir;
10231 +       int err;
10232 +       aufs_bindex_t btop, bindex;
10233 +
10234 +       sb = a->dentry->d_sb;
10235 +       if (d_really_is_negative(a->dentry))
10236 +               goto out;
10237 +       /* no dir->i_mutex lock */
10238 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10239 +
10240 +       dir = d_inode(a->dentry);
10241 +       btop = au_ibtop(dir);
10242 +       bindex = au_br_index(sb, a->brid);
10243 +       if (bindex < btop)
10244 +               goto out_unlock;
10245 +
10246 +       br = au_sbr(sb, bindex);
10247 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10248 +       if (!h_path.dentry)
10249 +               goto out_unlock;
10250 +       h_path.mnt = au_br_mnt(br);
10251 +       au_dtime_store(&dt, a->dentry, &h_path);
10252 +
10253 +       br = au_sbr(sb, btop);
10254 +       if (!au_br_writable(br->br_perm))
10255 +               goto out_unlock;
10256 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10257 +       h_path.mnt = au_br_mnt(br);
10258 +       err = vfsub_mnt_want_write(h_path.mnt);
10259 +       if (err)
10260 +               goto out_unlock;
10261 +       hdir = au_hi(dir, btop);
10262 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10263 +       h_dir = au_h_iptr(dir, btop);
10264 +       if (h_dir->i_nlink
10265 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10266 +               dt.dt_h_path = h_path;
10267 +               au_dtime_revert(&dt);
10268 +       }
10269 +       au_hn_inode_unlock(hdir);
10270 +       vfsub_mnt_drop_write(h_path.mnt);
10271 +       au_cpup_attr_timesizes(dir);
10272 +
10273 +out_unlock:
10274 +       aufs_read_unlock(a->dentry, AuLock_DW);
10275 +out:
10276 +       dput(a->dentry);
10277 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10278 +       kfree(arg);
10279 +}
10280 +
10281 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10282 +{
10283 +       int perm, wkq_err;
10284 +       aufs_bindex_t btop;
10285 +       struct au_dir_ts_arg *arg;
10286 +       struct dentry *dentry;
10287 +       struct super_block *sb;
10288 +
10289 +       IMustLock(dir);
10290 +
10291 +       dentry = d_find_any_alias(dir);
10292 +       AuDebugOn(!dentry);
10293 +       sb = dentry->d_sb;
10294 +       btop = au_ibtop(dir);
10295 +       if (btop == bindex) {
10296 +               au_cpup_attr_timesizes(dir);
10297 +               goto out;
10298 +       }
10299 +
10300 +       perm = au_sbr_perm(sb, btop);
10301 +       if (!au_br_writable(perm))
10302 +               goto out;
10303 +
10304 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10305 +       if (!arg)
10306 +               goto out;
10307 +
10308 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10309 +       arg->brid = au_sbr_id(sb, bindex);
10310 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10311 +       if (unlikely(wkq_err)) {
10312 +               pr_err("wkq %d\n", wkq_err);
10313 +               dput(dentry);
10314 +               kfree(arg);
10315 +       }
10316 +
10317 +out:
10318 +       dput(dentry);
10319 +}
10320 +
10321 +/* ---------------------------------------------------------------------- */
10322 +
10323 +static int reopen_dir(struct file *file)
10324 +{
10325 +       int err;
10326 +       unsigned int flags;
10327 +       aufs_bindex_t bindex, btail, btop;
10328 +       struct dentry *dentry, *h_dentry;
10329 +       struct file *h_file;
10330 +
10331 +       /* open all lower dirs */
10332 +       dentry = file->f_path.dentry;
10333 +       btop = au_dbtop(dentry);
10334 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10335 +               au_set_h_fptr(file, bindex, NULL);
10336 +       au_set_fbtop(file, btop);
10337 +
10338 +       btail = au_dbtaildir(dentry);
10339 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10340 +               au_set_h_fptr(file, bindex, NULL);
10341 +       au_set_fbbot_dir(file, btail);
10342 +
10343 +       flags = vfsub_file_flags(file);
10344 +       for (bindex = btop; bindex <= btail; bindex++) {
10345 +               h_dentry = au_h_dptr(dentry, bindex);
10346 +               if (!h_dentry)
10347 +                       continue;
10348 +               h_file = au_hf_dir(file, bindex);
10349 +               if (h_file)
10350 +                       continue;
10351 +
10352 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10353 +               err = PTR_ERR(h_file);
10354 +               if (IS_ERR(h_file))
10355 +                       goto out; /* close all? */
10356 +               au_set_h_fptr(file, bindex, h_file);
10357 +       }
10358 +       au_update_figen(file);
10359 +       /* todo: necessary? */
10360 +       /* file->f_ra = h_file->f_ra; */
10361 +       err = 0;
10362 +
10363 +out:
10364 +       return err;
10365 +}
10366 +
10367 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10368 +{
10369 +       int err;
10370 +       aufs_bindex_t bindex, btail;
10371 +       struct dentry *dentry, *h_dentry;
10372 +       struct vfsmount *mnt;
10373 +
10374 +       FiMustWriteLock(file);
10375 +       AuDebugOn(h_file);
10376 +
10377 +       err = 0;
10378 +       mnt = file->f_path.mnt;
10379 +       dentry = file->f_path.dentry;
10380 +       file->f_version = inode_query_iversion(d_inode(dentry));
10381 +       bindex = au_dbtop(dentry);
10382 +       au_set_fbtop(file, bindex);
10383 +       btail = au_dbtaildir(dentry);
10384 +       au_set_fbbot_dir(file, btail);
10385 +       for (; !err && bindex <= btail; bindex++) {
10386 +               h_dentry = au_h_dptr(dentry, bindex);
10387 +               if (!h_dentry)
10388 +                       continue;
10389 +
10390 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10391 +               if (unlikely(err))
10392 +                       break;
10393 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10394 +               if (IS_ERR(h_file)) {
10395 +                       err = PTR_ERR(h_file);
10396 +                       break;
10397 +               }
10398 +               au_set_h_fptr(file, bindex, h_file);
10399 +       }
10400 +       au_update_figen(file);
10401 +       /* todo: necessary? */
10402 +       /* file->f_ra = h_file->f_ra; */
10403 +       if (!err)
10404 +               return 0; /* success */
10405 +
10406 +       /* close all */
10407 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10408 +               au_set_h_fptr(file, bindex, NULL);
10409 +       au_set_fbtop(file, -1);
10410 +       au_set_fbbot_dir(file, -1);
10411 +
10412 +       return err;
10413 +}
10414 +
10415 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10416 +                        struct file *file)
10417 +{
10418 +       int err;
10419 +       struct super_block *sb;
10420 +       struct au_fidir *fidir;
10421 +
10422 +       err = -ENOMEM;
10423 +       sb = file->f_path.dentry->d_sb;
10424 +       si_read_lock(sb, AuLock_FLUSH);
10425 +       fidir = au_fidir_alloc(sb);
10426 +       if (fidir) {
10427 +               struct au_do_open_args args = {
10428 +                       .open   = do_open_dir,
10429 +                       .fidir  = fidir
10430 +               };
10431 +               err = au_do_open(file, &args);
10432 +               if (unlikely(err))
10433 +                       kfree(fidir);
10434 +       }
10435 +       si_read_unlock(sb);
10436 +       return err;
10437 +}
10438 +
10439 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10440 +                           struct file *file)
10441 +{
10442 +       struct au_vdir *vdir_cache;
10443 +       struct au_finfo *finfo;
10444 +       struct au_fidir *fidir;
10445 +       struct au_hfile *hf;
10446 +       aufs_bindex_t bindex, bbot;
10447 +
10448 +       finfo = au_fi(file);
10449 +       fidir = finfo->fi_hdir;
10450 +       if (fidir) {
10451 +               au_hbl_del(&finfo->fi_hlist,
10452 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10453 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10454 +               if (vdir_cache)
10455 +                       au_vdir_free(vdir_cache);
10456 +
10457 +               bindex = finfo->fi_btop;
10458 +               if (bindex >= 0) {
10459 +                       hf = fidir->fd_hfile + bindex;
10460 +                       /*
10461 +                        * calls fput() instead of filp_close(),
10462 +                        * since no dnotify or lock for the lower file.
10463 +                        */
10464 +                       bbot = fidir->fd_bbot;
10465 +                       for (; bindex <= bbot; bindex++, hf++)
10466 +                               if (hf->hf_file)
10467 +                                       au_hfput(hf, /*execed*/0);
10468 +               }
10469 +               kfree(fidir);
10470 +               finfo->fi_hdir = NULL;
10471 +       }
10472 +       au_finfo_fin(file);
10473 +       return 0;
10474 +}
10475 +
10476 +/* ---------------------------------------------------------------------- */
10477 +
10478 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10479 +{
10480 +       int err;
10481 +       aufs_bindex_t bindex, bbot;
10482 +       struct file *h_file;
10483 +
10484 +       err = 0;
10485 +       bbot = au_fbbot_dir(file);
10486 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10487 +               h_file = au_hf_dir(file, bindex);
10488 +               if (h_file)
10489 +                       err = vfsub_flush(h_file, id);
10490 +       }
10491 +       return err;
10492 +}
10493 +
10494 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10495 +{
10496 +       return au_do_flush(file, id, au_do_flush_dir);
10497 +}
10498 +
10499 +/* ---------------------------------------------------------------------- */
10500 +
10501 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10502 +{
10503 +       int err;
10504 +       aufs_bindex_t bbot, bindex;
10505 +       struct inode *inode;
10506 +       struct super_block *sb;
10507 +
10508 +       err = 0;
10509 +       sb = dentry->d_sb;
10510 +       inode = d_inode(dentry);
10511 +       IMustLock(inode);
10512 +       bbot = au_dbbot(dentry);
10513 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10514 +               struct path h_path;
10515 +
10516 +               if (au_test_ro(sb, bindex, inode))
10517 +                       continue;
10518 +               h_path.dentry = au_h_dptr(dentry, bindex);
10519 +               if (!h_path.dentry)
10520 +                       continue;
10521 +
10522 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10523 +               err = vfsub_fsync(NULL, &h_path, datasync);
10524 +       }
10525 +
10526 +       return err;
10527 +}
10528 +
10529 +static int au_do_fsync_dir(struct file *file, int datasync)
10530 +{
10531 +       int err;
10532 +       aufs_bindex_t bbot, bindex;
10533 +       struct file *h_file;
10534 +       struct super_block *sb;
10535 +       struct inode *inode;
10536 +
10537 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10538 +       if (unlikely(err))
10539 +               goto out;
10540 +
10541 +       inode = file_inode(file);
10542 +       sb = inode->i_sb;
10543 +       bbot = au_fbbot_dir(file);
10544 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10545 +               h_file = au_hf_dir(file, bindex);
10546 +               if (!h_file || au_test_ro(sb, bindex, inode))
10547 +                       continue;
10548 +
10549 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10550 +       }
10551 +
10552 +out:
10553 +       return err;
10554 +}
10555 +
10556 +/*
10557 + * @file may be NULL
10558 + */
10559 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10560 +                         int datasync)
10561 +{
10562 +       int err;
10563 +       struct dentry *dentry;
10564 +       struct inode *inode;
10565 +       struct super_block *sb;
10566 +
10567 +       err = 0;
10568 +       dentry = file->f_path.dentry;
10569 +       inode = d_inode(dentry);
10570 +       inode_lock(inode);
10571 +       sb = dentry->d_sb;
10572 +       si_noflush_read_lock(sb);
10573 +       if (file)
10574 +               err = au_do_fsync_dir(file, datasync);
10575 +       else {
10576 +               di_write_lock_child(dentry);
10577 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10578 +       }
10579 +       au_cpup_attr_timesizes(inode);
10580 +       di_write_unlock(dentry);
10581 +       if (file)
10582 +               fi_write_unlock(file);
10583 +
10584 +       si_read_unlock(sb);
10585 +       inode_unlock(inode);
10586 +       return err;
10587 +}
10588 +
10589 +/* ---------------------------------------------------------------------- */
10590 +
10591 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10592 +{
10593 +       int err;
10594 +       struct dentry *dentry;
10595 +       struct inode *inode, *h_inode;
10596 +       struct super_block *sb;
10597 +
10598 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10599 +
10600 +       dentry = file->f_path.dentry;
10601 +       inode = d_inode(dentry);
10602 +       IMustLock(inode);
10603 +
10604 +       sb = dentry->d_sb;
10605 +       si_read_lock(sb, AuLock_FLUSH);
10606 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10607 +       if (unlikely(err))
10608 +               goto out;
10609 +       err = au_alive_dir(dentry);
10610 +       if (!err)
10611 +               err = au_vdir_init(file);
10612 +       di_downgrade_lock(dentry, AuLock_IR);
10613 +       if (unlikely(err))
10614 +               goto out_unlock;
10615 +
10616 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10617 +       if (!au_test_nfsd()) {
10618 +               err = au_vdir_fill_de(file, ctx);
10619 +               fsstack_copy_attr_atime(inode, h_inode);
10620 +       } else {
10621 +               /*
10622 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10623 +                * encode_fh() and others.
10624 +                */
10625 +               atomic_inc(&h_inode->i_count);
10626 +               di_read_unlock(dentry, AuLock_IR);
10627 +               si_read_unlock(sb);
10628 +               err = au_vdir_fill_de(file, ctx);
10629 +               fsstack_copy_attr_atime(inode, h_inode);
10630 +               fi_write_unlock(file);
10631 +               iput(h_inode);
10632 +
10633 +               AuTraceErr(err);
10634 +               return err;
10635 +       }
10636 +
10637 +out_unlock:
10638 +       di_read_unlock(dentry, AuLock_IR);
10639 +       fi_write_unlock(file);
10640 +out:
10641 +       si_read_unlock(sb);
10642 +       return err;
10643 +}
10644 +
10645 +/* ---------------------------------------------------------------------- */
10646 +
10647 +#define AuTestEmpty_WHONLY     1
10648 +#define AuTestEmpty_CALLED     (1 << 1)
10649 +#define AuTestEmpty_SHWH       (1 << 2)
10650 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10651 +#define au_fset_testempty(flags, name) \
10652 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10653 +#define au_fclr_testempty(flags, name) \
10654 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10655 +
10656 +#ifndef CONFIG_AUFS_SHWH
10657 +#undef AuTestEmpty_SHWH
10658 +#define AuTestEmpty_SHWH       0
10659 +#endif
10660 +
10661 +struct test_empty_arg {
10662 +       struct dir_context ctx;
10663 +       struct au_nhash *whlist;
10664 +       unsigned int flags;
10665 +       int err;
10666 +       aufs_bindex_t bindex;
10667 +};
10668 +
10669 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10670 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10671 +                        unsigned int d_type)
10672 +{
10673 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10674 +                                                 ctx);
10675 +       char *name = (void *)__name;
10676 +
10677 +       arg->err = 0;
10678 +       au_fset_testempty(arg->flags, CALLED);
10679 +       /* smp_mb(); */
10680 +       if (name[0] == '.'
10681 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10682 +               goto out; /* success */
10683 +
10684 +       if (namelen <= AUFS_WH_PFX_LEN
10685 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10686 +               if (au_ftest_testempty(arg->flags, WHONLY)
10687 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10688 +                       arg->err = -ENOTEMPTY;
10689 +               goto out;
10690 +       }
10691 +
10692 +       name += AUFS_WH_PFX_LEN;
10693 +       namelen -= AUFS_WH_PFX_LEN;
10694 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10695 +               arg->err = au_nhash_append_wh
10696 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10697 +                        au_ftest_testempty(arg->flags, SHWH));
10698 +
10699 +out:
10700 +       /* smp_mb(); */
10701 +       AuTraceErr(arg->err);
10702 +       return arg->err;
10703 +}
10704 +
10705 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10706 +{
10707 +       int err;
10708 +       struct file *h_file;
10709 +
10710 +       h_file = au_h_open(dentry, arg->bindex,
10711 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10712 +                          /*file*/NULL, /*force_wr*/0);
10713 +       err = PTR_ERR(h_file);
10714 +       if (IS_ERR(h_file))
10715 +               goto out;
10716 +
10717 +       err = 0;
10718 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10719 +           && !file_inode(h_file)->i_nlink)
10720 +               goto out_put;
10721 +
10722 +       do {
10723 +               arg->err = 0;
10724 +               au_fclr_testempty(arg->flags, CALLED);
10725 +               /* smp_mb(); */
10726 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10727 +               if (err >= 0)
10728 +                       err = arg->err;
10729 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10730 +
10731 +out_put:
10732 +       fput(h_file);
10733 +       au_sbr_put(dentry->d_sb, arg->bindex);
10734 +out:
10735 +       return err;
10736 +}
10737 +
10738 +struct do_test_empty_args {
10739 +       int *errp;
10740 +       struct dentry *dentry;
10741 +       struct test_empty_arg *arg;
10742 +};
10743 +
10744 +static void call_do_test_empty(void *args)
10745 +{
10746 +       struct do_test_empty_args *a = args;
10747 +       *a->errp = do_test_empty(a->dentry, a->arg);
10748 +}
10749 +
10750 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10751 +{
10752 +       int err, wkq_err;
10753 +       struct dentry *h_dentry;
10754 +       struct inode *h_inode;
10755 +
10756 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10757 +       h_inode = d_inode(h_dentry);
10758 +       /* todo: i_mode changes anytime? */
10759 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10760 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10761 +       inode_unlock_shared(h_inode);
10762 +       if (!err)
10763 +               err = do_test_empty(dentry, arg);
10764 +       else {
10765 +               struct do_test_empty_args args = {
10766 +                       .errp   = &err,
10767 +                       .dentry = dentry,
10768 +                       .arg    = arg
10769 +               };
10770 +               unsigned int flags = arg->flags;
10771 +
10772 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10773 +               if (unlikely(wkq_err))
10774 +                       err = wkq_err;
10775 +               arg->flags = flags;
10776 +       }
10777 +
10778 +       return err;
10779 +}
10780 +
10781 +int au_test_empty_lower(struct dentry *dentry)
10782 +{
10783 +       int err;
10784 +       unsigned int rdhash;
10785 +       aufs_bindex_t bindex, btop, btail;
10786 +       struct au_nhash whlist;
10787 +       struct test_empty_arg arg = {
10788 +               .ctx = {
10789 +                       .actor = test_empty_cb
10790 +               }
10791 +       };
10792 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10793 +
10794 +       SiMustAnyLock(dentry->d_sb);
10795 +
10796 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10797 +       if (!rdhash)
10798 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10799 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10800 +       if (unlikely(err))
10801 +               goto out;
10802 +
10803 +       arg.flags = 0;
10804 +       arg.whlist = &whlist;
10805 +       btop = au_dbtop(dentry);
10806 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10807 +               au_fset_testempty(arg.flags, SHWH);
10808 +       test_empty = do_test_empty;
10809 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10810 +               test_empty = sio_test_empty;
10811 +       arg.bindex = btop;
10812 +       err = test_empty(dentry, &arg);
10813 +       if (unlikely(err))
10814 +               goto out_whlist;
10815 +
10816 +       au_fset_testempty(arg.flags, WHONLY);
10817 +       btail = au_dbtaildir(dentry);
10818 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10819 +               struct dentry *h_dentry;
10820 +
10821 +               h_dentry = au_h_dptr(dentry, bindex);
10822 +               if (h_dentry && d_is_positive(h_dentry)) {
10823 +                       arg.bindex = bindex;
10824 +                       err = test_empty(dentry, &arg);
10825 +               }
10826 +       }
10827 +
10828 +out_whlist:
10829 +       au_nhash_wh_free(&whlist);
10830 +out:
10831 +       return err;
10832 +}
10833 +
10834 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10835 +{
10836 +       int err;
10837 +       struct test_empty_arg arg = {
10838 +               .ctx = {
10839 +                       .actor = test_empty_cb
10840 +               }
10841 +       };
10842 +       aufs_bindex_t bindex, btail;
10843 +
10844 +       err = 0;
10845 +       arg.whlist = whlist;
10846 +       arg.flags = AuTestEmpty_WHONLY;
10847 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10848 +               au_fset_testempty(arg.flags, SHWH);
10849 +       btail = au_dbtaildir(dentry);
10850 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10851 +               struct dentry *h_dentry;
10852 +
10853 +               h_dentry = au_h_dptr(dentry, bindex);
10854 +               if (h_dentry && d_is_positive(h_dentry)) {
10855 +                       arg.bindex = bindex;
10856 +                       err = sio_test_empty(dentry, &arg);
10857 +               }
10858 +       }
10859 +
10860 +       return err;
10861 +}
10862 +
10863 +/* ---------------------------------------------------------------------- */
10864 +
10865 +const struct file_operations aufs_dir_fop = {
10866 +       .owner          = THIS_MODULE,
10867 +       .llseek         = default_llseek,
10868 +       .read           = generic_read_dir,
10869 +       .iterate_shared = aufs_iterate_shared,
10870 +       .unlocked_ioctl = aufs_ioctl_dir,
10871 +#ifdef CONFIG_COMPAT
10872 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10873 +#endif
10874 +       .open           = aufs_open_dir,
10875 +       .release        = aufs_release_dir,
10876 +       .flush          = aufs_flush_dir,
10877 +       .fsync          = aufs_fsync_dir
10878 +};
10879 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10880 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10881 +++ linux/fs/aufs/dir.h 2018-07-19 09:46:13.056647808 +0200
10882 @@ -0,0 +1,132 @@
10883 +/* SPDX-License-Identifier: GPL-2.0 */
10884 +/*
10885 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10886 + *
10887 + * This program, aufs is free software; you can redistribute it and/or modify
10888 + * it under the terms of the GNU General Public License as published by
10889 + * the Free Software Foundation; either version 2 of the License, or
10890 + * (at your option) any later version.
10891 + *
10892 + * This program is distributed in the hope that it will be useful,
10893 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10894 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10895 + * GNU General Public License for more details.
10896 + *
10897 + * You should have received a copy of the GNU General Public License
10898 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10899 + */
10900 +
10901 +/*
10902 + * directory operations
10903 + */
10904 +
10905 +#ifndef __AUFS_DIR_H__
10906 +#define __AUFS_DIR_H__
10907 +
10908 +#ifdef __KERNEL__
10909 +
10910 +#include <linux/fs.h>
10911 +
10912 +/* ---------------------------------------------------------------------- */
10913 +
10914 +/* need to be faster and smaller */
10915 +
10916 +struct au_nhash {
10917 +       unsigned int            nh_num;
10918 +       struct hlist_head       *nh_head;
10919 +};
10920 +
10921 +struct au_vdir_destr {
10922 +       unsigned char   len;
10923 +       unsigned char   name[0];
10924 +} __packed;
10925 +
10926 +struct au_vdir_dehstr {
10927 +       struct hlist_node       hash;
10928 +       struct au_vdir_destr    *str;
10929 +} ____cacheline_aligned_in_smp;
10930 +
10931 +struct au_vdir_de {
10932 +       ino_t                   de_ino;
10933 +       unsigned char           de_type;
10934 +       /* caution: packed */
10935 +       struct au_vdir_destr    de_str;
10936 +} __packed;
10937 +
10938 +struct au_vdir_wh {
10939 +       struct hlist_node       wh_hash;
10940 +#ifdef CONFIG_AUFS_SHWH
10941 +       ino_t                   wh_ino;
10942 +       aufs_bindex_t           wh_bindex;
10943 +       unsigned char           wh_type;
10944 +#else
10945 +       aufs_bindex_t           wh_bindex;
10946 +#endif
10947 +       /* caution: packed */
10948 +       struct au_vdir_destr    wh_str;
10949 +} __packed;
10950 +
10951 +union au_vdir_deblk_p {
10952 +       unsigned char           *deblk;
10953 +       struct au_vdir_de       *de;
10954 +};
10955 +
10956 +struct au_vdir {
10957 +       unsigned char   **vd_deblk;
10958 +       unsigned long   vd_nblk;
10959 +       struct {
10960 +               unsigned long           ul;
10961 +               union au_vdir_deblk_p   p;
10962 +       } vd_last;
10963 +
10964 +       u64             vd_version;
10965 +       unsigned int    vd_deblk_sz;
10966 +       unsigned long           vd_jiffy;
10967 +} ____cacheline_aligned_in_smp;
10968 +
10969 +/* ---------------------------------------------------------------------- */
10970 +
10971 +/* dir.c */
10972 +extern const struct file_operations aufs_dir_fop;
10973 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10974 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10975 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10976 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10977 +int au_test_empty_lower(struct dentry *dentry);
10978 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10979 +
10980 +/* vdir.c */
10981 +unsigned int au_rdhash_est(loff_t sz);
10982 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10983 +void au_nhash_wh_free(struct au_nhash *whlist);
10984 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10985 +                           int limit);
10986 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10987 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10988 +                      unsigned int d_type, aufs_bindex_t bindex,
10989 +                      unsigned char shwh);
10990 +void au_vdir_free(struct au_vdir *vdir);
10991 +int au_vdir_init(struct file *file);
10992 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10993 +
10994 +/* ioctl.c */
10995 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10996 +
10997 +#ifdef CONFIG_AUFS_RDU
10998 +/* rdu.c */
10999 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
11000 +#ifdef CONFIG_COMPAT
11001 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11002 +                        unsigned long arg);
11003 +#endif
11004 +#else
11005 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11006 +       unsigned int cmd, unsigned long arg)
11007 +#ifdef CONFIG_COMPAT
11008 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11009 +       unsigned int cmd, unsigned long arg)
11010 +#endif
11011 +#endif
11012 +
11013 +#endif /* __KERNEL__ */
11014 +#endif /* __AUFS_DIR_H__ */
11015 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11016 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
11017 +++ linux/fs/aufs/dirren.c      2018-07-19 09:46:13.056647808 +0200
11018 @@ -0,0 +1,1316 @@
11019 +/* SPDX-License-Identifier: GPL-2.0 */
11020 +/*
11021 + * Copyright (C) 2017-2018 Junjiro R. Okajima
11022 + *
11023 + * This program, aufs is free software; you can redistribute it and/or modify
11024 + * it under the terms of the GNU General Public License as published by
11025 + * the Free Software Foundation; either version 2 of the License, or
11026 + * (at your option) any later version.
11027 + *
11028 + * This program is distributed in the hope that it will be useful,
11029 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11030 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11031 + * GNU General Public License for more details.
11032 + *
11033 + * You should have received a copy of the GNU General Public License
11034 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11035 + */
11036 +
11037 +/*
11038 + * special handling in renaming a directoy
11039 + * in order to support looking-up the before-renamed name on the lower readonly
11040 + * branches
11041 + */
11042 +
11043 +#include <linux/byteorder/generic.h>
11044 +#include "aufs.h"
11045 +
11046 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11047 +{
11048 +       int idx;
11049 +
11050 +       idx = au_dr_ihash(ent->dr_h_ino);
11051 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11052 +}
11053 +
11054 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
11055 +{
11056 +       int ret, i;
11057 +       struct hlist_bl_head *hbl;
11058 +
11059 +       ret = 1;
11060 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
11061 +               hbl = dr->dr_h_ino + i;
11062 +               hlist_bl_lock(hbl);
11063 +               ret &= hlist_bl_empty(hbl);
11064 +               hlist_bl_unlock(hbl);
11065 +       }
11066 +
11067 +       return ret;
11068 +}
11069 +
11070 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11071 +{
11072 +       struct au_dr_hino *found, *ent;
11073 +       struct hlist_bl_head *hbl;
11074 +       struct hlist_bl_node *pos;
11075 +       int idx;
11076 +
11077 +       found = NULL;
11078 +       idx = au_dr_ihash(ino);
11079 +       hbl = dr->dr_h_ino + idx;
11080 +       hlist_bl_lock(hbl);
11081 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11082 +               if (ent->dr_h_ino == ino) {
11083 +                       found = ent;
11084 +                       break;
11085 +               }
11086 +       hlist_bl_unlock(hbl);
11087 +
11088 +       return found;
11089 +}
11090 +
11091 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11092 +                       struct au_dr_hino *add_ent)
11093 +{
11094 +       int found, idx;
11095 +       struct hlist_bl_head *hbl;
11096 +       struct hlist_bl_node *pos;
11097 +       struct au_dr_hino *ent;
11098 +
11099 +       found = 0;
11100 +       idx = au_dr_ihash(ino);
11101 +       hbl = dr->dr_h_ino + idx;
11102 +#if 0
11103 +       {
11104 +               struct hlist_bl_node *tmp;
11105 +
11106 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11107 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11108 +       }
11109 +#endif
11110 +       hlist_bl_lock(hbl);
11111 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11112 +               if (ent->dr_h_ino == ino) {
11113 +                       found = 1;
11114 +                       break;
11115 +               }
11116 +       if (!found && add_ent)
11117 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11118 +       hlist_bl_unlock(hbl);
11119 +
11120 +       if (!found && add_ent)
11121 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11122 +
11123 +       return found;
11124 +}
11125 +
11126 +void au_dr_hino_free(struct au_dr_br *dr)
11127 +{
11128 +       int i;
11129 +       struct hlist_bl_head *hbl;
11130 +       struct hlist_bl_node *pos, *tmp;
11131 +       struct au_dr_hino *ent;
11132 +
11133 +       /* SiMustWriteLock(sb); */
11134 +
11135 +       for (i = 0; i < AuDirren_NHASH; i++) {
11136 +               hbl = dr->dr_h_ino + i;
11137 +               /* no spinlock since sbinfo must be write-locked */
11138 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11139 +                       kfree(ent);
11140 +               INIT_HLIST_BL_HEAD(hbl);
11141 +       }
11142 +}
11143 +
11144 +/* returns the number of inodes or an error */
11145 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11146 +                           struct file *hinofile)
11147 +{
11148 +       int err, i;
11149 +       ssize_t ssz;
11150 +       loff_t pos, oldsize;
11151 +       __be64 u64;
11152 +       struct inode *hinoinode;
11153 +       struct hlist_bl_head *hbl;
11154 +       struct hlist_bl_node *n1, *n2;
11155 +       struct au_dr_hino *ent;
11156 +
11157 +       SiMustWriteLock(sb);
11158 +       AuDebugOn(!au_br_writable(br->br_perm));
11159 +
11160 +       hinoinode = file_inode(hinofile);
11161 +       oldsize = i_size_read(hinoinode);
11162 +
11163 +       err = 0;
11164 +       pos = 0;
11165 +       hbl = br->br_dirren.dr_h_ino;
11166 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11167 +               /* no bit-lock since sbinfo must be write-locked */
11168 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11169 +                       AuDbg("hi%llu, %pD2\n",
11170 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11171 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11172 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11173 +                       if (ssz == sizeof(u64))
11174 +                               continue;
11175 +
11176 +                       /* write error */
11177 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11178 +                       err = -ENOSPC;
11179 +                       if (ssz < 0)
11180 +                               err = ssz;
11181 +                       break;
11182 +               }
11183 +       }
11184 +       /* regardless the error */
11185 +       if (pos < oldsize) {
11186 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11187 +               AuTraceErr(err);
11188 +       }
11189 +
11190 +       AuTraceErr(err);
11191 +       return err;
11192 +}
11193 +
11194 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11195 +{
11196 +       int err, hidx;
11197 +       ssize_t ssz;
11198 +       size_t sz, n;
11199 +       loff_t pos;
11200 +       uint64_t u64;
11201 +       struct au_dr_hino *ent;
11202 +       struct inode *hinoinode;
11203 +       struct hlist_bl_head *hbl;
11204 +
11205 +       err = 0;
11206 +       pos = 0;
11207 +       hbl = dr->dr_h_ino;
11208 +       hinoinode = file_inode(hinofile);
11209 +       sz = i_size_read(hinoinode);
11210 +       AuDebugOn(sz % sizeof(u64));
11211 +       n = sz / sizeof(u64);
11212 +       while (n--) {
11213 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11214 +               if (unlikely(ssz != sizeof(u64))) {
11215 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11216 +                       err = -EINVAL;
11217 +                       if (ssz < 0)
11218 +                               err = ssz;
11219 +                       goto out_free;
11220 +               }
11221 +
11222 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11223 +               if (!ent) {
11224 +                       err = -ENOMEM;
11225 +                       AuTraceErr(err);
11226 +                       goto out_free;
11227 +               }
11228 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11229 +               AuDbg("hi%llu, %pD2\n",
11230 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11231 +               hidx = au_dr_ihash(ent->dr_h_ino);
11232 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11233 +       }
11234 +       goto out; /* success */
11235 +
11236 +out_free:
11237 +       au_dr_hino_free(dr);
11238 +out:
11239 +       AuTraceErr(err);
11240 +       return err;
11241 +}
11242 +
11243 +/*
11244 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11245 + * @path is a switch to distinguish load and store.
11246 + */
11247 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11248 +                     struct au_branch *br, const struct path *path)
11249 +{
11250 +       int err, flags;
11251 +       unsigned char load, suspend;
11252 +       struct file *hinofile;
11253 +       struct au_hinode *hdir;
11254 +       struct inode *dir, *delegated;
11255 +       struct path hinopath;
11256 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11257 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11258 +
11259 +       AuDebugOn(bindex < 0 && !br);
11260 +       AuDebugOn(bindex >= 0 && br);
11261 +
11262 +       err = -EINVAL;
11263 +       suspend = !br;
11264 +       if (suspend)
11265 +               br = au_sbr(sb, bindex);
11266 +       load = !!path;
11267 +       if (!load) {
11268 +               path = &br->br_path;
11269 +               AuDebugOn(!au_br_writable(br->br_perm));
11270 +               if (unlikely(!au_br_writable(br->br_perm)))
11271 +                       goto out;
11272 +       }
11273 +
11274 +       hdir = NULL;
11275 +       if (suspend) {
11276 +               dir = d_inode(sb->s_root);
11277 +               hdir = au_hinode(au_ii(dir), bindex);
11278 +               dir = hdir->hi_inode;
11279 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11280 +       } else {
11281 +               dir = d_inode(path->dentry);
11282 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11283 +       }
11284 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11285 +       err = PTR_ERR(hinopath.dentry);
11286 +       if (IS_ERR(hinopath.dentry))
11287 +               goto out_unlock;
11288 +
11289 +       err = 0;
11290 +       flags = O_RDONLY;
11291 +       if (load) {
11292 +               if (d_is_negative(hinopath.dentry))
11293 +                       goto out_dput; /* success */
11294 +       } else {
11295 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11296 +                       if (d_is_positive(hinopath.dentry)) {
11297 +                               delegated = NULL;
11298 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11299 +                                                  /*force*/0);
11300 +                               AuTraceErr(err);
11301 +                               if (unlikely(err))
11302 +                                       pr_err("ignored err %d, %pd2\n",
11303 +                                              err, hinopath.dentry);
11304 +                               if (unlikely(err == -EWOULDBLOCK))
11305 +                                       iput(delegated);
11306 +                               err = 0;
11307 +                       }
11308 +                       goto out_dput;
11309 +               } else if (!d_is_positive(hinopath.dentry)) {
11310 +                       err = vfsub_create(dir, &hinopath, 0600,
11311 +                                          /*want_excl*/false);
11312 +                       AuTraceErr(err);
11313 +                       if (unlikely(err))
11314 +                               goto out_dput;
11315 +               }
11316 +               flags = O_WRONLY;
11317 +       }
11318 +       hinopath.mnt = path->mnt;
11319 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11320 +       if (suspend)
11321 +               au_hn_inode_unlock(hdir);
11322 +       else
11323 +               inode_unlock(dir);
11324 +       dput(hinopath.dentry);
11325 +       AuTraceErrPtr(hinofile);
11326 +       if (IS_ERR(hinofile)) {
11327 +               err = PTR_ERR(hinofile);
11328 +               goto out;
11329 +       }
11330 +
11331 +       if (load)
11332 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11333 +       else
11334 +               err = au_dr_hino_store(sb, br, hinofile);
11335 +       fput(hinofile);
11336 +       goto out;
11337 +
11338 +out_dput:
11339 +       dput(hinopath.dentry);
11340 +out_unlock:
11341 +       if (suspend)
11342 +               au_hn_inode_unlock(hdir);
11343 +       else
11344 +               inode_unlock(dir);
11345 +out:
11346 +       AuTraceErr(err);
11347 +       return err;
11348 +}
11349 +
11350 +/* ---------------------------------------------------------------------- */
11351 +
11352 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11353 +{
11354 +       int err;
11355 +       struct kstatfs kstfs;
11356 +       dev_t dev;
11357 +       struct dentry *dentry;
11358 +       struct super_block *sb;
11359 +
11360 +       err = vfs_statfs((void *)path, &kstfs);
11361 +       AuTraceErr(err);
11362 +       if (unlikely(err))
11363 +               goto out;
11364 +
11365 +       /* todo: support for UUID */
11366 +
11367 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11368 +               brid->type = AuBrid_FSID;
11369 +               brid->fsid = kstfs.f_fsid;
11370 +       } else {
11371 +               dentry = path->dentry;
11372 +               sb = dentry->d_sb;
11373 +               dev = sb->s_dev;
11374 +               if (dev) {
11375 +                       brid->type = AuBrid_DEV;
11376 +                       brid->dev = dev;
11377 +               }
11378 +       }
11379 +
11380 +out:
11381 +       return err;
11382 +}
11383 +
11384 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11385 +                 const struct path *path)
11386 +{
11387 +       int err, i;
11388 +       struct au_dr_br *dr;
11389 +       struct hlist_bl_head *hbl;
11390 +
11391 +       dr = &br->br_dirren;
11392 +       hbl = dr->dr_h_ino;
11393 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11394 +               INIT_HLIST_BL_HEAD(hbl);
11395 +
11396 +       err = au_dr_brid_init(&dr->dr_brid, path);
11397 +       if (unlikely(err))
11398 +               goto out;
11399 +
11400 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11401 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11402 +
11403 +out:
11404 +       AuTraceErr(err);
11405 +       return err;
11406 +}
11407 +
11408 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11409 +{
11410 +       int err;
11411 +
11412 +       err = 0;
11413 +       if (au_br_writable(br->br_perm))
11414 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11415 +       if (!err)
11416 +               au_dr_hino_free(&br->br_dirren);
11417 +
11418 +       return err;
11419 +}
11420 +
11421 +/* ---------------------------------------------------------------------- */
11422 +
11423 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11424 +                      char *buf, size_t sz)
11425 +{
11426 +       int err;
11427 +       unsigned int major, minor;
11428 +       char *p;
11429 +
11430 +       p = buf;
11431 +       err = snprintf(p, sz, "%d_", brid->type);
11432 +       AuDebugOn(err > sz);
11433 +       p += err;
11434 +       sz -= err;
11435 +       switch (brid->type) {
11436 +       case AuBrid_Unset:
11437 +               return -EINVAL;
11438 +       case AuBrid_UUID:
11439 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11440 +               break;
11441 +       case AuBrid_FSID:
11442 +               err = snprintf(p, sz, "%08x-%08x",
11443 +                              brid->fsid.val[0], brid->fsid.val[1]);
11444 +               break;
11445 +       case AuBrid_DEV:
11446 +               major = MAJOR(brid->dev);
11447 +               minor = MINOR(brid->dev);
11448 +               if (major <= 0xff && minor <= 0xff)
11449 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11450 +               else
11451 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11452 +               break;
11453 +       }
11454 +       AuDebugOn(err > sz);
11455 +       p += err;
11456 +       sz -= err;
11457 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11458 +       AuDebugOn(err > sz);
11459 +       p += err;
11460 +       sz -= err;
11461 +
11462 +       return p - buf;
11463 +}
11464 +
11465 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11466 +{
11467 +       int rlen;
11468 +       struct dentry *br_dentry;
11469 +       struct inode *br_inode;
11470 +
11471 +       br_dentry = au_br_dentry(br);
11472 +       br_inode = d_inode(br_dentry);
11473 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11474 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11475 +       AuDebugOn(rlen > len);
11476 +
11477 +       return rlen;
11478 +}
11479 +
11480 +/* ---------------------------------------------------------------------- */
11481 +
11482 +/*
11483 + * from the given @h_dentry, construct drinfo at @*fdata.
11484 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11485 + * @allocated.
11486 + */
11487 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11488 +                              struct dentry *h_dentry,
11489 +                              unsigned char *allocated)
11490 +{
11491 +       int err, v;
11492 +       struct au_drinfo_fdata *f, *p;
11493 +       struct au_drinfo *drinfo;
11494 +       struct inode *h_inode;
11495 +       struct qstr *qname;
11496 +
11497 +       err = 0;
11498 +       f = *fdata;
11499 +       h_inode = d_inode(h_dentry);
11500 +       qname = &h_dentry->d_name;
11501 +       drinfo = &f->drinfo;
11502 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11503 +       drinfo->oldnamelen = qname->len;
11504 +       if (*allocated < sizeof(*f) + qname->len) {
11505 +               v = roundup_pow_of_two(*allocated + qname->len);
11506 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11507 +               if (unlikely(!p)) {
11508 +                       err = -ENOMEM;
11509 +                       AuTraceErr(err);
11510 +                       goto out;
11511 +               }
11512 +               f = p;
11513 +               *fdata = f;
11514 +               *allocated = v;
11515 +               drinfo = &f->drinfo;
11516 +       }
11517 +       memcpy(drinfo->oldname, qname->name, qname->len);
11518 +       AuDbg("i%llu, %.*s\n",
11519 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11520 +             drinfo->oldname);
11521 +
11522 +out:
11523 +       AuTraceErr(err);
11524 +       return err;
11525 +}
11526 +
11527 +/* callers have to free the return value */
11528 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11529 +{
11530 +       struct au_drinfo *ret, *drinfo;
11531 +       struct au_drinfo_fdata fdata;
11532 +       int len;
11533 +       loff_t pos;
11534 +       ssize_t ssz;
11535 +
11536 +       ret = ERR_PTR(-EIO);
11537 +       pos = 0;
11538 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11539 +       if (unlikely(ssz != sizeof(fdata))) {
11540 +               AuIOErr("ssz %zd, %u, %pD2\n",
11541 +                       ssz, (unsigned int)sizeof(fdata), file);
11542 +               goto out;
11543 +       }
11544 +
11545 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11546 +       switch (fdata.magic) {
11547 +       case AUFS_DRINFO_MAGIC_V1:
11548 +               break;
11549 +       default:
11550 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11551 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11552 +               goto out;
11553 +       }
11554 +
11555 +       drinfo = &fdata.drinfo;
11556 +       len = drinfo->oldnamelen;
11557 +       if (!len) {
11558 +               AuIOErr("broken drinfo %pD2\n", file);
11559 +               goto out;
11560 +       }
11561 +
11562 +       ret = NULL;
11563 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11564 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11565 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11566 +                     (unsigned long long)drinfo->ino,
11567 +                     (unsigned long long)h_ino, file);
11568 +               goto out; /* success */
11569 +       }
11570 +
11571 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11572 +       if (unlikely(!ret)) {
11573 +               ret = ERR_PTR(-ENOMEM);
11574 +               AuTraceErrPtr(ret);
11575 +               goto out;
11576 +       }
11577 +
11578 +       *ret = *drinfo;
11579 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11580 +       if (unlikely(ssz != len)) {
11581 +               kfree(ret);
11582 +               ret = ERR_PTR(-EIO);
11583 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11584 +               goto out;
11585 +       }
11586 +
11587 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11588 +
11589 +out:
11590 +       return ret;
11591 +}
11592 +
11593 +/* ---------------------------------------------------------------------- */
11594 +
11595 +/* in order to be revertible */
11596 +struct au_drinfo_rev_elm {
11597 +       int                     created;
11598 +       struct dentry           *info_dentry;
11599 +       struct au_drinfo        *info_last;
11600 +};
11601 +
11602 +struct au_drinfo_rev {
11603 +       unsigned char                   already;
11604 +       aufs_bindex_t                   nelm;
11605 +       struct au_drinfo_rev_elm        elm[0];
11606 +};
11607 +
11608 +/* todo: isn't it too large? */
11609 +struct au_drinfo_store {
11610 +       struct path h_ppath;
11611 +       struct dentry *h_dentry;
11612 +       struct au_drinfo_fdata *fdata;
11613 +       char *infoname;                 /* inside of whname, just after PFX */
11614 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11615 +       aufs_bindex_t btgt, btail;
11616 +       unsigned char no_sio,
11617 +               allocated,              /* current size of *fdata */
11618 +               infonamelen,            /* room size for p */
11619 +               whnamelen,              /* length of the genarated name */
11620 +               renameback;             /* renamed back */
11621 +};
11622 +
11623 +/* on rename(2) error, the caller should revert it using @elm */
11624 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11625 +                             struct au_drinfo_rev_elm *elm)
11626 +{
11627 +       int err, len;
11628 +       ssize_t ssz;
11629 +       loff_t pos;
11630 +       struct path infopath = {
11631 +               .mnt = w->h_ppath.mnt
11632 +       };
11633 +       struct inode *h_dir, *h_inode, *delegated;
11634 +       struct file *infofile;
11635 +       struct qstr *qname;
11636 +
11637 +       AuDebugOn(elm
11638 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11639 +
11640 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11641 +                                              w->whnamelen);
11642 +       AuTraceErrPtr(infopath.dentry);
11643 +       if (IS_ERR(infopath.dentry)) {
11644 +               err = PTR_ERR(infopath.dentry);
11645 +               goto out;
11646 +       }
11647 +
11648 +       err = 0;
11649 +       h_dir = d_inode(w->h_ppath.dentry);
11650 +       if (elm && d_is_negative(infopath.dentry)) {
11651 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11652 +               AuTraceErr(err);
11653 +               if (unlikely(err))
11654 +                       goto out_dput;
11655 +               elm->created = 1;
11656 +               elm->info_dentry = dget(infopath.dentry);
11657 +       }
11658 +
11659 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11660 +       AuTraceErrPtr(infofile);
11661 +       if (IS_ERR(infofile)) {
11662 +               err = PTR_ERR(infofile);
11663 +               goto out_dput;
11664 +       }
11665 +
11666 +       h_inode = d_inode(infopath.dentry);
11667 +       if (elm && i_size_read(h_inode)) {
11668 +               h_inode = d_inode(w->h_dentry);
11669 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11670 +               AuTraceErrPtr(elm->info_last);
11671 +               if (IS_ERR(elm->info_last)) {
11672 +                       err = PTR_ERR(elm->info_last);
11673 +                       elm->info_last = NULL;
11674 +                       AuDebugOn(elm->info_dentry);
11675 +                       goto out_fput;
11676 +               }
11677 +       }
11678 +
11679 +       if (elm && w->renameback) {
11680 +               delegated = NULL;
11681 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11682 +               AuTraceErr(err);
11683 +               if (unlikely(err == -EWOULDBLOCK))
11684 +                       iput(delegated);
11685 +               goto out_fput;
11686 +       }
11687 +
11688 +       pos = 0;
11689 +       qname = &w->h_dentry->d_name;
11690 +       len = sizeof(*w->fdata) + qname->len;
11691 +       if (!elm)
11692 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11693 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11694 +       if (ssz == len) {
11695 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11696 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11697 +               goto out_fput; /* success */
11698 +       } else {
11699 +               err = -EIO;
11700 +               if (ssz < 0)
11701 +                       err = ssz;
11702 +               /* the caller should revert it using @elm */
11703 +       }
11704 +
11705 +out_fput:
11706 +       fput(infofile);
11707 +out_dput:
11708 +       dput(infopath.dentry);
11709 +out:
11710 +       AuTraceErr(err);
11711 +       return err;
11712 +}
11713 +
11714 +struct au_call_drinfo_do_store_args {
11715 +       int *errp;
11716 +       struct au_drinfo_store *w;
11717 +       struct au_drinfo_rev_elm *elm;
11718 +};
11719 +
11720 +static void au_call_drinfo_do_store(void *args)
11721 +{
11722 +       struct au_call_drinfo_do_store_args *a = args;
11723 +
11724 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11725 +}
11726 +
11727 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11728 +                              struct au_drinfo_rev_elm *elm)
11729 +{
11730 +       int err, wkq_err;
11731 +
11732 +       if (w->no_sio)
11733 +               err = au_drinfo_do_store(w, elm);
11734 +       else {
11735 +               struct au_call_drinfo_do_store_args a = {
11736 +                       .errp   = &err,
11737 +                       .w      = w,
11738 +                       .elm    = elm
11739 +               };
11740 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11741 +               if (unlikely(wkq_err))
11742 +                       err = wkq_err;
11743 +       }
11744 +       AuTraceErr(err);
11745 +
11746 +       return err;
11747 +}
11748 +
11749 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11750 +                                    aufs_bindex_t btgt)
11751 +{
11752 +       int err;
11753 +
11754 +       memset(w, 0, sizeof(*w));
11755 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11756 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11757 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11758 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11759 +       w->btgt = btgt;
11760 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11761 +
11762 +       err = -ENOMEM;
11763 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11764 +       if (unlikely(!w->fdata)) {
11765 +               AuTraceErr(err);
11766 +               goto out;
11767 +       }
11768 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11769 +       err = 0;
11770 +
11771 +out:
11772 +       return err;
11773 +}
11774 +
11775 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11776 +{
11777 +       kfree(w->fdata);
11778 +}
11779 +
11780 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11781 +                               struct au_drinfo_store *w)
11782 +{
11783 +       struct au_drinfo_rev_elm *elm;
11784 +       struct inode *h_dir, *delegated;
11785 +       int err, nelm;
11786 +       struct path infopath = {
11787 +               .mnt = w->h_ppath.mnt
11788 +       };
11789 +
11790 +       h_dir = d_inode(w->h_ppath.dentry);
11791 +       IMustLock(h_dir);
11792 +
11793 +       err = 0;
11794 +       elm = rev->elm;
11795 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11796 +               AuDebugOn(elm->created && elm->info_last);
11797 +               if (elm->created) {
11798 +                       AuDbg("here\n");
11799 +                       delegated = NULL;
11800 +                       infopath.dentry = elm->info_dentry;
11801 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11802 +                                          !w->no_sio);
11803 +                       AuTraceErr(err);
11804 +                       if (unlikely(err == -EWOULDBLOCK))
11805 +                               iput(delegated);
11806 +                       dput(elm->info_dentry);
11807 +               } else if (elm->info_last) {
11808 +                       AuDbg("here\n");
11809 +                       w->fdata->drinfo = *elm->info_last;
11810 +                       memcpy(w->fdata->drinfo.oldname,
11811 +                              elm->info_last->oldname,
11812 +                              elm->info_last->oldnamelen);
11813 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11814 +                       kfree(elm->info_last);
11815 +               }
11816 +               if (unlikely(err))
11817 +                       AuIOErr("%d, %s\n", err, w->whname);
11818 +               /* go on even if err */
11819 +       }
11820 +}
11821 +
11822 +/* caller has to call au_dr_rename_fin() later */
11823 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11824 +                          struct qstr *dst_name, void *_rev)
11825 +{
11826 +       int err, sz, nelm;
11827 +       aufs_bindex_t bindex, btail;
11828 +       struct au_drinfo_store work;
11829 +       struct au_drinfo_rev *rev, **p;
11830 +       struct au_drinfo_rev_elm *elm;
11831 +       struct super_block *sb;
11832 +       struct au_branch *br;
11833 +       struct au_hinode *hdir;
11834 +
11835 +       err = au_drinfo_store_work_init(&work, btgt);
11836 +       AuTraceErr(err);
11837 +       if (unlikely(err))
11838 +               goto out;
11839 +
11840 +       err = -ENOMEM;
11841 +       btail = au_dbtaildir(dentry);
11842 +       nelm = btail - btgt;
11843 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11844 +       rev = kcalloc(1, sz, GFP_NOFS);
11845 +       if (unlikely(!rev)) {
11846 +               AuTraceErr(err);
11847 +               goto out_args;
11848 +       }
11849 +       rev->nelm = nelm;
11850 +       elm = rev->elm;
11851 +       p = _rev;
11852 +       *p = rev;
11853 +
11854 +       err = 0;
11855 +       sb = dentry->d_sb;
11856 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11857 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11858 +       hdir = au_hi(d_inode(dentry), btgt);
11859 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11860 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11861 +               work.h_dentry = au_h_dptr(dentry, bindex);
11862 +               if (!work.h_dentry)
11863 +                       continue;
11864 +
11865 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11866 +                                         &work.allocated);
11867 +               AuTraceErr(err);
11868 +               if (unlikely(err))
11869 +                       break;
11870 +
11871 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11872 +               br = au_sbr(sb, bindex);
11873 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11874 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11875 +                                                work.infonamelen);
11876 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11877 +                     work.whnamelen, work.whname,
11878 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11879 +                     work.fdata->drinfo.oldnamelen,
11880 +                     work.fdata->drinfo.oldname);
11881 +
11882 +               err = au_drinfo_store_sio(&work, elm);
11883 +               AuTraceErr(err);
11884 +               if (unlikely(err))
11885 +                       break;
11886 +       }
11887 +       if (unlikely(err)) {
11888 +               /* revert all drinfo */
11889 +               au_drinfo_store_rev(rev, &work);
11890 +               kfree(rev);
11891 +               *p = NULL;
11892 +       }
11893 +       au_hn_inode_unlock(hdir);
11894 +
11895 +out_args:
11896 +       au_drinfo_store_work_fin(&work);
11897 +out:
11898 +       return err;
11899 +}
11900 +
11901 +/* ---------------------------------------------------------------------- */
11902 +
11903 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11904 +                struct qstr *dst_name, void *_rev)
11905 +{
11906 +       int err, already;
11907 +       ino_t ino;
11908 +       struct super_block *sb;
11909 +       struct au_branch *br;
11910 +       struct au_dr_br *dr;
11911 +       struct dentry *h_dentry;
11912 +       struct inode *h_inode;
11913 +       struct au_dr_hino *ent;
11914 +       struct au_drinfo_rev *rev, **p;
11915 +
11916 +       AuDbg("bindex %d\n", bindex);
11917 +
11918 +       err = -ENOMEM;
11919 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11920 +       if (unlikely(!ent))
11921 +               goto out;
11922 +
11923 +       sb = src->d_sb;
11924 +       br = au_sbr(sb, bindex);
11925 +       dr = &br->br_dirren;
11926 +       h_dentry = au_h_dptr(src, bindex);
11927 +       h_inode = d_inode(h_dentry);
11928 +       ino = h_inode->i_ino;
11929 +       ent->dr_h_ino = ino;
11930 +       already = au_dr_hino_test_add(dr, ino, ent);
11931 +       AuDbg("b%d, hi%llu, already %d\n",
11932 +             bindex, (unsigned long long)ino, already);
11933 +
11934 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11935 +       AuTraceErr(err);
11936 +       if (!err) {
11937 +               p = _rev;
11938 +               rev = *p;
11939 +               rev->already = already;
11940 +               goto out; /* success */
11941 +       }
11942 +
11943 +       /* revert */
11944 +       if (!already)
11945 +               au_dr_hino_del(dr, ent);
11946 +       kfree(ent);
11947 +
11948 +out:
11949 +       AuTraceErr(err);
11950 +       return err;
11951 +}
11952 +
11953 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11954 +{
11955 +       struct au_drinfo_rev *rev;
11956 +       struct au_drinfo_rev_elm *elm;
11957 +       int nelm;
11958 +
11959 +       rev = _rev;
11960 +       elm = rev->elm;
11961 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11962 +               dput(elm->info_dentry);
11963 +               kfree(elm->info_last);
11964 +       }
11965 +       kfree(rev);
11966 +}
11967 +
11968 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11969 +{
11970 +       int err;
11971 +       struct au_drinfo_store work;
11972 +       struct au_drinfo_rev *rev = _rev;
11973 +       struct super_block *sb;
11974 +       struct au_branch *br;
11975 +       struct inode *h_inode;
11976 +       struct au_dr_br *dr;
11977 +       struct au_dr_hino *ent;
11978 +
11979 +       err = au_drinfo_store_work_init(&work, btgt);
11980 +       if (unlikely(err))
11981 +               goto out;
11982 +
11983 +       sb = src->d_sb;
11984 +       br = au_sbr(sb, btgt);
11985 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11986 +       work.h_ppath.mnt = au_br_mnt(br);
11987 +       au_drinfo_store_rev(rev, &work);
11988 +       au_drinfo_store_work_fin(&work);
11989 +       if (rev->already)
11990 +               goto out;
11991 +
11992 +       dr = &br->br_dirren;
11993 +       h_inode = d_inode(work.h_ppath.dentry);
11994 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11995 +       BUG_ON(!ent);
11996 +       au_dr_hino_del(dr, ent);
11997 +       kfree(ent);
11998 +
11999 +out:
12000 +       kfree(rev);
12001 +       if (unlikely(err))
12002 +               pr_err("failed to remove dirren info\n");
12003 +}
12004 +
12005 +/* ---------------------------------------------------------------------- */
12006 +
12007 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12008 +                                          char *whname, int whnamelen,
12009 +                                          struct dentry **info_dentry)
12010 +{
12011 +       struct au_drinfo *drinfo;
12012 +       struct file *f;
12013 +       struct inode *h_dir;
12014 +       struct path infopath;
12015 +       int unlocked;
12016 +
12017 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12018 +
12019 +       *info_dentry = NULL;
12020 +       drinfo = NULL;
12021 +       unlocked = 0;
12022 +       h_dir = d_inode(h_ppath->dentry);
12023 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
12024 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12025 +                                              whnamelen);
12026 +       if (IS_ERR(infopath.dentry)) {
12027 +               drinfo = (void *)infopath.dentry;
12028 +               goto out;
12029 +       }
12030 +
12031 +       if (d_is_negative(infopath.dentry))
12032 +               goto out_dput; /* success */
12033 +
12034 +       infopath.mnt = h_ppath->mnt;
12035 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
12036 +       inode_unlock_shared(h_dir);
12037 +       unlocked = 1;
12038 +       if (IS_ERR(f)) {
12039 +               drinfo = (void *)f;
12040 +               goto out_dput;
12041 +       }
12042 +
12043 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12044 +       if (IS_ERR_OR_NULL(drinfo))
12045 +               goto out_fput;
12046 +
12047 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12048 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
12049 +
12050 +out_fput:
12051 +       fput(f);
12052 +out_dput:
12053 +       dput(infopath.dentry);
12054 +out:
12055 +       if (!unlocked)
12056 +               inode_unlock_shared(h_dir);
12057 +       AuTraceErrPtr(drinfo);
12058 +       return drinfo;
12059 +}
12060 +
12061 +struct au_drinfo_do_load_args {
12062 +       struct au_drinfo **drinfop;
12063 +       struct path *h_ppath;
12064 +       char *whname;
12065 +       int whnamelen;
12066 +       struct dentry **info_dentry;
12067 +};
12068 +
12069 +static void au_call_drinfo_do_load(void *args)
12070 +{
12071 +       struct au_drinfo_do_load_args *a = args;
12072 +
12073 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12074 +                                       a->info_dentry);
12075 +}
12076 +
12077 +struct au_drinfo_load {
12078 +       struct path h_ppath;
12079 +       struct qstr *qname;
12080 +       unsigned char no_sio;
12081 +
12082 +       aufs_bindex_t ninfo;
12083 +       struct au_drinfo **drinfo;
12084 +};
12085 +
12086 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12087 +                         struct au_branch *br)
12088 +{
12089 +       int err, wkq_err, whnamelen, e;
12090 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12091 +               = AUFS_WH_DR_INFO_PFX;
12092 +       struct au_drinfo *drinfo;
12093 +       struct qstr oldname;
12094 +       struct inode *h_dir, *delegated;
12095 +       struct dentry *info_dentry;
12096 +       struct path infopath;
12097 +
12098 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12099 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12100 +                                   sizeof(whname) - whnamelen);
12101 +       if (w->no_sio)
12102 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12103 +                                          &info_dentry);
12104 +       else {
12105 +               struct au_drinfo_do_load_args args = {
12106 +                       .drinfop        = &drinfo,
12107 +                       .h_ppath        = &w->h_ppath,
12108 +                       .whname         = whname,
12109 +                       .whnamelen      = whnamelen,
12110 +                       .info_dentry    = &info_dentry
12111 +               };
12112 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12113 +               if (unlikely(wkq_err))
12114 +                       drinfo = ERR_PTR(wkq_err);
12115 +       }
12116 +       err = PTR_ERR(drinfo);
12117 +       if (IS_ERR_OR_NULL(drinfo))
12118 +               goto out;
12119 +
12120 +       err = 0;
12121 +       oldname.len = drinfo->oldnamelen;
12122 +       oldname.name = drinfo->oldname;
12123 +       if (au_qstreq(w->qname, &oldname)) {
12124 +               /* the name is renamed back */
12125 +               kfree(drinfo);
12126 +               drinfo = NULL;
12127 +
12128 +               infopath.dentry = info_dentry;
12129 +               infopath.mnt = w->h_ppath.mnt;
12130 +               h_dir = d_inode(w->h_ppath.dentry);
12131 +               delegated = NULL;
12132 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12133 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12134 +               inode_unlock(h_dir);
12135 +               if (unlikely(e))
12136 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12137 +               if (unlikely(e == -EWOULDBLOCK))
12138 +                       iput(delegated);
12139 +       }
12140 +       kfree(w->drinfo[bindex]);
12141 +       w->drinfo[bindex] = drinfo;
12142 +       dput(info_dentry);
12143 +
12144 +out:
12145 +       AuTraceErr(err);
12146 +       return err;
12147 +}
12148 +
12149 +/* ---------------------------------------------------------------------- */
12150 +
12151 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12152 +{
12153 +       struct au_drinfo **p = drinfo;
12154 +
12155 +       while (n-- > 0)
12156 +               kfree(*drinfo++);
12157 +       kfree(p);
12158 +}
12159 +
12160 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12161 +              aufs_bindex_t btgt)
12162 +{
12163 +       int err, ninfo;
12164 +       struct au_drinfo_load w;
12165 +       aufs_bindex_t bindex, bbot;
12166 +       struct au_branch *br;
12167 +       struct inode *h_dir;
12168 +       struct au_dr_hino *ent;
12169 +       struct super_block *sb;
12170 +
12171 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12172 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12173 +             AuLNPair(&lkup->whname), btgt);
12174 +
12175 +       sb = dentry->d_sb;
12176 +       bbot = au_sbbot(sb);
12177 +       w.ninfo = bbot + 1;
12178 +       if (!lkup->dirren.drinfo) {
12179 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12180 +                                             sizeof(*lkup->dirren.drinfo),
12181 +                                             GFP_NOFS);
12182 +               if (unlikely(!lkup->dirren.drinfo)) {
12183 +                       err = -ENOMEM;
12184 +                       goto out;
12185 +               }
12186 +               lkup->dirren.ninfo = w.ninfo;
12187 +       }
12188 +       w.drinfo = lkup->dirren.drinfo;
12189 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12190 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12191 +       AuDebugOn(!w.h_ppath.dentry);
12192 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12193 +       w.qname = &dentry->d_name;
12194 +
12195 +       ninfo = 0;
12196 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12197 +               br = au_sbr(sb, bindex);
12198 +               err = au_drinfo_load(&w, bindex, br);
12199 +               if (unlikely(err))
12200 +                       goto out_free;
12201 +               if (w.drinfo[bindex])
12202 +                       ninfo++;
12203 +       }
12204 +       if (!ninfo) {
12205 +               br = au_sbr(sb, btgt);
12206 +               h_dir = d_inode(w.h_ppath.dentry);
12207 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12208 +               AuDebugOn(!ent);
12209 +               au_dr_hino_del(&br->br_dirren, ent);
12210 +               kfree(ent);
12211 +       }
12212 +       goto out; /* success */
12213 +
12214 +out_free:
12215 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12216 +       lkup->dirren.ninfo = 0;
12217 +       lkup->dirren.drinfo = NULL;
12218 +out:
12219 +       AuTraceErr(err);
12220 +       return err;
12221 +}
12222 +
12223 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12224 +{
12225 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12226 +}
12227 +
12228 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12229 +{
12230 +       int err;
12231 +       struct au_drinfo *drinfo;
12232 +
12233 +       err = 0;
12234 +       if (!lkup->dirren.drinfo)
12235 +               goto out;
12236 +       AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12237 +       drinfo = lkup->dirren.drinfo[btgt + 1];
12238 +       if (!drinfo)
12239 +               goto out;
12240 +
12241 +       kfree(lkup->whname.name);
12242 +       lkup->whname.name = NULL;
12243 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12244 +       lkup->dirren.dr_name.name = drinfo->oldname;
12245 +       lkup->name = &lkup->dirren.dr_name;
12246 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12247 +       if (!err)
12248 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12249 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12250 +                     btgt);
12251 +
12252 +out:
12253 +       AuTraceErr(err);
12254 +       return err;
12255 +}
12256 +
12257 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12258 +                    ino_t h_ino)
12259 +{
12260 +       int match;
12261 +       struct au_drinfo *drinfo;
12262 +
12263 +       match = 1;
12264 +       if (!lkup->dirren.drinfo)
12265 +               goto out;
12266 +       AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12267 +       drinfo = lkup->dirren.drinfo[bindex + 1];
12268 +       if (!drinfo)
12269 +               goto out;
12270 +
12271 +       match = (drinfo->ino == h_ino);
12272 +       AuDbg("match %d\n", match);
12273 +
12274 +out:
12275 +       return match;
12276 +}
12277 +
12278 +/* ---------------------------------------------------------------------- */
12279 +
12280 +int au_dr_opt_set(struct super_block *sb)
12281 +{
12282 +       int err;
12283 +       aufs_bindex_t bindex, bbot;
12284 +       struct au_branch *br;
12285 +
12286 +       err = 0;
12287 +       bbot = au_sbbot(sb);
12288 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12289 +               br = au_sbr(sb, bindex);
12290 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12291 +       }
12292 +
12293 +       return err;
12294 +}
12295 +
12296 +int au_dr_opt_flush(struct super_block *sb)
12297 +{
12298 +       int err;
12299 +       aufs_bindex_t bindex, bbot;
12300 +       struct au_branch *br;
12301 +
12302 +       err = 0;
12303 +       bbot = au_sbbot(sb);
12304 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12305 +               br = au_sbr(sb, bindex);
12306 +               if (au_br_writable(br->br_perm))
12307 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12308 +       }
12309 +
12310 +       return err;
12311 +}
12312 +
12313 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12314 +{
12315 +       int err;
12316 +       aufs_bindex_t bindex, bbot;
12317 +       struct au_branch *br;
12318 +
12319 +       err = 0;
12320 +       if (!no_flush) {
12321 +               err = au_dr_opt_flush(sb);
12322 +               if (unlikely(err))
12323 +                       goto out;
12324 +       }
12325 +
12326 +       bbot = au_sbbot(sb);
12327 +       for (bindex = 0; bindex <= bbot; bindex++) {
12328 +               br = au_sbr(sb, bindex);
12329 +               au_dr_hino_free(&br->br_dirren);
12330 +       }
12331 +
12332 +out:
12333 +       return err;
12334 +}
12335 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12336 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12337 +++ linux/fs/aufs/dirren.h      2018-07-19 09:46:13.056647808 +0200
12338 @@ -0,0 +1,140 @@
12339 +/* SPDX-License-Identifier: GPL-2.0 */
12340 +/*
12341 + * Copyright (C) 2017-2018 Junjiro R. Okajima
12342 + *
12343 + * This program, aufs is free software; you can redistribute it and/or modify
12344 + * it under the terms of the GNU General Public License as published by
12345 + * the Free Software Foundation; either version 2 of the License, or
12346 + * (at your option) any later version.
12347 + *
12348 + * This program is distributed in the hope that it will be useful,
12349 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12350 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12351 + * GNU General Public License for more details.
12352 + *
12353 + * You should have received a copy of the GNU General Public License
12354 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12355 + */
12356 +
12357 +/*
12358 + * renamed dir info
12359 + */
12360 +
12361 +#ifndef __AUFS_DIRREN_H__
12362 +#define __AUFS_DIRREN_H__
12363 +
12364 +#ifdef __KERNEL__
12365 +
12366 +#include <linux/dcache.h>
12367 +#include <linux/statfs.h>
12368 +#include <linux/uuid.h>
12369 +#include "hbl.h"
12370 +
12371 +#define AuDirren_NHASH 100
12372 +
12373 +#ifdef CONFIG_AUFS_DIRREN
12374 +enum au_brid_type {
12375 +       AuBrid_Unset,
12376 +       AuBrid_UUID,
12377 +       AuBrid_FSID,
12378 +       AuBrid_DEV
12379 +};
12380 +
12381 +struct au_dr_brid {
12382 +       enum au_brid_type       type;
12383 +       union {
12384 +               uuid_t  uuid;   /* unimplemented yet */
12385 +               fsid_t  fsid;
12386 +               dev_t   dev;
12387 +       };
12388 +};
12389 +
12390 +/* 20 is the max digits length of ulong 64 */
12391 +/* brid-type "_" uuid "_" inum */
12392 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12393 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12394 +
12395 +struct au_dr_hino {
12396 +       struct hlist_bl_node    dr_hnode;
12397 +       ino_t                   dr_h_ino;
12398 +};
12399 +
12400 +struct au_dr_br {
12401 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12402 +       struct au_dr_brid       dr_brid;
12403 +};
12404 +
12405 +struct au_dr_lookup {
12406 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12407 +       struct qstr             dr_name; /* subset of dr_info */
12408 +       aufs_bindex_t           ninfo;
12409 +       struct au_drinfo        **drinfo;
12410 +};
12411 +#else
12412 +struct au_dr_hino;
12413 +/* empty */
12414 +struct au_dr_br { };
12415 +struct au_dr_lookup { };
12416 +#endif
12417 +
12418 +/* ---------------------------------------------------------------------- */
12419 +
12420 +struct au_branch;
12421 +struct au_do_lookup_args;
12422 +struct au_hinode;
12423 +#ifdef CONFIG_AUFS_DIRREN
12424 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12425 +                       struct au_dr_hino *add_ent);
12426 +void au_dr_hino_free(struct au_dr_br *dr);
12427 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12428 +                 const struct path *path);
12429 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12430 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12431 +                struct qstr *dst_name, void *_rev);
12432 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12433 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12434 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12435 +              aufs_bindex_t bindex);
12436 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12437 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12438 +                    ino_t h_ino);
12439 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12440 +int au_dr_opt_set(struct super_block *sb);
12441 +int au_dr_opt_flush(struct super_block *sb);
12442 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12443 +#else
12444 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12445 +          struct au_dr_hino *add_ent);
12446 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12447 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12448 +          const struct path *path);
12449 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12450 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12451 +          struct qstr *dst_name, void *_rev);
12452 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12453 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12454 +          void *rev);
12455 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12456 +          aufs_bindex_t bindex);
12457 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12458 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12459 +          aufs_bindex_t bindex, ino_t h_ino);
12460 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12461 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12462 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12463 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12464 +#endif
12465 +
12466 +/* ---------------------------------------------------------------------- */
12467 +
12468 +#ifdef CONFIG_AUFS_DIRREN
12469 +static inline int au_dr_ihash(ino_t h_ino)
12470 +{
12471 +       return h_ino % AuDirren_NHASH;
12472 +}
12473 +#else
12474 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12475 +#endif
12476 +
12477 +#endif /* __KERNEL__ */
12478 +#endif /* __AUFS_DIRREN_H__ */
12479 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12480 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12481 +++ linux/fs/aufs/dynop.c       2018-07-19 09:46:13.056647808 +0200
12482 @@ -0,0 +1,370 @@
12483 +/* SPDX-License-Identifier: GPL-2.0 */
12484 +/*
12485 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12486 + *
12487 + * This program, aufs is free software; you can redistribute it and/or modify
12488 + * it under the terms of the GNU General Public License as published by
12489 + * the Free Software Foundation; either version 2 of the License, or
12490 + * (at your option) any later version.
12491 + *
12492 + * This program is distributed in the hope that it will be useful,
12493 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12494 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12495 + * GNU General Public License for more details.
12496 + *
12497 + * You should have received a copy of the GNU General Public License
12498 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12499 + */
12500 +
12501 +/*
12502 + * dynamically customizable operations for regular files
12503 + */
12504 +
12505 +#include "aufs.h"
12506 +
12507 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12508 +
12509 +/*
12510 + * How large will these lists be?
12511 + * Usually just a few elements, 20-30 at most for each, I guess.
12512 + */
12513 +static struct hlist_bl_head dynop[AuDyLast];
12514 +
12515 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12516 +                                    const void *h_op)
12517 +{
12518 +       struct au_dykey *key, *tmp;
12519 +       struct hlist_bl_node *pos;
12520 +
12521 +       key = NULL;
12522 +       hlist_bl_lock(hbl);
12523 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12524 +               if (tmp->dk_op.dy_hop == h_op) {
12525 +                       key = tmp;
12526 +                       kref_get(&key->dk_kref);
12527 +                       break;
12528 +               }
12529 +       hlist_bl_unlock(hbl);
12530 +
12531 +       return key;
12532 +}
12533 +
12534 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12535 +{
12536 +       struct au_dykey **k, *found;
12537 +       const void *h_op = key->dk_op.dy_hop;
12538 +       int i;
12539 +
12540 +       found = NULL;
12541 +       k = br->br_dykey;
12542 +       for (i = 0; i < AuBrDynOp; i++)
12543 +               if (k[i]) {
12544 +                       if (k[i]->dk_op.dy_hop == h_op) {
12545 +                               found = k[i];
12546 +                               break;
12547 +                       }
12548 +               } else
12549 +                       break;
12550 +       if (!found) {
12551 +               spin_lock(&br->br_dykey_lock);
12552 +               for (; i < AuBrDynOp; i++)
12553 +                       if (k[i]) {
12554 +                               if (k[i]->dk_op.dy_hop == h_op) {
12555 +                                       found = k[i];
12556 +                                       break;
12557 +                               }
12558 +                       } else {
12559 +                               k[i] = key;
12560 +                               break;
12561 +                       }
12562 +               spin_unlock(&br->br_dykey_lock);
12563 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12564 +       }
12565 +
12566 +       return found;
12567 +}
12568 +
12569 +/* kref_get() if @key is already added */
12570 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12571 +{
12572 +       struct au_dykey *tmp, *found;
12573 +       struct hlist_bl_node *pos;
12574 +       const void *h_op = key->dk_op.dy_hop;
12575 +
12576 +       found = NULL;
12577 +       hlist_bl_lock(hbl);
12578 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12579 +               if (tmp->dk_op.dy_hop == h_op) {
12580 +                       kref_get(&tmp->dk_kref);
12581 +                       found = tmp;
12582 +                       break;
12583 +               }
12584 +       if (!found)
12585 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12586 +       hlist_bl_unlock(hbl);
12587 +
12588 +       if (!found)
12589 +               DyPrSym(key);
12590 +       return found;
12591 +}
12592 +
12593 +static void dy_free_rcu(struct rcu_head *rcu)
12594 +{
12595 +       struct au_dykey *key;
12596 +
12597 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12598 +       DyPrSym(key);
12599 +       kfree(key);
12600 +}
12601 +
12602 +static void dy_free(struct kref *kref)
12603 +{
12604 +       struct au_dykey *key;
12605 +       struct hlist_bl_head *hbl;
12606 +
12607 +       key = container_of(kref, struct au_dykey, dk_kref);
12608 +       hbl = dynop + key->dk_op.dy_type;
12609 +       au_hbl_del(&key->dk_hnode, hbl);
12610 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12611 +}
12612 +
12613 +void au_dy_put(struct au_dykey *key)
12614 +{
12615 +       kref_put(&key->dk_kref, dy_free);
12616 +}
12617 +
12618 +/* ---------------------------------------------------------------------- */
12619 +
12620 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12621 +
12622 +#ifdef CONFIG_AUFS_DEBUG
12623 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12624 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12625 +#else
12626 +#define DyDbgDeclare(cnt)      do {} while (0)
12627 +#define DyDbgInc(cnt)          do {} while (0)
12628 +#endif
12629 +
12630 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12631 +       DyDbgInc(cnt);                                                  \
12632 +       if (h_op->func) {                                               \
12633 +               if (src.func)                                           \
12634 +                       dst.func = src.func;                            \
12635 +               else                                                    \
12636 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12637 +       }                                                               \
12638 +} while (0)
12639 +
12640 +#define DySetForce(func, dst, src) do {                \
12641 +       AuDebugOn(!src.func);                   \
12642 +       DyDbgInc(cnt);                          \
12643 +       dst.func = src.func;                    \
12644 +} while (0)
12645 +
12646 +#define DySetAop(func) \
12647 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12648 +#define DySetAopForce(func) \
12649 +       DySetForce(func, dyaop->da_op, aufs_aop)
12650 +
12651 +static void dy_aop(struct au_dykey *key, const void *h_op,
12652 +                  struct super_block *h_sb __maybe_unused)
12653 +{
12654 +       struct au_dyaop *dyaop = (void *)key;
12655 +       const struct address_space_operations *h_aop = h_op;
12656 +       DyDbgDeclare(cnt);
12657 +
12658 +       AuDbg("%s\n", au_sbtype(h_sb));
12659 +
12660 +       DySetAop(writepage);
12661 +       DySetAopForce(readpage);        /* force */
12662 +       DySetAop(writepages);
12663 +       DySetAop(set_page_dirty);
12664 +       DySetAop(readpages);
12665 +       DySetAop(write_begin);
12666 +       DySetAop(write_end);
12667 +       DySetAop(bmap);
12668 +       DySetAop(invalidatepage);
12669 +       DySetAop(releasepage);
12670 +       DySetAop(freepage);
12671 +       /* this one will be changed according to an aufs mount option */
12672 +       DySetAop(direct_IO);
12673 +       DySetAop(migratepage);
12674 +       DySetAop(isolate_page);
12675 +       DySetAop(putback_page);
12676 +       DySetAop(launder_page);
12677 +       DySetAop(is_partially_uptodate);
12678 +       DySetAop(is_dirty_writeback);
12679 +       DySetAop(error_remove_page);
12680 +       DySetAop(swap_activate);
12681 +       DySetAop(swap_deactivate);
12682 +
12683 +       DyDbgSize(cnt, *h_aop);
12684 +}
12685 +
12686 +/* ---------------------------------------------------------------------- */
12687 +
12688 +static void dy_bug(struct kref *kref)
12689 +{
12690 +       BUG();
12691 +}
12692 +
12693 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12694 +{
12695 +       struct au_dykey *key, *old;
12696 +       struct hlist_bl_head *hbl;
12697 +       struct op {
12698 +               unsigned int sz;
12699 +               void (*set)(struct au_dykey *key, const void *h_op,
12700 +                           struct super_block *h_sb __maybe_unused);
12701 +       };
12702 +       static const struct op a[] = {
12703 +               [AuDy_AOP] = {
12704 +                       .sz     = sizeof(struct au_dyaop),
12705 +                       .set    = dy_aop
12706 +               }
12707 +       };
12708 +       const struct op *p;
12709 +
12710 +       hbl = dynop + op->dy_type;
12711 +       key = dy_gfind_get(hbl, op->dy_hop);
12712 +       if (key)
12713 +               goto out_add; /* success */
12714 +
12715 +       p = a + op->dy_type;
12716 +       key = kzalloc(p->sz, GFP_NOFS);
12717 +       if (unlikely(!key)) {
12718 +               key = ERR_PTR(-ENOMEM);
12719 +               goto out;
12720 +       }
12721 +
12722 +       key->dk_op.dy_hop = op->dy_hop;
12723 +       kref_init(&key->dk_kref);
12724 +       p->set(key, op->dy_hop, au_br_sb(br));
12725 +       old = dy_gadd(hbl, key);
12726 +       if (old) {
12727 +               kfree(key);
12728 +               key = old;
12729 +       }
12730 +
12731 +out_add:
12732 +       old = dy_bradd(br, key);
12733 +       if (old)
12734 +               /* its ref-count should never be zero here */
12735 +               kref_put(&key->dk_kref, dy_bug);
12736 +out:
12737 +       return key;
12738 +}
12739 +
12740 +/* ---------------------------------------------------------------------- */
12741 +/*
12742 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
12743 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12744 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12745 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12746 + * See the aufs manual in detail.
12747 + */
12748 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12749 +{
12750 +       if (!do_dx)
12751 +               dyaop->da_op.direct_IO = NULL;
12752 +       else
12753 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12754 +}
12755 +
12756 +static struct au_dyaop *dy_aget(struct au_branch *br,
12757 +                               const struct address_space_operations *h_aop,
12758 +                               int do_dx)
12759 +{
12760 +       struct au_dyaop *dyaop;
12761 +       struct au_dynop op;
12762 +
12763 +       op.dy_type = AuDy_AOP;
12764 +       op.dy_haop = h_aop;
12765 +       dyaop = (void *)dy_get(&op, br);
12766 +       if (IS_ERR(dyaop))
12767 +               goto out;
12768 +       dy_adx(dyaop, do_dx);
12769 +
12770 +out:
12771 +       return dyaop;
12772 +}
12773 +
12774 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12775 +               struct inode *h_inode)
12776 +{
12777 +       int err, do_dx;
12778 +       struct super_block *sb;
12779 +       struct au_branch *br;
12780 +       struct au_dyaop *dyaop;
12781 +
12782 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12783 +       IiMustWriteLock(inode);
12784 +
12785 +       sb = inode->i_sb;
12786 +       br = au_sbr(sb, bindex);
12787 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12788 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12789 +       err = PTR_ERR(dyaop);
12790 +       if (IS_ERR(dyaop))
12791 +               /* unnecessary to call dy_fput() */
12792 +               goto out;
12793 +
12794 +       err = 0;
12795 +       inode->i_mapping->a_ops = &dyaop->da_op;
12796 +
12797 +out:
12798 +       return err;
12799 +}
12800 +
12801 +/*
12802 + * Is it safe to replace a_ops during the inode/file is in operation?
12803 + * Yes, I hope so.
12804 + */
12805 +int au_dy_irefresh(struct inode *inode)
12806 +{
12807 +       int err;
12808 +       aufs_bindex_t btop;
12809 +       struct inode *h_inode;
12810 +
12811 +       err = 0;
12812 +       if (S_ISREG(inode->i_mode)) {
12813 +               btop = au_ibtop(inode);
12814 +               h_inode = au_h_iptr(inode, btop);
12815 +               err = au_dy_iaop(inode, btop, h_inode);
12816 +       }
12817 +       return err;
12818 +}
12819 +
12820 +void au_dy_arefresh(int do_dx)
12821 +{
12822 +       struct hlist_bl_head *hbl;
12823 +       struct hlist_bl_node *pos;
12824 +       struct au_dykey *key;
12825 +
12826 +       hbl = dynop + AuDy_AOP;
12827 +       hlist_bl_lock(hbl);
12828 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12829 +               dy_adx((void *)key, do_dx);
12830 +       hlist_bl_unlock(hbl);
12831 +}
12832 +
12833 +/* ---------------------------------------------------------------------- */
12834 +
12835 +void __init au_dy_init(void)
12836 +{
12837 +       int i;
12838 +
12839 +       /* make sure that 'struct au_dykey *' can be any type */
12840 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
12841 +
12842 +       for (i = 0; i < AuDyLast; i++)
12843 +               INIT_HLIST_BL_HEAD(dynop + i);
12844 +}
12845 +
12846 +void au_dy_fin(void)
12847 +{
12848 +       int i;
12849 +
12850 +       for (i = 0; i < AuDyLast; i++)
12851 +               WARN_ON(!hlist_bl_empty(dynop + i));
12852 +}
12853 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12854 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12855 +++ linux/fs/aufs/dynop.h       2018-07-19 09:46:13.056647808 +0200
12856 @@ -0,0 +1,75 @@
12857 +/* SPDX-License-Identifier: GPL-2.0 */
12858 +/*
12859 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12860 + *
12861 + * This program, aufs is free software; you can redistribute it and/or modify
12862 + * it under the terms of the GNU General Public License as published by
12863 + * the Free Software Foundation; either version 2 of the License, or
12864 + * (at your option) any later version.
12865 + *
12866 + * This program is distributed in the hope that it will be useful,
12867 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12868 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12869 + * GNU General Public License for more details.
12870 + *
12871 + * You should have received a copy of the GNU General Public License
12872 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12873 + */
12874 +
12875 +/*
12876 + * dynamically customizable operations (for regular files only)
12877 + */
12878 +
12879 +#ifndef __AUFS_DYNOP_H__
12880 +#define __AUFS_DYNOP_H__
12881 +
12882 +#ifdef __KERNEL__
12883 +
12884 +#include <linux/fs.h>
12885 +#include <linux/kref.h>
12886 +
12887 +enum {AuDy_AOP, AuDyLast};
12888 +
12889 +struct au_dynop {
12890 +       int                                             dy_type;
12891 +       union {
12892 +               const void                              *dy_hop;
12893 +               const struct address_space_operations   *dy_haop;
12894 +       };
12895 +};
12896 +
12897 +struct au_dykey {
12898 +       union {
12899 +               struct hlist_bl_node    dk_hnode;
12900 +               struct rcu_head         dk_rcu;
12901 +       };
12902 +       struct au_dynop         dk_op;
12903 +
12904 +       /*
12905 +        * during I am in the branch local array, kref is gotten. when the
12906 +        * branch is removed, kref is put.
12907 +        */
12908 +       struct kref             dk_kref;
12909 +};
12910 +
12911 +/* stop unioning since their sizes are very different from each other */
12912 +struct au_dyaop {
12913 +       struct au_dykey                 da_key;
12914 +       struct address_space_operations da_op; /* not const */
12915 +};
12916 +
12917 +/* ---------------------------------------------------------------------- */
12918 +
12919 +/* dynop.c */
12920 +struct au_branch;
12921 +void au_dy_put(struct au_dykey *key);
12922 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12923 +               struct inode *h_inode);
12924 +int au_dy_irefresh(struct inode *inode);
12925 +void au_dy_arefresh(int do_dio);
12926 +
12927 +void __init au_dy_init(void);
12928 +void au_dy_fin(void);
12929 +
12930 +#endif /* __KERNEL__ */
12931 +#endif /* __AUFS_DYNOP_H__ */
12932 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12933 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12934 +++ linux/fs/aufs/export.c      2018-07-19 09:46:13.056647808 +0200
12935 @@ -0,0 +1,838 @@
12936 +/* SPDX-License-Identifier: GPL-2.0 */
12937 +/*
12938 + * Copyright (C) 2005-2018 Junjiro R. Okajima
12939 + *
12940 + * This program, aufs is free software; you can redistribute it and/or modify
12941 + * it under the terms of the GNU General Public License as published by
12942 + * the Free Software Foundation; either version 2 of the License, or
12943 + * (at your option) any later version.
12944 + *
12945 + * This program is distributed in the hope that it will be useful,
12946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12948 + * GNU General Public License for more details.
12949 + *
12950 + * You should have received a copy of the GNU General Public License
12951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12952 + */
12953 +
12954 +/*
12955 + * export via nfs
12956 + */
12957 +
12958 +#include <linux/exportfs.h>
12959 +#include <linux/fs_struct.h>
12960 +#include <linux/namei.h>
12961 +#include <linux/nsproxy.h>
12962 +#include <linux/random.h>
12963 +#include <linux/writeback.h>
12964 +#include "aufs.h"
12965 +
12966 +union conv {
12967 +#ifdef CONFIG_AUFS_INO_T_64
12968 +       __u32 a[2];
12969 +#else
12970 +       __u32 a[1];
12971 +#endif
12972 +       ino_t ino;
12973 +};
12974 +
12975 +static ino_t decode_ino(__u32 *a)
12976 +{
12977 +       union conv u;
12978 +
12979 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12980 +       u.a[0] = a[0];
12981 +#ifdef CONFIG_AUFS_INO_T_64
12982 +       u.a[1] = a[1];
12983 +#endif
12984 +       return u.ino;
12985 +}
12986 +
12987 +static void encode_ino(__u32 *a, ino_t ino)
12988 +{
12989 +       union conv u;
12990 +
12991 +       u.ino = ino;
12992 +       a[0] = u.a[0];
12993 +#ifdef CONFIG_AUFS_INO_T_64
12994 +       a[1] = u.a[1];
12995 +#endif
12996 +}
12997 +
12998 +/* NFS file handle */
12999 +enum {
13000 +       Fh_br_id,
13001 +       Fh_sigen,
13002 +#ifdef CONFIG_AUFS_INO_T_64
13003 +       /* support 64bit inode number */
13004 +       Fh_ino1,
13005 +       Fh_ino2,
13006 +       Fh_dir_ino1,
13007 +       Fh_dir_ino2,
13008 +#else
13009 +       Fh_ino1,
13010 +       Fh_dir_ino1,
13011 +#endif
13012 +       Fh_igen,
13013 +       Fh_h_type,
13014 +       Fh_tail,
13015 +
13016 +       Fh_ino = Fh_ino1,
13017 +       Fh_dir_ino = Fh_dir_ino1
13018 +};
13019 +
13020 +static int au_test_anon(struct dentry *dentry)
13021 +{
13022 +       /* note: read d_flags without d_lock */
13023 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13024 +}
13025 +
13026 +int au_test_nfsd(void)
13027 +{
13028 +       int ret;
13029 +       struct task_struct *tsk = current;
13030 +       char comm[sizeof(tsk->comm)];
13031 +
13032 +       ret = 0;
13033 +       if (tsk->flags & PF_KTHREAD) {
13034 +               get_task_comm(comm, tsk);
13035 +               ret = !strcmp(comm, "nfsd");
13036 +       }
13037 +
13038 +       return ret;
13039 +}
13040 +
13041 +/* ---------------------------------------------------------------------- */
13042 +/* inode generation external table */
13043 +
13044 +void au_xigen_inc(struct inode *inode)
13045 +{
13046 +       loff_t pos;
13047 +       ssize_t sz;
13048 +       __u32 igen;
13049 +       struct super_block *sb;
13050 +       struct au_sbinfo *sbinfo;
13051 +
13052 +       sb = inode->i_sb;
13053 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
13054 +
13055 +       sbinfo = au_sbi(sb);
13056 +       pos = inode->i_ino;
13057 +       pos *= sizeof(igen);
13058 +       igen = inode->i_generation + 1;
13059 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13060 +                        sizeof(igen), &pos);
13061 +       if (sz == sizeof(igen))
13062 +               return; /* success */
13063 +
13064 +       if (unlikely(sz >= 0))
13065 +               AuIOErr("xigen error (%zd)\n", sz);
13066 +}
13067 +
13068 +int au_xigen_new(struct inode *inode)
13069 +{
13070 +       int err;
13071 +       loff_t pos;
13072 +       ssize_t sz;
13073 +       struct super_block *sb;
13074 +       struct au_sbinfo *sbinfo;
13075 +       struct file *file;
13076 +
13077 +       err = 0;
13078 +       /* todo: dirty, at mount time */
13079 +       if (inode->i_ino == AUFS_ROOT_INO)
13080 +               goto out;
13081 +       sb = inode->i_sb;
13082 +       SiMustAnyLock(sb);
13083 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13084 +               goto out;
13085 +
13086 +       err = -EFBIG;
13087 +       pos = inode->i_ino;
13088 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13089 +               AuIOErr1("too large i%lld\n", pos);
13090 +               goto out;
13091 +       }
13092 +       pos *= sizeof(inode->i_generation);
13093 +
13094 +       err = 0;
13095 +       sbinfo = au_sbi(sb);
13096 +       file = sbinfo->si_xigen;
13097 +       BUG_ON(!file);
13098 +
13099 +       if (vfsub_f_size_read(file)
13100 +           < pos + sizeof(inode->i_generation)) {
13101 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13102 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13103 +                                sizeof(inode->i_generation), &pos);
13104 +       } else
13105 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13106 +                               sizeof(inode->i_generation), &pos);
13107 +       if (sz == sizeof(inode->i_generation))
13108 +               goto out; /* success */
13109 +
13110 +       err = sz;
13111 +       if (unlikely(sz >= 0)) {
13112 +               err = -EIO;
13113 +               AuIOErr("xigen error (%zd)\n", sz);
13114 +       }
13115 +
13116 +out:
13117 +       return err;
13118 +}
13119 +
13120 +int au_xigen_set(struct super_block *sb, struct path *path)
13121 +{
13122 +       int err;
13123 +       struct au_sbinfo *sbinfo;
13124 +       struct file *file;
13125 +
13126 +       SiMustWriteLock(sb);
13127 +
13128 +       sbinfo = au_sbi(sb);
13129 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13130 +       err = PTR_ERR(file);
13131 +       if (IS_ERR(file))
13132 +               goto out;
13133 +       err = 0;
13134 +       if (sbinfo->si_xigen)
13135 +               fput(sbinfo->si_xigen);
13136 +       sbinfo->si_xigen = file;
13137 +
13138 +out:
13139 +       AuTraceErr(err);
13140 +       return err;
13141 +}
13142 +
13143 +void au_xigen_clr(struct super_block *sb)
13144 +{
13145 +       struct au_sbinfo *sbinfo;
13146 +
13147 +       SiMustWriteLock(sb);
13148 +
13149 +       sbinfo = au_sbi(sb);
13150 +       if (sbinfo->si_xigen) {
13151 +               fput(sbinfo->si_xigen);
13152 +               sbinfo->si_xigen = NULL;
13153 +       }
13154 +}
13155 +
13156 +/* ---------------------------------------------------------------------- */
13157 +
13158 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13159 +                                   ino_t dir_ino)
13160 +{
13161 +       struct dentry *dentry, *d;
13162 +       struct inode *inode;
13163 +       unsigned int sigen;
13164 +
13165 +       dentry = NULL;
13166 +       inode = ilookup(sb, ino);
13167 +       if (!inode)
13168 +               goto out;
13169 +
13170 +       dentry = ERR_PTR(-ESTALE);
13171 +       sigen = au_sigen(sb);
13172 +       if (unlikely(au_is_bad_inode(inode)
13173 +                    || IS_DEADDIR(inode)
13174 +                    || sigen != au_iigen(inode, NULL)))
13175 +               goto out_iput;
13176 +
13177 +       dentry = NULL;
13178 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13179 +               dentry = d_find_alias(inode);
13180 +       else {
13181 +               spin_lock(&inode->i_lock);
13182 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13183 +                       spin_lock(&d->d_lock);
13184 +                       if (!au_test_anon(d)
13185 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13186 +                               dentry = dget_dlock(d);
13187 +                               spin_unlock(&d->d_lock);
13188 +                               break;
13189 +                       }
13190 +                       spin_unlock(&d->d_lock);
13191 +               }
13192 +               spin_unlock(&inode->i_lock);
13193 +       }
13194 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13195 +               /* need to refresh */
13196 +               dput(dentry);
13197 +               dentry = NULL;
13198 +       }
13199 +
13200 +out_iput:
13201 +       iput(inode);
13202 +out:
13203 +       AuTraceErrPtr(dentry);
13204 +       return dentry;
13205 +}
13206 +
13207 +/* ---------------------------------------------------------------------- */
13208 +
13209 +/* todo: dirty? */
13210 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13211 +
13212 +struct au_compare_mnt_args {
13213 +       /* input */
13214 +       struct super_block *sb;
13215 +
13216 +       /* output */
13217 +       struct vfsmount *mnt;
13218 +};
13219 +
13220 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13221 +{
13222 +       struct au_compare_mnt_args *a = arg;
13223 +
13224 +       if (mnt->mnt_sb != a->sb)
13225 +               return 0;
13226 +       a->mnt = mntget(mnt);
13227 +       return 1;
13228 +}
13229 +
13230 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13231 +{
13232 +       int err;
13233 +       struct path root;
13234 +       struct au_compare_mnt_args args = {
13235 +               .sb = sb
13236 +       };
13237 +
13238 +       get_fs_root(current->fs, &root);
13239 +       rcu_read_lock();
13240 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13241 +       rcu_read_unlock();
13242 +       path_put(&root);
13243 +       AuDebugOn(!err);
13244 +       AuDebugOn(!args.mnt);
13245 +       return args.mnt;
13246 +}
13247 +
13248 +struct au_nfsd_si_lock {
13249 +       unsigned int sigen;
13250 +       aufs_bindex_t bindex, br_id;
13251 +       unsigned char force_lock;
13252 +};
13253 +
13254 +static int si_nfsd_read_lock(struct super_block *sb,
13255 +                            struct au_nfsd_si_lock *nsi_lock)
13256 +{
13257 +       int err;
13258 +       aufs_bindex_t bindex;
13259 +
13260 +       si_read_lock(sb, AuLock_FLUSH);
13261 +
13262 +       /* branch id may be wrapped around */
13263 +       err = 0;
13264 +       bindex = au_br_index(sb, nsi_lock->br_id);
13265 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13266 +               goto out; /* success */
13267 +
13268 +       err = -ESTALE;
13269 +       bindex = -1;
13270 +       if (!nsi_lock->force_lock)
13271 +               si_read_unlock(sb);
13272 +
13273 +out:
13274 +       nsi_lock->bindex = bindex;
13275 +       return err;
13276 +}
13277 +
13278 +struct find_name_by_ino {
13279 +       struct dir_context ctx;
13280 +       int called, found;
13281 +       ino_t ino;
13282 +       char *name;
13283 +       int namelen;
13284 +};
13285 +
13286 +static int
13287 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13288 +                loff_t offset, u64 ino, unsigned int d_type)
13289 +{
13290 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13291 +                                                 ctx);
13292 +
13293 +       a->called++;
13294 +       if (a->ino != ino)
13295 +               return 0;
13296 +
13297 +       memcpy(a->name, name, namelen);
13298 +       a->namelen = namelen;
13299 +       a->found = 1;
13300 +       return 1;
13301 +}
13302 +
13303 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13304 +                                    struct au_nfsd_si_lock *nsi_lock)
13305 +{
13306 +       struct dentry *dentry, *parent;
13307 +       struct file *file;
13308 +       struct inode *dir;
13309 +       struct find_name_by_ino arg = {
13310 +               .ctx = {
13311 +                       .actor = find_name_by_ino
13312 +               }
13313 +       };
13314 +       int err;
13315 +
13316 +       parent = path->dentry;
13317 +       if (nsi_lock)
13318 +               si_read_unlock(parent->d_sb);
13319 +       file = vfsub_dentry_open(path, au_dir_roflags);
13320 +       dentry = (void *)file;
13321 +       if (IS_ERR(file))
13322 +               goto out;
13323 +
13324 +       dentry = ERR_PTR(-ENOMEM);
13325 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13326 +       if (unlikely(!arg.name))
13327 +               goto out_file;
13328 +       arg.ino = ino;
13329 +       arg.found = 0;
13330 +       do {
13331 +               arg.called = 0;
13332 +               /* smp_mb(); */
13333 +               err = vfsub_iterate_dir(file, &arg.ctx);
13334 +       } while (!err && !arg.found && arg.called);
13335 +       dentry = ERR_PTR(err);
13336 +       if (unlikely(err))
13337 +               goto out_name;
13338 +       /* instead of ENOENT */
13339 +       dentry = ERR_PTR(-ESTALE);
13340 +       if (!arg.found)
13341 +               goto out_name;
13342 +
13343 +       /* do not call vfsub_lkup_one() */
13344 +       dir = d_inode(parent);
13345 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13346 +       AuTraceErrPtr(dentry);
13347 +       if (IS_ERR(dentry))
13348 +               goto out_name;
13349 +       AuDebugOn(au_test_anon(dentry));
13350 +       if (unlikely(d_really_is_negative(dentry))) {
13351 +               dput(dentry);
13352 +               dentry = ERR_PTR(-ENOENT);
13353 +       }
13354 +
13355 +out_name:
13356 +       free_page((unsigned long)arg.name);
13357 +out_file:
13358 +       fput(file);
13359 +out:
13360 +       if (unlikely(nsi_lock
13361 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13362 +               if (!IS_ERR(dentry)) {
13363 +                       dput(dentry);
13364 +                       dentry = ERR_PTR(-ESTALE);
13365 +               }
13366 +       AuTraceErrPtr(dentry);
13367 +       return dentry;
13368 +}
13369 +
13370 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13371 +                                       ino_t dir_ino,
13372 +                                       struct au_nfsd_si_lock *nsi_lock)
13373 +{
13374 +       struct dentry *dentry;
13375 +       struct path path;
13376 +
13377 +       if (dir_ino != AUFS_ROOT_INO) {
13378 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13379 +               dentry = path.dentry;
13380 +               if (!path.dentry || IS_ERR(path.dentry))
13381 +                       goto out;
13382 +               AuDebugOn(au_test_anon(path.dentry));
13383 +       } else
13384 +               path.dentry = dget(sb->s_root);
13385 +
13386 +       path.mnt = au_mnt_get(sb);
13387 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13388 +       path_put(&path);
13389 +
13390 +out:
13391 +       AuTraceErrPtr(dentry);
13392 +       return dentry;
13393 +}
13394 +
13395 +/* ---------------------------------------------------------------------- */
13396 +
13397 +static int h_acceptable(void *expv, struct dentry *dentry)
13398 +{
13399 +       return 1;
13400 +}
13401 +
13402 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13403 +                          char *buf, int len, struct super_block *sb)
13404 +{
13405 +       char *p;
13406 +       int n;
13407 +       struct path path;
13408 +
13409 +       p = d_path(h_rootpath, buf, len);
13410 +       if (IS_ERR(p))
13411 +               goto out;
13412 +       n = strlen(p);
13413 +
13414 +       path.mnt = h_rootpath->mnt;
13415 +       path.dentry = h_parent;
13416 +       p = d_path(&path, buf, len);
13417 +       if (IS_ERR(p))
13418 +               goto out;
13419 +       if (n != 1)
13420 +               p += n;
13421 +
13422 +       path.mnt = au_mnt_get(sb);
13423 +       path.dentry = sb->s_root;
13424 +       p = d_path(&path, buf, len - strlen(p));
13425 +       mntput(path.mnt);
13426 +       if (IS_ERR(p))
13427 +               goto out;
13428 +       if (n != 1)
13429 +               p[strlen(p)] = '/';
13430 +
13431 +out:
13432 +       AuTraceErrPtr(p);
13433 +       return p;
13434 +}
13435 +
13436 +static
13437 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13438 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13439 +{
13440 +       struct dentry *dentry, *h_parent, *root;
13441 +       struct super_block *h_sb;
13442 +       char *pathname, *p;
13443 +       struct vfsmount *h_mnt;
13444 +       struct au_branch *br;
13445 +       int err;
13446 +       struct path path;
13447 +
13448 +       br = au_sbr(sb, nsi_lock->bindex);
13449 +       h_mnt = au_br_mnt(br);
13450 +       h_sb = h_mnt->mnt_sb;
13451 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13452 +       lockdep_off();
13453 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13454 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13455 +                                     h_acceptable, /*context*/NULL);
13456 +       lockdep_on();
13457 +       dentry = h_parent;
13458 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13459 +               AuWarn1("%s decode_fh failed, %ld\n",
13460 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13461 +               goto out;
13462 +       }
13463 +       dentry = NULL;
13464 +       if (unlikely(au_test_anon(h_parent))) {
13465 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13466 +                       au_sbtype(h_sb));
13467 +               goto out_h_parent;
13468 +       }
13469 +
13470 +       dentry = ERR_PTR(-ENOMEM);
13471 +       pathname = (void *)__get_free_page(GFP_NOFS);
13472 +       if (unlikely(!pathname))
13473 +               goto out_h_parent;
13474 +
13475 +       root = sb->s_root;
13476 +       path.mnt = h_mnt;
13477 +       di_read_lock_parent(root, !AuLock_IR);
13478 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13479 +       di_read_unlock(root, !AuLock_IR);
13480 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13481 +       dentry = (void *)p;
13482 +       if (IS_ERR(p))
13483 +               goto out_pathname;
13484 +
13485 +       si_read_unlock(sb);
13486 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13487 +       dentry = ERR_PTR(err);
13488 +       if (unlikely(err))
13489 +               goto out_relock;
13490 +
13491 +       dentry = ERR_PTR(-ENOENT);
13492 +       AuDebugOn(au_test_anon(path.dentry));
13493 +       if (unlikely(d_really_is_negative(path.dentry)))
13494 +               goto out_path;
13495 +
13496 +       if (ino != d_inode(path.dentry)->i_ino)
13497 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13498 +       else
13499 +               dentry = dget(path.dentry);
13500 +
13501 +out_path:
13502 +       path_put(&path);
13503 +out_relock:
13504 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13505 +               if (!IS_ERR(dentry)) {
13506 +                       dput(dentry);
13507 +                       dentry = ERR_PTR(-ESTALE);
13508 +               }
13509 +out_pathname:
13510 +       free_page((unsigned long)pathname);
13511 +out_h_parent:
13512 +       dput(h_parent);
13513 +out:
13514 +       AuTraceErrPtr(dentry);
13515 +       return dentry;
13516 +}
13517 +
13518 +/* ---------------------------------------------------------------------- */
13519 +
13520 +static struct dentry *
13521 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13522 +                 int fh_type)
13523 +{
13524 +       struct dentry *dentry;
13525 +       __u32 *fh = fid->raw;
13526 +       struct au_branch *br;
13527 +       ino_t ino, dir_ino;
13528 +       struct au_nfsd_si_lock nsi_lock = {
13529 +               .force_lock     = 0
13530 +       };
13531 +
13532 +       dentry = ERR_PTR(-ESTALE);
13533 +       /* it should never happen, but the file handle is unreliable */
13534 +       if (unlikely(fh_len < Fh_tail))
13535 +               goto out;
13536 +       nsi_lock.sigen = fh[Fh_sigen];
13537 +       nsi_lock.br_id = fh[Fh_br_id];
13538 +
13539 +       /* branch id may be wrapped around */
13540 +       br = NULL;
13541 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13542 +               goto out;
13543 +       nsi_lock.force_lock = 1;
13544 +
13545 +       /* is this inode still cached? */
13546 +       ino = decode_ino(fh + Fh_ino);
13547 +       /* it should never happen */
13548 +       if (unlikely(ino == AUFS_ROOT_INO))
13549 +               goto out_unlock;
13550 +
13551 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13552 +       dentry = decode_by_ino(sb, ino, dir_ino);
13553 +       if (IS_ERR(dentry))
13554 +               goto out_unlock;
13555 +       if (dentry)
13556 +               goto accept;
13557 +
13558 +       /* is the parent dir cached? */
13559 +       br = au_sbr(sb, nsi_lock.bindex);
13560 +       au_br_get(br);
13561 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13562 +       if (IS_ERR(dentry))
13563 +               goto out_unlock;
13564 +       if (dentry)
13565 +               goto accept;
13566 +
13567 +       /* lookup path */
13568 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13569 +       if (IS_ERR(dentry))
13570 +               goto out_unlock;
13571 +       if (unlikely(!dentry))
13572 +               /* todo?: make it ESTALE */
13573 +               goto out_unlock;
13574 +
13575 +accept:
13576 +       if (!au_digen_test(dentry, au_sigen(sb))
13577 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13578 +               goto out_unlock; /* success */
13579 +
13580 +       dput(dentry);
13581 +       dentry = ERR_PTR(-ESTALE);
13582 +out_unlock:
13583 +       if (br)
13584 +               au_br_put(br);
13585 +       si_read_unlock(sb);
13586 +out:
13587 +       AuTraceErrPtr(dentry);
13588 +       return dentry;
13589 +}
13590 +
13591 +#if 0 /* reserved for future use */
13592 +/* support subtreecheck option */
13593 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13594 +                                       int fh_len, int fh_type)
13595 +{
13596 +       struct dentry *parent;
13597 +       __u32 *fh = fid->raw;
13598 +       ino_t dir_ino;
13599 +
13600 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13601 +       parent = decode_by_ino(sb, dir_ino, 0);
13602 +       if (IS_ERR(parent))
13603 +               goto out;
13604 +       if (!parent)
13605 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13606 +                                       dir_ino, fh, fh_len);
13607 +
13608 +out:
13609 +       AuTraceErrPtr(parent);
13610 +       return parent;
13611 +}
13612 +#endif
13613 +
13614 +/* ---------------------------------------------------------------------- */
13615 +
13616 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13617 +                         struct inode *dir)
13618 +{
13619 +       int err;
13620 +       aufs_bindex_t bindex;
13621 +       struct super_block *sb, *h_sb;
13622 +       struct dentry *dentry, *parent, *h_parent;
13623 +       struct inode *h_dir;
13624 +       struct au_branch *br;
13625 +
13626 +       err = -ENOSPC;
13627 +       if (unlikely(*max_len <= Fh_tail)) {
13628 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13629 +               goto out;
13630 +       }
13631 +
13632 +       err = FILEID_ROOT;
13633 +       if (inode->i_ino == AUFS_ROOT_INO) {
13634 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13635 +               goto out;
13636 +       }
13637 +
13638 +       h_parent = NULL;
13639 +       sb = inode->i_sb;
13640 +       err = si_read_lock(sb, AuLock_FLUSH);
13641 +       if (unlikely(err))
13642 +               goto out;
13643 +
13644 +#ifdef CONFIG_AUFS_DEBUG
13645 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13646 +               AuWarn1("NFS-exporting requires xino\n");
13647 +#endif
13648 +       err = -EIO;
13649 +       parent = NULL;
13650 +       ii_read_lock_child(inode);
13651 +       bindex = au_ibtop(inode);
13652 +       if (!dir) {
13653 +               dentry = d_find_any_alias(inode);
13654 +               if (unlikely(!dentry))
13655 +                       goto out_unlock;
13656 +               AuDebugOn(au_test_anon(dentry));
13657 +               parent = dget_parent(dentry);
13658 +               dput(dentry);
13659 +               if (unlikely(!parent))
13660 +                       goto out_unlock;
13661 +               if (d_really_is_positive(parent))
13662 +                       dir = d_inode(parent);
13663 +       }
13664 +
13665 +       ii_read_lock_parent(dir);
13666 +       h_dir = au_h_iptr(dir, bindex);
13667 +       ii_read_unlock(dir);
13668 +       if (unlikely(!h_dir))
13669 +               goto out_parent;
13670 +       h_parent = d_find_any_alias(h_dir);
13671 +       if (unlikely(!h_parent))
13672 +               goto out_hparent;
13673 +
13674 +       err = -EPERM;
13675 +       br = au_sbr(sb, bindex);
13676 +       h_sb = au_br_sb(br);
13677 +       if (unlikely(!h_sb->s_export_op)) {
13678 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13679 +               goto out_hparent;
13680 +       }
13681 +
13682 +       fh[Fh_br_id] = br->br_id;
13683 +       fh[Fh_sigen] = au_sigen(sb);
13684 +       encode_ino(fh + Fh_ino, inode->i_ino);
13685 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13686 +       fh[Fh_igen] = inode->i_generation;
13687 +
13688 +       *max_len -= Fh_tail;
13689 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13690 +                                          max_len,
13691 +                                          /*connectable or subtreecheck*/0);
13692 +       err = fh[Fh_h_type];
13693 +       *max_len += Fh_tail;
13694 +       /* todo: macros? */
13695 +       if (err != FILEID_INVALID)
13696 +               err = 99;
13697 +       else
13698 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13699 +
13700 +out_hparent:
13701 +       dput(h_parent);
13702 +out_parent:
13703 +       dput(parent);
13704 +out_unlock:
13705 +       ii_read_unlock(inode);
13706 +       si_read_unlock(sb);
13707 +out:
13708 +       if (unlikely(err < 0))
13709 +               err = FILEID_INVALID;
13710 +       return err;
13711 +}
13712 +
13713 +/* ---------------------------------------------------------------------- */
13714 +
13715 +static int aufs_commit_metadata(struct inode *inode)
13716 +{
13717 +       int err;
13718 +       aufs_bindex_t bindex;
13719 +       struct super_block *sb;
13720 +       struct inode *h_inode;
13721 +       int (*f)(struct inode *inode);
13722 +
13723 +       sb = inode->i_sb;
13724 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13725 +       ii_write_lock_child(inode);
13726 +       bindex = au_ibtop(inode);
13727 +       AuDebugOn(bindex < 0);
13728 +       h_inode = au_h_iptr(inode, bindex);
13729 +
13730 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13731 +       if (f)
13732 +               err = f(h_inode);
13733 +       else {
13734 +               struct writeback_control wbc = {
13735 +                       .sync_mode      = WB_SYNC_ALL,
13736 +                       .nr_to_write    = 0 /* metadata only */
13737 +               };
13738 +
13739 +               err = sync_inode(h_inode, &wbc);
13740 +       }
13741 +
13742 +       au_cpup_attr_timesizes(inode);
13743 +       ii_write_unlock(inode);
13744 +       si_read_unlock(sb);
13745 +       return err;
13746 +}
13747 +
13748 +/* ---------------------------------------------------------------------- */
13749 +
13750 +static struct export_operations aufs_export_op = {
13751 +       .fh_to_dentry           = aufs_fh_to_dentry,
13752 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13753 +       .encode_fh              = aufs_encode_fh,
13754 +       .commit_metadata        = aufs_commit_metadata
13755 +};
13756 +
13757 +void au_export_init(struct super_block *sb)
13758 +{
13759 +       struct au_sbinfo *sbinfo;
13760 +       __u32 u;
13761 +
13762 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13763 +                        && IS_MODULE(CONFIG_EXPORTFS),
13764 +                        AUFS_NAME ": unsupported configuration "
13765 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13766 +
13767 +       sb->s_export_op = &aufs_export_op;
13768 +       sbinfo = au_sbi(sb);
13769 +       sbinfo->si_xigen = NULL;
13770 +       get_random_bytes(&u, sizeof(u));
13771 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13772 +       atomic_set(&sbinfo->si_xigen_next, u);
13773 +}
13774 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13775 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13776 +++ linux/fs/aufs/fhsm.c        2018-07-19 09:46:13.056647808 +0200
13777 @@ -0,0 +1,427 @@
13778 +/* SPDX-License-Identifier: GPL-2.0 */
13779 +/*
13780 + * Copyright (C) 2011-2018 Junjiro R. Okajima
13781 + *
13782 + * This program, aufs is free software; you can redistribute it and/or modify
13783 + * it under the terms of the GNU General Public License as published by
13784 + * the Free Software Foundation; either version 2 of the License, or
13785 + * (at your option) any later version.
13786 + *
13787 + * This program is distributed in the hope that it will be useful,
13788 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13789 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13790 + * GNU General Public License for more details.
13791 + *
13792 + * You should have received a copy of the GNU General Public License
13793 + * along with this program; if not, write to the Free Software
13794 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13795 + */
13796 +
13797 +/*
13798 + * File-based Hierarchy Storage Management
13799 + */
13800 +
13801 +#include <linux/anon_inodes.h>
13802 +#include <linux/poll.h>
13803 +#include <linux/seq_file.h>
13804 +#include <linux/statfs.h>
13805 +#include "aufs.h"
13806 +
13807 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13808 +{
13809 +       struct au_sbinfo *sbinfo;
13810 +       struct au_fhsm *fhsm;
13811 +
13812 +       SiMustAnyLock(sb);
13813 +
13814 +       sbinfo = au_sbi(sb);
13815 +       fhsm = &sbinfo->si_fhsm;
13816 +       AuDebugOn(!fhsm);
13817 +       return fhsm->fhsm_bottom;
13818 +}
13819 +
13820 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13821 +{
13822 +       struct au_sbinfo *sbinfo;
13823 +       struct au_fhsm *fhsm;
13824 +
13825 +       SiMustWriteLock(sb);
13826 +
13827 +       sbinfo = au_sbi(sb);
13828 +       fhsm = &sbinfo->si_fhsm;
13829 +       AuDebugOn(!fhsm);
13830 +       fhsm->fhsm_bottom = bindex;
13831 +}
13832 +
13833 +/* ---------------------------------------------------------------------- */
13834 +
13835 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13836 +{
13837 +       struct au_br_fhsm *bf;
13838 +
13839 +       bf = br->br_fhsm;
13840 +       MtxMustLock(&bf->bf_lock);
13841 +
13842 +       return !bf->bf_readable
13843 +               || time_after(jiffies,
13844 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13845 +}
13846 +
13847 +/* ---------------------------------------------------------------------- */
13848 +
13849 +static void au_fhsm_notify(struct super_block *sb, int val)
13850 +{
13851 +       struct au_sbinfo *sbinfo;
13852 +       struct au_fhsm *fhsm;
13853 +
13854 +       SiMustAnyLock(sb);
13855 +
13856 +       sbinfo = au_sbi(sb);
13857 +       fhsm = &sbinfo->si_fhsm;
13858 +       if (au_fhsm_pid(fhsm)
13859 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13860 +               atomic_set(&fhsm->fhsm_readable, val);
13861 +               if (val)
13862 +                       wake_up(&fhsm->fhsm_wqh);
13863 +       }
13864 +}
13865 +
13866 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13867 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13868 +{
13869 +       int err;
13870 +       struct au_branch *br;
13871 +       struct au_br_fhsm *bf;
13872 +
13873 +       br = au_sbr(sb, bindex);
13874 +       AuDebugOn(au_br_rdonly(br));
13875 +       bf = br->br_fhsm;
13876 +       AuDebugOn(!bf);
13877 +
13878 +       if (do_lock)
13879 +               mutex_lock(&bf->bf_lock);
13880 +       else
13881 +               MtxMustLock(&bf->bf_lock);
13882 +
13883 +       /* sb->s_root for NFS is unreliable */
13884 +       err = au_br_stfs(br, &bf->bf_stfs);
13885 +       if (unlikely(err)) {
13886 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13887 +               goto out;
13888 +       }
13889 +
13890 +       bf->bf_jiffy = jiffies;
13891 +       bf->bf_readable = 1;
13892 +       if (do_notify)
13893 +               au_fhsm_notify(sb, /*val*/1);
13894 +       if (rstfs)
13895 +               *rstfs = bf->bf_stfs;
13896 +
13897 +out:
13898 +       if (do_lock)
13899 +               mutex_unlock(&bf->bf_lock);
13900 +       au_fhsm_notify(sb, /*val*/1);
13901 +
13902 +       return err;
13903 +}
13904 +
13905 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13906 +{
13907 +       int err;
13908 +       struct au_sbinfo *sbinfo;
13909 +       struct au_fhsm *fhsm;
13910 +       struct au_branch *br;
13911 +       struct au_br_fhsm *bf;
13912 +
13913 +       AuDbg("b%d, force %d\n", bindex, force);
13914 +       SiMustAnyLock(sb);
13915 +
13916 +       sbinfo = au_sbi(sb);
13917 +       fhsm = &sbinfo->si_fhsm;
13918 +       if (!au_ftest_si(sbinfo, FHSM)
13919 +           || fhsm->fhsm_bottom == bindex)
13920 +               return;
13921 +
13922 +       br = au_sbr(sb, bindex);
13923 +       bf = br->br_fhsm;
13924 +       AuDebugOn(!bf);
13925 +       mutex_lock(&bf->bf_lock);
13926 +       if (force
13927 +           || au_fhsm_pid(fhsm)
13928 +           || au_fhsm_test_jiffy(sbinfo, br))
13929 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13930 +                                 /*do_notify*/1);
13931 +       mutex_unlock(&bf->bf_lock);
13932 +}
13933 +
13934 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13935 +{
13936 +       aufs_bindex_t bindex, bbot;
13937 +       struct au_branch *br;
13938 +
13939 +       /* exclude the bottom */
13940 +       bbot = au_fhsm_bottom(sb);
13941 +       for (bindex = 0; bindex < bbot; bindex++) {
13942 +               br = au_sbr(sb, bindex);
13943 +               if (au_br_fhsm(br->br_perm))
13944 +                       au_fhsm_wrote(sb, bindex, force);
13945 +       }
13946 +}
13947 +
13948 +/* ---------------------------------------------------------------------- */
13949 +
13950 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13951 +{
13952 +       __poll_t mask;
13953 +       struct au_sbinfo *sbinfo;
13954 +       struct au_fhsm *fhsm;
13955 +
13956 +       mask = 0;
13957 +       sbinfo = file->private_data;
13958 +       fhsm = &sbinfo->si_fhsm;
13959 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13960 +       if (atomic_read(&fhsm->fhsm_readable))
13961 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13962 +
13963 +       if (!mask)
13964 +               AuDbg("mask 0x%x\n", mask);
13965 +       return mask;
13966 +}
13967 +
13968 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13969 +                             struct aufs_stfs *stfs, __s16 brid)
13970 +{
13971 +       int err;
13972 +
13973 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13974 +       if (!err)
13975 +               err = __put_user(brid, &stbr->brid);
13976 +       if (unlikely(err))
13977 +               err = -EFAULT;
13978 +
13979 +       return err;
13980 +}
13981 +
13982 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13983 +                              struct aufs_stbr __user *stbr, size_t count)
13984 +{
13985 +       ssize_t err;
13986 +       int nstbr;
13987 +       aufs_bindex_t bindex, bbot;
13988 +       struct au_branch *br;
13989 +       struct au_br_fhsm *bf;
13990 +
13991 +       /* except the bottom branch */
13992 +       err = 0;
13993 +       nstbr = 0;
13994 +       bbot = au_fhsm_bottom(sb);
13995 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13996 +               br = au_sbr(sb, bindex);
13997 +               if (!au_br_fhsm(br->br_perm))
13998 +                       continue;
13999 +
14000 +               bf = br->br_fhsm;
14001 +               mutex_lock(&bf->bf_lock);
14002 +               if (bf->bf_readable) {
14003 +                       err = -EFAULT;
14004 +                       if (count >= sizeof(*stbr))
14005 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14006 +                                                         br->br_id);
14007 +                       if (!err) {
14008 +                               bf->bf_readable = 0;
14009 +                               count -= sizeof(*stbr);
14010 +                               nstbr++;
14011 +                       }
14012 +               }
14013 +               mutex_unlock(&bf->bf_lock);
14014 +       }
14015 +       if (!err)
14016 +               err = sizeof(*stbr) * nstbr;
14017 +
14018 +       return err;
14019 +}
14020 +
14021 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14022 +                          loff_t *pos)
14023 +{
14024 +       ssize_t err;
14025 +       int readable;
14026 +       aufs_bindex_t nfhsm, bindex, bbot;
14027 +       struct au_sbinfo *sbinfo;
14028 +       struct au_fhsm *fhsm;
14029 +       struct au_branch *br;
14030 +       struct super_block *sb;
14031 +
14032 +       err = 0;
14033 +       sbinfo = file->private_data;
14034 +       fhsm = &sbinfo->si_fhsm;
14035 +need_data:
14036 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
14037 +       if (!atomic_read(&fhsm->fhsm_readable)) {
14038 +               if (vfsub_file_flags(file) & O_NONBLOCK)
14039 +                       err = -EAGAIN;
14040 +               else
14041 +                       err = wait_event_interruptible_locked_irq
14042 +                               (fhsm->fhsm_wqh,
14043 +                                atomic_read(&fhsm->fhsm_readable));
14044 +       }
14045 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14046 +       if (unlikely(err))
14047 +               goto out;
14048 +
14049 +       /* sb may already be dead */
14050 +       au_rw_read_lock(&sbinfo->si_rwsem);
14051 +       readable = atomic_read(&fhsm->fhsm_readable);
14052 +       if (readable > 0) {
14053 +               sb = sbinfo->si_sb;
14054 +               AuDebugOn(!sb);
14055 +               /* exclude the bottom branch */
14056 +               nfhsm = 0;
14057 +               bbot = au_fhsm_bottom(sb);
14058 +               for (bindex = 0; bindex < bbot; bindex++) {
14059 +                       br = au_sbr(sb, bindex);
14060 +                       if (au_br_fhsm(br->br_perm))
14061 +                               nfhsm++;
14062 +               }
14063 +               err = -EMSGSIZE;
14064 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14065 +                       atomic_set(&fhsm->fhsm_readable, 0);
14066 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14067 +                                            count);
14068 +               }
14069 +       }
14070 +       au_rw_read_unlock(&sbinfo->si_rwsem);
14071 +       if (!readable)
14072 +               goto need_data;
14073 +
14074 +out:
14075 +       return err;
14076 +}
14077 +
14078 +static int au_fhsm_release(struct inode *inode, struct file *file)
14079 +{
14080 +       struct au_sbinfo *sbinfo;
14081 +       struct au_fhsm *fhsm;
14082 +
14083 +       /* sb may already be dead */
14084 +       sbinfo = file->private_data;
14085 +       fhsm = &sbinfo->si_fhsm;
14086 +       spin_lock(&fhsm->fhsm_spin);
14087 +       fhsm->fhsm_pid = 0;
14088 +       spin_unlock(&fhsm->fhsm_spin);
14089 +       kobject_put(&sbinfo->si_kobj);
14090 +
14091 +       return 0;
14092 +}
14093 +
14094 +static const struct file_operations au_fhsm_fops = {
14095 +       .owner          = THIS_MODULE,
14096 +       .llseek         = noop_llseek,
14097 +       .read           = au_fhsm_read,
14098 +       .poll           = au_fhsm_poll,
14099 +       .release        = au_fhsm_release
14100 +};
14101 +
14102 +int au_fhsm_fd(struct super_block *sb, int oflags)
14103 +{
14104 +       int err, fd;
14105 +       struct au_sbinfo *sbinfo;
14106 +       struct au_fhsm *fhsm;
14107 +
14108 +       err = -EPERM;
14109 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14110 +               goto out;
14111 +
14112 +       err = -EINVAL;
14113 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14114 +               goto out;
14115 +
14116 +       err = 0;
14117 +       sbinfo = au_sbi(sb);
14118 +       fhsm = &sbinfo->si_fhsm;
14119 +       spin_lock(&fhsm->fhsm_spin);
14120 +       if (!fhsm->fhsm_pid)
14121 +               fhsm->fhsm_pid = current->pid;
14122 +       else
14123 +               err = -EBUSY;
14124 +       spin_unlock(&fhsm->fhsm_spin);
14125 +       if (unlikely(err))
14126 +               goto out;
14127 +
14128 +       oflags |= O_RDONLY;
14129 +       /* oflags |= FMODE_NONOTIFY; */
14130 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14131 +       err = fd;
14132 +       if (unlikely(fd < 0))
14133 +               goto out_pid;
14134 +
14135 +       /* succeed reglardless 'fhsm' status */
14136 +       kobject_get(&sbinfo->si_kobj);
14137 +       si_noflush_read_lock(sb);
14138 +       if (au_ftest_si(sbinfo, FHSM))
14139 +               au_fhsm_wrote_all(sb, /*force*/0);
14140 +       si_read_unlock(sb);
14141 +       goto out; /* success */
14142 +
14143 +out_pid:
14144 +       spin_lock(&fhsm->fhsm_spin);
14145 +       fhsm->fhsm_pid = 0;
14146 +       spin_unlock(&fhsm->fhsm_spin);
14147 +out:
14148 +       AuTraceErr(err);
14149 +       return err;
14150 +}
14151 +
14152 +/* ---------------------------------------------------------------------- */
14153 +
14154 +int au_fhsm_br_alloc(struct au_branch *br)
14155 +{
14156 +       int err;
14157 +
14158 +       err = 0;
14159 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14160 +       if (br->br_fhsm)
14161 +               au_br_fhsm_init(br->br_fhsm);
14162 +       else
14163 +               err = -ENOMEM;
14164 +
14165 +       return err;
14166 +}
14167 +
14168 +/* ---------------------------------------------------------------------- */
14169 +
14170 +void au_fhsm_fin(struct super_block *sb)
14171 +{
14172 +       au_fhsm_notify(sb, /*val*/-1);
14173 +}
14174 +
14175 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14176 +{
14177 +       struct au_fhsm *fhsm;
14178 +
14179 +       fhsm = &sbinfo->si_fhsm;
14180 +       spin_lock_init(&fhsm->fhsm_spin);
14181 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14182 +       atomic_set(&fhsm->fhsm_readable, 0);
14183 +       fhsm->fhsm_expire
14184 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14185 +       fhsm->fhsm_bottom = -1;
14186 +}
14187 +
14188 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14189 +{
14190 +       sbinfo->si_fhsm.fhsm_expire
14191 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14192 +}
14193 +
14194 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14195 +{
14196 +       unsigned int u;
14197 +
14198 +       if (!au_ftest_si(sbinfo, FHSM))
14199 +               return;
14200 +
14201 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14202 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14203 +               seq_printf(seq, ",fhsm_sec=%u", u);
14204 +}
14205 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14206 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14207 +++ linux/fs/aufs/file.c        2018-07-19 09:46:13.056647808 +0200
14208 @@ -0,0 +1,857 @@
14209 +/* SPDX-License-Identifier: GPL-2.0 */
14210 +/*
14211 + * Copyright (C) 2005-2018 Junjiro R. Okajima
14212 + *
14213 + * This program, aufs is free software; you can redistribute it and/or modify
14214 + * it under the terms of the GNU General Public License as published by
14215 + * the Free Software Foundation; either version 2 of the License, or
14216 + * (at your option) any later version.
14217 + *
14218 + * This program is distributed in the hope that it will be useful,
14219 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14220 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14221 + * GNU General Public License for more details.
14222 + *
14223 + * You should have received a copy of the GNU General Public License
14224 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14225 + */
14226 +
14227 +/*
14228 + * handling file/dir, and address_space operation
14229 + */
14230 +
14231 +#ifdef CONFIG_AUFS_DEBUG
14232 +#include <linux/migrate.h>
14233 +#endif
14234 +#include <linux/pagemap.h>
14235 +#include "aufs.h"
14236 +
14237 +/* drop flags for writing */
14238 +unsigned int au_file_roflags(unsigned int flags)
14239 +{
14240 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14241 +       flags |= O_RDONLY | O_NOATIME;
14242 +       return flags;
14243 +}
14244 +
14245 +/* common functions to regular file and dir */
14246 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14247 +                      struct file *file, int force_wr)
14248 +{
14249 +       struct file *h_file;
14250 +       struct dentry *h_dentry;
14251 +       struct inode *h_inode;
14252 +       struct super_block *sb;
14253 +       struct au_branch *br;
14254 +       struct path h_path;
14255 +       int err;
14256 +
14257 +       /* a race condition can happen between open and unlink/rmdir */
14258 +       h_file = ERR_PTR(-ENOENT);
14259 +       h_dentry = au_h_dptr(dentry, bindex);
14260 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14261 +               goto out;
14262 +       h_inode = d_inode(h_dentry);
14263 +       spin_lock(&h_dentry->d_lock);
14264 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14265 +               /* || !d_inode(dentry)->i_nlink */
14266 +               ;
14267 +       spin_unlock(&h_dentry->d_lock);
14268 +       if (unlikely(err))
14269 +               goto out;
14270 +
14271 +       sb = dentry->d_sb;
14272 +       br = au_sbr(sb, bindex);
14273 +       err = au_br_test_oflag(flags, br);
14274 +       h_file = ERR_PTR(err);
14275 +       if (unlikely(err))
14276 +               goto out;
14277 +
14278 +       /* drop flags for writing */
14279 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14280 +               if (force_wr && !(flags & O_WRONLY))
14281 +                       force_wr = 0;
14282 +               flags = au_file_roflags(flags);
14283 +               if (force_wr) {
14284 +                       h_file = ERR_PTR(-EROFS);
14285 +                       flags = au_file_roflags(flags);
14286 +                       if (unlikely(vfsub_native_ro(h_inode)
14287 +                                    || IS_APPEND(h_inode)))
14288 +                               goto out;
14289 +                       flags &= ~O_ACCMODE;
14290 +                       flags |= O_WRONLY;
14291 +               }
14292 +       }
14293 +       flags &= ~O_CREAT;
14294 +       au_br_get(br);
14295 +       h_path.dentry = h_dentry;
14296 +       h_path.mnt = au_br_mnt(br);
14297 +       h_file = vfsub_dentry_open(&h_path, flags);
14298 +       if (IS_ERR(h_file))
14299 +               goto out_br;
14300 +
14301 +       if (flags & __FMODE_EXEC) {
14302 +               err = deny_write_access(h_file);
14303 +               if (unlikely(err)) {
14304 +                       fput(h_file);
14305 +                       h_file = ERR_PTR(err);
14306 +                       goto out_br;
14307 +               }
14308 +       }
14309 +       fsnotify_open(h_file);
14310 +       goto out; /* success */
14311 +
14312 +out_br:
14313 +       au_br_put(br);
14314 +out:
14315 +       return h_file;
14316 +}
14317 +
14318 +static int au_cmoo(struct dentry *dentry)
14319 +{
14320 +       int err, cmoo, matched;
14321 +       unsigned int udba;
14322 +       struct path h_path;
14323 +       struct au_pin pin;
14324 +       struct au_cp_generic cpg = {
14325 +               .dentry = dentry,
14326 +               .bdst   = -1,
14327 +               .bsrc   = -1,
14328 +               .len    = -1,
14329 +               .pin    = &pin,
14330 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14331 +       };
14332 +       struct inode *delegated;
14333 +       struct super_block *sb;
14334 +       struct au_sbinfo *sbinfo;
14335 +       struct au_fhsm *fhsm;
14336 +       pid_t pid;
14337 +       struct au_branch *br;
14338 +       struct dentry *parent;
14339 +       struct au_hinode *hdir;
14340 +
14341 +       DiMustWriteLock(dentry);
14342 +       IiMustWriteLock(d_inode(dentry));
14343 +
14344 +       err = 0;
14345 +       if (IS_ROOT(dentry))
14346 +               goto out;
14347 +       cpg.bsrc = au_dbtop(dentry);
14348 +       if (!cpg.bsrc)
14349 +               goto out;
14350 +
14351 +       sb = dentry->d_sb;
14352 +       sbinfo = au_sbi(sb);
14353 +       fhsm = &sbinfo->si_fhsm;
14354 +       pid = au_fhsm_pid(fhsm);
14355 +       rcu_read_lock();
14356 +       matched = (pid
14357 +                  && (current->pid == pid
14358 +                      || rcu_dereference(current->real_parent)->pid == pid));
14359 +       rcu_read_unlock();
14360 +       if (matched)
14361 +               goto out;
14362 +
14363 +       br = au_sbr(sb, cpg.bsrc);
14364 +       cmoo = au_br_cmoo(br->br_perm);
14365 +       if (!cmoo)
14366 +               goto out;
14367 +       if (!d_is_reg(dentry))
14368 +               cmoo &= AuBrAttr_COO_ALL;
14369 +       if (!cmoo)
14370 +               goto out;
14371 +
14372 +       parent = dget_parent(dentry);
14373 +       di_write_lock_parent(parent);
14374 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14375 +       cpg.bdst = err;
14376 +       if (unlikely(err < 0)) {
14377 +               err = 0;        /* there is no upper writable branch */
14378 +               goto out_dgrade;
14379 +       }
14380 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14381 +
14382 +       /* do not respect the coo attrib for the target branch */
14383 +       err = au_cpup_dirs(dentry, cpg.bdst);
14384 +       if (unlikely(err))
14385 +               goto out_dgrade;
14386 +
14387 +       di_downgrade_lock(parent, AuLock_IR);
14388 +       udba = au_opt_udba(sb);
14389 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14390 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14391 +       if (unlikely(err))
14392 +               goto out_parent;
14393 +
14394 +       err = au_sio_cpup_simple(&cpg);
14395 +       au_unpin(&pin);
14396 +       if (unlikely(err))
14397 +               goto out_parent;
14398 +       if (!(cmoo & AuBrWAttr_MOO))
14399 +               goto out_parent; /* success */
14400 +
14401 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14402 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14403 +       if (unlikely(err))
14404 +               goto out_parent;
14405 +
14406 +       h_path.mnt = au_br_mnt(br);
14407 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14408 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14409 +       delegated = NULL;
14410 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14411 +       au_unpin(&pin);
14412 +       /* todo: keep h_dentry or not? */
14413 +       if (unlikely(err == -EWOULDBLOCK)) {
14414 +               pr_warn("cannot retry for NFSv4 delegation"
14415 +                       " for an internal unlink\n");
14416 +               iput(delegated);
14417 +       }
14418 +       if (unlikely(err)) {
14419 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14420 +                      dentry, err);
14421 +               err = 0;
14422 +       }
14423 +       goto out_parent; /* success */
14424 +
14425 +out_dgrade:
14426 +       di_downgrade_lock(parent, AuLock_IR);
14427 +out_parent:
14428 +       di_read_unlock(parent, AuLock_IR);
14429 +       dput(parent);
14430 +out:
14431 +       AuTraceErr(err);
14432 +       return err;
14433 +}
14434 +
14435 +int au_do_open(struct file *file, struct au_do_open_args *args)
14436 +{
14437 +       int err, aopen = args->aopen;
14438 +       struct dentry *dentry;
14439 +       struct au_finfo *finfo;
14440 +
14441 +       if (!aopen)
14442 +               err = au_finfo_init(file, args->fidir);
14443 +       else {
14444 +               lockdep_off();
14445 +               err = au_finfo_init(file, args->fidir);
14446 +               lockdep_on();
14447 +       }
14448 +       if (unlikely(err))
14449 +               goto out;
14450 +
14451 +       dentry = file->f_path.dentry;
14452 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14453 +       di_write_lock_child(dentry);
14454 +       err = au_cmoo(dentry);
14455 +       di_downgrade_lock(dentry, AuLock_IR);
14456 +       if (!err) {
14457 +               if (!aopen)
14458 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14459 +               else {
14460 +                       lockdep_off();
14461 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14462 +                       lockdep_on();
14463 +               }
14464 +       }
14465 +       di_read_unlock(dentry, AuLock_IR);
14466 +
14467 +       finfo = au_fi(file);
14468 +       if (!err) {
14469 +               finfo->fi_file = file;
14470 +               au_hbl_add(&finfo->fi_hlist,
14471 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14472 +       }
14473 +       if (!aopen)
14474 +               fi_write_unlock(file);
14475 +       else {
14476 +               lockdep_off();
14477 +               fi_write_unlock(file);
14478 +               lockdep_on();
14479 +       }
14480 +       if (unlikely(err)) {
14481 +               finfo->fi_hdir = NULL;
14482 +               au_finfo_fin(file);
14483 +       }
14484 +
14485 +out:
14486 +       AuTraceErr(err);
14487 +       return err;
14488 +}
14489 +
14490 +int au_reopen_nondir(struct file *file)
14491 +{
14492 +       int err;
14493 +       aufs_bindex_t btop;
14494 +       struct dentry *dentry;
14495 +       struct file *h_file, *h_file_tmp;
14496 +
14497 +       dentry = file->f_path.dentry;
14498 +       btop = au_dbtop(dentry);
14499 +       h_file_tmp = NULL;
14500 +       if (au_fbtop(file) == btop) {
14501 +               h_file = au_hf_top(file);
14502 +               if (file->f_mode == h_file->f_mode)
14503 +                       return 0; /* success */
14504 +               h_file_tmp = h_file;
14505 +               get_file(h_file_tmp);
14506 +               au_set_h_fptr(file, btop, NULL);
14507 +       }
14508 +       AuDebugOn(au_fi(file)->fi_hdir);
14509 +       /*
14510 +        * it can happen
14511 +        * file exists on both of rw and ro
14512 +        * open --> dbtop and fbtop are both 0
14513 +        * prepend a branch as rw, "rw" become ro
14514 +        * remove rw/file
14515 +        * delete the top branch, "rw" becomes rw again
14516 +        *      --> dbtop is 1, fbtop is still 0
14517 +        * write --> fbtop is 0 but dbtop is 1
14518 +        */
14519 +       /* AuDebugOn(au_fbtop(file) < btop); */
14520 +
14521 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14522 +                          file, /*force_wr*/0);
14523 +       err = PTR_ERR(h_file);
14524 +       if (IS_ERR(h_file)) {
14525 +               if (h_file_tmp) {
14526 +                       au_sbr_get(dentry->d_sb, btop);
14527 +                       au_set_h_fptr(file, btop, h_file_tmp);
14528 +                       h_file_tmp = NULL;
14529 +               }
14530 +               goto out; /* todo: close all? */
14531 +       }
14532 +
14533 +       err = 0;
14534 +       au_set_fbtop(file, btop);
14535 +       au_set_h_fptr(file, btop, h_file);
14536 +       au_update_figen(file);
14537 +       /* todo: necessary? */
14538 +       /* file->f_ra = h_file->f_ra; */
14539 +
14540 +out:
14541 +       if (h_file_tmp)
14542 +               fput(h_file_tmp);
14543 +       return err;
14544 +}
14545 +
14546 +/* ---------------------------------------------------------------------- */
14547 +
14548 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14549 +                       struct dentry *hi_wh)
14550 +{
14551 +       int err;
14552 +       aufs_bindex_t btop;
14553 +       struct au_dinfo *dinfo;
14554 +       struct dentry *h_dentry;
14555 +       struct au_hdentry *hdp;
14556 +
14557 +       dinfo = au_di(file->f_path.dentry);
14558 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14559 +
14560 +       btop = dinfo->di_btop;
14561 +       dinfo->di_btop = btgt;
14562 +       hdp = au_hdentry(dinfo, btgt);
14563 +       h_dentry = hdp->hd_dentry;
14564 +       hdp->hd_dentry = hi_wh;
14565 +       err = au_reopen_nondir(file);
14566 +       hdp->hd_dentry = h_dentry;
14567 +       dinfo->di_btop = btop;
14568 +
14569 +       return err;
14570 +}
14571 +
14572 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14573 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14574 +{
14575 +       int err;
14576 +       struct inode *inode, *h_inode;
14577 +       struct dentry *h_dentry, *hi_wh;
14578 +       struct au_cp_generic cpg = {
14579 +               .dentry = file->f_path.dentry,
14580 +               .bdst   = bcpup,
14581 +               .bsrc   = -1,
14582 +               .len    = len,
14583 +               .pin    = pin
14584 +       };
14585 +
14586 +       au_update_dbtop(cpg.dentry);
14587 +       inode = d_inode(cpg.dentry);
14588 +       h_inode = NULL;
14589 +       if (au_dbtop(cpg.dentry) <= bcpup
14590 +           && au_dbbot(cpg.dentry) >= bcpup) {
14591 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14592 +               if (h_dentry && d_is_positive(h_dentry))
14593 +                       h_inode = d_inode(h_dentry);
14594 +       }
14595 +       hi_wh = au_hi_wh(inode, bcpup);
14596 +       if (!hi_wh && !h_inode)
14597 +               err = au_sio_cpup_wh(&cpg, file);
14598 +       else
14599 +               /* already copied-up after unlink */
14600 +               err = au_reopen_wh(file, bcpup, hi_wh);
14601 +
14602 +       if (!err
14603 +           && (inode->i_nlink > 1
14604 +               || (inode->i_state & I_LINKABLE))
14605 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14606 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14607 +
14608 +       return err;
14609 +}
14610 +
14611 +/*
14612 + * prepare the @file for writing.
14613 + */
14614 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14615 +{
14616 +       int err;
14617 +       aufs_bindex_t dbtop;
14618 +       struct dentry *parent;
14619 +       struct inode *inode;
14620 +       struct super_block *sb;
14621 +       struct file *h_file;
14622 +       struct au_cp_generic cpg = {
14623 +               .dentry = file->f_path.dentry,
14624 +               .bdst   = -1,
14625 +               .bsrc   = -1,
14626 +               .len    = len,
14627 +               .pin    = pin,
14628 +               .flags  = AuCpup_DTIME
14629 +       };
14630 +
14631 +       sb = cpg.dentry->d_sb;
14632 +       inode = d_inode(cpg.dentry);
14633 +       cpg.bsrc = au_fbtop(file);
14634 +       err = au_test_ro(sb, cpg.bsrc, inode);
14635 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14636 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14637 +                            /*flags*/0);
14638 +               goto out;
14639 +       }
14640 +
14641 +       /* need to cpup or reopen */
14642 +       parent = dget_parent(cpg.dentry);
14643 +       di_write_lock_parent(parent);
14644 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14645 +       cpg.bdst = err;
14646 +       if (unlikely(err < 0))
14647 +               goto out_dgrade;
14648 +       err = 0;
14649 +
14650 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14651 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14652 +               if (unlikely(err))
14653 +                       goto out_dgrade;
14654 +       }
14655 +
14656 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14657 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14658 +       if (unlikely(err))
14659 +               goto out_dgrade;
14660 +
14661 +       dbtop = au_dbtop(cpg.dentry);
14662 +       if (dbtop <= cpg.bdst)
14663 +               cpg.bsrc = cpg.bdst;
14664 +
14665 +       if (dbtop <= cpg.bdst           /* just reopen */
14666 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14667 +               ) {
14668 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14669 +               if (IS_ERR(h_file))
14670 +                       err = PTR_ERR(h_file);
14671 +               else {
14672 +                       di_downgrade_lock(parent, AuLock_IR);
14673 +                       if (dbtop > cpg.bdst)
14674 +                               err = au_sio_cpup_simple(&cpg);
14675 +                       if (!err)
14676 +                               err = au_reopen_nondir(file);
14677 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14678 +               }
14679 +       } else {                        /* copyup as wh and reopen */
14680 +               /*
14681 +                * since writable hfsplus branch is not supported,
14682 +                * h_open_pre/post() are unnecessary.
14683 +                */
14684 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14685 +               di_downgrade_lock(parent, AuLock_IR);
14686 +       }
14687 +
14688 +       if (!err) {
14689 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14690 +               goto out_dput; /* success */
14691 +       }
14692 +       au_unpin(pin);
14693 +       goto out_unlock;
14694 +
14695 +out_dgrade:
14696 +       di_downgrade_lock(parent, AuLock_IR);
14697 +out_unlock:
14698 +       di_read_unlock(parent, AuLock_IR);
14699 +out_dput:
14700 +       dput(parent);
14701 +out:
14702 +       return err;
14703 +}
14704 +
14705 +/* ---------------------------------------------------------------------- */
14706 +
14707 +int au_do_flush(struct file *file, fl_owner_t id,
14708 +               int (*flush)(struct file *file, fl_owner_t id))
14709 +{
14710 +       int err;
14711 +       struct super_block *sb;
14712 +       struct inode *inode;
14713 +
14714 +       inode = file_inode(file);
14715 +       sb = inode->i_sb;
14716 +       si_noflush_read_lock(sb);
14717 +       fi_read_lock(file);
14718 +       ii_read_lock_child(inode);
14719 +
14720 +       err = flush(file, id);
14721 +       au_cpup_attr_timesizes(inode);
14722 +
14723 +       ii_read_unlock(inode);
14724 +       fi_read_unlock(file);
14725 +       si_read_unlock(sb);
14726 +       return err;
14727 +}
14728 +
14729 +/* ---------------------------------------------------------------------- */
14730 +
14731 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14732 +{
14733 +       int err;
14734 +       struct au_pin pin;
14735 +       struct au_finfo *finfo;
14736 +       struct dentry *parent, *hi_wh;
14737 +       struct inode *inode;
14738 +       struct super_block *sb;
14739 +       struct au_cp_generic cpg = {
14740 +               .dentry = file->f_path.dentry,
14741 +               .bdst   = -1,
14742 +               .bsrc   = -1,
14743 +               .len    = -1,
14744 +               .pin    = &pin,
14745 +               .flags  = AuCpup_DTIME
14746 +       };
14747 +
14748 +       FiMustWriteLock(file);
14749 +
14750 +       err = 0;
14751 +       finfo = au_fi(file);
14752 +       sb = cpg.dentry->d_sb;
14753 +       inode = d_inode(cpg.dentry);
14754 +       cpg.bdst = au_ibtop(inode);
14755 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14756 +               goto out;
14757 +
14758 +       parent = dget_parent(cpg.dentry);
14759 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14760 +               di_read_lock_parent(parent, !AuLock_IR);
14761 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14762 +               cpg.bdst = err;
14763 +               di_read_unlock(parent, !AuLock_IR);
14764 +               if (unlikely(err < 0))
14765 +                       goto out_parent;
14766 +               err = 0;
14767 +       }
14768 +
14769 +       di_read_lock_parent(parent, AuLock_IR);
14770 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14771 +       if (!S_ISDIR(inode->i_mode)
14772 +           && au_opt_test(au_mntflags(sb), PLINK)
14773 +           && au_plink_test(inode)
14774 +           && !d_unhashed(cpg.dentry)
14775 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14776 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14777 +               if (unlikely(err))
14778 +                       goto out_unlock;
14779 +
14780 +               /* always superio. */
14781 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14782 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14783 +               if (!err) {
14784 +                       err = au_sio_cpup_simple(&cpg);
14785 +                       au_unpin(&pin);
14786 +               }
14787 +       } else if (hi_wh) {
14788 +               /* already copied-up after unlink */
14789 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14790 +               *need_reopen = 0;
14791 +       }
14792 +
14793 +out_unlock:
14794 +       di_read_unlock(parent, AuLock_IR);
14795 +out_parent:
14796 +       dput(parent);
14797 +out:
14798 +       return err;
14799 +}
14800 +
14801 +static void au_do_refresh_dir(struct file *file)
14802 +{
14803 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14804 +       struct au_hfile *p, tmp, *q;
14805 +       struct au_finfo *finfo;
14806 +       struct super_block *sb;
14807 +       struct au_fidir *fidir;
14808 +
14809 +       FiMustWriteLock(file);
14810 +
14811 +       sb = file->f_path.dentry->d_sb;
14812 +       finfo = au_fi(file);
14813 +       fidir = finfo->fi_hdir;
14814 +       AuDebugOn(!fidir);
14815 +       p = fidir->fd_hfile + finfo->fi_btop;
14816 +       brid = p->hf_br->br_id;
14817 +       bbot = fidir->fd_bbot;
14818 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14819 +               if (!p->hf_file)
14820 +                       continue;
14821 +
14822 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14823 +               if (new_bindex == bindex)
14824 +                       continue;
14825 +               if (new_bindex < 0) {
14826 +                       au_set_h_fptr(file, bindex, NULL);
14827 +                       continue;
14828 +               }
14829 +
14830 +               /* swap two lower inode, and loop again */
14831 +               q = fidir->fd_hfile + new_bindex;
14832 +               tmp = *q;
14833 +               *q = *p;
14834 +               *p = tmp;
14835 +               if (tmp.hf_file) {
14836 +                       bindex--;
14837 +                       p--;
14838 +               }
14839 +       }
14840 +
14841 +       p = fidir->fd_hfile;
14842 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14843 +               bbot = au_sbbot(sb);
14844 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14845 +                    finfo->fi_btop++, p++)
14846 +                       if (p->hf_file) {
14847 +                               if (file_inode(p->hf_file))
14848 +                                       break;
14849 +                               au_hfput(p, /*execed*/0);
14850 +                       }
14851 +       } else {
14852 +               bbot = au_br_index(sb, brid);
14853 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14854 +                    finfo->fi_btop++, p++)
14855 +                       if (p->hf_file)
14856 +                               au_hfput(p, /*execed*/0);
14857 +               bbot = au_sbbot(sb);
14858 +       }
14859 +
14860 +       p = fidir->fd_hfile + bbot;
14861 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14862 +            fidir->fd_bbot--, p--)
14863 +               if (p->hf_file) {
14864 +                       if (file_inode(p->hf_file))
14865 +                               break;
14866 +                       au_hfput(p, /*execed*/0);
14867 +               }
14868 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14869 +}
14870 +
14871 +/*
14872 + * after branch manipulating, refresh the file.
14873 + */
14874 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14875 +{
14876 +       int err, need_reopen, nbr;
14877 +       aufs_bindex_t bbot, bindex;
14878 +       struct dentry *dentry;
14879 +       struct super_block *sb;
14880 +       struct au_finfo *finfo;
14881 +       struct au_hfile *hfile;
14882 +
14883 +       dentry = file->f_path.dentry;
14884 +       sb = dentry->d_sb;
14885 +       nbr = au_sbbot(sb) + 1;
14886 +       finfo = au_fi(file);
14887 +       if (!finfo->fi_hdir) {
14888 +               hfile = &finfo->fi_htop;
14889 +               AuDebugOn(!hfile->hf_file);
14890 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14891 +               AuDebugOn(bindex < 0);
14892 +               if (bindex != finfo->fi_btop)
14893 +                       au_set_fbtop(file, bindex);
14894 +       } else {
14895 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14896 +               if (unlikely(err))
14897 +                       goto out;
14898 +               au_do_refresh_dir(file);
14899 +       }
14900 +
14901 +       err = 0;
14902 +       need_reopen = 1;
14903 +       if (!au_test_mmapped(file))
14904 +               err = au_file_refresh_by_inode(file, &need_reopen);
14905 +       if (finfo->fi_hdir)
14906 +               /* harmless if err */
14907 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14908 +       if (!err && need_reopen && !d_unlinked(dentry))
14909 +               err = reopen(file);
14910 +       if (!err) {
14911 +               au_update_figen(file);
14912 +               goto out; /* success */
14913 +       }
14914 +
14915 +       /* error, close all lower files */
14916 +       if (finfo->fi_hdir) {
14917 +               bbot = au_fbbot_dir(file);
14918 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14919 +                       au_set_h_fptr(file, bindex, NULL);
14920 +       }
14921 +
14922 +out:
14923 +       return err;
14924 +}
14925 +
14926 +/* common function to regular file and dir */
14927 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14928 +                         int wlock, unsigned int fi_lsc)
14929 +{
14930 +       int err;
14931 +       unsigned int sigen, figen;
14932 +       aufs_bindex_t btop;
14933 +       unsigned char pseudo_link;
14934 +       struct dentry *dentry;
14935 +       struct inode *inode;
14936 +
14937 +       err = 0;
14938 +       dentry = file->f_path.dentry;
14939 +       inode = d_inode(dentry);
14940 +       sigen = au_sigen(dentry->d_sb);
14941 +       fi_write_lock_nested(file, fi_lsc);
14942 +       figen = au_figen(file);
14943 +       if (!fi_lsc)
14944 +               di_write_lock_child(dentry);
14945 +       else
14946 +               di_write_lock_child2(dentry);
14947 +       btop = au_dbtop(dentry);
14948 +       pseudo_link = (btop != au_ibtop(inode));
14949 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14950 +               if (!wlock) {
14951 +                       di_downgrade_lock(dentry, AuLock_IR);
14952 +                       fi_downgrade_lock(file);
14953 +               }
14954 +               goto out; /* success */
14955 +       }
14956 +
14957 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14958 +       if (au_digen_test(dentry, sigen)) {
14959 +               err = au_reval_dpath(dentry, sigen);
14960 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14961 +       }
14962 +
14963 +       if (!err)
14964 +               err = refresh_file(file, reopen);
14965 +       if (!err) {
14966 +               if (!wlock) {
14967 +                       di_downgrade_lock(dentry, AuLock_IR);
14968 +                       fi_downgrade_lock(file);
14969 +               }
14970 +       } else {
14971 +               di_write_unlock(dentry);
14972 +               fi_write_unlock(file);
14973 +       }
14974 +
14975 +out:
14976 +       return err;
14977 +}
14978 +
14979 +/* ---------------------------------------------------------------------- */
14980 +
14981 +/* cf. aufs_nopage() */
14982 +/* for madvise(2) */
14983 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14984 +{
14985 +       unlock_page(page);
14986 +       return 0;
14987 +}
14988 +
14989 +/* it will never be called, but necessary to support O_DIRECT */
14990 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14991 +{ BUG(); return 0; }
14992 +
14993 +/* they will never be called. */
14994 +#ifdef CONFIG_AUFS_DEBUG
14995 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14996 +                           loff_t pos, unsigned len, unsigned flags,
14997 +                           struct page **pagep, void **fsdata)
14998 +{ AuUnsupport(); return 0; }
14999 +static int aufs_write_end(struct file *file, struct address_space *mapping,
15000 +                         loff_t pos, unsigned len, unsigned copied,
15001 +                         struct page *page, void *fsdata)
15002 +{ AuUnsupport(); return 0; }
15003 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15004 +{ AuUnsupport(); return 0; }
15005 +
15006 +static int aufs_set_page_dirty(struct page *page)
15007 +{ AuUnsupport(); return 0; }
15008 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
15009 +                               unsigned int length)
15010 +{ AuUnsupport(); }
15011 +static int aufs_releasepage(struct page *page, gfp_t gfp)
15012 +{ AuUnsupport(); return 0; }
15013 +#if 0 /* called by memory compaction regardless file */
15014 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
15015 +                           struct page *page, enum migrate_mode mode)
15016 +{ AuUnsupport(); return 0; }
15017 +#endif
15018 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15019 +{ AuUnsupport(); return true; }
15020 +static void aufs_putback_page(struct page *page)
15021 +{ AuUnsupport(); }
15022 +static int aufs_launder_page(struct page *page)
15023 +{ AuUnsupport(); return 0; }
15024 +static int aufs_is_partially_uptodate(struct page *page,
15025 +                                     unsigned long from,
15026 +                                     unsigned long count)
15027 +{ AuUnsupport(); return 0; }
15028 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15029 +                                   bool *writeback)
15030 +{ AuUnsupport(); }
15031 +static int aufs_error_remove_page(struct address_space *mapping,
15032 +                                 struct page *page)
15033 +{ AuUnsupport(); return 0; }
15034 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15035 +                             sector_t *span)
15036 +{ AuUnsupport(); return 0; }
15037 +static void aufs_swap_deactivate(struct file *file)
15038 +{ AuUnsupport(); }
15039 +#endif /* CONFIG_AUFS_DEBUG */
15040 +
15041 +const struct address_space_operations aufs_aop = {
15042 +       .readpage               = aufs_readpage,
15043 +       .direct_IO              = aufs_direct_IO,
15044 +#ifdef CONFIG_AUFS_DEBUG
15045 +       .writepage              = aufs_writepage,
15046 +       /* no writepages, because of writepage */
15047 +       .set_page_dirty         = aufs_set_page_dirty,
15048 +       /* no readpages, because of readpage */
15049 +       .write_begin            = aufs_write_begin,
15050 +       .write_end              = aufs_write_end,
15051 +       /* no bmap, no block device */
15052 +       .invalidatepage         = aufs_invalidatepage,
15053 +       .releasepage            = aufs_releasepage,
15054 +       /* is fallback_migrate_page ok? */
15055 +       /* .migratepage         = aufs_migratepage, */
15056 +       .isolate_page           = aufs_isolate_page,
15057 +       .putback_page           = aufs_putback_page,
15058 +       .launder_page           = aufs_launder_page,
15059 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
15060 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
15061 +       .error_remove_page      = aufs_error_remove_page,
15062 +       .swap_activate          = aufs_swap_activate,
15063 +       .swap_deactivate        = aufs_swap_deactivate
15064 +#endif /* CONFIG_AUFS_DEBUG */
15065 +};
15066 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15067 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15068 +++ linux/fs/aufs/file.h        2018-07-19 09:46:13.059981241 +0200
15069 @@ -0,0 +1,341 @@
15070 +/* SPDX-License-Identifier: GPL-2.0 */
15071 +/*
15072 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15073 + *
15074 + * This program, aufs is free software; you can redistribute it and/or modify
15075 + * it under the terms of the GNU General Public License as published by
15076 + * the Free Software Foundation; either version 2 of the License, or
15077 + * (at your option) any later version.
15078 + *
15079 + * This program is distributed in the hope that it will be useful,
15080 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15081 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15082 + * GNU General Public License for more details.
15083 + *
15084 + * You should have received a copy of the GNU General Public License
15085 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15086 + */
15087 +
15088 +/*
15089 + * file operations
15090 + */
15091 +
15092 +#ifndef __AUFS_FILE_H__
15093 +#define __AUFS_FILE_H__
15094 +
15095 +#ifdef __KERNEL__
15096 +
15097 +#include <linux/file.h>
15098 +#include <linux/fs.h>
15099 +#include <linux/mm_types.h>
15100 +#include <linux/poll.h>
15101 +#include "rwsem.h"
15102 +
15103 +struct au_branch;
15104 +struct au_hfile {
15105 +       struct file             *hf_file;
15106 +       struct au_branch        *hf_br;
15107 +};
15108 +
15109 +struct au_vdir;
15110 +struct au_fidir {
15111 +       aufs_bindex_t           fd_bbot;
15112 +       aufs_bindex_t           fd_nent;
15113 +       struct au_vdir          *fd_vdir_cache;
15114 +       struct au_hfile         fd_hfile[];
15115 +};
15116 +
15117 +static inline int au_fidir_sz(int nent)
15118 +{
15119 +       AuDebugOn(nent < 0);
15120 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15121 +}
15122 +
15123 +struct au_finfo {
15124 +       atomic_t                fi_generation;
15125 +
15126 +       struct au_rwsem         fi_rwsem;
15127 +       aufs_bindex_t           fi_btop;
15128 +
15129 +       /* do not union them */
15130 +       struct {                                /* for non-dir */
15131 +               struct au_hfile                 fi_htop;
15132 +               atomic_t                        fi_mmapped;
15133 +       };
15134 +       struct au_fidir         *fi_hdir;       /* for dir only */
15135 +
15136 +       struct hlist_bl_node    fi_hlist;
15137 +       struct file             *fi_file;       /* very ugly */
15138 +} ____cacheline_aligned_in_smp;
15139 +
15140 +/* ---------------------------------------------------------------------- */
15141 +
15142 +/* file.c */
15143 +extern const struct address_space_operations aufs_aop;
15144 +unsigned int au_file_roflags(unsigned int flags);
15145 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15146 +                      struct file *file, int force_wr);
15147 +struct au_do_open_args {
15148 +       int             aopen;
15149 +       int             (*open)(struct file *file, int flags,
15150 +                               struct file *h_file);
15151 +       struct au_fidir *fidir;
15152 +       struct file     *h_file;
15153 +};
15154 +int au_do_open(struct file *file, struct au_do_open_args *args);
15155 +int au_reopen_nondir(struct file *file);
15156 +struct au_pin;
15157 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15158 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15159 +                         int wlock, unsigned int fi_lsc);
15160 +int au_do_flush(struct file *file, fl_owner_t id,
15161 +               int (*flush)(struct file *file, fl_owner_t id));
15162 +
15163 +/* poll.c */
15164 +#ifdef CONFIG_AUFS_POLL
15165 +__poll_t aufs_poll(struct file *file, poll_table *wait);
15166 +#endif
15167 +
15168 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15169 +/* hfsplus.c */
15170 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15171 +                          int force_wr);
15172 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15173 +                   struct file *h_file);
15174 +#else
15175 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15176 +       aufs_bindex_t bindex, int force_wr)
15177 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15178 +          struct file *h_file);
15179 +#endif
15180 +
15181 +/* f_op.c */
15182 +extern const struct file_operations aufs_file_fop;
15183 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15184 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15185 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15186 +
15187 +/* finfo.c */
15188 +void au_hfput(struct au_hfile *hf, int execed);
15189 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15190 +                  struct file *h_file);
15191 +
15192 +void au_update_figen(struct file *file);
15193 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15194 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15195 +
15196 +void au_fi_init_once(void *_fi);
15197 +void au_finfo_fin(struct file *file);
15198 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15199 +
15200 +/* ioctl.c */
15201 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15202 +#ifdef CONFIG_COMPAT
15203 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15204 +                          unsigned long arg);
15205 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15206 +                             unsigned long arg);
15207 +#endif
15208 +
15209 +/* ---------------------------------------------------------------------- */
15210 +
15211 +static inline struct au_finfo *au_fi(struct file *file)
15212 +{
15213 +       return file->private_data;
15214 +}
15215 +
15216 +/* ---------------------------------------------------------------------- */
15217 +
15218 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15219 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15220 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15221 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15222 +/*
15223 +#define fi_read_trylock_nested(f) \
15224 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15225 +#define fi_write_trylock_nested(f) \
15226 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15227 +*/
15228 +
15229 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15230 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15231 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15232 +
15233 +/* lock subclass for finfo */
15234 +enum {
15235 +       AuLsc_FI_1,
15236 +       AuLsc_FI_2
15237 +};
15238 +
15239 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15240 +{
15241 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15242 +}
15243 +
15244 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15245 +{
15246 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15247 +}
15248 +
15249 +/*
15250 + * fi_read_lock_1, fi_write_lock_1,
15251 + * fi_read_lock_2, fi_write_lock_2
15252 + */
15253 +#define AuReadLockFunc(name) \
15254 +static inline void fi_read_lock_##name(struct file *f) \
15255 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15256 +
15257 +#define AuWriteLockFunc(name) \
15258 +static inline void fi_write_lock_##name(struct file *f) \
15259 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15260 +
15261 +#define AuRWLockFuncs(name) \
15262 +       AuReadLockFunc(name) \
15263 +       AuWriteLockFunc(name)
15264 +
15265 +AuRWLockFuncs(1);
15266 +AuRWLockFuncs(2);
15267 +
15268 +#undef AuReadLockFunc
15269 +#undef AuWriteLockFunc
15270 +#undef AuRWLockFuncs
15271 +
15272 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15273 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15274 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15275 +
15276 +/* ---------------------------------------------------------------------- */
15277 +
15278 +/* todo: hard/soft set? */
15279 +static inline aufs_bindex_t au_fbtop(struct file *file)
15280 +{
15281 +       FiMustAnyLock(file);
15282 +       return au_fi(file)->fi_btop;
15283 +}
15284 +
15285 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15286 +{
15287 +       FiMustAnyLock(file);
15288 +       AuDebugOn(!au_fi(file)->fi_hdir);
15289 +       return au_fi(file)->fi_hdir->fd_bbot;
15290 +}
15291 +
15292 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15293 +{
15294 +       FiMustAnyLock(file);
15295 +       AuDebugOn(!au_fi(file)->fi_hdir);
15296 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15297 +}
15298 +
15299 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15300 +{
15301 +       FiMustWriteLock(file);
15302 +       au_fi(file)->fi_btop = bindex;
15303 +}
15304 +
15305 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15306 +{
15307 +       FiMustWriteLock(file);
15308 +       AuDebugOn(!au_fi(file)->fi_hdir);
15309 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15310 +}
15311 +
15312 +static inline void au_set_fvdir_cache(struct file *file,
15313 +                                     struct au_vdir *vdir_cache)
15314 +{
15315 +       FiMustWriteLock(file);
15316 +       AuDebugOn(!au_fi(file)->fi_hdir);
15317 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15318 +}
15319 +
15320 +static inline struct file *au_hf_top(struct file *file)
15321 +{
15322 +       FiMustAnyLock(file);
15323 +       AuDebugOn(au_fi(file)->fi_hdir);
15324 +       return au_fi(file)->fi_htop.hf_file;
15325 +}
15326 +
15327 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15328 +{
15329 +       FiMustAnyLock(file);
15330 +       AuDebugOn(!au_fi(file)->fi_hdir);
15331 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15332 +}
15333 +
15334 +/* todo: memory barrier? */
15335 +static inline unsigned int au_figen(struct file *f)
15336 +{
15337 +       return atomic_read(&au_fi(f)->fi_generation);
15338 +}
15339 +
15340 +static inline void au_set_mmapped(struct file *f)
15341 +{
15342 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15343 +               return;
15344 +       pr_warn("fi_mmapped wrapped around\n");
15345 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15346 +               ;
15347 +}
15348 +
15349 +static inline void au_unset_mmapped(struct file *f)
15350 +{
15351 +       atomic_dec(&au_fi(f)->fi_mmapped);
15352 +}
15353 +
15354 +static inline int au_test_mmapped(struct file *f)
15355 +{
15356 +       return atomic_read(&au_fi(f)->fi_mmapped);
15357 +}
15358 +
15359 +/* customize vma->vm_file */
15360 +
15361 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15362 +                                      struct file *file)
15363 +{
15364 +       struct file *f;
15365 +
15366 +       f = vma->vm_file;
15367 +       get_file(file);
15368 +       vma->vm_file = file;
15369 +       fput(f);
15370 +}
15371 +
15372 +#ifdef CONFIG_MMU
15373 +#define AuDbgVmRegion(file, vma) do {} while (0)
15374 +
15375 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15376 +                                   struct file *file)
15377 +{
15378 +       au_do_vm_file_reset(vma, file);
15379 +}
15380 +#else
15381 +#define AuDbgVmRegion(file, vma) \
15382 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15383 +
15384 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15385 +                                   struct file *file)
15386 +{
15387 +       struct file *f;
15388 +
15389 +       au_do_vm_file_reset(vma, file);
15390 +       f = vma->vm_region->vm_file;
15391 +       get_file(file);
15392 +       vma->vm_region->vm_file = file;
15393 +       fput(f);
15394 +}
15395 +#endif /* CONFIG_MMU */
15396 +
15397 +/* handle vma->vm_prfile */
15398 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15399 +                                   struct file *file)
15400 +{
15401 +       get_file(file);
15402 +       vma->vm_prfile = file;
15403 +#ifndef CONFIG_MMU
15404 +       get_file(file);
15405 +       vma->vm_region->vm_prfile = file;
15406 +#endif
15407 +}
15408 +
15409 +#endif /* __KERNEL__ */
15410 +#endif /* __AUFS_FILE_H__ */
15411 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15412 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15413 +++ linux/fs/aufs/finfo.c       2018-07-19 09:46:13.059981241 +0200
15414 @@ -0,0 +1,149 @@
15415 +/* SPDX-License-Identifier: GPL-2.0 */
15416 +/*
15417 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15418 + *
15419 + * This program, aufs is free software; you can redistribute it and/or modify
15420 + * it under the terms of the GNU General Public License as published by
15421 + * the Free Software Foundation; either version 2 of the License, or
15422 + * (at your option) any later version.
15423 + *
15424 + * This program is distributed in the hope that it will be useful,
15425 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15426 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15427 + * GNU General Public License for more details.
15428 + *
15429 + * You should have received a copy of the GNU General Public License
15430 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15431 + */
15432 +
15433 +/*
15434 + * file private data
15435 + */
15436 +
15437 +#include "aufs.h"
15438 +
15439 +void au_hfput(struct au_hfile *hf, int execed)
15440 +{
15441 +       if (execed)
15442 +               allow_write_access(hf->hf_file);
15443 +       fput(hf->hf_file);
15444 +       hf->hf_file = NULL;
15445 +       au_br_put(hf->hf_br);
15446 +       hf->hf_br = NULL;
15447 +}
15448 +
15449 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15450 +{
15451 +       struct au_finfo *finfo = au_fi(file);
15452 +       struct au_hfile *hf;
15453 +       struct au_fidir *fidir;
15454 +
15455 +       fidir = finfo->fi_hdir;
15456 +       if (!fidir) {
15457 +               AuDebugOn(finfo->fi_btop != bindex);
15458 +               hf = &finfo->fi_htop;
15459 +       } else
15460 +               hf = fidir->fd_hfile + bindex;
15461 +
15462 +       if (hf && hf->hf_file)
15463 +               au_hfput(hf, vfsub_file_execed(file));
15464 +       if (val) {
15465 +               FiMustWriteLock(file);
15466 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15467 +               hf->hf_file = val;
15468 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15469 +       }
15470 +}
15471 +
15472 +void au_update_figen(struct file *file)
15473 +{
15474 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15475 +       /* smp_mb(); */ /* atomic_set */
15476 +}
15477 +
15478 +/* ---------------------------------------------------------------------- */
15479 +
15480 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15481 +{
15482 +       struct au_fidir *fidir;
15483 +       int nbr;
15484 +
15485 +       nbr = au_sbbot(sb) + 1;
15486 +       if (nbr < 2)
15487 +               nbr = 2; /* initial allocate for 2 branches */
15488 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15489 +       if (fidir) {
15490 +               fidir->fd_bbot = -1;
15491 +               fidir->fd_nent = nbr;
15492 +       }
15493 +
15494 +       return fidir;
15495 +}
15496 +
15497 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15498 +{
15499 +       int err;
15500 +       struct au_fidir *fidir, *p;
15501 +
15502 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15503 +       fidir = finfo->fi_hdir;
15504 +       AuDebugOn(!fidir);
15505 +
15506 +       err = -ENOMEM;
15507 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15508 +                        GFP_NOFS, may_shrink);
15509 +       if (p) {
15510 +               p->fd_nent = nbr;
15511 +               finfo->fi_hdir = p;
15512 +               err = 0;
15513 +       }
15514 +
15515 +       return err;
15516 +}
15517 +
15518 +/* ---------------------------------------------------------------------- */
15519 +
15520 +void au_finfo_fin(struct file *file)
15521 +{
15522 +       struct au_finfo *finfo;
15523 +
15524 +       au_nfiles_dec(file->f_path.dentry->d_sb);
15525 +
15526 +       finfo = au_fi(file);
15527 +       AuDebugOn(finfo->fi_hdir);
15528 +       AuRwDestroy(&finfo->fi_rwsem);
15529 +       au_cache_free_finfo(finfo);
15530 +}
15531 +
15532 +void au_fi_init_once(void *_finfo)
15533 +{
15534 +       struct au_finfo *finfo = _finfo;
15535 +
15536 +       au_rw_init(&finfo->fi_rwsem);
15537 +}
15538 +
15539 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15540 +{
15541 +       int err;
15542 +       struct au_finfo *finfo;
15543 +       struct dentry *dentry;
15544 +
15545 +       err = -ENOMEM;
15546 +       dentry = file->f_path.dentry;
15547 +       finfo = au_cache_alloc_finfo();
15548 +       if (unlikely(!finfo))
15549 +               goto out;
15550 +
15551 +       err = 0;
15552 +       au_nfiles_inc(dentry->d_sb);
15553 +       au_rw_write_lock(&finfo->fi_rwsem);
15554 +       finfo->fi_btop = -1;
15555 +       finfo->fi_hdir = fidir;
15556 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15557 +       /* smp_mb(); */ /* atomic_set */
15558 +
15559 +       file->private_data = finfo;
15560 +
15561 +out:
15562 +       return err;
15563 +}
15564 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15565 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15566 +++ linux/fs/aufs/f_op.c        2018-07-19 09:46:13.056647808 +0200
15567 @@ -0,0 +1,818 @@
15568 +/* SPDX-License-Identifier: GPL-2.0 */
15569 +/*
15570 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15571 + *
15572 + * This program, aufs is free software; you can redistribute it and/or modify
15573 + * it under the terms of the GNU General Public License as published by
15574 + * the Free Software Foundation; either version 2 of the License, or
15575 + * (at your option) any later version.
15576 + *
15577 + * This program is distributed in the hope that it will be useful,
15578 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15579 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15580 + * GNU General Public License for more details.
15581 + *
15582 + * You should have received a copy of the GNU General Public License
15583 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15584 + */
15585 +
15586 +/*
15587 + * file and vm operations
15588 + */
15589 +
15590 +#include <linux/aio.h>
15591 +#include <linux/fs_stack.h>
15592 +#include <linux/mman.h>
15593 +#include <linux/security.h>
15594 +#include "aufs.h"
15595 +
15596 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15597 +{
15598 +       int err;
15599 +       aufs_bindex_t bindex;
15600 +       struct dentry *dentry, *h_dentry;
15601 +       struct au_finfo *finfo;
15602 +       struct inode *h_inode;
15603 +
15604 +       FiMustWriteLock(file);
15605 +
15606 +       err = 0;
15607 +       dentry = file->f_path.dentry;
15608 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15609 +       finfo = au_fi(file);
15610 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15611 +       atomic_set(&finfo->fi_mmapped, 0);
15612 +       bindex = au_dbtop(dentry);
15613 +       if (!h_file) {
15614 +               h_dentry = au_h_dptr(dentry, bindex);
15615 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15616 +               if (unlikely(err))
15617 +                       goto out;
15618 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15619 +       } else {
15620 +               h_dentry = h_file->f_path.dentry;
15621 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15622 +               if (unlikely(err))
15623 +                       goto out;
15624 +               get_file(h_file);
15625 +       }
15626 +       if (IS_ERR(h_file))
15627 +               err = PTR_ERR(h_file);
15628 +       else {
15629 +               if ((flags & __O_TMPFILE)
15630 +                   && !(flags & O_EXCL)) {
15631 +                       h_inode = file_inode(h_file);
15632 +                       spin_lock(&h_inode->i_lock);
15633 +                       h_inode->i_state |= I_LINKABLE;
15634 +                       spin_unlock(&h_inode->i_lock);
15635 +               }
15636 +               au_set_fbtop(file, bindex);
15637 +               au_set_h_fptr(file, bindex, h_file);
15638 +               au_update_figen(file);
15639 +               /* todo: necessary? */
15640 +               /* file->f_ra = h_file->f_ra; */
15641 +       }
15642 +
15643 +out:
15644 +       return err;
15645 +}
15646 +
15647 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15648 +                           struct file *file)
15649 +{
15650 +       int err;
15651 +       struct super_block *sb;
15652 +       struct au_do_open_args args = {
15653 +               .open   = au_do_open_nondir
15654 +       };
15655 +
15656 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15657 +             file, vfsub_file_flags(file), file->f_mode);
15658 +
15659 +       sb = file->f_path.dentry->d_sb;
15660 +       si_read_lock(sb, AuLock_FLUSH);
15661 +       err = au_do_open(file, &args);
15662 +       si_read_unlock(sb);
15663 +       return err;
15664 +}
15665 +
15666 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15667 +{
15668 +       struct au_finfo *finfo;
15669 +       aufs_bindex_t bindex;
15670 +
15671 +       finfo = au_fi(file);
15672 +       au_hbl_del(&finfo->fi_hlist,
15673 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15674 +       bindex = finfo->fi_btop;
15675 +       if (bindex >= 0)
15676 +               au_set_h_fptr(file, bindex, NULL);
15677 +
15678 +       au_finfo_fin(file);
15679 +       return 0;
15680 +}
15681 +
15682 +/* ---------------------------------------------------------------------- */
15683 +
15684 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15685 +{
15686 +       int err;
15687 +       struct file *h_file;
15688 +
15689 +       err = 0;
15690 +       h_file = au_hf_top(file);
15691 +       if (h_file)
15692 +               err = vfsub_flush(h_file, id);
15693 +       return err;
15694 +}
15695 +
15696 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15697 +{
15698 +       return au_do_flush(file, id, au_do_flush_nondir);
15699 +}
15700 +
15701 +/* ---------------------------------------------------------------------- */
15702 +/*
15703 + * read and write functions acquire [fdi]_rwsem once, but release before
15704 + * mmap_sem. This is because to stop a race condition between mmap(2).
15705 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
15706 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15707 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15708 + */
15709 +
15710 +/* Callers should call au_read_post() or fput() in the end */
15711 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15712 +{
15713 +       struct file *h_file;
15714 +       int err;
15715 +
15716 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15717 +       if (!err) {
15718 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15719 +               h_file = au_hf_top(file);
15720 +               get_file(h_file);
15721 +               if (!keep_fi)
15722 +                       fi_read_unlock(file);
15723 +       } else
15724 +               h_file = ERR_PTR(err);
15725 +
15726 +       return h_file;
15727 +}
15728 +
15729 +static void au_read_post(struct inode *inode, struct file *h_file)
15730 +{
15731 +       /* update without lock, I don't think it a problem */
15732 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15733 +       fput(h_file);
15734 +}
15735 +
15736 +struct au_write_pre {
15737 +       /* input */
15738 +       unsigned int lsc;
15739 +
15740 +       /* output */
15741 +       blkcnt_t blks;
15742 +       aufs_bindex_t btop;
15743 +};
15744 +
15745 +/*
15746 + * return with iinfo is write-locked
15747 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15748 + * end
15749 + */
15750 +static struct file *au_write_pre(struct file *file, int do_ready,
15751 +                                struct au_write_pre *wpre)
15752 +{
15753 +       struct file *h_file;
15754 +       struct dentry *dentry;
15755 +       int err;
15756 +       unsigned int lsc;
15757 +       struct au_pin pin;
15758 +
15759 +       lsc = 0;
15760 +       if (wpre)
15761 +               lsc = wpre->lsc;
15762 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15763 +       h_file = ERR_PTR(err);
15764 +       if (unlikely(err))
15765 +               goto out;
15766 +
15767 +       dentry = file->f_path.dentry;
15768 +       if (do_ready) {
15769 +               err = au_ready_to_write(file, -1, &pin);
15770 +               if (unlikely(err)) {
15771 +                       h_file = ERR_PTR(err);
15772 +                       di_write_unlock(dentry);
15773 +                       goto out_fi;
15774 +               }
15775 +       }
15776 +
15777 +       di_downgrade_lock(dentry, /*flags*/0);
15778 +       if (wpre)
15779 +               wpre->btop = au_fbtop(file);
15780 +       h_file = au_hf_top(file);
15781 +       get_file(h_file);
15782 +       if (wpre)
15783 +               wpre->blks = file_inode(h_file)->i_blocks;
15784 +       if (do_ready)
15785 +               au_unpin(&pin);
15786 +       di_read_unlock(dentry, /*flags*/0);
15787 +
15788 +out_fi:
15789 +       fi_write_unlock(file);
15790 +out:
15791 +       return h_file;
15792 +}
15793 +
15794 +static void au_write_post(struct inode *inode, struct file *h_file,
15795 +                         struct au_write_pre *wpre, ssize_t written)
15796 +{
15797 +       struct inode *h_inode;
15798 +
15799 +       au_cpup_attr_timesizes(inode);
15800 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15801 +       h_inode = file_inode(h_file);
15802 +       inode->i_mode = h_inode->i_mode;
15803 +       ii_write_unlock(inode);
15804 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15805 +       if (written > 0)
15806 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15807 +                             /*force*/h_inode->i_blocks > wpre->blks);
15808 +       fput(h_file);
15809 +}
15810 +
15811 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15812 +                        loff_t *ppos)
15813 +{
15814 +       ssize_t err;
15815 +       struct inode *inode;
15816 +       struct file *h_file;
15817 +       struct super_block *sb;
15818 +
15819 +       inode = file_inode(file);
15820 +       sb = inode->i_sb;
15821 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15822 +
15823 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15824 +       err = PTR_ERR(h_file);
15825 +       if (IS_ERR(h_file))
15826 +               goto out;
15827 +
15828 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15829 +       err = vfsub_read_u(h_file, buf, count, ppos);
15830 +       /* todo: necessary? */
15831 +       /* file->f_ra = h_file->f_ra; */
15832 +       au_read_post(inode, h_file);
15833 +
15834 +out:
15835 +       si_read_unlock(sb);
15836 +       return err;
15837 +}
15838 +
15839 +/*
15840 + * todo: very ugly
15841 + * it locks both of i_mutex and si_rwsem for read in safe.
15842 + * if the plink maintenance mode continues forever (that is the problem),
15843 + * may loop forever.
15844 + */
15845 +static void au_mtx_and_read_lock(struct inode *inode)
15846 +{
15847 +       int err;
15848 +       struct super_block *sb = inode->i_sb;
15849 +
15850 +       while (1) {
15851 +               inode_lock(inode);
15852 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15853 +               if (!err)
15854 +                       break;
15855 +               inode_unlock(inode);
15856 +               si_read_lock(sb, AuLock_NOPLMW);
15857 +               si_read_unlock(sb);
15858 +       }
15859 +}
15860 +
15861 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15862 +                         size_t count, loff_t *ppos)
15863 +{
15864 +       ssize_t err;
15865 +       struct au_write_pre wpre;
15866 +       struct inode *inode;
15867 +       struct file *h_file;
15868 +       char __user *buf = (char __user *)ubuf;
15869 +
15870 +       inode = file_inode(file);
15871 +       au_mtx_and_read_lock(inode);
15872 +
15873 +       wpre.lsc = 0;
15874 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15875 +       err = PTR_ERR(h_file);
15876 +       if (IS_ERR(h_file))
15877 +               goto out;
15878 +
15879 +       err = vfsub_write_u(h_file, buf, count, ppos);
15880 +       au_write_post(inode, h_file, &wpre, err);
15881 +
15882 +out:
15883 +       si_read_unlock(inode->i_sb);
15884 +       inode_unlock(inode);
15885 +       return err;
15886 +}
15887 +
15888 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15889 +                         struct iov_iter *iov_iter)
15890 +{
15891 +       ssize_t err;
15892 +       struct file *file;
15893 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15894 +
15895 +       err = security_file_permission(h_file, rw);
15896 +       if (unlikely(err))
15897 +               goto out;
15898 +
15899 +       err = -ENOSYS;
15900 +       iter = NULL;
15901 +       if (rw == MAY_READ)
15902 +               iter = h_file->f_op->read_iter;
15903 +       else if (rw == MAY_WRITE)
15904 +               iter = h_file->f_op->write_iter;
15905 +
15906 +       file = kio->ki_filp;
15907 +       kio->ki_filp = h_file;
15908 +       if (iter) {
15909 +               lockdep_off();
15910 +               err = iter(kio, iov_iter);
15911 +               lockdep_on();
15912 +       } else
15913 +               /* currently there is no such fs */
15914 +               WARN_ON_ONCE(1);
15915 +       kio->ki_filp = file;
15916 +
15917 +out:
15918 +       return err;
15919 +}
15920 +
15921 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15922 +{
15923 +       ssize_t err;
15924 +       struct file *file, *h_file;
15925 +       struct inode *inode;
15926 +       struct super_block *sb;
15927 +
15928 +       file = kio->ki_filp;
15929 +       inode = file_inode(file);
15930 +       sb = inode->i_sb;
15931 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15932 +
15933 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15934 +       err = PTR_ERR(h_file);
15935 +       if (IS_ERR(h_file))
15936 +               goto out;
15937 +
15938 +       if (au_test_loopback_kthread()) {
15939 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15940 +               if (file->f_mapping != h_file->f_mapping) {
15941 +                       file->f_mapping = h_file->f_mapping;
15942 +                       smp_mb(); /* unnecessary? */
15943 +               }
15944 +       }
15945 +       fi_read_unlock(file);
15946 +
15947 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15948 +       /* todo: necessary? */
15949 +       /* file->f_ra = h_file->f_ra; */
15950 +       au_read_post(inode, h_file);
15951 +
15952 +out:
15953 +       si_read_unlock(sb);
15954 +       return err;
15955 +}
15956 +
15957 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15958 +{
15959 +       ssize_t err;
15960 +       struct au_write_pre wpre;
15961 +       struct inode *inode;
15962 +       struct file *file, *h_file;
15963 +
15964 +       file = kio->ki_filp;
15965 +       inode = file_inode(file);
15966 +       au_mtx_and_read_lock(inode);
15967 +
15968 +       wpre.lsc = 0;
15969 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15970 +       err = PTR_ERR(h_file);
15971 +       if (IS_ERR(h_file))
15972 +               goto out;
15973 +
15974 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15975 +       au_write_post(inode, h_file, &wpre, err);
15976 +
15977 +out:
15978 +       si_read_unlock(inode->i_sb);
15979 +       inode_unlock(inode);
15980 +       return err;
15981 +}
15982 +
15983 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15984 +                               struct pipe_inode_info *pipe, size_t len,
15985 +                               unsigned int flags)
15986 +{
15987 +       ssize_t err;
15988 +       struct file *h_file;
15989 +       struct inode *inode;
15990 +       struct super_block *sb;
15991 +
15992 +       inode = file_inode(file);
15993 +       sb = inode->i_sb;
15994 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15995 +
15996 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15997 +       err = PTR_ERR(h_file);
15998 +       if (IS_ERR(h_file))
15999 +               goto out;
16000 +
16001 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
16002 +       /* todo: necessasry? */
16003 +       /* file->f_ra = h_file->f_ra; */
16004 +       au_read_post(inode, h_file);
16005 +
16006 +out:
16007 +       si_read_unlock(sb);
16008 +       return err;
16009 +}
16010 +
16011 +static ssize_t
16012 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16013 +                 size_t len, unsigned int flags)
16014 +{
16015 +       ssize_t err;
16016 +       struct au_write_pre wpre;
16017 +       struct inode *inode;
16018 +       struct file *h_file;
16019 +
16020 +       inode = file_inode(file);
16021 +       au_mtx_and_read_lock(inode);
16022 +
16023 +       wpre.lsc = 0;
16024 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16025 +       err = PTR_ERR(h_file);
16026 +       if (IS_ERR(h_file))
16027 +               goto out;
16028 +
16029 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
16030 +       au_write_post(inode, h_file, &wpre, err);
16031 +
16032 +out:
16033 +       si_read_unlock(inode->i_sb);
16034 +       inode_unlock(inode);
16035 +       return err;
16036 +}
16037 +
16038 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16039 +                          loff_t len)
16040 +{
16041 +       long err;
16042 +       struct au_write_pre wpre;
16043 +       struct inode *inode;
16044 +       struct file *h_file;
16045 +
16046 +       inode = file_inode(file);
16047 +       au_mtx_and_read_lock(inode);
16048 +
16049 +       wpre.lsc = 0;
16050 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16051 +       err = PTR_ERR(h_file);
16052 +       if (IS_ERR(h_file))
16053 +               goto out;
16054 +
16055 +       lockdep_off();
16056 +       err = vfs_fallocate(h_file, mode, offset, len);
16057 +       lockdep_on();
16058 +       au_write_post(inode, h_file, &wpre, /*written*/1);
16059 +
16060 +out:
16061 +       si_read_unlock(inode->i_sb);
16062 +       inode_unlock(inode);
16063 +       return err;
16064 +}
16065 +
16066 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16067 +                                   struct file *dst, loff_t dst_pos,
16068 +                                   size_t len, unsigned int flags)
16069 +{
16070 +       ssize_t err;
16071 +       struct au_write_pre wpre;
16072 +       enum { SRC, DST };
16073 +       struct {
16074 +               struct inode *inode;
16075 +               struct file *h_file;
16076 +               struct super_block *h_sb;
16077 +       } a[2];
16078 +#define a_src  a[SRC]
16079 +#define a_dst  a[DST]
16080 +
16081 +       err = -EINVAL;
16082 +       a_src.inode = file_inode(src);
16083 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16084 +               goto out;
16085 +       a_dst.inode = file_inode(dst);
16086 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16087 +               goto out;
16088 +
16089 +       au_mtx_and_read_lock(a_dst.inode);
16090 +       /*
16091 +        * in order to match the order in di_write_lock2_{child,parent}(),
16092 +        * use f_path.dentry for this comparision.
16093 +        */
16094 +       if (src->f_path.dentry < dst->f_path.dentry) {
16095 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16096 +               err = PTR_ERR(a_src.h_file);
16097 +               if (IS_ERR(a_src.h_file))
16098 +                       goto out_si;
16099 +
16100 +               wpre.lsc = AuLsc_FI_2;
16101 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16102 +               err = PTR_ERR(a_dst.h_file);
16103 +               if (IS_ERR(a_dst.h_file)) {
16104 +                       au_read_post(a_src.inode, a_src.h_file);
16105 +                       goto out_si;
16106 +               }
16107 +       } else {
16108 +               wpre.lsc = AuLsc_FI_1;
16109 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16110 +               err = PTR_ERR(a_dst.h_file);
16111 +               if (IS_ERR(a_dst.h_file))
16112 +                       goto out_si;
16113 +
16114 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16115 +               err = PTR_ERR(a_src.h_file);
16116 +               if (IS_ERR(a_src.h_file)) {
16117 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16118 +                                     /*written*/0);
16119 +                       goto out_si;
16120 +               }
16121 +       }
16122 +
16123 +       err = -EXDEV;
16124 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16125 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16126 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16127 +               AuDbgFile(src);
16128 +               AuDbgFile(dst);
16129 +               goto out_file;
16130 +       }
16131 +
16132 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16133 +                                   dst_pos, len, flags);
16134 +
16135 +out_file:
16136 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16137 +       fi_read_unlock(src);
16138 +       au_read_post(a_src.inode, a_src.h_file);
16139 +out_si:
16140 +       si_read_unlock(a_dst.inode->i_sb);
16141 +       inode_unlock(a_dst.inode);
16142 +out:
16143 +       return err;
16144 +#undef a_src
16145 +#undef a_dst
16146 +}
16147 +
16148 +/* ---------------------------------------------------------------------- */
16149 +
16150 +/*
16151 + * The locking order around current->mmap_sem.
16152 + * - in most and regular cases
16153 + *   file I/O syscall -- aufs_read() or something
16154 + *     -- si_rwsem for read -- mmap_sem
16155 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16156 + * - in mmap case
16157 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16158 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
16159 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
16160 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16161 + * It means that when aufs acquires si_rwsem for write, the process should never
16162 + * acquire mmap_sem.
16163 + *
16164 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16165 + * problem either since any directory is not able to be mmap-ed.
16166 + * The similar scenario is applied to aufs_readlink() too.
16167 + */
16168 +
16169 +#if 0 /* stop calling security_file_mmap() */
16170 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16171 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16172 +
16173 +static unsigned long au_arch_prot_conv(unsigned long flags)
16174 +{
16175 +       /* currently ppc64 only */
16176 +#ifdef CONFIG_PPC64
16177 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16178 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16179 +       return AuConv_VM_PROT(flags, SAO);
16180 +#else
16181 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16182 +       return 0;
16183 +#endif
16184 +}
16185 +
16186 +static unsigned long au_prot_conv(unsigned long flags)
16187 +{
16188 +       return AuConv_VM_PROT(flags, READ)
16189 +               | AuConv_VM_PROT(flags, WRITE)
16190 +               | AuConv_VM_PROT(flags, EXEC)
16191 +               | au_arch_prot_conv(flags);
16192 +}
16193 +
16194 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16195 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16196 +
16197 +static unsigned long au_flag_conv(unsigned long flags)
16198 +{
16199 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16200 +               | AuConv_VM_MAP(flags, DENYWRITE)
16201 +               | AuConv_VM_MAP(flags, LOCKED);
16202 +}
16203 +#endif
16204 +
16205 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16206 +{
16207 +       int err;
16208 +       const unsigned char wlock
16209 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16210 +       struct super_block *sb;
16211 +       struct file *h_file;
16212 +       struct inode *inode;
16213 +
16214 +       AuDbgVmRegion(file, vma);
16215 +
16216 +       inode = file_inode(file);
16217 +       sb = inode->i_sb;
16218 +       lockdep_off();
16219 +       si_read_lock(sb, AuLock_NOPLMW);
16220 +
16221 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16222 +       lockdep_on();
16223 +       err = PTR_ERR(h_file);
16224 +       if (IS_ERR(h_file))
16225 +               goto out;
16226 +
16227 +       err = 0;
16228 +       au_set_mmapped(file);
16229 +       au_vm_file_reset(vma, h_file);
16230 +       /*
16231 +        * we cannot call security_mmap_file() here since it may acquire
16232 +        * mmap_sem or i_mutex.
16233 +        *
16234 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16235 +        *                       au_flag_conv(vma->vm_flags));
16236 +        */
16237 +       if (!err)
16238 +               err = call_mmap(h_file, vma);
16239 +       if (!err) {
16240 +               au_vm_prfile_set(vma, file);
16241 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16242 +               goto out_fput; /* success */
16243 +       }
16244 +       au_unset_mmapped(file);
16245 +       au_vm_file_reset(vma, file);
16246 +
16247 +out_fput:
16248 +       lockdep_off();
16249 +       ii_write_unlock(inode);
16250 +       lockdep_on();
16251 +       fput(h_file);
16252 +out:
16253 +       lockdep_off();
16254 +       si_read_unlock(sb);
16255 +       lockdep_on();
16256 +       AuTraceErr(err);
16257 +       return err;
16258 +}
16259 +
16260 +/* ---------------------------------------------------------------------- */
16261 +
16262 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16263 +                            int datasync)
16264 +{
16265 +       int err;
16266 +       struct au_write_pre wpre;
16267 +       struct inode *inode;
16268 +       struct file *h_file;
16269 +
16270 +       err = 0; /* -EBADF; */ /* posix? */
16271 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16272 +               goto out;
16273 +
16274 +       inode = file_inode(file);
16275 +       au_mtx_and_read_lock(inode);
16276 +
16277 +       wpre.lsc = 0;
16278 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16279 +       err = PTR_ERR(h_file);
16280 +       if (IS_ERR(h_file))
16281 +               goto out_unlock;
16282 +
16283 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16284 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16285 +
16286 +out_unlock:
16287 +       si_read_unlock(inode->i_sb);
16288 +       inode_unlock(inode);
16289 +out:
16290 +       return err;
16291 +}
16292 +
16293 +static int aufs_fasync(int fd, struct file *file, int flag)
16294 +{
16295 +       int err;
16296 +       struct file *h_file;
16297 +       struct super_block *sb;
16298 +
16299 +       sb = file->f_path.dentry->d_sb;
16300 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16301 +
16302 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16303 +       err = PTR_ERR(h_file);
16304 +       if (IS_ERR(h_file))
16305 +               goto out;
16306 +
16307 +       if (h_file->f_op->fasync)
16308 +               err = h_file->f_op->fasync(fd, h_file, flag);
16309 +       fput(h_file); /* instead of au_read_post() */
16310 +
16311 +out:
16312 +       si_read_unlock(sb);
16313 +       return err;
16314 +}
16315 +
16316 +static int aufs_setfl(struct file *file, unsigned long arg)
16317 +{
16318 +       int err;
16319 +       struct file *h_file;
16320 +       struct super_block *sb;
16321 +
16322 +       sb = file->f_path.dentry->d_sb;
16323 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16324 +
16325 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16326 +       err = PTR_ERR(h_file);
16327 +       if (IS_ERR(h_file))
16328 +               goto out;
16329 +
16330 +       /* stop calling h_file->fasync */
16331 +       arg |= vfsub_file_flags(file) & FASYNC;
16332 +       err = setfl(/*unused fd*/-1, h_file, arg);
16333 +       fput(h_file); /* instead of au_read_post() */
16334 +
16335 +out:
16336 +       si_read_unlock(sb);
16337 +       return err;
16338 +}
16339 +
16340 +/* ---------------------------------------------------------------------- */
16341 +
16342 +/* no one supports this operation, currently */
16343 +#if 0
16344 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16345 +                            size_t len, loff_t *pos, int more)
16346 +{
16347 +}
16348 +#endif
16349 +
16350 +/* ---------------------------------------------------------------------- */
16351 +
16352 +const struct file_operations aufs_file_fop = {
16353 +       .owner          = THIS_MODULE,
16354 +
16355 +       .llseek         = default_llseek,
16356 +
16357 +       .read           = aufs_read,
16358 +       .write          = aufs_write,
16359 +       .read_iter      = aufs_read_iter,
16360 +       .write_iter     = aufs_write_iter,
16361 +
16362 +#ifdef CONFIG_AUFS_POLL
16363 +       .poll           = aufs_poll,
16364 +#endif
16365 +       .unlocked_ioctl = aufs_ioctl_nondir,
16366 +#ifdef CONFIG_COMPAT
16367 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16368 +#endif
16369 +       .mmap           = aufs_mmap,
16370 +       .open           = aufs_open_nondir,
16371 +       .flush          = aufs_flush_nondir,
16372 +       .release        = aufs_release_nondir,
16373 +       .fsync          = aufs_fsync_nondir,
16374 +       .fasync         = aufs_fasync,
16375 +       /* .sendpage    = aufs_sendpage, */
16376 +       .setfl          = aufs_setfl,
16377 +       .splice_write   = aufs_splice_write,
16378 +       .splice_read    = aufs_splice_read,
16379 +#if 0
16380 +       .aio_splice_write = aufs_aio_splice_write,
16381 +       .aio_splice_read  = aufs_aio_splice_read,
16382 +#endif
16383 +       .fallocate      = aufs_fallocate,
16384 +       .copy_file_range = aufs_copy_file_range
16385 +};
16386 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16387 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16388 +++ linux/fs/aufs/fstype.h      2018-07-19 09:46:13.059981241 +0200
16389 @@ -0,0 +1,401 @@
16390 +/* SPDX-License-Identifier: GPL-2.0 */
16391 +/*
16392 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16393 + *
16394 + * This program, aufs is free software; you can redistribute it and/or modify
16395 + * it under the terms of the GNU General Public License as published by
16396 + * the Free Software Foundation; either version 2 of the License, or
16397 + * (at your option) any later version.
16398 + *
16399 + * This program is distributed in the hope that it will be useful,
16400 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16401 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16402 + * GNU General Public License for more details.
16403 + *
16404 + * You should have received a copy of the GNU General Public License
16405 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16406 + */
16407 +
16408 +/*
16409 + * judging filesystem type
16410 + */
16411 +
16412 +#ifndef __AUFS_FSTYPE_H__
16413 +#define __AUFS_FSTYPE_H__
16414 +
16415 +#ifdef __KERNEL__
16416 +
16417 +#include <linux/fs.h>
16418 +#include <linux/magic.h>
16419 +#include <linux/nfs_fs.h>
16420 +#include <linux/romfs_fs.h>
16421 +
16422 +static inline int au_test_aufs(struct super_block *sb)
16423 +{
16424 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16425 +}
16426 +
16427 +static inline const char *au_sbtype(struct super_block *sb)
16428 +{
16429 +       return sb->s_type->name;
16430 +}
16431 +
16432 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16433 +{
16434 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16435 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16436 +#else
16437 +       return 0;
16438 +#endif
16439 +}
16440 +
16441 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16442 +{
16443 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16444 +       return sb->s_magic == ROMFS_MAGIC;
16445 +#else
16446 +       return 0;
16447 +#endif
16448 +}
16449 +
16450 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16451 +{
16452 +#if IS_ENABLED(CONFIG_CRAMFS)
16453 +       return sb->s_magic == CRAMFS_MAGIC;
16454 +#endif
16455 +       return 0;
16456 +}
16457 +
16458 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16459 +{
16460 +#if IS_ENABLED(CONFIG_NFS_FS)
16461 +       return sb->s_magic == NFS_SUPER_MAGIC;
16462 +#else
16463 +       return 0;
16464 +#endif
16465 +}
16466 +
16467 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16468 +{
16469 +#if IS_ENABLED(CONFIG_FUSE_FS)
16470 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16471 +#else
16472 +       return 0;
16473 +#endif
16474 +}
16475 +
16476 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16477 +{
16478 +#if IS_ENABLED(CONFIG_XFS_FS)
16479 +       return sb->s_magic == XFS_SB_MAGIC;
16480 +#else
16481 +       return 0;
16482 +#endif
16483 +}
16484 +
16485 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16486 +{
16487 +#ifdef CONFIG_TMPFS
16488 +       return sb->s_magic == TMPFS_MAGIC;
16489 +#else
16490 +       return 0;
16491 +#endif
16492 +}
16493 +
16494 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16495 +{
16496 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16497 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16498 +#else
16499 +       return 0;
16500 +#endif
16501 +}
16502 +
16503 +static inline int au_test_ramfs(struct super_block *sb)
16504 +{
16505 +       return sb->s_magic == RAMFS_MAGIC;
16506 +}
16507 +
16508 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16509 +{
16510 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16511 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16512 +#else
16513 +       return 0;
16514 +#endif
16515 +}
16516 +
16517 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16518 +{
16519 +#ifdef CONFIG_PROC_FS
16520 +       return sb->s_magic == PROC_SUPER_MAGIC;
16521 +#else
16522 +       return 0;
16523 +#endif
16524 +}
16525 +
16526 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16527 +{
16528 +#ifdef CONFIG_SYSFS
16529 +       return sb->s_magic == SYSFS_MAGIC;
16530 +#else
16531 +       return 0;
16532 +#endif
16533 +}
16534 +
16535 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16536 +{
16537 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16538 +       return sb->s_magic == CONFIGFS_MAGIC;
16539 +#else
16540 +       return 0;
16541 +#endif
16542 +}
16543 +
16544 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16545 +{
16546 +#if IS_ENABLED(CONFIG_MINIX_FS)
16547 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16548 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16549 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16550 +               || sb->s_magic == MINIX_SUPER_MAGIC
16551 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16552 +#else
16553 +       return 0;
16554 +#endif
16555 +}
16556 +
16557 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16558 +{
16559 +#if IS_ENABLED(CONFIG_FAT_FS)
16560 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16561 +#else
16562 +       return 0;
16563 +#endif
16564 +}
16565 +
16566 +static inline int au_test_msdos(struct super_block *sb)
16567 +{
16568 +       return au_test_fat(sb);
16569 +}
16570 +
16571 +static inline int au_test_vfat(struct super_block *sb)
16572 +{
16573 +       return au_test_fat(sb);
16574 +}
16575 +
16576 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16577 +{
16578 +#ifdef CONFIG_SECURITYFS
16579 +       return sb->s_magic == SECURITYFS_MAGIC;
16580 +#else
16581 +       return 0;
16582 +#endif
16583 +}
16584 +
16585 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16586 +{
16587 +#if IS_ENABLED(CONFIG_SQUASHFS)
16588 +       return sb->s_magic == SQUASHFS_MAGIC;
16589 +#else
16590 +       return 0;
16591 +#endif
16592 +}
16593 +
16594 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16595 +{
16596 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16597 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16598 +#else
16599 +       return 0;
16600 +#endif
16601 +}
16602 +
16603 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16604 +{
16605 +#if IS_ENABLED(CONFIG_XENFS)
16606 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16607 +#else
16608 +       return 0;
16609 +#endif
16610 +}
16611 +
16612 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16613 +{
16614 +#ifdef CONFIG_DEBUG_FS
16615 +       return sb->s_magic == DEBUGFS_MAGIC;
16616 +#else
16617 +       return 0;
16618 +#endif
16619 +}
16620 +
16621 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16622 +{
16623 +#if IS_ENABLED(CONFIG_NILFS)
16624 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16625 +#else
16626 +       return 0;
16627 +#endif
16628 +}
16629 +
16630 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16631 +{
16632 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16633 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16634 +#else
16635 +       return 0;
16636 +#endif
16637 +}
16638 +
16639 +/* ---------------------------------------------------------------------- */
16640 +/*
16641 + * they can't be an aufs branch.
16642 + */
16643 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16644 +{
16645 +       return
16646 +#ifndef CONFIG_AUFS_BR_RAMFS
16647 +               au_test_ramfs(sb) ||
16648 +#endif
16649 +               au_test_procfs(sb)
16650 +               || au_test_sysfs(sb)
16651 +               || au_test_configfs(sb)
16652 +               || au_test_debugfs(sb)
16653 +               || au_test_securityfs(sb)
16654 +               || au_test_xenfs(sb)
16655 +               || au_test_ecryptfs(sb)
16656 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16657 +               || au_test_aufs(sb); /* will be supported in next version */
16658 +}
16659 +
16660 +static inline int au_test_fs_remote(struct super_block *sb)
16661 +{
16662 +       return !au_test_tmpfs(sb)
16663 +#ifdef CONFIG_AUFS_BR_RAMFS
16664 +               && !au_test_ramfs(sb)
16665 +#endif
16666 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16667 +}
16668 +
16669 +/* ---------------------------------------------------------------------- */
16670 +
16671 +/*
16672 + * Note: these functions (below) are created after reading ->getattr() in all
16673 + * filesystems under linux/fs. it means we have to do so in every update...
16674 + */
16675 +
16676 +/*
16677 + * some filesystems require getattr to refresh the inode attributes before
16678 + * referencing.
16679 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16680 + * and leave the work for d_revalidate()
16681 + */
16682 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16683 +{
16684 +       return au_test_nfs(sb)
16685 +               || au_test_fuse(sb)
16686 +               /* || au_test_btrfs(sb) */      /* untested */
16687 +               ;
16688 +}
16689 +
16690 +/*
16691 + * filesystems which don't maintain i_size or i_blocks.
16692 + */
16693 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16694 +{
16695 +       return au_test_xfs(sb)
16696 +               || au_test_btrfs(sb)
16697 +               || au_test_ubifs(sb)
16698 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16699 +               /* || au_test_minix(sb) */      /* untested */
16700 +               ;
16701 +}
16702 +
16703 +/*
16704 + * filesystems which don't store the correct value in some of their inode
16705 + * attributes.
16706 + */
16707 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16708 +{
16709 +       return au_test_fs_bad_iattr_size(sb)
16710 +               || au_test_fat(sb)
16711 +               || au_test_msdos(sb)
16712 +               || au_test_vfat(sb);
16713 +}
16714 +
16715 +/* they don't check i_nlink in link(2) */
16716 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16717 +{
16718 +       return au_test_tmpfs(sb)
16719 +#ifdef CONFIG_AUFS_BR_RAMFS
16720 +               || au_test_ramfs(sb)
16721 +#endif
16722 +               || au_test_ubifs(sb)
16723 +               || au_test_hfsplus(sb);
16724 +}
16725 +
16726 +/*
16727 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16728 + */
16729 +static inline int au_test_fs_notime(struct super_block *sb)
16730 +{
16731 +       return au_test_nfs(sb)
16732 +               || au_test_fuse(sb)
16733 +               || au_test_ubifs(sb)
16734 +               ;
16735 +}
16736 +
16737 +/* temporary support for i#1 in cramfs */
16738 +static inline int au_test_fs_unique_ino(struct inode *inode)
16739 +{
16740 +       if (au_test_cramfs(inode->i_sb))
16741 +               return inode->i_ino != 1;
16742 +       return 1;
16743 +}
16744 +
16745 +/* ---------------------------------------------------------------------- */
16746 +
16747 +/*
16748 + * the filesystem where the xino files placed must support i/o after unlink and
16749 + * maintain i_size and i_blocks.
16750 + */
16751 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16752 +{
16753 +       return au_test_fs_remote(sb)
16754 +               || au_test_fs_bad_iattr_size(sb)
16755 +               /* don't want unnecessary work for xino */
16756 +               || au_test_aufs(sb)
16757 +               || au_test_ecryptfs(sb)
16758 +               || au_test_nilfs(sb);
16759 +}
16760 +
16761 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16762 +{
16763 +       return au_test_tmpfs(sb)
16764 +               || au_test_ramfs(sb);
16765 +}
16766 +
16767 +/*
16768 + * test if the @sb is real-readonly.
16769 + */
16770 +static inline int au_test_fs_rr(struct super_block *sb)
16771 +{
16772 +       return au_test_squashfs(sb)
16773 +               || au_test_iso9660(sb)
16774 +               || au_test_cramfs(sb)
16775 +               || au_test_romfs(sb);
16776 +}
16777 +
16778 +/*
16779 + * test if the @inode is nfs with 'noacl' option
16780 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16781 + */
16782 +static inline int au_test_nfs_noacl(struct inode *inode)
16783 +{
16784 +       return au_test_nfs(inode->i_sb)
16785 +               /* && IS_POSIXACL(inode) */
16786 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16787 +}
16788 +
16789 +#endif /* __KERNEL__ */
16790 +#endif /* __AUFS_FSTYPE_H__ */
16791 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16792 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16793 +++ linux/fs/aufs/hbl.h 2018-07-19 09:46:13.059981241 +0200
16794 @@ -0,0 +1,65 @@
16795 +/* SPDX-License-Identifier: GPL-2.0 */
16796 +/*
16797 + * Copyright (C) 2017-2018 Junjiro R. Okajima
16798 + *
16799 + * This program, aufs is free software; you can redistribute it and/or modify
16800 + * it under the terms of the GNU General Public License as published by
16801 + * the Free Software Foundation; either version 2 of the License, or
16802 + * (at your option) any later version.
16803 + *
16804 + * This program is distributed in the hope that it will be useful,
16805 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16806 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16807 + * GNU General Public License for more details.
16808 + *
16809 + * You should have received a copy of the GNU General Public License
16810 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16811 + */
16812 +
16813 +/*
16814 + * helpers for hlist_bl.h
16815 + */
16816 +
16817 +#ifndef __AUFS_HBL_H__
16818 +#define __AUFS_HBL_H__
16819 +
16820 +#ifdef __KERNEL__
16821 +
16822 +#include <linux/list_bl.h>
16823 +
16824 +static inline void au_hbl_add(struct hlist_bl_node *node,
16825 +                             struct hlist_bl_head *hbl)
16826 +{
16827 +       hlist_bl_lock(hbl);
16828 +       hlist_bl_add_head(node, hbl);
16829 +       hlist_bl_unlock(hbl);
16830 +}
16831 +
16832 +static inline void au_hbl_del(struct hlist_bl_node *node,
16833 +                             struct hlist_bl_head *hbl)
16834 +{
16835 +       hlist_bl_lock(hbl);
16836 +       hlist_bl_del(node);
16837 +       hlist_bl_unlock(hbl);
16838 +}
16839 +
16840 +#define au_hbl_for_each(pos, head)                                     \
16841 +       for (pos = hlist_bl_first(head);                                \
16842 +            pos;                                                       \
16843 +            pos = pos->next)
16844 +
16845 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16846 +{
16847 +       unsigned long cnt;
16848 +       struct hlist_bl_node *pos;
16849 +
16850 +       cnt = 0;
16851 +       hlist_bl_lock(hbl);
16852 +       au_hbl_for_each(pos, hbl)
16853 +               cnt++;
16854 +       hlist_bl_unlock(hbl);
16855 +       return cnt;
16856 +}
16857 +
16858 +#endif /* __KERNEL__ */
16859 +#endif /* __AUFS_HBL_H__ */
16860 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16861 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16862 +++ linux/fs/aufs/hfsnotify.c   2018-07-19 09:46:13.059981241 +0200
16863 @@ -0,0 +1,290 @@
16864 +/* SPDX-License-Identifier: GPL-2.0 */
16865 +/*
16866 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16867 + *
16868 + * This program, aufs is free software; you can redistribute it and/or modify
16869 + * it under the terms of the GNU General Public License as published by
16870 + * the Free Software Foundation; either version 2 of the License, or
16871 + * (at your option) any later version.
16872 + *
16873 + * This program is distributed in the hope that it will be useful,
16874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16876 + * GNU General Public License for more details.
16877 + *
16878 + * You should have received a copy of the GNU General Public License
16879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16880 + */
16881 +
16882 +/*
16883 + * fsnotify for the lower directories
16884 + */
16885 +
16886 +#include "aufs.h"
16887 +
16888 +/* FS_IN_IGNORED is unnecessary */
16889 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16890 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16891 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16892 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16893 +
16894 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16895 +{
16896 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16897 +                                            hn_mark);
16898 +       /* AuDbg("here\n"); */
16899 +       au_cache_free_hnotify(hn);
16900 +       smp_mb__before_atomic(); /* for atomic64_dec */
16901 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16902 +               wake_up(&au_hfsn_wq);
16903 +}
16904 +
16905 +static int au_hfsn_alloc(struct au_hinode *hinode)
16906 +{
16907 +       int err;
16908 +       struct au_hnotify *hn;
16909 +       struct super_block *sb;
16910 +       struct au_branch *br;
16911 +       struct fsnotify_mark *mark;
16912 +       aufs_bindex_t bindex;
16913 +
16914 +       hn = hinode->hi_notify;
16915 +       sb = hn->hn_aufs_inode->i_sb;
16916 +       bindex = au_br_index(sb, hinode->hi_id);
16917 +       br = au_sbr(sb, bindex);
16918 +       AuDebugOn(!br->br_hfsn);
16919 +
16920 +       mark = &hn->hn_mark;
16921 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16922 +       mark->mask = AuHfsnMask;
16923 +       /*
16924 +        * by udba rename or rmdir, aufs assign a new inode to the known
16925 +        * h_inode, so specify 1 to allow dups.
16926 +        */
16927 +       lockdep_off();
16928 +       err = fsnotify_add_mark(mark, hinode->hi_inode, /*mnt*/NULL,
16929 +                               /*allow_dups*/1);
16930 +       lockdep_on();
16931 +
16932 +       return err;
16933 +}
16934 +
16935 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16936 +{
16937 +       struct fsnotify_mark *mark;
16938 +       unsigned long long ull;
16939 +       struct fsnotify_group *group;
16940 +
16941 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16942 +       BUG_ON(!ull);
16943 +
16944 +       mark = &hn->hn_mark;
16945 +       spin_lock(&mark->lock);
16946 +       group = mark->group;
16947 +       fsnotify_get_group(group);
16948 +       spin_unlock(&mark->lock);
16949 +       lockdep_off();
16950 +       fsnotify_destroy_mark(mark, group);
16951 +       fsnotify_put_mark(mark);
16952 +       fsnotify_put_group(group);
16953 +       lockdep_on();
16954 +
16955 +       /* free hn by myself */
16956 +       return 0;
16957 +}
16958 +
16959 +/* ---------------------------------------------------------------------- */
16960 +
16961 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16962 +{
16963 +       struct fsnotify_mark *mark;
16964 +
16965 +       mark = &hinode->hi_notify->hn_mark;
16966 +       spin_lock(&mark->lock);
16967 +       if (do_set) {
16968 +               AuDebugOn(mark->mask & AuHfsnMask);
16969 +               mark->mask |= AuHfsnMask;
16970 +       } else {
16971 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16972 +               mark->mask &= ~AuHfsnMask;
16973 +       }
16974 +       spin_unlock(&mark->lock);
16975 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16976 +}
16977 +
16978 +/* ---------------------------------------------------------------------- */
16979 +
16980 +/* #define AuDbgHnotify */
16981 +#ifdef AuDbgHnotify
16982 +static char *au_hfsn_name(u32 mask)
16983 +{
16984 +#ifdef CONFIG_AUFS_DEBUG
16985 +#define test_ret(flag)                         \
16986 +       do {                                    \
16987 +               if (mask & flag)                \
16988 +                       return #flag;           \
16989 +       } while (0)
16990 +       test_ret(FS_ACCESS);
16991 +       test_ret(FS_MODIFY);
16992 +       test_ret(FS_ATTRIB);
16993 +       test_ret(FS_CLOSE_WRITE);
16994 +       test_ret(FS_CLOSE_NOWRITE);
16995 +       test_ret(FS_OPEN);
16996 +       test_ret(FS_MOVED_FROM);
16997 +       test_ret(FS_MOVED_TO);
16998 +       test_ret(FS_CREATE);
16999 +       test_ret(FS_DELETE);
17000 +       test_ret(FS_DELETE_SELF);
17001 +       test_ret(FS_MOVE_SELF);
17002 +       test_ret(FS_UNMOUNT);
17003 +       test_ret(FS_Q_OVERFLOW);
17004 +       test_ret(FS_IN_IGNORED);
17005 +       test_ret(FS_ISDIR);
17006 +       test_ret(FS_IN_ONESHOT);
17007 +       test_ret(FS_EVENT_ON_CHILD);
17008 +       return "";
17009 +#undef test_ret
17010 +#else
17011 +       return "??";
17012 +#endif
17013 +}
17014 +#endif
17015 +
17016 +/* ---------------------------------------------------------------------- */
17017 +
17018 +static void au_hfsn_free_group(struct fsnotify_group *group)
17019 +{
17020 +       struct au_br_hfsnotify *hfsn = group->private;
17021 +
17022 +       /* AuDbg("here\n"); */
17023 +       kfree(hfsn);
17024 +}
17025 +
17026 +static int au_hfsn_handle_event(struct fsnotify_group *group,
17027 +                               struct inode *inode,
17028 +                               struct fsnotify_mark *inode_mark,
17029 +                               struct fsnotify_mark *vfsmount_mark,
17030 +                               u32 mask, const void *data, int data_type,
17031 +                               const unsigned char *file_name, u32 cookie,
17032 +                               struct fsnotify_iter_info *iter_info)
17033 +{
17034 +       int err;
17035 +       struct au_hnotify *hnotify;
17036 +       struct inode *h_dir, *h_inode;
17037 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
17038 +
17039 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
17040 +
17041 +       err = 0;
17042 +       /* if FS_UNMOUNT happens, there must be another bug */
17043 +       AuDebugOn(mask & FS_UNMOUNT);
17044 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
17045 +               goto out;
17046 +
17047 +       h_dir = inode;
17048 +       h_inode = NULL;
17049 +#ifdef AuDbgHnotify
17050 +       au_debug_on();
17051 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17052 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17053 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17054 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
17055 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17056 +               /* WARN_ON(1); */
17057 +       }
17058 +       au_debug_off();
17059 +#endif
17060 +
17061 +       AuDebugOn(!inode_mark);
17062 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17063 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
17064 +
17065 +out:
17066 +       return err;
17067 +}
17068 +
17069 +static struct fsnotify_ops au_hfsn_ops = {
17070 +       .handle_event           = au_hfsn_handle_event,
17071 +       .free_group_priv        = au_hfsn_free_group,
17072 +       .free_mark              = au_hfsn_free_mark
17073 +};
17074 +
17075 +/* ---------------------------------------------------------------------- */
17076 +
17077 +static void au_hfsn_fin_br(struct au_branch *br)
17078 +{
17079 +       struct au_br_hfsnotify *hfsn;
17080 +
17081 +       hfsn = br->br_hfsn;
17082 +       if (hfsn) {
17083 +               lockdep_off();
17084 +               fsnotify_put_group(hfsn->hfsn_group);
17085 +               lockdep_on();
17086 +       }
17087 +}
17088 +
17089 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17090 +{
17091 +       int err;
17092 +       struct fsnotify_group *group;
17093 +       struct au_br_hfsnotify *hfsn;
17094 +
17095 +       err = 0;
17096 +       br->br_hfsn = NULL;
17097 +       if (!au_br_hnotifyable(perm))
17098 +               goto out;
17099 +
17100 +       err = -ENOMEM;
17101 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17102 +       if (unlikely(!hfsn))
17103 +               goto out;
17104 +
17105 +       err = 0;
17106 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17107 +       if (IS_ERR(group)) {
17108 +               err = PTR_ERR(group);
17109 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17110 +               goto out_hfsn;
17111 +       }
17112 +
17113 +       group->private = hfsn;
17114 +       hfsn->hfsn_group = group;
17115 +       br->br_hfsn = hfsn;
17116 +       goto out; /* success */
17117 +
17118 +out_hfsn:
17119 +       kfree(hfsn);
17120 +out:
17121 +       return err;
17122 +}
17123 +
17124 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17125 +{
17126 +       int err;
17127 +
17128 +       err = 0;
17129 +       if (!br->br_hfsn)
17130 +               err = au_hfsn_init_br(br, perm);
17131 +
17132 +       return err;
17133 +}
17134 +
17135 +/* ---------------------------------------------------------------------- */
17136 +
17137 +static void au_hfsn_fin(void)
17138 +{
17139 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17140 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17141 +}
17142 +
17143 +const struct au_hnotify_op au_hnotify_op = {
17144 +       .ctl            = au_hfsn_ctl,
17145 +       .alloc          = au_hfsn_alloc,
17146 +       .free           = au_hfsn_free,
17147 +
17148 +       .fin            = au_hfsn_fin,
17149 +
17150 +       .reset_br       = au_hfsn_reset_br,
17151 +       .fin_br         = au_hfsn_fin_br,
17152 +       .init_br        = au_hfsn_init_br
17153 +};
17154 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17155 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17156 +++ linux/fs/aufs/hfsplus.c     2018-07-19 09:46:13.059981241 +0200
17157 @@ -0,0 +1,57 @@
17158 +/* SPDX-License-Identifier: GPL-2.0 */
17159 +/*
17160 + * Copyright (C) 2010-2018 Junjiro R. Okajima
17161 + *
17162 + * This program, aufs is free software; you can redistribute it and/or modify
17163 + * it under the terms of the GNU General Public License as published by
17164 + * the Free Software Foundation; either version 2 of the License, or
17165 + * (at your option) any later version.
17166 + *
17167 + * This program is distributed in the hope that it will be useful,
17168 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17169 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17170 + * GNU General Public License for more details.
17171 + *
17172 + * You should have received a copy of the GNU General Public License
17173 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17174 + */
17175 +
17176 +/*
17177 + * special support for filesystems which aqucires an inode mutex
17178 + * at final closing a file, eg, hfsplus.
17179 + *
17180 + * This trick is very simple and stupid, just to open the file before really
17181 + * neceeary open to tell hfsplus that this is not the final closing.
17182 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17183 + * and au_h_open_post() after releasing it.
17184 + */
17185 +
17186 +#include "aufs.h"
17187 +
17188 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17189 +                          int force_wr)
17190 +{
17191 +       struct file *h_file;
17192 +       struct dentry *h_dentry;
17193 +
17194 +       h_dentry = au_h_dptr(dentry, bindex);
17195 +       AuDebugOn(!h_dentry);
17196 +       AuDebugOn(d_is_negative(h_dentry));
17197 +
17198 +       h_file = NULL;
17199 +       if (au_test_hfsplus(h_dentry->d_sb)
17200 +           && d_is_reg(h_dentry))
17201 +               h_file = au_h_open(dentry, bindex,
17202 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17203 +                                  /*file*/NULL, force_wr);
17204 +       return h_file;
17205 +}
17206 +
17207 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17208 +                   struct file *h_file)
17209 +{
17210 +       if (h_file) {
17211 +               fput(h_file);
17212 +               au_sbr_put(dentry->d_sb, bindex);
17213 +       }
17214 +}
17215 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17216 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17217 +++ linux/fs/aufs/hnotify.c     2018-07-19 09:46:13.059981241 +0200
17218 @@ -0,0 +1,720 @@
17219 +/* SPDX-License-Identifier: GPL-2.0 */
17220 +/*
17221 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17222 + *
17223 + * This program, aufs is free software; you can redistribute it and/or modify
17224 + * it under the terms of the GNU General Public License as published by
17225 + * the Free Software Foundation; either version 2 of the License, or
17226 + * (at your option) any later version.
17227 + *
17228 + * This program is distributed in the hope that it will be useful,
17229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17231 + * GNU General Public License for more details.
17232 + *
17233 + * You should have received a copy of the GNU General Public License
17234 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17235 + */
17236 +
17237 +/*
17238 + * abstraction to notify the direct changes on lower directories
17239 + */
17240 +
17241 +#include "aufs.h"
17242 +
17243 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17244 +{
17245 +       int err;
17246 +       struct au_hnotify *hn;
17247 +
17248 +       err = -ENOMEM;
17249 +       hn = au_cache_alloc_hnotify();
17250 +       if (hn) {
17251 +               hn->hn_aufs_inode = inode;
17252 +               hinode->hi_notify = hn;
17253 +               err = au_hnotify_op.alloc(hinode);
17254 +               AuTraceErr(err);
17255 +               if (unlikely(err)) {
17256 +                       hinode->hi_notify = NULL;
17257 +                       au_cache_free_hnotify(hn);
17258 +                       /*
17259 +                        * The upper dir was removed by udba, but the same named
17260 +                        * dir left. In this case, aufs assignes a new inode
17261 +                        * number and set the monitor again.
17262 +                        * For the lower dir, the old monitnor is still left.
17263 +                        */
17264 +                       if (err == -EEXIST)
17265 +                               err = 0;
17266 +               }
17267 +       }
17268 +
17269 +       AuTraceErr(err);
17270 +       return err;
17271 +}
17272 +
17273 +void au_hn_free(struct au_hinode *hinode)
17274 +{
17275 +       struct au_hnotify *hn;
17276 +
17277 +       hn = hinode->hi_notify;
17278 +       if (hn) {
17279 +               hinode->hi_notify = NULL;
17280 +               if (au_hnotify_op.free(hinode, hn))
17281 +                       au_cache_free_hnotify(hn);
17282 +       }
17283 +}
17284 +
17285 +/* ---------------------------------------------------------------------- */
17286 +
17287 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17288 +{
17289 +       if (hinode->hi_notify)
17290 +               au_hnotify_op.ctl(hinode, do_set);
17291 +}
17292 +
17293 +void au_hn_reset(struct inode *inode, unsigned int flags)
17294 +{
17295 +       aufs_bindex_t bindex, bbot;
17296 +       struct inode *hi;
17297 +       struct dentry *iwhdentry;
17298 +
17299 +       bbot = au_ibbot(inode);
17300 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17301 +               hi = au_h_iptr(inode, bindex);
17302 +               if (!hi)
17303 +                       continue;
17304 +
17305 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17306 +               iwhdentry = au_hi_wh(inode, bindex);
17307 +               if (iwhdentry)
17308 +                       dget(iwhdentry);
17309 +               au_igrab(hi);
17310 +               au_set_h_iptr(inode, bindex, NULL, 0);
17311 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17312 +                             flags & ~AuHi_XINO);
17313 +               iput(hi);
17314 +               dput(iwhdentry);
17315 +               /* inode_unlock(hi); */
17316 +       }
17317 +}
17318 +
17319 +/* ---------------------------------------------------------------------- */
17320 +
17321 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17322 +{
17323 +       int err;
17324 +       aufs_bindex_t bindex, bbot, bfound, btop;
17325 +       struct inode *h_i;
17326 +
17327 +       err = 0;
17328 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17329 +               pr_warn("branch root dir was changed\n");
17330 +               goto out;
17331 +       }
17332 +
17333 +       bfound = -1;
17334 +       bbot = au_ibbot(inode);
17335 +       btop = au_ibtop(inode);
17336 +#if 0 /* reserved for future use */
17337 +       if (bindex == bbot) {
17338 +               /* keep this ino in rename case */
17339 +               goto out;
17340 +       }
17341 +#endif
17342 +       for (bindex = btop; bindex <= bbot; bindex++)
17343 +               if (au_h_iptr(inode, bindex) == h_inode) {
17344 +                       bfound = bindex;
17345 +                       break;
17346 +               }
17347 +       if (bfound < 0)
17348 +               goto out;
17349 +
17350 +       for (bindex = btop; bindex <= bbot; bindex++) {
17351 +               h_i = au_h_iptr(inode, bindex);
17352 +               if (!h_i)
17353 +                       continue;
17354 +
17355 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17356 +               /* ignore this error */
17357 +               /* bad action? */
17358 +       }
17359 +
17360 +       /* children inode number will be broken */
17361 +
17362 +out:
17363 +       AuTraceErr(err);
17364 +       return err;
17365 +}
17366 +
17367 +static int hn_gen_tree(struct dentry *dentry)
17368 +{
17369 +       int err, i, j, ndentry;
17370 +       struct au_dcsub_pages dpages;
17371 +       struct au_dpage *dpage;
17372 +       struct dentry **dentries;
17373 +
17374 +       err = au_dpages_init(&dpages, GFP_NOFS);
17375 +       if (unlikely(err))
17376 +               goto out;
17377 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17378 +       if (unlikely(err))
17379 +               goto out_dpages;
17380 +
17381 +       for (i = 0; i < dpages.ndpage; i++) {
17382 +               dpage = dpages.dpages + i;
17383 +               dentries = dpage->dentries;
17384 +               ndentry = dpage->ndentry;
17385 +               for (j = 0; j < ndentry; j++) {
17386 +                       struct dentry *d;
17387 +
17388 +                       d = dentries[j];
17389 +                       if (IS_ROOT(d))
17390 +                               continue;
17391 +
17392 +                       au_digen_dec(d);
17393 +                       if (d_really_is_positive(d))
17394 +                               /* todo: reset children xino?
17395 +                                  cached children only? */
17396 +                               au_iigen_dec(d_inode(d));
17397 +               }
17398 +       }
17399 +
17400 +out_dpages:
17401 +       au_dpages_free(&dpages);
17402 +
17403 +#if 0
17404 +       /* discard children */
17405 +       dentry_unhash(dentry);
17406 +       dput(dentry);
17407 +#endif
17408 +out:
17409 +       return err;
17410 +}
17411 +
17412 +/*
17413 + * return 0 if processed.
17414 + */
17415 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17416 +                          const unsigned int isdir)
17417 +{
17418 +       int err;
17419 +       struct dentry *d;
17420 +       struct qstr *dname;
17421 +
17422 +       err = 1;
17423 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17424 +               pr_warn("branch root dir was changed\n");
17425 +               err = 0;
17426 +               goto out;
17427 +       }
17428 +
17429 +       if (!isdir) {
17430 +               AuDebugOn(!name);
17431 +               au_iigen_dec(inode);
17432 +               spin_lock(&inode->i_lock);
17433 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17434 +                       spin_lock(&d->d_lock);
17435 +                       dname = &d->d_name;
17436 +                       if (dname->len != nlen
17437 +                           && memcmp(dname->name, name, nlen)) {
17438 +                               spin_unlock(&d->d_lock);
17439 +                               continue;
17440 +                       }
17441 +                       err = 0;
17442 +                       au_digen_dec(d);
17443 +                       spin_unlock(&d->d_lock);
17444 +                       break;
17445 +               }
17446 +               spin_unlock(&inode->i_lock);
17447 +       } else {
17448 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17449 +               d = d_find_any_alias(inode);
17450 +               if (!d) {
17451 +                       au_iigen_dec(inode);
17452 +                       goto out;
17453 +               }
17454 +
17455 +               spin_lock(&d->d_lock);
17456 +               dname = &d->d_name;
17457 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17458 +                       spin_unlock(&d->d_lock);
17459 +                       err = hn_gen_tree(d);
17460 +                       spin_lock(&d->d_lock);
17461 +               }
17462 +               spin_unlock(&d->d_lock);
17463 +               dput(d);
17464 +       }
17465 +
17466 +out:
17467 +       AuTraceErr(err);
17468 +       return err;
17469 +}
17470 +
17471 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17472 +{
17473 +       int err;
17474 +
17475 +       if (IS_ROOT(dentry)) {
17476 +               pr_warn("branch root dir was changed\n");
17477 +               return 0;
17478 +       }
17479 +
17480 +       err = 0;
17481 +       if (!isdir) {
17482 +               au_digen_dec(dentry);
17483 +               if (d_really_is_positive(dentry))
17484 +                       au_iigen_dec(d_inode(dentry));
17485 +       } else {
17486 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17487 +               if (d_really_is_positive(dentry))
17488 +                       err = hn_gen_tree(dentry);
17489 +       }
17490 +
17491 +       AuTraceErr(err);
17492 +       return err;
17493 +}
17494 +
17495 +/* ---------------------------------------------------------------------- */
17496 +
17497 +/* hnotify job flags */
17498 +#define AuHnJob_XINO0          1
17499 +#define AuHnJob_GEN            (1 << 1)
17500 +#define AuHnJob_DIRENT         (1 << 2)
17501 +#define AuHnJob_ISDIR          (1 << 3)
17502 +#define AuHnJob_TRYXINO0       (1 << 4)
17503 +#define AuHnJob_MNTPNT         (1 << 5)
17504 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17505 +#define au_fset_hnjob(flags, name) \
17506 +       do { (flags) |= AuHnJob_##name; } while (0)
17507 +#define au_fclr_hnjob(flags, name) \
17508 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17509 +
17510 +enum {
17511 +       AuHn_CHILD,
17512 +       AuHn_PARENT,
17513 +       AuHnLast
17514 +};
17515 +
17516 +struct au_hnotify_args {
17517 +       struct inode *h_dir, *dir, *h_child_inode;
17518 +       u32 mask;
17519 +       unsigned int flags[AuHnLast];
17520 +       unsigned int h_child_nlen;
17521 +       char h_child_name[];
17522 +};
17523 +
17524 +struct hn_job_args {
17525 +       unsigned int flags;
17526 +       struct inode *inode, *h_inode, *dir, *h_dir;
17527 +       struct dentry *dentry;
17528 +       char *h_name;
17529 +       int h_nlen;
17530 +};
17531 +
17532 +static int hn_job(struct hn_job_args *a)
17533 +{
17534 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17535 +       int e;
17536 +
17537 +       /* reset xino */
17538 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17539 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17540 +
17541 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17542 +           && a->inode
17543 +           && a->h_inode) {
17544 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17545 +               if (!a->h_inode->i_nlink
17546 +                   && !(a->h_inode->i_state & I_LINKABLE))
17547 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17548 +               inode_unlock_shared(a->h_inode);
17549 +       }
17550 +
17551 +       /* make the generation obsolete */
17552 +       if (au_ftest_hnjob(a->flags, GEN)) {
17553 +               e = -1;
17554 +               if (a->inode)
17555 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17556 +                                             isdir);
17557 +               if (e && a->dentry)
17558 +                       hn_gen_by_name(a->dentry, isdir);
17559 +               /* ignore this error */
17560 +       }
17561 +
17562 +       /* make dir entries obsolete */
17563 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17564 +               struct au_vdir *vdir;
17565 +
17566 +               vdir = au_ivdir(a->inode);
17567 +               if (vdir)
17568 +                       vdir->vd_jiffy = 0;
17569 +               /* IMustLock(a->inode); */
17570 +               /* inode_inc_iversion(a->inode); */
17571 +       }
17572 +
17573 +       /* can do nothing but warn */
17574 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17575 +           && a->dentry
17576 +           && d_mountpoint(a->dentry))
17577 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17578 +
17579 +       return 0;
17580 +}
17581 +
17582 +/* ---------------------------------------------------------------------- */
17583 +
17584 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17585 +                                          struct inode *dir)
17586 +{
17587 +       struct dentry *dentry, *d, *parent;
17588 +       struct qstr *dname;
17589 +
17590 +       parent = d_find_any_alias(dir);
17591 +       if (!parent)
17592 +               return NULL;
17593 +
17594 +       dentry = NULL;
17595 +       spin_lock(&parent->d_lock);
17596 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17597 +               /* AuDbg("%pd\n", d); */
17598 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17599 +               dname = &d->d_name;
17600 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17601 +                       goto cont_unlock;
17602 +               if (au_di(d))
17603 +                       au_digen_dec(d);
17604 +               else
17605 +                       goto cont_unlock;
17606 +               if (au_dcount(d) > 0) {
17607 +                       dentry = dget_dlock(d);
17608 +                       spin_unlock(&d->d_lock);
17609 +                       break;
17610 +               }
17611 +
17612 +cont_unlock:
17613 +               spin_unlock(&d->d_lock);
17614 +       }
17615 +       spin_unlock(&parent->d_lock);
17616 +       dput(parent);
17617 +
17618 +       if (dentry)
17619 +               di_write_lock_child(dentry);
17620 +
17621 +       return dentry;
17622 +}
17623 +
17624 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17625 +                                        aufs_bindex_t bindex, ino_t h_ino)
17626 +{
17627 +       struct inode *inode;
17628 +       ino_t ino;
17629 +       int err;
17630 +
17631 +       inode = NULL;
17632 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17633 +       if (!err && ino)
17634 +               inode = ilookup(sb, ino);
17635 +       if (!inode)
17636 +               goto out;
17637 +
17638 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17639 +               pr_warn("wrong root branch\n");
17640 +               iput(inode);
17641 +               inode = NULL;
17642 +               goto out;
17643 +       }
17644 +
17645 +       ii_write_lock_child(inode);
17646 +
17647 +out:
17648 +       return inode;
17649 +}
17650 +
17651 +static void au_hn_bh(void *_args)
17652 +{
17653 +       struct au_hnotify_args *a = _args;
17654 +       struct super_block *sb;
17655 +       aufs_bindex_t bindex, bbot, bfound;
17656 +       unsigned char xino, try_iput;
17657 +       int err;
17658 +       struct inode *inode;
17659 +       ino_t h_ino;
17660 +       struct hn_job_args args;
17661 +       struct dentry *dentry;
17662 +       struct au_sbinfo *sbinfo;
17663 +
17664 +       AuDebugOn(!_args);
17665 +       AuDebugOn(!a->h_dir);
17666 +       AuDebugOn(!a->dir);
17667 +       AuDebugOn(!a->mask);
17668 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17669 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17670 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17671 +
17672 +       inode = NULL;
17673 +       dentry = NULL;
17674 +       /*
17675 +        * do not lock a->dir->i_mutex here
17676 +        * because of d_revalidate() may cause a deadlock.
17677 +        */
17678 +       sb = a->dir->i_sb;
17679 +       AuDebugOn(!sb);
17680 +       sbinfo = au_sbi(sb);
17681 +       AuDebugOn(!sbinfo);
17682 +       si_write_lock(sb, AuLock_NOPLMW);
17683 +
17684 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17685 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17686 +               case FS_MOVED_FROM:
17687 +               case FS_MOVED_TO:
17688 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17689 +                               "for the direct rename(2)\n");
17690 +               }
17691 +
17692 +       ii_read_lock_parent(a->dir);
17693 +       bfound = -1;
17694 +       bbot = au_ibbot(a->dir);
17695 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17696 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17697 +                       bfound = bindex;
17698 +                       break;
17699 +               }
17700 +       ii_read_unlock(a->dir);
17701 +       if (unlikely(bfound < 0))
17702 +               goto out;
17703 +
17704 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17705 +       h_ino = 0;
17706 +       if (a->h_child_inode)
17707 +               h_ino = a->h_child_inode->i_ino;
17708 +
17709 +       if (a->h_child_nlen
17710 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17711 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17712 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17713 +                                             a->dir);
17714 +       try_iput = 0;
17715 +       if (dentry && d_really_is_positive(dentry))
17716 +               inode = d_inode(dentry);
17717 +       if (xino && !inode && h_ino
17718 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17719 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17720 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17721 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17722 +               try_iput = 1;
17723 +       }
17724 +
17725 +       args.flags = a->flags[AuHn_CHILD];
17726 +       args.dentry = dentry;
17727 +       args.inode = inode;
17728 +       args.h_inode = a->h_child_inode;
17729 +       args.dir = a->dir;
17730 +       args.h_dir = a->h_dir;
17731 +       args.h_name = a->h_child_name;
17732 +       args.h_nlen = a->h_child_nlen;
17733 +       err = hn_job(&args);
17734 +       if (dentry) {
17735 +               if (au_di(dentry))
17736 +                       di_write_unlock(dentry);
17737 +               dput(dentry);
17738 +       }
17739 +       if (inode && try_iput) {
17740 +               ii_write_unlock(inode);
17741 +               iput(inode);
17742 +       }
17743 +
17744 +       ii_write_lock_parent(a->dir);
17745 +       args.flags = a->flags[AuHn_PARENT];
17746 +       args.dentry = NULL;
17747 +       args.inode = a->dir;
17748 +       args.h_inode = a->h_dir;
17749 +       args.dir = NULL;
17750 +       args.h_dir = NULL;
17751 +       args.h_name = NULL;
17752 +       args.h_nlen = 0;
17753 +       err = hn_job(&args);
17754 +       ii_write_unlock(a->dir);
17755 +
17756 +out:
17757 +       iput(a->h_child_inode);
17758 +       iput(a->h_dir);
17759 +       iput(a->dir);
17760 +       si_write_unlock(sb);
17761 +       au_nwt_done(&sbinfo->si_nowait);
17762 +       kfree(a);
17763 +}
17764 +
17765 +/* ---------------------------------------------------------------------- */
17766 +
17767 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17768 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
17769 +{
17770 +       int err, len;
17771 +       unsigned int flags[AuHnLast], f;
17772 +       unsigned char isdir, isroot, wh;
17773 +       struct inode *dir;
17774 +       struct au_hnotify_args *args;
17775 +       char *p, *h_child_name;
17776 +
17777 +       err = 0;
17778 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17779 +       dir = igrab(hnotify->hn_aufs_inode);
17780 +       if (!dir)
17781 +               goto out;
17782 +
17783 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17784 +       wh = 0;
17785 +       h_child_name = (void *)h_child_qstr->name;
17786 +       len = h_child_qstr->len;
17787 +       if (h_child_name) {
17788 +               if (len > AUFS_WH_PFX_LEN
17789 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17790 +                       h_child_name += AUFS_WH_PFX_LEN;
17791 +                       len -= AUFS_WH_PFX_LEN;
17792 +                       wh = 1;
17793 +               }
17794 +       }
17795 +
17796 +       isdir = 0;
17797 +       if (h_child_inode)
17798 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17799 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17800 +       flags[AuHn_CHILD] = 0;
17801 +       if (isdir)
17802 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17803 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17804 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17805 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17806 +       case FS_MOVED_FROM:
17807 +       case FS_MOVED_TO:
17808 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17809 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17810 +               /*FALLTHROUGH*/
17811 +       case FS_CREATE:
17812 +               AuDebugOn(!h_child_name);
17813 +               break;
17814 +
17815 +       case FS_DELETE:
17816 +               /*
17817 +                * aufs never be able to get this child inode.
17818 +                * revalidation should be in d_revalidate()
17819 +                * by checking i_nlink, i_generation or d_unhashed().
17820 +                */
17821 +               AuDebugOn(!h_child_name);
17822 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17823 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17824 +               break;
17825 +
17826 +       default:
17827 +               AuDebugOn(1);
17828 +       }
17829 +
17830 +       if (wh)
17831 +               h_child_inode = NULL;
17832 +
17833 +       err = -ENOMEM;
17834 +       /* iput() and kfree() will be called in au_hnotify() */
17835 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17836 +       if (unlikely(!args)) {
17837 +               AuErr1("no memory\n");
17838 +               iput(dir);
17839 +               goto out;
17840 +       }
17841 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17842 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17843 +       args->mask = mask;
17844 +       args->dir = dir;
17845 +       args->h_dir = igrab(h_dir);
17846 +       if (h_child_inode)
17847 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17848 +       args->h_child_inode = h_child_inode;
17849 +       args->h_child_nlen = len;
17850 +       if (len) {
17851 +               p = (void *)args;
17852 +               p += sizeof(*args);
17853 +               memcpy(p, h_child_name, len);
17854 +               p[len] = 0;
17855 +       }
17856 +
17857 +       /* NFS fires the event for silly-renamed one from kworker */
17858 +       f = 0;
17859 +       if (!dir->i_nlink
17860 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17861 +               f = AuWkq_NEST;
17862 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17863 +       if (unlikely(err)) {
17864 +               pr_err("wkq %d\n", err);
17865 +               iput(args->h_child_inode);
17866 +               iput(args->h_dir);
17867 +               iput(args->dir);
17868 +               kfree(args);
17869 +       }
17870 +
17871 +out:
17872 +       return err;
17873 +}
17874 +
17875 +/* ---------------------------------------------------------------------- */
17876 +
17877 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17878 +{
17879 +       int err;
17880 +
17881 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17882 +
17883 +       err = 0;
17884 +       if (au_hnotify_op.reset_br)
17885 +               err = au_hnotify_op.reset_br(udba, br, perm);
17886 +
17887 +       return err;
17888 +}
17889 +
17890 +int au_hnotify_init_br(struct au_branch *br, int perm)
17891 +{
17892 +       int err;
17893 +
17894 +       err = 0;
17895 +       if (au_hnotify_op.init_br)
17896 +               err = au_hnotify_op.init_br(br, perm);
17897 +
17898 +       return err;
17899 +}
17900 +
17901 +void au_hnotify_fin_br(struct au_branch *br)
17902 +{
17903 +       if (au_hnotify_op.fin_br)
17904 +               au_hnotify_op.fin_br(br);
17905 +}
17906 +
17907 +static void au_hn_destroy_cache(void)
17908 +{
17909 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17910 +       au_cache[AuCache_HNOTIFY] = NULL;
17911 +}
17912 +
17913 +int __init au_hnotify_init(void)
17914 +{
17915 +       int err;
17916 +
17917 +       err = -ENOMEM;
17918 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17919 +       if (au_cache[AuCache_HNOTIFY]) {
17920 +               err = 0;
17921 +               if (au_hnotify_op.init)
17922 +                       err = au_hnotify_op.init();
17923 +               if (unlikely(err))
17924 +                       au_hn_destroy_cache();
17925 +       }
17926 +       AuTraceErr(err);
17927 +       return err;
17928 +}
17929 +
17930 +void au_hnotify_fin(void)
17931 +{
17932 +       if (au_hnotify_op.fin)
17933 +               au_hnotify_op.fin();
17934 +
17935 +       /* cf. au_cache_fin() */
17936 +       if (au_cache[AuCache_HNOTIFY])
17937 +               au_hn_destroy_cache();
17938 +}
17939 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17940 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17941 +++ linux/fs/aufs/iinfo.c       2018-07-19 09:46:13.059981241 +0200
17942 @@ -0,0 +1,286 @@
17943 +/* SPDX-License-Identifier: GPL-2.0 */
17944 +/*
17945 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17946 + *
17947 + * This program, aufs is free software; you can redistribute it and/or modify
17948 + * it under the terms of the GNU General Public License as published by
17949 + * the Free Software Foundation; either version 2 of the License, or
17950 + * (at your option) any later version.
17951 + *
17952 + * This program is distributed in the hope that it will be useful,
17953 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17954 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17955 + * GNU General Public License for more details.
17956 + *
17957 + * You should have received a copy of the GNU General Public License
17958 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17959 + */
17960 +
17961 +/*
17962 + * inode private data
17963 + */
17964 +
17965 +#include "aufs.h"
17966 +
17967 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17968 +{
17969 +       struct inode *h_inode;
17970 +       struct au_hinode *hinode;
17971 +
17972 +       IiMustAnyLock(inode);
17973 +
17974 +       hinode = au_hinode(au_ii(inode), bindex);
17975 +       h_inode = hinode->hi_inode;
17976 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17977 +       return h_inode;
17978 +}
17979 +
17980 +/* todo: hard/soft set? */
17981 +void au_hiput(struct au_hinode *hinode)
17982 +{
17983 +       au_hn_free(hinode);
17984 +       dput(hinode->hi_whdentry);
17985 +       iput(hinode->hi_inode);
17986 +}
17987 +
17988 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17989 +{
17990 +       unsigned int flags;
17991 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17992 +
17993 +       flags = 0;
17994 +       if (au_opt_test(mnt_flags, XINO))
17995 +               au_fset_hi(flags, XINO);
17996 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17997 +               au_fset_hi(flags, HNOTIFY);
17998 +       return flags;
17999 +}
18000 +
18001 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18002 +                  struct inode *h_inode, unsigned int flags)
18003 +{
18004 +       struct au_hinode *hinode;
18005 +       struct inode *hi;
18006 +       struct au_iinfo *iinfo = au_ii(inode);
18007 +
18008 +       IiMustWriteLock(inode);
18009 +
18010 +       hinode = au_hinode(iinfo, bindex);
18011 +       hi = hinode->hi_inode;
18012 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18013 +
18014 +       if (hi)
18015 +               au_hiput(hinode);
18016 +       hinode->hi_inode = h_inode;
18017 +       if (h_inode) {
18018 +               int err;
18019 +               struct super_block *sb = inode->i_sb;
18020 +               struct au_branch *br;
18021 +
18022 +               AuDebugOn(inode->i_mode
18023 +                         && (h_inode->i_mode & S_IFMT)
18024 +                         != (inode->i_mode & S_IFMT));
18025 +               if (bindex == iinfo->ii_btop)
18026 +                       au_cpup_igen(inode, h_inode);
18027 +               br = au_sbr(sb, bindex);
18028 +               hinode->hi_id = br->br_id;
18029 +               if (au_ftest_hi(flags, XINO)) {
18030 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
18031 +                                           inode->i_ino);
18032 +                       if (unlikely(err))
18033 +                               AuIOErr1("failed au_xino_write() %d\n", err);
18034 +               }
18035 +
18036 +               if (au_ftest_hi(flags, HNOTIFY)
18037 +                   && au_br_hnotifyable(br->br_perm)) {
18038 +                       err = au_hn_alloc(hinode, inode);
18039 +                       if (unlikely(err))
18040 +                               AuIOErr1("au_hn_alloc() %d\n", err);
18041 +               }
18042 +       }
18043 +}
18044 +
18045 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18046 +                 struct dentry *h_wh)
18047 +{
18048 +       struct au_hinode *hinode;
18049 +
18050 +       IiMustWriteLock(inode);
18051 +
18052 +       hinode = au_hinode(au_ii(inode), bindex);
18053 +       AuDebugOn(hinode->hi_whdentry);
18054 +       hinode->hi_whdentry = h_wh;
18055 +}
18056 +
18057 +void au_update_iigen(struct inode *inode, int half)
18058 +{
18059 +       struct au_iinfo *iinfo;
18060 +       struct au_iigen *iigen;
18061 +       unsigned int sigen;
18062 +
18063 +       sigen = au_sigen(inode->i_sb);
18064 +       iinfo = au_ii(inode);
18065 +       iigen = &iinfo->ii_generation;
18066 +       spin_lock(&iigen->ig_spin);
18067 +       iigen->ig_generation = sigen;
18068 +       if (half)
18069 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18070 +       else
18071 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18072 +       spin_unlock(&iigen->ig_spin);
18073 +}
18074 +
18075 +/* it may be called at remount time, too */
18076 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18077 +{
18078 +       struct au_iinfo *iinfo;
18079 +       aufs_bindex_t bindex, bbot;
18080 +
18081 +       AuDebugOn(au_is_bad_inode(inode));
18082 +       IiMustWriteLock(inode);
18083 +
18084 +       iinfo = au_ii(inode);
18085 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18086 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18087 +                    bindex++) {
18088 +                       struct inode *h_i;
18089 +
18090 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18091 +                       if (h_i
18092 +                           && !h_i->i_nlink
18093 +                           && !(h_i->i_state & I_LINKABLE))
18094 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18095 +               }
18096 +       }
18097 +
18098 +       iinfo->ii_btop = -1;
18099 +       iinfo->ii_bbot = -1;
18100 +       bbot = au_sbbot(inode->i_sb);
18101 +       for (bindex = 0; bindex <= bbot; bindex++)
18102 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18103 +                       iinfo->ii_btop = bindex;
18104 +                       break;
18105 +               }
18106 +       if (iinfo->ii_btop >= 0)
18107 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18108 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18109 +                               iinfo->ii_bbot = bindex;
18110 +                               break;
18111 +                       }
18112 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18113 +}
18114 +
18115 +/* ---------------------------------------------------------------------- */
18116 +
18117 +void au_icntnr_init_once(void *_c)
18118 +{
18119 +       struct au_icntnr *c = _c;
18120 +       struct au_iinfo *iinfo = &c->iinfo;
18121 +
18122 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18123 +       au_rw_init(&iinfo->ii_rwsem);
18124 +       inode_init_once(&c->vfs_inode);
18125 +}
18126 +
18127 +void au_hinode_init(struct au_hinode *hinode)
18128 +{
18129 +       hinode->hi_inode = NULL;
18130 +       hinode->hi_id = -1;
18131 +       au_hn_init(hinode);
18132 +       hinode->hi_whdentry = NULL;
18133 +}
18134 +
18135 +int au_iinfo_init(struct inode *inode)
18136 +{
18137 +       struct au_iinfo *iinfo;
18138 +       struct super_block *sb;
18139 +       struct au_hinode *hi;
18140 +       int nbr, i;
18141 +
18142 +       sb = inode->i_sb;
18143 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18144 +       nbr = au_sbbot(sb) + 1;
18145 +       if (unlikely(nbr <= 0))
18146 +               nbr = 1;
18147 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18148 +       if (hi) {
18149 +               au_ninodes_inc(sb);
18150 +
18151 +               iinfo->ii_hinode = hi;
18152 +               for (i = 0; i < nbr; i++, hi++)
18153 +                       au_hinode_init(hi);
18154 +
18155 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18156 +               iinfo->ii_btop = -1;
18157 +               iinfo->ii_bbot = -1;
18158 +               iinfo->ii_vdir = NULL;
18159 +               return 0;
18160 +       }
18161 +       return -ENOMEM;
18162 +}
18163 +
18164 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18165 +{
18166 +       int err, i;
18167 +       struct au_hinode *hip;
18168 +
18169 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18170 +
18171 +       err = -ENOMEM;
18172 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18173 +                         may_shrink);
18174 +       if (hip) {
18175 +               iinfo->ii_hinode = hip;
18176 +               i = iinfo->ii_bbot + 1;
18177 +               hip += i;
18178 +               for (; i < nbr; i++, hip++)
18179 +                       au_hinode_init(hip);
18180 +               err = 0;
18181 +       }
18182 +
18183 +       return err;
18184 +}
18185 +
18186 +void au_iinfo_fin(struct inode *inode)
18187 +{
18188 +       struct au_iinfo *iinfo;
18189 +       struct au_hinode *hi;
18190 +       struct super_block *sb;
18191 +       aufs_bindex_t bindex, bbot;
18192 +       const unsigned char unlinked = !inode->i_nlink;
18193 +
18194 +       AuDebugOn(au_is_bad_inode(inode));
18195 +
18196 +       sb = inode->i_sb;
18197 +       au_ninodes_dec(sb);
18198 +       if (si_pid_test(sb))
18199 +               au_xino_delete_inode(inode, unlinked);
18200 +       else {
18201 +               /*
18202 +                * it is safe to hide the dependency between sbinfo and
18203 +                * sb->s_umount.
18204 +                */
18205 +               lockdep_off();
18206 +               si_noflush_read_lock(sb);
18207 +               au_xino_delete_inode(inode, unlinked);
18208 +               si_read_unlock(sb);
18209 +               lockdep_on();
18210 +       }
18211 +
18212 +       iinfo = au_ii(inode);
18213 +       if (iinfo->ii_vdir)
18214 +               au_vdir_free(iinfo->ii_vdir);
18215 +
18216 +       bindex = iinfo->ii_btop;
18217 +       if (bindex >= 0) {
18218 +               hi = au_hinode(iinfo, bindex);
18219 +               bbot = iinfo->ii_bbot;
18220 +               while (bindex++ <= bbot) {
18221 +                       if (hi->hi_inode)
18222 +                               au_hiput(hi);
18223 +                       hi++;
18224 +               }
18225 +       }
18226 +       kfree(iinfo->ii_hinode);
18227 +       AuRwDestroy(&iinfo->ii_rwsem);
18228 +}
18229 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18230 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18231 +++ linux/fs/aufs/inode.c       2018-07-19 09:46:13.059981241 +0200
18232 @@ -0,0 +1,528 @@
18233 +/* SPDX-License-Identifier: GPL-2.0 */
18234 +/*
18235 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18236 + *
18237 + * This program, aufs is free software; you can redistribute it and/or modify
18238 + * it under the terms of the GNU General Public License as published by
18239 + * the Free Software Foundation; either version 2 of the License, or
18240 + * (at your option) any later version.
18241 + *
18242 + * This program is distributed in the hope that it will be useful,
18243 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18244 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18245 + * GNU General Public License for more details.
18246 + *
18247 + * You should have received a copy of the GNU General Public License
18248 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18249 + */
18250 +
18251 +/*
18252 + * inode functions
18253 + */
18254 +
18255 +#include "aufs.h"
18256 +
18257 +struct inode *au_igrab(struct inode *inode)
18258 +{
18259 +       if (inode) {
18260 +               AuDebugOn(!atomic_read(&inode->i_count));
18261 +               ihold(inode);
18262 +       }
18263 +       return inode;
18264 +}
18265 +
18266 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18267 +{
18268 +       au_cpup_attr_all(inode, /*force*/0);
18269 +       au_update_iigen(inode, /*half*/1);
18270 +       if (do_version)
18271 +               inode_inc_iversion(inode);
18272 +}
18273 +
18274 +static int au_ii_refresh(struct inode *inode, int *update)
18275 +{
18276 +       int err, e, nbr;
18277 +       umode_t type;
18278 +       aufs_bindex_t bindex, new_bindex;
18279 +       struct super_block *sb;
18280 +       struct au_iinfo *iinfo;
18281 +       struct au_hinode *p, *q, tmp;
18282 +
18283 +       AuDebugOn(au_is_bad_inode(inode));
18284 +       IiMustWriteLock(inode);
18285 +
18286 +       *update = 0;
18287 +       sb = inode->i_sb;
18288 +       nbr = au_sbbot(sb) + 1;
18289 +       type = inode->i_mode & S_IFMT;
18290 +       iinfo = au_ii(inode);
18291 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18292 +       if (unlikely(err))
18293 +               goto out;
18294 +
18295 +       AuDebugOn(iinfo->ii_btop < 0);
18296 +       p = au_hinode(iinfo, iinfo->ii_btop);
18297 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18298 +            bindex++, p++) {
18299 +               if (!p->hi_inode)
18300 +                       continue;
18301 +
18302 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18303 +               new_bindex = au_br_index(sb, p->hi_id);
18304 +               if (new_bindex == bindex)
18305 +                       continue;
18306 +
18307 +               if (new_bindex < 0) {
18308 +                       *update = 1;
18309 +                       au_hiput(p);
18310 +                       p->hi_inode = NULL;
18311 +                       continue;
18312 +               }
18313 +
18314 +               if (new_bindex < iinfo->ii_btop)
18315 +                       iinfo->ii_btop = new_bindex;
18316 +               if (iinfo->ii_bbot < new_bindex)
18317 +                       iinfo->ii_bbot = new_bindex;
18318 +               /* swap two lower inode, and loop again */
18319 +               q = au_hinode(iinfo, new_bindex);
18320 +               tmp = *q;
18321 +               *q = *p;
18322 +               *p = tmp;
18323 +               if (tmp.hi_inode) {
18324 +                       bindex--;
18325 +                       p--;
18326 +               }
18327 +       }
18328 +       au_update_ibrange(inode, /*do_put_zero*/0);
18329 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18330 +       e = au_dy_irefresh(inode);
18331 +       if (unlikely(e && !err))
18332 +               err = e;
18333 +
18334 +out:
18335 +       AuTraceErr(err);
18336 +       return err;
18337 +}
18338 +
18339 +void au_refresh_iop(struct inode *inode, int force_getattr)
18340 +{
18341 +       int type;
18342 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18343 +       const struct inode_operations *iop
18344 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18345 +
18346 +       if (inode->i_op == iop)
18347 +               return;
18348 +
18349 +       switch (inode->i_mode & S_IFMT) {
18350 +       case S_IFDIR:
18351 +               type = AuIop_DIR;
18352 +               break;
18353 +       case S_IFLNK:
18354 +               type = AuIop_SYMLINK;
18355 +               break;
18356 +       default:
18357 +               type = AuIop_OTHER;
18358 +               break;
18359 +       }
18360 +
18361 +       inode->i_op = iop + type;
18362 +       /* unnecessary smp_wmb() */
18363 +}
18364 +
18365 +int au_refresh_hinode_self(struct inode *inode)
18366 +{
18367 +       int err, update;
18368 +
18369 +       err = au_ii_refresh(inode, &update);
18370 +       if (!err)
18371 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18372 +
18373 +       AuTraceErr(err);
18374 +       return err;
18375 +}
18376 +
18377 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18378 +{
18379 +       int err, e, update;
18380 +       unsigned int flags;
18381 +       umode_t mode;
18382 +       aufs_bindex_t bindex, bbot;
18383 +       unsigned char isdir;
18384 +       struct au_hinode *p;
18385 +       struct au_iinfo *iinfo;
18386 +
18387 +       err = au_ii_refresh(inode, &update);
18388 +       if (unlikely(err))
18389 +               goto out;
18390 +
18391 +       update = 0;
18392 +       iinfo = au_ii(inode);
18393 +       p = au_hinode(iinfo, iinfo->ii_btop);
18394 +       mode = (inode->i_mode & S_IFMT);
18395 +       isdir = S_ISDIR(mode);
18396 +       flags = au_hi_flags(inode, isdir);
18397 +       bbot = au_dbbot(dentry);
18398 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18399 +               struct inode *h_i, *h_inode;
18400 +               struct dentry *h_d;
18401 +
18402 +               h_d = au_h_dptr(dentry, bindex);
18403 +               if (!h_d || d_is_negative(h_d))
18404 +                       continue;
18405 +
18406 +               h_inode = d_inode(h_d);
18407 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18408 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18409 +                       h_i = au_h_iptr(inode, bindex);
18410 +                       if (h_i) {
18411 +                               if (h_i == h_inode)
18412 +                                       continue;
18413 +                               err = -EIO;
18414 +                               break;
18415 +                       }
18416 +               }
18417 +               if (bindex < iinfo->ii_btop)
18418 +                       iinfo->ii_btop = bindex;
18419 +               if (iinfo->ii_bbot < bindex)
18420 +                       iinfo->ii_bbot = bindex;
18421 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18422 +               update = 1;
18423 +       }
18424 +       au_update_ibrange(inode, /*do_put_zero*/0);
18425 +       e = au_dy_irefresh(inode);
18426 +       if (unlikely(e && !err))
18427 +               err = e;
18428 +       if (!err)
18429 +               au_refresh_hinode_attr(inode, update && isdir);
18430 +
18431 +out:
18432 +       AuTraceErr(err);
18433 +       return err;
18434 +}
18435 +
18436 +static int set_inode(struct inode *inode, struct dentry *dentry)
18437 +{
18438 +       int err;
18439 +       unsigned int flags;
18440 +       umode_t mode;
18441 +       aufs_bindex_t bindex, btop, btail;
18442 +       unsigned char isdir;
18443 +       struct dentry *h_dentry;
18444 +       struct inode *h_inode;
18445 +       struct au_iinfo *iinfo;
18446 +       struct inode_operations *iop;
18447 +
18448 +       IiMustWriteLock(inode);
18449 +
18450 +       err = 0;
18451 +       isdir = 0;
18452 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18453 +       btop = au_dbtop(dentry);
18454 +       h_dentry = au_h_dptr(dentry, btop);
18455 +       h_inode = d_inode(h_dentry);
18456 +       mode = h_inode->i_mode;
18457 +       switch (mode & S_IFMT) {
18458 +       case S_IFREG:
18459 +               btail = au_dbtail(dentry);
18460 +               inode->i_op = iop + AuIop_OTHER;
18461 +               inode->i_fop = &aufs_file_fop;
18462 +               err = au_dy_iaop(inode, btop, h_inode);
18463 +               if (unlikely(err))
18464 +                       goto out;
18465 +               break;
18466 +       case S_IFDIR:
18467 +               isdir = 1;
18468 +               btail = au_dbtaildir(dentry);
18469 +               inode->i_op = iop + AuIop_DIR;
18470 +               inode->i_fop = &aufs_dir_fop;
18471 +               break;
18472 +       case S_IFLNK:
18473 +               btail = au_dbtail(dentry);
18474 +               inode->i_op = iop + AuIop_SYMLINK;
18475 +               break;
18476 +       case S_IFBLK:
18477 +       case S_IFCHR:
18478 +       case S_IFIFO:
18479 +       case S_IFSOCK:
18480 +               btail = au_dbtail(dentry);
18481 +               inode->i_op = iop + AuIop_OTHER;
18482 +               init_special_inode(inode, mode, h_inode->i_rdev);
18483 +               break;
18484 +       default:
18485 +               AuIOErr("Unknown file type 0%o\n", mode);
18486 +               err = -EIO;
18487 +               goto out;
18488 +       }
18489 +
18490 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18491 +       flags = au_hi_flags(inode, isdir);
18492 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18493 +           && au_ftest_hi(flags, HNOTIFY)
18494 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18495 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18496 +               au_fclr_hi(flags, HNOTIFY);
18497 +       iinfo = au_ii(inode);
18498 +       iinfo->ii_btop = btop;
18499 +       iinfo->ii_bbot = btail;
18500 +       for (bindex = btop; bindex <= btail; bindex++) {
18501 +               h_dentry = au_h_dptr(dentry, bindex);
18502 +               if (h_dentry)
18503 +                       au_set_h_iptr(inode, bindex,
18504 +                                     au_igrab(d_inode(h_dentry)), flags);
18505 +       }
18506 +       au_cpup_attr_all(inode, /*force*/1);
18507 +       /*
18508 +        * to force calling aufs_get_acl() every time,
18509 +        * do not call cache_no_acl() for aufs inode.
18510 +        */
18511 +
18512 +out:
18513 +       return err;
18514 +}
18515 +
18516 +/*
18517 + * successful returns with iinfo write_locked
18518 + * minus: errno
18519 + * zero: success, matched
18520 + * plus: no error, but unmatched
18521 + */
18522 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18523 +{
18524 +       int err;
18525 +       unsigned int gen, igflags;
18526 +       aufs_bindex_t bindex, bbot;
18527 +       struct inode *h_inode, *h_dinode;
18528 +       struct dentry *h_dentry;
18529 +
18530 +       /*
18531 +        * before this function, if aufs got any iinfo lock, it must be only
18532 +        * one, the parent dir.
18533 +        * it can happen by UDBA and the obsoleted inode number.
18534 +        */
18535 +       err = -EIO;
18536 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18537 +               goto out;
18538 +
18539 +       err = 1;
18540 +       ii_write_lock_new_child(inode);
18541 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18542 +       h_dinode = d_inode(h_dentry);
18543 +       bbot = au_ibbot(inode);
18544 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18545 +               h_inode = au_h_iptr(inode, bindex);
18546 +               if (!h_inode || h_inode != h_dinode)
18547 +                       continue;
18548 +
18549 +               err = 0;
18550 +               gen = au_iigen(inode, &igflags);
18551 +               if (gen == au_digen(dentry)
18552 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18553 +                       break;
18554 +
18555 +               /* fully refresh inode using dentry */
18556 +               err = au_refresh_hinode(inode, dentry);
18557 +               if (!err)
18558 +                       au_update_iigen(inode, /*half*/0);
18559 +               break;
18560 +       }
18561 +
18562 +       if (unlikely(err))
18563 +               ii_write_unlock(inode);
18564 +out:
18565 +       return err;
18566 +}
18567 +
18568 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18569 +          unsigned int d_type, ino_t *ino)
18570 +{
18571 +       int err, idx;
18572 +       const int isnondir = d_type != DT_DIR;
18573 +
18574 +       /* prevent hardlinked inode number from race condition */
18575 +       if (isnondir) {
18576 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18577 +               if (unlikely(err))
18578 +                       goto out;
18579 +       }
18580 +
18581 +       err = au_xino_read(sb, bindex, h_ino, ino);
18582 +       if (unlikely(err))
18583 +               goto out_xinondir;
18584 +
18585 +       if (!*ino) {
18586 +               err = -EIO;
18587 +               *ino = au_xino_new_ino(sb);
18588 +               if (unlikely(!*ino))
18589 +                       goto out_xinondir;
18590 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18591 +               if (unlikely(err))
18592 +                       goto out_xinondir;
18593 +       }
18594 +
18595 +out_xinondir:
18596 +       if (isnondir && idx >= 0)
18597 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18598 +out:
18599 +       return err;
18600 +}
18601 +
18602 +/* successful returns with iinfo write_locked */
18603 +/* todo: return with unlocked? */
18604 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18605 +{
18606 +       struct inode *inode, *h_inode;
18607 +       struct dentry *h_dentry;
18608 +       struct super_block *sb;
18609 +       ino_t h_ino, ino;
18610 +       int err, idx, hlinked;
18611 +       aufs_bindex_t btop;
18612 +
18613 +       sb = dentry->d_sb;
18614 +       btop = au_dbtop(dentry);
18615 +       h_dentry = au_h_dptr(dentry, btop);
18616 +       h_inode = d_inode(h_dentry);
18617 +       h_ino = h_inode->i_ino;
18618 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18619 +
18620 +new_ino:
18621 +       /*
18622 +        * stop 'race'-ing between hardlinks under different
18623 +        * parents.
18624 +        */
18625 +       if (hlinked) {
18626 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18627 +               inode = ERR_PTR(err);
18628 +               if (unlikely(err))
18629 +                       goto out;
18630 +       }
18631 +
18632 +       err = au_xino_read(sb, btop, h_ino, &ino);
18633 +       inode = ERR_PTR(err);
18634 +       if (unlikely(err))
18635 +               goto out_xinondir;
18636 +
18637 +       if (!ino) {
18638 +               ino = au_xino_new_ino(sb);
18639 +               if (unlikely(!ino)) {
18640 +                       inode = ERR_PTR(-EIO);
18641 +                       goto out_xinondir;
18642 +               }
18643 +       }
18644 +
18645 +       AuDbg("i%lu\n", (unsigned long)ino);
18646 +       inode = au_iget_locked(sb, ino);
18647 +       err = PTR_ERR(inode);
18648 +       if (IS_ERR(inode))
18649 +               goto out_xinondir;
18650 +
18651 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18652 +       if (inode->i_state & I_NEW) {
18653 +               ii_write_lock_new_child(inode);
18654 +               err = set_inode(inode, dentry);
18655 +               if (!err) {
18656 +                       unlock_new_inode(inode);
18657 +                       goto out_xinondir; /* success */
18658 +               }
18659 +
18660 +               /*
18661 +                * iget_failed() calls iput(), but we need to call
18662 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18663 +                * i_count.
18664 +                */
18665 +               atomic_inc(&inode->i_count);
18666 +               iget_failed(inode);
18667 +               ii_write_unlock(inode);
18668 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18669 +               /* ignore this error */
18670 +               goto out_iput;
18671 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18672 +               /*
18673 +                * horrible race condition between lookup, readdir and copyup
18674 +                * (or something).
18675 +                */
18676 +               if (hlinked && idx >= 0)
18677 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18678 +               err = reval_inode(inode, dentry);
18679 +               if (unlikely(err < 0)) {
18680 +                       hlinked = 0;
18681 +                       goto out_iput;
18682 +               }
18683 +               if (!err)
18684 +                       goto out; /* success */
18685 +               else if (hlinked && idx >= 0) {
18686 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18687 +                       if (unlikely(err)) {
18688 +                               iput(inode);
18689 +                               inode = ERR_PTR(err);
18690 +                               goto out;
18691 +                       }
18692 +               }
18693 +       }
18694 +
18695 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18696 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18697 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18698 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18699 +                       (unsigned long)h_ino, (unsigned long)ino);
18700 +       ino = 0;
18701 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18702 +       if (!err) {
18703 +               iput(inode);
18704 +               if (hlinked && idx >= 0)
18705 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18706 +               goto new_ino;
18707 +       }
18708 +
18709 +out_iput:
18710 +       iput(inode);
18711 +       inode = ERR_PTR(err);
18712 +out_xinondir:
18713 +       if (hlinked && idx >= 0)
18714 +               au_xinondir_leave(sb, btop, h_ino, idx);
18715 +out:
18716 +       return inode;
18717 +}
18718 +
18719 +/* ---------------------------------------------------------------------- */
18720 +
18721 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18722 +              struct inode *inode)
18723 +{
18724 +       int err;
18725 +       struct inode *hi;
18726 +
18727 +       err = au_br_rdonly(au_sbr(sb, bindex));
18728 +
18729 +       /* pseudo-link after flushed may happen out of bounds */
18730 +       if (!err
18731 +           && inode
18732 +           && au_ibtop(inode) <= bindex
18733 +           && bindex <= au_ibbot(inode)) {
18734 +               /*
18735 +                * permission check is unnecessary since vfsub routine
18736 +                * will be called later
18737 +                */
18738 +               hi = au_h_iptr(inode, bindex);
18739 +               if (hi)
18740 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18741 +       }
18742 +
18743 +       return err;
18744 +}
18745 +
18746 +int au_test_h_perm(struct inode *h_inode, int mask)
18747 +{
18748 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18749 +               return 0;
18750 +       return inode_permission(h_inode, mask);
18751 +}
18752 +
18753 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18754 +{
18755 +       if (au_test_nfs(h_inode->i_sb)
18756 +           && (mask & MAY_WRITE)
18757 +           && S_ISDIR(h_inode->i_mode))
18758 +               mask |= MAY_READ; /* force permission check */
18759 +       return au_test_h_perm(h_inode, mask);
18760 +}
18761 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18762 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18763 +++ linux/fs/aufs/inode.h       2018-07-19 09:46:13.059981241 +0200
18764 @@ -0,0 +1,696 @@
18765 +/* SPDX-License-Identifier: GPL-2.0 */
18766 +/*
18767 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18768 + *
18769 + * This program, aufs is free software; you can redistribute it and/or modify
18770 + * it under the terms of the GNU General Public License as published by
18771 + * the Free Software Foundation; either version 2 of the License, or
18772 + * (at your option) any later version.
18773 + *
18774 + * This program is distributed in the hope that it will be useful,
18775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18777 + * GNU General Public License for more details.
18778 + *
18779 + * You should have received a copy of the GNU General Public License
18780 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18781 + */
18782 +
18783 +/*
18784 + * inode operations
18785 + */
18786 +
18787 +#ifndef __AUFS_INODE_H__
18788 +#define __AUFS_INODE_H__
18789 +
18790 +#ifdef __KERNEL__
18791 +
18792 +#include <linux/fsnotify.h>
18793 +#include "rwsem.h"
18794 +
18795 +struct vfsmount;
18796 +
18797 +struct au_hnotify {
18798 +#ifdef CONFIG_AUFS_HNOTIFY
18799 +#ifdef CONFIG_AUFS_HFSNOTIFY
18800 +       /* never use fsnotify_add_vfsmount_mark() */
18801 +       struct fsnotify_mark            hn_mark;
18802 +#endif
18803 +       struct inode            *hn_aufs_inode; /* no get/put */
18804 +#endif
18805 +} ____cacheline_aligned_in_smp;
18806 +
18807 +struct au_hinode {
18808 +       struct inode            *hi_inode;
18809 +       aufs_bindex_t           hi_id;
18810 +#ifdef CONFIG_AUFS_HNOTIFY
18811 +       struct au_hnotify       *hi_notify;
18812 +#endif
18813 +
18814 +       /* reference to the copied-up whiteout with get/put */
18815 +       struct dentry           *hi_whdentry;
18816 +};
18817 +
18818 +/* ig_flags */
18819 +#define AuIG_HALF_REFRESHED            1
18820 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18821 +#define au_ig_fset(flags, name) \
18822 +       do { (flags) |= AuIG_##name; } while (0)
18823 +#define au_ig_fclr(flags, name) \
18824 +       do { (flags) &= ~AuIG_##name; } while (0)
18825 +
18826 +struct au_iigen {
18827 +       spinlock_t      ig_spin;
18828 +       __u32           ig_generation, ig_flags;
18829 +};
18830 +
18831 +struct au_vdir;
18832 +struct au_iinfo {
18833 +       struct au_iigen         ii_generation;
18834 +       struct super_block      *ii_hsb1;       /* no get/put */
18835 +
18836 +       struct au_rwsem         ii_rwsem;
18837 +       aufs_bindex_t           ii_btop, ii_bbot;
18838 +       __u32                   ii_higen;
18839 +       struct au_hinode        *ii_hinode;
18840 +       struct au_vdir          *ii_vdir;
18841 +};
18842 +
18843 +struct au_icntnr {
18844 +       struct au_iinfo iinfo;
18845 +       struct inode vfs_inode;
18846 +       struct hlist_bl_node plink;
18847 +} ____cacheline_aligned_in_smp;
18848 +
18849 +/* au_pin flags */
18850 +#define AuPin_DI_LOCKED                1
18851 +#define AuPin_MNT_WRITE                (1 << 1)
18852 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18853 +#define au_fset_pin(flags, name) \
18854 +       do { (flags) |= AuPin_##name; } while (0)
18855 +#define au_fclr_pin(flags, name) \
18856 +       do { (flags) &= ~AuPin_##name; } while (0)
18857 +
18858 +struct au_pin {
18859 +       /* input */
18860 +       struct dentry *dentry;
18861 +       unsigned int udba;
18862 +       unsigned char lsc_di, lsc_hi, flags;
18863 +       aufs_bindex_t bindex;
18864 +
18865 +       /* output */
18866 +       struct dentry *parent;
18867 +       struct au_hinode *hdir;
18868 +       struct vfsmount *h_mnt;
18869 +
18870 +       /* temporary unlock/relock for copyup */
18871 +       struct dentry *h_dentry, *h_parent;
18872 +       struct au_branch *br;
18873 +       struct task_struct *task;
18874 +};
18875 +
18876 +void au_pin_hdir_unlock(struct au_pin *p);
18877 +int au_pin_hdir_lock(struct au_pin *p);
18878 +int au_pin_hdir_relock(struct au_pin *p);
18879 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18880 +void au_pin_hdir_release(struct au_pin *p);
18881 +
18882 +/* ---------------------------------------------------------------------- */
18883 +
18884 +static inline struct au_iinfo *au_ii(struct inode *inode)
18885 +{
18886 +       BUG_ON(is_bad_inode(inode));
18887 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18888 +}
18889 +
18890 +/* ---------------------------------------------------------------------- */
18891 +
18892 +/* inode.c */
18893 +struct inode *au_igrab(struct inode *inode);
18894 +void au_refresh_iop(struct inode *inode, int force_getattr);
18895 +int au_refresh_hinode_self(struct inode *inode);
18896 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18897 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18898 +          unsigned int d_type, ino_t *ino);
18899 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18900 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18901 +              struct inode *inode);
18902 +int au_test_h_perm(struct inode *h_inode, int mask);
18903 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18904 +
18905 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18906 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18907 +{
18908 +#ifdef CONFIG_AUFS_SHWH
18909 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18910 +#else
18911 +       return 0;
18912 +#endif
18913 +}
18914 +
18915 +/* i_op.c */
18916 +enum {
18917 +       AuIop_SYMLINK,
18918 +       AuIop_DIR,
18919 +       AuIop_OTHER,
18920 +       AuIop_Last
18921 +};
18922 +extern struct inode_operations aufs_iop[AuIop_Last],
18923 +       aufs_iop_nogetattr[AuIop_Last];
18924 +
18925 +/* au_wr_dir flags */
18926 +#define AuWrDir_ADD_ENTRY      1
18927 +#define AuWrDir_ISDIR          (1 << 1)
18928 +#define AuWrDir_TMPFILE                (1 << 2)
18929 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18930 +#define au_fset_wrdir(flags, name) \
18931 +       do { (flags) |= AuWrDir_##name; } while (0)
18932 +#define au_fclr_wrdir(flags, name) \
18933 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18934 +
18935 +struct au_wr_dir_args {
18936 +       aufs_bindex_t force_btgt;
18937 +       unsigned char flags;
18938 +};
18939 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18940 +             struct au_wr_dir_args *args);
18941 +
18942 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18943 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18944 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18945 +                unsigned int udba, unsigned char flags);
18946 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18947 +          unsigned int udba, unsigned char flags) __must_check;
18948 +int au_do_pin(struct au_pin *pin) __must_check;
18949 +void au_unpin(struct au_pin *pin);
18950 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18951 +
18952 +#define AuIcpup_DID_CPUP       1
18953 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18954 +#define au_fset_icpup(flags, name) \
18955 +       do { (flags) |= AuIcpup_##name; } while (0)
18956 +#define au_fclr_icpup(flags, name) \
18957 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18958 +
18959 +struct au_icpup_args {
18960 +       unsigned char flags;
18961 +       unsigned char pin_flags;
18962 +       aufs_bindex_t btgt;
18963 +       unsigned int udba;
18964 +       struct au_pin pin;
18965 +       struct path h_path;
18966 +       struct inode *h_inode;
18967 +};
18968 +
18969 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18970 +                    struct au_icpup_args *a);
18971 +
18972 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18973 +                     int locked);
18974 +
18975 +/* i_op_add.c */
18976 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18977 +              struct dentry *h_parent, int isdir);
18978 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18979 +              dev_t dev);
18980 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18981 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18982 +               bool want_excl);
18983 +struct vfsub_aopen_args;
18984 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18985 +                      struct vfsub_aopen_args *args);
18986 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18987 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18988 +             struct dentry *dentry);
18989 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18990 +
18991 +/* i_op_del.c */
18992 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18993 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18994 +              struct dentry *h_parent, int isdir);
18995 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18996 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18997 +
18998 +/* i_op_ren.c */
18999 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19000 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
19001 +               struct inode *dir, struct dentry *dentry,
19002 +               unsigned int flags);
19003 +
19004 +/* iinfo.c */
19005 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19006 +void au_hiput(struct au_hinode *hinode);
19007 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19008 +                 struct dentry *h_wh);
19009 +unsigned int au_hi_flags(struct inode *inode, int isdir);
19010 +
19011 +/* hinode flags */
19012 +#define AuHi_XINO      1
19013 +#define AuHi_HNOTIFY   (1 << 1)
19014 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
19015 +#define au_fset_hi(flags, name) \
19016 +       do { (flags) |= AuHi_##name; } while (0)
19017 +#define au_fclr_hi(flags, name) \
19018 +       do { (flags) &= ~AuHi_##name; } while (0)
19019 +
19020 +#ifndef CONFIG_AUFS_HNOTIFY
19021 +#undef AuHi_HNOTIFY
19022 +#define AuHi_HNOTIFY   0
19023 +#endif
19024 +
19025 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19026 +                  struct inode *h_inode, unsigned int flags);
19027 +
19028 +void au_update_iigen(struct inode *inode, int half);
19029 +void au_update_ibrange(struct inode *inode, int do_put_zero);
19030 +
19031 +void au_icntnr_init_once(void *_c);
19032 +void au_hinode_init(struct au_hinode *hinode);
19033 +int au_iinfo_init(struct inode *inode);
19034 +void au_iinfo_fin(struct inode *inode);
19035 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
19036 +
19037 +#ifdef CONFIG_PROC_FS
19038 +/* plink.c */
19039 +int au_plink_maint(struct super_block *sb, int flags);
19040 +struct au_sbinfo;
19041 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19042 +int au_plink_maint_enter(struct super_block *sb);
19043 +#ifdef CONFIG_AUFS_DEBUG
19044 +void au_plink_list(struct super_block *sb);
19045 +#else
19046 +AuStubVoid(au_plink_list, struct super_block *sb)
19047 +#endif
19048 +int au_plink_test(struct inode *inode);
19049 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19050 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19051 +                    struct dentry *h_dentry);
19052 +void au_plink_put(struct super_block *sb, int verbose);
19053 +void au_plink_clean(struct super_block *sb, int verbose);
19054 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
19055 +#else
19056 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19057 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19058 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19059 +AuStubVoid(au_plink_list, struct super_block *sb);
19060 +AuStubInt0(au_plink_test, struct inode *inode);
19061 +AuStub(struct dentry *, au_plink_lkup, return NULL,
19062 +       struct inode *inode, aufs_bindex_t bindex);
19063 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19064 +          struct dentry *h_dentry);
19065 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19066 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19067 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19068 +#endif /* CONFIG_PROC_FS */
19069 +
19070 +#ifdef CONFIG_AUFS_XATTR
19071 +/* xattr.c */
19072 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19073 +                 unsigned int verbose);
19074 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19075 +void au_xattr_init(struct super_block *sb);
19076 +#else
19077 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19078 +          int ignore_flags, unsigned int verbose);
19079 +AuStubVoid(au_xattr_init, struct super_block *sb);
19080 +#endif
19081 +
19082 +#ifdef CONFIG_FS_POSIX_ACL
19083 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19084 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19085 +#endif
19086 +
19087 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19088 +enum {
19089 +       AU_XATTR_SET,
19090 +       AU_ACL_SET
19091 +};
19092 +
19093 +struct au_sxattr {
19094 +       int type;
19095 +       union {
19096 +               struct {
19097 +                       const char      *name;
19098 +                       const void      *value;
19099 +                       size_t          size;
19100 +                       int             flags;
19101 +               } set;
19102 +               struct {
19103 +                       struct posix_acl *acl;
19104 +                       int             type;
19105 +               } acl_set;
19106 +       } u;
19107 +};
19108 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19109 +                 struct au_sxattr *arg);
19110 +#endif
19111 +
19112 +/* ---------------------------------------------------------------------- */
19113 +
19114 +/* lock subclass for iinfo */
19115 +enum {
19116 +       AuLsc_II_CHILD,         /* child first */
19117 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19118 +       AuLsc_II_CHILD3,        /* copyup dirs */
19119 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19120 +       AuLsc_II_PARENT2,
19121 +       AuLsc_II_PARENT3,       /* copyup dirs */
19122 +       AuLsc_II_NEW_CHILD
19123 +};
19124 +
19125 +/*
19126 + * ii_read_lock_child, ii_write_lock_child,
19127 + * ii_read_lock_child2, ii_write_lock_child2,
19128 + * ii_read_lock_child3, ii_write_lock_child3,
19129 + * ii_read_lock_parent, ii_write_lock_parent,
19130 + * ii_read_lock_parent2, ii_write_lock_parent2,
19131 + * ii_read_lock_parent3, ii_write_lock_parent3,
19132 + * ii_read_lock_new_child, ii_write_lock_new_child,
19133 + */
19134 +#define AuReadLockFunc(name, lsc) \
19135 +static inline void ii_read_lock_##name(struct inode *i) \
19136 +{ \
19137 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19138 +}
19139 +
19140 +#define AuWriteLockFunc(name, lsc) \
19141 +static inline void ii_write_lock_##name(struct inode *i) \
19142 +{ \
19143 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19144 +}
19145 +
19146 +#define AuRWLockFuncs(name, lsc) \
19147 +       AuReadLockFunc(name, lsc) \
19148 +       AuWriteLockFunc(name, lsc)
19149 +
19150 +AuRWLockFuncs(child, CHILD);
19151 +AuRWLockFuncs(child2, CHILD2);
19152 +AuRWLockFuncs(child3, CHILD3);
19153 +AuRWLockFuncs(parent, PARENT);
19154 +AuRWLockFuncs(parent2, PARENT2);
19155 +AuRWLockFuncs(parent3, PARENT3);
19156 +AuRWLockFuncs(new_child, NEW_CHILD);
19157 +
19158 +#undef AuReadLockFunc
19159 +#undef AuWriteLockFunc
19160 +#undef AuRWLockFuncs
19161 +
19162 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19163 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19164 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19165 +
19166 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19167 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19168 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19169 +
19170 +/* ---------------------------------------------------------------------- */
19171 +
19172 +static inline void au_icntnr_init(struct au_icntnr *c)
19173 +{
19174 +#ifdef CONFIG_AUFS_DEBUG
19175 +       c->vfs_inode.i_mode = 0;
19176 +#endif
19177 +}
19178 +
19179 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19180 +{
19181 +       unsigned int gen;
19182 +       struct au_iinfo *iinfo;
19183 +       struct au_iigen *iigen;
19184 +
19185 +       iinfo = au_ii(inode);
19186 +       iigen = &iinfo->ii_generation;
19187 +       spin_lock(&iigen->ig_spin);
19188 +       if (igflags)
19189 +               *igflags = iigen->ig_flags;
19190 +       gen = iigen->ig_generation;
19191 +       spin_unlock(&iigen->ig_spin);
19192 +
19193 +       return gen;
19194 +}
19195 +
19196 +/* tiny test for inode number */
19197 +/* tmpfs generation is too rough */
19198 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19199 +{
19200 +       struct au_iinfo *iinfo;
19201 +
19202 +       iinfo = au_ii(inode);
19203 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19204 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19205 +                && iinfo->ii_higen == h_inode->i_generation);
19206 +}
19207 +
19208 +static inline void au_iigen_dec(struct inode *inode)
19209 +{
19210 +       struct au_iinfo *iinfo;
19211 +       struct au_iigen *iigen;
19212 +
19213 +       iinfo = au_ii(inode);
19214 +       iigen = &iinfo->ii_generation;
19215 +       spin_lock(&iigen->ig_spin);
19216 +       iigen->ig_generation--;
19217 +       spin_unlock(&iigen->ig_spin);
19218 +}
19219 +
19220 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19221 +{
19222 +       int err;
19223 +
19224 +       err = 0;
19225 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19226 +               err = -EIO;
19227 +
19228 +       return err;
19229 +}
19230 +
19231 +/* ---------------------------------------------------------------------- */
19232 +
19233 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19234 +                                         aufs_bindex_t bindex)
19235 +{
19236 +       return iinfo->ii_hinode + bindex;
19237 +}
19238 +
19239 +static inline int au_is_bad_inode(struct inode *inode)
19240 +{
19241 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19242 +}
19243 +
19244 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19245 +                                       aufs_bindex_t bindex)
19246 +{
19247 +       IiMustAnyLock(inode);
19248 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19249 +}
19250 +
19251 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19252 +{
19253 +       IiMustAnyLock(inode);
19254 +       return au_ii(inode)->ii_btop;
19255 +}
19256 +
19257 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19258 +{
19259 +       IiMustAnyLock(inode);
19260 +       return au_ii(inode)->ii_bbot;
19261 +}
19262 +
19263 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19264 +{
19265 +       IiMustAnyLock(inode);
19266 +       return au_ii(inode)->ii_vdir;
19267 +}
19268 +
19269 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19270 +{
19271 +       IiMustAnyLock(inode);
19272 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19273 +}
19274 +
19275 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19276 +{
19277 +       IiMustWriteLock(inode);
19278 +       au_ii(inode)->ii_btop = bindex;
19279 +}
19280 +
19281 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19282 +{
19283 +       IiMustWriteLock(inode);
19284 +       au_ii(inode)->ii_bbot = bindex;
19285 +}
19286 +
19287 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19288 +{
19289 +       IiMustWriteLock(inode);
19290 +       au_ii(inode)->ii_vdir = vdir;
19291 +}
19292 +
19293 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19294 +{
19295 +       IiMustAnyLock(inode);
19296 +       return au_hinode(au_ii(inode), bindex);
19297 +}
19298 +
19299 +/* ---------------------------------------------------------------------- */
19300 +
19301 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19302 +{
19303 +       if (pin)
19304 +               return pin->parent;
19305 +       return NULL;
19306 +}
19307 +
19308 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19309 +{
19310 +       if (pin && pin->hdir)
19311 +               return pin->hdir->hi_inode;
19312 +       return NULL;
19313 +}
19314 +
19315 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19316 +{
19317 +       if (pin)
19318 +               return pin->hdir;
19319 +       return NULL;
19320 +}
19321 +
19322 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19323 +{
19324 +       if (pin)
19325 +               pin->dentry = dentry;
19326 +}
19327 +
19328 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19329 +                                          unsigned char lflag)
19330 +{
19331 +       if (pin) {
19332 +               if (lflag)
19333 +                       au_fset_pin(pin->flags, DI_LOCKED);
19334 +               else
19335 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19336 +       }
19337 +}
19338 +
19339 +#if 0 /* reserved */
19340 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19341 +{
19342 +       if (pin) {
19343 +               dput(pin->parent);
19344 +               pin->parent = dget(parent);
19345 +       }
19346 +}
19347 +#endif
19348 +
19349 +/* ---------------------------------------------------------------------- */
19350 +
19351 +struct au_branch;
19352 +#ifdef CONFIG_AUFS_HNOTIFY
19353 +struct au_hnotify_op {
19354 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19355 +       int (*alloc)(struct au_hinode *hinode);
19356 +
19357 +       /*
19358 +        * if it returns true, the the caller should free hinode->hi_notify,
19359 +        * otherwise ->free() frees it.
19360 +        */
19361 +       int (*free)(struct au_hinode *hinode,
19362 +                   struct au_hnotify *hn) __must_check;
19363 +
19364 +       void (*fin)(void);
19365 +       int (*init)(void);
19366 +
19367 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19368 +       void (*fin_br)(struct au_branch *br);
19369 +       int (*init_br)(struct au_branch *br, int perm);
19370 +};
19371 +
19372 +/* hnotify.c */
19373 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19374 +void au_hn_free(struct au_hinode *hinode);
19375 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19376 +void au_hn_reset(struct inode *inode, unsigned int flags);
19377 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19378 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
19379 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19380 +int au_hnotify_init_br(struct au_branch *br, int perm);
19381 +void au_hnotify_fin_br(struct au_branch *br);
19382 +int __init au_hnotify_init(void);
19383 +void au_hnotify_fin(void);
19384 +
19385 +/* hfsnotify.c */
19386 +extern const struct au_hnotify_op au_hnotify_op;
19387 +
19388 +static inline
19389 +void au_hn_init(struct au_hinode *hinode)
19390 +{
19391 +       hinode->hi_notify = NULL;
19392 +}
19393 +
19394 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19395 +{
19396 +       return hinode->hi_notify;
19397 +}
19398 +
19399 +#else
19400 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19401 +       struct au_hinode *hinode __maybe_unused,
19402 +       struct inode *inode __maybe_unused)
19403 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19404 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19405 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19406 +          int do_set __maybe_unused)
19407 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19408 +          unsigned int flags __maybe_unused)
19409 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19410 +          struct au_branch *br __maybe_unused,
19411 +          int perm __maybe_unused)
19412 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19413 +          int perm __maybe_unused)
19414 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19415 +AuStubInt0(__init au_hnotify_init, void)
19416 +AuStubVoid(au_hnotify_fin, void)
19417 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19418 +#endif /* CONFIG_AUFS_HNOTIFY */
19419 +
19420 +static inline void au_hn_suspend(struct au_hinode *hdir)
19421 +{
19422 +       au_hn_ctl(hdir, /*do_set*/0);
19423 +}
19424 +
19425 +static inline void au_hn_resume(struct au_hinode *hdir)
19426 +{
19427 +       au_hn_ctl(hdir, /*do_set*/1);
19428 +}
19429 +
19430 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19431 +{
19432 +       inode_lock(hdir->hi_inode);
19433 +       au_hn_suspend(hdir);
19434 +}
19435 +
19436 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19437 +                                         unsigned int sc __maybe_unused)
19438 +{
19439 +       inode_lock_nested(hdir->hi_inode, sc);
19440 +       au_hn_suspend(hdir);
19441 +}
19442 +
19443 +#if 0 /* unused */
19444 +#include "vfsub.h"
19445 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19446 +                                                 unsigned int sc)
19447 +{
19448 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19449 +       au_hn_suspend(hdir);
19450 +}
19451 +#endif
19452 +
19453 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19454 +{
19455 +       au_hn_resume(hdir);
19456 +       inode_unlock(hdir->hi_inode);
19457 +}
19458 +
19459 +#endif /* __KERNEL__ */
19460 +#endif /* __AUFS_INODE_H__ */
19461 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19462 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19463 +++ linux/fs/aufs/ioctl.c       2018-07-19 09:46:13.059981241 +0200
19464 @@ -0,0 +1,220 @@
19465 +/* SPDX-License-Identifier: GPL-2.0 */
19466 +/*
19467 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19468 + *
19469 + * This program, aufs is free software; you can redistribute it and/or modify
19470 + * it under the terms of the GNU General Public License as published by
19471 + * the Free Software Foundation; either version 2 of the License, or
19472 + * (at your option) any later version.
19473 + *
19474 + * This program is distributed in the hope that it will be useful,
19475 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19476 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19477 + * GNU General Public License for more details.
19478 + *
19479 + * You should have received a copy of the GNU General Public License
19480 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19481 + */
19482 +
19483 +/*
19484 + * ioctl
19485 + * plink-management and readdir in userspace.
19486 + * assist the pathconf(3) wrapper library.
19487 + * move-down
19488 + * File-based Hierarchical Storage Management.
19489 + */
19490 +
19491 +#include <linux/compat.h>
19492 +#include <linux/file.h>
19493 +#include "aufs.h"
19494 +
19495 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19496 +{
19497 +       int err, fd;
19498 +       aufs_bindex_t wbi, bindex, bbot;
19499 +       struct file *h_file;
19500 +       struct super_block *sb;
19501 +       struct dentry *root;
19502 +       struct au_branch *br;
19503 +       struct aufs_wbr_fd wbrfd = {
19504 +               .oflags = au_dir_roflags,
19505 +               .brid   = -1
19506 +       };
19507 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19508 +               | O_NOATIME | O_CLOEXEC;
19509 +
19510 +       AuDebugOn(wbrfd.oflags & ~valid);
19511 +
19512 +       if (arg) {
19513 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19514 +               if (unlikely(err)) {
19515 +                       err = -EFAULT;
19516 +                       goto out;
19517 +               }
19518 +
19519 +               err = -EINVAL;
19520 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19521 +               wbrfd.oflags |= au_dir_roflags;
19522 +               AuDbg("0%o\n", wbrfd.oflags);
19523 +               if (unlikely(wbrfd.oflags & ~valid))
19524 +                       goto out;
19525 +       }
19526 +
19527 +       fd = get_unused_fd_flags(0);
19528 +       err = fd;
19529 +       if (unlikely(fd < 0))
19530 +               goto out;
19531 +
19532 +       h_file = ERR_PTR(-EINVAL);
19533 +       wbi = 0;
19534 +       br = NULL;
19535 +       sb = path->dentry->d_sb;
19536 +       root = sb->s_root;
19537 +       aufs_read_lock(root, AuLock_IR);
19538 +       bbot = au_sbbot(sb);
19539 +       if (wbrfd.brid >= 0) {
19540 +               wbi = au_br_index(sb, wbrfd.brid);
19541 +               if (unlikely(wbi < 0 || wbi > bbot))
19542 +                       goto out_unlock;
19543 +       }
19544 +
19545 +       h_file = ERR_PTR(-ENOENT);
19546 +       br = au_sbr(sb, wbi);
19547 +       if (!au_br_writable(br->br_perm)) {
19548 +               if (arg)
19549 +                       goto out_unlock;
19550 +
19551 +               bindex = wbi + 1;
19552 +               wbi = -1;
19553 +               for (; bindex <= bbot; bindex++) {
19554 +                       br = au_sbr(sb, bindex);
19555 +                       if (au_br_writable(br->br_perm)) {
19556 +                               wbi = bindex;
19557 +                               br = au_sbr(sb, wbi);
19558 +                               break;
19559 +                       }
19560 +               }
19561 +       }
19562 +       AuDbg("wbi %d\n", wbi);
19563 +       if (wbi >= 0)
19564 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19565 +                                  /*force_wr*/0);
19566 +
19567 +out_unlock:
19568 +       aufs_read_unlock(root, AuLock_IR);
19569 +       err = PTR_ERR(h_file);
19570 +       if (IS_ERR(h_file))
19571 +               goto out_fd;
19572 +
19573 +       au_br_put(br); /* cf. au_h_open() */
19574 +       fd_install(fd, h_file);
19575 +       err = fd;
19576 +       goto out; /* success */
19577 +
19578 +out_fd:
19579 +       put_unused_fd(fd);
19580 +out:
19581 +       AuTraceErr(err);
19582 +       return err;
19583 +}
19584 +
19585 +/* ---------------------------------------------------------------------- */
19586 +
19587 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19588 +{
19589 +       long err;
19590 +       struct dentry *dentry;
19591 +
19592 +       switch (cmd) {
19593 +       case AUFS_CTL_RDU:
19594 +       case AUFS_CTL_RDU_INO:
19595 +               err = au_rdu_ioctl(file, cmd, arg);
19596 +               break;
19597 +
19598 +       case AUFS_CTL_WBR_FD:
19599 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19600 +               break;
19601 +
19602 +       case AUFS_CTL_IBUSY:
19603 +               err = au_ibusy_ioctl(file, arg);
19604 +               break;
19605 +
19606 +       case AUFS_CTL_BRINFO:
19607 +               err = au_brinfo_ioctl(file, arg);
19608 +               break;
19609 +
19610 +       case AUFS_CTL_FHSM_FD:
19611 +               dentry = file->f_path.dentry;
19612 +               if (IS_ROOT(dentry))
19613 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19614 +               else
19615 +                       err = -ENOTTY;
19616 +               break;
19617 +
19618 +       default:
19619 +               /* do not call the lower */
19620 +               AuDbg("0x%x\n", cmd);
19621 +               err = -ENOTTY;
19622 +       }
19623 +
19624 +       AuTraceErr(err);
19625 +       return err;
19626 +}
19627 +
19628 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19629 +{
19630 +       long err;
19631 +
19632 +       switch (cmd) {
19633 +       case AUFS_CTL_MVDOWN:
19634 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19635 +               break;
19636 +
19637 +       case AUFS_CTL_WBR_FD:
19638 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19639 +               break;
19640 +
19641 +       default:
19642 +               /* do not call the lower */
19643 +               AuDbg("0x%x\n", cmd);
19644 +               err = -ENOTTY;
19645 +       }
19646 +
19647 +       AuTraceErr(err);
19648 +       return err;
19649 +}
19650 +
19651 +#ifdef CONFIG_COMPAT
19652 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19653 +                          unsigned long arg)
19654 +{
19655 +       long err;
19656 +
19657 +       switch (cmd) {
19658 +       case AUFS_CTL_RDU:
19659 +       case AUFS_CTL_RDU_INO:
19660 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19661 +               break;
19662 +
19663 +       case AUFS_CTL_IBUSY:
19664 +               err = au_ibusy_compat_ioctl(file, arg);
19665 +               break;
19666 +
19667 +       case AUFS_CTL_BRINFO:
19668 +               err = au_brinfo_compat_ioctl(file, arg);
19669 +               break;
19670 +
19671 +       default:
19672 +               err = aufs_ioctl_dir(file, cmd, arg);
19673 +       }
19674 +
19675 +       AuTraceErr(err);
19676 +       return err;
19677 +}
19678 +
19679 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19680 +                             unsigned long arg)
19681 +{
19682 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19683 +}
19684 +#endif
19685 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19686 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19687 +++ linux/fs/aufs/i_op_add.c    2018-07-19 09:46:13.059981241 +0200
19688 @@ -0,0 +1,921 @@
19689 +/* SPDX-License-Identifier: GPL-2.0 */
19690 +/*
19691 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19692 + *
19693 + * This program, aufs is free software; you can redistribute it and/or modify
19694 + * it under the terms of the GNU General Public License as published by
19695 + * the Free Software Foundation; either version 2 of the License, or
19696 + * (at your option) any later version.
19697 + *
19698 + * This program is distributed in the hope that it will be useful,
19699 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19700 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19701 + * GNU General Public License for more details.
19702 + *
19703 + * You should have received a copy of the GNU General Public License
19704 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19705 + */
19706 +
19707 +/*
19708 + * inode operations (add entry)
19709 + */
19710 +
19711 +#include "aufs.h"
19712 +
19713 +/*
19714 + * final procedure of adding a new entry, except link(2).
19715 + * remove whiteout, instantiate, copyup the parent dir's times and size
19716 + * and update version.
19717 + * if it failed, re-create the removed whiteout.
19718 + */
19719 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19720 +                 struct dentry *wh_dentry, struct dentry *dentry)
19721 +{
19722 +       int err, rerr;
19723 +       aufs_bindex_t bwh;
19724 +       struct path h_path;
19725 +       struct super_block *sb;
19726 +       struct inode *inode, *h_dir;
19727 +       struct dentry *wh;
19728 +
19729 +       bwh = -1;
19730 +       sb = dir->i_sb;
19731 +       if (wh_dentry) {
19732 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19733 +               IMustLock(h_dir);
19734 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19735 +               bwh = au_dbwh(dentry);
19736 +               h_path.dentry = wh_dentry;
19737 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19738 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19739 +                                         dentry);
19740 +               if (unlikely(err))
19741 +                       goto out;
19742 +       }
19743 +
19744 +       inode = au_new_inode(dentry, /*must_new*/1);
19745 +       if (!IS_ERR(inode)) {
19746 +               d_instantiate(dentry, inode);
19747 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19748 +               IMustLock(dir);
19749 +               au_dir_ts(dir, bindex);
19750 +               inode_inc_iversion(dir);
19751 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19752 +               return 0; /* success */
19753 +       }
19754 +
19755 +       err = PTR_ERR(inode);
19756 +       if (!wh_dentry)
19757 +               goto out;
19758 +
19759 +       /* revert */
19760 +       /* dir inode is locked */
19761 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19762 +       rerr = PTR_ERR(wh);
19763 +       if (IS_ERR(wh)) {
19764 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19765 +                       dentry, err, rerr);
19766 +               err = -EIO;
19767 +       } else
19768 +               dput(wh);
19769 +
19770 +out:
19771 +       return err;
19772 +}
19773 +
19774 +static int au_d_may_add(struct dentry *dentry)
19775 +{
19776 +       int err;
19777 +
19778 +       err = 0;
19779 +       if (unlikely(d_unhashed(dentry)))
19780 +               err = -ENOENT;
19781 +       if (unlikely(d_really_is_positive(dentry)))
19782 +               err = -EEXIST;
19783 +       return err;
19784 +}
19785 +
19786 +/*
19787 + * simple tests for the adding inode operations.
19788 + * following the checks in vfs, plus the parent-child relationship.
19789 + */
19790 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19791 +              struct dentry *h_parent, int isdir)
19792 +{
19793 +       int err;
19794 +       umode_t h_mode;
19795 +       struct dentry *h_dentry;
19796 +       struct inode *h_inode;
19797 +
19798 +       err = -ENAMETOOLONG;
19799 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19800 +               goto out;
19801 +
19802 +       h_dentry = au_h_dptr(dentry, bindex);
19803 +       if (d_really_is_negative(dentry)) {
19804 +               err = -EEXIST;
19805 +               if (unlikely(d_is_positive(h_dentry)))
19806 +                       goto out;
19807 +       } else {
19808 +               /* rename(2) case */
19809 +               err = -EIO;
19810 +               if (unlikely(d_is_negative(h_dentry)))
19811 +                       goto out;
19812 +               h_inode = d_inode(h_dentry);
19813 +               if (unlikely(!h_inode->i_nlink))
19814 +                       goto out;
19815 +
19816 +               h_mode = h_inode->i_mode;
19817 +               if (!isdir) {
19818 +                       err = -EISDIR;
19819 +                       if (unlikely(S_ISDIR(h_mode)))
19820 +                               goto out;
19821 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19822 +                       err = -ENOTDIR;
19823 +                       goto out;
19824 +               }
19825 +       }
19826 +
19827 +       err = 0;
19828 +       /* expected parent dir is locked */
19829 +       if (unlikely(h_parent != h_dentry->d_parent))
19830 +               err = -EIO;
19831 +
19832 +out:
19833 +       AuTraceErr(err);
19834 +       return err;
19835 +}
19836 +
19837 +/*
19838 + * initial procedure of adding a new entry.
19839 + * prepare writable branch and the parent dir, lock it,
19840 + * and lookup whiteout for the new entry.
19841 + */
19842 +static struct dentry*
19843 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19844 +                 struct dentry *src_dentry, struct au_pin *pin,
19845 +                 struct au_wr_dir_args *wr_dir_args)
19846 +{
19847 +       struct dentry *wh_dentry, *h_parent;
19848 +       struct super_block *sb;
19849 +       struct au_branch *br;
19850 +       int err;
19851 +       unsigned int udba;
19852 +       aufs_bindex_t bcpup;
19853 +
19854 +       AuDbg("%pd\n", dentry);
19855 +
19856 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19857 +       bcpup = err;
19858 +       wh_dentry = ERR_PTR(err);
19859 +       if (unlikely(err < 0))
19860 +               goto out;
19861 +
19862 +       sb = dentry->d_sb;
19863 +       udba = au_opt_udba(sb);
19864 +       err = au_pin(pin, dentry, bcpup, udba,
19865 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19866 +       wh_dentry = ERR_PTR(err);
19867 +       if (unlikely(err))
19868 +               goto out;
19869 +
19870 +       h_parent = au_pinned_h_parent(pin);
19871 +       if (udba != AuOpt_UDBA_NONE
19872 +           && au_dbtop(dentry) == bcpup)
19873 +               err = au_may_add(dentry, bcpup, h_parent,
19874 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19875 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19876 +               err = -ENAMETOOLONG;
19877 +       wh_dentry = ERR_PTR(err);
19878 +       if (unlikely(err))
19879 +               goto out_unpin;
19880 +
19881 +       br = au_sbr(sb, bcpup);
19882 +       if (dt) {
19883 +               struct path tmp = {
19884 +                       .dentry = h_parent,
19885 +                       .mnt    = au_br_mnt(br)
19886 +               };
19887 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19888 +       }
19889 +
19890 +       wh_dentry = NULL;
19891 +       if (bcpup != au_dbwh(dentry))
19892 +               goto out; /* success */
19893 +
19894 +       /*
19895 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19896 +        * would not be able to removed in the future. So we don't allow such
19897 +        * name here and we don't handle ENAMETOOLONG differently here.
19898 +        */
19899 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19900 +
19901 +out_unpin:
19902 +       if (IS_ERR(wh_dentry))
19903 +               au_unpin(pin);
19904 +out:
19905 +       return wh_dentry;
19906 +}
19907 +
19908 +/* ---------------------------------------------------------------------- */
19909 +
19910 +enum { Mknod, Symlink, Creat };
19911 +struct simple_arg {
19912 +       int type;
19913 +       union {
19914 +               struct {
19915 +                       umode_t                 mode;
19916 +                       bool                    want_excl;
19917 +                       bool                    try_aopen;
19918 +                       struct vfsub_aopen_args *aopen;
19919 +               } c;
19920 +               struct {
19921 +                       const char *symname;
19922 +               } s;
19923 +               struct {
19924 +                       umode_t mode;
19925 +                       dev_t dev;
19926 +               } m;
19927 +       } u;
19928 +};
19929 +
19930 +static int add_simple(struct inode *dir, struct dentry *dentry,
19931 +                     struct simple_arg *arg)
19932 +{
19933 +       int err, rerr;
19934 +       aufs_bindex_t btop;
19935 +       unsigned char created;
19936 +       const unsigned char try_aopen
19937 +               = (arg->type == Creat && arg->u.c.try_aopen);
19938 +       struct dentry *wh_dentry, *parent;
19939 +       struct inode *h_dir;
19940 +       struct super_block *sb;
19941 +       struct au_branch *br;
19942 +       /* to reuduce stack size */
19943 +       struct {
19944 +               struct au_dtime dt;
19945 +               struct au_pin pin;
19946 +               struct path h_path;
19947 +               struct au_wr_dir_args wr_dir_args;
19948 +       } *a;
19949 +
19950 +       AuDbg("%pd\n", dentry);
19951 +       IMustLock(dir);
19952 +
19953 +       err = -ENOMEM;
19954 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19955 +       if (unlikely(!a))
19956 +               goto out;
19957 +       a->wr_dir_args.force_btgt = -1;
19958 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19959 +
19960 +       parent = dentry->d_parent; /* dir inode is locked */
19961 +       if (!try_aopen) {
19962 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19963 +               if (unlikely(err))
19964 +                       goto out_free;
19965 +       }
19966 +       err = au_d_may_add(dentry);
19967 +       if (unlikely(err))
19968 +               goto out_unlock;
19969 +       if (!try_aopen)
19970 +               di_write_lock_parent(parent);
19971 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19972 +                                     &a->pin, &a->wr_dir_args);
19973 +       err = PTR_ERR(wh_dentry);
19974 +       if (IS_ERR(wh_dentry))
19975 +               goto out_parent;
19976 +
19977 +       btop = au_dbtop(dentry);
19978 +       sb = dentry->d_sb;
19979 +       br = au_sbr(sb, btop);
19980 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19981 +       a->h_path.mnt = au_br_mnt(br);
19982 +       h_dir = au_pinned_h_dir(&a->pin);
19983 +       switch (arg->type) {
19984 +       case Creat:
19985 +               err = 0;
19986 +               if (!try_aopen || !h_dir->i_op->atomic_open)
19987 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19988 +                                          arg->u.c.want_excl);
19989 +               else
19990 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
19991 +                                               arg->u.c.aopen, br);
19992 +               break;
19993 +       case Symlink:
19994 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19995 +               break;
19996 +       case Mknod:
19997 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19998 +                                 arg->u.m.dev);
19999 +               break;
20000 +       default:
20001 +               BUG();
20002 +       }
20003 +       created = !err;
20004 +       if (!err)
20005 +               err = epilog(dir, btop, wh_dentry, dentry);
20006 +
20007 +       /* revert */
20008 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
20009 +               /* no delegation since it is just created */
20010 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20011 +                                   /*force*/0);
20012 +               if (rerr) {
20013 +                       AuIOErr("%pd revert failure(%d, %d)\n",
20014 +                               dentry, err, rerr);
20015 +                       err = -EIO;
20016 +               }
20017 +               au_dtime_revert(&a->dt);
20018 +       }
20019 +
20020 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
20021 +               *arg->u.c.aopen->opened |= FILE_CREATED;
20022 +
20023 +       au_unpin(&a->pin);
20024 +       dput(wh_dentry);
20025 +
20026 +out_parent:
20027 +       if (!try_aopen)
20028 +               di_write_unlock(parent);
20029 +out_unlock:
20030 +       if (unlikely(err)) {
20031 +               au_update_dbtop(dentry);
20032 +               d_drop(dentry);
20033 +       }
20034 +       if (!try_aopen)
20035 +               aufs_read_unlock(dentry, AuLock_DW);
20036 +out_free:
20037 +       kfree(a);
20038 +out:
20039 +       return err;
20040 +}
20041 +
20042 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20043 +              dev_t dev)
20044 +{
20045 +       struct simple_arg arg = {
20046 +               .type = Mknod,
20047 +               .u.m = {
20048 +                       .mode   = mode,
20049 +                       .dev    = dev
20050 +               }
20051 +       };
20052 +       return add_simple(dir, dentry, &arg);
20053 +}
20054 +
20055 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20056 +{
20057 +       struct simple_arg arg = {
20058 +               .type = Symlink,
20059 +               .u.s.symname = symname
20060 +       };
20061 +       return add_simple(dir, dentry, &arg);
20062 +}
20063 +
20064 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20065 +               bool want_excl)
20066 +{
20067 +       struct simple_arg arg = {
20068 +               .type = Creat,
20069 +               .u.c = {
20070 +                       .mode           = mode,
20071 +                       .want_excl      = want_excl
20072 +               }
20073 +       };
20074 +       return add_simple(dir, dentry, &arg);
20075 +}
20076 +
20077 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20078 +                      struct vfsub_aopen_args *aopen_args)
20079 +{
20080 +       struct simple_arg arg = {
20081 +               .type = Creat,
20082 +               .u.c = {
20083 +                       .mode           = aopen_args->create_mode,
20084 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20085 +                       .try_aopen      = true,
20086 +                       .aopen          = aopen_args
20087 +               }
20088 +       };
20089 +       return add_simple(dir, dentry, &arg);
20090 +}
20091 +
20092 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20093 +{
20094 +       int err;
20095 +       aufs_bindex_t bindex;
20096 +       struct super_block *sb;
20097 +       struct dentry *parent, *h_parent, *h_dentry;
20098 +       struct inode *h_dir, *inode;
20099 +       struct vfsmount *h_mnt;
20100 +       struct au_wr_dir_args wr_dir_args = {
20101 +               .force_btgt     = -1,
20102 +               .flags          = AuWrDir_TMPFILE
20103 +       };
20104 +
20105 +       /* copy-up may happen */
20106 +       inode_lock(dir);
20107 +
20108 +       sb = dir->i_sb;
20109 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20110 +       if (unlikely(err))
20111 +               goto out;
20112 +
20113 +       err = au_di_init(dentry);
20114 +       if (unlikely(err))
20115 +               goto out_si;
20116 +
20117 +       err = -EBUSY;
20118 +       parent = d_find_any_alias(dir);
20119 +       AuDebugOn(!parent);
20120 +       di_write_lock_parent(parent);
20121 +       if (unlikely(d_inode(parent) != dir))
20122 +               goto out_parent;
20123 +
20124 +       err = au_digen_test(parent, au_sigen(sb));
20125 +       if (unlikely(err))
20126 +               goto out_parent;
20127 +
20128 +       bindex = au_dbtop(parent);
20129 +       au_set_dbtop(dentry, bindex);
20130 +       au_set_dbbot(dentry, bindex);
20131 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20132 +       bindex = err;
20133 +       if (unlikely(err < 0))
20134 +               goto out_parent;
20135 +
20136 +       err = -EOPNOTSUPP;
20137 +       h_dir = au_h_iptr(dir, bindex);
20138 +       if (unlikely(!h_dir->i_op->tmpfile))
20139 +               goto out_parent;
20140 +
20141 +       h_mnt = au_sbr_mnt(sb, bindex);
20142 +       err = vfsub_mnt_want_write(h_mnt);
20143 +       if (unlikely(err))
20144 +               goto out_parent;
20145 +
20146 +       h_parent = au_h_dptr(parent, bindex);
20147 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20148 +       if (IS_ERR(h_dentry)) {
20149 +               err = PTR_ERR(h_dentry);
20150 +               goto out_mnt;
20151 +       }
20152 +
20153 +       au_set_dbtop(dentry, bindex);
20154 +       au_set_dbbot(dentry, bindex);
20155 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20156 +       inode = au_new_inode(dentry, /*must_new*/1);
20157 +       if (IS_ERR(inode)) {
20158 +               err = PTR_ERR(inode);
20159 +               au_set_h_dptr(dentry, bindex, NULL);
20160 +               au_set_dbtop(dentry, -1);
20161 +               au_set_dbbot(dentry, -1);
20162 +       } else {
20163 +               if (!inode->i_nlink)
20164 +                       set_nlink(inode, 1);
20165 +               d_tmpfile(dentry, inode);
20166 +               au_di(dentry)->di_tmpfile = 1;
20167 +
20168 +               /* update without i_mutex */
20169 +               if (au_ibtop(dir) == au_dbtop(dentry))
20170 +                       au_cpup_attr_timesizes(dir);
20171 +       }
20172 +       dput(h_dentry);
20173 +
20174 +out_mnt:
20175 +       vfsub_mnt_drop_write(h_mnt);
20176 +out_parent:
20177 +       di_write_unlock(parent);
20178 +       dput(parent);
20179 +       di_write_unlock(dentry);
20180 +       if (unlikely(err)) {
20181 +               au_di_fin(dentry);
20182 +               dentry->d_fsdata = NULL;
20183 +       }
20184 +out_si:
20185 +       si_read_unlock(sb);
20186 +out:
20187 +       inode_unlock(dir);
20188 +       return err;
20189 +}
20190 +
20191 +/* ---------------------------------------------------------------------- */
20192 +
20193 +struct au_link_args {
20194 +       aufs_bindex_t bdst, bsrc;
20195 +       struct au_pin pin;
20196 +       struct path h_path;
20197 +       struct dentry *src_parent, *parent;
20198 +};
20199 +
20200 +static int au_cpup_before_link(struct dentry *src_dentry,
20201 +                              struct au_link_args *a)
20202 +{
20203 +       int err;
20204 +       struct dentry *h_src_dentry;
20205 +       struct au_cp_generic cpg = {
20206 +               .dentry = src_dentry,
20207 +               .bdst   = a->bdst,
20208 +               .bsrc   = a->bsrc,
20209 +               .len    = -1,
20210 +               .pin    = &a->pin,
20211 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20212 +       };
20213 +
20214 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20215 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20216 +       if (unlikely(err))
20217 +               goto out;
20218 +
20219 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20220 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20221 +                    au_opt_udba(src_dentry->d_sb),
20222 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20223 +       if (unlikely(err))
20224 +               goto out;
20225 +
20226 +       err = au_sio_cpup_simple(&cpg);
20227 +       au_unpin(&a->pin);
20228 +
20229 +out:
20230 +       di_read_unlock(a->src_parent, AuLock_IR);
20231 +       return err;
20232 +}
20233 +
20234 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20235 +                          struct au_link_args *a)
20236 +{
20237 +       int err;
20238 +       unsigned char plink;
20239 +       aufs_bindex_t bbot;
20240 +       struct dentry *h_src_dentry;
20241 +       struct inode *h_inode, *inode, *delegated;
20242 +       struct super_block *sb;
20243 +       struct file *h_file;
20244 +
20245 +       plink = 0;
20246 +       h_inode = NULL;
20247 +       sb = src_dentry->d_sb;
20248 +       inode = d_inode(src_dentry);
20249 +       if (au_ibtop(inode) <= a->bdst)
20250 +               h_inode = au_h_iptr(inode, a->bdst);
20251 +       if (!h_inode || !h_inode->i_nlink) {
20252 +               /* copyup src_dentry as the name of dentry. */
20253 +               bbot = au_dbbot(dentry);
20254 +               if (bbot < a->bsrc)
20255 +                       au_set_dbbot(dentry, a->bsrc);
20256 +               au_set_h_dptr(dentry, a->bsrc,
20257 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20258 +               dget(a->h_path.dentry);
20259 +               au_set_h_dptr(dentry, a->bdst, NULL);
20260 +               AuDbg("temporary d_inode...\n");
20261 +               spin_lock(&dentry->d_lock);
20262 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20263 +               spin_unlock(&dentry->d_lock);
20264 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20265 +               if (IS_ERR(h_file))
20266 +                       err = PTR_ERR(h_file);
20267 +               else {
20268 +                       struct au_cp_generic cpg = {
20269 +                               .dentry = dentry,
20270 +                               .bdst   = a->bdst,
20271 +                               .bsrc   = -1,
20272 +                               .len    = -1,
20273 +                               .pin    = &a->pin,
20274 +                               .flags  = AuCpup_KEEPLINO
20275 +                       };
20276 +                       err = au_sio_cpup_simple(&cpg);
20277 +                       au_h_open_post(dentry, a->bsrc, h_file);
20278 +                       if (!err) {
20279 +                               dput(a->h_path.dentry);
20280 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20281 +                       } else
20282 +                               au_set_h_dptr(dentry, a->bdst,
20283 +                                             a->h_path.dentry);
20284 +               }
20285 +               spin_lock(&dentry->d_lock);
20286 +               dentry->d_inode = NULL; /* restore */
20287 +               spin_unlock(&dentry->d_lock);
20288 +               AuDbg("temporary d_inode...done\n");
20289 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20290 +               au_set_dbbot(dentry, bbot);
20291 +       } else {
20292 +               /* the inode of src_dentry already exists on a.bdst branch */
20293 +               h_src_dentry = d_find_alias(h_inode);
20294 +               if (!h_src_dentry && au_plink_test(inode)) {
20295 +                       plink = 1;
20296 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20297 +                       err = PTR_ERR(h_src_dentry);
20298 +                       if (IS_ERR(h_src_dentry))
20299 +                               goto out;
20300 +
20301 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20302 +                               dput(h_src_dentry);
20303 +                               h_src_dentry = NULL;
20304 +                       }
20305 +
20306 +               }
20307 +               if (h_src_dentry) {
20308 +                       delegated = NULL;
20309 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20310 +                                        &a->h_path, &delegated);
20311 +                       if (unlikely(err == -EWOULDBLOCK)) {
20312 +                               pr_warn("cannot retry for NFSv4 delegation"
20313 +                                       " for an internal link\n");
20314 +                               iput(delegated);
20315 +                       }
20316 +                       dput(h_src_dentry);
20317 +               } else {
20318 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20319 +                               h_inode->i_ino, a->bdst);
20320 +                       err = -EIO;
20321 +               }
20322 +       }
20323 +
20324 +       if (!err && !plink)
20325 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20326 +
20327 +out:
20328 +       AuTraceErr(err);
20329 +       return err;
20330 +}
20331 +
20332 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20333 +             struct dentry *dentry)
20334 +{
20335 +       int err, rerr;
20336 +       struct au_dtime dt;
20337 +       struct au_link_args *a;
20338 +       struct dentry *wh_dentry, *h_src_dentry;
20339 +       struct inode *inode, *delegated;
20340 +       struct super_block *sb;
20341 +       struct au_wr_dir_args wr_dir_args = {
20342 +               /* .force_btgt  = -1, */
20343 +               .flags          = AuWrDir_ADD_ENTRY
20344 +       };
20345 +
20346 +       IMustLock(dir);
20347 +       inode = d_inode(src_dentry);
20348 +       IMustLock(inode);
20349 +
20350 +       err = -ENOMEM;
20351 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20352 +       if (unlikely(!a))
20353 +               goto out;
20354 +
20355 +       a->parent = dentry->d_parent; /* dir inode is locked */
20356 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20357 +                                       AuLock_NOPLM | AuLock_GEN);
20358 +       if (unlikely(err))
20359 +               goto out_kfree;
20360 +       err = au_d_linkable(src_dentry);
20361 +       if (unlikely(err))
20362 +               goto out_unlock;
20363 +       err = au_d_may_add(dentry);
20364 +       if (unlikely(err))
20365 +               goto out_unlock;
20366 +
20367 +       a->src_parent = dget_parent(src_dentry);
20368 +       wr_dir_args.force_btgt = au_ibtop(inode);
20369 +
20370 +       di_write_lock_parent(a->parent);
20371 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20372 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20373 +                                     &wr_dir_args);
20374 +       err = PTR_ERR(wh_dentry);
20375 +       if (IS_ERR(wh_dentry))
20376 +               goto out_parent;
20377 +
20378 +       err = 0;
20379 +       sb = dentry->d_sb;
20380 +       a->bdst = au_dbtop(dentry);
20381 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20382 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20383 +       a->bsrc = au_ibtop(inode);
20384 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20385 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20386 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20387 +       if (!h_src_dentry) {
20388 +               a->bsrc = au_dbtop(src_dentry);
20389 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20390 +               AuDebugOn(!h_src_dentry);
20391 +       } else if (IS_ERR(h_src_dentry)) {
20392 +               err = PTR_ERR(h_src_dentry);
20393 +               goto out_parent;
20394 +       }
20395 +
20396 +       /*
20397 +        * aufs doesn't touch the credential so
20398 +        * security_dentry_create_files_as() is unnecrssary.
20399 +        */
20400 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20401 +               if (a->bdst < a->bsrc
20402 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20403 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20404 +               else {
20405 +                       delegated = NULL;
20406 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20407 +                                        &a->h_path, &delegated);
20408 +                       if (unlikely(err == -EWOULDBLOCK)) {
20409 +                               pr_warn("cannot retry for NFSv4 delegation"
20410 +                                       " for an internal link\n");
20411 +                               iput(delegated);
20412 +                       }
20413 +               }
20414 +               dput(h_src_dentry);
20415 +       } else {
20416 +               /*
20417 +                * copyup src_dentry to the branch we process,
20418 +                * and then link(2) to it.
20419 +                */
20420 +               dput(h_src_dentry);
20421 +               if (a->bdst < a->bsrc
20422 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20423 +                       au_unpin(&a->pin);
20424 +                       di_write_unlock(a->parent);
20425 +                       err = au_cpup_before_link(src_dentry, a);
20426 +                       di_write_lock_parent(a->parent);
20427 +                       if (!err)
20428 +                               err = au_pin(&a->pin, dentry, a->bdst,
20429 +                                            au_opt_udba(sb),
20430 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20431 +                       if (unlikely(err))
20432 +                               goto out_wh;
20433 +               }
20434 +               if (!err) {
20435 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20436 +                       err = -ENOENT;
20437 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20438 +                               delegated = NULL;
20439 +                               err = vfsub_link(h_src_dentry,
20440 +                                                au_pinned_h_dir(&a->pin),
20441 +                                                &a->h_path, &delegated);
20442 +                               if (unlikely(err == -EWOULDBLOCK)) {
20443 +                                       pr_warn("cannot retry"
20444 +                                               " for NFSv4 delegation"
20445 +                                               " for an internal link\n");
20446 +                                       iput(delegated);
20447 +                               }
20448 +                       }
20449 +               }
20450 +       }
20451 +       if (unlikely(err))
20452 +               goto out_unpin;
20453 +
20454 +       if (wh_dentry) {
20455 +               a->h_path.dentry = wh_dentry;
20456 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20457 +                                         dentry);
20458 +               if (unlikely(err))
20459 +                       goto out_revert;
20460 +       }
20461 +
20462 +       au_dir_ts(dir, a->bdst);
20463 +       inode_inc_iversion(dir);
20464 +       inc_nlink(inode);
20465 +       inode->i_ctime = dir->i_ctime;
20466 +       d_instantiate(dentry, au_igrab(inode));
20467 +       if (d_unhashed(a->h_path.dentry))
20468 +               /* some filesystem calls d_drop() */
20469 +               d_drop(dentry);
20470 +       /* some filesystems consume an inode even hardlink */
20471 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20472 +       goto out_unpin; /* success */
20473 +
20474 +out_revert:
20475 +       /* no delegation since it is just created */
20476 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20477 +                           /*delegated*/NULL, /*force*/0);
20478 +       if (unlikely(rerr)) {
20479 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20480 +               err = -EIO;
20481 +       }
20482 +       au_dtime_revert(&dt);
20483 +out_unpin:
20484 +       au_unpin(&a->pin);
20485 +out_wh:
20486 +       dput(wh_dentry);
20487 +out_parent:
20488 +       di_write_unlock(a->parent);
20489 +       dput(a->src_parent);
20490 +out_unlock:
20491 +       if (unlikely(err)) {
20492 +               au_update_dbtop(dentry);
20493 +               d_drop(dentry);
20494 +       }
20495 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20496 +out_kfree:
20497 +       kfree(a);
20498 +out:
20499 +       AuTraceErr(err);
20500 +       return err;
20501 +}
20502 +
20503 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20504 +{
20505 +       int err, rerr;
20506 +       aufs_bindex_t bindex;
20507 +       unsigned char diropq;
20508 +       struct path h_path;
20509 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20510 +       struct inode *h_inode;
20511 +       struct super_block *sb;
20512 +       struct {
20513 +               struct au_pin pin;
20514 +               struct au_dtime dt;
20515 +       } *a; /* reduce the stack usage */
20516 +       struct au_wr_dir_args wr_dir_args = {
20517 +               .force_btgt     = -1,
20518 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20519 +       };
20520 +
20521 +       IMustLock(dir);
20522 +
20523 +       err = -ENOMEM;
20524 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20525 +       if (unlikely(!a))
20526 +               goto out;
20527 +
20528 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20529 +       if (unlikely(err))
20530 +               goto out_free;
20531 +       err = au_d_may_add(dentry);
20532 +       if (unlikely(err))
20533 +               goto out_unlock;
20534 +
20535 +       parent = dentry->d_parent; /* dir inode is locked */
20536 +       di_write_lock_parent(parent);
20537 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20538 +                                     &a->pin, &wr_dir_args);
20539 +       err = PTR_ERR(wh_dentry);
20540 +       if (IS_ERR(wh_dentry))
20541 +               goto out_parent;
20542 +
20543 +       sb = dentry->d_sb;
20544 +       bindex = au_dbtop(dentry);
20545 +       h_path.dentry = au_h_dptr(dentry, bindex);
20546 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20547 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20548 +       if (unlikely(err))
20549 +               goto out_unpin;
20550 +
20551 +       /* make the dir opaque */
20552 +       diropq = 0;
20553 +       h_inode = d_inode(h_path.dentry);
20554 +       if (wh_dentry
20555 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20556 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20557 +               opq_dentry = au_diropq_create(dentry, bindex);
20558 +               inode_unlock(h_inode);
20559 +               err = PTR_ERR(opq_dentry);
20560 +               if (IS_ERR(opq_dentry))
20561 +                       goto out_dir;
20562 +               dput(opq_dentry);
20563 +               diropq = 1;
20564 +       }
20565 +
20566 +       err = epilog(dir, bindex, wh_dentry, dentry);
20567 +       if (!err) {
20568 +               inc_nlink(dir);
20569 +               goto out_unpin; /* success */
20570 +       }
20571 +
20572 +       /* revert */
20573 +       if (diropq) {
20574 +               AuLabel(revert opq);
20575 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20576 +               rerr = au_diropq_remove(dentry, bindex);
20577 +               inode_unlock(h_inode);
20578 +               if (rerr) {
20579 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20580 +                               dentry, err, rerr);
20581 +                       err = -EIO;
20582 +               }
20583 +       }
20584 +
20585 +out_dir:
20586 +       AuLabel(revert dir);
20587 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20588 +       if (rerr) {
20589 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20590 +                       dentry, err, rerr);
20591 +               err = -EIO;
20592 +       }
20593 +       au_dtime_revert(&a->dt);
20594 +out_unpin:
20595 +       au_unpin(&a->pin);
20596 +       dput(wh_dentry);
20597 +out_parent:
20598 +       di_write_unlock(parent);
20599 +out_unlock:
20600 +       if (unlikely(err)) {
20601 +               au_update_dbtop(dentry);
20602 +               d_drop(dentry);
20603 +       }
20604 +       aufs_read_unlock(dentry, AuLock_DW);
20605 +out_free:
20606 +       kfree(a);
20607 +out:
20608 +       return err;
20609 +}
20610 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20611 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20612 +++ linux/fs/aufs/i_op.c        2018-07-19 09:46:13.059981241 +0200
20613 @@ -0,0 +1,1460 @@
20614 +/* SPDX-License-Identifier: GPL-2.0 */
20615 +/*
20616 + * Copyright (C) 2005-2018 Junjiro R. Okajima
20617 + *
20618 + * This program, aufs is free software; you can redistribute it and/or modify
20619 + * it under the terms of the GNU General Public License as published by
20620 + * the Free Software Foundation; either version 2 of the License, or
20621 + * (at your option) any later version.
20622 + *
20623 + * This program is distributed in the hope that it will be useful,
20624 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20625 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20626 + * GNU General Public License for more details.
20627 + *
20628 + * You should have received a copy of the GNU General Public License
20629 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20630 + */
20631 +
20632 +/*
20633 + * inode operations (except add/del/rename)
20634 + */
20635 +
20636 +#include <linux/device_cgroup.h>
20637 +#include <linux/fs_stack.h>
20638 +#include <linux/namei.h>
20639 +#include <linux/security.h>
20640 +#include "aufs.h"
20641 +
20642 +static int h_permission(struct inode *h_inode, int mask,
20643 +                       struct path *h_path, int brperm)
20644 +{
20645 +       int err;
20646 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20647 +
20648 +       err = -EPERM;
20649 +       if (write_mask && IS_IMMUTABLE(h_inode))
20650 +               goto out;
20651 +
20652 +       err = -EACCES;
20653 +       if (((mask & MAY_EXEC)
20654 +            && S_ISREG(h_inode->i_mode)
20655 +            && (path_noexec(h_path)
20656 +                || !(h_inode->i_mode & S_IXUGO))))
20657 +               goto out;
20658 +
20659 +       /*
20660 +        * - skip the lower fs test in the case of write to ro branch.
20661 +        * - nfs dir permission write check is optimized, but a policy for
20662 +        *   link/rename requires a real check.
20663 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20664 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20665 +        */
20666 +       if ((write_mask && !au_br_writable(brperm))
20667 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20668 +               && write_mask && !(mask & MAY_READ))
20669 +           || !h_inode->i_op->permission) {
20670 +               /* AuLabel(generic_permission); */
20671 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20672 +               err = generic_permission(h_inode, mask);
20673 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20674 +                       err = h_inode->i_op->permission(h_inode, mask);
20675 +               AuTraceErr(err);
20676 +       } else {
20677 +               /* AuLabel(h_inode->permission); */
20678 +               err = h_inode->i_op->permission(h_inode, mask);
20679 +               AuTraceErr(err);
20680 +       }
20681 +
20682 +       if (!err)
20683 +               err = devcgroup_inode_permission(h_inode, mask);
20684 +       if (!err)
20685 +               err = security_inode_permission(h_inode, mask);
20686 +
20687 +#if 0
20688 +       if (!err) {
20689 +               /* todo: do we need to call ima_path_check()? */
20690 +               struct path h_path = {
20691 +                       .dentry =
20692 +                       .mnt    = h_mnt
20693 +               };
20694 +               err = ima_path_check(&h_path,
20695 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20696 +                                    IMA_COUNT_LEAVE);
20697 +       }
20698 +#endif
20699 +
20700 +out:
20701 +       return err;
20702 +}
20703 +
20704 +static int aufs_permission(struct inode *inode, int mask)
20705 +{
20706 +       int err;
20707 +       aufs_bindex_t bindex, bbot;
20708 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20709 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20710 +       struct inode *h_inode;
20711 +       struct super_block *sb;
20712 +       struct au_branch *br;
20713 +
20714 +       /* todo: support rcu-walk? */
20715 +       if (mask & MAY_NOT_BLOCK)
20716 +               return -ECHILD;
20717 +
20718 +       sb = inode->i_sb;
20719 +       si_read_lock(sb, AuLock_FLUSH);
20720 +       ii_read_lock_child(inode);
20721 +#if 0
20722 +       err = au_iigen_test(inode, au_sigen(sb));
20723 +       if (unlikely(err))
20724 +               goto out;
20725 +#endif
20726 +
20727 +       if (!isdir
20728 +           || write_mask
20729 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20730 +               err = au_busy_or_stale();
20731 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20732 +               if (unlikely(!h_inode
20733 +                            || (h_inode->i_mode & S_IFMT)
20734 +                            != (inode->i_mode & S_IFMT)))
20735 +                       goto out;
20736 +
20737 +               err = 0;
20738 +               bindex = au_ibtop(inode);
20739 +               br = au_sbr(sb, bindex);
20740 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20741 +               if (write_mask
20742 +                   && !err
20743 +                   && !special_file(h_inode->i_mode)) {
20744 +                       /* test whether the upper writable branch exists */
20745 +                       err = -EROFS;
20746 +                       for (; bindex >= 0; bindex--)
20747 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20748 +                                       err = 0;
20749 +                                       break;
20750 +                               }
20751 +               }
20752 +               goto out;
20753 +       }
20754 +
20755 +       /* non-write to dir */
20756 +       err = 0;
20757 +       bbot = au_ibbot(inode);
20758 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20759 +               h_inode = au_h_iptr(inode, bindex);
20760 +               if (h_inode) {
20761 +                       err = au_busy_or_stale();
20762 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20763 +                               break;
20764 +
20765 +                       br = au_sbr(sb, bindex);
20766 +                       err = h_permission(h_inode, mask, &br->br_path,
20767 +                                          br->br_perm);
20768 +               }
20769 +       }
20770 +
20771 +out:
20772 +       ii_read_unlock(inode);
20773 +       si_read_unlock(sb);
20774 +       return err;
20775 +}
20776 +
20777 +/* ---------------------------------------------------------------------- */
20778 +
20779 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20780 +                                 unsigned int flags)
20781 +{
20782 +       struct dentry *ret, *parent;
20783 +       struct inode *inode;
20784 +       struct super_block *sb;
20785 +       int err, npositive;
20786 +
20787 +       IMustLock(dir);
20788 +
20789 +       /* todo: support rcu-walk? */
20790 +       ret = ERR_PTR(-ECHILD);
20791 +       if (flags & LOOKUP_RCU)
20792 +               goto out;
20793 +
20794 +       ret = ERR_PTR(-ENAMETOOLONG);
20795 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20796 +               goto out;
20797 +
20798 +       sb = dir->i_sb;
20799 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20800 +       ret = ERR_PTR(err);
20801 +       if (unlikely(err))
20802 +               goto out;
20803 +
20804 +       err = au_di_init(dentry);
20805 +       ret = ERR_PTR(err);
20806 +       if (unlikely(err))
20807 +               goto out_si;
20808 +
20809 +       inode = NULL;
20810 +       npositive = 0; /* suppress a warning */
20811 +       parent = dentry->d_parent; /* dir inode is locked */
20812 +       di_read_lock_parent(parent, AuLock_IR);
20813 +       err = au_alive_dir(parent);
20814 +       if (!err)
20815 +               err = au_digen_test(parent, au_sigen(sb));
20816 +       if (!err) {
20817 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20818 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20819 +                                          AuLkup_ALLOW_NEG);
20820 +               err = npositive;
20821 +       }
20822 +       di_read_unlock(parent, AuLock_IR);
20823 +       ret = ERR_PTR(err);
20824 +       if (unlikely(err < 0))
20825 +               goto out_unlock;
20826 +
20827 +       if (npositive) {
20828 +               inode = au_new_inode(dentry, /*must_new*/0);
20829 +               if (IS_ERR(inode)) {
20830 +                       ret = (void *)inode;
20831 +                       inode = NULL;
20832 +                       goto out_unlock;
20833 +               }
20834 +       }
20835 +
20836 +       if (inode)
20837 +               atomic_inc(&inode->i_count);
20838 +       ret = d_splice_alias(inode, dentry);
20839 +#if 0
20840 +       if (unlikely(d_need_lookup(dentry))) {
20841 +               spin_lock(&dentry->d_lock);
20842 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20843 +               spin_unlock(&dentry->d_lock);
20844 +       } else
20845 +#endif
20846 +       if (inode) {
20847 +               if (!IS_ERR(ret)) {
20848 +                       iput(inode);
20849 +                       if (ret && ret != dentry)
20850 +                               ii_write_unlock(inode);
20851 +               } else {
20852 +                       ii_write_unlock(inode);
20853 +                       iput(inode);
20854 +                       inode = NULL;
20855 +               }
20856 +       }
20857 +
20858 +out_unlock:
20859 +       di_write_unlock(dentry);
20860 +out_si:
20861 +       si_read_unlock(sb);
20862 +out:
20863 +       return ret;
20864 +}
20865 +
20866 +/* ---------------------------------------------------------------------- */
20867 +
20868 +struct aopen_node {
20869 +       struct hlist_bl_node hblist;
20870 +       struct file *file, *h_file;
20871 +};
20872 +
20873 +static int au_do_aopen(struct inode *inode, struct file *file)
20874 +{
20875 +       struct hlist_bl_head *aopen;
20876 +       struct hlist_bl_node *pos;
20877 +       struct aopen_node *node;
20878 +       struct au_do_open_args args = {
20879 +               .aopen  = 1,
20880 +               .open   = au_do_open_nondir
20881 +       };
20882 +
20883 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20884 +       hlist_bl_lock(aopen);
20885 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20886 +               if (node->file == file) {
20887 +                       args.h_file = node->h_file;
20888 +                       break;
20889 +               }
20890 +       hlist_bl_unlock(aopen);
20891 +       /* AuDebugOn(!args.h_file); */
20892 +
20893 +       return au_do_open(file, &args);
20894 +}
20895 +
20896 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20897 +                           struct file *file, unsigned int open_flag,
20898 +                           umode_t create_mode, int *opened)
20899 +{
20900 +       int err, unlocked, h_opened = *opened;
20901 +       unsigned int lkup_flags;
20902 +       struct dentry *parent, *d;
20903 +       struct hlist_bl_head *aopen;
20904 +       struct vfsub_aopen_args args = {
20905 +               .open_flag      = open_flag,
20906 +               .create_mode    = create_mode,
20907 +               .opened         = &h_opened
20908 +       };
20909 +       struct aopen_node aopen_node = {
20910 +               .file   = file
20911 +       };
20912 +
20913 +       IMustLock(dir);
20914 +       AuDbg("open_flag 0%o\n", open_flag);
20915 +       AuDbgDentry(dentry);
20916 +
20917 +       err = 0;
20918 +       if (!au_di(dentry)) {
20919 +               lkup_flags = LOOKUP_OPEN;
20920 +               if (open_flag & O_CREAT)
20921 +                       lkup_flags |= LOOKUP_CREATE;
20922 +               d = aufs_lookup(dir, dentry, lkup_flags);
20923 +               if (IS_ERR(d)) {
20924 +                       err = PTR_ERR(d);
20925 +                       AuTraceErr(err);
20926 +                       goto out;
20927 +               } else if (d) {
20928 +                       /*
20929 +                        * obsoleted dentry found.
20930 +                        * another error will be returned later.
20931 +                        */
20932 +                       d_drop(d);
20933 +                       AuDbgDentry(d);
20934 +                       dput(d);
20935 +               }
20936 +               AuDbgDentry(dentry);
20937 +       }
20938 +
20939 +       if (d_is_positive(dentry)
20940 +           || d_unhashed(dentry)
20941 +           || d_unlinked(dentry)
20942 +           || !(open_flag & O_CREAT))
20943 +               goto out_no_open;
20944 +
20945 +       unlocked = 0;
20946 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20947 +       if (unlikely(err))
20948 +               goto out;
20949 +
20950 +       parent = dentry->d_parent;      /* dir is locked */
20951 +       di_write_lock_parent(parent);
20952 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20953 +       if (unlikely(err))
20954 +               goto out_unlock;
20955 +
20956 +       AuDbgDentry(dentry);
20957 +       if (d_is_positive(dentry))
20958 +               goto out_unlock;
20959 +
20960 +       args.file = get_empty_filp();
20961 +       err = PTR_ERR(args.file);
20962 +       if (IS_ERR(args.file))
20963 +               goto out_unlock;
20964 +
20965 +       args.file->f_flags = file->f_flags;
20966 +       err = au_aopen_or_create(dir, dentry, &args);
20967 +       AuTraceErr(err);
20968 +       AuDbgFile(args.file);
20969 +       if (unlikely(err < 0)) {
20970 +               if (h_opened & FILE_OPENED)
20971 +                       fput(args.file);
20972 +               else
20973 +                       put_filp(args.file);
20974 +               goto out_unlock;
20975 +       }
20976 +       di_write_unlock(parent);
20977 +       di_write_unlock(dentry);
20978 +       unlocked = 1;
20979 +
20980 +       /* some filesystems don't set FILE_CREATED while succeeded? */
20981 +       *opened |= FILE_CREATED;
20982 +       if (h_opened & FILE_OPENED)
20983 +               aopen_node.h_file = args.file;
20984 +       else {
20985 +               put_filp(args.file);
20986 +               args.file = NULL;
20987 +       }
20988 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
20989 +       au_hbl_add(&aopen_node.hblist, aopen);
20990 +       err = finish_open(file, dentry, au_do_aopen, opened);
20991 +       au_hbl_del(&aopen_node.hblist, aopen);
20992 +       AuTraceErr(err);
20993 +       AuDbgFile(file);
20994 +       if (aopen_node.h_file)
20995 +               fput(aopen_node.h_file);
20996 +
20997 +out_unlock:
20998 +       if (unlocked)
20999 +               si_read_unlock(dentry->d_sb);
21000 +       else {
21001 +               di_write_unlock(parent);
21002 +               aufs_read_unlock(dentry, AuLock_DW);
21003 +       }
21004 +       AuDbgDentry(dentry);
21005 +       if (unlikely(err < 0))
21006 +               goto out;
21007 +out_no_open:
21008 +       if (err >= 0 && !(*opened & FILE_CREATED)) {
21009 +               AuLabel(out_no_open);
21010 +               dget(dentry);
21011 +               err = finish_no_open(file, dentry);
21012 +       }
21013 +out:
21014 +       AuDbg("%pd%s%s\n", dentry,
21015 +             (*opened & FILE_CREATED) ? " created" : "",
21016 +             (*opened & FILE_OPENED) ? " opened" : "");
21017 +       AuTraceErr(err);
21018 +       return err;
21019 +}
21020 +
21021 +
21022 +/* ---------------------------------------------------------------------- */
21023 +
21024 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21025 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21026 +                         aufs_bindex_t btop)
21027 +{
21028 +       int err;
21029 +       struct dentry *h_parent;
21030 +       struct inode *h_dir;
21031 +
21032 +       if (add_entry)
21033 +               IMustLock(d_inode(parent));
21034 +       else
21035 +               di_write_lock_parent(parent);
21036 +
21037 +       err = 0;
21038 +       if (!au_h_dptr(parent, bcpup)) {
21039 +               if (btop > bcpup)
21040 +                       err = au_cpup_dirs(dentry, bcpup);
21041 +               else if (btop < bcpup)
21042 +                       err = au_cpdown_dirs(dentry, bcpup);
21043 +               else
21044 +                       BUG();
21045 +       }
21046 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21047 +               h_parent = au_h_dptr(parent, bcpup);
21048 +               h_dir = d_inode(h_parent);
21049 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21050 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21051 +               /* todo: no unlock here */
21052 +               inode_unlock_shared(h_dir);
21053 +
21054 +               AuDbg("bcpup %d\n", bcpup);
21055 +               if (!err) {
21056 +                       if (d_really_is_negative(dentry))
21057 +                               au_set_h_dptr(dentry, btop, NULL);
21058 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21059 +               }
21060 +       }
21061 +
21062 +       if (!add_entry)
21063 +               di_write_unlock(parent);
21064 +       if (!err)
21065 +               err = bcpup; /* success */
21066 +
21067 +       AuTraceErr(err);
21068 +       return err;
21069 +}
21070 +
21071 +/*
21072 + * decide the branch and the parent dir where we will create a new entry.
21073 + * returns new bindex or an error.
21074 + * copyup the parent dir if needed.
21075 + */
21076 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21077 +             struct au_wr_dir_args *args)
21078 +{
21079 +       int err;
21080 +       unsigned int flags;
21081 +       aufs_bindex_t bcpup, btop, src_btop;
21082 +       const unsigned char add_entry
21083 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21084 +               | au_ftest_wrdir(args->flags, TMPFILE);
21085 +       struct super_block *sb;
21086 +       struct dentry *parent;
21087 +       struct au_sbinfo *sbinfo;
21088 +
21089 +       sb = dentry->d_sb;
21090 +       sbinfo = au_sbi(sb);
21091 +       parent = dget_parent(dentry);
21092 +       btop = au_dbtop(dentry);
21093 +       bcpup = btop;
21094 +       if (args->force_btgt < 0) {
21095 +               if (src_dentry) {
21096 +                       src_btop = au_dbtop(src_dentry);
21097 +                       if (src_btop < btop)
21098 +                               bcpup = src_btop;
21099 +               } else if (add_entry) {
21100 +                       flags = 0;
21101 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21102 +                               au_fset_wbr(flags, DIR);
21103 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21104 +                       bcpup = err;
21105 +               }
21106 +
21107 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21108 +                       if (add_entry)
21109 +                               err = AuWbrCopyup(sbinfo, dentry);
21110 +                       else {
21111 +                               if (!IS_ROOT(dentry)) {
21112 +                                       di_read_lock_parent(parent, !AuLock_IR);
21113 +                                       err = AuWbrCopyup(sbinfo, dentry);
21114 +                                       di_read_unlock(parent, !AuLock_IR);
21115 +                               } else
21116 +                                       err = AuWbrCopyup(sbinfo, dentry);
21117 +                       }
21118 +                       bcpup = err;
21119 +                       if (unlikely(err < 0))
21120 +                               goto out;
21121 +               }
21122 +       } else {
21123 +               bcpup = args->force_btgt;
21124 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21125 +       }
21126 +
21127 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21128 +       err = bcpup;
21129 +       if (bcpup == btop)
21130 +               goto out; /* success */
21131 +
21132 +       /* copyup the new parent into the branch we process */
21133 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21134 +       if (err >= 0) {
21135 +               if (d_really_is_negative(dentry)) {
21136 +                       au_set_h_dptr(dentry, btop, NULL);
21137 +                       au_set_dbtop(dentry, bcpup);
21138 +                       au_set_dbbot(dentry, bcpup);
21139 +               }
21140 +               AuDebugOn(add_entry
21141 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21142 +                         && !au_h_dptr(dentry, bcpup));
21143 +       }
21144 +
21145 +out:
21146 +       dput(parent);
21147 +       return err;
21148 +}
21149 +
21150 +/* ---------------------------------------------------------------------- */
21151 +
21152 +void au_pin_hdir_unlock(struct au_pin *p)
21153 +{
21154 +       if (p->hdir)
21155 +               au_hn_inode_unlock(p->hdir);
21156 +}
21157 +
21158 +int au_pin_hdir_lock(struct au_pin *p)
21159 +{
21160 +       int err;
21161 +
21162 +       err = 0;
21163 +       if (!p->hdir)
21164 +               goto out;
21165 +
21166 +       /* even if an error happens later, keep this lock */
21167 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21168 +
21169 +       err = -EBUSY;
21170 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21171 +               goto out;
21172 +
21173 +       err = 0;
21174 +       if (p->h_dentry)
21175 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21176 +                                 p->h_parent, p->br);
21177 +
21178 +out:
21179 +       return err;
21180 +}
21181 +
21182 +int au_pin_hdir_relock(struct au_pin *p)
21183 +{
21184 +       int err, i;
21185 +       struct inode *h_i;
21186 +       struct dentry *h_d[] = {
21187 +               p->h_dentry,
21188 +               p->h_parent
21189 +       };
21190 +
21191 +       err = au_pin_hdir_lock(p);
21192 +       if (unlikely(err))
21193 +               goto out;
21194 +
21195 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21196 +               if (!h_d[i])
21197 +                       continue;
21198 +               if (d_is_positive(h_d[i])) {
21199 +                       h_i = d_inode(h_d[i]);
21200 +                       err = !h_i->i_nlink;
21201 +               }
21202 +       }
21203 +
21204 +out:
21205 +       return err;
21206 +}
21207 +
21208 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21209 +{
21210 +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21211 +       p->hdir->hi_inode->i_rwsem.owner = task;
21212 +#endif
21213 +}
21214 +
21215 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21216 +{
21217 +       if (p->hdir) {
21218 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21219 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21220 +               au_pin_hdir_set_owner(p, current);
21221 +       }
21222 +}
21223 +
21224 +void au_pin_hdir_release(struct au_pin *p)
21225 +{
21226 +       if (p->hdir) {
21227 +               au_pin_hdir_set_owner(p, p->task);
21228 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
21229 +       }
21230 +}
21231 +
21232 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21233 +{
21234 +       if (pin && pin->parent)
21235 +               return au_h_dptr(pin->parent, pin->bindex);
21236 +       return NULL;
21237 +}
21238 +
21239 +void au_unpin(struct au_pin *p)
21240 +{
21241 +       if (p->hdir)
21242 +               au_pin_hdir_unlock(p);
21243 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21244 +               vfsub_mnt_drop_write(p->h_mnt);
21245 +       if (!p->hdir)
21246 +               return;
21247 +
21248 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21249 +               di_read_unlock(p->parent, AuLock_IR);
21250 +       iput(p->hdir->hi_inode);
21251 +       dput(p->parent);
21252 +       p->parent = NULL;
21253 +       p->hdir = NULL;
21254 +       p->h_mnt = NULL;
21255 +       /* do not clear p->task */
21256 +}
21257 +
21258 +int au_do_pin(struct au_pin *p)
21259 +{
21260 +       int err;
21261 +       struct super_block *sb;
21262 +       struct inode *h_dir;
21263 +
21264 +       err = 0;
21265 +       sb = p->dentry->d_sb;
21266 +       p->br = au_sbr(sb, p->bindex);
21267 +       if (IS_ROOT(p->dentry)) {
21268 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21269 +                       p->h_mnt = au_br_mnt(p->br);
21270 +                       err = vfsub_mnt_want_write(p->h_mnt);
21271 +                       if (unlikely(err)) {
21272 +                               au_fclr_pin(p->flags, MNT_WRITE);
21273 +                               goto out_err;
21274 +                       }
21275 +               }
21276 +               goto out;
21277 +       }
21278 +
21279 +       p->h_dentry = NULL;
21280 +       if (p->bindex <= au_dbbot(p->dentry))
21281 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21282 +
21283 +       p->parent = dget_parent(p->dentry);
21284 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21285 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21286 +
21287 +       h_dir = NULL;
21288 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21289 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21290 +       if (p->hdir)
21291 +               h_dir = p->hdir->hi_inode;
21292 +
21293 +       /*
21294 +        * udba case, or
21295 +        * if DI_LOCKED is not set, then p->parent may be different
21296 +        * and h_parent can be NULL.
21297 +        */
21298 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21299 +               err = -EBUSY;
21300 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21301 +                       di_read_unlock(p->parent, AuLock_IR);
21302 +               dput(p->parent);
21303 +               p->parent = NULL;
21304 +               goto out_err;
21305 +       }
21306 +
21307 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21308 +               p->h_mnt = au_br_mnt(p->br);
21309 +               err = vfsub_mnt_want_write(p->h_mnt);
21310 +               if (unlikely(err)) {
21311 +                       au_fclr_pin(p->flags, MNT_WRITE);
21312 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21313 +                               di_read_unlock(p->parent, AuLock_IR);
21314 +                       dput(p->parent);
21315 +                       p->parent = NULL;
21316 +                       goto out_err;
21317 +               }
21318 +       }
21319 +
21320 +       au_igrab(h_dir);
21321 +       err = au_pin_hdir_lock(p);
21322 +       if (!err)
21323 +               goto out; /* success */
21324 +
21325 +       au_unpin(p);
21326 +
21327 +out_err:
21328 +       pr_err("err %d\n", err);
21329 +       err = au_busy_or_stale();
21330 +out:
21331 +       return err;
21332 +}
21333 +
21334 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21335 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21336 +                unsigned int udba, unsigned char flags)
21337 +{
21338 +       p->dentry = dentry;
21339 +       p->udba = udba;
21340 +       p->lsc_di = lsc_di;
21341 +       p->lsc_hi = lsc_hi;
21342 +       p->flags = flags;
21343 +       p->bindex = bindex;
21344 +
21345 +       p->parent = NULL;
21346 +       p->hdir = NULL;
21347 +       p->h_mnt = NULL;
21348 +
21349 +       p->h_dentry = NULL;
21350 +       p->h_parent = NULL;
21351 +       p->br = NULL;
21352 +       p->task = current;
21353 +}
21354 +
21355 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21356 +          unsigned int udba, unsigned char flags)
21357 +{
21358 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21359 +                   udba, flags);
21360 +       return au_do_pin(pin);
21361 +}
21362 +
21363 +/* ---------------------------------------------------------------------- */
21364 +
21365 +/*
21366 + * ->setattr() and ->getattr() are called in various cases.
21367 + * chmod, stat: dentry is revalidated.
21368 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21369 + *               unhashed.
21370 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21371 + */
21372 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21373 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21374 +{
21375 +       int err;
21376 +       struct dentry *parent;
21377 +
21378 +       err = 0;
21379 +       if (au_digen_test(dentry, sigen)) {
21380 +               parent = dget_parent(dentry);
21381 +               di_read_lock_parent(parent, AuLock_IR);
21382 +               err = au_refresh_dentry(dentry, parent);
21383 +               di_read_unlock(parent, AuLock_IR);
21384 +               dput(parent);
21385 +       }
21386 +
21387 +       AuTraceErr(err);
21388 +       return err;
21389 +}
21390 +
21391 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21392 +                    struct au_icpup_args *a)
21393 +{
21394 +       int err;
21395 +       loff_t sz;
21396 +       aufs_bindex_t btop, ibtop;
21397 +       struct dentry *hi_wh, *parent;
21398 +       struct inode *inode;
21399 +       struct au_wr_dir_args wr_dir_args = {
21400 +               .force_btgt     = -1,
21401 +               .flags          = 0
21402 +       };
21403 +
21404 +       if (d_is_dir(dentry))
21405 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21406 +       /* plink or hi_wh() case */
21407 +       btop = au_dbtop(dentry);
21408 +       inode = d_inode(dentry);
21409 +       ibtop = au_ibtop(inode);
21410 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21411 +               wr_dir_args.force_btgt = ibtop;
21412 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21413 +       if (unlikely(err < 0))
21414 +               goto out;
21415 +       a->btgt = err;
21416 +       if (err != btop)
21417 +               au_fset_icpup(a->flags, DID_CPUP);
21418 +
21419 +       err = 0;
21420 +       a->pin_flags = AuPin_MNT_WRITE;
21421 +       parent = NULL;
21422 +       if (!IS_ROOT(dentry)) {
21423 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21424 +               parent = dget_parent(dentry);
21425 +               di_write_lock_parent(parent);
21426 +       }
21427 +
21428 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21429 +       if (unlikely(err))
21430 +               goto out_parent;
21431 +
21432 +       sz = -1;
21433 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21434 +       a->h_inode = d_inode(a->h_path.dentry);
21435 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21436 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21437 +               if (ia->ia_size < i_size_read(a->h_inode))
21438 +                       sz = ia->ia_size;
21439 +               inode_unlock_shared(a->h_inode);
21440 +       }
21441 +
21442 +       hi_wh = NULL;
21443 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21444 +               hi_wh = au_hi_wh(inode, a->btgt);
21445 +               if (!hi_wh) {
21446 +                       struct au_cp_generic cpg = {
21447 +                               .dentry = dentry,
21448 +                               .bdst   = a->btgt,
21449 +                               .bsrc   = -1,
21450 +                               .len    = sz,
21451 +                               .pin    = &a->pin
21452 +                       };
21453 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21454 +                       if (unlikely(err))
21455 +                               goto out_unlock;
21456 +                       hi_wh = au_hi_wh(inode, a->btgt);
21457 +                       /* todo: revalidate hi_wh? */
21458 +               }
21459 +       }
21460 +
21461 +       if (parent) {
21462 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21463 +               di_downgrade_lock(parent, AuLock_IR);
21464 +               dput(parent);
21465 +               parent = NULL;
21466 +       }
21467 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21468 +               goto out; /* success */
21469 +
21470 +       if (!d_unhashed(dentry)) {
21471 +               struct au_cp_generic cpg = {
21472 +                       .dentry = dentry,
21473 +                       .bdst   = a->btgt,
21474 +                       .bsrc   = btop,
21475 +                       .len    = sz,
21476 +                       .pin    = &a->pin,
21477 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21478 +               };
21479 +               err = au_sio_cpup_simple(&cpg);
21480 +               if (!err)
21481 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21482 +       } else if (!hi_wh)
21483 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21484 +       else
21485 +               a->h_path.dentry = hi_wh; /* do not dget here */
21486 +
21487 +out_unlock:
21488 +       a->h_inode = d_inode(a->h_path.dentry);
21489 +       if (!err)
21490 +               goto out; /* success */
21491 +       au_unpin(&a->pin);
21492 +out_parent:
21493 +       if (parent) {
21494 +               di_write_unlock(parent);
21495 +               dput(parent);
21496 +       }
21497 +out:
21498 +       if (!err)
21499 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21500 +       return err;
21501 +}
21502 +
21503 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21504 +{
21505 +       int err;
21506 +       struct inode *inode, *delegated;
21507 +       struct super_block *sb;
21508 +       struct file *file;
21509 +       struct au_icpup_args *a;
21510 +
21511 +       inode = d_inode(dentry);
21512 +       IMustLock(inode);
21513 +
21514 +       err = setattr_prepare(dentry, ia);
21515 +       if (unlikely(err))
21516 +               goto out;
21517 +
21518 +       err = -ENOMEM;
21519 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21520 +       if (unlikely(!a))
21521 +               goto out;
21522 +
21523 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21524 +               ia->ia_valid &= ~ATTR_MODE;
21525 +
21526 +       file = NULL;
21527 +       sb = dentry->d_sb;
21528 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21529 +       if (unlikely(err))
21530 +               goto out_kfree;
21531 +
21532 +       if (ia->ia_valid & ATTR_FILE) {
21533 +               /* currently ftruncate(2) only */
21534 +               AuDebugOn(!d_is_reg(dentry));
21535 +               file = ia->ia_file;
21536 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21537 +                                           /*fi_lsc*/0);
21538 +               if (unlikely(err))
21539 +                       goto out_si;
21540 +               ia->ia_file = au_hf_top(file);
21541 +               a->udba = AuOpt_UDBA_NONE;
21542 +       } else {
21543 +               /* fchmod() doesn't pass ia_file */
21544 +               a->udba = au_opt_udba(sb);
21545 +               di_write_lock_child(dentry);
21546 +               /* no d_unlinked(), to set UDBA_NONE for root */
21547 +               if (d_unhashed(dentry))
21548 +                       a->udba = AuOpt_UDBA_NONE;
21549 +               if (a->udba != AuOpt_UDBA_NONE) {
21550 +                       AuDebugOn(IS_ROOT(dentry));
21551 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21552 +                       if (unlikely(err))
21553 +                               goto out_dentry;
21554 +               }
21555 +       }
21556 +
21557 +       err = au_pin_and_icpup(dentry, ia, a);
21558 +       if (unlikely(err < 0))
21559 +               goto out_dentry;
21560 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21561 +               ia->ia_file = NULL;
21562 +               ia->ia_valid &= ~ATTR_FILE;
21563 +       }
21564 +
21565 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21566 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21567 +           == (ATTR_MODE | ATTR_CTIME)) {
21568 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21569 +               if (unlikely(err))
21570 +                       goto out_unlock;
21571 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21572 +                  && (ia->ia_valid & ATTR_CTIME)) {
21573 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21574 +               if (unlikely(err))
21575 +                       goto out_unlock;
21576 +       }
21577 +
21578 +       if (ia->ia_valid & ATTR_SIZE) {
21579 +               struct file *f;
21580 +
21581 +               if (ia->ia_size < i_size_read(inode))
21582 +                       /* unmap only */
21583 +                       truncate_setsize(inode, ia->ia_size);
21584 +
21585 +               f = NULL;
21586 +               if (ia->ia_valid & ATTR_FILE)
21587 +                       f = ia->ia_file;
21588 +               inode_unlock(a->h_inode);
21589 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21590 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21591 +       } else {
21592 +               delegated = NULL;
21593 +               while (1) {
21594 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21595 +                       if (delegated) {
21596 +                               err = break_deleg_wait(&delegated);
21597 +                               if (!err)
21598 +                                       continue;
21599 +                       }
21600 +                       break;
21601 +               }
21602 +       }
21603 +       /*
21604 +        * regardless aufs 'acl' option setting.
21605 +        * why don't all acl-aware fs call this func from their ->setattr()?
21606 +        */
21607 +       if (!err && (ia->ia_valid & ATTR_MODE))
21608 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21609 +       if (!err)
21610 +               au_cpup_attr_changeable(inode);
21611 +
21612 +out_unlock:
21613 +       inode_unlock(a->h_inode);
21614 +       au_unpin(&a->pin);
21615 +       if (unlikely(err))
21616 +               au_update_dbtop(dentry);
21617 +out_dentry:
21618 +       di_write_unlock(dentry);
21619 +       if (file) {
21620 +               fi_write_unlock(file);
21621 +               ia->ia_file = file;
21622 +               ia->ia_valid |= ATTR_FILE;
21623 +       }
21624 +out_si:
21625 +       si_read_unlock(sb);
21626 +out_kfree:
21627 +       kfree(a);
21628 +out:
21629 +       AuTraceErr(err);
21630 +       return err;
21631 +}
21632 +
21633 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21634 +static int au_h_path_to_set_attr(struct dentry *dentry,
21635 +                                struct au_icpup_args *a, struct path *h_path)
21636 +{
21637 +       int err;
21638 +       struct super_block *sb;
21639 +
21640 +       sb = dentry->d_sb;
21641 +       a->udba = au_opt_udba(sb);
21642 +       /* no d_unlinked(), to set UDBA_NONE for root */
21643 +       if (d_unhashed(dentry))
21644 +               a->udba = AuOpt_UDBA_NONE;
21645 +       if (a->udba != AuOpt_UDBA_NONE) {
21646 +               AuDebugOn(IS_ROOT(dentry));
21647 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21648 +               if (unlikely(err))
21649 +                       goto out;
21650 +       }
21651 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21652 +       if (unlikely(err < 0))
21653 +               goto out;
21654 +
21655 +       h_path->dentry = a->h_path.dentry;
21656 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21657 +
21658 +out:
21659 +       return err;
21660 +}
21661 +
21662 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21663 +                 struct au_sxattr *arg)
21664 +{
21665 +       int err;
21666 +       struct path h_path;
21667 +       struct super_block *sb;
21668 +       struct au_icpup_args *a;
21669 +       struct inode *h_inode;
21670 +
21671 +       IMustLock(inode);
21672 +
21673 +       err = -ENOMEM;
21674 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21675 +       if (unlikely(!a))
21676 +               goto out;
21677 +
21678 +       sb = dentry->d_sb;
21679 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21680 +       if (unlikely(err))
21681 +               goto out_kfree;
21682 +
21683 +       h_path.dentry = NULL;   /* silence gcc */
21684 +       di_write_lock_child(dentry);
21685 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21686 +       if (unlikely(err))
21687 +               goto out_di;
21688 +
21689 +       inode_unlock(a->h_inode);
21690 +       switch (arg->type) {
21691 +       case AU_XATTR_SET:
21692 +               AuDebugOn(d_is_negative(h_path.dentry));
21693 +               err = vfsub_setxattr(h_path.dentry,
21694 +                                    arg->u.set.name, arg->u.set.value,
21695 +                                    arg->u.set.size, arg->u.set.flags);
21696 +               break;
21697 +       case AU_ACL_SET:
21698 +               err = -EOPNOTSUPP;
21699 +               h_inode = d_inode(h_path.dentry);
21700 +               if (h_inode->i_op->set_acl)
21701 +                       /* this will call posix_acl_update_mode */
21702 +                       err = h_inode->i_op->set_acl(h_inode,
21703 +                                                    arg->u.acl_set.acl,
21704 +                                                    arg->u.acl_set.type);
21705 +               break;
21706 +       }
21707 +       if (!err)
21708 +               au_cpup_attr_timesizes(inode);
21709 +
21710 +       au_unpin(&a->pin);
21711 +       if (unlikely(err))
21712 +               au_update_dbtop(dentry);
21713 +
21714 +out_di:
21715 +       di_write_unlock(dentry);
21716 +       si_read_unlock(sb);
21717 +out_kfree:
21718 +       kfree(a);
21719 +out:
21720 +       AuTraceErr(err);
21721 +       return err;
21722 +}
21723 +#endif
21724 +
21725 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21726 +                            unsigned int nlink)
21727 +{
21728 +       unsigned int n;
21729 +
21730 +       inode->i_mode = st->mode;
21731 +       /* don't i_[ug]id_write() here */
21732 +       inode->i_uid = st->uid;
21733 +       inode->i_gid = st->gid;
21734 +       inode->i_atime = st->atime;
21735 +       inode->i_mtime = st->mtime;
21736 +       inode->i_ctime = st->ctime;
21737 +
21738 +       au_cpup_attr_nlink(inode, /*force*/0);
21739 +       if (S_ISDIR(inode->i_mode)) {
21740 +               n = inode->i_nlink;
21741 +               n -= nlink;
21742 +               n += st->nlink;
21743 +               smp_mb(); /* for i_nlink */
21744 +               /* 0 can happen */
21745 +               set_nlink(inode, n);
21746 +       }
21747 +
21748 +       spin_lock(&inode->i_lock);
21749 +       inode->i_blocks = st->blocks;
21750 +       i_size_write(inode, st->size);
21751 +       spin_unlock(&inode->i_lock);
21752 +}
21753 +
21754 +/*
21755 + * common routine for aufs_getattr() and au_getxattr().
21756 + * returns zero or negative (an error).
21757 + * @dentry will be read-locked in success.
21758 + */
21759 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21760 +                     int locked)
21761 +{
21762 +       int err;
21763 +       unsigned int mnt_flags, sigen;
21764 +       unsigned char udba_none;
21765 +       aufs_bindex_t bindex;
21766 +       struct super_block *sb, *h_sb;
21767 +       struct inode *inode;
21768 +
21769 +       h_path->mnt = NULL;
21770 +       h_path->dentry = NULL;
21771 +
21772 +       err = 0;
21773 +       sb = dentry->d_sb;
21774 +       mnt_flags = au_mntflags(sb);
21775 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21776 +
21777 +       if (unlikely(locked))
21778 +               goto body; /* skip locking dinfo */
21779 +
21780 +       /* support fstat(2) */
21781 +       if (!d_unlinked(dentry) && !udba_none) {
21782 +               sigen = au_sigen(sb);
21783 +               err = au_digen_test(dentry, sigen);
21784 +               if (!err) {
21785 +                       di_read_lock_child(dentry, AuLock_IR);
21786 +                       err = au_dbrange_test(dentry);
21787 +                       if (unlikely(err)) {
21788 +                               di_read_unlock(dentry, AuLock_IR);
21789 +                               goto out;
21790 +                       }
21791 +               } else {
21792 +                       AuDebugOn(IS_ROOT(dentry));
21793 +                       di_write_lock_child(dentry);
21794 +                       err = au_dbrange_test(dentry);
21795 +                       if (!err)
21796 +                               err = au_reval_for_attr(dentry, sigen);
21797 +                       if (!err)
21798 +                               di_downgrade_lock(dentry, AuLock_IR);
21799 +                       else {
21800 +                               di_write_unlock(dentry);
21801 +                               goto out;
21802 +                       }
21803 +               }
21804 +       } else
21805 +               di_read_lock_child(dentry, AuLock_IR);
21806 +
21807 +body:
21808 +       inode = d_inode(dentry);
21809 +       bindex = au_ibtop(inode);
21810 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21811 +       h_sb = h_path->mnt->mnt_sb;
21812 +       if (!force
21813 +           && !au_test_fs_bad_iattr(h_sb)
21814 +           && udba_none)
21815 +               goto out; /* success */
21816 +
21817 +       if (au_dbtop(dentry) == bindex)
21818 +               h_path->dentry = au_h_dptr(dentry, bindex);
21819 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21820 +               h_path->dentry = au_plink_lkup(inode, bindex);
21821 +               if (IS_ERR(h_path->dentry))
21822 +                       /* pretending success */
21823 +                       h_path->dentry = NULL;
21824 +               else
21825 +                       dput(h_path->dentry);
21826 +       }
21827 +
21828 +out:
21829 +       return err;
21830 +}
21831 +
21832 +static int aufs_getattr(const struct path *path, struct kstat *st,
21833 +                       u32 request, unsigned int query)
21834 +{
21835 +       int err;
21836 +       unsigned char positive;
21837 +       struct path h_path;
21838 +       struct dentry *dentry;
21839 +       struct inode *inode;
21840 +       struct super_block *sb;
21841 +
21842 +       dentry = path->dentry;
21843 +       inode = d_inode(dentry);
21844 +       sb = dentry->d_sb;
21845 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21846 +       if (unlikely(err))
21847 +               goto out;
21848 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
21849 +       if (unlikely(err))
21850 +               goto out_si;
21851 +       if (unlikely(!h_path.dentry))
21852 +               /* illegally overlapped or something */
21853 +               goto out_fill; /* pretending success */
21854 +
21855 +       positive = d_is_positive(h_path.dentry);
21856 +       if (positive)
21857 +               /* no vfsub version */
21858 +               err = vfs_getattr(&h_path, st, request, query);
21859 +       if (!err) {
21860 +               if (positive)
21861 +                       au_refresh_iattr(inode, st,
21862 +                                        d_inode(h_path.dentry)->i_nlink);
21863 +               goto out_fill; /* success */
21864 +       }
21865 +       AuTraceErr(err);
21866 +       goto out_di;
21867 +
21868 +out_fill:
21869 +       generic_fillattr(inode, st);
21870 +out_di:
21871 +       di_read_unlock(dentry, AuLock_IR);
21872 +out_si:
21873 +       si_read_unlock(sb);
21874 +out:
21875 +       AuTraceErr(err);
21876 +       return err;
21877 +}
21878 +
21879 +/* ---------------------------------------------------------------------- */
21880 +
21881 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21882 +                                struct delayed_call *done)
21883 +{
21884 +       const char *ret;
21885 +       struct dentry *h_dentry;
21886 +       struct inode *h_inode;
21887 +       int err;
21888 +       aufs_bindex_t bindex;
21889 +
21890 +       ret = NULL; /* suppress a warning */
21891 +       err = -ECHILD;
21892 +       if (!dentry)
21893 +               goto out;
21894 +
21895 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21896 +       if (unlikely(err))
21897 +               goto out;
21898 +
21899 +       err = au_d_hashed_positive(dentry);
21900 +       if (unlikely(err))
21901 +               goto out_unlock;
21902 +
21903 +       err = -EINVAL;
21904 +       inode = d_inode(dentry);
21905 +       bindex = au_ibtop(inode);
21906 +       h_inode = au_h_iptr(inode, bindex);
21907 +       if (unlikely(!h_inode->i_op->get_link))
21908 +               goto out_unlock;
21909 +
21910 +       err = -EBUSY;
21911 +       h_dentry = NULL;
21912 +       if (au_dbtop(dentry) <= bindex) {
21913 +               h_dentry = au_h_dptr(dentry, bindex);
21914 +               if (h_dentry)
21915 +                       dget(h_dentry);
21916 +       }
21917 +       if (!h_dentry) {
21918 +               h_dentry = d_find_any_alias(h_inode);
21919 +               if (IS_ERR(h_dentry)) {
21920 +                       err = PTR_ERR(h_dentry);
21921 +                       goto out_unlock;
21922 +               }
21923 +       }
21924 +       if (unlikely(!h_dentry))
21925 +               goto out_unlock;
21926 +
21927 +       err = 0;
21928 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21929 +       AuDbgDentry(h_dentry);
21930 +       ret = vfs_get_link(h_dentry, done);
21931 +       dput(h_dentry);
21932 +       if (IS_ERR(ret))
21933 +               err = PTR_ERR(ret);
21934 +
21935 +out_unlock:
21936 +       aufs_read_unlock(dentry, AuLock_IR);
21937 +out:
21938 +       if (unlikely(err))
21939 +               ret = ERR_PTR(err);
21940 +       AuTraceErrPtr(ret);
21941 +       return ret;
21942 +}
21943 +
21944 +/* ---------------------------------------------------------------------- */
21945 +
21946 +static int au_is_special(struct inode *inode)
21947 +{
21948 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21949 +}
21950 +
21951 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
21952 +{
21953 +       int err;
21954 +       aufs_bindex_t bindex;
21955 +       struct super_block *sb;
21956 +       struct inode *h_inode;
21957 +       struct vfsmount *h_mnt;
21958 +
21959 +       sb = inode->i_sb;
21960 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21961 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21962 +
21963 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21964 +       lockdep_off();
21965 +       si_read_lock(sb, AuLock_FLUSH);
21966 +       ii_write_lock_child(inode);
21967 +
21968 +       err = 0;
21969 +       bindex = au_ibtop(inode);
21970 +       h_inode = au_h_iptr(inode, bindex);
21971 +       if (!au_test_ro(sb, bindex, inode)) {
21972 +               h_mnt = au_sbr_mnt(sb, bindex);
21973 +               err = vfsub_mnt_want_write(h_mnt);
21974 +               if (!err) {
21975 +                       err = vfsub_update_time(h_inode, ts, flags);
21976 +                       vfsub_mnt_drop_write(h_mnt);
21977 +               }
21978 +       } else if (au_is_special(h_inode)) {
21979 +               /*
21980 +                * Never copy-up here.
21981 +                * These special files may already be opened and used for
21982 +                * communicating. If we copied it up, then the communication
21983 +                * would be corrupted.
21984 +                */
21985 +               AuWarn1("timestamps for i%lu are ignored "
21986 +                       "since it is on readonly branch (hi%lu).\n",
21987 +                       inode->i_ino, h_inode->i_ino);
21988 +       } else if (flags & ~S_ATIME) {
21989 +               err = -EIO;
21990 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21991 +               AuDebugOn(1);
21992 +       }
21993 +
21994 +       if (!err)
21995 +               au_cpup_attr_timesizes(inode);
21996 +       ii_write_unlock(inode);
21997 +       si_read_unlock(sb);
21998 +       lockdep_on();
21999 +
22000 +       if (!err && (flags & S_VERSION))
22001 +               inode_inc_iversion(inode);
22002 +
22003 +       return err;
22004 +}
22005 +
22006 +/* ---------------------------------------------------------------------- */
22007 +
22008 +/* no getattr version will be set by module.c:aufs_init() */
22009 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22010 +       aufs_iop[] = {
22011 +       [AuIop_SYMLINK] = {
22012 +               .permission     = aufs_permission,
22013 +#ifdef CONFIG_FS_POSIX_ACL
22014 +               .get_acl        = aufs_get_acl,
22015 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22016 +#endif
22017 +
22018 +               .setattr        = aufs_setattr,
22019 +               .getattr        = aufs_getattr,
22020 +
22021 +#ifdef CONFIG_AUFS_XATTR
22022 +               .listxattr      = aufs_listxattr,
22023 +#endif
22024 +
22025 +               .get_link       = aufs_get_link,
22026 +
22027 +               /* .update_time = aufs_update_time */
22028 +       },
22029 +       [AuIop_DIR] = {
22030 +               .create         = aufs_create,
22031 +               .lookup         = aufs_lookup,
22032 +               .link           = aufs_link,
22033 +               .unlink         = aufs_unlink,
22034 +               .symlink        = aufs_symlink,
22035 +               .mkdir          = aufs_mkdir,
22036 +               .rmdir          = aufs_rmdir,
22037 +               .mknod          = aufs_mknod,
22038 +               .rename         = aufs_rename,
22039 +
22040 +               .permission     = aufs_permission,
22041 +#ifdef CONFIG_FS_POSIX_ACL
22042 +               .get_acl        = aufs_get_acl,
22043 +               .set_acl        = aufs_set_acl,
22044 +#endif
22045 +
22046 +               .setattr        = aufs_setattr,
22047 +               .getattr        = aufs_getattr,
22048 +
22049 +#ifdef CONFIG_AUFS_XATTR
22050 +               .listxattr      = aufs_listxattr,
22051 +#endif
22052 +
22053 +               .update_time    = aufs_update_time,
22054 +               .atomic_open    = aufs_atomic_open,
22055 +               .tmpfile        = aufs_tmpfile
22056 +       },
22057 +       [AuIop_OTHER] = {
22058 +               .permission     = aufs_permission,
22059 +#ifdef CONFIG_FS_POSIX_ACL
22060 +               .get_acl        = aufs_get_acl,
22061 +               .set_acl        = aufs_set_acl,
22062 +#endif
22063 +
22064 +               .setattr        = aufs_setattr,
22065 +               .getattr        = aufs_getattr,
22066 +
22067 +#ifdef CONFIG_AUFS_XATTR
22068 +               .listxattr      = aufs_listxattr,
22069 +#endif
22070 +
22071 +               .update_time    = aufs_update_time
22072 +       }
22073 +};
22074 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22075 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22076 +++ linux/fs/aufs/i_op_del.c    2018-07-19 09:46:13.059981241 +0200
22077 @@ -0,0 +1,512 @@
22078 +/* SPDX-License-Identifier: GPL-2.0 */
22079 +/*
22080 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22081 + *
22082 + * This program, aufs is free software; you can redistribute it and/or modify
22083 + * it under the terms of the GNU General Public License as published by
22084 + * the Free Software Foundation; either version 2 of the License, or
22085 + * (at your option) any later version.
22086 + *
22087 + * This program is distributed in the hope that it will be useful,
22088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22090 + * GNU General Public License for more details.
22091 + *
22092 + * You should have received a copy of the GNU General Public License
22093 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22094 + */
22095 +
22096 +/*
22097 + * inode operations (del entry)
22098 + */
22099 +
22100 +#include "aufs.h"
22101 +
22102 +/*
22103 + * decide if a new whiteout for @dentry is necessary or not.
22104 + * when it is necessary, prepare the parent dir for the upper branch whose
22105 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22106 + * be done by caller.
22107 + * return value:
22108 + * 0: wh is unnecessary
22109 + * plus: wh is necessary
22110 + * minus: error
22111 + */
22112 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22113 +{
22114 +       int need_wh, err;
22115 +       aufs_bindex_t btop;
22116 +       struct super_block *sb;
22117 +
22118 +       sb = dentry->d_sb;
22119 +       btop = au_dbtop(dentry);
22120 +       if (*bcpup < 0) {
22121 +               *bcpup = btop;
22122 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22123 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22124 +                       *bcpup = err;
22125 +                       if (unlikely(err < 0))
22126 +                               goto out;
22127 +               }
22128 +       } else
22129 +               AuDebugOn(btop < *bcpup
22130 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22131 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22132 +
22133 +       if (*bcpup != btop) {
22134 +               err = au_cpup_dirs(dentry, *bcpup);
22135 +               if (unlikely(err))
22136 +                       goto out;
22137 +               need_wh = 1;
22138 +       } else {
22139 +               struct au_dinfo *dinfo, *tmp;
22140 +
22141 +               need_wh = -ENOMEM;
22142 +               dinfo = au_di(dentry);
22143 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22144 +               if (tmp) {
22145 +                       au_di_cp(tmp, dinfo);
22146 +                       au_di_swap(tmp, dinfo);
22147 +                       /* returns the number of positive dentries */
22148 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22149 +                                                /* AuLkup_IGNORE_PERM */ 0);
22150 +                       au_di_swap(tmp, dinfo);
22151 +                       au_rw_write_unlock(&tmp->di_rwsem);
22152 +                       au_di_free(tmp);
22153 +               }
22154 +       }
22155 +       AuDbg("need_wh %d\n", need_wh);
22156 +       err = need_wh;
22157 +
22158 +out:
22159 +       return err;
22160 +}
22161 +
22162 +/*
22163 + * simple tests for the del-entry operations.
22164 + * following the checks in vfs, plus the parent-child relationship.
22165 + */
22166 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22167 +              struct dentry *h_parent, int isdir)
22168 +{
22169 +       int err;
22170 +       umode_t h_mode;
22171 +       struct dentry *h_dentry, *h_latest;
22172 +       struct inode *h_inode;
22173 +
22174 +       h_dentry = au_h_dptr(dentry, bindex);
22175 +       if (d_really_is_positive(dentry)) {
22176 +               err = -ENOENT;
22177 +               if (unlikely(d_is_negative(h_dentry)))
22178 +                       goto out;
22179 +               h_inode = d_inode(h_dentry);
22180 +               if (unlikely(!h_inode->i_nlink))
22181 +                       goto out;
22182 +
22183 +               h_mode = h_inode->i_mode;
22184 +               if (!isdir) {
22185 +                       err = -EISDIR;
22186 +                       if (unlikely(S_ISDIR(h_mode)))
22187 +                               goto out;
22188 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22189 +                       err = -ENOTDIR;
22190 +                       goto out;
22191 +               }
22192 +       } else {
22193 +               /* rename(2) case */
22194 +               err = -EIO;
22195 +               if (unlikely(d_is_positive(h_dentry)))
22196 +                       goto out;
22197 +       }
22198 +
22199 +       err = -ENOENT;
22200 +       /* expected parent dir is locked */
22201 +       if (unlikely(h_parent != h_dentry->d_parent))
22202 +               goto out;
22203 +       err = 0;
22204 +
22205 +       /*
22206 +        * rmdir a dir may break the consistency on some filesystem.
22207 +        * let's try heavy test.
22208 +        */
22209 +       err = -EACCES;
22210 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22211 +                    && au_test_h_perm(d_inode(h_parent),
22212 +                                      MAY_EXEC | MAY_WRITE)))
22213 +               goto out;
22214 +
22215 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22216 +       err = -EIO;
22217 +       if (IS_ERR(h_latest))
22218 +               goto out;
22219 +       if (h_latest == h_dentry)
22220 +               err = 0;
22221 +       dput(h_latest);
22222 +
22223 +out:
22224 +       return err;
22225 +}
22226 +
22227 +/*
22228 + * decide the branch where we operate for @dentry. the branch index will be set
22229 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
22230 + * dir for reverting.
22231 + * when a new whiteout is necessary, create it.
22232 + */
22233 +static struct dentry*
22234 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22235 +                   struct au_dtime *dt, struct au_pin *pin)
22236 +{
22237 +       struct dentry *wh_dentry;
22238 +       struct super_block *sb;
22239 +       struct path h_path;
22240 +       int err, need_wh;
22241 +       unsigned int udba;
22242 +       aufs_bindex_t bcpup;
22243 +
22244 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22245 +       wh_dentry = ERR_PTR(need_wh);
22246 +       if (unlikely(need_wh < 0))
22247 +               goto out;
22248 +
22249 +       sb = dentry->d_sb;
22250 +       udba = au_opt_udba(sb);
22251 +       bcpup = *rbcpup;
22252 +       err = au_pin(pin, dentry, bcpup, udba,
22253 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22254 +       wh_dentry = ERR_PTR(err);
22255 +       if (unlikely(err))
22256 +               goto out;
22257 +
22258 +       h_path.dentry = au_pinned_h_parent(pin);
22259 +       if (udba != AuOpt_UDBA_NONE
22260 +           && au_dbtop(dentry) == bcpup) {
22261 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22262 +               wh_dentry = ERR_PTR(err);
22263 +               if (unlikely(err))
22264 +                       goto out_unpin;
22265 +       }
22266 +
22267 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22268 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22269 +       wh_dentry = NULL;
22270 +       if (!need_wh)
22271 +               goto out; /* success, no need to create whiteout */
22272 +
22273 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22274 +       if (IS_ERR(wh_dentry))
22275 +               goto out_unpin;
22276 +
22277 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22278 +       goto out; /* success */
22279 +
22280 +out_unpin:
22281 +       au_unpin(pin);
22282 +out:
22283 +       return wh_dentry;
22284 +}
22285 +
22286 +/*
22287 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22288 + * in order to be revertible and save time for removing many child whiteouts
22289 + * under the dir.
22290 + * returns 1 when there are too many child whiteout and caller should remove
22291 + * them asynchronously. returns 0 when the number of children is enough small to
22292 + * remove now or the branch fs is a remote fs.
22293 + * otherwise return an error.
22294 + */
22295 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22296 +                          struct au_nhash *whlist, struct inode *dir)
22297 +{
22298 +       int rmdir_later, err, dirwh;
22299 +       struct dentry *h_dentry;
22300 +       struct super_block *sb;
22301 +       struct inode *inode;
22302 +
22303 +       sb = dentry->d_sb;
22304 +       SiMustAnyLock(sb);
22305 +       h_dentry = au_h_dptr(dentry, bindex);
22306 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22307 +       if (unlikely(err))
22308 +               goto out;
22309 +
22310 +       /* stop monitoring */
22311 +       inode = d_inode(dentry);
22312 +       au_hn_free(au_hi(inode, bindex));
22313 +
22314 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22315 +               dirwh = au_sbi(sb)->si_dirwh;
22316 +               rmdir_later = (dirwh <= 1);
22317 +               if (!rmdir_later)
22318 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22319 +                                                             dirwh);
22320 +               if (rmdir_later)
22321 +                       return rmdir_later;
22322 +       }
22323 +
22324 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22325 +       if (unlikely(err)) {
22326 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22327 +                       h_dentry, bindex, err);
22328 +               err = 0;
22329 +       }
22330 +
22331 +out:
22332 +       AuTraceErr(err);
22333 +       return err;
22334 +}
22335 +
22336 +/*
22337 + * final procedure for deleting a entry.
22338 + * maintain dentry and iattr.
22339 + */
22340 +static void epilog(struct inode *dir, struct dentry *dentry,
22341 +                  aufs_bindex_t bindex)
22342 +{
22343 +       struct inode *inode;
22344 +
22345 +       inode = d_inode(dentry);
22346 +       d_drop(dentry);
22347 +       inode->i_ctime = dir->i_ctime;
22348 +
22349 +       au_dir_ts(dir, bindex);
22350 +       inode_inc_iversion(dir);
22351 +}
22352 +
22353 +/*
22354 + * when an error happened, remove the created whiteout and revert everything.
22355 + */
22356 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22357 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22358 +                    struct dentry *dentry, struct au_dtime *dt)
22359 +{
22360 +       int rerr;
22361 +       struct path h_path = {
22362 +               .dentry = wh_dentry,
22363 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22364 +       };
22365 +
22366 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22367 +       if (!rerr) {
22368 +               au_set_dbwh(dentry, bwh);
22369 +               au_dtime_revert(dt);
22370 +               return 0;
22371 +       }
22372 +
22373 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22374 +       return -EIO;
22375 +}
22376 +
22377 +/* ---------------------------------------------------------------------- */
22378 +
22379 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22380 +{
22381 +       int err;
22382 +       aufs_bindex_t bwh, bindex, btop;
22383 +       struct inode *inode, *h_dir, *delegated;
22384 +       struct dentry *parent, *wh_dentry;
22385 +       /* to reuduce stack size */
22386 +       struct {
22387 +               struct au_dtime dt;
22388 +               struct au_pin pin;
22389 +               struct path h_path;
22390 +       } *a;
22391 +
22392 +       IMustLock(dir);
22393 +
22394 +       err = -ENOMEM;
22395 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22396 +       if (unlikely(!a))
22397 +               goto out;
22398 +
22399 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22400 +       if (unlikely(err))
22401 +               goto out_free;
22402 +       err = au_d_hashed_positive(dentry);
22403 +       if (unlikely(err))
22404 +               goto out_unlock;
22405 +       inode = d_inode(dentry);
22406 +       IMustLock(inode);
22407 +       err = -EISDIR;
22408 +       if (unlikely(d_is_dir(dentry)))
22409 +               goto out_unlock; /* possible? */
22410 +
22411 +       btop = au_dbtop(dentry);
22412 +       bwh = au_dbwh(dentry);
22413 +       bindex = -1;
22414 +       parent = dentry->d_parent; /* dir inode is locked */
22415 +       di_write_lock_parent(parent);
22416 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22417 +                                       &a->pin);
22418 +       err = PTR_ERR(wh_dentry);
22419 +       if (IS_ERR(wh_dentry))
22420 +               goto out_parent;
22421 +
22422 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22423 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22424 +       dget(a->h_path.dentry);
22425 +       if (bindex == btop) {
22426 +               h_dir = au_pinned_h_dir(&a->pin);
22427 +               delegated = NULL;
22428 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22429 +               if (unlikely(err == -EWOULDBLOCK)) {
22430 +                       pr_warn("cannot retry for NFSv4 delegation"
22431 +                               " for an internal unlink\n");
22432 +                       iput(delegated);
22433 +               }
22434 +       } else {
22435 +               /* dir inode is locked */
22436 +               h_dir = d_inode(wh_dentry->d_parent);
22437 +               IMustLock(h_dir);
22438 +               err = 0;
22439 +       }
22440 +
22441 +       if (!err) {
22442 +               vfsub_drop_nlink(inode);
22443 +               epilog(dir, dentry, bindex);
22444 +
22445 +               /* update target timestamps */
22446 +               if (bindex == btop) {
22447 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22448 +                       /*ignore*/
22449 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22450 +               } else
22451 +                       /* todo: this timestamp may be reverted later */
22452 +                       inode->i_ctime = h_dir->i_ctime;
22453 +               goto out_unpin; /* success */
22454 +       }
22455 +
22456 +       /* revert */
22457 +       if (wh_dentry) {
22458 +               int rerr;
22459 +
22460 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22461 +                                &a->dt);
22462 +               if (rerr)
22463 +                       err = rerr;
22464 +       }
22465 +
22466 +out_unpin:
22467 +       au_unpin(&a->pin);
22468 +       dput(wh_dentry);
22469 +       dput(a->h_path.dentry);
22470 +out_parent:
22471 +       di_write_unlock(parent);
22472 +out_unlock:
22473 +       aufs_read_unlock(dentry, AuLock_DW);
22474 +out_free:
22475 +       kfree(a);
22476 +out:
22477 +       return err;
22478 +}
22479 +
22480 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22481 +{
22482 +       int err, rmdir_later;
22483 +       aufs_bindex_t bwh, bindex, btop;
22484 +       struct inode *inode;
22485 +       struct dentry *parent, *wh_dentry, *h_dentry;
22486 +       struct au_whtmp_rmdir *args;
22487 +       /* to reuduce stack size */
22488 +       struct {
22489 +               struct au_dtime dt;
22490 +               struct au_pin pin;
22491 +       } *a;
22492 +
22493 +       IMustLock(dir);
22494 +
22495 +       err = -ENOMEM;
22496 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22497 +       if (unlikely(!a))
22498 +               goto out;
22499 +
22500 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22501 +       if (unlikely(err))
22502 +               goto out_free;
22503 +       err = au_alive_dir(dentry);
22504 +       if (unlikely(err))
22505 +               goto out_unlock;
22506 +       inode = d_inode(dentry);
22507 +       IMustLock(inode);
22508 +       err = -ENOTDIR;
22509 +       if (unlikely(!d_is_dir(dentry)))
22510 +               goto out_unlock; /* possible? */
22511 +
22512 +       err = -ENOMEM;
22513 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22514 +       if (unlikely(!args))
22515 +               goto out_unlock;
22516 +
22517 +       parent = dentry->d_parent; /* dir inode is locked */
22518 +       di_write_lock_parent(parent);
22519 +       err = au_test_empty(dentry, &args->whlist);
22520 +       if (unlikely(err))
22521 +               goto out_parent;
22522 +
22523 +       btop = au_dbtop(dentry);
22524 +       bwh = au_dbwh(dentry);
22525 +       bindex = -1;
22526 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22527 +                                       &a->pin);
22528 +       err = PTR_ERR(wh_dentry);
22529 +       if (IS_ERR(wh_dentry))
22530 +               goto out_parent;
22531 +
22532 +       h_dentry = au_h_dptr(dentry, btop);
22533 +       dget(h_dentry);
22534 +       rmdir_later = 0;
22535 +       if (bindex == btop) {
22536 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22537 +               if (err > 0) {
22538 +                       rmdir_later = err;
22539 +                       err = 0;
22540 +               }
22541 +       } else {
22542 +               /* stop monitoring */
22543 +               au_hn_free(au_hi(inode, btop));
22544 +
22545 +               /* dir inode is locked */
22546 +               IMustLock(d_inode(wh_dentry->d_parent));
22547 +               err = 0;
22548 +       }
22549 +
22550 +       if (!err) {
22551 +               vfsub_dead_dir(inode);
22552 +               au_set_dbdiropq(dentry, -1);
22553 +               epilog(dir, dentry, bindex);
22554 +
22555 +               if (rmdir_later) {
22556 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22557 +                       args = NULL;
22558 +               }
22559 +
22560 +               goto out_unpin; /* success */
22561 +       }
22562 +
22563 +       /* revert */
22564 +       AuLabel(revert);
22565 +       if (wh_dentry) {
22566 +               int rerr;
22567 +
22568 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22569 +                                &a->dt);
22570 +               if (rerr)
22571 +                       err = rerr;
22572 +       }
22573 +
22574 +out_unpin:
22575 +       au_unpin(&a->pin);
22576 +       dput(wh_dentry);
22577 +       dput(h_dentry);
22578 +out_parent:
22579 +       di_write_unlock(parent);
22580 +       if (args)
22581 +               au_whtmp_rmdir_free(args);
22582 +out_unlock:
22583 +       aufs_read_unlock(dentry, AuLock_DW);
22584 +out_free:
22585 +       kfree(a);
22586 +out:
22587 +       AuTraceErr(err);
22588 +       return err;
22589 +}
22590 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22591 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22592 +++ linux/fs/aufs/i_op_ren.c    2018-07-19 09:46:13.059981241 +0200
22593 @@ -0,0 +1,1247 @@
22594 +/* SPDX-License-Identifier: GPL-2.0 */
22595 +/*
22596 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22597 + *
22598 + * This program, aufs is free software; you can redistribute it and/or modify
22599 + * it under the terms of the GNU General Public License as published by
22600 + * the Free Software Foundation; either version 2 of the License, or
22601 + * (at your option) any later version.
22602 + *
22603 + * This program is distributed in the hope that it will be useful,
22604 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22605 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22606 + * GNU General Public License for more details.
22607 + *
22608 + * You should have received a copy of the GNU General Public License
22609 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22610 + */
22611 +
22612 +/*
22613 + * inode operation (rename entry)
22614 + * todo: this is crazy monster
22615 + */
22616 +
22617 +#include "aufs.h"
22618 +
22619 +enum { AuSRC, AuDST, AuSrcDst };
22620 +enum { AuPARENT, AuCHILD, AuParentChild };
22621 +
22622 +#define AuRen_ISDIR_SRC                1
22623 +#define AuRen_ISDIR_DST                (1 << 1)
22624 +#define AuRen_ISSAMEDIR                (1 << 2)
22625 +#define AuRen_WHSRC            (1 << 3)
22626 +#define AuRen_WHDST            (1 << 4)
22627 +#define AuRen_MNT_WRITE                (1 << 5)
22628 +#define AuRen_DT_DSTDIR                (1 << 6)
22629 +#define AuRen_DIROPQ_SRC       (1 << 7)
22630 +#define AuRen_DIROPQ_DST       (1 << 8)
22631 +#define AuRen_DIRREN           (1 << 9)
22632 +#define AuRen_DROPPED_SRC      (1 << 10)
22633 +#define AuRen_DROPPED_DST      (1 << 11)
22634 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22635 +#define au_fset_ren(flags, name) \
22636 +       do { (flags) |= AuRen_##name; } while (0)
22637 +#define au_fclr_ren(flags, name) \
22638 +       do { (flags) &= ~AuRen_##name; } while (0)
22639 +
22640 +#ifndef CONFIG_AUFS_DIRREN
22641 +#undef AuRen_DIRREN
22642 +#define AuRen_DIRREN           0
22643 +#endif
22644 +
22645 +struct au_ren_args {
22646 +       struct {
22647 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22648 +                       *wh_dentry;
22649 +               struct inode *dir, *inode;
22650 +               struct au_hinode *hdir, *hinode;
22651 +               struct au_dtime dt[AuParentChild];
22652 +               aufs_bindex_t btop, bdiropq;
22653 +       } sd[AuSrcDst];
22654 +
22655 +#define src_dentry     sd[AuSRC].dentry
22656 +#define src_dir                sd[AuSRC].dir
22657 +#define src_inode      sd[AuSRC].inode
22658 +#define src_h_dentry   sd[AuSRC].h_dentry
22659 +#define src_parent     sd[AuSRC].parent
22660 +#define src_h_parent   sd[AuSRC].h_parent
22661 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22662 +#define src_hdir       sd[AuSRC].hdir
22663 +#define src_hinode     sd[AuSRC].hinode
22664 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22665 +#define src_dt         sd[AuSRC].dt
22666 +#define src_btop       sd[AuSRC].btop
22667 +#define src_bdiropq    sd[AuSRC].bdiropq
22668 +
22669 +#define dst_dentry     sd[AuDST].dentry
22670 +#define dst_dir                sd[AuDST].dir
22671 +#define dst_inode      sd[AuDST].inode
22672 +#define dst_h_dentry   sd[AuDST].h_dentry
22673 +#define dst_parent     sd[AuDST].parent
22674 +#define dst_h_parent   sd[AuDST].h_parent
22675 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22676 +#define dst_hdir       sd[AuDST].hdir
22677 +#define dst_hinode     sd[AuDST].hinode
22678 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22679 +#define dst_dt         sd[AuDST].dt
22680 +#define dst_btop       sd[AuDST].btop
22681 +#define dst_bdiropq    sd[AuDST].bdiropq
22682 +
22683 +       struct dentry *h_trap;
22684 +       struct au_branch *br;
22685 +       struct path h_path;
22686 +       struct au_nhash whlist;
22687 +       aufs_bindex_t btgt, src_bwh;
22688 +
22689 +       struct {
22690 +               unsigned short auren_flags;
22691 +               unsigned char flags;    /* syscall parameter */
22692 +               unsigned char exchange;
22693 +       } __packed;
22694 +
22695 +       struct au_whtmp_rmdir *thargs;
22696 +       struct dentry *h_dst;
22697 +       struct au_hinode *h_root;
22698 +};
22699 +
22700 +/* ---------------------------------------------------------------------- */
22701 +
22702 +/*
22703 + * functions for reverting.
22704 + * when an error happened in a single rename systemcall, we should revert
22705 + * everything as if nothing happened.
22706 + * we don't need to revert the copied-up/down the parent dir since they are
22707 + * harmless.
22708 + */
22709 +
22710 +#define RevertFailure(fmt, ...) do { \
22711 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22712 +               ##__VA_ARGS__, err, rerr); \
22713 +       err = -EIO; \
22714 +} while (0)
22715 +
22716 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22717 +{
22718 +       int rerr;
22719 +       struct dentry *d;
22720 +#define src_or_dst(member) a->sd[idx].member
22721 +
22722 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22723 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22724 +       rerr = au_diropq_remove(d, a->btgt);
22725 +       au_hn_inode_unlock(src_or_dst(hinode));
22726 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22727 +       if (rerr)
22728 +               RevertFailure("remove diropq %pd", d);
22729 +
22730 +#undef src_or_dst_
22731 +}
22732 +
22733 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22734 +{
22735 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22736 +               au_ren_do_rev_diropq(err, a, AuSRC);
22737 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22738 +               au_ren_do_rev_diropq(err, a, AuDST);
22739 +}
22740 +
22741 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22742 +{
22743 +       int rerr;
22744 +       struct inode *delegated;
22745 +
22746 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22747 +                                         a->src_h_parent);
22748 +       rerr = PTR_ERR(a->h_path.dentry);
22749 +       if (IS_ERR(a->h_path.dentry)) {
22750 +               RevertFailure("lkup one %pd", a->src_dentry);
22751 +               return;
22752 +       }
22753 +
22754 +       delegated = NULL;
22755 +       rerr = vfsub_rename(a->dst_h_dir,
22756 +                           au_h_dptr(a->src_dentry, a->btgt),
22757 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22758 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22759 +               pr_warn("cannot retry for NFSv4 delegation"
22760 +                       " for an internal rename\n");
22761 +               iput(delegated);
22762 +       }
22763 +       d_drop(a->h_path.dentry);
22764 +       dput(a->h_path.dentry);
22765 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22766 +       if (rerr)
22767 +               RevertFailure("rename %pd", a->src_dentry);
22768 +}
22769 +
22770 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22771 +{
22772 +       int rerr;
22773 +       struct inode *delegated;
22774 +
22775 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22776 +                                         a->dst_h_parent);
22777 +       rerr = PTR_ERR(a->h_path.dentry);
22778 +       if (IS_ERR(a->h_path.dentry)) {
22779 +               RevertFailure("lkup one %pd", a->dst_dentry);
22780 +               return;
22781 +       }
22782 +       if (d_is_positive(a->h_path.dentry)) {
22783 +               d_drop(a->h_path.dentry);
22784 +               dput(a->h_path.dentry);
22785 +               return;
22786 +       }
22787 +
22788 +       delegated = NULL;
22789 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22790 +                           &delegated, a->flags);
22791 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22792 +               pr_warn("cannot retry for NFSv4 delegation"
22793 +                       " for an internal rename\n");
22794 +               iput(delegated);
22795 +       }
22796 +       d_drop(a->h_path.dentry);
22797 +       dput(a->h_path.dentry);
22798 +       if (!rerr)
22799 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22800 +       else
22801 +               RevertFailure("rename %pd", a->h_dst);
22802 +}
22803 +
22804 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22805 +{
22806 +       int rerr;
22807 +
22808 +       a->h_path.dentry = a->src_wh_dentry;
22809 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22810 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22811 +       if (rerr)
22812 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22813 +}
22814 +#undef RevertFailure
22815 +
22816 +/* ---------------------------------------------------------------------- */
22817 +
22818 +/*
22819 + * when we have to copyup the renaming entry, do it with the rename-target name
22820 + * in order to minimize the cost (the later actual rename is unnecessary).
22821 + * otherwise rename it on the target branch.
22822 + */
22823 +static int au_ren_or_cpup(struct au_ren_args *a)
22824 +{
22825 +       int err;
22826 +       struct dentry *d;
22827 +       struct inode *delegated;
22828 +
22829 +       d = a->src_dentry;
22830 +       if (au_dbtop(d) == a->btgt) {
22831 +               a->h_path.dentry = a->dst_h_dentry;
22832 +               AuDebugOn(au_dbtop(d) != a->btgt);
22833 +               delegated = NULL;
22834 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22835 +                                  a->dst_h_dir, &a->h_path, &delegated,
22836 +                                  a->flags);
22837 +               if (unlikely(err == -EWOULDBLOCK)) {
22838 +                       pr_warn("cannot retry for NFSv4 delegation"
22839 +                               " for an internal rename\n");
22840 +                       iput(delegated);
22841 +               }
22842 +       } else
22843 +               BUG();
22844 +
22845 +       if (!err && a->h_dst)
22846 +               /* it will be set to dinfo later */
22847 +               dget(a->h_dst);
22848 +
22849 +       return err;
22850 +}
22851 +
22852 +/* cf. aufs_rmdir() */
22853 +static int au_ren_del_whtmp(struct au_ren_args *a)
22854 +{
22855 +       int err;
22856 +       struct inode *dir;
22857 +
22858 +       dir = a->dst_dir;
22859 +       SiMustAnyLock(dir->i_sb);
22860 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22861 +                                    au_sbi(dir->i_sb)->si_dirwh)
22862 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22863 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22864 +               if (unlikely(err))
22865 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22866 +                               "ignored.\n", a->h_dst, err);
22867 +       } else {
22868 +               au_nhash_wh_free(&a->thargs->whlist);
22869 +               a->thargs->whlist = a->whlist;
22870 +               a->whlist.nh_num = 0;
22871 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22872 +               dput(a->h_dst);
22873 +               a->thargs = NULL;
22874 +       }
22875 +
22876 +       return 0;
22877 +}
22878 +
22879 +/* make it 'opaque' dir. */
22880 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22881 +{
22882 +       int err;
22883 +       struct dentry *d, *diropq;
22884 +#define src_or_dst(member) a->sd[idx].member
22885 +
22886 +       err = 0;
22887 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22888 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22889 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22890 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22891 +       diropq = au_diropq_create(d, a->btgt);
22892 +       au_hn_inode_unlock(src_or_dst(hinode));
22893 +       if (IS_ERR(diropq))
22894 +               err = PTR_ERR(diropq);
22895 +       else
22896 +               dput(diropq);
22897 +
22898 +#undef src_or_dst_
22899 +       return err;
22900 +}
22901 +
22902 +static int au_ren_diropq(struct au_ren_args *a)
22903 +{
22904 +       int err;
22905 +       unsigned char always;
22906 +       struct dentry *d;
22907 +
22908 +       err = 0;
22909 +       d = a->dst_dentry; /* already renamed on the branch */
22910 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22911 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22912 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22913 +           && a->btgt != au_dbdiropq(a->src_dentry)
22914 +           && (a->dst_wh_dentry
22915 +               || a->btgt <= au_dbdiropq(d)
22916 +               /* hide the lower to keep xino */
22917 +               /* the lowers may not be a dir, but we hide them anyway */
22918 +               || a->btgt < au_dbbot(d)
22919 +               || always)) {
22920 +               AuDbg("here\n");
22921 +               err = au_ren_do_diropq(a, AuSRC);
22922 +               if (unlikely(err))
22923 +                       goto out;
22924 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22925 +       }
22926 +       if (!a->exchange)
22927 +               goto out; /* success */
22928 +
22929 +       d = a->src_dentry; /* already renamed on the branch */
22930 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22931 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22932 +           && (a->btgt < au_dbdiropq(d)
22933 +               || a->btgt < au_dbbot(d)
22934 +               || always)) {
22935 +               AuDbgDentry(a->src_dentry);
22936 +               AuDbgDentry(a->dst_dentry);
22937 +               err = au_ren_do_diropq(a, AuDST);
22938 +               if (unlikely(err))
22939 +                       goto out_rev_src;
22940 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22941 +       }
22942 +       goto out; /* success */
22943 +
22944 +out_rev_src:
22945 +       AuDbg("err %d, reverting src\n", err);
22946 +       au_ren_rev_diropq(err, a);
22947 +out:
22948 +       return err;
22949 +}
22950 +
22951 +static int do_rename(struct au_ren_args *a)
22952 +{
22953 +       int err;
22954 +       struct dentry *d, *h_d;
22955 +
22956 +       if (!a->exchange) {
22957 +               /* prepare workqueue args for asynchronous rmdir */
22958 +               h_d = a->dst_h_dentry;
22959 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22960 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22961 +                   && d_is_positive(h_d)) {
22962 +                       err = -ENOMEM;
22963 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22964 +                                                        GFP_NOFS);
22965 +                       if (unlikely(!a->thargs))
22966 +                               goto out;
22967 +                       a->h_dst = dget(h_d);
22968 +               }
22969 +
22970 +               /* create whiteout for src_dentry */
22971 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22972 +                       a->src_bwh = au_dbwh(a->src_dentry);
22973 +                       AuDebugOn(a->src_bwh >= 0);
22974 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22975 +                                                       a->src_h_parent);
22976 +                       err = PTR_ERR(a->src_wh_dentry);
22977 +                       if (IS_ERR(a->src_wh_dentry))
22978 +                               goto out_thargs;
22979 +               }
22980 +
22981 +               /* lookup whiteout for dentry */
22982 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22983 +                       h_d = au_wh_lkup(a->dst_h_parent,
22984 +                                        &a->dst_dentry->d_name, a->br);
22985 +                       err = PTR_ERR(h_d);
22986 +                       if (IS_ERR(h_d))
22987 +                               goto out_whsrc;
22988 +                       if (d_is_negative(h_d))
22989 +                               dput(h_d);
22990 +                       else
22991 +                               a->dst_wh_dentry = h_d;
22992 +               }
22993 +
22994 +               /* rename dentry to tmpwh */
22995 +               if (a->thargs) {
22996 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22997 +                       if (unlikely(err))
22998 +                               goto out_whdst;
22999 +
23000 +                       d = a->dst_dentry;
23001 +                       au_set_h_dptr(d, a->btgt, NULL);
23002 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
23003 +                       if (unlikely(err))
23004 +                               goto out_whtmp;
23005 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
23006 +               }
23007 +       }
23008 +
23009 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23010 +#if 0
23011 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23012 +              && d_is_positive(a->dst_h_dentry)
23013 +              && a->src_btop != a->btgt);
23014 +#endif
23015 +
23016 +       /* rename by vfs_rename or cpup */
23017 +       err = au_ren_or_cpup(a);
23018 +       if (unlikely(err))
23019 +               /* leave the copied-up one */
23020 +               goto out_whtmp;
23021 +
23022 +       /* make dir opaque */
23023 +       err = au_ren_diropq(a);
23024 +       if (unlikely(err))
23025 +               goto out_rename;
23026 +
23027 +       /* update target timestamps */
23028 +       if (a->exchange) {
23029 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23030 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23031 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23032 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23033 +       }
23034 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23035 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23036 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23037 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23038 +
23039 +       if (!a->exchange) {
23040 +               /* remove whiteout for dentry */
23041 +               if (a->dst_wh_dentry) {
23042 +                       a->h_path.dentry = a->dst_wh_dentry;
23043 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23044 +                                                 a->dst_dentry);
23045 +                       if (unlikely(err))
23046 +                               goto out_diropq;
23047 +               }
23048 +
23049 +               /* remove whtmp */
23050 +               if (a->thargs)
23051 +                       au_ren_del_whtmp(a); /* ignore this error */
23052 +
23053 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23054 +       }
23055 +       err = 0;
23056 +       goto out_success;
23057 +
23058 +out_diropq:
23059 +       au_ren_rev_diropq(err, a);
23060 +out_rename:
23061 +       au_ren_rev_rename(err, a);
23062 +       dput(a->h_dst);
23063 +out_whtmp:
23064 +       if (a->thargs)
23065 +               au_ren_rev_whtmp(err, a);
23066 +out_whdst:
23067 +       dput(a->dst_wh_dentry);
23068 +       a->dst_wh_dentry = NULL;
23069 +out_whsrc:
23070 +       if (a->src_wh_dentry)
23071 +               au_ren_rev_whsrc(err, a);
23072 +out_success:
23073 +       dput(a->src_wh_dentry);
23074 +       dput(a->dst_wh_dentry);
23075 +out_thargs:
23076 +       if (a->thargs) {
23077 +               dput(a->h_dst);
23078 +               au_whtmp_rmdir_free(a->thargs);
23079 +               a->thargs = NULL;
23080 +       }
23081 +out:
23082 +       return err;
23083 +}
23084 +
23085 +/* ---------------------------------------------------------------------- */
23086 +
23087 +/*
23088 + * test if @dentry dir can be rename destination or not.
23089 + * success means, it is a logically empty dir.
23090 + */
23091 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23092 +{
23093 +       return au_test_empty(dentry, whlist);
23094 +}
23095 +
23096 +/*
23097 + * test if @a->src_dentry dir can be rename source or not.
23098 + * if it can, return 0.
23099 + * success means,
23100 + * - it is a logically empty dir.
23101 + * - or, it exists on writable branch and has no children including whiteouts
23102 + *   on the lower branch unless DIRREN is on.
23103 + */
23104 +static int may_rename_srcdir(struct au_ren_args *a)
23105 +{
23106 +       int err;
23107 +       unsigned int rdhash;
23108 +       aufs_bindex_t btop, btgt;
23109 +       struct dentry *dentry;
23110 +       struct super_block *sb;
23111 +       struct au_sbinfo *sbinfo;
23112 +
23113 +       dentry = a->src_dentry;
23114 +       sb = dentry->d_sb;
23115 +       sbinfo = au_sbi(sb);
23116 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23117 +               au_fset_ren(a->auren_flags, DIRREN);
23118 +
23119 +       btgt = a->btgt;
23120 +       btop = au_dbtop(dentry);
23121 +       if (btop != btgt) {
23122 +               struct au_nhash whlist;
23123 +
23124 +               SiMustAnyLock(sb);
23125 +               rdhash = sbinfo->si_rdhash;
23126 +               if (!rdhash)
23127 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23128 +                                                          dentry));
23129 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23130 +               if (unlikely(err))
23131 +                       goto out;
23132 +               err = au_test_empty(dentry, &whlist);
23133 +               au_nhash_wh_free(&whlist);
23134 +               goto out;
23135 +       }
23136 +
23137 +       if (btop == au_dbtaildir(dentry))
23138 +               return 0; /* success */
23139 +
23140 +       err = au_test_empty_lower(dentry);
23141 +
23142 +out:
23143 +       if (err == -ENOTEMPTY) {
23144 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23145 +                       err = 0;
23146 +               } else {
23147 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23148 +                               "branches, is not supported\n");
23149 +                       err = -EXDEV;
23150 +               }
23151 +       }
23152 +       return err;
23153 +}
23154 +
23155 +/* side effect: sets whlist and h_dentry */
23156 +static int au_ren_may_dir(struct au_ren_args *a)
23157 +{
23158 +       int err;
23159 +       unsigned int rdhash;
23160 +       struct dentry *d;
23161 +
23162 +       d = a->dst_dentry;
23163 +       SiMustAnyLock(d->d_sb);
23164 +
23165 +       err = 0;
23166 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23167 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23168 +               if (!rdhash)
23169 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23170 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23171 +               if (unlikely(err))
23172 +                       goto out;
23173 +
23174 +               if (!a->exchange) {
23175 +                       au_set_dbtop(d, a->dst_btop);
23176 +                       err = may_rename_dstdir(d, &a->whlist);
23177 +                       au_set_dbtop(d, a->btgt);
23178 +               } else
23179 +                       err = may_rename_srcdir(a);
23180 +       }
23181 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23182 +       if (unlikely(err))
23183 +               goto out;
23184 +
23185 +       d = a->src_dentry;
23186 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23187 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23188 +               err = may_rename_srcdir(a);
23189 +               if (unlikely(err)) {
23190 +                       au_nhash_wh_free(&a->whlist);
23191 +                       a->whlist.nh_num = 0;
23192 +               }
23193 +       }
23194 +out:
23195 +       return err;
23196 +}
23197 +
23198 +/* ---------------------------------------------------------------------- */
23199 +
23200 +/*
23201 + * simple tests for rename.
23202 + * following the checks in vfs, plus the parent-child relationship.
23203 + */
23204 +static int au_may_ren(struct au_ren_args *a)
23205 +{
23206 +       int err, isdir;
23207 +       struct inode *h_inode;
23208 +
23209 +       if (a->src_btop == a->btgt) {
23210 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23211 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23212 +               if (unlikely(err))
23213 +                       goto out;
23214 +               err = -EINVAL;
23215 +               if (unlikely(a->src_h_dentry == a->h_trap))
23216 +                       goto out;
23217 +       }
23218 +
23219 +       err = 0;
23220 +       if (a->dst_btop != a->btgt)
23221 +               goto out;
23222 +
23223 +       err = -ENOTEMPTY;
23224 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23225 +               goto out;
23226 +
23227 +       err = -EIO;
23228 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23229 +       if (d_really_is_negative(a->dst_dentry)) {
23230 +               if (d_is_negative(a->dst_h_dentry))
23231 +                       err = au_may_add(a->dst_dentry, a->btgt,
23232 +                                        a->dst_h_parent, isdir);
23233 +       } else {
23234 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23235 +                       goto out;
23236 +               h_inode = d_inode(a->dst_h_dentry);
23237 +               if (h_inode->i_nlink)
23238 +                       err = au_may_del(a->dst_dentry, a->btgt,
23239 +                                        a->dst_h_parent, isdir);
23240 +       }
23241 +
23242 +out:
23243 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23244 +               err = -EIO;
23245 +       AuTraceErr(err);
23246 +       return err;
23247 +}
23248 +
23249 +/* ---------------------------------------------------------------------- */
23250 +
23251 +/*
23252 + * locking order
23253 + * (VFS)
23254 + * - src_dir and dir by lock_rename()
23255 + * - inode if exitsts
23256 + * (aufs)
23257 + * - lock all
23258 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23259 + *     + si_read_lock
23260 + *     + di_write_lock2_child()
23261 + *       + di_write_lock_child()
23262 + *        + ii_write_lock_child()
23263 + *       + di_write_lock_child2()
23264 + *        + ii_write_lock_child2()
23265 + *     + src_parent and parent
23266 + *       + di_write_lock_parent()
23267 + *        + ii_write_lock_parent()
23268 + *       + di_write_lock_parent2()
23269 + *        + ii_write_lock_parent2()
23270 + *   + lower src_dir and dir by vfsub_lock_rename()
23271 + *   + verify the every relationships between child and parent. if any
23272 + *     of them failed, unlock all and return -EBUSY.
23273 + */
23274 +static void au_ren_unlock(struct au_ren_args *a)
23275 +{
23276 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23277 +                           a->dst_h_parent, a->dst_hdir);
23278 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23279 +           && a->h_root)
23280 +               au_hn_inode_unlock(a->h_root);
23281 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23282 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23283 +}
23284 +
23285 +static int au_ren_lock(struct au_ren_args *a)
23286 +{
23287 +       int err;
23288 +       unsigned int udba;
23289 +
23290 +       err = 0;
23291 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23292 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23293 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23294 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23295 +
23296 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23297 +       if (unlikely(err))
23298 +               goto out;
23299 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23300 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23301 +               struct dentry *root;
23302 +               struct inode *dir;
23303 +
23304 +               /*
23305 +                * sbinfo is already locked, so this ii_read_lock is
23306 +                * unnecessary. but our debugging feature checks it.
23307 +                */
23308 +               root = a->src_inode->i_sb->s_root;
23309 +               if (root != a->src_parent && root != a->dst_parent) {
23310 +                       dir = d_inode(root);
23311 +                       ii_read_lock_parent3(dir);
23312 +                       a->h_root = au_hi(dir, a->btgt);
23313 +                       ii_read_unlock(dir);
23314 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23315 +               }
23316 +       }
23317 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23318 +                                     a->dst_h_parent, a->dst_hdir);
23319 +       udba = au_opt_udba(a->src_dentry->d_sb);
23320 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23321 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23322 +               err = au_busy_or_stale();
23323 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23324 +               err = au_h_verify(a->src_h_dentry, udba,
23325 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23326 +                                 a->br);
23327 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23328 +               err = au_h_verify(a->dst_h_dentry, udba,
23329 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23330 +                                 a->br);
23331 +       if (!err)
23332 +               goto out; /* success */
23333 +
23334 +       err = au_busy_or_stale();
23335 +       au_ren_unlock(a);
23336 +
23337 +out:
23338 +       return err;
23339 +}
23340 +
23341 +/* ---------------------------------------------------------------------- */
23342 +
23343 +static void au_ren_refresh_dir(struct au_ren_args *a)
23344 +{
23345 +       struct inode *dir;
23346 +
23347 +       dir = a->dst_dir;
23348 +       inode_inc_iversion(dir);
23349 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23350 +               /* is this updating defined in POSIX? */
23351 +               au_cpup_attr_timesizes(a->src_inode);
23352 +               au_cpup_attr_nlink(dir, /*force*/1);
23353 +       }
23354 +       au_dir_ts(dir, a->btgt);
23355 +
23356 +       if (a->exchange) {
23357 +               dir = a->src_dir;
23358 +               inode_inc_iversion(dir);
23359 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23360 +                       /* is this updating defined in POSIX? */
23361 +                       au_cpup_attr_timesizes(a->dst_inode);
23362 +                       au_cpup_attr_nlink(dir, /*force*/1);
23363 +               }
23364 +               au_dir_ts(dir, a->btgt);
23365 +       }
23366 +
23367 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23368 +               return;
23369 +
23370 +       dir = a->src_dir;
23371 +       inode_inc_iversion(dir);
23372 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23373 +               au_cpup_attr_nlink(dir, /*force*/1);
23374 +       au_dir_ts(dir, a->btgt);
23375 +}
23376 +
23377 +static void au_ren_refresh(struct au_ren_args *a)
23378 +{
23379 +       aufs_bindex_t bbot, bindex;
23380 +       struct dentry *d, *h_d;
23381 +       struct inode *i, *h_i;
23382 +       struct super_block *sb;
23383 +
23384 +       d = a->dst_dentry;
23385 +       d_drop(d);
23386 +       if (a->h_dst)
23387 +               /* already dget-ed by au_ren_or_cpup() */
23388 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23389 +
23390 +       i = a->dst_inode;
23391 +       if (i) {
23392 +               if (!a->exchange) {
23393 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23394 +                               vfsub_drop_nlink(i);
23395 +                       else {
23396 +                               vfsub_dead_dir(i);
23397 +                               au_cpup_attr_timesizes(i);
23398 +                       }
23399 +                       au_update_dbrange(d, /*do_put_zero*/1);
23400 +               } else
23401 +                       au_cpup_attr_nlink(i, /*force*/1);
23402 +       } else {
23403 +               bbot = a->btgt;
23404 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23405 +                       au_set_h_dptr(d, bindex, NULL);
23406 +               bbot = au_dbbot(d);
23407 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23408 +                       au_set_h_dptr(d, bindex, NULL);
23409 +               au_update_dbrange(d, /*do_put_zero*/0);
23410 +       }
23411 +
23412 +       if (a->exchange
23413 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23414 +               d_drop(a->src_dentry);
23415 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23416 +                       au_set_dbwh(a->src_dentry, -1);
23417 +               return;
23418 +       }
23419 +
23420 +       d = a->src_dentry;
23421 +       au_set_dbwh(d, -1);
23422 +       bbot = au_dbbot(d);
23423 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23424 +               h_d = au_h_dptr(d, bindex);
23425 +               if (h_d)
23426 +                       au_set_h_dptr(d, bindex, NULL);
23427 +       }
23428 +       au_set_dbbot(d, a->btgt);
23429 +
23430 +       sb = d->d_sb;
23431 +       i = a->src_inode;
23432 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23433 +               return; /* success */
23434 +
23435 +       bbot = au_ibbot(i);
23436 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23437 +               h_i = au_h_iptr(i, bindex);
23438 +               if (h_i) {
23439 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23440 +                       /* ignore this error */
23441 +                       au_set_h_iptr(i, bindex, NULL, 0);
23442 +               }
23443 +       }
23444 +       au_set_ibbot(i, a->btgt);
23445 +}
23446 +
23447 +/* ---------------------------------------------------------------------- */
23448 +
23449 +/* mainly for link(2) and rename(2) */
23450 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23451 +{
23452 +       aufs_bindex_t bdiropq, bwh;
23453 +       struct dentry *parent;
23454 +       struct au_branch *br;
23455 +
23456 +       parent = dentry->d_parent;
23457 +       IMustLock(d_inode(parent)); /* dir is locked */
23458 +
23459 +       bdiropq = au_dbdiropq(parent);
23460 +       bwh = au_dbwh(dentry);
23461 +       br = au_sbr(dentry->d_sb, btgt);
23462 +       if (au_br_rdonly(br)
23463 +           || (0 <= bdiropq && bdiropq < btgt)
23464 +           || (0 <= bwh && bwh < btgt))
23465 +               btgt = -1;
23466 +
23467 +       AuDbg("btgt %d\n", btgt);
23468 +       return btgt;
23469 +}
23470 +
23471 +/* sets src_btop, dst_btop and btgt */
23472 +static int au_ren_wbr(struct au_ren_args *a)
23473 +{
23474 +       int err;
23475 +       struct au_wr_dir_args wr_dir_args = {
23476 +               /* .force_btgt  = -1, */
23477 +               .flags          = AuWrDir_ADD_ENTRY
23478 +       };
23479 +
23480 +       a->src_btop = au_dbtop(a->src_dentry);
23481 +       a->dst_btop = au_dbtop(a->dst_dentry);
23482 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23483 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23484 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23485 +       wr_dir_args.force_btgt = a->src_btop;
23486 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23487 +               wr_dir_args.force_btgt = a->dst_btop;
23488 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23489 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23490 +       a->btgt = err;
23491 +       if (a->exchange)
23492 +               au_update_dbtop(a->dst_dentry);
23493 +
23494 +       return err;
23495 +}
23496 +
23497 +static void au_ren_dt(struct au_ren_args *a)
23498 +{
23499 +       a->h_path.dentry = a->src_h_parent;
23500 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23501 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23502 +               a->h_path.dentry = a->dst_h_parent;
23503 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23504 +       }
23505 +
23506 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23507 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23508 +           && !a->exchange)
23509 +               return;
23510 +
23511 +       a->h_path.dentry = a->src_h_dentry;
23512 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23513 +       if (d_is_positive(a->dst_h_dentry)) {
23514 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23515 +               a->h_path.dentry = a->dst_h_dentry;
23516 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23517 +       }
23518 +}
23519 +
23520 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23521 +{
23522 +       struct dentry *h_d;
23523 +       struct inode *h_inode;
23524 +
23525 +       au_dtime_revert(a->src_dt + AuPARENT);
23526 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23527 +               au_dtime_revert(a->dst_dt + AuPARENT);
23528 +
23529 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23530 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23531 +               h_inode = d_inode(h_d);
23532 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23533 +               au_dtime_revert(a->src_dt + AuCHILD);
23534 +               inode_unlock(h_inode);
23535 +
23536 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23537 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23538 +                       h_inode = d_inode(h_d);
23539 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23540 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23541 +                       inode_unlock(h_inode);
23542 +               }
23543 +       }
23544 +}
23545 +
23546 +/* ---------------------------------------------------------------------- */
23547 +
23548 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23549 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23550 +               unsigned int _flags)
23551 +{
23552 +       int err, lock_flags;
23553 +       void *rev;
23554 +       /* reduce stack space */
23555 +       struct au_ren_args *a;
23556 +       struct au_pin pin;
23557 +
23558 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23559 +       IMustLock(_src_dir);
23560 +       IMustLock(_dst_dir);
23561 +
23562 +       err = -EINVAL;
23563 +       if (unlikely(_flags & RENAME_WHITEOUT))
23564 +               goto out;
23565 +
23566 +       err = -ENOMEM;
23567 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23568 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23569 +       if (unlikely(!a))
23570 +               goto out;
23571 +
23572 +       a->flags = _flags;
23573 +       a->exchange = _flags & RENAME_EXCHANGE;
23574 +       a->src_dir = _src_dir;
23575 +       a->src_dentry = _src_dentry;
23576 +       a->src_inode = NULL;
23577 +       if (d_really_is_positive(a->src_dentry))
23578 +               a->src_inode = d_inode(a->src_dentry);
23579 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23580 +       a->dst_dir = _dst_dir;
23581 +       a->dst_dentry = _dst_dentry;
23582 +       a->dst_inode = NULL;
23583 +       if (d_really_is_positive(a->dst_dentry))
23584 +               a->dst_inode = d_inode(a->dst_dentry);
23585 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23586 +       if (a->dst_inode) {
23587 +               /*
23588 +                * if EXCHANGE && src is non-dir && dst is dir,
23589 +                * dst is not locked.
23590 +                */
23591 +               /* IMustLock(a->dst_inode); */
23592 +               au_igrab(a->dst_inode);
23593 +       }
23594 +
23595 +       err = -ENOTDIR;
23596 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23597 +       if (d_is_dir(a->src_dentry)) {
23598 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23599 +               if (unlikely(!a->exchange
23600 +                            && d_really_is_positive(a->dst_dentry)
23601 +                            && !d_is_dir(a->dst_dentry)))
23602 +                       goto out_free;
23603 +               lock_flags |= AuLock_DIRS;
23604 +       }
23605 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23606 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23607 +               if (unlikely(!a->exchange
23608 +                            && d_really_is_positive(a->src_dentry)
23609 +                            && !d_is_dir(a->src_dentry)))
23610 +                       goto out_free;
23611 +               lock_flags |= AuLock_DIRS;
23612 +       }
23613 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23614 +                                       lock_flags);
23615 +       if (unlikely(err))
23616 +               goto out_free;
23617 +
23618 +       err = au_d_hashed_positive(a->src_dentry);
23619 +       if (unlikely(err))
23620 +               goto out_unlock;
23621 +       err = -ENOENT;
23622 +       if (a->dst_inode) {
23623 +               /*
23624 +                * If it is a dir, VFS unhash it before this
23625 +                * function. It means we cannot rely upon d_unhashed().
23626 +                */
23627 +               if (unlikely(!a->dst_inode->i_nlink))
23628 +                       goto out_unlock;
23629 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23630 +                       err = au_d_hashed_positive(a->dst_dentry);
23631 +                       if (unlikely(err && !a->exchange))
23632 +                               goto out_unlock;
23633 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23634 +                       goto out_unlock;
23635 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23636 +               goto out_unlock;
23637 +
23638 +       /*
23639 +        * is it possible?
23640 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23641 +        * there may exist a problem somewhere else.
23642 +        */
23643 +       err = -EINVAL;
23644 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23645 +               goto out_unlock;
23646 +
23647 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23648 +       di_write_lock_parent(a->dst_parent);
23649 +
23650 +       /* which branch we process */
23651 +       err = au_ren_wbr(a);
23652 +       if (unlikely(err < 0))
23653 +               goto out_parent;
23654 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23655 +       a->h_path.mnt = au_br_mnt(a->br);
23656 +
23657 +       /* are they available to be renamed */
23658 +       err = au_ren_may_dir(a);
23659 +       if (unlikely(err))
23660 +               goto out_children;
23661 +
23662 +       /* prepare the writable parent dir on the same branch */
23663 +       if (a->dst_btop == a->btgt) {
23664 +               au_fset_ren(a->auren_flags, WHDST);
23665 +       } else {
23666 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23667 +               if (unlikely(err))
23668 +                       goto out_children;
23669 +       }
23670 +
23671 +       err = 0;
23672 +       if (!a->exchange) {
23673 +               if (a->src_dir != a->dst_dir) {
23674 +                       /*
23675 +                        * this temporary unlock is safe,
23676 +                        * because both dir->i_mutex are locked.
23677 +                        */
23678 +                       di_write_unlock(a->dst_parent);
23679 +                       di_write_lock_parent(a->src_parent);
23680 +                       err = au_wr_dir_need_wh(a->src_dentry,
23681 +                                               au_ftest_ren(a->auren_flags,
23682 +                                                            ISDIR_SRC),
23683 +                                               &a->btgt);
23684 +                       di_write_unlock(a->src_parent);
23685 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23686 +                                             /*isdir*/1);
23687 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23688 +               } else
23689 +                       err = au_wr_dir_need_wh(a->src_dentry,
23690 +                                               au_ftest_ren(a->auren_flags,
23691 +                                                            ISDIR_SRC),
23692 +                                               &a->btgt);
23693 +       }
23694 +       if (unlikely(err < 0))
23695 +               goto out_children;
23696 +       if (err)
23697 +               au_fset_ren(a->auren_flags, WHSRC);
23698 +
23699 +       /* cpup src */
23700 +       if (a->src_btop != a->btgt) {
23701 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23702 +                            au_opt_udba(a->src_dentry->d_sb),
23703 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23704 +               if (!err) {
23705 +                       struct au_cp_generic cpg = {
23706 +                               .dentry = a->src_dentry,
23707 +                               .bdst   = a->btgt,
23708 +                               .bsrc   = a->src_btop,
23709 +                               .len    = -1,
23710 +                               .pin    = &pin,
23711 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23712 +                       };
23713 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23714 +                       err = au_sio_cpup_simple(&cpg);
23715 +                       au_unpin(&pin);
23716 +               }
23717 +               if (unlikely(err))
23718 +                       goto out_children;
23719 +               a->src_btop = a->btgt;
23720 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23721 +               if (!a->exchange)
23722 +                       au_fset_ren(a->auren_flags, WHSRC);
23723 +       }
23724 +
23725 +       /* cpup dst */
23726 +       if (a->exchange && a->dst_inode
23727 +           && a->dst_btop != a->btgt) {
23728 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23729 +                            au_opt_udba(a->dst_dentry->d_sb),
23730 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23731 +               if (!err) {
23732 +                       struct au_cp_generic cpg = {
23733 +                               .dentry = a->dst_dentry,
23734 +                               .bdst   = a->btgt,
23735 +                               .bsrc   = a->dst_btop,
23736 +                               .len    = -1,
23737 +                               .pin    = &pin,
23738 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23739 +                       };
23740 +                       err = au_sio_cpup_simple(&cpg);
23741 +                       au_unpin(&pin);
23742 +               }
23743 +               if (unlikely(err))
23744 +                       goto out_children;
23745 +               a->dst_btop = a->btgt;
23746 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23747 +       }
23748 +
23749 +       /* lock them all */
23750 +       err = au_ren_lock(a);
23751 +       if (unlikely(err))
23752 +               /* leave the copied-up one */
23753 +               goto out_children;
23754 +
23755 +       if (!a->exchange) {
23756 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23757 +                       err = au_may_ren(a);
23758 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23759 +                       err = -ENAMETOOLONG;
23760 +               if (unlikely(err))
23761 +                       goto out_hdir;
23762 +       }
23763 +
23764 +       /* store timestamps to be revertible */
23765 +       au_ren_dt(a);
23766 +
23767 +       /* store dirren info */
23768 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23769 +               err = au_dr_rename(a->src_dentry, a->btgt,
23770 +                                  &a->dst_dentry->d_name, &rev);
23771 +               AuTraceErr(err);
23772 +               if (unlikely(err))
23773 +                       goto out_dt;
23774 +       }
23775 +
23776 +       /* here we go */
23777 +       err = do_rename(a);
23778 +       if (unlikely(err))
23779 +               goto out_dirren;
23780 +
23781 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23782 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23783 +
23784 +       /* update dir attributes */
23785 +       au_ren_refresh_dir(a);
23786 +
23787 +       /* dput/iput all lower dentries */
23788 +       au_ren_refresh(a);
23789 +
23790 +       goto out_hdir; /* success */
23791 +
23792 +out_dirren:
23793 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23794 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23795 +out_dt:
23796 +       au_ren_rev_dt(err, a);
23797 +out_hdir:
23798 +       au_ren_unlock(a);
23799 +out_children:
23800 +       au_nhash_wh_free(&a->whlist);
23801 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23802 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23803 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23804 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23805 +       }
23806 +out_parent:
23807 +       if (!err) {
23808 +               if (d_unhashed(a->src_dentry))
23809 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23810 +               if (d_unhashed(a->dst_dentry))
23811 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23812 +               if (!a->exchange)
23813 +                       d_move(a->src_dentry, a->dst_dentry);
23814 +               else {
23815 +                       d_exchange(a->src_dentry, a->dst_dentry);
23816 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23817 +                               d_drop(a->dst_dentry);
23818 +               }
23819 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23820 +                       d_drop(a->src_dentry);
23821 +       } else {
23822 +               au_update_dbtop(a->dst_dentry);
23823 +               if (!a->dst_inode)
23824 +                       d_drop(a->dst_dentry);
23825 +       }
23826 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23827 +               di_write_unlock(a->dst_parent);
23828 +       else
23829 +               di_write_unlock2(a->src_parent, a->dst_parent);
23830 +out_unlock:
23831 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23832 +out_free:
23833 +       iput(a->dst_inode);
23834 +       if (a->thargs)
23835 +               au_whtmp_rmdir_free(a->thargs);
23836 +       kfree(a);
23837 +out:
23838 +       AuTraceErr(err);
23839 +       return err;
23840 +}
23841 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23842 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23843 +++ linux/fs/aufs/Kconfig       2018-06-04 09:08:09.181412645 +0200
23844 @@ -0,0 +1,199 @@
23845 +# SPDX-License-Identifier: GPL-2.0
23846 +config AUFS_FS
23847 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23848 +       help
23849 +       Aufs is a stackable unification filesystem such as Unionfs,
23850 +       which unifies several directories and provides a merged single
23851 +       directory.
23852 +       In the early days, aufs was entirely re-designed and
23853 +       re-implemented Unionfs Version 1.x series. Introducing many
23854 +       original ideas, approaches and improvements, it becomes totally
23855 +       different from Unionfs while keeping the basic features.
23856 +
23857 +if AUFS_FS
23858 +choice
23859 +       prompt "Maximum number of branches"
23860 +       default AUFS_BRANCH_MAX_127
23861 +       help
23862 +       Specifies the maximum number of branches (or member directories)
23863 +       in a single aufs. The larger value consumes more system
23864 +       resources and has a minor impact to performance.
23865 +config AUFS_BRANCH_MAX_127
23866 +       bool "127"
23867 +       help
23868 +       Specifies the maximum number of branches (or member directories)
23869 +       in a single aufs. The larger value consumes more system
23870 +       resources and has a minor impact to performance.
23871 +config AUFS_BRANCH_MAX_511
23872 +       bool "511"
23873 +       help
23874 +       Specifies the maximum number of branches (or member directories)
23875 +       in a single aufs. The larger value consumes more system
23876 +       resources and has a minor impact to performance.
23877 +config AUFS_BRANCH_MAX_1023
23878 +       bool "1023"
23879 +       help
23880 +       Specifies the maximum number of branches (or member directories)
23881 +       in a single aufs. The larger value consumes more system
23882 +       resources and has a minor impact to performance.
23883 +config AUFS_BRANCH_MAX_32767
23884 +       bool "32767"
23885 +       help
23886 +       Specifies the maximum number of branches (or member directories)
23887 +       in a single aufs. The larger value consumes more system
23888 +       resources and has a minor impact to performance.
23889 +endchoice
23890 +
23891 +config AUFS_SBILIST
23892 +       bool
23893 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23894 +       default y
23895 +       help
23896 +       Automatic configuration for internal use.
23897 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23898 +
23899 +config AUFS_HNOTIFY
23900 +       bool "Detect direct branch access (bypassing aufs)"
23901 +       help
23902 +       If you want to modify files on branches directly, eg. bypassing aufs,
23903 +       and want aufs to detect the changes of them fully, then enable this
23904 +       option and use 'udba=notify' mount option.
23905 +       Currently there is only one available configuration, "fsnotify".
23906 +       It will have a negative impact to the performance.
23907 +       See detail in aufs.5.
23908 +
23909 +choice
23910 +       prompt "method" if AUFS_HNOTIFY
23911 +       default AUFS_HFSNOTIFY
23912 +config AUFS_HFSNOTIFY
23913 +       bool "fsnotify"
23914 +       select FSNOTIFY
23915 +endchoice
23916 +
23917 +config AUFS_EXPORT
23918 +       bool "NFS-exportable aufs"
23919 +       depends on EXPORTFS
23920 +       help
23921 +       If you want to export your mounted aufs via NFS, then enable this
23922 +       option. There are several requirements for this configuration.
23923 +       See detail in aufs.5.
23924 +
23925 +config AUFS_INO_T_64
23926 +       bool
23927 +       depends on AUFS_EXPORT
23928 +       depends on 64BIT && !(ALPHA || S390)
23929 +       default y
23930 +       help
23931 +       Automatic configuration for internal use.
23932 +       /* typedef unsigned long/int __kernel_ino_t */
23933 +       /* alpha and s390x are int */
23934 +
23935 +config AUFS_XATTR
23936 +       bool "support for XATTR/EA (including Security Labels)"
23937 +       help
23938 +       If your branch fs supports XATTR/EA and you want to make them
23939 +       available in aufs too, then enable this opsion and specify the
23940 +       branch attributes for EA.
23941 +       See detail in aufs.5.
23942 +
23943 +config AUFS_FHSM
23944 +       bool "File-based Hierarchical Storage Management"
23945 +       help
23946 +       Hierarchical Storage Management (or HSM) is a well-known feature
23947 +       in the storage world. Aufs provides this feature as file-based.
23948 +       with multiple branches.
23949 +       These multiple branches are prioritized, ie. the topmost one
23950 +       should be the fastest drive and be used heavily.
23951 +
23952 +config AUFS_RDU
23953 +       bool "Readdir in userspace"
23954 +       help
23955 +       Aufs has two methods to provide a merged view for a directory,
23956 +       by a user-space library and by kernel-space natively. The latter
23957 +       is always enabled but sometimes large and slow.
23958 +       If you enable this option, install the library in aufs2-util
23959 +       package, and set some environment variables for your readdir(3),
23960 +       then the work will be handled in user-space which generally
23961 +       shows better performance in most cases.
23962 +       See detail in aufs.5.
23963 +
23964 +config AUFS_DIRREN
23965 +       bool "Workaround for rename(2)-ing a directory"
23966 +       help
23967 +       By default, aufs returns EXDEV error in renameing a dir who has
23968 +       his child on the lower branch, since it is a bad idea to issue
23969 +       rename(2) internally for every lower branch. But user may not
23970 +       accept this behaviour. So here is a workaround to allow such
23971 +       rename(2) and store some extra infromation on the writable
23972 +       branch. Obviously this costs high (and I don't like it).
23973 +       To use this feature, you need to enable this configuration AND
23974 +       to specify the mount option `dirren.'
23975 +       See details in aufs.5 and the design documents.
23976 +
23977 +config AUFS_SHWH
23978 +       bool "Show whiteouts"
23979 +       help
23980 +       If you want to make the whiteouts in aufs visible, then enable
23981 +       this option and specify 'shwh' mount option. Although it may
23982 +       sounds like philosophy or something, but in technically it
23983 +       simply shows the name of whiteout with keeping its behaviour.
23984 +
23985 +config AUFS_BR_RAMFS
23986 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23987 +       help
23988 +       If you want to use ramfs as an aufs branch fs, then enable this
23989 +       option. Generally tmpfs is recommended.
23990 +       Aufs prohibited them to be a branch fs by default, because
23991 +       initramfs becomes unusable after switch_root or something
23992 +       generally. If you sets initramfs as an aufs branch and boot your
23993 +       system by switch_root, you will meet a problem easily since the
23994 +       files in initramfs may be inaccessible.
23995 +       Unless you are going to use ramfs as an aufs branch fs without
23996 +       switch_root or something, leave it N.
23997 +
23998 +config AUFS_BR_FUSE
23999 +       bool "Fuse fs as an aufs branch"
24000 +       depends on FUSE_FS
24001 +       select AUFS_POLL
24002 +       help
24003 +       If you want to use fuse-based userspace filesystem as an aufs
24004 +       branch fs, then enable this option.
24005 +       It implements the internal poll(2) operation which is
24006 +       implemented by fuse only (curretnly).
24007 +
24008 +config AUFS_POLL
24009 +       bool
24010 +       help
24011 +       Automatic configuration for internal use.
24012 +
24013 +config AUFS_BR_HFSPLUS
24014 +       bool "Hfsplus as an aufs branch"
24015 +       depends on HFSPLUS_FS
24016 +       default y
24017 +       help
24018 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24019 +       this option. This option introduces a small overhead at
24020 +       copying-up a file on hfsplus.
24021 +
24022 +config AUFS_BDEV_LOOP
24023 +       bool
24024 +       depends on BLK_DEV_LOOP
24025 +       default y
24026 +       help
24027 +       Automatic configuration for internal use.
24028 +       Convert =[ym] into =y.
24029 +
24030 +config AUFS_DEBUG
24031 +       bool "Debug aufs"
24032 +       help
24033 +       Enable this to compile aufs internal debug code.
24034 +       It will have a negative impact to the performance.
24035 +
24036 +config AUFS_MAGIC_SYSRQ
24037 +       bool
24038 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24039 +       default y
24040 +       help
24041 +       Automatic configuration for internal use.
24042 +       When aufs supports Magic SysRq, enabled automatically.
24043 +endif
24044 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24045 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24046 +++ linux/fs/aufs/loop.c        2018-07-19 09:46:13.059981241 +0200
24047 @@ -0,0 +1,148 @@
24048 +/* SPDX-License-Identifier: GPL-2.0 */
24049 +/*
24050 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24051 + *
24052 + * This program, aufs is free software; you can redistribute it and/or modify
24053 + * it under the terms of the GNU General Public License as published by
24054 + * the Free Software Foundation; either version 2 of the License, or
24055 + * (at your option) any later version.
24056 + *
24057 + * This program is distributed in the hope that it will be useful,
24058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24060 + * GNU General Public License for more details.
24061 + *
24062 + * You should have received a copy of the GNU General Public License
24063 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24064 + */
24065 +
24066 +/*
24067 + * support for loopback block device as a branch
24068 + */
24069 +
24070 +#include "aufs.h"
24071 +
24072 +/* added into drivers/block/loop.c */
24073 +static struct file *(*backing_file_func)(struct super_block *sb);
24074 +
24075 +/*
24076 + * test if two lower dentries have overlapping branches.
24077 + */
24078 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24079 +{
24080 +       struct super_block *h_sb;
24081 +       struct file *backing_file;
24082 +
24083 +       if (unlikely(!backing_file_func)) {
24084 +               /* don't load "loop" module here */
24085 +               backing_file_func = symbol_get(loop_backing_file);
24086 +               if (unlikely(!backing_file_func))
24087 +                       /* "loop" module is not loaded */
24088 +                       return 0;
24089 +       }
24090 +
24091 +       h_sb = h_adding->d_sb;
24092 +       backing_file = backing_file_func(h_sb);
24093 +       if (!backing_file)
24094 +               return 0;
24095 +
24096 +       h_adding = backing_file->f_path.dentry;
24097 +       /*
24098 +        * h_adding can be local NFS.
24099 +        * in this case aufs cannot detect the loop.
24100 +        */
24101 +       if (unlikely(h_adding->d_sb == sb))
24102 +               return 1;
24103 +       return !!au_test_subdir(h_adding, sb->s_root);
24104 +}
24105 +
24106 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24107 +int au_test_loopback_kthread(void)
24108 +{
24109 +       int ret;
24110 +       struct task_struct *tsk = current;
24111 +       char c, comm[sizeof(tsk->comm)];
24112 +
24113 +       ret = 0;
24114 +       if (tsk->flags & PF_KTHREAD) {
24115 +               get_task_comm(comm, tsk);
24116 +               c = comm[4];
24117 +               ret = ('0' <= c && c <= '9'
24118 +                      && !strncmp(comm, "loop", 4));
24119 +       }
24120 +
24121 +       return ret;
24122 +}
24123 +
24124 +/* ---------------------------------------------------------------------- */
24125 +
24126 +#define au_warn_loopback_step  16
24127 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24128 +static unsigned long *au_warn_loopback_array;
24129 +
24130 +void au_warn_loopback(struct super_block *h_sb)
24131 +{
24132 +       int i, new_nelem;
24133 +       unsigned long *a, magic;
24134 +       static DEFINE_SPINLOCK(spin);
24135 +
24136 +       magic = h_sb->s_magic;
24137 +       spin_lock(&spin);
24138 +       a = au_warn_loopback_array;
24139 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24140 +               if (a[i] == magic) {
24141 +                       spin_unlock(&spin);
24142 +                       return;
24143 +               }
24144 +
24145 +       /* h_sb is new to us, print it */
24146 +       if (i < au_warn_loopback_nelem) {
24147 +               a[i] = magic;
24148 +               goto pr;
24149 +       }
24150 +
24151 +       /* expand the array */
24152 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24153 +       a = au_kzrealloc(au_warn_loopback_array,
24154 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24155 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24156 +                        /*may_shrink*/0);
24157 +       if (a) {
24158 +               au_warn_loopback_nelem = new_nelem;
24159 +               au_warn_loopback_array = a;
24160 +               a[i] = magic;
24161 +               goto pr;
24162 +       }
24163 +
24164 +       spin_unlock(&spin);
24165 +       AuWarn1("realloc failed, ignored\n");
24166 +       return;
24167 +
24168 +pr:
24169 +       spin_unlock(&spin);
24170 +       pr_warn("you may want to try another patch for loopback file "
24171 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24172 +}
24173 +
24174 +int au_loopback_init(void)
24175 +{
24176 +       int err;
24177 +       struct super_block *sb __maybe_unused;
24178 +
24179 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
24180 +
24181 +       err = 0;
24182 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24183 +                                        sizeof(unsigned long), GFP_NOFS);
24184 +       if (unlikely(!au_warn_loopback_array))
24185 +               err = -ENOMEM;
24186 +
24187 +       return err;
24188 +}
24189 +
24190 +void au_loopback_fin(void)
24191 +{
24192 +       if (backing_file_func)
24193 +               symbol_put(loop_backing_file);
24194 +       kfree(au_warn_loopback_array);
24195 +}
24196 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24197 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24198 +++ linux/fs/aufs/loop.h        2018-07-19 09:46:13.059981241 +0200
24199 @@ -0,0 +1,53 @@
24200 +/* SPDX-License-Identifier: GPL-2.0 */
24201 +/*
24202 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24203 + *
24204 + * This program, aufs is free software; you can redistribute it and/or modify
24205 + * it under the terms of the GNU General Public License as published by
24206 + * the Free Software Foundation; either version 2 of the License, or
24207 + * (at your option) any later version.
24208 + *
24209 + * This program is distributed in the hope that it will be useful,
24210 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24211 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24212 + * GNU General Public License for more details.
24213 + *
24214 + * You should have received a copy of the GNU General Public License
24215 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24216 + */
24217 +
24218 +/*
24219 + * support for loopback mount as a branch
24220 + */
24221 +
24222 +#ifndef __AUFS_LOOP_H__
24223 +#define __AUFS_LOOP_H__
24224 +
24225 +#ifdef __KERNEL__
24226 +
24227 +struct dentry;
24228 +struct super_block;
24229 +
24230 +#ifdef CONFIG_AUFS_BDEV_LOOP
24231 +/* drivers/block/loop.c */
24232 +struct file *loop_backing_file(struct super_block *sb);
24233 +
24234 +/* loop.c */
24235 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24236 +int au_test_loopback_kthread(void);
24237 +void au_warn_loopback(struct super_block *h_sb);
24238 +
24239 +int au_loopback_init(void);
24240 +void au_loopback_fin(void);
24241 +#else
24242 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24243 +          struct dentry *h_adding)
24244 +AuStubInt0(au_test_loopback_kthread, void)
24245 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24246 +
24247 +AuStubInt0(au_loopback_init, void)
24248 +AuStubVoid(au_loopback_fin, void)
24249 +#endif /* BLK_DEV_LOOP */
24250 +
24251 +#endif /* __KERNEL__ */
24252 +#endif /* __AUFS_LOOP_H__ */
24253 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24254 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24255 +++ linux/fs/aufs/magic.mk      2018-06-04 09:08:09.188079511 +0200
24256 @@ -0,0 +1,31 @@
24257 +# SPDX-License-Identifier: GPL-2.0
24258 +
24259 +# defined in ${srctree}/fs/fuse/inode.c
24260 +# tristate
24261 +ifdef CONFIG_FUSE_FS
24262 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24263 +endif
24264 +
24265 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24266 +# tristate
24267 +ifdef CONFIG_XFS_FS
24268 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24269 +endif
24270 +
24271 +# defined in ${srctree}/fs/configfs/mount.c
24272 +# tristate
24273 +ifdef CONFIG_CONFIGFS_FS
24274 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24275 +endif
24276 +
24277 +# defined in ${srctree}/fs/ubifs/ubifs.h
24278 +# tristate
24279 +ifdef CONFIG_UBIFS_FS
24280 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24281 +endif
24282 +
24283 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24284 +# tristate
24285 +ifdef CONFIG_HFSPLUS_FS
24286 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24287 +endif
24288 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24289 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24290 +++ linux/fs/aufs/Makefile      2018-06-04 09:08:09.181412645 +0200
24291 @@ -0,0 +1,46 @@
24292 +# SPDX-License-Identifier: GPL-2.0
24293 +
24294 +include ${src}/magic.mk
24295 +ifeq (${CONFIG_AUFS_FS},m)
24296 +include ${src}/conf.mk
24297 +endif
24298 +-include ${src}/priv_def.mk
24299 +
24300 +# cf. include/linux/kernel.h
24301 +# enable pr_debug
24302 +ccflags-y += -DDEBUG
24303 +# sparse requires the full pathname
24304 +ifdef M
24305 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24306 +else
24307 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24308 +endif
24309 +
24310 +obj-$(CONFIG_AUFS_FS) += aufs.o
24311 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24312 +       wkq.o vfsub.o dcsub.o \
24313 +       cpup.o whout.o wbr_policy.o \
24314 +       dinfo.o dentry.o \
24315 +       dynop.o \
24316 +       finfo.o file.o f_op.o \
24317 +       dir.o vdir.o \
24318 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24319 +       mvdown.o ioctl.o
24320 +
24321 +# all are boolean
24322 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24323 +aufs-$(CONFIG_SYSFS) += sysfs.o
24324 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24325 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24326 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24327 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24328 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24329 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24330 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24331 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24332 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24333 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24334 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24335 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24336 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24337 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24338 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24339 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24340 +++ linux/fs/aufs/module.c      2018-07-19 09:46:13.059981241 +0200
24341 @@ -0,0 +1,273 @@
24342 +/* SPDX-License-Identifier: GPL-2.0 */
24343 +/*
24344 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24345 + *
24346 + * This program, aufs is free software; you can redistribute it and/or modify
24347 + * it under the terms of the GNU General Public License as published by
24348 + * the Free Software Foundation; either version 2 of the License, or
24349 + * (at your option) any later version.
24350 + *
24351 + * This program is distributed in the hope that it will be useful,
24352 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24353 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24354 + * GNU General Public License for more details.
24355 + *
24356 + * You should have received a copy of the GNU General Public License
24357 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24358 + */
24359 +
24360 +/*
24361 + * module global variables and operations
24362 + */
24363 +
24364 +#include <linux/module.h>
24365 +#include <linux/seq_file.h>
24366 +#include "aufs.h"
24367 +
24368 +/* shrinkable realloc */
24369 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24370 +{
24371 +       size_t sz;
24372 +       int diff;
24373 +
24374 +       sz = 0;
24375 +       diff = -1;
24376 +       if (p) {
24377 +#if 0 /* unused */
24378 +               if (!new_sz) {
24379 +                       kfree(p);
24380 +                       p = NULL;
24381 +                       goto out;
24382 +               }
24383 +#else
24384 +               AuDebugOn(!new_sz);
24385 +#endif
24386 +               sz = ksize(p);
24387 +               diff = au_kmidx_sub(sz, new_sz);
24388 +       }
24389 +       if (sz && !diff)
24390 +               goto out;
24391 +
24392 +       if (sz < new_sz)
24393 +               /* expand or SLOB */
24394 +               p = krealloc(p, new_sz, gfp);
24395 +       else if (new_sz < sz && may_shrink) {
24396 +               /* shrink */
24397 +               void *q;
24398 +
24399 +               q = kmalloc(new_sz, gfp);
24400 +               if (q) {
24401 +                       if (p) {
24402 +                               memcpy(q, p, new_sz);
24403 +                               kfree(p);
24404 +                       }
24405 +                       p = q;
24406 +               } else
24407 +                       p = NULL;
24408 +       }
24409 +
24410 +out:
24411 +       return p;
24412 +}
24413 +
24414 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24415 +                  int may_shrink)
24416 +{
24417 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24418 +       if (p && new_sz > nused)
24419 +               memset(p + nused, 0, new_sz - nused);
24420 +       return p;
24421 +}
24422 +
24423 +/* ---------------------------------------------------------------------- */
24424 +/*
24425 + * aufs caches
24426 + */
24427 +struct kmem_cache *au_cache[AuCache_Last];
24428 +
24429 +static void au_cache_fin(void)
24430 +{
24431 +       int i;
24432 +
24433 +       /*
24434 +        * Make sure all delayed rcu free inodes are flushed before we
24435 +        * destroy cache.
24436 +        */
24437 +       rcu_barrier();
24438 +
24439 +       /* excluding AuCache_HNOTIFY */
24440 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24441 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24442 +               kmem_cache_destroy(au_cache[i]);
24443 +               au_cache[i] = NULL;
24444 +       }
24445 +}
24446 +
24447 +static int __init au_cache_init(void)
24448 +{
24449 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24450 +       if (au_cache[AuCache_DINFO])
24451 +               /* SLAB_DESTROY_BY_RCU */
24452 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24453 +                                                      au_icntnr_init_once);
24454 +       if (au_cache[AuCache_ICNTNR])
24455 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24456 +                                                     au_fi_init_once);
24457 +       if (au_cache[AuCache_FINFO])
24458 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24459 +       if (au_cache[AuCache_VDIR])
24460 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24461 +       if (au_cache[AuCache_DEHSTR])
24462 +               return 0;
24463 +
24464 +       au_cache_fin();
24465 +       return -ENOMEM;
24466 +}
24467 +
24468 +/* ---------------------------------------------------------------------- */
24469 +
24470 +int au_dir_roflags;
24471 +
24472 +#ifdef CONFIG_AUFS_SBILIST
24473 +/*
24474 + * iterate_supers_type() doesn't protect us from
24475 + * remounting (branch management)
24476 + */
24477 +struct hlist_bl_head au_sbilist;
24478 +#endif
24479 +
24480 +/*
24481 + * functions for module interface.
24482 + */
24483 +MODULE_LICENSE("GPL");
24484 +/* MODULE_LICENSE("GPL v2"); */
24485 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24486 +MODULE_DESCRIPTION(AUFS_NAME
24487 +       " -- Advanced multi layered unification filesystem");
24488 +MODULE_VERSION(AUFS_VERSION);
24489 +MODULE_ALIAS_FS(AUFS_NAME);
24490 +
24491 +/* this module parameter has no meaning when SYSFS is disabled */
24492 +int sysaufs_brs = 1;
24493 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24494 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
24495 +
24496 +/* this module parameter has no meaning when USER_NS is disabled */
24497 +bool au_userns;
24498 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24499 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
24500 +
24501 +/* ---------------------------------------------------------------------- */
24502 +
24503 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24504 +
24505 +int au_seq_path(struct seq_file *seq, struct path *path)
24506 +{
24507 +       int err;
24508 +
24509 +       err = seq_path(seq, path, au_esc_chars);
24510 +       if (err >= 0)
24511 +               err = 0;
24512 +       else
24513 +               err = -ENOMEM;
24514 +
24515 +       return err;
24516 +}
24517 +
24518 +/* ---------------------------------------------------------------------- */
24519 +
24520 +static int __init aufs_init(void)
24521 +{
24522 +       int err, i;
24523 +       char *p;
24524 +
24525 +       p = au_esc_chars;
24526 +       for (i = 1; i <= ' '; i++)
24527 +               *p++ = i;
24528 +       *p++ = '\\';
24529 +       *p++ = '\x7f';
24530 +       *p = 0;
24531 +
24532 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24533 +
24534 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24535 +       for (i = 0; i < AuIop_Last; i++)
24536 +               aufs_iop_nogetattr[i].getattr = NULL;
24537 +
24538 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24539 +
24540 +       au_sbilist_init();
24541 +       sysaufs_brs_init();
24542 +       au_debug_init();
24543 +       au_dy_init();
24544 +       err = sysaufs_init();
24545 +       if (unlikely(err))
24546 +               goto out;
24547 +       err = dbgaufs_init();
24548 +       if (unlikely(err))
24549 +               goto out_sysaufs;
24550 +       err = au_procfs_init();
24551 +       if (unlikely(err))
24552 +               goto out_dbgaufs;
24553 +       err = au_wkq_init();
24554 +       if (unlikely(err))
24555 +               goto out_procfs;
24556 +       err = au_loopback_init();
24557 +       if (unlikely(err))
24558 +               goto out_wkq;
24559 +       err = au_hnotify_init();
24560 +       if (unlikely(err))
24561 +               goto out_loopback;
24562 +       err = au_sysrq_init();
24563 +       if (unlikely(err))
24564 +               goto out_hin;
24565 +       err = au_cache_init();
24566 +       if (unlikely(err))
24567 +               goto out_sysrq;
24568 +
24569 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24570 +       err = register_filesystem(&aufs_fs_type);
24571 +       if (unlikely(err))
24572 +               goto out_cache;
24573 +
24574 +       /* since we define pr_fmt, call printk directly */
24575 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24576 +       goto out; /* success */
24577 +
24578 +out_cache:
24579 +       au_cache_fin();
24580 +out_sysrq:
24581 +       au_sysrq_fin();
24582 +out_hin:
24583 +       au_hnotify_fin();
24584 +out_loopback:
24585 +       au_loopback_fin();
24586 +out_wkq:
24587 +       au_wkq_fin();
24588 +out_procfs:
24589 +       au_procfs_fin();
24590 +out_dbgaufs:
24591 +       dbgaufs_fin();
24592 +out_sysaufs:
24593 +       sysaufs_fin();
24594 +       au_dy_fin();
24595 +out:
24596 +       return err;
24597 +}
24598 +
24599 +static void __exit aufs_exit(void)
24600 +{
24601 +       unregister_filesystem(&aufs_fs_type);
24602 +       au_cache_fin();
24603 +       au_sysrq_fin();
24604 +       au_hnotify_fin();
24605 +       au_loopback_fin();
24606 +       au_wkq_fin();
24607 +       au_procfs_fin();
24608 +       dbgaufs_fin();
24609 +       sysaufs_fin();
24610 +       au_dy_fin();
24611 +}
24612 +
24613 +module_init(aufs_init);
24614 +module_exit(aufs_exit);
24615 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24616 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24617 +++ linux/fs/aufs/module.h      2018-07-19 09:46:13.059981241 +0200
24618 @@ -0,0 +1,102 @@
24619 +/* SPDX-License-Identifier: GPL-2.0 */
24620 +/*
24621 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24622 + *
24623 + * This program, aufs is free software; you can redistribute it and/or modify
24624 + * it under the terms of the GNU General Public License as published by
24625 + * the Free Software Foundation; either version 2 of the License, or
24626 + * (at your option) any later version.
24627 + *
24628 + * This program is distributed in the hope that it will be useful,
24629 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24630 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24631 + * GNU General Public License for more details.
24632 + *
24633 + * You should have received a copy of the GNU General Public License
24634 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24635 + */
24636 +
24637 +/*
24638 + * module initialization and module-global
24639 + */
24640 +
24641 +#ifndef __AUFS_MODULE_H__
24642 +#define __AUFS_MODULE_H__
24643 +
24644 +#ifdef __KERNEL__
24645 +
24646 +#include <linux/slab.h>
24647 +
24648 +struct path;
24649 +struct seq_file;
24650 +
24651 +/* module parameters */
24652 +extern int sysaufs_brs;
24653 +extern bool au_userns;
24654 +
24655 +/* ---------------------------------------------------------------------- */
24656 +
24657 +extern int au_dir_roflags;
24658 +
24659 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24660 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24661 +                  int may_shrink);
24662 +
24663 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24664 +{
24665 +#ifndef CONFIG_SLOB
24666 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24667 +#else
24668 +       return -1; /* SLOB is untested */
24669 +#endif
24670 +}
24671 +
24672 +int au_seq_path(struct seq_file *seq, struct path *path);
24673 +
24674 +#ifdef CONFIG_PROC_FS
24675 +/* procfs.c */
24676 +int __init au_procfs_init(void);
24677 +void au_procfs_fin(void);
24678 +#else
24679 +AuStubInt0(au_procfs_init, void);
24680 +AuStubVoid(au_procfs_fin, void);
24681 +#endif
24682 +
24683 +/* ---------------------------------------------------------------------- */
24684 +
24685 +/* kmem cache */
24686 +enum {
24687 +       AuCache_DINFO,
24688 +       AuCache_ICNTNR,
24689 +       AuCache_FINFO,
24690 +       AuCache_VDIR,
24691 +       AuCache_DEHSTR,
24692 +       AuCache_HNOTIFY, /* must be last */
24693 +       AuCache_Last
24694 +};
24695 +
24696 +extern struct kmem_cache *au_cache[AuCache_Last];
24697 +
24698 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24699 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24700 +#define AuCacheCtor(type, ctor)        \
24701 +       kmem_cache_create(#type, sizeof(struct type), \
24702 +                         __alignof__(struct type), AuCacheFlags, ctor)
24703 +
24704 +#define AuCacheFuncs(name, index) \
24705 +static inline struct au_##name *au_cache_alloc_##name(void) \
24706 +{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24707 +static inline void au_cache_free_##name(struct au_##name *p) \
24708 +{ kmem_cache_free(au_cache[AuCache_##index], p); }
24709 +
24710 +AuCacheFuncs(dinfo, DINFO);
24711 +AuCacheFuncs(icntnr, ICNTNR);
24712 +AuCacheFuncs(finfo, FINFO);
24713 +AuCacheFuncs(vdir, VDIR);
24714 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24715 +#ifdef CONFIG_AUFS_HNOTIFY
24716 +AuCacheFuncs(hnotify, HNOTIFY);
24717 +#endif
24718 +
24719 +#endif /* __KERNEL__ */
24720 +#endif /* __AUFS_MODULE_H__ */
24721 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24722 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24723 +++ linux/fs/aufs/mvdown.c      2018-07-19 09:46:13.059981241 +0200
24724 @@ -0,0 +1,705 @@
24725 +/* SPDX-License-Identifier: GPL-2.0 */
24726 +/*
24727 + * Copyright (C) 2011-2018 Junjiro R. Okajima
24728 + *
24729 + * This program, aufs is free software; you can redistribute it and/or modify
24730 + * it under the terms of the GNU General Public License as published by
24731 + * the Free Software Foundation; either version 2 of the License, or
24732 + * (at your option) any later version.
24733 + *
24734 + * This program is distributed in the hope that it will be useful,
24735 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24736 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24737 + * GNU General Public License for more details.
24738 + *
24739 + * You should have received a copy of the GNU General Public License
24740 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24741 + */
24742 +
24743 +/*
24744 + * move-down, opposite of copy-up
24745 + */
24746 +
24747 +#include "aufs.h"
24748 +
24749 +struct au_mvd_args {
24750 +       struct {
24751 +               struct super_block *h_sb;
24752 +               struct dentry *h_parent;
24753 +               struct au_hinode *hdir;
24754 +               struct inode *h_dir, *h_inode;
24755 +               struct au_pin pin;
24756 +       } info[AUFS_MVDOWN_NARRAY];
24757 +
24758 +       struct aufs_mvdown mvdown;
24759 +       struct dentry *dentry, *parent;
24760 +       struct inode *inode, *dir;
24761 +       struct super_block *sb;
24762 +       aufs_bindex_t bopq, bwh, bfound;
24763 +       unsigned char rename_lock;
24764 +};
24765 +
24766 +#define mvd_errno              mvdown.au_errno
24767 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24768 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24769 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24770 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24771 +
24772 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24773 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24774 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24775 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24776 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24777 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24778 +
24779 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24780 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24781 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24782 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24783 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24784 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24785 +
24786 +#define AU_MVD_PR(flag, ...) do {                      \
24787 +               if (flag)                               \
24788 +                       pr_err(__VA_ARGS__);            \
24789 +       } while (0)
24790 +
24791 +static int find_lower_writable(struct au_mvd_args *a)
24792 +{
24793 +       struct super_block *sb;
24794 +       aufs_bindex_t bindex, bbot;
24795 +       struct au_branch *br;
24796 +
24797 +       sb = a->sb;
24798 +       bindex = a->mvd_bsrc;
24799 +       bbot = au_sbbot(sb);
24800 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24801 +               for (bindex++; bindex <= bbot; bindex++) {
24802 +                       br = au_sbr(sb, bindex);
24803 +                       if (au_br_fhsm(br->br_perm)
24804 +                           && !sb_rdonly(au_br_sb(br)))
24805 +                               return bindex;
24806 +               }
24807 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24808 +               for (bindex++; bindex <= bbot; bindex++) {
24809 +                       br = au_sbr(sb, bindex);
24810 +                       if (!au_br_rdonly(br))
24811 +                               return bindex;
24812 +               }
24813 +       else
24814 +               for (bindex++; bindex <= bbot; bindex++) {
24815 +                       br = au_sbr(sb, bindex);
24816 +                       if (!sb_rdonly(au_br_sb(br))) {
24817 +                               if (au_br_rdonly(br))
24818 +                                       a->mvdown.flags
24819 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24820 +                               return bindex;
24821 +                       }
24822 +               }
24823 +
24824 +       return -1;
24825 +}
24826 +
24827 +/* make the parent dir on bdst */
24828 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24829 +{
24830 +       int err;
24831 +
24832 +       err = 0;
24833 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24834 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24835 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24836 +       a->mvd_h_dst_parent = NULL;
24837 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24838 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24839 +       if (!a->mvd_h_dst_parent) {
24840 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24841 +               if (unlikely(err)) {
24842 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24843 +                       goto out;
24844 +               }
24845 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24846 +       }
24847 +
24848 +out:
24849 +       AuTraceErr(err);
24850 +       return err;
24851 +}
24852 +
24853 +/* lock them all */
24854 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24855 +{
24856 +       int err;
24857 +       struct dentry *h_trap;
24858 +
24859 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24860 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
24861 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24862 +                    au_opt_udba(a->sb),
24863 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24864 +       AuTraceErr(err);
24865 +       if (unlikely(err)) {
24866 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
24867 +               goto out;
24868 +       }
24869 +
24870 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24871 +               a->rename_lock = 0;
24872 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24873 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24874 +                           au_opt_udba(a->sb),
24875 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24876 +               err = au_do_pin(&a->mvd_pin_src);
24877 +               AuTraceErr(err);
24878 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24879 +               if (unlikely(err)) {
24880 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
24881 +                       goto out_dst;
24882 +               }
24883 +               goto out; /* success */
24884 +       }
24885 +
24886 +       a->rename_lock = 1;
24887 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
24888 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24889 +                    au_opt_udba(a->sb),
24890 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24891 +       AuTraceErr(err);
24892 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24893 +       if (unlikely(err)) {
24894 +               AU_MVD_PR(dmsg, "pin_src failed\n");
24895 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24896 +               goto out_dst;
24897 +       }
24898 +       au_pin_hdir_unlock(&a->mvd_pin_src);
24899 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24900 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
24901 +       if (h_trap) {
24902 +               err = (h_trap != a->mvd_h_src_parent);
24903 +               if (err)
24904 +                       err = (h_trap != a->mvd_h_dst_parent);
24905 +       }
24906 +       BUG_ON(err); /* it should never happen */
24907 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
24908 +               err = -EBUSY;
24909 +               AuTraceErr(err);
24910 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24911 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
24912 +               au_pin_hdir_lock(&a->mvd_pin_src);
24913 +               au_unpin(&a->mvd_pin_src);
24914 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24915 +               goto out_dst;
24916 +       }
24917 +       goto out; /* success */
24918 +
24919 +out_dst:
24920 +       au_unpin(&a->mvd_pin_dst);
24921 +out:
24922 +       AuTraceErr(err);
24923 +       return err;
24924 +}
24925 +
24926 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
24927 +{
24928 +       if (!a->rename_lock)
24929 +               au_unpin(&a->mvd_pin_src);
24930 +       else {
24931 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
24932 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
24933 +               au_pin_hdir_lock(&a->mvd_pin_src);
24934 +               au_unpin(&a->mvd_pin_src);
24935 +               au_pin_hdir_lock(&a->mvd_pin_dst);
24936 +       }
24937 +       au_unpin(&a->mvd_pin_dst);
24938 +}
24939 +
24940 +/* copy-down the file */
24941 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
24942 +{
24943 +       int err;
24944 +       struct au_cp_generic cpg = {
24945 +               .dentry = a->dentry,
24946 +               .bdst   = a->mvd_bdst,
24947 +               .bsrc   = a->mvd_bsrc,
24948 +               .len    = -1,
24949 +               .pin    = &a->mvd_pin_dst,
24950 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24951 +       };
24952 +
24953 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
24954 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
24955 +               au_fset_cpup(cpg.flags, OVERWRITE);
24956 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
24957 +               au_fset_cpup(cpg.flags, RWDST);
24958 +       err = au_sio_cpdown_simple(&cpg);
24959 +       if (unlikely(err))
24960 +               AU_MVD_PR(dmsg, "cpdown failed\n");
24961 +
24962 +       AuTraceErr(err);
24963 +       return err;
24964 +}
24965 +
24966 +/*
24967 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
24968 + * were sleeping
24969 + */
24970 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
24971 +{
24972 +       int err;
24973 +       struct path h_path;
24974 +       struct au_branch *br;
24975 +       struct inode *delegated;
24976 +
24977 +       br = au_sbr(a->sb, a->mvd_bdst);
24978 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
24979 +       err = PTR_ERR(h_path.dentry);
24980 +       if (IS_ERR(h_path.dentry)) {
24981 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
24982 +               goto out;
24983 +       }
24984 +
24985 +       err = 0;
24986 +       if (d_is_positive(h_path.dentry)) {
24987 +               h_path.mnt = au_br_mnt(br);
24988 +               delegated = NULL;
24989 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
24990 +                                  &delegated, /*force*/0);
24991 +               if (unlikely(err == -EWOULDBLOCK)) {
24992 +                       pr_warn("cannot retry for NFSv4 delegation"
24993 +                               " for an internal unlink\n");
24994 +                       iput(delegated);
24995 +               }
24996 +               if (unlikely(err))
24997 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
24998 +       }
24999 +       dput(h_path.dentry);
25000 +
25001 +out:
25002 +       AuTraceErr(err);
25003 +       return err;
25004 +}
25005 +
25006 +/*
25007 + * unlink the topmost h_dentry
25008 + */
25009 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25010 +{
25011 +       int err;
25012 +       struct path h_path;
25013 +       struct inode *delegated;
25014 +
25015 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25016 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25017 +       delegated = NULL;
25018 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25019 +       if (unlikely(err == -EWOULDBLOCK)) {
25020 +               pr_warn("cannot retry for NFSv4 delegation"
25021 +                       " for an internal unlink\n");
25022 +               iput(delegated);
25023 +       }
25024 +       if (unlikely(err))
25025 +               AU_MVD_PR(dmsg, "unlink failed\n");
25026 +
25027 +       AuTraceErr(err);
25028 +       return err;
25029 +}
25030 +
25031 +/* Since mvdown succeeded, we ignore an error of this function */
25032 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25033 +{
25034 +       int err;
25035 +       struct au_branch *br;
25036 +
25037 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25038 +       br = au_sbr(a->sb, a->mvd_bsrc);
25039 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25040 +       if (!err) {
25041 +               br = au_sbr(a->sb, a->mvd_bdst);
25042 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25043 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25044 +       }
25045 +       if (!err)
25046 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25047 +       else
25048 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25049 +}
25050 +
25051 +/*
25052 + * copy-down the file and unlink the bsrc file.
25053 + * - unlink the bdst whout if exist
25054 + * - copy-down the file (with whtmp name and rename)
25055 + * - unlink the bsrc file
25056 + */
25057 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25058 +{
25059 +       int err;
25060 +
25061 +       err = au_do_mkdir(dmsg, a);
25062 +       if (!err)
25063 +               err = au_do_lock(dmsg, a);
25064 +       if (unlikely(err))
25065 +               goto out;
25066 +
25067 +       /*
25068 +        * do not revert the activities we made on bdst since they should be
25069 +        * harmless in aufs.
25070 +        */
25071 +
25072 +       err = au_do_cpdown(dmsg, a);
25073 +       if (!err)
25074 +               err = au_do_unlink_wh(dmsg, a);
25075 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25076 +               err = au_do_unlink(dmsg, a);
25077 +       if (unlikely(err))
25078 +               goto out_unlock;
25079 +
25080 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25081 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25082 +       if (find_lower_writable(a) < 0)
25083 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25084 +
25085 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25086 +               au_do_stfs(dmsg, a);
25087 +
25088 +       /* maintain internal array */
25089 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25090 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25091 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25092 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25093 +               au_set_ibtop(a->inode, a->mvd_bdst);
25094 +       } else {
25095 +               /* hide the lower */
25096 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25097 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25098 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25099 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25100 +       }
25101 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25102 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25103 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25104 +               au_set_ibbot(a->inode, a->mvd_bdst);
25105 +
25106 +out_unlock:
25107 +       au_do_unlock(dmsg, a);
25108 +out:
25109 +       AuTraceErr(err);
25110 +       return err;
25111 +}
25112 +
25113 +/* ---------------------------------------------------------------------- */
25114 +
25115 +/* make sure the file is idle */
25116 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25117 +{
25118 +       int err, plinked;
25119 +
25120 +       err = 0;
25121 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25122 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25123 +           && au_dcount(a->dentry) == 1
25124 +           && atomic_read(&a->inode->i_count) == 1
25125 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25126 +           && (!plinked || !au_plink_test(a->inode))
25127 +           && a->inode->i_nlink == 1)
25128 +               goto out;
25129 +
25130 +       err = -EBUSY;
25131 +       AU_MVD_PR(dmsg,
25132 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25133 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25134 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25135 +                 a->mvd_h_src_inode->i_nlink,
25136 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25137 +
25138 +out:
25139 +       AuTraceErr(err);
25140 +       return err;
25141 +}
25142 +
25143 +/* make sure the parent dir is fine */
25144 +static int au_mvd_args_parent(const unsigned char dmsg,
25145 +                             struct au_mvd_args *a)
25146 +{
25147 +       int err;
25148 +       aufs_bindex_t bindex;
25149 +
25150 +       err = 0;
25151 +       if (unlikely(au_alive_dir(a->parent))) {
25152 +               err = -ENOENT;
25153 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25154 +               goto out;
25155 +       }
25156 +
25157 +       a->bopq = au_dbdiropq(a->parent);
25158 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25159 +       AuDbg("b%d\n", bindex);
25160 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25161 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25162 +               err = -EINVAL;
25163 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25164 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25165 +                         a->bopq, a->mvd_bdst);
25166 +       }
25167 +
25168 +out:
25169 +       AuTraceErr(err);
25170 +       return err;
25171 +}
25172 +
25173 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25174 +                                   struct au_mvd_args *a)
25175 +{
25176 +       int err;
25177 +       struct au_dinfo *dinfo, *tmp;
25178 +
25179 +       /* lookup the next lower positive entry */
25180 +       err = -ENOMEM;
25181 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25182 +       if (unlikely(!tmp))
25183 +               goto out;
25184 +
25185 +       a->bfound = -1;
25186 +       a->bwh = -1;
25187 +       dinfo = au_di(a->dentry);
25188 +       au_di_cp(tmp, dinfo);
25189 +       au_di_swap(tmp, dinfo);
25190 +
25191 +       /* returns the number of positive dentries */
25192 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25193 +                            /* AuLkup_IGNORE_PERM */ 0);
25194 +       if (!err)
25195 +               a->bwh = au_dbwh(a->dentry);
25196 +       else if (err > 0)
25197 +               a->bfound = au_dbtop(a->dentry);
25198 +
25199 +       au_di_swap(tmp, dinfo);
25200 +       au_rw_write_unlock(&tmp->di_rwsem);
25201 +       au_di_free(tmp);
25202 +       if (unlikely(err < 0))
25203 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25204 +
25205 +       /*
25206 +        * here, we have these cases.
25207 +        * bfound == -1
25208 +        *      no positive dentry under bsrc. there are more sub-cases.
25209 +        *      bwh < 0
25210 +        *              there no whiteout, we can safely move-down.
25211 +        *      bwh <= bsrc
25212 +        *              impossible
25213 +        *      bsrc < bwh && bwh < bdst
25214 +        *              there is a whiteout on RO branch. cannot proceed.
25215 +        *      bwh == bdst
25216 +        *              there is a whiteout on the RW target branch. it should
25217 +        *              be removed.
25218 +        *      bdst < bwh
25219 +        *              there is a whiteout somewhere unrelated branch.
25220 +        * -1 < bfound && bfound <= bsrc
25221 +        *      impossible.
25222 +        * bfound < bdst
25223 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25224 +        *      proceed.
25225 +        * bfound == bdst
25226 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25227 +        *      error.
25228 +        * bdst < bfound
25229 +        *      found, after we create the file on bdst, it will be hidden.
25230 +        */
25231 +
25232 +       AuDebugOn(a->bfound == -1
25233 +                 && a->bwh != -1
25234 +                 && a->bwh <= a->mvd_bsrc);
25235 +       AuDebugOn(-1 < a->bfound
25236 +                 && a->bfound <= a->mvd_bsrc);
25237 +
25238 +       err = -EINVAL;
25239 +       if (a->bfound == -1
25240 +           && a->mvd_bsrc < a->bwh
25241 +           && a->bwh != -1
25242 +           && a->bwh < a->mvd_bdst) {
25243 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25244 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25245 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25246 +               goto out;
25247 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25248 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25249 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25250 +                         a->mvd_bdst, a->bfound);
25251 +               goto out;
25252 +       }
25253 +
25254 +       err = 0; /* success */
25255 +
25256 +out:
25257 +       AuTraceErr(err);
25258 +       return err;
25259 +}
25260 +
25261 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25262 +{
25263 +       int err;
25264 +
25265 +       err = 0;
25266 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25267 +           && a->bfound == a->mvd_bdst)
25268 +               err = -EEXIST;
25269 +       AuTraceErr(err);
25270 +       return err;
25271 +}
25272 +
25273 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25274 +{
25275 +       int err;
25276 +       struct au_branch *br;
25277 +
25278 +       err = -EISDIR;
25279 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25280 +               goto out;
25281 +
25282 +       err = -EINVAL;
25283 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25284 +               a->mvd_bsrc = au_ibtop(a->inode);
25285 +       else {
25286 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25287 +               if (unlikely(a->mvd_bsrc < 0
25288 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25289 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25290 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25291 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25292 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25293 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25294 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25295 +                       AU_MVD_PR(dmsg, "no upper\n");
25296 +                       goto out;
25297 +               }
25298 +       }
25299 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25300 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25301 +               AU_MVD_PR(dmsg, "on the bottom\n");
25302 +               goto out;
25303 +       }
25304 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25305 +       br = au_sbr(a->sb, a->mvd_bsrc);
25306 +       err = au_br_rdonly(br);
25307 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25308 +               if (unlikely(err))
25309 +                       goto out;
25310 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25311 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25312 +               if (err)
25313 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25314 +               /* go on */
25315 +       } else
25316 +               goto out;
25317 +
25318 +       err = -EINVAL;
25319 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25320 +               a->mvd_bdst = find_lower_writable(a);
25321 +               if (unlikely(a->mvd_bdst < 0)) {
25322 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25323 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25324 +                       goto out;
25325 +               }
25326 +       } else {
25327 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25328 +               if (unlikely(a->mvd_bdst < 0
25329 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25330 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25331 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25332 +                       goto out;
25333 +               }
25334 +       }
25335 +
25336 +       err = au_mvd_args_busy(dmsg, a);
25337 +       if (!err)
25338 +               err = au_mvd_args_parent(dmsg, a);
25339 +       if (!err)
25340 +               err = au_mvd_args_intermediate(dmsg, a);
25341 +       if (!err)
25342 +               err = au_mvd_args_exist(dmsg, a);
25343 +       if (!err)
25344 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25345 +
25346 +out:
25347 +       AuTraceErr(err);
25348 +       return err;
25349 +}
25350 +
25351 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25352 +{
25353 +       int err, e;
25354 +       unsigned char dmsg;
25355 +       struct au_mvd_args *args;
25356 +       struct inode *inode;
25357 +
25358 +       inode = d_inode(dentry);
25359 +       err = -EPERM;
25360 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25361 +               goto out;
25362 +
25363 +       err = -ENOMEM;
25364 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25365 +       if (unlikely(!args))
25366 +               goto out;
25367 +
25368 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25369 +       if (!err)
25370 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
25371 +       if (unlikely(err)) {
25372 +               err = -EFAULT;
25373 +               AuTraceErr(err);
25374 +               goto out_free;
25375 +       }
25376 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25377 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25378 +       args->mvdown.au_errno = 0;
25379 +       args->dentry = dentry;
25380 +       args->inode = inode;
25381 +       args->sb = dentry->d_sb;
25382 +
25383 +       err = -ENOENT;
25384 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25385 +       args->parent = dget_parent(dentry);
25386 +       args->dir = d_inode(args->parent);
25387 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25388 +       dput(args->parent);
25389 +       if (unlikely(args->parent != dentry->d_parent)) {
25390 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25391 +               goto out_dir;
25392 +       }
25393 +
25394 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25395 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25396 +       if (unlikely(err))
25397 +               goto out_inode;
25398 +
25399 +       di_write_lock_parent(args->parent);
25400 +       err = au_mvd_args(dmsg, args);
25401 +       if (unlikely(err))
25402 +               goto out_parent;
25403 +
25404 +       err = au_do_mvdown(dmsg, args);
25405 +       if (unlikely(err))
25406 +               goto out_parent;
25407 +
25408 +       au_cpup_attr_timesizes(args->dir);
25409 +       au_cpup_attr_timesizes(inode);
25410 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25411 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25412 +       /* au_digen_dec(dentry); */
25413 +
25414 +out_parent:
25415 +       di_write_unlock(args->parent);
25416 +       aufs_read_unlock(dentry, AuLock_DW);
25417 +out_inode:
25418 +       inode_unlock(inode);
25419 +out_dir:
25420 +       inode_unlock(args->dir);
25421 +out_free:
25422 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25423 +       if (unlikely(e))
25424 +               err = -EFAULT;
25425 +       kfree(args);
25426 +out:
25427 +       AuTraceErr(err);
25428 +       return err;
25429 +}
25430 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25431 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25432 +++ linux/fs/aufs/opts.c        2018-07-19 09:46:13.059981241 +0200
25433 @@ -0,0 +1,1891 @@
25434 +/* SPDX-License-Identifier: GPL-2.0 */
25435 +/*
25436 + * Copyright (C) 2005-2018 Junjiro R. Okajima
25437 + *
25438 + * This program, aufs is free software; you can redistribute it and/or modify
25439 + * it under the terms of the GNU General Public License as published by
25440 + * the Free Software Foundation; either version 2 of the License, or
25441 + * (at your option) any later version.
25442 + *
25443 + * This program is distributed in the hope that it will be useful,
25444 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25445 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25446 + * GNU General Public License for more details.
25447 + *
25448 + * You should have received a copy of the GNU General Public License
25449 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25450 + */
25451 +
25452 +/*
25453 + * mount options/flags
25454 + */
25455 +
25456 +#include <linux/namei.h>
25457 +#include <linux/types.h> /* a distribution requires */
25458 +#include <linux/parser.h>
25459 +#include "aufs.h"
25460 +
25461 +/* ---------------------------------------------------------------------- */
25462 +
25463 +enum {
25464 +       Opt_br,
25465 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25466 +       Opt_idel, Opt_imod,
25467 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25468 +       Opt_rdblk_def, Opt_rdhash_def,
25469 +       Opt_xino, Opt_noxino,
25470 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25471 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25472 +       Opt_trunc_xib, Opt_notrunc_xib,
25473 +       Opt_shwh, Opt_noshwh,
25474 +       Opt_plink, Opt_noplink, Opt_list_plink,
25475 +       Opt_udba,
25476 +       Opt_dio, Opt_nodio,
25477 +       Opt_diropq_a, Opt_diropq_w,
25478 +       Opt_warn_perm, Opt_nowarn_perm,
25479 +       Opt_wbr_copyup, Opt_wbr_create,
25480 +       Opt_fhsm_sec,
25481 +       Opt_verbose, Opt_noverbose,
25482 +       Opt_sum, Opt_nosum, Opt_wsum,
25483 +       Opt_dirperm1, Opt_nodirperm1,
25484 +       Opt_dirren, Opt_nodirren,
25485 +       Opt_acl, Opt_noacl,
25486 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25487 +};
25488 +
25489 +static match_table_t options = {
25490 +       {Opt_br, "br=%s"},
25491 +       {Opt_br, "br:%s"},
25492 +
25493 +       {Opt_add, "add=%d:%s"},
25494 +       {Opt_add, "add:%d:%s"},
25495 +       {Opt_add, "ins=%d:%s"},
25496 +       {Opt_add, "ins:%d:%s"},
25497 +       {Opt_append, "append=%s"},
25498 +       {Opt_append, "append:%s"},
25499 +       {Opt_prepend, "prepend=%s"},
25500 +       {Opt_prepend, "prepend:%s"},
25501 +
25502 +       {Opt_del, "del=%s"},
25503 +       {Opt_del, "del:%s"},
25504 +       /* {Opt_idel, "idel:%d"}, */
25505 +       {Opt_mod, "mod=%s"},
25506 +       {Opt_mod, "mod:%s"},
25507 +       /* {Opt_imod, "imod:%d:%s"}, */
25508 +
25509 +       {Opt_dirwh, "dirwh=%d"},
25510 +
25511 +       {Opt_xino, "xino=%s"},
25512 +       {Opt_noxino, "noxino"},
25513 +       {Opt_trunc_xino, "trunc_xino"},
25514 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25515 +       {Opt_notrunc_xino, "notrunc_xino"},
25516 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25517 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25518 +       /* {Opt_zxino, "zxino=%s"}, */
25519 +       {Opt_trunc_xib, "trunc_xib"},
25520 +       {Opt_notrunc_xib, "notrunc_xib"},
25521 +
25522 +#ifdef CONFIG_PROC_FS
25523 +       {Opt_plink, "plink"},
25524 +#else
25525 +       {Opt_ignore_silent, "plink"},
25526 +#endif
25527 +
25528 +       {Opt_noplink, "noplink"},
25529 +
25530 +#ifdef CONFIG_AUFS_DEBUG
25531 +       {Opt_list_plink, "list_plink"},
25532 +#endif
25533 +
25534 +       {Opt_udba, "udba=%s"},
25535 +
25536 +       {Opt_dio, "dio"},
25537 +       {Opt_nodio, "nodio"},
25538 +
25539 +#ifdef CONFIG_AUFS_DIRREN
25540 +       {Opt_dirren, "dirren"},
25541 +       {Opt_nodirren, "nodirren"},
25542 +#else
25543 +       {Opt_ignore, "dirren"},
25544 +       {Opt_ignore_silent, "nodirren"},
25545 +#endif
25546 +
25547 +#ifdef CONFIG_AUFS_FHSM
25548 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25549 +#else
25550 +       {Opt_ignore, "fhsm_sec=%d"},
25551 +#endif
25552 +
25553 +       {Opt_diropq_a, "diropq=always"},
25554 +       {Opt_diropq_a, "diropq=a"},
25555 +       {Opt_diropq_w, "diropq=whiteouted"},
25556 +       {Opt_diropq_w, "diropq=w"},
25557 +
25558 +       {Opt_warn_perm, "warn_perm"},
25559 +       {Opt_nowarn_perm, "nowarn_perm"},
25560 +
25561 +       /* keep them temporary */
25562 +       {Opt_ignore_silent, "nodlgt"},
25563 +       {Opt_ignore, "clean_plink"},
25564 +
25565 +#ifdef CONFIG_AUFS_SHWH
25566 +       {Opt_shwh, "shwh"},
25567 +#endif
25568 +       {Opt_noshwh, "noshwh"},
25569 +
25570 +       {Opt_dirperm1, "dirperm1"},
25571 +       {Opt_nodirperm1, "nodirperm1"},
25572 +
25573 +       {Opt_verbose, "verbose"},
25574 +       {Opt_verbose, "v"},
25575 +       {Opt_noverbose, "noverbose"},
25576 +       {Opt_noverbose, "quiet"},
25577 +       {Opt_noverbose, "q"},
25578 +       {Opt_noverbose, "silent"},
25579 +
25580 +       {Opt_sum, "sum"},
25581 +       {Opt_nosum, "nosum"},
25582 +       {Opt_wsum, "wsum"},
25583 +
25584 +       {Opt_rdcache, "rdcache=%d"},
25585 +       {Opt_rdblk, "rdblk=%d"},
25586 +       {Opt_rdblk_def, "rdblk=def"},
25587 +       {Opt_rdhash, "rdhash=%d"},
25588 +       {Opt_rdhash_def, "rdhash=def"},
25589 +
25590 +       {Opt_wbr_create, "create=%s"},
25591 +       {Opt_wbr_create, "create_policy=%s"},
25592 +       {Opt_wbr_copyup, "cpup=%s"},
25593 +       {Opt_wbr_copyup, "copyup=%s"},
25594 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25595 +
25596 +       /* generic VFS flag */
25597 +#ifdef CONFIG_FS_POSIX_ACL
25598 +       {Opt_acl, "acl"},
25599 +       {Opt_noacl, "noacl"},
25600 +#else
25601 +       {Opt_ignore, "acl"},
25602 +       {Opt_ignore_silent, "noacl"},
25603 +#endif
25604 +
25605 +       /* internal use for the scripts */
25606 +       {Opt_ignore_silent, "si=%s"},
25607 +
25608 +       {Opt_br, "dirs=%s"},
25609 +       {Opt_ignore, "debug=%d"},
25610 +       {Opt_ignore, "delete=whiteout"},
25611 +       {Opt_ignore, "delete=all"},
25612 +       {Opt_ignore, "imap=%s"},
25613 +
25614 +       /* temporary workaround, due to old mount(8)? */
25615 +       {Opt_ignore_silent, "relatime"},
25616 +
25617 +       {Opt_err, NULL}
25618 +};
25619 +
25620 +/* ---------------------------------------------------------------------- */
25621 +
25622 +static const char *au_parser_pattern(int val, match_table_t tbl)
25623 +{
25624 +       struct match_token *p;
25625 +
25626 +       p = tbl;
25627 +       while (p->pattern) {
25628 +               if (p->token == val)
25629 +                       return p->pattern;
25630 +               p++;
25631 +       }
25632 +       BUG();
25633 +       return "??";
25634 +}
25635 +
25636 +static const char *au_optstr(int *val, match_table_t tbl)
25637 +{
25638 +       struct match_token *p;
25639 +       int v;
25640 +
25641 +       v = *val;
25642 +       if (!v)
25643 +               goto out;
25644 +       p = tbl;
25645 +       while (p->pattern) {
25646 +               if (p->token
25647 +                   && (v & p->token) == p->token) {
25648 +                       *val &= ~p->token;
25649 +                       return p->pattern;
25650 +               }
25651 +               p++;
25652 +       }
25653 +
25654 +out:
25655 +       return NULL;
25656 +}
25657 +
25658 +/* ---------------------------------------------------------------------- */
25659 +
25660 +static match_table_t brperm = {
25661 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25662 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25663 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25664 +       {0, NULL}
25665 +};
25666 +
25667 +static match_table_t brattr = {
25668 +       /* general */
25669 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25670 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25671 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25672 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25673 +#ifdef CONFIG_AUFS_FHSM
25674 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25675 +#endif
25676 +#ifdef CONFIG_AUFS_XATTR
25677 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25678 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25679 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25680 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25681 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25682 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25683 +#endif
25684 +
25685 +       /* ro/rr branch */
25686 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25687 +
25688 +       /* rw branch */
25689 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25690 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25691 +
25692 +       {0, NULL}
25693 +};
25694 +
25695 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25696 +{
25697 +       int attr, v;
25698 +       char *p;
25699 +
25700 +       attr = 0;
25701 +       do {
25702 +               p = strchr(str, '+');
25703 +               if (p)
25704 +                       *p = 0;
25705 +               v = match_token(str, table, args);
25706 +               if (v) {
25707 +                       if (v & AuBrAttr_CMOO_Mask)
25708 +                               attr &= ~AuBrAttr_CMOO_Mask;
25709 +                       attr |= v;
25710 +               } else {
25711 +                       if (p)
25712 +                               *p = '+';
25713 +                       pr_warn("ignored branch attribute %s\n", str);
25714 +                       break;
25715 +               }
25716 +               if (p)
25717 +                       str = p + 1;
25718 +       } while (p);
25719 +
25720 +       return attr;
25721 +}
25722 +
25723 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25724 +{
25725 +       int sz;
25726 +       const char *p;
25727 +       char *q;
25728 +
25729 +       q = str->a;
25730 +       *q = 0;
25731 +       p = au_optstr(&perm, brattr);
25732 +       if (p) {
25733 +               sz = strlen(p);
25734 +               memcpy(q, p, sz + 1);
25735 +               q += sz;
25736 +       } else
25737 +               goto out;
25738 +
25739 +       do {
25740 +               p = au_optstr(&perm, brattr);
25741 +               if (p) {
25742 +                       *q++ = '+';
25743 +                       sz = strlen(p);
25744 +                       memcpy(q, p, sz + 1);
25745 +                       q += sz;
25746 +               }
25747 +       } while (p);
25748 +
25749 +out:
25750 +       return q - str->a;
25751 +}
25752 +
25753 +static int noinline_for_stack br_perm_val(char *perm)
25754 +{
25755 +       int val, bad, sz;
25756 +       char *p;
25757 +       substring_t args[MAX_OPT_ARGS];
25758 +       au_br_perm_str_t attr;
25759 +
25760 +       p = strchr(perm, '+');
25761 +       if (p)
25762 +               *p = 0;
25763 +       val = match_token(perm, brperm, args);
25764 +       if (!val) {
25765 +               if (p)
25766 +                       *p = '+';
25767 +               pr_warn("ignored branch permission %s\n", perm);
25768 +               val = AuBrPerm_RO;
25769 +               goto out;
25770 +       }
25771 +       if (!p)
25772 +               goto out;
25773 +
25774 +       val |= br_attr_val(p + 1, brattr, args);
25775 +
25776 +       bad = 0;
25777 +       switch (val & AuBrPerm_Mask) {
25778 +       case AuBrPerm_RO:
25779 +       case AuBrPerm_RR:
25780 +               bad = val & AuBrWAttr_Mask;
25781 +               val &= ~AuBrWAttr_Mask;
25782 +               break;
25783 +       case AuBrPerm_RW:
25784 +               bad = val & AuBrRAttr_Mask;
25785 +               val &= ~AuBrRAttr_Mask;
25786 +               break;
25787 +       }
25788 +
25789 +       /*
25790 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25791 +        * does not treat it as an error, just warning.
25792 +        * this is a tiny guard for the user operation.
25793 +        */
25794 +       if (val & AuBrAttr_UNPIN) {
25795 +               bad |= AuBrAttr_UNPIN;
25796 +               val &= ~AuBrAttr_UNPIN;
25797 +       }
25798 +
25799 +       if (unlikely(bad)) {
25800 +               sz = au_do_optstr_br_attr(&attr, bad);
25801 +               AuDebugOn(!sz);
25802 +               pr_warn("ignored branch attribute %s\n", attr.a);
25803 +       }
25804 +
25805 +out:
25806 +       return val;
25807 +}
25808 +
25809 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25810 +{
25811 +       au_br_perm_str_t attr;
25812 +       const char *p;
25813 +       char *q;
25814 +       int sz;
25815 +
25816 +       q = str->a;
25817 +       p = au_optstr(&perm, brperm);
25818 +       AuDebugOn(!p || !*p);
25819 +       sz = strlen(p);
25820 +       memcpy(q, p, sz + 1);
25821 +       q += sz;
25822 +
25823 +       sz = au_do_optstr_br_attr(&attr, perm);
25824 +       if (sz) {
25825 +               *q++ = '+';
25826 +               memcpy(q, attr.a, sz + 1);
25827 +       }
25828 +
25829 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25830 +}
25831 +
25832 +/* ---------------------------------------------------------------------- */
25833 +
25834 +static match_table_t udbalevel = {
25835 +       {AuOpt_UDBA_REVAL, "reval"},
25836 +       {AuOpt_UDBA_NONE, "none"},
25837 +#ifdef CONFIG_AUFS_HNOTIFY
25838 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25839 +#ifdef CONFIG_AUFS_HFSNOTIFY
25840 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25841 +#endif
25842 +#endif
25843 +       {-1, NULL}
25844 +};
25845 +
25846 +static int noinline_for_stack udba_val(char *str)
25847 +{
25848 +       substring_t args[MAX_OPT_ARGS];
25849 +
25850 +       return match_token(str, udbalevel, args);
25851 +}
25852 +
25853 +const char *au_optstr_udba(int udba)
25854 +{
25855 +       return au_parser_pattern(udba, udbalevel);
25856 +}
25857 +
25858 +/* ---------------------------------------------------------------------- */
25859 +
25860 +static match_table_t au_wbr_create_policy = {
25861 +       {AuWbrCreate_TDP, "tdp"},
25862 +       {AuWbrCreate_TDP, "top-down-parent"},
25863 +       {AuWbrCreate_RR, "rr"},
25864 +       {AuWbrCreate_RR, "round-robin"},
25865 +       {AuWbrCreate_MFS, "mfs"},
25866 +       {AuWbrCreate_MFS, "most-free-space"},
25867 +       {AuWbrCreate_MFSV, "mfs:%d"},
25868 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
25869 +
25870 +       /* top-down regardless the parent, and then mfs */
25871 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
25872 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25873 +
25874 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
25875 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25876 +       {AuWbrCreate_PMFS, "pmfs"},
25877 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
25878 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25879 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
25880 +
25881 +       {-1, NULL}
25882 +};
25883 +
25884 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25885 +                           struct au_opt_wbr_create *create)
25886 +{
25887 +       int err;
25888 +       unsigned long long ull;
25889 +
25890 +       err = 0;
25891 +       if (!match_u64(arg, &ull))
25892 +               create->mfsrr_watermark = ull;
25893 +       else {
25894 +               pr_err("bad integer in %s\n", str);
25895 +               err = -EINVAL;
25896 +       }
25897 +
25898 +       return err;
25899 +}
25900 +
25901 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
25902 +                         struct au_opt_wbr_create *create)
25903 +{
25904 +       int n, err;
25905 +
25906 +       err = 0;
25907 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
25908 +               create->mfs_second = n;
25909 +       else {
25910 +               pr_err("bad integer in %s\n", str);
25911 +               err = -EINVAL;
25912 +       }
25913 +
25914 +       return err;
25915 +}
25916 +
25917 +static int noinline_for_stack
25918 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
25919 +{
25920 +       int err, e;
25921 +       substring_t args[MAX_OPT_ARGS];
25922 +
25923 +       err = match_token(str, au_wbr_create_policy, args);
25924 +       create->wbr_create = err;
25925 +       switch (err) {
25926 +       case AuWbrCreate_MFSRRV:
25927 +       case AuWbrCreate_TDMFSV:
25928 +       case AuWbrCreate_PMFSRRV:
25929 +               e = au_wbr_mfs_wmark(&args[0], str, create);
25930 +               if (!e)
25931 +                       e = au_wbr_mfs_sec(&args[1], str, create);
25932 +               if (unlikely(e))
25933 +                       err = e;
25934 +               break;
25935 +       case AuWbrCreate_MFSRR:
25936 +       case AuWbrCreate_TDMFS:
25937 +       case AuWbrCreate_PMFSRR:
25938 +               e = au_wbr_mfs_wmark(&args[0], str, create);
25939 +               if (unlikely(e)) {
25940 +                       err = e;
25941 +                       break;
25942 +               }
25943 +               /*FALLTHROUGH*/
25944 +       case AuWbrCreate_MFS:
25945 +       case AuWbrCreate_PMFS:
25946 +               create->mfs_second = AUFS_MFS_DEF_SEC;
25947 +               break;
25948 +       case AuWbrCreate_MFSV:
25949 +       case AuWbrCreate_PMFSV:
25950 +               e = au_wbr_mfs_sec(&args[0], str, create);
25951 +               if (unlikely(e))
25952 +                       err = e;
25953 +               break;
25954 +       }
25955 +
25956 +       return err;
25957 +}
25958 +
25959 +const char *au_optstr_wbr_create(int wbr_create)
25960 +{
25961 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
25962 +}
25963 +
25964 +static match_table_t au_wbr_copyup_policy = {
25965 +       {AuWbrCopyup_TDP, "tdp"},
25966 +       {AuWbrCopyup_TDP, "top-down-parent"},
25967 +       {AuWbrCopyup_BUP, "bup"},
25968 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
25969 +       {AuWbrCopyup_BU, "bu"},
25970 +       {AuWbrCopyup_BU, "bottom-up"},
25971 +       {-1, NULL}
25972 +};
25973 +
25974 +static int noinline_for_stack au_wbr_copyup_val(char *str)
25975 +{
25976 +       substring_t args[MAX_OPT_ARGS];
25977 +
25978 +       return match_token(str, au_wbr_copyup_policy, args);
25979 +}
25980 +
25981 +const char *au_optstr_wbr_copyup(int wbr_copyup)
25982 +{
25983 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
25984 +}
25985 +
25986 +/* ---------------------------------------------------------------------- */
25987 +
25988 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
25989 +
25990 +static void dump_opts(struct au_opts *opts)
25991 +{
25992 +#ifdef CONFIG_AUFS_DEBUG
25993 +       /* reduce stack space */
25994 +       union {
25995 +               struct au_opt_add *add;
25996 +               struct au_opt_del *del;
25997 +               struct au_opt_mod *mod;
25998 +               struct au_opt_xino *xino;
25999 +               struct au_opt_xino_itrunc *xino_itrunc;
26000 +               struct au_opt_wbr_create *create;
26001 +       } u;
26002 +       struct au_opt *opt;
26003 +
26004 +       opt = opts->opt;
26005 +       while (opt->type != Opt_tail) {
26006 +               switch (opt->type) {
26007 +               case Opt_add:
26008 +                       u.add = &opt->add;
26009 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26010 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26011 +                                 u.add->path.dentry);
26012 +                       break;
26013 +               case Opt_del:
26014 +               case Opt_idel:
26015 +                       u.del = &opt->del;
26016 +                       AuDbg("del {%s, %p}\n",
26017 +                             u.del->pathname, u.del->h_path.dentry);
26018 +                       break;
26019 +               case Opt_mod:
26020 +               case Opt_imod:
26021 +                       u.mod = &opt->mod;
26022 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26023 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26024 +                       break;
26025 +               case Opt_append:
26026 +                       u.add = &opt->add;
26027 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26028 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26029 +                                 u.add->path.dentry);
26030 +                       break;
26031 +               case Opt_prepend:
26032 +                       u.add = &opt->add;
26033 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26034 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26035 +                                 u.add->path.dentry);
26036 +                       break;
26037 +               case Opt_dirwh:
26038 +                       AuDbg("dirwh %d\n", opt->dirwh);
26039 +                       break;
26040 +               case Opt_rdcache:
26041 +                       AuDbg("rdcache %d\n", opt->rdcache);
26042 +                       break;
26043 +               case Opt_rdblk:
26044 +                       AuDbg("rdblk %u\n", opt->rdblk);
26045 +                       break;
26046 +               case Opt_rdblk_def:
26047 +                       AuDbg("rdblk_def\n");
26048 +                       break;
26049 +               case Opt_rdhash:
26050 +                       AuDbg("rdhash %u\n", opt->rdhash);
26051 +                       break;
26052 +               case Opt_rdhash_def:
26053 +                       AuDbg("rdhash_def\n");
26054 +                       break;
26055 +               case Opt_xino:
26056 +                       u.xino = &opt->xino;
26057 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26058 +                       break;
26059 +               case Opt_trunc_xino:
26060 +                       AuLabel(trunc_xino);
26061 +                       break;
26062 +               case Opt_notrunc_xino:
26063 +                       AuLabel(notrunc_xino);
26064 +                       break;
26065 +               case Opt_trunc_xino_path:
26066 +               case Opt_itrunc_xino:
26067 +                       u.xino_itrunc = &opt->xino_itrunc;
26068 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26069 +                       break;
26070 +               case Opt_noxino:
26071 +                       AuLabel(noxino);
26072 +                       break;
26073 +               case Opt_trunc_xib:
26074 +                       AuLabel(trunc_xib);
26075 +                       break;
26076 +               case Opt_notrunc_xib:
26077 +                       AuLabel(notrunc_xib);
26078 +                       break;
26079 +               case Opt_shwh:
26080 +                       AuLabel(shwh);
26081 +                       break;
26082 +               case Opt_noshwh:
26083 +                       AuLabel(noshwh);
26084 +                       break;
26085 +               case Opt_dirperm1:
26086 +                       AuLabel(dirperm1);
26087 +                       break;
26088 +               case Opt_nodirperm1:
26089 +                       AuLabel(nodirperm1);
26090 +                       break;
26091 +               case Opt_plink:
26092 +                       AuLabel(plink);
26093 +                       break;
26094 +               case Opt_noplink:
26095 +                       AuLabel(noplink);
26096 +                       break;
26097 +               case Opt_list_plink:
26098 +                       AuLabel(list_plink);
26099 +                       break;
26100 +               case Opt_udba:
26101 +                       AuDbg("udba %d, %s\n",
26102 +                                 opt->udba, au_optstr_udba(opt->udba));
26103 +                       break;
26104 +               case Opt_dio:
26105 +                       AuLabel(dio);
26106 +                       break;
26107 +               case Opt_nodio:
26108 +                       AuLabel(nodio);
26109 +                       break;
26110 +               case Opt_diropq_a:
26111 +                       AuLabel(diropq_a);
26112 +                       break;
26113 +               case Opt_diropq_w:
26114 +                       AuLabel(diropq_w);
26115 +                       break;
26116 +               case Opt_warn_perm:
26117 +                       AuLabel(warn_perm);
26118 +                       break;
26119 +               case Opt_nowarn_perm:
26120 +                       AuLabel(nowarn_perm);
26121 +                       break;
26122 +               case Opt_verbose:
26123 +                       AuLabel(verbose);
26124 +                       break;
26125 +               case Opt_noverbose:
26126 +                       AuLabel(noverbose);
26127 +                       break;
26128 +               case Opt_sum:
26129 +                       AuLabel(sum);
26130 +                       break;
26131 +               case Opt_nosum:
26132 +                       AuLabel(nosum);
26133 +                       break;
26134 +               case Opt_wsum:
26135 +                       AuLabel(wsum);
26136 +                       break;
26137 +               case Opt_wbr_create:
26138 +                       u.create = &opt->wbr_create;
26139 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26140 +                                 au_optstr_wbr_create(u.create->wbr_create));
26141 +                       switch (u.create->wbr_create) {
26142 +                       case AuWbrCreate_MFSV:
26143 +                       case AuWbrCreate_PMFSV:
26144 +                               AuDbg("%d sec\n", u.create->mfs_second);
26145 +                               break;
26146 +                       case AuWbrCreate_MFSRR:
26147 +                       case AuWbrCreate_TDMFS:
26148 +                               AuDbg("%llu watermark\n",
26149 +                                         u.create->mfsrr_watermark);
26150 +                               break;
26151 +                       case AuWbrCreate_MFSRRV:
26152 +                       case AuWbrCreate_TDMFSV:
26153 +                       case AuWbrCreate_PMFSRRV:
26154 +                               AuDbg("%llu watermark, %d sec\n",
26155 +                                         u.create->mfsrr_watermark,
26156 +                                         u.create->mfs_second);
26157 +                               break;
26158 +                       }
26159 +                       break;
26160 +               case Opt_wbr_copyup:
26161 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26162 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26163 +                       break;
26164 +               case Opt_fhsm_sec:
26165 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26166 +                       break;
26167 +               case Opt_dirren:
26168 +                       AuLabel(dirren);
26169 +                       break;
26170 +               case Opt_nodirren:
26171 +                       AuLabel(nodirren);
26172 +                       break;
26173 +               case Opt_acl:
26174 +                       AuLabel(acl);
26175 +                       break;
26176 +               case Opt_noacl:
26177 +                       AuLabel(noacl);
26178 +                       break;
26179 +               default:
26180 +                       BUG();
26181 +               }
26182 +               opt++;
26183 +       }
26184 +#endif
26185 +}
26186 +
26187 +void au_opts_free(struct au_opts *opts)
26188 +{
26189 +       struct au_opt *opt;
26190 +
26191 +       opt = opts->opt;
26192 +       while (opt->type != Opt_tail) {
26193 +               switch (opt->type) {
26194 +               case Opt_add:
26195 +               case Opt_append:
26196 +               case Opt_prepend:
26197 +                       path_put(&opt->add.path);
26198 +                       break;
26199 +               case Opt_del:
26200 +               case Opt_idel:
26201 +                       path_put(&opt->del.h_path);
26202 +                       break;
26203 +               case Opt_mod:
26204 +               case Opt_imod:
26205 +                       dput(opt->mod.h_root);
26206 +                       break;
26207 +               case Opt_xino:
26208 +                       fput(opt->xino.file);
26209 +                       break;
26210 +               }
26211 +               opt++;
26212 +       }
26213 +}
26214 +
26215 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26216 +                  aufs_bindex_t bindex)
26217 +{
26218 +       int err;
26219 +       struct au_opt_add *add = &opt->add;
26220 +       char *p;
26221 +
26222 +       add->bindex = bindex;
26223 +       add->perm = AuBrPerm_RO;
26224 +       add->pathname = opt_str;
26225 +       p = strchr(opt_str, '=');
26226 +       if (p) {
26227 +               *p++ = 0;
26228 +               if (*p)
26229 +                       add->perm = br_perm_val(p);
26230 +       }
26231 +
26232 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26233 +       if (!err) {
26234 +               if (!p) {
26235 +                       add->perm = AuBrPerm_RO;
26236 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26237 +                               add->perm = AuBrPerm_RR;
26238 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26239 +                               add->perm = AuBrPerm_RW;
26240 +               }
26241 +               opt->type = Opt_add;
26242 +               goto out;
26243 +       }
26244 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26245 +       err = -EINVAL;
26246 +
26247 +out:
26248 +       return err;
26249 +}
26250 +
26251 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26252 +{
26253 +       int err;
26254 +
26255 +       del->pathname = args[0].from;
26256 +       AuDbg("del path %s\n", del->pathname);
26257 +
26258 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26259 +       if (unlikely(err))
26260 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26261 +
26262 +       return err;
26263 +}
26264 +
26265 +#if 0 /* reserved for future use */
26266 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26267 +                             struct au_opt_del *del, substring_t args[])
26268 +{
26269 +       int err;
26270 +       struct dentry *root;
26271 +
26272 +       err = -EINVAL;
26273 +       root = sb->s_root;
26274 +       aufs_read_lock(root, AuLock_FLUSH);
26275 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26276 +               pr_err("out of bounds, %d\n", bindex);
26277 +               goto out;
26278 +       }
26279 +
26280 +       err = 0;
26281 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26282 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26283 +
26284 +out:
26285 +       aufs_read_unlock(root, !AuLock_IR);
26286 +       return err;
26287 +}
26288 +#endif
26289 +
26290 +static int noinline_for_stack
26291 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26292 +{
26293 +       int err;
26294 +       struct path path;
26295 +       char *p;
26296 +
26297 +       err = -EINVAL;
26298 +       mod->path = args[0].from;
26299 +       p = strchr(mod->path, '=');
26300 +       if (unlikely(!p)) {
26301 +               pr_err("no permssion %s\n", args[0].from);
26302 +               goto out;
26303 +       }
26304 +
26305 +       *p++ = 0;
26306 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26307 +       if (unlikely(err)) {
26308 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26309 +               goto out;
26310 +       }
26311 +
26312 +       mod->perm = br_perm_val(p);
26313 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26314 +       mod->h_root = dget(path.dentry);
26315 +       path_put(&path);
26316 +
26317 +out:
26318 +       return err;
26319 +}
26320 +
26321 +#if 0 /* reserved for future use */
26322 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26323 +                             struct au_opt_mod *mod, substring_t args[])
26324 +{
26325 +       int err;
26326 +       struct dentry *root;
26327 +
26328 +       err = -EINVAL;
26329 +       root = sb->s_root;
26330 +       aufs_read_lock(root, AuLock_FLUSH);
26331 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26332 +               pr_err("out of bounds, %d\n", bindex);
26333 +               goto out;
26334 +       }
26335 +
26336 +       err = 0;
26337 +       mod->perm = br_perm_val(args[1].from);
26338 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26339 +             mod->path, mod->perm, args[1].from);
26340 +       mod->h_root = dget(au_h_dptr(root, bindex));
26341 +
26342 +out:
26343 +       aufs_read_unlock(root, !AuLock_IR);
26344 +       return err;
26345 +}
26346 +#endif
26347 +
26348 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26349 +                             substring_t args[])
26350 +{
26351 +       int err;
26352 +       struct file *file;
26353 +
26354 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
26355 +       err = PTR_ERR(file);
26356 +       if (IS_ERR(file))
26357 +               goto out;
26358 +
26359 +       err = -EINVAL;
26360 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26361 +               fput(file);
26362 +               pr_err("%s must be outside\n", args[0].from);
26363 +               goto out;
26364 +       }
26365 +
26366 +       err = 0;
26367 +       xino->file = file;
26368 +       xino->path = args[0].from;
26369 +
26370 +out:
26371 +       return err;
26372 +}
26373 +
26374 +static int noinline_for_stack
26375 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26376 +                              struct au_opt_xino_itrunc *xino_itrunc,
26377 +                              substring_t args[])
26378 +{
26379 +       int err;
26380 +       aufs_bindex_t bbot, bindex;
26381 +       struct path path;
26382 +       struct dentry *root;
26383 +
26384 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26385 +       if (unlikely(err)) {
26386 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26387 +               goto out;
26388 +       }
26389 +
26390 +       xino_itrunc->bindex = -1;
26391 +       root = sb->s_root;
26392 +       aufs_read_lock(root, AuLock_FLUSH);
26393 +       bbot = au_sbbot(sb);
26394 +       for (bindex = 0; bindex <= bbot; bindex++) {
26395 +               if (au_h_dptr(root, bindex) == path.dentry) {
26396 +                       xino_itrunc->bindex = bindex;
26397 +                       break;
26398 +               }
26399 +       }
26400 +       aufs_read_unlock(root, !AuLock_IR);
26401 +       path_put(&path);
26402 +
26403 +       if (unlikely(xino_itrunc->bindex < 0)) {
26404 +               pr_err("no such branch %s\n", args[0].from);
26405 +               err = -EINVAL;
26406 +       }
26407 +
26408 +out:
26409 +       return err;
26410 +}
26411 +
26412 +/* called without aufs lock */
26413 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26414 +{
26415 +       int err, n, token;
26416 +       aufs_bindex_t bindex;
26417 +       unsigned char skipped;
26418 +       struct dentry *root;
26419 +       struct au_opt *opt, *opt_tail;
26420 +       char *opt_str;
26421 +       /* reduce the stack space */
26422 +       union {
26423 +               struct au_opt_xino_itrunc *xino_itrunc;
26424 +               struct au_opt_wbr_create *create;
26425 +       } u;
26426 +       struct {
26427 +               substring_t args[MAX_OPT_ARGS];
26428 +       } *a;
26429 +
26430 +       err = -ENOMEM;
26431 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26432 +       if (unlikely(!a))
26433 +               goto out;
26434 +
26435 +       root = sb->s_root;
26436 +       err = 0;
26437 +       bindex = 0;
26438 +       opt = opts->opt;
26439 +       opt_tail = opt + opts->max_opt - 1;
26440 +       opt->type = Opt_tail;
26441 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26442 +               err = -EINVAL;
26443 +               skipped = 0;
26444 +               token = match_token(opt_str, options, a->args);
26445 +               switch (token) {
26446 +               case Opt_br:
26447 +                       err = 0;
26448 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26449 +                              && *opt_str) {
26450 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26451 +                                             bindex++);
26452 +                               if (unlikely(!err && ++opt > opt_tail)) {
26453 +                                       err = -E2BIG;
26454 +                                       break;
26455 +                               }
26456 +                               opt->type = Opt_tail;
26457 +                               skipped = 1;
26458 +                       }
26459 +                       break;
26460 +               case Opt_add:
26461 +                       if (unlikely(match_int(&a->args[0], &n))) {
26462 +                               pr_err("bad integer in %s\n", opt_str);
26463 +                               break;
26464 +                       }
26465 +                       bindex = n;
26466 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26467 +                                     bindex);
26468 +                       if (!err)
26469 +                               opt->type = token;
26470 +                       break;
26471 +               case Opt_append:
26472 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26473 +                                     /*dummy bindex*/1);
26474 +                       if (!err)
26475 +                               opt->type = token;
26476 +                       break;
26477 +               case Opt_prepend:
26478 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26479 +                                     /*bindex*/0);
26480 +                       if (!err)
26481 +                               opt->type = token;
26482 +                       break;
26483 +               case Opt_del:
26484 +                       err = au_opts_parse_del(&opt->del, a->args);
26485 +                       if (!err)
26486 +                               opt->type = token;
26487 +                       break;
26488 +#if 0 /* reserved for future use */
26489 +               case Opt_idel:
26490 +                       del->pathname = "(indexed)";
26491 +                       if (unlikely(match_int(&args[0], &n))) {
26492 +                               pr_err("bad integer in %s\n", opt_str);
26493 +                               break;
26494 +                       }
26495 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26496 +                       if (!err)
26497 +                               opt->type = token;
26498 +                       break;
26499 +#endif
26500 +               case Opt_mod:
26501 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26502 +                       if (!err)
26503 +                               opt->type = token;
26504 +                       break;
26505 +#ifdef IMOD /* reserved for future use */
26506 +               case Opt_imod:
26507 +                       u.mod->path = "(indexed)";
26508 +                       if (unlikely(match_int(&a->args[0], &n))) {
26509 +                               pr_err("bad integer in %s\n", opt_str);
26510 +                               break;
26511 +                       }
26512 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26513 +                       if (!err)
26514 +                               opt->type = token;
26515 +                       break;
26516 +#endif
26517 +               case Opt_xino:
26518 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26519 +                       if (!err)
26520 +                               opt->type = token;
26521 +                       break;
26522 +
26523 +               case Opt_trunc_xino_path:
26524 +                       err = au_opts_parse_xino_itrunc_path
26525 +                               (sb, &opt->xino_itrunc, a->args);
26526 +                       if (!err)
26527 +                               opt->type = token;
26528 +                       break;
26529 +
26530 +               case Opt_itrunc_xino:
26531 +                       u.xino_itrunc = &opt->xino_itrunc;
26532 +                       if (unlikely(match_int(&a->args[0], &n))) {
26533 +                               pr_err("bad integer in %s\n", opt_str);
26534 +                               break;
26535 +                       }
26536 +                       u.xino_itrunc->bindex = n;
26537 +                       aufs_read_lock(root, AuLock_FLUSH);
26538 +                       if (n < 0 || au_sbbot(sb) < n) {
26539 +                               pr_err("out of bounds, %d\n", n);
26540 +                               aufs_read_unlock(root, !AuLock_IR);
26541 +                               break;
26542 +                       }
26543 +                       aufs_read_unlock(root, !AuLock_IR);
26544 +                       err = 0;
26545 +                       opt->type = token;
26546 +                       break;
26547 +
26548 +               case Opt_dirwh:
26549 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26550 +                               break;
26551 +                       err = 0;
26552 +                       opt->type = token;
26553 +                       break;
26554 +
26555 +               case Opt_rdcache:
26556 +                       if (unlikely(match_int(&a->args[0], &n))) {
26557 +                               pr_err("bad integer in %s\n", opt_str);
26558 +                               break;
26559 +                       }
26560 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26561 +                               pr_err("rdcache must be smaller than %d\n",
26562 +                                      AUFS_RDCACHE_MAX);
26563 +                               break;
26564 +                       }
26565 +                       opt->rdcache = n;
26566 +                       err = 0;
26567 +                       opt->type = token;
26568 +                       break;
26569 +               case Opt_rdblk:
26570 +                       if (unlikely(match_int(&a->args[0], &n)
26571 +                                    || n < 0
26572 +                                    || n > KMALLOC_MAX_SIZE)) {
26573 +                               pr_err("bad integer in %s\n", opt_str);
26574 +                               break;
26575 +                       }
26576 +                       if (unlikely(n && n < NAME_MAX)) {
26577 +                               pr_err("rdblk must be larger than %d\n",
26578 +                                      NAME_MAX);
26579 +                               break;
26580 +                       }
26581 +                       opt->rdblk = n;
26582 +                       err = 0;
26583 +                       opt->type = token;
26584 +                       break;
26585 +               case Opt_rdhash:
26586 +                       if (unlikely(match_int(&a->args[0], &n)
26587 +                                    || n < 0
26588 +                                    || n * sizeof(struct hlist_head)
26589 +                                    > KMALLOC_MAX_SIZE)) {
26590 +                               pr_err("bad integer in %s\n", opt_str);
26591 +                               break;
26592 +                       }
26593 +                       opt->rdhash = n;
26594 +                       err = 0;
26595 +                       opt->type = token;
26596 +                       break;
26597 +
26598 +               case Opt_trunc_xino:
26599 +               case Opt_notrunc_xino:
26600 +               case Opt_noxino:
26601 +               case Opt_trunc_xib:
26602 +               case Opt_notrunc_xib:
26603 +               case Opt_shwh:
26604 +               case Opt_noshwh:
26605 +               case Opt_dirperm1:
26606 +               case Opt_nodirperm1:
26607 +               case Opt_plink:
26608 +               case Opt_noplink:
26609 +               case Opt_list_plink:
26610 +               case Opt_dio:
26611 +               case Opt_nodio:
26612 +               case Opt_diropq_a:
26613 +               case Opt_diropq_w:
26614 +               case Opt_warn_perm:
26615 +               case Opt_nowarn_perm:
26616 +               case Opt_verbose:
26617 +               case Opt_noverbose:
26618 +               case Opt_sum:
26619 +               case Opt_nosum:
26620 +               case Opt_wsum:
26621 +               case Opt_rdblk_def:
26622 +               case Opt_rdhash_def:
26623 +               case Opt_dirren:
26624 +               case Opt_nodirren:
26625 +               case Opt_acl:
26626 +               case Opt_noacl:
26627 +                       err = 0;
26628 +                       opt->type = token;
26629 +                       break;
26630 +
26631 +               case Opt_udba:
26632 +                       opt->udba = udba_val(a->args[0].from);
26633 +                       if (opt->udba >= 0) {
26634 +                               err = 0;
26635 +                               opt->type = token;
26636 +                       } else
26637 +                               pr_err("wrong value, %s\n", opt_str);
26638 +                       break;
26639 +
26640 +               case Opt_wbr_create:
26641 +                       u.create = &opt->wbr_create;
26642 +                       u.create->wbr_create
26643 +                               = au_wbr_create_val(a->args[0].from, u.create);
26644 +                       if (u.create->wbr_create >= 0) {
26645 +                               err = 0;
26646 +                               opt->type = token;
26647 +                       } else
26648 +                               pr_err("wrong value, %s\n", opt_str);
26649 +                       break;
26650 +               case Opt_wbr_copyup:
26651 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26652 +                       if (opt->wbr_copyup >= 0) {
26653 +                               err = 0;
26654 +                               opt->type = token;
26655 +                       } else
26656 +                               pr_err("wrong value, %s\n", opt_str);
26657 +                       break;
26658 +
26659 +               case Opt_fhsm_sec:
26660 +                       if (unlikely(match_int(&a->args[0], &n)
26661 +                                    || n < 0)) {
26662 +                               pr_err("bad integer in %s\n", opt_str);
26663 +                               break;
26664 +                       }
26665 +                       if (sysaufs_brs) {
26666 +                               opt->fhsm_second = n;
26667 +                               opt->type = token;
26668 +                       } else
26669 +                               pr_warn("ignored %s\n", opt_str);
26670 +                       err = 0;
26671 +                       break;
26672 +
26673 +               case Opt_ignore:
26674 +                       pr_warn("ignored %s\n", opt_str);
26675 +                       /*FALLTHROUGH*/
26676 +               case Opt_ignore_silent:
26677 +                       skipped = 1;
26678 +                       err = 0;
26679 +                       break;
26680 +               case Opt_err:
26681 +                       pr_err("unknown option %s\n", opt_str);
26682 +                       break;
26683 +               }
26684 +
26685 +               if (!err && !skipped) {
26686 +                       if (unlikely(++opt > opt_tail)) {
26687 +                               err = -E2BIG;
26688 +                               opt--;
26689 +                               opt->type = Opt_tail;
26690 +                               break;
26691 +                       }
26692 +                       opt->type = Opt_tail;
26693 +               }
26694 +       }
26695 +
26696 +       kfree(a);
26697 +       dump_opts(opts);
26698 +       if (unlikely(err))
26699 +               au_opts_free(opts);
26700 +
26701 +out:
26702 +       return err;
26703 +}
26704 +
26705 +static int au_opt_wbr_create(struct super_block *sb,
26706 +                            struct au_opt_wbr_create *create)
26707 +{
26708 +       int err;
26709 +       struct au_sbinfo *sbinfo;
26710 +
26711 +       SiMustWriteLock(sb);
26712 +
26713 +       err = 1; /* handled */
26714 +       sbinfo = au_sbi(sb);
26715 +       if (sbinfo->si_wbr_create_ops->fin) {
26716 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26717 +               if (!err)
26718 +                       err = 1;
26719 +       }
26720 +
26721 +       sbinfo->si_wbr_create = create->wbr_create;
26722 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26723 +       switch (create->wbr_create) {
26724 +       case AuWbrCreate_MFSRRV:
26725 +       case AuWbrCreate_MFSRR:
26726 +       case AuWbrCreate_TDMFS:
26727 +       case AuWbrCreate_TDMFSV:
26728 +       case AuWbrCreate_PMFSRR:
26729 +       case AuWbrCreate_PMFSRRV:
26730 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26731 +               /*FALLTHROUGH*/
26732 +       case AuWbrCreate_MFS:
26733 +       case AuWbrCreate_MFSV:
26734 +       case AuWbrCreate_PMFS:
26735 +       case AuWbrCreate_PMFSV:
26736 +               sbinfo->si_wbr_mfs.mfs_expire
26737 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26738 +               break;
26739 +       }
26740 +
26741 +       if (sbinfo->si_wbr_create_ops->init)
26742 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26743 +
26744 +       return err;
26745 +}
26746 +
26747 +/*
26748 + * returns,
26749 + * plus: processed without an error
26750 + * zero: unprocessed
26751 + */
26752 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26753 +                        struct au_opts *opts)
26754 +{
26755 +       int err;
26756 +       struct au_sbinfo *sbinfo;
26757 +
26758 +       SiMustWriteLock(sb);
26759 +
26760 +       err = 1; /* handled */
26761 +       sbinfo = au_sbi(sb);
26762 +       switch (opt->type) {
26763 +       case Opt_udba:
26764 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26765 +               sbinfo->si_mntflags |= opt->udba;
26766 +               opts->given_udba |= opt->udba;
26767 +               break;
26768 +
26769 +       case Opt_plink:
26770 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26771 +               break;
26772 +       case Opt_noplink:
26773 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26774 +                       au_plink_put(sb, /*verbose*/1);
26775 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26776 +               break;
26777 +       case Opt_list_plink:
26778 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26779 +                       au_plink_list(sb);
26780 +               break;
26781 +
26782 +       case Opt_dio:
26783 +               au_opt_set(sbinfo->si_mntflags, DIO);
26784 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26785 +               break;
26786 +       case Opt_nodio:
26787 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26788 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26789 +               break;
26790 +
26791 +       case Opt_fhsm_sec:
26792 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26793 +               break;
26794 +
26795 +       case Opt_diropq_a:
26796 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26797 +               break;
26798 +       case Opt_diropq_w:
26799 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26800 +               break;
26801 +
26802 +       case Opt_warn_perm:
26803 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26804 +               break;
26805 +       case Opt_nowarn_perm:
26806 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26807 +               break;
26808 +
26809 +       case Opt_verbose:
26810 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26811 +               break;
26812 +       case Opt_noverbose:
26813 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26814 +               break;
26815 +
26816 +       case Opt_sum:
26817 +               au_opt_set(sbinfo->si_mntflags, SUM);
26818 +               break;
26819 +       case Opt_wsum:
26820 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26821 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26822 +       case Opt_nosum:
26823 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26824 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26825 +               break;
26826 +
26827 +       case Opt_wbr_create:
26828 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26829 +               break;
26830 +       case Opt_wbr_copyup:
26831 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26832 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26833 +               break;
26834 +
26835 +       case Opt_dirwh:
26836 +               sbinfo->si_dirwh = opt->dirwh;
26837 +               break;
26838 +
26839 +       case Opt_rdcache:
26840 +               sbinfo->si_rdcache
26841 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26842 +               break;
26843 +       case Opt_rdblk:
26844 +               sbinfo->si_rdblk = opt->rdblk;
26845 +               break;
26846 +       case Opt_rdblk_def:
26847 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26848 +               break;
26849 +       case Opt_rdhash:
26850 +               sbinfo->si_rdhash = opt->rdhash;
26851 +               break;
26852 +       case Opt_rdhash_def:
26853 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26854 +               break;
26855 +
26856 +       case Opt_shwh:
26857 +               au_opt_set(sbinfo->si_mntflags, SHWH);
26858 +               break;
26859 +       case Opt_noshwh:
26860 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
26861 +               break;
26862 +
26863 +       case Opt_dirperm1:
26864 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26865 +               break;
26866 +       case Opt_nodirperm1:
26867 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26868 +               break;
26869 +
26870 +       case Opt_trunc_xino:
26871 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26872 +               break;
26873 +       case Opt_notrunc_xino:
26874 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26875 +               break;
26876 +
26877 +       case Opt_trunc_xino_path:
26878 +       case Opt_itrunc_xino:
26879 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
26880 +               if (!err)
26881 +                       err = 1;
26882 +               break;
26883 +
26884 +       case Opt_trunc_xib:
26885 +               au_fset_opts(opts->flags, TRUNC_XIB);
26886 +               break;
26887 +       case Opt_notrunc_xib:
26888 +               au_fclr_opts(opts->flags, TRUNC_XIB);
26889 +               break;
26890 +
26891 +       case Opt_dirren:
26892 +               err = 1;
26893 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26894 +                       err = au_dr_opt_set(sb);
26895 +                       if (!err)
26896 +                               err = 1;
26897 +               }
26898 +               if (err == 1)
26899 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
26900 +               break;
26901 +       case Opt_nodirren:
26902 +               err = 1;
26903 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
26904 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
26905 +                                                             DR_FLUSHED));
26906 +                       if (!err)
26907 +                               err = 1;
26908 +               }
26909 +               if (err == 1)
26910 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
26911 +               break;
26912 +
26913 +       case Opt_acl:
26914 +               sb->s_flags |= SB_POSIXACL;
26915 +               break;
26916 +       case Opt_noacl:
26917 +               sb->s_flags &= ~SB_POSIXACL;
26918 +               break;
26919 +
26920 +       default:
26921 +               err = 0;
26922 +               break;
26923 +       }
26924 +
26925 +       return err;
26926 +}
26927 +
26928 +/*
26929 + * returns tri-state.
26930 + * plus: processed without an error
26931 + * zero: unprocessed
26932 + * minus: error
26933 + */
26934 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
26935 +                    struct au_opts *opts)
26936 +{
26937 +       int err, do_refresh;
26938 +
26939 +       err = 0;
26940 +       switch (opt->type) {
26941 +       case Opt_append:
26942 +               opt->add.bindex = au_sbbot(sb) + 1;
26943 +               if (opt->add.bindex < 0)
26944 +                       opt->add.bindex = 0;
26945 +               goto add;
26946 +       case Opt_prepend:
26947 +               opt->add.bindex = 0;
26948 +       add: /* indented label */
26949 +       case Opt_add:
26950 +               err = au_br_add(sb, &opt->add,
26951 +                               au_ftest_opts(opts->flags, REMOUNT));
26952 +               if (!err) {
26953 +                       err = 1;
26954 +                       au_fset_opts(opts->flags, REFRESH);
26955 +               }
26956 +               break;
26957 +
26958 +       case Opt_del:
26959 +       case Opt_idel:
26960 +               err = au_br_del(sb, &opt->del,
26961 +                               au_ftest_opts(opts->flags, REMOUNT));
26962 +               if (!err) {
26963 +                       err = 1;
26964 +                       au_fset_opts(opts->flags, TRUNC_XIB);
26965 +                       au_fset_opts(opts->flags, REFRESH);
26966 +               }
26967 +               break;
26968 +
26969 +       case Opt_mod:
26970 +       case Opt_imod:
26971 +               err = au_br_mod(sb, &opt->mod,
26972 +                               au_ftest_opts(opts->flags, REMOUNT),
26973 +                               &do_refresh);
26974 +               if (!err) {
26975 +                       err = 1;
26976 +                       if (do_refresh)
26977 +                               au_fset_opts(opts->flags, REFRESH);
26978 +               }
26979 +               break;
26980 +       }
26981 +       return err;
26982 +}
26983 +
26984 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
26985 +                      struct au_opt_xino **opt_xino,
26986 +                      struct au_opts *opts)
26987 +{
26988 +       int err;
26989 +       aufs_bindex_t bbot, bindex;
26990 +       struct dentry *root, *parent, *h_root;
26991 +
26992 +       err = 0;
26993 +       switch (opt->type) {
26994 +       case Opt_xino:
26995 +               au_xino_brid_set(sb, -1);
26996 +               /* safe d_parent access */
26997 +               parent = opt->xino.file->f_path.dentry->d_parent;
26998 +               root = sb->s_root;
26999 +               bbot = au_sbbot(sb);
27000 +               for (bindex = 0; bindex <= bbot; bindex++) {
27001 +                       h_root = au_h_dptr(root, bindex);
27002 +                       if (h_root == parent) {
27003 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
27004 +                               break;
27005 +                       }
27006 +               }
27007 +
27008 +               err = au_xino_set(sb, &opt->xino,
27009 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27010 +               if (unlikely(err))
27011 +                       break;
27012 +
27013 +               *opt_xino = &opt->xino;
27014 +               break;
27015 +
27016 +       case Opt_noxino:
27017 +               au_xino_clr(sb);
27018 +               *opt_xino = (void *)-1;
27019 +               break;
27020 +       }
27021 +
27022 +       return err;
27023 +}
27024 +
27025 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27026 +                  unsigned int pending)
27027 +{
27028 +       int err, fhsm;
27029 +       aufs_bindex_t bindex, bbot;
27030 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27031 +       struct au_branch *br;
27032 +       struct au_wbr *wbr;
27033 +       struct dentry *root, *dentry;
27034 +       struct inode *dir, *h_dir;
27035 +       struct au_sbinfo *sbinfo;
27036 +       struct au_hinode *hdir;
27037 +
27038 +       SiMustAnyLock(sb);
27039 +
27040 +       sbinfo = au_sbi(sb);
27041 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27042 +
27043 +       if (!(sb_flags & SB_RDONLY)) {
27044 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27045 +                       pr_warn("first branch should be rw\n");
27046 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27047 +                       pr_warn_once("shwh should be used with ro\n");
27048 +       }
27049 +
27050 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27051 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27052 +               pr_warn_once("udba=*notify requires xino\n");
27053 +
27054 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27055 +               pr_warn_once("dirperm1 breaks the protection"
27056 +                            " by the permission bits on the lower branch\n");
27057 +
27058 +       err = 0;
27059 +       fhsm = 0;
27060 +       root = sb->s_root;
27061 +       dir = d_inode(root);
27062 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27063 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27064 +                                     UDBA_NONE);
27065 +       bbot = au_sbbot(sb);
27066 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27067 +               skip = 0;
27068 +               h_dir = au_h_iptr(dir, bindex);
27069 +               br = au_sbr(sb, bindex);
27070 +
27071 +               if ((br->br_perm & AuBrAttr_ICEX)
27072 +                   && !h_dir->i_op->listxattr)
27073 +                       br->br_perm &= ~AuBrAttr_ICEX;
27074 +#if 0
27075 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27076 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27077 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27078 +#endif
27079 +
27080 +               do_free = 0;
27081 +               wbr = br->br_wbr;
27082 +               if (wbr)
27083 +                       wbr_wh_read_lock(wbr);
27084 +
27085 +               if (!au_br_writable(br->br_perm)) {
27086 +                       do_free = !!wbr;
27087 +                       skip = (!wbr
27088 +                               || (!wbr->wbr_whbase
27089 +                                   && !wbr->wbr_plink
27090 +                                   && !wbr->wbr_orph));
27091 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27092 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27093 +                       skip = (!wbr || !wbr->wbr_whbase);
27094 +                       if (skip && wbr) {
27095 +                               if (do_plink)
27096 +                                       skip = !!wbr->wbr_plink;
27097 +                               else
27098 +                                       skip = !wbr->wbr_plink;
27099 +                       }
27100 +               } else {
27101 +                       /* skip = (br->br_whbase && br->br_ohph); */
27102 +                       skip = (wbr && wbr->wbr_whbase);
27103 +                       if (skip) {
27104 +                               if (do_plink)
27105 +                                       skip = !!wbr->wbr_plink;
27106 +                               else
27107 +                                       skip = !wbr->wbr_plink;
27108 +                       }
27109 +               }
27110 +               if (wbr)
27111 +                       wbr_wh_read_unlock(wbr);
27112 +
27113 +               if (can_no_dreval) {
27114 +                       dentry = br->br_path.dentry;
27115 +                       spin_lock(&dentry->d_lock);
27116 +                       if (dentry->d_flags &
27117 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27118 +                               can_no_dreval = 0;
27119 +                       spin_unlock(&dentry->d_lock);
27120 +               }
27121 +
27122 +               if (au_br_fhsm(br->br_perm)) {
27123 +                       fhsm++;
27124 +                       AuDebugOn(!br->br_fhsm);
27125 +               }
27126 +
27127 +               if (skip)
27128 +                       continue;
27129 +
27130 +               hdir = au_hi(dir, bindex);
27131 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27132 +               if (wbr)
27133 +                       wbr_wh_write_lock(wbr);
27134 +               err = au_wh_init(br, sb);
27135 +               if (wbr)
27136 +                       wbr_wh_write_unlock(wbr);
27137 +               au_hn_inode_unlock(hdir);
27138 +
27139 +               if (!err && do_free) {
27140 +                       kfree(wbr);
27141 +                       br->br_wbr = NULL;
27142 +               }
27143 +       }
27144 +
27145 +       if (can_no_dreval)
27146 +               au_fset_si(sbinfo, NO_DREVAL);
27147 +       else
27148 +               au_fclr_si(sbinfo, NO_DREVAL);
27149 +
27150 +       if (fhsm >= 2) {
27151 +               au_fset_si(sbinfo, FHSM);
27152 +               for (bindex = bbot; bindex >= 0; bindex--) {
27153 +                       br = au_sbr(sb, bindex);
27154 +                       if (au_br_fhsm(br->br_perm)) {
27155 +                               au_fhsm_set_bottom(sb, bindex);
27156 +                               break;
27157 +                       }
27158 +               }
27159 +       } else {
27160 +               au_fclr_si(sbinfo, FHSM);
27161 +               au_fhsm_set_bottom(sb, -1);
27162 +       }
27163 +
27164 +       return err;
27165 +}
27166 +
27167 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27168 +{
27169 +       int err;
27170 +       unsigned int tmp;
27171 +       aufs_bindex_t bindex, bbot;
27172 +       struct au_opt *opt;
27173 +       struct au_opt_xino *opt_xino, xino;
27174 +       struct au_sbinfo *sbinfo;
27175 +       struct au_branch *br;
27176 +       struct inode *dir;
27177 +
27178 +       SiMustWriteLock(sb);
27179 +
27180 +       err = 0;
27181 +       opt_xino = NULL;
27182 +       opt = opts->opt;
27183 +       while (err >= 0 && opt->type != Opt_tail)
27184 +               err = au_opt_simple(sb, opt++, opts);
27185 +       if (err > 0)
27186 +               err = 0;
27187 +       else if (unlikely(err < 0))
27188 +               goto out;
27189 +
27190 +       /* disable xino and udba temporary */
27191 +       sbinfo = au_sbi(sb);
27192 +       tmp = sbinfo->si_mntflags;
27193 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27194 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27195 +
27196 +       opt = opts->opt;
27197 +       while (err >= 0 && opt->type != Opt_tail)
27198 +               err = au_opt_br(sb, opt++, opts);
27199 +       if (err > 0)
27200 +               err = 0;
27201 +       else if (unlikely(err < 0))
27202 +               goto out;
27203 +
27204 +       bbot = au_sbbot(sb);
27205 +       if (unlikely(bbot < 0)) {
27206 +               err = -EINVAL;
27207 +               pr_err("no branches\n");
27208 +               goto out;
27209 +       }
27210 +
27211 +       if (au_opt_test(tmp, XINO))
27212 +               au_opt_set(sbinfo->si_mntflags, XINO);
27213 +       opt = opts->opt;
27214 +       while (!err && opt->type != Opt_tail)
27215 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27216 +       if (unlikely(err))
27217 +               goto out;
27218 +
27219 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27220 +       if (unlikely(err))
27221 +               goto out;
27222 +
27223 +       /* restore xino */
27224 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27225 +               xino.file = au_xino_def(sb);
27226 +               err = PTR_ERR(xino.file);
27227 +               if (IS_ERR(xino.file))
27228 +                       goto out;
27229 +
27230 +               err = au_xino_set(sb, &xino, /*remount*/0);
27231 +               fput(xino.file);
27232 +               if (unlikely(err))
27233 +                       goto out;
27234 +       }
27235 +
27236 +       /* restore udba */
27237 +       tmp &= AuOptMask_UDBA;
27238 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27239 +       sbinfo->si_mntflags |= tmp;
27240 +       bbot = au_sbbot(sb);
27241 +       for (bindex = 0; bindex <= bbot; bindex++) {
27242 +               br = au_sbr(sb, bindex);
27243 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27244 +               if (unlikely(err))
27245 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27246 +                               bindex, err);
27247 +               /* go on even if err */
27248 +       }
27249 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27250 +               dir = d_inode(sb->s_root);
27251 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27252 +       }
27253 +
27254 +out:
27255 +       return err;
27256 +}
27257 +
27258 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27259 +{
27260 +       int err, rerr;
27261 +       unsigned char no_dreval;
27262 +       struct inode *dir;
27263 +       struct au_opt_xino *opt_xino;
27264 +       struct au_opt *opt;
27265 +       struct au_sbinfo *sbinfo;
27266 +
27267 +       SiMustWriteLock(sb);
27268 +
27269 +       err = au_dr_opt_flush(sb);
27270 +       if (unlikely(err))
27271 +               goto out;
27272 +       au_fset_opts(opts->flags, DR_FLUSHED);
27273 +
27274 +       dir = d_inode(sb->s_root);
27275 +       sbinfo = au_sbi(sb);
27276 +       opt_xino = NULL;
27277 +       opt = opts->opt;
27278 +       while (err >= 0 && opt->type != Opt_tail) {
27279 +               err = au_opt_simple(sb, opt, opts);
27280 +               if (!err)
27281 +                       err = au_opt_br(sb, opt, opts);
27282 +               if (!err)
27283 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27284 +               opt++;
27285 +       }
27286 +       if (err > 0)
27287 +               err = 0;
27288 +       AuTraceErr(err);
27289 +       /* go on even err */
27290 +
27291 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27292 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27293 +       if (unlikely(rerr && !err))
27294 +               err = rerr;
27295 +
27296 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27297 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27298 +
27299 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27300 +               rerr = au_xib_trunc(sb);
27301 +               if (unlikely(rerr && !err))
27302 +                       err = rerr;
27303 +       }
27304 +
27305 +       /* will be handled by the caller */
27306 +       if (!au_ftest_opts(opts->flags, REFRESH)
27307 +           && (opts->given_udba
27308 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27309 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27310 +                   ))
27311 +               au_fset_opts(opts->flags, REFRESH);
27312 +
27313 +       AuDbg("status 0x%x\n", opts->flags);
27314 +
27315 +out:
27316 +       return err;
27317 +}
27318 +
27319 +/* ---------------------------------------------------------------------- */
27320 +
27321 +unsigned int au_opt_udba(struct super_block *sb)
27322 +{
27323 +       return au_mntflags(sb) & AuOptMask_UDBA;
27324 +}
27325 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27326 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27327 +++ linux/fs/aufs/opts.h        2018-07-19 09:46:13.063314675 +0200
27328 @@ -0,0 +1,225 @@
27329 +/* SPDX-License-Identifier: GPL-2.0 */
27330 +/*
27331 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27332 + *
27333 + * This program, aufs is free software; you can redistribute it and/or modify
27334 + * it under the terms of the GNU General Public License as published by
27335 + * the Free Software Foundation; either version 2 of the License, or
27336 + * (at your option) any later version.
27337 + *
27338 + * This program is distributed in the hope that it will be useful,
27339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27341 + * GNU General Public License for more details.
27342 + *
27343 + * You should have received a copy of the GNU General Public License
27344 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27345 + */
27346 +
27347 +/*
27348 + * mount options/flags
27349 + */
27350 +
27351 +#ifndef __AUFS_OPTS_H__
27352 +#define __AUFS_OPTS_H__
27353 +
27354 +#ifdef __KERNEL__
27355 +
27356 +#include <linux/path.h>
27357 +
27358 +struct file;
27359 +
27360 +/* ---------------------------------------------------------------------- */
27361 +
27362 +/* mount flags */
27363 +#define AuOpt_XINO             1               /* external inode number bitmap
27364 +                                                  and translation table */
27365 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27366 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27367 +#define AuOpt_UDBA_REVAL       (1 << 3)
27368 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27369 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27370 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27371 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27372 +                                                  bits */
27373 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27374 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27375 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27376 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27377 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
27378 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27379 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27380 +
27381 +#ifndef CONFIG_AUFS_HNOTIFY
27382 +#undef AuOpt_UDBA_HNOTIFY
27383 +#define AuOpt_UDBA_HNOTIFY     0
27384 +#endif
27385 +#ifndef CONFIG_AUFS_DIRREN
27386 +#undef AuOpt_DIRREN
27387 +#define AuOpt_DIRREN           0
27388 +#endif
27389 +#ifndef CONFIG_AUFS_SHWH
27390 +#undef AuOpt_SHWH
27391 +#define AuOpt_SHWH             0
27392 +#endif
27393 +
27394 +#define AuOpt_Def      (AuOpt_XINO \
27395 +                        | AuOpt_UDBA_REVAL \
27396 +                        | AuOpt_PLINK \
27397 +                        /* | AuOpt_DIRPERM1 */ \
27398 +                        | AuOpt_WARN_PERM)
27399 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27400 +                        | AuOpt_UDBA_REVAL \
27401 +                        | AuOpt_UDBA_HNOTIFY)
27402 +
27403 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27404 +#define au_opt_set(flags, name) do { \
27405 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27406 +       ((flags) |= AuOpt_##name); \
27407 +} while (0)
27408 +#define au_opt_set_udba(flags, name) do { \
27409 +       (flags) &= ~AuOptMask_UDBA; \
27410 +       ((flags) |= AuOpt_##name); \
27411 +} while (0)
27412 +#define au_opt_clr(flags, name) do { \
27413 +       ((flags) &= ~AuOpt_##name); \
27414 +} while (0)
27415 +
27416 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27417 +{
27418 +#ifdef CONFIG_PROC_FS
27419 +       return mntflags;
27420 +#else
27421 +       return mntflags & ~AuOpt_PLINK;
27422 +#endif
27423 +}
27424 +
27425 +/* ---------------------------------------------------------------------- */
27426 +
27427 +/* policies to select one among multiple writable branches */
27428 +enum {
27429 +       AuWbrCreate_TDP,        /* top down parent */
27430 +       AuWbrCreate_RR,         /* round robin */
27431 +       AuWbrCreate_MFS,        /* most free space */
27432 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27433 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27434 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27435 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27436 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27437 +       AuWbrCreate_PMFS,       /* parent and mfs */
27438 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27439 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27440 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27441 +
27442 +       AuWbrCreate_Def = AuWbrCreate_TDP
27443 +};
27444 +
27445 +enum {
27446 +       AuWbrCopyup_TDP,        /* top down parent */
27447 +       AuWbrCopyup_BUP,        /* bottom up parent */
27448 +       AuWbrCopyup_BU,         /* bottom up */
27449 +
27450 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27451 +};
27452 +
27453 +/* ---------------------------------------------------------------------- */
27454 +
27455 +struct au_opt_add {
27456 +       aufs_bindex_t   bindex;
27457 +       char            *pathname;
27458 +       int             perm;
27459 +       struct path     path;
27460 +};
27461 +
27462 +struct au_opt_del {
27463 +       char            *pathname;
27464 +       struct path     h_path;
27465 +};
27466 +
27467 +struct au_opt_mod {
27468 +       char            *path;
27469 +       int             perm;
27470 +       struct dentry   *h_root;
27471 +};
27472 +
27473 +struct au_opt_xino {
27474 +       char            *path;
27475 +       struct file     *file;
27476 +};
27477 +
27478 +struct au_opt_xino_itrunc {
27479 +       aufs_bindex_t   bindex;
27480 +};
27481 +
27482 +struct au_opt_wbr_create {
27483 +       int                     wbr_create;
27484 +       int                     mfs_second;
27485 +       unsigned long long      mfsrr_watermark;
27486 +};
27487 +
27488 +struct au_opt {
27489 +       int type;
27490 +       union {
27491 +               struct au_opt_xino      xino;
27492 +               struct au_opt_xino_itrunc xino_itrunc;
27493 +               struct au_opt_add       add;
27494 +               struct au_opt_del       del;
27495 +               struct au_opt_mod       mod;
27496 +               int                     dirwh;
27497 +               int                     rdcache;
27498 +               unsigned int            rdblk;
27499 +               unsigned int            rdhash;
27500 +               int                     udba;
27501 +               struct au_opt_wbr_create wbr_create;
27502 +               int                     wbr_copyup;
27503 +               unsigned int            fhsm_second;
27504 +       };
27505 +};
27506 +
27507 +/* opts flags */
27508 +#define AuOpts_REMOUNT         1
27509 +#define AuOpts_REFRESH         (1 << 1)
27510 +#define AuOpts_TRUNC_XIB       (1 << 2)
27511 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27512 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27513 +#define AuOpts_DR_FLUSHED      (1 << 5)
27514 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27515 +#define au_fset_opts(flags, name) \
27516 +       do { (flags) |= AuOpts_##name; } while (0)
27517 +#define au_fclr_opts(flags, name) \
27518 +       do { (flags) &= ~AuOpts_##name; } while (0)
27519 +
27520 +#ifndef CONFIG_AUFS_DIRREN
27521 +#undef AuOpts_DR_FLUSHED
27522 +#define AuOpts_DR_FLUSHED      0
27523 +#endif
27524 +
27525 +struct au_opts {
27526 +       struct au_opt   *opt;
27527 +       int             max_opt;
27528 +
27529 +       unsigned int    given_udba;
27530 +       unsigned int    flags;
27531 +       unsigned long   sb_flags;
27532 +};
27533 +
27534 +/* ---------------------------------------------------------------------- */
27535 +
27536 +/* opts.c */
27537 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27538 +const char *au_optstr_udba(int udba);
27539 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27540 +const char *au_optstr_wbr_create(int wbr_create);
27541 +
27542 +void au_opts_free(struct au_opts *opts);
27543 +struct super_block;
27544 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27545 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27546 +                  unsigned int pending);
27547 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27548 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27549 +
27550 +unsigned int au_opt_udba(struct super_block *sb);
27551 +
27552 +#endif /* __KERNEL__ */
27553 +#endif /* __AUFS_OPTS_H__ */
27554 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27555 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27556 +++ linux/fs/aufs/plink.c       2018-07-19 09:46:13.063314675 +0200
27557 @@ -0,0 +1,516 @@
27558 +/* SPDX-License-Identifier: GPL-2.0 */
27559 +/*
27560 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27561 + *
27562 + * This program, aufs is free software; you can redistribute it and/or modify
27563 + * it under the terms of the GNU General Public License as published by
27564 + * the Free Software Foundation; either version 2 of the License, or
27565 + * (at your option) any later version.
27566 + *
27567 + * This program is distributed in the hope that it will be useful,
27568 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27569 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27570 + * GNU General Public License for more details.
27571 + *
27572 + * You should have received a copy of the GNU General Public License
27573 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27574 + */
27575 +
27576 +/*
27577 + * pseudo-link
27578 + */
27579 +
27580 +#include "aufs.h"
27581 +
27582 +/*
27583 + * the pseudo-link maintenance mode.
27584 + * during a user process maintains the pseudo-links,
27585 + * prohibit adding a new plink and branch manipulation.
27586 + *
27587 + * Flags
27588 + * NOPLM:
27589 + *     For entry functions which will handle plink, and i_mutex is already held
27590 + *     in VFS.
27591 + *     They cannot wait and should return an error at once.
27592 + *     Callers has to check the error.
27593 + * NOPLMW:
27594 + *     For entry functions which will handle plink, but i_mutex is not held
27595 + *     in VFS.
27596 + *     They can wait the plink maintenance mode to finish.
27597 + *
27598 + * They behave like F_SETLK and F_SETLKW.
27599 + * If the caller never handle plink, then both flags are unnecessary.
27600 + */
27601 +
27602 +int au_plink_maint(struct super_block *sb, int flags)
27603 +{
27604 +       int err;
27605 +       pid_t pid, ppid;
27606 +       struct task_struct *parent, *prev;
27607 +       struct au_sbinfo *sbi;
27608 +
27609 +       SiMustAnyLock(sb);
27610 +
27611 +       err = 0;
27612 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27613 +               goto out;
27614 +
27615 +       sbi = au_sbi(sb);
27616 +       pid = sbi->si_plink_maint_pid;
27617 +       if (!pid || pid == current->pid)
27618 +               goto out;
27619 +
27620 +       /* todo: it highly depends upon /sbin/mount.aufs */
27621 +       prev = NULL;
27622 +       parent = current;
27623 +       ppid = 0;
27624 +       rcu_read_lock();
27625 +       while (1) {
27626 +               parent = rcu_dereference(parent->real_parent);
27627 +               if (parent == prev)
27628 +                       break;
27629 +               ppid = task_pid_vnr(parent);
27630 +               if (pid == ppid) {
27631 +                       rcu_read_unlock();
27632 +                       goto out;
27633 +               }
27634 +               prev = parent;
27635 +       }
27636 +       rcu_read_unlock();
27637 +
27638 +       if (au_ftest_lock(flags, NOPLMW)) {
27639 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27640 +               /* AuDebugOn(!lockdep_depth(current)); */
27641 +               while (sbi->si_plink_maint_pid) {
27642 +                       si_read_unlock(sb);
27643 +                       /* gave up wake_up_bit() */
27644 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27645 +
27646 +                       if (au_ftest_lock(flags, FLUSH))
27647 +                               au_nwt_flush(&sbi->si_nowait);
27648 +                       si_noflush_read_lock(sb);
27649 +               }
27650 +       } else if (au_ftest_lock(flags, NOPLM)) {
27651 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27652 +               err = -EAGAIN;
27653 +       }
27654 +
27655 +out:
27656 +       return err;
27657 +}
27658 +
27659 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27660 +{
27661 +       spin_lock(&sbinfo->si_plink_maint_lock);
27662 +       sbinfo->si_plink_maint_pid = 0;
27663 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27664 +       wake_up_all(&sbinfo->si_plink_wq);
27665 +}
27666 +
27667 +int au_plink_maint_enter(struct super_block *sb)
27668 +{
27669 +       int err;
27670 +       struct au_sbinfo *sbinfo;
27671 +
27672 +       err = 0;
27673 +       sbinfo = au_sbi(sb);
27674 +       /* make sure i am the only one in this fs */
27675 +       si_write_lock(sb, AuLock_FLUSH);
27676 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27677 +               spin_lock(&sbinfo->si_plink_maint_lock);
27678 +               if (!sbinfo->si_plink_maint_pid)
27679 +                       sbinfo->si_plink_maint_pid = current->pid;
27680 +               else
27681 +                       err = -EBUSY;
27682 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27683 +       }
27684 +       si_write_unlock(sb);
27685 +
27686 +       return err;
27687 +}
27688 +
27689 +/* ---------------------------------------------------------------------- */
27690 +
27691 +#ifdef CONFIG_AUFS_DEBUG
27692 +void au_plink_list(struct super_block *sb)
27693 +{
27694 +       int i;
27695 +       struct au_sbinfo *sbinfo;
27696 +       struct hlist_bl_head *hbl;
27697 +       struct hlist_bl_node *pos;
27698 +       struct au_icntnr *icntnr;
27699 +
27700 +       SiMustAnyLock(sb);
27701 +
27702 +       sbinfo = au_sbi(sb);
27703 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27704 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27705 +
27706 +       for (i = 0; i < AuPlink_NHASH; i++) {
27707 +               hbl = sbinfo->si_plink + i;
27708 +               hlist_bl_lock(hbl);
27709 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27710 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27711 +               hlist_bl_unlock(hbl);
27712 +       }
27713 +}
27714 +#endif
27715 +
27716 +/* is the inode pseudo-linked? */
27717 +int au_plink_test(struct inode *inode)
27718 +{
27719 +       int found, i;
27720 +       struct au_sbinfo *sbinfo;
27721 +       struct hlist_bl_head *hbl;
27722 +       struct hlist_bl_node *pos;
27723 +       struct au_icntnr *icntnr;
27724 +
27725 +       sbinfo = au_sbi(inode->i_sb);
27726 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27727 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27728 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27729 +
27730 +       found = 0;
27731 +       i = au_plink_hash(inode->i_ino);
27732 +       hbl =  sbinfo->si_plink + i;
27733 +       hlist_bl_lock(hbl);
27734 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27735 +               if (&icntnr->vfs_inode == inode) {
27736 +                       found = 1;
27737 +                       break;
27738 +               }
27739 +       hlist_bl_unlock(hbl);
27740 +       return found;
27741 +}
27742 +
27743 +/* ---------------------------------------------------------------------- */
27744 +
27745 +/*
27746 + * generate a name for plink.
27747 + * the file will be stored under AUFS_WH_PLINKDIR.
27748 + */
27749 +/* 20 is max digits length of ulong 64 */
27750 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27751 +
27752 +static int plink_name(char *name, int len, struct inode *inode,
27753 +                     aufs_bindex_t bindex)
27754 +{
27755 +       int rlen;
27756 +       struct inode *h_inode;
27757 +
27758 +       h_inode = au_h_iptr(inode, bindex);
27759 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27760 +       return rlen;
27761 +}
27762 +
27763 +struct au_do_plink_lkup_args {
27764 +       struct dentry **errp;
27765 +       struct qstr *tgtname;
27766 +       struct dentry *h_parent;
27767 +       struct au_branch *br;
27768 +};
27769 +
27770 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27771 +                                      struct dentry *h_parent,
27772 +                                      struct au_branch *br)
27773 +{
27774 +       struct dentry *h_dentry;
27775 +       struct inode *h_inode;
27776 +
27777 +       h_inode = d_inode(h_parent);
27778 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27779 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
27780 +       inode_unlock_shared(h_inode);
27781 +       return h_dentry;
27782 +}
27783 +
27784 +static void au_call_do_plink_lkup(void *args)
27785 +{
27786 +       struct au_do_plink_lkup_args *a = args;
27787 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27788 +}
27789 +
27790 +/* lookup the plink-ed @inode under the branch at @bindex */
27791 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27792 +{
27793 +       struct dentry *h_dentry, *h_parent;
27794 +       struct au_branch *br;
27795 +       int wkq_err;
27796 +       char a[PLINK_NAME_LEN];
27797 +       struct qstr tgtname = QSTR_INIT(a, 0);
27798 +
27799 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27800 +
27801 +       br = au_sbr(inode->i_sb, bindex);
27802 +       h_parent = br->br_wbr->wbr_plink;
27803 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27804 +
27805 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27806 +               struct au_do_plink_lkup_args args = {
27807 +                       .errp           = &h_dentry,
27808 +                       .tgtname        = &tgtname,
27809 +                       .h_parent       = h_parent,
27810 +                       .br             = br
27811 +               };
27812 +
27813 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27814 +               if (unlikely(wkq_err))
27815 +                       h_dentry = ERR_PTR(wkq_err);
27816 +       } else
27817 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27818 +
27819 +       return h_dentry;
27820 +}
27821 +
27822 +/* create a pseudo-link */
27823 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27824 +                     struct dentry *h_dentry, struct au_branch *br)
27825 +{
27826 +       int err;
27827 +       struct path h_path = {
27828 +               .mnt = au_br_mnt(br)
27829 +       };
27830 +       struct inode *h_dir, *delegated;
27831 +
27832 +       h_dir = d_inode(h_parent);
27833 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27834 +again:
27835 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27836 +       err = PTR_ERR(h_path.dentry);
27837 +       if (IS_ERR(h_path.dentry))
27838 +               goto out;
27839 +
27840 +       err = 0;
27841 +       /* wh.plink dir is not monitored */
27842 +       /* todo: is it really safe? */
27843 +       if (d_is_positive(h_path.dentry)
27844 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27845 +               delegated = NULL;
27846 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27847 +               if (unlikely(err == -EWOULDBLOCK)) {
27848 +                       pr_warn("cannot retry for NFSv4 delegation"
27849 +                               " for an internal unlink\n");
27850 +                       iput(delegated);
27851 +               }
27852 +               dput(h_path.dentry);
27853 +               h_path.dentry = NULL;
27854 +               if (!err)
27855 +                       goto again;
27856 +       }
27857 +       if (!err && d_is_negative(h_path.dentry)) {
27858 +               delegated = NULL;
27859 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27860 +               if (unlikely(err == -EWOULDBLOCK)) {
27861 +                       pr_warn("cannot retry for NFSv4 delegation"
27862 +                               " for an internal link\n");
27863 +                       iput(delegated);
27864 +               }
27865 +       }
27866 +       dput(h_path.dentry);
27867 +
27868 +out:
27869 +       inode_unlock(h_dir);
27870 +       return err;
27871 +}
27872 +
27873 +struct do_whplink_args {
27874 +       int *errp;
27875 +       struct qstr *tgt;
27876 +       struct dentry *h_parent;
27877 +       struct dentry *h_dentry;
27878 +       struct au_branch *br;
27879 +};
27880 +
27881 +static void call_do_whplink(void *args)
27882 +{
27883 +       struct do_whplink_args *a = args;
27884 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27885 +}
27886 +
27887 +static int whplink(struct dentry *h_dentry, struct inode *inode,
27888 +                  aufs_bindex_t bindex, struct au_branch *br)
27889 +{
27890 +       int err, wkq_err;
27891 +       struct au_wbr *wbr;
27892 +       struct dentry *h_parent;
27893 +       char a[PLINK_NAME_LEN];
27894 +       struct qstr tgtname = QSTR_INIT(a, 0);
27895 +
27896 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27897 +       h_parent = wbr->wbr_plink;
27898 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27899 +
27900 +       /* always superio. */
27901 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27902 +               struct do_whplink_args args = {
27903 +                       .errp           = &err,
27904 +                       .tgt            = &tgtname,
27905 +                       .h_parent       = h_parent,
27906 +                       .h_dentry       = h_dentry,
27907 +                       .br             = br
27908 +               };
27909 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
27910 +               if (unlikely(wkq_err))
27911 +                       err = wkq_err;
27912 +       } else
27913 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
27914 +
27915 +       return err;
27916 +}
27917 +
27918 +/*
27919 + * create a new pseudo-link for @h_dentry on @bindex.
27920 + * the linked inode is held in aufs @inode.
27921 + */
27922 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
27923 +                    struct dentry *h_dentry)
27924 +{
27925 +       struct super_block *sb;
27926 +       struct au_sbinfo *sbinfo;
27927 +       struct hlist_bl_head *hbl;
27928 +       struct hlist_bl_node *pos;
27929 +       struct au_icntnr *icntnr;
27930 +       int found, err, cnt, i;
27931 +
27932 +       sb = inode->i_sb;
27933 +       sbinfo = au_sbi(sb);
27934 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27935 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27936 +
27937 +       found = au_plink_test(inode);
27938 +       if (found)
27939 +               return;
27940 +
27941 +       i = au_plink_hash(inode->i_ino);
27942 +       hbl = sbinfo->si_plink + i;
27943 +       au_igrab(inode);
27944 +
27945 +       hlist_bl_lock(hbl);
27946 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
27947 +               if (&icntnr->vfs_inode == inode) {
27948 +                       found = 1;
27949 +                       break;
27950 +               }
27951 +       }
27952 +       if (!found) {
27953 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
27954 +               hlist_bl_add_head(&icntnr->plink, hbl);
27955 +       }
27956 +       hlist_bl_unlock(hbl);
27957 +       if (!found) {
27958 +               cnt = au_hbl_count(hbl);
27959 +#define msg "unexpectedly unblanced or too many pseudo-links"
27960 +               if (cnt > AUFS_PLINK_WARN)
27961 +                       AuWarn1(msg ", %d\n", cnt);
27962 +#undef msg
27963 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
27964 +               if (unlikely(err)) {
27965 +                       pr_warn("err %d, damaged pseudo link.\n", err);
27966 +                       au_hbl_del(&icntnr->plink, hbl);
27967 +                       iput(&icntnr->vfs_inode);
27968 +               }
27969 +       } else
27970 +               iput(&icntnr->vfs_inode);
27971 +}
27972 +
27973 +/* free all plinks */
27974 +void au_plink_put(struct super_block *sb, int verbose)
27975 +{
27976 +       int i, warned;
27977 +       struct au_sbinfo *sbinfo;
27978 +       struct hlist_bl_head *hbl;
27979 +       struct hlist_bl_node *pos, *tmp;
27980 +       struct au_icntnr *icntnr;
27981 +
27982 +       SiMustWriteLock(sb);
27983 +
27984 +       sbinfo = au_sbi(sb);
27985 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27986 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27987 +
27988 +       /* no spin_lock since sbinfo is write-locked */
27989 +       warned = 0;
27990 +       for (i = 0; i < AuPlink_NHASH; i++) {
27991 +               hbl = sbinfo->si_plink + i;
27992 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
27993 +                       pr_warn("pseudo-link is not flushed");
27994 +                       warned = 1;
27995 +               }
27996 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
27997 +                       iput(&icntnr->vfs_inode);
27998 +               INIT_HLIST_BL_HEAD(hbl);
27999 +       }
28000 +}
28001 +
28002 +void au_plink_clean(struct super_block *sb, int verbose)
28003 +{
28004 +       struct dentry *root;
28005 +
28006 +       root = sb->s_root;
28007 +       aufs_write_lock(root);
28008 +       if (au_opt_test(au_mntflags(sb), PLINK))
28009 +               au_plink_put(sb, verbose);
28010 +       aufs_write_unlock(root);
28011 +}
28012 +
28013 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28014 +{
28015 +       int do_put;
28016 +       aufs_bindex_t btop, bbot, bindex;
28017 +
28018 +       do_put = 0;
28019 +       btop = au_ibtop(inode);
28020 +       bbot = au_ibbot(inode);
28021 +       if (btop >= 0) {
28022 +               for (bindex = btop; bindex <= bbot; bindex++) {
28023 +                       if (!au_h_iptr(inode, bindex)
28024 +                           || au_ii_br_id(inode, bindex) != br_id)
28025 +                               continue;
28026 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28027 +                       do_put = 1;
28028 +                       break;
28029 +               }
28030 +               if (do_put)
28031 +                       for (bindex = btop; bindex <= bbot; bindex++)
28032 +                               if (au_h_iptr(inode, bindex)) {
28033 +                                       do_put = 0;
28034 +                                       break;
28035 +                               }
28036 +       } else
28037 +               do_put = 1;
28038 +
28039 +       return do_put;
28040 +}
28041 +
28042 +/* free the plinks on a branch specified by @br_id */
28043 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28044 +{
28045 +       struct au_sbinfo *sbinfo;
28046 +       struct hlist_bl_head *hbl;
28047 +       struct hlist_bl_node *pos, *tmp;
28048 +       struct au_icntnr *icntnr;
28049 +       struct inode *inode;
28050 +       int i, do_put;
28051 +
28052 +       SiMustWriteLock(sb);
28053 +
28054 +       sbinfo = au_sbi(sb);
28055 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28056 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28057 +
28058 +       /* no bit_lock since sbinfo is write-locked */
28059 +       for (i = 0; i < AuPlink_NHASH; i++) {
28060 +               hbl = sbinfo->si_plink + i;
28061 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28062 +                       inode = au_igrab(&icntnr->vfs_inode);
28063 +                       ii_write_lock_child(inode);
28064 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28065 +                       if (do_put) {
28066 +                               hlist_bl_del(&icntnr->plink);
28067 +                               iput(inode);
28068 +                       }
28069 +                       ii_write_unlock(inode);
28070 +                       iput(inode);
28071 +               }
28072 +       }
28073 +}
28074 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28075 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28076 +++ linux/fs/aufs/poll.c        2018-07-19 09:46:13.063314675 +0200
28077 @@ -0,0 +1,54 @@
28078 +/* SPDX-License-Identifier: GPL-2.0 */
28079 +/*
28080 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28081 + *
28082 + * This program, aufs is free software; you can redistribute it and/or modify
28083 + * it under the terms of the GNU General Public License as published by
28084 + * the Free Software Foundation; either version 2 of the License, or
28085 + * (at your option) any later version.
28086 + *
28087 + * This program is distributed in the hope that it will be useful,
28088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28090 + * GNU General Public License for more details.
28091 + *
28092 + * You should have received a copy of the GNU General Public License
28093 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28094 + */
28095 +
28096 +/*
28097 + * poll operation
28098 + * There is only one filesystem which implements ->poll operation, currently.
28099 + */
28100 +
28101 +#include "aufs.h"
28102 +
28103 +__poll_t aufs_poll(struct file *file, poll_table *wait)
28104 +{
28105 +       __poll_t mask;
28106 +       struct file *h_file;
28107 +       struct super_block *sb;
28108 +
28109 +       /* We should pretend an error happened. */
28110 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28111 +       sb = file->f_path.dentry->d_sb;
28112 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28113 +
28114 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28115 +       if (IS_ERR(h_file)) {
28116 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28117 +               goto out;
28118 +       }
28119 +
28120 +       /* it is not an error if h_file has no operation */
28121 +       mask = DEFAULT_POLLMASK;
28122 +       if (h_file->f_op->poll)
28123 +               mask = h_file->f_op->poll(h_file, wait);
28124 +       fput(h_file); /* instead of au_read_post() */
28125 +
28126 +out:
28127 +       si_read_unlock(sb);
28128 +       if (mask & EPOLLERR)
28129 +               AuDbg("mask 0x%x\n", mask);
28130 +       return mask;
28131 +}
28132 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28133 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28134 +++ linux/fs/aufs/posix_acl.c   2018-07-19 09:46:13.063314675 +0200
28135 @@ -0,0 +1,103 @@
28136 +/* SPDX-License-Identifier: GPL-2.0 */
28137 +/*
28138 + * Copyright (C) 2014-2018 Junjiro R. Okajima
28139 + *
28140 + * This program, aufs is free software; you can redistribute it and/or modify
28141 + * it under the terms of the GNU General Public License as published by
28142 + * the Free Software Foundation; either version 2 of the License, or
28143 + * (at your option) any later version.
28144 + *
28145 + * This program is distributed in the hope that it will be useful,
28146 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28147 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28148 + * GNU General Public License for more details.
28149 + *
28150 + * You should have received a copy of the GNU General Public License
28151 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28152 + */
28153 +
28154 +/*
28155 + * posix acl operations
28156 + */
28157 +
28158 +#include <linux/fs.h>
28159 +#include "aufs.h"
28160 +
28161 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28162 +{
28163 +       struct posix_acl *acl;
28164 +       int err;
28165 +       aufs_bindex_t bindex;
28166 +       struct inode *h_inode;
28167 +       struct super_block *sb;
28168 +
28169 +       acl = NULL;
28170 +       sb = inode->i_sb;
28171 +       si_read_lock(sb, AuLock_FLUSH);
28172 +       ii_read_lock_child(inode);
28173 +       if (!(sb->s_flags & SB_POSIXACL))
28174 +               goto out;
28175 +
28176 +       bindex = au_ibtop(inode);
28177 +       h_inode = au_h_iptr(inode, bindex);
28178 +       if (unlikely(!h_inode
28179 +                    || ((h_inode->i_mode & S_IFMT)
28180 +                        != (inode->i_mode & S_IFMT)))) {
28181 +               err = au_busy_or_stale();
28182 +               acl = ERR_PTR(err);
28183 +               goto out;
28184 +       }
28185 +
28186 +       /* always topmost only */
28187 +       acl = get_acl(h_inode, type);
28188 +       if (!IS_ERR_OR_NULL(acl))
28189 +               set_cached_acl(inode, type, acl);
28190 +
28191 +out:
28192 +       ii_read_unlock(inode);
28193 +       si_read_unlock(sb);
28194 +
28195 +       AuTraceErrPtr(acl);
28196 +       return acl;
28197 +}
28198 +
28199 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28200 +{
28201 +       int err;
28202 +       ssize_t ssz;
28203 +       struct dentry *dentry;
28204 +       struct au_sxattr arg = {
28205 +               .type = AU_ACL_SET,
28206 +               .u.acl_set = {
28207 +                       .acl    = acl,
28208 +                       .type   = type
28209 +               },
28210 +       };
28211 +
28212 +       IMustLock(inode);
28213 +
28214 +       if (inode->i_ino == AUFS_ROOT_INO)
28215 +               dentry = dget(inode->i_sb->s_root);
28216 +       else {
28217 +               dentry = d_find_alias(inode);
28218 +               if (!dentry)
28219 +                       dentry = d_find_any_alias(inode);
28220 +               if (!dentry) {
28221 +                       pr_warn("cannot handle this inode, "
28222 +                               "please report to aufs-users ML\n");
28223 +                       err = -ENOENT;
28224 +                       goto out;
28225 +               }
28226 +       }
28227 +
28228 +       ssz = au_sxattr(dentry, inode, &arg);
28229 +       dput(dentry);
28230 +       err = ssz;
28231 +       if (ssz >= 0) {
28232 +               err = 0;
28233 +               set_cached_acl(inode, type, acl);
28234 +       }
28235 +
28236 +out:
28237 +       return err;
28238 +}
28239 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28240 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28241 +++ linux/fs/aufs/procfs.c      2018-07-19 09:46:13.063314675 +0200
28242 @@ -0,0 +1,171 @@
28243 +/* SPDX-License-Identifier: GPL-2.0 */
28244 +/*
28245 + * Copyright (C) 2010-2018 Junjiro R. Okajima
28246 + *
28247 + * This program, aufs is free software; you can redistribute it and/or modify
28248 + * it under the terms of the GNU General Public License as published by
28249 + * the Free Software Foundation; either version 2 of the License, or
28250 + * (at your option) any later version.
28251 + *
28252 + * This program is distributed in the hope that it will be useful,
28253 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28254 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28255 + * GNU General Public License for more details.
28256 + *
28257 + * You should have received a copy of the GNU General Public License
28258 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28259 + */
28260 +
28261 +/*
28262 + * procfs interfaces
28263 + */
28264 +
28265 +#include <linux/proc_fs.h>
28266 +#include "aufs.h"
28267 +
28268 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28269 +{
28270 +       struct au_sbinfo *sbinfo;
28271 +
28272 +       sbinfo = file->private_data;
28273 +       if (sbinfo) {
28274 +               au_plink_maint_leave(sbinfo);
28275 +               kobject_put(&sbinfo->si_kobj);
28276 +       }
28277 +
28278 +       return 0;
28279 +}
28280 +
28281 +static void au_procfs_plm_write_clean(struct file *file)
28282 +{
28283 +       struct au_sbinfo *sbinfo;
28284 +
28285 +       sbinfo = file->private_data;
28286 +       if (sbinfo)
28287 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28288 +}
28289 +
28290 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28291 +{
28292 +       int err;
28293 +       struct super_block *sb;
28294 +       struct au_sbinfo *sbinfo;
28295 +       struct hlist_bl_node *pos;
28296 +
28297 +       err = -EBUSY;
28298 +       if (unlikely(file->private_data))
28299 +               goto out;
28300 +
28301 +       sb = NULL;
28302 +       /* don't use au_sbilist_lock() here */
28303 +       hlist_bl_lock(&au_sbilist);
28304 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28305 +               if (id == sysaufs_si_id(sbinfo)) {
28306 +                       kobject_get(&sbinfo->si_kobj);
28307 +                       sb = sbinfo->si_sb;
28308 +                       break;
28309 +               }
28310 +       hlist_bl_unlock(&au_sbilist);
28311 +
28312 +       err = -EINVAL;
28313 +       if (unlikely(!sb))
28314 +               goto out;
28315 +
28316 +       err = au_plink_maint_enter(sb);
28317 +       if (!err)
28318 +               /* keep kobject_get() */
28319 +               file->private_data = sbinfo;
28320 +       else
28321 +               kobject_put(&sbinfo->si_kobj);
28322 +out:
28323 +       return err;
28324 +}
28325 +
28326 +/*
28327 + * Accept a valid "si=xxxx" only.
28328 + * Once it is accepted successfully, accept "clean" too.
28329 + */
28330 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28331 +                                  size_t count, loff_t *ppos)
28332 +{
28333 +       ssize_t err;
28334 +       unsigned long id;
28335 +       /* last newline is allowed */
28336 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28337 +
28338 +       err = -EACCES;
28339 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28340 +               goto out;
28341 +
28342 +       err = -EINVAL;
28343 +       if (unlikely(count > sizeof(buf)))
28344 +               goto out;
28345 +
28346 +       err = copy_from_user(buf, ubuf, count);
28347 +       if (unlikely(err)) {
28348 +               err = -EFAULT;
28349 +               goto out;
28350 +       }
28351 +       buf[count] = 0;
28352 +
28353 +       err = -EINVAL;
28354 +       if (!strcmp("clean", buf)) {
28355 +               au_procfs_plm_write_clean(file);
28356 +               goto out_success;
28357 +       } else if (unlikely(strncmp("si=", buf, 3)))
28358 +               goto out;
28359 +
28360 +       err = kstrtoul(buf + 3, 16, &id);
28361 +       if (unlikely(err))
28362 +               goto out;
28363 +
28364 +       err = au_procfs_plm_write_si(file, id);
28365 +       if (unlikely(err))
28366 +               goto out;
28367 +
28368 +out_success:
28369 +       err = count; /* success */
28370 +out:
28371 +       return err;
28372 +}
28373 +
28374 +static const struct file_operations au_procfs_plm_fop = {
28375 +       .write          = au_procfs_plm_write,
28376 +       .release        = au_procfs_plm_release,
28377 +       .owner          = THIS_MODULE
28378 +};
28379 +
28380 +/* ---------------------------------------------------------------------- */
28381 +
28382 +static struct proc_dir_entry *au_procfs_dir;
28383 +
28384 +void au_procfs_fin(void)
28385 +{
28386 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28387 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28388 +}
28389 +
28390 +int __init au_procfs_init(void)
28391 +{
28392 +       int err;
28393 +       struct proc_dir_entry *entry;
28394 +
28395 +       err = -ENOMEM;
28396 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28397 +       if (unlikely(!au_procfs_dir))
28398 +               goto out;
28399 +
28400 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
28401 +                           au_procfs_dir, &au_procfs_plm_fop);
28402 +       if (unlikely(!entry))
28403 +               goto out_dir;
28404 +
28405 +       err = 0;
28406 +       goto out; /* success */
28407 +
28408 +
28409 +out_dir:
28410 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28411 +out:
28412 +       return err;
28413 +}
28414 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28415 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28416 +++ linux/fs/aufs/rdu.c 2018-07-19 09:46:13.063314675 +0200
28417 @@ -0,0 +1,382 @@
28418 +/* SPDX-License-Identifier: GPL-2.0 */
28419 +/*
28420 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28421 + *
28422 + * This program, aufs is free software; you can redistribute it and/or modify
28423 + * it under the terms of the GNU General Public License as published by
28424 + * the Free Software Foundation; either version 2 of the License, or
28425 + * (at your option) any later version.
28426 + *
28427 + * This program is distributed in the hope that it will be useful,
28428 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28429 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28430 + * GNU General Public License for more details.
28431 + *
28432 + * You should have received a copy of the GNU General Public License
28433 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28434 + */
28435 +
28436 +/*
28437 + * readdir in userspace.
28438 + */
28439 +
28440 +#include <linux/compat.h>
28441 +#include <linux/fs_stack.h>
28442 +#include <linux/security.h>
28443 +#include "aufs.h"
28444 +
28445 +/* bits for struct aufs_rdu.flags */
28446 +#define        AuRdu_CALLED    1
28447 +#define        AuRdu_CONT      (1 << 1)
28448 +#define        AuRdu_FULL      (1 << 2)
28449 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28450 +#define au_fset_rdu(flags, name) \
28451 +       do { (flags) |= AuRdu_##name; } while (0)
28452 +#define au_fclr_rdu(flags, name) \
28453 +       do { (flags) &= ~AuRdu_##name; } while (0)
28454 +
28455 +struct au_rdu_arg {
28456 +       struct dir_context              ctx;
28457 +       struct aufs_rdu                 *rdu;
28458 +       union au_rdu_ent_ul             ent;
28459 +       unsigned long                   end;
28460 +
28461 +       struct super_block              *sb;
28462 +       int                             err;
28463 +};
28464 +
28465 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28466 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28467 +{
28468 +       int err, len;
28469 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28470 +       struct aufs_rdu *rdu = arg->rdu;
28471 +       struct au_rdu_ent ent;
28472 +
28473 +       err = 0;
28474 +       arg->err = 0;
28475 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28476 +       len = au_rdu_len(nlen);
28477 +       if (arg->ent.ul + len  < arg->end) {
28478 +               ent.ino = h_ino;
28479 +               ent.bindex = rdu->cookie.bindex;
28480 +               ent.type = d_type;
28481 +               ent.nlen = nlen;
28482 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28483 +                       ent.type = DT_UNKNOWN;
28484 +
28485 +               /* unnecessary to support mmap_sem since this is a dir */
28486 +               err = -EFAULT;
28487 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28488 +                       goto out;
28489 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28490 +                       goto out;
28491 +               /* the terminating NULL */
28492 +               if (__put_user(0, arg->ent.e->name + nlen))
28493 +                       goto out;
28494 +               err = 0;
28495 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28496 +               arg->ent.ul += len;
28497 +               rdu->rent++;
28498 +       } else {
28499 +               err = -EFAULT;
28500 +               au_fset_rdu(rdu->cookie.flags, FULL);
28501 +               rdu->full = 1;
28502 +               rdu->tail = arg->ent;
28503 +       }
28504 +
28505 +out:
28506 +       /* AuTraceErr(err); */
28507 +       return err;
28508 +}
28509 +
28510 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28511 +{
28512 +       int err;
28513 +       loff_t offset;
28514 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28515 +
28516 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28517 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28518 +       err = offset;
28519 +       if (unlikely(offset != cookie->h_pos))
28520 +               goto out;
28521 +
28522 +       err = 0;
28523 +       do {
28524 +               arg->err = 0;
28525 +               au_fclr_rdu(cookie->flags, CALLED);
28526 +               /* smp_mb(); */
28527 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28528 +               if (err >= 0)
28529 +                       err = arg->err;
28530 +       } while (!err
28531 +                && au_ftest_rdu(cookie->flags, CALLED)
28532 +                && !au_ftest_rdu(cookie->flags, FULL));
28533 +       cookie->h_pos = h_file->f_pos;
28534 +
28535 +out:
28536 +       AuTraceErr(err);
28537 +       return err;
28538 +}
28539 +
28540 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28541 +{
28542 +       int err;
28543 +       aufs_bindex_t bbot;
28544 +       struct au_rdu_arg arg = {
28545 +               .ctx = {
28546 +                       .actor = au_rdu_fill
28547 +               }
28548 +       };
28549 +       struct dentry *dentry;
28550 +       struct inode *inode;
28551 +       struct file *h_file;
28552 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28553 +
28554 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28555 +       if (unlikely(err)) {
28556 +               err = -EFAULT;
28557 +               AuTraceErr(err);
28558 +               goto out;
28559 +       }
28560 +       rdu->rent = 0;
28561 +       rdu->tail = rdu->ent;
28562 +       rdu->full = 0;
28563 +       arg.rdu = rdu;
28564 +       arg.ent = rdu->ent;
28565 +       arg.end = arg.ent.ul;
28566 +       arg.end += rdu->sz;
28567 +
28568 +       err = -ENOTDIR;
28569 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28570 +               goto out;
28571 +
28572 +       err = security_file_permission(file, MAY_READ);
28573 +       AuTraceErr(err);
28574 +       if (unlikely(err))
28575 +               goto out;
28576 +
28577 +       dentry = file->f_path.dentry;
28578 +       inode = d_inode(dentry);
28579 +       inode_lock_shared(inode);
28580 +
28581 +       arg.sb = inode->i_sb;
28582 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28583 +       if (unlikely(err))
28584 +               goto out_mtx;
28585 +       err = au_alive_dir(dentry);
28586 +       if (unlikely(err))
28587 +               goto out_si;
28588 +       /* todo: reval? */
28589 +       fi_read_lock(file);
28590 +
28591 +       err = -EAGAIN;
28592 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28593 +                    && cookie->generation != au_figen(file)))
28594 +               goto out_unlock;
28595 +
28596 +       err = 0;
28597 +       if (!rdu->blk) {
28598 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28599 +               if (!rdu->blk)
28600 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28601 +       }
28602 +       bbot = au_fbtop(file);
28603 +       if (cookie->bindex < bbot)
28604 +               cookie->bindex = bbot;
28605 +       bbot = au_fbbot_dir(file);
28606 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28607 +       for (; !err && cookie->bindex <= bbot;
28608 +            cookie->bindex++, cookie->h_pos = 0) {
28609 +               h_file = au_hf_dir(file, cookie->bindex);
28610 +               if (!h_file)
28611 +                       continue;
28612 +
28613 +               au_fclr_rdu(cookie->flags, FULL);
28614 +               err = au_rdu_do(h_file, &arg);
28615 +               AuTraceErr(err);
28616 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28617 +                       break;
28618 +       }
28619 +       AuDbg("rent %llu\n", rdu->rent);
28620 +
28621 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28622 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28623 +               au_fset_rdu(cookie->flags, CONT);
28624 +               cookie->generation = au_figen(file);
28625 +       }
28626 +
28627 +       ii_read_lock_child(inode);
28628 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28629 +       ii_read_unlock(inode);
28630 +
28631 +out_unlock:
28632 +       fi_read_unlock(file);
28633 +out_si:
28634 +       si_read_unlock(arg.sb);
28635 +out_mtx:
28636 +       inode_unlock_shared(inode);
28637 +out:
28638 +       AuTraceErr(err);
28639 +       return err;
28640 +}
28641 +
28642 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28643 +{
28644 +       int err;
28645 +       ino_t ino;
28646 +       unsigned long long nent;
28647 +       union au_rdu_ent_ul *u;
28648 +       struct au_rdu_ent ent;
28649 +       struct super_block *sb;
28650 +
28651 +       err = 0;
28652 +       nent = rdu->nent;
28653 +       u = &rdu->ent;
28654 +       sb = file->f_path.dentry->d_sb;
28655 +       si_read_lock(sb, AuLock_FLUSH);
28656 +       while (nent-- > 0) {
28657 +               /* unnecessary to support mmap_sem since this is a dir */
28658 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28659 +               if (!err)
28660 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
28661 +               if (unlikely(err)) {
28662 +                       err = -EFAULT;
28663 +                       AuTraceErr(err);
28664 +                       break;
28665 +               }
28666 +
28667 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28668 +               if (!ent.wh)
28669 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28670 +               else
28671 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28672 +                                       &ino);
28673 +               if (unlikely(err)) {
28674 +                       AuTraceErr(err);
28675 +                       break;
28676 +               }
28677 +
28678 +               err = __put_user(ino, &u->e->ino);
28679 +               if (unlikely(err)) {
28680 +                       err = -EFAULT;
28681 +                       AuTraceErr(err);
28682 +                       break;
28683 +               }
28684 +               u->ul += au_rdu_len(ent.nlen);
28685 +       }
28686 +       si_read_unlock(sb);
28687 +
28688 +       return err;
28689 +}
28690 +
28691 +/* ---------------------------------------------------------------------- */
28692 +
28693 +static int au_rdu_verify(struct aufs_rdu *rdu)
28694 +{
28695 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28696 +             "%llu, b%d, 0x%x, g%u}\n",
28697 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28698 +             rdu->blk,
28699 +             rdu->rent, rdu->shwh, rdu->full,
28700 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28701 +             rdu->cookie.generation);
28702 +
28703 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28704 +               return 0;
28705 +
28706 +       AuDbg("%u:%u\n",
28707 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28708 +       return -EINVAL;
28709 +}
28710 +
28711 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28712 +{
28713 +       long err, e;
28714 +       struct aufs_rdu rdu;
28715 +       void __user *p = (void __user *)arg;
28716 +
28717 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28718 +       if (unlikely(err)) {
28719 +               err = -EFAULT;
28720 +               AuTraceErr(err);
28721 +               goto out;
28722 +       }
28723 +       err = au_rdu_verify(&rdu);
28724 +       if (unlikely(err))
28725 +               goto out;
28726 +
28727 +       switch (cmd) {
28728 +       case AUFS_CTL_RDU:
28729 +               err = au_rdu(file, &rdu);
28730 +               if (unlikely(err))
28731 +                       break;
28732 +
28733 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28734 +               if (unlikely(e)) {
28735 +                       err = -EFAULT;
28736 +                       AuTraceErr(err);
28737 +               }
28738 +               break;
28739 +       case AUFS_CTL_RDU_INO:
28740 +               err = au_rdu_ino(file, &rdu);
28741 +               break;
28742 +
28743 +       default:
28744 +               /* err = -ENOTTY; */
28745 +               err = -EINVAL;
28746 +       }
28747 +
28748 +out:
28749 +       AuTraceErr(err);
28750 +       return err;
28751 +}
28752 +
28753 +#ifdef CONFIG_COMPAT
28754 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28755 +{
28756 +       long err, e;
28757 +       struct aufs_rdu rdu;
28758 +       void __user *p = compat_ptr(arg);
28759 +
28760 +       /* todo: get_user()? */
28761 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28762 +       if (unlikely(err)) {
28763 +               err = -EFAULT;
28764 +               AuTraceErr(err);
28765 +               goto out;
28766 +       }
28767 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28768 +       err = au_rdu_verify(&rdu);
28769 +       if (unlikely(err))
28770 +               goto out;
28771 +
28772 +       switch (cmd) {
28773 +       case AUFS_CTL_RDU:
28774 +               err = au_rdu(file, &rdu);
28775 +               if (unlikely(err))
28776 +                       break;
28777 +
28778 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28779 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28780 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28781 +               if (unlikely(e)) {
28782 +                       err = -EFAULT;
28783 +                       AuTraceErr(err);
28784 +               }
28785 +               break;
28786 +       case AUFS_CTL_RDU_INO:
28787 +               err = au_rdu_ino(file, &rdu);
28788 +               break;
28789 +
28790 +       default:
28791 +               /* err = -ENOTTY; */
28792 +               err = -EINVAL;
28793 +       }
28794 +
28795 +out:
28796 +       AuTraceErr(err);
28797 +       return err;
28798 +}
28799 +#endif
28800 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28801 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28802 +++ linux/fs/aufs/rwsem.h       2018-07-19 09:46:13.063314675 +0200
28803 @@ -0,0 +1,73 @@
28804 +/* SPDX-License-Identifier: GPL-2.0 */
28805 +/*
28806 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28807 + *
28808 + * This program, aufs is free software; you can redistribute it and/or modify
28809 + * it under the terms of the GNU General Public License as published by
28810 + * the Free Software Foundation; either version 2 of the License, or
28811 + * (at your option) any later version.
28812 + *
28813 + * This program is distributed in the hope that it will be useful,
28814 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28815 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28816 + * GNU General Public License for more details.
28817 + *
28818 + * You should have received a copy of the GNU General Public License
28819 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28820 + */
28821 +
28822 +/*
28823 + * simple read-write semaphore wrappers
28824 + */
28825 +
28826 +#ifndef __AUFS_RWSEM_H__
28827 +#define __AUFS_RWSEM_H__
28828 +
28829 +#ifdef __KERNEL__
28830 +
28831 +#include "debug.h"
28832 +
28833 +/* in the futre, the name 'au_rwsem' will be totally gone */
28834 +#define au_rwsem       rw_semaphore
28835 +
28836 +/* to debug easier, do not make them inlined functions */
28837 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28838 +/* rwsem_is_locked() is unusable */
28839 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
28840 +                                         && debug_locks \
28841 +                                         && !lockdep_is_held_type(rw, 1))
28842 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
28843 +                                         && debug_locks \
28844 +                                         && !lockdep_is_held_type(rw, 0))
28845 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
28846 +                                         && debug_locks \
28847 +                                         && !lockdep_is_held(rw))
28848 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
28849 +                                         && debug_locks \
28850 +                                         && lockdep_is_held(rw))
28851 +
28852 +#define au_rw_init(rw) init_rwsem(rw)
28853 +
28854 +#define au_rw_init_wlock(rw) do {              \
28855 +               au_rw_init(rw);                 \
28856 +               down_write(rw);                 \
28857 +       } while (0)
28858 +
28859 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
28860 +               au_rw_init(rw);                 \
28861 +               down_write_nested(rw, lsc);     \
28862 +       } while (0)
28863 +
28864 +#define au_rw_read_lock(rw)            down_read(rw)
28865 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
28866 +#define au_rw_read_unlock(rw)          up_read(rw)
28867 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
28868 +#define au_rw_write_lock(rw)           down_write(rw)
28869 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28870 +#define au_rw_write_unlock(rw)         up_write(rw)
28871 +/* why is not _nested version defined? */
28872 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
28873 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
28874 +
28875 +#endif /* __KERNEL__ */
28876 +#endif /* __AUFS_RWSEM_H__ */
28877 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28878 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
28879 +++ linux/fs/aufs/sbinfo.c      2018-07-19 09:46:13.063314675 +0200
28880 @@ -0,0 +1,312 @@
28881 +/* SPDX-License-Identifier: GPL-2.0 */
28882 +/*
28883 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28884 + *
28885 + * This program, aufs is free software; you can redistribute it and/or modify
28886 + * it under the terms of the GNU General Public License as published by
28887 + * the Free Software Foundation; either version 2 of the License, or
28888 + * (at your option) any later version.
28889 + *
28890 + * This program is distributed in the hope that it will be useful,
28891 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28892 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28893 + * GNU General Public License for more details.
28894 + *
28895 + * You should have received a copy of the GNU General Public License
28896 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28897 + */
28898 +
28899 +/*
28900 + * superblock private data
28901 + */
28902 +
28903 +#include "aufs.h"
28904 +
28905 +/*
28906 + * they are necessary regardless sysfs is disabled.
28907 + */
28908 +void au_si_free(struct kobject *kobj)
28909 +{
28910 +       int i;
28911 +       struct au_sbinfo *sbinfo;
28912 +       char *locked __maybe_unused; /* debug only */
28913 +
28914 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28915 +       for (i = 0; i < AuPlink_NHASH; i++)
28916 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
28917 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
28918 +
28919 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
28920 +       percpu_counter_destroy(&sbinfo->si_ninodes);
28921 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
28922 +       percpu_counter_destroy(&sbinfo->si_nfiles);
28923 +
28924 +       dbgaufs_si_fin(sbinfo);
28925 +       au_rw_write_lock(&sbinfo->si_rwsem);
28926 +       au_br_free(sbinfo);
28927 +       au_rw_write_unlock(&sbinfo->si_rwsem);
28928 +
28929 +       kfree(sbinfo->si_branch);
28930 +       mutex_destroy(&sbinfo->si_xib_mtx);
28931 +       AuRwDestroy(&sbinfo->si_rwsem);
28932 +
28933 +       kfree(sbinfo);
28934 +}
28935 +
28936 +int au_si_alloc(struct super_block *sb)
28937 +{
28938 +       int err, i;
28939 +       struct au_sbinfo *sbinfo;
28940 +
28941 +       err = -ENOMEM;
28942 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
28943 +       if (unlikely(!sbinfo))
28944 +               goto out;
28945 +
28946 +       /* will be reallocated separately */
28947 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
28948 +       if (unlikely(!sbinfo->si_branch))
28949 +               goto out_sbinfo;
28950 +
28951 +       err = sysaufs_si_init(sbinfo);
28952 +       if (!err) {
28953 +               dbgaufs_si_null(sbinfo);
28954 +               err = dbgaufs_si_init(sbinfo);
28955 +               if (unlikely(err))
28956 +                       kobject_put(&sbinfo->si_kobj);
28957 +       }
28958 +       if (unlikely(err))
28959 +               goto out_br;
28960 +
28961 +       au_nwt_init(&sbinfo->si_nowait);
28962 +       au_rw_init_wlock(&sbinfo->si_rwsem);
28963 +
28964 +       percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
28965 +       percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
28966 +
28967 +       sbinfo->si_bbot = -1;
28968 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
28969 +
28970 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
28971 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
28972 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
28973 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
28974 +
28975 +       au_fhsm_init(sbinfo);
28976 +
28977 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
28978 +
28979 +       sbinfo->si_xino_jiffy = jiffies;
28980 +       sbinfo->si_xino_expire
28981 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
28982 +       mutex_init(&sbinfo->si_xib_mtx);
28983 +       sbinfo->si_xino_brid = -1;
28984 +       /* leave si_xib_last_pindex and si_xib_next_bit */
28985 +
28986 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
28987 +
28988 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
28989 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
28990 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
28991 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
28992 +
28993 +       for (i = 0; i < AuPlink_NHASH; i++)
28994 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
28995 +       init_waitqueue_head(&sbinfo->si_plink_wq);
28996 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
28997 +
28998 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
28999 +
29000 +       /* with getattr by default */
29001 +       sbinfo->si_iop_array = aufs_iop;
29002 +
29003 +       /* leave other members for sysaufs and si_mnt. */
29004 +       sbinfo->si_sb = sb;
29005 +       sb->s_fs_info = sbinfo;
29006 +       si_pid_set(sb);
29007 +       return 0; /* success */
29008 +
29009 +out_br:
29010 +       kfree(sbinfo->si_branch);
29011 +out_sbinfo:
29012 +       kfree(sbinfo);
29013 +out:
29014 +       return err;
29015 +}
29016 +
29017 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29018 +{
29019 +       int err, sz;
29020 +       struct au_branch **brp;
29021 +
29022 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29023 +
29024 +       err = -ENOMEM;
29025 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29026 +       if (unlikely(!sz))
29027 +               sz = sizeof(*brp);
29028 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29029 +                          may_shrink);
29030 +       if (brp) {
29031 +               sbinfo->si_branch = brp;
29032 +               err = 0;
29033 +       }
29034 +
29035 +       return err;
29036 +}
29037 +
29038 +/* ---------------------------------------------------------------------- */
29039 +
29040 +unsigned int au_sigen_inc(struct super_block *sb)
29041 +{
29042 +       unsigned int gen;
29043 +       struct inode *inode;
29044 +
29045 +       SiMustWriteLock(sb);
29046 +
29047 +       gen = ++au_sbi(sb)->si_generation;
29048 +       au_update_digen(sb->s_root);
29049 +       inode = d_inode(sb->s_root);
29050 +       au_update_iigen(inode, /*half*/0);
29051 +       inode_inc_iversion(inode);
29052 +       return gen;
29053 +}
29054 +
29055 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29056 +{
29057 +       aufs_bindex_t br_id;
29058 +       int i;
29059 +       struct au_sbinfo *sbinfo;
29060 +
29061 +       SiMustWriteLock(sb);
29062 +
29063 +       sbinfo = au_sbi(sb);
29064 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29065 +               br_id = ++sbinfo->si_last_br_id;
29066 +               AuDebugOn(br_id < 0);
29067 +               if (br_id && au_br_index(sb, br_id) < 0)
29068 +                       return br_id;
29069 +       }
29070 +
29071 +       return -1;
29072 +}
29073 +
29074 +/* ---------------------------------------------------------------------- */
29075 +
29076 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29077 +int si_read_lock(struct super_block *sb, int flags)
29078 +{
29079 +       int err;
29080 +
29081 +       err = 0;
29082 +       if (au_ftest_lock(flags, FLUSH))
29083 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29084 +
29085 +       si_noflush_read_lock(sb);
29086 +       err = au_plink_maint(sb, flags);
29087 +       if (unlikely(err))
29088 +               si_read_unlock(sb);
29089 +
29090 +       return err;
29091 +}
29092 +
29093 +int si_write_lock(struct super_block *sb, int flags)
29094 +{
29095 +       int err;
29096 +
29097 +       if (au_ftest_lock(flags, FLUSH))
29098 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29099 +
29100 +       si_noflush_write_lock(sb);
29101 +       err = au_plink_maint(sb, flags);
29102 +       if (unlikely(err))
29103 +               si_write_unlock(sb);
29104 +
29105 +       return err;
29106 +}
29107 +
29108 +/* dentry and super_block lock. call at entry point */
29109 +int aufs_read_lock(struct dentry *dentry, int flags)
29110 +{
29111 +       int err;
29112 +       struct super_block *sb;
29113 +
29114 +       sb = dentry->d_sb;
29115 +       err = si_read_lock(sb, flags);
29116 +       if (unlikely(err))
29117 +               goto out;
29118 +
29119 +       if (au_ftest_lock(flags, DW))
29120 +               di_write_lock_child(dentry);
29121 +       else
29122 +               di_read_lock_child(dentry, flags);
29123 +
29124 +       if (au_ftest_lock(flags, GEN)) {
29125 +               err = au_digen_test(dentry, au_sigen(sb));
29126 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29127 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29128 +               else if (!err)
29129 +                       err = au_dbrange_test(dentry);
29130 +               if (unlikely(err))
29131 +                       aufs_read_unlock(dentry, flags);
29132 +       }
29133 +
29134 +out:
29135 +       return err;
29136 +}
29137 +
29138 +void aufs_read_unlock(struct dentry *dentry, int flags)
29139 +{
29140 +       if (au_ftest_lock(flags, DW))
29141 +               di_write_unlock(dentry);
29142 +       else
29143 +               di_read_unlock(dentry, flags);
29144 +       si_read_unlock(dentry->d_sb);
29145 +}
29146 +
29147 +void aufs_write_lock(struct dentry *dentry)
29148 +{
29149 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29150 +       di_write_lock_child(dentry);
29151 +}
29152 +
29153 +void aufs_write_unlock(struct dentry *dentry)
29154 +{
29155 +       di_write_unlock(dentry);
29156 +       si_write_unlock(dentry->d_sb);
29157 +}
29158 +
29159 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29160 +{
29161 +       int err;
29162 +       unsigned int sigen;
29163 +       struct super_block *sb;
29164 +
29165 +       sb = d1->d_sb;
29166 +       err = si_read_lock(sb, flags);
29167 +       if (unlikely(err))
29168 +               goto out;
29169 +
29170 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29171 +
29172 +       if (au_ftest_lock(flags, GEN)) {
29173 +               sigen = au_sigen(sb);
29174 +               err = au_digen_test(d1, sigen);
29175 +               AuDebugOn(!err && au_dbrange_test(d1));
29176 +               if (!err) {
29177 +                       err = au_digen_test(d2, sigen);
29178 +                       AuDebugOn(!err && au_dbrange_test(d2));
29179 +               }
29180 +               if (unlikely(err))
29181 +                       aufs_read_and_write_unlock2(d1, d2);
29182 +       }
29183 +
29184 +out:
29185 +       return err;
29186 +}
29187 +
29188 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29189 +{
29190 +       di_write_unlock2(d1, d2);
29191 +       si_read_unlock(d1->d_sb);
29192 +}
29193 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29194 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29195 +++ linux/fs/aufs/super.c       2018-07-19 09:46:13.063314675 +0200
29196 @@ -0,0 +1,1043 @@
29197 +/* SPDX-License-Identifier: GPL-2.0 */
29198 +/*
29199 + * Copyright (C) 2005-2018 Junjiro R. Okajima
29200 + *
29201 + * This program, aufs is free software; you can redistribute it and/or modify
29202 + * it under the terms of the GNU General Public License as published by
29203 + * the Free Software Foundation; either version 2 of the License, or
29204 + * (at your option) any later version.
29205 + *
29206 + * This program is distributed in the hope that it will be useful,
29207 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29208 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29209 + * GNU General Public License for more details.
29210 + *
29211 + * You should have received a copy of the GNU General Public License
29212 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29213 + */
29214 +
29215 +/*
29216 + * mount and super_block operations
29217 + */
29218 +
29219 +#include <linux/mm.h>
29220 +#include <linux/seq_file.h>
29221 +#include <linux/statfs.h>
29222 +#include <linux/vmalloc.h>
29223 +#include "aufs.h"
29224 +
29225 +/*
29226 + * super_operations
29227 + */
29228 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29229 +{
29230 +       struct au_icntnr *c;
29231 +
29232 +       c = au_cache_alloc_icntnr();
29233 +       if (c) {
29234 +               au_icntnr_init(c);
29235 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29236 +               c->iinfo.ii_hinode = NULL;
29237 +               return &c->vfs_inode;
29238 +       }
29239 +       return NULL;
29240 +}
29241 +
29242 +static void aufs_destroy_inode_cb(struct rcu_head *head)
29243 +{
29244 +       struct inode *inode = container_of(head, struct inode, i_rcu);
29245 +
29246 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29247 +}
29248 +
29249 +static void aufs_destroy_inode(struct inode *inode)
29250 +{
29251 +       if (!au_is_bad_inode(inode))
29252 +               au_iinfo_fin(inode);
29253 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
29254 +}
29255 +
29256 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29257 +{
29258 +       struct inode *inode;
29259 +       int err;
29260 +
29261 +       inode = iget_locked(sb, ino);
29262 +       if (unlikely(!inode)) {
29263 +               inode = ERR_PTR(-ENOMEM);
29264 +               goto out;
29265 +       }
29266 +       if (!(inode->i_state & I_NEW))
29267 +               goto out;
29268 +
29269 +       err = au_xigen_new(inode);
29270 +       if (!err)
29271 +               err = au_iinfo_init(inode);
29272 +       if (!err)
29273 +               inode_inc_iversion(inode);
29274 +       else {
29275 +               iget_failed(inode);
29276 +               inode = ERR_PTR(err);
29277 +       }
29278 +
29279 +out:
29280 +       /* never return NULL */
29281 +       AuDebugOn(!inode);
29282 +       AuTraceErrPtr(inode);
29283 +       return inode;
29284 +}
29285 +
29286 +/* lock free root dinfo */
29287 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29288 +{
29289 +       int err;
29290 +       aufs_bindex_t bindex, bbot;
29291 +       struct path path;
29292 +       struct au_hdentry *hdp;
29293 +       struct au_branch *br;
29294 +       au_br_perm_str_t perm;
29295 +
29296 +       err = 0;
29297 +       bbot = au_sbbot(sb);
29298 +       bindex = 0;
29299 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29300 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29301 +               br = au_sbr(sb, bindex);
29302 +               path.mnt = au_br_mnt(br);
29303 +               path.dentry = hdp->hd_dentry;
29304 +               err = au_seq_path(seq, &path);
29305 +               if (!err) {
29306 +                       au_optstr_br_perm(&perm, br->br_perm);
29307 +                       seq_printf(seq, "=%s", perm.a);
29308 +                       if (bindex != bbot)
29309 +                               seq_putc(seq, ':');
29310 +               }
29311 +       }
29312 +       if (unlikely(err || seq_has_overflowed(seq)))
29313 +               err = -E2BIG;
29314 +
29315 +       return err;
29316 +}
29317 +
29318 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29319 +                      const char *append)
29320 +{
29321 +       char *p;
29322 +
29323 +       p = fmt;
29324 +       while (*pat != ':')
29325 +               *p++ = *pat++;
29326 +       *p++ = *pat++;
29327 +       strcpy(p, append);
29328 +       AuDebugOn(strlen(fmt) >= len);
29329 +}
29330 +
29331 +static void au_show_wbr_create(struct seq_file *m, int v,
29332 +                              struct au_sbinfo *sbinfo)
29333 +{
29334 +       const char *pat;
29335 +       char fmt[32];
29336 +       struct au_wbr_mfs *mfs;
29337 +
29338 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29339 +
29340 +       seq_puts(m, ",create=");
29341 +       pat = au_optstr_wbr_create(v);
29342 +       mfs = &sbinfo->si_wbr_mfs;
29343 +       switch (v) {
29344 +       case AuWbrCreate_TDP:
29345 +       case AuWbrCreate_RR:
29346 +       case AuWbrCreate_MFS:
29347 +       case AuWbrCreate_PMFS:
29348 +               seq_puts(m, pat);
29349 +               break;
29350 +       case AuWbrCreate_MFSRR:
29351 +       case AuWbrCreate_TDMFS:
29352 +       case AuWbrCreate_PMFSRR:
29353 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29354 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29355 +               break;
29356 +       case AuWbrCreate_MFSV:
29357 +       case AuWbrCreate_PMFSV:
29358 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29359 +               seq_printf(m, fmt,
29360 +                          jiffies_to_msecs(mfs->mfs_expire)
29361 +                          / MSEC_PER_SEC);
29362 +               break;
29363 +       case AuWbrCreate_MFSRRV:
29364 +       case AuWbrCreate_TDMFSV:
29365 +       case AuWbrCreate_PMFSRRV:
29366 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29367 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29368 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29369 +               break;
29370 +       default:
29371 +               BUG();
29372 +       }
29373 +}
29374 +
29375 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29376 +{
29377 +#ifdef CONFIG_SYSFS
29378 +       return 0;
29379 +#else
29380 +       int err;
29381 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29382 +       aufs_bindex_t bindex, brid;
29383 +       struct qstr *name;
29384 +       struct file *f;
29385 +       struct dentry *d, *h_root;
29386 +
29387 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29388 +
29389 +       err = 0;
29390 +       f = au_sbi(sb)->si_xib;
29391 +       if (!f)
29392 +               goto out;
29393 +
29394 +       /* stop printing the default xino path on the first writable branch */
29395 +       h_root = NULL;
29396 +       brid = au_xino_brid(sb);
29397 +       if (brid >= 0) {
29398 +               bindex = au_br_index(sb, brid);
29399 +               h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
29400 +       }
29401 +       d = f->f_path.dentry;
29402 +       name = &d->d_name;
29403 +       /* safe ->d_parent because the file is unlinked */
29404 +       if (d->d_parent == h_root
29405 +           && name->len == len
29406 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29407 +               goto out;
29408 +
29409 +       seq_puts(seq, ",xino=");
29410 +       err = au_xino_path(seq, f);
29411 +
29412 +out:
29413 +       return err;
29414 +#endif
29415 +}
29416 +
29417 +/* seq_file will re-call me in case of too long string */
29418 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29419 +{
29420 +       int err;
29421 +       unsigned int mnt_flags, v;
29422 +       struct super_block *sb;
29423 +       struct au_sbinfo *sbinfo;
29424 +
29425 +#define AuBool(name, str) do { \
29426 +       v = au_opt_test(mnt_flags, name); \
29427 +       if (v != au_opt_test(AuOpt_Def, name)) \
29428 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29429 +} while (0)
29430 +
29431 +#define AuStr(name, str) do { \
29432 +       v = mnt_flags & AuOptMask_##name; \
29433 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29434 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29435 +} while (0)
29436 +
29437 +#define AuUInt(name, str, val) do { \
29438 +       if (val != AUFS_##name##_DEF) \
29439 +               seq_printf(m, "," #str "=%u", val); \
29440 +} while (0)
29441 +
29442 +       sb = dentry->d_sb;
29443 +       if (sb->s_flags & SB_POSIXACL)
29444 +               seq_puts(m, ",acl");
29445 +#if 0
29446 +       if (sb->s_flags & SB_I_VERSION)
29447 +               seq_puts(m, ",i_version");
29448 +#endif
29449 +
29450 +       /* lock free root dinfo */
29451 +       si_noflush_read_lock(sb);
29452 +       sbinfo = au_sbi(sb);
29453 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29454 +
29455 +       mnt_flags = au_mntflags(sb);
29456 +       if (au_opt_test(mnt_flags, XINO)) {
29457 +               err = au_show_xino(m, sb);
29458 +               if (unlikely(err))
29459 +                       goto out;
29460 +       } else
29461 +               seq_puts(m, ",noxino");
29462 +
29463 +       AuBool(TRUNC_XINO, trunc_xino);
29464 +       AuStr(UDBA, udba);
29465 +       AuBool(SHWH, shwh);
29466 +       AuBool(PLINK, plink);
29467 +       AuBool(DIO, dio);
29468 +       AuBool(DIRPERM1, dirperm1);
29469 +
29470 +       v = sbinfo->si_wbr_create;
29471 +       if (v != AuWbrCreate_Def)
29472 +               au_show_wbr_create(m, v, sbinfo);
29473 +
29474 +       v = sbinfo->si_wbr_copyup;
29475 +       if (v != AuWbrCopyup_Def)
29476 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29477 +
29478 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29479 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29480 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29481 +
29482 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29483 +
29484 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29485 +       AuUInt(RDCACHE, rdcache, v);
29486 +
29487 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29488 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29489 +
29490 +       au_fhsm_show(m, sbinfo);
29491 +
29492 +       AuBool(DIRREN, dirren);
29493 +       AuBool(SUM, sum);
29494 +       /* AuBool(SUM_W, wsum); */
29495 +       AuBool(WARN_PERM, warn_perm);
29496 +       AuBool(VERBOSE, verbose);
29497 +
29498 +out:
29499 +       /* be sure to print "br:" last */
29500 +       if (!sysaufs_brs) {
29501 +               seq_puts(m, ",br:");
29502 +               au_show_brs(m, sb);
29503 +       }
29504 +       si_read_unlock(sb);
29505 +       return 0;
29506 +
29507 +#undef AuBool
29508 +#undef AuStr
29509 +#undef AuUInt
29510 +}
29511 +
29512 +/* ---------------------------------------------------------------------- */
29513 +
29514 +/* sum mode which returns the summation for statfs(2) */
29515 +
29516 +static u64 au_add_till_max(u64 a, u64 b)
29517 +{
29518 +       u64 old;
29519 +
29520 +       old = a;
29521 +       a += b;
29522 +       if (old <= a)
29523 +               return a;
29524 +       return ULLONG_MAX;
29525 +}
29526 +
29527 +static u64 au_mul_till_max(u64 a, long mul)
29528 +{
29529 +       u64 old;
29530 +
29531 +       old = a;
29532 +       a *= mul;
29533 +       if (old <= a)
29534 +               return a;
29535 +       return ULLONG_MAX;
29536 +}
29537 +
29538 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29539 +{
29540 +       int err;
29541 +       long bsize, factor;
29542 +       u64 blocks, bfree, bavail, files, ffree;
29543 +       aufs_bindex_t bbot, bindex, i;
29544 +       unsigned char shared;
29545 +       struct path h_path;
29546 +       struct super_block *h_sb;
29547 +
29548 +       err = 0;
29549 +       bsize = LONG_MAX;
29550 +       files = 0;
29551 +       ffree = 0;
29552 +       blocks = 0;
29553 +       bfree = 0;
29554 +       bavail = 0;
29555 +       bbot = au_sbbot(sb);
29556 +       for (bindex = 0; bindex <= bbot; bindex++) {
29557 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29558 +               h_sb = h_path.mnt->mnt_sb;
29559 +               shared = 0;
29560 +               for (i = 0; !shared && i < bindex; i++)
29561 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29562 +               if (shared)
29563 +                       continue;
29564 +
29565 +               /* sb->s_root for NFS is unreliable */
29566 +               h_path.dentry = h_path.mnt->mnt_root;
29567 +               err = vfs_statfs(&h_path, buf);
29568 +               if (unlikely(err))
29569 +                       goto out;
29570 +
29571 +               if (bsize > buf->f_bsize) {
29572 +                       /*
29573 +                        * we will reduce bsize, so we have to expand blocks
29574 +                        * etc. to match them again
29575 +                        */
29576 +                       factor = (bsize / buf->f_bsize);
29577 +                       blocks = au_mul_till_max(blocks, factor);
29578 +                       bfree = au_mul_till_max(bfree, factor);
29579 +                       bavail = au_mul_till_max(bavail, factor);
29580 +                       bsize = buf->f_bsize;
29581 +               }
29582 +
29583 +               factor = (buf->f_bsize / bsize);
29584 +               blocks = au_add_till_max(blocks,
29585 +                               au_mul_till_max(buf->f_blocks, factor));
29586 +               bfree = au_add_till_max(bfree,
29587 +                               au_mul_till_max(buf->f_bfree, factor));
29588 +               bavail = au_add_till_max(bavail,
29589 +                               au_mul_till_max(buf->f_bavail, factor));
29590 +               files = au_add_till_max(files, buf->f_files);
29591 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29592 +       }
29593 +
29594 +       buf->f_bsize = bsize;
29595 +       buf->f_blocks = blocks;
29596 +       buf->f_bfree = bfree;
29597 +       buf->f_bavail = bavail;
29598 +       buf->f_files = files;
29599 +       buf->f_ffree = ffree;
29600 +       buf->f_frsize = 0;
29601 +
29602 +out:
29603 +       return err;
29604 +}
29605 +
29606 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29607 +{
29608 +       int err;
29609 +       struct path h_path;
29610 +       struct super_block *sb;
29611 +
29612 +       /* lock free root dinfo */
29613 +       sb = dentry->d_sb;
29614 +       si_noflush_read_lock(sb);
29615 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29616 +               /* sb->s_root for NFS is unreliable */
29617 +               h_path.mnt = au_sbr_mnt(sb, 0);
29618 +               h_path.dentry = h_path.mnt->mnt_root;
29619 +               err = vfs_statfs(&h_path, buf);
29620 +       } else
29621 +               err = au_statfs_sum(sb, buf);
29622 +       si_read_unlock(sb);
29623 +
29624 +       if (!err) {
29625 +               buf->f_type = AUFS_SUPER_MAGIC;
29626 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29627 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29628 +       }
29629 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29630 +
29631 +       return err;
29632 +}
29633 +
29634 +/* ---------------------------------------------------------------------- */
29635 +
29636 +static int aufs_sync_fs(struct super_block *sb, int wait)
29637 +{
29638 +       int err, e;
29639 +       aufs_bindex_t bbot, bindex;
29640 +       struct au_branch *br;
29641 +       struct super_block *h_sb;
29642 +
29643 +       err = 0;
29644 +       si_noflush_read_lock(sb);
29645 +       bbot = au_sbbot(sb);
29646 +       for (bindex = 0; bindex <= bbot; bindex++) {
29647 +               br = au_sbr(sb, bindex);
29648 +               if (!au_br_writable(br->br_perm))
29649 +                       continue;
29650 +
29651 +               h_sb = au_sbr_sb(sb, bindex);
29652 +               e = vfsub_sync_filesystem(h_sb, wait);
29653 +               if (unlikely(e && !err))
29654 +                       err = e;
29655 +               /* go on even if an error happens */
29656 +       }
29657 +       si_read_unlock(sb);
29658 +
29659 +       return err;
29660 +}
29661 +
29662 +/* ---------------------------------------------------------------------- */
29663 +
29664 +/* final actions when unmounting a file system */
29665 +static void aufs_put_super(struct super_block *sb)
29666 +{
29667 +       struct au_sbinfo *sbinfo;
29668 +
29669 +       sbinfo = au_sbi(sb);
29670 +       if (sbinfo)
29671 +               kobject_put(&sbinfo->si_kobj);
29672 +}
29673 +
29674 +/* ---------------------------------------------------------------------- */
29675 +
29676 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29677 +                    struct super_block *sb, void *arg)
29678 +{
29679 +       void *array;
29680 +       unsigned long long n, sz;
29681 +
29682 +       array = NULL;
29683 +       n = 0;
29684 +       if (!*hint)
29685 +               goto out;
29686 +
29687 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29688 +               array = ERR_PTR(-EMFILE);
29689 +               pr_err("hint %llu\n", *hint);
29690 +               goto out;
29691 +       }
29692 +
29693 +       sz = sizeof(array) * *hint;
29694 +       array = kzalloc(sz, GFP_NOFS);
29695 +       if (unlikely(!array))
29696 +               array = vzalloc(sz);
29697 +       if (unlikely(!array)) {
29698 +               array = ERR_PTR(-ENOMEM);
29699 +               goto out;
29700 +       }
29701 +
29702 +       n = cb(sb, array, *hint, arg);
29703 +       AuDebugOn(n > *hint);
29704 +
29705 +out:
29706 +       *hint = n;
29707 +       return array;
29708 +}
29709 +
29710 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29711 +                                      unsigned long long max __maybe_unused,
29712 +                                      void *arg)
29713 +{
29714 +       unsigned long long n;
29715 +       struct inode **p, *inode;
29716 +       struct list_head *head;
29717 +
29718 +       n = 0;
29719 +       p = a;
29720 +       head = arg;
29721 +       spin_lock(&sb->s_inode_list_lock);
29722 +       list_for_each_entry(inode, head, i_sb_list) {
29723 +               if (!au_is_bad_inode(inode)
29724 +                   && au_ii(inode)->ii_btop >= 0) {
29725 +                       spin_lock(&inode->i_lock);
29726 +                       if (atomic_read(&inode->i_count)) {
29727 +                               au_igrab(inode);
29728 +                               *p++ = inode;
29729 +                               n++;
29730 +                               AuDebugOn(n > max);
29731 +                       }
29732 +                       spin_unlock(&inode->i_lock);
29733 +               }
29734 +       }
29735 +       spin_unlock(&sb->s_inode_list_lock);
29736 +
29737 +       return n;
29738 +}
29739 +
29740 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29741 +{
29742 +       *max = au_ninodes(sb);
29743 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29744 +}
29745 +
29746 +void au_iarray_free(struct inode **a, unsigned long long max)
29747 +{
29748 +       unsigned long long ull;
29749 +
29750 +       for (ull = 0; ull < max; ull++)
29751 +               iput(a[ull]);
29752 +       kvfree(a);
29753 +}
29754 +
29755 +/* ---------------------------------------------------------------------- */
29756 +
29757 +/*
29758 + * refresh dentry and inode at remount time.
29759 + */
29760 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29761 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29762 +                     struct dentry *parent)
29763 +{
29764 +       int err;
29765 +
29766 +       di_write_lock_child(dentry);
29767 +       di_read_lock_parent(parent, AuLock_IR);
29768 +       err = au_refresh_dentry(dentry, parent);
29769 +       if (!err && dir_flags)
29770 +               au_hn_reset(d_inode(dentry), dir_flags);
29771 +       di_read_unlock(parent, AuLock_IR);
29772 +       di_write_unlock(dentry);
29773 +
29774 +       return err;
29775 +}
29776 +
29777 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29778 +                          struct au_sbinfo *sbinfo,
29779 +                          const unsigned int dir_flags, unsigned int do_idop)
29780 +{
29781 +       int err;
29782 +       struct dentry *parent;
29783 +
29784 +       err = 0;
29785 +       parent = dget_parent(dentry);
29786 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29787 +               if (d_really_is_positive(dentry)) {
29788 +                       if (!d_is_dir(dentry))
29789 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29790 +                                                parent);
29791 +                       else {
29792 +                               err = au_do_refresh(dentry, dir_flags, parent);
29793 +                               if (unlikely(err))
29794 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29795 +                       }
29796 +               } else
29797 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29798 +               AuDbgDentry(dentry);
29799 +       }
29800 +       dput(parent);
29801 +
29802 +       if (!err) {
29803 +               if (do_idop)
29804 +                       au_refresh_dop(dentry, /*force_reval*/0);
29805 +       } else
29806 +               au_refresh_dop(dentry, /*force_reval*/1);
29807 +
29808 +       AuTraceErr(err);
29809 +       return err;
29810 +}
29811 +
29812 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29813 +{
29814 +       int err, i, j, ndentry, e;
29815 +       unsigned int sigen;
29816 +       struct au_dcsub_pages dpages;
29817 +       struct au_dpage *dpage;
29818 +       struct dentry **dentries, *d;
29819 +       struct au_sbinfo *sbinfo;
29820 +       struct dentry *root = sb->s_root;
29821 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29822 +
29823 +       if (do_idop)
29824 +               au_refresh_dop(root, /*force_reval*/0);
29825 +
29826 +       err = au_dpages_init(&dpages, GFP_NOFS);
29827 +       if (unlikely(err))
29828 +               goto out;
29829 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
29830 +       if (unlikely(err))
29831 +               goto out_dpages;
29832 +
29833 +       sigen = au_sigen(sb);
29834 +       sbinfo = au_sbi(sb);
29835 +       for (i = 0; i < dpages.ndpage; i++) {
29836 +               dpage = dpages.dpages + i;
29837 +               dentries = dpage->dentries;
29838 +               ndentry = dpage->ndentry;
29839 +               for (j = 0; j < ndentry; j++) {
29840 +                       d = dentries[j];
29841 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29842 +                                           do_idop);
29843 +                       if (unlikely(e && !err))
29844 +                               err = e;
29845 +                       /* go on even err */
29846 +               }
29847 +       }
29848 +
29849 +out_dpages:
29850 +       au_dpages_free(&dpages);
29851 +out:
29852 +       return err;
29853 +}
29854 +
29855 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
29856 +{
29857 +       int err, e;
29858 +       unsigned int sigen;
29859 +       unsigned long long max, ull;
29860 +       struct inode *inode, **array;
29861 +
29862 +       array = au_iarray_alloc(sb, &max);
29863 +       err = PTR_ERR(array);
29864 +       if (IS_ERR(array))
29865 +               goto out;
29866 +
29867 +       err = 0;
29868 +       sigen = au_sigen(sb);
29869 +       for (ull = 0; ull < max; ull++) {
29870 +               inode = array[ull];
29871 +               if (unlikely(!inode))
29872 +                       break;
29873 +
29874 +               e = 0;
29875 +               ii_write_lock_child(inode);
29876 +               if (au_iigen(inode, NULL) != sigen) {
29877 +                       e = au_refresh_hinode_self(inode);
29878 +                       if (unlikely(e)) {
29879 +                               au_refresh_iop(inode, /*force_getattr*/1);
29880 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
29881 +                               if (!err)
29882 +                                       err = e;
29883 +                               /* go on even if err */
29884 +                       }
29885 +               }
29886 +               if (!e && do_idop)
29887 +                       au_refresh_iop(inode, /*force_getattr*/0);
29888 +               ii_write_unlock(inode);
29889 +       }
29890 +
29891 +       au_iarray_free(array, max);
29892 +
29893 +out:
29894 +       return err;
29895 +}
29896 +
29897 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
29898 +{
29899 +       int err, e;
29900 +       unsigned int udba;
29901 +       aufs_bindex_t bindex, bbot;
29902 +       struct dentry *root;
29903 +       struct inode *inode;
29904 +       struct au_branch *br;
29905 +       struct au_sbinfo *sbi;
29906 +
29907 +       au_sigen_inc(sb);
29908 +       sbi = au_sbi(sb);
29909 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
29910 +
29911 +       root = sb->s_root;
29912 +       DiMustNoWaiters(root);
29913 +       inode = d_inode(root);
29914 +       IiMustNoWaiters(inode);
29915 +
29916 +       udba = au_opt_udba(sb);
29917 +       bbot = au_sbbot(sb);
29918 +       for (bindex = 0; bindex <= bbot; bindex++) {
29919 +               br = au_sbr(sb, bindex);
29920 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
29921 +               if (unlikely(err))
29922 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
29923 +                               bindex, err);
29924 +               /* go on even if err */
29925 +       }
29926 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
29927 +
29928 +       if (do_idop) {
29929 +               if (au_ftest_si(sbi, NO_DREVAL)) {
29930 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
29931 +                       sb->s_d_op = &aufs_dop_noreval;
29932 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
29933 +                       sbi->si_iop_array = aufs_iop_nogetattr;
29934 +               } else {
29935 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
29936 +                       sb->s_d_op = &aufs_dop;
29937 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
29938 +                       sbi->si_iop_array = aufs_iop;
29939 +               }
29940 +               pr_info("reset to %ps and %ps\n",
29941 +                       sb->s_d_op, sbi->si_iop_array);
29942 +       }
29943 +
29944 +       di_write_unlock(root);
29945 +       err = au_refresh_d(sb, do_idop);
29946 +       e = au_refresh_i(sb, do_idop);
29947 +       if (unlikely(e && !err))
29948 +               err = e;
29949 +       /* aufs_write_lock() calls ..._child() */
29950 +       di_write_lock_child(root);
29951 +
29952 +       au_cpup_attr_all(inode, /*force*/1);
29953 +
29954 +       if (unlikely(err))
29955 +               AuIOErr("refresh failed, ignored, %d\n", err);
29956 +}
29957 +
29958 +/* stop extra interpretation of errno in mount(8), and strange error messages */
29959 +static int cvt_err(int err)
29960 +{
29961 +       AuTraceErr(err);
29962 +
29963 +       switch (err) {
29964 +       case -ENOENT:
29965 +       case -ENOTDIR:
29966 +       case -EEXIST:
29967 +       case -EIO:
29968 +               err = -EINVAL;
29969 +       }
29970 +       return err;
29971 +}
29972 +
29973 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
29974 +{
29975 +       int err, do_dx;
29976 +       unsigned int mntflags;
29977 +       struct au_opts opts = {
29978 +               .opt = NULL
29979 +       };
29980 +       struct dentry *root;
29981 +       struct inode *inode;
29982 +       struct au_sbinfo *sbinfo;
29983 +
29984 +       err = 0;
29985 +       root = sb->s_root;
29986 +       if (!data || !*data) {
29987 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
29988 +               if (!err) {
29989 +                       di_write_lock_child(root);
29990 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
29991 +                       aufs_write_unlock(root);
29992 +               }
29993 +               goto out;
29994 +       }
29995 +
29996 +       err = -ENOMEM;
29997 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
29998 +       if (unlikely(!opts.opt))
29999 +               goto out;
30000 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30001 +       opts.flags = AuOpts_REMOUNT;
30002 +       opts.sb_flags = *flags;
30003 +
30004 +       /* parse it before aufs lock */
30005 +       err = au_opts_parse(sb, data, &opts);
30006 +       if (unlikely(err))
30007 +               goto out_opts;
30008 +
30009 +       sbinfo = au_sbi(sb);
30010 +       inode = d_inode(root);
30011 +       inode_lock(inode);
30012 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30013 +       if (unlikely(err))
30014 +               goto out_mtx;
30015 +       di_write_lock_child(root);
30016 +
30017 +       /* au_opts_remount() may return an error */
30018 +       err = au_opts_remount(sb, &opts);
30019 +       au_opts_free(&opts);
30020 +
30021 +       if (au_ftest_opts(opts.flags, REFRESH))
30022 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30023 +
30024 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30025 +               mntflags = au_mntflags(sb);
30026 +               do_dx = !!au_opt_test(mntflags, DIO);
30027 +               au_dy_arefresh(do_dx);
30028 +       }
30029 +
30030 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30031 +       aufs_write_unlock(root);
30032 +
30033 +out_mtx:
30034 +       inode_unlock(inode);
30035 +out_opts:
30036 +       free_page((unsigned long)opts.opt);
30037 +out:
30038 +       err = cvt_err(err);
30039 +       AuTraceErr(err);
30040 +       return err;
30041 +}
30042 +
30043 +static const struct super_operations aufs_sop = {
30044 +       .alloc_inode    = aufs_alloc_inode,
30045 +       .destroy_inode  = aufs_destroy_inode,
30046 +       /* always deleting, no clearing */
30047 +       .drop_inode     = generic_delete_inode,
30048 +       .show_options   = aufs_show_options,
30049 +       .statfs         = aufs_statfs,
30050 +       .put_super      = aufs_put_super,
30051 +       .sync_fs        = aufs_sync_fs,
30052 +       .remount_fs     = aufs_remount_fs
30053 +};
30054 +
30055 +/* ---------------------------------------------------------------------- */
30056 +
30057 +static int alloc_root(struct super_block *sb)
30058 +{
30059 +       int err;
30060 +       struct inode *inode;
30061 +       struct dentry *root;
30062 +
30063 +       err = -ENOMEM;
30064 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30065 +       err = PTR_ERR(inode);
30066 +       if (IS_ERR(inode))
30067 +               goto out;
30068 +
30069 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30070 +       inode->i_fop = &aufs_dir_fop;
30071 +       inode->i_mode = S_IFDIR;
30072 +       set_nlink(inode, 2);
30073 +       unlock_new_inode(inode);
30074 +
30075 +       root = d_make_root(inode);
30076 +       if (unlikely(!root))
30077 +               goto out;
30078 +       err = PTR_ERR(root);
30079 +       if (IS_ERR(root))
30080 +               goto out;
30081 +
30082 +       err = au_di_init(root);
30083 +       if (!err) {
30084 +               sb->s_root = root;
30085 +               return 0; /* success */
30086 +       }
30087 +       dput(root);
30088 +
30089 +out:
30090 +       return err;
30091 +}
30092 +
30093 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30094 +                          int silent __maybe_unused)
30095 +{
30096 +       int err;
30097 +       struct au_opts opts = {
30098 +               .opt = NULL
30099 +       };
30100 +       struct au_sbinfo *sbinfo;
30101 +       struct dentry *root;
30102 +       struct inode *inode;
30103 +       char *arg = raw_data;
30104 +
30105 +       if (unlikely(!arg || !*arg)) {
30106 +               err = -EINVAL;
30107 +               pr_err("no arg\n");
30108 +               goto out;
30109 +       }
30110 +
30111 +       err = -ENOMEM;
30112 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30113 +       if (unlikely(!opts.opt))
30114 +               goto out;
30115 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30116 +       opts.sb_flags = sb->s_flags;
30117 +
30118 +       err = au_si_alloc(sb);
30119 +       if (unlikely(err))
30120 +               goto out_opts;
30121 +       sbinfo = au_sbi(sb);
30122 +
30123 +       /* all timestamps always follow the ones on the branch */
30124 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30125 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30126 +       sb->s_op = &aufs_sop;
30127 +       sb->s_d_op = &aufs_dop;
30128 +       sb->s_magic = AUFS_SUPER_MAGIC;
30129 +       sb->s_maxbytes = 0;
30130 +       sb->s_stack_depth = 1;
30131 +       au_export_init(sb);
30132 +       au_xattr_init(sb);
30133 +
30134 +       err = alloc_root(sb);
30135 +       if (unlikely(err)) {
30136 +               si_write_unlock(sb);
30137 +               goto out_info;
30138 +       }
30139 +       root = sb->s_root;
30140 +       inode = d_inode(root);
30141 +
30142 +       /*
30143 +        * actually we can parse options regardless aufs lock here.
30144 +        * but at remount time, parsing must be done before aufs lock.
30145 +        * so we follow the same rule.
30146 +        */
30147 +       ii_write_lock_parent(inode);
30148 +       aufs_write_unlock(root);
30149 +       err = au_opts_parse(sb, arg, &opts);
30150 +       if (unlikely(err))
30151 +               goto out_root;
30152 +
30153 +       /* lock vfs_inode first, then aufs. */
30154 +       inode_lock(inode);
30155 +       aufs_write_lock(root);
30156 +       err = au_opts_mount(sb, &opts);
30157 +       au_opts_free(&opts);
30158 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30159 +               sb->s_d_op = &aufs_dop_noreval;
30160 +               pr_info("%ps\n", sb->s_d_op);
30161 +               au_refresh_dop(root, /*force_reval*/0);
30162 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30163 +               au_refresh_iop(inode, /*force_getattr*/0);
30164 +       }
30165 +       aufs_write_unlock(root);
30166 +       inode_unlock(inode);
30167 +       if (!err)
30168 +               goto out_opts; /* success */
30169 +
30170 +out_root:
30171 +       dput(root);
30172 +       sb->s_root = NULL;
30173 +out_info:
30174 +       kobject_put(&sbinfo->si_kobj);
30175 +       sb->s_fs_info = NULL;
30176 +out_opts:
30177 +       free_page((unsigned long)opts.opt);
30178 +out:
30179 +       AuTraceErr(err);
30180 +       err = cvt_err(err);
30181 +       AuTraceErr(err);
30182 +       return err;
30183 +}
30184 +
30185 +/* ---------------------------------------------------------------------- */
30186 +
30187 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30188 +                                const char *dev_name __maybe_unused,
30189 +                                void *raw_data)
30190 +{
30191 +       struct dentry *root;
30192 +
30193 +       /* all timestamps always follow the ones on the branch */
30194 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30195 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30196 +       if (IS_ERR(root))
30197 +               goto out;
30198 +
30199 +       au_sbilist_add(root->d_sb);
30200 +
30201 +out:
30202 +       return root;
30203 +}
30204 +
30205 +static void aufs_kill_sb(struct super_block *sb)
30206 +{
30207 +       struct au_sbinfo *sbinfo;
30208 +
30209 +       sbinfo = au_sbi(sb);
30210 +       if (sbinfo) {
30211 +               au_sbilist_del(sb);
30212 +               aufs_write_lock(sb->s_root);
30213 +               au_fhsm_fin(sb);
30214 +               if (sbinfo->si_wbr_create_ops->fin)
30215 +                       sbinfo->si_wbr_create_ops->fin(sb);
30216 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30217 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30218 +                       au_remount_refresh(sb, /*do_idop*/0);
30219 +               }
30220 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30221 +                       au_plink_put(sb, /*verbose*/1);
30222 +               au_xino_clr(sb);
30223 +               au_dr_opt_flush(sb);
30224 +               sbinfo->si_sb = NULL;
30225 +               aufs_write_unlock(sb->s_root);
30226 +               au_nwt_flush(&sbinfo->si_nowait);
30227 +       }
30228 +       kill_anon_super(sb);
30229 +}
30230 +
30231 +struct file_system_type aufs_fs_type = {
30232 +       .name           = AUFS_FSTYPE,
30233 +       /* a race between rename and others */
30234 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30235 +       .mount          = aufs_mount,
30236 +       .kill_sb        = aufs_kill_sb,
30237 +       /* no need to __module_get() and module_put(). */
30238 +       .owner          = THIS_MODULE,
30239 +};
30240 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30241 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30242 +++ linux/fs/aufs/super.h       2018-07-19 09:46:13.063314675 +0200
30243 @@ -0,0 +1,627 @@
30244 +/* SPDX-License-Identifier: GPL-2.0 */
30245 +/*
30246 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30247 + *
30248 + * This program, aufs is free software; you can redistribute it and/or modify
30249 + * it under the terms of the GNU General Public License as published by
30250 + * the Free Software Foundation; either version 2 of the License, or
30251 + * (at your option) any later version.
30252 + *
30253 + * This program is distributed in the hope that it will be useful,
30254 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30255 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30256 + * GNU General Public License for more details.
30257 + *
30258 + * You should have received a copy of the GNU General Public License
30259 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30260 + */
30261 +
30262 +/*
30263 + * super_block operations
30264 + */
30265 +
30266 +#ifndef __AUFS_SUPER_H__
30267 +#define __AUFS_SUPER_H__
30268 +
30269 +#ifdef __KERNEL__
30270 +
30271 +#include <linux/fs.h>
30272 +#include <linux/kobject.h>
30273 +#include "hbl.h"
30274 +#include "rwsem.h"
30275 +#include "wkq.h"
30276 +
30277 +/* policies to select one among multiple writable branches */
30278 +struct au_wbr_copyup_operations {
30279 +       int (*copyup)(struct dentry *dentry);
30280 +};
30281 +
30282 +#define AuWbr_DIR      1               /* target is a dir */
30283 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30284 +
30285 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30286 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30287 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30288 +
30289 +struct au_wbr_create_operations {
30290 +       int (*create)(struct dentry *dentry, unsigned int flags);
30291 +       int (*init)(struct super_block *sb);
30292 +       int (*fin)(struct super_block *sb);
30293 +};
30294 +
30295 +struct au_wbr_mfs {
30296 +       struct mutex    mfs_lock; /* protect this structure */
30297 +       unsigned long   mfs_jiffy;
30298 +       unsigned long   mfs_expire;
30299 +       aufs_bindex_t   mfs_bindex;
30300 +
30301 +       unsigned long long      mfsrr_bytes;
30302 +       unsigned long long      mfsrr_watermark;
30303 +};
30304 +
30305 +#define AuPlink_NHASH 100
30306 +static inline int au_plink_hash(ino_t ino)
30307 +{
30308 +       return ino % AuPlink_NHASH;
30309 +}
30310 +
30311 +/* File-based Hierarchical Storage Management */
30312 +struct au_fhsm {
30313 +#ifdef CONFIG_AUFS_FHSM
30314 +       /* allow only one process who can receive the notification */
30315 +       spinlock_t              fhsm_spin;
30316 +       pid_t                   fhsm_pid;
30317 +       wait_queue_head_t       fhsm_wqh;
30318 +       atomic_t                fhsm_readable;
30319 +
30320 +       /* these are protected by si_rwsem */
30321 +       unsigned long           fhsm_expire;
30322 +       aufs_bindex_t           fhsm_bottom;
30323 +#endif
30324 +};
30325 +
30326 +struct au_branch;
30327 +struct au_sbinfo {
30328 +       /* nowait tasks in the system-wide workqueue */
30329 +       struct au_nowait_tasks  si_nowait;
30330 +
30331 +       /*
30332 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
30333 +        * rwsem for au_sbinfo is necessary.
30334 +        */
30335 +       struct au_rwsem         si_rwsem;
30336 +
30337 +       /*
30338 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30339 +        * remount.
30340 +        */
30341 +       struct percpu_counter   si_ninodes, si_nfiles;
30342 +
30343 +       /* branch management */
30344 +       unsigned int            si_generation;
30345 +
30346 +       /* see AuSi_ flags */
30347 +       unsigned char           au_si_status;
30348 +
30349 +       aufs_bindex_t           si_bbot;
30350 +
30351 +       /* dirty trick to keep br_id plus */
30352 +       unsigned int            si_last_br_id :
30353 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30354 +       struct au_branch        **si_branch;
30355 +
30356 +       /* policy to select a writable branch */
30357 +       unsigned char           si_wbr_copyup;
30358 +       unsigned char           si_wbr_create;
30359 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30360 +       struct au_wbr_create_operations *si_wbr_create_ops;
30361 +
30362 +       /* round robin */
30363 +       atomic_t                si_wbr_rr_next;
30364 +
30365 +       /* most free space */
30366 +       struct au_wbr_mfs       si_wbr_mfs;
30367 +
30368 +       /* File-based Hierarchical Storage Management */
30369 +       struct au_fhsm          si_fhsm;
30370 +
30371 +       /* mount flags */
30372 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30373 +       unsigned int            si_mntflags;
30374 +
30375 +       /* external inode number (bitmap and translation table) */
30376 +       vfs_readf_t             si_xread;
30377 +       vfs_writef_t            si_xwrite;
30378 +       struct file             *si_xib;
30379 +       struct mutex            si_xib_mtx; /* protect xib members */
30380 +       unsigned long           *si_xib_buf;
30381 +       unsigned long           si_xib_last_pindex;
30382 +       int                     si_xib_next_bit;
30383 +       aufs_bindex_t           si_xino_brid;
30384 +       unsigned long           si_xino_jiffy;
30385 +       unsigned long           si_xino_expire;
30386 +       /* reserved for future use */
30387 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30388 +
30389 +#ifdef CONFIG_AUFS_EXPORT
30390 +       /* i_generation */
30391 +       struct file             *si_xigen;
30392 +       atomic_t                si_xigen_next;
30393 +#endif
30394 +
30395 +       /* dirty trick to suppoer atomic_open */
30396 +       struct hlist_bl_head    si_aopen;
30397 +
30398 +       /* vdir parameters */
30399 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30400 +       unsigned int            si_rdblk;       /* deblk size */
30401 +       unsigned int            si_rdhash;      /* hash size */
30402 +
30403 +       /*
30404 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30405 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30406 +        * future fsck.aufs or kernel thread will remove them later.
30407 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30408 +        */
30409 +       unsigned int            si_dirwh;
30410 +
30411 +       /* pseudo_link list */
30412 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30413 +       wait_queue_head_t       si_plink_wq;
30414 +       spinlock_t              si_plink_maint_lock;
30415 +       pid_t                   si_plink_maint_pid;
30416 +
30417 +       /* file list */
30418 +       struct hlist_bl_head    si_files;
30419 +
30420 +       /* with/without getattr, brother of sb->s_d_op */
30421 +       struct inode_operations *si_iop_array;
30422 +
30423 +       /*
30424 +        * sysfs and lifetime management.
30425 +        * this is not a small structure and it may be a waste of memory in case
30426 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
30427 +        * but using sysfs is majority.
30428 +        */
30429 +       struct kobject          si_kobj;
30430 +#ifdef CONFIG_DEBUG_FS
30431 +       struct dentry            *si_dbgaufs;
30432 +       struct dentry            *si_dbgaufs_plink;
30433 +       struct dentry            *si_dbgaufs_xib;
30434 +#ifdef CONFIG_AUFS_EXPORT
30435 +       struct dentry            *si_dbgaufs_xigen;
30436 +#endif
30437 +#endif
30438 +
30439 +#ifdef CONFIG_AUFS_SBILIST
30440 +       struct hlist_bl_node    si_list;
30441 +#endif
30442 +
30443 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30444 +       struct super_block      *si_sb;
30445 +};
30446 +
30447 +/* sbinfo status flags */
30448 +/*
30449 + * set true when refresh_dirs() failed at remount time.
30450 + * then try refreshing dirs at access time again.
30451 + * if it is false, refreshing dirs at access time is unnecessary
30452 + */
30453 +#define AuSi_FAILED_REFRESH_DIR        1
30454 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30455 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30456 +
30457 +#ifndef CONFIG_AUFS_FHSM
30458 +#undef AuSi_FHSM
30459 +#define AuSi_FHSM              0
30460 +#endif
30461 +
30462 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30463 +                                          unsigned int flag)
30464 +{
30465 +       AuRwMustAnyLock(&sbi->si_rwsem);
30466 +       return sbi->au_si_status & flag;
30467 +}
30468 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30469 +#define au_fset_si(sbinfo, name) do { \
30470 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30471 +       (sbinfo)->au_si_status |= AuSi_##name; \
30472 +} while (0)
30473 +#define au_fclr_si(sbinfo, name) do { \
30474 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30475 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30476 +} while (0)
30477 +
30478 +/* ---------------------------------------------------------------------- */
30479 +
30480 +/* policy to select one among writable branches */
30481 +#define AuWbrCopyup(sbinfo, ...) \
30482 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30483 +#define AuWbrCreate(sbinfo, ...) \
30484 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30485 +
30486 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30487 +#define AuLock_DW              1               /* write-lock dentry */
30488 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30489 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30490 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30491 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30492 +                                               /* except RENAME_EXCHANGE */
30493 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30494 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30495 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30496 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30497 +#define au_fset_lock(flags, name) \
30498 +       do { (flags) |= AuLock_##name; } while (0)
30499 +#define au_fclr_lock(flags, name) \
30500 +       do { (flags) &= ~AuLock_##name; } while (0)
30501 +
30502 +/* ---------------------------------------------------------------------- */
30503 +
30504 +/* super.c */
30505 +extern struct file_system_type aufs_fs_type;
30506 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30507 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30508 +                                          unsigned long long max, void *arg);
30509 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30510 +                    struct super_block *sb, void *arg);
30511 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30512 +void au_iarray_free(struct inode **a, unsigned long long max);
30513 +
30514 +/* sbinfo.c */
30515 +void au_si_free(struct kobject *kobj);
30516 +int au_si_alloc(struct super_block *sb);
30517 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30518 +
30519 +unsigned int au_sigen_inc(struct super_block *sb);
30520 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30521 +
30522 +int si_read_lock(struct super_block *sb, int flags);
30523 +int si_write_lock(struct super_block *sb, int flags);
30524 +int aufs_read_lock(struct dentry *dentry, int flags);
30525 +void aufs_read_unlock(struct dentry *dentry, int flags);
30526 +void aufs_write_lock(struct dentry *dentry);
30527 +void aufs_write_unlock(struct dentry *dentry);
30528 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30529 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30530 +
30531 +/* wbr_policy.c */
30532 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30533 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30534 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30535 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30536 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30537 +
30538 +/* mvdown.c */
30539 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30540 +
30541 +#ifdef CONFIG_AUFS_FHSM
30542 +/* fhsm.c */
30543 +
30544 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30545 +{
30546 +       pid_t pid;
30547 +
30548 +       spin_lock(&fhsm->fhsm_spin);
30549 +       pid = fhsm->fhsm_pid;
30550 +       spin_unlock(&fhsm->fhsm_spin);
30551 +
30552 +       return pid;
30553 +}
30554 +
30555 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30556 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30557 +int au_fhsm_fd(struct super_block *sb, int oflags);
30558 +int au_fhsm_br_alloc(struct au_branch *br);
30559 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30560 +void au_fhsm_fin(struct super_block *sb);
30561 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30562 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30563 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30564 +#else
30565 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30566 +          int force)
30567 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30568 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30569 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30570 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30571 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30572 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30573 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30574 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30575 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30576 +#endif
30577 +
30578 +/* ---------------------------------------------------------------------- */
30579 +
30580 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30581 +{
30582 +       return sb->s_fs_info;
30583 +}
30584 +
30585 +/* ---------------------------------------------------------------------- */
30586 +
30587 +#ifdef CONFIG_AUFS_EXPORT
30588 +int au_test_nfsd(void);
30589 +void au_export_init(struct super_block *sb);
30590 +void au_xigen_inc(struct inode *inode);
30591 +int au_xigen_new(struct inode *inode);
30592 +int au_xigen_set(struct super_block *sb, struct path *path);
30593 +void au_xigen_clr(struct super_block *sb);
30594 +
30595 +static inline int au_busy_or_stale(void)
30596 +{
30597 +       if (!au_test_nfsd())
30598 +               return -EBUSY;
30599 +       return -ESTALE;
30600 +}
30601 +#else
30602 +AuStubInt0(au_test_nfsd, void)
30603 +AuStubVoid(au_export_init, struct super_block *sb)
30604 +AuStubVoid(au_xigen_inc, struct inode *inode)
30605 +AuStubInt0(au_xigen_new, struct inode *inode)
30606 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30607 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30608 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30609 +#endif /* CONFIG_AUFS_EXPORT */
30610 +
30611 +/* ---------------------------------------------------------------------- */
30612 +
30613 +#ifdef CONFIG_AUFS_SBILIST
30614 +/* module.c */
30615 +extern struct hlist_bl_head au_sbilist;
30616 +
30617 +static inline void au_sbilist_init(void)
30618 +{
30619 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30620 +}
30621 +
30622 +static inline void au_sbilist_add(struct super_block *sb)
30623 +{
30624 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30625 +}
30626 +
30627 +static inline void au_sbilist_del(struct super_block *sb)
30628 +{
30629 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30630 +}
30631 +
30632 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30633 +static inline void au_sbilist_lock(void)
30634 +{
30635 +       hlist_bl_lock(&au_sbilist);
30636 +}
30637 +
30638 +static inline void au_sbilist_unlock(void)
30639 +{
30640 +       hlist_bl_unlock(&au_sbilist);
30641 +}
30642 +#define AuGFP_SBILIST  GFP_ATOMIC
30643 +#else
30644 +AuStubVoid(au_sbilist_lock, void)
30645 +AuStubVoid(au_sbilist_unlock, void)
30646 +#define AuGFP_SBILIST  GFP_NOFS
30647 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30648 +#else
30649 +AuStubVoid(au_sbilist_init, void)
30650 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30651 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30652 +AuStubVoid(au_sbilist_lock, void)
30653 +AuStubVoid(au_sbilist_unlock, void)
30654 +#define AuGFP_SBILIST  GFP_NOFS
30655 +#endif
30656 +
30657 +/* ---------------------------------------------------------------------- */
30658 +
30659 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30660 +{
30661 +       /*
30662 +        * This function is a dynamic '__init' function actually,
30663 +        * so the tiny check for si_rwsem is unnecessary.
30664 +        */
30665 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30666 +#ifdef CONFIG_DEBUG_FS
30667 +       sbinfo->si_dbgaufs = NULL;
30668 +       sbinfo->si_dbgaufs_plink = NULL;
30669 +       sbinfo->si_dbgaufs_xib = NULL;
30670 +#ifdef CONFIG_AUFS_EXPORT
30671 +       sbinfo->si_dbgaufs_xigen = NULL;
30672 +#endif
30673 +#endif
30674 +}
30675 +
30676 +/* ---------------------------------------------------------------------- */
30677 +
30678 +/* current->atomic_flags */
30679 +/* this value should never corrupt the ones defined in linux/sched.h */
30680 +#define PFA_AUFS       7
30681 +
30682 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30683 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30684 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30685 +
30686 +static inline int si_pid_test(struct super_block *sb)
30687 +{
30688 +       return !!task_test_aufs(current);
30689 +}
30690 +
30691 +static inline void si_pid_clr(struct super_block *sb)
30692 +{
30693 +       AuDebugOn(!task_test_aufs(current));
30694 +       task_clear_aufs(current);
30695 +}
30696 +
30697 +static inline void si_pid_set(struct super_block *sb)
30698 +{
30699 +       AuDebugOn(task_test_aufs(current));
30700 +       task_set_aufs(current);
30701 +}
30702 +
30703 +/* ---------------------------------------------------------------------- */
30704 +
30705 +/* lock superblock. mainly for entry point functions */
30706 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30707 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30708 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30709 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30710 +/*
30711 +#define __si_read_trylock_nested(sb) \
30712 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30713 +#define __si_write_trylock_nested(sb) \
30714 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30715 +*/
30716 +
30717 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30718 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30719 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30720 +
30721 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30722 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30723 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30724 +
30725 +static inline void si_noflush_read_lock(struct super_block *sb)
30726 +{
30727 +       __si_read_lock(sb);
30728 +       si_pid_set(sb);
30729 +}
30730 +
30731 +static inline int si_noflush_read_trylock(struct super_block *sb)
30732 +{
30733 +       int locked;
30734 +
30735 +       locked = __si_read_trylock(sb);
30736 +       if (locked)
30737 +               si_pid_set(sb);
30738 +       return locked;
30739 +}
30740 +
30741 +static inline void si_noflush_write_lock(struct super_block *sb)
30742 +{
30743 +       __si_write_lock(sb);
30744 +       si_pid_set(sb);
30745 +}
30746 +
30747 +static inline int si_noflush_write_trylock(struct super_block *sb)
30748 +{
30749 +       int locked;
30750 +
30751 +       locked = __si_write_trylock(sb);
30752 +       if (locked)
30753 +               si_pid_set(sb);
30754 +       return locked;
30755 +}
30756 +
30757 +#if 0 /* reserved */
30758 +static inline int si_read_trylock(struct super_block *sb, int flags)
30759 +{
30760 +       if (au_ftest_lock(flags, FLUSH))
30761 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30762 +       return si_noflush_read_trylock(sb);
30763 +}
30764 +#endif
30765 +
30766 +static inline void si_read_unlock(struct super_block *sb)
30767 +{
30768 +       si_pid_clr(sb);
30769 +       __si_read_unlock(sb);
30770 +}
30771 +
30772 +#if 0 /* reserved */
30773 +static inline int si_write_trylock(struct super_block *sb, int flags)
30774 +{
30775 +       if (au_ftest_lock(flags, FLUSH))
30776 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30777 +       return si_noflush_write_trylock(sb);
30778 +}
30779 +#endif
30780 +
30781 +static inline void si_write_unlock(struct super_block *sb)
30782 +{
30783 +       si_pid_clr(sb);
30784 +       __si_write_unlock(sb);
30785 +}
30786 +
30787 +#if 0 /* reserved */
30788 +static inline void si_downgrade_lock(struct super_block *sb)
30789 +{
30790 +       __si_downgrade_lock(sb);
30791 +}
30792 +#endif
30793 +
30794 +/* ---------------------------------------------------------------------- */
30795 +
30796 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30797 +{
30798 +       SiMustAnyLock(sb);
30799 +       return au_sbi(sb)->si_bbot;
30800 +}
30801 +
30802 +static inline unsigned int au_mntflags(struct super_block *sb)
30803 +{
30804 +       SiMustAnyLock(sb);
30805 +       return au_sbi(sb)->si_mntflags;
30806 +}
30807 +
30808 +static inline unsigned int au_sigen(struct super_block *sb)
30809 +{
30810 +       SiMustAnyLock(sb);
30811 +       return au_sbi(sb)->si_generation;
30812 +}
30813 +
30814 +static inline unsigned long long au_ninodes(struct super_block *sb)
30815 +{
30816 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
30817 +
30818 +       BUG_ON(n < 0);
30819 +       return n;
30820 +}
30821 +
30822 +static inline void au_ninodes_inc(struct super_block *sb)
30823 +{
30824 +       percpu_counter_inc(&au_sbi(sb)->si_ninodes);
30825 +}
30826 +
30827 +static inline void au_ninodes_dec(struct super_block *sb)
30828 +{
30829 +       percpu_counter_dec(&au_sbi(sb)->si_ninodes);
30830 +}
30831 +
30832 +static inline unsigned long long au_nfiles(struct super_block *sb)
30833 +{
30834 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
30835 +
30836 +       BUG_ON(n < 0);
30837 +       return n;
30838 +}
30839 +
30840 +static inline void au_nfiles_inc(struct super_block *sb)
30841 +{
30842 +       percpu_counter_inc(&au_sbi(sb)->si_nfiles);
30843 +}
30844 +
30845 +static inline void au_nfiles_dec(struct super_block *sb)
30846 +{
30847 +       percpu_counter_dec(&au_sbi(sb)->si_nfiles);
30848 +}
30849 +
30850 +static inline struct au_branch *au_sbr(struct super_block *sb,
30851 +                                      aufs_bindex_t bindex)
30852 +{
30853 +       SiMustAnyLock(sb);
30854 +       return au_sbi(sb)->si_branch[0 + bindex];
30855 +}
30856 +
30857 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
30858 +{
30859 +       SiMustWriteLock(sb);
30860 +       au_sbi(sb)->si_xino_brid = brid;
30861 +}
30862 +
30863 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
30864 +{
30865 +       SiMustAnyLock(sb);
30866 +       return au_sbi(sb)->si_xino_brid;
30867 +}
30868 +
30869 +#endif /* __KERNEL__ */
30870 +#endif /* __AUFS_SUPER_H__ */
30871 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30872 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
30873 +++ linux/fs/aufs/sysaufs.c     2018-07-19 09:46:13.063314675 +0200
30874 @@ -0,0 +1,93 @@
30875 +/* SPDX-License-Identifier: GPL-2.0 */
30876 +/*
30877 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30878 + *
30879 + * This program, aufs is free software; you can redistribute it and/or modify
30880 + * it under the terms of the GNU General Public License as published by
30881 + * the Free Software Foundation; either version 2 of the License, or
30882 + * (at your option) any later version.
30883 + *
30884 + * This program is distributed in the hope that it will be useful,
30885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30887 + * GNU General Public License for more details.
30888 + *
30889 + * You should have received a copy of the GNU General Public License
30890 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30891 + */
30892 +
30893 +/*
30894 + * sysfs interface and lifetime management
30895 + * they are necessary regardless sysfs is disabled.
30896 + */
30897 +
30898 +#include <linux/random.h>
30899 +#include "aufs.h"
30900 +
30901 +unsigned long sysaufs_si_mask;
30902 +struct kset *sysaufs_kset;
30903 +
30904 +#define AuSiAttr(_name) { \
30905 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
30906 +       .show   = sysaufs_si_##_name,                           \
30907 +}
30908 +
30909 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30910 +struct attribute *sysaufs_si_attrs[] = {
30911 +       &sysaufs_si_attr_xi_path.attr,
30912 +       NULL,
30913 +};
30914 +
30915 +static const struct sysfs_ops au_sbi_ops = {
30916 +       .show   = sysaufs_si_show
30917 +};
30918 +
30919 +static struct kobj_type au_sbi_ktype = {
30920 +       .release        = au_si_free,
30921 +       .sysfs_ops      = &au_sbi_ops,
30922 +       .default_attrs  = sysaufs_si_attrs
30923 +};
30924 +
30925 +/* ---------------------------------------------------------------------- */
30926 +
30927 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
30928 +{
30929 +       int err;
30930 +
30931 +       sbinfo->si_kobj.kset = sysaufs_kset;
30932 +       /* cf. sysaufs_name() */
30933 +       err = kobject_init_and_add
30934 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
30935 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
30936 +
30937 +       return err;
30938 +}
30939 +
30940 +void sysaufs_fin(void)
30941 +{
30942 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30943 +       kset_unregister(sysaufs_kset);
30944 +}
30945 +
30946 +int __init sysaufs_init(void)
30947 +{
30948 +       int err;
30949 +
30950 +       do {
30951 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
30952 +       } while (!sysaufs_si_mask);
30953 +
30954 +       err = -EINVAL;
30955 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
30956 +       if (unlikely(!sysaufs_kset))
30957 +               goto out;
30958 +       err = PTR_ERR(sysaufs_kset);
30959 +       if (IS_ERR(sysaufs_kset))
30960 +               goto out;
30961 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
30962 +       if (unlikely(err))
30963 +               kset_unregister(sysaufs_kset);
30964 +
30965 +out:
30966 +       return err;
30967 +}
30968 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
30969 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
30970 +++ linux/fs/aufs/sysaufs.h     2018-07-19 09:46:13.063314675 +0200
30971 @@ -0,0 +1,102 @@
30972 +/* SPDX-License-Identifier: GPL-2.0 */
30973 +/*
30974 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30975 + *
30976 + * This program, aufs is free software; you can redistribute it and/or modify
30977 + * it under the terms of the GNU General Public License as published by
30978 + * the Free Software Foundation; either version 2 of the License, or
30979 + * (at your option) any later version.
30980 + *
30981 + * This program is distributed in the hope that it will be useful,
30982 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30983 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30984 + * GNU General Public License for more details.
30985 + *
30986 + * You should have received a copy of the GNU General Public License
30987 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30988 + */
30989 +
30990 +/*
30991 + * sysfs interface and mount lifetime management
30992 + */
30993 +
30994 +#ifndef __SYSAUFS_H__
30995 +#define __SYSAUFS_H__
30996 +
30997 +#ifdef __KERNEL__
30998 +
30999 +#include <linux/sysfs.h>
31000 +#include "module.h"
31001 +
31002 +struct super_block;
31003 +struct au_sbinfo;
31004 +
31005 +struct sysaufs_si_attr {
31006 +       struct attribute attr;
31007 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31008 +};
31009 +
31010 +/* ---------------------------------------------------------------------- */
31011 +
31012 +/* sysaufs.c */
31013 +extern unsigned long sysaufs_si_mask;
31014 +extern struct kset *sysaufs_kset;
31015 +extern struct attribute *sysaufs_si_attrs[];
31016 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31017 +int __init sysaufs_init(void);
31018 +void sysaufs_fin(void);
31019 +
31020 +/* ---------------------------------------------------------------------- */
31021 +
31022 +/* some people doesn't like to show a pointer in kernel */
31023 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31024 +{
31025 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31026 +}
31027 +
31028 +#define SysaufsSiNamePrefix    "si_"
31029 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31030 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31031 +{
31032 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31033 +                sysaufs_si_id(sbinfo));
31034 +}
31035 +
31036 +struct au_branch;
31037 +#ifdef CONFIG_SYSFS
31038 +/* sysfs.c */
31039 +extern struct attribute_group *sysaufs_attr_group;
31040 +
31041 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31042 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31043 +                        char *buf);
31044 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31045 +#ifdef CONFIG_COMPAT
31046 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31047 +#endif
31048 +
31049 +void sysaufs_br_init(struct au_branch *br);
31050 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31051 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31052 +
31053 +#define sysaufs_brs_init()     do {} while (0)
31054 +
31055 +#else
31056 +#define sysaufs_attr_group     NULL
31057 +
31058 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31059 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31060 +       struct attribute *attr, char *buf)
31061 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31062 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31063 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31064 +
31065 +static inline void sysaufs_brs_init(void)
31066 +{
31067 +       sysaufs_brs = 0;
31068 +}
31069 +
31070 +#endif /* CONFIG_SYSFS */
31071 +
31072 +#endif /* __KERNEL__ */
31073 +#endif /* __SYSAUFS_H__ */
31074 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31075 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31076 +++ linux/fs/aufs/sysfs.c       2018-07-19 09:46:13.063314675 +0200
31077 @@ -0,0 +1,373 @@
31078 +/* SPDX-License-Identifier: GPL-2.0 */
31079 +/*
31080 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31081 + *
31082 + * This program, aufs is free software; you can redistribute it and/or modify
31083 + * it under the terms of the GNU General Public License as published by
31084 + * the Free Software Foundation; either version 2 of the License, or
31085 + * (at your option) any later version.
31086 + *
31087 + * This program is distributed in the hope that it will be useful,
31088 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31089 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31090 + * GNU General Public License for more details.
31091 + *
31092 + * You should have received a copy of the GNU General Public License
31093 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31094 + */
31095 +
31096 +/*
31097 + * sysfs interface
31098 + */
31099 +
31100 +#include <linux/compat.h>
31101 +#include <linux/seq_file.h>
31102 +#include "aufs.h"
31103 +
31104 +#ifdef CONFIG_AUFS_FS_MODULE
31105 +/* this entry violates the "one line per file" policy of sysfs */
31106 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31107 +                          char *buf)
31108 +{
31109 +       ssize_t err;
31110 +       static char *conf =
31111 +/* this file is generated at compiling */
31112 +#include "conf.str"
31113 +               ;
31114 +
31115 +       err = snprintf(buf, PAGE_SIZE, conf);
31116 +       if (unlikely(err >= PAGE_SIZE))
31117 +               err = -EFBIG;
31118 +       return err;
31119 +}
31120 +
31121 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31122 +#endif
31123 +
31124 +static struct attribute *au_attr[] = {
31125 +#ifdef CONFIG_AUFS_FS_MODULE
31126 +       &au_config_attr.attr,
31127 +#endif
31128 +       NULL,   /* need to NULL terminate the list of attributes */
31129 +};
31130 +
31131 +static struct attribute_group sysaufs_attr_group_body = {
31132 +       .attrs = au_attr
31133 +};
31134 +
31135 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31136 +
31137 +/* ---------------------------------------------------------------------- */
31138 +
31139 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31140 +{
31141 +       int err;
31142 +
31143 +       SiMustAnyLock(sb);
31144 +
31145 +       err = 0;
31146 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31147 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31148 +               seq_putc(seq, '\n');
31149 +       }
31150 +       return err;
31151 +}
31152 +
31153 +/*
31154 + * the lifetime of branch is independent from the entry under sysfs.
31155 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31156 + * unlinked.
31157 + */
31158 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31159 +                        aufs_bindex_t bindex, int idx)
31160 +{
31161 +       int err;
31162 +       struct path path;
31163 +       struct dentry *root;
31164 +       struct au_branch *br;
31165 +       au_br_perm_str_t perm;
31166 +
31167 +       AuDbg("b%d\n", bindex);
31168 +
31169 +       err = 0;
31170 +       root = sb->s_root;
31171 +       di_read_lock_parent(root, !AuLock_IR);
31172 +       br = au_sbr(sb, bindex);
31173 +
31174 +       switch (idx) {
31175 +       case AuBrSysfs_BR:
31176 +               path.mnt = au_br_mnt(br);
31177 +               path.dentry = au_h_dptr(root, bindex);
31178 +               err = au_seq_path(seq, &path);
31179 +               if (!err) {
31180 +                       au_optstr_br_perm(&perm, br->br_perm);
31181 +                       seq_printf(seq, "=%s\n", perm.a);
31182 +               }
31183 +               break;
31184 +       case AuBrSysfs_BRID:
31185 +               seq_printf(seq, "%d\n", br->br_id);
31186 +               break;
31187 +       }
31188 +       di_read_unlock(root, !AuLock_IR);
31189 +       if (unlikely(err || seq_has_overflowed(seq)))
31190 +               err = -E2BIG;
31191 +
31192 +       return err;
31193 +}
31194 +
31195 +/* ---------------------------------------------------------------------- */
31196 +
31197 +static struct seq_file *au_seq(char *p, ssize_t len)
31198 +{
31199 +       struct seq_file *seq;
31200 +
31201 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31202 +       if (seq) {
31203 +               /* mutex_init(&seq.lock); */
31204 +               seq->buf = p;
31205 +               seq->size = len;
31206 +               return seq; /* success */
31207 +       }
31208 +
31209 +       seq = ERR_PTR(-ENOMEM);
31210 +       return seq;
31211 +}
31212 +
31213 +#define SysaufsBr_PREFIX       "br"
31214 +#define SysaufsBrid_PREFIX     "brid"
31215 +
31216 +/* todo: file size may exceed PAGE_SIZE */
31217 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31218 +                       char *buf)
31219 +{
31220 +       ssize_t err;
31221 +       int idx;
31222 +       long l;
31223 +       aufs_bindex_t bbot;
31224 +       struct au_sbinfo *sbinfo;
31225 +       struct super_block *sb;
31226 +       struct seq_file *seq;
31227 +       char *name;
31228 +       struct attribute **cattr;
31229 +
31230 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31231 +       sb = sbinfo->si_sb;
31232 +
31233 +       /*
31234 +        * prevent a race condition between sysfs and aufs.
31235 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31236 +        * prohibits maintaining the sysfs entries.
31237 +        * hew we acquire read lock after sysfs_get_active_two().
31238 +        * on the other hand, the remount process may maintain the sysfs/aufs
31239 +        * entries after acquiring write lock.
31240 +        * it can cause a deadlock.
31241 +        * simply we gave up processing read here.
31242 +        */
31243 +       err = -EBUSY;
31244 +       if (unlikely(!si_noflush_read_trylock(sb)))
31245 +               goto out;
31246 +
31247 +       seq = au_seq(buf, PAGE_SIZE);
31248 +       err = PTR_ERR(seq);
31249 +       if (IS_ERR(seq))
31250 +               goto out_unlock;
31251 +
31252 +       name = (void *)attr->name;
31253 +       cattr = sysaufs_si_attrs;
31254 +       while (*cattr) {
31255 +               if (!strcmp(name, (*cattr)->name)) {
31256 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31257 +                               ->show(seq, sb);
31258 +                       goto out_seq;
31259 +               }
31260 +               cattr++;
31261 +       }
31262 +
31263 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31264 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31265 +               idx = AuBrSysfs_BRID;
31266 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31267 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31268 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31269 +               idx = AuBrSysfs_BR;
31270 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31271 +       } else
31272 +                 BUG();
31273 +
31274 +       err = kstrtol(name, 10, &l);
31275 +       if (!err) {
31276 +               bbot = au_sbbot(sb);
31277 +               if (l <= bbot)
31278 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31279 +               else
31280 +                       err = -ENOENT;
31281 +       }
31282 +
31283 +out_seq:
31284 +       if (!err) {
31285 +               err = seq->count;
31286 +               /* sysfs limit */
31287 +               if (unlikely(err == PAGE_SIZE))
31288 +                       err = -EFBIG;
31289 +       }
31290 +       kfree(seq);
31291 +out_unlock:
31292 +       si_read_unlock(sb);
31293 +out:
31294 +       return err;
31295 +}
31296 +
31297 +/* ---------------------------------------------------------------------- */
31298 +
31299 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31300 +{
31301 +       int err;
31302 +       int16_t brid;
31303 +       aufs_bindex_t bindex, bbot;
31304 +       size_t sz;
31305 +       char *buf;
31306 +       struct seq_file *seq;
31307 +       struct au_branch *br;
31308 +
31309 +       si_read_lock(sb, AuLock_FLUSH);
31310 +       bbot = au_sbbot(sb);
31311 +       err = bbot + 1;
31312 +       if (!arg)
31313 +               goto out;
31314 +
31315 +       err = -ENOMEM;
31316 +       buf = (void *)__get_free_page(GFP_NOFS);
31317 +       if (unlikely(!buf))
31318 +               goto out;
31319 +
31320 +       seq = au_seq(buf, PAGE_SIZE);
31321 +       err = PTR_ERR(seq);
31322 +       if (IS_ERR(seq))
31323 +               goto out_buf;
31324 +
31325 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31326 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31327 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31328 +               if (unlikely(err))
31329 +                       break;
31330 +
31331 +               br = au_sbr(sb, bindex);
31332 +               brid = br->br_id;
31333 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31334 +               err = __put_user(brid, &arg->id);
31335 +               if (unlikely(err))
31336 +                       break;
31337 +
31338 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31339 +               err = __put_user(br->br_perm, &arg->perm);
31340 +               if (unlikely(err))
31341 +                       break;
31342 +
31343 +               err = au_seq_path(seq, &br->br_path);
31344 +               if (unlikely(err))
31345 +                       break;
31346 +               seq_putc(seq, '\0');
31347 +               if (!seq_has_overflowed(seq)) {
31348 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31349 +                       seq->count = 0;
31350 +                       if (unlikely(err))
31351 +                               break;
31352 +               } else {
31353 +                       err = -E2BIG;
31354 +                       goto out_seq;
31355 +               }
31356 +       }
31357 +       if (unlikely(err))
31358 +               err = -EFAULT;
31359 +
31360 +out_seq:
31361 +       kfree(seq);
31362 +out_buf:
31363 +       free_page((unsigned long)buf);
31364 +out:
31365 +       si_read_unlock(sb);
31366 +       return err;
31367 +}
31368 +
31369 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31370 +{
31371 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31372 +}
31373 +
31374 +#ifdef CONFIG_COMPAT
31375 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31376 +{
31377 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31378 +}
31379 +#endif
31380 +
31381 +/* ---------------------------------------------------------------------- */
31382 +
31383 +void sysaufs_br_init(struct au_branch *br)
31384 +{
31385 +       int i;
31386 +       struct au_brsysfs *br_sysfs;
31387 +       struct attribute *attr;
31388 +
31389 +       br_sysfs = br->br_sysfs;
31390 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31391 +               attr = &br_sysfs->attr;
31392 +               sysfs_attr_init(attr);
31393 +               attr->name = br_sysfs->name;
31394 +               attr->mode = S_IRUGO;
31395 +               br_sysfs++;
31396 +       }
31397 +}
31398 +
31399 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31400 +{
31401 +       struct au_branch *br;
31402 +       struct kobject *kobj;
31403 +       struct au_brsysfs *br_sysfs;
31404 +       int i;
31405 +       aufs_bindex_t bbot;
31406 +
31407 +       if (!sysaufs_brs)
31408 +               return;
31409 +
31410 +       kobj = &au_sbi(sb)->si_kobj;
31411 +       bbot = au_sbbot(sb);
31412 +       for (; bindex <= bbot; bindex++) {
31413 +               br = au_sbr(sb, bindex);
31414 +               br_sysfs = br->br_sysfs;
31415 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31416 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31417 +                       br_sysfs++;
31418 +               }
31419 +       }
31420 +}
31421 +
31422 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31423 +{
31424 +       int err, i;
31425 +       aufs_bindex_t bbot;
31426 +       struct kobject *kobj;
31427 +       struct au_branch *br;
31428 +       struct au_brsysfs *br_sysfs;
31429 +
31430 +       if (!sysaufs_brs)
31431 +               return;
31432 +
31433 +       kobj = &au_sbi(sb)->si_kobj;
31434 +       bbot = au_sbbot(sb);
31435 +       for (; bindex <= bbot; bindex++) {
31436 +               br = au_sbr(sb, bindex);
31437 +               br_sysfs = br->br_sysfs;
31438 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31439 +                        SysaufsBr_PREFIX "%d", bindex);
31440 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31441 +                        SysaufsBrid_PREFIX "%d", bindex);
31442 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31443 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31444 +                       if (unlikely(err))
31445 +                               pr_warn("failed %s under sysfs(%d)\n",
31446 +                                       br_sysfs->name, err);
31447 +                       br_sysfs++;
31448 +               }
31449 +       }
31450 +}
31451 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31452 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31453 +++ linux/fs/aufs/sysrq.c       2018-07-19 09:46:13.063314675 +0200
31454 @@ -0,0 +1,160 @@
31455 +/* SPDX-License-Identifier: GPL-2.0 */
31456 +/*
31457 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31458 + *
31459 + * This program, aufs is free software; you can redistribute it and/or modify
31460 + * it under the terms of the GNU General Public License as published by
31461 + * the Free Software Foundation; either version 2 of the License, or
31462 + * (at your option) any later version.
31463 + *
31464 + * This program is distributed in the hope that it will be useful,
31465 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31466 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31467 + * GNU General Public License for more details.
31468 + *
31469 + * You should have received a copy of the GNU General Public License
31470 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31471 + */
31472 +
31473 +/*
31474 + * magic sysrq hanlder
31475 + */
31476 +
31477 +/* #include <linux/sysrq.h> */
31478 +#include <linux/writeback.h>
31479 +#include "aufs.h"
31480 +
31481 +/* ---------------------------------------------------------------------- */
31482 +
31483 +static void sysrq_sb(struct super_block *sb)
31484 +{
31485 +       char *plevel;
31486 +       struct au_sbinfo *sbinfo;
31487 +       struct file *file;
31488 +       struct hlist_bl_head *files;
31489 +       struct hlist_bl_node *pos;
31490 +       struct au_finfo *finfo;
31491 +
31492 +       plevel = au_plevel;
31493 +       au_plevel = KERN_WARNING;
31494 +
31495 +       /* since we define pr_fmt, call printk directly */
31496 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31497 +
31498 +       sbinfo = au_sbi(sb);
31499 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31500 +       pr("superblock\n");
31501 +       au_dpri_sb(sb);
31502 +
31503 +#if 0
31504 +       pr("root dentry\n");
31505 +       au_dpri_dentry(sb->s_root);
31506 +       pr("root inode\n");
31507 +       au_dpri_inode(d_inode(sb->s_root));
31508 +#endif
31509 +
31510 +#if 0
31511 +       do {
31512 +               int err, i, j, ndentry;
31513 +               struct au_dcsub_pages dpages;
31514 +               struct au_dpage *dpage;
31515 +
31516 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31517 +               if (unlikely(err))
31518 +                       break;
31519 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31520 +               if (!err)
31521 +                       for (i = 0; i < dpages.ndpage; i++) {
31522 +                               dpage = dpages.dpages + i;
31523 +                               ndentry = dpage->ndentry;
31524 +                               for (j = 0; j < ndentry; j++)
31525 +                                       au_dpri_dentry(dpage->dentries[j]);
31526 +                       }
31527 +               au_dpages_free(&dpages);
31528 +       } while (0);
31529 +#endif
31530 +
31531 +#if 1
31532 +       {
31533 +               struct inode *i;
31534 +
31535 +               pr("isolated inode\n");
31536 +               spin_lock(&sb->s_inode_list_lock);
31537 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31538 +                       spin_lock(&i->i_lock);
31539 +                       if (1 || hlist_empty(&i->i_dentry))
31540 +                               au_dpri_inode(i);
31541 +                       spin_unlock(&i->i_lock);
31542 +               }
31543 +               spin_unlock(&sb->s_inode_list_lock);
31544 +       }
31545 +#endif
31546 +       pr("files\n");
31547 +       files = &au_sbi(sb)->si_files;
31548 +       hlist_bl_lock(files);
31549 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31550 +               umode_t mode;
31551 +
31552 +               file = finfo->fi_file;
31553 +               mode = file_inode(file)->i_mode;
31554 +               if (!special_file(mode))
31555 +                       au_dpri_file(file);
31556 +       }
31557 +       hlist_bl_unlock(files);
31558 +       pr("done\n");
31559 +
31560 +#undef pr
31561 +       au_plevel = plevel;
31562 +}
31563 +
31564 +/* ---------------------------------------------------------------------- */
31565 +
31566 +/* module parameter */
31567 +static char *aufs_sysrq_key = "a";
31568 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
31569 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31570 +
31571 +static void au_sysrq(int key __maybe_unused)
31572 +{
31573 +       struct au_sbinfo *sbinfo;
31574 +       struct hlist_bl_node *pos;
31575 +
31576 +       lockdep_off();
31577 +       au_sbilist_lock();
31578 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31579 +               sysrq_sb(sbinfo->si_sb);
31580 +       au_sbilist_unlock();
31581 +       lockdep_on();
31582 +}
31583 +
31584 +static struct sysrq_key_op au_sysrq_op = {
31585 +       .handler        = au_sysrq,
31586 +       .help_msg       = "Aufs",
31587 +       .action_msg     = "Aufs",
31588 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31589 +};
31590 +
31591 +/* ---------------------------------------------------------------------- */
31592 +
31593 +int __init au_sysrq_init(void)
31594 +{
31595 +       int err;
31596 +       char key;
31597 +
31598 +       err = -1;
31599 +       key = *aufs_sysrq_key;
31600 +       if ('a' <= key && key <= 'z')
31601 +               err = register_sysrq_key(key, &au_sysrq_op);
31602 +       if (unlikely(err))
31603 +               pr_err("err %d, sysrq=%c\n", err, key);
31604 +       return err;
31605 +}
31606 +
31607 +void au_sysrq_fin(void)
31608 +{
31609 +       int err;
31610 +
31611 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31612 +       if (unlikely(err))
31613 +               pr_err("err %d (ignored)\n", err);
31614 +}
31615 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31616 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31617 +++ linux/fs/aufs/vdir.c        2018-07-19 09:46:13.063314675 +0200
31618 @@ -0,0 +1,894 @@
31619 +/* SPDX-License-Identifier: GPL-2.0 */
31620 +/*
31621 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31622 + *
31623 + * This program, aufs is free software; you can redistribute it and/or modify
31624 + * it under the terms of the GNU General Public License as published by
31625 + * the Free Software Foundation; either version 2 of the License, or
31626 + * (at your option) any later version.
31627 + *
31628 + * This program is distributed in the hope that it will be useful,
31629 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31630 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31631 + * GNU General Public License for more details.
31632 + *
31633 + * You should have received a copy of the GNU General Public License
31634 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31635 + */
31636 +
31637 +/*
31638 + * virtual or vertical directory
31639 + */
31640 +
31641 +#include "aufs.h"
31642 +
31643 +static unsigned int calc_size(int nlen)
31644 +{
31645 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31646 +}
31647 +
31648 +static int set_deblk_end(union au_vdir_deblk_p *p,
31649 +                        union au_vdir_deblk_p *deblk_end)
31650 +{
31651 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31652 +               p->de->de_str.len = 0;
31653 +               /* smp_mb(); */
31654 +               return 0;
31655 +       }
31656 +       return -1; /* error */
31657 +}
31658 +
31659 +/* returns true or false */
31660 +static int is_deblk_end(union au_vdir_deblk_p *p,
31661 +                       union au_vdir_deblk_p *deblk_end)
31662 +{
31663 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31664 +               return !p->de->de_str.len;
31665 +       return 1;
31666 +}
31667 +
31668 +static unsigned char *last_deblk(struct au_vdir *vdir)
31669 +{
31670 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31671 +}
31672 +
31673 +/* ---------------------------------------------------------------------- */
31674 +
31675 +/* estimate the appropriate size for name hash table */
31676 +unsigned int au_rdhash_est(loff_t sz)
31677 +{
31678 +       unsigned int n;
31679 +
31680 +       n = UINT_MAX;
31681 +       sz >>= 10;
31682 +       if (sz < n)
31683 +               n = sz;
31684 +       if (sz < AUFS_RDHASH_DEF)
31685 +               n = AUFS_RDHASH_DEF;
31686 +       /* pr_info("n %u\n", n); */
31687 +       return n;
31688 +}
31689 +
31690 +/*
31691 + * the allocated memory has to be freed by
31692 + * au_nhash_wh_free() or au_nhash_de_free().
31693 + */
31694 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31695 +{
31696 +       struct hlist_head *head;
31697 +       unsigned int u;
31698 +       size_t sz;
31699 +
31700 +       sz = sizeof(*nhash->nh_head) * num_hash;
31701 +       head = kmalloc(sz, gfp);
31702 +       if (head) {
31703 +               nhash->nh_num = num_hash;
31704 +               nhash->nh_head = head;
31705 +               for (u = 0; u < num_hash; u++)
31706 +                       INIT_HLIST_HEAD(head++);
31707 +               return 0; /* success */
31708 +       }
31709 +
31710 +       return -ENOMEM;
31711 +}
31712 +
31713 +static void nhash_count(struct hlist_head *head)
31714 +{
31715 +#if 0
31716 +       unsigned long n;
31717 +       struct hlist_node *pos;
31718 +
31719 +       n = 0;
31720 +       hlist_for_each(pos, head)
31721 +               n++;
31722 +       pr_info("%lu\n", n);
31723 +#endif
31724 +}
31725 +
31726 +static void au_nhash_wh_do_free(struct hlist_head *head)
31727 +{
31728 +       struct au_vdir_wh *pos;
31729 +       struct hlist_node *node;
31730 +
31731 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31732 +               kfree(pos);
31733 +}
31734 +
31735 +static void au_nhash_de_do_free(struct hlist_head *head)
31736 +{
31737 +       struct au_vdir_dehstr *pos;
31738 +       struct hlist_node *node;
31739 +
31740 +       hlist_for_each_entry_safe(pos, node, head, hash)
31741 +               au_cache_free_vdir_dehstr(pos);
31742 +}
31743 +
31744 +static void au_nhash_do_free(struct au_nhash *nhash,
31745 +                            void (*free)(struct hlist_head *head))
31746 +{
31747 +       unsigned int n;
31748 +       struct hlist_head *head;
31749 +
31750 +       n = nhash->nh_num;
31751 +       if (!n)
31752 +               return;
31753 +
31754 +       head = nhash->nh_head;
31755 +       while (n-- > 0) {
31756 +               nhash_count(head);
31757 +               free(head++);
31758 +       }
31759 +       kfree(nhash->nh_head);
31760 +}
31761 +
31762 +void au_nhash_wh_free(struct au_nhash *whlist)
31763 +{
31764 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31765 +}
31766 +
31767 +static void au_nhash_de_free(struct au_nhash *delist)
31768 +{
31769 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31770 +}
31771 +
31772 +/* ---------------------------------------------------------------------- */
31773 +
31774 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31775 +                           int limit)
31776 +{
31777 +       int num;
31778 +       unsigned int u, n;
31779 +       struct hlist_head *head;
31780 +       struct au_vdir_wh *pos;
31781 +
31782 +       num = 0;
31783 +       n = whlist->nh_num;
31784 +       head = whlist->nh_head;
31785 +       for (u = 0; u < n; u++, head++)
31786 +               hlist_for_each_entry(pos, head, wh_hash)
31787 +                       if (pos->wh_bindex == btgt && ++num > limit)
31788 +                               return 1;
31789 +       return 0;
31790 +}
31791 +
31792 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31793 +                                      unsigned char *name,
31794 +                                      unsigned int len)
31795 +{
31796 +       unsigned int v;
31797 +       /* const unsigned int magic_bit = 12; */
31798 +
31799 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31800 +
31801 +       v = 0;
31802 +       if (len > 8)
31803 +               len = 8;
31804 +       while (len--)
31805 +               v += *name++;
31806 +       /* v = hash_long(v, magic_bit); */
31807 +       v %= nhash->nh_num;
31808 +       return nhash->nh_head + v;
31809 +}
31810 +
31811 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31812 +                             int nlen)
31813 +{
31814 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31815 +}
31816 +
31817 +/* returns found or not */
31818 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31819 +{
31820 +       struct hlist_head *head;
31821 +       struct au_vdir_wh *pos;
31822 +       struct au_vdir_destr *str;
31823 +
31824 +       head = au_name_hash(whlist, name, nlen);
31825 +       hlist_for_each_entry(pos, head, wh_hash) {
31826 +               str = &pos->wh_str;
31827 +               AuDbg("%.*s\n", str->len, str->name);
31828 +               if (au_nhash_test_name(str, name, nlen))
31829 +                       return 1;
31830 +       }
31831 +       return 0;
31832 +}
31833 +
31834 +/* returns found(true) or not */
31835 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31836 +{
31837 +       struct hlist_head *head;
31838 +       struct au_vdir_dehstr *pos;
31839 +       struct au_vdir_destr *str;
31840 +
31841 +       head = au_name_hash(delist, name, nlen);
31842 +       hlist_for_each_entry(pos, head, hash) {
31843 +               str = pos->str;
31844 +               AuDbg("%.*s\n", str->len, str->name);
31845 +               if (au_nhash_test_name(str, name, nlen))
31846 +                       return 1;
31847 +       }
31848 +       return 0;
31849 +}
31850 +
31851 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31852 +                           unsigned char d_type)
31853 +{
31854 +#ifdef CONFIG_AUFS_SHWH
31855 +       wh->wh_ino = ino;
31856 +       wh->wh_type = d_type;
31857 +#endif
31858 +}
31859 +
31860 +/* ---------------------------------------------------------------------- */
31861 +
31862 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31863 +                      unsigned int d_type, aufs_bindex_t bindex,
31864 +                      unsigned char shwh)
31865 +{
31866 +       int err;
31867 +       struct au_vdir_destr *str;
31868 +       struct au_vdir_wh *wh;
31869 +
31870 +       AuDbg("%.*s\n", nlen, name);
31871 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31872 +
31873 +       err = -ENOMEM;
31874 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31875 +       if (unlikely(!wh))
31876 +               goto out;
31877 +
31878 +       err = 0;
31879 +       wh->wh_bindex = bindex;
31880 +       if (shwh)
31881 +               au_shwh_init_wh(wh, ino, d_type);
31882 +       str = &wh->wh_str;
31883 +       str->len = nlen;
31884 +       memcpy(str->name, name, nlen);
31885 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31886 +       /* smp_mb(); */
31887 +
31888 +out:
31889 +       return err;
31890 +}
31891 +
31892 +static int append_deblk(struct au_vdir *vdir)
31893 +{
31894 +       int err;
31895 +       unsigned long ul;
31896 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31897 +       union au_vdir_deblk_p p, deblk_end;
31898 +       unsigned char **o;
31899 +
31900 +       err = -ENOMEM;
31901 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31902 +                       GFP_NOFS, /*may_shrink*/0);
31903 +       if (unlikely(!o))
31904 +               goto out;
31905 +
31906 +       vdir->vd_deblk = o;
31907 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31908 +       if (p.deblk) {
31909 +               ul = vdir->vd_nblk++;
31910 +               vdir->vd_deblk[ul] = p.deblk;
31911 +               vdir->vd_last.ul = ul;
31912 +               vdir->vd_last.p.deblk = p.deblk;
31913 +               deblk_end.deblk = p.deblk + deblk_sz;
31914 +               err = set_deblk_end(&p, &deblk_end);
31915 +       }
31916 +
31917 +out:
31918 +       return err;
31919 +}
31920 +
31921 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31922 +                    unsigned int d_type, struct au_nhash *delist)
31923 +{
31924 +       int err;
31925 +       unsigned int sz;
31926 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31927 +       union au_vdir_deblk_p p, *room, deblk_end;
31928 +       struct au_vdir_dehstr *dehstr;
31929 +
31930 +       p.deblk = last_deblk(vdir);
31931 +       deblk_end.deblk = p.deblk + deblk_sz;
31932 +       room = &vdir->vd_last.p;
31933 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
31934 +                 || !is_deblk_end(room, &deblk_end));
31935 +
31936 +       sz = calc_size(nlen);
31937 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
31938 +               err = append_deblk(vdir);
31939 +               if (unlikely(err))
31940 +                       goto out;
31941 +
31942 +               p.deblk = last_deblk(vdir);
31943 +               deblk_end.deblk = p.deblk + deblk_sz;
31944 +               /* smp_mb(); */
31945 +               AuDebugOn(room->deblk != p.deblk);
31946 +       }
31947 +
31948 +       err = -ENOMEM;
31949 +       dehstr = au_cache_alloc_vdir_dehstr();
31950 +       if (unlikely(!dehstr))
31951 +               goto out;
31952 +
31953 +       dehstr->str = &room->de->de_str;
31954 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
31955 +       room->de->de_ino = ino;
31956 +       room->de->de_type = d_type;
31957 +       room->de->de_str.len = nlen;
31958 +       memcpy(room->de->de_str.name, name, nlen);
31959 +
31960 +       err = 0;
31961 +       room->deblk += sz;
31962 +       if (unlikely(set_deblk_end(room, &deblk_end)))
31963 +               err = append_deblk(vdir);
31964 +       /* smp_mb(); */
31965 +
31966 +out:
31967 +       return err;
31968 +}
31969 +
31970 +/* ---------------------------------------------------------------------- */
31971 +
31972 +void au_vdir_free(struct au_vdir *vdir)
31973 +{
31974 +       unsigned char **deblk;
31975 +
31976 +       deblk = vdir->vd_deblk;
31977 +       while (vdir->vd_nblk--)
31978 +               kfree(*deblk++);
31979 +       kfree(vdir->vd_deblk);
31980 +       au_cache_free_vdir(vdir);
31981 +}
31982 +
31983 +static struct au_vdir *alloc_vdir(struct file *file)
31984 +{
31985 +       struct au_vdir *vdir;
31986 +       struct super_block *sb;
31987 +       int err;
31988 +
31989 +       sb = file->f_path.dentry->d_sb;
31990 +       SiMustAnyLock(sb);
31991 +
31992 +       err = -ENOMEM;
31993 +       vdir = au_cache_alloc_vdir();
31994 +       if (unlikely(!vdir))
31995 +               goto out;
31996 +
31997 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
31998 +       if (unlikely(!vdir->vd_deblk))
31999 +               goto out_free;
32000 +
32001 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32002 +       if (!vdir->vd_deblk_sz) {
32003 +               /* estimate the appropriate size for deblk */
32004 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32005 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32006 +       }
32007 +       vdir->vd_nblk = 0;
32008 +       vdir->vd_version = 0;
32009 +       vdir->vd_jiffy = 0;
32010 +       err = append_deblk(vdir);
32011 +       if (!err)
32012 +               return vdir; /* success */
32013 +
32014 +       kfree(vdir->vd_deblk);
32015 +
32016 +out_free:
32017 +       au_cache_free_vdir(vdir);
32018 +out:
32019 +       vdir = ERR_PTR(err);
32020 +       return vdir;
32021 +}
32022 +
32023 +static int reinit_vdir(struct au_vdir *vdir)
32024 +{
32025 +       int err;
32026 +       union au_vdir_deblk_p p, deblk_end;
32027 +
32028 +       while (vdir->vd_nblk > 1) {
32029 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
32030 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32031 +               vdir->vd_nblk--;
32032 +       }
32033 +       p.deblk = vdir->vd_deblk[0];
32034 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32035 +       err = set_deblk_end(&p, &deblk_end);
32036 +       /* keep vd_dblk_sz */
32037 +       vdir->vd_last.ul = 0;
32038 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32039 +       vdir->vd_version = 0;
32040 +       vdir->vd_jiffy = 0;
32041 +       /* smp_mb(); */
32042 +       return err;
32043 +}
32044 +
32045 +/* ---------------------------------------------------------------------- */
32046 +
32047 +#define AuFillVdir_CALLED      1
32048 +#define AuFillVdir_WHABLE      (1 << 1)
32049 +#define AuFillVdir_SHWH                (1 << 2)
32050 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32051 +#define au_fset_fillvdir(flags, name) \
32052 +       do { (flags) |= AuFillVdir_##name; } while (0)
32053 +#define au_fclr_fillvdir(flags, name) \
32054 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32055 +
32056 +#ifndef CONFIG_AUFS_SHWH
32057 +#undef AuFillVdir_SHWH
32058 +#define AuFillVdir_SHWH                0
32059 +#endif
32060 +
32061 +struct fillvdir_arg {
32062 +       struct dir_context      ctx;
32063 +       struct file             *file;
32064 +       struct au_vdir          *vdir;
32065 +       struct au_nhash         delist;
32066 +       struct au_nhash         whlist;
32067 +       aufs_bindex_t           bindex;
32068 +       unsigned int            flags;
32069 +       int                     err;
32070 +};
32071 +
32072 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32073 +                   loff_t offset __maybe_unused, u64 h_ino,
32074 +                   unsigned int d_type)
32075 +{
32076 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32077 +       char *name = (void *)__name;
32078 +       struct super_block *sb;
32079 +       ino_t ino;
32080 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32081 +
32082 +       arg->err = 0;
32083 +       sb = arg->file->f_path.dentry->d_sb;
32084 +       au_fset_fillvdir(arg->flags, CALLED);
32085 +       /* smp_mb(); */
32086 +       if (nlen <= AUFS_WH_PFX_LEN
32087 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32088 +               if (test_known(&arg->delist, name, nlen)
32089 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32090 +                       goto out; /* already exists or whiteouted */
32091 +
32092 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32093 +               if (!arg->err) {
32094 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32095 +                               d_type = DT_UNKNOWN;
32096 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32097 +                                            d_type, &arg->delist);
32098 +               }
32099 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32100 +               name += AUFS_WH_PFX_LEN;
32101 +               nlen -= AUFS_WH_PFX_LEN;
32102 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32103 +                       goto out; /* already whiteouted */
32104 +
32105 +               if (shwh)
32106 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32107 +                                            &ino);
32108 +               if (!arg->err) {
32109 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32110 +                               d_type = DT_UNKNOWN;
32111 +                       arg->err = au_nhash_append_wh
32112 +                               (&arg->whlist, name, nlen, ino, d_type,
32113 +                                arg->bindex, shwh);
32114 +               }
32115 +       }
32116 +
32117 +out:
32118 +       if (!arg->err)
32119 +               arg->vdir->vd_jiffy = jiffies;
32120 +       /* smp_mb(); */
32121 +       AuTraceErr(arg->err);
32122 +       return arg->err;
32123 +}
32124 +
32125 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32126 +                         struct au_nhash *whlist, struct au_nhash *delist)
32127 +{
32128 +#ifdef CONFIG_AUFS_SHWH
32129 +       int err;
32130 +       unsigned int nh, u;
32131 +       struct hlist_head *head;
32132 +       struct au_vdir_wh *pos;
32133 +       struct hlist_node *n;
32134 +       char *p, *o;
32135 +       struct au_vdir_destr *destr;
32136 +
32137 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32138 +
32139 +       err = -ENOMEM;
32140 +       o = p = (void *)__get_free_page(GFP_NOFS);
32141 +       if (unlikely(!p))
32142 +               goto out;
32143 +
32144 +       err = 0;
32145 +       nh = whlist->nh_num;
32146 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32147 +       p += AUFS_WH_PFX_LEN;
32148 +       for (u = 0; u < nh; u++) {
32149 +               head = whlist->nh_head + u;
32150 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32151 +                       destr = &pos->wh_str;
32152 +                       memcpy(p, destr->name, destr->len);
32153 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32154 +                                       pos->wh_ino, pos->wh_type, delist);
32155 +                       if (unlikely(err))
32156 +                               break;
32157 +               }
32158 +       }
32159 +
32160 +       free_page((unsigned long)o);
32161 +
32162 +out:
32163 +       AuTraceErr(err);
32164 +       return err;
32165 +#else
32166 +       return 0;
32167 +#endif
32168 +}
32169 +
32170 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32171 +{
32172 +       int err;
32173 +       unsigned int rdhash;
32174 +       loff_t offset;
32175 +       aufs_bindex_t bbot, bindex, btop;
32176 +       unsigned char shwh;
32177 +       struct file *hf, *file;
32178 +       struct super_block *sb;
32179 +
32180 +       file = arg->file;
32181 +       sb = file->f_path.dentry->d_sb;
32182 +       SiMustAnyLock(sb);
32183 +
32184 +       rdhash = au_sbi(sb)->si_rdhash;
32185 +       if (!rdhash)
32186 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32187 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32188 +       if (unlikely(err))
32189 +               goto out;
32190 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32191 +       if (unlikely(err))
32192 +               goto out_delist;
32193 +
32194 +       err = 0;
32195 +       arg->flags = 0;
32196 +       shwh = 0;
32197 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32198 +               shwh = 1;
32199 +               au_fset_fillvdir(arg->flags, SHWH);
32200 +       }
32201 +       btop = au_fbtop(file);
32202 +       bbot = au_fbbot_dir(file);
32203 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32204 +               hf = au_hf_dir(file, bindex);
32205 +               if (!hf)
32206 +                       continue;
32207 +
32208 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32209 +               err = offset;
32210 +               if (unlikely(offset))
32211 +                       break;
32212 +
32213 +               arg->bindex = bindex;
32214 +               au_fclr_fillvdir(arg->flags, WHABLE);
32215 +               if (shwh
32216 +                   || (bindex != bbot
32217 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32218 +                       au_fset_fillvdir(arg->flags, WHABLE);
32219 +               do {
32220 +                       arg->err = 0;
32221 +                       au_fclr_fillvdir(arg->flags, CALLED);
32222 +                       /* smp_mb(); */
32223 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32224 +                       if (err >= 0)
32225 +                               err = arg->err;
32226 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32227 +
32228 +               /*
32229 +                * dir_relax() may be good for concurrency, but aufs should not
32230 +                * use it since it will cause a lockdep problem.
32231 +                */
32232 +       }
32233 +
32234 +       if (!err && shwh)
32235 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32236 +
32237 +       au_nhash_wh_free(&arg->whlist);
32238 +
32239 +out_delist:
32240 +       au_nhash_de_free(&arg->delist);
32241 +out:
32242 +       return err;
32243 +}
32244 +
32245 +static int read_vdir(struct file *file, int may_read)
32246 +{
32247 +       int err;
32248 +       unsigned long expire;
32249 +       unsigned char do_read;
32250 +       struct fillvdir_arg arg = {
32251 +               .ctx = {
32252 +                       .actor = fillvdir
32253 +               }
32254 +       };
32255 +       struct inode *inode;
32256 +       struct au_vdir *vdir, *allocated;
32257 +
32258 +       err = 0;
32259 +       inode = file_inode(file);
32260 +       IMustLock(inode);
32261 +       IiMustWriteLock(inode);
32262 +       SiMustAnyLock(inode->i_sb);
32263 +
32264 +       allocated = NULL;
32265 +       do_read = 0;
32266 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32267 +       vdir = au_ivdir(inode);
32268 +       if (!vdir) {
32269 +               do_read = 1;
32270 +               vdir = alloc_vdir(file);
32271 +               err = PTR_ERR(vdir);
32272 +               if (IS_ERR(vdir))
32273 +                       goto out;
32274 +               err = 0;
32275 +               allocated = vdir;
32276 +       } else if (may_read
32277 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32278 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32279 +               do_read = 1;
32280 +               err = reinit_vdir(vdir);
32281 +               if (unlikely(err))
32282 +                       goto out;
32283 +       }
32284 +
32285 +       if (!do_read)
32286 +               return 0; /* success */
32287 +
32288 +       arg.file = file;
32289 +       arg.vdir = vdir;
32290 +       err = au_do_read_vdir(&arg);
32291 +       if (!err) {
32292 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32293 +               vdir->vd_version = inode_query_iversion(inode);
32294 +               vdir->vd_last.ul = 0;
32295 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32296 +               if (allocated)
32297 +                       au_set_ivdir(inode, allocated);
32298 +       } else if (allocated)
32299 +               au_vdir_free(allocated);
32300 +
32301 +out:
32302 +       return err;
32303 +}
32304 +
32305 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32306 +{
32307 +       int err, rerr;
32308 +       unsigned long ul, n;
32309 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32310 +
32311 +       AuDebugOn(tgt->vd_nblk != 1);
32312 +
32313 +       err = -ENOMEM;
32314 +       if (tgt->vd_nblk < src->vd_nblk) {
32315 +               unsigned char **p;
32316 +
32317 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32318 +                               GFP_NOFS, /*may_shrink*/0);
32319 +               if (unlikely(!p))
32320 +                       goto out;
32321 +               tgt->vd_deblk = p;
32322 +       }
32323 +
32324 +       if (tgt->vd_deblk_sz != deblk_sz) {
32325 +               unsigned char *p;
32326 +
32327 +               tgt->vd_deblk_sz = deblk_sz;
32328 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32329 +                               /*may_shrink*/1);
32330 +               if (unlikely(!p))
32331 +                       goto out;
32332 +               tgt->vd_deblk[0] = p;
32333 +       }
32334 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32335 +       tgt->vd_version = src->vd_version;
32336 +       tgt->vd_jiffy = src->vd_jiffy;
32337 +
32338 +       n = src->vd_nblk;
32339 +       for (ul = 1; ul < n; ul++) {
32340 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32341 +                                           GFP_NOFS);
32342 +               if (unlikely(!tgt->vd_deblk[ul]))
32343 +                       goto out;
32344 +               tgt->vd_nblk++;
32345 +       }
32346 +       tgt->vd_nblk = n;
32347 +       tgt->vd_last.ul = tgt->vd_last.ul;
32348 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32349 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32350 +               - src->vd_deblk[src->vd_last.ul];
32351 +       /* smp_mb(); */
32352 +       return 0; /* success */
32353 +
32354 +out:
32355 +       rerr = reinit_vdir(tgt);
32356 +       BUG_ON(rerr);
32357 +       return err;
32358 +}
32359 +
32360 +int au_vdir_init(struct file *file)
32361 +{
32362 +       int err;
32363 +       struct inode *inode;
32364 +       struct au_vdir *vdir_cache, *allocated;
32365 +
32366 +       /* test file->f_pos here instead of ctx->pos */
32367 +       err = read_vdir(file, !file->f_pos);
32368 +       if (unlikely(err))
32369 +               goto out;
32370 +
32371 +       allocated = NULL;
32372 +       vdir_cache = au_fvdir_cache(file);
32373 +       if (!vdir_cache) {
32374 +               vdir_cache = alloc_vdir(file);
32375 +               err = PTR_ERR(vdir_cache);
32376 +               if (IS_ERR(vdir_cache))
32377 +                       goto out;
32378 +               allocated = vdir_cache;
32379 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32380 +               /* test file->f_pos here instead of ctx->pos */
32381 +               err = reinit_vdir(vdir_cache);
32382 +               if (unlikely(err))
32383 +                       goto out;
32384 +       } else
32385 +               return 0; /* success */
32386 +
32387 +       inode = file_inode(file);
32388 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32389 +       if (!err) {
32390 +               file->f_version = inode_query_iversion(inode);
32391 +               if (allocated)
32392 +                       au_set_fvdir_cache(file, allocated);
32393 +       } else if (allocated)
32394 +               au_vdir_free(allocated);
32395 +
32396 +out:
32397 +       return err;
32398 +}
32399 +
32400 +static loff_t calc_offset(struct au_vdir *vdir)
32401 +{
32402 +       loff_t offset;
32403 +       union au_vdir_deblk_p p;
32404 +
32405 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32406 +       offset = vdir->vd_last.p.deblk - p.deblk;
32407 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32408 +       return offset;
32409 +}
32410 +
32411 +/* returns true or false */
32412 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32413 +{
32414 +       int valid;
32415 +       unsigned int deblk_sz;
32416 +       unsigned long ul, n;
32417 +       loff_t offset;
32418 +       union au_vdir_deblk_p p, deblk_end;
32419 +       struct au_vdir *vdir_cache;
32420 +
32421 +       valid = 1;
32422 +       vdir_cache = au_fvdir_cache(file);
32423 +       offset = calc_offset(vdir_cache);
32424 +       AuDbg("offset %lld\n", offset);
32425 +       if (ctx->pos == offset)
32426 +               goto out;
32427 +
32428 +       vdir_cache->vd_last.ul = 0;
32429 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32430 +       if (!ctx->pos)
32431 +               goto out;
32432 +
32433 +       valid = 0;
32434 +       deblk_sz = vdir_cache->vd_deblk_sz;
32435 +       ul = div64_u64(ctx->pos, deblk_sz);
32436 +       AuDbg("ul %lu\n", ul);
32437 +       if (ul >= vdir_cache->vd_nblk)
32438 +               goto out;
32439 +
32440 +       n = vdir_cache->vd_nblk;
32441 +       for (; ul < n; ul++) {
32442 +               p.deblk = vdir_cache->vd_deblk[ul];
32443 +               deblk_end.deblk = p.deblk + deblk_sz;
32444 +               offset = ul;
32445 +               offset *= deblk_sz;
32446 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32447 +                       unsigned int l;
32448 +
32449 +                       l = calc_size(p.de->de_str.len);
32450 +                       offset += l;
32451 +                       p.deblk += l;
32452 +               }
32453 +               if (!is_deblk_end(&p, &deblk_end)) {
32454 +                       valid = 1;
32455 +                       vdir_cache->vd_last.ul = ul;
32456 +                       vdir_cache->vd_last.p = p;
32457 +                       break;
32458 +               }
32459 +       }
32460 +
32461 +out:
32462 +       /* smp_mb(); */
32463 +       if (!valid)
32464 +               AuDbg("valid %d\n", !valid);
32465 +       return valid;
32466 +}
32467 +
32468 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32469 +{
32470 +       unsigned int l, deblk_sz;
32471 +       union au_vdir_deblk_p deblk_end;
32472 +       struct au_vdir *vdir_cache;
32473 +       struct au_vdir_de *de;
32474 +
32475 +       vdir_cache = au_fvdir_cache(file);
32476 +       if (!seek_vdir(file, ctx))
32477 +               return 0;
32478 +
32479 +       deblk_sz = vdir_cache->vd_deblk_sz;
32480 +       while (1) {
32481 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32482 +               deblk_end.deblk += deblk_sz;
32483 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32484 +                       de = vdir_cache->vd_last.p.de;
32485 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32486 +                             de->de_str.len, de->de_str.name, ctx->pos,
32487 +                             (unsigned long)de->de_ino, de->de_type);
32488 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32489 +                                              de->de_str.len, de->de_ino,
32490 +                                              de->de_type))) {
32491 +                               /* todo: ignore the error caused by udba? */
32492 +                               /* return err; */
32493 +                               return 0;
32494 +                       }
32495 +
32496 +                       l = calc_size(de->de_str.len);
32497 +                       vdir_cache->vd_last.p.deblk += l;
32498 +                       ctx->pos += l;
32499 +               }
32500 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32501 +                       vdir_cache->vd_last.ul++;
32502 +                       vdir_cache->vd_last.p.deblk
32503 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32504 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32505 +                       continue;
32506 +               }
32507 +               break;
32508 +       }
32509 +
32510 +       /* smp_mb(); */
32511 +       return 0;
32512 +}
32513 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32514 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32515 +++ linux/fs/aufs/vfsub.c       2018-07-19 09:46:13.063314675 +0200
32516 @@ -0,0 +1,895 @@
32517 +/* SPDX-License-Identifier: GPL-2.0 */
32518 +/*
32519 + * Copyright (C) 2005-2018 Junjiro R. Okajima
32520 + *
32521 + * This program, aufs is free software; you can redistribute it and/or modify
32522 + * it under the terms of the GNU General Public License as published by
32523 + * the Free Software Foundation; either version 2 of the License, or
32524 + * (at your option) any later version.
32525 + *
32526 + * This program is distributed in the hope that it will be useful,
32527 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32528 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32529 + * GNU General Public License for more details.
32530 + *
32531 + * You should have received a copy of the GNU General Public License
32532 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32533 + */
32534 +
32535 +/*
32536 + * sub-routines for VFS
32537 + */
32538 +
32539 +#include <linux/mnt_namespace.h>
32540 +#include <linux/namei.h>
32541 +#include <linux/nsproxy.h>
32542 +#include <linux/security.h>
32543 +#include <linux/splice.h>
32544 +#include "aufs.h"
32545 +
32546 +#ifdef CONFIG_AUFS_BR_FUSE
32547 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32548 +{
32549 +       if (!au_test_fuse(h_sb) || !au_userns)
32550 +               return 0;
32551 +
32552 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32553 +}
32554 +#endif
32555 +
32556 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32557 +{
32558 +       int err;
32559 +
32560 +       lockdep_off();
32561 +       down_read(&h_sb->s_umount);
32562 +       err = __sync_filesystem(h_sb, wait);
32563 +       up_read(&h_sb->s_umount);
32564 +       lockdep_on();
32565 +
32566 +       return err;
32567 +}
32568 +
32569 +/* ---------------------------------------------------------------------- */
32570 +
32571 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32572 +{
32573 +       int err;
32574 +       struct kstat st;
32575 +       struct super_block *h_sb;
32576 +
32577 +       /* for remote fs, leave work for its getattr or d_revalidate */
32578 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32579 +       /* still some fs may acquire i_mutex. we need to skip them */
32580 +       err = 0;
32581 +       if (!did)
32582 +               did = &err;
32583 +       h_sb = h_path->dentry->d_sb;
32584 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32585 +       if (*did)
32586 +               err = vfsub_getattr(h_path, &st);
32587 +
32588 +       return err;
32589 +}
32590 +
32591 +/* ---------------------------------------------------------------------- */
32592 +
32593 +struct file *vfsub_dentry_open(struct path *path, int flags)
32594 +{
32595 +       struct file *file;
32596 +
32597 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32598 +                          current_cred());
32599 +       if (!IS_ERR_OR_NULL(file)
32600 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
32601 +               i_readcount_inc(d_inode(path->dentry));
32602 +
32603 +       return file;
32604 +}
32605 +
32606 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32607 +{
32608 +       struct file *file;
32609 +
32610 +       lockdep_off();
32611 +       file = filp_open(path,
32612 +                        oflags /* | __FMODE_NONOTIFY */,
32613 +                        mode);
32614 +       lockdep_on();
32615 +       if (IS_ERR(file))
32616 +               goto out;
32617 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32618 +
32619 +out:
32620 +       return file;
32621 +}
32622 +
32623 +/*
32624 + * Ideally this function should call VFS:do_last() in order to keep all its
32625 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32626 + * structure such as nameidata. This is a second (or third) best approach.
32627 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32628 + */
32629 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32630 +                     struct vfsub_aopen_args *args, struct au_branch *br)
32631 +{
32632 +       int err;
32633 +       struct file *file = args->file;
32634 +       /* copied from linux/fs/namei.c:atomic_open() */
32635 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32636 +
32637 +       IMustLock(dir);
32638 +       AuDebugOn(!dir->i_op->atomic_open);
32639 +
32640 +       err = au_br_test_oflag(args->open_flag, br);
32641 +       if (unlikely(err))
32642 +               goto out;
32643 +
32644 +       args->file->f_path.dentry = DENTRY_NOT_SET;
32645 +       args->file->f_path.mnt = au_br_mnt(br);
32646 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32647 +                                    args->create_mode, args->opened);
32648 +       if (err >= 0) {
32649 +               /* some filesystems don't set FILE_CREATED while succeeded? */
32650 +               if (*args->opened & FILE_CREATED)
32651 +                       fsnotify_create(dir, dentry);
32652 +       } else
32653 +               goto out;
32654 +
32655 +
32656 +       if (!err) {
32657 +               /* todo: call VFS:may_open() here */
32658 +               err = open_check_o_direct(file);
32659 +               /* todo: ima_file_check() too? */
32660 +               if (!err && (args->open_flag & __FMODE_EXEC))
32661 +                       err = deny_write_access(file);
32662 +               if (unlikely(err))
32663 +                       /* note that the file is created and still opened */
32664 +                       goto out;
32665 +       }
32666 +
32667 +       au_br_get(br);
32668 +       fsnotify_open(file);
32669 +
32670 +out:
32671 +       return err;
32672 +}
32673 +
32674 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32675 +{
32676 +       int err;
32677 +
32678 +       err = kern_path(name, flags, path);
32679 +       if (!err && d_is_positive(path->dentry))
32680 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32681 +       return err;
32682 +}
32683 +
32684 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32685 +                                            struct dentry *parent, int len)
32686 +{
32687 +       struct path path = {
32688 +               .mnt = NULL
32689 +       };
32690 +
32691 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32692 +       if (IS_ERR(path.dentry))
32693 +               goto out;
32694 +       if (d_is_positive(path.dentry))
32695 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32696 +
32697 +out:
32698 +       AuTraceErrPtr(path.dentry);
32699 +       return path.dentry;
32700 +}
32701 +
32702 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32703 +                                   int len)
32704 +{
32705 +       struct path path = {
32706 +               .mnt = NULL
32707 +       };
32708 +
32709 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32710 +       IMustLock(d_inode(parent));
32711 +
32712 +       path.dentry = lookup_one_len(name, parent, len);
32713 +       if (IS_ERR(path.dentry))
32714 +               goto out;
32715 +       if (d_is_positive(path.dentry))
32716 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32717 +
32718 +out:
32719 +       AuTraceErrPtr(path.dentry);
32720 +       return path.dentry;
32721 +}
32722 +
32723 +void vfsub_call_lkup_one(void *args)
32724 +{
32725 +       struct vfsub_lkup_one_args *a = args;
32726 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32727 +}
32728 +
32729 +/* ---------------------------------------------------------------------- */
32730 +
32731 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32732 +                                struct dentry *d2, struct au_hinode *hdir2)
32733 +{
32734 +       struct dentry *d;
32735 +
32736 +       lockdep_off();
32737 +       d = lock_rename(d1, d2);
32738 +       lockdep_on();
32739 +       au_hn_suspend(hdir1);
32740 +       if (hdir1 != hdir2)
32741 +               au_hn_suspend(hdir2);
32742 +
32743 +       return d;
32744 +}
32745 +
32746 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32747 +                        struct dentry *d2, struct au_hinode *hdir2)
32748 +{
32749 +       au_hn_resume(hdir1);
32750 +       if (hdir1 != hdir2)
32751 +               au_hn_resume(hdir2);
32752 +       lockdep_off();
32753 +       unlock_rename(d1, d2);
32754 +       lockdep_on();
32755 +}
32756 +
32757 +/* ---------------------------------------------------------------------- */
32758 +
32759 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32760 +{
32761 +       int err;
32762 +       struct dentry *d;
32763 +
32764 +       IMustLock(dir);
32765 +
32766 +       d = path->dentry;
32767 +       path->dentry = d->d_parent;
32768 +       err = security_path_mknod(path, d, mode, 0);
32769 +       path->dentry = d;
32770 +       if (unlikely(err))
32771 +               goto out;
32772 +
32773 +       lockdep_off();
32774 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32775 +       lockdep_on();
32776 +       if (!err) {
32777 +               struct path tmp = *path;
32778 +               int did;
32779 +
32780 +               vfsub_update_h_iattr(&tmp, &did);
32781 +               if (did) {
32782 +                       tmp.dentry = path->dentry->d_parent;
32783 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32784 +               }
32785 +               /*ignore*/
32786 +       }
32787 +
32788 +out:
32789 +       return err;
32790 +}
32791 +
32792 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32793 +{
32794 +       int err;
32795 +       struct dentry *d;
32796 +
32797 +       IMustLock(dir);
32798 +
32799 +       d = path->dentry;
32800 +       path->dentry = d->d_parent;
32801 +       err = security_path_symlink(path, d, symname);
32802 +       path->dentry = d;
32803 +       if (unlikely(err))
32804 +               goto out;
32805 +
32806 +       lockdep_off();
32807 +       err = vfs_symlink(dir, path->dentry, symname);
32808 +       lockdep_on();
32809 +       if (!err) {
32810 +               struct path tmp = *path;
32811 +               int did;
32812 +
32813 +               vfsub_update_h_iattr(&tmp, &did);
32814 +               if (did) {
32815 +                       tmp.dentry = path->dentry->d_parent;
32816 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32817 +               }
32818 +               /*ignore*/
32819 +       }
32820 +
32821 +out:
32822 +       return err;
32823 +}
32824 +
32825 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32826 +{
32827 +       int err;
32828 +       struct dentry *d;
32829 +
32830 +       IMustLock(dir);
32831 +
32832 +       d = path->dentry;
32833 +       path->dentry = d->d_parent;
32834 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32835 +       path->dentry = d;
32836 +       if (unlikely(err))
32837 +               goto out;
32838 +
32839 +       lockdep_off();
32840 +       err = vfs_mknod(dir, path->dentry, mode, dev);
32841 +       lockdep_on();
32842 +       if (!err) {
32843 +               struct path tmp = *path;
32844 +               int did;
32845 +
32846 +               vfsub_update_h_iattr(&tmp, &did);
32847 +               if (did) {
32848 +                       tmp.dentry = path->dentry->d_parent;
32849 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32850 +               }
32851 +               /*ignore*/
32852 +       }
32853 +
32854 +out:
32855 +       return err;
32856 +}
32857 +
32858 +static int au_test_nlink(struct inode *inode)
32859 +{
32860 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32861 +
32862 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
32863 +           || inode->i_nlink < link_max)
32864 +               return 0;
32865 +       return -EMLINK;
32866 +}
32867 +
32868 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32869 +              struct inode **delegated_inode)
32870 +{
32871 +       int err;
32872 +       struct dentry *d;
32873 +
32874 +       IMustLock(dir);
32875 +
32876 +       err = au_test_nlink(d_inode(src_dentry));
32877 +       if (unlikely(err))
32878 +               return err;
32879 +
32880 +       /* we don't call may_linkat() */
32881 +       d = path->dentry;
32882 +       path->dentry = d->d_parent;
32883 +       err = security_path_link(src_dentry, path, d);
32884 +       path->dentry = d;
32885 +       if (unlikely(err))
32886 +               goto out;
32887 +
32888 +       lockdep_off();
32889 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
32890 +       lockdep_on();
32891 +       if (!err) {
32892 +               struct path tmp = *path;
32893 +               int did;
32894 +
32895 +               /* fuse has different memory inode for the same inumber */
32896 +               vfsub_update_h_iattr(&tmp, &did);
32897 +               if (did) {
32898 +                       tmp.dentry = path->dentry->d_parent;
32899 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32900 +                       tmp.dentry = src_dentry;
32901 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32902 +               }
32903 +               /*ignore*/
32904 +       }
32905 +
32906 +out:
32907 +       return err;
32908 +}
32909 +
32910 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32911 +                struct inode *dir, struct path *path,
32912 +                struct inode **delegated_inode, unsigned int flags)
32913 +{
32914 +       int err;
32915 +       struct path tmp = {
32916 +               .mnt    = path->mnt
32917 +       };
32918 +       struct dentry *d;
32919 +
32920 +       IMustLock(dir);
32921 +       IMustLock(src_dir);
32922 +
32923 +       d = path->dentry;
32924 +       path->dentry = d->d_parent;
32925 +       tmp.dentry = src_dentry->d_parent;
32926 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32927 +       path->dentry = d;
32928 +       if (unlikely(err))
32929 +               goto out;
32930 +
32931 +       lockdep_off();
32932 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
32933 +                        delegated_inode, flags);
32934 +       lockdep_on();
32935 +       if (!err) {
32936 +               int did;
32937 +
32938 +               tmp.dentry = d->d_parent;
32939 +               vfsub_update_h_iattr(&tmp, &did);
32940 +               if (did) {
32941 +                       tmp.dentry = src_dentry;
32942 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32943 +                       tmp.dentry = src_dentry->d_parent;
32944 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32945 +               }
32946 +               /*ignore*/
32947 +       }
32948 +
32949 +out:
32950 +       return err;
32951 +}
32952 +
32953 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
32954 +{
32955 +       int err;
32956 +       struct dentry *d;
32957 +
32958 +       IMustLock(dir);
32959 +
32960 +       d = path->dentry;
32961 +       path->dentry = d->d_parent;
32962 +       err = security_path_mkdir(path, d, mode);
32963 +       path->dentry = d;
32964 +       if (unlikely(err))
32965 +               goto out;
32966 +
32967 +       lockdep_off();
32968 +       err = vfs_mkdir(dir, path->dentry, mode);
32969 +       lockdep_on();
32970 +       if (!err) {
32971 +               struct path tmp = *path;
32972 +               int did;
32973 +
32974 +               vfsub_update_h_iattr(&tmp, &did);
32975 +               if (did) {
32976 +                       tmp.dentry = path->dentry->d_parent;
32977 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32978 +               }
32979 +               /*ignore*/
32980 +       }
32981 +
32982 +out:
32983 +       return err;
32984 +}
32985 +
32986 +int vfsub_rmdir(struct inode *dir, struct path *path)
32987 +{
32988 +       int err;
32989 +       struct dentry *d;
32990 +
32991 +       IMustLock(dir);
32992 +
32993 +       d = path->dentry;
32994 +       path->dentry = d->d_parent;
32995 +       err = security_path_rmdir(path, d);
32996 +       path->dentry = d;
32997 +       if (unlikely(err))
32998 +               goto out;
32999 +
33000 +       lockdep_off();
33001 +       err = vfs_rmdir(dir, path->dentry);
33002 +       lockdep_on();
33003 +       if (!err) {
33004 +               struct path tmp = {
33005 +                       .dentry = path->dentry->d_parent,
33006 +                       .mnt    = path->mnt
33007 +               };
33008 +
33009 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33010 +       }
33011 +
33012 +out:
33013 +       return err;
33014 +}
33015 +
33016 +/* ---------------------------------------------------------------------- */
33017 +
33018 +/* todo: support mmap_sem? */
33019 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33020 +                    loff_t *ppos)
33021 +{
33022 +       ssize_t err;
33023 +
33024 +       lockdep_off();
33025 +       err = vfs_read(file, ubuf, count, ppos);
33026 +       lockdep_on();
33027 +       if (err >= 0)
33028 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33029 +       return err;
33030 +}
33031 +
33032 +/* todo: kernel_read()? */
33033 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33034 +                    loff_t *ppos)
33035 +{
33036 +       ssize_t err;
33037 +       mm_segment_t oldfs;
33038 +       union {
33039 +               void *k;
33040 +               char __user *u;
33041 +       } buf;
33042 +
33043 +       buf.k = kbuf;
33044 +       oldfs = get_fs();
33045 +       set_fs(KERNEL_DS);
33046 +       err = vfsub_read_u(file, buf.u, count, ppos);
33047 +       set_fs(oldfs);
33048 +       return err;
33049 +}
33050 +
33051 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33052 +                     loff_t *ppos)
33053 +{
33054 +       ssize_t err;
33055 +
33056 +       lockdep_off();
33057 +       err = vfs_write(file, ubuf, count, ppos);
33058 +       lockdep_on();
33059 +       if (err >= 0)
33060 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33061 +       return err;
33062 +}
33063 +
33064 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33065 +{
33066 +       ssize_t err;
33067 +       mm_segment_t oldfs;
33068 +       union {
33069 +               void *k;
33070 +               const char __user *u;
33071 +       } buf;
33072 +
33073 +       buf.k = kbuf;
33074 +       oldfs = get_fs();
33075 +       set_fs(KERNEL_DS);
33076 +       err = vfsub_write_u(file, buf.u, count, ppos);
33077 +       set_fs(oldfs);
33078 +       return err;
33079 +}
33080 +
33081 +int vfsub_flush(struct file *file, fl_owner_t id)
33082 +{
33083 +       int err;
33084 +
33085 +       err = 0;
33086 +       if (file->f_op->flush) {
33087 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33088 +                       err = file->f_op->flush(file, id);
33089 +               else {
33090 +                       lockdep_off();
33091 +                       err = file->f_op->flush(file, id);
33092 +                       lockdep_on();
33093 +               }
33094 +               if (!err)
33095 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33096 +               /*ignore*/
33097 +       }
33098 +       return err;
33099 +}
33100 +
33101 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33102 +{
33103 +       int err;
33104 +
33105 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33106 +
33107 +       lockdep_off();
33108 +       err = iterate_dir(file, ctx);
33109 +       lockdep_on();
33110 +       if (err >= 0)
33111 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33112 +
33113 +       return err;
33114 +}
33115 +
33116 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33117 +                    struct pipe_inode_info *pipe, size_t len,
33118 +                    unsigned int flags)
33119 +{
33120 +       long err;
33121 +
33122 +       lockdep_off();
33123 +       err = do_splice_to(in, ppos, pipe, len, flags);
33124 +       lockdep_on();
33125 +       file_accessed(in);
33126 +       if (err >= 0)
33127 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33128 +       return err;
33129 +}
33130 +
33131 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33132 +                      loff_t *ppos, size_t len, unsigned int flags)
33133 +{
33134 +       long err;
33135 +
33136 +       lockdep_off();
33137 +       err = do_splice_from(pipe, out, ppos, len, flags);
33138 +       lockdep_on();
33139 +       if (err >= 0)
33140 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33141 +       return err;
33142 +}
33143 +
33144 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33145 +{
33146 +       int err;
33147 +
33148 +       /* file can be NULL */
33149 +       lockdep_off();
33150 +       err = vfs_fsync(file, datasync);
33151 +       lockdep_on();
33152 +       if (!err) {
33153 +               if (!path) {
33154 +                       AuDebugOn(!file);
33155 +                       path = &file->f_path;
33156 +               }
33157 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33158 +       }
33159 +       return err;
33160 +}
33161 +
33162 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33163 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33164 +               struct file *h_file)
33165 +{
33166 +       int err;
33167 +       struct inode *h_inode;
33168 +       struct super_block *h_sb;
33169 +
33170 +       if (!h_file) {
33171 +               err = vfsub_truncate(h_path, length);
33172 +               goto out;
33173 +       }
33174 +
33175 +       h_inode = d_inode(h_path->dentry);
33176 +       h_sb = h_inode->i_sb;
33177 +       lockdep_off();
33178 +       sb_start_write(h_sb);
33179 +       lockdep_on();
33180 +       err = locks_verify_truncate(h_inode, h_file, length);
33181 +       if (!err)
33182 +               err = security_path_truncate(h_path);
33183 +       if (!err) {
33184 +               lockdep_off();
33185 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33186 +               lockdep_on();
33187 +       }
33188 +       lockdep_off();
33189 +       sb_end_write(h_sb);
33190 +       lockdep_on();
33191 +
33192 +out:
33193 +       return err;
33194 +}
33195 +
33196 +/* ---------------------------------------------------------------------- */
33197 +
33198 +struct au_vfsub_mkdir_args {
33199 +       int *errp;
33200 +       struct inode *dir;
33201 +       struct path *path;
33202 +       int mode;
33203 +};
33204 +
33205 +static void au_call_vfsub_mkdir(void *args)
33206 +{
33207 +       struct au_vfsub_mkdir_args *a = args;
33208 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33209 +}
33210 +
33211 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33212 +{
33213 +       int err, do_sio, wkq_err;
33214 +
33215 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33216 +       if (!do_sio) {
33217 +               lockdep_off();
33218 +               err = vfsub_mkdir(dir, path, mode);
33219 +               lockdep_on();
33220 +       } else {
33221 +               struct au_vfsub_mkdir_args args = {
33222 +                       .errp   = &err,
33223 +                       .dir    = dir,
33224 +                       .path   = path,
33225 +                       .mode   = mode
33226 +               };
33227 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33228 +               if (unlikely(wkq_err))
33229 +                       err = wkq_err;
33230 +       }
33231 +
33232 +       return err;
33233 +}
33234 +
33235 +struct au_vfsub_rmdir_args {
33236 +       int *errp;
33237 +       struct inode *dir;
33238 +       struct path *path;
33239 +};
33240 +
33241 +static void au_call_vfsub_rmdir(void *args)
33242 +{
33243 +       struct au_vfsub_rmdir_args *a = args;
33244 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33245 +}
33246 +
33247 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33248 +{
33249 +       int err, do_sio, wkq_err;
33250 +
33251 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33252 +       if (!do_sio) {
33253 +               lockdep_off();
33254 +               err = vfsub_rmdir(dir, path);
33255 +               lockdep_on();
33256 +       } else {
33257 +               struct au_vfsub_rmdir_args args = {
33258 +                       .errp   = &err,
33259 +                       .dir    = dir,
33260 +                       .path   = path
33261 +               };
33262 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33263 +               if (unlikely(wkq_err))
33264 +                       err = wkq_err;
33265 +       }
33266 +
33267 +       return err;
33268 +}
33269 +
33270 +/* ---------------------------------------------------------------------- */
33271 +
33272 +struct notify_change_args {
33273 +       int *errp;
33274 +       struct path *path;
33275 +       struct iattr *ia;
33276 +       struct inode **delegated_inode;
33277 +};
33278 +
33279 +static void call_notify_change(void *args)
33280 +{
33281 +       struct notify_change_args *a = args;
33282 +       struct inode *h_inode;
33283 +
33284 +       h_inode = d_inode(a->path->dentry);
33285 +       IMustLock(h_inode);
33286 +
33287 +       *a->errp = -EPERM;
33288 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33289 +               lockdep_off();
33290 +               *a->errp = notify_change(a->path->dentry, a->ia,
33291 +                                        a->delegated_inode);
33292 +               lockdep_on();
33293 +               if (!*a->errp)
33294 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33295 +       }
33296 +       AuTraceErr(*a->errp);
33297 +}
33298 +
33299 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33300 +                       struct inode **delegated_inode)
33301 +{
33302 +       int err;
33303 +       struct notify_change_args args = {
33304 +               .errp                   = &err,
33305 +               .path                   = path,
33306 +               .ia                     = ia,
33307 +               .delegated_inode        = delegated_inode
33308 +       };
33309 +
33310 +       call_notify_change(&args);
33311 +
33312 +       return err;
33313 +}
33314 +
33315 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33316 +                           struct inode **delegated_inode)
33317 +{
33318 +       int err, wkq_err;
33319 +       struct notify_change_args args = {
33320 +               .errp                   = &err,
33321 +               .path                   = path,
33322 +               .ia                     = ia,
33323 +               .delegated_inode        = delegated_inode
33324 +       };
33325 +
33326 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33327 +       if (unlikely(wkq_err))
33328 +               err = wkq_err;
33329 +
33330 +       return err;
33331 +}
33332 +
33333 +/* ---------------------------------------------------------------------- */
33334 +
33335 +struct unlink_args {
33336 +       int *errp;
33337 +       struct inode *dir;
33338 +       struct path *path;
33339 +       struct inode **delegated_inode;
33340 +};
33341 +
33342 +static void call_unlink(void *args)
33343 +{
33344 +       struct unlink_args *a = args;
33345 +       struct dentry *d = a->path->dentry;
33346 +       struct inode *h_inode;
33347 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33348 +                                     && au_dcount(d) == 1);
33349 +
33350 +       IMustLock(a->dir);
33351 +
33352 +       a->path->dentry = d->d_parent;
33353 +       *a->errp = security_path_unlink(a->path, d);
33354 +       a->path->dentry = d;
33355 +       if (unlikely(*a->errp))
33356 +               return;
33357 +
33358 +       if (!stop_sillyrename)
33359 +               dget(d);
33360 +       h_inode = NULL;
33361 +       if (d_is_positive(d)) {
33362 +               h_inode = d_inode(d);
33363 +               ihold(h_inode);
33364 +       }
33365 +
33366 +       lockdep_off();
33367 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33368 +       lockdep_on();
33369 +       if (!*a->errp) {
33370 +               struct path tmp = {
33371 +                       .dentry = d->d_parent,
33372 +                       .mnt    = a->path->mnt
33373 +               };
33374 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33375 +       }
33376 +
33377 +       if (!stop_sillyrename)
33378 +               dput(d);
33379 +       if (h_inode)
33380 +               iput(h_inode);
33381 +
33382 +       AuTraceErr(*a->errp);
33383 +}
33384 +
33385 +/*
33386 + * @dir: must be locked.
33387 + * @dentry: target dentry.
33388 + */
33389 +int vfsub_unlink(struct inode *dir, struct path *path,
33390 +                struct inode **delegated_inode, int force)
33391 +{
33392 +       int err;
33393 +       struct unlink_args args = {
33394 +               .errp                   = &err,
33395 +               .dir                    = dir,
33396 +               .path                   = path,
33397 +               .delegated_inode        = delegated_inode
33398 +       };
33399 +
33400 +       if (!force)
33401 +               call_unlink(&args);
33402 +       else {
33403 +               int wkq_err;
33404 +
33405 +               wkq_err = au_wkq_wait(call_unlink, &args);
33406 +               if (unlikely(wkq_err))
33407 +                       err = wkq_err;
33408 +       }
33409 +
33410 +       return err;
33411 +}
33412 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33413 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33414 +++ linux/fs/aufs/vfsub.h       2018-07-19 09:46:13.063314675 +0200
33415 @@ -0,0 +1,355 @@
33416 +/* SPDX-License-Identifier: GPL-2.0 */
33417 +/*
33418 + * Copyright (C) 2005-2018 Junjiro R. Okajima
33419 + *
33420 + * This program, aufs is free software; you can redistribute it and/or modify
33421 + * it under the terms of the GNU General Public License as published by
33422 + * the Free Software Foundation; either version 2 of the License, or
33423 + * (at your option) any later version.
33424 + *
33425 + * This program is distributed in the hope that it will be useful,
33426 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33427 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33428 + * GNU General Public License for more details.
33429 + *
33430 + * You should have received a copy of the GNU General Public License
33431 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33432 + */
33433 +
33434 +/*
33435 + * sub-routines for VFS
33436 + */
33437 +
33438 +#ifndef __AUFS_VFSUB_H__
33439 +#define __AUFS_VFSUB_H__
33440 +
33441 +#ifdef __KERNEL__
33442 +
33443 +#include <linux/fs.h>
33444 +#include <linux/iversion.h>
33445 +#include <linux/mount.h>
33446 +#include <linux/posix_acl.h>
33447 +#include <linux/xattr.h>
33448 +#include "debug.h"
33449 +
33450 +/* copied from linux/fs/internal.h */
33451 +/* todo: BAD approach!! */
33452 +extern void __mnt_drop_write(struct vfsmount *);
33453 +extern int open_check_o_direct(struct file *f);
33454 +
33455 +/* ---------------------------------------------------------------------- */
33456 +
33457 +/* lock subclass for lower inode */
33458 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33459 +/* reduce? gave up. */
33460 +enum {
33461 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33462 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33463 +       AuLsc_I_PARENT2,        /* copyup dirs */
33464 +       AuLsc_I_PARENT3,        /* copyup wh */
33465 +       AuLsc_I_CHILD,
33466 +       AuLsc_I_CHILD2,
33467 +       AuLsc_I_End
33468 +};
33469 +
33470 +/* to debug easier, do not make them inlined functions */
33471 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33472 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33473 +
33474 +/* ---------------------------------------------------------------------- */
33475 +
33476 +static inline void vfsub_drop_nlink(struct inode *inode)
33477 +{
33478 +       AuDebugOn(!inode->i_nlink);
33479 +       drop_nlink(inode);
33480 +}
33481 +
33482 +static inline void vfsub_dead_dir(struct inode *inode)
33483 +{
33484 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33485 +       inode->i_flags |= S_DEAD;
33486 +       clear_nlink(inode);
33487 +}
33488 +
33489 +static inline int vfsub_native_ro(struct inode *inode)
33490 +{
33491 +       return sb_rdonly(inode->i_sb)
33492 +               || IS_RDONLY(inode)
33493 +               /* || IS_APPEND(inode) */
33494 +               || IS_IMMUTABLE(inode);
33495 +}
33496 +
33497 +#ifdef CONFIG_AUFS_BR_FUSE
33498 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33499 +#else
33500 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33501 +#endif
33502 +
33503 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33504 +
33505 +/* ---------------------------------------------------------------------- */
33506 +
33507 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33508 +struct file *vfsub_dentry_open(struct path *path, int flags);
33509 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33510 +struct vfsub_aopen_args {
33511 +       struct file     *file;
33512 +       unsigned int    open_flag;
33513 +       umode_t         create_mode;
33514 +       int             *opened;
33515 +};
33516 +struct au_branch;
33517 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33518 +                     struct vfsub_aopen_args *args, struct au_branch *br);
33519 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33520 +
33521 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33522 +                                            struct dentry *parent, int len);
33523 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33524 +                                   int len);
33525 +
33526 +struct vfsub_lkup_one_args {
33527 +       struct dentry **errp;
33528 +       struct qstr *name;
33529 +       struct dentry *parent;
33530 +};
33531 +
33532 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33533 +                                           struct dentry *parent)
33534 +{
33535 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33536 +}
33537 +
33538 +void vfsub_call_lkup_one(void *args);
33539 +
33540 +/* ---------------------------------------------------------------------- */
33541 +
33542 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33543 +{
33544 +       int err;
33545 +
33546 +       lockdep_off();
33547 +       err = mnt_want_write(mnt);
33548 +       lockdep_on();
33549 +       return err;
33550 +}
33551 +
33552 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33553 +{
33554 +       lockdep_off();
33555 +       mnt_drop_write(mnt);
33556 +       lockdep_on();
33557 +}
33558 +
33559 +#if 0 /* reserved */
33560 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33561 +{
33562 +       lockdep_off();
33563 +       mnt_drop_write_file(file);
33564 +       lockdep_on();
33565 +}
33566 +#endif
33567 +
33568 +/* ---------------------------------------------------------------------- */
33569 +
33570 +struct au_hinode;
33571 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33572 +                                struct dentry *d2, struct au_hinode *hdir2);
33573 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33574 +                        struct dentry *d2, struct au_hinode *hdir2);
33575 +
33576 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33577 +                bool want_excl);
33578 +int vfsub_symlink(struct inode *dir, struct path *path,
33579 +                 const char *symname);
33580 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33581 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33582 +              struct path *path, struct inode **delegated_inode);
33583 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33584 +                struct inode *hdir, struct path *path,
33585 +                struct inode **delegated_inode, unsigned int flags);
33586 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33587 +int vfsub_rmdir(struct inode *dir, struct path *path);
33588 +
33589 +/* ---------------------------------------------------------------------- */
33590 +
33591 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33592 +                    loff_t *ppos);
33593 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33594 +                       loff_t *ppos);
33595 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33596 +                     loff_t *ppos);
33597 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33598 +                     loff_t *ppos);
33599 +int vfsub_flush(struct file *file, fl_owner_t id);
33600 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33601 +
33602 +static inline loff_t vfsub_f_size_read(struct file *file)
33603 +{
33604 +       return i_size_read(file_inode(file));
33605 +}
33606 +
33607 +static inline unsigned int vfsub_file_flags(struct file *file)
33608 +{
33609 +       unsigned int flags;
33610 +
33611 +       spin_lock(&file->f_lock);
33612 +       flags = file->f_flags;
33613 +       spin_unlock(&file->f_lock);
33614 +
33615 +       return flags;
33616 +}
33617 +
33618 +static inline int vfsub_file_execed(struct file *file)
33619 +{
33620 +       /* todo: direct access f_flags */
33621 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33622 +}
33623 +
33624 +#if 0 /* reserved */
33625 +static inline void vfsub_file_accessed(struct file *h_file)
33626 +{
33627 +       file_accessed(h_file);
33628 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33629 +}
33630 +#endif
33631 +
33632 +#if 0 /* reserved */
33633 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33634 +                                    struct dentry *h_dentry)
33635 +{
33636 +       struct path h_path = {
33637 +               .dentry = h_dentry,
33638 +               .mnt    = h_mnt
33639 +       };
33640 +       touch_atime(&h_path);
33641 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33642 +}
33643 +#endif
33644 +
33645 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
33646 +                                   int flags)
33647 +{
33648 +       return update_time(h_inode, ts, flags);
33649 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33650 +}
33651 +
33652 +#ifdef CONFIG_FS_POSIX_ACL
33653 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33654 +{
33655 +       int err;
33656 +
33657 +       err = posix_acl_chmod(h_inode, h_mode);
33658 +       if (err == -EOPNOTSUPP)
33659 +               err = 0;
33660 +       return err;
33661 +}
33662 +#else
33663 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33664 +#endif
33665 +
33666 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33667 +                    struct pipe_inode_info *pipe, size_t len,
33668 +                    unsigned int flags);
33669 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33670 +                      loff_t *ppos, size_t len, unsigned int flags);
33671 +
33672 +static inline long vfsub_truncate(struct path *path, loff_t length)
33673 +{
33674 +       long err;
33675 +
33676 +       lockdep_off();
33677 +       err = vfs_truncate(path, length);
33678 +       lockdep_on();
33679 +       return err;
33680 +}
33681 +
33682 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33683 +               struct file *h_file);
33684 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33685 +
33686 +/*
33687 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33688 + * ioctl.
33689 + */
33690 +static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
33691 +                                        u64 len)
33692 +{
33693 +       int err;
33694 +
33695 +       lockdep_off();
33696 +       err = vfs_clone_file_range(src, 0, dst, 0, len);
33697 +       lockdep_on();
33698 +
33699 +       return err;
33700 +}
33701 +
33702 +/* copy_file_range(2) is a systemcall */
33703 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33704 +                                           struct file *dst, loff_t dst_pos,
33705 +                                           size_t len, unsigned int flags)
33706 +{
33707 +       ssize_t ssz;
33708 +
33709 +       lockdep_off();
33710 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33711 +       lockdep_on();
33712 +
33713 +       return ssz;
33714 +}
33715 +
33716 +/* ---------------------------------------------------------------------- */
33717 +
33718 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33719 +{
33720 +       loff_t err;
33721 +
33722 +       lockdep_off();
33723 +       err = vfs_llseek(file, offset, origin);
33724 +       lockdep_on();
33725 +       return err;
33726 +}
33727 +
33728 +/* ---------------------------------------------------------------------- */
33729 +
33730 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33731 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33732 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33733 +                           struct inode **delegated_inode);
33734 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33735 +                       struct inode **delegated_inode);
33736 +int vfsub_unlink(struct inode *dir, struct path *path,
33737 +                struct inode **delegated_inode, int force);
33738 +
33739 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33740 +{
33741 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33742 +}
33743 +
33744 +/* ---------------------------------------------------------------------- */
33745 +
33746 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33747 +                                const void *value, size_t size, int flags)
33748 +{
33749 +       int err;
33750 +
33751 +       lockdep_off();
33752 +       err = vfs_setxattr(dentry, name, value, size, flags);
33753 +       lockdep_on();
33754 +
33755 +       return err;
33756 +}
33757 +
33758 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33759 +{
33760 +       int err;
33761 +
33762 +       lockdep_off();
33763 +       err = vfs_removexattr(dentry, name);
33764 +       lockdep_on();
33765 +
33766 +       return err;
33767 +}
33768 +
33769 +#endif /* __KERNEL__ */
33770 +#endif /* __AUFS_VFSUB_H__ */
33771 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33772 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33773 +++ linux/fs/aufs/wbr_policy.c  2018-07-19 09:46:13.063314675 +0200
33774 @@ -0,0 +1,831 @@
33775 +/* SPDX-License-Identifier: GPL-2.0 */
33776 +/*
33777 + * Copyright (C) 2005-2018 Junjiro R. Okajima
33778 + *
33779 + * This program, aufs is free software; you can redistribute it and/or modify
33780 + * it under the terms of the GNU General Public License as published by
33781 + * the Free Software Foundation; either version 2 of the License, or
33782 + * (at your option) any later version.
33783 + *
33784 + * This program is distributed in the hope that it will be useful,
33785 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33786 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33787 + * GNU General Public License for more details.
33788 + *
33789 + * You should have received a copy of the GNU General Public License
33790 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33791 + */
33792 +
33793 +/*
33794 + * policies for selecting one among multiple writable branches
33795 + */
33796 +
33797 +#include <linux/statfs.h>
33798 +#include "aufs.h"
33799 +
33800 +/* subset of cpup_attr() */
33801 +static noinline_for_stack
33802 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33803 +{
33804 +       int err, sbits;
33805 +       struct iattr ia;
33806 +       struct inode *h_isrc;
33807 +
33808 +       h_isrc = d_inode(h_src);
33809 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33810 +       ia.ia_mode = h_isrc->i_mode;
33811 +       ia.ia_uid = h_isrc->i_uid;
33812 +       ia.ia_gid = h_isrc->i_gid;
33813 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33814 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33815 +       /* no delegation since it is just created */
33816 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33817 +
33818 +       /* is this nfs only? */
33819 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33820 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33821 +               ia.ia_mode = h_isrc->i_mode;
33822 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33823 +       }
33824 +
33825 +       return err;
33826 +}
33827 +
33828 +#define AuCpdown_PARENT_OPQ    1
33829 +#define AuCpdown_WHED          (1 << 1)
33830 +#define AuCpdown_MADE_DIR      (1 << 2)
33831 +#define AuCpdown_DIROPQ                (1 << 3)
33832 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33833 +#define au_fset_cpdown(flags, name) \
33834 +       do { (flags) |= AuCpdown_##name; } while (0)
33835 +#define au_fclr_cpdown(flags, name) \
33836 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33837 +
33838 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33839 +                            unsigned int *flags)
33840 +{
33841 +       int err;
33842 +       struct dentry *opq_dentry;
33843 +
33844 +       opq_dentry = au_diropq_create(dentry, bdst);
33845 +       err = PTR_ERR(opq_dentry);
33846 +       if (IS_ERR(opq_dentry))
33847 +               goto out;
33848 +       dput(opq_dentry);
33849 +       au_fset_cpdown(*flags, DIROPQ);
33850 +
33851 +out:
33852 +       return err;
33853 +}
33854 +
33855 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33856 +                           struct inode *dir, aufs_bindex_t bdst)
33857 +{
33858 +       int err;
33859 +       struct path h_path;
33860 +       struct au_branch *br;
33861 +
33862 +       br = au_sbr(dentry->d_sb, bdst);
33863 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33864 +       err = PTR_ERR(h_path.dentry);
33865 +       if (IS_ERR(h_path.dentry))
33866 +               goto out;
33867 +
33868 +       err = 0;
33869 +       if (d_is_positive(h_path.dentry)) {
33870 +               h_path.mnt = au_br_mnt(br);
33871 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33872 +                                         dentry);
33873 +       }
33874 +       dput(h_path.dentry);
33875 +
33876 +out:
33877 +       return err;
33878 +}
33879 +
33880 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33881 +                        struct au_pin *pin,
33882 +                        struct dentry *h_parent, void *arg)
33883 +{
33884 +       int err, rerr;
33885 +       aufs_bindex_t bopq, btop;
33886 +       struct path h_path;
33887 +       struct dentry *parent;
33888 +       struct inode *h_dir, *h_inode, *inode, *dir;
33889 +       unsigned int *flags = arg;
33890 +
33891 +       btop = au_dbtop(dentry);
33892 +       /* dentry is di-locked */
33893 +       parent = dget_parent(dentry);
33894 +       dir = d_inode(parent);
33895 +       h_dir = d_inode(h_parent);
33896 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33897 +       IMustLock(h_dir);
33898 +
33899 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
33900 +       if (unlikely(err < 0))
33901 +               goto out;
33902 +       h_path.dentry = au_h_dptr(dentry, bdst);
33903 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33904 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
33905 +                             S_IRWXU | S_IRUGO | S_IXUGO);
33906 +       if (unlikely(err))
33907 +               goto out_put;
33908 +       au_fset_cpdown(*flags, MADE_DIR);
33909 +
33910 +       bopq = au_dbdiropq(dentry);
33911 +       au_fclr_cpdown(*flags, WHED);
33912 +       au_fclr_cpdown(*flags, DIROPQ);
33913 +       if (au_dbwh(dentry) == bdst)
33914 +               au_fset_cpdown(*flags, WHED);
33915 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33916 +               au_fset_cpdown(*flags, PARENT_OPQ);
33917 +       h_inode = d_inode(h_path.dentry);
33918 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
33919 +       if (au_ftest_cpdown(*flags, WHED)) {
33920 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
33921 +               if (unlikely(err)) {
33922 +                       inode_unlock(h_inode);
33923 +                       goto out_dir;
33924 +               }
33925 +       }
33926 +
33927 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
33928 +       inode_unlock(h_inode);
33929 +       if (unlikely(err))
33930 +               goto out_opq;
33931 +
33932 +       if (au_ftest_cpdown(*flags, WHED)) {
33933 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33934 +               if (unlikely(err))
33935 +                       goto out_opq;
33936 +       }
33937 +
33938 +       inode = d_inode(dentry);
33939 +       if (au_ibbot(inode) < bdst)
33940 +               au_set_ibbot(inode, bdst);
33941 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33942 +                     au_hi_flags(inode, /*isdir*/1));
33943 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
33944 +       goto out; /* success */
33945 +
33946 +       /* revert */
33947 +out_opq:
33948 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
33949 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
33950 +               rerr = au_diropq_remove(dentry, bdst);
33951 +               inode_unlock(h_inode);
33952 +               if (unlikely(rerr)) {
33953 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
33954 +                               dentry, bdst, rerr);
33955 +                       err = -EIO;
33956 +                       goto out;
33957 +               }
33958 +       }
33959 +out_dir:
33960 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
33961 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
33962 +               if (unlikely(rerr)) {
33963 +                       AuIOErr("failed removing %pd b%d (%d)\n",
33964 +                               dentry, bdst, rerr);
33965 +                       err = -EIO;
33966 +               }
33967 +       }
33968 +out_put:
33969 +       au_set_h_dptr(dentry, bdst, NULL);
33970 +       if (au_dbbot(dentry) == bdst)
33971 +               au_update_dbbot(dentry);
33972 +out:
33973 +       dput(parent);
33974 +       return err;
33975 +}
33976 +
33977 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
33978 +{
33979 +       int err;
33980 +       unsigned int flags;
33981 +
33982 +       flags = 0;
33983 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
33984 +
33985 +       return err;
33986 +}
33987 +
33988 +/* ---------------------------------------------------------------------- */
33989 +
33990 +/* policies for create */
33991 +
33992 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
33993 +{
33994 +       int err, i, j, ndentry;
33995 +       aufs_bindex_t bopq;
33996 +       struct au_dcsub_pages dpages;
33997 +       struct au_dpage *dpage;
33998 +       struct dentry **dentries, *parent, *d;
33999 +
34000 +       err = au_dpages_init(&dpages, GFP_NOFS);
34001 +       if (unlikely(err))
34002 +               goto out;
34003 +       parent = dget_parent(dentry);
34004 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34005 +       if (unlikely(err))
34006 +               goto out_free;
34007 +
34008 +       err = bindex;
34009 +       for (i = 0; i < dpages.ndpage; i++) {
34010 +               dpage = dpages.dpages + i;
34011 +               dentries = dpage->dentries;
34012 +               ndentry = dpage->ndentry;
34013 +               for (j = 0; j < ndentry; j++) {
34014 +                       d = dentries[j];
34015 +                       di_read_lock_parent2(d, !AuLock_IR);
34016 +                       bopq = au_dbdiropq(d);
34017 +                       di_read_unlock(d, !AuLock_IR);
34018 +                       if (bopq >= 0 && bopq < err)
34019 +                               err = bopq;
34020 +               }
34021 +       }
34022 +
34023 +out_free:
34024 +       dput(parent);
34025 +       au_dpages_free(&dpages);
34026 +out:
34027 +       return err;
34028 +}
34029 +
34030 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34031 +{
34032 +       for (; bindex >= 0; bindex--)
34033 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34034 +                       return bindex;
34035 +       return -EROFS;
34036 +}
34037 +
34038 +/* top down parent */
34039 +static int au_wbr_create_tdp(struct dentry *dentry,
34040 +                            unsigned int flags __maybe_unused)
34041 +{
34042 +       int err;
34043 +       aufs_bindex_t btop, bindex;
34044 +       struct super_block *sb;
34045 +       struct dentry *parent, *h_parent;
34046 +
34047 +       sb = dentry->d_sb;
34048 +       btop = au_dbtop(dentry);
34049 +       err = btop;
34050 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34051 +               goto out;
34052 +
34053 +       err = -EROFS;
34054 +       parent = dget_parent(dentry);
34055 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34056 +               h_parent = au_h_dptr(parent, bindex);
34057 +               if (!h_parent || d_is_negative(h_parent))
34058 +                       continue;
34059 +
34060 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34061 +                       err = bindex;
34062 +                       break;
34063 +               }
34064 +       }
34065 +       dput(parent);
34066 +
34067 +       /* bottom up here */
34068 +       if (unlikely(err < 0)) {
34069 +               err = au_wbr_bu(sb, btop - 1);
34070 +               if (err >= 0)
34071 +                       err = au_wbr_nonopq(dentry, err);
34072 +       }
34073 +
34074 +out:
34075 +       AuDbg("b%d\n", err);
34076 +       return err;
34077 +}
34078 +
34079 +/* ---------------------------------------------------------------------- */
34080 +
34081 +/* an exception for the policy other than tdp */
34082 +static int au_wbr_create_exp(struct dentry *dentry)
34083 +{
34084 +       int err;
34085 +       aufs_bindex_t bwh, bdiropq;
34086 +       struct dentry *parent;
34087 +
34088 +       err = -1;
34089 +       bwh = au_dbwh(dentry);
34090 +       parent = dget_parent(dentry);
34091 +       bdiropq = au_dbdiropq(parent);
34092 +       if (bwh >= 0) {
34093 +               if (bdiropq >= 0)
34094 +                       err = min(bdiropq, bwh);
34095 +               else
34096 +                       err = bwh;
34097 +               AuDbg("%d\n", err);
34098 +       } else if (bdiropq >= 0) {
34099 +               err = bdiropq;
34100 +               AuDbg("%d\n", err);
34101 +       }
34102 +       dput(parent);
34103 +
34104 +       if (err >= 0)
34105 +               err = au_wbr_nonopq(dentry, err);
34106 +
34107 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34108 +               err = -1;
34109 +
34110 +       AuDbg("%d\n", err);
34111 +       return err;
34112 +}
34113 +
34114 +/* ---------------------------------------------------------------------- */
34115 +
34116 +/* round robin */
34117 +static int au_wbr_create_init_rr(struct super_block *sb)
34118 +{
34119 +       int err;
34120 +
34121 +       err = au_wbr_bu(sb, au_sbbot(sb));
34122 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34123 +       /* smp_mb(); */
34124 +
34125 +       AuDbg("b%d\n", err);
34126 +       return err;
34127 +}
34128 +
34129 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34130 +{
34131 +       int err, nbr;
34132 +       unsigned int u;
34133 +       aufs_bindex_t bindex, bbot;
34134 +       struct super_block *sb;
34135 +       atomic_t *next;
34136 +
34137 +       err = au_wbr_create_exp(dentry);
34138 +       if (err >= 0)
34139 +               goto out;
34140 +
34141 +       sb = dentry->d_sb;
34142 +       next = &au_sbi(sb)->si_wbr_rr_next;
34143 +       bbot = au_sbbot(sb);
34144 +       nbr = bbot + 1;
34145 +       for (bindex = 0; bindex <= bbot; bindex++) {
34146 +               if (!au_ftest_wbr(flags, DIR)) {
34147 +                       err = atomic_dec_return(next) + 1;
34148 +                       /* modulo for 0 is meaningless */
34149 +                       if (unlikely(!err))
34150 +                               err = atomic_dec_return(next) + 1;
34151 +               } else
34152 +                       err = atomic_read(next);
34153 +               AuDbg("%d\n", err);
34154 +               u = err;
34155 +               err = u % nbr;
34156 +               AuDbg("%d\n", err);
34157 +               if (!au_br_rdonly(au_sbr(sb, err)))
34158 +                       break;
34159 +               err = -EROFS;
34160 +       }
34161 +
34162 +       if (err >= 0)
34163 +               err = au_wbr_nonopq(dentry, err);
34164 +
34165 +out:
34166 +       AuDbg("%d\n", err);
34167 +       return err;
34168 +}
34169 +
34170 +/* ---------------------------------------------------------------------- */
34171 +
34172 +/* most free space */
34173 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34174 +{
34175 +       struct super_block *sb;
34176 +       struct au_branch *br;
34177 +       struct au_wbr_mfs *mfs;
34178 +       struct dentry *h_parent;
34179 +       aufs_bindex_t bindex, bbot;
34180 +       int err;
34181 +       unsigned long long b, bavail;
34182 +       struct path h_path;
34183 +       /* reduce the stack usage */
34184 +       struct kstatfs *st;
34185 +
34186 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34187 +       if (unlikely(!st)) {
34188 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34189 +               return;
34190 +       }
34191 +
34192 +       bavail = 0;
34193 +       sb = dentry->d_sb;
34194 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34195 +       MtxMustLock(&mfs->mfs_lock);
34196 +       mfs->mfs_bindex = -EROFS;
34197 +       mfs->mfsrr_bytes = 0;
34198 +       if (!parent) {
34199 +               bindex = 0;
34200 +               bbot = au_sbbot(sb);
34201 +       } else {
34202 +               bindex = au_dbtop(parent);
34203 +               bbot = au_dbtaildir(parent);
34204 +       }
34205 +
34206 +       for (; bindex <= bbot; bindex++) {
34207 +               if (parent) {
34208 +                       h_parent = au_h_dptr(parent, bindex);
34209 +                       if (!h_parent || d_is_negative(h_parent))
34210 +                               continue;
34211 +               }
34212 +               br = au_sbr(sb, bindex);
34213 +               if (au_br_rdonly(br))
34214 +                       continue;
34215 +
34216 +               /* sb->s_root for NFS is unreliable */
34217 +               h_path.mnt = au_br_mnt(br);
34218 +               h_path.dentry = h_path.mnt->mnt_root;
34219 +               err = vfs_statfs(&h_path, st);
34220 +               if (unlikely(err)) {
34221 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34222 +                       continue;
34223 +               }
34224 +
34225 +               /* when the available size is equal, select the lower one */
34226 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34227 +                            || sizeof(b) < sizeof(st->f_bsize));
34228 +               b = st->f_bavail * st->f_bsize;
34229 +               br->br_wbr->wbr_bytes = b;
34230 +               if (b >= bavail) {
34231 +                       bavail = b;
34232 +                       mfs->mfs_bindex = bindex;
34233 +                       mfs->mfs_jiffy = jiffies;
34234 +               }
34235 +       }
34236 +
34237 +       mfs->mfsrr_bytes = bavail;
34238 +       AuDbg("b%d\n", mfs->mfs_bindex);
34239 +       kfree(st);
34240 +}
34241 +
34242 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34243 +{
34244 +       int err;
34245 +       struct dentry *parent;
34246 +       struct super_block *sb;
34247 +       struct au_wbr_mfs *mfs;
34248 +
34249 +       err = au_wbr_create_exp(dentry);
34250 +       if (err >= 0)
34251 +               goto out;
34252 +
34253 +       sb = dentry->d_sb;
34254 +       parent = NULL;
34255 +       if (au_ftest_wbr(flags, PARENT))
34256 +               parent = dget_parent(dentry);
34257 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34258 +       mutex_lock(&mfs->mfs_lock);
34259 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34260 +           || mfs->mfs_bindex < 0
34261 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34262 +               au_mfs(dentry, parent);
34263 +       mutex_unlock(&mfs->mfs_lock);
34264 +       err = mfs->mfs_bindex;
34265 +       dput(parent);
34266 +
34267 +       if (err >= 0)
34268 +               err = au_wbr_nonopq(dentry, err);
34269 +
34270 +out:
34271 +       AuDbg("b%d\n", err);
34272 +       return err;
34273 +}
34274 +
34275 +static int au_wbr_create_init_mfs(struct super_block *sb)
34276 +{
34277 +       struct au_wbr_mfs *mfs;
34278 +
34279 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34280 +       mutex_init(&mfs->mfs_lock);
34281 +       mfs->mfs_jiffy = 0;
34282 +       mfs->mfs_bindex = -EROFS;
34283 +
34284 +       return 0;
34285 +}
34286 +
34287 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34288 +{
34289 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34290 +       return 0;
34291 +}
34292 +
34293 +/* ---------------------------------------------------------------------- */
34294 +
34295 +/* top down regardless parent, and then mfs */
34296 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34297 +                              unsigned int flags __maybe_unused)
34298 +{
34299 +       int err;
34300 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34301 +       unsigned long long watermark;
34302 +       struct super_block *sb;
34303 +       struct au_wbr_mfs *mfs;
34304 +       struct au_branch *br;
34305 +       struct dentry *parent;
34306 +
34307 +       sb = dentry->d_sb;
34308 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34309 +       mutex_lock(&mfs->mfs_lock);
34310 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34311 +           || mfs->mfs_bindex < 0)
34312 +               au_mfs(dentry, /*parent*/NULL);
34313 +       watermark = mfs->mfsrr_watermark;
34314 +       bmfs = mfs->mfs_bindex;
34315 +       mutex_unlock(&mfs->mfs_lock);
34316 +
34317 +       /* another style of au_wbr_create_exp() */
34318 +       bwh = au_dbwh(dentry);
34319 +       parent = dget_parent(dentry);
34320 +       btail = au_dbtaildir(parent);
34321 +       if (bwh >= 0 && bwh < btail)
34322 +               btail = bwh;
34323 +
34324 +       err = au_wbr_nonopq(dentry, btail);
34325 +       if (unlikely(err < 0))
34326 +               goto out;
34327 +       btail = err;
34328 +       bfound = -1;
34329 +       for (bindex = 0; bindex <= btail; bindex++) {
34330 +               br = au_sbr(sb, bindex);
34331 +               if (au_br_rdonly(br))
34332 +                       continue;
34333 +               if (br->br_wbr->wbr_bytes > watermark) {
34334 +                       bfound = bindex;
34335 +                       break;
34336 +               }
34337 +       }
34338 +       err = bfound;
34339 +       if (err < 0)
34340 +               err = bmfs;
34341 +
34342 +out:
34343 +       dput(parent);
34344 +       AuDbg("b%d\n", err);
34345 +       return err;
34346 +}
34347 +
34348 +/* ---------------------------------------------------------------------- */
34349 +
34350 +/* most free space and then round robin */
34351 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34352 +{
34353 +       int err;
34354 +       struct au_wbr_mfs *mfs;
34355 +
34356 +       err = au_wbr_create_mfs(dentry, flags);
34357 +       if (err >= 0) {
34358 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34359 +               mutex_lock(&mfs->mfs_lock);
34360 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34361 +                       err = au_wbr_create_rr(dentry, flags);
34362 +               mutex_unlock(&mfs->mfs_lock);
34363 +       }
34364 +
34365 +       AuDbg("b%d\n", err);
34366 +       return err;
34367 +}
34368 +
34369 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34370 +{
34371 +       int err;
34372 +
34373 +       au_wbr_create_init_mfs(sb); /* ignore */
34374 +       err = au_wbr_create_init_rr(sb);
34375 +
34376 +       return err;
34377 +}
34378 +
34379 +/* ---------------------------------------------------------------------- */
34380 +
34381 +/* top down parent and most free space */
34382 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34383 +{
34384 +       int err, e2;
34385 +       unsigned long long b;
34386 +       aufs_bindex_t bindex, btop, bbot;
34387 +       struct super_block *sb;
34388 +       struct dentry *parent, *h_parent;
34389 +       struct au_branch *br;
34390 +
34391 +       err = au_wbr_create_tdp(dentry, flags);
34392 +       if (unlikely(err < 0))
34393 +               goto out;
34394 +       parent = dget_parent(dentry);
34395 +       btop = au_dbtop(parent);
34396 +       bbot = au_dbtaildir(parent);
34397 +       if (btop == bbot)
34398 +               goto out_parent; /* success */
34399 +
34400 +       e2 = au_wbr_create_mfs(dentry, flags);
34401 +       if (e2 < 0)
34402 +               goto out_parent; /* success */
34403 +
34404 +       /* when the available size is equal, select upper one */
34405 +       sb = dentry->d_sb;
34406 +       br = au_sbr(sb, err);
34407 +       b = br->br_wbr->wbr_bytes;
34408 +       AuDbg("b%d, %llu\n", err, b);
34409 +
34410 +       for (bindex = btop; bindex <= bbot; bindex++) {
34411 +               h_parent = au_h_dptr(parent, bindex);
34412 +               if (!h_parent || d_is_negative(h_parent))
34413 +                       continue;
34414 +
34415 +               br = au_sbr(sb, bindex);
34416 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34417 +                       b = br->br_wbr->wbr_bytes;
34418 +                       err = bindex;
34419 +                       AuDbg("b%d, %llu\n", err, b);
34420 +               }
34421 +       }
34422 +
34423 +       if (err >= 0)
34424 +               err = au_wbr_nonopq(dentry, err);
34425 +
34426 +out_parent:
34427 +       dput(parent);
34428 +out:
34429 +       AuDbg("b%d\n", err);
34430 +       return err;
34431 +}
34432 +
34433 +/* ---------------------------------------------------------------------- */
34434 +
34435 +/*
34436 + * - top down parent
34437 + * - most free space with parent
34438 + * - most free space round-robin regardless parent
34439 + */
34440 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34441 +{
34442 +       int err;
34443 +       unsigned long long watermark;
34444 +       struct super_block *sb;
34445 +       struct au_branch *br;
34446 +       struct au_wbr_mfs *mfs;
34447 +
34448 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34449 +       if (unlikely(err < 0))
34450 +               goto out;
34451 +
34452 +       sb = dentry->d_sb;
34453 +       br = au_sbr(sb, err);
34454 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34455 +       mutex_lock(&mfs->mfs_lock);
34456 +       watermark = mfs->mfsrr_watermark;
34457 +       mutex_unlock(&mfs->mfs_lock);
34458 +       if (br->br_wbr->wbr_bytes < watermark)
34459 +               /* regardless the parent dir */
34460 +               err = au_wbr_create_mfsrr(dentry, flags);
34461 +
34462 +out:
34463 +       AuDbg("b%d\n", err);
34464 +       return err;
34465 +}
34466 +
34467 +/* ---------------------------------------------------------------------- */
34468 +
34469 +/* policies for copyup */
34470 +
34471 +/* top down parent */
34472 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34473 +{
34474 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34475 +}
34476 +
34477 +/* bottom up parent */
34478 +static int au_wbr_copyup_bup(struct dentry *dentry)
34479 +{
34480 +       int err;
34481 +       aufs_bindex_t bindex, btop;
34482 +       struct dentry *parent, *h_parent;
34483 +       struct super_block *sb;
34484 +
34485 +       err = -EROFS;
34486 +       sb = dentry->d_sb;
34487 +       parent = dget_parent(dentry);
34488 +       btop = au_dbtop(parent);
34489 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34490 +               h_parent = au_h_dptr(parent, bindex);
34491 +               if (!h_parent || d_is_negative(h_parent))
34492 +                       continue;
34493 +
34494 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34495 +                       err = bindex;
34496 +                       break;
34497 +               }
34498 +       }
34499 +       dput(parent);
34500 +
34501 +       /* bottom up here */
34502 +       if (unlikely(err < 0))
34503 +               err = au_wbr_bu(sb, btop - 1);
34504 +
34505 +       AuDbg("b%d\n", err);
34506 +       return err;
34507 +}
34508 +
34509 +/* bottom up */
34510 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34511 +{
34512 +       int err;
34513 +
34514 +       err = au_wbr_bu(dentry->d_sb, btop);
34515 +       AuDbg("b%d\n", err);
34516 +       if (err > btop)
34517 +               err = au_wbr_nonopq(dentry, err);
34518 +
34519 +       AuDbg("b%d\n", err);
34520 +       return err;
34521 +}
34522 +
34523 +static int au_wbr_copyup_bu(struct dentry *dentry)
34524 +{
34525 +       int err;
34526 +       aufs_bindex_t btop;
34527 +
34528 +       btop = au_dbtop(dentry);
34529 +       err = au_wbr_do_copyup_bu(dentry, btop);
34530 +       return err;
34531 +}
34532 +
34533 +/* ---------------------------------------------------------------------- */
34534 +
34535 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34536 +       [AuWbrCopyup_TDP] = {
34537 +               .copyup = au_wbr_copyup_tdp
34538 +       },
34539 +       [AuWbrCopyup_BUP] = {
34540 +               .copyup = au_wbr_copyup_bup
34541 +       },
34542 +       [AuWbrCopyup_BU] = {
34543 +               .copyup = au_wbr_copyup_bu
34544 +       }
34545 +};
34546 +
34547 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34548 +       [AuWbrCreate_TDP] = {
34549 +               .create = au_wbr_create_tdp
34550 +       },
34551 +       [AuWbrCreate_RR] = {
34552 +               .create = au_wbr_create_rr,
34553 +               .init   = au_wbr_create_init_rr
34554 +       },
34555 +       [AuWbrCreate_MFS] = {
34556 +               .create = au_wbr_create_mfs,
34557 +               .init   = au_wbr_create_init_mfs,
34558 +               .fin    = au_wbr_create_fin_mfs
34559 +       },
34560 +       [AuWbrCreate_MFSV] = {
34561 +               .create = au_wbr_create_mfs,
34562 +               .init   = au_wbr_create_init_mfs,
34563 +               .fin    = au_wbr_create_fin_mfs
34564 +       },
34565 +       [AuWbrCreate_MFSRR] = {
34566 +               .create = au_wbr_create_mfsrr,
34567 +               .init   = au_wbr_create_init_mfsrr,
34568 +               .fin    = au_wbr_create_fin_mfs
34569 +       },
34570 +       [AuWbrCreate_MFSRRV] = {
34571 +               .create = au_wbr_create_mfsrr,
34572 +               .init   = au_wbr_create_init_mfsrr,
34573 +               .fin    = au_wbr_create_fin_mfs
34574 +       },
34575 +       [AuWbrCreate_TDMFS] = {
34576 +               .create = au_wbr_create_tdmfs,
34577 +               .init   = au_wbr_create_init_mfs,
34578 +               .fin    = au_wbr_create_fin_mfs
34579 +       },
34580 +       [AuWbrCreate_TDMFSV] = {
34581 +               .create = au_wbr_create_tdmfs,
34582 +               .init   = au_wbr_create_init_mfs,
34583 +               .fin    = au_wbr_create_fin_mfs
34584 +       },
34585 +       [AuWbrCreate_PMFS] = {
34586 +               .create = au_wbr_create_pmfs,
34587 +               .init   = au_wbr_create_init_mfs,
34588 +               .fin    = au_wbr_create_fin_mfs
34589 +       },
34590 +       [AuWbrCreate_PMFSV] = {
34591 +               .create = au_wbr_create_pmfs,
34592 +               .init   = au_wbr_create_init_mfs,
34593 +               .fin    = au_wbr_create_fin_mfs
34594 +       },
34595 +       [AuWbrCreate_PMFSRR] = {
34596 +               .create = au_wbr_create_pmfsrr,
34597 +               .init   = au_wbr_create_init_mfsrr,
34598 +               .fin    = au_wbr_create_fin_mfs
34599 +       },
34600 +       [AuWbrCreate_PMFSRRV] = {
34601 +               .create = au_wbr_create_pmfsrr,
34602 +               .init   = au_wbr_create_init_mfsrr,
34603 +               .fin    = au_wbr_create_fin_mfs
34604 +       }
34605 +};
34606 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34607 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34608 +++ linux/fs/aufs/whout.c       2018-07-19 09:46:13.063314675 +0200
34609 @@ -0,0 +1,1062 @@
34610 +/* SPDX-License-Identifier: GPL-2.0 */
34611 +/*
34612 + * Copyright (C) 2005-2018 Junjiro R. Okajima
34613 + *
34614 + * This program, aufs is free software; you can redistribute it and/or modify
34615 + * it under the terms of the GNU General Public License as published by
34616 + * the Free Software Foundation; either version 2 of the License, or
34617 + * (at your option) any later version.
34618 + *
34619 + * This program is distributed in the hope that it will be useful,
34620 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34621 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34622 + * GNU General Public License for more details.
34623 + *
34624 + * You should have received a copy of the GNU General Public License
34625 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34626 + */
34627 +
34628 +/*
34629 + * whiteout for logical deletion and opaque directory
34630 + */
34631 +
34632 +#include "aufs.h"
34633 +
34634 +#define WH_MASK                        S_IRUGO
34635 +
34636 +/*
34637 + * If a directory contains this file, then it is opaque.  We start with the
34638 + * .wh. flag so that it is blocked by lookup.
34639 + */
34640 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34641 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34642 +
34643 +/*
34644 + * generate whiteout name, which is NOT terminated by NULL.
34645 + * @name: original d_name.name
34646 + * @len: original d_name.len
34647 + * @wh: whiteout qstr
34648 + * returns zero when succeeds, otherwise error.
34649 + * succeeded value as wh->name should be freed by kfree().
34650 + */
34651 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34652 +{
34653 +       char *p;
34654 +
34655 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34656 +               return -ENAMETOOLONG;
34657 +
34658 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34659 +       p = kmalloc(wh->len, GFP_NOFS);
34660 +       wh->name = p;
34661 +       if (p) {
34662 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34663 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34664 +               /* smp_mb(); */
34665 +               return 0;
34666 +       }
34667 +       return -ENOMEM;
34668 +}
34669 +
34670 +/* ---------------------------------------------------------------------- */
34671 +
34672 +/*
34673 + * test if the @wh_name exists under @h_parent.
34674 + * @try_sio specifies the necessary of super-io.
34675 + */
34676 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34677 +{
34678 +       int err;
34679 +       struct dentry *wh_dentry;
34680 +
34681 +       if (!try_sio)
34682 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34683 +       else
34684 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34685 +       err = PTR_ERR(wh_dentry);
34686 +       if (IS_ERR(wh_dentry)) {
34687 +               if (err == -ENAMETOOLONG)
34688 +                       err = 0;
34689 +               goto out;
34690 +       }
34691 +
34692 +       err = 0;
34693 +       if (d_is_negative(wh_dentry))
34694 +               goto out_wh; /* success */
34695 +
34696 +       err = 1;
34697 +       if (d_is_reg(wh_dentry))
34698 +               goto out_wh; /* success */
34699 +
34700 +       err = -EIO;
34701 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34702 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34703 +
34704 +out_wh:
34705 +       dput(wh_dentry);
34706 +out:
34707 +       return err;
34708 +}
34709 +
34710 +/*
34711 + * test if the @h_dentry sets opaque or not.
34712 + */
34713 +int au_diropq_test(struct dentry *h_dentry)
34714 +{
34715 +       int err;
34716 +       struct inode *h_dir;
34717 +
34718 +       h_dir = d_inode(h_dentry);
34719 +       err = au_wh_test(h_dentry, &diropq_name,
34720 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34721 +       return err;
34722 +}
34723 +
34724 +/*
34725 + * returns a negative dentry whose name is unique and temporary.
34726 + */
34727 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34728 +                            struct qstr *prefix)
34729 +{
34730 +       struct dentry *dentry;
34731 +       int i;
34732 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34733 +               *name, *p;
34734 +       /* strict atomic_t is unnecessary here */
34735 +       static unsigned short cnt;
34736 +       struct qstr qs;
34737 +
34738 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34739 +
34740 +       name = defname;
34741 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34742 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34743 +               dentry = ERR_PTR(-ENAMETOOLONG);
34744 +               if (unlikely(qs.len > NAME_MAX))
34745 +                       goto out;
34746 +               dentry = ERR_PTR(-ENOMEM);
34747 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34748 +               if (unlikely(!name))
34749 +                       goto out;
34750 +       }
34751 +
34752 +       /* doubly whiteout-ed */
34753 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34754 +       p = name + AUFS_WH_PFX_LEN * 2;
34755 +       memcpy(p, prefix->name, prefix->len);
34756 +       p += prefix->len;
34757 +       *p++ = '.';
34758 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34759 +
34760 +       qs.name = name;
34761 +       for (i = 0; i < 3; i++) {
34762 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34763 +               dentry = au_sio_lkup_one(&qs, h_parent);
34764 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34765 +                       goto out_name;
34766 +               dput(dentry);
34767 +       }
34768 +       /* pr_warn("could not get random name\n"); */
34769 +       dentry = ERR_PTR(-EEXIST);
34770 +       AuDbg("%.*s\n", AuLNPair(&qs));
34771 +       BUG();
34772 +
34773 +out_name:
34774 +       if (name != defname)
34775 +               kfree(name);
34776 +out:
34777 +       AuTraceErrPtr(dentry);
34778 +       return dentry;
34779 +}
34780 +
34781 +/*
34782 + * rename the @h_dentry on @br to the whiteouted temporary name.
34783 + */
34784 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34785 +{
34786 +       int err;
34787 +       struct path h_path = {
34788 +               .mnt = au_br_mnt(br)
34789 +       };
34790 +       struct inode *h_dir, *delegated;
34791 +       struct dentry *h_parent;
34792 +
34793 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34794 +       h_dir = d_inode(h_parent);
34795 +       IMustLock(h_dir);
34796 +
34797 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34798 +       err = PTR_ERR(h_path.dentry);
34799 +       if (IS_ERR(h_path.dentry))
34800 +               goto out;
34801 +
34802 +       /* under the same dir, no need to lock_rename() */
34803 +       delegated = NULL;
34804 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34805 +                          /*flags*/0);
34806 +       AuTraceErr(err);
34807 +       if (unlikely(err == -EWOULDBLOCK)) {
34808 +               pr_warn("cannot retry for NFSv4 delegation"
34809 +                       " for an internal rename\n");
34810 +               iput(delegated);
34811 +       }
34812 +       dput(h_path.dentry);
34813 +
34814 +out:
34815 +       AuTraceErr(err);
34816 +       return err;
34817 +}
34818 +
34819 +/* ---------------------------------------------------------------------- */
34820 +/*
34821 + * functions for removing a whiteout
34822 + */
34823 +
34824 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34825 +{
34826 +       int err, force;
34827 +       struct inode *delegated;
34828 +
34829 +       /*
34830 +        * forces superio when the dir has a sticky bit.
34831 +        * this may be a violation of unix fs semantics.
34832 +        */
34833 +       force = (h_dir->i_mode & S_ISVTX)
34834 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34835 +       delegated = NULL;
34836 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34837 +       if (unlikely(err == -EWOULDBLOCK)) {
34838 +               pr_warn("cannot retry for NFSv4 delegation"
34839 +                       " for an internal unlink\n");
34840 +               iput(delegated);
34841 +       }
34842 +       return err;
34843 +}
34844 +
34845 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34846 +                       struct dentry *dentry)
34847 +{
34848 +       int err;
34849 +
34850 +       err = do_unlink_wh(h_dir, h_path);
34851 +       if (!err && dentry)
34852 +               au_set_dbwh(dentry, -1);
34853 +
34854 +       return err;
34855 +}
34856 +
34857 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34858 +                         struct au_branch *br)
34859 +{
34860 +       int err;
34861 +       struct path h_path = {
34862 +               .mnt = au_br_mnt(br)
34863 +       };
34864 +
34865 +       err = 0;
34866 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
34867 +       if (IS_ERR(h_path.dentry))
34868 +               err = PTR_ERR(h_path.dentry);
34869 +       else {
34870 +               if (d_is_reg(h_path.dentry))
34871 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
34872 +               dput(h_path.dentry);
34873 +       }
34874 +
34875 +       return err;
34876 +}
34877 +
34878 +/* ---------------------------------------------------------------------- */
34879 +/*
34880 + * initialize/clean whiteout for a branch
34881 + */
34882 +
34883 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34884 +                       const int isdir)
34885 +{
34886 +       int err;
34887 +       struct inode *delegated;
34888 +
34889 +       if (d_is_negative(whpath->dentry))
34890 +               return;
34891 +
34892 +       if (isdir)
34893 +               err = vfsub_rmdir(h_dir, whpath);
34894 +       else {
34895 +               delegated = NULL;
34896 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34897 +               if (unlikely(err == -EWOULDBLOCK)) {
34898 +                       pr_warn("cannot retry for NFSv4 delegation"
34899 +                               " for an internal unlink\n");
34900 +                       iput(delegated);
34901 +               }
34902 +       }
34903 +       if (unlikely(err))
34904 +               pr_warn("failed removing %pd (%d), ignored.\n",
34905 +                       whpath->dentry, err);
34906 +}
34907 +
34908 +static int test_linkable(struct dentry *h_root)
34909 +{
34910 +       struct inode *h_dir = d_inode(h_root);
34911 +
34912 +       if (h_dir->i_op->link)
34913 +               return 0;
34914 +
34915 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34916 +              h_root, au_sbtype(h_root->d_sb));
34917 +       return -ENOSYS;
34918 +}
34919 +
34920 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34921 +static int au_whdir(struct inode *h_dir, struct path *path)
34922 +{
34923 +       int err;
34924 +
34925 +       err = -EEXIST;
34926 +       if (d_is_negative(path->dentry)) {
34927 +               int mode = S_IRWXU;
34928 +
34929 +               if (au_test_nfs(path->dentry->d_sb))
34930 +                       mode |= S_IXUGO;
34931 +               err = vfsub_mkdir(h_dir, path, mode);
34932 +       } else if (d_is_dir(path->dentry))
34933 +               err = 0;
34934 +       else
34935 +               pr_err("unknown %pd exists\n", path->dentry);
34936 +
34937 +       return err;
34938 +}
34939 +
34940 +struct au_wh_base {
34941 +       const struct qstr *name;
34942 +       struct dentry *dentry;
34943 +};
34944 +
34945 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
34946 +                         struct path *h_path)
34947 +{
34948 +       h_path->dentry = base[AuBrWh_BASE].dentry;
34949 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
34950 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
34951 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
34952 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
34953 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
34954 +}
34955 +
34956 +/*
34957 + * returns tri-state,
34958 + * minus: error, caller should print the message
34959 + * zero: succuess
34960 + * plus: error, caller should NOT print the message
34961 + */
34962 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
34963 +                               int do_plink, struct au_wh_base base[],
34964 +                               struct path *h_path)
34965 +{
34966 +       int err;
34967 +       struct inode *h_dir;
34968 +
34969 +       h_dir = d_inode(h_root);
34970 +       h_path->dentry = base[AuBrWh_BASE].dentry;
34971 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
34972 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
34973 +       if (do_plink) {
34974 +               err = test_linkable(h_root);
34975 +               if (unlikely(err)) {
34976 +                       err = 1;
34977 +                       goto out;
34978 +               }
34979 +
34980 +               err = au_whdir(h_dir, h_path);
34981 +               if (unlikely(err))
34982 +                       goto out;
34983 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
34984 +       } else
34985 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
34986 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
34987 +       err = au_whdir(h_dir, h_path);
34988 +       if (unlikely(err))
34989 +               goto out;
34990 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
34991 +
34992 +out:
34993 +       return err;
34994 +}
34995 +
34996 +/*
34997 + * for the moment, aufs supports the branch filesystem which does not support
34998 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
34999 + * copyup failed. finally, such filesystem will not be used as the writable
35000 + * branch.
35001 + *
35002 + * returns tri-state, see above.
35003 + */
35004 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35005 +                        int do_plink, struct au_wh_base base[],
35006 +                        struct path *h_path)
35007 +{
35008 +       int err;
35009 +       struct inode *h_dir;
35010 +
35011 +       WbrWhMustWriteLock(wbr);
35012 +
35013 +       err = test_linkable(h_root);
35014 +       if (unlikely(err)) {
35015 +               err = 1;
35016 +               goto out;
35017 +       }
35018 +
35019 +       /*
35020 +        * todo: should this create be done in /sbin/mount.aufs helper?
35021 +        */
35022 +       err = -EEXIST;
35023 +       h_dir = d_inode(h_root);
35024 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35025 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35026 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35027 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35028 +               err = 0;
35029 +       else
35030 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35031 +       if (unlikely(err))
35032 +               goto out;
35033 +
35034 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35035 +       if (do_plink) {
35036 +               err = au_whdir(h_dir, h_path);
35037 +               if (unlikely(err))
35038 +                       goto out;
35039 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35040 +       } else
35041 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35042 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35043 +
35044 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35045 +       err = au_whdir(h_dir, h_path);
35046 +       if (unlikely(err))
35047 +               goto out;
35048 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35049 +
35050 +out:
35051 +       return err;
35052 +}
35053 +
35054 +/*
35055 + * initialize the whiteout base file/dir for @br.
35056 + */
35057 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35058 +{
35059 +       int err, i;
35060 +       const unsigned char do_plink
35061 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35062 +       struct inode *h_dir;
35063 +       struct path path = br->br_path;
35064 +       struct dentry *h_root = path.dentry;
35065 +       struct au_wbr *wbr = br->br_wbr;
35066 +       static const struct qstr base_name[] = {
35067 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35068 +                                         sizeof(AUFS_BASE_NAME) - 1),
35069 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35070 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35071 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35072 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35073 +       };
35074 +       struct au_wh_base base[] = {
35075 +               [AuBrWh_BASE] = {
35076 +                       .name   = base_name + AuBrWh_BASE,
35077 +                       .dentry = NULL
35078 +               },
35079 +               [AuBrWh_PLINK] = {
35080 +                       .name   = base_name + AuBrWh_PLINK,
35081 +                       .dentry = NULL
35082 +               },
35083 +               [AuBrWh_ORPH] = {
35084 +                       .name   = base_name + AuBrWh_ORPH,
35085 +                       .dentry = NULL
35086 +               }
35087 +       };
35088 +
35089 +       if (wbr)
35090 +               WbrWhMustWriteLock(wbr);
35091 +
35092 +       for (i = 0; i < AuBrWh_Last; i++) {
35093 +               /* doubly whiteouted */
35094 +               struct dentry *d;
35095 +
35096 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35097 +               err = PTR_ERR(d);
35098 +               if (IS_ERR(d))
35099 +                       goto out;
35100 +
35101 +               base[i].dentry = d;
35102 +               AuDebugOn(wbr
35103 +                         && wbr->wbr_wh[i]
35104 +                         && wbr->wbr_wh[i] != base[i].dentry);
35105 +       }
35106 +
35107 +       if (wbr)
35108 +               for (i = 0; i < AuBrWh_Last; i++) {
35109 +                       dput(wbr->wbr_wh[i]);
35110 +                       wbr->wbr_wh[i] = NULL;
35111 +               }
35112 +
35113 +       err = 0;
35114 +       if (!au_br_writable(br->br_perm)) {
35115 +               h_dir = d_inode(h_root);
35116 +               au_wh_init_ro(h_dir, base, &path);
35117 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35118 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35119 +               if (err > 0)
35120 +                       goto out;
35121 +               else if (err)
35122 +                       goto out_err;
35123 +       } else {
35124 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35125 +               if (err > 0)
35126 +                       goto out;
35127 +               else if (err)
35128 +                       goto out_err;
35129 +       }
35130 +       goto out; /* success */
35131 +
35132 +out_err:
35133 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35134 +              err, h_root, au_sbtype(h_root->d_sb));
35135 +out:
35136 +       for (i = 0; i < AuBrWh_Last; i++)
35137 +               dput(base[i].dentry);
35138 +       return err;
35139 +}
35140 +
35141 +/* ---------------------------------------------------------------------- */
35142 +/*
35143 + * whiteouts are all hard-linked usually.
35144 + * when its link count reaches a ceiling, we create a new whiteout base
35145 + * asynchronously.
35146 + */
35147 +
35148 +struct reinit_br_wh {
35149 +       struct super_block *sb;
35150 +       struct au_branch *br;
35151 +};
35152 +
35153 +static void reinit_br_wh(void *arg)
35154 +{
35155 +       int err;
35156 +       aufs_bindex_t bindex;
35157 +       struct path h_path;
35158 +       struct reinit_br_wh *a = arg;
35159 +       struct au_wbr *wbr;
35160 +       struct inode *dir, *delegated;
35161 +       struct dentry *h_root;
35162 +       struct au_hinode *hdir;
35163 +
35164 +       err = 0;
35165 +       wbr = a->br->br_wbr;
35166 +       /* big aufs lock */
35167 +       si_noflush_write_lock(a->sb);
35168 +       if (!au_br_writable(a->br->br_perm))
35169 +               goto out;
35170 +       bindex = au_br_index(a->sb, a->br->br_id);
35171 +       if (unlikely(bindex < 0))
35172 +               goto out;
35173 +
35174 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35175 +       dir = d_inode(a->sb->s_root);
35176 +       hdir = au_hi(dir, bindex);
35177 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35178 +       AuDebugOn(h_root != au_br_dentry(a->br));
35179 +
35180 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35181 +       wbr_wh_write_lock(wbr);
35182 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35183 +                         h_root, a->br);
35184 +       if (!err) {
35185 +               h_path.dentry = wbr->wbr_whbase;
35186 +               h_path.mnt = au_br_mnt(a->br);
35187 +               delegated = NULL;
35188 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35189 +                                  /*force*/0);
35190 +               if (unlikely(err == -EWOULDBLOCK)) {
35191 +                       pr_warn("cannot retry for NFSv4 delegation"
35192 +                               " for an internal unlink\n");
35193 +                       iput(delegated);
35194 +               }
35195 +       } else {
35196 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35197 +               err = 0;
35198 +       }
35199 +       dput(wbr->wbr_whbase);
35200 +       wbr->wbr_whbase = NULL;
35201 +       if (!err)
35202 +               err = au_wh_init(a->br, a->sb);
35203 +       wbr_wh_write_unlock(wbr);
35204 +       au_hn_inode_unlock(hdir);
35205 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35206 +       if (!err)
35207 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35208 +
35209 +out:
35210 +       if (wbr)
35211 +               atomic_dec(&wbr->wbr_wh_running);
35212 +       au_br_put(a->br);
35213 +       si_write_unlock(a->sb);
35214 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35215 +       kfree(arg);
35216 +       if (unlikely(err))
35217 +               AuIOErr("err %d\n", err);
35218 +}
35219 +
35220 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35221 +{
35222 +       int do_dec, wkq_err;
35223 +       struct reinit_br_wh *arg;
35224 +
35225 +       do_dec = 1;
35226 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35227 +               goto out;
35228 +
35229 +       /* ignore ENOMEM */
35230 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35231 +       if (arg) {
35232 +               /*
35233 +                * dec(wh_running), kfree(arg) and dec(br_count)
35234 +                * in reinit function
35235 +                */
35236 +               arg->sb = sb;
35237 +               arg->br = br;
35238 +               au_br_get(br);
35239 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35240 +               if (unlikely(wkq_err)) {
35241 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35242 +                       au_br_put(br);
35243 +                       kfree(arg);
35244 +               }
35245 +               do_dec = 0;
35246 +       }
35247 +
35248 +out:
35249 +       if (do_dec)
35250 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35251 +}
35252 +
35253 +/* ---------------------------------------------------------------------- */
35254 +
35255 +/*
35256 + * create the whiteout @wh.
35257 + */
35258 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35259 +                            struct dentry *wh)
35260 +{
35261 +       int err;
35262 +       struct path h_path = {
35263 +               .dentry = wh
35264 +       };
35265 +       struct au_branch *br;
35266 +       struct au_wbr *wbr;
35267 +       struct dentry *h_parent;
35268 +       struct inode *h_dir, *delegated;
35269 +
35270 +       h_parent = wh->d_parent; /* dir inode is locked */
35271 +       h_dir = d_inode(h_parent);
35272 +       IMustLock(h_dir);
35273 +
35274 +       br = au_sbr(sb, bindex);
35275 +       h_path.mnt = au_br_mnt(br);
35276 +       wbr = br->br_wbr;
35277 +       wbr_wh_read_lock(wbr);
35278 +       if (wbr->wbr_whbase) {
35279 +               delegated = NULL;
35280 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35281 +               if (unlikely(err == -EWOULDBLOCK)) {
35282 +                       pr_warn("cannot retry for NFSv4 delegation"
35283 +                               " for an internal link\n");
35284 +                       iput(delegated);
35285 +               }
35286 +               if (!err || err != -EMLINK)
35287 +                       goto out;
35288 +
35289 +               /* link count full. re-initialize br_whbase. */
35290 +               kick_reinit_br_wh(sb, br);
35291 +       }
35292 +
35293 +       /* return this error in this context */
35294 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35295 +       if (!err)
35296 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35297 +
35298 +out:
35299 +       wbr_wh_read_unlock(wbr);
35300 +       return err;
35301 +}
35302 +
35303 +/* ---------------------------------------------------------------------- */
35304 +
35305 +/*
35306 + * create or remove the diropq.
35307 + */
35308 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35309 +                               unsigned int flags)
35310 +{
35311 +       struct dentry *opq_dentry, *h_dentry;
35312 +       struct super_block *sb;
35313 +       struct au_branch *br;
35314 +       int err;
35315 +
35316 +       sb = dentry->d_sb;
35317 +       br = au_sbr(sb, bindex);
35318 +       h_dentry = au_h_dptr(dentry, bindex);
35319 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35320 +       if (IS_ERR(opq_dentry))
35321 +               goto out;
35322 +
35323 +       if (au_ftest_diropq(flags, CREATE)) {
35324 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35325 +               if (!err) {
35326 +                       au_set_dbdiropq(dentry, bindex);
35327 +                       goto out; /* success */
35328 +               }
35329 +       } else {
35330 +               struct path tmp = {
35331 +                       .dentry = opq_dentry,
35332 +                       .mnt    = au_br_mnt(br)
35333 +               };
35334 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35335 +               if (!err)
35336 +                       au_set_dbdiropq(dentry, -1);
35337 +       }
35338 +       dput(opq_dentry);
35339 +       opq_dentry = ERR_PTR(err);
35340 +
35341 +out:
35342 +       return opq_dentry;
35343 +}
35344 +
35345 +struct do_diropq_args {
35346 +       struct dentry **errp;
35347 +       struct dentry *dentry;
35348 +       aufs_bindex_t bindex;
35349 +       unsigned int flags;
35350 +};
35351 +
35352 +static void call_do_diropq(void *args)
35353 +{
35354 +       struct do_diropq_args *a = args;
35355 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35356 +}
35357 +
35358 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35359 +                            unsigned int flags)
35360 +{
35361 +       struct dentry *diropq, *h_dentry;
35362 +
35363 +       h_dentry = au_h_dptr(dentry, bindex);
35364 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35365 +               diropq = do_diropq(dentry, bindex, flags);
35366 +       else {
35367 +               int wkq_err;
35368 +               struct do_diropq_args args = {
35369 +                       .errp           = &diropq,
35370 +                       .dentry         = dentry,
35371 +                       .bindex         = bindex,
35372 +                       .flags          = flags
35373 +               };
35374 +
35375 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35376 +               if (unlikely(wkq_err))
35377 +                       diropq = ERR_PTR(wkq_err);
35378 +       }
35379 +
35380 +       return diropq;
35381 +}
35382 +
35383 +/* ---------------------------------------------------------------------- */
35384 +
35385 +/*
35386 + * lookup whiteout dentry.
35387 + * @h_parent: lower parent dentry which must exist and be locked
35388 + * @base_name: name of dentry which will be whiteouted
35389 + * returns dentry for whiteout.
35390 + */
35391 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35392 +                         struct au_branch *br)
35393 +{
35394 +       int err;
35395 +       struct qstr wh_name;
35396 +       struct dentry *wh_dentry;
35397 +
35398 +       err = au_wh_name_alloc(&wh_name, base_name);
35399 +       wh_dentry = ERR_PTR(err);
35400 +       if (!err) {
35401 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35402 +               kfree(wh_name.name);
35403 +       }
35404 +       return wh_dentry;
35405 +}
35406 +
35407 +/*
35408 + * link/create a whiteout for @dentry on @bindex.
35409 + */
35410 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35411 +                           struct dentry *h_parent)
35412 +{
35413 +       struct dentry *wh_dentry;
35414 +       struct super_block *sb;
35415 +       int err;
35416 +
35417 +       sb = dentry->d_sb;
35418 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35419 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35420 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35421 +               if (!err) {
35422 +                       au_set_dbwh(dentry, bindex);
35423 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35424 +               } else {
35425 +                       dput(wh_dentry);
35426 +                       wh_dentry = ERR_PTR(err);
35427 +               }
35428 +       }
35429 +
35430 +       return wh_dentry;
35431 +}
35432 +
35433 +/* ---------------------------------------------------------------------- */
35434 +
35435 +/* Delete all whiteouts in this directory on branch bindex. */
35436 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35437 +                          aufs_bindex_t bindex, struct au_branch *br)
35438 +{
35439 +       int err;
35440 +       unsigned long ul, n;
35441 +       struct qstr wh_name;
35442 +       char *p;
35443 +       struct hlist_head *head;
35444 +       struct au_vdir_wh *pos;
35445 +       struct au_vdir_destr *str;
35446 +
35447 +       err = -ENOMEM;
35448 +       p = (void *)__get_free_page(GFP_NOFS);
35449 +       wh_name.name = p;
35450 +       if (unlikely(!wh_name.name))
35451 +               goto out;
35452 +
35453 +       err = 0;
35454 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35455 +       p += AUFS_WH_PFX_LEN;
35456 +       n = whlist->nh_num;
35457 +       head = whlist->nh_head;
35458 +       for (ul = 0; !err && ul < n; ul++, head++) {
35459 +               hlist_for_each_entry(pos, head, wh_hash) {
35460 +                       if (pos->wh_bindex != bindex)
35461 +                               continue;
35462 +
35463 +                       str = &pos->wh_str;
35464 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35465 +                               memcpy(p, str->name, str->len);
35466 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35467 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35468 +                               if (!err)
35469 +                                       continue;
35470 +                               break;
35471 +                       }
35472 +                       AuIOErr("whiteout name too long %.*s\n",
35473 +                               str->len, str->name);
35474 +                       err = -EIO;
35475 +                       break;
35476 +               }
35477 +       }
35478 +       free_page((unsigned long)wh_name.name);
35479 +
35480 +out:
35481 +       return err;
35482 +}
35483 +
35484 +struct del_wh_children_args {
35485 +       int *errp;
35486 +       struct dentry *h_dentry;
35487 +       struct au_nhash *whlist;
35488 +       aufs_bindex_t bindex;
35489 +       struct au_branch *br;
35490 +};
35491 +
35492 +static void call_del_wh_children(void *args)
35493 +{
35494 +       struct del_wh_children_args *a = args;
35495 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35496 +}
35497 +
35498 +/* ---------------------------------------------------------------------- */
35499 +
35500 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35501 +{
35502 +       struct au_whtmp_rmdir *whtmp;
35503 +       int err;
35504 +       unsigned int rdhash;
35505 +
35506 +       SiMustAnyLock(sb);
35507 +
35508 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35509 +       if (unlikely(!whtmp)) {
35510 +               whtmp = ERR_PTR(-ENOMEM);
35511 +               goto out;
35512 +       }
35513 +
35514 +       /* no estimation for dir size */
35515 +       rdhash = au_sbi(sb)->si_rdhash;
35516 +       if (!rdhash)
35517 +               rdhash = AUFS_RDHASH_DEF;
35518 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35519 +       if (unlikely(err)) {
35520 +               kfree(whtmp);
35521 +               whtmp = ERR_PTR(err);
35522 +       }
35523 +
35524 +out:
35525 +       return whtmp;
35526 +}
35527 +
35528 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35529 +{
35530 +       if (whtmp->br)
35531 +               au_br_put(whtmp->br);
35532 +       dput(whtmp->wh_dentry);
35533 +       iput(whtmp->dir);
35534 +       au_nhash_wh_free(&whtmp->whlist);
35535 +       kfree(whtmp);
35536 +}
35537 +
35538 +/*
35539 + * rmdir the whiteouted temporary named dir @h_dentry.
35540 + * @whlist: whiteouted children.
35541 + */
35542 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35543 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35544 +{
35545 +       int err;
35546 +       unsigned int h_nlink;
35547 +       struct path h_tmp;
35548 +       struct inode *wh_inode, *h_dir;
35549 +       struct au_branch *br;
35550 +
35551 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35552 +       IMustLock(h_dir);
35553 +
35554 +       br = au_sbr(dir->i_sb, bindex);
35555 +       wh_inode = d_inode(wh_dentry);
35556 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35557 +
35558 +       /*
35559 +        * someone else might change some whiteouts while we were sleeping.
35560 +        * it means this whlist may have an obsoleted entry.
35561 +        */
35562 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35563 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35564 +       else {
35565 +               int wkq_err;
35566 +               struct del_wh_children_args args = {
35567 +                       .errp           = &err,
35568 +                       .h_dentry       = wh_dentry,
35569 +                       .whlist         = whlist,
35570 +                       .bindex         = bindex,
35571 +                       .br             = br
35572 +               };
35573 +
35574 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35575 +               if (unlikely(wkq_err))
35576 +                       err = wkq_err;
35577 +       }
35578 +       inode_unlock(wh_inode);
35579 +
35580 +       if (!err) {
35581 +               h_tmp.dentry = wh_dentry;
35582 +               h_tmp.mnt = au_br_mnt(br);
35583 +               h_nlink = h_dir->i_nlink;
35584 +               err = vfsub_rmdir(h_dir, &h_tmp);
35585 +               /* some fs doesn't change the parent nlink in some cases */
35586 +               h_nlink -= h_dir->i_nlink;
35587 +       }
35588 +
35589 +       if (!err) {
35590 +               if (au_ibtop(dir) == bindex) {
35591 +                       /* todo: dir->i_mutex is necessary */
35592 +                       au_cpup_attr_timesizes(dir);
35593 +                       if (h_nlink)
35594 +                               vfsub_drop_nlink(dir);
35595 +               }
35596 +               return 0; /* success */
35597 +       }
35598 +
35599 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35600 +       return err;
35601 +}
35602 +
35603 +static void call_rmdir_whtmp(void *args)
35604 +{
35605 +       int err;
35606 +       aufs_bindex_t bindex;
35607 +       struct au_whtmp_rmdir *a = args;
35608 +       struct super_block *sb;
35609 +       struct dentry *h_parent;
35610 +       struct inode *h_dir;
35611 +       struct au_hinode *hdir;
35612 +
35613 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35614 +       /* inode_lock(a->dir); */
35615 +       err = -EROFS;
35616 +       sb = a->dir->i_sb;
35617 +       si_read_lock(sb, !AuLock_FLUSH);
35618 +       if (!au_br_writable(a->br->br_perm))
35619 +               goto out;
35620 +       bindex = au_br_index(sb, a->br->br_id);
35621 +       if (unlikely(bindex < 0))
35622 +               goto out;
35623 +
35624 +       err = -EIO;
35625 +       ii_write_lock_parent(a->dir);
35626 +       h_parent = dget_parent(a->wh_dentry);
35627 +       h_dir = d_inode(h_parent);
35628 +       hdir = au_hi(a->dir, bindex);
35629 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35630 +       if (unlikely(err))
35631 +               goto out_mnt;
35632 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35633 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35634 +                         a->br);
35635 +       if (!err)
35636 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35637 +       au_hn_inode_unlock(hdir);
35638 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35639 +
35640 +out_mnt:
35641 +       dput(h_parent);
35642 +       ii_write_unlock(a->dir);
35643 +out:
35644 +       /* inode_unlock(a->dir); */
35645 +       au_whtmp_rmdir_free(a);
35646 +       si_read_unlock(sb);
35647 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35648 +       if (unlikely(err))
35649 +               AuIOErr("err %d\n", err);
35650 +}
35651 +
35652 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35653 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35654 +{
35655 +       int wkq_err;
35656 +       struct super_block *sb;
35657 +
35658 +       IMustLock(dir);
35659 +
35660 +       /* all post-process will be done in do_rmdir_whtmp(). */
35661 +       sb = dir->i_sb;
35662 +       args->dir = au_igrab(dir);
35663 +       args->br = au_sbr(sb, bindex);
35664 +       au_br_get(args->br);
35665 +       args->wh_dentry = dget(wh_dentry);
35666 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35667 +       if (unlikely(wkq_err)) {
35668 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35669 +               au_whtmp_rmdir_free(args);
35670 +       }
35671 +}
35672 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35673 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35674 +++ linux/fs/aufs/whout.h       2018-07-19 09:46:13.063314675 +0200
35675 @@ -0,0 +1,86 @@
35676 +/* SPDX-License-Identifier: GPL-2.0 */
35677 +/*
35678 + * Copyright (C) 2005-2018 Junjiro R. Okajima
35679 + *
35680 + * This program, aufs is free software; you can redistribute it and/or modify
35681 + * it under the terms of the GNU General Public License as published by
35682 + * the Free Software Foundation; either version 2 of the License, or
35683 + * (at your option) any later version.
35684 + *
35685 + * This program is distributed in the hope that it will be useful,
35686 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35687 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35688 + * GNU General Public License for more details.
35689 + *
35690 + * You should have received a copy of the GNU General Public License
35691 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35692 + */
35693 +
35694 +/*
35695 + * whiteout for logical deletion and opaque directory
35696 + */
35697 +
35698 +#ifndef __AUFS_WHOUT_H__
35699 +#define __AUFS_WHOUT_H__
35700 +
35701 +#ifdef __KERNEL__
35702 +
35703 +#include "dir.h"
35704 +
35705 +/* whout.c */
35706 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35707 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35708 +int au_diropq_test(struct dentry *h_dentry);
35709 +struct au_branch;
35710 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35711 +                            struct qstr *prefix);
35712 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35713 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35714 +                       struct dentry *dentry);
35715 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35716 +
35717 +/* diropq flags */
35718 +#define AuDiropq_CREATE        1
35719 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35720 +#define au_fset_diropq(flags, name) \
35721 +       do { (flags) |= AuDiropq_##name; } while (0)
35722 +#define au_fclr_diropq(flags, name) \
35723 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35724 +
35725 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35726 +                            unsigned int flags);
35727 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35728 +                         struct au_branch *br);
35729 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35730 +                           struct dentry *h_parent);
35731 +
35732 +/* real rmdir for the whiteout-ed dir */
35733 +struct au_whtmp_rmdir {
35734 +       struct inode *dir;
35735 +       struct au_branch *br;
35736 +       struct dentry *wh_dentry;
35737 +       struct au_nhash whlist;
35738 +};
35739 +
35740 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35741 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35742 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35743 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35744 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35745 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35746 +
35747 +/* ---------------------------------------------------------------------- */
35748 +
35749 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35750 +                                             aufs_bindex_t bindex)
35751 +{
35752 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35753 +}
35754 +
35755 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35756 +{
35757 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35758 +}
35759 +
35760 +#endif /* __KERNEL__ */
35761 +#endif /* __AUFS_WHOUT_H__ */
35762 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35763 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35764 +++ linux/fs/aufs/wkq.c 2018-07-19 09:46:13.063314675 +0200
35765 @@ -0,0 +1,391 @@
35766 +/* SPDX-License-Identifier: GPL-2.0 */
35767 +/*
35768 + * Copyright (C) 2005-2018 Junjiro R. Okajima
35769 + *
35770 + * This program, aufs is free software; you can redistribute it and/or modify
35771 + * it under the terms of the GNU General Public License as published by
35772 + * the Free Software Foundation; either version 2 of the License, or
35773 + * (at your option) any later version.
35774 + *
35775 + * This program is distributed in the hope that it will be useful,
35776 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35777 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35778 + * GNU General Public License for more details.
35779 + *
35780 + * You should have received a copy of the GNU General Public License
35781 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35782 + */
35783 +
35784 +/*
35785 + * workqueue for asynchronous/super-io operations
35786 + * todo: try new dredential scheme
35787 + */
35788 +
35789 +#include <linux/module.h>
35790 +#include "aufs.h"
35791 +
35792 +/* internal workqueue named AUFS_WKQ_NAME */
35793 +
35794 +static struct workqueue_struct *au_wkq;
35795 +
35796 +struct au_wkinfo {
35797 +       struct work_struct wk;
35798 +       struct kobject *kobj;
35799 +
35800 +       unsigned int flags; /* see wkq.h */
35801 +
35802 +       au_wkq_func_t func;
35803 +       void *args;
35804 +
35805 +#ifdef CONFIG_LOCKDEP
35806 +       int dont_check;
35807 +       struct held_lock **hlock;
35808 +#endif
35809 +
35810 +       struct completion *comp;
35811 +};
35812 +
35813 +/* ---------------------------------------------------------------------- */
35814 +/*
35815 + * Aufs passes some operations to the workqueue such as the internal copyup.
35816 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35817 + * job run by workqueue depends upon the locks acquired in the other task.
35818 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35819 + * information too. And the job in the workqueue restores the info in order to
35820 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35821 + * correctly and expectedly.
35822 + */
35823 +
35824 +#ifndef CONFIG_LOCKDEP
35825 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35826 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35827 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35828 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35829 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35830 +#else
35831 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35832 +{
35833 +       wkinfo->hlock = NULL;
35834 +       wkinfo->dont_check = 0;
35835 +}
35836 +
35837 +/*
35838 + * 1: matched
35839 + * 0: unmatched
35840 + */
35841 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35842 +{
35843 +       static DEFINE_SPINLOCK(spin);
35844 +       static struct {
35845 +               char *name;
35846 +               struct lock_class_key *key;
35847 +       } a[] = {
35848 +               { .name = "&sbinfo->si_rwsem" },
35849 +               { .name = "&finfo->fi_rwsem" },
35850 +               { .name = "&dinfo->di_rwsem" },
35851 +               { .name = "&iinfo->ii_rwsem" }
35852 +       };
35853 +       static int set;
35854 +       int i;
35855 +
35856 +       /* lockless read from 'set.' see below */
35857 +       if (set == ARRAY_SIZE(a)) {
35858 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35859 +                       if (a[i].key == key)
35860 +                               goto match;
35861 +               goto unmatch;
35862 +       }
35863 +
35864 +       spin_lock(&spin);
35865 +       if (set)
35866 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35867 +                       if (a[i].key == key) {
35868 +                               spin_unlock(&spin);
35869 +                               goto match;
35870 +                       }
35871 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
35872 +               if (a[i].key) {
35873 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
35874 +                               spin_unlock(&spin);
35875 +                               goto match;
35876 +                       } else
35877 +                               continue;
35878 +               }
35879 +               if (strstr(a[i].name, name)) {
35880 +                       /*
35881 +                        * the order of these three lines is important for the
35882 +                        * lockless read above.
35883 +                        */
35884 +                       a[i].key = key;
35885 +                       spin_unlock(&spin);
35886 +                       set++;
35887 +                       /* AuDbg("%d, %s\n", set, name); */
35888 +                       goto match;
35889 +               }
35890 +       }
35891 +       spin_unlock(&spin);
35892 +       goto unmatch;
35893 +
35894 +match:
35895 +       return 1;
35896 +unmatch:
35897 +       return 0;
35898 +}
35899 +
35900 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35901 +{
35902 +       int err, n;
35903 +       struct task_struct *curr;
35904 +       struct held_lock **hl, *held_locks, *p;
35905 +
35906 +       err = 0;
35907 +       curr = current;
35908 +       wkinfo->dont_check = lockdep_recursing(curr);
35909 +       if (wkinfo->dont_check)
35910 +               goto out;
35911 +       n = curr->lockdep_depth;
35912 +       if (!n)
35913 +               goto out;
35914 +
35915 +       err = -ENOMEM;
35916 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35917 +       if (unlikely(!wkinfo->hlock))
35918 +               goto out;
35919 +
35920 +       err = 0;
35921 +#if 0
35922 +       if (0 && au_debug_test()) /* left for debugging */
35923 +               lockdep_print_held_locks(curr);
35924 +#endif
35925 +       held_locks = curr->held_locks;
35926 +       hl = wkinfo->hlock;
35927 +       while (n--) {
35928 +               p = held_locks++;
35929 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35930 +                       *hl++ = p;
35931 +       }
35932 +       *hl = NULL;
35933 +
35934 +out:
35935 +       return err;
35936 +}
35937 +
35938 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35939 +{
35940 +       kfree(wkinfo->hlock);
35941 +}
35942 +
35943 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
35944 +{
35945 +       struct held_lock *p, **hl = wkinfo->hlock;
35946 +       int subclass;
35947 +
35948 +       if (wkinfo->dont_check)
35949 +               lockdep_off();
35950 +       if (!hl)
35951 +               return;
35952 +       while ((p = *hl++)) { /* assignment */
35953 +               subclass = lockdep_hlock_class(p)->subclass;
35954 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
35955 +               if (p->read)
35956 +                       rwsem_acquire_read(p->instance, subclass, 0,
35957 +                                          /*p->acquire_ip*/_RET_IP_);
35958 +               else
35959 +                       rwsem_acquire(p->instance, subclass, 0,
35960 +                                     /*p->acquire_ip*/_RET_IP_);
35961 +       }
35962 +}
35963 +
35964 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
35965 +{
35966 +       struct held_lock *p, **hl = wkinfo->hlock;
35967 +
35968 +       if (wkinfo->dont_check)
35969 +               lockdep_on();
35970 +       if (!hl)
35971 +               return;
35972 +       while ((p = *hl++)) /* assignment */
35973 +               rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
35974 +}
35975 +#endif
35976 +
35977 +static void wkq_func(struct work_struct *wk)
35978 +{
35979 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
35980 +
35981 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
35982 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
35983 +
35984 +       au_wkq_lockdep_pre(wkinfo);
35985 +       wkinfo->func(wkinfo->args);
35986 +       au_wkq_lockdep_post(wkinfo);
35987 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
35988 +               complete(wkinfo->comp);
35989 +       else {
35990 +               kobject_put(wkinfo->kobj);
35991 +               module_put(THIS_MODULE); /* todo: ?? */
35992 +               kfree(wkinfo);
35993 +       }
35994 +}
35995 +
35996 +/*
35997 + * Since struct completion is large, try allocating it dynamically.
35998 + */
35999 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
36000 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36001 +
36002 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36003 +{
36004 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36005 +       if (*comp) {
36006 +               init_completion(*comp);
36007 +               wkinfo->comp = *comp;
36008 +               return 0;
36009 +       }
36010 +       return -ENOMEM;
36011 +}
36012 +
36013 +static void au_wkq_comp_free(struct completion *comp)
36014 +{
36015 +       kfree(comp);
36016 +}
36017 +
36018 +#else
36019 +
36020 +/* no braces */
36021 +#define AuWkqCompDeclare(name) \
36022 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
36023 +       struct completion *comp = &_ ## name
36024 +
36025 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36026 +{
36027 +       wkinfo->comp = *comp;
36028 +       return 0;
36029 +}
36030 +
36031 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36032 +{
36033 +       /* empty */
36034 +}
36035 +#endif /* 4KSTACKS */
36036 +
36037 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36038 +{
36039 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36040 +               if (au_wkq_test()) {
36041 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36042 +                               " due to a dead dir by UDBA?\n");
36043 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36044 +               }
36045 +       } else
36046 +               au_dbg_verify_kthread();
36047 +
36048 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36049 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36050 +               queue_work(au_wkq, &wkinfo->wk);
36051 +       } else {
36052 +               INIT_WORK(&wkinfo->wk, wkq_func);
36053 +               schedule_work(&wkinfo->wk);
36054 +       }
36055 +}
36056 +
36057 +/*
36058 + * Be careful. It is easy to make deadlock happen.
36059 + * processA: lock, wkq and wait
36060 + * processB: wkq and wait, lock in wkq
36061 + * --> deadlock
36062 + */
36063 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36064 +{
36065 +       int err;
36066 +       AuWkqCompDeclare(comp);
36067 +       struct au_wkinfo wkinfo = {
36068 +               .flags  = flags,
36069 +               .func   = func,
36070 +               .args   = args
36071 +       };
36072 +
36073 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36074 +       if (unlikely(err))
36075 +               goto out;
36076 +       err = au_wkq_lockdep_alloc(&wkinfo);
36077 +       if (unlikely(err))
36078 +               goto out_comp;
36079 +       if (!err) {
36080 +               au_wkq_run(&wkinfo);
36081 +               /* no timeout, no interrupt */
36082 +               wait_for_completion(wkinfo.comp);
36083 +       }
36084 +       au_wkq_lockdep_free(&wkinfo);
36085 +
36086 +out_comp:
36087 +       au_wkq_comp_free(comp);
36088 +out:
36089 +       destroy_work_on_stack(&wkinfo.wk);
36090 +       return err;
36091 +}
36092 +
36093 +/*
36094 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36095 + * problem in a concurrent umounting.
36096 + */
36097 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36098 +                 unsigned int flags)
36099 +{
36100 +       int err;
36101 +       struct au_wkinfo *wkinfo;
36102 +
36103 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36104 +
36105 +       /*
36106 +        * wkq_func() must free this wkinfo.
36107 +        * it highly depends upon the implementation of workqueue.
36108 +        */
36109 +       err = 0;
36110 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36111 +       if (wkinfo) {
36112 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36113 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36114 +               wkinfo->func = func;
36115 +               wkinfo->args = args;
36116 +               wkinfo->comp = NULL;
36117 +               au_wkq_lockdep_init(wkinfo);
36118 +               kobject_get(wkinfo->kobj);
36119 +               __module_get(THIS_MODULE); /* todo: ?? */
36120 +
36121 +               au_wkq_run(wkinfo);
36122 +       } else {
36123 +               err = -ENOMEM;
36124 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36125 +       }
36126 +
36127 +       return err;
36128 +}
36129 +
36130 +/* ---------------------------------------------------------------------- */
36131 +
36132 +void au_nwt_init(struct au_nowait_tasks *nwt)
36133 +{
36134 +       atomic_set(&nwt->nw_len, 0);
36135 +       /* smp_mb(); */ /* atomic_set */
36136 +       init_waitqueue_head(&nwt->nw_wq);
36137 +}
36138 +
36139 +void au_wkq_fin(void)
36140 +{
36141 +       destroy_workqueue(au_wkq);
36142 +}
36143 +
36144 +int __init au_wkq_init(void)
36145 +{
36146 +       int err;
36147 +
36148 +       err = 0;
36149 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36150 +       if (IS_ERR(au_wkq))
36151 +               err = PTR_ERR(au_wkq);
36152 +       else if (!au_wkq)
36153 +               err = -ENOMEM;
36154 +
36155 +       return err;
36156 +}
36157 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36158 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36159 +++ linux/fs/aufs/wkq.h 2018-07-19 09:46:13.063314675 +0200
36160 @@ -0,0 +1,94 @@
36161 +/* SPDX-License-Identifier: GPL-2.0 */
36162 +/*
36163 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36164 + *
36165 + * This program, aufs is free software; you can redistribute it and/or modify
36166 + * it under the terms of the GNU General Public License as published by
36167 + * the Free Software Foundation; either version 2 of the License, or
36168 + * (at your option) any later version.
36169 + *
36170 + * This program is distributed in the hope that it will be useful,
36171 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36172 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36173 + * GNU General Public License for more details.
36174 + *
36175 + * You should have received a copy of the GNU General Public License
36176 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36177 + */
36178 +
36179 +/*
36180 + * workqueue for asynchronous/super-io operations
36181 + * todo: try new credentials management scheme
36182 + */
36183 +
36184 +#ifndef __AUFS_WKQ_H__
36185 +#define __AUFS_WKQ_H__
36186 +
36187 +#ifdef __KERNEL__
36188 +
36189 +#include <linux/wait.h>
36190 +
36191 +struct super_block;
36192 +
36193 +/* ---------------------------------------------------------------------- */
36194 +
36195 +/*
36196 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36197 + */
36198 +struct au_nowait_tasks {
36199 +       atomic_t                nw_len;
36200 +       wait_queue_head_t       nw_wq;
36201 +};
36202 +
36203 +/* ---------------------------------------------------------------------- */
36204 +
36205 +typedef void (*au_wkq_func_t)(void *args);
36206 +
36207 +/* wkq flags */
36208 +#define AuWkq_WAIT     1
36209 +#define AuWkq_NEST     (1 << 1)
36210 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36211 +#define au_fset_wkq(flags, name) \
36212 +       do { (flags) |= AuWkq_##name; } while (0)
36213 +#define au_fclr_wkq(flags, name) \
36214 +       do { (flags) &= ~AuWkq_##name; } while (0)
36215 +
36216 +#ifndef CONFIG_AUFS_HNOTIFY
36217 +#undef AuWkq_NEST
36218 +#define AuWkq_NEST     0
36219 +#endif
36220 +
36221 +/* wkq.c */
36222 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36223 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36224 +                 unsigned int flags);
36225 +void au_nwt_init(struct au_nowait_tasks *nwt);
36226 +int __init au_wkq_init(void);
36227 +void au_wkq_fin(void);
36228 +
36229 +/* ---------------------------------------------------------------------- */
36230 +
36231 +static inline int au_wkq_test(void)
36232 +{
36233 +       return current->flags & PF_WQ_WORKER;
36234 +}
36235 +
36236 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36237 +{
36238 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36239 +}
36240 +
36241 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36242 +{
36243 +       if (atomic_dec_and_test(&nwt->nw_len))
36244 +               wake_up_all(&nwt->nw_wq);
36245 +}
36246 +
36247 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36248 +{
36249 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36250 +       return 0;
36251 +}
36252 +
36253 +#endif /* __KERNEL__ */
36254 +#endif /* __AUFS_WKQ_H__ */
36255 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36256 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36257 +++ linux/fs/aufs/xattr.c       2018-07-19 09:46:13.066648108 +0200
36258 @@ -0,0 +1,356 @@
36259 +/* SPDX-License-Identifier: GPL-2.0 */
36260 +/*
36261 + * Copyright (C) 2014-2018 Junjiro R. Okajima
36262 + *
36263 + * This program, aufs is free software; you can redistribute it and/or modify
36264 + * it under the terms of the GNU General Public License as published by
36265 + * the Free Software Foundation; either version 2 of the License, or
36266 + * (at your option) any later version.
36267 + *
36268 + * This program is distributed in the hope that it will be useful,
36269 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36270 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36271 + * GNU General Public License for more details.
36272 + *
36273 + * You should have received a copy of the GNU General Public License
36274 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36275 + */
36276 +
36277 +/*
36278 + * handling xattr functions
36279 + */
36280 +
36281 +#include <linux/fs.h>
36282 +#include <linux/posix_acl_xattr.h>
36283 +#include <linux/xattr.h>
36284 +#include "aufs.h"
36285 +
36286 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36287 +{
36288 +       if (!ignore_flags)
36289 +               goto out;
36290 +       switch (err) {
36291 +       case -ENOMEM:
36292 +       case -EDQUOT:
36293 +               goto out;
36294 +       }
36295 +
36296 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36297 +               err = 0;
36298 +               goto out;
36299 +       }
36300 +
36301 +#define cmp(brattr, prefix) do {                                       \
36302 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36303 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36304 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36305 +                               err = 0;                                \
36306 +                       goto out;                                       \
36307 +               }                                                       \
36308 +       } while (0)
36309 +
36310 +       cmp(SEC, SECURITY);
36311 +       cmp(SYS, SYSTEM);
36312 +       cmp(TR, TRUSTED);
36313 +       cmp(USR, USER);
36314 +#undef cmp
36315 +
36316 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36317 +               err = 0;
36318 +
36319 +out:
36320 +       return err;
36321 +}
36322 +
36323 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36324 +
36325 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36326 +                           char *name, char **buf, unsigned int ignore_flags,
36327 +                           unsigned int verbose)
36328 +{
36329 +       int err;
36330 +       ssize_t ssz;
36331 +       struct inode *h_idst;
36332 +
36333 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36334 +       err = ssz;
36335 +       if (unlikely(err <= 0)) {
36336 +               if (err == -ENODATA
36337 +                   || (err == -EOPNOTSUPP
36338 +                       && ((ignore_flags & au_xattr_out_of_list)
36339 +                           || (au_test_nfs_noacl(d_inode(h_src))
36340 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36341 +                                   || !strcmp(name,
36342 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36343 +                           ))
36344 +                       err = 0;
36345 +               if (err && (verbose || au_debug_test()))
36346 +                       pr_err("%s, err %d\n", name, err);
36347 +               goto out;
36348 +       }
36349 +
36350 +       /* unlock it temporary */
36351 +       h_idst = d_inode(h_dst);
36352 +       inode_unlock(h_idst);
36353 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36354 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36355 +       if (unlikely(err)) {
36356 +               if (verbose || au_debug_test())
36357 +                       pr_err("%s, err %d\n", name, err);
36358 +               err = au_xattr_ignore(err, name, ignore_flags);
36359 +       }
36360 +
36361 +out:
36362 +       return err;
36363 +}
36364 +
36365 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36366 +                 unsigned int verbose)
36367 +{
36368 +       int err, unlocked, acl_access, acl_default;
36369 +       ssize_t ssz;
36370 +       struct inode *h_isrc, *h_idst;
36371 +       char *value, *p, *o, *e;
36372 +
36373 +       /* try stopping to update the source inode while we are referencing */
36374 +       /* there should not be the parent-child relationship between them */
36375 +       h_isrc = d_inode(h_src);
36376 +       h_idst = d_inode(h_dst);
36377 +       inode_unlock(h_idst);
36378 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36379 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36380 +       unlocked = 0;
36381 +
36382 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36383 +       ssz = vfs_listxattr(h_src, NULL, 0);
36384 +       err = ssz;
36385 +       if (unlikely(err < 0)) {
36386 +               AuTraceErr(err);
36387 +               if (err == -ENODATA
36388 +                   || err == -EOPNOTSUPP)
36389 +                       err = 0;        /* ignore */
36390 +               goto out;
36391 +       }
36392 +
36393 +       err = 0;
36394 +       p = NULL;
36395 +       o = NULL;
36396 +       if (ssz) {
36397 +               err = -ENOMEM;
36398 +               p = kmalloc(ssz, GFP_NOFS);
36399 +               o = p;
36400 +               if (unlikely(!p))
36401 +                       goto out;
36402 +               err = vfs_listxattr(h_src, p, ssz);
36403 +       }
36404 +       inode_unlock_shared(h_isrc);
36405 +       unlocked = 1;
36406 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36407 +       if (unlikely(err < 0))
36408 +               goto out_free;
36409 +
36410 +       err = 0;
36411 +       e = p + ssz;
36412 +       value = NULL;
36413 +       acl_access = 0;
36414 +       acl_default = 0;
36415 +       while (!err && p < e) {
36416 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36417 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36418 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36419 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36420 +                                       - 1);
36421 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36422 +                                      verbose);
36423 +               p += strlen(p) + 1;
36424 +       }
36425 +       AuTraceErr(err);
36426 +       ignore_flags |= au_xattr_out_of_list;
36427 +       if (!err && !acl_access) {
36428 +               err = au_do_cpup_xattr(h_dst, h_src,
36429 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36430 +                                      ignore_flags, verbose);
36431 +               AuTraceErr(err);
36432 +       }
36433 +       if (!err && !acl_default) {
36434 +               err = au_do_cpup_xattr(h_dst, h_src,
36435 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36436 +                                      ignore_flags, verbose);
36437 +               AuTraceErr(err);
36438 +       }
36439 +
36440 +       kfree(value);
36441 +
36442 +out_free:
36443 +       kfree(o);
36444 +out:
36445 +       if (!unlocked)
36446 +               inode_unlock_shared(h_isrc);
36447 +       AuTraceErr(err);
36448 +       return err;
36449 +}
36450 +
36451 +/* ---------------------------------------------------------------------- */
36452 +
36453 +static int au_smack_reentering(struct super_block *sb)
36454 +{
36455 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36456 +       /*
36457 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36458 +        * i_op->getxattr(). ouch.
36459 +        */
36460 +       return si_pid_test(sb);
36461 +#else
36462 +       return 0;
36463 +#endif
36464 +}
36465 +
36466 +enum {
36467 +       AU_XATTR_LIST,
36468 +       AU_XATTR_GET
36469 +};
36470 +
36471 +struct au_lgxattr {
36472 +       int type;
36473 +       union {
36474 +               struct {
36475 +                       char    *list;
36476 +                       size_t  size;
36477 +               } list;
36478 +               struct {
36479 +                       const char      *name;
36480 +                       void            *value;
36481 +                       size_t          size;
36482 +               } get;
36483 +       } u;
36484 +};
36485 +
36486 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36487 +{
36488 +       ssize_t err;
36489 +       int reenter;
36490 +       struct path h_path;
36491 +       struct super_block *sb;
36492 +
36493 +       sb = dentry->d_sb;
36494 +       reenter = au_smack_reentering(sb);
36495 +       if (!reenter) {
36496 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36497 +               if (unlikely(err))
36498 +                       goto out;
36499 +       }
36500 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
36501 +       if (unlikely(err))
36502 +               goto out_si;
36503 +       if (unlikely(!h_path.dentry))
36504 +               /* illegally overlapped or something */
36505 +               goto out_di; /* pretending success */
36506 +
36507 +       /* always topmost entry only */
36508 +       switch (arg->type) {
36509 +       case AU_XATTR_LIST:
36510 +               err = vfs_listxattr(h_path.dentry,
36511 +                                   arg->u.list.list, arg->u.list.size);
36512 +               break;
36513 +       case AU_XATTR_GET:
36514 +               AuDebugOn(d_is_negative(h_path.dentry));
36515 +               err = vfs_getxattr(h_path.dentry,
36516 +                                  arg->u.get.name, arg->u.get.value,
36517 +                                  arg->u.get.size);
36518 +               break;
36519 +       }
36520 +
36521 +out_di:
36522 +       if (!reenter)
36523 +               di_read_unlock(dentry, AuLock_IR);
36524 +out_si:
36525 +       if (!reenter)
36526 +               si_read_unlock(sb);
36527 +out:
36528 +       AuTraceErr(err);
36529 +       return err;
36530 +}
36531 +
36532 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36533 +{
36534 +       struct au_lgxattr arg = {
36535 +               .type = AU_XATTR_LIST,
36536 +               .u.list = {
36537 +                       .list   = list,
36538 +                       .size   = size
36539 +               },
36540 +       };
36541 +
36542 +       return au_lgxattr(dentry, &arg);
36543 +}
36544 +
36545 +static ssize_t au_getxattr(struct dentry *dentry,
36546 +                          struct inode *inode __maybe_unused,
36547 +                          const char *name, void *value, size_t size)
36548 +{
36549 +       struct au_lgxattr arg = {
36550 +               .type = AU_XATTR_GET,
36551 +               .u.get = {
36552 +                       .name   = name,
36553 +                       .value  = value,
36554 +                       .size   = size
36555 +               },
36556 +       };
36557 +
36558 +       return au_lgxattr(dentry, &arg);
36559 +}
36560 +
36561 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36562 +                      const char *name, const void *value, size_t size,
36563 +                      int flags)
36564 +{
36565 +       struct au_sxattr arg = {
36566 +               .type = AU_XATTR_SET,
36567 +               .u.set = {
36568 +                       .name   = name,
36569 +                       .value  = value,
36570 +                       .size   = size,
36571 +                       .flags  = flags
36572 +               },
36573 +       };
36574 +
36575 +       return au_sxattr(dentry, inode, &arg);
36576 +}
36577 +
36578 +/* ---------------------------------------------------------------------- */
36579 +
36580 +static int au_xattr_get(const struct xattr_handler *handler,
36581 +                       struct dentry *dentry, struct inode *inode,
36582 +                       const char *name, void *buffer, size_t size)
36583 +{
36584 +       return au_getxattr(dentry, inode, name, buffer, size);
36585 +}
36586 +
36587 +static int au_xattr_set(const struct xattr_handler *handler,
36588 +                       struct dentry *dentry, struct inode *inode,
36589 +                       const char *name, const void *value, size_t size,
36590 +                       int flags)
36591 +{
36592 +       return au_setxattr(dentry, inode, name, value, size, flags);
36593 +}
36594 +
36595 +static const struct xattr_handler au_xattr_handler = {
36596 +       .name   = "",
36597 +       .prefix = "",
36598 +       .get    = au_xattr_get,
36599 +       .set    = au_xattr_set
36600 +};
36601 +
36602 +static const struct xattr_handler *au_xattr_handlers[] = {
36603 +#ifdef CONFIG_FS_POSIX_ACL
36604 +       &posix_acl_access_xattr_handler,
36605 +       &posix_acl_default_xattr_handler,
36606 +#endif
36607 +       &au_xattr_handler, /* must be last */
36608 +       NULL
36609 +};
36610 +
36611 +void au_xattr_init(struct super_block *sb)
36612 +{
36613 +       sb->s_xattr = au_xattr_handlers;
36614 +}
36615 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36616 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36617 +++ linux/fs/aufs/xino.c        2018-07-19 09:46:13.066648108 +0200
36618 @@ -0,0 +1,1593 @@
36619 +/* SPDX-License-Identifier: GPL-2.0 */
36620 +/*
36621 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36622 + *
36623 + * This program, aufs is free software; you can redistribute it and/or modify
36624 + * it under the terms of the GNU General Public License as published by
36625 + * the Free Software Foundation; either version 2 of the License, or
36626 + * (at your option) any later version.
36627 + *
36628 + * This program is distributed in the hope that it will be useful,
36629 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36630 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36631 + * GNU General Public License for more details.
36632 + *
36633 + * You should have received a copy of the GNU General Public License
36634 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36635 + */
36636 +
36637 +/*
36638 + * external inode number translation table and bitmap
36639 + *
36640 + * things to consider
36641 + * - the lifetime
36642 + *   + au_xino object
36643 + *   + XINO files (xino, xib, xigen)
36644 + *   + dynamic debugfs entries (xiN)
36645 + *   + static debugfs entries (xib, xigen)
36646 + *   + static sysfs entry (xi_path)
36647 + * - several entry points to handle them.
36648 + *   + mount(2) without xino option (default)
36649 + *   + mount(2) with xino option
36650 + *   + mount(2) with noxino option
36651 + *   + umount(2)
36652 + *   + remount with add/del branches
36653 + *   + remount with xino/noxino options
36654 + */
36655 +
36656 +#include <linux/seq_file.h>
36657 +#include <linux/statfs.h>
36658 +#include "aufs.h"
36659 +
36660 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36661 +                                    aufs_bindex_t bbot,
36662 +                                    struct super_block *h_sb)
36663 +{
36664 +       /* todo: try binary-search if the branches are many */
36665 +       for (; btop <= bbot; btop++)
36666 +               if (h_sb == au_sbr_sb(sb, btop))
36667 +                       return btop;
36668 +       return -1;
36669 +}
36670 +
36671 +/*
36672 + * find another branch who is on the same filesystem of the specified
36673 + * branch{@btgt}. search until @bbot.
36674 + */
36675 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36676 +                                 aufs_bindex_t bbot)
36677 +{
36678 +       aufs_bindex_t bindex;
36679 +       struct super_block *tgt_sb;
36680 +
36681 +       tgt_sb = au_sbr_sb(sb, btgt);
36682 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36683 +       if (bindex < 0)
36684 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36685 +
36686 +       return bindex;
36687 +}
36688 +
36689 +/* ---------------------------------------------------------------------- */
36690 +
36691 +/*
36692 + * stop unnecessary notify events at creating xino files
36693 + */
36694 +struct au_xino_lock_dir {
36695 +       struct au_hinode *hdir;
36696 +       struct dentry *parent;
36697 +       struct inode *dir;
36698 +};
36699 +
36700 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36701 +                                         unsigned int lsc)
36702 +{
36703 +       struct dentry *parent;
36704 +       struct inode *dir;
36705 +
36706 +       parent = dget_parent(dentry);
36707 +       dir = d_inode(parent);
36708 +       inode_lock_nested(dir, lsc);
36709 +#if 0 /* it should not happen */
36710 +       spin_lock(&dentry->d_lock);
36711 +       if (unlikely(dentry->d_parent != parent)) {
36712 +               spin_unlock(&dentry->d_lock);
36713 +               inode_unlock(dir);
36714 +               dput(parent);
36715 +               parent = NULL;
36716 +               goto out;
36717 +       }
36718 +       spin_unlock(&dentry->d_lock);
36719 +
36720 +out:
36721 +#endif
36722 +       return parent;
36723 +}
36724 +
36725 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36726 +                            struct au_xino_lock_dir *ldir)
36727 +{
36728 +       aufs_bindex_t brid, bindex;
36729 +
36730 +       ldir->hdir = NULL;
36731 +       bindex = -1;
36732 +       brid = au_xino_brid(sb);
36733 +       if (brid >= 0)
36734 +               bindex = au_br_index(sb, brid);
36735 +       if (bindex >= 0) {
36736 +               /* rw branch root */
36737 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36738 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36739 +       } else {
36740 +               /* other */
36741 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36742 +                                                  AuLsc_I_PARENT);
36743 +               ldir->dir = d_inode(ldir->parent);
36744 +       }
36745 +}
36746 +
36747 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36748 +{
36749 +       if (ldir->hdir)
36750 +               au_hn_inode_unlock(ldir->hdir);
36751 +       else {
36752 +               inode_unlock(ldir->dir);
36753 +               dput(ldir->parent);
36754 +       }
36755 +}
36756 +
36757 +/* ---------------------------------------------------------------------- */
36758 +
36759 +/*
36760 + * create and set a new xino file
36761 + */
36762 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
36763 +{
36764 +       struct file *file;
36765 +       struct dentry *h_parent, *d;
36766 +       struct inode *h_dir, *inode;
36767 +       int err;
36768 +
36769 +       /*
36770 +        * at mount-time, and the xino file is the default path,
36771 +        * hnotify is disabled so we have no notify events to ignore.
36772 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36773 +        */
36774 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36775 +                              /* | __FMODE_NONOTIFY */,
36776 +                              S_IRUGO | S_IWUGO);
36777 +       if (IS_ERR(file)) {
36778 +               if (!silent)
36779 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36780 +               return file;
36781 +       }
36782 +
36783 +       /* keep file count */
36784 +       err = 0;
36785 +       d = file->f_path.dentry;
36786 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36787 +       /* mnt_want_write() is unnecessary here */
36788 +       h_dir = d_inode(h_parent);
36789 +       inode = file_inode(file);
36790 +       /* no delegation since it is just created */
36791 +       if (inode->i_nlink)
36792 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36793 +                                  /*force*/0);
36794 +       inode_unlock(h_dir);
36795 +       dput(h_parent);
36796 +       if (unlikely(err)) {
36797 +               if (!silent)
36798 +                       pr_err("unlink %s(%d)\n", fpath, err);
36799 +               goto out;
36800 +       }
36801 +
36802 +       err = -EINVAL;
36803 +       if (unlikely(sb == d->d_sb)) {
36804 +               if (!silent)
36805 +                       pr_err("%s must be outside\n", fpath);
36806 +               goto out;
36807 +       }
36808 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36809 +               if (!silent)
36810 +                       pr_err("xino doesn't support %s(%s)\n",
36811 +                              fpath, au_sbtype(d->d_sb));
36812 +               goto out;
36813 +       }
36814 +       return file; /* success */
36815 +
36816 +out:
36817 +       fput(file);
36818 +       file = ERR_PTR(err);
36819 +       return file;
36820 +}
36821 +
36822 +/*
36823 + * create a new xinofile at the same place/path as @base.
36824 + */
36825 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36826 +                            struct file *copy_src)
36827 +{
36828 +       struct file *file;
36829 +       struct dentry *dentry, *parent;
36830 +       struct inode *dir, *delegated;
36831 +       struct qstr *name;
36832 +       struct path path;
36833 +       int err, do_unlock;
36834 +       struct au_xino_lock_dir ldir;
36835 +
36836 +       do_unlock = 1;
36837 +       au_xino_lock_dir(sb, base, &ldir);
36838 +       dentry = base->dentry;
36839 +       parent = dentry->d_parent; /* dir inode is locked */
36840 +       dir = d_inode(parent);
36841 +       IMustLock(dir);
36842 +
36843 +       name = &dentry->d_name;
36844 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36845 +       if (IS_ERR(path.dentry)) {
36846 +               file = (void *)path.dentry;
36847 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36848 +               goto out;
36849 +       }
36850 +
36851 +       /* no need to mnt_want_write() since we call dentry_open() later */
36852 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
36853 +       if (unlikely(err)) {
36854 +               file = ERR_PTR(err);
36855 +               pr_err("%pd create err %d\n", dentry, err);
36856 +               goto out_dput;
36857 +       }
36858 +
36859 +       path.mnt = base->mnt;
36860 +       file = vfsub_dentry_open(&path,
36861 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36862 +                                /* | __FMODE_NONOTIFY */);
36863 +       if (IS_ERR(file)) {
36864 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36865 +               goto out_dput;
36866 +       }
36867 +
36868 +       delegated = NULL;
36869 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36870 +       au_xino_unlock_dir(&ldir);
36871 +       do_unlock = 0;
36872 +       if (unlikely(err == -EWOULDBLOCK)) {
36873 +               pr_warn("cannot retry for NFSv4 delegation"
36874 +                       " for an internal unlink\n");
36875 +               iput(delegated);
36876 +       }
36877 +       if (unlikely(err)) {
36878 +               pr_err("%pd unlink err %d\n", dentry, err);
36879 +               goto out_fput;
36880 +       }
36881 +
36882 +       if (copy_src) {
36883 +               /* no one can touch copy_src xino */
36884 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36885 +               if (unlikely(err)) {
36886 +                       pr_err("%pd copy err %d\n", dentry, err);
36887 +                       goto out_fput;
36888 +               }
36889 +       }
36890 +       goto out_dput; /* success */
36891 +
36892 +out_fput:
36893 +       fput(file);
36894 +       file = ERR_PTR(err);
36895 +out_dput:
36896 +       dput(path.dentry);
36897 +out:
36898 +       if (do_unlock)
36899 +               au_xino_unlock_dir(&ldir);
36900 +       return file;
36901 +}
36902 +
36903 +/* ---------------------------------------------------------------------- */
36904 +
36905 +/*
36906 + * truncate xino files
36907 + */
36908 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
36909 +{
36910 +       int err;
36911 +       unsigned long jiffy;
36912 +       blkcnt_t blocks;
36913 +       aufs_bindex_t bbot;
36914 +       struct kstatfs *st;
36915 +       struct au_branch *br;
36916 +       struct file *new_xino, *file;
36917 +       struct path *path;
36918 +
36919 +       err = -ENOMEM;
36920 +       st = kmalloc(sizeof(*st), GFP_NOFS);
36921 +       if (unlikely(!st))
36922 +               goto out;
36923 +
36924 +       err = -EINVAL;
36925 +       bbot = au_sbbot(sb);
36926 +       if (unlikely(bindex < 0 || bbot < bindex))
36927 +               goto out_st;
36928 +       br = au_sbr(sb, bindex);
36929 +       file = au_xino_file(br);
36930 +       if (!file)
36931 +               goto out_st;
36932 +
36933 +       path = &file->f_path;
36934 +       err = vfs_statfs(path, st);
36935 +       if (unlikely(err))
36936 +               AuErr1("statfs err %d, ignored\n", err);
36937 +       jiffy = jiffies;
36938 +       blocks = file_inode(file)->i_blocks;
36939 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36940 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
36941 +
36942 +       new_xino = au_xino_create2(sb, path, file);
36943 +       err = PTR_ERR(new_xino);
36944 +       if (IS_ERR(new_xino)) {
36945 +               pr_err("err %d, ignored\n", err);
36946 +               goto out_st;
36947 +       }
36948 +       err = 0;
36949 +       au_xino_file_set(br, new_xino);
36950 +
36951 +       err = vfs_statfs(&new_xino->f_path, st);
36952 +       if (!err) {
36953 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
36954 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
36955 +                       st->f_bfree, st->f_blocks);
36956 +               if (file_inode(new_xino)->i_blocks < blocks)
36957 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
36958 +       } else
36959 +               AuErr1("statfs err %d, ignored\n", err);
36960 +       fput(new_xino);
36961 +
36962 +out_st:
36963 +       kfree(st);
36964 +out:
36965 +       return err;
36966 +}
36967 +
36968 +struct xino_do_trunc_args {
36969 +       struct super_block *sb;
36970 +       struct au_branch *br;
36971 +};
36972 +
36973 +static void xino_do_trunc(void *_args)
36974 +{
36975 +       struct xino_do_trunc_args *args = _args;
36976 +       struct super_block *sb;
36977 +       struct au_branch *br;
36978 +       struct inode *dir;
36979 +       int err;
36980 +       aufs_bindex_t bindex;
36981 +
36982 +       err = 0;
36983 +       sb = args->sb;
36984 +       dir = d_inode(sb->s_root);
36985 +       br = args->br;
36986 +
36987 +       si_noflush_write_lock(sb);
36988 +       ii_read_lock_parent(dir);
36989 +       bindex = au_br_index(sb, br->br_id);
36990 +       err = au_xino_trunc(sb, bindex);
36991 +       ii_read_unlock(dir);
36992 +       if (unlikely(err))
36993 +               pr_warn("err b%d, (%d)\n", bindex, err);
36994 +       atomic_dec(&br->br_xino->xi_truncating);
36995 +       au_br_put(br);
36996 +       si_write_unlock(sb);
36997 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36998 +       kfree(args);
36999 +}
37000 +
37001 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37002 +{
37003 +       int err;
37004 +       struct kstatfs st;
37005 +       struct au_sbinfo *sbinfo;
37006 +       struct file *file;
37007 +
37008 +       /* todo: si_xino_expire and the ratio should be customizable */
37009 +       sbinfo = au_sbi(sb);
37010 +       if (time_before(jiffies,
37011 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37012 +               return 0;
37013 +
37014 +       /* truncation border */
37015 +       file = au_xino_file(br);
37016 +       AuDebugOn(!file);
37017 +       err = vfs_statfs(&file->f_path, &st);
37018 +       if (unlikely(err)) {
37019 +               AuErr1("statfs err %d, ignored\n", err);
37020 +               return 0;
37021 +       }
37022 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
37023 +               return 0;
37024 +
37025 +       return 1;
37026 +}
37027 +
37028 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37029 +{
37030 +       struct xino_do_trunc_args *args;
37031 +       int wkq_err;
37032 +
37033 +       if (!xino_trunc_test(sb, br))
37034 +               return;
37035 +
37036 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37037 +               goto out;
37038 +
37039 +       /* lock and kfree() will be called in trunc_xino() */
37040 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37041 +       if (unlikely(!args)) {
37042 +               AuErr1("no memory\n");
37043 +               goto out;
37044 +       }
37045 +
37046 +       au_br_get(br);
37047 +       args->sb = sb;
37048 +       args->br = br;
37049 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37050 +       if (!wkq_err)
37051 +               return; /* success */
37052 +
37053 +       pr_err("wkq %d\n", wkq_err);
37054 +       au_br_put(br);
37055 +       kfree(args);
37056 +
37057 +out:
37058 +       atomic_dec(&br->br_xino->xi_truncating);
37059 +}
37060 +
37061 +/* ---------------------------------------------------------------------- */
37062 +
37063 +/*
37064 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37065 + * at the position of @h_ino.
37066 + */
37067 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37068 +                ino_t *ino)
37069 +{
37070 +       int err;
37071 +       ssize_t sz;
37072 +       loff_t pos;
37073 +       struct au_branch *br;
37074 +       struct file *file;
37075 +       struct au_sbinfo *sbinfo;
37076 +
37077 +       *ino = 0;
37078 +       if (!au_opt_test(au_mntflags(sb), XINO))
37079 +               return 0; /* no xino */
37080 +
37081 +       err = 0;
37082 +       sbinfo = au_sbi(sb);
37083 +       pos = h_ino;
37084 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
37085 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37086 +               return -EFBIG;
37087 +       }
37088 +       pos *= sizeof(*ino);
37089 +
37090 +       br = au_sbr(sb, bindex);
37091 +       file = au_xino_file(br);
37092 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
37093 +               return 0; /* no ino */
37094 +
37095 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
37096 +       if (sz == sizeof(*ino))
37097 +               return 0; /* success */
37098 +
37099 +       err = sz;
37100 +       if (unlikely(sz >= 0)) {
37101 +               err = -EIO;
37102 +               AuIOErr("xino read error (%zd)\n", sz);
37103 +       }
37104 +
37105 +       return err;
37106 +}
37107 +
37108 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37109 +                           ino_t h_ino, ino_t ino)
37110 +{
37111 +       loff_t pos;
37112 +       ssize_t sz;
37113 +
37114 +       pos = h_ino;
37115 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
37116 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
37117 +               return -EFBIG;
37118 +       }
37119 +       pos *= sizeof(ino);
37120 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
37121 +       if (sz == sizeof(ino))
37122 +               return 0; /* success */
37123 +
37124 +       AuIOErr("write failed (%zd)\n", sz);
37125 +       return -EIO;
37126 +}
37127 +
37128 +/*
37129 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37130 + * at the position of @h_ino.
37131 + * even if @ino is zero, it is written to the xinofile and means no entry.
37132 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37133 + * try truncating it.
37134 + */
37135 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37136 +                 ino_t ino)
37137 +{
37138 +       int err;
37139 +       unsigned int mnt_flags;
37140 +       struct au_branch *br;
37141 +
37142 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
37143 +                    || ((loff_t)-1) > 0);
37144 +       SiMustAnyLock(sb);
37145 +
37146 +       mnt_flags = au_mntflags(sb);
37147 +       if (!au_opt_test(mnt_flags, XINO))
37148 +               return 0;
37149 +
37150 +       br = au_sbr(sb, bindex);
37151 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, au_xino_file(br), h_ino,
37152 +                              ino);
37153 +       if (!err) {
37154 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37155 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37156 +                       xino_try_trunc(sb, br);
37157 +               return 0; /* success */
37158 +       }
37159 +
37160 +       AuIOErr("write failed (%d)\n", err);
37161 +       return -EIO;
37162 +}
37163 +
37164 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37165 +                             size_t size, loff_t *pos);
37166 +
37167 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37168 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37169 +                  loff_t *pos)
37170 +{
37171 +       ssize_t err;
37172 +       mm_segment_t oldfs;
37173 +       union {
37174 +               void *k;
37175 +               char __user *u;
37176 +       } buf;
37177 +       int i;
37178 +       const int prevent_endless = 10;
37179 +
37180 +       i = 0;
37181 +       buf.k = kbuf;
37182 +       oldfs = get_fs();
37183 +       set_fs(KERNEL_DS);
37184 +       do {
37185 +               err = func(file, buf.u, size, pos);
37186 +               if (err == -EINTR
37187 +                   && !au_wkq_test()
37188 +                   && fatal_signal_pending(current)) {
37189 +                       set_fs(oldfs);
37190 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37191 +                       BUG_ON(err == -EINTR);
37192 +                       oldfs = get_fs();
37193 +                       set_fs(KERNEL_DS);
37194 +               }
37195 +       } while (i++ < prevent_endless
37196 +                && (err == -EAGAIN || err == -EINTR));
37197 +       set_fs(oldfs);
37198 +
37199 +#if 0 /* reserved for future use */
37200 +       if (err > 0)
37201 +               fsnotify_access(file->f_path.dentry);
37202 +#endif
37203 +
37204 +       return err;
37205 +}
37206 +
37207 +struct xino_fread_args {
37208 +       ssize_t *errp;
37209 +       vfs_readf_t func;
37210 +       struct file *file;
37211 +       void *buf;
37212 +       size_t size;
37213 +       loff_t *pos;
37214 +};
37215 +
37216 +static void call_xino_fread(void *args)
37217 +{
37218 +       struct xino_fread_args *a = args;
37219 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37220 +}
37221 +
37222 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37223 +                             size_t size, loff_t *pos)
37224 +{
37225 +       ssize_t err;
37226 +       int wkq_err;
37227 +       struct xino_fread_args args = {
37228 +               .errp   = &err,
37229 +               .func   = func,
37230 +               .file   = file,
37231 +               .buf    = buf,
37232 +               .size   = size,
37233 +               .pos    = pos
37234 +       };
37235 +
37236 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37237 +       if (unlikely(wkq_err))
37238 +               err = wkq_err;
37239 +
37240 +       return err;
37241 +}
37242 +
37243 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37244 +                              size_t size, loff_t *pos);
37245 +
37246 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37247 +                             size_t size, loff_t *pos)
37248 +{
37249 +       ssize_t err;
37250 +       mm_segment_t oldfs;
37251 +       union {
37252 +               void *k;
37253 +               const char __user *u;
37254 +       } buf;
37255 +       int i;
37256 +       const int prevent_endless = 10;
37257 +
37258 +       i = 0;
37259 +       buf.k = kbuf;
37260 +       oldfs = get_fs();
37261 +       set_fs(KERNEL_DS);
37262 +       do {
37263 +               err = func(file, buf.u, size, pos);
37264 +               if (err == -EINTR
37265 +                   && !au_wkq_test()
37266 +                   && fatal_signal_pending(current)) {
37267 +                       set_fs(oldfs);
37268 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37269 +                       BUG_ON(err == -EINTR);
37270 +                       oldfs = get_fs();
37271 +                       set_fs(KERNEL_DS);
37272 +               }
37273 +       } while (i++ < prevent_endless
37274 +                && (err == -EAGAIN || err == -EINTR));
37275 +       set_fs(oldfs);
37276 +
37277 +#if 0 /* reserved for future use */
37278 +       if (err > 0)
37279 +               fsnotify_modify(file->f_path.dentry);
37280 +#endif
37281 +
37282 +       return err;
37283 +}
37284 +
37285 +struct do_xino_fwrite_args {
37286 +       ssize_t *errp;
37287 +       vfs_writef_t func;
37288 +       struct file *file;
37289 +       void *buf;
37290 +       size_t size;
37291 +       loff_t *pos;
37292 +};
37293 +
37294 +static void call_do_xino_fwrite(void *args)
37295 +{
37296 +       struct do_xino_fwrite_args *a = args;
37297 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37298 +}
37299 +
37300 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37301 +                              size_t size, loff_t *pos)
37302 +{
37303 +       ssize_t err;
37304 +       int wkq_err;
37305 +       struct do_xino_fwrite_args args = {
37306 +               .errp   = &err,
37307 +               .func   = func,
37308 +               .file   = file,
37309 +               .buf    = buf,
37310 +               .size   = size,
37311 +               .pos    = pos
37312 +       };
37313 +
37314 +       /*
37315 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37316 +        * users should care about quota and real 'filesystem full.'
37317 +        */
37318 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37319 +       if (unlikely(wkq_err))
37320 +               err = wkq_err;
37321 +
37322 +       return err;
37323 +}
37324 +
37325 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37326 +                   size_t size, loff_t *pos)
37327 +{
37328 +       ssize_t err;
37329 +
37330 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37331 +               lockdep_off();
37332 +               err = do_xino_fwrite(func, file, buf, size, pos);
37333 +               lockdep_on();
37334 +       } else {
37335 +               lockdep_off();
37336 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37337 +               lockdep_on();
37338 +       }
37339 +
37340 +       return err;
37341 +}
37342 +
37343 +/* ---------------------------------------------------------------------- */
37344 +
37345 +/*
37346 + * inode number bitmap
37347 + */
37348 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37349 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37350 +{
37351 +       ino_t ino;
37352 +
37353 +       AuDebugOn(bit < 0 || page_bits <= bit);
37354 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37355 +       return ino;
37356 +}
37357 +
37358 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37359 +{
37360 +       AuDebugOn(ino < AUFS_FIRST_INO);
37361 +       ino -= AUFS_FIRST_INO;
37362 +       *pindex = ino / page_bits;
37363 +       *bit = ino % page_bits;
37364 +}
37365 +
37366 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37367 +{
37368 +       int err;
37369 +       loff_t pos;
37370 +       ssize_t sz;
37371 +       struct au_sbinfo *sbinfo;
37372 +       struct file *xib;
37373 +       unsigned long *p;
37374 +
37375 +       sbinfo = au_sbi(sb);
37376 +       MtxMustLock(&sbinfo->si_xib_mtx);
37377 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37378 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37379 +
37380 +       if (pindex == sbinfo->si_xib_last_pindex)
37381 +               return 0;
37382 +
37383 +       xib = sbinfo->si_xib;
37384 +       p = sbinfo->si_xib_buf;
37385 +       pos = sbinfo->si_xib_last_pindex;
37386 +       pos *= PAGE_SIZE;
37387 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37388 +       if (unlikely(sz != PAGE_SIZE))
37389 +               goto out;
37390 +
37391 +       pos = pindex;
37392 +       pos *= PAGE_SIZE;
37393 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37394 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37395 +       else {
37396 +               memset(p, 0, PAGE_SIZE);
37397 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37398 +       }
37399 +       if (sz == PAGE_SIZE) {
37400 +               sbinfo->si_xib_last_pindex = pindex;
37401 +               return 0; /* success */
37402 +       }
37403 +
37404 +out:
37405 +       AuIOErr1("write failed (%zd)\n", sz);
37406 +       err = sz;
37407 +       if (sz >= 0)
37408 +               err = -EIO;
37409 +       return err;
37410 +}
37411 +
37412 +static void au_xib_clear_bit(struct inode *inode)
37413 +{
37414 +       int err, bit;
37415 +       unsigned long pindex;
37416 +       struct super_block *sb;
37417 +       struct au_sbinfo *sbinfo;
37418 +
37419 +       AuDebugOn(inode->i_nlink);
37420 +
37421 +       sb = inode->i_sb;
37422 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37423 +       AuDebugOn(page_bits <= bit);
37424 +       sbinfo = au_sbi(sb);
37425 +       mutex_lock(&sbinfo->si_xib_mtx);
37426 +       err = xib_pindex(sb, pindex);
37427 +       if (!err) {
37428 +               clear_bit(bit, sbinfo->si_xib_buf);
37429 +               sbinfo->si_xib_next_bit = bit;
37430 +       }
37431 +       mutex_unlock(&sbinfo->si_xib_mtx);
37432 +}
37433 +
37434 +/* ---------------------------------------------------------------------- */
37435 +
37436 +/*
37437 + * truncate a xino bitmap file
37438 + */
37439 +
37440 +/* todo: slow */
37441 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37442 +{
37443 +       int err, bit;
37444 +       ssize_t sz;
37445 +       unsigned long pindex;
37446 +       loff_t pos, pend;
37447 +       struct au_sbinfo *sbinfo;
37448 +       vfs_readf_t func;
37449 +       ino_t *ino;
37450 +       unsigned long *p;
37451 +
37452 +       err = 0;
37453 +       sbinfo = au_sbi(sb);
37454 +       MtxMustLock(&sbinfo->si_xib_mtx);
37455 +       p = sbinfo->si_xib_buf;
37456 +       func = sbinfo->si_xread;
37457 +       pend = vfsub_f_size_read(file);
37458 +       pos = 0;
37459 +       while (pos < pend) {
37460 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37461 +               err = sz;
37462 +               if (unlikely(sz <= 0))
37463 +                       goto out;
37464 +
37465 +               err = 0;
37466 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37467 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37468 +                               continue;
37469 +
37470 +                       xib_calc_bit(*ino, &pindex, &bit);
37471 +                       AuDebugOn(page_bits <= bit);
37472 +                       err = xib_pindex(sb, pindex);
37473 +                       if (!err)
37474 +                               set_bit(bit, p);
37475 +                       else
37476 +                               goto out;
37477 +               }
37478 +       }
37479 +
37480 +out:
37481 +       return err;
37482 +}
37483 +
37484 +static int xib_restore(struct super_block *sb)
37485 +{
37486 +       int err;
37487 +       aufs_bindex_t bindex, bbot;
37488 +       void *page;
37489 +       struct au_branch *br;
37490 +
37491 +       err = -ENOMEM;
37492 +       page = (void *)__get_free_page(GFP_NOFS);
37493 +       if (unlikely(!page))
37494 +               goto out;
37495 +
37496 +       err = 0;
37497 +       bbot = au_sbbot(sb);
37498 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37499 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37500 +                       br = au_sbr(sb, bindex);
37501 +                       err = do_xib_restore(sb, au_xino_file(br), page);
37502 +               } else
37503 +                       AuDbg("skip shared b%d\n", bindex);
37504 +       free_page((unsigned long)page);
37505 +
37506 +out:
37507 +       return err;
37508 +}
37509 +
37510 +int au_xib_trunc(struct super_block *sb)
37511 +{
37512 +       int err;
37513 +       ssize_t sz;
37514 +       loff_t pos;
37515 +       struct au_sbinfo *sbinfo;
37516 +       unsigned long *p;
37517 +       struct file *file;
37518 +
37519 +       SiMustWriteLock(sb);
37520 +
37521 +       err = 0;
37522 +       sbinfo = au_sbi(sb);
37523 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37524 +               goto out;
37525 +
37526 +       file = sbinfo->si_xib;
37527 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37528 +               goto out;
37529 +
37530 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37531 +       err = PTR_ERR(file);
37532 +       if (IS_ERR(file))
37533 +               goto out;
37534 +       fput(sbinfo->si_xib);
37535 +       sbinfo->si_xib = file;
37536 +
37537 +       p = sbinfo->si_xib_buf;
37538 +       memset(p, 0, PAGE_SIZE);
37539 +       pos = 0;
37540 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
37541 +       if (unlikely(sz != PAGE_SIZE)) {
37542 +               err = sz;
37543 +               AuIOErr("err %d\n", err);
37544 +               if (sz >= 0)
37545 +                       err = -EIO;
37546 +               goto out;
37547 +       }
37548 +
37549 +       mutex_lock(&sbinfo->si_xib_mtx);
37550 +       /* mnt_want_write() is unnecessary here */
37551 +       err = xib_restore(sb);
37552 +       mutex_unlock(&sbinfo->si_xib_mtx);
37553 +
37554 +out:
37555 +       return err;
37556 +}
37557 +
37558 +/* ---------------------------------------------------------------------- */
37559 +
37560 +struct au_xino *au_xino_alloc(void)
37561 +{
37562 +       struct au_xino *xi;
37563 +
37564 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
37565 +       if (unlikely(!xi))
37566 +               goto out;
37567 +
37568 +       xi->xi_nondir.total = 8; /* initial size */
37569 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37570 +                                     GFP_NOFS);
37571 +       if (unlikely(!xi->xi_nondir.array))
37572 +               goto out_free;
37573 +
37574 +       spin_lock_init(&xi->xi_nondir.spin);
37575 +       init_waitqueue_head(&xi->xi_nondir.wqh);
37576 +       atomic_set(&xi->xi_truncating, 0);
37577 +       kref_init(&xi->xi_kref);
37578 +       goto out; /* success */
37579 +
37580 +out_free:
37581 +       kfree(xi);
37582 +       xi = NULL;
37583 +out:
37584 +       return xi;
37585 +}
37586 +
37587 +static int au_xino_init(struct au_branch *br, struct file *file)
37588 +{
37589 +       int err;
37590 +       struct au_xino *xi;
37591 +
37592 +       err = 0;
37593 +       xi = au_xino_alloc();
37594 +       if (unlikely(!xi)) {
37595 +               err = -ENOMEM;
37596 +               goto out;
37597 +       }
37598 +
37599 +       get_file(file);
37600 +       xi->xi_file = file;
37601 +       AuDebugOn(br->br_xino);
37602 +       br->br_xino = xi;
37603 +
37604 +out:
37605 +       return err;
37606 +}
37607 +
37608 +static void au_xino_release(struct kref *kref)
37609 +{
37610 +       struct au_xino *xi;
37611 +       int i;
37612 +
37613 +       xi = container_of(kref, struct au_xino, xi_kref);
37614 +       if (xi->xi_file)
37615 +               fput(xi->xi_file);
37616 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37617 +               AuDebugOn(xi->xi_nondir.array[i]);
37618 +       kfree(xi->xi_nondir.array);
37619 +       kfree(xi);
37620 +}
37621 +
37622 +int au_xino_put(struct au_branch *br)
37623 +{
37624 +       int ret;
37625 +       struct au_xino *xi;
37626 +
37627 +       ret = 0;
37628 +       xi = br->br_xino;
37629 +       if (xi) {
37630 +               br->br_xino = NULL;
37631 +               ret = kref_put(&xi->xi_kref, au_xino_release);
37632 +       }
37633 +
37634 +       return ret;
37635 +}
37636 +
37637 +void au_xino_file_set(struct au_branch *br, struct file *file)
37638 +{
37639 +       struct au_xino *xi;
37640 +       struct file *f;
37641 +
37642 +       if (file)
37643 +               get_file(file);
37644 +       xi = br->br_xino;
37645 +       AuDebugOn(!xi);
37646 +       f = xi->xi_file;
37647 +       if (f)
37648 +               fput(f);
37649 +       xi->xi_file = file;
37650 +}
37651 +
37652 +/* ---------------------------------------------------------------------- */
37653 +
37654 +/*
37655 + * xino mount option handlers
37656 + */
37657 +
37658 +/* xino bitmap */
37659 +static void xino_clear_xib(struct super_block *sb)
37660 +{
37661 +       struct au_sbinfo *sbinfo;
37662 +
37663 +       SiMustWriteLock(sb);
37664 +
37665 +       sbinfo = au_sbi(sb);
37666 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
37667 +       if (sbinfo->si_xib)
37668 +               fput(sbinfo->si_xib);
37669 +       sbinfo->si_xib = NULL;
37670 +       if (sbinfo->si_xib_buf)
37671 +               free_page((unsigned long)sbinfo->si_xib_buf);
37672 +       sbinfo->si_xib_buf = NULL;
37673 +}
37674 +
37675 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
37676 +{
37677 +       int err;
37678 +       loff_t pos;
37679 +       struct au_sbinfo *sbinfo;
37680 +       struct file *file;
37681 +
37682 +       SiMustWriteLock(sb);
37683 +
37684 +       sbinfo = au_sbi(sb);
37685 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
37686 +       err = PTR_ERR(file);
37687 +       if (IS_ERR(file))
37688 +               goto out;
37689 +       if (sbinfo->si_xib)
37690 +               fput(sbinfo->si_xib);
37691 +       sbinfo->si_xib = file;
37692 +       sbinfo->si_xread = vfs_readf(file);
37693 +       sbinfo->si_xwrite = vfs_writef(file);
37694 +
37695 +       err = -ENOMEM;
37696 +       if (!sbinfo->si_xib_buf)
37697 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
37698 +       if (unlikely(!sbinfo->si_xib_buf))
37699 +               goto out_unset;
37700 +
37701 +       sbinfo->si_xib_last_pindex = 0;
37702 +       sbinfo->si_xib_next_bit = 0;
37703 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
37704 +               pos = 0;
37705 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
37706 +                                 PAGE_SIZE, &pos);
37707 +               if (unlikely(err != PAGE_SIZE))
37708 +                       goto out_free;
37709 +       }
37710 +       err = 0;
37711 +       goto out; /* success */
37712 +
37713 +out_free:
37714 +       if (sbinfo->si_xib_buf)
37715 +               free_page((unsigned long)sbinfo->si_xib_buf);
37716 +       sbinfo->si_xib_buf = NULL;
37717 +       if (err >= 0)
37718 +               err = -EIO;
37719 +out_unset:
37720 +       fput(sbinfo->si_xib);
37721 +       sbinfo->si_xib = NULL;
37722 +out:
37723 +       AuTraceErr(err);
37724 +       return err;
37725 +}
37726 +
37727 +/* xino for each branch */
37728 +static void xino_clear_br(struct super_block *sb)
37729 +{
37730 +       aufs_bindex_t bindex, bbot;
37731 +       struct au_branch *br;
37732 +
37733 +       bbot = au_sbbot(sb);
37734 +       for (bindex = 0; bindex <= bbot; bindex++) {
37735 +               br = au_sbr(sb, bindex);
37736 +               AuDebugOn(!br);
37737 +               au_xino_put(br);
37738 +       }
37739 +}
37740 +
37741 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
37742 +                                 aufs_bindex_t bshared)
37743 +{
37744 +       struct au_branch *brshared;
37745 +
37746 +       brshared = au_sbr(sb, bshared);
37747 +       AuDebugOn(!brshared->br_xino);
37748 +       AuDebugOn(!brshared->br_xino->xi_file);
37749 +       if (br->br_xino != brshared->br_xino) {
37750 +               au_xino_get(brshared);
37751 +               au_xino_put(br);
37752 +               br->br_xino = brshared->br_xino;
37753 +       }
37754 +}
37755 +
37756 +struct au_xino_do_set_br {
37757 +       vfs_writef_t writef;
37758 +       struct au_branch *br;
37759 +       ino_t h_ino;
37760 +       aufs_bindex_t bshared;
37761 +};
37762 +
37763 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
37764 +                            struct au_xino_do_set_br *args)
37765 +{
37766 +       int err;
37767 +       struct file *file;
37768 +
37769 +       if (args->bshared >= 0) {
37770 +               /* shared xino */
37771 +               au_xino_set_br_shared(sb, args->br, args->bshared);
37772 +               file = au_xino_file(args->br);
37773 +               goto out_ino; /* success */
37774 +       }
37775 +
37776 +       /* new xino */
37777 +       file = au_xino_create2(sb, path, au_xino_file(args->br));
37778 +       err = PTR_ERR(file);
37779 +       if (IS_ERR(file))
37780 +               goto out;
37781 +       if (!args->br->br_xino) {
37782 +               err = au_xino_init(args->br, file);
37783 +               fput(file);
37784 +               if (unlikely(err))
37785 +                       goto out;
37786 +       } else {
37787 +               au_xino_file_set(args->br, file);
37788 +               fput(file);
37789 +       }
37790 +
37791 +out_ino:
37792 +       err = au_xino_do_write(args->writef, file, args->h_ino, AUFS_ROOT_INO);
37793 +out:
37794 +       AuTraceErr(err);
37795 +       return err;
37796 +}
37797 +
37798 +static int au_xino_set_br(struct super_block *sb, struct path *path)
37799 +{
37800 +       int err;
37801 +       aufs_bindex_t bindex, bbot;
37802 +       struct au_xino_do_set_br args;
37803 +       struct inode *inode;
37804 +
37805 +       SiMustWriteLock(sb);
37806 +
37807 +       bbot = au_sbbot(sb);
37808 +       inode = d_inode(sb->s_root);
37809 +       args.writef = au_sbi(sb)->si_xwrite;
37810 +       for (bindex = 0; bindex <= bbot; bindex++) {
37811 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
37812 +               args.br = au_sbr(sb, bindex);
37813 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
37814 +               err = au_xino_do_set_br(sb, path, &args);
37815 +               if (unlikely(err))
37816 +                       break;
37817 +       }
37818 +
37819 +       AuTraceErr(err);
37820 +       return err;
37821 +}
37822 +
37823 +void au_xino_clr(struct super_block *sb)
37824 +{
37825 +       struct au_sbinfo *sbinfo;
37826 +
37827 +       au_xigen_clr(sb);
37828 +       xino_clear_xib(sb);
37829 +       xino_clear_br(sb);
37830 +       dbgaufs_brs_del(sb, 0);
37831 +       au_xino_brid_set(sb, -1);
37832 +       sbinfo = au_sbi(sb);
37833 +       /* lvalue, do not call au_mntflags() */
37834 +       au_opt_clr(sbinfo->si_mntflags, XINO);
37835 +}
37836 +
37837 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
37838 +{
37839 +       int err, skip;
37840 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
37841 +       struct qstr *dname, *cur_name;
37842 +       struct file *cur_xino;
37843 +       struct au_sbinfo *sbinfo;
37844 +       struct path *path, *cur_path;
37845 +
37846 +       SiMustWriteLock(sb);
37847 +
37848 +       err = 0;
37849 +       sbinfo = au_sbi(sb);
37850 +       path = &xiopt->file->f_path;
37851 +       dentry = path->dentry;
37852 +       parent = dget_parent(dentry);
37853 +       if (remount) {
37854 +               skip = 0;
37855 +               cur_xino = sbinfo->si_xib;
37856 +               if (cur_xino) {
37857 +                       cur_path = &cur_xino->f_path;
37858 +                       cur_dentry = cur_path->dentry;
37859 +                       cur_parent = dget_parent(cur_dentry);
37860 +                       cur_name = &cur_dentry->d_name;
37861 +                       dname = &dentry->d_name;
37862 +                       skip = (cur_parent == parent
37863 +                               && au_qstreq(dname, cur_name));
37864 +                       dput(cur_parent);
37865 +               }
37866 +               if (skip)
37867 +                       goto out;
37868 +       }
37869 +
37870 +       au_opt_set(sbinfo->si_mntflags, XINO);
37871 +       err = au_xino_set_xib(sb, path);
37872 +       /* si_x{read,write} are set */
37873 +       if (!err)
37874 +               err = au_xigen_set(sb, path);
37875 +       if (!err)
37876 +               err = au_xino_set_br(sb, path);
37877 +       if (!err) {
37878 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
37879 +               goto out; /* success */
37880 +       }
37881 +
37882 +       /* reset all */
37883 +       AuIOErr("failed setting xino(%d).\n", err);
37884 +       au_xino_clr(sb);
37885 +
37886 +out:
37887 +       dput(parent);
37888 +       return err;
37889 +}
37890 +
37891 +/*
37892 + * create a xinofile at the default place/path.
37893 + */
37894 +struct file *au_xino_def(struct super_block *sb)
37895 +{
37896 +       struct file *file;
37897 +       char *page, *p;
37898 +       struct au_branch *br;
37899 +       struct super_block *h_sb;
37900 +       struct path path;
37901 +       aufs_bindex_t bbot, bindex, bwr;
37902 +
37903 +       br = NULL;
37904 +       bbot = au_sbbot(sb);
37905 +       bwr = -1;
37906 +       for (bindex = 0; bindex <= bbot; bindex++) {
37907 +               br = au_sbr(sb, bindex);
37908 +               if (au_br_writable(br->br_perm)
37909 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
37910 +                       bwr = bindex;
37911 +                       break;
37912 +               }
37913 +       }
37914 +
37915 +       if (bwr >= 0) {
37916 +               file = ERR_PTR(-ENOMEM);
37917 +               page = (void *)__get_free_page(GFP_NOFS);
37918 +               if (unlikely(!page))
37919 +                       goto out;
37920 +               path.mnt = au_br_mnt(br);
37921 +               path.dentry = au_h_dptr(sb->s_root, bwr);
37922 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
37923 +               file = (void *)p;
37924 +               if (!IS_ERR(p)) {
37925 +                       strcat(p, "/" AUFS_XINO_FNAME);
37926 +                       AuDbg("%s\n", p);
37927 +                       file = au_xino_create(sb, p, /*silent*/0);
37928 +                       if (!IS_ERR(file))
37929 +                               au_xino_brid_set(sb, br->br_id);
37930 +               }
37931 +               free_page((unsigned long)page);
37932 +       } else {
37933 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
37934 +               if (IS_ERR(file))
37935 +                       goto out;
37936 +               h_sb = file->f_path.dentry->d_sb;
37937 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
37938 +                       pr_err("xino doesn't support %s(%s)\n",
37939 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
37940 +                       fput(file);
37941 +                       file = ERR_PTR(-EINVAL);
37942 +               }
37943 +               if (!IS_ERR(file))
37944 +                       au_xino_brid_set(sb, -1);
37945 +       }
37946 +
37947 +out:
37948 +       return file;
37949 +}
37950 +
37951 +/* ---------------------------------------------------------------------- */
37952 +
37953 +/*
37954 + * initialize the xinofile for the specified branch @br
37955 + * at the place/path where @base_file indicates.
37956 + * test whether another branch is on the same filesystem or not,
37957 + * if found then share the xinofile with another branch.
37958 + */
37959 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
37960 +                   struct path *base)
37961 +{
37962 +       int err;
37963 +       struct au_xino_do_set_br args = {
37964 +               .h_ino  = h_ino,
37965 +               .br     = br
37966 +       };
37967 +
37968 +       args.writef = au_sbi(sb)->si_xwrite;
37969 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
37970 +                                      au_br_sb(br));
37971 +       err = au_xino_do_set_br(sb, base, &args);
37972 +       if (unlikely(err))
37973 +               au_xino_put(br);
37974 +
37975 +       return err;
37976 +}
37977 +
37978 +/* ---------------------------------------------------------------------- */
37979 +
37980 +/*
37981 + * get an unused inode number from bitmap
37982 + */
37983 +ino_t au_xino_new_ino(struct super_block *sb)
37984 +{
37985 +       ino_t ino;
37986 +       unsigned long *p, pindex, ul, pend;
37987 +       struct au_sbinfo *sbinfo;
37988 +       struct file *file;
37989 +       int free_bit, err;
37990 +
37991 +       if (!au_opt_test(au_mntflags(sb), XINO))
37992 +               return iunique(sb, AUFS_FIRST_INO);
37993 +
37994 +       sbinfo = au_sbi(sb);
37995 +       mutex_lock(&sbinfo->si_xib_mtx);
37996 +       p = sbinfo->si_xib_buf;
37997 +       free_bit = sbinfo->si_xib_next_bit;
37998 +       if (free_bit < page_bits && !test_bit(free_bit, p))
37999 +               goto out; /* success */
38000 +       free_bit = find_first_zero_bit(p, page_bits);
38001 +       if (free_bit < page_bits)
38002 +               goto out; /* success */
38003 +
38004 +       pindex = sbinfo->si_xib_last_pindex;
38005 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38006 +               err = xib_pindex(sb, ul);
38007 +               if (unlikely(err))
38008 +                       goto out_err;
38009 +               free_bit = find_first_zero_bit(p, page_bits);
38010 +               if (free_bit < page_bits)
38011 +                       goto out; /* success */
38012 +       }
38013 +
38014 +       file = sbinfo->si_xib;
38015 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38016 +       for (ul = pindex + 1; ul <= pend; ul++) {
38017 +               err = xib_pindex(sb, ul);
38018 +               if (unlikely(err))
38019 +                       goto out_err;
38020 +               free_bit = find_first_zero_bit(p, page_bits);
38021 +               if (free_bit < page_bits)
38022 +                       goto out; /* success */
38023 +       }
38024 +       BUG();
38025 +
38026 +out:
38027 +       set_bit(free_bit, p);
38028 +       sbinfo->si_xib_next_bit = free_bit + 1;
38029 +       pindex = sbinfo->si_xib_last_pindex;
38030 +       mutex_unlock(&sbinfo->si_xib_mtx);
38031 +       ino = xib_calc_ino(pindex, free_bit);
38032 +       AuDbg("i%lu\n", (unsigned long)ino);
38033 +       return ino;
38034 +out_err:
38035 +       mutex_unlock(&sbinfo->si_xib_mtx);
38036 +       AuDbg("i0\n");
38037 +       return 0;
38038 +}
38039 +
38040 +/* for s_op->delete_inode() */
38041 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38042 +{
38043 +       int err;
38044 +       unsigned int mnt_flags;
38045 +       aufs_bindex_t bindex, bbot, bi;
38046 +       unsigned char try_trunc;
38047 +       struct au_iinfo *iinfo;
38048 +       struct super_block *sb;
38049 +       struct au_hinode *hi;
38050 +       struct inode *h_inode;
38051 +       struct au_branch *br;
38052 +       vfs_writef_t xwrite;
38053 +
38054 +       AuDebugOn(au_is_bad_inode(inode));
38055 +
38056 +       sb = inode->i_sb;
38057 +       mnt_flags = au_mntflags(sb);
38058 +       if (!au_opt_test(mnt_flags, XINO)
38059 +           || inode->i_ino == AUFS_ROOT_INO)
38060 +               return;
38061 +
38062 +       if (unlinked) {
38063 +               au_xigen_inc(inode);
38064 +               au_xib_clear_bit(inode);
38065 +       }
38066 +
38067 +       iinfo = au_ii(inode);
38068 +       bindex = iinfo->ii_btop;
38069 +       if (bindex < 0)
38070 +               return;
38071 +
38072 +       xwrite = au_sbi(sb)->si_xwrite;
38073 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38074 +       hi = au_hinode(iinfo, bindex);
38075 +       bbot = iinfo->ii_bbot;
38076 +       for (; bindex <= bbot; bindex++, hi++) {
38077 +               h_inode = hi->hi_inode;
38078 +               if (!h_inode
38079 +                   || (!unlinked && h_inode->i_nlink))
38080 +                       continue;
38081 +
38082 +               /* inode may not be revalidated */
38083 +               bi = au_br_index(sb, hi->hi_id);
38084 +               if (bi < 0)
38085 +                       continue;
38086 +
38087 +               br = au_sbr(sb, bi);
38088 +               err = au_xino_do_write(xwrite, au_xino_file(br),
38089 +                                      h_inode->i_ino, /*ino*/0);
38090 +               if (!err && try_trunc
38091 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38092 +                       xino_try_trunc(sb, br);
38093 +       }
38094 +}
38095 +
38096 +/* ---------------------------------------------------------------------- */
38097 +
38098 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38099 +{
38100 +       int found, total, i;
38101 +
38102 +       found = -1;
38103 +       total = xi->xi_nondir.total;
38104 +       for (i = 0; i < total; i++) {
38105 +               if (xi->xi_nondir.array[i] != h_ino)
38106 +                       continue;
38107 +               found = i;
38108 +               break;
38109 +       }
38110 +
38111 +       return found;
38112 +}
38113 +
38114 +static int au_xinondir_expand(struct au_xino *xi)
38115 +{
38116 +       int err, sz;
38117 +       ino_t *p;
38118 +
38119 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38120 +
38121 +       err = -ENOMEM;
38122 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38123 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38124 +               goto out;
38125 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38126 +                        /*may_shrink*/0);
38127 +       if (p) {
38128 +               xi->xi_nondir.array = p;
38129 +               xi->xi_nondir.total <<= 1;
38130 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38131 +               err = 0;
38132 +       }
38133 +
38134 +out:
38135 +       return err;
38136 +}
38137 +
38138 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38139 +                      ino_t h_ino, int idx)
38140 +{
38141 +       struct au_xino *xi;
38142 +
38143 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38144 +       xi = au_sbr(sb, bindex)->br_xino;
38145 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38146 +
38147 +       spin_lock(&xi->xi_nondir.spin);
38148 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38149 +       xi->xi_nondir.array[idx] = 0;
38150 +       spin_unlock(&xi->xi_nondir.spin);
38151 +       wake_up_all(&xi->xi_nondir.wqh);
38152 +}
38153 +
38154 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38155 +                     int *idx)
38156 +{
38157 +       int err, found, empty;
38158 +       struct au_xino *xi;
38159 +
38160 +       err = 0;
38161 +       *idx = -1;
38162 +       if (!au_opt_test(au_mntflags(sb), XINO))
38163 +               goto out; /* no xino */
38164 +
38165 +       xi = au_sbr(sb, bindex)->br_xino;
38166 +
38167 +again:
38168 +       spin_lock(&xi->xi_nondir.spin);
38169 +       found = au_xinondir_find(xi, h_ino);
38170 +       if (found == -1) {
38171 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38172 +               if (empty == -1) {
38173 +                       empty = xi->xi_nondir.total;
38174 +                       err = au_xinondir_expand(xi);
38175 +                       if (unlikely(err))
38176 +                               goto out_unlock;
38177 +               }
38178 +               xi->xi_nondir.array[empty] = h_ino;
38179 +               *idx = empty;
38180 +       } else {
38181 +               spin_unlock(&xi->xi_nondir.spin);
38182 +               wait_event(xi->xi_nondir.wqh,
38183 +                          xi->xi_nondir.array[found] != h_ino);
38184 +               goto again;
38185 +       }
38186 +
38187 +out_unlock:
38188 +       spin_unlock(&xi->xi_nondir.spin);
38189 +out:
38190 +       return err;
38191 +}
38192 +
38193 +/* ---------------------------------------------------------------------- */
38194 +
38195 +int au_xino_path(struct seq_file *seq, struct file *file)
38196 +{
38197 +       int err;
38198 +
38199 +       err = au_seq_path(seq, &file->f_path);
38200 +       if (unlikely(err))
38201 +               goto out;
38202 +
38203 +#define Deleted "\\040(deleted)"
38204 +       seq->count -= sizeof(Deleted) - 1;
38205 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38206 +                        sizeof(Deleted) - 1));
38207 +#undef Deleted
38208 +
38209 +out:
38210 +       return err;
38211 +}
38212 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38213 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38214 +++ linux/include/uapi/linux/aufs_type.h        2018-07-19 09:46:13.066648108 +0200
38215 @@ -0,0 +1,448 @@
38216 +/* SPDX-License-Identifier: GPL-2.0 */
38217 +/*
38218 + * Copyright (C) 2005-2018 Junjiro R. Okajima
38219 + *
38220 + * This program, aufs is free software; you can redistribute it and/or modify
38221 + * it under the terms of the GNU General Public License as published by
38222 + * the Free Software Foundation; either version 2 of the License, or
38223 + * (at your option) any later version.
38224 + *
38225 + * This program is distributed in the hope that it will be useful,
38226 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38227 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38228 + * GNU General Public License for more details.
38229 + *
38230 + * You should have received a copy of the GNU General Public License
38231 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38232 + */
38233 +
38234 +#ifndef __AUFS_TYPE_H__
38235 +#define __AUFS_TYPE_H__
38236 +
38237 +#define AUFS_NAME      "aufs"
38238 +
38239 +#ifdef __KERNEL__
38240 +/*
38241 + * define it before including all other headers.
38242 + * sched.h may use pr_* macros before defining "current", so define the
38243 + * no-current version first, and re-define later.
38244 + */
38245 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38246 +#include <linux/sched.h>
38247 +#undef pr_fmt
38248 +#define pr_fmt(fmt) \
38249 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38250 +               (int)sizeof(current->comm), current->comm, current->pid
38251 +#else
38252 +#include <stdint.h>
38253 +#include <sys/types.h>
38254 +#endif /* __KERNEL__ */
38255 +
38256 +#include <linux/limits.h>
38257 +
38258 +#define AUFS_VERSION   "4.17-20180716"
38259 +
38260 +/* todo? move this to linux-2.6.19/include/magic.h */
38261 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38262 +
38263 +/* ---------------------------------------------------------------------- */
38264 +
38265 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38266 +typedef int8_t aufs_bindex_t;
38267 +#define AUFS_BRANCH_MAX 127
38268 +#else
38269 +typedef int16_t aufs_bindex_t;
38270 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38271 +#define AUFS_BRANCH_MAX 511
38272 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38273 +#define AUFS_BRANCH_MAX 1023
38274 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38275 +#define AUFS_BRANCH_MAX 32767
38276 +#endif
38277 +#endif
38278 +
38279 +#ifdef __KERNEL__
38280 +#ifndef AUFS_BRANCH_MAX
38281 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38282 +#endif
38283 +#endif /* __KERNEL__ */
38284 +
38285 +/* ---------------------------------------------------------------------- */
38286 +
38287 +#define AUFS_FSTYPE            AUFS_NAME
38288 +
38289 +#define AUFS_ROOT_INO          2
38290 +#define AUFS_FIRST_INO         11
38291 +
38292 +#define AUFS_WH_PFX            ".wh."
38293 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38294 +#define AUFS_WH_TMP_LEN                4
38295 +/* a limit for rmdir/rename a dir and copyup */
38296 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38297 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38298 +                               - 1                     /* dot */\
38299 +                               - AUFS_WH_TMP_LEN)      /* hex */
38300 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38301 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38302 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38303 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38304 +#define AUFS_DIRWH_DEF         3
38305 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38306 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38307 +#define AUFS_RDBLK_DEF         512 /* bytes */
38308 +#define AUFS_RDHASH_DEF                32
38309 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38310 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38311 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38312 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38313 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38314 +
38315 +/* pseudo-link maintenace under /proc */
38316 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38317 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38318 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38319 +
38320 +/* dirren, renamed dir */
38321 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38322 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38323 +/* whiteouted doubly */
38324 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38325 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38326 +
38327 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38328 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38329 +
38330 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38331 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38332 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38333 +
38334 +/* doubly whiteouted */
38335 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38336 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38337 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38338 +
38339 +/* branch permissions and attributes */
38340 +#define AUFS_BRPERM_RW         "rw"
38341 +#define AUFS_BRPERM_RO         "ro"
38342 +#define AUFS_BRPERM_RR         "rr"
38343 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38344 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38345 +#define AUFS_BRATTR_FHSM       "fhsm"
38346 +#define AUFS_BRATTR_UNPIN      "unpin"
38347 +#define AUFS_BRATTR_ICEX       "icex"
38348 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38349 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38350 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38351 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38352 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38353 +#define AUFS_BRRATTR_WH                "wh"
38354 +#define AUFS_BRWATTR_NLWH      "nolwh"
38355 +#define AUFS_BRWATTR_MOO       "moo"
38356 +
38357 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38358 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38359 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38360 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38361 +
38362 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38363 +#define AuBrAttr_COO_ALL       (1 << 4)
38364 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38365 +
38366 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38367 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38368 +                                                  branch. meaningless since
38369 +                                                  linux-3.18-rc1 */
38370 +
38371 +/* ignore error in copying XATTR */
38372 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38373 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38374 +#define AuBrAttr_ICEX_TR       (1 << 9)
38375 +#define AuBrAttr_ICEX_USR      (1 << 10)
38376 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38377 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38378 +                                | AuBrAttr_ICEX_SYS    \
38379 +                                | AuBrAttr_ICEX_TR     \
38380 +                                | AuBrAttr_ICEX_USR    \
38381 +                                | AuBrAttr_ICEX_OTH)
38382 +
38383 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38384 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38385 +
38386 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38387 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38388 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38389 +
38390 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38391 +
38392 +/* #warning test userspace */
38393 +#ifdef __KERNEL__
38394 +#ifndef CONFIG_AUFS_FHSM
38395 +#undef AuBrAttr_FHSM
38396 +#define AuBrAttr_FHSM          0
38397 +#endif
38398 +#ifndef CONFIG_AUFS_XATTR
38399 +#undef AuBrAttr_ICEX
38400 +#define AuBrAttr_ICEX          0
38401 +#undef AuBrAttr_ICEX_SEC
38402 +#define AuBrAttr_ICEX_SEC      0
38403 +#undef AuBrAttr_ICEX_SYS
38404 +#define AuBrAttr_ICEX_SYS      0
38405 +#undef AuBrAttr_ICEX_TR
38406 +#define AuBrAttr_ICEX_TR       0
38407 +#undef AuBrAttr_ICEX_USR
38408 +#define AuBrAttr_ICEX_USR      0
38409 +#undef AuBrAttr_ICEX_OTH
38410 +#define AuBrAttr_ICEX_OTH      0
38411 +#endif
38412 +#endif
38413 +
38414 +/* the longest combination */
38415 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38416 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38417 +                              "+" AUFS_BRATTR_COO_REG          \
38418 +                              "+" AUFS_BRATTR_FHSM             \
38419 +                              "+" AUFS_BRATTR_UNPIN            \
38420 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38421 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38422 +                              "+" AUFS_BRATTR_ICEX_USR         \
38423 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38424 +                              "+" AUFS_BRWATTR_NLWH)
38425 +
38426 +typedef struct {
38427 +       char a[AuBrPermStrSz];
38428 +} au_br_perm_str_t;
38429 +
38430 +static inline int au_br_writable(int brperm)
38431 +{
38432 +       return brperm & AuBrPerm_RW;
38433 +}
38434 +
38435 +static inline int au_br_whable(int brperm)
38436 +{
38437 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38438 +}
38439 +
38440 +static inline int au_br_wh_linkable(int brperm)
38441 +{
38442 +       return !(brperm & AuBrWAttr_NoLinkWH);
38443 +}
38444 +
38445 +static inline int au_br_cmoo(int brperm)
38446 +{
38447 +       return brperm & AuBrAttr_CMOO_Mask;
38448 +}
38449 +
38450 +static inline int au_br_fhsm(int brperm)
38451 +{
38452 +       return brperm & AuBrAttr_FHSM;
38453 +}
38454 +
38455 +/* ---------------------------------------------------------------------- */
38456 +
38457 +/* ioctl */
38458 +enum {
38459 +       /* readdir in userspace */
38460 +       AuCtl_RDU,
38461 +       AuCtl_RDU_INO,
38462 +
38463 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38464 +       AuCtl_IBUSY,    /* busy inode */
38465 +       AuCtl_MVDOWN,   /* move-down */
38466 +       AuCtl_BR,       /* info about branches */
38467 +       AuCtl_FHSM_FD   /* connection for fhsm */
38468 +};
38469 +
38470 +/* borrowed from linux/include/linux/kernel.h */
38471 +#ifndef ALIGN
38472 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38473 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38474 +#endif
38475 +
38476 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38477 +#ifndef __aligned
38478 +#define __aligned(x)                   __attribute__((aligned(x)))
38479 +#endif
38480 +
38481 +#ifdef __KERNEL__
38482 +#ifndef __packed
38483 +#define __packed                       __attribute__((packed))
38484 +#endif
38485 +#endif
38486 +
38487 +struct au_rdu_cookie {
38488 +       uint64_t        h_pos;
38489 +       int16_t         bindex;
38490 +       uint8_t         flags;
38491 +       uint8_t         pad;
38492 +       uint32_t        generation;
38493 +} __aligned(8);
38494 +
38495 +struct au_rdu_ent {
38496 +       uint64_t        ino;
38497 +       int16_t         bindex;
38498 +       uint8_t         type;
38499 +       uint8_t         nlen;
38500 +       uint8_t         wh;
38501 +       char            name[0];
38502 +} __aligned(8);
38503 +
38504 +static inline int au_rdu_len(int nlen)
38505 +{
38506 +       /* include the terminating NULL */
38507 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38508 +                    sizeof(uint64_t));
38509 +}
38510 +
38511 +union au_rdu_ent_ul {
38512 +       struct au_rdu_ent __user        *e;
38513 +       uint64_t                        ul;
38514 +};
38515 +
38516 +enum {
38517 +       AufsCtlRduV_SZ,
38518 +       AufsCtlRduV_End
38519 +};
38520 +
38521 +struct aufs_rdu {
38522 +       /* input */
38523 +       union {
38524 +               uint64_t        sz;     /* AuCtl_RDU */
38525 +               uint64_t        nent;   /* AuCtl_RDU_INO */
38526 +       };
38527 +       union au_rdu_ent_ul     ent;
38528 +       uint16_t                verify[AufsCtlRduV_End];
38529 +
38530 +       /* input/output */
38531 +       uint32_t                blk;
38532 +
38533 +       /* output */
38534 +       union au_rdu_ent_ul     tail;
38535 +       /* number of entries which were added in a single call */
38536 +       uint64_t                rent;
38537 +       uint8_t                 full;
38538 +       uint8_t                 shwh;
38539 +
38540 +       struct au_rdu_cookie    cookie;
38541 +} __aligned(8);
38542 +
38543 +/* ---------------------------------------------------------------------- */
38544 +
38545 +/* dirren. the branch is identified by the filename who contains this */
38546 +struct au_drinfo {
38547 +       uint64_t ino;
38548 +       union {
38549 +               uint8_t oldnamelen;
38550 +               uint64_t _padding;
38551 +       };
38552 +       uint8_t oldname[0];
38553 +} __aligned(8);
38554 +
38555 +struct au_drinfo_fdata {
38556 +       uint32_t magic;
38557 +       struct au_drinfo drinfo;
38558 +} __aligned(8);
38559 +
38560 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38561 +/* future */
38562 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38563 +
38564 +/* ---------------------------------------------------------------------- */
38565 +
38566 +struct aufs_wbr_fd {
38567 +       uint32_t        oflags;
38568 +       int16_t         brid;
38569 +} __aligned(8);
38570 +
38571 +/* ---------------------------------------------------------------------- */
38572 +
38573 +struct aufs_ibusy {
38574 +       uint64_t        ino, h_ino;
38575 +       int16_t         bindex;
38576 +} __aligned(8);
38577 +
38578 +/* ---------------------------------------------------------------------- */
38579 +
38580 +/* error code for move-down */
38581 +/* the actual message strings are implemented in aufs-util.git */
38582 +enum {
38583 +       EAU_MVDOWN_OPAQUE = 1,
38584 +       EAU_MVDOWN_WHITEOUT,
38585 +       EAU_MVDOWN_UPPER,
38586 +       EAU_MVDOWN_BOTTOM,
38587 +       EAU_MVDOWN_NOUPPER,
38588 +       EAU_MVDOWN_NOLOWERBR,
38589 +       EAU_Last
38590 +};
38591 +
38592 +/* flags for move-down */
38593 +#define AUFS_MVDOWN_DMSG       1
38594 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
38595 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
38596 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
38597 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
38598 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
38599 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
38600 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
38601 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
38602 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
38603 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
38604 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
38605 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
38606 +
38607 +/* index for move-down */
38608 +enum {
38609 +       AUFS_MVDOWN_UPPER,
38610 +       AUFS_MVDOWN_LOWER,
38611 +       AUFS_MVDOWN_NARRAY
38612 +};
38613 +
38614 +/*
38615 + * additional info of move-down
38616 + * number of free blocks and inodes.
38617 + * subset of struct kstatfs, but smaller and always 64bit.
38618 + */
38619 +struct aufs_stfs {
38620 +       uint64_t        f_blocks;
38621 +       uint64_t        f_bavail;
38622 +       uint64_t        f_files;
38623 +       uint64_t        f_ffree;
38624 +};
38625 +
38626 +struct aufs_stbr {
38627 +       int16_t                 brid;   /* optional input */
38628 +       int16_t                 bindex; /* output */
38629 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
38630 +} __aligned(8);
38631 +
38632 +struct aufs_mvdown {
38633 +       uint32_t                flags;                  /* input/output */
38634 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
38635 +       int8_t                  au_errno;               /* output */
38636 +} __aligned(8);
38637 +
38638 +/* ---------------------------------------------------------------------- */
38639 +
38640 +union aufs_brinfo {
38641 +       /* PATH_MAX may differ between kernel-space and user-space */
38642 +       char    _spacer[4096];
38643 +       struct {
38644 +               int16_t id;
38645 +               int     perm;
38646 +               char    path[0];
38647 +       };
38648 +} __aligned(8);
38649 +
38650 +/* ---------------------------------------------------------------------- */
38651 +
38652 +#define AuCtlType              'A'
38653 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
38654 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
38655 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
38656 +                                    struct aufs_wbr_fd)
38657 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
38658 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
38659 +                                     struct aufs_mvdown)
38660 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
38661 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
38662 +
38663 +#endif /* __AUFS_TYPE_H__ */
38664 SPDX-License-Identifier: GPL-2.0
38665 aufs4.17 loopback patch
38666
38667 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
38668 index bc965e5..852868a 100644
38669 --- a/drivers/block/loop.c
38670 +++ b/drivers/block/loop.c
38671 @@ -622,6 +622,15 @@ static inline void loop_update_dio(struct loop_device *lo)
38672                         lo->use_dio);
38673  }
38674  
38675 +static struct file *loop_real_file(struct file *file)
38676 +{
38677 +       struct file *f = NULL;
38678 +
38679 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
38680 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
38681 +       return f;
38682 +}
38683 +
38684  static void loop_reread_partitions(struct loop_device *lo,
38685                                    struct block_device *bdev)
38686  {
38687 @@ -656,6 +665,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38688                           unsigned int arg)
38689  {
38690         struct file     *file, *old_file;
38691 +       struct file     *f, *virt_file = NULL, *old_virt_file;
38692         struct inode    *inode;
38693         int             error;
38694  
38695 @@ -672,9 +682,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38696         error = loop_validate_file(file, bdev);
38697         if (error)
38698                 goto out_putf;
38699 +       f = loop_real_file(file);
38700 +       if (f) {
38701 +               virt_file = file;
38702 +               file = f;
38703 +               get_file(file);
38704 +       }
38705  
38706         inode = file->f_mapping->host;
38707         old_file = lo->lo_backing_file;
38708 +       old_virt_file = lo->lo_backing_virt_file;
38709  
38710         error = -EINVAL;
38711  
38712 @@ -689,6 +706,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38713         blk_mq_freeze_queue(lo->lo_queue);
38714         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
38715         lo->lo_backing_file = file;
38716 +       lo->lo_backing_virt_file = virt_file;
38717         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
38718         mapping_set_gfp_mask(file->f_mapping,
38719                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
38720 @@ -696,12 +714,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
38721         blk_mq_unfreeze_queue(lo->lo_queue);
38722  
38723         fput(old_file);
38724 +       if (old_virt_file)
38725 +               fput(old_virt_file);
38726         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
38727                 loop_reread_partitions(lo, bdev);
38728         return 0;
38729  
38730   out_putf:
38731         fput(file);
38732 +       if (virt_file)
38733 +               fput(virt_file);
38734   out:
38735         return error;
38736  }
38737 @@ -895,7 +917,7 @@ static int loop_prepare_queue(struct loop_device *lo)
38738  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38739                        struct block_device *bdev, unsigned int arg)
38740  {
38741 -       struct file     *file;
38742 +       struct file     *file, *f, *virt_file = NULL;
38743         struct inode    *inode;
38744         struct address_space *mapping;
38745         int             lo_flags = 0;
38746 @@ -909,6 +931,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38747         file = fget(arg);
38748         if (!file)
38749                 goto out;
38750 +       f = loop_real_file(file);
38751 +       if (f) {
38752 +               virt_file = file;
38753 +               file = f;
38754 +               get_file(file);
38755 +       }
38756  
38757         error = -EBUSY;
38758         if (lo->lo_state != Lo_unbound)
38759 @@ -957,6 +985,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38760         lo->lo_device = bdev;
38761         lo->lo_flags = lo_flags;
38762         lo->lo_backing_file = file;
38763 +       lo->lo_backing_virt_file = virt_file;
38764         lo->transfer = NULL;
38765         lo->ioctl = NULL;
38766         lo->lo_sizelimit = 0;
38767 @@ -990,6 +1019,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
38768  
38769   out_putf:
38770         fput(file);
38771 +       if (virt_file)
38772 +               fput(virt_file);
38773   out:
38774         /* This is safe: open() is still holding a reference. */
38775         module_put(THIS_MODULE);
38776 @@ -1036,6 +1067,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
38777  static int loop_clr_fd(struct loop_device *lo)
38778  {
38779         struct file *filp = lo->lo_backing_file;
38780 +       struct file *virt_filp = lo->lo_backing_virt_file;
38781         gfp_t gfp = lo->old_gfp_mask;
38782         struct block_device *bdev = lo->lo_device;
38783  
38784 @@ -1067,6 +1099,7 @@ static int loop_clr_fd(struct loop_device *lo)
38785         spin_lock_irq(&lo->lo_lock);
38786         lo->lo_state = Lo_rundown;
38787         lo->lo_backing_file = NULL;
38788 +       lo->lo_backing_virt_file = NULL;
38789         spin_unlock_irq(&lo->lo_lock);
38790  
38791         loop_release_xfer(lo);
38792 @@ -1115,6 +1148,8 @@ static int loop_clr_fd(struct loop_device *lo)
38793          * bd_mutex which is usually taken before lo_ctl_mutex.
38794          */
38795         fput(filp);
38796 +       if (virt_filp)
38797 +               fput(virt_filp);
38798         return 0;
38799  }
38800  
38801 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
38802 index b78de98..2bbbd92 100644
38803 --- a/drivers/block/loop.h
38804 +++ b/drivers/block/loop.h
38805 @@ -46,7 +46,7 @@ struct loop_device {
38806         int             (*ioctl)(struct loop_device *, int cmd, 
38807                                  unsigned long arg); 
38808  
38809 -       struct file *   lo_backing_file;
38810 +       struct file *   lo_backing_file, *lo_backing_virt_file;
38811         struct block_device *lo_device;
38812         void            *key_data; 
38813  
38814 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
38815 index 7ca7f67..3d4e90d 100644
38816 --- a/fs/aufs/f_op.c
38817 +++ b/fs/aufs/f_op.c
38818 @@ -358,7 +358,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
38819         if (IS_ERR(h_file))
38820                 goto out;
38821  
38822 -       if (au_test_loopback_kthread()) {
38823 +       if (0 && au_test_loopback_kthread()) {
38824                 au_warn_loopback(h_file->f_path.dentry->d_sb);
38825                 if (file->f_mapping != h_file->f_mapping) {
38826                         file->f_mapping = h_file->f_mapping;
38827 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
38828 index 2d65b8f..5ed7903 100644
38829 --- a/fs/aufs/loop.c
38830 +++ b/fs/aufs/loop.c
38831 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
38832                 symbol_put(loop_backing_file);
38833         kfree(au_warn_loopback_array);
38834  }
38835 +
38836 +/* ---------------------------------------------------------------------- */
38837 +
38838 +/* support the loopback block device insude aufs */
38839 +
38840 +struct file *aufs_real_loop(struct file *file)
38841 +{
38842 +       struct file *f;
38843 +
38844 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
38845 +       fi_read_lock(file);
38846 +       f = au_hf_top(file);
38847 +       fi_read_unlock(file);
38848 +       AuDebugOn(!f);
38849 +       return f;
38850 +}
38851 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
38852 index 05d703d..6bb23c8 100644
38853 --- a/fs/aufs/loop.h
38854 +++ b/fs/aufs/loop.h
38855 @@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
38856  
38857  int au_loopback_init(void);
38858  void au_loopback_fin(void);
38859 +
38860 +struct file *aufs_real_loop(struct file *file);
38861  #else
38862 +AuStub(struct file *, loop_backing_file, return NULL)
38863 +
38864  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
38865            struct dentry *h_adding)
38866  AuStubInt0(au_test_loopback_kthread, void)
38867 @@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
38868  
38869  AuStubInt0(au_loopback_init, void)
38870  AuStubVoid(au_loopback_fin, void)
38871 +
38872 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
38873  #endif /* BLK_DEV_LOOP */
38874  
38875  #endif /* __KERNEL__ */
38876 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
38877 index eed37ba..a3a5cd7 100644
38878 --- a/fs/aufs/super.c
38879 +++ b/fs/aufs/super.c
38880 @@ -840,7 +840,10 @@ static const struct super_operations aufs_sop = {
38881         .statfs         = aufs_statfs,
38882         .put_super      = aufs_put_super,
38883         .sync_fs        = aufs_sync_fs,
38884 -       .remount_fs     = aufs_remount_fs
38885 +       .remount_fs     = aufs_remount_fs,
38886 +#ifdef CONFIG_AUFS_BDEV_LOOP
38887 +       .real_loop      = aufs_real_loop
38888 +#endif
38889  };
38890  
38891  /* ---------------------------------------------------------------------- */
38892 diff --git a/include/linux/fs.h b/include/linux/fs.h
38893 index 09a2542..11a6346 100644
38894 --- a/include/linux/fs.h
38895 +++ b/include/linux/fs.h
38896 @@ -1852,6 +1852,10 @@ struct super_operations {
38897                                   struct shrink_control *);
38898         long (*free_cached_objects)(struct super_block *,
38899                                     struct shrink_control *);
38900 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
38901 +       /* and aufs */
38902 +       struct file *(*real_loop)(struct file *);
38903 +#endif
38904  };
38905  
38906  /*
This page took 6.984478 seconds and 3 git commands to generate.