]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- up to 4.20.13
[packages/kernel.git] / kernel-aufs4.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs4.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index ac474a6..284cee9 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -255,6 +255,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 293733f..12d19d0 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.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 8119141..5e84420 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2590,6 +2590,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  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index cb0cc86..470dd02 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -738,6 +738,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         return error;
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 2593153..6369b30 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1224,7 +1224,7 @@ enum d_walk_ret {
86   *
87   * The @enter() 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  {
93         struct dentry *this_parent;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index 0831851..78234ee 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 35d2108..d2395eb 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1660,7 +1660,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 timespec64 *time, int flags)
125 +int update_time(struct inode *inode, struct timespec64 *time, int flags)
126  {
127         int (*update_time)(struct inode *, struct timespec64 *, int);
128  
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index a7f9126..46ed643 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -770,6 +770,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 58f3053..a2a55ea 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 de2ede0..5dcf77b 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/fs.h b/include/linux/fs.h
221 index c95c080..0e44705 100644
222 --- a/include/linux/fs.h
223 +++ b/include/linux/fs.h
224 @@ -1305,6 +1305,7 @@ extern void fasync_free(struct fasync_struct *);
225  /* can be called from interrupts */
226  extern void kill_fasync(struct fasync_struct **, int, int);
227  
228 +extern int setfl(int fd, struct file * filp, unsigned long arg);
229  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
230  extern int f_setown(struct file *filp, unsigned long arg, int force);
231  extern void f_delown(struct file *filp);
232 @@ -1797,6 +1798,7 @@ struct file_operations {
233         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
234         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
235         int (*check_flags)(int);
236 +       int (*setfl)(struct file *, unsigned long);
237         int (*flock) (struct file *, int, struct file_lock *);
238         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
239         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
240 @@ -1867,6 +1869,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
241                               struct iovec *fast_pointer,
242                               struct iovec **ret_pointer);
243  
244 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
245 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
246 +                               loff_t *);
247 +vfs_readf_t vfs_readf(struct file *file);
248 +vfs_writef_t vfs_writef(struct file *file);
249 +
250  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
251  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
252  extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
253 @@ -2292,6 +2300,7 @@ extern int current_umask(void);
254  extern void ihold(struct inode * inode);
255  extern void iput(struct inode *);
256  extern int generic_update_time(struct inode *, struct timespec64 *, int);
257 +extern int update_time(struct inode *, struct timespec64 *, int);
258  
259  /* /sys/fs */
260  extern struct kobject *fs_kobj;
261 @@ -2579,6 +2588,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
262         return false;
263  }
264  #endif
265 +extern int __sync_filesystem(struct super_block *, int);
266  extern int sync_filesystem(struct super_block *);
267  extern const struct file_operations def_blk_fops;
268  extern const struct file_operations def_chr_fops;
269 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
270 index 1fd82ff..a5ccac5 100644
271 --- a/include/linux/lockdep.h
272 +++ b/include/linux/lockdep.h
273 @@ -308,6 +308,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
274         return lock->key == key;
275  }
276  
277 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
278 +
279  /*
280   * Acquire a lock.
281   *
282 @@ -434,6 +436,7 @@ struct lockdep_map { };
283  
284  #define lockdep_depth(tsk)     (0)
285  
286 +#define lockdep_is_held(lock)                  (1)
287  #define lockdep_is_held_type(l, r)             (1)
288  
289  #define lockdep_assert_held(l)                 do { (void)(l); } while (0)
290 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
291 index 3594208..24f5fd1 100644
292 --- a/include/linux/mnt_namespace.h
293 +++ b/include/linux/mnt_namespace.h
294 @@ -6,11 +6,14 @@
295  struct mnt_namespace;
296  struct fs_struct;
297  struct user_namespace;
298 +struct vfsmount;
299  
300  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
301                 struct user_namespace *, struct fs_struct *);
302  extern void put_mnt_ns(struct mnt_namespace *ns);
303  
304 +extern int is_current_mnt_ns(struct vfsmount *mnt);
305 +
306  extern const struct file_operations proc_mounts_operations;
307  extern const struct file_operations proc_mountinfo_operations;
308  extern const struct file_operations proc_mountstats_operations;
309 diff --git a/include/linux/splice.h b/include/linux/splice.h
310 index 74b4911..19789fb 100644
311 --- a/include/linux/splice.h
312 +++ b/include/linux/splice.h
313 @@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
314  
315  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
316  extern const struct pipe_buf_operations default_pipe_buf_ops;
317 +
318 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
319 +                          loff_t *ppos, size_t len, unsigned int flags);
320 +extern long do_splice_to(struct file *in, loff_t *ppos,
321 +                        struct pipe_inode_info *pipe, size_t len,
322 +                        unsigned int flags);
323  #endif
324 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
325 index 1efada2..447bc0b 100644
326 --- a/kernel/locking/lockdep.c
327 +++ b/kernel/locking/lockdep.c
328 @@ -140,7 +140,7 @@ static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
329  unsigned long nr_lock_classes;
330  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
331  
332 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
333 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
334  {
335         if (!hlock->class_idx) {
336                 /*
337 @@ -151,6 +151,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
338         }
339         return lock_classes + hlock->class_idx - 1;
340  }
341 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
342  
343  #ifdef CONFIG_LOCK_STAT
344  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
345 SPDX-License-Identifier: GPL-2.0
346 aufs4.x-rcN mmap patch
347
348 diff --git a/fs/proc/base.c b/fs/proc/base.c
349 index ce34654..28508b1 100644
350 --- a/fs/proc/base.c
351 +++ b/fs/proc/base.c
352 @@ -2016,7 +2016,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
353         down_read(&mm->mmap_sem);
354         vma = find_exact_vma(mm, vm_start, vm_end);
355         if (vma && vma->vm_file) {
356 -               *path = vma->vm_file->f_path;
357 +               *path = vma_pr_or_file(vma)->f_path;
358                 path_get(path);
359                 rc = 0;
360         }
361 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
362 index 3b63be6..fb9913b 100644
363 --- a/fs/proc/nommu.c
364 +++ b/fs/proc/nommu.c
365 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
366         file = region->vm_file;
367  
368         if (file) {
369 -               struct inode *inode = file_inode(region->vm_file);
370 +               struct inode *inode;
371 +
372 +               file = vmr_pr_or_file(region);
373 +               inode = file_inode(file);
374                 dev = inode->i_sb->s_dev;
375                 ino = inode->i_ino;
376         }
377 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
378 index 47c3764..e37e4b5 100644
379 --- a/fs/proc/task_mmu.c
380 +++ b/fs/proc/task_mmu.c
381 @@ -305,7 +305,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
382         const char *name = NULL;
383  
384         if (file) {
385 -               struct inode *inode = file_inode(vma->vm_file);
386 +               struct inode *inode;
387 +
388 +               file = vma_pr_or_file(vma);
389 +               inode = file_inode(file);
390                 dev = inode->i_sb->s_dev;
391                 ino = inode->i_ino;
392                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
393 @@ -1729,7 +1732,7 @@ static int show_numa_map(struct seq_file *m, void *v)
394         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
395         struct vm_area_struct *vma = v;
396         struct numa_maps *md = &numa_priv->md;
397 -       struct file *file = vma->vm_file;
398 +       struct file *file = vma_pr_or_file(vma);
399         struct mm_struct *mm = vma->vm_mm;
400         struct mm_walk walk = {
401                 .hugetlb_entry = gather_hugetlb_stats,
402 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
403 index 0b63d68..400d1c5 100644
404 --- a/fs/proc/task_nommu.c
405 +++ b/fs/proc/task_nommu.c
406 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
407         file = vma->vm_file;
408  
409         if (file) {
410 -               struct inode *inode = file_inode(vma->vm_file);
411 +               struct inode *inode;
412 +
413 +               file = vma_pr_or_file(vma);
414 +               inode = file_inode(file);
415                 dev = inode->i_sb->s_dev;
416                 ino = inode->i_ino;
417                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
418 diff --git a/include/linux/mm.h b/include/linux/mm.h
419 index 5411de9..b3cd025 100644
420 --- a/include/linux/mm.h
421 +++ b/include/linux/mm.h
422 @@ -1460,6 +1460,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
423         unmap_mapping_range(mapping, holebegin, holelen, 0);
424  }
425  
426 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
427 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
428 +                                     int);
429 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
430 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
431 +
432 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
433 +                                                               __LINE__)
434 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
435 +                                                         __LINE__)
436 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
437 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
438 +
439 +#ifndef CONFIG_MMU
440 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
441 +extern void vmr_do_fput(struct vm_region *, const char[], int);
442 +
443 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
444 +                                                         __LINE__)
445 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
446 +#endif /* !CONFIG_MMU */
447 +
448  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
449                 void *buf, int len, unsigned int gup_flags);
450  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
451 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
452 index 5ed8f62..0122975 100644
453 --- a/include/linux/mm_types.h
454 +++ b/include/linux/mm_types.h
455 @@ -239,6 +239,7 @@ struct vm_region {
456         unsigned long   vm_top;         /* region allocated to here */
457         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
458         struct file     *vm_file;       /* the backing file or NULL */
459 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
460  
461         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
462         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
463 @@ -313,6 +314,7 @@ struct vm_area_struct {
464         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
465                                            units */
466         struct file * vm_file;          /* File we map to (can be NULL). */
467 +       struct file *vm_prfile;         /* shadow of vm_file */
468         void * vm_private_data;         /* was vm_pte (shared mem) */
469  
470         atomic_long_t swap_readahead_info;
471 diff --git a/kernel/fork.c b/kernel/fork.c
472 index 07cddff..d837e55 100644
473 --- a/kernel/fork.c
474 +++ b/kernel/fork.c
475 @@ -546,7 +546,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
476                         struct inode *inode = file_inode(file);
477                         struct address_space *mapping = file->f_mapping;
478  
479 -                       get_file(file);
480 +                       vma_get_file(tmp);
481                         if (tmp->vm_flags & VM_DENYWRITE)
482                                 atomic_dec(&inode->i_writecount);
483                         i_mmap_lock_write(mapping);
484 diff --git a/mm/Makefile b/mm/Makefile
485 index d210cc9..e77e80c 100644
486 --- a/mm/Makefile
487 +++ b/mm/Makefile
488 @@ -39,7 +39,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
489                            mm_init.o mmu_context.o percpu.o slab_common.o \
490                            compaction.o vmacache.o \
491                            interval_tree.o list_lru.o workingset.o \
492 -                          debug.o $(mmu-y)
493 +                          prfile.o debug.o $(mmu-y)
494  
495  obj-y += init-mm.o
496  obj-y += memblock.o
497 diff --git a/mm/filemap.c b/mm/filemap.c
498 index 81adec8..8507cec 100644
499 --- a/mm/filemap.c
500 +++ b/mm/filemap.c
501 @@ -2609,7 +2609,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
502         vm_fault_t ret = VM_FAULT_LOCKED;
503  
504         sb_start_pagefault(inode->i_sb);
505 -       file_update_time(vmf->vma->vm_file);
506 +       vma_file_update_time(vmf->vma);
507         lock_page(page);
508         if (page->mapping != inode->i_mapping) {
509                 unlock_page(page);
510 diff --git a/mm/mmap.c b/mm/mmap.c
511 index 6c04292..f3629c1 100644
512 --- a/mm/mmap.c
513 +++ b/mm/mmap.c
514 @@ -180,7 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
515         if (vma->vm_ops && vma->vm_ops->close)
516                 vma->vm_ops->close(vma);
517         if (vma->vm_file)
518 -               fput(vma->vm_file);
519 +               vma_fput(vma);
520         mpol_put(vma_policy(vma));
521         vm_area_free(vma);
522         return next;
523 @@ -929,7 +929,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
524         if (remove_next) {
525                 if (file) {
526                         uprobe_munmap(next, next->vm_start, next->vm_end);
527 -                       fput(file);
528 +                       vma_fput(vma);
529                 }
530                 if (next->anon_vma)
531                         anon_vma_merge(vma, next);
532 @@ -1845,8 +1845,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
533         return addr;
534  
535  unmap_and_free_vma:
536 +       vma_fput(vma);
537         vma->vm_file = NULL;
538 -       fput(file);
539  
540         /* Undo any partial mapping done by a device driver. */
541         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
542 @@ -2665,7 +2665,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
543                 goto out_free_mpol;
544  
545         if (new->vm_file)
546 -               get_file(new->vm_file);
547 +               vma_get_file(new);
548  
549         if (new->vm_ops && new->vm_ops->open)
550                 new->vm_ops->open(new);
551 @@ -2684,7 +2684,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
552         if (new->vm_ops && new->vm_ops->close)
553                 new->vm_ops->close(new);
554         if (new->vm_file)
555 -               fput(new->vm_file);
556 +               vma_fput(new);
557         unlink_anon_vmas(new);
558   out_free_mpol:
559         mpol_put(vma_policy(new));
560 @@ -2874,7 +2874,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
561         struct vm_area_struct *vma;
562         unsigned long populate = 0;
563         unsigned long ret = -EINVAL;
564 -       struct file *file;
565 +       struct file *file, *prfile;
566  
567         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
568                      current->comm, current->pid);
569 @@ -2949,10 +2949,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
570                 }
571         }
572  
573 -       file = get_file(vma->vm_file);
574 +       vma_get_file(vma);
575 +       file = vma->vm_file;
576 +       prfile = vma->vm_prfile;
577         ret = do_mmap_pgoff(vma->vm_file, start, size,
578                         prot, flags, pgoff, &populate, NULL);
579 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
580 +               struct vm_area_struct *new_vma;
581 +
582 +               new_vma = find_vma(mm, ret);
583 +               if (!new_vma->vm_prfile)
584 +                       new_vma->vm_prfile = prfile;
585 +               if (new_vma != vma)
586 +                       get_file(prfile);
587 +       }
588 +       /*
589 +        * two fput()s instead of vma_fput(vma),
590 +        * coz vma may not be available anymore.
591 +        */
592         fput(file);
593 +       if (prfile)
594 +               fput(prfile);
595  out:
596         up_write(&mm->mmap_sem);
597         if (populate)
598 @@ -3258,7 +3275,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
599                 if (anon_vma_clone(new_vma, vma))
600                         goto out_free_mempol;
601                 if (new_vma->vm_file)
602 -                       get_file(new_vma->vm_file);
603 +                       vma_get_file(new_vma);
604                 if (new_vma->vm_ops && new_vma->vm_ops->open)
605                         new_vma->vm_ops->open(new_vma);
606                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
607 diff --git a/mm/nommu.c b/mm/nommu.c
608 index 749276b..d56f8f2 100644
609 --- a/mm/nommu.c
610 +++ b/mm/nommu.c
611 @@ -625,7 +625,7 @@ static void __put_nommu_region(struct vm_region *region)
612                 up_write(&nommu_region_sem);
613  
614                 if (region->vm_file)
615 -                       fput(region->vm_file);
616 +                       vmr_fput(region);
617  
618                 /* IO memory and memory shared directly out of the pagecache
619                  * from ramfs/tmpfs mustn't be released here */
620 @@ -763,7 +763,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
621         if (vma->vm_ops && vma->vm_ops->close)
622                 vma->vm_ops->close(vma);
623         if (vma->vm_file)
624 -               fput(vma->vm_file);
625 +               vma_fput(vma);
626         put_nommu_region(vma->vm_region);
627         vm_area_free(vma);
628  }
629 @@ -1286,7 +1286,7 @@ unsigned long do_mmap(struct file *file,
630                                         goto error_just_free;
631                                 }
632                         }
633 -                       fput(region->vm_file);
634 +                       vmr_fput(region);
635                         kmem_cache_free(vm_region_jar, region);
636                         region = pregion;
637                         result = start;
638 @@ -1361,7 +1361,7 @@ unsigned long do_mmap(struct file *file,
639         up_write(&nommu_region_sem);
640  error:
641         if (region->vm_file)
642 -               fput(region->vm_file);
643 +               vmr_fput(region);
644         kmem_cache_free(vm_region_jar, region);
645         if (vma->vm_file)
646                 fput(vma->vm_file);
647 diff --git a/mm/prfile.c b/mm/prfile.c
648 new file mode 100644
649 index 0000000..a27ac36
650 --- /dev/null
651 +++ b/mm/prfile.c
652 @@ -0,0 +1,86 @@
653 +// SPDX-License-Identifier: GPL-2.0
654 +/*
655 + * Mainly for aufs which mmap(2) different file and wants to print different
656 + * path in /proc/PID/maps.
657 + * Call these functions via macros defined in linux/mm.h.
658 + *
659 + * See Documentation/filesystems/aufs/design/06mmap.txt
660 + *
661 + * Copyright (c) 2014-2018 Junjro R. Okajima
662 + * Copyright (c) 2014 Ian Campbell
663 + */
664 +
665 +#include <linux/mm.h>
666 +#include <linux/file.h>
667 +#include <linux/fs.h>
668 +
669 +/* #define PRFILE_TRACE */
670 +static inline void prfile_trace(struct file *f, struct file *pr,
671 +                             const char func[], int line, const char func2[])
672 +{
673 +#ifdef PRFILE_TRACE
674 +       if (pr)
675 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
676 +#endif
677 +}
678 +
679 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
680 +                            int line)
681 +{
682 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
683 +
684 +       prfile_trace(f, pr, func, line, __func__);
685 +       file_update_time(f);
686 +       if (f && pr)
687 +               file_update_time(pr);
688 +}
689 +
690 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
691 +                              int line)
692 +{
693 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
694 +
695 +       prfile_trace(f, pr, func, line, __func__);
696 +       return (f && pr) ? pr : f;
697 +}
698 +
699 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
700 +{
701 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
702 +
703 +       prfile_trace(f, pr, func, line, __func__);
704 +       get_file(f);
705 +       if (f && pr)
706 +               get_file(pr);
707 +}
708 +
709 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
710 +{
711 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
712 +
713 +       prfile_trace(f, pr, func, line, __func__);
714 +       fput(f);
715 +       if (f && pr)
716 +               fput(pr);
717 +}
718 +
719 +#ifndef CONFIG_MMU
720 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
721 +                              int line)
722 +{
723 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
724 +
725 +       prfile_trace(f, pr, func, line, __func__);
726 +       return (f && pr) ? pr : f;
727 +}
728 +
729 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
730 +{
731 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
732 +
733 +       prfile_trace(f, pr, func, line, __func__);
734 +       fput(f);
735 +       if (f && pr)
736 +               fput(pr);
737 +}
738 +#endif /* !CONFIG_MMU */
739 SPDX-License-Identifier: GPL-2.0
740 aufs4.x-rcN standalone patch
741
742 diff --git a/fs/dcache.c b/fs/dcache.c
743 index 6369b30..df4a5fe 100644
744 --- a/fs/dcache.c
745 +++ b/fs/dcache.c
746 @@ -1329,6 +1329,7 @@ void d_walk(struct dentry *parent, void *data,
747         seq = 1;
748         goto again;
749  }
750 +EXPORT_SYMBOL_GPL(d_walk);
751  
752  struct check_mount {
753         struct vfsmount *mnt;
754 @@ -2817,6 +2818,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
755  
756         write_sequnlock(&rename_lock);
757  }
758 +EXPORT_SYMBOL_GPL(d_exchange);
759  
760  /**
761   * d_ancestor - search for an ancestor
762 diff --git a/fs/exec.c b/fs/exec.c
763 index fc281b7..65eaaca 100644
764 --- a/fs/exec.c
765 +++ b/fs/exec.c
766 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
767         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
768                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
769  }
770 +EXPORT_SYMBOL_GPL(path_noexec);
771  
772  #ifdef CONFIG_USELIB
773  /*
774 diff --git a/fs/fcntl.c b/fs/fcntl.c
775 index 78234ee..2072f69 100644
776 --- a/fs/fcntl.c
777 +++ b/fs/fcntl.c
778 @@ -85,6 +85,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
779   out:
780         return error;
781  }
782 +EXPORT_SYMBOL_GPL(setfl);
783  
784  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
785                       int force)
786 diff --git a/fs/file_table.c b/fs/file_table.c
787 index e49af4c..569020f 100644
788 --- a/fs/file_table.c
789 +++ b/fs/file_table.c
790 @@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
791         }
792         return ERR_PTR(-ENFILE);
793  }
794 +EXPORT_SYMBOL_GPL(alloc_empty_file);
795  
796  /*
797   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
798 @@ -323,6 +324,7 @@ void flush_delayed_fput(void)
799  {
800         delayed_fput(NULL);
801  }
802 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
803  
804  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
805  
806 @@ -365,6 +367,7 @@ void __fput_sync(struct file *file)
807  }
808  
809  EXPORT_SYMBOL(fput);
810 +EXPORT_SYMBOL_GPL(__fput_sync);
811  
812  void __init files_init(void)
813  {
814 diff --git a/fs/inode.c b/fs/inode.c
815 index d2395eb..b8be7be 100644
816 --- a/fs/inode.c
817 +++ b/fs/inode.c
818 @@ -1669,6 +1669,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
819  
820         return update_time(inode, time, flags);
821  }
822 +EXPORT_SYMBOL_GPL(update_time);
823  
824  /**
825   *     touch_atime     -       update the access time
826 diff --git a/fs/namespace.c b/fs/namespace.c
827 index 46ed643..44502c2 100644
828 --- a/fs/namespace.c
829 +++ b/fs/namespace.c
830 @@ -437,6 +437,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
831         mnt_dec_writers(real_mount(mnt));
832         preempt_enable();
833  }
834 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
835  
836  /**
837   * mnt_drop_write - give up write access to a mount
838 @@ -775,6 +776,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
839  {
840         return check_mnt(real_mount(mnt));
841  }
842 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
843  
844  /*
845   * vfsmount lock must be held for write
846 @@ -1844,6 +1846,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
847         }
848         return 0;
849  }
850 +EXPORT_SYMBOL_GPL(iterate_mounts);
851  
852  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
853  {
854 diff --git a/fs/notify/group.c b/fs/notify/group.c
855 index c03b836..817f22c 100644
856 --- a/fs/notify/group.c
857 +++ b/fs/notify/group.c
858 @@ -23,6 +23,7 @@
859  #include <linux/rculist.h>
860  #include <linux/wait.h>
861  #include <linux/memcontrol.h>
862 +#include <linux/module.h>
863  
864  #include <linux/fsnotify_backend.h>
865  #include "fsnotify.h"
866 @@ -112,6 +113,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
867  {
868         refcount_inc(&group->refcnt);
869  }
870 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
871  
872  /*
873   * Drop a reference to a group.  Free it if it's through.
874 @@ -121,6 +123,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
875         if (refcount_dec_and_test(&group->refcnt))
876                 fsnotify_final_destroy_group(group);
877  }
878 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
879  
880  /*
881   * Create a new fsnotify_group and hold a reference for the group returned.
882 @@ -150,6 +153,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
883  
884         return group;
885  }
886 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
887  
888  int fsnotify_fasync(int fd, struct file *file, int on)
889  {
890 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
891 index d2dd16c..cf709b7 100644
892 --- a/fs/notify/mark.c
893 +++ b/fs/notify/mark.c
894 @@ -289,6 +289,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
895         queue_delayed_work(system_unbound_wq, &reaper_work,
896                            FSNOTIFY_REAPER_DELAY);
897  }
898 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
899  
900  /*
901   * Get mark reference when we found the mark via lockless traversal of object
902 @@ -443,6 +444,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
903         mutex_unlock(&group->mark_mutex);
904         fsnotify_free_mark(mark);
905  }
906 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
907  
908  /*
909   * Sorting function for lists of fsnotify marks.
910 @@ -658,6 +660,7 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
911         mutex_unlock(&group->mark_mutex);
912         return ret;
913  }
914 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
915  
916  /*
917   * Given a list of marks, find the mark associated with given group. If found
918 @@ -781,6 +784,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
919         fsnotify_get_group(group);
920         mark->group = group;
921  }
922 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
923  
924  /*
925   * Destroy all marks in destroy_list, waits for SRCU period to finish before
926 diff --git a/fs/open.c b/fs/open.c
927 index 0285ce7..cb81623 100644
928 --- a/fs/open.c
929 +++ b/fs/open.c
930 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
931         inode_unlock(dentry->d_inode);
932         return ret;
933  }
934 +EXPORT_SYMBOL_GPL(do_truncate);
935  
936  long vfs_truncate(const struct path *path, loff_t length)
937  {
938 diff --git a/fs/read_write.c b/fs/read_write.c
939 index a2a55ea..a1366ed 100644
940 --- a/fs/read_write.c
941 +++ b/fs/read_write.c
942 @@ -459,6 +459,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
943  
944         return ret;
945  }
946 +EXPORT_SYMBOL_GPL(vfs_read);
947  
948  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
949  {
950 @@ -499,6 +500,7 @@ vfs_readf_t vfs_readf(struct file *file)
951                 return new_sync_read;
952         return ERR_PTR(-ENOSYS);
953  }
954 +EXPORT_SYMBOL_GPL(vfs_readf);
955  
956  vfs_writef_t vfs_writef(struct file *file)
957  {
958 @@ -510,6 +512,7 @@ vfs_writef_t vfs_writef(struct file *file)
959                 return new_sync_write;
960         return ERR_PTR(-ENOSYS);
961  }
962 +EXPORT_SYMBOL_GPL(vfs_writef);
963  
964  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
965  {
966 @@ -579,6 +582,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
967  
968         return ret;
969  }
970 +EXPORT_SYMBOL_GPL(vfs_write);
971  
972  static inline loff_t file_pos_read(struct file *file)
973  {
974 diff --git a/fs/splice.c b/fs/splice.c
975 index 5dcf77b..63fe265 100644
976 --- a/fs/splice.c
977 +++ b/fs/splice.c
978 @@ -850,6 +850,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
979  
980         return splice_write(pipe, out, ppos, len, flags);
981  }
982 +EXPORT_SYMBOL_GPL(do_splice_from);
983  
984  /*
985   * Attempt to initiate a splice from a file to a pipe.
986 @@ -879,6 +880,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
987  
988         return splice_read(in, ppos, pipe, len, flags);
989  }
990 +EXPORT_SYMBOL_GPL(do_splice_to);
991  
992  /**
993   * splice_direct_to_actor - splices data directly between two non-pipes
994 diff --git a/fs/sync.c b/fs/sync.c
995 index 2860782..ffd7ea4 100644
996 --- a/fs/sync.c
997 +++ b/fs/sync.c
998 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
999                 sb->s_op->sync_fs(sb, wait);
1000         return __sync_blockdev(sb->s_bdev, wait);
1001  }
1002 +EXPORT_SYMBOL_GPL(__sync_filesystem);
1003  
1004  /*
1005   * Write out and wait upon all dirty data associated with this
1006 diff --git a/fs/xattr.c b/fs/xattr.c
1007 index 0d6a6a4..7ce4701 100644
1008 --- a/fs/xattr.c
1009 +++ b/fs/xattr.c
1010 @@ -295,6 +295,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
1011         *xattr_value = value;
1012         return error;
1013  }
1014 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
1015  
1016  ssize_t
1017  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
1018 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
1019 index 447bc0b..4e7581c 100644
1020 --- a/kernel/locking/lockdep.c
1021 +++ b/kernel/locking/lockdep.c
1022 @@ -151,6 +151,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
1023         }
1024         return lock_classes + hlock->class_idx - 1;
1025  }
1026 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
1027  #define hlock_class(hlock) lockdep_hlock_class(hlock)
1028  
1029  #ifdef CONFIG_LOCK_STAT
1030 diff --git a/kernel/task_work.c b/kernel/task_work.c
1031 index 0fef395..83fb1ec 100644
1032 --- a/kernel/task_work.c
1033 +++ b/kernel/task_work.c
1034 @@ -116,3 +116,4 @@ void task_work_run(void)
1035                 } while (work);
1036         }
1037  }
1038 +EXPORT_SYMBOL_GPL(task_work_run);
1039 diff --git a/security/commoncap.c b/security/commoncap.c
1040 index 18a4fdf..e49f723 100644
1041 --- a/security/commoncap.c
1042 +++ b/security/commoncap.c
1043 @@ -1333,12 +1333,14 @@ int cap_mmap_addr(unsigned long addr)
1044         }
1045         return ret;
1046  }
1047 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
1048  
1049  int cap_mmap_file(struct file *file, unsigned long reqprot,
1050                   unsigned long prot, unsigned long flags)
1051  {
1052         return 0;
1053  }
1054 +EXPORT_SYMBOL_GPL(cap_mmap_file);
1055  
1056  #ifdef CONFIG_SECURITY
1057  
1058 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
1059 index cd97929..424fd23 100644
1060 --- a/security/device_cgroup.c
1061 +++ b/security/device_cgroup.c
1062 @@ -8,6 +8,7 @@
1063  #include <linux/device_cgroup.h>
1064  #include <linux/cgroup.h>
1065  #include <linux/ctype.h>
1066 +#include <linux/export.h>
1067  #include <linux/list.h>
1068  #include <linux/uaccess.h>
1069  #include <linux/seq_file.h>
1070 @@ -824,3 +825,4 @@ int __devcgroup_check_permission(short type, u32 major, u32 minor,
1071  
1072         return 0;
1073  }
1074 +EXPORT_SYMBOL_GPL(__devcgroup_check_permission);
1075 diff --git a/security/security.c b/security/security.c
1076 index 04d173e..470af62 100644
1077 --- a/security/security.c
1078 +++ b/security/security.c
1079 @@ -553,6 +553,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1080                 return 0;
1081         return call_int_hook(path_rmdir, 0, dir, dentry);
1082  }
1083 +EXPORT_SYMBOL_GPL(security_path_rmdir);
1084  
1085  int security_path_unlink(const struct path *dir, struct dentry *dentry)
1086  {
1087 @@ -569,6 +570,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
1088                 return 0;
1089         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1090  }
1091 +EXPORT_SYMBOL_GPL(security_path_symlink);
1092  
1093  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1094                        struct dentry *new_dentry)
1095 @@ -577,6 +579,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1096                 return 0;
1097         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1098  }
1099 +EXPORT_SYMBOL_GPL(security_path_link);
1100  
1101  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1102                          const struct path *new_dir, struct dentry *new_dentry,
1103 @@ -604,6 +607,7 @@ int security_path_truncate(const struct path *path)
1104                 return 0;
1105         return call_int_hook(path_truncate, 0, path);
1106  }
1107 +EXPORT_SYMBOL_GPL(security_path_truncate);
1108  
1109  int security_path_chmod(const struct path *path, umode_t mode)
1110  {
1111 @@ -611,6 +615,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1112                 return 0;
1113         return call_int_hook(path_chmod, 0, path, mode);
1114  }
1115 +EXPORT_SYMBOL_GPL(security_path_chmod);
1116  
1117  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1118  {
1119 @@ -618,6 +623,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1120                 return 0;
1121         return call_int_hook(path_chown, 0, path, uid, gid);
1122  }
1123 +EXPORT_SYMBOL_GPL(security_path_chown);
1124  
1125  int security_path_chroot(const struct path *path)
1126  {
1127 @@ -703,6 +709,7 @@ int security_inode_readlink(struct dentry *dentry)
1128                 return 0;
1129         return call_int_hook(inode_readlink, 0, dentry);
1130  }
1131 +EXPORT_SYMBOL_GPL(security_inode_readlink);
1132  
1133  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1134                                bool rcu)
1135 @@ -718,6 +725,7 @@ int security_inode_permission(struct inode *inode, int mask)
1136                 return 0;
1137         return call_int_hook(inode_permission, 0, inode, mask);
1138  }
1139 +EXPORT_SYMBOL_GPL(security_inode_permission);
1140  
1141  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1142  {
1143 @@ -889,6 +897,7 @@ int security_file_permission(struct file *file, int mask)
1144  
1145         return fsnotify_perm(file, mask);
1146  }
1147 +EXPORT_SYMBOL_GPL(security_file_permission);
1148  
1149  int security_file_alloc(struct file *file)
1150  {
1151 @@ -948,6 +957,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1152                 return ret;
1153         return ima_file_mmap(file, prot);
1154  }
1155 +EXPORT_SYMBOL_GPL(security_mmap_file);
1156  
1157  int security_mmap_addr(unsigned long addr)
1158  {
1159 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1160 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1161 +++ linux/Documentation/ABI/testing/debugfs-aufs        2018-08-12 23:43:05.450124426 +0200
1162 @@ -0,0 +1,55 @@
1163 +What:          /debug/aufs/si_<id>/
1164 +Date:          March 2009
1165 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1166 +Description:
1167 +               Under /debug/aufs, a directory named si_<id> is created
1168 +               per aufs mount, where <id> is a unique id generated
1169 +               internally.
1170 +
1171 +What:          /debug/aufs/si_<id>/plink
1172 +Date:          Apr 2013
1173 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1174 +Description:
1175 +               It has three lines and shows the information about the
1176 +               pseudo-link. The first line is a single number
1177 +               representing a number of buckets. The second line is a
1178 +               number of pseudo-links per buckets (separated by a
1179 +               blank). The last line is a single number representing a
1180 +               total number of psedo-links.
1181 +               When the aufs mount option 'noplink' is specified, it
1182 +               will show "1\n0\n0\n".
1183 +
1184 +What:          /debug/aufs/si_<id>/xib
1185 +Date:          March 2009
1186 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1187 +Description:
1188 +               It shows the consumed blocks by xib (External Inode Number
1189 +               Bitmap), its block size and file size.
1190 +               When the aufs mount option 'noxino' is specified, it
1191 +               will be empty. About XINO files, see the aufs manual.
1192 +
1193 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1194 +Date:          March 2009
1195 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1196 +Description:
1197 +               It shows the consumed blocks by xino (External Inode Number
1198 +               Translation Table), its link count, block size and file
1199 +               size.
1200 +               Due to the file size limit, there may exist multiple
1201 +               xino files per branch.  In this case, "-N" is added to
1202 +               the filename and it corresponds to the index of the
1203 +               internal xino array.  "-0" is omitted.
1204 +               When the aufs mount option 'noxino' is specified, Those
1205 +               entries won't exist.  About XINO files, see the aufs
1206 +               manual.
1207 +
1208 +What:          /debug/aufs/si_<id>/xigen
1209 +Date:          March 2009
1210 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1211 +Description:
1212 +               It shows the consumed blocks by xigen (External Inode
1213 +               Generation Table), its block size and file size.
1214 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1215 +               be created.
1216 +               When the aufs mount option 'noxino' is specified, it
1217 +               will be empty. About XINO files, see the aufs manual.
1218 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1219 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1220 +++ linux/Documentation/ABI/testing/sysfs-aufs  2017-07-29 12:14:25.893041746 +0200
1221 @@ -0,0 +1,31 @@
1222 +What:          /sys/fs/aufs/si_<id>/
1223 +Date:          March 2009
1224 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1225 +Description:
1226 +               Under /sys/fs/aufs, a directory named si_<id> is created
1227 +               per aufs mount, where <id> is a unique id generated
1228 +               internally.
1229 +
1230 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1231 +Date:          March 2009
1232 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1233 +Description:
1234 +               It shows the abolute path of a member directory (which
1235 +               is called branch) in aufs, and its permission.
1236 +
1237 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1238 +Date:          July 2013
1239 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1240 +Description:
1241 +               It shows the id of a member directory (which is called
1242 +               branch) in aufs.
1243 +
1244 +What:          /sys/fs/aufs/si_<id>/xi_path
1245 +Date:          March 2009
1246 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1247 +Description:
1248 +               It shows the abolute path of XINO (External Inode Number
1249 +               Bitmap, Translation Table and Generation Table) file
1250 +               even if it is the default path.
1251 +               When the aufs mount option 'noxino' is specified, it
1252 +               will be empty. About XINO files, see the aufs manual.
1253 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1254 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1255 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2018-04-15 08:49:13.394483860 +0200
1256 @@ -0,0 +1,171 @@
1257 +
1258 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1259 +# 
1260 +# This program is free software; you can redistribute it and/or modify
1261 +# it under the terms of the GNU General Public License as published by
1262 +# the Free Software Foundation; either version 2 of the License, or
1263 +# (at your option) any later version.
1264 +# 
1265 +# This program is distributed in the hope that it will be useful,
1266 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1267 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1268 +# GNU General Public License for more details.
1269 +# 
1270 +# You should have received a copy of the GNU General Public License
1271 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1272 +
1273 +Introduction
1274 +----------------------------------------
1275 +
1276 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1277 +1. abbrev. for "advanced multi-layered unification filesystem".
1278 +2. abbrev. for "another unionfs".
1279 +3. abbrev. for "auf das" in German which means "on the" in English.
1280 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1281 +   But "Filesystem aufs Filesystem" is hard to understand.
1282 +4. abbrev. for "African Urban Fashion Show".
1283 +
1284 +AUFS is a filesystem with features:
1285 +- multi layered stackable unification filesystem, the member directory
1286 +  is called as a branch.
1287 +- branch permission and attribute, 'readonly', 'real-readonly',
1288 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1289 +  combination.
1290 +- internal "file copy-on-write".
1291 +- logical deletion, whiteout.
1292 +- dynamic branch manipulation, adding, deleting and changing permission.
1293 +- allow bypassing aufs, user's direct branch access.
1294 +- external inode number translation table and bitmap which maintains the
1295 +  persistent aufs inode number.
1296 +- seekable directory, including NFS readdir.
1297 +- file mapping, mmap and sharing pages.
1298 +- pseudo-link, hardlink over branches.
1299 +- loopback mounted filesystem as a branch.
1300 +- several policies to select one among multiple writable branches.
1301 +- revert a single systemcall when an error occurs in aufs.
1302 +- and more...
1303 +
1304 +
1305 +Multi Layered Stackable Unification Filesystem
1306 +----------------------------------------------------------------------
1307 +Most people already knows what it is.
1308 +It is a filesystem which unifies several directories and provides a
1309 +merged single directory. When users access a file, the access will be
1310 +passed/re-directed/converted (sorry, I am not sure which English word is
1311 +correct) to the real file on the member filesystem. The member
1312 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1313 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1314 +readonly branch is handled by creating 'whiteout' on the upper writable
1315 +branch.
1316 +
1317 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1318 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1319 +different approaches to implement the merged-view.
1320 +The former tries putting it into VFS, and the latter implements as a
1321 +separate filesystem.
1322 +(If I misunderstand about these implementations, please let me know and
1323 +I shall correct it. Because it is a long time ago when I read their
1324 +source files last time).
1325 +
1326 +UnionMount's approach will be able to small, but may be hard to share
1327 +branches between several UnionMount since the whiteout in it is
1328 +implemented in the inode on branch filesystem and always
1329 +shared. According to Bharata's post, readdir does not seems to be
1330 +finished yet.
1331 +There are several missing features known in this implementations such as
1332 +- for users, the inode number may change silently. eg. copy-up.
1333 +- link(2) may break by copy-up.
1334 +- read(2) may get an obsoleted filedata (fstat(2) too).
1335 +- fcntl(F_SETLK) may be broken by copy-up.
1336 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1337 +  open(O_RDWR).
1338 +
1339 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1340 +merged into mainline. This is another implementation of UnionMount as a
1341 +separated filesystem. All the limitations and known problems which
1342 +UnionMount are equally inherited to "overlay" filesystem.
1343 +
1344 +Unionfs has a longer history. When I started implementing a stackable
1345 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1346 +inode, dentry and file objects and they have an array pointing lower
1347 +same kind objects. After contributing many patches for Unionfs, I
1348 +re-started my project AUFS (Jun 2006).
1349 +
1350 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1351 +implemented my own ideas, approaches and enhancements and it became
1352 +totally different one.
1353 +
1354 +Comparing DM snapshot and fs based implementation
1355 +- the number of bytes to be copied between devices is much smaller.
1356 +- the type of filesystem must be one and only.
1357 +- the fs must be writable, no readonly fs, even for the lower original
1358 +  device. so the compression fs will not be usable. but if we use
1359 +  loopback mount, we may address this issue.
1360 +  for instance,
1361 +       mount /cdrom/squashfs.img /sq
1362 +       losetup /sq/ext2.img
1363 +       losetup /somewhere/cow
1364 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1365 +- it will be difficult (or needs more operations) to extract the
1366 +  difference between the original device and COW.
1367 +- DM snapshot-merge may help a lot when users try merging. in the
1368 +  fs-layer union, users will use rsync(1).
1369 +
1370 +You may want to read my old paper "Filesystems in LiveCD"
1371 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1372 +
1373 +
1374 +Several characters/aspects/persona of aufs
1375 +----------------------------------------------------------------------
1376 +
1377 +Aufs has several characters, aspects or persona.
1378 +1. a filesystem, callee of VFS helper
1379 +2. sub-VFS, caller of VFS helper for branches
1380 +3. a virtual filesystem which maintains persistent inode number
1381 +4. reader/writer of files on branches such like an application
1382 +
1383 +1. Callee of VFS Helper
1384 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1385 +unlink(2) from an application reaches sys_unlink() kernel function and
1386 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1387 +calls filesystem specific unlink operation. Actually aufs implements the
1388 +unlink operation but it behaves like a redirector.
1389 +
1390 +2. Caller of VFS Helper for Branches
1391 +aufs_unlink() passes the unlink request to the branch filesystem as if
1392 +it were called from VFS. So the called unlink operation of the branch
1393 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1394 +every necessary pre/post operation for the branch filesystem.
1395 +- acquire the lock for the parent dir on a branch
1396 +- lookup in a branch
1397 +- revalidate dentry on a branch
1398 +- mnt_want_write() for a branch
1399 +- vfs_unlink() for a branch
1400 +- mnt_drop_write() for a branch
1401 +- release the lock on a branch
1402 +
1403 +3. Persistent Inode Number
1404 +One of the most important issue for a filesystem is to maintain inode
1405 +numbers. This is particularly important to support exporting a
1406 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1407 +backend block device for its own. But some storage is necessary to
1408 +keep and maintain the inode numbers. It may be a large space and may not
1409 +suit to keep in memory. Aufs rents some space from its first writable
1410 +branch filesystem (by default) and creates file(s) on it. These files
1411 +are created by aufs internally and removed soon (currently) keeping
1412 +opened.
1413 +Note: Because these files are removed, they are totally gone after
1414 +      unmounting aufs. It means the inode numbers are not persistent
1415 +      across unmount or reboot. I have a plan to make them really
1416 +      persistent which will be important for aufs on NFS server.
1417 +
1418 +4. Read/Write Files Internally (copy-on-write)
1419 +Because a branch can be readonly, when you write a file on it, aufs will
1420 +"copy-up" it to the upper writable branch internally. And then write the
1421 +originally requested thing to the file. Generally kernel doesn't
1422 +open/read/write file actively. In aufs, even a single write may cause a
1423 +internal "file copy". This behaviour is very similar to cp(1) command.
1424 +
1425 +Some people may think it is better to pass such work to user space
1426 +helper, instead of doing in kernel space. Actually I am still thinking
1427 +about it. But currently I have implemented it in kernel space.
1428 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1429 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1430 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2018-04-15 08:49:13.394483860 +0200
1431 @@ -0,0 +1,258 @@
1432 +
1433 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1434 +# 
1435 +# This program is free software; you can redistribute it and/or modify
1436 +# it under the terms of the GNU General Public License as published by
1437 +# the Free Software Foundation; either version 2 of the License, or
1438 +# (at your option) any later version.
1439 +# 
1440 +# This program is distributed in the hope that it will be useful,
1441 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1442 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1443 +# GNU General Public License for more details.
1444 +# 
1445 +# You should have received a copy of the GNU General Public License
1446 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1447 +
1448 +Basic Aufs Internal Structure
1449 +
1450 +Superblock/Inode/Dentry/File Objects
1451 +----------------------------------------------------------------------
1452 +As like an ordinary filesystem, aufs has its own
1453 +superblock/inode/dentry/file objects. All these objects have a
1454 +dynamically allocated array and store the same kind of pointers to the
1455 +lower filesystem, branch.
1456 +For example, when you build a union with one readwrite branch and one
1457 +readonly, mounted /au, /rw and /ro respectively.
1458 +- /au = /rw + /ro
1459 +- /ro/fileA exists but /rw/fileA
1460 +
1461 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1462 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1463 +- [0] = NULL (because /rw/fileA doesn't exist)
1464 +- [1] = /ro/fileA
1465 +
1466 +This style of an array is essentially same to the aufs
1467 +superblock/inode/dentry/file objects.
1468 +
1469 +Because aufs supports manipulating branches, ie. add/delete/change
1470 +branches dynamically, these objects has its own generation. When
1471 +branches are changed, the generation in aufs superblock is
1472 +incremented. And a generation in other object are compared when it is
1473 +accessed. When a generation in other objects are obsoleted, aufs
1474 +refreshes the internal array.
1475 +
1476 +
1477 +Superblock
1478 +----------------------------------------------------------------------
1479 +Additionally aufs superblock has some data for policies to select one
1480 +among multiple writable branches, XIB files, pseudo-links and kobject.
1481 +See below in detail.
1482 +About the policies which supports copy-down a directory, see
1483 +wbr_policy.txt too.
1484 +
1485 +
1486 +Branch and XINO(External Inode Number Translation Table)
1487 +----------------------------------------------------------------------
1488 +Every branch has its own xino (external inode number translation table)
1489 +file. The xino file is created and unlinked by aufs internally. When two
1490 +members of a union exist on the same filesystem, they share the single
1491 +xino file.
1492 +The struct of a xino file is simple, just a sequence of aufs inode
1493 +numbers which is indexed by the lower inode number.
1494 +In the above sample, assume the inode number of /ro/fileA is i111 and
1495 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1496 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1497 +
1498 +When the inode numbers are not contiguous, the xino file will be sparse
1499 +which has a hole in it and doesn't consume as much disk space as it
1500 +might appear. If your branch filesystem consumes disk space for such
1501 +holes, then you should specify 'xino=' option at mounting aufs.
1502 +
1503 +Aufs has a mount option to free the disk blocks for such holes in XINO
1504 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1505 +meet a problem of disk shortage due to XINO files, then you should try
1506 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1507 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1508 +the holes in XINO files.
1509 +
1510 +Also a writable branch has three kinds of "whiteout bases". All these
1511 +are existed when the branch is joined to aufs, and their names are
1512 +whiteout-ed doubly, so that users will never see their names in aufs
1513 +hierarchy.
1514 +1. a regular file which will be hardlinked to all whiteouts.
1515 +2. a directory to store a pseudo-link.
1516 +3. a directory to store an "orphan"-ed file temporary.
1517 +
1518 +1. Whiteout Base
1519 +   When you remove a file on a readonly branch, aufs handles it as a
1520 +   logical deletion and creates a whiteout on the upper writable branch
1521 +   as a hardlink of this file in order not to consume inode on the
1522 +   writable branch.
1523 +2. Pseudo-link Dir
1524 +   See below, Pseudo-link.
1525 +3. Step-Parent Dir
1526 +   When "fileC" exists on the lower readonly branch only and it is
1527 +   opened and removed with its parent dir, and then user writes
1528 +   something into it, then aufs copies-up fileC to this
1529 +   directory. Because there is no other dir to store fileC. After
1530 +   creating a file under this dir, the file is unlinked.
1531 +
1532 +Because aufs supports manipulating branches, ie. add/delete/change
1533 +dynamically, a branch has its own id. When the branch order changes,
1534 +aufs finds the new index by searching the branch id.
1535 +
1536 +
1537 +Pseudo-link
1538 +----------------------------------------------------------------------
1539 +Assume "fileA" exists on the lower readonly branch only and it is
1540 +hardlinked to "fileB" on the branch. When you write something to fileA,
1541 +aufs copies-up it to the upper writable branch. Additionally aufs
1542 +creates a hardlink under the Pseudo-link Directory of the writable
1543 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1544 +simple list. If fileB is read after unlinking fileA, aufs returns
1545 +filedata from the pseudo-link instead of the lower readonly
1546 +branch. Because the pseudo-link is based upon the inode, to keep the
1547 +inode number by xino (see above) is essentially necessary.
1548 +
1549 +All the hardlinks under the Pseudo-link Directory of the writable branch
1550 +should be restored in a proper location later. Aufs provides a utility
1551 +to do this. The userspace helpers executed at remounting and unmounting
1552 +aufs by default.
1553 +During this utility is running, it puts aufs into the pseudo-link
1554 +maintenance mode. In this mode, only the process which began the
1555 +maintenance mode (and its child processes) is allowed to operate in
1556 +aufs. Some other processes which are not related to the pseudo-link will
1557 +be allowed to run too, but the rest have to return an error or wait
1558 +until the maintenance mode ends. If a process already acquires an inode
1559 +mutex (in VFS), it has to return an error.
1560 +
1561 +
1562 +XIB(external inode number bitmap)
1563 +----------------------------------------------------------------------
1564 +Addition to the xino file per a branch, aufs has an external inode number
1565 +bitmap in a superblock object. It is also an internal file such like a
1566 +xino file.
1567 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1568 +not.
1569 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1570 +
1571 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1572 +reduce the number of consumed disk blocks for these files.
1573 +
1574 +
1575 +Virtual or Vertical Dir, and Readdir in Userspace
1576 +----------------------------------------------------------------------
1577 +In order to support multiple layers (branches), aufs readdir operation
1578 +constructs a virtual dir block on memory. For readdir, aufs calls
1579 +vfs_readdir() internally for each dir on branches, merges their entries
1580 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1581 +object. So the file object has its entry list until it is closed. The
1582 +entry list will be updated when the file position is zero and becomes
1583 +obsoleted. This decision is made in aufs automatically.
1584 +
1585 +The dynamically allocated memory block for the name of entries has a
1586 +unit of 512 bytes (by default) and stores the names contiguously (no
1587 +padding). Another block for each entry is handled by kmem_cache too.
1588 +During building dir blocks, aufs creates hash list and judging whether
1589 +the entry is whiteouted by its upper branch or already listed.
1590 +The merged result is cached in the corresponding inode object and
1591 +maintained by a customizable life-time option.
1592 +
1593 +Some people may call it can be a security hole or invite DoS attack
1594 +since the opened and once readdir-ed dir (file object) holds its entry
1595 +list and becomes a pressure for system memory. But I'd say it is similar
1596 +to files under /proc or /sys. The virtual files in them also holds a
1597 +memory page (generally) while they are opened. When an idea to reduce
1598 +memory for them is introduced, it will be applied to aufs too.
1599 +For those who really hate this situation, I've developed readdir(3)
1600 +library which operates this merging in userspace. You just need to set
1601 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1602 +kernel space for readdir(3).
1603 +
1604 +
1605 +Workqueue
1606 +----------------------------------------------------------------------
1607 +Aufs sometimes requires privilege access to a branch. For instance,
1608 +in copy-up/down operation. When a user process is going to make changes
1609 +to a file which exists in the lower readonly branch only, and the mode
1610 +of one of ancestor directories may not be writable by a user
1611 +process. Here aufs copy-up the file with its ancestors and they may
1612 +require privilege to set its owner/group/mode/etc.
1613 +This is a typical case of a application character of aufs (see
1614 +Introduction).
1615 +
1616 +Aufs uses workqueue synchronously for this case. It creates its own
1617 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1618 +passes the request to call mkdir or write (for example), and wait for
1619 +its completion. This approach solves a problem of a signal handler
1620 +simply.
1621 +If aufs didn't adopt the workqueue and changed the privilege of the
1622 +process, then the process may receive the unexpected SIGXFSZ or other
1623 +signals.
1624 +
1625 +Also aufs uses the system global workqueue ("events" kernel thread) too
1626 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1627 +whiteout base and etc. This is unrelated to a privilege.
1628 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1629 +superblock at the beginning, at the same time waits for the completion
1630 +of all queued asynchronous tasks.
1631 +
1632 +
1633 +Whiteout
1634 +----------------------------------------------------------------------
1635 +The whiteout in aufs is very similar to Unionfs's. That is represented
1636 +by its filename. UnionMount takes an approach of a file mode, but I am
1637 +afraid several utilities (find(1) or something) will have to support it.
1638 +
1639 +Basically the whiteout represents "logical deletion" which stops aufs to
1640 +lookup further, but also it represents "dir is opaque" which also stop
1641 +further lookup.
1642 +
1643 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1644 +In order to make several functions in a single systemcall to be
1645 +revertible, aufs adopts an approach to rename a directory to a temporary
1646 +unique whiteouted name.
1647 +For example, in rename(2) dir where the target dir already existed, aufs
1648 +renames the target dir to a temporary unique whiteouted name before the
1649 +actual rename on a branch, and then handles other actions (make it opaque,
1650 +update the attributes, etc). If an error happens in these actions, aufs
1651 +simply renames the whiteouted name back and returns an error. If all are
1652 +succeeded, aufs registers a function to remove the whiteouted unique
1653 +temporary name completely and asynchronously to the system global
1654 +workqueue.
1655 +
1656 +
1657 +Copy-up
1658 +----------------------------------------------------------------------
1659 +It is a well-known feature or concept.
1660 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1661 +internally and makes change to the new file on the upper writable branch.
1662 +When the trigger systemcall does not update the timestamps of the parent
1663 +dir, aufs reverts it after copy-up.
1664 +
1665 +
1666 +Move-down (aufs3.9 and later)
1667 +----------------------------------------------------------------------
1668 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1669 +the lower readonly branch to the upper writable branch when a user
1670 +changes something about the file.
1671 +"Move-down" is an opposite action of copy-up. Basically this action is
1672 +ran manually instead of automatically and internally.
1673 +For desgin and implementation, aufs has to consider these issues.
1674 +- whiteout for the file may exist on the lower branch.
1675 +- ancestor directories may not exist on the lower branch.
1676 +- diropq for the ancestor directories may exist on the upper branch.
1677 +- free space on the lower branch will reduce.
1678 +- another access to the file may happen during moving-down, including
1679 +  UDBA (see "Revalidate Dentry and UDBA").
1680 +- the file should not be hard-linked nor pseudo-linked. they should be
1681 +  handled by auplink utility later.
1682 +
1683 +Sometimes users want to move-down a file from the upper writable branch
1684 +to the lower readonly or writable branch. For instance,
1685 +- the free space of the upper writable branch is going to run out.
1686 +- create a new intermediate branch between the upper and lower branch.
1687 +- etc.
1688 +
1689 +For this purpose, use "aumvdown" command in aufs-util.git.
1690 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1691 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1692 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2018-04-15 08:49:13.394483860 +0200
1693 @@ -0,0 +1,85 @@
1694 +
1695 +# Copyright (C) 2015-2018 Junjiro R. Okajima
1696 +# 
1697 +# This program is free software; you can redistribute it and/or modify
1698 +# it under the terms of the GNU General Public License as published by
1699 +# the Free Software Foundation; either version 2 of the License, or
1700 +# (at your option) any later version.
1701 +# 
1702 +# This program is distributed in the hope that it will be useful,
1703 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1704 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1705 +# GNU General Public License for more details.
1706 +# 
1707 +# You should have received a copy of the GNU General Public License
1708 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1709 +
1710 +Support for a branch who has its ->atomic_open()
1711 +----------------------------------------------------------------------
1712 +The filesystems who implement its ->atomic_open() are not majority. For
1713 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1714 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1715 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1716 +sure whether all filesystems who have ->atomic_open() behave like this,
1717 +but NFSv4 surely returns the error.
1718 +
1719 +In order to support ->atomic_open() for aufs, there are a few
1720 +approaches.
1721 +
1722 +A. Introduce aufs_atomic_open()
1723 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1724 +     branch fs.
1725 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1726 +   an aufs user Pip Cet's approach
1727 +   - calls aufs_create(), VFS finish_open() and notify_change().
1728 +   - pass fake-mode to finish_open(), and then correct the mode by
1729 +     notify_change().
1730 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1731 +   - no aufs_atomic_open().
1732 +   - aufs_lookup() registers the TID to an aufs internal object.
1733 +   - aufs_create() does nothing when the matching TID is registered, but
1734 +     registers the mode.
1735 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1736 +     TID is registered.
1737 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1738 +   credential
1739 +   - no aufs_atomic_open().
1740 +   - aufs_create() registers the TID to an internal object. this info
1741 +     represents "this process created this file just now."
1742 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1743 +     registered TID and re-try open() with superuser's credential.
1744 +
1745 +Pros and cons for each approach.
1746 +
1747 +A.
1748 +   - straightforward but highly depends upon VFS internal.
1749 +   - the atomic behavaiour is kept.
1750 +   - some of parameters such as nameidata are hard to reproduce for
1751 +     branch fs.
1752 +   - large overhead.
1753 +B.
1754 +   - easy to implement.
1755 +   - the atomic behavaiour is lost.
1756 +C.
1757 +   - the atomic behavaiour is kept.
1758 +   - dirty and tricky.
1759 +   - VFS checks whether the file is created correctly after calling
1760 +     ->create(), which means this approach doesn't work.
1761 +D.
1762 +   - easy to implement.
1763 +   - the atomic behavaiour is lost.
1764 +   - to open a file with superuser's credential and give it to a user
1765 +     process is a bad idea, since the file object keeps the credential
1766 +     in it. It may affect LSM or something. This approach doesn't work
1767 +     either.
1768 +
1769 +The approach A is ideal, but it hard to implement. So here is a
1770 +variation of A, which is to be implemented.
1771 +
1772 +A-1. Introduce aufs_atomic_open()
1773 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1774 +       vfs_create() and finish_open().
1775 +     - the demerit is that the several checks after branch fs
1776 +       ->atomic_open() are lost. in the ordinary case, the checks are
1777 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1778 +       be implemented in aufs, but not all I am afraid.
1779 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1780 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1781 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2018-04-15 08:49:13.394483860 +0200
1782 @@ -0,0 +1,113 @@
1783 +
1784 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1785 +# 
1786 +# This program is free software; you can redistribute it and/or modify
1787 +# it under the terms of the GNU General Public License as published by
1788 +# the Free Software Foundation; either version 2 of the License, or
1789 +# (at your option) any later version.
1790 +# 
1791 +# This program is distributed in the hope that it will be useful,
1792 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1793 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1794 +# GNU General Public License for more details.
1795 +# 
1796 +# You should have received a copy of the GNU General Public License
1797 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1798 +
1799 +Lookup in a Branch
1800 +----------------------------------------------------------------------
1801 +Since aufs has a character of sub-VFS (see Introduction), it operates
1802 +lookup for branches as VFS does. It may be a heavy work. But almost all
1803 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1804 +directly connected to its parent. Digging down the directory hierarchy
1805 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1806 +aufs calls it.
1807 +
1808 +When a branch is a remote filesystem, aufs basically relies upon its
1809 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1810 +them.
1811 +For d_revalidate, aufs implements three levels of revalidate tests. See
1812 +"Revalidate Dentry and UDBA" in detail.
1813 +
1814 +
1815 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1816 +----------------------------------------------------------------------
1817 +Let's try case study.
1818 +- aufs has two branches, upper readwrite and lower readonly.
1819 +  /au = /rw + /ro
1820 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1821 +- user invoked "chmod a+rx /au/dirA"
1822 +- the internal copy-up is activated and "/rw/dirA" is created and its
1823 +  permission bits are set to world readable.
1824 +- then "/au/dirA" becomes world readable?
1825 +
1826 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1827 +or it may be a natively readonly filesystem. If aufs respects the lower
1828 +branch, it should not respond readdir request from other users. But user
1829 +allowed it by chmod. Should really aufs rejects showing the entries
1830 +under /ro/dirA?
1831 +
1832 +To be honest, I don't have a good solution for this case. So aufs
1833 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1834 +users.
1835 +When dirperm1 is specified, aufs checks only the highest one for the
1836 +directory permission, and shows the entries. Otherwise, as usual, checks
1837 +every dir existing on all branches and rejects the request.
1838 +
1839 +As a side effect, dirperm1 option improves the performance of aufs
1840 +because the number of permission check is reduced when the number of
1841 +branch is many.
1842 +
1843 +
1844 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1845 +----------------------------------------------------------------------
1846 +Generally VFS helpers re-validate a dentry as a part of lookup.
1847 +0. digging down the directory hierarchy.
1848 +1. lock the parent dir by its i_mutex.
1849 +2. lookup the final (child) entry.
1850 +3. revalidate it.
1851 +4. call the actual operation (create, unlink, etc.)
1852 +5. unlock the parent dir
1853 +
1854 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1855 +called. Actually aufs implements it and checks the dentry on a branch is
1856 +still valid.
1857 +But it is not enough. Because aufs has to release the lock for the
1858 +parent dir on a branch at the end of ->lookup() (step 2) and
1859 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1860 +held by VFS.
1861 +If the file on a branch is changed directly, eg. bypassing aufs, after
1862 +aufs released the lock, then the subsequent operation may cause
1863 +something unpleasant result.
1864 +
1865 +This situation is a result of VFS architecture, ->lookup() and
1866 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1867 +design from VFS's point of view. It is just not suitable for sub-VFS
1868 +character in aufs.
1869 +
1870 +Aufs supports such case by three level of revalidation which is
1871 +selectable by user.
1872 +1. Simple Revalidate
1873 +   Addition to the native flow in VFS's, confirm the child-parent
1874 +   relationship on the branch just after locking the parent dir on the
1875 +   branch in the "actual operation" (step 4). When this validation
1876 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1877 +   checks the validation of the dentry on branches.
1878 +2. Monitor Changes Internally by Inotify/Fsnotify
1879 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1880 +   the dentry on the branch, and returns EBUSY if it finds different
1881 +   dentry.
1882 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1883 +   during it is in cache. When the event is notified, aufs registers a
1884 +   function to kernel 'events' thread by schedule_work(). And the
1885 +   function sets some special status to the cached aufs dentry and inode
1886 +   private data. If they are not cached, then aufs has nothing to
1887 +   do. When the same file is accessed through aufs (step 0-3) later,
1888 +   aufs will detect the status and refresh all necessary data.
1889 +   In this mode, aufs has to ignore the event which is fired by aufs
1890 +   itself.
1891 +3. No Extra Validation
1892 +   This is the simplest test and doesn't add any additional revalidation
1893 +   test, and skip the revalidation in step 4. It is useful and improves
1894 +   aufs performance when system surely hide the aufs branches from user,
1895 +   by over-mounting something (or another method).
1896 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1897 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1898 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2018-04-15 08:49:13.394483860 +0200
1899 @@ -0,0 +1,74 @@
1900 +
1901 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1902 +# 
1903 +# This program is free software; you can redistribute it and/or modify
1904 +# it under the terms of the GNU General Public License as published by
1905 +# the Free Software Foundation; either version 2 of the License, or
1906 +# (at your option) any later version.
1907 +# 
1908 +# This program is distributed in the hope that it will be useful,
1909 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1910 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1911 +# GNU General Public License for more details.
1912 +# 
1913 +# You should have received a copy of the GNU General Public License
1914 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1915 +
1916 +Branch Manipulation
1917 +
1918 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1919 +and changing its permission/attribute, there are a lot of works to do.
1920 +
1921 +
1922 +Add a Branch
1923 +----------------------------------------------------------------------
1924 +o Confirm the adding dir exists outside of aufs, including loopback
1925 +  mount, and its various attributes.
1926 +o Initialize the xino file and whiteout bases if necessary.
1927 +  See struct.txt.
1928 +
1929 +o Check the owner/group/mode of the directory
1930 +  When the owner/group/mode of the adding directory differs from the
1931 +  existing branch, aufs issues a warning because it may impose a
1932 +  security risk.
1933 +  For example, when a upper writable branch has a world writable empty
1934 +  top directory, a malicious user can create any files on the writable
1935 +  branch directly, like copy-up and modify manually. If something like
1936 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1937 +  writable branch, and the writable branch is world-writable, then a
1938 +  malicious guy may create /etc/passwd on the writable branch directly
1939 +  and the infected file will be valid in aufs.
1940 +  I am afraid it can be a security issue, but aufs can do nothing except
1941 +  producing a warning.
1942 +
1943 +
1944 +Delete a Branch
1945 +----------------------------------------------------------------------
1946 +o Confirm the deleting branch is not busy
1947 +  To be general, there is one merit to adopt "remount" interface to
1948 +  manipulate branches. It is to discard caches. At deleting a branch,
1949 +  aufs checks the still cached (and connected) dentries and inodes. If
1950 +  there are any, then they are all in-use. An inode without its
1951 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1952 +
1953 +  For the cached one, aufs checks whether the same named entry exists on
1954 +  other branches.
1955 +  If the cached one is a directory, because aufs provides a merged view
1956 +  to users, as long as one dir is left on any branch aufs can show the
1957 +  dir to users. In this case, the branch can be removed from aufs.
1958 +  Otherwise aufs rejects deleting the branch.
1959 +
1960 +  If any file on the deleting branch is opened by aufs, then aufs
1961 +  rejects deleting.
1962 +
1963 +
1964 +Modify the Permission of a Branch
1965 +----------------------------------------------------------------------
1966 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1967 +  See struct.txt.
1968 +
1969 +o rw --> ro: Confirm the modifying branch is not busy
1970 +  Aufs rejects the request if any of these conditions are true.
1971 +  - a file on the branch is mmap-ed.
1972 +  - a regular file on the branch is opened for write and there is no
1973 +    same named entry on the upper branch.
1974 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1975 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1976 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2018-04-15 08:49:13.394483860 +0200
1977 @@ -0,0 +1,64 @@
1978 +
1979 +# Copyright (C) 2005-2018 Junjiro R. Okajima
1980 +# 
1981 +# This program is free software; you can redistribute it and/or modify
1982 +# it under the terms of the GNU General Public License as published by
1983 +# the Free Software Foundation; either version 2 of the License, or
1984 +# (at your option) any later version.
1985 +# 
1986 +# This program is distributed in the hope that it will be useful,
1987 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1988 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1989 +# GNU General Public License for more details.
1990 +# 
1991 +# You should have received a copy of the GNU General Public License
1992 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1993 +
1994 +Policies to Select One among Multiple Writable Branches
1995 +----------------------------------------------------------------------
1996 +When the number of writable branch is more than one, aufs has to decide
1997 +the target branch for file creation or copy-up. By default, the highest
1998 +writable branch which has the parent (or ancestor) dir of the target
1999 +file is chosen (top-down-parent policy).
2000 +By user's request, aufs implements some other policies to select the
2001 +writable branch, for file creation several policies, round-robin,
2002 +most-free-space, and other policies. For copy-up, top-down-parent,
2003 +bottom-up-parent, bottom-up and others.
2004 +
2005 +As expected, the round-robin policy selects the branch in circular. When
2006 +you have two writable branches and creates 10 new files, 5 files will be
2007 +created for each branch. mkdir(2) systemcall is an exception. When you
2008 +create 10 new directories, all will be created on the same branch.
2009 +And the most-free-space policy selects the one which has most free
2010 +space among the writable branches. The amount of free space will be
2011 +checked by aufs internally, and users can specify its time interval.
2012 +
2013 +The policies for copy-up is more simple,
2014 +top-down-parent is equivalent to the same named on in create policy,
2015 +bottom-up-parent selects the writable branch where the parent dir
2016 +exists and the nearest upper one from the copyup-source,
2017 +bottom-up selects the nearest upper writable branch from the
2018 +copyup-source, regardless the existence of the parent dir.
2019 +
2020 +There are some rules or exceptions to apply these policies.
2021 +- If there is a readonly branch above the policy-selected branch and
2022 +  the parent dir is marked as opaque (a variation of whiteout), or the
2023 +  target (creating) file is whiteout-ed on the upper readonly branch,
2024 +  then the result of the policy is ignored and the target file will be
2025 +  created on the nearest upper writable branch than the readonly branch.
2026 +- If there is a writable branch above the policy-selected branch and
2027 +  the parent dir is marked as opaque or the target file is whiteouted
2028 +  on the branch, then the result of the policy is ignored and the target
2029 +  file will be created on the highest one among the upper writable
2030 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
2031 +  it as usual.
2032 +- link(2) and rename(2) systemcalls are exceptions in every policy.
2033 +  They try selecting the branch where the source exists as possible
2034 +  since copyup a large file will take long time. If it can't be,
2035 +  ie. the branch where the source exists is readonly, then they will
2036 +  follow the copyup policy.
2037 +- There is an exception for rename(2) when the target exists.
2038 +  If the rename target exists, aufs compares the index of the branches
2039 +  where the source and the target exists and selects the higher
2040 +  one. If the selected branch is readonly, then aufs follows the
2041 +  copyup policy.
2042 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
2043 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
2044 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2018-04-15 08:49:13.394483860 +0200
2045 @@ -0,0 +1,31 @@
2046 +
2047 +// to view this graph, run dot(1) command in GRAPHVIZ.
2048 +
2049 +digraph G {
2050 +node [shape=box];
2051 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
2052 +
2053 +node [shape=oval];
2054 +
2055 +aufs_rename -> whinfo [label="store/remove"];
2056 +
2057 +node [shape=oval];
2058 +inode_list [label="h_inum list in branch\ncache"];
2059 +
2060 +node [shape=box];
2061 +whinode [label="h_inum list file"];
2062 +
2063 +node [shape=oval];
2064 +brmgmt [label="br_add/del/mod/umount"];
2065 +
2066 +brmgmt -> inode_list [label="create/remove"];
2067 +brmgmt -> whinode [label="load/store"];
2068 +
2069 +inode_list -> whinode [style=dashed,dir=both];
2070 +
2071 +aufs_rename -> inode_list [label="add/del"];
2072 +
2073 +aufs_lookup -> inode_list [label="search"];
2074 +
2075 +aufs_lookup -> whinfo [label="load/remove"];
2076 +}
2077 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
2078 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
2079 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2018-04-15 08:49:13.394483860 +0200
2080 @@ -0,0 +1,102 @@
2081 +
2082 +# Copyright (C) 2017-2018 Junjiro R. Okajima
2083 +#
2084 +# This program is free software; you can redistribute it and/or modify
2085 +# it under the terms of the GNU General Public License as published by
2086 +# the Free Software Foundation; either version 2 of the License, or
2087 +# (at your option) any later version.
2088 +#
2089 +# This program is distributed in the hope that it will be useful,
2090 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2091 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2092 +# GNU General Public License for more details.
2093 +#
2094 +# You should have received a copy of the GNU General Public License
2095 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2096 +
2097 +Special handling for renaming a directory (DIRREN)
2098 +----------------------------------------------------------------------
2099 +First, let's assume we have a simple usecase.
2100 +
2101 +- /u = /rw + /ro
2102 +- /rw/dirA exists
2103 +- /ro/dirA and /ro/dirA/file exist too
2104 +- there is no dirB on both branches
2105 +- a user issues rename("dirA", "dirB")
2106 +
2107 +Now, what should aufs behave against this rename(2)?
2108 +There are a few possible cases.
2109 +
2110 +A. returns EROFS.
2111 +   since dirA exists on a readonly branch which cannot be renamed.
2112 +B. returns EXDEV.
2113 +   it is possible to copy-up dirA (only the dir itself), but the child
2114 +   entries ("file" in this case) should not be. it must be a bad
2115 +   approach to copy-up recursively.
2116 +C. returns a success.
2117 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2118 +   is a violation of aufs' policy.
2119 +D. construct an extra information which indicates that /ro/dirA should
2120 +   be handled as the name of dirB.
2121 +   overlayfs has a similar feature called REDIRECT.
2122 +
2123 +Until now, aufs implements the case B only which returns EXDEV, and
2124 +expects the userspace application behaves like mv(1) which tries
2125 +issueing rename(2) recursively.
2126 +
2127 +A new aufs feature called DIRREN is introduced which implements the case
2128 +D. There are several "extra information" added.
2129 +
2130 +1. detailed info per renamed directory
2131 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2132 +2. the inode-number list of directories on a branch
2133 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2134 +
2135 +The filename of "detailed info per directory" represents the lower
2136 +branch, and its format is
2137 +- a type of the branch id
2138 +  one of these.
2139 +  + uuid (not implemented yet)
2140 +  + fsid
2141 +  + dev
2142 +- the inode-number of the branch root dir
2143 +
2144 +And it contains these info in a single regular file.
2145 +- magic number
2146 +- branch's inode-number of the logically renamed dir
2147 +- the name of the before-renamed dir
2148 +
2149 +The "detailed info per directory" file is created in aufs rename(2), and
2150 +loaded in any lookup.
2151 +The info is considered in lookup for the matching case only. Here
2152 +"matching" means that the root of branch (in the info filename) is same
2153 +to the current looking-up branch. After looking-up the before-renamed
2154 +name, the inode-number is compared. And the matched dentry is used.
2155 +
2156 +The "inode-number list of directories" is a regular file which contains
2157 +simply the inode-numbers on the branch. The file is created or updated
2158 +in removing the branch, and loaded in adding the branch. Its lifetime is
2159 +equal to the branch.
2160 +The list is refered in lookup, and when the current target inode is
2161 +found in the list, the aufs tries loading the "detailed info per
2162 +directory" and get the changed and valid name of the dir.
2163 +
2164 +Theoretically these "extra informaiton" may be able to be put into XATTR
2165 +in the dir inode. But aufs doesn't choose this way because
2166 +1. XATTR may not be supported by the branch (or its configuration)
2167 +2. XATTR may have its size limit.
2168 +3. XATTR may be less easy to convert than a regular file, when the
2169 +   format of the info is changed in the future.
2170 +At the same time, I agree that the regular file approach is much slower
2171 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2172 +better approach.
2173 +
2174 +This DIRREN feature is enabled by aufs configuration, and is activated
2175 +by a new mount option.
2176 +
2177 +For the more complicated case, there is a work with UDBA option, which
2178 +is to dected the direct access to the branches (by-passing aufs) and to
2179 +maintain the cashes in aufs. Since a single cached aufs dentry may
2180 +contains two names, before- and after-rename, the name comparision in
2181 +UDBA handler may not work correctly. In this case, the behaviour will be
2182 +equivalen to udba=reval case.
2183 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2184 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2185 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2018-04-15 08:49:13.394483860 +0200
2186 @@ -0,0 +1,120 @@
2187 +
2188 +# Copyright (C) 2011-2018 Junjiro R. Okajima
2189 +# 
2190 +# This program is free software; you can redistribute it and/or modify
2191 +# it under the terms of the GNU General Public License as published by
2192 +# the Free Software Foundation; either version 2 of the License, or
2193 +# (at your option) any later version.
2194 +# 
2195 +# This program is distributed in the hope that it will be useful,
2196 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2197 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2198 +# GNU General Public License for more details.
2199 +# 
2200 +# You should have received a copy of the GNU General Public License
2201 +# along with this program; if not, write to the Free Software
2202 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2203 +
2204 +
2205 +File-based Hierarchical Storage Management (FHSM)
2206 +----------------------------------------------------------------------
2207 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2208 +storage world. Aufs provides this feature as file-based with multiple
2209 +writable branches, based upon the principle of "Colder, the Lower".
2210 +Here the word "colder" means that the less used files, and "lower" means
2211 +that the position in the order of the stacked branches vertically.
2212 +These multiple writable branches are prioritized, ie. the topmost one
2213 +should be the fastest drive and be used heavily.
2214 +
2215 +o Characters in aufs FHSM story
2216 +- aufs itself and a new branch attribute.
2217 +- a new ioctl interface to move-down and to establish a connection with
2218 +  the daemon ("move-down" is a converse of "copy-up").
2219 +- userspace tool and daemon.
2220 +
2221 +The userspace daemon establishes a connection with aufs and waits for
2222 +the notification. The notified information is very similar to struct
2223 +statfs containing the number of consumed blocks and inodes.
2224 +When the consumed blocks/inodes of a branch exceeds the user-specified
2225 +upper watermark, the daemon activates its move-down process until the
2226 +consumed blocks/inodes reaches the user-specified lower watermark.
2227 +
2228 +The actual move-down is done by aufs based upon the request from
2229 +user-space since we need to maintain the inode number and the internal
2230 +pointer arrays in aufs.
2231 +
2232 +Currently aufs FHSM handles the regular files only. Additionally they
2233 +must not be hard-linked nor pseudo-linked.
2234 +
2235 +
2236 +o Cowork of aufs and the user-space daemon
2237 +  During the userspace daemon established the connection, aufs sends a
2238 +  small notification to it whenever aufs writes something into the
2239 +  writable branch. But it may cost high since aufs issues statfs(2)
2240 +  internally. So user can specify a new option to cache the
2241 +  info. Actually the notification is controlled by these factors.
2242 +  + the specified cache time.
2243 +  + classified as "force" by aufs internally.
2244 +  Until the specified time expires, aufs doesn't send the info
2245 +  except the forced cases. When aufs decide forcing, the info is always
2246 +  notified to userspace.
2247 +  For example, the number of free inodes is generally large enough and
2248 +  the shortage of it happens rarely. So aufs doesn't force the
2249 +  notification when creating a new file, directory and others. This is
2250 +  the typical case which aufs doesn't force.
2251 +  When aufs writes the actual filedata and the files consumes any of new
2252 +  blocks, the aufs forces notifying.
2253 +
2254 +
2255 +o Interfaces in aufs
2256 +- New branch attribute.
2257 +  + fhsm
2258 +    Specifies that the branch is managed by FHSM feature. In other word,
2259 +    participant in the FHSM.
2260 +    When nofhsm is set to the branch, it will not be the source/target
2261 +    branch of the move-down operation. This attribute is set
2262 +    independently from coo and moo attributes, and if you want full
2263 +    FHSM, you should specify them as well.
2264 +- New mount option.
2265 +  + fhsm_sec
2266 +    Specifies a second to suppress many less important info to be
2267 +    notified.
2268 +- New ioctl.
2269 +  + AUFS_CTL_FHSM_FD
2270 +    create a new file descriptor which userspace can read the notification
2271 +    (a subset of struct statfs) from aufs.
2272 +- Module parameter 'brs'
2273 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2274 +  be set.
2275 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2276 +  When there are two or more branches with fhsm attributes,
2277 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2278 +  terminates it. As a result of remounting and branch-manipulation, the
2279 +  number of branches with fhsm attribute can be one. In this case,
2280 +  /sbin/mount.aufs will terminate the user-space daemon.
2281 +
2282 +
2283 +Finally the operation is done as these steps in kernel-space.
2284 +- make sure that,
2285 +  + no one else is using the file.
2286 +  + the file is not hard-linked.
2287 +  + the file is not pseudo-linked.
2288 +  + the file is a regular file.
2289 +  + the parent dir is not opaqued.
2290 +- find the target writable branch.
2291 +- make sure the file is not whiteout-ed by the upper (than the target)
2292 +  branch.
2293 +- make the parent dir on the target branch.
2294 +- mutex lock the inode on the branch.
2295 +- unlink the whiteout on the target branch (if exists).
2296 +- lookup and create the whiteout-ed temporary name on the target branch.
2297 +- copy the file as the whiteout-ed temporary name on the target branch.
2298 +- rename the whiteout-ed temporary name to the original name.
2299 +- unlink the file on the source branch.
2300 +- maintain the internal pointer array and the external inode number
2301 +  table (XINO).
2302 +- maintain the timestamps and other attributes of the parent dir and the
2303 +  file.
2304 +
2305 +And of course, in every step, an error may happen. So the operation
2306 +should restore the original file state after an error happens.
2307 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2308 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2309 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2018-04-15 08:49:13.394483860 +0200
2310 @@ -0,0 +1,72 @@
2311 +
2312 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2313 +# 
2314 +# This program is free software; you can redistribute it and/or modify
2315 +# it under the terms of the GNU General Public License as published by
2316 +# the Free Software Foundation; either version 2 of the License, or
2317 +# (at your option) any later version.
2318 +# 
2319 +# This program is distributed in the hope that it will be useful,
2320 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2321 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2322 +# GNU General Public License for more details.
2323 +# 
2324 +# You should have received a copy of the GNU General Public License
2325 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2326 +
2327 +mmap(2) -- File Memory Mapping
2328 +----------------------------------------------------------------------
2329 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2330 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2331 +->mmap().
2332 +This approach is simple and good, but there is one problem.
2333 +Under /proc, several entries show the mmapped files by its path (with
2334 +device and inode number), and the printed path will be the path on the
2335 +branch fs's instead of virtual aufs's.
2336 +This is not a problem in most cases, but some utilities lsof(1) (and its
2337 +user) may expect the path on aufs.
2338 +
2339 +To address this issue, aufs adds a new member called vm_prfile in struct
2340 +vm_area_struct (and struct vm_region). The original vm_file points to
2341 +the file on the branch fs in order to handle everything correctly as
2342 +usual. The new vm_prfile points to a virtual file in aufs, and the
2343 +show-functions in procfs refers to vm_prfile if it is set.
2344 +Also we need to maintain several other places where touching vm_file
2345 +such like
2346 +- fork()/clone() copies vma and the reference count of vm_file is
2347 +  incremented.
2348 +- merging vma maintains the ref count too.
2349 +
2350 +This is not a good approach. It just fakes the printed path. But it
2351 +leaves all behaviour around f_mapping unchanged. This is surely an
2352 +advantage.
2353 +Actually aufs had adopted another complicated approach which calls
2354 +generic_file_mmap() and handles struct vm_operations_struct. In this
2355 +approach, aufs met a hard problem and I could not solve it without
2356 +switching the approach.
2357 +
2358 +There may be one more another approach which is
2359 +- bind-mount the branch-root onto the aufs-root internally
2360 +- grab the new vfsmount (ie. struct mount)
2361 +- lazy-umount the branch-root internally
2362 +- in open(2) the aufs-file, open the branch-file with the hidden
2363 +  vfsmount (instead of the original branch's vfsmount)
2364 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2365 +  but it may be possible from userspace by the mount helper.
2366 +
2367 +Adding the internal hidden vfsmount and using it in opening a file, the
2368 +file path under /proc will be printed correctly. This approach looks
2369 +smarter, but is not possible I am afraid.
2370 +- aufs-root may be bind-mount later. when it happens, another hidden
2371 +  vfsmount will be required.
2372 +- it is hard to get the chance to bind-mount and lazy-umount
2373 +  + in kernel-space, FS can have vfsmount in open(2) via
2374 +    file->f_path, and aufs can know its vfsmount. But several locks are
2375 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2376 +    here, then it may cause a deadlock.
2377 +  + in user-space, bind-mount doesn't invoke the mount helper.
2378 +- since /proc shows dev and ino, aufs has to give vma these info. it
2379 +  means a new member vm_prinode will be necessary. this is essentially
2380 +  equivalent to vm_prfile described above.
2381 +
2382 +I have to give up this "looks-smater" approach.
2383 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2384 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2385 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2018-04-15 08:49:13.394483860 +0200
2386 @@ -0,0 +1,96 @@
2387 +
2388 +# Copyright (C) 2014-2018 Junjiro R. Okajima
2389 +#
2390 +# This program is free software; you can redistribute it and/or modify
2391 +# it under the terms of the GNU General Public License as published by
2392 +# the Free Software Foundation; either version 2 of the License, or
2393 +# (at your option) any later version.
2394 +#
2395 +# This program is distributed in the hope that it will be useful,
2396 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2397 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2398 +# GNU General Public License for more details.
2399 +#
2400 +# You should have received a copy of the GNU General Public License
2401 +# along with this program; if not, write to the Free Software
2402 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2403 +
2404 +
2405 +Listing XATTR/EA and getting the value
2406 +----------------------------------------------------------------------
2407 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2408 +shows the values from the topmost existing file. This behaviour is good
2409 +for the non-dir entries since the bahaviour exactly matches the shown
2410 +information. But for the directories, aufs considers all the same named
2411 +entries on the lower branches. Which means, if one of the lower entry
2412 +rejects readdir call, then aufs returns an error even if the topmost
2413 +entry allows it. This behaviour is necessary to respect the branch fs's
2414 +security, but can make users confused since the user-visible standard
2415 +attributes don't match the behaviour.
2416 +To address this issue, aufs has a mount option called dirperm1 which
2417 +checks the permission for the topmost entry only, and ignores the lower
2418 +entry's permission.
2419 +
2420 +A similar issue can happen around XATTR.
2421 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2422 +always set. Otherwise these very unpleasant situation would happen.
2423 +- listxattr(2) may return the duplicated entries.
2424 +- users may not be able to remove or reset the XATTR forever,
2425 +
2426 +
2427 +XATTR/EA support in the internal (copy,move)-(up,down)
2428 +----------------------------------------------------------------------
2429 +Generally the extended attributes of inode are categorized as these.
2430 +- "security" for LSM and capability.
2431 +- "system" for posix ACL, 'acl' mount option is required for the branch
2432 +  fs generally.
2433 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2434 +- "user" for userspace, 'user_xattr' mount option is required for the
2435 +  branch fs generally.
2436 +
2437 +Moreover there are some other categories. Aufs handles these rather
2438 +unpopular categories as the ordinary ones, ie. there is no special
2439 +condition nor exception.
2440 +
2441 +In copy-up, the support for XATTR on the dst branch may differ from the
2442 +src branch. In this case, the copy-up operation will get an error and
2443 +the original user operation which triggered the copy-up will fail. It
2444 +can happen that even all copy-up will fail.
2445 +When both of src and dst branches support XATTR and if an error occurs
2446 +during copying XATTR, then the copy-up should fail obviously. That is a
2447 +good reason and aufs should return an error to userspace. But when only
2448 +the src branch support that XATTR, aufs should not return an error.
2449 +For example, the src branch supports ACL but the dst branch doesn't
2450 +because the dst branch may natively un-support it or temporary
2451 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2452 +may NOT return an error even if the XATTR is not supported. It is
2453 +totally up to the branch fs.
2454 +
2455 +Anyway when the aufs internal copy-up gets an error from the dst branch
2456 +fs, then aufs tries removing the just copied entry and returns the error
2457 +to the userspace. The worst case of this situation will be all copy-up
2458 +will fail.
2459 +
2460 +For the copy-up operation, there two basic approaches.
2461 +- copy the specified XATTR only (by category above), and return the
2462 +  error unconditionally if it happens.
2463 +- copy all XATTR, and ignore the error on the specified category only.
2464 +
2465 +In order to support XATTR and to implement the correct behaviour, aufs
2466 +chooses the latter approach and introduces some new branch attributes,
2467 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2468 +They correspond to the XATTR namespaces (see above). Additionally, to be
2469 +convenient, "icex" is also provided which means all "icex*" attributes
2470 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2471 +
2472 +The meaning of these attributes is to ignore the error from setting
2473 +XATTR on that branch.
2474 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2475 +error from the dst branch according to the specified attributes.
2476 +
2477 +Some XATTR may have its default value. The default value may come from
2478 +the parent dir or the environment. If the default value is set at the
2479 +file creating-time, it will be overwritten by copy-up.
2480 +Some contradiction may happen I am afraid.
2481 +Do we need another attribute to stop copying XATTR? I am unsure. For
2482 +now, aufs implements the branch attributes to ignore the error.
2483 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2484 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2485 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2018-04-15 08:49:13.394483860 +0200
2486 @@ -0,0 +1,58 @@
2487 +
2488 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2489 +# 
2490 +# This program is free software; you can redistribute it and/or modify
2491 +# it under the terms of the GNU General Public License as published by
2492 +# the Free Software Foundation; either version 2 of the License, or
2493 +# (at your option) any later version.
2494 +# 
2495 +# This program is distributed in the hope that it will be useful,
2496 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2497 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2498 +# GNU General Public License for more details.
2499 +# 
2500 +# You should have received a copy of the GNU General Public License
2501 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2502 +
2503 +Export Aufs via NFS
2504 +----------------------------------------------------------------------
2505 +Here is an approach.
2506 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2507 +  generation.
2508 +- iget_locked(): initialize aufs inode generation for a new inode, and
2509 +  store it in xigen file.
2510 +- destroy_inode(): increment aufs inode generation and store it in xigen
2511 +  file. it is necessary even if it is not unlinked, because any data of
2512 +  inode may be changed by UDBA.
2513 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2514 +  build file handle by
2515 +  + branch id (4 bytes)
2516 +  + superblock generation (4 bytes)
2517 +  + inode number (4 or 8 bytes)
2518 +  + parent dir inode number (4 or 8 bytes)
2519 +  + inode generation (4 bytes))
2520 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2521 +    bytes)
2522 +  + file handle for a branch (by exportfs_encode_fh())
2523 +- fh_to_dentry():
2524 +  + find the index of a branch from its id in handle, and check it is
2525 +    still exist in aufs.
2526 +  + 1st level: get the inode number from handle and search it in cache.
2527 +  + 2nd level: if not found in cache, get the parent inode number from
2528 +    the handle and search it in cache. and then open the found parent
2529 +    dir, find the matching inode number by vfs_readdir() and get its
2530 +    name, and call lookup_one_len() for the target dentry.
2531 +  + 3rd level: if the parent dir is not cached, call
2532 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2533 +    build a pathname of it, convert it a pathname in aufs, call
2534 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2535 +    the 2nd level.
2536 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2537 +    for every branch, but not itself. to get this, (currently) aufs
2538 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2539 +    idea, but I didn't get other approach.
2540 +  + test the generation of the gotten inode.
2541 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2542 +  convert it into ESTALE for NFSD.
2543 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2544 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2545 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2546 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2547 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2018-04-15 08:49:13.394483860 +0200
2548 @@ -0,0 +1,52 @@
2549 +
2550 +# Copyright (C) 2005-2018 Junjiro R. Okajima
2551 +# 
2552 +# This program is free software; you can redistribute it and/or modify
2553 +# it under the terms of the GNU General Public License as published by
2554 +# the Free Software Foundation; either version 2 of the License, or
2555 +# (at your option) any later version.
2556 +# 
2557 +# This program is distributed in the hope that it will be useful,
2558 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2559 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2560 +# GNU General Public License for more details.
2561 +# 
2562 +# You should have received a copy of the GNU General Public License
2563 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2564 +
2565 +Show Whiteout Mode (shwh)
2566 +----------------------------------------------------------------------
2567 +Generally aufs hides the name of whiteouts. But in some cases, to show
2568 +them is very useful for users. For instance, creating a new middle layer
2569 +(branch) by merging existing layers.
2570 +
2571 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2572 +When you have three branches,
2573 +- Bottom: 'system', squashfs (underlying base system), read-only
2574 +- Middle: 'mods', squashfs, read-only
2575 +- Top: 'overlay', ram (tmpfs), read-write
2576 +
2577 +The top layer is loaded at boot time and saved at shutdown, to preserve
2578 +the changes made to the system during the session.
2579 +When larger changes have been made, or smaller changes have accumulated,
2580 +the size of the saved top layer data grows. At this point, it would be
2581 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2582 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2583 +restoring save and load speed.
2584 +
2585 +This merging is simplified by the use of another aufs mount, of just the
2586 +two overlay branches using the 'shwh' option.
2587 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2588 +       aufs /livesys/merge_union
2589 +
2590 +A merged view of these two branches is then available at
2591 +/livesys/merge_union, and the new feature is that the whiteouts are
2592 +visible!
2593 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2594 +writing to all branches. Also the default mode for all branches is 'ro'.
2595 +It is now possible to save the combined contents of the two overlay
2596 +branches to a new squashfs, e.g.:
2597 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2598 +
2599 +This new squashfs archive can be stored on the boot device and the
2600 +initramfs will use it to replace the old one at the next boot.
2601 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2602 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2603 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2018-04-15 08:49:13.394483860 +0200
2604 @@ -0,0 +1,47 @@
2605 +
2606 +# Copyright (C) 2010-2018 Junjiro R. Okajima
2607 +#
2608 +# This program is free software; you can redistribute it and/or modify
2609 +# it under the terms of the GNU General Public License as published by
2610 +# the Free Software Foundation; either version 2 of the License, or
2611 +# (at your option) any later version.
2612 +#
2613 +# This program is distributed in the hope that it will be useful,
2614 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2615 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2616 +# GNU General Public License for more details.
2617 +#
2618 +# You should have received a copy of the GNU General Public License
2619 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2620 +
2621 +Dynamically customizable FS operations
2622 +----------------------------------------------------------------------
2623 +Generally FS operations (struct inode_operations, struct
2624 +address_space_operations, struct file_operations, etc.) are defined as
2625 +"static const", but it never means that FS have only one set of
2626 +operation. Some FS have multiple sets of them. For instance, ext2 has
2627 +three sets, one for XIP, for NOBH, and for normal.
2628 +Since aufs overrides and redirects these operations, sometimes aufs has
2629 +to change its behaviour according to the branch FS type. More importantly
2630 +VFS acts differently if a function (member in the struct) is set or
2631 +not. It means aufs should have several sets of operations and select one
2632 +among them according to the branch FS definition.
2633 +
2634 +In order to solve this problem and not to affect the behaviour of VFS,
2635 +aufs defines these operations dynamically. For instance, aufs defines
2636 +dummy direct_IO function for struct address_space_operations, but it may
2637 +not be set to the address_space_operations actually. When the branch FS
2638 +doesn't have it, aufs doesn't set it to its address_space_operations
2639 +while the function definition itself is still alive. So the behaviour
2640 +itself will not change, and it will return an error when direct_IO is
2641 +not set.
2642 +
2643 +The lifetime of these dynamically generated operation object is
2644 +maintained by aufs branch object. When the branch is removed from aufs,
2645 +the reference counter of the object is decremented. When it reaches
2646 +zero, the dynamically generated operation object will be freed.
2647 +
2648 +This approach is designed to support AIO (io_submit), Direct I/O and
2649 +XIP (DAX) mainly.
2650 +Currently this approach is applied to address_space_operations for
2651 +regular files only.
2652 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2653 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2654 +++ linux/Documentation/filesystems/aufs/README 2018-12-27 13:19:17.705082621 +0100
2655 @@ -0,0 +1,395 @@
2656 +
2657 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2658 +http://aufs.sf.net
2659 +Junjiro R. Okajima
2660 +
2661 +
2662 +0. Introduction
2663 +----------------------------------------
2664 +In the early days, aufs was entirely re-designed and re-implemented
2665 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2666 +improvements and implementations, it becomes totally different from
2667 +Unionfs while keeping the basic features.
2668 +Recently, Unionfs Version 2.x series begin taking some of the same
2669 +approaches to aufs1's.
2670 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2671 +University and his team.
2672 +
2673 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2674 +If you want older kernel version support, try aufs2-2.6.git or
2675 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2676 +
2677 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2678 +      According to Christoph Hellwig, linux rejects all union-type
2679 +      filesystems but UnionMount.
2680 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2681 +
2682 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2683 +    UnionMount, and he pointed out an issue around a directory mutex
2684 +    lock and aufs addressed it. But it is still unsure whether aufs will
2685 +    be merged (or any other union solution).
2686 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2687 +
2688 +
2689 +1. Features
2690 +----------------------------------------
2691 +- unite several directories into a single virtual filesystem. The member
2692 +  directory is called as a branch.
2693 +- you can specify the permission flags to the branch, which are 'readonly',
2694 +  'readwrite' and 'whiteout-able.'
2695 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2696 +  readonly branch are modifiable logically.
2697 +- dynamic branch manipulation, add, del.
2698 +- etc...
2699 +
2700 +Also there are many enhancements in aufs, such as:
2701 +- test only the highest one for the directory permission (dirperm1)
2702 +- copyup on open (coo=)
2703 +- 'move' policy for copy-up between two writable branches, after
2704 +  checking free space.
2705 +- xattr, acl
2706 +- readdir(3) in userspace.
2707 +- keep inode number by external inode number table
2708 +- keep the timestamps of file/dir in internal copyup operation
2709 +- seekable directory, supporting NFS readdir.
2710 +- whiteout is hardlinked in order to reduce the consumption of inodes
2711 +  on branch
2712 +- do not copyup, nor create a whiteout when it is unnecessary
2713 +- revert a single systemcall when an error occurs in aufs
2714 +- remount interface instead of ioctl
2715 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2716 +- loopback mounted filesystem as a branch
2717 +- kernel thread for removing the dir who has a plenty of whiteouts
2718 +- support copyup sparse file (a file which has a 'hole' in it)
2719 +- default permission flags for branches
2720 +- selectable permission flags for ro branch, whether whiteout can
2721 +  exist or not
2722 +- export via NFS.
2723 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2724 +- support multiple writable branches, some policies to select one
2725 +  among multiple writable branches.
2726 +- a new semantics for link(2) and rename(2) to support multiple
2727 +  writable branches.
2728 +- no glibc changes are required.
2729 +- pseudo hardlink (hardlink over branches)
2730 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2731 +  including NFS or remote filesystem branch.
2732 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2733 +- and more...
2734 +
2735 +Currently these features are dropped temporary from aufs4.
2736 +See design/08plan.txt in detail.
2737 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2738 +  (robr)
2739 +- statistics of aufs thread (/sys/fs/aufs/stat)
2740 +
2741 +Features or just an idea in the future (see also design/*.txt),
2742 +- reorder the branch index without del/re-add.
2743 +- permanent xino files for NFSD
2744 +- an option for refreshing the opened files after add/del branches
2745 +- light version, without branch manipulation. (unnecessary?)
2746 +- copyup in userspace
2747 +- inotify in userspace
2748 +- readv/writev
2749 +
2750 +
2751 +2. Download
2752 +----------------------------------------
2753 +There are three GIT trees for aufs4, aufs4-linux.git,
2754 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2755 +"aufs-util.git."
2756 +While the aufs-util is always necessary, you need either of aufs4-linux
2757 +or aufs4-standalone.
2758 +
2759 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2760 +git://git.kernel.org/.../torvalds/linux.git.
2761 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2762 +build aufs4 as an external kernel module.
2763 +Several extra patches are not included in this tree. Only
2764 +aufs4-standalone tree contains them. They are described in the later
2765 +section "Configuration and Compilation."
2766 +
2767 +On the other hand, the aufs4-standalone tree has only aufs source files
2768 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2769 +But you need to apply all aufs patches manually.
2770 +
2771 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2772 +represents the linux kernel version, "linux-4.x". For instance,
2773 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2774 +"aufs4.x-rcN" branch.
2775 +
2776 +o aufs4-linux tree
2777 +$ git clone --reference /your/linux/git/tree \
2778 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2779 +- if you don't have linux GIT tree, then remove "--reference ..."
2780 +$ cd aufs4-linux.git
2781 +$ git checkout origin/aufs4.0
2782 +
2783 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2784 +leave the patch-work to GIT.
2785 +$ cd /your/linux/git/tree
2786 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2787 +$ git fetch aufs4
2788 +$ git checkout -b my4.0 v4.0
2789 +$ (add your local change...)
2790 +$ git pull aufs4 aufs4.0
2791 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2792 +- you may need to solve some conflicts between your_changes and
2793 +  aufs4.0. in this case, git-rerere is recommended so that you can
2794 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2795 +  later in the future.
2796 +
2797 +o aufs4-standalone tree
2798 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2799 +$ cd aufs4-standalone.git
2800 +$ git checkout origin/aufs4.0
2801 +
2802 +o aufs-util tree
2803 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2804 +- note that the public aufs-util.git is on SourceForge instead of
2805 +  GitHUB.
2806 +$ cd aufs-util.git
2807 +$ git checkout origin/aufs4.0
2808 +
2809 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2810 +The minor version number, 'x' in '4.x', of aufs may not always
2811 +follow the minor version number of the kernel.
2812 +Because changes in the kernel that cause the use of a new
2813 +minor version number do not always require changes to aufs-util.
2814 +
2815 +Since aufs-util has its own minor version number, you may not be
2816 +able to find a GIT branch in aufs-util for your kernel's
2817 +exact minor version number.
2818 +In this case, you should git-checkout the branch for the
2819 +nearest lower number.
2820 +
2821 +For (an unreleased) example:
2822 +If you are using "linux-4.10" and the "aufs4.10" branch
2823 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2824 +or something numerically smaller is the branch for your kernel.
2825 +
2826 +Also you can view all branches by
2827 +       $ git branch -a
2828 +
2829 +
2830 +3. Configuration and Compilation
2831 +----------------------------------------
2832 +Make sure you have git-checkout'ed the correct branch.
2833 +
2834 +For aufs4-linux tree,
2835 +- enable CONFIG_AUFS_FS.
2836 +- set other aufs configurations if necessary.
2837 +
2838 +For aufs4-standalone tree,
2839 +There are several ways to build.
2840 +
2841 +1.
2842 +- apply ./aufs4-kbuild.patch to your kernel source files.
2843 +- apply ./aufs4-base.patch too.
2844 +- apply ./aufs4-mmap.patch too.
2845 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2846 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2847 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2848 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2849 +- enable CONFIG_AUFS_FS, you can select either
2850 +  =m or =y.
2851 +- and build your kernel as usual.
2852 +- install the built kernel.
2853 +  Note: Since linux-3.9, every filesystem module requires an alias
2854 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2855 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2856 +- install the header files too by "make headers_install" to the
2857 +  directory where you specify. By default, it is $PWD/usr.
2858 +  "make help" shows a brief note for headers_install.
2859 +- and reboot your system.
2860 +
2861 +2.
2862 +- module only (CONFIG_AUFS_FS=m).
2863 +- apply ./aufs4-base.patch to your kernel source files.
2864 +- apply ./aufs4-mmap.patch too.
2865 +- apply ./aufs4-standalone.patch too.
2866 +- build your kernel, don't forget "make headers_install", and reboot.
2867 +- edit ./config.mk and set other aufs configurations if necessary.
2868 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2869 +  every aufs configurations.
2870 +- build the module by simple "make".
2871 +  Note: Since linux-3.9, every filesystem module requires an alias
2872 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2873 +  modules.aliases file.
2874 +- you can specify ${KDIR} make variable which points to your kernel
2875 +  source tree.
2876 +- install the files
2877 +  + run "make install" to install the aufs module, or copy the built
2878 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2879 +  + run "make install_headers" (instead of headers_install) to install
2880 +    the modified aufs header file (you can specify DESTDIR which is
2881 +    available in aufs standalone version's Makefile only), or copy
2882 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2883 +    you like manually. By default, the target directory is $PWD/usr.
2884 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2885 +  kernel source tree.
2886 +
2887 +Note: The header file aufs_type.h is necessary to build aufs-util
2888 +      as well as "make headers_install" in the kernel source tree.
2889 +      headers_install is subject to be forgotten, but it is essentially
2890 +      necessary, not only for building aufs-util.
2891 +      You may not meet problems without headers_install in some older
2892 +      version though.
2893 +
2894 +And then,
2895 +- read README in aufs-util, build and install it
2896 +- note that your distribution may contain an obsoleted version of
2897 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2898 +  utilities, make sure that your compiler refers the correct aufs header
2899 +  file which is built by "make headers_install."
2900 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2901 +  then run "make install_ulib" too. And refer to the aufs manual in
2902 +  detail.
2903 +
2904 +There several other patches in aufs4-standalone.git. They are all
2905 +optional. When you meet some problems, they will help you.
2906 +- aufs4-loopback.patch
2907 +  Supports a nested loopback mount in a branch-fs. This patch is
2908 +  unnecessary until aufs produces a message like "you may want to try
2909 +  another patch for loopback file".
2910 +- vfs-ino.patch
2911 +  Modifies a system global kernel internal function get_next_ino() in
2912 +  order to stop assigning 0 for an inode-number. Not directly related to
2913 +  aufs, but recommended generally.
2914 +- tmpfs-idr.patch
2915 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2916 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2917 +  duplication of inode number, which is important for backup tools and
2918 +  other utilities. When you find aufs XINO files for tmpfs branch
2919 +  growing too much, try this patch.
2920 +- lockdep-debug.patch
2921 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2922 +  also a caller of VFS functions for branch filesystems, subclassing of
2923 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2924 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2925 +  need to apply this debug patch to expand several constant values.
2926 +  If don't know what LOCKDEP, then you don't have apply this patch.
2927 +
2928 +
2929 +4. Usage
2930 +----------------------------------------
2931 +At first, make sure aufs-util are installed, and please read the aufs
2932 +manual, aufs.5 in aufs-util.git tree.
2933 +$ man -l aufs.5
2934 +
2935 +And then,
2936 +$ mkdir /tmp/rw /tmp/aufs
2937 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2938 +
2939 +Here is another example. The result is equivalent.
2940 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2941 +  Or
2942 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2943 +# mount -o remount,append:${HOME} /tmp/aufs
2944 +
2945 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2946 +you modify a file under /tmp/aufs, the one on your home directory is
2947 +not affected, instead the same named file will be newly created under
2948 +/tmp/rw. And all of your modification to a file will be applied to
2949 +the one under /tmp/rw. This is called the file based Copy on Write
2950 +(COW) method.
2951 +Aufs mount options are described in aufs.5.
2952 +If you run chroot or something and make your aufs as a root directory,
2953 +then you need to customize the shutdown script. See the aufs manual in
2954 +detail.
2955 +
2956 +Additionally, there are some sample usages of aufs which are a
2957 +diskless system with network booting, and LiveCD over NFS.
2958 +See sample dir in CVS tree on SourceForge.
2959 +
2960 +
2961 +5. Contact
2962 +----------------------------------------
2963 +When you have any problems or strange behaviour in aufs, please let me
2964 +know with:
2965 +- /proc/mounts (instead of the output of mount(8))
2966 +- /sys/module/aufs/*
2967 +- /sys/fs/aufs/* (if you have them)
2968 +- /debug/aufs/* (if you have them)
2969 +- linux kernel version
2970 +  if your kernel is not plain, for example modified by distributor,
2971 +  the url where i can download its source is necessary too.
2972 +- aufs version which was printed at loading the module or booting the
2973 +  system, instead of the date you downloaded.
2974 +- configuration (define/undefine CONFIG_AUFS_xxx)
2975 +- kernel configuration or /proc/config.gz (if you have it)
2976 +- behaviour which you think to be incorrect
2977 +- actual operation, reproducible one is better
2978 +- mailto: aufs-users at lists.sourceforge.net
2979 +
2980 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2981 +and Feature Requests) on SourceForge. Please join and write to
2982 +aufs-users ML.
2983 +
2984 +
2985 +6. Acknowledgements
2986 +----------------------------------------
2987 +Thanks to everyone who have tried and are using aufs, whoever
2988 +have reported a bug or any feedback.
2989 +
2990 +Especially donators:
2991 +Tomas Matejicek(slax.org) made a donation (much more than once).
2992 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2993 +       scripts) is making "doubling" donations.
2994 +       Unfortunately I cannot list all of the donators, but I really
2995 +       appreciate.
2996 +       It ends Aug 2010, but the ordinary donation URL is still available.
2997 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2998 +Dai Itasaka made a donation (2007/8).
2999 +Chuck Smith made a donation (2008/4, 10 and 12).
3000 +Henk Schoneveld made a donation (2008/9).
3001 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
3002 +Francois Dupoux made a donation (2008/11).
3003 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
3004 +       aufs2 GIT tree (2009/2).
3005 +William Grant made a donation (2009/3).
3006 +Patrick Lane made a donation (2009/4).
3007 +The Mail Archive (mail-archive.com) made donations (2009/5).
3008 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
3009 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
3010 +Pavel Pronskiy made a donation (2011/2).
3011 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
3012 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
3013 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
3014 +11).
3015 +Sam Liddicott made a donation (2011/9).
3016 +Era Scarecrow made a donation (2013/4).
3017 +Bor Ratajc made a donation (2013/4).
3018 +Alessandro Gorreta made a donation (2013/4).
3019 +POIRETTE Marc made a donation (2013/4).
3020 +Alessandro Gorreta made a donation (2013/4).
3021 +lauri kasvandik made a donation (2013/5).
3022 +"pemasu from Finland" made a donation (2013/7).
3023 +The Parted Magic Project made a donation (2013/9 and 11).
3024 +Pavel Barta made a donation (2013/10).
3025 +Nikolay Pertsev made a donation (2014/5).
3026 +James B made a donation (2014/7 and 2015/7).
3027 +Stefano Di Biase made a donation (2014/8).
3028 +Daniel Epellei made a donation (2015/1).
3029 +OmegaPhil made a donation (2016/1, 2018/4).
3030 +Tomasz Szewczyk made a donation (2016/4).
3031 +James Burry made a donation (2016/12).
3032 +Carsten Rose made a donation (2018/9).
3033 +Porteus Kiosk made a donation (2018/10).
3034 +
3035 +Thank you very much.
3036 +Donations are always, including future donations, very important and
3037 +helpful for me to keep on developing aufs.
3038 +
3039 +
3040 +7.
3041 +----------------------------------------
3042 +If you are an experienced user, no explanation is needed. Aufs is
3043 +just a linux filesystem.
3044 +
3045 +
3046 +Enjoy!
3047 +
3048 +# Local variables: ;
3049 +# mode: text;
3050 +# End: ;
3051 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
3052 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
3053 +++ linux/fs/aufs/aufs.h        2018-10-23 12:33:35.592708932 +0200
3054 @@ -0,0 +1,62 @@
3055 +/* SPDX-License-Identifier: GPL-2.0 */
3056 +/*
3057 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3058 + *
3059 + * This program, aufs is free software; you can redistribute it and/or modify
3060 + * it under the terms of the GNU General Public License as published by
3061 + * the Free Software Foundation; either version 2 of the License, or
3062 + * (at your option) any later version.
3063 + *
3064 + * This program is distributed in the hope that it will be useful,
3065 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3066 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3067 + * GNU General Public License for more details.
3068 + *
3069 + * You should have received a copy of the GNU General Public License
3070 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3071 + */
3072 +
3073 +/*
3074 + * all header files
3075 + */
3076 +
3077 +#ifndef __AUFS_H__
3078 +#define __AUFS_H__
3079 +
3080 +#ifdef __KERNEL__
3081 +
3082 +#define AuStub(type, name, body, ...) \
3083 +       static inline type name(__VA_ARGS__) { body; }
3084 +
3085 +#define AuStubVoid(name, ...) \
3086 +       AuStub(void, name, , __VA_ARGS__)
3087 +#define AuStubInt0(name, ...) \
3088 +       AuStub(int, name, return 0, __VA_ARGS__)
3089 +
3090 +#include "debug.h"
3091 +
3092 +#include "branch.h"
3093 +#include "cpup.h"
3094 +#include "dcsub.h"
3095 +#include "dbgaufs.h"
3096 +#include "dentry.h"
3097 +#include "dir.h"
3098 +#include "dirren.h"
3099 +#include "dynop.h"
3100 +#include "file.h"
3101 +#include "fstype.h"
3102 +#include "hbl.h"
3103 +#include "inode.h"
3104 +#include "lcnt.h"
3105 +#include "loop.h"
3106 +#include "module.h"
3107 +#include "opts.h"
3108 +#include "rwsem.h"
3109 +#include "super.h"
3110 +#include "sysaufs.h"
3111 +#include "vfsub.h"
3112 +#include "whout.h"
3113 +#include "wkq.h"
3114 +
3115 +#endif /* __KERNEL__ */
3116 +#endif /* __AUFS_H__ */
3117 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3118 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3119 +++ linux/fs/aufs/branch.c      2018-12-27 13:19:17.708416053 +0100
3120 @@ -0,0 +1,1422 @@
3121 +// SPDX-License-Identifier: GPL-2.0
3122 +/*
3123 + * Copyright (C) 2005-2018 Junjiro R. Okajima
3124 + *
3125 + * This program, aufs is free software; you can redistribute it and/or modify
3126 + * it under the terms of the GNU General Public License as published by
3127 + * the Free Software Foundation; either version 2 of the License, or
3128 + * (at your option) any later version.
3129 + *
3130 + * This program is distributed in the hope that it will be useful,
3131 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3132 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3133 + * GNU General Public License for more details.
3134 + *
3135 + * You should have received a copy of the GNU General Public License
3136 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3137 + */
3138 +
3139 +/*
3140 + * branch management
3141 + */
3142 +
3143 +#include <linux/compat.h>
3144 +#include <linux/statfs.h>
3145 +#include "aufs.h"
3146 +
3147 +/*
3148 + * free a single branch
3149 + */
3150 +static void au_br_do_free(struct au_branch *br)
3151 +{
3152 +       int i;
3153 +       struct au_wbr *wbr;
3154 +       struct au_dykey **key;
3155 +
3156 +       au_hnotify_fin_br(br);
3157 +       /* always, regardless the mount option */
3158 +       au_dr_hino_free(&br->br_dirren);
3159 +       au_xino_put(br);
3160 +
3161 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3162 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3163 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3164 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
3165 +
3166 +       wbr = br->br_wbr;
3167 +       if (wbr) {
3168 +               for (i = 0; i < AuBrWh_Last; i++)
3169 +                       dput(wbr->wbr_wh[i]);
3170 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3171 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3172 +       }
3173 +
3174 +       if (br->br_fhsm) {
3175 +               au_br_fhsm_fin(br->br_fhsm);
3176 +               au_kfree_try_rcu(br->br_fhsm);
3177 +       }
3178 +
3179 +       key = br->br_dykey;
3180 +       for (i = 0; i < AuBrDynOp; i++, key++)
3181 +               if (*key)
3182 +                       au_dy_put(*key);
3183 +               else
3184 +                       break;
3185 +
3186 +       /* recursive lock, s_umount of branch's */
3187 +       /* synchronize_rcu(); */ /* why? */
3188 +       lockdep_off();
3189 +       path_put(&br->br_path);
3190 +       lockdep_on();
3191 +       au_kfree_rcu(wbr);
3192 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3193 +       au_lcnt_wait_for_fin(&br->br_count);
3194 +       /* I don't know why, but percpu_refcount requires this */
3195 +       /* synchronize_rcu(); */
3196 +       au_kfree_rcu(br);
3197 +}
3198 +
3199 +/*
3200 + * frees all branches
3201 + */
3202 +void au_br_free(struct au_sbinfo *sbinfo)
3203 +{
3204 +       aufs_bindex_t bmax;
3205 +       struct au_branch **br;
3206 +
3207 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3208 +
3209 +       bmax = sbinfo->si_bbot + 1;
3210 +       br = sbinfo->si_branch;
3211 +       while (bmax--)
3212 +               au_br_do_free(*br++);
3213 +}
3214 +
3215 +/*
3216 + * find the index of a branch which is specified by @br_id.
3217 + */
3218 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3219 +{
3220 +       aufs_bindex_t bindex, bbot;
3221 +
3222 +       bbot = au_sbbot(sb);
3223 +       for (bindex = 0; bindex <= bbot; bindex++)
3224 +               if (au_sbr_id(sb, bindex) == br_id)
3225 +                       return bindex;
3226 +       return -1;
3227 +}
3228 +
3229 +/* ---------------------------------------------------------------------- */
3230 +
3231 +/*
3232 + * add a branch
3233 + */
3234 +
3235 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3236 +                       struct dentry *h_root)
3237 +{
3238 +       if (unlikely(h_adding == h_root
3239 +                    || au_test_loopback_overlap(sb, h_adding)))
3240 +               return 1;
3241 +       if (h_adding->d_sb != h_root->d_sb)
3242 +               return 0;
3243 +       return au_test_subdir(h_adding, h_root)
3244 +               || au_test_subdir(h_root, h_adding);
3245 +}
3246 +
3247 +/*
3248 + * returns a newly allocated branch. @new_nbranch is a number of branches
3249 + * after adding a branch.
3250 + */
3251 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3252 +                                    int perm)
3253 +{
3254 +       struct au_branch *add_branch;
3255 +       struct dentry *root;
3256 +       struct inode *inode;
3257 +       int err;
3258 +
3259 +       err = -ENOMEM;
3260 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3261 +       if (unlikely(!add_branch))
3262 +               goto out;
3263 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3264 +       if (unlikely(!add_branch->br_xino))
3265 +               goto out_br;
3266 +       err = au_hnotify_init_br(add_branch, perm);
3267 +       if (unlikely(err))
3268 +               goto out_xino;
3269 +
3270 +       if (au_br_writable(perm)) {
3271 +               /* may be freed separately at changing the branch permission */
3272 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3273 +                                            GFP_NOFS);
3274 +               if (unlikely(!add_branch->br_wbr))
3275 +                       goto out_hnotify;
3276 +       }
3277 +
3278 +       if (au_br_fhsm(perm)) {
3279 +               err = au_fhsm_br_alloc(add_branch);
3280 +               if (unlikely(err))
3281 +                       goto out_wbr;
3282 +       }
3283 +
3284 +       root = sb->s_root;
3285 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3286 +       if (!err)
3287 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3288 +       if (!err) {
3289 +               inode = d_inode(root);
3290 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3291 +                                       /*may_shrink*/0);
3292 +       }
3293 +       if (!err)
3294 +               return add_branch; /* success */
3295 +
3296 +out_wbr:
3297 +       au_kfree_rcu(add_branch->br_wbr);
3298 +out_hnotify:
3299 +       au_hnotify_fin_br(add_branch);
3300 +out_xino:
3301 +       au_xino_put(add_branch);
3302 +out_br:
3303 +       au_kfree_rcu(add_branch);
3304 +out:
3305 +       return ERR_PTR(err);
3306 +}
3307 +
3308 +/*
3309 + * test if the branch permission is legal or not.
3310 + */
3311 +static int test_br(struct inode *inode, int brperm, char *path)
3312 +{
3313 +       int err;
3314 +
3315 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3316 +       if (!err)
3317 +               goto out;
3318 +
3319 +       err = -EINVAL;
3320 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3321 +
3322 +out:
3323 +       return err;
3324 +}
3325 +
3326 +/*
3327 + * returns:
3328 + * 0: success, the caller will add it
3329 + * plus: success, it is already unified, the caller should ignore it
3330 + * minus: error
3331 + */
3332 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3333 +{
3334 +       int err;
3335 +       aufs_bindex_t bbot, bindex;
3336 +       struct dentry *root, *h_dentry;
3337 +       struct inode *inode, *h_inode;
3338 +
3339 +       root = sb->s_root;
3340 +       bbot = au_sbbot(sb);
3341 +       if (unlikely(bbot >= 0
3342 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3343 +               err = 1;
3344 +               if (!remount) {
3345 +                       err = -EINVAL;
3346 +                       pr_err("%s duplicated\n", add->pathname);
3347 +               }
3348 +               goto out;
3349 +       }
3350 +
3351 +       err = -ENOSPC; /* -E2BIG; */
3352 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3353 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3354 +               pr_err("number of branches exceeded %s\n", add->pathname);
3355 +               goto out;
3356 +       }
3357 +
3358 +       err = -EDOM;
3359 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3360 +               pr_err("bad index %d\n", add->bindex);
3361 +               goto out;
3362 +       }
3363 +
3364 +       inode = d_inode(add->path.dentry);
3365 +       err = -ENOENT;
3366 +       if (unlikely(!inode->i_nlink)) {
3367 +               pr_err("no existence %s\n", add->pathname);
3368 +               goto out;
3369 +       }
3370 +
3371 +       err = -EINVAL;
3372 +       if (unlikely(inode->i_sb == sb)) {
3373 +               pr_err("%s must be outside\n", add->pathname);
3374 +               goto out;
3375 +       }
3376 +
3377 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3378 +               pr_err("unsupported filesystem, %s (%s)\n",
3379 +                      add->pathname, au_sbtype(inode->i_sb));
3380 +               goto out;
3381 +       }
3382 +
3383 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3384 +               pr_err("already stacked, %s (%s)\n",
3385 +                      add->pathname, au_sbtype(inode->i_sb));
3386 +               goto out;
3387 +       }
3388 +
3389 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3390 +       if (unlikely(err))
3391 +               goto out;
3392 +
3393 +       if (bbot < 0)
3394 +               return 0; /* success */
3395 +
3396 +       err = -EINVAL;
3397 +       for (bindex = 0; bindex <= bbot; bindex++)
3398 +               if (unlikely(test_overlap(sb, add->path.dentry,
3399 +                                         au_h_dptr(root, bindex)))) {
3400 +                       pr_err("%s is overlapped\n", add->pathname);
3401 +                       goto out;
3402 +               }
3403 +
3404 +       err = 0;
3405 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3406 +               h_dentry = au_h_dptr(root, 0);
3407 +               h_inode = d_inode(h_dentry);
3408 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3409 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3410 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3411 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3412 +                               add->pathname,
3413 +                               i_uid_read(inode), i_gid_read(inode),
3414 +                               (inode->i_mode & S_IALLUGO),
3415 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3416 +                               (h_inode->i_mode & S_IALLUGO));
3417 +       }
3418 +
3419 +out:
3420 +       return err;
3421 +}
3422 +
3423 +/*
3424 + * initialize or clean the whiteouts for an adding branch
3425 + */
3426 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3427 +                        int new_perm)
3428 +{
3429 +       int err, old_perm;
3430 +       aufs_bindex_t bindex;
3431 +       struct inode *h_inode;
3432 +       struct au_wbr *wbr;
3433 +       struct au_hinode *hdir;
3434 +       struct dentry *h_dentry;
3435 +
3436 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3437 +       if (unlikely(err))
3438 +               goto out;
3439 +
3440 +       wbr = br->br_wbr;
3441 +       old_perm = br->br_perm;
3442 +       br->br_perm = new_perm;
3443 +       hdir = NULL;
3444 +       h_inode = NULL;
3445 +       bindex = au_br_index(sb, br->br_id);
3446 +       if (0 <= bindex) {
3447 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3448 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3449 +       } else {
3450 +               h_dentry = au_br_dentry(br);
3451 +               h_inode = d_inode(h_dentry);
3452 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3453 +       }
3454 +       if (!wbr)
3455 +               err = au_wh_init(br, sb);
3456 +       else {
3457 +               wbr_wh_write_lock(wbr);
3458 +               err = au_wh_init(br, sb);
3459 +               wbr_wh_write_unlock(wbr);
3460 +       }
3461 +       if (hdir)
3462 +               au_hn_inode_unlock(hdir);
3463 +       else
3464 +               inode_unlock(h_inode);
3465 +       vfsub_mnt_drop_write(au_br_mnt(br));
3466 +       br->br_perm = old_perm;
3467 +
3468 +       if (!err && wbr && !au_br_writable(new_perm)) {
3469 +               au_kfree_rcu(wbr);
3470 +               br->br_wbr = NULL;
3471 +       }
3472 +
3473 +out:
3474 +       return err;
3475 +}
3476 +
3477 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3478 +                      int perm)
3479 +{
3480 +       int err;
3481 +       struct kstatfs kst;
3482 +       struct au_wbr *wbr;
3483 +
3484 +       wbr = br->br_wbr;
3485 +       au_rw_init(&wbr->wbr_wh_rwsem);
3486 +       atomic_set(&wbr->wbr_wh_running, 0);
3487 +
3488 +       /*
3489 +        * a limit for rmdir/rename a dir
3490 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3491 +        */
3492 +       err = vfs_statfs(&br->br_path, &kst);
3493 +       if (unlikely(err))
3494 +               goto out;
3495 +       err = -EINVAL;
3496 +       if (kst.f_namelen >= NAME_MAX)
3497 +               err = au_br_init_wh(sb, br, perm);
3498 +       else
3499 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3500 +                      au_br_dentry(br),
3501 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3502 +
3503 +out:
3504 +       return err;
3505 +}
3506 +
3507 +/* initialize a new branch */
3508 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3509 +                     struct au_opt_add *add)
3510 +{
3511 +       int err;
3512 +       struct au_branch *brbase;
3513 +       struct file *xf;
3514 +       struct inode *h_inode;
3515 +
3516 +       err = 0;
3517 +       br->br_perm = add->perm;
3518 +       br->br_path = add->path; /* set first, path_get() later */
3519 +       spin_lock_init(&br->br_dykey_lock);
3520 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3521 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3522 +       br->br_id = au_new_br_id(sb);
3523 +       AuDebugOn(br->br_id < 0);
3524 +
3525 +       /* always, regardless the given option */
3526 +       err = au_dr_br_init(sb, br, &add->path);
3527 +       if (unlikely(err))
3528 +               goto out_err;
3529 +
3530 +       if (au_br_writable(add->perm)) {
3531 +               err = au_wbr_init(br, sb, add->perm);
3532 +               if (unlikely(err))
3533 +                       goto out_err;
3534 +       }
3535 +
3536 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3537 +               brbase = au_sbr(sb, 0);
3538 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3539 +               AuDebugOn(!xf);
3540 +               h_inode = d_inode(add->path.dentry);
3541 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3542 +               if (unlikely(err)) {
3543 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3544 +                       goto out_err;
3545 +               }
3546 +       }
3547 +
3548 +       sysaufs_br_init(br);
3549 +       path_get(&br->br_path);
3550 +       goto out; /* success */
3551 +
3552 +out_err:
3553 +       memset(&br->br_path, 0, sizeof(br->br_path));
3554 +out:
3555 +       return err;
3556 +}
3557 +
3558 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3559 +                            struct au_branch *br, aufs_bindex_t bbot,
3560 +                            aufs_bindex_t amount)
3561 +{
3562 +       struct au_branch **brp;
3563 +
3564 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3565 +
3566 +       brp = sbinfo->si_branch + bindex;
3567 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3568 +       *brp = br;
3569 +       sbinfo->si_bbot++;
3570 +       if (unlikely(bbot < 0))
3571 +               sbinfo->si_bbot = 0;
3572 +}
3573 +
3574 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3575 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3576 +{
3577 +       struct au_hdentry *hdp;
3578 +
3579 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3580 +
3581 +       hdp = au_hdentry(dinfo, bindex);
3582 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3583 +       au_h_dentry_init(hdp);
3584 +       dinfo->di_bbot++;
3585 +       if (unlikely(bbot < 0))
3586 +               dinfo->di_btop = 0;
3587 +}
3588 +
3589 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3590 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3591 +{
3592 +       struct au_hinode *hip;
3593 +
3594 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3595 +
3596 +       hip = au_hinode(iinfo, bindex);
3597 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3598 +       au_hinode_init(hip);
3599 +       iinfo->ii_bbot++;
3600 +       if (unlikely(bbot < 0))
3601 +               iinfo->ii_btop = 0;
3602 +}
3603 +
3604 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3605 +                        aufs_bindex_t bindex)
3606 +{
3607 +       struct dentry *root, *h_dentry;
3608 +       struct inode *root_inode, *h_inode;
3609 +       aufs_bindex_t bbot, amount;
3610 +
3611 +       root = sb->s_root;
3612 +       root_inode = d_inode(root);
3613 +       bbot = au_sbbot(sb);
3614 +       amount = bbot + 1 - bindex;
3615 +       h_dentry = au_br_dentry(br);
3616 +       au_sbilist_lock();
3617 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3618 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3619 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3620 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3621 +       h_inode = d_inode(h_dentry);
3622 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3623 +       au_sbilist_unlock();
3624 +}
3625 +
3626 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3627 +{
3628 +       int err;
3629 +       aufs_bindex_t bbot, add_bindex;
3630 +       struct dentry *root, *h_dentry;
3631 +       struct inode *root_inode;
3632 +       struct au_branch *add_branch;
3633 +
3634 +       root = sb->s_root;
3635 +       root_inode = d_inode(root);
3636 +       IMustLock(root_inode);
3637 +       IiMustWriteLock(root_inode);
3638 +       err = test_add(sb, add, remount);
3639 +       if (unlikely(err < 0))
3640 +               goto out;
3641 +       if (err) {
3642 +               err = 0;
3643 +               goto out; /* success */
3644 +       }
3645 +
3646 +       bbot = au_sbbot(sb);
3647 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3648 +       err = PTR_ERR(add_branch);
3649 +       if (IS_ERR(add_branch))
3650 +               goto out;
3651 +
3652 +       err = au_br_init(add_branch, sb, add);
3653 +       if (unlikely(err)) {
3654 +               au_br_do_free(add_branch);
3655 +               goto out;
3656 +       }
3657 +
3658 +       add_bindex = add->bindex;
3659 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3660 +       au_br_do_add(sb, add_branch, add_bindex);
3661 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3662 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3663 +
3664 +       h_dentry = add->path.dentry;
3665 +       if (!add_bindex) {
3666 +               au_cpup_attr_all(root_inode, /*force*/1);
3667 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3668 +       } else
3669 +               au_add_nlink(root_inode, d_inode(h_dentry));
3670 +
3671 +out:
3672 +       return err;
3673 +}
3674 +
3675 +/* ---------------------------------------------------------------------- */
3676 +
3677 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3678 +                                      unsigned long long max __maybe_unused,
3679 +                                      void *arg)
3680 +{
3681 +       unsigned long long n;
3682 +       struct file **p, *f;
3683 +       struct hlist_bl_head *files;
3684 +       struct hlist_bl_node *pos;
3685 +       struct au_finfo *finfo;
3686 +
3687 +       n = 0;
3688 +       p = a;
3689 +       files = &au_sbi(sb)->si_files;
3690 +       hlist_bl_lock(files);
3691 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3692 +               f = finfo->fi_file;
3693 +               if (file_count(f)
3694 +                   && !special_file(file_inode(f)->i_mode)) {
3695 +                       get_file(f);
3696 +                       *p++ = f;
3697 +                       n++;
3698 +                       AuDebugOn(n > max);
3699 +               }
3700 +       }
3701 +       hlist_bl_unlock(files);
3702 +
3703 +       return n;
3704 +}
3705 +
3706 +static struct file **au_farray_alloc(struct super_block *sb,
3707 +                                    unsigned long long *max)
3708 +{
3709 +       struct au_sbinfo *sbi;
3710 +
3711 +       sbi = au_sbi(sb);
3712 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3713 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3714 +}
3715 +
3716 +static void au_farray_free(struct file **a, unsigned long long max)
3717 +{
3718 +       unsigned long long ull;
3719 +
3720 +       for (ull = 0; ull < max; ull++)
3721 +               if (a[ull])
3722 +                       fput(a[ull]);
3723 +       kvfree(a);
3724 +}
3725 +
3726 +/* ---------------------------------------------------------------------- */
3727 +
3728 +/*
3729 + * delete a branch
3730 + */
3731 +
3732 +/* to show the line number, do not make it inlined function */
3733 +#define AuVerbose(do_info, fmt, ...) do { \
3734 +       if (do_info) \
3735 +               pr_info(fmt, ##__VA_ARGS__); \
3736 +} while (0)
3737 +
3738 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3739 +                        aufs_bindex_t bbot)
3740 +{
3741 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3742 +}
3743 +
3744 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3745 +                        aufs_bindex_t bbot)
3746 +{
3747 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3748 +}
3749 +
3750 +/*
3751 + * test if the branch is deletable or not.
3752 + */
3753 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3754 +                           unsigned int sigen, const unsigned int verbose)
3755 +{
3756 +       int err, i, j, ndentry;
3757 +       aufs_bindex_t btop, bbot;
3758 +       struct au_dcsub_pages dpages;
3759 +       struct au_dpage *dpage;
3760 +       struct dentry *d;
3761 +
3762 +       err = au_dpages_init(&dpages, GFP_NOFS);
3763 +       if (unlikely(err))
3764 +               goto out;
3765 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3766 +       if (unlikely(err))
3767 +               goto out_dpages;
3768 +
3769 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3770 +               dpage = dpages.dpages + i;
3771 +               ndentry = dpage->ndentry;
3772 +               for (j = 0; !err && j < ndentry; j++) {
3773 +                       d = dpage->dentries[j];
3774 +                       AuDebugOn(au_dcount(d) <= 0);
3775 +                       if (!au_digen_test(d, sigen)) {
3776 +                               di_read_lock_child(d, AuLock_IR);
3777 +                               if (unlikely(au_dbrange_test(d))) {
3778 +                                       di_read_unlock(d, AuLock_IR);
3779 +                                       continue;
3780 +                               }
3781 +                       } else {
3782 +                               di_write_lock_child(d);
3783 +                               if (unlikely(au_dbrange_test(d))) {
3784 +                                       di_write_unlock(d);
3785 +                                       continue;
3786 +                               }
3787 +                               err = au_reval_dpath(d, sigen);
3788 +                               if (!err)
3789 +                                       di_downgrade_lock(d, AuLock_IR);
3790 +                               else {
3791 +                                       di_write_unlock(d);
3792 +                                       break;
3793 +                               }
3794 +                       }
3795 +
3796 +                       /* AuDbgDentry(d); */
3797 +                       btop = au_dbtop(d);
3798 +                       bbot = au_dbbot(d);
3799 +                       if (btop <= bindex
3800 +                           && bindex <= bbot
3801 +                           && au_h_dptr(d, bindex)
3802 +                           && au_test_dbusy(d, btop, bbot)) {
3803 +                               err = -EBUSY;
3804 +                               AuVerbose(verbose, "busy %pd\n", d);
3805 +                               AuDbgDentry(d);
3806 +                       }
3807 +                       di_read_unlock(d, AuLock_IR);
3808 +               }
3809 +       }
3810 +
3811 +out_dpages:
3812 +       au_dpages_free(&dpages);
3813 +out:
3814 +       return err;
3815 +}
3816 +
3817 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3818 +                          unsigned int sigen, const unsigned int verbose)
3819 +{
3820 +       int err;
3821 +       unsigned long long max, ull;
3822 +       struct inode *i, **array;
3823 +       aufs_bindex_t btop, bbot;
3824 +
3825 +       array = au_iarray_alloc(sb, &max);
3826 +       err = PTR_ERR(array);
3827 +       if (IS_ERR(array))
3828 +               goto out;
3829 +
3830 +       err = 0;
3831 +       AuDbg("b%d\n", bindex);
3832 +       for (ull = 0; !err && ull < max; ull++) {
3833 +               i = array[ull];
3834 +               if (unlikely(!i))
3835 +                       break;
3836 +               if (i->i_ino == AUFS_ROOT_INO)
3837 +                       continue;
3838 +
3839 +               /* AuDbgInode(i); */
3840 +               if (au_iigen(i, NULL) == sigen)
3841 +                       ii_read_lock_child(i);
3842 +               else {
3843 +                       ii_write_lock_child(i);
3844 +                       err = au_refresh_hinode_self(i);
3845 +                       au_iigen_dec(i);
3846 +                       if (!err)
3847 +                               ii_downgrade_lock(i);
3848 +                       else {
3849 +                               ii_write_unlock(i);
3850 +                               break;
3851 +                       }
3852 +               }
3853 +
3854 +               btop = au_ibtop(i);
3855 +               bbot = au_ibbot(i);
3856 +               if (btop <= bindex
3857 +                   && bindex <= bbot
3858 +                   && au_h_iptr(i, bindex)
3859 +                   && au_test_ibusy(i, btop, bbot)) {
3860 +                       err = -EBUSY;
3861 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3862 +                       AuDbgInode(i);
3863 +               }
3864 +               ii_read_unlock(i);
3865 +       }
3866 +       au_iarray_free(array, max);
3867 +
3868 +out:
3869 +       return err;
3870 +}
3871 +
3872 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3873 +                             const unsigned int verbose)
3874 +{
3875 +       int err;
3876 +       unsigned int sigen;
3877 +
3878 +       sigen = au_sigen(root->d_sb);
3879 +       DiMustNoWaiters(root);
3880 +       IiMustNoWaiters(d_inode(root));
3881 +       di_write_unlock(root);
3882 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3883 +       if (!err)
3884 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3885 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3886 +
3887 +       return err;
3888 +}
3889 +
3890 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3891 +                        struct file **to_free, int *idx)
3892 +{
3893 +       int err;
3894 +       unsigned char matched, root;
3895 +       aufs_bindex_t bindex, bbot;
3896 +       struct au_fidir *fidir;
3897 +       struct au_hfile *hfile;
3898 +
3899 +       err = 0;
3900 +       root = IS_ROOT(file->f_path.dentry);
3901 +       if (root) {
3902 +               get_file(file);
3903 +               to_free[*idx] = file;
3904 +               (*idx)++;
3905 +               goto out;
3906 +       }
3907 +
3908 +       matched = 0;
3909 +       fidir = au_fi(file)->fi_hdir;
3910 +       AuDebugOn(!fidir);
3911 +       bbot = au_fbbot_dir(file);
3912 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3913 +               hfile = fidir->fd_hfile + bindex;
3914 +               if (!hfile->hf_file)
3915 +                       continue;
3916 +
3917 +               if (hfile->hf_br->br_id == br_id) {
3918 +                       matched = 1;
3919 +                       break;
3920 +               }
3921 +       }
3922 +       if (matched)
3923 +               err = -EBUSY;
3924 +
3925 +out:
3926 +       return err;
3927 +}
3928 +
3929 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3930 +                         struct file **to_free, int opened)
3931 +{
3932 +       int err, idx;
3933 +       unsigned long long ull, max;
3934 +       aufs_bindex_t btop;
3935 +       struct file *file, **array;
3936 +       struct dentry *root;
3937 +       struct au_hfile *hfile;
3938 +
3939 +       array = au_farray_alloc(sb, &max);
3940 +       err = PTR_ERR(array);
3941 +       if (IS_ERR(array))
3942 +               goto out;
3943 +
3944 +       err = 0;
3945 +       idx = 0;
3946 +       root = sb->s_root;
3947 +       di_write_unlock(root);
3948 +       for (ull = 0; ull < max; ull++) {
3949 +               file = array[ull];
3950 +               if (unlikely(!file))
3951 +                       break;
3952 +
3953 +               /* AuDbg("%pD\n", file); */
3954 +               fi_read_lock(file);
3955 +               btop = au_fbtop(file);
3956 +               if (!d_is_dir(file->f_path.dentry)) {
3957 +                       hfile = &au_fi(file)->fi_htop;
3958 +                       if (hfile->hf_br->br_id == br_id)
3959 +                               err = -EBUSY;
3960 +               } else
3961 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3962 +               fi_read_unlock(file);
3963 +               if (unlikely(err))
3964 +                       break;
3965 +       }
3966 +       di_write_lock_child(root);
3967 +       au_farray_free(array, max);
3968 +       AuDebugOn(idx > opened);
3969 +
3970 +out:
3971 +       return err;
3972 +}
3973 +
3974 +static void br_del_file(struct file **to_free, unsigned long long opened,
3975 +                       aufs_bindex_t br_id)
3976 +{
3977 +       unsigned long long ull;
3978 +       aufs_bindex_t bindex, btop, bbot, bfound;
3979 +       struct file *file;
3980 +       struct au_fidir *fidir;
3981 +       struct au_hfile *hfile;
3982 +
3983 +       for (ull = 0; ull < opened; ull++) {
3984 +               file = to_free[ull];
3985 +               if (unlikely(!file))
3986 +                       break;
3987 +
3988 +               /* AuDbg("%pD\n", file); */
3989 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3990 +               bfound = -1;
3991 +               fidir = au_fi(file)->fi_hdir;
3992 +               AuDebugOn(!fidir);
3993 +               fi_write_lock(file);
3994 +               btop = au_fbtop(file);
3995 +               bbot = au_fbbot_dir(file);
3996 +               for (bindex = btop; bindex <= bbot; bindex++) {
3997 +                       hfile = fidir->fd_hfile + bindex;
3998 +                       if (!hfile->hf_file)
3999 +                               continue;
4000 +
4001 +                       if (hfile->hf_br->br_id == br_id) {
4002 +                               bfound = bindex;
4003 +                               break;
4004 +                       }
4005 +               }
4006 +               AuDebugOn(bfound < 0);
4007 +               au_set_h_fptr(file, bfound, NULL);
4008 +               if (bfound == btop) {
4009 +                       for (btop++; btop <= bbot; btop++)
4010 +                               if (au_hf_dir(file, btop)) {
4011 +                                       au_set_fbtop(file, btop);
4012 +                                       break;
4013 +                               }
4014 +               }
4015 +               fi_write_unlock(file);
4016 +       }
4017 +}
4018 +
4019 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
4020 +                            const aufs_bindex_t bindex,
4021 +                            const aufs_bindex_t bbot)
4022 +{
4023 +       struct au_branch **brp, **p;
4024 +
4025 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
4026 +
4027 +       brp = sbinfo->si_branch + bindex;
4028 +       if (bindex < bbot)
4029 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
4030 +       sbinfo->si_branch[0 + bbot] = NULL;
4031 +       sbinfo->si_bbot--;
4032 +
4033 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
4034 +                       /*may_shrink*/1);
4035 +       if (p)
4036 +               sbinfo->si_branch = p;
4037 +       /* harmless error */
4038 +}
4039 +
4040 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
4041 +                            const aufs_bindex_t bbot)
4042 +{
4043 +       struct au_hdentry *hdp, *p;
4044 +
4045 +       AuRwMustWriteLock(&dinfo->di_rwsem);
4046 +
4047 +       hdp = au_hdentry(dinfo, bindex);
4048 +       if (bindex < bbot)
4049 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
4050 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
4051 +       dinfo->di_bbot--;
4052 +
4053 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
4054 +                       /*may_shrink*/1);
4055 +       if (p)
4056 +               dinfo->di_hdentry = p;
4057 +       /* harmless error */
4058 +}
4059 +
4060 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
4061 +                            const aufs_bindex_t bbot)
4062 +{
4063 +       struct au_hinode *hip, *p;
4064 +
4065 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
4066 +
4067 +       hip = au_hinode(iinfo, bindex);
4068 +       if (bindex < bbot)
4069 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
4070 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
4071 +       iinfo->ii_bbot--;
4072 +
4073 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
4074 +                       /*may_shrink*/1);
4075 +       if (p)
4076 +               iinfo->ii_hinode = p;
4077 +       /* harmless error */
4078 +}
4079 +
4080 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
4081 +                        struct au_branch *br)
4082 +{
4083 +       aufs_bindex_t bbot;
4084 +       struct au_sbinfo *sbinfo;
4085 +       struct dentry *root, *h_root;
4086 +       struct inode *inode, *h_inode;
4087 +       struct au_hinode *hinode;
4088 +
4089 +       SiMustWriteLock(sb);
4090 +
4091 +       root = sb->s_root;
4092 +       inode = d_inode(root);
4093 +       sbinfo = au_sbi(sb);
4094 +       bbot = sbinfo->si_bbot;
4095 +
4096 +       h_root = au_h_dptr(root, bindex);
4097 +       hinode = au_hi(inode, bindex);
4098 +       h_inode = au_igrab(hinode->hi_inode);
4099 +       au_hiput(hinode);
4100 +
4101 +       au_sbilist_lock();
4102 +       au_br_do_del_brp(sbinfo, bindex, bbot);
4103 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
4104 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
4105 +       au_sbilist_unlock();
4106 +
4107 +       /* ignore an error */
4108 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
4109 +
4110 +       dput(h_root);
4111 +       iput(h_inode);
4112 +       au_br_do_free(br);
4113 +}
4114 +
4115 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4116 +                                  unsigned long long max, void *arg)
4117 +{
4118 +       return max;
4119 +}
4120 +
4121 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4122 +{
4123 +       int err, rerr, i;
4124 +       unsigned long long opened;
4125 +       unsigned int mnt_flags;
4126 +       aufs_bindex_t bindex, bbot, br_id;
4127 +       unsigned char do_wh, verbose;
4128 +       struct au_branch *br;
4129 +       struct au_wbr *wbr;
4130 +       struct dentry *root;
4131 +       struct file **to_free;
4132 +
4133 +       err = 0;
4134 +       opened = 0;
4135 +       to_free = NULL;
4136 +       root = sb->s_root;
4137 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4138 +       if (bindex < 0) {
4139 +               if (remount)
4140 +                       goto out; /* success */
4141 +               err = -ENOENT;
4142 +               pr_err("%s no such branch\n", del->pathname);
4143 +               goto out;
4144 +       }
4145 +       AuDbg("bindex b%d\n", bindex);
4146 +
4147 +       err = -EBUSY;
4148 +       mnt_flags = au_mntflags(sb);
4149 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4150 +       bbot = au_sbbot(sb);
4151 +       if (unlikely(!bbot)) {
4152 +               AuVerbose(verbose, "no more branches left\n");
4153 +               goto out;
4154 +       }
4155 +
4156 +       br = au_sbr(sb, bindex);
4157 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4158 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4159 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4160 +               goto out;
4161 +       }
4162 +
4163 +       br_id = br->br_id;
4164 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
4165 +       if (unlikely(opened)) {
4166 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4167 +               err = PTR_ERR(to_free);
4168 +               if (IS_ERR(to_free))
4169 +                       goto out;
4170 +
4171 +               err = test_file_busy(sb, br_id, to_free, opened);
4172 +               if (unlikely(err)) {
4173 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4174 +                       goto out;
4175 +               }
4176 +       }
4177 +
4178 +       wbr = br->br_wbr;
4179 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4180 +       if (do_wh) {
4181 +               /* instead of WbrWhMustWriteLock(wbr) */
4182 +               SiMustWriteLock(sb);
4183 +               for (i = 0; i < AuBrWh_Last; i++) {
4184 +                       dput(wbr->wbr_wh[i]);
4185 +                       wbr->wbr_wh[i] = NULL;
4186 +               }
4187 +       }
4188 +
4189 +       err = test_children_busy(root, bindex, verbose);
4190 +       if (unlikely(err)) {
4191 +               if (do_wh)
4192 +                       goto out_wh;
4193 +               goto out;
4194 +       }
4195 +
4196 +       err = 0;
4197 +       if (to_free) {
4198 +               /*
4199 +                * now we confirmed the branch is deletable.
4200 +                * let's free the remaining opened dirs on the branch.
4201 +                */
4202 +               di_write_unlock(root);
4203 +               br_del_file(to_free, opened, br_id);
4204 +               di_write_lock_child(root);
4205 +       }
4206 +
4207 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4208 +       dbgaufs_xino_del(br);           /* remove one */
4209 +       au_br_do_del(sb, bindex, br);
4210 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4211 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4212 +
4213 +       if (!bindex) {
4214 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4215 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4216 +       } else
4217 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4218 +       if (au_opt_test(mnt_flags, PLINK))
4219 +               au_plink_half_refresh(sb, br_id);
4220 +
4221 +       goto out; /* success */
4222 +
4223 +out_wh:
4224 +       /* revert */
4225 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4226 +       if (rerr)
4227 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4228 +                       del->pathname, rerr);
4229 +out:
4230 +       if (to_free)
4231 +               au_farray_free(to_free, opened);
4232 +       return err;
4233 +}
4234 +
4235 +/* ---------------------------------------------------------------------- */
4236 +
4237 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4238 +{
4239 +       int err;
4240 +       aufs_bindex_t btop, bbot;
4241 +       struct aufs_ibusy ibusy;
4242 +       struct inode *inode, *h_inode;
4243 +
4244 +       err = -EPERM;
4245 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4246 +               goto out;
4247 +
4248 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4249 +       if (!err)
4250 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4251 +       if (unlikely(err)) {
4252 +               err = -EFAULT;
4253 +               AuTraceErr(err);
4254 +               goto out;
4255 +       }
4256 +
4257 +       err = -EINVAL;
4258 +       si_read_lock(sb, AuLock_FLUSH);
4259 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4260 +               goto out_unlock;
4261 +
4262 +       err = 0;
4263 +       ibusy.h_ino = 0; /* invalid */
4264 +       inode = ilookup(sb, ibusy.ino);
4265 +       if (!inode
4266 +           || inode->i_ino == AUFS_ROOT_INO
4267 +           || au_is_bad_inode(inode))
4268 +               goto out_unlock;
4269 +
4270 +       ii_read_lock_child(inode);
4271 +       btop = au_ibtop(inode);
4272 +       bbot = au_ibbot(inode);
4273 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4274 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4275 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4276 +                       ibusy.h_ino = h_inode->i_ino;
4277 +       }
4278 +       ii_read_unlock(inode);
4279 +       iput(inode);
4280 +
4281 +out_unlock:
4282 +       si_read_unlock(sb);
4283 +       if (!err) {
4284 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4285 +               if (unlikely(err)) {
4286 +                       err = -EFAULT;
4287 +                       AuTraceErr(err);
4288 +               }
4289 +       }
4290 +out:
4291 +       return err;
4292 +}
4293 +
4294 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4295 +{
4296 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4297 +}
4298 +
4299 +#ifdef CONFIG_COMPAT
4300 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4301 +{
4302 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4303 +}
4304 +#endif
4305 +
4306 +/* ---------------------------------------------------------------------- */
4307 +
4308 +/*
4309 + * change a branch permission
4310 + */
4311 +
4312 +static void au_warn_ima(void)
4313 +{
4314 +#ifdef CONFIG_IMA
4315 +       /* since it doesn't support mark_files_ro() */
4316 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4317 +#endif
4318 +}
4319 +
4320 +static int do_need_sigen_inc(int a, int b)
4321 +{
4322 +       return au_br_whable(a) && !au_br_whable(b);
4323 +}
4324 +
4325 +static int need_sigen_inc(int old, int new)
4326 +{
4327 +       return do_need_sigen_inc(old, new)
4328 +               || do_need_sigen_inc(new, old);
4329 +}
4330 +
4331 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4332 +{
4333 +       int err, do_warn;
4334 +       unsigned int mnt_flags;
4335 +       unsigned long long ull, max;
4336 +       aufs_bindex_t br_id;
4337 +       unsigned char verbose, writer;
4338 +       struct file *file, *hf, **array;
4339 +       struct au_hfile *hfile;
4340 +
4341 +       mnt_flags = au_mntflags(sb);
4342 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4343 +
4344 +       array = au_farray_alloc(sb, &max);
4345 +       err = PTR_ERR(array);
4346 +       if (IS_ERR(array))
4347 +               goto out;
4348 +
4349 +       do_warn = 0;
4350 +       br_id = au_sbr_id(sb, bindex);
4351 +       for (ull = 0; ull < max; ull++) {
4352 +               file = array[ull];
4353 +               if (unlikely(!file))
4354 +                       break;
4355 +
4356 +               /* AuDbg("%pD\n", file); */
4357 +               fi_read_lock(file);
4358 +               if (unlikely(au_test_mmapped(file))) {
4359 +                       err = -EBUSY;
4360 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4361 +                       AuDbgFile(file);
4362 +                       FiMustNoWaiters(file);
4363 +                       fi_read_unlock(file);
4364 +                       goto out_array;
4365 +               }
4366 +
4367 +               hfile = &au_fi(file)->fi_htop;
4368 +               hf = hfile->hf_file;
4369 +               if (!d_is_reg(file->f_path.dentry)
4370 +                   || !(file->f_mode & FMODE_WRITE)
4371 +                   || hfile->hf_br->br_id != br_id
4372 +                   || !(hf->f_mode & FMODE_WRITE))
4373 +                       array[ull] = NULL;
4374 +               else {
4375 +                       do_warn = 1;
4376 +                       get_file(file);
4377 +               }
4378 +
4379 +               FiMustNoWaiters(file);
4380 +               fi_read_unlock(file);
4381 +               fput(file);
4382 +       }
4383 +
4384 +       err = 0;
4385 +       if (do_warn)
4386 +               au_warn_ima();
4387 +
4388 +       for (ull = 0; ull < max; ull++) {
4389 +               file = array[ull];
4390 +               if (!file)
4391 +                       continue;
4392 +
4393 +               /* todo: already flushed? */
4394 +               /*
4395 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4396 +                * approach which resets f_mode and calls mnt_drop_write() and
4397 +                * file_release_write() for each file, because the branch
4398 +                * attribute in aufs world is totally different from the native
4399 +                * fs rw/ro mode.
4400 +               */
4401 +               /* fi_read_lock(file); */
4402 +               hfile = &au_fi(file)->fi_htop;
4403 +               hf = hfile->hf_file;
4404 +               /* fi_read_unlock(file); */
4405 +               spin_lock(&hf->f_lock);
4406 +               writer = !!(hf->f_mode & FMODE_WRITER);
4407 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4408 +               spin_unlock(&hf->f_lock);
4409 +               if (writer) {
4410 +                       put_write_access(file_inode(hf));
4411 +                       __mnt_drop_write(hf->f_path.mnt);
4412 +               }
4413 +       }
4414 +
4415 +out_array:
4416 +       au_farray_free(array, max);
4417 +out:
4418 +       AuTraceErr(err);
4419 +       return err;
4420 +}
4421 +
4422 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4423 +             int *do_refresh)
4424 +{
4425 +       int err, rerr;
4426 +       aufs_bindex_t bindex;
4427 +       struct dentry *root;
4428 +       struct au_branch *br;
4429 +       struct au_br_fhsm *bf;
4430 +
4431 +       root = sb->s_root;
4432 +       bindex = au_find_dbindex(root, mod->h_root);
4433 +       if (bindex < 0) {
4434 +               if (remount)
4435 +                       return 0; /* success */
4436 +               err = -ENOENT;
4437 +               pr_err("%s no such branch\n", mod->path);
4438 +               goto out;
4439 +       }
4440 +       AuDbg("bindex b%d\n", bindex);
4441 +
4442 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4443 +       if (unlikely(err))
4444 +               goto out;
4445 +
4446 +       br = au_sbr(sb, bindex);
4447 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4448 +       if (br->br_perm == mod->perm)
4449 +               return 0; /* success */
4450 +
4451 +       /* pre-allocate for non-fhsm --> fhsm */
4452 +       bf = NULL;
4453 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4454 +               err = au_fhsm_br_alloc(br);
4455 +               if (unlikely(err))
4456 +                       goto out;
4457 +               bf = br->br_fhsm;
4458 +               br->br_fhsm = NULL;
4459 +       }
4460 +
4461 +       if (au_br_writable(br->br_perm)) {
4462 +               /* remove whiteout base */
4463 +               err = au_br_init_wh(sb, br, mod->perm);
4464 +               if (unlikely(err))
4465 +                       goto out_bf;
4466 +
4467 +               if (!au_br_writable(mod->perm)) {
4468 +                       /* rw --> ro, file might be mmapped */
4469 +                       DiMustNoWaiters(root);
4470 +                       IiMustNoWaiters(d_inode(root));
4471 +                       di_write_unlock(root);
4472 +                       err = au_br_mod_files_ro(sb, bindex);
4473 +                       /* aufs_write_lock() calls ..._child() */
4474 +                       di_write_lock_child(root);
4475 +
4476 +                       if (unlikely(err)) {
4477 +                               rerr = -ENOMEM;
4478 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4479 +                                                    GFP_NOFS);
4480 +                               if (br->br_wbr)
4481 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4482 +                               if (unlikely(rerr)) {
4483 +                                       AuIOErr("nested error %d (%d)\n",
4484 +                                               rerr, err);
4485 +                                       br->br_perm = mod->perm;
4486 +                               }
4487 +                       }
4488 +               }
4489 +       } else if (au_br_writable(mod->perm)) {
4490 +               /* ro --> rw */
4491 +               err = -ENOMEM;
4492 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4493 +               if (br->br_wbr) {
4494 +                       err = au_wbr_init(br, sb, mod->perm);
4495 +                       if (unlikely(err)) {
4496 +                               au_kfree_rcu(br->br_wbr);
4497 +                               br->br_wbr = NULL;
4498 +                       }
4499 +               }
4500 +       }
4501 +       if (unlikely(err))
4502 +               goto out_bf;
4503 +
4504 +       if (au_br_fhsm(br->br_perm)) {
4505 +               if (!au_br_fhsm(mod->perm)) {
4506 +                       /* fhsm --> non-fhsm */
4507 +                       au_br_fhsm_fin(br->br_fhsm);
4508 +                       au_kfree_rcu(br->br_fhsm);
4509 +                       br->br_fhsm = NULL;
4510 +               }
4511 +       } else if (au_br_fhsm(mod->perm))
4512 +               /* non-fhsm --> fhsm */
4513 +               br->br_fhsm = bf;
4514 +
4515 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4516 +       br->br_perm = mod->perm;
4517 +       goto out; /* success */
4518 +
4519 +out_bf:
4520 +       au_kfree_try_rcu(bf);
4521 +out:
4522 +       AuTraceErr(err);
4523 +       return err;
4524 +}
4525 +
4526 +/* ---------------------------------------------------------------------- */
4527 +
4528 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4529 +{
4530 +       int err;
4531 +       struct kstatfs kstfs;
4532 +
4533 +       err = vfs_statfs(&br->br_path, &kstfs);
4534 +       if (!err) {
4535 +               stfs->f_blocks = kstfs.f_blocks;
4536 +               stfs->f_bavail = kstfs.f_bavail;
4537 +               stfs->f_files = kstfs.f_files;
4538 +               stfs->f_ffree = kstfs.f_ffree;
4539 +       }
4540 +
4541 +       return err;
4542 +}
4543 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4544 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4545 +++ linux/fs/aufs/branch.h      2018-12-27 13:19:17.708416053 +0100
4546 @@ -0,0 +1,365 @@
4547 +/* SPDX-License-Identifier: GPL-2.0 */
4548 +/*
4549 + * Copyright (C) 2005-2018 Junjiro R. Okajima
4550 + *
4551 + * This program, aufs is free software; you can redistribute it and/or modify
4552 + * it under the terms of the GNU General Public License as published by
4553 + * the Free Software Foundation; either version 2 of the License, or
4554 + * (at your option) any later version.
4555 + *
4556 + * This program is distributed in the hope that it will be useful,
4557 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4558 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4559 + * GNU General Public License for more details.
4560 + *
4561 + * You should have received a copy of the GNU General Public License
4562 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4563 + */
4564 +
4565 +/*
4566 + * branch filesystems and xino for them
4567 + */
4568 +
4569 +#ifndef __AUFS_BRANCH_H__
4570 +#define __AUFS_BRANCH_H__
4571 +
4572 +#ifdef __KERNEL__
4573 +
4574 +#include <linux/mount.h>
4575 +#include "dirren.h"
4576 +#include "dynop.h"
4577 +#include "lcnt.h"
4578 +#include "rwsem.h"
4579 +#include "super.h"
4580 +
4581 +/* ---------------------------------------------------------------------- */
4582 +
4583 +/* a xino file */
4584 +struct au_xino {
4585 +       struct file             **xi_file;
4586 +       unsigned int            xi_nfile;
4587 +
4588 +       struct {
4589 +               spinlock_t              spin;
4590 +               ino_t                   *array;
4591 +               int                     total;
4592 +               /* reserved for future use */
4593 +               /* unsigned long        *bitmap; */
4594 +               wait_queue_head_t       wqh;
4595 +       } xi_nondir;
4596 +
4597 +       struct mutex            xi_mtx; /* protects xi_file array */
4598 +       struct hlist_bl_head    xi_writing;
4599 +
4600 +       atomic_t                xi_truncating;
4601 +
4602 +       struct kref             xi_kref;
4603 +};
4604 +
4605 +/* File-based Hierarchical Storage Management */
4606 +struct au_br_fhsm {
4607 +#ifdef CONFIG_AUFS_FHSM
4608 +       struct mutex            bf_lock;
4609 +       unsigned long           bf_jiffy;
4610 +       struct aufs_stfs        bf_stfs;
4611 +       int                     bf_readable;
4612 +#endif
4613 +};
4614 +
4615 +/* members for writable branch only */
4616 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4617 +struct au_wbr {
4618 +       struct au_rwsem         wbr_wh_rwsem;
4619 +       struct dentry           *wbr_wh[AuBrWh_Last];
4620 +       atomic_t                wbr_wh_running;
4621 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4622 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4623 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4624 +
4625 +       /* mfs mode */
4626 +       unsigned long long      wbr_bytes;
4627 +};
4628 +
4629 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4630 +#define AuBrDynOp (AuDyLast * 4)
4631 +
4632 +#ifdef CONFIG_AUFS_HFSNOTIFY
4633 +/* support for asynchronous destruction */
4634 +struct au_br_hfsnotify {
4635 +       struct fsnotify_group   *hfsn_group;
4636 +};
4637 +#endif
4638 +
4639 +/* sysfs entries */
4640 +struct au_brsysfs {
4641 +       char                    name[16];
4642 +       struct attribute        attr;
4643 +};
4644 +
4645 +enum {
4646 +       AuBrSysfs_BR,
4647 +       AuBrSysfs_BRID,
4648 +       AuBrSysfs_Last
4649 +};
4650 +
4651 +/* protected by superblock rwsem */
4652 +struct au_branch {
4653 +       struct au_xino          *br_xino;
4654 +
4655 +       aufs_bindex_t           br_id;
4656 +
4657 +       int                     br_perm;
4658 +       struct path             br_path;
4659 +       spinlock_t              br_dykey_lock;
4660 +       struct au_dykey         *br_dykey[AuBrDynOp];
4661 +       au_lcnt_t               br_nfiles;      /* opened files */
4662 +       au_lcnt_t               br_count;       /* in-use for other */
4663 +
4664 +       struct au_wbr           *br_wbr;
4665 +       struct au_br_fhsm       *br_fhsm;
4666 +
4667 +#ifdef CONFIG_AUFS_HFSNOTIFY
4668 +       struct au_br_hfsnotify  *br_hfsn;
4669 +#endif
4670 +
4671 +#ifdef CONFIG_SYSFS
4672 +       /* entries under sysfs per mount-point */
4673 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4674 +#endif
4675 +
4676 +#ifdef CONFIG_DEBUG_FS
4677 +       struct dentry            *br_dbgaufs; /* xino */
4678 +#endif
4679 +
4680 +       struct au_dr_br         br_dirren;
4681 +};
4682 +
4683 +/* ---------------------------------------------------------------------- */
4684 +
4685 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4686 +{
4687 +       return br->br_path.mnt;
4688 +}
4689 +
4690 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4691 +{
4692 +       return br->br_path.dentry;
4693 +}
4694 +
4695 +static inline struct super_block *au_br_sb(struct au_branch *br)
4696 +{
4697 +       return au_br_mnt(br)->mnt_sb;
4698 +}
4699 +
4700 +static inline int au_br_rdonly(struct au_branch *br)
4701 +{
4702 +       return (sb_rdonly(au_br_sb(br))
4703 +               || !au_br_writable(br->br_perm))
4704 +               ? -EROFS : 0;
4705 +}
4706 +
4707 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4708 +{
4709 +#ifdef CONFIG_AUFS_HNOTIFY
4710 +       return !(brperm & AuBrPerm_RR);
4711 +#else
4712 +       return 0;
4713 +#endif
4714 +}
4715 +
4716 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4717 +{
4718 +       int err, exec_flag;
4719 +
4720 +       err = 0;
4721 +       exec_flag = oflag & __FMODE_EXEC;
4722 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4723 +               err = -EACCES;
4724 +
4725 +       return err;
4726 +}
4727 +
4728 +static inline void au_xino_get(struct au_branch *br)
4729 +{
4730 +       struct au_xino *xi;
4731 +
4732 +       xi = br->br_xino;
4733 +       if (xi)
4734 +               kref_get(&xi->xi_kref);
4735 +}
4736 +
4737 +static inline int au_xino_count(struct au_branch *br)
4738 +{
4739 +       int v;
4740 +       struct au_xino *xi;
4741 +
4742 +       v = 0;
4743 +       xi = br->br_xino;
4744 +       if (xi)
4745 +               v = kref_read(&xi->xi_kref);
4746 +
4747 +       return v;
4748 +}
4749 +
4750 +/* ---------------------------------------------------------------------- */
4751 +
4752 +/* branch.c */
4753 +struct au_sbinfo;
4754 +void au_br_free(struct au_sbinfo *sinfo);
4755 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4756 +struct au_opt_add;
4757 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4758 +struct au_opt_del;
4759 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4760 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4761 +#ifdef CONFIG_COMPAT
4762 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4763 +#endif
4764 +struct au_opt_mod;
4765 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4766 +             int *do_refresh);
4767 +struct aufs_stfs;
4768 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4769 +
4770 +/* xino.c */
4771 +static const loff_t au_loff_max = LLONG_MAX;
4772 +
4773 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4774 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
4775 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4776 +                            struct file *copy_src);
4777 +struct au_xi_new {
4778 +       struct au_xino *xi;     /* switch between xino and xigen */
4779 +       int idx;
4780 +       struct path *base;
4781 +       struct file *copy_src;
4782 +};
4783 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4784 +
4785 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4786 +                ino_t *ino);
4787 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4788 +                 ino_t ino);
4789 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4790 +                  loff_t *pos);
4791 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4792 +                   size_t size, loff_t *pos);
4793 +
4794 +int au_xib_trunc(struct super_block *sb);
4795 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4796 +
4797 +struct au_xino *au_xino_alloc(unsigned int nfile);
4798 +int au_xino_put(struct au_branch *br);
4799 +struct file *au_xino_file1(struct au_xino *xi);
4800 +
4801 +struct au_opt_xino;
4802 +void au_xino_clr(struct super_block *sb);
4803 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4804 +struct file *au_xino_def(struct super_block *sb);
4805 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4806 +                   struct path *base);
4807 +
4808 +ino_t au_xino_new_ino(struct super_block *sb);
4809 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4810 +
4811 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4812 +                      ino_t h_ino, int idx);
4813 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4814 +                     int *idx);
4815 +
4816 +int au_xino_path(struct seq_file *seq, struct file *file);
4817 +
4818 +/* ---------------------------------------------------------------------- */
4819 +
4820 +/* @idx is signed to accept -1 meaning the first file */
4821 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4822 +{
4823 +       struct file *file;
4824 +
4825 +       file = NULL;
4826 +       if (!xi)
4827 +               goto out;
4828 +
4829 +       if (idx >= 0) {
4830 +               if (idx < xi->xi_nfile)
4831 +                       file = xi->xi_file[idx];
4832 +       } else
4833 +               file = au_xino_file1(xi);
4834 +
4835 +out:
4836 +       return file;
4837 +}
4838 +
4839 +/* ---------------------------------------------------------------------- */
4840 +
4841 +/* Superblock to branch */
4842 +static inline
4843 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4844 +{
4845 +       return au_sbr(sb, bindex)->br_id;
4846 +}
4847 +
4848 +static inline
4849 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4850 +{
4851 +       return au_br_mnt(au_sbr(sb, bindex));
4852 +}
4853 +
4854 +static inline
4855 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4856 +{
4857 +       return au_br_sb(au_sbr(sb, bindex));
4858 +}
4859 +
4860 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4861 +{
4862 +       return au_sbr(sb, bindex)->br_perm;
4863 +}
4864 +
4865 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4866 +{
4867 +       return au_br_whable(au_sbr_perm(sb, bindex));
4868 +}
4869 +
4870 +/* ---------------------------------------------------------------------- */
4871 +
4872 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4873 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4874 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4875 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4876 +/*
4877 +#define wbr_wh_read_trylock_nested(wbr) \
4878 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4879 +#define wbr_wh_write_trylock_nested(wbr) \
4880 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4881 +*/
4882 +
4883 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4884 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4885 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4886 +
4887 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4888 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4889 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4890 +
4891 +/* ---------------------------------------------------------------------- */
4892 +
4893 +#ifdef CONFIG_AUFS_FHSM
4894 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4895 +{
4896 +       mutex_init(&brfhsm->bf_lock);
4897 +       brfhsm->bf_jiffy = 0;
4898 +       brfhsm->bf_readable = 0;
4899 +}
4900 +
4901 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4902 +{
4903 +       mutex_destroy(&brfhsm->bf_lock);
4904 +}
4905 +#else
4906 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4907 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4908 +#endif
4909 +
4910 +#endif /* __KERNEL__ */
4911 +#endif /* __AUFS_BRANCH_H__ */
4912 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4913 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4914 +++ linux/fs/aufs/conf.mk       2018-06-04 09:08:09.181412645 +0200
4915 @@ -0,0 +1,40 @@
4916 +# SPDX-License-Identifier: GPL-2.0
4917 +
4918 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4919 +
4920 +define AuConf
4921 +ifdef ${1}
4922 +AuConfStr += ${1}=${${1}}
4923 +endif
4924 +endef
4925 +
4926 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4927 +       SBILIST \
4928 +       HNOTIFY HFSNOTIFY \
4929 +       EXPORT INO_T_64 \
4930 +       XATTR \
4931 +       FHSM \
4932 +       RDU \
4933 +       DIRREN \
4934 +       SHWH \
4935 +       BR_RAMFS \
4936 +       BR_FUSE POLL \
4937 +       BR_HFSPLUS \
4938 +       BDEV_LOOP \
4939 +       DEBUG MAGIC_SYSRQ
4940 +$(foreach i, ${AuConfAll}, \
4941 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4942 +
4943 +AuConfName = ${obj}/conf.str
4944 +${AuConfName}.tmp: FORCE
4945 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4946 +${AuConfName}: ${AuConfName}.tmp
4947 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4948 +       echo '  GEN    ' $@; \
4949 +       cp -p $< $@; \
4950 +       }
4951 +FORCE:
4952 +clean-files += ${AuConfName} ${AuConfName}.tmp
4953 +${obj}/sysfs.o: ${AuConfName}
4954 +
4955 +-include ${srctree}/${src}/conf_priv.mk
4956 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4957 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4958 +++ linux/fs/aufs/cpup.c        2018-12-27 13:19:17.708416053 +0100
4959 @@ -0,0 +1,1458 @@
4960 +// SPDX-License-Identifier: GPL-2.0
4961 +/*
4962 + * Copyright (C) 2005-2018 Junjiro R. Okajima
4963 + *
4964 + * This program, aufs is free software; you can redistribute it and/or modify
4965 + * it under the terms of the GNU General Public License as published by
4966 + * the Free Software Foundation; either version 2 of the License, or
4967 + * (at your option) any later version.
4968 + *
4969 + * This program is distributed in the hope that it will be useful,
4970 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4971 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4972 + * GNU General Public License for more details.
4973 + *
4974 + * You should have received a copy of the GNU General Public License
4975 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4976 + */
4977 +
4978 +/*
4979 + * copy-up functions, see wbr_policy.c for copy-down
4980 + */
4981 +
4982 +#include <linux/fs_stack.h>
4983 +#include <linux/mm.h>
4984 +#include <linux/task_work.h>
4985 +#include "aufs.h"
4986 +
4987 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4988 +{
4989 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4990 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4991 +
4992 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4993 +
4994 +       dst->i_flags |= iflags & ~mask;
4995 +       if (au_test_fs_notime(dst->i_sb))
4996 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4997 +}
4998 +
4999 +void au_cpup_attr_timesizes(struct inode *inode)
5000 +{
5001 +       struct inode *h_inode;
5002 +
5003 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5004 +       fsstack_copy_attr_times(inode, h_inode);
5005 +       fsstack_copy_inode_size(inode, h_inode);
5006 +}
5007 +
5008 +void au_cpup_attr_nlink(struct inode *inode, int force)
5009 +{
5010 +       struct inode *h_inode;
5011 +       struct super_block *sb;
5012 +       aufs_bindex_t bindex, bbot;
5013 +
5014 +       sb = inode->i_sb;
5015 +       bindex = au_ibtop(inode);
5016 +       h_inode = au_h_iptr(inode, bindex);
5017 +       if (!force
5018 +           && !S_ISDIR(h_inode->i_mode)
5019 +           && au_opt_test(au_mntflags(sb), PLINK)
5020 +           && au_plink_test(inode))
5021 +               return;
5022 +
5023 +       /*
5024 +        * 0 can happen in revalidating.
5025 +        * h_inode->i_mutex may not be held here, but it is harmless since once
5026 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
5027 +        * case.
5028 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
5029 +        *       the incorrect link count.
5030 +        */
5031 +       set_nlink(inode, h_inode->i_nlink);
5032 +
5033 +       /*
5034 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
5035 +        * it may includes whplink directory.
5036 +        */
5037 +       if (S_ISDIR(h_inode->i_mode)) {
5038 +               bbot = au_ibbot(inode);
5039 +               for (bindex++; bindex <= bbot; bindex++) {
5040 +                       h_inode = au_h_iptr(inode, bindex);
5041 +                       if (h_inode)
5042 +                               au_add_nlink(inode, h_inode);
5043 +               }
5044 +       }
5045 +}
5046 +
5047 +void au_cpup_attr_changeable(struct inode *inode)
5048 +{
5049 +       struct inode *h_inode;
5050 +
5051 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5052 +       inode->i_mode = h_inode->i_mode;
5053 +       inode->i_uid = h_inode->i_uid;
5054 +       inode->i_gid = h_inode->i_gid;
5055 +       au_cpup_attr_timesizes(inode);
5056 +       au_cpup_attr_flags(inode, h_inode->i_flags);
5057 +}
5058 +
5059 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
5060 +{
5061 +       struct au_iinfo *iinfo = au_ii(inode);
5062 +
5063 +       IiMustWriteLock(inode);
5064 +
5065 +       iinfo->ii_higen = h_inode->i_generation;
5066 +       iinfo->ii_hsb1 = h_inode->i_sb;
5067 +}
5068 +
5069 +void au_cpup_attr_all(struct inode *inode, int force)
5070 +{
5071 +       struct inode *h_inode;
5072 +
5073 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
5074 +       au_cpup_attr_changeable(inode);
5075 +       if (inode->i_nlink > 0)
5076 +               au_cpup_attr_nlink(inode, force);
5077 +       inode->i_rdev = h_inode->i_rdev;
5078 +       inode->i_blkbits = h_inode->i_blkbits;
5079 +       au_cpup_igen(inode, h_inode);
5080 +}
5081 +
5082 +/* ---------------------------------------------------------------------- */
5083 +
5084 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
5085 +
5086 +/* keep the timestamps of the parent dir when cpup */
5087 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5088 +                   struct path *h_path)
5089 +{
5090 +       struct inode *h_inode;
5091 +
5092 +       dt->dt_dentry = dentry;
5093 +       dt->dt_h_path = *h_path;
5094 +       h_inode = d_inode(h_path->dentry);
5095 +       dt->dt_atime = h_inode->i_atime;
5096 +       dt->dt_mtime = h_inode->i_mtime;
5097 +       /* smp_mb(); */
5098 +}
5099 +
5100 +void au_dtime_revert(struct au_dtime *dt)
5101 +{
5102 +       struct iattr attr;
5103 +       int err;
5104 +
5105 +       attr.ia_atime = dt->dt_atime;
5106 +       attr.ia_mtime = dt->dt_mtime;
5107 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5108 +               | ATTR_ATIME | ATTR_ATIME_SET;
5109 +
5110 +       /* no delegation since this is a directory */
5111 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5112 +       if (unlikely(err))
5113 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5114 +}
5115 +
5116 +/* ---------------------------------------------------------------------- */
5117 +
5118 +/* internal use only */
5119 +struct au_cpup_reg_attr {
5120 +       int             valid;
5121 +       struct kstat    st;
5122 +       unsigned int    iflags; /* inode->i_flags */
5123 +};
5124 +
5125 +static noinline_for_stack
5126 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5127 +              struct au_cpup_reg_attr *h_src_attr)
5128 +{
5129 +       int err, sbits, icex;
5130 +       unsigned int mnt_flags;
5131 +       unsigned char verbose;
5132 +       struct iattr ia;
5133 +       struct path h_path;
5134 +       struct inode *h_isrc, *h_idst;
5135 +       struct kstat *h_st;
5136 +       struct au_branch *br;
5137 +
5138 +       h_path.dentry = au_h_dptr(dst, bindex);
5139 +       h_idst = d_inode(h_path.dentry);
5140 +       br = au_sbr(dst->d_sb, bindex);
5141 +       h_path.mnt = au_br_mnt(br);
5142 +       h_isrc = d_inode(h_src);
5143 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5144 +               | ATTR_ATIME | ATTR_MTIME
5145 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5146 +       if (h_src_attr && h_src_attr->valid) {
5147 +               h_st = &h_src_attr->st;
5148 +               ia.ia_uid = h_st->uid;
5149 +               ia.ia_gid = h_st->gid;
5150 +               ia.ia_atime = h_st->atime;
5151 +               ia.ia_mtime = h_st->mtime;
5152 +               if (h_idst->i_mode != h_st->mode
5153 +                   && !S_ISLNK(h_idst->i_mode)) {
5154 +                       ia.ia_valid |= ATTR_MODE;
5155 +                       ia.ia_mode = h_st->mode;
5156 +               }
5157 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5158 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5159 +       } else {
5160 +               ia.ia_uid = h_isrc->i_uid;
5161 +               ia.ia_gid = h_isrc->i_gid;
5162 +               ia.ia_atime = h_isrc->i_atime;
5163 +               ia.ia_mtime = h_isrc->i_mtime;
5164 +               if (h_idst->i_mode != h_isrc->i_mode
5165 +                   && !S_ISLNK(h_idst->i_mode)) {
5166 +                       ia.ia_valid |= ATTR_MODE;
5167 +                       ia.ia_mode = h_isrc->i_mode;
5168 +               }
5169 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5170 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5171 +       }
5172 +       /* no delegation since it is just created */
5173 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5174 +
5175 +       /* is this nfs only? */
5176 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5177 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5178 +               ia.ia_mode = h_isrc->i_mode;
5179 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5180 +       }
5181 +
5182 +       icex = br->br_perm & AuBrAttr_ICEX;
5183 +       if (!err) {
5184 +               mnt_flags = au_mntflags(dst->d_sb);
5185 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5186 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5187 +       }
5188 +
5189 +       return err;
5190 +}
5191 +
5192 +/* ---------------------------------------------------------------------- */
5193 +
5194 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5195 +                          char *buf, unsigned long blksize)
5196 +{
5197 +       int err;
5198 +       size_t sz, rbytes, wbytes;
5199 +       unsigned char all_zero;
5200 +       char *p, *zp;
5201 +       struct inode *h_inode;
5202 +       /* reduce stack usage */
5203 +       struct iattr *ia;
5204 +
5205 +       zp = page_address(ZERO_PAGE(0));
5206 +       if (unlikely(!zp))
5207 +               return -ENOMEM; /* possible? */
5208 +
5209 +       err = 0;
5210 +       all_zero = 0;
5211 +       while (len) {
5212 +               AuDbg("len %lld\n", len);
5213 +               sz = blksize;
5214 +               if (len < blksize)
5215 +                       sz = len;
5216 +
5217 +               rbytes = 0;
5218 +               /* todo: signal_pending? */
5219 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5220 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5221 +                       err = rbytes;
5222 +               }
5223 +               if (unlikely(err < 0))
5224 +                       break;
5225 +
5226 +               all_zero = 0;
5227 +               if (len >= rbytes && rbytes == blksize)
5228 +                       all_zero = !memcmp(buf, zp, rbytes);
5229 +               if (!all_zero) {
5230 +                       wbytes = rbytes;
5231 +                       p = buf;
5232 +                       while (wbytes) {
5233 +                               size_t b;
5234 +
5235 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5236 +                               err = b;
5237 +                               /* todo: signal_pending? */
5238 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5239 +                                       continue;
5240 +                               if (unlikely(err < 0))
5241 +                                       break;
5242 +                               wbytes -= b;
5243 +                               p += b;
5244 +                       }
5245 +                       if (unlikely(err < 0))
5246 +                               break;
5247 +               } else {
5248 +                       loff_t res;
5249 +
5250 +                       AuLabel(hole);
5251 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5252 +                       err = res;
5253 +                       if (unlikely(res < 0))
5254 +                               break;
5255 +               }
5256 +               len -= rbytes;
5257 +               err = 0;
5258 +       }
5259 +
5260 +       /* the last block may be a hole */
5261 +       if (!err && all_zero) {
5262 +               AuLabel(last hole);
5263 +
5264 +               err = 1;
5265 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5266 +                       /* nfs requires this step to make last hole */
5267 +                       /* is this only nfs? */
5268 +                       do {
5269 +                               /* todo: signal_pending? */
5270 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5271 +                       } while (err == -EAGAIN || err == -EINTR);
5272 +                       if (err == 1)
5273 +                               dst->f_pos--;
5274 +               }
5275 +
5276 +               if (err == 1) {
5277 +                       ia = (void *)buf;
5278 +                       ia->ia_size = dst->f_pos;
5279 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5280 +                       ia->ia_file = dst;
5281 +                       h_inode = file_inode(dst);
5282 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5283 +                       /* no delegation since it is just created */
5284 +                       err = vfsub_notify_change(&dst->f_path, ia,
5285 +                                                 /*delegated*/NULL);
5286 +                       inode_unlock(h_inode);
5287 +               }
5288 +       }
5289 +
5290 +       return err;
5291 +}
5292 +
5293 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5294 +{
5295 +       int err;
5296 +       unsigned long blksize;
5297 +       unsigned char do_kfree;
5298 +       char *buf;
5299 +       struct super_block *h_sb;
5300 +
5301 +       err = -ENOMEM;
5302 +       h_sb = file_inode(dst)->i_sb;
5303 +       blksize = h_sb->s_blocksize;
5304 +       if (!blksize || PAGE_SIZE < blksize)
5305 +               blksize = PAGE_SIZE;
5306 +       AuDbg("blksize %lu\n", blksize);
5307 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5308 +       if (do_kfree)
5309 +               buf = kmalloc(blksize, GFP_NOFS);
5310 +       else
5311 +               buf = (void *)__get_free_page(GFP_NOFS);
5312 +       if (unlikely(!buf))
5313 +               goto out;
5314 +
5315 +       if (len > (1 << 22))
5316 +               AuDbg("copying a large file %lld\n", (long long)len);
5317 +
5318 +       src->f_pos = 0;
5319 +       dst->f_pos = 0;
5320 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5321 +       if (do_kfree) {
5322 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5323 +               au_kfree_do_rcu(buf);
5324 +       } else
5325 +               free_page((unsigned long)buf);
5326 +
5327 +out:
5328 +       return err;
5329 +}
5330 +
5331 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5332 +{
5333 +       int err;
5334 +       struct super_block *h_src_sb;
5335 +       struct inode *h_src_inode;
5336 +
5337 +       h_src_inode = file_inode(src);
5338 +       h_src_sb = h_src_inode->i_sb;
5339 +
5340 +       /* XFS acquires inode_lock */
5341 +       if (!au_test_xfs(h_src_sb))
5342 +               err = au_copy_file(dst, src, len);
5343 +       else {
5344 +               inode_unlock_shared(h_src_inode);
5345 +               err = au_copy_file(dst, src, len);
5346 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5347 +       }
5348 +
5349 +       return err;
5350 +}
5351 +
5352 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5353 +{
5354 +       int err;
5355 +       loff_t lo;
5356 +       struct super_block *h_src_sb;
5357 +       struct inode *h_src_inode;
5358 +
5359 +       h_src_inode = file_inode(src);
5360 +       h_src_sb = h_src_inode->i_sb;
5361 +       if (h_src_sb != file_inode(dst)->i_sb
5362 +           || !dst->f_op->remap_file_range) {
5363 +               err = au_do_copy(dst, src, len);
5364 +               goto out;
5365 +       }
5366 +
5367 +       if (!au_test_nfs(h_src_sb)) {
5368 +               inode_unlock_shared(h_src_inode);
5369 +               lo = vfsub_clone_file_range(src, dst, len);
5370 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5371 +       } else
5372 +               lo = vfsub_clone_file_range(src, dst, len);
5373 +       if (lo == len) {
5374 +               err = 0;
5375 +               goto out; /* success */
5376 +       } else if (lo >= 0)
5377 +               /* todo: possible? */
5378 +               /* paritially succeeded */
5379 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5380 +       else if (lo != -EOPNOTSUPP) {
5381 +               /* older XFS has a condition in cloning */
5382 +               err = lo;
5383 +               goto out;
5384 +       }
5385 +
5386 +       /* the backend fs on NFS may not support cloning */
5387 +       err = au_do_copy(dst, src, len);
5388 +
5389 +out:
5390 +       AuTraceErr(err);
5391 +       return err;
5392 +}
5393 +
5394 +/*
5395 + * to support a sparse file which is opened with O_APPEND,
5396 + * we need to close the file.
5397 + */
5398 +static int au_cp_regular(struct au_cp_generic *cpg)
5399 +{
5400 +       int err, i;
5401 +       enum { SRC, DST };
5402 +       struct {
5403 +               aufs_bindex_t bindex;
5404 +               unsigned int flags;
5405 +               struct dentry *dentry;
5406 +               int force_wr;
5407 +               struct file *file;
5408 +       } *f, file[] = {
5409 +               {
5410 +                       .bindex = cpg->bsrc,
5411 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5412 +               },
5413 +               {
5414 +                       .bindex = cpg->bdst,
5415 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5416 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5417 +               }
5418 +       };
5419 +       struct au_branch *br;
5420 +       struct super_block *sb, *h_src_sb;
5421 +       struct inode *h_src_inode;
5422 +       struct task_struct *tsk = current;
5423 +
5424 +       /* bsrc branch can be ro/rw. */
5425 +       sb = cpg->dentry->d_sb;
5426 +       f = file;
5427 +       for (i = 0; i < 2; i++, f++) {
5428 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5429 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5430 +                                   /*file*/NULL, f->force_wr);
5431 +               if (IS_ERR(f->file)) {
5432 +                       err = PTR_ERR(f->file);
5433 +                       if (i == SRC)
5434 +                               goto out;
5435 +                       else
5436 +                               goto out_src;
5437 +               }
5438 +       }
5439 +
5440 +       /* try stopping to update while we copyup */
5441 +       h_src_inode = d_inode(file[SRC].dentry);
5442 +       h_src_sb = h_src_inode->i_sb;
5443 +       if (!au_test_nfs(h_src_sb))
5444 +               IMustLock(h_src_inode);
5445 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5446 +
5447 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5448 +       if (tsk->flags & PF_KTHREAD)
5449 +               __fput_sync(file[DST].file);
5450 +       else {
5451 +               /* it happened actually */
5452 +               fput(file[DST].file);
5453 +               /*
5454 +                * too bad.
5455 +                * we have to call both since we don't know which place the file
5456 +                * was added to.
5457 +                */
5458 +               task_work_run();
5459 +               flush_delayed_fput();
5460 +       }
5461 +       br = au_sbr(sb, file[DST].bindex);
5462 +       au_lcnt_dec(&br->br_nfiles);
5463 +
5464 +out_src:
5465 +       fput(file[SRC].file);
5466 +       br = au_sbr(sb, file[SRC].bindex);
5467 +       au_lcnt_dec(&br->br_nfiles);
5468 +out:
5469 +       return err;
5470 +}
5471 +
5472 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5473 +                             struct au_cpup_reg_attr *h_src_attr)
5474 +{
5475 +       int err, rerr;
5476 +       loff_t l;
5477 +       struct path h_path;
5478 +       struct inode *h_src_inode, *h_dst_inode;
5479 +
5480 +       err = 0;
5481 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5482 +       l = i_size_read(h_src_inode);
5483 +       if (cpg->len == -1 || l < cpg->len)
5484 +               cpg->len = l;
5485 +       if (cpg->len) {
5486 +               /* try stopping to update while we are referencing */
5487 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5488 +               au_pin_hdir_unlock(cpg->pin);
5489 +
5490 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5491 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5492 +               h_src_attr->iflags = h_src_inode->i_flags;
5493 +               if (!au_test_nfs(h_src_inode->i_sb))
5494 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5495 +               else {
5496 +                       inode_unlock_shared(h_src_inode);
5497 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5498 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5499 +               }
5500 +               if (unlikely(err)) {
5501 +                       inode_unlock_shared(h_src_inode);
5502 +                       goto out;
5503 +               }
5504 +               h_src_attr->valid = 1;
5505 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5506 +                       err = au_cp_regular(cpg);
5507 +                       inode_unlock_shared(h_src_inode);
5508 +               } else {
5509 +                       inode_unlock_shared(h_src_inode);
5510 +                       err = au_cp_regular(cpg);
5511 +               }
5512 +               rerr = au_pin_hdir_relock(cpg->pin);
5513 +               if (!err && rerr)
5514 +                       err = rerr;
5515 +       }
5516 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5517 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5518 +               h_dst_inode = d_inode(h_path.dentry);
5519 +               spin_lock(&h_dst_inode->i_lock);
5520 +               h_dst_inode->i_state |= I_LINKABLE;
5521 +               spin_unlock(&h_dst_inode->i_lock);
5522 +       }
5523 +
5524 +out:
5525 +       return err;
5526 +}
5527 +
5528 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5529 +                             struct inode *h_dir)
5530 +{
5531 +       int err, symlen;
5532 +       mm_segment_t old_fs;
5533 +       union {
5534 +               char *k;
5535 +               char __user *u;
5536 +       } sym;
5537 +
5538 +       err = -ENOMEM;
5539 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5540 +       if (unlikely(!sym.k))
5541 +               goto out;
5542 +
5543 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5544 +       old_fs = get_fs();
5545 +       set_fs(KERNEL_DS);
5546 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5547 +       err = symlen;
5548 +       set_fs(old_fs);
5549 +
5550 +       if (symlen > 0) {
5551 +               sym.k[symlen] = 0;
5552 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5553 +       }
5554 +       free_page((unsigned long)sym.k);
5555 +
5556 +out:
5557 +       return err;
5558 +}
5559 +
5560 +/*
5561 + * regardless 'acl' option, reset all ACL.
5562 + * All ACL will be copied up later from the original entry on the lower branch.
5563 + */
5564 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5565 +{
5566 +       int err;
5567 +       struct dentry *h_dentry;
5568 +       struct inode *h_inode;
5569 +
5570 +       h_dentry = h_path->dentry;
5571 +       h_inode = d_inode(h_dentry);
5572 +       /* forget_all_cached_acls(h_inode)); */
5573 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5574 +       AuTraceErr(err);
5575 +       if (err == -EOPNOTSUPP)
5576 +               err = 0;
5577 +       if (!err)
5578 +               err = vfsub_acl_chmod(h_inode, mode);
5579 +
5580 +       AuTraceErr(err);
5581 +       return err;
5582 +}
5583 +
5584 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5585 +                         struct inode *h_dir, struct path *h_path)
5586 +{
5587 +       int err;
5588 +       struct inode *dir, *inode;
5589 +
5590 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5591 +       AuTraceErr(err);
5592 +       if (err == -EOPNOTSUPP)
5593 +               err = 0;
5594 +       if (unlikely(err))
5595 +               goto out;
5596 +
5597 +       /*
5598 +        * strange behaviour from the users view,
5599 +        * particularly setattr case
5600 +        */
5601 +       dir = d_inode(dst_parent);
5602 +       if (au_ibtop(dir) == cpg->bdst)
5603 +               au_cpup_attr_nlink(dir, /*force*/1);
5604 +       inode = d_inode(cpg->dentry);
5605 +       au_cpup_attr_nlink(inode, /*force*/1);
5606 +
5607 +out:
5608 +       return err;
5609 +}
5610 +
5611 +static noinline_for_stack
5612 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5613 +              struct au_cpup_reg_attr *h_src_attr)
5614 +{
5615 +       int err;
5616 +       umode_t mode;
5617 +       unsigned int mnt_flags;
5618 +       unsigned char isdir, isreg, force;
5619 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5620 +       struct au_dtime dt;
5621 +       struct path h_path;
5622 +       struct dentry *h_src, *h_dst, *h_parent;
5623 +       struct inode *h_inode, *h_dir;
5624 +       struct super_block *sb;
5625 +
5626 +       /* bsrc branch can be ro/rw. */
5627 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5628 +       h_inode = d_inode(h_src);
5629 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5630 +
5631 +       /* try stopping to be referenced while we are creating */
5632 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5633 +       if (au_ftest_cpup(cpg->flags, RENAME))
5634 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5635 +                                 AUFS_WH_PFX_LEN));
5636 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5637 +       h_dir = d_inode(h_parent);
5638 +       IMustLock(h_dir);
5639 +       AuDebugOn(h_parent != h_dst->d_parent);
5640 +
5641 +       sb = cpg->dentry->d_sb;
5642 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5643 +       if (do_dt) {
5644 +               h_path.dentry = h_parent;
5645 +               au_dtime_store(&dt, dst_parent, &h_path);
5646 +       }
5647 +       h_path.dentry = h_dst;
5648 +
5649 +       isreg = 0;
5650 +       isdir = 0;
5651 +       mode = h_inode->i_mode;
5652 +       switch (mode & S_IFMT) {
5653 +       case S_IFREG:
5654 +               isreg = 1;
5655 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5656 +               if (!err)
5657 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5658 +               break;
5659 +       case S_IFDIR:
5660 +               isdir = 1;
5661 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5662 +               if (!err)
5663 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5664 +               break;
5665 +       case S_IFLNK:
5666 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5667 +               break;
5668 +       case S_IFCHR:
5669 +       case S_IFBLK:
5670 +               AuDebugOn(!capable(CAP_MKNOD));
5671 +               /*FALLTHROUGH*/
5672 +       case S_IFIFO:
5673 +       case S_IFSOCK:
5674 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5675 +               break;
5676 +       default:
5677 +               AuIOErr("Unknown inode type 0%o\n", mode);
5678 +               err = -EIO;
5679 +       }
5680 +       if (!err)
5681 +               err = au_reset_acl(h_dir, &h_path, mode);
5682 +
5683 +       mnt_flags = au_mntflags(sb);
5684 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5685 +           && !isdir
5686 +           && au_opt_test(mnt_flags, XINO)
5687 +           && (h_inode->i_nlink == 1
5688 +               || (h_inode->i_state & I_LINKABLE))
5689 +           /* todo: unnecessary? */
5690 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5691 +           && cpg->bdst < cpg->bsrc
5692 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5693 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5694 +               /* ignore this error */
5695 +
5696 +       if (!err) {
5697 +               force = 0;
5698 +               if (isreg) {
5699 +                       force = !!cpg->len;
5700 +                       if (cpg->len == -1)
5701 +                               force = !!i_size_read(h_inode);
5702 +               }
5703 +               au_fhsm_wrote(sb, cpg->bdst, force);
5704 +       }
5705 +
5706 +       if (do_dt)
5707 +               au_dtime_revert(&dt);
5708 +       return err;
5709 +}
5710 +
5711 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5712 +{
5713 +       int err;
5714 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5715 +       struct inode *h_dir;
5716 +       aufs_bindex_t bdst;
5717 +
5718 +       dentry = cpg->dentry;
5719 +       bdst = cpg->bdst;
5720 +       h_dentry = au_h_dptr(dentry, bdst);
5721 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5722 +               dget(h_dentry);
5723 +               au_set_h_dptr(dentry, bdst, NULL);
5724 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5725 +               if (!err)
5726 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5727 +               au_set_h_dptr(dentry, bdst, h_dentry);
5728 +       } else {
5729 +               err = 0;
5730 +               parent = dget_parent(dentry);
5731 +               h_parent = au_h_dptr(parent, bdst);
5732 +               dput(parent);
5733 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5734 +               if (IS_ERR(h_path->dentry))
5735 +                       err = PTR_ERR(h_path->dentry);
5736 +       }
5737 +       if (unlikely(err))
5738 +               goto out;
5739 +
5740 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5741 +       h_dir = d_inode(h_parent);
5742 +       IMustLock(h_dir);
5743 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5744 +       /* no delegation since it is just created */
5745 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5746 +                          /*flags*/0);
5747 +       dput(h_path->dentry);
5748 +
5749 +out:
5750 +       return err;
5751 +}
5752 +
5753 +/*
5754 + * copyup the @dentry from @bsrc to @bdst.
5755 + * the caller must set the both of lower dentries.
5756 + * @len is for truncating when it is -1 copyup the entire file.
5757 + * in link/rename cases, @dst_parent may be different from the real one.
5758 + * basic->bsrc can be larger than basic->bdst.
5759 + * aufs doesn't touch the credential so
5760 + * security_inode_copy_up{,_xattr}() are unnecessary.
5761 + */
5762 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5763 +{
5764 +       int err, rerr;
5765 +       aufs_bindex_t old_ibtop;
5766 +       unsigned char isdir, plink;
5767 +       struct dentry *h_src, *h_dst, *h_parent;
5768 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5769 +       struct super_block *sb;
5770 +       struct au_branch *br;
5771 +       /* to reduce stack size */
5772 +       struct {
5773 +               struct au_dtime dt;
5774 +               struct path h_path;
5775 +               struct au_cpup_reg_attr h_src_attr;
5776 +       } *a;
5777 +
5778 +       err = -ENOMEM;
5779 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5780 +       if (unlikely(!a))
5781 +               goto out;
5782 +       a->h_src_attr.valid = 0;
5783 +
5784 +       sb = cpg->dentry->d_sb;
5785 +       br = au_sbr(sb, cpg->bdst);
5786 +       a->h_path.mnt = au_br_mnt(br);
5787 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5788 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5789 +       h_dir = d_inode(h_parent);
5790 +       IMustLock(h_dir);
5791 +
5792 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5793 +       inode = d_inode(cpg->dentry);
5794 +
5795 +       if (!dst_parent)
5796 +               dst_parent = dget_parent(cpg->dentry);
5797 +       else
5798 +               dget(dst_parent);
5799 +
5800 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5801 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5802 +       if (dst_inode) {
5803 +               if (unlikely(!plink)) {
5804 +                       err = -EIO;
5805 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5806 +                               "but plink is disabled\n",
5807 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5808 +                       goto out_parent;
5809 +               }
5810 +
5811 +               if (dst_inode->i_nlink) {
5812 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5813 +
5814 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5815 +                       err = PTR_ERR(h_src);
5816 +                       if (IS_ERR(h_src))
5817 +                               goto out_parent;
5818 +                       if (unlikely(d_is_negative(h_src))) {
5819 +                               err = -EIO;
5820 +                               AuIOErr("i%lu exists on b%d "
5821 +                                       "but not pseudo-linked\n",
5822 +                                       inode->i_ino, cpg->bdst);
5823 +                               dput(h_src);
5824 +                               goto out_parent;
5825 +                       }
5826 +
5827 +                       if (do_dt) {
5828 +                               a->h_path.dentry = h_parent;
5829 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5830 +                       }
5831 +
5832 +                       a->h_path.dentry = h_dst;
5833 +                       delegated = NULL;
5834 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5835 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5836 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5837 +                       if (do_dt)
5838 +                               au_dtime_revert(&a->dt);
5839 +                       if (unlikely(err == -EWOULDBLOCK)) {
5840 +                               pr_warn("cannot retry for NFSv4 delegation"
5841 +                                       " for an internal link\n");
5842 +                               iput(delegated);
5843 +                       }
5844 +                       dput(h_src);
5845 +                       goto out_parent;
5846 +               } else
5847 +                       /* todo: cpup_wh_file? */
5848 +                       /* udba work */
5849 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5850 +       }
5851 +
5852 +       isdir = S_ISDIR(inode->i_mode);
5853 +       old_ibtop = au_ibtop(inode);
5854 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5855 +       if (unlikely(err))
5856 +               goto out_rev;
5857 +       dst_inode = d_inode(h_dst);
5858 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5859 +       /* todo: necessary? */
5860 +       /* au_pin_hdir_unlock(cpg->pin); */
5861 +
5862 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5863 +       if (unlikely(err)) {
5864 +               /* todo: necessary? */
5865 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5866 +               inode_unlock(dst_inode);
5867 +               goto out_rev;
5868 +       }
5869 +
5870 +       if (cpg->bdst < old_ibtop) {
5871 +               if (S_ISREG(inode->i_mode)) {
5872 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5873 +                       if (unlikely(err)) {
5874 +                               /* ignore an error */
5875 +                               /* au_pin_hdir_relock(cpg->pin); */
5876 +                               inode_unlock(dst_inode);
5877 +                               goto out_rev;
5878 +                       }
5879 +               }
5880 +               au_set_ibtop(inode, cpg->bdst);
5881 +       } else
5882 +               au_set_ibbot(inode, cpg->bdst);
5883 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5884 +                     au_hi_flags(inode, isdir));
5885 +
5886 +       /* todo: necessary? */
5887 +       /* err = au_pin_hdir_relock(cpg->pin); */
5888 +       inode_unlock(dst_inode);
5889 +       if (unlikely(err))
5890 +               goto out_rev;
5891 +
5892 +       src_inode = d_inode(h_src);
5893 +       if (!isdir
5894 +           && (src_inode->i_nlink > 1
5895 +               || src_inode->i_state & I_LINKABLE)
5896 +           && plink)
5897 +               au_plink_append(inode, cpg->bdst, h_dst);
5898 +
5899 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5900 +               a->h_path.dentry = h_dst;
5901 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5902 +       }
5903 +       if (!err)
5904 +               goto out_parent; /* success */
5905 +
5906 +       /* revert */
5907 +out_rev:
5908 +       a->h_path.dentry = h_parent;
5909 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5910 +       a->h_path.dentry = h_dst;
5911 +       rerr = 0;
5912 +       if (d_is_positive(h_dst)) {
5913 +               if (!isdir) {
5914 +                       /* no delegation since it is just created */
5915 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5916 +                                           /*delegated*/NULL, /*force*/0);
5917 +               } else
5918 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5919 +       }
5920 +       au_dtime_revert(&a->dt);
5921 +       if (rerr) {
5922 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5923 +               err = -EIO;
5924 +       }
5925 +out_parent:
5926 +       dput(dst_parent);
5927 +       au_kfree_rcu(a);
5928 +out:
5929 +       return err;
5930 +}
5931 +
5932 +#if 0 /* reserved */
5933 +struct au_cpup_single_args {
5934 +       int *errp;
5935 +       struct au_cp_generic *cpg;
5936 +       struct dentry *dst_parent;
5937 +};
5938 +
5939 +static void au_call_cpup_single(void *args)
5940 +{
5941 +       struct au_cpup_single_args *a = args;
5942 +
5943 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5944 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5945 +       au_pin_hdir_release(a->cpg->pin);
5946 +}
5947 +#endif
5948 +
5949 +/*
5950 + * prevent SIGXFSZ in copy-up.
5951 + * testing CAP_MKNOD is for generic fs,
5952 + * but CAP_FSETID is for xfs only, currently.
5953 + */
5954 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5955 +{
5956 +       int do_sio;
5957 +       struct super_block *sb;
5958 +       struct inode *h_dir;
5959 +
5960 +       do_sio = 0;
5961 +       sb = au_pinned_parent(pin)->d_sb;
5962 +       if (!au_wkq_test()
5963 +           && (!au_sbi(sb)->si_plink_maint_pid
5964 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5965 +               switch (mode & S_IFMT) {
5966 +               case S_IFREG:
5967 +                       /* no condition about RLIMIT_FSIZE and the file size */
5968 +                       do_sio = 1;
5969 +                       break;
5970 +               case S_IFCHR:
5971 +               case S_IFBLK:
5972 +                       do_sio = !capable(CAP_MKNOD);
5973 +                       break;
5974 +               }
5975 +               if (!do_sio)
5976 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5977 +                                 && !capable(CAP_FSETID));
5978 +               /* this workaround may be removed in the future */
5979 +               if (!do_sio) {
5980 +                       h_dir = au_pinned_h_dir(pin);
5981 +                       do_sio = h_dir->i_mode & S_ISVTX;
5982 +               }
5983 +       }
5984 +
5985 +       return do_sio;
5986 +}
5987 +
5988 +#if 0 /* reserved */
5989 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5990 +{
5991 +       int err, wkq_err;
5992 +       struct dentry *h_dentry;
5993 +
5994 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5995 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5996 +               err = au_cpup_single(cpg, dst_parent);
5997 +       else {
5998 +               struct au_cpup_single_args args = {
5999 +                       .errp           = &err,
6000 +                       .cpg            = cpg,
6001 +                       .dst_parent     = dst_parent
6002 +               };
6003 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
6004 +               if (unlikely(wkq_err))
6005 +                       err = wkq_err;
6006 +       }
6007 +
6008 +       return err;
6009 +}
6010 +#endif
6011 +
6012 +/*
6013 + * copyup the @dentry from the first active lower branch to @bdst,
6014 + * using au_cpup_single().
6015 + */
6016 +static int au_cpup_simple(struct au_cp_generic *cpg)
6017 +{
6018 +       int err;
6019 +       unsigned int flags_orig;
6020 +       struct dentry *dentry;
6021 +
6022 +       AuDebugOn(cpg->bsrc < 0);
6023 +
6024 +       dentry = cpg->dentry;
6025 +       DiMustWriteLock(dentry);
6026 +
6027 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
6028 +       if (!err) {
6029 +               flags_orig = cpg->flags;
6030 +               au_fset_cpup(cpg->flags, RENAME);
6031 +               err = au_cpup_single(cpg, NULL);
6032 +               cpg->flags = flags_orig;
6033 +               if (!err)
6034 +                       return 0; /* success */
6035 +
6036 +               /* revert */
6037 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
6038 +               au_set_dbtop(dentry, cpg->bsrc);
6039 +       }
6040 +
6041 +       return err;
6042 +}
6043 +
6044 +struct au_cpup_simple_args {
6045 +       int *errp;
6046 +       struct au_cp_generic *cpg;
6047 +};
6048 +
6049 +static void au_call_cpup_simple(void *args)
6050 +{
6051 +       struct au_cpup_simple_args *a = args;
6052 +
6053 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6054 +       *a->errp = au_cpup_simple(a->cpg);
6055 +       au_pin_hdir_release(a->cpg->pin);
6056 +}
6057 +
6058 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
6059 +{
6060 +       int err, wkq_err;
6061 +       struct dentry *dentry, *parent;
6062 +       struct file *h_file;
6063 +       struct inode *h_dir;
6064 +
6065 +       dentry = cpg->dentry;
6066 +       h_file = NULL;
6067 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
6068 +               AuDebugOn(cpg->bsrc < 0);
6069 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
6070 +               err = PTR_ERR(h_file);
6071 +               if (IS_ERR(h_file))
6072 +                       goto out;
6073 +       }
6074 +
6075 +       parent = dget_parent(dentry);
6076 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
6077 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
6078 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6079 +               err = au_cpup_simple(cpg);
6080 +       else {
6081 +               struct au_cpup_simple_args args = {
6082 +                       .errp           = &err,
6083 +                       .cpg            = cpg
6084 +               };
6085 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
6086 +               if (unlikely(wkq_err))
6087 +                       err = wkq_err;
6088 +       }
6089 +
6090 +       dput(parent);
6091 +       if (h_file)
6092 +               au_h_open_post(dentry, cpg->bsrc, h_file);
6093 +
6094 +out:
6095 +       return err;
6096 +}
6097 +
6098 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
6099 +{
6100 +       aufs_bindex_t bsrc, bbot;
6101 +       struct dentry *dentry, *h_dentry;
6102 +
6103 +       if (cpg->bsrc < 0) {
6104 +               dentry = cpg->dentry;
6105 +               bbot = au_dbbot(dentry);
6106 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6107 +                       h_dentry = au_h_dptr(dentry, bsrc);
6108 +                       if (h_dentry) {
6109 +                               AuDebugOn(d_is_negative(h_dentry));
6110 +                               break;
6111 +                       }
6112 +               }
6113 +               AuDebugOn(bsrc > bbot);
6114 +               cpg->bsrc = bsrc;
6115 +       }
6116 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6117 +       return au_do_sio_cpup_simple(cpg);
6118 +}
6119 +
6120 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6121 +{
6122 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6123 +       return au_do_sio_cpup_simple(cpg);
6124 +}
6125 +
6126 +/* ---------------------------------------------------------------------- */
6127 +
6128 +/*
6129 + * copyup the deleted file for writing.
6130 + */
6131 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6132 +                        struct file *file)
6133 +{
6134 +       int err;
6135 +       unsigned int flags_orig;
6136 +       aufs_bindex_t bsrc_orig;
6137 +       struct au_dinfo *dinfo;
6138 +       struct {
6139 +               struct au_hdentry *hd;
6140 +               struct dentry *h_dentry;
6141 +       } hdst, hsrc;
6142 +
6143 +       dinfo = au_di(cpg->dentry);
6144 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6145 +
6146 +       bsrc_orig = cpg->bsrc;
6147 +       cpg->bsrc = dinfo->di_btop;
6148 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6149 +       hdst.h_dentry = hdst.hd->hd_dentry;
6150 +       hdst.hd->hd_dentry = wh_dentry;
6151 +       dinfo->di_btop = cpg->bdst;
6152 +
6153 +       hsrc.h_dentry = NULL;
6154 +       if (file) {
6155 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6156 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6157 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6158 +       }
6159 +       flags_orig = cpg->flags;
6160 +       cpg->flags = !AuCpup_DTIME;
6161 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6162 +       cpg->flags = flags_orig;
6163 +       if (file) {
6164 +               if (!err)
6165 +                       err = au_reopen_nondir(file);
6166 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6167 +       }
6168 +       hdst.hd->hd_dentry = hdst.h_dentry;
6169 +       dinfo->di_btop = cpg->bsrc;
6170 +       cpg->bsrc = bsrc_orig;
6171 +
6172 +       return err;
6173 +}
6174 +
6175 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6176 +{
6177 +       int err;
6178 +       aufs_bindex_t bdst;
6179 +       struct au_dtime dt;
6180 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6181 +       struct au_branch *br;
6182 +       struct path h_path;
6183 +
6184 +       dentry = cpg->dentry;
6185 +       bdst = cpg->bdst;
6186 +       br = au_sbr(dentry->d_sb, bdst);
6187 +       parent = dget_parent(dentry);
6188 +       h_parent = au_h_dptr(parent, bdst);
6189 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6190 +       err = PTR_ERR(wh_dentry);
6191 +       if (IS_ERR(wh_dentry))
6192 +               goto out;
6193 +
6194 +       h_path.dentry = h_parent;
6195 +       h_path.mnt = au_br_mnt(br);
6196 +       au_dtime_store(&dt, parent, &h_path);
6197 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6198 +       if (unlikely(err))
6199 +               goto out_wh;
6200 +
6201 +       dget(wh_dentry);
6202 +       h_path.dentry = wh_dentry;
6203 +       if (!d_is_dir(wh_dentry)) {
6204 +               /* no delegation since it is just created */
6205 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6206 +                                  /*delegated*/NULL, /*force*/0);
6207 +       } else
6208 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6209 +       if (unlikely(err)) {
6210 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6211 +                       wh_dentry, err);
6212 +               err = -EIO;
6213 +       }
6214 +       au_dtime_revert(&dt);
6215 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6216 +
6217 +out_wh:
6218 +       dput(wh_dentry);
6219 +out:
6220 +       dput(parent);
6221 +       return err;
6222 +}
6223 +
6224 +struct au_cpup_wh_args {
6225 +       int *errp;
6226 +       struct au_cp_generic *cpg;
6227 +       struct file *file;
6228 +};
6229 +
6230 +static void au_call_cpup_wh(void *args)
6231 +{
6232 +       struct au_cpup_wh_args *a = args;
6233 +
6234 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6235 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6236 +       au_pin_hdir_release(a->cpg->pin);
6237 +}
6238 +
6239 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6240 +{
6241 +       int err, wkq_err;
6242 +       aufs_bindex_t bdst;
6243 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6244 +       struct inode *dir, *h_dir, *h_tmpdir;
6245 +       struct au_wbr *wbr;
6246 +       struct au_pin wh_pin, *pin_orig;
6247 +
6248 +       dentry = cpg->dentry;
6249 +       bdst = cpg->bdst;
6250 +       parent = dget_parent(dentry);
6251 +       dir = d_inode(parent);
6252 +       h_orph = NULL;
6253 +       h_parent = NULL;
6254 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6255 +       h_tmpdir = h_dir;
6256 +       pin_orig = NULL;
6257 +       if (!h_dir->i_nlink) {
6258 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6259 +               h_orph = wbr->wbr_orph;
6260 +
6261 +               h_parent = dget(au_h_dptr(parent, bdst));
6262 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6263 +               h_tmpdir = d_inode(h_orph);
6264 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6265 +
6266 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6267 +               /* todo: au_h_open_pre()? */
6268 +
6269 +               pin_orig = cpg->pin;
6270 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6271 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6272 +               cpg->pin = &wh_pin;
6273 +       }
6274 +
6275 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6276 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6277 +               err = au_cpup_wh(cpg, file);
6278 +       else {
6279 +               struct au_cpup_wh_args args = {
6280 +                       .errp   = &err,
6281 +                       .cpg    = cpg,
6282 +                       .file   = file
6283 +               };
6284 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6285 +               if (unlikely(wkq_err))
6286 +                       err = wkq_err;
6287 +       }
6288 +
6289 +       if (h_orph) {
6290 +               inode_unlock(h_tmpdir);
6291 +               /* todo: au_h_open_post()? */
6292 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6293 +               au_set_h_dptr(parent, bdst, h_parent);
6294 +               AuDebugOn(!pin_orig);
6295 +               cpg->pin = pin_orig;
6296 +       }
6297 +       iput(h_dir);
6298 +       dput(parent);
6299 +
6300 +       return err;
6301 +}
6302 +
6303 +/* ---------------------------------------------------------------------- */
6304 +
6305 +/*
6306 + * generic routine for both of copy-up and copy-down.
6307 + */
6308 +/* cf. revalidate function in file.c */
6309 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6310 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6311 +                        struct au_pin *pin,
6312 +                        struct dentry *h_parent, void *arg),
6313 +              void *arg)
6314 +{
6315 +       int err;
6316 +       struct au_pin pin;
6317 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6318 +
6319 +       err = 0;
6320 +       parent = dget_parent(dentry);
6321 +       if (IS_ROOT(parent))
6322 +               goto out;
6323 +
6324 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6325 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6326 +
6327 +       /* do not use au_dpage */
6328 +       real_parent = parent;
6329 +       while (1) {
6330 +               dput(parent);
6331 +               parent = dget_parent(dentry);
6332 +               h_parent = au_h_dptr(parent, bdst);
6333 +               if (h_parent)
6334 +                       goto out; /* success */
6335 +
6336 +               /* find top dir which is necessary to cpup */
6337 +               do {
6338 +                       d = parent;
6339 +                       dput(parent);
6340 +                       parent = dget_parent(d);
6341 +                       di_read_lock_parent3(parent, !AuLock_IR);
6342 +                       h_parent = au_h_dptr(parent, bdst);
6343 +                       di_read_unlock(parent, !AuLock_IR);
6344 +               } while (!h_parent);
6345 +
6346 +               if (d != real_parent)
6347 +                       di_write_lock_child3(d);
6348 +
6349 +               /* somebody else might create while we were sleeping */
6350 +               h_dentry = au_h_dptr(d, bdst);
6351 +               if (!h_dentry || d_is_negative(h_dentry)) {
6352 +                       if (h_dentry)
6353 +                               au_update_dbtop(d);
6354 +
6355 +                       au_pin_set_dentry(&pin, d);
6356 +                       err = au_do_pin(&pin);
6357 +                       if (!err) {
6358 +                               err = cp(d, bdst, &pin, h_parent, arg);
6359 +                               au_unpin(&pin);
6360 +                       }
6361 +               }
6362 +
6363 +               if (d != real_parent)
6364 +                       di_write_unlock(d);
6365 +               if (unlikely(err))
6366 +                       break;
6367 +       }
6368 +
6369 +out:
6370 +       dput(parent);
6371 +       return err;
6372 +}
6373 +
6374 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6375 +                      struct au_pin *pin,
6376 +                      struct dentry *h_parent __maybe_unused,
6377 +                      void *arg __maybe_unused)
6378 +{
6379 +       struct au_cp_generic cpg = {
6380 +               .dentry = dentry,
6381 +               .bdst   = bdst,
6382 +               .bsrc   = -1,
6383 +               .len    = 0,
6384 +               .pin    = pin,
6385 +               .flags  = AuCpup_DTIME
6386 +       };
6387 +       return au_sio_cpup_simple(&cpg);
6388 +}
6389 +
6390 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6391 +{
6392 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6393 +}
6394 +
6395 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6396 +{
6397 +       int err;
6398 +       struct dentry *parent;
6399 +       struct inode *dir;
6400 +
6401 +       parent = dget_parent(dentry);
6402 +       dir = d_inode(parent);
6403 +       err = 0;
6404 +       if (au_h_iptr(dir, bdst))
6405 +               goto out;
6406 +
6407 +       di_read_unlock(parent, AuLock_IR);
6408 +       di_write_lock_parent(parent);
6409 +       /* someone else might change our inode while we were sleeping */
6410 +       if (!au_h_iptr(dir, bdst))
6411 +               err = au_cpup_dirs(dentry, bdst);
6412 +       di_downgrade_lock(parent, AuLock_IR);
6413 +
6414 +out:
6415 +       dput(parent);
6416 +       return err;
6417 +}
6418 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6419 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6420 +++ linux/fs/aufs/cpup.h        2018-08-12 23:43:05.453457863 +0200
6421 @@ -0,0 +1,100 @@
6422 +/* SPDX-License-Identifier: GPL-2.0 */
6423 +/*
6424 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6425 + *
6426 + * This program, aufs is free software; you can redistribute it and/or modify
6427 + * it under the terms of the GNU General Public License as published by
6428 + * the Free Software Foundation; either version 2 of the License, or
6429 + * (at your option) any later version.
6430 + *
6431 + * This program is distributed in the hope that it will be useful,
6432 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6433 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6434 + * GNU General Public License for more details.
6435 + *
6436 + * You should have received a copy of the GNU General Public License
6437 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6438 + */
6439 +
6440 +/*
6441 + * copy-up/down functions
6442 + */
6443 +
6444 +#ifndef __AUFS_CPUP_H__
6445 +#define __AUFS_CPUP_H__
6446 +
6447 +#ifdef __KERNEL__
6448 +
6449 +#include <linux/path.h>
6450 +
6451 +struct inode;
6452 +struct file;
6453 +struct au_pin;
6454 +
6455 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6456 +void au_cpup_attr_timesizes(struct inode *inode);
6457 +void au_cpup_attr_nlink(struct inode *inode, int force);
6458 +void au_cpup_attr_changeable(struct inode *inode);
6459 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6460 +void au_cpup_attr_all(struct inode *inode, int force);
6461 +
6462 +/* ---------------------------------------------------------------------- */
6463 +
6464 +struct au_cp_generic {
6465 +       struct dentry   *dentry;
6466 +       aufs_bindex_t   bdst, bsrc;
6467 +       loff_t          len;
6468 +       struct au_pin   *pin;
6469 +       unsigned int    flags;
6470 +};
6471 +
6472 +/* cpup flags */
6473 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6474 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6475 +                                                  for link(2) */
6476 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6477 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6478 +                                                  cpup */
6479 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6480 +                                                  existing entry */
6481 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6482 +                                                  the branch is marked as RO */
6483 +
6484 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6485 +#undef AuCpup_HOPEN
6486 +#define AuCpup_HOPEN           0
6487 +#endif
6488 +
6489 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6490 +#define au_fset_cpup(flags, name) \
6491 +       do { (flags) |= AuCpup_##name; } while (0)
6492 +#define au_fclr_cpup(flags, name) \
6493 +       do { (flags) &= ~AuCpup_##name; } while (0)
6494 +
6495 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6496 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6497 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6498 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6499 +
6500 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6501 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6502 +                        struct au_pin *pin,
6503 +                        struct dentry *h_parent, void *arg),
6504 +              void *arg);
6505 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6506 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6507 +
6508 +/* ---------------------------------------------------------------------- */
6509 +
6510 +/* keep timestamps when copyup */
6511 +struct au_dtime {
6512 +       struct dentry *dt_dentry;
6513 +       struct path dt_h_path;
6514 +       struct timespec64 dt_atime, dt_mtime;
6515 +};
6516 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6517 +                   struct path *h_path);
6518 +void au_dtime_revert(struct au_dtime *dt);
6519 +
6520 +#endif /* __KERNEL__ */
6521 +#endif /* __AUFS_CPUP_H__ */
6522 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6523 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6524 +++ linux/fs/aufs/dbgaufs.c     2018-12-27 13:19:17.708416053 +0100
6525 @@ -0,0 +1,526 @@
6526 +// SPDX-License-Identifier: GPL-2.0
6527 +/*
6528 + * Copyright (C) 2005-2018 Junjiro R. Okajima
6529 + *
6530 + * This program, aufs is free software; you can redistribute it and/or modify
6531 + * it under the terms of the GNU General Public License as published by
6532 + * the Free Software Foundation; either version 2 of the License, or
6533 + * (at your option) any later version.
6534 + *
6535 + * This program is distributed in the hope that it will be useful,
6536 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6537 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6538 + * GNU General Public License for more details.
6539 + *
6540 + * You should have received a copy of the GNU General Public License
6541 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6542 + */
6543 +
6544 +/*
6545 + * debugfs interface
6546 + */
6547 +
6548 +#include <linux/debugfs.h>
6549 +#include "aufs.h"
6550 +
6551 +#ifndef CONFIG_SYSFS
6552 +#error DEBUG_FS depends upon SYSFS
6553 +#endif
6554 +
6555 +static struct dentry *dbgaufs;
6556 +static const mode_t dbgaufs_mode = 0444;
6557 +
6558 +/* 20 is max digits length of ulong 64 */
6559 +struct dbgaufs_arg {
6560 +       int n;
6561 +       char a[20 * 4];
6562 +};
6563 +
6564 +/*
6565 + * common function for all XINO files
6566 + */
6567 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6568 +                             struct file *file)
6569 +{
6570 +       void *p;
6571 +
6572 +       p = file->private_data;
6573 +       if (p) {
6574 +               /* this is struct dbgaufs_arg */
6575 +               AuDebugOn(!au_kfree_sz_test(p));
6576 +               au_kfree_do_rcu(p);
6577 +       }
6578 +       return 0;
6579 +}
6580 +
6581 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6582 +                          int cnt)
6583 +{
6584 +       int err;
6585 +       struct kstat st;
6586 +       struct dbgaufs_arg *p;
6587 +
6588 +       err = -ENOMEM;
6589 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6590 +       if (unlikely(!p))
6591 +               goto out;
6592 +
6593 +       err = 0;
6594 +       p->n = 0;
6595 +       file->private_data = p;
6596 +       if (!xf)
6597 +               goto out;
6598 +
6599 +       err = vfsub_getattr(&xf->f_path, &st);
6600 +       if (!err) {
6601 +               if (do_fcnt)
6602 +                       p->n = snprintf
6603 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6604 +                                cnt, st.blocks, st.blksize,
6605 +                                (long long)st.size);
6606 +               else
6607 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6608 +                                       st.blocks, st.blksize,
6609 +                                       (long long)st.size);
6610 +               AuDebugOn(p->n >= sizeof(p->a));
6611 +       } else {
6612 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6613 +               err = 0;
6614 +       }
6615 +
6616 +out:
6617 +       return err;
6618 +}
6619 +
6620 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6621 +                              size_t count, loff_t *ppos)
6622 +{
6623 +       struct dbgaufs_arg *p;
6624 +
6625 +       p = file->private_data;
6626 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6627 +}
6628 +
6629 +/* ---------------------------------------------------------------------- */
6630 +
6631 +struct dbgaufs_plink_arg {
6632 +       int n;
6633 +       char a[];
6634 +};
6635 +
6636 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6637 +                                struct file *file)
6638 +{
6639 +       free_page((unsigned long)file->private_data);
6640 +       return 0;
6641 +}
6642 +
6643 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6644 +{
6645 +       int err, i, limit;
6646 +       unsigned long n, sum;
6647 +       struct dbgaufs_plink_arg *p;
6648 +       struct au_sbinfo *sbinfo;
6649 +       struct super_block *sb;
6650 +       struct hlist_bl_head *hbl;
6651 +
6652 +       err = -ENOMEM;
6653 +       p = (void *)get_zeroed_page(GFP_NOFS);
6654 +       if (unlikely(!p))
6655 +               goto out;
6656 +
6657 +       err = -EFBIG;
6658 +       sbinfo = inode->i_private;
6659 +       sb = sbinfo->si_sb;
6660 +       si_noflush_read_lock(sb);
6661 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6662 +               limit = PAGE_SIZE - sizeof(p->n);
6663 +
6664 +               /* the number of buckets */
6665 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6666 +               p->n += n;
6667 +               limit -= n;
6668 +
6669 +               sum = 0;
6670 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6671 +                    i++, hbl++) {
6672 +                       n = au_hbl_count(hbl);
6673 +                       sum += n;
6674 +
6675 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6676 +                       p->n += n;
6677 +                       limit -= n;
6678 +                       if (unlikely(limit <= 0))
6679 +                               goto out_free;
6680 +               }
6681 +               p->a[p->n - 1] = '\n';
6682 +
6683 +               /* the sum of plinks */
6684 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6685 +               p->n += n;
6686 +               limit -= n;
6687 +               if (unlikely(limit <= 0))
6688 +                       goto out_free;
6689 +       } else {
6690 +#define str "1\n0\n0\n"
6691 +               p->n = sizeof(str) - 1;
6692 +               strcpy(p->a, str);
6693 +#undef str
6694 +       }
6695 +       si_read_unlock(sb);
6696 +
6697 +       err = 0;
6698 +       file->private_data = p;
6699 +       goto out; /* success */
6700 +
6701 +out_free:
6702 +       free_page((unsigned long)p);
6703 +out:
6704 +       return err;
6705 +}
6706 +
6707 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6708 +                                 size_t count, loff_t *ppos)
6709 +{
6710 +       struct dbgaufs_plink_arg *p;
6711 +
6712 +       p = file->private_data;
6713 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6714 +}
6715 +
6716 +static const struct file_operations dbgaufs_plink_fop = {
6717 +       .owner          = THIS_MODULE,
6718 +       .open           = dbgaufs_plink_open,
6719 +       .release        = dbgaufs_plink_release,
6720 +       .read           = dbgaufs_plink_read
6721 +};
6722 +
6723 +/* ---------------------------------------------------------------------- */
6724 +
6725 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6726 +{
6727 +       int err;
6728 +       struct au_sbinfo *sbinfo;
6729 +       struct super_block *sb;
6730 +
6731 +       sbinfo = inode->i_private;
6732 +       sb = sbinfo->si_sb;
6733 +       si_noflush_read_lock(sb);
6734 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6735 +       si_read_unlock(sb);
6736 +       return err;
6737 +}
6738 +
6739 +static const struct file_operations dbgaufs_xib_fop = {
6740 +       .owner          = THIS_MODULE,
6741 +       .open           = dbgaufs_xib_open,
6742 +       .release        = dbgaufs_xi_release,
6743 +       .read           = dbgaufs_xi_read
6744 +};
6745 +
6746 +/* ---------------------------------------------------------------------- */
6747 +
6748 +#define DbgaufsXi_PREFIX "xi"
6749 +
6750 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6751 +{
6752 +       int err, idx;
6753 +       long l;
6754 +       aufs_bindex_t bindex;
6755 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6756 +       struct au_sbinfo *sbinfo;
6757 +       struct super_block *sb;
6758 +       struct au_xino *xi;
6759 +       struct file *xf;
6760 +       struct qstr *name;
6761 +       struct au_branch *br;
6762 +
6763 +       err = -ENOENT;
6764 +       name = &file->f_path.dentry->d_name;
6765 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6766 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6767 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6768 +               goto out;
6769 +
6770 +       AuDebugOn(name->len >= sizeof(a));
6771 +       memcpy(a, name->name, name->len);
6772 +       a[name->len] = '\0';
6773 +       p = strchr(a, '-');
6774 +       if (p)
6775 +               *p = '\0';
6776 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6777 +       if (unlikely(err))
6778 +               goto out;
6779 +       bindex = l;
6780 +       idx = 0;
6781 +       if (p) {
6782 +               err = kstrtol(p + 1, 10, &l);
6783 +               if (unlikely(err))
6784 +                       goto out;
6785 +               idx = l;
6786 +       }
6787 +
6788 +       err = -ENOENT;
6789 +       sbinfo = inode->i_private;
6790 +       sb = sbinfo->si_sb;
6791 +       si_noflush_read_lock(sb);
6792 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6793 +               goto out_si;
6794 +       br = au_sbr(sb, bindex);
6795 +       xi = br->br_xino;
6796 +       if (unlikely(idx >= xi->xi_nfile))
6797 +               goto out_si;
6798 +       xf = au_xino_file(xi, idx);
6799 +       if (xf)
6800 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6801 +                                     au_xino_count(br));
6802 +
6803 +out_si:
6804 +       si_read_unlock(sb);
6805 +out:
6806 +       AuTraceErr(err);
6807 +       return err;
6808 +}
6809 +
6810 +static const struct file_operations dbgaufs_xino_fop = {
6811 +       .owner          = THIS_MODULE,
6812 +       .open           = dbgaufs_xino_open,
6813 +       .release        = dbgaufs_xi_release,
6814 +       .read           = dbgaufs_xi_read
6815 +};
6816 +
6817 +void dbgaufs_xino_del(struct au_branch *br)
6818 +{
6819 +       struct dentry *dbgaufs;
6820 +
6821 +       dbgaufs = br->br_dbgaufs;
6822 +       if (!dbgaufs)
6823 +               return;
6824 +
6825 +       br->br_dbgaufs = NULL;
6826 +       /* debugfs acquires the parent i_mutex */
6827 +       lockdep_off();
6828 +       debugfs_remove(dbgaufs);
6829 +       lockdep_on();
6830 +}
6831 +
6832 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6833 +{
6834 +       aufs_bindex_t bbot;
6835 +       struct au_branch *br;
6836 +
6837 +       if (!au_sbi(sb)->si_dbgaufs)
6838 +               return;
6839 +
6840 +       bbot = au_sbbot(sb);
6841 +       for (; bindex <= bbot; bindex++) {
6842 +               br = au_sbr(sb, bindex);
6843 +               dbgaufs_xino_del(br);
6844 +       }
6845 +}
6846 +
6847 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6848 +                             unsigned int idx, struct dentry *parent,
6849 +                             struct au_sbinfo *sbinfo)
6850 +{
6851 +       struct au_branch *br;
6852 +       struct dentry *d;
6853 +       /* "xi" bindex(5) "-" idx(2) NULL */
6854 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6855 +
6856 +       if (!idx)
6857 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6858 +       else
6859 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6860 +                        bindex, idx);
6861 +       br = au_sbr(sb, bindex);
6862 +       if (br->br_dbgaufs) {
6863 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6864 +
6865 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6866 +                       /* debugfs acquires the parent i_mutex */
6867 +                       lockdep_off();
6868 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6869 +                                          name);
6870 +                       lockdep_on();
6871 +                       if (unlikely(!d))
6872 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6873 +                                       parent, name);
6874 +               }
6875 +       } else {
6876 +               lockdep_off();
6877 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6878 +                                                    sbinfo, &dbgaufs_xino_fop);
6879 +               lockdep_on();
6880 +               if (unlikely(!br->br_dbgaufs))
6881 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6882 +                               parent, name);
6883 +       }
6884 +}
6885 +
6886 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6887 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6888 +{
6889 +       struct au_branch *br;
6890 +       struct au_xino *xi;
6891 +       unsigned int u;
6892 +
6893 +       br = au_sbr(sb, bindex);
6894 +       xi = br->br_xino;
6895 +       for (u = 0; u < xi->xi_nfile; u++)
6896 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6897 +}
6898 +
6899 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6900 +{
6901 +       struct au_sbinfo *sbinfo;
6902 +       struct dentry *parent;
6903 +       aufs_bindex_t bbot;
6904 +
6905 +       if (!au_opt_test(au_mntflags(sb), XINO))
6906 +               return;
6907 +
6908 +       sbinfo = au_sbi(sb);
6909 +       parent = sbinfo->si_dbgaufs;
6910 +       if (!parent)
6911 +               return;
6912 +
6913 +       bbot = au_sbbot(sb);
6914 +       if (topdown)
6915 +               for (; bindex <= bbot; bindex++)
6916 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6917 +       else
6918 +               for (; bbot >= bindex; bbot--)
6919 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6920 +}
6921 +
6922 +/* ---------------------------------------------------------------------- */
6923 +
6924 +#ifdef CONFIG_AUFS_EXPORT
6925 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6926 +{
6927 +       int err;
6928 +       struct au_sbinfo *sbinfo;
6929 +       struct super_block *sb;
6930 +
6931 +       sbinfo = inode->i_private;
6932 +       sb = sbinfo->si_sb;
6933 +       si_noflush_read_lock(sb);
6934 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6935 +       si_read_unlock(sb);
6936 +       return err;
6937 +}
6938 +
6939 +static const struct file_operations dbgaufs_xigen_fop = {
6940 +       .owner          = THIS_MODULE,
6941 +       .open           = dbgaufs_xigen_open,
6942 +       .release        = dbgaufs_xi_release,
6943 +       .read           = dbgaufs_xi_read
6944 +};
6945 +
6946 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6947 +{
6948 +       int err;
6949 +
6950 +       /*
6951 +        * This function is a dynamic '__init' function actually,
6952 +        * so the tiny check for si_rwsem is unnecessary.
6953 +        */
6954 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6955 +
6956 +       err = -EIO;
6957 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6958 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6959 +                &dbgaufs_xigen_fop);
6960 +       if (sbinfo->si_dbgaufs_xigen)
6961 +               err = 0;
6962 +
6963 +       return err;
6964 +}
6965 +#else
6966 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6967 +{
6968 +       return 0;
6969 +}
6970 +#endif /* CONFIG_AUFS_EXPORT */
6971 +
6972 +/* ---------------------------------------------------------------------- */
6973 +
6974 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6975 +{
6976 +       /*
6977 +        * This function is a dynamic '__fin' function actually,
6978 +        * so the tiny check for si_rwsem is unnecessary.
6979 +        */
6980 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6981 +
6982 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6983 +       sbinfo->si_dbgaufs = NULL;
6984 +}
6985 +
6986 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6987 +{
6988 +       int err;
6989 +       char name[SysaufsSiNameLen];
6990 +
6991 +       /*
6992 +        * This function is a dynamic '__init' function actually,
6993 +        * so the tiny check for si_rwsem is unnecessary.
6994 +        */
6995 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6996 +
6997 +       err = -ENOENT;
6998 +       if (!dbgaufs) {
6999 +               AuErr1("/debug/aufs is uninitialized\n");
7000 +               goto out;
7001 +       }
7002 +
7003 +       err = -EIO;
7004 +       sysaufs_name(sbinfo, name);
7005 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
7006 +       if (unlikely(!sbinfo->si_dbgaufs))
7007 +               goto out;
7008 +
7009 +       /* regardless plink/noplink option */
7010 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
7011 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7012 +                &dbgaufs_plink_fop);
7013 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
7014 +               goto out_dir;
7015 +
7016 +       /* regardless xino/noxino option */
7017 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
7018 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
7019 +                &dbgaufs_xib_fop);
7020 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
7021 +               goto out_dir;
7022 +
7023 +       err = dbgaufs_xigen_init(sbinfo);
7024 +       if (!err)
7025 +               goto out; /* success */
7026 +
7027 +out_dir:
7028 +       dbgaufs_si_fin(sbinfo);
7029 +out:
7030 +       if (unlikely(err))
7031 +               pr_err("debugfs/aufs failed\n");
7032 +       return err;
7033 +}
7034 +
7035 +/* ---------------------------------------------------------------------- */
7036 +
7037 +void dbgaufs_fin(void)
7038 +{
7039 +       debugfs_remove(dbgaufs);
7040 +}
7041 +
7042 +int __init dbgaufs_init(void)
7043 +{
7044 +       int err;
7045 +
7046 +       err = -EIO;
7047 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
7048 +       if (dbgaufs)
7049 +               err = 0;
7050 +       return err;
7051 +}
7052 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
7053 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
7054 +++ linux/fs/aufs/dbgaufs.h     2018-08-12 23:43:05.453457863 +0200
7055 @@ -0,0 +1,53 @@
7056 +/* SPDX-License-Identifier: GPL-2.0 */
7057 +/*
7058 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7059 + *
7060 + * This program, aufs is free software; you can redistribute it and/or modify
7061 + * it under the terms of the GNU General Public License as published by
7062 + * the Free Software Foundation; either version 2 of the License, or
7063 + * (at your option) any later version.
7064 + *
7065 + * This program is distributed in the hope that it will be useful,
7066 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7067 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7068 + * GNU General Public License for more details.
7069 + *
7070 + * You should have received a copy of the GNU General Public License
7071 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7072 + */
7073 +
7074 +/*
7075 + * debugfs interface
7076 + */
7077 +
7078 +#ifndef __DBGAUFS_H__
7079 +#define __DBGAUFS_H__
7080 +
7081 +#ifdef __KERNEL__
7082 +
7083 +struct super_block;
7084 +struct au_sbinfo;
7085 +struct au_branch;
7086 +
7087 +#ifdef CONFIG_DEBUG_FS
7088 +/* dbgaufs.c */
7089 +void dbgaufs_xino_del(struct au_branch *br);
7090 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
7091 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
7092 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
7093 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
7094 +void dbgaufs_fin(void);
7095 +int __init dbgaufs_init(void);
7096 +#else
7097 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
7098 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
7099 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
7100 +          int topdown)
7101 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
7102 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7103 +AuStubVoid(dbgaufs_fin, void)
7104 +AuStubInt0(__init dbgaufs_init, void)
7105 +#endif /* CONFIG_DEBUG_FS */
7106 +
7107 +#endif /* __KERNEL__ */
7108 +#endif /* __DBGAUFS_H__ */
7109 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7110 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7111 +++ linux/fs/aufs/dcsub.c       2018-12-27 13:19:17.708416053 +0100
7112 @@ -0,0 +1,225 @@
7113 +// SPDX-License-Identifier: GPL-2.0
7114 +/*
7115 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7116 + *
7117 + * This program, aufs is free software; you can redistribute it and/or modify
7118 + * it under the terms of the GNU General Public License as published by
7119 + * the Free Software Foundation; either version 2 of the License, or
7120 + * (at your option) any later version.
7121 + *
7122 + * This program is distributed in the hope that it will be useful,
7123 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7124 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7125 + * GNU General Public License for more details.
7126 + *
7127 + * You should have received a copy of the GNU General Public License
7128 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7129 + */
7130 +
7131 +/*
7132 + * sub-routines for dentry cache
7133 + */
7134 +
7135 +#include "aufs.h"
7136 +
7137 +static void au_dpage_free(struct au_dpage *dpage)
7138 +{
7139 +       int i;
7140 +       struct dentry **p;
7141 +
7142 +       p = dpage->dentries;
7143 +       for (i = 0; i < dpage->ndentry; i++)
7144 +               dput(*p++);
7145 +       free_page((unsigned long)dpage->dentries);
7146 +}
7147 +
7148 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7149 +{
7150 +       int err;
7151 +       void *p;
7152 +
7153 +       err = -ENOMEM;
7154 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7155 +       if (unlikely(!dpages->dpages))
7156 +               goto out;
7157 +
7158 +       p = (void *)__get_free_page(gfp);
7159 +       if (unlikely(!p))
7160 +               goto out_dpages;
7161 +
7162 +       dpages->dpages[0].ndentry = 0;
7163 +       dpages->dpages[0].dentries = p;
7164 +       dpages->ndpage = 1;
7165 +       return 0; /* success */
7166 +
7167 +out_dpages:
7168 +       au_kfree_try_rcu(dpages->dpages);
7169 +out:
7170 +       return err;
7171 +}
7172 +
7173 +void au_dpages_free(struct au_dcsub_pages *dpages)
7174 +{
7175 +       int i;
7176 +       struct au_dpage *p;
7177 +
7178 +       p = dpages->dpages;
7179 +       for (i = 0; i < dpages->ndpage; i++)
7180 +               au_dpage_free(p++);
7181 +       au_kfree_try_rcu(dpages->dpages);
7182 +}
7183 +
7184 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7185 +                           struct dentry *dentry, gfp_t gfp)
7186 +{
7187 +       int err, sz;
7188 +       struct au_dpage *dpage;
7189 +       void *p;
7190 +
7191 +       dpage = dpages->dpages + dpages->ndpage - 1;
7192 +       sz = PAGE_SIZE / sizeof(dentry);
7193 +       if (unlikely(dpage->ndentry >= sz)) {
7194 +               AuLabel(new dpage);
7195 +               err = -ENOMEM;
7196 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7197 +               p = au_kzrealloc(dpages->dpages, sz,
7198 +                                sz + sizeof(*dpages->dpages), gfp,
7199 +                                /*may_shrink*/0);
7200 +               if (unlikely(!p))
7201 +                       goto out;
7202 +
7203 +               dpages->dpages = p;
7204 +               dpage = dpages->dpages + dpages->ndpage;
7205 +               p = (void *)__get_free_page(gfp);
7206 +               if (unlikely(!p))
7207 +                       goto out;
7208 +
7209 +               dpage->ndentry = 0;
7210 +               dpage->dentries = p;
7211 +               dpages->ndpage++;
7212 +       }
7213 +
7214 +       AuDebugOn(au_dcount(dentry) <= 0);
7215 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7216 +       return 0; /* success */
7217 +
7218 +out:
7219 +       return err;
7220 +}
7221 +
7222 +/* todo: BAD approach */
7223 +/* copied from linux/fs/dcache.c */
7224 +enum d_walk_ret {
7225 +       D_WALK_CONTINUE,
7226 +       D_WALK_QUIT,
7227 +       D_WALK_NORETRY,
7228 +       D_WALK_SKIP,
7229 +};
7230 +
7231 +extern void d_walk(struct dentry *parent, void *data,
7232 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7233 +
7234 +struct ac_dpages_arg {
7235 +       int err;
7236 +       struct au_dcsub_pages *dpages;
7237 +       struct super_block *sb;
7238 +       au_dpages_test test;
7239 +       void *arg;
7240 +};
7241 +
7242 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7243 +{
7244 +       enum d_walk_ret ret;
7245 +       struct ac_dpages_arg *arg = _arg;
7246 +
7247 +       ret = D_WALK_CONTINUE;
7248 +       if (dentry->d_sb == arg->sb
7249 +           && !IS_ROOT(dentry)
7250 +           && au_dcount(dentry) > 0
7251 +           && au_di(dentry)
7252 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7253 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7254 +               if (unlikely(arg->err))
7255 +                       ret = D_WALK_QUIT;
7256 +       }
7257 +
7258 +       return ret;
7259 +}
7260 +
7261 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7262 +                  au_dpages_test test, void *arg)
7263 +{
7264 +       struct ac_dpages_arg args = {
7265 +               .err    = 0,
7266 +               .dpages = dpages,
7267 +               .sb     = root->d_sb,
7268 +               .test   = test,
7269 +               .arg    = arg
7270 +       };
7271 +
7272 +       d_walk(root, &args, au_call_dpages_append);
7273 +
7274 +       return args.err;
7275 +}
7276 +
7277 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7278 +                      int do_include, au_dpages_test test, void *arg)
7279 +{
7280 +       int err;
7281 +
7282 +       err = 0;
7283 +       write_seqlock(&rename_lock);
7284 +       spin_lock(&dentry->d_lock);
7285 +       if (do_include
7286 +           && au_dcount(dentry) > 0
7287 +           && (!test || test(dentry, arg)))
7288 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7289 +       spin_unlock(&dentry->d_lock);
7290 +       if (unlikely(err))
7291 +               goto out;
7292 +
7293 +       /*
7294 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7295 +        * mount
7296 +        */
7297 +       while (!IS_ROOT(dentry)) {
7298 +               dentry = dentry->d_parent; /* rename_lock is locked */
7299 +               spin_lock(&dentry->d_lock);
7300 +               if (au_dcount(dentry) > 0
7301 +                   && (!test || test(dentry, arg)))
7302 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7303 +               spin_unlock(&dentry->d_lock);
7304 +               if (unlikely(err))
7305 +                       break;
7306 +       }
7307 +
7308 +out:
7309 +       write_sequnlock(&rename_lock);
7310 +       return err;
7311 +}
7312 +
7313 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7314 +{
7315 +       return au_di(dentry) && dentry->d_sb == arg;
7316 +}
7317 +
7318 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7319 +                           struct dentry *dentry, int do_include)
7320 +{
7321 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7322 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7323 +}
7324 +
7325 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7326 +{
7327 +       struct path path[2] = {
7328 +               {
7329 +                       .dentry = d1
7330 +               },
7331 +               {
7332 +                       .dentry = d2
7333 +               }
7334 +       };
7335 +
7336 +       return path_is_under(path + 0, path + 1);
7337 +}
7338 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7339 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7340 +++ linux/fs/aufs/dcsub.h       2018-08-12 23:43:05.453457863 +0200
7341 @@ -0,0 +1,137 @@
7342 +/* SPDX-License-Identifier: GPL-2.0 */
7343 +/*
7344 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7345 + *
7346 + * This program, aufs is free software; you can redistribute it and/or modify
7347 + * it under the terms of the GNU General Public License as published by
7348 + * the Free Software Foundation; either version 2 of the License, or
7349 + * (at your option) any later version.
7350 + *
7351 + * This program is distributed in the hope that it will be useful,
7352 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7353 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7354 + * GNU General Public License for more details.
7355 + *
7356 + * You should have received a copy of the GNU General Public License
7357 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7358 + */
7359 +
7360 +/*
7361 + * sub-routines for dentry cache
7362 + */
7363 +
7364 +#ifndef __AUFS_DCSUB_H__
7365 +#define __AUFS_DCSUB_H__
7366 +
7367 +#ifdef __KERNEL__
7368 +
7369 +#include <linux/dcache.h>
7370 +#include <linux/fs.h>
7371 +
7372 +struct au_dpage {
7373 +       int ndentry;
7374 +       struct dentry **dentries;
7375 +};
7376 +
7377 +struct au_dcsub_pages {
7378 +       int ndpage;
7379 +       struct au_dpage *dpages;
7380 +};
7381 +
7382 +/* ---------------------------------------------------------------------- */
7383 +
7384 +/* dcsub.c */
7385 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7386 +void au_dpages_free(struct au_dcsub_pages *dpages);
7387 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7388 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7389 +                  au_dpages_test test, void *arg);
7390 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7391 +                      int do_include, au_dpages_test test, void *arg);
7392 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7393 +                           struct dentry *dentry, int do_include);
7394 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7395 +
7396 +/* ---------------------------------------------------------------------- */
7397 +
7398 +/*
7399 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7400 + * include/linux/dcache.h. Try them (in the future).
7401 + */
7402 +
7403 +static inline int au_d_hashed_positive(struct dentry *d)
7404 +{
7405 +       int err;
7406 +       struct inode *inode = d_inode(d);
7407 +
7408 +       err = 0;
7409 +       if (unlikely(d_unhashed(d)
7410 +                    || d_is_negative(d)
7411 +                    || !inode->i_nlink))
7412 +               err = -ENOENT;
7413 +       return err;
7414 +}
7415 +
7416 +static inline int au_d_linkable(struct dentry *d)
7417 +{
7418 +       int err;
7419 +       struct inode *inode = d_inode(d);
7420 +
7421 +       err = au_d_hashed_positive(d);
7422 +       if (err
7423 +           && d_is_positive(d)
7424 +           && (inode->i_state & I_LINKABLE))
7425 +               err = 0;
7426 +       return err;
7427 +}
7428 +
7429 +static inline int au_d_alive(struct dentry *d)
7430 +{
7431 +       int err;
7432 +       struct inode *inode;
7433 +
7434 +       err = 0;
7435 +       if (!IS_ROOT(d))
7436 +               err = au_d_hashed_positive(d);
7437 +       else {
7438 +               inode = d_inode(d);
7439 +               if (unlikely(d_unlinked(d)
7440 +                            || d_is_negative(d)
7441 +                            || !inode->i_nlink))
7442 +                       err = -ENOENT;
7443 +       }
7444 +       return err;
7445 +}
7446 +
7447 +static inline int au_alive_dir(struct dentry *d)
7448 +{
7449 +       int err;
7450 +
7451 +       err = au_d_alive(d);
7452 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7453 +               err = -ENOENT;
7454 +       return err;
7455 +}
7456 +
7457 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7458 +{
7459 +       return a->len == b->len
7460 +               && !memcmp(a->name, b->name, a->len);
7461 +}
7462 +
7463 +/*
7464 + * by the commit
7465 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7466 + *                     taking d_lock
7467 + * the type of d_lockref.count became int, but the inlined function d_count()
7468 + * still returns unsigned int.
7469 + * I don't know why. Maybe it is for every d_count() users?
7470 + * Anyway au_dcount() lives on.
7471 + */
7472 +static inline int au_dcount(struct dentry *d)
7473 +{
7474 +       return (int)d_count(d);
7475 +}
7476 +
7477 +#endif /* __KERNEL__ */
7478 +#endif /* __AUFS_DCSUB_H__ */
7479 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7480 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7481 +++ linux/fs/aufs/debug.c       2018-12-27 13:19:17.708416053 +0100
7482 @@ -0,0 +1,440 @@
7483 +// SPDX-License-Identifier: GPL-2.0
7484 +/*
7485 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7486 + *
7487 + * This program, aufs is free software; you can redistribute it and/or modify
7488 + * it under the terms of the GNU General Public License as published by
7489 + * the Free Software Foundation; either version 2 of the License, or
7490 + * (at your option) any later version.
7491 + *
7492 + * This program is distributed in the hope that it will be useful,
7493 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7494 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7495 + * GNU General Public License for more details.
7496 + *
7497 + * You should have received a copy of the GNU General Public License
7498 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7499 + */
7500 +
7501 +/*
7502 + * debug print functions
7503 + */
7504 +
7505 +#include "aufs.h"
7506 +
7507 +/* Returns 0, or -errno.  arg is in kp->arg. */
7508 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7509 +{
7510 +       int err, n;
7511 +
7512 +       err = kstrtoint(val, 0, &n);
7513 +       if (!err) {
7514 +               if (n > 0)
7515 +                       au_debug_on();
7516 +               else
7517 +                       au_debug_off();
7518 +       }
7519 +       return err;
7520 +}
7521 +
7522 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7523 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7524 +{
7525 +       atomic_t *a;
7526 +
7527 +       a = kp->arg;
7528 +       return sprintf(buffer, "%d", atomic_read(a));
7529 +}
7530 +
7531 +static struct kernel_param_ops param_ops_atomic_t = {
7532 +       .set = param_atomic_t_set,
7533 +       .get = param_atomic_t_get
7534 +       /* void (*free)(void *arg) */
7535 +};
7536 +
7537 +atomic_t aufs_debug = ATOMIC_INIT(0);
7538 +MODULE_PARM_DESC(debug, "debug print");
7539 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7540 +
7541 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7542 +char *au_plevel = KERN_DEBUG;
7543 +#define dpri(fmt, ...) do {                                    \
7544 +       if ((au_plevel                                          \
7545 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7546 +           || au_debug_test())                                 \
7547 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7548 +} while (0)
7549 +
7550 +/* ---------------------------------------------------------------------- */
7551 +
7552 +void au_dpri_whlist(struct au_nhash *whlist)
7553 +{
7554 +       unsigned long ul, n;
7555 +       struct hlist_head *head;
7556 +       struct au_vdir_wh *pos;
7557 +
7558 +       n = whlist->nh_num;
7559 +       head = whlist->nh_head;
7560 +       for (ul = 0; ul < n; ul++) {
7561 +               hlist_for_each_entry(pos, head, wh_hash)
7562 +                       dpri("b%d, %.*s, %d\n",
7563 +                            pos->wh_bindex,
7564 +                            pos->wh_str.len, pos->wh_str.name,
7565 +                            pos->wh_str.len);
7566 +               head++;
7567 +       }
7568 +}
7569 +
7570 +void au_dpri_vdir(struct au_vdir *vdir)
7571 +{
7572 +       unsigned long ul;
7573 +       union au_vdir_deblk_p p;
7574 +       unsigned char *o;
7575 +
7576 +       if (!vdir || IS_ERR(vdir)) {
7577 +               dpri("err %ld\n", PTR_ERR(vdir));
7578 +               return;
7579 +       }
7580 +
7581 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7582 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7583 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7584 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7585 +               p.deblk = vdir->vd_deblk[ul];
7586 +               o = p.deblk;
7587 +               dpri("[%lu]: %p\n", ul, o);
7588 +       }
7589 +}
7590 +
7591 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7592 +                       struct dentry *wh)
7593 +{
7594 +       char *n = NULL;
7595 +       int l = 0;
7596 +
7597 +       if (!inode || IS_ERR(inode)) {
7598 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7599 +               return -1;
7600 +       }
7601 +
7602 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7603 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7604 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7605 +       if (wh) {
7606 +               n = (void *)wh->d_name.name;
7607 +               l = wh->d_name.len;
7608 +       }
7609 +
7610 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7611 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7612 +            bindex, inode,
7613 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7614 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7615 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7616 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7617 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7618 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7619 +            inode->i_generation,
7620 +            l ? ", wh " : "", l, n);
7621 +       return 0;
7622 +}
7623 +
7624 +void au_dpri_inode(struct inode *inode)
7625 +{
7626 +       struct au_iinfo *iinfo;
7627 +       struct au_hinode *hi;
7628 +       aufs_bindex_t bindex;
7629 +       int err, hn;
7630 +
7631 +       err = do_pri_inode(-1, inode, -1, NULL);
7632 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7633 +               return;
7634 +
7635 +       iinfo = au_ii(inode);
7636 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7637 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7638 +       if (iinfo->ii_btop < 0)
7639 +               return;
7640 +       hn = 0;
7641 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7642 +               hi = au_hinode(iinfo, bindex);
7643 +               hn = !!au_hn(hi);
7644 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7645 +       }
7646 +}
7647 +
7648 +void au_dpri_dalias(struct inode *inode)
7649 +{
7650 +       struct dentry *d;
7651 +
7652 +       spin_lock(&inode->i_lock);
7653 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7654 +               au_dpri_dentry(d);
7655 +       spin_unlock(&inode->i_lock);
7656 +}
7657 +
7658 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7659 +{
7660 +       struct dentry *wh = NULL;
7661 +       int hn;
7662 +       struct inode *inode;
7663 +       struct au_iinfo *iinfo;
7664 +       struct au_hinode *hi;
7665 +
7666 +       if (!dentry || IS_ERR(dentry)) {
7667 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7668 +               return -1;
7669 +       }
7670 +       /* do not call dget_parent() here */
7671 +       /* note: access d_xxx without d_lock */
7672 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7673 +            bindex, dentry, dentry,
7674 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7675 +            au_dcount(dentry), dentry->d_flags,
7676 +            d_unhashed(dentry) ? "un" : "");
7677 +       hn = -1;
7678 +       inode = NULL;
7679 +       if (d_is_positive(dentry))
7680 +               inode = d_inode(dentry);
7681 +       if (inode
7682 +           && au_test_aufs(dentry->d_sb)
7683 +           && bindex >= 0
7684 +           && !au_is_bad_inode(inode)) {
7685 +               iinfo = au_ii(inode);
7686 +               hi = au_hinode(iinfo, bindex);
7687 +               hn = !!au_hn(hi);
7688 +               wh = hi->hi_whdentry;
7689 +       }
7690 +       do_pri_inode(bindex, inode, hn, wh);
7691 +       return 0;
7692 +}
7693 +
7694 +void au_dpri_dentry(struct dentry *dentry)
7695 +{
7696 +       struct au_dinfo *dinfo;
7697 +       aufs_bindex_t bindex;
7698 +       int err;
7699 +
7700 +       err = do_pri_dentry(-1, dentry);
7701 +       if (err || !au_test_aufs(dentry->d_sb))
7702 +               return;
7703 +
7704 +       dinfo = au_di(dentry);
7705 +       if (!dinfo)
7706 +               return;
7707 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7708 +            dinfo->di_btop, dinfo->di_bbot,
7709 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7710 +            dinfo->di_tmpfile);
7711 +       if (dinfo->di_btop < 0)
7712 +               return;
7713 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7714 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7715 +}
7716 +
7717 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7718 +{
7719 +       char a[32];
7720 +
7721 +       if (!file || IS_ERR(file)) {
7722 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7723 +               return -1;
7724 +       }
7725 +       a[0] = 0;
7726 +       if (bindex < 0
7727 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7728 +           && au_test_aufs(file->f_path.dentry->d_sb)
7729 +           && au_fi(file))
7730 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7731 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7732 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7733 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7734 +            file->f_version, file->f_pos, a);
7735 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7736 +               do_pri_dentry(bindex, file->f_path.dentry);
7737 +       return 0;
7738 +}
7739 +
7740 +void au_dpri_file(struct file *file)
7741 +{
7742 +       struct au_finfo *finfo;
7743 +       struct au_fidir *fidir;
7744 +       struct au_hfile *hfile;
7745 +       aufs_bindex_t bindex;
7746 +       int err;
7747 +
7748 +       err = do_pri_file(-1, file);
7749 +       if (err
7750 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7751 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7752 +               return;
7753 +
7754 +       finfo = au_fi(file);
7755 +       if (!finfo)
7756 +               return;
7757 +       if (finfo->fi_btop < 0)
7758 +               return;
7759 +       fidir = finfo->fi_hdir;
7760 +       if (!fidir)
7761 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7762 +       else
7763 +               for (bindex = finfo->fi_btop;
7764 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7765 +                    bindex++) {
7766 +                       hfile = fidir->fd_hfile + bindex;
7767 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7768 +               }
7769 +}
7770 +
7771 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7772 +{
7773 +       struct vfsmount *mnt;
7774 +       struct super_block *sb;
7775 +
7776 +       if (!br || IS_ERR(br))
7777 +               goto out;
7778 +       mnt = au_br_mnt(br);
7779 +       if (!mnt || IS_ERR(mnt))
7780 +               goto out;
7781 +       sb = mnt->mnt_sb;
7782 +       if (!sb || IS_ERR(sb))
7783 +               goto out;
7784 +
7785 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7786 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7787 +            "xino %d\n",
7788 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7789 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7790 +            sb->s_flags, sb->s_count,
7791 +            atomic_read(&sb->s_active),
7792 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7793 +       return 0;
7794 +
7795 +out:
7796 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7797 +       return -1;
7798 +}
7799 +
7800 +void au_dpri_sb(struct super_block *sb)
7801 +{
7802 +       struct au_sbinfo *sbinfo;
7803 +       aufs_bindex_t bindex;
7804 +       int err;
7805 +       /* to reduce stack size */
7806 +       struct {
7807 +               struct vfsmount mnt;
7808 +               struct au_branch fake;
7809 +       } *a;
7810 +
7811 +       /* this function can be called from magic sysrq */
7812 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7813 +       if (unlikely(!a)) {
7814 +               dpri("no memory\n");
7815 +               return;
7816 +       }
7817 +
7818 +       a->mnt.mnt_sb = sb;
7819 +       a->fake.br_path.mnt = &a->mnt;
7820 +       err = do_pri_br(-1, &a->fake);
7821 +       au_kfree_rcu(a);
7822 +       dpri("dev 0x%x\n", sb->s_dev);
7823 +       if (err || !au_test_aufs(sb))
7824 +               return;
7825 +
7826 +       sbinfo = au_sbi(sb);
7827 +       if (!sbinfo)
7828 +               return;
7829 +       dpri("nw %d, gen %u, kobj %d\n",
7830 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7831 +            kref_read(&sbinfo->si_kobj.kref));
7832 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7833 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7834 +}
7835 +
7836 +/* ---------------------------------------------------------------------- */
7837 +
7838 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7839 +{
7840 +       struct inode *h_inode, *inode = d_inode(dentry);
7841 +       struct dentry *h_dentry;
7842 +       aufs_bindex_t bindex, bbot, bi;
7843 +
7844 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7845 +               return;
7846 +
7847 +       bbot = au_dbbot(dentry);
7848 +       bi = au_ibbot(inode);
7849 +       if (bi < bbot)
7850 +               bbot = bi;
7851 +       bindex = au_dbtop(dentry);
7852 +       bi = au_ibtop(inode);
7853 +       if (bi > bindex)
7854 +               bindex = bi;
7855 +
7856 +       for (; bindex <= bbot; bindex++) {
7857 +               h_dentry = au_h_dptr(dentry, bindex);
7858 +               if (!h_dentry)
7859 +                       continue;
7860 +               h_inode = au_h_iptr(inode, bindex);
7861 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7862 +                       au_debug_on();
7863 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7864 +                       AuDbgDentry(dentry);
7865 +                       AuDbgInode(inode);
7866 +                       au_debug_off();
7867 +                       BUG();
7868 +               }
7869 +       }
7870 +}
7871 +
7872 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7873 +{
7874 +       int err, i, j;
7875 +       struct au_dcsub_pages dpages;
7876 +       struct au_dpage *dpage;
7877 +       struct dentry **dentries;
7878 +
7879 +       err = au_dpages_init(&dpages, GFP_NOFS);
7880 +       AuDebugOn(err);
7881 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7882 +       AuDebugOn(err);
7883 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7884 +               dpage = dpages.dpages + i;
7885 +               dentries = dpage->dentries;
7886 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7887 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7888 +       }
7889 +       au_dpages_free(&dpages);
7890 +}
7891 +
7892 +void au_dbg_verify_kthread(void)
7893 +{
7894 +       if (au_wkq_test()) {
7895 +               au_dbg_blocked();
7896 +               /*
7897 +                * It may be recursive, but udba=notify between two aufs mounts,
7898 +                * where a single ro branch is shared, is not a problem.
7899 +                */
7900 +               /* WARN_ON(1); */
7901 +       }
7902 +}
7903 +
7904 +/* ---------------------------------------------------------------------- */
7905 +
7906 +int __init au_debug_init(void)
7907 +{
7908 +       aufs_bindex_t bindex;
7909 +       struct au_vdir_destr destr;
7910 +
7911 +       bindex = -1;
7912 +       AuDebugOn(bindex >= 0);
7913 +
7914 +       destr.len = -1;
7915 +       AuDebugOn(destr.len < NAME_MAX);
7916 +
7917 +#ifdef CONFIG_4KSTACKS
7918 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7919 +#endif
7920 +
7921 +       return 0;
7922 +}
7923 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7924 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7925 +++ linux/fs/aufs/debug.h       2018-08-12 23:43:05.453457863 +0200
7926 @@ -0,0 +1,226 @@
7927 +/* SPDX-License-Identifier: GPL-2.0 */
7928 +/*
7929 + * Copyright (C) 2005-2018 Junjiro R. Okajima
7930 + *
7931 + * This program, aufs is free software; you can redistribute it and/or modify
7932 + * it under the terms of the GNU General Public License as published by
7933 + * the Free Software Foundation; either version 2 of the License, or
7934 + * (at your option) any later version.
7935 + *
7936 + * This program is distributed in the hope that it will be useful,
7937 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7938 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7939 + * GNU General Public License for more details.
7940 + *
7941 + * You should have received a copy of the GNU General Public License
7942 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7943 + */
7944 +
7945 +/*
7946 + * debug print functions
7947 + */
7948 +
7949 +#ifndef __AUFS_DEBUG_H__
7950 +#define __AUFS_DEBUG_H__
7951 +
7952 +#ifdef __KERNEL__
7953 +
7954 +#include <linux/atomic.h>
7955 +#include <linux/module.h>
7956 +#include <linux/kallsyms.h>
7957 +#include <linux/sysrq.h>
7958 +
7959 +#ifdef CONFIG_AUFS_DEBUG
7960 +#define AuDebugOn(a)           BUG_ON(a)
7961 +
7962 +/* module parameter */
7963 +extern atomic_t aufs_debug;
7964 +static inline void au_debug_on(void)
7965 +{
7966 +       atomic_inc(&aufs_debug);
7967 +}
7968 +static inline void au_debug_off(void)
7969 +{
7970 +       atomic_dec_if_positive(&aufs_debug);
7971 +}
7972 +
7973 +static inline int au_debug_test(void)
7974 +{
7975 +       return atomic_read(&aufs_debug) > 0;
7976 +}
7977 +#else
7978 +#define AuDebugOn(a)           do {} while (0)
7979 +AuStubVoid(au_debug_on, void)
7980 +AuStubVoid(au_debug_off, void)
7981 +AuStubInt0(au_debug_test, void)
7982 +#endif /* CONFIG_AUFS_DEBUG */
7983 +
7984 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7985 +
7986 +/* ---------------------------------------------------------------------- */
7987 +
7988 +/* debug print */
7989 +
7990 +#define AuDbg(fmt, ...) do { \
7991 +       if (au_debug_test()) \
7992 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7993 +} while (0)
7994 +#define AuLabel(l)             AuDbg(#l "\n")
7995 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7996 +#define AuWarn1(fmt, ...) do { \
7997 +       static unsigned char _c; \
7998 +       if (!_c++) \
7999 +               pr_warn(fmt, ##__VA_ARGS__); \
8000 +} while (0)
8001 +
8002 +#define AuErr1(fmt, ...) do { \
8003 +       static unsigned char _c; \
8004 +       if (!_c++) \
8005 +               pr_err(fmt, ##__VA_ARGS__); \
8006 +} while (0)
8007 +
8008 +#define AuIOErr1(fmt, ...) do { \
8009 +       static unsigned char _c; \
8010 +       if (!_c++) \
8011 +               AuIOErr(fmt, ##__VA_ARGS__); \
8012 +} while (0)
8013 +
8014 +#define AuUnsupportMsg "This operation is not supported." \
8015 +                       " Please report this application to aufs-users ML."
8016 +#define AuUnsupport(fmt, ...) do { \
8017 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
8018 +       dump_stack(); \
8019 +} while (0)
8020 +
8021 +#define AuTraceErr(e) do { \
8022 +       if (unlikely((e) < 0)) \
8023 +               AuDbg("err %d\n", (int)(e)); \
8024 +} while (0)
8025 +
8026 +#define AuTraceErrPtr(p) do { \
8027 +       if (IS_ERR(p)) \
8028 +               AuDbg("err %ld\n", PTR_ERR(p)); \
8029 +} while (0)
8030 +
8031 +/* dirty macros for debug print, use with "%.*s" and caution */
8032 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
8033 +
8034 +/* ---------------------------------------------------------------------- */
8035 +
8036 +struct dentry;
8037 +#ifdef CONFIG_AUFS_DEBUG
8038 +extern struct mutex au_dbg_mtx;
8039 +extern char *au_plevel;
8040 +struct au_nhash;
8041 +void au_dpri_whlist(struct au_nhash *whlist);
8042 +struct au_vdir;
8043 +void au_dpri_vdir(struct au_vdir *vdir);
8044 +struct inode;
8045 +void au_dpri_inode(struct inode *inode);
8046 +void au_dpri_dalias(struct inode *inode);
8047 +void au_dpri_dentry(struct dentry *dentry);
8048 +struct file;
8049 +void au_dpri_file(struct file *filp);
8050 +struct super_block;
8051 +void au_dpri_sb(struct super_block *sb);
8052 +
8053 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
8054 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
8055 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
8056 +void au_dbg_verify_kthread(void);
8057 +
8058 +int __init au_debug_init(void);
8059 +
8060 +#define AuDbgWhlist(w) do { \
8061 +       mutex_lock(&au_dbg_mtx); \
8062 +       AuDbg(#w "\n"); \
8063 +       au_dpri_whlist(w); \
8064 +       mutex_unlock(&au_dbg_mtx); \
8065 +} while (0)
8066 +
8067 +#define AuDbgVdir(v) do { \
8068 +       mutex_lock(&au_dbg_mtx); \
8069 +       AuDbg(#v "\n"); \
8070 +       au_dpri_vdir(v); \
8071 +       mutex_unlock(&au_dbg_mtx); \
8072 +} while (0)
8073 +
8074 +#define AuDbgInode(i) do { \
8075 +       mutex_lock(&au_dbg_mtx); \
8076 +       AuDbg(#i "\n"); \
8077 +       au_dpri_inode(i); \
8078 +       mutex_unlock(&au_dbg_mtx); \
8079 +} while (0)
8080 +
8081 +#define AuDbgDAlias(i) do { \
8082 +       mutex_lock(&au_dbg_mtx); \
8083 +       AuDbg(#i "\n"); \
8084 +       au_dpri_dalias(i); \
8085 +       mutex_unlock(&au_dbg_mtx); \
8086 +} while (0)
8087 +
8088 +#define AuDbgDentry(d) do { \
8089 +       mutex_lock(&au_dbg_mtx); \
8090 +       AuDbg(#d "\n"); \
8091 +       au_dpri_dentry(d); \
8092 +       mutex_unlock(&au_dbg_mtx); \
8093 +} while (0)
8094 +
8095 +#define AuDbgFile(f) do { \
8096 +       mutex_lock(&au_dbg_mtx); \
8097 +       AuDbg(#f "\n"); \
8098 +       au_dpri_file(f); \
8099 +       mutex_unlock(&au_dbg_mtx); \
8100 +} while (0)
8101 +
8102 +#define AuDbgSb(sb) do { \
8103 +       mutex_lock(&au_dbg_mtx); \
8104 +       AuDbg(#sb "\n"); \
8105 +       au_dpri_sb(sb); \
8106 +       mutex_unlock(&au_dbg_mtx); \
8107 +} while (0)
8108 +
8109 +#define AuDbgSym(addr) do {                            \
8110 +       char sym[KSYM_SYMBOL_LEN];                      \
8111 +       sprint_symbol(sym, (unsigned long)addr);        \
8112 +       AuDbg("%s\n", sym);                             \
8113 +} while (0)
8114 +#else
8115 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8116 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8117 +AuStubVoid(au_dbg_verify_kthread, void)
8118 +AuStubInt0(__init au_debug_init, void)
8119 +
8120 +#define AuDbgWhlist(w)         do {} while (0)
8121 +#define AuDbgVdir(v)           do {} while (0)
8122 +#define AuDbgInode(i)          do {} while (0)
8123 +#define AuDbgDAlias(i)         do {} while (0)
8124 +#define AuDbgDentry(d)         do {} while (0)
8125 +#define AuDbgFile(f)           do {} while (0)
8126 +#define AuDbgSb(sb)            do {} while (0)
8127 +#define AuDbgSym(addr)         do {} while (0)
8128 +#endif /* CONFIG_AUFS_DEBUG */
8129 +
8130 +/* ---------------------------------------------------------------------- */
8131 +
8132 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8133 +int __init au_sysrq_init(void);
8134 +void au_sysrq_fin(void);
8135 +
8136 +#ifdef CONFIG_HW_CONSOLE
8137 +#define au_dbg_blocked() do { \
8138 +       WARN_ON(1); \
8139 +       handle_sysrq('w'); \
8140 +} while (0)
8141 +#else
8142 +AuStubVoid(au_dbg_blocked, void)
8143 +#endif
8144 +
8145 +#else
8146 +AuStubInt0(__init au_sysrq_init, void)
8147 +AuStubVoid(au_sysrq_fin, void)
8148 +AuStubVoid(au_dbg_blocked, void)
8149 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8150 +
8151 +#endif /* __KERNEL__ */
8152 +#endif /* __AUFS_DEBUG_H__ */
8153 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8154 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8155 +++ linux/fs/aufs/dentry.c      2018-12-27 13:19:17.708416053 +0100
8156 @@ -0,0 +1,1153 @@
8157 +// SPDX-License-Identifier: GPL-2.0
8158 +/*
8159 + * Copyright (C) 2005-2018 Junjiro R. Okajima
8160 + *
8161 + * This program, aufs is free software; you can redistribute it and/or modify
8162 + * it under the terms of the GNU General Public License as published by
8163 + * the Free Software Foundation; either version 2 of the License, or
8164 + * (at your option) any later version.
8165 + *
8166 + * This program is distributed in the hope that it will be useful,
8167 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8168 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8169 + * GNU General Public License for more details.
8170 + *
8171 + * You should have received a copy of the GNU General Public License
8172 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8173 + */
8174 +
8175 +/*
8176 + * lookup and dentry operations
8177 + */
8178 +
8179 +#include <linux/namei.h>
8180 +#include "aufs.h"
8181 +
8182 +/*
8183 + * returns positive/negative dentry, NULL or an error.
8184 + * NULL means whiteout-ed or not-found.
8185 + */
8186 +static struct dentry*
8187 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8188 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8189 +{
8190 +       struct dentry *h_dentry;
8191 +       struct inode *h_inode;
8192 +       struct au_branch *br;
8193 +       int wh_found, opq;
8194 +       unsigned char wh_able;
8195 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8196 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8197 +                                                         IGNORE_PERM);
8198 +
8199 +       wh_found = 0;
8200 +       br = au_sbr(dentry->d_sb, bindex);
8201 +       wh_able = !!au_br_whable(br->br_perm);
8202 +       if (wh_able)
8203 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8204 +       h_dentry = ERR_PTR(wh_found);
8205 +       if (!wh_found)
8206 +               goto real_lookup;
8207 +       if (unlikely(wh_found < 0))
8208 +               goto out;
8209 +
8210 +       /* We found a whiteout */
8211 +       /* au_set_dbbot(dentry, bindex); */
8212 +       au_set_dbwh(dentry, bindex);
8213 +       if (!allow_neg)
8214 +               return NULL; /* success */
8215 +
8216 +real_lookup:
8217 +       if (!ignore_perm)
8218 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8219 +       else
8220 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8221 +       if (IS_ERR(h_dentry)) {
8222 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8223 +                   && !allow_neg)
8224 +                       h_dentry = NULL;
8225 +               goto out;
8226 +       }
8227 +
8228 +       h_inode = d_inode(h_dentry);
8229 +       if (d_is_negative(h_dentry)) {
8230 +               if (!allow_neg)
8231 +                       goto out_neg;
8232 +       } else if (wh_found
8233 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8234 +               goto out_neg;
8235 +       else if (au_ftest_lkup(args->flags, DIRREN)
8236 +                /* && h_inode */
8237 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8238 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8239 +                     (unsigned long long)h_inode->i_ino);
8240 +               goto out_neg;
8241 +       }
8242 +
8243 +       if (au_dbbot(dentry) <= bindex)
8244 +               au_set_dbbot(dentry, bindex);
8245 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8246 +               au_set_dbtop(dentry, bindex);
8247 +       au_set_h_dptr(dentry, bindex, h_dentry);
8248 +
8249 +       if (!d_is_dir(h_dentry)
8250 +           || !wh_able
8251 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8252 +               goto out; /* success */
8253 +
8254 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8255 +       opq = au_diropq_test(h_dentry);
8256 +       inode_unlock_shared(h_inode);
8257 +       if (opq > 0)
8258 +               au_set_dbdiropq(dentry, bindex);
8259 +       else if (unlikely(opq < 0)) {
8260 +               au_set_h_dptr(dentry, bindex, NULL);
8261 +               h_dentry = ERR_PTR(opq);
8262 +       }
8263 +       goto out;
8264 +
8265 +out_neg:
8266 +       dput(h_dentry);
8267 +       h_dentry = NULL;
8268 +out:
8269 +       return h_dentry;
8270 +}
8271 +
8272 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8273 +{
8274 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8275 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8276 +               return -EPERM;
8277 +       return 0;
8278 +}
8279 +
8280 +/*
8281 + * returns the number of lower positive dentries,
8282 + * otherwise an error.
8283 + * can be called at unlinking with @type is zero.
8284 + */
8285 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8286 +                  unsigned int flags)
8287 +{
8288 +       int npositive, err;
8289 +       aufs_bindex_t bindex, btail, bdiropq;
8290 +       unsigned char isdir, dirperm1, dirren;
8291 +       struct au_do_lookup_args args = {
8292 +               .flags          = flags,
8293 +               .name           = &dentry->d_name
8294 +       };
8295 +       struct dentry *parent;
8296 +       struct super_block *sb;
8297 +
8298 +       sb = dentry->d_sb;
8299 +       err = au_test_shwh(sb, args.name);
8300 +       if (unlikely(err))
8301 +               goto out;
8302 +
8303 +       err = au_wh_name_alloc(&args.whname, args.name);
8304 +       if (unlikely(err))
8305 +               goto out;
8306 +
8307 +       isdir = !!d_is_dir(dentry);
8308 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8309 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8310 +       if (dirren)
8311 +               au_fset_lkup(args.flags, DIRREN);
8312 +
8313 +       npositive = 0;
8314 +       parent = dget_parent(dentry);
8315 +       btail = au_dbtaildir(parent);
8316 +       for (bindex = btop; bindex <= btail; bindex++) {
8317 +               struct dentry *h_parent, *h_dentry;
8318 +               struct inode *h_inode, *h_dir;
8319 +               struct au_branch *br;
8320 +
8321 +               h_dentry = au_h_dptr(dentry, bindex);
8322 +               if (h_dentry) {
8323 +                       if (d_is_positive(h_dentry))
8324 +                               npositive++;
8325 +                       break;
8326 +               }
8327 +               h_parent = au_h_dptr(parent, bindex);
8328 +               if (!h_parent || !d_is_dir(h_parent))
8329 +                       continue;
8330 +
8331 +               if (dirren) {
8332 +                       /* if the inum matches, then use the prepared name */
8333 +                       err = au_dr_lkup_name(&args, bindex);
8334 +                       if (unlikely(err))
8335 +                               goto out_parent;
8336 +               }
8337 +
8338 +               h_dir = d_inode(h_parent);
8339 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8340 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8341 +               inode_unlock_shared(h_dir);
8342 +               err = PTR_ERR(h_dentry);
8343 +               if (IS_ERR(h_dentry))
8344 +                       goto out_parent;
8345 +               if (h_dentry)
8346 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8347 +               if (dirperm1)
8348 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8349 +
8350 +               if (au_dbwh(dentry) == bindex)
8351 +                       break;
8352 +               if (!h_dentry)
8353 +                       continue;
8354 +               if (d_is_negative(h_dentry))
8355 +                       continue;
8356 +               h_inode = d_inode(h_dentry);
8357 +               npositive++;
8358 +               if (!args.type)
8359 +                       args.type = h_inode->i_mode & S_IFMT;
8360 +               if (args.type != S_IFDIR)
8361 +                       break;
8362 +               else if (isdir) {
8363 +                       /* the type of lower may be different */
8364 +                       bdiropq = au_dbdiropq(dentry);
8365 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8366 +                               break;
8367 +               }
8368 +               br = au_sbr(sb, bindex);
8369 +               if (dirren
8370 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8371 +                                          /*add_ent*/NULL)) {
8372 +                       /* prepare next name to lookup */
8373 +                       err = au_dr_lkup(&args, dentry, bindex);
8374 +                       if (unlikely(err))
8375 +                               goto out_parent;
8376 +               }
8377 +       }
8378 +
8379 +       if (npositive) {
8380 +               AuLabel(positive);
8381 +               au_update_dbtop(dentry);
8382 +       }
8383 +       err = npositive;
8384 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8385 +                    && au_dbtop(dentry) < 0)) {
8386 +               err = -EIO;
8387 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8388 +                       dentry, err);
8389 +       }
8390 +
8391 +out_parent:
8392 +       dput(parent);
8393 +       au_kfree_try_rcu(args.whname.name);
8394 +       if (dirren)
8395 +               au_dr_lkup_fin(&args);
8396 +out:
8397 +       return err;
8398 +}
8399 +
8400 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8401 +{
8402 +       struct dentry *dentry;
8403 +       int wkq_err;
8404 +
8405 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8406 +               dentry = vfsub_lkup_one(name, parent);
8407 +       else {
8408 +               struct vfsub_lkup_one_args args = {
8409 +                       .errp   = &dentry,
8410 +                       .name   = name,
8411 +                       .parent = parent
8412 +               };
8413 +
8414 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8415 +               if (unlikely(wkq_err))
8416 +                       dentry = ERR_PTR(wkq_err);
8417 +       }
8418 +
8419 +       return dentry;
8420 +}
8421 +
8422 +/*
8423 + * lookup @dentry on @bindex which should be negative.
8424 + */
8425 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8426 +{
8427 +       int err;
8428 +       struct dentry *parent, *h_parent, *h_dentry;
8429 +       struct au_branch *br;
8430 +
8431 +       parent = dget_parent(dentry);
8432 +       h_parent = au_h_dptr(parent, bindex);
8433 +       br = au_sbr(dentry->d_sb, bindex);
8434 +       if (wh)
8435 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8436 +       else
8437 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8438 +       err = PTR_ERR(h_dentry);
8439 +       if (IS_ERR(h_dentry))
8440 +               goto out;
8441 +       if (unlikely(d_is_positive(h_dentry))) {
8442 +               err = -EIO;
8443 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8444 +               dput(h_dentry);
8445 +               goto out;
8446 +       }
8447 +
8448 +       err = 0;
8449 +       if (bindex < au_dbtop(dentry))
8450 +               au_set_dbtop(dentry, bindex);
8451 +       if (au_dbbot(dentry) < bindex)
8452 +               au_set_dbbot(dentry, bindex);
8453 +       au_set_h_dptr(dentry, bindex, h_dentry);
8454 +
8455 +out:
8456 +       dput(parent);
8457 +       return err;
8458 +}
8459 +
8460 +/* ---------------------------------------------------------------------- */
8461 +
8462 +/* subset of struct inode */
8463 +struct au_iattr {
8464 +       unsigned long           i_ino;
8465 +       /* unsigned int         i_nlink; */
8466 +       kuid_t                  i_uid;
8467 +       kgid_t                  i_gid;
8468 +       u64                     i_version;
8469 +/*
8470 +       loff_t                  i_size;
8471 +       blkcnt_t                i_blocks;
8472 +*/
8473 +       umode_t                 i_mode;
8474 +};
8475 +
8476 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8477 +{
8478 +       ia->i_ino = h_inode->i_ino;
8479 +       /* ia->i_nlink = h_inode->i_nlink; */
8480 +       ia->i_uid = h_inode->i_uid;
8481 +       ia->i_gid = h_inode->i_gid;
8482 +       ia->i_version = inode_query_iversion(h_inode);
8483 +/*
8484 +       ia->i_size = h_inode->i_size;
8485 +       ia->i_blocks = h_inode->i_blocks;
8486 +*/
8487 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8488 +}
8489 +
8490 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8491 +{
8492 +       return ia->i_ino != h_inode->i_ino
8493 +               /* || ia->i_nlink != h_inode->i_nlink */
8494 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8495 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8496 +               || !inode_eq_iversion(h_inode, ia->i_version)
8497 +/*
8498 +               || ia->i_size != h_inode->i_size
8499 +               || ia->i_blocks != h_inode->i_blocks
8500 +*/
8501 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8502 +}
8503 +
8504 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8505 +                             struct au_branch *br)
8506 +{
8507 +       int err;
8508 +       struct au_iattr ia;
8509 +       struct inode *h_inode;
8510 +       struct dentry *h_d;
8511 +       struct super_block *h_sb;
8512 +
8513 +       err = 0;
8514 +       memset(&ia, -1, sizeof(ia));
8515 +       h_sb = h_dentry->d_sb;
8516 +       h_inode = NULL;
8517 +       if (d_is_positive(h_dentry)) {
8518 +               h_inode = d_inode(h_dentry);
8519 +               au_iattr_save(&ia, h_inode);
8520 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8521 +               /* nfs d_revalidate may return 0 for negative dentry */
8522 +               /* fuse d_revalidate always return 0 for negative dentry */
8523 +               goto out;
8524 +
8525 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8526 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8527 +       err = PTR_ERR(h_d);
8528 +       if (IS_ERR(h_d))
8529 +               goto out;
8530 +
8531 +       err = 0;
8532 +       if (unlikely(h_d != h_dentry
8533 +                    || d_inode(h_d) != h_inode
8534 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8535 +               err = au_busy_or_stale();
8536 +       dput(h_d);
8537 +
8538 +out:
8539 +       AuTraceErr(err);
8540 +       return err;
8541 +}
8542 +
8543 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8544 +               struct dentry *h_parent, struct au_branch *br)
8545 +{
8546 +       int err;
8547 +
8548 +       err = 0;
8549 +       if (udba == AuOpt_UDBA_REVAL
8550 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8551 +               IMustLock(h_dir);
8552 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8553 +       } else if (udba != AuOpt_UDBA_NONE)
8554 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8555 +
8556 +       return err;
8557 +}
8558 +
8559 +/* ---------------------------------------------------------------------- */
8560 +
8561 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8562 +{
8563 +       int err;
8564 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8565 +       struct au_hdentry tmp, *p, *q;
8566 +       struct au_dinfo *dinfo;
8567 +       struct super_block *sb;
8568 +
8569 +       DiMustWriteLock(dentry);
8570 +
8571 +       sb = dentry->d_sb;
8572 +       dinfo = au_di(dentry);
8573 +       bbot = dinfo->di_bbot;
8574 +       bwh = dinfo->di_bwh;
8575 +       bdiropq = dinfo->di_bdiropq;
8576 +       bindex = dinfo->di_btop;
8577 +       p = au_hdentry(dinfo, bindex);
8578 +       for (; bindex <= bbot; bindex++, p++) {
8579 +               if (!p->hd_dentry)
8580 +                       continue;
8581 +
8582 +               new_bindex = au_br_index(sb, p->hd_id);
8583 +               if (new_bindex == bindex)
8584 +                       continue;
8585 +
8586 +               if (dinfo->di_bwh == bindex)
8587 +                       bwh = new_bindex;
8588 +               if (dinfo->di_bdiropq == bindex)
8589 +                       bdiropq = new_bindex;
8590 +               if (new_bindex < 0) {
8591 +                       au_hdput(p);
8592 +                       p->hd_dentry = NULL;
8593 +                       continue;
8594 +               }
8595 +
8596 +               /* swap two lower dentries, and loop again */
8597 +               q = au_hdentry(dinfo, new_bindex);
8598 +               tmp = *q;
8599 +               *q = *p;
8600 +               *p = tmp;
8601 +               if (tmp.hd_dentry) {
8602 +                       bindex--;
8603 +                       p--;
8604 +               }
8605 +       }
8606 +
8607 +       dinfo->di_bwh = -1;
8608 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8609 +               dinfo->di_bwh = bwh;
8610 +
8611 +       dinfo->di_bdiropq = -1;
8612 +       if (bdiropq >= 0
8613 +           && bdiropq <= au_sbbot(sb)
8614 +           && au_sbr_whable(sb, bdiropq))
8615 +               dinfo->di_bdiropq = bdiropq;
8616 +
8617 +       err = -EIO;
8618 +       dinfo->di_btop = -1;
8619 +       dinfo->di_bbot = -1;
8620 +       bbot = au_dbbot(parent);
8621 +       bindex = 0;
8622 +       p = au_hdentry(dinfo, bindex);
8623 +       for (; bindex <= bbot; bindex++, p++)
8624 +               if (p->hd_dentry) {
8625 +                       dinfo->di_btop = bindex;
8626 +                       break;
8627 +               }
8628 +
8629 +       if (dinfo->di_btop >= 0) {
8630 +               bindex = bbot;
8631 +               p = au_hdentry(dinfo, bindex);
8632 +               for (; bindex >= 0; bindex--, p--)
8633 +                       if (p->hd_dentry) {
8634 +                               dinfo->di_bbot = bindex;
8635 +                               err = 0;
8636 +                               break;
8637 +                       }
8638 +       }
8639 +
8640 +       return err;
8641 +}
8642 +
8643 +static void au_do_hide(struct dentry *dentry)
8644 +{
8645 +       struct inode *inode;
8646 +
8647 +       if (d_really_is_positive(dentry)) {
8648 +               inode = d_inode(dentry);
8649 +               if (!d_is_dir(dentry)) {
8650 +                       if (inode->i_nlink && !d_unhashed(dentry))
8651 +                               drop_nlink(inode);
8652 +               } else {
8653 +                       clear_nlink(inode);
8654 +                       /* stop next lookup */
8655 +                       inode->i_flags |= S_DEAD;
8656 +               }
8657 +               smp_mb(); /* necessary? */
8658 +       }
8659 +       d_drop(dentry);
8660 +}
8661 +
8662 +static int au_hide_children(struct dentry *parent)
8663 +{
8664 +       int err, i, j, ndentry;
8665 +       struct au_dcsub_pages dpages;
8666 +       struct au_dpage *dpage;
8667 +       struct dentry *dentry;
8668 +
8669 +       err = au_dpages_init(&dpages, GFP_NOFS);
8670 +       if (unlikely(err))
8671 +               goto out;
8672 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8673 +       if (unlikely(err))
8674 +               goto out_dpages;
8675 +
8676 +       /* in reverse order */
8677 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8678 +               dpage = dpages.dpages + i;
8679 +               ndentry = dpage->ndentry;
8680 +               for (j = ndentry - 1; j >= 0; j--) {
8681 +                       dentry = dpage->dentries[j];
8682 +                       if (dentry != parent)
8683 +                               au_do_hide(dentry);
8684 +               }
8685 +       }
8686 +
8687 +out_dpages:
8688 +       au_dpages_free(&dpages);
8689 +out:
8690 +       return err;
8691 +}
8692 +
8693 +static void au_hide(struct dentry *dentry)
8694 +{
8695 +       int err;
8696 +
8697 +       AuDbgDentry(dentry);
8698 +       if (d_is_dir(dentry)) {
8699 +               /* shrink_dcache_parent(dentry); */
8700 +               err = au_hide_children(dentry);
8701 +               if (unlikely(err))
8702 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8703 +                               dentry, err);
8704 +       }
8705 +       au_do_hide(dentry);
8706 +}
8707 +
8708 +/*
8709 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8710 + *
8711 + * a dirty branch is added
8712 + * - on the top of layers
8713 + * - in the middle of layers
8714 + * - to the bottom of layers
8715 + *
8716 + * on the added branch there exists
8717 + * - a whiteout
8718 + * - a diropq
8719 + * - a same named entry
8720 + *   + exist
8721 + *     * negative --> positive
8722 + *     * positive --> positive
8723 + *      - type is unchanged
8724 + *      - type is changed
8725 + *   + doesn't exist
8726 + *     * negative --> negative
8727 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8728 + * - none
8729 + */
8730 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8731 +                              struct au_dinfo *tmp)
8732 +{
8733 +       int err;
8734 +       aufs_bindex_t bindex, bbot;
8735 +       struct {
8736 +               struct dentry *dentry;
8737 +               struct inode *inode;
8738 +               mode_t mode;
8739 +       } orig_h, tmp_h = {
8740 +               .dentry = NULL
8741 +       };
8742 +       struct au_hdentry *hd;
8743 +       struct inode *inode, *h_inode;
8744 +       struct dentry *h_dentry;
8745 +
8746 +       err = 0;
8747 +       AuDebugOn(dinfo->di_btop < 0);
8748 +       orig_h.mode = 0;
8749 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8750 +       orig_h.inode = NULL;
8751 +       if (d_is_positive(orig_h.dentry)) {
8752 +               orig_h.inode = d_inode(orig_h.dentry);
8753 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8754 +       }
8755 +       if (tmp->di_btop >= 0) {
8756 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8757 +               if (d_is_positive(tmp_h.dentry)) {
8758 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8759 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8760 +               }
8761 +       }
8762 +
8763 +       inode = NULL;
8764 +       if (d_really_is_positive(dentry))
8765 +               inode = d_inode(dentry);
8766 +       if (!orig_h.inode) {
8767 +               AuDbg("negative originally\n");
8768 +               if (inode) {
8769 +                       au_hide(dentry);
8770 +                       goto out;
8771 +               }
8772 +               AuDebugOn(inode);
8773 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8774 +               AuDebugOn(dinfo->di_bdiropq != -1);
8775 +
8776 +               if (!tmp_h.inode) {
8777 +                       AuDbg("negative --> negative\n");
8778 +                       /* should have only one negative lower */
8779 +                       if (tmp->di_btop >= 0
8780 +                           && tmp->di_btop < dinfo->di_btop) {
8781 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8782 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8783 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8784 +                               au_di_cp(dinfo, tmp);
8785 +                               hd = au_hdentry(tmp, tmp->di_btop);
8786 +                               au_set_h_dptr(dentry, tmp->di_btop,
8787 +                                             dget(hd->hd_dentry));
8788 +                       }
8789 +                       au_dbg_verify_dinode(dentry);
8790 +               } else {
8791 +                       AuDbg("negative --> positive\n");
8792 +                       /*
8793 +                        * similar to the behaviour of creating with bypassing
8794 +                        * aufs.
8795 +                        * unhash it in order to force an error in the
8796 +                        * succeeding create operation.
8797 +                        * we should not set S_DEAD here.
8798 +                        */
8799 +                       d_drop(dentry);
8800 +                       /* au_di_swap(tmp, dinfo); */
8801 +                       au_dbg_verify_dinode(dentry);
8802 +               }
8803 +       } else {
8804 +               AuDbg("positive originally\n");
8805 +               /* inode may be NULL */
8806 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8807 +               if (!tmp_h.inode) {
8808 +                       AuDbg("positive --> negative\n");
8809 +                       /* or bypassing aufs */
8810 +                       au_hide(dentry);
8811 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8812 +                               dinfo->di_bwh = tmp->di_bwh;
8813 +                       if (inode)
8814 +                               err = au_refresh_hinode_self(inode);
8815 +                       au_dbg_verify_dinode(dentry);
8816 +               } else if (orig_h.mode == tmp_h.mode) {
8817 +                       AuDbg("positive --> positive, same type\n");
8818 +                       if (!S_ISDIR(orig_h.mode)
8819 +                           && dinfo->di_btop > tmp->di_btop) {
8820 +                               /*
8821 +                                * similar to the behaviour of removing and
8822 +                                * creating.
8823 +                                */
8824 +                               au_hide(dentry);
8825 +                               if (inode)
8826 +                                       err = au_refresh_hinode_self(inode);
8827 +                               au_dbg_verify_dinode(dentry);
8828 +                       } else {
8829 +                               /* fill empty slots */
8830 +                               if (dinfo->di_btop > tmp->di_btop)
8831 +                                       dinfo->di_btop = tmp->di_btop;
8832 +                               if (dinfo->di_bbot < tmp->di_bbot)
8833 +                                       dinfo->di_bbot = tmp->di_bbot;
8834 +                               dinfo->di_bwh = tmp->di_bwh;
8835 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8836 +                               bbot = dinfo->di_bbot;
8837 +                               bindex = tmp->di_btop;
8838 +                               hd = au_hdentry(tmp, bindex);
8839 +                               for (; bindex <= bbot; bindex++, hd++) {
8840 +                                       if (au_h_dptr(dentry, bindex))
8841 +                                               continue;
8842 +                                       h_dentry = hd->hd_dentry;
8843 +                                       if (!h_dentry)
8844 +                                               continue;
8845 +                                       AuDebugOn(d_is_negative(h_dentry));
8846 +                                       h_inode = d_inode(h_dentry);
8847 +                                       AuDebugOn(orig_h.mode
8848 +                                                 != (h_inode->i_mode
8849 +                                                     & S_IFMT));
8850 +                                       au_set_h_dptr(dentry, bindex,
8851 +                                                     dget(h_dentry));
8852 +                               }
8853 +                               if (inode)
8854 +                                       err = au_refresh_hinode(inode, dentry);
8855 +                               au_dbg_verify_dinode(dentry);
8856 +                       }
8857 +               } else {
8858 +                       AuDbg("positive --> positive, different type\n");
8859 +                       /* similar to the behaviour of removing and creating */
8860 +                       au_hide(dentry);
8861 +                       if (inode)
8862 +                               err = au_refresh_hinode_self(inode);
8863 +                       au_dbg_verify_dinode(dentry);
8864 +               }
8865 +       }
8866 +
8867 +out:
8868 +       return err;
8869 +}
8870 +
8871 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8872 +{
8873 +       const struct dentry_operations *dop
8874 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8875 +       static const unsigned int mask
8876 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8877 +
8878 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8879 +
8880 +       if (dentry->d_op == dop)
8881 +               return;
8882 +
8883 +       AuDbg("%pd\n", dentry);
8884 +       spin_lock(&dentry->d_lock);
8885 +       if (dop == &aufs_dop)
8886 +               dentry->d_flags |= mask;
8887 +       else
8888 +               dentry->d_flags &= ~mask;
8889 +       dentry->d_op = dop;
8890 +       spin_unlock(&dentry->d_lock);
8891 +}
8892 +
8893 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8894 +{
8895 +       int err, ebrange, nbr;
8896 +       unsigned int sigen;
8897 +       struct au_dinfo *dinfo, *tmp;
8898 +       struct super_block *sb;
8899 +       struct inode *inode;
8900 +
8901 +       DiMustWriteLock(dentry);
8902 +       AuDebugOn(IS_ROOT(dentry));
8903 +       AuDebugOn(d_really_is_negative(parent));
8904 +
8905 +       sb = dentry->d_sb;
8906 +       sigen = au_sigen(sb);
8907 +       err = au_digen_test(parent, sigen);
8908 +       if (unlikely(err))
8909 +               goto out;
8910 +
8911 +       nbr = au_sbbot(sb) + 1;
8912 +       dinfo = au_di(dentry);
8913 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8914 +       if (unlikely(err))
8915 +               goto out;
8916 +       ebrange = au_dbrange_test(dentry);
8917 +       if (!ebrange)
8918 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8919 +
8920 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8921 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8922 +               if (d_really_is_positive(dentry)) {
8923 +                       inode = d_inode(dentry);
8924 +                       err = au_refresh_hinode_self(inode);
8925 +               }
8926 +               au_dbg_verify_dinode(dentry);
8927 +               if (!err)
8928 +                       goto out_dgen; /* success */
8929 +               goto out;
8930 +       }
8931 +
8932 +       /* temporary dinfo */
8933 +       AuDbgDentry(dentry);
8934 +       err = -ENOMEM;
8935 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8936 +       if (unlikely(!tmp))
8937 +               goto out;
8938 +       au_di_swap(tmp, dinfo);
8939 +       /* returns the number of positive dentries */
8940 +       /*
8941 +        * if current working dir is removed, it returns an error.
8942 +        * but the dentry is legal.
8943 +        */
8944 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8945 +       AuDbgDentry(dentry);
8946 +       au_di_swap(tmp, dinfo);
8947 +       if (err == -ENOENT)
8948 +               err = 0;
8949 +       if (err >= 0) {
8950 +               /* compare/refresh by dinfo */
8951 +               AuDbgDentry(dentry);
8952 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8953 +               au_dbg_verify_dinode(dentry);
8954 +               AuTraceErr(err);
8955 +       }
8956 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8957 +       au_rw_write_unlock(&tmp->di_rwsem);
8958 +       au_di_free(tmp);
8959 +       if (unlikely(err))
8960 +               goto out;
8961 +
8962 +out_dgen:
8963 +       au_update_digen(dentry);
8964 +out:
8965 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8966 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8967 +               AuDbgDentry(dentry);
8968 +       }
8969 +       AuTraceErr(err);
8970 +       return err;
8971 +}
8972 +
8973 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8974 +                          struct dentry *dentry, aufs_bindex_t bindex)
8975 +{
8976 +       int err, valid;
8977 +
8978 +       err = 0;
8979 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8980 +               goto out;
8981 +
8982 +       AuDbg("b%d\n", bindex);
8983 +       /*
8984 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8985 +        * due to whiteout and branch permission.
8986 +        */
8987 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8988 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8989 +       /* it may return tri-state */
8990 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8991 +
8992 +       if (unlikely(valid < 0))
8993 +               err = valid;
8994 +       else if (!valid)
8995 +               err = -EINVAL;
8996 +
8997 +out:
8998 +       AuTraceErr(err);
8999 +       return err;
9000 +}
9001 +
9002 +/* todo: remove this */
9003 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
9004 +                         unsigned int flags, int do_udba, int dirren)
9005 +{
9006 +       int err;
9007 +       umode_t mode, h_mode;
9008 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
9009 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
9010 +       struct inode *h_inode, *h_cached_inode;
9011 +       struct dentry *h_dentry;
9012 +       struct qstr *name, *h_name;
9013 +
9014 +       err = 0;
9015 +       plus = 0;
9016 +       mode = 0;
9017 +       ibs = -1;
9018 +       ibe = -1;
9019 +       unhashed = !!d_unhashed(dentry);
9020 +       is_root = !!IS_ROOT(dentry);
9021 +       name = &dentry->d_name;
9022 +       tmpfile = au_di(dentry)->di_tmpfile;
9023 +
9024 +       /*
9025 +        * Theoretically, REVAL test should be unnecessary in case of
9026 +        * {FS,I}NOTIFY.
9027 +        * But {fs,i}notify doesn't fire some necessary events,
9028 +        *      IN_ATTRIB for atime/nlink/pageio
9029 +        * Let's do REVAL test too.
9030 +        */
9031 +       if (do_udba && inode) {
9032 +               mode = (inode->i_mode & S_IFMT);
9033 +               plus = (inode->i_nlink > 0);
9034 +               ibs = au_ibtop(inode);
9035 +               ibe = au_ibbot(inode);
9036 +       }
9037 +
9038 +       btop = au_dbtop(dentry);
9039 +       btail = btop;
9040 +       if (inode && S_ISDIR(inode->i_mode))
9041 +               btail = au_dbtaildir(dentry);
9042 +       for (bindex = btop; bindex <= btail; bindex++) {
9043 +               h_dentry = au_h_dptr(dentry, bindex);
9044 +               if (!h_dentry)
9045 +                       continue;
9046 +
9047 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
9048 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
9049 +               spin_lock(&h_dentry->d_lock);
9050 +               h_name = &h_dentry->d_name;
9051 +               if (unlikely(do_udba
9052 +                            && !is_root
9053 +                            && ((!h_nfs
9054 +                                 && (unhashed != !!d_unhashed(h_dentry)
9055 +                                     || (!tmpfile && !dirren
9056 +                                         && !au_qstreq(name, h_name))
9057 +                                         ))
9058 +                                || (h_nfs
9059 +                                    && !(flags & LOOKUP_OPEN)
9060 +                                    && (h_dentry->d_flags
9061 +                                        & DCACHE_NFSFS_RENAMED)))
9062 +                           )) {
9063 +                       int h_unhashed;
9064 +
9065 +                       h_unhashed = d_unhashed(h_dentry);
9066 +                       spin_unlock(&h_dentry->d_lock);
9067 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
9068 +                             unhashed, h_unhashed, dentry, h_dentry);
9069 +                       goto err;
9070 +               }
9071 +               spin_unlock(&h_dentry->d_lock);
9072 +
9073 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
9074 +               if (unlikely(err))
9075 +                       /* do not goto err, to keep the errno */
9076 +                       break;
9077 +
9078 +               /* todo: plink too? */
9079 +               if (!do_udba)
9080 +                       continue;
9081 +
9082 +               /* UDBA tests */
9083 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
9084 +                       goto err;
9085 +
9086 +               h_inode = NULL;
9087 +               if (d_is_positive(h_dentry))
9088 +                       h_inode = d_inode(h_dentry);
9089 +               h_plus = plus;
9090 +               h_mode = mode;
9091 +               h_cached_inode = h_inode;
9092 +               if (h_inode) {
9093 +                       h_mode = (h_inode->i_mode & S_IFMT);
9094 +                       h_plus = (h_inode->i_nlink > 0);
9095 +               }
9096 +               if (inode && ibs <= bindex && bindex <= ibe)
9097 +                       h_cached_inode = au_h_iptr(inode, bindex);
9098 +
9099 +               if (!h_nfs) {
9100 +                       if (unlikely(plus != h_plus && !tmpfile))
9101 +                               goto err;
9102 +               } else {
9103 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9104 +                                    && !is_root
9105 +                                    && !IS_ROOT(h_dentry)
9106 +                                    && unhashed != d_unhashed(h_dentry)))
9107 +                               goto err;
9108 +               }
9109 +               if (unlikely(mode != h_mode
9110 +                            || h_cached_inode != h_inode))
9111 +                       goto err;
9112 +               continue;
9113 +
9114 +err:
9115 +               err = -EINVAL;
9116 +               break;
9117 +       }
9118 +
9119 +       AuTraceErr(err);
9120 +       return err;
9121 +}
9122 +
9123 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9124 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9125 +{
9126 +       int err;
9127 +       struct dentry *parent;
9128 +
9129 +       if (!au_digen_test(dentry, sigen))
9130 +               return 0;
9131 +
9132 +       parent = dget_parent(dentry);
9133 +       di_read_lock_parent(parent, AuLock_IR);
9134 +       AuDebugOn(au_digen_test(parent, sigen));
9135 +       au_dbg_verify_gen(parent, sigen);
9136 +       err = au_refresh_dentry(dentry, parent);
9137 +       di_read_unlock(parent, AuLock_IR);
9138 +       dput(parent);
9139 +       AuTraceErr(err);
9140 +       return err;
9141 +}
9142 +
9143 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9144 +{
9145 +       int err;
9146 +       struct dentry *d, *parent;
9147 +
9148 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9149 +               return simple_reval_dpath(dentry, sigen);
9150 +
9151 +       /* slow loop, keep it simple and stupid */
9152 +       /* cf: au_cpup_dirs() */
9153 +       err = 0;
9154 +       parent = NULL;
9155 +       while (au_digen_test(dentry, sigen)) {
9156 +               d = dentry;
9157 +               while (1) {
9158 +                       dput(parent);
9159 +                       parent = dget_parent(d);
9160 +                       if (!au_digen_test(parent, sigen))
9161 +                               break;
9162 +                       d = parent;
9163 +               }
9164 +
9165 +               if (d != dentry)
9166 +                       di_write_lock_child2(d);
9167 +
9168 +               /* someone might update our dentry while we were sleeping */
9169 +               if (au_digen_test(d, sigen)) {
9170 +                       /*
9171 +                        * todo: consolidate with simple_reval_dpath(),
9172 +                        * do_refresh() and au_reval_for_attr().
9173 +                        */
9174 +                       di_read_lock_parent(parent, AuLock_IR);
9175 +                       err = au_refresh_dentry(d, parent);
9176 +                       di_read_unlock(parent, AuLock_IR);
9177 +               }
9178 +
9179 +               if (d != dentry)
9180 +                       di_write_unlock(d);
9181 +               dput(parent);
9182 +               if (unlikely(err))
9183 +                       break;
9184 +       }
9185 +
9186 +       return err;
9187 +}
9188 +
9189 +/*
9190 + * if valid returns 1, otherwise 0.
9191 + */
9192 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9193 +{
9194 +       int valid, err;
9195 +       unsigned int sigen;
9196 +       unsigned char do_udba, dirren;
9197 +       struct super_block *sb;
9198 +       struct inode *inode;
9199 +
9200 +       /* todo: support rcu-walk? */
9201 +       if (flags & LOOKUP_RCU)
9202 +               return -ECHILD;
9203 +
9204 +       valid = 0;
9205 +       if (unlikely(!au_di(dentry)))
9206 +               goto out;
9207 +
9208 +       valid = 1;
9209 +       sb = dentry->d_sb;
9210 +       /*
9211 +        * todo: very ugly
9212 +        * i_mutex of parent dir may be held,
9213 +        * but we should not return 'invalid' due to busy.
9214 +        */
9215 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9216 +       if (unlikely(err)) {
9217 +               valid = err;
9218 +               AuTraceErr(err);
9219 +               goto out;
9220 +       }
9221 +       inode = NULL;
9222 +       if (d_really_is_positive(dentry))
9223 +               inode = d_inode(dentry);
9224 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9225 +               err = -EINVAL;
9226 +               AuTraceErr(err);
9227 +               goto out_dgrade;
9228 +       }
9229 +       if (unlikely(au_dbrange_test(dentry))) {
9230 +               err = -EINVAL;
9231 +               AuTraceErr(err);
9232 +               goto out_dgrade;
9233 +       }
9234 +
9235 +       sigen = au_sigen(sb);
9236 +       if (au_digen_test(dentry, sigen)) {
9237 +               AuDebugOn(IS_ROOT(dentry));
9238 +               err = au_reval_dpath(dentry, sigen);
9239 +               if (unlikely(err)) {
9240 +                       AuTraceErr(err);
9241 +                       goto out_dgrade;
9242 +               }
9243 +       }
9244 +       di_downgrade_lock(dentry, AuLock_IR);
9245 +
9246 +       err = -EINVAL;
9247 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9248 +           && inode
9249 +           && !(inode->i_state && I_LINKABLE)
9250 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9251 +               AuTraceErr(err);
9252 +               goto out_inval;
9253 +       }
9254 +
9255 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9256 +       if (do_udba && inode) {
9257 +               aufs_bindex_t btop = au_ibtop(inode);
9258 +               struct inode *h_inode;
9259 +
9260 +               if (btop >= 0) {
9261 +                       h_inode = au_h_iptr(inode, btop);
9262 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9263 +                               AuTraceErr(err);
9264 +                               goto out_inval;
9265 +                       }
9266 +               }
9267 +       }
9268 +
9269 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9270 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9271 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9272 +               err = -EIO;
9273 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9274 +                     dentry, err);
9275 +       }
9276 +       goto out_inval;
9277 +
9278 +out_dgrade:
9279 +       di_downgrade_lock(dentry, AuLock_IR);
9280 +out_inval:
9281 +       aufs_read_unlock(dentry, AuLock_IR);
9282 +       AuTraceErr(err);
9283 +       valid = !err;
9284 +out:
9285 +       if (!valid) {
9286 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9287 +               d_drop(dentry);
9288 +       }
9289 +       return valid;
9290 +}
9291 +
9292 +static void aufs_d_release(struct dentry *dentry)
9293 +{
9294 +       if (au_di(dentry)) {
9295 +               au_di_fin(dentry);
9296 +               au_hn_di_reinit(dentry);
9297 +       }
9298 +}
9299 +
9300 +const struct dentry_operations aufs_dop = {
9301 +       .d_revalidate           = aufs_d_revalidate,
9302 +       .d_weak_revalidate      = aufs_d_revalidate,
9303 +       .d_release              = aufs_d_release
9304 +};
9305 +
9306 +/* aufs_dop without d_revalidate */
9307 +const struct dentry_operations aufs_dop_noreval = {
9308 +       .d_release              = aufs_d_release
9309 +};
9310 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9311 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9312 +++ linux/fs/aufs/dentry.h      2018-12-27 13:19:17.708416053 +0100
9313 @@ -0,0 +1,268 @@
9314 +/* SPDX-License-Identifier: GPL-2.0 */
9315 +/*
9316 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9317 + *
9318 + * This program, aufs is free software; you can redistribute it and/or modify
9319 + * it under the terms of the GNU General Public License as published by
9320 + * the Free Software Foundation; either version 2 of the License, or
9321 + * (at your option) any later version.
9322 + *
9323 + * This program is distributed in the hope that it will be useful,
9324 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9325 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9326 + * GNU General Public License for more details.
9327 + *
9328 + * You should have received a copy of the GNU General Public License
9329 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9330 + */
9331 +
9332 +/*
9333 + * lookup and dentry operations
9334 + */
9335 +
9336 +#ifndef __AUFS_DENTRY_H__
9337 +#define __AUFS_DENTRY_H__
9338 +
9339 +#ifdef __KERNEL__
9340 +
9341 +#include <linux/dcache.h>
9342 +#include "dirren.h"
9343 +#include "rwsem.h"
9344 +
9345 +struct au_hdentry {
9346 +       struct dentry           *hd_dentry;
9347 +       aufs_bindex_t           hd_id;
9348 +};
9349 +
9350 +struct au_dinfo {
9351 +       atomic_t                di_generation;
9352 +
9353 +       struct au_rwsem         di_rwsem;
9354 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9355 +       unsigned char           di_tmpfile; /* to allow the different name */
9356 +       struct au_hdentry       *di_hdentry;
9357 +       struct rcu_head         rcu;
9358 +} ____cacheline_aligned_in_smp;
9359 +
9360 +/* ---------------------------------------------------------------------- */
9361 +
9362 +/* flags for au_lkup_dentry() */
9363 +#define AuLkup_ALLOW_NEG       1
9364 +#define AuLkup_IGNORE_PERM     (1 << 1)
9365 +#define AuLkup_DIRREN          (1 << 2)
9366 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9367 +#define au_fset_lkup(flags, name) \
9368 +       do { (flags) |= AuLkup_##name; } while (0)
9369 +#define au_fclr_lkup(flags, name) \
9370 +       do { (flags) &= ~AuLkup_##name; } while (0)
9371 +
9372 +#ifndef CONFIG_AUFS_DIRREN
9373 +#undef AuLkup_DIRREN
9374 +#define AuLkup_DIRREN 0
9375 +#endif
9376 +
9377 +struct au_do_lookup_args {
9378 +       unsigned int            flags;
9379 +       mode_t                  type;
9380 +       struct qstr             whname, *name;
9381 +       struct au_dr_lookup     dirren;
9382 +};
9383 +
9384 +/* ---------------------------------------------------------------------- */
9385 +
9386 +/* dentry.c */
9387 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9388 +struct au_branch;
9389 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9390 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9391 +               struct dentry *h_parent, struct au_branch *br);
9392 +
9393 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9394 +                  unsigned int flags);
9395 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9396 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9397 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9398 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9399 +
9400 +/* dinfo.c */
9401 +void au_di_init_once(void *_di);
9402 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9403 +void au_di_free(struct au_dinfo *dinfo);
9404 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9405 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9406 +int au_di_init(struct dentry *dentry);
9407 +void au_di_fin(struct dentry *dentry);
9408 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9409 +
9410 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9411 +void di_read_unlock(struct dentry *d, int flags);
9412 +void di_downgrade_lock(struct dentry *d, int flags);
9413 +void di_write_lock(struct dentry *d, unsigned int lsc);
9414 +void di_write_unlock(struct dentry *d);
9415 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9416 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9417 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9418 +
9419 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9420 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9421 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9422 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9423 +
9424 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9425 +                  struct dentry *h_dentry);
9426 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9427 +int au_dbrange_test(struct dentry *dentry);
9428 +void au_update_digen(struct dentry *dentry);
9429 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9430 +void au_update_dbtop(struct dentry *dentry);
9431 +void au_update_dbbot(struct dentry *dentry);
9432 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9433 +
9434 +/* ---------------------------------------------------------------------- */
9435 +
9436 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9437 +{
9438 +       return dentry->d_fsdata;
9439 +}
9440 +
9441 +/* ---------------------------------------------------------------------- */
9442 +
9443 +/* lock subclass for dinfo */
9444 +enum {
9445 +       AuLsc_DI_CHILD,         /* child first */
9446 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9447 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9448 +       AuLsc_DI_PARENT,
9449 +       AuLsc_DI_PARENT2,
9450 +       AuLsc_DI_PARENT3,
9451 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9452 +};
9453 +
9454 +/*
9455 + * di_read_lock_child, di_write_lock_child,
9456 + * di_read_lock_child2, di_write_lock_child2,
9457 + * di_read_lock_child3, di_write_lock_child3,
9458 + * di_read_lock_parent, di_write_lock_parent,
9459 + * di_read_lock_parent2, di_write_lock_parent2,
9460 + * di_read_lock_parent3, di_write_lock_parent3,
9461 + */
9462 +#define AuReadLockFunc(name, lsc) \
9463 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9464 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9465 +
9466 +#define AuWriteLockFunc(name, lsc) \
9467 +static inline void di_write_lock_##name(struct dentry *d) \
9468 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9469 +
9470 +#define AuRWLockFuncs(name, lsc) \
9471 +       AuReadLockFunc(name, lsc) \
9472 +       AuWriteLockFunc(name, lsc)
9473 +
9474 +AuRWLockFuncs(child, CHILD);
9475 +AuRWLockFuncs(child2, CHILD2);
9476 +AuRWLockFuncs(child3, CHILD3);
9477 +AuRWLockFuncs(parent, PARENT);
9478 +AuRWLockFuncs(parent2, PARENT2);
9479 +AuRWLockFuncs(parent3, PARENT3);
9480 +
9481 +#undef AuReadLockFunc
9482 +#undef AuWriteLockFunc
9483 +#undef AuRWLockFuncs
9484 +
9485 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9486 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9487 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9488 +
9489 +/* ---------------------------------------------------------------------- */
9490 +
9491 +/* todo: memory barrier? */
9492 +static inline unsigned int au_digen(struct dentry *d)
9493 +{
9494 +       return atomic_read(&au_di(d)->di_generation);
9495 +}
9496 +
9497 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9498 +{
9499 +       hdentry->hd_dentry = NULL;
9500 +}
9501 +
9502 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9503 +                                           aufs_bindex_t bindex)
9504 +{
9505 +       return di->di_hdentry + bindex;
9506 +}
9507 +
9508 +static inline void au_hdput(struct au_hdentry *hd)
9509 +{
9510 +       if (hd)
9511 +               dput(hd->hd_dentry);
9512 +}
9513 +
9514 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9515 +{
9516 +       DiMustAnyLock(dentry);
9517 +       return au_di(dentry)->di_btop;
9518 +}
9519 +
9520 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9521 +{
9522 +       DiMustAnyLock(dentry);
9523 +       return au_di(dentry)->di_bbot;
9524 +}
9525 +
9526 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9527 +{
9528 +       DiMustAnyLock(dentry);
9529 +       return au_di(dentry)->di_bwh;
9530 +}
9531 +
9532 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9533 +{
9534 +       DiMustAnyLock(dentry);
9535 +       return au_di(dentry)->di_bdiropq;
9536 +}
9537 +
9538 +/* todo: hard/soft set? */
9539 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9540 +{
9541 +       DiMustWriteLock(dentry);
9542 +       au_di(dentry)->di_btop = bindex;
9543 +}
9544 +
9545 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9546 +{
9547 +       DiMustWriteLock(dentry);
9548 +       au_di(dentry)->di_bbot = bindex;
9549 +}
9550 +
9551 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9552 +{
9553 +       DiMustWriteLock(dentry);
9554 +       /* dbwh can be outside of btop - bbot range */
9555 +       au_di(dentry)->di_bwh = bindex;
9556 +}
9557 +
9558 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9559 +{
9560 +       DiMustWriteLock(dentry);
9561 +       au_di(dentry)->di_bdiropq = bindex;
9562 +}
9563 +
9564 +/* ---------------------------------------------------------------------- */
9565 +
9566 +#ifdef CONFIG_AUFS_HNOTIFY
9567 +static inline void au_digen_dec(struct dentry *d)
9568 +{
9569 +       atomic_dec(&au_di(d)->di_generation);
9570 +}
9571 +
9572 +static inline void au_hn_di_reinit(struct dentry *dentry)
9573 +{
9574 +       dentry->d_fsdata = NULL;
9575 +}
9576 +#else
9577 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9578 +#endif /* CONFIG_AUFS_HNOTIFY */
9579 +
9580 +#endif /* __KERNEL__ */
9581 +#endif /* __AUFS_DENTRY_H__ */
9582 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9583 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9584 +++ linux/fs/aufs/dinfo.c       2018-12-27 13:19:17.708416053 +0100
9585 @@ -0,0 +1,554 @@
9586 +// SPDX-License-Identifier: GPL-2.0
9587 +/*
9588 + * Copyright (C) 2005-2018 Junjiro R. Okajima
9589 + *
9590 + * This program, aufs is free software; you can redistribute it and/or modify
9591 + * it under the terms of the GNU General Public License as published by
9592 + * the Free Software Foundation; either version 2 of the License, or
9593 + * (at your option) any later version.
9594 + *
9595 + * This program is distributed in the hope that it will be useful,
9596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9598 + * GNU General Public License for more details.
9599 + *
9600 + * You should have received a copy of the GNU General Public License
9601 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9602 + */
9603 +
9604 +/*
9605 + * dentry private data
9606 + */
9607 +
9608 +#include "aufs.h"
9609 +
9610 +void au_di_init_once(void *_dinfo)
9611 +{
9612 +       struct au_dinfo *dinfo = _dinfo;
9613 +
9614 +       au_rw_init(&dinfo->di_rwsem);
9615 +}
9616 +
9617 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9618 +{
9619 +       struct au_dinfo *dinfo;
9620 +       int nbr, i;
9621 +
9622 +       dinfo = au_cache_alloc_dinfo();
9623 +       if (unlikely(!dinfo))
9624 +               goto out;
9625 +
9626 +       nbr = au_sbbot(sb) + 1;
9627 +       if (nbr <= 0)
9628 +               nbr = 1;
9629 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9630 +       if (dinfo->di_hdentry) {
9631 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9632 +               dinfo->di_btop = -1;
9633 +               dinfo->di_bbot = -1;
9634 +               dinfo->di_bwh = -1;
9635 +               dinfo->di_bdiropq = -1;
9636 +               dinfo->di_tmpfile = 0;
9637 +               for (i = 0; i < nbr; i++)
9638 +                       dinfo->di_hdentry[i].hd_id = -1;
9639 +               goto out;
9640 +       }
9641 +
9642 +       au_cache_free_dinfo(dinfo);
9643 +       dinfo = NULL;
9644 +
9645 +out:
9646 +       return dinfo;
9647 +}
9648 +
9649 +void au_di_free(struct au_dinfo *dinfo)
9650 +{
9651 +       struct au_hdentry *p;
9652 +       aufs_bindex_t bbot, bindex;
9653 +
9654 +       /* dentry may not be revalidated */
9655 +       bindex = dinfo->di_btop;
9656 +       if (bindex >= 0) {
9657 +               bbot = dinfo->di_bbot;
9658 +               p = au_hdentry(dinfo, bindex);
9659 +               while (bindex++ <= bbot)
9660 +                       au_hdput(p++);
9661 +       }
9662 +       au_kfree_try_rcu(dinfo->di_hdentry);
9663 +       au_cache_free_dinfo(dinfo);
9664 +}
9665 +
9666 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9667 +{
9668 +       struct au_hdentry *p;
9669 +       aufs_bindex_t bi;
9670 +
9671 +       AuRwMustWriteLock(&a->di_rwsem);
9672 +       AuRwMustWriteLock(&b->di_rwsem);
9673 +
9674 +#define DiSwap(v, name)                                \
9675 +       do {                                    \
9676 +               v = a->di_##name;               \
9677 +               a->di_##name = b->di_##name;    \
9678 +               b->di_##name = v;               \
9679 +       } while (0)
9680 +
9681 +       DiSwap(p, hdentry);
9682 +       DiSwap(bi, btop);
9683 +       DiSwap(bi, bbot);
9684 +       DiSwap(bi, bwh);
9685 +       DiSwap(bi, bdiropq);
9686 +       /* smp_mb(); */
9687 +
9688 +#undef DiSwap
9689 +}
9690 +
9691 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9692 +{
9693 +       AuRwMustWriteLock(&dst->di_rwsem);
9694 +       AuRwMustWriteLock(&src->di_rwsem);
9695 +
9696 +       dst->di_btop = src->di_btop;
9697 +       dst->di_bbot = src->di_bbot;
9698 +       dst->di_bwh = src->di_bwh;
9699 +       dst->di_bdiropq = src->di_bdiropq;
9700 +       /* smp_mb(); */
9701 +}
9702 +
9703 +int au_di_init(struct dentry *dentry)
9704 +{
9705 +       int err;
9706 +       struct super_block *sb;
9707 +       struct au_dinfo *dinfo;
9708 +
9709 +       err = 0;
9710 +       sb = dentry->d_sb;
9711 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9712 +       if (dinfo) {
9713 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9714 +               /* smp_mb(); */ /* atomic_set */
9715 +               dentry->d_fsdata = dinfo;
9716 +       } else
9717 +               err = -ENOMEM;
9718 +
9719 +       return err;
9720 +}
9721 +
9722 +void au_di_fin(struct dentry *dentry)
9723 +{
9724 +       struct au_dinfo *dinfo;
9725 +
9726 +       dinfo = au_di(dentry);
9727 +       AuRwDestroy(&dinfo->di_rwsem);
9728 +       au_di_free(dinfo);
9729 +}
9730 +
9731 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9732 +{
9733 +       int err, sz;
9734 +       struct au_hdentry *hdp;
9735 +
9736 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9737 +
9738 +       err = -ENOMEM;
9739 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9740 +       if (!sz)
9741 +               sz = sizeof(*hdp);
9742 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9743 +                          may_shrink);
9744 +       if (hdp) {
9745 +               dinfo->di_hdentry = hdp;
9746 +               err = 0;
9747 +       }
9748 +
9749 +       return err;
9750 +}
9751 +
9752 +/* ---------------------------------------------------------------------- */
9753 +
9754 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9755 +{
9756 +       switch (lsc) {
9757 +       case AuLsc_DI_CHILD:
9758 +               ii_write_lock_child(inode);
9759 +               break;
9760 +       case AuLsc_DI_CHILD2:
9761 +               ii_write_lock_child2(inode);
9762 +               break;
9763 +       case AuLsc_DI_CHILD3:
9764 +               ii_write_lock_child3(inode);
9765 +               break;
9766 +       case AuLsc_DI_PARENT:
9767 +               ii_write_lock_parent(inode);
9768 +               break;
9769 +       case AuLsc_DI_PARENT2:
9770 +               ii_write_lock_parent2(inode);
9771 +               break;
9772 +       case AuLsc_DI_PARENT3:
9773 +               ii_write_lock_parent3(inode);
9774 +               break;
9775 +       default:
9776 +               BUG();
9777 +       }
9778 +}
9779 +
9780 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9781 +{
9782 +       switch (lsc) {
9783 +       case AuLsc_DI_CHILD:
9784 +               ii_read_lock_child(inode);
9785 +               break;
9786 +       case AuLsc_DI_CHILD2:
9787 +               ii_read_lock_child2(inode);
9788 +               break;
9789 +       case AuLsc_DI_CHILD3:
9790 +               ii_read_lock_child3(inode);
9791 +               break;
9792 +       case AuLsc_DI_PARENT:
9793 +               ii_read_lock_parent(inode);
9794 +               break;
9795 +       case AuLsc_DI_PARENT2:
9796 +               ii_read_lock_parent2(inode);
9797 +               break;
9798 +       case AuLsc_DI_PARENT3:
9799 +               ii_read_lock_parent3(inode);
9800 +               break;
9801 +       default:
9802 +               BUG();
9803 +       }
9804 +}
9805 +
9806 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9807 +{
9808 +       struct inode *inode;
9809 +
9810 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9811 +       if (d_really_is_positive(d)) {
9812 +               inode = d_inode(d);
9813 +               if (au_ftest_lock(flags, IW))
9814 +                       do_ii_write_lock(inode, lsc);
9815 +               else if (au_ftest_lock(flags, IR))
9816 +                       do_ii_read_lock(inode, lsc);
9817 +       }
9818 +}
9819 +
9820 +void di_read_unlock(struct dentry *d, int flags)
9821 +{
9822 +       struct inode *inode;
9823 +
9824 +       if (d_really_is_positive(d)) {
9825 +               inode = d_inode(d);
9826 +               if (au_ftest_lock(flags, IW)) {
9827 +                       au_dbg_verify_dinode(d);
9828 +                       ii_write_unlock(inode);
9829 +               } else if (au_ftest_lock(flags, IR)) {
9830 +                       au_dbg_verify_dinode(d);
9831 +                       ii_read_unlock(inode);
9832 +               }
9833 +       }
9834 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9835 +}
9836 +
9837 +void di_downgrade_lock(struct dentry *d, int flags)
9838 +{
9839 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9840 +               ii_downgrade_lock(d_inode(d));
9841 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9842 +}
9843 +
9844 +void di_write_lock(struct dentry *d, unsigned int lsc)
9845 +{
9846 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9847 +       if (d_really_is_positive(d))
9848 +               do_ii_write_lock(d_inode(d), lsc);
9849 +}
9850 +
9851 +void di_write_unlock(struct dentry *d)
9852 +{
9853 +       au_dbg_verify_dinode(d);
9854 +       if (d_really_is_positive(d))
9855 +               ii_write_unlock(d_inode(d));
9856 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9857 +}
9858 +
9859 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9860 +{
9861 +       AuDebugOn(d1 == d2
9862 +                 || d_inode(d1) == d_inode(d2)
9863 +                 || d1->d_sb != d2->d_sb);
9864 +
9865 +       if ((isdir && au_test_subdir(d1, d2))
9866 +           || d1 < d2) {
9867 +               di_write_lock_child(d1);
9868 +               di_write_lock_child2(d2);
9869 +       } else {
9870 +               di_write_lock_child(d2);
9871 +               di_write_lock_child2(d1);
9872 +       }
9873 +}
9874 +
9875 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9876 +{
9877 +       AuDebugOn(d1 == d2
9878 +                 || d_inode(d1) == d_inode(d2)
9879 +                 || d1->d_sb != d2->d_sb);
9880 +
9881 +       if ((isdir && au_test_subdir(d1, d2))
9882 +           || d1 < d2) {
9883 +               di_write_lock_parent(d1);
9884 +               di_write_lock_parent2(d2);
9885 +       } else {
9886 +               di_write_lock_parent(d2);
9887 +               di_write_lock_parent2(d1);
9888 +       }
9889 +}
9890 +
9891 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9892 +{
9893 +       di_write_unlock(d1);
9894 +       if (d_inode(d1) == d_inode(d2))
9895 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9896 +       else
9897 +               di_write_unlock(d2);
9898 +}
9899 +
9900 +/* ---------------------------------------------------------------------- */
9901 +
9902 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9903 +{
9904 +       struct dentry *d;
9905 +
9906 +       DiMustAnyLock(dentry);
9907 +
9908 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9909 +               return NULL;
9910 +       AuDebugOn(bindex < 0);
9911 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9912 +       AuDebugOn(d && au_dcount(d) <= 0);
9913 +       return d;
9914 +}
9915 +
9916 +/*
9917 + * extended version of au_h_dptr().
9918 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9919 + * error.
9920 + */
9921 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9922 +{
9923 +       struct dentry *h_dentry;
9924 +       struct inode *inode, *h_inode;
9925 +
9926 +       AuDebugOn(d_really_is_negative(dentry));
9927 +
9928 +       h_dentry = NULL;
9929 +       if (au_dbtop(dentry) <= bindex
9930 +           && bindex <= au_dbbot(dentry))
9931 +               h_dentry = au_h_dptr(dentry, bindex);
9932 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9933 +               dget(h_dentry);
9934 +               goto out; /* success */
9935 +       }
9936 +
9937 +       inode = d_inode(dentry);
9938 +       AuDebugOn(bindex < au_ibtop(inode));
9939 +       AuDebugOn(au_ibbot(inode) < bindex);
9940 +       h_inode = au_h_iptr(inode, bindex);
9941 +       h_dentry = d_find_alias(h_inode);
9942 +       if (h_dentry) {
9943 +               if (!IS_ERR(h_dentry)) {
9944 +                       if (!au_d_linkable(h_dentry))
9945 +                               goto out; /* success */
9946 +                       dput(h_dentry);
9947 +               } else
9948 +                       goto out;
9949 +       }
9950 +
9951 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9952 +               h_dentry = au_plink_lkup(inode, bindex);
9953 +               AuDebugOn(!h_dentry);
9954 +               if (!IS_ERR(h_dentry)) {
9955 +                       if (!au_d_hashed_positive(h_dentry))
9956 +                               goto out; /* success */
9957 +                       dput(h_dentry);
9958 +                       h_dentry = NULL;
9959 +               }
9960 +       }
9961 +
9962 +out:
9963 +       AuDbgDentry(h_dentry);
9964 +       return h_dentry;
9965 +}
9966 +
9967 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9968 +{
9969 +       aufs_bindex_t bbot, bwh;
9970 +
9971 +       bbot = au_dbbot(dentry);
9972 +       if (0 <= bbot) {
9973 +               bwh = au_dbwh(dentry);
9974 +               if (!bwh)
9975 +                       return bwh;
9976 +               if (0 < bwh && bwh < bbot)
9977 +                       return bwh - 1;
9978 +       }
9979 +       return bbot;
9980 +}
9981 +
9982 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9983 +{
9984 +       aufs_bindex_t bbot, bopq;
9985 +
9986 +       bbot = au_dbtail(dentry);
9987 +       if (0 <= bbot) {
9988 +               bopq = au_dbdiropq(dentry);
9989 +               if (0 <= bopq && bopq < bbot)
9990 +                       bbot = bopq;
9991 +       }
9992 +       return bbot;
9993 +}
9994 +
9995 +/* ---------------------------------------------------------------------- */
9996 +
9997 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9998 +                  struct dentry *h_dentry)
9999 +{
10000 +       struct au_dinfo *dinfo;
10001 +       struct au_hdentry *hd;
10002 +       struct au_branch *br;
10003 +
10004 +       DiMustWriteLock(dentry);
10005 +
10006 +       dinfo = au_di(dentry);
10007 +       hd = au_hdentry(dinfo, bindex);
10008 +       au_hdput(hd);
10009 +       hd->hd_dentry = h_dentry;
10010 +       if (h_dentry) {
10011 +               br = au_sbr(dentry->d_sb, bindex);
10012 +               hd->hd_id = br->br_id;
10013 +       }
10014 +}
10015 +
10016 +int au_dbrange_test(struct dentry *dentry)
10017 +{
10018 +       int err;
10019 +       aufs_bindex_t btop, bbot;
10020 +
10021 +       err = 0;
10022 +       btop = au_dbtop(dentry);
10023 +       bbot = au_dbbot(dentry);
10024 +       if (btop >= 0)
10025 +               AuDebugOn(bbot < 0 && btop > bbot);
10026 +       else {
10027 +               err = -EIO;
10028 +               AuDebugOn(bbot >= 0);
10029 +       }
10030 +
10031 +       return err;
10032 +}
10033 +
10034 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
10035 +{
10036 +       int err;
10037 +
10038 +       err = 0;
10039 +       if (unlikely(au_digen(dentry) != sigen
10040 +                    || au_iigen_test(d_inode(dentry), sigen)))
10041 +               err = -EIO;
10042 +
10043 +       return err;
10044 +}
10045 +
10046 +void au_update_digen(struct dentry *dentry)
10047 +{
10048 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
10049 +       /* smp_mb(); */ /* atomic_set */
10050 +}
10051 +
10052 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
10053 +{
10054 +       struct au_dinfo *dinfo;
10055 +       struct dentry *h_d;
10056 +       struct au_hdentry *hdp;
10057 +       aufs_bindex_t bindex, bbot;
10058 +
10059 +       DiMustWriteLock(dentry);
10060 +
10061 +       dinfo = au_di(dentry);
10062 +       if (!dinfo || dinfo->di_btop < 0)
10063 +               return;
10064 +
10065 +       if (do_put_zero) {
10066 +               bbot = dinfo->di_bbot;
10067 +               bindex = dinfo->di_btop;
10068 +               hdp = au_hdentry(dinfo, bindex);
10069 +               for (; bindex <= bbot; bindex++, hdp++) {
10070 +                       h_d = hdp->hd_dentry;
10071 +                       if (h_d && d_is_negative(h_d))
10072 +                               au_set_h_dptr(dentry, bindex, NULL);
10073 +               }
10074 +       }
10075 +
10076 +       dinfo->di_btop = 0;
10077 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
10078 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
10079 +               if (hdp->hd_dentry)
10080 +                       break;
10081 +       if (dinfo->di_btop > dinfo->di_bbot) {
10082 +               dinfo->di_btop = -1;
10083 +               dinfo->di_bbot = -1;
10084 +               return;
10085 +       }
10086 +
10087 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
10088 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
10089 +               if (hdp->hd_dentry)
10090 +                       break;
10091 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
10092 +}
10093 +
10094 +void au_update_dbtop(struct dentry *dentry)
10095 +{
10096 +       aufs_bindex_t bindex, bbot;
10097 +       struct dentry *h_dentry;
10098 +
10099 +       bbot = au_dbbot(dentry);
10100 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10101 +               h_dentry = au_h_dptr(dentry, bindex);
10102 +               if (!h_dentry)
10103 +                       continue;
10104 +               if (d_is_positive(h_dentry)) {
10105 +                       au_set_dbtop(dentry, bindex);
10106 +                       return;
10107 +               }
10108 +               au_set_h_dptr(dentry, bindex, NULL);
10109 +       }
10110 +}
10111 +
10112 +void au_update_dbbot(struct dentry *dentry)
10113 +{
10114 +       aufs_bindex_t bindex, btop;
10115 +       struct dentry *h_dentry;
10116 +
10117 +       btop = au_dbtop(dentry);
10118 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10119 +               h_dentry = au_h_dptr(dentry, bindex);
10120 +               if (!h_dentry)
10121 +                       continue;
10122 +               if (d_is_positive(h_dentry)) {
10123 +                       au_set_dbbot(dentry, bindex);
10124 +                       return;
10125 +               }
10126 +               au_set_h_dptr(dentry, bindex, NULL);
10127 +       }
10128 +}
10129 +
10130 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10131 +{
10132 +       aufs_bindex_t bindex, bbot;
10133 +
10134 +       bbot = au_dbbot(dentry);
10135 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10136 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10137 +                       return bindex;
10138 +       return -1;
10139 +}
10140 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10141 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10142 +++ linux/fs/aufs/dir.c 2018-12-27 13:19:17.708416053 +0100
10143 @@ -0,0 +1,762 @@
10144 +// SPDX-License-Identifier: GPL-2.0
10145 +/*
10146 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10147 + *
10148 + * This program, aufs is free software; you can redistribute it and/or modify
10149 + * it under the terms of the GNU General Public License as published by
10150 + * the Free Software Foundation; either version 2 of the License, or
10151 + * (at your option) any later version.
10152 + *
10153 + * This program is distributed in the hope that it will be useful,
10154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10156 + * GNU General Public License for more details.
10157 + *
10158 + * You should have received a copy of the GNU General Public License
10159 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10160 + */
10161 +
10162 +/*
10163 + * directory operations
10164 + */
10165 +
10166 +#include <linux/fs_stack.h>
10167 +#include "aufs.h"
10168 +
10169 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10170 +{
10171 +       unsigned int nlink;
10172 +
10173 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10174 +
10175 +       nlink = dir->i_nlink;
10176 +       nlink += h_dir->i_nlink - 2;
10177 +       if (h_dir->i_nlink < 2)
10178 +               nlink += 2;
10179 +       smp_mb(); /* for i_nlink */
10180 +       /* 0 can happen in revaliding */
10181 +       set_nlink(dir, nlink);
10182 +}
10183 +
10184 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10185 +{
10186 +       unsigned int nlink;
10187 +
10188 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10189 +
10190 +       nlink = dir->i_nlink;
10191 +       nlink -= h_dir->i_nlink - 2;
10192 +       if (h_dir->i_nlink < 2)
10193 +               nlink -= 2;
10194 +       smp_mb(); /* for i_nlink */
10195 +       /* nlink == 0 means the branch-fs is broken */
10196 +       set_nlink(dir, nlink);
10197 +}
10198 +
10199 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10200 +{
10201 +       loff_t sz;
10202 +       aufs_bindex_t bindex, bbot;
10203 +       struct file *h_file;
10204 +       struct dentry *h_dentry;
10205 +
10206 +       sz = 0;
10207 +       if (file) {
10208 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10209 +
10210 +               bbot = au_fbbot_dir(file);
10211 +               for (bindex = au_fbtop(file);
10212 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10213 +                    bindex++) {
10214 +                       h_file = au_hf_dir(file, bindex);
10215 +                       if (h_file && file_inode(h_file))
10216 +                               sz += vfsub_f_size_read(h_file);
10217 +               }
10218 +       } else {
10219 +               AuDebugOn(!dentry);
10220 +               AuDebugOn(!d_is_dir(dentry));
10221 +
10222 +               bbot = au_dbtaildir(dentry);
10223 +               for (bindex = au_dbtop(dentry);
10224 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10225 +                    bindex++) {
10226 +                       h_dentry = au_h_dptr(dentry, bindex);
10227 +                       if (h_dentry && d_is_positive(h_dentry))
10228 +                               sz += i_size_read(d_inode(h_dentry));
10229 +               }
10230 +       }
10231 +       if (sz < KMALLOC_MAX_SIZE)
10232 +               sz = roundup_pow_of_two(sz);
10233 +       if (sz > KMALLOC_MAX_SIZE)
10234 +               sz = KMALLOC_MAX_SIZE;
10235 +       else if (sz < NAME_MAX) {
10236 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10237 +               sz = AUFS_RDBLK_DEF;
10238 +       }
10239 +       return sz;
10240 +}
10241 +
10242 +struct au_dir_ts_arg {
10243 +       struct dentry *dentry;
10244 +       aufs_bindex_t brid;
10245 +};
10246 +
10247 +static void au_do_dir_ts(void *arg)
10248 +{
10249 +       struct au_dir_ts_arg *a = arg;
10250 +       struct au_dtime dt;
10251 +       struct path h_path;
10252 +       struct inode *dir, *h_dir;
10253 +       struct super_block *sb;
10254 +       struct au_branch *br;
10255 +       struct au_hinode *hdir;
10256 +       int err;
10257 +       aufs_bindex_t btop, bindex;
10258 +
10259 +       sb = a->dentry->d_sb;
10260 +       if (d_really_is_negative(a->dentry))
10261 +               goto out;
10262 +       /* no dir->i_mutex lock */
10263 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10264 +
10265 +       dir = d_inode(a->dentry);
10266 +       btop = au_ibtop(dir);
10267 +       bindex = au_br_index(sb, a->brid);
10268 +       if (bindex < btop)
10269 +               goto out_unlock;
10270 +
10271 +       br = au_sbr(sb, bindex);
10272 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10273 +       if (!h_path.dentry)
10274 +               goto out_unlock;
10275 +       h_path.mnt = au_br_mnt(br);
10276 +       au_dtime_store(&dt, a->dentry, &h_path);
10277 +
10278 +       br = au_sbr(sb, btop);
10279 +       if (!au_br_writable(br->br_perm))
10280 +               goto out_unlock;
10281 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10282 +       h_path.mnt = au_br_mnt(br);
10283 +       err = vfsub_mnt_want_write(h_path.mnt);
10284 +       if (err)
10285 +               goto out_unlock;
10286 +       hdir = au_hi(dir, btop);
10287 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10288 +       h_dir = au_h_iptr(dir, btop);
10289 +       if (h_dir->i_nlink
10290 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10291 +               dt.dt_h_path = h_path;
10292 +               au_dtime_revert(&dt);
10293 +       }
10294 +       au_hn_inode_unlock(hdir);
10295 +       vfsub_mnt_drop_write(h_path.mnt);
10296 +       au_cpup_attr_timesizes(dir);
10297 +
10298 +out_unlock:
10299 +       aufs_read_unlock(a->dentry, AuLock_DW);
10300 +out:
10301 +       dput(a->dentry);
10302 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10303 +       au_kfree_try_rcu(arg);
10304 +}
10305 +
10306 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10307 +{
10308 +       int perm, wkq_err;
10309 +       aufs_bindex_t btop;
10310 +       struct au_dir_ts_arg *arg;
10311 +       struct dentry *dentry;
10312 +       struct super_block *sb;
10313 +
10314 +       IMustLock(dir);
10315 +
10316 +       dentry = d_find_any_alias(dir);
10317 +       AuDebugOn(!dentry);
10318 +       sb = dentry->d_sb;
10319 +       btop = au_ibtop(dir);
10320 +       if (btop == bindex) {
10321 +               au_cpup_attr_timesizes(dir);
10322 +               goto out;
10323 +       }
10324 +
10325 +       perm = au_sbr_perm(sb, btop);
10326 +       if (!au_br_writable(perm))
10327 +               goto out;
10328 +
10329 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10330 +       if (!arg)
10331 +               goto out;
10332 +
10333 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10334 +       arg->brid = au_sbr_id(sb, bindex);
10335 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10336 +       if (unlikely(wkq_err)) {
10337 +               pr_err("wkq %d\n", wkq_err);
10338 +               dput(dentry);
10339 +               au_kfree_try_rcu(arg);
10340 +       }
10341 +
10342 +out:
10343 +       dput(dentry);
10344 +}
10345 +
10346 +/* ---------------------------------------------------------------------- */
10347 +
10348 +static int reopen_dir(struct file *file)
10349 +{
10350 +       int err;
10351 +       unsigned int flags;
10352 +       aufs_bindex_t bindex, btail, btop;
10353 +       struct dentry *dentry, *h_dentry;
10354 +       struct file *h_file;
10355 +
10356 +       /* open all lower dirs */
10357 +       dentry = file->f_path.dentry;
10358 +       btop = au_dbtop(dentry);
10359 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10360 +               au_set_h_fptr(file, bindex, NULL);
10361 +       au_set_fbtop(file, btop);
10362 +
10363 +       btail = au_dbtaildir(dentry);
10364 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10365 +               au_set_h_fptr(file, bindex, NULL);
10366 +       au_set_fbbot_dir(file, btail);
10367 +
10368 +       flags = vfsub_file_flags(file);
10369 +       for (bindex = btop; bindex <= btail; bindex++) {
10370 +               h_dentry = au_h_dptr(dentry, bindex);
10371 +               if (!h_dentry)
10372 +                       continue;
10373 +               h_file = au_hf_dir(file, bindex);
10374 +               if (h_file)
10375 +                       continue;
10376 +
10377 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10378 +               err = PTR_ERR(h_file);
10379 +               if (IS_ERR(h_file))
10380 +                       goto out; /* close all? */
10381 +               au_set_h_fptr(file, bindex, h_file);
10382 +       }
10383 +       au_update_figen(file);
10384 +       /* todo: necessary? */
10385 +       /* file->f_ra = h_file->f_ra; */
10386 +       err = 0;
10387 +
10388 +out:
10389 +       return err;
10390 +}
10391 +
10392 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10393 +{
10394 +       int err;
10395 +       aufs_bindex_t bindex, btail;
10396 +       struct dentry *dentry, *h_dentry;
10397 +       struct vfsmount *mnt;
10398 +
10399 +       FiMustWriteLock(file);
10400 +       AuDebugOn(h_file);
10401 +
10402 +       err = 0;
10403 +       mnt = file->f_path.mnt;
10404 +       dentry = file->f_path.dentry;
10405 +       file->f_version = inode_query_iversion(d_inode(dentry));
10406 +       bindex = au_dbtop(dentry);
10407 +       au_set_fbtop(file, bindex);
10408 +       btail = au_dbtaildir(dentry);
10409 +       au_set_fbbot_dir(file, btail);
10410 +       for (; !err && bindex <= btail; bindex++) {
10411 +               h_dentry = au_h_dptr(dentry, bindex);
10412 +               if (!h_dentry)
10413 +                       continue;
10414 +
10415 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10416 +               if (unlikely(err))
10417 +                       break;
10418 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10419 +               if (IS_ERR(h_file)) {
10420 +                       err = PTR_ERR(h_file);
10421 +                       break;
10422 +               }
10423 +               au_set_h_fptr(file, bindex, h_file);
10424 +       }
10425 +       au_update_figen(file);
10426 +       /* todo: necessary? */
10427 +       /* file->f_ra = h_file->f_ra; */
10428 +       if (!err)
10429 +               return 0; /* success */
10430 +
10431 +       /* close all */
10432 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10433 +               au_set_h_fptr(file, bindex, NULL);
10434 +       au_set_fbtop(file, -1);
10435 +       au_set_fbbot_dir(file, -1);
10436 +
10437 +       return err;
10438 +}
10439 +
10440 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10441 +                        struct file *file)
10442 +{
10443 +       int err;
10444 +       struct super_block *sb;
10445 +       struct au_fidir *fidir;
10446 +
10447 +       err = -ENOMEM;
10448 +       sb = file->f_path.dentry->d_sb;
10449 +       si_read_lock(sb, AuLock_FLUSH);
10450 +       fidir = au_fidir_alloc(sb);
10451 +       if (fidir) {
10452 +               struct au_do_open_args args = {
10453 +                       .open   = do_open_dir,
10454 +                       .fidir  = fidir
10455 +               };
10456 +               err = au_do_open(file, &args);
10457 +               if (unlikely(err))
10458 +                       au_kfree_rcu(fidir);
10459 +       }
10460 +       si_read_unlock(sb);
10461 +       return err;
10462 +}
10463 +
10464 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10465 +                           struct file *file)
10466 +{
10467 +       struct au_vdir *vdir_cache;
10468 +       struct au_finfo *finfo;
10469 +       struct au_fidir *fidir;
10470 +       struct au_hfile *hf;
10471 +       aufs_bindex_t bindex, bbot;
10472 +
10473 +       finfo = au_fi(file);
10474 +       fidir = finfo->fi_hdir;
10475 +       if (fidir) {
10476 +               au_hbl_del(&finfo->fi_hlist,
10477 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10478 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10479 +               if (vdir_cache)
10480 +                       au_vdir_free(vdir_cache);
10481 +
10482 +               bindex = finfo->fi_btop;
10483 +               if (bindex >= 0) {
10484 +                       hf = fidir->fd_hfile + bindex;
10485 +                       /*
10486 +                        * calls fput() instead of filp_close(),
10487 +                        * since no dnotify or lock for the lower file.
10488 +                        */
10489 +                       bbot = fidir->fd_bbot;
10490 +                       for (; bindex <= bbot; bindex++, hf++)
10491 +                               if (hf->hf_file)
10492 +                                       au_hfput(hf, /*execed*/0);
10493 +               }
10494 +               au_kfree_rcu(fidir);
10495 +               finfo->fi_hdir = NULL;
10496 +       }
10497 +       au_finfo_fin(file);
10498 +       return 0;
10499 +}
10500 +
10501 +/* ---------------------------------------------------------------------- */
10502 +
10503 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10504 +{
10505 +       int err;
10506 +       aufs_bindex_t bindex, bbot;
10507 +       struct file *h_file;
10508 +
10509 +       err = 0;
10510 +       bbot = au_fbbot_dir(file);
10511 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10512 +               h_file = au_hf_dir(file, bindex);
10513 +               if (h_file)
10514 +                       err = vfsub_flush(h_file, id);
10515 +       }
10516 +       return err;
10517 +}
10518 +
10519 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10520 +{
10521 +       return au_do_flush(file, id, au_do_flush_dir);
10522 +}
10523 +
10524 +/* ---------------------------------------------------------------------- */
10525 +
10526 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10527 +{
10528 +       int err;
10529 +       aufs_bindex_t bbot, bindex;
10530 +       struct inode *inode;
10531 +       struct super_block *sb;
10532 +
10533 +       err = 0;
10534 +       sb = dentry->d_sb;
10535 +       inode = d_inode(dentry);
10536 +       IMustLock(inode);
10537 +       bbot = au_dbbot(dentry);
10538 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10539 +               struct path h_path;
10540 +
10541 +               if (au_test_ro(sb, bindex, inode))
10542 +                       continue;
10543 +               h_path.dentry = au_h_dptr(dentry, bindex);
10544 +               if (!h_path.dentry)
10545 +                       continue;
10546 +
10547 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10548 +               err = vfsub_fsync(NULL, &h_path, datasync);
10549 +       }
10550 +
10551 +       return err;
10552 +}
10553 +
10554 +static int au_do_fsync_dir(struct file *file, int datasync)
10555 +{
10556 +       int err;
10557 +       aufs_bindex_t bbot, bindex;
10558 +       struct file *h_file;
10559 +       struct super_block *sb;
10560 +       struct inode *inode;
10561 +
10562 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10563 +       if (unlikely(err))
10564 +               goto out;
10565 +
10566 +       inode = file_inode(file);
10567 +       sb = inode->i_sb;
10568 +       bbot = au_fbbot_dir(file);
10569 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10570 +               h_file = au_hf_dir(file, bindex);
10571 +               if (!h_file || au_test_ro(sb, bindex, inode))
10572 +                       continue;
10573 +
10574 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10575 +       }
10576 +
10577 +out:
10578 +       return err;
10579 +}
10580 +
10581 +/*
10582 + * @file may be NULL
10583 + */
10584 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10585 +                         int datasync)
10586 +{
10587 +       int err;
10588 +       struct dentry *dentry;
10589 +       struct inode *inode;
10590 +       struct super_block *sb;
10591 +
10592 +       err = 0;
10593 +       dentry = file->f_path.dentry;
10594 +       inode = d_inode(dentry);
10595 +       inode_lock(inode);
10596 +       sb = dentry->d_sb;
10597 +       si_noflush_read_lock(sb);
10598 +       if (file)
10599 +               err = au_do_fsync_dir(file, datasync);
10600 +       else {
10601 +               di_write_lock_child(dentry);
10602 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10603 +       }
10604 +       au_cpup_attr_timesizes(inode);
10605 +       di_write_unlock(dentry);
10606 +       if (file)
10607 +               fi_write_unlock(file);
10608 +
10609 +       si_read_unlock(sb);
10610 +       inode_unlock(inode);
10611 +       return err;
10612 +}
10613 +
10614 +/* ---------------------------------------------------------------------- */
10615 +
10616 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10617 +{
10618 +       int err;
10619 +       struct dentry *dentry;
10620 +       struct inode *inode, *h_inode;
10621 +       struct super_block *sb;
10622 +
10623 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10624 +
10625 +       dentry = file->f_path.dentry;
10626 +       inode = d_inode(dentry);
10627 +       IMustLock(inode);
10628 +
10629 +       sb = dentry->d_sb;
10630 +       si_read_lock(sb, AuLock_FLUSH);
10631 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10632 +       if (unlikely(err))
10633 +               goto out;
10634 +       err = au_alive_dir(dentry);
10635 +       if (!err)
10636 +               err = au_vdir_init(file);
10637 +       di_downgrade_lock(dentry, AuLock_IR);
10638 +       if (unlikely(err))
10639 +               goto out_unlock;
10640 +
10641 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10642 +       if (!au_test_nfsd()) {
10643 +               err = au_vdir_fill_de(file, ctx);
10644 +               fsstack_copy_attr_atime(inode, h_inode);
10645 +       } else {
10646 +               /*
10647 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10648 +                * encode_fh() and others.
10649 +                */
10650 +               atomic_inc(&h_inode->i_count);
10651 +               di_read_unlock(dentry, AuLock_IR);
10652 +               si_read_unlock(sb);
10653 +               err = au_vdir_fill_de(file, ctx);
10654 +               fsstack_copy_attr_atime(inode, h_inode);
10655 +               fi_write_unlock(file);
10656 +               iput(h_inode);
10657 +
10658 +               AuTraceErr(err);
10659 +               return err;
10660 +       }
10661 +
10662 +out_unlock:
10663 +       di_read_unlock(dentry, AuLock_IR);
10664 +       fi_write_unlock(file);
10665 +out:
10666 +       si_read_unlock(sb);
10667 +       return err;
10668 +}
10669 +
10670 +/* ---------------------------------------------------------------------- */
10671 +
10672 +#define AuTestEmpty_WHONLY     1
10673 +#define AuTestEmpty_CALLED     (1 << 1)
10674 +#define AuTestEmpty_SHWH       (1 << 2)
10675 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10676 +#define au_fset_testempty(flags, name) \
10677 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10678 +#define au_fclr_testempty(flags, name) \
10679 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10680 +
10681 +#ifndef CONFIG_AUFS_SHWH
10682 +#undef AuTestEmpty_SHWH
10683 +#define AuTestEmpty_SHWH       0
10684 +#endif
10685 +
10686 +struct test_empty_arg {
10687 +       struct dir_context ctx;
10688 +       struct au_nhash *whlist;
10689 +       unsigned int flags;
10690 +       int err;
10691 +       aufs_bindex_t bindex;
10692 +};
10693 +
10694 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10695 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10696 +                        unsigned int d_type)
10697 +{
10698 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10699 +                                                 ctx);
10700 +       char *name = (void *)__name;
10701 +
10702 +       arg->err = 0;
10703 +       au_fset_testempty(arg->flags, CALLED);
10704 +       /* smp_mb(); */
10705 +       if (name[0] == '.'
10706 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10707 +               goto out; /* success */
10708 +
10709 +       if (namelen <= AUFS_WH_PFX_LEN
10710 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10711 +               if (au_ftest_testempty(arg->flags, WHONLY)
10712 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10713 +                       arg->err = -ENOTEMPTY;
10714 +               goto out;
10715 +       }
10716 +
10717 +       name += AUFS_WH_PFX_LEN;
10718 +       namelen -= AUFS_WH_PFX_LEN;
10719 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10720 +               arg->err = au_nhash_append_wh
10721 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10722 +                        au_ftest_testempty(arg->flags, SHWH));
10723 +
10724 +out:
10725 +       /* smp_mb(); */
10726 +       AuTraceErr(arg->err);
10727 +       return arg->err;
10728 +}
10729 +
10730 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10731 +{
10732 +       int err;
10733 +       struct file *h_file;
10734 +       struct au_branch *br;
10735 +
10736 +       h_file = au_h_open(dentry, arg->bindex,
10737 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10738 +                          /*file*/NULL, /*force_wr*/0);
10739 +       err = PTR_ERR(h_file);
10740 +       if (IS_ERR(h_file))
10741 +               goto out;
10742 +
10743 +       err = 0;
10744 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10745 +           && !file_inode(h_file)->i_nlink)
10746 +               goto out_put;
10747 +
10748 +       do {
10749 +               arg->err = 0;
10750 +               au_fclr_testempty(arg->flags, CALLED);
10751 +               /* smp_mb(); */
10752 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10753 +               if (err >= 0)
10754 +                       err = arg->err;
10755 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10756 +
10757 +out_put:
10758 +       fput(h_file);
10759 +       br = au_sbr(dentry->d_sb, arg->bindex);
10760 +       au_lcnt_dec(&br->br_nfiles);
10761 +out:
10762 +       return err;
10763 +}
10764 +
10765 +struct do_test_empty_args {
10766 +       int *errp;
10767 +       struct dentry *dentry;
10768 +       struct test_empty_arg *arg;
10769 +};
10770 +
10771 +static void call_do_test_empty(void *args)
10772 +{
10773 +       struct do_test_empty_args *a = args;
10774 +       *a->errp = do_test_empty(a->dentry, a->arg);
10775 +}
10776 +
10777 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10778 +{
10779 +       int err, wkq_err;
10780 +       struct dentry *h_dentry;
10781 +       struct inode *h_inode;
10782 +
10783 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10784 +       h_inode = d_inode(h_dentry);
10785 +       /* todo: i_mode changes anytime? */
10786 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10787 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10788 +       inode_unlock_shared(h_inode);
10789 +       if (!err)
10790 +               err = do_test_empty(dentry, arg);
10791 +       else {
10792 +               struct do_test_empty_args args = {
10793 +                       .errp   = &err,
10794 +                       .dentry = dentry,
10795 +                       .arg    = arg
10796 +               };
10797 +               unsigned int flags = arg->flags;
10798 +
10799 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10800 +               if (unlikely(wkq_err))
10801 +                       err = wkq_err;
10802 +               arg->flags = flags;
10803 +       }
10804 +
10805 +       return err;
10806 +}
10807 +
10808 +int au_test_empty_lower(struct dentry *dentry)
10809 +{
10810 +       int err;
10811 +       unsigned int rdhash;
10812 +       aufs_bindex_t bindex, btop, btail;
10813 +       struct au_nhash whlist;
10814 +       struct test_empty_arg arg = {
10815 +               .ctx = {
10816 +                       .actor = test_empty_cb
10817 +               }
10818 +       };
10819 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10820 +
10821 +       SiMustAnyLock(dentry->d_sb);
10822 +
10823 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10824 +       if (!rdhash)
10825 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10826 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10827 +       if (unlikely(err))
10828 +               goto out;
10829 +
10830 +       arg.flags = 0;
10831 +       arg.whlist = &whlist;
10832 +       btop = au_dbtop(dentry);
10833 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10834 +               au_fset_testempty(arg.flags, SHWH);
10835 +       test_empty = do_test_empty;
10836 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10837 +               test_empty = sio_test_empty;
10838 +       arg.bindex = btop;
10839 +       err = test_empty(dentry, &arg);
10840 +       if (unlikely(err))
10841 +               goto out_whlist;
10842 +
10843 +       au_fset_testempty(arg.flags, WHONLY);
10844 +       btail = au_dbtaildir(dentry);
10845 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10846 +               struct dentry *h_dentry;
10847 +
10848 +               h_dentry = au_h_dptr(dentry, bindex);
10849 +               if (h_dentry && d_is_positive(h_dentry)) {
10850 +                       arg.bindex = bindex;
10851 +                       err = test_empty(dentry, &arg);
10852 +               }
10853 +       }
10854 +
10855 +out_whlist:
10856 +       au_nhash_wh_free(&whlist);
10857 +out:
10858 +       return err;
10859 +}
10860 +
10861 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10862 +{
10863 +       int err;
10864 +       struct test_empty_arg arg = {
10865 +               .ctx = {
10866 +                       .actor = test_empty_cb
10867 +               }
10868 +       };
10869 +       aufs_bindex_t bindex, btail;
10870 +
10871 +       err = 0;
10872 +       arg.whlist = whlist;
10873 +       arg.flags = AuTestEmpty_WHONLY;
10874 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10875 +               au_fset_testempty(arg.flags, SHWH);
10876 +       btail = au_dbtaildir(dentry);
10877 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10878 +               struct dentry *h_dentry;
10879 +
10880 +               h_dentry = au_h_dptr(dentry, bindex);
10881 +               if (h_dentry && d_is_positive(h_dentry)) {
10882 +                       arg.bindex = bindex;
10883 +                       err = sio_test_empty(dentry, &arg);
10884 +               }
10885 +       }
10886 +
10887 +       return err;
10888 +}
10889 +
10890 +/* ---------------------------------------------------------------------- */
10891 +
10892 +const struct file_operations aufs_dir_fop = {
10893 +       .owner          = THIS_MODULE,
10894 +       .llseek         = default_llseek,
10895 +       .read           = generic_read_dir,
10896 +       .iterate_shared = aufs_iterate_shared,
10897 +       .unlocked_ioctl = aufs_ioctl_dir,
10898 +#ifdef CONFIG_COMPAT
10899 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10900 +#endif
10901 +       .open           = aufs_open_dir,
10902 +       .release        = aufs_release_dir,
10903 +       .flush          = aufs_flush_dir,
10904 +       .fsync          = aufs_fsync_dir
10905 +};
10906 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10907 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10908 +++ linux/fs/aufs/dir.h 2018-12-27 13:19:17.708416053 +0100
10909 @@ -0,0 +1,134 @@
10910 +/* SPDX-License-Identifier: GPL-2.0 */
10911 +/*
10912 + * Copyright (C) 2005-2018 Junjiro R. Okajima
10913 + *
10914 + * This program, aufs is free software; you can redistribute it and/or modify
10915 + * it under the terms of the GNU General Public License as published by
10916 + * the Free Software Foundation; either version 2 of the License, or
10917 + * (at your option) any later version.
10918 + *
10919 + * This program is distributed in the hope that it will be useful,
10920 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10921 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10922 + * GNU General Public License for more details.
10923 + *
10924 + * You should have received a copy of the GNU General Public License
10925 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10926 + */
10927 +
10928 +/*
10929 + * directory operations
10930 + */
10931 +
10932 +#ifndef __AUFS_DIR_H__
10933 +#define __AUFS_DIR_H__
10934 +
10935 +#ifdef __KERNEL__
10936 +
10937 +#include <linux/fs.h>
10938 +
10939 +/* ---------------------------------------------------------------------- */
10940 +
10941 +/* need to be faster and smaller */
10942 +
10943 +struct au_nhash {
10944 +       unsigned int            nh_num;
10945 +       struct hlist_head       *nh_head;
10946 +};
10947 +
10948 +struct au_vdir_destr {
10949 +       unsigned char   len;
10950 +       unsigned char   name[0];
10951 +} __packed;
10952 +
10953 +struct au_vdir_dehstr {
10954 +       struct hlist_node       hash;
10955 +       struct au_vdir_destr    *str;
10956 +       struct rcu_head         rcu;
10957 +} ____cacheline_aligned_in_smp;
10958 +
10959 +struct au_vdir_de {
10960 +       ino_t                   de_ino;
10961 +       unsigned char           de_type;
10962 +       /* caution: packed */
10963 +       struct au_vdir_destr    de_str;
10964 +} __packed;
10965 +
10966 +struct au_vdir_wh {
10967 +       struct hlist_node       wh_hash;
10968 +#ifdef CONFIG_AUFS_SHWH
10969 +       ino_t                   wh_ino;
10970 +       aufs_bindex_t           wh_bindex;
10971 +       unsigned char           wh_type;
10972 +#else
10973 +       aufs_bindex_t           wh_bindex;
10974 +#endif
10975 +       /* caution: packed */
10976 +       struct au_vdir_destr    wh_str;
10977 +} __packed;
10978 +
10979 +union au_vdir_deblk_p {
10980 +       unsigned char           *deblk;
10981 +       struct au_vdir_de       *de;
10982 +};
10983 +
10984 +struct au_vdir {
10985 +       unsigned char   **vd_deblk;
10986 +       unsigned long   vd_nblk;
10987 +       struct {
10988 +               unsigned long           ul;
10989 +               union au_vdir_deblk_p   p;
10990 +       } vd_last;
10991 +
10992 +       u64             vd_version;
10993 +       unsigned int    vd_deblk_sz;
10994 +       unsigned long   vd_jiffy;
10995 +       struct rcu_head rcu;
10996 +} ____cacheline_aligned_in_smp;
10997 +
10998 +/* ---------------------------------------------------------------------- */
10999 +
11000 +/* dir.c */
11001 +extern const struct file_operations aufs_dir_fop;
11002 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
11003 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
11004 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
11005 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
11006 +int au_test_empty_lower(struct dentry *dentry);
11007 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
11008 +
11009 +/* vdir.c */
11010 +unsigned int au_rdhash_est(loff_t sz);
11011 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
11012 +void au_nhash_wh_free(struct au_nhash *whlist);
11013 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
11014 +                           int limit);
11015 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
11016 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
11017 +                      unsigned int d_type, aufs_bindex_t bindex,
11018 +                      unsigned char shwh);
11019 +void au_vdir_free(struct au_vdir *vdir);
11020 +int au_vdir_init(struct file *file);
11021 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
11022 +
11023 +/* ioctl.c */
11024 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
11025 +
11026 +#ifdef CONFIG_AUFS_RDU
11027 +/* rdu.c */
11028 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
11029 +#ifdef CONFIG_COMPAT
11030 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
11031 +                        unsigned long arg);
11032 +#endif
11033 +#else
11034 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
11035 +       unsigned int cmd, unsigned long arg)
11036 +#ifdef CONFIG_COMPAT
11037 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
11038 +       unsigned int cmd, unsigned long arg)
11039 +#endif
11040 +#endif
11041 +
11042 +#endif /* __KERNEL__ */
11043 +#endif /* __AUFS_DIR_H__ */
11044 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
11045 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
11046 +++ linux/fs/aufs/dirren.c      2018-12-27 13:19:17.708416053 +0100
11047 @@ -0,0 +1,1316 @@
11048 +// SPDX-License-Identifier: GPL-2.0
11049 +/*
11050 + * Copyright (C) 2017-2018 Junjiro R. Okajima
11051 + *
11052 + * This program, aufs is free software; you can redistribute it and/or modify
11053 + * it under the terms of the GNU General Public License as published by
11054 + * the Free Software Foundation; either version 2 of the License, or
11055 + * (at your option) any later version.
11056 + *
11057 + * This program is distributed in the hope that it will be useful,
11058 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11059 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11060 + * GNU General Public License for more details.
11061 + *
11062 + * You should have received a copy of the GNU General Public License
11063 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11064 + */
11065 +
11066 +/*
11067 + * special handling in renaming a directory
11068 + * in order to support looking-up the before-renamed name on the lower readonly
11069 + * branches
11070 + */
11071 +
11072 +#include <linux/byteorder/generic.h>
11073 +#include "aufs.h"
11074 +
11075 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
11076 +{
11077 +       int idx;
11078 +
11079 +       idx = au_dr_ihash(ent->dr_h_ino);
11080 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
11081 +}
11082 +
11083 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
11084 +{
11085 +       int ret, i;
11086 +       struct hlist_bl_head *hbl;
11087 +
11088 +       ret = 1;
11089 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
11090 +               hbl = dr->dr_h_ino + i;
11091 +               hlist_bl_lock(hbl);
11092 +               ret &= hlist_bl_empty(hbl);
11093 +               hlist_bl_unlock(hbl);
11094 +       }
11095 +
11096 +       return ret;
11097 +}
11098 +
11099 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11100 +{
11101 +       struct au_dr_hino *found, *ent;
11102 +       struct hlist_bl_head *hbl;
11103 +       struct hlist_bl_node *pos;
11104 +       int idx;
11105 +
11106 +       found = NULL;
11107 +       idx = au_dr_ihash(ino);
11108 +       hbl = dr->dr_h_ino + idx;
11109 +       hlist_bl_lock(hbl);
11110 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11111 +               if (ent->dr_h_ino == ino) {
11112 +                       found = ent;
11113 +                       break;
11114 +               }
11115 +       hlist_bl_unlock(hbl);
11116 +
11117 +       return found;
11118 +}
11119 +
11120 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11121 +                       struct au_dr_hino *add_ent)
11122 +{
11123 +       int found, idx;
11124 +       struct hlist_bl_head *hbl;
11125 +       struct hlist_bl_node *pos;
11126 +       struct au_dr_hino *ent;
11127 +
11128 +       found = 0;
11129 +       idx = au_dr_ihash(ino);
11130 +       hbl = dr->dr_h_ino + idx;
11131 +#if 0
11132 +       {
11133 +               struct hlist_bl_node *tmp;
11134 +
11135 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11136 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11137 +       }
11138 +#endif
11139 +       hlist_bl_lock(hbl);
11140 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11141 +               if (ent->dr_h_ino == ino) {
11142 +                       found = 1;
11143 +                       break;
11144 +               }
11145 +       if (!found && add_ent)
11146 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11147 +       hlist_bl_unlock(hbl);
11148 +
11149 +       if (!found && add_ent)
11150 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11151 +
11152 +       return found;
11153 +}
11154 +
11155 +void au_dr_hino_free(struct au_dr_br *dr)
11156 +{
11157 +       int i;
11158 +       struct hlist_bl_head *hbl;
11159 +       struct hlist_bl_node *pos, *tmp;
11160 +       struct au_dr_hino *ent;
11161 +
11162 +       /* SiMustWriteLock(sb); */
11163 +
11164 +       for (i = 0; i < AuDirren_NHASH; i++) {
11165 +               hbl = dr->dr_h_ino + i;
11166 +               /* no spinlock since sbinfo must be write-locked */
11167 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11168 +                       au_kfree_rcu(ent);
11169 +               INIT_HLIST_BL_HEAD(hbl);
11170 +       }
11171 +}
11172 +
11173 +/* returns the number of inodes or an error */
11174 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11175 +                           struct file *hinofile)
11176 +{
11177 +       int err, i;
11178 +       ssize_t ssz;
11179 +       loff_t pos, oldsize;
11180 +       __be64 u64;
11181 +       struct inode *hinoinode;
11182 +       struct hlist_bl_head *hbl;
11183 +       struct hlist_bl_node *n1, *n2;
11184 +       struct au_dr_hino *ent;
11185 +
11186 +       SiMustWriteLock(sb);
11187 +       AuDebugOn(!au_br_writable(br->br_perm));
11188 +
11189 +       hinoinode = file_inode(hinofile);
11190 +       oldsize = i_size_read(hinoinode);
11191 +
11192 +       err = 0;
11193 +       pos = 0;
11194 +       hbl = br->br_dirren.dr_h_ino;
11195 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11196 +               /* no bit-lock since sbinfo must be write-locked */
11197 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11198 +                       AuDbg("hi%llu, %pD2\n",
11199 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11200 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11201 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11202 +                       if (ssz == sizeof(u64))
11203 +                               continue;
11204 +
11205 +                       /* write error */
11206 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11207 +                       err = -ENOSPC;
11208 +                       if (ssz < 0)
11209 +                               err = ssz;
11210 +                       break;
11211 +               }
11212 +       }
11213 +       /* regardless the error */
11214 +       if (pos < oldsize) {
11215 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11216 +               AuTraceErr(err);
11217 +       }
11218 +
11219 +       AuTraceErr(err);
11220 +       return err;
11221 +}
11222 +
11223 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11224 +{
11225 +       int err, hidx;
11226 +       ssize_t ssz;
11227 +       size_t sz, n;
11228 +       loff_t pos;
11229 +       uint64_t u64;
11230 +       struct au_dr_hino *ent;
11231 +       struct inode *hinoinode;
11232 +       struct hlist_bl_head *hbl;
11233 +
11234 +       err = 0;
11235 +       pos = 0;
11236 +       hbl = dr->dr_h_ino;
11237 +       hinoinode = file_inode(hinofile);
11238 +       sz = i_size_read(hinoinode);
11239 +       AuDebugOn(sz % sizeof(u64));
11240 +       n = sz / sizeof(u64);
11241 +       while (n--) {
11242 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11243 +               if (unlikely(ssz != sizeof(u64))) {
11244 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11245 +                       err = -EINVAL;
11246 +                       if (ssz < 0)
11247 +                               err = ssz;
11248 +                       goto out_free;
11249 +               }
11250 +
11251 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11252 +               if (!ent) {
11253 +                       err = -ENOMEM;
11254 +                       AuTraceErr(err);
11255 +                       goto out_free;
11256 +               }
11257 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11258 +               AuDbg("hi%llu, %pD2\n",
11259 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11260 +               hidx = au_dr_ihash(ent->dr_h_ino);
11261 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11262 +       }
11263 +       goto out; /* success */
11264 +
11265 +out_free:
11266 +       au_dr_hino_free(dr);
11267 +out:
11268 +       AuTraceErr(err);
11269 +       return err;
11270 +}
11271 +
11272 +/*
11273 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11274 + * @path is a switch to distinguish load and store.
11275 + */
11276 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11277 +                     struct au_branch *br, const struct path *path)
11278 +{
11279 +       int err, flags;
11280 +       unsigned char load, suspend;
11281 +       struct file *hinofile;
11282 +       struct au_hinode *hdir;
11283 +       struct inode *dir, *delegated;
11284 +       struct path hinopath;
11285 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11286 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11287 +
11288 +       AuDebugOn(bindex < 0 && !br);
11289 +       AuDebugOn(bindex >= 0 && br);
11290 +
11291 +       err = -EINVAL;
11292 +       suspend = !br;
11293 +       if (suspend)
11294 +               br = au_sbr(sb, bindex);
11295 +       load = !!path;
11296 +       if (!load) {
11297 +               path = &br->br_path;
11298 +               AuDebugOn(!au_br_writable(br->br_perm));
11299 +               if (unlikely(!au_br_writable(br->br_perm)))
11300 +                       goto out;
11301 +       }
11302 +
11303 +       hdir = NULL;
11304 +       if (suspend) {
11305 +               dir = d_inode(sb->s_root);
11306 +               hdir = au_hinode(au_ii(dir), bindex);
11307 +               dir = hdir->hi_inode;
11308 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11309 +       } else {
11310 +               dir = d_inode(path->dentry);
11311 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11312 +       }
11313 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11314 +       err = PTR_ERR(hinopath.dentry);
11315 +       if (IS_ERR(hinopath.dentry))
11316 +               goto out_unlock;
11317 +
11318 +       err = 0;
11319 +       flags = O_RDONLY;
11320 +       if (load) {
11321 +               if (d_is_negative(hinopath.dentry))
11322 +                       goto out_dput; /* success */
11323 +       } else {
11324 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11325 +                       if (d_is_positive(hinopath.dentry)) {
11326 +                               delegated = NULL;
11327 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11328 +                                                  /*force*/0);
11329 +                               AuTraceErr(err);
11330 +                               if (unlikely(err))
11331 +                                       pr_err("ignored err %d, %pd2\n",
11332 +                                              err, hinopath.dentry);
11333 +                               if (unlikely(err == -EWOULDBLOCK))
11334 +                                       iput(delegated);
11335 +                               err = 0;
11336 +                       }
11337 +                       goto out_dput;
11338 +               } else if (!d_is_positive(hinopath.dentry)) {
11339 +                       err = vfsub_create(dir, &hinopath, 0600,
11340 +                                          /*want_excl*/false);
11341 +                       AuTraceErr(err);
11342 +                       if (unlikely(err))
11343 +                               goto out_dput;
11344 +               }
11345 +               flags = O_WRONLY;
11346 +       }
11347 +       hinopath.mnt = path->mnt;
11348 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11349 +       if (suspend)
11350 +               au_hn_inode_unlock(hdir);
11351 +       else
11352 +               inode_unlock(dir);
11353 +       dput(hinopath.dentry);
11354 +       AuTraceErrPtr(hinofile);
11355 +       if (IS_ERR(hinofile)) {
11356 +               err = PTR_ERR(hinofile);
11357 +               goto out;
11358 +       }
11359 +
11360 +       if (load)
11361 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11362 +       else
11363 +               err = au_dr_hino_store(sb, br, hinofile);
11364 +       fput(hinofile);
11365 +       goto out;
11366 +
11367 +out_dput:
11368 +       dput(hinopath.dentry);
11369 +out_unlock:
11370 +       if (suspend)
11371 +               au_hn_inode_unlock(hdir);
11372 +       else
11373 +               inode_unlock(dir);
11374 +out:
11375 +       AuTraceErr(err);
11376 +       return err;
11377 +}
11378 +
11379 +/* ---------------------------------------------------------------------- */
11380 +
11381 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11382 +{
11383 +       int err;
11384 +       struct kstatfs kstfs;
11385 +       dev_t dev;
11386 +       struct dentry *dentry;
11387 +       struct super_block *sb;
11388 +
11389 +       err = vfs_statfs((void *)path, &kstfs);
11390 +       AuTraceErr(err);
11391 +       if (unlikely(err))
11392 +               goto out;
11393 +
11394 +       /* todo: support for UUID */
11395 +
11396 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11397 +               brid->type = AuBrid_FSID;
11398 +               brid->fsid = kstfs.f_fsid;
11399 +       } else {
11400 +               dentry = path->dentry;
11401 +               sb = dentry->d_sb;
11402 +               dev = sb->s_dev;
11403 +               if (dev) {
11404 +                       brid->type = AuBrid_DEV;
11405 +                       brid->dev = dev;
11406 +               }
11407 +       }
11408 +
11409 +out:
11410 +       return err;
11411 +}
11412 +
11413 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11414 +                 const struct path *path)
11415 +{
11416 +       int err, i;
11417 +       struct au_dr_br *dr;
11418 +       struct hlist_bl_head *hbl;
11419 +
11420 +       dr = &br->br_dirren;
11421 +       hbl = dr->dr_h_ino;
11422 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11423 +               INIT_HLIST_BL_HEAD(hbl);
11424 +
11425 +       err = au_dr_brid_init(&dr->dr_brid, path);
11426 +       if (unlikely(err))
11427 +               goto out;
11428 +
11429 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11430 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11431 +
11432 +out:
11433 +       AuTraceErr(err);
11434 +       return err;
11435 +}
11436 +
11437 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11438 +{
11439 +       int err;
11440 +
11441 +       err = 0;
11442 +       if (au_br_writable(br->br_perm))
11443 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11444 +       if (!err)
11445 +               au_dr_hino_free(&br->br_dirren);
11446 +
11447 +       return err;
11448 +}
11449 +
11450 +/* ---------------------------------------------------------------------- */
11451 +
11452 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11453 +                      char *buf, size_t sz)
11454 +{
11455 +       int err;
11456 +       unsigned int major, minor;
11457 +       char *p;
11458 +
11459 +       p = buf;
11460 +       err = snprintf(p, sz, "%d_", brid->type);
11461 +       AuDebugOn(err > sz);
11462 +       p += err;
11463 +       sz -= err;
11464 +       switch (brid->type) {
11465 +       case AuBrid_Unset:
11466 +               return -EINVAL;
11467 +       case AuBrid_UUID:
11468 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11469 +               break;
11470 +       case AuBrid_FSID:
11471 +               err = snprintf(p, sz, "%08x-%08x",
11472 +                              brid->fsid.val[0], brid->fsid.val[1]);
11473 +               break;
11474 +       case AuBrid_DEV:
11475 +               major = MAJOR(brid->dev);
11476 +               minor = MINOR(brid->dev);
11477 +               if (major <= 0xff && minor <= 0xff)
11478 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11479 +               else
11480 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11481 +               break;
11482 +       }
11483 +       AuDebugOn(err > sz);
11484 +       p += err;
11485 +       sz -= err;
11486 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11487 +       AuDebugOn(err > sz);
11488 +       p += err;
11489 +       sz -= err;
11490 +
11491 +       return p - buf;
11492 +}
11493 +
11494 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11495 +{
11496 +       int rlen;
11497 +       struct dentry *br_dentry;
11498 +       struct inode *br_inode;
11499 +
11500 +       br_dentry = au_br_dentry(br);
11501 +       br_inode = d_inode(br_dentry);
11502 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11503 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11504 +       AuDebugOn(rlen > len);
11505 +
11506 +       return rlen;
11507 +}
11508 +
11509 +/* ---------------------------------------------------------------------- */
11510 +
11511 +/*
11512 + * from the given @h_dentry, construct drinfo at @*fdata.
11513 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11514 + * @allocated.
11515 + */
11516 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11517 +                              struct dentry *h_dentry,
11518 +                              unsigned char *allocated)
11519 +{
11520 +       int err, v;
11521 +       struct au_drinfo_fdata *f, *p;
11522 +       struct au_drinfo *drinfo;
11523 +       struct inode *h_inode;
11524 +       struct qstr *qname;
11525 +
11526 +       err = 0;
11527 +       f = *fdata;
11528 +       h_inode = d_inode(h_dentry);
11529 +       qname = &h_dentry->d_name;
11530 +       drinfo = &f->drinfo;
11531 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11532 +       drinfo->oldnamelen = qname->len;
11533 +       if (*allocated < sizeof(*f) + qname->len) {
11534 +               v = roundup_pow_of_two(*allocated + qname->len);
11535 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11536 +               if (unlikely(!p)) {
11537 +                       err = -ENOMEM;
11538 +                       AuTraceErr(err);
11539 +                       goto out;
11540 +               }
11541 +               f = p;
11542 +               *fdata = f;
11543 +               *allocated = v;
11544 +               drinfo = &f->drinfo;
11545 +       }
11546 +       memcpy(drinfo->oldname, qname->name, qname->len);
11547 +       AuDbg("i%llu, %.*s\n",
11548 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11549 +             drinfo->oldname);
11550 +
11551 +out:
11552 +       AuTraceErr(err);
11553 +       return err;
11554 +}
11555 +
11556 +/* callers have to free the return value */
11557 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11558 +{
11559 +       struct au_drinfo *ret, *drinfo;
11560 +       struct au_drinfo_fdata fdata;
11561 +       int len;
11562 +       loff_t pos;
11563 +       ssize_t ssz;
11564 +
11565 +       ret = ERR_PTR(-EIO);
11566 +       pos = 0;
11567 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11568 +       if (unlikely(ssz != sizeof(fdata))) {
11569 +               AuIOErr("ssz %zd, %u, %pD2\n",
11570 +                       ssz, (unsigned int)sizeof(fdata), file);
11571 +               goto out;
11572 +       }
11573 +
11574 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11575 +       switch (fdata.magic) {
11576 +       case AUFS_DRINFO_MAGIC_V1:
11577 +               break;
11578 +       default:
11579 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11580 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11581 +               goto out;
11582 +       }
11583 +
11584 +       drinfo = &fdata.drinfo;
11585 +       len = drinfo->oldnamelen;
11586 +       if (!len) {
11587 +               AuIOErr("broken drinfo %pD2\n", file);
11588 +               goto out;
11589 +       }
11590 +
11591 +       ret = NULL;
11592 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11593 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11594 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11595 +                     (unsigned long long)drinfo->ino,
11596 +                     (unsigned long long)h_ino, file);
11597 +               goto out; /* success */
11598 +       }
11599 +
11600 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11601 +       if (unlikely(!ret)) {
11602 +               ret = ERR_PTR(-ENOMEM);
11603 +               AuTraceErrPtr(ret);
11604 +               goto out;
11605 +       }
11606 +
11607 +       *ret = *drinfo;
11608 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11609 +       if (unlikely(ssz != len)) {
11610 +               au_kfree_rcu(ret);
11611 +               ret = ERR_PTR(-EIO);
11612 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11613 +               goto out;
11614 +       }
11615 +
11616 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11617 +
11618 +out:
11619 +       return ret;
11620 +}
11621 +
11622 +/* ---------------------------------------------------------------------- */
11623 +
11624 +/* in order to be revertible */
11625 +struct au_drinfo_rev_elm {
11626 +       int                     created;
11627 +       struct dentry           *info_dentry;
11628 +       struct au_drinfo        *info_last;
11629 +};
11630 +
11631 +struct au_drinfo_rev {
11632 +       unsigned char                   already;
11633 +       aufs_bindex_t                   nelm;
11634 +       struct au_drinfo_rev_elm        elm[0];
11635 +};
11636 +
11637 +/* todo: isn't it too large? */
11638 +struct au_drinfo_store {
11639 +       struct path h_ppath;
11640 +       struct dentry *h_dentry;
11641 +       struct au_drinfo_fdata *fdata;
11642 +       char *infoname;                 /* inside of whname, just after PFX */
11643 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11644 +       aufs_bindex_t btgt, btail;
11645 +       unsigned char no_sio,
11646 +               allocated,              /* current size of *fdata */
11647 +               infonamelen,            /* room size for p */
11648 +               whnamelen,              /* length of the generated name */
11649 +               renameback;             /* renamed back */
11650 +};
11651 +
11652 +/* on rename(2) error, the caller should revert it using @elm */
11653 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11654 +                             struct au_drinfo_rev_elm *elm)
11655 +{
11656 +       int err, len;
11657 +       ssize_t ssz;
11658 +       loff_t pos;
11659 +       struct path infopath = {
11660 +               .mnt = w->h_ppath.mnt
11661 +       };
11662 +       struct inode *h_dir, *h_inode, *delegated;
11663 +       struct file *infofile;
11664 +       struct qstr *qname;
11665 +
11666 +       AuDebugOn(elm
11667 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11668 +
11669 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11670 +                                              w->whnamelen);
11671 +       AuTraceErrPtr(infopath.dentry);
11672 +       if (IS_ERR(infopath.dentry)) {
11673 +               err = PTR_ERR(infopath.dentry);
11674 +               goto out;
11675 +       }
11676 +
11677 +       err = 0;
11678 +       h_dir = d_inode(w->h_ppath.dentry);
11679 +       if (elm && d_is_negative(infopath.dentry)) {
11680 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11681 +               AuTraceErr(err);
11682 +               if (unlikely(err))
11683 +                       goto out_dput;
11684 +               elm->created = 1;
11685 +               elm->info_dentry = dget(infopath.dentry);
11686 +       }
11687 +
11688 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11689 +       AuTraceErrPtr(infofile);
11690 +       if (IS_ERR(infofile)) {
11691 +               err = PTR_ERR(infofile);
11692 +               goto out_dput;
11693 +       }
11694 +
11695 +       h_inode = d_inode(infopath.dentry);
11696 +       if (elm && i_size_read(h_inode)) {
11697 +               h_inode = d_inode(w->h_dentry);
11698 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11699 +               AuTraceErrPtr(elm->info_last);
11700 +               if (IS_ERR(elm->info_last)) {
11701 +                       err = PTR_ERR(elm->info_last);
11702 +                       elm->info_last = NULL;
11703 +                       AuDebugOn(elm->info_dentry);
11704 +                       goto out_fput;
11705 +               }
11706 +       }
11707 +
11708 +       if (elm && w->renameback) {
11709 +               delegated = NULL;
11710 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11711 +               AuTraceErr(err);
11712 +               if (unlikely(err == -EWOULDBLOCK))
11713 +                       iput(delegated);
11714 +               goto out_fput;
11715 +       }
11716 +
11717 +       pos = 0;
11718 +       qname = &w->h_dentry->d_name;
11719 +       len = sizeof(*w->fdata) + qname->len;
11720 +       if (!elm)
11721 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11722 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11723 +       if (ssz == len) {
11724 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11725 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11726 +               goto out_fput; /* success */
11727 +       } else {
11728 +               err = -EIO;
11729 +               if (ssz < 0)
11730 +                       err = ssz;
11731 +               /* the caller should revert it using @elm */
11732 +       }
11733 +
11734 +out_fput:
11735 +       fput(infofile);
11736 +out_dput:
11737 +       dput(infopath.dentry);
11738 +out:
11739 +       AuTraceErr(err);
11740 +       return err;
11741 +}
11742 +
11743 +struct au_call_drinfo_do_store_args {
11744 +       int *errp;
11745 +       struct au_drinfo_store *w;
11746 +       struct au_drinfo_rev_elm *elm;
11747 +};
11748 +
11749 +static void au_call_drinfo_do_store(void *args)
11750 +{
11751 +       struct au_call_drinfo_do_store_args *a = args;
11752 +
11753 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11754 +}
11755 +
11756 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11757 +                              struct au_drinfo_rev_elm *elm)
11758 +{
11759 +       int err, wkq_err;
11760 +
11761 +       if (w->no_sio)
11762 +               err = au_drinfo_do_store(w, elm);
11763 +       else {
11764 +               struct au_call_drinfo_do_store_args a = {
11765 +                       .errp   = &err,
11766 +                       .w      = w,
11767 +                       .elm    = elm
11768 +               };
11769 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11770 +               if (unlikely(wkq_err))
11771 +                       err = wkq_err;
11772 +       }
11773 +       AuTraceErr(err);
11774 +
11775 +       return err;
11776 +}
11777 +
11778 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11779 +                                    aufs_bindex_t btgt)
11780 +{
11781 +       int err;
11782 +
11783 +       memset(w, 0, sizeof(*w));
11784 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11785 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11786 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11787 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11788 +       w->btgt = btgt;
11789 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11790 +
11791 +       err = -ENOMEM;
11792 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11793 +       if (unlikely(!w->fdata)) {
11794 +               AuTraceErr(err);
11795 +               goto out;
11796 +       }
11797 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11798 +       err = 0;
11799 +
11800 +out:
11801 +       return err;
11802 +}
11803 +
11804 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11805 +{
11806 +       au_kfree_rcu(w->fdata);
11807 +}
11808 +
11809 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11810 +                               struct au_drinfo_store *w)
11811 +{
11812 +       struct au_drinfo_rev_elm *elm;
11813 +       struct inode *h_dir, *delegated;
11814 +       int err, nelm;
11815 +       struct path infopath = {
11816 +               .mnt = w->h_ppath.mnt
11817 +       };
11818 +
11819 +       h_dir = d_inode(w->h_ppath.dentry);
11820 +       IMustLock(h_dir);
11821 +
11822 +       err = 0;
11823 +       elm = rev->elm;
11824 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11825 +               AuDebugOn(elm->created && elm->info_last);
11826 +               if (elm->created) {
11827 +                       AuDbg("here\n");
11828 +                       delegated = NULL;
11829 +                       infopath.dentry = elm->info_dentry;
11830 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11831 +                                          !w->no_sio);
11832 +                       AuTraceErr(err);
11833 +                       if (unlikely(err == -EWOULDBLOCK))
11834 +                               iput(delegated);
11835 +                       dput(elm->info_dentry);
11836 +               } else if (elm->info_last) {
11837 +                       AuDbg("here\n");
11838 +                       w->fdata->drinfo = *elm->info_last;
11839 +                       memcpy(w->fdata->drinfo.oldname,
11840 +                              elm->info_last->oldname,
11841 +                              elm->info_last->oldnamelen);
11842 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11843 +                       au_kfree_rcu(elm->info_last);
11844 +               }
11845 +               if (unlikely(err))
11846 +                       AuIOErr("%d, %s\n", err, w->whname);
11847 +               /* go on even if err */
11848 +       }
11849 +}
11850 +
11851 +/* caller has to call au_dr_rename_fin() later */
11852 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11853 +                          struct qstr *dst_name, void *_rev)
11854 +{
11855 +       int err, sz, nelm;
11856 +       aufs_bindex_t bindex, btail;
11857 +       struct au_drinfo_store work;
11858 +       struct au_drinfo_rev *rev, **p;
11859 +       struct au_drinfo_rev_elm *elm;
11860 +       struct super_block *sb;
11861 +       struct au_branch *br;
11862 +       struct au_hinode *hdir;
11863 +
11864 +       err = au_drinfo_store_work_init(&work, btgt);
11865 +       AuTraceErr(err);
11866 +       if (unlikely(err))
11867 +               goto out;
11868 +
11869 +       err = -ENOMEM;
11870 +       btail = au_dbtaildir(dentry);
11871 +       nelm = btail - btgt;
11872 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11873 +       rev = kcalloc(1, sz, GFP_NOFS);
11874 +       if (unlikely(!rev)) {
11875 +               AuTraceErr(err);
11876 +               goto out_args;
11877 +       }
11878 +       rev->nelm = nelm;
11879 +       elm = rev->elm;
11880 +       p = _rev;
11881 +       *p = rev;
11882 +
11883 +       err = 0;
11884 +       sb = dentry->d_sb;
11885 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11886 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11887 +       hdir = au_hi(d_inode(dentry), btgt);
11888 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11889 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11890 +               work.h_dentry = au_h_dptr(dentry, bindex);
11891 +               if (!work.h_dentry)
11892 +                       continue;
11893 +
11894 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11895 +                                         &work.allocated);
11896 +               AuTraceErr(err);
11897 +               if (unlikely(err))
11898 +                       break;
11899 +
11900 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11901 +               br = au_sbr(sb, bindex);
11902 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11903 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11904 +                                                work.infonamelen);
11905 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11906 +                     work.whnamelen, work.whname,
11907 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11908 +                     work.fdata->drinfo.oldnamelen,
11909 +                     work.fdata->drinfo.oldname);
11910 +
11911 +               err = au_drinfo_store_sio(&work, elm);
11912 +               AuTraceErr(err);
11913 +               if (unlikely(err))
11914 +                       break;
11915 +       }
11916 +       if (unlikely(err)) {
11917 +               /* revert all drinfo */
11918 +               au_drinfo_store_rev(rev, &work);
11919 +               au_kfree_try_rcu(rev);
11920 +               *p = NULL;
11921 +       }
11922 +       au_hn_inode_unlock(hdir);
11923 +
11924 +out_args:
11925 +       au_drinfo_store_work_fin(&work);
11926 +out:
11927 +       return err;
11928 +}
11929 +
11930 +/* ---------------------------------------------------------------------- */
11931 +
11932 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11933 +                struct qstr *dst_name, void *_rev)
11934 +{
11935 +       int err, already;
11936 +       ino_t ino;
11937 +       struct super_block *sb;
11938 +       struct au_branch *br;
11939 +       struct au_dr_br *dr;
11940 +       struct dentry *h_dentry;
11941 +       struct inode *h_inode;
11942 +       struct au_dr_hino *ent;
11943 +       struct au_drinfo_rev *rev, **p;
11944 +
11945 +       AuDbg("bindex %d\n", bindex);
11946 +
11947 +       err = -ENOMEM;
11948 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11949 +       if (unlikely(!ent))
11950 +               goto out;
11951 +
11952 +       sb = src->d_sb;
11953 +       br = au_sbr(sb, bindex);
11954 +       dr = &br->br_dirren;
11955 +       h_dentry = au_h_dptr(src, bindex);
11956 +       h_inode = d_inode(h_dentry);
11957 +       ino = h_inode->i_ino;
11958 +       ent->dr_h_ino = ino;
11959 +       already = au_dr_hino_test_add(dr, ino, ent);
11960 +       AuDbg("b%d, hi%llu, already %d\n",
11961 +             bindex, (unsigned long long)ino, already);
11962 +
11963 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11964 +       AuTraceErr(err);
11965 +       if (!err) {
11966 +               p = _rev;
11967 +               rev = *p;
11968 +               rev->already = already;
11969 +               goto out; /* success */
11970 +       }
11971 +
11972 +       /* revert */
11973 +       if (!already)
11974 +               au_dr_hino_del(dr, ent);
11975 +       au_kfree_rcu(ent);
11976 +
11977 +out:
11978 +       AuTraceErr(err);
11979 +       return err;
11980 +}
11981 +
11982 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11983 +{
11984 +       struct au_drinfo_rev *rev;
11985 +       struct au_drinfo_rev_elm *elm;
11986 +       int nelm;
11987 +
11988 +       rev = _rev;
11989 +       elm = rev->elm;
11990 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11991 +               dput(elm->info_dentry);
11992 +               au_kfree_rcu(elm->info_last);
11993 +       }
11994 +       au_kfree_try_rcu(rev);
11995 +}
11996 +
11997 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11998 +{
11999 +       int err;
12000 +       struct au_drinfo_store work;
12001 +       struct au_drinfo_rev *rev = _rev;
12002 +       struct super_block *sb;
12003 +       struct au_branch *br;
12004 +       struct inode *h_inode;
12005 +       struct au_dr_br *dr;
12006 +       struct au_dr_hino *ent;
12007 +
12008 +       err = au_drinfo_store_work_init(&work, btgt);
12009 +       if (unlikely(err))
12010 +               goto out;
12011 +
12012 +       sb = src->d_sb;
12013 +       br = au_sbr(sb, btgt);
12014 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
12015 +       work.h_ppath.mnt = au_br_mnt(br);
12016 +       au_drinfo_store_rev(rev, &work);
12017 +       au_drinfo_store_work_fin(&work);
12018 +       if (rev->already)
12019 +               goto out;
12020 +
12021 +       dr = &br->br_dirren;
12022 +       h_inode = d_inode(work.h_ppath.dentry);
12023 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
12024 +       BUG_ON(!ent);
12025 +       au_dr_hino_del(dr, ent);
12026 +       au_kfree_rcu(ent);
12027 +
12028 +out:
12029 +       au_kfree_try_rcu(rev);
12030 +       if (unlikely(err))
12031 +               pr_err("failed to remove dirren info\n");
12032 +}
12033 +
12034 +/* ---------------------------------------------------------------------- */
12035 +
12036 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
12037 +                                          char *whname, int whnamelen,
12038 +                                          struct dentry **info_dentry)
12039 +{
12040 +       struct au_drinfo *drinfo;
12041 +       struct file *f;
12042 +       struct inode *h_dir;
12043 +       struct path infopath;
12044 +       int unlocked;
12045 +
12046 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
12047 +
12048 +       *info_dentry = NULL;
12049 +       drinfo = NULL;
12050 +       unlocked = 0;
12051 +       h_dir = d_inode(h_ppath->dentry);
12052 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
12053 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
12054 +                                              whnamelen);
12055 +       if (IS_ERR(infopath.dentry)) {
12056 +               drinfo = (void *)infopath.dentry;
12057 +               goto out;
12058 +       }
12059 +
12060 +       if (d_is_negative(infopath.dentry))
12061 +               goto out_dput; /* success */
12062 +
12063 +       infopath.mnt = h_ppath->mnt;
12064 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
12065 +       inode_unlock_shared(h_dir);
12066 +       unlocked = 1;
12067 +       if (IS_ERR(f)) {
12068 +               drinfo = (void *)f;
12069 +               goto out_dput;
12070 +       }
12071 +
12072 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
12073 +       if (IS_ERR_OR_NULL(drinfo))
12074 +               goto out_fput;
12075 +
12076 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
12077 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
12078 +
12079 +out_fput:
12080 +       fput(f);
12081 +out_dput:
12082 +       dput(infopath.dentry);
12083 +out:
12084 +       if (!unlocked)
12085 +               inode_unlock_shared(h_dir);
12086 +       AuTraceErrPtr(drinfo);
12087 +       return drinfo;
12088 +}
12089 +
12090 +struct au_drinfo_do_load_args {
12091 +       struct au_drinfo **drinfop;
12092 +       struct path *h_ppath;
12093 +       char *whname;
12094 +       int whnamelen;
12095 +       struct dentry **info_dentry;
12096 +};
12097 +
12098 +static void au_call_drinfo_do_load(void *args)
12099 +{
12100 +       struct au_drinfo_do_load_args *a = args;
12101 +
12102 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12103 +                                       a->info_dentry);
12104 +}
12105 +
12106 +struct au_drinfo_load {
12107 +       struct path h_ppath;
12108 +       struct qstr *qname;
12109 +       unsigned char no_sio;
12110 +
12111 +       aufs_bindex_t ninfo;
12112 +       struct au_drinfo **drinfo;
12113 +};
12114 +
12115 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12116 +                         struct au_branch *br)
12117 +{
12118 +       int err, wkq_err, whnamelen, e;
12119 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12120 +               = AUFS_WH_DR_INFO_PFX;
12121 +       struct au_drinfo *drinfo;
12122 +       struct qstr oldname;
12123 +       struct inode *h_dir, *delegated;
12124 +       struct dentry *info_dentry;
12125 +       struct path infopath;
12126 +
12127 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12128 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12129 +                                   sizeof(whname) - whnamelen);
12130 +       if (w->no_sio)
12131 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12132 +                                          &info_dentry);
12133 +       else {
12134 +               struct au_drinfo_do_load_args args = {
12135 +                       .drinfop        = &drinfo,
12136 +                       .h_ppath        = &w->h_ppath,
12137 +                       .whname         = whname,
12138 +                       .whnamelen      = whnamelen,
12139 +                       .info_dentry    = &info_dentry
12140 +               };
12141 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12142 +               if (unlikely(wkq_err))
12143 +                       drinfo = ERR_PTR(wkq_err);
12144 +       }
12145 +       err = PTR_ERR(drinfo);
12146 +       if (IS_ERR_OR_NULL(drinfo))
12147 +               goto out;
12148 +
12149 +       err = 0;
12150 +       oldname.len = drinfo->oldnamelen;
12151 +       oldname.name = drinfo->oldname;
12152 +       if (au_qstreq(w->qname, &oldname)) {
12153 +               /* the name is renamed back */
12154 +               au_kfree_rcu(drinfo);
12155 +               drinfo = NULL;
12156 +
12157 +               infopath.dentry = info_dentry;
12158 +               infopath.mnt = w->h_ppath.mnt;
12159 +               h_dir = d_inode(w->h_ppath.dentry);
12160 +               delegated = NULL;
12161 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12162 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12163 +               inode_unlock(h_dir);
12164 +               if (unlikely(e))
12165 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12166 +               if (unlikely(e == -EWOULDBLOCK))
12167 +                       iput(delegated);
12168 +       }
12169 +       au_kfree_rcu(w->drinfo[bindex]);
12170 +       w->drinfo[bindex] = drinfo;
12171 +       dput(info_dentry);
12172 +
12173 +out:
12174 +       AuTraceErr(err);
12175 +       return err;
12176 +}
12177 +
12178 +/* ---------------------------------------------------------------------- */
12179 +
12180 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12181 +{
12182 +       struct au_drinfo **p = drinfo;
12183 +
12184 +       while (n-- > 0)
12185 +               au_kfree_rcu(*drinfo++);
12186 +       au_kfree_try_rcu(p);
12187 +}
12188 +
12189 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12190 +              aufs_bindex_t btgt)
12191 +{
12192 +       int err, ninfo;
12193 +       struct au_drinfo_load w;
12194 +       aufs_bindex_t bindex, bbot;
12195 +       struct au_branch *br;
12196 +       struct inode *h_dir;
12197 +       struct au_dr_hino *ent;
12198 +       struct super_block *sb;
12199 +
12200 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12201 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12202 +             AuLNPair(&lkup->whname), btgt);
12203 +
12204 +       sb = dentry->d_sb;
12205 +       bbot = au_sbbot(sb);
12206 +       w.ninfo = bbot + 1;
12207 +       if (!lkup->dirren.drinfo) {
12208 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12209 +                                             sizeof(*lkup->dirren.drinfo),
12210 +                                             GFP_NOFS);
12211 +               if (unlikely(!lkup->dirren.drinfo)) {
12212 +                       err = -ENOMEM;
12213 +                       goto out;
12214 +               }
12215 +               lkup->dirren.ninfo = w.ninfo;
12216 +       }
12217 +       w.drinfo = lkup->dirren.drinfo;
12218 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12219 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12220 +       AuDebugOn(!w.h_ppath.dentry);
12221 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12222 +       w.qname = &dentry->d_name;
12223 +
12224 +       ninfo = 0;
12225 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12226 +               br = au_sbr(sb, bindex);
12227 +               err = au_drinfo_load(&w, bindex, br);
12228 +               if (unlikely(err))
12229 +                       goto out_free;
12230 +               if (w.drinfo[bindex])
12231 +                       ninfo++;
12232 +       }
12233 +       if (!ninfo) {
12234 +               br = au_sbr(sb, btgt);
12235 +               h_dir = d_inode(w.h_ppath.dentry);
12236 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12237 +               AuDebugOn(!ent);
12238 +               au_dr_hino_del(&br->br_dirren, ent);
12239 +               au_kfree_rcu(ent);
12240 +       }
12241 +       goto out; /* success */
12242 +
12243 +out_free:
12244 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12245 +       lkup->dirren.ninfo = 0;
12246 +       lkup->dirren.drinfo = NULL;
12247 +out:
12248 +       AuTraceErr(err);
12249 +       return err;
12250 +}
12251 +
12252 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12253 +{
12254 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12255 +}
12256 +
12257 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12258 +{
12259 +       int err;
12260 +       struct au_drinfo *drinfo;
12261 +
12262 +       err = 0;
12263 +       if (!lkup->dirren.drinfo)
12264 +               goto out;
12265 +       AuDebugOn(lkup->dirren.ninfo < btgt + 1);
12266 +       drinfo = lkup->dirren.drinfo[btgt + 1];
12267 +       if (!drinfo)
12268 +               goto out;
12269 +
12270 +       au_kfree_try_rcu(lkup->whname.name);
12271 +       lkup->whname.name = NULL;
12272 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12273 +       lkup->dirren.dr_name.name = drinfo->oldname;
12274 +       lkup->name = &lkup->dirren.dr_name;
12275 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12276 +       if (!err)
12277 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12278 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12279 +                     btgt);
12280 +
12281 +out:
12282 +       AuTraceErr(err);
12283 +       return err;
12284 +}
12285 +
12286 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12287 +                    ino_t h_ino)
12288 +{
12289 +       int match;
12290 +       struct au_drinfo *drinfo;
12291 +
12292 +       match = 1;
12293 +       if (!lkup->dirren.drinfo)
12294 +               goto out;
12295 +       AuDebugOn(lkup->dirren.ninfo < bindex + 1);
12296 +       drinfo = lkup->dirren.drinfo[bindex + 1];
12297 +       if (!drinfo)
12298 +               goto out;
12299 +
12300 +       match = (drinfo->ino == h_ino);
12301 +       AuDbg("match %d\n", match);
12302 +
12303 +out:
12304 +       return match;
12305 +}
12306 +
12307 +/* ---------------------------------------------------------------------- */
12308 +
12309 +int au_dr_opt_set(struct super_block *sb)
12310 +{
12311 +       int err;
12312 +       aufs_bindex_t bindex, bbot;
12313 +       struct au_branch *br;
12314 +
12315 +       err = 0;
12316 +       bbot = au_sbbot(sb);
12317 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12318 +               br = au_sbr(sb, bindex);
12319 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12320 +       }
12321 +
12322 +       return err;
12323 +}
12324 +
12325 +int au_dr_opt_flush(struct super_block *sb)
12326 +{
12327 +       int err;
12328 +       aufs_bindex_t bindex, bbot;
12329 +       struct au_branch *br;
12330 +
12331 +       err = 0;
12332 +       bbot = au_sbbot(sb);
12333 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12334 +               br = au_sbr(sb, bindex);
12335 +               if (au_br_writable(br->br_perm))
12336 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12337 +       }
12338 +
12339 +       return err;
12340 +}
12341 +
12342 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12343 +{
12344 +       int err;
12345 +       aufs_bindex_t bindex, bbot;
12346 +       struct au_branch *br;
12347 +
12348 +       err = 0;
12349 +       if (!no_flush) {
12350 +               err = au_dr_opt_flush(sb);
12351 +               if (unlikely(err))
12352 +                       goto out;
12353 +       }
12354 +
12355 +       bbot = au_sbbot(sb);
12356 +       for (bindex = 0; bindex <= bbot; bindex++) {
12357 +               br = au_sbr(sb, bindex);
12358 +               au_dr_hino_free(&br->br_dirren);
12359 +       }
12360 +
12361 +out:
12362 +       return err;
12363 +}
12364 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12365 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12366 +++ linux/fs/aufs/dirren.h      2018-08-12 23:43:05.453457863 +0200
12367 @@ -0,0 +1,140 @@
12368 +/* SPDX-License-Identifier: GPL-2.0 */
12369 +/*
12370 + * Copyright (C) 2017-2018 Junjiro R. Okajima
12371 + *
12372 + * This program, aufs is free software; you can redistribute it and/or modify
12373 + * it under the terms of the GNU General Public License as published by
12374 + * the Free Software Foundation; either version 2 of the License, or
12375 + * (at your option) any later version.
12376 + *
12377 + * This program is distributed in the hope that it will be useful,
12378 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12379 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12380 + * GNU General Public License for more details.
12381 + *
12382 + * You should have received a copy of the GNU General Public License
12383 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12384 + */
12385 +
12386 +/*
12387 + * renamed dir info
12388 + */
12389 +
12390 +#ifndef __AUFS_DIRREN_H__
12391 +#define __AUFS_DIRREN_H__
12392 +
12393 +#ifdef __KERNEL__
12394 +
12395 +#include <linux/dcache.h>
12396 +#include <linux/statfs.h>
12397 +#include <linux/uuid.h>
12398 +#include "hbl.h"
12399 +
12400 +#define AuDirren_NHASH 100
12401 +
12402 +#ifdef CONFIG_AUFS_DIRREN
12403 +enum au_brid_type {
12404 +       AuBrid_Unset,
12405 +       AuBrid_UUID,
12406 +       AuBrid_FSID,
12407 +       AuBrid_DEV
12408 +};
12409 +
12410 +struct au_dr_brid {
12411 +       enum au_brid_type       type;
12412 +       union {
12413 +               uuid_t  uuid;   /* unimplemented yet */
12414 +               fsid_t  fsid;
12415 +               dev_t   dev;
12416 +       };
12417 +};
12418 +
12419 +/* 20 is the max digits length of ulong 64 */
12420 +/* brid-type "_" uuid "_" inum */
12421 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12422 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12423 +
12424 +struct au_dr_hino {
12425 +       struct hlist_bl_node    dr_hnode;
12426 +       ino_t                   dr_h_ino;
12427 +};
12428 +
12429 +struct au_dr_br {
12430 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12431 +       struct au_dr_brid       dr_brid;
12432 +};
12433 +
12434 +struct au_dr_lookup {
12435 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12436 +       struct qstr             dr_name; /* subset of dr_info */
12437 +       aufs_bindex_t           ninfo;
12438 +       struct au_drinfo        **drinfo;
12439 +};
12440 +#else
12441 +struct au_dr_hino;
12442 +/* empty */
12443 +struct au_dr_br { };
12444 +struct au_dr_lookup { };
12445 +#endif
12446 +
12447 +/* ---------------------------------------------------------------------- */
12448 +
12449 +struct au_branch;
12450 +struct au_do_lookup_args;
12451 +struct au_hinode;
12452 +#ifdef CONFIG_AUFS_DIRREN
12453 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12454 +                       struct au_dr_hino *add_ent);
12455 +void au_dr_hino_free(struct au_dr_br *dr);
12456 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12457 +                 const struct path *path);
12458 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12459 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12460 +                struct qstr *dst_name, void *_rev);
12461 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12462 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12463 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12464 +              aufs_bindex_t bindex);
12465 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12466 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12467 +                    ino_t h_ino);
12468 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12469 +int au_dr_opt_set(struct super_block *sb);
12470 +int au_dr_opt_flush(struct super_block *sb);
12471 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12472 +#else
12473 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12474 +          struct au_dr_hino *add_ent);
12475 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12476 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12477 +          const struct path *path);
12478 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12479 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12480 +          struct qstr *dst_name, void *_rev);
12481 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12482 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12483 +          void *rev);
12484 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12485 +          aufs_bindex_t bindex);
12486 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12487 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12488 +          aufs_bindex_t bindex, ino_t h_ino);
12489 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12490 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12491 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12492 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12493 +#endif
12494 +
12495 +/* ---------------------------------------------------------------------- */
12496 +
12497 +#ifdef CONFIG_AUFS_DIRREN
12498 +static inline int au_dr_ihash(ino_t h_ino)
12499 +{
12500 +       return h_ino % AuDirren_NHASH;
12501 +}
12502 +#else
12503 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12504 +#endif
12505 +
12506 +#endif /* __KERNEL__ */
12507 +#endif /* __AUFS_DIRREN_H__ */
12508 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12509 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12510 +++ linux/fs/aufs/dynop.c       2018-12-27 13:19:17.708416053 +0100
12511 @@ -0,0 +1,370 @@
12512 +// SPDX-License-Identifier: GPL-2.0
12513 +/*
12514 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12515 + *
12516 + * This program, aufs is free software; you can redistribute it and/or modify
12517 + * it under the terms of the GNU General Public License as published by
12518 + * the Free Software Foundation; either version 2 of the License, or
12519 + * (at your option) any later version.
12520 + *
12521 + * This program is distributed in the hope that it will be useful,
12522 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12523 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12524 + * GNU General Public License for more details.
12525 + *
12526 + * You should have received a copy of the GNU General Public License
12527 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12528 + */
12529 +
12530 +/*
12531 + * dynamically customizable operations for regular files
12532 + */
12533 +
12534 +#include "aufs.h"
12535 +
12536 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12537 +
12538 +/*
12539 + * How large will these lists be?
12540 + * Usually just a few elements, 20-30 at most for each, I guess.
12541 + */
12542 +static struct hlist_bl_head dynop[AuDyLast];
12543 +
12544 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12545 +                                    const void *h_op)
12546 +{
12547 +       struct au_dykey *key, *tmp;
12548 +       struct hlist_bl_node *pos;
12549 +
12550 +       key = NULL;
12551 +       hlist_bl_lock(hbl);
12552 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12553 +               if (tmp->dk_op.dy_hop == h_op) {
12554 +                       key = tmp;
12555 +                       kref_get(&key->dk_kref);
12556 +                       break;
12557 +               }
12558 +       hlist_bl_unlock(hbl);
12559 +
12560 +       return key;
12561 +}
12562 +
12563 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12564 +{
12565 +       struct au_dykey **k, *found;
12566 +       const void *h_op = key->dk_op.dy_hop;
12567 +       int i;
12568 +
12569 +       found = NULL;
12570 +       k = br->br_dykey;
12571 +       for (i = 0; i < AuBrDynOp; i++)
12572 +               if (k[i]) {
12573 +                       if (k[i]->dk_op.dy_hop == h_op) {
12574 +                               found = k[i];
12575 +                               break;
12576 +                       }
12577 +               } else
12578 +                       break;
12579 +       if (!found) {
12580 +               spin_lock(&br->br_dykey_lock);
12581 +               for (; i < AuBrDynOp; i++)
12582 +                       if (k[i]) {
12583 +                               if (k[i]->dk_op.dy_hop == h_op) {
12584 +                                       found = k[i];
12585 +                                       break;
12586 +                               }
12587 +                       } else {
12588 +                               k[i] = key;
12589 +                               break;
12590 +                       }
12591 +               spin_unlock(&br->br_dykey_lock);
12592 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12593 +       }
12594 +
12595 +       return found;
12596 +}
12597 +
12598 +/* kref_get() if @key is already added */
12599 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12600 +{
12601 +       struct au_dykey *tmp, *found;
12602 +       struct hlist_bl_node *pos;
12603 +       const void *h_op = key->dk_op.dy_hop;
12604 +
12605 +       found = NULL;
12606 +       hlist_bl_lock(hbl);
12607 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12608 +               if (tmp->dk_op.dy_hop == h_op) {
12609 +                       kref_get(&tmp->dk_kref);
12610 +                       found = tmp;
12611 +                       break;
12612 +               }
12613 +       if (!found)
12614 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12615 +       hlist_bl_unlock(hbl);
12616 +
12617 +       if (!found)
12618 +               DyPrSym(key);
12619 +       return found;
12620 +}
12621 +
12622 +static void dy_free_rcu(struct rcu_head *rcu)
12623 +{
12624 +       struct au_dykey *key;
12625 +
12626 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12627 +       DyPrSym(key);
12628 +       au_kfree_rcu(key);
12629 +}
12630 +
12631 +static void dy_free(struct kref *kref)
12632 +{
12633 +       struct au_dykey *key;
12634 +       struct hlist_bl_head *hbl;
12635 +
12636 +       key = container_of(kref, struct au_dykey, dk_kref);
12637 +       hbl = dynop + key->dk_op.dy_type;
12638 +       au_hbl_del(&key->dk_hnode, hbl);
12639 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12640 +}
12641 +
12642 +void au_dy_put(struct au_dykey *key)
12643 +{
12644 +       kref_put(&key->dk_kref, dy_free);
12645 +}
12646 +
12647 +/* ---------------------------------------------------------------------- */
12648 +
12649 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12650 +
12651 +#ifdef CONFIG_AUFS_DEBUG
12652 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12653 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12654 +#else
12655 +#define DyDbgDeclare(cnt)      do {} while (0)
12656 +#define DyDbgInc(cnt)          do {} while (0)
12657 +#endif
12658 +
12659 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12660 +       DyDbgInc(cnt);                                                  \
12661 +       if (h_op->func) {                                               \
12662 +               if (src.func)                                           \
12663 +                       dst.func = src.func;                            \
12664 +               else                                                    \
12665 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12666 +       }                                                               \
12667 +} while (0)
12668 +
12669 +#define DySetForce(func, dst, src) do {                \
12670 +       AuDebugOn(!src.func);                   \
12671 +       DyDbgInc(cnt);                          \
12672 +       dst.func = src.func;                    \
12673 +} while (0)
12674 +
12675 +#define DySetAop(func) \
12676 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12677 +#define DySetAopForce(func) \
12678 +       DySetForce(func, dyaop->da_op, aufs_aop)
12679 +
12680 +static void dy_aop(struct au_dykey *key, const void *h_op,
12681 +                  struct super_block *h_sb __maybe_unused)
12682 +{
12683 +       struct au_dyaop *dyaop = (void *)key;
12684 +       const struct address_space_operations *h_aop = h_op;
12685 +       DyDbgDeclare(cnt);
12686 +
12687 +       AuDbg("%s\n", au_sbtype(h_sb));
12688 +
12689 +       DySetAop(writepage);
12690 +       DySetAopForce(readpage);        /* force */
12691 +       DySetAop(writepages);
12692 +       DySetAop(set_page_dirty);
12693 +       DySetAop(readpages);
12694 +       DySetAop(write_begin);
12695 +       DySetAop(write_end);
12696 +       DySetAop(bmap);
12697 +       DySetAop(invalidatepage);
12698 +       DySetAop(releasepage);
12699 +       DySetAop(freepage);
12700 +       /* this one will be changed according to an aufs mount option */
12701 +       DySetAop(direct_IO);
12702 +       DySetAop(migratepage);
12703 +       DySetAop(isolate_page);
12704 +       DySetAop(putback_page);
12705 +       DySetAop(launder_page);
12706 +       DySetAop(is_partially_uptodate);
12707 +       DySetAop(is_dirty_writeback);
12708 +       DySetAop(error_remove_page);
12709 +       DySetAop(swap_activate);
12710 +       DySetAop(swap_deactivate);
12711 +
12712 +       DyDbgSize(cnt, *h_aop);
12713 +}
12714 +
12715 +/* ---------------------------------------------------------------------- */
12716 +
12717 +static void dy_bug(struct kref *kref)
12718 +{
12719 +       BUG();
12720 +}
12721 +
12722 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12723 +{
12724 +       struct au_dykey *key, *old;
12725 +       struct hlist_bl_head *hbl;
12726 +       struct op {
12727 +               unsigned int sz;
12728 +               void (*set)(struct au_dykey *key, const void *h_op,
12729 +                           struct super_block *h_sb __maybe_unused);
12730 +       };
12731 +       static const struct op a[] = {
12732 +               [AuDy_AOP] = {
12733 +                       .sz     = sizeof(struct au_dyaop),
12734 +                       .set    = dy_aop
12735 +               }
12736 +       };
12737 +       const struct op *p;
12738 +
12739 +       hbl = dynop + op->dy_type;
12740 +       key = dy_gfind_get(hbl, op->dy_hop);
12741 +       if (key)
12742 +               goto out_add; /* success */
12743 +
12744 +       p = a + op->dy_type;
12745 +       key = kzalloc(p->sz, GFP_NOFS);
12746 +       if (unlikely(!key)) {
12747 +               key = ERR_PTR(-ENOMEM);
12748 +               goto out;
12749 +       }
12750 +
12751 +       key->dk_op.dy_hop = op->dy_hop;
12752 +       kref_init(&key->dk_kref);
12753 +       p->set(key, op->dy_hop, au_br_sb(br));
12754 +       old = dy_gadd(hbl, key);
12755 +       if (old) {
12756 +               au_kfree_rcu(key);
12757 +               key = old;
12758 +       }
12759 +
12760 +out_add:
12761 +       old = dy_bradd(br, key);
12762 +       if (old)
12763 +               /* its ref-count should never be zero here */
12764 +               kref_put(&key->dk_kref, dy_bug);
12765 +out:
12766 +       return key;
12767 +}
12768 +
12769 +/* ---------------------------------------------------------------------- */
12770 +/*
12771 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12772 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12773 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12774 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12775 + * See the aufs manual in detail.
12776 + */
12777 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12778 +{
12779 +       if (!do_dx)
12780 +               dyaop->da_op.direct_IO = NULL;
12781 +       else
12782 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12783 +}
12784 +
12785 +static struct au_dyaop *dy_aget(struct au_branch *br,
12786 +                               const struct address_space_operations *h_aop,
12787 +                               int do_dx)
12788 +{
12789 +       struct au_dyaop *dyaop;
12790 +       struct au_dynop op;
12791 +
12792 +       op.dy_type = AuDy_AOP;
12793 +       op.dy_haop = h_aop;
12794 +       dyaop = (void *)dy_get(&op, br);
12795 +       if (IS_ERR(dyaop))
12796 +               goto out;
12797 +       dy_adx(dyaop, do_dx);
12798 +
12799 +out:
12800 +       return dyaop;
12801 +}
12802 +
12803 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12804 +               struct inode *h_inode)
12805 +{
12806 +       int err, do_dx;
12807 +       struct super_block *sb;
12808 +       struct au_branch *br;
12809 +       struct au_dyaop *dyaop;
12810 +
12811 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12812 +       IiMustWriteLock(inode);
12813 +
12814 +       sb = inode->i_sb;
12815 +       br = au_sbr(sb, bindex);
12816 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12817 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12818 +       err = PTR_ERR(dyaop);
12819 +       if (IS_ERR(dyaop))
12820 +               /* unnecessary to call dy_fput() */
12821 +               goto out;
12822 +
12823 +       err = 0;
12824 +       inode->i_mapping->a_ops = &dyaop->da_op;
12825 +
12826 +out:
12827 +       return err;
12828 +}
12829 +
12830 +/*
12831 + * Is it safe to replace a_ops during the inode/file is in operation?
12832 + * Yes, I hope so.
12833 + */
12834 +int au_dy_irefresh(struct inode *inode)
12835 +{
12836 +       int err;
12837 +       aufs_bindex_t btop;
12838 +       struct inode *h_inode;
12839 +
12840 +       err = 0;
12841 +       if (S_ISREG(inode->i_mode)) {
12842 +               btop = au_ibtop(inode);
12843 +               h_inode = au_h_iptr(inode, btop);
12844 +               err = au_dy_iaop(inode, btop, h_inode);
12845 +       }
12846 +       return err;
12847 +}
12848 +
12849 +void au_dy_arefresh(int do_dx)
12850 +{
12851 +       struct hlist_bl_head *hbl;
12852 +       struct hlist_bl_node *pos;
12853 +       struct au_dykey *key;
12854 +
12855 +       hbl = dynop + AuDy_AOP;
12856 +       hlist_bl_lock(hbl);
12857 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12858 +               dy_adx((void *)key, do_dx);
12859 +       hlist_bl_unlock(hbl);
12860 +}
12861 +
12862 +/* ---------------------------------------------------------------------- */
12863 +
12864 +void __init au_dy_init(void)
12865 +{
12866 +       int i;
12867 +
12868 +       /* make sure that 'struct au_dykey *' can be any type */
12869 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
12870 +
12871 +       for (i = 0; i < AuDyLast; i++)
12872 +               INIT_HLIST_BL_HEAD(dynop + i);
12873 +}
12874 +
12875 +void au_dy_fin(void)
12876 +{
12877 +       int i;
12878 +
12879 +       for (i = 0; i < AuDyLast; i++)
12880 +               WARN_ON(!hlist_bl_empty(dynop + i));
12881 +}
12882 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12883 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12884 +++ linux/fs/aufs/dynop.h       2018-08-12 23:43:05.456791299 +0200
12885 @@ -0,0 +1,75 @@
12886 +/* SPDX-License-Identifier: GPL-2.0 */
12887 +/*
12888 + * Copyright (C) 2010-2018 Junjiro R. Okajima
12889 + *
12890 + * This program, aufs is free software; you can redistribute it and/or modify
12891 + * it under the terms of the GNU General Public License as published by
12892 + * the Free Software Foundation; either version 2 of the License, or
12893 + * (at your option) any later version.
12894 + *
12895 + * This program is distributed in the hope that it will be useful,
12896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12898 + * GNU General Public License for more details.
12899 + *
12900 + * You should have received a copy of the GNU General Public License
12901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12902 + */
12903 +
12904 +/*
12905 + * dynamically customizable operations (for regular files only)
12906 + */
12907 +
12908 +#ifndef __AUFS_DYNOP_H__
12909 +#define __AUFS_DYNOP_H__
12910 +
12911 +#ifdef __KERNEL__
12912 +
12913 +#include <linux/fs.h>
12914 +#include <linux/kref.h>
12915 +
12916 +enum {AuDy_AOP, AuDyLast};
12917 +
12918 +struct au_dynop {
12919 +       int                                             dy_type;
12920 +       union {
12921 +               const void                              *dy_hop;
12922 +               const struct address_space_operations   *dy_haop;
12923 +       };
12924 +};
12925 +
12926 +struct au_dykey {
12927 +       union {
12928 +               struct hlist_bl_node    dk_hnode;
12929 +               struct rcu_head         dk_rcu;
12930 +       };
12931 +       struct au_dynop         dk_op;
12932 +
12933 +       /*
12934 +        * during I am in the branch local array, kref is gotten. when the
12935 +        * branch is removed, kref is put.
12936 +        */
12937 +       struct kref             dk_kref;
12938 +};
12939 +
12940 +/* stop unioning since their sizes are very different from each other */
12941 +struct au_dyaop {
12942 +       struct au_dykey                 da_key;
12943 +       struct address_space_operations da_op; /* not const */
12944 +};
12945 +
12946 +/* ---------------------------------------------------------------------- */
12947 +
12948 +/* dynop.c */
12949 +struct au_branch;
12950 +void au_dy_put(struct au_dykey *key);
12951 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12952 +               struct inode *h_inode);
12953 +int au_dy_irefresh(struct inode *inode);
12954 +void au_dy_arefresh(int do_dio);
12955 +
12956 +void __init au_dy_init(void);
12957 +void au_dy_fin(void);
12958 +
12959 +#endif /* __KERNEL__ */
12960 +#endif /* __AUFS_DYNOP_H__ */
12961 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12962 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12963 +++ linux/fs/aufs/export.c      2018-10-23 12:33:35.596042364 +0200
12964 @@ -0,0 +1,838 @@
12965 +// SPDX-License-Identifier: GPL-2.0
12966 +/*
12967 + * Copyright (C) 2005-2018 Junjiro R. Okajima
12968 + *
12969 + * This program, aufs is free software; you can redistribute it and/or modify
12970 + * it under the terms of the GNU General Public License as published by
12971 + * the Free Software Foundation; either version 2 of the License, or
12972 + * (at your option) any later version.
12973 + *
12974 + * This program is distributed in the hope that it will be useful,
12975 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12976 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12977 + * GNU General Public License for more details.
12978 + *
12979 + * You should have received a copy of the GNU General Public License
12980 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12981 + */
12982 +
12983 +/*
12984 + * export via nfs
12985 + */
12986 +
12987 +#include <linux/exportfs.h>
12988 +#include <linux/fs_struct.h>
12989 +#include <linux/namei.h>
12990 +#include <linux/nsproxy.h>
12991 +#include <linux/random.h>
12992 +#include <linux/writeback.h>
12993 +#include "aufs.h"
12994 +
12995 +union conv {
12996 +#ifdef CONFIG_AUFS_INO_T_64
12997 +       __u32 a[2];
12998 +#else
12999 +       __u32 a[1];
13000 +#endif
13001 +       ino_t ino;
13002 +};
13003 +
13004 +static ino_t decode_ino(__u32 *a)
13005 +{
13006 +       union conv u;
13007 +
13008 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
13009 +       u.a[0] = a[0];
13010 +#ifdef CONFIG_AUFS_INO_T_64
13011 +       u.a[1] = a[1];
13012 +#endif
13013 +       return u.ino;
13014 +}
13015 +
13016 +static void encode_ino(__u32 *a, ino_t ino)
13017 +{
13018 +       union conv u;
13019 +
13020 +       u.ino = ino;
13021 +       a[0] = u.a[0];
13022 +#ifdef CONFIG_AUFS_INO_T_64
13023 +       a[1] = u.a[1];
13024 +#endif
13025 +}
13026 +
13027 +/* NFS file handle */
13028 +enum {
13029 +       Fh_br_id,
13030 +       Fh_sigen,
13031 +#ifdef CONFIG_AUFS_INO_T_64
13032 +       /* support 64bit inode number */
13033 +       Fh_ino1,
13034 +       Fh_ino2,
13035 +       Fh_dir_ino1,
13036 +       Fh_dir_ino2,
13037 +#else
13038 +       Fh_ino1,
13039 +       Fh_dir_ino1,
13040 +#endif
13041 +       Fh_igen,
13042 +       Fh_h_type,
13043 +       Fh_tail,
13044 +
13045 +       Fh_ino = Fh_ino1,
13046 +       Fh_dir_ino = Fh_dir_ino1
13047 +};
13048 +
13049 +static int au_test_anon(struct dentry *dentry)
13050 +{
13051 +       /* note: read d_flags without d_lock */
13052 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
13053 +}
13054 +
13055 +int au_test_nfsd(void)
13056 +{
13057 +       int ret;
13058 +       struct task_struct *tsk = current;
13059 +       char comm[sizeof(tsk->comm)];
13060 +
13061 +       ret = 0;
13062 +       if (tsk->flags & PF_KTHREAD) {
13063 +               get_task_comm(comm, tsk);
13064 +               ret = !strcmp(comm, "nfsd");
13065 +       }
13066 +
13067 +       return ret;
13068 +}
13069 +
13070 +/* ---------------------------------------------------------------------- */
13071 +/* inode generation external table */
13072 +
13073 +void au_xigen_inc(struct inode *inode)
13074 +{
13075 +       loff_t pos;
13076 +       ssize_t sz;
13077 +       __u32 igen;
13078 +       struct super_block *sb;
13079 +       struct au_sbinfo *sbinfo;
13080 +
13081 +       sb = inode->i_sb;
13082 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
13083 +
13084 +       sbinfo = au_sbi(sb);
13085 +       pos = inode->i_ino;
13086 +       pos *= sizeof(igen);
13087 +       igen = inode->i_generation + 1;
13088 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
13089 +                        sizeof(igen), &pos);
13090 +       if (sz == sizeof(igen))
13091 +               return; /* success */
13092 +
13093 +       if (unlikely(sz >= 0))
13094 +               AuIOErr("xigen error (%zd)\n", sz);
13095 +}
13096 +
13097 +int au_xigen_new(struct inode *inode)
13098 +{
13099 +       int err;
13100 +       loff_t pos;
13101 +       ssize_t sz;
13102 +       struct super_block *sb;
13103 +       struct au_sbinfo *sbinfo;
13104 +       struct file *file;
13105 +
13106 +       err = 0;
13107 +       /* todo: dirty, at mount time */
13108 +       if (inode->i_ino == AUFS_ROOT_INO)
13109 +               goto out;
13110 +       sb = inode->i_sb;
13111 +       SiMustAnyLock(sb);
13112 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13113 +               goto out;
13114 +
13115 +       err = -EFBIG;
13116 +       pos = inode->i_ino;
13117 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13118 +               AuIOErr1("too large i%lld\n", pos);
13119 +               goto out;
13120 +       }
13121 +       pos *= sizeof(inode->i_generation);
13122 +
13123 +       err = 0;
13124 +       sbinfo = au_sbi(sb);
13125 +       file = sbinfo->si_xigen;
13126 +       BUG_ON(!file);
13127 +
13128 +       if (vfsub_f_size_read(file)
13129 +           < pos + sizeof(inode->i_generation)) {
13130 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13131 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13132 +                                sizeof(inode->i_generation), &pos);
13133 +       } else
13134 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13135 +                               sizeof(inode->i_generation), &pos);
13136 +       if (sz == sizeof(inode->i_generation))
13137 +               goto out; /* success */
13138 +
13139 +       err = sz;
13140 +       if (unlikely(sz >= 0)) {
13141 +               err = -EIO;
13142 +               AuIOErr("xigen error (%zd)\n", sz);
13143 +       }
13144 +
13145 +out:
13146 +       return err;
13147 +}
13148 +
13149 +int au_xigen_set(struct super_block *sb, struct path *path)
13150 +{
13151 +       int err;
13152 +       struct au_sbinfo *sbinfo;
13153 +       struct file *file;
13154 +
13155 +       SiMustWriteLock(sb);
13156 +
13157 +       sbinfo = au_sbi(sb);
13158 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13159 +       err = PTR_ERR(file);
13160 +       if (IS_ERR(file))
13161 +               goto out;
13162 +       err = 0;
13163 +       if (sbinfo->si_xigen)
13164 +               fput(sbinfo->si_xigen);
13165 +       sbinfo->si_xigen = file;
13166 +
13167 +out:
13168 +       AuTraceErr(err);
13169 +       return err;
13170 +}
13171 +
13172 +void au_xigen_clr(struct super_block *sb)
13173 +{
13174 +       struct au_sbinfo *sbinfo;
13175 +
13176 +       SiMustWriteLock(sb);
13177 +
13178 +       sbinfo = au_sbi(sb);
13179 +       if (sbinfo->si_xigen) {
13180 +               fput(sbinfo->si_xigen);
13181 +               sbinfo->si_xigen = NULL;
13182 +       }
13183 +}
13184 +
13185 +/* ---------------------------------------------------------------------- */
13186 +
13187 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13188 +                                   ino_t dir_ino)
13189 +{
13190 +       struct dentry *dentry, *d;
13191 +       struct inode *inode;
13192 +       unsigned int sigen;
13193 +
13194 +       dentry = NULL;
13195 +       inode = ilookup(sb, ino);
13196 +       if (!inode)
13197 +               goto out;
13198 +
13199 +       dentry = ERR_PTR(-ESTALE);
13200 +       sigen = au_sigen(sb);
13201 +       if (unlikely(au_is_bad_inode(inode)
13202 +                    || IS_DEADDIR(inode)
13203 +                    || sigen != au_iigen(inode, NULL)))
13204 +               goto out_iput;
13205 +
13206 +       dentry = NULL;
13207 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13208 +               dentry = d_find_alias(inode);
13209 +       else {
13210 +               spin_lock(&inode->i_lock);
13211 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13212 +                       spin_lock(&d->d_lock);
13213 +                       if (!au_test_anon(d)
13214 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13215 +                               dentry = dget_dlock(d);
13216 +                               spin_unlock(&d->d_lock);
13217 +                               break;
13218 +                       }
13219 +                       spin_unlock(&d->d_lock);
13220 +               }
13221 +               spin_unlock(&inode->i_lock);
13222 +       }
13223 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13224 +               /* need to refresh */
13225 +               dput(dentry);
13226 +               dentry = NULL;
13227 +       }
13228 +
13229 +out_iput:
13230 +       iput(inode);
13231 +out:
13232 +       AuTraceErrPtr(dentry);
13233 +       return dentry;
13234 +}
13235 +
13236 +/* ---------------------------------------------------------------------- */
13237 +
13238 +/* todo: dirty? */
13239 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13240 +
13241 +struct au_compare_mnt_args {
13242 +       /* input */
13243 +       struct super_block *sb;
13244 +
13245 +       /* output */
13246 +       struct vfsmount *mnt;
13247 +};
13248 +
13249 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13250 +{
13251 +       struct au_compare_mnt_args *a = arg;
13252 +
13253 +       if (mnt->mnt_sb != a->sb)
13254 +               return 0;
13255 +       a->mnt = mntget(mnt);
13256 +       return 1;
13257 +}
13258 +
13259 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13260 +{
13261 +       int err;
13262 +       struct path root;
13263 +       struct au_compare_mnt_args args = {
13264 +               .sb = sb
13265 +       };
13266 +
13267 +       get_fs_root(current->fs, &root);
13268 +       rcu_read_lock();
13269 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13270 +       rcu_read_unlock();
13271 +       path_put(&root);
13272 +       AuDebugOn(!err);
13273 +       AuDebugOn(!args.mnt);
13274 +       return args.mnt;
13275 +}
13276 +
13277 +struct au_nfsd_si_lock {
13278 +       unsigned int sigen;
13279 +       aufs_bindex_t bindex, br_id;
13280 +       unsigned char force_lock;
13281 +};
13282 +
13283 +static int si_nfsd_read_lock(struct super_block *sb,
13284 +                            struct au_nfsd_si_lock *nsi_lock)
13285 +{
13286 +       int err;
13287 +       aufs_bindex_t bindex;
13288 +
13289 +       si_read_lock(sb, AuLock_FLUSH);
13290 +
13291 +       /* branch id may be wrapped around */
13292 +       err = 0;
13293 +       bindex = au_br_index(sb, nsi_lock->br_id);
13294 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13295 +               goto out; /* success */
13296 +
13297 +       err = -ESTALE;
13298 +       bindex = -1;
13299 +       if (!nsi_lock->force_lock)
13300 +               si_read_unlock(sb);
13301 +
13302 +out:
13303 +       nsi_lock->bindex = bindex;
13304 +       return err;
13305 +}
13306 +
13307 +struct find_name_by_ino {
13308 +       struct dir_context ctx;
13309 +       int called, found;
13310 +       ino_t ino;
13311 +       char *name;
13312 +       int namelen;
13313 +};
13314 +
13315 +static int
13316 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13317 +                loff_t offset, u64 ino, unsigned int d_type)
13318 +{
13319 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13320 +                                                 ctx);
13321 +
13322 +       a->called++;
13323 +       if (a->ino != ino)
13324 +               return 0;
13325 +
13326 +       memcpy(a->name, name, namelen);
13327 +       a->namelen = namelen;
13328 +       a->found = 1;
13329 +       return 1;
13330 +}
13331 +
13332 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13333 +                                    struct au_nfsd_si_lock *nsi_lock)
13334 +{
13335 +       struct dentry *dentry, *parent;
13336 +       struct file *file;
13337 +       struct inode *dir;
13338 +       struct find_name_by_ino arg = {
13339 +               .ctx = {
13340 +                       .actor = find_name_by_ino
13341 +               }
13342 +       };
13343 +       int err;
13344 +
13345 +       parent = path->dentry;
13346 +       if (nsi_lock)
13347 +               si_read_unlock(parent->d_sb);
13348 +       file = vfsub_dentry_open(path, au_dir_roflags);
13349 +       dentry = (void *)file;
13350 +       if (IS_ERR(file))
13351 +               goto out;
13352 +
13353 +       dentry = ERR_PTR(-ENOMEM);
13354 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13355 +       if (unlikely(!arg.name))
13356 +               goto out_file;
13357 +       arg.ino = ino;
13358 +       arg.found = 0;
13359 +       do {
13360 +               arg.called = 0;
13361 +               /* smp_mb(); */
13362 +               err = vfsub_iterate_dir(file, &arg.ctx);
13363 +       } while (!err && !arg.found && arg.called);
13364 +       dentry = ERR_PTR(err);
13365 +       if (unlikely(err))
13366 +               goto out_name;
13367 +       /* instead of ENOENT */
13368 +       dentry = ERR_PTR(-ESTALE);
13369 +       if (!arg.found)
13370 +               goto out_name;
13371 +
13372 +       /* do not call vfsub_lkup_one() */
13373 +       dir = d_inode(parent);
13374 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13375 +       AuTraceErrPtr(dentry);
13376 +       if (IS_ERR(dentry))
13377 +               goto out_name;
13378 +       AuDebugOn(au_test_anon(dentry));
13379 +       if (unlikely(d_really_is_negative(dentry))) {
13380 +               dput(dentry);
13381 +               dentry = ERR_PTR(-ENOENT);
13382 +       }
13383 +
13384 +out_name:
13385 +       free_page((unsigned long)arg.name);
13386 +out_file:
13387 +       fput(file);
13388 +out:
13389 +       if (unlikely(nsi_lock
13390 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13391 +               if (!IS_ERR(dentry)) {
13392 +                       dput(dentry);
13393 +                       dentry = ERR_PTR(-ESTALE);
13394 +               }
13395 +       AuTraceErrPtr(dentry);
13396 +       return dentry;
13397 +}
13398 +
13399 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13400 +                                       ino_t dir_ino,
13401 +                                       struct au_nfsd_si_lock *nsi_lock)
13402 +{
13403 +       struct dentry *dentry;
13404 +       struct path path;
13405 +
13406 +       if (dir_ino != AUFS_ROOT_INO) {
13407 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13408 +               dentry = path.dentry;
13409 +               if (!path.dentry || IS_ERR(path.dentry))
13410 +                       goto out;
13411 +               AuDebugOn(au_test_anon(path.dentry));
13412 +       } else
13413 +               path.dentry = dget(sb->s_root);
13414 +
13415 +       path.mnt = au_mnt_get(sb);
13416 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13417 +       path_put(&path);
13418 +
13419 +out:
13420 +       AuTraceErrPtr(dentry);
13421 +       return dentry;
13422 +}
13423 +
13424 +/* ---------------------------------------------------------------------- */
13425 +
13426 +static int h_acceptable(void *expv, struct dentry *dentry)
13427 +{
13428 +       return 1;
13429 +}
13430 +
13431 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13432 +                          char *buf, int len, struct super_block *sb)
13433 +{
13434 +       char *p;
13435 +       int n;
13436 +       struct path path;
13437 +
13438 +       p = d_path(h_rootpath, buf, len);
13439 +       if (IS_ERR(p))
13440 +               goto out;
13441 +       n = strlen(p);
13442 +
13443 +       path.mnt = h_rootpath->mnt;
13444 +       path.dentry = h_parent;
13445 +       p = d_path(&path, buf, len);
13446 +       if (IS_ERR(p))
13447 +               goto out;
13448 +       if (n != 1)
13449 +               p += n;
13450 +
13451 +       path.mnt = au_mnt_get(sb);
13452 +       path.dentry = sb->s_root;
13453 +       p = d_path(&path, buf, len - strlen(p));
13454 +       mntput(path.mnt);
13455 +       if (IS_ERR(p))
13456 +               goto out;
13457 +       if (n != 1)
13458 +               p[strlen(p)] = '/';
13459 +
13460 +out:
13461 +       AuTraceErrPtr(p);
13462 +       return p;
13463 +}
13464 +
13465 +static
13466 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13467 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13468 +{
13469 +       struct dentry *dentry, *h_parent, *root;
13470 +       struct super_block *h_sb;
13471 +       char *pathname, *p;
13472 +       struct vfsmount *h_mnt;
13473 +       struct au_branch *br;
13474 +       int err;
13475 +       struct path path;
13476 +
13477 +       br = au_sbr(sb, nsi_lock->bindex);
13478 +       h_mnt = au_br_mnt(br);
13479 +       h_sb = h_mnt->mnt_sb;
13480 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13481 +       lockdep_off();
13482 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13483 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13484 +                                     h_acceptable, /*context*/NULL);
13485 +       lockdep_on();
13486 +       dentry = h_parent;
13487 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13488 +               AuWarn1("%s decode_fh failed, %ld\n",
13489 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13490 +               goto out;
13491 +       }
13492 +       dentry = NULL;
13493 +       if (unlikely(au_test_anon(h_parent))) {
13494 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13495 +                       au_sbtype(h_sb));
13496 +               goto out_h_parent;
13497 +       }
13498 +
13499 +       dentry = ERR_PTR(-ENOMEM);
13500 +       pathname = (void *)__get_free_page(GFP_NOFS);
13501 +       if (unlikely(!pathname))
13502 +               goto out_h_parent;
13503 +
13504 +       root = sb->s_root;
13505 +       path.mnt = h_mnt;
13506 +       di_read_lock_parent(root, !AuLock_IR);
13507 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13508 +       di_read_unlock(root, !AuLock_IR);
13509 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13510 +       dentry = (void *)p;
13511 +       if (IS_ERR(p))
13512 +               goto out_pathname;
13513 +
13514 +       si_read_unlock(sb);
13515 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13516 +       dentry = ERR_PTR(err);
13517 +       if (unlikely(err))
13518 +               goto out_relock;
13519 +
13520 +       dentry = ERR_PTR(-ENOENT);
13521 +       AuDebugOn(au_test_anon(path.dentry));
13522 +       if (unlikely(d_really_is_negative(path.dentry)))
13523 +               goto out_path;
13524 +
13525 +       if (ino != d_inode(path.dentry)->i_ino)
13526 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13527 +       else
13528 +               dentry = dget(path.dentry);
13529 +
13530 +out_path:
13531 +       path_put(&path);
13532 +out_relock:
13533 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13534 +               if (!IS_ERR(dentry)) {
13535 +                       dput(dentry);
13536 +                       dentry = ERR_PTR(-ESTALE);
13537 +               }
13538 +out_pathname:
13539 +       free_page((unsigned long)pathname);
13540 +out_h_parent:
13541 +       dput(h_parent);
13542 +out:
13543 +       AuTraceErrPtr(dentry);
13544 +       return dentry;
13545 +}
13546 +
13547 +/* ---------------------------------------------------------------------- */
13548 +
13549 +static struct dentry *
13550 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13551 +                 int fh_type)
13552 +{
13553 +       struct dentry *dentry;
13554 +       __u32 *fh = fid->raw;
13555 +       struct au_branch *br;
13556 +       ino_t ino, dir_ino;
13557 +       struct au_nfsd_si_lock nsi_lock = {
13558 +               .force_lock     = 0
13559 +       };
13560 +
13561 +       dentry = ERR_PTR(-ESTALE);
13562 +       /* it should never happen, but the file handle is unreliable */
13563 +       if (unlikely(fh_len < Fh_tail))
13564 +               goto out;
13565 +       nsi_lock.sigen = fh[Fh_sigen];
13566 +       nsi_lock.br_id = fh[Fh_br_id];
13567 +
13568 +       /* branch id may be wrapped around */
13569 +       br = NULL;
13570 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13571 +               goto out;
13572 +       nsi_lock.force_lock = 1;
13573 +
13574 +       /* is this inode still cached? */
13575 +       ino = decode_ino(fh + Fh_ino);
13576 +       /* it should never happen */
13577 +       if (unlikely(ino == AUFS_ROOT_INO))
13578 +               goto out_unlock;
13579 +
13580 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13581 +       dentry = decode_by_ino(sb, ino, dir_ino);
13582 +       if (IS_ERR(dentry))
13583 +               goto out_unlock;
13584 +       if (dentry)
13585 +               goto accept;
13586 +
13587 +       /* is the parent dir cached? */
13588 +       br = au_sbr(sb, nsi_lock.bindex);
13589 +       au_lcnt_inc(&br->br_nfiles);
13590 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13591 +       if (IS_ERR(dentry))
13592 +               goto out_unlock;
13593 +       if (dentry)
13594 +               goto accept;
13595 +
13596 +       /* lookup path */
13597 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13598 +       if (IS_ERR(dentry))
13599 +               goto out_unlock;
13600 +       if (unlikely(!dentry))
13601 +               /* todo?: make it ESTALE */
13602 +               goto out_unlock;
13603 +
13604 +accept:
13605 +       if (!au_digen_test(dentry, au_sigen(sb))
13606 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13607 +               goto out_unlock; /* success */
13608 +
13609 +       dput(dentry);
13610 +       dentry = ERR_PTR(-ESTALE);
13611 +out_unlock:
13612 +       if (br)
13613 +               au_lcnt_dec(&br->br_nfiles);
13614 +       si_read_unlock(sb);
13615 +out:
13616 +       AuTraceErrPtr(dentry);
13617 +       return dentry;
13618 +}
13619 +
13620 +#if 0 /* reserved for future use */
13621 +/* support subtreecheck option */
13622 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13623 +                                       int fh_len, int fh_type)
13624 +{
13625 +       struct dentry *parent;
13626 +       __u32 *fh = fid->raw;
13627 +       ino_t dir_ino;
13628 +
13629 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13630 +       parent = decode_by_ino(sb, dir_ino, 0);
13631 +       if (IS_ERR(parent))
13632 +               goto out;
13633 +       if (!parent)
13634 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13635 +                                       dir_ino, fh, fh_len);
13636 +
13637 +out:
13638 +       AuTraceErrPtr(parent);
13639 +       return parent;
13640 +}
13641 +#endif
13642 +
13643 +/* ---------------------------------------------------------------------- */
13644 +
13645 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13646 +                         struct inode *dir)
13647 +{
13648 +       int err;
13649 +       aufs_bindex_t bindex;
13650 +       struct super_block *sb, *h_sb;
13651 +       struct dentry *dentry, *parent, *h_parent;
13652 +       struct inode *h_dir;
13653 +       struct au_branch *br;
13654 +
13655 +       err = -ENOSPC;
13656 +       if (unlikely(*max_len <= Fh_tail)) {
13657 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13658 +               goto out;
13659 +       }
13660 +
13661 +       err = FILEID_ROOT;
13662 +       if (inode->i_ino == AUFS_ROOT_INO) {
13663 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13664 +               goto out;
13665 +       }
13666 +
13667 +       h_parent = NULL;
13668 +       sb = inode->i_sb;
13669 +       err = si_read_lock(sb, AuLock_FLUSH);
13670 +       if (unlikely(err))
13671 +               goto out;
13672 +
13673 +#ifdef CONFIG_AUFS_DEBUG
13674 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13675 +               AuWarn1("NFS-exporting requires xino\n");
13676 +#endif
13677 +       err = -EIO;
13678 +       parent = NULL;
13679 +       ii_read_lock_child(inode);
13680 +       bindex = au_ibtop(inode);
13681 +       if (!dir) {
13682 +               dentry = d_find_any_alias(inode);
13683 +               if (unlikely(!dentry))
13684 +                       goto out_unlock;
13685 +               AuDebugOn(au_test_anon(dentry));
13686 +               parent = dget_parent(dentry);
13687 +               dput(dentry);
13688 +               if (unlikely(!parent))
13689 +                       goto out_unlock;
13690 +               if (d_really_is_positive(parent))
13691 +                       dir = d_inode(parent);
13692 +       }
13693 +
13694 +       ii_read_lock_parent(dir);
13695 +       h_dir = au_h_iptr(dir, bindex);
13696 +       ii_read_unlock(dir);
13697 +       if (unlikely(!h_dir))
13698 +               goto out_parent;
13699 +       h_parent = d_find_any_alias(h_dir);
13700 +       if (unlikely(!h_parent))
13701 +               goto out_hparent;
13702 +
13703 +       err = -EPERM;
13704 +       br = au_sbr(sb, bindex);
13705 +       h_sb = au_br_sb(br);
13706 +       if (unlikely(!h_sb->s_export_op)) {
13707 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13708 +               goto out_hparent;
13709 +       }
13710 +
13711 +       fh[Fh_br_id] = br->br_id;
13712 +       fh[Fh_sigen] = au_sigen(sb);
13713 +       encode_ino(fh + Fh_ino, inode->i_ino);
13714 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13715 +       fh[Fh_igen] = inode->i_generation;
13716 +
13717 +       *max_len -= Fh_tail;
13718 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13719 +                                          max_len,
13720 +                                          /*connectable or subtreecheck*/0);
13721 +       err = fh[Fh_h_type];
13722 +       *max_len += Fh_tail;
13723 +       /* todo: macros? */
13724 +       if (err != FILEID_INVALID)
13725 +               err = 99;
13726 +       else
13727 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13728 +
13729 +out_hparent:
13730 +       dput(h_parent);
13731 +out_parent:
13732 +       dput(parent);
13733 +out_unlock:
13734 +       ii_read_unlock(inode);
13735 +       si_read_unlock(sb);
13736 +out:
13737 +       if (unlikely(err < 0))
13738 +               err = FILEID_INVALID;
13739 +       return err;
13740 +}
13741 +
13742 +/* ---------------------------------------------------------------------- */
13743 +
13744 +static int aufs_commit_metadata(struct inode *inode)
13745 +{
13746 +       int err;
13747 +       aufs_bindex_t bindex;
13748 +       struct super_block *sb;
13749 +       struct inode *h_inode;
13750 +       int (*f)(struct inode *inode);
13751 +
13752 +       sb = inode->i_sb;
13753 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13754 +       ii_write_lock_child(inode);
13755 +       bindex = au_ibtop(inode);
13756 +       AuDebugOn(bindex < 0);
13757 +       h_inode = au_h_iptr(inode, bindex);
13758 +
13759 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13760 +       if (f)
13761 +               err = f(h_inode);
13762 +       else {
13763 +               struct writeback_control wbc = {
13764 +                       .sync_mode      = WB_SYNC_ALL,
13765 +                       .nr_to_write    = 0 /* metadata only */
13766 +               };
13767 +
13768 +               err = sync_inode(h_inode, &wbc);
13769 +       }
13770 +
13771 +       au_cpup_attr_timesizes(inode);
13772 +       ii_write_unlock(inode);
13773 +       si_read_unlock(sb);
13774 +       return err;
13775 +}
13776 +
13777 +/* ---------------------------------------------------------------------- */
13778 +
13779 +static struct export_operations aufs_export_op = {
13780 +       .fh_to_dentry           = aufs_fh_to_dentry,
13781 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13782 +       .encode_fh              = aufs_encode_fh,
13783 +       .commit_metadata        = aufs_commit_metadata
13784 +};
13785 +
13786 +void au_export_init(struct super_block *sb)
13787 +{
13788 +       struct au_sbinfo *sbinfo;
13789 +       __u32 u;
13790 +
13791 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13792 +                        && IS_MODULE(CONFIG_EXPORTFS),
13793 +                        AUFS_NAME ": unsupported configuration "
13794 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13795 +
13796 +       sb->s_export_op = &aufs_export_op;
13797 +       sbinfo = au_sbi(sb);
13798 +       sbinfo->si_xigen = NULL;
13799 +       get_random_bytes(&u, sizeof(u));
13800 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13801 +       atomic_set(&sbinfo->si_xigen_next, u);
13802 +}
13803 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13804 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13805 +++ linux/fs/aufs/fhsm.c        2018-10-23 12:33:35.596042364 +0200
13806 @@ -0,0 +1,427 @@
13807 +// SPDX-License-Identifier: GPL-2.0
13808 +/*
13809 + * Copyright (C) 2011-2018 Junjiro R. Okajima
13810 + *
13811 + * This program, aufs is free software; you can redistribute it and/or modify
13812 + * it under the terms of the GNU General Public License as published by
13813 + * the Free Software Foundation; either version 2 of the License, or
13814 + * (at your option) any later version.
13815 + *
13816 + * This program is distributed in the hope that it will be useful,
13817 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13818 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13819 + * GNU General Public License for more details.
13820 + *
13821 + * You should have received a copy of the GNU General Public License
13822 + * along with this program; if not, write to the Free Software
13823 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13824 + */
13825 +
13826 +/*
13827 + * File-based Hierarchy Storage Management
13828 + */
13829 +
13830 +#include <linux/anon_inodes.h>
13831 +#include <linux/poll.h>
13832 +#include <linux/seq_file.h>
13833 +#include <linux/statfs.h>
13834 +#include "aufs.h"
13835 +
13836 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13837 +{
13838 +       struct au_sbinfo *sbinfo;
13839 +       struct au_fhsm *fhsm;
13840 +
13841 +       SiMustAnyLock(sb);
13842 +
13843 +       sbinfo = au_sbi(sb);
13844 +       fhsm = &sbinfo->si_fhsm;
13845 +       AuDebugOn(!fhsm);
13846 +       return fhsm->fhsm_bottom;
13847 +}
13848 +
13849 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13850 +{
13851 +       struct au_sbinfo *sbinfo;
13852 +       struct au_fhsm *fhsm;
13853 +
13854 +       SiMustWriteLock(sb);
13855 +
13856 +       sbinfo = au_sbi(sb);
13857 +       fhsm = &sbinfo->si_fhsm;
13858 +       AuDebugOn(!fhsm);
13859 +       fhsm->fhsm_bottom = bindex;
13860 +}
13861 +
13862 +/* ---------------------------------------------------------------------- */
13863 +
13864 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13865 +{
13866 +       struct au_br_fhsm *bf;
13867 +
13868 +       bf = br->br_fhsm;
13869 +       MtxMustLock(&bf->bf_lock);
13870 +
13871 +       return !bf->bf_readable
13872 +               || time_after(jiffies,
13873 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13874 +}
13875 +
13876 +/* ---------------------------------------------------------------------- */
13877 +
13878 +static void au_fhsm_notify(struct super_block *sb, int val)
13879 +{
13880 +       struct au_sbinfo *sbinfo;
13881 +       struct au_fhsm *fhsm;
13882 +
13883 +       SiMustAnyLock(sb);
13884 +
13885 +       sbinfo = au_sbi(sb);
13886 +       fhsm = &sbinfo->si_fhsm;
13887 +       if (au_fhsm_pid(fhsm)
13888 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13889 +               atomic_set(&fhsm->fhsm_readable, val);
13890 +               if (val)
13891 +                       wake_up(&fhsm->fhsm_wqh);
13892 +       }
13893 +}
13894 +
13895 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13896 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13897 +{
13898 +       int err;
13899 +       struct au_branch *br;
13900 +       struct au_br_fhsm *bf;
13901 +
13902 +       br = au_sbr(sb, bindex);
13903 +       AuDebugOn(au_br_rdonly(br));
13904 +       bf = br->br_fhsm;
13905 +       AuDebugOn(!bf);
13906 +
13907 +       if (do_lock)
13908 +               mutex_lock(&bf->bf_lock);
13909 +       else
13910 +               MtxMustLock(&bf->bf_lock);
13911 +
13912 +       /* sb->s_root for NFS is unreliable */
13913 +       err = au_br_stfs(br, &bf->bf_stfs);
13914 +       if (unlikely(err)) {
13915 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13916 +               goto out;
13917 +       }
13918 +
13919 +       bf->bf_jiffy = jiffies;
13920 +       bf->bf_readable = 1;
13921 +       if (do_notify)
13922 +               au_fhsm_notify(sb, /*val*/1);
13923 +       if (rstfs)
13924 +               *rstfs = bf->bf_stfs;
13925 +
13926 +out:
13927 +       if (do_lock)
13928 +               mutex_unlock(&bf->bf_lock);
13929 +       au_fhsm_notify(sb, /*val*/1);
13930 +
13931 +       return err;
13932 +}
13933 +
13934 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13935 +{
13936 +       int err;
13937 +       struct au_sbinfo *sbinfo;
13938 +       struct au_fhsm *fhsm;
13939 +       struct au_branch *br;
13940 +       struct au_br_fhsm *bf;
13941 +
13942 +       AuDbg("b%d, force %d\n", bindex, force);
13943 +       SiMustAnyLock(sb);
13944 +
13945 +       sbinfo = au_sbi(sb);
13946 +       fhsm = &sbinfo->si_fhsm;
13947 +       if (!au_ftest_si(sbinfo, FHSM)
13948 +           || fhsm->fhsm_bottom == bindex)
13949 +               return;
13950 +
13951 +       br = au_sbr(sb, bindex);
13952 +       bf = br->br_fhsm;
13953 +       AuDebugOn(!bf);
13954 +       mutex_lock(&bf->bf_lock);
13955 +       if (force
13956 +           || au_fhsm_pid(fhsm)
13957 +           || au_fhsm_test_jiffy(sbinfo, br))
13958 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13959 +                                 /*do_notify*/1);
13960 +       mutex_unlock(&bf->bf_lock);
13961 +}
13962 +
13963 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13964 +{
13965 +       aufs_bindex_t bindex, bbot;
13966 +       struct au_branch *br;
13967 +
13968 +       /* exclude the bottom */
13969 +       bbot = au_fhsm_bottom(sb);
13970 +       for (bindex = 0; bindex < bbot; bindex++) {
13971 +               br = au_sbr(sb, bindex);
13972 +               if (au_br_fhsm(br->br_perm))
13973 +                       au_fhsm_wrote(sb, bindex, force);
13974 +       }
13975 +}
13976 +
13977 +/* ---------------------------------------------------------------------- */
13978 +
13979 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13980 +{
13981 +       __poll_t mask;
13982 +       struct au_sbinfo *sbinfo;
13983 +       struct au_fhsm *fhsm;
13984 +
13985 +       mask = 0;
13986 +       sbinfo = file->private_data;
13987 +       fhsm = &sbinfo->si_fhsm;
13988 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13989 +       if (atomic_read(&fhsm->fhsm_readable))
13990 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13991 +
13992 +       if (!mask)
13993 +               AuDbg("mask 0x%x\n", mask);
13994 +       return mask;
13995 +}
13996 +
13997 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13998 +                             struct aufs_stfs *stfs, __s16 brid)
13999 +{
14000 +       int err;
14001 +
14002 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
14003 +       if (!err)
14004 +               err = __put_user(brid, &stbr->brid);
14005 +       if (unlikely(err))
14006 +               err = -EFAULT;
14007 +
14008 +       return err;
14009 +}
14010 +
14011 +static ssize_t au_fhsm_do_read(struct super_block *sb,
14012 +                              struct aufs_stbr __user *stbr, size_t count)
14013 +{
14014 +       ssize_t err;
14015 +       int nstbr;
14016 +       aufs_bindex_t bindex, bbot;
14017 +       struct au_branch *br;
14018 +       struct au_br_fhsm *bf;
14019 +
14020 +       /* except the bottom branch */
14021 +       err = 0;
14022 +       nstbr = 0;
14023 +       bbot = au_fhsm_bottom(sb);
14024 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
14025 +               br = au_sbr(sb, bindex);
14026 +               if (!au_br_fhsm(br->br_perm))
14027 +                       continue;
14028 +
14029 +               bf = br->br_fhsm;
14030 +               mutex_lock(&bf->bf_lock);
14031 +               if (bf->bf_readable) {
14032 +                       err = -EFAULT;
14033 +                       if (count >= sizeof(*stbr))
14034 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
14035 +                                                         br->br_id);
14036 +                       if (!err) {
14037 +                               bf->bf_readable = 0;
14038 +                               count -= sizeof(*stbr);
14039 +                               nstbr++;
14040 +                       }
14041 +               }
14042 +               mutex_unlock(&bf->bf_lock);
14043 +       }
14044 +       if (!err)
14045 +               err = sizeof(*stbr) * nstbr;
14046 +
14047 +       return err;
14048 +}
14049 +
14050 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
14051 +                          loff_t *pos)
14052 +{
14053 +       ssize_t err;
14054 +       int readable;
14055 +       aufs_bindex_t nfhsm, bindex, bbot;
14056 +       struct au_sbinfo *sbinfo;
14057 +       struct au_fhsm *fhsm;
14058 +       struct au_branch *br;
14059 +       struct super_block *sb;
14060 +
14061 +       err = 0;
14062 +       sbinfo = file->private_data;
14063 +       fhsm = &sbinfo->si_fhsm;
14064 +need_data:
14065 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
14066 +       if (!atomic_read(&fhsm->fhsm_readable)) {
14067 +               if (vfsub_file_flags(file) & O_NONBLOCK)
14068 +                       err = -EAGAIN;
14069 +               else
14070 +                       err = wait_event_interruptible_locked_irq
14071 +                               (fhsm->fhsm_wqh,
14072 +                                atomic_read(&fhsm->fhsm_readable));
14073 +       }
14074 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
14075 +       if (unlikely(err))
14076 +               goto out;
14077 +
14078 +       /* sb may already be dead */
14079 +       au_rw_read_lock(&sbinfo->si_rwsem);
14080 +       readable = atomic_read(&fhsm->fhsm_readable);
14081 +       if (readable > 0) {
14082 +               sb = sbinfo->si_sb;
14083 +               AuDebugOn(!sb);
14084 +               /* exclude the bottom branch */
14085 +               nfhsm = 0;
14086 +               bbot = au_fhsm_bottom(sb);
14087 +               for (bindex = 0; bindex < bbot; bindex++) {
14088 +                       br = au_sbr(sb, bindex);
14089 +                       if (au_br_fhsm(br->br_perm))
14090 +                               nfhsm++;
14091 +               }
14092 +               err = -EMSGSIZE;
14093 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
14094 +                       atomic_set(&fhsm->fhsm_readable, 0);
14095 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
14096 +                                            count);
14097 +               }
14098 +       }
14099 +       au_rw_read_unlock(&sbinfo->si_rwsem);
14100 +       if (!readable)
14101 +               goto need_data;
14102 +
14103 +out:
14104 +       return err;
14105 +}
14106 +
14107 +static int au_fhsm_release(struct inode *inode, struct file *file)
14108 +{
14109 +       struct au_sbinfo *sbinfo;
14110 +       struct au_fhsm *fhsm;
14111 +
14112 +       /* sb may already be dead */
14113 +       sbinfo = file->private_data;
14114 +       fhsm = &sbinfo->si_fhsm;
14115 +       spin_lock(&fhsm->fhsm_spin);
14116 +       fhsm->fhsm_pid = 0;
14117 +       spin_unlock(&fhsm->fhsm_spin);
14118 +       kobject_put(&sbinfo->si_kobj);
14119 +
14120 +       return 0;
14121 +}
14122 +
14123 +static const struct file_operations au_fhsm_fops = {
14124 +       .owner          = THIS_MODULE,
14125 +       .llseek         = noop_llseek,
14126 +       .read           = au_fhsm_read,
14127 +       .poll           = au_fhsm_poll,
14128 +       .release        = au_fhsm_release
14129 +};
14130 +
14131 +int au_fhsm_fd(struct super_block *sb, int oflags)
14132 +{
14133 +       int err, fd;
14134 +       struct au_sbinfo *sbinfo;
14135 +       struct au_fhsm *fhsm;
14136 +
14137 +       err = -EPERM;
14138 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14139 +               goto out;
14140 +
14141 +       err = -EINVAL;
14142 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14143 +               goto out;
14144 +
14145 +       err = 0;
14146 +       sbinfo = au_sbi(sb);
14147 +       fhsm = &sbinfo->si_fhsm;
14148 +       spin_lock(&fhsm->fhsm_spin);
14149 +       if (!fhsm->fhsm_pid)
14150 +               fhsm->fhsm_pid = current->pid;
14151 +       else
14152 +               err = -EBUSY;
14153 +       spin_unlock(&fhsm->fhsm_spin);
14154 +       if (unlikely(err))
14155 +               goto out;
14156 +
14157 +       oflags |= O_RDONLY;
14158 +       /* oflags |= FMODE_NONOTIFY; */
14159 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14160 +       err = fd;
14161 +       if (unlikely(fd < 0))
14162 +               goto out_pid;
14163 +
14164 +       /* succeed regardless 'fhsm' status */
14165 +       kobject_get(&sbinfo->si_kobj);
14166 +       si_noflush_read_lock(sb);
14167 +       if (au_ftest_si(sbinfo, FHSM))
14168 +               au_fhsm_wrote_all(sb, /*force*/0);
14169 +       si_read_unlock(sb);
14170 +       goto out; /* success */
14171 +
14172 +out_pid:
14173 +       spin_lock(&fhsm->fhsm_spin);
14174 +       fhsm->fhsm_pid = 0;
14175 +       spin_unlock(&fhsm->fhsm_spin);
14176 +out:
14177 +       AuTraceErr(err);
14178 +       return err;
14179 +}
14180 +
14181 +/* ---------------------------------------------------------------------- */
14182 +
14183 +int au_fhsm_br_alloc(struct au_branch *br)
14184 +{
14185 +       int err;
14186 +
14187 +       err = 0;
14188 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14189 +       if (br->br_fhsm)
14190 +               au_br_fhsm_init(br->br_fhsm);
14191 +       else
14192 +               err = -ENOMEM;
14193 +
14194 +       return err;
14195 +}
14196 +
14197 +/* ---------------------------------------------------------------------- */
14198 +
14199 +void au_fhsm_fin(struct super_block *sb)
14200 +{
14201 +       au_fhsm_notify(sb, /*val*/-1);
14202 +}
14203 +
14204 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14205 +{
14206 +       struct au_fhsm *fhsm;
14207 +
14208 +       fhsm = &sbinfo->si_fhsm;
14209 +       spin_lock_init(&fhsm->fhsm_spin);
14210 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14211 +       atomic_set(&fhsm->fhsm_readable, 0);
14212 +       fhsm->fhsm_expire
14213 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14214 +       fhsm->fhsm_bottom = -1;
14215 +}
14216 +
14217 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14218 +{
14219 +       sbinfo->si_fhsm.fhsm_expire
14220 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14221 +}
14222 +
14223 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14224 +{
14225 +       unsigned int u;
14226 +
14227 +       if (!au_ftest_si(sbinfo, FHSM))
14228 +               return;
14229 +
14230 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14231 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14232 +               seq_printf(seq, ",fhsm_sec=%u", u);
14233 +}
14234 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14235 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14236 +++ linux/fs/aufs/file.c        2018-10-23 12:33:35.596042364 +0200
14237 @@ -0,0 +1,863 @@
14238 +// SPDX-License-Identifier: GPL-2.0
14239 +/*
14240 + * Copyright (C) 2005-2018 Junjiro R. Okajima
14241 + *
14242 + * This program, aufs is free software; you can redistribute it and/or modify
14243 + * it under the terms of the GNU General Public License as published by
14244 + * the Free Software Foundation; either version 2 of the License, or
14245 + * (at your option) any later version.
14246 + *
14247 + * This program is distributed in the hope that it will be useful,
14248 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14249 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14250 + * GNU General Public License for more details.
14251 + *
14252 + * You should have received a copy of the GNU General Public License
14253 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14254 + */
14255 +
14256 +/*
14257 + * handling file/dir, and address_space operation
14258 + */
14259 +
14260 +#ifdef CONFIG_AUFS_DEBUG
14261 +#include <linux/migrate.h>
14262 +#endif
14263 +#include <linux/pagemap.h>
14264 +#include "aufs.h"
14265 +
14266 +/* drop flags for writing */
14267 +unsigned int au_file_roflags(unsigned int flags)
14268 +{
14269 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14270 +       flags |= O_RDONLY | O_NOATIME;
14271 +       return flags;
14272 +}
14273 +
14274 +/* common functions to regular file and dir */
14275 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14276 +                      struct file *file, int force_wr)
14277 +{
14278 +       struct file *h_file;
14279 +       struct dentry *h_dentry;
14280 +       struct inode *h_inode;
14281 +       struct super_block *sb;
14282 +       struct au_branch *br;
14283 +       struct path h_path;
14284 +       int err;
14285 +
14286 +       /* a race condition can happen between open and unlink/rmdir */
14287 +       h_file = ERR_PTR(-ENOENT);
14288 +       h_dentry = au_h_dptr(dentry, bindex);
14289 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14290 +               goto out;
14291 +       h_inode = d_inode(h_dentry);
14292 +       spin_lock(&h_dentry->d_lock);
14293 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14294 +               /* || !d_inode(dentry)->i_nlink */
14295 +               ;
14296 +       spin_unlock(&h_dentry->d_lock);
14297 +       if (unlikely(err))
14298 +               goto out;
14299 +
14300 +       sb = dentry->d_sb;
14301 +       br = au_sbr(sb, bindex);
14302 +       err = au_br_test_oflag(flags, br);
14303 +       h_file = ERR_PTR(err);
14304 +       if (unlikely(err))
14305 +               goto out;
14306 +
14307 +       /* drop flags for writing */
14308 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14309 +               if (force_wr && !(flags & O_WRONLY))
14310 +                       force_wr = 0;
14311 +               flags = au_file_roflags(flags);
14312 +               if (force_wr) {
14313 +                       h_file = ERR_PTR(-EROFS);
14314 +                       flags = au_file_roflags(flags);
14315 +                       if (unlikely(vfsub_native_ro(h_inode)
14316 +                                    || IS_APPEND(h_inode)))
14317 +                               goto out;
14318 +                       flags &= ~O_ACCMODE;
14319 +                       flags |= O_WRONLY;
14320 +               }
14321 +       }
14322 +       flags &= ~O_CREAT;
14323 +       au_lcnt_inc(&br->br_nfiles);
14324 +       h_path.dentry = h_dentry;
14325 +       h_path.mnt = au_br_mnt(br);
14326 +       h_file = vfsub_dentry_open(&h_path, flags);
14327 +       if (IS_ERR(h_file))
14328 +               goto out_br;
14329 +
14330 +       if (flags & __FMODE_EXEC) {
14331 +               err = deny_write_access(h_file);
14332 +               if (unlikely(err)) {
14333 +                       fput(h_file);
14334 +                       h_file = ERR_PTR(err);
14335 +                       goto out_br;
14336 +               }
14337 +       }
14338 +       fsnotify_open(h_file);
14339 +       goto out; /* success */
14340 +
14341 +out_br:
14342 +       au_lcnt_dec(&br->br_nfiles);
14343 +out:
14344 +       return h_file;
14345 +}
14346 +
14347 +static int au_cmoo(struct dentry *dentry)
14348 +{
14349 +       int err, cmoo, matched;
14350 +       unsigned int udba;
14351 +       struct path h_path;
14352 +       struct au_pin pin;
14353 +       struct au_cp_generic cpg = {
14354 +               .dentry = dentry,
14355 +               .bdst   = -1,
14356 +               .bsrc   = -1,
14357 +               .len    = -1,
14358 +               .pin    = &pin,
14359 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14360 +       };
14361 +       struct inode *delegated;
14362 +       struct super_block *sb;
14363 +       struct au_sbinfo *sbinfo;
14364 +       struct au_fhsm *fhsm;
14365 +       pid_t pid;
14366 +       struct au_branch *br;
14367 +       struct dentry *parent;
14368 +       struct au_hinode *hdir;
14369 +
14370 +       DiMustWriteLock(dentry);
14371 +       IiMustWriteLock(d_inode(dentry));
14372 +
14373 +       err = 0;
14374 +       if (IS_ROOT(dentry))
14375 +               goto out;
14376 +       cpg.bsrc = au_dbtop(dentry);
14377 +       if (!cpg.bsrc)
14378 +               goto out;
14379 +
14380 +       sb = dentry->d_sb;
14381 +       sbinfo = au_sbi(sb);
14382 +       fhsm = &sbinfo->si_fhsm;
14383 +       pid = au_fhsm_pid(fhsm);
14384 +       rcu_read_lock();
14385 +       matched = (pid
14386 +                  && (current->pid == pid
14387 +                      || rcu_dereference(current->real_parent)->pid == pid));
14388 +       rcu_read_unlock();
14389 +       if (matched)
14390 +               goto out;
14391 +
14392 +       br = au_sbr(sb, cpg.bsrc);
14393 +       cmoo = au_br_cmoo(br->br_perm);
14394 +       if (!cmoo)
14395 +               goto out;
14396 +       if (!d_is_reg(dentry))
14397 +               cmoo &= AuBrAttr_COO_ALL;
14398 +       if (!cmoo)
14399 +               goto out;
14400 +
14401 +       parent = dget_parent(dentry);
14402 +       di_write_lock_parent(parent);
14403 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14404 +       cpg.bdst = err;
14405 +       if (unlikely(err < 0)) {
14406 +               err = 0;        /* there is no upper writable branch */
14407 +               goto out_dgrade;
14408 +       }
14409 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14410 +
14411 +       /* do not respect the coo attrib for the target branch */
14412 +       err = au_cpup_dirs(dentry, cpg.bdst);
14413 +       if (unlikely(err))
14414 +               goto out_dgrade;
14415 +
14416 +       di_downgrade_lock(parent, AuLock_IR);
14417 +       udba = au_opt_udba(sb);
14418 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14419 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14420 +       if (unlikely(err))
14421 +               goto out_parent;
14422 +
14423 +       err = au_sio_cpup_simple(&cpg);
14424 +       au_unpin(&pin);
14425 +       if (unlikely(err))
14426 +               goto out_parent;
14427 +       if (!(cmoo & AuBrWAttr_MOO))
14428 +               goto out_parent; /* success */
14429 +
14430 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14431 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14432 +       if (unlikely(err))
14433 +               goto out_parent;
14434 +
14435 +       h_path.mnt = au_br_mnt(br);
14436 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14437 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14438 +       delegated = NULL;
14439 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14440 +       au_unpin(&pin);
14441 +       /* todo: keep h_dentry or not? */
14442 +       if (unlikely(err == -EWOULDBLOCK)) {
14443 +               pr_warn("cannot retry for NFSv4 delegation"
14444 +                       " for an internal unlink\n");
14445 +               iput(delegated);
14446 +       }
14447 +       if (unlikely(err)) {
14448 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14449 +                      dentry, err);
14450 +               err = 0;
14451 +       }
14452 +       goto out_parent; /* success */
14453 +
14454 +out_dgrade:
14455 +       di_downgrade_lock(parent, AuLock_IR);
14456 +out_parent:
14457 +       di_read_unlock(parent, AuLock_IR);
14458 +       dput(parent);
14459 +out:
14460 +       AuTraceErr(err);
14461 +       return err;
14462 +}
14463 +
14464 +int au_do_open(struct file *file, struct au_do_open_args *args)
14465 +{
14466 +       int err, aopen = args->aopen;
14467 +       struct dentry *dentry;
14468 +       struct au_finfo *finfo;
14469 +
14470 +       if (!aopen)
14471 +               err = au_finfo_init(file, args->fidir);
14472 +       else {
14473 +               lockdep_off();
14474 +               err = au_finfo_init(file, args->fidir);
14475 +               lockdep_on();
14476 +       }
14477 +       if (unlikely(err))
14478 +               goto out;
14479 +
14480 +       dentry = file->f_path.dentry;
14481 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14482 +       di_write_lock_child(dentry);
14483 +       err = au_cmoo(dentry);
14484 +       di_downgrade_lock(dentry, AuLock_IR);
14485 +       if (!err) {
14486 +               if (!aopen)
14487 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14488 +               else {
14489 +                       lockdep_off();
14490 +                       err = args->open(file, vfsub_file_flags(file),
14491 +                                        args->h_file);
14492 +                       lockdep_on();
14493 +               }
14494 +       }
14495 +       di_read_unlock(dentry, AuLock_IR);
14496 +
14497 +       finfo = au_fi(file);
14498 +       if (!err) {
14499 +               finfo->fi_file = file;
14500 +               au_hbl_add(&finfo->fi_hlist,
14501 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14502 +       }
14503 +       if (!aopen)
14504 +               fi_write_unlock(file);
14505 +       else {
14506 +               lockdep_off();
14507 +               fi_write_unlock(file);
14508 +               lockdep_on();
14509 +       }
14510 +       if (unlikely(err)) {
14511 +               finfo->fi_hdir = NULL;
14512 +               au_finfo_fin(file);
14513 +       }
14514 +
14515 +out:
14516 +       AuTraceErr(err);
14517 +       return err;
14518 +}
14519 +
14520 +int au_reopen_nondir(struct file *file)
14521 +{
14522 +       int err;
14523 +       aufs_bindex_t btop;
14524 +       struct dentry *dentry;
14525 +       struct au_branch *br;
14526 +       struct file *h_file, *h_file_tmp;
14527 +
14528 +       dentry = file->f_path.dentry;
14529 +       btop = au_dbtop(dentry);
14530 +       br = au_sbr(dentry->d_sb, btop);
14531 +       h_file_tmp = NULL;
14532 +       if (au_fbtop(file) == btop) {
14533 +               h_file = au_hf_top(file);
14534 +               if (file->f_mode == h_file->f_mode)
14535 +                       return 0; /* success */
14536 +               h_file_tmp = h_file;
14537 +               get_file(h_file_tmp);
14538 +               au_lcnt_inc(&br->br_nfiles);
14539 +               au_set_h_fptr(file, btop, NULL);
14540 +       }
14541 +       AuDebugOn(au_fi(file)->fi_hdir);
14542 +       /*
14543 +        * it can happen
14544 +        * file exists on both of rw and ro
14545 +        * open --> dbtop and fbtop are both 0
14546 +        * prepend a branch as rw, "rw" become ro
14547 +        * remove rw/file
14548 +        * delete the top branch, "rw" becomes rw again
14549 +        *      --> dbtop is 1, fbtop is still 0
14550 +        * write --> fbtop is 0 but dbtop is 1
14551 +        */
14552 +       /* AuDebugOn(au_fbtop(file) < btop); */
14553 +
14554 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14555 +                          file, /*force_wr*/0);
14556 +       err = PTR_ERR(h_file);
14557 +       if (IS_ERR(h_file)) {
14558 +               if (h_file_tmp) {
14559 +                       /* revert */
14560 +                       au_set_h_fptr(file, btop, h_file_tmp);
14561 +                       h_file_tmp = NULL;
14562 +               }
14563 +               goto out; /* todo: close all? */
14564 +       }
14565 +
14566 +       err = 0;
14567 +       au_set_fbtop(file, btop);
14568 +       au_set_h_fptr(file, btop, h_file);
14569 +       au_update_figen(file);
14570 +       /* todo: necessary? */
14571 +       /* file->f_ra = h_file->f_ra; */
14572 +
14573 +out:
14574 +       if (h_file_tmp) {
14575 +               fput(h_file_tmp);
14576 +               au_lcnt_dec(&br->br_nfiles);
14577 +       }
14578 +       return err;
14579 +}
14580 +
14581 +/* ---------------------------------------------------------------------- */
14582 +
14583 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14584 +                       struct dentry *hi_wh)
14585 +{
14586 +       int err;
14587 +       aufs_bindex_t btop;
14588 +       struct au_dinfo *dinfo;
14589 +       struct dentry *h_dentry;
14590 +       struct au_hdentry *hdp;
14591 +
14592 +       dinfo = au_di(file->f_path.dentry);
14593 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14594 +
14595 +       btop = dinfo->di_btop;
14596 +       dinfo->di_btop = btgt;
14597 +       hdp = au_hdentry(dinfo, btgt);
14598 +       h_dentry = hdp->hd_dentry;
14599 +       hdp->hd_dentry = hi_wh;
14600 +       err = au_reopen_nondir(file);
14601 +       hdp->hd_dentry = h_dentry;
14602 +       dinfo->di_btop = btop;
14603 +
14604 +       return err;
14605 +}
14606 +
14607 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14608 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14609 +{
14610 +       int err;
14611 +       struct inode *inode, *h_inode;
14612 +       struct dentry *h_dentry, *hi_wh;
14613 +       struct au_cp_generic cpg = {
14614 +               .dentry = file->f_path.dentry,
14615 +               .bdst   = bcpup,
14616 +               .bsrc   = -1,
14617 +               .len    = len,
14618 +               .pin    = pin
14619 +       };
14620 +
14621 +       au_update_dbtop(cpg.dentry);
14622 +       inode = d_inode(cpg.dentry);
14623 +       h_inode = NULL;
14624 +       if (au_dbtop(cpg.dentry) <= bcpup
14625 +           && au_dbbot(cpg.dentry) >= bcpup) {
14626 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14627 +               if (h_dentry && d_is_positive(h_dentry))
14628 +                       h_inode = d_inode(h_dentry);
14629 +       }
14630 +       hi_wh = au_hi_wh(inode, bcpup);
14631 +       if (!hi_wh && !h_inode)
14632 +               err = au_sio_cpup_wh(&cpg, file);
14633 +       else
14634 +               /* already copied-up after unlink */
14635 +               err = au_reopen_wh(file, bcpup, hi_wh);
14636 +
14637 +       if (!err
14638 +           && (inode->i_nlink > 1
14639 +               || (inode->i_state & I_LINKABLE))
14640 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14641 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14642 +
14643 +       return err;
14644 +}
14645 +
14646 +/*
14647 + * prepare the @file for writing.
14648 + */
14649 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14650 +{
14651 +       int err;
14652 +       aufs_bindex_t dbtop;
14653 +       struct dentry *parent;
14654 +       struct inode *inode;
14655 +       struct super_block *sb;
14656 +       struct file *h_file;
14657 +       struct au_cp_generic cpg = {
14658 +               .dentry = file->f_path.dentry,
14659 +               .bdst   = -1,
14660 +               .bsrc   = -1,
14661 +               .len    = len,
14662 +               .pin    = pin,
14663 +               .flags  = AuCpup_DTIME
14664 +       };
14665 +
14666 +       sb = cpg.dentry->d_sb;
14667 +       inode = d_inode(cpg.dentry);
14668 +       cpg.bsrc = au_fbtop(file);
14669 +       err = au_test_ro(sb, cpg.bsrc, inode);
14670 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14671 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14672 +                            /*flags*/0);
14673 +               goto out;
14674 +       }
14675 +
14676 +       /* need to cpup or reopen */
14677 +       parent = dget_parent(cpg.dentry);
14678 +       di_write_lock_parent(parent);
14679 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14680 +       cpg.bdst = err;
14681 +       if (unlikely(err < 0))
14682 +               goto out_dgrade;
14683 +       err = 0;
14684 +
14685 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14686 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14687 +               if (unlikely(err))
14688 +                       goto out_dgrade;
14689 +       }
14690 +
14691 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14692 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14693 +       if (unlikely(err))
14694 +               goto out_dgrade;
14695 +
14696 +       dbtop = au_dbtop(cpg.dentry);
14697 +       if (dbtop <= cpg.bdst)
14698 +               cpg.bsrc = cpg.bdst;
14699 +
14700 +       if (dbtop <= cpg.bdst           /* just reopen */
14701 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14702 +               ) {
14703 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14704 +               if (IS_ERR(h_file))
14705 +                       err = PTR_ERR(h_file);
14706 +               else {
14707 +                       di_downgrade_lock(parent, AuLock_IR);
14708 +                       if (dbtop > cpg.bdst)
14709 +                               err = au_sio_cpup_simple(&cpg);
14710 +                       if (!err)
14711 +                               err = au_reopen_nondir(file);
14712 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14713 +               }
14714 +       } else {                        /* copyup as wh and reopen */
14715 +               /*
14716 +                * since writable hfsplus branch is not supported,
14717 +                * h_open_pre/post() are unnecessary.
14718 +                */
14719 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14720 +               di_downgrade_lock(parent, AuLock_IR);
14721 +       }
14722 +
14723 +       if (!err) {
14724 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14725 +               goto out_dput; /* success */
14726 +       }
14727 +       au_unpin(pin);
14728 +       goto out_unlock;
14729 +
14730 +out_dgrade:
14731 +       di_downgrade_lock(parent, AuLock_IR);
14732 +out_unlock:
14733 +       di_read_unlock(parent, AuLock_IR);
14734 +out_dput:
14735 +       dput(parent);
14736 +out:
14737 +       return err;
14738 +}
14739 +
14740 +/* ---------------------------------------------------------------------- */
14741 +
14742 +int au_do_flush(struct file *file, fl_owner_t id,
14743 +               int (*flush)(struct file *file, fl_owner_t id))
14744 +{
14745 +       int err;
14746 +       struct super_block *sb;
14747 +       struct inode *inode;
14748 +
14749 +       inode = file_inode(file);
14750 +       sb = inode->i_sb;
14751 +       si_noflush_read_lock(sb);
14752 +       fi_read_lock(file);
14753 +       ii_read_lock_child(inode);
14754 +
14755 +       err = flush(file, id);
14756 +       au_cpup_attr_timesizes(inode);
14757 +
14758 +       ii_read_unlock(inode);
14759 +       fi_read_unlock(file);
14760 +       si_read_unlock(sb);
14761 +       return err;
14762 +}
14763 +
14764 +/* ---------------------------------------------------------------------- */
14765 +
14766 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14767 +{
14768 +       int err;
14769 +       struct au_pin pin;
14770 +       struct au_finfo *finfo;
14771 +       struct dentry *parent, *hi_wh;
14772 +       struct inode *inode;
14773 +       struct super_block *sb;
14774 +       struct au_cp_generic cpg = {
14775 +               .dentry = file->f_path.dentry,
14776 +               .bdst   = -1,
14777 +               .bsrc   = -1,
14778 +               .len    = -1,
14779 +               .pin    = &pin,
14780 +               .flags  = AuCpup_DTIME
14781 +       };
14782 +
14783 +       FiMustWriteLock(file);
14784 +
14785 +       err = 0;
14786 +       finfo = au_fi(file);
14787 +       sb = cpg.dentry->d_sb;
14788 +       inode = d_inode(cpg.dentry);
14789 +       cpg.bdst = au_ibtop(inode);
14790 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14791 +               goto out;
14792 +
14793 +       parent = dget_parent(cpg.dentry);
14794 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14795 +               di_read_lock_parent(parent, !AuLock_IR);
14796 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14797 +               cpg.bdst = err;
14798 +               di_read_unlock(parent, !AuLock_IR);
14799 +               if (unlikely(err < 0))
14800 +                       goto out_parent;
14801 +               err = 0;
14802 +       }
14803 +
14804 +       di_read_lock_parent(parent, AuLock_IR);
14805 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14806 +       if (!S_ISDIR(inode->i_mode)
14807 +           && au_opt_test(au_mntflags(sb), PLINK)
14808 +           && au_plink_test(inode)
14809 +           && !d_unhashed(cpg.dentry)
14810 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14811 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14812 +               if (unlikely(err))
14813 +                       goto out_unlock;
14814 +
14815 +               /* always superio. */
14816 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14817 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14818 +               if (!err) {
14819 +                       err = au_sio_cpup_simple(&cpg);
14820 +                       au_unpin(&pin);
14821 +               }
14822 +       } else if (hi_wh) {
14823 +               /* already copied-up after unlink */
14824 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14825 +               *need_reopen = 0;
14826 +       }
14827 +
14828 +out_unlock:
14829 +       di_read_unlock(parent, AuLock_IR);
14830 +out_parent:
14831 +       dput(parent);
14832 +out:
14833 +       return err;
14834 +}
14835 +
14836 +static void au_do_refresh_dir(struct file *file)
14837 +{
14838 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14839 +       struct au_hfile *p, tmp, *q;
14840 +       struct au_finfo *finfo;
14841 +       struct super_block *sb;
14842 +       struct au_fidir *fidir;
14843 +
14844 +       FiMustWriteLock(file);
14845 +
14846 +       sb = file->f_path.dentry->d_sb;
14847 +       finfo = au_fi(file);
14848 +       fidir = finfo->fi_hdir;
14849 +       AuDebugOn(!fidir);
14850 +       p = fidir->fd_hfile + finfo->fi_btop;
14851 +       brid = p->hf_br->br_id;
14852 +       bbot = fidir->fd_bbot;
14853 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14854 +               if (!p->hf_file)
14855 +                       continue;
14856 +
14857 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14858 +               if (new_bindex == bindex)
14859 +                       continue;
14860 +               if (new_bindex < 0) {
14861 +                       au_set_h_fptr(file, bindex, NULL);
14862 +                       continue;
14863 +               }
14864 +
14865 +               /* swap two lower inode, and loop again */
14866 +               q = fidir->fd_hfile + new_bindex;
14867 +               tmp = *q;
14868 +               *q = *p;
14869 +               *p = tmp;
14870 +               if (tmp.hf_file) {
14871 +                       bindex--;
14872 +                       p--;
14873 +               }
14874 +       }
14875 +
14876 +       p = fidir->fd_hfile;
14877 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14878 +               bbot = au_sbbot(sb);
14879 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14880 +                    finfo->fi_btop++, p++)
14881 +                       if (p->hf_file) {
14882 +                               if (file_inode(p->hf_file))
14883 +                                       break;
14884 +                               au_hfput(p, /*execed*/0);
14885 +                       }
14886 +       } else {
14887 +               bbot = au_br_index(sb, brid);
14888 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14889 +                    finfo->fi_btop++, p++)
14890 +                       if (p->hf_file)
14891 +                               au_hfput(p, /*execed*/0);
14892 +               bbot = au_sbbot(sb);
14893 +       }
14894 +
14895 +       p = fidir->fd_hfile + bbot;
14896 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14897 +            fidir->fd_bbot--, p--)
14898 +               if (p->hf_file) {
14899 +                       if (file_inode(p->hf_file))
14900 +                               break;
14901 +                       au_hfput(p, /*execed*/0);
14902 +               }
14903 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14904 +}
14905 +
14906 +/*
14907 + * after branch manipulating, refresh the file.
14908 + */
14909 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14910 +{
14911 +       int err, need_reopen, nbr;
14912 +       aufs_bindex_t bbot, bindex;
14913 +       struct dentry *dentry;
14914 +       struct super_block *sb;
14915 +       struct au_finfo *finfo;
14916 +       struct au_hfile *hfile;
14917 +
14918 +       dentry = file->f_path.dentry;
14919 +       sb = dentry->d_sb;
14920 +       nbr = au_sbbot(sb) + 1;
14921 +       finfo = au_fi(file);
14922 +       if (!finfo->fi_hdir) {
14923 +               hfile = &finfo->fi_htop;
14924 +               AuDebugOn(!hfile->hf_file);
14925 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14926 +               AuDebugOn(bindex < 0);
14927 +               if (bindex != finfo->fi_btop)
14928 +                       au_set_fbtop(file, bindex);
14929 +       } else {
14930 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14931 +               if (unlikely(err))
14932 +                       goto out;
14933 +               au_do_refresh_dir(file);
14934 +       }
14935 +
14936 +       err = 0;
14937 +       need_reopen = 1;
14938 +       if (!au_test_mmapped(file))
14939 +               err = au_file_refresh_by_inode(file, &need_reopen);
14940 +       if (finfo->fi_hdir)
14941 +               /* harmless if err */
14942 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14943 +       if (!err && need_reopen && !d_unlinked(dentry))
14944 +               err = reopen(file);
14945 +       if (!err) {
14946 +               au_update_figen(file);
14947 +               goto out; /* success */
14948 +       }
14949 +
14950 +       /* error, close all lower files */
14951 +       if (finfo->fi_hdir) {
14952 +               bbot = au_fbbot_dir(file);
14953 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14954 +                       au_set_h_fptr(file, bindex, NULL);
14955 +       }
14956 +
14957 +out:
14958 +       return err;
14959 +}
14960 +
14961 +/* common function to regular file and dir */
14962 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14963 +                         int wlock, unsigned int fi_lsc)
14964 +{
14965 +       int err;
14966 +       unsigned int sigen, figen;
14967 +       aufs_bindex_t btop;
14968 +       unsigned char pseudo_link;
14969 +       struct dentry *dentry;
14970 +       struct inode *inode;
14971 +
14972 +       err = 0;
14973 +       dentry = file->f_path.dentry;
14974 +       inode = d_inode(dentry);
14975 +       sigen = au_sigen(dentry->d_sb);
14976 +       fi_write_lock_nested(file, fi_lsc);
14977 +       figen = au_figen(file);
14978 +       if (!fi_lsc)
14979 +               di_write_lock_child(dentry);
14980 +       else
14981 +               di_write_lock_child2(dentry);
14982 +       btop = au_dbtop(dentry);
14983 +       pseudo_link = (btop != au_ibtop(inode));
14984 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14985 +               if (!wlock) {
14986 +                       di_downgrade_lock(dentry, AuLock_IR);
14987 +                       fi_downgrade_lock(file);
14988 +               }
14989 +               goto out; /* success */
14990 +       }
14991 +
14992 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14993 +       if (au_digen_test(dentry, sigen)) {
14994 +               err = au_reval_dpath(dentry, sigen);
14995 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14996 +       }
14997 +
14998 +       if (!err)
14999 +               err = refresh_file(file, reopen);
15000 +       if (!err) {
15001 +               if (!wlock) {
15002 +                       di_downgrade_lock(dentry, AuLock_IR);
15003 +                       fi_downgrade_lock(file);
15004 +               }
15005 +       } else {
15006 +               di_write_unlock(dentry);
15007 +               fi_write_unlock(file);
15008 +       }
15009 +
15010 +out:
15011 +       return err;
15012 +}
15013 +
15014 +/* ---------------------------------------------------------------------- */
15015 +
15016 +/* cf. aufs_nopage() */
15017 +/* for madvise(2) */
15018 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
15019 +{
15020 +       unlock_page(page);
15021 +       return 0;
15022 +}
15023 +
15024 +/* it will never be called, but necessary to support O_DIRECT */
15025 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
15026 +{ BUG(); return 0; }
15027 +
15028 +/* they will never be called. */
15029 +#ifdef CONFIG_AUFS_DEBUG
15030 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
15031 +                           loff_t pos, unsigned len, unsigned flags,
15032 +                           struct page **pagep, void **fsdata)
15033 +{ AuUnsupport(); return 0; }
15034 +static int aufs_write_end(struct file *file, struct address_space *mapping,
15035 +                         loff_t pos, unsigned len, unsigned copied,
15036 +                         struct page *page, void *fsdata)
15037 +{ AuUnsupport(); return 0; }
15038 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
15039 +{ AuUnsupport(); return 0; }
15040 +
15041 +static int aufs_set_page_dirty(struct page *page)
15042 +{ AuUnsupport(); return 0; }
15043 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
15044 +                               unsigned int length)
15045 +{ AuUnsupport(); }
15046 +static int aufs_releasepage(struct page *page, gfp_t gfp)
15047 +{ AuUnsupport(); return 0; }
15048 +#if 0 /* called by memory compaction regardless file */
15049 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
15050 +                           struct page *page, enum migrate_mode mode)
15051 +{ AuUnsupport(); return 0; }
15052 +#endif
15053 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
15054 +{ AuUnsupport(); return true; }
15055 +static void aufs_putback_page(struct page *page)
15056 +{ AuUnsupport(); }
15057 +static int aufs_launder_page(struct page *page)
15058 +{ AuUnsupport(); return 0; }
15059 +static int aufs_is_partially_uptodate(struct page *page,
15060 +                                     unsigned long from,
15061 +                                     unsigned long count)
15062 +{ AuUnsupport(); return 0; }
15063 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
15064 +                                   bool *writeback)
15065 +{ AuUnsupport(); }
15066 +static int aufs_error_remove_page(struct address_space *mapping,
15067 +                                 struct page *page)
15068 +{ AuUnsupport(); return 0; }
15069 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
15070 +                             sector_t *span)
15071 +{ AuUnsupport(); return 0; }
15072 +static void aufs_swap_deactivate(struct file *file)
15073 +{ AuUnsupport(); }
15074 +#endif /* CONFIG_AUFS_DEBUG */
15075 +
15076 +const struct address_space_operations aufs_aop = {
15077 +       .readpage               = aufs_readpage,
15078 +       .direct_IO              = aufs_direct_IO,
15079 +#ifdef CONFIG_AUFS_DEBUG
15080 +       .writepage              = aufs_writepage,
15081 +       /* no writepages, because of writepage */
15082 +       .set_page_dirty         = aufs_set_page_dirty,
15083 +       /* no readpages, because of readpage */
15084 +       .write_begin            = aufs_write_begin,
15085 +       .write_end              = aufs_write_end,
15086 +       /* no bmap, no block device */
15087 +       .invalidatepage         = aufs_invalidatepage,
15088 +       .releasepage            = aufs_releasepage,
15089 +       /* is fallback_migrate_page ok? */
15090 +       /* .migratepage         = aufs_migratepage, */
15091 +       .isolate_page           = aufs_isolate_page,
15092 +       .putback_page           = aufs_putback_page,
15093 +       .launder_page           = aufs_launder_page,
15094 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
15095 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
15096 +       .error_remove_page      = aufs_error_remove_page,
15097 +       .swap_activate          = aufs_swap_activate,
15098 +       .swap_deactivate        = aufs_swap_deactivate
15099 +#endif /* CONFIG_AUFS_DEBUG */
15100 +};
15101 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15102 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15103 +++ linux/fs/aufs/file.h        2018-12-27 13:19:17.708416053 +0100
15104 @@ -0,0 +1,342 @@
15105 +/* SPDX-License-Identifier: GPL-2.0 */
15106 +/*
15107 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15108 + *
15109 + * This program, aufs is free software; you can redistribute it and/or modify
15110 + * it under the terms of the GNU General Public License as published by
15111 + * the Free Software Foundation; either version 2 of the License, or
15112 + * (at your option) any later version.
15113 + *
15114 + * This program is distributed in the hope that it will be useful,
15115 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15116 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15117 + * GNU General Public License for more details.
15118 + *
15119 + * You should have received a copy of the GNU General Public License
15120 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15121 + */
15122 +
15123 +/*
15124 + * file operations
15125 + */
15126 +
15127 +#ifndef __AUFS_FILE_H__
15128 +#define __AUFS_FILE_H__
15129 +
15130 +#ifdef __KERNEL__
15131 +
15132 +#include <linux/file.h>
15133 +#include <linux/fs.h>
15134 +#include <linux/mm_types.h>
15135 +#include <linux/poll.h>
15136 +#include "rwsem.h"
15137 +
15138 +struct au_branch;
15139 +struct au_hfile {
15140 +       struct file             *hf_file;
15141 +       struct au_branch        *hf_br;
15142 +};
15143 +
15144 +struct au_vdir;
15145 +struct au_fidir {
15146 +       aufs_bindex_t           fd_bbot;
15147 +       aufs_bindex_t           fd_nent;
15148 +       struct au_vdir          *fd_vdir_cache;
15149 +       struct au_hfile         fd_hfile[];
15150 +};
15151 +
15152 +static inline int au_fidir_sz(int nent)
15153 +{
15154 +       AuDebugOn(nent < 0);
15155 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15156 +}
15157 +
15158 +struct au_finfo {
15159 +       atomic_t                fi_generation;
15160 +
15161 +       struct au_rwsem         fi_rwsem;
15162 +       aufs_bindex_t           fi_btop;
15163 +
15164 +       /* do not union them */
15165 +       struct {                                /* for non-dir */
15166 +               struct au_hfile                 fi_htop;
15167 +               atomic_t                        fi_mmapped;
15168 +       };
15169 +       struct au_fidir         *fi_hdir;       /* for dir only */
15170 +
15171 +       struct hlist_bl_node    fi_hlist;
15172 +       struct file             *fi_file;       /* very ugly */
15173 +       struct rcu_head         rcu;
15174 +} ____cacheline_aligned_in_smp;
15175 +
15176 +/* ---------------------------------------------------------------------- */
15177 +
15178 +/* file.c */
15179 +extern const struct address_space_operations aufs_aop;
15180 +unsigned int au_file_roflags(unsigned int flags);
15181 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15182 +                      struct file *file, int force_wr);
15183 +struct au_do_open_args {
15184 +       int             aopen;
15185 +       int             (*open)(struct file *file, int flags,
15186 +                               struct file *h_file);
15187 +       struct au_fidir *fidir;
15188 +       struct file     *h_file;
15189 +};
15190 +int au_do_open(struct file *file, struct au_do_open_args *args);
15191 +int au_reopen_nondir(struct file *file);
15192 +struct au_pin;
15193 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15194 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15195 +                         int wlock, unsigned int fi_lsc);
15196 +int au_do_flush(struct file *file, fl_owner_t id,
15197 +               int (*flush)(struct file *file, fl_owner_t id));
15198 +
15199 +/* poll.c */
15200 +#ifdef CONFIG_AUFS_POLL
15201 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15202 +#endif
15203 +
15204 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15205 +/* hfsplus.c */
15206 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15207 +                          int force_wr);
15208 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15209 +                   struct file *h_file);
15210 +#else
15211 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15212 +       aufs_bindex_t bindex, int force_wr)
15213 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15214 +          struct file *h_file);
15215 +#endif
15216 +
15217 +/* f_op.c */
15218 +extern const struct file_operations aufs_file_fop;
15219 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15220 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15221 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15222 +
15223 +/* finfo.c */
15224 +void au_hfput(struct au_hfile *hf, int execed);
15225 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15226 +                  struct file *h_file);
15227 +
15228 +void au_update_figen(struct file *file);
15229 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15230 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15231 +
15232 +void au_fi_init_once(void *_fi);
15233 +void au_finfo_fin(struct file *file);
15234 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15235 +
15236 +/* ioctl.c */
15237 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15238 +#ifdef CONFIG_COMPAT
15239 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15240 +                          unsigned long arg);
15241 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15242 +                             unsigned long arg);
15243 +#endif
15244 +
15245 +/* ---------------------------------------------------------------------- */
15246 +
15247 +static inline struct au_finfo *au_fi(struct file *file)
15248 +{
15249 +       return file->private_data;
15250 +}
15251 +
15252 +/* ---------------------------------------------------------------------- */
15253 +
15254 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15255 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15256 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15257 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15258 +/*
15259 +#define fi_read_trylock_nested(f) \
15260 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15261 +#define fi_write_trylock_nested(f) \
15262 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15263 +*/
15264 +
15265 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15266 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15267 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15268 +
15269 +/* lock subclass for finfo */
15270 +enum {
15271 +       AuLsc_FI_1,
15272 +       AuLsc_FI_2
15273 +};
15274 +
15275 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15276 +{
15277 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15278 +}
15279 +
15280 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15281 +{
15282 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15283 +}
15284 +
15285 +/*
15286 + * fi_read_lock_1, fi_write_lock_1,
15287 + * fi_read_lock_2, fi_write_lock_2
15288 + */
15289 +#define AuReadLockFunc(name) \
15290 +static inline void fi_read_lock_##name(struct file *f) \
15291 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15292 +
15293 +#define AuWriteLockFunc(name) \
15294 +static inline void fi_write_lock_##name(struct file *f) \
15295 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15296 +
15297 +#define AuRWLockFuncs(name) \
15298 +       AuReadLockFunc(name) \
15299 +       AuWriteLockFunc(name)
15300 +
15301 +AuRWLockFuncs(1);
15302 +AuRWLockFuncs(2);
15303 +
15304 +#undef AuReadLockFunc
15305 +#undef AuWriteLockFunc
15306 +#undef AuRWLockFuncs
15307 +
15308 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15309 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15310 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15311 +
15312 +/* ---------------------------------------------------------------------- */
15313 +
15314 +/* todo: hard/soft set? */
15315 +static inline aufs_bindex_t au_fbtop(struct file *file)
15316 +{
15317 +       FiMustAnyLock(file);
15318 +       return au_fi(file)->fi_btop;
15319 +}
15320 +
15321 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15322 +{
15323 +       FiMustAnyLock(file);
15324 +       AuDebugOn(!au_fi(file)->fi_hdir);
15325 +       return au_fi(file)->fi_hdir->fd_bbot;
15326 +}
15327 +
15328 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15329 +{
15330 +       FiMustAnyLock(file);
15331 +       AuDebugOn(!au_fi(file)->fi_hdir);
15332 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15333 +}
15334 +
15335 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15336 +{
15337 +       FiMustWriteLock(file);
15338 +       au_fi(file)->fi_btop = bindex;
15339 +}
15340 +
15341 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15342 +{
15343 +       FiMustWriteLock(file);
15344 +       AuDebugOn(!au_fi(file)->fi_hdir);
15345 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15346 +}
15347 +
15348 +static inline void au_set_fvdir_cache(struct file *file,
15349 +                                     struct au_vdir *vdir_cache)
15350 +{
15351 +       FiMustWriteLock(file);
15352 +       AuDebugOn(!au_fi(file)->fi_hdir);
15353 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15354 +}
15355 +
15356 +static inline struct file *au_hf_top(struct file *file)
15357 +{
15358 +       FiMustAnyLock(file);
15359 +       AuDebugOn(au_fi(file)->fi_hdir);
15360 +       return au_fi(file)->fi_htop.hf_file;
15361 +}
15362 +
15363 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15364 +{
15365 +       FiMustAnyLock(file);
15366 +       AuDebugOn(!au_fi(file)->fi_hdir);
15367 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15368 +}
15369 +
15370 +/* todo: memory barrier? */
15371 +static inline unsigned int au_figen(struct file *f)
15372 +{
15373 +       return atomic_read(&au_fi(f)->fi_generation);
15374 +}
15375 +
15376 +static inline void au_set_mmapped(struct file *f)
15377 +{
15378 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15379 +               return;
15380 +       pr_warn("fi_mmapped wrapped around\n");
15381 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15382 +               ;
15383 +}
15384 +
15385 +static inline void au_unset_mmapped(struct file *f)
15386 +{
15387 +       atomic_dec(&au_fi(f)->fi_mmapped);
15388 +}
15389 +
15390 +static inline int au_test_mmapped(struct file *f)
15391 +{
15392 +       return atomic_read(&au_fi(f)->fi_mmapped);
15393 +}
15394 +
15395 +/* customize vma->vm_file */
15396 +
15397 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15398 +                                      struct file *file)
15399 +{
15400 +       struct file *f;
15401 +
15402 +       f = vma->vm_file;
15403 +       get_file(file);
15404 +       vma->vm_file = file;
15405 +       fput(f);
15406 +}
15407 +
15408 +#ifdef CONFIG_MMU
15409 +#define AuDbgVmRegion(file, vma) do {} while (0)
15410 +
15411 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15412 +                                   struct file *file)
15413 +{
15414 +       au_do_vm_file_reset(vma, file);
15415 +}
15416 +#else
15417 +#define AuDbgVmRegion(file, vma) \
15418 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15419 +
15420 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15421 +                                   struct file *file)
15422 +{
15423 +       struct file *f;
15424 +
15425 +       au_do_vm_file_reset(vma, file);
15426 +       f = vma->vm_region->vm_file;
15427 +       get_file(file);
15428 +       vma->vm_region->vm_file = file;
15429 +       fput(f);
15430 +}
15431 +#endif /* CONFIG_MMU */
15432 +
15433 +/* handle vma->vm_prfile */
15434 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15435 +                                   struct file *file)
15436 +{
15437 +       get_file(file);
15438 +       vma->vm_prfile = file;
15439 +#ifndef CONFIG_MMU
15440 +       get_file(file);
15441 +       vma->vm_region->vm_prfile = file;
15442 +#endif
15443 +}
15444 +
15445 +#endif /* __KERNEL__ */
15446 +#endif /* __AUFS_FILE_H__ */
15447 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15448 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15449 +++ linux/fs/aufs/finfo.c       2018-10-23 12:33:35.596042364 +0200
15450 @@ -0,0 +1,149 @@
15451 +// SPDX-License-Identifier: GPL-2.0
15452 +/*
15453 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15454 + *
15455 + * This program, aufs is free software; you can redistribute it and/or modify
15456 + * it under the terms of the GNU General Public License as published by
15457 + * the Free Software Foundation; either version 2 of the License, or
15458 + * (at your option) any later version.
15459 + *
15460 + * This program is distributed in the hope that it will be useful,
15461 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15462 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15463 + * GNU General Public License for more details.
15464 + *
15465 + * You should have received a copy of the GNU General Public License
15466 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15467 + */
15468 +
15469 +/*
15470 + * file private data
15471 + */
15472 +
15473 +#include "aufs.h"
15474 +
15475 +void au_hfput(struct au_hfile *hf, int execed)
15476 +{
15477 +       if (execed)
15478 +               allow_write_access(hf->hf_file);
15479 +       fput(hf->hf_file);
15480 +       hf->hf_file = NULL;
15481 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15482 +       hf->hf_br = NULL;
15483 +}
15484 +
15485 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15486 +{
15487 +       struct au_finfo *finfo = au_fi(file);
15488 +       struct au_hfile *hf;
15489 +       struct au_fidir *fidir;
15490 +
15491 +       fidir = finfo->fi_hdir;
15492 +       if (!fidir) {
15493 +               AuDebugOn(finfo->fi_btop != bindex);
15494 +               hf = &finfo->fi_htop;
15495 +       } else
15496 +               hf = fidir->fd_hfile + bindex;
15497 +
15498 +       if (hf && hf->hf_file)
15499 +               au_hfput(hf, vfsub_file_execed(file));
15500 +       if (val) {
15501 +               FiMustWriteLock(file);
15502 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15503 +               hf->hf_file = val;
15504 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15505 +       }
15506 +}
15507 +
15508 +void au_update_figen(struct file *file)
15509 +{
15510 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15511 +       /* smp_mb(); */ /* atomic_set */
15512 +}
15513 +
15514 +/* ---------------------------------------------------------------------- */
15515 +
15516 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15517 +{
15518 +       struct au_fidir *fidir;
15519 +       int nbr;
15520 +
15521 +       nbr = au_sbbot(sb) + 1;
15522 +       if (nbr < 2)
15523 +               nbr = 2; /* initial allocate for 2 branches */
15524 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15525 +       if (fidir) {
15526 +               fidir->fd_bbot = -1;
15527 +               fidir->fd_nent = nbr;
15528 +       }
15529 +
15530 +       return fidir;
15531 +}
15532 +
15533 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15534 +{
15535 +       int err;
15536 +       struct au_fidir *fidir, *p;
15537 +
15538 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15539 +       fidir = finfo->fi_hdir;
15540 +       AuDebugOn(!fidir);
15541 +
15542 +       err = -ENOMEM;
15543 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15544 +                        GFP_NOFS, may_shrink);
15545 +       if (p) {
15546 +               p->fd_nent = nbr;
15547 +               finfo->fi_hdir = p;
15548 +               err = 0;
15549 +       }
15550 +
15551 +       return err;
15552 +}
15553 +
15554 +/* ---------------------------------------------------------------------- */
15555 +
15556 +void au_finfo_fin(struct file *file)
15557 +{
15558 +       struct au_finfo *finfo;
15559 +
15560 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15561 +
15562 +       finfo = au_fi(file);
15563 +       AuDebugOn(finfo->fi_hdir);
15564 +       AuRwDestroy(&finfo->fi_rwsem);
15565 +       au_cache_free_finfo(finfo);
15566 +}
15567 +
15568 +void au_fi_init_once(void *_finfo)
15569 +{
15570 +       struct au_finfo *finfo = _finfo;
15571 +
15572 +       au_rw_init(&finfo->fi_rwsem);
15573 +}
15574 +
15575 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15576 +{
15577 +       int err;
15578 +       struct au_finfo *finfo;
15579 +       struct dentry *dentry;
15580 +
15581 +       err = -ENOMEM;
15582 +       dentry = file->f_path.dentry;
15583 +       finfo = au_cache_alloc_finfo();
15584 +       if (unlikely(!finfo))
15585 +               goto out;
15586 +
15587 +       err = 0;
15588 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15589 +       au_rw_write_lock(&finfo->fi_rwsem);
15590 +       finfo->fi_btop = -1;
15591 +       finfo->fi_hdir = fidir;
15592 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15593 +       /* smp_mb(); */ /* atomic_set */
15594 +
15595 +       file->private_data = finfo;
15596 +
15597 +out:
15598 +       return err;
15599 +}
15600 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15601 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15602 +++ linux/fs/aufs/f_op.c        2018-10-23 12:33:35.596042364 +0200
15603 @@ -0,0 +1,819 @@
15604 +// SPDX-License-Identifier: GPL-2.0
15605 +/*
15606 + * Copyright (C) 2005-2018 Junjiro R. Okajima
15607 + *
15608 + * This program, aufs is free software; you can redistribute it and/or modify
15609 + * it under the terms of the GNU General Public License as published by
15610 + * the Free Software Foundation; either version 2 of the License, or
15611 + * (at your option) any later version.
15612 + *
15613 + * This program is distributed in the hope that it will be useful,
15614 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15615 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15616 + * GNU General Public License for more details.
15617 + *
15618 + * You should have received a copy of the GNU General Public License
15619 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15620 + */
15621 +
15622 +/*
15623 + * file and vm operations
15624 + */
15625 +
15626 +#include <linux/aio.h>
15627 +#include <linux/fs_stack.h>
15628 +#include <linux/mman.h>
15629 +#include <linux/security.h>
15630 +#include "aufs.h"
15631 +
15632 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15633 +{
15634 +       int err;
15635 +       aufs_bindex_t bindex;
15636 +       struct dentry *dentry, *h_dentry;
15637 +       struct au_finfo *finfo;
15638 +       struct inode *h_inode;
15639 +
15640 +       FiMustWriteLock(file);
15641 +
15642 +       err = 0;
15643 +       dentry = file->f_path.dentry;
15644 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15645 +       finfo = au_fi(file);
15646 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15647 +       atomic_set(&finfo->fi_mmapped, 0);
15648 +       bindex = au_dbtop(dentry);
15649 +       if (!h_file) {
15650 +               h_dentry = au_h_dptr(dentry, bindex);
15651 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15652 +               if (unlikely(err))
15653 +                       goto out;
15654 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15655 +               if (IS_ERR(h_file)) {
15656 +                       err = PTR_ERR(h_file);
15657 +                       goto out;
15658 +               }
15659 +       } else {
15660 +               h_dentry = h_file->f_path.dentry;
15661 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15662 +               if (unlikely(err))
15663 +                       goto out;
15664 +               /* br ref is already inc-ed */
15665 +       }
15666 +
15667 +       if ((flags & __O_TMPFILE)
15668 +           && !(flags & O_EXCL)) {
15669 +               h_inode = file_inode(h_file);
15670 +               spin_lock(&h_inode->i_lock);
15671 +               h_inode->i_state |= I_LINKABLE;
15672 +               spin_unlock(&h_inode->i_lock);
15673 +       }
15674 +       au_set_fbtop(file, bindex);
15675 +       au_set_h_fptr(file, bindex, h_file);
15676 +       au_update_figen(file);
15677 +       /* todo: necessary? */
15678 +       /* file->f_ra = h_file->f_ra; */
15679 +
15680 +out:
15681 +       return err;
15682 +}
15683 +
15684 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15685 +                           struct file *file)
15686 +{
15687 +       int err;
15688 +       struct super_block *sb;
15689 +       struct au_do_open_args args = {
15690 +               .open   = au_do_open_nondir
15691 +       };
15692 +
15693 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15694 +             file, vfsub_file_flags(file), file->f_mode);
15695 +
15696 +       sb = file->f_path.dentry->d_sb;
15697 +       si_read_lock(sb, AuLock_FLUSH);
15698 +       err = au_do_open(file, &args);
15699 +       si_read_unlock(sb);
15700 +       return err;
15701 +}
15702 +
15703 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15704 +{
15705 +       struct au_finfo *finfo;
15706 +       aufs_bindex_t bindex;
15707 +
15708 +       finfo = au_fi(file);
15709 +       au_hbl_del(&finfo->fi_hlist,
15710 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15711 +       bindex = finfo->fi_btop;
15712 +       if (bindex >= 0)
15713 +               au_set_h_fptr(file, bindex, NULL);
15714 +
15715 +       au_finfo_fin(file);
15716 +       return 0;
15717 +}
15718 +
15719 +/* ---------------------------------------------------------------------- */
15720 +
15721 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15722 +{
15723 +       int err;
15724 +       struct file *h_file;
15725 +
15726 +       err = 0;
15727 +       h_file = au_hf_top(file);
15728 +       if (h_file)
15729 +               err = vfsub_flush(h_file, id);
15730 +       return err;
15731 +}
15732 +
15733 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15734 +{
15735 +       return au_do_flush(file, id, au_do_flush_nondir);
15736 +}
15737 +
15738 +/* ---------------------------------------------------------------------- */
15739 +/*
15740 + * read and write functions acquire [fdi]_rwsem once, but release before
15741 + * mmap_sem. This is because to stop a race condition between mmap(2).
15742 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15743 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15744 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15745 + */
15746 +
15747 +/* Callers should call au_read_post() or fput() in the end */
15748 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15749 +{
15750 +       struct file *h_file;
15751 +       int err;
15752 +
15753 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15754 +       if (!err) {
15755 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15756 +               h_file = au_hf_top(file);
15757 +               get_file(h_file);
15758 +               if (!keep_fi)
15759 +                       fi_read_unlock(file);
15760 +       } else
15761 +               h_file = ERR_PTR(err);
15762 +
15763 +       return h_file;
15764 +}
15765 +
15766 +static void au_read_post(struct inode *inode, struct file *h_file)
15767 +{
15768 +       /* update without lock, I don't think it a problem */
15769 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15770 +       fput(h_file);
15771 +}
15772 +
15773 +struct au_write_pre {
15774 +       /* input */
15775 +       unsigned int lsc;
15776 +
15777 +       /* output */
15778 +       blkcnt_t blks;
15779 +       aufs_bindex_t btop;
15780 +};
15781 +
15782 +/*
15783 + * return with iinfo is write-locked
15784 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15785 + * end
15786 + */
15787 +static struct file *au_write_pre(struct file *file, int do_ready,
15788 +                                struct au_write_pre *wpre)
15789 +{
15790 +       struct file *h_file;
15791 +       struct dentry *dentry;
15792 +       int err;
15793 +       unsigned int lsc;
15794 +       struct au_pin pin;
15795 +
15796 +       lsc = 0;
15797 +       if (wpre)
15798 +               lsc = wpre->lsc;
15799 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15800 +       h_file = ERR_PTR(err);
15801 +       if (unlikely(err))
15802 +               goto out;
15803 +
15804 +       dentry = file->f_path.dentry;
15805 +       if (do_ready) {
15806 +               err = au_ready_to_write(file, -1, &pin);
15807 +               if (unlikely(err)) {
15808 +                       h_file = ERR_PTR(err);
15809 +                       di_write_unlock(dentry);
15810 +                       goto out_fi;
15811 +               }
15812 +       }
15813 +
15814 +       di_downgrade_lock(dentry, /*flags*/0);
15815 +       if (wpre)
15816 +               wpre->btop = au_fbtop(file);
15817 +       h_file = au_hf_top(file);
15818 +       get_file(h_file);
15819 +       if (wpre)
15820 +               wpre->blks = file_inode(h_file)->i_blocks;
15821 +       if (do_ready)
15822 +               au_unpin(&pin);
15823 +       di_read_unlock(dentry, /*flags*/0);
15824 +
15825 +out_fi:
15826 +       fi_write_unlock(file);
15827 +out:
15828 +       return h_file;
15829 +}
15830 +
15831 +static void au_write_post(struct inode *inode, struct file *h_file,
15832 +                         struct au_write_pre *wpre, ssize_t written)
15833 +{
15834 +       struct inode *h_inode;
15835 +
15836 +       au_cpup_attr_timesizes(inode);
15837 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15838 +       h_inode = file_inode(h_file);
15839 +       inode->i_mode = h_inode->i_mode;
15840 +       ii_write_unlock(inode);
15841 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15842 +       if (written > 0)
15843 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15844 +                             /*force*/h_inode->i_blocks > wpre->blks);
15845 +       fput(h_file);
15846 +}
15847 +
15848 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15849 +                        loff_t *ppos)
15850 +{
15851 +       ssize_t err;
15852 +       struct inode *inode;
15853 +       struct file *h_file;
15854 +       struct super_block *sb;
15855 +
15856 +       inode = file_inode(file);
15857 +       sb = inode->i_sb;
15858 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15859 +
15860 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15861 +       err = PTR_ERR(h_file);
15862 +       if (IS_ERR(h_file))
15863 +               goto out;
15864 +
15865 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15866 +       err = vfsub_read_u(h_file, buf, count, ppos);
15867 +       /* todo: necessary? */
15868 +       /* file->f_ra = h_file->f_ra; */
15869 +       au_read_post(inode, h_file);
15870 +
15871 +out:
15872 +       si_read_unlock(sb);
15873 +       return err;
15874 +}
15875 +
15876 +/*
15877 + * todo: very ugly
15878 + * it locks both of i_mutex and si_rwsem for read in safe.
15879 + * if the plink maintenance mode continues forever (that is the problem),
15880 + * may loop forever.
15881 + */
15882 +static void au_mtx_and_read_lock(struct inode *inode)
15883 +{
15884 +       int err;
15885 +       struct super_block *sb = inode->i_sb;
15886 +
15887 +       while (1) {
15888 +               inode_lock(inode);
15889 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15890 +               if (!err)
15891 +                       break;
15892 +               inode_unlock(inode);
15893 +               si_read_lock(sb, AuLock_NOPLMW);
15894 +               si_read_unlock(sb);
15895 +       }
15896 +}
15897 +
15898 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15899 +                         size_t count, loff_t *ppos)
15900 +{
15901 +       ssize_t err;
15902 +       struct au_write_pre wpre;
15903 +       struct inode *inode;
15904 +       struct file *h_file;
15905 +       char __user *buf = (char __user *)ubuf;
15906 +
15907 +       inode = file_inode(file);
15908 +       au_mtx_and_read_lock(inode);
15909 +
15910 +       wpre.lsc = 0;
15911 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15912 +       err = PTR_ERR(h_file);
15913 +       if (IS_ERR(h_file))
15914 +               goto out;
15915 +
15916 +       err = vfsub_write_u(h_file, buf, count, ppos);
15917 +       au_write_post(inode, h_file, &wpre, err);
15918 +
15919 +out:
15920 +       si_read_unlock(inode->i_sb);
15921 +       inode_unlock(inode);
15922 +       return err;
15923 +}
15924 +
15925 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15926 +                         struct iov_iter *iov_iter)
15927 +{
15928 +       ssize_t err;
15929 +       struct file *file;
15930 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15931 +
15932 +       err = security_file_permission(h_file, rw);
15933 +       if (unlikely(err))
15934 +               goto out;
15935 +
15936 +       err = -ENOSYS;
15937 +       iter = NULL;
15938 +       if (rw == MAY_READ)
15939 +               iter = h_file->f_op->read_iter;
15940 +       else if (rw == MAY_WRITE)
15941 +               iter = h_file->f_op->write_iter;
15942 +
15943 +       file = kio->ki_filp;
15944 +       kio->ki_filp = h_file;
15945 +       if (iter) {
15946 +               lockdep_off();
15947 +               err = iter(kio, iov_iter);
15948 +               lockdep_on();
15949 +       } else
15950 +               /* currently there is no such fs */
15951 +               WARN_ON_ONCE(1);
15952 +       kio->ki_filp = file;
15953 +
15954 +out:
15955 +       return err;
15956 +}
15957 +
15958 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15959 +{
15960 +       ssize_t err;
15961 +       struct file *file, *h_file;
15962 +       struct inode *inode;
15963 +       struct super_block *sb;
15964 +
15965 +       file = kio->ki_filp;
15966 +       inode = file_inode(file);
15967 +       sb = inode->i_sb;
15968 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15969 +
15970 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15971 +       err = PTR_ERR(h_file);
15972 +       if (IS_ERR(h_file))
15973 +               goto out;
15974 +
15975 +       if (au_test_loopback_kthread()) {
15976 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15977 +               if (file->f_mapping != h_file->f_mapping) {
15978 +                       file->f_mapping = h_file->f_mapping;
15979 +                       smp_mb(); /* unnecessary? */
15980 +               }
15981 +       }
15982 +       fi_read_unlock(file);
15983 +
15984 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15985 +       /* todo: necessary? */
15986 +       /* file->f_ra = h_file->f_ra; */
15987 +       au_read_post(inode, h_file);
15988 +
15989 +out:
15990 +       si_read_unlock(sb);
15991 +       return err;
15992 +}
15993 +
15994 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15995 +{
15996 +       ssize_t err;
15997 +       struct au_write_pre wpre;
15998 +       struct inode *inode;
15999 +       struct file *file, *h_file;
16000 +
16001 +       file = kio->ki_filp;
16002 +       inode = file_inode(file);
16003 +       au_mtx_and_read_lock(inode);
16004 +
16005 +       wpre.lsc = 0;
16006 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16007 +       err = PTR_ERR(h_file);
16008 +       if (IS_ERR(h_file))
16009 +               goto out;
16010 +
16011 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
16012 +       au_write_post(inode, h_file, &wpre, err);
16013 +
16014 +out:
16015 +       si_read_unlock(inode->i_sb);
16016 +       inode_unlock(inode);
16017 +       return err;
16018 +}
16019 +
16020 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
16021 +                               struct pipe_inode_info *pipe, size_t len,
16022 +                               unsigned int flags)
16023 +{
16024 +       ssize_t err;
16025 +       struct file *h_file;
16026 +       struct inode *inode;
16027 +       struct super_block *sb;
16028 +
16029 +       inode = file_inode(file);
16030 +       sb = inode->i_sb;
16031 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16032 +
16033 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16034 +       err = PTR_ERR(h_file);
16035 +       if (IS_ERR(h_file))
16036 +               goto out;
16037 +
16038 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
16039 +       /* todo: necessary? */
16040 +       /* file->f_ra = h_file->f_ra; */
16041 +       au_read_post(inode, h_file);
16042 +
16043 +out:
16044 +       si_read_unlock(sb);
16045 +       return err;
16046 +}
16047 +
16048 +static ssize_t
16049 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
16050 +                 size_t len, unsigned int flags)
16051 +{
16052 +       ssize_t err;
16053 +       struct au_write_pre wpre;
16054 +       struct inode *inode;
16055 +       struct file *h_file;
16056 +
16057 +       inode = file_inode(file);
16058 +       au_mtx_and_read_lock(inode);
16059 +
16060 +       wpre.lsc = 0;
16061 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16062 +       err = PTR_ERR(h_file);
16063 +       if (IS_ERR(h_file))
16064 +               goto out;
16065 +
16066 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
16067 +       au_write_post(inode, h_file, &wpre, err);
16068 +
16069 +out:
16070 +       si_read_unlock(inode->i_sb);
16071 +       inode_unlock(inode);
16072 +       return err;
16073 +}
16074 +
16075 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
16076 +                          loff_t len)
16077 +{
16078 +       long err;
16079 +       struct au_write_pre wpre;
16080 +       struct inode *inode;
16081 +       struct file *h_file;
16082 +
16083 +       inode = file_inode(file);
16084 +       au_mtx_and_read_lock(inode);
16085 +
16086 +       wpre.lsc = 0;
16087 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16088 +       err = PTR_ERR(h_file);
16089 +       if (IS_ERR(h_file))
16090 +               goto out;
16091 +
16092 +       lockdep_off();
16093 +       err = vfs_fallocate(h_file, mode, offset, len);
16094 +       lockdep_on();
16095 +       au_write_post(inode, h_file, &wpre, /*written*/1);
16096 +
16097 +out:
16098 +       si_read_unlock(inode->i_sb);
16099 +       inode_unlock(inode);
16100 +       return err;
16101 +}
16102 +
16103 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16104 +                                   struct file *dst, loff_t dst_pos,
16105 +                                   size_t len, unsigned int flags)
16106 +{
16107 +       ssize_t err;
16108 +       struct au_write_pre wpre;
16109 +       enum { SRC, DST };
16110 +       struct {
16111 +               struct inode *inode;
16112 +               struct file *h_file;
16113 +               struct super_block *h_sb;
16114 +       } a[2];
16115 +#define a_src  a[SRC]
16116 +#define a_dst  a[DST]
16117 +
16118 +       err = -EINVAL;
16119 +       a_src.inode = file_inode(src);
16120 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16121 +               goto out;
16122 +       a_dst.inode = file_inode(dst);
16123 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16124 +               goto out;
16125 +
16126 +       au_mtx_and_read_lock(a_dst.inode);
16127 +       /*
16128 +        * in order to match the order in di_write_lock2_{child,parent}(),
16129 +        * use f_path.dentry for this comparison.
16130 +        */
16131 +       if (src->f_path.dentry < dst->f_path.dentry) {
16132 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16133 +               err = PTR_ERR(a_src.h_file);
16134 +               if (IS_ERR(a_src.h_file))
16135 +                       goto out_si;
16136 +
16137 +               wpre.lsc = AuLsc_FI_2;
16138 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16139 +               err = PTR_ERR(a_dst.h_file);
16140 +               if (IS_ERR(a_dst.h_file)) {
16141 +                       au_read_post(a_src.inode, a_src.h_file);
16142 +                       goto out_si;
16143 +               }
16144 +       } else {
16145 +               wpre.lsc = AuLsc_FI_1;
16146 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16147 +               err = PTR_ERR(a_dst.h_file);
16148 +               if (IS_ERR(a_dst.h_file))
16149 +                       goto out_si;
16150 +
16151 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16152 +               err = PTR_ERR(a_src.h_file);
16153 +               if (IS_ERR(a_src.h_file)) {
16154 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16155 +                                     /*written*/0);
16156 +                       goto out_si;
16157 +               }
16158 +       }
16159 +
16160 +       err = -EXDEV;
16161 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16162 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16163 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16164 +               AuDbgFile(src);
16165 +               AuDbgFile(dst);
16166 +               goto out_file;
16167 +       }
16168 +
16169 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16170 +                                   dst_pos, len, flags);
16171 +
16172 +out_file:
16173 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16174 +       fi_read_unlock(src);
16175 +       au_read_post(a_src.inode, a_src.h_file);
16176 +out_si:
16177 +       si_read_unlock(a_dst.inode->i_sb);
16178 +       inode_unlock(a_dst.inode);
16179 +out:
16180 +       return err;
16181 +#undef a_src
16182 +#undef a_dst
16183 +}
16184 +
16185 +/* ---------------------------------------------------------------------- */
16186 +
16187 +/*
16188 + * The locking order around current->mmap_sem.
16189 + * - in most and regular cases
16190 + *   file I/O syscall -- aufs_read() or something
16191 + *     -- si_rwsem for read -- mmap_sem
16192 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16193 + * - in mmap case
16194 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16195 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16196 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16197 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16198 + * It means that when aufs acquires si_rwsem for write, the process should never
16199 + * acquire mmap_sem.
16200 + *
16201 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16202 + * problem either since any directory is not able to be mmap-ed.
16203 + * The similar scenario is applied to aufs_readlink() too.
16204 + */
16205 +
16206 +#if 0 /* stop calling security_file_mmap() */
16207 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16208 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16209 +
16210 +static unsigned long au_arch_prot_conv(unsigned long flags)
16211 +{
16212 +       /* currently ppc64 only */
16213 +#ifdef CONFIG_PPC64
16214 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16215 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16216 +       return AuConv_VM_PROT(flags, SAO);
16217 +#else
16218 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16219 +       return 0;
16220 +#endif
16221 +}
16222 +
16223 +static unsigned long au_prot_conv(unsigned long flags)
16224 +{
16225 +       return AuConv_VM_PROT(flags, READ)
16226 +               | AuConv_VM_PROT(flags, WRITE)
16227 +               | AuConv_VM_PROT(flags, EXEC)
16228 +               | au_arch_prot_conv(flags);
16229 +}
16230 +
16231 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16232 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16233 +
16234 +static unsigned long au_flag_conv(unsigned long flags)
16235 +{
16236 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16237 +               | AuConv_VM_MAP(flags, DENYWRITE)
16238 +               | AuConv_VM_MAP(flags, LOCKED);
16239 +}
16240 +#endif
16241 +
16242 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16243 +{
16244 +       int err;
16245 +       const unsigned char wlock
16246 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16247 +       struct super_block *sb;
16248 +       struct file *h_file;
16249 +       struct inode *inode;
16250 +
16251 +       AuDbgVmRegion(file, vma);
16252 +
16253 +       inode = file_inode(file);
16254 +       sb = inode->i_sb;
16255 +       lockdep_off();
16256 +       si_read_lock(sb, AuLock_NOPLMW);
16257 +
16258 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16259 +       lockdep_on();
16260 +       err = PTR_ERR(h_file);
16261 +       if (IS_ERR(h_file))
16262 +               goto out;
16263 +
16264 +       err = 0;
16265 +       au_set_mmapped(file);
16266 +       au_vm_file_reset(vma, h_file);
16267 +       /*
16268 +        * we cannot call security_mmap_file() here since it may acquire
16269 +        * mmap_sem or i_mutex.
16270 +        *
16271 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16272 +        *                       au_flag_conv(vma->vm_flags));
16273 +        */
16274 +       if (!err)
16275 +               err = call_mmap(h_file, vma);
16276 +       if (!err) {
16277 +               au_vm_prfile_set(vma, file);
16278 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16279 +               goto out_fput; /* success */
16280 +       }
16281 +       au_unset_mmapped(file);
16282 +       au_vm_file_reset(vma, file);
16283 +
16284 +out_fput:
16285 +       lockdep_off();
16286 +       ii_write_unlock(inode);
16287 +       lockdep_on();
16288 +       fput(h_file);
16289 +out:
16290 +       lockdep_off();
16291 +       si_read_unlock(sb);
16292 +       lockdep_on();
16293 +       AuTraceErr(err);
16294 +       return err;
16295 +}
16296 +
16297 +/* ---------------------------------------------------------------------- */
16298 +
16299 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16300 +                            int datasync)
16301 +{
16302 +       int err;
16303 +       struct au_write_pre wpre;
16304 +       struct inode *inode;
16305 +       struct file *h_file;
16306 +
16307 +       err = 0; /* -EBADF; */ /* posix? */
16308 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16309 +               goto out;
16310 +
16311 +       inode = file_inode(file);
16312 +       au_mtx_and_read_lock(inode);
16313 +
16314 +       wpre.lsc = 0;
16315 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16316 +       err = PTR_ERR(h_file);
16317 +       if (IS_ERR(h_file))
16318 +               goto out_unlock;
16319 +
16320 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16321 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16322 +
16323 +out_unlock:
16324 +       si_read_unlock(inode->i_sb);
16325 +       inode_unlock(inode);
16326 +out:
16327 +       return err;
16328 +}
16329 +
16330 +static int aufs_fasync(int fd, struct file *file, int flag)
16331 +{
16332 +       int err;
16333 +       struct file *h_file;
16334 +       struct super_block *sb;
16335 +
16336 +       sb = file->f_path.dentry->d_sb;
16337 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16338 +
16339 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16340 +       err = PTR_ERR(h_file);
16341 +       if (IS_ERR(h_file))
16342 +               goto out;
16343 +
16344 +       if (h_file->f_op->fasync)
16345 +               err = h_file->f_op->fasync(fd, h_file, flag);
16346 +       fput(h_file); /* instead of au_read_post() */
16347 +
16348 +out:
16349 +       si_read_unlock(sb);
16350 +       return err;
16351 +}
16352 +
16353 +static int aufs_setfl(struct file *file, unsigned long arg)
16354 +{
16355 +       int err;
16356 +       struct file *h_file;
16357 +       struct super_block *sb;
16358 +
16359 +       sb = file->f_path.dentry->d_sb;
16360 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16361 +
16362 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16363 +       err = PTR_ERR(h_file);
16364 +       if (IS_ERR(h_file))
16365 +               goto out;
16366 +
16367 +       /* stop calling h_file->fasync */
16368 +       arg |= vfsub_file_flags(file) & FASYNC;
16369 +       err = setfl(/*unused fd*/-1, h_file, arg);
16370 +       fput(h_file); /* instead of au_read_post() */
16371 +
16372 +out:
16373 +       si_read_unlock(sb);
16374 +       return err;
16375 +}
16376 +
16377 +/* ---------------------------------------------------------------------- */
16378 +
16379 +/* no one supports this operation, currently */
16380 +#if 0
16381 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16382 +                            size_t len, loff_t *pos, int more)
16383 +{
16384 +}
16385 +#endif
16386 +
16387 +/* ---------------------------------------------------------------------- */
16388 +
16389 +const struct file_operations aufs_file_fop = {
16390 +       .owner          = THIS_MODULE,
16391 +
16392 +       .llseek         = default_llseek,
16393 +
16394 +       .read           = aufs_read,
16395 +       .write          = aufs_write,
16396 +       .read_iter      = aufs_read_iter,
16397 +       .write_iter     = aufs_write_iter,
16398 +
16399 +#ifdef CONFIG_AUFS_POLL
16400 +       .poll           = aufs_poll,
16401 +#endif
16402 +       .unlocked_ioctl = aufs_ioctl_nondir,
16403 +#ifdef CONFIG_COMPAT
16404 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16405 +#endif
16406 +       .mmap           = aufs_mmap,
16407 +       .open           = aufs_open_nondir,
16408 +       .flush          = aufs_flush_nondir,
16409 +       .release        = aufs_release_nondir,
16410 +       .fsync          = aufs_fsync_nondir,
16411 +       .fasync         = aufs_fasync,
16412 +       /* .sendpage    = aufs_sendpage, */
16413 +       .setfl          = aufs_setfl,
16414 +       .splice_write   = aufs_splice_write,
16415 +       .splice_read    = aufs_splice_read,
16416 +#if 0
16417 +       .aio_splice_write = aufs_aio_splice_write,
16418 +       .aio_splice_read  = aufs_aio_splice_read,
16419 +#endif
16420 +       .fallocate      = aufs_fallocate,
16421 +       .copy_file_range = aufs_copy_file_range
16422 +};
16423 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16424 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16425 +++ linux/fs/aufs/fstype.h      2018-08-12 23:43:05.456791299 +0200
16426 @@ -0,0 +1,401 @@
16427 +/* SPDX-License-Identifier: GPL-2.0 */
16428 +/*
16429 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16430 + *
16431 + * This program, aufs is free software; you can redistribute it and/or modify
16432 + * it under the terms of the GNU General Public License as published by
16433 + * the Free Software Foundation; either version 2 of the License, or
16434 + * (at your option) any later version.
16435 + *
16436 + * This program is distributed in the hope that it will be useful,
16437 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16438 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16439 + * GNU General Public License for more details.
16440 + *
16441 + * You should have received a copy of the GNU General Public License
16442 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16443 + */
16444 +
16445 +/*
16446 + * judging filesystem type
16447 + */
16448 +
16449 +#ifndef __AUFS_FSTYPE_H__
16450 +#define __AUFS_FSTYPE_H__
16451 +
16452 +#ifdef __KERNEL__
16453 +
16454 +#include <linux/fs.h>
16455 +#include <linux/magic.h>
16456 +#include <linux/nfs_fs.h>
16457 +#include <linux/romfs_fs.h>
16458 +
16459 +static inline int au_test_aufs(struct super_block *sb)
16460 +{
16461 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16462 +}
16463 +
16464 +static inline const char *au_sbtype(struct super_block *sb)
16465 +{
16466 +       return sb->s_type->name;
16467 +}
16468 +
16469 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16470 +{
16471 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16472 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16473 +#else
16474 +       return 0;
16475 +#endif
16476 +}
16477 +
16478 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16479 +{
16480 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16481 +       return sb->s_magic == ROMFS_MAGIC;
16482 +#else
16483 +       return 0;
16484 +#endif
16485 +}
16486 +
16487 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16488 +{
16489 +#if IS_ENABLED(CONFIG_CRAMFS)
16490 +       return sb->s_magic == CRAMFS_MAGIC;
16491 +#endif
16492 +       return 0;
16493 +}
16494 +
16495 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16496 +{
16497 +#if IS_ENABLED(CONFIG_NFS_FS)
16498 +       return sb->s_magic == NFS_SUPER_MAGIC;
16499 +#else
16500 +       return 0;
16501 +#endif
16502 +}
16503 +
16504 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16505 +{
16506 +#if IS_ENABLED(CONFIG_FUSE_FS)
16507 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16508 +#else
16509 +       return 0;
16510 +#endif
16511 +}
16512 +
16513 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16514 +{
16515 +#if IS_ENABLED(CONFIG_XFS_FS)
16516 +       return sb->s_magic == XFS_SB_MAGIC;
16517 +#else
16518 +       return 0;
16519 +#endif
16520 +}
16521 +
16522 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16523 +{
16524 +#ifdef CONFIG_TMPFS
16525 +       return sb->s_magic == TMPFS_MAGIC;
16526 +#else
16527 +       return 0;
16528 +#endif
16529 +}
16530 +
16531 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16532 +{
16533 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16534 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16535 +#else
16536 +       return 0;
16537 +#endif
16538 +}
16539 +
16540 +static inline int au_test_ramfs(struct super_block *sb)
16541 +{
16542 +       return sb->s_magic == RAMFS_MAGIC;
16543 +}
16544 +
16545 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16546 +{
16547 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16548 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16549 +#else
16550 +       return 0;
16551 +#endif
16552 +}
16553 +
16554 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16555 +{
16556 +#ifdef CONFIG_PROC_FS
16557 +       return sb->s_magic == PROC_SUPER_MAGIC;
16558 +#else
16559 +       return 0;
16560 +#endif
16561 +}
16562 +
16563 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16564 +{
16565 +#ifdef CONFIG_SYSFS
16566 +       return sb->s_magic == SYSFS_MAGIC;
16567 +#else
16568 +       return 0;
16569 +#endif
16570 +}
16571 +
16572 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16573 +{
16574 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16575 +       return sb->s_magic == CONFIGFS_MAGIC;
16576 +#else
16577 +       return 0;
16578 +#endif
16579 +}
16580 +
16581 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16582 +{
16583 +#if IS_ENABLED(CONFIG_MINIX_FS)
16584 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16585 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16586 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16587 +               || sb->s_magic == MINIX_SUPER_MAGIC
16588 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16589 +#else
16590 +       return 0;
16591 +#endif
16592 +}
16593 +
16594 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16595 +{
16596 +#if IS_ENABLED(CONFIG_FAT_FS)
16597 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16598 +#else
16599 +       return 0;
16600 +#endif
16601 +}
16602 +
16603 +static inline int au_test_msdos(struct super_block *sb)
16604 +{
16605 +       return au_test_fat(sb);
16606 +}
16607 +
16608 +static inline int au_test_vfat(struct super_block *sb)
16609 +{
16610 +       return au_test_fat(sb);
16611 +}
16612 +
16613 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16614 +{
16615 +#ifdef CONFIG_SECURITYFS
16616 +       return sb->s_magic == SECURITYFS_MAGIC;
16617 +#else
16618 +       return 0;
16619 +#endif
16620 +}
16621 +
16622 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16623 +{
16624 +#if IS_ENABLED(CONFIG_SQUASHFS)
16625 +       return sb->s_magic == SQUASHFS_MAGIC;
16626 +#else
16627 +       return 0;
16628 +#endif
16629 +}
16630 +
16631 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16632 +{
16633 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16634 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16635 +#else
16636 +       return 0;
16637 +#endif
16638 +}
16639 +
16640 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16641 +{
16642 +#if IS_ENABLED(CONFIG_XENFS)
16643 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16644 +#else
16645 +       return 0;
16646 +#endif
16647 +}
16648 +
16649 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16650 +{
16651 +#ifdef CONFIG_DEBUG_FS
16652 +       return sb->s_magic == DEBUGFS_MAGIC;
16653 +#else
16654 +       return 0;
16655 +#endif
16656 +}
16657 +
16658 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16659 +{
16660 +#if IS_ENABLED(CONFIG_NILFS)
16661 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16662 +#else
16663 +       return 0;
16664 +#endif
16665 +}
16666 +
16667 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16668 +{
16669 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16670 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16671 +#else
16672 +       return 0;
16673 +#endif
16674 +}
16675 +
16676 +/* ---------------------------------------------------------------------- */
16677 +/*
16678 + * they can't be an aufs branch.
16679 + */
16680 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16681 +{
16682 +       return
16683 +#ifndef CONFIG_AUFS_BR_RAMFS
16684 +               au_test_ramfs(sb) ||
16685 +#endif
16686 +               au_test_procfs(sb)
16687 +               || au_test_sysfs(sb)
16688 +               || au_test_configfs(sb)
16689 +               || au_test_debugfs(sb)
16690 +               || au_test_securityfs(sb)
16691 +               || au_test_xenfs(sb)
16692 +               || au_test_ecryptfs(sb)
16693 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16694 +               || au_test_aufs(sb); /* will be supported in next version */
16695 +}
16696 +
16697 +static inline int au_test_fs_remote(struct super_block *sb)
16698 +{
16699 +       return !au_test_tmpfs(sb)
16700 +#ifdef CONFIG_AUFS_BR_RAMFS
16701 +               && !au_test_ramfs(sb)
16702 +#endif
16703 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16704 +}
16705 +
16706 +/* ---------------------------------------------------------------------- */
16707 +
16708 +/*
16709 + * Note: these functions (below) are created after reading ->getattr() in all
16710 + * filesystems under linux/fs. it means we have to do so in every update...
16711 + */
16712 +
16713 +/*
16714 + * some filesystems require getattr to refresh the inode attributes before
16715 + * referencing.
16716 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16717 + * and leave the work for d_revalidate()
16718 + */
16719 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16720 +{
16721 +       return au_test_nfs(sb)
16722 +               || au_test_fuse(sb)
16723 +               /* || au_test_btrfs(sb) */      /* untested */
16724 +               ;
16725 +}
16726 +
16727 +/*
16728 + * filesystems which don't maintain i_size or i_blocks.
16729 + */
16730 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16731 +{
16732 +       return au_test_xfs(sb)
16733 +               || au_test_btrfs(sb)
16734 +               || au_test_ubifs(sb)
16735 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16736 +               /* || au_test_minix(sb) */      /* untested */
16737 +               ;
16738 +}
16739 +
16740 +/*
16741 + * filesystems which don't store the correct value in some of their inode
16742 + * attributes.
16743 + */
16744 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16745 +{
16746 +       return au_test_fs_bad_iattr_size(sb)
16747 +               || au_test_fat(sb)
16748 +               || au_test_msdos(sb)
16749 +               || au_test_vfat(sb);
16750 +}
16751 +
16752 +/* they don't check i_nlink in link(2) */
16753 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16754 +{
16755 +       return au_test_tmpfs(sb)
16756 +#ifdef CONFIG_AUFS_BR_RAMFS
16757 +               || au_test_ramfs(sb)
16758 +#endif
16759 +               || au_test_ubifs(sb)
16760 +               || au_test_hfsplus(sb);
16761 +}
16762 +
16763 +/*
16764 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16765 + */
16766 +static inline int au_test_fs_notime(struct super_block *sb)
16767 +{
16768 +       return au_test_nfs(sb)
16769 +               || au_test_fuse(sb)
16770 +               || au_test_ubifs(sb)
16771 +               ;
16772 +}
16773 +
16774 +/* temporary support for i#1 in cramfs */
16775 +static inline int au_test_fs_unique_ino(struct inode *inode)
16776 +{
16777 +       if (au_test_cramfs(inode->i_sb))
16778 +               return inode->i_ino != 1;
16779 +       return 1;
16780 +}
16781 +
16782 +/* ---------------------------------------------------------------------- */
16783 +
16784 +/*
16785 + * the filesystem where the xino files placed must support i/o after unlink and
16786 + * maintain i_size and i_blocks.
16787 + */
16788 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16789 +{
16790 +       return au_test_fs_remote(sb)
16791 +               || au_test_fs_bad_iattr_size(sb)
16792 +               /* don't want unnecessary work for xino */
16793 +               || au_test_aufs(sb)
16794 +               || au_test_ecryptfs(sb)
16795 +               || au_test_nilfs(sb);
16796 +}
16797 +
16798 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16799 +{
16800 +       return au_test_tmpfs(sb)
16801 +               || au_test_ramfs(sb);
16802 +}
16803 +
16804 +/*
16805 + * test if the @sb is real-readonly.
16806 + */
16807 +static inline int au_test_fs_rr(struct super_block *sb)
16808 +{
16809 +       return au_test_squashfs(sb)
16810 +               || au_test_iso9660(sb)
16811 +               || au_test_cramfs(sb)
16812 +               || au_test_romfs(sb);
16813 +}
16814 +
16815 +/*
16816 + * test if the @inode is nfs with 'noacl' option
16817 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16818 + */
16819 +static inline int au_test_nfs_noacl(struct inode *inode)
16820 +{
16821 +       return au_test_nfs(inode->i_sb)
16822 +               /* && IS_POSIXACL(inode) */
16823 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16824 +}
16825 +
16826 +#endif /* __KERNEL__ */
16827 +#endif /* __AUFS_FSTYPE_H__ */
16828 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16829 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16830 +++ linux/fs/aufs/hbl.h 2018-08-12 23:43:05.456791299 +0200
16831 @@ -0,0 +1,65 @@
16832 +/* SPDX-License-Identifier: GPL-2.0 */
16833 +/*
16834 + * Copyright (C) 2017-2018 Junjiro R. Okajima
16835 + *
16836 + * This program, aufs is free software; you can redistribute it and/or modify
16837 + * it under the terms of the GNU General Public License as published by
16838 + * the Free Software Foundation; either version 2 of the License, or
16839 + * (at your option) any later version.
16840 + *
16841 + * This program is distributed in the hope that it will be useful,
16842 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16843 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16844 + * GNU General Public License for more details.
16845 + *
16846 + * You should have received a copy of the GNU General Public License
16847 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16848 + */
16849 +
16850 +/*
16851 + * helpers for hlist_bl.h
16852 + */
16853 +
16854 +#ifndef __AUFS_HBL_H__
16855 +#define __AUFS_HBL_H__
16856 +
16857 +#ifdef __KERNEL__
16858 +
16859 +#include <linux/list_bl.h>
16860 +
16861 +static inline void au_hbl_add(struct hlist_bl_node *node,
16862 +                             struct hlist_bl_head *hbl)
16863 +{
16864 +       hlist_bl_lock(hbl);
16865 +       hlist_bl_add_head(node, hbl);
16866 +       hlist_bl_unlock(hbl);
16867 +}
16868 +
16869 +static inline void au_hbl_del(struct hlist_bl_node *node,
16870 +                             struct hlist_bl_head *hbl)
16871 +{
16872 +       hlist_bl_lock(hbl);
16873 +       hlist_bl_del(node);
16874 +       hlist_bl_unlock(hbl);
16875 +}
16876 +
16877 +#define au_hbl_for_each(pos, head)                                     \
16878 +       for (pos = hlist_bl_first(head);                                \
16879 +            pos;                                                       \
16880 +            pos = pos->next)
16881 +
16882 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16883 +{
16884 +       unsigned long cnt;
16885 +       struct hlist_bl_node *pos;
16886 +
16887 +       cnt = 0;
16888 +       hlist_bl_lock(hbl);
16889 +       au_hbl_for_each(pos, hbl)
16890 +               cnt++;
16891 +       hlist_bl_unlock(hbl);
16892 +       return cnt;
16893 +}
16894 +
16895 +#endif /* __KERNEL__ */
16896 +#endif /* __AUFS_HBL_H__ */
16897 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16898 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16899 +++ linux/fs/aufs/hfsnotify.c   2018-12-27 13:19:17.708416053 +0100
16900 @@ -0,0 +1,289 @@
16901 +// SPDX-License-Identifier: GPL-2.0
16902 +/*
16903 + * Copyright (C) 2005-2018 Junjiro R. Okajima
16904 + *
16905 + * This program, aufs is free software; you can redistribute it and/or modify
16906 + * it under the terms of the GNU General Public License as published by
16907 + * the Free Software Foundation; either version 2 of the License, or
16908 + * (at your option) any later version.
16909 + *
16910 + * This program is distributed in the hope that it will be useful,
16911 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16912 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16913 + * GNU General Public License for more details.
16914 + *
16915 + * You should have received a copy of the GNU General Public License
16916 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16917 + */
16918 +
16919 +/*
16920 + * fsnotify for the lower directories
16921 + */
16922 +
16923 +#include "aufs.h"
16924 +
16925 +/* FS_IN_IGNORED is unnecessary */
16926 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16927 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16928 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16929 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16930 +
16931 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16932 +{
16933 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16934 +                                            hn_mark);
16935 +       /* AuDbg("here\n"); */
16936 +       au_cache_free_hnotify(hn);
16937 +       smp_mb__before_atomic(); /* for atomic64_dec */
16938 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16939 +               wake_up(&au_hfsn_wq);
16940 +}
16941 +
16942 +static int au_hfsn_alloc(struct au_hinode *hinode)
16943 +{
16944 +       int err;
16945 +       struct au_hnotify *hn;
16946 +       struct super_block *sb;
16947 +       struct au_branch *br;
16948 +       struct fsnotify_mark *mark;
16949 +       aufs_bindex_t bindex;
16950 +
16951 +       hn = hinode->hi_notify;
16952 +       sb = hn->hn_aufs_inode->i_sb;
16953 +       bindex = au_br_index(sb, hinode->hi_id);
16954 +       br = au_sbr(sb, bindex);
16955 +       AuDebugOn(!br->br_hfsn);
16956 +
16957 +       mark = &hn->hn_mark;
16958 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16959 +       mark->mask = AuHfsnMask;
16960 +       /*
16961 +        * by udba rename or rmdir, aufs assign a new inode to the known
16962 +        * h_inode, so specify 1 to allow dups.
16963 +        */
16964 +       lockdep_off();
16965 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16966 +       lockdep_on();
16967 +
16968 +       return err;
16969 +}
16970 +
16971 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16972 +{
16973 +       struct fsnotify_mark *mark;
16974 +       unsigned long long ull;
16975 +       struct fsnotify_group *group;
16976 +
16977 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16978 +       BUG_ON(!ull);
16979 +
16980 +       mark = &hn->hn_mark;
16981 +       spin_lock(&mark->lock);
16982 +       group = mark->group;
16983 +       fsnotify_get_group(group);
16984 +       spin_unlock(&mark->lock);
16985 +       lockdep_off();
16986 +       fsnotify_destroy_mark(mark, group);
16987 +       fsnotify_put_mark(mark);
16988 +       fsnotify_put_group(group);
16989 +       lockdep_on();
16990 +
16991 +       /* free hn by myself */
16992 +       return 0;
16993 +}
16994 +
16995 +/* ---------------------------------------------------------------------- */
16996 +
16997 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16998 +{
16999 +       struct fsnotify_mark *mark;
17000 +
17001 +       mark = &hinode->hi_notify->hn_mark;
17002 +       spin_lock(&mark->lock);
17003 +       if (do_set) {
17004 +               AuDebugOn(mark->mask & AuHfsnMask);
17005 +               mark->mask |= AuHfsnMask;
17006 +       } else {
17007 +               AuDebugOn(!(mark->mask & AuHfsnMask));
17008 +               mark->mask &= ~AuHfsnMask;
17009 +       }
17010 +       spin_unlock(&mark->lock);
17011 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
17012 +}
17013 +
17014 +/* ---------------------------------------------------------------------- */
17015 +
17016 +/* #define AuDbgHnotify */
17017 +#ifdef AuDbgHnotify
17018 +static char *au_hfsn_name(u32 mask)
17019 +{
17020 +#ifdef CONFIG_AUFS_DEBUG
17021 +#define test_ret(flag)                         \
17022 +       do {                                    \
17023 +               if (mask & flag)                \
17024 +                       return #flag;           \
17025 +       } while (0)
17026 +       test_ret(FS_ACCESS);
17027 +       test_ret(FS_MODIFY);
17028 +       test_ret(FS_ATTRIB);
17029 +       test_ret(FS_CLOSE_WRITE);
17030 +       test_ret(FS_CLOSE_NOWRITE);
17031 +       test_ret(FS_OPEN);
17032 +       test_ret(FS_MOVED_FROM);
17033 +       test_ret(FS_MOVED_TO);
17034 +       test_ret(FS_CREATE);
17035 +       test_ret(FS_DELETE);
17036 +       test_ret(FS_DELETE_SELF);
17037 +       test_ret(FS_MOVE_SELF);
17038 +       test_ret(FS_UNMOUNT);
17039 +       test_ret(FS_Q_OVERFLOW);
17040 +       test_ret(FS_IN_IGNORED);
17041 +       test_ret(FS_ISDIR);
17042 +       test_ret(FS_IN_ONESHOT);
17043 +       test_ret(FS_EVENT_ON_CHILD);
17044 +       return "";
17045 +#undef test_ret
17046 +#else
17047 +       return "??";
17048 +#endif
17049 +}
17050 +#endif
17051 +
17052 +/* ---------------------------------------------------------------------- */
17053 +
17054 +static void au_hfsn_free_group(struct fsnotify_group *group)
17055 +{
17056 +       struct au_br_hfsnotify *hfsn = group->private;
17057 +
17058 +       /* AuDbg("here\n"); */
17059 +       au_kfree_try_rcu(hfsn);
17060 +}
17061 +
17062 +static int au_hfsn_handle_event(struct fsnotify_group *group,
17063 +                               struct inode *inode,
17064 +                               u32 mask, const void *data, int data_type,
17065 +                               const unsigned char *file_name, u32 cookie,
17066 +                               struct fsnotify_iter_info *iter_info)
17067 +{
17068 +       int err;
17069 +       struct au_hnotify *hnotify;
17070 +       struct inode *h_dir, *h_inode;
17071 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
17072 +       struct fsnotify_mark *inode_mark;
17073 +
17074 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
17075 +
17076 +       err = 0;
17077 +       /* if FS_UNMOUNT happens, there must be another bug */
17078 +       AuDebugOn(mask & FS_UNMOUNT);
17079 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
17080 +               goto out;
17081 +
17082 +       h_dir = inode;
17083 +       h_inode = NULL;
17084 +#ifdef AuDbgHnotify
17085 +       au_debug_on();
17086 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
17087 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
17088 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
17089 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
17090 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
17091 +               /* WARN_ON(1); */
17092 +       }
17093 +       au_debug_off();
17094 +#endif
17095 +
17096 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
17097 +       AuDebugOn(!inode_mark);
17098 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
17099 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
17100 +
17101 +out:
17102 +       return err;
17103 +}
17104 +
17105 +static struct fsnotify_ops au_hfsn_ops = {
17106 +       .handle_event           = au_hfsn_handle_event,
17107 +       .free_group_priv        = au_hfsn_free_group,
17108 +       .free_mark              = au_hfsn_free_mark
17109 +};
17110 +
17111 +/* ---------------------------------------------------------------------- */
17112 +
17113 +static void au_hfsn_fin_br(struct au_branch *br)
17114 +{
17115 +       struct au_br_hfsnotify *hfsn;
17116 +
17117 +       hfsn = br->br_hfsn;
17118 +       if (hfsn) {
17119 +               lockdep_off();
17120 +               fsnotify_put_group(hfsn->hfsn_group);
17121 +               lockdep_on();
17122 +       }
17123 +}
17124 +
17125 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17126 +{
17127 +       int err;
17128 +       struct fsnotify_group *group;
17129 +       struct au_br_hfsnotify *hfsn;
17130 +
17131 +       err = 0;
17132 +       br->br_hfsn = NULL;
17133 +       if (!au_br_hnotifyable(perm))
17134 +               goto out;
17135 +
17136 +       err = -ENOMEM;
17137 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17138 +       if (unlikely(!hfsn))
17139 +               goto out;
17140 +
17141 +       err = 0;
17142 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17143 +       if (IS_ERR(group)) {
17144 +               err = PTR_ERR(group);
17145 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17146 +               goto out_hfsn;
17147 +       }
17148 +
17149 +       group->private = hfsn;
17150 +       hfsn->hfsn_group = group;
17151 +       br->br_hfsn = hfsn;
17152 +       goto out; /* success */
17153 +
17154 +out_hfsn:
17155 +       au_kfree_try_rcu(hfsn);
17156 +out:
17157 +       return err;
17158 +}
17159 +
17160 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17161 +{
17162 +       int err;
17163 +
17164 +       err = 0;
17165 +       if (!br->br_hfsn)
17166 +               err = au_hfsn_init_br(br, perm);
17167 +
17168 +       return err;
17169 +}
17170 +
17171 +/* ---------------------------------------------------------------------- */
17172 +
17173 +static void au_hfsn_fin(void)
17174 +{
17175 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17176 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17177 +}
17178 +
17179 +const struct au_hnotify_op au_hnotify_op = {
17180 +       .ctl            = au_hfsn_ctl,
17181 +       .alloc          = au_hfsn_alloc,
17182 +       .free           = au_hfsn_free,
17183 +
17184 +       .fin            = au_hfsn_fin,
17185 +
17186 +       .reset_br       = au_hfsn_reset_br,
17187 +       .fin_br         = au_hfsn_fin_br,
17188 +       .init_br        = au_hfsn_init_br
17189 +};
17190 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17191 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17192 +++ linux/fs/aufs/hfsplus.c     2018-10-23 12:33:35.596042364 +0200
17193 @@ -0,0 +1,60 @@
17194 +// SPDX-License-Identifier: GPL-2.0
17195 +/*
17196 + * Copyright (C) 2010-2018 Junjiro R. Okajima
17197 + *
17198 + * This program, aufs is free software; you can redistribute it and/or modify
17199 + * it under the terms of the GNU General Public License as published by
17200 + * the Free Software Foundation; either version 2 of the License, or
17201 + * (at your option) any later version.
17202 + *
17203 + * This program is distributed in the hope that it will be useful,
17204 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17205 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17206 + * GNU General Public License for more details.
17207 + *
17208 + * You should have received a copy of the GNU General Public License
17209 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17210 + */
17211 +
17212 +/*
17213 + * special support for filesystems which acquires an inode mutex
17214 + * at final closing a file, eg, hfsplus.
17215 + *
17216 + * This trick is very simple and stupid, just to open the file before really
17217 + * necessary open to tell hfsplus that this is not the final closing.
17218 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17219 + * and au_h_open_post() after releasing it.
17220 + */
17221 +
17222 +#include "aufs.h"
17223 +
17224 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17225 +                          int force_wr)
17226 +{
17227 +       struct file *h_file;
17228 +       struct dentry *h_dentry;
17229 +
17230 +       h_dentry = au_h_dptr(dentry, bindex);
17231 +       AuDebugOn(!h_dentry);
17232 +       AuDebugOn(d_is_negative(h_dentry));
17233 +
17234 +       h_file = NULL;
17235 +       if (au_test_hfsplus(h_dentry->d_sb)
17236 +           && d_is_reg(h_dentry))
17237 +               h_file = au_h_open(dentry, bindex,
17238 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17239 +                                  /*file*/NULL, force_wr);
17240 +       return h_file;
17241 +}
17242 +
17243 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17244 +                   struct file *h_file)
17245 +{
17246 +       struct au_branch *br;
17247 +
17248 +       if (h_file) {
17249 +               fput(h_file);
17250 +               br = au_sbr(dentry->d_sb, bindex);
17251 +               au_lcnt_dec(&br->br_nfiles);
17252 +       }
17253 +}
17254 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17255 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17256 +++ linux/fs/aufs/hnotify.c     2018-12-27 13:19:17.708416053 +0100
17257 @@ -0,0 +1,720 @@
17258 +// SPDX-License-Identifier: GPL-2.0
17259 +/*
17260 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17261 + *
17262 + * This program, aufs is free software; you can redistribute it and/or modify
17263 + * it under the terms of the GNU General Public License as published by
17264 + * the Free Software Foundation; either version 2 of the License, or
17265 + * (at your option) any later version.
17266 + *
17267 + * This program is distributed in the hope that it will be useful,
17268 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17269 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17270 + * GNU General Public License for more details.
17271 + *
17272 + * You should have received a copy of the GNU General Public License
17273 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17274 + */
17275 +
17276 +/*
17277 + * abstraction to notify the direct changes on lower directories
17278 + */
17279 +
17280 +#include "aufs.h"
17281 +
17282 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17283 +{
17284 +       int err;
17285 +       struct au_hnotify *hn;
17286 +
17287 +       err = -ENOMEM;
17288 +       hn = au_cache_alloc_hnotify();
17289 +       if (hn) {
17290 +               hn->hn_aufs_inode = inode;
17291 +               hinode->hi_notify = hn;
17292 +               err = au_hnotify_op.alloc(hinode);
17293 +               AuTraceErr(err);
17294 +               if (unlikely(err)) {
17295 +                       hinode->hi_notify = NULL;
17296 +                       au_cache_free_hnotify(hn);
17297 +                       /*
17298 +                        * The upper dir was removed by udba, but the same named
17299 +                        * dir left. In this case, aufs assigns a new inode
17300 +                        * number and set the monitor again.
17301 +                        * For the lower dir, the old monitor is still left.
17302 +                        */
17303 +                       if (err == -EEXIST)
17304 +                               err = 0;
17305 +               }
17306 +       }
17307 +
17308 +       AuTraceErr(err);
17309 +       return err;
17310 +}
17311 +
17312 +void au_hn_free(struct au_hinode *hinode)
17313 +{
17314 +       struct au_hnotify *hn;
17315 +
17316 +       hn = hinode->hi_notify;
17317 +       if (hn) {
17318 +               hinode->hi_notify = NULL;
17319 +               if (au_hnotify_op.free(hinode, hn))
17320 +                       au_cache_free_hnotify(hn);
17321 +       }
17322 +}
17323 +
17324 +/* ---------------------------------------------------------------------- */
17325 +
17326 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17327 +{
17328 +       if (hinode->hi_notify)
17329 +               au_hnotify_op.ctl(hinode, do_set);
17330 +}
17331 +
17332 +void au_hn_reset(struct inode *inode, unsigned int flags)
17333 +{
17334 +       aufs_bindex_t bindex, bbot;
17335 +       struct inode *hi;
17336 +       struct dentry *iwhdentry;
17337 +
17338 +       bbot = au_ibbot(inode);
17339 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17340 +               hi = au_h_iptr(inode, bindex);
17341 +               if (!hi)
17342 +                       continue;
17343 +
17344 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17345 +               iwhdentry = au_hi_wh(inode, bindex);
17346 +               if (iwhdentry)
17347 +                       dget(iwhdentry);
17348 +               au_igrab(hi);
17349 +               au_set_h_iptr(inode, bindex, NULL, 0);
17350 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17351 +                             flags & ~AuHi_XINO);
17352 +               iput(hi);
17353 +               dput(iwhdentry);
17354 +               /* inode_unlock(hi); */
17355 +       }
17356 +}
17357 +
17358 +/* ---------------------------------------------------------------------- */
17359 +
17360 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17361 +{
17362 +       int err;
17363 +       aufs_bindex_t bindex, bbot, bfound, btop;
17364 +       struct inode *h_i;
17365 +
17366 +       err = 0;
17367 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17368 +               pr_warn("branch root dir was changed\n");
17369 +               goto out;
17370 +       }
17371 +
17372 +       bfound = -1;
17373 +       bbot = au_ibbot(inode);
17374 +       btop = au_ibtop(inode);
17375 +#if 0 /* reserved for future use */
17376 +       if (bindex == bbot) {
17377 +               /* keep this ino in rename case */
17378 +               goto out;
17379 +       }
17380 +#endif
17381 +       for (bindex = btop; bindex <= bbot; bindex++)
17382 +               if (au_h_iptr(inode, bindex) == h_inode) {
17383 +                       bfound = bindex;
17384 +                       break;
17385 +               }
17386 +       if (bfound < 0)
17387 +               goto out;
17388 +
17389 +       for (bindex = btop; bindex <= bbot; bindex++) {
17390 +               h_i = au_h_iptr(inode, bindex);
17391 +               if (!h_i)
17392 +                       continue;
17393 +
17394 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17395 +               /* ignore this error */
17396 +               /* bad action? */
17397 +       }
17398 +
17399 +       /* children inode number will be broken */
17400 +
17401 +out:
17402 +       AuTraceErr(err);
17403 +       return err;
17404 +}
17405 +
17406 +static int hn_gen_tree(struct dentry *dentry)
17407 +{
17408 +       int err, i, j, ndentry;
17409 +       struct au_dcsub_pages dpages;
17410 +       struct au_dpage *dpage;
17411 +       struct dentry **dentries;
17412 +
17413 +       err = au_dpages_init(&dpages, GFP_NOFS);
17414 +       if (unlikely(err))
17415 +               goto out;
17416 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17417 +       if (unlikely(err))
17418 +               goto out_dpages;
17419 +
17420 +       for (i = 0; i < dpages.ndpage; i++) {
17421 +               dpage = dpages.dpages + i;
17422 +               dentries = dpage->dentries;
17423 +               ndentry = dpage->ndentry;
17424 +               for (j = 0; j < ndentry; j++) {
17425 +                       struct dentry *d;
17426 +
17427 +                       d = dentries[j];
17428 +                       if (IS_ROOT(d))
17429 +                               continue;
17430 +
17431 +                       au_digen_dec(d);
17432 +                       if (d_really_is_positive(d))
17433 +                               /* todo: reset children xino?
17434 +                                  cached children only? */
17435 +                               au_iigen_dec(d_inode(d));
17436 +               }
17437 +       }
17438 +
17439 +out_dpages:
17440 +       au_dpages_free(&dpages);
17441 +
17442 +#if 0
17443 +       /* discard children */
17444 +       dentry_unhash(dentry);
17445 +       dput(dentry);
17446 +#endif
17447 +out:
17448 +       return err;
17449 +}
17450 +
17451 +/*
17452 + * return 0 if processed.
17453 + */
17454 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17455 +                          const unsigned int isdir)
17456 +{
17457 +       int err;
17458 +       struct dentry *d;
17459 +       struct qstr *dname;
17460 +
17461 +       err = 1;
17462 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17463 +               pr_warn("branch root dir was changed\n");
17464 +               err = 0;
17465 +               goto out;
17466 +       }
17467 +
17468 +       if (!isdir) {
17469 +               AuDebugOn(!name);
17470 +               au_iigen_dec(inode);
17471 +               spin_lock(&inode->i_lock);
17472 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17473 +                       spin_lock(&d->d_lock);
17474 +                       dname = &d->d_name;
17475 +                       if (dname->len != nlen
17476 +                           && memcmp(dname->name, name, nlen)) {
17477 +                               spin_unlock(&d->d_lock);
17478 +                               continue;
17479 +                       }
17480 +                       err = 0;
17481 +                       au_digen_dec(d);
17482 +                       spin_unlock(&d->d_lock);
17483 +                       break;
17484 +               }
17485 +               spin_unlock(&inode->i_lock);
17486 +       } else {
17487 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17488 +               d = d_find_any_alias(inode);
17489 +               if (!d) {
17490 +                       au_iigen_dec(inode);
17491 +                       goto out;
17492 +               }
17493 +
17494 +               spin_lock(&d->d_lock);
17495 +               dname = &d->d_name;
17496 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17497 +                       spin_unlock(&d->d_lock);
17498 +                       err = hn_gen_tree(d);
17499 +                       spin_lock(&d->d_lock);
17500 +               }
17501 +               spin_unlock(&d->d_lock);
17502 +               dput(d);
17503 +       }
17504 +
17505 +out:
17506 +       AuTraceErr(err);
17507 +       return err;
17508 +}
17509 +
17510 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17511 +{
17512 +       int err;
17513 +
17514 +       if (IS_ROOT(dentry)) {
17515 +               pr_warn("branch root dir was changed\n");
17516 +               return 0;
17517 +       }
17518 +
17519 +       err = 0;
17520 +       if (!isdir) {
17521 +               au_digen_dec(dentry);
17522 +               if (d_really_is_positive(dentry))
17523 +                       au_iigen_dec(d_inode(dentry));
17524 +       } else {
17525 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17526 +               if (d_really_is_positive(dentry))
17527 +                       err = hn_gen_tree(dentry);
17528 +       }
17529 +
17530 +       AuTraceErr(err);
17531 +       return err;
17532 +}
17533 +
17534 +/* ---------------------------------------------------------------------- */
17535 +
17536 +/* hnotify job flags */
17537 +#define AuHnJob_XINO0          1
17538 +#define AuHnJob_GEN            (1 << 1)
17539 +#define AuHnJob_DIRENT         (1 << 2)
17540 +#define AuHnJob_ISDIR          (1 << 3)
17541 +#define AuHnJob_TRYXINO0       (1 << 4)
17542 +#define AuHnJob_MNTPNT         (1 << 5)
17543 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17544 +#define au_fset_hnjob(flags, name) \
17545 +       do { (flags) |= AuHnJob_##name; } while (0)
17546 +#define au_fclr_hnjob(flags, name) \
17547 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17548 +
17549 +enum {
17550 +       AuHn_CHILD,
17551 +       AuHn_PARENT,
17552 +       AuHnLast
17553 +};
17554 +
17555 +struct au_hnotify_args {
17556 +       struct inode *h_dir, *dir, *h_child_inode;
17557 +       u32 mask;
17558 +       unsigned int flags[AuHnLast];
17559 +       unsigned int h_child_nlen;
17560 +       char h_child_name[];
17561 +};
17562 +
17563 +struct hn_job_args {
17564 +       unsigned int flags;
17565 +       struct inode *inode, *h_inode, *dir, *h_dir;
17566 +       struct dentry *dentry;
17567 +       char *h_name;
17568 +       int h_nlen;
17569 +};
17570 +
17571 +static int hn_job(struct hn_job_args *a)
17572 +{
17573 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17574 +       int e;
17575 +
17576 +       /* reset xino */
17577 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17578 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17579 +
17580 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17581 +           && a->inode
17582 +           && a->h_inode) {
17583 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17584 +               if (!a->h_inode->i_nlink
17585 +                   && !(a->h_inode->i_state & I_LINKABLE))
17586 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17587 +               inode_unlock_shared(a->h_inode);
17588 +       }
17589 +
17590 +       /* make the generation obsolete */
17591 +       if (au_ftest_hnjob(a->flags, GEN)) {
17592 +               e = -1;
17593 +               if (a->inode)
17594 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17595 +                                             isdir);
17596 +               if (e && a->dentry)
17597 +                       hn_gen_by_name(a->dentry, isdir);
17598 +               /* ignore this error */
17599 +       }
17600 +
17601 +       /* make dir entries obsolete */
17602 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17603 +               struct au_vdir *vdir;
17604 +
17605 +               vdir = au_ivdir(a->inode);
17606 +               if (vdir)
17607 +                       vdir->vd_jiffy = 0;
17608 +               /* IMustLock(a->inode); */
17609 +               /* inode_inc_iversion(a->inode); */
17610 +       }
17611 +
17612 +       /* can do nothing but warn */
17613 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17614 +           && a->dentry
17615 +           && d_mountpoint(a->dentry))
17616 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17617 +
17618 +       return 0;
17619 +}
17620 +
17621 +/* ---------------------------------------------------------------------- */
17622 +
17623 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17624 +                                          struct inode *dir)
17625 +{
17626 +       struct dentry *dentry, *d, *parent;
17627 +       struct qstr *dname;
17628 +
17629 +       parent = d_find_any_alias(dir);
17630 +       if (!parent)
17631 +               return NULL;
17632 +
17633 +       dentry = NULL;
17634 +       spin_lock(&parent->d_lock);
17635 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17636 +               /* AuDbg("%pd\n", d); */
17637 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17638 +               dname = &d->d_name;
17639 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17640 +                       goto cont_unlock;
17641 +               if (au_di(d))
17642 +                       au_digen_dec(d);
17643 +               else
17644 +                       goto cont_unlock;
17645 +               if (au_dcount(d) > 0) {
17646 +                       dentry = dget_dlock(d);
17647 +                       spin_unlock(&d->d_lock);
17648 +                       break;
17649 +               }
17650 +
17651 +cont_unlock:
17652 +               spin_unlock(&d->d_lock);
17653 +       }
17654 +       spin_unlock(&parent->d_lock);
17655 +       dput(parent);
17656 +
17657 +       if (dentry)
17658 +               di_write_lock_child(dentry);
17659 +
17660 +       return dentry;
17661 +}
17662 +
17663 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17664 +                                        aufs_bindex_t bindex, ino_t h_ino)
17665 +{
17666 +       struct inode *inode;
17667 +       ino_t ino;
17668 +       int err;
17669 +
17670 +       inode = NULL;
17671 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17672 +       if (!err && ino)
17673 +               inode = ilookup(sb, ino);
17674 +       if (!inode)
17675 +               goto out;
17676 +
17677 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17678 +               pr_warn("wrong root branch\n");
17679 +               iput(inode);
17680 +               inode = NULL;
17681 +               goto out;
17682 +       }
17683 +
17684 +       ii_write_lock_child(inode);
17685 +
17686 +out:
17687 +       return inode;
17688 +}
17689 +
17690 +static void au_hn_bh(void *_args)
17691 +{
17692 +       struct au_hnotify_args *a = _args;
17693 +       struct super_block *sb;
17694 +       aufs_bindex_t bindex, bbot, bfound;
17695 +       unsigned char xino, try_iput;
17696 +       int err;
17697 +       struct inode *inode;
17698 +       ino_t h_ino;
17699 +       struct hn_job_args args;
17700 +       struct dentry *dentry;
17701 +       struct au_sbinfo *sbinfo;
17702 +
17703 +       AuDebugOn(!_args);
17704 +       AuDebugOn(!a->h_dir);
17705 +       AuDebugOn(!a->dir);
17706 +       AuDebugOn(!a->mask);
17707 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17708 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17709 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17710 +
17711 +       inode = NULL;
17712 +       dentry = NULL;
17713 +       /*
17714 +        * do not lock a->dir->i_mutex here
17715 +        * because of d_revalidate() may cause a deadlock.
17716 +        */
17717 +       sb = a->dir->i_sb;
17718 +       AuDebugOn(!sb);
17719 +       sbinfo = au_sbi(sb);
17720 +       AuDebugOn(!sbinfo);
17721 +       si_write_lock(sb, AuLock_NOPLMW);
17722 +
17723 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17724 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17725 +               case FS_MOVED_FROM:
17726 +               case FS_MOVED_TO:
17727 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17728 +                               "for the direct rename(2)\n");
17729 +               }
17730 +
17731 +       ii_read_lock_parent(a->dir);
17732 +       bfound = -1;
17733 +       bbot = au_ibbot(a->dir);
17734 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17735 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17736 +                       bfound = bindex;
17737 +                       break;
17738 +               }
17739 +       ii_read_unlock(a->dir);
17740 +       if (unlikely(bfound < 0))
17741 +               goto out;
17742 +
17743 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17744 +       h_ino = 0;
17745 +       if (a->h_child_inode)
17746 +               h_ino = a->h_child_inode->i_ino;
17747 +
17748 +       if (a->h_child_nlen
17749 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17750 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17751 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17752 +                                             a->dir);
17753 +       try_iput = 0;
17754 +       if (dentry && d_really_is_positive(dentry))
17755 +               inode = d_inode(dentry);
17756 +       if (xino && !inode && h_ino
17757 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17758 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17759 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17760 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17761 +               try_iput = 1;
17762 +       }
17763 +
17764 +       args.flags = a->flags[AuHn_CHILD];
17765 +       args.dentry = dentry;
17766 +       args.inode = inode;
17767 +       args.h_inode = a->h_child_inode;
17768 +       args.dir = a->dir;
17769 +       args.h_dir = a->h_dir;
17770 +       args.h_name = a->h_child_name;
17771 +       args.h_nlen = a->h_child_nlen;
17772 +       err = hn_job(&args);
17773 +       if (dentry) {
17774 +               if (au_di(dentry))
17775 +                       di_write_unlock(dentry);
17776 +               dput(dentry);
17777 +       }
17778 +       if (inode && try_iput) {
17779 +               ii_write_unlock(inode);
17780 +               iput(inode);
17781 +       }
17782 +
17783 +       ii_write_lock_parent(a->dir);
17784 +       args.flags = a->flags[AuHn_PARENT];
17785 +       args.dentry = NULL;
17786 +       args.inode = a->dir;
17787 +       args.h_inode = a->h_dir;
17788 +       args.dir = NULL;
17789 +       args.h_dir = NULL;
17790 +       args.h_name = NULL;
17791 +       args.h_nlen = 0;
17792 +       err = hn_job(&args);
17793 +       ii_write_unlock(a->dir);
17794 +
17795 +out:
17796 +       iput(a->h_child_inode);
17797 +       iput(a->h_dir);
17798 +       iput(a->dir);
17799 +       si_write_unlock(sb);
17800 +       au_nwt_done(&sbinfo->si_nowait);
17801 +       au_kfree_rcu(a);
17802 +}
17803 +
17804 +/* ---------------------------------------------------------------------- */
17805 +
17806 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17807 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
17808 +{
17809 +       int err, len;
17810 +       unsigned int flags[AuHnLast], f;
17811 +       unsigned char isdir, isroot, wh;
17812 +       struct inode *dir;
17813 +       struct au_hnotify_args *args;
17814 +       char *p, *h_child_name;
17815 +
17816 +       err = 0;
17817 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17818 +       dir = igrab(hnotify->hn_aufs_inode);
17819 +       if (!dir)
17820 +               goto out;
17821 +
17822 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17823 +       wh = 0;
17824 +       h_child_name = (void *)h_child_qstr->name;
17825 +       len = h_child_qstr->len;
17826 +       if (h_child_name) {
17827 +               if (len > AUFS_WH_PFX_LEN
17828 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17829 +                       h_child_name += AUFS_WH_PFX_LEN;
17830 +                       len -= AUFS_WH_PFX_LEN;
17831 +                       wh = 1;
17832 +               }
17833 +       }
17834 +
17835 +       isdir = 0;
17836 +       if (h_child_inode)
17837 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17838 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17839 +       flags[AuHn_CHILD] = 0;
17840 +       if (isdir)
17841 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17842 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17843 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17844 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
17845 +       case FS_MOVED_FROM:
17846 +       case FS_MOVED_TO:
17847 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17848 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17849 +               /*FALLTHROUGH*/
17850 +       case FS_CREATE:
17851 +               AuDebugOn(!h_child_name);
17852 +               break;
17853 +
17854 +       case FS_DELETE:
17855 +               /*
17856 +                * aufs never be able to get this child inode.
17857 +                * revalidation should be in d_revalidate()
17858 +                * by checking i_nlink, i_generation or d_unhashed().
17859 +                */
17860 +               AuDebugOn(!h_child_name);
17861 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17862 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17863 +               break;
17864 +
17865 +       default:
17866 +               AuDebugOn(1);
17867 +       }
17868 +
17869 +       if (wh)
17870 +               h_child_inode = NULL;
17871 +
17872 +       err = -ENOMEM;
17873 +       /* iput() and kfree() will be called in au_hnotify() */
17874 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17875 +       if (unlikely(!args)) {
17876 +               AuErr1("no memory\n");
17877 +               iput(dir);
17878 +               goto out;
17879 +       }
17880 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17881 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17882 +       args->mask = mask;
17883 +       args->dir = dir;
17884 +       args->h_dir = igrab(h_dir);
17885 +       if (h_child_inode)
17886 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17887 +       args->h_child_inode = h_child_inode;
17888 +       args->h_child_nlen = len;
17889 +       if (len) {
17890 +               p = (void *)args;
17891 +               p += sizeof(*args);
17892 +               memcpy(p, h_child_name, len);
17893 +               p[len] = 0;
17894 +       }
17895 +
17896 +       /* NFS fires the event for silly-renamed one from kworker */
17897 +       f = 0;
17898 +       if (!dir->i_nlink
17899 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17900 +               f = AuWkq_NEST;
17901 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17902 +       if (unlikely(err)) {
17903 +               pr_err("wkq %d\n", err);
17904 +               iput(args->h_child_inode);
17905 +               iput(args->h_dir);
17906 +               iput(args->dir);
17907 +               au_kfree_rcu(args);
17908 +       }
17909 +
17910 +out:
17911 +       return err;
17912 +}
17913 +
17914 +/* ---------------------------------------------------------------------- */
17915 +
17916 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17917 +{
17918 +       int err;
17919 +
17920 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17921 +
17922 +       err = 0;
17923 +       if (au_hnotify_op.reset_br)
17924 +               err = au_hnotify_op.reset_br(udba, br, perm);
17925 +
17926 +       return err;
17927 +}
17928 +
17929 +int au_hnotify_init_br(struct au_branch *br, int perm)
17930 +{
17931 +       int err;
17932 +
17933 +       err = 0;
17934 +       if (au_hnotify_op.init_br)
17935 +               err = au_hnotify_op.init_br(br, perm);
17936 +
17937 +       return err;
17938 +}
17939 +
17940 +void au_hnotify_fin_br(struct au_branch *br)
17941 +{
17942 +       if (au_hnotify_op.fin_br)
17943 +               au_hnotify_op.fin_br(br);
17944 +}
17945 +
17946 +static void au_hn_destroy_cache(void)
17947 +{
17948 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17949 +       au_cache[AuCache_HNOTIFY] = NULL;
17950 +}
17951 +
17952 +int __init au_hnotify_init(void)
17953 +{
17954 +       int err;
17955 +
17956 +       err = -ENOMEM;
17957 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17958 +       if (au_cache[AuCache_HNOTIFY]) {
17959 +               err = 0;
17960 +               if (au_hnotify_op.init)
17961 +                       err = au_hnotify_op.init();
17962 +               if (unlikely(err))
17963 +                       au_hn_destroy_cache();
17964 +       }
17965 +       AuTraceErr(err);
17966 +       return err;
17967 +}
17968 +
17969 +void au_hnotify_fin(void)
17970 +{
17971 +       if (au_hnotify_op.fin)
17972 +               au_hnotify_op.fin();
17973 +
17974 +       /* cf. au_cache_fin() */
17975 +       if (au_cache[AuCache_HNOTIFY])
17976 +               au_hn_destroy_cache();
17977 +}
17978 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17979 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17980 +++ linux/fs/aufs/iinfo.c       2018-12-27 13:19:17.711749485 +0100
17981 @@ -0,0 +1,286 @@
17982 +// SPDX-License-Identifier: GPL-2.0
17983 +/*
17984 + * Copyright (C) 2005-2018 Junjiro R. Okajima
17985 + *
17986 + * This program, aufs is free software; you can redistribute it and/or modify
17987 + * it under the terms of the GNU General Public License as published by
17988 + * the Free Software Foundation; either version 2 of the License, or
17989 + * (at your option) any later version.
17990 + *
17991 + * This program is distributed in the hope that it will be useful,
17992 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17993 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17994 + * GNU General Public License for more details.
17995 + *
17996 + * You should have received a copy of the GNU General Public License
17997 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17998 + */
17999 +
18000 +/*
18001 + * inode private data
18002 + */
18003 +
18004 +#include "aufs.h"
18005 +
18006 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
18007 +{
18008 +       struct inode *h_inode;
18009 +       struct au_hinode *hinode;
18010 +
18011 +       IiMustAnyLock(inode);
18012 +
18013 +       hinode = au_hinode(au_ii(inode), bindex);
18014 +       h_inode = hinode->hi_inode;
18015 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18016 +       return h_inode;
18017 +}
18018 +
18019 +/* todo: hard/soft set? */
18020 +void au_hiput(struct au_hinode *hinode)
18021 +{
18022 +       au_hn_free(hinode);
18023 +       dput(hinode->hi_whdentry);
18024 +       iput(hinode->hi_inode);
18025 +}
18026 +
18027 +unsigned int au_hi_flags(struct inode *inode, int isdir)
18028 +{
18029 +       unsigned int flags;
18030 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
18031 +
18032 +       flags = 0;
18033 +       if (au_opt_test(mnt_flags, XINO))
18034 +               au_fset_hi(flags, XINO);
18035 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
18036 +               au_fset_hi(flags, HNOTIFY);
18037 +       return flags;
18038 +}
18039 +
18040 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18041 +                  struct inode *h_inode, unsigned int flags)
18042 +{
18043 +       struct au_hinode *hinode;
18044 +       struct inode *hi;
18045 +       struct au_iinfo *iinfo = au_ii(inode);
18046 +
18047 +       IiMustWriteLock(inode);
18048 +
18049 +       hinode = au_hinode(iinfo, bindex);
18050 +       hi = hinode->hi_inode;
18051 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18052 +
18053 +       if (hi)
18054 +               au_hiput(hinode);
18055 +       hinode->hi_inode = h_inode;
18056 +       if (h_inode) {
18057 +               int err;
18058 +               struct super_block *sb = inode->i_sb;
18059 +               struct au_branch *br;
18060 +
18061 +               AuDebugOn(inode->i_mode
18062 +                         && (h_inode->i_mode & S_IFMT)
18063 +                         != (inode->i_mode & S_IFMT));
18064 +               if (bindex == iinfo->ii_btop)
18065 +                       au_cpup_igen(inode, h_inode);
18066 +               br = au_sbr(sb, bindex);
18067 +               hinode->hi_id = br->br_id;
18068 +               if (au_ftest_hi(flags, XINO)) {
18069 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
18070 +                                           inode->i_ino);
18071 +                       if (unlikely(err))
18072 +                               AuIOErr1("failed au_xino_write() %d\n", err);
18073 +               }
18074 +
18075 +               if (au_ftest_hi(flags, HNOTIFY)
18076 +                   && au_br_hnotifyable(br->br_perm)) {
18077 +                       err = au_hn_alloc(hinode, inode);
18078 +                       if (unlikely(err))
18079 +                               AuIOErr1("au_hn_alloc() %d\n", err);
18080 +               }
18081 +       }
18082 +}
18083 +
18084 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18085 +                 struct dentry *h_wh)
18086 +{
18087 +       struct au_hinode *hinode;
18088 +
18089 +       IiMustWriteLock(inode);
18090 +
18091 +       hinode = au_hinode(au_ii(inode), bindex);
18092 +       AuDebugOn(hinode->hi_whdentry);
18093 +       hinode->hi_whdentry = h_wh;
18094 +}
18095 +
18096 +void au_update_iigen(struct inode *inode, int half)
18097 +{
18098 +       struct au_iinfo *iinfo;
18099 +       struct au_iigen *iigen;
18100 +       unsigned int sigen;
18101 +
18102 +       sigen = au_sigen(inode->i_sb);
18103 +       iinfo = au_ii(inode);
18104 +       iigen = &iinfo->ii_generation;
18105 +       spin_lock(&iigen->ig_spin);
18106 +       iigen->ig_generation = sigen;
18107 +       if (half)
18108 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18109 +       else
18110 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18111 +       spin_unlock(&iigen->ig_spin);
18112 +}
18113 +
18114 +/* it may be called at remount time, too */
18115 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18116 +{
18117 +       struct au_iinfo *iinfo;
18118 +       aufs_bindex_t bindex, bbot;
18119 +
18120 +       AuDebugOn(au_is_bad_inode(inode));
18121 +       IiMustWriteLock(inode);
18122 +
18123 +       iinfo = au_ii(inode);
18124 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18125 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18126 +                    bindex++) {
18127 +                       struct inode *h_i;
18128 +
18129 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18130 +                       if (h_i
18131 +                           && !h_i->i_nlink
18132 +                           && !(h_i->i_state & I_LINKABLE))
18133 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18134 +               }
18135 +       }
18136 +
18137 +       iinfo->ii_btop = -1;
18138 +       iinfo->ii_bbot = -1;
18139 +       bbot = au_sbbot(inode->i_sb);
18140 +       for (bindex = 0; bindex <= bbot; bindex++)
18141 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18142 +                       iinfo->ii_btop = bindex;
18143 +                       break;
18144 +               }
18145 +       if (iinfo->ii_btop >= 0)
18146 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18147 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18148 +                               iinfo->ii_bbot = bindex;
18149 +                               break;
18150 +                       }
18151 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18152 +}
18153 +
18154 +/* ---------------------------------------------------------------------- */
18155 +
18156 +void au_icntnr_init_once(void *_c)
18157 +{
18158 +       struct au_icntnr *c = _c;
18159 +       struct au_iinfo *iinfo = &c->iinfo;
18160 +
18161 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18162 +       au_rw_init(&iinfo->ii_rwsem);
18163 +       inode_init_once(&c->vfs_inode);
18164 +}
18165 +
18166 +void au_hinode_init(struct au_hinode *hinode)
18167 +{
18168 +       hinode->hi_inode = NULL;
18169 +       hinode->hi_id = -1;
18170 +       au_hn_init(hinode);
18171 +       hinode->hi_whdentry = NULL;
18172 +}
18173 +
18174 +int au_iinfo_init(struct inode *inode)
18175 +{
18176 +       struct au_iinfo *iinfo;
18177 +       struct super_block *sb;
18178 +       struct au_hinode *hi;
18179 +       int nbr, i;
18180 +
18181 +       sb = inode->i_sb;
18182 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18183 +       nbr = au_sbbot(sb) + 1;
18184 +       if (unlikely(nbr <= 0))
18185 +               nbr = 1;
18186 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18187 +       if (hi) {
18188 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
18189 +
18190 +               iinfo->ii_hinode = hi;
18191 +               for (i = 0; i < nbr; i++, hi++)
18192 +                       au_hinode_init(hi);
18193 +
18194 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18195 +               iinfo->ii_btop = -1;
18196 +               iinfo->ii_bbot = -1;
18197 +               iinfo->ii_vdir = NULL;
18198 +               return 0;
18199 +       }
18200 +       return -ENOMEM;
18201 +}
18202 +
18203 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18204 +{
18205 +       int err, i;
18206 +       struct au_hinode *hip;
18207 +
18208 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18209 +
18210 +       err = -ENOMEM;
18211 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18212 +                         may_shrink);
18213 +       if (hip) {
18214 +               iinfo->ii_hinode = hip;
18215 +               i = iinfo->ii_bbot + 1;
18216 +               hip += i;
18217 +               for (; i < nbr; i++, hip++)
18218 +                       au_hinode_init(hip);
18219 +               err = 0;
18220 +       }
18221 +
18222 +       return err;
18223 +}
18224 +
18225 +void au_iinfo_fin(struct inode *inode)
18226 +{
18227 +       struct au_iinfo *iinfo;
18228 +       struct au_hinode *hi;
18229 +       struct super_block *sb;
18230 +       aufs_bindex_t bindex, bbot;
18231 +       const unsigned char unlinked = !inode->i_nlink;
18232 +
18233 +       AuDebugOn(au_is_bad_inode(inode));
18234 +
18235 +       sb = inode->i_sb;
18236 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18237 +       if (si_pid_test(sb))
18238 +               au_xino_delete_inode(inode, unlinked);
18239 +       else {
18240 +               /*
18241 +                * it is safe to hide the dependency between sbinfo and
18242 +                * sb->s_umount.
18243 +                */
18244 +               lockdep_off();
18245 +               si_noflush_read_lock(sb);
18246 +               au_xino_delete_inode(inode, unlinked);
18247 +               si_read_unlock(sb);
18248 +               lockdep_on();
18249 +       }
18250 +
18251 +       iinfo = au_ii(inode);
18252 +       if (iinfo->ii_vdir)
18253 +               au_vdir_free(iinfo->ii_vdir);
18254 +
18255 +       bindex = iinfo->ii_btop;
18256 +       if (bindex >= 0) {
18257 +               hi = au_hinode(iinfo, bindex);
18258 +               bbot = iinfo->ii_bbot;
18259 +               while (bindex++ <= bbot) {
18260 +                       if (hi->hi_inode)
18261 +                               au_hiput(hi);
18262 +                       hi++;
18263 +               }
18264 +       }
18265 +       au_kfree_rcu(iinfo->ii_hinode);
18266 +       AuRwDestroy(&iinfo->ii_rwsem);
18267 +}
18268 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18269 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18270 +++ linux/fs/aufs/inode.c       2018-08-12 23:43:05.460124736 +0200
18271 @@ -0,0 +1,528 @@
18272 +// SPDX-License-Identifier: GPL-2.0
18273 +/*
18274 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18275 + *
18276 + * This program, aufs is free software; you can redistribute it and/or modify
18277 + * it under the terms of the GNU General Public License as published by
18278 + * the Free Software Foundation; either version 2 of the License, or
18279 + * (at your option) any later version.
18280 + *
18281 + * This program is distributed in the hope that it will be useful,
18282 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18283 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18284 + * GNU General Public License for more details.
18285 + *
18286 + * You should have received a copy of the GNU General Public License
18287 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18288 + */
18289 +
18290 +/*
18291 + * inode functions
18292 + */
18293 +
18294 +#include "aufs.h"
18295 +
18296 +struct inode *au_igrab(struct inode *inode)
18297 +{
18298 +       if (inode) {
18299 +               AuDebugOn(!atomic_read(&inode->i_count));
18300 +               ihold(inode);
18301 +       }
18302 +       return inode;
18303 +}
18304 +
18305 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18306 +{
18307 +       au_cpup_attr_all(inode, /*force*/0);
18308 +       au_update_iigen(inode, /*half*/1);
18309 +       if (do_version)
18310 +               inode_inc_iversion(inode);
18311 +}
18312 +
18313 +static int au_ii_refresh(struct inode *inode, int *update)
18314 +{
18315 +       int err, e, nbr;
18316 +       umode_t type;
18317 +       aufs_bindex_t bindex, new_bindex;
18318 +       struct super_block *sb;
18319 +       struct au_iinfo *iinfo;
18320 +       struct au_hinode *p, *q, tmp;
18321 +
18322 +       AuDebugOn(au_is_bad_inode(inode));
18323 +       IiMustWriteLock(inode);
18324 +
18325 +       *update = 0;
18326 +       sb = inode->i_sb;
18327 +       nbr = au_sbbot(sb) + 1;
18328 +       type = inode->i_mode & S_IFMT;
18329 +       iinfo = au_ii(inode);
18330 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18331 +       if (unlikely(err))
18332 +               goto out;
18333 +
18334 +       AuDebugOn(iinfo->ii_btop < 0);
18335 +       p = au_hinode(iinfo, iinfo->ii_btop);
18336 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18337 +            bindex++, p++) {
18338 +               if (!p->hi_inode)
18339 +                       continue;
18340 +
18341 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18342 +               new_bindex = au_br_index(sb, p->hi_id);
18343 +               if (new_bindex == bindex)
18344 +                       continue;
18345 +
18346 +               if (new_bindex < 0) {
18347 +                       *update = 1;
18348 +                       au_hiput(p);
18349 +                       p->hi_inode = NULL;
18350 +                       continue;
18351 +               }
18352 +
18353 +               if (new_bindex < iinfo->ii_btop)
18354 +                       iinfo->ii_btop = new_bindex;
18355 +               if (iinfo->ii_bbot < new_bindex)
18356 +                       iinfo->ii_bbot = new_bindex;
18357 +               /* swap two lower inode, and loop again */
18358 +               q = au_hinode(iinfo, new_bindex);
18359 +               tmp = *q;
18360 +               *q = *p;
18361 +               *p = tmp;
18362 +               if (tmp.hi_inode) {
18363 +                       bindex--;
18364 +                       p--;
18365 +               }
18366 +       }
18367 +       au_update_ibrange(inode, /*do_put_zero*/0);
18368 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18369 +       e = au_dy_irefresh(inode);
18370 +       if (unlikely(e && !err))
18371 +               err = e;
18372 +
18373 +out:
18374 +       AuTraceErr(err);
18375 +       return err;
18376 +}
18377 +
18378 +void au_refresh_iop(struct inode *inode, int force_getattr)
18379 +{
18380 +       int type;
18381 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18382 +       const struct inode_operations *iop
18383 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18384 +
18385 +       if (inode->i_op == iop)
18386 +               return;
18387 +
18388 +       switch (inode->i_mode & S_IFMT) {
18389 +       case S_IFDIR:
18390 +               type = AuIop_DIR;
18391 +               break;
18392 +       case S_IFLNK:
18393 +               type = AuIop_SYMLINK;
18394 +               break;
18395 +       default:
18396 +               type = AuIop_OTHER;
18397 +               break;
18398 +       }
18399 +
18400 +       inode->i_op = iop + type;
18401 +       /* unnecessary smp_wmb() */
18402 +}
18403 +
18404 +int au_refresh_hinode_self(struct inode *inode)
18405 +{
18406 +       int err, update;
18407 +
18408 +       err = au_ii_refresh(inode, &update);
18409 +       if (!err)
18410 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18411 +
18412 +       AuTraceErr(err);
18413 +       return err;
18414 +}
18415 +
18416 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18417 +{
18418 +       int err, e, update;
18419 +       unsigned int flags;
18420 +       umode_t mode;
18421 +       aufs_bindex_t bindex, bbot;
18422 +       unsigned char isdir;
18423 +       struct au_hinode *p;
18424 +       struct au_iinfo *iinfo;
18425 +
18426 +       err = au_ii_refresh(inode, &update);
18427 +       if (unlikely(err))
18428 +               goto out;
18429 +
18430 +       update = 0;
18431 +       iinfo = au_ii(inode);
18432 +       p = au_hinode(iinfo, iinfo->ii_btop);
18433 +       mode = (inode->i_mode & S_IFMT);
18434 +       isdir = S_ISDIR(mode);
18435 +       flags = au_hi_flags(inode, isdir);
18436 +       bbot = au_dbbot(dentry);
18437 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18438 +               struct inode *h_i, *h_inode;
18439 +               struct dentry *h_d;
18440 +
18441 +               h_d = au_h_dptr(dentry, bindex);
18442 +               if (!h_d || d_is_negative(h_d))
18443 +                       continue;
18444 +
18445 +               h_inode = d_inode(h_d);
18446 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18447 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18448 +                       h_i = au_h_iptr(inode, bindex);
18449 +                       if (h_i) {
18450 +                               if (h_i == h_inode)
18451 +                                       continue;
18452 +                               err = -EIO;
18453 +                               break;
18454 +                       }
18455 +               }
18456 +               if (bindex < iinfo->ii_btop)
18457 +                       iinfo->ii_btop = bindex;
18458 +               if (iinfo->ii_bbot < bindex)
18459 +                       iinfo->ii_bbot = bindex;
18460 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18461 +               update = 1;
18462 +       }
18463 +       au_update_ibrange(inode, /*do_put_zero*/0);
18464 +       e = au_dy_irefresh(inode);
18465 +       if (unlikely(e && !err))
18466 +               err = e;
18467 +       if (!err)
18468 +               au_refresh_hinode_attr(inode, update && isdir);
18469 +
18470 +out:
18471 +       AuTraceErr(err);
18472 +       return err;
18473 +}
18474 +
18475 +static int set_inode(struct inode *inode, struct dentry *dentry)
18476 +{
18477 +       int err;
18478 +       unsigned int flags;
18479 +       umode_t mode;
18480 +       aufs_bindex_t bindex, btop, btail;
18481 +       unsigned char isdir;
18482 +       struct dentry *h_dentry;
18483 +       struct inode *h_inode;
18484 +       struct au_iinfo *iinfo;
18485 +       struct inode_operations *iop;
18486 +
18487 +       IiMustWriteLock(inode);
18488 +
18489 +       err = 0;
18490 +       isdir = 0;
18491 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18492 +       btop = au_dbtop(dentry);
18493 +       h_dentry = au_h_dptr(dentry, btop);
18494 +       h_inode = d_inode(h_dentry);
18495 +       mode = h_inode->i_mode;
18496 +       switch (mode & S_IFMT) {
18497 +       case S_IFREG:
18498 +               btail = au_dbtail(dentry);
18499 +               inode->i_op = iop + AuIop_OTHER;
18500 +               inode->i_fop = &aufs_file_fop;
18501 +               err = au_dy_iaop(inode, btop, h_inode);
18502 +               if (unlikely(err))
18503 +                       goto out;
18504 +               break;
18505 +       case S_IFDIR:
18506 +               isdir = 1;
18507 +               btail = au_dbtaildir(dentry);
18508 +               inode->i_op = iop + AuIop_DIR;
18509 +               inode->i_fop = &aufs_dir_fop;
18510 +               break;
18511 +       case S_IFLNK:
18512 +               btail = au_dbtail(dentry);
18513 +               inode->i_op = iop + AuIop_SYMLINK;
18514 +               break;
18515 +       case S_IFBLK:
18516 +       case S_IFCHR:
18517 +       case S_IFIFO:
18518 +       case S_IFSOCK:
18519 +               btail = au_dbtail(dentry);
18520 +               inode->i_op = iop + AuIop_OTHER;
18521 +               init_special_inode(inode, mode, h_inode->i_rdev);
18522 +               break;
18523 +       default:
18524 +               AuIOErr("Unknown file type 0%o\n", mode);
18525 +               err = -EIO;
18526 +               goto out;
18527 +       }
18528 +
18529 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18530 +       flags = au_hi_flags(inode, isdir);
18531 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18532 +           && au_ftest_hi(flags, HNOTIFY)
18533 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18534 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18535 +               au_fclr_hi(flags, HNOTIFY);
18536 +       iinfo = au_ii(inode);
18537 +       iinfo->ii_btop = btop;
18538 +       iinfo->ii_bbot = btail;
18539 +       for (bindex = btop; bindex <= btail; bindex++) {
18540 +               h_dentry = au_h_dptr(dentry, bindex);
18541 +               if (h_dentry)
18542 +                       au_set_h_iptr(inode, bindex,
18543 +                                     au_igrab(d_inode(h_dentry)), flags);
18544 +       }
18545 +       au_cpup_attr_all(inode, /*force*/1);
18546 +       /*
18547 +        * to force calling aufs_get_acl() every time,
18548 +        * do not call cache_no_acl() for aufs inode.
18549 +        */
18550 +
18551 +out:
18552 +       return err;
18553 +}
18554 +
18555 +/*
18556 + * successful returns with iinfo write_locked
18557 + * minus: errno
18558 + * zero: success, matched
18559 + * plus: no error, but unmatched
18560 + */
18561 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18562 +{
18563 +       int err;
18564 +       unsigned int gen, igflags;
18565 +       aufs_bindex_t bindex, bbot;
18566 +       struct inode *h_inode, *h_dinode;
18567 +       struct dentry *h_dentry;
18568 +
18569 +       /*
18570 +        * before this function, if aufs got any iinfo lock, it must be only
18571 +        * one, the parent dir.
18572 +        * it can happen by UDBA and the obsoleted inode number.
18573 +        */
18574 +       err = -EIO;
18575 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18576 +               goto out;
18577 +
18578 +       err = 1;
18579 +       ii_write_lock_new_child(inode);
18580 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18581 +       h_dinode = d_inode(h_dentry);
18582 +       bbot = au_ibbot(inode);
18583 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18584 +               h_inode = au_h_iptr(inode, bindex);
18585 +               if (!h_inode || h_inode != h_dinode)
18586 +                       continue;
18587 +
18588 +               err = 0;
18589 +               gen = au_iigen(inode, &igflags);
18590 +               if (gen == au_digen(dentry)
18591 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18592 +                       break;
18593 +
18594 +               /* fully refresh inode using dentry */
18595 +               err = au_refresh_hinode(inode, dentry);
18596 +               if (!err)
18597 +                       au_update_iigen(inode, /*half*/0);
18598 +               break;
18599 +       }
18600 +
18601 +       if (unlikely(err))
18602 +               ii_write_unlock(inode);
18603 +out:
18604 +       return err;
18605 +}
18606 +
18607 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18608 +          unsigned int d_type, ino_t *ino)
18609 +{
18610 +       int err, idx;
18611 +       const int isnondir = d_type != DT_DIR;
18612 +
18613 +       /* prevent hardlinked inode number from race condition */
18614 +       if (isnondir) {
18615 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18616 +               if (unlikely(err))
18617 +                       goto out;
18618 +       }
18619 +
18620 +       err = au_xino_read(sb, bindex, h_ino, ino);
18621 +       if (unlikely(err))
18622 +               goto out_xinondir;
18623 +
18624 +       if (!*ino) {
18625 +               err = -EIO;
18626 +               *ino = au_xino_new_ino(sb);
18627 +               if (unlikely(!*ino))
18628 +                       goto out_xinondir;
18629 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18630 +               if (unlikely(err))
18631 +                       goto out_xinondir;
18632 +       }
18633 +
18634 +out_xinondir:
18635 +       if (isnondir && idx >= 0)
18636 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18637 +out:
18638 +       return err;
18639 +}
18640 +
18641 +/* successful returns with iinfo write_locked */
18642 +/* todo: return with unlocked? */
18643 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18644 +{
18645 +       struct inode *inode, *h_inode;
18646 +       struct dentry *h_dentry;
18647 +       struct super_block *sb;
18648 +       ino_t h_ino, ino;
18649 +       int err, idx, hlinked;
18650 +       aufs_bindex_t btop;
18651 +
18652 +       sb = dentry->d_sb;
18653 +       btop = au_dbtop(dentry);
18654 +       h_dentry = au_h_dptr(dentry, btop);
18655 +       h_inode = d_inode(h_dentry);
18656 +       h_ino = h_inode->i_ino;
18657 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18658 +
18659 +new_ino:
18660 +       /*
18661 +        * stop 'race'-ing between hardlinks under different
18662 +        * parents.
18663 +        */
18664 +       if (hlinked) {
18665 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18666 +               inode = ERR_PTR(err);
18667 +               if (unlikely(err))
18668 +                       goto out;
18669 +       }
18670 +
18671 +       err = au_xino_read(sb, btop, h_ino, &ino);
18672 +       inode = ERR_PTR(err);
18673 +       if (unlikely(err))
18674 +               goto out_xinondir;
18675 +
18676 +       if (!ino) {
18677 +               ino = au_xino_new_ino(sb);
18678 +               if (unlikely(!ino)) {
18679 +                       inode = ERR_PTR(-EIO);
18680 +                       goto out_xinondir;
18681 +               }
18682 +       }
18683 +
18684 +       AuDbg("i%lu\n", (unsigned long)ino);
18685 +       inode = au_iget_locked(sb, ino);
18686 +       err = PTR_ERR(inode);
18687 +       if (IS_ERR(inode))
18688 +               goto out_xinondir;
18689 +
18690 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18691 +       if (inode->i_state & I_NEW) {
18692 +               ii_write_lock_new_child(inode);
18693 +               err = set_inode(inode, dentry);
18694 +               if (!err) {
18695 +                       unlock_new_inode(inode);
18696 +                       goto out_xinondir; /* success */
18697 +               }
18698 +
18699 +               /*
18700 +                * iget_failed() calls iput(), but we need to call
18701 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18702 +                * i_count.
18703 +                */
18704 +               atomic_inc(&inode->i_count);
18705 +               iget_failed(inode);
18706 +               ii_write_unlock(inode);
18707 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18708 +               /* ignore this error */
18709 +               goto out_iput;
18710 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18711 +               /*
18712 +                * horrible race condition between lookup, readdir and copyup
18713 +                * (or something).
18714 +                */
18715 +               if (hlinked && idx >= 0)
18716 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18717 +               err = reval_inode(inode, dentry);
18718 +               if (unlikely(err < 0)) {
18719 +                       hlinked = 0;
18720 +                       goto out_iput;
18721 +               }
18722 +               if (!err)
18723 +                       goto out; /* success */
18724 +               else if (hlinked && idx >= 0) {
18725 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18726 +                       if (unlikely(err)) {
18727 +                               iput(inode);
18728 +                               inode = ERR_PTR(err);
18729 +                               goto out;
18730 +                       }
18731 +               }
18732 +       }
18733 +
18734 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18735 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18736 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18737 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18738 +                       (unsigned long)h_ino, (unsigned long)ino);
18739 +       ino = 0;
18740 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18741 +       if (!err) {
18742 +               iput(inode);
18743 +               if (hlinked && idx >= 0)
18744 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18745 +               goto new_ino;
18746 +       }
18747 +
18748 +out_iput:
18749 +       iput(inode);
18750 +       inode = ERR_PTR(err);
18751 +out_xinondir:
18752 +       if (hlinked && idx >= 0)
18753 +               au_xinondir_leave(sb, btop, h_ino, idx);
18754 +out:
18755 +       return inode;
18756 +}
18757 +
18758 +/* ---------------------------------------------------------------------- */
18759 +
18760 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18761 +              struct inode *inode)
18762 +{
18763 +       int err;
18764 +       struct inode *hi;
18765 +
18766 +       err = au_br_rdonly(au_sbr(sb, bindex));
18767 +
18768 +       /* pseudo-link after flushed may happen out of bounds */
18769 +       if (!err
18770 +           && inode
18771 +           && au_ibtop(inode) <= bindex
18772 +           && bindex <= au_ibbot(inode)) {
18773 +               /*
18774 +                * permission check is unnecessary since vfsub routine
18775 +                * will be called later
18776 +                */
18777 +               hi = au_h_iptr(inode, bindex);
18778 +               if (hi)
18779 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18780 +       }
18781 +
18782 +       return err;
18783 +}
18784 +
18785 +int au_test_h_perm(struct inode *h_inode, int mask)
18786 +{
18787 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18788 +               return 0;
18789 +       return inode_permission(h_inode, mask);
18790 +}
18791 +
18792 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18793 +{
18794 +       if (au_test_nfs(h_inode->i_sb)
18795 +           && (mask & MAY_WRITE)
18796 +           && S_ISDIR(h_inode->i_mode))
18797 +               mask |= MAY_READ; /* force permission check */
18798 +       return au_test_h_perm(h_inode, mask);
18799 +}
18800 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18801 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18802 +++ linux/fs/aufs/inode.h       2018-12-27 13:19:17.711749485 +0100
18803 @@ -0,0 +1,698 @@
18804 +/* SPDX-License-Identifier: GPL-2.0 */
18805 +/*
18806 + * Copyright (C) 2005-2018 Junjiro R. Okajima
18807 + *
18808 + * This program, aufs is free software; you can redistribute it and/or modify
18809 + * it under the terms of the GNU General Public License as published by
18810 + * the Free Software Foundation; either version 2 of the License, or
18811 + * (at your option) any later version.
18812 + *
18813 + * This program is distributed in the hope that it will be useful,
18814 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18815 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18816 + * GNU General Public License for more details.
18817 + *
18818 + * You should have received a copy of the GNU General Public License
18819 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18820 + */
18821 +
18822 +/*
18823 + * inode operations
18824 + */
18825 +
18826 +#ifndef __AUFS_INODE_H__
18827 +#define __AUFS_INODE_H__
18828 +
18829 +#ifdef __KERNEL__
18830 +
18831 +#include <linux/fsnotify.h>
18832 +#include "rwsem.h"
18833 +
18834 +struct vfsmount;
18835 +
18836 +struct au_hnotify {
18837 +#ifdef CONFIG_AUFS_HNOTIFY
18838 +#ifdef CONFIG_AUFS_HFSNOTIFY
18839 +       /* never use fsnotify_add_vfsmount_mark() */
18840 +       struct fsnotify_mark            hn_mark;
18841 +#endif
18842 +       struct inode            *hn_aufs_inode; /* no get/put */
18843 +       struct rcu_head         rcu;
18844 +#endif
18845 +} ____cacheline_aligned_in_smp;
18846 +
18847 +struct au_hinode {
18848 +       struct inode            *hi_inode;
18849 +       aufs_bindex_t           hi_id;
18850 +#ifdef CONFIG_AUFS_HNOTIFY
18851 +       struct au_hnotify       *hi_notify;
18852 +#endif
18853 +
18854 +       /* reference to the copied-up whiteout with get/put */
18855 +       struct dentry           *hi_whdentry;
18856 +};
18857 +
18858 +/* ig_flags */
18859 +#define AuIG_HALF_REFRESHED            1
18860 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18861 +#define au_ig_fset(flags, name) \
18862 +       do { (flags) |= AuIG_##name; } while (0)
18863 +#define au_ig_fclr(flags, name) \
18864 +       do { (flags) &= ~AuIG_##name; } while (0)
18865 +
18866 +struct au_iigen {
18867 +       spinlock_t      ig_spin;
18868 +       __u32           ig_generation, ig_flags;
18869 +};
18870 +
18871 +struct au_vdir;
18872 +struct au_iinfo {
18873 +       struct au_iigen         ii_generation;
18874 +       struct super_block      *ii_hsb1;       /* no get/put */
18875 +
18876 +       struct au_rwsem         ii_rwsem;
18877 +       aufs_bindex_t           ii_btop, ii_bbot;
18878 +       __u32                   ii_higen;
18879 +       struct au_hinode        *ii_hinode;
18880 +       struct au_vdir          *ii_vdir;
18881 +};
18882 +
18883 +struct au_icntnr {
18884 +       struct au_iinfo         iinfo;
18885 +       struct inode            vfs_inode;
18886 +       struct hlist_bl_node    plink;
18887 +       struct rcu_head         rcu;
18888 +} ____cacheline_aligned_in_smp;
18889 +
18890 +/* au_pin flags */
18891 +#define AuPin_DI_LOCKED                1
18892 +#define AuPin_MNT_WRITE                (1 << 1)
18893 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18894 +#define au_fset_pin(flags, name) \
18895 +       do { (flags) |= AuPin_##name; } while (0)
18896 +#define au_fclr_pin(flags, name) \
18897 +       do { (flags) &= ~AuPin_##name; } while (0)
18898 +
18899 +struct au_pin {
18900 +       /* input */
18901 +       struct dentry *dentry;
18902 +       unsigned int udba;
18903 +       unsigned char lsc_di, lsc_hi, flags;
18904 +       aufs_bindex_t bindex;
18905 +
18906 +       /* output */
18907 +       struct dentry *parent;
18908 +       struct au_hinode *hdir;
18909 +       struct vfsmount *h_mnt;
18910 +
18911 +       /* temporary unlock/relock for copyup */
18912 +       struct dentry *h_dentry, *h_parent;
18913 +       struct au_branch *br;
18914 +       struct task_struct *task;
18915 +};
18916 +
18917 +void au_pin_hdir_unlock(struct au_pin *p);
18918 +int au_pin_hdir_lock(struct au_pin *p);
18919 +int au_pin_hdir_relock(struct au_pin *p);
18920 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18921 +void au_pin_hdir_release(struct au_pin *p);
18922 +
18923 +/* ---------------------------------------------------------------------- */
18924 +
18925 +static inline struct au_iinfo *au_ii(struct inode *inode)
18926 +{
18927 +       BUG_ON(is_bad_inode(inode));
18928 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18929 +}
18930 +
18931 +/* ---------------------------------------------------------------------- */
18932 +
18933 +/* inode.c */
18934 +struct inode *au_igrab(struct inode *inode);
18935 +void au_refresh_iop(struct inode *inode, int force_getattr);
18936 +int au_refresh_hinode_self(struct inode *inode);
18937 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18938 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18939 +          unsigned int d_type, ino_t *ino);
18940 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18941 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18942 +              struct inode *inode);
18943 +int au_test_h_perm(struct inode *h_inode, int mask);
18944 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18945 +
18946 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18947 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18948 +{
18949 +#ifdef CONFIG_AUFS_SHWH
18950 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18951 +#else
18952 +       return 0;
18953 +#endif
18954 +}
18955 +
18956 +/* i_op.c */
18957 +enum {
18958 +       AuIop_SYMLINK,
18959 +       AuIop_DIR,
18960 +       AuIop_OTHER,
18961 +       AuIop_Last
18962 +};
18963 +extern struct inode_operations aufs_iop[AuIop_Last],
18964 +       aufs_iop_nogetattr[AuIop_Last];
18965 +
18966 +/* au_wr_dir flags */
18967 +#define AuWrDir_ADD_ENTRY      1
18968 +#define AuWrDir_ISDIR          (1 << 1)
18969 +#define AuWrDir_TMPFILE                (1 << 2)
18970 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18971 +#define au_fset_wrdir(flags, name) \
18972 +       do { (flags) |= AuWrDir_##name; } while (0)
18973 +#define au_fclr_wrdir(flags, name) \
18974 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18975 +
18976 +struct au_wr_dir_args {
18977 +       aufs_bindex_t force_btgt;
18978 +       unsigned char flags;
18979 +};
18980 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18981 +             struct au_wr_dir_args *args);
18982 +
18983 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18984 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18985 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18986 +                unsigned int udba, unsigned char flags);
18987 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18988 +          unsigned int udba, unsigned char flags) __must_check;
18989 +int au_do_pin(struct au_pin *pin) __must_check;
18990 +void au_unpin(struct au_pin *pin);
18991 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18992 +
18993 +#define AuIcpup_DID_CPUP       1
18994 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18995 +#define au_fset_icpup(flags, name) \
18996 +       do { (flags) |= AuIcpup_##name; } while (0)
18997 +#define au_fclr_icpup(flags, name) \
18998 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18999 +
19000 +struct au_icpup_args {
19001 +       unsigned char flags;
19002 +       unsigned char pin_flags;
19003 +       aufs_bindex_t btgt;
19004 +       unsigned int udba;
19005 +       struct au_pin pin;
19006 +       struct path h_path;
19007 +       struct inode *h_inode;
19008 +};
19009 +
19010 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19011 +                    struct au_icpup_args *a);
19012 +
19013 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19014 +                     int locked);
19015 +
19016 +/* i_op_add.c */
19017 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19018 +              struct dentry *h_parent, int isdir);
19019 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19020 +              dev_t dev);
19021 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
19022 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
19023 +               bool want_excl);
19024 +struct vfsub_aopen_args;
19025 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19026 +                      struct vfsub_aopen_args *args);
19027 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
19028 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
19029 +             struct dentry *dentry);
19030 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
19031 +
19032 +/* i_op_del.c */
19033 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
19034 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19035 +              struct dentry *h_parent, int isdir);
19036 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
19037 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
19038 +
19039 +/* i_op_ren.c */
19040 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
19041 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
19042 +               struct inode *dir, struct dentry *dentry,
19043 +               unsigned int flags);
19044 +
19045 +/* iinfo.c */
19046 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
19047 +void au_hiput(struct au_hinode *hinode);
19048 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19049 +                 struct dentry *h_wh);
19050 +unsigned int au_hi_flags(struct inode *inode, int isdir);
19051 +
19052 +/* hinode flags */
19053 +#define AuHi_XINO      1
19054 +#define AuHi_HNOTIFY   (1 << 1)
19055 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
19056 +#define au_fset_hi(flags, name) \
19057 +       do { (flags) |= AuHi_##name; } while (0)
19058 +#define au_fclr_hi(flags, name) \
19059 +       do { (flags) &= ~AuHi_##name; } while (0)
19060 +
19061 +#ifndef CONFIG_AUFS_HNOTIFY
19062 +#undef AuHi_HNOTIFY
19063 +#define AuHi_HNOTIFY   0
19064 +#endif
19065 +
19066 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19067 +                  struct inode *h_inode, unsigned int flags);
19068 +
19069 +void au_update_iigen(struct inode *inode, int half);
19070 +void au_update_ibrange(struct inode *inode, int do_put_zero);
19071 +
19072 +void au_icntnr_init_once(void *_c);
19073 +void au_hinode_init(struct au_hinode *hinode);
19074 +int au_iinfo_init(struct inode *inode);
19075 +void au_iinfo_fin(struct inode *inode);
19076 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
19077 +
19078 +#ifdef CONFIG_PROC_FS
19079 +/* plink.c */
19080 +int au_plink_maint(struct super_block *sb, int flags);
19081 +struct au_sbinfo;
19082 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
19083 +int au_plink_maint_enter(struct super_block *sb);
19084 +#ifdef CONFIG_AUFS_DEBUG
19085 +void au_plink_list(struct super_block *sb);
19086 +#else
19087 +AuStubVoid(au_plink_list, struct super_block *sb)
19088 +#endif
19089 +int au_plink_test(struct inode *inode);
19090 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
19091 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
19092 +                    struct dentry *h_dentry);
19093 +void au_plink_put(struct super_block *sb, int verbose);
19094 +void au_plink_clean(struct super_block *sb, int verbose);
19095 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
19096 +#else
19097 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
19098 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
19099 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
19100 +AuStubVoid(au_plink_list, struct super_block *sb);
19101 +AuStubInt0(au_plink_test, struct inode *inode);
19102 +AuStub(struct dentry *, au_plink_lkup, return NULL,
19103 +       struct inode *inode, aufs_bindex_t bindex);
19104 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
19105 +          struct dentry *h_dentry);
19106 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19107 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19108 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19109 +#endif /* CONFIG_PROC_FS */
19110 +
19111 +#ifdef CONFIG_AUFS_XATTR
19112 +/* xattr.c */
19113 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19114 +                 unsigned int verbose);
19115 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19116 +void au_xattr_init(struct super_block *sb);
19117 +#else
19118 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19119 +          int ignore_flags, unsigned int verbose);
19120 +AuStubVoid(au_xattr_init, struct super_block *sb);
19121 +#endif
19122 +
19123 +#ifdef CONFIG_FS_POSIX_ACL
19124 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19125 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19126 +#endif
19127 +
19128 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19129 +enum {
19130 +       AU_XATTR_SET,
19131 +       AU_ACL_SET
19132 +};
19133 +
19134 +struct au_sxattr {
19135 +       int type;
19136 +       union {
19137 +               struct {
19138 +                       const char      *name;
19139 +                       const void      *value;
19140 +                       size_t          size;
19141 +                       int             flags;
19142 +               } set;
19143 +               struct {
19144 +                       struct posix_acl *acl;
19145 +                       int             type;
19146 +               } acl_set;
19147 +       } u;
19148 +};
19149 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19150 +                 struct au_sxattr *arg);
19151 +#endif
19152 +
19153 +/* ---------------------------------------------------------------------- */
19154 +
19155 +/* lock subclass for iinfo */
19156 +enum {
19157 +       AuLsc_II_CHILD,         /* child first */
19158 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19159 +       AuLsc_II_CHILD3,        /* copyup dirs */
19160 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19161 +       AuLsc_II_PARENT2,
19162 +       AuLsc_II_PARENT3,       /* copyup dirs */
19163 +       AuLsc_II_NEW_CHILD
19164 +};
19165 +
19166 +/*
19167 + * ii_read_lock_child, ii_write_lock_child,
19168 + * ii_read_lock_child2, ii_write_lock_child2,
19169 + * ii_read_lock_child3, ii_write_lock_child3,
19170 + * ii_read_lock_parent, ii_write_lock_parent,
19171 + * ii_read_lock_parent2, ii_write_lock_parent2,
19172 + * ii_read_lock_parent3, ii_write_lock_parent3,
19173 + * ii_read_lock_new_child, ii_write_lock_new_child,
19174 + */
19175 +#define AuReadLockFunc(name, lsc) \
19176 +static inline void ii_read_lock_##name(struct inode *i) \
19177 +{ \
19178 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19179 +}
19180 +
19181 +#define AuWriteLockFunc(name, lsc) \
19182 +static inline void ii_write_lock_##name(struct inode *i) \
19183 +{ \
19184 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19185 +}
19186 +
19187 +#define AuRWLockFuncs(name, lsc) \
19188 +       AuReadLockFunc(name, lsc) \
19189 +       AuWriteLockFunc(name, lsc)
19190 +
19191 +AuRWLockFuncs(child, CHILD);
19192 +AuRWLockFuncs(child2, CHILD2);
19193 +AuRWLockFuncs(child3, CHILD3);
19194 +AuRWLockFuncs(parent, PARENT);
19195 +AuRWLockFuncs(parent2, PARENT2);
19196 +AuRWLockFuncs(parent3, PARENT3);
19197 +AuRWLockFuncs(new_child, NEW_CHILD);
19198 +
19199 +#undef AuReadLockFunc
19200 +#undef AuWriteLockFunc
19201 +#undef AuRWLockFuncs
19202 +
19203 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19204 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19205 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19206 +
19207 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19208 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19209 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19210 +
19211 +/* ---------------------------------------------------------------------- */
19212 +
19213 +static inline void au_icntnr_init(struct au_icntnr *c)
19214 +{
19215 +#ifdef CONFIG_AUFS_DEBUG
19216 +       c->vfs_inode.i_mode = 0;
19217 +#endif
19218 +}
19219 +
19220 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19221 +{
19222 +       unsigned int gen;
19223 +       struct au_iinfo *iinfo;
19224 +       struct au_iigen *iigen;
19225 +
19226 +       iinfo = au_ii(inode);
19227 +       iigen = &iinfo->ii_generation;
19228 +       spin_lock(&iigen->ig_spin);
19229 +       if (igflags)
19230 +               *igflags = iigen->ig_flags;
19231 +       gen = iigen->ig_generation;
19232 +       spin_unlock(&iigen->ig_spin);
19233 +
19234 +       return gen;
19235 +}
19236 +
19237 +/* tiny test for inode number */
19238 +/* tmpfs generation is too rough */
19239 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19240 +{
19241 +       struct au_iinfo *iinfo;
19242 +
19243 +       iinfo = au_ii(inode);
19244 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19245 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19246 +                && iinfo->ii_higen == h_inode->i_generation);
19247 +}
19248 +
19249 +static inline void au_iigen_dec(struct inode *inode)
19250 +{
19251 +       struct au_iinfo *iinfo;
19252 +       struct au_iigen *iigen;
19253 +
19254 +       iinfo = au_ii(inode);
19255 +       iigen = &iinfo->ii_generation;
19256 +       spin_lock(&iigen->ig_spin);
19257 +       iigen->ig_generation--;
19258 +       spin_unlock(&iigen->ig_spin);
19259 +}
19260 +
19261 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19262 +{
19263 +       int err;
19264 +
19265 +       err = 0;
19266 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19267 +               err = -EIO;
19268 +
19269 +       return err;
19270 +}
19271 +
19272 +/* ---------------------------------------------------------------------- */
19273 +
19274 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19275 +                                         aufs_bindex_t bindex)
19276 +{
19277 +       return iinfo->ii_hinode + bindex;
19278 +}
19279 +
19280 +static inline int au_is_bad_inode(struct inode *inode)
19281 +{
19282 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19283 +}
19284 +
19285 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19286 +                                       aufs_bindex_t bindex)
19287 +{
19288 +       IiMustAnyLock(inode);
19289 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19290 +}
19291 +
19292 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19293 +{
19294 +       IiMustAnyLock(inode);
19295 +       return au_ii(inode)->ii_btop;
19296 +}
19297 +
19298 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19299 +{
19300 +       IiMustAnyLock(inode);
19301 +       return au_ii(inode)->ii_bbot;
19302 +}
19303 +
19304 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19305 +{
19306 +       IiMustAnyLock(inode);
19307 +       return au_ii(inode)->ii_vdir;
19308 +}
19309 +
19310 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19311 +{
19312 +       IiMustAnyLock(inode);
19313 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19314 +}
19315 +
19316 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19317 +{
19318 +       IiMustWriteLock(inode);
19319 +       au_ii(inode)->ii_btop = bindex;
19320 +}
19321 +
19322 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19323 +{
19324 +       IiMustWriteLock(inode);
19325 +       au_ii(inode)->ii_bbot = bindex;
19326 +}
19327 +
19328 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19329 +{
19330 +       IiMustWriteLock(inode);
19331 +       au_ii(inode)->ii_vdir = vdir;
19332 +}
19333 +
19334 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19335 +{
19336 +       IiMustAnyLock(inode);
19337 +       return au_hinode(au_ii(inode), bindex);
19338 +}
19339 +
19340 +/* ---------------------------------------------------------------------- */
19341 +
19342 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19343 +{
19344 +       if (pin)
19345 +               return pin->parent;
19346 +       return NULL;
19347 +}
19348 +
19349 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19350 +{
19351 +       if (pin && pin->hdir)
19352 +               return pin->hdir->hi_inode;
19353 +       return NULL;
19354 +}
19355 +
19356 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19357 +{
19358 +       if (pin)
19359 +               return pin->hdir;
19360 +       return NULL;
19361 +}
19362 +
19363 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19364 +{
19365 +       if (pin)
19366 +               pin->dentry = dentry;
19367 +}
19368 +
19369 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19370 +                                          unsigned char lflag)
19371 +{
19372 +       if (pin) {
19373 +               if (lflag)
19374 +                       au_fset_pin(pin->flags, DI_LOCKED);
19375 +               else
19376 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19377 +       }
19378 +}
19379 +
19380 +#if 0 /* reserved */
19381 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19382 +{
19383 +       if (pin) {
19384 +               dput(pin->parent);
19385 +               pin->parent = dget(parent);
19386 +       }
19387 +}
19388 +#endif
19389 +
19390 +/* ---------------------------------------------------------------------- */
19391 +
19392 +struct au_branch;
19393 +#ifdef CONFIG_AUFS_HNOTIFY
19394 +struct au_hnotify_op {
19395 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19396 +       int (*alloc)(struct au_hinode *hinode);
19397 +
19398 +       /*
19399 +        * if it returns true, the the caller should free hinode->hi_notify,
19400 +        * otherwise ->free() frees it.
19401 +        */
19402 +       int (*free)(struct au_hinode *hinode,
19403 +                   struct au_hnotify *hn) __must_check;
19404 +
19405 +       void (*fin)(void);
19406 +       int (*init)(void);
19407 +
19408 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19409 +       void (*fin_br)(struct au_branch *br);
19410 +       int (*init_br)(struct au_branch *br, int perm);
19411 +};
19412 +
19413 +/* hnotify.c */
19414 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19415 +void au_hn_free(struct au_hinode *hinode);
19416 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19417 +void au_hn_reset(struct inode *inode, unsigned int flags);
19418 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19419 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
19420 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19421 +int au_hnotify_init_br(struct au_branch *br, int perm);
19422 +void au_hnotify_fin_br(struct au_branch *br);
19423 +int __init au_hnotify_init(void);
19424 +void au_hnotify_fin(void);
19425 +
19426 +/* hfsnotify.c */
19427 +extern const struct au_hnotify_op au_hnotify_op;
19428 +
19429 +static inline
19430 +void au_hn_init(struct au_hinode *hinode)
19431 +{
19432 +       hinode->hi_notify = NULL;
19433 +}
19434 +
19435 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19436 +{
19437 +       return hinode->hi_notify;
19438 +}
19439 +
19440 +#else
19441 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19442 +       struct au_hinode *hinode __maybe_unused,
19443 +       struct inode *inode __maybe_unused)
19444 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19445 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19446 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19447 +          int do_set __maybe_unused)
19448 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19449 +          unsigned int flags __maybe_unused)
19450 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19451 +          struct au_branch *br __maybe_unused,
19452 +          int perm __maybe_unused)
19453 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19454 +          int perm __maybe_unused)
19455 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19456 +AuStubInt0(__init au_hnotify_init, void)
19457 +AuStubVoid(au_hnotify_fin, void)
19458 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19459 +#endif /* CONFIG_AUFS_HNOTIFY */
19460 +
19461 +static inline void au_hn_suspend(struct au_hinode *hdir)
19462 +{
19463 +       au_hn_ctl(hdir, /*do_set*/0);
19464 +}
19465 +
19466 +static inline void au_hn_resume(struct au_hinode *hdir)
19467 +{
19468 +       au_hn_ctl(hdir, /*do_set*/1);
19469 +}
19470 +
19471 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19472 +{
19473 +       inode_lock(hdir->hi_inode);
19474 +       au_hn_suspend(hdir);
19475 +}
19476 +
19477 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19478 +                                         unsigned int sc __maybe_unused)
19479 +{
19480 +       inode_lock_nested(hdir->hi_inode, sc);
19481 +       au_hn_suspend(hdir);
19482 +}
19483 +
19484 +#if 0 /* unused */
19485 +#include "vfsub.h"
19486 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19487 +                                                 unsigned int sc)
19488 +{
19489 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19490 +       au_hn_suspend(hdir);
19491 +}
19492 +#endif
19493 +
19494 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19495 +{
19496 +       au_hn_resume(hdir);
19497 +       inode_unlock(hdir->hi_inode);
19498 +}
19499 +
19500 +#endif /* __KERNEL__ */
19501 +#endif /* __AUFS_INODE_H__ */
19502 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19503 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19504 +++ linux/fs/aufs/ioctl.c       2018-10-23 12:33:35.599375796 +0200
19505 @@ -0,0 +1,220 @@
19506 +// SPDX-License-Identifier: GPL-2.0
19507 +/*
19508 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19509 + *
19510 + * This program, aufs is free software; you can redistribute it and/or modify
19511 + * it under the terms of the GNU General Public License as published by
19512 + * the Free Software Foundation; either version 2 of the License, or
19513 + * (at your option) any later version.
19514 + *
19515 + * This program is distributed in the hope that it will be useful,
19516 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19517 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19518 + * GNU General Public License for more details.
19519 + *
19520 + * You should have received a copy of the GNU General Public License
19521 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19522 + */
19523 +
19524 +/*
19525 + * ioctl
19526 + * plink-management and readdir in userspace.
19527 + * assist the pathconf(3) wrapper library.
19528 + * move-down
19529 + * File-based Hierarchical Storage Management.
19530 + */
19531 +
19532 +#include <linux/compat.h>
19533 +#include <linux/file.h>
19534 +#include "aufs.h"
19535 +
19536 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19537 +{
19538 +       int err, fd;
19539 +       aufs_bindex_t wbi, bindex, bbot;
19540 +       struct file *h_file;
19541 +       struct super_block *sb;
19542 +       struct dentry *root;
19543 +       struct au_branch *br;
19544 +       struct aufs_wbr_fd wbrfd = {
19545 +               .oflags = au_dir_roflags,
19546 +               .brid   = -1
19547 +       };
19548 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19549 +               | O_NOATIME | O_CLOEXEC;
19550 +
19551 +       AuDebugOn(wbrfd.oflags & ~valid);
19552 +
19553 +       if (arg) {
19554 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19555 +               if (unlikely(err)) {
19556 +                       err = -EFAULT;
19557 +                       goto out;
19558 +               }
19559 +
19560 +               err = -EINVAL;
19561 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19562 +               wbrfd.oflags |= au_dir_roflags;
19563 +               AuDbg("0%o\n", wbrfd.oflags);
19564 +               if (unlikely(wbrfd.oflags & ~valid))
19565 +                       goto out;
19566 +       }
19567 +
19568 +       fd = get_unused_fd_flags(0);
19569 +       err = fd;
19570 +       if (unlikely(fd < 0))
19571 +               goto out;
19572 +
19573 +       h_file = ERR_PTR(-EINVAL);
19574 +       wbi = 0;
19575 +       br = NULL;
19576 +       sb = path->dentry->d_sb;
19577 +       root = sb->s_root;
19578 +       aufs_read_lock(root, AuLock_IR);
19579 +       bbot = au_sbbot(sb);
19580 +       if (wbrfd.brid >= 0) {
19581 +               wbi = au_br_index(sb, wbrfd.brid);
19582 +               if (unlikely(wbi < 0 || wbi > bbot))
19583 +                       goto out_unlock;
19584 +       }
19585 +
19586 +       h_file = ERR_PTR(-ENOENT);
19587 +       br = au_sbr(sb, wbi);
19588 +       if (!au_br_writable(br->br_perm)) {
19589 +               if (arg)
19590 +                       goto out_unlock;
19591 +
19592 +               bindex = wbi + 1;
19593 +               wbi = -1;
19594 +               for (; bindex <= bbot; bindex++) {
19595 +                       br = au_sbr(sb, bindex);
19596 +                       if (au_br_writable(br->br_perm)) {
19597 +                               wbi = bindex;
19598 +                               br = au_sbr(sb, wbi);
19599 +                               break;
19600 +                       }
19601 +               }
19602 +       }
19603 +       AuDbg("wbi %d\n", wbi);
19604 +       if (wbi >= 0)
19605 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19606 +                                  /*force_wr*/0);
19607 +
19608 +out_unlock:
19609 +       aufs_read_unlock(root, AuLock_IR);
19610 +       err = PTR_ERR(h_file);
19611 +       if (IS_ERR(h_file))
19612 +               goto out_fd;
19613 +
19614 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19615 +       fd_install(fd, h_file);
19616 +       err = fd;
19617 +       goto out; /* success */
19618 +
19619 +out_fd:
19620 +       put_unused_fd(fd);
19621 +out:
19622 +       AuTraceErr(err);
19623 +       return err;
19624 +}
19625 +
19626 +/* ---------------------------------------------------------------------- */
19627 +
19628 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19629 +{
19630 +       long err;
19631 +       struct dentry *dentry;
19632 +
19633 +       switch (cmd) {
19634 +       case AUFS_CTL_RDU:
19635 +       case AUFS_CTL_RDU_INO:
19636 +               err = au_rdu_ioctl(file, cmd, arg);
19637 +               break;
19638 +
19639 +       case AUFS_CTL_WBR_FD:
19640 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19641 +               break;
19642 +
19643 +       case AUFS_CTL_IBUSY:
19644 +               err = au_ibusy_ioctl(file, arg);
19645 +               break;
19646 +
19647 +       case AUFS_CTL_BRINFO:
19648 +               err = au_brinfo_ioctl(file, arg);
19649 +               break;
19650 +
19651 +       case AUFS_CTL_FHSM_FD:
19652 +               dentry = file->f_path.dentry;
19653 +               if (IS_ROOT(dentry))
19654 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19655 +               else
19656 +                       err = -ENOTTY;
19657 +               break;
19658 +
19659 +       default:
19660 +               /* do not call the lower */
19661 +               AuDbg("0x%x\n", cmd);
19662 +               err = -ENOTTY;
19663 +       }
19664 +
19665 +       AuTraceErr(err);
19666 +       return err;
19667 +}
19668 +
19669 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19670 +{
19671 +       long err;
19672 +
19673 +       switch (cmd) {
19674 +       case AUFS_CTL_MVDOWN:
19675 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19676 +               break;
19677 +
19678 +       case AUFS_CTL_WBR_FD:
19679 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19680 +               break;
19681 +
19682 +       default:
19683 +               /* do not call the lower */
19684 +               AuDbg("0x%x\n", cmd);
19685 +               err = -ENOTTY;
19686 +       }
19687 +
19688 +       AuTraceErr(err);
19689 +       return err;
19690 +}
19691 +
19692 +#ifdef CONFIG_COMPAT
19693 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19694 +                          unsigned long arg)
19695 +{
19696 +       long err;
19697 +
19698 +       switch (cmd) {
19699 +       case AUFS_CTL_RDU:
19700 +       case AUFS_CTL_RDU_INO:
19701 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19702 +               break;
19703 +
19704 +       case AUFS_CTL_IBUSY:
19705 +               err = au_ibusy_compat_ioctl(file, arg);
19706 +               break;
19707 +
19708 +       case AUFS_CTL_BRINFO:
19709 +               err = au_brinfo_compat_ioctl(file, arg);
19710 +               break;
19711 +
19712 +       default:
19713 +               err = aufs_ioctl_dir(file, cmd, arg);
19714 +       }
19715 +
19716 +       AuTraceErr(err);
19717 +       return err;
19718 +}
19719 +
19720 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19721 +                             unsigned long arg)
19722 +{
19723 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19724 +}
19725 +#endif
19726 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19727 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19728 +++ linux/fs/aufs/i_op_add.c    2018-12-27 13:19:17.711749485 +0100
19729 @@ -0,0 +1,935 @@
19730 +// SPDX-License-Identifier: GPL-2.0
19731 +/*
19732 + * Copyright (C) 2005-2018 Junjiro R. Okajima
19733 + *
19734 + * This program, aufs is free software; you can redistribute it and/or modify
19735 + * it under the terms of the GNU General Public License as published by
19736 + * the Free Software Foundation; either version 2 of the License, or
19737 + * (at your option) any later version.
19738 + *
19739 + * This program is distributed in the hope that it will be useful,
19740 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19741 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19742 + * GNU General Public License for more details.
19743 + *
19744 + * You should have received a copy of the GNU General Public License
19745 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19746 + */
19747 +
19748 +/*
19749 + * inode operations (add entry)
19750 + */
19751 +
19752 +#include "aufs.h"
19753 +
19754 +/*
19755 + * final procedure of adding a new entry, except link(2).
19756 + * remove whiteout, instantiate, copyup the parent dir's times and size
19757 + * and update version.
19758 + * if it failed, re-create the removed whiteout.
19759 + */
19760 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19761 +                 struct dentry *wh_dentry, struct dentry *dentry)
19762 +{
19763 +       int err, rerr;
19764 +       aufs_bindex_t bwh;
19765 +       struct path h_path;
19766 +       struct super_block *sb;
19767 +       struct inode *inode, *h_dir;
19768 +       struct dentry *wh;
19769 +
19770 +       bwh = -1;
19771 +       sb = dir->i_sb;
19772 +       if (wh_dentry) {
19773 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19774 +               IMustLock(h_dir);
19775 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19776 +               bwh = au_dbwh(dentry);
19777 +               h_path.dentry = wh_dentry;
19778 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19779 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19780 +                                         dentry);
19781 +               if (unlikely(err))
19782 +                       goto out;
19783 +       }
19784 +
19785 +       inode = au_new_inode(dentry, /*must_new*/1);
19786 +       if (!IS_ERR(inode)) {
19787 +               d_instantiate(dentry, inode);
19788 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19789 +               IMustLock(dir);
19790 +               au_dir_ts(dir, bindex);
19791 +               inode_inc_iversion(dir);
19792 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19793 +               return 0; /* success */
19794 +       }
19795 +
19796 +       err = PTR_ERR(inode);
19797 +       if (!wh_dentry)
19798 +               goto out;
19799 +
19800 +       /* revert */
19801 +       /* dir inode is locked */
19802 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19803 +       rerr = PTR_ERR(wh);
19804 +       if (IS_ERR(wh)) {
19805 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19806 +                       dentry, err, rerr);
19807 +               err = -EIO;
19808 +       } else
19809 +               dput(wh);
19810 +
19811 +out:
19812 +       return err;
19813 +}
19814 +
19815 +static int au_d_may_add(struct dentry *dentry)
19816 +{
19817 +       int err;
19818 +
19819 +       err = 0;
19820 +       if (unlikely(d_unhashed(dentry)))
19821 +               err = -ENOENT;
19822 +       if (unlikely(d_really_is_positive(dentry)))
19823 +               err = -EEXIST;
19824 +       return err;
19825 +}
19826 +
19827 +/*
19828 + * simple tests for the adding inode operations.
19829 + * following the checks in vfs, plus the parent-child relationship.
19830 + */
19831 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19832 +              struct dentry *h_parent, int isdir)
19833 +{
19834 +       int err;
19835 +       umode_t h_mode;
19836 +       struct dentry *h_dentry;
19837 +       struct inode *h_inode;
19838 +
19839 +       err = -ENAMETOOLONG;
19840 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19841 +               goto out;
19842 +
19843 +       h_dentry = au_h_dptr(dentry, bindex);
19844 +       if (d_really_is_negative(dentry)) {
19845 +               err = -EEXIST;
19846 +               if (unlikely(d_is_positive(h_dentry)))
19847 +                       goto out;
19848 +       } else {
19849 +               /* rename(2) case */
19850 +               err = -EIO;
19851 +               if (unlikely(d_is_negative(h_dentry)))
19852 +                       goto out;
19853 +               h_inode = d_inode(h_dentry);
19854 +               if (unlikely(!h_inode->i_nlink))
19855 +                       goto out;
19856 +
19857 +               h_mode = h_inode->i_mode;
19858 +               if (!isdir) {
19859 +                       err = -EISDIR;
19860 +                       if (unlikely(S_ISDIR(h_mode)))
19861 +                               goto out;
19862 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19863 +                       err = -ENOTDIR;
19864 +                       goto out;
19865 +               }
19866 +       }
19867 +
19868 +       err = 0;
19869 +       /* expected parent dir is locked */
19870 +       if (unlikely(h_parent != h_dentry->d_parent))
19871 +               err = -EIO;
19872 +
19873 +out:
19874 +       AuTraceErr(err);
19875 +       return err;
19876 +}
19877 +
19878 +/*
19879 + * initial procedure of adding a new entry.
19880 + * prepare writable branch and the parent dir, lock it,
19881 + * and lookup whiteout for the new entry.
19882 + */
19883 +static struct dentry*
19884 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19885 +                 struct dentry *src_dentry, struct au_pin *pin,
19886 +                 struct au_wr_dir_args *wr_dir_args)
19887 +{
19888 +       struct dentry *wh_dentry, *h_parent;
19889 +       struct super_block *sb;
19890 +       struct au_branch *br;
19891 +       int err;
19892 +       unsigned int udba;
19893 +       aufs_bindex_t bcpup;
19894 +
19895 +       AuDbg("%pd\n", dentry);
19896 +
19897 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19898 +       bcpup = err;
19899 +       wh_dentry = ERR_PTR(err);
19900 +       if (unlikely(err < 0))
19901 +               goto out;
19902 +
19903 +       sb = dentry->d_sb;
19904 +       udba = au_opt_udba(sb);
19905 +       err = au_pin(pin, dentry, bcpup, udba,
19906 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19907 +       wh_dentry = ERR_PTR(err);
19908 +       if (unlikely(err))
19909 +               goto out;
19910 +
19911 +       h_parent = au_pinned_h_parent(pin);
19912 +       if (udba != AuOpt_UDBA_NONE
19913 +           && au_dbtop(dentry) == bcpup)
19914 +               err = au_may_add(dentry, bcpup, h_parent,
19915 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19916 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19917 +               err = -ENAMETOOLONG;
19918 +       wh_dentry = ERR_PTR(err);
19919 +       if (unlikely(err))
19920 +               goto out_unpin;
19921 +
19922 +       br = au_sbr(sb, bcpup);
19923 +       if (dt) {
19924 +               struct path tmp = {
19925 +                       .dentry = h_parent,
19926 +                       .mnt    = au_br_mnt(br)
19927 +               };
19928 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19929 +       }
19930 +
19931 +       wh_dentry = NULL;
19932 +       if (bcpup != au_dbwh(dentry))
19933 +               goto out; /* success */
19934 +
19935 +       /*
19936 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19937 +        * would not be able to removed in the future. So we don't allow such
19938 +        * name here and we don't handle ENAMETOOLONG differently here.
19939 +        */
19940 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19941 +
19942 +out_unpin:
19943 +       if (IS_ERR(wh_dentry))
19944 +               au_unpin(pin);
19945 +out:
19946 +       return wh_dentry;
19947 +}
19948 +
19949 +/* ---------------------------------------------------------------------- */
19950 +
19951 +enum { Mknod, Symlink, Creat };
19952 +struct simple_arg {
19953 +       int type;
19954 +       union {
19955 +               struct {
19956 +                       umode_t                 mode;
19957 +                       bool                    want_excl;
19958 +                       bool                    try_aopen;
19959 +                       struct vfsub_aopen_args *aopen;
19960 +               } c;
19961 +               struct {
19962 +                       const char *symname;
19963 +               } s;
19964 +               struct {
19965 +                       umode_t mode;
19966 +                       dev_t dev;
19967 +               } m;
19968 +       } u;
19969 +};
19970 +
19971 +static int add_simple(struct inode *dir, struct dentry *dentry,
19972 +                     struct simple_arg *arg)
19973 +{
19974 +       int err, rerr;
19975 +       aufs_bindex_t btop;
19976 +       unsigned char created;
19977 +       const unsigned char try_aopen
19978 +               = (arg->type == Creat && arg->u.c.try_aopen);
19979 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19980 +       struct dentry *wh_dentry, *parent;
19981 +       struct inode *h_dir;
19982 +       struct super_block *sb;
19983 +       struct au_branch *br;
19984 +       /* to reduce stack size */
19985 +       struct {
19986 +               struct au_dtime dt;
19987 +               struct au_pin pin;
19988 +               struct path h_path;
19989 +               struct au_wr_dir_args wr_dir_args;
19990 +       } *a;
19991 +
19992 +       AuDbg("%pd\n", dentry);
19993 +       IMustLock(dir);
19994 +
19995 +       err = -ENOMEM;
19996 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19997 +       if (unlikely(!a))
19998 +               goto out;
19999 +       a->wr_dir_args.force_btgt = -1;
20000 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
20001 +
20002 +       parent = dentry->d_parent; /* dir inode is locked */
20003 +       if (!try_aopen) {
20004 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20005 +               if (unlikely(err))
20006 +                       goto out_free;
20007 +       }
20008 +       err = au_d_may_add(dentry);
20009 +       if (unlikely(err))
20010 +               goto out_unlock;
20011 +       if (!try_aopen)
20012 +               di_write_lock_parent(parent);
20013 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20014 +                                     &a->pin, &a->wr_dir_args);
20015 +       err = PTR_ERR(wh_dentry);
20016 +       if (IS_ERR(wh_dentry))
20017 +               goto out_parent;
20018 +
20019 +       btop = au_dbtop(dentry);
20020 +       sb = dentry->d_sb;
20021 +       br = au_sbr(sb, btop);
20022 +       a->h_path.dentry = au_h_dptr(dentry, btop);
20023 +       a->h_path.mnt = au_br_mnt(br);
20024 +       h_dir = au_pinned_h_dir(&a->pin);
20025 +       switch (arg->type) {
20026 +       case Creat:
20027 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
20028 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
20029 +                                          arg->u.c.want_excl);
20030 +                       created = !err;
20031 +                       if (!err && try_aopen)
20032 +                               aopen->file->f_mode |= FMODE_CREATED;
20033 +               } else {
20034 +                       aopen->br = br;
20035 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
20036 +                       AuDbg("err %d\n", err);
20037 +                       AuDbgFile(aopen->file);
20038 +                       created = err >= 0
20039 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
20040 +               }
20041 +               break;
20042 +       case Symlink:
20043 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
20044 +               created = !err;
20045 +               break;
20046 +       case Mknod:
20047 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
20048 +                                 arg->u.m.dev);
20049 +               created = !err;
20050 +               break;
20051 +       default:
20052 +               BUG();
20053 +       }
20054 +       if (unlikely(err < 0))
20055 +               goto out_unpin;
20056 +
20057 +       err = epilog(dir, btop, wh_dentry, dentry);
20058 +       if (!err)
20059 +               goto out_unpin; /* success */
20060 +
20061 +       /* revert */
20062 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
20063 +               /* no delegation since it is just created */
20064 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
20065 +                                   /*force*/0);
20066 +               if (rerr) {
20067 +                       AuIOErr("%pd revert failure(%d, %d)\n",
20068 +                               dentry, err, rerr);
20069 +                       err = -EIO;
20070 +               }
20071 +               au_dtime_revert(&a->dt);
20072 +       }
20073 +       if (try_aopen && h_dir->i_op->atomic_open
20074 +           && (aopen->file->f_mode & FMODE_OPENED))
20075 +               /* aopen->file is still opened */
20076 +               au_lcnt_dec(&aopen->br->br_nfiles);
20077 +
20078 +out_unpin:
20079 +       au_unpin(&a->pin);
20080 +       dput(wh_dentry);
20081 +out_parent:
20082 +       if (!try_aopen)
20083 +               di_write_unlock(parent);
20084 +out_unlock:
20085 +       if (unlikely(err)) {
20086 +               au_update_dbtop(dentry);
20087 +               d_drop(dentry);
20088 +       }
20089 +       if (!try_aopen)
20090 +               aufs_read_unlock(dentry, AuLock_DW);
20091 +out_free:
20092 +       au_kfree_rcu(a);
20093 +out:
20094 +       return err;
20095 +}
20096 +
20097 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
20098 +              dev_t dev)
20099 +{
20100 +       struct simple_arg arg = {
20101 +               .type = Mknod,
20102 +               .u.m = {
20103 +                       .mode   = mode,
20104 +                       .dev    = dev
20105 +               }
20106 +       };
20107 +       return add_simple(dir, dentry, &arg);
20108 +}
20109 +
20110 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20111 +{
20112 +       struct simple_arg arg = {
20113 +               .type = Symlink,
20114 +               .u.s.symname = symname
20115 +       };
20116 +       return add_simple(dir, dentry, &arg);
20117 +}
20118 +
20119 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20120 +               bool want_excl)
20121 +{
20122 +       struct simple_arg arg = {
20123 +               .type = Creat,
20124 +               .u.c = {
20125 +                       .mode           = mode,
20126 +                       .want_excl      = want_excl
20127 +               }
20128 +       };
20129 +       return add_simple(dir, dentry, &arg);
20130 +}
20131 +
20132 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20133 +                      struct vfsub_aopen_args *aopen_args)
20134 +{
20135 +       struct simple_arg arg = {
20136 +               .type = Creat,
20137 +               .u.c = {
20138 +                       .mode           = aopen_args->create_mode,
20139 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20140 +                       .try_aopen      = true,
20141 +                       .aopen          = aopen_args
20142 +               }
20143 +       };
20144 +       return add_simple(dir, dentry, &arg);
20145 +}
20146 +
20147 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20148 +{
20149 +       int err;
20150 +       aufs_bindex_t bindex;
20151 +       struct super_block *sb;
20152 +       struct dentry *parent, *h_parent, *h_dentry;
20153 +       struct inode *h_dir, *inode;
20154 +       struct vfsmount *h_mnt;
20155 +       struct au_wr_dir_args wr_dir_args = {
20156 +               .force_btgt     = -1,
20157 +               .flags          = AuWrDir_TMPFILE
20158 +       };
20159 +
20160 +       /* copy-up may happen */
20161 +       inode_lock(dir);
20162 +
20163 +       sb = dir->i_sb;
20164 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20165 +       if (unlikely(err))
20166 +               goto out;
20167 +
20168 +       err = au_di_init(dentry);
20169 +       if (unlikely(err))
20170 +               goto out_si;
20171 +
20172 +       err = -EBUSY;
20173 +       parent = d_find_any_alias(dir);
20174 +       AuDebugOn(!parent);
20175 +       di_write_lock_parent(parent);
20176 +       if (unlikely(d_inode(parent) != dir))
20177 +               goto out_parent;
20178 +
20179 +       err = au_digen_test(parent, au_sigen(sb));
20180 +       if (unlikely(err))
20181 +               goto out_parent;
20182 +
20183 +       bindex = au_dbtop(parent);
20184 +       au_set_dbtop(dentry, bindex);
20185 +       au_set_dbbot(dentry, bindex);
20186 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20187 +       bindex = err;
20188 +       if (unlikely(err < 0))
20189 +               goto out_parent;
20190 +
20191 +       err = -EOPNOTSUPP;
20192 +       h_dir = au_h_iptr(dir, bindex);
20193 +       if (unlikely(!h_dir->i_op->tmpfile))
20194 +               goto out_parent;
20195 +
20196 +       h_mnt = au_sbr_mnt(sb, bindex);
20197 +       err = vfsub_mnt_want_write(h_mnt);
20198 +       if (unlikely(err))
20199 +               goto out_parent;
20200 +
20201 +       h_parent = au_h_dptr(parent, bindex);
20202 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20203 +       if (IS_ERR(h_dentry)) {
20204 +               err = PTR_ERR(h_dentry);
20205 +               goto out_mnt;
20206 +       }
20207 +
20208 +       au_set_dbtop(dentry, bindex);
20209 +       au_set_dbbot(dentry, bindex);
20210 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20211 +       inode = au_new_inode(dentry, /*must_new*/1);
20212 +       if (IS_ERR(inode)) {
20213 +               err = PTR_ERR(inode);
20214 +               au_set_h_dptr(dentry, bindex, NULL);
20215 +               au_set_dbtop(dentry, -1);
20216 +               au_set_dbbot(dentry, -1);
20217 +       } else {
20218 +               if (!inode->i_nlink)
20219 +                       set_nlink(inode, 1);
20220 +               d_tmpfile(dentry, inode);
20221 +               au_di(dentry)->di_tmpfile = 1;
20222 +
20223 +               /* update without i_mutex */
20224 +               if (au_ibtop(dir) == au_dbtop(dentry))
20225 +                       au_cpup_attr_timesizes(dir);
20226 +       }
20227 +       dput(h_dentry);
20228 +
20229 +out_mnt:
20230 +       vfsub_mnt_drop_write(h_mnt);
20231 +out_parent:
20232 +       di_write_unlock(parent);
20233 +       dput(parent);
20234 +       di_write_unlock(dentry);
20235 +       if (unlikely(err)) {
20236 +               au_di_fin(dentry);
20237 +               dentry->d_fsdata = NULL;
20238 +       }
20239 +out_si:
20240 +       si_read_unlock(sb);
20241 +out:
20242 +       inode_unlock(dir);
20243 +       return err;
20244 +}
20245 +
20246 +/* ---------------------------------------------------------------------- */
20247 +
20248 +struct au_link_args {
20249 +       aufs_bindex_t bdst, bsrc;
20250 +       struct au_pin pin;
20251 +       struct path h_path;
20252 +       struct dentry *src_parent, *parent;
20253 +};
20254 +
20255 +static int au_cpup_before_link(struct dentry *src_dentry,
20256 +                              struct au_link_args *a)
20257 +{
20258 +       int err;
20259 +       struct dentry *h_src_dentry;
20260 +       struct au_cp_generic cpg = {
20261 +               .dentry = src_dentry,
20262 +               .bdst   = a->bdst,
20263 +               .bsrc   = a->bsrc,
20264 +               .len    = -1,
20265 +               .pin    = &a->pin,
20266 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20267 +       };
20268 +
20269 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20270 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20271 +       if (unlikely(err))
20272 +               goto out;
20273 +
20274 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20275 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20276 +                    au_opt_udba(src_dentry->d_sb),
20277 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20278 +       if (unlikely(err))
20279 +               goto out;
20280 +
20281 +       err = au_sio_cpup_simple(&cpg);
20282 +       au_unpin(&a->pin);
20283 +
20284 +out:
20285 +       di_read_unlock(a->src_parent, AuLock_IR);
20286 +       return err;
20287 +}
20288 +
20289 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20290 +                          struct au_link_args *a)
20291 +{
20292 +       int err;
20293 +       unsigned char plink;
20294 +       aufs_bindex_t bbot;
20295 +       struct dentry *h_src_dentry;
20296 +       struct inode *h_inode, *inode, *delegated;
20297 +       struct super_block *sb;
20298 +       struct file *h_file;
20299 +
20300 +       plink = 0;
20301 +       h_inode = NULL;
20302 +       sb = src_dentry->d_sb;
20303 +       inode = d_inode(src_dentry);
20304 +       if (au_ibtop(inode) <= a->bdst)
20305 +               h_inode = au_h_iptr(inode, a->bdst);
20306 +       if (!h_inode || !h_inode->i_nlink) {
20307 +               /* copyup src_dentry as the name of dentry. */
20308 +               bbot = au_dbbot(dentry);
20309 +               if (bbot < a->bsrc)
20310 +                       au_set_dbbot(dentry, a->bsrc);
20311 +               au_set_h_dptr(dentry, a->bsrc,
20312 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20313 +               dget(a->h_path.dentry);
20314 +               au_set_h_dptr(dentry, a->bdst, NULL);
20315 +               AuDbg("temporary d_inode...\n");
20316 +               spin_lock(&dentry->d_lock);
20317 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20318 +               spin_unlock(&dentry->d_lock);
20319 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20320 +               if (IS_ERR(h_file))
20321 +                       err = PTR_ERR(h_file);
20322 +               else {
20323 +                       struct au_cp_generic cpg = {
20324 +                               .dentry = dentry,
20325 +                               .bdst   = a->bdst,
20326 +                               .bsrc   = -1,
20327 +                               .len    = -1,
20328 +                               .pin    = &a->pin,
20329 +                               .flags  = AuCpup_KEEPLINO
20330 +                       };
20331 +                       err = au_sio_cpup_simple(&cpg);
20332 +                       au_h_open_post(dentry, a->bsrc, h_file);
20333 +                       if (!err) {
20334 +                               dput(a->h_path.dentry);
20335 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20336 +                       } else
20337 +                               au_set_h_dptr(dentry, a->bdst,
20338 +                                             a->h_path.dentry);
20339 +               }
20340 +               spin_lock(&dentry->d_lock);
20341 +               dentry->d_inode = NULL; /* restore */
20342 +               spin_unlock(&dentry->d_lock);
20343 +               AuDbg("temporary d_inode...done\n");
20344 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20345 +               au_set_dbbot(dentry, bbot);
20346 +       } else {
20347 +               /* the inode of src_dentry already exists on a.bdst branch */
20348 +               h_src_dentry = d_find_alias(h_inode);
20349 +               if (!h_src_dentry && au_plink_test(inode)) {
20350 +                       plink = 1;
20351 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20352 +                       err = PTR_ERR(h_src_dentry);
20353 +                       if (IS_ERR(h_src_dentry))
20354 +                               goto out;
20355 +
20356 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20357 +                               dput(h_src_dentry);
20358 +                               h_src_dentry = NULL;
20359 +                       }
20360 +
20361 +               }
20362 +               if (h_src_dentry) {
20363 +                       delegated = NULL;
20364 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20365 +                                        &a->h_path, &delegated);
20366 +                       if (unlikely(err == -EWOULDBLOCK)) {
20367 +                               pr_warn("cannot retry for NFSv4 delegation"
20368 +                                       " for an internal link\n");
20369 +                               iput(delegated);
20370 +                       }
20371 +                       dput(h_src_dentry);
20372 +               } else {
20373 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20374 +                               h_inode->i_ino, a->bdst);
20375 +                       err = -EIO;
20376 +               }
20377 +       }
20378 +
20379 +       if (!err && !plink)
20380 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20381 +
20382 +out:
20383 +       AuTraceErr(err);
20384 +       return err;
20385 +}
20386 +
20387 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20388 +             struct dentry *dentry)
20389 +{
20390 +       int err, rerr;
20391 +       struct au_dtime dt;
20392 +       struct au_link_args *a;
20393 +       struct dentry *wh_dentry, *h_src_dentry;
20394 +       struct inode *inode, *delegated;
20395 +       struct super_block *sb;
20396 +       struct au_wr_dir_args wr_dir_args = {
20397 +               /* .force_btgt  = -1, */
20398 +               .flags          = AuWrDir_ADD_ENTRY
20399 +       };
20400 +
20401 +       IMustLock(dir);
20402 +       inode = d_inode(src_dentry);
20403 +       IMustLock(inode);
20404 +
20405 +       err = -ENOMEM;
20406 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20407 +       if (unlikely(!a))
20408 +               goto out;
20409 +
20410 +       a->parent = dentry->d_parent; /* dir inode is locked */
20411 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20412 +                                       AuLock_NOPLM | AuLock_GEN);
20413 +       if (unlikely(err))
20414 +               goto out_kfree;
20415 +       err = au_d_linkable(src_dentry);
20416 +       if (unlikely(err))
20417 +               goto out_unlock;
20418 +       err = au_d_may_add(dentry);
20419 +       if (unlikely(err))
20420 +               goto out_unlock;
20421 +
20422 +       a->src_parent = dget_parent(src_dentry);
20423 +       wr_dir_args.force_btgt = au_ibtop(inode);
20424 +
20425 +       di_write_lock_parent(a->parent);
20426 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20427 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20428 +                                     &wr_dir_args);
20429 +       err = PTR_ERR(wh_dentry);
20430 +       if (IS_ERR(wh_dentry))
20431 +               goto out_parent;
20432 +
20433 +       err = 0;
20434 +       sb = dentry->d_sb;
20435 +       a->bdst = au_dbtop(dentry);
20436 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20437 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20438 +       a->bsrc = au_ibtop(inode);
20439 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20440 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20441 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20442 +       if (!h_src_dentry) {
20443 +               a->bsrc = au_dbtop(src_dentry);
20444 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20445 +               AuDebugOn(!h_src_dentry);
20446 +       } else if (IS_ERR(h_src_dentry)) {
20447 +               err = PTR_ERR(h_src_dentry);
20448 +               goto out_parent;
20449 +       }
20450 +
20451 +       /*
20452 +        * aufs doesn't touch the credential so
20453 +        * security_dentry_create_files_as() is unnecessary.
20454 +        */
20455 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20456 +               if (a->bdst < a->bsrc
20457 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20458 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20459 +               else {
20460 +                       delegated = NULL;
20461 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20462 +                                        &a->h_path, &delegated);
20463 +                       if (unlikely(err == -EWOULDBLOCK)) {
20464 +                               pr_warn("cannot retry for NFSv4 delegation"
20465 +                                       " for an internal link\n");
20466 +                               iput(delegated);
20467 +                       }
20468 +               }
20469 +               dput(h_src_dentry);
20470 +       } else {
20471 +               /*
20472 +                * copyup src_dentry to the branch we process,
20473 +                * and then link(2) to it.
20474 +                */
20475 +               dput(h_src_dentry);
20476 +               if (a->bdst < a->bsrc
20477 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20478 +                       au_unpin(&a->pin);
20479 +                       di_write_unlock(a->parent);
20480 +                       err = au_cpup_before_link(src_dentry, a);
20481 +                       di_write_lock_parent(a->parent);
20482 +                       if (!err)
20483 +                               err = au_pin(&a->pin, dentry, a->bdst,
20484 +                                            au_opt_udba(sb),
20485 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20486 +                       if (unlikely(err))
20487 +                               goto out_wh;
20488 +               }
20489 +               if (!err) {
20490 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20491 +                       err = -ENOENT;
20492 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20493 +                               delegated = NULL;
20494 +                               err = vfsub_link(h_src_dentry,
20495 +                                                au_pinned_h_dir(&a->pin),
20496 +                                                &a->h_path, &delegated);
20497 +                               if (unlikely(err == -EWOULDBLOCK)) {
20498 +                                       pr_warn("cannot retry"
20499 +                                               " for NFSv4 delegation"
20500 +                                               " for an internal link\n");
20501 +                                       iput(delegated);
20502 +                               }
20503 +                       }
20504 +               }
20505 +       }
20506 +       if (unlikely(err))
20507 +               goto out_unpin;
20508 +
20509 +       if (wh_dentry) {
20510 +               a->h_path.dentry = wh_dentry;
20511 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20512 +                                         dentry);
20513 +               if (unlikely(err))
20514 +                       goto out_revert;
20515 +       }
20516 +
20517 +       au_dir_ts(dir, a->bdst);
20518 +       inode_inc_iversion(dir);
20519 +       inc_nlink(inode);
20520 +       inode->i_ctime = dir->i_ctime;
20521 +       d_instantiate(dentry, au_igrab(inode));
20522 +       if (d_unhashed(a->h_path.dentry))
20523 +               /* some filesystem calls d_drop() */
20524 +               d_drop(dentry);
20525 +       /* some filesystems consume an inode even hardlink */
20526 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20527 +       goto out_unpin; /* success */
20528 +
20529 +out_revert:
20530 +       /* no delegation since it is just created */
20531 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20532 +                           /*delegated*/NULL, /*force*/0);
20533 +       if (unlikely(rerr)) {
20534 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20535 +               err = -EIO;
20536 +       }
20537 +       au_dtime_revert(&dt);
20538 +out_unpin:
20539 +       au_unpin(&a->pin);
20540 +out_wh:
20541 +       dput(wh_dentry);
20542 +out_parent:
20543 +       di_write_unlock(a->parent);
20544 +       dput(a->src_parent);
20545 +out_unlock:
20546 +       if (unlikely(err)) {
20547 +               au_update_dbtop(dentry);
20548 +               d_drop(dentry);
20549 +       }
20550 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20551 +out_kfree:
20552 +       au_kfree_rcu(a);
20553 +out:
20554 +       AuTraceErr(err);
20555 +       return err;
20556 +}
20557 +
20558 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20559 +{
20560 +       int err, rerr;
20561 +       aufs_bindex_t bindex;
20562 +       unsigned char diropq;
20563 +       struct path h_path;
20564 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20565 +       struct inode *h_inode;
20566 +       struct super_block *sb;
20567 +       struct {
20568 +               struct au_pin pin;
20569 +               struct au_dtime dt;
20570 +       } *a; /* reduce the stack usage */
20571 +       struct au_wr_dir_args wr_dir_args = {
20572 +               .force_btgt     = -1,
20573 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20574 +       };
20575 +
20576 +       IMustLock(dir);
20577 +
20578 +       err = -ENOMEM;
20579 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20580 +       if (unlikely(!a))
20581 +               goto out;
20582 +
20583 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20584 +       if (unlikely(err))
20585 +               goto out_free;
20586 +       err = au_d_may_add(dentry);
20587 +       if (unlikely(err))
20588 +               goto out_unlock;
20589 +
20590 +       parent = dentry->d_parent; /* dir inode is locked */
20591 +       di_write_lock_parent(parent);
20592 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20593 +                                     &a->pin, &wr_dir_args);
20594 +       err = PTR_ERR(wh_dentry);
20595 +       if (IS_ERR(wh_dentry))
20596 +               goto out_parent;
20597 +
20598 +       sb = dentry->d_sb;
20599 +       bindex = au_dbtop(dentry);
20600 +       h_path.dentry = au_h_dptr(dentry, bindex);
20601 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20602 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20603 +       if (unlikely(err))
20604 +               goto out_unpin;
20605 +
20606 +       /* make the dir opaque */
20607 +       diropq = 0;
20608 +       h_inode = d_inode(h_path.dentry);
20609 +       if (wh_dentry
20610 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20611 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20612 +               opq_dentry = au_diropq_create(dentry, bindex);
20613 +               inode_unlock(h_inode);
20614 +               err = PTR_ERR(opq_dentry);
20615 +               if (IS_ERR(opq_dentry))
20616 +                       goto out_dir;
20617 +               dput(opq_dentry);
20618 +               diropq = 1;
20619 +       }
20620 +
20621 +       err = epilog(dir, bindex, wh_dentry, dentry);
20622 +       if (!err) {
20623 +               inc_nlink(dir);
20624 +               goto out_unpin; /* success */
20625 +       }
20626 +
20627 +       /* revert */
20628 +       if (diropq) {
20629 +               AuLabel(revert opq);
20630 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20631 +               rerr = au_diropq_remove(dentry, bindex);
20632 +               inode_unlock(h_inode);
20633 +               if (rerr) {
20634 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20635 +                               dentry, err, rerr);
20636 +                       err = -EIO;
20637 +               }
20638 +       }
20639 +
20640 +out_dir:
20641 +       AuLabel(revert dir);
20642 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20643 +       if (rerr) {
20644 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20645 +                       dentry, err, rerr);
20646 +               err = -EIO;
20647 +       }
20648 +       au_dtime_revert(&a->dt);
20649 +out_unpin:
20650 +       au_unpin(&a->pin);
20651 +       dput(wh_dentry);
20652 +out_parent:
20653 +       di_write_unlock(parent);
20654 +out_unlock:
20655 +       if (unlikely(err)) {
20656 +               au_update_dbtop(dentry);
20657 +               d_drop(dentry);
20658 +       }
20659 +       aufs_read_unlock(dentry, AuLock_DW);
20660 +out_free:
20661 +       au_kfree_rcu(a);
20662 +out:
20663 +       return err;
20664 +}
20665 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20666 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20667 +++ linux/fs/aufs/i_op.c        2018-12-27 13:19:17.708416053 +0100
20668 @@ -0,0 +1,1506 @@
20669 +// SPDX-License-Identifier: GPL-2.0
20670 +/*
20671 + * Copyright (C) 2005-2018 Junjiro R. Okajima
20672 + *
20673 + * This program, aufs is free software; you can redistribute it and/or modify
20674 + * it under the terms of the GNU General Public License as published by
20675 + * the Free Software Foundation; either version 2 of the License, or
20676 + * (at your option) any later version.
20677 + *
20678 + * This program is distributed in the hope that it will be useful,
20679 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20680 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20681 + * GNU General Public License for more details.
20682 + *
20683 + * You should have received a copy of the GNU General Public License
20684 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20685 + */
20686 +
20687 +/*
20688 + * inode operations (except add/del/rename)
20689 + */
20690 +
20691 +#include <linux/device_cgroup.h>
20692 +#include <linux/fs_stack.h>
20693 +#include <linux/namei.h>
20694 +#include <linux/security.h>
20695 +#include "aufs.h"
20696 +
20697 +static int h_permission(struct inode *h_inode, int mask,
20698 +                       struct path *h_path, int brperm)
20699 +{
20700 +       int err;
20701 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20702 +
20703 +       err = -EPERM;
20704 +       if (write_mask && IS_IMMUTABLE(h_inode))
20705 +               goto out;
20706 +
20707 +       err = -EACCES;
20708 +       if (((mask & MAY_EXEC)
20709 +            && S_ISREG(h_inode->i_mode)
20710 +            && (path_noexec(h_path)
20711 +                || !(h_inode->i_mode & 0111))))
20712 +               goto out;
20713 +
20714 +       /*
20715 +        * - skip the lower fs test in the case of write to ro branch.
20716 +        * - nfs dir permission write check is optimized, but a policy for
20717 +        *   link/rename requires a real check.
20718 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20719 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20720 +        */
20721 +       if ((write_mask && !au_br_writable(brperm))
20722 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20723 +               && write_mask && !(mask & MAY_READ))
20724 +           || !h_inode->i_op->permission) {
20725 +               /* AuLabel(generic_permission); */
20726 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20727 +               err = generic_permission(h_inode, mask);
20728 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20729 +                       err = h_inode->i_op->permission(h_inode, mask);
20730 +               AuTraceErr(err);
20731 +       } else {
20732 +               /* AuLabel(h_inode->permission); */
20733 +               err = h_inode->i_op->permission(h_inode, mask);
20734 +               AuTraceErr(err);
20735 +       }
20736 +
20737 +       if (!err)
20738 +               err = devcgroup_inode_permission(h_inode, mask);
20739 +       if (!err)
20740 +               err = security_inode_permission(h_inode, mask);
20741 +
20742 +#if 0
20743 +       if (!err) {
20744 +               /* todo: do we need to call ima_path_check()? */
20745 +               struct path h_path = {
20746 +                       .dentry =
20747 +                       .mnt    = h_mnt
20748 +               };
20749 +               err = ima_path_check(&h_path,
20750 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
20751 +                                    IMA_COUNT_LEAVE);
20752 +       }
20753 +#endif
20754 +
20755 +out:
20756 +       return err;
20757 +}
20758 +
20759 +static int aufs_permission(struct inode *inode, int mask)
20760 +{
20761 +       int err;
20762 +       aufs_bindex_t bindex, bbot;
20763 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20764 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20765 +       struct inode *h_inode;
20766 +       struct super_block *sb;
20767 +       struct au_branch *br;
20768 +
20769 +       /* todo: support rcu-walk? */
20770 +       if (mask & MAY_NOT_BLOCK)
20771 +               return -ECHILD;
20772 +
20773 +       sb = inode->i_sb;
20774 +       si_read_lock(sb, AuLock_FLUSH);
20775 +       ii_read_lock_child(inode);
20776 +#if 0
20777 +       err = au_iigen_test(inode, au_sigen(sb));
20778 +       if (unlikely(err))
20779 +               goto out;
20780 +#endif
20781 +
20782 +       if (!isdir
20783 +           || write_mask
20784 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20785 +               err = au_busy_or_stale();
20786 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20787 +               if (unlikely(!h_inode
20788 +                            || (h_inode->i_mode & S_IFMT)
20789 +                            != (inode->i_mode & S_IFMT)))
20790 +                       goto out;
20791 +
20792 +               err = 0;
20793 +               bindex = au_ibtop(inode);
20794 +               br = au_sbr(sb, bindex);
20795 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20796 +               if (write_mask
20797 +                   && !err
20798 +                   && !special_file(h_inode->i_mode)) {
20799 +                       /* test whether the upper writable branch exists */
20800 +                       err = -EROFS;
20801 +                       for (; bindex >= 0; bindex--)
20802 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20803 +                                       err = 0;
20804 +                                       break;
20805 +                               }
20806 +               }
20807 +               goto out;
20808 +       }
20809 +
20810 +       /* non-write to dir */
20811 +       err = 0;
20812 +       bbot = au_ibbot(inode);
20813 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20814 +               h_inode = au_h_iptr(inode, bindex);
20815 +               if (h_inode) {
20816 +                       err = au_busy_or_stale();
20817 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20818 +                               break;
20819 +
20820 +                       br = au_sbr(sb, bindex);
20821 +                       err = h_permission(h_inode, mask, &br->br_path,
20822 +                                          br->br_perm);
20823 +               }
20824 +       }
20825 +
20826 +out:
20827 +       ii_read_unlock(inode);
20828 +       si_read_unlock(sb);
20829 +       return err;
20830 +}
20831 +
20832 +/* ---------------------------------------------------------------------- */
20833 +
20834 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20835 +                                 unsigned int flags)
20836 +{
20837 +       struct dentry *ret, *parent;
20838 +       struct inode *inode;
20839 +       struct super_block *sb;
20840 +       int err, npositive;
20841 +
20842 +       IMustLock(dir);
20843 +
20844 +       /* todo: support rcu-walk? */
20845 +       ret = ERR_PTR(-ECHILD);
20846 +       if (flags & LOOKUP_RCU)
20847 +               goto out;
20848 +
20849 +       ret = ERR_PTR(-ENAMETOOLONG);
20850 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20851 +               goto out;
20852 +
20853 +       sb = dir->i_sb;
20854 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20855 +       ret = ERR_PTR(err);
20856 +       if (unlikely(err))
20857 +               goto out;
20858 +
20859 +       err = au_di_init(dentry);
20860 +       ret = ERR_PTR(err);
20861 +       if (unlikely(err))
20862 +               goto out_si;
20863 +
20864 +       inode = NULL;
20865 +       npositive = 0; /* suppress a warning */
20866 +       parent = dentry->d_parent; /* dir inode is locked */
20867 +       di_read_lock_parent(parent, AuLock_IR);
20868 +       err = au_alive_dir(parent);
20869 +       if (!err)
20870 +               err = au_digen_test(parent, au_sigen(sb));
20871 +       if (!err) {
20872 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20873 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20874 +                                          AuLkup_ALLOW_NEG);
20875 +               err = npositive;
20876 +       }
20877 +       di_read_unlock(parent, AuLock_IR);
20878 +       ret = ERR_PTR(err);
20879 +       if (unlikely(err < 0))
20880 +               goto out_unlock;
20881 +
20882 +       if (npositive) {
20883 +               inode = au_new_inode(dentry, /*must_new*/0);
20884 +               if (IS_ERR(inode)) {
20885 +                       ret = (void *)inode;
20886 +                       inode = NULL;
20887 +                       goto out_unlock;
20888 +               }
20889 +       }
20890 +
20891 +       if (inode)
20892 +               atomic_inc(&inode->i_count);
20893 +       ret = d_splice_alias(inode, dentry);
20894 +#if 0
20895 +       if (unlikely(d_need_lookup(dentry))) {
20896 +               spin_lock(&dentry->d_lock);
20897 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20898 +               spin_unlock(&dentry->d_lock);
20899 +       } else
20900 +#endif
20901 +       if (inode) {
20902 +               if (!IS_ERR(ret)) {
20903 +                       iput(inode);
20904 +                       if (ret && ret != dentry)
20905 +                               ii_write_unlock(inode);
20906 +               } else {
20907 +                       ii_write_unlock(inode);
20908 +                       iput(inode);
20909 +                       inode = NULL;
20910 +               }
20911 +       }
20912 +
20913 +out_unlock:
20914 +       di_write_unlock(dentry);
20915 +out_si:
20916 +       si_read_unlock(sb);
20917 +out:
20918 +       return ret;
20919 +}
20920 +
20921 +/* ---------------------------------------------------------------------- */
20922 +
20923 +/*
20924 + * very dirty and complicated aufs ->atomic_open().
20925 + * aufs_atomic_open()
20926 + * + au_aopen_or_create()
20927 + *   + add_simple()
20928 + *     + vfsub_atomic_open()
20929 + *       + branch fs ->atomic_open()
20930 + *        may call the actual 'open' for h_file
20931 + *       + inc br_nfiles only if opened
20932 + * + au_aopen_no_open() or au_aopen_do_open()
20933 + *
20934 + * au_aopen_do_open()
20935 + * + finish_open()
20936 + *   + au_do_aopen()
20937 + *     + au_do_open() the body of all 'open'
20938 + *       + au_do_open_nondir()
20939 + *        set the passed h_file
20940 + *
20941 + * au_aopen_no_open()
20942 + * + finish_no_open()
20943 + */
20944 +
20945 +struct aopen_node {
20946 +       struct hlist_bl_node hblist;
20947 +       struct file *file, *h_file;
20948 +};
20949 +
20950 +static int au_do_aopen(struct inode *inode, struct file *file)
20951 +{
20952 +       struct hlist_bl_head *aopen;
20953 +       struct hlist_bl_node *pos;
20954 +       struct aopen_node *node;
20955 +       struct au_do_open_args args = {
20956 +               .aopen  = 1,
20957 +               .open   = au_do_open_nondir
20958 +       };
20959 +
20960 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20961 +       hlist_bl_lock(aopen);
20962 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20963 +               if (node->file == file) {
20964 +                       args.h_file = node->h_file;
20965 +                       break;
20966 +               }
20967 +       hlist_bl_unlock(aopen);
20968 +       /* AuDebugOn(!args.h_file); */
20969 +
20970 +       return au_do_open(file, &args);
20971 +}
20972 +
20973 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20974 +                           struct aopen_node *aopen_node)
20975 +{
20976 +       int err;
20977 +       struct hlist_bl_head *aopen;
20978 +
20979 +       AuLabel(here);
20980 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20981 +       au_hbl_add(&aopen_node->hblist, aopen);
20982 +       err = finish_open(file, dentry, au_do_aopen);
20983 +       au_hbl_del(&aopen_node->hblist, aopen);
20984 +       /* AuDbgFile(file); */
20985 +       AuDbg("%pd%s%s\n", dentry,
20986 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20987 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20988 +
20989 +       AuTraceErr(err);
20990 +       return err;
20991 +}
20992 +
20993 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20994 +{
20995 +       int err;
20996 +
20997 +       AuLabel(here);
20998 +       dget(dentry);
20999 +       err = finish_no_open(file, dentry);
21000 +
21001 +       AuTraceErr(err);
21002 +       return err;
21003 +}
21004 +
21005 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
21006 +                           struct file *file, unsigned int open_flag,
21007 +                           umode_t create_mode)
21008 +{
21009 +       int err, did_open;
21010 +       unsigned int lkup_flags;
21011 +       aufs_bindex_t bindex;
21012 +       struct super_block *sb;
21013 +       struct dentry *parent, *d;
21014 +       struct vfsub_aopen_args args = {
21015 +               .open_flag      = open_flag,
21016 +               .create_mode    = create_mode
21017 +       };
21018 +       struct aopen_node aopen_node = {
21019 +               .file   = file
21020 +       };
21021 +
21022 +       IMustLock(dir);
21023 +       AuDbg("open_flag 0%o\n", open_flag);
21024 +       AuDbgDentry(dentry);
21025 +
21026 +       err = 0;
21027 +       if (!au_di(dentry)) {
21028 +               lkup_flags = LOOKUP_OPEN;
21029 +               if (open_flag & O_CREAT)
21030 +                       lkup_flags |= LOOKUP_CREATE;
21031 +               d = aufs_lookup(dir, dentry, lkup_flags);
21032 +               if (IS_ERR(d)) {
21033 +                       err = PTR_ERR(d);
21034 +                       AuTraceErr(err);
21035 +                       goto out;
21036 +               } else if (d) {
21037 +                       /*
21038 +                        * obsoleted dentry found.
21039 +                        * another error will be returned later.
21040 +                        */
21041 +                       d_drop(d);
21042 +                       AuDbgDentry(d);
21043 +                       dput(d);
21044 +               }
21045 +               AuDbgDentry(dentry);
21046 +       }
21047 +
21048 +       if (d_is_positive(dentry)
21049 +           || d_unhashed(dentry)
21050 +           || d_unlinked(dentry)
21051 +           || !(open_flag & O_CREAT)) {
21052 +               err = au_aopen_no_open(file, dentry);
21053 +               goto out; /* success */
21054 +       }
21055 +
21056 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
21057 +       if (unlikely(err))
21058 +               goto out;
21059 +
21060 +       sb = dentry->d_sb;
21061 +       parent = dentry->d_parent;      /* dir is locked */
21062 +       di_write_lock_parent(parent);
21063 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
21064 +       if (unlikely(err < 0))
21065 +               goto out_parent;
21066 +
21067 +       AuDbgDentry(dentry);
21068 +       if (d_is_positive(dentry)) {
21069 +               err = au_aopen_no_open(file, dentry);
21070 +               goto out_parent; /* success */
21071 +       }
21072 +
21073 +       args.file = alloc_empty_file(file->f_flags, current_cred());
21074 +       err = PTR_ERR(args.file);
21075 +       if (IS_ERR(args.file))
21076 +               goto out_parent;
21077 +
21078 +       bindex = au_dbtop(dentry);
21079 +       err = au_aopen_or_create(dir, dentry, &args);
21080 +       AuTraceErr(err);
21081 +       AuDbgFile(args.file);
21082 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
21083 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
21084 +       if (!did_open) {
21085 +               fput(args.file);
21086 +               args.file = NULL;
21087 +       }
21088 +       di_write_unlock(parent);
21089 +       di_write_unlock(dentry);
21090 +       if (unlikely(err < 0)) {
21091 +               if (args.file)
21092 +                       fput(args.file);
21093 +               goto out_sb;
21094 +       }
21095 +
21096 +       if (!did_open)
21097 +               err = au_aopen_no_open(file, dentry);
21098 +       else {
21099 +               aopen_node.h_file = args.file;
21100 +               err = au_aopen_do_open(file, dentry, &aopen_node);
21101 +       }
21102 +       if (unlikely(err < 0)) {
21103 +               if (args.file)
21104 +                       fput(args.file);
21105 +               if (did_open)
21106 +                       au_lcnt_dec(&args.br->br_nfiles);
21107 +       }
21108 +       goto out_sb; /* success */
21109 +
21110 +out_parent:
21111 +       di_write_unlock(parent);
21112 +       di_write_unlock(dentry);
21113 +out_sb:
21114 +       si_read_unlock(sb);
21115 +out:
21116 +       AuTraceErr(err);
21117 +       AuDbgFile(file);
21118 +       return err;
21119 +}
21120 +
21121 +
21122 +/* ---------------------------------------------------------------------- */
21123 +
21124 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21125 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21126 +                         aufs_bindex_t btop)
21127 +{
21128 +       int err;
21129 +       struct dentry *h_parent;
21130 +       struct inode *h_dir;
21131 +
21132 +       if (add_entry)
21133 +               IMustLock(d_inode(parent));
21134 +       else
21135 +               di_write_lock_parent(parent);
21136 +
21137 +       err = 0;
21138 +       if (!au_h_dptr(parent, bcpup)) {
21139 +               if (btop > bcpup)
21140 +                       err = au_cpup_dirs(dentry, bcpup);
21141 +               else if (btop < bcpup)
21142 +                       err = au_cpdown_dirs(dentry, bcpup);
21143 +               else
21144 +                       BUG();
21145 +       }
21146 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21147 +               h_parent = au_h_dptr(parent, bcpup);
21148 +               h_dir = d_inode(h_parent);
21149 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21150 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21151 +               /* todo: no unlock here */
21152 +               inode_unlock_shared(h_dir);
21153 +
21154 +               AuDbg("bcpup %d\n", bcpup);
21155 +               if (!err) {
21156 +                       if (d_really_is_negative(dentry))
21157 +                               au_set_h_dptr(dentry, btop, NULL);
21158 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21159 +               }
21160 +       }
21161 +
21162 +       if (!add_entry)
21163 +               di_write_unlock(parent);
21164 +       if (!err)
21165 +               err = bcpup; /* success */
21166 +
21167 +       AuTraceErr(err);
21168 +       return err;
21169 +}
21170 +
21171 +/*
21172 + * decide the branch and the parent dir where we will create a new entry.
21173 + * returns new bindex or an error.
21174 + * copyup the parent dir if needed.
21175 + */
21176 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21177 +             struct au_wr_dir_args *args)
21178 +{
21179 +       int err;
21180 +       unsigned int flags;
21181 +       aufs_bindex_t bcpup, btop, src_btop;
21182 +       const unsigned char add_entry
21183 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21184 +               | au_ftest_wrdir(args->flags, TMPFILE);
21185 +       struct super_block *sb;
21186 +       struct dentry *parent;
21187 +       struct au_sbinfo *sbinfo;
21188 +
21189 +       sb = dentry->d_sb;
21190 +       sbinfo = au_sbi(sb);
21191 +       parent = dget_parent(dentry);
21192 +       btop = au_dbtop(dentry);
21193 +       bcpup = btop;
21194 +       if (args->force_btgt < 0) {
21195 +               if (src_dentry) {
21196 +                       src_btop = au_dbtop(src_dentry);
21197 +                       if (src_btop < btop)
21198 +                               bcpup = src_btop;
21199 +               } else if (add_entry) {
21200 +                       flags = 0;
21201 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21202 +                               au_fset_wbr(flags, DIR);
21203 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21204 +                       bcpup = err;
21205 +               }
21206 +
21207 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21208 +                       if (add_entry)
21209 +                               err = AuWbrCopyup(sbinfo, dentry);
21210 +                       else {
21211 +                               if (!IS_ROOT(dentry)) {
21212 +                                       di_read_lock_parent(parent, !AuLock_IR);
21213 +                                       err = AuWbrCopyup(sbinfo, dentry);
21214 +                                       di_read_unlock(parent, !AuLock_IR);
21215 +                               } else
21216 +                                       err = AuWbrCopyup(sbinfo, dentry);
21217 +                       }
21218 +                       bcpup = err;
21219 +                       if (unlikely(err < 0))
21220 +                               goto out;
21221 +               }
21222 +       } else {
21223 +               bcpup = args->force_btgt;
21224 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21225 +       }
21226 +
21227 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21228 +       err = bcpup;
21229 +       if (bcpup == btop)
21230 +               goto out; /* success */
21231 +
21232 +       /* copyup the new parent into the branch we process */
21233 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21234 +       if (err >= 0) {
21235 +               if (d_really_is_negative(dentry)) {
21236 +                       au_set_h_dptr(dentry, btop, NULL);
21237 +                       au_set_dbtop(dentry, bcpup);
21238 +                       au_set_dbbot(dentry, bcpup);
21239 +               }
21240 +               AuDebugOn(add_entry
21241 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21242 +                         && !au_h_dptr(dentry, bcpup));
21243 +       }
21244 +
21245 +out:
21246 +       dput(parent);
21247 +       return err;
21248 +}
21249 +
21250 +/* ---------------------------------------------------------------------- */
21251 +
21252 +void au_pin_hdir_unlock(struct au_pin *p)
21253 +{
21254 +       if (p->hdir)
21255 +               au_hn_inode_unlock(p->hdir);
21256 +}
21257 +
21258 +int au_pin_hdir_lock(struct au_pin *p)
21259 +{
21260 +       int err;
21261 +
21262 +       err = 0;
21263 +       if (!p->hdir)
21264 +               goto out;
21265 +
21266 +       /* even if an error happens later, keep this lock */
21267 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21268 +
21269 +       err = -EBUSY;
21270 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21271 +               goto out;
21272 +
21273 +       err = 0;
21274 +       if (p->h_dentry)
21275 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21276 +                                 p->h_parent, p->br);
21277 +
21278 +out:
21279 +       return err;
21280 +}
21281 +
21282 +int au_pin_hdir_relock(struct au_pin *p)
21283 +{
21284 +       int err, i;
21285 +       struct inode *h_i;
21286 +       struct dentry *h_d[] = {
21287 +               p->h_dentry,
21288 +               p->h_parent
21289 +       };
21290 +
21291 +       err = au_pin_hdir_lock(p);
21292 +       if (unlikely(err))
21293 +               goto out;
21294 +
21295 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21296 +               if (!h_d[i])
21297 +                       continue;
21298 +               if (d_is_positive(h_d[i])) {
21299 +                       h_i = d_inode(h_d[i]);
21300 +                       err = !h_i->i_nlink;
21301 +               }
21302 +       }
21303 +
21304 +out:
21305 +       return err;
21306 +}
21307 +
21308 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21309 +{
21310 +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
21311 +       p->hdir->hi_inode->i_rwsem.owner = task;
21312 +#endif
21313 +}
21314 +
21315 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21316 +{
21317 +       if (p->hdir) {
21318 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21319 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21320 +               au_pin_hdir_set_owner(p, current);
21321 +       }
21322 +}
21323 +
21324 +void au_pin_hdir_release(struct au_pin *p)
21325 +{
21326 +       if (p->hdir) {
21327 +               au_pin_hdir_set_owner(p, p->task);
21328 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
21329 +       }
21330 +}
21331 +
21332 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21333 +{
21334 +       if (pin && pin->parent)
21335 +               return au_h_dptr(pin->parent, pin->bindex);
21336 +       return NULL;
21337 +}
21338 +
21339 +void au_unpin(struct au_pin *p)
21340 +{
21341 +       if (p->hdir)
21342 +               au_pin_hdir_unlock(p);
21343 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21344 +               vfsub_mnt_drop_write(p->h_mnt);
21345 +       if (!p->hdir)
21346 +               return;
21347 +
21348 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21349 +               di_read_unlock(p->parent, AuLock_IR);
21350 +       iput(p->hdir->hi_inode);
21351 +       dput(p->parent);
21352 +       p->parent = NULL;
21353 +       p->hdir = NULL;
21354 +       p->h_mnt = NULL;
21355 +       /* do not clear p->task */
21356 +}
21357 +
21358 +int au_do_pin(struct au_pin *p)
21359 +{
21360 +       int err;
21361 +       struct super_block *sb;
21362 +       struct inode *h_dir;
21363 +
21364 +       err = 0;
21365 +       sb = p->dentry->d_sb;
21366 +       p->br = au_sbr(sb, p->bindex);
21367 +       if (IS_ROOT(p->dentry)) {
21368 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21369 +                       p->h_mnt = au_br_mnt(p->br);
21370 +                       err = vfsub_mnt_want_write(p->h_mnt);
21371 +                       if (unlikely(err)) {
21372 +                               au_fclr_pin(p->flags, MNT_WRITE);
21373 +                               goto out_err;
21374 +                       }
21375 +               }
21376 +               goto out;
21377 +       }
21378 +
21379 +       p->h_dentry = NULL;
21380 +       if (p->bindex <= au_dbbot(p->dentry))
21381 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21382 +
21383 +       p->parent = dget_parent(p->dentry);
21384 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21385 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21386 +
21387 +       h_dir = NULL;
21388 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21389 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21390 +       if (p->hdir)
21391 +               h_dir = p->hdir->hi_inode;
21392 +
21393 +       /*
21394 +        * udba case, or
21395 +        * if DI_LOCKED is not set, then p->parent may be different
21396 +        * and h_parent can be NULL.
21397 +        */
21398 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21399 +               err = -EBUSY;
21400 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21401 +                       di_read_unlock(p->parent, AuLock_IR);
21402 +               dput(p->parent);
21403 +               p->parent = NULL;
21404 +               goto out_err;
21405 +       }
21406 +
21407 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21408 +               p->h_mnt = au_br_mnt(p->br);
21409 +               err = vfsub_mnt_want_write(p->h_mnt);
21410 +               if (unlikely(err)) {
21411 +                       au_fclr_pin(p->flags, MNT_WRITE);
21412 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21413 +                               di_read_unlock(p->parent, AuLock_IR);
21414 +                       dput(p->parent);
21415 +                       p->parent = NULL;
21416 +                       goto out_err;
21417 +               }
21418 +       }
21419 +
21420 +       au_igrab(h_dir);
21421 +       err = au_pin_hdir_lock(p);
21422 +       if (!err)
21423 +               goto out; /* success */
21424 +
21425 +       au_unpin(p);
21426 +
21427 +out_err:
21428 +       pr_err("err %d\n", err);
21429 +       err = au_busy_or_stale();
21430 +out:
21431 +       return err;
21432 +}
21433 +
21434 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21435 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21436 +                unsigned int udba, unsigned char flags)
21437 +{
21438 +       p->dentry = dentry;
21439 +       p->udba = udba;
21440 +       p->lsc_di = lsc_di;
21441 +       p->lsc_hi = lsc_hi;
21442 +       p->flags = flags;
21443 +       p->bindex = bindex;
21444 +
21445 +       p->parent = NULL;
21446 +       p->hdir = NULL;
21447 +       p->h_mnt = NULL;
21448 +
21449 +       p->h_dentry = NULL;
21450 +       p->h_parent = NULL;
21451 +       p->br = NULL;
21452 +       p->task = current;
21453 +}
21454 +
21455 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21456 +          unsigned int udba, unsigned char flags)
21457 +{
21458 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21459 +                   udba, flags);
21460 +       return au_do_pin(pin);
21461 +}
21462 +
21463 +/* ---------------------------------------------------------------------- */
21464 +
21465 +/*
21466 + * ->setattr() and ->getattr() are called in various cases.
21467 + * chmod, stat: dentry is revalidated.
21468 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21469 + *               unhashed.
21470 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21471 + */
21472 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21473 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21474 +{
21475 +       int err;
21476 +       struct dentry *parent;
21477 +
21478 +       err = 0;
21479 +       if (au_digen_test(dentry, sigen)) {
21480 +               parent = dget_parent(dentry);
21481 +               di_read_lock_parent(parent, AuLock_IR);
21482 +               err = au_refresh_dentry(dentry, parent);
21483 +               di_read_unlock(parent, AuLock_IR);
21484 +               dput(parent);
21485 +       }
21486 +
21487 +       AuTraceErr(err);
21488 +       return err;
21489 +}
21490 +
21491 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21492 +                    struct au_icpup_args *a)
21493 +{
21494 +       int err;
21495 +       loff_t sz;
21496 +       aufs_bindex_t btop, ibtop;
21497 +       struct dentry *hi_wh, *parent;
21498 +       struct inode *inode;
21499 +       struct au_wr_dir_args wr_dir_args = {
21500 +               .force_btgt     = -1,
21501 +               .flags          = 0
21502 +       };
21503 +
21504 +       if (d_is_dir(dentry))
21505 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21506 +       /* plink or hi_wh() case */
21507 +       btop = au_dbtop(dentry);
21508 +       inode = d_inode(dentry);
21509 +       ibtop = au_ibtop(inode);
21510 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21511 +               wr_dir_args.force_btgt = ibtop;
21512 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21513 +       if (unlikely(err < 0))
21514 +               goto out;
21515 +       a->btgt = err;
21516 +       if (err != btop)
21517 +               au_fset_icpup(a->flags, DID_CPUP);
21518 +
21519 +       err = 0;
21520 +       a->pin_flags = AuPin_MNT_WRITE;
21521 +       parent = NULL;
21522 +       if (!IS_ROOT(dentry)) {
21523 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21524 +               parent = dget_parent(dentry);
21525 +               di_write_lock_parent(parent);
21526 +       }
21527 +
21528 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21529 +       if (unlikely(err))
21530 +               goto out_parent;
21531 +
21532 +       sz = -1;
21533 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21534 +       a->h_inode = d_inode(a->h_path.dentry);
21535 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21536 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21537 +               if (ia->ia_size < i_size_read(a->h_inode))
21538 +                       sz = ia->ia_size;
21539 +               inode_unlock_shared(a->h_inode);
21540 +       }
21541 +
21542 +       hi_wh = NULL;
21543 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21544 +               hi_wh = au_hi_wh(inode, a->btgt);
21545 +               if (!hi_wh) {
21546 +                       struct au_cp_generic cpg = {
21547 +                               .dentry = dentry,
21548 +                               .bdst   = a->btgt,
21549 +                               .bsrc   = -1,
21550 +                               .len    = sz,
21551 +                               .pin    = &a->pin
21552 +                       };
21553 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21554 +                       if (unlikely(err))
21555 +                               goto out_unlock;
21556 +                       hi_wh = au_hi_wh(inode, a->btgt);
21557 +                       /* todo: revalidate hi_wh? */
21558 +               }
21559 +       }
21560 +
21561 +       if (parent) {
21562 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21563 +               di_downgrade_lock(parent, AuLock_IR);
21564 +               dput(parent);
21565 +               parent = NULL;
21566 +       }
21567 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21568 +               goto out; /* success */
21569 +
21570 +       if (!d_unhashed(dentry)) {
21571 +               struct au_cp_generic cpg = {
21572 +                       .dentry = dentry,
21573 +                       .bdst   = a->btgt,
21574 +                       .bsrc   = btop,
21575 +                       .len    = sz,
21576 +                       .pin    = &a->pin,
21577 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21578 +               };
21579 +               err = au_sio_cpup_simple(&cpg);
21580 +               if (!err)
21581 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21582 +       } else if (!hi_wh)
21583 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21584 +       else
21585 +               a->h_path.dentry = hi_wh; /* do not dget here */
21586 +
21587 +out_unlock:
21588 +       a->h_inode = d_inode(a->h_path.dentry);
21589 +       if (!err)
21590 +               goto out; /* success */
21591 +       au_unpin(&a->pin);
21592 +out_parent:
21593 +       if (parent) {
21594 +               di_write_unlock(parent);
21595 +               dput(parent);
21596 +       }
21597 +out:
21598 +       if (!err)
21599 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21600 +       return err;
21601 +}
21602 +
21603 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21604 +{
21605 +       int err;
21606 +       struct inode *inode, *delegated;
21607 +       struct super_block *sb;
21608 +       struct file *file;
21609 +       struct au_icpup_args *a;
21610 +
21611 +       inode = d_inode(dentry);
21612 +       IMustLock(inode);
21613 +
21614 +       err = setattr_prepare(dentry, ia);
21615 +       if (unlikely(err))
21616 +               goto out;
21617 +
21618 +       err = -ENOMEM;
21619 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21620 +       if (unlikely(!a))
21621 +               goto out;
21622 +
21623 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21624 +               ia->ia_valid &= ~ATTR_MODE;
21625 +
21626 +       file = NULL;
21627 +       sb = dentry->d_sb;
21628 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21629 +       if (unlikely(err))
21630 +               goto out_kfree;
21631 +
21632 +       if (ia->ia_valid & ATTR_FILE) {
21633 +               /* currently ftruncate(2) only */
21634 +               AuDebugOn(!d_is_reg(dentry));
21635 +               file = ia->ia_file;
21636 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21637 +                                           /*fi_lsc*/0);
21638 +               if (unlikely(err))
21639 +                       goto out_si;
21640 +               ia->ia_file = au_hf_top(file);
21641 +               a->udba = AuOpt_UDBA_NONE;
21642 +       } else {
21643 +               /* fchmod() doesn't pass ia_file */
21644 +               a->udba = au_opt_udba(sb);
21645 +               di_write_lock_child(dentry);
21646 +               /* no d_unlinked(), to set UDBA_NONE for root */
21647 +               if (d_unhashed(dentry))
21648 +                       a->udba = AuOpt_UDBA_NONE;
21649 +               if (a->udba != AuOpt_UDBA_NONE) {
21650 +                       AuDebugOn(IS_ROOT(dentry));
21651 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21652 +                       if (unlikely(err))
21653 +                               goto out_dentry;
21654 +               }
21655 +       }
21656 +
21657 +       err = au_pin_and_icpup(dentry, ia, a);
21658 +       if (unlikely(err < 0))
21659 +               goto out_dentry;
21660 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21661 +               ia->ia_file = NULL;
21662 +               ia->ia_valid &= ~ATTR_FILE;
21663 +       }
21664 +
21665 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21666 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21667 +           == (ATTR_MODE | ATTR_CTIME)) {
21668 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21669 +               if (unlikely(err))
21670 +                       goto out_unlock;
21671 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21672 +                  && (ia->ia_valid & ATTR_CTIME)) {
21673 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21674 +               if (unlikely(err))
21675 +                       goto out_unlock;
21676 +       }
21677 +
21678 +       if (ia->ia_valid & ATTR_SIZE) {
21679 +               struct file *f;
21680 +
21681 +               if (ia->ia_size < i_size_read(inode))
21682 +                       /* unmap only */
21683 +                       truncate_setsize(inode, ia->ia_size);
21684 +
21685 +               f = NULL;
21686 +               if (ia->ia_valid & ATTR_FILE)
21687 +                       f = ia->ia_file;
21688 +               inode_unlock(a->h_inode);
21689 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21690 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21691 +       } else {
21692 +               delegated = NULL;
21693 +               while (1) {
21694 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21695 +                       if (delegated) {
21696 +                               err = break_deleg_wait(&delegated);
21697 +                               if (!err)
21698 +                                       continue;
21699 +                       }
21700 +                       break;
21701 +               }
21702 +       }
21703 +       /*
21704 +        * regardless aufs 'acl' option setting.
21705 +        * why don't all acl-aware fs call this func from their ->setattr()?
21706 +        */
21707 +       if (!err && (ia->ia_valid & ATTR_MODE))
21708 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21709 +       if (!err)
21710 +               au_cpup_attr_changeable(inode);
21711 +
21712 +out_unlock:
21713 +       inode_unlock(a->h_inode);
21714 +       au_unpin(&a->pin);
21715 +       if (unlikely(err))
21716 +               au_update_dbtop(dentry);
21717 +out_dentry:
21718 +       di_write_unlock(dentry);
21719 +       if (file) {
21720 +               fi_write_unlock(file);
21721 +               ia->ia_file = file;
21722 +               ia->ia_valid |= ATTR_FILE;
21723 +       }
21724 +out_si:
21725 +       si_read_unlock(sb);
21726 +out_kfree:
21727 +       au_kfree_rcu(a);
21728 +out:
21729 +       AuTraceErr(err);
21730 +       return err;
21731 +}
21732 +
21733 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21734 +static int au_h_path_to_set_attr(struct dentry *dentry,
21735 +                                struct au_icpup_args *a, struct path *h_path)
21736 +{
21737 +       int err;
21738 +       struct super_block *sb;
21739 +
21740 +       sb = dentry->d_sb;
21741 +       a->udba = au_opt_udba(sb);
21742 +       /* no d_unlinked(), to set UDBA_NONE for root */
21743 +       if (d_unhashed(dentry))
21744 +               a->udba = AuOpt_UDBA_NONE;
21745 +       if (a->udba != AuOpt_UDBA_NONE) {
21746 +               AuDebugOn(IS_ROOT(dentry));
21747 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21748 +               if (unlikely(err))
21749 +                       goto out;
21750 +       }
21751 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21752 +       if (unlikely(err < 0))
21753 +               goto out;
21754 +
21755 +       h_path->dentry = a->h_path.dentry;
21756 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21757 +
21758 +out:
21759 +       return err;
21760 +}
21761 +
21762 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21763 +                 struct au_sxattr *arg)
21764 +{
21765 +       int err;
21766 +       struct path h_path;
21767 +       struct super_block *sb;
21768 +       struct au_icpup_args *a;
21769 +       struct inode *h_inode;
21770 +
21771 +       IMustLock(inode);
21772 +
21773 +       err = -ENOMEM;
21774 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21775 +       if (unlikely(!a))
21776 +               goto out;
21777 +
21778 +       sb = dentry->d_sb;
21779 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21780 +       if (unlikely(err))
21781 +               goto out_kfree;
21782 +
21783 +       h_path.dentry = NULL;   /* silence gcc */
21784 +       di_write_lock_child(dentry);
21785 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21786 +       if (unlikely(err))
21787 +               goto out_di;
21788 +
21789 +       inode_unlock(a->h_inode);
21790 +       switch (arg->type) {
21791 +       case AU_XATTR_SET:
21792 +               AuDebugOn(d_is_negative(h_path.dentry));
21793 +               err = vfsub_setxattr(h_path.dentry,
21794 +                                    arg->u.set.name, arg->u.set.value,
21795 +                                    arg->u.set.size, arg->u.set.flags);
21796 +               break;
21797 +       case AU_ACL_SET:
21798 +               err = -EOPNOTSUPP;
21799 +               h_inode = d_inode(h_path.dentry);
21800 +               if (h_inode->i_op->set_acl)
21801 +                       /* this will call posix_acl_update_mode */
21802 +                       err = h_inode->i_op->set_acl(h_inode,
21803 +                                                    arg->u.acl_set.acl,
21804 +                                                    arg->u.acl_set.type);
21805 +               break;
21806 +       }
21807 +       if (!err)
21808 +               au_cpup_attr_timesizes(inode);
21809 +
21810 +       au_unpin(&a->pin);
21811 +       if (unlikely(err))
21812 +               au_update_dbtop(dentry);
21813 +
21814 +out_di:
21815 +       di_write_unlock(dentry);
21816 +       si_read_unlock(sb);
21817 +out_kfree:
21818 +       au_kfree_rcu(a);
21819 +out:
21820 +       AuTraceErr(err);
21821 +       return err;
21822 +}
21823 +#endif
21824 +
21825 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21826 +                            unsigned int nlink)
21827 +{
21828 +       unsigned int n;
21829 +
21830 +       inode->i_mode = st->mode;
21831 +       /* don't i_[ug]id_write() here */
21832 +       inode->i_uid = st->uid;
21833 +       inode->i_gid = st->gid;
21834 +       inode->i_atime = st->atime;
21835 +       inode->i_mtime = st->mtime;
21836 +       inode->i_ctime = st->ctime;
21837 +
21838 +       au_cpup_attr_nlink(inode, /*force*/0);
21839 +       if (S_ISDIR(inode->i_mode)) {
21840 +               n = inode->i_nlink;
21841 +               n -= nlink;
21842 +               n += st->nlink;
21843 +               smp_mb(); /* for i_nlink */
21844 +               /* 0 can happen */
21845 +               set_nlink(inode, n);
21846 +       }
21847 +
21848 +       spin_lock(&inode->i_lock);
21849 +       inode->i_blocks = st->blocks;
21850 +       i_size_write(inode, st->size);
21851 +       spin_unlock(&inode->i_lock);
21852 +}
21853 +
21854 +/*
21855 + * common routine for aufs_getattr() and au_getxattr().
21856 + * returns zero or negative (an error).
21857 + * @dentry will be read-locked in success.
21858 + */
21859 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21860 +                     int locked)
21861 +{
21862 +       int err;
21863 +       unsigned int mnt_flags, sigen;
21864 +       unsigned char udba_none;
21865 +       aufs_bindex_t bindex;
21866 +       struct super_block *sb, *h_sb;
21867 +       struct inode *inode;
21868 +
21869 +       h_path->mnt = NULL;
21870 +       h_path->dentry = NULL;
21871 +
21872 +       err = 0;
21873 +       sb = dentry->d_sb;
21874 +       mnt_flags = au_mntflags(sb);
21875 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21876 +
21877 +       if (unlikely(locked))
21878 +               goto body; /* skip locking dinfo */
21879 +
21880 +       /* support fstat(2) */
21881 +       if (!d_unlinked(dentry) && !udba_none) {
21882 +               sigen = au_sigen(sb);
21883 +               err = au_digen_test(dentry, sigen);
21884 +               if (!err) {
21885 +                       di_read_lock_child(dentry, AuLock_IR);
21886 +                       err = au_dbrange_test(dentry);
21887 +                       if (unlikely(err)) {
21888 +                               di_read_unlock(dentry, AuLock_IR);
21889 +                               goto out;
21890 +                       }
21891 +               } else {
21892 +                       AuDebugOn(IS_ROOT(dentry));
21893 +                       di_write_lock_child(dentry);
21894 +                       err = au_dbrange_test(dentry);
21895 +                       if (!err)
21896 +                               err = au_reval_for_attr(dentry, sigen);
21897 +                       if (!err)
21898 +                               di_downgrade_lock(dentry, AuLock_IR);
21899 +                       else {
21900 +                               di_write_unlock(dentry);
21901 +                               goto out;
21902 +                       }
21903 +               }
21904 +       } else
21905 +               di_read_lock_child(dentry, AuLock_IR);
21906 +
21907 +body:
21908 +       inode = d_inode(dentry);
21909 +       bindex = au_ibtop(inode);
21910 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21911 +       h_sb = h_path->mnt->mnt_sb;
21912 +       if (!force
21913 +           && !au_test_fs_bad_iattr(h_sb)
21914 +           && udba_none)
21915 +               goto out; /* success */
21916 +
21917 +       if (au_dbtop(dentry) == bindex)
21918 +               h_path->dentry = au_h_dptr(dentry, bindex);
21919 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21920 +               h_path->dentry = au_plink_lkup(inode, bindex);
21921 +               if (IS_ERR(h_path->dentry))
21922 +                       /* pretending success */
21923 +                       h_path->dentry = NULL;
21924 +               else
21925 +                       dput(h_path->dentry);
21926 +       }
21927 +
21928 +out:
21929 +       return err;
21930 +}
21931 +
21932 +static int aufs_getattr(const struct path *path, struct kstat *st,
21933 +                       u32 request, unsigned int query)
21934 +{
21935 +       int err;
21936 +       unsigned char positive;
21937 +       struct path h_path;
21938 +       struct dentry *dentry;
21939 +       struct inode *inode;
21940 +       struct super_block *sb;
21941 +
21942 +       dentry = path->dentry;
21943 +       inode = d_inode(dentry);
21944 +       sb = dentry->d_sb;
21945 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21946 +       if (unlikely(err))
21947 +               goto out;
21948 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
21949 +       if (unlikely(err))
21950 +               goto out_si;
21951 +       if (unlikely(!h_path.dentry))
21952 +               /* illegally overlapped or something */
21953 +               goto out_fill; /* pretending success */
21954 +
21955 +       positive = d_is_positive(h_path.dentry);
21956 +       if (positive)
21957 +               /* no vfsub version */
21958 +               err = vfs_getattr(&h_path, st, request, query);
21959 +       if (!err) {
21960 +               if (positive)
21961 +                       au_refresh_iattr(inode, st,
21962 +                                        d_inode(h_path.dentry)->i_nlink);
21963 +               goto out_fill; /* success */
21964 +       }
21965 +       AuTraceErr(err);
21966 +       goto out_di;
21967 +
21968 +out_fill:
21969 +       generic_fillattr(inode, st);
21970 +out_di:
21971 +       di_read_unlock(dentry, AuLock_IR);
21972 +out_si:
21973 +       si_read_unlock(sb);
21974 +out:
21975 +       AuTraceErr(err);
21976 +       return err;
21977 +}
21978 +
21979 +/* ---------------------------------------------------------------------- */
21980 +
21981 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21982 +                                struct delayed_call *done)
21983 +{
21984 +       const char *ret;
21985 +       struct dentry *h_dentry;
21986 +       struct inode *h_inode;
21987 +       int err;
21988 +       aufs_bindex_t bindex;
21989 +
21990 +       ret = NULL; /* suppress a warning */
21991 +       err = -ECHILD;
21992 +       if (!dentry)
21993 +               goto out;
21994 +
21995 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21996 +       if (unlikely(err))
21997 +               goto out;
21998 +
21999 +       err = au_d_hashed_positive(dentry);
22000 +       if (unlikely(err))
22001 +               goto out_unlock;
22002 +
22003 +       err = -EINVAL;
22004 +       inode = d_inode(dentry);
22005 +       bindex = au_ibtop(inode);
22006 +       h_inode = au_h_iptr(inode, bindex);
22007 +       if (unlikely(!h_inode->i_op->get_link))
22008 +               goto out_unlock;
22009 +
22010 +       err = -EBUSY;
22011 +       h_dentry = NULL;
22012 +       if (au_dbtop(dentry) <= bindex) {
22013 +               h_dentry = au_h_dptr(dentry, bindex);
22014 +               if (h_dentry)
22015 +                       dget(h_dentry);
22016 +       }
22017 +       if (!h_dentry) {
22018 +               h_dentry = d_find_any_alias(h_inode);
22019 +               if (IS_ERR(h_dentry)) {
22020 +                       err = PTR_ERR(h_dentry);
22021 +                       goto out_unlock;
22022 +               }
22023 +       }
22024 +       if (unlikely(!h_dentry))
22025 +               goto out_unlock;
22026 +
22027 +       err = 0;
22028 +       AuDbg("%ps\n", h_inode->i_op->get_link);
22029 +       AuDbgDentry(h_dentry);
22030 +       ret = vfs_get_link(h_dentry, done);
22031 +       dput(h_dentry);
22032 +       if (IS_ERR(ret))
22033 +               err = PTR_ERR(ret);
22034 +
22035 +out_unlock:
22036 +       aufs_read_unlock(dentry, AuLock_IR);
22037 +out:
22038 +       if (unlikely(err))
22039 +               ret = ERR_PTR(err);
22040 +       AuTraceErrPtr(ret);
22041 +       return ret;
22042 +}
22043 +
22044 +/* ---------------------------------------------------------------------- */
22045 +
22046 +static int au_is_special(struct inode *inode)
22047 +{
22048 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
22049 +}
22050 +
22051 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
22052 +                           int flags)
22053 +{
22054 +       int err;
22055 +       aufs_bindex_t bindex;
22056 +       struct super_block *sb;
22057 +       struct inode *h_inode;
22058 +       struct vfsmount *h_mnt;
22059 +
22060 +       sb = inode->i_sb;
22061 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
22062 +                 "unexpected s_flags 0x%lx", sb->s_flags);
22063 +
22064 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
22065 +       lockdep_off();
22066 +       si_read_lock(sb, AuLock_FLUSH);
22067 +       ii_write_lock_child(inode);
22068 +
22069 +       err = 0;
22070 +       bindex = au_ibtop(inode);
22071 +       h_inode = au_h_iptr(inode, bindex);
22072 +       if (!au_test_ro(sb, bindex, inode)) {
22073 +               h_mnt = au_sbr_mnt(sb, bindex);
22074 +               err = vfsub_mnt_want_write(h_mnt);
22075 +               if (!err) {
22076 +                       err = vfsub_update_time(h_inode, ts, flags);
22077 +                       vfsub_mnt_drop_write(h_mnt);
22078 +               }
22079 +       } else if (au_is_special(h_inode)) {
22080 +               /*
22081 +                * Never copy-up here.
22082 +                * These special files may already be opened and used for
22083 +                * communicating. If we copied it up, then the communication
22084 +                * would be corrupted.
22085 +                */
22086 +               AuWarn1("timestamps for i%lu are ignored "
22087 +                       "since it is on readonly branch (hi%lu).\n",
22088 +                       inode->i_ino, h_inode->i_ino);
22089 +       } else if (flags & ~S_ATIME) {
22090 +               err = -EIO;
22091 +               AuIOErr1("unexpected flags 0x%x\n", flags);
22092 +               AuDebugOn(1);
22093 +       }
22094 +
22095 +       if (!err)
22096 +               au_cpup_attr_timesizes(inode);
22097 +       ii_write_unlock(inode);
22098 +       si_read_unlock(sb);
22099 +       lockdep_on();
22100 +
22101 +       if (!err && (flags & S_VERSION))
22102 +               inode_inc_iversion(inode);
22103 +
22104 +       return err;
22105 +}
22106 +
22107 +/* ---------------------------------------------------------------------- */
22108 +
22109 +/* no getattr version will be set by module.c:aufs_init() */
22110 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22111 +       aufs_iop[] = {
22112 +       [AuIop_SYMLINK] = {
22113 +               .permission     = aufs_permission,
22114 +#ifdef CONFIG_FS_POSIX_ACL
22115 +               .get_acl        = aufs_get_acl,
22116 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22117 +#endif
22118 +
22119 +               .setattr        = aufs_setattr,
22120 +               .getattr        = aufs_getattr,
22121 +
22122 +#ifdef CONFIG_AUFS_XATTR
22123 +               .listxattr      = aufs_listxattr,
22124 +#endif
22125 +
22126 +               .get_link       = aufs_get_link,
22127 +
22128 +               /* .update_time = aufs_update_time */
22129 +       },
22130 +       [AuIop_DIR] = {
22131 +               .create         = aufs_create,
22132 +               .lookup         = aufs_lookup,
22133 +               .link           = aufs_link,
22134 +               .unlink         = aufs_unlink,
22135 +               .symlink        = aufs_symlink,
22136 +               .mkdir          = aufs_mkdir,
22137 +               .rmdir          = aufs_rmdir,
22138 +               .mknod          = aufs_mknod,
22139 +               .rename         = aufs_rename,
22140 +
22141 +               .permission     = aufs_permission,
22142 +#ifdef CONFIG_FS_POSIX_ACL
22143 +               .get_acl        = aufs_get_acl,
22144 +               .set_acl        = aufs_set_acl,
22145 +#endif
22146 +
22147 +               .setattr        = aufs_setattr,
22148 +               .getattr        = aufs_getattr,
22149 +
22150 +#ifdef CONFIG_AUFS_XATTR
22151 +               .listxattr      = aufs_listxattr,
22152 +#endif
22153 +
22154 +               .update_time    = aufs_update_time,
22155 +               .atomic_open    = aufs_atomic_open,
22156 +               .tmpfile        = aufs_tmpfile
22157 +       },
22158 +       [AuIop_OTHER] = {
22159 +               .permission     = aufs_permission,
22160 +#ifdef CONFIG_FS_POSIX_ACL
22161 +               .get_acl        = aufs_get_acl,
22162 +               .set_acl        = aufs_set_acl,
22163 +#endif
22164 +
22165 +               .setattr        = aufs_setattr,
22166 +               .getattr        = aufs_getattr,
22167 +
22168 +#ifdef CONFIG_AUFS_XATTR
22169 +               .listxattr      = aufs_listxattr,
22170 +#endif
22171 +
22172 +               .update_time    = aufs_update_time
22173 +       }
22174 +};
22175 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22176 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22177 +++ linux/fs/aufs/i_op_del.c    2018-12-27 13:19:17.711749485 +0100
22178 @@ -0,0 +1,512 @@
22179 +// SPDX-License-Identifier: GPL-2.0
22180 +/*
22181 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22182 + *
22183 + * This program, aufs is free software; you can redistribute it and/or modify
22184 + * it under the terms of the GNU General Public License as published by
22185 + * the Free Software Foundation; either version 2 of the License, or
22186 + * (at your option) any later version.
22187 + *
22188 + * This program is distributed in the hope that it will be useful,
22189 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22190 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22191 + * GNU General Public License for more details.
22192 + *
22193 + * You should have received a copy of the GNU General Public License
22194 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22195 + */
22196 +
22197 +/*
22198 + * inode operations (del entry)
22199 + */
22200 +
22201 +#include "aufs.h"
22202 +
22203 +/*
22204 + * decide if a new whiteout for @dentry is necessary or not.
22205 + * when it is necessary, prepare the parent dir for the upper branch whose
22206 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22207 + * be done by caller.
22208 + * return value:
22209 + * 0: wh is unnecessary
22210 + * plus: wh is necessary
22211 + * minus: error
22212 + */
22213 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22214 +{
22215 +       int need_wh, err;
22216 +       aufs_bindex_t btop;
22217 +       struct super_block *sb;
22218 +
22219 +       sb = dentry->d_sb;
22220 +       btop = au_dbtop(dentry);
22221 +       if (*bcpup < 0) {
22222 +               *bcpup = btop;
22223 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22224 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22225 +                       *bcpup = err;
22226 +                       if (unlikely(err < 0))
22227 +                               goto out;
22228 +               }
22229 +       } else
22230 +               AuDebugOn(btop < *bcpup
22231 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22232 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22233 +
22234 +       if (*bcpup != btop) {
22235 +               err = au_cpup_dirs(dentry, *bcpup);
22236 +               if (unlikely(err))
22237 +                       goto out;
22238 +               need_wh = 1;
22239 +       } else {
22240 +               struct au_dinfo *dinfo, *tmp;
22241 +
22242 +               need_wh = -ENOMEM;
22243 +               dinfo = au_di(dentry);
22244 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22245 +               if (tmp) {
22246 +                       au_di_cp(tmp, dinfo);
22247 +                       au_di_swap(tmp, dinfo);
22248 +                       /* returns the number of positive dentries */
22249 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22250 +                                                /* AuLkup_IGNORE_PERM */ 0);
22251 +                       au_di_swap(tmp, dinfo);
22252 +                       au_rw_write_unlock(&tmp->di_rwsem);
22253 +                       au_di_free(tmp);
22254 +               }
22255 +       }
22256 +       AuDbg("need_wh %d\n", need_wh);
22257 +       err = need_wh;
22258 +
22259 +out:
22260 +       return err;
22261 +}
22262 +
22263 +/*
22264 + * simple tests for the del-entry operations.
22265 + * following the checks in vfs, plus the parent-child relationship.
22266 + */
22267 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22268 +              struct dentry *h_parent, int isdir)
22269 +{
22270 +       int err;
22271 +       umode_t h_mode;
22272 +       struct dentry *h_dentry, *h_latest;
22273 +       struct inode *h_inode;
22274 +
22275 +       h_dentry = au_h_dptr(dentry, bindex);
22276 +       if (d_really_is_positive(dentry)) {
22277 +               err = -ENOENT;
22278 +               if (unlikely(d_is_negative(h_dentry)))
22279 +                       goto out;
22280 +               h_inode = d_inode(h_dentry);
22281 +               if (unlikely(!h_inode->i_nlink))
22282 +                       goto out;
22283 +
22284 +               h_mode = h_inode->i_mode;
22285 +               if (!isdir) {
22286 +                       err = -EISDIR;
22287 +                       if (unlikely(S_ISDIR(h_mode)))
22288 +                               goto out;
22289 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22290 +                       err = -ENOTDIR;
22291 +                       goto out;
22292 +               }
22293 +       } else {
22294 +               /* rename(2) case */
22295 +               err = -EIO;
22296 +               if (unlikely(d_is_positive(h_dentry)))
22297 +                       goto out;
22298 +       }
22299 +
22300 +       err = -ENOENT;
22301 +       /* expected parent dir is locked */
22302 +       if (unlikely(h_parent != h_dentry->d_parent))
22303 +               goto out;
22304 +       err = 0;
22305 +
22306 +       /*
22307 +        * rmdir a dir may break the consistency on some filesystem.
22308 +        * let's try heavy test.
22309 +        */
22310 +       err = -EACCES;
22311 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22312 +                    && au_test_h_perm(d_inode(h_parent),
22313 +                                      MAY_EXEC | MAY_WRITE)))
22314 +               goto out;
22315 +
22316 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22317 +       err = -EIO;
22318 +       if (IS_ERR(h_latest))
22319 +               goto out;
22320 +       if (h_latest == h_dentry)
22321 +               err = 0;
22322 +       dput(h_latest);
22323 +
22324 +out:
22325 +       return err;
22326 +}
22327 +
22328 +/*
22329 + * decide the branch where we operate for @dentry. the branch index will be set
22330 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22331 + * dir for reverting.
22332 + * when a new whiteout is necessary, create it.
22333 + */
22334 +static struct dentry*
22335 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22336 +                   struct au_dtime *dt, struct au_pin *pin)
22337 +{
22338 +       struct dentry *wh_dentry;
22339 +       struct super_block *sb;
22340 +       struct path h_path;
22341 +       int err, need_wh;
22342 +       unsigned int udba;
22343 +       aufs_bindex_t bcpup;
22344 +
22345 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22346 +       wh_dentry = ERR_PTR(need_wh);
22347 +       if (unlikely(need_wh < 0))
22348 +               goto out;
22349 +
22350 +       sb = dentry->d_sb;
22351 +       udba = au_opt_udba(sb);
22352 +       bcpup = *rbcpup;
22353 +       err = au_pin(pin, dentry, bcpup, udba,
22354 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22355 +       wh_dentry = ERR_PTR(err);
22356 +       if (unlikely(err))
22357 +               goto out;
22358 +
22359 +       h_path.dentry = au_pinned_h_parent(pin);
22360 +       if (udba != AuOpt_UDBA_NONE
22361 +           && au_dbtop(dentry) == bcpup) {
22362 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22363 +               wh_dentry = ERR_PTR(err);
22364 +               if (unlikely(err))
22365 +                       goto out_unpin;
22366 +       }
22367 +
22368 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22369 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22370 +       wh_dentry = NULL;
22371 +       if (!need_wh)
22372 +               goto out; /* success, no need to create whiteout */
22373 +
22374 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22375 +       if (IS_ERR(wh_dentry))
22376 +               goto out_unpin;
22377 +
22378 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22379 +       goto out; /* success */
22380 +
22381 +out_unpin:
22382 +       au_unpin(pin);
22383 +out:
22384 +       return wh_dentry;
22385 +}
22386 +
22387 +/*
22388 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22389 + * in order to be revertible and save time for removing many child whiteouts
22390 + * under the dir.
22391 + * returns 1 when there are too many child whiteout and caller should remove
22392 + * them asynchronously. returns 0 when the number of children is enough small to
22393 + * remove now or the branch fs is a remote fs.
22394 + * otherwise return an error.
22395 + */
22396 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22397 +                          struct au_nhash *whlist, struct inode *dir)
22398 +{
22399 +       int rmdir_later, err, dirwh;
22400 +       struct dentry *h_dentry;
22401 +       struct super_block *sb;
22402 +       struct inode *inode;
22403 +
22404 +       sb = dentry->d_sb;
22405 +       SiMustAnyLock(sb);
22406 +       h_dentry = au_h_dptr(dentry, bindex);
22407 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22408 +       if (unlikely(err))
22409 +               goto out;
22410 +
22411 +       /* stop monitoring */
22412 +       inode = d_inode(dentry);
22413 +       au_hn_free(au_hi(inode, bindex));
22414 +
22415 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22416 +               dirwh = au_sbi(sb)->si_dirwh;
22417 +               rmdir_later = (dirwh <= 1);
22418 +               if (!rmdir_later)
22419 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22420 +                                                             dirwh);
22421 +               if (rmdir_later)
22422 +                       return rmdir_later;
22423 +       }
22424 +
22425 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22426 +       if (unlikely(err)) {
22427 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22428 +                       h_dentry, bindex, err);
22429 +               err = 0;
22430 +       }
22431 +
22432 +out:
22433 +       AuTraceErr(err);
22434 +       return err;
22435 +}
22436 +
22437 +/*
22438 + * final procedure for deleting a entry.
22439 + * maintain dentry and iattr.
22440 + */
22441 +static void epilog(struct inode *dir, struct dentry *dentry,
22442 +                  aufs_bindex_t bindex)
22443 +{
22444 +       struct inode *inode;
22445 +
22446 +       inode = d_inode(dentry);
22447 +       d_drop(dentry);
22448 +       inode->i_ctime = dir->i_ctime;
22449 +
22450 +       au_dir_ts(dir, bindex);
22451 +       inode_inc_iversion(dir);
22452 +}
22453 +
22454 +/*
22455 + * when an error happened, remove the created whiteout and revert everything.
22456 + */
22457 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22458 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22459 +                    struct dentry *dentry, struct au_dtime *dt)
22460 +{
22461 +       int rerr;
22462 +       struct path h_path = {
22463 +               .dentry = wh_dentry,
22464 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22465 +       };
22466 +
22467 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22468 +       if (!rerr) {
22469 +               au_set_dbwh(dentry, bwh);
22470 +               au_dtime_revert(dt);
22471 +               return 0;
22472 +       }
22473 +
22474 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22475 +       return -EIO;
22476 +}
22477 +
22478 +/* ---------------------------------------------------------------------- */
22479 +
22480 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22481 +{
22482 +       int err;
22483 +       aufs_bindex_t bwh, bindex, btop;
22484 +       struct inode *inode, *h_dir, *delegated;
22485 +       struct dentry *parent, *wh_dentry;
22486 +       /* to reduce stack size */
22487 +       struct {
22488 +               struct au_dtime dt;
22489 +               struct au_pin pin;
22490 +               struct path h_path;
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_GEN);
22501 +       if (unlikely(err))
22502 +               goto out_free;
22503 +       err = au_d_hashed_positive(dentry);
22504 +       if (unlikely(err))
22505 +               goto out_unlock;
22506 +       inode = d_inode(dentry);
22507 +       IMustLock(inode);
22508 +       err = -EISDIR;
22509 +       if (unlikely(d_is_dir(dentry)))
22510 +               goto out_unlock; /* possible? */
22511 +
22512 +       btop = au_dbtop(dentry);
22513 +       bwh = au_dbwh(dentry);
22514 +       bindex = -1;
22515 +       parent = dentry->d_parent; /* dir inode is locked */
22516 +       di_write_lock_parent(parent);
22517 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22518 +                                       &a->pin);
22519 +       err = PTR_ERR(wh_dentry);
22520 +       if (IS_ERR(wh_dentry))
22521 +               goto out_parent;
22522 +
22523 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22524 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22525 +       dget(a->h_path.dentry);
22526 +       if (bindex == btop) {
22527 +               h_dir = au_pinned_h_dir(&a->pin);
22528 +               delegated = NULL;
22529 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22530 +               if (unlikely(err == -EWOULDBLOCK)) {
22531 +                       pr_warn("cannot retry for NFSv4 delegation"
22532 +                               " for an internal unlink\n");
22533 +                       iput(delegated);
22534 +               }
22535 +       } else {
22536 +               /* dir inode is locked */
22537 +               h_dir = d_inode(wh_dentry->d_parent);
22538 +               IMustLock(h_dir);
22539 +               err = 0;
22540 +       }
22541 +
22542 +       if (!err) {
22543 +               vfsub_drop_nlink(inode);
22544 +               epilog(dir, dentry, bindex);
22545 +
22546 +               /* update target timestamps */
22547 +               if (bindex == btop) {
22548 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22549 +                       /*ignore*/
22550 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22551 +               } else
22552 +                       /* todo: this timestamp may be reverted later */
22553 +                       inode->i_ctime = h_dir->i_ctime;
22554 +               goto out_unpin; /* success */
22555 +       }
22556 +
22557 +       /* revert */
22558 +       if (wh_dentry) {
22559 +               int rerr;
22560 +
22561 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22562 +                                &a->dt);
22563 +               if (rerr)
22564 +                       err = rerr;
22565 +       }
22566 +
22567 +out_unpin:
22568 +       au_unpin(&a->pin);
22569 +       dput(wh_dentry);
22570 +       dput(a->h_path.dentry);
22571 +out_parent:
22572 +       di_write_unlock(parent);
22573 +out_unlock:
22574 +       aufs_read_unlock(dentry, AuLock_DW);
22575 +out_free:
22576 +       au_kfree_rcu(a);
22577 +out:
22578 +       return err;
22579 +}
22580 +
22581 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22582 +{
22583 +       int err, rmdir_later;
22584 +       aufs_bindex_t bwh, bindex, btop;
22585 +       struct inode *inode;
22586 +       struct dentry *parent, *wh_dentry, *h_dentry;
22587 +       struct au_whtmp_rmdir *args;
22588 +       /* to reduce stack size */
22589 +       struct {
22590 +               struct au_dtime dt;
22591 +               struct au_pin pin;
22592 +       } *a;
22593 +
22594 +       IMustLock(dir);
22595 +
22596 +       err = -ENOMEM;
22597 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22598 +       if (unlikely(!a))
22599 +               goto out;
22600 +
22601 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22602 +       if (unlikely(err))
22603 +               goto out_free;
22604 +       err = au_alive_dir(dentry);
22605 +       if (unlikely(err))
22606 +               goto out_unlock;
22607 +       inode = d_inode(dentry);
22608 +       IMustLock(inode);
22609 +       err = -ENOTDIR;
22610 +       if (unlikely(!d_is_dir(dentry)))
22611 +               goto out_unlock; /* possible? */
22612 +
22613 +       err = -ENOMEM;
22614 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22615 +       if (unlikely(!args))
22616 +               goto out_unlock;
22617 +
22618 +       parent = dentry->d_parent; /* dir inode is locked */
22619 +       di_write_lock_parent(parent);
22620 +       err = au_test_empty(dentry, &args->whlist);
22621 +       if (unlikely(err))
22622 +               goto out_parent;
22623 +
22624 +       btop = au_dbtop(dentry);
22625 +       bwh = au_dbwh(dentry);
22626 +       bindex = -1;
22627 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22628 +                                       &a->pin);
22629 +       err = PTR_ERR(wh_dentry);
22630 +       if (IS_ERR(wh_dentry))
22631 +               goto out_parent;
22632 +
22633 +       h_dentry = au_h_dptr(dentry, btop);
22634 +       dget(h_dentry);
22635 +       rmdir_later = 0;
22636 +       if (bindex == btop) {
22637 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22638 +               if (err > 0) {
22639 +                       rmdir_later = err;
22640 +                       err = 0;
22641 +               }
22642 +       } else {
22643 +               /* stop monitoring */
22644 +               au_hn_free(au_hi(inode, btop));
22645 +
22646 +               /* dir inode is locked */
22647 +               IMustLock(d_inode(wh_dentry->d_parent));
22648 +               err = 0;
22649 +       }
22650 +
22651 +       if (!err) {
22652 +               vfsub_dead_dir(inode);
22653 +               au_set_dbdiropq(dentry, -1);
22654 +               epilog(dir, dentry, bindex);
22655 +
22656 +               if (rmdir_later) {
22657 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22658 +                       args = NULL;
22659 +               }
22660 +
22661 +               goto out_unpin; /* success */
22662 +       }
22663 +
22664 +       /* revert */
22665 +       AuLabel(revert);
22666 +       if (wh_dentry) {
22667 +               int rerr;
22668 +
22669 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22670 +                                &a->dt);
22671 +               if (rerr)
22672 +                       err = rerr;
22673 +       }
22674 +
22675 +out_unpin:
22676 +       au_unpin(&a->pin);
22677 +       dput(wh_dentry);
22678 +       dput(h_dentry);
22679 +out_parent:
22680 +       di_write_unlock(parent);
22681 +       if (args)
22682 +               au_whtmp_rmdir_free(args);
22683 +out_unlock:
22684 +       aufs_read_unlock(dentry, AuLock_DW);
22685 +out_free:
22686 +       au_kfree_rcu(a);
22687 +out:
22688 +       AuTraceErr(err);
22689 +       return err;
22690 +}
22691 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22692 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22693 +++ linux/fs/aufs/i_op_ren.c    2018-12-27 13:19:17.711749485 +0100
22694 @@ -0,0 +1,1249 @@
22695 +// SPDX-License-Identifier: GPL-2.0
22696 +/*
22697 + * Copyright (C) 2005-2018 Junjiro R. Okajima
22698 + *
22699 + * This program, aufs is free software; you can redistribute it and/or modify
22700 + * it under the terms of the GNU General Public License as published by
22701 + * the Free Software Foundation; either version 2 of the License, or
22702 + * (at your option) any later version.
22703 + *
22704 + * This program is distributed in the hope that it will be useful,
22705 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22706 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22707 + * GNU General Public License for more details.
22708 + *
22709 + * You should have received a copy of the GNU General Public License
22710 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22711 + */
22712 +
22713 +/*
22714 + * inode operation (rename entry)
22715 + * todo: this is crazy monster
22716 + */
22717 +
22718 +#include "aufs.h"
22719 +
22720 +enum { AuSRC, AuDST, AuSrcDst };
22721 +enum { AuPARENT, AuCHILD, AuParentChild };
22722 +
22723 +#define AuRen_ISDIR_SRC                1
22724 +#define AuRen_ISDIR_DST                (1 << 1)
22725 +#define AuRen_ISSAMEDIR                (1 << 2)
22726 +#define AuRen_WHSRC            (1 << 3)
22727 +#define AuRen_WHDST            (1 << 4)
22728 +#define AuRen_MNT_WRITE                (1 << 5)
22729 +#define AuRen_DT_DSTDIR                (1 << 6)
22730 +#define AuRen_DIROPQ_SRC       (1 << 7)
22731 +#define AuRen_DIROPQ_DST       (1 << 8)
22732 +#define AuRen_DIRREN           (1 << 9)
22733 +#define AuRen_DROPPED_SRC      (1 << 10)
22734 +#define AuRen_DROPPED_DST      (1 << 11)
22735 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22736 +#define au_fset_ren(flags, name) \
22737 +       do { (flags) |= AuRen_##name; } while (0)
22738 +#define au_fclr_ren(flags, name) \
22739 +       do { (flags) &= ~AuRen_##name; } while (0)
22740 +
22741 +#ifndef CONFIG_AUFS_DIRREN
22742 +#undef AuRen_DIRREN
22743 +#define AuRen_DIRREN           0
22744 +#endif
22745 +
22746 +struct au_ren_args {
22747 +       struct {
22748 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22749 +                       *wh_dentry;
22750 +               struct inode *dir, *inode;
22751 +               struct au_hinode *hdir, *hinode;
22752 +               struct au_dtime dt[AuParentChild];
22753 +               aufs_bindex_t btop, bdiropq;
22754 +       } sd[AuSrcDst];
22755 +
22756 +#define src_dentry     sd[AuSRC].dentry
22757 +#define src_dir                sd[AuSRC].dir
22758 +#define src_inode      sd[AuSRC].inode
22759 +#define src_h_dentry   sd[AuSRC].h_dentry
22760 +#define src_parent     sd[AuSRC].parent
22761 +#define src_h_parent   sd[AuSRC].h_parent
22762 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22763 +#define src_hdir       sd[AuSRC].hdir
22764 +#define src_hinode     sd[AuSRC].hinode
22765 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22766 +#define src_dt         sd[AuSRC].dt
22767 +#define src_btop       sd[AuSRC].btop
22768 +#define src_bdiropq    sd[AuSRC].bdiropq
22769 +
22770 +#define dst_dentry     sd[AuDST].dentry
22771 +#define dst_dir                sd[AuDST].dir
22772 +#define dst_inode      sd[AuDST].inode
22773 +#define dst_h_dentry   sd[AuDST].h_dentry
22774 +#define dst_parent     sd[AuDST].parent
22775 +#define dst_h_parent   sd[AuDST].h_parent
22776 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22777 +#define dst_hdir       sd[AuDST].hdir
22778 +#define dst_hinode     sd[AuDST].hinode
22779 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22780 +#define dst_dt         sd[AuDST].dt
22781 +#define dst_btop       sd[AuDST].btop
22782 +#define dst_bdiropq    sd[AuDST].bdiropq
22783 +
22784 +       struct dentry *h_trap;
22785 +       struct au_branch *br;
22786 +       struct path h_path;
22787 +       struct au_nhash whlist;
22788 +       aufs_bindex_t btgt, src_bwh;
22789 +
22790 +       struct {
22791 +               unsigned short auren_flags;
22792 +               unsigned char flags;    /* syscall parameter */
22793 +               unsigned char exchange;
22794 +       } __packed;
22795 +
22796 +       struct au_whtmp_rmdir *thargs;
22797 +       struct dentry *h_dst;
22798 +       struct au_hinode *h_root;
22799 +};
22800 +
22801 +/* ---------------------------------------------------------------------- */
22802 +
22803 +/*
22804 + * functions for reverting.
22805 + * when an error happened in a single rename systemcall, we should revert
22806 + * everything as if nothing happened.
22807 + * we don't need to revert the copied-up/down the parent dir since they are
22808 + * harmless.
22809 + */
22810 +
22811 +#define RevertFailure(fmt, ...) do { \
22812 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22813 +               ##__VA_ARGS__, err, rerr); \
22814 +       err = -EIO; \
22815 +} while (0)
22816 +
22817 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22818 +{
22819 +       int rerr;
22820 +       struct dentry *d;
22821 +#define src_or_dst(member) a->sd[idx].member
22822 +
22823 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22824 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22825 +       rerr = au_diropq_remove(d, a->btgt);
22826 +       au_hn_inode_unlock(src_or_dst(hinode));
22827 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22828 +       if (rerr)
22829 +               RevertFailure("remove diropq %pd", d);
22830 +
22831 +#undef src_or_dst_
22832 +}
22833 +
22834 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22835 +{
22836 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22837 +               au_ren_do_rev_diropq(err, a, AuSRC);
22838 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22839 +               au_ren_do_rev_diropq(err, a, AuDST);
22840 +}
22841 +
22842 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22843 +{
22844 +       int rerr;
22845 +       struct inode *delegated;
22846 +
22847 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22848 +                                         a->src_h_parent);
22849 +       rerr = PTR_ERR(a->h_path.dentry);
22850 +       if (IS_ERR(a->h_path.dentry)) {
22851 +               RevertFailure("lkup one %pd", a->src_dentry);
22852 +               return;
22853 +       }
22854 +
22855 +       delegated = NULL;
22856 +       rerr = vfsub_rename(a->dst_h_dir,
22857 +                           au_h_dptr(a->src_dentry, a->btgt),
22858 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22859 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22860 +               pr_warn("cannot retry for NFSv4 delegation"
22861 +                       " for an internal rename\n");
22862 +               iput(delegated);
22863 +       }
22864 +       d_drop(a->h_path.dentry);
22865 +       dput(a->h_path.dentry);
22866 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22867 +       if (rerr)
22868 +               RevertFailure("rename %pd", a->src_dentry);
22869 +}
22870 +
22871 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22872 +{
22873 +       int rerr;
22874 +       struct inode *delegated;
22875 +
22876 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22877 +                                         a->dst_h_parent);
22878 +       rerr = PTR_ERR(a->h_path.dentry);
22879 +       if (IS_ERR(a->h_path.dentry)) {
22880 +               RevertFailure("lkup one %pd", a->dst_dentry);
22881 +               return;
22882 +       }
22883 +       if (d_is_positive(a->h_path.dentry)) {
22884 +               d_drop(a->h_path.dentry);
22885 +               dput(a->h_path.dentry);
22886 +               return;
22887 +       }
22888 +
22889 +       delegated = NULL;
22890 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22891 +                           &delegated, a->flags);
22892 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22893 +               pr_warn("cannot retry for NFSv4 delegation"
22894 +                       " for an internal rename\n");
22895 +               iput(delegated);
22896 +       }
22897 +       d_drop(a->h_path.dentry);
22898 +       dput(a->h_path.dentry);
22899 +       if (!rerr)
22900 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22901 +       else
22902 +               RevertFailure("rename %pd", a->h_dst);
22903 +}
22904 +
22905 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22906 +{
22907 +       int rerr;
22908 +
22909 +       a->h_path.dentry = a->src_wh_dentry;
22910 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22911 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22912 +       if (rerr)
22913 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22914 +}
22915 +#undef RevertFailure
22916 +
22917 +/* ---------------------------------------------------------------------- */
22918 +
22919 +/*
22920 + * when we have to copyup the renaming entry, do it with the rename-target name
22921 + * in order to minimize the cost (the later actual rename is unnecessary).
22922 + * otherwise rename it on the target branch.
22923 + */
22924 +static int au_ren_or_cpup(struct au_ren_args *a)
22925 +{
22926 +       int err;
22927 +       struct dentry *d;
22928 +       struct inode *delegated;
22929 +
22930 +       d = a->src_dentry;
22931 +       if (au_dbtop(d) == a->btgt) {
22932 +               a->h_path.dentry = a->dst_h_dentry;
22933 +               AuDebugOn(au_dbtop(d) != a->btgt);
22934 +               delegated = NULL;
22935 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22936 +                                  a->dst_h_dir, &a->h_path, &delegated,
22937 +                                  a->flags);
22938 +               if (unlikely(err == -EWOULDBLOCK)) {
22939 +                       pr_warn("cannot retry for NFSv4 delegation"
22940 +                               " for an internal rename\n");
22941 +                       iput(delegated);
22942 +               }
22943 +       } else
22944 +               BUG();
22945 +
22946 +       if (!err && a->h_dst)
22947 +               /* it will be set to dinfo later */
22948 +               dget(a->h_dst);
22949 +
22950 +       return err;
22951 +}
22952 +
22953 +/* cf. aufs_rmdir() */
22954 +static int au_ren_del_whtmp(struct au_ren_args *a)
22955 +{
22956 +       int err;
22957 +       struct inode *dir;
22958 +
22959 +       dir = a->dst_dir;
22960 +       SiMustAnyLock(dir->i_sb);
22961 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22962 +                                    au_sbi(dir->i_sb)->si_dirwh)
22963 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22964 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22965 +               if (unlikely(err))
22966 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22967 +                               "ignored.\n", a->h_dst, err);
22968 +       } else {
22969 +               au_nhash_wh_free(&a->thargs->whlist);
22970 +               a->thargs->whlist = a->whlist;
22971 +               a->whlist.nh_num = 0;
22972 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22973 +               dput(a->h_dst);
22974 +               a->thargs = NULL;
22975 +       }
22976 +
22977 +       return 0;
22978 +}
22979 +
22980 +/* make it 'opaque' dir. */
22981 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22982 +{
22983 +       int err;
22984 +       struct dentry *d, *diropq;
22985 +#define src_or_dst(member) a->sd[idx].member
22986 +
22987 +       err = 0;
22988 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22989 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22990 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22991 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22992 +       diropq = au_diropq_create(d, a->btgt);
22993 +       au_hn_inode_unlock(src_or_dst(hinode));
22994 +       if (IS_ERR(diropq))
22995 +               err = PTR_ERR(diropq);
22996 +       else
22997 +               dput(diropq);
22998 +
22999 +#undef src_or_dst_
23000 +       return err;
23001 +}
23002 +
23003 +static int au_ren_diropq(struct au_ren_args *a)
23004 +{
23005 +       int err;
23006 +       unsigned char always;
23007 +       struct dentry *d;
23008 +
23009 +       err = 0;
23010 +       d = a->dst_dentry; /* already renamed on the branch */
23011 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
23012 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23013 +           && !au_ftest_ren(a->auren_flags, DIRREN)
23014 +           && a->btgt != au_dbdiropq(a->src_dentry)
23015 +           && (a->dst_wh_dentry
23016 +               || a->btgt <= au_dbdiropq(d)
23017 +               /* hide the lower to keep xino */
23018 +               /* the lowers may not be a dir, but we hide them anyway */
23019 +               || a->btgt < au_dbbot(d)
23020 +               || always)) {
23021 +               AuDbg("here\n");
23022 +               err = au_ren_do_diropq(a, AuSRC);
23023 +               if (unlikely(err))
23024 +                       goto out;
23025 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
23026 +       }
23027 +       if (!a->exchange)
23028 +               goto out; /* success */
23029 +
23030 +       d = a->src_dentry; /* already renamed on the branch */
23031 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
23032 +           && a->btgt != au_dbdiropq(a->dst_dentry)
23033 +           && (a->btgt < au_dbdiropq(d)
23034 +               || a->btgt < au_dbbot(d)
23035 +               || always)) {
23036 +               AuDbgDentry(a->src_dentry);
23037 +               AuDbgDentry(a->dst_dentry);
23038 +               err = au_ren_do_diropq(a, AuDST);
23039 +               if (unlikely(err))
23040 +                       goto out_rev_src;
23041 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
23042 +       }
23043 +       goto out; /* success */
23044 +
23045 +out_rev_src:
23046 +       AuDbg("err %d, reverting src\n", err);
23047 +       au_ren_rev_diropq(err, a);
23048 +out:
23049 +       return err;
23050 +}
23051 +
23052 +static int do_rename(struct au_ren_args *a)
23053 +{
23054 +       int err;
23055 +       struct dentry *d, *h_d;
23056 +
23057 +       if (!a->exchange) {
23058 +               /* prepare workqueue args for asynchronous rmdir */
23059 +               h_d = a->dst_h_dentry;
23060 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
23061 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
23062 +                   && d_is_positive(h_d)) {
23063 +                       err = -ENOMEM;
23064 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
23065 +                                                        GFP_NOFS);
23066 +                       if (unlikely(!a->thargs))
23067 +                               goto out;
23068 +                       a->h_dst = dget(h_d);
23069 +               }
23070 +
23071 +               /* create whiteout for src_dentry */
23072 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
23073 +                       a->src_bwh = au_dbwh(a->src_dentry);
23074 +                       AuDebugOn(a->src_bwh >= 0);
23075 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
23076 +                                                       a->src_h_parent);
23077 +                       err = PTR_ERR(a->src_wh_dentry);
23078 +                       if (IS_ERR(a->src_wh_dentry))
23079 +                               goto out_thargs;
23080 +               }
23081 +
23082 +               /* lookup whiteout for dentry */
23083 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
23084 +                       h_d = au_wh_lkup(a->dst_h_parent,
23085 +                                        &a->dst_dentry->d_name, a->br);
23086 +                       err = PTR_ERR(h_d);
23087 +                       if (IS_ERR(h_d))
23088 +                               goto out_whsrc;
23089 +                       if (d_is_negative(h_d))
23090 +                               dput(h_d);
23091 +                       else
23092 +                               a->dst_wh_dentry = h_d;
23093 +               }
23094 +
23095 +               /* rename dentry to tmpwh */
23096 +               if (a->thargs) {
23097 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
23098 +                       if (unlikely(err))
23099 +                               goto out_whdst;
23100 +
23101 +                       d = a->dst_dentry;
23102 +                       au_set_h_dptr(d, a->btgt, NULL);
23103 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
23104 +                       if (unlikely(err))
23105 +                               goto out_whtmp;
23106 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
23107 +               }
23108 +       }
23109 +
23110 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23111 +#if 0
23112 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23113 +              && d_is_positive(a->dst_h_dentry)
23114 +              && a->src_btop != a->btgt);
23115 +#endif
23116 +
23117 +       /* rename by vfs_rename or cpup */
23118 +       err = au_ren_or_cpup(a);
23119 +       if (unlikely(err))
23120 +               /* leave the copied-up one */
23121 +               goto out_whtmp;
23122 +
23123 +       /* make dir opaque */
23124 +       err = au_ren_diropq(a);
23125 +       if (unlikely(err))
23126 +               goto out_rename;
23127 +
23128 +       /* update target timestamps */
23129 +       if (a->exchange) {
23130 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23131 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23132 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23133 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23134 +       }
23135 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23136 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23137 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23138 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23139 +
23140 +       if (!a->exchange) {
23141 +               /* remove whiteout for dentry */
23142 +               if (a->dst_wh_dentry) {
23143 +                       a->h_path.dentry = a->dst_wh_dentry;
23144 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23145 +                                                 a->dst_dentry);
23146 +                       if (unlikely(err))
23147 +                               goto out_diropq;
23148 +               }
23149 +
23150 +               /* remove whtmp */
23151 +               if (a->thargs)
23152 +                       au_ren_del_whtmp(a); /* ignore this error */
23153 +
23154 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23155 +       }
23156 +       err = 0;
23157 +       goto out_success;
23158 +
23159 +out_diropq:
23160 +       au_ren_rev_diropq(err, a);
23161 +out_rename:
23162 +       au_ren_rev_rename(err, a);
23163 +       dput(a->h_dst);
23164 +out_whtmp:
23165 +       if (a->thargs)
23166 +               au_ren_rev_whtmp(err, a);
23167 +out_whdst:
23168 +       dput(a->dst_wh_dentry);
23169 +       a->dst_wh_dentry = NULL;
23170 +out_whsrc:
23171 +       if (a->src_wh_dentry)
23172 +               au_ren_rev_whsrc(err, a);
23173 +out_success:
23174 +       dput(a->src_wh_dentry);
23175 +       dput(a->dst_wh_dentry);
23176 +out_thargs:
23177 +       if (a->thargs) {
23178 +               dput(a->h_dst);
23179 +               au_whtmp_rmdir_free(a->thargs);
23180 +               a->thargs = NULL;
23181 +       }
23182 +out:
23183 +       return err;
23184 +}
23185 +
23186 +/* ---------------------------------------------------------------------- */
23187 +
23188 +/*
23189 + * test if @dentry dir can be rename destination or not.
23190 + * success means, it is a logically empty dir.
23191 + */
23192 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23193 +{
23194 +       return au_test_empty(dentry, whlist);
23195 +}
23196 +
23197 +/*
23198 + * test if @a->src_dentry dir can be rename source or not.
23199 + * if it can, return 0.
23200 + * success means,
23201 + * - it is a logically empty dir.
23202 + * - or, it exists on writable branch and has no children including whiteouts
23203 + *   on the lower branch unless DIRREN is on.
23204 + */
23205 +static int may_rename_srcdir(struct au_ren_args *a)
23206 +{
23207 +       int err;
23208 +       unsigned int rdhash;
23209 +       aufs_bindex_t btop, btgt;
23210 +       struct dentry *dentry;
23211 +       struct super_block *sb;
23212 +       struct au_sbinfo *sbinfo;
23213 +
23214 +       dentry = a->src_dentry;
23215 +       sb = dentry->d_sb;
23216 +       sbinfo = au_sbi(sb);
23217 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23218 +               au_fset_ren(a->auren_flags, DIRREN);
23219 +
23220 +       btgt = a->btgt;
23221 +       btop = au_dbtop(dentry);
23222 +       if (btop != btgt) {
23223 +               struct au_nhash whlist;
23224 +
23225 +               SiMustAnyLock(sb);
23226 +               rdhash = sbinfo->si_rdhash;
23227 +               if (!rdhash)
23228 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23229 +                                                          dentry));
23230 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23231 +               if (unlikely(err))
23232 +                       goto out;
23233 +               err = au_test_empty(dentry, &whlist);
23234 +               au_nhash_wh_free(&whlist);
23235 +               goto out;
23236 +       }
23237 +
23238 +       if (btop == au_dbtaildir(dentry))
23239 +               return 0; /* success */
23240 +
23241 +       err = au_test_empty_lower(dentry);
23242 +
23243 +out:
23244 +       if (err == -ENOTEMPTY) {
23245 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23246 +                       err = 0;
23247 +               } else {
23248 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23249 +                               "branches, is not supported\n");
23250 +                       err = -EXDEV;
23251 +               }
23252 +       }
23253 +       return err;
23254 +}
23255 +
23256 +/* side effect: sets whlist and h_dentry */
23257 +static int au_ren_may_dir(struct au_ren_args *a)
23258 +{
23259 +       int err;
23260 +       unsigned int rdhash;
23261 +       struct dentry *d;
23262 +
23263 +       d = a->dst_dentry;
23264 +       SiMustAnyLock(d->d_sb);
23265 +
23266 +       err = 0;
23267 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23268 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23269 +               if (!rdhash)
23270 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23271 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23272 +               if (unlikely(err))
23273 +                       goto out;
23274 +
23275 +               if (!a->exchange) {
23276 +                       au_set_dbtop(d, a->dst_btop);
23277 +                       err = may_rename_dstdir(d, &a->whlist);
23278 +                       au_set_dbtop(d, a->btgt);
23279 +               } else
23280 +                       err = may_rename_srcdir(a);
23281 +       }
23282 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23283 +       if (unlikely(err))
23284 +               goto out;
23285 +
23286 +       d = a->src_dentry;
23287 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23288 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23289 +               err = may_rename_srcdir(a);
23290 +               if (unlikely(err)) {
23291 +                       au_nhash_wh_free(&a->whlist);
23292 +                       a->whlist.nh_num = 0;
23293 +               }
23294 +       }
23295 +out:
23296 +       return err;
23297 +}
23298 +
23299 +/* ---------------------------------------------------------------------- */
23300 +
23301 +/*
23302 + * simple tests for rename.
23303 + * following the checks in vfs, plus the parent-child relationship.
23304 + */
23305 +static int au_may_ren(struct au_ren_args *a)
23306 +{
23307 +       int err, isdir;
23308 +       struct inode *h_inode;
23309 +
23310 +       if (a->src_btop == a->btgt) {
23311 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23312 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23313 +               if (unlikely(err))
23314 +                       goto out;
23315 +               err = -EINVAL;
23316 +               if (unlikely(a->src_h_dentry == a->h_trap))
23317 +                       goto out;
23318 +       }
23319 +
23320 +       err = 0;
23321 +       if (a->dst_btop != a->btgt)
23322 +               goto out;
23323 +
23324 +       err = -ENOTEMPTY;
23325 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23326 +               goto out;
23327 +
23328 +       err = -EIO;
23329 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23330 +       if (d_really_is_negative(a->dst_dentry)) {
23331 +               if (d_is_negative(a->dst_h_dentry))
23332 +                       err = au_may_add(a->dst_dentry, a->btgt,
23333 +                                        a->dst_h_parent, isdir);
23334 +       } else {
23335 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23336 +                       goto out;
23337 +               h_inode = d_inode(a->dst_h_dentry);
23338 +               if (h_inode->i_nlink)
23339 +                       err = au_may_del(a->dst_dentry, a->btgt,
23340 +                                        a->dst_h_parent, isdir);
23341 +       }
23342 +
23343 +out:
23344 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23345 +               err = -EIO;
23346 +       AuTraceErr(err);
23347 +       return err;
23348 +}
23349 +
23350 +/* ---------------------------------------------------------------------- */
23351 +
23352 +/*
23353 + * locking order
23354 + * (VFS)
23355 + * - src_dir and dir by lock_rename()
23356 + * - inode if exists
23357 + * (aufs)
23358 + * - lock all
23359 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23360 + *     + si_read_lock
23361 + *     + di_write_lock2_child()
23362 + *       + di_write_lock_child()
23363 + *        + ii_write_lock_child()
23364 + *       + di_write_lock_child2()
23365 + *        + ii_write_lock_child2()
23366 + *     + src_parent and parent
23367 + *       + di_write_lock_parent()
23368 + *        + ii_write_lock_parent()
23369 + *       + di_write_lock_parent2()
23370 + *        + ii_write_lock_parent2()
23371 + *   + lower src_dir and dir by vfsub_lock_rename()
23372 + *   + verify the every relationships between child and parent. if any
23373 + *     of them failed, unlock all and return -EBUSY.
23374 + */
23375 +static void au_ren_unlock(struct au_ren_args *a)
23376 +{
23377 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23378 +                           a->dst_h_parent, a->dst_hdir);
23379 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23380 +           && a->h_root)
23381 +               au_hn_inode_unlock(a->h_root);
23382 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23383 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23384 +}
23385 +
23386 +static int au_ren_lock(struct au_ren_args *a)
23387 +{
23388 +       int err;
23389 +       unsigned int udba;
23390 +
23391 +       err = 0;
23392 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23393 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23394 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23395 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23396 +
23397 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23398 +       if (unlikely(err))
23399 +               goto out;
23400 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23401 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23402 +               struct dentry *root;
23403 +               struct inode *dir;
23404 +
23405 +               /*
23406 +                * sbinfo is already locked, so this ii_read_lock is
23407 +                * unnecessary. but our debugging feature checks it.
23408 +                */
23409 +               root = a->src_inode->i_sb->s_root;
23410 +               if (root != a->src_parent && root != a->dst_parent) {
23411 +                       dir = d_inode(root);
23412 +                       ii_read_lock_parent3(dir);
23413 +                       a->h_root = au_hi(dir, a->btgt);
23414 +                       ii_read_unlock(dir);
23415 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23416 +               }
23417 +       }
23418 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23419 +                                     a->dst_h_parent, a->dst_hdir);
23420 +       udba = au_opt_udba(a->src_dentry->d_sb);
23421 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23422 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23423 +               err = au_busy_or_stale();
23424 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23425 +               err = au_h_verify(a->src_h_dentry, udba,
23426 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23427 +                                 a->br);
23428 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23429 +               err = au_h_verify(a->dst_h_dentry, udba,
23430 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23431 +                                 a->br);
23432 +       if (!err)
23433 +               goto out; /* success */
23434 +
23435 +       err = au_busy_or_stale();
23436 +       au_ren_unlock(a);
23437 +
23438 +out:
23439 +       return err;
23440 +}
23441 +
23442 +/* ---------------------------------------------------------------------- */
23443 +
23444 +static void au_ren_refresh_dir(struct au_ren_args *a)
23445 +{
23446 +       struct inode *dir;
23447 +
23448 +       dir = a->dst_dir;
23449 +       inode_inc_iversion(dir);
23450 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23451 +               /* is this updating defined in POSIX? */
23452 +               au_cpup_attr_timesizes(a->src_inode);
23453 +               au_cpup_attr_nlink(dir, /*force*/1);
23454 +       }
23455 +       au_dir_ts(dir, a->btgt);
23456 +
23457 +       if (a->exchange) {
23458 +               dir = a->src_dir;
23459 +               inode_inc_iversion(dir);
23460 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23461 +                       /* is this updating defined in POSIX? */
23462 +                       au_cpup_attr_timesizes(a->dst_inode);
23463 +                       au_cpup_attr_nlink(dir, /*force*/1);
23464 +               }
23465 +               au_dir_ts(dir, a->btgt);
23466 +       }
23467 +
23468 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23469 +               return;
23470 +
23471 +       dir = a->src_dir;
23472 +       inode_inc_iversion(dir);
23473 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23474 +               au_cpup_attr_nlink(dir, /*force*/1);
23475 +       au_dir_ts(dir, a->btgt);
23476 +}
23477 +
23478 +static void au_ren_refresh(struct au_ren_args *a)
23479 +{
23480 +       aufs_bindex_t bbot, bindex;
23481 +       struct dentry *d, *h_d;
23482 +       struct inode *i, *h_i;
23483 +       struct super_block *sb;
23484 +
23485 +       d = a->dst_dentry;
23486 +       d_drop(d);
23487 +       if (a->h_dst)
23488 +               /* already dget-ed by au_ren_or_cpup() */
23489 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23490 +
23491 +       i = a->dst_inode;
23492 +       if (i) {
23493 +               if (!a->exchange) {
23494 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23495 +                               vfsub_drop_nlink(i);
23496 +                       else {
23497 +                               vfsub_dead_dir(i);
23498 +                               au_cpup_attr_timesizes(i);
23499 +                       }
23500 +                       au_update_dbrange(d, /*do_put_zero*/1);
23501 +               } else
23502 +                       au_cpup_attr_nlink(i, /*force*/1);
23503 +       } else {
23504 +               bbot = a->btgt;
23505 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23506 +                       au_set_h_dptr(d, bindex, NULL);
23507 +               bbot = au_dbbot(d);
23508 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23509 +                       au_set_h_dptr(d, bindex, NULL);
23510 +               au_update_dbrange(d, /*do_put_zero*/0);
23511 +       }
23512 +
23513 +       if (a->exchange
23514 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23515 +               d_drop(a->src_dentry);
23516 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23517 +                       au_set_dbwh(a->src_dentry, -1);
23518 +               return;
23519 +       }
23520 +
23521 +       d = a->src_dentry;
23522 +       au_set_dbwh(d, -1);
23523 +       bbot = au_dbbot(d);
23524 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23525 +               h_d = au_h_dptr(d, bindex);
23526 +               if (h_d)
23527 +                       au_set_h_dptr(d, bindex, NULL);
23528 +       }
23529 +       au_set_dbbot(d, a->btgt);
23530 +
23531 +       sb = d->d_sb;
23532 +       i = a->src_inode;
23533 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23534 +               return; /* success */
23535 +
23536 +       bbot = au_ibbot(i);
23537 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23538 +               h_i = au_h_iptr(i, bindex);
23539 +               if (h_i) {
23540 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23541 +                       /* ignore this error */
23542 +                       au_set_h_iptr(i, bindex, NULL, 0);
23543 +               }
23544 +       }
23545 +       au_set_ibbot(i, a->btgt);
23546 +}
23547 +
23548 +/* ---------------------------------------------------------------------- */
23549 +
23550 +/* mainly for link(2) and rename(2) */
23551 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23552 +{
23553 +       aufs_bindex_t bdiropq, bwh;
23554 +       struct dentry *parent;
23555 +       struct au_branch *br;
23556 +
23557 +       parent = dentry->d_parent;
23558 +       IMustLock(d_inode(parent)); /* dir is locked */
23559 +
23560 +       bdiropq = au_dbdiropq(parent);
23561 +       bwh = au_dbwh(dentry);
23562 +       br = au_sbr(dentry->d_sb, btgt);
23563 +       if (au_br_rdonly(br)
23564 +           || (0 <= bdiropq && bdiropq < btgt)
23565 +           || (0 <= bwh && bwh < btgt))
23566 +               btgt = -1;
23567 +
23568 +       AuDbg("btgt %d\n", btgt);
23569 +       return btgt;
23570 +}
23571 +
23572 +/* sets src_btop, dst_btop and btgt */
23573 +static int au_ren_wbr(struct au_ren_args *a)
23574 +{
23575 +       int err;
23576 +       struct au_wr_dir_args wr_dir_args = {
23577 +               /* .force_btgt  = -1, */
23578 +               .flags          = AuWrDir_ADD_ENTRY
23579 +       };
23580 +
23581 +       a->src_btop = au_dbtop(a->src_dentry);
23582 +       a->dst_btop = au_dbtop(a->dst_dentry);
23583 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23584 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23585 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23586 +       wr_dir_args.force_btgt = a->src_btop;
23587 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23588 +               wr_dir_args.force_btgt = a->dst_btop;
23589 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23590 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23591 +       a->btgt = err;
23592 +       if (a->exchange)
23593 +               au_update_dbtop(a->dst_dentry);
23594 +
23595 +       return err;
23596 +}
23597 +
23598 +static void au_ren_dt(struct au_ren_args *a)
23599 +{
23600 +       a->h_path.dentry = a->src_h_parent;
23601 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23602 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23603 +               a->h_path.dentry = a->dst_h_parent;
23604 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23605 +       }
23606 +
23607 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23608 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23609 +           && !a->exchange)
23610 +               return;
23611 +
23612 +       a->h_path.dentry = a->src_h_dentry;
23613 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23614 +       if (d_is_positive(a->dst_h_dentry)) {
23615 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23616 +               a->h_path.dentry = a->dst_h_dentry;
23617 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23618 +       }
23619 +}
23620 +
23621 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23622 +{
23623 +       struct dentry *h_d;
23624 +       struct inode *h_inode;
23625 +
23626 +       au_dtime_revert(a->src_dt + AuPARENT);
23627 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23628 +               au_dtime_revert(a->dst_dt + AuPARENT);
23629 +
23630 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23631 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23632 +               h_inode = d_inode(h_d);
23633 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23634 +               au_dtime_revert(a->src_dt + AuCHILD);
23635 +               inode_unlock(h_inode);
23636 +
23637 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23638 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23639 +                       h_inode = d_inode(h_d);
23640 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23641 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23642 +                       inode_unlock(h_inode);
23643 +               }
23644 +       }
23645 +}
23646 +
23647 +/* ---------------------------------------------------------------------- */
23648 +
23649 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23650 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23651 +               unsigned int _flags)
23652 +{
23653 +       int err, lock_flags;
23654 +       void *rev;
23655 +       /* reduce stack space */
23656 +       struct au_ren_args *a;
23657 +       struct au_pin pin;
23658 +
23659 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23660 +       IMustLock(_src_dir);
23661 +       IMustLock(_dst_dir);
23662 +
23663 +       err = -EINVAL;
23664 +       if (unlikely(_flags & RENAME_WHITEOUT))
23665 +               goto out;
23666 +
23667 +       err = -ENOMEM;
23668 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23669 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23670 +       if (unlikely(!a))
23671 +               goto out;
23672 +
23673 +       a->flags = _flags;
23674 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23675 +                    && RENAME_EXCHANGE > U8_MAX);
23676 +       a->exchange = _flags & RENAME_EXCHANGE;
23677 +       a->src_dir = _src_dir;
23678 +       a->src_dentry = _src_dentry;
23679 +       a->src_inode = NULL;
23680 +       if (d_really_is_positive(a->src_dentry))
23681 +               a->src_inode = d_inode(a->src_dentry);
23682 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23683 +       a->dst_dir = _dst_dir;
23684 +       a->dst_dentry = _dst_dentry;
23685 +       a->dst_inode = NULL;
23686 +       if (d_really_is_positive(a->dst_dentry))
23687 +               a->dst_inode = d_inode(a->dst_dentry);
23688 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23689 +       if (a->dst_inode) {
23690 +               /*
23691 +                * if EXCHANGE && src is non-dir && dst is dir,
23692 +                * dst is not locked.
23693 +                */
23694 +               /* IMustLock(a->dst_inode); */
23695 +               au_igrab(a->dst_inode);
23696 +       }
23697 +
23698 +       err = -ENOTDIR;
23699 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23700 +       if (d_is_dir(a->src_dentry)) {
23701 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23702 +               if (unlikely(!a->exchange
23703 +                            && d_really_is_positive(a->dst_dentry)
23704 +                            && !d_is_dir(a->dst_dentry)))
23705 +                       goto out_free;
23706 +               lock_flags |= AuLock_DIRS;
23707 +       }
23708 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23709 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23710 +               if (unlikely(!a->exchange
23711 +                            && d_really_is_positive(a->src_dentry)
23712 +                            && !d_is_dir(a->src_dentry)))
23713 +                       goto out_free;
23714 +               lock_flags |= AuLock_DIRS;
23715 +       }
23716 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23717 +                                       lock_flags);
23718 +       if (unlikely(err))
23719 +               goto out_free;
23720 +
23721 +       err = au_d_hashed_positive(a->src_dentry);
23722 +       if (unlikely(err))
23723 +               goto out_unlock;
23724 +       err = -ENOENT;
23725 +       if (a->dst_inode) {
23726 +               /*
23727 +                * If it is a dir, VFS unhash it before this
23728 +                * function. It means we cannot rely upon d_unhashed().
23729 +                */
23730 +               if (unlikely(!a->dst_inode->i_nlink))
23731 +                       goto out_unlock;
23732 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23733 +                       err = au_d_hashed_positive(a->dst_dentry);
23734 +                       if (unlikely(err && !a->exchange))
23735 +                               goto out_unlock;
23736 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23737 +                       goto out_unlock;
23738 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23739 +               goto out_unlock;
23740 +
23741 +       /*
23742 +        * is it possible?
23743 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23744 +        * there may exist a problem somewhere else.
23745 +        */
23746 +       err = -EINVAL;
23747 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23748 +               goto out_unlock;
23749 +
23750 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23751 +       di_write_lock_parent(a->dst_parent);
23752 +
23753 +       /* which branch we process */
23754 +       err = au_ren_wbr(a);
23755 +       if (unlikely(err < 0))
23756 +               goto out_parent;
23757 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23758 +       a->h_path.mnt = au_br_mnt(a->br);
23759 +
23760 +       /* are they available to be renamed */
23761 +       err = au_ren_may_dir(a);
23762 +       if (unlikely(err))
23763 +               goto out_children;
23764 +
23765 +       /* prepare the writable parent dir on the same branch */
23766 +       if (a->dst_btop == a->btgt) {
23767 +               au_fset_ren(a->auren_flags, WHDST);
23768 +       } else {
23769 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23770 +               if (unlikely(err))
23771 +                       goto out_children;
23772 +       }
23773 +
23774 +       err = 0;
23775 +       if (!a->exchange) {
23776 +               if (a->src_dir != a->dst_dir) {
23777 +                       /*
23778 +                        * this temporary unlock is safe,
23779 +                        * because both dir->i_mutex are locked.
23780 +                        */
23781 +                       di_write_unlock(a->dst_parent);
23782 +                       di_write_lock_parent(a->src_parent);
23783 +                       err = au_wr_dir_need_wh(a->src_dentry,
23784 +                                               au_ftest_ren(a->auren_flags,
23785 +                                                            ISDIR_SRC),
23786 +                                               &a->btgt);
23787 +                       di_write_unlock(a->src_parent);
23788 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23789 +                                             /*isdir*/1);
23790 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23791 +               } else
23792 +                       err = au_wr_dir_need_wh(a->src_dentry,
23793 +                                               au_ftest_ren(a->auren_flags,
23794 +                                                            ISDIR_SRC),
23795 +                                               &a->btgt);
23796 +       }
23797 +       if (unlikely(err < 0))
23798 +               goto out_children;
23799 +       if (err)
23800 +               au_fset_ren(a->auren_flags, WHSRC);
23801 +
23802 +       /* cpup src */
23803 +       if (a->src_btop != a->btgt) {
23804 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23805 +                            au_opt_udba(a->src_dentry->d_sb),
23806 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23807 +               if (!err) {
23808 +                       struct au_cp_generic cpg = {
23809 +                               .dentry = a->src_dentry,
23810 +                               .bdst   = a->btgt,
23811 +                               .bsrc   = a->src_btop,
23812 +                               .len    = -1,
23813 +                               .pin    = &pin,
23814 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23815 +                       };
23816 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23817 +                       err = au_sio_cpup_simple(&cpg);
23818 +                       au_unpin(&pin);
23819 +               }
23820 +               if (unlikely(err))
23821 +                       goto out_children;
23822 +               a->src_btop = a->btgt;
23823 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23824 +               if (!a->exchange)
23825 +                       au_fset_ren(a->auren_flags, WHSRC);
23826 +       }
23827 +
23828 +       /* cpup dst */
23829 +       if (a->exchange && a->dst_inode
23830 +           && a->dst_btop != a->btgt) {
23831 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23832 +                            au_opt_udba(a->dst_dentry->d_sb),
23833 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23834 +               if (!err) {
23835 +                       struct au_cp_generic cpg = {
23836 +                               .dentry = a->dst_dentry,
23837 +                               .bdst   = a->btgt,
23838 +                               .bsrc   = a->dst_btop,
23839 +                               .len    = -1,
23840 +                               .pin    = &pin,
23841 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23842 +                       };
23843 +                       err = au_sio_cpup_simple(&cpg);
23844 +                       au_unpin(&pin);
23845 +               }
23846 +               if (unlikely(err))
23847 +                       goto out_children;
23848 +               a->dst_btop = a->btgt;
23849 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23850 +       }
23851 +
23852 +       /* lock them all */
23853 +       err = au_ren_lock(a);
23854 +       if (unlikely(err))
23855 +               /* leave the copied-up one */
23856 +               goto out_children;
23857 +
23858 +       if (!a->exchange) {
23859 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23860 +                       err = au_may_ren(a);
23861 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23862 +                       err = -ENAMETOOLONG;
23863 +               if (unlikely(err))
23864 +                       goto out_hdir;
23865 +       }
23866 +
23867 +       /* store timestamps to be revertible */
23868 +       au_ren_dt(a);
23869 +
23870 +       /* store dirren info */
23871 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23872 +               err = au_dr_rename(a->src_dentry, a->btgt,
23873 +                                  &a->dst_dentry->d_name, &rev);
23874 +               AuTraceErr(err);
23875 +               if (unlikely(err))
23876 +                       goto out_dt;
23877 +       }
23878 +
23879 +       /* here we go */
23880 +       err = do_rename(a);
23881 +       if (unlikely(err))
23882 +               goto out_dirren;
23883 +
23884 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23885 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23886 +
23887 +       /* update dir attributes */
23888 +       au_ren_refresh_dir(a);
23889 +
23890 +       /* dput/iput all lower dentries */
23891 +       au_ren_refresh(a);
23892 +
23893 +       goto out_hdir; /* success */
23894 +
23895 +out_dirren:
23896 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23897 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23898 +out_dt:
23899 +       au_ren_rev_dt(err, a);
23900 +out_hdir:
23901 +       au_ren_unlock(a);
23902 +out_children:
23903 +       au_nhash_wh_free(&a->whlist);
23904 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23905 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23906 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23907 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23908 +       }
23909 +out_parent:
23910 +       if (!err) {
23911 +               if (d_unhashed(a->src_dentry))
23912 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23913 +               if (d_unhashed(a->dst_dentry))
23914 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23915 +               if (!a->exchange)
23916 +                       d_move(a->src_dentry, a->dst_dentry);
23917 +               else {
23918 +                       d_exchange(a->src_dentry, a->dst_dentry);
23919 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23920 +                               d_drop(a->dst_dentry);
23921 +               }
23922 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23923 +                       d_drop(a->src_dentry);
23924 +       } else {
23925 +               au_update_dbtop(a->dst_dentry);
23926 +               if (!a->dst_inode)
23927 +                       d_drop(a->dst_dentry);
23928 +       }
23929 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23930 +               di_write_unlock(a->dst_parent);
23931 +       else
23932 +               di_write_unlock2(a->src_parent, a->dst_parent);
23933 +out_unlock:
23934 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23935 +out_free:
23936 +       iput(a->dst_inode);
23937 +       if (a->thargs)
23938 +               au_whtmp_rmdir_free(a->thargs);
23939 +       au_kfree_rcu(a);
23940 +out:
23941 +       AuTraceErr(err);
23942 +       return err;
23943 +}
23944 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23945 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23946 +++ linux/fs/aufs/Kconfig       2018-06-04 09:08:09.181412645 +0200
23947 @@ -0,0 +1,199 @@
23948 +# SPDX-License-Identifier: GPL-2.0
23949 +config AUFS_FS
23950 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23951 +       help
23952 +       Aufs is a stackable unification filesystem such as Unionfs,
23953 +       which unifies several directories and provides a merged single
23954 +       directory.
23955 +       In the early days, aufs was entirely re-designed and
23956 +       re-implemented Unionfs Version 1.x series. Introducing many
23957 +       original ideas, approaches and improvements, it becomes totally
23958 +       different from Unionfs while keeping the basic features.
23959 +
23960 +if AUFS_FS
23961 +choice
23962 +       prompt "Maximum number of branches"
23963 +       default AUFS_BRANCH_MAX_127
23964 +       help
23965 +       Specifies the maximum number of branches (or member directories)
23966 +       in a single aufs. The larger value consumes more system
23967 +       resources and has a minor impact to performance.
23968 +config AUFS_BRANCH_MAX_127
23969 +       bool "127"
23970 +       help
23971 +       Specifies the maximum number of branches (or member directories)
23972 +       in a single aufs. The larger value consumes more system
23973 +       resources and has a minor impact to performance.
23974 +config AUFS_BRANCH_MAX_511
23975 +       bool "511"
23976 +       help
23977 +       Specifies the maximum number of branches (or member directories)
23978 +       in a single aufs. The larger value consumes more system
23979 +       resources and has a minor impact to performance.
23980 +config AUFS_BRANCH_MAX_1023
23981 +       bool "1023"
23982 +       help
23983 +       Specifies the maximum number of branches (or member directories)
23984 +       in a single aufs. The larger value consumes more system
23985 +       resources and has a minor impact to performance.
23986 +config AUFS_BRANCH_MAX_32767
23987 +       bool "32767"
23988 +       help
23989 +       Specifies the maximum number of branches (or member directories)
23990 +       in a single aufs. The larger value consumes more system
23991 +       resources and has a minor impact to performance.
23992 +endchoice
23993 +
23994 +config AUFS_SBILIST
23995 +       bool
23996 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23997 +       default y
23998 +       help
23999 +       Automatic configuration for internal use.
24000 +       When aufs supports Magic SysRq or /proc, enabled automatically.
24001 +
24002 +config AUFS_HNOTIFY
24003 +       bool "Detect direct branch access (bypassing aufs)"
24004 +       help
24005 +       If you want to modify files on branches directly, eg. bypassing aufs,
24006 +       and want aufs to detect the changes of them fully, then enable this
24007 +       option and use 'udba=notify' mount option.
24008 +       Currently there is only one available configuration, "fsnotify".
24009 +       It will have a negative impact to the performance.
24010 +       See detail in aufs.5.
24011 +
24012 +choice
24013 +       prompt "method" if AUFS_HNOTIFY
24014 +       default AUFS_HFSNOTIFY
24015 +config AUFS_HFSNOTIFY
24016 +       bool "fsnotify"
24017 +       select FSNOTIFY
24018 +endchoice
24019 +
24020 +config AUFS_EXPORT
24021 +       bool "NFS-exportable aufs"
24022 +       depends on EXPORTFS
24023 +       help
24024 +       If you want to export your mounted aufs via NFS, then enable this
24025 +       option. There are several requirements for this configuration.
24026 +       See detail in aufs.5.
24027 +
24028 +config AUFS_INO_T_64
24029 +       bool
24030 +       depends on AUFS_EXPORT
24031 +       depends on 64BIT && !(ALPHA || S390)
24032 +       default y
24033 +       help
24034 +       Automatic configuration for internal use.
24035 +       /* typedef unsigned long/int __kernel_ino_t */
24036 +       /* alpha and s390x are int */
24037 +
24038 +config AUFS_XATTR
24039 +       bool "support for XATTR/EA (including Security Labels)"
24040 +       help
24041 +       If your branch fs supports XATTR/EA and you want to make them
24042 +       available in aufs too, then enable this opsion and specify the
24043 +       branch attributes for EA.
24044 +       See detail in aufs.5.
24045 +
24046 +config AUFS_FHSM
24047 +       bool "File-based Hierarchical Storage Management"
24048 +       help
24049 +       Hierarchical Storage Management (or HSM) is a well-known feature
24050 +       in the storage world. Aufs provides this feature as file-based.
24051 +       with multiple branches.
24052 +       These multiple branches are prioritized, ie. the topmost one
24053 +       should be the fastest drive and be used heavily.
24054 +
24055 +config AUFS_RDU
24056 +       bool "Readdir in userspace"
24057 +       help
24058 +       Aufs has two methods to provide a merged view for a directory,
24059 +       by a user-space library and by kernel-space natively. The latter
24060 +       is always enabled but sometimes large and slow.
24061 +       If you enable this option, install the library in aufs2-util
24062 +       package, and set some environment variables for your readdir(3),
24063 +       then the work will be handled in user-space which generally
24064 +       shows better performance in most cases.
24065 +       See detail in aufs.5.
24066 +
24067 +config AUFS_DIRREN
24068 +       bool "Workaround for rename(2)-ing a directory"
24069 +       help
24070 +       By default, aufs returns EXDEV error in renameing a dir who has
24071 +       his child on the lower branch, since it is a bad idea to issue
24072 +       rename(2) internally for every lower branch. But user may not
24073 +       accept this behaviour. So here is a workaround to allow such
24074 +       rename(2) and store some extra infromation on the writable
24075 +       branch. Obviously this costs high (and I don't like it).
24076 +       To use this feature, you need to enable this configuration AND
24077 +       to specify the mount option `dirren.'
24078 +       See details in aufs.5 and the design documents.
24079 +
24080 +config AUFS_SHWH
24081 +       bool "Show whiteouts"
24082 +       help
24083 +       If you want to make the whiteouts in aufs visible, then enable
24084 +       this option and specify 'shwh' mount option. Although it may
24085 +       sounds like philosophy or something, but in technically it
24086 +       simply shows the name of whiteout with keeping its behaviour.
24087 +
24088 +config AUFS_BR_RAMFS
24089 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
24090 +       help
24091 +       If you want to use ramfs as an aufs branch fs, then enable this
24092 +       option. Generally tmpfs is recommended.
24093 +       Aufs prohibited them to be a branch fs by default, because
24094 +       initramfs becomes unusable after switch_root or something
24095 +       generally. If you sets initramfs as an aufs branch and boot your
24096 +       system by switch_root, you will meet a problem easily since the
24097 +       files in initramfs may be inaccessible.
24098 +       Unless you are going to use ramfs as an aufs branch fs without
24099 +       switch_root or something, leave it N.
24100 +
24101 +config AUFS_BR_FUSE
24102 +       bool "Fuse fs as an aufs branch"
24103 +       depends on FUSE_FS
24104 +       select AUFS_POLL
24105 +       help
24106 +       If you want to use fuse-based userspace filesystem as an aufs
24107 +       branch fs, then enable this option.
24108 +       It implements the internal poll(2) operation which is
24109 +       implemented by fuse only (curretnly).
24110 +
24111 +config AUFS_POLL
24112 +       bool
24113 +       help
24114 +       Automatic configuration for internal use.
24115 +
24116 +config AUFS_BR_HFSPLUS
24117 +       bool "Hfsplus as an aufs branch"
24118 +       depends on HFSPLUS_FS
24119 +       default y
24120 +       help
24121 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24122 +       this option. This option introduces a small overhead at
24123 +       copying-up a file on hfsplus.
24124 +
24125 +config AUFS_BDEV_LOOP
24126 +       bool
24127 +       depends on BLK_DEV_LOOP
24128 +       default y
24129 +       help
24130 +       Automatic configuration for internal use.
24131 +       Convert =[ym] into =y.
24132 +
24133 +config AUFS_DEBUG
24134 +       bool "Debug aufs"
24135 +       help
24136 +       Enable this to compile aufs internal debug code.
24137 +       It will have a negative impact to the performance.
24138 +
24139 +config AUFS_MAGIC_SYSRQ
24140 +       bool
24141 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24142 +       default y
24143 +       help
24144 +       Automatic configuration for internal use.
24145 +       When aufs supports Magic SysRq, enabled automatically.
24146 +endif
24147 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24148 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
24149 +++ linux/fs/aufs/lcnt.h        2018-10-23 12:33:35.599375796 +0200
24150 @@ -0,0 +1,186 @@
24151 +/* SPDX-License-Identifier: GPL-2.0 */
24152 +/*
24153 + * Copyright (C) 2018 Junjiro R. Okajima
24154 + *
24155 + * This program, aufs is free software; you can redistribute it and/or modify
24156 + * it under the terms of the GNU General Public License as published by
24157 + * the Free Software Foundation; either version 2 of the License, or
24158 + * (at your option) any later version.
24159 + *
24160 + * This program is distributed in the hope that it will be useful,
24161 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24162 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24163 + * GNU General Public License for more details.
24164 + *
24165 + * You should have received a copy of the GNU General Public License
24166 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24167 + */
24168 +
24169 +/*
24170 + * simple long counter wrapper
24171 + */
24172 +
24173 +#ifndef __AUFS_LCNT_H__
24174 +#define __AUFS_LCNT_H__
24175 +
24176 +#ifdef __KERNEL__
24177 +
24178 +#include "debug.h"
24179 +
24180 +#define AuLCntATOMIC   1
24181 +#define AuLCntPCPUCNT  2
24182 +/*
24183 + * why does percpu_refcount require extra synchronize_rcu()s in
24184 + * au_br_do_free()
24185 + */
24186 +#define AuLCntPCPUREF  3
24187 +
24188 +/* #define AuLCntChosen        AuLCntATOMIC */
24189 +#define AuLCntChosen   AuLCntPCPUCNT
24190 +/* #define AuLCntChosen        AuLCntPCPUREF */
24191 +
24192 +#if AuLCntChosen == AuLCntATOMIC
24193 +#include <linux/atomic.h>
24194 +
24195 +typedef atomic_long_t au_lcnt_t;
24196 +
24197 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24198 +{
24199 +       atomic_long_set(cnt, 0);
24200 +       return 0;
24201 +}
24202 +
24203 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24204 +{
24205 +       /* empty */
24206 +}
24207 +
24208 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24209 +                              int do_sync __maybe_unused)
24210 +{
24211 +       /* empty */
24212 +}
24213 +
24214 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24215 +{
24216 +       atomic_long_inc(cnt);
24217 +}
24218 +
24219 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24220 +{
24221 +       atomic_long_dec(cnt);
24222 +}
24223 +
24224 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24225 +{
24226 +       return atomic_long_read(cnt);
24227 +}
24228 +#endif
24229 +
24230 +#if AuLCntChosen == AuLCntPCPUCNT
24231 +#include <linux/percpu_counter.h>
24232 +
24233 +typedef struct percpu_counter au_lcnt_t;
24234 +
24235 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24236 +{
24237 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24238 +}
24239 +
24240 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24241 +{
24242 +       /* empty */
24243 +}
24244 +
24245 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24246 +{
24247 +       percpu_counter_destroy(cnt);
24248 +}
24249 +
24250 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24251 +{
24252 +       percpu_counter_inc(cnt);
24253 +}
24254 +
24255 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24256 +{
24257 +       percpu_counter_dec(cnt);
24258 +}
24259 +
24260 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24261 +{
24262 +       s64 n;
24263 +
24264 +       n = percpu_counter_sum(cnt);
24265 +       BUG_ON(n < 0);
24266 +       if (LONG_MAX != LLONG_MAX
24267 +           && n > LONG_MAX)
24268 +               AuWarn1("%s\n", "wrap-around");
24269 +
24270 +       return n;
24271 +}
24272 +#endif
24273 +
24274 +#if AuLCntChosen == AuLCntPCPUREF
24275 +#include <linux/percpu-refcount.h>
24276 +
24277 +typedef struct percpu_ref au_lcnt_t;
24278 +
24279 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24280 +{
24281 +       if (!release)
24282 +               release = percpu_ref_exit;
24283 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24284 +}
24285 +
24286 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24287 +{
24288 +       synchronize_rcu();
24289 +}
24290 +
24291 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24292 +{
24293 +       percpu_ref_kill(cnt);
24294 +       if (do_sync)
24295 +               au_lcnt_wait_for_fin(cnt);
24296 +}
24297 +
24298 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24299 +{
24300 +       percpu_ref_get(cnt);
24301 +}
24302 +
24303 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24304 +{
24305 +       percpu_ref_put(cnt);
24306 +}
24307 +
24308 +/*
24309 + * avoid calling this func as possible.
24310 + */
24311 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24312 +{
24313 +       long l;
24314 +
24315 +       percpu_ref_switch_to_atomic_sync(cnt);
24316 +       l = atomic_long_read(&cnt->count);
24317 +       if (do_rev)
24318 +               percpu_ref_switch_to_percpu(cnt);
24319 +
24320 +       /* percpu_ref is initialized by 1 instead of 0 */
24321 +       return l - 1;
24322 +}
24323 +#endif
24324 +
24325 +#ifdef CONFIG_AUFS_DEBUG
24326 +#define AuLCntZero(val) do {                   \
24327 +       long l = val;                           \
24328 +       if (l)                                  \
24329 +               AuDbg("%s = %ld\n", #val, l);   \
24330 +} while (0)
24331 +#else
24332 +#define AuLCntZero(val)                do {} while (0)
24333 +#endif
24334 +
24335 +#endif /* __KERNEL__ */
24336 +#endif /* __AUFS_LCNT_H__ */
24337 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24338 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24339 +++ linux/fs/aufs/loop.c        2018-12-27 13:19:17.711749485 +0100
24340 @@ -0,0 +1,148 @@
24341 +// SPDX-License-Identifier: GPL-2.0
24342 +/*
24343 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24344 + *
24345 + * This program, aufs is free software; you can redistribute it and/or modify
24346 + * it under the terms of the GNU General Public License as published by
24347 + * the Free Software Foundation; either version 2 of the License, or
24348 + * (at your option) any later version.
24349 + *
24350 + * This program is distributed in the hope that it will be useful,
24351 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24352 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24353 + * GNU General Public License for more details.
24354 + *
24355 + * You should have received a copy of the GNU General Public License
24356 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24357 + */
24358 +
24359 +/*
24360 + * support for loopback block device as a branch
24361 + */
24362 +
24363 +#include "aufs.h"
24364 +
24365 +/* added into drivers/block/loop.c */
24366 +static struct file *(*backing_file_func)(struct super_block *sb);
24367 +
24368 +/*
24369 + * test if two lower dentries have overlapping branches.
24370 + */
24371 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24372 +{
24373 +       struct super_block *h_sb;
24374 +       struct file *backing_file;
24375 +
24376 +       if (unlikely(!backing_file_func)) {
24377 +               /* don't load "loop" module here */
24378 +               backing_file_func = symbol_get(loop_backing_file);
24379 +               if (unlikely(!backing_file_func))
24380 +                       /* "loop" module is not loaded */
24381 +                       return 0;
24382 +       }
24383 +
24384 +       h_sb = h_adding->d_sb;
24385 +       backing_file = backing_file_func(h_sb);
24386 +       if (!backing_file)
24387 +               return 0;
24388 +
24389 +       h_adding = backing_file->f_path.dentry;
24390 +       /*
24391 +        * h_adding can be local NFS.
24392 +        * in this case aufs cannot detect the loop.
24393 +        */
24394 +       if (unlikely(h_adding->d_sb == sb))
24395 +               return 1;
24396 +       return !!au_test_subdir(h_adding, sb->s_root);
24397 +}
24398 +
24399 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24400 +int au_test_loopback_kthread(void)
24401 +{
24402 +       int ret;
24403 +       struct task_struct *tsk = current;
24404 +       char c, comm[sizeof(tsk->comm)];
24405 +
24406 +       ret = 0;
24407 +       if (tsk->flags & PF_KTHREAD) {
24408 +               get_task_comm(comm, tsk);
24409 +               c = comm[4];
24410 +               ret = ('0' <= c && c <= '9'
24411 +                      && !strncmp(comm, "loop", 4));
24412 +       }
24413 +
24414 +       return ret;
24415 +}
24416 +
24417 +/* ---------------------------------------------------------------------- */
24418 +
24419 +#define au_warn_loopback_step  16
24420 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24421 +static unsigned long *au_warn_loopback_array;
24422 +
24423 +void au_warn_loopback(struct super_block *h_sb)
24424 +{
24425 +       int i, new_nelem;
24426 +       unsigned long *a, magic;
24427 +       static DEFINE_SPINLOCK(spin);
24428 +
24429 +       magic = h_sb->s_magic;
24430 +       spin_lock(&spin);
24431 +       a = au_warn_loopback_array;
24432 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24433 +               if (a[i] == magic) {
24434 +                       spin_unlock(&spin);
24435 +                       return;
24436 +               }
24437 +
24438 +       /* h_sb is new to us, print it */
24439 +       if (i < au_warn_loopback_nelem) {
24440 +               a[i] = magic;
24441 +               goto pr;
24442 +       }
24443 +
24444 +       /* expand the array */
24445 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24446 +       a = au_kzrealloc(au_warn_loopback_array,
24447 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24448 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24449 +                        /*may_shrink*/0);
24450 +       if (a) {
24451 +               au_warn_loopback_nelem = new_nelem;
24452 +               au_warn_loopback_array = a;
24453 +               a[i] = magic;
24454 +               goto pr;
24455 +       }
24456 +
24457 +       spin_unlock(&spin);
24458 +       AuWarn1("realloc failed, ignored\n");
24459 +       return;
24460 +
24461 +pr:
24462 +       spin_unlock(&spin);
24463 +       pr_warn("you may want to try another patch for loopback file "
24464 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24465 +}
24466 +
24467 +int au_loopback_init(void)
24468 +{
24469 +       int err;
24470 +       struct super_block *sb __maybe_unused;
24471 +
24472 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
24473 +
24474 +       err = 0;
24475 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24476 +                                        sizeof(unsigned long), GFP_NOFS);
24477 +       if (unlikely(!au_warn_loopback_array))
24478 +               err = -ENOMEM;
24479 +
24480 +       return err;
24481 +}
24482 +
24483 +void au_loopback_fin(void)
24484 +{
24485 +       if (backing_file_func)
24486 +               symbol_put(loop_backing_file);
24487 +       au_kfree_try_rcu(au_warn_loopback_array);
24488 +}
24489 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24490 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24491 +++ linux/fs/aufs/loop.h        2018-08-12 23:43:05.460124736 +0200
24492 @@ -0,0 +1,53 @@
24493 +/* SPDX-License-Identifier: GPL-2.0 */
24494 +/*
24495 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24496 + *
24497 + * This program, aufs is free software; you can redistribute it and/or modify
24498 + * it under the terms of the GNU General Public License as published by
24499 + * the Free Software Foundation; either version 2 of the License, or
24500 + * (at your option) any later version.
24501 + *
24502 + * This program is distributed in the hope that it will be useful,
24503 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24504 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24505 + * GNU General Public License for more details.
24506 + *
24507 + * You should have received a copy of the GNU General Public License
24508 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24509 + */
24510 +
24511 +/*
24512 + * support for loopback mount as a branch
24513 + */
24514 +
24515 +#ifndef __AUFS_LOOP_H__
24516 +#define __AUFS_LOOP_H__
24517 +
24518 +#ifdef __KERNEL__
24519 +
24520 +struct dentry;
24521 +struct super_block;
24522 +
24523 +#ifdef CONFIG_AUFS_BDEV_LOOP
24524 +/* drivers/block/loop.c */
24525 +struct file *loop_backing_file(struct super_block *sb);
24526 +
24527 +/* loop.c */
24528 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24529 +int au_test_loopback_kthread(void);
24530 +void au_warn_loopback(struct super_block *h_sb);
24531 +
24532 +int au_loopback_init(void);
24533 +void au_loopback_fin(void);
24534 +#else
24535 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24536 +          struct dentry *h_adding)
24537 +AuStubInt0(au_test_loopback_kthread, void)
24538 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24539 +
24540 +AuStubInt0(au_loopback_init, void)
24541 +AuStubVoid(au_loopback_fin, void)
24542 +#endif /* BLK_DEV_LOOP */
24543 +
24544 +#endif /* __KERNEL__ */
24545 +#endif /* __AUFS_LOOP_H__ */
24546 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24547 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24548 +++ linux/fs/aufs/magic.mk      2018-06-04 09:08:09.188079511 +0200
24549 @@ -0,0 +1,31 @@
24550 +# SPDX-License-Identifier: GPL-2.0
24551 +
24552 +# defined in ${srctree}/fs/fuse/inode.c
24553 +# tristate
24554 +ifdef CONFIG_FUSE_FS
24555 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24556 +endif
24557 +
24558 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24559 +# tristate
24560 +ifdef CONFIG_XFS_FS
24561 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24562 +endif
24563 +
24564 +# defined in ${srctree}/fs/configfs/mount.c
24565 +# tristate
24566 +ifdef CONFIG_CONFIGFS_FS
24567 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24568 +endif
24569 +
24570 +# defined in ${srctree}/fs/ubifs/ubifs.h
24571 +# tristate
24572 +ifdef CONFIG_UBIFS_FS
24573 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24574 +endif
24575 +
24576 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24577 +# tristate
24578 +ifdef CONFIG_HFSPLUS_FS
24579 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24580 +endif
24581 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24582 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24583 +++ linux/fs/aufs/Makefile      2018-06-04 09:08:09.181412645 +0200
24584 @@ -0,0 +1,46 @@
24585 +# SPDX-License-Identifier: GPL-2.0
24586 +
24587 +include ${src}/magic.mk
24588 +ifeq (${CONFIG_AUFS_FS},m)
24589 +include ${src}/conf.mk
24590 +endif
24591 +-include ${src}/priv_def.mk
24592 +
24593 +# cf. include/linux/kernel.h
24594 +# enable pr_debug
24595 +ccflags-y += -DDEBUG
24596 +# sparse requires the full pathname
24597 +ifdef M
24598 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24599 +else
24600 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24601 +endif
24602 +
24603 +obj-$(CONFIG_AUFS_FS) += aufs.o
24604 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24605 +       wkq.o vfsub.o dcsub.o \
24606 +       cpup.o whout.o wbr_policy.o \
24607 +       dinfo.o dentry.o \
24608 +       dynop.o \
24609 +       finfo.o file.o f_op.o \
24610 +       dir.o vdir.o \
24611 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24612 +       mvdown.o ioctl.o
24613 +
24614 +# all are boolean
24615 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24616 +aufs-$(CONFIG_SYSFS) += sysfs.o
24617 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24618 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24619 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24620 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24621 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24622 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24623 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24624 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24625 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24626 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24627 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24628 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24629 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24630 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24631 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24632 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24633 +++ linux/fs/aufs/module.c      2018-12-27 13:19:17.711749485 +0100
24634 @@ -0,0 +1,273 @@
24635 +// SPDX-License-Identifier: GPL-2.0
24636 +/*
24637 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24638 + *
24639 + * This program, aufs is free software; you can redistribute it and/or modify
24640 + * it under the terms of the GNU General Public License as published by
24641 + * the Free Software Foundation; either version 2 of the License, or
24642 + * (at your option) any later version.
24643 + *
24644 + * This program is distributed in the hope that it will be useful,
24645 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24646 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24647 + * GNU General Public License for more details.
24648 + *
24649 + * You should have received a copy of the GNU General Public License
24650 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24651 + */
24652 +
24653 +/*
24654 + * module global variables and operations
24655 + */
24656 +
24657 +#include <linux/module.h>
24658 +#include <linux/seq_file.h>
24659 +#include "aufs.h"
24660 +
24661 +/* shrinkable realloc */
24662 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24663 +{
24664 +       size_t sz;
24665 +       int diff;
24666 +
24667 +       sz = 0;
24668 +       diff = -1;
24669 +       if (p) {
24670 +#if 0 /* unused */
24671 +               if (!new_sz) {
24672 +                       au_kfree_rcu(p);
24673 +                       p = NULL;
24674 +                       goto out;
24675 +               }
24676 +#else
24677 +               AuDebugOn(!new_sz);
24678 +#endif
24679 +               sz = ksize(p);
24680 +               diff = au_kmidx_sub(sz, new_sz);
24681 +       }
24682 +       if (sz && !diff)
24683 +               goto out;
24684 +
24685 +       if (sz < new_sz)
24686 +               /* expand or SLOB */
24687 +               p = krealloc(p, new_sz, gfp);
24688 +       else if (new_sz < sz && may_shrink) {
24689 +               /* shrink */
24690 +               void *q;
24691 +
24692 +               q = kmalloc(new_sz, gfp);
24693 +               if (q) {
24694 +                       if (p) {
24695 +                               memcpy(q, p, new_sz);
24696 +                               au_kfree_try_rcu(p);
24697 +                       }
24698 +                       p = q;
24699 +               } else
24700 +                       p = NULL;
24701 +       }
24702 +
24703 +out:
24704 +       return p;
24705 +}
24706 +
24707 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24708 +                  int may_shrink)
24709 +{
24710 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24711 +       if (p && new_sz > nused)
24712 +               memset(p + nused, 0, new_sz - nused);
24713 +       return p;
24714 +}
24715 +
24716 +/* ---------------------------------------------------------------------- */
24717 +/*
24718 + * aufs caches
24719 + */
24720 +struct kmem_cache *au_cache[AuCache_Last];
24721 +
24722 +static void au_cache_fin(void)
24723 +{
24724 +       int i;
24725 +
24726 +       /*
24727 +        * Make sure all delayed rcu free inodes are flushed before we
24728 +        * destroy cache.
24729 +        */
24730 +       rcu_barrier();
24731 +
24732 +       /* excluding AuCache_HNOTIFY */
24733 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24734 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24735 +               kmem_cache_destroy(au_cache[i]);
24736 +               au_cache[i] = NULL;
24737 +       }
24738 +}
24739 +
24740 +static int __init au_cache_init(void)
24741 +{
24742 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24743 +       if (au_cache[AuCache_DINFO])
24744 +               /* SLAB_DESTROY_BY_RCU */
24745 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24746 +                                                      au_icntnr_init_once);
24747 +       if (au_cache[AuCache_ICNTNR])
24748 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24749 +                                                     au_fi_init_once);
24750 +       if (au_cache[AuCache_FINFO])
24751 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24752 +       if (au_cache[AuCache_VDIR])
24753 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24754 +       if (au_cache[AuCache_DEHSTR])
24755 +               return 0;
24756 +
24757 +       au_cache_fin();
24758 +       return -ENOMEM;
24759 +}
24760 +
24761 +/* ---------------------------------------------------------------------- */
24762 +
24763 +int au_dir_roflags;
24764 +
24765 +#ifdef CONFIG_AUFS_SBILIST
24766 +/*
24767 + * iterate_supers_type() doesn't protect us from
24768 + * remounting (branch management)
24769 + */
24770 +struct hlist_bl_head au_sbilist;
24771 +#endif
24772 +
24773 +/*
24774 + * functions for module interface.
24775 + */
24776 +MODULE_LICENSE("GPL");
24777 +/* MODULE_LICENSE("GPL v2"); */
24778 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24779 +MODULE_DESCRIPTION(AUFS_NAME
24780 +       " -- Advanced multi layered unification filesystem");
24781 +MODULE_VERSION(AUFS_VERSION);
24782 +MODULE_ALIAS_FS(AUFS_NAME);
24783 +
24784 +/* this module parameter has no meaning when SYSFS is disabled */
24785 +int sysaufs_brs = 1;
24786 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24787 +module_param_named(brs, sysaufs_brs, int, 0444);
24788 +
24789 +/* this module parameter has no meaning when USER_NS is disabled */
24790 +bool au_userns;
24791 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24792 +module_param_named(allow_userns, au_userns, bool, 0444);
24793 +
24794 +/* ---------------------------------------------------------------------- */
24795 +
24796 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24797 +
24798 +int au_seq_path(struct seq_file *seq, struct path *path)
24799 +{
24800 +       int err;
24801 +
24802 +       err = seq_path(seq, path, au_esc_chars);
24803 +       if (err >= 0)
24804 +               err = 0;
24805 +       else
24806 +               err = -ENOMEM;
24807 +
24808 +       return err;
24809 +}
24810 +
24811 +/* ---------------------------------------------------------------------- */
24812 +
24813 +static int __init aufs_init(void)
24814 +{
24815 +       int err, i;
24816 +       char *p;
24817 +
24818 +       p = au_esc_chars;
24819 +       for (i = 1; i <= ' '; i++)
24820 +               *p++ = i;
24821 +       *p++ = '\\';
24822 +       *p++ = '\x7f';
24823 +       *p = 0;
24824 +
24825 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24826 +
24827 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24828 +       for (i = 0; i < AuIop_Last; i++)
24829 +               aufs_iop_nogetattr[i].getattr = NULL;
24830 +
24831 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24832 +
24833 +       au_sbilist_init();
24834 +       sysaufs_brs_init();
24835 +       au_debug_init();
24836 +       au_dy_init();
24837 +       err = sysaufs_init();
24838 +       if (unlikely(err))
24839 +               goto out;
24840 +       err = dbgaufs_init();
24841 +       if (unlikely(err))
24842 +               goto out_sysaufs;
24843 +       err = au_procfs_init();
24844 +       if (unlikely(err))
24845 +               goto out_dbgaufs;
24846 +       err = au_wkq_init();
24847 +       if (unlikely(err))
24848 +               goto out_procfs;
24849 +       err = au_loopback_init();
24850 +       if (unlikely(err))
24851 +               goto out_wkq;
24852 +       err = au_hnotify_init();
24853 +       if (unlikely(err))
24854 +               goto out_loopback;
24855 +       err = au_sysrq_init();
24856 +       if (unlikely(err))
24857 +               goto out_hin;
24858 +       err = au_cache_init();
24859 +       if (unlikely(err))
24860 +               goto out_sysrq;
24861 +
24862 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24863 +       err = register_filesystem(&aufs_fs_type);
24864 +       if (unlikely(err))
24865 +               goto out_cache;
24866 +
24867 +       /* since we define pr_fmt, call printk directly */
24868 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24869 +       goto out; /* success */
24870 +
24871 +out_cache:
24872 +       au_cache_fin();
24873 +out_sysrq:
24874 +       au_sysrq_fin();
24875 +out_hin:
24876 +       au_hnotify_fin();
24877 +out_loopback:
24878 +       au_loopback_fin();
24879 +out_wkq:
24880 +       au_wkq_fin();
24881 +out_procfs:
24882 +       au_procfs_fin();
24883 +out_dbgaufs:
24884 +       dbgaufs_fin();
24885 +out_sysaufs:
24886 +       sysaufs_fin();
24887 +       au_dy_fin();
24888 +out:
24889 +       return err;
24890 +}
24891 +
24892 +static void __exit aufs_exit(void)
24893 +{
24894 +       unregister_filesystem(&aufs_fs_type);
24895 +       au_cache_fin();
24896 +       au_sysrq_fin();
24897 +       au_hnotify_fin();
24898 +       au_loopback_fin();
24899 +       au_wkq_fin();
24900 +       au_procfs_fin();
24901 +       dbgaufs_fin();
24902 +       sysaufs_fin();
24903 +       au_dy_fin();
24904 +}
24905 +
24906 +module_init(aufs_init);
24907 +module_exit(aufs_exit);
24908 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24909 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24910 +++ linux/fs/aufs/module.h      2018-12-27 13:19:17.711749485 +0100
24911 @@ -0,0 +1,166 @@
24912 +/* SPDX-License-Identifier: GPL-2.0 */
24913 +/*
24914 + * Copyright (C) 2005-2018 Junjiro R. Okajima
24915 + *
24916 + * This program, aufs is free software; you can redistribute it and/or modify
24917 + * it under the terms of the GNU General Public License as published by
24918 + * the Free Software Foundation; either version 2 of the License, or
24919 + * (at your option) any later version.
24920 + *
24921 + * This program is distributed in the hope that it will be useful,
24922 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24923 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24924 + * GNU General Public License for more details.
24925 + *
24926 + * You should have received a copy of the GNU General Public License
24927 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24928 + */
24929 +
24930 +/*
24931 + * module initialization and module-global
24932 + */
24933 +
24934 +#ifndef __AUFS_MODULE_H__
24935 +#define __AUFS_MODULE_H__
24936 +
24937 +#ifdef __KERNEL__
24938 +
24939 +#include <linux/slab.h>
24940 +#include "debug.h"
24941 +#include "dentry.h"
24942 +#include "dir.h"
24943 +#include "file.h"
24944 +#include "inode.h"
24945 +
24946 +struct path;
24947 +struct seq_file;
24948 +
24949 +/* module parameters */
24950 +extern int sysaufs_brs;
24951 +extern bool au_userns;
24952 +
24953 +/* ---------------------------------------------------------------------- */
24954 +
24955 +extern int au_dir_roflags;
24956 +
24957 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24958 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24959 +                  int may_shrink);
24960 +
24961 +/*
24962 + * Comparing the size of the object with sizeof(struct rcu_head)
24963 + * case 1: object is always larger
24964 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24965 + * case 2: object is always smaller
24966 + *     --> au_kfree_small()
24967 + * case 3: object can be any size
24968 + *     --> au_kfree_try_rcu()
24969 + */
24970 +
24971 +static inline void au_kfree_do_rcu(const void *p)
24972 +{
24973 +       struct {
24974 +               struct rcu_head rcu;
24975 +       } *a = (void *)p;
24976 +
24977 +       kfree_rcu(a, rcu);
24978 +}
24979 +
24980 +#define au_kfree_rcu(_p) do {                                          \
24981 +               typeof(_p) p = (_p);                                    \
24982 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24983 +               if (p)                                                  \
24984 +                       au_kfree_do_rcu(p);                             \
24985 +       } while (0)
24986 +
24987 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24988 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24989 +
24990 +static inline void au_kfree_try_rcu(const void *p)
24991 +{
24992 +       if (!p)
24993 +               return;
24994 +       if (au_kfree_sz_test(p))
24995 +               au_kfree_do_rcu(p);
24996 +       else
24997 +               kfree(p);
24998 +}
24999 +
25000 +static inline void au_kfree_small(const void *p)
25001 +{
25002 +       if (!p)
25003 +               return;
25004 +       AuDebugOn(au_kfree_sz_test(p));
25005 +       kfree(p);
25006 +}
25007 +
25008 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
25009 +{
25010 +#ifndef CONFIG_SLOB
25011 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
25012 +#else
25013 +       return -1; /* SLOB is untested */
25014 +#endif
25015 +}
25016 +
25017 +int au_seq_path(struct seq_file *seq, struct path *path);
25018 +
25019 +#ifdef CONFIG_PROC_FS
25020 +/* procfs.c */
25021 +int __init au_procfs_init(void);
25022 +void au_procfs_fin(void);
25023 +#else
25024 +AuStubInt0(au_procfs_init, void);
25025 +AuStubVoid(au_procfs_fin, void);
25026 +#endif
25027 +
25028 +/* ---------------------------------------------------------------------- */
25029 +
25030 +/* kmem cache */
25031 +enum {
25032 +       AuCache_DINFO,
25033 +       AuCache_ICNTNR,
25034 +       AuCache_FINFO,
25035 +       AuCache_VDIR,
25036 +       AuCache_DEHSTR,
25037 +       AuCache_HNOTIFY, /* must be last */
25038 +       AuCache_Last
25039 +};
25040 +
25041 +extern struct kmem_cache *au_cache[AuCache_Last];
25042 +
25043 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
25044 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
25045 +#define AuCacheCtor(type, ctor)        \
25046 +       kmem_cache_create(#type, sizeof(struct type), \
25047 +                         __alignof__(struct type), AuCacheFlags, ctor)
25048 +
25049 +#define AuCacheFuncs(name, index)                                      \
25050 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
25051 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
25052 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
25053 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
25054 +                                                                       \
25055 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
25056 +       { void *p = rcu;                                                \
25057 +               p -= offsetof(struct au_##name, rcu);                   \
25058 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
25059 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
25060 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
25061 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
25062 +                                                                       \
25063 +       static inline void au_cache_free_##name(struct au_##name *p)    \
25064 +       { /* au_cache_free_##name##_norcu(p); */                        \
25065 +               au_cache_free_##name##_rcu(p); }
25066 +
25067 +AuCacheFuncs(dinfo, DINFO);
25068 +AuCacheFuncs(icntnr, ICNTNR);
25069 +AuCacheFuncs(finfo, FINFO);
25070 +AuCacheFuncs(vdir, VDIR);
25071 +AuCacheFuncs(vdir_dehstr, DEHSTR);
25072 +#ifdef CONFIG_AUFS_HNOTIFY
25073 +AuCacheFuncs(hnotify, HNOTIFY);
25074 +#endif
25075 +
25076 +#endif /* __KERNEL__ */
25077 +#endif /* __AUFS_MODULE_H__ */
25078 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
25079 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
25080 +++ linux/fs/aufs/mvdown.c      2018-12-27 13:19:17.711749485 +0100
25081 @@ -0,0 +1,705 @@
25082 +// SPDX-License-Identifier: GPL-2.0
25083 +/*
25084 + * Copyright (C) 2011-2018 Junjiro R. Okajima
25085 + *
25086 + * This program, aufs is free software; you can redistribute it and/or modify
25087 + * it under the terms of the GNU General Public License as published by
25088 + * the Free Software Foundation; either version 2 of the License, or
25089 + * (at your option) any later version.
25090 + *
25091 + * This program is distributed in the hope that it will be useful,
25092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25094 + * GNU General Public License for more details.
25095 + *
25096 + * You should have received a copy of the GNU General Public License
25097 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25098 + */
25099 +
25100 +/*
25101 + * move-down, opposite of copy-up
25102 + */
25103 +
25104 +#include "aufs.h"
25105 +
25106 +struct au_mvd_args {
25107 +       struct {
25108 +               struct super_block *h_sb;
25109 +               struct dentry *h_parent;
25110 +               struct au_hinode *hdir;
25111 +               struct inode *h_dir, *h_inode;
25112 +               struct au_pin pin;
25113 +       } info[AUFS_MVDOWN_NARRAY];
25114 +
25115 +       struct aufs_mvdown mvdown;
25116 +       struct dentry *dentry, *parent;
25117 +       struct inode *inode, *dir;
25118 +       struct super_block *sb;
25119 +       aufs_bindex_t bopq, bwh, bfound;
25120 +       unsigned char rename_lock;
25121 +};
25122 +
25123 +#define mvd_errno              mvdown.au_errno
25124 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25125 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25126 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25127 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25128 +
25129 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
25130 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
25131 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
25132 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
25133 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
25134 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
25135 +
25136 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
25137 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
25138 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
25139 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
25140 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
25141 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
25142 +
25143 +#define AU_MVD_PR(flag, ...) do {                      \
25144 +               if (flag)                               \
25145 +                       pr_err(__VA_ARGS__);            \
25146 +       } while (0)
25147 +
25148 +static int find_lower_writable(struct au_mvd_args *a)
25149 +{
25150 +       struct super_block *sb;
25151 +       aufs_bindex_t bindex, bbot;
25152 +       struct au_branch *br;
25153 +
25154 +       sb = a->sb;
25155 +       bindex = a->mvd_bsrc;
25156 +       bbot = au_sbbot(sb);
25157 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25158 +               for (bindex++; bindex <= bbot; bindex++) {
25159 +                       br = au_sbr(sb, bindex);
25160 +                       if (au_br_fhsm(br->br_perm)
25161 +                           && !sb_rdonly(au_br_sb(br)))
25162 +                               return bindex;
25163 +               }
25164 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25165 +               for (bindex++; bindex <= bbot; bindex++) {
25166 +                       br = au_sbr(sb, bindex);
25167 +                       if (!au_br_rdonly(br))
25168 +                               return bindex;
25169 +               }
25170 +       else
25171 +               for (bindex++; bindex <= bbot; bindex++) {
25172 +                       br = au_sbr(sb, bindex);
25173 +                       if (!sb_rdonly(au_br_sb(br))) {
25174 +                               if (au_br_rdonly(br))
25175 +                                       a->mvdown.flags
25176 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25177 +                               return bindex;
25178 +                       }
25179 +               }
25180 +
25181 +       return -1;
25182 +}
25183 +
25184 +/* make the parent dir on bdst */
25185 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25186 +{
25187 +       int err;
25188 +
25189 +       err = 0;
25190 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25191 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25192 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25193 +       a->mvd_h_dst_parent = NULL;
25194 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25195 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25196 +       if (!a->mvd_h_dst_parent) {
25197 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25198 +               if (unlikely(err)) {
25199 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25200 +                       goto out;
25201 +               }
25202 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25203 +       }
25204 +
25205 +out:
25206 +       AuTraceErr(err);
25207 +       return err;
25208 +}
25209 +
25210 +/* lock them all */
25211 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25212 +{
25213 +       int err;
25214 +       struct dentry *h_trap;
25215 +
25216 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25217 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25218 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25219 +                    au_opt_udba(a->sb),
25220 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25221 +       AuTraceErr(err);
25222 +       if (unlikely(err)) {
25223 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25224 +               goto out;
25225 +       }
25226 +
25227 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25228 +               a->rename_lock = 0;
25229 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25230 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25231 +                           au_opt_udba(a->sb),
25232 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25233 +               err = au_do_pin(&a->mvd_pin_src);
25234 +               AuTraceErr(err);
25235 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25236 +               if (unlikely(err)) {
25237 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25238 +                       goto out_dst;
25239 +               }
25240 +               goto out; /* success */
25241 +       }
25242 +
25243 +       a->rename_lock = 1;
25244 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25245 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25246 +                    au_opt_udba(a->sb),
25247 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25248 +       AuTraceErr(err);
25249 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25250 +       if (unlikely(err)) {
25251 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25252 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25253 +               goto out_dst;
25254 +       }
25255 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25256 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25257 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25258 +       if (h_trap) {
25259 +               err = (h_trap != a->mvd_h_src_parent);
25260 +               if (err)
25261 +                       err = (h_trap != a->mvd_h_dst_parent);
25262 +       }
25263 +       BUG_ON(err); /* it should never happen */
25264 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25265 +               err = -EBUSY;
25266 +               AuTraceErr(err);
25267 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25268 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25269 +               au_pin_hdir_lock(&a->mvd_pin_src);
25270 +               au_unpin(&a->mvd_pin_src);
25271 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25272 +               goto out_dst;
25273 +       }
25274 +       goto out; /* success */
25275 +
25276 +out_dst:
25277 +       au_unpin(&a->mvd_pin_dst);
25278 +out:
25279 +       AuTraceErr(err);
25280 +       return err;
25281 +}
25282 +
25283 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25284 +{
25285 +       if (!a->rename_lock)
25286 +               au_unpin(&a->mvd_pin_src);
25287 +       else {
25288 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25289 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25290 +               au_pin_hdir_lock(&a->mvd_pin_src);
25291 +               au_unpin(&a->mvd_pin_src);
25292 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25293 +       }
25294 +       au_unpin(&a->mvd_pin_dst);
25295 +}
25296 +
25297 +/* copy-down the file */
25298 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25299 +{
25300 +       int err;
25301 +       struct au_cp_generic cpg = {
25302 +               .dentry = a->dentry,
25303 +               .bdst   = a->mvd_bdst,
25304 +               .bsrc   = a->mvd_bsrc,
25305 +               .len    = -1,
25306 +               .pin    = &a->mvd_pin_dst,
25307 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25308 +       };
25309 +
25310 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25311 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25312 +               au_fset_cpup(cpg.flags, OVERWRITE);
25313 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25314 +               au_fset_cpup(cpg.flags, RWDST);
25315 +       err = au_sio_cpdown_simple(&cpg);
25316 +       if (unlikely(err))
25317 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25318 +
25319 +       AuTraceErr(err);
25320 +       return err;
25321 +}
25322 +
25323 +/*
25324 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25325 + * were sleeping
25326 + */
25327 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25328 +{
25329 +       int err;
25330 +       struct path h_path;
25331 +       struct au_branch *br;
25332 +       struct inode *delegated;
25333 +
25334 +       br = au_sbr(a->sb, a->mvd_bdst);
25335 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25336 +       err = PTR_ERR(h_path.dentry);
25337 +       if (IS_ERR(h_path.dentry)) {
25338 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25339 +               goto out;
25340 +       }
25341 +
25342 +       err = 0;
25343 +       if (d_is_positive(h_path.dentry)) {
25344 +               h_path.mnt = au_br_mnt(br);
25345 +               delegated = NULL;
25346 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25347 +                                  &delegated, /*force*/0);
25348 +               if (unlikely(err == -EWOULDBLOCK)) {
25349 +                       pr_warn("cannot retry for NFSv4 delegation"
25350 +                               " for an internal unlink\n");
25351 +                       iput(delegated);
25352 +               }
25353 +               if (unlikely(err))
25354 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25355 +       }
25356 +       dput(h_path.dentry);
25357 +
25358 +out:
25359 +       AuTraceErr(err);
25360 +       return err;
25361 +}
25362 +
25363 +/*
25364 + * unlink the topmost h_dentry
25365 + */
25366 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25367 +{
25368 +       int err;
25369 +       struct path h_path;
25370 +       struct inode *delegated;
25371 +
25372 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25373 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25374 +       delegated = NULL;
25375 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25376 +       if (unlikely(err == -EWOULDBLOCK)) {
25377 +               pr_warn("cannot retry for NFSv4 delegation"
25378 +                       " for an internal unlink\n");
25379 +               iput(delegated);
25380 +       }
25381 +       if (unlikely(err))
25382 +               AU_MVD_PR(dmsg, "unlink failed\n");
25383 +
25384 +       AuTraceErr(err);
25385 +       return err;
25386 +}
25387 +
25388 +/* Since mvdown succeeded, we ignore an error of this function */
25389 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25390 +{
25391 +       int err;
25392 +       struct au_branch *br;
25393 +
25394 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25395 +       br = au_sbr(a->sb, a->mvd_bsrc);
25396 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25397 +       if (!err) {
25398 +               br = au_sbr(a->sb, a->mvd_bdst);
25399 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25400 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25401 +       }
25402 +       if (!err)
25403 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25404 +       else
25405 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25406 +}
25407 +
25408 +/*
25409 + * copy-down the file and unlink the bsrc file.
25410 + * - unlink the bdst whout if exist
25411 + * - copy-down the file (with whtmp name and rename)
25412 + * - unlink the bsrc file
25413 + */
25414 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25415 +{
25416 +       int err;
25417 +
25418 +       err = au_do_mkdir(dmsg, a);
25419 +       if (!err)
25420 +               err = au_do_lock(dmsg, a);
25421 +       if (unlikely(err))
25422 +               goto out;
25423 +
25424 +       /*
25425 +        * do not revert the activities we made on bdst since they should be
25426 +        * harmless in aufs.
25427 +        */
25428 +
25429 +       err = au_do_cpdown(dmsg, a);
25430 +       if (!err)
25431 +               err = au_do_unlink_wh(dmsg, a);
25432 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25433 +               err = au_do_unlink(dmsg, a);
25434 +       if (unlikely(err))
25435 +               goto out_unlock;
25436 +
25437 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25438 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25439 +       if (find_lower_writable(a) < 0)
25440 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25441 +
25442 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25443 +               au_do_stfs(dmsg, a);
25444 +
25445 +       /* maintain internal array */
25446 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25447 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25448 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25449 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25450 +               au_set_ibtop(a->inode, a->mvd_bdst);
25451 +       } else {
25452 +               /* hide the lower */
25453 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25454 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25455 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25456 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25457 +       }
25458 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25459 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25460 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25461 +               au_set_ibbot(a->inode, a->mvd_bdst);
25462 +
25463 +out_unlock:
25464 +       au_do_unlock(dmsg, a);
25465 +out:
25466 +       AuTraceErr(err);
25467 +       return err;
25468 +}
25469 +
25470 +/* ---------------------------------------------------------------------- */
25471 +
25472 +/* make sure the file is idle */
25473 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25474 +{
25475 +       int err, plinked;
25476 +
25477 +       err = 0;
25478 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25479 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25480 +           && au_dcount(a->dentry) == 1
25481 +           && atomic_read(&a->inode->i_count) == 1
25482 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25483 +           && (!plinked || !au_plink_test(a->inode))
25484 +           && a->inode->i_nlink == 1)
25485 +               goto out;
25486 +
25487 +       err = -EBUSY;
25488 +       AU_MVD_PR(dmsg,
25489 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25490 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25491 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25492 +                 a->mvd_h_src_inode->i_nlink,
25493 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25494 +
25495 +out:
25496 +       AuTraceErr(err);
25497 +       return err;
25498 +}
25499 +
25500 +/* make sure the parent dir is fine */
25501 +static int au_mvd_args_parent(const unsigned char dmsg,
25502 +                             struct au_mvd_args *a)
25503 +{
25504 +       int err;
25505 +       aufs_bindex_t bindex;
25506 +
25507 +       err = 0;
25508 +       if (unlikely(au_alive_dir(a->parent))) {
25509 +               err = -ENOENT;
25510 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25511 +               goto out;
25512 +       }
25513 +
25514 +       a->bopq = au_dbdiropq(a->parent);
25515 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25516 +       AuDbg("b%d\n", bindex);
25517 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25518 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25519 +               err = -EINVAL;
25520 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25521 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25522 +                         a->bopq, a->mvd_bdst);
25523 +       }
25524 +
25525 +out:
25526 +       AuTraceErr(err);
25527 +       return err;
25528 +}
25529 +
25530 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25531 +                                   struct au_mvd_args *a)
25532 +{
25533 +       int err;
25534 +       struct au_dinfo *dinfo, *tmp;
25535 +
25536 +       /* lookup the next lower positive entry */
25537 +       err = -ENOMEM;
25538 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25539 +       if (unlikely(!tmp))
25540 +               goto out;
25541 +
25542 +       a->bfound = -1;
25543 +       a->bwh = -1;
25544 +       dinfo = au_di(a->dentry);
25545 +       au_di_cp(tmp, dinfo);
25546 +       au_di_swap(tmp, dinfo);
25547 +
25548 +       /* returns the number of positive dentries */
25549 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25550 +                            /* AuLkup_IGNORE_PERM */ 0);
25551 +       if (!err)
25552 +               a->bwh = au_dbwh(a->dentry);
25553 +       else if (err > 0)
25554 +               a->bfound = au_dbtop(a->dentry);
25555 +
25556 +       au_di_swap(tmp, dinfo);
25557 +       au_rw_write_unlock(&tmp->di_rwsem);
25558 +       au_di_free(tmp);
25559 +       if (unlikely(err < 0))
25560 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25561 +
25562 +       /*
25563 +        * here, we have these cases.
25564 +        * bfound == -1
25565 +        *      no positive dentry under bsrc. there are more sub-cases.
25566 +        *      bwh < 0
25567 +        *              there no whiteout, we can safely move-down.
25568 +        *      bwh <= bsrc
25569 +        *              impossible
25570 +        *      bsrc < bwh && bwh < bdst
25571 +        *              there is a whiteout on RO branch. cannot proceed.
25572 +        *      bwh == bdst
25573 +        *              there is a whiteout on the RW target branch. it should
25574 +        *              be removed.
25575 +        *      bdst < bwh
25576 +        *              there is a whiteout somewhere unrelated branch.
25577 +        * -1 < bfound && bfound <= bsrc
25578 +        *      impossible.
25579 +        * bfound < bdst
25580 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25581 +        *      proceed.
25582 +        * bfound == bdst
25583 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25584 +        *      error.
25585 +        * bdst < bfound
25586 +        *      found, after we create the file on bdst, it will be hidden.
25587 +        */
25588 +
25589 +       AuDebugOn(a->bfound == -1
25590 +                 && a->bwh != -1
25591 +                 && a->bwh <= a->mvd_bsrc);
25592 +       AuDebugOn(-1 < a->bfound
25593 +                 && a->bfound <= a->mvd_bsrc);
25594 +
25595 +       err = -EINVAL;
25596 +       if (a->bfound == -1
25597 +           && a->mvd_bsrc < a->bwh
25598 +           && a->bwh != -1
25599 +           && a->bwh < a->mvd_bdst) {
25600 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25601 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25602 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25603 +               goto out;
25604 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25605 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25606 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25607 +                         a->mvd_bdst, a->bfound);
25608 +               goto out;
25609 +       }
25610 +
25611 +       err = 0; /* success */
25612 +
25613 +out:
25614 +       AuTraceErr(err);
25615 +       return err;
25616 +}
25617 +
25618 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25619 +{
25620 +       int err;
25621 +
25622 +       err = 0;
25623 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25624 +           && a->bfound == a->mvd_bdst)
25625 +               err = -EEXIST;
25626 +       AuTraceErr(err);
25627 +       return err;
25628 +}
25629 +
25630 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25631 +{
25632 +       int err;
25633 +       struct au_branch *br;
25634 +
25635 +       err = -EISDIR;
25636 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25637 +               goto out;
25638 +
25639 +       err = -EINVAL;
25640 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25641 +               a->mvd_bsrc = au_ibtop(a->inode);
25642 +       else {
25643 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25644 +               if (unlikely(a->mvd_bsrc < 0
25645 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25646 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25647 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25648 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25649 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25650 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25651 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25652 +                       AU_MVD_PR(dmsg, "no upper\n");
25653 +                       goto out;
25654 +               }
25655 +       }
25656 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25657 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25658 +               AU_MVD_PR(dmsg, "on the bottom\n");
25659 +               goto out;
25660 +       }
25661 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25662 +       br = au_sbr(a->sb, a->mvd_bsrc);
25663 +       err = au_br_rdonly(br);
25664 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25665 +               if (unlikely(err))
25666 +                       goto out;
25667 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25668 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25669 +               if (err)
25670 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25671 +               /* go on */
25672 +       } else
25673 +               goto out;
25674 +
25675 +       err = -EINVAL;
25676 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25677 +               a->mvd_bdst = find_lower_writable(a);
25678 +               if (unlikely(a->mvd_bdst < 0)) {
25679 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25680 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25681 +                       goto out;
25682 +               }
25683 +       } else {
25684 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25685 +               if (unlikely(a->mvd_bdst < 0
25686 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25687 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25688 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25689 +                       goto out;
25690 +               }
25691 +       }
25692 +
25693 +       err = au_mvd_args_busy(dmsg, a);
25694 +       if (!err)
25695 +               err = au_mvd_args_parent(dmsg, a);
25696 +       if (!err)
25697 +               err = au_mvd_args_intermediate(dmsg, a);
25698 +       if (!err)
25699 +               err = au_mvd_args_exist(dmsg, a);
25700 +       if (!err)
25701 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25702 +
25703 +out:
25704 +       AuTraceErr(err);
25705 +       return err;
25706 +}
25707 +
25708 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25709 +{
25710 +       int err, e;
25711 +       unsigned char dmsg;
25712 +       struct au_mvd_args *args;
25713 +       struct inode *inode;
25714 +
25715 +       inode = d_inode(dentry);
25716 +       err = -EPERM;
25717 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25718 +               goto out;
25719 +
25720 +       err = -ENOMEM;
25721 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25722 +       if (unlikely(!args))
25723 +               goto out;
25724 +
25725 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25726 +       if (!err)
25727 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
25728 +       if (unlikely(err)) {
25729 +               err = -EFAULT;
25730 +               AuTraceErr(err);
25731 +               goto out_free;
25732 +       }
25733 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25734 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25735 +       args->mvdown.au_errno = 0;
25736 +       args->dentry = dentry;
25737 +       args->inode = inode;
25738 +       args->sb = dentry->d_sb;
25739 +
25740 +       err = -ENOENT;
25741 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25742 +       args->parent = dget_parent(dentry);
25743 +       args->dir = d_inode(args->parent);
25744 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25745 +       dput(args->parent);
25746 +       if (unlikely(args->parent != dentry->d_parent)) {
25747 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25748 +               goto out_dir;
25749 +       }
25750 +
25751 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25752 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25753 +       if (unlikely(err))
25754 +               goto out_inode;
25755 +
25756 +       di_write_lock_parent(args->parent);
25757 +       err = au_mvd_args(dmsg, args);
25758 +       if (unlikely(err))
25759 +               goto out_parent;
25760 +
25761 +       err = au_do_mvdown(dmsg, args);
25762 +       if (unlikely(err))
25763 +               goto out_parent;
25764 +
25765 +       au_cpup_attr_timesizes(args->dir);
25766 +       au_cpup_attr_timesizes(inode);
25767 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25768 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25769 +       /* au_digen_dec(dentry); */
25770 +
25771 +out_parent:
25772 +       di_write_unlock(args->parent);
25773 +       aufs_read_unlock(dentry, AuLock_DW);
25774 +out_inode:
25775 +       inode_unlock(inode);
25776 +out_dir:
25777 +       inode_unlock(args->dir);
25778 +out_free:
25779 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25780 +       if (unlikely(e))
25781 +               err = -EFAULT;
25782 +       au_kfree_rcu(args);
25783 +out:
25784 +       AuTraceErr(err);
25785 +       return err;
25786 +}
25787 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25788 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25789 +++ linux/fs/aufs/opts.c        2018-12-27 13:19:17.711749485 +0100
25790 @@ -0,0 +1,1877 @@
25791 +// SPDX-License-Identifier: GPL-2.0
25792 +/*
25793 + * Copyright (C) 2005-2018 Junjiro R. Okajima
25794 + *
25795 + * This program, aufs is free software; you can redistribute it and/or modify
25796 + * it under the terms of the GNU General Public License as published by
25797 + * the Free Software Foundation; either version 2 of the License, or
25798 + * (at your option) any later version.
25799 + *
25800 + * This program is distributed in the hope that it will be useful,
25801 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25802 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25803 + * GNU General Public License for more details.
25804 + *
25805 + * You should have received a copy of the GNU General Public License
25806 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25807 + */
25808 +
25809 +/*
25810 + * mount options/flags
25811 + */
25812 +
25813 +#include <linux/namei.h>
25814 +#include <linux/types.h> /* a distribution requires */
25815 +#include <linux/parser.h>
25816 +#include "aufs.h"
25817 +
25818 +/* ---------------------------------------------------------------------- */
25819 +
25820 +enum {
25821 +       Opt_br,
25822 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25823 +       Opt_idel, Opt_imod,
25824 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25825 +       Opt_rdblk_def, Opt_rdhash_def,
25826 +       Opt_xino, Opt_noxino,
25827 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25828 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25829 +       Opt_trunc_xib, Opt_notrunc_xib,
25830 +       Opt_shwh, Opt_noshwh,
25831 +       Opt_plink, Opt_noplink, Opt_list_plink,
25832 +       Opt_udba,
25833 +       Opt_dio, Opt_nodio,
25834 +       Opt_diropq_a, Opt_diropq_w,
25835 +       Opt_warn_perm, Opt_nowarn_perm,
25836 +       Opt_wbr_copyup, Opt_wbr_create,
25837 +       Opt_fhsm_sec,
25838 +       Opt_verbose, Opt_noverbose,
25839 +       Opt_sum, Opt_nosum, Opt_wsum,
25840 +       Opt_dirperm1, Opt_nodirperm1,
25841 +       Opt_dirren, Opt_nodirren,
25842 +       Opt_acl, Opt_noacl,
25843 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25844 +};
25845 +
25846 +static match_table_t options = {
25847 +       {Opt_br, "br=%s"},
25848 +       {Opt_br, "br:%s"},
25849 +
25850 +       {Opt_add, "add=%d:%s"},
25851 +       {Opt_add, "add:%d:%s"},
25852 +       {Opt_add, "ins=%d:%s"},
25853 +       {Opt_add, "ins:%d:%s"},
25854 +       {Opt_append, "append=%s"},
25855 +       {Opt_append, "append:%s"},
25856 +       {Opt_prepend, "prepend=%s"},
25857 +       {Opt_prepend, "prepend:%s"},
25858 +
25859 +       {Opt_del, "del=%s"},
25860 +       {Opt_del, "del:%s"},
25861 +       /* {Opt_idel, "idel:%d"}, */
25862 +       {Opt_mod, "mod=%s"},
25863 +       {Opt_mod, "mod:%s"},
25864 +       /* {Opt_imod, "imod:%d:%s"}, */
25865 +
25866 +       {Opt_dirwh, "dirwh=%d"},
25867 +
25868 +       {Opt_xino, "xino=%s"},
25869 +       {Opt_noxino, "noxino"},
25870 +       {Opt_trunc_xino, "trunc_xino"},
25871 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25872 +       {Opt_notrunc_xino, "notrunc_xino"},
25873 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25874 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25875 +       /* {Opt_zxino, "zxino=%s"}, */
25876 +       {Opt_trunc_xib, "trunc_xib"},
25877 +       {Opt_notrunc_xib, "notrunc_xib"},
25878 +
25879 +#ifdef CONFIG_PROC_FS
25880 +       {Opt_plink, "plink"},
25881 +#else
25882 +       {Opt_ignore_silent, "plink"},
25883 +#endif
25884 +
25885 +       {Opt_noplink, "noplink"},
25886 +
25887 +#ifdef CONFIG_AUFS_DEBUG
25888 +       {Opt_list_plink, "list_plink"},
25889 +#endif
25890 +
25891 +       {Opt_udba, "udba=%s"},
25892 +
25893 +       {Opt_dio, "dio"},
25894 +       {Opt_nodio, "nodio"},
25895 +
25896 +#ifdef CONFIG_AUFS_DIRREN
25897 +       {Opt_dirren, "dirren"},
25898 +       {Opt_nodirren, "nodirren"},
25899 +#else
25900 +       {Opt_ignore, "dirren"},
25901 +       {Opt_ignore_silent, "nodirren"},
25902 +#endif
25903 +
25904 +#ifdef CONFIG_AUFS_FHSM
25905 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25906 +#else
25907 +       {Opt_ignore, "fhsm_sec=%d"},
25908 +#endif
25909 +
25910 +       {Opt_diropq_a, "diropq=always"},
25911 +       {Opt_diropq_a, "diropq=a"},
25912 +       {Opt_diropq_w, "diropq=whiteouted"},
25913 +       {Opt_diropq_w, "diropq=w"},
25914 +
25915 +       {Opt_warn_perm, "warn_perm"},
25916 +       {Opt_nowarn_perm, "nowarn_perm"},
25917 +
25918 +       /* keep them temporary */
25919 +       {Opt_ignore_silent, "nodlgt"},
25920 +       {Opt_ignore, "clean_plink"},
25921 +
25922 +#ifdef CONFIG_AUFS_SHWH
25923 +       {Opt_shwh, "shwh"},
25924 +#endif
25925 +       {Opt_noshwh, "noshwh"},
25926 +
25927 +       {Opt_dirperm1, "dirperm1"},
25928 +       {Opt_nodirperm1, "nodirperm1"},
25929 +
25930 +       {Opt_verbose, "verbose"},
25931 +       {Opt_verbose, "v"},
25932 +       {Opt_noverbose, "noverbose"},
25933 +       {Opt_noverbose, "quiet"},
25934 +       {Opt_noverbose, "q"},
25935 +       {Opt_noverbose, "silent"},
25936 +
25937 +       {Opt_sum, "sum"},
25938 +       {Opt_nosum, "nosum"},
25939 +       {Opt_wsum, "wsum"},
25940 +
25941 +       {Opt_rdcache, "rdcache=%d"},
25942 +       {Opt_rdblk, "rdblk=%d"},
25943 +       {Opt_rdblk_def, "rdblk=def"},
25944 +       {Opt_rdhash, "rdhash=%d"},
25945 +       {Opt_rdhash_def, "rdhash=def"},
25946 +
25947 +       {Opt_wbr_create, "create=%s"},
25948 +       {Opt_wbr_create, "create_policy=%s"},
25949 +       {Opt_wbr_copyup, "cpup=%s"},
25950 +       {Opt_wbr_copyup, "copyup=%s"},
25951 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25952 +
25953 +       /* generic VFS flag */
25954 +#ifdef CONFIG_FS_POSIX_ACL
25955 +       {Opt_acl, "acl"},
25956 +       {Opt_noacl, "noacl"},
25957 +#else
25958 +       {Opt_ignore, "acl"},
25959 +       {Opt_ignore_silent, "noacl"},
25960 +#endif
25961 +
25962 +       /* internal use for the scripts */
25963 +       {Opt_ignore_silent, "si=%s"},
25964 +
25965 +       {Opt_br, "dirs=%s"},
25966 +       {Opt_ignore, "debug=%d"},
25967 +       {Opt_ignore, "delete=whiteout"},
25968 +       {Opt_ignore, "delete=all"},
25969 +       {Opt_ignore, "imap=%s"},
25970 +
25971 +       /* temporary workaround, due to old mount(8)? */
25972 +       {Opt_ignore_silent, "relatime"},
25973 +
25974 +       {Opt_err, NULL}
25975 +};
25976 +
25977 +/* ---------------------------------------------------------------------- */
25978 +
25979 +static const char *au_parser_pattern(int val, match_table_t tbl)
25980 +{
25981 +       struct match_token *p;
25982 +
25983 +       p = tbl;
25984 +       while (p->pattern) {
25985 +               if (p->token == val)
25986 +                       return p->pattern;
25987 +               p++;
25988 +       }
25989 +       BUG();
25990 +       return "??";
25991 +}
25992 +
25993 +static const char *au_optstr(int *val, match_table_t tbl)
25994 +{
25995 +       struct match_token *p;
25996 +       int v;
25997 +
25998 +       v = *val;
25999 +       if (!v)
26000 +               goto out;
26001 +       p = tbl;
26002 +       while (p->pattern) {
26003 +               if (p->token
26004 +                   && (v & p->token) == p->token) {
26005 +                       *val &= ~p->token;
26006 +                       return p->pattern;
26007 +               }
26008 +               p++;
26009 +       }
26010 +
26011 +out:
26012 +       return NULL;
26013 +}
26014 +
26015 +/* ---------------------------------------------------------------------- */
26016 +
26017 +static match_table_t brperm = {
26018 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26019 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26020 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26021 +       {0, NULL}
26022 +};
26023 +
26024 +static match_table_t brattr = {
26025 +       /* general */
26026 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26027 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26028 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26029 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26030 +#ifdef CONFIG_AUFS_FHSM
26031 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26032 +#endif
26033 +#ifdef CONFIG_AUFS_XATTR
26034 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26035 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26036 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26037 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26038 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26039 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26040 +#endif
26041 +
26042 +       /* ro/rr branch */
26043 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26044 +
26045 +       /* rw branch */
26046 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26047 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26048 +
26049 +       {0, NULL}
26050 +};
26051 +
26052 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26053 +{
26054 +       int attr, v;
26055 +       char *p;
26056 +
26057 +       attr = 0;
26058 +       do {
26059 +               p = strchr(str, '+');
26060 +               if (p)
26061 +                       *p = 0;
26062 +               v = match_token(str, table, args);
26063 +               if (v) {
26064 +                       if (v & AuBrAttr_CMOO_Mask)
26065 +                               attr &= ~AuBrAttr_CMOO_Mask;
26066 +                       attr |= v;
26067 +               } else {
26068 +                       if (p)
26069 +                               *p = '+';
26070 +                       pr_warn("ignored branch attribute %s\n", str);
26071 +                       break;
26072 +               }
26073 +               if (p)
26074 +                       str = p + 1;
26075 +       } while (p);
26076 +
26077 +       return attr;
26078 +}
26079 +
26080 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26081 +{
26082 +       int sz;
26083 +       const char *p;
26084 +       char *q;
26085 +
26086 +       q = str->a;
26087 +       *q = 0;
26088 +       p = au_optstr(&perm, brattr);
26089 +       if (p) {
26090 +               sz = strlen(p);
26091 +               memcpy(q, p, sz + 1);
26092 +               q += sz;
26093 +       } else
26094 +               goto out;
26095 +
26096 +       do {
26097 +               p = au_optstr(&perm, brattr);
26098 +               if (p) {
26099 +                       *q++ = '+';
26100 +                       sz = strlen(p);
26101 +                       memcpy(q, p, sz + 1);
26102 +                       q += sz;
26103 +               }
26104 +       } while (p);
26105 +
26106 +out:
26107 +       return q - str->a;
26108 +}
26109 +
26110 +static int noinline_for_stack br_perm_val(char *perm)
26111 +{
26112 +       int val, bad, sz;
26113 +       char *p;
26114 +       substring_t args[MAX_OPT_ARGS];
26115 +       au_br_perm_str_t attr;
26116 +
26117 +       p = strchr(perm, '+');
26118 +       if (p)
26119 +               *p = 0;
26120 +       val = match_token(perm, brperm, args);
26121 +       if (!val) {
26122 +               if (p)
26123 +                       *p = '+';
26124 +               pr_warn("ignored branch permission %s\n", perm);
26125 +               val = AuBrPerm_RO;
26126 +               goto out;
26127 +       }
26128 +       if (!p)
26129 +               goto out;
26130 +
26131 +       val |= br_attr_val(p + 1, brattr, args);
26132 +
26133 +       bad = 0;
26134 +       switch (val & AuBrPerm_Mask) {
26135 +       case AuBrPerm_RO:
26136 +       case AuBrPerm_RR:
26137 +               bad = val & AuBrWAttr_Mask;
26138 +               val &= ~AuBrWAttr_Mask;
26139 +               break;
26140 +       case AuBrPerm_RW:
26141 +               bad = val & AuBrRAttr_Mask;
26142 +               val &= ~AuBrRAttr_Mask;
26143 +               break;
26144 +       }
26145 +
26146 +       /*
26147 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26148 +        * does not treat it as an error, just warning.
26149 +        * this is a tiny guard for the user operation.
26150 +        */
26151 +       if (val & AuBrAttr_UNPIN) {
26152 +               bad |= AuBrAttr_UNPIN;
26153 +               val &= ~AuBrAttr_UNPIN;
26154 +       }
26155 +
26156 +       if (unlikely(bad)) {
26157 +               sz = au_do_optstr_br_attr(&attr, bad);
26158 +               AuDebugOn(!sz);
26159 +               pr_warn("ignored branch attribute %s\n", attr.a);
26160 +       }
26161 +
26162 +out:
26163 +       return val;
26164 +}
26165 +
26166 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26167 +{
26168 +       au_br_perm_str_t attr;
26169 +       const char *p;
26170 +       char *q;
26171 +       int sz;
26172 +
26173 +       q = str->a;
26174 +       p = au_optstr(&perm, brperm);
26175 +       AuDebugOn(!p || !*p);
26176 +       sz = strlen(p);
26177 +       memcpy(q, p, sz + 1);
26178 +       q += sz;
26179 +
26180 +       sz = au_do_optstr_br_attr(&attr, perm);
26181 +       if (sz) {
26182 +               *q++ = '+';
26183 +               memcpy(q, attr.a, sz + 1);
26184 +       }
26185 +
26186 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26187 +}
26188 +
26189 +/* ---------------------------------------------------------------------- */
26190 +
26191 +static match_table_t udbalevel = {
26192 +       {AuOpt_UDBA_REVAL, "reval"},
26193 +       {AuOpt_UDBA_NONE, "none"},
26194 +#ifdef CONFIG_AUFS_HNOTIFY
26195 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26196 +#ifdef CONFIG_AUFS_HFSNOTIFY
26197 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26198 +#endif
26199 +#endif
26200 +       {-1, NULL}
26201 +};
26202 +
26203 +static int noinline_for_stack udba_val(char *str)
26204 +{
26205 +       substring_t args[MAX_OPT_ARGS];
26206 +
26207 +       return match_token(str, udbalevel, args);
26208 +}
26209 +
26210 +const char *au_optstr_udba(int udba)
26211 +{
26212 +       return au_parser_pattern(udba, udbalevel);
26213 +}
26214 +
26215 +/* ---------------------------------------------------------------------- */
26216 +
26217 +static match_table_t au_wbr_create_policy = {
26218 +       {AuWbrCreate_TDP, "tdp"},
26219 +       {AuWbrCreate_TDP, "top-down-parent"},
26220 +       {AuWbrCreate_RR, "rr"},
26221 +       {AuWbrCreate_RR, "round-robin"},
26222 +       {AuWbrCreate_MFS, "mfs"},
26223 +       {AuWbrCreate_MFS, "most-free-space"},
26224 +       {AuWbrCreate_MFSV, "mfs:%d"},
26225 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26226 +
26227 +       /* top-down regardless the parent, and then mfs */
26228 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26229 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26230 +
26231 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26232 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26233 +       {AuWbrCreate_PMFS, "pmfs"},
26234 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26235 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26236 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26237 +
26238 +       {-1, NULL}
26239 +};
26240 +
26241 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26242 +                           struct au_opt_wbr_create *create)
26243 +{
26244 +       int err;
26245 +       unsigned long long ull;
26246 +
26247 +       err = 0;
26248 +       if (!match_u64(arg, &ull))
26249 +               create->mfsrr_watermark = ull;
26250 +       else {
26251 +               pr_err("bad integer in %s\n", str);
26252 +               err = -EINVAL;
26253 +       }
26254 +
26255 +       return err;
26256 +}
26257 +
26258 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26259 +                         struct au_opt_wbr_create *create)
26260 +{
26261 +       int n, err;
26262 +
26263 +       err = 0;
26264 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26265 +               create->mfs_second = n;
26266 +       else {
26267 +               pr_err("bad integer in %s\n", str);
26268 +               err = -EINVAL;
26269 +       }
26270 +
26271 +       return err;
26272 +}
26273 +
26274 +static int noinline_for_stack
26275 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26276 +{
26277 +       int err, e;
26278 +       substring_t args[MAX_OPT_ARGS];
26279 +
26280 +       err = match_token(str, au_wbr_create_policy, args);
26281 +       create->wbr_create = err;
26282 +       switch (err) {
26283 +       case AuWbrCreate_MFSRRV:
26284 +       case AuWbrCreate_TDMFSV:
26285 +       case AuWbrCreate_PMFSRRV:
26286 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26287 +               if (!e)
26288 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26289 +               if (unlikely(e))
26290 +                       err = e;
26291 +               break;
26292 +       case AuWbrCreate_MFSRR:
26293 +       case AuWbrCreate_TDMFS:
26294 +       case AuWbrCreate_PMFSRR:
26295 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26296 +               if (unlikely(e)) {
26297 +                       err = e;
26298 +                       break;
26299 +               }
26300 +               /*FALLTHROUGH*/
26301 +       case AuWbrCreate_MFS:
26302 +       case AuWbrCreate_PMFS:
26303 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26304 +               break;
26305 +       case AuWbrCreate_MFSV:
26306 +       case AuWbrCreate_PMFSV:
26307 +               e = au_wbr_mfs_sec(&args[0], str, create);
26308 +               if (unlikely(e))
26309 +                       err = e;
26310 +               break;
26311 +       }
26312 +
26313 +       return err;
26314 +}
26315 +
26316 +const char *au_optstr_wbr_create(int wbr_create)
26317 +{
26318 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26319 +}
26320 +
26321 +static match_table_t au_wbr_copyup_policy = {
26322 +       {AuWbrCopyup_TDP, "tdp"},
26323 +       {AuWbrCopyup_TDP, "top-down-parent"},
26324 +       {AuWbrCopyup_BUP, "bup"},
26325 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26326 +       {AuWbrCopyup_BU, "bu"},
26327 +       {AuWbrCopyup_BU, "bottom-up"},
26328 +       {-1, NULL}
26329 +};
26330 +
26331 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26332 +{
26333 +       substring_t args[MAX_OPT_ARGS];
26334 +
26335 +       return match_token(str, au_wbr_copyup_policy, args);
26336 +}
26337 +
26338 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26339 +{
26340 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26341 +}
26342 +
26343 +/* ---------------------------------------------------------------------- */
26344 +
26345 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26346 +
26347 +static void dump_opts(struct au_opts *opts)
26348 +{
26349 +#ifdef CONFIG_AUFS_DEBUG
26350 +       /* reduce stack space */
26351 +       union {
26352 +               struct au_opt_add *add;
26353 +               struct au_opt_del *del;
26354 +               struct au_opt_mod *mod;
26355 +               struct au_opt_xino *xino;
26356 +               struct au_opt_xino_itrunc *xino_itrunc;
26357 +               struct au_opt_wbr_create *create;
26358 +       } u;
26359 +       struct au_opt *opt;
26360 +
26361 +       opt = opts->opt;
26362 +       while (opt->type != Opt_tail) {
26363 +               switch (opt->type) {
26364 +               case Opt_add:
26365 +                       u.add = &opt->add;
26366 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26367 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26368 +                                 u.add->path.dentry);
26369 +                       break;
26370 +               case Opt_del:
26371 +               case Opt_idel:
26372 +                       u.del = &opt->del;
26373 +                       AuDbg("del {%s, %p}\n",
26374 +                             u.del->pathname, u.del->h_path.dentry);
26375 +                       break;
26376 +               case Opt_mod:
26377 +               case Opt_imod:
26378 +                       u.mod = &opt->mod;
26379 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26380 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26381 +                       break;
26382 +               case Opt_append:
26383 +                       u.add = &opt->add;
26384 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26385 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26386 +                                 u.add->path.dentry);
26387 +                       break;
26388 +               case Opt_prepend:
26389 +                       u.add = &opt->add;
26390 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26391 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26392 +                                 u.add->path.dentry);
26393 +                       break;
26394 +               case Opt_dirwh:
26395 +                       AuDbg("dirwh %d\n", opt->dirwh);
26396 +                       break;
26397 +               case Opt_rdcache:
26398 +                       AuDbg("rdcache %d\n", opt->rdcache);
26399 +                       break;
26400 +               case Opt_rdblk:
26401 +                       AuDbg("rdblk %u\n", opt->rdblk);
26402 +                       break;
26403 +               case Opt_rdblk_def:
26404 +                       AuDbg("rdblk_def\n");
26405 +                       break;
26406 +               case Opt_rdhash:
26407 +                       AuDbg("rdhash %u\n", opt->rdhash);
26408 +                       break;
26409 +               case Opt_rdhash_def:
26410 +                       AuDbg("rdhash_def\n");
26411 +                       break;
26412 +               case Opt_xino:
26413 +                       u.xino = &opt->xino;
26414 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26415 +                       break;
26416 +               case Opt_trunc_xino:
26417 +                       AuLabel(trunc_xino);
26418 +                       break;
26419 +               case Opt_notrunc_xino:
26420 +                       AuLabel(notrunc_xino);
26421 +                       break;
26422 +               case Opt_trunc_xino_path:
26423 +               case Opt_itrunc_xino:
26424 +                       u.xino_itrunc = &opt->xino_itrunc;
26425 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26426 +                       break;
26427 +               case Opt_noxino:
26428 +                       AuLabel(noxino);
26429 +                       break;
26430 +               case Opt_trunc_xib:
26431 +                       AuLabel(trunc_xib);
26432 +                       break;
26433 +               case Opt_notrunc_xib:
26434 +                       AuLabel(notrunc_xib);
26435 +                       break;
26436 +               case Opt_shwh:
26437 +                       AuLabel(shwh);
26438 +                       break;
26439 +               case Opt_noshwh:
26440 +                       AuLabel(noshwh);
26441 +                       break;
26442 +               case Opt_dirperm1:
26443 +                       AuLabel(dirperm1);
26444 +                       break;
26445 +               case Opt_nodirperm1:
26446 +                       AuLabel(nodirperm1);
26447 +                       break;
26448 +               case Opt_plink:
26449 +                       AuLabel(plink);
26450 +                       break;
26451 +               case Opt_noplink:
26452 +                       AuLabel(noplink);
26453 +                       break;
26454 +               case Opt_list_plink:
26455 +                       AuLabel(list_plink);
26456 +                       break;
26457 +               case Opt_udba:
26458 +                       AuDbg("udba %d, %s\n",
26459 +                                 opt->udba, au_optstr_udba(opt->udba));
26460 +                       break;
26461 +               case Opt_dio:
26462 +                       AuLabel(dio);
26463 +                       break;
26464 +               case Opt_nodio:
26465 +                       AuLabel(nodio);
26466 +                       break;
26467 +               case Opt_diropq_a:
26468 +                       AuLabel(diropq_a);
26469 +                       break;
26470 +               case Opt_diropq_w:
26471 +                       AuLabel(diropq_w);
26472 +                       break;
26473 +               case Opt_warn_perm:
26474 +                       AuLabel(warn_perm);
26475 +                       break;
26476 +               case Opt_nowarn_perm:
26477 +                       AuLabel(nowarn_perm);
26478 +                       break;
26479 +               case Opt_verbose:
26480 +                       AuLabel(verbose);
26481 +                       break;
26482 +               case Opt_noverbose:
26483 +                       AuLabel(noverbose);
26484 +                       break;
26485 +               case Opt_sum:
26486 +                       AuLabel(sum);
26487 +                       break;
26488 +               case Opt_nosum:
26489 +                       AuLabel(nosum);
26490 +                       break;
26491 +               case Opt_wsum:
26492 +                       AuLabel(wsum);
26493 +                       break;
26494 +               case Opt_wbr_create:
26495 +                       u.create = &opt->wbr_create;
26496 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26497 +                                 au_optstr_wbr_create(u.create->wbr_create));
26498 +                       switch (u.create->wbr_create) {
26499 +                       case AuWbrCreate_MFSV:
26500 +                       case AuWbrCreate_PMFSV:
26501 +                               AuDbg("%d sec\n", u.create->mfs_second);
26502 +                               break;
26503 +                       case AuWbrCreate_MFSRR:
26504 +                       case AuWbrCreate_TDMFS:
26505 +                               AuDbg("%llu watermark\n",
26506 +                                         u.create->mfsrr_watermark);
26507 +                               break;
26508 +                       case AuWbrCreate_MFSRRV:
26509 +                       case AuWbrCreate_TDMFSV:
26510 +                       case AuWbrCreate_PMFSRRV:
26511 +                               AuDbg("%llu watermark, %d sec\n",
26512 +                                         u.create->mfsrr_watermark,
26513 +                                         u.create->mfs_second);
26514 +                               break;
26515 +                       }
26516 +                       break;
26517 +               case Opt_wbr_copyup:
26518 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26519 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26520 +                       break;
26521 +               case Opt_fhsm_sec:
26522 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26523 +                       break;
26524 +               case Opt_dirren:
26525 +                       AuLabel(dirren);
26526 +                       break;
26527 +               case Opt_nodirren:
26528 +                       AuLabel(nodirren);
26529 +                       break;
26530 +               case Opt_acl:
26531 +                       AuLabel(acl);
26532 +                       break;
26533 +               case Opt_noacl:
26534 +                       AuLabel(noacl);
26535 +                       break;
26536 +               default:
26537 +                       BUG();
26538 +               }
26539 +               opt++;
26540 +       }
26541 +#endif
26542 +}
26543 +
26544 +void au_opts_free(struct au_opts *opts)
26545 +{
26546 +       struct au_opt *opt;
26547 +
26548 +       opt = opts->opt;
26549 +       while (opt->type != Opt_tail) {
26550 +               switch (opt->type) {
26551 +               case Opt_add:
26552 +               case Opt_append:
26553 +               case Opt_prepend:
26554 +                       path_put(&opt->add.path);
26555 +                       break;
26556 +               case Opt_del:
26557 +               case Opt_idel:
26558 +                       path_put(&opt->del.h_path);
26559 +                       break;
26560 +               case Opt_mod:
26561 +               case Opt_imod:
26562 +                       dput(opt->mod.h_root);
26563 +                       break;
26564 +               case Opt_xino:
26565 +                       fput(opt->xino.file);
26566 +                       break;
26567 +               }
26568 +               opt++;
26569 +       }
26570 +}
26571 +
26572 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26573 +                  aufs_bindex_t bindex)
26574 +{
26575 +       int err;
26576 +       struct au_opt_add *add = &opt->add;
26577 +       char *p;
26578 +
26579 +       add->bindex = bindex;
26580 +       add->perm = AuBrPerm_RO;
26581 +       add->pathname = opt_str;
26582 +       p = strchr(opt_str, '=');
26583 +       if (p) {
26584 +               *p++ = 0;
26585 +               if (*p)
26586 +                       add->perm = br_perm_val(p);
26587 +       }
26588 +
26589 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26590 +       if (!err) {
26591 +               if (!p) {
26592 +                       add->perm = AuBrPerm_RO;
26593 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26594 +                               add->perm = AuBrPerm_RR;
26595 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26596 +                               add->perm = AuBrPerm_RW;
26597 +               }
26598 +               opt->type = Opt_add;
26599 +               goto out;
26600 +       }
26601 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26602 +       err = -EINVAL;
26603 +
26604 +out:
26605 +       return err;
26606 +}
26607 +
26608 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26609 +{
26610 +       int err;
26611 +
26612 +       del->pathname = args[0].from;
26613 +       AuDbg("del path %s\n", del->pathname);
26614 +
26615 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26616 +       if (unlikely(err))
26617 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26618 +
26619 +       return err;
26620 +}
26621 +
26622 +#if 0 /* reserved for future use */
26623 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26624 +                             struct au_opt_del *del, substring_t args[])
26625 +{
26626 +       int err;
26627 +       struct dentry *root;
26628 +
26629 +       err = -EINVAL;
26630 +       root = sb->s_root;
26631 +       aufs_read_lock(root, AuLock_FLUSH);
26632 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26633 +               pr_err("out of bounds, %d\n", bindex);
26634 +               goto out;
26635 +       }
26636 +
26637 +       err = 0;
26638 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26639 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26640 +
26641 +out:
26642 +       aufs_read_unlock(root, !AuLock_IR);
26643 +       return err;
26644 +}
26645 +#endif
26646 +
26647 +static int noinline_for_stack
26648 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26649 +{
26650 +       int err;
26651 +       struct path path;
26652 +       char *p;
26653 +
26654 +       err = -EINVAL;
26655 +       mod->path = args[0].from;
26656 +       p = strchr(mod->path, '=');
26657 +       if (unlikely(!p)) {
26658 +               pr_err("no permission %s\n", args[0].from);
26659 +               goto out;
26660 +       }
26661 +
26662 +       *p++ = 0;
26663 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26664 +       if (unlikely(err)) {
26665 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26666 +               goto out;
26667 +       }
26668 +
26669 +       mod->perm = br_perm_val(p);
26670 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26671 +       mod->h_root = dget(path.dentry);
26672 +       path_put(&path);
26673 +
26674 +out:
26675 +       return err;
26676 +}
26677 +
26678 +#if 0 /* reserved for future use */
26679 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26680 +                             struct au_opt_mod *mod, substring_t args[])
26681 +{
26682 +       int err;
26683 +       struct dentry *root;
26684 +
26685 +       err = -EINVAL;
26686 +       root = sb->s_root;
26687 +       aufs_read_lock(root, AuLock_FLUSH);
26688 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26689 +               pr_err("out of bounds, %d\n", bindex);
26690 +               goto out;
26691 +       }
26692 +
26693 +       err = 0;
26694 +       mod->perm = br_perm_val(args[1].from);
26695 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26696 +             mod->path, mod->perm, args[1].from);
26697 +       mod->h_root = dget(au_h_dptr(root, bindex));
26698 +
26699 +out:
26700 +       aufs_read_unlock(root, !AuLock_IR);
26701 +       return err;
26702 +}
26703 +#endif
26704 +
26705 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26706 +                             substring_t args[])
26707 +{
26708 +       int err;
26709 +       struct file *file;
26710 +
26711 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
26712 +       err = PTR_ERR(file);
26713 +       if (IS_ERR(file))
26714 +               goto out;
26715 +
26716 +       err = -EINVAL;
26717 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26718 +               fput(file);
26719 +               pr_err("%s must be outside\n", args[0].from);
26720 +               goto out;
26721 +       }
26722 +
26723 +       err = 0;
26724 +       xino->file = file;
26725 +       xino->path = args[0].from;
26726 +
26727 +out:
26728 +       return err;
26729 +}
26730 +
26731 +static int noinline_for_stack
26732 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26733 +                              struct au_opt_xino_itrunc *xino_itrunc,
26734 +                              substring_t args[])
26735 +{
26736 +       int err;
26737 +       aufs_bindex_t bbot, bindex;
26738 +       struct path path;
26739 +       struct dentry *root;
26740 +
26741 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26742 +       if (unlikely(err)) {
26743 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26744 +               goto out;
26745 +       }
26746 +
26747 +       xino_itrunc->bindex = -1;
26748 +       root = sb->s_root;
26749 +       aufs_read_lock(root, AuLock_FLUSH);
26750 +       bbot = au_sbbot(sb);
26751 +       for (bindex = 0; bindex <= bbot; bindex++) {
26752 +               if (au_h_dptr(root, bindex) == path.dentry) {
26753 +                       xino_itrunc->bindex = bindex;
26754 +                       break;
26755 +               }
26756 +       }
26757 +       aufs_read_unlock(root, !AuLock_IR);
26758 +       path_put(&path);
26759 +
26760 +       if (unlikely(xino_itrunc->bindex < 0)) {
26761 +               pr_err("no such branch %s\n", args[0].from);
26762 +               err = -EINVAL;
26763 +       }
26764 +
26765 +out:
26766 +       return err;
26767 +}
26768 +
26769 +/* called without aufs lock */
26770 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26771 +{
26772 +       int err, n, token;
26773 +       aufs_bindex_t bindex;
26774 +       unsigned char skipped;
26775 +       struct dentry *root;
26776 +       struct au_opt *opt, *opt_tail;
26777 +       char *opt_str;
26778 +       /* reduce the stack space */
26779 +       union {
26780 +               struct au_opt_xino_itrunc *xino_itrunc;
26781 +               struct au_opt_wbr_create *create;
26782 +       } u;
26783 +       struct {
26784 +               substring_t args[MAX_OPT_ARGS];
26785 +       } *a;
26786 +
26787 +       err = -ENOMEM;
26788 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26789 +       if (unlikely(!a))
26790 +               goto out;
26791 +
26792 +       root = sb->s_root;
26793 +       err = 0;
26794 +       bindex = 0;
26795 +       opt = opts->opt;
26796 +       opt_tail = opt + opts->max_opt - 1;
26797 +       opt->type = Opt_tail;
26798 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26799 +               err = -EINVAL;
26800 +               skipped = 0;
26801 +               token = match_token(opt_str, options, a->args);
26802 +               switch (token) {
26803 +               case Opt_br:
26804 +                       err = 0;
26805 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26806 +                              && *opt_str) {
26807 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26808 +                                             bindex++);
26809 +                               if (unlikely(!err && ++opt > opt_tail)) {
26810 +                                       err = -E2BIG;
26811 +                                       break;
26812 +                               }
26813 +                               opt->type = Opt_tail;
26814 +                               skipped = 1;
26815 +                       }
26816 +                       break;
26817 +               case Opt_add:
26818 +                       if (unlikely(match_int(&a->args[0], &n))) {
26819 +                               pr_err("bad integer in %s\n", opt_str);
26820 +                               break;
26821 +                       }
26822 +                       bindex = n;
26823 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26824 +                                     bindex);
26825 +                       if (!err)
26826 +                               opt->type = token;
26827 +                       break;
26828 +               case Opt_append:
26829 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26830 +                                     /*dummy bindex*/1);
26831 +                       if (!err)
26832 +                               opt->type = token;
26833 +                       break;
26834 +               case Opt_prepend:
26835 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26836 +                                     /*bindex*/0);
26837 +                       if (!err)
26838 +                               opt->type = token;
26839 +                       break;
26840 +               case Opt_del:
26841 +                       err = au_opts_parse_del(&opt->del, a->args);
26842 +                       if (!err)
26843 +                               opt->type = token;
26844 +                       break;
26845 +#if 0 /* reserved for future use */
26846 +               case Opt_idel:
26847 +                       del->pathname = "(indexed)";
26848 +                       if (unlikely(match_int(&args[0], &n))) {
26849 +                               pr_err("bad integer in %s\n", opt_str);
26850 +                               break;
26851 +                       }
26852 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26853 +                       if (!err)
26854 +                               opt->type = token;
26855 +                       break;
26856 +#endif
26857 +               case Opt_mod:
26858 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26859 +                       if (!err)
26860 +                               opt->type = token;
26861 +                       break;
26862 +#ifdef IMOD /* reserved for future use */
26863 +               case Opt_imod:
26864 +                       u.mod->path = "(indexed)";
26865 +                       if (unlikely(match_int(&a->args[0], &n))) {
26866 +                               pr_err("bad integer in %s\n", opt_str);
26867 +                               break;
26868 +                       }
26869 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26870 +                       if (!err)
26871 +                               opt->type = token;
26872 +                       break;
26873 +#endif
26874 +               case Opt_xino:
26875 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26876 +                       if (!err)
26877 +                               opt->type = token;
26878 +                       break;
26879 +
26880 +               case Opt_trunc_xino_path:
26881 +                       err = au_opts_parse_xino_itrunc_path
26882 +                               (sb, &opt->xino_itrunc, a->args);
26883 +                       if (!err)
26884 +                               opt->type = token;
26885 +                       break;
26886 +
26887 +               case Opt_itrunc_xino:
26888 +                       u.xino_itrunc = &opt->xino_itrunc;
26889 +                       if (unlikely(match_int(&a->args[0], &n))) {
26890 +                               pr_err("bad integer in %s\n", opt_str);
26891 +                               break;
26892 +                       }
26893 +                       u.xino_itrunc->bindex = n;
26894 +                       aufs_read_lock(root, AuLock_FLUSH);
26895 +                       if (n < 0 || au_sbbot(sb) < n) {
26896 +                               pr_err("out of bounds, %d\n", n);
26897 +                               aufs_read_unlock(root, !AuLock_IR);
26898 +                               break;
26899 +                       }
26900 +                       aufs_read_unlock(root, !AuLock_IR);
26901 +                       err = 0;
26902 +                       opt->type = token;
26903 +                       break;
26904 +
26905 +               case Opt_dirwh:
26906 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26907 +                               break;
26908 +                       err = 0;
26909 +                       opt->type = token;
26910 +                       break;
26911 +
26912 +               case Opt_rdcache:
26913 +                       if (unlikely(match_int(&a->args[0], &n))) {
26914 +                               pr_err("bad integer in %s\n", opt_str);
26915 +                               break;
26916 +                       }
26917 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26918 +                               pr_err("rdcache must be smaller than %d\n",
26919 +                                      AUFS_RDCACHE_MAX);
26920 +                               break;
26921 +                       }
26922 +                       opt->rdcache = n;
26923 +                       err = 0;
26924 +                       opt->type = token;
26925 +                       break;
26926 +               case Opt_rdblk:
26927 +                       if (unlikely(match_int(&a->args[0], &n)
26928 +                                    || n < 0
26929 +                                    || n > KMALLOC_MAX_SIZE)) {
26930 +                               pr_err("bad integer in %s\n", opt_str);
26931 +                               break;
26932 +                       }
26933 +                       if (unlikely(n && n < NAME_MAX)) {
26934 +                               pr_err("rdblk must be larger than %d\n",
26935 +                                      NAME_MAX);
26936 +                               break;
26937 +                       }
26938 +                       opt->rdblk = n;
26939 +                       err = 0;
26940 +                       opt->type = token;
26941 +                       break;
26942 +               case Opt_rdhash:
26943 +                       if (unlikely(match_int(&a->args[0], &n)
26944 +                                    || n < 0
26945 +                                    || n * sizeof(struct hlist_head)
26946 +                                    > KMALLOC_MAX_SIZE)) {
26947 +                               pr_err("bad integer in %s\n", opt_str);
26948 +                               break;
26949 +                       }
26950 +                       opt->rdhash = n;
26951 +                       err = 0;
26952 +                       opt->type = token;
26953 +                       break;
26954 +
26955 +               case Opt_trunc_xino:
26956 +               case Opt_notrunc_xino:
26957 +               case Opt_noxino:
26958 +               case Opt_trunc_xib:
26959 +               case Opt_notrunc_xib:
26960 +               case Opt_shwh:
26961 +               case Opt_noshwh:
26962 +               case Opt_dirperm1:
26963 +               case Opt_nodirperm1:
26964 +               case Opt_plink:
26965 +               case Opt_noplink:
26966 +               case Opt_list_plink:
26967 +               case Opt_dio:
26968 +               case Opt_nodio:
26969 +               case Opt_diropq_a:
26970 +               case Opt_diropq_w:
26971 +               case Opt_warn_perm:
26972 +               case Opt_nowarn_perm:
26973 +               case Opt_verbose:
26974 +               case Opt_noverbose:
26975 +               case Opt_sum:
26976 +               case Opt_nosum:
26977 +               case Opt_wsum:
26978 +               case Opt_rdblk_def:
26979 +               case Opt_rdhash_def:
26980 +               case Opt_dirren:
26981 +               case Opt_nodirren:
26982 +               case Opt_acl:
26983 +               case Opt_noacl:
26984 +                       err = 0;
26985 +                       opt->type = token;
26986 +                       break;
26987 +
26988 +               case Opt_udba:
26989 +                       opt->udba = udba_val(a->args[0].from);
26990 +                       if (opt->udba >= 0) {
26991 +                               err = 0;
26992 +                               opt->type = token;
26993 +                       } else
26994 +                               pr_err("wrong value, %s\n", opt_str);
26995 +                       break;
26996 +
26997 +               case Opt_wbr_create:
26998 +                       u.create = &opt->wbr_create;
26999 +                       u.create->wbr_create
27000 +                               = au_wbr_create_val(a->args[0].from, u.create);
27001 +                       if (u.create->wbr_create >= 0) {
27002 +                               err = 0;
27003 +                               opt->type = token;
27004 +                       } else
27005 +                               pr_err("wrong value, %s\n", opt_str);
27006 +                       break;
27007 +               case Opt_wbr_copyup:
27008 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
27009 +                       if (opt->wbr_copyup >= 0) {
27010 +                               err = 0;
27011 +                               opt->type = token;
27012 +                       } else
27013 +                               pr_err("wrong value, %s\n", opt_str);
27014 +                       break;
27015 +
27016 +               case Opt_fhsm_sec:
27017 +                       if (unlikely(match_int(&a->args[0], &n)
27018 +                                    || n < 0)) {
27019 +                               pr_err("bad integer in %s\n", opt_str);
27020 +                               break;
27021 +                       }
27022 +                       if (sysaufs_brs) {
27023 +                               opt->fhsm_second = n;
27024 +                               opt->type = token;
27025 +                       } else
27026 +                               pr_warn("ignored %s\n", opt_str);
27027 +                       err = 0;
27028 +                       break;
27029 +
27030 +               case Opt_ignore:
27031 +                       pr_warn("ignored %s\n", opt_str);
27032 +                       /*FALLTHROUGH*/
27033 +               case Opt_ignore_silent:
27034 +                       skipped = 1;
27035 +                       err = 0;
27036 +                       break;
27037 +               case Opt_err:
27038 +                       pr_err("unknown option %s\n", opt_str);
27039 +                       break;
27040 +               }
27041 +
27042 +               if (!err && !skipped) {
27043 +                       if (unlikely(++opt > opt_tail)) {
27044 +                               err = -E2BIG;
27045 +                               opt--;
27046 +                               opt->type = Opt_tail;
27047 +                               break;
27048 +                       }
27049 +                       opt->type = Opt_tail;
27050 +               }
27051 +       }
27052 +
27053 +       au_kfree_rcu(a);
27054 +       dump_opts(opts);
27055 +       if (unlikely(err))
27056 +               au_opts_free(opts);
27057 +
27058 +out:
27059 +       return err;
27060 +}
27061 +
27062 +static int au_opt_wbr_create(struct super_block *sb,
27063 +                            struct au_opt_wbr_create *create)
27064 +{
27065 +       int err;
27066 +       struct au_sbinfo *sbinfo;
27067 +
27068 +       SiMustWriteLock(sb);
27069 +
27070 +       err = 1; /* handled */
27071 +       sbinfo = au_sbi(sb);
27072 +       if (sbinfo->si_wbr_create_ops->fin) {
27073 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27074 +               if (!err)
27075 +                       err = 1;
27076 +       }
27077 +
27078 +       sbinfo->si_wbr_create = create->wbr_create;
27079 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27080 +       switch (create->wbr_create) {
27081 +       case AuWbrCreate_MFSRRV:
27082 +       case AuWbrCreate_MFSRR:
27083 +       case AuWbrCreate_TDMFS:
27084 +       case AuWbrCreate_TDMFSV:
27085 +       case AuWbrCreate_PMFSRR:
27086 +       case AuWbrCreate_PMFSRRV:
27087 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27088 +               /*FALLTHROUGH*/
27089 +       case AuWbrCreate_MFS:
27090 +       case AuWbrCreate_MFSV:
27091 +       case AuWbrCreate_PMFS:
27092 +       case AuWbrCreate_PMFSV:
27093 +               sbinfo->si_wbr_mfs.mfs_expire
27094 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27095 +               break;
27096 +       }
27097 +
27098 +       if (sbinfo->si_wbr_create_ops->init)
27099 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27100 +
27101 +       return err;
27102 +}
27103 +
27104 +/*
27105 + * returns,
27106 + * plus: processed without an error
27107 + * zero: unprocessed
27108 + */
27109 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27110 +                        struct au_opts *opts)
27111 +{
27112 +       int err;
27113 +       struct au_sbinfo *sbinfo;
27114 +
27115 +       SiMustWriteLock(sb);
27116 +
27117 +       err = 1; /* handled */
27118 +       sbinfo = au_sbi(sb);
27119 +       switch (opt->type) {
27120 +       case Opt_udba:
27121 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27122 +               sbinfo->si_mntflags |= opt->udba;
27123 +               opts->given_udba |= opt->udba;
27124 +               break;
27125 +
27126 +       case Opt_plink:
27127 +               au_opt_set(sbinfo->si_mntflags, PLINK);
27128 +               break;
27129 +       case Opt_noplink:
27130 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27131 +                       au_plink_put(sb, /*verbose*/1);
27132 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
27133 +               break;
27134 +       case Opt_list_plink:
27135 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27136 +                       au_plink_list(sb);
27137 +               break;
27138 +
27139 +       case Opt_dio:
27140 +               au_opt_set(sbinfo->si_mntflags, DIO);
27141 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27142 +               break;
27143 +       case Opt_nodio:
27144 +               au_opt_clr(sbinfo->si_mntflags, DIO);
27145 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27146 +               break;
27147 +
27148 +       case Opt_fhsm_sec:
27149 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27150 +               break;
27151 +
27152 +       case Opt_diropq_a:
27153 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27154 +               break;
27155 +       case Opt_diropq_w:
27156 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27157 +               break;
27158 +
27159 +       case Opt_warn_perm:
27160 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27161 +               break;
27162 +       case Opt_nowarn_perm:
27163 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27164 +               break;
27165 +
27166 +       case Opt_verbose:
27167 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27168 +               break;
27169 +       case Opt_noverbose:
27170 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27171 +               break;
27172 +
27173 +       case Opt_sum:
27174 +               au_opt_set(sbinfo->si_mntflags, SUM);
27175 +               break;
27176 +       case Opt_wsum:
27177 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27178 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27179 +       case Opt_nosum:
27180 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27181 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27182 +               break;
27183 +
27184 +       case Opt_wbr_create:
27185 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27186 +               break;
27187 +       case Opt_wbr_copyup:
27188 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27189 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27190 +               break;
27191 +
27192 +       case Opt_dirwh:
27193 +               sbinfo->si_dirwh = opt->dirwh;
27194 +               break;
27195 +
27196 +       case Opt_rdcache:
27197 +               sbinfo->si_rdcache
27198 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27199 +               break;
27200 +       case Opt_rdblk:
27201 +               sbinfo->si_rdblk = opt->rdblk;
27202 +               break;
27203 +       case Opt_rdblk_def:
27204 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27205 +               break;
27206 +       case Opt_rdhash:
27207 +               sbinfo->si_rdhash = opt->rdhash;
27208 +               break;
27209 +       case Opt_rdhash_def:
27210 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27211 +               break;
27212 +
27213 +       case Opt_shwh:
27214 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27215 +               break;
27216 +       case Opt_noshwh:
27217 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27218 +               break;
27219 +
27220 +       case Opt_dirperm1:
27221 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27222 +               break;
27223 +       case Opt_nodirperm1:
27224 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27225 +               break;
27226 +
27227 +       case Opt_trunc_xino:
27228 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27229 +               break;
27230 +       case Opt_notrunc_xino:
27231 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27232 +               break;
27233 +
27234 +       case Opt_trunc_xino_path:
27235 +       case Opt_itrunc_xino:
27236 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27237 +                                   /*idx_begin*/0);
27238 +               if (!err)
27239 +                       err = 1;
27240 +               break;
27241 +
27242 +       case Opt_trunc_xib:
27243 +               au_fset_opts(opts->flags, TRUNC_XIB);
27244 +               break;
27245 +       case Opt_notrunc_xib:
27246 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27247 +               break;
27248 +
27249 +       case Opt_dirren:
27250 +               err = 1;
27251 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27252 +                       err = au_dr_opt_set(sb);
27253 +                       if (!err)
27254 +                               err = 1;
27255 +               }
27256 +               if (err == 1)
27257 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27258 +               break;
27259 +       case Opt_nodirren:
27260 +               err = 1;
27261 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27262 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27263 +                                                             DR_FLUSHED));
27264 +                       if (!err)
27265 +                               err = 1;
27266 +               }
27267 +               if (err == 1)
27268 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27269 +               break;
27270 +
27271 +       case Opt_acl:
27272 +               sb->s_flags |= SB_POSIXACL;
27273 +               break;
27274 +       case Opt_noacl:
27275 +               sb->s_flags &= ~SB_POSIXACL;
27276 +               break;
27277 +
27278 +       default:
27279 +               err = 0;
27280 +               break;
27281 +       }
27282 +
27283 +       return err;
27284 +}
27285 +
27286 +/*
27287 + * returns tri-state.
27288 + * plus: processed without an error
27289 + * zero: unprocessed
27290 + * minus: error
27291 + */
27292 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27293 +                    struct au_opts *opts)
27294 +{
27295 +       int err, do_refresh;
27296 +
27297 +       err = 0;
27298 +       switch (opt->type) {
27299 +       case Opt_append:
27300 +               opt->add.bindex = au_sbbot(sb) + 1;
27301 +               if (opt->add.bindex < 0)
27302 +                       opt->add.bindex = 0;
27303 +               goto add;
27304 +       case Opt_prepend:
27305 +               opt->add.bindex = 0;
27306 +       add: /* indented label */
27307 +       case Opt_add:
27308 +               err = au_br_add(sb, &opt->add,
27309 +                               au_ftest_opts(opts->flags, REMOUNT));
27310 +               if (!err) {
27311 +                       err = 1;
27312 +                       au_fset_opts(opts->flags, REFRESH);
27313 +               }
27314 +               break;
27315 +
27316 +       case Opt_del:
27317 +       case Opt_idel:
27318 +               err = au_br_del(sb, &opt->del,
27319 +                               au_ftest_opts(opts->flags, REMOUNT));
27320 +               if (!err) {
27321 +                       err = 1;
27322 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27323 +                       au_fset_opts(opts->flags, REFRESH);
27324 +               }
27325 +               break;
27326 +
27327 +       case Opt_mod:
27328 +       case Opt_imod:
27329 +               err = au_br_mod(sb, &opt->mod,
27330 +                               au_ftest_opts(opts->flags, REMOUNT),
27331 +                               &do_refresh);
27332 +               if (!err) {
27333 +                       err = 1;
27334 +                       if (do_refresh)
27335 +                               au_fset_opts(opts->flags, REFRESH);
27336 +               }
27337 +               break;
27338 +       }
27339 +       return err;
27340 +}
27341 +
27342 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27343 +                      struct au_opt_xino **opt_xino,
27344 +                      struct au_opts *opts)
27345 +{
27346 +       int err;
27347 +
27348 +       err = 0;
27349 +       switch (opt->type) {
27350 +       case Opt_xino:
27351 +               err = au_xino_set(sb, &opt->xino,
27352 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27353 +               if (unlikely(err))
27354 +                       break;
27355 +
27356 +               *opt_xino = &opt->xino;
27357 +               break;
27358 +
27359 +       case Opt_noxino:
27360 +               au_xino_clr(sb);
27361 +               *opt_xino = (void *)-1;
27362 +               break;
27363 +       }
27364 +
27365 +       return err;
27366 +}
27367 +
27368 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27369 +                  unsigned int pending)
27370 +{
27371 +       int err, fhsm;
27372 +       aufs_bindex_t bindex, bbot;
27373 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27374 +       struct au_branch *br;
27375 +       struct au_wbr *wbr;
27376 +       struct dentry *root, *dentry;
27377 +       struct inode *dir, *h_dir;
27378 +       struct au_sbinfo *sbinfo;
27379 +       struct au_hinode *hdir;
27380 +
27381 +       SiMustAnyLock(sb);
27382 +
27383 +       sbinfo = au_sbi(sb);
27384 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27385 +
27386 +       if (!(sb_flags & SB_RDONLY)) {
27387 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27388 +                       pr_warn("first branch should be rw\n");
27389 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27390 +                       pr_warn_once("shwh should be used with ro\n");
27391 +       }
27392 +
27393 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27394 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27395 +               pr_warn_once("udba=*notify requires xino\n");
27396 +
27397 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27398 +               pr_warn_once("dirperm1 breaks the protection"
27399 +                            " by the permission bits on the lower branch\n");
27400 +
27401 +       err = 0;
27402 +       fhsm = 0;
27403 +       root = sb->s_root;
27404 +       dir = d_inode(root);
27405 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27406 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27407 +                                     UDBA_NONE);
27408 +       bbot = au_sbbot(sb);
27409 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27410 +               skip = 0;
27411 +               h_dir = au_h_iptr(dir, bindex);
27412 +               br = au_sbr(sb, bindex);
27413 +
27414 +               if ((br->br_perm & AuBrAttr_ICEX)
27415 +                   && !h_dir->i_op->listxattr)
27416 +                       br->br_perm &= ~AuBrAttr_ICEX;
27417 +#if 0
27418 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27419 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27420 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27421 +#endif
27422 +
27423 +               do_free = 0;
27424 +               wbr = br->br_wbr;
27425 +               if (wbr)
27426 +                       wbr_wh_read_lock(wbr);
27427 +
27428 +               if (!au_br_writable(br->br_perm)) {
27429 +                       do_free = !!wbr;
27430 +                       skip = (!wbr
27431 +                               || (!wbr->wbr_whbase
27432 +                                   && !wbr->wbr_plink
27433 +                                   && !wbr->wbr_orph));
27434 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27435 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27436 +                       skip = (!wbr || !wbr->wbr_whbase);
27437 +                       if (skip && wbr) {
27438 +                               if (do_plink)
27439 +                                       skip = !!wbr->wbr_plink;
27440 +                               else
27441 +                                       skip = !wbr->wbr_plink;
27442 +                       }
27443 +               } else {
27444 +                       /* skip = (br->br_whbase && br->br_ohph); */
27445 +                       skip = (wbr && wbr->wbr_whbase);
27446 +                       if (skip) {
27447 +                               if (do_plink)
27448 +                                       skip = !!wbr->wbr_plink;
27449 +                               else
27450 +                                       skip = !wbr->wbr_plink;
27451 +                       }
27452 +               }
27453 +               if (wbr)
27454 +                       wbr_wh_read_unlock(wbr);
27455 +
27456 +               if (can_no_dreval) {
27457 +                       dentry = br->br_path.dentry;
27458 +                       spin_lock(&dentry->d_lock);
27459 +                       if (dentry->d_flags &
27460 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27461 +                               can_no_dreval = 0;
27462 +                       spin_unlock(&dentry->d_lock);
27463 +               }
27464 +
27465 +               if (au_br_fhsm(br->br_perm)) {
27466 +                       fhsm++;
27467 +                       AuDebugOn(!br->br_fhsm);
27468 +               }
27469 +
27470 +               if (skip)
27471 +                       continue;
27472 +
27473 +               hdir = au_hi(dir, bindex);
27474 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27475 +               if (wbr)
27476 +                       wbr_wh_write_lock(wbr);
27477 +               err = au_wh_init(br, sb);
27478 +               if (wbr)
27479 +                       wbr_wh_write_unlock(wbr);
27480 +               au_hn_inode_unlock(hdir);
27481 +
27482 +               if (!err && do_free) {
27483 +                       au_kfree_rcu(wbr);
27484 +                       br->br_wbr = NULL;
27485 +               }
27486 +       }
27487 +
27488 +       if (can_no_dreval)
27489 +               au_fset_si(sbinfo, NO_DREVAL);
27490 +       else
27491 +               au_fclr_si(sbinfo, NO_DREVAL);
27492 +
27493 +       if (fhsm >= 2) {
27494 +               au_fset_si(sbinfo, FHSM);
27495 +               for (bindex = bbot; bindex >= 0; bindex--) {
27496 +                       br = au_sbr(sb, bindex);
27497 +                       if (au_br_fhsm(br->br_perm)) {
27498 +                               au_fhsm_set_bottom(sb, bindex);
27499 +                               break;
27500 +                       }
27501 +               }
27502 +       } else {
27503 +               au_fclr_si(sbinfo, FHSM);
27504 +               au_fhsm_set_bottom(sb, -1);
27505 +       }
27506 +
27507 +       return err;
27508 +}
27509 +
27510 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27511 +{
27512 +       int err;
27513 +       unsigned int tmp;
27514 +       aufs_bindex_t bindex, bbot;
27515 +       struct au_opt *opt;
27516 +       struct au_opt_xino *opt_xino, xino;
27517 +       struct au_sbinfo *sbinfo;
27518 +       struct au_branch *br;
27519 +       struct inode *dir;
27520 +
27521 +       SiMustWriteLock(sb);
27522 +
27523 +       err = 0;
27524 +       opt_xino = NULL;
27525 +       opt = opts->opt;
27526 +       while (err >= 0 && opt->type != Opt_tail)
27527 +               err = au_opt_simple(sb, opt++, opts);
27528 +       if (err > 0)
27529 +               err = 0;
27530 +       else if (unlikely(err < 0))
27531 +               goto out;
27532 +
27533 +       /* disable xino and udba temporary */
27534 +       sbinfo = au_sbi(sb);
27535 +       tmp = sbinfo->si_mntflags;
27536 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27537 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27538 +
27539 +       opt = opts->opt;
27540 +       while (err >= 0 && opt->type != Opt_tail)
27541 +               err = au_opt_br(sb, opt++, opts);
27542 +       if (err > 0)
27543 +               err = 0;
27544 +       else if (unlikely(err < 0))
27545 +               goto out;
27546 +
27547 +       bbot = au_sbbot(sb);
27548 +       if (unlikely(bbot < 0)) {
27549 +               err = -EINVAL;
27550 +               pr_err("no branches\n");
27551 +               goto out;
27552 +       }
27553 +
27554 +       if (au_opt_test(tmp, XINO))
27555 +               au_opt_set(sbinfo->si_mntflags, XINO);
27556 +       opt = opts->opt;
27557 +       while (!err && opt->type != Opt_tail)
27558 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27559 +       if (unlikely(err))
27560 +               goto out;
27561 +
27562 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27563 +       if (unlikely(err))
27564 +               goto out;
27565 +
27566 +       /* restore xino */
27567 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27568 +               xino.file = au_xino_def(sb);
27569 +               err = PTR_ERR(xino.file);
27570 +               if (IS_ERR(xino.file))
27571 +                       goto out;
27572 +
27573 +               err = au_xino_set(sb, &xino, /*remount*/0);
27574 +               fput(xino.file);
27575 +               if (unlikely(err))
27576 +                       goto out;
27577 +       }
27578 +
27579 +       /* restore udba */
27580 +       tmp &= AuOptMask_UDBA;
27581 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27582 +       sbinfo->si_mntflags |= tmp;
27583 +       bbot = au_sbbot(sb);
27584 +       for (bindex = 0; bindex <= bbot; bindex++) {
27585 +               br = au_sbr(sb, bindex);
27586 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27587 +               if (unlikely(err))
27588 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27589 +                               bindex, err);
27590 +               /* go on even if err */
27591 +       }
27592 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27593 +               dir = d_inode(sb->s_root);
27594 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27595 +       }
27596 +
27597 +out:
27598 +       return err;
27599 +}
27600 +
27601 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27602 +{
27603 +       int err, rerr;
27604 +       unsigned char no_dreval;
27605 +       struct inode *dir;
27606 +       struct au_opt_xino *opt_xino;
27607 +       struct au_opt *opt;
27608 +       struct au_sbinfo *sbinfo;
27609 +
27610 +       SiMustWriteLock(sb);
27611 +
27612 +       err = au_dr_opt_flush(sb);
27613 +       if (unlikely(err))
27614 +               goto out;
27615 +       au_fset_opts(opts->flags, DR_FLUSHED);
27616 +
27617 +       dir = d_inode(sb->s_root);
27618 +       sbinfo = au_sbi(sb);
27619 +       opt_xino = NULL;
27620 +       opt = opts->opt;
27621 +       while (err >= 0 && opt->type != Opt_tail) {
27622 +               err = au_opt_simple(sb, opt, opts);
27623 +               if (!err)
27624 +                       err = au_opt_br(sb, opt, opts);
27625 +               if (!err)
27626 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27627 +               opt++;
27628 +       }
27629 +       if (err > 0)
27630 +               err = 0;
27631 +       AuTraceErr(err);
27632 +       /* go on even err */
27633 +
27634 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27635 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27636 +       if (unlikely(rerr && !err))
27637 +               err = rerr;
27638 +
27639 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27640 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27641 +
27642 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27643 +               rerr = au_xib_trunc(sb);
27644 +               if (unlikely(rerr && !err))
27645 +                       err = rerr;
27646 +       }
27647 +
27648 +       /* will be handled by the caller */
27649 +       if (!au_ftest_opts(opts->flags, REFRESH)
27650 +           && (opts->given_udba
27651 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27652 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27653 +                   ))
27654 +               au_fset_opts(opts->flags, REFRESH);
27655 +
27656 +       AuDbg("status 0x%x\n", opts->flags);
27657 +
27658 +out:
27659 +       return err;
27660 +}
27661 +
27662 +/* ---------------------------------------------------------------------- */
27663 +
27664 +unsigned int au_opt_udba(struct super_block *sb)
27665 +{
27666 +       return au_mntflags(sb) & AuOptMask_UDBA;
27667 +}
27668 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27669 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27670 +++ linux/fs/aufs/opts.h        2018-08-12 23:43:05.460124736 +0200
27671 @@ -0,0 +1,225 @@
27672 +/* SPDX-License-Identifier: GPL-2.0 */
27673 +/*
27674 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27675 + *
27676 + * This program, aufs is free software; you can redistribute it and/or modify
27677 + * it under the terms of the GNU General Public License as published by
27678 + * the Free Software Foundation; either version 2 of the License, or
27679 + * (at your option) any later version.
27680 + *
27681 + * This program is distributed in the hope that it will be useful,
27682 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27683 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27684 + * GNU General Public License for more details.
27685 + *
27686 + * You should have received a copy of the GNU General Public License
27687 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27688 + */
27689 +
27690 +/*
27691 + * mount options/flags
27692 + */
27693 +
27694 +#ifndef __AUFS_OPTS_H__
27695 +#define __AUFS_OPTS_H__
27696 +
27697 +#ifdef __KERNEL__
27698 +
27699 +#include <linux/path.h>
27700 +
27701 +struct file;
27702 +
27703 +/* ---------------------------------------------------------------------- */
27704 +
27705 +/* mount flags */
27706 +#define AuOpt_XINO             1               /* external inode number bitmap
27707 +                                                  and translation table */
27708 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27709 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27710 +#define AuOpt_UDBA_REVAL       (1 << 3)
27711 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27712 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27713 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27714 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27715 +                                                  bits */
27716 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27717 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27718 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27719 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27720 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
27721 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27722 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27723 +
27724 +#ifndef CONFIG_AUFS_HNOTIFY
27725 +#undef AuOpt_UDBA_HNOTIFY
27726 +#define AuOpt_UDBA_HNOTIFY     0
27727 +#endif
27728 +#ifndef CONFIG_AUFS_DIRREN
27729 +#undef AuOpt_DIRREN
27730 +#define AuOpt_DIRREN           0
27731 +#endif
27732 +#ifndef CONFIG_AUFS_SHWH
27733 +#undef AuOpt_SHWH
27734 +#define AuOpt_SHWH             0
27735 +#endif
27736 +
27737 +#define AuOpt_Def      (AuOpt_XINO \
27738 +                        | AuOpt_UDBA_REVAL \
27739 +                        | AuOpt_PLINK \
27740 +                        /* | AuOpt_DIRPERM1 */ \
27741 +                        | AuOpt_WARN_PERM)
27742 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27743 +                        | AuOpt_UDBA_REVAL \
27744 +                        | AuOpt_UDBA_HNOTIFY)
27745 +
27746 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27747 +#define au_opt_set(flags, name) do { \
27748 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27749 +       ((flags) |= AuOpt_##name); \
27750 +} while (0)
27751 +#define au_opt_set_udba(flags, name) do { \
27752 +       (flags) &= ~AuOptMask_UDBA; \
27753 +       ((flags) |= AuOpt_##name); \
27754 +} while (0)
27755 +#define au_opt_clr(flags, name) do { \
27756 +       ((flags) &= ~AuOpt_##name); \
27757 +} while (0)
27758 +
27759 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27760 +{
27761 +#ifdef CONFIG_PROC_FS
27762 +       return mntflags;
27763 +#else
27764 +       return mntflags & ~AuOpt_PLINK;
27765 +#endif
27766 +}
27767 +
27768 +/* ---------------------------------------------------------------------- */
27769 +
27770 +/* policies to select one among multiple writable branches */
27771 +enum {
27772 +       AuWbrCreate_TDP,        /* top down parent */
27773 +       AuWbrCreate_RR,         /* round robin */
27774 +       AuWbrCreate_MFS,        /* most free space */
27775 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27776 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27777 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27778 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27779 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27780 +       AuWbrCreate_PMFS,       /* parent and mfs */
27781 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27782 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27783 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27784 +
27785 +       AuWbrCreate_Def = AuWbrCreate_TDP
27786 +};
27787 +
27788 +enum {
27789 +       AuWbrCopyup_TDP,        /* top down parent */
27790 +       AuWbrCopyup_BUP,        /* bottom up parent */
27791 +       AuWbrCopyup_BU,         /* bottom up */
27792 +
27793 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27794 +};
27795 +
27796 +/* ---------------------------------------------------------------------- */
27797 +
27798 +struct au_opt_add {
27799 +       aufs_bindex_t   bindex;
27800 +       char            *pathname;
27801 +       int             perm;
27802 +       struct path     path;
27803 +};
27804 +
27805 +struct au_opt_del {
27806 +       char            *pathname;
27807 +       struct path     h_path;
27808 +};
27809 +
27810 +struct au_opt_mod {
27811 +       char            *path;
27812 +       int             perm;
27813 +       struct dentry   *h_root;
27814 +};
27815 +
27816 +struct au_opt_xino {
27817 +       char            *path;
27818 +       struct file     *file;
27819 +};
27820 +
27821 +struct au_opt_xino_itrunc {
27822 +       aufs_bindex_t   bindex;
27823 +};
27824 +
27825 +struct au_opt_wbr_create {
27826 +       int                     wbr_create;
27827 +       int                     mfs_second;
27828 +       unsigned long long      mfsrr_watermark;
27829 +};
27830 +
27831 +struct au_opt {
27832 +       int type;
27833 +       union {
27834 +               struct au_opt_xino      xino;
27835 +               struct au_opt_xino_itrunc xino_itrunc;
27836 +               struct au_opt_add       add;
27837 +               struct au_opt_del       del;
27838 +               struct au_opt_mod       mod;
27839 +               int                     dirwh;
27840 +               int                     rdcache;
27841 +               unsigned int            rdblk;
27842 +               unsigned int            rdhash;
27843 +               int                     udba;
27844 +               struct au_opt_wbr_create wbr_create;
27845 +               int                     wbr_copyup;
27846 +               unsigned int            fhsm_second;
27847 +       };
27848 +};
27849 +
27850 +/* opts flags */
27851 +#define AuOpts_REMOUNT         1
27852 +#define AuOpts_REFRESH         (1 << 1)
27853 +#define AuOpts_TRUNC_XIB       (1 << 2)
27854 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27855 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27856 +#define AuOpts_DR_FLUSHED      (1 << 5)
27857 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27858 +#define au_fset_opts(flags, name) \
27859 +       do { (flags) |= AuOpts_##name; } while (0)
27860 +#define au_fclr_opts(flags, name) \
27861 +       do { (flags) &= ~AuOpts_##name; } while (0)
27862 +
27863 +#ifndef CONFIG_AUFS_DIRREN
27864 +#undef AuOpts_DR_FLUSHED
27865 +#define AuOpts_DR_FLUSHED      0
27866 +#endif
27867 +
27868 +struct au_opts {
27869 +       struct au_opt   *opt;
27870 +       int             max_opt;
27871 +
27872 +       unsigned int    given_udba;
27873 +       unsigned int    flags;
27874 +       unsigned long   sb_flags;
27875 +};
27876 +
27877 +/* ---------------------------------------------------------------------- */
27878 +
27879 +/* opts.c */
27880 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27881 +const char *au_optstr_udba(int udba);
27882 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27883 +const char *au_optstr_wbr_create(int wbr_create);
27884 +
27885 +void au_opts_free(struct au_opts *opts);
27886 +struct super_block;
27887 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27888 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27889 +                  unsigned int pending);
27890 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27891 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27892 +
27893 +unsigned int au_opt_udba(struct super_block *sb);
27894 +
27895 +#endif /* __KERNEL__ */
27896 +#endif /* __AUFS_OPTS_H__ */
27897 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27898 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27899 +++ linux/fs/aufs/plink.c       2018-10-23 12:33:35.599375796 +0200
27900 @@ -0,0 +1,516 @@
27901 +// SPDX-License-Identifier: GPL-2.0
27902 +/*
27903 + * Copyright (C) 2005-2018 Junjiro R. Okajima
27904 + *
27905 + * This program, aufs is free software; you can redistribute it and/or modify
27906 + * it under the terms of the GNU General Public License as published by
27907 + * the Free Software Foundation; either version 2 of the License, or
27908 + * (at your option) any later version.
27909 + *
27910 + * This program is distributed in the hope that it will be useful,
27911 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27912 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27913 + * GNU General Public License for more details.
27914 + *
27915 + * You should have received a copy of the GNU General Public License
27916 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27917 + */
27918 +
27919 +/*
27920 + * pseudo-link
27921 + */
27922 +
27923 +#include "aufs.h"
27924 +
27925 +/*
27926 + * the pseudo-link maintenance mode.
27927 + * during a user process maintains the pseudo-links,
27928 + * prohibit adding a new plink and branch manipulation.
27929 + *
27930 + * Flags
27931 + * NOPLM:
27932 + *     For entry functions which will handle plink, and i_mutex is already held
27933 + *     in VFS.
27934 + *     They cannot wait and should return an error at once.
27935 + *     Callers has to check the error.
27936 + * NOPLMW:
27937 + *     For entry functions which will handle plink, but i_mutex is not held
27938 + *     in VFS.
27939 + *     They can wait the plink maintenance mode to finish.
27940 + *
27941 + * They behave like F_SETLK and F_SETLKW.
27942 + * If the caller never handle plink, then both flags are unnecessary.
27943 + */
27944 +
27945 +int au_plink_maint(struct super_block *sb, int flags)
27946 +{
27947 +       int err;
27948 +       pid_t pid, ppid;
27949 +       struct task_struct *parent, *prev;
27950 +       struct au_sbinfo *sbi;
27951 +
27952 +       SiMustAnyLock(sb);
27953 +
27954 +       err = 0;
27955 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27956 +               goto out;
27957 +
27958 +       sbi = au_sbi(sb);
27959 +       pid = sbi->si_plink_maint_pid;
27960 +       if (!pid || pid == current->pid)
27961 +               goto out;
27962 +
27963 +       /* todo: it highly depends upon /sbin/mount.aufs */
27964 +       prev = NULL;
27965 +       parent = current;
27966 +       ppid = 0;
27967 +       rcu_read_lock();
27968 +       while (1) {
27969 +               parent = rcu_dereference(parent->real_parent);
27970 +               if (parent == prev)
27971 +                       break;
27972 +               ppid = task_pid_vnr(parent);
27973 +               if (pid == ppid) {
27974 +                       rcu_read_unlock();
27975 +                       goto out;
27976 +               }
27977 +               prev = parent;
27978 +       }
27979 +       rcu_read_unlock();
27980 +
27981 +       if (au_ftest_lock(flags, NOPLMW)) {
27982 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27983 +               /* AuDebugOn(!lockdep_depth(current)); */
27984 +               while (sbi->si_plink_maint_pid) {
27985 +                       si_read_unlock(sb);
27986 +                       /* gave up wake_up_bit() */
27987 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27988 +
27989 +                       if (au_ftest_lock(flags, FLUSH))
27990 +                               au_nwt_flush(&sbi->si_nowait);
27991 +                       si_noflush_read_lock(sb);
27992 +               }
27993 +       } else if (au_ftest_lock(flags, NOPLM)) {
27994 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27995 +               err = -EAGAIN;
27996 +       }
27997 +
27998 +out:
27999 +       return err;
28000 +}
28001 +
28002 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28003 +{
28004 +       spin_lock(&sbinfo->si_plink_maint_lock);
28005 +       sbinfo->si_plink_maint_pid = 0;
28006 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28007 +       wake_up_all(&sbinfo->si_plink_wq);
28008 +}
28009 +
28010 +int au_plink_maint_enter(struct super_block *sb)
28011 +{
28012 +       int err;
28013 +       struct au_sbinfo *sbinfo;
28014 +
28015 +       err = 0;
28016 +       sbinfo = au_sbi(sb);
28017 +       /* make sure i am the only one in this fs */
28018 +       si_write_lock(sb, AuLock_FLUSH);
28019 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28020 +               spin_lock(&sbinfo->si_plink_maint_lock);
28021 +               if (!sbinfo->si_plink_maint_pid)
28022 +                       sbinfo->si_plink_maint_pid = current->pid;
28023 +               else
28024 +                       err = -EBUSY;
28025 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28026 +       }
28027 +       si_write_unlock(sb);
28028 +
28029 +       return err;
28030 +}
28031 +
28032 +/* ---------------------------------------------------------------------- */
28033 +
28034 +#ifdef CONFIG_AUFS_DEBUG
28035 +void au_plink_list(struct super_block *sb)
28036 +{
28037 +       int i;
28038 +       struct au_sbinfo *sbinfo;
28039 +       struct hlist_bl_head *hbl;
28040 +       struct hlist_bl_node *pos;
28041 +       struct au_icntnr *icntnr;
28042 +
28043 +       SiMustAnyLock(sb);
28044 +
28045 +       sbinfo = au_sbi(sb);
28046 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28047 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28048 +
28049 +       for (i = 0; i < AuPlink_NHASH; i++) {
28050 +               hbl = sbinfo->si_plink + i;
28051 +               hlist_bl_lock(hbl);
28052 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28053 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28054 +               hlist_bl_unlock(hbl);
28055 +       }
28056 +}
28057 +#endif
28058 +
28059 +/* is the inode pseudo-linked? */
28060 +int au_plink_test(struct inode *inode)
28061 +{
28062 +       int found, i;
28063 +       struct au_sbinfo *sbinfo;
28064 +       struct hlist_bl_head *hbl;
28065 +       struct hlist_bl_node *pos;
28066 +       struct au_icntnr *icntnr;
28067 +
28068 +       sbinfo = au_sbi(inode->i_sb);
28069 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28070 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28071 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28072 +
28073 +       found = 0;
28074 +       i = au_plink_hash(inode->i_ino);
28075 +       hbl =  sbinfo->si_plink + i;
28076 +       hlist_bl_lock(hbl);
28077 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28078 +               if (&icntnr->vfs_inode == inode) {
28079 +                       found = 1;
28080 +                       break;
28081 +               }
28082 +       hlist_bl_unlock(hbl);
28083 +       return found;
28084 +}
28085 +
28086 +/* ---------------------------------------------------------------------- */
28087 +
28088 +/*
28089 + * generate a name for plink.
28090 + * the file will be stored under AUFS_WH_PLINKDIR.
28091 + */
28092 +/* 20 is max digits length of ulong 64 */
28093 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28094 +
28095 +static int plink_name(char *name, int len, struct inode *inode,
28096 +                     aufs_bindex_t bindex)
28097 +{
28098 +       int rlen;
28099 +       struct inode *h_inode;
28100 +
28101 +       h_inode = au_h_iptr(inode, bindex);
28102 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28103 +       return rlen;
28104 +}
28105 +
28106 +struct au_do_plink_lkup_args {
28107 +       struct dentry **errp;
28108 +       struct qstr *tgtname;
28109 +       struct dentry *h_parent;
28110 +       struct au_branch *br;
28111 +};
28112 +
28113 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28114 +                                      struct dentry *h_parent,
28115 +                                      struct au_branch *br)
28116 +{
28117 +       struct dentry *h_dentry;
28118 +       struct inode *h_inode;
28119 +
28120 +       h_inode = d_inode(h_parent);
28121 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28122 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
28123 +       inode_unlock_shared(h_inode);
28124 +       return h_dentry;
28125 +}
28126 +
28127 +static void au_call_do_plink_lkup(void *args)
28128 +{
28129 +       struct au_do_plink_lkup_args *a = args;
28130 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28131 +}
28132 +
28133 +/* lookup the plink-ed @inode under the branch at @bindex */
28134 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28135 +{
28136 +       struct dentry *h_dentry, *h_parent;
28137 +       struct au_branch *br;
28138 +       int wkq_err;
28139 +       char a[PLINK_NAME_LEN];
28140 +       struct qstr tgtname = QSTR_INIT(a, 0);
28141 +
28142 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28143 +
28144 +       br = au_sbr(inode->i_sb, bindex);
28145 +       h_parent = br->br_wbr->wbr_plink;
28146 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28147 +
28148 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28149 +               struct au_do_plink_lkup_args args = {
28150 +                       .errp           = &h_dentry,
28151 +                       .tgtname        = &tgtname,
28152 +                       .h_parent       = h_parent,
28153 +                       .br             = br
28154 +               };
28155 +
28156 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28157 +               if (unlikely(wkq_err))
28158 +                       h_dentry = ERR_PTR(wkq_err);
28159 +       } else
28160 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28161 +
28162 +       return h_dentry;
28163 +}
28164 +
28165 +/* create a pseudo-link */
28166 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28167 +                     struct dentry *h_dentry, struct au_branch *br)
28168 +{
28169 +       int err;
28170 +       struct path h_path = {
28171 +               .mnt = au_br_mnt(br)
28172 +       };
28173 +       struct inode *h_dir, *delegated;
28174 +
28175 +       h_dir = d_inode(h_parent);
28176 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28177 +again:
28178 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
28179 +       err = PTR_ERR(h_path.dentry);
28180 +       if (IS_ERR(h_path.dentry))
28181 +               goto out;
28182 +
28183 +       err = 0;
28184 +       /* wh.plink dir is not monitored */
28185 +       /* todo: is it really safe? */
28186 +       if (d_is_positive(h_path.dentry)
28187 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28188 +               delegated = NULL;
28189 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28190 +               if (unlikely(err == -EWOULDBLOCK)) {
28191 +                       pr_warn("cannot retry for NFSv4 delegation"
28192 +                               " for an internal unlink\n");
28193 +                       iput(delegated);
28194 +               }
28195 +               dput(h_path.dentry);
28196 +               h_path.dentry = NULL;
28197 +               if (!err)
28198 +                       goto again;
28199 +       }
28200 +       if (!err && d_is_negative(h_path.dentry)) {
28201 +               delegated = NULL;
28202 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28203 +               if (unlikely(err == -EWOULDBLOCK)) {
28204 +                       pr_warn("cannot retry for NFSv4 delegation"
28205 +                               " for an internal link\n");
28206 +                       iput(delegated);
28207 +               }
28208 +       }
28209 +       dput(h_path.dentry);
28210 +
28211 +out:
28212 +       inode_unlock(h_dir);
28213 +       return err;
28214 +}
28215 +
28216 +struct do_whplink_args {
28217 +       int *errp;
28218 +       struct qstr *tgt;
28219 +       struct dentry *h_parent;
28220 +       struct dentry *h_dentry;
28221 +       struct au_branch *br;
28222 +};
28223 +
28224 +static void call_do_whplink(void *args)
28225 +{
28226 +       struct do_whplink_args *a = args;
28227 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28228 +}
28229 +
28230 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28231 +                  aufs_bindex_t bindex, struct au_branch *br)
28232 +{
28233 +       int err, wkq_err;
28234 +       struct au_wbr *wbr;
28235 +       struct dentry *h_parent;
28236 +       char a[PLINK_NAME_LEN];
28237 +       struct qstr tgtname = QSTR_INIT(a, 0);
28238 +
28239 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28240 +       h_parent = wbr->wbr_plink;
28241 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28242 +
28243 +       /* always superio. */
28244 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28245 +               struct do_whplink_args args = {
28246 +                       .errp           = &err,
28247 +                       .tgt            = &tgtname,
28248 +                       .h_parent       = h_parent,
28249 +                       .h_dentry       = h_dentry,
28250 +                       .br             = br
28251 +               };
28252 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28253 +               if (unlikely(wkq_err))
28254 +                       err = wkq_err;
28255 +       } else
28256 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28257 +
28258 +       return err;
28259 +}
28260 +
28261 +/*
28262 + * create a new pseudo-link for @h_dentry on @bindex.
28263 + * the linked inode is held in aufs @inode.
28264 + */
28265 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28266 +                    struct dentry *h_dentry)
28267 +{
28268 +       struct super_block *sb;
28269 +       struct au_sbinfo *sbinfo;
28270 +       struct hlist_bl_head *hbl;
28271 +       struct hlist_bl_node *pos;
28272 +       struct au_icntnr *icntnr;
28273 +       int found, err, cnt, i;
28274 +
28275 +       sb = inode->i_sb;
28276 +       sbinfo = au_sbi(sb);
28277 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28278 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28279 +
28280 +       found = au_plink_test(inode);
28281 +       if (found)
28282 +               return;
28283 +
28284 +       i = au_plink_hash(inode->i_ino);
28285 +       hbl = sbinfo->si_plink + i;
28286 +       au_igrab(inode);
28287 +
28288 +       hlist_bl_lock(hbl);
28289 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28290 +               if (&icntnr->vfs_inode == inode) {
28291 +                       found = 1;
28292 +                       break;
28293 +               }
28294 +       }
28295 +       if (!found) {
28296 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28297 +               hlist_bl_add_head(&icntnr->plink, hbl);
28298 +       }
28299 +       hlist_bl_unlock(hbl);
28300 +       if (!found) {
28301 +               cnt = au_hbl_count(hbl);
28302 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28303 +               if (cnt > AUFS_PLINK_WARN)
28304 +                       AuWarn1(msg ", %d\n", cnt);
28305 +#undef msg
28306 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28307 +               if (unlikely(err)) {
28308 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28309 +                       au_hbl_del(&icntnr->plink, hbl);
28310 +                       iput(&icntnr->vfs_inode);
28311 +               }
28312 +       } else
28313 +               iput(&icntnr->vfs_inode);
28314 +}
28315 +
28316 +/* free all plinks */
28317 +void au_plink_put(struct super_block *sb, int verbose)
28318 +{
28319 +       int i, warned;
28320 +       struct au_sbinfo *sbinfo;
28321 +       struct hlist_bl_head *hbl;
28322 +       struct hlist_bl_node *pos, *tmp;
28323 +       struct au_icntnr *icntnr;
28324 +
28325 +       SiMustWriteLock(sb);
28326 +
28327 +       sbinfo = au_sbi(sb);
28328 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28329 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28330 +
28331 +       /* no spin_lock since sbinfo is write-locked */
28332 +       warned = 0;
28333 +       for (i = 0; i < AuPlink_NHASH; i++) {
28334 +               hbl = sbinfo->si_plink + i;
28335 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28336 +                       pr_warn("pseudo-link is not flushed");
28337 +                       warned = 1;
28338 +               }
28339 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28340 +                       iput(&icntnr->vfs_inode);
28341 +               INIT_HLIST_BL_HEAD(hbl);
28342 +       }
28343 +}
28344 +
28345 +void au_plink_clean(struct super_block *sb, int verbose)
28346 +{
28347 +       struct dentry *root;
28348 +
28349 +       root = sb->s_root;
28350 +       aufs_write_lock(root);
28351 +       if (au_opt_test(au_mntflags(sb), PLINK))
28352 +               au_plink_put(sb, verbose);
28353 +       aufs_write_unlock(root);
28354 +}
28355 +
28356 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28357 +{
28358 +       int do_put;
28359 +       aufs_bindex_t btop, bbot, bindex;
28360 +
28361 +       do_put = 0;
28362 +       btop = au_ibtop(inode);
28363 +       bbot = au_ibbot(inode);
28364 +       if (btop >= 0) {
28365 +               for (bindex = btop; bindex <= bbot; bindex++) {
28366 +                       if (!au_h_iptr(inode, bindex)
28367 +                           || au_ii_br_id(inode, bindex) != br_id)
28368 +                               continue;
28369 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28370 +                       do_put = 1;
28371 +                       break;
28372 +               }
28373 +               if (do_put)
28374 +                       for (bindex = btop; bindex <= bbot; bindex++)
28375 +                               if (au_h_iptr(inode, bindex)) {
28376 +                                       do_put = 0;
28377 +                                       break;
28378 +                               }
28379 +       } else
28380 +               do_put = 1;
28381 +
28382 +       return do_put;
28383 +}
28384 +
28385 +/* free the plinks on a branch specified by @br_id */
28386 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28387 +{
28388 +       struct au_sbinfo *sbinfo;
28389 +       struct hlist_bl_head *hbl;
28390 +       struct hlist_bl_node *pos, *tmp;
28391 +       struct au_icntnr *icntnr;
28392 +       struct inode *inode;
28393 +       int i, do_put;
28394 +
28395 +       SiMustWriteLock(sb);
28396 +
28397 +       sbinfo = au_sbi(sb);
28398 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28399 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28400 +
28401 +       /* no bit_lock since sbinfo is write-locked */
28402 +       for (i = 0; i < AuPlink_NHASH; i++) {
28403 +               hbl = sbinfo->si_plink + i;
28404 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28405 +                       inode = au_igrab(&icntnr->vfs_inode);
28406 +                       ii_write_lock_child(inode);
28407 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28408 +                       if (do_put) {
28409 +                               hlist_bl_del(&icntnr->plink);
28410 +                               iput(inode);
28411 +                       }
28412 +                       ii_write_unlock(inode);
28413 +                       iput(inode);
28414 +               }
28415 +       }
28416 +}
28417 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28418 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28419 +++ linux/fs/aufs/poll.c        2018-08-12 23:43:05.460124736 +0200
28420 @@ -0,0 +1,51 @@
28421 +// SPDX-License-Identifier: GPL-2.0
28422 +/*
28423 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28424 + *
28425 + * This program, aufs is free software; you can redistribute it and/or modify
28426 + * it under the terms of the GNU General Public License as published by
28427 + * the Free Software Foundation; either version 2 of the License, or
28428 + * (at your option) any later version.
28429 + *
28430 + * This program is distributed in the hope that it will be useful,
28431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28433 + * GNU General Public License for more details.
28434 + *
28435 + * You should have received a copy of the GNU General Public License
28436 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28437 + */
28438 +
28439 +/*
28440 + * poll operation
28441 + * There is only one filesystem which implements ->poll operation, currently.
28442 + */
28443 +
28444 +#include "aufs.h"
28445 +
28446 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28447 +{
28448 +       __poll_t mask;
28449 +       struct file *h_file;
28450 +       struct super_block *sb;
28451 +
28452 +       /* We should pretend an error happened. */
28453 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28454 +       sb = file->f_path.dentry->d_sb;
28455 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28456 +
28457 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28458 +       if (IS_ERR(h_file)) {
28459 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28460 +               goto out;
28461 +       }
28462 +
28463 +       mask = vfs_poll(h_file, pt);
28464 +       fput(h_file); /* instead of au_read_post() */
28465 +
28466 +out:
28467 +       si_read_unlock(sb);
28468 +       if (mask & EPOLLERR)
28469 +               AuDbg("mask 0x%x\n", mask);
28470 +       return mask;
28471 +}
28472 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28473 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28474 +++ linux/fs/aufs/posix_acl.c   2018-08-12 23:43:05.460124736 +0200
28475 @@ -0,0 +1,103 @@
28476 +// SPDX-License-Identifier: GPL-2.0
28477 +/*
28478 + * Copyright (C) 2014-2018 Junjiro R. Okajima
28479 + *
28480 + * This program, aufs is free software; you can redistribute it and/or modify
28481 + * it under the terms of the GNU General Public License as published by
28482 + * the Free Software Foundation; either version 2 of the License, or
28483 + * (at your option) any later version.
28484 + *
28485 + * This program is distributed in the hope that it will be useful,
28486 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28487 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28488 + * GNU General Public License for more details.
28489 + *
28490 + * You should have received a copy of the GNU General Public License
28491 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28492 + */
28493 +
28494 +/*
28495 + * posix acl operations
28496 + */
28497 +
28498 +#include <linux/fs.h>
28499 +#include "aufs.h"
28500 +
28501 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28502 +{
28503 +       struct posix_acl *acl;
28504 +       int err;
28505 +       aufs_bindex_t bindex;
28506 +       struct inode *h_inode;
28507 +       struct super_block *sb;
28508 +
28509 +       acl = NULL;
28510 +       sb = inode->i_sb;
28511 +       si_read_lock(sb, AuLock_FLUSH);
28512 +       ii_read_lock_child(inode);
28513 +       if (!(sb->s_flags & SB_POSIXACL))
28514 +               goto out;
28515 +
28516 +       bindex = au_ibtop(inode);
28517 +       h_inode = au_h_iptr(inode, bindex);
28518 +       if (unlikely(!h_inode
28519 +                    || ((h_inode->i_mode & S_IFMT)
28520 +                        != (inode->i_mode & S_IFMT)))) {
28521 +               err = au_busy_or_stale();
28522 +               acl = ERR_PTR(err);
28523 +               goto out;
28524 +       }
28525 +
28526 +       /* always topmost only */
28527 +       acl = get_acl(h_inode, type);
28528 +       if (!IS_ERR_OR_NULL(acl))
28529 +               set_cached_acl(inode, type, acl);
28530 +
28531 +out:
28532 +       ii_read_unlock(inode);
28533 +       si_read_unlock(sb);
28534 +
28535 +       AuTraceErrPtr(acl);
28536 +       return acl;
28537 +}
28538 +
28539 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28540 +{
28541 +       int err;
28542 +       ssize_t ssz;
28543 +       struct dentry *dentry;
28544 +       struct au_sxattr arg = {
28545 +               .type = AU_ACL_SET,
28546 +               .u.acl_set = {
28547 +                       .acl    = acl,
28548 +                       .type   = type
28549 +               },
28550 +       };
28551 +
28552 +       IMustLock(inode);
28553 +
28554 +       if (inode->i_ino == AUFS_ROOT_INO)
28555 +               dentry = dget(inode->i_sb->s_root);
28556 +       else {
28557 +               dentry = d_find_alias(inode);
28558 +               if (!dentry)
28559 +                       dentry = d_find_any_alias(inode);
28560 +               if (!dentry) {
28561 +                       pr_warn("cannot handle this inode, "
28562 +                               "please report to aufs-users ML\n");
28563 +                       err = -ENOENT;
28564 +                       goto out;
28565 +               }
28566 +       }
28567 +
28568 +       ssz = au_sxattr(dentry, inode, &arg);
28569 +       dput(dentry);
28570 +       err = ssz;
28571 +       if (ssz >= 0) {
28572 +               err = 0;
28573 +               set_cached_acl(inode, type, acl);
28574 +       }
28575 +
28576 +out:
28577 +       return err;
28578 +}
28579 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28580 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28581 +++ linux/fs/aufs/procfs.c      2018-08-12 23:43:05.460124736 +0200
28582 @@ -0,0 +1,171 @@
28583 +// SPDX-License-Identifier: GPL-2.0
28584 +/*
28585 + * Copyright (C) 2010-2018 Junjiro R. Okajima
28586 + *
28587 + * This program, aufs is free software; you can redistribute it and/or modify
28588 + * it under the terms of the GNU General Public License as published by
28589 + * the Free Software Foundation; either version 2 of the License, or
28590 + * (at your option) any later version.
28591 + *
28592 + * This program is distributed in the hope that it will be useful,
28593 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28594 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28595 + * GNU General Public License for more details.
28596 + *
28597 + * You should have received a copy of the GNU General Public License
28598 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28599 + */
28600 +
28601 +/*
28602 + * procfs interfaces
28603 + */
28604 +
28605 +#include <linux/proc_fs.h>
28606 +#include "aufs.h"
28607 +
28608 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28609 +{
28610 +       struct au_sbinfo *sbinfo;
28611 +
28612 +       sbinfo = file->private_data;
28613 +       if (sbinfo) {
28614 +               au_plink_maint_leave(sbinfo);
28615 +               kobject_put(&sbinfo->si_kobj);
28616 +       }
28617 +
28618 +       return 0;
28619 +}
28620 +
28621 +static void au_procfs_plm_write_clean(struct file *file)
28622 +{
28623 +       struct au_sbinfo *sbinfo;
28624 +
28625 +       sbinfo = file->private_data;
28626 +       if (sbinfo)
28627 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28628 +}
28629 +
28630 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28631 +{
28632 +       int err;
28633 +       struct super_block *sb;
28634 +       struct au_sbinfo *sbinfo;
28635 +       struct hlist_bl_node *pos;
28636 +
28637 +       err = -EBUSY;
28638 +       if (unlikely(file->private_data))
28639 +               goto out;
28640 +
28641 +       sb = NULL;
28642 +       /* don't use au_sbilist_lock() here */
28643 +       hlist_bl_lock(&au_sbilist);
28644 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28645 +               if (id == sysaufs_si_id(sbinfo)) {
28646 +                       kobject_get(&sbinfo->si_kobj);
28647 +                       sb = sbinfo->si_sb;
28648 +                       break;
28649 +               }
28650 +       hlist_bl_unlock(&au_sbilist);
28651 +
28652 +       err = -EINVAL;
28653 +       if (unlikely(!sb))
28654 +               goto out;
28655 +
28656 +       err = au_plink_maint_enter(sb);
28657 +       if (!err)
28658 +               /* keep kobject_get() */
28659 +               file->private_data = sbinfo;
28660 +       else
28661 +               kobject_put(&sbinfo->si_kobj);
28662 +out:
28663 +       return err;
28664 +}
28665 +
28666 +/*
28667 + * Accept a valid "si=xxxx" only.
28668 + * Once it is accepted successfully, accept "clean" too.
28669 + */
28670 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28671 +                                  size_t count, loff_t *ppos)
28672 +{
28673 +       ssize_t err;
28674 +       unsigned long id;
28675 +       /* last newline is allowed */
28676 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28677 +
28678 +       err = -EACCES;
28679 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28680 +               goto out;
28681 +
28682 +       err = -EINVAL;
28683 +       if (unlikely(count > sizeof(buf)))
28684 +               goto out;
28685 +
28686 +       err = copy_from_user(buf, ubuf, count);
28687 +       if (unlikely(err)) {
28688 +               err = -EFAULT;
28689 +               goto out;
28690 +       }
28691 +       buf[count] = 0;
28692 +
28693 +       err = -EINVAL;
28694 +       if (!strcmp("clean", buf)) {
28695 +               au_procfs_plm_write_clean(file);
28696 +               goto out_success;
28697 +       } else if (unlikely(strncmp("si=", buf, 3)))
28698 +               goto out;
28699 +
28700 +       err = kstrtoul(buf + 3, 16, &id);
28701 +       if (unlikely(err))
28702 +               goto out;
28703 +
28704 +       err = au_procfs_plm_write_si(file, id);
28705 +       if (unlikely(err))
28706 +               goto out;
28707 +
28708 +out_success:
28709 +       err = count; /* success */
28710 +out:
28711 +       return err;
28712 +}
28713 +
28714 +static const struct file_operations au_procfs_plm_fop = {
28715 +       .write          = au_procfs_plm_write,
28716 +       .release        = au_procfs_plm_release,
28717 +       .owner          = THIS_MODULE
28718 +};
28719 +
28720 +/* ---------------------------------------------------------------------- */
28721 +
28722 +static struct proc_dir_entry *au_procfs_dir;
28723 +
28724 +void au_procfs_fin(void)
28725 +{
28726 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28727 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28728 +}
28729 +
28730 +int __init au_procfs_init(void)
28731 +{
28732 +       int err;
28733 +       struct proc_dir_entry *entry;
28734 +
28735 +       err = -ENOMEM;
28736 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28737 +       if (unlikely(!au_procfs_dir))
28738 +               goto out;
28739 +
28740 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28741 +                           au_procfs_dir, &au_procfs_plm_fop);
28742 +       if (unlikely(!entry))
28743 +               goto out_dir;
28744 +
28745 +       err = 0;
28746 +       goto out; /* success */
28747 +
28748 +
28749 +out_dir:
28750 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28751 +out:
28752 +       return err;
28753 +}
28754 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28755 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28756 +++ linux/fs/aufs/rdu.c 2018-08-12 23:43:05.463458173 +0200
28757 @@ -0,0 +1,382 @@
28758 +// SPDX-License-Identifier: GPL-2.0
28759 +/*
28760 + * Copyright (C) 2005-2018 Junjiro R. Okajima
28761 + *
28762 + * This program, aufs is free software; you can redistribute it and/or modify
28763 + * it under the terms of the GNU General Public License as published by
28764 + * the Free Software Foundation; either version 2 of the License, or
28765 + * (at your option) any later version.
28766 + *
28767 + * This program is distributed in the hope that it will be useful,
28768 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28769 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28770 + * GNU General Public License for more details.
28771 + *
28772 + * You should have received a copy of the GNU General Public License
28773 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28774 + */
28775 +
28776 +/*
28777 + * readdir in userspace.
28778 + */
28779 +
28780 +#include <linux/compat.h>
28781 +#include <linux/fs_stack.h>
28782 +#include <linux/security.h>
28783 +#include "aufs.h"
28784 +
28785 +/* bits for struct aufs_rdu.flags */
28786 +#define        AuRdu_CALLED    1
28787 +#define        AuRdu_CONT      (1 << 1)
28788 +#define        AuRdu_FULL      (1 << 2)
28789 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28790 +#define au_fset_rdu(flags, name) \
28791 +       do { (flags) |= AuRdu_##name; } while (0)
28792 +#define au_fclr_rdu(flags, name) \
28793 +       do { (flags) &= ~AuRdu_##name; } while (0)
28794 +
28795 +struct au_rdu_arg {
28796 +       struct dir_context              ctx;
28797 +       struct aufs_rdu                 *rdu;
28798 +       union au_rdu_ent_ul             ent;
28799 +       unsigned long                   end;
28800 +
28801 +       struct super_block              *sb;
28802 +       int                             err;
28803 +};
28804 +
28805 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28806 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28807 +{
28808 +       int err, len;
28809 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28810 +       struct aufs_rdu *rdu = arg->rdu;
28811 +       struct au_rdu_ent ent;
28812 +
28813 +       err = 0;
28814 +       arg->err = 0;
28815 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28816 +       len = au_rdu_len(nlen);
28817 +       if (arg->ent.ul + len  < arg->end) {
28818 +               ent.ino = h_ino;
28819 +               ent.bindex = rdu->cookie.bindex;
28820 +               ent.type = d_type;
28821 +               ent.nlen = nlen;
28822 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28823 +                       ent.type = DT_UNKNOWN;
28824 +
28825 +               /* unnecessary to support mmap_sem since this is a dir */
28826 +               err = -EFAULT;
28827 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28828 +                       goto out;
28829 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28830 +                       goto out;
28831 +               /* the terminating NULL */
28832 +               if (__put_user(0, arg->ent.e->name + nlen))
28833 +                       goto out;
28834 +               err = 0;
28835 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28836 +               arg->ent.ul += len;
28837 +               rdu->rent++;
28838 +       } else {
28839 +               err = -EFAULT;
28840 +               au_fset_rdu(rdu->cookie.flags, FULL);
28841 +               rdu->full = 1;
28842 +               rdu->tail = arg->ent;
28843 +       }
28844 +
28845 +out:
28846 +       /* AuTraceErr(err); */
28847 +       return err;
28848 +}
28849 +
28850 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28851 +{
28852 +       int err;
28853 +       loff_t offset;
28854 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28855 +
28856 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28857 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28858 +       err = offset;
28859 +       if (unlikely(offset != cookie->h_pos))
28860 +               goto out;
28861 +
28862 +       err = 0;
28863 +       do {
28864 +               arg->err = 0;
28865 +               au_fclr_rdu(cookie->flags, CALLED);
28866 +               /* smp_mb(); */
28867 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28868 +               if (err >= 0)
28869 +                       err = arg->err;
28870 +       } while (!err
28871 +                && au_ftest_rdu(cookie->flags, CALLED)
28872 +                && !au_ftest_rdu(cookie->flags, FULL));
28873 +       cookie->h_pos = h_file->f_pos;
28874 +
28875 +out:
28876 +       AuTraceErr(err);
28877 +       return err;
28878 +}
28879 +
28880 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28881 +{
28882 +       int err;
28883 +       aufs_bindex_t bbot;
28884 +       struct au_rdu_arg arg = {
28885 +               .ctx = {
28886 +                       .actor = au_rdu_fill
28887 +               }
28888 +       };
28889 +       struct dentry *dentry;
28890 +       struct inode *inode;
28891 +       struct file *h_file;
28892 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28893 +
28894 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
28895 +       if (unlikely(err)) {
28896 +               err = -EFAULT;
28897 +               AuTraceErr(err);
28898 +               goto out;
28899 +       }
28900 +       rdu->rent = 0;
28901 +       rdu->tail = rdu->ent;
28902 +       rdu->full = 0;
28903 +       arg.rdu = rdu;
28904 +       arg.ent = rdu->ent;
28905 +       arg.end = arg.ent.ul;
28906 +       arg.end += rdu->sz;
28907 +
28908 +       err = -ENOTDIR;
28909 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28910 +               goto out;
28911 +
28912 +       err = security_file_permission(file, MAY_READ);
28913 +       AuTraceErr(err);
28914 +       if (unlikely(err))
28915 +               goto out;
28916 +
28917 +       dentry = file->f_path.dentry;
28918 +       inode = d_inode(dentry);
28919 +       inode_lock_shared(inode);
28920 +
28921 +       arg.sb = inode->i_sb;
28922 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28923 +       if (unlikely(err))
28924 +               goto out_mtx;
28925 +       err = au_alive_dir(dentry);
28926 +       if (unlikely(err))
28927 +               goto out_si;
28928 +       /* todo: reval? */
28929 +       fi_read_lock(file);
28930 +
28931 +       err = -EAGAIN;
28932 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28933 +                    && cookie->generation != au_figen(file)))
28934 +               goto out_unlock;
28935 +
28936 +       err = 0;
28937 +       if (!rdu->blk) {
28938 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28939 +               if (!rdu->blk)
28940 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28941 +       }
28942 +       bbot = au_fbtop(file);
28943 +       if (cookie->bindex < bbot)
28944 +               cookie->bindex = bbot;
28945 +       bbot = au_fbbot_dir(file);
28946 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28947 +       for (; !err && cookie->bindex <= bbot;
28948 +            cookie->bindex++, cookie->h_pos = 0) {
28949 +               h_file = au_hf_dir(file, cookie->bindex);
28950 +               if (!h_file)
28951 +                       continue;
28952 +
28953 +               au_fclr_rdu(cookie->flags, FULL);
28954 +               err = au_rdu_do(h_file, &arg);
28955 +               AuTraceErr(err);
28956 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28957 +                       break;
28958 +       }
28959 +       AuDbg("rent %llu\n", rdu->rent);
28960 +
28961 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28962 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28963 +               au_fset_rdu(cookie->flags, CONT);
28964 +               cookie->generation = au_figen(file);
28965 +       }
28966 +
28967 +       ii_read_lock_child(inode);
28968 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28969 +       ii_read_unlock(inode);
28970 +
28971 +out_unlock:
28972 +       fi_read_unlock(file);
28973 +out_si:
28974 +       si_read_unlock(arg.sb);
28975 +out_mtx:
28976 +       inode_unlock_shared(inode);
28977 +out:
28978 +       AuTraceErr(err);
28979 +       return err;
28980 +}
28981 +
28982 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28983 +{
28984 +       int err;
28985 +       ino_t ino;
28986 +       unsigned long long nent;
28987 +       union au_rdu_ent_ul *u;
28988 +       struct au_rdu_ent ent;
28989 +       struct super_block *sb;
28990 +
28991 +       err = 0;
28992 +       nent = rdu->nent;
28993 +       u = &rdu->ent;
28994 +       sb = file->f_path.dentry->d_sb;
28995 +       si_read_lock(sb, AuLock_FLUSH);
28996 +       while (nent-- > 0) {
28997 +               /* unnecessary to support mmap_sem since this is a dir */
28998 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28999 +               if (!err)
29000 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
29001 +               if (unlikely(err)) {
29002 +                       err = -EFAULT;
29003 +                       AuTraceErr(err);
29004 +                       break;
29005 +               }
29006 +
29007 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29008 +               if (!ent.wh)
29009 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29010 +               else
29011 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29012 +                                       &ino);
29013 +               if (unlikely(err)) {
29014 +                       AuTraceErr(err);
29015 +                       break;
29016 +               }
29017 +
29018 +               err = __put_user(ino, &u->e->ino);
29019 +               if (unlikely(err)) {
29020 +                       err = -EFAULT;
29021 +                       AuTraceErr(err);
29022 +                       break;
29023 +               }
29024 +               u->ul += au_rdu_len(ent.nlen);
29025 +       }
29026 +       si_read_unlock(sb);
29027 +
29028 +       return err;
29029 +}
29030 +
29031 +/* ---------------------------------------------------------------------- */
29032 +
29033 +static int au_rdu_verify(struct aufs_rdu *rdu)
29034 +{
29035 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29036 +             "%llu, b%d, 0x%x, g%u}\n",
29037 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29038 +             rdu->blk,
29039 +             rdu->rent, rdu->shwh, rdu->full,
29040 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29041 +             rdu->cookie.generation);
29042 +
29043 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29044 +               return 0;
29045 +
29046 +       AuDbg("%u:%u\n",
29047 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29048 +       return -EINVAL;
29049 +}
29050 +
29051 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29052 +{
29053 +       long err, e;
29054 +       struct aufs_rdu rdu;
29055 +       void __user *p = (void __user *)arg;
29056 +
29057 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29058 +       if (unlikely(err)) {
29059 +               err = -EFAULT;
29060 +               AuTraceErr(err);
29061 +               goto out;
29062 +       }
29063 +       err = au_rdu_verify(&rdu);
29064 +       if (unlikely(err))
29065 +               goto out;
29066 +
29067 +       switch (cmd) {
29068 +       case AUFS_CTL_RDU:
29069 +               err = au_rdu(file, &rdu);
29070 +               if (unlikely(err))
29071 +                       break;
29072 +
29073 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29074 +               if (unlikely(e)) {
29075 +                       err = -EFAULT;
29076 +                       AuTraceErr(err);
29077 +               }
29078 +               break;
29079 +       case AUFS_CTL_RDU_INO:
29080 +               err = au_rdu_ino(file, &rdu);
29081 +               break;
29082 +
29083 +       default:
29084 +               /* err = -ENOTTY; */
29085 +               err = -EINVAL;
29086 +       }
29087 +
29088 +out:
29089 +       AuTraceErr(err);
29090 +       return err;
29091 +}
29092 +
29093 +#ifdef CONFIG_COMPAT
29094 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29095 +{
29096 +       long err, e;
29097 +       struct aufs_rdu rdu;
29098 +       void __user *p = compat_ptr(arg);
29099 +
29100 +       /* todo: get_user()? */
29101 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29102 +       if (unlikely(err)) {
29103 +               err = -EFAULT;
29104 +               AuTraceErr(err);
29105 +               goto out;
29106 +       }
29107 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29108 +       err = au_rdu_verify(&rdu);
29109 +       if (unlikely(err))
29110 +               goto out;
29111 +
29112 +       switch (cmd) {
29113 +       case AUFS_CTL_RDU:
29114 +               err = au_rdu(file, &rdu);
29115 +               if (unlikely(err))
29116 +                       break;
29117 +
29118 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29119 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29120 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29121 +               if (unlikely(e)) {
29122 +                       err = -EFAULT;
29123 +                       AuTraceErr(err);
29124 +               }
29125 +               break;
29126 +       case AUFS_CTL_RDU_INO:
29127 +               err = au_rdu_ino(file, &rdu);
29128 +               break;
29129 +
29130 +       default:
29131 +               /* err = -ENOTTY; */
29132 +               err = -EINVAL;
29133 +       }
29134 +
29135 +out:
29136 +       AuTraceErr(err);
29137 +       return err;
29138 +}
29139 +#endif
29140 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29141 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29142 +++ linux/fs/aufs/rwsem.h       2018-10-23 12:33:35.599375796 +0200
29143 @@ -0,0 +1,73 @@
29144 +/* SPDX-License-Identifier: GPL-2.0 */
29145 +/*
29146 + * Copyright (C) 2005-2018 Junjiro R. Okajima
29147 + *
29148 + * This program, aufs is free software; you can redistribute it and/or modify
29149 + * it under the terms of the GNU General Public License as published by
29150 + * the Free Software Foundation; either version 2 of the License, or
29151 + * (at your option) any later version.
29152 + *
29153 + * This program is distributed in the hope that it will be useful,
29154 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29155 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29156 + * GNU General Public License for more details.
29157 + *
29158 + * You should have received a copy of the GNU General Public License
29159 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29160 + */
29161 +
29162 +/*
29163 + * simple read-write semaphore wrappers
29164 + */
29165 +
29166 +#ifndef __AUFS_RWSEM_H__
29167 +#define __AUFS_RWSEM_H__
29168 +
29169 +#ifdef __KERNEL__
29170 +
29171 +#include "debug.h"
29172 +
29173 +/* in the future, the name 'au_rwsem' will be totally gone */
29174 +#define au_rwsem       rw_semaphore
29175 +
29176 +/* to debug easier, do not make them inlined functions */
29177 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29178 +/* rwsem_is_locked() is unusable */
29179 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
29180 +                                         && debug_locks \
29181 +                                         && !lockdep_is_held_type(rw, 1))
29182 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
29183 +                                         && debug_locks \
29184 +                                         && !lockdep_is_held_type(rw, 0))
29185 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
29186 +                                         && debug_locks \
29187 +                                         && !lockdep_is_held(rw))
29188 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
29189 +                                         && debug_locks \
29190 +                                         && lockdep_is_held(rw))
29191 +
29192 +#define au_rw_init(rw) init_rwsem(rw)
29193 +
29194 +#define au_rw_init_wlock(rw) do {              \
29195 +               au_rw_init(rw);                 \
29196 +               down_write(rw);                 \
29197 +       } while (0)
29198 +
29199 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29200 +               au_rw_init(rw);                 \
29201 +               down_write_nested(rw, lsc);     \
29202 +       } while (0)
29203 +
29204 +#define au_rw_read_lock(rw)            down_read(rw)
29205 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29206 +#define au_rw_read_unlock(rw)          up_read(rw)
29207 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29208 +#define au_rw_write_lock(rw)           down_write(rw)
29209 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29210 +#define au_rw_write_unlock(rw)         up_write(rw)
29211 +/* why is not _nested version defined? */
29212 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29213 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29214 +
29215 +#endif /* __KERNEL__ */
29216 +#endif /* __AUFS_RWSEM_H__ */
29217 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29218 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29219 +++ linux/fs/aufs/sbinfo.c      2018-12-27 13:19:17.711749485 +0100
29220 @@ -0,0 +1,313 @@
29221 +// SPDX-License-Identifier: GPL-2.0
29222 +/*
29223 + * Copyright (C) 2005-2018 Junjiro R. Okajima
29224 + *
29225 + * This program, aufs is free software; you can redistribute it and/or modify
29226 + * it under the terms of the GNU General Public License as published by
29227 + * the Free Software Foundation; either version 2 of the License, or
29228 + * (at your option) any later version.
29229 + *
29230 + * This program is distributed in the hope that it will be useful,
29231 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29232 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29233 + * GNU General Public License for more details.
29234 + *
29235 + * You should have received a copy of the GNU General Public License
29236 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29237 + */
29238 +
29239 +/*
29240 + * superblock private data
29241 + */
29242 +
29243 +#include "aufs.h"
29244 +
29245 +/*
29246 + * they are necessary regardless sysfs is disabled.
29247 + */
29248 +void au_si_free(struct kobject *kobj)
29249 +{
29250 +       int i;
29251 +       struct au_sbinfo *sbinfo;
29252 +       char *locked __maybe_unused; /* debug only */
29253 +
29254 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29255 +       for (i = 0; i < AuPlink_NHASH; i++)
29256 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29257 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29258 +
29259 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29260 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29261 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29262 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29263 +
29264 +       dbgaufs_si_fin(sbinfo);
29265 +       au_rw_write_lock(&sbinfo->si_rwsem);
29266 +       au_br_free(sbinfo);
29267 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29268 +
29269 +       au_kfree_try_rcu(sbinfo->si_branch);
29270 +       mutex_destroy(&sbinfo->si_xib_mtx);
29271 +       AuRwDestroy(&sbinfo->si_rwsem);
29272 +
29273 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29274 +       /* si_nfiles is waited too */
29275 +       au_kfree_rcu(sbinfo);
29276 +}
29277 +
29278 +int au_si_alloc(struct super_block *sb)
29279 +{
29280 +       int err, i;
29281 +       struct au_sbinfo *sbinfo;
29282 +
29283 +       err = -ENOMEM;
29284 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29285 +       if (unlikely(!sbinfo))
29286 +               goto out;
29287 +
29288 +       /* will be reallocated separately */
29289 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29290 +       if (unlikely(!sbinfo->si_branch))
29291 +               goto out_sbinfo;
29292 +
29293 +       err = sysaufs_si_init(sbinfo);
29294 +       if (!err) {
29295 +               dbgaufs_si_null(sbinfo);
29296 +               err = dbgaufs_si_init(sbinfo);
29297 +               if (unlikely(err))
29298 +                       kobject_put(&sbinfo->si_kobj);
29299 +       }
29300 +       if (unlikely(err))
29301 +               goto out_br;
29302 +
29303 +       au_nwt_init(&sbinfo->si_nowait);
29304 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29305 +
29306 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29307 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29308 +
29309 +       sbinfo->si_bbot = -1;
29310 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29311 +
29312 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29313 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29314 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29315 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29316 +
29317 +       au_fhsm_init(sbinfo);
29318 +
29319 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29320 +
29321 +       sbinfo->si_xino_jiffy = jiffies;
29322 +       sbinfo->si_xino_expire
29323 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29324 +       mutex_init(&sbinfo->si_xib_mtx);
29325 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29326 +
29327 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29328 +
29329 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29330 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29331 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29332 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29333 +
29334 +       for (i = 0; i < AuPlink_NHASH; i++)
29335 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29336 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29337 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29338 +
29339 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29340 +
29341 +       /* with getattr by default */
29342 +       sbinfo->si_iop_array = aufs_iop;
29343 +
29344 +       /* leave other members for sysaufs and si_mnt. */
29345 +       sbinfo->si_sb = sb;
29346 +       sb->s_fs_info = sbinfo;
29347 +       si_pid_set(sb);
29348 +       return 0; /* success */
29349 +
29350 +out_br:
29351 +       au_kfree_try_rcu(sbinfo->si_branch);
29352 +out_sbinfo:
29353 +       au_kfree_rcu(sbinfo);
29354 +out:
29355 +       return err;
29356 +}
29357 +
29358 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29359 +{
29360 +       int err, sz;
29361 +       struct au_branch **brp;
29362 +
29363 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29364 +
29365 +       err = -ENOMEM;
29366 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29367 +       if (unlikely(!sz))
29368 +               sz = sizeof(*brp);
29369 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29370 +                          may_shrink);
29371 +       if (brp) {
29372 +               sbinfo->si_branch = brp;
29373 +               err = 0;
29374 +       }
29375 +
29376 +       return err;
29377 +}
29378 +
29379 +/* ---------------------------------------------------------------------- */
29380 +
29381 +unsigned int au_sigen_inc(struct super_block *sb)
29382 +{
29383 +       unsigned int gen;
29384 +       struct inode *inode;
29385 +
29386 +       SiMustWriteLock(sb);
29387 +
29388 +       gen = ++au_sbi(sb)->si_generation;
29389 +       au_update_digen(sb->s_root);
29390 +       inode = d_inode(sb->s_root);
29391 +       au_update_iigen(inode, /*half*/0);
29392 +       inode_inc_iversion(inode);
29393 +       return gen;
29394 +}
29395 +
29396 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29397 +{
29398 +       aufs_bindex_t br_id;
29399 +       int i;
29400 +       struct au_sbinfo *sbinfo;
29401 +
29402 +       SiMustWriteLock(sb);
29403 +
29404 +       sbinfo = au_sbi(sb);
29405 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29406 +               br_id = ++sbinfo->si_last_br_id;
29407 +               AuDebugOn(br_id < 0);
29408 +               if (br_id && au_br_index(sb, br_id) < 0)
29409 +                       return br_id;
29410 +       }
29411 +
29412 +       return -1;
29413 +}
29414 +
29415 +/* ---------------------------------------------------------------------- */
29416 +
29417 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29418 +int si_read_lock(struct super_block *sb, int flags)
29419 +{
29420 +       int err;
29421 +
29422 +       err = 0;
29423 +       if (au_ftest_lock(flags, FLUSH))
29424 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29425 +
29426 +       si_noflush_read_lock(sb);
29427 +       err = au_plink_maint(sb, flags);
29428 +       if (unlikely(err))
29429 +               si_read_unlock(sb);
29430 +
29431 +       return err;
29432 +}
29433 +
29434 +int si_write_lock(struct super_block *sb, int flags)
29435 +{
29436 +       int err;
29437 +
29438 +       if (au_ftest_lock(flags, FLUSH))
29439 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29440 +
29441 +       si_noflush_write_lock(sb);
29442 +       err = au_plink_maint(sb, flags);
29443 +       if (unlikely(err))
29444 +               si_write_unlock(sb);
29445 +
29446 +       return err;
29447 +}
29448 +
29449 +/* dentry and super_block lock. call at entry point */
29450 +int aufs_read_lock(struct dentry *dentry, int flags)
29451 +{
29452 +       int err;
29453 +       struct super_block *sb;
29454 +
29455 +       sb = dentry->d_sb;
29456 +       err = si_read_lock(sb, flags);
29457 +       if (unlikely(err))
29458 +               goto out;
29459 +
29460 +       if (au_ftest_lock(flags, DW))
29461 +               di_write_lock_child(dentry);
29462 +       else
29463 +               di_read_lock_child(dentry, flags);
29464 +
29465 +       if (au_ftest_lock(flags, GEN)) {
29466 +               err = au_digen_test(dentry, au_sigen(sb));
29467 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29468 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29469 +               else if (!err)
29470 +                       err = au_dbrange_test(dentry);
29471 +               if (unlikely(err))
29472 +                       aufs_read_unlock(dentry, flags);
29473 +       }
29474 +
29475 +out:
29476 +       return err;
29477 +}
29478 +
29479 +void aufs_read_unlock(struct dentry *dentry, int flags)
29480 +{
29481 +       if (au_ftest_lock(flags, DW))
29482 +               di_write_unlock(dentry);
29483 +       else
29484 +               di_read_unlock(dentry, flags);
29485 +       si_read_unlock(dentry->d_sb);
29486 +}
29487 +
29488 +void aufs_write_lock(struct dentry *dentry)
29489 +{
29490 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29491 +       di_write_lock_child(dentry);
29492 +}
29493 +
29494 +void aufs_write_unlock(struct dentry *dentry)
29495 +{
29496 +       di_write_unlock(dentry);
29497 +       si_write_unlock(dentry->d_sb);
29498 +}
29499 +
29500 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29501 +{
29502 +       int err;
29503 +       unsigned int sigen;
29504 +       struct super_block *sb;
29505 +
29506 +       sb = d1->d_sb;
29507 +       err = si_read_lock(sb, flags);
29508 +       if (unlikely(err))
29509 +               goto out;
29510 +
29511 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29512 +
29513 +       if (au_ftest_lock(flags, GEN)) {
29514 +               sigen = au_sigen(sb);
29515 +               err = au_digen_test(d1, sigen);
29516 +               AuDebugOn(!err && au_dbrange_test(d1));
29517 +               if (!err) {
29518 +                       err = au_digen_test(d2, sigen);
29519 +                       AuDebugOn(!err && au_dbrange_test(d2));
29520 +               }
29521 +               if (unlikely(err))
29522 +                       aufs_read_and_write_unlock2(d1, d2);
29523 +       }
29524 +
29525 +out:
29526 +       return err;
29527 +}
29528 +
29529 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29530 +{
29531 +       di_write_unlock2(d1, d2);
29532 +       si_read_unlock(d1->d_sb);
29533 +}
29534 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29535 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29536 +++ linux/fs/aufs/super.c       2018-10-23 12:33:35.599375796 +0200
29537 @@ -0,0 +1,1048 @@
29538 +// SPDX-License-Identifier: GPL-2.0
29539 +/*
29540 + * Copyright (C) 2005-2018 Junjiro R. Okajima
29541 + *
29542 + * This program, aufs is free software; you can redistribute it and/or modify
29543 + * it under the terms of the GNU General Public License as published by
29544 + * the Free Software Foundation; either version 2 of the License, or
29545 + * (at your option) any later version.
29546 + *
29547 + * This program is distributed in the hope that it will be useful,
29548 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29549 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29550 + * GNU General Public License for more details.
29551 + *
29552 + * You should have received a copy of the GNU General Public License
29553 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29554 + */
29555 +
29556 +/*
29557 + * mount and super_block operations
29558 + */
29559 +
29560 +#include <linux/mm.h>
29561 +#include <linux/seq_file.h>
29562 +#include <linux/statfs.h>
29563 +#include <linux/vmalloc.h>
29564 +#include "aufs.h"
29565 +
29566 +/*
29567 + * super_operations
29568 + */
29569 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29570 +{
29571 +       struct au_icntnr *c;
29572 +
29573 +       c = au_cache_alloc_icntnr();
29574 +       if (c) {
29575 +               au_icntnr_init(c);
29576 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29577 +               c->iinfo.ii_hinode = NULL;
29578 +               return &c->vfs_inode;
29579 +       }
29580 +       return NULL;
29581 +}
29582 +
29583 +static void aufs_destroy_inode_cb(struct rcu_head *head)
29584 +{
29585 +       struct inode *inode = container_of(head, struct inode, i_rcu);
29586 +
29587 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29588 +}
29589 +
29590 +static void aufs_destroy_inode(struct inode *inode)
29591 +{
29592 +       if (!au_is_bad_inode(inode))
29593 +               au_iinfo_fin(inode);
29594 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
29595 +}
29596 +
29597 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29598 +{
29599 +       struct inode *inode;
29600 +       int err;
29601 +
29602 +       inode = iget_locked(sb, ino);
29603 +       if (unlikely(!inode)) {
29604 +               inode = ERR_PTR(-ENOMEM);
29605 +               goto out;
29606 +       }
29607 +       if (!(inode->i_state & I_NEW))
29608 +               goto out;
29609 +
29610 +       err = au_xigen_new(inode);
29611 +       if (!err)
29612 +               err = au_iinfo_init(inode);
29613 +       if (!err)
29614 +               inode_inc_iversion(inode);
29615 +       else {
29616 +               iget_failed(inode);
29617 +               inode = ERR_PTR(err);
29618 +       }
29619 +
29620 +out:
29621 +       /* never return NULL */
29622 +       AuDebugOn(!inode);
29623 +       AuTraceErrPtr(inode);
29624 +       return inode;
29625 +}
29626 +
29627 +/* lock free root dinfo */
29628 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29629 +{
29630 +       int err;
29631 +       aufs_bindex_t bindex, bbot;
29632 +       struct path path;
29633 +       struct au_hdentry *hdp;
29634 +       struct au_branch *br;
29635 +       au_br_perm_str_t perm;
29636 +
29637 +       err = 0;
29638 +       bbot = au_sbbot(sb);
29639 +       bindex = 0;
29640 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29641 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29642 +               br = au_sbr(sb, bindex);
29643 +               path.mnt = au_br_mnt(br);
29644 +               path.dentry = hdp->hd_dentry;
29645 +               err = au_seq_path(seq, &path);
29646 +               if (!err) {
29647 +                       au_optstr_br_perm(&perm, br->br_perm);
29648 +                       seq_printf(seq, "=%s", perm.a);
29649 +                       if (bindex != bbot)
29650 +                               seq_putc(seq, ':');
29651 +               }
29652 +       }
29653 +       if (unlikely(err || seq_has_overflowed(seq)))
29654 +               err = -E2BIG;
29655 +
29656 +       return err;
29657 +}
29658 +
29659 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29660 +                      const char *append)
29661 +{
29662 +       char *p;
29663 +
29664 +       p = fmt;
29665 +       while (*pat != ':')
29666 +               *p++ = *pat++;
29667 +       *p++ = *pat++;
29668 +       strcpy(p, append);
29669 +       AuDebugOn(strlen(fmt) >= len);
29670 +}
29671 +
29672 +static void au_show_wbr_create(struct seq_file *m, int v,
29673 +                              struct au_sbinfo *sbinfo)
29674 +{
29675 +       const char *pat;
29676 +       char fmt[32];
29677 +       struct au_wbr_mfs *mfs;
29678 +
29679 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29680 +
29681 +       seq_puts(m, ",create=");
29682 +       pat = au_optstr_wbr_create(v);
29683 +       mfs = &sbinfo->si_wbr_mfs;
29684 +       switch (v) {
29685 +       case AuWbrCreate_TDP:
29686 +       case AuWbrCreate_RR:
29687 +       case AuWbrCreate_MFS:
29688 +       case AuWbrCreate_PMFS:
29689 +               seq_puts(m, pat);
29690 +               break;
29691 +       case AuWbrCreate_MFSRR:
29692 +       case AuWbrCreate_TDMFS:
29693 +       case AuWbrCreate_PMFSRR:
29694 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29695 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29696 +               break;
29697 +       case AuWbrCreate_MFSV:
29698 +       case AuWbrCreate_PMFSV:
29699 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29700 +               seq_printf(m, fmt,
29701 +                          jiffies_to_msecs(mfs->mfs_expire)
29702 +                          / MSEC_PER_SEC);
29703 +               break;
29704 +       case AuWbrCreate_MFSRRV:
29705 +       case AuWbrCreate_TDMFSV:
29706 +       case AuWbrCreate_PMFSRRV:
29707 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29708 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29709 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29710 +               break;
29711 +       default:
29712 +               BUG();
29713 +       }
29714 +}
29715 +
29716 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29717 +{
29718 +#ifdef CONFIG_SYSFS
29719 +       return 0;
29720 +#else
29721 +       int err;
29722 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29723 +       aufs_bindex_t bindex, brid;
29724 +       struct qstr *name;
29725 +       struct file *f;
29726 +       struct dentry *d, *h_root;
29727 +       struct au_branch *br;
29728 +
29729 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29730 +
29731 +       err = 0;
29732 +       f = au_sbi(sb)->si_xib;
29733 +       if (!f)
29734 +               goto out;
29735 +
29736 +       /* stop printing the default xino path on the first writable branch */
29737 +       h_root = NULL;
29738 +       bindex = au_xi_root(sb, f->f_path.dentry);
29739 +       if (bindex >= 0) {
29740 +               br = au_sbr_sb(sb, bindex);
29741 +               h_root = au_br_dentry(br);
29742 +       }
29743 +
29744 +       d = f->f_path.dentry;
29745 +       name = &d->d_name;
29746 +       /* safe ->d_parent because the file is unlinked */
29747 +       if (d->d_parent == h_root
29748 +           && name->len == len
29749 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29750 +               goto out;
29751 +
29752 +       seq_puts(seq, ",xino=");
29753 +       err = au_xino_path(seq, f);
29754 +
29755 +out:
29756 +       return err;
29757 +#endif
29758 +}
29759 +
29760 +/* seq_file will re-call me in case of too long string */
29761 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29762 +{
29763 +       int err;
29764 +       unsigned int mnt_flags, v;
29765 +       struct super_block *sb;
29766 +       struct au_sbinfo *sbinfo;
29767 +
29768 +#define AuBool(name, str) do { \
29769 +       v = au_opt_test(mnt_flags, name); \
29770 +       if (v != au_opt_test(AuOpt_Def, name)) \
29771 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29772 +} while (0)
29773 +
29774 +#define AuStr(name, str) do { \
29775 +       v = mnt_flags & AuOptMask_##name; \
29776 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29777 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29778 +} while (0)
29779 +
29780 +#define AuUInt(name, str, val) do { \
29781 +       if (val != AUFS_##name##_DEF) \
29782 +               seq_printf(m, "," #str "=%u", val); \
29783 +} while (0)
29784 +
29785 +       sb = dentry->d_sb;
29786 +       if (sb->s_flags & SB_POSIXACL)
29787 +               seq_puts(m, ",acl");
29788 +#if 0
29789 +       if (sb->s_flags & SB_I_VERSION)
29790 +               seq_puts(m, ",i_version");
29791 +#endif
29792 +
29793 +       /* lock free root dinfo */
29794 +       si_noflush_read_lock(sb);
29795 +       sbinfo = au_sbi(sb);
29796 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29797 +
29798 +       mnt_flags = au_mntflags(sb);
29799 +       if (au_opt_test(mnt_flags, XINO)) {
29800 +               err = au_show_xino(m, sb);
29801 +               if (unlikely(err))
29802 +                       goto out;
29803 +       } else
29804 +               seq_puts(m, ",noxino");
29805 +
29806 +       AuBool(TRUNC_XINO, trunc_xino);
29807 +       AuStr(UDBA, udba);
29808 +       AuBool(SHWH, shwh);
29809 +       AuBool(PLINK, plink);
29810 +       AuBool(DIO, dio);
29811 +       AuBool(DIRPERM1, dirperm1);
29812 +
29813 +       v = sbinfo->si_wbr_create;
29814 +       if (v != AuWbrCreate_Def)
29815 +               au_show_wbr_create(m, v, sbinfo);
29816 +
29817 +       v = sbinfo->si_wbr_copyup;
29818 +       if (v != AuWbrCopyup_Def)
29819 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29820 +
29821 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29822 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29823 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29824 +
29825 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29826 +
29827 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29828 +       AuUInt(RDCACHE, rdcache, v);
29829 +
29830 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29831 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29832 +
29833 +       au_fhsm_show(m, sbinfo);
29834 +
29835 +       AuBool(DIRREN, dirren);
29836 +       AuBool(SUM, sum);
29837 +       /* AuBool(SUM_W, wsum); */
29838 +       AuBool(WARN_PERM, warn_perm);
29839 +       AuBool(VERBOSE, verbose);
29840 +
29841 +out:
29842 +       /* be sure to print "br:" last */
29843 +       if (!sysaufs_brs) {
29844 +               seq_puts(m, ",br:");
29845 +               au_show_brs(m, sb);
29846 +       }
29847 +       si_read_unlock(sb);
29848 +       return 0;
29849 +
29850 +#undef AuBool
29851 +#undef AuStr
29852 +#undef AuUInt
29853 +}
29854 +
29855 +/* ---------------------------------------------------------------------- */
29856 +
29857 +/* sum mode which returns the summation for statfs(2) */
29858 +
29859 +static u64 au_add_till_max(u64 a, u64 b)
29860 +{
29861 +       u64 old;
29862 +
29863 +       old = a;
29864 +       a += b;
29865 +       if (old <= a)
29866 +               return a;
29867 +       return ULLONG_MAX;
29868 +}
29869 +
29870 +static u64 au_mul_till_max(u64 a, long mul)
29871 +{
29872 +       u64 old;
29873 +
29874 +       old = a;
29875 +       a *= mul;
29876 +       if (old <= a)
29877 +               return a;
29878 +       return ULLONG_MAX;
29879 +}
29880 +
29881 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29882 +{
29883 +       int err;
29884 +       long bsize, factor;
29885 +       u64 blocks, bfree, bavail, files, ffree;
29886 +       aufs_bindex_t bbot, bindex, i;
29887 +       unsigned char shared;
29888 +       struct path h_path;
29889 +       struct super_block *h_sb;
29890 +
29891 +       err = 0;
29892 +       bsize = LONG_MAX;
29893 +       files = 0;
29894 +       ffree = 0;
29895 +       blocks = 0;
29896 +       bfree = 0;
29897 +       bavail = 0;
29898 +       bbot = au_sbbot(sb);
29899 +       for (bindex = 0; bindex <= bbot; bindex++) {
29900 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29901 +               h_sb = h_path.mnt->mnt_sb;
29902 +               shared = 0;
29903 +               for (i = 0; !shared && i < bindex; i++)
29904 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29905 +               if (shared)
29906 +                       continue;
29907 +
29908 +               /* sb->s_root for NFS is unreliable */
29909 +               h_path.dentry = h_path.mnt->mnt_root;
29910 +               err = vfs_statfs(&h_path, buf);
29911 +               if (unlikely(err))
29912 +                       goto out;
29913 +
29914 +               if (bsize > buf->f_bsize) {
29915 +                       /*
29916 +                        * we will reduce bsize, so we have to expand blocks
29917 +                        * etc. to match them again
29918 +                        */
29919 +                       factor = (bsize / buf->f_bsize);
29920 +                       blocks = au_mul_till_max(blocks, factor);
29921 +                       bfree = au_mul_till_max(bfree, factor);
29922 +                       bavail = au_mul_till_max(bavail, factor);
29923 +                       bsize = buf->f_bsize;
29924 +               }
29925 +
29926 +               factor = (buf->f_bsize / bsize);
29927 +               blocks = au_add_till_max(blocks,
29928 +                               au_mul_till_max(buf->f_blocks, factor));
29929 +               bfree = au_add_till_max(bfree,
29930 +                               au_mul_till_max(buf->f_bfree, factor));
29931 +               bavail = au_add_till_max(bavail,
29932 +                               au_mul_till_max(buf->f_bavail, factor));
29933 +               files = au_add_till_max(files, buf->f_files);
29934 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29935 +       }
29936 +
29937 +       buf->f_bsize = bsize;
29938 +       buf->f_blocks = blocks;
29939 +       buf->f_bfree = bfree;
29940 +       buf->f_bavail = bavail;
29941 +       buf->f_files = files;
29942 +       buf->f_ffree = ffree;
29943 +       buf->f_frsize = 0;
29944 +
29945 +out:
29946 +       return err;
29947 +}
29948 +
29949 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29950 +{
29951 +       int err;
29952 +       struct path h_path;
29953 +       struct super_block *sb;
29954 +
29955 +       /* lock free root dinfo */
29956 +       sb = dentry->d_sb;
29957 +       si_noflush_read_lock(sb);
29958 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29959 +               /* sb->s_root for NFS is unreliable */
29960 +               h_path.mnt = au_sbr_mnt(sb, 0);
29961 +               h_path.dentry = h_path.mnt->mnt_root;
29962 +               err = vfs_statfs(&h_path, buf);
29963 +       } else
29964 +               err = au_statfs_sum(sb, buf);
29965 +       si_read_unlock(sb);
29966 +
29967 +       if (!err) {
29968 +               buf->f_type = AUFS_SUPER_MAGIC;
29969 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29970 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29971 +       }
29972 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29973 +
29974 +       return err;
29975 +}
29976 +
29977 +/* ---------------------------------------------------------------------- */
29978 +
29979 +static int aufs_sync_fs(struct super_block *sb, int wait)
29980 +{
29981 +       int err, e;
29982 +       aufs_bindex_t bbot, bindex;
29983 +       struct au_branch *br;
29984 +       struct super_block *h_sb;
29985 +
29986 +       err = 0;
29987 +       si_noflush_read_lock(sb);
29988 +       bbot = au_sbbot(sb);
29989 +       for (bindex = 0; bindex <= bbot; bindex++) {
29990 +               br = au_sbr(sb, bindex);
29991 +               if (!au_br_writable(br->br_perm))
29992 +                       continue;
29993 +
29994 +               h_sb = au_sbr_sb(sb, bindex);
29995 +               e = vfsub_sync_filesystem(h_sb, wait);
29996 +               if (unlikely(e && !err))
29997 +                       err = e;
29998 +               /* go on even if an error happens */
29999 +       }
30000 +       si_read_unlock(sb);
30001 +
30002 +       return err;
30003 +}
30004 +
30005 +/* ---------------------------------------------------------------------- */
30006 +
30007 +/* final actions when unmounting a file system */
30008 +static void aufs_put_super(struct super_block *sb)
30009 +{
30010 +       struct au_sbinfo *sbinfo;
30011 +
30012 +       sbinfo = au_sbi(sb);
30013 +       if (sbinfo)
30014 +               kobject_put(&sbinfo->si_kobj);
30015 +}
30016 +
30017 +/* ---------------------------------------------------------------------- */
30018 +
30019 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30020 +                    struct super_block *sb, void *arg)
30021 +{
30022 +       void *array;
30023 +       unsigned long long n, sz;
30024 +
30025 +       array = NULL;
30026 +       n = 0;
30027 +       if (!*hint)
30028 +               goto out;
30029 +
30030 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30031 +               array = ERR_PTR(-EMFILE);
30032 +               pr_err("hint %llu\n", *hint);
30033 +               goto out;
30034 +       }
30035 +
30036 +       sz = sizeof(array) * *hint;
30037 +       array = kzalloc(sz, GFP_NOFS);
30038 +       if (unlikely(!array))
30039 +               array = vzalloc(sz);
30040 +       if (unlikely(!array)) {
30041 +               array = ERR_PTR(-ENOMEM);
30042 +               goto out;
30043 +       }
30044 +
30045 +       n = cb(sb, array, *hint, arg);
30046 +       AuDebugOn(n > *hint);
30047 +
30048 +out:
30049 +       *hint = n;
30050 +       return array;
30051 +}
30052 +
30053 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30054 +                                      unsigned long long max __maybe_unused,
30055 +                                      void *arg)
30056 +{
30057 +       unsigned long long n;
30058 +       struct inode **p, *inode;
30059 +       struct list_head *head;
30060 +
30061 +       n = 0;
30062 +       p = a;
30063 +       head = arg;
30064 +       spin_lock(&sb->s_inode_list_lock);
30065 +       list_for_each_entry(inode, head, i_sb_list) {
30066 +               if (!au_is_bad_inode(inode)
30067 +                   && au_ii(inode)->ii_btop >= 0) {
30068 +                       spin_lock(&inode->i_lock);
30069 +                       if (atomic_read(&inode->i_count)) {
30070 +                               au_igrab(inode);
30071 +                               *p++ = inode;
30072 +                               n++;
30073 +                               AuDebugOn(n > max);
30074 +                       }
30075 +                       spin_unlock(&inode->i_lock);
30076 +               }
30077 +       }
30078 +       spin_unlock(&sb->s_inode_list_lock);
30079 +
30080 +       return n;
30081 +}
30082 +
30083 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30084 +{
30085 +       struct au_sbinfo *sbi;
30086 +
30087 +       sbi = au_sbi(sb);
30088 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30089 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30090 +}
30091 +
30092 +void au_iarray_free(struct inode **a, unsigned long long max)
30093 +{
30094 +       unsigned long long ull;
30095 +
30096 +       for (ull = 0; ull < max; ull++)
30097 +               iput(a[ull]);
30098 +       kvfree(a);
30099 +}
30100 +
30101 +/* ---------------------------------------------------------------------- */
30102 +
30103 +/*
30104 + * refresh dentry and inode at remount time.
30105 + */
30106 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30107 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30108 +                     struct dentry *parent)
30109 +{
30110 +       int err;
30111 +
30112 +       di_write_lock_child(dentry);
30113 +       di_read_lock_parent(parent, AuLock_IR);
30114 +       err = au_refresh_dentry(dentry, parent);
30115 +       if (!err && dir_flags)
30116 +               au_hn_reset(d_inode(dentry), dir_flags);
30117 +       di_read_unlock(parent, AuLock_IR);
30118 +       di_write_unlock(dentry);
30119 +
30120 +       return err;
30121 +}
30122 +
30123 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30124 +                          struct au_sbinfo *sbinfo,
30125 +                          const unsigned int dir_flags, unsigned int do_idop)
30126 +{
30127 +       int err;
30128 +       struct dentry *parent;
30129 +
30130 +       err = 0;
30131 +       parent = dget_parent(dentry);
30132 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30133 +               if (d_really_is_positive(dentry)) {
30134 +                       if (!d_is_dir(dentry))
30135 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30136 +                                                parent);
30137 +                       else {
30138 +                               err = au_do_refresh(dentry, dir_flags, parent);
30139 +                               if (unlikely(err))
30140 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30141 +                       }
30142 +               } else
30143 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30144 +               AuDbgDentry(dentry);
30145 +       }
30146 +       dput(parent);
30147 +
30148 +       if (!err) {
30149 +               if (do_idop)
30150 +                       au_refresh_dop(dentry, /*force_reval*/0);
30151 +       } else
30152 +               au_refresh_dop(dentry, /*force_reval*/1);
30153 +
30154 +       AuTraceErr(err);
30155 +       return err;
30156 +}
30157 +
30158 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30159 +{
30160 +       int err, i, j, ndentry, e;
30161 +       unsigned int sigen;
30162 +       struct au_dcsub_pages dpages;
30163 +       struct au_dpage *dpage;
30164 +       struct dentry **dentries, *d;
30165 +       struct au_sbinfo *sbinfo;
30166 +       struct dentry *root = sb->s_root;
30167 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30168 +
30169 +       if (do_idop)
30170 +               au_refresh_dop(root, /*force_reval*/0);
30171 +
30172 +       err = au_dpages_init(&dpages, GFP_NOFS);
30173 +       if (unlikely(err))
30174 +               goto out;
30175 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30176 +       if (unlikely(err))
30177 +               goto out_dpages;
30178 +
30179 +       sigen = au_sigen(sb);
30180 +       sbinfo = au_sbi(sb);
30181 +       for (i = 0; i < dpages.ndpage; i++) {
30182 +               dpage = dpages.dpages + i;
30183 +               dentries = dpage->dentries;
30184 +               ndentry = dpage->ndentry;
30185 +               for (j = 0; j < ndentry; j++) {
30186 +                       d = dentries[j];
30187 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30188 +                                           do_idop);
30189 +                       if (unlikely(e && !err))
30190 +                               err = e;
30191 +                       /* go on even err */
30192 +               }
30193 +       }
30194 +
30195 +out_dpages:
30196 +       au_dpages_free(&dpages);
30197 +out:
30198 +       return err;
30199 +}
30200 +
30201 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30202 +{
30203 +       int err, e;
30204 +       unsigned int sigen;
30205 +       unsigned long long max, ull;
30206 +       struct inode *inode, **array;
30207 +
30208 +       array = au_iarray_alloc(sb, &max);
30209 +       err = PTR_ERR(array);
30210 +       if (IS_ERR(array))
30211 +               goto out;
30212 +
30213 +       err = 0;
30214 +       sigen = au_sigen(sb);
30215 +       for (ull = 0; ull < max; ull++) {
30216 +               inode = array[ull];
30217 +               if (unlikely(!inode))
30218 +                       break;
30219 +
30220 +               e = 0;
30221 +               ii_write_lock_child(inode);
30222 +               if (au_iigen(inode, NULL) != sigen) {
30223 +                       e = au_refresh_hinode_self(inode);
30224 +                       if (unlikely(e)) {
30225 +                               au_refresh_iop(inode, /*force_getattr*/1);
30226 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30227 +                               if (!err)
30228 +                                       err = e;
30229 +                               /* go on even if err */
30230 +                       }
30231 +               }
30232 +               if (!e && do_idop)
30233 +                       au_refresh_iop(inode, /*force_getattr*/0);
30234 +               ii_write_unlock(inode);
30235 +       }
30236 +
30237 +       au_iarray_free(array, max);
30238 +
30239 +out:
30240 +       return err;
30241 +}
30242 +
30243 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30244 +{
30245 +       int err, e;
30246 +       unsigned int udba;
30247 +       aufs_bindex_t bindex, bbot;
30248 +       struct dentry *root;
30249 +       struct inode *inode;
30250 +       struct au_branch *br;
30251 +       struct au_sbinfo *sbi;
30252 +
30253 +       au_sigen_inc(sb);
30254 +       sbi = au_sbi(sb);
30255 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30256 +
30257 +       root = sb->s_root;
30258 +       DiMustNoWaiters(root);
30259 +       inode = d_inode(root);
30260 +       IiMustNoWaiters(inode);
30261 +
30262 +       udba = au_opt_udba(sb);
30263 +       bbot = au_sbbot(sb);
30264 +       for (bindex = 0; bindex <= bbot; bindex++) {
30265 +               br = au_sbr(sb, bindex);
30266 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30267 +               if (unlikely(err))
30268 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30269 +                               bindex, err);
30270 +               /* go on even if err */
30271 +       }
30272 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30273 +
30274 +       if (do_idop) {
30275 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30276 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30277 +                       sb->s_d_op = &aufs_dop_noreval;
30278 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30279 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30280 +               } else {
30281 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30282 +                       sb->s_d_op = &aufs_dop;
30283 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30284 +                       sbi->si_iop_array = aufs_iop;
30285 +               }
30286 +               pr_info("reset to %ps and %ps\n",
30287 +                       sb->s_d_op, sbi->si_iop_array);
30288 +       }
30289 +
30290 +       di_write_unlock(root);
30291 +       err = au_refresh_d(sb, do_idop);
30292 +       e = au_refresh_i(sb, do_idop);
30293 +       if (unlikely(e && !err))
30294 +               err = e;
30295 +       /* aufs_write_lock() calls ..._child() */
30296 +       di_write_lock_child(root);
30297 +
30298 +       au_cpup_attr_all(inode, /*force*/1);
30299 +
30300 +       if (unlikely(err))
30301 +               AuIOErr("refresh failed, ignored, %d\n", err);
30302 +}
30303 +
30304 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30305 +static int cvt_err(int err)
30306 +{
30307 +       AuTraceErr(err);
30308 +
30309 +       switch (err) {
30310 +       case -ENOENT:
30311 +       case -ENOTDIR:
30312 +       case -EEXIST:
30313 +       case -EIO:
30314 +               err = -EINVAL;
30315 +       }
30316 +       return err;
30317 +}
30318 +
30319 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30320 +{
30321 +       int err, do_dx;
30322 +       unsigned int mntflags;
30323 +       struct au_opts opts = {
30324 +               .opt = NULL
30325 +       };
30326 +       struct dentry *root;
30327 +       struct inode *inode;
30328 +       struct au_sbinfo *sbinfo;
30329 +
30330 +       err = 0;
30331 +       root = sb->s_root;
30332 +       if (!data || !*data) {
30333 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30334 +               if (!err) {
30335 +                       di_write_lock_child(root);
30336 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30337 +                       aufs_write_unlock(root);
30338 +               }
30339 +               goto out;
30340 +       }
30341 +
30342 +       err = -ENOMEM;
30343 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30344 +       if (unlikely(!opts.opt))
30345 +               goto out;
30346 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30347 +       opts.flags = AuOpts_REMOUNT;
30348 +       opts.sb_flags = *flags;
30349 +
30350 +       /* parse it before aufs lock */
30351 +       err = au_opts_parse(sb, data, &opts);
30352 +       if (unlikely(err))
30353 +               goto out_opts;
30354 +
30355 +       sbinfo = au_sbi(sb);
30356 +       inode = d_inode(root);
30357 +       inode_lock(inode);
30358 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30359 +       if (unlikely(err))
30360 +               goto out_mtx;
30361 +       di_write_lock_child(root);
30362 +
30363 +       /* au_opts_remount() may return an error */
30364 +       err = au_opts_remount(sb, &opts);
30365 +       au_opts_free(&opts);
30366 +
30367 +       if (au_ftest_opts(opts.flags, REFRESH))
30368 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30369 +
30370 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30371 +               mntflags = au_mntflags(sb);
30372 +               do_dx = !!au_opt_test(mntflags, DIO);
30373 +               au_dy_arefresh(do_dx);
30374 +       }
30375 +
30376 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30377 +       aufs_write_unlock(root);
30378 +
30379 +out_mtx:
30380 +       inode_unlock(inode);
30381 +out_opts:
30382 +       free_page((unsigned long)opts.opt);
30383 +out:
30384 +       err = cvt_err(err);
30385 +       AuTraceErr(err);
30386 +       return err;
30387 +}
30388 +
30389 +static const struct super_operations aufs_sop = {
30390 +       .alloc_inode    = aufs_alloc_inode,
30391 +       .destroy_inode  = aufs_destroy_inode,
30392 +       /* always deleting, no clearing */
30393 +       .drop_inode     = generic_delete_inode,
30394 +       .show_options   = aufs_show_options,
30395 +       .statfs         = aufs_statfs,
30396 +       .put_super      = aufs_put_super,
30397 +       .sync_fs        = aufs_sync_fs,
30398 +       .remount_fs     = aufs_remount_fs
30399 +};
30400 +
30401 +/* ---------------------------------------------------------------------- */
30402 +
30403 +static int alloc_root(struct super_block *sb)
30404 +{
30405 +       int err;
30406 +       struct inode *inode;
30407 +       struct dentry *root;
30408 +
30409 +       err = -ENOMEM;
30410 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30411 +       err = PTR_ERR(inode);
30412 +       if (IS_ERR(inode))
30413 +               goto out;
30414 +
30415 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30416 +       inode->i_fop = &aufs_dir_fop;
30417 +       inode->i_mode = S_IFDIR;
30418 +       set_nlink(inode, 2);
30419 +       unlock_new_inode(inode);
30420 +
30421 +       root = d_make_root(inode);
30422 +       if (unlikely(!root))
30423 +               goto out;
30424 +       err = PTR_ERR(root);
30425 +       if (IS_ERR(root))
30426 +               goto out;
30427 +
30428 +       err = au_di_init(root);
30429 +       if (!err) {
30430 +               sb->s_root = root;
30431 +               return 0; /* success */
30432 +       }
30433 +       dput(root);
30434 +
30435 +out:
30436 +       return err;
30437 +}
30438 +
30439 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30440 +                          int silent __maybe_unused)
30441 +{
30442 +       int err;
30443 +       struct au_opts opts = {
30444 +               .opt = NULL
30445 +       };
30446 +       struct au_sbinfo *sbinfo;
30447 +       struct dentry *root;
30448 +       struct inode *inode;
30449 +       char *arg = raw_data;
30450 +
30451 +       if (unlikely(!arg || !*arg)) {
30452 +               err = -EINVAL;
30453 +               pr_err("no arg\n");
30454 +               goto out;
30455 +       }
30456 +
30457 +       err = -ENOMEM;
30458 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30459 +       if (unlikely(!opts.opt))
30460 +               goto out;
30461 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30462 +       opts.sb_flags = sb->s_flags;
30463 +
30464 +       err = au_si_alloc(sb);
30465 +       if (unlikely(err))
30466 +               goto out_opts;
30467 +       sbinfo = au_sbi(sb);
30468 +
30469 +       /* all timestamps always follow the ones on the branch */
30470 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30471 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30472 +       sb->s_op = &aufs_sop;
30473 +       sb->s_d_op = &aufs_dop;
30474 +       sb->s_magic = AUFS_SUPER_MAGIC;
30475 +       sb->s_maxbytes = 0;
30476 +       sb->s_stack_depth = 1;
30477 +       au_export_init(sb);
30478 +       au_xattr_init(sb);
30479 +
30480 +       err = alloc_root(sb);
30481 +       if (unlikely(err)) {
30482 +               si_write_unlock(sb);
30483 +               goto out_info;
30484 +       }
30485 +       root = sb->s_root;
30486 +       inode = d_inode(root);
30487 +
30488 +       /*
30489 +        * actually we can parse options regardless aufs lock here.
30490 +        * but at remount time, parsing must be done before aufs lock.
30491 +        * so we follow the same rule.
30492 +        */
30493 +       ii_write_lock_parent(inode);
30494 +       aufs_write_unlock(root);
30495 +       err = au_opts_parse(sb, arg, &opts);
30496 +       if (unlikely(err))
30497 +               goto out_root;
30498 +
30499 +       /* lock vfs_inode first, then aufs. */
30500 +       inode_lock(inode);
30501 +       aufs_write_lock(root);
30502 +       err = au_opts_mount(sb, &opts);
30503 +       au_opts_free(&opts);
30504 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30505 +               sb->s_d_op = &aufs_dop_noreval;
30506 +               pr_info("%ps\n", sb->s_d_op);
30507 +               au_refresh_dop(root, /*force_reval*/0);
30508 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30509 +               au_refresh_iop(inode, /*force_getattr*/0);
30510 +       }
30511 +       aufs_write_unlock(root);
30512 +       inode_unlock(inode);
30513 +       if (!err)
30514 +               goto out_opts; /* success */
30515 +
30516 +out_root:
30517 +       dput(root);
30518 +       sb->s_root = NULL;
30519 +out_info:
30520 +       kobject_put(&sbinfo->si_kobj);
30521 +       sb->s_fs_info = NULL;
30522 +out_opts:
30523 +       free_page((unsigned long)opts.opt);
30524 +out:
30525 +       AuTraceErr(err);
30526 +       err = cvt_err(err);
30527 +       AuTraceErr(err);
30528 +       return err;
30529 +}
30530 +
30531 +/* ---------------------------------------------------------------------- */
30532 +
30533 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30534 +                                const char *dev_name __maybe_unused,
30535 +                                void *raw_data)
30536 +{
30537 +       struct dentry *root;
30538 +
30539 +       /* all timestamps always follow the ones on the branch */
30540 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30541 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30542 +       if (IS_ERR(root))
30543 +               goto out;
30544 +
30545 +       au_sbilist_add(root->d_sb);
30546 +
30547 +out:
30548 +       return root;
30549 +}
30550 +
30551 +static void aufs_kill_sb(struct super_block *sb)
30552 +{
30553 +       struct au_sbinfo *sbinfo;
30554 +
30555 +       sbinfo = au_sbi(sb);
30556 +       if (sbinfo) {
30557 +               au_sbilist_del(sb);
30558 +               aufs_write_lock(sb->s_root);
30559 +               au_fhsm_fin(sb);
30560 +               if (sbinfo->si_wbr_create_ops->fin)
30561 +                       sbinfo->si_wbr_create_ops->fin(sb);
30562 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30563 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30564 +                       au_remount_refresh(sb, /*do_idop*/0);
30565 +               }
30566 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30567 +                       au_plink_put(sb, /*verbose*/1);
30568 +               au_xino_clr(sb);
30569 +               au_dr_opt_flush(sb);
30570 +               sbinfo->si_sb = NULL;
30571 +               aufs_write_unlock(sb->s_root);
30572 +               au_nwt_flush(&sbinfo->si_nowait);
30573 +       }
30574 +       kill_anon_super(sb);
30575 +}
30576 +
30577 +struct file_system_type aufs_fs_type = {
30578 +       .name           = AUFS_FSTYPE,
30579 +       /* a race between rename and others */
30580 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30581 +       .mount          = aufs_mount,
30582 +       .kill_sb        = aufs_kill_sb,
30583 +       /* no need to __module_get() and module_put(). */
30584 +       .owner          = THIS_MODULE,
30585 +};
30586 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30587 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30588 +++ linux/fs/aufs/super.h       2018-10-23 12:33:35.599375796 +0200
30589 @@ -0,0 +1,589 @@
30590 +/* SPDX-License-Identifier: GPL-2.0 */
30591 +/*
30592 + * Copyright (C) 2005-2018 Junjiro R. Okajima
30593 + *
30594 + * This program, aufs is free software; you can redistribute it and/or modify
30595 + * it under the terms of the GNU General Public License as published by
30596 + * the Free Software Foundation; either version 2 of the License, or
30597 + * (at your option) any later version.
30598 + *
30599 + * This program is distributed in the hope that it will be useful,
30600 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30601 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30602 + * GNU General Public License for more details.
30603 + *
30604 + * You should have received a copy of the GNU General Public License
30605 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30606 + */
30607 +
30608 +/*
30609 + * super_block operations
30610 + */
30611 +
30612 +#ifndef __AUFS_SUPER_H__
30613 +#define __AUFS_SUPER_H__
30614 +
30615 +#ifdef __KERNEL__
30616 +
30617 +#include <linux/fs.h>
30618 +#include <linux/kobject.h>
30619 +#include "hbl.h"
30620 +#include "lcnt.h"
30621 +#include "rwsem.h"
30622 +#include "wkq.h"
30623 +
30624 +/* policies to select one among multiple writable branches */
30625 +struct au_wbr_copyup_operations {
30626 +       int (*copyup)(struct dentry *dentry);
30627 +};
30628 +
30629 +#define AuWbr_DIR      1               /* target is a dir */
30630 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30631 +
30632 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30633 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30634 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30635 +
30636 +struct au_wbr_create_operations {
30637 +       int (*create)(struct dentry *dentry, unsigned int flags);
30638 +       int (*init)(struct super_block *sb);
30639 +       int (*fin)(struct super_block *sb);
30640 +};
30641 +
30642 +struct au_wbr_mfs {
30643 +       struct mutex    mfs_lock; /* protect this structure */
30644 +       unsigned long   mfs_jiffy;
30645 +       unsigned long   mfs_expire;
30646 +       aufs_bindex_t   mfs_bindex;
30647 +
30648 +       unsigned long long      mfsrr_bytes;
30649 +       unsigned long long      mfsrr_watermark;
30650 +};
30651 +
30652 +#define AuPlink_NHASH 100
30653 +static inline int au_plink_hash(ino_t ino)
30654 +{
30655 +       return ino % AuPlink_NHASH;
30656 +}
30657 +
30658 +/* File-based Hierarchical Storage Management */
30659 +struct au_fhsm {
30660 +#ifdef CONFIG_AUFS_FHSM
30661 +       /* allow only one process who can receive the notification */
30662 +       spinlock_t              fhsm_spin;
30663 +       pid_t                   fhsm_pid;
30664 +       wait_queue_head_t       fhsm_wqh;
30665 +       atomic_t                fhsm_readable;
30666 +
30667 +       /* these are protected by si_rwsem */
30668 +       unsigned long           fhsm_expire;
30669 +       aufs_bindex_t           fhsm_bottom;
30670 +#endif
30671 +};
30672 +
30673 +struct au_branch;
30674 +struct au_sbinfo {
30675 +       /* nowait tasks in the system-wide workqueue */
30676 +       struct au_nowait_tasks  si_nowait;
30677 +
30678 +       /*
30679 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30680 +        * rwsem for au_sbinfo is necessary.
30681 +        */
30682 +       struct au_rwsem         si_rwsem;
30683 +
30684 +       /*
30685 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30686 +        * remount.
30687 +        */
30688 +       au_lcnt_t               si_ninodes, si_nfiles;
30689 +
30690 +       /* branch management */
30691 +       unsigned int            si_generation;
30692 +
30693 +       /* see AuSi_ flags */
30694 +       unsigned char           au_si_status;
30695 +
30696 +       aufs_bindex_t           si_bbot;
30697 +
30698 +       /* dirty trick to keep br_id plus */
30699 +       unsigned int            si_last_br_id :
30700 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30701 +       struct au_branch        **si_branch;
30702 +
30703 +       /* policy to select a writable branch */
30704 +       unsigned char           si_wbr_copyup;
30705 +       unsigned char           si_wbr_create;
30706 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30707 +       struct au_wbr_create_operations *si_wbr_create_ops;
30708 +
30709 +       /* round robin */
30710 +       atomic_t                si_wbr_rr_next;
30711 +
30712 +       /* most free space */
30713 +       struct au_wbr_mfs       si_wbr_mfs;
30714 +
30715 +       /* File-based Hierarchical Storage Management */
30716 +       struct au_fhsm          si_fhsm;
30717 +
30718 +       /* mount flags */
30719 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30720 +       unsigned int            si_mntflags;
30721 +
30722 +       /* external inode number (bitmap and translation table) */
30723 +       vfs_readf_t             si_xread;
30724 +       vfs_writef_t            si_xwrite;
30725 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30726 +
30727 +       struct file             *si_xib;
30728 +       struct mutex            si_xib_mtx; /* protect xib members */
30729 +       unsigned long           *si_xib_buf;
30730 +       unsigned long           si_xib_last_pindex;
30731 +       int                     si_xib_next_bit;
30732 +
30733 +       unsigned long           si_xino_jiffy;
30734 +       unsigned long           si_xino_expire;
30735 +       /* reserved for future use */
30736 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30737 +
30738 +#ifdef CONFIG_AUFS_EXPORT
30739 +       /* i_generation */
30740 +       /* todo: make xigen file an array to support many inode numbers */
30741 +       struct file             *si_xigen;
30742 +       atomic_t                si_xigen_next;
30743 +#endif
30744 +
30745 +       /* dirty trick to support atomic_open */
30746 +       struct hlist_bl_head    si_aopen;
30747 +
30748 +       /* vdir parameters */
30749 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30750 +       unsigned int            si_rdblk;       /* deblk size */
30751 +       unsigned int            si_rdhash;      /* hash size */
30752 +
30753 +       /*
30754 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30755 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30756 +        * future fsck.aufs or kernel thread will remove them later.
30757 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30758 +        */
30759 +       unsigned int            si_dirwh;
30760 +
30761 +       /* pseudo_link list */
30762 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30763 +       wait_queue_head_t       si_plink_wq;
30764 +       spinlock_t              si_plink_maint_lock;
30765 +       pid_t                   si_plink_maint_pid;
30766 +
30767 +       /* file list */
30768 +       struct hlist_bl_head    si_files;
30769 +
30770 +       /* with/without getattr, brother of sb->s_d_op */
30771 +       struct inode_operations *si_iop_array;
30772 +
30773 +       /*
30774 +        * sysfs and lifetime management.
30775 +        * this is not a small structure and it may be a waste of memory in case
30776 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30777 +        * but using sysfs is majority.
30778 +        */
30779 +       struct kobject          si_kobj;
30780 +#ifdef CONFIG_DEBUG_FS
30781 +       struct dentry            *si_dbgaufs;
30782 +       struct dentry            *si_dbgaufs_plink;
30783 +       struct dentry            *si_dbgaufs_xib;
30784 +#ifdef CONFIG_AUFS_EXPORT
30785 +       struct dentry            *si_dbgaufs_xigen;
30786 +#endif
30787 +#endif
30788 +
30789 +#ifdef CONFIG_AUFS_SBILIST
30790 +       struct hlist_bl_node    si_list;
30791 +#endif
30792 +
30793 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30794 +       struct super_block      *si_sb;
30795 +};
30796 +
30797 +/* sbinfo status flags */
30798 +/*
30799 + * set true when refresh_dirs() failed at remount time.
30800 + * then try refreshing dirs at access time again.
30801 + * if it is false, refreshing dirs at access time is unnecessary
30802 + */
30803 +#define AuSi_FAILED_REFRESH_DIR        1
30804 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30805 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30806 +
30807 +#ifndef CONFIG_AUFS_FHSM
30808 +#undef AuSi_FHSM
30809 +#define AuSi_FHSM              0
30810 +#endif
30811 +
30812 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30813 +                                          unsigned int flag)
30814 +{
30815 +       AuRwMustAnyLock(&sbi->si_rwsem);
30816 +       return sbi->au_si_status & flag;
30817 +}
30818 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30819 +#define au_fset_si(sbinfo, name) do { \
30820 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30821 +       (sbinfo)->au_si_status |= AuSi_##name; \
30822 +} while (0)
30823 +#define au_fclr_si(sbinfo, name) do { \
30824 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30825 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30826 +} while (0)
30827 +
30828 +/* ---------------------------------------------------------------------- */
30829 +
30830 +/* policy to select one among writable branches */
30831 +#define AuWbrCopyup(sbinfo, ...) \
30832 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30833 +#define AuWbrCreate(sbinfo, ...) \
30834 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30835 +
30836 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30837 +#define AuLock_DW              1               /* write-lock dentry */
30838 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30839 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30840 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30841 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30842 +                                               /* except RENAME_EXCHANGE */
30843 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30844 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30845 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30846 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30847 +#define au_fset_lock(flags, name) \
30848 +       do { (flags) |= AuLock_##name; } while (0)
30849 +#define au_fclr_lock(flags, name) \
30850 +       do { (flags) &= ~AuLock_##name; } while (0)
30851 +
30852 +/* ---------------------------------------------------------------------- */
30853 +
30854 +/* super.c */
30855 +extern struct file_system_type aufs_fs_type;
30856 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30857 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30858 +                                          unsigned long long max, void *arg);
30859 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30860 +                    struct super_block *sb, void *arg);
30861 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30862 +void au_iarray_free(struct inode **a, unsigned long long max);
30863 +
30864 +/* sbinfo.c */
30865 +void au_si_free(struct kobject *kobj);
30866 +int au_si_alloc(struct super_block *sb);
30867 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30868 +
30869 +unsigned int au_sigen_inc(struct super_block *sb);
30870 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30871 +
30872 +int si_read_lock(struct super_block *sb, int flags);
30873 +int si_write_lock(struct super_block *sb, int flags);
30874 +int aufs_read_lock(struct dentry *dentry, int flags);
30875 +void aufs_read_unlock(struct dentry *dentry, int flags);
30876 +void aufs_write_lock(struct dentry *dentry);
30877 +void aufs_write_unlock(struct dentry *dentry);
30878 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30879 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30880 +
30881 +/* wbr_policy.c */
30882 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30883 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30884 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30885 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30886 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30887 +
30888 +/* mvdown.c */
30889 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30890 +
30891 +#ifdef CONFIG_AUFS_FHSM
30892 +/* fhsm.c */
30893 +
30894 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30895 +{
30896 +       pid_t pid;
30897 +
30898 +       spin_lock(&fhsm->fhsm_spin);
30899 +       pid = fhsm->fhsm_pid;
30900 +       spin_unlock(&fhsm->fhsm_spin);
30901 +
30902 +       return pid;
30903 +}
30904 +
30905 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30906 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30907 +int au_fhsm_fd(struct super_block *sb, int oflags);
30908 +int au_fhsm_br_alloc(struct au_branch *br);
30909 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30910 +void au_fhsm_fin(struct super_block *sb);
30911 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30912 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30913 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30914 +#else
30915 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30916 +          int force)
30917 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30918 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30919 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30920 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30921 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30922 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30923 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30924 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30925 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30926 +#endif
30927 +
30928 +/* ---------------------------------------------------------------------- */
30929 +
30930 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30931 +{
30932 +       return sb->s_fs_info;
30933 +}
30934 +
30935 +/* ---------------------------------------------------------------------- */
30936 +
30937 +#ifdef CONFIG_AUFS_EXPORT
30938 +int au_test_nfsd(void);
30939 +void au_export_init(struct super_block *sb);
30940 +void au_xigen_inc(struct inode *inode);
30941 +int au_xigen_new(struct inode *inode);
30942 +int au_xigen_set(struct super_block *sb, struct path *path);
30943 +void au_xigen_clr(struct super_block *sb);
30944 +
30945 +static inline int au_busy_or_stale(void)
30946 +{
30947 +       if (!au_test_nfsd())
30948 +               return -EBUSY;
30949 +       return -ESTALE;
30950 +}
30951 +#else
30952 +AuStubInt0(au_test_nfsd, void)
30953 +AuStubVoid(au_export_init, struct super_block *sb)
30954 +AuStubVoid(au_xigen_inc, struct inode *inode)
30955 +AuStubInt0(au_xigen_new, struct inode *inode)
30956 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30957 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30958 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30959 +#endif /* CONFIG_AUFS_EXPORT */
30960 +
30961 +/* ---------------------------------------------------------------------- */
30962 +
30963 +#ifdef CONFIG_AUFS_SBILIST
30964 +/* module.c */
30965 +extern struct hlist_bl_head au_sbilist;
30966 +
30967 +static inline void au_sbilist_init(void)
30968 +{
30969 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30970 +}
30971 +
30972 +static inline void au_sbilist_add(struct super_block *sb)
30973 +{
30974 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30975 +}
30976 +
30977 +static inline void au_sbilist_del(struct super_block *sb)
30978 +{
30979 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30980 +}
30981 +
30982 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30983 +static inline void au_sbilist_lock(void)
30984 +{
30985 +       hlist_bl_lock(&au_sbilist);
30986 +}
30987 +
30988 +static inline void au_sbilist_unlock(void)
30989 +{
30990 +       hlist_bl_unlock(&au_sbilist);
30991 +}
30992 +#define AuGFP_SBILIST  GFP_ATOMIC
30993 +#else
30994 +AuStubVoid(au_sbilist_lock, void)
30995 +AuStubVoid(au_sbilist_unlock, void)
30996 +#define AuGFP_SBILIST  GFP_NOFS
30997 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30998 +#else
30999 +AuStubVoid(au_sbilist_init, void)
31000 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31001 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31002 +AuStubVoid(au_sbilist_lock, void)
31003 +AuStubVoid(au_sbilist_unlock, void)
31004 +#define AuGFP_SBILIST  GFP_NOFS
31005 +#endif
31006 +
31007 +/* ---------------------------------------------------------------------- */
31008 +
31009 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31010 +{
31011 +       /*
31012 +        * This function is a dynamic '__init' function actually,
31013 +        * so the tiny check for si_rwsem is unnecessary.
31014 +        */
31015 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31016 +#ifdef CONFIG_DEBUG_FS
31017 +       sbinfo->si_dbgaufs = NULL;
31018 +       sbinfo->si_dbgaufs_plink = NULL;
31019 +       sbinfo->si_dbgaufs_xib = NULL;
31020 +#ifdef CONFIG_AUFS_EXPORT
31021 +       sbinfo->si_dbgaufs_xigen = NULL;
31022 +#endif
31023 +#endif
31024 +}
31025 +
31026 +/* ---------------------------------------------------------------------- */
31027 +
31028 +/* current->atomic_flags */
31029 +/* this value should never corrupt the ones defined in linux/sched.h */
31030 +#define PFA_AUFS       7
31031 +
31032 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31033 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31034 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31035 +
31036 +static inline int si_pid_test(struct super_block *sb)
31037 +{
31038 +       return !!task_test_aufs(current);
31039 +}
31040 +
31041 +static inline void si_pid_clr(struct super_block *sb)
31042 +{
31043 +       AuDebugOn(!task_test_aufs(current));
31044 +       task_clear_aufs(current);
31045 +}
31046 +
31047 +static inline void si_pid_set(struct super_block *sb)
31048 +{
31049 +       AuDebugOn(task_test_aufs(current));
31050 +       task_set_aufs(current);
31051 +}
31052 +
31053 +/* ---------------------------------------------------------------------- */
31054 +
31055 +/* lock superblock. mainly for entry point functions */
31056 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31057 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31058 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31059 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31060 +/*
31061 +#define __si_read_trylock_nested(sb) \
31062 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31063 +#define __si_write_trylock_nested(sb) \
31064 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31065 +*/
31066 +
31067 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31068 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31069 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31070 +
31071 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31072 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31073 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31074 +
31075 +static inline void si_noflush_read_lock(struct super_block *sb)
31076 +{
31077 +       __si_read_lock(sb);
31078 +       si_pid_set(sb);
31079 +}
31080 +
31081 +static inline int si_noflush_read_trylock(struct super_block *sb)
31082 +{
31083 +       int locked;
31084 +
31085 +       locked = __si_read_trylock(sb);
31086 +       if (locked)
31087 +               si_pid_set(sb);
31088 +       return locked;
31089 +}
31090 +
31091 +static inline void si_noflush_write_lock(struct super_block *sb)
31092 +{
31093 +       __si_write_lock(sb);
31094 +       si_pid_set(sb);
31095 +}
31096 +
31097 +static inline int si_noflush_write_trylock(struct super_block *sb)
31098 +{
31099 +       int locked;
31100 +
31101 +       locked = __si_write_trylock(sb);
31102 +       if (locked)
31103 +               si_pid_set(sb);
31104 +       return locked;
31105 +}
31106 +
31107 +#if 0 /* reserved */
31108 +static inline int si_read_trylock(struct super_block *sb, int flags)
31109 +{
31110 +       if (au_ftest_lock(flags, FLUSH))
31111 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31112 +       return si_noflush_read_trylock(sb);
31113 +}
31114 +#endif
31115 +
31116 +static inline void si_read_unlock(struct super_block *sb)
31117 +{
31118 +       si_pid_clr(sb);
31119 +       __si_read_unlock(sb);
31120 +}
31121 +
31122 +#if 0 /* reserved */
31123 +static inline int si_write_trylock(struct super_block *sb, int flags)
31124 +{
31125 +       if (au_ftest_lock(flags, FLUSH))
31126 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31127 +       return si_noflush_write_trylock(sb);
31128 +}
31129 +#endif
31130 +
31131 +static inline void si_write_unlock(struct super_block *sb)
31132 +{
31133 +       si_pid_clr(sb);
31134 +       __si_write_unlock(sb);
31135 +}
31136 +
31137 +#if 0 /* reserved */
31138 +static inline void si_downgrade_lock(struct super_block *sb)
31139 +{
31140 +       __si_downgrade_lock(sb);
31141 +}
31142 +#endif
31143 +
31144 +/* ---------------------------------------------------------------------- */
31145 +
31146 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31147 +{
31148 +       SiMustAnyLock(sb);
31149 +       return au_sbi(sb)->si_bbot;
31150 +}
31151 +
31152 +static inline unsigned int au_mntflags(struct super_block *sb)
31153 +{
31154 +       SiMustAnyLock(sb);
31155 +       return au_sbi(sb)->si_mntflags;
31156 +}
31157 +
31158 +static inline unsigned int au_sigen(struct super_block *sb)
31159 +{
31160 +       SiMustAnyLock(sb);
31161 +       return au_sbi(sb)->si_generation;
31162 +}
31163 +
31164 +static inline struct au_branch *au_sbr(struct super_block *sb,
31165 +                                      aufs_bindex_t bindex)
31166 +{
31167 +       SiMustAnyLock(sb);
31168 +       return au_sbi(sb)->si_branch[0 + bindex];
31169 +}
31170 +
31171 +static inline loff_t au_xi_maxent(struct super_block *sb)
31172 +{
31173 +       SiMustAnyLock(sb);
31174 +       return au_sbi(sb)->si_ximaxent;
31175 +}
31176 +
31177 +#endif /* __KERNEL__ */
31178 +#endif /* __AUFS_SUPER_H__ */
31179 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31180 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31181 +++ linux/fs/aufs/sysaufs.c     2018-08-12 23:43:05.463458173 +0200
31182 @@ -0,0 +1,93 @@
31183 +// SPDX-License-Identifier: GPL-2.0
31184 +/*
31185 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31186 + *
31187 + * This program, aufs is free software; you can redistribute it and/or modify
31188 + * it under the terms of the GNU General Public License as published by
31189 + * the Free Software Foundation; either version 2 of the License, or
31190 + * (at your option) any later version.
31191 + *
31192 + * This program is distributed in the hope that it will be useful,
31193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31195 + * GNU General Public License for more details.
31196 + *
31197 + * You should have received a copy of the GNU General Public License
31198 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31199 + */
31200 +
31201 +/*
31202 + * sysfs interface and lifetime management
31203 + * they are necessary regardless sysfs is disabled.
31204 + */
31205 +
31206 +#include <linux/random.h>
31207 +#include "aufs.h"
31208 +
31209 +unsigned long sysaufs_si_mask;
31210 +struct kset *sysaufs_kset;
31211 +
31212 +#define AuSiAttr(_name) { \
31213 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31214 +       .show   = sysaufs_si_##_name,                           \
31215 +}
31216 +
31217 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31218 +struct attribute *sysaufs_si_attrs[] = {
31219 +       &sysaufs_si_attr_xi_path.attr,
31220 +       NULL,
31221 +};
31222 +
31223 +static const struct sysfs_ops au_sbi_ops = {
31224 +       .show   = sysaufs_si_show
31225 +};
31226 +
31227 +static struct kobj_type au_sbi_ktype = {
31228 +       .release        = au_si_free,
31229 +       .sysfs_ops      = &au_sbi_ops,
31230 +       .default_attrs  = sysaufs_si_attrs
31231 +};
31232 +
31233 +/* ---------------------------------------------------------------------- */
31234 +
31235 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31236 +{
31237 +       int err;
31238 +
31239 +       sbinfo->si_kobj.kset = sysaufs_kset;
31240 +       /* cf. sysaufs_name() */
31241 +       err = kobject_init_and_add
31242 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31243 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31244 +
31245 +       return err;
31246 +}
31247 +
31248 +void sysaufs_fin(void)
31249 +{
31250 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31251 +       kset_unregister(sysaufs_kset);
31252 +}
31253 +
31254 +int __init sysaufs_init(void)
31255 +{
31256 +       int err;
31257 +
31258 +       do {
31259 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31260 +       } while (!sysaufs_si_mask);
31261 +
31262 +       err = -EINVAL;
31263 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31264 +       if (unlikely(!sysaufs_kset))
31265 +               goto out;
31266 +       err = PTR_ERR(sysaufs_kset);
31267 +       if (IS_ERR(sysaufs_kset))
31268 +               goto out;
31269 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31270 +       if (unlikely(err))
31271 +               kset_unregister(sysaufs_kset);
31272 +
31273 +out:
31274 +       return err;
31275 +}
31276 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31277 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31278 +++ linux/fs/aufs/sysaufs.h     2018-08-12 23:43:05.463458173 +0200
31279 @@ -0,0 +1,102 @@
31280 +/* SPDX-License-Identifier: GPL-2.0 */
31281 +/*
31282 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31283 + *
31284 + * This program, aufs is free software; you can redistribute it and/or modify
31285 + * it under the terms of the GNU General Public License as published by
31286 + * the Free Software Foundation; either version 2 of the License, or
31287 + * (at your option) any later version.
31288 + *
31289 + * This program is distributed in the hope that it will be useful,
31290 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31291 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31292 + * GNU General Public License for more details.
31293 + *
31294 + * You should have received a copy of the GNU General Public License
31295 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31296 + */
31297 +
31298 +/*
31299 + * sysfs interface and mount lifetime management
31300 + */
31301 +
31302 +#ifndef __SYSAUFS_H__
31303 +#define __SYSAUFS_H__
31304 +
31305 +#ifdef __KERNEL__
31306 +
31307 +#include <linux/sysfs.h>
31308 +#include "module.h"
31309 +
31310 +struct super_block;
31311 +struct au_sbinfo;
31312 +
31313 +struct sysaufs_si_attr {
31314 +       struct attribute attr;
31315 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31316 +};
31317 +
31318 +/* ---------------------------------------------------------------------- */
31319 +
31320 +/* sysaufs.c */
31321 +extern unsigned long sysaufs_si_mask;
31322 +extern struct kset *sysaufs_kset;
31323 +extern struct attribute *sysaufs_si_attrs[];
31324 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31325 +int __init sysaufs_init(void);
31326 +void sysaufs_fin(void);
31327 +
31328 +/* ---------------------------------------------------------------------- */
31329 +
31330 +/* some people doesn't like to show a pointer in kernel */
31331 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31332 +{
31333 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31334 +}
31335 +
31336 +#define SysaufsSiNamePrefix    "si_"
31337 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31338 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31339 +{
31340 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31341 +                sysaufs_si_id(sbinfo));
31342 +}
31343 +
31344 +struct au_branch;
31345 +#ifdef CONFIG_SYSFS
31346 +/* sysfs.c */
31347 +extern struct attribute_group *sysaufs_attr_group;
31348 +
31349 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31350 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31351 +                        char *buf);
31352 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31353 +#ifdef CONFIG_COMPAT
31354 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31355 +#endif
31356 +
31357 +void sysaufs_br_init(struct au_branch *br);
31358 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31359 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31360 +
31361 +#define sysaufs_brs_init()     do {} while (0)
31362 +
31363 +#else
31364 +#define sysaufs_attr_group     NULL
31365 +
31366 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31367 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31368 +       struct attribute *attr, char *buf)
31369 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31370 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31371 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31372 +
31373 +static inline void sysaufs_brs_init(void)
31374 +{
31375 +       sysaufs_brs = 0;
31376 +}
31377 +
31378 +#endif /* CONFIG_SYSFS */
31379 +
31380 +#endif /* __KERNEL__ */
31381 +#endif /* __SYSAUFS_H__ */
31382 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31383 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31384 +++ linux/fs/aufs/sysfs.c       2018-12-27 13:19:17.711749485 +0100
31385 @@ -0,0 +1,373 @@
31386 +// SPDX-License-Identifier: GPL-2.0
31387 +/*
31388 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31389 + *
31390 + * This program, aufs is free software; you can redistribute it and/or modify
31391 + * it under the terms of the GNU General Public License as published by
31392 + * the Free Software Foundation; either version 2 of the License, or
31393 + * (at your option) any later version.
31394 + *
31395 + * This program is distributed in the hope that it will be useful,
31396 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31397 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31398 + * GNU General Public License for more details.
31399 + *
31400 + * You should have received a copy of the GNU General Public License
31401 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31402 + */
31403 +
31404 +/*
31405 + * sysfs interface
31406 + */
31407 +
31408 +#include <linux/compat.h>
31409 +#include <linux/seq_file.h>
31410 +#include "aufs.h"
31411 +
31412 +#ifdef CONFIG_AUFS_FS_MODULE
31413 +/* this entry violates the "one line per file" policy of sysfs */
31414 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31415 +                          char *buf)
31416 +{
31417 +       ssize_t err;
31418 +       static char *conf =
31419 +/* this file is generated at compiling */
31420 +#include "conf.str"
31421 +               ;
31422 +
31423 +       err = snprintf(buf, PAGE_SIZE, conf);
31424 +       if (unlikely(err >= PAGE_SIZE))
31425 +               err = -EFBIG;
31426 +       return err;
31427 +}
31428 +
31429 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31430 +#endif
31431 +
31432 +static struct attribute *au_attr[] = {
31433 +#ifdef CONFIG_AUFS_FS_MODULE
31434 +       &au_config_attr.attr,
31435 +#endif
31436 +       NULL,   /* need to NULL terminate the list of attributes */
31437 +};
31438 +
31439 +static struct attribute_group sysaufs_attr_group_body = {
31440 +       .attrs = au_attr
31441 +};
31442 +
31443 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31444 +
31445 +/* ---------------------------------------------------------------------- */
31446 +
31447 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31448 +{
31449 +       int err;
31450 +
31451 +       SiMustAnyLock(sb);
31452 +
31453 +       err = 0;
31454 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31455 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31456 +               seq_putc(seq, '\n');
31457 +       }
31458 +       return err;
31459 +}
31460 +
31461 +/*
31462 + * the lifetime of branch is independent from the entry under sysfs.
31463 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31464 + * unlinked.
31465 + */
31466 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31467 +                        aufs_bindex_t bindex, int idx)
31468 +{
31469 +       int err;
31470 +       struct path path;
31471 +       struct dentry *root;
31472 +       struct au_branch *br;
31473 +       au_br_perm_str_t perm;
31474 +
31475 +       AuDbg("b%d\n", bindex);
31476 +
31477 +       err = 0;
31478 +       root = sb->s_root;
31479 +       di_read_lock_parent(root, !AuLock_IR);
31480 +       br = au_sbr(sb, bindex);
31481 +
31482 +       switch (idx) {
31483 +       case AuBrSysfs_BR:
31484 +               path.mnt = au_br_mnt(br);
31485 +               path.dentry = au_h_dptr(root, bindex);
31486 +               err = au_seq_path(seq, &path);
31487 +               if (!err) {
31488 +                       au_optstr_br_perm(&perm, br->br_perm);
31489 +                       seq_printf(seq, "=%s\n", perm.a);
31490 +               }
31491 +               break;
31492 +       case AuBrSysfs_BRID:
31493 +               seq_printf(seq, "%d\n", br->br_id);
31494 +               break;
31495 +       }
31496 +       di_read_unlock(root, !AuLock_IR);
31497 +       if (unlikely(err || seq_has_overflowed(seq)))
31498 +               err = -E2BIG;
31499 +
31500 +       return err;
31501 +}
31502 +
31503 +/* ---------------------------------------------------------------------- */
31504 +
31505 +static struct seq_file *au_seq(char *p, ssize_t len)
31506 +{
31507 +       struct seq_file *seq;
31508 +
31509 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31510 +       if (seq) {
31511 +               /* mutex_init(&seq.lock); */
31512 +               seq->buf = p;
31513 +               seq->size = len;
31514 +               return seq; /* success */
31515 +       }
31516 +
31517 +       seq = ERR_PTR(-ENOMEM);
31518 +       return seq;
31519 +}
31520 +
31521 +#define SysaufsBr_PREFIX       "br"
31522 +#define SysaufsBrid_PREFIX     "brid"
31523 +
31524 +/* todo: file size may exceed PAGE_SIZE */
31525 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31526 +                       char *buf)
31527 +{
31528 +       ssize_t err;
31529 +       int idx;
31530 +       long l;
31531 +       aufs_bindex_t bbot;
31532 +       struct au_sbinfo *sbinfo;
31533 +       struct super_block *sb;
31534 +       struct seq_file *seq;
31535 +       char *name;
31536 +       struct attribute **cattr;
31537 +
31538 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31539 +       sb = sbinfo->si_sb;
31540 +
31541 +       /*
31542 +        * prevent a race condition between sysfs and aufs.
31543 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31544 +        * prohibits maintaining the sysfs entries.
31545 +        * hew we acquire read lock after sysfs_get_active_two().
31546 +        * on the other hand, the remount process may maintain the sysfs/aufs
31547 +        * entries after acquiring write lock.
31548 +        * it can cause a deadlock.
31549 +        * simply we gave up processing read here.
31550 +        */
31551 +       err = -EBUSY;
31552 +       if (unlikely(!si_noflush_read_trylock(sb)))
31553 +               goto out;
31554 +
31555 +       seq = au_seq(buf, PAGE_SIZE);
31556 +       err = PTR_ERR(seq);
31557 +       if (IS_ERR(seq))
31558 +               goto out_unlock;
31559 +
31560 +       name = (void *)attr->name;
31561 +       cattr = sysaufs_si_attrs;
31562 +       while (*cattr) {
31563 +               if (!strcmp(name, (*cattr)->name)) {
31564 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31565 +                               ->show(seq, sb);
31566 +                       goto out_seq;
31567 +               }
31568 +               cattr++;
31569 +       }
31570 +
31571 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31572 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31573 +               idx = AuBrSysfs_BRID;
31574 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31575 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31576 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31577 +               idx = AuBrSysfs_BR;
31578 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31579 +       } else
31580 +                 BUG();
31581 +
31582 +       err = kstrtol(name, 10, &l);
31583 +       if (!err) {
31584 +               bbot = au_sbbot(sb);
31585 +               if (l <= bbot)
31586 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31587 +               else
31588 +                       err = -ENOENT;
31589 +       }
31590 +
31591 +out_seq:
31592 +       if (!err) {
31593 +               err = seq->count;
31594 +               /* sysfs limit */
31595 +               if (unlikely(err == PAGE_SIZE))
31596 +                       err = -EFBIG;
31597 +       }
31598 +       au_kfree_rcu(seq);
31599 +out_unlock:
31600 +       si_read_unlock(sb);
31601 +out:
31602 +       return err;
31603 +}
31604 +
31605 +/* ---------------------------------------------------------------------- */
31606 +
31607 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31608 +{
31609 +       int err;
31610 +       int16_t brid;
31611 +       aufs_bindex_t bindex, bbot;
31612 +       size_t sz;
31613 +       char *buf;
31614 +       struct seq_file *seq;
31615 +       struct au_branch *br;
31616 +
31617 +       si_read_lock(sb, AuLock_FLUSH);
31618 +       bbot = au_sbbot(sb);
31619 +       err = bbot + 1;
31620 +       if (!arg)
31621 +               goto out;
31622 +
31623 +       err = -ENOMEM;
31624 +       buf = (void *)__get_free_page(GFP_NOFS);
31625 +       if (unlikely(!buf))
31626 +               goto out;
31627 +
31628 +       seq = au_seq(buf, PAGE_SIZE);
31629 +       err = PTR_ERR(seq);
31630 +       if (IS_ERR(seq))
31631 +               goto out_buf;
31632 +
31633 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31634 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31635 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
31636 +               if (unlikely(err))
31637 +                       break;
31638 +
31639 +               br = au_sbr(sb, bindex);
31640 +               brid = br->br_id;
31641 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31642 +               err = __put_user(brid, &arg->id);
31643 +               if (unlikely(err))
31644 +                       break;
31645 +
31646 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31647 +               err = __put_user(br->br_perm, &arg->perm);
31648 +               if (unlikely(err))
31649 +                       break;
31650 +
31651 +               err = au_seq_path(seq, &br->br_path);
31652 +               if (unlikely(err))
31653 +                       break;
31654 +               seq_putc(seq, '\0');
31655 +               if (!seq_has_overflowed(seq)) {
31656 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31657 +                       seq->count = 0;
31658 +                       if (unlikely(err))
31659 +                               break;
31660 +               } else {
31661 +                       err = -E2BIG;
31662 +                       goto out_seq;
31663 +               }
31664 +       }
31665 +       if (unlikely(err))
31666 +               err = -EFAULT;
31667 +
31668 +out_seq:
31669 +       au_kfree_rcu(seq);
31670 +out_buf:
31671 +       free_page((unsigned long)buf);
31672 +out:
31673 +       si_read_unlock(sb);
31674 +       return err;
31675 +}
31676 +
31677 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31678 +{
31679 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31680 +}
31681 +
31682 +#ifdef CONFIG_COMPAT
31683 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31684 +{
31685 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31686 +}
31687 +#endif
31688 +
31689 +/* ---------------------------------------------------------------------- */
31690 +
31691 +void sysaufs_br_init(struct au_branch *br)
31692 +{
31693 +       int i;
31694 +       struct au_brsysfs *br_sysfs;
31695 +       struct attribute *attr;
31696 +
31697 +       br_sysfs = br->br_sysfs;
31698 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31699 +               attr = &br_sysfs->attr;
31700 +               sysfs_attr_init(attr);
31701 +               attr->name = br_sysfs->name;
31702 +               attr->mode = 0444;
31703 +               br_sysfs++;
31704 +       }
31705 +}
31706 +
31707 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31708 +{
31709 +       struct au_branch *br;
31710 +       struct kobject *kobj;
31711 +       struct au_brsysfs *br_sysfs;
31712 +       int i;
31713 +       aufs_bindex_t bbot;
31714 +
31715 +       if (!sysaufs_brs)
31716 +               return;
31717 +
31718 +       kobj = &au_sbi(sb)->si_kobj;
31719 +       bbot = au_sbbot(sb);
31720 +       for (; bindex <= bbot; bindex++) {
31721 +               br = au_sbr(sb, bindex);
31722 +               br_sysfs = br->br_sysfs;
31723 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31724 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31725 +                       br_sysfs++;
31726 +               }
31727 +       }
31728 +}
31729 +
31730 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31731 +{
31732 +       int err, i;
31733 +       aufs_bindex_t bbot;
31734 +       struct kobject *kobj;
31735 +       struct au_branch *br;
31736 +       struct au_brsysfs *br_sysfs;
31737 +
31738 +       if (!sysaufs_brs)
31739 +               return;
31740 +
31741 +       kobj = &au_sbi(sb)->si_kobj;
31742 +       bbot = au_sbbot(sb);
31743 +       for (; bindex <= bbot; bindex++) {
31744 +               br = au_sbr(sb, bindex);
31745 +               br_sysfs = br->br_sysfs;
31746 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31747 +                        SysaufsBr_PREFIX "%d", bindex);
31748 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31749 +                        SysaufsBrid_PREFIX "%d", bindex);
31750 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31751 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31752 +                       if (unlikely(err))
31753 +                               pr_warn("failed %s under sysfs(%d)\n",
31754 +                                       br_sysfs->name, err);
31755 +                       br_sysfs++;
31756 +               }
31757 +       }
31758 +}
31759 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31760 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31761 +++ linux/fs/aufs/sysrq.c       2018-10-23 12:33:35.599375796 +0200
31762 @@ -0,0 +1,160 @@
31763 +// SPDX-License-Identifier: GPL-2.0
31764 +/*
31765 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31766 + *
31767 + * This program, aufs is free software; you can redistribute it and/or modify
31768 + * it under the terms of the GNU General Public License as published by
31769 + * the Free Software Foundation; either version 2 of the License, or
31770 + * (at your option) any later version.
31771 + *
31772 + * This program is distributed in the hope that it will be useful,
31773 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31774 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31775 + * GNU General Public License for more details.
31776 + *
31777 + * You should have received a copy of the GNU General Public License
31778 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31779 + */
31780 +
31781 +/*
31782 + * magic sysrq handler
31783 + */
31784 +
31785 +/* #include <linux/sysrq.h> */
31786 +#include <linux/writeback.h>
31787 +#include "aufs.h"
31788 +
31789 +/* ---------------------------------------------------------------------- */
31790 +
31791 +static void sysrq_sb(struct super_block *sb)
31792 +{
31793 +       char *plevel;
31794 +       struct au_sbinfo *sbinfo;
31795 +       struct file *file;
31796 +       struct hlist_bl_head *files;
31797 +       struct hlist_bl_node *pos;
31798 +       struct au_finfo *finfo;
31799 +
31800 +       plevel = au_plevel;
31801 +       au_plevel = KERN_WARNING;
31802 +
31803 +       /* since we define pr_fmt, call printk directly */
31804 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31805 +
31806 +       sbinfo = au_sbi(sb);
31807 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31808 +       pr("superblock\n");
31809 +       au_dpri_sb(sb);
31810 +
31811 +#if 0
31812 +       pr("root dentry\n");
31813 +       au_dpri_dentry(sb->s_root);
31814 +       pr("root inode\n");
31815 +       au_dpri_inode(d_inode(sb->s_root));
31816 +#endif
31817 +
31818 +#if 0
31819 +       do {
31820 +               int err, i, j, ndentry;
31821 +               struct au_dcsub_pages dpages;
31822 +               struct au_dpage *dpage;
31823 +
31824 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31825 +               if (unlikely(err))
31826 +                       break;
31827 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31828 +               if (!err)
31829 +                       for (i = 0; i < dpages.ndpage; i++) {
31830 +                               dpage = dpages.dpages + i;
31831 +                               ndentry = dpage->ndentry;
31832 +                               for (j = 0; j < ndentry; j++)
31833 +                                       au_dpri_dentry(dpage->dentries[j]);
31834 +                       }
31835 +               au_dpages_free(&dpages);
31836 +       } while (0);
31837 +#endif
31838 +
31839 +#if 1
31840 +       {
31841 +               struct inode *i;
31842 +
31843 +               pr("isolated inode\n");
31844 +               spin_lock(&sb->s_inode_list_lock);
31845 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31846 +                       spin_lock(&i->i_lock);
31847 +                       if (1 || hlist_empty(&i->i_dentry))
31848 +                               au_dpri_inode(i);
31849 +                       spin_unlock(&i->i_lock);
31850 +               }
31851 +               spin_unlock(&sb->s_inode_list_lock);
31852 +       }
31853 +#endif
31854 +       pr("files\n");
31855 +       files = &au_sbi(sb)->si_files;
31856 +       hlist_bl_lock(files);
31857 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31858 +               umode_t mode;
31859 +
31860 +               file = finfo->fi_file;
31861 +               mode = file_inode(file)->i_mode;
31862 +               if (!special_file(mode))
31863 +                       au_dpri_file(file);
31864 +       }
31865 +       hlist_bl_unlock(files);
31866 +       pr("done\n");
31867 +
31868 +#undef pr
31869 +       au_plevel = plevel;
31870 +}
31871 +
31872 +/* ---------------------------------------------------------------------- */
31873 +
31874 +/* module parameter */
31875 +static char *aufs_sysrq_key = "a";
31876 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31877 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31878 +
31879 +static void au_sysrq(int key __maybe_unused)
31880 +{
31881 +       struct au_sbinfo *sbinfo;
31882 +       struct hlist_bl_node *pos;
31883 +
31884 +       lockdep_off();
31885 +       au_sbilist_lock();
31886 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31887 +               sysrq_sb(sbinfo->si_sb);
31888 +       au_sbilist_unlock();
31889 +       lockdep_on();
31890 +}
31891 +
31892 +static struct sysrq_key_op au_sysrq_op = {
31893 +       .handler        = au_sysrq,
31894 +       .help_msg       = "Aufs",
31895 +       .action_msg     = "Aufs",
31896 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31897 +};
31898 +
31899 +/* ---------------------------------------------------------------------- */
31900 +
31901 +int __init au_sysrq_init(void)
31902 +{
31903 +       int err;
31904 +       char key;
31905 +
31906 +       err = -1;
31907 +       key = *aufs_sysrq_key;
31908 +       if ('a' <= key && key <= 'z')
31909 +               err = register_sysrq_key(key, &au_sysrq_op);
31910 +       if (unlikely(err))
31911 +               pr_err("err %d, sysrq=%c\n", err, key);
31912 +       return err;
31913 +}
31914 +
31915 +void au_sysrq_fin(void)
31916 +{
31917 +       int err;
31918 +
31919 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31920 +       if (unlikely(err))
31921 +               pr_err("err %d (ignored)\n", err);
31922 +}
31923 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31924 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31925 +++ linux/fs/aufs/vdir.c        2018-12-27 13:19:17.711749485 +0100
31926 @@ -0,0 +1,895 @@
31927 +// SPDX-License-Identifier: GPL-2.0
31928 +/*
31929 + * Copyright (C) 2005-2018 Junjiro R. Okajima
31930 + *
31931 + * This program, aufs is free software; you can redistribute it and/or modify
31932 + * it under the terms of the GNU General Public License as published by
31933 + * the Free Software Foundation; either version 2 of the License, or
31934 + * (at your option) any later version.
31935 + *
31936 + * This program is distributed in the hope that it will be useful,
31937 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31938 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31939 + * GNU General Public License for more details.
31940 + *
31941 + * You should have received a copy of the GNU General Public License
31942 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31943 + */
31944 +
31945 +/*
31946 + * virtual or vertical directory
31947 + */
31948 +
31949 +#include "aufs.h"
31950 +
31951 +static unsigned int calc_size(int nlen)
31952 +{
31953 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31954 +}
31955 +
31956 +static int set_deblk_end(union au_vdir_deblk_p *p,
31957 +                        union au_vdir_deblk_p *deblk_end)
31958 +{
31959 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31960 +               p->de->de_str.len = 0;
31961 +               /* smp_mb(); */
31962 +               return 0;
31963 +       }
31964 +       return -1; /* error */
31965 +}
31966 +
31967 +/* returns true or false */
31968 +static int is_deblk_end(union au_vdir_deblk_p *p,
31969 +                       union au_vdir_deblk_p *deblk_end)
31970 +{
31971 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31972 +               return !p->de->de_str.len;
31973 +       return 1;
31974 +}
31975 +
31976 +static unsigned char *last_deblk(struct au_vdir *vdir)
31977 +{
31978 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31979 +}
31980 +
31981 +/* ---------------------------------------------------------------------- */
31982 +
31983 +/* estimate the appropriate size for name hash table */
31984 +unsigned int au_rdhash_est(loff_t sz)
31985 +{
31986 +       unsigned int n;
31987 +
31988 +       n = UINT_MAX;
31989 +       sz >>= 10;
31990 +       if (sz < n)
31991 +               n = sz;
31992 +       if (sz < AUFS_RDHASH_DEF)
31993 +               n = AUFS_RDHASH_DEF;
31994 +       /* pr_info("n %u\n", n); */
31995 +       return n;
31996 +}
31997 +
31998 +/*
31999 + * the allocated memory has to be freed by
32000 + * au_nhash_wh_free() or au_nhash_de_free().
32001 + */
32002 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32003 +{
32004 +       struct hlist_head *head;
32005 +       unsigned int u;
32006 +       size_t sz;
32007 +
32008 +       sz = sizeof(*nhash->nh_head) * num_hash;
32009 +       head = kmalloc(sz, gfp);
32010 +       if (head) {
32011 +               nhash->nh_num = num_hash;
32012 +               nhash->nh_head = head;
32013 +               for (u = 0; u < num_hash; u++)
32014 +                       INIT_HLIST_HEAD(head++);
32015 +               return 0; /* success */
32016 +       }
32017 +
32018 +       return -ENOMEM;
32019 +}
32020 +
32021 +static void nhash_count(struct hlist_head *head)
32022 +{
32023 +#if 0
32024 +       unsigned long n;
32025 +       struct hlist_node *pos;
32026 +
32027 +       n = 0;
32028 +       hlist_for_each(pos, head)
32029 +               n++;
32030 +       pr_info("%lu\n", n);
32031 +#endif
32032 +}
32033 +
32034 +static void au_nhash_wh_do_free(struct hlist_head *head)
32035 +{
32036 +       struct au_vdir_wh *pos;
32037 +       struct hlist_node *node;
32038 +
32039 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32040 +               au_kfree_rcu(pos);
32041 +}
32042 +
32043 +static void au_nhash_de_do_free(struct hlist_head *head)
32044 +{
32045 +       struct au_vdir_dehstr *pos;
32046 +       struct hlist_node *node;
32047 +
32048 +       hlist_for_each_entry_safe(pos, node, head, hash)
32049 +               au_cache_free_vdir_dehstr(pos);
32050 +}
32051 +
32052 +static void au_nhash_do_free(struct au_nhash *nhash,
32053 +                            void (*free)(struct hlist_head *head))
32054 +{
32055 +       unsigned int n;
32056 +       struct hlist_head *head;
32057 +
32058 +       n = nhash->nh_num;
32059 +       if (!n)
32060 +               return;
32061 +
32062 +       head = nhash->nh_head;
32063 +       while (n-- > 0) {
32064 +               nhash_count(head);
32065 +               free(head++);
32066 +       }
32067 +       au_kfree_try_rcu(nhash->nh_head);
32068 +}
32069 +
32070 +void au_nhash_wh_free(struct au_nhash *whlist)
32071 +{
32072 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32073 +}
32074 +
32075 +static void au_nhash_de_free(struct au_nhash *delist)
32076 +{
32077 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32078 +}
32079 +
32080 +/* ---------------------------------------------------------------------- */
32081 +
32082 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32083 +                           int limit)
32084 +{
32085 +       int num;
32086 +       unsigned int u, n;
32087 +       struct hlist_head *head;
32088 +       struct au_vdir_wh *pos;
32089 +
32090 +       num = 0;
32091 +       n = whlist->nh_num;
32092 +       head = whlist->nh_head;
32093 +       for (u = 0; u < n; u++, head++)
32094 +               hlist_for_each_entry(pos, head, wh_hash)
32095 +                       if (pos->wh_bindex == btgt && ++num > limit)
32096 +                               return 1;
32097 +       return 0;
32098 +}
32099 +
32100 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32101 +                                      unsigned char *name,
32102 +                                      unsigned int len)
32103 +{
32104 +       unsigned int v;
32105 +       /* const unsigned int magic_bit = 12; */
32106 +
32107 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32108 +
32109 +       v = 0;
32110 +       if (len > 8)
32111 +               len = 8;
32112 +       while (len--)
32113 +               v += *name++;
32114 +       /* v = hash_long(v, magic_bit); */
32115 +       v %= nhash->nh_num;
32116 +       return nhash->nh_head + v;
32117 +}
32118 +
32119 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32120 +                             int nlen)
32121 +{
32122 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32123 +}
32124 +
32125 +/* returns found or not */
32126 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32127 +{
32128 +       struct hlist_head *head;
32129 +       struct au_vdir_wh *pos;
32130 +       struct au_vdir_destr *str;
32131 +
32132 +       head = au_name_hash(whlist, name, nlen);
32133 +       hlist_for_each_entry(pos, head, wh_hash) {
32134 +               str = &pos->wh_str;
32135 +               AuDbg("%.*s\n", str->len, str->name);
32136 +               if (au_nhash_test_name(str, name, nlen))
32137 +                       return 1;
32138 +       }
32139 +       return 0;
32140 +}
32141 +
32142 +/* returns found(true) or not */
32143 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32144 +{
32145 +       struct hlist_head *head;
32146 +       struct au_vdir_dehstr *pos;
32147 +       struct au_vdir_destr *str;
32148 +
32149 +       head = au_name_hash(delist, name, nlen);
32150 +       hlist_for_each_entry(pos, head, hash) {
32151 +               str = pos->str;
32152 +               AuDbg("%.*s\n", str->len, str->name);
32153 +               if (au_nhash_test_name(str, name, nlen))
32154 +                       return 1;
32155 +       }
32156 +       return 0;
32157 +}
32158 +
32159 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32160 +                           unsigned char d_type)
32161 +{
32162 +#ifdef CONFIG_AUFS_SHWH
32163 +       wh->wh_ino = ino;
32164 +       wh->wh_type = d_type;
32165 +#endif
32166 +}
32167 +
32168 +/* ---------------------------------------------------------------------- */
32169 +
32170 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32171 +                      unsigned int d_type, aufs_bindex_t bindex,
32172 +                      unsigned char shwh)
32173 +{
32174 +       int err;
32175 +       struct au_vdir_destr *str;
32176 +       struct au_vdir_wh *wh;
32177 +
32178 +       AuDbg("%.*s\n", nlen, name);
32179 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32180 +
32181 +       err = -ENOMEM;
32182 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32183 +       if (unlikely(!wh))
32184 +               goto out;
32185 +
32186 +       err = 0;
32187 +       wh->wh_bindex = bindex;
32188 +       if (shwh)
32189 +               au_shwh_init_wh(wh, ino, d_type);
32190 +       str = &wh->wh_str;
32191 +       str->len = nlen;
32192 +       memcpy(str->name, name, nlen);
32193 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32194 +       /* smp_mb(); */
32195 +
32196 +out:
32197 +       return err;
32198 +}
32199 +
32200 +static int append_deblk(struct au_vdir *vdir)
32201 +{
32202 +       int err;
32203 +       unsigned long ul;
32204 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32205 +       union au_vdir_deblk_p p, deblk_end;
32206 +       unsigned char **o;
32207 +
32208 +       err = -ENOMEM;
32209 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32210 +                       GFP_NOFS, /*may_shrink*/0);
32211 +       if (unlikely(!o))
32212 +               goto out;
32213 +
32214 +       vdir->vd_deblk = o;
32215 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32216 +       if (p.deblk) {
32217 +               ul = vdir->vd_nblk++;
32218 +               vdir->vd_deblk[ul] = p.deblk;
32219 +               vdir->vd_last.ul = ul;
32220 +               vdir->vd_last.p.deblk = p.deblk;
32221 +               deblk_end.deblk = p.deblk + deblk_sz;
32222 +               err = set_deblk_end(&p, &deblk_end);
32223 +       }
32224 +
32225 +out:
32226 +       return err;
32227 +}
32228 +
32229 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32230 +                    unsigned int d_type, struct au_nhash *delist)
32231 +{
32232 +       int err;
32233 +       unsigned int sz;
32234 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32235 +       union au_vdir_deblk_p p, *room, deblk_end;
32236 +       struct au_vdir_dehstr *dehstr;
32237 +
32238 +       p.deblk = last_deblk(vdir);
32239 +       deblk_end.deblk = p.deblk + deblk_sz;
32240 +       room = &vdir->vd_last.p;
32241 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32242 +                 || !is_deblk_end(room, &deblk_end));
32243 +
32244 +       sz = calc_size(nlen);
32245 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32246 +               err = append_deblk(vdir);
32247 +               if (unlikely(err))
32248 +                       goto out;
32249 +
32250 +               p.deblk = last_deblk(vdir);
32251 +               deblk_end.deblk = p.deblk + deblk_sz;
32252 +               /* smp_mb(); */
32253 +               AuDebugOn(room->deblk != p.deblk);
32254 +       }
32255 +
32256 +       err = -ENOMEM;
32257 +       dehstr = au_cache_alloc_vdir_dehstr();
32258 +       if (unlikely(!dehstr))
32259 +               goto out;
32260 +
32261 +       dehstr->str = &room->de->de_str;
32262 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32263 +       room->de->de_ino = ino;
32264 +       room->de->de_type = d_type;
32265 +       room->de->de_str.len = nlen;
32266 +       memcpy(room->de->de_str.name, name, nlen);
32267 +
32268 +       err = 0;
32269 +       room->deblk += sz;
32270 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32271 +               err = append_deblk(vdir);
32272 +       /* smp_mb(); */
32273 +
32274 +out:
32275 +       return err;
32276 +}
32277 +
32278 +/* ---------------------------------------------------------------------- */
32279 +
32280 +void au_vdir_free(struct au_vdir *vdir)
32281 +{
32282 +       unsigned char **deblk;
32283 +
32284 +       deblk = vdir->vd_deblk;
32285 +       while (vdir->vd_nblk--)
32286 +               au_kfree_try_rcu(*deblk++);
32287 +       au_kfree_try_rcu(vdir->vd_deblk);
32288 +       au_cache_free_vdir(vdir);
32289 +}
32290 +
32291 +static struct au_vdir *alloc_vdir(struct file *file)
32292 +{
32293 +       struct au_vdir *vdir;
32294 +       struct super_block *sb;
32295 +       int err;
32296 +
32297 +       sb = file->f_path.dentry->d_sb;
32298 +       SiMustAnyLock(sb);
32299 +
32300 +       err = -ENOMEM;
32301 +       vdir = au_cache_alloc_vdir();
32302 +       if (unlikely(!vdir))
32303 +               goto out;
32304 +
32305 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32306 +       if (unlikely(!vdir->vd_deblk))
32307 +               goto out_free;
32308 +
32309 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32310 +       if (!vdir->vd_deblk_sz) {
32311 +               /* estimate the appropriate size for deblk */
32312 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32313 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32314 +       }
32315 +       vdir->vd_nblk = 0;
32316 +       vdir->vd_version = 0;
32317 +       vdir->vd_jiffy = 0;
32318 +       err = append_deblk(vdir);
32319 +       if (!err)
32320 +               return vdir; /* success */
32321 +
32322 +       au_kfree_try_rcu(vdir->vd_deblk);
32323 +
32324 +out_free:
32325 +       au_cache_free_vdir(vdir);
32326 +out:
32327 +       vdir = ERR_PTR(err);
32328 +       return vdir;
32329 +}
32330 +
32331 +static int reinit_vdir(struct au_vdir *vdir)
32332 +{
32333 +       int err;
32334 +       union au_vdir_deblk_p p, deblk_end;
32335 +
32336 +       while (vdir->vd_nblk > 1) {
32337 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32338 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32339 +               vdir->vd_nblk--;
32340 +       }
32341 +       p.deblk = vdir->vd_deblk[0];
32342 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32343 +       err = set_deblk_end(&p, &deblk_end);
32344 +       /* keep vd_dblk_sz */
32345 +       vdir->vd_last.ul = 0;
32346 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32347 +       vdir->vd_version = 0;
32348 +       vdir->vd_jiffy = 0;
32349 +       /* smp_mb(); */
32350 +       return err;
32351 +}
32352 +
32353 +/* ---------------------------------------------------------------------- */
32354 +
32355 +#define AuFillVdir_CALLED      1
32356 +#define AuFillVdir_WHABLE      (1 << 1)
32357 +#define AuFillVdir_SHWH                (1 << 2)
32358 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32359 +#define au_fset_fillvdir(flags, name) \
32360 +       do { (flags) |= AuFillVdir_##name; } while (0)
32361 +#define au_fclr_fillvdir(flags, name) \
32362 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32363 +
32364 +#ifndef CONFIG_AUFS_SHWH
32365 +#undef AuFillVdir_SHWH
32366 +#define AuFillVdir_SHWH                0
32367 +#endif
32368 +
32369 +struct fillvdir_arg {
32370 +       struct dir_context      ctx;
32371 +       struct file             *file;
32372 +       struct au_vdir          *vdir;
32373 +       struct au_nhash         delist;
32374 +       struct au_nhash         whlist;
32375 +       aufs_bindex_t           bindex;
32376 +       unsigned int            flags;
32377 +       int                     err;
32378 +};
32379 +
32380 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32381 +                   loff_t offset __maybe_unused, u64 h_ino,
32382 +                   unsigned int d_type)
32383 +{
32384 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32385 +       char *name = (void *)__name;
32386 +       struct super_block *sb;
32387 +       ino_t ino;
32388 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32389 +
32390 +       arg->err = 0;
32391 +       sb = arg->file->f_path.dentry->d_sb;
32392 +       au_fset_fillvdir(arg->flags, CALLED);
32393 +       /* smp_mb(); */
32394 +       if (nlen <= AUFS_WH_PFX_LEN
32395 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32396 +               if (test_known(&arg->delist, name, nlen)
32397 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32398 +                       goto out; /* already exists or whiteouted */
32399 +
32400 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32401 +               if (!arg->err) {
32402 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32403 +                               d_type = DT_UNKNOWN;
32404 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32405 +                                            d_type, &arg->delist);
32406 +               }
32407 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32408 +               name += AUFS_WH_PFX_LEN;
32409 +               nlen -= AUFS_WH_PFX_LEN;
32410 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32411 +                       goto out; /* already whiteouted */
32412 +
32413 +               ino = 0; /* just to suppress a warning */
32414 +               if (shwh)
32415 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32416 +                                            &ino);
32417 +               if (!arg->err) {
32418 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32419 +                               d_type = DT_UNKNOWN;
32420 +                       arg->err = au_nhash_append_wh
32421 +                               (&arg->whlist, name, nlen, ino, d_type,
32422 +                                arg->bindex, shwh);
32423 +               }
32424 +       }
32425 +
32426 +out:
32427 +       if (!arg->err)
32428 +               arg->vdir->vd_jiffy = jiffies;
32429 +       /* smp_mb(); */
32430 +       AuTraceErr(arg->err);
32431 +       return arg->err;
32432 +}
32433 +
32434 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32435 +                         struct au_nhash *whlist, struct au_nhash *delist)
32436 +{
32437 +#ifdef CONFIG_AUFS_SHWH
32438 +       int err;
32439 +       unsigned int nh, u;
32440 +       struct hlist_head *head;
32441 +       struct au_vdir_wh *pos;
32442 +       struct hlist_node *n;
32443 +       char *p, *o;
32444 +       struct au_vdir_destr *destr;
32445 +
32446 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32447 +
32448 +       err = -ENOMEM;
32449 +       o = p = (void *)__get_free_page(GFP_NOFS);
32450 +       if (unlikely(!p))
32451 +               goto out;
32452 +
32453 +       err = 0;
32454 +       nh = whlist->nh_num;
32455 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32456 +       p += AUFS_WH_PFX_LEN;
32457 +       for (u = 0; u < nh; u++) {
32458 +               head = whlist->nh_head + u;
32459 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32460 +                       destr = &pos->wh_str;
32461 +                       memcpy(p, destr->name, destr->len);
32462 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32463 +                                       pos->wh_ino, pos->wh_type, delist);
32464 +                       if (unlikely(err))
32465 +                               break;
32466 +               }
32467 +       }
32468 +
32469 +       free_page((unsigned long)o);
32470 +
32471 +out:
32472 +       AuTraceErr(err);
32473 +       return err;
32474 +#else
32475 +       return 0;
32476 +#endif
32477 +}
32478 +
32479 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32480 +{
32481 +       int err;
32482 +       unsigned int rdhash;
32483 +       loff_t offset;
32484 +       aufs_bindex_t bbot, bindex, btop;
32485 +       unsigned char shwh;
32486 +       struct file *hf, *file;
32487 +       struct super_block *sb;
32488 +
32489 +       file = arg->file;
32490 +       sb = file->f_path.dentry->d_sb;
32491 +       SiMustAnyLock(sb);
32492 +
32493 +       rdhash = au_sbi(sb)->si_rdhash;
32494 +       if (!rdhash)
32495 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32496 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32497 +       if (unlikely(err))
32498 +               goto out;
32499 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32500 +       if (unlikely(err))
32501 +               goto out_delist;
32502 +
32503 +       err = 0;
32504 +       arg->flags = 0;
32505 +       shwh = 0;
32506 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32507 +               shwh = 1;
32508 +               au_fset_fillvdir(arg->flags, SHWH);
32509 +       }
32510 +       btop = au_fbtop(file);
32511 +       bbot = au_fbbot_dir(file);
32512 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32513 +               hf = au_hf_dir(file, bindex);
32514 +               if (!hf)
32515 +                       continue;
32516 +
32517 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32518 +               err = offset;
32519 +               if (unlikely(offset))
32520 +                       break;
32521 +
32522 +               arg->bindex = bindex;
32523 +               au_fclr_fillvdir(arg->flags, WHABLE);
32524 +               if (shwh
32525 +                   || (bindex != bbot
32526 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32527 +                       au_fset_fillvdir(arg->flags, WHABLE);
32528 +               do {
32529 +                       arg->err = 0;
32530 +                       au_fclr_fillvdir(arg->flags, CALLED);
32531 +                       /* smp_mb(); */
32532 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32533 +                       if (err >= 0)
32534 +                               err = arg->err;
32535 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32536 +
32537 +               /*
32538 +                * dir_relax() may be good for concurrency, but aufs should not
32539 +                * use it since it will cause a lockdep problem.
32540 +                */
32541 +       }
32542 +
32543 +       if (!err && shwh)
32544 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32545 +
32546 +       au_nhash_wh_free(&arg->whlist);
32547 +
32548 +out_delist:
32549 +       au_nhash_de_free(&arg->delist);
32550 +out:
32551 +       return err;
32552 +}
32553 +
32554 +static int read_vdir(struct file *file, int may_read)
32555 +{
32556 +       int err;
32557 +       unsigned long expire;
32558 +       unsigned char do_read;
32559 +       struct fillvdir_arg arg = {
32560 +               .ctx = {
32561 +                       .actor = fillvdir
32562 +               }
32563 +       };
32564 +       struct inode *inode;
32565 +       struct au_vdir *vdir, *allocated;
32566 +
32567 +       err = 0;
32568 +       inode = file_inode(file);
32569 +       IMustLock(inode);
32570 +       IiMustWriteLock(inode);
32571 +       SiMustAnyLock(inode->i_sb);
32572 +
32573 +       allocated = NULL;
32574 +       do_read = 0;
32575 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32576 +       vdir = au_ivdir(inode);
32577 +       if (!vdir) {
32578 +               do_read = 1;
32579 +               vdir = alloc_vdir(file);
32580 +               err = PTR_ERR(vdir);
32581 +               if (IS_ERR(vdir))
32582 +                       goto out;
32583 +               err = 0;
32584 +               allocated = vdir;
32585 +       } else if (may_read
32586 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32587 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32588 +               do_read = 1;
32589 +               err = reinit_vdir(vdir);
32590 +               if (unlikely(err))
32591 +                       goto out;
32592 +       }
32593 +
32594 +       if (!do_read)
32595 +               return 0; /* success */
32596 +
32597 +       arg.file = file;
32598 +       arg.vdir = vdir;
32599 +       err = au_do_read_vdir(&arg);
32600 +       if (!err) {
32601 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32602 +               vdir->vd_version = inode_query_iversion(inode);
32603 +               vdir->vd_last.ul = 0;
32604 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32605 +               if (allocated)
32606 +                       au_set_ivdir(inode, allocated);
32607 +       } else if (allocated)
32608 +               au_vdir_free(allocated);
32609 +
32610 +out:
32611 +       return err;
32612 +}
32613 +
32614 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32615 +{
32616 +       int err, rerr;
32617 +       unsigned long ul, n;
32618 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32619 +
32620 +       AuDebugOn(tgt->vd_nblk != 1);
32621 +
32622 +       err = -ENOMEM;
32623 +       if (tgt->vd_nblk < src->vd_nblk) {
32624 +               unsigned char **p;
32625 +
32626 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32627 +                               GFP_NOFS, /*may_shrink*/0);
32628 +               if (unlikely(!p))
32629 +                       goto out;
32630 +               tgt->vd_deblk = p;
32631 +       }
32632 +
32633 +       if (tgt->vd_deblk_sz != deblk_sz) {
32634 +               unsigned char *p;
32635 +
32636 +               tgt->vd_deblk_sz = deblk_sz;
32637 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32638 +                               /*may_shrink*/1);
32639 +               if (unlikely(!p))
32640 +                       goto out;
32641 +               tgt->vd_deblk[0] = p;
32642 +       }
32643 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32644 +       tgt->vd_version = src->vd_version;
32645 +       tgt->vd_jiffy = src->vd_jiffy;
32646 +
32647 +       n = src->vd_nblk;
32648 +       for (ul = 1; ul < n; ul++) {
32649 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32650 +                                           GFP_NOFS);
32651 +               if (unlikely(!tgt->vd_deblk[ul]))
32652 +                       goto out;
32653 +               tgt->vd_nblk++;
32654 +       }
32655 +       tgt->vd_nblk = n;
32656 +       tgt->vd_last.ul = tgt->vd_last.ul;
32657 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32658 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32659 +               - src->vd_deblk[src->vd_last.ul];
32660 +       /* smp_mb(); */
32661 +       return 0; /* success */
32662 +
32663 +out:
32664 +       rerr = reinit_vdir(tgt);
32665 +       BUG_ON(rerr);
32666 +       return err;
32667 +}
32668 +
32669 +int au_vdir_init(struct file *file)
32670 +{
32671 +       int err;
32672 +       struct inode *inode;
32673 +       struct au_vdir *vdir_cache, *allocated;
32674 +
32675 +       /* test file->f_pos here instead of ctx->pos */
32676 +       err = read_vdir(file, !file->f_pos);
32677 +       if (unlikely(err))
32678 +               goto out;
32679 +
32680 +       allocated = NULL;
32681 +       vdir_cache = au_fvdir_cache(file);
32682 +       if (!vdir_cache) {
32683 +               vdir_cache = alloc_vdir(file);
32684 +               err = PTR_ERR(vdir_cache);
32685 +               if (IS_ERR(vdir_cache))
32686 +                       goto out;
32687 +               allocated = vdir_cache;
32688 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32689 +               /* test file->f_pos here instead of ctx->pos */
32690 +               err = reinit_vdir(vdir_cache);
32691 +               if (unlikely(err))
32692 +                       goto out;
32693 +       } else
32694 +               return 0; /* success */
32695 +
32696 +       inode = file_inode(file);
32697 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32698 +       if (!err) {
32699 +               file->f_version = inode_query_iversion(inode);
32700 +               if (allocated)
32701 +                       au_set_fvdir_cache(file, allocated);
32702 +       } else if (allocated)
32703 +               au_vdir_free(allocated);
32704 +
32705 +out:
32706 +       return err;
32707 +}
32708 +
32709 +static loff_t calc_offset(struct au_vdir *vdir)
32710 +{
32711 +       loff_t offset;
32712 +       union au_vdir_deblk_p p;
32713 +
32714 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32715 +       offset = vdir->vd_last.p.deblk - p.deblk;
32716 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32717 +       return offset;
32718 +}
32719 +
32720 +/* returns true or false */
32721 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32722 +{
32723 +       int valid;
32724 +       unsigned int deblk_sz;
32725 +       unsigned long ul, n;
32726 +       loff_t offset;
32727 +       union au_vdir_deblk_p p, deblk_end;
32728 +       struct au_vdir *vdir_cache;
32729 +
32730 +       valid = 1;
32731 +       vdir_cache = au_fvdir_cache(file);
32732 +       offset = calc_offset(vdir_cache);
32733 +       AuDbg("offset %lld\n", offset);
32734 +       if (ctx->pos == offset)
32735 +               goto out;
32736 +
32737 +       vdir_cache->vd_last.ul = 0;
32738 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32739 +       if (!ctx->pos)
32740 +               goto out;
32741 +
32742 +       valid = 0;
32743 +       deblk_sz = vdir_cache->vd_deblk_sz;
32744 +       ul = div64_u64(ctx->pos, deblk_sz);
32745 +       AuDbg("ul %lu\n", ul);
32746 +       if (ul >= vdir_cache->vd_nblk)
32747 +               goto out;
32748 +
32749 +       n = vdir_cache->vd_nblk;
32750 +       for (; ul < n; ul++) {
32751 +               p.deblk = vdir_cache->vd_deblk[ul];
32752 +               deblk_end.deblk = p.deblk + deblk_sz;
32753 +               offset = ul;
32754 +               offset *= deblk_sz;
32755 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32756 +                       unsigned int l;
32757 +
32758 +                       l = calc_size(p.de->de_str.len);
32759 +                       offset += l;
32760 +                       p.deblk += l;
32761 +               }
32762 +               if (!is_deblk_end(&p, &deblk_end)) {
32763 +                       valid = 1;
32764 +                       vdir_cache->vd_last.ul = ul;
32765 +                       vdir_cache->vd_last.p = p;
32766 +                       break;
32767 +               }
32768 +       }
32769 +
32770 +out:
32771 +       /* smp_mb(); */
32772 +       if (!valid)
32773 +               AuDbg("valid %d\n", !valid);
32774 +       return valid;
32775 +}
32776 +
32777 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32778 +{
32779 +       unsigned int l, deblk_sz;
32780 +       union au_vdir_deblk_p deblk_end;
32781 +       struct au_vdir *vdir_cache;
32782 +       struct au_vdir_de *de;
32783 +
32784 +       if (!seek_vdir(file, ctx))
32785 +               return 0;
32786 +
32787 +       vdir_cache = au_fvdir_cache(file);
32788 +       deblk_sz = vdir_cache->vd_deblk_sz;
32789 +       while (1) {
32790 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32791 +               deblk_end.deblk += deblk_sz;
32792 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32793 +                       de = vdir_cache->vd_last.p.de;
32794 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32795 +                             de->de_str.len, de->de_str.name, ctx->pos,
32796 +                             (unsigned long)de->de_ino, de->de_type);
32797 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32798 +                                              de->de_str.len, de->de_ino,
32799 +                                              de->de_type))) {
32800 +                               /* todo: ignore the error caused by udba? */
32801 +                               /* return err; */
32802 +                               return 0;
32803 +                       }
32804 +
32805 +                       l = calc_size(de->de_str.len);
32806 +                       vdir_cache->vd_last.p.deblk += l;
32807 +                       ctx->pos += l;
32808 +               }
32809 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32810 +                       vdir_cache->vd_last.ul++;
32811 +                       vdir_cache->vd_last.p.deblk
32812 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32813 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32814 +                       continue;
32815 +               }
32816 +               break;
32817 +       }
32818 +
32819 +       /* smp_mb(); */
32820 +       return 0;
32821 +}
32822 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32823 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32824 +++ linux/fs/aufs/vfsub.c       2018-10-23 12:33:35.599375796 +0200
32825 @@ -0,0 +1,902 @@
32826 +// SPDX-License-Identifier: GPL-2.0
32827 +/*
32828 + * Copyright (C) 2005-2018 Junjiro R. Okajima
32829 + *
32830 + * This program, aufs is free software; you can redistribute it and/or modify
32831 + * it under the terms of the GNU General Public License as published by
32832 + * the Free Software Foundation; either version 2 of the License, or
32833 + * (at your option) any later version.
32834 + *
32835 + * This program is distributed in the hope that it will be useful,
32836 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32837 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32838 + * GNU General Public License for more details.
32839 + *
32840 + * You should have received a copy of the GNU General Public License
32841 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32842 + */
32843 +
32844 +/*
32845 + * sub-routines for VFS
32846 + */
32847 +
32848 +#include <linux/mnt_namespace.h>
32849 +#include <linux/namei.h>
32850 +#include <linux/nsproxy.h>
32851 +#include <linux/security.h>
32852 +#include <linux/splice.h>
32853 +#include "aufs.h"
32854 +
32855 +#ifdef CONFIG_AUFS_BR_FUSE
32856 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32857 +{
32858 +       if (!au_test_fuse(h_sb) || !au_userns)
32859 +               return 0;
32860 +
32861 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32862 +}
32863 +#endif
32864 +
32865 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32866 +{
32867 +       int err;
32868 +
32869 +       lockdep_off();
32870 +       down_read(&h_sb->s_umount);
32871 +       err = __sync_filesystem(h_sb, wait);
32872 +       up_read(&h_sb->s_umount);
32873 +       lockdep_on();
32874 +
32875 +       return err;
32876 +}
32877 +
32878 +/* ---------------------------------------------------------------------- */
32879 +
32880 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32881 +{
32882 +       int err;
32883 +       struct kstat st;
32884 +       struct super_block *h_sb;
32885 +
32886 +       /* for remote fs, leave work for its getattr or d_revalidate */
32887 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32888 +       /* still some fs may acquire i_mutex. we need to skip them */
32889 +       err = 0;
32890 +       if (!did)
32891 +               did = &err;
32892 +       h_sb = h_path->dentry->d_sb;
32893 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32894 +       if (*did)
32895 +               err = vfsub_getattr(h_path, &st);
32896 +
32897 +       return err;
32898 +}
32899 +
32900 +/* ---------------------------------------------------------------------- */
32901 +
32902 +struct file *vfsub_dentry_open(struct path *path, int flags)
32903 +{
32904 +       struct file *file;
32905 +
32906 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32907 +                          current_cred());
32908 +       if (!IS_ERR_OR_NULL(file)
32909 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
32910 +               i_readcount_inc(d_inode(path->dentry));
32911 +
32912 +       return file;
32913 +}
32914 +
32915 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32916 +{
32917 +       struct file *file;
32918 +
32919 +       lockdep_off();
32920 +       file = filp_open(path,
32921 +                        oflags /* | __FMODE_NONOTIFY */,
32922 +                        mode);
32923 +       lockdep_on();
32924 +       if (IS_ERR(file))
32925 +               goto out;
32926 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32927 +
32928 +out:
32929 +       return file;
32930 +}
32931 +
32932 +/*
32933 + * Ideally this function should call VFS:do_last() in order to keep all its
32934 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32935 + * structure such as nameidata. This is a second (or third) best approach.
32936 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32937 + */
32938 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32939 +                     struct vfsub_aopen_args *args)
32940 +{
32941 +       int err;
32942 +       struct au_branch *br = args->br;
32943 +       struct file *file = args->file;
32944 +       /* copied from linux/fs/namei.c:atomic_open() */
32945 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32946 +
32947 +       IMustLock(dir);
32948 +       AuDebugOn(!dir->i_op->atomic_open);
32949 +
32950 +       err = au_br_test_oflag(args->open_flag, br);
32951 +       if (unlikely(err))
32952 +               goto out;
32953 +
32954 +       au_lcnt_inc(&br->br_nfiles);
32955 +       file->f_path.dentry = DENTRY_NOT_SET;
32956 +       file->f_path.mnt = au_br_mnt(br);
32957 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32958 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32959 +                                    args->create_mode);
32960 +       if (unlikely(err < 0)) {
32961 +               au_lcnt_dec(&br->br_nfiles);
32962 +               goto out;
32963 +       }
32964 +
32965 +       /* temporary workaround for nfsv4 branch */
32966 +       if (au_test_nfs(dir->i_sb))
32967 +               nfs_mark_for_revalidate(dir);
32968 +
32969 +       if (file->f_mode & FMODE_CREATED)
32970 +               fsnotify_create(dir, dentry);
32971 +       if (!(file->f_mode & FMODE_OPENED)) {
32972 +               au_lcnt_dec(&br->br_nfiles);
32973 +               goto out;
32974 +       }
32975 +
32976 +       /* todo: call VFS:may_open() here */
32977 +       /* todo: ima_file_check() too? */
32978 +       if (!err && (args->open_flag & __FMODE_EXEC))
32979 +               err = deny_write_access(file);
32980 +       if (!err)
32981 +               fsnotify_open(file);
32982 +       else
32983 +               au_lcnt_dec(&br->br_nfiles);
32984 +       /* note that the file is created and still opened */
32985 +
32986 +out:
32987 +       return err;
32988 +}
32989 +
32990 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32991 +{
32992 +       int err;
32993 +
32994 +       err = kern_path(name, flags, path);
32995 +       if (!err && d_is_positive(path->dentry))
32996 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32997 +       return err;
32998 +}
32999 +
33000 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33001 +                                            struct dentry *parent, int len)
33002 +{
33003 +       struct path path = {
33004 +               .mnt = NULL
33005 +       };
33006 +
33007 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
33008 +       if (IS_ERR(path.dentry))
33009 +               goto out;
33010 +       if (d_is_positive(path.dentry))
33011 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33012 +
33013 +out:
33014 +       AuTraceErrPtr(path.dentry);
33015 +       return path.dentry;
33016 +}
33017 +
33018 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33019 +                                   int len)
33020 +{
33021 +       struct path path = {
33022 +               .mnt = NULL
33023 +       };
33024 +
33025 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33026 +       IMustLock(d_inode(parent));
33027 +
33028 +       path.dentry = lookup_one_len(name, parent, len);
33029 +       if (IS_ERR(path.dentry))
33030 +               goto out;
33031 +       if (d_is_positive(path.dentry))
33032 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33033 +
33034 +out:
33035 +       AuTraceErrPtr(path.dentry);
33036 +       return path.dentry;
33037 +}
33038 +
33039 +void vfsub_call_lkup_one(void *args)
33040 +{
33041 +       struct vfsub_lkup_one_args *a = args;
33042 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
33043 +}
33044 +
33045 +/* ---------------------------------------------------------------------- */
33046 +
33047 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33048 +                                struct dentry *d2, struct au_hinode *hdir2)
33049 +{
33050 +       struct dentry *d;
33051 +
33052 +       lockdep_off();
33053 +       d = lock_rename(d1, d2);
33054 +       lockdep_on();
33055 +       au_hn_suspend(hdir1);
33056 +       if (hdir1 != hdir2)
33057 +               au_hn_suspend(hdir2);
33058 +
33059 +       return d;
33060 +}
33061 +
33062 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33063 +                        struct dentry *d2, struct au_hinode *hdir2)
33064 +{
33065 +       au_hn_resume(hdir1);
33066 +       if (hdir1 != hdir2)
33067 +               au_hn_resume(hdir2);
33068 +       lockdep_off();
33069 +       unlock_rename(d1, d2);
33070 +       lockdep_on();
33071 +}
33072 +
33073 +/* ---------------------------------------------------------------------- */
33074 +
33075 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33076 +{
33077 +       int err;
33078 +       struct dentry *d;
33079 +
33080 +       IMustLock(dir);
33081 +
33082 +       d = path->dentry;
33083 +       path->dentry = d->d_parent;
33084 +       err = security_path_mknod(path, d, mode, 0);
33085 +       path->dentry = d;
33086 +       if (unlikely(err))
33087 +               goto out;
33088 +
33089 +       lockdep_off();
33090 +       err = vfs_create(dir, path->dentry, mode, want_excl);
33091 +       lockdep_on();
33092 +       if (!err) {
33093 +               struct path tmp = *path;
33094 +               int did;
33095 +
33096 +               vfsub_update_h_iattr(&tmp, &did);
33097 +               if (did) {
33098 +                       tmp.dentry = path->dentry->d_parent;
33099 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33100 +               }
33101 +               /*ignore*/
33102 +       }
33103 +
33104 +out:
33105 +       return err;
33106 +}
33107 +
33108 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33109 +{
33110 +       int err;
33111 +       struct dentry *d;
33112 +
33113 +       IMustLock(dir);
33114 +
33115 +       d = path->dentry;
33116 +       path->dentry = d->d_parent;
33117 +       err = security_path_symlink(path, d, symname);
33118 +       path->dentry = d;
33119 +       if (unlikely(err))
33120 +               goto out;
33121 +
33122 +       lockdep_off();
33123 +       err = vfs_symlink(dir, path->dentry, symname);
33124 +       lockdep_on();
33125 +       if (!err) {
33126 +               struct path tmp = *path;
33127 +               int did;
33128 +
33129 +               vfsub_update_h_iattr(&tmp, &did);
33130 +               if (did) {
33131 +                       tmp.dentry = path->dentry->d_parent;
33132 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33133 +               }
33134 +               /*ignore*/
33135 +       }
33136 +
33137 +out:
33138 +       return err;
33139 +}
33140 +
33141 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33142 +{
33143 +       int err;
33144 +       struct dentry *d;
33145 +
33146 +       IMustLock(dir);
33147 +
33148 +       d = path->dentry;
33149 +       path->dentry = d->d_parent;
33150 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33151 +       path->dentry = d;
33152 +       if (unlikely(err))
33153 +               goto out;
33154 +
33155 +       lockdep_off();
33156 +       err = vfs_mknod(dir, path->dentry, mode, dev);
33157 +       lockdep_on();
33158 +       if (!err) {
33159 +               struct path tmp = *path;
33160 +               int did;
33161 +
33162 +               vfsub_update_h_iattr(&tmp, &did);
33163 +               if (did) {
33164 +                       tmp.dentry = path->dentry->d_parent;
33165 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33166 +               }
33167 +               /*ignore*/
33168 +       }
33169 +
33170 +out:
33171 +       return err;
33172 +}
33173 +
33174 +static int au_test_nlink(struct inode *inode)
33175 +{
33176 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33177 +
33178 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33179 +           || inode->i_nlink < link_max)
33180 +               return 0;
33181 +       return -EMLINK;
33182 +}
33183 +
33184 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33185 +              struct inode **delegated_inode)
33186 +{
33187 +       int err;
33188 +       struct dentry *d;
33189 +
33190 +       IMustLock(dir);
33191 +
33192 +       err = au_test_nlink(d_inode(src_dentry));
33193 +       if (unlikely(err))
33194 +               return err;
33195 +
33196 +       /* we don't call may_linkat() */
33197 +       d = path->dentry;
33198 +       path->dentry = d->d_parent;
33199 +       err = security_path_link(src_dentry, path, d);
33200 +       path->dentry = d;
33201 +       if (unlikely(err))
33202 +               goto out;
33203 +
33204 +       lockdep_off();
33205 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
33206 +       lockdep_on();
33207 +       if (!err) {
33208 +               struct path tmp = *path;
33209 +               int did;
33210 +
33211 +               /* fuse has different memory inode for the same inumber */
33212 +               vfsub_update_h_iattr(&tmp, &did);
33213 +               if (did) {
33214 +                       tmp.dentry = path->dentry->d_parent;
33215 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33216 +                       tmp.dentry = src_dentry;
33217 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33218 +               }
33219 +               /*ignore*/
33220 +       }
33221 +
33222 +out:
33223 +       return err;
33224 +}
33225 +
33226 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33227 +                struct inode *dir, struct path *path,
33228 +                struct inode **delegated_inode, unsigned int flags)
33229 +{
33230 +       int err;
33231 +       struct path tmp = {
33232 +               .mnt    = path->mnt
33233 +       };
33234 +       struct dentry *d;
33235 +
33236 +       IMustLock(dir);
33237 +       IMustLock(src_dir);
33238 +
33239 +       d = path->dentry;
33240 +       path->dentry = d->d_parent;
33241 +       tmp.dentry = src_dentry->d_parent;
33242 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33243 +       path->dentry = d;
33244 +       if (unlikely(err))
33245 +               goto out;
33246 +
33247 +       lockdep_off();
33248 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33249 +                        delegated_inode, flags);
33250 +       lockdep_on();
33251 +       if (!err) {
33252 +               int did;
33253 +
33254 +               tmp.dentry = d->d_parent;
33255 +               vfsub_update_h_iattr(&tmp, &did);
33256 +               if (did) {
33257 +                       tmp.dentry = src_dentry;
33258 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33259 +                       tmp.dentry = src_dentry->d_parent;
33260 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33261 +               }
33262 +               /*ignore*/
33263 +       }
33264 +
33265 +out:
33266 +       return err;
33267 +}
33268 +
33269 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33270 +{
33271 +       int err;
33272 +       struct dentry *d;
33273 +
33274 +       IMustLock(dir);
33275 +
33276 +       d = path->dentry;
33277 +       path->dentry = d->d_parent;
33278 +       err = security_path_mkdir(path, d, mode);
33279 +       path->dentry = d;
33280 +       if (unlikely(err))
33281 +               goto out;
33282 +
33283 +       lockdep_off();
33284 +       err = vfs_mkdir(dir, path->dentry, mode);
33285 +       lockdep_on();
33286 +       if (!err) {
33287 +               struct path tmp = *path;
33288 +               int did;
33289 +
33290 +               vfsub_update_h_iattr(&tmp, &did);
33291 +               if (did) {
33292 +                       tmp.dentry = path->dentry->d_parent;
33293 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33294 +               }
33295 +               /*ignore*/
33296 +       }
33297 +
33298 +out:
33299 +       return err;
33300 +}
33301 +
33302 +int vfsub_rmdir(struct inode *dir, struct path *path)
33303 +{
33304 +       int err;
33305 +       struct dentry *d;
33306 +
33307 +       IMustLock(dir);
33308 +
33309 +       d = path->dentry;
33310 +       path->dentry = d->d_parent;
33311 +       err = security_path_rmdir(path, d);
33312 +       path->dentry = d;
33313 +       if (unlikely(err))
33314 +               goto out;
33315 +
33316 +       lockdep_off();
33317 +       err = vfs_rmdir(dir, path->dentry);
33318 +       lockdep_on();
33319 +       if (!err) {
33320 +               struct path tmp = {
33321 +                       .dentry = path->dentry->d_parent,
33322 +                       .mnt    = path->mnt
33323 +               };
33324 +
33325 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33326 +       }
33327 +
33328 +out:
33329 +       return err;
33330 +}
33331 +
33332 +/* ---------------------------------------------------------------------- */
33333 +
33334 +/* todo: support mmap_sem? */
33335 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33336 +                    loff_t *ppos)
33337 +{
33338 +       ssize_t err;
33339 +
33340 +       lockdep_off();
33341 +       err = vfs_read(file, ubuf, count, ppos);
33342 +       lockdep_on();
33343 +       if (err >= 0)
33344 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33345 +       return err;
33346 +}
33347 +
33348 +/* todo: kernel_read()? */
33349 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33350 +                    loff_t *ppos)
33351 +{
33352 +       ssize_t err;
33353 +       mm_segment_t oldfs;
33354 +       union {
33355 +               void *k;
33356 +               char __user *u;
33357 +       } buf;
33358 +
33359 +       buf.k = kbuf;
33360 +       oldfs = get_fs();
33361 +       set_fs(KERNEL_DS);
33362 +       err = vfsub_read_u(file, buf.u, count, ppos);
33363 +       set_fs(oldfs);
33364 +       return err;
33365 +}
33366 +
33367 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33368 +                     loff_t *ppos)
33369 +{
33370 +       ssize_t err;
33371 +
33372 +       lockdep_off();
33373 +       err = vfs_write(file, ubuf, count, ppos);
33374 +       lockdep_on();
33375 +       if (err >= 0)
33376 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33377 +       return err;
33378 +}
33379 +
33380 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33381 +{
33382 +       ssize_t err;
33383 +       mm_segment_t oldfs;
33384 +       union {
33385 +               void *k;
33386 +               const char __user *u;
33387 +       } buf;
33388 +
33389 +       buf.k = kbuf;
33390 +       oldfs = get_fs();
33391 +       set_fs(KERNEL_DS);
33392 +       err = vfsub_write_u(file, buf.u, count, ppos);
33393 +       set_fs(oldfs);
33394 +       return err;
33395 +}
33396 +
33397 +int vfsub_flush(struct file *file, fl_owner_t id)
33398 +{
33399 +       int err;
33400 +
33401 +       err = 0;
33402 +       if (file->f_op->flush) {
33403 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33404 +                       err = file->f_op->flush(file, id);
33405 +               else {
33406 +                       lockdep_off();
33407 +                       err = file->f_op->flush(file, id);
33408 +                       lockdep_on();
33409 +               }
33410 +               if (!err)
33411 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33412 +               /*ignore*/
33413 +       }
33414 +       return err;
33415 +}
33416 +
33417 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33418 +{
33419 +       int err;
33420 +
33421 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33422 +
33423 +       lockdep_off();
33424 +       err = iterate_dir(file, ctx);
33425 +       lockdep_on();
33426 +       if (err >= 0)
33427 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33428 +
33429 +       return err;
33430 +}
33431 +
33432 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33433 +                    struct pipe_inode_info *pipe, size_t len,
33434 +                    unsigned int flags)
33435 +{
33436 +       long err;
33437 +
33438 +       lockdep_off();
33439 +       err = do_splice_to(in, ppos, pipe, len, flags);
33440 +       lockdep_on();
33441 +       file_accessed(in);
33442 +       if (err >= 0)
33443 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33444 +       return err;
33445 +}
33446 +
33447 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33448 +                      loff_t *ppos, size_t len, unsigned int flags)
33449 +{
33450 +       long err;
33451 +
33452 +       lockdep_off();
33453 +       err = do_splice_from(pipe, out, ppos, len, flags);
33454 +       lockdep_on();
33455 +       if (err >= 0)
33456 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33457 +       return err;
33458 +}
33459 +
33460 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33461 +{
33462 +       int err;
33463 +
33464 +       /* file can be NULL */
33465 +       lockdep_off();
33466 +       err = vfs_fsync(file, datasync);
33467 +       lockdep_on();
33468 +       if (!err) {
33469 +               if (!path) {
33470 +                       AuDebugOn(!file);
33471 +                       path = &file->f_path;
33472 +               }
33473 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33474 +       }
33475 +       return err;
33476 +}
33477 +
33478 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33479 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33480 +               struct file *h_file)
33481 +{
33482 +       int err;
33483 +       struct inode *h_inode;
33484 +       struct super_block *h_sb;
33485 +
33486 +       if (!h_file) {
33487 +               err = vfsub_truncate(h_path, length);
33488 +               goto out;
33489 +       }
33490 +
33491 +       h_inode = d_inode(h_path->dentry);
33492 +       h_sb = h_inode->i_sb;
33493 +       lockdep_off();
33494 +       sb_start_write(h_sb);
33495 +       lockdep_on();
33496 +       err = locks_verify_truncate(h_inode, h_file, length);
33497 +       if (!err)
33498 +               err = security_path_truncate(h_path);
33499 +       if (!err) {
33500 +               lockdep_off();
33501 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33502 +               lockdep_on();
33503 +       }
33504 +       lockdep_off();
33505 +       sb_end_write(h_sb);
33506 +       lockdep_on();
33507 +
33508 +out:
33509 +       return err;
33510 +}
33511 +
33512 +/* ---------------------------------------------------------------------- */
33513 +
33514 +struct au_vfsub_mkdir_args {
33515 +       int *errp;
33516 +       struct inode *dir;
33517 +       struct path *path;
33518 +       int mode;
33519 +};
33520 +
33521 +static void au_call_vfsub_mkdir(void *args)
33522 +{
33523 +       struct au_vfsub_mkdir_args *a = args;
33524 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33525 +}
33526 +
33527 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33528 +{
33529 +       int err, do_sio, wkq_err;
33530 +
33531 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33532 +       if (!do_sio) {
33533 +               lockdep_off();
33534 +               err = vfsub_mkdir(dir, path, mode);
33535 +               lockdep_on();
33536 +       } else {
33537 +               struct au_vfsub_mkdir_args args = {
33538 +                       .errp   = &err,
33539 +                       .dir    = dir,
33540 +                       .path   = path,
33541 +                       .mode   = mode
33542 +               };
33543 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33544 +               if (unlikely(wkq_err))
33545 +                       err = wkq_err;
33546 +       }
33547 +
33548 +       return err;
33549 +}
33550 +
33551 +struct au_vfsub_rmdir_args {
33552 +       int *errp;
33553 +       struct inode *dir;
33554 +       struct path *path;
33555 +};
33556 +
33557 +static void au_call_vfsub_rmdir(void *args)
33558 +{
33559 +       struct au_vfsub_rmdir_args *a = args;
33560 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33561 +}
33562 +
33563 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33564 +{
33565 +       int err, do_sio, wkq_err;
33566 +
33567 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33568 +       if (!do_sio) {
33569 +               lockdep_off();
33570 +               err = vfsub_rmdir(dir, path);
33571 +               lockdep_on();
33572 +       } else {
33573 +               struct au_vfsub_rmdir_args args = {
33574 +                       .errp   = &err,
33575 +                       .dir    = dir,
33576 +                       .path   = path
33577 +               };
33578 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33579 +               if (unlikely(wkq_err))
33580 +                       err = wkq_err;
33581 +       }
33582 +
33583 +       return err;
33584 +}
33585 +
33586 +/* ---------------------------------------------------------------------- */
33587 +
33588 +struct notify_change_args {
33589 +       int *errp;
33590 +       struct path *path;
33591 +       struct iattr *ia;
33592 +       struct inode **delegated_inode;
33593 +};
33594 +
33595 +static void call_notify_change(void *args)
33596 +{
33597 +       struct notify_change_args *a = args;
33598 +       struct inode *h_inode;
33599 +
33600 +       h_inode = d_inode(a->path->dentry);
33601 +       IMustLock(h_inode);
33602 +
33603 +       *a->errp = -EPERM;
33604 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33605 +               lockdep_off();
33606 +               *a->errp = notify_change(a->path->dentry, a->ia,
33607 +                                        a->delegated_inode);
33608 +               lockdep_on();
33609 +               if (!*a->errp)
33610 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33611 +       }
33612 +       AuTraceErr(*a->errp);
33613 +}
33614 +
33615 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33616 +                       struct inode **delegated_inode)
33617 +{
33618 +       int err;
33619 +       struct notify_change_args args = {
33620 +               .errp                   = &err,
33621 +               .path                   = path,
33622 +               .ia                     = ia,
33623 +               .delegated_inode        = delegated_inode
33624 +       };
33625 +
33626 +       call_notify_change(&args);
33627 +
33628 +       return err;
33629 +}
33630 +
33631 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33632 +                           struct inode **delegated_inode)
33633 +{
33634 +       int err, wkq_err;
33635 +       struct notify_change_args args = {
33636 +               .errp                   = &err,
33637 +               .path                   = path,
33638 +               .ia                     = ia,
33639 +               .delegated_inode        = delegated_inode
33640 +       };
33641 +
33642 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33643 +       if (unlikely(wkq_err))
33644 +               err = wkq_err;
33645 +
33646 +       return err;
33647 +}
33648 +
33649 +/* ---------------------------------------------------------------------- */
33650 +
33651 +struct unlink_args {
33652 +       int *errp;
33653 +       struct inode *dir;
33654 +       struct path *path;
33655 +       struct inode **delegated_inode;
33656 +};
33657 +
33658 +static void call_unlink(void *args)
33659 +{
33660 +       struct unlink_args *a = args;
33661 +       struct dentry *d = a->path->dentry;
33662 +       struct inode *h_inode;
33663 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33664 +                                     && au_dcount(d) == 1);
33665 +
33666 +       IMustLock(a->dir);
33667 +
33668 +       a->path->dentry = d->d_parent;
33669 +       *a->errp = security_path_unlink(a->path, d);
33670 +       a->path->dentry = d;
33671 +       if (unlikely(*a->errp))
33672 +               return;
33673 +
33674 +       if (!stop_sillyrename)
33675 +               dget(d);
33676 +       h_inode = NULL;
33677 +       if (d_is_positive(d)) {
33678 +               h_inode = d_inode(d);
33679 +               ihold(h_inode);
33680 +       }
33681 +
33682 +       lockdep_off();
33683 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33684 +       lockdep_on();
33685 +       if (!*a->errp) {
33686 +               struct path tmp = {
33687 +                       .dentry = d->d_parent,
33688 +                       .mnt    = a->path->mnt
33689 +               };
33690 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33691 +       }
33692 +
33693 +       if (!stop_sillyrename)
33694 +               dput(d);
33695 +       if (h_inode)
33696 +               iput(h_inode);
33697 +
33698 +       AuTraceErr(*a->errp);
33699 +}
33700 +
33701 +/*
33702 + * @dir: must be locked.
33703 + * @dentry: target dentry.
33704 + */
33705 +int vfsub_unlink(struct inode *dir, struct path *path,
33706 +                struct inode **delegated_inode, int force)
33707 +{
33708 +       int err;
33709 +       struct unlink_args args = {
33710 +               .errp                   = &err,
33711 +               .dir                    = dir,
33712 +               .path                   = path,
33713 +               .delegated_inode        = delegated_inode
33714 +       };
33715 +
33716 +       if (!force)
33717 +               call_unlink(&args);
33718 +       else {
33719 +               int wkq_err;
33720 +
33721 +               wkq_err = au_wkq_wait(call_unlink, &args);
33722 +               if (unlikely(wkq_err))
33723 +                       err = wkq_err;
33724 +       }
33725 +
33726 +       return err;
33727 +}
33728 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33729 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33730 +++ linux/fs/aufs/vfsub.h       2018-12-27 13:19:17.711749485 +0100
33731 @@ -0,0 +1,355 @@
33732 +/* SPDX-License-Identifier: GPL-2.0 */
33733 +/*
33734 + * Copyright (C) 2005-2018 Junjiro R. Okajima
33735 + *
33736 + * This program, aufs is free software; you can redistribute it and/or modify
33737 + * it under the terms of the GNU General Public License as published by
33738 + * the Free Software Foundation; either version 2 of the License, or
33739 + * (at your option) any later version.
33740 + *
33741 + * This program is distributed in the hope that it will be useful,
33742 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33743 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33744 + * GNU General Public License for more details.
33745 + *
33746 + * You should have received a copy of the GNU General Public License
33747 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33748 + */
33749 +
33750 +/*
33751 + * sub-routines for VFS
33752 + */
33753 +
33754 +#ifndef __AUFS_VFSUB_H__
33755 +#define __AUFS_VFSUB_H__
33756 +
33757 +#ifdef __KERNEL__
33758 +
33759 +#include <linux/fs.h>
33760 +#include <linux/iversion.h>
33761 +#include <linux/mount.h>
33762 +#include <linux/posix_acl.h>
33763 +#include <linux/xattr.h>
33764 +#include "debug.h"
33765 +
33766 +/* copied from linux/fs/internal.h */
33767 +/* todo: BAD approach!! */
33768 +extern void __mnt_drop_write(struct vfsmount *);
33769 +extern struct file *alloc_empty_file(int, const struct cred *);
33770 +
33771 +/* ---------------------------------------------------------------------- */
33772 +
33773 +/* lock subclass for lower inode */
33774 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33775 +/* reduce? gave up. */
33776 +enum {
33777 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33778 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33779 +       AuLsc_I_PARENT2,        /* copyup dirs */
33780 +       AuLsc_I_PARENT3,        /* copyup wh */
33781 +       AuLsc_I_CHILD,
33782 +       AuLsc_I_CHILD2,
33783 +       AuLsc_I_End
33784 +};
33785 +
33786 +/* to debug easier, do not make them inlined functions */
33787 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33788 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33789 +
33790 +/* ---------------------------------------------------------------------- */
33791 +
33792 +static inline void vfsub_drop_nlink(struct inode *inode)
33793 +{
33794 +       AuDebugOn(!inode->i_nlink);
33795 +       drop_nlink(inode);
33796 +}
33797 +
33798 +static inline void vfsub_dead_dir(struct inode *inode)
33799 +{
33800 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33801 +       inode->i_flags |= S_DEAD;
33802 +       clear_nlink(inode);
33803 +}
33804 +
33805 +static inline int vfsub_native_ro(struct inode *inode)
33806 +{
33807 +       return sb_rdonly(inode->i_sb)
33808 +               || IS_RDONLY(inode)
33809 +               /* || IS_APPEND(inode) */
33810 +               || IS_IMMUTABLE(inode);
33811 +}
33812 +
33813 +#ifdef CONFIG_AUFS_BR_FUSE
33814 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33815 +#else
33816 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33817 +#endif
33818 +
33819 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33820 +
33821 +/* ---------------------------------------------------------------------- */
33822 +
33823 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33824 +struct file *vfsub_dentry_open(struct path *path, int flags);
33825 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33826 +struct au_branch;
33827 +struct vfsub_aopen_args {
33828 +       struct file             *file;
33829 +       unsigned int            open_flag;
33830 +       umode_t                 create_mode;
33831 +       struct au_branch        *br;
33832 +};
33833 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33834 +                     struct vfsub_aopen_args *args);
33835 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33836 +
33837 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33838 +                                            struct dentry *parent, int len);
33839 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33840 +                                   int len);
33841 +
33842 +struct vfsub_lkup_one_args {
33843 +       struct dentry **errp;
33844 +       struct qstr *name;
33845 +       struct dentry *parent;
33846 +};
33847 +
33848 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33849 +                                           struct dentry *parent)
33850 +{
33851 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33852 +}
33853 +
33854 +void vfsub_call_lkup_one(void *args);
33855 +
33856 +/* ---------------------------------------------------------------------- */
33857 +
33858 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33859 +{
33860 +       int err;
33861 +
33862 +       lockdep_off();
33863 +       err = mnt_want_write(mnt);
33864 +       lockdep_on();
33865 +       return err;
33866 +}
33867 +
33868 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33869 +{
33870 +       lockdep_off();
33871 +       mnt_drop_write(mnt);
33872 +       lockdep_on();
33873 +}
33874 +
33875 +#if 0 /* reserved */
33876 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33877 +{
33878 +       lockdep_off();
33879 +       mnt_drop_write_file(file);
33880 +       lockdep_on();
33881 +}
33882 +#endif
33883 +
33884 +/* ---------------------------------------------------------------------- */
33885 +
33886 +struct au_hinode;
33887 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33888 +                                struct dentry *d2, struct au_hinode *hdir2);
33889 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33890 +                        struct dentry *d2, struct au_hinode *hdir2);
33891 +
33892 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33893 +                bool want_excl);
33894 +int vfsub_symlink(struct inode *dir, struct path *path,
33895 +                 const char *symname);
33896 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33897 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33898 +              struct path *path, struct inode **delegated_inode);
33899 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33900 +                struct inode *hdir, struct path *path,
33901 +                struct inode **delegated_inode, unsigned int flags);
33902 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33903 +int vfsub_rmdir(struct inode *dir, struct path *path);
33904 +
33905 +/* ---------------------------------------------------------------------- */
33906 +
33907 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33908 +                    loff_t *ppos);
33909 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33910 +                       loff_t *ppos);
33911 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33912 +                     loff_t *ppos);
33913 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33914 +                     loff_t *ppos);
33915 +int vfsub_flush(struct file *file, fl_owner_t id);
33916 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33917 +
33918 +static inline loff_t vfsub_f_size_read(struct file *file)
33919 +{
33920 +       return i_size_read(file_inode(file));
33921 +}
33922 +
33923 +static inline unsigned int vfsub_file_flags(struct file *file)
33924 +{
33925 +       unsigned int flags;
33926 +
33927 +       spin_lock(&file->f_lock);
33928 +       flags = file->f_flags;
33929 +       spin_unlock(&file->f_lock);
33930 +
33931 +       return flags;
33932 +}
33933 +
33934 +static inline int vfsub_file_execed(struct file *file)
33935 +{
33936 +       /* todo: direct access f_flags */
33937 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33938 +}
33939 +
33940 +#if 0 /* reserved */
33941 +static inline void vfsub_file_accessed(struct file *h_file)
33942 +{
33943 +       file_accessed(h_file);
33944 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33945 +}
33946 +#endif
33947 +
33948 +#if 0 /* reserved */
33949 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33950 +                                    struct dentry *h_dentry)
33951 +{
33952 +       struct path h_path = {
33953 +               .dentry = h_dentry,
33954 +               .mnt    = h_mnt
33955 +       };
33956 +       touch_atime(&h_path);
33957 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33958 +}
33959 +#endif
33960 +
33961 +static inline int vfsub_update_time(struct inode *h_inode,
33962 +                                   struct timespec64 *ts, int flags)
33963 +{
33964 +       return update_time(h_inode, ts, flags);
33965 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33966 +}
33967 +
33968 +#ifdef CONFIG_FS_POSIX_ACL
33969 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33970 +{
33971 +       int err;
33972 +
33973 +       err = posix_acl_chmod(h_inode, h_mode);
33974 +       if (err == -EOPNOTSUPP)
33975 +               err = 0;
33976 +       return err;
33977 +}
33978 +#else
33979 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33980 +#endif
33981 +
33982 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33983 +                    struct pipe_inode_info *pipe, size_t len,
33984 +                    unsigned int flags);
33985 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33986 +                      loff_t *ppos, size_t len, unsigned int flags);
33987 +
33988 +static inline long vfsub_truncate(struct path *path, loff_t length)
33989 +{
33990 +       long err;
33991 +
33992 +       lockdep_off();
33993 +       err = vfs_truncate(path, length);
33994 +       lockdep_on();
33995 +       return err;
33996 +}
33997 +
33998 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33999 +               struct file *h_file);
34000 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34001 +
34002 +/*
34003 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34004 + * ioctl.
34005 + */
34006 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34007 +                                           loff_t len)
34008 +{
34009 +       loff_t err;
34010 +
34011 +       lockdep_off();
34012 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34013 +       lockdep_on();
34014 +
34015 +       return err;
34016 +}
34017 +
34018 +/* copy_file_range(2) is a systemcall */
34019 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34020 +                                           struct file *dst, loff_t dst_pos,
34021 +                                           size_t len, unsigned int flags)
34022 +{
34023 +       ssize_t ssz;
34024 +
34025 +       lockdep_off();
34026 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34027 +       lockdep_on();
34028 +
34029 +       return ssz;
34030 +}
34031 +
34032 +/* ---------------------------------------------------------------------- */
34033 +
34034 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34035 +{
34036 +       loff_t err;
34037 +
34038 +       lockdep_off();
34039 +       err = vfs_llseek(file, offset, origin);
34040 +       lockdep_on();
34041 +       return err;
34042 +}
34043 +
34044 +/* ---------------------------------------------------------------------- */
34045 +
34046 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34047 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34048 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34049 +                           struct inode **delegated_inode);
34050 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34051 +                       struct inode **delegated_inode);
34052 +int vfsub_unlink(struct inode *dir, struct path *path,
34053 +                struct inode **delegated_inode, int force);
34054 +
34055 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34056 +{
34057 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34058 +}
34059 +
34060 +/* ---------------------------------------------------------------------- */
34061 +
34062 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
34063 +                                const void *value, size_t size, int flags)
34064 +{
34065 +       int err;
34066 +
34067 +       lockdep_off();
34068 +       err = vfs_setxattr(dentry, name, value, size, flags);
34069 +       lockdep_on();
34070 +
34071 +       return err;
34072 +}
34073 +
34074 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
34075 +{
34076 +       int err;
34077 +
34078 +       lockdep_off();
34079 +       err = vfs_removexattr(dentry, name);
34080 +       lockdep_on();
34081 +
34082 +       return err;
34083 +}
34084 +
34085 +#endif /* __KERNEL__ */
34086 +#endif /* __AUFS_VFSUB_H__ */
34087 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34088 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34089 +++ linux/fs/aufs/wbr_policy.c  2018-12-27 13:19:17.711749485 +0100
34090 @@ -0,0 +1,830 @@
34091 +// SPDX-License-Identifier: GPL-2.0
34092 +/*
34093 + * Copyright (C) 2005-2018 Junjiro R. Okajima
34094 + *
34095 + * This program, aufs is free software; you can redistribute it and/or modify
34096 + * it under the terms of the GNU General Public License as published by
34097 + * the Free Software Foundation; either version 2 of the License, or
34098 + * (at your option) any later version.
34099 + *
34100 + * This program is distributed in the hope that it will be useful,
34101 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34102 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34103 + * GNU General Public License for more details.
34104 + *
34105 + * You should have received a copy of the GNU General Public License
34106 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34107 + */
34108 +
34109 +/*
34110 + * policies for selecting one among multiple writable branches
34111 + */
34112 +
34113 +#include <linux/statfs.h>
34114 +#include "aufs.h"
34115 +
34116 +/* subset of cpup_attr() */
34117 +static noinline_for_stack
34118 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34119 +{
34120 +       int err, sbits;
34121 +       struct iattr ia;
34122 +       struct inode *h_isrc;
34123 +
34124 +       h_isrc = d_inode(h_src);
34125 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34126 +       ia.ia_mode = h_isrc->i_mode;
34127 +       ia.ia_uid = h_isrc->i_uid;
34128 +       ia.ia_gid = h_isrc->i_gid;
34129 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34130 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34131 +       /* no delegation since it is just created */
34132 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34133 +
34134 +       /* is this nfs only? */
34135 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34136 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34137 +               ia.ia_mode = h_isrc->i_mode;
34138 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34139 +       }
34140 +
34141 +       return err;
34142 +}
34143 +
34144 +#define AuCpdown_PARENT_OPQ    1
34145 +#define AuCpdown_WHED          (1 << 1)
34146 +#define AuCpdown_MADE_DIR      (1 << 2)
34147 +#define AuCpdown_DIROPQ                (1 << 3)
34148 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34149 +#define au_fset_cpdown(flags, name) \
34150 +       do { (flags) |= AuCpdown_##name; } while (0)
34151 +#define au_fclr_cpdown(flags, name) \
34152 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34153 +
34154 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34155 +                            unsigned int *flags)
34156 +{
34157 +       int err;
34158 +       struct dentry *opq_dentry;
34159 +
34160 +       opq_dentry = au_diropq_create(dentry, bdst);
34161 +       err = PTR_ERR(opq_dentry);
34162 +       if (IS_ERR(opq_dentry))
34163 +               goto out;
34164 +       dput(opq_dentry);
34165 +       au_fset_cpdown(*flags, DIROPQ);
34166 +
34167 +out:
34168 +       return err;
34169 +}
34170 +
34171 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34172 +                           struct inode *dir, aufs_bindex_t bdst)
34173 +{
34174 +       int err;
34175 +       struct path h_path;
34176 +       struct au_branch *br;
34177 +
34178 +       br = au_sbr(dentry->d_sb, bdst);
34179 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34180 +       err = PTR_ERR(h_path.dentry);
34181 +       if (IS_ERR(h_path.dentry))
34182 +               goto out;
34183 +
34184 +       err = 0;
34185 +       if (d_is_positive(h_path.dentry)) {
34186 +               h_path.mnt = au_br_mnt(br);
34187 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34188 +                                         dentry);
34189 +       }
34190 +       dput(h_path.dentry);
34191 +
34192 +out:
34193 +       return err;
34194 +}
34195 +
34196 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34197 +                        struct au_pin *pin,
34198 +                        struct dentry *h_parent, void *arg)
34199 +{
34200 +       int err, rerr;
34201 +       aufs_bindex_t bopq, btop;
34202 +       struct path h_path;
34203 +       struct dentry *parent;
34204 +       struct inode *h_dir, *h_inode, *inode, *dir;
34205 +       unsigned int *flags = arg;
34206 +
34207 +       btop = au_dbtop(dentry);
34208 +       /* dentry is di-locked */
34209 +       parent = dget_parent(dentry);
34210 +       dir = d_inode(parent);
34211 +       h_dir = d_inode(h_parent);
34212 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34213 +       IMustLock(h_dir);
34214 +
34215 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34216 +       if (unlikely(err < 0))
34217 +               goto out;
34218 +       h_path.dentry = au_h_dptr(dentry, bdst);
34219 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34220 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34221 +       if (unlikely(err))
34222 +               goto out_put;
34223 +       au_fset_cpdown(*flags, MADE_DIR);
34224 +
34225 +       bopq = au_dbdiropq(dentry);
34226 +       au_fclr_cpdown(*flags, WHED);
34227 +       au_fclr_cpdown(*flags, DIROPQ);
34228 +       if (au_dbwh(dentry) == bdst)
34229 +               au_fset_cpdown(*flags, WHED);
34230 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34231 +               au_fset_cpdown(*flags, PARENT_OPQ);
34232 +       h_inode = d_inode(h_path.dentry);
34233 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34234 +       if (au_ftest_cpdown(*flags, WHED)) {
34235 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34236 +               if (unlikely(err)) {
34237 +                       inode_unlock(h_inode);
34238 +                       goto out_dir;
34239 +               }
34240 +       }
34241 +
34242 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34243 +       inode_unlock(h_inode);
34244 +       if (unlikely(err))
34245 +               goto out_opq;
34246 +
34247 +       if (au_ftest_cpdown(*flags, WHED)) {
34248 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34249 +               if (unlikely(err))
34250 +                       goto out_opq;
34251 +       }
34252 +
34253 +       inode = d_inode(dentry);
34254 +       if (au_ibbot(inode) < bdst)
34255 +               au_set_ibbot(inode, bdst);
34256 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34257 +                     au_hi_flags(inode, /*isdir*/1));
34258 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34259 +       goto out; /* success */
34260 +
34261 +       /* revert */
34262 +out_opq:
34263 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34264 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34265 +               rerr = au_diropq_remove(dentry, bdst);
34266 +               inode_unlock(h_inode);
34267 +               if (unlikely(rerr)) {
34268 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34269 +                               dentry, bdst, rerr);
34270 +                       err = -EIO;
34271 +                       goto out;
34272 +               }
34273 +       }
34274 +out_dir:
34275 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34276 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34277 +               if (unlikely(rerr)) {
34278 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34279 +                               dentry, bdst, rerr);
34280 +                       err = -EIO;
34281 +               }
34282 +       }
34283 +out_put:
34284 +       au_set_h_dptr(dentry, bdst, NULL);
34285 +       if (au_dbbot(dentry) == bdst)
34286 +               au_update_dbbot(dentry);
34287 +out:
34288 +       dput(parent);
34289 +       return err;
34290 +}
34291 +
34292 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34293 +{
34294 +       int err;
34295 +       unsigned int flags;
34296 +
34297 +       flags = 0;
34298 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34299 +
34300 +       return err;
34301 +}
34302 +
34303 +/* ---------------------------------------------------------------------- */
34304 +
34305 +/* policies for create */
34306 +
34307 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34308 +{
34309 +       int err, i, j, ndentry;
34310 +       aufs_bindex_t bopq;
34311 +       struct au_dcsub_pages dpages;
34312 +       struct au_dpage *dpage;
34313 +       struct dentry **dentries, *parent, *d;
34314 +
34315 +       err = au_dpages_init(&dpages, GFP_NOFS);
34316 +       if (unlikely(err))
34317 +               goto out;
34318 +       parent = dget_parent(dentry);
34319 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34320 +       if (unlikely(err))
34321 +               goto out_free;
34322 +
34323 +       err = bindex;
34324 +       for (i = 0; i < dpages.ndpage; i++) {
34325 +               dpage = dpages.dpages + i;
34326 +               dentries = dpage->dentries;
34327 +               ndentry = dpage->ndentry;
34328 +               for (j = 0; j < ndentry; j++) {
34329 +                       d = dentries[j];
34330 +                       di_read_lock_parent2(d, !AuLock_IR);
34331 +                       bopq = au_dbdiropq(d);
34332 +                       di_read_unlock(d, !AuLock_IR);
34333 +                       if (bopq >= 0 && bopq < err)
34334 +                               err = bopq;
34335 +               }
34336 +       }
34337 +
34338 +out_free:
34339 +       dput(parent);
34340 +       au_dpages_free(&dpages);
34341 +out:
34342 +       return err;
34343 +}
34344 +
34345 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34346 +{
34347 +       for (; bindex >= 0; bindex--)
34348 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34349 +                       return bindex;
34350 +       return -EROFS;
34351 +}
34352 +
34353 +/* top down parent */
34354 +static int au_wbr_create_tdp(struct dentry *dentry,
34355 +                            unsigned int flags __maybe_unused)
34356 +{
34357 +       int err;
34358 +       aufs_bindex_t btop, bindex;
34359 +       struct super_block *sb;
34360 +       struct dentry *parent, *h_parent;
34361 +
34362 +       sb = dentry->d_sb;
34363 +       btop = au_dbtop(dentry);
34364 +       err = btop;
34365 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34366 +               goto out;
34367 +
34368 +       err = -EROFS;
34369 +       parent = dget_parent(dentry);
34370 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34371 +               h_parent = au_h_dptr(parent, bindex);
34372 +               if (!h_parent || d_is_negative(h_parent))
34373 +                       continue;
34374 +
34375 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34376 +                       err = bindex;
34377 +                       break;
34378 +               }
34379 +       }
34380 +       dput(parent);
34381 +
34382 +       /* bottom up here */
34383 +       if (unlikely(err < 0)) {
34384 +               err = au_wbr_bu(sb, btop - 1);
34385 +               if (err >= 0)
34386 +                       err = au_wbr_nonopq(dentry, err);
34387 +       }
34388 +
34389 +out:
34390 +       AuDbg("b%d\n", err);
34391 +       return err;
34392 +}
34393 +
34394 +/* ---------------------------------------------------------------------- */
34395 +
34396 +/* an exception for the policy other than tdp */
34397 +static int au_wbr_create_exp(struct dentry *dentry)
34398 +{
34399 +       int err;
34400 +       aufs_bindex_t bwh, bdiropq;
34401 +       struct dentry *parent;
34402 +
34403 +       err = -1;
34404 +       bwh = au_dbwh(dentry);
34405 +       parent = dget_parent(dentry);
34406 +       bdiropq = au_dbdiropq(parent);
34407 +       if (bwh >= 0) {
34408 +               if (bdiropq >= 0)
34409 +                       err = min(bdiropq, bwh);
34410 +               else
34411 +                       err = bwh;
34412 +               AuDbg("%d\n", err);
34413 +       } else if (bdiropq >= 0) {
34414 +               err = bdiropq;
34415 +               AuDbg("%d\n", err);
34416 +       }
34417 +       dput(parent);
34418 +
34419 +       if (err >= 0)
34420 +               err = au_wbr_nonopq(dentry, err);
34421 +
34422 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34423 +               err = -1;
34424 +
34425 +       AuDbg("%d\n", err);
34426 +       return err;
34427 +}
34428 +
34429 +/* ---------------------------------------------------------------------- */
34430 +
34431 +/* round robin */
34432 +static int au_wbr_create_init_rr(struct super_block *sb)
34433 +{
34434 +       int err;
34435 +
34436 +       err = au_wbr_bu(sb, au_sbbot(sb));
34437 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34438 +       /* smp_mb(); */
34439 +
34440 +       AuDbg("b%d\n", err);
34441 +       return err;
34442 +}
34443 +
34444 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34445 +{
34446 +       int err, nbr;
34447 +       unsigned int u;
34448 +       aufs_bindex_t bindex, bbot;
34449 +       struct super_block *sb;
34450 +       atomic_t *next;
34451 +
34452 +       err = au_wbr_create_exp(dentry);
34453 +       if (err >= 0)
34454 +               goto out;
34455 +
34456 +       sb = dentry->d_sb;
34457 +       next = &au_sbi(sb)->si_wbr_rr_next;
34458 +       bbot = au_sbbot(sb);
34459 +       nbr = bbot + 1;
34460 +       for (bindex = 0; bindex <= bbot; bindex++) {
34461 +               if (!au_ftest_wbr(flags, DIR)) {
34462 +                       err = atomic_dec_return(next) + 1;
34463 +                       /* modulo for 0 is meaningless */
34464 +                       if (unlikely(!err))
34465 +                               err = atomic_dec_return(next) + 1;
34466 +               } else
34467 +                       err = atomic_read(next);
34468 +               AuDbg("%d\n", err);
34469 +               u = err;
34470 +               err = u % nbr;
34471 +               AuDbg("%d\n", err);
34472 +               if (!au_br_rdonly(au_sbr(sb, err)))
34473 +                       break;
34474 +               err = -EROFS;
34475 +       }
34476 +
34477 +       if (err >= 0)
34478 +               err = au_wbr_nonopq(dentry, err);
34479 +
34480 +out:
34481 +       AuDbg("%d\n", err);
34482 +       return err;
34483 +}
34484 +
34485 +/* ---------------------------------------------------------------------- */
34486 +
34487 +/* most free space */
34488 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34489 +{
34490 +       struct super_block *sb;
34491 +       struct au_branch *br;
34492 +       struct au_wbr_mfs *mfs;
34493 +       struct dentry *h_parent;
34494 +       aufs_bindex_t bindex, bbot;
34495 +       int err;
34496 +       unsigned long long b, bavail;
34497 +       struct path h_path;
34498 +       /* reduce the stack usage */
34499 +       struct kstatfs *st;
34500 +
34501 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34502 +       if (unlikely(!st)) {
34503 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34504 +               return;
34505 +       }
34506 +
34507 +       bavail = 0;
34508 +       sb = dentry->d_sb;
34509 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34510 +       MtxMustLock(&mfs->mfs_lock);
34511 +       mfs->mfs_bindex = -EROFS;
34512 +       mfs->mfsrr_bytes = 0;
34513 +       if (!parent) {
34514 +               bindex = 0;
34515 +               bbot = au_sbbot(sb);
34516 +       } else {
34517 +               bindex = au_dbtop(parent);
34518 +               bbot = au_dbtaildir(parent);
34519 +       }
34520 +
34521 +       for (; bindex <= bbot; bindex++) {
34522 +               if (parent) {
34523 +                       h_parent = au_h_dptr(parent, bindex);
34524 +                       if (!h_parent || d_is_negative(h_parent))
34525 +                               continue;
34526 +               }
34527 +               br = au_sbr(sb, bindex);
34528 +               if (au_br_rdonly(br))
34529 +                       continue;
34530 +
34531 +               /* sb->s_root for NFS is unreliable */
34532 +               h_path.mnt = au_br_mnt(br);
34533 +               h_path.dentry = h_path.mnt->mnt_root;
34534 +               err = vfs_statfs(&h_path, st);
34535 +               if (unlikely(err)) {
34536 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34537 +                       continue;
34538 +               }
34539 +
34540 +               /* when the available size is equal, select the lower one */
34541 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34542 +                            || sizeof(b) < sizeof(st->f_bsize));
34543 +               b = st->f_bavail * st->f_bsize;
34544 +               br->br_wbr->wbr_bytes = b;
34545 +               if (b >= bavail) {
34546 +                       bavail = b;
34547 +                       mfs->mfs_bindex = bindex;
34548 +                       mfs->mfs_jiffy = jiffies;
34549 +               }
34550 +       }
34551 +
34552 +       mfs->mfsrr_bytes = bavail;
34553 +       AuDbg("b%d\n", mfs->mfs_bindex);
34554 +       au_kfree_rcu(st);
34555 +}
34556 +
34557 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34558 +{
34559 +       int err;
34560 +       struct dentry *parent;
34561 +       struct super_block *sb;
34562 +       struct au_wbr_mfs *mfs;
34563 +
34564 +       err = au_wbr_create_exp(dentry);
34565 +       if (err >= 0)
34566 +               goto out;
34567 +
34568 +       sb = dentry->d_sb;
34569 +       parent = NULL;
34570 +       if (au_ftest_wbr(flags, PARENT))
34571 +               parent = dget_parent(dentry);
34572 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34573 +       mutex_lock(&mfs->mfs_lock);
34574 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34575 +           || mfs->mfs_bindex < 0
34576 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34577 +               au_mfs(dentry, parent);
34578 +       mutex_unlock(&mfs->mfs_lock);
34579 +       err = mfs->mfs_bindex;
34580 +       dput(parent);
34581 +
34582 +       if (err >= 0)
34583 +               err = au_wbr_nonopq(dentry, err);
34584 +
34585 +out:
34586 +       AuDbg("b%d\n", err);
34587 +       return err;
34588 +}
34589 +
34590 +static int au_wbr_create_init_mfs(struct super_block *sb)
34591 +{
34592 +       struct au_wbr_mfs *mfs;
34593 +
34594 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34595 +       mutex_init(&mfs->mfs_lock);
34596 +       mfs->mfs_jiffy = 0;
34597 +       mfs->mfs_bindex = -EROFS;
34598 +
34599 +       return 0;
34600 +}
34601 +
34602 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34603 +{
34604 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34605 +       return 0;
34606 +}
34607 +
34608 +/* ---------------------------------------------------------------------- */
34609 +
34610 +/* top down regardless parent, and then mfs */
34611 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34612 +                              unsigned int flags __maybe_unused)
34613 +{
34614 +       int err;
34615 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34616 +       unsigned long long watermark;
34617 +       struct super_block *sb;
34618 +       struct au_wbr_mfs *mfs;
34619 +       struct au_branch *br;
34620 +       struct dentry *parent;
34621 +
34622 +       sb = dentry->d_sb;
34623 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34624 +       mutex_lock(&mfs->mfs_lock);
34625 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34626 +           || mfs->mfs_bindex < 0)
34627 +               au_mfs(dentry, /*parent*/NULL);
34628 +       watermark = mfs->mfsrr_watermark;
34629 +       bmfs = mfs->mfs_bindex;
34630 +       mutex_unlock(&mfs->mfs_lock);
34631 +
34632 +       /* another style of au_wbr_create_exp() */
34633 +       bwh = au_dbwh(dentry);
34634 +       parent = dget_parent(dentry);
34635 +       btail = au_dbtaildir(parent);
34636 +       if (bwh >= 0 && bwh < btail)
34637 +               btail = bwh;
34638 +
34639 +       err = au_wbr_nonopq(dentry, btail);
34640 +       if (unlikely(err < 0))
34641 +               goto out;
34642 +       btail = err;
34643 +       bfound = -1;
34644 +       for (bindex = 0; bindex <= btail; bindex++) {
34645 +               br = au_sbr(sb, bindex);
34646 +               if (au_br_rdonly(br))
34647 +                       continue;
34648 +               if (br->br_wbr->wbr_bytes > watermark) {
34649 +                       bfound = bindex;
34650 +                       break;
34651 +               }
34652 +       }
34653 +       err = bfound;
34654 +       if (err < 0)
34655 +               err = bmfs;
34656 +
34657 +out:
34658 +       dput(parent);
34659 +       AuDbg("b%d\n", err);
34660 +       return err;
34661 +}
34662 +
34663 +/* ---------------------------------------------------------------------- */
34664 +
34665 +/* most free space and then round robin */
34666 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34667 +{
34668 +       int err;
34669 +       struct au_wbr_mfs *mfs;
34670 +
34671 +       err = au_wbr_create_mfs(dentry, flags);
34672 +       if (err >= 0) {
34673 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34674 +               mutex_lock(&mfs->mfs_lock);
34675 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34676 +                       err = au_wbr_create_rr(dentry, flags);
34677 +               mutex_unlock(&mfs->mfs_lock);
34678 +       }
34679 +
34680 +       AuDbg("b%d\n", err);
34681 +       return err;
34682 +}
34683 +
34684 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34685 +{
34686 +       int err;
34687 +
34688 +       au_wbr_create_init_mfs(sb); /* ignore */
34689 +       err = au_wbr_create_init_rr(sb);
34690 +
34691 +       return err;
34692 +}
34693 +
34694 +/* ---------------------------------------------------------------------- */
34695 +
34696 +/* top down parent and most free space */
34697 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34698 +{
34699 +       int err, e2;
34700 +       unsigned long long b;
34701 +       aufs_bindex_t bindex, btop, bbot;
34702 +       struct super_block *sb;
34703 +       struct dentry *parent, *h_parent;
34704 +       struct au_branch *br;
34705 +
34706 +       err = au_wbr_create_tdp(dentry, flags);
34707 +       if (unlikely(err < 0))
34708 +               goto out;
34709 +       parent = dget_parent(dentry);
34710 +       btop = au_dbtop(parent);
34711 +       bbot = au_dbtaildir(parent);
34712 +       if (btop == bbot)
34713 +               goto out_parent; /* success */
34714 +
34715 +       e2 = au_wbr_create_mfs(dentry, flags);
34716 +       if (e2 < 0)
34717 +               goto out_parent; /* success */
34718 +
34719 +       /* when the available size is equal, select upper one */
34720 +       sb = dentry->d_sb;
34721 +       br = au_sbr(sb, err);
34722 +       b = br->br_wbr->wbr_bytes;
34723 +       AuDbg("b%d, %llu\n", err, b);
34724 +
34725 +       for (bindex = btop; bindex <= bbot; bindex++) {
34726 +               h_parent = au_h_dptr(parent, bindex);
34727 +               if (!h_parent || d_is_negative(h_parent))
34728 +                       continue;
34729 +
34730 +               br = au_sbr(sb, bindex);
34731 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34732 +                       b = br->br_wbr->wbr_bytes;
34733 +                       err = bindex;
34734 +                       AuDbg("b%d, %llu\n", err, b);
34735 +               }
34736 +       }
34737 +
34738 +       if (err >= 0)
34739 +               err = au_wbr_nonopq(dentry, err);
34740 +
34741 +out_parent:
34742 +       dput(parent);
34743 +out:
34744 +       AuDbg("b%d\n", err);
34745 +       return err;
34746 +}
34747 +
34748 +/* ---------------------------------------------------------------------- */
34749 +
34750 +/*
34751 + * - top down parent
34752 + * - most free space with parent
34753 + * - most free space round-robin regardless parent
34754 + */
34755 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34756 +{
34757 +       int err;
34758 +       unsigned long long watermark;
34759 +       struct super_block *sb;
34760 +       struct au_branch *br;
34761 +       struct au_wbr_mfs *mfs;
34762 +
34763 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34764 +       if (unlikely(err < 0))
34765 +               goto out;
34766 +
34767 +       sb = dentry->d_sb;
34768 +       br = au_sbr(sb, err);
34769 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34770 +       mutex_lock(&mfs->mfs_lock);
34771 +       watermark = mfs->mfsrr_watermark;
34772 +       mutex_unlock(&mfs->mfs_lock);
34773 +       if (br->br_wbr->wbr_bytes < watermark)
34774 +               /* regardless the parent dir */
34775 +               err = au_wbr_create_mfsrr(dentry, flags);
34776 +
34777 +out:
34778 +       AuDbg("b%d\n", err);
34779 +       return err;
34780 +}
34781 +
34782 +/* ---------------------------------------------------------------------- */
34783 +
34784 +/* policies for copyup */
34785 +
34786 +/* top down parent */
34787 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34788 +{
34789 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34790 +}
34791 +
34792 +/* bottom up parent */
34793 +static int au_wbr_copyup_bup(struct dentry *dentry)
34794 +{
34795 +       int err;
34796 +       aufs_bindex_t bindex, btop;
34797 +       struct dentry *parent, *h_parent;
34798 +       struct super_block *sb;
34799 +
34800 +       err = -EROFS;
34801 +       sb = dentry->d_sb;
34802 +       parent = dget_parent(dentry);
34803 +       btop = au_dbtop(parent);
34804 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34805 +               h_parent = au_h_dptr(parent, bindex);
34806 +               if (!h_parent || d_is_negative(h_parent))
34807 +                       continue;
34808 +
34809 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34810 +                       err = bindex;
34811 +                       break;
34812 +               }
34813 +       }
34814 +       dput(parent);
34815 +
34816 +       /* bottom up here */
34817 +       if (unlikely(err < 0))
34818 +               err = au_wbr_bu(sb, btop - 1);
34819 +
34820 +       AuDbg("b%d\n", err);
34821 +       return err;
34822 +}
34823 +
34824 +/* bottom up */
34825 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34826 +{
34827 +       int err;
34828 +
34829 +       err = au_wbr_bu(dentry->d_sb, btop);
34830 +       AuDbg("b%d\n", err);
34831 +       if (err > btop)
34832 +               err = au_wbr_nonopq(dentry, err);
34833 +
34834 +       AuDbg("b%d\n", err);
34835 +       return err;
34836 +}
34837 +
34838 +static int au_wbr_copyup_bu(struct dentry *dentry)
34839 +{
34840 +       int err;
34841 +       aufs_bindex_t btop;
34842 +
34843 +       btop = au_dbtop(dentry);
34844 +       err = au_wbr_do_copyup_bu(dentry, btop);
34845 +       return err;
34846 +}
34847 +
34848 +/* ---------------------------------------------------------------------- */
34849 +
34850 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34851 +       [AuWbrCopyup_TDP] = {
34852 +               .copyup = au_wbr_copyup_tdp
34853 +       },
34854 +       [AuWbrCopyup_BUP] = {
34855 +               .copyup = au_wbr_copyup_bup
34856 +       },
34857 +       [AuWbrCopyup_BU] = {
34858 +               .copyup = au_wbr_copyup_bu
34859 +       }
34860 +};
34861 +
34862 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34863 +       [AuWbrCreate_TDP] = {
34864 +               .create = au_wbr_create_tdp
34865 +       },
34866 +       [AuWbrCreate_RR] = {
34867 +               .create = au_wbr_create_rr,
34868 +               .init   = au_wbr_create_init_rr
34869 +       },
34870 +       [AuWbrCreate_MFS] = {
34871 +               .create = au_wbr_create_mfs,
34872 +               .init   = au_wbr_create_init_mfs,
34873 +               .fin    = au_wbr_create_fin_mfs
34874 +       },
34875 +       [AuWbrCreate_MFSV] = {
34876 +               .create = au_wbr_create_mfs,
34877 +               .init   = au_wbr_create_init_mfs,
34878 +               .fin    = au_wbr_create_fin_mfs
34879 +       },
34880 +       [AuWbrCreate_MFSRR] = {
34881 +               .create = au_wbr_create_mfsrr,
34882 +               .init   = au_wbr_create_init_mfsrr,
34883 +               .fin    = au_wbr_create_fin_mfs
34884 +       },
34885 +       [AuWbrCreate_MFSRRV] = {
34886 +               .create = au_wbr_create_mfsrr,
34887 +               .init   = au_wbr_create_init_mfsrr,
34888 +               .fin    = au_wbr_create_fin_mfs
34889 +       },
34890 +       [AuWbrCreate_TDMFS] = {
34891 +               .create = au_wbr_create_tdmfs,
34892 +               .init   = au_wbr_create_init_mfs,
34893 +               .fin    = au_wbr_create_fin_mfs
34894 +       },
34895 +       [AuWbrCreate_TDMFSV] = {
34896 +               .create = au_wbr_create_tdmfs,
34897 +               .init   = au_wbr_create_init_mfs,
34898 +               .fin    = au_wbr_create_fin_mfs
34899 +       },
34900 +       [AuWbrCreate_PMFS] = {
34901 +               .create = au_wbr_create_pmfs,
34902 +               .init   = au_wbr_create_init_mfs,
34903 +               .fin    = au_wbr_create_fin_mfs
34904 +       },
34905 +       [AuWbrCreate_PMFSV] = {
34906 +               .create = au_wbr_create_pmfs,
34907 +               .init   = au_wbr_create_init_mfs,
34908 +               .fin    = au_wbr_create_fin_mfs
34909 +       },
34910 +       [AuWbrCreate_PMFSRR] = {
34911 +               .create = au_wbr_create_pmfsrr,
34912 +               .init   = au_wbr_create_init_mfsrr,
34913 +               .fin    = au_wbr_create_fin_mfs
34914 +       },
34915 +       [AuWbrCreate_PMFSRRV] = {
34916 +               .create = au_wbr_create_pmfsrr,
34917 +               .init   = au_wbr_create_init_mfsrr,
34918 +               .fin    = au_wbr_create_fin_mfs
34919 +       }
34920 +};
34921 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34922 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34923 +++ linux/fs/aufs/whout.c       2018-12-27 13:19:17.711749485 +0100
34924 @@ -0,0 +1,1062 @@
34925 +// SPDX-License-Identifier: GPL-2.0
34926 +/*
34927 + * Copyright (C) 2005-2018 Junjiro R. Okajima
34928 + *
34929 + * This program, aufs is free software; you can redistribute it and/or modify
34930 + * it under the terms of the GNU General Public License as published by
34931 + * the Free Software Foundation; either version 2 of the License, or
34932 + * (at your option) any later version.
34933 + *
34934 + * This program is distributed in the hope that it will be useful,
34935 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34936 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34937 + * GNU General Public License for more details.
34938 + *
34939 + * You should have received a copy of the GNU General Public License
34940 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34941 + */
34942 +
34943 +/*
34944 + * whiteout for logical deletion and opaque directory
34945 + */
34946 +
34947 +#include "aufs.h"
34948 +
34949 +#define WH_MASK                        0444
34950 +
34951 +/*
34952 + * If a directory contains this file, then it is opaque.  We start with the
34953 + * .wh. flag so that it is blocked by lookup.
34954 + */
34955 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34956 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34957 +
34958 +/*
34959 + * generate whiteout name, which is NOT terminated by NULL.
34960 + * @name: original d_name.name
34961 + * @len: original d_name.len
34962 + * @wh: whiteout qstr
34963 + * returns zero when succeeds, otherwise error.
34964 + * succeeded value as wh->name should be freed by kfree().
34965 + */
34966 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34967 +{
34968 +       char *p;
34969 +
34970 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34971 +               return -ENAMETOOLONG;
34972 +
34973 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34974 +       p = kmalloc(wh->len, GFP_NOFS);
34975 +       wh->name = p;
34976 +       if (p) {
34977 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34978 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34979 +               /* smp_mb(); */
34980 +               return 0;
34981 +       }
34982 +       return -ENOMEM;
34983 +}
34984 +
34985 +/* ---------------------------------------------------------------------- */
34986 +
34987 +/*
34988 + * test if the @wh_name exists under @h_parent.
34989 + * @try_sio specifies the necessary of super-io.
34990 + */
34991 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34992 +{
34993 +       int err;
34994 +       struct dentry *wh_dentry;
34995 +
34996 +       if (!try_sio)
34997 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34998 +       else
34999 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
35000 +       err = PTR_ERR(wh_dentry);
35001 +       if (IS_ERR(wh_dentry)) {
35002 +               if (err == -ENAMETOOLONG)
35003 +                       err = 0;
35004 +               goto out;
35005 +       }
35006 +
35007 +       err = 0;
35008 +       if (d_is_negative(wh_dentry))
35009 +               goto out_wh; /* success */
35010 +
35011 +       err = 1;
35012 +       if (d_is_reg(wh_dentry))
35013 +               goto out_wh; /* success */
35014 +
35015 +       err = -EIO;
35016 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35017 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35018 +
35019 +out_wh:
35020 +       dput(wh_dentry);
35021 +out:
35022 +       return err;
35023 +}
35024 +
35025 +/*
35026 + * test if the @h_dentry sets opaque or not.
35027 + */
35028 +int au_diropq_test(struct dentry *h_dentry)
35029 +{
35030 +       int err;
35031 +       struct inode *h_dir;
35032 +
35033 +       h_dir = d_inode(h_dentry);
35034 +       err = au_wh_test(h_dentry, &diropq_name,
35035 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
35036 +       return err;
35037 +}
35038 +
35039 +/*
35040 + * returns a negative dentry whose name is unique and temporary.
35041 + */
35042 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35043 +                            struct qstr *prefix)
35044 +{
35045 +       struct dentry *dentry;
35046 +       int i;
35047 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35048 +               *name, *p;
35049 +       /* strict atomic_t is unnecessary here */
35050 +       static unsigned short cnt;
35051 +       struct qstr qs;
35052 +
35053 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35054 +
35055 +       name = defname;
35056 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35057 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35058 +               dentry = ERR_PTR(-ENAMETOOLONG);
35059 +               if (unlikely(qs.len > NAME_MAX))
35060 +                       goto out;
35061 +               dentry = ERR_PTR(-ENOMEM);
35062 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35063 +               if (unlikely(!name))
35064 +                       goto out;
35065 +       }
35066 +
35067 +       /* doubly whiteout-ed */
35068 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35069 +       p = name + AUFS_WH_PFX_LEN * 2;
35070 +       memcpy(p, prefix->name, prefix->len);
35071 +       p += prefix->len;
35072 +       *p++ = '.';
35073 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35074 +
35075 +       qs.name = name;
35076 +       for (i = 0; i < 3; i++) {
35077 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35078 +               dentry = au_sio_lkup_one(&qs, h_parent);
35079 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35080 +                       goto out_name;
35081 +               dput(dentry);
35082 +       }
35083 +       /* pr_warn("could not get random name\n"); */
35084 +       dentry = ERR_PTR(-EEXIST);
35085 +       AuDbg("%.*s\n", AuLNPair(&qs));
35086 +       BUG();
35087 +
35088 +out_name:
35089 +       if (name != defname)
35090 +               au_kfree_try_rcu(name);
35091 +out:
35092 +       AuTraceErrPtr(dentry);
35093 +       return dentry;
35094 +}
35095 +
35096 +/*
35097 + * rename the @h_dentry on @br to the whiteouted temporary name.
35098 + */
35099 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35100 +{
35101 +       int err;
35102 +       struct path h_path = {
35103 +               .mnt = au_br_mnt(br)
35104 +       };
35105 +       struct inode *h_dir, *delegated;
35106 +       struct dentry *h_parent;
35107 +
35108 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35109 +       h_dir = d_inode(h_parent);
35110 +       IMustLock(h_dir);
35111 +
35112 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35113 +       err = PTR_ERR(h_path.dentry);
35114 +       if (IS_ERR(h_path.dentry))
35115 +               goto out;
35116 +
35117 +       /* under the same dir, no need to lock_rename() */
35118 +       delegated = NULL;
35119 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35120 +                          /*flags*/0);
35121 +       AuTraceErr(err);
35122 +       if (unlikely(err == -EWOULDBLOCK)) {
35123 +               pr_warn("cannot retry for NFSv4 delegation"
35124 +                       " for an internal rename\n");
35125 +               iput(delegated);
35126 +       }
35127 +       dput(h_path.dentry);
35128 +
35129 +out:
35130 +       AuTraceErr(err);
35131 +       return err;
35132 +}
35133 +
35134 +/* ---------------------------------------------------------------------- */
35135 +/*
35136 + * functions for removing a whiteout
35137 + */
35138 +
35139 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35140 +{
35141 +       int err, force;
35142 +       struct inode *delegated;
35143 +
35144 +       /*
35145 +        * forces superio when the dir has a sticky bit.
35146 +        * this may be a violation of unix fs semantics.
35147 +        */
35148 +       force = (h_dir->i_mode & S_ISVTX)
35149 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35150 +       delegated = NULL;
35151 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35152 +       if (unlikely(err == -EWOULDBLOCK)) {
35153 +               pr_warn("cannot retry for NFSv4 delegation"
35154 +                       " for an internal unlink\n");
35155 +               iput(delegated);
35156 +       }
35157 +       return err;
35158 +}
35159 +
35160 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35161 +                       struct dentry *dentry)
35162 +{
35163 +       int err;
35164 +
35165 +       err = do_unlink_wh(h_dir, h_path);
35166 +       if (!err && dentry)
35167 +               au_set_dbwh(dentry, -1);
35168 +
35169 +       return err;
35170 +}
35171 +
35172 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35173 +                         struct au_branch *br)
35174 +{
35175 +       int err;
35176 +       struct path h_path = {
35177 +               .mnt = au_br_mnt(br)
35178 +       };
35179 +
35180 +       err = 0;
35181 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
35182 +       if (IS_ERR(h_path.dentry))
35183 +               err = PTR_ERR(h_path.dentry);
35184 +       else {
35185 +               if (d_is_reg(h_path.dentry))
35186 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
35187 +               dput(h_path.dentry);
35188 +       }
35189 +
35190 +       return err;
35191 +}
35192 +
35193 +/* ---------------------------------------------------------------------- */
35194 +/*
35195 + * initialize/clean whiteout for a branch
35196 + */
35197 +
35198 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35199 +                       const int isdir)
35200 +{
35201 +       int err;
35202 +       struct inode *delegated;
35203 +
35204 +       if (d_is_negative(whpath->dentry))
35205 +               return;
35206 +
35207 +       if (isdir)
35208 +               err = vfsub_rmdir(h_dir, whpath);
35209 +       else {
35210 +               delegated = NULL;
35211 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35212 +               if (unlikely(err == -EWOULDBLOCK)) {
35213 +                       pr_warn("cannot retry for NFSv4 delegation"
35214 +                               " for an internal unlink\n");
35215 +                       iput(delegated);
35216 +               }
35217 +       }
35218 +       if (unlikely(err))
35219 +               pr_warn("failed removing %pd (%d), ignored.\n",
35220 +                       whpath->dentry, err);
35221 +}
35222 +
35223 +static int test_linkable(struct dentry *h_root)
35224 +{
35225 +       struct inode *h_dir = d_inode(h_root);
35226 +
35227 +       if (h_dir->i_op->link)
35228 +               return 0;
35229 +
35230 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35231 +              h_root, au_sbtype(h_root->d_sb));
35232 +       return -ENOSYS;
35233 +}
35234 +
35235 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35236 +static int au_whdir(struct inode *h_dir, struct path *path)
35237 +{
35238 +       int err;
35239 +
35240 +       err = -EEXIST;
35241 +       if (d_is_negative(path->dentry)) {
35242 +               int mode = 0700;
35243 +
35244 +               if (au_test_nfs(path->dentry->d_sb))
35245 +                       mode |= 0111;
35246 +               err = vfsub_mkdir(h_dir, path, mode);
35247 +       } else if (d_is_dir(path->dentry))
35248 +               err = 0;
35249 +       else
35250 +               pr_err("unknown %pd exists\n", path->dentry);
35251 +
35252 +       return err;
35253 +}
35254 +
35255 +struct au_wh_base {
35256 +       const struct qstr *name;
35257 +       struct dentry *dentry;
35258 +};
35259 +
35260 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35261 +                         struct path *h_path)
35262 +{
35263 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35264 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35265 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35266 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35267 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35268 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35269 +}
35270 +
35271 +/*
35272 + * returns tri-state,
35273 + * minus: error, caller should print the message
35274 + * zero: success
35275 + * plus: error, caller should NOT print the message
35276 + */
35277 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35278 +                               int do_plink, struct au_wh_base base[],
35279 +                               struct path *h_path)
35280 +{
35281 +       int err;
35282 +       struct inode *h_dir;
35283 +
35284 +       h_dir = d_inode(h_root);
35285 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35286 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35287 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35288 +       if (do_plink) {
35289 +               err = test_linkable(h_root);
35290 +               if (unlikely(err)) {
35291 +                       err = 1;
35292 +                       goto out;
35293 +               }
35294 +
35295 +               err = au_whdir(h_dir, h_path);
35296 +               if (unlikely(err))
35297 +                       goto out;
35298 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35299 +       } else
35300 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35301 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35302 +       err = au_whdir(h_dir, h_path);
35303 +       if (unlikely(err))
35304 +               goto out;
35305 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35306 +
35307 +out:
35308 +       return err;
35309 +}
35310 +
35311 +/*
35312 + * for the moment, aufs supports the branch filesystem which does not support
35313 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35314 + * copyup failed. finally, such filesystem will not be used as the writable
35315 + * branch.
35316 + *
35317 + * returns tri-state, see above.
35318 + */
35319 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35320 +                        int do_plink, struct au_wh_base base[],
35321 +                        struct path *h_path)
35322 +{
35323 +       int err;
35324 +       struct inode *h_dir;
35325 +
35326 +       WbrWhMustWriteLock(wbr);
35327 +
35328 +       err = test_linkable(h_root);
35329 +       if (unlikely(err)) {
35330 +               err = 1;
35331 +               goto out;
35332 +       }
35333 +
35334 +       /*
35335 +        * todo: should this create be done in /sbin/mount.aufs helper?
35336 +        */
35337 +       err = -EEXIST;
35338 +       h_dir = d_inode(h_root);
35339 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35340 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35341 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35342 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35343 +               err = 0;
35344 +       else
35345 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35346 +       if (unlikely(err))
35347 +               goto out;
35348 +
35349 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35350 +       if (do_plink) {
35351 +               err = au_whdir(h_dir, h_path);
35352 +               if (unlikely(err))
35353 +                       goto out;
35354 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35355 +       } else
35356 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35357 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35358 +
35359 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35360 +       err = au_whdir(h_dir, h_path);
35361 +       if (unlikely(err))
35362 +               goto out;
35363 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35364 +
35365 +out:
35366 +       return err;
35367 +}
35368 +
35369 +/*
35370 + * initialize the whiteout base file/dir for @br.
35371 + */
35372 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35373 +{
35374 +       int err, i;
35375 +       const unsigned char do_plink
35376 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35377 +       struct inode *h_dir;
35378 +       struct path path = br->br_path;
35379 +       struct dentry *h_root = path.dentry;
35380 +       struct au_wbr *wbr = br->br_wbr;
35381 +       static const struct qstr base_name[] = {
35382 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35383 +                                         sizeof(AUFS_BASE_NAME) - 1),
35384 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35385 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35386 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35387 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35388 +       };
35389 +       struct au_wh_base base[] = {
35390 +               [AuBrWh_BASE] = {
35391 +                       .name   = base_name + AuBrWh_BASE,
35392 +                       .dentry = NULL
35393 +               },
35394 +               [AuBrWh_PLINK] = {
35395 +                       .name   = base_name + AuBrWh_PLINK,
35396 +                       .dentry = NULL
35397 +               },
35398 +               [AuBrWh_ORPH] = {
35399 +                       .name   = base_name + AuBrWh_ORPH,
35400 +                       .dentry = NULL
35401 +               }
35402 +       };
35403 +
35404 +       if (wbr)
35405 +               WbrWhMustWriteLock(wbr);
35406 +
35407 +       for (i = 0; i < AuBrWh_Last; i++) {
35408 +               /* doubly whiteouted */
35409 +               struct dentry *d;
35410 +
35411 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35412 +               err = PTR_ERR(d);
35413 +               if (IS_ERR(d))
35414 +                       goto out;
35415 +
35416 +               base[i].dentry = d;
35417 +               AuDebugOn(wbr
35418 +                         && wbr->wbr_wh[i]
35419 +                         && wbr->wbr_wh[i] != base[i].dentry);
35420 +       }
35421 +
35422 +       if (wbr)
35423 +               for (i = 0; i < AuBrWh_Last; i++) {
35424 +                       dput(wbr->wbr_wh[i]);
35425 +                       wbr->wbr_wh[i] = NULL;
35426 +               }
35427 +
35428 +       err = 0;
35429 +       if (!au_br_writable(br->br_perm)) {
35430 +               h_dir = d_inode(h_root);
35431 +               au_wh_init_ro(h_dir, base, &path);
35432 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35433 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35434 +               if (err > 0)
35435 +                       goto out;
35436 +               else if (err)
35437 +                       goto out_err;
35438 +       } else {
35439 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35440 +               if (err > 0)
35441 +                       goto out;
35442 +               else if (err)
35443 +                       goto out_err;
35444 +       }
35445 +       goto out; /* success */
35446 +
35447 +out_err:
35448 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35449 +              err, h_root, au_sbtype(h_root->d_sb));
35450 +out:
35451 +       for (i = 0; i < AuBrWh_Last; i++)
35452 +               dput(base[i].dentry);
35453 +       return err;
35454 +}
35455 +
35456 +/* ---------------------------------------------------------------------- */
35457 +/*
35458 + * whiteouts are all hard-linked usually.
35459 + * when its link count reaches a ceiling, we create a new whiteout base
35460 + * asynchronously.
35461 + */
35462 +
35463 +struct reinit_br_wh {
35464 +       struct super_block *sb;
35465 +       struct au_branch *br;
35466 +};
35467 +
35468 +static void reinit_br_wh(void *arg)
35469 +{
35470 +       int err;
35471 +       aufs_bindex_t bindex;
35472 +       struct path h_path;
35473 +       struct reinit_br_wh *a = arg;
35474 +       struct au_wbr *wbr;
35475 +       struct inode *dir, *delegated;
35476 +       struct dentry *h_root;
35477 +       struct au_hinode *hdir;
35478 +
35479 +       err = 0;
35480 +       wbr = a->br->br_wbr;
35481 +       /* big aufs lock */
35482 +       si_noflush_write_lock(a->sb);
35483 +       if (!au_br_writable(a->br->br_perm))
35484 +               goto out;
35485 +       bindex = au_br_index(a->sb, a->br->br_id);
35486 +       if (unlikely(bindex < 0))
35487 +               goto out;
35488 +
35489 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35490 +       dir = d_inode(a->sb->s_root);
35491 +       hdir = au_hi(dir, bindex);
35492 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35493 +       AuDebugOn(h_root != au_br_dentry(a->br));
35494 +
35495 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35496 +       wbr_wh_write_lock(wbr);
35497 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35498 +                         h_root, a->br);
35499 +       if (!err) {
35500 +               h_path.dentry = wbr->wbr_whbase;
35501 +               h_path.mnt = au_br_mnt(a->br);
35502 +               delegated = NULL;
35503 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35504 +                                  /*force*/0);
35505 +               if (unlikely(err == -EWOULDBLOCK)) {
35506 +                       pr_warn("cannot retry for NFSv4 delegation"
35507 +                               " for an internal unlink\n");
35508 +                       iput(delegated);
35509 +               }
35510 +       } else {
35511 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35512 +               err = 0;
35513 +       }
35514 +       dput(wbr->wbr_whbase);
35515 +       wbr->wbr_whbase = NULL;
35516 +       if (!err)
35517 +               err = au_wh_init(a->br, a->sb);
35518 +       wbr_wh_write_unlock(wbr);
35519 +       au_hn_inode_unlock(hdir);
35520 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35521 +       if (!err)
35522 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35523 +
35524 +out:
35525 +       if (wbr)
35526 +               atomic_dec(&wbr->wbr_wh_running);
35527 +       au_lcnt_dec(&a->br->br_count);
35528 +       si_write_unlock(a->sb);
35529 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35530 +       au_kfree_rcu(a);
35531 +       if (unlikely(err))
35532 +               AuIOErr("err %d\n", err);
35533 +}
35534 +
35535 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35536 +{
35537 +       int do_dec, wkq_err;
35538 +       struct reinit_br_wh *arg;
35539 +
35540 +       do_dec = 1;
35541 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35542 +               goto out;
35543 +
35544 +       /* ignore ENOMEM */
35545 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35546 +       if (arg) {
35547 +               /*
35548 +                * dec(wh_running), kfree(arg) and dec(br_count)
35549 +                * in reinit function
35550 +                */
35551 +               arg->sb = sb;
35552 +               arg->br = br;
35553 +               au_lcnt_inc(&br->br_count);
35554 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35555 +               if (unlikely(wkq_err)) {
35556 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35557 +                       au_lcnt_dec(&br->br_count);
35558 +                       au_kfree_rcu(arg);
35559 +               }
35560 +               do_dec = 0;
35561 +       }
35562 +
35563 +out:
35564 +       if (do_dec)
35565 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35566 +}
35567 +
35568 +/* ---------------------------------------------------------------------- */
35569 +
35570 +/*
35571 + * create the whiteout @wh.
35572 + */
35573 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35574 +                            struct dentry *wh)
35575 +{
35576 +       int err;
35577 +       struct path h_path = {
35578 +               .dentry = wh
35579 +       };
35580 +       struct au_branch *br;
35581 +       struct au_wbr *wbr;
35582 +       struct dentry *h_parent;
35583 +       struct inode *h_dir, *delegated;
35584 +
35585 +       h_parent = wh->d_parent; /* dir inode is locked */
35586 +       h_dir = d_inode(h_parent);
35587 +       IMustLock(h_dir);
35588 +
35589 +       br = au_sbr(sb, bindex);
35590 +       h_path.mnt = au_br_mnt(br);
35591 +       wbr = br->br_wbr;
35592 +       wbr_wh_read_lock(wbr);
35593 +       if (wbr->wbr_whbase) {
35594 +               delegated = NULL;
35595 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35596 +               if (unlikely(err == -EWOULDBLOCK)) {
35597 +                       pr_warn("cannot retry for NFSv4 delegation"
35598 +                               " for an internal link\n");
35599 +                       iput(delegated);
35600 +               }
35601 +               if (!err || err != -EMLINK)
35602 +                       goto out;
35603 +
35604 +               /* link count full. re-initialize br_whbase. */
35605 +               kick_reinit_br_wh(sb, br);
35606 +       }
35607 +
35608 +       /* return this error in this context */
35609 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35610 +       if (!err)
35611 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35612 +
35613 +out:
35614 +       wbr_wh_read_unlock(wbr);
35615 +       return err;
35616 +}
35617 +
35618 +/* ---------------------------------------------------------------------- */
35619 +
35620 +/*
35621 + * create or remove the diropq.
35622 + */
35623 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35624 +                               unsigned int flags)
35625 +{
35626 +       struct dentry *opq_dentry, *h_dentry;
35627 +       struct super_block *sb;
35628 +       struct au_branch *br;
35629 +       int err;
35630 +
35631 +       sb = dentry->d_sb;
35632 +       br = au_sbr(sb, bindex);
35633 +       h_dentry = au_h_dptr(dentry, bindex);
35634 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35635 +       if (IS_ERR(opq_dentry))
35636 +               goto out;
35637 +
35638 +       if (au_ftest_diropq(flags, CREATE)) {
35639 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35640 +               if (!err) {
35641 +                       au_set_dbdiropq(dentry, bindex);
35642 +                       goto out; /* success */
35643 +               }
35644 +       } else {
35645 +               struct path tmp = {
35646 +                       .dentry = opq_dentry,
35647 +                       .mnt    = au_br_mnt(br)
35648 +               };
35649 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35650 +               if (!err)
35651 +                       au_set_dbdiropq(dentry, -1);
35652 +       }
35653 +       dput(opq_dentry);
35654 +       opq_dentry = ERR_PTR(err);
35655 +
35656 +out:
35657 +       return opq_dentry;
35658 +}
35659 +
35660 +struct do_diropq_args {
35661 +       struct dentry **errp;
35662 +       struct dentry *dentry;
35663 +       aufs_bindex_t bindex;
35664 +       unsigned int flags;
35665 +};
35666 +
35667 +static void call_do_diropq(void *args)
35668 +{
35669 +       struct do_diropq_args *a = args;
35670 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35671 +}
35672 +
35673 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35674 +                            unsigned int flags)
35675 +{
35676 +       struct dentry *diropq, *h_dentry;
35677 +
35678 +       h_dentry = au_h_dptr(dentry, bindex);
35679 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35680 +               diropq = do_diropq(dentry, bindex, flags);
35681 +       else {
35682 +               int wkq_err;
35683 +               struct do_diropq_args args = {
35684 +                       .errp           = &diropq,
35685 +                       .dentry         = dentry,
35686 +                       .bindex         = bindex,
35687 +                       .flags          = flags
35688 +               };
35689 +
35690 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35691 +               if (unlikely(wkq_err))
35692 +                       diropq = ERR_PTR(wkq_err);
35693 +       }
35694 +
35695 +       return diropq;
35696 +}
35697 +
35698 +/* ---------------------------------------------------------------------- */
35699 +
35700 +/*
35701 + * lookup whiteout dentry.
35702 + * @h_parent: lower parent dentry which must exist and be locked
35703 + * @base_name: name of dentry which will be whiteouted
35704 + * returns dentry for whiteout.
35705 + */
35706 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35707 +                         struct au_branch *br)
35708 +{
35709 +       int err;
35710 +       struct qstr wh_name;
35711 +       struct dentry *wh_dentry;
35712 +
35713 +       err = au_wh_name_alloc(&wh_name, base_name);
35714 +       wh_dentry = ERR_PTR(err);
35715 +       if (!err) {
35716 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35717 +               au_kfree_try_rcu(wh_name.name);
35718 +       }
35719 +       return wh_dentry;
35720 +}
35721 +
35722 +/*
35723 + * link/create a whiteout for @dentry on @bindex.
35724 + */
35725 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35726 +                           struct dentry *h_parent)
35727 +{
35728 +       struct dentry *wh_dentry;
35729 +       struct super_block *sb;
35730 +       int err;
35731 +
35732 +       sb = dentry->d_sb;
35733 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35734 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35735 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35736 +               if (!err) {
35737 +                       au_set_dbwh(dentry, bindex);
35738 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35739 +               } else {
35740 +                       dput(wh_dentry);
35741 +                       wh_dentry = ERR_PTR(err);
35742 +               }
35743 +       }
35744 +
35745 +       return wh_dentry;
35746 +}
35747 +
35748 +/* ---------------------------------------------------------------------- */
35749 +
35750 +/* Delete all whiteouts in this directory on branch bindex. */
35751 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35752 +                          aufs_bindex_t bindex, struct au_branch *br)
35753 +{
35754 +       int err;
35755 +       unsigned long ul, n;
35756 +       struct qstr wh_name;
35757 +       char *p;
35758 +       struct hlist_head *head;
35759 +       struct au_vdir_wh *pos;
35760 +       struct au_vdir_destr *str;
35761 +
35762 +       err = -ENOMEM;
35763 +       p = (void *)__get_free_page(GFP_NOFS);
35764 +       wh_name.name = p;
35765 +       if (unlikely(!wh_name.name))
35766 +               goto out;
35767 +
35768 +       err = 0;
35769 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35770 +       p += AUFS_WH_PFX_LEN;
35771 +       n = whlist->nh_num;
35772 +       head = whlist->nh_head;
35773 +       for (ul = 0; !err && ul < n; ul++, head++) {
35774 +               hlist_for_each_entry(pos, head, wh_hash) {
35775 +                       if (pos->wh_bindex != bindex)
35776 +                               continue;
35777 +
35778 +                       str = &pos->wh_str;
35779 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35780 +                               memcpy(p, str->name, str->len);
35781 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35782 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35783 +                               if (!err)
35784 +                                       continue;
35785 +                               break;
35786 +                       }
35787 +                       AuIOErr("whiteout name too long %.*s\n",
35788 +                               str->len, str->name);
35789 +                       err = -EIO;
35790 +                       break;
35791 +               }
35792 +       }
35793 +       free_page((unsigned long)wh_name.name);
35794 +
35795 +out:
35796 +       return err;
35797 +}
35798 +
35799 +struct del_wh_children_args {
35800 +       int *errp;
35801 +       struct dentry *h_dentry;
35802 +       struct au_nhash *whlist;
35803 +       aufs_bindex_t bindex;
35804 +       struct au_branch *br;
35805 +};
35806 +
35807 +static void call_del_wh_children(void *args)
35808 +{
35809 +       struct del_wh_children_args *a = args;
35810 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35811 +}
35812 +
35813 +/* ---------------------------------------------------------------------- */
35814 +
35815 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35816 +{
35817 +       struct au_whtmp_rmdir *whtmp;
35818 +       int err;
35819 +       unsigned int rdhash;
35820 +
35821 +       SiMustAnyLock(sb);
35822 +
35823 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35824 +       if (unlikely(!whtmp)) {
35825 +               whtmp = ERR_PTR(-ENOMEM);
35826 +               goto out;
35827 +       }
35828 +
35829 +       /* no estimation for dir size */
35830 +       rdhash = au_sbi(sb)->si_rdhash;
35831 +       if (!rdhash)
35832 +               rdhash = AUFS_RDHASH_DEF;
35833 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35834 +       if (unlikely(err)) {
35835 +               au_kfree_rcu(whtmp);
35836 +               whtmp = ERR_PTR(err);
35837 +       }
35838 +
35839 +out:
35840 +       return whtmp;
35841 +}
35842 +
35843 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35844 +{
35845 +       if (whtmp->br)
35846 +               au_lcnt_dec(&whtmp->br->br_count);
35847 +       dput(whtmp->wh_dentry);
35848 +       iput(whtmp->dir);
35849 +       au_nhash_wh_free(&whtmp->whlist);
35850 +       au_kfree_rcu(whtmp);
35851 +}
35852 +
35853 +/*
35854 + * rmdir the whiteouted temporary named dir @h_dentry.
35855 + * @whlist: whiteouted children.
35856 + */
35857 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35858 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35859 +{
35860 +       int err;
35861 +       unsigned int h_nlink;
35862 +       struct path h_tmp;
35863 +       struct inode *wh_inode, *h_dir;
35864 +       struct au_branch *br;
35865 +
35866 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35867 +       IMustLock(h_dir);
35868 +
35869 +       br = au_sbr(dir->i_sb, bindex);
35870 +       wh_inode = d_inode(wh_dentry);
35871 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35872 +
35873 +       /*
35874 +        * someone else might change some whiteouts while we were sleeping.
35875 +        * it means this whlist may have an obsoleted entry.
35876 +        */
35877 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35878 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35879 +       else {
35880 +               int wkq_err;
35881 +               struct del_wh_children_args args = {
35882 +                       .errp           = &err,
35883 +                       .h_dentry       = wh_dentry,
35884 +                       .whlist         = whlist,
35885 +                       .bindex         = bindex,
35886 +                       .br             = br
35887 +               };
35888 +
35889 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35890 +               if (unlikely(wkq_err))
35891 +                       err = wkq_err;
35892 +       }
35893 +       inode_unlock(wh_inode);
35894 +
35895 +       if (!err) {
35896 +               h_tmp.dentry = wh_dentry;
35897 +               h_tmp.mnt = au_br_mnt(br);
35898 +               h_nlink = h_dir->i_nlink;
35899 +               err = vfsub_rmdir(h_dir, &h_tmp);
35900 +               /* some fs doesn't change the parent nlink in some cases */
35901 +               h_nlink -= h_dir->i_nlink;
35902 +       }
35903 +
35904 +       if (!err) {
35905 +               if (au_ibtop(dir) == bindex) {
35906 +                       /* todo: dir->i_mutex is necessary */
35907 +                       au_cpup_attr_timesizes(dir);
35908 +                       if (h_nlink)
35909 +                               vfsub_drop_nlink(dir);
35910 +               }
35911 +               return 0; /* success */
35912 +       }
35913 +
35914 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35915 +       return err;
35916 +}
35917 +
35918 +static void call_rmdir_whtmp(void *args)
35919 +{
35920 +       int err;
35921 +       aufs_bindex_t bindex;
35922 +       struct au_whtmp_rmdir *a = args;
35923 +       struct super_block *sb;
35924 +       struct dentry *h_parent;
35925 +       struct inode *h_dir;
35926 +       struct au_hinode *hdir;
35927 +
35928 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35929 +       /* inode_lock(a->dir); */
35930 +       err = -EROFS;
35931 +       sb = a->dir->i_sb;
35932 +       si_read_lock(sb, !AuLock_FLUSH);
35933 +       if (!au_br_writable(a->br->br_perm))
35934 +               goto out;
35935 +       bindex = au_br_index(sb, a->br->br_id);
35936 +       if (unlikely(bindex < 0))
35937 +               goto out;
35938 +
35939 +       err = -EIO;
35940 +       ii_write_lock_parent(a->dir);
35941 +       h_parent = dget_parent(a->wh_dentry);
35942 +       h_dir = d_inode(h_parent);
35943 +       hdir = au_hi(a->dir, bindex);
35944 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35945 +       if (unlikely(err))
35946 +               goto out_mnt;
35947 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35948 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35949 +                         a->br);
35950 +       if (!err)
35951 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35952 +       au_hn_inode_unlock(hdir);
35953 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35954 +
35955 +out_mnt:
35956 +       dput(h_parent);
35957 +       ii_write_unlock(a->dir);
35958 +out:
35959 +       /* inode_unlock(a->dir); */
35960 +       au_whtmp_rmdir_free(a);
35961 +       si_read_unlock(sb);
35962 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35963 +       if (unlikely(err))
35964 +               AuIOErr("err %d\n", err);
35965 +}
35966 +
35967 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35968 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35969 +{
35970 +       int wkq_err;
35971 +       struct super_block *sb;
35972 +
35973 +       IMustLock(dir);
35974 +
35975 +       /* all post-process will be done in do_rmdir_whtmp(). */
35976 +       sb = dir->i_sb;
35977 +       args->dir = au_igrab(dir);
35978 +       args->br = au_sbr(sb, bindex);
35979 +       au_lcnt_inc(&args->br->br_count);
35980 +       args->wh_dentry = dget(wh_dentry);
35981 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35982 +       if (unlikely(wkq_err)) {
35983 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35984 +               au_whtmp_rmdir_free(args);
35985 +       }
35986 +}
35987 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35988 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35989 +++ linux/fs/aufs/whout.h       2018-08-12 23:43:05.463458173 +0200
35990 @@ -0,0 +1,86 @@
35991 +/* SPDX-License-Identifier: GPL-2.0 */
35992 +/*
35993 + * Copyright (C) 2005-2018 Junjiro R. Okajima
35994 + *
35995 + * This program, aufs is free software; you can redistribute it and/or modify
35996 + * it under the terms of the GNU General Public License as published by
35997 + * the Free Software Foundation; either version 2 of the License, or
35998 + * (at your option) any later version.
35999 + *
36000 + * This program is distributed in the hope that it will be useful,
36001 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36002 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36003 + * GNU General Public License for more details.
36004 + *
36005 + * You should have received a copy of the GNU General Public License
36006 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36007 + */
36008 +
36009 +/*
36010 + * whiteout for logical deletion and opaque directory
36011 + */
36012 +
36013 +#ifndef __AUFS_WHOUT_H__
36014 +#define __AUFS_WHOUT_H__
36015 +
36016 +#ifdef __KERNEL__
36017 +
36018 +#include "dir.h"
36019 +
36020 +/* whout.c */
36021 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36022 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
36023 +int au_diropq_test(struct dentry *h_dentry);
36024 +struct au_branch;
36025 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36026 +                            struct qstr *prefix);
36027 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36028 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36029 +                       struct dentry *dentry);
36030 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36031 +
36032 +/* diropq flags */
36033 +#define AuDiropq_CREATE        1
36034 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36035 +#define au_fset_diropq(flags, name) \
36036 +       do { (flags) |= AuDiropq_##name; } while (0)
36037 +#define au_fclr_diropq(flags, name) \
36038 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36039 +
36040 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36041 +                            unsigned int flags);
36042 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36043 +                         struct au_branch *br);
36044 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36045 +                           struct dentry *h_parent);
36046 +
36047 +/* real rmdir for the whiteout-ed dir */
36048 +struct au_whtmp_rmdir {
36049 +       struct inode *dir;
36050 +       struct au_branch *br;
36051 +       struct dentry *wh_dentry;
36052 +       struct au_nhash whlist;
36053 +};
36054 +
36055 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36056 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36057 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36058 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36059 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36060 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36061 +
36062 +/* ---------------------------------------------------------------------- */
36063 +
36064 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36065 +                                             aufs_bindex_t bindex)
36066 +{
36067 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36068 +}
36069 +
36070 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36071 +{
36072 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36073 +}
36074 +
36075 +#endif /* __KERNEL__ */
36076 +#endif /* __AUFS_WHOUT_H__ */
36077 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36078 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36079 +++ linux/fs/aufs/wkq.c 2018-12-27 13:19:17.711749485 +0100
36080 @@ -0,0 +1,392 @@
36081 +// SPDX-License-Identifier: GPL-2.0
36082 +/*
36083 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36084 + *
36085 + * This program, aufs is free software; you can redistribute it and/or modify
36086 + * it under the terms of the GNU General Public License as published by
36087 + * the Free Software Foundation; either version 2 of the License, or
36088 + * (at your option) any later version.
36089 + *
36090 + * This program is distributed in the hope that it will be useful,
36091 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36092 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36093 + * GNU General Public License for more details.
36094 + *
36095 + * You should have received a copy of the GNU General Public License
36096 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36097 + */
36098 +
36099 +/*
36100 + * workqueue for asynchronous/super-io operations
36101 + * todo: try new credential scheme
36102 + */
36103 +
36104 +#include <linux/module.h>
36105 +#include "aufs.h"
36106 +
36107 +/* internal workqueue named AUFS_WKQ_NAME */
36108 +
36109 +static struct workqueue_struct *au_wkq;
36110 +
36111 +struct au_wkinfo {
36112 +       struct work_struct wk;
36113 +       struct kobject *kobj;
36114 +
36115 +       unsigned int flags; /* see wkq.h */
36116 +
36117 +       au_wkq_func_t func;
36118 +       void *args;
36119 +
36120 +#ifdef CONFIG_LOCKDEP
36121 +       int dont_check;
36122 +       struct held_lock **hlock;
36123 +#endif
36124 +
36125 +       struct completion *comp;
36126 +};
36127 +
36128 +/* ---------------------------------------------------------------------- */
36129 +/*
36130 + * Aufs passes some operations to the workqueue such as the internal copyup.
36131 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36132 + * job run by workqueue depends upon the locks acquired in the other task.
36133 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36134 + * information too. And the job in the workqueue restores the info in order to
36135 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36136 + * correctly and expectedly.
36137 + */
36138 +
36139 +#ifndef CONFIG_LOCKDEP
36140 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36141 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36142 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36143 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36144 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36145 +#else
36146 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36147 +{
36148 +       wkinfo->hlock = NULL;
36149 +       wkinfo->dont_check = 0;
36150 +}
36151 +
36152 +/*
36153 + * 1: matched
36154 + * 0: unmatched
36155 + */
36156 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36157 +{
36158 +       static DEFINE_SPINLOCK(spin);
36159 +       static struct {
36160 +               char *name;
36161 +               struct lock_class_key *key;
36162 +       } a[] = {
36163 +               { .name = "&sbinfo->si_rwsem" },
36164 +               { .name = "&finfo->fi_rwsem" },
36165 +               { .name = "&dinfo->di_rwsem" },
36166 +               { .name = "&iinfo->ii_rwsem" }
36167 +       };
36168 +       static int set;
36169 +       int i;
36170 +
36171 +       /* lockless read from 'set.' see below */
36172 +       if (set == ARRAY_SIZE(a)) {
36173 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36174 +                       if (a[i].key == key)
36175 +                               goto match;
36176 +               goto unmatch;
36177 +       }
36178 +
36179 +       spin_lock(&spin);
36180 +       if (set)
36181 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36182 +                       if (a[i].key == key) {
36183 +                               spin_unlock(&spin);
36184 +                               goto match;
36185 +                       }
36186 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36187 +               if (a[i].key) {
36188 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36189 +                               spin_unlock(&spin);
36190 +                               goto match;
36191 +                       } else
36192 +                               continue;
36193 +               }
36194 +               if (strstr(a[i].name, name)) {
36195 +                       /*
36196 +                        * the order of these three lines is important for the
36197 +                        * lockless read above.
36198 +                        */
36199 +                       a[i].key = key;
36200 +                       spin_unlock(&spin);
36201 +                       set++;
36202 +                       /* AuDbg("%d, %s\n", set, name); */
36203 +                       goto match;
36204 +               }
36205 +       }
36206 +       spin_unlock(&spin);
36207 +       goto unmatch;
36208 +
36209 +match:
36210 +       return 1;
36211 +unmatch:
36212 +       return 0;
36213 +}
36214 +
36215 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36216 +{
36217 +       int err, n;
36218 +       struct task_struct *curr;
36219 +       struct held_lock **hl, *held_locks, *p;
36220 +
36221 +       err = 0;
36222 +       curr = current;
36223 +       wkinfo->dont_check = lockdep_recursing(curr);
36224 +       if (wkinfo->dont_check)
36225 +               goto out;
36226 +       n = curr->lockdep_depth;
36227 +       if (!n)
36228 +               goto out;
36229 +
36230 +       err = -ENOMEM;
36231 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36232 +       if (unlikely(!wkinfo->hlock))
36233 +               goto out;
36234 +
36235 +       err = 0;
36236 +#if 0
36237 +       if (0 && au_debug_test()) /* left for debugging */
36238 +               lockdep_print_held_locks(curr);
36239 +#endif
36240 +       held_locks = curr->held_locks;
36241 +       hl = wkinfo->hlock;
36242 +       while (n--) {
36243 +               p = held_locks++;
36244 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36245 +                       *hl++ = p;
36246 +       }
36247 +       *hl = NULL;
36248 +
36249 +out:
36250 +       return err;
36251 +}
36252 +
36253 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36254 +{
36255 +       au_kfree_try_rcu(wkinfo->hlock);
36256 +}
36257 +
36258 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36259 +{
36260 +       struct held_lock *p, **hl = wkinfo->hlock;
36261 +       int subclass;
36262 +
36263 +       if (wkinfo->dont_check)
36264 +               lockdep_off();
36265 +       if (!hl)
36266 +               return;
36267 +       while ((p = *hl++)) { /* assignment */
36268 +               subclass = lockdep_hlock_class(p)->subclass;
36269 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36270 +               if (p->read)
36271 +                       rwsem_acquire_read(p->instance, subclass, 0,
36272 +                                          /*p->acquire_ip*/_RET_IP_);
36273 +               else
36274 +                       rwsem_acquire(p->instance, subclass, 0,
36275 +                                     /*p->acquire_ip*/_RET_IP_);
36276 +       }
36277 +}
36278 +
36279 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36280 +{
36281 +       struct held_lock *p, **hl = wkinfo->hlock;
36282 +
36283 +       if (wkinfo->dont_check)
36284 +               lockdep_on();
36285 +       if (!hl)
36286 +               return;
36287 +       while ((p = *hl++)) /* assignment */
36288 +               rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
36289 +}
36290 +#endif
36291 +
36292 +static void wkq_func(struct work_struct *wk)
36293 +{
36294 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36295 +
36296 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36297 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36298 +
36299 +       au_wkq_lockdep_pre(wkinfo);
36300 +       wkinfo->func(wkinfo->args);
36301 +       au_wkq_lockdep_post(wkinfo);
36302 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36303 +               complete(wkinfo->comp);
36304 +       else {
36305 +               kobject_put(wkinfo->kobj);
36306 +               module_put(THIS_MODULE); /* todo: ?? */
36307 +               au_kfree_rcu(wkinfo);
36308 +       }
36309 +}
36310 +
36311 +/*
36312 + * Since struct completion is large, try allocating it dynamically.
36313 + */
36314 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
36315 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36316 +
36317 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36318 +{
36319 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36320 +       if (*comp) {
36321 +               init_completion(*comp);
36322 +               wkinfo->comp = *comp;
36323 +               return 0;
36324 +       }
36325 +       return -ENOMEM;
36326 +}
36327 +
36328 +static void au_wkq_comp_free(struct completion *comp)
36329 +{
36330 +       au_kfree_rcu(comp);
36331 +}
36332 +
36333 +#else
36334 +
36335 +/* no braces */
36336 +#define AuWkqCompDeclare(name) \
36337 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
36338 +       struct completion *comp = &_ ## name
36339 +
36340 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36341 +{
36342 +       wkinfo->comp = *comp;
36343 +       return 0;
36344 +}
36345 +
36346 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
36347 +{
36348 +       /* empty */
36349 +}
36350 +#endif /* 4KSTACKS */
36351 +
36352 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36353 +{
36354 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36355 +               if (au_wkq_test()) {
36356 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36357 +                               " due to a dead dir by UDBA,"
36358 +                               " or async xino write?\n");
36359 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36360 +               }
36361 +       } else
36362 +               au_dbg_verify_kthread();
36363 +
36364 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36365 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36366 +               queue_work(au_wkq, &wkinfo->wk);
36367 +       } else {
36368 +               INIT_WORK(&wkinfo->wk, wkq_func);
36369 +               schedule_work(&wkinfo->wk);
36370 +       }
36371 +}
36372 +
36373 +/*
36374 + * Be careful. It is easy to make deadlock happen.
36375 + * processA: lock, wkq and wait
36376 + * processB: wkq and wait, lock in wkq
36377 + * --> deadlock
36378 + */
36379 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36380 +{
36381 +       int err;
36382 +       AuWkqCompDeclare(comp);
36383 +       struct au_wkinfo wkinfo = {
36384 +               .flags  = flags,
36385 +               .func   = func,
36386 +               .args   = args
36387 +       };
36388 +
36389 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36390 +       if (unlikely(err))
36391 +               goto out;
36392 +       err = au_wkq_lockdep_alloc(&wkinfo);
36393 +       if (unlikely(err))
36394 +               goto out_comp;
36395 +       if (!err) {
36396 +               au_wkq_run(&wkinfo);
36397 +               /* no timeout, no interrupt */
36398 +               wait_for_completion(wkinfo.comp);
36399 +       }
36400 +       au_wkq_lockdep_free(&wkinfo);
36401 +
36402 +out_comp:
36403 +       au_wkq_comp_free(comp);
36404 +out:
36405 +       destroy_work_on_stack(&wkinfo.wk);
36406 +       return err;
36407 +}
36408 +
36409 +/*
36410 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36411 + * problem in a concurrent umounting.
36412 + */
36413 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36414 +                 unsigned int flags)
36415 +{
36416 +       int err;
36417 +       struct au_wkinfo *wkinfo;
36418 +
36419 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36420 +
36421 +       /*
36422 +        * wkq_func() must free this wkinfo.
36423 +        * it highly depends upon the implementation of workqueue.
36424 +        */
36425 +       err = 0;
36426 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36427 +       if (wkinfo) {
36428 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36429 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36430 +               wkinfo->func = func;
36431 +               wkinfo->args = args;
36432 +               wkinfo->comp = NULL;
36433 +               au_wkq_lockdep_init(wkinfo);
36434 +               kobject_get(wkinfo->kobj);
36435 +               __module_get(THIS_MODULE); /* todo: ?? */
36436 +
36437 +               au_wkq_run(wkinfo);
36438 +       } else {
36439 +               err = -ENOMEM;
36440 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36441 +       }
36442 +
36443 +       return err;
36444 +}
36445 +
36446 +/* ---------------------------------------------------------------------- */
36447 +
36448 +void au_nwt_init(struct au_nowait_tasks *nwt)
36449 +{
36450 +       atomic_set(&nwt->nw_len, 0);
36451 +       /* smp_mb(); */ /* atomic_set */
36452 +       init_waitqueue_head(&nwt->nw_wq);
36453 +}
36454 +
36455 +void au_wkq_fin(void)
36456 +{
36457 +       destroy_workqueue(au_wkq);
36458 +}
36459 +
36460 +int __init au_wkq_init(void)
36461 +{
36462 +       int err;
36463 +
36464 +       err = 0;
36465 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36466 +       if (IS_ERR(au_wkq))
36467 +               err = PTR_ERR(au_wkq);
36468 +       else if (!au_wkq)
36469 +               err = -ENOMEM;
36470 +
36471 +       return err;
36472 +}
36473 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36474 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36475 +++ linux/fs/aufs/wkq.h 2018-10-23 12:33:35.599375796 +0200
36476 @@ -0,0 +1,89 @@
36477 +/* SPDX-License-Identifier: GPL-2.0 */
36478 +/*
36479 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36480 + *
36481 + * This program, aufs is free software; you can redistribute it and/or modify
36482 + * it under the terms of the GNU General Public License as published by
36483 + * the Free Software Foundation; either version 2 of the License, or
36484 + * (at your option) any later version.
36485 + *
36486 + * This program is distributed in the hope that it will be useful,
36487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36489 + * GNU General Public License for more details.
36490 + *
36491 + * You should have received a copy of the GNU General Public License
36492 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36493 + */
36494 +
36495 +/*
36496 + * workqueue for asynchronous/super-io operations
36497 + * todo: try new credentials management scheme
36498 + */
36499 +
36500 +#ifndef __AUFS_WKQ_H__
36501 +#define __AUFS_WKQ_H__
36502 +
36503 +#ifdef __KERNEL__
36504 +
36505 +#include <linux/wait.h>
36506 +
36507 +struct super_block;
36508 +
36509 +/* ---------------------------------------------------------------------- */
36510 +
36511 +/*
36512 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36513 + */
36514 +struct au_nowait_tasks {
36515 +       atomic_t                nw_len;
36516 +       wait_queue_head_t       nw_wq;
36517 +};
36518 +
36519 +/* ---------------------------------------------------------------------- */
36520 +
36521 +typedef void (*au_wkq_func_t)(void *args);
36522 +
36523 +/* wkq flags */
36524 +#define AuWkq_WAIT     1
36525 +#define AuWkq_NEST     (1 << 1)
36526 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36527 +#define au_fset_wkq(flags, name) \
36528 +       do { (flags) |= AuWkq_##name; } while (0)
36529 +#define au_fclr_wkq(flags, name) \
36530 +       do { (flags) &= ~AuWkq_##name; } while (0)
36531 +
36532 +/* wkq.c */
36533 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36534 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36535 +                 unsigned int flags);
36536 +void au_nwt_init(struct au_nowait_tasks *nwt);
36537 +int __init au_wkq_init(void);
36538 +void au_wkq_fin(void);
36539 +
36540 +/* ---------------------------------------------------------------------- */
36541 +
36542 +static inline int au_wkq_test(void)
36543 +{
36544 +       return current->flags & PF_WQ_WORKER;
36545 +}
36546 +
36547 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36548 +{
36549 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36550 +}
36551 +
36552 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36553 +{
36554 +       if (atomic_dec_and_test(&nwt->nw_len))
36555 +               wake_up_all(&nwt->nw_wq);
36556 +}
36557 +
36558 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36559 +{
36560 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36561 +       return 0;
36562 +}
36563 +
36564 +#endif /* __KERNEL__ */
36565 +#endif /* __AUFS_WKQ_H__ */
36566 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36567 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36568 +++ linux/fs/aufs/xattr.c       2018-12-27 13:19:17.711749485 +0100
36569 @@ -0,0 +1,356 @@
36570 +// SPDX-License-Identifier: GPL-2.0
36571 +/*
36572 + * Copyright (C) 2014-2018 Junjiro R. Okajima
36573 + *
36574 + * This program, aufs is free software; you can redistribute it and/or modify
36575 + * it under the terms of the GNU General Public License as published by
36576 + * the Free Software Foundation; either version 2 of the License, or
36577 + * (at your option) any later version.
36578 + *
36579 + * This program is distributed in the hope that it will be useful,
36580 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36581 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36582 + * GNU General Public License for more details.
36583 + *
36584 + * You should have received a copy of the GNU General Public License
36585 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36586 + */
36587 +
36588 +/*
36589 + * handling xattr functions
36590 + */
36591 +
36592 +#include <linux/fs.h>
36593 +#include <linux/posix_acl_xattr.h>
36594 +#include <linux/xattr.h>
36595 +#include "aufs.h"
36596 +
36597 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36598 +{
36599 +       if (!ignore_flags)
36600 +               goto out;
36601 +       switch (err) {
36602 +       case -ENOMEM:
36603 +       case -EDQUOT:
36604 +               goto out;
36605 +       }
36606 +
36607 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36608 +               err = 0;
36609 +               goto out;
36610 +       }
36611 +
36612 +#define cmp(brattr, prefix) do {                                       \
36613 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36614 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36615 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36616 +                               err = 0;                                \
36617 +                       goto out;                                       \
36618 +               }                                                       \
36619 +       } while (0)
36620 +
36621 +       cmp(SEC, SECURITY);
36622 +       cmp(SYS, SYSTEM);
36623 +       cmp(TR, TRUSTED);
36624 +       cmp(USR, USER);
36625 +#undef cmp
36626 +
36627 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36628 +               err = 0;
36629 +
36630 +out:
36631 +       return err;
36632 +}
36633 +
36634 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36635 +
36636 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36637 +                           char *name, char **buf, unsigned int ignore_flags,
36638 +                           unsigned int verbose)
36639 +{
36640 +       int err;
36641 +       ssize_t ssz;
36642 +       struct inode *h_idst;
36643 +
36644 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36645 +       err = ssz;
36646 +       if (unlikely(err <= 0)) {
36647 +               if (err == -ENODATA
36648 +                   || (err == -EOPNOTSUPP
36649 +                       && ((ignore_flags & au_xattr_out_of_list)
36650 +                           || (au_test_nfs_noacl(d_inode(h_src))
36651 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36652 +                                   || !strcmp(name,
36653 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36654 +                           ))
36655 +                       err = 0;
36656 +               if (err && (verbose || au_debug_test()))
36657 +                       pr_err("%s, err %d\n", name, err);
36658 +               goto out;
36659 +       }
36660 +
36661 +       /* unlock it temporary */
36662 +       h_idst = d_inode(h_dst);
36663 +       inode_unlock(h_idst);
36664 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36665 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36666 +       if (unlikely(err)) {
36667 +               if (verbose || au_debug_test())
36668 +                       pr_err("%s, err %d\n", name, err);
36669 +               err = au_xattr_ignore(err, name, ignore_flags);
36670 +       }
36671 +
36672 +out:
36673 +       return err;
36674 +}
36675 +
36676 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36677 +                 unsigned int verbose)
36678 +{
36679 +       int err, unlocked, acl_access, acl_default;
36680 +       ssize_t ssz;
36681 +       struct inode *h_isrc, *h_idst;
36682 +       char *value, *p, *o, *e;
36683 +
36684 +       /* try stopping to update the source inode while we are referencing */
36685 +       /* there should not be the parent-child relationship between them */
36686 +       h_isrc = d_inode(h_src);
36687 +       h_idst = d_inode(h_dst);
36688 +       inode_unlock(h_idst);
36689 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36690 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36691 +       unlocked = 0;
36692 +
36693 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36694 +       ssz = vfs_listxattr(h_src, NULL, 0);
36695 +       err = ssz;
36696 +       if (unlikely(err < 0)) {
36697 +               AuTraceErr(err);
36698 +               if (err == -ENODATA
36699 +                   || err == -EOPNOTSUPP)
36700 +                       err = 0;        /* ignore */
36701 +               goto out;
36702 +       }
36703 +
36704 +       err = 0;
36705 +       p = NULL;
36706 +       o = NULL;
36707 +       if (ssz) {
36708 +               err = -ENOMEM;
36709 +               p = kmalloc(ssz, GFP_NOFS);
36710 +               o = p;
36711 +               if (unlikely(!p))
36712 +                       goto out;
36713 +               err = vfs_listxattr(h_src, p, ssz);
36714 +       }
36715 +       inode_unlock_shared(h_isrc);
36716 +       unlocked = 1;
36717 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36718 +       if (unlikely(err < 0))
36719 +               goto out_free;
36720 +
36721 +       err = 0;
36722 +       e = p + ssz;
36723 +       value = NULL;
36724 +       acl_access = 0;
36725 +       acl_default = 0;
36726 +       while (!err && p < e) {
36727 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36728 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36729 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36730 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36731 +                                       - 1);
36732 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36733 +                                      verbose);
36734 +               p += strlen(p) + 1;
36735 +       }
36736 +       AuTraceErr(err);
36737 +       ignore_flags |= au_xattr_out_of_list;
36738 +       if (!err && !acl_access) {
36739 +               err = au_do_cpup_xattr(h_dst, h_src,
36740 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36741 +                                      ignore_flags, verbose);
36742 +               AuTraceErr(err);
36743 +       }
36744 +       if (!err && !acl_default) {
36745 +               err = au_do_cpup_xattr(h_dst, h_src,
36746 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36747 +                                      ignore_flags, verbose);
36748 +               AuTraceErr(err);
36749 +       }
36750 +
36751 +       au_kfree_try_rcu(value);
36752 +
36753 +out_free:
36754 +       au_kfree_try_rcu(o);
36755 +out:
36756 +       if (!unlocked)
36757 +               inode_unlock_shared(h_isrc);
36758 +       AuTraceErr(err);
36759 +       return err;
36760 +}
36761 +
36762 +/* ---------------------------------------------------------------------- */
36763 +
36764 +static int au_smack_reentering(struct super_block *sb)
36765 +{
36766 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36767 +       /*
36768 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36769 +        * i_op->getxattr(). ouch.
36770 +        */
36771 +       return si_pid_test(sb);
36772 +#else
36773 +       return 0;
36774 +#endif
36775 +}
36776 +
36777 +enum {
36778 +       AU_XATTR_LIST,
36779 +       AU_XATTR_GET
36780 +};
36781 +
36782 +struct au_lgxattr {
36783 +       int type;
36784 +       union {
36785 +               struct {
36786 +                       char    *list;
36787 +                       size_t  size;
36788 +               } list;
36789 +               struct {
36790 +                       const char      *name;
36791 +                       void            *value;
36792 +                       size_t          size;
36793 +               } get;
36794 +       } u;
36795 +};
36796 +
36797 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36798 +{
36799 +       ssize_t err;
36800 +       int reenter;
36801 +       struct path h_path;
36802 +       struct super_block *sb;
36803 +
36804 +       sb = dentry->d_sb;
36805 +       reenter = au_smack_reentering(sb);
36806 +       if (!reenter) {
36807 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36808 +               if (unlikely(err))
36809 +                       goto out;
36810 +       }
36811 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
36812 +       if (unlikely(err))
36813 +               goto out_si;
36814 +       if (unlikely(!h_path.dentry))
36815 +               /* illegally overlapped or something */
36816 +               goto out_di; /* pretending success */
36817 +
36818 +       /* always topmost entry only */
36819 +       switch (arg->type) {
36820 +       case AU_XATTR_LIST:
36821 +               err = vfs_listxattr(h_path.dentry,
36822 +                                   arg->u.list.list, arg->u.list.size);
36823 +               break;
36824 +       case AU_XATTR_GET:
36825 +               AuDebugOn(d_is_negative(h_path.dentry));
36826 +               err = vfs_getxattr(h_path.dentry,
36827 +                                  arg->u.get.name, arg->u.get.value,
36828 +                                  arg->u.get.size);
36829 +               break;
36830 +       }
36831 +
36832 +out_di:
36833 +       if (!reenter)
36834 +               di_read_unlock(dentry, AuLock_IR);
36835 +out_si:
36836 +       if (!reenter)
36837 +               si_read_unlock(sb);
36838 +out:
36839 +       AuTraceErr(err);
36840 +       return err;
36841 +}
36842 +
36843 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36844 +{
36845 +       struct au_lgxattr arg = {
36846 +               .type = AU_XATTR_LIST,
36847 +               .u.list = {
36848 +                       .list   = list,
36849 +                       .size   = size
36850 +               },
36851 +       };
36852 +
36853 +       return au_lgxattr(dentry, &arg);
36854 +}
36855 +
36856 +static ssize_t au_getxattr(struct dentry *dentry,
36857 +                          struct inode *inode __maybe_unused,
36858 +                          const char *name, void *value, size_t size)
36859 +{
36860 +       struct au_lgxattr arg = {
36861 +               .type = AU_XATTR_GET,
36862 +               .u.get = {
36863 +                       .name   = name,
36864 +                       .value  = value,
36865 +                       .size   = size
36866 +               },
36867 +       };
36868 +
36869 +       return au_lgxattr(dentry, &arg);
36870 +}
36871 +
36872 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36873 +                      const char *name, const void *value, size_t size,
36874 +                      int flags)
36875 +{
36876 +       struct au_sxattr arg = {
36877 +               .type = AU_XATTR_SET,
36878 +               .u.set = {
36879 +                       .name   = name,
36880 +                       .value  = value,
36881 +                       .size   = size,
36882 +                       .flags  = flags
36883 +               },
36884 +       };
36885 +
36886 +       return au_sxattr(dentry, inode, &arg);
36887 +}
36888 +
36889 +/* ---------------------------------------------------------------------- */
36890 +
36891 +static int au_xattr_get(const struct xattr_handler *handler,
36892 +                       struct dentry *dentry, struct inode *inode,
36893 +                       const char *name, void *buffer, size_t size)
36894 +{
36895 +       return au_getxattr(dentry, inode, name, buffer, size);
36896 +}
36897 +
36898 +static int au_xattr_set(const struct xattr_handler *handler,
36899 +                       struct dentry *dentry, struct inode *inode,
36900 +                       const char *name, const void *value, size_t size,
36901 +                       int flags)
36902 +{
36903 +       return au_setxattr(dentry, inode, name, value, size, flags);
36904 +}
36905 +
36906 +static const struct xattr_handler au_xattr_handler = {
36907 +       .name   = "",
36908 +       .prefix = "",
36909 +       .get    = au_xattr_get,
36910 +       .set    = au_xattr_set
36911 +};
36912 +
36913 +static const struct xattr_handler *au_xattr_handlers[] = {
36914 +#ifdef CONFIG_FS_POSIX_ACL
36915 +       &posix_acl_access_xattr_handler,
36916 +       &posix_acl_default_xattr_handler,
36917 +#endif
36918 +       &au_xattr_handler, /* must be last */
36919 +       NULL
36920 +};
36921 +
36922 +void au_xattr_init(struct super_block *sb)
36923 +{
36924 +       sb->s_xattr = au_xattr_handlers;
36925 +}
36926 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36927 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36928 +++ linux/fs/aufs/xino.c        2018-12-27 13:19:17.715082917 +0100
36929 @@ -0,0 +1,1956 @@
36930 +// SPDX-License-Identifier: GPL-2.0
36931 +/*
36932 + * Copyright (C) 2005-2018 Junjiro R. Okajima
36933 + *
36934 + * This program, aufs is free software; you can redistribute it and/or modify
36935 + * it under the terms of the GNU General Public License as published by
36936 + * the Free Software Foundation; either version 2 of the License, or
36937 + * (at your option) any later version.
36938 + *
36939 + * This program is distributed in the hope that it will be useful,
36940 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36941 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36942 + * GNU General Public License for more details.
36943 + *
36944 + * You should have received a copy of the GNU General Public License
36945 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36946 + */
36947 +
36948 +/*
36949 + * external inode number translation table and bitmap
36950 + *
36951 + * things to consider
36952 + * - the lifetime
36953 + *   + au_xino object
36954 + *   + XINO files (xino, xib, xigen)
36955 + *   + dynamic debugfs entries (xiN)
36956 + *   + static debugfs entries (xib, xigen)
36957 + *   + static sysfs entry (xi_path)
36958 + * - several entry points to handle them.
36959 + *   + mount(2) without xino option (default)
36960 + *   + mount(2) with xino option
36961 + *   + mount(2) with noxino option
36962 + *   + umount(2)
36963 + *   + remount with add/del branches
36964 + *   + remount with xino/noxino options
36965 + */
36966 +
36967 +#include <linux/seq_file.h>
36968 +#include <linux/statfs.h>
36969 +#include "aufs.h"
36970 +
36971 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36972 +                                    aufs_bindex_t bbot,
36973 +                                    struct super_block *h_sb)
36974 +{
36975 +       /* todo: try binary-search if the branches are many */
36976 +       for (; btop <= bbot; btop++)
36977 +               if (h_sb == au_sbr_sb(sb, btop))
36978 +                       return btop;
36979 +       return -1;
36980 +}
36981 +
36982 +/*
36983 + * find another branch who is on the same filesystem of the specified
36984 + * branch{@btgt}. search until @bbot.
36985 + */
36986 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36987 +                                 aufs_bindex_t bbot)
36988 +{
36989 +       aufs_bindex_t bindex;
36990 +       struct super_block *tgt_sb;
36991 +
36992 +       tgt_sb = au_sbr_sb(sb, btgt);
36993 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36994 +       if (bindex < 0)
36995 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36996 +
36997 +       return bindex;
36998 +}
36999 +
37000 +/* ---------------------------------------------------------------------- */
37001 +
37002 +/*
37003 + * stop unnecessary notify events at creating xino files
37004 + */
37005 +
37006 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37007 +{
37008 +       aufs_bindex_t bfound, bindex, bbot;
37009 +       struct dentry *parent;
37010 +       struct au_branch *br;
37011 +
37012 +       bfound = -1;
37013 +       parent = dentry->d_parent; /* safe d_parent access */
37014 +       bbot = au_sbbot(sb);
37015 +       for (bindex = 0; bindex <= bbot; bindex++) {
37016 +               br = au_sbr(sb, bindex);
37017 +               if (au_br_dentry(br) == parent) {
37018 +                       bfound = bindex;
37019 +                       break;
37020 +               }
37021 +       }
37022 +
37023 +       AuDbg("bfound b%d\n", bfound);
37024 +       return bfound;
37025 +}
37026 +
37027 +struct au_xino_lock_dir {
37028 +       struct au_hinode *hdir;
37029 +       struct dentry *parent;
37030 +       struct inode *dir;
37031 +};
37032 +
37033 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37034 +                                         unsigned int lsc)
37035 +{
37036 +       struct dentry *parent;
37037 +       struct inode *dir;
37038 +
37039 +       parent = dget_parent(dentry);
37040 +       dir = d_inode(parent);
37041 +       inode_lock_nested(dir, lsc);
37042 +#if 0 /* it should not happen */
37043 +       spin_lock(&dentry->d_lock);
37044 +       if (unlikely(dentry->d_parent != parent)) {
37045 +               spin_unlock(&dentry->d_lock);
37046 +               inode_unlock(dir);
37047 +               dput(parent);
37048 +               parent = NULL;
37049 +               goto out;
37050 +       }
37051 +       spin_unlock(&dentry->d_lock);
37052 +
37053 +out:
37054 +#endif
37055 +       return parent;
37056 +}
37057 +
37058 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37059 +                            struct au_xino_lock_dir *ldir)
37060 +{
37061 +       aufs_bindex_t bindex;
37062 +
37063 +       ldir->hdir = NULL;
37064 +       bindex = au_xi_root(sb, xipath->dentry);
37065 +       if (bindex >= 0) {
37066 +               /* rw branch root */
37067 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37068 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37069 +       } else {
37070 +               /* other */
37071 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37072 +                                                  AuLsc_I_PARENT);
37073 +               ldir->dir = d_inode(ldir->parent);
37074 +       }
37075 +}
37076 +
37077 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37078 +{
37079 +       if (ldir->hdir)
37080 +               au_hn_inode_unlock(ldir->hdir);
37081 +       else {
37082 +               inode_unlock(ldir->dir);
37083 +               dput(ldir->parent);
37084 +       }
37085 +}
37086 +
37087 +/* ---------------------------------------------------------------------- */
37088 +
37089 +/*
37090 + * create and set a new xino file
37091 + */
37092 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
37093 +{
37094 +       struct file *file;
37095 +       struct dentry *h_parent, *d;
37096 +       struct inode *h_dir, *inode;
37097 +       int err;
37098 +
37099 +       /*
37100 +        * at mount-time, and the xino file is the default path,
37101 +        * hnotify is disabled so we have no notify events to ignore.
37102 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37103 +        */
37104 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37105 +                              /* | __FMODE_NONOTIFY */,
37106 +                              0666);
37107 +       if (IS_ERR(file)) {
37108 +               if (!silent)
37109 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37110 +               return file;
37111 +       }
37112 +
37113 +       /* keep file count */
37114 +       err = 0;
37115 +       d = file->f_path.dentry;
37116 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37117 +       /* mnt_want_write() is unnecessary here */
37118 +       h_dir = d_inode(h_parent);
37119 +       inode = file_inode(file);
37120 +       /* no delegation since it is just created */
37121 +       if (inode->i_nlink)
37122 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37123 +                                  /*force*/0);
37124 +       inode_unlock(h_dir);
37125 +       dput(h_parent);
37126 +       if (unlikely(err)) {
37127 +               if (!silent)
37128 +                       pr_err("unlink %s(%d)\n", fpath, err);
37129 +               goto out;
37130 +       }
37131 +
37132 +       err = -EINVAL;
37133 +       if (unlikely(sb == d->d_sb)) {
37134 +               if (!silent)
37135 +                       pr_err("%s must be outside\n", fpath);
37136 +               goto out;
37137 +       }
37138 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37139 +               if (!silent)
37140 +                       pr_err("xino doesn't support %s(%s)\n",
37141 +                              fpath, au_sbtype(d->d_sb));
37142 +               goto out;
37143 +       }
37144 +       return file; /* success */
37145 +
37146 +out:
37147 +       fput(file);
37148 +       file = ERR_PTR(err);
37149 +       return file;
37150 +}
37151 +
37152 +/*
37153 + * create a new xinofile at the same place/path as @base.
37154 + */
37155 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37156 +                            struct file *copy_src)
37157 +{
37158 +       struct file *file;
37159 +       struct dentry *dentry, *parent;
37160 +       struct inode *dir, *delegated;
37161 +       struct qstr *name;
37162 +       struct path path;
37163 +       int err, do_unlock;
37164 +       struct au_xino_lock_dir ldir;
37165 +
37166 +       do_unlock = 1;
37167 +       au_xino_lock_dir(sb, base, &ldir);
37168 +       dentry = base->dentry;
37169 +       parent = dentry->d_parent; /* dir inode is locked */
37170 +       dir = d_inode(parent);
37171 +       IMustLock(dir);
37172 +
37173 +       name = &dentry->d_name;
37174 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37175 +       if (IS_ERR(path.dentry)) {
37176 +               file = (void *)path.dentry;
37177 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37178 +               goto out;
37179 +       }
37180 +
37181 +       /* no need to mnt_want_write() since we call dentry_open() later */
37182 +       err = vfs_create(dir, path.dentry, 0666, NULL);
37183 +       if (unlikely(err)) {
37184 +               file = ERR_PTR(err);
37185 +               pr_err("%pd create err %d\n", dentry, err);
37186 +               goto out_dput;
37187 +       }
37188 +
37189 +       path.mnt = base->mnt;
37190 +       file = vfsub_dentry_open(&path,
37191 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37192 +                                /* | __FMODE_NONOTIFY */);
37193 +       if (IS_ERR(file)) {
37194 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37195 +               goto out_dput;
37196 +       }
37197 +
37198 +       delegated = NULL;
37199 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37200 +       au_xino_unlock_dir(&ldir);
37201 +       do_unlock = 0;
37202 +       if (unlikely(err == -EWOULDBLOCK)) {
37203 +               pr_warn("cannot retry for NFSv4 delegation"
37204 +                       " for an internal unlink\n");
37205 +               iput(delegated);
37206 +       }
37207 +       if (unlikely(err)) {
37208 +               pr_err("%pd unlink err %d\n", dentry, err);
37209 +               goto out_fput;
37210 +       }
37211 +
37212 +       if (copy_src) {
37213 +               /* no one can touch copy_src xino */
37214 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37215 +               if (unlikely(err)) {
37216 +                       pr_err("%pd copy err %d\n", dentry, err);
37217 +                       goto out_fput;
37218 +               }
37219 +       }
37220 +       goto out_dput; /* success */
37221 +
37222 +out_fput:
37223 +       fput(file);
37224 +       file = ERR_PTR(err);
37225 +out_dput:
37226 +       dput(path.dentry);
37227 +out:
37228 +       if (do_unlock)
37229 +               au_xino_unlock_dir(&ldir);
37230 +       return file;
37231 +}
37232 +
37233 +struct file *au_xino_file1(struct au_xino *xi)
37234 +{
37235 +       struct file *file;
37236 +       unsigned int u, nfile;
37237 +
37238 +       file = NULL;
37239 +       nfile = xi->xi_nfile;
37240 +       for (u = 0; u < nfile; u++) {
37241 +               file = xi->xi_file[u];
37242 +               if (file)
37243 +                       break;
37244 +       }
37245 +
37246 +       return file;
37247 +}
37248 +
37249 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37250 +{
37251 +       int err;
37252 +       struct file *f;
37253 +       void *p;
37254 +
37255 +       if (file)
37256 +               get_file(file);
37257 +
37258 +       err = 0;
37259 +       f = NULL;
37260 +       if (idx < xi->xi_nfile) {
37261 +               f = xi->xi_file[idx];
37262 +               if (f)
37263 +                       fput(f);
37264 +       } else {
37265 +               p = au_kzrealloc(xi->xi_file,
37266 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37267 +                                sizeof(*xi->xi_file) * (idx + 1),
37268 +                                GFP_NOFS, /*may_shrink*/0);
37269 +               if (p) {
37270 +                       MtxMustLock(&xi->xi_mtx);
37271 +                       xi->xi_file = p;
37272 +                       xi->xi_nfile = idx + 1;
37273 +               } else {
37274 +                       err = -ENOMEM;
37275 +                       if (file)
37276 +                               fput(file);
37277 +                       goto out;
37278 +               }
37279 +       }
37280 +       xi->xi_file[idx] = file;
37281 +
37282 +out:
37283 +       return err;
37284 +}
37285 +
37286 +/*
37287 + * if @xinew->xi is not set, then create new xigen file.
37288 + */
37289 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37290 +{
37291 +       struct file *file;
37292 +       int err;
37293 +
37294 +       SiMustAnyLock(sb);
37295 +
37296 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37297 +       if (IS_ERR(file)) {
37298 +               err = PTR_ERR(file);
37299 +               pr_err("%s[%d], err %d\n",
37300 +                      xinew->xi ? "xino" : "xigen",
37301 +                      xinew->idx, err);
37302 +               goto out;
37303 +       }
37304 +
37305 +       if (xinew->xi)
37306 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37307 +       else {
37308 +               BUG();
37309 +               /* todo: make xigen file an array */
37310 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37311 +       }
37312 +       fput(file);
37313 +       if (unlikely(err))
37314 +               file = ERR_PTR(err);
37315 +
37316 +out:
37317 +       return file;
37318 +}
37319 +
37320 +/* ---------------------------------------------------------------------- */
37321 +
37322 +/*
37323 + * truncate xino files
37324 + */
37325 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37326 +                           int idx, struct kstatfs *st)
37327 +{
37328 +       int err;
37329 +       blkcnt_t blocks;
37330 +       struct file *file, *new_xino;
37331 +       struct au_xi_new xinew = {
37332 +               .idx = idx
37333 +       };
37334 +
37335 +       err = 0;
37336 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37337 +       file = au_xino_file(xinew.xi, idx);
37338 +       if (!file)
37339 +               goto out;
37340 +
37341 +       xinew.base = &file->f_path;
37342 +       err = vfs_statfs(xinew.base, st);
37343 +       if (unlikely(err)) {
37344 +               AuErr1("statfs err %d, ignored\n", err);
37345 +               err = 0;
37346 +               goto out;
37347 +       }
37348 +
37349 +       blocks = file_inode(file)->i_blocks;
37350 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37351 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37352 +
37353 +       xinew.copy_src = file;
37354 +       new_xino = au_xi_new(sb, &xinew);
37355 +       if (IS_ERR(new_xino)) {
37356 +               err = PTR_ERR(new_xino);
37357 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37358 +               goto out;
37359 +       }
37360 +
37361 +       err = vfs_statfs(&new_xino->f_path, st);
37362 +       if (!err)
37363 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37364 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37365 +                       st->f_bfree, st->f_blocks);
37366 +       else {
37367 +               AuErr1("statfs err %d, ignored\n", err);
37368 +               err = 0;
37369 +       }
37370 +
37371 +out:
37372 +       return err;
37373 +}
37374 +
37375 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37376 +{
37377 +       int err, i;
37378 +       unsigned long jiffy;
37379 +       aufs_bindex_t bbot;
37380 +       struct kstatfs *st;
37381 +       struct au_branch *br;
37382 +       struct au_xino *xi;
37383 +
37384 +       err = -ENOMEM;
37385 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37386 +       if (unlikely(!st))
37387 +               goto out;
37388 +
37389 +       err = -EINVAL;
37390 +       bbot = au_sbbot(sb);
37391 +       if (unlikely(bindex < 0 || bbot < bindex))
37392 +               goto out_st;
37393 +
37394 +       err = 0;
37395 +       jiffy = jiffies;
37396 +       br = au_sbr(sb, bindex);
37397 +       xi = br->br_xino;
37398 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37399 +               err = au_xino_do_trunc(sb, bindex, i, st);
37400 +       if (!err)
37401 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37402 +
37403 +out_st:
37404 +       au_kfree_rcu(st);
37405 +out:
37406 +       return err;
37407 +}
37408 +
37409 +struct xino_do_trunc_args {
37410 +       struct super_block *sb;
37411 +       struct au_branch *br;
37412 +       int idx;
37413 +};
37414 +
37415 +static void xino_do_trunc(void *_args)
37416 +{
37417 +       struct xino_do_trunc_args *args = _args;
37418 +       struct super_block *sb;
37419 +       struct au_branch *br;
37420 +       struct inode *dir;
37421 +       int err, idx;
37422 +       aufs_bindex_t bindex;
37423 +
37424 +       err = 0;
37425 +       sb = args->sb;
37426 +       dir = d_inode(sb->s_root);
37427 +       br = args->br;
37428 +       idx = args->idx;
37429 +
37430 +       si_noflush_write_lock(sb);
37431 +       ii_read_lock_parent(dir);
37432 +       bindex = au_br_index(sb, br->br_id);
37433 +       err = au_xino_trunc(sb, bindex, idx);
37434 +       ii_read_unlock(dir);
37435 +       if (unlikely(err))
37436 +               pr_warn("err b%d, (%d)\n", bindex, err);
37437 +       atomic_dec(&br->br_xino->xi_truncating);
37438 +       au_lcnt_dec(&br->br_count);
37439 +       si_write_unlock(sb);
37440 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37441 +       au_kfree_rcu(args);
37442 +}
37443 +
37444 +/*
37445 + * returns the index in the xi_file array whose corresponding file is necessary
37446 + * to truncate, or -1 which means no need to truncate.
37447 + */
37448 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37449 +{
37450 +       int err;
37451 +       unsigned int u;
37452 +       struct kstatfs st;
37453 +       struct au_sbinfo *sbinfo;
37454 +       struct au_xino *xi;
37455 +       struct file *file;
37456 +
37457 +       /* todo: si_xino_expire and the ratio should be customizable */
37458 +       sbinfo = au_sbi(sb);
37459 +       if (time_before(jiffies,
37460 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37461 +               return -1;
37462 +
37463 +       /* truncation border */
37464 +       xi = br->br_xino;
37465 +       for (u = 0; u < xi->xi_nfile; u++) {
37466 +               file = au_xino_file(xi, u);
37467 +               if (!file)
37468 +                       continue;
37469 +
37470 +               err = vfs_statfs(&file->f_path, &st);
37471 +               if (unlikely(err)) {
37472 +                       AuErr1("statfs err %d, ignored\n", err);
37473 +                       return -1;
37474 +               }
37475 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37476 +                   >= AUFS_XINO_DEF_TRUNC)
37477 +                       return u;
37478 +       }
37479 +
37480 +       return -1;
37481 +}
37482 +
37483 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37484 +{
37485 +       int idx;
37486 +       struct xino_do_trunc_args *args;
37487 +       int wkq_err;
37488 +
37489 +       idx = xino_trunc_test(sb, br);
37490 +       if (idx < 0)
37491 +               return;
37492 +
37493 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37494 +               goto out;
37495 +
37496 +       /* lock and kfree() will be called in trunc_xino() */
37497 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37498 +       if (unlikely(!args)) {
37499 +               AuErr1("no memory\n");
37500 +               goto out;
37501 +       }
37502 +
37503 +       au_lcnt_inc(&br->br_count);
37504 +       args->sb = sb;
37505 +       args->br = br;
37506 +       args->idx = idx;
37507 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37508 +       if (!wkq_err)
37509 +               return; /* success */
37510 +
37511 +       pr_err("wkq %d\n", wkq_err);
37512 +       au_lcnt_dec(&br->br_count);
37513 +       au_kfree_rcu(args);
37514 +
37515 +out:
37516 +       atomic_dec(&br->br_xino->xi_truncating);
37517 +}
37518 +
37519 +/* ---------------------------------------------------------------------- */
37520 +
37521 +struct au_xi_calc {
37522 +       int idx;
37523 +       loff_t pos;
37524 +};
37525 +
37526 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37527 +                      struct au_xi_calc *calc)
37528 +{
37529 +       loff_t maxent;
37530 +
37531 +       maxent = au_xi_maxent(sb);
37532 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37533 +       calc->pos *= sizeof(ino_t);
37534 +}
37535 +
37536 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37537 +                               struct au_xi_calc *calc)
37538 +{
37539 +       int err;
37540 +       struct file *file;
37541 +       struct au_xino *xi = br->br_xino;
37542 +       struct au_xi_new xinew = {
37543 +               .xi = xi
37544 +       };
37545 +
37546 +       SiMustAnyLock(sb);
37547 +
37548 +       err = 0;
37549 +       if (!xi)
37550 +               goto out;
37551 +
37552 +       mutex_lock(&xi->xi_mtx);
37553 +       file = au_xino_file(xi, calc->idx);
37554 +       if (file)
37555 +               goto out_mtx;
37556 +
37557 +       file = au_xino_file(xi, /*idx*/-1);
37558 +       AuDebugOn(!file);
37559 +       xinew.idx = calc->idx;
37560 +       xinew.base = &file->f_path;
37561 +       /* xinew.copy_src = NULL; */
37562 +       file = au_xi_new(sb, &xinew);
37563 +       if (IS_ERR(file))
37564 +               err = PTR_ERR(file);
37565 +
37566 +out_mtx:
37567 +       mutex_unlock(&xi->xi_mtx);
37568 +out:
37569 +       return err;
37570 +}
37571 +
37572 +struct au_xino_do_new_async_args {
37573 +       struct super_block *sb;
37574 +       struct au_branch *br;
37575 +       struct au_xi_calc calc;
37576 +       ino_t ino;
37577 +};
37578 +
37579 +struct au_xi_writing {
37580 +       struct hlist_bl_node node;
37581 +       ino_t h_ino, ino;
37582 +};
37583 +
37584 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37585 +                           struct au_xi_calc *calc, ino_t ino);
37586 +
37587 +static void au_xino_call_do_new_async(void *args)
37588 +{
37589 +       struct au_xino_do_new_async_args *a = args;
37590 +       struct au_branch *br;
37591 +       struct super_block *sb;
37592 +       struct au_sbinfo *sbi;
37593 +       struct inode *root;
37594 +       struct file *file;
37595 +       struct au_xi_writing *del, *p;
37596 +       struct hlist_bl_head *hbl;
37597 +       struct hlist_bl_node *pos;
37598 +       int err;
37599 +
37600 +       br = a->br;
37601 +       sb = a->sb;
37602 +       sbi = au_sbi(sb);
37603 +       si_noflush_read_lock(sb);
37604 +       root = d_inode(sb->s_root);
37605 +       ii_read_lock_child(root);
37606 +       err = au_xino_do_new_async(sb, br, &a->calc);
37607 +       if (unlikely(err)) {
37608 +               AuIOErr("err %d\n", err);
37609 +               goto out;
37610 +       }
37611 +
37612 +       file = au_xino_file(br->br_xino, a->calc.idx);
37613 +       AuDebugOn(!file);
37614 +       err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37615 +       if (unlikely(err)) {
37616 +               AuIOErr("err %d\n", err);
37617 +               goto out;
37618 +       }
37619 +
37620 +       del = NULL;
37621 +       hbl = &br->br_xino->xi_writing;
37622 +       hlist_bl_lock(hbl);
37623 +       au_hbl_for_each(pos, hbl) {
37624 +               p = container_of(pos, struct au_xi_writing, node);
37625 +               if (p->ino == a->ino) {
37626 +                       del = p;
37627 +                       hlist_bl_del(&p->node);
37628 +                       break;
37629 +               }
37630 +       }
37631 +       hlist_bl_unlock(hbl);
37632 +       au_kfree_rcu(del);
37633 +
37634 +out:
37635 +       au_lcnt_dec(&br->br_count);
37636 +       ii_read_unlock(root);
37637 +       si_read_unlock(sb);
37638 +       au_nwt_done(&sbi->si_nowait);
37639 +       au_kfree_rcu(a);
37640 +}
37641 +
37642 +/*
37643 + * create a new xino file asynchronously
37644 + */
37645 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37646 +                            struct au_xi_calc *calc, ino_t ino)
37647 +{
37648 +       int err;
37649 +       struct au_xino_do_new_async_args *arg;
37650 +
37651 +       err = -ENOMEM;
37652 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37653 +       if (unlikely(!arg))
37654 +               goto out;
37655 +
37656 +       arg->sb = sb;
37657 +       arg->br = br;
37658 +       arg->calc = *calc;
37659 +       arg->ino = ino;
37660 +       au_lcnt_inc(&br->br_count);
37661 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37662 +       if (unlikely(err)) {
37663 +               pr_err("wkq %d\n", err);
37664 +               au_lcnt_dec(&br->br_count);
37665 +               au_kfree_rcu(arg);
37666 +       }
37667 +
37668 +out:
37669 +       return err;
37670 +}
37671 +
37672 +/*
37673 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37674 + * at the position of @h_ino.
37675 + */
37676 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37677 +                ino_t *ino)
37678 +{
37679 +       int err;
37680 +       ssize_t sz;
37681 +       struct au_xi_calc calc;
37682 +       struct au_sbinfo *sbinfo;
37683 +       struct file *file;
37684 +       struct au_xino *xi;
37685 +       struct hlist_bl_head *hbl;
37686 +       struct hlist_bl_node *pos;
37687 +       struct au_xi_writing *p;
37688 +
37689 +       *ino = 0;
37690 +       if (!au_opt_test(au_mntflags(sb), XINO))
37691 +               return 0; /* no xino */
37692 +
37693 +       err = 0;
37694 +       au_xi_calc(sb, h_ino, &calc);
37695 +       xi = au_sbr(sb, bindex)->br_xino;
37696 +       file = au_xino_file(xi, calc.idx);
37697 +       if (!file) {
37698 +               hbl = &xi->xi_writing;
37699 +               hlist_bl_lock(hbl);
37700 +               au_hbl_for_each(pos, hbl) {
37701 +                       p = container_of(pos, struct au_xi_writing, node);
37702 +                       if (p->h_ino == h_ino) {
37703 +                               AuDbg("hi%llu, i%llu, found\n",
37704 +                                     (u64)p->h_ino, (u64)p->ino);
37705 +                               *ino = p->ino;
37706 +                               break;
37707 +                       }
37708 +               }
37709 +               hlist_bl_unlock(hbl);
37710 +               return 0;
37711 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37712 +               return 0; /* no xino */
37713 +
37714 +       sbinfo = au_sbi(sb);
37715 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
37716 +       if (sz == sizeof(*ino))
37717 +               return 0; /* success */
37718 +
37719 +       err = sz;
37720 +       if (unlikely(sz >= 0)) {
37721 +               err = -EIO;
37722 +               AuIOErr("xino read error (%zd)\n", sz);
37723 +       }
37724 +       return err;
37725 +}
37726 +
37727 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37728 +                           struct au_xi_calc *calc, ino_t ino)
37729 +{
37730 +       ssize_t sz;
37731 +
37732 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
37733 +       if (sz == sizeof(ino))
37734 +               return 0; /* success */
37735 +
37736 +       AuIOErr("write failed (%zd)\n", sz);
37737 +       return -EIO;
37738 +}
37739 +
37740 +/*
37741 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37742 + * at the position of @h_ino.
37743 + * even if @ino is zero, it is written to the xinofile and means no entry.
37744 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37745 + * try truncating it.
37746 + */
37747 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37748 +                 ino_t ino)
37749 +{
37750 +       int err;
37751 +       unsigned int mnt_flags;
37752 +       struct au_xi_calc calc;
37753 +       struct file *file;
37754 +       struct au_branch *br;
37755 +       struct au_xino *xi;
37756 +       struct au_xi_writing *p;
37757 +
37758 +       SiMustAnyLock(sb);
37759 +
37760 +       mnt_flags = au_mntflags(sb);
37761 +       if (!au_opt_test(mnt_flags, XINO))
37762 +               return 0;
37763 +
37764 +       au_xi_calc(sb, h_ino, &calc);
37765 +       br = au_sbr(sb, bindex);
37766 +       xi = br->br_xino;
37767 +       file = au_xino_file(xi, calc.idx);
37768 +       if (!file) {
37769 +               /* store the inum pair into the list */
37770 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37771 +               p->h_ino = h_ino;
37772 +               p->ino = ino;
37773 +               au_hbl_add(&p->node, &xi->xi_writing);
37774 +
37775 +               /* create and write a new xino file asynchronously */
37776 +               err = au_xino_new_async(sb, br, &calc, ino);
37777 +               if (!err)
37778 +                       return 0; /* success */
37779 +               goto out;
37780 +       }
37781 +
37782 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
37783 +       if (!err) {
37784 +               br = au_sbr(sb, bindex);
37785 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37786 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37787 +                       xino_try_trunc(sb, br);
37788 +               return 0; /* success */
37789 +       }
37790 +
37791 +out:
37792 +       AuIOErr("write failed (%d)\n", err);
37793 +       return -EIO;
37794 +}
37795 +
37796 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37797 +                             size_t size, loff_t *pos);
37798 +
37799 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37800 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37801 +                  loff_t *pos)
37802 +{
37803 +       ssize_t err;
37804 +       mm_segment_t oldfs;
37805 +       union {
37806 +               void *k;
37807 +               char __user *u;
37808 +       } buf;
37809 +       int i;
37810 +       const int prevent_endless = 10;
37811 +
37812 +       i = 0;
37813 +       buf.k = kbuf;
37814 +       oldfs = get_fs();
37815 +       set_fs(KERNEL_DS);
37816 +       do {
37817 +               err = func(file, buf.u, size, pos);
37818 +               if (err == -EINTR
37819 +                   && !au_wkq_test()
37820 +                   && fatal_signal_pending(current)) {
37821 +                       set_fs(oldfs);
37822 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37823 +                       BUG_ON(err == -EINTR);
37824 +                       oldfs = get_fs();
37825 +                       set_fs(KERNEL_DS);
37826 +               }
37827 +       } while (i++ < prevent_endless
37828 +                && (err == -EAGAIN || err == -EINTR));
37829 +       set_fs(oldfs);
37830 +
37831 +#if 0 /* reserved for future use */
37832 +       if (err > 0)
37833 +               fsnotify_access(file->f_path.dentry);
37834 +#endif
37835 +
37836 +       return err;
37837 +}
37838 +
37839 +struct xino_fread_args {
37840 +       ssize_t *errp;
37841 +       vfs_readf_t func;
37842 +       struct file *file;
37843 +       void *buf;
37844 +       size_t size;
37845 +       loff_t *pos;
37846 +};
37847 +
37848 +static void call_xino_fread(void *args)
37849 +{
37850 +       struct xino_fread_args *a = args;
37851 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37852 +}
37853 +
37854 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37855 +                             size_t size, loff_t *pos)
37856 +{
37857 +       ssize_t err;
37858 +       int wkq_err;
37859 +       struct xino_fread_args args = {
37860 +               .errp   = &err,
37861 +               .func   = func,
37862 +               .file   = file,
37863 +               .buf    = buf,
37864 +               .size   = size,
37865 +               .pos    = pos
37866 +       };
37867 +
37868 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37869 +       if (unlikely(wkq_err))
37870 +               err = wkq_err;
37871 +
37872 +       return err;
37873 +}
37874 +
37875 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37876 +                              size_t size, loff_t *pos);
37877 +
37878 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37879 +                             size_t size, loff_t *pos)
37880 +{
37881 +       ssize_t err;
37882 +       mm_segment_t oldfs;
37883 +       union {
37884 +               void *k;
37885 +               const char __user *u;
37886 +       } buf;
37887 +       int i;
37888 +       const int prevent_endless = 10;
37889 +
37890 +       i = 0;
37891 +       buf.k = kbuf;
37892 +       oldfs = get_fs();
37893 +       set_fs(KERNEL_DS);
37894 +       do {
37895 +               err = func(file, buf.u, size, pos);
37896 +               if (err == -EINTR
37897 +                   && !au_wkq_test()
37898 +                   && fatal_signal_pending(current)) {
37899 +                       set_fs(oldfs);
37900 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37901 +                       BUG_ON(err == -EINTR);
37902 +                       oldfs = get_fs();
37903 +                       set_fs(KERNEL_DS);
37904 +               }
37905 +       } while (i++ < prevent_endless
37906 +                && (err == -EAGAIN || err == -EINTR));
37907 +       set_fs(oldfs);
37908 +
37909 +#if 0 /* reserved for future use */
37910 +       if (err > 0)
37911 +               fsnotify_modify(file->f_path.dentry);
37912 +#endif
37913 +
37914 +       return err;
37915 +}
37916 +
37917 +struct do_xino_fwrite_args {
37918 +       ssize_t *errp;
37919 +       vfs_writef_t func;
37920 +       struct file *file;
37921 +       void *buf;
37922 +       size_t size;
37923 +       loff_t *pos;
37924 +};
37925 +
37926 +static void call_do_xino_fwrite(void *args)
37927 +{
37928 +       struct do_xino_fwrite_args *a = args;
37929 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37930 +}
37931 +
37932 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37933 +                              size_t size, loff_t *pos)
37934 +{
37935 +       ssize_t err;
37936 +       int wkq_err;
37937 +       struct do_xino_fwrite_args args = {
37938 +               .errp   = &err,
37939 +               .func   = func,
37940 +               .file   = file,
37941 +               .buf    = buf,
37942 +               .size   = size,
37943 +               .pos    = pos
37944 +       };
37945 +
37946 +       /*
37947 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37948 +        * users should care about quota and real 'filesystem full.'
37949 +        */
37950 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37951 +       if (unlikely(wkq_err))
37952 +               err = wkq_err;
37953 +
37954 +       return err;
37955 +}
37956 +
37957 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37958 +                   size_t size, loff_t *pos)
37959 +{
37960 +       ssize_t err;
37961 +
37962 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37963 +               lockdep_off();
37964 +               err = do_xino_fwrite(func, file, buf, size, pos);
37965 +               lockdep_on();
37966 +       } else {
37967 +               lockdep_off();
37968 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37969 +               lockdep_on();
37970 +       }
37971 +
37972 +       return err;
37973 +}
37974 +
37975 +/* ---------------------------------------------------------------------- */
37976 +
37977 +/*
37978 + * inode number bitmap
37979 + */
37980 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37981 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37982 +{
37983 +       ino_t ino;
37984 +
37985 +       AuDebugOn(bit < 0 || page_bits <= bit);
37986 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37987 +       return ino;
37988 +}
37989 +
37990 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37991 +{
37992 +       AuDebugOn(ino < AUFS_FIRST_INO);
37993 +       ino -= AUFS_FIRST_INO;
37994 +       *pindex = ino / page_bits;
37995 +       *bit = ino % page_bits;
37996 +}
37997 +
37998 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37999 +{
38000 +       int err;
38001 +       loff_t pos;
38002 +       ssize_t sz;
38003 +       struct au_sbinfo *sbinfo;
38004 +       struct file *xib;
38005 +       unsigned long *p;
38006 +
38007 +       sbinfo = au_sbi(sb);
38008 +       MtxMustLock(&sbinfo->si_xib_mtx);
38009 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38010 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38011 +
38012 +       if (pindex == sbinfo->si_xib_last_pindex)
38013 +               return 0;
38014 +
38015 +       xib = sbinfo->si_xib;
38016 +       p = sbinfo->si_xib_buf;
38017 +       pos = sbinfo->si_xib_last_pindex;
38018 +       pos *= PAGE_SIZE;
38019 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38020 +       if (unlikely(sz != PAGE_SIZE))
38021 +               goto out;
38022 +
38023 +       pos = pindex;
38024 +       pos *= PAGE_SIZE;
38025 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38026 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
38027 +       else {
38028 +               memset(p, 0, PAGE_SIZE);
38029 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
38030 +       }
38031 +       if (sz == PAGE_SIZE) {
38032 +               sbinfo->si_xib_last_pindex = pindex;
38033 +               return 0; /* success */
38034 +       }
38035 +
38036 +out:
38037 +       AuIOErr1("write failed (%zd)\n", sz);
38038 +       err = sz;
38039 +       if (sz >= 0)
38040 +               err = -EIO;
38041 +       return err;
38042 +}
38043 +
38044 +static void au_xib_clear_bit(struct inode *inode)
38045 +{
38046 +       int err, bit;
38047 +       unsigned long pindex;
38048 +       struct super_block *sb;
38049 +       struct au_sbinfo *sbinfo;
38050 +
38051 +       AuDebugOn(inode->i_nlink);
38052 +
38053 +       sb = inode->i_sb;
38054 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38055 +       AuDebugOn(page_bits <= bit);
38056 +       sbinfo = au_sbi(sb);
38057 +       mutex_lock(&sbinfo->si_xib_mtx);
38058 +       err = xib_pindex(sb, pindex);
38059 +       if (!err) {
38060 +               clear_bit(bit, sbinfo->si_xib_buf);
38061 +               sbinfo->si_xib_next_bit = bit;
38062 +       }
38063 +       mutex_unlock(&sbinfo->si_xib_mtx);
38064 +}
38065 +
38066 +/* ---------------------------------------------------------------------- */
38067 +
38068 +/*
38069 + * truncate a xino bitmap file
38070 + */
38071 +
38072 +/* todo: slow */
38073 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38074 +{
38075 +       int err, bit;
38076 +       ssize_t sz;
38077 +       unsigned long pindex;
38078 +       loff_t pos, pend;
38079 +       struct au_sbinfo *sbinfo;
38080 +       vfs_readf_t func;
38081 +       ino_t *ino;
38082 +       unsigned long *p;
38083 +
38084 +       err = 0;
38085 +       sbinfo = au_sbi(sb);
38086 +       MtxMustLock(&sbinfo->si_xib_mtx);
38087 +       p = sbinfo->si_xib_buf;
38088 +       func = sbinfo->si_xread;
38089 +       pend = vfsub_f_size_read(file);
38090 +       pos = 0;
38091 +       while (pos < pend) {
38092 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
38093 +               err = sz;
38094 +               if (unlikely(sz <= 0))
38095 +                       goto out;
38096 +
38097 +               err = 0;
38098 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38099 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38100 +                               continue;
38101 +
38102 +                       xib_calc_bit(*ino, &pindex, &bit);
38103 +                       AuDebugOn(page_bits <= bit);
38104 +                       err = xib_pindex(sb, pindex);
38105 +                       if (!err)
38106 +                               set_bit(bit, p);
38107 +                       else
38108 +                               goto out;
38109 +               }
38110 +       }
38111 +
38112 +out:
38113 +       return err;
38114 +}
38115 +
38116 +static int xib_restore(struct super_block *sb)
38117 +{
38118 +       int err, i;
38119 +       unsigned int nfile;
38120 +       aufs_bindex_t bindex, bbot;
38121 +       void *page;
38122 +       struct au_branch *br;
38123 +       struct au_xino *xi;
38124 +       struct file *file;
38125 +
38126 +       err = -ENOMEM;
38127 +       page = (void *)__get_free_page(GFP_NOFS);
38128 +       if (unlikely(!page))
38129 +               goto out;
38130 +
38131 +       err = 0;
38132 +       bbot = au_sbbot(sb);
38133 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38134 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38135 +                       br = au_sbr(sb, bindex);
38136 +                       xi = br->br_xino;
38137 +                       nfile = xi->xi_nfile;
38138 +                       for (i = 0; i < nfile; i++) {
38139 +                               file = au_xino_file(xi, i);
38140 +                               if (file)
38141 +                                       err = do_xib_restore(sb, file, page);
38142 +                       }
38143 +               } else
38144 +                       AuDbg("skip shared b%d\n", bindex);
38145 +       free_page((unsigned long)page);
38146 +
38147 +out:
38148 +       return err;
38149 +}
38150 +
38151 +int au_xib_trunc(struct super_block *sb)
38152 +{
38153 +       int err;
38154 +       ssize_t sz;
38155 +       loff_t pos;
38156 +       struct au_sbinfo *sbinfo;
38157 +       unsigned long *p;
38158 +       struct file *file;
38159 +
38160 +       SiMustWriteLock(sb);
38161 +
38162 +       err = 0;
38163 +       sbinfo = au_sbi(sb);
38164 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38165 +               goto out;
38166 +
38167 +       file = sbinfo->si_xib;
38168 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38169 +               goto out;
38170 +
38171 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38172 +       err = PTR_ERR(file);
38173 +       if (IS_ERR(file))
38174 +               goto out;
38175 +       fput(sbinfo->si_xib);
38176 +       sbinfo->si_xib = file;
38177 +
38178 +       p = sbinfo->si_xib_buf;
38179 +       memset(p, 0, PAGE_SIZE);
38180 +       pos = 0;
38181 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38182 +       if (unlikely(sz != PAGE_SIZE)) {
38183 +               err = sz;
38184 +               AuIOErr("err %d\n", err);
38185 +               if (sz >= 0)
38186 +                       err = -EIO;
38187 +               goto out;
38188 +       }
38189 +
38190 +       mutex_lock(&sbinfo->si_xib_mtx);
38191 +       /* mnt_want_write() is unnecessary here */
38192 +       err = xib_restore(sb);
38193 +       mutex_unlock(&sbinfo->si_xib_mtx);
38194 +
38195 +out:
38196 +       return err;
38197 +}
38198 +
38199 +/* ---------------------------------------------------------------------- */
38200 +
38201 +struct au_xino *au_xino_alloc(unsigned int nfile)
38202 +{
38203 +       struct au_xino *xi;
38204 +
38205 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38206 +       if (unlikely(!xi))
38207 +               goto out;
38208 +       xi->xi_nfile = nfile;
38209 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38210 +       if (unlikely(!xi->xi_file))
38211 +               goto out_free;
38212 +
38213 +       xi->xi_nondir.total = 8; /* initial size */
38214 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38215 +                                     GFP_NOFS);
38216 +       if (unlikely(!xi->xi_nondir.array))
38217 +               goto out_file;
38218 +
38219 +       spin_lock_init(&xi->xi_nondir.spin);
38220 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38221 +       mutex_init(&xi->xi_mtx);
38222 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38223 +       atomic_set(&xi->xi_truncating, 0);
38224 +       kref_init(&xi->xi_kref);
38225 +       goto out; /* success */
38226 +
38227 +out_file:
38228 +       au_kfree_try_rcu(xi->xi_file);
38229 +out_free:
38230 +       au_kfree_rcu(xi);
38231 +       xi = NULL;
38232 +out:
38233 +       return xi;
38234 +}
38235 +
38236 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38237 +{
38238 +       int err;
38239 +       struct au_xino *xi;
38240 +
38241 +       err = 0;
38242 +       xi = au_xino_alloc(idx + 1);
38243 +       if (unlikely(!xi)) {
38244 +               err = -ENOMEM;
38245 +               goto out;
38246 +       }
38247 +
38248 +       if (file)
38249 +               get_file(file);
38250 +       xi->xi_file[idx] = file;
38251 +       AuDebugOn(br->br_xino);
38252 +       br->br_xino = xi;
38253 +
38254 +out:
38255 +       return err;
38256 +}
38257 +
38258 +static void au_xino_release(struct kref *kref)
38259 +{
38260 +       struct au_xino *xi;
38261 +       int i;
38262 +       unsigned long ul;
38263 +       struct hlist_bl_head *hbl;
38264 +       struct hlist_bl_node *pos, *n;
38265 +       struct au_xi_writing *p;
38266 +
38267 +       xi = container_of(kref, struct au_xino, xi_kref);
38268 +       for (i = 0; i < xi->xi_nfile; i++)
38269 +               if (xi->xi_file[i])
38270 +                       fput(xi->xi_file[i]);
38271 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38272 +               AuDebugOn(xi->xi_nondir.array[i]);
38273 +       mutex_destroy(&xi->xi_mtx);
38274 +       hbl = &xi->xi_writing;
38275 +       ul = au_hbl_count(hbl);
38276 +       if (unlikely(ul)) {
38277 +               pr_warn("xi_writing %lu\n", ul);
38278 +               hlist_bl_lock(hbl);
38279 +               hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
38280 +                       hlist_bl_del(&p->node);
38281 +                       au_kfree_rcu(p);
38282 +               }
38283 +               hlist_bl_unlock(hbl);
38284 +       }
38285 +       au_kfree_try_rcu(xi->xi_file);
38286 +       au_kfree_try_rcu(xi->xi_nondir.array);
38287 +       au_kfree_rcu(xi);
38288 +}
38289 +
38290 +int au_xino_put(struct au_branch *br)
38291 +{
38292 +       int ret;
38293 +       struct au_xino *xi;
38294 +
38295 +       ret = 0;
38296 +       xi = br->br_xino;
38297 +       if (xi) {
38298 +               br->br_xino = NULL;
38299 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38300 +       }
38301 +
38302 +       return ret;
38303 +}
38304 +
38305 +/* ---------------------------------------------------------------------- */
38306 +
38307 +/*
38308 + * xino mount option handlers
38309 + */
38310 +
38311 +/* xino bitmap */
38312 +static void xino_clear_xib(struct super_block *sb)
38313 +{
38314 +       struct au_sbinfo *sbinfo;
38315 +
38316 +       SiMustWriteLock(sb);
38317 +
38318 +       sbinfo = au_sbi(sb);
38319 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
38320 +       if (sbinfo->si_xib)
38321 +               fput(sbinfo->si_xib);
38322 +       sbinfo->si_xib = NULL;
38323 +       if (sbinfo->si_xib_buf)
38324 +               free_page((unsigned long)sbinfo->si_xib_buf);
38325 +       sbinfo->si_xib_buf = NULL;
38326 +}
38327 +
38328 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38329 +{
38330 +       int err;
38331 +       loff_t pos;
38332 +       struct au_sbinfo *sbinfo;
38333 +       struct file *file;
38334 +       struct super_block *xi_sb;
38335 +
38336 +       SiMustWriteLock(sb);
38337 +
38338 +       sbinfo = au_sbi(sb);
38339 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38340 +       err = PTR_ERR(file);
38341 +       if (IS_ERR(file))
38342 +               goto out;
38343 +       if (sbinfo->si_xib)
38344 +               fput(sbinfo->si_xib);
38345 +       sbinfo->si_xib = file;
38346 +       sbinfo->si_xread = vfs_readf(file);
38347 +       sbinfo->si_xwrite = vfs_writef(file);
38348 +       xi_sb = file_inode(file)->i_sb;
38349 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38350 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38351 +               err = -EIO;
38352 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38353 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38354 +               goto out_unset;
38355 +       }
38356 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38357 +
38358 +       err = -ENOMEM;
38359 +       if (!sbinfo->si_xib_buf)
38360 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38361 +       if (unlikely(!sbinfo->si_xib_buf))
38362 +               goto out_unset;
38363 +
38364 +       sbinfo->si_xib_last_pindex = 0;
38365 +       sbinfo->si_xib_next_bit = 0;
38366 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38367 +               pos = 0;
38368 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38369 +                                 PAGE_SIZE, &pos);
38370 +               if (unlikely(err != PAGE_SIZE))
38371 +                       goto out_free;
38372 +       }
38373 +       err = 0;
38374 +       goto out; /* success */
38375 +
38376 +out_free:
38377 +       if (sbinfo->si_xib_buf)
38378 +               free_page((unsigned long)sbinfo->si_xib_buf);
38379 +       sbinfo->si_xib_buf = NULL;
38380 +       if (err >= 0)
38381 +               err = -EIO;
38382 +out_unset:
38383 +       fput(sbinfo->si_xib);
38384 +       sbinfo->si_xib = NULL;
38385 +out:
38386 +       AuTraceErr(err);
38387 +       return err;
38388 +}
38389 +
38390 +/* xino for each branch */
38391 +static void xino_clear_br(struct super_block *sb)
38392 +{
38393 +       aufs_bindex_t bindex, bbot;
38394 +       struct au_branch *br;
38395 +
38396 +       bbot = au_sbbot(sb);
38397 +       for (bindex = 0; bindex <= bbot; bindex++) {
38398 +               br = au_sbr(sb, bindex);
38399 +               AuDebugOn(!br);
38400 +               au_xino_put(br);
38401 +       }
38402 +}
38403 +
38404 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38405 +                                 aufs_bindex_t bshared)
38406 +{
38407 +       struct au_branch *brshared;
38408 +
38409 +       brshared = au_sbr(sb, bshared);
38410 +       AuDebugOn(!brshared->br_xino);
38411 +       AuDebugOn(!brshared->br_xino->xi_file);
38412 +       if (br->br_xino != brshared->br_xino) {
38413 +               au_xino_get(brshared);
38414 +               au_xino_put(br);
38415 +               br->br_xino = brshared->br_xino;
38416 +       }
38417 +}
38418 +
38419 +struct au_xino_do_set_br {
38420 +       vfs_writef_t writef;
38421 +       struct au_branch *br;
38422 +       ino_t h_ino;
38423 +       aufs_bindex_t bshared;
38424 +};
38425 +
38426 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38427 +                            struct au_xino_do_set_br *args)
38428 +{
38429 +       int err;
38430 +       struct au_xi_calc calc;
38431 +       struct file *file;
38432 +       struct au_branch *br;
38433 +       struct au_xi_new xinew = {
38434 +               .base = path
38435 +       };
38436 +
38437 +       br = args->br;
38438 +       xinew.xi = br->br_xino;
38439 +       au_xi_calc(sb, args->h_ino, &calc);
38440 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38441 +       if (args->bshared >= 0)
38442 +               /* shared xino */
38443 +               au_xino_set_br_shared(sb, br, args->bshared);
38444 +       else if (!xinew.xi) {
38445 +               /* new xino */
38446 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38447 +               if (unlikely(err))
38448 +                       goto out;
38449 +       }
38450 +
38451 +       /* force re-creating */
38452 +       xinew.xi = br->br_xino;
38453 +       xinew.idx = calc.idx;
38454 +       mutex_lock(&xinew.xi->xi_mtx);
38455 +       file = au_xi_new(sb, &xinew);
38456 +       mutex_unlock(&xinew.xi->xi_mtx);
38457 +       err = PTR_ERR(file);
38458 +       if (IS_ERR(file))
38459 +               goto out;
38460 +       AuDebugOn(!file);
38461 +
38462 +       err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38463 +       if (unlikely(err))
38464 +               au_xino_put(br);
38465 +
38466 +out:
38467 +       AuTraceErr(err);
38468 +       return err;
38469 +}
38470 +
38471 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38472 +{
38473 +       int err;
38474 +       aufs_bindex_t bindex, bbot;
38475 +       struct au_xino_do_set_br args;
38476 +       struct inode *inode;
38477 +
38478 +       SiMustWriteLock(sb);
38479 +
38480 +       bbot = au_sbbot(sb);
38481 +       inode = d_inode(sb->s_root);
38482 +       args.writef = au_sbi(sb)->si_xwrite;
38483 +       for (bindex = 0; bindex <= bbot; bindex++) {
38484 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38485 +               args.br = au_sbr(sb, bindex);
38486 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38487 +               err = au_xino_do_set_br(sb, path, &args);
38488 +               if (unlikely(err))
38489 +                       break;
38490 +       }
38491 +
38492 +       AuTraceErr(err);
38493 +       return err;
38494 +}
38495 +
38496 +void au_xino_clr(struct super_block *sb)
38497 +{
38498 +       struct au_sbinfo *sbinfo;
38499 +
38500 +       au_xigen_clr(sb);
38501 +       xino_clear_xib(sb);
38502 +       xino_clear_br(sb);
38503 +       dbgaufs_brs_del(sb, 0);
38504 +       sbinfo = au_sbi(sb);
38505 +       /* lvalue, do not call au_mntflags() */
38506 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38507 +}
38508 +
38509 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38510 +{
38511 +       int err, skip;
38512 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38513 +       struct qstr *dname, *cur_name;
38514 +       struct file *cur_xino;
38515 +       struct au_sbinfo *sbinfo;
38516 +       struct path *path, *cur_path;
38517 +
38518 +       SiMustWriteLock(sb);
38519 +
38520 +       err = 0;
38521 +       sbinfo = au_sbi(sb);
38522 +       path = &xiopt->file->f_path;
38523 +       dentry = path->dentry;
38524 +       parent = dget_parent(dentry);
38525 +       if (remount) {
38526 +               skip = 0;
38527 +               cur_xino = sbinfo->si_xib;
38528 +               if (cur_xino) {
38529 +                       cur_path = &cur_xino->f_path;
38530 +                       cur_dentry = cur_path->dentry;
38531 +                       cur_parent = dget_parent(cur_dentry);
38532 +                       cur_name = &cur_dentry->d_name;
38533 +                       dname = &dentry->d_name;
38534 +                       skip = (cur_parent == parent
38535 +                               && au_qstreq(dname, cur_name));
38536 +                       dput(cur_parent);
38537 +               }
38538 +               if (skip)
38539 +                       goto out;
38540 +       }
38541 +
38542 +       au_opt_set(sbinfo->si_mntflags, XINO);
38543 +       err = au_xino_set_xib(sb, path);
38544 +       /* si_x{read,write} are set */
38545 +       if (!err)
38546 +               err = au_xigen_set(sb, path);
38547 +       if (!err)
38548 +               err = au_xino_set_br(sb, path);
38549 +       if (!err) {
38550 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38551 +               goto out; /* success */
38552 +       }
38553 +
38554 +       /* reset all */
38555 +       AuIOErr("failed setting xino(%d).\n", err);
38556 +       au_xino_clr(sb);
38557 +
38558 +out:
38559 +       dput(parent);
38560 +       return err;
38561 +}
38562 +
38563 +/*
38564 + * create a xinofile at the default place/path.
38565 + */
38566 +struct file *au_xino_def(struct super_block *sb)
38567 +{
38568 +       struct file *file;
38569 +       char *page, *p;
38570 +       struct au_branch *br;
38571 +       struct super_block *h_sb;
38572 +       struct path path;
38573 +       aufs_bindex_t bbot, bindex, bwr;
38574 +
38575 +       br = NULL;
38576 +       bbot = au_sbbot(sb);
38577 +       bwr = -1;
38578 +       for (bindex = 0; bindex <= bbot; bindex++) {
38579 +               br = au_sbr(sb, bindex);
38580 +               if (au_br_writable(br->br_perm)
38581 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38582 +                       bwr = bindex;
38583 +                       break;
38584 +               }
38585 +       }
38586 +
38587 +       if (bwr >= 0) {
38588 +               file = ERR_PTR(-ENOMEM);
38589 +               page = (void *)__get_free_page(GFP_NOFS);
38590 +               if (unlikely(!page))
38591 +                       goto out;
38592 +               path.mnt = au_br_mnt(br);
38593 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38594 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38595 +               file = (void *)p;
38596 +               if (!IS_ERR(p)) {
38597 +                       strcat(p, "/" AUFS_XINO_FNAME);
38598 +                       AuDbg("%s\n", p);
38599 +                       file = au_xino_create(sb, p, /*silent*/0);
38600 +               }
38601 +               free_page((unsigned long)page);
38602 +       } else {
38603 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
38604 +               if (IS_ERR(file))
38605 +                       goto out;
38606 +               h_sb = file->f_path.dentry->d_sb;
38607 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38608 +                       pr_err("xino doesn't support %s(%s)\n",
38609 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38610 +                       fput(file);
38611 +                       file = ERR_PTR(-EINVAL);
38612 +               }
38613 +       }
38614 +
38615 +out:
38616 +       return file;
38617 +}
38618 +
38619 +/* ---------------------------------------------------------------------- */
38620 +
38621 +/*
38622 + * initialize the xinofile for the specified branch @br
38623 + * at the place/path where @base_file indicates.
38624 + * test whether another branch is on the same filesystem or not,
38625 + * if found then share the xinofile with another branch.
38626 + */
38627 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38628 +                   struct path *base)
38629 +{
38630 +       int err;
38631 +       struct au_xino_do_set_br args = {
38632 +               .h_ino  = h_ino,
38633 +               .br     = br
38634 +       };
38635 +
38636 +       args.writef = au_sbi(sb)->si_xwrite;
38637 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38638 +                                      au_br_sb(br));
38639 +       err = au_xino_do_set_br(sb, base, &args);
38640 +       if (unlikely(err))
38641 +               au_xino_put(br);
38642 +
38643 +       return err;
38644 +}
38645 +
38646 +/* ---------------------------------------------------------------------- */
38647 +
38648 +/*
38649 + * get an unused inode number from bitmap
38650 + */
38651 +ino_t au_xino_new_ino(struct super_block *sb)
38652 +{
38653 +       ino_t ino;
38654 +       unsigned long *p, pindex, ul, pend;
38655 +       struct au_sbinfo *sbinfo;
38656 +       struct file *file;
38657 +       int free_bit, err;
38658 +
38659 +       if (!au_opt_test(au_mntflags(sb), XINO))
38660 +               return iunique(sb, AUFS_FIRST_INO);
38661 +
38662 +       sbinfo = au_sbi(sb);
38663 +       mutex_lock(&sbinfo->si_xib_mtx);
38664 +       p = sbinfo->si_xib_buf;
38665 +       free_bit = sbinfo->si_xib_next_bit;
38666 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38667 +               goto out; /* success */
38668 +       free_bit = find_first_zero_bit(p, page_bits);
38669 +       if (free_bit < page_bits)
38670 +               goto out; /* success */
38671 +
38672 +       pindex = sbinfo->si_xib_last_pindex;
38673 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38674 +               err = xib_pindex(sb, ul);
38675 +               if (unlikely(err))
38676 +                       goto out_err;
38677 +               free_bit = find_first_zero_bit(p, page_bits);
38678 +               if (free_bit < page_bits)
38679 +                       goto out; /* success */
38680 +       }
38681 +
38682 +       file = sbinfo->si_xib;
38683 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38684 +       for (ul = pindex + 1; ul <= pend; ul++) {
38685 +               err = xib_pindex(sb, ul);
38686 +               if (unlikely(err))
38687 +                       goto out_err;
38688 +               free_bit = find_first_zero_bit(p, page_bits);
38689 +               if (free_bit < page_bits)
38690 +                       goto out; /* success */
38691 +       }
38692 +       BUG();
38693 +
38694 +out:
38695 +       set_bit(free_bit, p);
38696 +       sbinfo->si_xib_next_bit = free_bit + 1;
38697 +       pindex = sbinfo->si_xib_last_pindex;
38698 +       mutex_unlock(&sbinfo->si_xib_mtx);
38699 +       ino = xib_calc_ino(pindex, free_bit);
38700 +       AuDbg("i%lu\n", (unsigned long)ino);
38701 +       return ino;
38702 +out_err:
38703 +       mutex_unlock(&sbinfo->si_xib_mtx);
38704 +       AuDbg("i0\n");
38705 +       return 0;
38706 +}
38707 +
38708 +/* for s_op->delete_inode() */
38709 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38710 +{
38711 +       int err;
38712 +       unsigned int mnt_flags;
38713 +       aufs_bindex_t bindex, bbot, bi;
38714 +       unsigned char try_trunc;
38715 +       struct au_iinfo *iinfo;
38716 +       struct super_block *sb;
38717 +       struct au_hinode *hi;
38718 +       struct inode *h_inode;
38719 +       struct au_branch *br;
38720 +       vfs_writef_t xwrite;
38721 +       struct au_xi_calc calc;
38722 +       struct file *file;
38723 +
38724 +       AuDebugOn(au_is_bad_inode(inode));
38725 +
38726 +       sb = inode->i_sb;
38727 +       mnt_flags = au_mntflags(sb);
38728 +       if (!au_opt_test(mnt_flags, XINO)
38729 +           || inode->i_ino == AUFS_ROOT_INO)
38730 +               return;
38731 +
38732 +       if (unlinked) {
38733 +               au_xigen_inc(inode);
38734 +               au_xib_clear_bit(inode);
38735 +       }
38736 +
38737 +       iinfo = au_ii(inode);
38738 +       bindex = iinfo->ii_btop;
38739 +       if (bindex < 0)
38740 +               return;
38741 +
38742 +       xwrite = au_sbi(sb)->si_xwrite;
38743 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38744 +       hi = au_hinode(iinfo, bindex);
38745 +       bbot = iinfo->ii_bbot;
38746 +       for (; bindex <= bbot; bindex++, hi++) {
38747 +               h_inode = hi->hi_inode;
38748 +               if (!h_inode
38749 +                   || (!unlinked && h_inode->i_nlink))
38750 +                       continue;
38751 +
38752 +               /* inode may not be revalidated */
38753 +               bi = au_br_index(sb, hi->hi_id);
38754 +               if (bi < 0)
38755 +                       continue;
38756 +
38757 +               br = au_sbr(sb, bi);
38758 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38759 +               file = au_xino_file(br->br_xino, calc.idx);
38760 +               if (IS_ERR_OR_NULL(file))
38761 +                       continue;
38762 +
38763 +               err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
38764 +               if (!err && try_trunc
38765 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38766 +                       xino_try_trunc(sb, br);
38767 +       }
38768 +}
38769 +
38770 +/* ---------------------------------------------------------------------- */
38771 +
38772 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38773 +{
38774 +       int found, total, i;
38775 +
38776 +       found = -1;
38777 +       total = xi->xi_nondir.total;
38778 +       for (i = 0; i < total; i++) {
38779 +               if (xi->xi_nondir.array[i] != h_ino)
38780 +                       continue;
38781 +               found = i;
38782 +               break;
38783 +       }
38784 +
38785 +       return found;
38786 +}
38787 +
38788 +static int au_xinondir_expand(struct au_xino *xi)
38789 +{
38790 +       int err, sz;
38791 +       ino_t *p;
38792 +
38793 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38794 +
38795 +       err = -ENOMEM;
38796 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38797 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38798 +               goto out;
38799 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38800 +                        /*may_shrink*/0);
38801 +       if (p) {
38802 +               xi->xi_nondir.array = p;
38803 +               xi->xi_nondir.total <<= 1;
38804 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38805 +               err = 0;
38806 +       }
38807 +
38808 +out:
38809 +       return err;
38810 +}
38811 +
38812 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38813 +                      ino_t h_ino, int idx)
38814 +{
38815 +       struct au_xino *xi;
38816 +
38817 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38818 +       xi = au_sbr(sb, bindex)->br_xino;
38819 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38820 +
38821 +       spin_lock(&xi->xi_nondir.spin);
38822 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38823 +       xi->xi_nondir.array[idx] = 0;
38824 +       spin_unlock(&xi->xi_nondir.spin);
38825 +       wake_up_all(&xi->xi_nondir.wqh);
38826 +}
38827 +
38828 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38829 +                     int *idx)
38830 +{
38831 +       int err, found, empty;
38832 +       struct au_xino *xi;
38833 +
38834 +       err = 0;
38835 +       *idx = -1;
38836 +       if (!au_opt_test(au_mntflags(sb), XINO))
38837 +               goto out; /* no xino */
38838 +
38839 +       xi = au_sbr(sb, bindex)->br_xino;
38840 +
38841 +again:
38842 +       spin_lock(&xi->xi_nondir.spin);
38843 +       found = au_xinondir_find(xi, h_ino);
38844 +       if (found == -1) {
38845 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38846 +               if (empty == -1) {
38847 +                       empty = xi->xi_nondir.total;
38848 +                       err = au_xinondir_expand(xi);
38849 +                       if (unlikely(err))
38850 +                               goto out_unlock;
38851 +               }
38852 +               xi->xi_nondir.array[empty] = h_ino;
38853 +               *idx = empty;
38854 +       } else {
38855 +               spin_unlock(&xi->xi_nondir.spin);
38856 +               wait_event(xi->xi_nondir.wqh,
38857 +                          xi->xi_nondir.array[found] != h_ino);
38858 +               goto again;
38859 +       }
38860 +
38861 +out_unlock:
38862 +       spin_unlock(&xi->xi_nondir.spin);
38863 +out:
38864 +       return err;
38865 +}
38866 +
38867 +/* ---------------------------------------------------------------------- */
38868 +
38869 +int au_xino_path(struct seq_file *seq, struct file *file)
38870 +{
38871 +       int err;
38872 +
38873 +       err = au_seq_path(seq, &file->f_path);
38874 +       if (unlikely(err))
38875 +               goto out;
38876 +
38877 +#define Deleted "\\040(deleted)"
38878 +       seq->count -= sizeof(Deleted) - 1;
38879 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38880 +                        sizeof(Deleted) - 1));
38881 +#undef Deleted
38882 +
38883 +out:
38884 +       return err;
38885 +}
38886 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38887 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38888 +++ linux/include/uapi/linux/aufs_type.h        2018-12-27 13:19:17.715082917 +0100
38889 @@ -0,0 +1,448 @@
38890 +/* SPDX-License-Identifier: GPL-2.0 */
38891 +/*
38892 + * Copyright (C) 2005-2018 Junjiro R. Okajima
38893 + *
38894 + * This program, aufs is free software; you can redistribute it and/or modify
38895 + * it under the terms of the GNU General Public License as published by
38896 + * the Free Software Foundation; either version 2 of the License, or
38897 + * (at your option) any later version.
38898 + *
38899 + * This program is distributed in the hope that it will be useful,
38900 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38901 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38902 + * GNU General Public License for more details.
38903 + *
38904 + * You should have received a copy of the GNU General Public License
38905 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38906 + */
38907 +
38908 +#ifndef __AUFS_TYPE_H__
38909 +#define __AUFS_TYPE_H__
38910 +
38911 +#define AUFS_NAME      "aufs"
38912 +
38913 +#ifdef __KERNEL__
38914 +/*
38915 + * define it before including all other headers.
38916 + * sched.h may use pr_* macros before defining "current", so define the
38917 + * no-current version first, and re-define later.
38918 + */
38919 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38920 +#include <linux/sched.h>
38921 +#undef pr_fmt
38922 +#define pr_fmt(fmt) \
38923 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38924 +               (int)sizeof(current->comm), current->comm, current->pid
38925 +#else
38926 +#include <stdint.h>
38927 +#include <sys/types.h>
38928 +#endif /* __KERNEL__ */
38929 +
38930 +#include <linux/limits.h>
38931 +
38932 +#define AUFS_VERSION   "4.x-rcN-20181217"
38933 +
38934 +/* todo? move this to linux-2.6.19/include/magic.h */
38935 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38936 +
38937 +/* ---------------------------------------------------------------------- */
38938 +
38939 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38940 +typedef int8_t aufs_bindex_t;
38941 +#define AUFS_BRANCH_MAX 127
38942 +#else
38943 +typedef int16_t aufs_bindex_t;
38944 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38945 +#define AUFS_BRANCH_MAX 511
38946 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38947 +#define AUFS_BRANCH_MAX 1023
38948 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38949 +#define AUFS_BRANCH_MAX 32767
38950 +#endif
38951 +#endif
38952 +
38953 +#ifdef __KERNEL__
38954 +#ifndef AUFS_BRANCH_MAX
38955 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38956 +#endif
38957 +#endif /* __KERNEL__ */
38958 +
38959 +/* ---------------------------------------------------------------------- */
38960 +
38961 +#define AUFS_FSTYPE            AUFS_NAME
38962 +
38963 +#define AUFS_ROOT_INO          2
38964 +#define AUFS_FIRST_INO         11
38965 +
38966 +#define AUFS_WH_PFX            ".wh."
38967 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38968 +#define AUFS_WH_TMP_LEN                4
38969 +/* a limit for rmdir/rename a dir and copyup */
38970 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38971 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38972 +                               - 1                     /* dot */\
38973 +                               - AUFS_WH_TMP_LEN)      /* hex */
38974 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38975 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38976 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38977 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38978 +#define AUFS_DIRWH_DEF         3
38979 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38980 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38981 +#define AUFS_RDBLK_DEF         512 /* bytes */
38982 +#define AUFS_RDHASH_DEF                32
38983 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38984 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38985 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38986 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38987 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38988 +
38989 +/* pseudo-link maintenace under /proc */
38990 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38991 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38992 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38993 +
38994 +/* dirren, renamed dir */
38995 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38996 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38997 +/* whiteouted doubly */
38998 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38999 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39000 +
39001 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39002 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39003 +
39004 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39005 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39006 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39007 +
39008 +/* doubly whiteouted */
39009 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39010 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39011 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39012 +
39013 +/* branch permissions and attributes */
39014 +#define AUFS_BRPERM_RW         "rw"
39015 +#define AUFS_BRPERM_RO         "ro"
39016 +#define AUFS_BRPERM_RR         "rr"
39017 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39018 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39019 +#define AUFS_BRATTR_FHSM       "fhsm"
39020 +#define AUFS_BRATTR_UNPIN      "unpin"
39021 +#define AUFS_BRATTR_ICEX       "icex"
39022 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39023 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39024 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39025 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39026 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39027 +#define AUFS_BRRATTR_WH                "wh"
39028 +#define AUFS_BRWATTR_NLWH      "nolwh"
39029 +#define AUFS_BRWATTR_MOO       "moo"
39030 +
39031 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39032 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39033 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39034 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39035 +
39036 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39037 +#define AuBrAttr_COO_ALL       (1 << 4)
39038 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39039 +
39040 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39041 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39042 +                                                  branch. meaningless since
39043 +                                                  linux-3.18-rc1 */
39044 +
39045 +/* ignore error in copying XATTR */
39046 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39047 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39048 +#define AuBrAttr_ICEX_TR       (1 << 9)
39049 +#define AuBrAttr_ICEX_USR      (1 << 10)
39050 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39051 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39052 +                                | AuBrAttr_ICEX_SYS    \
39053 +                                | AuBrAttr_ICEX_TR     \
39054 +                                | AuBrAttr_ICEX_USR    \
39055 +                                | AuBrAttr_ICEX_OTH)
39056 +
39057 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39058 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39059 +
39060 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39061 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39062 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39063 +
39064 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39065 +
39066 +/* #warning test userspace */
39067 +#ifdef __KERNEL__
39068 +#ifndef CONFIG_AUFS_FHSM
39069 +#undef AuBrAttr_FHSM
39070 +#define AuBrAttr_FHSM          0
39071 +#endif
39072 +#ifndef CONFIG_AUFS_XATTR
39073 +#undef AuBrAttr_ICEX
39074 +#define AuBrAttr_ICEX          0
39075 +#undef AuBrAttr_ICEX_SEC
39076 +#define AuBrAttr_ICEX_SEC      0
39077 +#undef AuBrAttr_ICEX_SYS
39078 +#define AuBrAttr_ICEX_SYS      0
39079 +#undef AuBrAttr_ICEX_TR
39080 +#define AuBrAttr_ICEX_TR       0
39081 +#undef AuBrAttr_ICEX_USR
39082 +#define AuBrAttr_ICEX_USR      0
39083 +#undef AuBrAttr_ICEX_OTH
39084 +#define AuBrAttr_ICEX_OTH      0
39085 +#endif
39086 +#endif
39087 +
39088 +/* the longest combination */
39089 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39090 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39091 +                              "+" AUFS_BRATTR_COO_REG          \
39092 +                              "+" AUFS_BRATTR_FHSM             \
39093 +                              "+" AUFS_BRATTR_UNPIN            \
39094 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39095 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39096 +                              "+" AUFS_BRATTR_ICEX_USR         \
39097 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39098 +                              "+" AUFS_BRWATTR_NLWH)
39099 +
39100 +typedef struct {
39101 +       char a[AuBrPermStrSz];
39102 +} au_br_perm_str_t;
39103 +
39104 +static inline int au_br_writable(int brperm)
39105 +{
39106 +       return brperm & AuBrPerm_RW;
39107 +}
39108 +
39109 +static inline int au_br_whable(int brperm)
39110 +{
39111 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39112 +}
39113 +
39114 +static inline int au_br_wh_linkable(int brperm)
39115 +{
39116 +       return !(brperm & AuBrWAttr_NoLinkWH);
39117 +}
39118 +
39119 +static inline int au_br_cmoo(int brperm)
39120 +{
39121 +       return brperm & AuBrAttr_CMOO_Mask;
39122 +}
39123 +
39124 +static inline int au_br_fhsm(int brperm)
39125 +{
39126 +       return brperm & AuBrAttr_FHSM;
39127 +}
39128 +
39129 +/* ---------------------------------------------------------------------- */
39130 +
39131 +/* ioctl */
39132 +enum {
39133 +       /* readdir in userspace */
39134 +       AuCtl_RDU,
39135 +       AuCtl_RDU_INO,
39136 +
39137 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39138 +       AuCtl_IBUSY,    /* busy inode */
39139 +       AuCtl_MVDOWN,   /* move-down */
39140 +       AuCtl_BR,       /* info about branches */
39141 +       AuCtl_FHSM_FD   /* connection for fhsm */
39142 +};
39143 +
39144 +/* borrowed from linux/include/linux/kernel.h */
39145 +#ifndef ALIGN
39146 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39147 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39148 +#endif
39149 +
39150 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39151 +#ifndef __aligned
39152 +#define __aligned(x)                   __attribute__((aligned(x)))
39153 +#endif
39154 +
39155 +#ifdef __KERNEL__
39156 +#ifndef __packed
39157 +#define __packed                       __attribute__((packed))
39158 +#endif
39159 +#endif
39160 +
39161 +struct au_rdu_cookie {
39162 +       uint64_t        h_pos;
39163 +       int16_t         bindex;
39164 +       uint8_t         flags;
39165 +       uint8_t         pad;
39166 +       uint32_t        generation;
39167 +} __aligned(8);
39168 +
39169 +struct au_rdu_ent {
39170 +       uint64_t        ino;
39171 +       int16_t         bindex;
39172 +       uint8_t         type;
39173 +       uint8_t         nlen;
39174 +       uint8_t         wh;
39175 +       char            name[0];
39176 +} __aligned(8);
39177 +
39178 +static inline int au_rdu_len(int nlen)
39179 +{
39180 +       /* include the terminating NULL */
39181 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39182 +                    sizeof(uint64_t));
39183 +}
39184 +
39185 +union au_rdu_ent_ul {
39186 +       struct au_rdu_ent __user        *e;
39187 +       uint64_t                        ul;
39188 +};
39189 +
39190 +enum {
39191 +       AufsCtlRduV_SZ,
39192 +       AufsCtlRduV_End
39193 +};
39194 +
39195 +struct aufs_rdu {
39196 +       /* input */
39197 +       union {
39198 +               uint64_t        sz;     /* AuCtl_RDU */
39199 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39200 +       };
39201 +       union au_rdu_ent_ul     ent;
39202 +       uint16_t                verify[AufsCtlRduV_End];
39203 +
39204 +       /* input/output */
39205 +       uint32_t                blk;
39206 +
39207 +       /* output */
39208 +       union au_rdu_ent_ul     tail;
39209 +       /* number of entries which were added in a single call */
39210 +       uint64_t                rent;
39211 +       uint8_t                 full;
39212 +       uint8_t                 shwh;
39213 +
39214 +       struct au_rdu_cookie    cookie;
39215 +} __aligned(8);
39216 +
39217 +/* ---------------------------------------------------------------------- */
39218 +
39219 +/* dirren. the branch is identified by the filename who contains this */
39220 +struct au_drinfo {
39221 +       uint64_t ino;
39222 +       union {
39223 +               uint8_t oldnamelen;
39224 +               uint64_t _padding;
39225 +       };
39226 +       uint8_t oldname[0];
39227 +} __aligned(8);
39228 +
39229 +struct au_drinfo_fdata {
39230 +       uint32_t magic;
39231 +       struct au_drinfo drinfo;
39232 +} __aligned(8);
39233 +
39234 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39235 +/* future */
39236 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39237 +
39238 +/* ---------------------------------------------------------------------- */
39239 +
39240 +struct aufs_wbr_fd {
39241 +       uint32_t        oflags;
39242 +       int16_t         brid;
39243 +} __aligned(8);
39244 +
39245 +/* ---------------------------------------------------------------------- */
39246 +
39247 +struct aufs_ibusy {
39248 +       uint64_t        ino, h_ino;
39249 +       int16_t         bindex;
39250 +} __aligned(8);
39251 +
39252 +/* ---------------------------------------------------------------------- */
39253 +
39254 +/* error code for move-down */
39255 +/* the actual message strings are implemented in aufs-util.git */
39256 +enum {
39257 +       EAU_MVDOWN_OPAQUE = 1,
39258 +       EAU_MVDOWN_WHITEOUT,
39259 +       EAU_MVDOWN_UPPER,
39260 +       EAU_MVDOWN_BOTTOM,
39261 +       EAU_MVDOWN_NOUPPER,
39262 +       EAU_MVDOWN_NOLOWERBR,
39263 +       EAU_Last
39264 +};
39265 +
39266 +/* flags for move-down */
39267 +#define AUFS_MVDOWN_DMSG       1
39268 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39269 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39270 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39271 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39272 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39273 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39274 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39275 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39276 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39277 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39278 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39279 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39280 +
39281 +/* index for move-down */
39282 +enum {
39283 +       AUFS_MVDOWN_UPPER,
39284 +       AUFS_MVDOWN_LOWER,
39285 +       AUFS_MVDOWN_NARRAY
39286 +};
39287 +
39288 +/*
39289 + * additional info of move-down
39290 + * number of free blocks and inodes.
39291 + * subset of struct kstatfs, but smaller and always 64bit.
39292 + */
39293 +struct aufs_stfs {
39294 +       uint64_t        f_blocks;
39295 +       uint64_t        f_bavail;
39296 +       uint64_t        f_files;
39297 +       uint64_t        f_ffree;
39298 +};
39299 +
39300 +struct aufs_stbr {
39301 +       int16_t                 brid;   /* optional input */
39302 +       int16_t                 bindex; /* output */
39303 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39304 +} __aligned(8);
39305 +
39306 +struct aufs_mvdown {
39307 +       uint32_t                flags;                  /* input/output */
39308 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39309 +       int8_t                  au_errno;               /* output */
39310 +} __aligned(8);
39311 +
39312 +/* ---------------------------------------------------------------------- */
39313 +
39314 +union aufs_brinfo {
39315 +       /* PATH_MAX may differ between kernel-space and user-space */
39316 +       char    _spacer[4096];
39317 +       struct {
39318 +               int16_t id;
39319 +               int     perm;
39320 +               char    path[0];
39321 +       };
39322 +} __aligned(8);
39323 +
39324 +/* ---------------------------------------------------------------------- */
39325 +
39326 +#define AuCtlType              'A'
39327 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39328 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39329 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39330 +                                    struct aufs_wbr_fd)
39331 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39332 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39333 +                                     struct aufs_mvdown)
39334 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39335 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39336 +
39337 +#endif /* __AUFS_TYPE_H__ */
39338 SPDX-License-Identifier: GPL-2.0
39339 aufs4.x-rcN loopback patch
39340
39341 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39342 index 470dd02..20dc3ec 100644
39343 --- a/drivers/block/loop.c
39344 +++ b/drivers/block/loop.c
39345 @@ -625,6 +625,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39346                         lo->use_dio);
39347  }
39348  
39349 +static struct file *loop_real_file(struct file *file)
39350 +{
39351 +       struct file *f = NULL;
39352 +
39353 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39354 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39355 +       return f;
39356 +}
39357 +
39358  static void loop_reread_partitions(struct loop_device *lo,
39359                                    struct block_device *bdev)
39360  {
39361 @@ -689,6 +698,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39362                           unsigned int arg)
39363  {
39364         struct file     *file = NULL, *old_file;
39365 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39366         int             error;
39367  
39368         error = -ENXIO;
39369 @@ -704,12 +714,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39370         file = fget(arg);
39371         if (!file)
39372                 goto out_err;
39373 +       f = loop_real_file(file);
39374 +       if (f) {
39375 +               virt_file = file;
39376 +               file = f;
39377 +               get_file(file);
39378 +       }
39379  
39380         error = loop_validate_file(file, bdev);
39381         if (error)
39382                 goto out_err;
39383  
39384         old_file = lo->lo_backing_file;
39385 +       old_virt_file = lo->lo_backing_virt_file;
39386  
39387         error = -EINVAL;
39388  
39389 @@ -721,6 +738,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39390         blk_mq_freeze_queue(lo->lo_queue);
39391         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39392         lo->lo_backing_file = file;
39393 +       lo->lo_backing_virt_file = virt_file;
39394         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39395         mapping_set_gfp_mask(file->f_mapping,
39396                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39397 @@ -728,14 +746,18 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39398          * dependency.
39399          */
39400         fput(old_file);
39401 +       if (old_virt_file)
39402 +               fput(old_virt_file);
39403         if (partscan)
39404                 loop_reread_partitions(lo, bdev);
39405         return 0;
39406  
39407  out_err:
39408         mutex_unlock(&loop_ctl_mutex);
39409         if (file)
39410                 fput(file);
39411 +       if (virt_file)
39412 +               fput(virt_file);
39413         return error;
39414  }
39415  
39416 @@ -921,7 +943,7 @@ static int loop_prepare_queue(struct loop_device *lo)
39417  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39418                        struct block_device *bdev, unsigned int arg)
39419  {
39420 -       struct file     *file;
39421 +       struct file     *file, *f, *virt_file = NULL;
39422         struct inode    *inode;
39423         struct address_space *mapping;
39424         int             lo_flags = 0;
39425 @@ -935,6 +957,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39426         file = fget(arg);
39427         if (!file)
39428                 goto out;
39429 +       f = loop_real_file(file);
39430 +       if (f) {
39431 +               virt_file = file;
39432 +               file = f;
39433 +               get_file(file);
39434 +       }
39435  
39436         error = -EBUSY;
39437         if (lo->lo_state != Lo_unbound)
39438 @@ -967,6 +995,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39439         lo->lo_device = bdev;
39440         lo->lo_flags = lo_flags;
39441         lo->lo_backing_file = file;
39442 +       lo->lo_backing_virt_file = virt_file;
39443         lo->transfer = NULL;
39444         lo->ioctl = NULL;
39445         lo->lo_sizelimit = 0;
39446 @@ -1000,6 +1029,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39447  
39448  out_putf:
39449         fput(file);
39450 +       if (virt_file)
39451 +               fput(virt_file);
39452  out:
39453         /* This is safe: open() is still holding a reference. */
39454         module_put(THIS_MODULE);
39455 @@ -1046,6 +1077,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39456  static int loop_clr_fd(struct loop_device *lo)
39457  {
39458         struct file *filp = NULL;
39459 +       struct file *virt_filp = lo->lo_backing_virt_file;
39460         gfp_t gfp = lo->old_gfp_mask;
39461         struct block_device *bdev = lo->lo_device;
39462  
39463 @@ -1077,6 +1109,7 @@ static int loop_clr_fd(struct loop_device *lo)
39464         spin_lock_irq(&lo->lo_lock);
39465         lo->lo_state = Lo_rundown;
39466         lo->lo_backing_file = NULL;
39467 +       lo->lo_backing_virt_file = NULL;
39468         spin_unlock_irq(&lo->lo_lock);
39469  
39470         loop_release_xfer(lo);
39471 @@ -1125,6 +1158,8 @@ static int loop_clr_fd(struct loop_device *lo)
39472          */
39473         if (filp)
39474                 fput(filp);
39475 +       if (virt_filp)
39476 +               fput(virt_filp);
39477         return err;
39478  }
39479  
39480 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39481 index 4d42c7a..a4974ee 100644
39482 --- a/drivers/block/loop.h
39483 +++ b/drivers/block/loop.h
39484 @@ -46,7 +46,7 @@ struct loop_device {
39485         int             (*ioctl)(struct loop_device *, int cmd, 
39486                                  unsigned long arg); 
39487  
39488 -       struct file *   lo_backing_file;
39489 +       struct file *   lo_backing_file, *lo_backing_virt_file;
39490         struct block_device *lo_device;
39491         void            *key_data; 
39492  
39493 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39494 index 5309874..1a334cf 100644
39495 --- a/fs/aufs/f_op.c
39496 +++ b/fs/aufs/f_op.c
39497 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39498         if (IS_ERR(h_file))
39499                 goto out;
39500  
39501 -       if (au_test_loopback_kthread()) {
39502 +       if (0 && au_test_loopback_kthread()) {
39503                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39504                 if (file->f_mapping != h_file->f_mapping) {
39505                         file->f_mapping = h_file->f_mapping;
39506 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39507 index 3f3577d..3b44158 100644
39508 --- a/fs/aufs/loop.c
39509 +++ b/fs/aufs/loop.c
39510 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39511                 symbol_put(loop_backing_file);
39512         au_kfree_try_rcu(au_warn_loopback_array);
39513  }
39514 +
39515 +/* ---------------------------------------------------------------------- */
39516 +
39517 +/* support the loopback block device insude aufs */
39518 +
39519 +struct file *aufs_real_loop(struct file *file)
39520 +{
39521 +       struct file *f;
39522 +
39523 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39524 +       fi_read_lock(file);
39525 +       f = au_hf_top(file);
39526 +       fi_read_unlock(file);
39527 +       AuDebugOn(!f);
39528 +       return f;
39529 +}
39530 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39531 index 05d703d..6bb23c8 100644
39532 --- a/fs/aufs/loop.h
39533 +++ b/fs/aufs/loop.h
39534 @@ -26,7 +26,11 @@ void au_warn_loopback(struct super_block *h_sb);
39535  
39536  int au_loopback_init(void);
39537  void au_loopback_fin(void);
39538 +
39539 +struct file *aufs_real_loop(struct file *file);
39540  #else
39541 +AuStub(struct file *, loop_backing_file, return NULL)
39542 +
39543  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
39544            struct dentry *h_adding)
39545  AuStubInt0(au_test_loopback_kthread, void)
39546 @@ -34,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39547  
39548  AuStubInt0(au_loopback_init, void)
39549  AuStubVoid(au_loopback_fin, void)
39550 +
39551 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39552  #endif /* BLK_DEV_LOOP */
39553  
39554  #endif /* __KERNEL__ */
39555 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39556 index 777503e..7130061 100644
39557 --- a/fs/aufs/super.c
39558 +++ b/fs/aufs/super.c
39559 @@ -845,7 +845,10 @@ static const struct super_operations aufs_sop = {
39560         .statfs         = aufs_statfs,
39561         .put_super      = aufs_put_super,
39562         .sync_fs        = aufs_sync_fs,
39563 -       .remount_fs     = aufs_remount_fs
39564 +       .remount_fs     = aufs_remount_fs,
39565 +#ifdef CONFIG_AUFS_BDEV_LOOP
39566 +       .real_loop      = aufs_real_loop
39567 +#endif
39568  };
39569  
39570  /* ---------------------------------------------------------------------- */
39571 diff --git a/include/linux/fs.h b/include/linux/fs.h
39572 index 0e44705..59fb8ae 100644
39573 --- a/include/linux/fs.h
39574 +++ b/include/linux/fs.h
39575 @@ -1931,6 +1931,10 @@ struct super_operations {
39576                                   struct shrink_control *);
39577         long (*free_cached_objects)(struct super_block *,
39578                                     struct shrink_control *);
39579 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
39580 +       /* and aufs */
39581 +       struct file *(*real_loop)(struct file *);
39582 +#endif
39583  };
39584  
39585  /*
This page took 5.103191 seconds and 3 git commands to generate.