]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
C: libcap < 2.34
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index f08fbbfafd9a0..1c597cddc351c 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -267,6 +267,7 @@ source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/erofs/Kconfig"
11  source "fs/vboxsf/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index 2ce5112b02c86..d9b4a51985538 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs5.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 50659d76976b7..80f67140e4f70 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2946,6 +2946,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:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
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 da693e6a834e5..0479902a0cd50 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -753,6 +753,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 b280e07e162b1..046000653e4d8 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1266,7 +1266,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 2e4c0fa2074b0..0b28a37f7e505 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 93d9252a00ab4..7700c5e4783b9 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1688,7 +1688,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         if (inode->i_op->update_time)
128                 return inode->i_op->update_time(inode, time, flags);
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index a28e4db075ede..12e725e34c6ca 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -776,6 +776,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 bbfa9b12b15eb..5d684d7d29207 100644
148 --- a/fs/read_write.c
149 +++ b/fs/read_write.c
150 @@ -499,6 +499,28 @@ static ssize_t __vfs_write(struct file *file, const char __user *p,
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); /* doesn't have ->read(|_iter)() op */
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); /* doesn't have ->write(|_iter)() op */
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 4e53efbd621db..9f230ee854e20 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -849,8 +849,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 @@ -866,9 +866,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 4d1ff010bc5af..457f4e4a5cc1f 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 45cc10cdf6ddd..916efd7e612b3 100644
222 --- a/include/linux/fs.h
223 +++ b/include/linux/fs.h
224 @@ -1355,6 +1355,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 @@ -1847,6 +1848,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 @@ -1917,6 +1919,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 @@ -2349,6 +2357,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 @@ -2633,6 +2642,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 206774ac69460..e5eda5226fbc7 100644
271 --- a/include/linux/lockdep.h
272 +++ b/include/linux/lockdep.h
273 @@ -376,6 +376,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 @@ -521,6 +523,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 35942084cd40d..24f5fd1a789de 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 ebbbfea48aa0b..d68d574250283 100644
311 --- a/include/linux/splice.h
312 +++ b/include/linux/splice.h
313 @@ -90,4 +90,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 ac10db66cc63f..837a3358e77ca 100644
326 --- a/kernel/locking/lockdep.c
327 +++ b/kernel/locking/lockdep.c
328 @@ -169,7 +169,7 @@ static
329  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
330  static DECLARE_BITMAP(lock_classes_in_use, 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         unsigned int class_idx = hlock->class_idx;
336  
337 @@ -190,6 +190,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
338          */
339         return lock_classes + class_idx;
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 aufs5.x-rcN mmap patch
347
348 diff --git a/fs/proc/base.c b/fs/proc/base.c
349 index eb2255e95f62b..92f6062423c0c 100644
350 --- a/fs/proc/base.c
351 +++ b/fs/proc/base.c
352 @@ -2162,7 +2162,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
353         rc = -ENOENT;
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 14c2badb8fd93..65afe5287e43e 100644
363 --- a/fs/proc/nommu.c
364 +++ b/fs/proc/nommu.c
365 @@ -41,7 +41,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 8d382d4ec0672..93a768f754c91 100644
379 --- a/fs/proc/task_mmu.c
380 +++ b/fs/proc/task_mmu.c
381 @@ -280,7 +280,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 @@ -1787,7 +1790,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 mempolicy *pol;
401         char buffer[64];
402 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
403 index 7907e6419e572..d17209cf52bc3 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 f3fe7371855ce..a5b9751a5b331 100644
420 --- a/include/linux/mm.h
421 +++ b/include/linux/mm.h
422 @@ -1688,6 +1688,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 4aba6c0c2ba80..974802987f69d 100644
453 --- a/include/linux/mm_types.h
454 +++ b/include/linux/mm_types.h
455 @@ -272,6 +272,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 @@ -351,6 +352,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  #ifdef CONFIG_SWAP
471 diff --git a/kernel/fork.c b/kernel/fork.c
472 index 48ed22774efaa..d7439388c1c52 100644
473 --- a/kernel/fork.c
474 +++ b/kernel/fork.c
475 @@ -568,7 +568,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 fccd3756b25f0..51485ceec650a 100644
486 --- a/mm/Makefile
487 +++ b/mm/Makefile
488 @@ -44,7 +44,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 gup.o $(mmu-y)
493 +                          prfile.o debug.o gup.o $(mmu-y)
494  
495  # Give 'page_alloc' its own module-parameter namespace
496  page-alloc-y := page_alloc.o
497 diff --git a/mm/filemap.c b/mm/filemap.c
498 index 23a051a7ef0fb..e2fe9132feb21 100644
499 --- a/mm/filemap.c
500 +++ b/mm/filemap.c
501 @@ -2660,7 +2660,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 f609e9ec4a253..d554b349c653c 100644
512 --- a/mm/mmap.c
513 +++ b/mm/mmap.c
514 @@ -179,7 +179,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 @@ -910,7 +910,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 @@ -1831,8 +1831,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 @@ -2683,7 +2683,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 @@ -2702,7 +2702,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 @@ -2894,7 +2894,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 @@ -2969,10 +2969,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 @@ -3263,7 +3280,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 318df4e236c99..7f051e86ea1dd 100644
609 --- a/mm/nommu.c
610 +++ b/mm/nommu.c
611 @@ -567,7 +567,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 @@ -699,7 +699,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 @@ -1222,7 +1222,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 @@ -1299,10 +1299,10 @@ 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 +               vma_fput(vma);
648         vm_area_free(vma);
649         return ret;
650  
651 diff --git a/mm/prfile.c b/mm/prfile.c
652 new file mode 100644
653 index 0000000000000..00d51187c3250
654 --- /dev/null
655 +++ b/mm/prfile.c
656 @@ -0,0 +1,86 @@
657 +// SPDX-License-Identifier: GPL-2.0
658 +/*
659 + * Mainly for aufs which mmap(2) different file and wants to print different
660 + * path in /proc/PID/maps.
661 + * Call these functions via macros defined in linux/mm.h.
662 + *
663 + * See Documentation/filesystems/aufs/design/06mmap.txt
664 + *
665 + * Copyright (c) 2014-2020 Junjro R. Okajima
666 + * Copyright (c) 2014 Ian Campbell
667 + */
668 +
669 +#include <linux/mm.h>
670 +#include <linux/file.h>
671 +#include <linux/fs.h>
672 +
673 +/* #define PRFILE_TRACE */
674 +static inline void prfile_trace(struct file *f, struct file *pr,
675 +                             const char func[], int line, const char func2[])
676 +{
677 +#ifdef PRFILE_TRACE
678 +       if (pr)
679 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
680 +#endif
681 +}
682 +
683 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
684 +                            int line)
685 +{
686 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
687 +
688 +       prfile_trace(f, pr, func, line, __func__);
689 +       file_update_time(f);
690 +       if (f && pr)
691 +               file_update_time(pr);
692 +}
693 +
694 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
695 +                              int line)
696 +{
697 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
698 +
699 +       prfile_trace(f, pr, func, line, __func__);
700 +       return (f && pr) ? pr : f;
701 +}
702 +
703 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
704 +{
705 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706 +
707 +       prfile_trace(f, pr, func, line, __func__);
708 +       get_file(f);
709 +       if (f && pr)
710 +               get_file(pr);
711 +}
712 +
713 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
714 +{
715 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
716 +
717 +       prfile_trace(f, pr, func, line, __func__);
718 +       fput(f);
719 +       if (f && pr)
720 +               fput(pr);
721 +}
722 +
723 +#ifndef CONFIG_MMU
724 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
725 +                              int line)
726 +{
727 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
728 +
729 +       prfile_trace(f, pr, func, line, __func__);
730 +       return (f && pr) ? pr : f;
731 +}
732 +
733 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
734 +{
735 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
736 +
737 +       prfile_trace(f, pr, func, line, __func__);
738 +       fput(f);
739 +       if (f && pr)
740 +               fput(pr);
741 +}
742 +#endif /* !CONFIG_MMU */
743 SPDX-License-Identifier: GPL-2.0
744 aufs5.x-rcN standalone patch
745
746 diff --git a/fs/dcache.c b/fs/dcache.c
747 index 046000653e4d8..15aa871d1b450 100644
748 --- a/fs/dcache.c
749 +++ b/fs/dcache.c
750 @@ -1371,6 +1371,7 @@ void d_walk(struct dentry *parent, void *data,
751         seq = 1;
752         goto again;
753  }
754 +EXPORT_SYMBOL_GPL(d_walk);
755  
756  struct check_mount {
757         struct vfsmount *mnt;
758 @@ -2916,6 +2917,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
759  
760         write_sequnlock(&rename_lock);
761  }
762 +EXPORT_SYMBOL_GPL(d_exchange);
763  
764  /**
765   * d_ancestor - search for an ancestor
766 diff --git a/fs/exec.c b/fs/exec.c
767 index 2c465119affcc..239bcb8906e78 100644
768 --- a/fs/exec.c
769 +++ b/fs/exec.c
770 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
771         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
772                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
773  }
774 +EXPORT_SYMBOL_GPL(path_noexec);
775  
776  #ifdef CONFIG_USELIB
777  /*
778 diff --git a/fs/fcntl.c b/fs/fcntl.c
779 index 0b28a37f7e505..f2c90a416b751 100644
780 --- a/fs/fcntl.c
781 +++ b/fs/fcntl.c
782 @@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
783   out:
784         return error;
785  }
786 +EXPORT_SYMBOL_GPL(setfl);
787  
788  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
789                       int force)
790 diff --git a/fs/file_table.c b/fs/file_table.c
791 index 30d55c9a1744a..34b9bbf4c5566 100644
792 --- a/fs/file_table.c
793 +++ b/fs/file_table.c
794 @@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
795         }
796         return ERR_PTR(-ENFILE);
797  }
798 +EXPORT_SYMBOL_GPL(alloc_empty_file);
799  
800  /*
801   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
802 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
803  }
804  
805  EXPORT_SYMBOL(fput);
806 +EXPORT_SYMBOL_GPL(__fput_sync);
807  
808  void __init files_init(void)
809  {
810 diff --git a/fs/inode.c b/fs/inode.c
811 index 7700c5e4783b9..6284b41fcf3f0 100644
812 --- a/fs/inode.c
813 +++ b/fs/inode.c
814 @@ -1694,6 +1694,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
815                 return inode->i_op->update_time(inode, time, flags);
816         return generic_update_time(inode, time, flags);
817  }
818 +EXPORT_SYMBOL_GPL(update_time);
819  
820  /**
821   *     touch_atime     -       update the access time
822 diff --git a/fs/namespace.c b/fs/namespace.c
823 index 12e725e34c6ca..fa17b9d5926ba 100644
824 --- a/fs/namespace.c
825 +++ b/fs/namespace.c
826 @@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
827         mnt_dec_writers(real_mount(mnt));
828         preempt_enable();
829  }
830 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
831  
832  /**
833   * mnt_drop_write - give up write access to a mount
834 @@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
835  {
836         return check_mnt(real_mount(mnt));
837  }
838 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
839  
840  /*
841   * vfsmount lock must be held for write
842 @@ -1903,6 +1905,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
843         }
844         return 0;
845  }
846 +EXPORT_SYMBOL_GPL(iterate_mounts);
847  
848  static void lock_mnt_tree(struct mount *mnt)
849  {
850 diff --git a/fs/notify/group.c b/fs/notify/group.c
851 index 133f723aca070..0b9f7f6d8390f 100644
852 --- a/fs/notify/group.c
853 +++ b/fs/notify/group.c
854 @@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
855  {
856         refcount_inc(&group->refcnt);
857  }
858 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
859  
860  /*
861   * Drop a reference to a group.  Free it if it's through.
862 diff --git a/fs/open.c b/fs/open.c
863 index 719b320ede52b..f88ce55c1c998 100644
864 --- a/fs/open.c
865 +++ b/fs/open.c
866 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
867         inode_unlock(dentry->d_inode);
868         return ret;
869  }
870 +EXPORT_SYMBOL_GPL(do_truncate);
871  
872  long vfs_truncate(const struct path *path, loff_t length)
873  {
874 diff --git a/fs/read_write.c b/fs/read_write.c
875 index 5d684d7d29207..6f61c0b6aa664 100644
876 --- a/fs/read_write.c
877 +++ b/fs/read_write.c
878 @@ -469,6 +469,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
879  
880         return ret;
881  }
882 +EXPORT_SYMBOL_GPL(vfs_read);
883  
884  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
885  {
886 @@ -509,6 +510,7 @@ vfs_readf_t vfs_readf(struct file *file)
887                 return new_sync_read;
888         return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
889  }
890 +EXPORT_SYMBOL_GPL(vfs_readf);
891  
892  vfs_writef_t vfs_writef(struct file *file)
893  {
894 @@ -520,6 +522,7 @@ vfs_writef_t vfs_writef(struct file *file)
895                 return new_sync_write;
896         return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
897  }
898 +EXPORT_SYMBOL_GPL(vfs_writef);
899  
900  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
901  {
902 @@ -589,6 +592,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
903  
904         return ret;
905  }
906 +EXPORT_SYMBOL_GPL(vfs_write);
907  
908  /* file_ppos returns &file->f_pos or NULL if file is stream */
909  static inline loff_t *file_ppos(struct file *file)
910 diff --git a/fs/splice.c b/fs/splice.c
911 index 9f230ee854e20..a6bd05004433c 100644
912 --- a/fs/splice.c
913 +++ b/fs/splice.c
914 @@ -862,6 +862,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
915  
916         return splice_write(pipe, out, ppos, len, flags);
917  }
918 +EXPORT_SYMBOL_GPL(do_splice_from);
919  
920  /*
921   * Attempt to initiate a splice from a file to a pipe.
922 @@ -891,6 +892,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
923  
924         return splice_read(in, ppos, pipe, len, flags);
925  }
926 +EXPORT_SYMBOL_GPL(do_splice_to);
927  
928  /**
929   * splice_direct_to_actor - splices data directly between two non-pipes
930 diff --git a/fs/sync.c b/fs/sync.c
931 index 457f4e4a5cc1f..67c66358f3fe4 100644
932 --- a/fs/sync.c
933 +++ b/fs/sync.c
934 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
935                 sb->s_op->sync_fs(sb, wait);
936         return __sync_blockdev(sb->s_bdev, wait);
937  }
938 +EXPORT_SYMBOL_GPL(__sync_filesystem);
939  
940  /*
941   * Write out and wait upon all dirty data associated with this
942 diff --git a/fs/xattr.c b/fs/xattr.c
943 index 91608d9bfc6aa..02d19ab3ba540 100644
944 --- a/fs/xattr.c
945 +++ b/fs/xattr.c
946 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
947         *xattr_value = value;
948         return error;
949  }
950 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
951  
952  ssize_t
953  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
954 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
955 index 837a3358e77ca..715ba9c1b91ae 100644
956 --- a/kernel/locking/lockdep.c
957 +++ b/kernel/locking/lockdep.c
958 @@ -190,6 +190,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
959          */
960         return lock_classes + class_idx;
961  }
962 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
963  #define hlock_class(hlock) lockdep_hlock_class(hlock)
964  
965  #ifdef CONFIG_LOCK_STAT
966 diff --git a/kernel/task_work.c b/kernel/task_work.c
967 index 825f28259a19a..b77593b29c1a5 100644
968 --- a/kernel/task_work.c
969 +++ b/kernel/task_work.c
970 @@ -126,3 +126,4 @@ void task_work_run(void)
971                 } while (work);
972         }
973  }
974 +EXPORT_SYMBOL_GPL(task_work_run);
975 diff --git a/security/security.c b/security/security.c
976 index 51de970fbb1ed..b21f1ffd90051 100644
977 --- a/security/security.c
978 +++ b/security/security.c
979 @@ -1087,6 +1087,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
980                 return 0;
981         return call_int_hook(path_rmdir, 0, dir, dentry);
982  }
983 +EXPORT_SYMBOL_GPL(security_path_rmdir);
984  
985  int security_path_unlink(const struct path *dir, struct dentry *dentry)
986  {
987 @@ -1103,6 +1104,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
988                 return 0;
989         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
990  }
991 +EXPORT_SYMBOL_GPL(security_path_symlink);
992  
993  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
994                        struct dentry *new_dentry)
995 @@ -1111,6 +1113,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
996                 return 0;
997         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
998  }
999 +EXPORT_SYMBOL_GPL(security_path_link);
1000  
1001  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1002                          const struct path *new_dir, struct dentry *new_dentry,
1003 @@ -1138,6 +1141,7 @@ int security_path_truncate(const struct path *path)
1004                 return 0;
1005         return call_int_hook(path_truncate, 0, path);
1006  }
1007 +EXPORT_SYMBOL_GPL(security_path_truncate);
1008  
1009  int security_path_chmod(const struct path *path, umode_t mode)
1010  {
1011 @@ -1145,6 +1149,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1012                 return 0;
1013         return call_int_hook(path_chmod, 0, path, mode);
1014  }
1015 +EXPORT_SYMBOL_GPL(security_path_chmod);
1016  
1017  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1018  {
1019 @@ -1152,6 +1157,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1020                 return 0;
1021         return call_int_hook(path_chown, 0, path, uid, gid);
1022  }
1023 +EXPORT_SYMBOL_GPL(security_path_chown);
1024  
1025  int security_path_chroot(const struct path *path)
1026  {
1027 @@ -1252,6 +1258,7 @@ int security_inode_permission(struct inode *inode, int mask)
1028                 return 0;
1029         return call_int_hook(inode_permission, 0, inode, mask);
1030  }
1031 +EXPORT_SYMBOL_GPL(security_inode_permission);
1032  
1033  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1034  {
1035 @@ -1429,6 +1436,7 @@ int security_file_permission(struct file *file, int mask)
1036  
1037         return fsnotify_perm(file, mask);
1038  }
1039 +EXPORT_SYMBOL_GPL(security_file_permission);
1040  
1041  int security_file_alloc(struct file *file)
1042  {
1043 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1044 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1045 +++ linux/Documentation/ABI/testing/debugfs-aufs        2019-07-11 15:42:14.455570938 +0200
1046 @@ -0,0 +1,55 @@
1047 +What:          /debug/aufs/si_<id>/
1048 +Date:          March 2009
1049 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1050 +Description:
1051 +               Under /debug/aufs, a directory named si_<id> is created
1052 +               per aufs mount, where <id> is a unique id generated
1053 +               internally.
1054 +
1055 +What:          /debug/aufs/si_<id>/plink
1056 +Date:          Apr 2013
1057 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1058 +Description:
1059 +               It has three lines and shows the information about the
1060 +               pseudo-link. The first line is a single number
1061 +               representing a number of buckets. The second line is a
1062 +               number of pseudo-links per buckets (separated by a
1063 +               blank). The last line is a single number representing a
1064 +               total number of psedo-links.
1065 +               When the aufs mount option 'noplink' is specified, it
1066 +               will show "1\n0\n0\n".
1067 +
1068 +What:          /debug/aufs/si_<id>/xib
1069 +Date:          March 2009
1070 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1071 +Description:
1072 +               It shows the consumed blocks by xib (External Inode Number
1073 +               Bitmap), its block size and file size.
1074 +               When the aufs mount option 'noxino' is specified, it
1075 +               will be empty. About XINO files, see the aufs manual.
1076 +
1077 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1078 +Date:          March 2009
1079 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1080 +Description:
1081 +               It shows the consumed blocks by xino (External Inode Number
1082 +               Translation Table), its link count, block size and file
1083 +               size.
1084 +               Due to the file size limit, there may exist multiple
1085 +               xino files per branch.  In this case, "-N" is added to
1086 +               the filename and it corresponds to the index of the
1087 +               internal xino array.  "-0" is omitted.
1088 +               When the aufs mount option 'noxino' is specified, Those
1089 +               entries won't exist.  About XINO files, see the aufs
1090 +               manual.
1091 +
1092 +What:          /debug/aufs/si_<id>/xigen
1093 +Date:          March 2009
1094 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1095 +Description:
1096 +               It shows the consumed blocks by xigen (External Inode
1097 +               Generation Table), its block size and file size.
1098 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1099 +               be created.
1100 +               When the aufs mount option 'noxino' is specified, it
1101 +               will be empty. About XINO files, see the aufs manual.
1102 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1103 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1104 +++ linux/Documentation/ABI/testing/sysfs-aufs  2019-07-11 15:42:14.455570938 +0200
1105 @@ -0,0 +1,31 @@
1106 +What:          /sys/fs/aufs/si_<id>/
1107 +Date:          March 2009
1108 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1109 +Description:
1110 +               Under /sys/fs/aufs, a directory named si_<id> is created
1111 +               per aufs mount, where <id> is a unique id generated
1112 +               internally.
1113 +
1114 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1115 +Date:          March 2009
1116 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1117 +Description:
1118 +               It shows the abolute path of a member directory (which
1119 +               is called branch) in aufs, and its permission.
1120 +
1121 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1122 +Date:          July 2013
1123 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1124 +Description:
1125 +               It shows the id of a member directory (which is called
1126 +               branch) in aufs.
1127 +
1128 +What:          /sys/fs/aufs/si_<id>/xi_path
1129 +Date:          March 2009
1130 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1131 +Description:
1132 +               It shows the abolute path of XINO (External Inode Number
1133 +               Bitmap, Translation Table and Generation Table) file
1134 +               even if it is the default path.
1135 +               When the aufs mount option 'noxino' is specified, it
1136 +               will be empty. About XINO files, see the aufs manual.
1137 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1138 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1139 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2020-01-27 10:57:18.162204582 +0100
1140 @@ -0,0 +1,171 @@
1141 +
1142 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1143 +# 
1144 +# This program is free software; you can redistribute it and/or modify
1145 +# it under the terms of the GNU General Public License as published by
1146 +# the Free Software Foundation; either version 2 of the License, or
1147 +# (at your option) any later version.
1148 +# 
1149 +# This program is distributed in the hope that it will be useful,
1150 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1151 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1152 +# GNU General Public License for more details.
1153 +# 
1154 +# You should have received a copy of the GNU General Public License
1155 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1156 +
1157 +Introduction
1158 +----------------------------------------
1159 +
1160 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1161 +1. abbrev. for "advanced multi-layered unification filesystem".
1162 +2. abbrev. for "another unionfs".
1163 +3. abbrev. for "auf das" in German which means "on the" in English.
1164 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1165 +   But "Filesystem aufs Filesystem" is hard to understand.
1166 +4. abbrev. for "African Urban Fashion Show".
1167 +
1168 +AUFS is a filesystem with features:
1169 +- multi layered stackable unification filesystem, the member directory
1170 +  is called as a branch.
1171 +- branch permission and attribute, 'readonly', 'real-readonly',
1172 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1173 +  combination.
1174 +- internal "file copy-on-write".
1175 +- logical deletion, whiteout.
1176 +- dynamic branch manipulation, adding, deleting and changing permission.
1177 +- allow bypassing aufs, user's direct branch access.
1178 +- external inode number translation table and bitmap which maintains the
1179 +  persistent aufs inode number.
1180 +- seekable directory, including NFS readdir.
1181 +- file mapping, mmap and sharing pages.
1182 +- pseudo-link, hardlink over branches.
1183 +- loopback mounted filesystem as a branch.
1184 +- several policies to select one among multiple writable branches.
1185 +- revert a single systemcall when an error occurs in aufs.
1186 +- and more...
1187 +
1188 +
1189 +Multi Layered Stackable Unification Filesystem
1190 +----------------------------------------------------------------------
1191 +Most people already knows what it is.
1192 +It is a filesystem which unifies several directories and provides a
1193 +merged single directory. When users access a file, the access will be
1194 +passed/re-directed/converted (sorry, I am not sure which English word is
1195 +correct) to the real file on the member filesystem. The member
1196 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1197 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1198 +readonly branch is handled by creating 'whiteout' on the upper writable
1199 +branch.
1200 +
1201 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1202 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1203 +different approaches to implement the merged-view.
1204 +The former tries putting it into VFS, and the latter implements as a
1205 +separate filesystem.
1206 +(If I misunderstand about these implementations, please let me know and
1207 +I shall correct it. Because it is a long time ago when I read their
1208 +source files last time).
1209 +
1210 +UnionMount's approach will be able to small, but may be hard to share
1211 +branches between several UnionMount since the whiteout in it is
1212 +implemented in the inode on branch filesystem and always
1213 +shared. According to Bharata's post, readdir does not seems to be
1214 +finished yet.
1215 +There are several missing features known in this implementations such as
1216 +- for users, the inode number may change silently. eg. copy-up.
1217 +- link(2) may break by copy-up.
1218 +- read(2) may get an obsoleted filedata (fstat(2) too).
1219 +- fcntl(F_SETLK) may be broken by copy-up.
1220 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1221 +  open(O_RDWR).
1222 +
1223 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1224 +merged into mainline. This is another implementation of UnionMount as a
1225 +separated filesystem. All the limitations and known problems which
1226 +UnionMount are equally inherited to "overlay" filesystem.
1227 +
1228 +Unionfs has a longer history. When I started implementing a stackable
1229 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1230 +inode, dentry and file objects and they have an array pointing lower
1231 +same kind objects. After contributing many patches for Unionfs, I
1232 +re-started my project AUFS (Jun 2006).
1233 +
1234 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1235 +implemented my own ideas, approaches and enhancements and it became
1236 +totally different one.
1237 +
1238 +Comparing DM snapshot and fs based implementation
1239 +- the number of bytes to be copied between devices is much smaller.
1240 +- the type of filesystem must be one and only.
1241 +- the fs must be writable, no readonly fs, even for the lower original
1242 +  device. so the compression fs will not be usable. but if we use
1243 +  loopback mount, we may address this issue.
1244 +  for instance,
1245 +       mount /cdrom/squashfs.img /sq
1246 +       losetup /sq/ext2.img
1247 +       losetup /somewhere/cow
1248 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1249 +- it will be difficult (or needs more operations) to extract the
1250 +  difference between the original device and COW.
1251 +- DM snapshot-merge may help a lot when users try merging. in the
1252 +  fs-layer union, users will use rsync(1).
1253 +
1254 +You may want to read my old paper "Filesystems in LiveCD"
1255 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1256 +
1257 +
1258 +Several characters/aspects/persona of aufs
1259 +----------------------------------------------------------------------
1260 +
1261 +Aufs has several characters, aspects or persona.
1262 +1. a filesystem, callee of VFS helper
1263 +2. sub-VFS, caller of VFS helper for branches
1264 +3. a virtual filesystem which maintains persistent inode number
1265 +4. reader/writer of files on branches such like an application
1266 +
1267 +1. Callee of VFS Helper
1268 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1269 +unlink(2) from an application reaches sys_unlink() kernel function and
1270 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1271 +calls filesystem specific unlink operation. Actually aufs implements the
1272 +unlink operation but it behaves like a redirector.
1273 +
1274 +2. Caller of VFS Helper for Branches
1275 +aufs_unlink() passes the unlink request to the branch filesystem as if
1276 +it were called from VFS. So the called unlink operation of the branch
1277 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1278 +every necessary pre/post operation for the branch filesystem.
1279 +- acquire the lock for the parent dir on a branch
1280 +- lookup in a branch
1281 +- revalidate dentry on a branch
1282 +- mnt_want_write() for a branch
1283 +- vfs_unlink() for a branch
1284 +- mnt_drop_write() for a branch
1285 +- release the lock on a branch
1286 +
1287 +3. Persistent Inode Number
1288 +One of the most important issue for a filesystem is to maintain inode
1289 +numbers. This is particularly important to support exporting a
1290 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1291 +backend block device for its own. But some storage is necessary to
1292 +keep and maintain the inode numbers. It may be a large space and may not
1293 +suit to keep in memory. Aufs rents some space from its first writable
1294 +branch filesystem (by default) and creates file(s) on it. These files
1295 +are created by aufs internally and removed soon (currently) keeping
1296 +opened.
1297 +Note: Because these files are removed, they are totally gone after
1298 +      unmounting aufs. It means the inode numbers are not persistent
1299 +      across unmount or reboot. I have a plan to make them really
1300 +      persistent which will be important for aufs on NFS server.
1301 +
1302 +4. Read/Write Files Internally (copy-on-write)
1303 +Because a branch can be readonly, when you write a file on it, aufs will
1304 +"copy-up" it to the upper writable branch internally. And then write the
1305 +originally requested thing to the file. Generally kernel doesn't
1306 +open/read/write file actively. In aufs, even a single write may cause a
1307 +internal "file copy". This behaviour is very similar to cp(1) command.
1308 +
1309 +Some people may think it is better to pass such work to user space
1310 +helper, instead of doing in kernel space. Actually I am still thinking
1311 +about it. But currently I have implemented it in kernel space.
1312 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1313 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1314 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2020-01-27 10:57:18.162204582 +0100
1315 @@ -0,0 +1,258 @@
1316 +
1317 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1318 +# 
1319 +# This program is free software; you can redistribute it and/or modify
1320 +# it under the terms of the GNU General Public License as published by
1321 +# the Free Software Foundation; either version 2 of the License, or
1322 +# (at your option) any later version.
1323 +# 
1324 +# This program is distributed in the hope that it will be useful,
1325 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1326 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1327 +# GNU General Public License for more details.
1328 +# 
1329 +# You should have received a copy of the GNU General Public License
1330 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1331 +
1332 +Basic Aufs Internal Structure
1333 +
1334 +Superblock/Inode/Dentry/File Objects
1335 +----------------------------------------------------------------------
1336 +As like an ordinary filesystem, aufs has its own
1337 +superblock/inode/dentry/file objects. All these objects have a
1338 +dynamically allocated array and store the same kind of pointers to the
1339 +lower filesystem, branch.
1340 +For example, when you build a union with one readwrite branch and one
1341 +readonly, mounted /au, /rw and /ro respectively.
1342 +- /au = /rw + /ro
1343 +- /ro/fileA exists but /rw/fileA
1344 +
1345 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1346 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1347 +- [0] = NULL (because /rw/fileA doesn't exist)
1348 +- [1] = /ro/fileA
1349 +
1350 +This style of an array is essentially same to the aufs
1351 +superblock/inode/dentry/file objects.
1352 +
1353 +Because aufs supports manipulating branches, ie. add/delete/change
1354 +branches dynamically, these objects has its own generation. When
1355 +branches are changed, the generation in aufs superblock is
1356 +incremented. And a generation in other object are compared when it is
1357 +accessed. When a generation in other objects are obsoleted, aufs
1358 +refreshes the internal array.
1359 +
1360 +
1361 +Superblock
1362 +----------------------------------------------------------------------
1363 +Additionally aufs superblock has some data for policies to select one
1364 +among multiple writable branches, XIB files, pseudo-links and kobject.
1365 +See below in detail.
1366 +About the policies which supports copy-down a directory, see
1367 +wbr_policy.txt too.
1368 +
1369 +
1370 +Branch and XINO(External Inode Number Translation Table)
1371 +----------------------------------------------------------------------
1372 +Every branch has its own xino (external inode number translation table)
1373 +file. The xino file is created and unlinked by aufs internally. When two
1374 +members of a union exist on the same filesystem, they share the single
1375 +xino file.
1376 +The struct of a xino file is simple, just a sequence of aufs inode
1377 +numbers which is indexed by the lower inode number.
1378 +In the above sample, assume the inode number of /ro/fileA is i111 and
1379 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1380 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1381 +
1382 +When the inode numbers are not contiguous, the xino file will be sparse
1383 +which has a hole in it and doesn't consume as much disk space as it
1384 +might appear. If your branch filesystem consumes disk space for such
1385 +holes, then you should specify 'xino=' option at mounting aufs.
1386 +
1387 +Aufs has a mount option to free the disk blocks for such holes in XINO
1388 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1389 +meet a problem of disk shortage due to XINO files, then you should try
1390 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1391 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1392 +the holes in XINO files.
1393 +
1394 +Also a writable branch has three kinds of "whiteout bases". All these
1395 +are existed when the branch is joined to aufs, and their names are
1396 +whiteout-ed doubly, so that users will never see their names in aufs
1397 +hierarchy.
1398 +1. a regular file which will be hardlinked to all whiteouts.
1399 +2. a directory to store a pseudo-link.
1400 +3. a directory to store an "orphan"-ed file temporary.
1401 +
1402 +1. Whiteout Base
1403 +   When you remove a file on a readonly branch, aufs handles it as a
1404 +   logical deletion and creates a whiteout on the upper writable branch
1405 +   as a hardlink of this file in order not to consume inode on the
1406 +   writable branch.
1407 +2. Pseudo-link Dir
1408 +   See below, Pseudo-link.
1409 +3. Step-Parent Dir
1410 +   When "fileC" exists on the lower readonly branch only and it is
1411 +   opened and removed with its parent dir, and then user writes
1412 +   something into it, then aufs copies-up fileC to this
1413 +   directory. Because there is no other dir to store fileC. After
1414 +   creating a file under this dir, the file is unlinked.
1415 +
1416 +Because aufs supports manipulating branches, ie. add/delete/change
1417 +dynamically, a branch has its own id. When the branch order changes,
1418 +aufs finds the new index by searching the branch id.
1419 +
1420 +
1421 +Pseudo-link
1422 +----------------------------------------------------------------------
1423 +Assume "fileA" exists on the lower readonly branch only and it is
1424 +hardlinked to "fileB" on the branch. When you write something to fileA,
1425 +aufs copies-up it to the upper writable branch. Additionally aufs
1426 +creates a hardlink under the Pseudo-link Directory of the writable
1427 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1428 +simple list. If fileB is read after unlinking fileA, aufs returns
1429 +filedata from the pseudo-link instead of the lower readonly
1430 +branch. Because the pseudo-link is based upon the inode, to keep the
1431 +inode number by xino (see above) is essentially necessary.
1432 +
1433 +All the hardlinks under the Pseudo-link Directory of the writable branch
1434 +should be restored in a proper location later. Aufs provides a utility
1435 +to do this. The userspace helpers executed at remounting and unmounting
1436 +aufs by default.
1437 +During this utility is running, it puts aufs into the pseudo-link
1438 +maintenance mode. In this mode, only the process which began the
1439 +maintenance mode (and its child processes) is allowed to operate in
1440 +aufs. Some other processes which are not related to the pseudo-link will
1441 +be allowed to run too, but the rest have to return an error or wait
1442 +until the maintenance mode ends. If a process already acquires an inode
1443 +mutex (in VFS), it has to return an error.
1444 +
1445 +
1446 +XIB(external inode number bitmap)
1447 +----------------------------------------------------------------------
1448 +Addition to the xino file per a branch, aufs has an external inode number
1449 +bitmap in a superblock object. It is also an internal file such like a
1450 +xino file.
1451 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1452 +not.
1453 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1454 +
1455 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1456 +reduce the number of consumed disk blocks for these files.
1457 +
1458 +
1459 +Virtual or Vertical Dir, and Readdir in Userspace
1460 +----------------------------------------------------------------------
1461 +In order to support multiple layers (branches), aufs readdir operation
1462 +constructs a virtual dir block on memory. For readdir, aufs calls
1463 +vfs_readdir() internally for each dir on branches, merges their entries
1464 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1465 +object. So the file object has its entry list until it is closed. The
1466 +entry list will be updated when the file position is zero and becomes
1467 +obsoleted. This decision is made in aufs automatically.
1468 +
1469 +The dynamically allocated memory block for the name of entries has a
1470 +unit of 512 bytes (by default) and stores the names contiguously (no
1471 +padding). Another block for each entry is handled by kmem_cache too.
1472 +During building dir blocks, aufs creates hash list and judging whether
1473 +the entry is whiteouted by its upper branch or already listed.
1474 +The merged result is cached in the corresponding inode object and
1475 +maintained by a customizable life-time option.
1476 +
1477 +Some people may call it can be a security hole or invite DoS attack
1478 +since the opened and once readdir-ed dir (file object) holds its entry
1479 +list and becomes a pressure for system memory. But I'd say it is similar
1480 +to files under /proc or /sys. The virtual files in them also holds a
1481 +memory page (generally) while they are opened. When an idea to reduce
1482 +memory for them is introduced, it will be applied to aufs too.
1483 +For those who really hate this situation, I've developed readdir(3)
1484 +library which operates this merging in userspace. You just need to set
1485 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1486 +kernel space for readdir(3).
1487 +
1488 +
1489 +Workqueue
1490 +----------------------------------------------------------------------
1491 +Aufs sometimes requires privilege access to a branch. For instance,
1492 +in copy-up/down operation. When a user process is going to make changes
1493 +to a file which exists in the lower readonly branch only, and the mode
1494 +of one of ancestor directories may not be writable by a user
1495 +process. Here aufs copy-up the file with its ancestors and they may
1496 +require privilege to set its owner/group/mode/etc.
1497 +This is a typical case of a application character of aufs (see
1498 +Introduction).
1499 +
1500 +Aufs uses workqueue synchronously for this case. It creates its own
1501 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1502 +passes the request to call mkdir or write (for example), and wait for
1503 +its completion. This approach solves a problem of a signal handler
1504 +simply.
1505 +If aufs didn't adopt the workqueue and changed the privilege of the
1506 +process, then the process may receive the unexpected SIGXFSZ or other
1507 +signals.
1508 +
1509 +Also aufs uses the system global workqueue ("events" kernel thread) too
1510 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1511 +whiteout base and etc. This is unrelated to a privilege.
1512 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1513 +superblock at the beginning, at the same time waits for the completion
1514 +of all queued asynchronous tasks.
1515 +
1516 +
1517 +Whiteout
1518 +----------------------------------------------------------------------
1519 +The whiteout in aufs is very similar to Unionfs's. That is represented
1520 +by its filename. UnionMount takes an approach of a file mode, but I am
1521 +afraid several utilities (find(1) or something) will have to support it.
1522 +
1523 +Basically the whiteout represents "logical deletion" which stops aufs to
1524 +lookup further, but also it represents "dir is opaque" which also stop
1525 +further lookup.
1526 +
1527 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1528 +In order to make several functions in a single systemcall to be
1529 +revertible, aufs adopts an approach to rename a directory to a temporary
1530 +unique whiteouted name.
1531 +For example, in rename(2) dir where the target dir already existed, aufs
1532 +renames the target dir to a temporary unique whiteouted name before the
1533 +actual rename on a branch, and then handles other actions (make it opaque,
1534 +update the attributes, etc). If an error happens in these actions, aufs
1535 +simply renames the whiteouted name back and returns an error. If all are
1536 +succeeded, aufs registers a function to remove the whiteouted unique
1537 +temporary name completely and asynchronously to the system global
1538 +workqueue.
1539 +
1540 +
1541 +Copy-up
1542 +----------------------------------------------------------------------
1543 +It is a well-known feature or concept.
1544 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1545 +internally and makes change to the new file on the upper writable branch.
1546 +When the trigger systemcall does not update the timestamps of the parent
1547 +dir, aufs reverts it after copy-up.
1548 +
1549 +
1550 +Move-down (aufs3.9 and later)
1551 +----------------------------------------------------------------------
1552 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1553 +the lower readonly branch to the upper writable branch when a user
1554 +changes something about the file.
1555 +"Move-down" is an opposite action of copy-up. Basically this action is
1556 +ran manually instead of automatically and internally.
1557 +For desgin and implementation, aufs has to consider these issues.
1558 +- whiteout for the file may exist on the lower branch.
1559 +- ancestor directories may not exist on the lower branch.
1560 +- diropq for the ancestor directories may exist on the upper branch.
1561 +- free space on the lower branch will reduce.
1562 +- another access to the file may happen during moving-down, including
1563 +  UDBA (see "Revalidate Dentry and UDBA").
1564 +- the file should not be hard-linked nor pseudo-linked. they should be
1565 +  handled by auplink utility later.
1566 +
1567 +Sometimes users want to move-down a file from the upper writable branch
1568 +to the lower readonly or writable branch. For instance,
1569 +- the free space of the upper writable branch is going to run out.
1570 +- create a new intermediate branch between the upper and lower branch.
1571 +- etc.
1572 +
1573 +For this purpose, use "aumvdown" command in aufs-util.git.
1574 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1575 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1576 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2020-01-27 10:57:18.162204582 +0100
1577 @@ -0,0 +1,85 @@
1578 +
1579 +# Copyright (C) 2015-2020 Junjiro R. Okajima
1580 +# 
1581 +# This program is free software; you can redistribute it and/or modify
1582 +# it under the terms of the GNU General Public License as published by
1583 +# the Free Software Foundation; either version 2 of the License, or
1584 +# (at your option) any later version.
1585 +# 
1586 +# This program is distributed in the hope that it will be useful,
1587 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1588 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1589 +# GNU General Public License for more details.
1590 +# 
1591 +# You should have received a copy of the GNU General Public License
1592 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1593 +
1594 +Support for a branch who has its ->atomic_open()
1595 +----------------------------------------------------------------------
1596 +The filesystems who implement its ->atomic_open() are not majority. For
1597 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1598 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1599 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1600 +sure whether all filesystems who have ->atomic_open() behave like this,
1601 +but NFSv4 surely returns the error.
1602 +
1603 +In order to support ->atomic_open() for aufs, there are a few
1604 +approaches.
1605 +
1606 +A. Introduce aufs_atomic_open()
1607 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1608 +     branch fs.
1609 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1610 +   an aufs user Pip Cet's approach
1611 +   - calls aufs_create(), VFS finish_open() and notify_change().
1612 +   - pass fake-mode to finish_open(), and then correct the mode by
1613 +     notify_change().
1614 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1615 +   - no aufs_atomic_open().
1616 +   - aufs_lookup() registers the TID to an aufs internal object.
1617 +   - aufs_create() does nothing when the matching TID is registered, but
1618 +     registers the mode.
1619 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1620 +     TID is registered.
1621 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1622 +   credential
1623 +   - no aufs_atomic_open().
1624 +   - aufs_create() registers the TID to an internal object. this info
1625 +     represents "this process created this file just now."
1626 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1627 +     registered TID and re-try open() with superuser's credential.
1628 +
1629 +Pros and cons for each approach.
1630 +
1631 +A.
1632 +   - straightforward but highly depends upon VFS internal.
1633 +   - the atomic behavaiour is kept.
1634 +   - some of parameters such as nameidata are hard to reproduce for
1635 +     branch fs.
1636 +   - large overhead.
1637 +B.
1638 +   - easy to implement.
1639 +   - the atomic behavaiour is lost.
1640 +C.
1641 +   - the atomic behavaiour is kept.
1642 +   - dirty and tricky.
1643 +   - VFS checks whether the file is created correctly after calling
1644 +     ->create(), which means this approach doesn't work.
1645 +D.
1646 +   - easy to implement.
1647 +   - the atomic behavaiour is lost.
1648 +   - to open a file with superuser's credential and give it to a user
1649 +     process is a bad idea, since the file object keeps the credential
1650 +     in it. It may affect LSM or something. This approach doesn't work
1651 +     either.
1652 +
1653 +The approach A is ideal, but it hard to implement. So here is a
1654 +variation of A, which is to be implemented.
1655 +
1656 +A-1. Introduce aufs_atomic_open()
1657 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1658 +       vfs_create() and finish_open().
1659 +     - the demerit is that the several checks after branch fs
1660 +       ->atomic_open() are lost. in the ordinary case, the checks are
1661 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1662 +       be implemented in aufs, but not all I am afraid.
1663 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1664 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1665 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2020-01-27 10:57:18.165538015 +0100
1666 @@ -0,0 +1,113 @@
1667 +
1668 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1669 +# 
1670 +# This program is free software; you can redistribute it and/or modify
1671 +# it under the terms of the GNU General Public License as published by
1672 +# the Free Software Foundation; either version 2 of the License, or
1673 +# (at your option) any later version.
1674 +# 
1675 +# This program is distributed in the hope that it will be useful,
1676 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1677 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1678 +# GNU General Public License for more details.
1679 +# 
1680 +# You should have received a copy of the GNU General Public License
1681 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1682 +
1683 +Lookup in a Branch
1684 +----------------------------------------------------------------------
1685 +Since aufs has a character of sub-VFS (see Introduction), it operates
1686 +lookup for branches as VFS does. It may be a heavy work. But almost all
1687 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1688 +directly connected to its parent. Digging down the directory hierarchy
1689 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1690 +aufs calls it.
1691 +
1692 +When a branch is a remote filesystem, aufs basically relies upon its
1693 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1694 +them.
1695 +For d_revalidate, aufs implements three levels of revalidate tests. See
1696 +"Revalidate Dentry and UDBA" in detail.
1697 +
1698 +
1699 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1700 +----------------------------------------------------------------------
1701 +Let's try case study.
1702 +- aufs has two branches, upper readwrite and lower readonly.
1703 +  /au = /rw + /ro
1704 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1705 +- user invoked "chmod a+rx /au/dirA"
1706 +- the internal copy-up is activated and "/rw/dirA" is created and its
1707 +  permission bits are set to world readable.
1708 +- then "/au/dirA" becomes world readable?
1709 +
1710 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1711 +or it may be a natively readonly filesystem. If aufs respects the lower
1712 +branch, it should not respond readdir request from other users. But user
1713 +allowed it by chmod. Should really aufs rejects showing the entries
1714 +under /ro/dirA?
1715 +
1716 +To be honest, I don't have a good solution for this case. So aufs
1717 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1718 +users.
1719 +When dirperm1 is specified, aufs checks only the highest one for the
1720 +directory permission, and shows the entries. Otherwise, as usual, checks
1721 +every dir existing on all branches and rejects the request.
1722 +
1723 +As a side effect, dirperm1 option improves the performance of aufs
1724 +because the number of permission check is reduced when the number of
1725 +branch is many.
1726 +
1727 +
1728 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1729 +----------------------------------------------------------------------
1730 +Generally VFS helpers re-validate a dentry as a part of lookup.
1731 +0. digging down the directory hierarchy.
1732 +1. lock the parent dir by its i_mutex.
1733 +2. lookup the final (child) entry.
1734 +3. revalidate it.
1735 +4. call the actual operation (create, unlink, etc.)
1736 +5. unlock the parent dir
1737 +
1738 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1739 +called. Actually aufs implements it and checks the dentry on a branch is
1740 +still valid.
1741 +But it is not enough. Because aufs has to release the lock for the
1742 +parent dir on a branch at the end of ->lookup() (step 2) and
1743 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1744 +held by VFS.
1745 +If the file on a branch is changed directly, eg. bypassing aufs, after
1746 +aufs released the lock, then the subsequent operation may cause
1747 +something unpleasant result.
1748 +
1749 +This situation is a result of VFS architecture, ->lookup() and
1750 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1751 +design from VFS's point of view. It is just not suitable for sub-VFS
1752 +character in aufs.
1753 +
1754 +Aufs supports such case by three level of revalidation which is
1755 +selectable by user.
1756 +1. Simple Revalidate
1757 +   Addition to the native flow in VFS's, confirm the child-parent
1758 +   relationship on the branch just after locking the parent dir on the
1759 +   branch in the "actual operation" (step 4). When this validation
1760 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1761 +   checks the validation of the dentry on branches.
1762 +2. Monitor Changes Internally by Inotify/Fsnotify
1763 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1764 +   the dentry on the branch, and returns EBUSY if it finds different
1765 +   dentry.
1766 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1767 +   during it is in cache. When the event is notified, aufs registers a
1768 +   function to kernel 'events' thread by schedule_work(). And the
1769 +   function sets some special status to the cached aufs dentry and inode
1770 +   private data. If they are not cached, then aufs has nothing to
1771 +   do. When the same file is accessed through aufs (step 0-3) later,
1772 +   aufs will detect the status and refresh all necessary data.
1773 +   In this mode, aufs has to ignore the event which is fired by aufs
1774 +   itself.
1775 +3. No Extra Validation
1776 +   This is the simplest test and doesn't add any additional revalidation
1777 +   test, and skip the revalidation in step 4. It is useful and improves
1778 +   aufs performance when system surely hide the aufs branches from user,
1779 +   by over-mounting something (or another method).
1780 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1781 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1782 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2020-01-27 10:57:18.165538015 +0100
1783 @@ -0,0 +1,74 @@
1784 +
1785 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1786 +# 
1787 +# This program is free software; you can redistribute it and/or modify
1788 +# it under the terms of the GNU General Public License as published by
1789 +# the Free Software Foundation; either version 2 of the License, or
1790 +# (at your option) any later version.
1791 +# 
1792 +# This program is distributed in the hope that it will be useful,
1793 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1794 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1795 +# GNU General Public License for more details.
1796 +# 
1797 +# You should have received a copy of the GNU General Public License
1798 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1799 +
1800 +Branch Manipulation
1801 +
1802 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1803 +and changing its permission/attribute, there are a lot of works to do.
1804 +
1805 +
1806 +Add a Branch
1807 +----------------------------------------------------------------------
1808 +o Confirm the adding dir exists outside of aufs, including loopback
1809 +  mount, and its various attributes.
1810 +o Initialize the xino file and whiteout bases if necessary.
1811 +  See struct.txt.
1812 +
1813 +o Check the owner/group/mode of the directory
1814 +  When the owner/group/mode of the adding directory differs from the
1815 +  existing branch, aufs issues a warning because it may impose a
1816 +  security risk.
1817 +  For example, when a upper writable branch has a world writable empty
1818 +  top directory, a malicious user can create any files on the writable
1819 +  branch directly, like copy-up and modify manually. If something like
1820 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1821 +  writable branch, and the writable branch is world-writable, then a
1822 +  malicious guy may create /etc/passwd on the writable branch directly
1823 +  and the infected file will be valid in aufs.
1824 +  I am afraid it can be a security issue, but aufs can do nothing except
1825 +  producing a warning.
1826 +
1827 +
1828 +Delete a Branch
1829 +----------------------------------------------------------------------
1830 +o Confirm the deleting branch is not busy
1831 +  To be general, there is one merit to adopt "remount" interface to
1832 +  manipulate branches. It is to discard caches. At deleting a branch,
1833 +  aufs checks the still cached (and connected) dentries and inodes. If
1834 +  there are any, then they are all in-use. An inode without its
1835 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1836 +
1837 +  For the cached one, aufs checks whether the same named entry exists on
1838 +  other branches.
1839 +  If the cached one is a directory, because aufs provides a merged view
1840 +  to users, as long as one dir is left on any branch aufs can show the
1841 +  dir to users. In this case, the branch can be removed from aufs.
1842 +  Otherwise aufs rejects deleting the branch.
1843 +
1844 +  If any file on the deleting branch is opened by aufs, then aufs
1845 +  rejects deleting.
1846 +
1847 +
1848 +Modify the Permission of a Branch
1849 +----------------------------------------------------------------------
1850 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1851 +  See struct.txt.
1852 +
1853 +o rw --> ro: Confirm the modifying branch is not busy
1854 +  Aufs rejects the request if any of these conditions are true.
1855 +  - a file on the branch is mmap-ed.
1856 +  - a regular file on the branch is opened for write and there is no
1857 +    same named entry on the upper branch.
1858 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1859 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1860 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2020-01-27 10:57:18.165538015 +0100
1861 @@ -0,0 +1,64 @@
1862 +
1863 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1864 +# 
1865 +# This program is free software; you can redistribute it and/or modify
1866 +# it under the terms of the GNU General Public License as published by
1867 +# the Free Software Foundation; either version 2 of the License, or
1868 +# (at your option) any later version.
1869 +# 
1870 +# This program is distributed in the hope that it will be useful,
1871 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1872 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1873 +# GNU General Public License for more details.
1874 +# 
1875 +# You should have received a copy of the GNU General Public License
1876 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1877 +
1878 +Policies to Select One among Multiple Writable Branches
1879 +----------------------------------------------------------------------
1880 +When the number of writable branch is more than one, aufs has to decide
1881 +the target branch for file creation or copy-up. By default, the highest
1882 +writable branch which has the parent (or ancestor) dir of the target
1883 +file is chosen (top-down-parent policy).
1884 +By user's request, aufs implements some other policies to select the
1885 +writable branch, for file creation several policies, round-robin,
1886 +most-free-space, and other policies. For copy-up, top-down-parent,
1887 +bottom-up-parent, bottom-up and others.
1888 +
1889 +As expected, the round-robin policy selects the branch in circular. When
1890 +you have two writable branches and creates 10 new files, 5 files will be
1891 +created for each branch. mkdir(2) systemcall is an exception. When you
1892 +create 10 new directories, all will be created on the same branch.
1893 +And the most-free-space policy selects the one which has most free
1894 +space among the writable branches. The amount of free space will be
1895 +checked by aufs internally, and users can specify its time interval.
1896 +
1897 +The policies for copy-up is more simple,
1898 +top-down-parent is equivalent to the same named on in create policy,
1899 +bottom-up-parent selects the writable branch where the parent dir
1900 +exists and the nearest upper one from the copyup-source,
1901 +bottom-up selects the nearest upper writable branch from the
1902 +copyup-source, regardless the existence of the parent dir.
1903 +
1904 +There are some rules or exceptions to apply these policies.
1905 +- If there is a readonly branch above the policy-selected branch and
1906 +  the parent dir is marked as opaque (a variation of whiteout), or the
1907 +  target (creating) file is whiteout-ed on the upper readonly branch,
1908 +  then the result of the policy is ignored and the target file will be
1909 +  created on the nearest upper writable branch than the readonly branch.
1910 +- If there is a writable branch above the policy-selected branch and
1911 +  the parent dir is marked as opaque or the target file is whiteouted
1912 +  on the branch, then the result of the policy is ignored and the target
1913 +  file will be created on the highest one among the upper writable
1914 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1915 +  it as usual.
1916 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1917 +  They try selecting the branch where the source exists as possible
1918 +  since copyup a large file will take long time. If it can't be,
1919 +  ie. the branch where the source exists is readonly, then they will
1920 +  follow the copyup policy.
1921 +- There is an exception for rename(2) when the target exists.
1922 +  If the rename target exists, aufs compares the index of the branches
1923 +  where the source and the target exists and selects the higher
1924 +  one. If the selected branch is readonly, then aufs follows the
1925 +  copyup policy.
1926 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1927 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1928 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2019-07-11 15:42:14.458904362 +0200
1929 @@ -0,0 +1,31 @@
1930 +
1931 +// to view this graph, run dot(1) command in GRAPHVIZ.
1932 +
1933 +digraph G {
1934 +node [shape=box];
1935 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1936 +
1937 +node [shape=oval];
1938 +
1939 +aufs_rename -> whinfo [label="store/remove"];
1940 +
1941 +node [shape=oval];
1942 +inode_list [label="h_inum list in branch\ncache"];
1943 +
1944 +node [shape=box];
1945 +whinode [label="h_inum list file"];
1946 +
1947 +node [shape=oval];
1948 +brmgmt [label="br_add/del/mod/umount"];
1949 +
1950 +brmgmt -> inode_list [label="create/remove"];
1951 +brmgmt -> whinode [label="load/store"];
1952 +
1953 +inode_list -> whinode [style=dashed,dir=both];
1954 +
1955 +aufs_rename -> inode_list [label="add/del"];
1956 +
1957 +aufs_lookup -> inode_list [label="search"];
1958 +
1959 +aufs_lookup -> whinfo [label="load/remove"];
1960 +}
1961 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1962 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1963 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2020-01-27 10:57:18.165538015 +0100
1964 @@ -0,0 +1,102 @@
1965 +
1966 +# Copyright (C) 2017-2020 Junjiro R. Okajima
1967 +#
1968 +# This program is free software; you can redistribute it and/or modify
1969 +# it under the terms of the GNU General Public License as published by
1970 +# the Free Software Foundation; either version 2 of the License, or
1971 +# (at your option) any later version.
1972 +#
1973 +# This program is distributed in the hope that it will be useful,
1974 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1975 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1976 +# GNU General Public License for more details.
1977 +#
1978 +# You should have received a copy of the GNU General Public License
1979 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1980 +
1981 +Special handling for renaming a directory (DIRREN)
1982 +----------------------------------------------------------------------
1983 +First, let's assume we have a simple usecase.
1984 +
1985 +- /u = /rw + /ro
1986 +- /rw/dirA exists
1987 +- /ro/dirA and /ro/dirA/file exist too
1988 +- there is no dirB on both branches
1989 +- a user issues rename("dirA", "dirB")
1990 +
1991 +Now, what should aufs behave against this rename(2)?
1992 +There are a few possible cases.
1993 +
1994 +A. returns EROFS.
1995 +   since dirA exists on a readonly branch which cannot be renamed.
1996 +B. returns EXDEV.
1997 +   it is possible to copy-up dirA (only the dir itself), but the child
1998 +   entries ("file" in this case) should not be. it must be a bad
1999 +   approach to copy-up recursively.
2000 +C. returns a success.
2001 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2002 +   is a violation of aufs' policy.
2003 +D. construct an extra information which indicates that /ro/dirA should
2004 +   be handled as the name of dirB.
2005 +   overlayfs has a similar feature called REDIRECT.
2006 +
2007 +Until now, aufs implements the case B only which returns EXDEV, and
2008 +expects the userspace application behaves like mv(1) which tries
2009 +issueing rename(2) recursively.
2010 +
2011 +A new aufs feature called DIRREN is introduced which implements the case
2012 +D. There are several "extra information" added.
2013 +
2014 +1. detailed info per renamed directory
2015 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2016 +2. the inode-number list of directories on a branch
2017 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2018 +
2019 +The filename of "detailed info per directory" represents the lower
2020 +branch, and its format is
2021 +- a type of the branch id
2022 +  one of these.
2023 +  + uuid (not implemented yet)
2024 +  + fsid
2025 +  + dev
2026 +- the inode-number of the branch root dir
2027 +
2028 +And it contains these info in a single regular file.
2029 +- magic number
2030 +- branch's inode-number of the logically renamed dir
2031 +- the name of the before-renamed dir
2032 +
2033 +The "detailed info per directory" file is created in aufs rename(2), and
2034 +loaded in any lookup.
2035 +The info is considered in lookup for the matching case only. Here
2036 +"matching" means that the root of branch (in the info filename) is same
2037 +to the current looking-up branch. After looking-up the before-renamed
2038 +name, the inode-number is compared. And the matched dentry is used.
2039 +
2040 +The "inode-number list of directories" is a regular file which contains
2041 +simply the inode-numbers on the branch. The file is created or updated
2042 +in removing the branch, and loaded in adding the branch. Its lifetime is
2043 +equal to the branch.
2044 +The list is refered in lookup, and when the current target inode is
2045 +found in the list, the aufs tries loading the "detailed info per
2046 +directory" and get the changed and valid name of the dir.
2047 +
2048 +Theoretically these "extra informaiton" may be able to be put into XATTR
2049 +in the dir inode. But aufs doesn't choose this way because
2050 +1. XATTR may not be supported by the branch (or its configuration)
2051 +2. XATTR may have its size limit.
2052 +3. XATTR may be less easy to convert than a regular file, when the
2053 +   format of the info is changed in the future.
2054 +At the same time, I agree that the regular file approach is much slower
2055 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2056 +better approach.
2057 +
2058 +This DIRREN feature is enabled by aufs configuration, and is activated
2059 +by a new mount option.
2060 +
2061 +For the more complicated case, there is a work with UDBA option, which
2062 +is to dected the direct access to the branches (by-passing aufs) and to
2063 +maintain the cashes in aufs. Since a single cached aufs dentry may
2064 +contains two names, before- and after-rename, the name comparision in
2065 +UDBA handler may not work correctly. In this case, the behaviour will be
2066 +equivalen to udba=reval case.
2067 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2068 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2069 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2020-01-27 10:57:18.165538015 +0100
2070 @@ -0,0 +1,120 @@
2071 +
2072 +# Copyright (C) 2011-2020 Junjiro R. Okajima
2073 +# 
2074 +# This program is free software; you can redistribute it and/or modify
2075 +# it under the terms of the GNU General Public License as published by
2076 +# the Free Software Foundation; either version 2 of the License, or
2077 +# (at your option) any later version.
2078 +# 
2079 +# This program is distributed in the hope that it will be useful,
2080 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2081 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2082 +# GNU General Public License for more details.
2083 +# 
2084 +# You should have received a copy of the GNU General Public License
2085 +# along with this program; if not, write to the Free Software
2086 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2087 +
2088 +
2089 +File-based Hierarchical Storage Management (FHSM)
2090 +----------------------------------------------------------------------
2091 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2092 +storage world. Aufs provides this feature as file-based with multiple
2093 +writable branches, based upon the principle of "Colder, the Lower".
2094 +Here the word "colder" means that the less used files, and "lower" means
2095 +that the position in the order of the stacked branches vertically.
2096 +These multiple writable branches are prioritized, ie. the topmost one
2097 +should be the fastest drive and be used heavily.
2098 +
2099 +o Characters in aufs FHSM story
2100 +- aufs itself and a new branch attribute.
2101 +- a new ioctl interface to move-down and to establish a connection with
2102 +  the daemon ("move-down" is a converse of "copy-up").
2103 +- userspace tool and daemon.
2104 +
2105 +The userspace daemon establishes a connection with aufs and waits for
2106 +the notification. The notified information is very similar to struct
2107 +statfs containing the number of consumed blocks and inodes.
2108 +When the consumed blocks/inodes of a branch exceeds the user-specified
2109 +upper watermark, the daemon activates its move-down process until the
2110 +consumed blocks/inodes reaches the user-specified lower watermark.
2111 +
2112 +The actual move-down is done by aufs based upon the request from
2113 +user-space since we need to maintain the inode number and the internal
2114 +pointer arrays in aufs.
2115 +
2116 +Currently aufs FHSM handles the regular files only. Additionally they
2117 +must not be hard-linked nor pseudo-linked.
2118 +
2119 +
2120 +o Cowork of aufs and the user-space daemon
2121 +  During the userspace daemon established the connection, aufs sends a
2122 +  small notification to it whenever aufs writes something into the
2123 +  writable branch. But it may cost high since aufs issues statfs(2)
2124 +  internally. So user can specify a new option to cache the
2125 +  info. Actually the notification is controlled by these factors.
2126 +  + the specified cache time.
2127 +  + classified as "force" by aufs internally.
2128 +  Until the specified time expires, aufs doesn't send the info
2129 +  except the forced cases. When aufs decide forcing, the info is always
2130 +  notified to userspace.
2131 +  For example, the number of free inodes is generally large enough and
2132 +  the shortage of it happens rarely. So aufs doesn't force the
2133 +  notification when creating a new file, directory and others. This is
2134 +  the typical case which aufs doesn't force.
2135 +  When aufs writes the actual filedata and the files consumes any of new
2136 +  blocks, the aufs forces notifying.
2137 +
2138 +
2139 +o Interfaces in aufs
2140 +- New branch attribute.
2141 +  + fhsm
2142 +    Specifies that the branch is managed by FHSM feature. In other word,
2143 +    participant in the FHSM.
2144 +    When nofhsm is set to the branch, it will not be the source/target
2145 +    branch of the move-down operation. This attribute is set
2146 +    independently from coo and moo attributes, and if you want full
2147 +    FHSM, you should specify them as well.
2148 +- New mount option.
2149 +  + fhsm_sec
2150 +    Specifies a second to suppress many less important info to be
2151 +    notified.
2152 +- New ioctl.
2153 +  + AUFS_CTL_FHSM_FD
2154 +    create a new file descriptor which userspace can read the notification
2155 +    (a subset of struct statfs) from aufs.
2156 +- Module parameter 'brs'
2157 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2158 +  be set.
2159 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2160 +  When there are two or more branches with fhsm attributes,
2161 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2162 +  terminates it. As a result of remounting and branch-manipulation, the
2163 +  number of branches with fhsm attribute can be one. In this case,
2164 +  /sbin/mount.aufs will terminate the user-space daemon.
2165 +
2166 +
2167 +Finally the operation is done as these steps in kernel-space.
2168 +- make sure that,
2169 +  + no one else is using the file.
2170 +  + the file is not hard-linked.
2171 +  + the file is not pseudo-linked.
2172 +  + the file is a regular file.
2173 +  + the parent dir is not opaqued.
2174 +- find the target writable branch.
2175 +- make sure the file is not whiteout-ed by the upper (than the target)
2176 +  branch.
2177 +- make the parent dir on the target branch.
2178 +- mutex lock the inode on the branch.
2179 +- unlink the whiteout on the target branch (if exists).
2180 +- lookup and create the whiteout-ed temporary name on the target branch.
2181 +- copy the file as the whiteout-ed temporary name on the target branch.
2182 +- rename the whiteout-ed temporary name to the original name.
2183 +- unlink the file on the source branch.
2184 +- maintain the internal pointer array and the external inode number
2185 +  table (XINO).
2186 +- maintain the timestamps and other attributes of the parent dir and the
2187 +  file.
2188 +
2189 +And of course, in every step, an error may happen. So the operation
2190 +should restore the original file state after an error happens.
2191 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2192 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2193 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2020-01-27 10:57:18.165538015 +0100
2194 @@ -0,0 +1,72 @@
2195 +
2196 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2197 +# 
2198 +# This program is free software; you can redistribute it and/or modify
2199 +# it under the terms of the GNU General Public License as published by
2200 +# the Free Software Foundation; either version 2 of the License, or
2201 +# (at your option) any later version.
2202 +# 
2203 +# This program is distributed in the hope that it will be useful,
2204 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2205 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2206 +# GNU General Public License for more details.
2207 +# 
2208 +# You should have received a copy of the GNU General Public License
2209 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2210 +
2211 +mmap(2) -- File Memory Mapping
2212 +----------------------------------------------------------------------
2213 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2214 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2215 +->mmap().
2216 +This approach is simple and good, but there is one problem.
2217 +Under /proc, several entries show the mmapped files by its path (with
2218 +device and inode number), and the printed path will be the path on the
2219 +branch fs's instead of virtual aufs's.
2220 +This is not a problem in most cases, but some utilities lsof(1) (and its
2221 +user) may expect the path on aufs.
2222 +
2223 +To address this issue, aufs adds a new member called vm_prfile in struct
2224 +vm_area_struct (and struct vm_region). The original vm_file points to
2225 +the file on the branch fs in order to handle everything correctly as
2226 +usual. The new vm_prfile points to a virtual file in aufs, and the
2227 +show-functions in procfs refers to vm_prfile if it is set.
2228 +Also we need to maintain several other places where touching vm_file
2229 +such like
2230 +- fork()/clone() copies vma and the reference count of vm_file is
2231 +  incremented.
2232 +- merging vma maintains the ref count too.
2233 +
2234 +This is not a good approach. It just fakes the printed path. But it
2235 +leaves all behaviour around f_mapping unchanged. This is surely an
2236 +advantage.
2237 +Actually aufs had adopted another complicated approach which calls
2238 +generic_file_mmap() and handles struct vm_operations_struct. In this
2239 +approach, aufs met a hard problem and I could not solve it without
2240 +switching the approach.
2241 +
2242 +There may be one more another approach which is
2243 +- bind-mount the branch-root onto the aufs-root internally
2244 +- grab the new vfsmount (ie. struct mount)
2245 +- lazy-umount the branch-root internally
2246 +- in open(2) the aufs-file, open the branch-file with the hidden
2247 +  vfsmount (instead of the original branch's vfsmount)
2248 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2249 +  but it may be possible from userspace by the mount helper.
2250 +
2251 +Adding the internal hidden vfsmount and using it in opening a file, the
2252 +file path under /proc will be printed correctly. This approach looks
2253 +smarter, but is not possible I am afraid.
2254 +- aufs-root may be bind-mount later. when it happens, another hidden
2255 +  vfsmount will be required.
2256 +- it is hard to get the chance to bind-mount and lazy-umount
2257 +  + in kernel-space, FS can have vfsmount in open(2) via
2258 +    file->f_path, and aufs can know its vfsmount. But several locks are
2259 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2260 +    here, then it may cause a deadlock.
2261 +  + in user-space, bind-mount doesn't invoke the mount helper.
2262 +- since /proc shows dev and ino, aufs has to give vma these info. it
2263 +  means a new member vm_prinode will be necessary. this is essentially
2264 +  equivalent to vm_prfile described above.
2265 +
2266 +I have to give up this "looks-smater" approach.
2267 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2268 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2269 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2020-01-27 10:57:18.165538015 +0100
2270 @@ -0,0 +1,96 @@
2271 +
2272 +# Copyright (C) 2014-2020 Junjiro R. Okajima
2273 +#
2274 +# This program is free software; you can redistribute it and/or modify
2275 +# it under the terms of the GNU General Public License as published by
2276 +# the Free Software Foundation; either version 2 of the License, or
2277 +# (at your option) any later version.
2278 +#
2279 +# This program is distributed in the hope that it will be useful,
2280 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2281 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2282 +# GNU General Public License for more details.
2283 +#
2284 +# You should have received a copy of the GNU General Public License
2285 +# along with this program; if not, write to the Free Software
2286 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2287 +
2288 +
2289 +Listing XATTR/EA and getting the value
2290 +----------------------------------------------------------------------
2291 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2292 +shows the values from the topmost existing file. This behaviour is good
2293 +for the non-dir entries since the bahaviour exactly matches the shown
2294 +information. But for the directories, aufs considers all the same named
2295 +entries on the lower branches. Which means, if one of the lower entry
2296 +rejects readdir call, then aufs returns an error even if the topmost
2297 +entry allows it. This behaviour is necessary to respect the branch fs's
2298 +security, but can make users confused since the user-visible standard
2299 +attributes don't match the behaviour.
2300 +To address this issue, aufs has a mount option called dirperm1 which
2301 +checks the permission for the topmost entry only, and ignores the lower
2302 +entry's permission.
2303 +
2304 +A similar issue can happen around XATTR.
2305 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2306 +always set. Otherwise these very unpleasant situation would happen.
2307 +- listxattr(2) may return the duplicated entries.
2308 +- users may not be able to remove or reset the XATTR forever,
2309 +
2310 +
2311 +XATTR/EA support in the internal (copy,move)-(up,down)
2312 +----------------------------------------------------------------------
2313 +Generally the extended attributes of inode are categorized as these.
2314 +- "security" for LSM and capability.
2315 +- "system" for posix ACL, 'acl' mount option is required for the branch
2316 +  fs generally.
2317 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2318 +- "user" for userspace, 'user_xattr' mount option is required for the
2319 +  branch fs generally.
2320 +
2321 +Moreover there are some other categories. Aufs handles these rather
2322 +unpopular categories as the ordinary ones, ie. there is no special
2323 +condition nor exception.
2324 +
2325 +In copy-up, the support for XATTR on the dst branch may differ from the
2326 +src branch. In this case, the copy-up operation will get an error and
2327 +the original user operation which triggered the copy-up will fail. It
2328 +can happen that even all copy-up will fail.
2329 +When both of src and dst branches support XATTR and if an error occurs
2330 +during copying XATTR, then the copy-up should fail obviously. That is a
2331 +good reason and aufs should return an error to userspace. But when only
2332 +the src branch support that XATTR, aufs should not return an error.
2333 +For example, the src branch supports ACL but the dst branch doesn't
2334 +because the dst branch may natively un-support it or temporary
2335 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2336 +may NOT return an error even if the XATTR is not supported. It is
2337 +totally up to the branch fs.
2338 +
2339 +Anyway when the aufs internal copy-up gets an error from the dst branch
2340 +fs, then aufs tries removing the just copied entry and returns the error
2341 +to the userspace. The worst case of this situation will be all copy-up
2342 +will fail.
2343 +
2344 +For the copy-up operation, there two basic approaches.
2345 +- copy the specified XATTR only (by category above), and return the
2346 +  error unconditionally if it happens.
2347 +- copy all XATTR, and ignore the error on the specified category only.
2348 +
2349 +In order to support XATTR and to implement the correct behaviour, aufs
2350 +chooses the latter approach and introduces some new branch attributes,
2351 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2352 +They correspond to the XATTR namespaces (see above). Additionally, to be
2353 +convenient, "icex" is also provided which means all "icex*" attributes
2354 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2355 +
2356 +The meaning of these attributes is to ignore the error from setting
2357 +XATTR on that branch.
2358 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2359 +error from the dst branch according to the specified attributes.
2360 +
2361 +Some XATTR may have its default value. The default value may come from
2362 +the parent dir or the environment. If the default value is set at the
2363 +file creating-time, it will be overwritten by copy-up.
2364 +Some contradiction may happen I am afraid.
2365 +Do we need another attribute to stop copying XATTR? I am unsure. For
2366 +now, aufs implements the branch attributes to ignore the error.
2367 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2368 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2369 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2020-01-27 10:57:18.165538015 +0100
2370 @@ -0,0 +1,58 @@
2371 +
2372 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2373 +# 
2374 +# This program is free software; you can redistribute it and/or modify
2375 +# it under the terms of the GNU General Public License as published by
2376 +# the Free Software Foundation; either version 2 of the License, or
2377 +# (at your option) any later version.
2378 +# 
2379 +# This program is distributed in the hope that it will be useful,
2380 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2381 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2382 +# GNU General Public License for more details.
2383 +# 
2384 +# You should have received a copy of the GNU General Public License
2385 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2386 +
2387 +Export Aufs via NFS
2388 +----------------------------------------------------------------------
2389 +Here is an approach.
2390 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2391 +  generation.
2392 +- iget_locked(): initialize aufs inode generation for a new inode, and
2393 +  store it in xigen file.
2394 +- destroy_inode(): increment aufs inode generation and store it in xigen
2395 +  file. it is necessary even if it is not unlinked, because any data of
2396 +  inode may be changed by UDBA.
2397 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2398 +  build file handle by
2399 +  + branch id (4 bytes)
2400 +  + superblock generation (4 bytes)
2401 +  + inode number (4 or 8 bytes)
2402 +  + parent dir inode number (4 or 8 bytes)
2403 +  + inode generation (4 bytes))
2404 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2405 +    bytes)
2406 +  + file handle for a branch (by exportfs_encode_fh())
2407 +- fh_to_dentry():
2408 +  + find the index of a branch from its id in handle, and check it is
2409 +    still exist in aufs.
2410 +  + 1st level: get the inode number from handle and search it in cache.
2411 +  + 2nd level: if not found in cache, get the parent inode number from
2412 +    the handle and search it in cache. and then open the found parent
2413 +    dir, find the matching inode number by vfs_readdir() and get its
2414 +    name, and call lookup_one_len() for the target dentry.
2415 +  + 3rd level: if the parent dir is not cached, call
2416 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2417 +    build a pathname of it, convert it a pathname in aufs, call
2418 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2419 +    the 2nd level.
2420 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2421 +    for every branch, but not itself. to get this, (currently) aufs
2422 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2423 +    idea, but I didn't get other approach.
2424 +  + test the generation of the gotten inode.
2425 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2426 +  convert it into ESTALE for NFSD.
2427 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2428 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2429 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2430 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2431 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2020-01-27 10:57:18.165538015 +0100
2432 @@ -0,0 +1,52 @@
2433 +
2434 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2435 +# 
2436 +# This program is free software; you can redistribute it and/or modify
2437 +# it under the terms of the GNU General Public License as published by
2438 +# the Free Software Foundation; either version 2 of the License, or
2439 +# (at your option) any later version.
2440 +# 
2441 +# This program is distributed in the hope that it will be useful,
2442 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2443 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2444 +# GNU General Public License for more details.
2445 +# 
2446 +# You should have received a copy of the GNU General Public License
2447 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2448 +
2449 +Show Whiteout Mode (shwh)
2450 +----------------------------------------------------------------------
2451 +Generally aufs hides the name of whiteouts. But in some cases, to show
2452 +them is very useful for users. For instance, creating a new middle layer
2453 +(branch) by merging existing layers.
2454 +
2455 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2456 +When you have three branches,
2457 +- Bottom: 'system', squashfs (underlying base system), read-only
2458 +- Middle: 'mods', squashfs, read-only
2459 +- Top: 'overlay', ram (tmpfs), read-write
2460 +
2461 +The top layer is loaded at boot time and saved at shutdown, to preserve
2462 +the changes made to the system during the session.
2463 +When larger changes have been made, or smaller changes have accumulated,
2464 +the size of the saved top layer data grows. At this point, it would be
2465 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2466 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2467 +restoring save and load speed.
2468 +
2469 +This merging is simplified by the use of another aufs mount, of just the
2470 +two overlay branches using the 'shwh' option.
2471 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2472 +       aufs /livesys/merge_union
2473 +
2474 +A merged view of these two branches is then available at
2475 +/livesys/merge_union, and the new feature is that the whiteouts are
2476 +visible!
2477 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2478 +writing to all branches. Also the default mode for all branches is 'ro'.
2479 +It is now possible to save the combined contents of the two overlay
2480 +branches to a new squashfs, e.g.:
2481 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2482 +
2483 +This new squashfs archive can be stored on the boot device and the
2484 +initramfs will use it to replace the old one at the next boot.
2485 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2486 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2487 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2020-01-27 10:57:18.165538015 +0100
2488 @@ -0,0 +1,47 @@
2489 +
2490 +# Copyright (C) 2010-2020 Junjiro R. Okajima
2491 +#
2492 +# This program is free software; you can redistribute it and/or modify
2493 +# it under the terms of the GNU General Public License as published by
2494 +# the Free Software Foundation; either version 2 of the License, or
2495 +# (at your option) any later version.
2496 +#
2497 +# This program is distributed in the hope that it will be useful,
2498 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2499 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2500 +# GNU General Public License for more details.
2501 +#
2502 +# You should have received a copy of the GNU General Public License
2503 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2504 +
2505 +Dynamically customizable FS operations
2506 +----------------------------------------------------------------------
2507 +Generally FS operations (struct inode_operations, struct
2508 +address_space_operations, struct file_operations, etc.) are defined as
2509 +"static const", but it never means that FS have only one set of
2510 +operation. Some FS have multiple sets of them. For instance, ext2 has
2511 +three sets, one for XIP, for NOBH, and for normal.
2512 +Since aufs overrides and redirects these operations, sometimes aufs has
2513 +to change its behaviour according to the branch FS type. More importantly
2514 +VFS acts differently if a function (member in the struct) is set or
2515 +not. It means aufs should have several sets of operations and select one
2516 +among them according to the branch FS definition.
2517 +
2518 +In order to solve this problem and not to affect the behaviour of VFS,
2519 +aufs defines these operations dynamically. For instance, aufs defines
2520 +dummy direct_IO function for struct address_space_operations, but it may
2521 +not be set to the address_space_operations actually. When the branch FS
2522 +doesn't have it, aufs doesn't set it to its address_space_operations
2523 +while the function definition itself is still alive. So the behaviour
2524 +itself will not change, and it will return an error when direct_IO is
2525 +not set.
2526 +
2527 +The lifetime of these dynamically generated operation object is
2528 +maintained by aufs branch object. When the branch is removed from aufs,
2529 +the reference counter of the object is decremented. When it reaches
2530 +zero, the dynamically generated operation object will be freed.
2531 +
2532 +This approach is designed to support AIO (io_submit), Direct I/O and
2533 +XIP (DAX) mainly.
2534 +Currently this approach is applied to address_space_operations for
2535 +regular files only.
2536 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2537 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2538 +++ linux/Documentation/filesystems/aufs/README 2020-01-23 09:59:36.748660743 +0100
2539 @@ -0,0 +1,401 @@
2540 +
2541 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2542 +http://aufs.sf.net
2543 +Junjiro R. Okajima
2544 +
2545 +
2546 +0. Introduction
2547 +----------------------------------------
2548 +In the early days, aufs was entirely re-designed and re-implemented
2549 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2550 +improvements and implementations, it became totally different from
2551 +Unionfs while keeping the basic features.
2552 +Later, Unionfs Version 2.x series began taking some of the same
2553 +approaches to aufs1's.
2554 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2555 +University and his team.
2556 +
2557 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2558 +support,
2559 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2560 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2561 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2562 +  or aufs1 from CVS on SourceForge.
2563 +
2564 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2565 +      According to Christoph Hellwig, linux rejects all union-type
2566 +      filesystems but UnionMount.
2567 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2568 +
2569 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2570 +    UnionMount, and he pointed out an issue around a directory mutex
2571 +    lock and aufs addressed it. But it is still unsure whether aufs will
2572 +    be merged (or any other union solution).
2573 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2574 +
2575 +
2576 +1. Features
2577 +----------------------------------------
2578 +- unite several directories into a single virtual filesystem. The member
2579 +  directory is called as a branch.
2580 +- you can specify the permission flags to the branch, which are 'readonly',
2581 +  'readwrite' and 'whiteout-able.'
2582 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2583 +  readonly branch are modifiable logically.
2584 +- dynamic branch manipulation, add, del.
2585 +- etc...
2586 +
2587 +Also there are many enhancements in aufs, such as:
2588 +- test only the highest one for the directory permission (dirperm1)
2589 +- copyup on open (coo=)
2590 +- 'move' policy for copy-up between two writable branches, after
2591 +  checking free space.
2592 +- xattr, acl
2593 +- readdir(3) in userspace.
2594 +- keep inode number by external inode number table
2595 +- keep the timestamps of file/dir in internal copyup operation
2596 +- seekable directory, supporting NFS readdir.
2597 +- whiteout is hardlinked in order to reduce the consumption of inodes
2598 +  on branch
2599 +- do not copyup, nor create a whiteout when it is unnecessary
2600 +- revert a single systemcall when an error occurs in aufs
2601 +- remount interface instead of ioctl
2602 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2603 +- loopback mounted filesystem as a branch
2604 +- kernel thread for removing the dir who has a plenty of whiteouts
2605 +- support copyup sparse file (a file which has a 'hole' in it)
2606 +- default permission flags for branches
2607 +- selectable permission flags for ro branch, whether whiteout can
2608 +  exist or not
2609 +- export via NFS.
2610 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2611 +- support multiple writable branches, some policies to select one
2612 +  among multiple writable branches.
2613 +- a new semantics for link(2) and rename(2) to support multiple
2614 +  writable branches.
2615 +- no glibc changes are required.
2616 +- pseudo hardlink (hardlink over branches)
2617 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2618 +  including NFS or remote filesystem branch.
2619 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2620 +- and more...
2621 +
2622 +Currently these features are dropped temporary from aufs5.
2623 +See design/08plan.txt in detail.
2624 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2625 +  (robr)
2626 +- statistics of aufs thread (/sys/fs/aufs/stat)
2627 +
2628 +Features or just an idea in the future (see also design/*.txt),
2629 +- reorder the branch index without del/re-add.
2630 +- permanent xino files for NFSD
2631 +- an option for refreshing the opened files after add/del branches
2632 +- light version, without branch manipulation. (unnecessary?)
2633 +- copyup in userspace
2634 +- inotify in userspace
2635 +- readv/writev
2636 +
2637 +
2638 +2. Download
2639 +----------------------------------------
2640 +There are three GIT trees for aufs5, aufs5-linux.git,
2641 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2642 +"aufs-util.git."
2643 +While the aufs-util is always necessary, you need either of aufs5-linux
2644 +or aufs5-standalone.
2645 +
2646 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2647 +git://git.kernel.org/.../torvalds/linux.git.
2648 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2649 +build aufs5 as an external kernel module.
2650 +Several extra patches are not included in this tree. Only
2651 +aufs5-standalone tree contains them. They are described in the later
2652 +section "Configuration and Compilation."
2653 +
2654 +On the other hand, the aufs5-standalone tree has only aufs source files
2655 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2656 +But you need to apply all aufs patches manually.
2657 +
2658 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2659 +represents the linux kernel version, "linux-5.x". For instance,
2660 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2661 +"aufs5.x-rcN" branch.
2662 +
2663 +o aufs5-linux tree
2664 +$ git clone --reference /your/linux/git/tree \
2665 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2666 +- if you don't have linux GIT tree, then remove "--reference ..."
2667 +$ cd aufs5-linux.git
2668 +$ git checkout origin/aufs5.0
2669 +
2670 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2671 +leave the patch-work to GIT.
2672 +$ cd /your/linux/git/tree
2673 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2674 +$ git fetch aufs5
2675 +$ git checkout -b my5.0 v5.0
2676 +$ (add your local change...)
2677 +$ git pull aufs5 aufs5.0
2678 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2679 +- you may need to solve some conflicts between your_changes and
2680 +  aufs5.0. in this case, git-rerere is recommended so that you can
2681 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2682 +  later in the future.
2683 +
2684 +o aufs5-standalone tree
2685 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2686 +$ cd aufs5-standalone.git
2687 +$ git checkout origin/aufs5.0
2688 +
2689 +o aufs-util tree
2690 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2691 +- note that the public aufs-util.git is on SourceForge instead of
2692 +  GitHUB.
2693 +$ cd aufs-util.git
2694 +$ git checkout origin/aufs5.0
2695 +
2696 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2697 +The minor version number, 'x' in '5.x', of aufs may not always
2698 +follow the minor version number of the kernel.
2699 +Because changes in the kernel that cause the use of a new
2700 +minor version number do not always require changes to aufs-util.
2701 +
2702 +Since aufs-util has its own minor version number, you may not be
2703 +able to find a GIT branch in aufs-util for your kernel's
2704 +exact minor version number.
2705 +In this case, you should git-checkout the branch for the
2706 +nearest lower number.
2707 +
2708 +For (an unreleased) example:
2709 +If you are using "linux-5.10" and the "aufs5.10" branch
2710 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2711 +or something numerically smaller is the branch for your kernel.
2712 +
2713 +Also you can view all branches by
2714 +       $ git branch -a
2715 +
2716 +
2717 +3. Configuration and Compilation
2718 +----------------------------------------
2719 +Make sure you have git-checkout'ed the correct branch.
2720 +
2721 +For aufs5-linux tree,
2722 +- enable CONFIG_AUFS_FS.
2723 +- set other aufs configurations if necessary.
2724 +
2725 +For aufs5-standalone tree,
2726 +There are several ways to build.
2727 +
2728 +1.
2729 +- apply ./aufs5-kbuild.patch to your kernel source files.
2730 +- apply ./aufs5-base.patch too.
2731 +- apply ./aufs5-mmap.patch too.
2732 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2733 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2734 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2735 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2736 +- enable CONFIG_AUFS_FS, you can select either
2737 +  =m or =y.
2738 +- and build your kernel as usual.
2739 +- install the built kernel.
2740 +- install the header files too by "make headers_install" to the
2741 +  directory where you specify. By default, it is $PWD/usr.
2742 +  "make help" shows a brief note for headers_install.
2743 +- and reboot your system.
2744 +
2745 +2.
2746 +- module only (CONFIG_AUFS_FS=m).
2747 +- apply ./aufs5-base.patch to your kernel source files.
2748 +- apply ./aufs5-mmap.patch too.
2749 +- apply ./aufs5-standalone.patch too.
2750 +- build your kernel, don't forget "make headers_install", and reboot.
2751 +- edit ./config.mk and set other aufs configurations if necessary.
2752 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2753 +  every aufs configurations.
2754 +- build the module by simple "make".
2755 +- you can specify ${KDIR} make variable which points to your kernel
2756 +  source tree.
2757 +- install the files
2758 +  + run "make install" to install the aufs module, or copy the built
2759 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2760 +  + run "make install_headers" (instead of headers_install) to install
2761 +    the modified aufs header file (you can specify DESTDIR which is
2762 +    available in aufs standalone version's Makefile only), or copy
2763 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2764 +    you like manually. By default, the target directory is $PWD/usr.
2765 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2766 +  kernel source tree.
2767 +
2768 +Note: The header file aufs_type.h is necessary to build aufs-util
2769 +      as well as "make headers_install" in the kernel source tree.
2770 +      headers_install is subject to be forgotten, but it is essentially
2771 +      necessary, not only for building aufs-util.
2772 +      You may not meet problems without headers_install in some older
2773 +      version though.
2774 +
2775 +And then,
2776 +- read README in aufs-util, build and install it
2777 +- note that your distribution may contain an obsoleted version of
2778 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2779 +  utilities, make sure that your compiler refers the correct aufs header
2780 +  file which is built by "make headers_install."
2781 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2782 +  then run "make install_ulib" too. And refer to the aufs manual in
2783 +  detail.
2784 +
2785 +There several other patches in aufs5-standalone.git. They are all
2786 +optional. When you meet some problems, they will help you.
2787 +- aufs5-loopback.patch
2788 +  Supports a nested loopback mount in a branch-fs. This patch is
2789 +  unnecessary until aufs produces a message like "you may want to try
2790 +  another patch for loopback file".
2791 +- proc_mounts.patch
2792 +  When there are many mountpoints and many mount(2)/umount(2) are
2793 +  running, then /proc/mounts may not show the all mountpoints.  This
2794 +  patch makes /proc/mounts always show the full mountpoints list.
2795 +  If you don't want to apply this patch and meet such problem, then you
2796 +  need to increase the value of 'ProcMounts_Times' make-variable in
2797 +  aufs-util.git as a second best solution.
2798 +- vfs-ino.patch
2799 +  Modifies a system global kernel internal function get_next_ino() in
2800 +  order to stop assigning 0 for an inode-number. Not directly related to
2801 +  aufs, but recommended generally.
2802 +- tmpfs-idr.patch
2803 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2804 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2805 +  duplication of inode number, which is important for backup tools and
2806 +  other utilities. When you find aufs XINO files for tmpfs branch
2807 +  growing too much, try this patch.
2808 +- lockdep-debug.patch
2809 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2810 +  also a caller of VFS functions for branch filesystems, subclassing of
2811 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2812 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2813 +  need to apply this debug patch to expand several constant values.
2814 +  If you don't know what LOCKDEP is, then you don't have apply this
2815 +  patch.
2816 +
2817 +
2818 +4. Usage
2819 +----------------------------------------
2820 +At first, make sure aufs-util are installed, and please read the aufs
2821 +manual, aufs.5 in aufs-util.git tree.
2822 +$ man -l aufs.5
2823 +
2824 +And then,
2825 +$ mkdir /tmp/rw /tmp/aufs
2826 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2827 +
2828 +Here is another example. The result is equivalent.
2829 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2830 +  Or
2831 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2832 +# mount -o remount,append:${HOME} /tmp/aufs
2833 +
2834 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2835 +you modify a file under /tmp/aufs, the one on your home directory is
2836 +not affected, instead the same named file will be newly created under
2837 +/tmp/rw. And all of your modification to a file will be applied to
2838 +the one under /tmp/rw. This is called the file based Copy on Write
2839 +(COW) method.
2840 +Aufs mount options are described in aufs.5.
2841 +If you run chroot or something and make your aufs as a root directory,
2842 +then you need to customize the shutdown script. See the aufs manual in
2843 +detail.
2844 +
2845 +Additionally, there are some sample usages of aufs which are a
2846 +diskless system with network booting, and LiveCD over NFS.
2847 +See sample dir in CVS tree on SourceForge.
2848 +
2849 +
2850 +5. Contact
2851 +----------------------------------------
2852 +When you have any problems or strange behaviour in aufs, please let me
2853 +know with:
2854 +- /proc/mounts (instead of the output of mount(8))
2855 +- /sys/module/aufs/*
2856 +- /sys/fs/aufs/* (if you have them)
2857 +- /debug/aufs/* (if you have them)
2858 +- linux kernel version
2859 +  if your kernel is not plain, for example modified by distributor,
2860 +  the url where i can download its source is necessary too.
2861 +- aufs version which was printed at loading the module or booting the
2862 +  system, instead of the date you downloaded.
2863 +- configuration (define/undefine CONFIG_AUFS_xxx)
2864 +- kernel configuration or /proc/config.gz (if you have it)
2865 +- LSM (linux security module, if you are using)
2866 +- behaviour which you think to be incorrect
2867 +- actual operation, reproducible one is better
2868 +- mailto: aufs-users at lists.sourceforge.net
2869 +
2870 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2871 +and Feature Requests) on SourceForge. Please join and write to
2872 +aufs-users ML.
2873 +
2874 +
2875 +6. Acknowledgements
2876 +----------------------------------------
2877 +Thanks to everyone who have tried and are using aufs, whoever
2878 +have reported a bug or any feedback.
2879 +
2880 +Especially donators:
2881 +Tomas Matejicek(slax.org) made a donation (much more than once).
2882 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2883 +       scripts) is making "doubling" donations.
2884 +       Unfortunately I cannot list all of the donators, but I really
2885 +       appreciate.
2886 +       It ends Aug 2010, but the ordinary donation URL is still available.
2887 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2888 +Dai Itasaka made a donation (2007/8).
2889 +Chuck Smith made a donation (2008/4, 10 and 12).
2890 +Henk Schoneveld made a donation (2008/9).
2891 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2892 +Francois Dupoux made a donation (2008/11).
2893 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2894 +       aufs2 GIT tree (2009/2).
2895 +William Grant made a donation (2009/3).
2896 +Patrick Lane made a donation (2009/4).
2897 +The Mail Archive (mail-archive.com) made donations (2009/5).
2898 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2899 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2900 +Pavel Pronskiy made a donation (2011/2).
2901 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2902 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2903 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2904 +11).
2905 +Sam Liddicott made a donation (2011/9).
2906 +Era Scarecrow made a donation (2013/4).
2907 +Bor Ratajc made a donation (2013/4).
2908 +Alessandro Gorreta made a donation (2013/4).
2909 +POIRETTE Marc made a donation (2013/4).
2910 +Alessandro Gorreta made a donation (2013/4).
2911 +lauri kasvandik made a donation (2013/5).
2912 +"pemasu from Finland" made a donation (2013/7).
2913 +The Parted Magic Project made a donation (2013/9 and 11).
2914 +Pavel Barta made a donation (2013/10).
2915 +Nikolay Pertsev made a donation (2014/5).
2916 +James B made a donation (2014/7 and 2015/7).
2917 +Stefano Di Biase made a donation (2014/8).
2918 +Daniel Epellei made a donation (2015/1).
2919 +OmegaPhil made a donation (2016/1, 2018/4).
2920 +Tomasz Szewczyk made a donation (2016/4).
2921 +James Burry made a donation (2016/12).
2922 +Carsten Rose made a donation (2018/9).
2923 +Porteus Kiosk made a donation (2018/10).
2924 +
2925 +Thank you very much.
2926 +Donations are always, including future donations, very important and
2927 +helpful for me to keep on developing aufs.
2928 +
2929 +
2930 +7.
2931 +----------------------------------------
2932 +If you are an experienced user, no explanation is needed. Aufs is
2933 +just a linux filesystem.
2934 +
2935 +
2936 +Enjoy!
2937 +
2938 +# Local variables: ;
2939 +# mode: text;
2940 +# End: ;
2941 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2942 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2943 +++ linux/fs/aufs/aufs.h        2020-01-27 10:57:18.165538015 +0100
2944 @@ -0,0 +1,62 @@
2945 +/* SPDX-License-Identifier: GPL-2.0 */
2946 +/*
2947 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2948 + *
2949 + * This program, aufs is free software; you can redistribute it and/or modify
2950 + * it under the terms of the GNU General Public License as published by
2951 + * the Free Software Foundation; either version 2 of the License, or
2952 + * (at your option) any later version.
2953 + *
2954 + * This program is distributed in the hope that it will be useful,
2955 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2956 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2957 + * GNU General Public License for more details.
2958 + *
2959 + * You should have received a copy of the GNU General Public License
2960 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2961 + */
2962 +
2963 +/*
2964 + * all header files
2965 + */
2966 +
2967 +#ifndef __AUFS_H__
2968 +#define __AUFS_H__
2969 +
2970 +#ifdef __KERNEL__
2971 +
2972 +#define AuStub(type, name, body, ...) \
2973 +       static inline type name(__VA_ARGS__) { body; }
2974 +
2975 +#define AuStubVoid(name, ...) \
2976 +       AuStub(void, name, , __VA_ARGS__)
2977 +#define AuStubInt0(name, ...) \
2978 +       AuStub(int, name, return 0, __VA_ARGS__)
2979 +
2980 +#include "debug.h"
2981 +
2982 +#include "branch.h"
2983 +#include "cpup.h"
2984 +#include "dcsub.h"
2985 +#include "dbgaufs.h"
2986 +#include "dentry.h"
2987 +#include "dir.h"
2988 +#include "dirren.h"
2989 +#include "dynop.h"
2990 +#include "file.h"
2991 +#include "fstype.h"
2992 +#include "hbl.h"
2993 +#include "inode.h"
2994 +#include "lcnt.h"
2995 +#include "loop.h"
2996 +#include "module.h"
2997 +#include "opts.h"
2998 +#include "rwsem.h"
2999 +#include "super.h"
3000 +#include "sysaufs.h"
3001 +#include "vfsub.h"
3002 +#include "whout.h"
3003 +#include "wkq.h"
3004 +
3005 +#endif /* __KERNEL__ */
3006 +#endif /* __AUFS_H__ */
3007 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3008 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3009 +++ linux/fs/aufs/branch.c      2020-08-03 09:14:46.095748745 +0200
3010 @@ -0,0 +1,1427 @@
3011 +// SPDX-License-Identifier: GPL-2.0
3012 +/*
3013 + * Copyright (C) 2005-2020 Junjiro R. Okajima
3014 + *
3015 + * This program, aufs is free software; you can redistribute it and/or modify
3016 + * it under the terms of the GNU General Public License as published by
3017 + * the Free Software Foundation; either version 2 of the License, or
3018 + * (at your option) any later version.
3019 + *
3020 + * This program is distributed in the hope that it will be useful,
3021 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3022 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3023 + * GNU General Public License for more details.
3024 + *
3025 + * You should have received a copy of the GNU General Public License
3026 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3027 + */
3028 +
3029 +/*
3030 + * branch management
3031 + */
3032 +
3033 +#include <linux/compat.h>
3034 +#include <linux/statfs.h>
3035 +#include "aufs.h"
3036 +
3037 +/*
3038 + * free a single branch
3039 + */
3040 +static void au_br_do_free(struct au_branch *br)
3041 +{
3042 +       int i;
3043 +       struct au_wbr *wbr;
3044 +       struct au_dykey **key;
3045 +
3046 +       au_hnotify_fin_br(br);
3047 +       /* always, regardless the mount option */
3048 +       au_dr_hino_free(&br->br_dirren);
3049 +       au_xino_put(br);
3050 +
3051 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3052 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3053 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3054 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
3055 +
3056 +       wbr = br->br_wbr;
3057 +       if (wbr) {
3058 +               for (i = 0; i < AuBrWh_Last; i++)
3059 +                       dput(wbr->wbr_wh[i]);
3060 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3061 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3062 +       }
3063 +
3064 +       if (br->br_fhsm) {
3065 +               au_br_fhsm_fin(br->br_fhsm);
3066 +               au_kfree_try_rcu(br->br_fhsm);
3067 +       }
3068 +
3069 +       key = br->br_dykey;
3070 +       for (i = 0; i < AuBrDynOp; i++, key++)
3071 +               if (*key)
3072 +                       au_dy_put(*key);
3073 +               else
3074 +                       break;
3075 +
3076 +       /* recursive lock, s_umount of branch's */
3077 +       /* synchronize_rcu(); */ /* why? */
3078 +       lockdep_off();
3079 +       path_put(&br->br_path);
3080 +       lockdep_on();
3081 +       au_kfree_rcu(wbr);
3082 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3083 +       au_lcnt_wait_for_fin(&br->br_count);
3084 +       /* I don't know why, but percpu_refcount requires this */
3085 +       /* synchronize_rcu(); */
3086 +       au_kfree_rcu(br);
3087 +}
3088 +
3089 +/*
3090 + * frees all branches
3091 + */
3092 +void au_br_free(struct au_sbinfo *sbinfo)
3093 +{
3094 +       aufs_bindex_t bmax;
3095 +       struct au_branch **br;
3096 +
3097 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3098 +
3099 +       bmax = sbinfo->si_bbot + 1;
3100 +       br = sbinfo->si_branch;
3101 +       while (bmax--)
3102 +               au_br_do_free(*br++);
3103 +}
3104 +
3105 +/*
3106 + * find the index of a branch which is specified by @br_id.
3107 + */
3108 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3109 +{
3110 +       aufs_bindex_t bindex, bbot;
3111 +
3112 +       bbot = au_sbbot(sb);
3113 +       for (bindex = 0; bindex <= bbot; bindex++)
3114 +               if (au_sbr_id(sb, bindex) == br_id)
3115 +                       return bindex;
3116 +       return -1;
3117 +}
3118 +
3119 +/* ---------------------------------------------------------------------- */
3120 +
3121 +/*
3122 + * add a branch
3123 + */
3124 +
3125 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3126 +                       struct dentry *h_root)
3127 +{
3128 +       if (unlikely(h_adding == h_root
3129 +                    || au_test_loopback_overlap(sb, h_adding)))
3130 +               return 1;
3131 +       if (h_adding->d_sb != h_root->d_sb)
3132 +               return 0;
3133 +       return au_test_subdir(h_adding, h_root)
3134 +               || au_test_subdir(h_root, h_adding);
3135 +}
3136 +
3137 +/*
3138 + * returns a newly allocated branch. @new_nbranch is a number of branches
3139 + * after adding a branch.
3140 + */
3141 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3142 +                                    int perm)
3143 +{
3144 +       struct au_branch *add_branch;
3145 +       struct dentry *root;
3146 +       struct inode *inode;
3147 +       int err;
3148 +
3149 +       err = -ENOMEM;
3150 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3151 +       if (unlikely(!add_branch))
3152 +               goto out;
3153 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3154 +       if (unlikely(!add_branch->br_xino))
3155 +               goto out_br;
3156 +       err = au_hnotify_init_br(add_branch, perm);
3157 +       if (unlikely(err))
3158 +               goto out_xino;
3159 +
3160 +       if (au_br_writable(perm)) {
3161 +               /* may be freed separately at changing the branch permission */
3162 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3163 +                                            GFP_NOFS);
3164 +               if (unlikely(!add_branch->br_wbr))
3165 +                       goto out_hnotify;
3166 +       }
3167 +
3168 +       if (au_br_fhsm(perm)) {
3169 +               err = au_fhsm_br_alloc(add_branch);
3170 +               if (unlikely(err))
3171 +                       goto out_wbr;
3172 +       }
3173 +
3174 +       root = sb->s_root;
3175 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3176 +       if (!err)
3177 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3178 +       if (!err) {
3179 +               inode = d_inode(root);
3180 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3181 +                                       /*may_shrink*/0);
3182 +       }
3183 +       if (!err)
3184 +               return add_branch; /* success */
3185 +
3186 +out_wbr:
3187 +       au_kfree_rcu(add_branch->br_wbr);
3188 +out_hnotify:
3189 +       au_hnotify_fin_br(add_branch);
3190 +out_xino:
3191 +       au_xino_put(add_branch);
3192 +out_br:
3193 +       au_kfree_rcu(add_branch);
3194 +out:
3195 +       return ERR_PTR(err);
3196 +}
3197 +
3198 +/*
3199 + * test if the branch permission is legal or not.
3200 + */
3201 +static int test_br(struct inode *inode, int brperm, char *path)
3202 +{
3203 +       int err;
3204 +
3205 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3206 +       if (!err)
3207 +               goto out;
3208 +
3209 +       err = -EINVAL;
3210 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3211 +
3212 +out:
3213 +       return err;
3214 +}
3215 +
3216 +/*
3217 + * returns:
3218 + * 0: success, the caller will add it
3219 + * plus: success, it is already unified, the caller should ignore it
3220 + * minus: error
3221 + */
3222 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3223 +{
3224 +       int err;
3225 +       aufs_bindex_t bbot, bindex;
3226 +       struct dentry *root, *h_dentry;
3227 +       struct inode *inode, *h_inode;
3228 +
3229 +       root = sb->s_root;
3230 +       bbot = au_sbbot(sb);
3231 +       if (unlikely(bbot >= 0
3232 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3233 +               err = 1;
3234 +               if (!remount) {
3235 +                       err = -EINVAL;
3236 +                       pr_err("%s duplicated\n", add->pathname);
3237 +               }
3238 +               goto out;
3239 +       }
3240 +
3241 +       err = -ENOSPC; /* -E2BIG; */
3242 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3243 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3244 +               pr_err("number of branches exceeded %s\n", add->pathname);
3245 +               goto out;
3246 +       }
3247 +
3248 +       err = -EDOM;
3249 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3250 +               pr_err("bad index %d\n", add->bindex);
3251 +               goto out;
3252 +       }
3253 +
3254 +       inode = d_inode(add->path.dentry);
3255 +       err = -ENOENT;
3256 +       if (unlikely(!inode->i_nlink)) {
3257 +               pr_err("no existence %s\n", add->pathname);
3258 +               goto out;
3259 +       }
3260 +
3261 +       err = -EINVAL;
3262 +       if (unlikely(inode->i_sb == sb)) {
3263 +               pr_err("%s must be outside\n", add->pathname);
3264 +               goto out;
3265 +       }
3266 +
3267 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3268 +               pr_err("unsupported filesystem, %s (%s)\n",
3269 +                      add->pathname, au_sbtype(inode->i_sb));
3270 +               goto out;
3271 +       }
3272 +
3273 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3274 +               pr_err("already stacked, %s (%s)\n",
3275 +                      add->pathname, au_sbtype(inode->i_sb));
3276 +               goto out;
3277 +       }
3278 +
3279 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3280 +       if (unlikely(err))
3281 +               goto out;
3282 +
3283 +       if (bbot < 0)
3284 +               return 0; /* success */
3285 +
3286 +       err = -EINVAL;
3287 +       for (bindex = 0; bindex <= bbot; bindex++)
3288 +               if (unlikely(test_overlap(sb, add->path.dentry,
3289 +                                         au_h_dptr(root, bindex)))) {
3290 +                       pr_err("%s is overlapped\n", add->pathname);
3291 +                       goto out;
3292 +               }
3293 +
3294 +       err = 0;
3295 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3296 +               h_dentry = au_h_dptr(root, 0);
3297 +               h_inode = d_inode(h_dentry);
3298 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3299 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3300 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3301 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3302 +                               add->pathname,
3303 +                               i_uid_read(inode), i_gid_read(inode),
3304 +                               (inode->i_mode & S_IALLUGO),
3305 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3306 +                               (h_inode->i_mode & S_IALLUGO));
3307 +       }
3308 +
3309 +out:
3310 +       return err;
3311 +}
3312 +
3313 +/*
3314 + * initialize or clean the whiteouts for an adding branch
3315 + */
3316 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3317 +                        int new_perm)
3318 +{
3319 +       int err, old_perm;
3320 +       aufs_bindex_t bindex;
3321 +       struct inode *h_inode;
3322 +       struct au_wbr *wbr;
3323 +       struct au_hinode *hdir;
3324 +       struct dentry *h_dentry;
3325 +
3326 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3327 +       if (unlikely(err))
3328 +               goto out;
3329 +
3330 +       wbr = br->br_wbr;
3331 +       old_perm = br->br_perm;
3332 +       br->br_perm = new_perm;
3333 +       hdir = NULL;
3334 +       h_inode = NULL;
3335 +       bindex = au_br_index(sb, br->br_id);
3336 +       if (0 <= bindex) {
3337 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3338 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3339 +       } else {
3340 +               h_dentry = au_br_dentry(br);
3341 +               h_inode = d_inode(h_dentry);
3342 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3343 +       }
3344 +       if (!wbr)
3345 +               err = au_wh_init(br, sb);
3346 +       else {
3347 +               wbr_wh_write_lock(wbr);
3348 +               err = au_wh_init(br, sb);
3349 +               wbr_wh_write_unlock(wbr);
3350 +       }
3351 +       if (hdir)
3352 +               au_hn_inode_unlock(hdir);
3353 +       else
3354 +               inode_unlock(h_inode);
3355 +       vfsub_mnt_drop_write(au_br_mnt(br));
3356 +       br->br_perm = old_perm;
3357 +
3358 +       if (!err && wbr && !au_br_writable(new_perm)) {
3359 +               au_kfree_rcu(wbr);
3360 +               br->br_wbr = NULL;
3361 +       }
3362 +
3363 +out:
3364 +       return err;
3365 +}
3366 +
3367 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3368 +                      int perm)
3369 +{
3370 +       int err;
3371 +       struct kstatfs kst;
3372 +       struct au_wbr *wbr;
3373 +
3374 +       wbr = br->br_wbr;
3375 +       au_rw_init(&wbr->wbr_wh_rwsem);
3376 +       atomic_set(&wbr->wbr_wh_running, 0);
3377 +
3378 +       /*
3379 +        * a limit for rmdir/rename a dir
3380 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3381 +        */
3382 +       err = vfs_statfs(&br->br_path, &kst);
3383 +       if (unlikely(err))
3384 +               goto out;
3385 +       err = -EINVAL;
3386 +       if (kst.f_namelen >= NAME_MAX)
3387 +               err = au_br_init_wh(sb, br, perm);
3388 +       else
3389 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3390 +                      au_br_dentry(br),
3391 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3392 +
3393 +out:
3394 +       return err;
3395 +}
3396 +
3397 +/* initialize a new branch */
3398 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3399 +                     struct au_opt_add *add)
3400 +{
3401 +       int err;
3402 +       struct au_branch *brbase;
3403 +       struct file *xf;
3404 +       struct inode *h_inode;
3405 +
3406 +       err = 0;
3407 +       br->br_perm = add->perm;
3408 +       br->br_path = add->path; /* set first, path_get() later */
3409 +       spin_lock_init(&br->br_dykey_lock);
3410 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3411 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3412 +       br->br_id = au_new_br_id(sb);
3413 +       AuDebugOn(br->br_id < 0);
3414 +
3415 +       /* always, regardless the given option */
3416 +       err = au_dr_br_init(sb, br, &add->path);
3417 +       if (unlikely(err))
3418 +               goto out_err;
3419 +
3420 +       if (au_br_writable(add->perm)) {
3421 +               err = au_wbr_init(br, sb, add->perm);
3422 +               if (unlikely(err))
3423 +                       goto out_err;
3424 +       }
3425 +
3426 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3427 +               brbase = au_sbr(sb, 0);
3428 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3429 +               AuDebugOn(!xf);
3430 +               h_inode = d_inode(add->path.dentry);
3431 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3432 +               if (unlikely(err)) {
3433 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3434 +                       goto out_err;
3435 +               }
3436 +       }
3437 +
3438 +       sysaufs_br_init(br);
3439 +       path_get(&br->br_path);
3440 +       goto out; /* success */
3441 +
3442 +out_err:
3443 +       memset(&br->br_path, 0, sizeof(br->br_path));
3444 +out:
3445 +       return err;
3446 +}
3447 +
3448 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3449 +                            struct au_branch *br, aufs_bindex_t bbot,
3450 +                            aufs_bindex_t amount)
3451 +{
3452 +       struct au_branch **brp;
3453 +
3454 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3455 +
3456 +       brp = sbinfo->si_branch + bindex;
3457 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3458 +       *brp = br;
3459 +       sbinfo->si_bbot++;
3460 +       if (unlikely(bbot < 0))
3461 +               sbinfo->si_bbot = 0;
3462 +}
3463 +
3464 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3465 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3466 +{
3467 +       struct au_hdentry *hdp;
3468 +
3469 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3470 +
3471 +       hdp = au_hdentry(dinfo, bindex);
3472 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3473 +       au_h_dentry_init(hdp);
3474 +       dinfo->di_bbot++;
3475 +       if (unlikely(bbot < 0))
3476 +               dinfo->di_btop = 0;
3477 +}
3478 +
3479 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3480 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3481 +{
3482 +       struct au_hinode *hip;
3483 +
3484 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3485 +
3486 +       hip = au_hinode(iinfo, bindex);
3487 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3488 +       au_hinode_init(hip);
3489 +       iinfo->ii_bbot++;
3490 +       if (unlikely(bbot < 0))
3491 +               iinfo->ii_btop = 0;
3492 +}
3493 +
3494 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3495 +                        aufs_bindex_t bindex)
3496 +{
3497 +       struct dentry *root, *h_dentry;
3498 +       struct inode *root_inode, *h_inode;
3499 +       aufs_bindex_t bbot, amount;
3500 +
3501 +       root = sb->s_root;
3502 +       root_inode = d_inode(root);
3503 +       bbot = au_sbbot(sb);
3504 +       amount = bbot + 1 - bindex;
3505 +       h_dentry = au_br_dentry(br);
3506 +       au_sbilist_lock();
3507 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3508 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3509 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3510 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3511 +       h_inode = d_inode(h_dentry);
3512 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3513 +       au_sbilist_unlock();
3514 +}
3515 +
3516 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3517 +{
3518 +       int err;
3519 +       aufs_bindex_t bbot, add_bindex;
3520 +       struct dentry *root, *h_dentry;
3521 +       struct inode *root_inode;
3522 +       struct au_branch *add_branch;
3523 +
3524 +       root = sb->s_root;
3525 +       root_inode = d_inode(root);
3526 +       IMustLock(root_inode);
3527 +       IiMustWriteLock(root_inode);
3528 +       err = test_add(sb, add, remount);
3529 +       if (unlikely(err < 0))
3530 +               goto out;
3531 +       if (err) {
3532 +               err = 0;
3533 +               goto out; /* success */
3534 +       }
3535 +
3536 +       bbot = au_sbbot(sb);
3537 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3538 +       err = PTR_ERR(add_branch);
3539 +       if (IS_ERR(add_branch))
3540 +               goto out;
3541 +
3542 +       err = au_br_init(add_branch, sb, add);
3543 +       if (unlikely(err)) {
3544 +               au_br_do_free(add_branch);
3545 +               goto out;
3546 +       }
3547 +
3548 +       add_bindex = add->bindex;
3549 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3550 +       au_br_do_add(sb, add_branch, add_bindex);
3551 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3552 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3553 +
3554 +       h_dentry = add->path.dentry;
3555 +       if (!add_bindex) {
3556 +               au_cpup_attr_all(root_inode, /*force*/1);
3557 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3558 +       } else
3559 +               au_add_nlink(root_inode, d_inode(h_dentry));
3560 +
3561 +out:
3562 +       return err;
3563 +}
3564 +
3565 +/* ---------------------------------------------------------------------- */
3566 +
3567 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3568 +                                      unsigned long long max __maybe_unused,
3569 +                                      void *arg)
3570 +{
3571 +       unsigned long long n;
3572 +       struct file **p, *f;
3573 +       struct hlist_bl_head *files;
3574 +       struct hlist_bl_node *pos;
3575 +       struct au_finfo *finfo;
3576 +
3577 +       n = 0;
3578 +       p = a;
3579 +       files = &au_sbi(sb)->si_files;
3580 +       hlist_bl_lock(files);
3581 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3582 +               f = finfo->fi_file;
3583 +               if (file_count(f)
3584 +                   && !special_file(file_inode(f)->i_mode)) {
3585 +                       get_file(f);
3586 +                       *p++ = f;
3587 +                       n++;
3588 +                       AuDebugOn(n > max);
3589 +               }
3590 +       }
3591 +       hlist_bl_unlock(files);
3592 +
3593 +       return n;
3594 +}
3595 +
3596 +static struct file **au_farray_alloc(struct super_block *sb,
3597 +                                    unsigned long long *max)
3598 +{
3599 +       struct au_sbinfo *sbi;
3600 +
3601 +       sbi = au_sbi(sb);
3602 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3603 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3604 +}
3605 +
3606 +static void au_farray_free(struct file **a, unsigned long long max)
3607 +{
3608 +       unsigned long long ull;
3609 +
3610 +       for (ull = 0; ull < max; ull++)
3611 +               if (a[ull])
3612 +                       fput(a[ull]);
3613 +       kvfree(a);
3614 +}
3615 +
3616 +/* ---------------------------------------------------------------------- */
3617 +
3618 +/*
3619 + * delete a branch
3620 + */
3621 +
3622 +/* to show the line number, do not make it inlined function */
3623 +#define AuVerbose(do_info, fmt, ...) do { \
3624 +       if (do_info) \
3625 +               pr_info(fmt, ##__VA_ARGS__); \
3626 +} while (0)
3627 +
3628 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3629 +                        aufs_bindex_t bbot)
3630 +{
3631 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3632 +}
3633 +
3634 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3635 +                        aufs_bindex_t bbot)
3636 +{
3637 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3638 +}
3639 +
3640 +/*
3641 + * test if the branch is deletable or not.
3642 + */
3643 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3644 +                           unsigned int sigen, const unsigned int verbose)
3645 +{
3646 +       int err, i, j, ndentry;
3647 +       aufs_bindex_t btop, bbot;
3648 +       struct au_dcsub_pages dpages;
3649 +       struct au_dpage *dpage;
3650 +       struct dentry *d;
3651 +
3652 +       err = au_dpages_init(&dpages, GFP_NOFS);
3653 +       if (unlikely(err))
3654 +               goto out;
3655 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3656 +       if (unlikely(err))
3657 +               goto out_dpages;
3658 +
3659 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3660 +               dpage = dpages.dpages + i;
3661 +               ndentry = dpage->ndentry;
3662 +               for (j = 0; !err && j < ndentry; j++) {
3663 +                       d = dpage->dentries[j];
3664 +                       AuDebugOn(au_dcount(d) <= 0);
3665 +                       if (!au_digen_test(d, sigen)) {
3666 +                               di_read_lock_child(d, AuLock_IR);
3667 +                               if (unlikely(au_dbrange_test(d))) {
3668 +                                       di_read_unlock(d, AuLock_IR);
3669 +                                       continue;
3670 +                               }
3671 +                       } else {
3672 +                               di_write_lock_child(d);
3673 +                               if (unlikely(au_dbrange_test(d))) {
3674 +                                       di_write_unlock(d);
3675 +                                       continue;
3676 +                               }
3677 +                               err = au_reval_dpath(d, sigen);
3678 +                               if (!err)
3679 +                                       di_downgrade_lock(d, AuLock_IR);
3680 +                               else {
3681 +                                       di_write_unlock(d);
3682 +                                       break;
3683 +                               }
3684 +                       }
3685 +
3686 +                       /* AuDbgDentry(d); */
3687 +                       btop = au_dbtop(d);
3688 +                       bbot = au_dbbot(d);
3689 +                       if (btop <= bindex
3690 +                           && bindex <= bbot
3691 +                           && au_h_dptr(d, bindex)
3692 +                           && au_test_dbusy(d, btop, bbot)) {
3693 +                               err = -EBUSY;
3694 +                               AuVerbose(verbose, "busy %pd\n", d);
3695 +                               AuDbgDentry(d);
3696 +                       }
3697 +                       di_read_unlock(d, AuLock_IR);
3698 +               }
3699 +       }
3700 +
3701 +out_dpages:
3702 +       au_dpages_free(&dpages);
3703 +out:
3704 +       return err;
3705 +}
3706 +
3707 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3708 +                          unsigned int sigen, const unsigned int verbose)
3709 +{
3710 +       int err;
3711 +       unsigned long long max, ull;
3712 +       struct inode *i, **array;
3713 +       aufs_bindex_t btop, bbot;
3714 +
3715 +       array = au_iarray_alloc(sb, &max);
3716 +       err = PTR_ERR(array);
3717 +       if (IS_ERR(array))
3718 +               goto out;
3719 +
3720 +       err = 0;
3721 +       AuDbg("b%d\n", bindex);
3722 +       for (ull = 0; !err && ull < max; ull++) {
3723 +               i = array[ull];
3724 +               if (unlikely(!i))
3725 +                       break;
3726 +               if (i->i_ino == AUFS_ROOT_INO)
3727 +                       continue;
3728 +
3729 +               /* AuDbgInode(i); */
3730 +               if (au_iigen(i, NULL) == sigen)
3731 +                       ii_read_lock_child(i);
3732 +               else {
3733 +                       ii_write_lock_child(i);
3734 +                       err = au_refresh_hinode_self(i);
3735 +                       au_iigen_dec(i);
3736 +                       if (!err)
3737 +                               ii_downgrade_lock(i);
3738 +                       else {
3739 +                               ii_write_unlock(i);
3740 +                               break;
3741 +                       }
3742 +               }
3743 +
3744 +               btop = au_ibtop(i);
3745 +               bbot = au_ibbot(i);
3746 +               if (btop <= bindex
3747 +                   && bindex <= bbot
3748 +                   && au_h_iptr(i, bindex)
3749 +                   && au_test_ibusy(i, btop, bbot)) {
3750 +                       err = -EBUSY;
3751 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3752 +                       AuDbgInode(i);
3753 +               }
3754 +               ii_read_unlock(i);
3755 +       }
3756 +       au_iarray_free(array, max);
3757 +
3758 +out:
3759 +       return err;
3760 +}
3761 +
3762 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3763 +                             const unsigned int verbose)
3764 +{
3765 +       int err;
3766 +       unsigned int sigen;
3767 +
3768 +       sigen = au_sigen(root->d_sb);
3769 +       DiMustNoWaiters(root);
3770 +       IiMustNoWaiters(d_inode(root));
3771 +       di_write_unlock(root);
3772 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3773 +       if (!err)
3774 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3775 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3776 +
3777 +       return err;
3778 +}
3779 +
3780 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3781 +                        struct file **to_free, int *idx)
3782 +{
3783 +       int err;
3784 +       unsigned char matched, root;
3785 +       aufs_bindex_t bindex, bbot;
3786 +       struct au_fidir *fidir;
3787 +       struct au_hfile *hfile;
3788 +
3789 +       err = 0;
3790 +       root = IS_ROOT(file->f_path.dentry);
3791 +       if (root) {
3792 +               get_file(file);
3793 +               to_free[*idx] = file;
3794 +               (*idx)++;
3795 +               goto out;
3796 +       }
3797 +
3798 +       matched = 0;
3799 +       fidir = au_fi(file)->fi_hdir;
3800 +       AuDebugOn(!fidir);
3801 +       bbot = au_fbbot_dir(file);
3802 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3803 +               hfile = fidir->fd_hfile + bindex;
3804 +               if (!hfile->hf_file)
3805 +                       continue;
3806 +
3807 +               if (hfile->hf_br->br_id == br_id) {
3808 +                       matched = 1;
3809 +                       break;
3810 +               }
3811 +       }
3812 +       if (matched)
3813 +               err = -EBUSY;
3814 +
3815 +out:
3816 +       return err;
3817 +}
3818 +
3819 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3820 +                         struct file **to_free, int opened)
3821 +{
3822 +       int err, idx;
3823 +       unsigned long long ull, max;
3824 +       aufs_bindex_t btop;
3825 +       struct file *file, **array;
3826 +       struct dentry *root;
3827 +       struct au_hfile *hfile;
3828 +
3829 +       array = au_farray_alloc(sb, &max);
3830 +       err = PTR_ERR(array);
3831 +       if (IS_ERR(array))
3832 +               goto out;
3833 +
3834 +       err = 0;
3835 +       idx = 0;
3836 +       root = sb->s_root;
3837 +       di_write_unlock(root);
3838 +       for (ull = 0; ull < max; ull++) {
3839 +               file = array[ull];
3840 +               if (unlikely(!file))
3841 +                       break;
3842 +
3843 +               /* AuDbg("%pD\n", file); */
3844 +               fi_read_lock(file);
3845 +               btop = au_fbtop(file);
3846 +               if (!d_is_dir(file->f_path.dentry)) {
3847 +                       hfile = &au_fi(file)->fi_htop;
3848 +                       if (hfile->hf_br->br_id == br_id)
3849 +                               err = -EBUSY;
3850 +               } else
3851 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3852 +               fi_read_unlock(file);
3853 +               if (unlikely(err))
3854 +                       break;
3855 +       }
3856 +       di_write_lock_child(root);
3857 +       au_farray_free(array, max);
3858 +       AuDebugOn(idx > opened);
3859 +
3860 +out:
3861 +       return err;
3862 +}
3863 +
3864 +static void br_del_file(struct file **to_free, unsigned long long opened,
3865 +                       aufs_bindex_t br_id)
3866 +{
3867 +       unsigned long long ull;
3868 +       aufs_bindex_t bindex, btop, bbot, bfound;
3869 +       struct file *file;
3870 +       struct au_fidir *fidir;
3871 +       struct au_hfile *hfile;
3872 +
3873 +       for (ull = 0; ull < opened; ull++) {
3874 +               file = to_free[ull];
3875 +               if (unlikely(!file))
3876 +                       break;
3877 +
3878 +               /* AuDbg("%pD\n", file); */
3879 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3880 +               bfound = -1;
3881 +               fidir = au_fi(file)->fi_hdir;
3882 +               AuDebugOn(!fidir);
3883 +               fi_write_lock(file);
3884 +               btop = au_fbtop(file);
3885 +               bbot = au_fbbot_dir(file);
3886 +               for (bindex = btop; bindex <= bbot; bindex++) {
3887 +                       hfile = fidir->fd_hfile + bindex;
3888 +                       if (!hfile->hf_file)
3889 +                               continue;
3890 +
3891 +                       if (hfile->hf_br->br_id == br_id) {
3892 +                               bfound = bindex;
3893 +                               break;
3894 +                       }
3895 +               }
3896 +               AuDebugOn(bfound < 0);
3897 +               au_set_h_fptr(file, bfound, NULL);
3898 +               if (bfound == btop) {
3899 +                       for (btop++; btop <= bbot; btop++)
3900 +                               if (au_hf_dir(file, btop)) {
3901 +                                       au_set_fbtop(file, btop);
3902 +                                       break;
3903 +                               }
3904 +               }
3905 +               fi_write_unlock(file);
3906 +       }
3907 +}
3908 +
3909 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3910 +                            const aufs_bindex_t bindex,
3911 +                            const aufs_bindex_t bbot)
3912 +{
3913 +       struct au_branch **brp, **p;
3914 +
3915 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3916 +
3917 +       brp = sbinfo->si_branch + bindex;
3918 +       if (bindex < bbot)
3919 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3920 +       sbinfo->si_branch[0 + bbot] = NULL;
3921 +       sbinfo->si_bbot--;
3922 +
3923 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3924 +                       /*may_shrink*/1);
3925 +       if (p)
3926 +               sbinfo->si_branch = p;
3927 +       /* harmless error */
3928 +}
3929 +
3930 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3931 +                            const aufs_bindex_t bbot)
3932 +{
3933 +       struct au_hdentry *hdp, *p;
3934 +
3935 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3936 +
3937 +       hdp = au_hdentry(dinfo, bindex);
3938 +       if (bindex < bbot)
3939 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3940 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3941 +       dinfo->di_bbot--;
3942 +
3943 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3944 +                       /*may_shrink*/1);
3945 +       if (p)
3946 +               dinfo->di_hdentry = p;
3947 +       /* harmless error */
3948 +}
3949 +
3950 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3951 +                            const aufs_bindex_t bbot)
3952 +{
3953 +       struct au_hinode *hip, *p;
3954 +
3955 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3956 +
3957 +       hip = au_hinode(iinfo, bindex);
3958 +       if (bindex < bbot)
3959 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3960 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3961 +       iinfo->ii_bbot--;
3962 +
3963 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3964 +                       /*may_shrink*/1);
3965 +       if (p)
3966 +               iinfo->ii_hinode = p;
3967 +       /* harmless error */
3968 +}
3969 +
3970 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3971 +                        struct au_branch *br)
3972 +{
3973 +       aufs_bindex_t bbot;
3974 +       struct au_sbinfo *sbinfo;
3975 +       struct dentry *root, *h_root;
3976 +       struct inode *inode, *h_inode;
3977 +       struct au_hinode *hinode;
3978 +
3979 +       SiMustWriteLock(sb);
3980 +
3981 +       root = sb->s_root;
3982 +       inode = d_inode(root);
3983 +       sbinfo = au_sbi(sb);
3984 +       bbot = sbinfo->si_bbot;
3985 +
3986 +       h_root = au_h_dptr(root, bindex);
3987 +       hinode = au_hi(inode, bindex);
3988 +       h_inode = au_igrab(hinode->hi_inode);
3989 +       au_hiput(hinode);
3990 +
3991 +       au_sbilist_lock();
3992 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3993 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3994 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3995 +       au_sbilist_unlock();
3996 +
3997 +       /* ignore an error */
3998 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3999 +
4000 +       dput(h_root);
4001 +       iput(h_inode);
4002 +       au_br_do_free(br);
4003 +}
4004 +
4005 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4006 +                                  unsigned long long max, void *arg)
4007 +{
4008 +       return max;
4009 +}
4010 +
4011 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4012 +{
4013 +       int err, rerr, i;
4014 +       unsigned long long opened;
4015 +       unsigned int mnt_flags;
4016 +       aufs_bindex_t bindex, bbot, br_id;
4017 +       unsigned char do_wh, verbose;
4018 +       struct au_branch *br;
4019 +       struct au_wbr *wbr;
4020 +       struct dentry *root;
4021 +       struct file **to_free;
4022 +
4023 +       err = 0;
4024 +       opened = 0;
4025 +       to_free = NULL;
4026 +       root = sb->s_root;
4027 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4028 +       if (bindex < 0) {
4029 +               if (remount)
4030 +                       goto out; /* success */
4031 +               err = -ENOENT;
4032 +               pr_err("%s no such branch\n", del->pathname);
4033 +               goto out;
4034 +       }
4035 +       AuDbg("bindex b%d\n", bindex);
4036 +
4037 +       err = -EBUSY;
4038 +       mnt_flags = au_mntflags(sb);
4039 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4040 +       bbot = au_sbbot(sb);
4041 +       if (unlikely(!bbot)) {
4042 +               AuVerbose(verbose, "no more branches left\n");
4043 +               goto out;
4044 +       }
4045 +
4046 +       br = au_sbr(sb, bindex);
4047 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4048 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4049 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4050 +               goto out;
4051 +       }
4052 +
4053 +       br_id = br->br_id;
4054 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
4055 +       if (unlikely(opened)) {
4056 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4057 +               err = PTR_ERR(to_free);
4058 +               if (IS_ERR(to_free))
4059 +                       goto out;
4060 +
4061 +               err = test_file_busy(sb, br_id, to_free, opened);
4062 +               if (unlikely(err)) {
4063 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4064 +                       goto out;
4065 +               }
4066 +       }
4067 +
4068 +       wbr = br->br_wbr;
4069 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4070 +       if (do_wh) {
4071 +               /* instead of WbrWhMustWriteLock(wbr) */
4072 +               SiMustWriteLock(sb);
4073 +               for (i = 0; i < AuBrWh_Last; i++) {
4074 +                       dput(wbr->wbr_wh[i]);
4075 +                       wbr->wbr_wh[i] = NULL;
4076 +               }
4077 +       }
4078 +
4079 +       err = test_children_busy(root, bindex, verbose);
4080 +       if (unlikely(err)) {
4081 +               if (do_wh)
4082 +                       goto out_wh;
4083 +               goto out;
4084 +       }
4085 +
4086 +       err = 0;
4087 +       if (to_free) {
4088 +               /*
4089 +                * now we confirmed the branch is deletable.
4090 +                * let's free the remaining opened dirs on the branch.
4091 +                */
4092 +               di_write_unlock(root);
4093 +               br_del_file(to_free, opened, br_id);
4094 +               di_write_lock_child(root);
4095 +       }
4096 +
4097 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4098 +       dbgaufs_xino_del(br);           /* remove one */
4099 +       au_br_do_del(sb, bindex, br);
4100 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4101 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4102 +
4103 +       if (!bindex) {
4104 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4105 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4106 +       } else
4107 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4108 +       if (au_opt_test(mnt_flags, PLINK))
4109 +               au_plink_half_refresh(sb, br_id);
4110 +
4111 +       goto out; /* success */
4112 +
4113 +out_wh:
4114 +       /* revert */
4115 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4116 +       if (rerr)
4117 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4118 +                       del->pathname, rerr);
4119 +out:
4120 +       if (to_free)
4121 +               au_farray_free(to_free, opened);
4122 +       return err;
4123 +}
4124 +
4125 +/* ---------------------------------------------------------------------- */
4126 +
4127 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4128 +{
4129 +       int err;
4130 +       aufs_bindex_t btop, bbot;
4131 +       struct aufs_ibusy ibusy;
4132 +       struct inode *inode, *h_inode;
4133 +
4134 +       err = -EPERM;
4135 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4136 +               goto out;
4137 +
4138 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4139 +       if (!err)
4140 +               /* VERIFY_WRITE */
4141 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4142 +       if (unlikely(err)) {
4143 +               err = -EFAULT;
4144 +               AuTraceErr(err);
4145 +               goto out;
4146 +       }
4147 +
4148 +       err = -EINVAL;
4149 +       si_read_lock(sb, AuLock_FLUSH);
4150 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4151 +               goto out_unlock;
4152 +
4153 +       err = 0;
4154 +       ibusy.h_ino = 0; /* invalid */
4155 +       inode = ilookup(sb, ibusy.ino);
4156 +       if (!inode
4157 +           || inode->i_ino == AUFS_ROOT_INO
4158 +           || au_is_bad_inode(inode))
4159 +               goto out_unlock;
4160 +
4161 +       ii_read_lock_child(inode);
4162 +       btop = au_ibtop(inode);
4163 +       bbot = au_ibbot(inode);
4164 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4165 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4166 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4167 +                       ibusy.h_ino = h_inode->i_ino;
4168 +       }
4169 +       ii_read_unlock(inode);
4170 +       iput(inode);
4171 +
4172 +out_unlock:
4173 +       si_read_unlock(sb);
4174 +       if (!err) {
4175 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4176 +               if (unlikely(err)) {
4177 +                       err = -EFAULT;
4178 +                       AuTraceErr(err);
4179 +               }
4180 +       }
4181 +out:
4182 +       return err;
4183 +}
4184 +
4185 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4186 +{
4187 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4188 +}
4189 +
4190 +#ifdef CONFIG_COMPAT
4191 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4192 +{
4193 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4194 +}
4195 +#endif
4196 +
4197 +/* ---------------------------------------------------------------------- */
4198 +
4199 +/*
4200 + * change a branch permission
4201 + */
4202 +
4203 +static void au_warn_ima(void)
4204 +{
4205 +#ifdef CONFIG_IMA
4206 +       /* since it doesn't support mark_files_ro() */
4207 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4208 +#endif
4209 +}
4210 +
4211 +static int do_need_sigen_inc(int a, int b)
4212 +{
4213 +       return au_br_whable(a) && !au_br_whable(b);
4214 +}
4215 +
4216 +static int need_sigen_inc(int old, int new)
4217 +{
4218 +       return do_need_sigen_inc(old, new)
4219 +               || do_need_sigen_inc(new, old);
4220 +}
4221 +
4222 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4223 +{
4224 +       int err, do_warn;
4225 +       unsigned int mnt_flags;
4226 +       unsigned long long ull, max;
4227 +       aufs_bindex_t br_id;
4228 +       unsigned char verbose, writer;
4229 +       struct file *file, *hf, **array;
4230 +       struct au_hfile *hfile;
4231 +       struct inode *h_inode;
4232 +
4233 +       mnt_flags = au_mntflags(sb);
4234 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4235 +
4236 +       array = au_farray_alloc(sb, &max);
4237 +       err = PTR_ERR(array);
4238 +       if (IS_ERR(array))
4239 +               goto out;
4240 +
4241 +       do_warn = 0;
4242 +       br_id = au_sbr_id(sb, bindex);
4243 +       for (ull = 0; ull < max; ull++) {
4244 +               file = array[ull];
4245 +               if (unlikely(!file))
4246 +                       break;
4247 +
4248 +               /* AuDbg("%pD\n", file); */
4249 +               fi_read_lock(file);
4250 +               if (unlikely(au_test_mmapped(file))) {
4251 +                       err = -EBUSY;
4252 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4253 +                       AuDbgFile(file);
4254 +                       FiMustNoWaiters(file);
4255 +                       fi_read_unlock(file);
4256 +                       goto out_array;
4257 +               }
4258 +
4259 +               hfile = &au_fi(file)->fi_htop;
4260 +               hf = hfile->hf_file;
4261 +               if (!d_is_reg(file->f_path.dentry)
4262 +                   || !(file->f_mode & FMODE_WRITE)
4263 +                   || hfile->hf_br->br_id != br_id
4264 +                   || !(hf->f_mode & FMODE_WRITE))
4265 +                       array[ull] = NULL;
4266 +               else {
4267 +                       do_warn = 1;
4268 +                       get_file(file);
4269 +               }
4270 +
4271 +               FiMustNoWaiters(file);
4272 +               fi_read_unlock(file);
4273 +               fput(file);
4274 +       }
4275 +
4276 +       err = 0;
4277 +       if (do_warn)
4278 +               au_warn_ima();
4279 +
4280 +       for (ull = 0; ull < max; ull++) {
4281 +               file = array[ull];
4282 +               if (!file)
4283 +                       continue;
4284 +
4285 +               /* todo: already flushed? */
4286 +               /*
4287 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4288 +                * approach which resets f_mode and calls mnt_drop_write() and
4289 +                * file_release_write() for each file, because the branch
4290 +                * attribute in aufs world is totally different from the native
4291 +                * fs rw/ro mode.
4292 +               */
4293 +               /* fi_read_lock(file); */
4294 +               hfile = &au_fi(file)->fi_htop;
4295 +               hf = hfile->hf_file;
4296 +               /* fi_read_unlock(file); */
4297 +               spin_lock(&hf->f_lock);
4298 +               writer = !!(hf->f_mode & FMODE_WRITER);
4299 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4300 +               spin_unlock(&hf->f_lock);
4301 +               if (writer) {
4302 +                       h_inode = file_inode(hf);
4303 +                       if (hf->f_mode & FMODE_READ)
4304 +                               i_readcount_inc(h_inode);
4305 +                       put_write_access(h_inode);
4306 +                       __mnt_drop_write(hf->f_path.mnt);
4307 +               }
4308 +       }
4309 +
4310 +out_array:
4311 +       au_farray_free(array, max);
4312 +out:
4313 +       AuTraceErr(err);
4314 +       return err;
4315 +}
4316 +
4317 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4318 +             int *do_refresh)
4319 +{
4320 +       int err, rerr;
4321 +       aufs_bindex_t bindex;
4322 +       struct dentry *root;
4323 +       struct au_branch *br;
4324 +       struct au_br_fhsm *bf;
4325 +
4326 +       root = sb->s_root;
4327 +       bindex = au_find_dbindex(root, mod->h_root);
4328 +       if (bindex < 0) {
4329 +               if (remount)
4330 +                       return 0; /* success */
4331 +               err = -ENOENT;
4332 +               pr_err("%s no such branch\n", mod->path);
4333 +               goto out;
4334 +       }
4335 +       AuDbg("bindex b%d\n", bindex);
4336 +
4337 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4338 +       if (unlikely(err))
4339 +               goto out;
4340 +
4341 +       br = au_sbr(sb, bindex);
4342 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4343 +       if (br->br_perm == mod->perm)
4344 +               return 0; /* success */
4345 +
4346 +       /* pre-allocate for non-fhsm --> fhsm */
4347 +       bf = NULL;
4348 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4349 +               err = au_fhsm_br_alloc(br);
4350 +               if (unlikely(err))
4351 +                       goto out;
4352 +               bf = br->br_fhsm;
4353 +               br->br_fhsm = NULL;
4354 +       }
4355 +
4356 +       if (au_br_writable(br->br_perm)) {
4357 +               /* remove whiteout base */
4358 +               err = au_br_init_wh(sb, br, mod->perm);
4359 +               if (unlikely(err))
4360 +                       goto out_bf;
4361 +
4362 +               if (!au_br_writable(mod->perm)) {
4363 +                       /* rw --> ro, file might be mmapped */
4364 +                       DiMustNoWaiters(root);
4365 +                       IiMustNoWaiters(d_inode(root));
4366 +                       di_write_unlock(root);
4367 +                       err = au_br_mod_files_ro(sb, bindex);
4368 +                       /* aufs_write_lock() calls ..._child() */
4369 +                       di_write_lock_child(root);
4370 +
4371 +                       if (unlikely(err)) {
4372 +                               rerr = -ENOMEM;
4373 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4374 +                                                    GFP_NOFS);
4375 +                               if (br->br_wbr)
4376 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4377 +                               if (unlikely(rerr)) {
4378 +                                       AuIOErr("nested error %d (%d)\n",
4379 +                                               rerr, err);
4380 +                                       br->br_perm = mod->perm;
4381 +                               }
4382 +                       }
4383 +               }
4384 +       } else if (au_br_writable(mod->perm)) {
4385 +               /* ro --> rw */
4386 +               err = -ENOMEM;
4387 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4388 +               if (br->br_wbr) {
4389 +                       err = au_wbr_init(br, sb, mod->perm);
4390 +                       if (unlikely(err)) {
4391 +                               au_kfree_rcu(br->br_wbr);
4392 +                               br->br_wbr = NULL;
4393 +                       }
4394 +               }
4395 +       }
4396 +       if (unlikely(err))
4397 +               goto out_bf;
4398 +
4399 +       if (au_br_fhsm(br->br_perm)) {
4400 +               if (!au_br_fhsm(mod->perm)) {
4401 +                       /* fhsm --> non-fhsm */
4402 +                       au_br_fhsm_fin(br->br_fhsm);
4403 +                       au_kfree_rcu(br->br_fhsm);
4404 +                       br->br_fhsm = NULL;
4405 +               }
4406 +       } else if (au_br_fhsm(mod->perm))
4407 +               /* non-fhsm --> fhsm */
4408 +               br->br_fhsm = bf;
4409 +
4410 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4411 +       br->br_perm = mod->perm;
4412 +       goto out; /* success */
4413 +
4414 +out_bf:
4415 +       au_kfree_try_rcu(bf);
4416 +out:
4417 +       AuTraceErr(err);
4418 +       return err;
4419 +}
4420 +
4421 +/* ---------------------------------------------------------------------- */
4422 +
4423 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4424 +{
4425 +       int err;
4426 +       struct kstatfs kstfs;
4427 +
4428 +       err = vfs_statfs(&br->br_path, &kstfs);
4429 +       if (!err) {
4430 +               stfs->f_blocks = kstfs.f_blocks;
4431 +               stfs->f_bavail = kstfs.f_bavail;
4432 +               stfs->f_files = kstfs.f_files;
4433 +               stfs->f_ffree = kstfs.f_ffree;
4434 +       }
4435 +
4436 +       return err;
4437 +}
4438 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4439 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4440 +++ linux/fs/aufs/branch.h      2020-01-27 10:57:18.165538015 +0100
4441 @@ -0,0 +1,366 @@
4442 +/* SPDX-License-Identifier: GPL-2.0 */
4443 +/*
4444 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4445 + *
4446 + * This program, aufs is free software; you can redistribute it and/or modify
4447 + * it under the terms of the GNU General Public License as published by
4448 + * the Free Software Foundation; either version 2 of the License, or
4449 + * (at your option) any later version.
4450 + *
4451 + * This program is distributed in the hope that it will be useful,
4452 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4453 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4454 + * GNU General Public License for more details.
4455 + *
4456 + * You should have received a copy of the GNU General Public License
4457 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4458 + */
4459 +
4460 +/*
4461 + * branch filesystems and xino for them
4462 + */
4463 +
4464 +#ifndef __AUFS_BRANCH_H__
4465 +#define __AUFS_BRANCH_H__
4466 +
4467 +#ifdef __KERNEL__
4468 +
4469 +#include <linux/mount.h>
4470 +#include "dirren.h"
4471 +#include "dynop.h"
4472 +#include "lcnt.h"
4473 +#include "rwsem.h"
4474 +#include "super.h"
4475 +
4476 +/* ---------------------------------------------------------------------- */
4477 +
4478 +/* a xino file */
4479 +struct au_xino {
4480 +       struct file             **xi_file;
4481 +       unsigned int            xi_nfile;
4482 +
4483 +       struct {
4484 +               spinlock_t              spin;
4485 +               ino_t                   *array;
4486 +               int                     total;
4487 +               /* reserved for future use */
4488 +               /* unsigned long        *bitmap; */
4489 +               wait_queue_head_t       wqh;
4490 +       } xi_nondir;
4491 +
4492 +       struct mutex            xi_mtx; /* protects xi_file array */
4493 +       struct hlist_bl_head    xi_writing;
4494 +
4495 +       atomic_t                xi_truncating;
4496 +
4497 +       struct kref             xi_kref;
4498 +};
4499 +
4500 +/* File-based Hierarchical Storage Management */
4501 +struct au_br_fhsm {
4502 +#ifdef CONFIG_AUFS_FHSM
4503 +       struct mutex            bf_lock;
4504 +       unsigned long           bf_jiffy;
4505 +       struct aufs_stfs        bf_stfs;
4506 +       int                     bf_readable;
4507 +#endif
4508 +};
4509 +
4510 +/* members for writable branch only */
4511 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4512 +struct au_wbr {
4513 +       struct au_rwsem         wbr_wh_rwsem;
4514 +       struct dentry           *wbr_wh[AuBrWh_Last];
4515 +       atomic_t                wbr_wh_running;
4516 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4517 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4518 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4519 +
4520 +       /* mfs mode */
4521 +       unsigned long long      wbr_bytes;
4522 +};
4523 +
4524 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4525 +#define AuBrDynOp (AuDyLast * 4)
4526 +
4527 +#ifdef CONFIG_AUFS_HFSNOTIFY
4528 +/* support for asynchronous destruction */
4529 +struct au_br_hfsnotify {
4530 +       struct fsnotify_group   *hfsn_group;
4531 +};
4532 +#endif
4533 +
4534 +/* sysfs entries */
4535 +struct au_brsysfs {
4536 +       char                    name[16];
4537 +       struct attribute        attr;
4538 +};
4539 +
4540 +enum {
4541 +       AuBrSysfs_BR,
4542 +       AuBrSysfs_BRID,
4543 +       AuBrSysfs_Last
4544 +};
4545 +
4546 +/* protected by superblock rwsem */
4547 +struct au_branch {
4548 +       struct au_xino          *br_xino;
4549 +
4550 +       aufs_bindex_t           br_id;
4551 +
4552 +       int                     br_perm;
4553 +       struct path             br_path;
4554 +       spinlock_t              br_dykey_lock;
4555 +       struct au_dykey         *br_dykey[AuBrDynOp];
4556 +       au_lcnt_t               br_nfiles;      /* opened files */
4557 +       au_lcnt_t               br_count;       /* in-use for other */
4558 +
4559 +       struct au_wbr           *br_wbr;
4560 +       struct au_br_fhsm       *br_fhsm;
4561 +
4562 +#ifdef CONFIG_AUFS_HFSNOTIFY
4563 +       struct au_br_hfsnotify  *br_hfsn;
4564 +#endif
4565 +
4566 +#ifdef CONFIG_SYSFS
4567 +       /* entries under sysfs per mount-point */
4568 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4569 +#endif
4570 +
4571 +#ifdef CONFIG_DEBUG_FS
4572 +       struct dentry            *br_dbgaufs; /* xino */
4573 +#endif
4574 +
4575 +       struct au_dr_br         br_dirren;
4576 +};
4577 +
4578 +/* ---------------------------------------------------------------------- */
4579 +
4580 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4581 +{
4582 +       return br->br_path.mnt;
4583 +}
4584 +
4585 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4586 +{
4587 +       return br->br_path.dentry;
4588 +}
4589 +
4590 +static inline struct super_block *au_br_sb(struct au_branch *br)
4591 +{
4592 +       return au_br_mnt(br)->mnt_sb;
4593 +}
4594 +
4595 +static inline int au_br_rdonly(struct au_branch *br)
4596 +{
4597 +       return (sb_rdonly(au_br_sb(br))
4598 +               || !au_br_writable(br->br_perm))
4599 +               ? -EROFS : 0;
4600 +}
4601 +
4602 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4603 +{
4604 +#ifdef CONFIG_AUFS_HNOTIFY
4605 +       return !(brperm & AuBrPerm_RR);
4606 +#else
4607 +       return 0;
4608 +#endif
4609 +}
4610 +
4611 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4612 +{
4613 +       int err, exec_flag;
4614 +
4615 +       err = 0;
4616 +       exec_flag = oflag & __FMODE_EXEC;
4617 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4618 +               err = -EACCES;
4619 +
4620 +       return err;
4621 +}
4622 +
4623 +static inline void au_xino_get(struct au_branch *br)
4624 +{
4625 +       struct au_xino *xi;
4626 +
4627 +       xi = br->br_xino;
4628 +       if (xi)
4629 +               kref_get(&xi->xi_kref);
4630 +}
4631 +
4632 +static inline int au_xino_count(struct au_branch *br)
4633 +{
4634 +       int v;
4635 +       struct au_xino *xi;
4636 +
4637 +       v = 0;
4638 +       xi = br->br_xino;
4639 +       if (xi)
4640 +               v = kref_read(&xi->xi_kref);
4641 +
4642 +       return v;
4643 +}
4644 +
4645 +/* ---------------------------------------------------------------------- */
4646 +
4647 +/* branch.c */
4648 +struct au_sbinfo;
4649 +void au_br_free(struct au_sbinfo *sinfo);
4650 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4651 +struct au_opt_add;
4652 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4653 +struct au_opt_del;
4654 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4655 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4656 +#ifdef CONFIG_COMPAT
4657 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4658 +#endif
4659 +struct au_opt_mod;
4660 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4661 +             int *do_refresh);
4662 +struct aufs_stfs;
4663 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4664 +
4665 +/* xino.c */
4666 +static const loff_t au_loff_max = LLONG_MAX;
4667 +
4668 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4669 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4670 +                           int wbrtop);
4671 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4672 +                            struct file *copy_src);
4673 +struct au_xi_new {
4674 +       struct au_xino *xi;     /* switch between xino and xigen */
4675 +       int idx;
4676 +       struct path *base;
4677 +       struct file *copy_src;
4678 +};
4679 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4680 +
4681 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4682 +                ino_t *ino);
4683 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4684 +                 ino_t ino);
4685 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4686 +                  loff_t *pos);
4687 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4688 +                   size_t size, loff_t *pos);
4689 +
4690 +int au_xib_trunc(struct super_block *sb);
4691 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4692 +
4693 +struct au_xino *au_xino_alloc(unsigned int nfile);
4694 +int au_xino_put(struct au_branch *br);
4695 +struct file *au_xino_file1(struct au_xino *xi);
4696 +
4697 +struct au_opt_xino;
4698 +void au_xino_clr(struct super_block *sb);
4699 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4700 +struct file *au_xino_def(struct super_block *sb);
4701 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4702 +                   struct path *base);
4703 +
4704 +ino_t au_xino_new_ino(struct super_block *sb);
4705 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4706 +
4707 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4708 +                      ino_t h_ino, int idx);
4709 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4710 +                     int *idx);
4711 +
4712 +int au_xino_path(struct seq_file *seq, struct file *file);
4713 +
4714 +/* ---------------------------------------------------------------------- */
4715 +
4716 +/* @idx is signed to accept -1 meaning the first file */
4717 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4718 +{
4719 +       struct file *file;
4720 +
4721 +       file = NULL;
4722 +       if (!xi)
4723 +               goto out;
4724 +
4725 +       if (idx >= 0) {
4726 +               if (idx < xi->xi_nfile)
4727 +                       file = xi->xi_file[idx];
4728 +       } else
4729 +               file = au_xino_file1(xi);
4730 +
4731 +out:
4732 +       return file;
4733 +}
4734 +
4735 +/* ---------------------------------------------------------------------- */
4736 +
4737 +/* Superblock to branch */
4738 +static inline
4739 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4740 +{
4741 +       return au_sbr(sb, bindex)->br_id;
4742 +}
4743 +
4744 +static inline
4745 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4746 +{
4747 +       return au_br_mnt(au_sbr(sb, bindex));
4748 +}
4749 +
4750 +static inline
4751 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4752 +{
4753 +       return au_br_sb(au_sbr(sb, bindex));
4754 +}
4755 +
4756 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4757 +{
4758 +       return au_sbr(sb, bindex)->br_perm;
4759 +}
4760 +
4761 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4762 +{
4763 +       return au_br_whable(au_sbr_perm(sb, bindex));
4764 +}
4765 +
4766 +/* ---------------------------------------------------------------------- */
4767 +
4768 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4769 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4770 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4771 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4772 +/*
4773 +#define wbr_wh_read_trylock_nested(wbr) \
4774 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4775 +#define wbr_wh_write_trylock_nested(wbr) \
4776 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4777 +*/
4778 +
4779 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4780 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4781 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4782 +
4783 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4784 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4785 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4786 +
4787 +/* ---------------------------------------------------------------------- */
4788 +
4789 +#ifdef CONFIG_AUFS_FHSM
4790 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4791 +{
4792 +       mutex_init(&brfhsm->bf_lock);
4793 +       brfhsm->bf_jiffy = 0;
4794 +       brfhsm->bf_readable = 0;
4795 +}
4796 +
4797 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4798 +{
4799 +       mutex_destroy(&brfhsm->bf_lock);
4800 +}
4801 +#else
4802 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4803 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4804 +#endif
4805 +
4806 +#endif /* __KERNEL__ */
4807 +#endif /* __AUFS_BRANCH_H__ */
4808 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4809 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4810 +++ linux/fs/aufs/conf.mk       2019-07-11 15:42:14.462237786 +0200
4811 @@ -0,0 +1,40 @@
4812 +# SPDX-License-Identifier: GPL-2.0
4813 +
4814 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4815 +
4816 +define AuConf
4817 +ifdef ${1}
4818 +AuConfStr += ${1}=${${1}}
4819 +endif
4820 +endef
4821 +
4822 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4823 +       SBILIST \
4824 +       HNOTIFY HFSNOTIFY \
4825 +       EXPORT INO_T_64 \
4826 +       XATTR \
4827 +       FHSM \
4828 +       RDU \
4829 +       DIRREN \
4830 +       SHWH \
4831 +       BR_RAMFS \
4832 +       BR_FUSE POLL \
4833 +       BR_HFSPLUS \
4834 +       BDEV_LOOP \
4835 +       DEBUG MAGIC_SYSRQ
4836 +$(foreach i, ${AuConfAll}, \
4837 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4838 +
4839 +AuConfName = ${obj}/conf.str
4840 +${AuConfName}.tmp: FORCE
4841 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4842 +${AuConfName}: ${AuConfName}.tmp
4843 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4844 +       echo '  GEN    ' $@; \
4845 +       cp -p $< $@; \
4846 +       }
4847 +FORCE:
4848 +clean-files += ${AuConfName} ${AuConfName}.tmp
4849 +${obj}/sysfs.o: ${AuConfName}
4850 +
4851 +-include ${srctree}/${src}/conf_priv.mk
4852 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4853 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4854 +++ linux/fs/aufs/cpup.c        2020-01-27 10:57:18.165538015 +0100
4855 @@ -0,0 +1,1458 @@
4856 +// SPDX-License-Identifier: GPL-2.0
4857 +/*
4858 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4859 + *
4860 + * This program, aufs is free software; you can redistribute it and/or modify
4861 + * it under the terms of the GNU General Public License as published by
4862 + * the Free Software Foundation; either version 2 of the License, or
4863 + * (at your option) any later version.
4864 + *
4865 + * This program is distributed in the hope that it will be useful,
4866 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4867 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4868 + * GNU General Public License for more details.
4869 + *
4870 + * You should have received a copy of the GNU General Public License
4871 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4872 + */
4873 +
4874 +/*
4875 + * copy-up functions, see wbr_policy.c for copy-down
4876 + */
4877 +
4878 +#include <linux/fs_stack.h>
4879 +#include <linux/mm.h>
4880 +#include <linux/task_work.h>
4881 +#include "aufs.h"
4882 +
4883 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4884 +{
4885 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4886 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4887 +
4888 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4889 +
4890 +       dst->i_flags |= iflags & ~mask;
4891 +       if (au_test_fs_notime(dst->i_sb))
4892 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4893 +}
4894 +
4895 +void au_cpup_attr_timesizes(struct inode *inode)
4896 +{
4897 +       struct inode *h_inode;
4898 +
4899 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4900 +       fsstack_copy_attr_times(inode, h_inode);
4901 +       fsstack_copy_inode_size(inode, h_inode);
4902 +}
4903 +
4904 +void au_cpup_attr_nlink(struct inode *inode, int force)
4905 +{
4906 +       struct inode *h_inode;
4907 +       struct super_block *sb;
4908 +       aufs_bindex_t bindex, bbot;
4909 +
4910 +       sb = inode->i_sb;
4911 +       bindex = au_ibtop(inode);
4912 +       h_inode = au_h_iptr(inode, bindex);
4913 +       if (!force
4914 +           && !S_ISDIR(h_inode->i_mode)
4915 +           && au_opt_test(au_mntflags(sb), PLINK)
4916 +           && au_plink_test(inode))
4917 +               return;
4918 +
4919 +       /*
4920 +        * 0 can happen in revalidating.
4921 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4922 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4923 +        * case.
4924 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4925 +        *       the incorrect link count.
4926 +        */
4927 +       set_nlink(inode, h_inode->i_nlink);
4928 +
4929 +       /*
4930 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4931 +        * it may includes whplink directory.
4932 +        */
4933 +       if (S_ISDIR(h_inode->i_mode)) {
4934 +               bbot = au_ibbot(inode);
4935 +               for (bindex++; bindex <= bbot; bindex++) {
4936 +                       h_inode = au_h_iptr(inode, bindex);
4937 +                       if (h_inode)
4938 +                               au_add_nlink(inode, h_inode);
4939 +               }
4940 +       }
4941 +}
4942 +
4943 +void au_cpup_attr_changeable(struct inode *inode)
4944 +{
4945 +       struct inode *h_inode;
4946 +
4947 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4948 +       inode->i_mode = h_inode->i_mode;
4949 +       inode->i_uid = h_inode->i_uid;
4950 +       inode->i_gid = h_inode->i_gid;
4951 +       au_cpup_attr_timesizes(inode);
4952 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4953 +}
4954 +
4955 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4956 +{
4957 +       struct au_iinfo *iinfo = au_ii(inode);
4958 +
4959 +       IiMustWriteLock(inode);
4960 +
4961 +       iinfo->ii_higen = h_inode->i_generation;
4962 +       iinfo->ii_hsb1 = h_inode->i_sb;
4963 +}
4964 +
4965 +void au_cpup_attr_all(struct inode *inode, int force)
4966 +{
4967 +       struct inode *h_inode;
4968 +
4969 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4970 +       au_cpup_attr_changeable(inode);
4971 +       if (inode->i_nlink > 0)
4972 +               au_cpup_attr_nlink(inode, force);
4973 +       inode->i_rdev = h_inode->i_rdev;
4974 +       inode->i_blkbits = h_inode->i_blkbits;
4975 +       au_cpup_igen(inode, h_inode);
4976 +}
4977 +
4978 +/* ---------------------------------------------------------------------- */
4979 +
4980 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4981 +
4982 +/* keep the timestamps of the parent dir when cpup */
4983 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4984 +                   struct path *h_path)
4985 +{
4986 +       struct inode *h_inode;
4987 +
4988 +       dt->dt_dentry = dentry;
4989 +       dt->dt_h_path = *h_path;
4990 +       h_inode = d_inode(h_path->dentry);
4991 +       dt->dt_atime = h_inode->i_atime;
4992 +       dt->dt_mtime = h_inode->i_mtime;
4993 +       /* smp_mb(); */
4994 +}
4995 +
4996 +void au_dtime_revert(struct au_dtime *dt)
4997 +{
4998 +       struct iattr attr;
4999 +       int err;
5000 +
5001 +       attr.ia_atime = dt->dt_atime;
5002 +       attr.ia_mtime = dt->dt_mtime;
5003 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5004 +               | ATTR_ATIME | ATTR_ATIME_SET;
5005 +
5006 +       /* no delegation since this is a directory */
5007 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5008 +       if (unlikely(err))
5009 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5010 +}
5011 +
5012 +/* ---------------------------------------------------------------------- */
5013 +
5014 +/* internal use only */
5015 +struct au_cpup_reg_attr {
5016 +       int             valid;
5017 +       struct kstat    st;
5018 +       unsigned int    iflags; /* inode->i_flags */
5019 +};
5020 +
5021 +static noinline_for_stack
5022 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5023 +              struct au_cpup_reg_attr *h_src_attr)
5024 +{
5025 +       int err, sbits, icex;
5026 +       unsigned int mnt_flags;
5027 +       unsigned char verbose;
5028 +       struct iattr ia;
5029 +       struct path h_path;
5030 +       struct inode *h_isrc, *h_idst;
5031 +       struct kstat *h_st;
5032 +       struct au_branch *br;
5033 +
5034 +       h_path.dentry = au_h_dptr(dst, bindex);
5035 +       h_idst = d_inode(h_path.dentry);
5036 +       br = au_sbr(dst->d_sb, bindex);
5037 +       h_path.mnt = au_br_mnt(br);
5038 +       h_isrc = d_inode(h_src);
5039 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5040 +               | ATTR_ATIME | ATTR_MTIME
5041 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5042 +       if (h_src_attr && h_src_attr->valid) {
5043 +               h_st = &h_src_attr->st;
5044 +               ia.ia_uid = h_st->uid;
5045 +               ia.ia_gid = h_st->gid;
5046 +               ia.ia_atime = h_st->atime;
5047 +               ia.ia_mtime = h_st->mtime;
5048 +               if (h_idst->i_mode != h_st->mode
5049 +                   && !S_ISLNK(h_idst->i_mode)) {
5050 +                       ia.ia_valid |= ATTR_MODE;
5051 +                       ia.ia_mode = h_st->mode;
5052 +               }
5053 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5054 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5055 +       } else {
5056 +               ia.ia_uid = h_isrc->i_uid;
5057 +               ia.ia_gid = h_isrc->i_gid;
5058 +               ia.ia_atime = h_isrc->i_atime;
5059 +               ia.ia_mtime = h_isrc->i_mtime;
5060 +               if (h_idst->i_mode != h_isrc->i_mode
5061 +                   && !S_ISLNK(h_idst->i_mode)) {
5062 +                       ia.ia_valid |= ATTR_MODE;
5063 +                       ia.ia_mode = h_isrc->i_mode;
5064 +               }
5065 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5066 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5067 +       }
5068 +       /* no delegation since it is just created */
5069 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5070 +
5071 +       /* is this nfs only? */
5072 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5073 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5074 +               ia.ia_mode = h_isrc->i_mode;
5075 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5076 +       }
5077 +
5078 +       icex = br->br_perm & AuBrAttr_ICEX;
5079 +       if (!err) {
5080 +               mnt_flags = au_mntflags(dst->d_sb);
5081 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5082 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5083 +       }
5084 +
5085 +       return err;
5086 +}
5087 +
5088 +/* ---------------------------------------------------------------------- */
5089 +
5090 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5091 +                          char *buf, unsigned long blksize)
5092 +{
5093 +       int err;
5094 +       size_t sz, rbytes, wbytes;
5095 +       unsigned char all_zero;
5096 +       char *p, *zp;
5097 +       struct inode *h_inode;
5098 +       /* reduce stack usage */
5099 +       struct iattr *ia;
5100 +
5101 +       zp = page_address(ZERO_PAGE(0));
5102 +       if (unlikely(!zp))
5103 +               return -ENOMEM; /* possible? */
5104 +
5105 +       err = 0;
5106 +       all_zero = 0;
5107 +       while (len) {
5108 +               AuDbg("len %lld\n", len);
5109 +               sz = blksize;
5110 +               if (len < blksize)
5111 +                       sz = len;
5112 +
5113 +               rbytes = 0;
5114 +               /* todo: signal_pending? */
5115 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5116 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5117 +                       err = rbytes;
5118 +               }
5119 +               if (unlikely(err < 0))
5120 +                       break;
5121 +
5122 +               all_zero = 0;
5123 +               if (len >= rbytes && rbytes == blksize)
5124 +                       all_zero = !memcmp(buf, zp, rbytes);
5125 +               if (!all_zero) {
5126 +                       wbytes = rbytes;
5127 +                       p = buf;
5128 +                       while (wbytes) {
5129 +                               size_t b;
5130 +
5131 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5132 +                               err = b;
5133 +                               /* todo: signal_pending? */
5134 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5135 +                                       continue;
5136 +                               if (unlikely(err < 0))
5137 +                                       break;
5138 +                               wbytes -= b;
5139 +                               p += b;
5140 +                       }
5141 +                       if (unlikely(err < 0))
5142 +                               break;
5143 +               } else {
5144 +                       loff_t res;
5145 +
5146 +                       AuLabel(hole);
5147 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5148 +                       err = res;
5149 +                       if (unlikely(res < 0))
5150 +                               break;
5151 +               }
5152 +               len -= rbytes;
5153 +               err = 0;
5154 +       }
5155 +
5156 +       /* the last block may be a hole */
5157 +       if (!err && all_zero) {
5158 +               AuLabel(last hole);
5159 +
5160 +               err = 1;
5161 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5162 +                       /* nfs requires this step to make last hole */
5163 +                       /* is this only nfs? */
5164 +                       do {
5165 +                               /* todo: signal_pending? */
5166 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5167 +                       } while (err == -EAGAIN || err == -EINTR);
5168 +                       if (err == 1)
5169 +                               dst->f_pos--;
5170 +               }
5171 +
5172 +               if (err == 1) {
5173 +                       ia = (void *)buf;
5174 +                       ia->ia_size = dst->f_pos;
5175 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5176 +                       ia->ia_file = dst;
5177 +                       h_inode = file_inode(dst);
5178 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5179 +                       /* no delegation since it is just created */
5180 +                       err = vfsub_notify_change(&dst->f_path, ia,
5181 +                                                 /*delegated*/NULL);
5182 +                       inode_unlock(h_inode);
5183 +               }
5184 +       }
5185 +
5186 +       return err;
5187 +}
5188 +
5189 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5190 +{
5191 +       int err;
5192 +       unsigned long blksize;
5193 +       unsigned char do_kfree;
5194 +       char *buf;
5195 +       struct super_block *h_sb;
5196 +
5197 +       err = -ENOMEM;
5198 +       h_sb = file_inode(dst)->i_sb;
5199 +       blksize = h_sb->s_blocksize;
5200 +       if (!blksize || PAGE_SIZE < blksize)
5201 +               blksize = PAGE_SIZE;
5202 +       AuDbg("blksize %lu\n", blksize);
5203 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5204 +       if (do_kfree)
5205 +               buf = kmalloc(blksize, GFP_NOFS);
5206 +       else
5207 +               buf = (void *)__get_free_page(GFP_NOFS);
5208 +       if (unlikely(!buf))
5209 +               goto out;
5210 +
5211 +       if (len > (1 << 22))
5212 +               AuDbg("copying a large file %lld\n", (long long)len);
5213 +
5214 +       src->f_pos = 0;
5215 +       dst->f_pos = 0;
5216 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5217 +       if (do_kfree) {
5218 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5219 +               au_kfree_do_rcu(buf);
5220 +       } else
5221 +               free_page((unsigned long)buf);
5222 +
5223 +out:
5224 +       return err;
5225 +}
5226 +
5227 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5228 +{
5229 +       int err;
5230 +       struct super_block *h_src_sb;
5231 +       struct inode *h_src_inode;
5232 +
5233 +       h_src_inode = file_inode(src);
5234 +       h_src_sb = h_src_inode->i_sb;
5235 +
5236 +       /* XFS acquires inode_lock */
5237 +       if (!au_test_xfs(h_src_sb))
5238 +               err = au_copy_file(dst, src, len);
5239 +       else {
5240 +               inode_unlock_shared(h_src_inode);
5241 +               err = au_copy_file(dst, src, len);
5242 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5243 +       }
5244 +
5245 +       return err;
5246 +}
5247 +
5248 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5249 +{
5250 +       int err;
5251 +       loff_t lo;
5252 +       struct super_block *h_src_sb;
5253 +       struct inode *h_src_inode;
5254 +
5255 +       h_src_inode = file_inode(src);
5256 +       h_src_sb = h_src_inode->i_sb;
5257 +       if (h_src_sb != file_inode(dst)->i_sb
5258 +           || !dst->f_op->remap_file_range) {
5259 +               err = au_do_copy(dst, src, len);
5260 +               goto out;
5261 +       }
5262 +
5263 +       if (!au_test_nfs(h_src_sb)) {
5264 +               inode_unlock_shared(h_src_inode);
5265 +               lo = vfsub_clone_file_range(src, dst, len);
5266 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5267 +       } else
5268 +               lo = vfsub_clone_file_range(src, dst, len);
5269 +       if (lo == len) {
5270 +               err = 0;
5271 +               goto out; /* success */
5272 +       } else if (lo >= 0)
5273 +               /* todo: possible? */
5274 +               /* paritially succeeded */
5275 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5276 +       else if (lo != -EOPNOTSUPP) {
5277 +               /* older XFS has a condition in cloning */
5278 +               err = lo;
5279 +               goto out;
5280 +       }
5281 +
5282 +       /* the backend fs on NFS may not support cloning */
5283 +       err = au_do_copy(dst, src, len);
5284 +
5285 +out:
5286 +       AuTraceErr(err);
5287 +       return err;
5288 +}
5289 +
5290 +/*
5291 + * to support a sparse file which is opened with O_APPEND,
5292 + * we need to close the file.
5293 + */
5294 +static int au_cp_regular(struct au_cp_generic *cpg)
5295 +{
5296 +       int err, i;
5297 +       enum { SRC, DST };
5298 +       struct {
5299 +               aufs_bindex_t bindex;
5300 +               unsigned int flags;
5301 +               struct dentry *dentry;
5302 +               int force_wr;
5303 +               struct file *file;
5304 +       } *f, file[] = {
5305 +               {
5306 +                       .bindex = cpg->bsrc,
5307 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5308 +               },
5309 +               {
5310 +                       .bindex = cpg->bdst,
5311 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5312 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5313 +               }
5314 +       };
5315 +       struct au_branch *br;
5316 +       struct super_block *sb, *h_src_sb;
5317 +       struct inode *h_src_inode;
5318 +       struct task_struct *tsk = current;
5319 +
5320 +       /* bsrc branch can be ro/rw. */
5321 +       sb = cpg->dentry->d_sb;
5322 +       f = file;
5323 +       for (i = 0; i < 2; i++, f++) {
5324 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5325 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5326 +                                   /*file*/NULL, f->force_wr);
5327 +               if (IS_ERR(f->file)) {
5328 +                       err = PTR_ERR(f->file);
5329 +                       if (i == SRC)
5330 +                               goto out;
5331 +                       else
5332 +                               goto out_src;
5333 +               }
5334 +       }
5335 +
5336 +       /* try stopping to update while we copyup */
5337 +       h_src_inode = d_inode(file[SRC].dentry);
5338 +       h_src_sb = h_src_inode->i_sb;
5339 +       if (!au_test_nfs(h_src_sb))
5340 +               IMustLock(h_src_inode);
5341 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5342 +
5343 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5344 +       if (tsk->flags & PF_KTHREAD)
5345 +               __fput_sync(file[DST].file);
5346 +       else {
5347 +               /* it happened actually */
5348 +               fput(file[DST].file);
5349 +               /*
5350 +                * too bad.
5351 +                * we have to call both since we don't know which place the file
5352 +                * was added to.
5353 +                */
5354 +               task_work_run();
5355 +               flush_delayed_fput();
5356 +       }
5357 +       br = au_sbr(sb, file[DST].bindex);
5358 +       au_lcnt_dec(&br->br_nfiles);
5359 +
5360 +out_src:
5361 +       fput(file[SRC].file);
5362 +       br = au_sbr(sb, file[SRC].bindex);
5363 +       au_lcnt_dec(&br->br_nfiles);
5364 +out:
5365 +       return err;
5366 +}
5367 +
5368 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5369 +                             struct au_cpup_reg_attr *h_src_attr)
5370 +{
5371 +       int err, rerr;
5372 +       loff_t l;
5373 +       struct path h_path;
5374 +       struct inode *h_src_inode, *h_dst_inode;
5375 +
5376 +       err = 0;
5377 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5378 +       l = i_size_read(h_src_inode);
5379 +       if (cpg->len == -1 || l < cpg->len)
5380 +               cpg->len = l;
5381 +       if (cpg->len) {
5382 +               /* try stopping to update while we are referencing */
5383 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5384 +               au_pin_hdir_unlock(cpg->pin);
5385 +
5386 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5387 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5388 +               h_src_attr->iflags = h_src_inode->i_flags;
5389 +               if (!au_test_nfs(h_src_inode->i_sb))
5390 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5391 +               else {
5392 +                       inode_unlock_shared(h_src_inode);
5393 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5394 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5395 +               }
5396 +               if (unlikely(err)) {
5397 +                       inode_unlock_shared(h_src_inode);
5398 +                       goto out;
5399 +               }
5400 +               h_src_attr->valid = 1;
5401 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5402 +                       err = au_cp_regular(cpg);
5403 +                       inode_unlock_shared(h_src_inode);
5404 +               } else {
5405 +                       inode_unlock_shared(h_src_inode);
5406 +                       err = au_cp_regular(cpg);
5407 +               }
5408 +               rerr = au_pin_hdir_relock(cpg->pin);
5409 +               if (!err && rerr)
5410 +                       err = rerr;
5411 +       }
5412 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5413 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5414 +               h_dst_inode = d_inode(h_path.dentry);
5415 +               spin_lock(&h_dst_inode->i_lock);
5416 +               h_dst_inode->i_state |= I_LINKABLE;
5417 +               spin_unlock(&h_dst_inode->i_lock);
5418 +       }
5419 +
5420 +out:
5421 +       return err;
5422 +}
5423 +
5424 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5425 +                             struct inode *h_dir)
5426 +{
5427 +       int err, symlen;
5428 +       mm_segment_t old_fs;
5429 +       union {
5430 +               char *k;
5431 +               char __user *u;
5432 +       } sym;
5433 +
5434 +       err = -ENOMEM;
5435 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5436 +       if (unlikely(!sym.k))
5437 +               goto out;
5438 +
5439 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5440 +       old_fs = get_fs();
5441 +       set_fs(KERNEL_DS);
5442 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5443 +       err = symlen;
5444 +       set_fs(old_fs);
5445 +
5446 +       if (symlen > 0) {
5447 +               sym.k[symlen] = 0;
5448 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5449 +       }
5450 +       free_page((unsigned long)sym.k);
5451 +
5452 +out:
5453 +       return err;
5454 +}
5455 +
5456 +/*
5457 + * regardless 'acl' option, reset all ACL.
5458 + * All ACL will be copied up later from the original entry on the lower branch.
5459 + */
5460 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5461 +{
5462 +       int err;
5463 +       struct dentry *h_dentry;
5464 +       struct inode *h_inode;
5465 +
5466 +       h_dentry = h_path->dentry;
5467 +       h_inode = d_inode(h_dentry);
5468 +       /* forget_all_cached_acls(h_inode)); */
5469 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5470 +       AuTraceErr(err);
5471 +       if (err == -EOPNOTSUPP)
5472 +               err = 0;
5473 +       if (!err)
5474 +               err = vfsub_acl_chmod(h_inode, mode);
5475 +
5476 +       AuTraceErr(err);
5477 +       return err;
5478 +}
5479 +
5480 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5481 +                         struct inode *h_dir, struct path *h_path)
5482 +{
5483 +       int err;
5484 +       struct inode *dir, *inode;
5485 +
5486 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5487 +       AuTraceErr(err);
5488 +       if (err == -EOPNOTSUPP)
5489 +               err = 0;
5490 +       if (unlikely(err))
5491 +               goto out;
5492 +
5493 +       /*
5494 +        * strange behaviour from the users view,
5495 +        * particularly setattr case
5496 +        */
5497 +       dir = d_inode(dst_parent);
5498 +       if (au_ibtop(dir) == cpg->bdst)
5499 +               au_cpup_attr_nlink(dir, /*force*/1);
5500 +       inode = d_inode(cpg->dentry);
5501 +       au_cpup_attr_nlink(inode, /*force*/1);
5502 +
5503 +out:
5504 +       return err;
5505 +}
5506 +
5507 +static noinline_for_stack
5508 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5509 +              struct au_cpup_reg_attr *h_src_attr)
5510 +{
5511 +       int err;
5512 +       umode_t mode;
5513 +       unsigned int mnt_flags;
5514 +       unsigned char isdir, isreg, force;
5515 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5516 +       struct au_dtime dt;
5517 +       struct path h_path;
5518 +       struct dentry *h_src, *h_dst, *h_parent;
5519 +       struct inode *h_inode, *h_dir;
5520 +       struct super_block *sb;
5521 +
5522 +       /* bsrc branch can be ro/rw. */
5523 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5524 +       h_inode = d_inode(h_src);
5525 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5526 +
5527 +       /* try stopping to be referenced while we are creating */
5528 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5529 +       if (au_ftest_cpup(cpg->flags, RENAME))
5530 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5531 +                                 AUFS_WH_PFX_LEN));
5532 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5533 +       h_dir = d_inode(h_parent);
5534 +       IMustLock(h_dir);
5535 +       AuDebugOn(h_parent != h_dst->d_parent);
5536 +
5537 +       sb = cpg->dentry->d_sb;
5538 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5539 +       if (do_dt) {
5540 +               h_path.dentry = h_parent;
5541 +               au_dtime_store(&dt, dst_parent, &h_path);
5542 +       }
5543 +       h_path.dentry = h_dst;
5544 +
5545 +       isreg = 0;
5546 +       isdir = 0;
5547 +       mode = h_inode->i_mode;
5548 +       switch (mode & S_IFMT) {
5549 +       case S_IFREG:
5550 +               isreg = 1;
5551 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5552 +               if (!err)
5553 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5554 +               break;
5555 +       case S_IFDIR:
5556 +               isdir = 1;
5557 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5558 +               if (!err)
5559 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5560 +               break;
5561 +       case S_IFLNK:
5562 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5563 +               break;
5564 +       case S_IFCHR:
5565 +       case S_IFBLK:
5566 +               AuDebugOn(!capable(CAP_MKNOD));
5567 +               /*FALLTHROUGH*/
5568 +       case S_IFIFO:
5569 +       case S_IFSOCK:
5570 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5571 +               break;
5572 +       default:
5573 +               AuIOErr("Unknown inode type 0%o\n", mode);
5574 +               err = -EIO;
5575 +       }
5576 +       if (!err)
5577 +               err = au_reset_acl(h_dir, &h_path, mode);
5578 +
5579 +       mnt_flags = au_mntflags(sb);
5580 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5581 +           && !isdir
5582 +           && au_opt_test(mnt_flags, XINO)
5583 +           && (h_inode->i_nlink == 1
5584 +               || (h_inode->i_state & I_LINKABLE))
5585 +           /* todo: unnecessary? */
5586 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5587 +           && cpg->bdst < cpg->bsrc
5588 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5589 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5590 +               /* ignore this error */
5591 +
5592 +       if (!err) {
5593 +               force = 0;
5594 +               if (isreg) {
5595 +                       force = !!cpg->len;
5596 +                       if (cpg->len == -1)
5597 +                               force = !!i_size_read(h_inode);
5598 +               }
5599 +               au_fhsm_wrote(sb, cpg->bdst, force);
5600 +       }
5601 +
5602 +       if (do_dt)
5603 +               au_dtime_revert(&dt);
5604 +       return err;
5605 +}
5606 +
5607 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5608 +{
5609 +       int err;
5610 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5611 +       struct inode *h_dir;
5612 +       aufs_bindex_t bdst;
5613 +
5614 +       dentry = cpg->dentry;
5615 +       bdst = cpg->bdst;
5616 +       h_dentry = au_h_dptr(dentry, bdst);
5617 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5618 +               dget(h_dentry);
5619 +               au_set_h_dptr(dentry, bdst, NULL);
5620 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5621 +               if (!err)
5622 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5623 +               au_set_h_dptr(dentry, bdst, h_dentry);
5624 +       } else {
5625 +               err = 0;
5626 +               parent = dget_parent(dentry);
5627 +               h_parent = au_h_dptr(parent, bdst);
5628 +               dput(parent);
5629 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5630 +               if (IS_ERR(h_path->dentry))
5631 +                       err = PTR_ERR(h_path->dentry);
5632 +       }
5633 +       if (unlikely(err))
5634 +               goto out;
5635 +
5636 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5637 +       h_dir = d_inode(h_parent);
5638 +       IMustLock(h_dir);
5639 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5640 +       /* no delegation since it is just created */
5641 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5642 +                          /*flags*/0);
5643 +       dput(h_path->dentry);
5644 +
5645 +out:
5646 +       return err;
5647 +}
5648 +
5649 +/*
5650 + * copyup the @dentry from @bsrc to @bdst.
5651 + * the caller must set the both of lower dentries.
5652 + * @len is for truncating when it is -1 copyup the entire file.
5653 + * in link/rename cases, @dst_parent may be different from the real one.
5654 + * basic->bsrc can be larger than basic->bdst.
5655 + * aufs doesn't touch the credential so
5656 + * security_inode_copy_up{,_xattr}() are unnecessary.
5657 + */
5658 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5659 +{
5660 +       int err, rerr;
5661 +       aufs_bindex_t old_ibtop;
5662 +       unsigned char isdir, plink;
5663 +       struct dentry *h_src, *h_dst, *h_parent;
5664 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5665 +       struct super_block *sb;
5666 +       struct au_branch *br;
5667 +       /* to reduce stack size */
5668 +       struct {
5669 +               struct au_dtime dt;
5670 +               struct path h_path;
5671 +               struct au_cpup_reg_attr h_src_attr;
5672 +       } *a;
5673 +
5674 +       err = -ENOMEM;
5675 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5676 +       if (unlikely(!a))
5677 +               goto out;
5678 +       a->h_src_attr.valid = 0;
5679 +
5680 +       sb = cpg->dentry->d_sb;
5681 +       br = au_sbr(sb, cpg->bdst);
5682 +       a->h_path.mnt = au_br_mnt(br);
5683 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5684 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5685 +       h_dir = d_inode(h_parent);
5686 +       IMustLock(h_dir);
5687 +
5688 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5689 +       inode = d_inode(cpg->dentry);
5690 +
5691 +       if (!dst_parent)
5692 +               dst_parent = dget_parent(cpg->dentry);
5693 +       else
5694 +               dget(dst_parent);
5695 +
5696 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5697 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5698 +       if (dst_inode) {
5699 +               if (unlikely(!plink)) {
5700 +                       err = -EIO;
5701 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5702 +                               "but plink is disabled\n",
5703 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5704 +                       goto out_parent;
5705 +               }
5706 +
5707 +               if (dst_inode->i_nlink) {
5708 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5709 +
5710 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5711 +                       err = PTR_ERR(h_src);
5712 +                       if (IS_ERR(h_src))
5713 +                               goto out_parent;
5714 +                       if (unlikely(d_is_negative(h_src))) {
5715 +                               err = -EIO;
5716 +                               AuIOErr("i%lu exists on b%d "
5717 +                                       "but not pseudo-linked\n",
5718 +                                       inode->i_ino, cpg->bdst);
5719 +                               dput(h_src);
5720 +                               goto out_parent;
5721 +                       }
5722 +
5723 +                       if (do_dt) {
5724 +                               a->h_path.dentry = h_parent;
5725 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5726 +                       }
5727 +
5728 +                       a->h_path.dentry = h_dst;
5729 +                       delegated = NULL;
5730 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5731 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5732 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5733 +                       if (do_dt)
5734 +                               au_dtime_revert(&a->dt);
5735 +                       if (unlikely(err == -EWOULDBLOCK)) {
5736 +                               pr_warn("cannot retry for NFSv4 delegation"
5737 +                                       " for an internal link\n");
5738 +                               iput(delegated);
5739 +                       }
5740 +                       dput(h_src);
5741 +                       goto out_parent;
5742 +               } else
5743 +                       /* todo: cpup_wh_file? */
5744 +                       /* udba work */
5745 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5746 +       }
5747 +
5748 +       isdir = S_ISDIR(inode->i_mode);
5749 +       old_ibtop = au_ibtop(inode);
5750 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5751 +       if (unlikely(err))
5752 +               goto out_rev;
5753 +       dst_inode = d_inode(h_dst);
5754 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5755 +       /* todo: necessary? */
5756 +       /* au_pin_hdir_unlock(cpg->pin); */
5757 +
5758 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5759 +       if (unlikely(err)) {
5760 +               /* todo: necessary? */
5761 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5762 +               inode_unlock(dst_inode);
5763 +               goto out_rev;
5764 +       }
5765 +
5766 +       if (cpg->bdst < old_ibtop) {
5767 +               if (S_ISREG(inode->i_mode)) {
5768 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5769 +                       if (unlikely(err)) {
5770 +                               /* ignore an error */
5771 +                               /* au_pin_hdir_relock(cpg->pin); */
5772 +                               inode_unlock(dst_inode);
5773 +                               goto out_rev;
5774 +                       }
5775 +               }
5776 +               au_set_ibtop(inode, cpg->bdst);
5777 +       } else
5778 +               au_set_ibbot(inode, cpg->bdst);
5779 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5780 +                     au_hi_flags(inode, isdir));
5781 +
5782 +       /* todo: necessary? */
5783 +       /* err = au_pin_hdir_relock(cpg->pin); */
5784 +       inode_unlock(dst_inode);
5785 +       if (unlikely(err))
5786 +               goto out_rev;
5787 +
5788 +       src_inode = d_inode(h_src);
5789 +       if (!isdir
5790 +           && (src_inode->i_nlink > 1
5791 +               || src_inode->i_state & I_LINKABLE)
5792 +           && plink)
5793 +               au_plink_append(inode, cpg->bdst, h_dst);
5794 +
5795 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5796 +               a->h_path.dentry = h_dst;
5797 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5798 +       }
5799 +       if (!err)
5800 +               goto out_parent; /* success */
5801 +
5802 +       /* revert */
5803 +out_rev:
5804 +       a->h_path.dentry = h_parent;
5805 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5806 +       a->h_path.dentry = h_dst;
5807 +       rerr = 0;
5808 +       if (d_is_positive(h_dst)) {
5809 +               if (!isdir) {
5810 +                       /* no delegation since it is just created */
5811 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5812 +                                           /*delegated*/NULL, /*force*/0);
5813 +               } else
5814 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5815 +       }
5816 +       au_dtime_revert(&a->dt);
5817 +       if (rerr) {
5818 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5819 +               err = -EIO;
5820 +       }
5821 +out_parent:
5822 +       dput(dst_parent);
5823 +       au_kfree_rcu(a);
5824 +out:
5825 +       return err;
5826 +}
5827 +
5828 +#if 0 /* reserved */
5829 +struct au_cpup_single_args {
5830 +       int *errp;
5831 +       struct au_cp_generic *cpg;
5832 +       struct dentry *dst_parent;
5833 +};
5834 +
5835 +static void au_call_cpup_single(void *args)
5836 +{
5837 +       struct au_cpup_single_args *a = args;
5838 +
5839 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5840 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5841 +       au_pin_hdir_release(a->cpg->pin);
5842 +}
5843 +#endif
5844 +
5845 +/*
5846 + * prevent SIGXFSZ in copy-up.
5847 + * testing CAP_MKNOD is for generic fs,
5848 + * but CAP_FSETID is for xfs only, currently.
5849 + */
5850 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5851 +{
5852 +       int do_sio;
5853 +       struct super_block *sb;
5854 +       struct inode *h_dir;
5855 +
5856 +       do_sio = 0;
5857 +       sb = au_pinned_parent(pin)->d_sb;
5858 +       if (!au_wkq_test()
5859 +           && (!au_sbi(sb)->si_plink_maint_pid
5860 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5861 +               switch (mode & S_IFMT) {
5862 +               case S_IFREG:
5863 +                       /* no condition about RLIMIT_FSIZE and the file size */
5864 +                       do_sio = 1;
5865 +                       break;
5866 +               case S_IFCHR:
5867 +               case S_IFBLK:
5868 +                       do_sio = !capable(CAP_MKNOD);
5869 +                       break;
5870 +               }
5871 +               if (!do_sio)
5872 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5873 +                                 && !capable(CAP_FSETID));
5874 +               /* this workaround may be removed in the future */
5875 +               if (!do_sio) {
5876 +                       h_dir = au_pinned_h_dir(pin);
5877 +                       do_sio = h_dir->i_mode & S_ISVTX;
5878 +               }
5879 +       }
5880 +
5881 +       return do_sio;
5882 +}
5883 +
5884 +#if 0 /* reserved */
5885 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5886 +{
5887 +       int err, wkq_err;
5888 +       struct dentry *h_dentry;
5889 +
5890 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5891 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5892 +               err = au_cpup_single(cpg, dst_parent);
5893 +       else {
5894 +               struct au_cpup_single_args args = {
5895 +                       .errp           = &err,
5896 +                       .cpg            = cpg,
5897 +                       .dst_parent     = dst_parent
5898 +               };
5899 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5900 +               if (unlikely(wkq_err))
5901 +                       err = wkq_err;
5902 +       }
5903 +
5904 +       return err;
5905 +}
5906 +#endif
5907 +
5908 +/*
5909 + * copyup the @dentry from the first active lower branch to @bdst,
5910 + * using au_cpup_single().
5911 + */
5912 +static int au_cpup_simple(struct au_cp_generic *cpg)
5913 +{
5914 +       int err;
5915 +       unsigned int flags_orig;
5916 +       struct dentry *dentry;
5917 +
5918 +       AuDebugOn(cpg->bsrc < 0);
5919 +
5920 +       dentry = cpg->dentry;
5921 +       DiMustWriteLock(dentry);
5922 +
5923 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5924 +       if (!err) {
5925 +               flags_orig = cpg->flags;
5926 +               au_fset_cpup(cpg->flags, RENAME);
5927 +               err = au_cpup_single(cpg, NULL);
5928 +               cpg->flags = flags_orig;
5929 +               if (!err)
5930 +                       return 0; /* success */
5931 +
5932 +               /* revert */
5933 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5934 +               au_set_dbtop(dentry, cpg->bsrc);
5935 +       }
5936 +
5937 +       return err;
5938 +}
5939 +
5940 +struct au_cpup_simple_args {
5941 +       int *errp;
5942 +       struct au_cp_generic *cpg;
5943 +};
5944 +
5945 +static void au_call_cpup_simple(void *args)
5946 +{
5947 +       struct au_cpup_simple_args *a = args;
5948 +
5949 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5950 +       *a->errp = au_cpup_simple(a->cpg);
5951 +       au_pin_hdir_release(a->cpg->pin);
5952 +}
5953 +
5954 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5955 +{
5956 +       int err, wkq_err;
5957 +       struct dentry *dentry, *parent;
5958 +       struct file *h_file;
5959 +       struct inode *h_dir;
5960 +
5961 +       dentry = cpg->dentry;
5962 +       h_file = NULL;
5963 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5964 +               AuDebugOn(cpg->bsrc < 0);
5965 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5966 +               err = PTR_ERR(h_file);
5967 +               if (IS_ERR(h_file))
5968 +                       goto out;
5969 +       }
5970 +
5971 +       parent = dget_parent(dentry);
5972 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5973 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5974 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5975 +               err = au_cpup_simple(cpg);
5976 +       else {
5977 +               struct au_cpup_simple_args args = {
5978 +                       .errp           = &err,
5979 +                       .cpg            = cpg
5980 +               };
5981 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5982 +               if (unlikely(wkq_err))
5983 +                       err = wkq_err;
5984 +       }
5985 +
5986 +       dput(parent);
5987 +       if (h_file)
5988 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5989 +
5990 +out:
5991 +       return err;
5992 +}
5993 +
5994 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5995 +{
5996 +       aufs_bindex_t bsrc, bbot;
5997 +       struct dentry *dentry, *h_dentry;
5998 +
5999 +       if (cpg->bsrc < 0) {
6000 +               dentry = cpg->dentry;
6001 +               bbot = au_dbbot(dentry);
6002 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6003 +                       h_dentry = au_h_dptr(dentry, bsrc);
6004 +                       if (h_dentry) {
6005 +                               AuDebugOn(d_is_negative(h_dentry));
6006 +                               break;
6007 +                       }
6008 +               }
6009 +               AuDebugOn(bsrc > bbot);
6010 +               cpg->bsrc = bsrc;
6011 +       }
6012 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6013 +       return au_do_sio_cpup_simple(cpg);
6014 +}
6015 +
6016 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6017 +{
6018 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6019 +       return au_do_sio_cpup_simple(cpg);
6020 +}
6021 +
6022 +/* ---------------------------------------------------------------------- */
6023 +
6024 +/*
6025 + * copyup the deleted file for writing.
6026 + */
6027 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6028 +                        struct file *file)
6029 +{
6030 +       int err;
6031 +       unsigned int flags_orig;
6032 +       aufs_bindex_t bsrc_orig;
6033 +       struct au_dinfo *dinfo;
6034 +       struct {
6035 +               struct au_hdentry *hd;
6036 +               struct dentry *h_dentry;
6037 +       } hdst, hsrc;
6038 +
6039 +       dinfo = au_di(cpg->dentry);
6040 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6041 +
6042 +       bsrc_orig = cpg->bsrc;
6043 +       cpg->bsrc = dinfo->di_btop;
6044 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6045 +       hdst.h_dentry = hdst.hd->hd_dentry;
6046 +       hdst.hd->hd_dentry = wh_dentry;
6047 +       dinfo->di_btop = cpg->bdst;
6048 +
6049 +       hsrc.h_dentry = NULL;
6050 +       if (file) {
6051 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6052 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6053 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6054 +       }
6055 +       flags_orig = cpg->flags;
6056 +       cpg->flags = !AuCpup_DTIME;
6057 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6058 +       cpg->flags = flags_orig;
6059 +       if (file) {
6060 +               if (!err)
6061 +                       err = au_reopen_nondir(file);
6062 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6063 +       }
6064 +       hdst.hd->hd_dentry = hdst.h_dentry;
6065 +       dinfo->di_btop = cpg->bsrc;
6066 +       cpg->bsrc = bsrc_orig;
6067 +
6068 +       return err;
6069 +}
6070 +
6071 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6072 +{
6073 +       int err;
6074 +       aufs_bindex_t bdst;
6075 +       struct au_dtime dt;
6076 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6077 +       struct au_branch *br;
6078 +       struct path h_path;
6079 +
6080 +       dentry = cpg->dentry;
6081 +       bdst = cpg->bdst;
6082 +       br = au_sbr(dentry->d_sb, bdst);
6083 +       parent = dget_parent(dentry);
6084 +       h_parent = au_h_dptr(parent, bdst);
6085 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6086 +       err = PTR_ERR(wh_dentry);
6087 +       if (IS_ERR(wh_dentry))
6088 +               goto out;
6089 +
6090 +       h_path.dentry = h_parent;
6091 +       h_path.mnt = au_br_mnt(br);
6092 +       au_dtime_store(&dt, parent, &h_path);
6093 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6094 +       if (unlikely(err))
6095 +               goto out_wh;
6096 +
6097 +       dget(wh_dentry);
6098 +       h_path.dentry = wh_dentry;
6099 +       if (!d_is_dir(wh_dentry)) {
6100 +               /* no delegation since it is just created */
6101 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6102 +                                  /*delegated*/NULL, /*force*/0);
6103 +       } else
6104 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6105 +       if (unlikely(err)) {
6106 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6107 +                       wh_dentry, err);
6108 +               err = -EIO;
6109 +       }
6110 +       au_dtime_revert(&dt);
6111 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6112 +
6113 +out_wh:
6114 +       dput(wh_dentry);
6115 +out:
6116 +       dput(parent);
6117 +       return err;
6118 +}
6119 +
6120 +struct au_cpup_wh_args {
6121 +       int *errp;
6122 +       struct au_cp_generic *cpg;
6123 +       struct file *file;
6124 +};
6125 +
6126 +static void au_call_cpup_wh(void *args)
6127 +{
6128 +       struct au_cpup_wh_args *a = args;
6129 +
6130 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6131 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6132 +       au_pin_hdir_release(a->cpg->pin);
6133 +}
6134 +
6135 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6136 +{
6137 +       int err, wkq_err;
6138 +       aufs_bindex_t bdst;
6139 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6140 +       struct inode *dir, *h_dir, *h_tmpdir;
6141 +       struct au_wbr *wbr;
6142 +       struct au_pin wh_pin, *pin_orig;
6143 +
6144 +       dentry = cpg->dentry;
6145 +       bdst = cpg->bdst;
6146 +       parent = dget_parent(dentry);
6147 +       dir = d_inode(parent);
6148 +       h_orph = NULL;
6149 +       h_parent = NULL;
6150 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6151 +       h_tmpdir = h_dir;
6152 +       pin_orig = NULL;
6153 +       if (!h_dir->i_nlink) {
6154 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6155 +               h_orph = wbr->wbr_orph;
6156 +
6157 +               h_parent = dget(au_h_dptr(parent, bdst));
6158 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6159 +               h_tmpdir = d_inode(h_orph);
6160 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6161 +
6162 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6163 +               /* todo: au_h_open_pre()? */
6164 +
6165 +               pin_orig = cpg->pin;
6166 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6167 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6168 +               cpg->pin = &wh_pin;
6169 +       }
6170 +
6171 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6172 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6173 +               err = au_cpup_wh(cpg, file);
6174 +       else {
6175 +               struct au_cpup_wh_args args = {
6176 +                       .errp   = &err,
6177 +                       .cpg    = cpg,
6178 +                       .file   = file
6179 +               };
6180 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6181 +               if (unlikely(wkq_err))
6182 +                       err = wkq_err;
6183 +       }
6184 +
6185 +       if (h_orph) {
6186 +               inode_unlock(h_tmpdir);
6187 +               /* todo: au_h_open_post()? */
6188 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6189 +               au_set_h_dptr(parent, bdst, h_parent);
6190 +               AuDebugOn(!pin_orig);
6191 +               cpg->pin = pin_orig;
6192 +       }
6193 +       iput(h_dir);
6194 +       dput(parent);
6195 +
6196 +       return err;
6197 +}
6198 +
6199 +/* ---------------------------------------------------------------------- */
6200 +
6201 +/*
6202 + * generic routine for both of copy-up and copy-down.
6203 + */
6204 +/* cf. revalidate function in file.c */
6205 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6206 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6207 +                        struct au_pin *pin,
6208 +                        struct dentry *h_parent, void *arg),
6209 +              void *arg)
6210 +{
6211 +       int err;
6212 +       struct au_pin pin;
6213 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6214 +
6215 +       err = 0;
6216 +       parent = dget_parent(dentry);
6217 +       if (IS_ROOT(parent))
6218 +               goto out;
6219 +
6220 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6221 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6222 +
6223 +       /* do not use au_dpage */
6224 +       real_parent = parent;
6225 +       while (1) {
6226 +               dput(parent);
6227 +               parent = dget_parent(dentry);
6228 +               h_parent = au_h_dptr(parent, bdst);
6229 +               if (h_parent)
6230 +                       goto out; /* success */
6231 +
6232 +               /* find top dir which is necessary to cpup */
6233 +               do {
6234 +                       d = parent;
6235 +                       dput(parent);
6236 +                       parent = dget_parent(d);
6237 +                       di_read_lock_parent3(parent, !AuLock_IR);
6238 +                       h_parent = au_h_dptr(parent, bdst);
6239 +                       di_read_unlock(parent, !AuLock_IR);
6240 +               } while (!h_parent);
6241 +
6242 +               if (d != real_parent)
6243 +                       di_write_lock_child3(d);
6244 +
6245 +               /* somebody else might create while we were sleeping */
6246 +               h_dentry = au_h_dptr(d, bdst);
6247 +               if (!h_dentry || d_is_negative(h_dentry)) {
6248 +                       if (h_dentry)
6249 +                               au_update_dbtop(d);
6250 +
6251 +                       au_pin_set_dentry(&pin, d);
6252 +                       err = au_do_pin(&pin);
6253 +                       if (!err) {
6254 +                               err = cp(d, bdst, &pin, h_parent, arg);
6255 +                               au_unpin(&pin);
6256 +                       }
6257 +               }
6258 +
6259 +               if (d != real_parent)
6260 +                       di_write_unlock(d);
6261 +               if (unlikely(err))
6262 +                       break;
6263 +       }
6264 +
6265 +out:
6266 +       dput(parent);
6267 +       return err;
6268 +}
6269 +
6270 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6271 +                      struct au_pin *pin,
6272 +                      struct dentry *h_parent __maybe_unused,
6273 +                      void *arg __maybe_unused)
6274 +{
6275 +       struct au_cp_generic cpg = {
6276 +               .dentry = dentry,
6277 +               .bdst   = bdst,
6278 +               .bsrc   = -1,
6279 +               .len    = 0,
6280 +               .pin    = pin,
6281 +               .flags  = AuCpup_DTIME
6282 +       };
6283 +       return au_sio_cpup_simple(&cpg);
6284 +}
6285 +
6286 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6287 +{
6288 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6289 +}
6290 +
6291 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6292 +{
6293 +       int err;
6294 +       struct dentry *parent;
6295 +       struct inode *dir;
6296 +
6297 +       parent = dget_parent(dentry);
6298 +       dir = d_inode(parent);
6299 +       err = 0;
6300 +       if (au_h_iptr(dir, bdst))
6301 +               goto out;
6302 +
6303 +       di_read_unlock(parent, AuLock_IR);
6304 +       di_write_lock_parent(parent);
6305 +       /* someone else might change our inode while we were sleeping */
6306 +       if (!au_h_iptr(dir, bdst))
6307 +               err = au_cpup_dirs(dentry, bdst);
6308 +       di_downgrade_lock(parent, AuLock_IR);
6309 +
6310 +out:
6311 +       dput(parent);
6312 +       return err;
6313 +}
6314 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6315 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6316 +++ linux/fs/aufs/cpup.h        2020-01-27 10:57:18.168871450 +0100
6317 @@ -0,0 +1,100 @@
6318 +/* SPDX-License-Identifier: GPL-2.0 */
6319 +/*
6320 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6321 + *
6322 + * This program, aufs is free software; you can redistribute it and/or modify
6323 + * it under the terms of the GNU General Public License as published by
6324 + * the Free Software Foundation; either version 2 of the License, or
6325 + * (at your option) any later version.
6326 + *
6327 + * This program is distributed in the hope that it will be useful,
6328 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6329 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6330 + * GNU General Public License for more details.
6331 + *
6332 + * You should have received a copy of the GNU General Public License
6333 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6334 + */
6335 +
6336 +/*
6337 + * copy-up/down functions
6338 + */
6339 +
6340 +#ifndef __AUFS_CPUP_H__
6341 +#define __AUFS_CPUP_H__
6342 +
6343 +#ifdef __KERNEL__
6344 +
6345 +#include <linux/path.h>
6346 +
6347 +struct inode;
6348 +struct file;
6349 +struct au_pin;
6350 +
6351 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6352 +void au_cpup_attr_timesizes(struct inode *inode);
6353 +void au_cpup_attr_nlink(struct inode *inode, int force);
6354 +void au_cpup_attr_changeable(struct inode *inode);
6355 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6356 +void au_cpup_attr_all(struct inode *inode, int force);
6357 +
6358 +/* ---------------------------------------------------------------------- */
6359 +
6360 +struct au_cp_generic {
6361 +       struct dentry   *dentry;
6362 +       aufs_bindex_t   bdst, bsrc;
6363 +       loff_t          len;
6364 +       struct au_pin   *pin;
6365 +       unsigned int    flags;
6366 +};
6367 +
6368 +/* cpup flags */
6369 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6370 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6371 +                                                  for link(2) */
6372 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6373 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6374 +                                                  cpup */
6375 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6376 +                                                  existing entry */
6377 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6378 +                                                  the branch is marked as RO */
6379 +
6380 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6381 +#undef AuCpup_HOPEN
6382 +#define AuCpup_HOPEN           0
6383 +#endif
6384 +
6385 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6386 +#define au_fset_cpup(flags, name) \
6387 +       do { (flags) |= AuCpup_##name; } while (0)
6388 +#define au_fclr_cpup(flags, name) \
6389 +       do { (flags) &= ~AuCpup_##name; } while (0)
6390 +
6391 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6392 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6393 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6394 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6395 +
6396 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6397 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6398 +                        struct au_pin *pin,
6399 +                        struct dentry *h_parent, void *arg),
6400 +              void *arg);
6401 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6402 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6403 +
6404 +/* ---------------------------------------------------------------------- */
6405 +
6406 +/* keep timestamps when copyup */
6407 +struct au_dtime {
6408 +       struct dentry *dt_dentry;
6409 +       struct path dt_h_path;
6410 +       struct timespec64 dt_atime, dt_mtime;
6411 +};
6412 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6413 +                   struct path *h_path);
6414 +void au_dtime_revert(struct au_dtime *dt);
6415 +
6416 +#endif /* __KERNEL__ */
6417 +#endif /* __AUFS_CPUP_H__ */
6418 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6419 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6420 +++ linux/fs/aufs/dbgaufs.c     2020-01-27 10:57:18.168871450 +0100
6421 @@ -0,0 +1,526 @@
6422 +// SPDX-License-Identifier: GPL-2.0
6423 +/*
6424 + * Copyright (C) 2005-2020 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 + * debugfs interface
6442 + */
6443 +
6444 +#include <linux/debugfs.h>
6445 +#include "aufs.h"
6446 +
6447 +#ifndef CONFIG_SYSFS
6448 +#error DEBUG_FS depends upon SYSFS
6449 +#endif
6450 +
6451 +static struct dentry *dbgaufs;
6452 +static const mode_t dbgaufs_mode = 0444;
6453 +
6454 +/* 20 is max digits length of ulong 64 */
6455 +struct dbgaufs_arg {
6456 +       int n;
6457 +       char a[20 * 4];
6458 +};
6459 +
6460 +/*
6461 + * common function for all XINO files
6462 + */
6463 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6464 +                             struct file *file)
6465 +{
6466 +       void *p;
6467 +
6468 +       p = file->private_data;
6469 +       if (p) {
6470 +               /* this is struct dbgaufs_arg */
6471 +               AuDebugOn(!au_kfree_sz_test(p));
6472 +               au_kfree_do_rcu(p);
6473 +       }
6474 +       return 0;
6475 +}
6476 +
6477 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6478 +                          int cnt)
6479 +{
6480 +       int err;
6481 +       struct kstat st;
6482 +       struct dbgaufs_arg *p;
6483 +
6484 +       err = -ENOMEM;
6485 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6486 +       if (unlikely(!p))
6487 +               goto out;
6488 +
6489 +       err = 0;
6490 +       p->n = 0;
6491 +       file->private_data = p;
6492 +       if (!xf)
6493 +               goto out;
6494 +
6495 +       err = vfsub_getattr(&xf->f_path, &st);
6496 +       if (!err) {
6497 +               if (do_fcnt)
6498 +                       p->n = snprintf
6499 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6500 +                                cnt, st.blocks, st.blksize,
6501 +                                (long long)st.size);
6502 +               else
6503 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6504 +                                       st.blocks, st.blksize,
6505 +                                       (long long)st.size);
6506 +               AuDebugOn(p->n >= sizeof(p->a));
6507 +       } else {
6508 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6509 +               err = 0;
6510 +       }
6511 +
6512 +out:
6513 +       return err;
6514 +}
6515 +
6516 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6517 +                              size_t count, loff_t *ppos)
6518 +{
6519 +       struct dbgaufs_arg *p;
6520 +
6521 +       p = file->private_data;
6522 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6523 +}
6524 +
6525 +/* ---------------------------------------------------------------------- */
6526 +
6527 +struct dbgaufs_plink_arg {
6528 +       int n;
6529 +       char a[];
6530 +};
6531 +
6532 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6533 +                                struct file *file)
6534 +{
6535 +       free_page((unsigned long)file->private_data);
6536 +       return 0;
6537 +}
6538 +
6539 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6540 +{
6541 +       int err, i, limit;
6542 +       unsigned long n, sum;
6543 +       struct dbgaufs_plink_arg *p;
6544 +       struct au_sbinfo *sbinfo;
6545 +       struct super_block *sb;
6546 +       struct hlist_bl_head *hbl;
6547 +
6548 +       err = -ENOMEM;
6549 +       p = (void *)get_zeroed_page(GFP_NOFS);
6550 +       if (unlikely(!p))
6551 +               goto out;
6552 +
6553 +       err = -EFBIG;
6554 +       sbinfo = inode->i_private;
6555 +       sb = sbinfo->si_sb;
6556 +       si_noflush_read_lock(sb);
6557 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6558 +               limit = PAGE_SIZE - sizeof(p->n);
6559 +
6560 +               /* the number of buckets */
6561 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6562 +               p->n += n;
6563 +               limit -= n;
6564 +
6565 +               sum = 0;
6566 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6567 +                    i++, hbl++) {
6568 +                       n = au_hbl_count(hbl);
6569 +                       sum += n;
6570 +
6571 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6572 +                       p->n += n;
6573 +                       limit -= n;
6574 +                       if (unlikely(limit <= 0))
6575 +                               goto out_free;
6576 +               }
6577 +               p->a[p->n - 1] = '\n';
6578 +
6579 +               /* the sum of plinks */
6580 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6581 +               p->n += n;
6582 +               limit -= n;
6583 +               if (unlikely(limit <= 0))
6584 +                       goto out_free;
6585 +       } else {
6586 +#define str "1\n0\n0\n"
6587 +               p->n = sizeof(str) - 1;
6588 +               strcpy(p->a, str);
6589 +#undef str
6590 +       }
6591 +       si_read_unlock(sb);
6592 +
6593 +       err = 0;
6594 +       file->private_data = p;
6595 +       goto out; /* success */
6596 +
6597 +out_free:
6598 +       free_page((unsigned long)p);
6599 +out:
6600 +       return err;
6601 +}
6602 +
6603 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6604 +                                 size_t count, loff_t *ppos)
6605 +{
6606 +       struct dbgaufs_plink_arg *p;
6607 +
6608 +       p = file->private_data;
6609 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6610 +}
6611 +
6612 +static const struct file_operations dbgaufs_plink_fop = {
6613 +       .owner          = THIS_MODULE,
6614 +       .open           = dbgaufs_plink_open,
6615 +       .release        = dbgaufs_plink_release,
6616 +       .read           = dbgaufs_plink_read
6617 +};
6618 +
6619 +/* ---------------------------------------------------------------------- */
6620 +
6621 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6622 +{
6623 +       int err;
6624 +       struct au_sbinfo *sbinfo;
6625 +       struct super_block *sb;
6626 +
6627 +       sbinfo = inode->i_private;
6628 +       sb = sbinfo->si_sb;
6629 +       si_noflush_read_lock(sb);
6630 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6631 +       si_read_unlock(sb);
6632 +       return err;
6633 +}
6634 +
6635 +static const struct file_operations dbgaufs_xib_fop = {
6636 +       .owner          = THIS_MODULE,
6637 +       .open           = dbgaufs_xib_open,
6638 +       .release        = dbgaufs_xi_release,
6639 +       .read           = dbgaufs_xi_read
6640 +};
6641 +
6642 +/* ---------------------------------------------------------------------- */
6643 +
6644 +#define DbgaufsXi_PREFIX "xi"
6645 +
6646 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6647 +{
6648 +       int err, idx;
6649 +       long l;
6650 +       aufs_bindex_t bindex;
6651 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6652 +       struct au_sbinfo *sbinfo;
6653 +       struct super_block *sb;
6654 +       struct au_xino *xi;
6655 +       struct file *xf;
6656 +       struct qstr *name;
6657 +       struct au_branch *br;
6658 +
6659 +       err = -ENOENT;
6660 +       name = &file->f_path.dentry->d_name;
6661 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6662 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6663 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6664 +               goto out;
6665 +
6666 +       AuDebugOn(name->len >= sizeof(a));
6667 +       memcpy(a, name->name, name->len);
6668 +       a[name->len] = '\0';
6669 +       p = strchr(a, '-');
6670 +       if (p)
6671 +               *p = '\0';
6672 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6673 +       if (unlikely(err))
6674 +               goto out;
6675 +       bindex = l;
6676 +       idx = 0;
6677 +       if (p) {
6678 +               err = kstrtol(p + 1, 10, &l);
6679 +               if (unlikely(err))
6680 +                       goto out;
6681 +               idx = l;
6682 +       }
6683 +
6684 +       err = -ENOENT;
6685 +       sbinfo = inode->i_private;
6686 +       sb = sbinfo->si_sb;
6687 +       si_noflush_read_lock(sb);
6688 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6689 +               goto out_si;
6690 +       br = au_sbr(sb, bindex);
6691 +       xi = br->br_xino;
6692 +       if (unlikely(idx >= xi->xi_nfile))
6693 +               goto out_si;
6694 +       xf = au_xino_file(xi, idx);
6695 +       if (xf)
6696 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6697 +                                     au_xino_count(br));
6698 +
6699 +out_si:
6700 +       si_read_unlock(sb);
6701 +out:
6702 +       AuTraceErr(err);
6703 +       return err;
6704 +}
6705 +
6706 +static const struct file_operations dbgaufs_xino_fop = {
6707 +       .owner          = THIS_MODULE,
6708 +       .open           = dbgaufs_xino_open,
6709 +       .release        = dbgaufs_xi_release,
6710 +       .read           = dbgaufs_xi_read
6711 +};
6712 +
6713 +void dbgaufs_xino_del(struct au_branch *br)
6714 +{
6715 +       struct dentry *dbgaufs;
6716 +
6717 +       dbgaufs = br->br_dbgaufs;
6718 +       if (!dbgaufs)
6719 +               return;
6720 +
6721 +       br->br_dbgaufs = NULL;
6722 +       /* debugfs acquires the parent i_mutex */
6723 +       lockdep_off();
6724 +       debugfs_remove(dbgaufs);
6725 +       lockdep_on();
6726 +}
6727 +
6728 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6729 +{
6730 +       aufs_bindex_t bbot;
6731 +       struct au_branch *br;
6732 +
6733 +       if (!au_sbi(sb)->si_dbgaufs)
6734 +               return;
6735 +
6736 +       bbot = au_sbbot(sb);
6737 +       for (; bindex <= bbot; bindex++) {
6738 +               br = au_sbr(sb, bindex);
6739 +               dbgaufs_xino_del(br);
6740 +       }
6741 +}
6742 +
6743 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6744 +                             unsigned int idx, struct dentry *parent,
6745 +                             struct au_sbinfo *sbinfo)
6746 +{
6747 +       struct au_branch *br;
6748 +       struct dentry *d;
6749 +       /* "xi" bindex(5) "-" idx(2) NULL */
6750 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6751 +
6752 +       if (!idx)
6753 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6754 +       else
6755 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6756 +                        bindex, idx);
6757 +       br = au_sbr(sb, bindex);
6758 +       if (br->br_dbgaufs) {
6759 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6760 +
6761 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6762 +                       /* debugfs acquires the parent i_mutex */
6763 +                       lockdep_off();
6764 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6765 +                                          name);
6766 +                       lockdep_on();
6767 +                       if (unlikely(!d))
6768 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6769 +                                       parent, name);
6770 +               }
6771 +       } else {
6772 +               lockdep_off();
6773 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6774 +                                                    sbinfo, &dbgaufs_xino_fop);
6775 +               lockdep_on();
6776 +               if (unlikely(!br->br_dbgaufs))
6777 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6778 +                               parent, name);
6779 +       }
6780 +}
6781 +
6782 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6783 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6784 +{
6785 +       struct au_branch *br;
6786 +       struct au_xino *xi;
6787 +       unsigned int u;
6788 +
6789 +       br = au_sbr(sb, bindex);
6790 +       xi = br->br_xino;
6791 +       for (u = 0; u < xi->xi_nfile; u++)
6792 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6793 +}
6794 +
6795 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6796 +{
6797 +       struct au_sbinfo *sbinfo;
6798 +       struct dentry *parent;
6799 +       aufs_bindex_t bbot;
6800 +
6801 +       if (!au_opt_test(au_mntflags(sb), XINO))
6802 +               return;
6803 +
6804 +       sbinfo = au_sbi(sb);
6805 +       parent = sbinfo->si_dbgaufs;
6806 +       if (!parent)
6807 +               return;
6808 +
6809 +       bbot = au_sbbot(sb);
6810 +       if (topdown)
6811 +               for (; bindex <= bbot; bindex++)
6812 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6813 +       else
6814 +               for (; bbot >= bindex; bbot--)
6815 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6816 +}
6817 +
6818 +/* ---------------------------------------------------------------------- */
6819 +
6820 +#ifdef CONFIG_AUFS_EXPORT
6821 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6822 +{
6823 +       int err;
6824 +       struct au_sbinfo *sbinfo;
6825 +       struct super_block *sb;
6826 +
6827 +       sbinfo = inode->i_private;
6828 +       sb = sbinfo->si_sb;
6829 +       si_noflush_read_lock(sb);
6830 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6831 +       si_read_unlock(sb);
6832 +       return err;
6833 +}
6834 +
6835 +static const struct file_operations dbgaufs_xigen_fop = {
6836 +       .owner          = THIS_MODULE,
6837 +       .open           = dbgaufs_xigen_open,
6838 +       .release        = dbgaufs_xi_release,
6839 +       .read           = dbgaufs_xi_read
6840 +};
6841 +
6842 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6843 +{
6844 +       int err;
6845 +
6846 +       /*
6847 +        * This function is a dynamic '__init' function actually,
6848 +        * so the tiny check for si_rwsem is unnecessary.
6849 +        */
6850 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6851 +
6852 +       err = -EIO;
6853 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6854 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6855 +                &dbgaufs_xigen_fop);
6856 +       if (sbinfo->si_dbgaufs_xigen)
6857 +               err = 0;
6858 +
6859 +       return err;
6860 +}
6861 +#else
6862 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6863 +{
6864 +       return 0;
6865 +}
6866 +#endif /* CONFIG_AUFS_EXPORT */
6867 +
6868 +/* ---------------------------------------------------------------------- */
6869 +
6870 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6871 +{
6872 +       /*
6873 +        * This function is a dynamic '__fin' function actually,
6874 +        * so the tiny check for si_rwsem is unnecessary.
6875 +        */
6876 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6877 +
6878 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6879 +       sbinfo->si_dbgaufs = NULL;
6880 +}
6881 +
6882 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6883 +{
6884 +       int err;
6885 +       char name[SysaufsSiNameLen];
6886 +
6887 +       /*
6888 +        * This function is a dynamic '__init' function actually,
6889 +        * so the tiny check for si_rwsem is unnecessary.
6890 +        */
6891 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6892 +
6893 +       err = -ENOENT;
6894 +       if (!dbgaufs) {
6895 +               AuErr1("/debug/aufs is uninitialized\n");
6896 +               goto out;
6897 +       }
6898 +
6899 +       err = -EIO;
6900 +       sysaufs_name(sbinfo, name);
6901 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6902 +       if (unlikely(!sbinfo->si_dbgaufs))
6903 +               goto out;
6904 +
6905 +       /* regardless plink/noplink option */
6906 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6907 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6908 +                &dbgaufs_plink_fop);
6909 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6910 +               goto out_dir;
6911 +
6912 +       /* regardless xino/noxino option */
6913 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6914 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6915 +                &dbgaufs_xib_fop);
6916 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6917 +               goto out_dir;
6918 +
6919 +       err = dbgaufs_xigen_init(sbinfo);
6920 +       if (!err)
6921 +               goto out; /* success */
6922 +
6923 +out_dir:
6924 +       dbgaufs_si_fin(sbinfo);
6925 +out:
6926 +       if (unlikely(err))
6927 +               pr_err("debugfs/aufs failed\n");
6928 +       return err;
6929 +}
6930 +
6931 +/* ---------------------------------------------------------------------- */
6932 +
6933 +void dbgaufs_fin(void)
6934 +{
6935 +       debugfs_remove(dbgaufs);
6936 +}
6937 +
6938 +int __init dbgaufs_init(void)
6939 +{
6940 +       int err;
6941 +
6942 +       err = -EIO;
6943 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6944 +       if (dbgaufs)
6945 +               err = 0;
6946 +       return err;
6947 +}
6948 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6949 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6950 +++ linux/fs/aufs/dbgaufs.h     2020-01-27 10:57:18.168871450 +0100
6951 @@ -0,0 +1,53 @@
6952 +/* SPDX-License-Identifier: GPL-2.0 */
6953 +/*
6954 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6955 + *
6956 + * This program, aufs is free software; you can redistribute it and/or modify
6957 + * it under the terms of the GNU General Public License as published by
6958 + * the Free Software Foundation; either version 2 of the License, or
6959 + * (at your option) any later version.
6960 + *
6961 + * This program is distributed in the hope that it will be useful,
6962 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6963 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6964 + * GNU General Public License for more details.
6965 + *
6966 + * You should have received a copy of the GNU General Public License
6967 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6968 + */
6969 +
6970 +/*
6971 + * debugfs interface
6972 + */
6973 +
6974 +#ifndef __DBGAUFS_H__
6975 +#define __DBGAUFS_H__
6976 +
6977 +#ifdef __KERNEL__
6978 +
6979 +struct super_block;
6980 +struct au_sbinfo;
6981 +struct au_branch;
6982 +
6983 +#ifdef CONFIG_DEBUG_FS
6984 +/* dbgaufs.c */
6985 +void dbgaufs_xino_del(struct au_branch *br);
6986 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6987 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6988 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6989 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6990 +void dbgaufs_fin(void);
6991 +int __init dbgaufs_init(void);
6992 +#else
6993 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6994 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6995 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6996 +          int topdown)
6997 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6998 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6999 +AuStubVoid(dbgaufs_fin, void)
7000 +AuStubInt0(__init dbgaufs_init, void)
7001 +#endif /* CONFIG_DEBUG_FS */
7002 +
7003 +#endif /* __KERNEL__ */
7004 +#endif /* __DBGAUFS_H__ */
7005 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7006 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7007 +++ linux/fs/aufs/dcsub.c       2020-01-27 10:57:18.168871450 +0100
7008 @@ -0,0 +1,225 @@
7009 +// SPDX-License-Identifier: GPL-2.0
7010 +/*
7011 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7012 + *
7013 + * This program, aufs is free software; you can redistribute it and/or modify
7014 + * it under the terms of the GNU General Public License as published by
7015 + * the Free Software Foundation; either version 2 of the License, or
7016 + * (at your option) any later version.
7017 + *
7018 + * This program is distributed in the hope that it will be useful,
7019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7021 + * GNU General Public License for more details.
7022 + *
7023 + * You should have received a copy of the GNU General Public License
7024 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7025 + */
7026 +
7027 +/*
7028 + * sub-routines for dentry cache
7029 + */
7030 +
7031 +#include "aufs.h"
7032 +
7033 +static void au_dpage_free(struct au_dpage *dpage)
7034 +{
7035 +       int i;
7036 +       struct dentry **p;
7037 +
7038 +       p = dpage->dentries;
7039 +       for (i = 0; i < dpage->ndentry; i++)
7040 +               dput(*p++);
7041 +       free_page((unsigned long)dpage->dentries);
7042 +}
7043 +
7044 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7045 +{
7046 +       int err;
7047 +       void *p;
7048 +
7049 +       err = -ENOMEM;
7050 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7051 +       if (unlikely(!dpages->dpages))
7052 +               goto out;
7053 +
7054 +       p = (void *)__get_free_page(gfp);
7055 +       if (unlikely(!p))
7056 +               goto out_dpages;
7057 +
7058 +       dpages->dpages[0].ndentry = 0;
7059 +       dpages->dpages[0].dentries = p;
7060 +       dpages->ndpage = 1;
7061 +       return 0; /* success */
7062 +
7063 +out_dpages:
7064 +       au_kfree_try_rcu(dpages->dpages);
7065 +out:
7066 +       return err;
7067 +}
7068 +
7069 +void au_dpages_free(struct au_dcsub_pages *dpages)
7070 +{
7071 +       int i;
7072 +       struct au_dpage *p;
7073 +
7074 +       p = dpages->dpages;
7075 +       for (i = 0; i < dpages->ndpage; i++)
7076 +               au_dpage_free(p++);
7077 +       au_kfree_try_rcu(dpages->dpages);
7078 +}
7079 +
7080 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7081 +                           struct dentry *dentry, gfp_t gfp)
7082 +{
7083 +       int err, sz;
7084 +       struct au_dpage *dpage;
7085 +       void *p;
7086 +
7087 +       dpage = dpages->dpages + dpages->ndpage - 1;
7088 +       sz = PAGE_SIZE / sizeof(dentry);
7089 +       if (unlikely(dpage->ndentry >= sz)) {
7090 +               AuLabel(new dpage);
7091 +               err = -ENOMEM;
7092 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7093 +               p = au_kzrealloc(dpages->dpages, sz,
7094 +                                sz + sizeof(*dpages->dpages), gfp,
7095 +                                /*may_shrink*/0);
7096 +               if (unlikely(!p))
7097 +                       goto out;
7098 +
7099 +               dpages->dpages = p;
7100 +               dpage = dpages->dpages + dpages->ndpage;
7101 +               p = (void *)__get_free_page(gfp);
7102 +               if (unlikely(!p))
7103 +                       goto out;
7104 +
7105 +               dpage->ndentry = 0;
7106 +               dpage->dentries = p;
7107 +               dpages->ndpage++;
7108 +       }
7109 +
7110 +       AuDebugOn(au_dcount(dentry) <= 0);
7111 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7112 +       return 0; /* success */
7113 +
7114 +out:
7115 +       return err;
7116 +}
7117 +
7118 +/* todo: BAD approach */
7119 +/* copied from linux/fs/dcache.c */
7120 +enum d_walk_ret {
7121 +       D_WALK_CONTINUE,
7122 +       D_WALK_QUIT,
7123 +       D_WALK_NORETRY,
7124 +       D_WALK_SKIP,
7125 +};
7126 +
7127 +extern void d_walk(struct dentry *parent, void *data,
7128 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7129 +
7130 +struct ac_dpages_arg {
7131 +       int err;
7132 +       struct au_dcsub_pages *dpages;
7133 +       struct super_block *sb;
7134 +       au_dpages_test test;
7135 +       void *arg;
7136 +};
7137 +
7138 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7139 +{
7140 +       enum d_walk_ret ret;
7141 +       struct ac_dpages_arg *arg = _arg;
7142 +
7143 +       ret = D_WALK_CONTINUE;
7144 +       if (dentry->d_sb == arg->sb
7145 +           && !IS_ROOT(dentry)
7146 +           && au_dcount(dentry) > 0
7147 +           && au_di(dentry)
7148 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7149 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7150 +               if (unlikely(arg->err))
7151 +                       ret = D_WALK_QUIT;
7152 +       }
7153 +
7154 +       return ret;
7155 +}
7156 +
7157 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7158 +                  au_dpages_test test, void *arg)
7159 +{
7160 +       struct ac_dpages_arg args = {
7161 +               .err    = 0,
7162 +               .dpages = dpages,
7163 +               .sb     = root->d_sb,
7164 +               .test   = test,
7165 +               .arg    = arg
7166 +       };
7167 +
7168 +       d_walk(root, &args, au_call_dpages_append);
7169 +
7170 +       return args.err;
7171 +}
7172 +
7173 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7174 +                      int do_include, au_dpages_test test, void *arg)
7175 +{
7176 +       int err;
7177 +
7178 +       err = 0;
7179 +       write_seqlock(&rename_lock);
7180 +       spin_lock(&dentry->d_lock);
7181 +       if (do_include
7182 +           && au_dcount(dentry) > 0
7183 +           && (!test || test(dentry, arg)))
7184 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7185 +       spin_unlock(&dentry->d_lock);
7186 +       if (unlikely(err))
7187 +               goto out;
7188 +
7189 +       /*
7190 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7191 +        * mount
7192 +        */
7193 +       while (!IS_ROOT(dentry)) {
7194 +               dentry = dentry->d_parent; /* rename_lock is locked */
7195 +               spin_lock(&dentry->d_lock);
7196 +               if (au_dcount(dentry) > 0
7197 +                   && (!test || test(dentry, arg)))
7198 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7199 +               spin_unlock(&dentry->d_lock);
7200 +               if (unlikely(err))
7201 +                       break;
7202 +       }
7203 +
7204 +out:
7205 +       write_sequnlock(&rename_lock);
7206 +       return err;
7207 +}
7208 +
7209 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7210 +{
7211 +       return au_di(dentry) && dentry->d_sb == arg;
7212 +}
7213 +
7214 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7215 +                           struct dentry *dentry, int do_include)
7216 +{
7217 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7218 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7219 +}
7220 +
7221 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7222 +{
7223 +       struct path path[2] = {
7224 +               {
7225 +                       .dentry = d1
7226 +               },
7227 +               {
7228 +                       .dentry = d2
7229 +               }
7230 +       };
7231 +
7232 +       return path_is_under(path + 0, path + 1);
7233 +}
7234 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7235 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7236 +++ linux/fs/aufs/dcsub.h       2020-01-27 10:57:18.168871450 +0100
7237 @@ -0,0 +1,137 @@
7238 +/* SPDX-License-Identifier: GPL-2.0 */
7239 +/*
7240 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7241 + *
7242 + * This program, aufs is free software; you can redistribute it and/or modify
7243 + * it under the terms of the GNU General Public License as published by
7244 + * the Free Software Foundation; either version 2 of the License, or
7245 + * (at your option) any later version.
7246 + *
7247 + * This program is distributed in the hope that it will be useful,
7248 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7249 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7250 + * GNU General Public License for more details.
7251 + *
7252 + * You should have received a copy of the GNU General Public License
7253 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7254 + */
7255 +
7256 +/*
7257 + * sub-routines for dentry cache
7258 + */
7259 +
7260 +#ifndef __AUFS_DCSUB_H__
7261 +#define __AUFS_DCSUB_H__
7262 +
7263 +#ifdef __KERNEL__
7264 +
7265 +#include <linux/dcache.h>
7266 +#include <linux/fs.h>
7267 +
7268 +struct au_dpage {
7269 +       int ndentry;
7270 +       struct dentry **dentries;
7271 +};
7272 +
7273 +struct au_dcsub_pages {
7274 +       int ndpage;
7275 +       struct au_dpage *dpages;
7276 +};
7277 +
7278 +/* ---------------------------------------------------------------------- */
7279 +
7280 +/* dcsub.c */
7281 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7282 +void au_dpages_free(struct au_dcsub_pages *dpages);
7283 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7284 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7285 +                  au_dpages_test test, void *arg);
7286 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7287 +                      int do_include, au_dpages_test test, void *arg);
7288 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7289 +                           struct dentry *dentry, int do_include);
7290 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7291 +
7292 +/* ---------------------------------------------------------------------- */
7293 +
7294 +/*
7295 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7296 + * include/linux/dcache.h. Try them (in the future).
7297 + */
7298 +
7299 +static inline int au_d_hashed_positive(struct dentry *d)
7300 +{
7301 +       int err;
7302 +       struct inode *inode = d_inode(d);
7303 +
7304 +       err = 0;
7305 +       if (unlikely(d_unhashed(d)
7306 +                    || d_is_negative(d)
7307 +                    || !inode->i_nlink))
7308 +               err = -ENOENT;
7309 +       return err;
7310 +}
7311 +
7312 +static inline int au_d_linkable(struct dentry *d)
7313 +{
7314 +       int err;
7315 +       struct inode *inode = d_inode(d);
7316 +
7317 +       err = au_d_hashed_positive(d);
7318 +       if (err
7319 +           && d_is_positive(d)
7320 +           && (inode->i_state & I_LINKABLE))
7321 +               err = 0;
7322 +       return err;
7323 +}
7324 +
7325 +static inline int au_d_alive(struct dentry *d)
7326 +{
7327 +       int err;
7328 +       struct inode *inode;
7329 +
7330 +       err = 0;
7331 +       if (!IS_ROOT(d))
7332 +               err = au_d_hashed_positive(d);
7333 +       else {
7334 +               inode = d_inode(d);
7335 +               if (unlikely(d_unlinked(d)
7336 +                            || d_is_negative(d)
7337 +                            || !inode->i_nlink))
7338 +                       err = -ENOENT;
7339 +       }
7340 +       return err;
7341 +}
7342 +
7343 +static inline int au_alive_dir(struct dentry *d)
7344 +{
7345 +       int err;
7346 +
7347 +       err = au_d_alive(d);
7348 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7349 +               err = -ENOENT;
7350 +       return err;
7351 +}
7352 +
7353 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7354 +{
7355 +       return a->len == b->len
7356 +               && !memcmp(a->name, b->name, a->len);
7357 +}
7358 +
7359 +/*
7360 + * by the commit
7361 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7362 + *                     taking d_lock
7363 + * the type of d_lockref.count became int, but the inlined function d_count()
7364 + * still returns unsigned int.
7365 + * I don't know why. Maybe it is for every d_count() users?
7366 + * Anyway au_dcount() lives on.
7367 + */
7368 +static inline int au_dcount(struct dentry *d)
7369 +{
7370 +       return (int)d_count(d);
7371 +}
7372 +
7373 +#endif /* __KERNEL__ */
7374 +#endif /* __AUFS_DCSUB_H__ */
7375 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7376 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7377 +++ linux/fs/aufs/debug.c       2020-01-27 10:57:18.168871450 +0100
7378 @@ -0,0 +1,441 @@
7379 +// SPDX-License-Identifier: GPL-2.0
7380 +/*
7381 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7382 + *
7383 + * This program, aufs is free software; you can redistribute it and/or modify
7384 + * it under the terms of the GNU General Public License as published by
7385 + * the Free Software Foundation; either version 2 of the License, or
7386 + * (at your option) any later version.
7387 + *
7388 + * This program is distributed in the hope that it will be useful,
7389 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7390 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7391 + * GNU General Public License for more details.
7392 + *
7393 + * You should have received a copy of the GNU General Public License
7394 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7395 + */
7396 +
7397 +/*
7398 + * debug print functions
7399 + */
7400 +
7401 +#include <linux/iversion.h>
7402 +#include "aufs.h"
7403 +
7404 +/* Returns 0, or -errno.  arg is in kp->arg. */
7405 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7406 +{
7407 +       int err, n;
7408 +
7409 +       err = kstrtoint(val, 0, &n);
7410 +       if (!err) {
7411 +               if (n > 0)
7412 +                       au_debug_on();
7413 +               else
7414 +                       au_debug_off();
7415 +       }
7416 +       return err;
7417 +}
7418 +
7419 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7420 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7421 +{
7422 +       atomic_t *a;
7423 +
7424 +       a = kp->arg;
7425 +       return sprintf(buffer, "%d", atomic_read(a));
7426 +}
7427 +
7428 +static struct kernel_param_ops param_ops_atomic_t = {
7429 +       .set = param_atomic_t_set,
7430 +       .get = param_atomic_t_get
7431 +       /* void (*free)(void *arg) */
7432 +};
7433 +
7434 +atomic_t aufs_debug = ATOMIC_INIT(0);
7435 +MODULE_PARM_DESC(debug, "debug print");
7436 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7437 +
7438 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7439 +char *au_plevel = KERN_DEBUG;
7440 +#define dpri(fmt, ...) do {                                    \
7441 +       if ((au_plevel                                          \
7442 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7443 +           || au_debug_test())                                 \
7444 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7445 +} while (0)
7446 +
7447 +/* ---------------------------------------------------------------------- */
7448 +
7449 +void au_dpri_whlist(struct au_nhash *whlist)
7450 +{
7451 +       unsigned long ul, n;
7452 +       struct hlist_head *head;
7453 +       struct au_vdir_wh *pos;
7454 +
7455 +       n = whlist->nh_num;
7456 +       head = whlist->nh_head;
7457 +       for (ul = 0; ul < n; ul++) {
7458 +               hlist_for_each_entry(pos, head, wh_hash)
7459 +                       dpri("b%d, %.*s, %d\n",
7460 +                            pos->wh_bindex,
7461 +                            pos->wh_str.len, pos->wh_str.name,
7462 +                            pos->wh_str.len);
7463 +               head++;
7464 +       }
7465 +}
7466 +
7467 +void au_dpri_vdir(struct au_vdir *vdir)
7468 +{
7469 +       unsigned long ul;
7470 +       union au_vdir_deblk_p p;
7471 +       unsigned char *o;
7472 +
7473 +       if (!vdir || IS_ERR(vdir)) {
7474 +               dpri("err %ld\n", PTR_ERR(vdir));
7475 +               return;
7476 +       }
7477 +
7478 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7479 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7480 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7481 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7482 +               p.deblk = vdir->vd_deblk[ul];
7483 +               o = p.deblk;
7484 +               dpri("[%lu]: %p\n", ul, o);
7485 +       }
7486 +}
7487 +
7488 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7489 +                       struct dentry *wh)
7490 +{
7491 +       char *n = NULL;
7492 +       int l = 0;
7493 +
7494 +       if (!inode || IS_ERR(inode)) {
7495 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7496 +               return -1;
7497 +       }
7498 +
7499 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7500 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7501 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7502 +       if (wh) {
7503 +               n = (void *)wh->d_name.name;
7504 +               l = wh->d_name.len;
7505 +       }
7506 +
7507 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7508 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7509 +            bindex, inode,
7510 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7511 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7512 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7513 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7514 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7515 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7516 +            inode->i_generation,
7517 +            l ? ", wh " : "", l, n);
7518 +       return 0;
7519 +}
7520 +
7521 +void au_dpri_inode(struct inode *inode)
7522 +{
7523 +       struct au_iinfo *iinfo;
7524 +       struct au_hinode *hi;
7525 +       aufs_bindex_t bindex;
7526 +       int err, hn;
7527 +
7528 +       err = do_pri_inode(-1, inode, -1, NULL);
7529 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7530 +               return;
7531 +
7532 +       iinfo = au_ii(inode);
7533 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7534 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7535 +       if (iinfo->ii_btop < 0)
7536 +               return;
7537 +       hn = 0;
7538 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7539 +               hi = au_hinode(iinfo, bindex);
7540 +               hn = !!au_hn(hi);
7541 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7542 +       }
7543 +}
7544 +
7545 +void au_dpri_dalias(struct inode *inode)
7546 +{
7547 +       struct dentry *d;
7548 +
7549 +       spin_lock(&inode->i_lock);
7550 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7551 +               au_dpri_dentry(d);
7552 +       spin_unlock(&inode->i_lock);
7553 +}
7554 +
7555 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7556 +{
7557 +       struct dentry *wh = NULL;
7558 +       int hn;
7559 +       struct inode *inode;
7560 +       struct au_iinfo *iinfo;
7561 +       struct au_hinode *hi;
7562 +
7563 +       if (!dentry || IS_ERR(dentry)) {
7564 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7565 +               return -1;
7566 +       }
7567 +       /* do not call dget_parent() here */
7568 +       /* note: access d_xxx without d_lock */
7569 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7570 +            bindex, dentry, dentry,
7571 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7572 +            au_dcount(dentry), dentry->d_flags,
7573 +            d_unhashed(dentry) ? "un" : "");
7574 +       hn = -1;
7575 +       inode = NULL;
7576 +       if (d_is_positive(dentry))
7577 +               inode = d_inode(dentry);
7578 +       if (inode
7579 +           && au_test_aufs(dentry->d_sb)
7580 +           && bindex >= 0
7581 +           && !au_is_bad_inode(inode)) {
7582 +               iinfo = au_ii(inode);
7583 +               hi = au_hinode(iinfo, bindex);
7584 +               hn = !!au_hn(hi);
7585 +               wh = hi->hi_whdentry;
7586 +       }
7587 +       do_pri_inode(bindex, inode, hn, wh);
7588 +       return 0;
7589 +}
7590 +
7591 +void au_dpri_dentry(struct dentry *dentry)
7592 +{
7593 +       struct au_dinfo *dinfo;
7594 +       aufs_bindex_t bindex;
7595 +       int err;
7596 +
7597 +       err = do_pri_dentry(-1, dentry);
7598 +       if (err || !au_test_aufs(dentry->d_sb))
7599 +               return;
7600 +
7601 +       dinfo = au_di(dentry);
7602 +       if (!dinfo)
7603 +               return;
7604 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7605 +            dinfo->di_btop, dinfo->di_bbot,
7606 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7607 +            dinfo->di_tmpfile);
7608 +       if (dinfo->di_btop < 0)
7609 +               return;
7610 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7611 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7612 +}
7613 +
7614 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7615 +{
7616 +       char a[32];
7617 +
7618 +       if (!file || IS_ERR(file)) {
7619 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7620 +               return -1;
7621 +       }
7622 +       a[0] = 0;
7623 +       if (bindex < 0
7624 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7625 +           && au_test_aufs(file->f_path.dentry->d_sb)
7626 +           && au_fi(file))
7627 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7628 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7629 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7630 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7631 +            file->f_version, file->f_pos, a);
7632 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7633 +               do_pri_dentry(bindex, file->f_path.dentry);
7634 +       return 0;
7635 +}
7636 +
7637 +void au_dpri_file(struct file *file)
7638 +{
7639 +       struct au_finfo *finfo;
7640 +       struct au_fidir *fidir;
7641 +       struct au_hfile *hfile;
7642 +       aufs_bindex_t bindex;
7643 +       int err;
7644 +
7645 +       err = do_pri_file(-1, file);
7646 +       if (err
7647 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7648 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7649 +               return;
7650 +
7651 +       finfo = au_fi(file);
7652 +       if (!finfo)
7653 +               return;
7654 +       if (finfo->fi_btop < 0)
7655 +               return;
7656 +       fidir = finfo->fi_hdir;
7657 +       if (!fidir)
7658 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7659 +       else
7660 +               for (bindex = finfo->fi_btop;
7661 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7662 +                    bindex++) {
7663 +                       hfile = fidir->fd_hfile + bindex;
7664 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7665 +               }
7666 +}
7667 +
7668 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7669 +{
7670 +       struct vfsmount *mnt;
7671 +       struct super_block *sb;
7672 +
7673 +       if (!br || IS_ERR(br))
7674 +               goto out;
7675 +       mnt = au_br_mnt(br);
7676 +       if (!mnt || IS_ERR(mnt))
7677 +               goto out;
7678 +       sb = mnt->mnt_sb;
7679 +       if (!sb || IS_ERR(sb))
7680 +               goto out;
7681 +
7682 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7683 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7684 +            "xino %d\n",
7685 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7686 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7687 +            sb->s_flags, sb->s_count,
7688 +            atomic_read(&sb->s_active),
7689 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7690 +       return 0;
7691 +
7692 +out:
7693 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7694 +       return -1;
7695 +}
7696 +
7697 +void au_dpri_sb(struct super_block *sb)
7698 +{
7699 +       struct au_sbinfo *sbinfo;
7700 +       aufs_bindex_t bindex;
7701 +       int err;
7702 +       /* to reduce stack size */
7703 +       struct {
7704 +               struct vfsmount mnt;
7705 +               struct au_branch fake;
7706 +       } *a;
7707 +
7708 +       /* this function can be called from magic sysrq */
7709 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7710 +       if (unlikely(!a)) {
7711 +               dpri("no memory\n");
7712 +               return;
7713 +       }
7714 +
7715 +       a->mnt.mnt_sb = sb;
7716 +       a->fake.br_path.mnt = &a->mnt;
7717 +       err = do_pri_br(-1, &a->fake);
7718 +       au_kfree_rcu(a);
7719 +       dpri("dev 0x%x\n", sb->s_dev);
7720 +       if (err || !au_test_aufs(sb))
7721 +               return;
7722 +
7723 +       sbinfo = au_sbi(sb);
7724 +       if (!sbinfo)
7725 +               return;
7726 +       dpri("nw %d, gen %u, kobj %d\n",
7727 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7728 +            kref_read(&sbinfo->si_kobj.kref));
7729 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7730 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7731 +}
7732 +
7733 +/* ---------------------------------------------------------------------- */
7734 +
7735 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7736 +{
7737 +       struct inode *h_inode, *inode = d_inode(dentry);
7738 +       struct dentry *h_dentry;
7739 +       aufs_bindex_t bindex, bbot, bi;
7740 +
7741 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7742 +               return;
7743 +
7744 +       bbot = au_dbbot(dentry);
7745 +       bi = au_ibbot(inode);
7746 +       if (bi < bbot)
7747 +               bbot = bi;
7748 +       bindex = au_dbtop(dentry);
7749 +       bi = au_ibtop(inode);
7750 +       if (bi > bindex)
7751 +               bindex = bi;
7752 +
7753 +       for (; bindex <= bbot; bindex++) {
7754 +               h_dentry = au_h_dptr(dentry, bindex);
7755 +               if (!h_dentry)
7756 +                       continue;
7757 +               h_inode = au_h_iptr(inode, bindex);
7758 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7759 +                       au_debug_on();
7760 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7761 +                       AuDbgDentry(dentry);
7762 +                       AuDbgInode(inode);
7763 +                       au_debug_off();
7764 +                       BUG();
7765 +               }
7766 +       }
7767 +}
7768 +
7769 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7770 +{
7771 +       int err, i, j;
7772 +       struct au_dcsub_pages dpages;
7773 +       struct au_dpage *dpage;
7774 +       struct dentry **dentries;
7775 +
7776 +       err = au_dpages_init(&dpages, GFP_NOFS);
7777 +       AuDebugOn(err);
7778 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7779 +       AuDebugOn(err);
7780 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7781 +               dpage = dpages.dpages + i;
7782 +               dentries = dpage->dentries;
7783 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7784 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7785 +       }
7786 +       au_dpages_free(&dpages);
7787 +}
7788 +
7789 +void au_dbg_verify_kthread(void)
7790 +{
7791 +       if (au_wkq_test()) {
7792 +               au_dbg_blocked();
7793 +               /*
7794 +                * It may be recursive, but udba=notify between two aufs mounts,
7795 +                * where a single ro branch is shared, is not a problem.
7796 +                */
7797 +               /* WARN_ON(1); */
7798 +       }
7799 +}
7800 +
7801 +/* ---------------------------------------------------------------------- */
7802 +
7803 +int __init au_debug_init(void)
7804 +{
7805 +       aufs_bindex_t bindex;
7806 +       struct au_vdir_destr destr;
7807 +
7808 +       bindex = -1;
7809 +       AuDebugOn(bindex >= 0);
7810 +
7811 +       destr.len = -1;
7812 +       AuDebugOn(destr.len < NAME_MAX);
7813 +
7814 +#ifdef CONFIG_4KSTACKS
7815 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7816 +#endif
7817 +
7818 +       return 0;
7819 +}
7820 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7821 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7822 +++ linux/fs/aufs/debug.h       2020-01-27 10:57:18.168871450 +0100
7823 @@ -0,0 +1,226 @@
7824 +/* SPDX-License-Identifier: GPL-2.0 */
7825 +/*
7826 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7827 + *
7828 + * This program, aufs is free software; you can redistribute it and/or modify
7829 + * it under the terms of the GNU General Public License as published by
7830 + * the Free Software Foundation; either version 2 of the License, or
7831 + * (at your option) any later version.
7832 + *
7833 + * This program is distributed in the hope that it will be useful,
7834 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7835 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7836 + * GNU General Public License for more details.
7837 + *
7838 + * You should have received a copy of the GNU General Public License
7839 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7840 + */
7841 +
7842 +/*
7843 + * debug print functions
7844 + */
7845 +
7846 +#ifndef __AUFS_DEBUG_H__
7847 +#define __AUFS_DEBUG_H__
7848 +
7849 +#ifdef __KERNEL__
7850 +
7851 +#include <linux/atomic.h>
7852 +#include <linux/module.h>
7853 +#include <linux/kallsyms.h>
7854 +#include <linux/sysrq.h>
7855 +
7856 +#ifdef CONFIG_AUFS_DEBUG
7857 +#define AuDebugOn(a)           BUG_ON(a)
7858 +
7859 +/* module parameter */
7860 +extern atomic_t aufs_debug;
7861 +static inline void au_debug_on(void)
7862 +{
7863 +       atomic_inc(&aufs_debug);
7864 +}
7865 +static inline void au_debug_off(void)
7866 +{
7867 +       atomic_dec_if_positive(&aufs_debug);
7868 +}
7869 +
7870 +static inline int au_debug_test(void)
7871 +{
7872 +       return atomic_read(&aufs_debug) > 0;
7873 +}
7874 +#else
7875 +#define AuDebugOn(a)           do {} while (0)
7876 +AuStubVoid(au_debug_on, void)
7877 +AuStubVoid(au_debug_off, void)
7878 +AuStubInt0(au_debug_test, void)
7879 +#endif /* CONFIG_AUFS_DEBUG */
7880 +
7881 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7882 +
7883 +/* ---------------------------------------------------------------------- */
7884 +
7885 +/* debug print */
7886 +
7887 +#define AuDbg(fmt, ...) do { \
7888 +       if (au_debug_test()) \
7889 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7890 +} while (0)
7891 +#define AuLabel(l)             AuDbg(#l "\n")
7892 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7893 +#define AuWarn1(fmt, ...) do { \
7894 +       static unsigned char _c; \
7895 +       if (!_c++) \
7896 +               pr_warn(fmt, ##__VA_ARGS__); \
7897 +} while (0)
7898 +
7899 +#define AuErr1(fmt, ...) do { \
7900 +       static unsigned char _c; \
7901 +       if (!_c++) \
7902 +               pr_err(fmt, ##__VA_ARGS__); \
7903 +} while (0)
7904 +
7905 +#define AuIOErr1(fmt, ...) do { \
7906 +       static unsigned char _c; \
7907 +       if (!_c++) \
7908 +               AuIOErr(fmt, ##__VA_ARGS__); \
7909 +} while (0)
7910 +
7911 +#define AuUnsupportMsg "This operation is not supported." \
7912 +                       " Please report this application to aufs-users ML."
7913 +#define AuUnsupport(fmt, ...) do { \
7914 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7915 +       dump_stack(); \
7916 +} while (0)
7917 +
7918 +#define AuTraceErr(e) do { \
7919 +       if (unlikely((e) < 0)) \
7920 +               AuDbg("err %d\n", (int)(e)); \
7921 +} while (0)
7922 +
7923 +#define AuTraceErrPtr(p) do { \
7924 +       if (IS_ERR(p)) \
7925 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7926 +} while (0)
7927 +
7928 +/* dirty macros for debug print, use with "%.*s" and caution */
7929 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7930 +
7931 +/* ---------------------------------------------------------------------- */
7932 +
7933 +struct dentry;
7934 +#ifdef CONFIG_AUFS_DEBUG
7935 +extern struct mutex au_dbg_mtx;
7936 +extern char *au_plevel;
7937 +struct au_nhash;
7938 +void au_dpri_whlist(struct au_nhash *whlist);
7939 +struct au_vdir;
7940 +void au_dpri_vdir(struct au_vdir *vdir);
7941 +struct inode;
7942 +void au_dpri_inode(struct inode *inode);
7943 +void au_dpri_dalias(struct inode *inode);
7944 +void au_dpri_dentry(struct dentry *dentry);
7945 +struct file;
7946 +void au_dpri_file(struct file *filp);
7947 +struct super_block;
7948 +void au_dpri_sb(struct super_block *sb);
7949 +
7950 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7951 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7952 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7953 +void au_dbg_verify_kthread(void);
7954 +
7955 +int __init au_debug_init(void);
7956 +
7957 +#define AuDbgWhlist(w) do { \
7958 +       mutex_lock(&au_dbg_mtx); \
7959 +       AuDbg(#w "\n"); \
7960 +       au_dpri_whlist(w); \
7961 +       mutex_unlock(&au_dbg_mtx); \
7962 +} while (0)
7963 +
7964 +#define AuDbgVdir(v) do { \
7965 +       mutex_lock(&au_dbg_mtx); \
7966 +       AuDbg(#v "\n"); \
7967 +       au_dpri_vdir(v); \
7968 +       mutex_unlock(&au_dbg_mtx); \
7969 +} while (0)
7970 +
7971 +#define AuDbgInode(i) do { \
7972 +       mutex_lock(&au_dbg_mtx); \
7973 +       AuDbg(#i "\n"); \
7974 +       au_dpri_inode(i); \
7975 +       mutex_unlock(&au_dbg_mtx); \
7976 +} while (0)
7977 +
7978 +#define AuDbgDAlias(i) do { \
7979 +       mutex_lock(&au_dbg_mtx); \
7980 +       AuDbg(#i "\n"); \
7981 +       au_dpri_dalias(i); \
7982 +       mutex_unlock(&au_dbg_mtx); \
7983 +} while (0)
7984 +
7985 +#define AuDbgDentry(d) do { \
7986 +       mutex_lock(&au_dbg_mtx); \
7987 +       AuDbg(#d "\n"); \
7988 +       au_dpri_dentry(d); \
7989 +       mutex_unlock(&au_dbg_mtx); \
7990 +} while (0)
7991 +
7992 +#define AuDbgFile(f) do { \
7993 +       mutex_lock(&au_dbg_mtx); \
7994 +       AuDbg(#f "\n"); \
7995 +       au_dpri_file(f); \
7996 +       mutex_unlock(&au_dbg_mtx); \
7997 +} while (0)
7998 +
7999 +#define AuDbgSb(sb) do { \
8000 +       mutex_lock(&au_dbg_mtx); \
8001 +       AuDbg(#sb "\n"); \
8002 +       au_dpri_sb(sb); \
8003 +       mutex_unlock(&au_dbg_mtx); \
8004 +} while (0)
8005 +
8006 +#define AuDbgSym(addr) do {                            \
8007 +       char sym[KSYM_SYMBOL_LEN];                      \
8008 +       sprint_symbol(sym, (unsigned long)addr);        \
8009 +       AuDbg("%s\n", sym);                             \
8010 +} while (0)
8011 +#else
8012 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8013 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8014 +AuStubVoid(au_dbg_verify_kthread, void)
8015 +AuStubInt0(__init au_debug_init, void)
8016 +
8017 +#define AuDbgWhlist(w)         do {} while (0)
8018 +#define AuDbgVdir(v)           do {} while (0)
8019 +#define AuDbgInode(i)          do {} while (0)
8020 +#define AuDbgDAlias(i)         do {} while (0)
8021 +#define AuDbgDentry(d)         do {} while (0)
8022 +#define AuDbgFile(f)           do {} while (0)
8023 +#define AuDbgSb(sb)            do {} while (0)
8024 +#define AuDbgSym(addr)         do {} while (0)
8025 +#endif /* CONFIG_AUFS_DEBUG */
8026 +
8027 +/* ---------------------------------------------------------------------- */
8028 +
8029 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8030 +int __init au_sysrq_init(void);
8031 +void au_sysrq_fin(void);
8032 +
8033 +#ifdef CONFIG_HW_CONSOLE
8034 +#define au_dbg_blocked() do { \
8035 +       WARN_ON(1); \
8036 +       handle_sysrq('w'); \
8037 +} while (0)
8038 +#else
8039 +AuStubVoid(au_dbg_blocked, void)
8040 +#endif
8041 +
8042 +#else
8043 +AuStubInt0(__init au_sysrq_init, void)
8044 +AuStubVoid(au_sysrq_fin, void)
8045 +AuStubVoid(au_dbg_blocked, void)
8046 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8047 +
8048 +#endif /* __KERNEL__ */
8049 +#endif /* __AUFS_DEBUG_H__ */
8050 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8051 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8052 +++ linux/fs/aufs/dentry.c      2020-01-27 10:57:18.168871450 +0100
8053 @@ -0,0 +1,1154 @@
8054 +// SPDX-License-Identifier: GPL-2.0
8055 +/*
8056 + * Copyright (C) 2005-2020 Junjiro R. Okajima
8057 + *
8058 + * This program, aufs is free software; you can redistribute it and/or modify
8059 + * it under the terms of the GNU General Public License as published by
8060 + * the Free Software Foundation; either version 2 of the License, or
8061 + * (at your option) any later version.
8062 + *
8063 + * This program is distributed in the hope that it will be useful,
8064 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8065 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8066 + * GNU General Public License for more details.
8067 + *
8068 + * You should have received a copy of the GNU General Public License
8069 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8070 + */
8071 +
8072 +/*
8073 + * lookup and dentry operations
8074 + */
8075 +
8076 +#include <linux/iversion.h>
8077 +#include <linux/namei.h>
8078 +#include "aufs.h"
8079 +
8080 +/*
8081 + * returns positive/negative dentry, NULL or an error.
8082 + * NULL means whiteout-ed or not-found.
8083 + */
8084 +static struct dentry*
8085 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8086 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8087 +{
8088 +       struct dentry *h_dentry;
8089 +       struct inode *h_inode;
8090 +       struct au_branch *br;
8091 +       int wh_found, opq;
8092 +       unsigned char wh_able;
8093 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8094 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8095 +                                                         IGNORE_PERM);
8096 +
8097 +       wh_found = 0;
8098 +       br = au_sbr(dentry->d_sb, bindex);
8099 +       wh_able = !!au_br_whable(br->br_perm);
8100 +       if (wh_able)
8101 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8102 +       h_dentry = ERR_PTR(wh_found);
8103 +       if (!wh_found)
8104 +               goto real_lookup;
8105 +       if (unlikely(wh_found < 0))
8106 +               goto out;
8107 +
8108 +       /* We found a whiteout */
8109 +       /* au_set_dbbot(dentry, bindex); */
8110 +       au_set_dbwh(dentry, bindex);
8111 +       if (!allow_neg)
8112 +               return NULL; /* success */
8113 +
8114 +real_lookup:
8115 +       if (!ignore_perm)
8116 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8117 +       else
8118 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8119 +       if (IS_ERR(h_dentry)) {
8120 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8121 +                   && !allow_neg)
8122 +                       h_dentry = NULL;
8123 +               goto out;
8124 +       }
8125 +
8126 +       h_inode = d_inode(h_dentry);
8127 +       if (d_is_negative(h_dentry)) {
8128 +               if (!allow_neg)
8129 +                       goto out_neg;
8130 +       } else if (wh_found
8131 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8132 +               goto out_neg;
8133 +       else if (au_ftest_lkup(args->flags, DIRREN)
8134 +                /* && h_inode */
8135 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8136 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8137 +                     (unsigned long long)h_inode->i_ino);
8138 +               goto out_neg;
8139 +       }
8140 +
8141 +       if (au_dbbot(dentry) <= bindex)
8142 +               au_set_dbbot(dentry, bindex);
8143 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8144 +               au_set_dbtop(dentry, bindex);
8145 +       au_set_h_dptr(dentry, bindex, h_dentry);
8146 +
8147 +       if (!d_is_dir(h_dentry)
8148 +           || !wh_able
8149 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8150 +               goto out; /* success */
8151 +
8152 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8153 +       opq = au_diropq_test(h_dentry);
8154 +       inode_unlock_shared(h_inode);
8155 +       if (opq > 0)
8156 +               au_set_dbdiropq(dentry, bindex);
8157 +       else if (unlikely(opq < 0)) {
8158 +               au_set_h_dptr(dentry, bindex, NULL);
8159 +               h_dentry = ERR_PTR(opq);
8160 +       }
8161 +       goto out;
8162 +
8163 +out_neg:
8164 +       dput(h_dentry);
8165 +       h_dentry = NULL;
8166 +out:
8167 +       return h_dentry;
8168 +}
8169 +
8170 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8171 +{
8172 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8173 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8174 +               return -EPERM;
8175 +       return 0;
8176 +}
8177 +
8178 +/*
8179 + * returns the number of lower positive dentries,
8180 + * otherwise an error.
8181 + * can be called at unlinking with @type is zero.
8182 + */
8183 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8184 +                  unsigned int flags)
8185 +{
8186 +       int npositive, err;
8187 +       aufs_bindex_t bindex, btail, bdiropq;
8188 +       unsigned char isdir, dirperm1, dirren;
8189 +       struct au_do_lookup_args args = {
8190 +               .flags          = flags,
8191 +               .name           = &dentry->d_name
8192 +       };
8193 +       struct dentry *parent;
8194 +       struct super_block *sb;
8195 +
8196 +       sb = dentry->d_sb;
8197 +       err = au_test_shwh(sb, args.name);
8198 +       if (unlikely(err))
8199 +               goto out;
8200 +
8201 +       err = au_wh_name_alloc(&args.whname, args.name);
8202 +       if (unlikely(err))
8203 +               goto out;
8204 +
8205 +       isdir = !!d_is_dir(dentry);
8206 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8207 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8208 +       if (dirren)
8209 +               au_fset_lkup(args.flags, DIRREN);
8210 +
8211 +       npositive = 0;
8212 +       parent = dget_parent(dentry);
8213 +       btail = au_dbtaildir(parent);
8214 +       for (bindex = btop; bindex <= btail; bindex++) {
8215 +               struct dentry *h_parent, *h_dentry;
8216 +               struct inode *h_inode, *h_dir;
8217 +               struct au_branch *br;
8218 +
8219 +               h_dentry = au_h_dptr(dentry, bindex);
8220 +               if (h_dentry) {
8221 +                       if (d_is_positive(h_dentry))
8222 +                               npositive++;
8223 +                       break;
8224 +               }
8225 +               h_parent = au_h_dptr(parent, bindex);
8226 +               if (!h_parent || !d_is_dir(h_parent))
8227 +                       continue;
8228 +
8229 +               if (dirren) {
8230 +                       /* if the inum matches, then use the prepared name */
8231 +                       err = au_dr_lkup_name(&args, bindex);
8232 +                       if (unlikely(err))
8233 +                               goto out_parent;
8234 +               }
8235 +
8236 +               h_dir = d_inode(h_parent);
8237 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8238 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8239 +               inode_unlock_shared(h_dir);
8240 +               err = PTR_ERR(h_dentry);
8241 +               if (IS_ERR(h_dentry))
8242 +                       goto out_parent;
8243 +               if (h_dentry)
8244 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8245 +               if (dirperm1)
8246 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8247 +
8248 +               if (au_dbwh(dentry) == bindex)
8249 +                       break;
8250 +               if (!h_dentry)
8251 +                       continue;
8252 +               if (d_is_negative(h_dentry))
8253 +                       continue;
8254 +               h_inode = d_inode(h_dentry);
8255 +               npositive++;
8256 +               if (!args.type)
8257 +                       args.type = h_inode->i_mode & S_IFMT;
8258 +               if (args.type != S_IFDIR)
8259 +                       break;
8260 +               else if (isdir) {
8261 +                       /* the type of lower may be different */
8262 +                       bdiropq = au_dbdiropq(dentry);
8263 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8264 +                               break;
8265 +               }
8266 +               br = au_sbr(sb, bindex);
8267 +               if (dirren
8268 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8269 +                                          /*add_ent*/NULL)) {
8270 +                       /* prepare next name to lookup */
8271 +                       err = au_dr_lkup(&args, dentry, bindex);
8272 +                       if (unlikely(err))
8273 +                               goto out_parent;
8274 +               }
8275 +       }
8276 +
8277 +       if (npositive) {
8278 +               AuLabel(positive);
8279 +               au_update_dbtop(dentry);
8280 +       }
8281 +       err = npositive;
8282 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8283 +                    && au_dbtop(dentry) < 0)) {
8284 +               err = -EIO;
8285 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8286 +                       dentry, err);
8287 +       }
8288 +
8289 +out_parent:
8290 +       dput(parent);
8291 +       au_kfree_try_rcu(args.whname.name);
8292 +       if (dirren)
8293 +               au_dr_lkup_fin(&args);
8294 +out:
8295 +       return err;
8296 +}
8297 +
8298 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8299 +{
8300 +       struct dentry *dentry;
8301 +       int wkq_err;
8302 +
8303 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8304 +               dentry = vfsub_lkup_one(name, parent);
8305 +       else {
8306 +               struct vfsub_lkup_one_args args = {
8307 +                       .errp   = &dentry,
8308 +                       .name   = name,
8309 +                       .parent = parent
8310 +               };
8311 +
8312 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8313 +               if (unlikely(wkq_err))
8314 +                       dentry = ERR_PTR(wkq_err);
8315 +       }
8316 +
8317 +       return dentry;
8318 +}
8319 +
8320 +/*
8321 + * lookup @dentry on @bindex which should be negative.
8322 + */
8323 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8324 +{
8325 +       int err;
8326 +       struct dentry *parent, *h_parent, *h_dentry;
8327 +       struct au_branch *br;
8328 +
8329 +       parent = dget_parent(dentry);
8330 +       h_parent = au_h_dptr(parent, bindex);
8331 +       br = au_sbr(dentry->d_sb, bindex);
8332 +       if (wh)
8333 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8334 +       else
8335 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8336 +       err = PTR_ERR(h_dentry);
8337 +       if (IS_ERR(h_dentry))
8338 +               goto out;
8339 +       if (unlikely(d_is_positive(h_dentry))) {
8340 +               err = -EIO;
8341 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8342 +               dput(h_dentry);
8343 +               goto out;
8344 +       }
8345 +
8346 +       err = 0;
8347 +       if (bindex < au_dbtop(dentry))
8348 +               au_set_dbtop(dentry, bindex);
8349 +       if (au_dbbot(dentry) < bindex)
8350 +               au_set_dbbot(dentry, bindex);
8351 +       au_set_h_dptr(dentry, bindex, h_dentry);
8352 +
8353 +out:
8354 +       dput(parent);
8355 +       return err;
8356 +}
8357 +
8358 +/* ---------------------------------------------------------------------- */
8359 +
8360 +/* subset of struct inode */
8361 +struct au_iattr {
8362 +       unsigned long           i_ino;
8363 +       /* unsigned int         i_nlink; */
8364 +       kuid_t                  i_uid;
8365 +       kgid_t                  i_gid;
8366 +       u64                     i_version;
8367 +/*
8368 +       loff_t                  i_size;
8369 +       blkcnt_t                i_blocks;
8370 +*/
8371 +       umode_t                 i_mode;
8372 +};
8373 +
8374 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8375 +{
8376 +       ia->i_ino = h_inode->i_ino;
8377 +       /* ia->i_nlink = h_inode->i_nlink; */
8378 +       ia->i_uid = h_inode->i_uid;
8379 +       ia->i_gid = h_inode->i_gid;
8380 +       ia->i_version = inode_query_iversion(h_inode);
8381 +/*
8382 +       ia->i_size = h_inode->i_size;
8383 +       ia->i_blocks = h_inode->i_blocks;
8384 +*/
8385 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8386 +}
8387 +
8388 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8389 +{
8390 +       return ia->i_ino != h_inode->i_ino
8391 +               /* || ia->i_nlink != h_inode->i_nlink */
8392 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8393 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8394 +               || !inode_eq_iversion(h_inode, ia->i_version)
8395 +/*
8396 +               || ia->i_size != h_inode->i_size
8397 +               || ia->i_blocks != h_inode->i_blocks
8398 +*/
8399 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8400 +}
8401 +
8402 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8403 +                             struct au_branch *br)
8404 +{
8405 +       int err;
8406 +       struct au_iattr ia;
8407 +       struct inode *h_inode;
8408 +       struct dentry *h_d;
8409 +       struct super_block *h_sb;
8410 +
8411 +       err = 0;
8412 +       memset(&ia, -1, sizeof(ia));
8413 +       h_sb = h_dentry->d_sb;
8414 +       h_inode = NULL;
8415 +       if (d_is_positive(h_dentry)) {
8416 +               h_inode = d_inode(h_dentry);
8417 +               au_iattr_save(&ia, h_inode);
8418 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8419 +               /* nfs d_revalidate may return 0 for negative dentry */
8420 +               /* fuse d_revalidate always return 0 for negative dentry */
8421 +               goto out;
8422 +
8423 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8424 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8425 +       err = PTR_ERR(h_d);
8426 +       if (IS_ERR(h_d))
8427 +               goto out;
8428 +
8429 +       err = 0;
8430 +       if (unlikely(h_d != h_dentry
8431 +                    || d_inode(h_d) != h_inode
8432 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8433 +               err = au_busy_or_stale();
8434 +       dput(h_d);
8435 +
8436 +out:
8437 +       AuTraceErr(err);
8438 +       return err;
8439 +}
8440 +
8441 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8442 +               struct dentry *h_parent, struct au_branch *br)
8443 +{
8444 +       int err;
8445 +
8446 +       err = 0;
8447 +       if (udba == AuOpt_UDBA_REVAL
8448 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8449 +               IMustLock(h_dir);
8450 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8451 +       } else if (udba != AuOpt_UDBA_NONE)
8452 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8453 +
8454 +       return err;
8455 +}
8456 +
8457 +/* ---------------------------------------------------------------------- */
8458 +
8459 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8460 +{
8461 +       int err;
8462 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8463 +       struct au_hdentry tmp, *p, *q;
8464 +       struct au_dinfo *dinfo;
8465 +       struct super_block *sb;
8466 +
8467 +       DiMustWriteLock(dentry);
8468 +
8469 +       sb = dentry->d_sb;
8470 +       dinfo = au_di(dentry);
8471 +       bbot = dinfo->di_bbot;
8472 +       bwh = dinfo->di_bwh;
8473 +       bdiropq = dinfo->di_bdiropq;
8474 +       bindex = dinfo->di_btop;
8475 +       p = au_hdentry(dinfo, bindex);
8476 +       for (; bindex <= bbot; bindex++, p++) {
8477 +               if (!p->hd_dentry)
8478 +                       continue;
8479 +
8480 +               new_bindex = au_br_index(sb, p->hd_id);
8481 +               if (new_bindex == bindex)
8482 +                       continue;
8483 +
8484 +               if (dinfo->di_bwh == bindex)
8485 +                       bwh = new_bindex;
8486 +               if (dinfo->di_bdiropq == bindex)
8487 +                       bdiropq = new_bindex;
8488 +               if (new_bindex < 0) {
8489 +                       au_hdput(p);
8490 +                       p->hd_dentry = NULL;
8491 +                       continue;
8492 +               }
8493 +
8494 +               /* swap two lower dentries, and loop again */
8495 +               q = au_hdentry(dinfo, new_bindex);
8496 +               tmp = *q;
8497 +               *q = *p;
8498 +               *p = tmp;
8499 +               if (tmp.hd_dentry) {
8500 +                       bindex--;
8501 +                       p--;
8502 +               }
8503 +       }
8504 +
8505 +       dinfo->di_bwh = -1;
8506 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8507 +               dinfo->di_bwh = bwh;
8508 +
8509 +       dinfo->di_bdiropq = -1;
8510 +       if (bdiropq >= 0
8511 +           && bdiropq <= au_sbbot(sb)
8512 +           && au_sbr_whable(sb, bdiropq))
8513 +               dinfo->di_bdiropq = bdiropq;
8514 +
8515 +       err = -EIO;
8516 +       dinfo->di_btop = -1;
8517 +       dinfo->di_bbot = -1;
8518 +       bbot = au_dbbot(parent);
8519 +       bindex = 0;
8520 +       p = au_hdentry(dinfo, bindex);
8521 +       for (; bindex <= bbot; bindex++, p++)
8522 +               if (p->hd_dentry) {
8523 +                       dinfo->di_btop = bindex;
8524 +                       break;
8525 +               }
8526 +
8527 +       if (dinfo->di_btop >= 0) {
8528 +               bindex = bbot;
8529 +               p = au_hdentry(dinfo, bindex);
8530 +               for (; bindex >= 0; bindex--, p--)
8531 +                       if (p->hd_dentry) {
8532 +                               dinfo->di_bbot = bindex;
8533 +                               err = 0;
8534 +                               break;
8535 +                       }
8536 +       }
8537 +
8538 +       return err;
8539 +}
8540 +
8541 +static void au_do_hide(struct dentry *dentry)
8542 +{
8543 +       struct inode *inode;
8544 +
8545 +       if (d_really_is_positive(dentry)) {
8546 +               inode = d_inode(dentry);
8547 +               if (!d_is_dir(dentry)) {
8548 +                       if (inode->i_nlink && !d_unhashed(dentry))
8549 +                               drop_nlink(inode);
8550 +               } else {
8551 +                       clear_nlink(inode);
8552 +                       /* stop next lookup */
8553 +                       inode->i_flags |= S_DEAD;
8554 +               }
8555 +               smp_mb(); /* necessary? */
8556 +       }
8557 +       d_drop(dentry);
8558 +}
8559 +
8560 +static int au_hide_children(struct dentry *parent)
8561 +{
8562 +       int err, i, j, ndentry;
8563 +       struct au_dcsub_pages dpages;
8564 +       struct au_dpage *dpage;
8565 +       struct dentry *dentry;
8566 +
8567 +       err = au_dpages_init(&dpages, GFP_NOFS);
8568 +       if (unlikely(err))
8569 +               goto out;
8570 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8571 +       if (unlikely(err))
8572 +               goto out_dpages;
8573 +
8574 +       /* in reverse order */
8575 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8576 +               dpage = dpages.dpages + i;
8577 +               ndentry = dpage->ndentry;
8578 +               for (j = ndentry - 1; j >= 0; j--) {
8579 +                       dentry = dpage->dentries[j];
8580 +                       if (dentry != parent)
8581 +                               au_do_hide(dentry);
8582 +               }
8583 +       }
8584 +
8585 +out_dpages:
8586 +       au_dpages_free(&dpages);
8587 +out:
8588 +       return err;
8589 +}
8590 +
8591 +static void au_hide(struct dentry *dentry)
8592 +{
8593 +       int err;
8594 +
8595 +       AuDbgDentry(dentry);
8596 +       if (d_is_dir(dentry)) {
8597 +               /* shrink_dcache_parent(dentry); */
8598 +               err = au_hide_children(dentry);
8599 +               if (unlikely(err))
8600 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8601 +                               dentry, err);
8602 +       }
8603 +       au_do_hide(dentry);
8604 +}
8605 +
8606 +/*
8607 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8608 + *
8609 + * a dirty branch is added
8610 + * - on the top of layers
8611 + * - in the middle of layers
8612 + * - to the bottom of layers
8613 + *
8614 + * on the added branch there exists
8615 + * - a whiteout
8616 + * - a diropq
8617 + * - a same named entry
8618 + *   + exist
8619 + *     * negative --> positive
8620 + *     * positive --> positive
8621 + *      - type is unchanged
8622 + *      - type is changed
8623 + *   + doesn't exist
8624 + *     * negative --> negative
8625 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8626 + * - none
8627 + */
8628 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8629 +                              struct au_dinfo *tmp)
8630 +{
8631 +       int err;
8632 +       aufs_bindex_t bindex, bbot;
8633 +       struct {
8634 +               struct dentry *dentry;
8635 +               struct inode *inode;
8636 +               mode_t mode;
8637 +       } orig_h, tmp_h = {
8638 +               .dentry = NULL
8639 +       };
8640 +       struct au_hdentry *hd;
8641 +       struct inode *inode, *h_inode;
8642 +       struct dentry *h_dentry;
8643 +
8644 +       err = 0;
8645 +       AuDebugOn(dinfo->di_btop < 0);
8646 +       orig_h.mode = 0;
8647 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8648 +       orig_h.inode = NULL;
8649 +       if (d_is_positive(orig_h.dentry)) {
8650 +               orig_h.inode = d_inode(orig_h.dentry);
8651 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8652 +       }
8653 +       if (tmp->di_btop >= 0) {
8654 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8655 +               if (d_is_positive(tmp_h.dentry)) {
8656 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8657 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8658 +               }
8659 +       }
8660 +
8661 +       inode = NULL;
8662 +       if (d_really_is_positive(dentry))
8663 +               inode = d_inode(dentry);
8664 +       if (!orig_h.inode) {
8665 +               AuDbg("negative originally\n");
8666 +               if (inode) {
8667 +                       au_hide(dentry);
8668 +                       goto out;
8669 +               }
8670 +               AuDebugOn(inode);
8671 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8672 +               AuDebugOn(dinfo->di_bdiropq != -1);
8673 +
8674 +               if (!tmp_h.inode) {
8675 +                       AuDbg("negative --> negative\n");
8676 +                       /* should have only one negative lower */
8677 +                       if (tmp->di_btop >= 0
8678 +                           && tmp->di_btop < dinfo->di_btop) {
8679 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8680 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8681 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8682 +                               au_di_cp(dinfo, tmp);
8683 +                               hd = au_hdentry(tmp, tmp->di_btop);
8684 +                               au_set_h_dptr(dentry, tmp->di_btop,
8685 +                                             dget(hd->hd_dentry));
8686 +                       }
8687 +                       au_dbg_verify_dinode(dentry);
8688 +               } else {
8689 +                       AuDbg("negative --> positive\n");
8690 +                       /*
8691 +                        * similar to the behaviour of creating with bypassing
8692 +                        * aufs.
8693 +                        * unhash it in order to force an error in the
8694 +                        * succeeding create operation.
8695 +                        * we should not set S_DEAD here.
8696 +                        */
8697 +                       d_drop(dentry);
8698 +                       /* au_di_swap(tmp, dinfo); */
8699 +                       au_dbg_verify_dinode(dentry);
8700 +               }
8701 +       } else {
8702 +               AuDbg("positive originally\n");
8703 +               /* inode may be NULL */
8704 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8705 +               if (!tmp_h.inode) {
8706 +                       AuDbg("positive --> negative\n");
8707 +                       /* or bypassing aufs */
8708 +                       au_hide(dentry);
8709 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8710 +                               dinfo->di_bwh = tmp->di_bwh;
8711 +                       if (inode)
8712 +                               err = au_refresh_hinode_self(inode);
8713 +                       au_dbg_verify_dinode(dentry);
8714 +               } else if (orig_h.mode == tmp_h.mode) {
8715 +                       AuDbg("positive --> positive, same type\n");
8716 +                       if (!S_ISDIR(orig_h.mode)
8717 +                           && dinfo->di_btop > tmp->di_btop) {
8718 +                               /*
8719 +                                * similar to the behaviour of removing and
8720 +                                * creating.
8721 +                                */
8722 +                               au_hide(dentry);
8723 +                               if (inode)
8724 +                                       err = au_refresh_hinode_self(inode);
8725 +                               au_dbg_verify_dinode(dentry);
8726 +                       } else {
8727 +                               /* fill empty slots */
8728 +                               if (dinfo->di_btop > tmp->di_btop)
8729 +                                       dinfo->di_btop = tmp->di_btop;
8730 +                               if (dinfo->di_bbot < tmp->di_bbot)
8731 +                                       dinfo->di_bbot = tmp->di_bbot;
8732 +                               dinfo->di_bwh = tmp->di_bwh;
8733 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8734 +                               bbot = dinfo->di_bbot;
8735 +                               bindex = tmp->di_btop;
8736 +                               hd = au_hdentry(tmp, bindex);
8737 +                               for (; bindex <= bbot; bindex++, hd++) {
8738 +                                       if (au_h_dptr(dentry, bindex))
8739 +                                               continue;
8740 +                                       h_dentry = hd->hd_dentry;
8741 +                                       if (!h_dentry)
8742 +                                               continue;
8743 +                                       AuDebugOn(d_is_negative(h_dentry));
8744 +                                       h_inode = d_inode(h_dentry);
8745 +                                       AuDebugOn(orig_h.mode
8746 +                                                 != (h_inode->i_mode
8747 +                                                     & S_IFMT));
8748 +                                       au_set_h_dptr(dentry, bindex,
8749 +                                                     dget(h_dentry));
8750 +                               }
8751 +                               if (inode)
8752 +                                       err = au_refresh_hinode(inode, dentry);
8753 +                               au_dbg_verify_dinode(dentry);
8754 +                       }
8755 +               } else {
8756 +                       AuDbg("positive --> positive, different type\n");
8757 +                       /* similar to the behaviour of removing and creating */
8758 +                       au_hide(dentry);
8759 +                       if (inode)
8760 +                               err = au_refresh_hinode_self(inode);
8761 +                       au_dbg_verify_dinode(dentry);
8762 +               }
8763 +       }
8764 +
8765 +out:
8766 +       return err;
8767 +}
8768 +
8769 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8770 +{
8771 +       const struct dentry_operations *dop
8772 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8773 +       static const unsigned int mask
8774 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8775 +
8776 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8777 +
8778 +       if (dentry->d_op == dop)
8779 +               return;
8780 +
8781 +       AuDbg("%pd\n", dentry);
8782 +       spin_lock(&dentry->d_lock);
8783 +       if (dop == &aufs_dop)
8784 +               dentry->d_flags |= mask;
8785 +       else
8786 +               dentry->d_flags &= ~mask;
8787 +       dentry->d_op = dop;
8788 +       spin_unlock(&dentry->d_lock);
8789 +}
8790 +
8791 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8792 +{
8793 +       int err, ebrange, nbr;
8794 +       unsigned int sigen;
8795 +       struct au_dinfo *dinfo, *tmp;
8796 +       struct super_block *sb;
8797 +       struct inode *inode;
8798 +
8799 +       DiMustWriteLock(dentry);
8800 +       AuDebugOn(IS_ROOT(dentry));
8801 +       AuDebugOn(d_really_is_negative(parent));
8802 +
8803 +       sb = dentry->d_sb;
8804 +       sigen = au_sigen(sb);
8805 +       err = au_digen_test(parent, sigen);
8806 +       if (unlikely(err))
8807 +               goto out;
8808 +
8809 +       nbr = au_sbbot(sb) + 1;
8810 +       dinfo = au_di(dentry);
8811 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8812 +       if (unlikely(err))
8813 +               goto out;
8814 +       ebrange = au_dbrange_test(dentry);
8815 +       if (!ebrange)
8816 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8817 +
8818 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8819 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8820 +               if (d_really_is_positive(dentry)) {
8821 +                       inode = d_inode(dentry);
8822 +                       err = au_refresh_hinode_self(inode);
8823 +               }
8824 +               au_dbg_verify_dinode(dentry);
8825 +               if (!err)
8826 +                       goto out_dgen; /* success */
8827 +               goto out;
8828 +       }
8829 +
8830 +       /* temporary dinfo */
8831 +       AuDbgDentry(dentry);
8832 +       err = -ENOMEM;
8833 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8834 +       if (unlikely(!tmp))
8835 +               goto out;
8836 +       au_di_swap(tmp, dinfo);
8837 +       /* returns the number of positive dentries */
8838 +       /*
8839 +        * if current working dir is removed, it returns an error.
8840 +        * but the dentry is legal.
8841 +        */
8842 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8843 +       AuDbgDentry(dentry);
8844 +       au_di_swap(tmp, dinfo);
8845 +       if (err == -ENOENT)
8846 +               err = 0;
8847 +       if (err >= 0) {
8848 +               /* compare/refresh by dinfo */
8849 +               AuDbgDentry(dentry);
8850 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8851 +               au_dbg_verify_dinode(dentry);
8852 +               AuTraceErr(err);
8853 +       }
8854 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8855 +       au_rw_write_unlock(&tmp->di_rwsem);
8856 +       au_di_free(tmp);
8857 +       if (unlikely(err))
8858 +               goto out;
8859 +
8860 +out_dgen:
8861 +       au_update_digen(dentry);
8862 +out:
8863 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8864 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8865 +               AuDbgDentry(dentry);
8866 +       }
8867 +       AuTraceErr(err);
8868 +       return err;
8869 +}
8870 +
8871 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8872 +                          struct dentry *dentry, aufs_bindex_t bindex)
8873 +{
8874 +       int err, valid;
8875 +
8876 +       err = 0;
8877 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8878 +               goto out;
8879 +
8880 +       AuDbg("b%d\n", bindex);
8881 +       /*
8882 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8883 +        * due to whiteout and branch permission.
8884 +        */
8885 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8886 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8887 +       /* it may return tri-state */
8888 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8889 +
8890 +       if (unlikely(valid < 0))
8891 +               err = valid;
8892 +       else if (!valid)
8893 +               err = -EINVAL;
8894 +
8895 +out:
8896 +       AuTraceErr(err);
8897 +       return err;
8898 +}
8899 +
8900 +/* todo: remove this */
8901 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8902 +                         unsigned int flags, int do_udba, int dirren)
8903 +{
8904 +       int err;
8905 +       umode_t mode, h_mode;
8906 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8907 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8908 +       struct inode *h_inode, *h_cached_inode;
8909 +       struct dentry *h_dentry;
8910 +       struct qstr *name, *h_name;
8911 +
8912 +       err = 0;
8913 +       plus = 0;
8914 +       mode = 0;
8915 +       ibs = -1;
8916 +       ibe = -1;
8917 +       unhashed = !!d_unhashed(dentry);
8918 +       is_root = !!IS_ROOT(dentry);
8919 +       name = &dentry->d_name;
8920 +       tmpfile = au_di(dentry)->di_tmpfile;
8921 +
8922 +       /*
8923 +        * Theoretically, REVAL test should be unnecessary in case of
8924 +        * {FS,I}NOTIFY.
8925 +        * But {fs,i}notify doesn't fire some necessary events,
8926 +        *      IN_ATTRIB for atime/nlink/pageio
8927 +        * Let's do REVAL test too.
8928 +        */
8929 +       if (do_udba && inode) {
8930 +               mode = (inode->i_mode & S_IFMT);
8931 +               plus = (inode->i_nlink > 0);
8932 +               ibs = au_ibtop(inode);
8933 +               ibe = au_ibbot(inode);
8934 +       }
8935 +
8936 +       btop = au_dbtop(dentry);
8937 +       btail = btop;
8938 +       if (inode && S_ISDIR(inode->i_mode))
8939 +               btail = au_dbtaildir(dentry);
8940 +       for (bindex = btop; bindex <= btail; bindex++) {
8941 +               h_dentry = au_h_dptr(dentry, bindex);
8942 +               if (!h_dentry)
8943 +                       continue;
8944 +
8945 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8946 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8947 +               spin_lock(&h_dentry->d_lock);
8948 +               h_name = &h_dentry->d_name;
8949 +               if (unlikely(do_udba
8950 +                            && !is_root
8951 +                            && ((!h_nfs
8952 +                                 && (unhashed != !!d_unhashed(h_dentry)
8953 +                                     || (!tmpfile && !dirren
8954 +                                         && !au_qstreq(name, h_name))
8955 +                                         ))
8956 +                                || (h_nfs
8957 +                                    && !(flags & LOOKUP_OPEN)
8958 +                                    && (h_dentry->d_flags
8959 +                                        & DCACHE_NFSFS_RENAMED)))
8960 +                           )) {
8961 +                       int h_unhashed;
8962 +
8963 +                       h_unhashed = d_unhashed(h_dentry);
8964 +                       spin_unlock(&h_dentry->d_lock);
8965 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8966 +                             unhashed, h_unhashed, dentry, h_dentry);
8967 +                       goto err;
8968 +               }
8969 +               spin_unlock(&h_dentry->d_lock);
8970 +
8971 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8972 +               if (unlikely(err))
8973 +                       /* do not goto err, to keep the errno */
8974 +                       break;
8975 +
8976 +               /* todo: plink too? */
8977 +               if (!do_udba)
8978 +                       continue;
8979 +
8980 +               /* UDBA tests */
8981 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8982 +                       goto err;
8983 +
8984 +               h_inode = NULL;
8985 +               if (d_is_positive(h_dentry))
8986 +                       h_inode = d_inode(h_dentry);
8987 +               h_plus = plus;
8988 +               h_mode = mode;
8989 +               h_cached_inode = h_inode;
8990 +               if (h_inode) {
8991 +                       h_mode = (h_inode->i_mode & S_IFMT);
8992 +                       h_plus = (h_inode->i_nlink > 0);
8993 +               }
8994 +               if (inode && ibs <= bindex && bindex <= ibe)
8995 +                       h_cached_inode = au_h_iptr(inode, bindex);
8996 +
8997 +               if (!h_nfs) {
8998 +                       if (unlikely(plus != h_plus && !tmpfile))
8999 +                               goto err;
9000 +               } else {
9001 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9002 +                                    && !is_root
9003 +                                    && !IS_ROOT(h_dentry)
9004 +                                    && unhashed != d_unhashed(h_dentry)))
9005 +                               goto err;
9006 +               }
9007 +               if (unlikely(mode != h_mode
9008 +                            || h_cached_inode != h_inode))
9009 +                       goto err;
9010 +               continue;
9011 +
9012 +err:
9013 +               err = -EINVAL;
9014 +               break;
9015 +       }
9016 +
9017 +       AuTraceErr(err);
9018 +       return err;
9019 +}
9020 +
9021 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9022 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9023 +{
9024 +       int err;
9025 +       struct dentry *parent;
9026 +
9027 +       if (!au_digen_test(dentry, sigen))
9028 +               return 0;
9029 +
9030 +       parent = dget_parent(dentry);
9031 +       di_read_lock_parent(parent, AuLock_IR);
9032 +       AuDebugOn(au_digen_test(parent, sigen));
9033 +       au_dbg_verify_gen(parent, sigen);
9034 +       err = au_refresh_dentry(dentry, parent);
9035 +       di_read_unlock(parent, AuLock_IR);
9036 +       dput(parent);
9037 +       AuTraceErr(err);
9038 +       return err;
9039 +}
9040 +
9041 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9042 +{
9043 +       int err;
9044 +       struct dentry *d, *parent;
9045 +
9046 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9047 +               return simple_reval_dpath(dentry, sigen);
9048 +
9049 +       /* slow loop, keep it simple and stupid */
9050 +       /* cf: au_cpup_dirs() */
9051 +       err = 0;
9052 +       parent = NULL;
9053 +       while (au_digen_test(dentry, sigen)) {
9054 +               d = dentry;
9055 +               while (1) {
9056 +                       dput(parent);
9057 +                       parent = dget_parent(d);
9058 +                       if (!au_digen_test(parent, sigen))
9059 +                               break;
9060 +                       d = parent;
9061 +               }
9062 +
9063 +               if (d != dentry)
9064 +                       di_write_lock_child2(d);
9065 +
9066 +               /* someone might update our dentry while we were sleeping */
9067 +               if (au_digen_test(d, sigen)) {
9068 +                       /*
9069 +                        * todo: consolidate with simple_reval_dpath(),
9070 +                        * do_refresh() and au_reval_for_attr().
9071 +                        */
9072 +                       di_read_lock_parent(parent, AuLock_IR);
9073 +                       err = au_refresh_dentry(d, parent);
9074 +                       di_read_unlock(parent, AuLock_IR);
9075 +               }
9076 +
9077 +               if (d != dentry)
9078 +                       di_write_unlock(d);
9079 +               dput(parent);
9080 +               if (unlikely(err))
9081 +                       break;
9082 +       }
9083 +
9084 +       return err;
9085 +}
9086 +
9087 +/*
9088 + * if valid returns 1, otherwise 0.
9089 + */
9090 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9091 +{
9092 +       int valid, err;
9093 +       unsigned int sigen;
9094 +       unsigned char do_udba, dirren;
9095 +       struct super_block *sb;
9096 +       struct inode *inode;
9097 +
9098 +       /* todo: support rcu-walk? */
9099 +       if (flags & LOOKUP_RCU)
9100 +               return -ECHILD;
9101 +
9102 +       valid = 0;
9103 +       if (unlikely(!au_di(dentry)))
9104 +               goto out;
9105 +
9106 +       valid = 1;
9107 +       sb = dentry->d_sb;
9108 +       /*
9109 +        * todo: very ugly
9110 +        * i_mutex of parent dir may be held,
9111 +        * but we should not return 'invalid' due to busy.
9112 +        */
9113 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9114 +       if (unlikely(err)) {
9115 +               valid = err;
9116 +               AuTraceErr(err);
9117 +               goto out;
9118 +       }
9119 +       inode = NULL;
9120 +       if (d_really_is_positive(dentry))
9121 +               inode = d_inode(dentry);
9122 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9123 +               err = -EINVAL;
9124 +               AuTraceErr(err);
9125 +               goto out_dgrade;
9126 +       }
9127 +       if (unlikely(au_dbrange_test(dentry))) {
9128 +               err = -EINVAL;
9129 +               AuTraceErr(err);
9130 +               goto out_dgrade;
9131 +       }
9132 +
9133 +       sigen = au_sigen(sb);
9134 +       if (au_digen_test(dentry, sigen)) {
9135 +               AuDebugOn(IS_ROOT(dentry));
9136 +               err = au_reval_dpath(dentry, sigen);
9137 +               if (unlikely(err)) {
9138 +                       AuTraceErr(err);
9139 +                       goto out_dgrade;
9140 +               }
9141 +       }
9142 +       di_downgrade_lock(dentry, AuLock_IR);
9143 +
9144 +       err = -EINVAL;
9145 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9146 +           && inode
9147 +           && !(inode->i_state && I_LINKABLE)
9148 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9149 +               AuTraceErr(err);
9150 +               goto out_inval;
9151 +       }
9152 +
9153 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9154 +       if (do_udba && inode) {
9155 +               aufs_bindex_t btop = au_ibtop(inode);
9156 +               struct inode *h_inode;
9157 +
9158 +               if (btop >= 0) {
9159 +                       h_inode = au_h_iptr(inode, btop);
9160 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9161 +                               AuTraceErr(err);
9162 +                               goto out_inval;
9163 +                       }
9164 +               }
9165 +       }
9166 +
9167 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9168 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9169 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9170 +               err = -EIO;
9171 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9172 +                     dentry, err);
9173 +       }
9174 +       goto out_inval;
9175 +
9176 +out_dgrade:
9177 +       di_downgrade_lock(dentry, AuLock_IR);
9178 +out_inval:
9179 +       aufs_read_unlock(dentry, AuLock_IR);
9180 +       AuTraceErr(err);
9181 +       valid = !err;
9182 +out:
9183 +       if (!valid) {
9184 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9185 +               d_drop(dentry);
9186 +       }
9187 +       return valid;
9188 +}
9189 +
9190 +static void aufs_d_release(struct dentry *dentry)
9191 +{
9192 +       if (au_di(dentry)) {
9193 +               au_di_fin(dentry);
9194 +               au_hn_di_reinit(dentry);
9195 +       }
9196 +}
9197 +
9198 +const struct dentry_operations aufs_dop = {
9199 +       .d_revalidate           = aufs_d_revalidate,
9200 +       .d_weak_revalidate      = aufs_d_revalidate,
9201 +       .d_release              = aufs_d_release
9202 +};
9203 +
9204 +/* aufs_dop without d_revalidate */
9205 +const struct dentry_operations aufs_dop_noreval = {
9206 +       .d_release              = aufs_d_release
9207 +};
9208 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9209 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9210 +++ linux/fs/aufs/dentry.h      2020-01-27 10:57:18.168871450 +0100
9211 @@ -0,0 +1,268 @@
9212 +/* SPDX-License-Identifier: GPL-2.0 */
9213 +/*
9214 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9215 + *
9216 + * This program, aufs is free software; you can redistribute it and/or modify
9217 + * it under the terms of the GNU General Public License as published by
9218 + * the Free Software Foundation; either version 2 of the License, or
9219 + * (at your option) any later version.
9220 + *
9221 + * This program is distributed in the hope that it will be useful,
9222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9224 + * GNU General Public License for more details.
9225 + *
9226 + * You should have received a copy of the GNU General Public License
9227 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9228 + */
9229 +
9230 +/*
9231 + * lookup and dentry operations
9232 + */
9233 +
9234 +#ifndef __AUFS_DENTRY_H__
9235 +#define __AUFS_DENTRY_H__
9236 +
9237 +#ifdef __KERNEL__
9238 +
9239 +#include <linux/dcache.h>
9240 +#include "dirren.h"
9241 +#include "rwsem.h"
9242 +
9243 +struct au_hdentry {
9244 +       struct dentry           *hd_dentry;
9245 +       aufs_bindex_t           hd_id;
9246 +};
9247 +
9248 +struct au_dinfo {
9249 +       atomic_t                di_generation;
9250 +
9251 +       struct au_rwsem         di_rwsem;
9252 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9253 +       unsigned char           di_tmpfile; /* to allow the different name */
9254 +       struct au_hdentry       *di_hdentry;
9255 +       struct rcu_head         rcu;
9256 +} ____cacheline_aligned_in_smp;
9257 +
9258 +/* ---------------------------------------------------------------------- */
9259 +
9260 +/* flags for au_lkup_dentry() */
9261 +#define AuLkup_ALLOW_NEG       1
9262 +#define AuLkup_IGNORE_PERM     (1 << 1)
9263 +#define AuLkup_DIRREN          (1 << 2)
9264 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9265 +#define au_fset_lkup(flags, name) \
9266 +       do { (flags) |= AuLkup_##name; } while (0)
9267 +#define au_fclr_lkup(flags, name) \
9268 +       do { (flags) &= ~AuLkup_##name; } while (0)
9269 +
9270 +#ifndef CONFIG_AUFS_DIRREN
9271 +#undef AuLkup_DIRREN
9272 +#define AuLkup_DIRREN 0
9273 +#endif
9274 +
9275 +struct au_do_lookup_args {
9276 +       unsigned int            flags;
9277 +       mode_t                  type;
9278 +       struct qstr             whname, *name;
9279 +       struct au_dr_lookup     dirren;
9280 +};
9281 +
9282 +/* ---------------------------------------------------------------------- */
9283 +
9284 +/* dentry.c */
9285 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9286 +struct au_branch;
9287 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9288 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9289 +               struct dentry *h_parent, struct au_branch *br);
9290 +
9291 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9292 +                  unsigned int flags);
9293 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9294 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9295 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9296 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9297 +
9298 +/* dinfo.c */
9299 +void au_di_init_once(void *_di);
9300 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9301 +void au_di_free(struct au_dinfo *dinfo);
9302 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9303 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9304 +int au_di_init(struct dentry *dentry);
9305 +void au_di_fin(struct dentry *dentry);
9306 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9307 +
9308 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9309 +void di_read_unlock(struct dentry *d, int flags);
9310 +void di_downgrade_lock(struct dentry *d, int flags);
9311 +void di_write_lock(struct dentry *d, unsigned int lsc);
9312 +void di_write_unlock(struct dentry *d);
9313 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9314 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9315 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9316 +
9317 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9318 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9319 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9320 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9321 +
9322 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9323 +                  struct dentry *h_dentry);
9324 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9325 +int au_dbrange_test(struct dentry *dentry);
9326 +void au_update_digen(struct dentry *dentry);
9327 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9328 +void au_update_dbtop(struct dentry *dentry);
9329 +void au_update_dbbot(struct dentry *dentry);
9330 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9331 +
9332 +/* ---------------------------------------------------------------------- */
9333 +
9334 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9335 +{
9336 +       return dentry->d_fsdata;
9337 +}
9338 +
9339 +/* ---------------------------------------------------------------------- */
9340 +
9341 +/* lock subclass for dinfo */
9342 +enum {
9343 +       AuLsc_DI_CHILD,         /* child first */
9344 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9345 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9346 +       AuLsc_DI_PARENT,
9347 +       AuLsc_DI_PARENT2,
9348 +       AuLsc_DI_PARENT3,
9349 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9350 +};
9351 +
9352 +/*
9353 + * di_read_lock_child, di_write_lock_child,
9354 + * di_read_lock_child2, di_write_lock_child2,
9355 + * di_read_lock_child3, di_write_lock_child3,
9356 + * di_read_lock_parent, di_write_lock_parent,
9357 + * di_read_lock_parent2, di_write_lock_parent2,
9358 + * di_read_lock_parent3, di_write_lock_parent3,
9359 + */
9360 +#define AuReadLockFunc(name, lsc) \
9361 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9362 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9363 +
9364 +#define AuWriteLockFunc(name, lsc) \
9365 +static inline void di_write_lock_##name(struct dentry *d) \
9366 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9367 +
9368 +#define AuRWLockFuncs(name, lsc) \
9369 +       AuReadLockFunc(name, lsc) \
9370 +       AuWriteLockFunc(name, lsc)
9371 +
9372 +AuRWLockFuncs(child, CHILD);
9373 +AuRWLockFuncs(child2, CHILD2);
9374 +AuRWLockFuncs(child3, CHILD3);
9375 +AuRWLockFuncs(parent, PARENT);
9376 +AuRWLockFuncs(parent2, PARENT2);
9377 +AuRWLockFuncs(parent3, PARENT3);
9378 +
9379 +#undef AuReadLockFunc
9380 +#undef AuWriteLockFunc
9381 +#undef AuRWLockFuncs
9382 +
9383 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9384 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9385 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9386 +
9387 +/* ---------------------------------------------------------------------- */
9388 +
9389 +/* todo: memory barrier? */
9390 +static inline unsigned int au_digen(struct dentry *d)
9391 +{
9392 +       return atomic_read(&au_di(d)->di_generation);
9393 +}
9394 +
9395 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9396 +{
9397 +       hdentry->hd_dentry = NULL;
9398 +}
9399 +
9400 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9401 +                                           aufs_bindex_t bindex)
9402 +{
9403 +       return di->di_hdentry + bindex;
9404 +}
9405 +
9406 +static inline void au_hdput(struct au_hdentry *hd)
9407 +{
9408 +       if (hd)
9409 +               dput(hd->hd_dentry);
9410 +}
9411 +
9412 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9413 +{
9414 +       DiMustAnyLock(dentry);
9415 +       return au_di(dentry)->di_btop;
9416 +}
9417 +
9418 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9419 +{
9420 +       DiMustAnyLock(dentry);
9421 +       return au_di(dentry)->di_bbot;
9422 +}
9423 +
9424 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9425 +{
9426 +       DiMustAnyLock(dentry);
9427 +       return au_di(dentry)->di_bwh;
9428 +}
9429 +
9430 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9431 +{
9432 +       DiMustAnyLock(dentry);
9433 +       return au_di(dentry)->di_bdiropq;
9434 +}
9435 +
9436 +/* todo: hard/soft set? */
9437 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9438 +{
9439 +       DiMustWriteLock(dentry);
9440 +       au_di(dentry)->di_btop = bindex;
9441 +}
9442 +
9443 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9444 +{
9445 +       DiMustWriteLock(dentry);
9446 +       au_di(dentry)->di_bbot = bindex;
9447 +}
9448 +
9449 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9450 +{
9451 +       DiMustWriteLock(dentry);
9452 +       /* dbwh can be outside of btop - bbot range */
9453 +       au_di(dentry)->di_bwh = bindex;
9454 +}
9455 +
9456 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9457 +{
9458 +       DiMustWriteLock(dentry);
9459 +       au_di(dentry)->di_bdiropq = bindex;
9460 +}
9461 +
9462 +/* ---------------------------------------------------------------------- */
9463 +
9464 +#ifdef CONFIG_AUFS_HNOTIFY
9465 +static inline void au_digen_dec(struct dentry *d)
9466 +{
9467 +       atomic_dec(&au_di(d)->di_generation);
9468 +}
9469 +
9470 +static inline void au_hn_di_reinit(struct dentry *dentry)
9471 +{
9472 +       dentry->d_fsdata = NULL;
9473 +}
9474 +#else
9475 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9476 +#endif /* CONFIG_AUFS_HNOTIFY */
9477 +
9478 +#endif /* __KERNEL__ */
9479 +#endif /* __AUFS_DENTRY_H__ */
9480 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9481 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9482 +++ linux/fs/aufs/dinfo.c       2020-01-27 10:57:18.168871450 +0100
9483 @@ -0,0 +1,554 @@
9484 +// SPDX-License-Identifier: GPL-2.0
9485 +/*
9486 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9487 + *
9488 + * This program, aufs is free software; you can redistribute it and/or modify
9489 + * it under the terms of the GNU General Public License as published by
9490 + * the Free Software Foundation; either version 2 of the License, or
9491 + * (at your option) any later version.
9492 + *
9493 + * This program is distributed in the hope that it will be useful,
9494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9496 + * GNU General Public License for more details.
9497 + *
9498 + * You should have received a copy of the GNU General Public License
9499 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9500 + */
9501 +
9502 +/*
9503 + * dentry private data
9504 + */
9505 +
9506 +#include "aufs.h"
9507 +
9508 +void au_di_init_once(void *_dinfo)
9509 +{
9510 +       struct au_dinfo *dinfo = _dinfo;
9511 +
9512 +       au_rw_init(&dinfo->di_rwsem);
9513 +}
9514 +
9515 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9516 +{
9517 +       struct au_dinfo *dinfo;
9518 +       int nbr, i;
9519 +
9520 +       dinfo = au_cache_alloc_dinfo();
9521 +       if (unlikely(!dinfo))
9522 +               goto out;
9523 +
9524 +       nbr = au_sbbot(sb) + 1;
9525 +       if (nbr <= 0)
9526 +               nbr = 1;
9527 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9528 +       if (dinfo->di_hdentry) {
9529 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9530 +               dinfo->di_btop = -1;
9531 +               dinfo->di_bbot = -1;
9532 +               dinfo->di_bwh = -1;
9533 +               dinfo->di_bdiropq = -1;
9534 +               dinfo->di_tmpfile = 0;
9535 +               for (i = 0; i < nbr; i++)
9536 +                       dinfo->di_hdentry[i].hd_id = -1;
9537 +               goto out;
9538 +       }
9539 +
9540 +       au_cache_free_dinfo(dinfo);
9541 +       dinfo = NULL;
9542 +
9543 +out:
9544 +       return dinfo;
9545 +}
9546 +
9547 +void au_di_free(struct au_dinfo *dinfo)
9548 +{
9549 +       struct au_hdentry *p;
9550 +       aufs_bindex_t bbot, bindex;
9551 +
9552 +       /* dentry may not be revalidated */
9553 +       bindex = dinfo->di_btop;
9554 +       if (bindex >= 0) {
9555 +               bbot = dinfo->di_bbot;
9556 +               p = au_hdentry(dinfo, bindex);
9557 +               while (bindex++ <= bbot)
9558 +                       au_hdput(p++);
9559 +       }
9560 +       au_kfree_try_rcu(dinfo->di_hdentry);
9561 +       au_cache_free_dinfo(dinfo);
9562 +}
9563 +
9564 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9565 +{
9566 +       struct au_hdentry *p;
9567 +       aufs_bindex_t bi;
9568 +
9569 +       AuRwMustWriteLock(&a->di_rwsem);
9570 +       AuRwMustWriteLock(&b->di_rwsem);
9571 +
9572 +#define DiSwap(v, name)                                \
9573 +       do {                                    \
9574 +               v = a->di_##name;               \
9575 +               a->di_##name = b->di_##name;    \
9576 +               b->di_##name = v;               \
9577 +       } while (0)
9578 +
9579 +       DiSwap(p, hdentry);
9580 +       DiSwap(bi, btop);
9581 +       DiSwap(bi, bbot);
9582 +       DiSwap(bi, bwh);
9583 +       DiSwap(bi, bdiropq);
9584 +       /* smp_mb(); */
9585 +
9586 +#undef DiSwap
9587 +}
9588 +
9589 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9590 +{
9591 +       AuRwMustWriteLock(&dst->di_rwsem);
9592 +       AuRwMustWriteLock(&src->di_rwsem);
9593 +
9594 +       dst->di_btop = src->di_btop;
9595 +       dst->di_bbot = src->di_bbot;
9596 +       dst->di_bwh = src->di_bwh;
9597 +       dst->di_bdiropq = src->di_bdiropq;
9598 +       /* smp_mb(); */
9599 +}
9600 +
9601 +int au_di_init(struct dentry *dentry)
9602 +{
9603 +       int err;
9604 +       struct super_block *sb;
9605 +       struct au_dinfo *dinfo;
9606 +
9607 +       err = 0;
9608 +       sb = dentry->d_sb;
9609 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9610 +       if (dinfo) {
9611 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9612 +               /* smp_mb(); */ /* atomic_set */
9613 +               dentry->d_fsdata = dinfo;
9614 +       } else
9615 +               err = -ENOMEM;
9616 +
9617 +       return err;
9618 +}
9619 +
9620 +void au_di_fin(struct dentry *dentry)
9621 +{
9622 +       struct au_dinfo *dinfo;
9623 +
9624 +       dinfo = au_di(dentry);
9625 +       AuRwDestroy(&dinfo->di_rwsem);
9626 +       au_di_free(dinfo);
9627 +}
9628 +
9629 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9630 +{
9631 +       int err, sz;
9632 +       struct au_hdentry *hdp;
9633 +
9634 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9635 +
9636 +       err = -ENOMEM;
9637 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9638 +       if (!sz)
9639 +               sz = sizeof(*hdp);
9640 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9641 +                          may_shrink);
9642 +       if (hdp) {
9643 +               dinfo->di_hdentry = hdp;
9644 +               err = 0;
9645 +       }
9646 +
9647 +       return err;
9648 +}
9649 +
9650 +/* ---------------------------------------------------------------------- */
9651 +
9652 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9653 +{
9654 +       switch (lsc) {
9655 +       case AuLsc_DI_CHILD:
9656 +               ii_write_lock_child(inode);
9657 +               break;
9658 +       case AuLsc_DI_CHILD2:
9659 +               ii_write_lock_child2(inode);
9660 +               break;
9661 +       case AuLsc_DI_CHILD3:
9662 +               ii_write_lock_child3(inode);
9663 +               break;
9664 +       case AuLsc_DI_PARENT:
9665 +               ii_write_lock_parent(inode);
9666 +               break;
9667 +       case AuLsc_DI_PARENT2:
9668 +               ii_write_lock_parent2(inode);
9669 +               break;
9670 +       case AuLsc_DI_PARENT3:
9671 +               ii_write_lock_parent3(inode);
9672 +               break;
9673 +       default:
9674 +               BUG();
9675 +       }
9676 +}
9677 +
9678 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9679 +{
9680 +       switch (lsc) {
9681 +       case AuLsc_DI_CHILD:
9682 +               ii_read_lock_child(inode);
9683 +               break;
9684 +       case AuLsc_DI_CHILD2:
9685 +               ii_read_lock_child2(inode);
9686 +               break;
9687 +       case AuLsc_DI_CHILD3:
9688 +               ii_read_lock_child3(inode);
9689 +               break;
9690 +       case AuLsc_DI_PARENT:
9691 +               ii_read_lock_parent(inode);
9692 +               break;
9693 +       case AuLsc_DI_PARENT2:
9694 +               ii_read_lock_parent2(inode);
9695 +               break;
9696 +       case AuLsc_DI_PARENT3:
9697 +               ii_read_lock_parent3(inode);
9698 +               break;
9699 +       default:
9700 +               BUG();
9701 +       }
9702 +}
9703 +
9704 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9705 +{
9706 +       struct inode *inode;
9707 +
9708 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9709 +       if (d_really_is_positive(d)) {
9710 +               inode = d_inode(d);
9711 +               if (au_ftest_lock(flags, IW))
9712 +                       do_ii_write_lock(inode, lsc);
9713 +               else if (au_ftest_lock(flags, IR))
9714 +                       do_ii_read_lock(inode, lsc);
9715 +       }
9716 +}
9717 +
9718 +void di_read_unlock(struct dentry *d, int flags)
9719 +{
9720 +       struct inode *inode;
9721 +
9722 +       if (d_really_is_positive(d)) {
9723 +               inode = d_inode(d);
9724 +               if (au_ftest_lock(flags, IW)) {
9725 +                       au_dbg_verify_dinode(d);
9726 +                       ii_write_unlock(inode);
9727 +               } else if (au_ftest_lock(flags, IR)) {
9728 +                       au_dbg_verify_dinode(d);
9729 +                       ii_read_unlock(inode);
9730 +               }
9731 +       }
9732 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9733 +}
9734 +
9735 +void di_downgrade_lock(struct dentry *d, int flags)
9736 +{
9737 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9738 +               ii_downgrade_lock(d_inode(d));
9739 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9740 +}
9741 +
9742 +void di_write_lock(struct dentry *d, unsigned int lsc)
9743 +{
9744 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9745 +       if (d_really_is_positive(d))
9746 +               do_ii_write_lock(d_inode(d), lsc);
9747 +}
9748 +
9749 +void di_write_unlock(struct dentry *d)
9750 +{
9751 +       au_dbg_verify_dinode(d);
9752 +       if (d_really_is_positive(d))
9753 +               ii_write_unlock(d_inode(d));
9754 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9755 +}
9756 +
9757 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9758 +{
9759 +       AuDebugOn(d1 == d2
9760 +                 || d_inode(d1) == d_inode(d2)
9761 +                 || d1->d_sb != d2->d_sb);
9762 +
9763 +       if ((isdir && au_test_subdir(d1, d2))
9764 +           || d1 < d2) {
9765 +               di_write_lock_child(d1);
9766 +               di_write_lock_child2(d2);
9767 +       } else {
9768 +               di_write_lock_child(d2);
9769 +               di_write_lock_child2(d1);
9770 +       }
9771 +}
9772 +
9773 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9774 +{
9775 +       AuDebugOn(d1 == d2
9776 +                 || d_inode(d1) == d_inode(d2)
9777 +                 || d1->d_sb != d2->d_sb);
9778 +
9779 +       if ((isdir && au_test_subdir(d1, d2))
9780 +           || d1 < d2) {
9781 +               di_write_lock_parent(d1);
9782 +               di_write_lock_parent2(d2);
9783 +       } else {
9784 +               di_write_lock_parent(d2);
9785 +               di_write_lock_parent2(d1);
9786 +       }
9787 +}
9788 +
9789 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9790 +{
9791 +       di_write_unlock(d1);
9792 +       if (d_inode(d1) == d_inode(d2))
9793 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9794 +       else
9795 +               di_write_unlock(d2);
9796 +}
9797 +
9798 +/* ---------------------------------------------------------------------- */
9799 +
9800 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9801 +{
9802 +       struct dentry *d;
9803 +
9804 +       DiMustAnyLock(dentry);
9805 +
9806 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9807 +               return NULL;
9808 +       AuDebugOn(bindex < 0);
9809 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9810 +       AuDebugOn(d && au_dcount(d) <= 0);
9811 +       return d;
9812 +}
9813 +
9814 +/*
9815 + * extended version of au_h_dptr().
9816 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9817 + * error.
9818 + */
9819 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9820 +{
9821 +       struct dentry *h_dentry;
9822 +       struct inode *inode, *h_inode;
9823 +
9824 +       AuDebugOn(d_really_is_negative(dentry));
9825 +
9826 +       h_dentry = NULL;
9827 +       if (au_dbtop(dentry) <= bindex
9828 +           && bindex <= au_dbbot(dentry))
9829 +               h_dentry = au_h_dptr(dentry, bindex);
9830 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9831 +               dget(h_dentry);
9832 +               goto out; /* success */
9833 +       }
9834 +
9835 +       inode = d_inode(dentry);
9836 +       AuDebugOn(bindex < au_ibtop(inode));
9837 +       AuDebugOn(au_ibbot(inode) < bindex);
9838 +       h_inode = au_h_iptr(inode, bindex);
9839 +       h_dentry = d_find_alias(h_inode);
9840 +       if (h_dentry) {
9841 +               if (!IS_ERR(h_dentry)) {
9842 +                       if (!au_d_linkable(h_dentry))
9843 +                               goto out; /* success */
9844 +                       dput(h_dentry);
9845 +               } else
9846 +                       goto out;
9847 +       }
9848 +
9849 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9850 +               h_dentry = au_plink_lkup(inode, bindex);
9851 +               AuDebugOn(!h_dentry);
9852 +               if (!IS_ERR(h_dentry)) {
9853 +                       if (!au_d_hashed_positive(h_dentry))
9854 +                               goto out; /* success */
9855 +                       dput(h_dentry);
9856 +                       h_dentry = NULL;
9857 +               }
9858 +       }
9859 +
9860 +out:
9861 +       AuDbgDentry(h_dentry);
9862 +       return h_dentry;
9863 +}
9864 +
9865 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9866 +{
9867 +       aufs_bindex_t bbot, bwh;
9868 +
9869 +       bbot = au_dbbot(dentry);
9870 +       if (0 <= bbot) {
9871 +               bwh = au_dbwh(dentry);
9872 +               if (!bwh)
9873 +                       return bwh;
9874 +               if (0 < bwh && bwh < bbot)
9875 +                       return bwh - 1;
9876 +       }
9877 +       return bbot;
9878 +}
9879 +
9880 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9881 +{
9882 +       aufs_bindex_t bbot, bopq;
9883 +
9884 +       bbot = au_dbtail(dentry);
9885 +       if (0 <= bbot) {
9886 +               bopq = au_dbdiropq(dentry);
9887 +               if (0 <= bopq && bopq < bbot)
9888 +                       bbot = bopq;
9889 +       }
9890 +       return bbot;
9891 +}
9892 +
9893 +/* ---------------------------------------------------------------------- */
9894 +
9895 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9896 +                  struct dentry *h_dentry)
9897 +{
9898 +       struct au_dinfo *dinfo;
9899 +       struct au_hdentry *hd;
9900 +       struct au_branch *br;
9901 +
9902 +       DiMustWriteLock(dentry);
9903 +
9904 +       dinfo = au_di(dentry);
9905 +       hd = au_hdentry(dinfo, bindex);
9906 +       au_hdput(hd);
9907 +       hd->hd_dentry = h_dentry;
9908 +       if (h_dentry) {
9909 +               br = au_sbr(dentry->d_sb, bindex);
9910 +               hd->hd_id = br->br_id;
9911 +       }
9912 +}
9913 +
9914 +int au_dbrange_test(struct dentry *dentry)
9915 +{
9916 +       int err;
9917 +       aufs_bindex_t btop, bbot;
9918 +
9919 +       err = 0;
9920 +       btop = au_dbtop(dentry);
9921 +       bbot = au_dbbot(dentry);
9922 +       if (btop >= 0)
9923 +               AuDebugOn(bbot < 0 && btop > bbot);
9924 +       else {
9925 +               err = -EIO;
9926 +               AuDebugOn(bbot >= 0);
9927 +       }
9928 +
9929 +       return err;
9930 +}
9931 +
9932 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9933 +{
9934 +       int err;
9935 +
9936 +       err = 0;
9937 +       if (unlikely(au_digen(dentry) != sigen
9938 +                    || au_iigen_test(d_inode(dentry), sigen)))
9939 +               err = -EIO;
9940 +
9941 +       return err;
9942 +}
9943 +
9944 +void au_update_digen(struct dentry *dentry)
9945 +{
9946 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9947 +       /* smp_mb(); */ /* atomic_set */
9948 +}
9949 +
9950 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9951 +{
9952 +       struct au_dinfo *dinfo;
9953 +       struct dentry *h_d;
9954 +       struct au_hdentry *hdp;
9955 +       aufs_bindex_t bindex, bbot;
9956 +
9957 +       DiMustWriteLock(dentry);
9958 +
9959 +       dinfo = au_di(dentry);
9960 +       if (!dinfo || dinfo->di_btop < 0)
9961 +               return;
9962 +
9963 +       if (do_put_zero) {
9964 +               bbot = dinfo->di_bbot;
9965 +               bindex = dinfo->di_btop;
9966 +               hdp = au_hdentry(dinfo, bindex);
9967 +               for (; bindex <= bbot; bindex++, hdp++) {
9968 +                       h_d = hdp->hd_dentry;
9969 +                       if (h_d && d_is_negative(h_d))
9970 +                               au_set_h_dptr(dentry, bindex, NULL);
9971 +               }
9972 +       }
9973 +
9974 +       dinfo->di_btop = 0;
9975 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9976 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9977 +               if (hdp->hd_dentry)
9978 +                       break;
9979 +       if (dinfo->di_btop > dinfo->di_bbot) {
9980 +               dinfo->di_btop = -1;
9981 +               dinfo->di_bbot = -1;
9982 +               return;
9983 +       }
9984 +
9985 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9986 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9987 +               if (hdp->hd_dentry)
9988 +                       break;
9989 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9990 +}
9991 +
9992 +void au_update_dbtop(struct dentry *dentry)
9993 +{
9994 +       aufs_bindex_t bindex, bbot;
9995 +       struct dentry *h_dentry;
9996 +
9997 +       bbot = au_dbbot(dentry);
9998 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9999 +               h_dentry = au_h_dptr(dentry, bindex);
10000 +               if (!h_dentry)
10001 +                       continue;
10002 +               if (d_is_positive(h_dentry)) {
10003 +                       au_set_dbtop(dentry, bindex);
10004 +                       return;
10005 +               }
10006 +               au_set_h_dptr(dentry, bindex, NULL);
10007 +       }
10008 +}
10009 +
10010 +void au_update_dbbot(struct dentry *dentry)
10011 +{
10012 +       aufs_bindex_t bindex, btop;
10013 +       struct dentry *h_dentry;
10014 +
10015 +       btop = au_dbtop(dentry);
10016 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10017 +               h_dentry = au_h_dptr(dentry, bindex);
10018 +               if (!h_dentry)
10019 +                       continue;
10020 +               if (d_is_positive(h_dentry)) {
10021 +                       au_set_dbbot(dentry, bindex);
10022 +                       return;
10023 +               }
10024 +               au_set_h_dptr(dentry, bindex, NULL);
10025 +       }
10026 +}
10027 +
10028 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10029 +{
10030 +       aufs_bindex_t bindex, bbot;
10031 +
10032 +       bbot = au_dbbot(dentry);
10033 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10034 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10035 +                       return bindex;
10036 +       return -1;
10037 +}
10038 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10039 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10040 +++ linux/fs/aufs/dir.c 2020-01-27 10:57:18.168871450 +0100
10041 @@ -0,0 +1,763 @@
10042 +// SPDX-License-Identifier: GPL-2.0
10043 +/*
10044 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10045 + *
10046 + * This program, aufs is free software; you can redistribute it and/or modify
10047 + * it under the terms of the GNU General Public License as published by
10048 + * the Free Software Foundation; either version 2 of the License, or
10049 + * (at your option) any later version.
10050 + *
10051 + * This program is distributed in the hope that it will be useful,
10052 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10053 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10054 + * GNU General Public License for more details.
10055 + *
10056 + * You should have received a copy of the GNU General Public License
10057 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10058 + */
10059 +
10060 +/*
10061 + * directory operations
10062 + */
10063 +
10064 +#include <linux/fs_stack.h>
10065 +#include <linux/iversion.h>
10066 +#include "aufs.h"
10067 +
10068 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10069 +{
10070 +       unsigned int nlink;
10071 +
10072 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10073 +
10074 +       nlink = dir->i_nlink;
10075 +       nlink += h_dir->i_nlink - 2;
10076 +       if (h_dir->i_nlink < 2)
10077 +               nlink += 2;
10078 +       smp_mb(); /* for i_nlink */
10079 +       /* 0 can happen in revaliding */
10080 +       set_nlink(dir, nlink);
10081 +}
10082 +
10083 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10084 +{
10085 +       unsigned int nlink;
10086 +
10087 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10088 +
10089 +       nlink = dir->i_nlink;
10090 +       nlink -= h_dir->i_nlink - 2;
10091 +       if (h_dir->i_nlink < 2)
10092 +               nlink -= 2;
10093 +       smp_mb(); /* for i_nlink */
10094 +       /* nlink == 0 means the branch-fs is broken */
10095 +       set_nlink(dir, nlink);
10096 +}
10097 +
10098 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10099 +{
10100 +       loff_t sz;
10101 +       aufs_bindex_t bindex, bbot;
10102 +       struct file *h_file;
10103 +       struct dentry *h_dentry;
10104 +
10105 +       sz = 0;
10106 +       if (file) {
10107 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10108 +
10109 +               bbot = au_fbbot_dir(file);
10110 +               for (bindex = au_fbtop(file);
10111 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10112 +                    bindex++) {
10113 +                       h_file = au_hf_dir(file, bindex);
10114 +                       if (h_file && file_inode(h_file))
10115 +                               sz += vfsub_f_size_read(h_file);
10116 +               }
10117 +       } else {
10118 +               AuDebugOn(!dentry);
10119 +               AuDebugOn(!d_is_dir(dentry));
10120 +
10121 +               bbot = au_dbtaildir(dentry);
10122 +               for (bindex = au_dbtop(dentry);
10123 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10124 +                    bindex++) {
10125 +                       h_dentry = au_h_dptr(dentry, bindex);
10126 +                       if (h_dentry && d_is_positive(h_dentry))
10127 +                               sz += i_size_read(d_inode(h_dentry));
10128 +               }
10129 +       }
10130 +       if (sz < KMALLOC_MAX_SIZE)
10131 +               sz = roundup_pow_of_two(sz);
10132 +       if (sz > KMALLOC_MAX_SIZE)
10133 +               sz = KMALLOC_MAX_SIZE;
10134 +       else if (sz < NAME_MAX) {
10135 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10136 +               sz = AUFS_RDBLK_DEF;
10137 +       }
10138 +       return sz;
10139 +}
10140 +
10141 +struct au_dir_ts_arg {
10142 +       struct dentry *dentry;
10143 +       aufs_bindex_t brid;
10144 +};
10145 +
10146 +static void au_do_dir_ts(void *arg)
10147 +{
10148 +       struct au_dir_ts_arg *a = arg;
10149 +       struct au_dtime dt;
10150 +       struct path h_path;
10151 +       struct inode *dir, *h_dir;
10152 +       struct super_block *sb;
10153 +       struct au_branch *br;
10154 +       struct au_hinode *hdir;
10155 +       int err;
10156 +       aufs_bindex_t btop, bindex;
10157 +
10158 +       sb = a->dentry->d_sb;
10159 +       if (d_really_is_negative(a->dentry))
10160 +               goto out;
10161 +       /* no dir->i_mutex lock */
10162 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10163 +
10164 +       dir = d_inode(a->dentry);
10165 +       btop = au_ibtop(dir);
10166 +       bindex = au_br_index(sb, a->brid);
10167 +       if (bindex < btop)
10168 +               goto out_unlock;
10169 +
10170 +       br = au_sbr(sb, bindex);
10171 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10172 +       if (!h_path.dentry)
10173 +               goto out_unlock;
10174 +       h_path.mnt = au_br_mnt(br);
10175 +       au_dtime_store(&dt, a->dentry, &h_path);
10176 +
10177 +       br = au_sbr(sb, btop);
10178 +       if (!au_br_writable(br->br_perm))
10179 +               goto out_unlock;
10180 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10181 +       h_path.mnt = au_br_mnt(br);
10182 +       err = vfsub_mnt_want_write(h_path.mnt);
10183 +       if (err)
10184 +               goto out_unlock;
10185 +       hdir = au_hi(dir, btop);
10186 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10187 +       h_dir = au_h_iptr(dir, btop);
10188 +       if (h_dir->i_nlink
10189 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10190 +               dt.dt_h_path = h_path;
10191 +               au_dtime_revert(&dt);
10192 +       }
10193 +       au_hn_inode_unlock(hdir);
10194 +       vfsub_mnt_drop_write(h_path.mnt);
10195 +       au_cpup_attr_timesizes(dir);
10196 +
10197 +out_unlock:
10198 +       aufs_read_unlock(a->dentry, AuLock_DW);
10199 +out:
10200 +       dput(a->dentry);
10201 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10202 +       au_kfree_try_rcu(arg);
10203 +}
10204 +
10205 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10206 +{
10207 +       int perm, wkq_err;
10208 +       aufs_bindex_t btop;
10209 +       struct au_dir_ts_arg *arg;
10210 +       struct dentry *dentry;
10211 +       struct super_block *sb;
10212 +
10213 +       IMustLock(dir);
10214 +
10215 +       dentry = d_find_any_alias(dir);
10216 +       AuDebugOn(!dentry);
10217 +       sb = dentry->d_sb;
10218 +       btop = au_ibtop(dir);
10219 +       if (btop == bindex) {
10220 +               au_cpup_attr_timesizes(dir);
10221 +               goto out;
10222 +       }
10223 +
10224 +       perm = au_sbr_perm(sb, btop);
10225 +       if (!au_br_writable(perm))
10226 +               goto out;
10227 +
10228 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10229 +       if (!arg)
10230 +               goto out;
10231 +
10232 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10233 +       arg->brid = au_sbr_id(sb, bindex);
10234 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10235 +       if (unlikely(wkq_err)) {
10236 +               pr_err("wkq %d\n", wkq_err);
10237 +               dput(dentry);
10238 +               au_kfree_try_rcu(arg);
10239 +       }
10240 +
10241 +out:
10242 +       dput(dentry);
10243 +}
10244 +
10245 +/* ---------------------------------------------------------------------- */
10246 +
10247 +static int reopen_dir(struct file *file)
10248 +{
10249 +       int err;
10250 +       unsigned int flags;
10251 +       aufs_bindex_t bindex, btail, btop;
10252 +       struct dentry *dentry, *h_dentry;
10253 +       struct file *h_file;
10254 +
10255 +       /* open all lower dirs */
10256 +       dentry = file->f_path.dentry;
10257 +       btop = au_dbtop(dentry);
10258 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10259 +               au_set_h_fptr(file, bindex, NULL);
10260 +       au_set_fbtop(file, btop);
10261 +
10262 +       btail = au_dbtaildir(dentry);
10263 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10264 +               au_set_h_fptr(file, bindex, NULL);
10265 +       au_set_fbbot_dir(file, btail);
10266 +
10267 +       flags = vfsub_file_flags(file);
10268 +       for (bindex = btop; bindex <= btail; bindex++) {
10269 +               h_dentry = au_h_dptr(dentry, bindex);
10270 +               if (!h_dentry)
10271 +                       continue;
10272 +               h_file = au_hf_dir(file, bindex);
10273 +               if (h_file)
10274 +                       continue;
10275 +
10276 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10277 +               err = PTR_ERR(h_file);
10278 +               if (IS_ERR(h_file))
10279 +                       goto out; /* close all? */
10280 +               au_set_h_fptr(file, bindex, h_file);
10281 +       }
10282 +       au_update_figen(file);
10283 +       /* todo: necessary? */
10284 +       /* file->f_ra = h_file->f_ra; */
10285 +       err = 0;
10286 +
10287 +out:
10288 +       return err;
10289 +}
10290 +
10291 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10292 +{
10293 +       int err;
10294 +       aufs_bindex_t bindex, btail;
10295 +       struct dentry *dentry, *h_dentry;
10296 +       struct vfsmount *mnt;
10297 +
10298 +       FiMustWriteLock(file);
10299 +       AuDebugOn(h_file);
10300 +
10301 +       err = 0;
10302 +       mnt = file->f_path.mnt;
10303 +       dentry = file->f_path.dentry;
10304 +       file->f_version = inode_query_iversion(d_inode(dentry));
10305 +       bindex = au_dbtop(dentry);
10306 +       au_set_fbtop(file, bindex);
10307 +       btail = au_dbtaildir(dentry);
10308 +       au_set_fbbot_dir(file, btail);
10309 +       for (; !err && bindex <= btail; bindex++) {
10310 +               h_dentry = au_h_dptr(dentry, bindex);
10311 +               if (!h_dentry)
10312 +                       continue;
10313 +
10314 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10315 +               if (unlikely(err))
10316 +                       break;
10317 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10318 +               if (IS_ERR(h_file)) {
10319 +                       err = PTR_ERR(h_file);
10320 +                       break;
10321 +               }
10322 +               au_set_h_fptr(file, bindex, h_file);
10323 +       }
10324 +       au_update_figen(file);
10325 +       /* todo: necessary? */
10326 +       /* file->f_ra = h_file->f_ra; */
10327 +       if (!err)
10328 +               return 0; /* success */
10329 +
10330 +       /* close all */
10331 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10332 +               au_set_h_fptr(file, bindex, NULL);
10333 +       au_set_fbtop(file, -1);
10334 +       au_set_fbbot_dir(file, -1);
10335 +
10336 +       return err;
10337 +}
10338 +
10339 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10340 +                        struct file *file)
10341 +{
10342 +       int err;
10343 +       struct super_block *sb;
10344 +       struct au_fidir *fidir;
10345 +
10346 +       err = -ENOMEM;
10347 +       sb = file->f_path.dentry->d_sb;
10348 +       si_read_lock(sb, AuLock_FLUSH);
10349 +       fidir = au_fidir_alloc(sb);
10350 +       if (fidir) {
10351 +               struct au_do_open_args args = {
10352 +                       .open   = do_open_dir,
10353 +                       .fidir  = fidir
10354 +               };
10355 +               err = au_do_open(file, &args);
10356 +               if (unlikely(err))
10357 +                       au_kfree_rcu(fidir);
10358 +       }
10359 +       si_read_unlock(sb);
10360 +       return err;
10361 +}
10362 +
10363 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10364 +                           struct file *file)
10365 +{
10366 +       struct au_vdir *vdir_cache;
10367 +       struct au_finfo *finfo;
10368 +       struct au_fidir *fidir;
10369 +       struct au_hfile *hf;
10370 +       aufs_bindex_t bindex, bbot;
10371 +
10372 +       finfo = au_fi(file);
10373 +       fidir = finfo->fi_hdir;
10374 +       if (fidir) {
10375 +               au_hbl_del(&finfo->fi_hlist,
10376 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10377 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10378 +               if (vdir_cache)
10379 +                       au_vdir_free(vdir_cache);
10380 +
10381 +               bindex = finfo->fi_btop;
10382 +               if (bindex >= 0) {
10383 +                       hf = fidir->fd_hfile + bindex;
10384 +                       /*
10385 +                        * calls fput() instead of filp_close(),
10386 +                        * since no dnotify or lock for the lower file.
10387 +                        */
10388 +                       bbot = fidir->fd_bbot;
10389 +                       for (; bindex <= bbot; bindex++, hf++)
10390 +                               if (hf->hf_file)
10391 +                                       au_hfput(hf, /*execed*/0);
10392 +               }
10393 +               au_kfree_rcu(fidir);
10394 +               finfo->fi_hdir = NULL;
10395 +       }
10396 +       au_finfo_fin(file);
10397 +       return 0;
10398 +}
10399 +
10400 +/* ---------------------------------------------------------------------- */
10401 +
10402 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10403 +{
10404 +       int err;
10405 +       aufs_bindex_t bindex, bbot;
10406 +       struct file *h_file;
10407 +
10408 +       err = 0;
10409 +       bbot = au_fbbot_dir(file);
10410 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10411 +               h_file = au_hf_dir(file, bindex);
10412 +               if (h_file)
10413 +                       err = vfsub_flush(h_file, id);
10414 +       }
10415 +       return err;
10416 +}
10417 +
10418 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10419 +{
10420 +       return au_do_flush(file, id, au_do_flush_dir);
10421 +}
10422 +
10423 +/* ---------------------------------------------------------------------- */
10424 +
10425 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10426 +{
10427 +       int err;
10428 +       aufs_bindex_t bbot, bindex;
10429 +       struct inode *inode;
10430 +       struct super_block *sb;
10431 +
10432 +       err = 0;
10433 +       sb = dentry->d_sb;
10434 +       inode = d_inode(dentry);
10435 +       IMustLock(inode);
10436 +       bbot = au_dbbot(dentry);
10437 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10438 +               struct path h_path;
10439 +
10440 +               if (au_test_ro(sb, bindex, inode))
10441 +                       continue;
10442 +               h_path.dentry = au_h_dptr(dentry, bindex);
10443 +               if (!h_path.dentry)
10444 +                       continue;
10445 +
10446 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10447 +               err = vfsub_fsync(NULL, &h_path, datasync);
10448 +       }
10449 +
10450 +       return err;
10451 +}
10452 +
10453 +static int au_do_fsync_dir(struct file *file, int datasync)
10454 +{
10455 +       int err;
10456 +       aufs_bindex_t bbot, bindex;
10457 +       struct file *h_file;
10458 +       struct super_block *sb;
10459 +       struct inode *inode;
10460 +
10461 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10462 +       if (unlikely(err))
10463 +               goto out;
10464 +
10465 +       inode = file_inode(file);
10466 +       sb = inode->i_sb;
10467 +       bbot = au_fbbot_dir(file);
10468 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10469 +               h_file = au_hf_dir(file, bindex);
10470 +               if (!h_file || au_test_ro(sb, bindex, inode))
10471 +                       continue;
10472 +
10473 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10474 +       }
10475 +
10476 +out:
10477 +       return err;
10478 +}
10479 +
10480 +/*
10481 + * @file may be NULL
10482 + */
10483 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10484 +                         int datasync)
10485 +{
10486 +       int err;
10487 +       struct dentry *dentry;
10488 +       struct inode *inode;
10489 +       struct super_block *sb;
10490 +
10491 +       err = 0;
10492 +       dentry = file->f_path.dentry;
10493 +       inode = d_inode(dentry);
10494 +       inode_lock(inode);
10495 +       sb = dentry->d_sb;
10496 +       si_noflush_read_lock(sb);
10497 +       if (file)
10498 +               err = au_do_fsync_dir(file, datasync);
10499 +       else {
10500 +               di_write_lock_child(dentry);
10501 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10502 +       }
10503 +       au_cpup_attr_timesizes(inode);
10504 +       di_write_unlock(dentry);
10505 +       if (file)
10506 +               fi_write_unlock(file);
10507 +
10508 +       si_read_unlock(sb);
10509 +       inode_unlock(inode);
10510 +       return err;
10511 +}
10512 +
10513 +/* ---------------------------------------------------------------------- */
10514 +
10515 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10516 +{
10517 +       int err;
10518 +       struct dentry *dentry;
10519 +       struct inode *inode, *h_inode;
10520 +       struct super_block *sb;
10521 +
10522 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10523 +
10524 +       dentry = file->f_path.dentry;
10525 +       inode = d_inode(dentry);
10526 +       IMustLock(inode);
10527 +
10528 +       sb = dentry->d_sb;
10529 +       si_read_lock(sb, AuLock_FLUSH);
10530 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10531 +       if (unlikely(err))
10532 +               goto out;
10533 +       err = au_alive_dir(dentry);
10534 +       if (!err)
10535 +               err = au_vdir_init(file);
10536 +       di_downgrade_lock(dentry, AuLock_IR);
10537 +       if (unlikely(err))
10538 +               goto out_unlock;
10539 +
10540 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10541 +       if (!au_test_nfsd()) {
10542 +               err = au_vdir_fill_de(file, ctx);
10543 +               fsstack_copy_attr_atime(inode, h_inode);
10544 +       } else {
10545 +               /*
10546 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10547 +                * encode_fh() and others.
10548 +                */
10549 +               atomic_inc(&h_inode->i_count);
10550 +               di_read_unlock(dentry, AuLock_IR);
10551 +               si_read_unlock(sb);
10552 +               err = au_vdir_fill_de(file, ctx);
10553 +               fsstack_copy_attr_atime(inode, h_inode);
10554 +               fi_write_unlock(file);
10555 +               iput(h_inode);
10556 +
10557 +               AuTraceErr(err);
10558 +               return err;
10559 +       }
10560 +
10561 +out_unlock:
10562 +       di_read_unlock(dentry, AuLock_IR);
10563 +       fi_write_unlock(file);
10564 +out:
10565 +       si_read_unlock(sb);
10566 +       return err;
10567 +}
10568 +
10569 +/* ---------------------------------------------------------------------- */
10570 +
10571 +#define AuTestEmpty_WHONLY     1
10572 +#define AuTestEmpty_CALLED     (1 << 1)
10573 +#define AuTestEmpty_SHWH       (1 << 2)
10574 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10575 +#define au_fset_testempty(flags, name) \
10576 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10577 +#define au_fclr_testempty(flags, name) \
10578 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10579 +
10580 +#ifndef CONFIG_AUFS_SHWH
10581 +#undef AuTestEmpty_SHWH
10582 +#define AuTestEmpty_SHWH       0
10583 +#endif
10584 +
10585 +struct test_empty_arg {
10586 +       struct dir_context ctx;
10587 +       struct au_nhash *whlist;
10588 +       unsigned int flags;
10589 +       int err;
10590 +       aufs_bindex_t bindex;
10591 +};
10592 +
10593 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10594 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10595 +                        unsigned int d_type)
10596 +{
10597 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10598 +                                                 ctx);
10599 +       char *name = (void *)__name;
10600 +
10601 +       arg->err = 0;
10602 +       au_fset_testempty(arg->flags, CALLED);
10603 +       /* smp_mb(); */
10604 +       if (name[0] == '.'
10605 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10606 +               goto out; /* success */
10607 +
10608 +       if (namelen <= AUFS_WH_PFX_LEN
10609 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10610 +               if (au_ftest_testempty(arg->flags, WHONLY)
10611 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10612 +                       arg->err = -ENOTEMPTY;
10613 +               goto out;
10614 +       }
10615 +
10616 +       name += AUFS_WH_PFX_LEN;
10617 +       namelen -= AUFS_WH_PFX_LEN;
10618 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10619 +               arg->err = au_nhash_append_wh
10620 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10621 +                        au_ftest_testempty(arg->flags, SHWH));
10622 +
10623 +out:
10624 +       /* smp_mb(); */
10625 +       AuTraceErr(arg->err);
10626 +       return arg->err;
10627 +}
10628 +
10629 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10630 +{
10631 +       int err;
10632 +       struct file *h_file;
10633 +       struct au_branch *br;
10634 +
10635 +       h_file = au_h_open(dentry, arg->bindex,
10636 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10637 +                          /*file*/NULL, /*force_wr*/0);
10638 +       err = PTR_ERR(h_file);
10639 +       if (IS_ERR(h_file))
10640 +               goto out;
10641 +
10642 +       err = 0;
10643 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10644 +           && !file_inode(h_file)->i_nlink)
10645 +               goto out_put;
10646 +
10647 +       do {
10648 +               arg->err = 0;
10649 +               au_fclr_testempty(arg->flags, CALLED);
10650 +               /* smp_mb(); */
10651 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10652 +               if (err >= 0)
10653 +                       err = arg->err;
10654 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10655 +
10656 +out_put:
10657 +       fput(h_file);
10658 +       br = au_sbr(dentry->d_sb, arg->bindex);
10659 +       au_lcnt_dec(&br->br_nfiles);
10660 +out:
10661 +       return err;
10662 +}
10663 +
10664 +struct do_test_empty_args {
10665 +       int *errp;
10666 +       struct dentry *dentry;
10667 +       struct test_empty_arg *arg;
10668 +};
10669 +
10670 +static void call_do_test_empty(void *args)
10671 +{
10672 +       struct do_test_empty_args *a = args;
10673 +       *a->errp = do_test_empty(a->dentry, a->arg);
10674 +}
10675 +
10676 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10677 +{
10678 +       int err, wkq_err;
10679 +       struct dentry *h_dentry;
10680 +       struct inode *h_inode;
10681 +
10682 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10683 +       h_inode = d_inode(h_dentry);
10684 +       /* todo: i_mode changes anytime? */
10685 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10686 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10687 +       inode_unlock_shared(h_inode);
10688 +       if (!err)
10689 +               err = do_test_empty(dentry, arg);
10690 +       else {
10691 +               struct do_test_empty_args args = {
10692 +                       .errp   = &err,
10693 +                       .dentry = dentry,
10694 +                       .arg    = arg
10695 +               };
10696 +               unsigned int flags = arg->flags;
10697 +
10698 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10699 +               if (unlikely(wkq_err))
10700 +                       err = wkq_err;
10701 +               arg->flags = flags;
10702 +       }
10703 +
10704 +       return err;
10705 +}
10706 +
10707 +int au_test_empty_lower(struct dentry *dentry)
10708 +{
10709 +       int err;
10710 +       unsigned int rdhash;
10711 +       aufs_bindex_t bindex, btop, btail;
10712 +       struct au_nhash whlist;
10713 +       struct test_empty_arg arg = {
10714 +               .ctx = {
10715 +                       .actor = test_empty_cb
10716 +               }
10717 +       };
10718 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10719 +
10720 +       SiMustAnyLock(dentry->d_sb);
10721 +
10722 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10723 +       if (!rdhash)
10724 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10725 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10726 +       if (unlikely(err))
10727 +               goto out;
10728 +
10729 +       arg.flags = 0;
10730 +       arg.whlist = &whlist;
10731 +       btop = au_dbtop(dentry);
10732 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10733 +               au_fset_testempty(arg.flags, SHWH);
10734 +       test_empty = do_test_empty;
10735 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10736 +               test_empty = sio_test_empty;
10737 +       arg.bindex = btop;
10738 +       err = test_empty(dentry, &arg);
10739 +       if (unlikely(err))
10740 +               goto out_whlist;
10741 +
10742 +       au_fset_testempty(arg.flags, WHONLY);
10743 +       btail = au_dbtaildir(dentry);
10744 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10745 +               struct dentry *h_dentry;
10746 +
10747 +               h_dentry = au_h_dptr(dentry, bindex);
10748 +               if (h_dentry && d_is_positive(h_dentry)) {
10749 +                       arg.bindex = bindex;
10750 +                       err = test_empty(dentry, &arg);
10751 +               }
10752 +       }
10753 +
10754 +out_whlist:
10755 +       au_nhash_wh_free(&whlist);
10756 +out:
10757 +       return err;
10758 +}
10759 +
10760 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10761 +{
10762 +       int err;
10763 +       struct test_empty_arg arg = {
10764 +               .ctx = {
10765 +                       .actor = test_empty_cb
10766 +               }
10767 +       };
10768 +       aufs_bindex_t bindex, btail;
10769 +
10770 +       err = 0;
10771 +       arg.whlist = whlist;
10772 +       arg.flags = AuTestEmpty_WHONLY;
10773 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10774 +               au_fset_testempty(arg.flags, SHWH);
10775 +       btail = au_dbtaildir(dentry);
10776 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10777 +               struct dentry *h_dentry;
10778 +
10779 +               h_dentry = au_h_dptr(dentry, bindex);
10780 +               if (h_dentry && d_is_positive(h_dentry)) {
10781 +                       arg.bindex = bindex;
10782 +                       err = sio_test_empty(dentry, &arg);
10783 +               }
10784 +       }
10785 +
10786 +       return err;
10787 +}
10788 +
10789 +/* ---------------------------------------------------------------------- */
10790 +
10791 +const struct file_operations aufs_dir_fop = {
10792 +       .owner          = THIS_MODULE,
10793 +       .llseek         = default_llseek,
10794 +       .read           = generic_read_dir,
10795 +       .iterate_shared = aufs_iterate_shared,
10796 +       .unlocked_ioctl = aufs_ioctl_dir,
10797 +#ifdef CONFIG_COMPAT
10798 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10799 +#endif
10800 +       .open           = aufs_open_dir,
10801 +       .release        = aufs_release_dir,
10802 +       .flush          = aufs_flush_dir,
10803 +       .fsync          = aufs_fsync_dir
10804 +};
10805 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10806 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10807 +++ linux/fs/aufs/dir.h 2020-08-03 09:14:46.095748745 +0200
10808 @@ -0,0 +1,134 @@
10809 +/* SPDX-License-Identifier: GPL-2.0 */
10810 +/*
10811 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10812 + *
10813 + * This program, aufs is free software; you can redistribute it and/or modify
10814 + * it under the terms of the GNU General Public License as published by
10815 + * the Free Software Foundation; either version 2 of the License, or
10816 + * (at your option) any later version.
10817 + *
10818 + * This program is distributed in the hope that it will be useful,
10819 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10820 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10821 + * GNU General Public License for more details.
10822 + *
10823 + * You should have received a copy of the GNU General Public License
10824 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10825 + */
10826 +
10827 +/*
10828 + * directory operations
10829 + */
10830 +
10831 +#ifndef __AUFS_DIR_H__
10832 +#define __AUFS_DIR_H__
10833 +
10834 +#ifdef __KERNEL__
10835 +
10836 +#include <linux/fs.h>
10837 +
10838 +/* ---------------------------------------------------------------------- */
10839 +
10840 +/* need to be faster and smaller */
10841 +
10842 +struct au_nhash {
10843 +       unsigned int            nh_num;
10844 +       struct hlist_head       *nh_head;
10845 +};
10846 +
10847 +struct au_vdir_destr {
10848 +       unsigned char   len;
10849 +       unsigned char   name[];
10850 +} __packed;
10851 +
10852 +struct au_vdir_dehstr {
10853 +       struct hlist_node       hash;
10854 +       struct au_vdir_destr    *str;
10855 +       struct rcu_head         rcu;
10856 +} ____cacheline_aligned_in_smp;
10857 +
10858 +struct au_vdir_de {
10859 +       ino_t                   de_ino;
10860 +       unsigned char           de_type;
10861 +       /* caution: packed */
10862 +       struct au_vdir_destr    de_str;
10863 +} __packed;
10864 +
10865 +struct au_vdir_wh {
10866 +       struct hlist_node       wh_hash;
10867 +#ifdef CONFIG_AUFS_SHWH
10868 +       ino_t                   wh_ino;
10869 +       aufs_bindex_t           wh_bindex;
10870 +       unsigned char           wh_type;
10871 +#else
10872 +       aufs_bindex_t           wh_bindex;
10873 +#endif
10874 +       /* caution: packed */
10875 +       struct au_vdir_destr    wh_str;
10876 +} __packed;
10877 +
10878 +union au_vdir_deblk_p {
10879 +       unsigned char           *deblk;
10880 +       struct au_vdir_de       *de;
10881 +};
10882 +
10883 +struct au_vdir {
10884 +       unsigned char   **vd_deblk;
10885 +       unsigned long   vd_nblk;
10886 +       struct {
10887 +               unsigned long           ul;
10888 +               union au_vdir_deblk_p   p;
10889 +       } vd_last;
10890 +
10891 +       u64             vd_version;
10892 +       unsigned int    vd_deblk_sz;
10893 +       unsigned long   vd_jiffy;
10894 +       struct rcu_head rcu;
10895 +} ____cacheline_aligned_in_smp;
10896 +
10897 +/* ---------------------------------------------------------------------- */
10898 +
10899 +/* dir.c */
10900 +extern const struct file_operations aufs_dir_fop;
10901 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10902 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10903 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10904 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10905 +int au_test_empty_lower(struct dentry *dentry);
10906 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10907 +
10908 +/* vdir.c */
10909 +unsigned int au_rdhash_est(loff_t sz);
10910 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10911 +void au_nhash_wh_free(struct au_nhash *whlist);
10912 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10913 +                           int limit);
10914 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10915 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10916 +                      unsigned int d_type, aufs_bindex_t bindex,
10917 +                      unsigned char shwh);
10918 +void au_vdir_free(struct au_vdir *vdir);
10919 +int au_vdir_init(struct file *file);
10920 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10921 +
10922 +/* ioctl.c */
10923 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10924 +
10925 +#ifdef CONFIG_AUFS_RDU
10926 +/* rdu.c */
10927 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10928 +#ifdef CONFIG_COMPAT
10929 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10930 +                        unsigned long arg);
10931 +#endif
10932 +#else
10933 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10934 +       unsigned int cmd, unsigned long arg)
10935 +#ifdef CONFIG_COMPAT
10936 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10937 +       unsigned int cmd, unsigned long arg)
10938 +#endif
10939 +#endif
10940 +
10941 +#endif /* __KERNEL__ */
10942 +#endif /* __AUFS_DIR_H__ */
10943 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10944 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10945 +++ linux/fs/aufs/dirren.c      2020-08-03 09:14:46.095748745 +0200
10946 @@ -0,0 +1,1316 @@
10947 +// SPDX-License-Identifier: GPL-2.0
10948 +/*
10949 + * Copyright (C) 2017-2020 Junjiro R. Okajima
10950 + *
10951 + * This program, aufs is free software; you can redistribute it and/or modify
10952 + * it under the terms of the GNU General Public License as published by
10953 + * the Free Software Foundation; either version 2 of the License, or
10954 + * (at your option) any later version.
10955 + *
10956 + * This program is distributed in the hope that it will be useful,
10957 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10958 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10959 + * GNU General Public License for more details.
10960 + *
10961 + * You should have received a copy of the GNU General Public License
10962 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10963 + */
10964 +
10965 +/*
10966 + * special handling in renaming a directory
10967 + * in order to support looking-up the before-renamed name on the lower readonly
10968 + * branches
10969 + */
10970 +
10971 +#include <linux/byteorder/generic.h>
10972 +#include "aufs.h"
10973 +
10974 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10975 +{
10976 +       int idx;
10977 +
10978 +       idx = au_dr_ihash(ent->dr_h_ino);
10979 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10980 +}
10981 +
10982 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10983 +{
10984 +       int ret, i;
10985 +       struct hlist_bl_head *hbl;
10986 +
10987 +       ret = 1;
10988 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10989 +               hbl = dr->dr_h_ino + i;
10990 +               hlist_bl_lock(hbl);
10991 +               ret &= hlist_bl_empty(hbl);
10992 +               hlist_bl_unlock(hbl);
10993 +       }
10994 +
10995 +       return ret;
10996 +}
10997 +
10998 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10999 +{
11000 +       struct au_dr_hino *found, *ent;
11001 +       struct hlist_bl_head *hbl;
11002 +       struct hlist_bl_node *pos;
11003 +       int idx;
11004 +
11005 +       found = NULL;
11006 +       idx = au_dr_ihash(ino);
11007 +       hbl = dr->dr_h_ino + idx;
11008 +       hlist_bl_lock(hbl);
11009 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11010 +               if (ent->dr_h_ino == ino) {
11011 +                       found = ent;
11012 +                       break;
11013 +               }
11014 +       hlist_bl_unlock(hbl);
11015 +
11016 +       return found;
11017 +}
11018 +
11019 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11020 +                       struct au_dr_hino *add_ent)
11021 +{
11022 +       int found, idx;
11023 +       struct hlist_bl_head *hbl;
11024 +       struct hlist_bl_node *pos;
11025 +       struct au_dr_hino *ent;
11026 +
11027 +       found = 0;
11028 +       idx = au_dr_ihash(ino);
11029 +       hbl = dr->dr_h_ino + idx;
11030 +#if 0 /* debug print */
11031 +       {
11032 +               struct hlist_bl_node *tmp;
11033 +
11034 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11035 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11036 +       }
11037 +#endif
11038 +       hlist_bl_lock(hbl);
11039 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11040 +               if (ent->dr_h_ino == ino) {
11041 +                       found = 1;
11042 +                       break;
11043 +               }
11044 +       if (!found && add_ent)
11045 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11046 +       hlist_bl_unlock(hbl);
11047 +
11048 +       if (!found && add_ent)
11049 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11050 +
11051 +       return found;
11052 +}
11053 +
11054 +void au_dr_hino_free(struct au_dr_br *dr)
11055 +{
11056 +       int i;
11057 +       struct hlist_bl_head *hbl;
11058 +       struct hlist_bl_node *pos, *tmp;
11059 +       struct au_dr_hino *ent;
11060 +
11061 +       /* SiMustWriteLock(sb); */
11062 +
11063 +       for (i = 0; i < AuDirren_NHASH; i++) {
11064 +               hbl = dr->dr_h_ino + i;
11065 +               /* no spinlock since sbinfo must be write-locked */
11066 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11067 +                       au_kfree_rcu(ent);
11068 +               INIT_HLIST_BL_HEAD(hbl);
11069 +       }
11070 +}
11071 +
11072 +/* returns the number of inodes or an error */
11073 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11074 +                           struct file *hinofile)
11075 +{
11076 +       int err, i;
11077 +       ssize_t ssz;
11078 +       loff_t pos, oldsize;
11079 +       __be64 u64;
11080 +       struct inode *hinoinode;
11081 +       struct hlist_bl_head *hbl;
11082 +       struct hlist_bl_node *n1, *n2;
11083 +       struct au_dr_hino *ent;
11084 +
11085 +       SiMustWriteLock(sb);
11086 +       AuDebugOn(!au_br_writable(br->br_perm));
11087 +
11088 +       hinoinode = file_inode(hinofile);
11089 +       oldsize = i_size_read(hinoinode);
11090 +
11091 +       err = 0;
11092 +       pos = 0;
11093 +       hbl = br->br_dirren.dr_h_ino;
11094 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11095 +               /* no bit-lock since sbinfo must be write-locked */
11096 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11097 +                       AuDbg("hi%llu, %pD2\n",
11098 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11099 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11100 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11101 +                       if (ssz == sizeof(u64))
11102 +                               continue;
11103 +
11104 +                       /* write error */
11105 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11106 +                       err = -ENOSPC;
11107 +                       if (ssz < 0)
11108 +                               err = ssz;
11109 +                       break;
11110 +               }
11111 +       }
11112 +       /* regardless the error */
11113 +       if (pos < oldsize) {
11114 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11115 +               AuTraceErr(err);
11116 +       }
11117 +
11118 +       AuTraceErr(err);
11119 +       return err;
11120 +}
11121 +
11122 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11123 +{
11124 +       int err, hidx;
11125 +       ssize_t ssz;
11126 +       size_t sz, n;
11127 +       loff_t pos;
11128 +       uint64_t u64;
11129 +       struct au_dr_hino *ent;
11130 +       struct inode *hinoinode;
11131 +       struct hlist_bl_head *hbl;
11132 +
11133 +       err = 0;
11134 +       pos = 0;
11135 +       hbl = dr->dr_h_ino;
11136 +       hinoinode = file_inode(hinofile);
11137 +       sz = i_size_read(hinoinode);
11138 +       AuDebugOn(sz % sizeof(u64));
11139 +       n = sz / sizeof(u64);
11140 +       while (n--) {
11141 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11142 +               if (unlikely(ssz != sizeof(u64))) {
11143 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11144 +                       err = -EINVAL;
11145 +                       if (ssz < 0)
11146 +                               err = ssz;
11147 +                       goto out_free;
11148 +               }
11149 +
11150 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11151 +               if (!ent) {
11152 +                       err = -ENOMEM;
11153 +                       AuTraceErr(err);
11154 +                       goto out_free;
11155 +               }
11156 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11157 +               AuDbg("hi%llu, %pD2\n",
11158 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11159 +               hidx = au_dr_ihash(ent->dr_h_ino);
11160 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11161 +       }
11162 +       goto out; /* success */
11163 +
11164 +out_free:
11165 +       au_dr_hino_free(dr);
11166 +out:
11167 +       AuTraceErr(err);
11168 +       return err;
11169 +}
11170 +
11171 +/*
11172 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11173 + * @path is a switch to distinguish load and store.
11174 + */
11175 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11176 +                     struct au_branch *br, const struct path *path)
11177 +{
11178 +       int err, flags;
11179 +       unsigned char load, suspend;
11180 +       struct file *hinofile;
11181 +       struct au_hinode *hdir;
11182 +       struct inode *dir, *delegated;
11183 +       struct path hinopath;
11184 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11185 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11186 +
11187 +       AuDebugOn(bindex < 0 && !br);
11188 +       AuDebugOn(bindex >= 0 && br);
11189 +
11190 +       err = -EINVAL;
11191 +       suspend = !br;
11192 +       if (suspend)
11193 +               br = au_sbr(sb, bindex);
11194 +       load = !!path;
11195 +       if (!load) {
11196 +               path = &br->br_path;
11197 +               AuDebugOn(!au_br_writable(br->br_perm));
11198 +               if (unlikely(!au_br_writable(br->br_perm)))
11199 +                       goto out;
11200 +       }
11201 +
11202 +       hdir = NULL;
11203 +       if (suspend) {
11204 +               dir = d_inode(sb->s_root);
11205 +               hdir = au_hinode(au_ii(dir), bindex);
11206 +               dir = hdir->hi_inode;
11207 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11208 +       } else {
11209 +               dir = d_inode(path->dentry);
11210 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11211 +       }
11212 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11213 +       err = PTR_ERR(hinopath.dentry);
11214 +       if (IS_ERR(hinopath.dentry))
11215 +               goto out_unlock;
11216 +
11217 +       err = 0;
11218 +       flags = O_RDONLY;
11219 +       if (load) {
11220 +               if (d_is_negative(hinopath.dentry))
11221 +                       goto out_dput; /* success */
11222 +       } else {
11223 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11224 +                       if (d_is_positive(hinopath.dentry)) {
11225 +                               delegated = NULL;
11226 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11227 +                                                  /*force*/0);
11228 +                               AuTraceErr(err);
11229 +                               if (unlikely(err))
11230 +                                       pr_err("ignored err %d, %pd2\n",
11231 +                                              err, hinopath.dentry);
11232 +                               if (unlikely(err == -EWOULDBLOCK))
11233 +                                       iput(delegated);
11234 +                               err = 0;
11235 +                       }
11236 +                       goto out_dput;
11237 +               } else if (!d_is_positive(hinopath.dentry)) {
11238 +                       err = vfsub_create(dir, &hinopath, 0600,
11239 +                                          /*want_excl*/false);
11240 +                       AuTraceErr(err);
11241 +                       if (unlikely(err))
11242 +                               goto out_dput;
11243 +               }
11244 +               flags = O_WRONLY;
11245 +       }
11246 +       hinopath.mnt = path->mnt;
11247 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11248 +       if (suspend)
11249 +               au_hn_inode_unlock(hdir);
11250 +       else
11251 +               inode_unlock(dir);
11252 +       dput(hinopath.dentry);
11253 +       AuTraceErrPtr(hinofile);
11254 +       if (IS_ERR(hinofile)) {
11255 +               err = PTR_ERR(hinofile);
11256 +               goto out;
11257 +       }
11258 +
11259 +       if (load)
11260 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11261 +       else
11262 +               err = au_dr_hino_store(sb, br, hinofile);
11263 +       fput(hinofile);
11264 +       goto out;
11265 +
11266 +out_dput:
11267 +       dput(hinopath.dentry);
11268 +out_unlock:
11269 +       if (suspend)
11270 +               au_hn_inode_unlock(hdir);
11271 +       else
11272 +               inode_unlock(dir);
11273 +out:
11274 +       AuTraceErr(err);
11275 +       return err;
11276 +}
11277 +
11278 +/* ---------------------------------------------------------------------- */
11279 +
11280 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11281 +{
11282 +       int err;
11283 +       struct kstatfs kstfs;
11284 +       dev_t dev;
11285 +       struct dentry *dentry;
11286 +       struct super_block *sb;
11287 +
11288 +       err = vfs_statfs((void *)path, &kstfs);
11289 +       AuTraceErr(err);
11290 +       if (unlikely(err))
11291 +               goto out;
11292 +
11293 +       /* todo: support for UUID */
11294 +
11295 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11296 +               brid->type = AuBrid_FSID;
11297 +               brid->fsid = kstfs.f_fsid;
11298 +       } else {
11299 +               dentry = path->dentry;
11300 +               sb = dentry->d_sb;
11301 +               dev = sb->s_dev;
11302 +               if (dev) {
11303 +                       brid->type = AuBrid_DEV;
11304 +                       brid->dev = dev;
11305 +               }
11306 +       }
11307 +
11308 +out:
11309 +       return err;
11310 +}
11311 +
11312 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11313 +                 const struct path *path)
11314 +{
11315 +       int err, i;
11316 +       struct au_dr_br *dr;
11317 +       struct hlist_bl_head *hbl;
11318 +
11319 +       dr = &br->br_dirren;
11320 +       hbl = dr->dr_h_ino;
11321 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11322 +               INIT_HLIST_BL_HEAD(hbl);
11323 +
11324 +       err = au_dr_brid_init(&dr->dr_brid, path);
11325 +       if (unlikely(err))
11326 +               goto out;
11327 +
11328 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11329 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11330 +
11331 +out:
11332 +       AuTraceErr(err);
11333 +       return err;
11334 +}
11335 +
11336 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11337 +{
11338 +       int err;
11339 +
11340 +       err = 0;
11341 +       if (au_br_writable(br->br_perm))
11342 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11343 +       if (!err)
11344 +               au_dr_hino_free(&br->br_dirren);
11345 +
11346 +       return err;
11347 +}
11348 +
11349 +/* ---------------------------------------------------------------------- */
11350 +
11351 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11352 +                      char *buf, size_t sz)
11353 +{
11354 +       int err;
11355 +       unsigned int major, minor;
11356 +       char *p;
11357 +
11358 +       p = buf;
11359 +       err = snprintf(p, sz, "%d_", brid->type);
11360 +       AuDebugOn(err > sz);
11361 +       p += err;
11362 +       sz -= err;
11363 +       switch (brid->type) {
11364 +       case AuBrid_Unset:
11365 +               return -EINVAL;
11366 +       case AuBrid_UUID:
11367 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11368 +               break;
11369 +       case AuBrid_FSID:
11370 +               err = snprintf(p, sz, "%08x-%08x",
11371 +                              brid->fsid.val[0], brid->fsid.val[1]);
11372 +               break;
11373 +       case AuBrid_DEV:
11374 +               major = MAJOR(brid->dev);
11375 +               minor = MINOR(brid->dev);
11376 +               if (major <= 0xff && minor <= 0xff)
11377 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11378 +               else
11379 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11380 +               break;
11381 +       }
11382 +       AuDebugOn(err > sz);
11383 +       p += err;
11384 +       sz -= err;
11385 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11386 +       AuDebugOn(err > sz);
11387 +       p += err;
11388 +       sz -= err;
11389 +
11390 +       return p - buf;
11391 +}
11392 +
11393 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11394 +{
11395 +       int rlen;
11396 +       struct dentry *br_dentry;
11397 +       struct inode *br_inode;
11398 +
11399 +       br_dentry = au_br_dentry(br);
11400 +       br_inode = d_inode(br_dentry);
11401 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11402 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11403 +       AuDebugOn(rlen > len);
11404 +
11405 +       return rlen;
11406 +}
11407 +
11408 +/* ---------------------------------------------------------------------- */
11409 +
11410 +/*
11411 + * from the given @h_dentry, construct drinfo at @*fdata.
11412 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11413 + * @allocated.
11414 + */
11415 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11416 +                              struct dentry *h_dentry,
11417 +                              unsigned char *allocated)
11418 +{
11419 +       int err, v;
11420 +       struct au_drinfo_fdata *f, *p;
11421 +       struct au_drinfo *drinfo;
11422 +       struct inode *h_inode;
11423 +       struct qstr *qname;
11424 +
11425 +       err = 0;
11426 +       f = *fdata;
11427 +       h_inode = d_inode(h_dentry);
11428 +       qname = &h_dentry->d_name;
11429 +       drinfo = &f->drinfo;
11430 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11431 +       drinfo->oldnamelen = qname->len;
11432 +       if (*allocated < sizeof(*f) + qname->len) {
11433 +               v = roundup_pow_of_two(*allocated + qname->len);
11434 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11435 +               if (unlikely(!p)) {
11436 +                       err = -ENOMEM;
11437 +                       AuTraceErr(err);
11438 +                       goto out;
11439 +               }
11440 +               f = p;
11441 +               *fdata = f;
11442 +               *allocated = v;
11443 +               drinfo = &f->drinfo;
11444 +       }
11445 +       memcpy(drinfo->oldname, qname->name, qname->len);
11446 +       AuDbg("i%llu, %.*s\n",
11447 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11448 +             drinfo->oldname);
11449 +
11450 +out:
11451 +       AuTraceErr(err);
11452 +       return err;
11453 +}
11454 +
11455 +/* callers have to free the return value */
11456 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11457 +{
11458 +       struct au_drinfo *ret, *drinfo;
11459 +       struct au_drinfo_fdata fdata;
11460 +       int len;
11461 +       loff_t pos;
11462 +       ssize_t ssz;
11463 +
11464 +       ret = ERR_PTR(-EIO);
11465 +       pos = 0;
11466 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11467 +       if (unlikely(ssz != sizeof(fdata))) {
11468 +               AuIOErr("ssz %zd, %u, %pD2\n",
11469 +                       ssz, (unsigned int)sizeof(fdata), file);
11470 +               goto out;
11471 +       }
11472 +
11473 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11474 +       switch (fdata.magic) {
11475 +       case AUFS_DRINFO_MAGIC_V1:
11476 +               break;
11477 +       default:
11478 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11479 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11480 +               goto out;
11481 +       }
11482 +
11483 +       drinfo = &fdata.drinfo;
11484 +       len = drinfo->oldnamelen;
11485 +       if (!len) {
11486 +               AuIOErr("broken drinfo %pD2\n", file);
11487 +               goto out;
11488 +       }
11489 +
11490 +       ret = NULL;
11491 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11492 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11493 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11494 +                     (unsigned long long)drinfo->ino,
11495 +                     (unsigned long long)h_ino, file);
11496 +               goto out; /* success */
11497 +       }
11498 +
11499 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11500 +       if (unlikely(!ret)) {
11501 +               ret = ERR_PTR(-ENOMEM);
11502 +               AuTraceErrPtr(ret);
11503 +               goto out;
11504 +       }
11505 +
11506 +       *ret = *drinfo;
11507 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11508 +       if (unlikely(ssz != len)) {
11509 +               au_kfree_rcu(ret);
11510 +               ret = ERR_PTR(-EIO);
11511 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11512 +               goto out;
11513 +       }
11514 +
11515 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11516 +
11517 +out:
11518 +       return ret;
11519 +}
11520 +
11521 +/* ---------------------------------------------------------------------- */
11522 +
11523 +/* in order to be revertible */
11524 +struct au_drinfo_rev_elm {
11525 +       int                     created;
11526 +       struct dentry           *info_dentry;
11527 +       struct au_drinfo        *info_last;
11528 +};
11529 +
11530 +struct au_drinfo_rev {
11531 +       unsigned char                   already;
11532 +       aufs_bindex_t                   nelm;
11533 +       struct au_drinfo_rev_elm        elm[];
11534 +};
11535 +
11536 +/* todo: isn't it too large? */
11537 +struct au_drinfo_store {
11538 +       struct path h_ppath;
11539 +       struct dentry *h_dentry;
11540 +       struct au_drinfo_fdata *fdata;
11541 +       char *infoname;                 /* inside of whname, just after PFX */
11542 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11543 +       aufs_bindex_t btgt, btail;
11544 +       unsigned char no_sio,
11545 +               allocated,              /* current size of *fdata */
11546 +               infonamelen,            /* room size for p */
11547 +               whnamelen,              /* length of the generated name */
11548 +               renameback;             /* renamed back */
11549 +};
11550 +
11551 +/* on rename(2) error, the caller should revert it using @elm */
11552 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11553 +                             struct au_drinfo_rev_elm *elm)
11554 +{
11555 +       int err, len;
11556 +       ssize_t ssz;
11557 +       loff_t pos;
11558 +       struct path infopath = {
11559 +               .mnt = w->h_ppath.mnt
11560 +       };
11561 +       struct inode *h_dir, *h_inode, *delegated;
11562 +       struct file *infofile;
11563 +       struct qstr *qname;
11564 +
11565 +       AuDebugOn(elm
11566 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11567 +
11568 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11569 +                                              w->whnamelen);
11570 +       AuTraceErrPtr(infopath.dentry);
11571 +       if (IS_ERR(infopath.dentry)) {
11572 +               err = PTR_ERR(infopath.dentry);
11573 +               goto out;
11574 +       }
11575 +
11576 +       err = 0;
11577 +       h_dir = d_inode(w->h_ppath.dentry);
11578 +       if (elm && d_is_negative(infopath.dentry)) {
11579 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11580 +               AuTraceErr(err);
11581 +               if (unlikely(err))
11582 +                       goto out_dput;
11583 +               elm->created = 1;
11584 +               elm->info_dentry = dget(infopath.dentry);
11585 +       }
11586 +
11587 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11588 +       AuTraceErrPtr(infofile);
11589 +       if (IS_ERR(infofile)) {
11590 +               err = PTR_ERR(infofile);
11591 +               goto out_dput;
11592 +       }
11593 +
11594 +       h_inode = d_inode(infopath.dentry);
11595 +       if (elm && i_size_read(h_inode)) {
11596 +               h_inode = d_inode(w->h_dentry);
11597 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11598 +               AuTraceErrPtr(elm->info_last);
11599 +               if (IS_ERR(elm->info_last)) {
11600 +                       err = PTR_ERR(elm->info_last);
11601 +                       elm->info_last = NULL;
11602 +                       AuDebugOn(elm->info_dentry);
11603 +                       goto out_fput;
11604 +               }
11605 +       }
11606 +
11607 +       if (elm && w->renameback) {
11608 +               delegated = NULL;
11609 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11610 +               AuTraceErr(err);
11611 +               if (unlikely(err == -EWOULDBLOCK))
11612 +                       iput(delegated);
11613 +               goto out_fput;
11614 +       }
11615 +
11616 +       pos = 0;
11617 +       qname = &w->h_dentry->d_name;
11618 +       len = sizeof(*w->fdata) + qname->len;
11619 +       if (!elm)
11620 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11621 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11622 +       if (ssz == len) {
11623 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11624 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11625 +               goto out_fput; /* success */
11626 +       } else {
11627 +               err = -EIO;
11628 +               if (ssz < 0)
11629 +                       err = ssz;
11630 +               /* the caller should revert it using @elm */
11631 +       }
11632 +
11633 +out_fput:
11634 +       fput(infofile);
11635 +out_dput:
11636 +       dput(infopath.dentry);
11637 +out:
11638 +       AuTraceErr(err);
11639 +       return err;
11640 +}
11641 +
11642 +struct au_call_drinfo_do_store_args {
11643 +       int *errp;
11644 +       struct au_drinfo_store *w;
11645 +       struct au_drinfo_rev_elm *elm;
11646 +};
11647 +
11648 +static void au_call_drinfo_do_store(void *args)
11649 +{
11650 +       struct au_call_drinfo_do_store_args *a = args;
11651 +
11652 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11653 +}
11654 +
11655 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11656 +                              struct au_drinfo_rev_elm *elm)
11657 +{
11658 +       int err, wkq_err;
11659 +
11660 +       if (w->no_sio)
11661 +               err = au_drinfo_do_store(w, elm);
11662 +       else {
11663 +               struct au_call_drinfo_do_store_args a = {
11664 +                       .errp   = &err,
11665 +                       .w      = w,
11666 +                       .elm    = elm
11667 +               };
11668 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11669 +               if (unlikely(wkq_err))
11670 +                       err = wkq_err;
11671 +       }
11672 +       AuTraceErr(err);
11673 +
11674 +       return err;
11675 +}
11676 +
11677 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11678 +                                    aufs_bindex_t btgt)
11679 +{
11680 +       int err;
11681 +
11682 +       memset(w, 0, sizeof(*w));
11683 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11684 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11685 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11686 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11687 +       w->btgt = btgt;
11688 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11689 +
11690 +       err = -ENOMEM;
11691 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11692 +       if (unlikely(!w->fdata)) {
11693 +               AuTraceErr(err);
11694 +               goto out;
11695 +       }
11696 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11697 +       err = 0;
11698 +
11699 +out:
11700 +       return err;
11701 +}
11702 +
11703 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11704 +{
11705 +       au_kfree_rcu(w->fdata);
11706 +}
11707 +
11708 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11709 +                               struct au_drinfo_store *w)
11710 +{
11711 +       struct au_drinfo_rev_elm *elm;
11712 +       struct inode *h_dir, *delegated;
11713 +       int err, nelm;
11714 +       struct path infopath = {
11715 +               .mnt = w->h_ppath.mnt
11716 +       };
11717 +
11718 +       h_dir = d_inode(w->h_ppath.dentry);
11719 +       IMustLock(h_dir);
11720 +
11721 +       err = 0;
11722 +       elm = rev->elm;
11723 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11724 +               AuDebugOn(elm->created && elm->info_last);
11725 +               if (elm->created) {
11726 +                       AuDbg("here\n");
11727 +                       delegated = NULL;
11728 +                       infopath.dentry = elm->info_dentry;
11729 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11730 +                                          !w->no_sio);
11731 +                       AuTraceErr(err);
11732 +                       if (unlikely(err == -EWOULDBLOCK))
11733 +                               iput(delegated);
11734 +                       dput(elm->info_dentry);
11735 +               } else if (elm->info_last) {
11736 +                       AuDbg("here\n");
11737 +                       w->fdata->drinfo = *elm->info_last;
11738 +                       memcpy(w->fdata->drinfo.oldname,
11739 +                              elm->info_last->oldname,
11740 +                              elm->info_last->oldnamelen);
11741 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11742 +                       au_kfree_rcu(elm->info_last);
11743 +               }
11744 +               if (unlikely(err))
11745 +                       AuIOErr("%d, %s\n", err, w->whname);
11746 +               /* go on even if err */
11747 +       }
11748 +}
11749 +
11750 +/* caller has to call au_dr_rename_fin() later */
11751 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11752 +                          struct qstr *dst_name, void *_rev)
11753 +{
11754 +       int err, sz, nelm;
11755 +       aufs_bindex_t bindex, btail;
11756 +       struct au_drinfo_store work;
11757 +       struct au_drinfo_rev *rev, **p;
11758 +       struct au_drinfo_rev_elm *elm;
11759 +       struct super_block *sb;
11760 +       struct au_branch *br;
11761 +       struct au_hinode *hdir;
11762 +
11763 +       err = au_drinfo_store_work_init(&work, btgt);
11764 +       AuTraceErr(err);
11765 +       if (unlikely(err))
11766 +               goto out;
11767 +
11768 +       err = -ENOMEM;
11769 +       btail = au_dbtaildir(dentry);
11770 +       nelm = btail - btgt;
11771 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11772 +       rev = kcalloc(1, sz, GFP_NOFS);
11773 +       if (unlikely(!rev)) {
11774 +               AuTraceErr(err);
11775 +               goto out_args;
11776 +       }
11777 +       rev->nelm = nelm;
11778 +       elm = rev->elm;
11779 +       p = _rev;
11780 +       *p = rev;
11781 +
11782 +       err = 0;
11783 +       sb = dentry->d_sb;
11784 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11785 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11786 +       hdir = au_hi(d_inode(dentry), btgt);
11787 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11788 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11789 +               work.h_dentry = au_h_dptr(dentry, bindex);
11790 +               if (!work.h_dentry)
11791 +                       continue;
11792 +
11793 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11794 +                                         &work.allocated);
11795 +               AuTraceErr(err);
11796 +               if (unlikely(err))
11797 +                       break;
11798 +
11799 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11800 +               br = au_sbr(sb, bindex);
11801 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11802 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11803 +                                                work.infonamelen);
11804 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11805 +                     work.whnamelen, work.whname,
11806 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11807 +                     work.fdata->drinfo.oldnamelen,
11808 +                     work.fdata->drinfo.oldname);
11809 +
11810 +               err = au_drinfo_store_sio(&work, elm);
11811 +               AuTraceErr(err);
11812 +               if (unlikely(err))
11813 +                       break;
11814 +       }
11815 +       if (unlikely(err)) {
11816 +               /* revert all drinfo */
11817 +               au_drinfo_store_rev(rev, &work);
11818 +               au_kfree_try_rcu(rev);
11819 +               *p = NULL;
11820 +       }
11821 +       au_hn_inode_unlock(hdir);
11822 +
11823 +out_args:
11824 +       au_drinfo_store_work_fin(&work);
11825 +out:
11826 +       return err;
11827 +}
11828 +
11829 +/* ---------------------------------------------------------------------- */
11830 +
11831 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11832 +                struct qstr *dst_name, void *_rev)
11833 +{
11834 +       int err, already;
11835 +       ino_t ino;
11836 +       struct super_block *sb;
11837 +       struct au_branch *br;
11838 +       struct au_dr_br *dr;
11839 +       struct dentry *h_dentry;
11840 +       struct inode *h_inode;
11841 +       struct au_dr_hino *ent;
11842 +       struct au_drinfo_rev *rev, **p;
11843 +
11844 +       AuDbg("bindex %d\n", bindex);
11845 +
11846 +       err = -ENOMEM;
11847 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11848 +       if (unlikely(!ent))
11849 +               goto out;
11850 +
11851 +       sb = src->d_sb;
11852 +       br = au_sbr(sb, bindex);
11853 +       dr = &br->br_dirren;
11854 +       h_dentry = au_h_dptr(src, bindex);
11855 +       h_inode = d_inode(h_dentry);
11856 +       ino = h_inode->i_ino;
11857 +       ent->dr_h_ino = ino;
11858 +       already = au_dr_hino_test_add(dr, ino, ent);
11859 +       AuDbg("b%d, hi%llu, already %d\n",
11860 +             bindex, (unsigned long long)ino, already);
11861 +
11862 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11863 +       AuTraceErr(err);
11864 +       if (!err) {
11865 +               p = _rev;
11866 +               rev = *p;
11867 +               rev->already = already;
11868 +               goto out; /* success */
11869 +       }
11870 +
11871 +       /* revert */
11872 +       if (!already)
11873 +               au_dr_hino_del(dr, ent);
11874 +       au_kfree_rcu(ent);
11875 +
11876 +out:
11877 +       AuTraceErr(err);
11878 +       return err;
11879 +}
11880 +
11881 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11882 +{
11883 +       struct au_drinfo_rev *rev;
11884 +       struct au_drinfo_rev_elm *elm;
11885 +       int nelm;
11886 +
11887 +       rev = _rev;
11888 +       elm = rev->elm;
11889 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11890 +               dput(elm->info_dentry);
11891 +               au_kfree_rcu(elm->info_last);
11892 +       }
11893 +       au_kfree_try_rcu(rev);
11894 +}
11895 +
11896 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11897 +{
11898 +       int err;
11899 +       struct au_drinfo_store work;
11900 +       struct au_drinfo_rev *rev = _rev;
11901 +       struct super_block *sb;
11902 +       struct au_branch *br;
11903 +       struct inode *h_inode;
11904 +       struct au_dr_br *dr;
11905 +       struct au_dr_hino *ent;
11906 +
11907 +       err = au_drinfo_store_work_init(&work, btgt);
11908 +       if (unlikely(err))
11909 +               goto out;
11910 +
11911 +       sb = src->d_sb;
11912 +       br = au_sbr(sb, btgt);
11913 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11914 +       work.h_ppath.mnt = au_br_mnt(br);
11915 +       au_drinfo_store_rev(rev, &work);
11916 +       au_drinfo_store_work_fin(&work);
11917 +       if (rev->already)
11918 +               goto out;
11919 +
11920 +       dr = &br->br_dirren;
11921 +       h_inode = d_inode(work.h_ppath.dentry);
11922 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11923 +       BUG_ON(!ent);
11924 +       au_dr_hino_del(dr, ent);
11925 +       au_kfree_rcu(ent);
11926 +
11927 +out:
11928 +       au_kfree_try_rcu(rev);
11929 +       if (unlikely(err))
11930 +               pr_err("failed to remove dirren info\n");
11931 +}
11932 +
11933 +/* ---------------------------------------------------------------------- */
11934 +
11935 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11936 +                                          char *whname, int whnamelen,
11937 +                                          struct dentry **info_dentry)
11938 +{
11939 +       struct au_drinfo *drinfo;
11940 +       struct file *f;
11941 +       struct inode *h_dir;
11942 +       struct path infopath;
11943 +       int unlocked;
11944 +
11945 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11946 +
11947 +       *info_dentry = NULL;
11948 +       drinfo = NULL;
11949 +       unlocked = 0;
11950 +       h_dir = d_inode(h_ppath->dentry);
11951 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11952 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11953 +                                              whnamelen);
11954 +       if (IS_ERR(infopath.dentry)) {
11955 +               drinfo = (void *)infopath.dentry;
11956 +               goto out;
11957 +       }
11958 +
11959 +       if (d_is_negative(infopath.dentry))
11960 +               goto out_dput; /* success */
11961 +
11962 +       infopath.mnt = h_ppath->mnt;
11963 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11964 +       inode_unlock_shared(h_dir);
11965 +       unlocked = 1;
11966 +       if (IS_ERR(f)) {
11967 +               drinfo = (void *)f;
11968 +               goto out_dput;
11969 +       }
11970 +
11971 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11972 +       if (IS_ERR_OR_NULL(drinfo))
11973 +               goto out_fput;
11974 +
11975 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11976 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11977 +
11978 +out_fput:
11979 +       fput(f);
11980 +out_dput:
11981 +       dput(infopath.dentry);
11982 +out:
11983 +       if (!unlocked)
11984 +               inode_unlock_shared(h_dir);
11985 +       AuTraceErrPtr(drinfo);
11986 +       return drinfo;
11987 +}
11988 +
11989 +struct au_drinfo_do_load_args {
11990 +       struct au_drinfo **drinfop;
11991 +       struct path *h_ppath;
11992 +       char *whname;
11993 +       int whnamelen;
11994 +       struct dentry **info_dentry;
11995 +};
11996 +
11997 +static void au_call_drinfo_do_load(void *args)
11998 +{
11999 +       struct au_drinfo_do_load_args *a = args;
12000 +
12001 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12002 +                                       a->info_dentry);
12003 +}
12004 +
12005 +struct au_drinfo_load {
12006 +       struct path h_ppath;
12007 +       struct qstr *qname;
12008 +       unsigned char no_sio;
12009 +
12010 +       aufs_bindex_t ninfo;
12011 +       struct au_drinfo **drinfo;
12012 +};
12013 +
12014 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12015 +                         struct au_branch *br)
12016 +{
12017 +       int err, wkq_err, whnamelen, e;
12018 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12019 +               = AUFS_WH_DR_INFO_PFX;
12020 +       struct au_drinfo *drinfo;
12021 +       struct qstr oldname;
12022 +       struct inode *h_dir, *delegated;
12023 +       struct dentry *info_dentry;
12024 +       struct path infopath;
12025 +
12026 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12027 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12028 +                                   sizeof(whname) - whnamelen);
12029 +       if (w->no_sio)
12030 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12031 +                                          &info_dentry);
12032 +       else {
12033 +               struct au_drinfo_do_load_args args = {
12034 +                       .drinfop        = &drinfo,
12035 +                       .h_ppath        = &w->h_ppath,
12036 +                       .whname         = whname,
12037 +                       .whnamelen      = whnamelen,
12038 +                       .info_dentry    = &info_dentry
12039 +               };
12040 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12041 +               if (unlikely(wkq_err))
12042 +                       drinfo = ERR_PTR(wkq_err);
12043 +       }
12044 +       err = PTR_ERR(drinfo);
12045 +       if (IS_ERR_OR_NULL(drinfo))
12046 +               goto out;
12047 +
12048 +       err = 0;
12049 +       oldname.len = drinfo->oldnamelen;
12050 +       oldname.name = drinfo->oldname;
12051 +       if (au_qstreq(w->qname, &oldname)) {
12052 +               /* the name is renamed back */
12053 +               au_kfree_rcu(drinfo);
12054 +               drinfo = NULL;
12055 +
12056 +               infopath.dentry = info_dentry;
12057 +               infopath.mnt = w->h_ppath.mnt;
12058 +               h_dir = d_inode(w->h_ppath.dentry);
12059 +               delegated = NULL;
12060 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12061 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12062 +               inode_unlock(h_dir);
12063 +               if (unlikely(e))
12064 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12065 +               if (unlikely(e == -EWOULDBLOCK))
12066 +                       iput(delegated);
12067 +       }
12068 +       au_kfree_rcu(w->drinfo[bindex]);
12069 +       w->drinfo[bindex] = drinfo;
12070 +       dput(info_dentry);
12071 +
12072 +out:
12073 +       AuTraceErr(err);
12074 +       return err;
12075 +}
12076 +
12077 +/* ---------------------------------------------------------------------- */
12078 +
12079 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12080 +{
12081 +       struct au_drinfo **p = drinfo;
12082 +
12083 +       while (n-- > 0)
12084 +               au_kfree_rcu(*drinfo++);
12085 +       au_kfree_try_rcu(p);
12086 +}
12087 +
12088 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12089 +              aufs_bindex_t btgt)
12090 +{
12091 +       int err, ninfo;
12092 +       struct au_drinfo_load w;
12093 +       aufs_bindex_t bindex, bbot;
12094 +       struct au_branch *br;
12095 +       struct inode *h_dir;
12096 +       struct au_dr_hino *ent;
12097 +       struct super_block *sb;
12098 +
12099 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12100 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12101 +             AuLNPair(&lkup->whname), btgt);
12102 +
12103 +       sb = dentry->d_sb;
12104 +       bbot = au_sbbot(sb);
12105 +       w.ninfo = bbot + 1;
12106 +       if (!lkup->dirren.drinfo) {
12107 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12108 +                                             sizeof(*lkup->dirren.drinfo),
12109 +                                             GFP_NOFS);
12110 +               if (unlikely(!lkup->dirren.drinfo)) {
12111 +                       err = -ENOMEM;
12112 +                       goto out;
12113 +               }
12114 +               lkup->dirren.ninfo = w.ninfo;
12115 +       }
12116 +       w.drinfo = lkup->dirren.drinfo;
12117 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12118 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12119 +       AuDebugOn(!w.h_ppath.dentry);
12120 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12121 +       w.qname = &dentry->d_name;
12122 +
12123 +       ninfo = 0;
12124 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12125 +               br = au_sbr(sb, bindex);
12126 +               err = au_drinfo_load(&w, bindex, br);
12127 +               if (unlikely(err))
12128 +                       goto out_free;
12129 +               if (w.drinfo[bindex])
12130 +                       ninfo++;
12131 +       }
12132 +       if (!ninfo) {
12133 +               br = au_sbr(sb, btgt);
12134 +               h_dir = d_inode(w.h_ppath.dentry);
12135 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12136 +               AuDebugOn(!ent);
12137 +               au_dr_hino_del(&br->br_dirren, ent);
12138 +               au_kfree_rcu(ent);
12139 +       }
12140 +       goto out; /* success */
12141 +
12142 +out_free:
12143 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12144 +       lkup->dirren.ninfo = 0;
12145 +       lkup->dirren.drinfo = NULL;
12146 +out:
12147 +       AuTraceErr(err);
12148 +       return err;
12149 +}
12150 +
12151 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12152 +{
12153 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12154 +}
12155 +
12156 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12157 +{
12158 +       int err;
12159 +       struct au_drinfo *drinfo;
12160 +
12161 +       err = 0;
12162 +       if (!lkup->dirren.drinfo)
12163 +               goto out;
12164 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12165 +       drinfo = lkup->dirren.drinfo[btgt];
12166 +       if (!drinfo)
12167 +               goto out;
12168 +
12169 +       au_kfree_try_rcu(lkup->whname.name);
12170 +       lkup->whname.name = NULL;
12171 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12172 +       lkup->dirren.dr_name.name = drinfo->oldname;
12173 +       lkup->name = &lkup->dirren.dr_name;
12174 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12175 +       if (!err)
12176 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12177 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12178 +                     btgt);
12179 +
12180 +out:
12181 +       AuTraceErr(err);
12182 +       return err;
12183 +}
12184 +
12185 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12186 +                    ino_t h_ino)
12187 +{
12188 +       int match;
12189 +       struct au_drinfo *drinfo;
12190 +
12191 +       match = 1;
12192 +       if (!lkup->dirren.drinfo)
12193 +               goto out;
12194 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12195 +       drinfo = lkup->dirren.drinfo[bindex];
12196 +       if (!drinfo)
12197 +               goto out;
12198 +
12199 +       match = (drinfo->ino == h_ino);
12200 +       AuDbg("match %d\n", match);
12201 +
12202 +out:
12203 +       return match;
12204 +}
12205 +
12206 +/* ---------------------------------------------------------------------- */
12207 +
12208 +int au_dr_opt_set(struct super_block *sb)
12209 +{
12210 +       int err;
12211 +       aufs_bindex_t bindex, bbot;
12212 +       struct au_branch *br;
12213 +
12214 +       err = 0;
12215 +       bbot = au_sbbot(sb);
12216 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12217 +               br = au_sbr(sb, bindex);
12218 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12219 +       }
12220 +
12221 +       return err;
12222 +}
12223 +
12224 +int au_dr_opt_flush(struct super_block *sb)
12225 +{
12226 +       int err;
12227 +       aufs_bindex_t bindex, bbot;
12228 +       struct au_branch *br;
12229 +
12230 +       err = 0;
12231 +       bbot = au_sbbot(sb);
12232 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12233 +               br = au_sbr(sb, bindex);
12234 +               if (au_br_writable(br->br_perm))
12235 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12236 +       }
12237 +
12238 +       return err;
12239 +}
12240 +
12241 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12242 +{
12243 +       int err;
12244 +       aufs_bindex_t bindex, bbot;
12245 +       struct au_branch *br;
12246 +
12247 +       err = 0;
12248 +       if (!no_flush) {
12249 +               err = au_dr_opt_flush(sb);
12250 +               if (unlikely(err))
12251 +                       goto out;
12252 +       }
12253 +
12254 +       bbot = au_sbbot(sb);
12255 +       for (bindex = 0; bindex <= bbot; bindex++) {
12256 +               br = au_sbr(sb, bindex);
12257 +               au_dr_hino_free(&br->br_dirren);
12258 +       }
12259 +
12260 +out:
12261 +       return err;
12262 +}
12263 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12264 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12265 +++ linux/fs/aufs/dirren.h      2020-01-27 10:57:18.168871450 +0100
12266 @@ -0,0 +1,140 @@
12267 +/* SPDX-License-Identifier: GPL-2.0 */
12268 +/*
12269 + * Copyright (C) 2017-2020 Junjiro R. Okajima
12270 + *
12271 + * This program, aufs is free software; you can redistribute it and/or modify
12272 + * it under the terms of the GNU General Public License as published by
12273 + * the Free Software Foundation; either version 2 of the License, or
12274 + * (at your option) any later version.
12275 + *
12276 + * This program is distributed in the hope that it will be useful,
12277 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12278 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12279 + * GNU General Public License for more details.
12280 + *
12281 + * You should have received a copy of the GNU General Public License
12282 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12283 + */
12284 +
12285 +/*
12286 + * renamed dir info
12287 + */
12288 +
12289 +#ifndef __AUFS_DIRREN_H__
12290 +#define __AUFS_DIRREN_H__
12291 +
12292 +#ifdef __KERNEL__
12293 +
12294 +#include <linux/dcache.h>
12295 +#include <linux/statfs.h>
12296 +#include <linux/uuid.h>
12297 +#include "hbl.h"
12298 +
12299 +#define AuDirren_NHASH 100
12300 +
12301 +#ifdef CONFIG_AUFS_DIRREN
12302 +enum au_brid_type {
12303 +       AuBrid_Unset,
12304 +       AuBrid_UUID,
12305 +       AuBrid_FSID,
12306 +       AuBrid_DEV
12307 +};
12308 +
12309 +struct au_dr_brid {
12310 +       enum au_brid_type       type;
12311 +       union {
12312 +               uuid_t  uuid;   /* unimplemented yet */
12313 +               fsid_t  fsid;
12314 +               dev_t   dev;
12315 +       };
12316 +};
12317 +
12318 +/* 20 is the max digits length of ulong 64 */
12319 +/* brid-type "_" uuid "_" inum */
12320 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12321 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12322 +
12323 +struct au_dr_hino {
12324 +       struct hlist_bl_node    dr_hnode;
12325 +       ino_t                   dr_h_ino;
12326 +};
12327 +
12328 +struct au_dr_br {
12329 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12330 +       struct au_dr_brid       dr_brid;
12331 +};
12332 +
12333 +struct au_dr_lookup {
12334 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12335 +       struct qstr             dr_name; /* subset of dr_info */
12336 +       aufs_bindex_t           ninfo;
12337 +       struct au_drinfo        **drinfo;
12338 +};
12339 +#else
12340 +struct au_dr_hino;
12341 +/* empty */
12342 +struct au_dr_br { };
12343 +struct au_dr_lookup { };
12344 +#endif
12345 +
12346 +/* ---------------------------------------------------------------------- */
12347 +
12348 +struct au_branch;
12349 +struct au_do_lookup_args;
12350 +struct au_hinode;
12351 +#ifdef CONFIG_AUFS_DIRREN
12352 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12353 +                       struct au_dr_hino *add_ent);
12354 +void au_dr_hino_free(struct au_dr_br *dr);
12355 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12356 +                 const struct path *path);
12357 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12358 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12359 +                struct qstr *dst_name, void *_rev);
12360 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12361 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12362 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12363 +              aufs_bindex_t bindex);
12364 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12365 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12366 +                    ino_t h_ino);
12367 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12368 +int au_dr_opt_set(struct super_block *sb);
12369 +int au_dr_opt_flush(struct super_block *sb);
12370 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12371 +#else
12372 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12373 +          struct au_dr_hino *add_ent);
12374 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12375 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12376 +          const struct path *path);
12377 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12378 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12379 +          struct qstr *dst_name, void *_rev);
12380 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12381 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12382 +          void *rev);
12383 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12384 +          aufs_bindex_t bindex);
12385 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12386 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12387 +          aufs_bindex_t bindex, ino_t h_ino);
12388 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12389 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12390 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12391 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12392 +#endif
12393 +
12394 +/* ---------------------------------------------------------------------- */
12395 +
12396 +#ifdef CONFIG_AUFS_DIRREN
12397 +static inline int au_dr_ihash(ino_t h_ino)
12398 +{
12399 +       return h_ino % AuDirren_NHASH;
12400 +}
12401 +#else
12402 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12403 +#endif
12404 +
12405 +#endif /* __KERNEL__ */
12406 +#endif /* __AUFS_DIRREN_H__ */
12407 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12408 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12409 +++ linux/fs/aufs/dynop.c       2020-01-27 10:57:18.168871450 +0100
12410 @@ -0,0 +1,367 @@
12411 +// SPDX-License-Identifier: GPL-2.0
12412 +/*
12413 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12414 + *
12415 + * This program, aufs is free software; you can redistribute it and/or modify
12416 + * it under the terms of the GNU General Public License as published by
12417 + * the Free Software Foundation; either version 2 of the License, or
12418 + * (at your option) any later version.
12419 + *
12420 + * This program is distributed in the hope that it will be useful,
12421 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12422 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12423 + * GNU General Public License for more details.
12424 + *
12425 + * You should have received a copy of the GNU General Public License
12426 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12427 + */
12428 +
12429 +/*
12430 + * dynamically customizable operations for regular files
12431 + */
12432 +
12433 +#include "aufs.h"
12434 +
12435 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12436 +
12437 +/*
12438 + * How large will these lists be?
12439 + * Usually just a few elements, 20-30 at most for each, I guess.
12440 + */
12441 +static struct hlist_bl_head dynop[AuDyLast];
12442 +
12443 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12444 +                                    const void *h_op)
12445 +{
12446 +       struct au_dykey *key, *tmp;
12447 +       struct hlist_bl_node *pos;
12448 +
12449 +       key = NULL;
12450 +       hlist_bl_lock(hbl);
12451 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12452 +               if (tmp->dk_op.dy_hop == h_op) {
12453 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12454 +                               key = tmp;
12455 +                       break;
12456 +               }
12457 +       hlist_bl_unlock(hbl);
12458 +
12459 +       return key;
12460 +}
12461 +
12462 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12463 +{
12464 +       struct au_dykey **k, *found;
12465 +       const void *h_op = key->dk_op.dy_hop;
12466 +       int i;
12467 +
12468 +       found = NULL;
12469 +       k = br->br_dykey;
12470 +       for (i = 0; i < AuBrDynOp; i++)
12471 +               if (k[i]) {
12472 +                       if (k[i]->dk_op.dy_hop == h_op) {
12473 +                               found = k[i];
12474 +                               break;
12475 +                       }
12476 +               } else
12477 +                       break;
12478 +       if (!found) {
12479 +               spin_lock(&br->br_dykey_lock);
12480 +               for (; i < AuBrDynOp; i++)
12481 +                       if (k[i]) {
12482 +                               if (k[i]->dk_op.dy_hop == h_op) {
12483 +                                       found = k[i];
12484 +                                       break;
12485 +                               }
12486 +                       } else {
12487 +                               k[i] = key;
12488 +                               break;
12489 +                       }
12490 +               spin_unlock(&br->br_dykey_lock);
12491 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12492 +       }
12493 +
12494 +       return found;
12495 +}
12496 +
12497 +/* kref_get() if @key is already added */
12498 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12499 +{
12500 +       struct au_dykey *tmp, *found;
12501 +       struct hlist_bl_node *pos;
12502 +       const void *h_op = key->dk_op.dy_hop;
12503 +
12504 +       found = NULL;
12505 +       hlist_bl_lock(hbl);
12506 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12507 +               if (tmp->dk_op.dy_hop == h_op) {
12508 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12509 +                               found = tmp;
12510 +                       break;
12511 +               }
12512 +       if (!found)
12513 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12514 +       hlist_bl_unlock(hbl);
12515 +
12516 +       if (!found)
12517 +               DyPrSym(key);
12518 +       return found;
12519 +}
12520 +
12521 +static void dy_free_rcu(struct rcu_head *rcu)
12522 +{
12523 +       struct au_dykey *key;
12524 +
12525 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12526 +       DyPrSym(key);
12527 +       kfree(key);
12528 +}
12529 +
12530 +static void dy_free(struct kref *kref)
12531 +{
12532 +       struct au_dykey *key;
12533 +       struct hlist_bl_head *hbl;
12534 +
12535 +       key = container_of(kref, struct au_dykey, dk_kref);
12536 +       hbl = dynop + key->dk_op.dy_type;
12537 +       au_hbl_del(&key->dk_hnode, hbl);
12538 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12539 +}
12540 +
12541 +void au_dy_put(struct au_dykey *key)
12542 +{
12543 +       kref_put(&key->dk_kref, dy_free);
12544 +}
12545 +
12546 +/* ---------------------------------------------------------------------- */
12547 +
12548 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12549 +
12550 +#ifdef CONFIG_AUFS_DEBUG
12551 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12552 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12553 +#else
12554 +#define DyDbgDeclare(cnt)      do {} while (0)
12555 +#define DyDbgInc(cnt)          do {} while (0)
12556 +#endif
12557 +
12558 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12559 +       DyDbgInc(cnt);                                                  \
12560 +       if (h_op->func) {                                               \
12561 +               if (src.func)                                           \
12562 +                       dst.func = src.func;                            \
12563 +               else                                                    \
12564 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12565 +       }                                                               \
12566 +} while (0)
12567 +
12568 +#define DySetForce(func, dst, src) do {                \
12569 +       AuDebugOn(!src.func);                   \
12570 +       DyDbgInc(cnt);                          \
12571 +       dst.func = src.func;                    \
12572 +} while (0)
12573 +
12574 +#define DySetAop(func) \
12575 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12576 +#define DySetAopForce(func) \
12577 +       DySetForce(func, dyaop->da_op, aufs_aop)
12578 +
12579 +static void dy_aop(struct au_dykey *key, const void *h_op,
12580 +                  struct super_block *h_sb __maybe_unused)
12581 +{
12582 +       struct au_dyaop *dyaop = (void *)key;
12583 +       const struct address_space_operations *h_aop = h_op;
12584 +       DyDbgDeclare(cnt);
12585 +
12586 +       AuDbg("%s\n", au_sbtype(h_sb));
12587 +
12588 +       DySetAop(writepage);
12589 +       DySetAopForce(readpage);        /* force */
12590 +       DySetAop(writepages);
12591 +       DySetAop(set_page_dirty);
12592 +       DySetAop(readpages);
12593 +       DySetAop(write_begin);
12594 +       DySetAop(write_end);
12595 +       DySetAop(bmap);
12596 +       DySetAop(invalidatepage);
12597 +       DySetAop(releasepage);
12598 +       DySetAop(freepage);
12599 +       /* this one will be changed according to an aufs mount option */
12600 +       DySetAop(direct_IO);
12601 +       DySetAop(migratepage);
12602 +       DySetAop(isolate_page);
12603 +       DySetAop(putback_page);
12604 +       DySetAop(launder_page);
12605 +       DySetAop(is_partially_uptodate);
12606 +       DySetAop(is_dirty_writeback);
12607 +       DySetAop(error_remove_page);
12608 +       DySetAop(swap_activate);
12609 +       DySetAop(swap_deactivate);
12610 +
12611 +       DyDbgSize(cnt, *h_aop);
12612 +}
12613 +
12614 +/* ---------------------------------------------------------------------- */
12615 +
12616 +static void dy_bug(struct kref *kref)
12617 +{
12618 +       BUG();
12619 +}
12620 +
12621 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12622 +{
12623 +       struct au_dykey *key, *old;
12624 +       struct hlist_bl_head *hbl;
12625 +       struct op {
12626 +               unsigned int sz;
12627 +               void (*set)(struct au_dykey *key, const void *h_op,
12628 +                           struct super_block *h_sb __maybe_unused);
12629 +       };
12630 +       static const struct op a[] = {
12631 +               [AuDy_AOP] = {
12632 +                       .sz     = sizeof(struct au_dyaop),
12633 +                       .set    = dy_aop
12634 +               }
12635 +       };
12636 +       const struct op *p;
12637 +
12638 +       hbl = dynop + op->dy_type;
12639 +       key = dy_gfind_get(hbl, op->dy_hop);
12640 +       if (key)
12641 +               goto out_add; /* success */
12642 +
12643 +       p = a + op->dy_type;
12644 +       key = kzalloc(p->sz, GFP_NOFS);
12645 +       if (unlikely(!key)) {
12646 +               key = ERR_PTR(-ENOMEM);
12647 +               goto out;
12648 +       }
12649 +
12650 +       key->dk_op.dy_hop = op->dy_hop;
12651 +       kref_init(&key->dk_kref);
12652 +       p->set(key, op->dy_hop, au_br_sb(br));
12653 +       old = dy_gadd(hbl, key);
12654 +       if (old) {
12655 +               au_kfree_rcu(key);
12656 +               key = old;
12657 +       }
12658 +
12659 +out_add:
12660 +       old = dy_bradd(br, key);
12661 +       if (old)
12662 +               /* its ref-count should never be zero here */
12663 +               kref_put(&key->dk_kref, dy_bug);
12664 +out:
12665 +       return key;
12666 +}
12667 +
12668 +/* ---------------------------------------------------------------------- */
12669 +/*
12670 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12671 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12672 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12673 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12674 + * See the aufs manual in detail.
12675 + */
12676 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12677 +{
12678 +       if (!do_dx)
12679 +               dyaop->da_op.direct_IO = NULL;
12680 +       else
12681 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12682 +}
12683 +
12684 +static struct au_dyaop *dy_aget(struct au_branch *br,
12685 +                               const struct address_space_operations *h_aop,
12686 +                               int do_dx)
12687 +{
12688 +       struct au_dyaop *dyaop;
12689 +       struct au_dynop op;
12690 +
12691 +       op.dy_type = AuDy_AOP;
12692 +       op.dy_haop = h_aop;
12693 +       dyaop = (void *)dy_get(&op, br);
12694 +       if (IS_ERR(dyaop))
12695 +               goto out;
12696 +       dy_adx(dyaop, do_dx);
12697 +
12698 +out:
12699 +       return dyaop;
12700 +}
12701 +
12702 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12703 +               struct inode *h_inode)
12704 +{
12705 +       int err, do_dx;
12706 +       struct super_block *sb;
12707 +       struct au_branch *br;
12708 +       struct au_dyaop *dyaop;
12709 +
12710 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12711 +       IiMustWriteLock(inode);
12712 +
12713 +       sb = inode->i_sb;
12714 +       br = au_sbr(sb, bindex);
12715 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12716 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12717 +       err = PTR_ERR(dyaop);
12718 +       if (IS_ERR(dyaop))
12719 +               /* unnecessary to call dy_fput() */
12720 +               goto out;
12721 +
12722 +       err = 0;
12723 +       inode->i_mapping->a_ops = &dyaop->da_op;
12724 +
12725 +out:
12726 +       return err;
12727 +}
12728 +
12729 +/*
12730 + * Is it safe to replace a_ops during the inode/file is in operation?
12731 + * Yes, I hope so.
12732 + */
12733 +int au_dy_irefresh(struct inode *inode)
12734 +{
12735 +       int err;
12736 +       aufs_bindex_t btop;
12737 +       struct inode *h_inode;
12738 +
12739 +       err = 0;
12740 +       if (S_ISREG(inode->i_mode)) {
12741 +               btop = au_ibtop(inode);
12742 +               h_inode = au_h_iptr(inode, btop);
12743 +               err = au_dy_iaop(inode, btop, h_inode);
12744 +       }
12745 +       return err;
12746 +}
12747 +
12748 +void au_dy_arefresh(int do_dx)
12749 +{
12750 +       struct hlist_bl_head *hbl;
12751 +       struct hlist_bl_node *pos;
12752 +       struct au_dykey *key;
12753 +
12754 +       hbl = dynop + AuDy_AOP;
12755 +       hlist_bl_lock(hbl);
12756 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12757 +               dy_adx((void *)key, do_dx);
12758 +       hlist_bl_unlock(hbl);
12759 +}
12760 +
12761 +/* ---------------------------------------------------------------------- */
12762 +
12763 +void __init au_dy_init(void)
12764 +{
12765 +       int i;
12766 +
12767 +       for (i = 0; i < AuDyLast; i++)
12768 +               INIT_HLIST_BL_HEAD(dynop + i);
12769 +}
12770 +
12771 +void au_dy_fin(void)
12772 +{
12773 +       int i;
12774 +
12775 +       for (i = 0; i < AuDyLast; i++)
12776 +               WARN_ON(!hlist_bl_empty(dynop + i));
12777 +}
12778 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12779 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12780 +++ linux/fs/aufs/dynop.h       2020-01-27 10:57:18.168871450 +0100
12781 @@ -0,0 +1,77 @@
12782 +/* SPDX-License-Identifier: GPL-2.0 */
12783 +/*
12784 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12785 + *
12786 + * This program, aufs is free software; you can redistribute it and/or modify
12787 + * it under the terms of the GNU General Public License as published by
12788 + * the Free Software Foundation; either version 2 of the License, or
12789 + * (at your option) any later version.
12790 + *
12791 + * This program is distributed in the hope that it will be useful,
12792 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12793 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12794 + * GNU General Public License for more details.
12795 + *
12796 + * You should have received a copy of the GNU General Public License
12797 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12798 + */
12799 +
12800 +/*
12801 + * dynamically customizable operations (for regular files only)
12802 + */
12803 +
12804 +#ifndef __AUFS_DYNOP_H__
12805 +#define __AUFS_DYNOP_H__
12806 +
12807 +#ifdef __KERNEL__
12808 +
12809 +#include <linux/fs.h>
12810 +#include <linux/kref.h>
12811 +
12812 +enum {AuDy_AOP, AuDyLast};
12813 +
12814 +struct au_dynop {
12815 +       int                                             dy_type;
12816 +       union {
12817 +               const void                              *dy_hop;
12818 +               const struct address_space_operations   *dy_haop;
12819 +       };
12820 +};
12821 +
12822 +struct au_dykey {
12823 +       union {
12824 +               struct hlist_bl_node    dk_hnode;
12825 +               struct rcu_head         dk_rcu;
12826 +       };
12827 +       struct au_dynop         dk_op;
12828 +
12829 +       /*
12830 +        * during I am in the branch local array, kref is gotten. when the
12831 +        * branch is removed, kref is put.
12832 +        */
12833 +       struct kref             dk_kref;
12834 +};
12835 +
12836 +/* stop unioning since their sizes are very different from each other */
12837 +struct au_dyaop {
12838 +       struct au_dykey                 da_key;
12839 +       struct address_space_operations da_op; /* not const */
12840 +};
12841 +/* make sure that 'struct au_dykey *' can be any type */
12842 +static_assert(!offsetof(struct au_dyaop, da_key));
12843 +
12844 +/* ---------------------------------------------------------------------- */
12845 +
12846 +/* dynop.c */
12847 +struct au_branch;
12848 +void au_dy_put(struct au_dykey *key);
12849 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12850 +               struct inode *h_inode);
12851 +int au_dy_irefresh(struct inode *inode);
12852 +void au_dy_arefresh(int do_dio);
12853 +
12854 +void __init au_dy_init(void);
12855 +void au_dy_fin(void);
12856 +
12857 +#endif /* __KERNEL__ */
12858 +#endif /* __AUFS_DYNOP_H__ */
12859 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12860 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12861 +++ linux/fs/aufs/export.c      2020-01-27 10:57:18.168871450 +0100
12862 @@ -0,0 +1,838 @@
12863 +// SPDX-License-Identifier: GPL-2.0
12864 +/*
12865 + * Copyright (C) 2005-2020 Junjiro R. Okajima
12866 + *
12867 + * This program, aufs is free software; you can redistribute it and/or modify
12868 + * it under the terms of the GNU General Public License as published by
12869 + * the Free Software Foundation; either version 2 of the License, or
12870 + * (at your option) any later version.
12871 + *
12872 + * This program is distributed in the hope that it will be useful,
12873 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12874 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12875 + * GNU General Public License for more details.
12876 + *
12877 + * You should have received a copy of the GNU General Public License
12878 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12879 + */
12880 +
12881 +/*
12882 + * export via nfs
12883 + */
12884 +
12885 +#include <linux/exportfs.h>
12886 +#include <linux/fs_struct.h>
12887 +#include <linux/namei.h>
12888 +#include <linux/nsproxy.h>
12889 +#include <linux/random.h>
12890 +#include <linux/writeback.h>
12891 +#include "aufs.h"
12892 +
12893 +union conv {
12894 +#ifdef CONFIG_AUFS_INO_T_64
12895 +       __u32 a[2];
12896 +#else
12897 +       __u32 a[1];
12898 +#endif
12899 +       ino_t ino;
12900 +};
12901 +
12902 +static ino_t decode_ino(__u32 *a)
12903 +{
12904 +       union conv u;
12905 +
12906 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12907 +       u.a[0] = a[0];
12908 +#ifdef CONFIG_AUFS_INO_T_64
12909 +       u.a[1] = a[1];
12910 +#endif
12911 +       return u.ino;
12912 +}
12913 +
12914 +static void encode_ino(__u32 *a, ino_t ino)
12915 +{
12916 +       union conv u;
12917 +
12918 +       u.ino = ino;
12919 +       a[0] = u.a[0];
12920 +#ifdef CONFIG_AUFS_INO_T_64
12921 +       a[1] = u.a[1];
12922 +#endif
12923 +}
12924 +
12925 +/* NFS file handle */
12926 +enum {
12927 +       Fh_br_id,
12928 +       Fh_sigen,
12929 +#ifdef CONFIG_AUFS_INO_T_64
12930 +       /* support 64bit inode number */
12931 +       Fh_ino1,
12932 +       Fh_ino2,
12933 +       Fh_dir_ino1,
12934 +       Fh_dir_ino2,
12935 +#else
12936 +       Fh_ino1,
12937 +       Fh_dir_ino1,
12938 +#endif
12939 +       Fh_igen,
12940 +       Fh_h_type,
12941 +       Fh_tail,
12942 +
12943 +       Fh_ino = Fh_ino1,
12944 +       Fh_dir_ino = Fh_dir_ino1
12945 +};
12946 +
12947 +static int au_test_anon(struct dentry *dentry)
12948 +{
12949 +       /* note: read d_flags without d_lock */
12950 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12951 +}
12952 +
12953 +int au_test_nfsd(void)
12954 +{
12955 +       int ret;
12956 +       struct task_struct *tsk = current;
12957 +       char comm[sizeof(tsk->comm)];
12958 +
12959 +       ret = 0;
12960 +       if (tsk->flags & PF_KTHREAD) {
12961 +               get_task_comm(comm, tsk);
12962 +               ret = !strcmp(comm, "nfsd");
12963 +       }
12964 +
12965 +       return ret;
12966 +}
12967 +
12968 +/* ---------------------------------------------------------------------- */
12969 +/* inode generation external table */
12970 +
12971 +void au_xigen_inc(struct inode *inode)
12972 +{
12973 +       loff_t pos;
12974 +       ssize_t sz;
12975 +       __u32 igen;
12976 +       struct super_block *sb;
12977 +       struct au_sbinfo *sbinfo;
12978 +
12979 +       sb = inode->i_sb;
12980 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12981 +
12982 +       sbinfo = au_sbi(sb);
12983 +       pos = inode->i_ino;
12984 +       pos *= sizeof(igen);
12985 +       igen = inode->i_generation + 1;
12986 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12987 +                        sizeof(igen), &pos);
12988 +       if (sz == sizeof(igen))
12989 +               return; /* success */
12990 +
12991 +       if (unlikely(sz >= 0))
12992 +               AuIOErr("xigen error (%zd)\n", sz);
12993 +}
12994 +
12995 +int au_xigen_new(struct inode *inode)
12996 +{
12997 +       int err;
12998 +       loff_t pos;
12999 +       ssize_t sz;
13000 +       struct super_block *sb;
13001 +       struct au_sbinfo *sbinfo;
13002 +       struct file *file;
13003 +
13004 +       err = 0;
13005 +       /* todo: dirty, at mount time */
13006 +       if (inode->i_ino == AUFS_ROOT_INO)
13007 +               goto out;
13008 +       sb = inode->i_sb;
13009 +       SiMustAnyLock(sb);
13010 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13011 +               goto out;
13012 +
13013 +       err = -EFBIG;
13014 +       pos = inode->i_ino;
13015 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13016 +               AuIOErr1("too large i%lld\n", pos);
13017 +               goto out;
13018 +       }
13019 +       pos *= sizeof(inode->i_generation);
13020 +
13021 +       err = 0;
13022 +       sbinfo = au_sbi(sb);
13023 +       file = sbinfo->si_xigen;
13024 +       BUG_ON(!file);
13025 +
13026 +       if (vfsub_f_size_read(file)
13027 +           < pos + sizeof(inode->i_generation)) {
13028 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13029 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13030 +                                sizeof(inode->i_generation), &pos);
13031 +       } else
13032 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13033 +                               sizeof(inode->i_generation), &pos);
13034 +       if (sz == sizeof(inode->i_generation))
13035 +               goto out; /* success */
13036 +
13037 +       err = sz;
13038 +       if (unlikely(sz >= 0)) {
13039 +               err = -EIO;
13040 +               AuIOErr("xigen error (%zd)\n", sz);
13041 +       }
13042 +
13043 +out:
13044 +       return err;
13045 +}
13046 +
13047 +int au_xigen_set(struct super_block *sb, struct path *path)
13048 +{
13049 +       int err;
13050 +       struct au_sbinfo *sbinfo;
13051 +       struct file *file;
13052 +
13053 +       SiMustWriteLock(sb);
13054 +
13055 +       sbinfo = au_sbi(sb);
13056 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13057 +       err = PTR_ERR(file);
13058 +       if (IS_ERR(file))
13059 +               goto out;
13060 +       err = 0;
13061 +       if (sbinfo->si_xigen)
13062 +               fput(sbinfo->si_xigen);
13063 +       sbinfo->si_xigen = file;
13064 +
13065 +out:
13066 +       AuTraceErr(err);
13067 +       return err;
13068 +}
13069 +
13070 +void au_xigen_clr(struct super_block *sb)
13071 +{
13072 +       struct au_sbinfo *sbinfo;
13073 +
13074 +       SiMustWriteLock(sb);
13075 +
13076 +       sbinfo = au_sbi(sb);
13077 +       if (sbinfo->si_xigen) {
13078 +               fput(sbinfo->si_xigen);
13079 +               sbinfo->si_xigen = NULL;
13080 +       }
13081 +}
13082 +
13083 +/* ---------------------------------------------------------------------- */
13084 +
13085 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13086 +                                   ino_t dir_ino)
13087 +{
13088 +       struct dentry *dentry, *d;
13089 +       struct inode *inode;
13090 +       unsigned int sigen;
13091 +
13092 +       dentry = NULL;
13093 +       inode = ilookup(sb, ino);
13094 +       if (!inode)
13095 +               goto out;
13096 +
13097 +       dentry = ERR_PTR(-ESTALE);
13098 +       sigen = au_sigen(sb);
13099 +       if (unlikely(au_is_bad_inode(inode)
13100 +                    || IS_DEADDIR(inode)
13101 +                    || sigen != au_iigen(inode, NULL)))
13102 +               goto out_iput;
13103 +
13104 +       dentry = NULL;
13105 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13106 +               dentry = d_find_alias(inode);
13107 +       else {
13108 +               spin_lock(&inode->i_lock);
13109 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13110 +                       spin_lock(&d->d_lock);
13111 +                       if (!au_test_anon(d)
13112 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13113 +                               dentry = dget_dlock(d);
13114 +                               spin_unlock(&d->d_lock);
13115 +                               break;
13116 +                       }
13117 +                       spin_unlock(&d->d_lock);
13118 +               }
13119 +               spin_unlock(&inode->i_lock);
13120 +       }
13121 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13122 +               /* need to refresh */
13123 +               dput(dentry);
13124 +               dentry = NULL;
13125 +       }
13126 +
13127 +out_iput:
13128 +       iput(inode);
13129 +out:
13130 +       AuTraceErrPtr(dentry);
13131 +       return dentry;
13132 +}
13133 +
13134 +/* ---------------------------------------------------------------------- */
13135 +
13136 +/* todo: dirty? */
13137 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13138 +
13139 +struct au_compare_mnt_args {
13140 +       /* input */
13141 +       struct super_block *sb;
13142 +
13143 +       /* output */
13144 +       struct vfsmount *mnt;
13145 +};
13146 +
13147 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13148 +{
13149 +       struct au_compare_mnt_args *a = arg;
13150 +
13151 +       if (mnt->mnt_sb != a->sb)
13152 +               return 0;
13153 +       a->mnt = mntget(mnt);
13154 +       return 1;
13155 +}
13156 +
13157 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13158 +{
13159 +       int err;
13160 +       struct path root;
13161 +       struct au_compare_mnt_args args = {
13162 +               .sb = sb
13163 +       };
13164 +
13165 +       get_fs_root(current->fs, &root);
13166 +       rcu_read_lock();
13167 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13168 +       rcu_read_unlock();
13169 +       path_put(&root);
13170 +       AuDebugOn(!err);
13171 +       AuDebugOn(!args.mnt);
13172 +       return args.mnt;
13173 +}
13174 +
13175 +struct au_nfsd_si_lock {
13176 +       unsigned int sigen;
13177 +       aufs_bindex_t bindex, br_id;
13178 +       unsigned char force_lock;
13179 +};
13180 +
13181 +static int si_nfsd_read_lock(struct super_block *sb,
13182 +                            struct au_nfsd_si_lock *nsi_lock)
13183 +{
13184 +       int err;
13185 +       aufs_bindex_t bindex;
13186 +
13187 +       si_read_lock(sb, AuLock_FLUSH);
13188 +
13189 +       /* branch id may be wrapped around */
13190 +       err = 0;
13191 +       bindex = au_br_index(sb, nsi_lock->br_id);
13192 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13193 +               goto out; /* success */
13194 +
13195 +       err = -ESTALE;
13196 +       bindex = -1;
13197 +       if (!nsi_lock->force_lock)
13198 +               si_read_unlock(sb);
13199 +
13200 +out:
13201 +       nsi_lock->bindex = bindex;
13202 +       return err;
13203 +}
13204 +
13205 +struct find_name_by_ino {
13206 +       struct dir_context ctx;
13207 +       int called, found;
13208 +       ino_t ino;
13209 +       char *name;
13210 +       int namelen;
13211 +};
13212 +
13213 +static int
13214 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13215 +                loff_t offset, u64 ino, unsigned int d_type)
13216 +{
13217 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13218 +                                                 ctx);
13219 +
13220 +       a->called++;
13221 +       if (a->ino != ino)
13222 +               return 0;
13223 +
13224 +       memcpy(a->name, name, namelen);
13225 +       a->namelen = namelen;
13226 +       a->found = 1;
13227 +       return 1;
13228 +}
13229 +
13230 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13231 +                                    struct au_nfsd_si_lock *nsi_lock)
13232 +{
13233 +       struct dentry *dentry, *parent;
13234 +       struct file *file;
13235 +       struct inode *dir;
13236 +       struct find_name_by_ino arg = {
13237 +               .ctx = {
13238 +                       .actor = find_name_by_ino
13239 +               }
13240 +       };
13241 +       int err;
13242 +
13243 +       parent = path->dentry;
13244 +       if (nsi_lock)
13245 +               si_read_unlock(parent->d_sb);
13246 +       file = vfsub_dentry_open(path, au_dir_roflags);
13247 +       dentry = (void *)file;
13248 +       if (IS_ERR(file))
13249 +               goto out;
13250 +
13251 +       dentry = ERR_PTR(-ENOMEM);
13252 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13253 +       if (unlikely(!arg.name))
13254 +               goto out_file;
13255 +       arg.ino = ino;
13256 +       arg.found = 0;
13257 +       do {
13258 +               arg.called = 0;
13259 +               /* smp_mb(); */
13260 +               err = vfsub_iterate_dir(file, &arg.ctx);
13261 +       } while (!err && !arg.found && arg.called);
13262 +       dentry = ERR_PTR(err);
13263 +       if (unlikely(err))
13264 +               goto out_name;
13265 +       /* instead of ENOENT */
13266 +       dentry = ERR_PTR(-ESTALE);
13267 +       if (!arg.found)
13268 +               goto out_name;
13269 +
13270 +       /* do not call vfsub_lkup_one() */
13271 +       dir = d_inode(parent);
13272 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13273 +       AuTraceErrPtr(dentry);
13274 +       if (IS_ERR(dentry))
13275 +               goto out_name;
13276 +       AuDebugOn(au_test_anon(dentry));
13277 +       if (unlikely(d_really_is_negative(dentry))) {
13278 +               dput(dentry);
13279 +               dentry = ERR_PTR(-ENOENT);
13280 +       }
13281 +
13282 +out_name:
13283 +       free_page((unsigned long)arg.name);
13284 +out_file:
13285 +       fput(file);
13286 +out:
13287 +       if (unlikely(nsi_lock
13288 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13289 +               if (!IS_ERR(dentry)) {
13290 +                       dput(dentry);
13291 +                       dentry = ERR_PTR(-ESTALE);
13292 +               }
13293 +       AuTraceErrPtr(dentry);
13294 +       return dentry;
13295 +}
13296 +
13297 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13298 +                                       ino_t dir_ino,
13299 +                                       struct au_nfsd_si_lock *nsi_lock)
13300 +{
13301 +       struct dentry *dentry;
13302 +       struct path path;
13303 +
13304 +       if (dir_ino != AUFS_ROOT_INO) {
13305 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13306 +               dentry = path.dentry;
13307 +               if (!path.dentry || IS_ERR(path.dentry))
13308 +                       goto out;
13309 +               AuDebugOn(au_test_anon(path.dentry));
13310 +       } else
13311 +               path.dentry = dget(sb->s_root);
13312 +
13313 +       path.mnt = au_mnt_get(sb);
13314 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13315 +       path_put(&path);
13316 +
13317 +out:
13318 +       AuTraceErrPtr(dentry);
13319 +       return dentry;
13320 +}
13321 +
13322 +/* ---------------------------------------------------------------------- */
13323 +
13324 +static int h_acceptable(void *expv, struct dentry *dentry)
13325 +{
13326 +       return 1;
13327 +}
13328 +
13329 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13330 +                          char *buf, int len, struct super_block *sb)
13331 +{
13332 +       char *p;
13333 +       int n;
13334 +       struct path path;
13335 +
13336 +       p = d_path(h_rootpath, buf, len);
13337 +       if (IS_ERR(p))
13338 +               goto out;
13339 +       n = strlen(p);
13340 +
13341 +       path.mnt = h_rootpath->mnt;
13342 +       path.dentry = h_parent;
13343 +       p = d_path(&path, buf, len);
13344 +       if (IS_ERR(p))
13345 +               goto out;
13346 +       if (n != 1)
13347 +               p += n;
13348 +
13349 +       path.mnt = au_mnt_get(sb);
13350 +       path.dentry = sb->s_root;
13351 +       p = d_path(&path, buf, len - strlen(p));
13352 +       mntput(path.mnt);
13353 +       if (IS_ERR(p))
13354 +               goto out;
13355 +       if (n != 1)
13356 +               p[strlen(p)] = '/';
13357 +
13358 +out:
13359 +       AuTraceErrPtr(p);
13360 +       return p;
13361 +}
13362 +
13363 +static
13364 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13365 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13366 +{
13367 +       struct dentry *dentry, *h_parent, *root;
13368 +       struct super_block *h_sb;
13369 +       char *pathname, *p;
13370 +       struct vfsmount *h_mnt;
13371 +       struct au_branch *br;
13372 +       int err;
13373 +       struct path path;
13374 +
13375 +       br = au_sbr(sb, nsi_lock->bindex);
13376 +       h_mnt = au_br_mnt(br);
13377 +       h_sb = h_mnt->mnt_sb;
13378 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13379 +       lockdep_off();
13380 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13381 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13382 +                                     h_acceptable, /*context*/NULL);
13383 +       lockdep_on();
13384 +       dentry = h_parent;
13385 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13386 +               AuWarn1("%s decode_fh failed, %ld\n",
13387 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13388 +               goto out;
13389 +       }
13390 +       dentry = NULL;
13391 +       if (unlikely(au_test_anon(h_parent))) {
13392 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13393 +                       au_sbtype(h_sb));
13394 +               goto out_h_parent;
13395 +       }
13396 +
13397 +       dentry = ERR_PTR(-ENOMEM);
13398 +       pathname = (void *)__get_free_page(GFP_NOFS);
13399 +       if (unlikely(!pathname))
13400 +               goto out_h_parent;
13401 +
13402 +       root = sb->s_root;
13403 +       path.mnt = h_mnt;
13404 +       di_read_lock_parent(root, !AuLock_IR);
13405 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13406 +       di_read_unlock(root, !AuLock_IR);
13407 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13408 +       dentry = (void *)p;
13409 +       if (IS_ERR(p))
13410 +               goto out_pathname;
13411 +
13412 +       si_read_unlock(sb);
13413 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13414 +       dentry = ERR_PTR(err);
13415 +       if (unlikely(err))
13416 +               goto out_relock;
13417 +
13418 +       dentry = ERR_PTR(-ENOENT);
13419 +       AuDebugOn(au_test_anon(path.dentry));
13420 +       if (unlikely(d_really_is_negative(path.dentry)))
13421 +               goto out_path;
13422 +
13423 +       if (ino != d_inode(path.dentry)->i_ino)
13424 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13425 +       else
13426 +               dentry = dget(path.dentry);
13427 +
13428 +out_path:
13429 +       path_put(&path);
13430 +out_relock:
13431 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13432 +               if (!IS_ERR(dentry)) {
13433 +                       dput(dentry);
13434 +                       dentry = ERR_PTR(-ESTALE);
13435 +               }
13436 +out_pathname:
13437 +       free_page((unsigned long)pathname);
13438 +out_h_parent:
13439 +       dput(h_parent);
13440 +out:
13441 +       AuTraceErrPtr(dentry);
13442 +       return dentry;
13443 +}
13444 +
13445 +/* ---------------------------------------------------------------------- */
13446 +
13447 +static struct dentry *
13448 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13449 +                 int fh_type)
13450 +{
13451 +       struct dentry *dentry;
13452 +       __u32 *fh = fid->raw;
13453 +       struct au_branch *br;
13454 +       ino_t ino, dir_ino;
13455 +       struct au_nfsd_si_lock nsi_lock = {
13456 +               .force_lock     = 0
13457 +       };
13458 +
13459 +       dentry = ERR_PTR(-ESTALE);
13460 +       /* it should never happen, but the file handle is unreliable */
13461 +       if (unlikely(fh_len < Fh_tail))
13462 +               goto out;
13463 +       nsi_lock.sigen = fh[Fh_sigen];
13464 +       nsi_lock.br_id = fh[Fh_br_id];
13465 +
13466 +       /* branch id may be wrapped around */
13467 +       br = NULL;
13468 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13469 +               goto out;
13470 +       nsi_lock.force_lock = 1;
13471 +
13472 +       /* is this inode still cached? */
13473 +       ino = decode_ino(fh + Fh_ino);
13474 +       /* it should never happen */
13475 +       if (unlikely(ino == AUFS_ROOT_INO))
13476 +               goto out_unlock;
13477 +
13478 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13479 +       dentry = decode_by_ino(sb, ino, dir_ino);
13480 +       if (IS_ERR(dentry))
13481 +               goto out_unlock;
13482 +       if (dentry)
13483 +               goto accept;
13484 +
13485 +       /* is the parent dir cached? */
13486 +       br = au_sbr(sb, nsi_lock.bindex);
13487 +       au_lcnt_inc(&br->br_nfiles);
13488 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13489 +       if (IS_ERR(dentry))
13490 +               goto out_unlock;
13491 +       if (dentry)
13492 +               goto accept;
13493 +
13494 +       /* lookup path */
13495 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13496 +       if (IS_ERR(dentry))
13497 +               goto out_unlock;
13498 +       if (unlikely(!dentry))
13499 +               /* todo?: make it ESTALE */
13500 +               goto out_unlock;
13501 +
13502 +accept:
13503 +       if (!au_digen_test(dentry, au_sigen(sb))
13504 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13505 +               goto out_unlock; /* success */
13506 +
13507 +       dput(dentry);
13508 +       dentry = ERR_PTR(-ESTALE);
13509 +out_unlock:
13510 +       if (br)
13511 +               au_lcnt_dec(&br->br_nfiles);
13512 +       si_read_unlock(sb);
13513 +out:
13514 +       AuTraceErrPtr(dentry);
13515 +       return dentry;
13516 +}
13517 +
13518 +#if 0 /* reserved for future use */
13519 +/* support subtreecheck option */
13520 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13521 +                                       int fh_len, int fh_type)
13522 +{
13523 +       struct dentry *parent;
13524 +       __u32 *fh = fid->raw;
13525 +       ino_t dir_ino;
13526 +
13527 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13528 +       parent = decode_by_ino(sb, dir_ino, 0);
13529 +       if (IS_ERR(parent))
13530 +               goto out;
13531 +       if (!parent)
13532 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13533 +                                       dir_ino, fh, fh_len);
13534 +
13535 +out:
13536 +       AuTraceErrPtr(parent);
13537 +       return parent;
13538 +}
13539 +#endif
13540 +
13541 +/* ---------------------------------------------------------------------- */
13542 +
13543 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13544 +                         struct inode *dir)
13545 +{
13546 +       int err;
13547 +       aufs_bindex_t bindex;
13548 +       struct super_block *sb, *h_sb;
13549 +       struct dentry *dentry, *parent, *h_parent;
13550 +       struct inode *h_dir;
13551 +       struct au_branch *br;
13552 +
13553 +       err = -ENOSPC;
13554 +       if (unlikely(*max_len <= Fh_tail)) {
13555 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13556 +               goto out;
13557 +       }
13558 +
13559 +       err = FILEID_ROOT;
13560 +       if (inode->i_ino == AUFS_ROOT_INO) {
13561 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13562 +               goto out;
13563 +       }
13564 +
13565 +       h_parent = NULL;
13566 +       sb = inode->i_sb;
13567 +       err = si_read_lock(sb, AuLock_FLUSH);
13568 +       if (unlikely(err))
13569 +               goto out;
13570 +
13571 +#ifdef CONFIG_AUFS_DEBUG
13572 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13573 +               AuWarn1("NFS-exporting requires xino\n");
13574 +#endif
13575 +       err = -EIO;
13576 +       parent = NULL;
13577 +       ii_read_lock_child(inode);
13578 +       bindex = au_ibtop(inode);
13579 +       if (!dir) {
13580 +               dentry = d_find_any_alias(inode);
13581 +               if (unlikely(!dentry))
13582 +                       goto out_unlock;
13583 +               AuDebugOn(au_test_anon(dentry));
13584 +               parent = dget_parent(dentry);
13585 +               dput(dentry);
13586 +               if (unlikely(!parent))
13587 +                       goto out_unlock;
13588 +               if (d_really_is_positive(parent))
13589 +                       dir = d_inode(parent);
13590 +       }
13591 +
13592 +       ii_read_lock_parent(dir);
13593 +       h_dir = au_h_iptr(dir, bindex);
13594 +       ii_read_unlock(dir);
13595 +       if (unlikely(!h_dir))
13596 +               goto out_parent;
13597 +       h_parent = d_find_any_alias(h_dir);
13598 +       if (unlikely(!h_parent))
13599 +               goto out_hparent;
13600 +
13601 +       err = -EPERM;
13602 +       br = au_sbr(sb, bindex);
13603 +       h_sb = au_br_sb(br);
13604 +       if (unlikely(!h_sb->s_export_op)) {
13605 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13606 +               goto out_hparent;
13607 +       }
13608 +
13609 +       fh[Fh_br_id] = br->br_id;
13610 +       fh[Fh_sigen] = au_sigen(sb);
13611 +       encode_ino(fh + Fh_ino, inode->i_ino);
13612 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13613 +       fh[Fh_igen] = inode->i_generation;
13614 +
13615 +       *max_len -= Fh_tail;
13616 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13617 +                                          max_len,
13618 +                                          /*connectable or subtreecheck*/0);
13619 +       err = fh[Fh_h_type];
13620 +       *max_len += Fh_tail;
13621 +       /* todo: macros? */
13622 +       if (err != FILEID_INVALID)
13623 +               err = 99;
13624 +       else
13625 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13626 +
13627 +out_hparent:
13628 +       dput(h_parent);
13629 +out_parent:
13630 +       dput(parent);
13631 +out_unlock:
13632 +       ii_read_unlock(inode);
13633 +       si_read_unlock(sb);
13634 +out:
13635 +       if (unlikely(err < 0))
13636 +               err = FILEID_INVALID;
13637 +       return err;
13638 +}
13639 +
13640 +/* ---------------------------------------------------------------------- */
13641 +
13642 +static int aufs_commit_metadata(struct inode *inode)
13643 +{
13644 +       int err;
13645 +       aufs_bindex_t bindex;
13646 +       struct super_block *sb;
13647 +       struct inode *h_inode;
13648 +       int (*f)(struct inode *inode);
13649 +
13650 +       sb = inode->i_sb;
13651 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13652 +       ii_write_lock_child(inode);
13653 +       bindex = au_ibtop(inode);
13654 +       AuDebugOn(bindex < 0);
13655 +       h_inode = au_h_iptr(inode, bindex);
13656 +
13657 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13658 +       if (f)
13659 +               err = f(h_inode);
13660 +       else {
13661 +               struct writeback_control wbc = {
13662 +                       .sync_mode      = WB_SYNC_ALL,
13663 +                       .nr_to_write    = 0 /* metadata only */
13664 +               };
13665 +
13666 +               err = sync_inode(h_inode, &wbc);
13667 +       }
13668 +
13669 +       au_cpup_attr_timesizes(inode);
13670 +       ii_write_unlock(inode);
13671 +       si_read_unlock(sb);
13672 +       return err;
13673 +}
13674 +
13675 +/* ---------------------------------------------------------------------- */
13676 +
13677 +static struct export_operations aufs_export_op = {
13678 +       .fh_to_dentry           = aufs_fh_to_dentry,
13679 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13680 +       .encode_fh              = aufs_encode_fh,
13681 +       .commit_metadata        = aufs_commit_metadata
13682 +};
13683 +
13684 +void au_export_init(struct super_block *sb)
13685 +{
13686 +       struct au_sbinfo *sbinfo;
13687 +       __u32 u;
13688 +
13689 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13690 +                        && IS_MODULE(CONFIG_EXPORTFS),
13691 +                        AUFS_NAME ": unsupported configuration "
13692 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13693 +
13694 +       sb->s_export_op = &aufs_export_op;
13695 +       sbinfo = au_sbi(sb);
13696 +       sbinfo->si_xigen = NULL;
13697 +       get_random_bytes(&u, sizeof(u));
13698 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13699 +       atomic_set(&sbinfo->si_xigen_next, u);
13700 +}
13701 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13702 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13703 +++ linux/fs/aufs/fhsm.c        2020-01-27 10:57:18.172204883 +0100
13704 @@ -0,0 +1,427 @@
13705 +// SPDX-License-Identifier: GPL-2.0
13706 +/*
13707 + * Copyright (C) 2011-2020 Junjiro R. Okajima
13708 + *
13709 + * This program, aufs is free software; you can redistribute it and/or modify
13710 + * it under the terms of the GNU General Public License as published by
13711 + * the Free Software Foundation; either version 2 of the License, or
13712 + * (at your option) any later version.
13713 + *
13714 + * This program is distributed in the hope that it will be useful,
13715 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13716 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13717 + * GNU General Public License for more details.
13718 + *
13719 + * You should have received a copy of the GNU General Public License
13720 + * along with this program; if not, write to the Free Software
13721 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13722 + */
13723 +
13724 +/*
13725 + * File-based Hierarchy Storage Management
13726 + */
13727 +
13728 +#include <linux/anon_inodes.h>
13729 +#include <linux/poll.h>
13730 +#include <linux/seq_file.h>
13731 +#include <linux/statfs.h>
13732 +#include "aufs.h"
13733 +
13734 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13735 +{
13736 +       struct au_sbinfo *sbinfo;
13737 +       struct au_fhsm *fhsm;
13738 +
13739 +       SiMustAnyLock(sb);
13740 +
13741 +       sbinfo = au_sbi(sb);
13742 +       fhsm = &sbinfo->si_fhsm;
13743 +       AuDebugOn(!fhsm);
13744 +       return fhsm->fhsm_bottom;
13745 +}
13746 +
13747 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13748 +{
13749 +       struct au_sbinfo *sbinfo;
13750 +       struct au_fhsm *fhsm;
13751 +
13752 +       SiMustWriteLock(sb);
13753 +
13754 +       sbinfo = au_sbi(sb);
13755 +       fhsm = &sbinfo->si_fhsm;
13756 +       AuDebugOn(!fhsm);
13757 +       fhsm->fhsm_bottom = bindex;
13758 +}
13759 +
13760 +/* ---------------------------------------------------------------------- */
13761 +
13762 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13763 +{
13764 +       struct au_br_fhsm *bf;
13765 +
13766 +       bf = br->br_fhsm;
13767 +       MtxMustLock(&bf->bf_lock);
13768 +
13769 +       return !bf->bf_readable
13770 +               || time_after(jiffies,
13771 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13772 +}
13773 +
13774 +/* ---------------------------------------------------------------------- */
13775 +
13776 +static void au_fhsm_notify(struct super_block *sb, int val)
13777 +{
13778 +       struct au_sbinfo *sbinfo;
13779 +       struct au_fhsm *fhsm;
13780 +
13781 +       SiMustAnyLock(sb);
13782 +
13783 +       sbinfo = au_sbi(sb);
13784 +       fhsm = &sbinfo->si_fhsm;
13785 +       if (au_fhsm_pid(fhsm)
13786 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13787 +               atomic_set(&fhsm->fhsm_readable, val);
13788 +               if (val)
13789 +                       wake_up(&fhsm->fhsm_wqh);
13790 +       }
13791 +}
13792 +
13793 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13794 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13795 +{
13796 +       int err;
13797 +       struct au_branch *br;
13798 +       struct au_br_fhsm *bf;
13799 +
13800 +       br = au_sbr(sb, bindex);
13801 +       AuDebugOn(au_br_rdonly(br));
13802 +       bf = br->br_fhsm;
13803 +       AuDebugOn(!bf);
13804 +
13805 +       if (do_lock)
13806 +               mutex_lock(&bf->bf_lock);
13807 +       else
13808 +               MtxMustLock(&bf->bf_lock);
13809 +
13810 +       /* sb->s_root for NFS is unreliable */
13811 +       err = au_br_stfs(br, &bf->bf_stfs);
13812 +       if (unlikely(err)) {
13813 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13814 +               goto out;
13815 +       }
13816 +
13817 +       bf->bf_jiffy = jiffies;
13818 +       bf->bf_readable = 1;
13819 +       if (do_notify)
13820 +               au_fhsm_notify(sb, /*val*/1);
13821 +       if (rstfs)
13822 +               *rstfs = bf->bf_stfs;
13823 +
13824 +out:
13825 +       if (do_lock)
13826 +               mutex_unlock(&bf->bf_lock);
13827 +       au_fhsm_notify(sb, /*val*/1);
13828 +
13829 +       return err;
13830 +}
13831 +
13832 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13833 +{
13834 +       int err;
13835 +       struct au_sbinfo *sbinfo;
13836 +       struct au_fhsm *fhsm;
13837 +       struct au_branch *br;
13838 +       struct au_br_fhsm *bf;
13839 +
13840 +       AuDbg("b%d, force %d\n", bindex, force);
13841 +       SiMustAnyLock(sb);
13842 +
13843 +       sbinfo = au_sbi(sb);
13844 +       fhsm = &sbinfo->si_fhsm;
13845 +       if (!au_ftest_si(sbinfo, FHSM)
13846 +           || fhsm->fhsm_bottom == bindex)
13847 +               return;
13848 +
13849 +       br = au_sbr(sb, bindex);
13850 +       bf = br->br_fhsm;
13851 +       AuDebugOn(!bf);
13852 +       mutex_lock(&bf->bf_lock);
13853 +       if (force
13854 +           || au_fhsm_pid(fhsm)
13855 +           || au_fhsm_test_jiffy(sbinfo, br))
13856 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13857 +                                 /*do_notify*/1);
13858 +       mutex_unlock(&bf->bf_lock);
13859 +}
13860 +
13861 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13862 +{
13863 +       aufs_bindex_t bindex, bbot;
13864 +       struct au_branch *br;
13865 +
13866 +       /* exclude the bottom */
13867 +       bbot = au_fhsm_bottom(sb);
13868 +       for (bindex = 0; bindex < bbot; bindex++) {
13869 +               br = au_sbr(sb, bindex);
13870 +               if (au_br_fhsm(br->br_perm))
13871 +                       au_fhsm_wrote(sb, bindex, force);
13872 +       }
13873 +}
13874 +
13875 +/* ---------------------------------------------------------------------- */
13876 +
13877 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13878 +{
13879 +       __poll_t mask;
13880 +       struct au_sbinfo *sbinfo;
13881 +       struct au_fhsm *fhsm;
13882 +
13883 +       mask = 0;
13884 +       sbinfo = file->private_data;
13885 +       fhsm = &sbinfo->si_fhsm;
13886 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13887 +       if (atomic_read(&fhsm->fhsm_readable))
13888 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13889 +
13890 +       if (!mask)
13891 +               AuDbg("mask 0x%x\n", mask);
13892 +       return mask;
13893 +}
13894 +
13895 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13896 +                             struct aufs_stfs *stfs, __s16 brid)
13897 +{
13898 +       int err;
13899 +
13900 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13901 +       if (!err)
13902 +               err = __put_user(brid, &stbr->brid);
13903 +       if (unlikely(err))
13904 +               err = -EFAULT;
13905 +
13906 +       return err;
13907 +}
13908 +
13909 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13910 +                              struct aufs_stbr __user *stbr, size_t count)
13911 +{
13912 +       ssize_t err;
13913 +       int nstbr;
13914 +       aufs_bindex_t bindex, bbot;
13915 +       struct au_branch *br;
13916 +       struct au_br_fhsm *bf;
13917 +
13918 +       /* except the bottom branch */
13919 +       err = 0;
13920 +       nstbr = 0;
13921 +       bbot = au_fhsm_bottom(sb);
13922 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13923 +               br = au_sbr(sb, bindex);
13924 +               if (!au_br_fhsm(br->br_perm))
13925 +                       continue;
13926 +
13927 +               bf = br->br_fhsm;
13928 +               mutex_lock(&bf->bf_lock);
13929 +               if (bf->bf_readable) {
13930 +                       err = -EFAULT;
13931 +                       if (count >= sizeof(*stbr))
13932 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13933 +                                                         br->br_id);
13934 +                       if (!err) {
13935 +                               bf->bf_readable = 0;
13936 +                               count -= sizeof(*stbr);
13937 +                               nstbr++;
13938 +                       }
13939 +               }
13940 +               mutex_unlock(&bf->bf_lock);
13941 +       }
13942 +       if (!err)
13943 +               err = sizeof(*stbr) * nstbr;
13944 +
13945 +       return err;
13946 +}
13947 +
13948 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13949 +                          loff_t *pos)
13950 +{
13951 +       ssize_t err;
13952 +       int readable;
13953 +       aufs_bindex_t nfhsm, bindex, bbot;
13954 +       struct au_sbinfo *sbinfo;
13955 +       struct au_fhsm *fhsm;
13956 +       struct au_branch *br;
13957 +       struct super_block *sb;
13958 +
13959 +       err = 0;
13960 +       sbinfo = file->private_data;
13961 +       fhsm = &sbinfo->si_fhsm;
13962 +need_data:
13963 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13964 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13965 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13966 +                       err = -EAGAIN;
13967 +               else
13968 +                       err = wait_event_interruptible_locked_irq
13969 +                               (fhsm->fhsm_wqh,
13970 +                                atomic_read(&fhsm->fhsm_readable));
13971 +       }
13972 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13973 +       if (unlikely(err))
13974 +               goto out;
13975 +
13976 +       /* sb may already be dead */
13977 +       au_rw_read_lock(&sbinfo->si_rwsem);
13978 +       readable = atomic_read(&fhsm->fhsm_readable);
13979 +       if (readable > 0) {
13980 +               sb = sbinfo->si_sb;
13981 +               AuDebugOn(!sb);
13982 +               /* exclude the bottom branch */
13983 +               nfhsm = 0;
13984 +               bbot = au_fhsm_bottom(sb);
13985 +               for (bindex = 0; bindex < bbot; bindex++) {
13986 +                       br = au_sbr(sb, bindex);
13987 +                       if (au_br_fhsm(br->br_perm))
13988 +                               nfhsm++;
13989 +               }
13990 +               err = -EMSGSIZE;
13991 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13992 +                       atomic_set(&fhsm->fhsm_readable, 0);
13993 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13994 +                                            count);
13995 +               }
13996 +       }
13997 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13998 +       if (!readable)
13999 +               goto need_data;
14000 +
14001 +out:
14002 +       return err;
14003 +}
14004 +
14005 +static int au_fhsm_release(struct inode *inode, struct file *file)
14006 +{
14007 +       struct au_sbinfo *sbinfo;
14008 +       struct au_fhsm *fhsm;
14009 +
14010 +       /* sb may already be dead */
14011 +       sbinfo = file->private_data;
14012 +       fhsm = &sbinfo->si_fhsm;
14013 +       spin_lock(&fhsm->fhsm_spin);
14014 +       fhsm->fhsm_pid = 0;
14015 +       spin_unlock(&fhsm->fhsm_spin);
14016 +       kobject_put(&sbinfo->si_kobj);
14017 +
14018 +       return 0;
14019 +}
14020 +
14021 +static const struct file_operations au_fhsm_fops = {
14022 +       .owner          = THIS_MODULE,
14023 +       .llseek         = noop_llseek,
14024 +       .read           = au_fhsm_read,
14025 +       .poll           = au_fhsm_poll,
14026 +       .release        = au_fhsm_release
14027 +};
14028 +
14029 +int au_fhsm_fd(struct super_block *sb, int oflags)
14030 +{
14031 +       int err, fd;
14032 +       struct au_sbinfo *sbinfo;
14033 +       struct au_fhsm *fhsm;
14034 +
14035 +       err = -EPERM;
14036 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14037 +               goto out;
14038 +
14039 +       err = -EINVAL;
14040 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14041 +               goto out;
14042 +
14043 +       err = 0;
14044 +       sbinfo = au_sbi(sb);
14045 +       fhsm = &sbinfo->si_fhsm;
14046 +       spin_lock(&fhsm->fhsm_spin);
14047 +       if (!fhsm->fhsm_pid)
14048 +               fhsm->fhsm_pid = current->pid;
14049 +       else
14050 +               err = -EBUSY;
14051 +       spin_unlock(&fhsm->fhsm_spin);
14052 +       if (unlikely(err))
14053 +               goto out;
14054 +
14055 +       oflags |= O_RDONLY;
14056 +       /* oflags |= FMODE_NONOTIFY; */
14057 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14058 +       err = fd;
14059 +       if (unlikely(fd < 0))
14060 +               goto out_pid;
14061 +
14062 +       /* succeed regardless 'fhsm' status */
14063 +       kobject_get(&sbinfo->si_kobj);
14064 +       si_noflush_read_lock(sb);
14065 +       if (au_ftest_si(sbinfo, FHSM))
14066 +               au_fhsm_wrote_all(sb, /*force*/0);
14067 +       si_read_unlock(sb);
14068 +       goto out; /* success */
14069 +
14070 +out_pid:
14071 +       spin_lock(&fhsm->fhsm_spin);
14072 +       fhsm->fhsm_pid = 0;
14073 +       spin_unlock(&fhsm->fhsm_spin);
14074 +out:
14075 +       AuTraceErr(err);
14076 +       return err;
14077 +}
14078 +
14079 +/* ---------------------------------------------------------------------- */
14080 +
14081 +int au_fhsm_br_alloc(struct au_branch *br)
14082 +{
14083 +       int err;
14084 +
14085 +       err = 0;
14086 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14087 +       if (br->br_fhsm)
14088 +               au_br_fhsm_init(br->br_fhsm);
14089 +       else
14090 +               err = -ENOMEM;
14091 +
14092 +       return err;
14093 +}
14094 +
14095 +/* ---------------------------------------------------------------------- */
14096 +
14097 +void au_fhsm_fin(struct super_block *sb)
14098 +{
14099 +       au_fhsm_notify(sb, /*val*/-1);
14100 +}
14101 +
14102 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14103 +{
14104 +       struct au_fhsm *fhsm;
14105 +
14106 +       fhsm = &sbinfo->si_fhsm;
14107 +       spin_lock_init(&fhsm->fhsm_spin);
14108 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14109 +       atomic_set(&fhsm->fhsm_readable, 0);
14110 +       fhsm->fhsm_expire
14111 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14112 +       fhsm->fhsm_bottom = -1;
14113 +}
14114 +
14115 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14116 +{
14117 +       sbinfo->si_fhsm.fhsm_expire
14118 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14119 +}
14120 +
14121 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14122 +{
14123 +       unsigned int u;
14124 +
14125 +       if (!au_ftest_si(sbinfo, FHSM))
14126 +               return;
14127 +
14128 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14129 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14130 +               seq_printf(seq, ",fhsm_sec=%u", u);
14131 +}
14132 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14133 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14134 +++ linux/fs/aufs/file.c        2020-01-27 10:57:18.172204883 +0100
14135 @@ -0,0 +1,863 @@
14136 +// SPDX-License-Identifier: GPL-2.0
14137 +/*
14138 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14139 + *
14140 + * This program, aufs is free software; you can redistribute it and/or modify
14141 + * it under the terms of the GNU General Public License as published by
14142 + * the Free Software Foundation; either version 2 of the License, or
14143 + * (at your option) any later version.
14144 + *
14145 + * This program is distributed in the hope that it will be useful,
14146 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14147 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14148 + * GNU General Public License for more details.
14149 + *
14150 + * You should have received a copy of the GNU General Public License
14151 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14152 + */
14153 +
14154 +/*
14155 + * handling file/dir, and address_space operation
14156 + */
14157 +
14158 +#ifdef CONFIG_AUFS_DEBUG
14159 +#include <linux/migrate.h>
14160 +#endif
14161 +#include <linux/pagemap.h>
14162 +#include "aufs.h"
14163 +
14164 +/* drop flags for writing */
14165 +unsigned int au_file_roflags(unsigned int flags)
14166 +{
14167 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14168 +       flags |= O_RDONLY | O_NOATIME;
14169 +       return flags;
14170 +}
14171 +
14172 +/* common functions to regular file and dir */
14173 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14174 +                      struct file *file, int force_wr)
14175 +{
14176 +       struct file *h_file;
14177 +       struct dentry *h_dentry;
14178 +       struct inode *h_inode;
14179 +       struct super_block *sb;
14180 +       struct au_branch *br;
14181 +       struct path h_path;
14182 +       int err;
14183 +
14184 +       /* a race condition can happen between open and unlink/rmdir */
14185 +       h_file = ERR_PTR(-ENOENT);
14186 +       h_dentry = au_h_dptr(dentry, bindex);
14187 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14188 +               goto out;
14189 +       h_inode = d_inode(h_dentry);
14190 +       spin_lock(&h_dentry->d_lock);
14191 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14192 +               /* || !d_inode(dentry)->i_nlink */
14193 +               ;
14194 +       spin_unlock(&h_dentry->d_lock);
14195 +       if (unlikely(err))
14196 +               goto out;
14197 +
14198 +       sb = dentry->d_sb;
14199 +       br = au_sbr(sb, bindex);
14200 +       err = au_br_test_oflag(flags, br);
14201 +       h_file = ERR_PTR(err);
14202 +       if (unlikely(err))
14203 +               goto out;
14204 +
14205 +       /* drop flags for writing */
14206 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14207 +               if (force_wr && !(flags & O_WRONLY))
14208 +                       force_wr = 0;
14209 +               flags = au_file_roflags(flags);
14210 +               if (force_wr) {
14211 +                       h_file = ERR_PTR(-EROFS);
14212 +                       flags = au_file_roflags(flags);
14213 +                       if (unlikely(vfsub_native_ro(h_inode)
14214 +                                    || IS_APPEND(h_inode)))
14215 +                               goto out;
14216 +                       flags &= ~O_ACCMODE;
14217 +                       flags |= O_WRONLY;
14218 +               }
14219 +       }
14220 +       flags &= ~O_CREAT;
14221 +       au_lcnt_inc(&br->br_nfiles);
14222 +       h_path.dentry = h_dentry;
14223 +       h_path.mnt = au_br_mnt(br);
14224 +       h_file = vfsub_dentry_open(&h_path, flags);
14225 +       if (IS_ERR(h_file))
14226 +               goto out_br;
14227 +
14228 +       if (flags & __FMODE_EXEC) {
14229 +               err = deny_write_access(h_file);
14230 +               if (unlikely(err)) {
14231 +                       fput(h_file);
14232 +                       h_file = ERR_PTR(err);
14233 +                       goto out_br;
14234 +               }
14235 +       }
14236 +       fsnotify_open(h_file);
14237 +       goto out; /* success */
14238 +
14239 +out_br:
14240 +       au_lcnt_dec(&br->br_nfiles);
14241 +out:
14242 +       return h_file;
14243 +}
14244 +
14245 +static int au_cmoo(struct dentry *dentry)
14246 +{
14247 +       int err, cmoo, matched;
14248 +       unsigned int udba;
14249 +       struct path h_path;
14250 +       struct au_pin pin;
14251 +       struct au_cp_generic cpg = {
14252 +               .dentry = dentry,
14253 +               .bdst   = -1,
14254 +               .bsrc   = -1,
14255 +               .len    = -1,
14256 +               .pin    = &pin,
14257 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14258 +       };
14259 +       struct inode *delegated;
14260 +       struct super_block *sb;
14261 +       struct au_sbinfo *sbinfo;
14262 +       struct au_fhsm *fhsm;
14263 +       pid_t pid;
14264 +       struct au_branch *br;
14265 +       struct dentry *parent;
14266 +       struct au_hinode *hdir;
14267 +
14268 +       DiMustWriteLock(dentry);
14269 +       IiMustWriteLock(d_inode(dentry));
14270 +
14271 +       err = 0;
14272 +       if (IS_ROOT(dentry))
14273 +               goto out;
14274 +       cpg.bsrc = au_dbtop(dentry);
14275 +       if (!cpg.bsrc)
14276 +               goto out;
14277 +
14278 +       sb = dentry->d_sb;
14279 +       sbinfo = au_sbi(sb);
14280 +       fhsm = &sbinfo->si_fhsm;
14281 +       pid = au_fhsm_pid(fhsm);
14282 +       rcu_read_lock();
14283 +       matched = (pid
14284 +                  && (current->pid == pid
14285 +                      || rcu_dereference(current->real_parent)->pid == pid));
14286 +       rcu_read_unlock();
14287 +       if (matched)
14288 +               goto out;
14289 +
14290 +       br = au_sbr(sb, cpg.bsrc);
14291 +       cmoo = au_br_cmoo(br->br_perm);
14292 +       if (!cmoo)
14293 +               goto out;
14294 +       if (!d_is_reg(dentry))
14295 +               cmoo &= AuBrAttr_COO_ALL;
14296 +       if (!cmoo)
14297 +               goto out;
14298 +
14299 +       parent = dget_parent(dentry);
14300 +       di_write_lock_parent(parent);
14301 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14302 +       cpg.bdst = err;
14303 +       if (unlikely(err < 0)) {
14304 +               err = 0;        /* there is no upper writable branch */
14305 +               goto out_dgrade;
14306 +       }
14307 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14308 +
14309 +       /* do not respect the coo attrib for the target branch */
14310 +       err = au_cpup_dirs(dentry, cpg.bdst);
14311 +       if (unlikely(err))
14312 +               goto out_dgrade;
14313 +
14314 +       di_downgrade_lock(parent, AuLock_IR);
14315 +       udba = au_opt_udba(sb);
14316 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14317 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14318 +       if (unlikely(err))
14319 +               goto out_parent;
14320 +
14321 +       err = au_sio_cpup_simple(&cpg);
14322 +       au_unpin(&pin);
14323 +       if (unlikely(err))
14324 +               goto out_parent;
14325 +       if (!(cmoo & AuBrWAttr_MOO))
14326 +               goto out_parent; /* success */
14327 +
14328 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14329 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14330 +       if (unlikely(err))
14331 +               goto out_parent;
14332 +
14333 +       h_path.mnt = au_br_mnt(br);
14334 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14335 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14336 +       delegated = NULL;
14337 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14338 +       au_unpin(&pin);
14339 +       /* todo: keep h_dentry or not? */
14340 +       if (unlikely(err == -EWOULDBLOCK)) {
14341 +               pr_warn("cannot retry for NFSv4 delegation"
14342 +                       " for an internal unlink\n");
14343 +               iput(delegated);
14344 +       }
14345 +       if (unlikely(err)) {
14346 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14347 +                      dentry, err);
14348 +               err = 0;
14349 +       }
14350 +       goto out_parent; /* success */
14351 +
14352 +out_dgrade:
14353 +       di_downgrade_lock(parent, AuLock_IR);
14354 +out_parent:
14355 +       di_read_unlock(parent, AuLock_IR);
14356 +       dput(parent);
14357 +out:
14358 +       AuTraceErr(err);
14359 +       return err;
14360 +}
14361 +
14362 +int au_do_open(struct file *file, struct au_do_open_args *args)
14363 +{
14364 +       int err, aopen = args->aopen;
14365 +       struct dentry *dentry;
14366 +       struct au_finfo *finfo;
14367 +
14368 +       if (!aopen)
14369 +               err = au_finfo_init(file, args->fidir);
14370 +       else {
14371 +               lockdep_off();
14372 +               err = au_finfo_init(file, args->fidir);
14373 +               lockdep_on();
14374 +       }
14375 +       if (unlikely(err))
14376 +               goto out;
14377 +
14378 +       dentry = file->f_path.dentry;
14379 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14380 +       di_write_lock_child(dentry);
14381 +       err = au_cmoo(dentry);
14382 +       di_downgrade_lock(dentry, AuLock_IR);
14383 +       if (!err) {
14384 +               if (!aopen)
14385 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14386 +               else {
14387 +                       lockdep_off();
14388 +                       err = args->open(file, vfsub_file_flags(file),
14389 +                                        args->h_file);
14390 +                       lockdep_on();
14391 +               }
14392 +       }
14393 +       di_read_unlock(dentry, AuLock_IR);
14394 +
14395 +       finfo = au_fi(file);
14396 +       if (!err) {
14397 +               finfo->fi_file = file;
14398 +               au_hbl_add(&finfo->fi_hlist,
14399 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14400 +       }
14401 +       if (!aopen)
14402 +               fi_write_unlock(file);
14403 +       else {
14404 +               lockdep_off();
14405 +               fi_write_unlock(file);
14406 +               lockdep_on();
14407 +       }
14408 +       if (unlikely(err)) {
14409 +               finfo->fi_hdir = NULL;
14410 +               au_finfo_fin(file);
14411 +       }
14412 +
14413 +out:
14414 +       AuTraceErr(err);
14415 +       return err;
14416 +}
14417 +
14418 +int au_reopen_nondir(struct file *file)
14419 +{
14420 +       int err;
14421 +       aufs_bindex_t btop;
14422 +       struct dentry *dentry;
14423 +       struct au_branch *br;
14424 +       struct file *h_file, *h_file_tmp;
14425 +
14426 +       dentry = file->f_path.dentry;
14427 +       btop = au_dbtop(dentry);
14428 +       br = au_sbr(dentry->d_sb, btop);
14429 +       h_file_tmp = NULL;
14430 +       if (au_fbtop(file) == btop) {
14431 +               h_file = au_hf_top(file);
14432 +               if (file->f_mode == h_file->f_mode)
14433 +                       return 0; /* success */
14434 +               h_file_tmp = h_file;
14435 +               get_file(h_file_tmp);
14436 +               au_lcnt_inc(&br->br_nfiles);
14437 +               au_set_h_fptr(file, btop, NULL);
14438 +       }
14439 +       AuDebugOn(au_fi(file)->fi_hdir);
14440 +       /*
14441 +        * it can happen
14442 +        * file exists on both of rw and ro
14443 +        * open --> dbtop and fbtop are both 0
14444 +        * prepend a branch as rw, "rw" become ro
14445 +        * remove rw/file
14446 +        * delete the top branch, "rw" becomes rw again
14447 +        *      --> dbtop is 1, fbtop is still 0
14448 +        * write --> fbtop is 0 but dbtop is 1
14449 +        */
14450 +       /* AuDebugOn(au_fbtop(file) < btop); */
14451 +
14452 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14453 +                          file, /*force_wr*/0);
14454 +       err = PTR_ERR(h_file);
14455 +       if (IS_ERR(h_file)) {
14456 +               if (h_file_tmp) {
14457 +                       /* revert */
14458 +                       au_set_h_fptr(file, btop, h_file_tmp);
14459 +                       h_file_tmp = NULL;
14460 +               }
14461 +               goto out; /* todo: close all? */
14462 +       }
14463 +
14464 +       err = 0;
14465 +       au_set_fbtop(file, btop);
14466 +       au_set_h_fptr(file, btop, h_file);
14467 +       au_update_figen(file);
14468 +       /* todo: necessary? */
14469 +       /* file->f_ra = h_file->f_ra; */
14470 +
14471 +out:
14472 +       if (h_file_tmp) {
14473 +               fput(h_file_tmp);
14474 +               au_lcnt_dec(&br->br_nfiles);
14475 +       }
14476 +       return err;
14477 +}
14478 +
14479 +/* ---------------------------------------------------------------------- */
14480 +
14481 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14482 +                       struct dentry *hi_wh)
14483 +{
14484 +       int err;
14485 +       aufs_bindex_t btop;
14486 +       struct au_dinfo *dinfo;
14487 +       struct dentry *h_dentry;
14488 +       struct au_hdentry *hdp;
14489 +
14490 +       dinfo = au_di(file->f_path.dentry);
14491 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14492 +
14493 +       btop = dinfo->di_btop;
14494 +       dinfo->di_btop = btgt;
14495 +       hdp = au_hdentry(dinfo, btgt);
14496 +       h_dentry = hdp->hd_dentry;
14497 +       hdp->hd_dentry = hi_wh;
14498 +       err = au_reopen_nondir(file);
14499 +       hdp->hd_dentry = h_dentry;
14500 +       dinfo->di_btop = btop;
14501 +
14502 +       return err;
14503 +}
14504 +
14505 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14506 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14507 +{
14508 +       int err;
14509 +       struct inode *inode, *h_inode;
14510 +       struct dentry *h_dentry, *hi_wh;
14511 +       struct au_cp_generic cpg = {
14512 +               .dentry = file->f_path.dentry,
14513 +               .bdst   = bcpup,
14514 +               .bsrc   = -1,
14515 +               .len    = len,
14516 +               .pin    = pin
14517 +       };
14518 +
14519 +       au_update_dbtop(cpg.dentry);
14520 +       inode = d_inode(cpg.dentry);
14521 +       h_inode = NULL;
14522 +       if (au_dbtop(cpg.dentry) <= bcpup
14523 +           && au_dbbot(cpg.dentry) >= bcpup) {
14524 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14525 +               if (h_dentry && d_is_positive(h_dentry))
14526 +                       h_inode = d_inode(h_dentry);
14527 +       }
14528 +       hi_wh = au_hi_wh(inode, bcpup);
14529 +       if (!hi_wh && !h_inode)
14530 +               err = au_sio_cpup_wh(&cpg, file);
14531 +       else
14532 +               /* already copied-up after unlink */
14533 +               err = au_reopen_wh(file, bcpup, hi_wh);
14534 +
14535 +       if (!err
14536 +           && (inode->i_nlink > 1
14537 +               || (inode->i_state & I_LINKABLE))
14538 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14539 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14540 +
14541 +       return err;
14542 +}
14543 +
14544 +/*
14545 + * prepare the @file for writing.
14546 + */
14547 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14548 +{
14549 +       int err;
14550 +       aufs_bindex_t dbtop;
14551 +       struct dentry *parent;
14552 +       struct inode *inode;
14553 +       struct super_block *sb;
14554 +       struct file *h_file;
14555 +       struct au_cp_generic cpg = {
14556 +               .dentry = file->f_path.dentry,
14557 +               .bdst   = -1,
14558 +               .bsrc   = -1,
14559 +               .len    = len,
14560 +               .pin    = pin,
14561 +               .flags  = AuCpup_DTIME
14562 +       };
14563 +
14564 +       sb = cpg.dentry->d_sb;
14565 +       inode = d_inode(cpg.dentry);
14566 +       cpg.bsrc = au_fbtop(file);
14567 +       err = au_test_ro(sb, cpg.bsrc, inode);
14568 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14569 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14570 +                            /*flags*/0);
14571 +               goto out;
14572 +       }
14573 +
14574 +       /* need to cpup or reopen */
14575 +       parent = dget_parent(cpg.dentry);
14576 +       di_write_lock_parent(parent);
14577 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14578 +       cpg.bdst = err;
14579 +       if (unlikely(err < 0))
14580 +               goto out_dgrade;
14581 +       err = 0;
14582 +
14583 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14584 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14585 +               if (unlikely(err))
14586 +                       goto out_dgrade;
14587 +       }
14588 +
14589 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14590 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14591 +       if (unlikely(err))
14592 +               goto out_dgrade;
14593 +
14594 +       dbtop = au_dbtop(cpg.dentry);
14595 +       if (dbtop <= cpg.bdst)
14596 +               cpg.bsrc = cpg.bdst;
14597 +
14598 +       if (dbtop <= cpg.bdst           /* just reopen */
14599 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14600 +               ) {
14601 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14602 +               if (IS_ERR(h_file))
14603 +                       err = PTR_ERR(h_file);
14604 +               else {
14605 +                       di_downgrade_lock(parent, AuLock_IR);
14606 +                       if (dbtop > cpg.bdst)
14607 +                               err = au_sio_cpup_simple(&cpg);
14608 +                       if (!err)
14609 +                               err = au_reopen_nondir(file);
14610 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14611 +               }
14612 +       } else {                        /* copyup as wh and reopen */
14613 +               /*
14614 +                * since writable hfsplus branch is not supported,
14615 +                * h_open_pre/post() are unnecessary.
14616 +                */
14617 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14618 +               di_downgrade_lock(parent, AuLock_IR);
14619 +       }
14620 +
14621 +       if (!err) {
14622 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14623 +               goto out_dput; /* success */
14624 +       }
14625 +       au_unpin(pin);
14626 +       goto out_unlock;
14627 +
14628 +out_dgrade:
14629 +       di_downgrade_lock(parent, AuLock_IR);
14630 +out_unlock:
14631 +       di_read_unlock(parent, AuLock_IR);
14632 +out_dput:
14633 +       dput(parent);
14634 +out:
14635 +       return err;
14636 +}
14637 +
14638 +/* ---------------------------------------------------------------------- */
14639 +
14640 +int au_do_flush(struct file *file, fl_owner_t id,
14641 +               int (*flush)(struct file *file, fl_owner_t id))
14642 +{
14643 +       int err;
14644 +       struct super_block *sb;
14645 +       struct inode *inode;
14646 +
14647 +       inode = file_inode(file);
14648 +       sb = inode->i_sb;
14649 +       si_noflush_read_lock(sb);
14650 +       fi_read_lock(file);
14651 +       ii_read_lock_child(inode);
14652 +
14653 +       err = flush(file, id);
14654 +       au_cpup_attr_timesizes(inode);
14655 +
14656 +       ii_read_unlock(inode);
14657 +       fi_read_unlock(file);
14658 +       si_read_unlock(sb);
14659 +       return err;
14660 +}
14661 +
14662 +/* ---------------------------------------------------------------------- */
14663 +
14664 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14665 +{
14666 +       int err;
14667 +       struct au_pin pin;
14668 +       struct au_finfo *finfo;
14669 +       struct dentry *parent, *hi_wh;
14670 +       struct inode *inode;
14671 +       struct super_block *sb;
14672 +       struct au_cp_generic cpg = {
14673 +               .dentry = file->f_path.dentry,
14674 +               .bdst   = -1,
14675 +               .bsrc   = -1,
14676 +               .len    = -1,
14677 +               .pin    = &pin,
14678 +               .flags  = AuCpup_DTIME
14679 +       };
14680 +
14681 +       FiMustWriteLock(file);
14682 +
14683 +       err = 0;
14684 +       finfo = au_fi(file);
14685 +       sb = cpg.dentry->d_sb;
14686 +       inode = d_inode(cpg.dentry);
14687 +       cpg.bdst = au_ibtop(inode);
14688 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14689 +               goto out;
14690 +
14691 +       parent = dget_parent(cpg.dentry);
14692 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14693 +               di_read_lock_parent(parent, !AuLock_IR);
14694 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14695 +               cpg.bdst = err;
14696 +               di_read_unlock(parent, !AuLock_IR);
14697 +               if (unlikely(err < 0))
14698 +                       goto out_parent;
14699 +               err = 0;
14700 +       }
14701 +
14702 +       di_read_lock_parent(parent, AuLock_IR);
14703 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14704 +       if (!S_ISDIR(inode->i_mode)
14705 +           && au_opt_test(au_mntflags(sb), PLINK)
14706 +           && au_plink_test(inode)
14707 +           && !d_unhashed(cpg.dentry)
14708 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14709 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14710 +               if (unlikely(err))
14711 +                       goto out_unlock;
14712 +
14713 +               /* always superio. */
14714 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14715 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14716 +               if (!err) {
14717 +                       err = au_sio_cpup_simple(&cpg);
14718 +                       au_unpin(&pin);
14719 +               }
14720 +       } else if (hi_wh) {
14721 +               /* already copied-up after unlink */
14722 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14723 +               *need_reopen = 0;
14724 +       }
14725 +
14726 +out_unlock:
14727 +       di_read_unlock(parent, AuLock_IR);
14728 +out_parent:
14729 +       dput(parent);
14730 +out:
14731 +       return err;
14732 +}
14733 +
14734 +static void au_do_refresh_dir(struct file *file)
14735 +{
14736 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14737 +       struct au_hfile *p, tmp, *q;
14738 +       struct au_finfo *finfo;
14739 +       struct super_block *sb;
14740 +       struct au_fidir *fidir;
14741 +
14742 +       FiMustWriteLock(file);
14743 +
14744 +       sb = file->f_path.dentry->d_sb;
14745 +       finfo = au_fi(file);
14746 +       fidir = finfo->fi_hdir;
14747 +       AuDebugOn(!fidir);
14748 +       p = fidir->fd_hfile + finfo->fi_btop;
14749 +       brid = p->hf_br->br_id;
14750 +       bbot = fidir->fd_bbot;
14751 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14752 +               if (!p->hf_file)
14753 +                       continue;
14754 +
14755 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14756 +               if (new_bindex == bindex)
14757 +                       continue;
14758 +               if (new_bindex < 0) {
14759 +                       au_set_h_fptr(file, bindex, NULL);
14760 +                       continue;
14761 +               }
14762 +
14763 +               /* swap two lower inode, and loop again */
14764 +               q = fidir->fd_hfile + new_bindex;
14765 +               tmp = *q;
14766 +               *q = *p;
14767 +               *p = tmp;
14768 +               if (tmp.hf_file) {
14769 +                       bindex--;
14770 +                       p--;
14771 +               }
14772 +       }
14773 +
14774 +       p = fidir->fd_hfile;
14775 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14776 +               bbot = au_sbbot(sb);
14777 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14778 +                    finfo->fi_btop++, p++)
14779 +                       if (p->hf_file) {
14780 +                               if (file_inode(p->hf_file))
14781 +                                       break;
14782 +                               au_hfput(p, /*execed*/0);
14783 +                       }
14784 +       } else {
14785 +               bbot = au_br_index(sb, brid);
14786 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14787 +                    finfo->fi_btop++, p++)
14788 +                       if (p->hf_file)
14789 +                               au_hfput(p, /*execed*/0);
14790 +               bbot = au_sbbot(sb);
14791 +       }
14792 +
14793 +       p = fidir->fd_hfile + bbot;
14794 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14795 +            fidir->fd_bbot--, p--)
14796 +               if (p->hf_file) {
14797 +                       if (file_inode(p->hf_file))
14798 +                               break;
14799 +                       au_hfput(p, /*execed*/0);
14800 +               }
14801 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14802 +}
14803 +
14804 +/*
14805 + * after branch manipulating, refresh the file.
14806 + */
14807 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14808 +{
14809 +       int err, need_reopen, nbr;
14810 +       aufs_bindex_t bbot, bindex;
14811 +       struct dentry *dentry;
14812 +       struct super_block *sb;
14813 +       struct au_finfo *finfo;
14814 +       struct au_hfile *hfile;
14815 +
14816 +       dentry = file->f_path.dentry;
14817 +       sb = dentry->d_sb;
14818 +       nbr = au_sbbot(sb) + 1;
14819 +       finfo = au_fi(file);
14820 +       if (!finfo->fi_hdir) {
14821 +               hfile = &finfo->fi_htop;
14822 +               AuDebugOn(!hfile->hf_file);
14823 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14824 +               AuDebugOn(bindex < 0);
14825 +               if (bindex != finfo->fi_btop)
14826 +                       au_set_fbtop(file, bindex);
14827 +       } else {
14828 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14829 +               if (unlikely(err))
14830 +                       goto out;
14831 +               au_do_refresh_dir(file);
14832 +       }
14833 +
14834 +       err = 0;
14835 +       need_reopen = 1;
14836 +       if (!au_test_mmapped(file))
14837 +               err = au_file_refresh_by_inode(file, &need_reopen);
14838 +       if (finfo->fi_hdir)
14839 +               /* harmless if err */
14840 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14841 +       if (!err && need_reopen && !d_unlinked(dentry))
14842 +               err = reopen(file);
14843 +       if (!err) {
14844 +               au_update_figen(file);
14845 +               goto out; /* success */
14846 +       }
14847 +
14848 +       /* error, close all lower files */
14849 +       if (finfo->fi_hdir) {
14850 +               bbot = au_fbbot_dir(file);
14851 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14852 +                       au_set_h_fptr(file, bindex, NULL);
14853 +       }
14854 +
14855 +out:
14856 +       return err;
14857 +}
14858 +
14859 +/* common function to regular file and dir */
14860 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14861 +                         int wlock, unsigned int fi_lsc)
14862 +{
14863 +       int err;
14864 +       unsigned int sigen, figen;
14865 +       aufs_bindex_t btop;
14866 +       unsigned char pseudo_link;
14867 +       struct dentry *dentry;
14868 +       struct inode *inode;
14869 +
14870 +       err = 0;
14871 +       dentry = file->f_path.dentry;
14872 +       inode = d_inode(dentry);
14873 +       sigen = au_sigen(dentry->d_sb);
14874 +       fi_write_lock_nested(file, fi_lsc);
14875 +       figen = au_figen(file);
14876 +       if (!fi_lsc)
14877 +               di_write_lock_child(dentry);
14878 +       else
14879 +               di_write_lock_child2(dentry);
14880 +       btop = au_dbtop(dentry);
14881 +       pseudo_link = (btop != au_ibtop(inode));
14882 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14883 +               if (!wlock) {
14884 +                       di_downgrade_lock(dentry, AuLock_IR);
14885 +                       fi_downgrade_lock(file);
14886 +               }
14887 +               goto out; /* success */
14888 +       }
14889 +
14890 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14891 +       if (au_digen_test(dentry, sigen)) {
14892 +               err = au_reval_dpath(dentry, sigen);
14893 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14894 +       }
14895 +
14896 +       if (!err)
14897 +               err = refresh_file(file, reopen);
14898 +       if (!err) {
14899 +               if (!wlock) {
14900 +                       di_downgrade_lock(dentry, AuLock_IR);
14901 +                       fi_downgrade_lock(file);
14902 +               }
14903 +       } else {
14904 +               di_write_unlock(dentry);
14905 +               fi_write_unlock(file);
14906 +       }
14907 +
14908 +out:
14909 +       return err;
14910 +}
14911 +
14912 +/* ---------------------------------------------------------------------- */
14913 +
14914 +/* cf. aufs_nopage() */
14915 +/* for madvise(2) */
14916 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14917 +{
14918 +       unlock_page(page);
14919 +       return 0;
14920 +}
14921 +
14922 +/* it will never be called, but necessary to support O_DIRECT */
14923 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14924 +{ BUG(); return 0; }
14925 +
14926 +/* they will never be called. */
14927 +#ifdef CONFIG_AUFS_DEBUG
14928 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14929 +                           loff_t pos, unsigned len, unsigned flags,
14930 +                           struct page **pagep, void **fsdata)
14931 +{ AuUnsupport(); return 0; }
14932 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14933 +                         loff_t pos, unsigned len, unsigned copied,
14934 +                         struct page *page, void *fsdata)
14935 +{ AuUnsupport(); return 0; }
14936 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14937 +{ AuUnsupport(); return 0; }
14938 +
14939 +static int aufs_set_page_dirty(struct page *page)
14940 +{ AuUnsupport(); return 0; }
14941 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14942 +                               unsigned int length)
14943 +{ AuUnsupport(); }
14944 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14945 +{ AuUnsupport(); return 0; }
14946 +#if 0 /* called by memory compaction regardless file */
14947 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14948 +                           struct page *page, enum migrate_mode mode)
14949 +{ AuUnsupport(); return 0; }
14950 +#endif
14951 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14952 +{ AuUnsupport(); return true; }
14953 +static void aufs_putback_page(struct page *page)
14954 +{ AuUnsupport(); }
14955 +static int aufs_launder_page(struct page *page)
14956 +{ AuUnsupport(); return 0; }
14957 +static int aufs_is_partially_uptodate(struct page *page,
14958 +                                     unsigned long from,
14959 +                                     unsigned long count)
14960 +{ AuUnsupport(); return 0; }
14961 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14962 +                                   bool *writeback)
14963 +{ AuUnsupport(); }
14964 +static int aufs_error_remove_page(struct address_space *mapping,
14965 +                                 struct page *page)
14966 +{ AuUnsupport(); return 0; }
14967 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14968 +                             sector_t *span)
14969 +{ AuUnsupport(); return 0; }
14970 +static void aufs_swap_deactivate(struct file *file)
14971 +{ AuUnsupport(); }
14972 +#endif /* CONFIG_AUFS_DEBUG */
14973 +
14974 +const struct address_space_operations aufs_aop = {
14975 +       .readpage               = aufs_readpage,
14976 +       .direct_IO              = aufs_direct_IO,
14977 +#ifdef CONFIG_AUFS_DEBUG
14978 +       .writepage              = aufs_writepage,
14979 +       /* no writepages, because of writepage */
14980 +       .set_page_dirty         = aufs_set_page_dirty,
14981 +       /* no readpages, because of readpage */
14982 +       .write_begin            = aufs_write_begin,
14983 +       .write_end              = aufs_write_end,
14984 +       /* no bmap, no block device */
14985 +       .invalidatepage         = aufs_invalidatepage,
14986 +       .releasepage            = aufs_releasepage,
14987 +       /* is fallback_migrate_page ok? */
14988 +       /* .migratepage         = aufs_migratepage, */
14989 +       .isolate_page           = aufs_isolate_page,
14990 +       .putback_page           = aufs_putback_page,
14991 +       .launder_page           = aufs_launder_page,
14992 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14993 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14994 +       .error_remove_page      = aufs_error_remove_page,
14995 +       .swap_activate          = aufs_swap_activate,
14996 +       .swap_deactivate        = aufs_swap_deactivate
14997 +#endif /* CONFIG_AUFS_DEBUG */
14998 +};
14999 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15000 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15001 +++ linux/fs/aufs/file.h        2020-01-27 10:57:18.172204883 +0100
15002 @@ -0,0 +1,342 @@
15003 +/* SPDX-License-Identifier: GPL-2.0 */
15004 +/*
15005 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15006 + *
15007 + * This program, aufs is free software; you can redistribute it and/or modify
15008 + * it under the terms of the GNU General Public License as published by
15009 + * the Free Software Foundation; either version 2 of the License, or
15010 + * (at your option) any later version.
15011 + *
15012 + * This program is distributed in the hope that it will be useful,
15013 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15014 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15015 + * GNU General Public License for more details.
15016 + *
15017 + * You should have received a copy of the GNU General Public License
15018 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15019 + */
15020 +
15021 +/*
15022 + * file operations
15023 + */
15024 +
15025 +#ifndef __AUFS_FILE_H__
15026 +#define __AUFS_FILE_H__
15027 +
15028 +#ifdef __KERNEL__
15029 +
15030 +#include <linux/file.h>
15031 +#include <linux/fs.h>
15032 +#include <linux/mm_types.h>
15033 +#include <linux/poll.h>
15034 +#include "rwsem.h"
15035 +
15036 +struct au_branch;
15037 +struct au_hfile {
15038 +       struct file             *hf_file;
15039 +       struct au_branch        *hf_br;
15040 +};
15041 +
15042 +struct au_vdir;
15043 +struct au_fidir {
15044 +       aufs_bindex_t           fd_bbot;
15045 +       aufs_bindex_t           fd_nent;
15046 +       struct au_vdir          *fd_vdir_cache;
15047 +       struct au_hfile         fd_hfile[];
15048 +};
15049 +
15050 +static inline int au_fidir_sz(int nent)
15051 +{
15052 +       AuDebugOn(nent < 0);
15053 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15054 +}
15055 +
15056 +struct au_finfo {
15057 +       atomic_t                fi_generation;
15058 +
15059 +       struct au_rwsem         fi_rwsem;
15060 +       aufs_bindex_t           fi_btop;
15061 +
15062 +       /* do not union them */
15063 +       struct {                                /* for non-dir */
15064 +               struct au_hfile                 fi_htop;
15065 +               atomic_t                        fi_mmapped;
15066 +       };
15067 +       struct au_fidir         *fi_hdir;       /* for dir only */
15068 +
15069 +       struct hlist_bl_node    fi_hlist;
15070 +       struct file             *fi_file;       /* very ugly */
15071 +       struct rcu_head         rcu;
15072 +} ____cacheline_aligned_in_smp;
15073 +
15074 +/* ---------------------------------------------------------------------- */
15075 +
15076 +/* file.c */
15077 +extern const struct address_space_operations aufs_aop;
15078 +unsigned int au_file_roflags(unsigned int flags);
15079 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15080 +                      struct file *file, int force_wr);
15081 +struct au_do_open_args {
15082 +       int             aopen;
15083 +       int             (*open)(struct file *file, int flags,
15084 +                               struct file *h_file);
15085 +       struct au_fidir *fidir;
15086 +       struct file     *h_file;
15087 +};
15088 +int au_do_open(struct file *file, struct au_do_open_args *args);
15089 +int au_reopen_nondir(struct file *file);
15090 +struct au_pin;
15091 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15092 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15093 +                         int wlock, unsigned int fi_lsc);
15094 +int au_do_flush(struct file *file, fl_owner_t id,
15095 +               int (*flush)(struct file *file, fl_owner_t id));
15096 +
15097 +/* poll.c */
15098 +#ifdef CONFIG_AUFS_POLL
15099 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15100 +#endif
15101 +
15102 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15103 +/* hfsplus.c */
15104 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15105 +                          int force_wr);
15106 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15107 +                   struct file *h_file);
15108 +#else
15109 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15110 +       aufs_bindex_t bindex, int force_wr)
15111 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15112 +          struct file *h_file);
15113 +#endif
15114 +
15115 +/* f_op.c */
15116 +extern const struct file_operations aufs_file_fop;
15117 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15118 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15119 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15120 +
15121 +/* finfo.c */
15122 +void au_hfput(struct au_hfile *hf, int execed);
15123 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15124 +                  struct file *h_file);
15125 +
15126 +void au_update_figen(struct file *file);
15127 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15128 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15129 +
15130 +void au_fi_init_once(void *_fi);
15131 +void au_finfo_fin(struct file *file);
15132 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15133 +
15134 +/* ioctl.c */
15135 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15136 +#ifdef CONFIG_COMPAT
15137 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15138 +                          unsigned long arg);
15139 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15140 +                             unsigned long arg);
15141 +#endif
15142 +
15143 +/* ---------------------------------------------------------------------- */
15144 +
15145 +static inline struct au_finfo *au_fi(struct file *file)
15146 +{
15147 +       return file->private_data;
15148 +}
15149 +
15150 +/* ---------------------------------------------------------------------- */
15151 +
15152 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15153 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15154 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15155 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15156 +/*
15157 +#define fi_read_trylock_nested(f) \
15158 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15159 +#define fi_write_trylock_nested(f) \
15160 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15161 +*/
15162 +
15163 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15164 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15165 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15166 +
15167 +/* lock subclass for finfo */
15168 +enum {
15169 +       AuLsc_FI_1,
15170 +       AuLsc_FI_2
15171 +};
15172 +
15173 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15174 +{
15175 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15176 +}
15177 +
15178 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15179 +{
15180 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15181 +}
15182 +
15183 +/*
15184 + * fi_read_lock_1, fi_write_lock_1,
15185 + * fi_read_lock_2, fi_write_lock_2
15186 + */
15187 +#define AuReadLockFunc(name) \
15188 +static inline void fi_read_lock_##name(struct file *f) \
15189 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15190 +
15191 +#define AuWriteLockFunc(name) \
15192 +static inline void fi_write_lock_##name(struct file *f) \
15193 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15194 +
15195 +#define AuRWLockFuncs(name) \
15196 +       AuReadLockFunc(name) \
15197 +       AuWriteLockFunc(name)
15198 +
15199 +AuRWLockFuncs(1);
15200 +AuRWLockFuncs(2);
15201 +
15202 +#undef AuReadLockFunc
15203 +#undef AuWriteLockFunc
15204 +#undef AuRWLockFuncs
15205 +
15206 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15207 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15208 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15209 +
15210 +/* ---------------------------------------------------------------------- */
15211 +
15212 +/* todo: hard/soft set? */
15213 +static inline aufs_bindex_t au_fbtop(struct file *file)
15214 +{
15215 +       FiMustAnyLock(file);
15216 +       return au_fi(file)->fi_btop;
15217 +}
15218 +
15219 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15220 +{
15221 +       FiMustAnyLock(file);
15222 +       AuDebugOn(!au_fi(file)->fi_hdir);
15223 +       return au_fi(file)->fi_hdir->fd_bbot;
15224 +}
15225 +
15226 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15227 +{
15228 +       FiMustAnyLock(file);
15229 +       AuDebugOn(!au_fi(file)->fi_hdir);
15230 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15231 +}
15232 +
15233 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15234 +{
15235 +       FiMustWriteLock(file);
15236 +       au_fi(file)->fi_btop = bindex;
15237 +}
15238 +
15239 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15240 +{
15241 +       FiMustWriteLock(file);
15242 +       AuDebugOn(!au_fi(file)->fi_hdir);
15243 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15244 +}
15245 +
15246 +static inline void au_set_fvdir_cache(struct file *file,
15247 +                                     struct au_vdir *vdir_cache)
15248 +{
15249 +       FiMustWriteLock(file);
15250 +       AuDebugOn(!au_fi(file)->fi_hdir);
15251 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15252 +}
15253 +
15254 +static inline struct file *au_hf_top(struct file *file)
15255 +{
15256 +       FiMustAnyLock(file);
15257 +       AuDebugOn(au_fi(file)->fi_hdir);
15258 +       return au_fi(file)->fi_htop.hf_file;
15259 +}
15260 +
15261 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15262 +{
15263 +       FiMustAnyLock(file);
15264 +       AuDebugOn(!au_fi(file)->fi_hdir);
15265 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15266 +}
15267 +
15268 +/* todo: memory barrier? */
15269 +static inline unsigned int au_figen(struct file *f)
15270 +{
15271 +       return atomic_read(&au_fi(f)->fi_generation);
15272 +}
15273 +
15274 +static inline void au_set_mmapped(struct file *f)
15275 +{
15276 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15277 +               return;
15278 +       pr_warn("fi_mmapped wrapped around\n");
15279 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15280 +               ;
15281 +}
15282 +
15283 +static inline void au_unset_mmapped(struct file *f)
15284 +{
15285 +       atomic_dec(&au_fi(f)->fi_mmapped);
15286 +}
15287 +
15288 +static inline int au_test_mmapped(struct file *f)
15289 +{
15290 +       return atomic_read(&au_fi(f)->fi_mmapped);
15291 +}
15292 +
15293 +/* customize vma->vm_file */
15294 +
15295 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15296 +                                      struct file *file)
15297 +{
15298 +       struct file *f;
15299 +
15300 +       f = vma->vm_file;
15301 +       get_file(file);
15302 +       vma->vm_file = file;
15303 +       fput(f);
15304 +}
15305 +
15306 +#ifdef CONFIG_MMU
15307 +#define AuDbgVmRegion(file, vma) do {} while (0)
15308 +
15309 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15310 +                                   struct file *file)
15311 +{
15312 +       au_do_vm_file_reset(vma, file);
15313 +}
15314 +#else
15315 +#define AuDbgVmRegion(file, vma) \
15316 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15317 +
15318 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15319 +                                   struct file *file)
15320 +{
15321 +       struct file *f;
15322 +
15323 +       au_do_vm_file_reset(vma, file);
15324 +       f = vma->vm_region->vm_file;
15325 +       get_file(file);
15326 +       vma->vm_region->vm_file = file;
15327 +       fput(f);
15328 +}
15329 +#endif /* CONFIG_MMU */
15330 +
15331 +/* handle vma->vm_prfile */
15332 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15333 +                                   struct file *file)
15334 +{
15335 +       get_file(file);
15336 +       vma->vm_prfile = file;
15337 +#ifndef CONFIG_MMU
15338 +       get_file(file);
15339 +       vma->vm_region->vm_prfile = file;
15340 +#endif
15341 +}
15342 +
15343 +#endif /* __KERNEL__ */
15344 +#endif /* __AUFS_FILE_H__ */
15345 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15346 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15347 +++ linux/fs/aufs/finfo.c       2020-01-27 10:57:18.172204883 +0100
15348 @@ -0,0 +1,149 @@
15349 +// SPDX-License-Identifier: GPL-2.0
15350 +/*
15351 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15352 + *
15353 + * This program, aufs is free software; you can redistribute it and/or modify
15354 + * it under the terms of the GNU General Public License as published by
15355 + * the Free Software Foundation; either version 2 of the License, or
15356 + * (at your option) any later version.
15357 + *
15358 + * This program is distributed in the hope that it will be useful,
15359 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15360 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15361 + * GNU General Public License for more details.
15362 + *
15363 + * You should have received a copy of the GNU General Public License
15364 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15365 + */
15366 +
15367 +/*
15368 + * file private data
15369 + */
15370 +
15371 +#include "aufs.h"
15372 +
15373 +void au_hfput(struct au_hfile *hf, int execed)
15374 +{
15375 +       if (execed)
15376 +               allow_write_access(hf->hf_file);
15377 +       fput(hf->hf_file);
15378 +       hf->hf_file = NULL;
15379 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15380 +       hf->hf_br = NULL;
15381 +}
15382 +
15383 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15384 +{
15385 +       struct au_finfo *finfo = au_fi(file);
15386 +       struct au_hfile *hf;
15387 +       struct au_fidir *fidir;
15388 +
15389 +       fidir = finfo->fi_hdir;
15390 +       if (!fidir) {
15391 +               AuDebugOn(finfo->fi_btop != bindex);
15392 +               hf = &finfo->fi_htop;
15393 +       } else
15394 +               hf = fidir->fd_hfile + bindex;
15395 +
15396 +       if (hf && hf->hf_file)
15397 +               au_hfput(hf, vfsub_file_execed(file));
15398 +       if (val) {
15399 +               FiMustWriteLock(file);
15400 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15401 +               hf->hf_file = val;
15402 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15403 +       }
15404 +}
15405 +
15406 +void au_update_figen(struct file *file)
15407 +{
15408 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15409 +       /* smp_mb(); */ /* atomic_set */
15410 +}
15411 +
15412 +/* ---------------------------------------------------------------------- */
15413 +
15414 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15415 +{
15416 +       struct au_fidir *fidir;
15417 +       int nbr;
15418 +
15419 +       nbr = au_sbbot(sb) + 1;
15420 +       if (nbr < 2)
15421 +               nbr = 2; /* initial allocate for 2 branches */
15422 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15423 +       if (fidir) {
15424 +               fidir->fd_bbot = -1;
15425 +               fidir->fd_nent = nbr;
15426 +       }
15427 +
15428 +       return fidir;
15429 +}
15430 +
15431 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15432 +{
15433 +       int err;
15434 +       struct au_fidir *fidir, *p;
15435 +
15436 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15437 +       fidir = finfo->fi_hdir;
15438 +       AuDebugOn(!fidir);
15439 +
15440 +       err = -ENOMEM;
15441 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15442 +                        GFP_NOFS, may_shrink);
15443 +       if (p) {
15444 +               p->fd_nent = nbr;
15445 +               finfo->fi_hdir = p;
15446 +               err = 0;
15447 +       }
15448 +
15449 +       return err;
15450 +}
15451 +
15452 +/* ---------------------------------------------------------------------- */
15453 +
15454 +void au_finfo_fin(struct file *file)
15455 +{
15456 +       struct au_finfo *finfo;
15457 +
15458 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15459 +
15460 +       finfo = au_fi(file);
15461 +       AuDebugOn(finfo->fi_hdir);
15462 +       AuRwDestroy(&finfo->fi_rwsem);
15463 +       au_cache_free_finfo(finfo);
15464 +}
15465 +
15466 +void au_fi_init_once(void *_finfo)
15467 +{
15468 +       struct au_finfo *finfo = _finfo;
15469 +
15470 +       au_rw_init(&finfo->fi_rwsem);
15471 +}
15472 +
15473 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15474 +{
15475 +       int err;
15476 +       struct au_finfo *finfo;
15477 +       struct dentry *dentry;
15478 +
15479 +       err = -ENOMEM;
15480 +       dentry = file->f_path.dentry;
15481 +       finfo = au_cache_alloc_finfo();
15482 +       if (unlikely(!finfo))
15483 +               goto out;
15484 +
15485 +       err = 0;
15486 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15487 +       au_rw_write_lock(&finfo->fi_rwsem);
15488 +       finfo->fi_btop = -1;
15489 +       finfo->fi_hdir = fidir;
15490 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15491 +       /* smp_mb(); */ /* atomic_set */
15492 +
15493 +       file->private_data = finfo;
15494 +
15495 +out:
15496 +       return err;
15497 +}
15498 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15499 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15500 +++ linux/fs/aufs/f_op.c        2020-01-27 10:57:18.172204883 +0100
15501 @@ -0,0 +1,819 @@
15502 +// SPDX-License-Identifier: GPL-2.0
15503 +/*
15504 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15505 + *
15506 + * This program, aufs is free software; you can redistribute it and/or modify
15507 + * it under the terms of the GNU General Public License as published by
15508 + * the Free Software Foundation; either version 2 of the License, or
15509 + * (at your option) any later version.
15510 + *
15511 + * This program is distributed in the hope that it will be useful,
15512 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15513 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15514 + * GNU General Public License for more details.
15515 + *
15516 + * You should have received a copy of the GNU General Public License
15517 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15518 + */
15519 +
15520 +/*
15521 + * file and vm operations
15522 + */
15523 +
15524 +#include <linux/aio.h>
15525 +#include <linux/fs_stack.h>
15526 +#include <linux/mman.h>
15527 +#include <linux/security.h>
15528 +#include "aufs.h"
15529 +
15530 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15531 +{
15532 +       int err;
15533 +       aufs_bindex_t bindex;
15534 +       struct dentry *dentry, *h_dentry;
15535 +       struct au_finfo *finfo;
15536 +       struct inode *h_inode;
15537 +
15538 +       FiMustWriteLock(file);
15539 +
15540 +       err = 0;
15541 +       dentry = file->f_path.dentry;
15542 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15543 +       finfo = au_fi(file);
15544 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15545 +       atomic_set(&finfo->fi_mmapped, 0);
15546 +       bindex = au_dbtop(dentry);
15547 +       if (!h_file) {
15548 +               h_dentry = au_h_dptr(dentry, bindex);
15549 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15550 +               if (unlikely(err))
15551 +                       goto out;
15552 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15553 +               if (IS_ERR(h_file)) {
15554 +                       err = PTR_ERR(h_file);
15555 +                       goto out;
15556 +               }
15557 +       } else {
15558 +               h_dentry = h_file->f_path.dentry;
15559 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15560 +               if (unlikely(err))
15561 +                       goto out;
15562 +               /* br ref is already inc-ed */
15563 +       }
15564 +
15565 +       if ((flags & __O_TMPFILE)
15566 +           && !(flags & O_EXCL)) {
15567 +               h_inode = file_inode(h_file);
15568 +               spin_lock(&h_inode->i_lock);
15569 +               h_inode->i_state |= I_LINKABLE;
15570 +               spin_unlock(&h_inode->i_lock);
15571 +       }
15572 +       au_set_fbtop(file, bindex);
15573 +       au_set_h_fptr(file, bindex, h_file);
15574 +       au_update_figen(file);
15575 +       /* todo: necessary? */
15576 +       /* file->f_ra = h_file->f_ra; */
15577 +
15578 +out:
15579 +       return err;
15580 +}
15581 +
15582 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15583 +                           struct file *file)
15584 +{
15585 +       int err;
15586 +       struct super_block *sb;
15587 +       struct au_do_open_args args = {
15588 +               .open   = au_do_open_nondir
15589 +       };
15590 +
15591 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15592 +             file, vfsub_file_flags(file), file->f_mode);
15593 +
15594 +       sb = file->f_path.dentry->d_sb;
15595 +       si_read_lock(sb, AuLock_FLUSH);
15596 +       err = au_do_open(file, &args);
15597 +       si_read_unlock(sb);
15598 +       return err;
15599 +}
15600 +
15601 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15602 +{
15603 +       struct au_finfo *finfo;
15604 +       aufs_bindex_t bindex;
15605 +
15606 +       finfo = au_fi(file);
15607 +       au_hbl_del(&finfo->fi_hlist,
15608 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15609 +       bindex = finfo->fi_btop;
15610 +       if (bindex >= 0)
15611 +               au_set_h_fptr(file, bindex, NULL);
15612 +
15613 +       au_finfo_fin(file);
15614 +       return 0;
15615 +}
15616 +
15617 +/* ---------------------------------------------------------------------- */
15618 +
15619 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15620 +{
15621 +       int err;
15622 +       struct file *h_file;
15623 +
15624 +       err = 0;
15625 +       h_file = au_hf_top(file);
15626 +       if (h_file)
15627 +               err = vfsub_flush(h_file, id);
15628 +       return err;
15629 +}
15630 +
15631 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15632 +{
15633 +       return au_do_flush(file, id, au_do_flush_nondir);
15634 +}
15635 +
15636 +/* ---------------------------------------------------------------------- */
15637 +/*
15638 + * read and write functions acquire [fdi]_rwsem once, but release before
15639 + * mmap_sem. This is because to stop a race condition between mmap(2).
15640 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15641 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15642 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15643 + */
15644 +
15645 +/* Callers should call au_read_post() or fput() in the end */
15646 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15647 +{
15648 +       struct file *h_file;
15649 +       int err;
15650 +
15651 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15652 +       if (!err) {
15653 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15654 +               h_file = au_hf_top(file);
15655 +               get_file(h_file);
15656 +               if (!keep_fi)
15657 +                       fi_read_unlock(file);
15658 +       } else
15659 +               h_file = ERR_PTR(err);
15660 +
15661 +       return h_file;
15662 +}
15663 +
15664 +static void au_read_post(struct inode *inode, struct file *h_file)
15665 +{
15666 +       /* update without lock, I don't think it a problem */
15667 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15668 +       fput(h_file);
15669 +}
15670 +
15671 +struct au_write_pre {
15672 +       /* input */
15673 +       unsigned int lsc;
15674 +
15675 +       /* output */
15676 +       blkcnt_t blks;
15677 +       aufs_bindex_t btop;
15678 +};
15679 +
15680 +/*
15681 + * return with iinfo is write-locked
15682 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15683 + * end
15684 + */
15685 +static struct file *au_write_pre(struct file *file, int do_ready,
15686 +                                struct au_write_pre *wpre)
15687 +{
15688 +       struct file *h_file;
15689 +       struct dentry *dentry;
15690 +       int err;
15691 +       unsigned int lsc;
15692 +       struct au_pin pin;
15693 +
15694 +       lsc = 0;
15695 +       if (wpre)
15696 +               lsc = wpre->lsc;
15697 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15698 +       h_file = ERR_PTR(err);
15699 +       if (unlikely(err))
15700 +               goto out;
15701 +
15702 +       dentry = file->f_path.dentry;
15703 +       if (do_ready) {
15704 +               err = au_ready_to_write(file, -1, &pin);
15705 +               if (unlikely(err)) {
15706 +                       h_file = ERR_PTR(err);
15707 +                       di_write_unlock(dentry);
15708 +                       goto out_fi;
15709 +               }
15710 +       }
15711 +
15712 +       di_downgrade_lock(dentry, /*flags*/0);
15713 +       if (wpre)
15714 +               wpre->btop = au_fbtop(file);
15715 +       h_file = au_hf_top(file);
15716 +       get_file(h_file);
15717 +       if (wpre)
15718 +               wpre->blks = file_inode(h_file)->i_blocks;
15719 +       if (do_ready)
15720 +               au_unpin(&pin);
15721 +       di_read_unlock(dentry, /*flags*/0);
15722 +
15723 +out_fi:
15724 +       fi_write_unlock(file);
15725 +out:
15726 +       return h_file;
15727 +}
15728 +
15729 +static void au_write_post(struct inode *inode, struct file *h_file,
15730 +                         struct au_write_pre *wpre, ssize_t written)
15731 +{
15732 +       struct inode *h_inode;
15733 +
15734 +       au_cpup_attr_timesizes(inode);
15735 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15736 +       h_inode = file_inode(h_file);
15737 +       inode->i_mode = h_inode->i_mode;
15738 +       ii_write_unlock(inode);
15739 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15740 +       if (written > 0)
15741 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15742 +                             /*force*/h_inode->i_blocks > wpre->blks);
15743 +       fput(h_file);
15744 +}
15745 +
15746 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15747 +                        loff_t *ppos)
15748 +{
15749 +       ssize_t err;
15750 +       struct inode *inode;
15751 +       struct file *h_file;
15752 +       struct super_block *sb;
15753 +
15754 +       inode = file_inode(file);
15755 +       sb = inode->i_sb;
15756 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15757 +
15758 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15759 +       err = PTR_ERR(h_file);
15760 +       if (IS_ERR(h_file))
15761 +               goto out;
15762 +
15763 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15764 +       err = vfsub_read_u(h_file, buf, count, ppos);
15765 +       /* todo: necessary? */
15766 +       /* file->f_ra = h_file->f_ra; */
15767 +       au_read_post(inode, h_file);
15768 +
15769 +out:
15770 +       si_read_unlock(sb);
15771 +       return err;
15772 +}
15773 +
15774 +/*
15775 + * todo: very ugly
15776 + * it locks both of i_mutex and si_rwsem for read in safe.
15777 + * if the plink maintenance mode continues forever (that is the problem),
15778 + * may loop forever.
15779 + */
15780 +static void au_mtx_and_read_lock(struct inode *inode)
15781 +{
15782 +       int err;
15783 +       struct super_block *sb = inode->i_sb;
15784 +
15785 +       while (1) {
15786 +               inode_lock(inode);
15787 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15788 +               if (!err)
15789 +                       break;
15790 +               inode_unlock(inode);
15791 +               si_read_lock(sb, AuLock_NOPLMW);
15792 +               si_read_unlock(sb);
15793 +       }
15794 +}
15795 +
15796 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15797 +                         size_t count, loff_t *ppos)
15798 +{
15799 +       ssize_t err;
15800 +       struct au_write_pre wpre;
15801 +       struct inode *inode;
15802 +       struct file *h_file;
15803 +       char __user *buf = (char __user *)ubuf;
15804 +
15805 +       inode = file_inode(file);
15806 +       au_mtx_and_read_lock(inode);
15807 +
15808 +       wpre.lsc = 0;
15809 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15810 +       err = PTR_ERR(h_file);
15811 +       if (IS_ERR(h_file))
15812 +               goto out;
15813 +
15814 +       err = vfsub_write_u(h_file, buf, count, ppos);
15815 +       au_write_post(inode, h_file, &wpre, err);
15816 +
15817 +out:
15818 +       si_read_unlock(inode->i_sb);
15819 +       inode_unlock(inode);
15820 +       return err;
15821 +}
15822 +
15823 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15824 +                         struct iov_iter *iov_iter)
15825 +{
15826 +       ssize_t err;
15827 +       struct file *file;
15828 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15829 +
15830 +       err = security_file_permission(h_file, rw);
15831 +       if (unlikely(err))
15832 +               goto out;
15833 +
15834 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15835 +       iter = NULL;
15836 +       if (rw == MAY_READ)
15837 +               iter = h_file->f_op->read_iter;
15838 +       else if (rw == MAY_WRITE)
15839 +               iter = h_file->f_op->write_iter;
15840 +
15841 +       file = kio->ki_filp;
15842 +       kio->ki_filp = h_file;
15843 +       if (iter) {
15844 +               lockdep_off();
15845 +               err = iter(kio, iov_iter);
15846 +               lockdep_on();
15847 +       } else
15848 +               /* currently there is no such fs */
15849 +               WARN_ON_ONCE(1);
15850 +       kio->ki_filp = file;
15851 +
15852 +out:
15853 +       return err;
15854 +}
15855 +
15856 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15857 +{
15858 +       ssize_t err;
15859 +       struct file *file, *h_file;
15860 +       struct inode *inode;
15861 +       struct super_block *sb;
15862 +
15863 +       file = kio->ki_filp;
15864 +       inode = file_inode(file);
15865 +       sb = inode->i_sb;
15866 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15867 +
15868 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15869 +       err = PTR_ERR(h_file);
15870 +       if (IS_ERR(h_file))
15871 +               goto out;
15872 +
15873 +       if (au_test_loopback_kthread()) {
15874 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15875 +               if (file->f_mapping != h_file->f_mapping) {
15876 +                       file->f_mapping = h_file->f_mapping;
15877 +                       smp_mb(); /* unnecessary? */
15878 +               }
15879 +       }
15880 +       fi_read_unlock(file);
15881 +
15882 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15883 +       /* todo: necessary? */
15884 +       /* file->f_ra = h_file->f_ra; */
15885 +       au_read_post(inode, h_file);
15886 +
15887 +out:
15888 +       si_read_unlock(sb);
15889 +       return err;
15890 +}
15891 +
15892 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15893 +{
15894 +       ssize_t err;
15895 +       struct au_write_pre wpre;
15896 +       struct inode *inode;
15897 +       struct file *file, *h_file;
15898 +
15899 +       file = kio->ki_filp;
15900 +       inode = file_inode(file);
15901 +       au_mtx_and_read_lock(inode);
15902 +
15903 +       wpre.lsc = 0;
15904 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15905 +       err = PTR_ERR(h_file);
15906 +       if (IS_ERR(h_file))
15907 +               goto out;
15908 +
15909 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15910 +       au_write_post(inode, h_file, &wpre, err);
15911 +
15912 +out:
15913 +       si_read_unlock(inode->i_sb);
15914 +       inode_unlock(inode);
15915 +       return err;
15916 +}
15917 +
15918 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15919 +                               struct pipe_inode_info *pipe, size_t len,
15920 +                               unsigned int flags)
15921 +{
15922 +       ssize_t err;
15923 +       struct file *h_file;
15924 +       struct inode *inode;
15925 +       struct super_block *sb;
15926 +
15927 +       inode = file_inode(file);
15928 +       sb = inode->i_sb;
15929 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15930 +
15931 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15932 +       err = PTR_ERR(h_file);
15933 +       if (IS_ERR(h_file))
15934 +               goto out;
15935 +
15936 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15937 +       /* todo: necessary? */
15938 +       /* file->f_ra = h_file->f_ra; */
15939 +       au_read_post(inode, h_file);
15940 +
15941 +out:
15942 +       si_read_unlock(sb);
15943 +       return err;
15944 +}
15945 +
15946 +static ssize_t
15947 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15948 +                 size_t len, unsigned int flags)
15949 +{
15950 +       ssize_t err;
15951 +       struct au_write_pre wpre;
15952 +       struct inode *inode;
15953 +       struct file *h_file;
15954 +
15955 +       inode = file_inode(file);
15956 +       au_mtx_and_read_lock(inode);
15957 +
15958 +       wpre.lsc = 0;
15959 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15960 +       err = PTR_ERR(h_file);
15961 +       if (IS_ERR(h_file))
15962 +               goto out;
15963 +
15964 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15965 +       au_write_post(inode, h_file, &wpre, err);
15966 +
15967 +out:
15968 +       si_read_unlock(inode->i_sb);
15969 +       inode_unlock(inode);
15970 +       return err;
15971 +}
15972 +
15973 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15974 +                          loff_t len)
15975 +{
15976 +       long err;
15977 +       struct au_write_pre wpre;
15978 +       struct inode *inode;
15979 +       struct file *h_file;
15980 +
15981 +       inode = file_inode(file);
15982 +       au_mtx_and_read_lock(inode);
15983 +
15984 +       wpre.lsc = 0;
15985 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15986 +       err = PTR_ERR(h_file);
15987 +       if (IS_ERR(h_file))
15988 +               goto out;
15989 +
15990 +       lockdep_off();
15991 +       err = vfs_fallocate(h_file, mode, offset, len);
15992 +       lockdep_on();
15993 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15994 +
15995 +out:
15996 +       si_read_unlock(inode->i_sb);
15997 +       inode_unlock(inode);
15998 +       return err;
15999 +}
16000 +
16001 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16002 +                                   struct file *dst, loff_t dst_pos,
16003 +                                   size_t len, unsigned int flags)
16004 +{
16005 +       ssize_t err;
16006 +       struct au_write_pre wpre;
16007 +       enum { SRC, DST };
16008 +       struct {
16009 +               struct inode *inode;
16010 +               struct file *h_file;
16011 +               struct super_block *h_sb;
16012 +       } a[2];
16013 +#define a_src  a[SRC]
16014 +#define a_dst  a[DST]
16015 +
16016 +       err = -EINVAL;
16017 +       a_src.inode = file_inode(src);
16018 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16019 +               goto out;
16020 +       a_dst.inode = file_inode(dst);
16021 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16022 +               goto out;
16023 +
16024 +       au_mtx_and_read_lock(a_dst.inode);
16025 +       /*
16026 +        * in order to match the order in di_write_lock2_{child,parent}(),
16027 +        * use f_path.dentry for this comparison.
16028 +        */
16029 +       if (src->f_path.dentry < dst->f_path.dentry) {
16030 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16031 +               err = PTR_ERR(a_src.h_file);
16032 +               if (IS_ERR(a_src.h_file))
16033 +                       goto out_si;
16034 +
16035 +               wpre.lsc = AuLsc_FI_2;
16036 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16037 +               err = PTR_ERR(a_dst.h_file);
16038 +               if (IS_ERR(a_dst.h_file)) {
16039 +                       au_read_post(a_src.inode, a_src.h_file);
16040 +                       goto out_si;
16041 +               }
16042 +       } else {
16043 +               wpre.lsc = AuLsc_FI_1;
16044 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16045 +               err = PTR_ERR(a_dst.h_file);
16046 +               if (IS_ERR(a_dst.h_file))
16047 +                       goto out_si;
16048 +
16049 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16050 +               err = PTR_ERR(a_src.h_file);
16051 +               if (IS_ERR(a_src.h_file)) {
16052 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16053 +                                     /*written*/0);
16054 +                       goto out_si;
16055 +               }
16056 +       }
16057 +
16058 +       err = -EXDEV;
16059 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16060 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16061 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16062 +               AuDbgFile(src);
16063 +               AuDbgFile(dst);
16064 +               goto out_file;
16065 +       }
16066 +
16067 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16068 +                                   dst_pos, len, flags);
16069 +
16070 +out_file:
16071 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16072 +       fi_read_unlock(src);
16073 +       au_read_post(a_src.inode, a_src.h_file);
16074 +out_si:
16075 +       si_read_unlock(a_dst.inode->i_sb);
16076 +       inode_unlock(a_dst.inode);
16077 +out:
16078 +       return err;
16079 +#undef a_src
16080 +#undef a_dst
16081 +}
16082 +
16083 +/* ---------------------------------------------------------------------- */
16084 +
16085 +/*
16086 + * The locking order around current->mmap_sem.
16087 + * - in most and regular cases
16088 + *   file I/O syscall -- aufs_read() or something
16089 + *     -- si_rwsem for read -- mmap_sem
16090 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16091 + * - in mmap case
16092 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16093 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16094 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16095 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16096 + * It means that when aufs acquires si_rwsem for write, the process should never
16097 + * acquire mmap_sem.
16098 + *
16099 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16100 + * problem either since any directory is not able to be mmap-ed.
16101 + * The similar scenario is applied to aufs_readlink() too.
16102 + */
16103 +
16104 +#if 0 /* stop calling security_file_mmap() */
16105 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16106 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16107 +
16108 +static unsigned long au_arch_prot_conv(unsigned long flags)
16109 +{
16110 +       /* currently ppc64 only */
16111 +#ifdef CONFIG_PPC64
16112 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16113 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16114 +       return AuConv_VM_PROT(flags, SAO);
16115 +#else
16116 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16117 +       return 0;
16118 +#endif
16119 +}
16120 +
16121 +static unsigned long au_prot_conv(unsigned long flags)
16122 +{
16123 +       return AuConv_VM_PROT(flags, READ)
16124 +               | AuConv_VM_PROT(flags, WRITE)
16125 +               | AuConv_VM_PROT(flags, EXEC)
16126 +               | au_arch_prot_conv(flags);
16127 +}
16128 +
16129 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16130 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16131 +
16132 +static unsigned long au_flag_conv(unsigned long flags)
16133 +{
16134 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16135 +               | AuConv_VM_MAP(flags, DENYWRITE)
16136 +               | AuConv_VM_MAP(flags, LOCKED);
16137 +}
16138 +#endif
16139 +
16140 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16141 +{
16142 +       int err;
16143 +       const unsigned char wlock
16144 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16145 +       struct super_block *sb;
16146 +       struct file *h_file;
16147 +       struct inode *inode;
16148 +
16149 +       AuDbgVmRegion(file, vma);
16150 +
16151 +       inode = file_inode(file);
16152 +       sb = inode->i_sb;
16153 +       lockdep_off();
16154 +       si_read_lock(sb, AuLock_NOPLMW);
16155 +
16156 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16157 +       lockdep_on();
16158 +       err = PTR_ERR(h_file);
16159 +       if (IS_ERR(h_file))
16160 +               goto out;
16161 +
16162 +       err = 0;
16163 +       au_set_mmapped(file);
16164 +       au_vm_file_reset(vma, h_file);
16165 +       /*
16166 +        * we cannot call security_mmap_file() here since it may acquire
16167 +        * mmap_sem or i_mutex.
16168 +        *
16169 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16170 +        *                       au_flag_conv(vma->vm_flags));
16171 +        */
16172 +       if (!err)
16173 +               err = call_mmap(h_file, vma);
16174 +       if (!err) {
16175 +               au_vm_prfile_set(vma, file);
16176 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16177 +               goto out_fput; /* success */
16178 +       }
16179 +       au_unset_mmapped(file);
16180 +       au_vm_file_reset(vma, file);
16181 +
16182 +out_fput:
16183 +       lockdep_off();
16184 +       ii_write_unlock(inode);
16185 +       lockdep_on();
16186 +       fput(h_file);
16187 +out:
16188 +       lockdep_off();
16189 +       si_read_unlock(sb);
16190 +       lockdep_on();
16191 +       AuTraceErr(err);
16192 +       return err;
16193 +}
16194 +
16195 +/* ---------------------------------------------------------------------- */
16196 +
16197 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16198 +                            int datasync)
16199 +{
16200 +       int err;
16201 +       struct au_write_pre wpre;
16202 +       struct inode *inode;
16203 +       struct file *h_file;
16204 +
16205 +       err = 0; /* -EBADF; */ /* posix? */
16206 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16207 +               goto out;
16208 +
16209 +       inode = file_inode(file);
16210 +       au_mtx_and_read_lock(inode);
16211 +
16212 +       wpre.lsc = 0;
16213 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16214 +       err = PTR_ERR(h_file);
16215 +       if (IS_ERR(h_file))
16216 +               goto out_unlock;
16217 +
16218 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16219 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16220 +
16221 +out_unlock:
16222 +       si_read_unlock(inode->i_sb);
16223 +       inode_unlock(inode);
16224 +out:
16225 +       return err;
16226 +}
16227 +
16228 +static int aufs_fasync(int fd, struct file *file, int flag)
16229 +{
16230 +       int err;
16231 +       struct file *h_file;
16232 +       struct super_block *sb;
16233 +
16234 +       sb = file->f_path.dentry->d_sb;
16235 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16236 +
16237 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16238 +       err = PTR_ERR(h_file);
16239 +       if (IS_ERR(h_file))
16240 +               goto out;
16241 +
16242 +       if (h_file->f_op->fasync)
16243 +               err = h_file->f_op->fasync(fd, h_file, flag);
16244 +       fput(h_file); /* instead of au_read_post() */
16245 +
16246 +out:
16247 +       si_read_unlock(sb);
16248 +       return err;
16249 +}
16250 +
16251 +static int aufs_setfl(struct file *file, unsigned long arg)
16252 +{
16253 +       int err;
16254 +       struct file *h_file;
16255 +       struct super_block *sb;
16256 +
16257 +       sb = file->f_path.dentry->d_sb;
16258 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16259 +
16260 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16261 +       err = PTR_ERR(h_file);
16262 +       if (IS_ERR(h_file))
16263 +               goto out;
16264 +
16265 +       /* stop calling h_file->fasync */
16266 +       arg |= vfsub_file_flags(file) & FASYNC;
16267 +       err = setfl(/*unused fd*/-1, h_file, arg);
16268 +       fput(h_file); /* instead of au_read_post() */
16269 +
16270 +out:
16271 +       si_read_unlock(sb);
16272 +       return err;
16273 +}
16274 +
16275 +/* ---------------------------------------------------------------------- */
16276 +
16277 +/* no one supports this operation, currently */
16278 +#if 0 /* reserved for future use */
16279 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16280 +                            size_t len, loff_t *pos, int more)
16281 +{
16282 +}
16283 +#endif
16284 +
16285 +/* ---------------------------------------------------------------------- */
16286 +
16287 +const struct file_operations aufs_file_fop = {
16288 +       .owner          = THIS_MODULE,
16289 +
16290 +       .llseek         = default_llseek,
16291 +
16292 +       .read           = aufs_read,
16293 +       .write          = aufs_write,
16294 +       .read_iter      = aufs_read_iter,
16295 +       .write_iter     = aufs_write_iter,
16296 +
16297 +#ifdef CONFIG_AUFS_POLL
16298 +       .poll           = aufs_poll,
16299 +#endif
16300 +       .unlocked_ioctl = aufs_ioctl_nondir,
16301 +#ifdef CONFIG_COMPAT
16302 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16303 +#endif
16304 +       .mmap           = aufs_mmap,
16305 +       .open           = aufs_open_nondir,
16306 +       .flush          = aufs_flush_nondir,
16307 +       .release        = aufs_release_nondir,
16308 +       .fsync          = aufs_fsync_nondir,
16309 +       .fasync         = aufs_fasync,
16310 +       /* .sendpage    = aufs_sendpage, */
16311 +       .setfl          = aufs_setfl,
16312 +       .splice_write   = aufs_splice_write,
16313 +       .splice_read    = aufs_splice_read,
16314 +#if 0 /* reserved for future use */
16315 +       .aio_splice_write = aufs_aio_splice_write,
16316 +       .aio_splice_read  = aufs_aio_splice_read,
16317 +#endif
16318 +       .fallocate      = aufs_fallocate,
16319 +       .copy_file_range = aufs_copy_file_range
16320 +};
16321 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16322 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16323 +++ linux/fs/aufs/fstype.h      2020-01-27 10:57:18.172204883 +0100
16324 @@ -0,0 +1,401 @@
16325 +/* SPDX-License-Identifier: GPL-2.0 */
16326 +/*
16327 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16328 + *
16329 + * This program, aufs is free software; you can redistribute it and/or modify
16330 + * it under the terms of the GNU General Public License as published by
16331 + * the Free Software Foundation; either version 2 of the License, or
16332 + * (at your option) any later version.
16333 + *
16334 + * This program is distributed in the hope that it will be useful,
16335 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16336 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16337 + * GNU General Public License for more details.
16338 + *
16339 + * You should have received a copy of the GNU General Public License
16340 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16341 + */
16342 +
16343 +/*
16344 + * judging filesystem type
16345 + */
16346 +
16347 +#ifndef __AUFS_FSTYPE_H__
16348 +#define __AUFS_FSTYPE_H__
16349 +
16350 +#ifdef __KERNEL__
16351 +
16352 +#include <linux/fs.h>
16353 +#include <linux/magic.h>
16354 +#include <linux/nfs_fs.h>
16355 +#include <linux/romfs_fs.h>
16356 +
16357 +static inline int au_test_aufs(struct super_block *sb)
16358 +{
16359 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16360 +}
16361 +
16362 +static inline const char *au_sbtype(struct super_block *sb)
16363 +{
16364 +       return sb->s_type->name;
16365 +}
16366 +
16367 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16368 +{
16369 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16370 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16371 +#else
16372 +       return 0;
16373 +#endif
16374 +}
16375 +
16376 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16377 +{
16378 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16379 +       return sb->s_magic == ROMFS_MAGIC;
16380 +#else
16381 +       return 0;
16382 +#endif
16383 +}
16384 +
16385 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16386 +{
16387 +#if IS_ENABLED(CONFIG_CRAMFS)
16388 +       return sb->s_magic == CRAMFS_MAGIC;
16389 +#endif
16390 +       return 0;
16391 +}
16392 +
16393 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16394 +{
16395 +#if IS_ENABLED(CONFIG_NFS_FS)
16396 +       return sb->s_magic == NFS_SUPER_MAGIC;
16397 +#else
16398 +       return 0;
16399 +#endif
16400 +}
16401 +
16402 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16403 +{
16404 +#if IS_ENABLED(CONFIG_FUSE_FS)
16405 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16406 +#else
16407 +       return 0;
16408 +#endif
16409 +}
16410 +
16411 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16412 +{
16413 +#if IS_ENABLED(CONFIG_XFS_FS)
16414 +       return sb->s_magic == XFS_SB_MAGIC;
16415 +#else
16416 +       return 0;
16417 +#endif
16418 +}
16419 +
16420 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16421 +{
16422 +#ifdef CONFIG_TMPFS
16423 +       return sb->s_magic == TMPFS_MAGIC;
16424 +#else
16425 +       return 0;
16426 +#endif
16427 +}
16428 +
16429 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16430 +{
16431 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16432 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16433 +#else
16434 +       return 0;
16435 +#endif
16436 +}
16437 +
16438 +static inline int au_test_ramfs(struct super_block *sb)
16439 +{
16440 +       return sb->s_magic == RAMFS_MAGIC;
16441 +}
16442 +
16443 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16444 +{
16445 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16446 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16447 +#else
16448 +       return 0;
16449 +#endif
16450 +}
16451 +
16452 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16453 +{
16454 +#ifdef CONFIG_PROC_FS
16455 +       return sb->s_magic == PROC_SUPER_MAGIC;
16456 +#else
16457 +       return 0;
16458 +#endif
16459 +}
16460 +
16461 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16462 +{
16463 +#ifdef CONFIG_SYSFS
16464 +       return sb->s_magic == SYSFS_MAGIC;
16465 +#else
16466 +       return 0;
16467 +#endif
16468 +}
16469 +
16470 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16471 +{
16472 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16473 +       return sb->s_magic == CONFIGFS_MAGIC;
16474 +#else
16475 +       return 0;
16476 +#endif
16477 +}
16478 +
16479 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16480 +{
16481 +#if IS_ENABLED(CONFIG_MINIX_FS)
16482 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16483 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16484 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16485 +               || sb->s_magic == MINIX_SUPER_MAGIC
16486 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16487 +#else
16488 +       return 0;
16489 +#endif
16490 +}
16491 +
16492 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16493 +{
16494 +#if IS_ENABLED(CONFIG_FAT_FS)
16495 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16496 +#else
16497 +       return 0;
16498 +#endif
16499 +}
16500 +
16501 +static inline int au_test_msdos(struct super_block *sb)
16502 +{
16503 +       return au_test_fat(sb);
16504 +}
16505 +
16506 +static inline int au_test_vfat(struct super_block *sb)
16507 +{
16508 +       return au_test_fat(sb);
16509 +}
16510 +
16511 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16512 +{
16513 +#ifdef CONFIG_SECURITYFS
16514 +       return sb->s_magic == SECURITYFS_MAGIC;
16515 +#else
16516 +       return 0;
16517 +#endif
16518 +}
16519 +
16520 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16521 +{
16522 +#if IS_ENABLED(CONFIG_SQUASHFS)
16523 +       return sb->s_magic == SQUASHFS_MAGIC;
16524 +#else
16525 +       return 0;
16526 +#endif
16527 +}
16528 +
16529 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16530 +{
16531 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16532 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16533 +#else
16534 +       return 0;
16535 +#endif
16536 +}
16537 +
16538 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16539 +{
16540 +#if IS_ENABLED(CONFIG_XENFS)
16541 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16542 +#else
16543 +       return 0;
16544 +#endif
16545 +}
16546 +
16547 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16548 +{
16549 +#ifdef CONFIG_DEBUG_FS
16550 +       return sb->s_magic == DEBUGFS_MAGIC;
16551 +#else
16552 +       return 0;
16553 +#endif
16554 +}
16555 +
16556 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16557 +{
16558 +#if IS_ENABLED(CONFIG_NILFS)
16559 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16560 +#else
16561 +       return 0;
16562 +#endif
16563 +}
16564 +
16565 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16566 +{
16567 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16568 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16569 +#else
16570 +       return 0;
16571 +#endif
16572 +}
16573 +
16574 +/* ---------------------------------------------------------------------- */
16575 +/*
16576 + * they can't be an aufs branch.
16577 + */
16578 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16579 +{
16580 +       return
16581 +#ifndef CONFIG_AUFS_BR_RAMFS
16582 +               au_test_ramfs(sb) ||
16583 +#endif
16584 +               au_test_procfs(sb)
16585 +               || au_test_sysfs(sb)
16586 +               || au_test_configfs(sb)
16587 +               || au_test_debugfs(sb)
16588 +               || au_test_securityfs(sb)
16589 +               || au_test_xenfs(sb)
16590 +               || au_test_ecryptfs(sb)
16591 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16592 +               || au_test_aufs(sb); /* will be supported in next version */
16593 +}
16594 +
16595 +static inline int au_test_fs_remote(struct super_block *sb)
16596 +{
16597 +       return !au_test_tmpfs(sb)
16598 +#ifdef CONFIG_AUFS_BR_RAMFS
16599 +               && !au_test_ramfs(sb)
16600 +#endif
16601 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16602 +}
16603 +
16604 +/* ---------------------------------------------------------------------- */
16605 +
16606 +/*
16607 + * Note: these functions (below) are created after reading ->getattr() in all
16608 + * filesystems under linux/fs. it means we have to do so in every update...
16609 + */
16610 +
16611 +/*
16612 + * some filesystems require getattr to refresh the inode attributes before
16613 + * referencing.
16614 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16615 + * and leave the work for d_revalidate()
16616 + */
16617 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16618 +{
16619 +       return au_test_nfs(sb)
16620 +               || au_test_fuse(sb)
16621 +               /* || au_test_btrfs(sb) */      /* untested */
16622 +               ;
16623 +}
16624 +
16625 +/*
16626 + * filesystems which don't maintain i_size or i_blocks.
16627 + */
16628 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16629 +{
16630 +       return au_test_xfs(sb)
16631 +               || au_test_btrfs(sb)
16632 +               || au_test_ubifs(sb)
16633 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16634 +               /* || au_test_minix(sb) */      /* untested */
16635 +               ;
16636 +}
16637 +
16638 +/*
16639 + * filesystems which don't store the correct value in some of their inode
16640 + * attributes.
16641 + */
16642 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16643 +{
16644 +       return au_test_fs_bad_iattr_size(sb)
16645 +               || au_test_fat(sb)
16646 +               || au_test_msdos(sb)
16647 +               || au_test_vfat(sb);
16648 +}
16649 +
16650 +/* they don't check i_nlink in link(2) */
16651 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16652 +{
16653 +       return au_test_tmpfs(sb)
16654 +#ifdef CONFIG_AUFS_BR_RAMFS
16655 +               || au_test_ramfs(sb)
16656 +#endif
16657 +               || au_test_ubifs(sb)
16658 +               || au_test_hfsplus(sb);
16659 +}
16660 +
16661 +/*
16662 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16663 + */
16664 +static inline int au_test_fs_notime(struct super_block *sb)
16665 +{
16666 +       return au_test_nfs(sb)
16667 +               || au_test_fuse(sb)
16668 +               || au_test_ubifs(sb)
16669 +               ;
16670 +}
16671 +
16672 +/* temporary support for i#1 in cramfs */
16673 +static inline int au_test_fs_unique_ino(struct inode *inode)
16674 +{
16675 +       if (au_test_cramfs(inode->i_sb))
16676 +               return inode->i_ino != 1;
16677 +       return 1;
16678 +}
16679 +
16680 +/* ---------------------------------------------------------------------- */
16681 +
16682 +/*
16683 + * the filesystem where the xino files placed must support i/o after unlink and
16684 + * maintain i_size and i_blocks.
16685 + */
16686 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16687 +{
16688 +       return au_test_fs_remote(sb)
16689 +               || au_test_fs_bad_iattr_size(sb)
16690 +               /* don't want unnecessary work for xino */
16691 +               || au_test_aufs(sb)
16692 +               || au_test_ecryptfs(sb)
16693 +               || au_test_nilfs(sb);
16694 +}
16695 +
16696 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16697 +{
16698 +       return au_test_tmpfs(sb)
16699 +               || au_test_ramfs(sb);
16700 +}
16701 +
16702 +/*
16703 + * test if the @sb is real-readonly.
16704 + */
16705 +static inline int au_test_fs_rr(struct super_block *sb)
16706 +{
16707 +       return au_test_squashfs(sb)
16708 +               || au_test_iso9660(sb)
16709 +               || au_test_cramfs(sb)
16710 +               || au_test_romfs(sb);
16711 +}
16712 +
16713 +/*
16714 + * test if the @inode is nfs with 'noacl' option
16715 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16716 + */
16717 +static inline int au_test_nfs_noacl(struct inode *inode)
16718 +{
16719 +       return au_test_nfs(inode->i_sb)
16720 +               /* && IS_POSIXACL(inode) */
16721 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16722 +}
16723 +
16724 +#endif /* __KERNEL__ */
16725 +#endif /* __AUFS_FSTYPE_H__ */
16726 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16727 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16728 +++ linux/fs/aufs/hbl.h 2020-01-27 10:57:18.172204883 +0100
16729 @@ -0,0 +1,65 @@
16730 +/* SPDX-License-Identifier: GPL-2.0 */
16731 +/*
16732 + * Copyright (C) 2017-2020 Junjiro R. Okajima
16733 + *
16734 + * This program, aufs is free software; you can redistribute it and/or modify
16735 + * it under the terms of the GNU General Public License as published by
16736 + * the Free Software Foundation; either version 2 of the License, or
16737 + * (at your option) any later version.
16738 + *
16739 + * This program is distributed in the hope that it will be useful,
16740 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16741 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16742 + * GNU General Public License for more details.
16743 + *
16744 + * You should have received a copy of the GNU General Public License
16745 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16746 + */
16747 +
16748 +/*
16749 + * helpers for hlist_bl.h
16750 + */
16751 +
16752 +#ifndef __AUFS_HBL_H__
16753 +#define __AUFS_HBL_H__
16754 +
16755 +#ifdef __KERNEL__
16756 +
16757 +#include <linux/list_bl.h>
16758 +
16759 +static inline void au_hbl_add(struct hlist_bl_node *node,
16760 +                             struct hlist_bl_head *hbl)
16761 +{
16762 +       hlist_bl_lock(hbl);
16763 +       hlist_bl_add_head(node, hbl);
16764 +       hlist_bl_unlock(hbl);
16765 +}
16766 +
16767 +static inline void au_hbl_del(struct hlist_bl_node *node,
16768 +                             struct hlist_bl_head *hbl)
16769 +{
16770 +       hlist_bl_lock(hbl);
16771 +       hlist_bl_del(node);
16772 +       hlist_bl_unlock(hbl);
16773 +}
16774 +
16775 +#define au_hbl_for_each(pos, head)                                     \
16776 +       for (pos = hlist_bl_first(head);                                \
16777 +            pos;                                                       \
16778 +            pos = pos->next)
16779 +
16780 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16781 +{
16782 +       unsigned long cnt;
16783 +       struct hlist_bl_node *pos;
16784 +
16785 +       cnt = 0;
16786 +       hlist_bl_lock(hbl);
16787 +       au_hbl_for_each(pos, hbl)
16788 +               cnt++;
16789 +       hlist_bl_unlock(hbl);
16790 +       return cnt;
16791 +}
16792 +
16793 +#endif /* __KERNEL__ */
16794 +#endif /* __AUFS_HBL_H__ */
16795 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16796 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16797 +++ linux/fs/aufs/hfsnotify.c   2020-01-27 10:57:18.172204883 +0100
16798 @@ -0,0 +1,288 @@
16799 +// SPDX-License-Identifier: GPL-2.0
16800 +/*
16801 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16802 + *
16803 + * This program, aufs is free software; you can redistribute it and/or modify
16804 + * it under the terms of the GNU General Public License as published by
16805 + * the Free Software Foundation; either version 2 of the License, or
16806 + * (at your option) any later version.
16807 + *
16808 + * This program is distributed in the hope that it will be useful,
16809 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16810 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16811 + * GNU General Public License for more details.
16812 + *
16813 + * You should have received a copy of the GNU General Public License
16814 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16815 + */
16816 +
16817 +/*
16818 + * fsnotify for the lower directories
16819 + */
16820 +
16821 +#include "aufs.h"
16822 +
16823 +/* FS_IN_IGNORED is unnecessary */
16824 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16825 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16826 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16827 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16828 +
16829 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16830 +{
16831 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16832 +                                            hn_mark);
16833 +       /* AuDbg("here\n"); */
16834 +       au_cache_free_hnotify(hn);
16835 +       smp_mb__before_atomic(); /* for atomic64_dec */
16836 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16837 +               wake_up(&au_hfsn_wq);
16838 +}
16839 +
16840 +static int au_hfsn_alloc(struct au_hinode *hinode)
16841 +{
16842 +       int err;
16843 +       struct au_hnotify *hn;
16844 +       struct super_block *sb;
16845 +       struct au_branch *br;
16846 +       struct fsnotify_mark *mark;
16847 +       aufs_bindex_t bindex;
16848 +
16849 +       hn = hinode->hi_notify;
16850 +       sb = hn->hn_aufs_inode->i_sb;
16851 +       bindex = au_br_index(sb, hinode->hi_id);
16852 +       br = au_sbr(sb, bindex);
16853 +       AuDebugOn(!br->br_hfsn);
16854 +
16855 +       mark = &hn->hn_mark;
16856 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16857 +       mark->mask = AuHfsnMask;
16858 +       /*
16859 +        * by udba rename or rmdir, aufs assign a new inode to the known
16860 +        * h_inode, so specify 1 to allow dups.
16861 +        */
16862 +       lockdep_off();
16863 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16864 +       lockdep_on();
16865 +
16866 +       return err;
16867 +}
16868 +
16869 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16870 +{
16871 +       struct fsnotify_mark *mark;
16872 +       unsigned long long ull;
16873 +       struct fsnotify_group *group;
16874 +
16875 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16876 +       BUG_ON(!ull);
16877 +
16878 +       mark = &hn->hn_mark;
16879 +       spin_lock(&mark->lock);
16880 +       group = mark->group;
16881 +       fsnotify_get_group(group);
16882 +       spin_unlock(&mark->lock);
16883 +       lockdep_off();
16884 +       fsnotify_destroy_mark(mark, group);
16885 +       fsnotify_put_mark(mark);
16886 +       fsnotify_put_group(group);
16887 +       lockdep_on();
16888 +
16889 +       /* free hn by myself */
16890 +       return 0;
16891 +}
16892 +
16893 +/* ---------------------------------------------------------------------- */
16894 +
16895 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16896 +{
16897 +       struct fsnotify_mark *mark;
16898 +
16899 +       mark = &hinode->hi_notify->hn_mark;
16900 +       spin_lock(&mark->lock);
16901 +       if (do_set) {
16902 +               AuDebugOn(mark->mask & AuHfsnMask);
16903 +               mark->mask |= AuHfsnMask;
16904 +       } else {
16905 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16906 +               mark->mask &= ~AuHfsnMask;
16907 +       }
16908 +       spin_unlock(&mark->lock);
16909 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16910 +}
16911 +
16912 +/* ---------------------------------------------------------------------- */
16913 +
16914 +/* #define AuDbgHnotify */
16915 +#ifdef AuDbgHnotify
16916 +static char *au_hfsn_name(u32 mask)
16917 +{
16918 +#ifdef CONFIG_AUFS_DEBUG
16919 +#define test_ret(flag)                         \
16920 +       do {                                    \
16921 +               if (mask & flag)                \
16922 +                       return #flag;           \
16923 +       } while (0)
16924 +       test_ret(FS_ACCESS);
16925 +       test_ret(FS_MODIFY);
16926 +       test_ret(FS_ATTRIB);
16927 +       test_ret(FS_CLOSE_WRITE);
16928 +       test_ret(FS_CLOSE_NOWRITE);
16929 +       test_ret(FS_OPEN);
16930 +       test_ret(FS_MOVED_FROM);
16931 +       test_ret(FS_MOVED_TO);
16932 +       test_ret(FS_CREATE);
16933 +       test_ret(FS_DELETE);
16934 +       test_ret(FS_DELETE_SELF);
16935 +       test_ret(FS_MOVE_SELF);
16936 +       test_ret(FS_UNMOUNT);
16937 +       test_ret(FS_Q_OVERFLOW);
16938 +       test_ret(FS_IN_IGNORED);
16939 +       test_ret(FS_ISDIR);
16940 +       test_ret(FS_IN_ONESHOT);
16941 +       test_ret(FS_EVENT_ON_CHILD);
16942 +       return "";
16943 +#undef test_ret
16944 +#else
16945 +       return "??";
16946 +#endif
16947 +}
16948 +#endif
16949 +
16950 +/* ---------------------------------------------------------------------- */
16951 +
16952 +static void au_hfsn_free_group(struct fsnotify_group *group)
16953 +{
16954 +       struct au_br_hfsnotify *hfsn = group->private;
16955 +
16956 +       /* AuDbg("here\n"); */
16957 +       au_kfree_try_rcu(hfsn);
16958 +}
16959 +
16960 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16961 +                               struct inode *inode,
16962 +                               u32 mask, const void *data, int data_type,
16963 +                               const struct qstr *file_name, u32 cookie,
16964 +                               struct fsnotify_iter_info *iter_info)
16965 +{
16966 +       int err;
16967 +       struct au_hnotify *hnotify;
16968 +       struct inode *h_dir, *h_inode;
16969 +       struct fsnotify_mark *inode_mark;
16970 +
16971 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16972 +
16973 +       err = 0;
16974 +       /* if FS_UNMOUNT happens, there must be another bug */
16975 +       AuDebugOn(mask & FS_UNMOUNT);
16976 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16977 +               goto out;
16978 +
16979 +       h_dir = inode;
16980 +       h_inode = NULL;
16981 +#ifdef AuDbgHnotify
16982 +       au_debug_on();
16983 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16984 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16985 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16986 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16987 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16988 +               /* WARN_ON(1); */
16989 +       }
16990 +       au_debug_off();
16991 +#endif
16992 +
16993 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16994 +       AuDebugOn(!inode_mark);
16995 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16996 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16997 +
16998 +out:
16999 +       return err;
17000 +}
17001 +
17002 +static struct fsnotify_ops au_hfsn_ops = {
17003 +       .handle_event           = au_hfsn_handle_event,
17004 +       .free_group_priv        = au_hfsn_free_group,
17005 +       .free_mark              = au_hfsn_free_mark
17006 +};
17007 +
17008 +/* ---------------------------------------------------------------------- */
17009 +
17010 +static void au_hfsn_fin_br(struct au_branch *br)
17011 +{
17012 +       struct au_br_hfsnotify *hfsn;
17013 +
17014 +       hfsn = br->br_hfsn;
17015 +       if (hfsn) {
17016 +               lockdep_off();
17017 +               fsnotify_put_group(hfsn->hfsn_group);
17018 +               lockdep_on();
17019 +       }
17020 +}
17021 +
17022 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17023 +{
17024 +       int err;
17025 +       struct fsnotify_group *group;
17026 +       struct au_br_hfsnotify *hfsn;
17027 +
17028 +       err = 0;
17029 +       br->br_hfsn = NULL;
17030 +       if (!au_br_hnotifyable(perm))
17031 +               goto out;
17032 +
17033 +       err = -ENOMEM;
17034 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17035 +       if (unlikely(!hfsn))
17036 +               goto out;
17037 +
17038 +       err = 0;
17039 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17040 +       if (IS_ERR(group)) {
17041 +               err = PTR_ERR(group);
17042 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17043 +               goto out_hfsn;
17044 +       }
17045 +
17046 +       group->private = hfsn;
17047 +       hfsn->hfsn_group = group;
17048 +       br->br_hfsn = hfsn;
17049 +       goto out; /* success */
17050 +
17051 +out_hfsn:
17052 +       au_kfree_try_rcu(hfsn);
17053 +out:
17054 +       return err;
17055 +}
17056 +
17057 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17058 +{
17059 +       int err;
17060 +
17061 +       err = 0;
17062 +       if (!br->br_hfsn)
17063 +               err = au_hfsn_init_br(br, perm);
17064 +
17065 +       return err;
17066 +}
17067 +
17068 +/* ---------------------------------------------------------------------- */
17069 +
17070 +static void au_hfsn_fin(void)
17071 +{
17072 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17073 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17074 +}
17075 +
17076 +const struct au_hnotify_op au_hnotify_op = {
17077 +       .ctl            = au_hfsn_ctl,
17078 +       .alloc          = au_hfsn_alloc,
17079 +       .free           = au_hfsn_free,
17080 +
17081 +       .fin            = au_hfsn_fin,
17082 +
17083 +       .reset_br       = au_hfsn_reset_br,
17084 +       .fin_br         = au_hfsn_fin_br,
17085 +       .init_br        = au_hfsn_init_br
17086 +};
17087 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17088 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17089 +++ linux/fs/aufs/hfsplus.c     2020-01-27 10:57:18.172204883 +0100
17090 @@ -0,0 +1,60 @@
17091 +// SPDX-License-Identifier: GPL-2.0
17092 +/*
17093 + * Copyright (C) 2010-2020 Junjiro R. Okajima
17094 + *
17095 + * This program, aufs is free software; you can redistribute it and/or modify
17096 + * it under the terms of the GNU General Public License as published by
17097 + * the Free Software Foundation; either version 2 of the License, or
17098 + * (at your option) any later version.
17099 + *
17100 + * This program is distributed in the hope that it will be useful,
17101 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17102 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17103 + * GNU General Public License for more details.
17104 + *
17105 + * You should have received a copy of the GNU General Public License
17106 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17107 + */
17108 +
17109 +/*
17110 + * special support for filesystems which acquires an inode mutex
17111 + * at final closing a file, eg, hfsplus.
17112 + *
17113 + * This trick is very simple and stupid, just to open the file before really
17114 + * necessary open to tell hfsplus that this is not the final closing.
17115 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17116 + * and au_h_open_post() after releasing it.
17117 + */
17118 +
17119 +#include "aufs.h"
17120 +
17121 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17122 +                          int force_wr)
17123 +{
17124 +       struct file *h_file;
17125 +       struct dentry *h_dentry;
17126 +
17127 +       h_dentry = au_h_dptr(dentry, bindex);
17128 +       AuDebugOn(!h_dentry);
17129 +       AuDebugOn(d_is_negative(h_dentry));
17130 +
17131 +       h_file = NULL;
17132 +       if (au_test_hfsplus(h_dentry->d_sb)
17133 +           && d_is_reg(h_dentry))
17134 +               h_file = au_h_open(dentry, bindex,
17135 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17136 +                                  /*file*/NULL, force_wr);
17137 +       return h_file;
17138 +}
17139 +
17140 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17141 +                   struct file *h_file)
17142 +{
17143 +       struct au_branch *br;
17144 +
17145 +       if (h_file) {
17146 +               fput(h_file);
17147 +               br = au_sbr(dentry->d_sb, bindex);
17148 +               au_lcnt_dec(&br->br_nfiles);
17149 +       }
17150 +}
17151 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17152 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17153 +++ linux/fs/aufs/hnotify.c     2020-01-27 10:57:18.172204883 +0100
17154 @@ -0,0 +1,715 @@
17155 +// SPDX-License-Identifier: GPL-2.0
17156 +/*
17157 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17158 + *
17159 + * This program, aufs is free software; you can redistribute it and/or modify
17160 + * it under the terms of the GNU General Public License as published by
17161 + * the Free Software Foundation; either version 2 of the License, or
17162 + * (at your option) any later version.
17163 + *
17164 + * This program is distributed in the hope that it will be useful,
17165 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17166 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17167 + * GNU General Public License for more details.
17168 + *
17169 + * You should have received a copy of the GNU General Public License
17170 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17171 + */
17172 +
17173 +/*
17174 + * abstraction to notify the direct changes on lower directories
17175 + */
17176 +
17177 +/* #include <linux/iversion.h> */
17178 +#include "aufs.h"
17179 +
17180 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17181 +{
17182 +       int err;
17183 +       struct au_hnotify *hn;
17184 +
17185 +       err = -ENOMEM;
17186 +       hn = au_cache_alloc_hnotify();
17187 +       if (hn) {
17188 +               hn->hn_aufs_inode = inode;
17189 +               hinode->hi_notify = hn;
17190 +               err = au_hnotify_op.alloc(hinode);
17191 +               AuTraceErr(err);
17192 +               if (unlikely(err)) {
17193 +                       hinode->hi_notify = NULL;
17194 +                       au_cache_free_hnotify(hn);
17195 +                       /*
17196 +                        * The upper dir was removed by udba, but the same named
17197 +                        * dir left. In this case, aufs assigns a new inode
17198 +                        * number and set the monitor again.
17199 +                        * For the lower dir, the old monitor is still left.
17200 +                        */
17201 +                       if (err == -EEXIST)
17202 +                               err = 0;
17203 +               }
17204 +       }
17205 +
17206 +       AuTraceErr(err);
17207 +       return err;
17208 +}
17209 +
17210 +void au_hn_free(struct au_hinode *hinode)
17211 +{
17212 +       struct au_hnotify *hn;
17213 +
17214 +       hn = hinode->hi_notify;
17215 +       if (hn) {
17216 +               hinode->hi_notify = NULL;
17217 +               if (au_hnotify_op.free(hinode, hn))
17218 +                       au_cache_free_hnotify(hn);
17219 +       }
17220 +}
17221 +
17222 +/* ---------------------------------------------------------------------- */
17223 +
17224 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17225 +{
17226 +       if (hinode->hi_notify)
17227 +               au_hnotify_op.ctl(hinode, do_set);
17228 +}
17229 +
17230 +void au_hn_reset(struct inode *inode, unsigned int flags)
17231 +{
17232 +       aufs_bindex_t bindex, bbot;
17233 +       struct inode *hi;
17234 +       struct dentry *iwhdentry;
17235 +
17236 +       bbot = au_ibbot(inode);
17237 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17238 +               hi = au_h_iptr(inode, bindex);
17239 +               if (!hi)
17240 +                       continue;
17241 +
17242 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17243 +               iwhdentry = au_hi_wh(inode, bindex);
17244 +               if (iwhdentry)
17245 +                       dget(iwhdentry);
17246 +               au_igrab(hi);
17247 +               au_set_h_iptr(inode, bindex, NULL, 0);
17248 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17249 +                             flags & ~AuHi_XINO);
17250 +               iput(hi);
17251 +               dput(iwhdentry);
17252 +               /* inode_unlock(hi); */
17253 +       }
17254 +}
17255 +
17256 +/* ---------------------------------------------------------------------- */
17257 +
17258 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17259 +{
17260 +       int err;
17261 +       aufs_bindex_t bindex, bbot, bfound, btop;
17262 +       struct inode *h_i;
17263 +
17264 +       err = 0;
17265 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17266 +               pr_warn("branch root dir was changed\n");
17267 +               goto out;
17268 +       }
17269 +
17270 +       bfound = -1;
17271 +       bbot = au_ibbot(inode);
17272 +       btop = au_ibtop(inode);
17273 +#if 0 /* reserved for future use */
17274 +       if (bindex == bbot) {
17275 +               /* keep this ino in rename case */
17276 +               goto out;
17277 +       }
17278 +#endif
17279 +       for (bindex = btop; bindex <= bbot; bindex++)
17280 +               if (au_h_iptr(inode, bindex) == h_inode) {
17281 +                       bfound = bindex;
17282 +                       break;
17283 +               }
17284 +       if (bfound < 0)
17285 +               goto out;
17286 +
17287 +       for (bindex = btop; bindex <= bbot; bindex++) {
17288 +               h_i = au_h_iptr(inode, bindex);
17289 +               if (!h_i)
17290 +                       continue;
17291 +
17292 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17293 +               /* ignore this error */
17294 +               /* bad action? */
17295 +       }
17296 +
17297 +       /* children inode number will be broken */
17298 +
17299 +out:
17300 +       AuTraceErr(err);
17301 +       return err;
17302 +}
17303 +
17304 +static int hn_gen_tree(struct dentry *dentry)
17305 +{
17306 +       int err, i, j, ndentry;
17307 +       struct au_dcsub_pages dpages;
17308 +       struct au_dpage *dpage;
17309 +       struct dentry **dentries;
17310 +
17311 +       err = au_dpages_init(&dpages, GFP_NOFS);
17312 +       if (unlikely(err))
17313 +               goto out;
17314 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17315 +       if (unlikely(err))
17316 +               goto out_dpages;
17317 +
17318 +       for (i = 0; i < dpages.ndpage; i++) {
17319 +               dpage = dpages.dpages + i;
17320 +               dentries = dpage->dentries;
17321 +               ndentry = dpage->ndentry;
17322 +               for (j = 0; j < ndentry; j++) {
17323 +                       struct dentry *d;
17324 +
17325 +                       d = dentries[j];
17326 +                       if (IS_ROOT(d))
17327 +                               continue;
17328 +
17329 +                       au_digen_dec(d);
17330 +                       if (d_really_is_positive(d))
17331 +                               /* todo: reset children xino?
17332 +                                  cached children only? */
17333 +                               au_iigen_dec(d_inode(d));
17334 +               }
17335 +       }
17336 +
17337 +out_dpages:
17338 +       au_dpages_free(&dpages);
17339 +out:
17340 +       return err;
17341 +}
17342 +
17343 +/*
17344 + * return 0 if processed.
17345 + */
17346 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17347 +                          const unsigned int isdir)
17348 +{
17349 +       int err;
17350 +       struct dentry *d;
17351 +       struct qstr *dname;
17352 +
17353 +       err = 1;
17354 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17355 +               pr_warn("branch root dir was changed\n");
17356 +               err = 0;
17357 +               goto out;
17358 +       }
17359 +
17360 +       if (!isdir) {
17361 +               AuDebugOn(!name);
17362 +               au_iigen_dec(inode);
17363 +               spin_lock(&inode->i_lock);
17364 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17365 +                       spin_lock(&d->d_lock);
17366 +                       dname = &d->d_name;
17367 +                       if (dname->len != nlen
17368 +                           && memcmp(dname->name, name, nlen)) {
17369 +                               spin_unlock(&d->d_lock);
17370 +                               continue;
17371 +                       }
17372 +                       err = 0;
17373 +                       au_digen_dec(d);
17374 +                       spin_unlock(&d->d_lock);
17375 +                       break;
17376 +               }
17377 +               spin_unlock(&inode->i_lock);
17378 +       } else {
17379 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17380 +               d = d_find_any_alias(inode);
17381 +               if (!d) {
17382 +                       au_iigen_dec(inode);
17383 +                       goto out;
17384 +               }
17385 +
17386 +               spin_lock(&d->d_lock);
17387 +               dname = &d->d_name;
17388 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17389 +                       spin_unlock(&d->d_lock);
17390 +                       err = hn_gen_tree(d);
17391 +                       spin_lock(&d->d_lock);
17392 +               }
17393 +               spin_unlock(&d->d_lock);
17394 +               dput(d);
17395 +       }
17396 +
17397 +out:
17398 +       AuTraceErr(err);
17399 +       return err;
17400 +}
17401 +
17402 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17403 +{
17404 +       int err;
17405 +
17406 +       if (IS_ROOT(dentry)) {
17407 +               pr_warn("branch root dir was changed\n");
17408 +               return 0;
17409 +       }
17410 +
17411 +       err = 0;
17412 +       if (!isdir) {
17413 +               au_digen_dec(dentry);
17414 +               if (d_really_is_positive(dentry))
17415 +                       au_iigen_dec(d_inode(dentry));
17416 +       } else {
17417 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17418 +               if (d_really_is_positive(dentry))
17419 +                       err = hn_gen_tree(dentry);
17420 +       }
17421 +
17422 +       AuTraceErr(err);
17423 +       return err;
17424 +}
17425 +
17426 +/* ---------------------------------------------------------------------- */
17427 +
17428 +/* hnotify job flags */
17429 +#define AuHnJob_XINO0          1
17430 +#define AuHnJob_GEN            (1 << 1)
17431 +#define AuHnJob_DIRENT         (1 << 2)
17432 +#define AuHnJob_ISDIR          (1 << 3)
17433 +#define AuHnJob_TRYXINO0       (1 << 4)
17434 +#define AuHnJob_MNTPNT         (1 << 5)
17435 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17436 +#define au_fset_hnjob(flags, name) \
17437 +       do { (flags) |= AuHnJob_##name; } while (0)
17438 +#define au_fclr_hnjob(flags, name) \
17439 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17440 +
17441 +enum {
17442 +       AuHn_CHILD,
17443 +       AuHn_PARENT,
17444 +       AuHnLast
17445 +};
17446 +
17447 +struct au_hnotify_args {
17448 +       struct inode *h_dir, *dir, *h_child_inode;
17449 +       u32 mask;
17450 +       unsigned int flags[AuHnLast];
17451 +       unsigned int h_child_nlen;
17452 +       char h_child_name[];
17453 +};
17454 +
17455 +struct hn_job_args {
17456 +       unsigned int flags;
17457 +       struct inode *inode, *h_inode, *dir, *h_dir;
17458 +       struct dentry *dentry;
17459 +       char *h_name;
17460 +       int h_nlen;
17461 +};
17462 +
17463 +static int hn_job(struct hn_job_args *a)
17464 +{
17465 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17466 +       int e;
17467 +
17468 +       /* reset xino */
17469 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17470 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17471 +
17472 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17473 +           && a->inode
17474 +           && a->h_inode) {
17475 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17476 +               if (!a->h_inode->i_nlink
17477 +                   && !(a->h_inode->i_state & I_LINKABLE))
17478 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17479 +               inode_unlock_shared(a->h_inode);
17480 +       }
17481 +
17482 +       /* make the generation obsolete */
17483 +       if (au_ftest_hnjob(a->flags, GEN)) {
17484 +               e = -1;
17485 +               if (a->inode)
17486 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17487 +                                             isdir);
17488 +               if (e && a->dentry)
17489 +                       hn_gen_by_name(a->dentry, isdir);
17490 +               /* ignore this error */
17491 +       }
17492 +
17493 +       /* make dir entries obsolete */
17494 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17495 +               struct au_vdir *vdir;
17496 +
17497 +               vdir = au_ivdir(a->inode);
17498 +               if (vdir)
17499 +                       vdir->vd_jiffy = 0;
17500 +               /* IMustLock(a->inode); */
17501 +               /* inode_inc_iversion(a->inode); */
17502 +       }
17503 +
17504 +       /* can do nothing but warn */
17505 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17506 +           && a->dentry
17507 +           && d_mountpoint(a->dentry))
17508 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17509 +
17510 +       return 0;
17511 +}
17512 +
17513 +/* ---------------------------------------------------------------------- */
17514 +
17515 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17516 +                                          struct inode *dir)
17517 +{
17518 +       struct dentry *dentry, *d, *parent;
17519 +       struct qstr *dname;
17520 +
17521 +       parent = d_find_any_alias(dir);
17522 +       if (!parent)
17523 +               return NULL;
17524 +
17525 +       dentry = NULL;
17526 +       spin_lock(&parent->d_lock);
17527 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17528 +               /* AuDbg("%pd\n", d); */
17529 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17530 +               dname = &d->d_name;
17531 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17532 +                       goto cont_unlock;
17533 +               if (au_di(d))
17534 +                       au_digen_dec(d);
17535 +               else
17536 +                       goto cont_unlock;
17537 +               if (au_dcount(d) > 0) {
17538 +                       dentry = dget_dlock(d);
17539 +                       spin_unlock(&d->d_lock);
17540 +                       break;
17541 +               }
17542 +
17543 +cont_unlock:
17544 +               spin_unlock(&d->d_lock);
17545 +       }
17546 +       spin_unlock(&parent->d_lock);
17547 +       dput(parent);
17548 +
17549 +       if (dentry)
17550 +               di_write_lock_child(dentry);
17551 +
17552 +       return dentry;
17553 +}
17554 +
17555 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17556 +                                        aufs_bindex_t bindex, ino_t h_ino)
17557 +{
17558 +       struct inode *inode;
17559 +       ino_t ino;
17560 +       int err;
17561 +
17562 +       inode = NULL;
17563 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17564 +       if (!err && ino)
17565 +               inode = ilookup(sb, ino);
17566 +       if (!inode)
17567 +               goto out;
17568 +
17569 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17570 +               pr_warn("wrong root branch\n");
17571 +               iput(inode);
17572 +               inode = NULL;
17573 +               goto out;
17574 +       }
17575 +
17576 +       ii_write_lock_child(inode);
17577 +
17578 +out:
17579 +       return inode;
17580 +}
17581 +
17582 +static void au_hn_bh(void *_args)
17583 +{
17584 +       struct au_hnotify_args *a = _args;
17585 +       struct super_block *sb;
17586 +       aufs_bindex_t bindex, bbot, bfound;
17587 +       unsigned char xino, try_iput;
17588 +       int err;
17589 +       struct inode *inode;
17590 +       ino_t h_ino;
17591 +       struct hn_job_args args;
17592 +       struct dentry *dentry;
17593 +       struct au_sbinfo *sbinfo;
17594 +
17595 +       AuDebugOn(!_args);
17596 +       AuDebugOn(!a->h_dir);
17597 +       AuDebugOn(!a->dir);
17598 +       AuDebugOn(!a->mask);
17599 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17600 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17601 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17602 +
17603 +       inode = NULL;
17604 +       dentry = NULL;
17605 +       /*
17606 +        * do not lock a->dir->i_mutex here
17607 +        * because of d_revalidate() may cause a deadlock.
17608 +        */
17609 +       sb = a->dir->i_sb;
17610 +       AuDebugOn(!sb);
17611 +       sbinfo = au_sbi(sb);
17612 +       AuDebugOn(!sbinfo);
17613 +       si_write_lock(sb, AuLock_NOPLMW);
17614 +
17615 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17616 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17617 +               case FS_MOVED_FROM:
17618 +               case FS_MOVED_TO:
17619 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17620 +                               "for the direct rename(2)\n");
17621 +               }
17622 +
17623 +       ii_read_lock_parent(a->dir);
17624 +       bfound = -1;
17625 +       bbot = au_ibbot(a->dir);
17626 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17627 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17628 +                       bfound = bindex;
17629 +                       break;
17630 +               }
17631 +       ii_read_unlock(a->dir);
17632 +       if (unlikely(bfound < 0))
17633 +               goto out;
17634 +
17635 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17636 +       h_ino = 0;
17637 +       if (a->h_child_inode)
17638 +               h_ino = a->h_child_inode->i_ino;
17639 +
17640 +       if (a->h_child_nlen
17641 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17642 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17643 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17644 +                                             a->dir);
17645 +       try_iput = 0;
17646 +       if (dentry && d_really_is_positive(dentry))
17647 +               inode = d_inode(dentry);
17648 +       if (xino && !inode && h_ino
17649 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17650 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17651 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17652 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17653 +               try_iput = 1;
17654 +       }
17655 +
17656 +       args.flags = a->flags[AuHn_CHILD];
17657 +       args.dentry = dentry;
17658 +       args.inode = inode;
17659 +       args.h_inode = a->h_child_inode;
17660 +       args.dir = a->dir;
17661 +       args.h_dir = a->h_dir;
17662 +       args.h_name = a->h_child_name;
17663 +       args.h_nlen = a->h_child_nlen;
17664 +       err = hn_job(&args);
17665 +       if (dentry) {
17666 +               if (au_di(dentry))
17667 +                       di_write_unlock(dentry);
17668 +               dput(dentry);
17669 +       }
17670 +       if (inode && try_iput) {
17671 +               ii_write_unlock(inode);
17672 +               iput(inode);
17673 +       }
17674 +
17675 +       ii_write_lock_parent(a->dir);
17676 +       args.flags = a->flags[AuHn_PARENT];
17677 +       args.dentry = NULL;
17678 +       args.inode = a->dir;
17679 +       args.h_inode = a->h_dir;
17680 +       args.dir = NULL;
17681 +       args.h_dir = NULL;
17682 +       args.h_name = NULL;
17683 +       args.h_nlen = 0;
17684 +       err = hn_job(&args);
17685 +       ii_write_unlock(a->dir);
17686 +
17687 +out:
17688 +       iput(a->h_child_inode);
17689 +       iput(a->h_dir);
17690 +       iput(a->dir);
17691 +       si_write_unlock(sb);
17692 +       au_nwt_done(&sbinfo->si_nowait);
17693 +       au_kfree_rcu(a);
17694 +}
17695 +
17696 +/* ---------------------------------------------------------------------- */
17697 +
17698 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17699 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17700 +{
17701 +       int err, len;
17702 +       unsigned int flags[AuHnLast], f;
17703 +       unsigned char isdir, isroot, wh;
17704 +       struct inode *dir;
17705 +       struct au_hnotify_args *args;
17706 +       char *p, *h_child_name;
17707 +
17708 +       err = 0;
17709 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17710 +       dir = igrab(hnotify->hn_aufs_inode);
17711 +       if (!dir)
17712 +               goto out;
17713 +
17714 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17715 +       wh = 0;
17716 +       h_child_name = (void *)h_child_qstr->name;
17717 +       len = h_child_qstr->len;
17718 +       if (h_child_name) {
17719 +               if (len > AUFS_WH_PFX_LEN
17720 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17721 +                       h_child_name += AUFS_WH_PFX_LEN;
17722 +                       len -= AUFS_WH_PFX_LEN;
17723 +                       wh = 1;
17724 +               }
17725 +       }
17726 +
17727 +       isdir = 0;
17728 +       if (h_child_inode)
17729 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17730 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17731 +       flags[AuHn_CHILD] = 0;
17732 +       if (isdir)
17733 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17734 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17735 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17736 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17737 +       case FS_MOVED_FROM:
17738 +       case FS_MOVED_TO:
17739 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17740 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17741 +               /*FALLTHROUGH*/
17742 +       case FS_CREATE:
17743 +               AuDebugOn(!h_child_name);
17744 +               break;
17745 +
17746 +       case FS_DELETE:
17747 +               /*
17748 +                * aufs never be able to get this child inode.
17749 +                * revalidation should be in d_revalidate()
17750 +                * by checking i_nlink, i_generation or d_unhashed().
17751 +                */
17752 +               AuDebugOn(!h_child_name);
17753 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17754 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17755 +               break;
17756 +
17757 +       default:
17758 +               AuDebugOn(1);
17759 +       }
17760 +
17761 +       if (wh)
17762 +               h_child_inode = NULL;
17763 +
17764 +       err = -ENOMEM;
17765 +       /* iput() and kfree() will be called in au_hnotify() */
17766 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17767 +       if (unlikely(!args)) {
17768 +               AuErr1("no memory\n");
17769 +               iput(dir);
17770 +               goto out;
17771 +       }
17772 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17773 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17774 +       args->mask = mask;
17775 +       args->dir = dir;
17776 +       args->h_dir = igrab(h_dir);
17777 +       if (h_child_inode)
17778 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17779 +       args->h_child_inode = h_child_inode;
17780 +       args->h_child_nlen = len;
17781 +       if (len) {
17782 +               p = (void *)args;
17783 +               p += sizeof(*args);
17784 +               memcpy(p, h_child_name, len);
17785 +               p[len] = 0;
17786 +       }
17787 +
17788 +       /* NFS fires the event for silly-renamed one from kworker */
17789 +       f = 0;
17790 +       if (!dir->i_nlink
17791 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17792 +               f = AuWkq_NEST;
17793 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17794 +       if (unlikely(err)) {
17795 +               pr_err("wkq %d\n", err);
17796 +               iput(args->h_child_inode);
17797 +               iput(args->h_dir);
17798 +               iput(args->dir);
17799 +               au_kfree_rcu(args);
17800 +       }
17801 +
17802 +out:
17803 +       return err;
17804 +}
17805 +
17806 +/* ---------------------------------------------------------------------- */
17807 +
17808 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17809 +{
17810 +       int err;
17811 +
17812 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17813 +
17814 +       err = 0;
17815 +       if (au_hnotify_op.reset_br)
17816 +               err = au_hnotify_op.reset_br(udba, br, perm);
17817 +
17818 +       return err;
17819 +}
17820 +
17821 +int au_hnotify_init_br(struct au_branch *br, int perm)
17822 +{
17823 +       int err;
17824 +
17825 +       err = 0;
17826 +       if (au_hnotify_op.init_br)
17827 +               err = au_hnotify_op.init_br(br, perm);
17828 +
17829 +       return err;
17830 +}
17831 +
17832 +void au_hnotify_fin_br(struct au_branch *br)
17833 +{
17834 +       if (au_hnotify_op.fin_br)
17835 +               au_hnotify_op.fin_br(br);
17836 +}
17837 +
17838 +static void au_hn_destroy_cache(void)
17839 +{
17840 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17841 +       au_cache[AuCache_HNOTIFY] = NULL;
17842 +}
17843 +
17844 +int __init au_hnotify_init(void)
17845 +{
17846 +       int err;
17847 +
17848 +       err = -ENOMEM;
17849 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17850 +       if (au_cache[AuCache_HNOTIFY]) {
17851 +               err = 0;
17852 +               if (au_hnotify_op.init)
17853 +                       err = au_hnotify_op.init();
17854 +               if (unlikely(err))
17855 +                       au_hn_destroy_cache();
17856 +       }
17857 +       AuTraceErr(err);
17858 +       return err;
17859 +}
17860 +
17861 +void au_hnotify_fin(void)
17862 +{
17863 +       if (au_hnotify_op.fin)
17864 +               au_hnotify_op.fin();
17865 +
17866 +       /* cf. au_cache_fin() */
17867 +       if (au_cache[AuCache_HNOTIFY])
17868 +               au_hn_destroy_cache();
17869 +}
17870 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17871 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17872 +++ linux/fs/aufs/iinfo.c       2020-01-27 10:57:18.175538316 +0100
17873 @@ -0,0 +1,286 @@
17874 +// SPDX-License-Identifier: GPL-2.0
17875 +/*
17876 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17877 + *
17878 + * This program, aufs is free software; you can redistribute it and/or modify
17879 + * it under the terms of the GNU General Public License as published by
17880 + * the Free Software Foundation; either version 2 of the License, or
17881 + * (at your option) any later version.
17882 + *
17883 + * This program is distributed in the hope that it will be useful,
17884 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17885 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17886 + * GNU General Public License for more details.
17887 + *
17888 + * You should have received a copy of the GNU General Public License
17889 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17890 + */
17891 +
17892 +/*
17893 + * inode private data
17894 + */
17895 +
17896 +#include "aufs.h"
17897 +
17898 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17899 +{
17900 +       struct inode *h_inode;
17901 +       struct au_hinode *hinode;
17902 +
17903 +       IiMustAnyLock(inode);
17904 +
17905 +       hinode = au_hinode(au_ii(inode), bindex);
17906 +       h_inode = hinode->hi_inode;
17907 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17908 +       return h_inode;
17909 +}
17910 +
17911 +/* todo: hard/soft set? */
17912 +void au_hiput(struct au_hinode *hinode)
17913 +{
17914 +       au_hn_free(hinode);
17915 +       dput(hinode->hi_whdentry);
17916 +       iput(hinode->hi_inode);
17917 +}
17918 +
17919 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17920 +{
17921 +       unsigned int flags;
17922 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17923 +
17924 +       flags = 0;
17925 +       if (au_opt_test(mnt_flags, XINO))
17926 +               au_fset_hi(flags, XINO);
17927 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17928 +               au_fset_hi(flags, HNOTIFY);
17929 +       return flags;
17930 +}
17931 +
17932 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17933 +                  struct inode *h_inode, unsigned int flags)
17934 +{
17935 +       struct au_hinode *hinode;
17936 +       struct inode *hi;
17937 +       struct au_iinfo *iinfo = au_ii(inode);
17938 +
17939 +       IiMustWriteLock(inode);
17940 +
17941 +       hinode = au_hinode(iinfo, bindex);
17942 +       hi = hinode->hi_inode;
17943 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17944 +
17945 +       if (hi)
17946 +               au_hiput(hinode);
17947 +       hinode->hi_inode = h_inode;
17948 +       if (h_inode) {
17949 +               int err;
17950 +               struct super_block *sb = inode->i_sb;
17951 +               struct au_branch *br;
17952 +
17953 +               AuDebugOn(inode->i_mode
17954 +                         && (h_inode->i_mode & S_IFMT)
17955 +                         != (inode->i_mode & S_IFMT));
17956 +               if (bindex == iinfo->ii_btop)
17957 +                       au_cpup_igen(inode, h_inode);
17958 +               br = au_sbr(sb, bindex);
17959 +               hinode->hi_id = br->br_id;
17960 +               if (au_ftest_hi(flags, XINO)) {
17961 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17962 +                                           inode->i_ino);
17963 +                       if (unlikely(err))
17964 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17965 +               }
17966 +
17967 +               if (au_ftest_hi(flags, HNOTIFY)
17968 +                   && au_br_hnotifyable(br->br_perm)) {
17969 +                       err = au_hn_alloc(hinode, inode);
17970 +                       if (unlikely(err))
17971 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17972 +               }
17973 +       }
17974 +}
17975 +
17976 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17977 +                 struct dentry *h_wh)
17978 +{
17979 +       struct au_hinode *hinode;
17980 +
17981 +       IiMustWriteLock(inode);
17982 +
17983 +       hinode = au_hinode(au_ii(inode), bindex);
17984 +       AuDebugOn(hinode->hi_whdentry);
17985 +       hinode->hi_whdentry = h_wh;
17986 +}
17987 +
17988 +void au_update_iigen(struct inode *inode, int half)
17989 +{
17990 +       struct au_iinfo *iinfo;
17991 +       struct au_iigen *iigen;
17992 +       unsigned int sigen;
17993 +
17994 +       sigen = au_sigen(inode->i_sb);
17995 +       iinfo = au_ii(inode);
17996 +       iigen = &iinfo->ii_generation;
17997 +       spin_lock(&iigen->ig_spin);
17998 +       iigen->ig_generation = sigen;
17999 +       if (half)
18000 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18001 +       else
18002 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18003 +       spin_unlock(&iigen->ig_spin);
18004 +}
18005 +
18006 +/* it may be called at remount time, too */
18007 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18008 +{
18009 +       struct au_iinfo *iinfo;
18010 +       aufs_bindex_t bindex, bbot;
18011 +
18012 +       AuDebugOn(au_is_bad_inode(inode));
18013 +       IiMustWriteLock(inode);
18014 +
18015 +       iinfo = au_ii(inode);
18016 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18017 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18018 +                    bindex++) {
18019 +                       struct inode *h_i;
18020 +
18021 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18022 +                       if (h_i
18023 +                           && !h_i->i_nlink
18024 +                           && !(h_i->i_state & I_LINKABLE))
18025 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18026 +               }
18027 +       }
18028 +
18029 +       iinfo->ii_btop = -1;
18030 +       iinfo->ii_bbot = -1;
18031 +       bbot = au_sbbot(inode->i_sb);
18032 +       for (bindex = 0; bindex <= bbot; bindex++)
18033 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18034 +                       iinfo->ii_btop = bindex;
18035 +                       break;
18036 +               }
18037 +       if (iinfo->ii_btop >= 0)
18038 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18039 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18040 +                               iinfo->ii_bbot = bindex;
18041 +                               break;
18042 +                       }
18043 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18044 +}
18045 +
18046 +/* ---------------------------------------------------------------------- */
18047 +
18048 +void au_icntnr_init_once(void *_c)
18049 +{
18050 +       struct au_icntnr *c = _c;
18051 +       struct au_iinfo *iinfo = &c->iinfo;
18052 +
18053 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18054 +       au_rw_init(&iinfo->ii_rwsem);
18055 +       inode_init_once(&c->vfs_inode);
18056 +}
18057 +
18058 +void au_hinode_init(struct au_hinode *hinode)
18059 +{
18060 +       hinode->hi_inode = NULL;
18061 +       hinode->hi_id = -1;
18062 +       au_hn_init(hinode);
18063 +       hinode->hi_whdentry = NULL;
18064 +}
18065 +
18066 +int au_iinfo_init(struct inode *inode)
18067 +{
18068 +       struct au_iinfo *iinfo;
18069 +       struct super_block *sb;
18070 +       struct au_hinode *hi;
18071 +       int nbr, i;
18072 +
18073 +       sb = inode->i_sb;
18074 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18075 +       nbr = au_sbbot(sb) + 1;
18076 +       if (unlikely(nbr <= 0))
18077 +               nbr = 1;
18078 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18079 +       if (hi) {
18080 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
18081 +
18082 +               iinfo->ii_hinode = hi;
18083 +               for (i = 0; i < nbr; i++, hi++)
18084 +                       au_hinode_init(hi);
18085 +
18086 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18087 +               iinfo->ii_btop = -1;
18088 +               iinfo->ii_bbot = -1;
18089 +               iinfo->ii_vdir = NULL;
18090 +               return 0;
18091 +       }
18092 +       return -ENOMEM;
18093 +}
18094 +
18095 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18096 +{
18097 +       int err, i;
18098 +       struct au_hinode *hip;
18099 +
18100 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18101 +
18102 +       err = -ENOMEM;
18103 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18104 +                         may_shrink);
18105 +       if (hip) {
18106 +               iinfo->ii_hinode = hip;
18107 +               i = iinfo->ii_bbot + 1;
18108 +               hip += i;
18109 +               for (; i < nbr; i++, hip++)
18110 +                       au_hinode_init(hip);
18111 +               err = 0;
18112 +       }
18113 +
18114 +       return err;
18115 +}
18116 +
18117 +void au_iinfo_fin(struct inode *inode)
18118 +{
18119 +       struct au_iinfo *iinfo;
18120 +       struct au_hinode *hi;
18121 +       struct super_block *sb;
18122 +       aufs_bindex_t bindex, bbot;
18123 +       const unsigned char unlinked = !inode->i_nlink;
18124 +
18125 +       AuDebugOn(au_is_bad_inode(inode));
18126 +
18127 +       sb = inode->i_sb;
18128 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18129 +       if (si_pid_test(sb))
18130 +               au_xino_delete_inode(inode, unlinked);
18131 +       else {
18132 +               /*
18133 +                * it is safe to hide the dependency between sbinfo and
18134 +                * sb->s_umount.
18135 +                */
18136 +               lockdep_off();
18137 +               si_noflush_read_lock(sb);
18138 +               au_xino_delete_inode(inode, unlinked);
18139 +               si_read_unlock(sb);
18140 +               lockdep_on();
18141 +       }
18142 +
18143 +       iinfo = au_ii(inode);
18144 +       if (iinfo->ii_vdir)
18145 +               au_vdir_free(iinfo->ii_vdir);
18146 +
18147 +       bindex = iinfo->ii_btop;
18148 +       if (bindex >= 0) {
18149 +               hi = au_hinode(iinfo, bindex);
18150 +               bbot = iinfo->ii_bbot;
18151 +               while (bindex++ <= bbot) {
18152 +                       if (hi->hi_inode)
18153 +                               au_hiput(hi);
18154 +                       hi++;
18155 +               }
18156 +       }
18157 +       au_kfree_rcu(iinfo->ii_hinode);
18158 +       AuRwDestroy(&iinfo->ii_rwsem);
18159 +}
18160 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18161 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18162 +++ linux/fs/aufs/inode.c       2020-01-27 10:57:18.175538316 +0100
18163 @@ -0,0 +1,529 @@
18164 +// SPDX-License-Identifier: GPL-2.0
18165 +/*
18166 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18167 + *
18168 + * This program, aufs is free software; you can redistribute it and/or modify
18169 + * it under the terms of the GNU General Public License as published by
18170 + * the Free Software Foundation; either version 2 of the License, or
18171 + * (at your option) any later version.
18172 + *
18173 + * This program is distributed in the hope that it will be useful,
18174 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18175 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18176 + * GNU General Public License for more details.
18177 + *
18178 + * You should have received a copy of the GNU General Public License
18179 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18180 + */
18181 +
18182 +/*
18183 + * inode functions
18184 + */
18185 +
18186 +#include <linux/iversion.h>
18187 +#include "aufs.h"
18188 +
18189 +struct inode *au_igrab(struct inode *inode)
18190 +{
18191 +       if (inode) {
18192 +               AuDebugOn(!atomic_read(&inode->i_count));
18193 +               ihold(inode);
18194 +       }
18195 +       return inode;
18196 +}
18197 +
18198 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18199 +{
18200 +       au_cpup_attr_all(inode, /*force*/0);
18201 +       au_update_iigen(inode, /*half*/1);
18202 +       if (do_version)
18203 +               inode_inc_iversion(inode);
18204 +}
18205 +
18206 +static int au_ii_refresh(struct inode *inode, int *update)
18207 +{
18208 +       int err, e, nbr;
18209 +       umode_t type;
18210 +       aufs_bindex_t bindex, new_bindex;
18211 +       struct super_block *sb;
18212 +       struct au_iinfo *iinfo;
18213 +       struct au_hinode *p, *q, tmp;
18214 +
18215 +       AuDebugOn(au_is_bad_inode(inode));
18216 +       IiMustWriteLock(inode);
18217 +
18218 +       *update = 0;
18219 +       sb = inode->i_sb;
18220 +       nbr = au_sbbot(sb) + 1;
18221 +       type = inode->i_mode & S_IFMT;
18222 +       iinfo = au_ii(inode);
18223 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18224 +       if (unlikely(err))
18225 +               goto out;
18226 +
18227 +       AuDebugOn(iinfo->ii_btop < 0);
18228 +       p = au_hinode(iinfo, iinfo->ii_btop);
18229 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18230 +            bindex++, p++) {
18231 +               if (!p->hi_inode)
18232 +                       continue;
18233 +
18234 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18235 +               new_bindex = au_br_index(sb, p->hi_id);
18236 +               if (new_bindex == bindex)
18237 +                       continue;
18238 +
18239 +               if (new_bindex < 0) {
18240 +                       *update = 1;
18241 +                       au_hiput(p);
18242 +                       p->hi_inode = NULL;
18243 +                       continue;
18244 +               }
18245 +
18246 +               if (new_bindex < iinfo->ii_btop)
18247 +                       iinfo->ii_btop = new_bindex;
18248 +               if (iinfo->ii_bbot < new_bindex)
18249 +                       iinfo->ii_bbot = new_bindex;
18250 +               /* swap two lower inode, and loop again */
18251 +               q = au_hinode(iinfo, new_bindex);
18252 +               tmp = *q;
18253 +               *q = *p;
18254 +               *p = tmp;
18255 +               if (tmp.hi_inode) {
18256 +                       bindex--;
18257 +                       p--;
18258 +               }
18259 +       }
18260 +       au_update_ibrange(inode, /*do_put_zero*/0);
18261 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18262 +       e = au_dy_irefresh(inode);
18263 +       if (unlikely(e && !err))
18264 +               err = e;
18265 +
18266 +out:
18267 +       AuTraceErr(err);
18268 +       return err;
18269 +}
18270 +
18271 +void au_refresh_iop(struct inode *inode, int force_getattr)
18272 +{
18273 +       int type;
18274 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18275 +       const struct inode_operations *iop
18276 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18277 +
18278 +       if (inode->i_op == iop)
18279 +               return;
18280 +
18281 +       switch (inode->i_mode & S_IFMT) {
18282 +       case S_IFDIR:
18283 +               type = AuIop_DIR;
18284 +               break;
18285 +       case S_IFLNK:
18286 +               type = AuIop_SYMLINK;
18287 +               break;
18288 +       default:
18289 +               type = AuIop_OTHER;
18290 +               break;
18291 +       }
18292 +
18293 +       inode->i_op = iop + type;
18294 +       /* unnecessary smp_wmb() */
18295 +}
18296 +
18297 +int au_refresh_hinode_self(struct inode *inode)
18298 +{
18299 +       int err, update;
18300 +
18301 +       err = au_ii_refresh(inode, &update);
18302 +       if (!err)
18303 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18304 +
18305 +       AuTraceErr(err);
18306 +       return err;
18307 +}
18308 +
18309 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18310 +{
18311 +       int err, e, update;
18312 +       unsigned int flags;
18313 +       umode_t mode;
18314 +       aufs_bindex_t bindex, bbot;
18315 +       unsigned char isdir;
18316 +       struct au_hinode *p;
18317 +       struct au_iinfo *iinfo;
18318 +
18319 +       err = au_ii_refresh(inode, &update);
18320 +       if (unlikely(err))
18321 +               goto out;
18322 +
18323 +       update = 0;
18324 +       iinfo = au_ii(inode);
18325 +       p = au_hinode(iinfo, iinfo->ii_btop);
18326 +       mode = (inode->i_mode & S_IFMT);
18327 +       isdir = S_ISDIR(mode);
18328 +       flags = au_hi_flags(inode, isdir);
18329 +       bbot = au_dbbot(dentry);
18330 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18331 +               struct inode *h_i, *h_inode;
18332 +               struct dentry *h_d;
18333 +
18334 +               h_d = au_h_dptr(dentry, bindex);
18335 +               if (!h_d || d_is_negative(h_d))
18336 +                       continue;
18337 +
18338 +               h_inode = d_inode(h_d);
18339 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18340 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18341 +                       h_i = au_h_iptr(inode, bindex);
18342 +                       if (h_i) {
18343 +                               if (h_i == h_inode)
18344 +                                       continue;
18345 +                               err = -EIO;
18346 +                               break;
18347 +                       }
18348 +               }
18349 +               if (bindex < iinfo->ii_btop)
18350 +                       iinfo->ii_btop = bindex;
18351 +               if (iinfo->ii_bbot < bindex)
18352 +                       iinfo->ii_bbot = bindex;
18353 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18354 +               update = 1;
18355 +       }
18356 +       au_update_ibrange(inode, /*do_put_zero*/0);
18357 +       e = au_dy_irefresh(inode);
18358 +       if (unlikely(e && !err))
18359 +               err = e;
18360 +       if (!err)
18361 +               au_refresh_hinode_attr(inode, update && isdir);
18362 +
18363 +out:
18364 +       AuTraceErr(err);
18365 +       return err;
18366 +}
18367 +
18368 +static int set_inode(struct inode *inode, struct dentry *dentry)
18369 +{
18370 +       int err;
18371 +       unsigned int flags;
18372 +       umode_t mode;
18373 +       aufs_bindex_t bindex, btop, btail;
18374 +       unsigned char isdir;
18375 +       struct dentry *h_dentry;
18376 +       struct inode *h_inode;
18377 +       struct au_iinfo *iinfo;
18378 +       const struct inode_operations *iop;
18379 +
18380 +       IiMustWriteLock(inode);
18381 +
18382 +       err = 0;
18383 +       isdir = 0;
18384 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18385 +       btop = au_dbtop(dentry);
18386 +       h_dentry = au_h_dptr(dentry, btop);
18387 +       h_inode = d_inode(h_dentry);
18388 +       mode = h_inode->i_mode;
18389 +       switch (mode & S_IFMT) {
18390 +       case S_IFREG:
18391 +               btail = au_dbtail(dentry);
18392 +               inode->i_op = iop + AuIop_OTHER;
18393 +               inode->i_fop = &aufs_file_fop;
18394 +               err = au_dy_iaop(inode, btop, h_inode);
18395 +               if (unlikely(err))
18396 +                       goto out;
18397 +               break;
18398 +       case S_IFDIR:
18399 +               isdir = 1;
18400 +               btail = au_dbtaildir(dentry);
18401 +               inode->i_op = iop + AuIop_DIR;
18402 +               inode->i_fop = &aufs_dir_fop;
18403 +               break;
18404 +       case S_IFLNK:
18405 +               btail = au_dbtail(dentry);
18406 +               inode->i_op = iop + AuIop_SYMLINK;
18407 +               break;
18408 +       case S_IFBLK:
18409 +       case S_IFCHR:
18410 +       case S_IFIFO:
18411 +       case S_IFSOCK:
18412 +               btail = au_dbtail(dentry);
18413 +               inode->i_op = iop + AuIop_OTHER;
18414 +               init_special_inode(inode, mode, h_inode->i_rdev);
18415 +               break;
18416 +       default:
18417 +               AuIOErr("Unknown file type 0%o\n", mode);
18418 +               err = -EIO;
18419 +               goto out;
18420 +       }
18421 +
18422 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18423 +       flags = au_hi_flags(inode, isdir);
18424 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18425 +           && au_ftest_hi(flags, HNOTIFY)
18426 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18427 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18428 +               au_fclr_hi(flags, HNOTIFY);
18429 +       iinfo = au_ii(inode);
18430 +       iinfo->ii_btop = btop;
18431 +       iinfo->ii_bbot = btail;
18432 +       for (bindex = btop; bindex <= btail; bindex++) {
18433 +               h_dentry = au_h_dptr(dentry, bindex);
18434 +               if (h_dentry)
18435 +                       au_set_h_iptr(inode, bindex,
18436 +                                     au_igrab(d_inode(h_dentry)), flags);
18437 +       }
18438 +       au_cpup_attr_all(inode, /*force*/1);
18439 +       /*
18440 +        * to force calling aufs_get_acl() every time,
18441 +        * do not call cache_no_acl() for aufs inode.
18442 +        */
18443 +
18444 +out:
18445 +       return err;
18446 +}
18447 +
18448 +/*
18449 + * successful returns with iinfo write_locked
18450 + * minus: errno
18451 + * zero: success, matched
18452 + * plus: no error, but unmatched
18453 + */
18454 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18455 +{
18456 +       int err;
18457 +       unsigned int gen, igflags;
18458 +       aufs_bindex_t bindex, bbot;
18459 +       struct inode *h_inode, *h_dinode;
18460 +       struct dentry *h_dentry;
18461 +
18462 +       /*
18463 +        * before this function, if aufs got any iinfo lock, it must be only
18464 +        * one, the parent dir.
18465 +        * it can happen by UDBA and the obsoleted inode number.
18466 +        */
18467 +       err = -EIO;
18468 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18469 +               goto out;
18470 +
18471 +       err = 1;
18472 +       ii_write_lock_new_child(inode);
18473 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18474 +       h_dinode = d_inode(h_dentry);
18475 +       bbot = au_ibbot(inode);
18476 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18477 +               h_inode = au_h_iptr(inode, bindex);
18478 +               if (!h_inode || h_inode != h_dinode)
18479 +                       continue;
18480 +
18481 +               err = 0;
18482 +               gen = au_iigen(inode, &igflags);
18483 +               if (gen == au_digen(dentry)
18484 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18485 +                       break;
18486 +
18487 +               /* fully refresh inode using dentry */
18488 +               err = au_refresh_hinode(inode, dentry);
18489 +               if (!err)
18490 +                       au_update_iigen(inode, /*half*/0);
18491 +               break;
18492 +       }
18493 +
18494 +       if (unlikely(err))
18495 +               ii_write_unlock(inode);
18496 +out:
18497 +       return err;
18498 +}
18499 +
18500 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18501 +          unsigned int d_type, ino_t *ino)
18502 +{
18503 +       int err, idx;
18504 +       const int isnondir = d_type != DT_DIR;
18505 +
18506 +       /* prevent hardlinked inode number from race condition */
18507 +       if (isnondir) {
18508 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18509 +               if (unlikely(err))
18510 +                       goto out;
18511 +       }
18512 +
18513 +       err = au_xino_read(sb, bindex, h_ino, ino);
18514 +       if (unlikely(err))
18515 +               goto out_xinondir;
18516 +
18517 +       if (!*ino) {
18518 +               err = -EIO;
18519 +               *ino = au_xino_new_ino(sb);
18520 +               if (unlikely(!*ino))
18521 +                       goto out_xinondir;
18522 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18523 +               if (unlikely(err))
18524 +                       goto out_xinondir;
18525 +       }
18526 +
18527 +out_xinondir:
18528 +       if (isnondir && idx >= 0)
18529 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18530 +out:
18531 +       return err;
18532 +}
18533 +
18534 +/* successful returns with iinfo write_locked */
18535 +/* todo: return with unlocked? */
18536 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18537 +{
18538 +       struct inode *inode, *h_inode;
18539 +       struct dentry *h_dentry;
18540 +       struct super_block *sb;
18541 +       ino_t h_ino, ino;
18542 +       int err, idx, hlinked;
18543 +       aufs_bindex_t btop;
18544 +
18545 +       sb = dentry->d_sb;
18546 +       btop = au_dbtop(dentry);
18547 +       h_dentry = au_h_dptr(dentry, btop);
18548 +       h_inode = d_inode(h_dentry);
18549 +       h_ino = h_inode->i_ino;
18550 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18551 +
18552 +new_ino:
18553 +       /*
18554 +        * stop 'race'-ing between hardlinks under different
18555 +        * parents.
18556 +        */
18557 +       if (hlinked) {
18558 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18559 +               inode = ERR_PTR(err);
18560 +               if (unlikely(err))
18561 +                       goto out;
18562 +       }
18563 +
18564 +       err = au_xino_read(sb, btop, h_ino, &ino);
18565 +       inode = ERR_PTR(err);
18566 +       if (unlikely(err))
18567 +               goto out_xinondir;
18568 +
18569 +       if (!ino) {
18570 +               ino = au_xino_new_ino(sb);
18571 +               if (unlikely(!ino)) {
18572 +                       inode = ERR_PTR(-EIO);
18573 +                       goto out_xinondir;
18574 +               }
18575 +       }
18576 +
18577 +       AuDbg("i%lu\n", (unsigned long)ino);
18578 +       inode = au_iget_locked(sb, ino);
18579 +       err = PTR_ERR(inode);
18580 +       if (IS_ERR(inode))
18581 +               goto out_xinondir;
18582 +
18583 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18584 +       if (inode->i_state & I_NEW) {
18585 +               ii_write_lock_new_child(inode);
18586 +               err = set_inode(inode, dentry);
18587 +               if (!err) {
18588 +                       unlock_new_inode(inode);
18589 +                       goto out_xinondir; /* success */
18590 +               }
18591 +
18592 +               /*
18593 +                * iget_failed() calls iput(), but we need to call
18594 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18595 +                * i_count.
18596 +                */
18597 +               atomic_inc(&inode->i_count);
18598 +               iget_failed(inode);
18599 +               ii_write_unlock(inode);
18600 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18601 +               /* ignore this error */
18602 +               goto out_iput;
18603 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18604 +               /*
18605 +                * horrible race condition between lookup, readdir and copyup
18606 +                * (or something).
18607 +                */
18608 +               if (hlinked && idx >= 0)
18609 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18610 +               err = reval_inode(inode, dentry);
18611 +               if (unlikely(err < 0)) {
18612 +                       hlinked = 0;
18613 +                       goto out_iput;
18614 +               }
18615 +               if (!err)
18616 +                       goto out; /* success */
18617 +               else if (hlinked && idx >= 0) {
18618 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18619 +                       if (unlikely(err)) {
18620 +                               iput(inode);
18621 +                               inode = ERR_PTR(err);
18622 +                               goto out;
18623 +                       }
18624 +               }
18625 +       }
18626 +
18627 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18628 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18629 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18630 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18631 +                       (unsigned long)h_ino, (unsigned long)ino);
18632 +       ino = 0;
18633 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18634 +       if (!err) {
18635 +               iput(inode);
18636 +               if (hlinked && idx >= 0)
18637 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18638 +               goto new_ino;
18639 +       }
18640 +
18641 +out_iput:
18642 +       iput(inode);
18643 +       inode = ERR_PTR(err);
18644 +out_xinondir:
18645 +       if (hlinked && idx >= 0)
18646 +               au_xinondir_leave(sb, btop, h_ino, idx);
18647 +out:
18648 +       return inode;
18649 +}
18650 +
18651 +/* ---------------------------------------------------------------------- */
18652 +
18653 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18654 +              struct inode *inode)
18655 +{
18656 +       int err;
18657 +       struct inode *hi;
18658 +
18659 +       err = au_br_rdonly(au_sbr(sb, bindex));
18660 +
18661 +       /* pseudo-link after flushed may happen out of bounds */
18662 +       if (!err
18663 +           && inode
18664 +           && au_ibtop(inode) <= bindex
18665 +           && bindex <= au_ibbot(inode)) {
18666 +               /*
18667 +                * permission check is unnecessary since vfsub routine
18668 +                * will be called later
18669 +                */
18670 +               hi = au_h_iptr(inode, bindex);
18671 +               if (hi)
18672 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18673 +       }
18674 +
18675 +       return err;
18676 +}
18677 +
18678 +int au_test_h_perm(struct inode *h_inode, int mask)
18679 +{
18680 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18681 +               return 0;
18682 +       return inode_permission(h_inode, mask);
18683 +}
18684 +
18685 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18686 +{
18687 +       if (au_test_nfs(h_inode->i_sb)
18688 +           && (mask & MAY_WRITE)
18689 +           && S_ISDIR(h_inode->i_mode))
18690 +               mask |= MAY_READ; /* force permission check */
18691 +       return au_test_h_perm(h_inode, mask);
18692 +}
18693 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18694 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18695 +++ linux/fs/aufs/inode.h       2020-08-03 09:14:46.095748745 +0200
18696 @@ -0,0 +1,698 @@
18697 +/* SPDX-License-Identifier: GPL-2.0 */
18698 +/*
18699 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18700 + *
18701 + * This program, aufs is free software; you can redistribute it and/or modify
18702 + * it under the terms of the GNU General Public License as published by
18703 + * the Free Software Foundation; either version 2 of the License, or
18704 + * (at your option) any later version.
18705 + *
18706 + * This program is distributed in the hope that it will be useful,
18707 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18708 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18709 + * GNU General Public License for more details.
18710 + *
18711 + * You should have received a copy of the GNU General Public License
18712 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18713 + */
18714 +
18715 +/*
18716 + * inode operations
18717 + */
18718 +
18719 +#ifndef __AUFS_INODE_H__
18720 +#define __AUFS_INODE_H__
18721 +
18722 +#ifdef __KERNEL__
18723 +
18724 +#include <linux/fsnotify.h>
18725 +#include "rwsem.h"
18726 +
18727 +struct vfsmount;
18728 +
18729 +struct au_hnotify {
18730 +#ifdef CONFIG_AUFS_HNOTIFY
18731 +#ifdef CONFIG_AUFS_HFSNOTIFY
18732 +       /* never use fsnotify_add_vfsmount_mark() */
18733 +       struct fsnotify_mark            hn_mark;
18734 +#endif
18735 +       struct inode            *hn_aufs_inode; /* no get/put */
18736 +       struct rcu_head         rcu;
18737 +#endif
18738 +} ____cacheline_aligned_in_smp;
18739 +
18740 +struct au_hinode {
18741 +       struct inode            *hi_inode;
18742 +       aufs_bindex_t           hi_id;
18743 +#ifdef CONFIG_AUFS_HNOTIFY
18744 +       struct au_hnotify       *hi_notify;
18745 +#endif
18746 +
18747 +       /* reference to the copied-up whiteout with get/put */
18748 +       struct dentry           *hi_whdentry;
18749 +};
18750 +
18751 +/* ig_flags */
18752 +#define AuIG_HALF_REFRESHED            1
18753 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18754 +#define au_ig_fset(flags, name) \
18755 +       do { (flags) |= AuIG_##name; } while (0)
18756 +#define au_ig_fclr(flags, name) \
18757 +       do { (flags) &= ~AuIG_##name; } while (0)
18758 +
18759 +struct au_iigen {
18760 +       spinlock_t      ig_spin;
18761 +       __u32           ig_generation, ig_flags;
18762 +};
18763 +
18764 +struct au_vdir;
18765 +struct au_iinfo {
18766 +       struct au_iigen         ii_generation;
18767 +       struct super_block      *ii_hsb1;       /* no get/put */
18768 +
18769 +       struct au_rwsem         ii_rwsem;
18770 +       aufs_bindex_t           ii_btop, ii_bbot;
18771 +       __u32                   ii_higen;
18772 +       struct au_hinode        *ii_hinode;
18773 +       struct au_vdir          *ii_vdir;
18774 +};
18775 +
18776 +struct au_icntnr {
18777 +       struct au_iinfo         iinfo;
18778 +       struct inode            vfs_inode;
18779 +       struct hlist_bl_node    plink;
18780 +       struct rcu_head         rcu;
18781 +} ____cacheline_aligned_in_smp;
18782 +
18783 +/* au_pin flags */
18784 +#define AuPin_DI_LOCKED                1
18785 +#define AuPin_MNT_WRITE                (1 << 1)
18786 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18787 +#define au_fset_pin(flags, name) \
18788 +       do { (flags) |= AuPin_##name; } while (0)
18789 +#define au_fclr_pin(flags, name) \
18790 +       do { (flags) &= ~AuPin_##name; } while (0)
18791 +
18792 +struct au_pin {
18793 +       /* input */
18794 +       struct dentry *dentry;
18795 +       unsigned int udba;
18796 +       unsigned char lsc_di, lsc_hi, flags;
18797 +       aufs_bindex_t bindex;
18798 +
18799 +       /* output */
18800 +       struct dentry *parent;
18801 +       struct au_hinode *hdir;
18802 +       struct vfsmount *h_mnt;
18803 +
18804 +       /* temporary unlock/relock for copyup */
18805 +       struct dentry *h_dentry, *h_parent;
18806 +       struct au_branch *br;
18807 +       struct task_struct *task;
18808 +};
18809 +
18810 +void au_pin_hdir_unlock(struct au_pin *p);
18811 +int au_pin_hdir_lock(struct au_pin *p);
18812 +int au_pin_hdir_relock(struct au_pin *p);
18813 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18814 +void au_pin_hdir_release(struct au_pin *p);
18815 +
18816 +/* ---------------------------------------------------------------------- */
18817 +
18818 +static inline struct au_iinfo *au_ii(struct inode *inode)
18819 +{
18820 +       BUG_ON(is_bad_inode(inode));
18821 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18822 +}
18823 +
18824 +/* ---------------------------------------------------------------------- */
18825 +
18826 +/* inode.c */
18827 +struct inode *au_igrab(struct inode *inode);
18828 +void au_refresh_iop(struct inode *inode, int force_getattr);
18829 +int au_refresh_hinode_self(struct inode *inode);
18830 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18831 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18832 +          unsigned int d_type, ino_t *ino);
18833 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18834 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18835 +              struct inode *inode);
18836 +int au_test_h_perm(struct inode *h_inode, int mask);
18837 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18838 +
18839 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18840 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18841 +{
18842 +#ifdef CONFIG_AUFS_SHWH
18843 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18844 +#else
18845 +       return 0;
18846 +#endif
18847 +}
18848 +
18849 +/* i_op.c */
18850 +enum {
18851 +       AuIop_SYMLINK,
18852 +       AuIop_DIR,
18853 +       AuIop_OTHER,
18854 +       AuIop_Last
18855 +};
18856 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18857 +       aufs_iop_nogetattr[AuIop_Last];
18858 +
18859 +/* au_wr_dir flags */
18860 +#define AuWrDir_ADD_ENTRY      1
18861 +#define AuWrDir_ISDIR          (1 << 1)
18862 +#define AuWrDir_TMPFILE                (1 << 2)
18863 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18864 +#define au_fset_wrdir(flags, name) \
18865 +       do { (flags) |= AuWrDir_##name; } while (0)
18866 +#define au_fclr_wrdir(flags, name) \
18867 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18868 +
18869 +struct au_wr_dir_args {
18870 +       aufs_bindex_t force_btgt;
18871 +       unsigned char flags;
18872 +};
18873 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18874 +             struct au_wr_dir_args *args);
18875 +
18876 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18877 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18878 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18879 +                unsigned int udba, unsigned char flags);
18880 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18881 +          unsigned int udba, unsigned char flags) __must_check;
18882 +int au_do_pin(struct au_pin *pin) __must_check;
18883 +void au_unpin(struct au_pin *pin);
18884 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18885 +
18886 +#define AuIcpup_DID_CPUP       1
18887 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18888 +#define au_fset_icpup(flags, name) \
18889 +       do { (flags) |= AuIcpup_##name; } while (0)
18890 +#define au_fclr_icpup(flags, name) \
18891 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18892 +
18893 +struct au_icpup_args {
18894 +       unsigned char flags;
18895 +       unsigned char pin_flags;
18896 +       aufs_bindex_t btgt;
18897 +       unsigned int udba;
18898 +       struct au_pin pin;
18899 +       struct path h_path;
18900 +       struct inode *h_inode;
18901 +};
18902 +
18903 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18904 +                    struct au_icpup_args *a);
18905 +
18906 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18907 +                     struct path *h_path, int locked);
18908 +
18909 +/* i_op_add.c */
18910 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18911 +              struct dentry *h_parent, int isdir);
18912 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18913 +              dev_t dev);
18914 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18915 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18916 +               bool want_excl);
18917 +struct vfsub_aopen_args;
18918 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18919 +                      struct vfsub_aopen_args *args);
18920 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18921 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18922 +             struct dentry *dentry);
18923 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18924 +
18925 +/* i_op_del.c */
18926 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18927 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18928 +              struct dentry *h_parent, int isdir);
18929 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18930 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18931 +
18932 +/* i_op_ren.c */
18933 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18934 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18935 +               struct inode *dir, struct dentry *dentry,
18936 +               unsigned int flags);
18937 +
18938 +/* iinfo.c */
18939 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18940 +void au_hiput(struct au_hinode *hinode);
18941 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18942 +                 struct dentry *h_wh);
18943 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18944 +
18945 +/* hinode flags */
18946 +#define AuHi_XINO      1
18947 +#define AuHi_HNOTIFY   (1 << 1)
18948 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18949 +#define au_fset_hi(flags, name) \
18950 +       do { (flags) |= AuHi_##name; } while (0)
18951 +#define au_fclr_hi(flags, name) \
18952 +       do { (flags) &= ~AuHi_##name; } while (0)
18953 +
18954 +#ifndef CONFIG_AUFS_HNOTIFY
18955 +#undef AuHi_HNOTIFY
18956 +#define AuHi_HNOTIFY   0
18957 +#endif
18958 +
18959 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18960 +                  struct inode *h_inode, unsigned int flags);
18961 +
18962 +void au_update_iigen(struct inode *inode, int half);
18963 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18964 +
18965 +void au_icntnr_init_once(void *_c);
18966 +void au_hinode_init(struct au_hinode *hinode);
18967 +int au_iinfo_init(struct inode *inode);
18968 +void au_iinfo_fin(struct inode *inode);
18969 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18970 +
18971 +#ifdef CONFIG_PROC_FS
18972 +/* plink.c */
18973 +int au_plink_maint(struct super_block *sb, int flags);
18974 +struct au_sbinfo;
18975 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18976 +int au_plink_maint_enter(struct super_block *sb);
18977 +#ifdef CONFIG_AUFS_DEBUG
18978 +void au_plink_list(struct super_block *sb);
18979 +#else
18980 +AuStubVoid(au_plink_list, struct super_block *sb)
18981 +#endif
18982 +int au_plink_test(struct inode *inode);
18983 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18984 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18985 +                    struct dentry *h_dentry);
18986 +void au_plink_put(struct super_block *sb, int verbose);
18987 +void au_plink_clean(struct super_block *sb, int verbose);
18988 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18989 +#else
18990 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18991 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18992 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18993 +AuStubVoid(au_plink_list, struct super_block *sb);
18994 +AuStubInt0(au_plink_test, struct inode *inode);
18995 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18996 +       struct inode *inode, aufs_bindex_t bindex);
18997 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18998 +          struct dentry *h_dentry);
18999 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19000 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19001 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19002 +#endif /* CONFIG_PROC_FS */
19003 +
19004 +#ifdef CONFIG_AUFS_XATTR
19005 +/* xattr.c */
19006 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19007 +                 unsigned int verbose);
19008 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19009 +void au_xattr_init(struct super_block *sb);
19010 +#else
19011 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19012 +          int ignore_flags, unsigned int verbose);
19013 +AuStubVoid(au_xattr_init, struct super_block *sb);
19014 +#endif
19015 +
19016 +#ifdef CONFIG_FS_POSIX_ACL
19017 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19018 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19019 +#endif
19020 +
19021 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19022 +enum {
19023 +       AU_XATTR_SET,
19024 +       AU_ACL_SET
19025 +};
19026 +
19027 +struct au_sxattr {
19028 +       int type;
19029 +       union {
19030 +               struct {
19031 +                       const char      *name;
19032 +                       const void      *value;
19033 +                       size_t          size;
19034 +                       int             flags;
19035 +               } set;
19036 +               struct {
19037 +                       struct posix_acl *acl;
19038 +                       int             type;
19039 +               } acl_set;
19040 +       } u;
19041 +};
19042 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19043 +                 struct au_sxattr *arg);
19044 +#endif
19045 +
19046 +/* ---------------------------------------------------------------------- */
19047 +
19048 +/* lock subclass for iinfo */
19049 +enum {
19050 +       AuLsc_II_CHILD,         /* child first */
19051 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19052 +       AuLsc_II_CHILD3,        /* copyup dirs */
19053 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19054 +       AuLsc_II_PARENT2,
19055 +       AuLsc_II_PARENT3,       /* copyup dirs */
19056 +       AuLsc_II_NEW_CHILD
19057 +};
19058 +
19059 +/*
19060 + * ii_read_lock_child, ii_write_lock_child,
19061 + * ii_read_lock_child2, ii_write_lock_child2,
19062 + * ii_read_lock_child3, ii_write_lock_child3,
19063 + * ii_read_lock_parent, ii_write_lock_parent,
19064 + * ii_read_lock_parent2, ii_write_lock_parent2,
19065 + * ii_read_lock_parent3, ii_write_lock_parent3,
19066 + * ii_read_lock_new_child, ii_write_lock_new_child,
19067 + */
19068 +#define AuReadLockFunc(name, lsc) \
19069 +static inline void ii_read_lock_##name(struct inode *i) \
19070 +{ \
19071 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19072 +}
19073 +
19074 +#define AuWriteLockFunc(name, lsc) \
19075 +static inline void ii_write_lock_##name(struct inode *i) \
19076 +{ \
19077 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19078 +}
19079 +
19080 +#define AuRWLockFuncs(name, lsc) \
19081 +       AuReadLockFunc(name, lsc) \
19082 +       AuWriteLockFunc(name, lsc)
19083 +
19084 +AuRWLockFuncs(child, CHILD);
19085 +AuRWLockFuncs(child2, CHILD2);
19086 +AuRWLockFuncs(child3, CHILD3);
19087 +AuRWLockFuncs(parent, PARENT);
19088 +AuRWLockFuncs(parent2, PARENT2);
19089 +AuRWLockFuncs(parent3, PARENT3);
19090 +AuRWLockFuncs(new_child, NEW_CHILD);
19091 +
19092 +#undef AuReadLockFunc
19093 +#undef AuWriteLockFunc
19094 +#undef AuRWLockFuncs
19095 +
19096 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19097 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19098 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19099 +
19100 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19101 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19102 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19103 +
19104 +/* ---------------------------------------------------------------------- */
19105 +
19106 +static inline void au_icntnr_init(struct au_icntnr *c)
19107 +{
19108 +#ifdef CONFIG_AUFS_DEBUG
19109 +       c->vfs_inode.i_mode = 0;
19110 +#endif
19111 +}
19112 +
19113 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19114 +{
19115 +       unsigned int gen;
19116 +       struct au_iinfo *iinfo;
19117 +       struct au_iigen *iigen;
19118 +
19119 +       iinfo = au_ii(inode);
19120 +       iigen = &iinfo->ii_generation;
19121 +       spin_lock(&iigen->ig_spin);
19122 +       if (igflags)
19123 +               *igflags = iigen->ig_flags;
19124 +       gen = iigen->ig_generation;
19125 +       spin_unlock(&iigen->ig_spin);
19126 +
19127 +       return gen;
19128 +}
19129 +
19130 +/* tiny test for inode number */
19131 +/* tmpfs generation is too rough */
19132 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19133 +{
19134 +       struct au_iinfo *iinfo;
19135 +
19136 +       iinfo = au_ii(inode);
19137 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19138 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19139 +                && iinfo->ii_higen == h_inode->i_generation);
19140 +}
19141 +
19142 +static inline void au_iigen_dec(struct inode *inode)
19143 +{
19144 +       struct au_iinfo *iinfo;
19145 +       struct au_iigen *iigen;
19146 +
19147 +       iinfo = au_ii(inode);
19148 +       iigen = &iinfo->ii_generation;
19149 +       spin_lock(&iigen->ig_spin);
19150 +       iigen->ig_generation--;
19151 +       spin_unlock(&iigen->ig_spin);
19152 +}
19153 +
19154 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19155 +{
19156 +       int err;
19157 +
19158 +       err = 0;
19159 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19160 +               err = -EIO;
19161 +
19162 +       return err;
19163 +}
19164 +
19165 +/* ---------------------------------------------------------------------- */
19166 +
19167 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19168 +                                         aufs_bindex_t bindex)
19169 +{
19170 +       return iinfo->ii_hinode + bindex;
19171 +}
19172 +
19173 +static inline int au_is_bad_inode(struct inode *inode)
19174 +{
19175 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19176 +}
19177 +
19178 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19179 +                                       aufs_bindex_t bindex)
19180 +{
19181 +       IiMustAnyLock(inode);
19182 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19183 +}
19184 +
19185 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19186 +{
19187 +       IiMustAnyLock(inode);
19188 +       return au_ii(inode)->ii_btop;
19189 +}
19190 +
19191 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19192 +{
19193 +       IiMustAnyLock(inode);
19194 +       return au_ii(inode)->ii_bbot;
19195 +}
19196 +
19197 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19198 +{
19199 +       IiMustAnyLock(inode);
19200 +       return au_ii(inode)->ii_vdir;
19201 +}
19202 +
19203 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19204 +{
19205 +       IiMustAnyLock(inode);
19206 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19207 +}
19208 +
19209 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19210 +{
19211 +       IiMustWriteLock(inode);
19212 +       au_ii(inode)->ii_btop = bindex;
19213 +}
19214 +
19215 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19216 +{
19217 +       IiMustWriteLock(inode);
19218 +       au_ii(inode)->ii_bbot = bindex;
19219 +}
19220 +
19221 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19222 +{
19223 +       IiMustWriteLock(inode);
19224 +       au_ii(inode)->ii_vdir = vdir;
19225 +}
19226 +
19227 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19228 +{
19229 +       IiMustAnyLock(inode);
19230 +       return au_hinode(au_ii(inode), bindex);
19231 +}
19232 +
19233 +/* ---------------------------------------------------------------------- */
19234 +
19235 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19236 +{
19237 +       if (pin)
19238 +               return pin->parent;
19239 +       return NULL;
19240 +}
19241 +
19242 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19243 +{
19244 +       if (pin && pin->hdir)
19245 +               return pin->hdir->hi_inode;
19246 +       return NULL;
19247 +}
19248 +
19249 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19250 +{
19251 +       if (pin)
19252 +               return pin->hdir;
19253 +       return NULL;
19254 +}
19255 +
19256 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19257 +{
19258 +       if (pin)
19259 +               pin->dentry = dentry;
19260 +}
19261 +
19262 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19263 +                                          unsigned char lflag)
19264 +{
19265 +       if (pin) {
19266 +               if (lflag)
19267 +                       au_fset_pin(pin->flags, DI_LOCKED);
19268 +               else
19269 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19270 +       }
19271 +}
19272 +
19273 +#if 0 /* reserved */
19274 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19275 +{
19276 +       if (pin) {
19277 +               dput(pin->parent);
19278 +               pin->parent = dget(parent);
19279 +       }
19280 +}
19281 +#endif
19282 +
19283 +/* ---------------------------------------------------------------------- */
19284 +
19285 +struct au_branch;
19286 +#ifdef CONFIG_AUFS_HNOTIFY
19287 +struct au_hnotify_op {
19288 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19289 +       int (*alloc)(struct au_hinode *hinode);
19290 +
19291 +       /*
19292 +        * if it returns true, the the caller should free hinode->hi_notify,
19293 +        * otherwise ->free() frees it.
19294 +        */
19295 +       int (*free)(struct au_hinode *hinode,
19296 +                   struct au_hnotify *hn) __must_check;
19297 +
19298 +       void (*fin)(void);
19299 +       int (*init)(void);
19300 +
19301 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19302 +       void (*fin_br)(struct au_branch *br);
19303 +       int (*init_br)(struct au_branch *br, int perm);
19304 +};
19305 +
19306 +/* hnotify.c */
19307 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19308 +void au_hn_free(struct au_hinode *hinode);
19309 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19310 +void au_hn_reset(struct inode *inode, unsigned int flags);
19311 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19312 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19313 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19314 +int au_hnotify_init_br(struct au_branch *br, int perm);
19315 +void au_hnotify_fin_br(struct au_branch *br);
19316 +int __init au_hnotify_init(void);
19317 +void au_hnotify_fin(void);
19318 +
19319 +/* hfsnotify.c */
19320 +extern const struct au_hnotify_op au_hnotify_op;
19321 +
19322 +static inline
19323 +void au_hn_init(struct au_hinode *hinode)
19324 +{
19325 +       hinode->hi_notify = NULL;
19326 +}
19327 +
19328 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19329 +{
19330 +       return hinode->hi_notify;
19331 +}
19332 +
19333 +#else
19334 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19335 +       struct au_hinode *hinode __maybe_unused,
19336 +       struct inode *inode __maybe_unused)
19337 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19338 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19339 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19340 +          int do_set __maybe_unused)
19341 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19342 +          unsigned int flags __maybe_unused)
19343 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19344 +          struct au_branch *br __maybe_unused,
19345 +          int perm __maybe_unused)
19346 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19347 +          int perm __maybe_unused)
19348 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19349 +AuStubInt0(__init au_hnotify_init, void)
19350 +AuStubVoid(au_hnotify_fin, void)
19351 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19352 +#endif /* CONFIG_AUFS_HNOTIFY */
19353 +
19354 +static inline void au_hn_suspend(struct au_hinode *hdir)
19355 +{
19356 +       au_hn_ctl(hdir, /*do_set*/0);
19357 +}
19358 +
19359 +static inline void au_hn_resume(struct au_hinode *hdir)
19360 +{
19361 +       au_hn_ctl(hdir, /*do_set*/1);
19362 +}
19363 +
19364 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19365 +{
19366 +       inode_lock(hdir->hi_inode);
19367 +       au_hn_suspend(hdir);
19368 +}
19369 +
19370 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19371 +                                         unsigned int sc __maybe_unused)
19372 +{
19373 +       inode_lock_nested(hdir->hi_inode, sc);
19374 +       au_hn_suspend(hdir);
19375 +}
19376 +
19377 +#if 0 /* unused */
19378 +#include "vfsub.h"
19379 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19380 +                                                 unsigned int sc)
19381 +{
19382 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19383 +       au_hn_suspend(hdir);
19384 +}
19385 +#endif
19386 +
19387 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19388 +{
19389 +       au_hn_resume(hdir);
19390 +       inode_unlock(hdir->hi_inode);
19391 +}
19392 +
19393 +#endif /* __KERNEL__ */
19394 +#endif /* __AUFS_INODE_H__ */
19395 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19396 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19397 +++ linux/fs/aufs/ioctl.c       2020-01-27 10:57:18.175538316 +0100
19398 @@ -0,0 +1,220 @@
19399 +// SPDX-License-Identifier: GPL-2.0
19400 +/*
19401 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19402 + *
19403 + * This program, aufs is free software; you can redistribute it and/or modify
19404 + * it under the terms of the GNU General Public License as published by
19405 + * the Free Software Foundation; either version 2 of the License, or
19406 + * (at your option) any later version.
19407 + *
19408 + * This program is distributed in the hope that it will be useful,
19409 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19410 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19411 + * GNU General Public License for more details.
19412 + *
19413 + * You should have received a copy of the GNU General Public License
19414 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19415 + */
19416 +
19417 +/*
19418 + * ioctl
19419 + * plink-management and readdir in userspace.
19420 + * assist the pathconf(3) wrapper library.
19421 + * move-down
19422 + * File-based Hierarchical Storage Management.
19423 + */
19424 +
19425 +#include <linux/compat.h>
19426 +#include <linux/file.h>
19427 +#include "aufs.h"
19428 +
19429 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19430 +{
19431 +       int err, fd;
19432 +       aufs_bindex_t wbi, bindex, bbot;
19433 +       struct file *h_file;
19434 +       struct super_block *sb;
19435 +       struct dentry *root;
19436 +       struct au_branch *br;
19437 +       struct aufs_wbr_fd wbrfd = {
19438 +               .oflags = au_dir_roflags,
19439 +               .brid   = -1
19440 +       };
19441 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19442 +               | O_NOATIME | O_CLOEXEC;
19443 +
19444 +       AuDebugOn(wbrfd.oflags & ~valid);
19445 +
19446 +       if (arg) {
19447 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19448 +               if (unlikely(err)) {
19449 +                       err = -EFAULT;
19450 +                       goto out;
19451 +               }
19452 +
19453 +               err = -EINVAL;
19454 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19455 +               wbrfd.oflags |= au_dir_roflags;
19456 +               AuDbg("0%o\n", wbrfd.oflags);
19457 +               if (unlikely(wbrfd.oflags & ~valid))
19458 +                       goto out;
19459 +       }
19460 +
19461 +       fd = get_unused_fd_flags(0);
19462 +       err = fd;
19463 +       if (unlikely(fd < 0))
19464 +               goto out;
19465 +
19466 +       h_file = ERR_PTR(-EINVAL);
19467 +       wbi = 0;
19468 +       br = NULL;
19469 +       sb = path->dentry->d_sb;
19470 +       root = sb->s_root;
19471 +       aufs_read_lock(root, AuLock_IR);
19472 +       bbot = au_sbbot(sb);
19473 +       if (wbrfd.brid >= 0) {
19474 +               wbi = au_br_index(sb, wbrfd.brid);
19475 +               if (unlikely(wbi < 0 || wbi > bbot))
19476 +                       goto out_unlock;
19477 +       }
19478 +
19479 +       h_file = ERR_PTR(-ENOENT);
19480 +       br = au_sbr(sb, wbi);
19481 +       if (!au_br_writable(br->br_perm)) {
19482 +               if (arg)
19483 +                       goto out_unlock;
19484 +
19485 +               bindex = wbi + 1;
19486 +               wbi = -1;
19487 +               for (; bindex <= bbot; bindex++) {
19488 +                       br = au_sbr(sb, bindex);
19489 +                       if (au_br_writable(br->br_perm)) {
19490 +                               wbi = bindex;
19491 +                               br = au_sbr(sb, wbi);
19492 +                               break;
19493 +                       }
19494 +               }
19495 +       }
19496 +       AuDbg("wbi %d\n", wbi);
19497 +       if (wbi >= 0)
19498 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19499 +                                  /*force_wr*/0);
19500 +
19501 +out_unlock:
19502 +       aufs_read_unlock(root, AuLock_IR);
19503 +       err = PTR_ERR(h_file);
19504 +       if (IS_ERR(h_file))
19505 +               goto out_fd;
19506 +
19507 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19508 +       fd_install(fd, h_file);
19509 +       err = fd;
19510 +       goto out; /* success */
19511 +
19512 +out_fd:
19513 +       put_unused_fd(fd);
19514 +out:
19515 +       AuTraceErr(err);
19516 +       return err;
19517 +}
19518 +
19519 +/* ---------------------------------------------------------------------- */
19520 +
19521 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19522 +{
19523 +       long err;
19524 +       struct dentry *dentry;
19525 +
19526 +       switch (cmd) {
19527 +       case AUFS_CTL_RDU:
19528 +       case AUFS_CTL_RDU_INO:
19529 +               err = au_rdu_ioctl(file, cmd, arg);
19530 +               break;
19531 +
19532 +       case AUFS_CTL_WBR_FD:
19533 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19534 +               break;
19535 +
19536 +       case AUFS_CTL_IBUSY:
19537 +               err = au_ibusy_ioctl(file, arg);
19538 +               break;
19539 +
19540 +       case AUFS_CTL_BRINFO:
19541 +               err = au_brinfo_ioctl(file, arg);
19542 +               break;
19543 +
19544 +       case AUFS_CTL_FHSM_FD:
19545 +               dentry = file->f_path.dentry;
19546 +               if (IS_ROOT(dentry))
19547 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19548 +               else
19549 +                       err = -ENOTTY;
19550 +               break;
19551 +
19552 +       default:
19553 +               /* do not call the lower */
19554 +               AuDbg("0x%x\n", cmd);
19555 +               err = -ENOTTY;
19556 +       }
19557 +
19558 +       AuTraceErr(err);
19559 +       return err;
19560 +}
19561 +
19562 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19563 +{
19564 +       long err;
19565 +
19566 +       switch (cmd) {
19567 +       case AUFS_CTL_MVDOWN:
19568 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19569 +               break;
19570 +
19571 +       case AUFS_CTL_WBR_FD:
19572 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19573 +               break;
19574 +
19575 +       default:
19576 +               /* do not call the lower */
19577 +               AuDbg("0x%x\n", cmd);
19578 +               err = -ENOTTY;
19579 +       }
19580 +
19581 +       AuTraceErr(err);
19582 +       return err;
19583 +}
19584 +
19585 +#ifdef CONFIG_COMPAT
19586 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19587 +                          unsigned long arg)
19588 +{
19589 +       long err;
19590 +
19591 +       switch (cmd) {
19592 +       case AUFS_CTL_RDU:
19593 +       case AUFS_CTL_RDU_INO:
19594 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19595 +               break;
19596 +
19597 +       case AUFS_CTL_IBUSY:
19598 +               err = au_ibusy_compat_ioctl(file, arg);
19599 +               break;
19600 +
19601 +       case AUFS_CTL_BRINFO:
19602 +               err = au_brinfo_compat_ioctl(file, arg);
19603 +               break;
19604 +
19605 +       default:
19606 +               err = aufs_ioctl_dir(file, cmd, arg);
19607 +       }
19608 +
19609 +       AuTraceErr(err);
19610 +       return err;
19611 +}
19612 +
19613 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19614 +                             unsigned long arg)
19615 +{
19616 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19617 +}
19618 +#endif
19619 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19620 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19621 +++ linux/fs/aufs/i_op_add.c    2020-01-27 10:57:18.172204883 +0100
19622 @@ -0,0 +1,936 @@
19623 +// SPDX-License-Identifier: GPL-2.0
19624 +/*
19625 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19626 + *
19627 + * This program, aufs is free software; you can redistribute it and/or modify
19628 + * it under the terms of the GNU General Public License as published by
19629 + * the Free Software Foundation; either version 2 of the License, or
19630 + * (at your option) any later version.
19631 + *
19632 + * This program is distributed in the hope that it will be useful,
19633 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19634 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19635 + * GNU General Public License for more details.
19636 + *
19637 + * You should have received a copy of the GNU General Public License
19638 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19639 + */
19640 +
19641 +/*
19642 + * inode operations (add entry)
19643 + */
19644 +
19645 +#include <linux/iversion.h>
19646 +#include "aufs.h"
19647 +
19648 +/*
19649 + * final procedure of adding a new entry, except link(2).
19650 + * remove whiteout, instantiate, copyup the parent dir's times and size
19651 + * and update version.
19652 + * if it failed, re-create the removed whiteout.
19653 + */
19654 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19655 +                 struct dentry *wh_dentry, struct dentry *dentry)
19656 +{
19657 +       int err, rerr;
19658 +       aufs_bindex_t bwh;
19659 +       struct path h_path;
19660 +       struct super_block *sb;
19661 +       struct inode *inode, *h_dir;
19662 +       struct dentry *wh;
19663 +
19664 +       bwh = -1;
19665 +       sb = dir->i_sb;
19666 +       if (wh_dentry) {
19667 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19668 +               IMustLock(h_dir);
19669 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19670 +               bwh = au_dbwh(dentry);
19671 +               h_path.dentry = wh_dentry;
19672 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19673 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19674 +                                         dentry);
19675 +               if (unlikely(err))
19676 +                       goto out;
19677 +       }
19678 +
19679 +       inode = au_new_inode(dentry, /*must_new*/1);
19680 +       if (!IS_ERR(inode)) {
19681 +               d_instantiate(dentry, inode);
19682 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19683 +               IMustLock(dir);
19684 +               au_dir_ts(dir, bindex);
19685 +               inode_inc_iversion(dir);
19686 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19687 +               return 0; /* success */
19688 +       }
19689 +
19690 +       err = PTR_ERR(inode);
19691 +       if (!wh_dentry)
19692 +               goto out;
19693 +
19694 +       /* revert */
19695 +       /* dir inode is locked */
19696 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19697 +       rerr = PTR_ERR(wh);
19698 +       if (IS_ERR(wh)) {
19699 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19700 +                       dentry, err, rerr);
19701 +               err = -EIO;
19702 +       } else
19703 +               dput(wh);
19704 +
19705 +out:
19706 +       return err;
19707 +}
19708 +
19709 +static int au_d_may_add(struct dentry *dentry)
19710 +{
19711 +       int err;
19712 +
19713 +       err = 0;
19714 +       if (unlikely(d_unhashed(dentry)))
19715 +               err = -ENOENT;
19716 +       if (unlikely(d_really_is_positive(dentry)))
19717 +               err = -EEXIST;
19718 +       return err;
19719 +}
19720 +
19721 +/*
19722 + * simple tests for the adding inode operations.
19723 + * following the checks in vfs, plus the parent-child relationship.
19724 + */
19725 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19726 +              struct dentry *h_parent, int isdir)
19727 +{
19728 +       int err;
19729 +       umode_t h_mode;
19730 +       struct dentry *h_dentry;
19731 +       struct inode *h_inode;
19732 +
19733 +       err = -ENAMETOOLONG;
19734 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19735 +               goto out;
19736 +
19737 +       h_dentry = au_h_dptr(dentry, bindex);
19738 +       if (d_really_is_negative(dentry)) {
19739 +               err = -EEXIST;
19740 +               if (unlikely(d_is_positive(h_dentry)))
19741 +                       goto out;
19742 +       } else {
19743 +               /* rename(2) case */
19744 +               err = -EIO;
19745 +               if (unlikely(d_is_negative(h_dentry)))
19746 +                       goto out;
19747 +               h_inode = d_inode(h_dentry);
19748 +               if (unlikely(!h_inode->i_nlink))
19749 +                       goto out;
19750 +
19751 +               h_mode = h_inode->i_mode;
19752 +               if (!isdir) {
19753 +                       err = -EISDIR;
19754 +                       if (unlikely(S_ISDIR(h_mode)))
19755 +                               goto out;
19756 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19757 +                       err = -ENOTDIR;
19758 +                       goto out;
19759 +               }
19760 +       }
19761 +
19762 +       err = 0;
19763 +       /* expected parent dir is locked */
19764 +       if (unlikely(h_parent != h_dentry->d_parent))
19765 +               err = -EIO;
19766 +
19767 +out:
19768 +       AuTraceErr(err);
19769 +       return err;
19770 +}
19771 +
19772 +/*
19773 + * initial procedure of adding a new entry.
19774 + * prepare writable branch and the parent dir, lock it,
19775 + * and lookup whiteout for the new entry.
19776 + */
19777 +static struct dentry*
19778 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19779 +                 struct dentry *src_dentry, struct au_pin *pin,
19780 +                 struct au_wr_dir_args *wr_dir_args)
19781 +{
19782 +       struct dentry *wh_dentry, *h_parent;
19783 +       struct super_block *sb;
19784 +       struct au_branch *br;
19785 +       int err;
19786 +       unsigned int udba;
19787 +       aufs_bindex_t bcpup;
19788 +
19789 +       AuDbg("%pd\n", dentry);
19790 +
19791 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19792 +       bcpup = err;
19793 +       wh_dentry = ERR_PTR(err);
19794 +       if (unlikely(err < 0))
19795 +               goto out;
19796 +
19797 +       sb = dentry->d_sb;
19798 +       udba = au_opt_udba(sb);
19799 +       err = au_pin(pin, dentry, bcpup, udba,
19800 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19801 +       wh_dentry = ERR_PTR(err);
19802 +       if (unlikely(err))
19803 +               goto out;
19804 +
19805 +       h_parent = au_pinned_h_parent(pin);
19806 +       if (udba != AuOpt_UDBA_NONE
19807 +           && au_dbtop(dentry) == bcpup)
19808 +               err = au_may_add(dentry, bcpup, h_parent,
19809 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19810 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19811 +               err = -ENAMETOOLONG;
19812 +       wh_dentry = ERR_PTR(err);
19813 +       if (unlikely(err))
19814 +               goto out_unpin;
19815 +
19816 +       br = au_sbr(sb, bcpup);
19817 +       if (dt) {
19818 +               struct path tmp = {
19819 +                       .dentry = h_parent,
19820 +                       .mnt    = au_br_mnt(br)
19821 +               };
19822 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19823 +       }
19824 +
19825 +       wh_dentry = NULL;
19826 +       if (bcpup != au_dbwh(dentry))
19827 +               goto out; /* success */
19828 +
19829 +       /*
19830 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19831 +        * would not be able to removed in the future. So we don't allow such
19832 +        * name here and we don't handle ENAMETOOLONG differently here.
19833 +        */
19834 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19835 +
19836 +out_unpin:
19837 +       if (IS_ERR(wh_dentry))
19838 +               au_unpin(pin);
19839 +out:
19840 +       return wh_dentry;
19841 +}
19842 +
19843 +/* ---------------------------------------------------------------------- */
19844 +
19845 +enum { Mknod, Symlink, Creat };
19846 +struct simple_arg {
19847 +       int type;
19848 +       union {
19849 +               struct {
19850 +                       umode_t                 mode;
19851 +                       bool                    want_excl;
19852 +                       bool                    try_aopen;
19853 +                       struct vfsub_aopen_args *aopen;
19854 +               } c;
19855 +               struct {
19856 +                       const char *symname;
19857 +               } s;
19858 +               struct {
19859 +                       umode_t mode;
19860 +                       dev_t dev;
19861 +               } m;
19862 +       } u;
19863 +};
19864 +
19865 +static int add_simple(struct inode *dir, struct dentry *dentry,
19866 +                     struct simple_arg *arg)
19867 +{
19868 +       int err, rerr;
19869 +       aufs_bindex_t btop;
19870 +       unsigned char created;
19871 +       const unsigned char try_aopen
19872 +               = (arg->type == Creat && arg->u.c.try_aopen);
19873 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19874 +       struct dentry *wh_dentry, *parent;
19875 +       struct inode *h_dir;
19876 +       struct super_block *sb;
19877 +       struct au_branch *br;
19878 +       /* to reduce stack size */
19879 +       struct {
19880 +               struct au_dtime dt;
19881 +               struct au_pin pin;
19882 +               struct path h_path;
19883 +               struct au_wr_dir_args wr_dir_args;
19884 +       } *a;
19885 +
19886 +       AuDbg("%pd\n", dentry);
19887 +       IMustLock(dir);
19888 +
19889 +       err = -ENOMEM;
19890 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19891 +       if (unlikely(!a))
19892 +               goto out;
19893 +       a->wr_dir_args.force_btgt = -1;
19894 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19895 +
19896 +       parent = dentry->d_parent; /* dir inode is locked */
19897 +       if (!try_aopen) {
19898 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19899 +               if (unlikely(err))
19900 +                       goto out_free;
19901 +       }
19902 +       err = au_d_may_add(dentry);
19903 +       if (unlikely(err))
19904 +               goto out_unlock;
19905 +       if (!try_aopen)
19906 +               di_write_lock_parent(parent);
19907 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19908 +                                     &a->pin, &a->wr_dir_args);
19909 +       err = PTR_ERR(wh_dentry);
19910 +       if (IS_ERR(wh_dentry))
19911 +               goto out_parent;
19912 +
19913 +       btop = au_dbtop(dentry);
19914 +       sb = dentry->d_sb;
19915 +       br = au_sbr(sb, btop);
19916 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19917 +       a->h_path.mnt = au_br_mnt(br);
19918 +       h_dir = au_pinned_h_dir(&a->pin);
19919 +       switch (arg->type) {
19920 +       case Creat:
19921 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19922 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19923 +                                          arg->u.c.want_excl);
19924 +                       created = !err;
19925 +                       if (!err && try_aopen)
19926 +                               aopen->file->f_mode |= FMODE_CREATED;
19927 +               } else {
19928 +                       aopen->br = br;
19929 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19930 +                       AuDbg("err %d\n", err);
19931 +                       AuDbgFile(aopen->file);
19932 +                       created = err >= 0
19933 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19934 +               }
19935 +               break;
19936 +       case Symlink:
19937 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19938 +               created = !err;
19939 +               break;
19940 +       case Mknod:
19941 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19942 +                                 arg->u.m.dev);
19943 +               created = !err;
19944 +               break;
19945 +       default:
19946 +               BUG();
19947 +       }
19948 +       if (unlikely(err < 0))
19949 +               goto out_unpin;
19950 +
19951 +       err = epilog(dir, btop, wh_dentry, dentry);
19952 +       if (!err)
19953 +               goto out_unpin; /* success */
19954 +
19955 +       /* revert */
19956 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19957 +               /* no delegation since it is just created */
19958 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19959 +                                   /*force*/0);
19960 +               if (rerr) {
19961 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19962 +                               dentry, err, rerr);
19963 +                       err = -EIO;
19964 +               }
19965 +               au_dtime_revert(&a->dt);
19966 +       }
19967 +       if (try_aopen && h_dir->i_op->atomic_open
19968 +           && (aopen->file->f_mode & FMODE_OPENED))
19969 +               /* aopen->file is still opened */
19970 +               au_lcnt_dec(&aopen->br->br_nfiles);
19971 +
19972 +out_unpin:
19973 +       au_unpin(&a->pin);
19974 +       dput(wh_dentry);
19975 +out_parent:
19976 +       if (!try_aopen)
19977 +               di_write_unlock(parent);
19978 +out_unlock:
19979 +       if (unlikely(err)) {
19980 +               au_update_dbtop(dentry);
19981 +               d_drop(dentry);
19982 +       }
19983 +       if (!try_aopen)
19984 +               aufs_read_unlock(dentry, AuLock_DW);
19985 +out_free:
19986 +       au_kfree_rcu(a);
19987 +out:
19988 +       return err;
19989 +}
19990 +
19991 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19992 +              dev_t dev)
19993 +{
19994 +       struct simple_arg arg = {
19995 +               .type = Mknod,
19996 +               .u.m = {
19997 +                       .mode   = mode,
19998 +                       .dev    = dev
19999 +               }
20000 +       };
20001 +       return add_simple(dir, dentry, &arg);
20002 +}
20003 +
20004 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20005 +{
20006 +       struct simple_arg arg = {
20007 +               .type = Symlink,
20008 +               .u.s.symname = symname
20009 +       };
20010 +       return add_simple(dir, dentry, &arg);
20011 +}
20012 +
20013 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20014 +               bool want_excl)
20015 +{
20016 +       struct simple_arg arg = {
20017 +               .type = Creat,
20018 +               .u.c = {
20019 +                       .mode           = mode,
20020 +                       .want_excl      = want_excl
20021 +               }
20022 +       };
20023 +       return add_simple(dir, dentry, &arg);
20024 +}
20025 +
20026 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20027 +                      struct vfsub_aopen_args *aopen_args)
20028 +{
20029 +       struct simple_arg arg = {
20030 +               .type = Creat,
20031 +               .u.c = {
20032 +                       .mode           = aopen_args->create_mode,
20033 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20034 +                       .try_aopen      = true,
20035 +                       .aopen          = aopen_args
20036 +               }
20037 +       };
20038 +       return add_simple(dir, dentry, &arg);
20039 +}
20040 +
20041 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20042 +{
20043 +       int err;
20044 +       aufs_bindex_t bindex;
20045 +       struct super_block *sb;
20046 +       struct dentry *parent, *h_parent, *h_dentry;
20047 +       struct inode *h_dir, *inode;
20048 +       struct vfsmount *h_mnt;
20049 +       struct au_wr_dir_args wr_dir_args = {
20050 +               .force_btgt     = -1,
20051 +               .flags          = AuWrDir_TMPFILE
20052 +       };
20053 +
20054 +       /* copy-up may happen */
20055 +       inode_lock(dir);
20056 +
20057 +       sb = dir->i_sb;
20058 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20059 +       if (unlikely(err))
20060 +               goto out;
20061 +
20062 +       err = au_di_init(dentry);
20063 +       if (unlikely(err))
20064 +               goto out_si;
20065 +
20066 +       err = -EBUSY;
20067 +       parent = d_find_any_alias(dir);
20068 +       AuDebugOn(!parent);
20069 +       di_write_lock_parent(parent);
20070 +       if (unlikely(d_inode(parent) != dir))
20071 +               goto out_parent;
20072 +
20073 +       err = au_digen_test(parent, au_sigen(sb));
20074 +       if (unlikely(err))
20075 +               goto out_parent;
20076 +
20077 +       bindex = au_dbtop(parent);
20078 +       au_set_dbtop(dentry, bindex);
20079 +       au_set_dbbot(dentry, bindex);
20080 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20081 +       bindex = err;
20082 +       if (unlikely(err < 0))
20083 +               goto out_parent;
20084 +
20085 +       err = -EOPNOTSUPP;
20086 +       h_dir = au_h_iptr(dir, bindex);
20087 +       if (unlikely(!h_dir->i_op->tmpfile))
20088 +               goto out_parent;
20089 +
20090 +       h_mnt = au_sbr_mnt(sb, bindex);
20091 +       err = vfsub_mnt_want_write(h_mnt);
20092 +       if (unlikely(err))
20093 +               goto out_parent;
20094 +
20095 +       h_parent = au_h_dptr(parent, bindex);
20096 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20097 +       if (IS_ERR(h_dentry)) {
20098 +               err = PTR_ERR(h_dentry);
20099 +               goto out_mnt;
20100 +       }
20101 +
20102 +       au_set_dbtop(dentry, bindex);
20103 +       au_set_dbbot(dentry, bindex);
20104 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20105 +       inode = au_new_inode(dentry, /*must_new*/1);
20106 +       if (IS_ERR(inode)) {
20107 +               err = PTR_ERR(inode);
20108 +               au_set_h_dptr(dentry, bindex, NULL);
20109 +               au_set_dbtop(dentry, -1);
20110 +               au_set_dbbot(dentry, -1);
20111 +       } else {
20112 +               if (!inode->i_nlink)
20113 +                       set_nlink(inode, 1);
20114 +               d_tmpfile(dentry, inode);
20115 +               au_di(dentry)->di_tmpfile = 1;
20116 +
20117 +               /* update without i_mutex */
20118 +               if (au_ibtop(dir) == au_dbtop(dentry))
20119 +                       au_cpup_attr_timesizes(dir);
20120 +       }
20121 +       dput(h_dentry);
20122 +
20123 +out_mnt:
20124 +       vfsub_mnt_drop_write(h_mnt);
20125 +out_parent:
20126 +       di_write_unlock(parent);
20127 +       dput(parent);
20128 +       di_write_unlock(dentry);
20129 +       if (unlikely(err)) {
20130 +               au_di_fin(dentry);
20131 +               dentry->d_fsdata = NULL;
20132 +       }
20133 +out_si:
20134 +       si_read_unlock(sb);
20135 +out:
20136 +       inode_unlock(dir);
20137 +       return err;
20138 +}
20139 +
20140 +/* ---------------------------------------------------------------------- */
20141 +
20142 +struct au_link_args {
20143 +       aufs_bindex_t bdst, bsrc;
20144 +       struct au_pin pin;
20145 +       struct path h_path;
20146 +       struct dentry *src_parent, *parent;
20147 +};
20148 +
20149 +static int au_cpup_before_link(struct dentry *src_dentry,
20150 +                              struct au_link_args *a)
20151 +{
20152 +       int err;
20153 +       struct dentry *h_src_dentry;
20154 +       struct au_cp_generic cpg = {
20155 +               .dentry = src_dentry,
20156 +               .bdst   = a->bdst,
20157 +               .bsrc   = a->bsrc,
20158 +               .len    = -1,
20159 +               .pin    = &a->pin,
20160 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20161 +       };
20162 +
20163 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20164 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20165 +       if (unlikely(err))
20166 +               goto out;
20167 +
20168 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20169 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20170 +                    au_opt_udba(src_dentry->d_sb),
20171 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20172 +       if (unlikely(err))
20173 +               goto out;
20174 +
20175 +       err = au_sio_cpup_simple(&cpg);
20176 +       au_unpin(&a->pin);
20177 +
20178 +out:
20179 +       di_read_unlock(a->src_parent, AuLock_IR);
20180 +       return err;
20181 +}
20182 +
20183 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20184 +                          struct au_link_args *a)
20185 +{
20186 +       int err;
20187 +       unsigned char plink;
20188 +       aufs_bindex_t bbot;
20189 +       struct dentry *h_src_dentry;
20190 +       struct inode *h_inode, *inode, *delegated;
20191 +       struct super_block *sb;
20192 +       struct file *h_file;
20193 +
20194 +       plink = 0;
20195 +       h_inode = NULL;
20196 +       sb = src_dentry->d_sb;
20197 +       inode = d_inode(src_dentry);
20198 +       if (au_ibtop(inode) <= a->bdst)
20199 +               h_inode = au_h_iptr(inode, a->bdst);
20200 +       if (!h_inode || !h_inode->i_nlink) {
20201 +               /* copyup src_dentry as the name of dentry. */
20202 +               bbot = au_dbbot(dentry);
20203 +               if (bbot < a->bsrc)
20204 +                       au_set_dbbot(dentry, a->bsrc);
20205 +               au_set_h_dptr(dentry, a->bsrc,
20206 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20207 +               dget(a->h_path.dentry);
20208 +               au_set_h_dptr(dentry, a->bdst, NULL);
20209 +               AuDbg("temporary d_inode...\n");
20210 +               spin_lock(&dentry->d_lock);
20211 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20212 +               spin_unlock(&dentry->d_lock);
20213 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20214 +               if (IS_ERR(h_file))
20215 +                       err = PTR_ERR(h_file);
20216 +               else {
20217 +                       struct au_cp_generic cpg = {
20218 +                               .dentry = dentry,
20219 +                               .bdst   = a->bdst,
20220 +                               .bsrc   = -1,
20221 +                               .len    = -1,
20222 +                               .pin    = &a->pin,
20223 +                               .flags  = AuCpup_KEEPLINO
20224 +                       };
20225 +                       err = au_sio_cpup_simple(&cpg);
20226 +                       au_h_open_post(dentry, a->bsrc, h_file);
20227 +                       if (!err) {
20228 +                               dput(a->h_path.dentry);
20229 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20230 +                       } else
20231 +                               au_set_h_dptr(dentry, a->bdst,
20232 +                                             a->h_path.dentry);
20233 +               }
20234 +               spin_lock(&dentry->d_lock);
20235 +               dentry->d_inode = NULL; /* restore */
20236 +               spin_unlock(&dentry->d_lock);
20237 +               AuDbg("temporary d_inode...done\n");
20238 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20239 +               au_set_dbbot(dentry, bbot);
20240 +       } else {
20241 +               /* the inode of src_dentry already exists on a.bdst branch */
20242 +               h_src_dentry = d_find_alias(h_inode);
20243 +               if (!h_src_dentry && au_plink_test(inode)) {
20244 +                       plink = 1;
20245 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20246 +                       err = PTR_ERR(h_src_dentry);
20247 +                       if (IS_ERR(h_src_dentry))
20248 +                               goto out;
20249 +
20250 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20251 +                               dput(h_src_dentry);
20252 +                               h_src_dentry = NULL;
20253 +                       }
20254 +
20255 +               }
20256 +               if (h_src_dentry) {
20257 +                       delegated = NULL;
20258 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20259 +                                        &a->h_path, &delegated);
20260 +                       if (unlikely(err == -EWOULDBLOCK)) {
20261 +                               pr_warn("cannot retry for NFSv4 delegation"
20262 +                                       " for an internal link\n");
20263 +                               iput(delegated);
20264 +                       }
20265 +                       dput(h_src_dentry);
20266 +               } else {
20267 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20268 +                               h_inode->i_ino, a->bdst);
20269 +                       err = -EIO;
20270 +               }
20271 +       }
20272 +
20273 +       if (!err && !plink)
20274 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20275 +
20276 +out:
20277 +       AuTraceErr(err);
20278 +       return err;
20279 +}
20280 +
20281 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20282 +             struct dentry *dentry)
20283 +{
20284 +       int err, rerr;
20285 +       struct au_dtime dt;
20286 +       struct au_link_args *a;
20287 +       struct dentry *wh_dentry, *h_src_dentry;
20288 +       struct inode *inode, *delegated;
20289 +       struct super_block *sb;
20290 +       struct au_wr_dir_args wr_dir_args = {
20291 +               /* .force_btgt  = -1, */
20292 +               .flags          = AuWrDir_ADD_ENTRY
20293 +       };
20294 +
20295 +       IMustLock(dir);
20296 +       inode = d_inode(src_dentry);
20297 +       IMustLock(inode);
20298 +
20299 +       err = -ENOMEM;
20300 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20301 +       if (unlikely(!a))
20302 +               goto out;
20303 +
20304 +       a->parent = dentry->d_parent; /* dir inode is locked */
20305 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20306 +                                       AuLock_NOPLM | AuLock_GEN);
20307 +       if (unlikely(err))
20308 +               goto out_kfree;
20309 +       err = au_d_linkable(src_dentry);
20310 +       if (unlikely(err))
20311 +               goto out_unlock;
20312 +       err = au_d_may_add(dentry);
20313 +       if (unlikely(err))
20314 +               goto out_unlock;
20315 +
20316 +       a->src_parent = dget_parent(src_dentry);
20317 +       wr_dir_args.force_btgt = au_ibtop(inode);
20318 +
20319 +       di_write_lock_parent(a->parent);
20320 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20321 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20322 +                                     &wr_dir_args);
20323 +       err = PTR_ERR(wh_dentry);
20324 +       if (IS_ERR(wh_dentry))
20325 +               goto out_parent;
20326 +
20327 +       err = 0;
20328 +       sb = dentry->d_sb;
20329 +       a->bdst = au_dbtop(dentry);
20330 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20331 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20332 +       a->bsrc = au_ibtop(inode);
20333 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20334 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20335 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20336 +       if (!h_src_dentry) {
20337 +               a->bsrc = au_dbtop(src_dentry);
20338 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20339 +               AuDebugOn(!h_src_dentry);
20340 +       } else if (IS_ERR(h_src_dentry)) {
20341 +               err = PTR_ERR(h_src_dentry);
20342 +               goto out_parent;
20343 +       }
20344 +
20345 +       /*
20346 +        * aufs doesn't touch the credential so
20347 +        * security_dentry_create_files_as() is unnecessary.
20348 +        */
20349 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20350 +               if (a->bdst < a->bsrc
20351 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20352 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20353 +               else {
20354 +                       delegated = NULL;
20355 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20356 +                                        &a->h_path, &delegated);
20357 +                       if (unlikely(err == -EWOULDBLOCK)) {
20358 +                               pr_warn("cannot retry for NFSv4 delegation"
20359 +                                       " for an internal link\n");
20360 +                               iput(delegated);
20361 +                       }
20362 +               }
20363 +               dput(h_src_dentry);
20364 +       } else {
20365 +               /*
20366 +                * copyup src_dentry to the branch we process,
20367 +                * and then link(2) to it.
20368 +                */
20369 +               dput(h_src_dentry);
20370 +               if (a->bdst < a->bsrc
20371 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20372 +                       au_unpin(&a->pin);
20373 +                       di_write_unlock(a->parent);
20374 +                       err = au_cpup_before_link(src_dentry, a);
20375 +                       di_write_lock_parent(a->parent);
20376 +                       if (!err)
20377 +                               err = au_pin(&a->pin, dentry, a->bdst,
20378 +                                            au_opt_udba(sb),
20379 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20380 +                       if (unlikely(err))
20381 +                               goto out_wh;
20382 +               }
20383 +               if (!err) {
20384 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20385 +                       err = -ENOENT;
20386 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20387 +                               delegated = NULL;
20388 +                               err = vfsub_link(h_src_dentry,
20389 +                                                au_pinned_h_dir(&a->pin),
20390 +                                                &a->h_path, &delegated);
20391 +                               if (unlikely(err == -EWOULDBLOCK)) {
20392 +                                       pr_warn("cannot retry"
20393 +                                               " for NFSv4 delegation"
20394 +                                               " for an internal link\n");
20395 +                                       iput(delegated);
20396 +                               }
20397 +                       }
20398 +               }
20399 +       }
20400 +       if (unlikely(err))
20401 +               goto out_unpin;
20402 +
20403 +       if (wh_dentry) {
20404 +               a->h_path.dentry = wh_dentry;
20405 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20406 +                                         dentry);
20407 +               if (unlikely(err))
20408 +                       goto out_revert;
20409 +       }
20410 +
20411 +       au_dir_ts(dir, a->bdst);
20412 +       inode_inc_iversion(dir);
20413 +       inc_nlink(inode);
20414 +       inode->i_ctime = dir->i_ctime;
20415 +       d_instantiate(dentry, au_igrab(inode));
20416 +       if (d_unhashed(a->h_path.dentry))
20417 +               /* some filesystem calls d_drop() */
20418 +               d_drop(dentry);
20419 +       /* some filesystems consume an inode even hardlink */
20420 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20421 +       goto out_unpin; /* success */
20422 +
20423 +out_revert:
20424 +       /* no delegation since it is just created */
20425 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20426 +                           /*delegated*/NULL, /*force*/0);
20427 +       if (unlikely(rerr)) {
20428 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20429 +               err = -EIO;
20430 +       }
20431 +       au_dtime_revert(&dt);
20432 +out_unpin:
20433 +       au_unpin(&a->pin);
20434 +out_wh:
20435 +       dput(wh_dentry);
20436 +out_parent:
20437 +       di_write_unlock(a->parent);
20438 +       dput(a->src_parent);
20439 +out_unlock:
20440 +       if (unlikely(err)) {
20441 +               au_update_dbtop(dentry);
20442 +               d_drop(dentry);
20443 +       }
20444 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20445 +out_kfree:
20446 +       au_kfree_rcu(a);
20447 +out:
20448 +       AuTraceErr(err);
20449 +       return err;
20450 +}
20451 +
20452 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20453 +{
20454 +       int err, rerr;
20455 +       aufs_bindex_t bindex;
20456 +       unsigned char diropq;
20457 +       struct path h_path;
20458 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20459 +       struct inode *h_inode;
20460 +       struct super_block *sb;
20461 +       struct {
20462 +               struct au_pin pin;
20463 +               struct au_dtime dt;
20464 +       } *a; /* reduce the stack usage */
20465 +       struct au_wr_dir_args wr_dir_args = {
20466 +               .force_btgt     = -1,
20467 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20468 +       };
20469 +
20470 +       IMustLock(dir);
20471 +
20472 +       err = -ENOMEM;
20473 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20474 +       if (unlikely(!a))
20475 +               goto out;
20476 +
20477 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20478 +       if (unlikely(err))
20479 +               goto out_free;
20480 +       err = au_d_may_add(dentry);
20481 +       if (unlikely(err))
20482 +               goto out_unlock;
20483 +
20484 +       parent = dentry->d_parent; /* dir inode is locked */
20485 +       di_write_lock_parent(parent);
20486 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20487 +                                     &a->pin, &wr_dir_args);
20488 +       err = PTR_ERR(wh_dentry);
20489 +       if (IS_ERR(wh_dentry))
20490 +               goto out_parent;
20491 +
20492 +       sb = dentry->d_sb;
20493 +       bindex = au_dbtop(dentry);
20494 +       h_path.dentry = au_h_dptr(dentry, bindex);
20495 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20496 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20497 +       if (unlikely(err))
20498 +               goto out_unpin;
20499 +
20500 +       /* make the dir opaque */
20501 +       diropq = 0;
20502 +       h_inode = d_inode(h_path.dentry);
20503 +       if (wh_dentry
20504 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20505 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20506 +               opq_dentry = au_diropq_create(dentry, bindex);
20507 +               inode_unlock(h_inode);
20508 +               err = PTR_ERR(opq_dentry);
20509 +               if (IS_ERR(opq_dentry))
20510 +                       goto out_dir;
20511 +               dput(opq_dentry);
20512 +               diropq = 1;
20513 +       }
20514 +
20515 +       err = epilog(dir, bindex, wh_dentry, dentry);
20516 +       if (!err) {
20517 +               inc_nlink(dir);
20518 +               goto out_unpin; /* success */
20519 +       }
20520 +
20521 +       /* revert */
20522 +       if (diropq) {
20523 +               AuLabel(revert opq);
20524 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20525 +               rerr = au_diropq_remove(dentry, bindex);
20526 +               inode_unlock(h_inode);
20527 +               if (rerr) {
20528 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20529 +                               dentry, err, rerr);
20530 +                       err = -EIO;
20531 +               }
20532 +       }
20533 +
20534 +out_dir:
20535 +       AuLabel(revert dir);
20536 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20537 +       if (rerr) {
20538 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20539 +                       dentry, err, rerr);
20540 +               err = -EIO;
20541 +       }
20542 +       au_dtime_revert(&a->dt);
20543 +out_unpin:
20544 +       au_unpin(&a->pin);
20545 +       dput(wh_dentry);
20546 +out_parent:
20547 +       di_write_unlock(parent);
20548 +out_unlock:
20549 +       if (unlikely(err)) {
20550 +               au_update_dbtop(dentry);
20551 +               d_drop(dentry);
20552 +       }
20553 +       aufs_read_unlock(dentry, AuLock_DW);
20554 +out_free:
20555 +       au_kfree_rcu(a);
20556 +out:
20557 +       return err;
20558 +}
20559 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20560 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20561 +++ linux/fs/aufs/i_op.c        2020-08-03 09:14:46.095748745 +0200
20562 @@ -0,0 +1,1502 @@
20563 +// SPDX-License-Identifier: GPL-2.0
20564 +/*
20565 + * Copyright (C) 2005-2020 Junjiro R. Okajima
20566 + *
20567 + * This program, aufs is free software; you can redistribute it and/or modify
20568 + * it under the terms of the GNU General Public License as published by
20569 + * the Free Software Foundation; either version 2 of the License, or
20570 + * (at your option) any later version.
20571 + *
20572 + * This program is distributed in the hope that it will be useful,
20573 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20574 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20575 + * GNU General Public License for more details.
20576 + *
20577 + * You should have received a copy of the GNU General Public License
20578 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20579 + */
20580 +
20581 +/*
20582 + * inode operations (except add/del/rename)
20583 + */
20584 +
20585 +#include <linux/device_cgroup.h>
20586 +#include <linux/fs_stack.h>
20587 +#include <linux/iversion.h>
20588 +#include <linux/namei.h>
20589 +#include <linux/security.h>
20590 +#include "aufs.h"
20591 +
20592 +static int h_permission(struct inode *h_inode, int mask,
20593 +                       struct path *h_path, int brperm)
20594 +{
20595 +       int err;
20596 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20597 +
20598 +       err = -EPERM;
20599 +       if (write_mask && IS_IMMUTABLE(h_inode))
20600 +               goto out;
20601 +
20602 +       err = -EACCES;
20603 +       if (((mask & MAY_EXEC)
20604 +            && S_ISREG(h_inode->i_mode)
20605 +            && (path_noexec(h_path)
20606 +                || !(h_inode->i_mode & 0111))))
20607 +               goto out;
20608 +
20609 +       /*
20610 +        * - skip the lower fs test in the case of write to ro branch.
20611 +        * - nfs dir permission write check is optimized, but a policy for
20612 +        *   link/rename requires a real check.
20613 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20614 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20615 +        */
20616 +       if ((write_mask && !au_br_writable(brperm))
20617 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20618 +               && write_mask && !(mask & MAY_READ))
20619 +           || !h_inode->i_op->permission) {
20620 +               /* AuLabel(generic_permission); */
20621 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20622 +               err = generic_permission(h_inode, mask);
20623 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20624 +                       err = h_inode->i_op->permission(h_inode, mask);
20625 +               AuTraceErr(err);
20626 +       } else {
20627 +               /* AuLabel(h_inode->permission); */
20628 +               err = h_inode->i_op->permission(h_inode, mask);
20629 +               AuTraceErr(err);
20630 +       }
20631 +
20632 +       if (!err)
20633 +               err = devcgroup_inode_permission(h_inode, mask);
20634 +       if (!err)
20635 +               err = security_inode_permission(h_inode, mask);
20636 +
20637 +out:
20638 +       return err;
20639 +}
20640 +
20641 +static int aufs_permission(struct inode *inode, int mask)
20642 +{
20643 +       int err;
20644 +       aufs_bindex_t bindex, bbot;
20645 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20646 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20647 +       struct inode *h_inode;
20648 +       struct super_block *sb;
20649 +       struct au_branch *br;
20650 +
20651 +       /* todo: support rcu-walk? */
20652 +       if (mask & MAY_NOT_BLOCK)
20653 +               return -ECHILD;
20654 +
20655 +       sb = inode->i_sb;
20656 +       si_read_lock(sb, AuLock_FLUSH);
20657 +       ii_read_lock_child(inode);
20658 +#if 0 /* reserved for future use */
20659 +       /*
20660 +        * This test may be rather 'too much' since the test is essentially done
20661 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20662 +        * generation doesn't match to the superblock's here.  But it isn't a
20663 +        * big deal I suppose.
20664 +        */
20665 +       err = au_iigen_test(inode, au_sigen(sb));
20666 +       if (unlikely(err))
20667 +               goto out;
20668 +#endif
20669 +
20670 +       if (!isdir
20671 +           || write_mask
20672 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20673 +               err = au_busy_or_stale();
20674 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20675 +               if (unlikely(!h_inode
20676 +                            || (h_inode->i_mode & S_IFMT)
20677 +                            != (inode->i_mode & S_IFMT)))
20678 +                       goto out;
20679 +
20680 +               err = 0;
20681 +               bindex = au_ibtop(inode);
20682 +               br = au_sbr(sb, bindex);
20683 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20684 +               if (write_mask
20685 +                   && !err
20686 +                   && !special_file(h_inode->i_mode)) {
20687 +                       /* test whether the upper writable branch exists */
20688 +                       err = -EROFS;
20689 +                       for (; bindex >= 0; bindex--)
20690 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20691 +                                       err = 0;
20692 +                                       break;
20693 +                               }
20694 +               }
20695 +               goto out;
20696 +       }
20697 +
20698 +       /* non-write to dir */
20699 +       err = 0;
20700 +       bbot = au_ibbot(inode);
20701 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20702 +               h_inode = au_h_iptr(inode, bindex);
20703 +               if (h_inode) {
20704 +                       err = au_busy_or_stale();
20705 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20706 +                               break;
20707 +
20708 +                       br = au_sbr(sb, bindex);
20709 +                       err = h_permission(h_inode, mask, &br->br_path,
20710 +                                          br->br_perm);
20711 +               }
20712 +       }
20713 +
20714 +out:
20715 +       ii_read_unlock(inode);
20716 +       si_read_unlock(sb);
20717 +       return err;
20718 +}
20719 +
20720 +/* ---------------------------------------------------------------------- */
20721 +
20722 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20723 +                                 unsigned int flags)
20724 +{
20725 +       struct dentry *ret, *parent;
20726 +       struct inode *inode;
20727 +       struct super_block *sb;
20728 +       int err, npositive;
20729 +
20730 +       IMustLock(dir);
20731 +
20732 +       /* todo: support rcu-walk? */
20733 +       ret = ERR_PTR(-ECHILD);
20734 +       if (flags & LOOKUP_RCU)
20735 +               goto out;
20736 +
20737 +       ret = ERR_PTR(-ENAMETOOLONG);
20738 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20739 +               goto out;
20740 +
20741 +       sb = dir->i_sb;
20742 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20743 +       ret = ERR_PTR(err);
20744 +       if (unlikely(err))
20745 +               goto out;
20746 +
20747 +       err = au_di_init(dentry);
20748 +       ret = ERR_PTR(err);
20749 +       if (unlikely(err))
20750 +               goto out_si;
20751 +
20752 +       inode = NULL;
20753 +       npositive = 0; /* suppress a warning */
20754 +       parent = dentry->d_parent; /* dir inode is locked */
20755 +       di_read_lock_parent(parent, AuLock_IR);
20756 +       err = au_alive_dir(parent);
20757 +       if (!err)
20758 +               err = au_digen_test(parent, au_sigen(sb));
20759 +       if (!err) {
20760 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20761 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20762 +                                          AuLkup_ALLOW_NEG);
20763 +               err = npositive;
20764 +       }
20765 +       di_read_unlock(parent, AuLock_IR);
20766 +       ret = ERR_PTR(err);
20767 +       if (unlikely(err < 0))
20768 +               goto out_unlock;
20769 +
20770 +       if (npositive) {
20771 +               inode = au_new_inode(dentry, /*must_new*/0);
20772 +               if (IS_ERR(inode)) {
20773 +                       ret = (void *)inode;
20774 +                       inode = NULL;
20775 +                       goto out_unlock;
20776 +               }
20777 +       }
20778 +
20779 +       if (inode)
20780 +               atomic_inc(&inode->i_count);
20781 +       ret = d_splice_alias(inode, dentry);
20782 +#if 0 /* reserved for future use */
20783 +       if (unlikely(d_need_lookup(dentry))) {
20784 +               spin_lock(&dentry->d_lock);
20785 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20786 +               spin_unlock(&dentry->d_lock);
20787 +       } else
20788 +#endif
20789 +       if (inode) {
20790 +               if (!IS_ERR(ret)) {
20791 +                       iput(inode);
20792 +                       if (ret && ret != dentry)
20793 +                               ii_write_unlock(inode);
20794 +               } else {
20795 +                       ii_write_unlock(inode);
20796 +                       iput(inode);
20797 +                       inode = NULL;
20798 +               }
20799 +       }
20800 +
20801 +out_unlock:
20802 +       di_write_unlock(dentry);
20803 +out_si:
20804 +       si_read_unlock(sb);
20805 +out:
20806 +       return ret;
20807 +}
20808 +
20809 +/* ---------------------------------------------------------------------- */
20810 +
20811 +/*
20812 + * very dirty and complicated aufs ->atomic_open().
20813 + * aufs_atomic_open()
20814 + * + au_aopen_or_create()
20815 + *   + add_simple()
20816 + *     + vfsub_atomic_open()
20817 + *       + branch fs ->atomic_open()
20818 + *        may call the actual 'open' for h_file
20819 + *       + inc br_nfiles only if opened
20820 + * + au_aopen_no_open() or au_aopen_do_open()
20821 + *
20822 + * au_aopen_do_open()
20823 + * + finish_open()
20824 + *   + au_do_aopen()
20825 + *     + au_do_open() the body of all 'open'
20826 + *       + au_do_open_nondir()
20827 + *        set the passed h_file
20828 + *
20829 + * au_aopen_no_open()
20830 + * + finish_no_open()
20831 + */
20832 +
20833 +struct aopen_node {
20834 +       struct hlist_bl_node hblist;
20835 +       struct file *file, *h_file;
20836 +};
20837 +
20838 +static int au_do_aopen(struct inode *inode, struct file *file)
20839 +{
20840 +       struct hlist_bl_head *aopen;
20841 +       struct hlist_bl_node *pos;
20842 +       struct aopen_node *node;
20843 +       struct au_do_open_args args = {
20844 +               .aopen  = 1,
20845 +               .open   = au_do_open_nondir
20846 +       };
20847 +
20848 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20849 +       hlist_bl_lock(aopen);
20850 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20851 +               if (node->file == file) {
20852 +                       args.h_file = node->h_file;
20853 +                       break;
20854 +               }
20855 +       hlist_bl_unlock(aopen);
20856 +       /* AuDebugOn(!args.h_file); */
20857 +
20858 +       return au_do_open(file, &args);
20859 +}
20860 +
20861 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20862 +                           struct aopen_node *aopen_node)
20863 +{
20864 +       int err;
20865 +       struct hlist_bl_head *aopen;
20866 +
20867 +       AuLabel(here);
20868 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20869 +       au_hbl_add(&aopen_node->hblist, aopen);
20870 +       err = finish_open(file, dentry, au_do_aopen);
20871 +       au_hbl_del(&aopen_node->hblist, aopen);
20872 +       /* AuDbgFile(file); */
20873 +       AuDbg("%pd%s%s\n", dentry,
20874 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20875 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20876 +
20877 +       AuTraceErr(err);
20878 +       return err;
20879 +}
20880 +
20881 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20882 +{
20883 +       int err;
20884 +
20885 +       AuLabel(here);
20886 +       dget(dentry);
20887 +       err = finish_no_open(file, dentry);
20888 +
20889 +       AuTraceErr(err);
20890 +       return err;
20891 +}
20892 +
20893 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20894 +                           struct file *file, unsigned int open_flag,
20895 +                           umode_t create_mode)
20896 +{
20897 +       int err, did_open;
20898 +       unsigned int lkup_flags;
20899 +       aufs_bindex_t bindex;
20900 +       struct super_block *sb;
20901 +       struct dentry *parent, *d;
20902 +       struct vfsub_aopen_args args = {
20903 +               .open_flag      = open_flag,
20904 +               .create_mode    = create_mode
20905 +       };
20906 +       struct aopen_node aopen_node = {
20907 +               .file   = file
20908 +       };
20909 +
20910 +       IMustLock(dir);
20911 +       AuDbg("open_flag 0%o\n", open_flag);
20912 +       AuDbgDentry(dentry);
20913 +
20914 +       err = 0;
20915 +       if (!au_di(dentry)) {
20916 +               lkup_flags = LOOKUP_OPEN;
20917 +               if (open_flag & O_CREAT)
20918 +                       lkup_flags |= LOOKUP_CREATE;
20919 +               d = aufs_lookup(dir, dentry, lkup_flags);
20920 +               if (IS_ERR(d)) {
20921 +                       err = PTR_ERR(d);
20922 +                       AuTraceErr(err);
20923 +                       goto out;
20924 +               } else if (d) {
20925 +                       /*
20926 +                        * obsoleted dentry found.
20927 +                        * another error will be returned later.
20928 +                        */
20929 +                       d_drop(d);
20930 +                       AuDbgDentry(d);
20931 +                       dput(d);
20932 +               }
20933 +               AuDbgDentry(dentry);
20934 +       }
20935 +
20936 +       if (d_is_positive(dentry)
20937 +           || d_unhashed(dentry)
20938 +           || d_unlinked(dentry)
20939 +           || !(open_flag & O_CREAT)) {
20940 +               err = au_aopen_no_open(file, dentry);
20941 +               goto out; /* success */
20942 +       }
20943 +
20944 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20945 +       if (unlikely(err))
20946 +               goto out;
20947 +
20948 +       sb = dentry->d_sb;
20949 +       parent = dentry->d_parent;      /* dir is locked */
20950 +       di_write_lock_parent(parent);
20951 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20952 +       if (unlikely(err < 0))
20953 +               goto out_parent;
20954 +
20955 +       AuDbgDentry(dentry);
20956 +       if (d_is_positive(dentry)) {
20957 +               err = au_aopen_no_open(file, dentry);
20958 +               goto out_parent; /* success */
20959 +       }
20960 +
20961 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20962 +       err = PTR_ERR(args.file);
20963 +       if (IS_ERR(args.file))
20964 +               goto out_parent;
20965 +
20966 +       bindex = au_dbtop(dentry);
20967 +       err = au_aopen_or_create(dir, dentry, &args);
20968 +       AuTraceErr(err);
20969 +       AuDbgFile(args.file);
20970 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20971 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20972 +       if (!did_open) {
20973 +               fput(args.file);
20974 +               args.file = NULL;
20975 +       }
20976 +       di_write_unlock(parent);
20977 +       di_write_unlock(dentry);
20978 +       if (unlikely(err < 0)) {
20979 +               if (args.file)
20980 +                       fput(args.file);
20981 +               goto out_sb;
20982 +       }
20983 +
20984 +       if (!did_open)
20985 +               err = au_aopen_no_open(file, dentry);
20986 +       else {
20987 +               aopen_node.h_file = args.file;
20988 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20989 +       }
20990 +       if (unlikely(err < 0)) {
20991 +               if (args.file)
20992 +                       fput(args.file);
20993 +               if (did_open)
20994 +                       au_lcnt_dec(&args.br->br_nfiles);
20995 +       }
20996 +       goto out_sb; /* success */
20997 +
20998 +out_parent:
20999 +       di_write_unlock(parent);
21000 +       di_write_unlock(dentry);
21001 +out_sb:
21002 +       si_read_unlock(sb);
21003 +out:
21004 +       AuTraceErr(err);
21005 +       AuDbgFile(file);
21006 +       return err;
21007 +}
21008 +
21009 +
21010 +/* ---------------------------------------------------------------------- */
21011 +
21012 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21013 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21014 +                         aufs_bindex_t btop)
21015 +{
21016 +       int err;
21017 +       struct dentry *h_parent;
21018 +       struct inode *h_dir;
21019 +
21020 +       if (add_entry)
21021 +               IMustLock(d_inode(parent));
21022 +       else
21023 +               di_write_lock_parent(parent);
21024 +
21025 +       err = 0;
21026 +       if (!au_h_dptr(parent, bcpup)) {
21027 +               if (btop > bcpup)
21028 +                       err = au_cpup_dirs(dentry, bcpup);
21029 +               else if (btop < bcpup)
21030 +                       err = au_cpdown_dirs(dentry, bcpup);
21031 +               else
21032 +                       BUG();
21033 +       }
21034 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21035 +               h_parent = au_h_dptr(parent, bcpup);
21036 +               h_dir = d_inode(h_parent);
21037 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21038 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21039 +               /* todo: no unlock here */
21040 +               inode_unlock_shared(h_dir);
21041 +
21042 +               AuDbg("bcpup %d\n", bcpup);
21043 +               if (!err) {
21044 +                       if (d_really_is_negative(dentry))
21045 +                               au_set_h_dptr(dentry, btop, NULL);
21046 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21047 +               }
21048 +       }
21049 +
21050 +       if (!add_entry)
21051 +               di_write_unlock(parent);
21052 +       if (!err)
21053 +               err = bcpup; /* success */
21054 +
21055 +       AuTraceErr(err);
21056 +       return err;
21057 +}
21058 +
21059 +/*
21060 + * decide the branch and the parent dir where we will create a new entry.
21061 + * returns new bindex or an error.
21062 + * copyup the parent dir if needed.
21063 + */
21064 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21065 +             struct au_wr_dir_args *args)
21066 +{
21067 +       int err;
21068 +       unsigned int flags;
21069 +       aufs_bindex_t bcpup, btop, src_btop;
21070 +       const unsigned char add_entry
21071 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21072 +               | au_ftest_wrdir(args->flags, TMPFILE);
21073 +       struct super_block *sb;
21074 +       struct dentry *parent;
21075 +       struct au_sbinfo *sbinfo;
21076 +
21077 +       sb = dentry->d_sb;
21078 +       sbinfo = au_sbi(sb);
21079 +       parent = dget_parent(dentry);
21080 +       btop = au_dbtop(dentry);
21081 +       bcpup = btop;
21082 +       if (args->force_btgt < 0) {
21083 +               if (src_dentry) {
21084 +                       src_btop = au_dbtop(src_dentry);
21085 +                       if (src_btop < btop)
21086 +                               bcpup = src_btop;
21087 +               } else if (add_entry) {
21088 +                       flags = 0;
21089 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21090 +                               au_fset_wbr(flags, DIR);
21091 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21092 +                       bcpup = err;
21093 +               }
21094 +
21095 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21096 +                       if (add_entry)
21097 +                               err = AuWbrCopyup(sbinfo, dentry);
21098 +                       else {
21099 +                               if (!IS_ROOT(dentry)) {
21100 +                                       di_read_lock_parent(parent, !AuLock_IR);
21101 +                                       err = AuWbrCopyup(sbinfo, dentry);
21102 +                                       di_read_unlock(parent, !AuLock_IR);
21103 +                               } else
21104 +                                       err = AuWbrCopyup(sbinfo, dentry);
21105 +                       }
21106 +                       bcpup = err;
21107 +                       if (unlikely(err < 0))
21108 +                               goto out;
21109 +               }
21110 +       } else {
21111 +               bcpup = args->force_btgt;
21112 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21113 +       }
21114 +
21115 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21116 +       err = bcpup;
21117 +       if (bcpup == btop)
21118 +               goto out; /* success */
21119 +
21120 +       /* copyup the new parent into the branch we process */
21121 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21122 +       if (err >= 0) {
21123 +               if (d_really_is_negative(dentry)) {
21124 +                       au_set_h_dptr(dentry, btop, NULL);
21125 +                       au_set_dbtop(dentry, bcpup);
21126 +                       au_set_dbbot(dentry, bcpup);
21127 +               }
21128 +               AuDebugOn(add_entry
21129 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21130 +                         && !au_h_dptr(dentry, bcpup));
21131 +       }
21132 +
21133 +out:
21134 +       dput(parent);
21135 +       return err;
21136 +}
21137 +
21138 +/* ---------------------------------------------------------------------- */
21139 +
21140 +void au_pin_hdir_unlock(struct au_pin *p)
21141 +{
21142 +       if (p->hdir)
21143 +               au_hn_inode_unlock(p->hdir);
21144 +}
21145 +
21146 +int au_pin_hdir_lock(struct au_pin *p)
21147 +{
21148 +       int err;
21149 +
21150 +       err = 0;
21151 +       if (!p->hdir)
21152 +               goto out;
21153 +
21154 +       /* even if an error happens later, keep this lock */
21155 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21156 +
21157 +       err = -EBUSY;
21158 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21159 +               goto out;
21160 +
21161 +       err = 0;
21162 +       if (p->h_dentry)
21163 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21164 +                                 p->h_parent, p->br);
21165 +
21166 +out:
21167 +       return err;
21168 +}
21169 +
21170 +int au_pin_hdir_relock(struct au_pin *p)
21171 +{
21172 +       int err, i;
21173 +       struct inode *h_i;
21174 +       struct dentry *h_d[] = {
21175 +               p->h_dentry,
21176 +               p->h_parent
21177 +       };
21178 +
21179 +       err = au_pin_hdir_lock(p);
21180 +       if (unlikely(err))
21181 +               goto out;
21182 +
21183 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21184 +               if (!h_d[i])
21185 +                       continue;
21186 +               if (d_is_positive(h_d[i])) {
21187 +                       h_i = d_inode(h_d[i]);
21188 +                       err = !h_i->i_nlink;
21189 +               }
21190 +       }
21191 +
21192 +out:
21193 +       return err;
21194 +}
21195 +
21196 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21197 +{
21198 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21199 +}
21200 +
21201 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21202 +{
21203 +       if (p->hdir) {
21204 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21205 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21206 +               au_pin_hdir_set_owner(p, current);
21207 +       }
21208 +}
21209 +
21210 +void au_pin_hdir_release(struct au_pin *p)
21211 +{
21212 +       if (p->hdir) {
21213 +               au_pin_hdir_set_owner(p, p->task);
21214 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21215 +       }
21216 +}
21217 +
21218 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21219 +{
21220 +       if (pin && pin->parent)
21221 +               return au_h_dptr(pin->parent, pin->bindex);
21222 +       return NULL;
21223 +}
21224 +
21225 +void au_unpin(struct au_pin *p)
21226 +{
21227 +       if (p->hdir)
21228 +               au_pin_hdir_unlock(p);
21229 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21230 +               vfsub_mnt_drop_write(p->h_mnt);
21231 +       if (!p->hdir)
21232 +               return;
21233 +
21234 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21235 +               di_read_unlock(p->parent, AuLock_IR);
21236 +       iput(p->hdir->hi_inode);
21237 +       dput(p->parent);
21238 +       p->parent = NULL;
21239 +       p->hdir = NULL;
21240 +       p->h_mnt = NULL;
21241 +       /* do not clear p->task */
21242 +}
21243 +
21244 +int au_do_pin(struct au_pin *p)
21245 +{
21246 +       int err;
21247 +       struct super_block *sb;
21248 +       struct inode *h_dir;
21249 +
21250 +       err = 0;
21251 +       sb = p->dentry->d_sb;
21252 +       p->br = au_sbr(sb, p->bindex);
21253 +       if (IS_ROOT(p->dentry)) {
21254 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21255 +                       p->h_mnt = au_br_mnt(p->br);
21256 +                       err = vfsub_mnt_want_write(p->h_mnt);
21257 +                       if (unlikely(err)) {
21258 +                               au_fclr_pin(p->flags, MNT_WRITE);
21259 +                               goto out_err;
21260 +                       }
21261 +               }
21262 +               goto out;
21263 +       }
21264 +
21265 +       p->h_dentry = NULL;
21266 +       if (p->bindex <= au_dbbot(p->dentry))
21267 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21268 +
21269 +       p->parent = dget_parent(p->dentry);
21270 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21271 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21272 +
21273 +       h_dir = NULL;
21274 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21275 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21276 +       if (p->hdir)
21277 +               h_dir = p->hdir->hi_inode;
21278 +
21279 +       /*
21280 +        * udba case, or
21281 +        * if DI_LOCKED is not set, then p->parent may be different
21282 +        * and h_parent can be NULL.
21283 +        */
21284 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21285 +               err = -EBUSY;
21286 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21287 +                       di_read_unlock(p->parent, AuLock_IR);
21288 +               dput(p->parent);
21289 +               p->parent = NULL;
21290 +               goto out_err;
21291 +       }
21292 +
21293 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21294 +               p->h_mnt = au_br_mnt(p->br);
21295 +               err = vfsub_mnt_want_write(p->h_mnt);
21296 +               if (unlikely(err)) {
21297 +                       au_fclr_pin(p->flags, MNT_WRITE);
21298 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21299 +                               di_read_unlock(p->parent, AuLock_IR);
21300 +                       dput(p->parent);
21301 +                       p->parent = NULL;
21302 +                       goto out_err;
21303 +               }
21304 +       }
21305 +
21306 +       au_igrab(h_dir);
21307 +       err = au_pin_hdir_lock(p);
21308 +       if (!err)
21309 +               goto out; /* success */
21310 +
21311 +       au_unpin(p);
21312 +
21313 +out_err:
21314 +       pr_err("err %d\n", err);
21315 +       err = au_busy_or_stale();
21316 +out:
21317 +       return err;
21318 +}
21319 +
21320 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21321 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21322 +                unsigned int udba, unsigned char flags)
21323 +{
21324 +       p->dentry = dentry;
21325 +       p->udba = udba;
21326 +       p->lsc_di = lsc_di;
21327 +       p->lsc_hi = lsc_hi;
21328 +       p->flags = flags;
21329 +       p->bindex = bindex;
21330 +
21331 +       p->parent = NULL;
21332 +       p->hdir = NULL;
21333 +       p->h_mnt = NULL;
21334 +
21335 +       p->h_dentry = NULL;
21336 +       p->h_parent = NULL;
21337 +       p->br = NULL;
21338 +       p->task = current;
21339 +}
21340 +
21341 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21342 +          unsigned int udba, unsigned char flags)
21343 +{
21344 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21345 +                   udba, flags);
21346 +       return au_do_pin(pin);
21347 +}
21348 +
21349 +/* ---------------------------------------------------------------------- */
21350 +
21351 +/*
21352 + * ->setattr() and ->getattr() are called in various cases.
21353 + * chmod, stat: dentry is revalidated.
21354 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21355 + *               unhashed.
21356 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21357 + */
21358 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21359 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21360 +{
21361 +       int err;
21362 +       struct dentry *parent;
21363 +
21364 +       err = 0;
21365 +       if (au_digen_test(dentry, sigen)) {
21366 +               parent = dget_parent(dentry);
21367 +               di_read_lock_parent(parent, AuLock_IR);
21368 +               err = au_refresh_dentry(dentry, parent);
21369 +               di_read_unlock(parent, AuLock_IR);
21370 +               dput(parent);
21371 +       }
21372 +
21373 +       AuTraceErr(err);
21374 +       return err;
21375 +}
21376 +
21377 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21378 +                    struct au_icpup_args *a)
21379 +{
21380 +       int err;
21381 +       loff_t sz;
21382 +       aufs_bindex_t btop, ibtop;
21383 +       struct dentry *hi_wh, *parent;
21384 +       struct inode *inode;
21385 +       struct au_wr_dir_args wr_dir_args = {
21386 +               .force_btgt     = -1,
21387 +               .flags          = 0
21388 +       };
21389 +
21390 +       if (d_is_dir(dentry))
21391 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21392 +       /* plink or hi_wh() case */
21393 +       btop = au_dbtop(dentry);
21394 +       inode = d_inode(dentry);
21395 +       ibtop = au_ibtop(inode);
21396 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21397 +               wr_dir_args.force_btgt = ibtop;
21398 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21399 +       if (unlikely(err < 0))
21400 +               goto out;
21401 +       a->btgt = err;
21402 +       if (err != btop)
21403 +               au_fset_icpup(a->flags, DID_CPUP);
21404 +
21405 +       err = 0;
21406 +       a->pin_flags = AuPin_MNT_WRITE;
21407 +       parent = NULL;
21408 +       if (!IS_ROOT(dentry)) {
21409 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21410 +               parent = dget_parent(dentry);
21411 +               di_write_lock_parent(parent);
21412 +       }
21413 +
21414 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21415 +       if (unlikely(err))
21416 +               goto out_parent;
21417 +
21418 +       sz = -1;
21419 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21420 +       a->h_inode = d_inode(a->h_path.dentry);
21421 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21422 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21423 +               if (ia->ia_size < i_size_read(a->h_inode))
21424 +                       sz = ia->ia_size;
21425 +               inode_unlock_shared(a->h_inode);
21426 +       }
21427 +
21428 +       hi_wh = NULL;
21429 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21430 +               hi_wh = au_hi_wh(inode, a->btgt);
21431 +               if (!hi_wh) {
21432 +                       struct au_cp_generic cpg = {
21433 +                               .dentry = dentry,
21434 +                               .bdst   = a->btgt,
21435 +                               .bsrc   = -1,
21436 +                               .len    = sz,
21437 +                               .pin    = &a->pin
21438 +                       };
21439 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21440 +                       if (unlikely(err))
21441 +                               goto out_unlock;
21442 +                       hi_wh = au_hi_wh(inode, a->btgt);
21443 +                       /* todo: revalidate hi_wh? */
21444 +               }
21445 +       }
21446 +
21447 +       if (parent) {
21448 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21449 +               di_downgrade_lock(parent, AuLock_IR);
21450 +               dput(parent);
21451 +               parent = NULL;
21452 +       }
21453 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21454 +               goto out; /* success */
21455 +
21456 +       if (!d_unhashed(dentry)) {
21457 +               struct au_cp_generic cpg = {
21458 +                       .dentry = dentry,
21459 +                       .bdst   = a->btgt,
21460 +                       .bsrc   = btop,
21461 +                       .len    = sz,
21462 +                       .pin    = &a->pin,
21463 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21464 +               };
21465 +               err = au_sio_cpup_simple(&cpg);
21466 +               if (!err)
21467 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21468 +       } else if (!hi_wh)
21469 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21470 +       else
21471 +               a->h_path.dentry = hi_wh; /* do not dget here */
21472 +
21473 +out_unlock:
21474 +       a->h_inode = d_inode(a->h_path.dentry);
21475 +       if (!err)
21476 +               goto out; /* success */
21477 +       au_unpin(&a->pin);
21478 +out_parent:
21479 +       if (parent) {
21480 +               di_write_unlock(parent);
21481 +               dput(parent);
21482 +       }
21483 +out:
21484 +       if (!err)
21485 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21486 +       return err;
21487 +}
21488 +
21489 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21490 +{
21491 +       int err;
21492 +       struct inode *inode, *delegated;
21493 +       struct super_block *sb;
21494 +       struct file *file;
21495 +       struct au_icpup_args *a;
21496 +
21497 +       inode = d_inode(dentry);
21498 +       IMustLock(inode);
21499 +
21500 +       err = setattr_prepare(dentry, ia);
21501 +       if (unlikely(err))
21502 +               goto out;
21503 +
21504 +       err = -ENOMEM;
21505 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21506 +       if (unlikely(!a))
21507 +               goto out;
21508 +
21509 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21510 +               ia->ia_valid &= ~ATTR_MODE;
21511 +
21512 +       file = NULL;
21513 +       sb = dentry->d_sb;
21514 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21515 +       if (unlikely(err))
21516 +               goto out_kfree;
21517 +
21518 +       if (ia->ia_valid & ATTR_FILE) {
21519 +               /* currently ftruncate(2) only */
21520 +               AuDebugOn(!d_is_reg(dentry));
21521 +               file = ia->ia_file;
21522 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21523 +                                           /*fi_lsc*/0);
21524 +               if (unlikely(err))
21525 +                       goto out_si;
21526 +               ia->ia_file = au_hf_top(file);
21527 +               a->udba = AuOpt_UDBA_NONE;
21528 +       } else {
21529 +               /* fchmod() doesn't pass ia_file */
21530 +               a->udba = au_opt_udba(sb);
21531 +               di_write_lock_child(dentry);
21532 +               /* no d_unlinked(), to set UDBA_NONE for root */
21533 +               if (d_unhashed(dentry))
21534 +                       a->udba = AuOpt_UDBA_NONE;
21535 +               if (a->udba != AuOpt_UDBA_NONE) {
21536 +                       AuDebugOn(IS_ROOT(dentry));
21537 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21538 +                       if (unlikely(err))
21539 +                               goto out_dentry;
21540 +               }
21541 +       }
21542 +
21543 +       err = au_pin_and_icpup(dentry, ia, a);
21544 +       if (unlikely(err < 0))
21545 +               goto out_dentry;
21546 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21547 +               ia->ia_file = NULL;
21548 +               ia->ia_valid &= ~ATTR_FILE;
21549 +       }
21550 +
21551 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21552 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21553 +           == (ATTR_MODE | ATTR_CTIME)) {
21554 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21555 +               if (unlikely(err))
21556 +                       goto out_unlock;
21557 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21558 +                  && (ia->ia_valid & ATTR_CTIME)) {
21559 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21560 +               if (unlikely(err))
21561 +                       goto out_unlock;
21562 +       }
21563 +
21564 +       if (ia->ia_valid & ATTR_SIZE) {
21565 +               struct file *f;
21566 +
21567 +               if (ia->ia_size < i_size_read(inode))
21568 +                       /* unmap only */
21569 +                       truncate_setsize(inode, ia->ia_size);
21570 +
21571 +               f = NULL;
21572 +               if (ia->ia_valid & ATTR_FILE)
21573 +                       f = ia->ia_file;
21574 +               inode_unlock(a->h_inode);
21575 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21576 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21577 +       } else {
21578 +               delegated = NULL;
21579 +               while (1) {
21580 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21581 +                       if (delegated) {
21582 +                               err = break_deleg_wait(&delegated);
21583 +                               if (!err)
21584 +                                       continue;
21585 +                       }
21586 +                       break;
21587 +               }
21588 +       }
21589 +       /*
21590 +        * regardless aufs 'acl' option setting.
21591 +        * why don't all acl-aware fs call this func from their ->setattr()?
21592 +        */
21593 +       if (!err && (ia->ia_valid & ATTR_MODE))
21594 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21595 +       if (!err)
21596 +               au_cpup_attr_changeable(inode);
21597 +
21598 +out_unlock:
21599 +       inode_unlock(a->h_inode);
21600 +       au_unpin(&a->pin);
21601 +       if (unlikely(err))
21602 +               au_update_dbtop(dentry);
21603 +out_dentry:
21604 +       di_write_unlock(dentry);
21605 +       if (file) {
21606 +               fi_write_unlock(file);
21607 +               ia->ia_file = file;
21608 +               ia->ia_valid |= ATTR_FILE;
21609 +       }
21610 +out_si:
21611 +       si_read_unlock(sb);
21612 +out_kfree:
21613 +       au_kfree_rcu(a);
21614 +out:
21615 +       AuTraceErr(err);
21616 +       return err;
21617 +}
21618 +
21619 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21620 +static int au_h_path_to_set_attr(struct dentry *dentry,
21621 +                                struct au_icpup_args *a, struct path *h_path)
21622 +{
21623 +       int err;
21624 +       struct super_block *sb;
21625 +
21626 +       sb = dentry->d_sb;
21627 +       a->udba = au_opt_udba(sb);
21628 +       /* no d_unlinked(), to set UDBA_NONE for root */
21629 +       if (d_unhashed(dentry))
21630 +               a->udba = AuOpt_UDBA_NONE;
21631 +       if (a->udba != AuOpt_UDBA_NONE) {
21632 +               AuDebugOn(IS_ROOT(dentry));
21633 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21634 +               if (unlikely(err))
21635 +                       goto out;
21636 +       }
21637 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21638 +       if (unlikely(err < 0))
21639 +               goto out;
21640 +
21641 +       h_path->dentry = a->h_path.dentry;
21642 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21643 +
21644 +out:
21645 +       return err;
21646 +}
21647 +
21648 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21649 +                 struct au_sxattr *arg)
21650 +{
21651 +       int err;
21652 +       struct path h_path;
21653 +       struct super_block *sb;
21654 +       struct au_icpup_args *a;
21655 +       struct inode *h_inode;
21656 +
21657 +       IMustLock(inode);
21658 +
21659 +       err = -ENOMEM;
21660 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21661 +       if (unlikely(!a))
21662 +               goto out;
21663 +
21664 +       sb = dentry->d_sb;
21665 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21666 +       if (unlikely(err))
21667 +               goto out_kfree;
21668 +
21669 +       h_path.dentry = NULL;   /* silence gcc */
21670 +       di_write_lock_child(dentry);
21671 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21672 +       if (unlikely(err))
21673 +               goto out_di;
21674 +
21675 +       inode_unlock(a->h_inode);
21676 +       switch (arg->type) {
21677 +       case AU_XATTR_SET:
21678 +               AuDebugOn(d_is_negative(h_path.dentry));
21679 +               err = vfsub_setxattr(h_path.dentry,
21680 +                                    arg->u.set.name, arg->u.set.value,
21681 +                                    arg->u.set.size, arg->u.set.flags);
21682 +               break;
21683 +       case AU_ACL_SET:
21684 +               err = -EOPNOTSUPP;
21685 +               h_inode = d_inode(h_path.dentry);
21686 +               if (h_inode->i_op->set_acl)
21687 +                       /* this will call posix_acl_update_mode */
21688 +                       err = h_inode->i_op->set_acl(h_inode,
21689 +                                                    arg->u.acl_set.acl,
21690 +                                                    arg->u.acl_set.type);
21691 +               break;
21692 +       }
21693 +       if (!err)
21694 +               au_cpup_attr_timesizes(inode);
21695 +
21696 +       au_unpin(&a->pin);
21697 +       if (unlikely(err))
21698 +               au_update_dbtop(dentry);
21699 +
21700 +out_di:
21701 +       di_write_unlock(dentry);
21702 +       si_read_unlock(sb);
21703 +out_kfree:
21704 +       au_kfree_rcu(a);
21705 +out:
21706 +       AuTraceErr(err);
21707 +       return err;
21708 +}
21709 +#endif
21710 +
21711 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21712 +                            unsigned int nlink)
21713 +{
21714 +       unsigned int n;
21715 +
21716 +       inode->i_mode = st->mode;
21717 +       /* don't i_[ug]id_write() here */
21718 +       inode->i_uid = st->uid;
21719 +       inode->i_gid = st->gid;
21720 +       inode->i_atime = st->atime;
21721 +       inode->i_mtime = st->mtime;
21722 +       inode->i_ctime = st->ctime;
21723 +
21724 +       au_cpup_attr_nlink(inode, /*force*/0);
21725 +       if (S_ISDIR(inode->i_mode)) {
21726 +               n = inode->i_nlink;
21727 +               n -= nlink;
21728 +               n += st->nlink;
21729 +               smp_mb(); /* for i_nlink */
21730 +               /* 0 can happen */
21731 +               set_nlink(inode, n);
21732 +       }
21733 +
21734 +       spin_lock(&inode->i_lock);
21735 +       inode->i_blocks = st->blocks;
21736 +       i_size_write(inode, st->size);
21737 +       spin_unlock(&inode->i_lock);
21738 +}
21739 +
21740 +/*
21741 + * common routine for aufs_getattr() and au_getxattr().
21742 + * returns zero or negative (an error).
21743 + * @dentry will be read-locked in success.
21744 + */
21745 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21746 +                     struct path *h_path, int locked)
21747 +{
21748 +       int err;
21749 +       unsigned int mnt_flags, sigen;
21750 +       unsigned char udba_none;
21751 +       aufs_bindex_t bindex;
21752 +       struct super_block *sb, *h_sb;
21753 +
21754 +       h_path->mnt = NULL;
21755 +       h_path->dentry = NULL;
21756 +
21757 +       err = 0;
21758 +       sb = dentry->d_sb;
21759 +       mnt_flags = au_mntflags(sb);
21760 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21761 +
21762 +       if (unlikely(locked))
21763 +               goto body; /* skip locking dinfo */
21764 +
21765 +       /* support fstat(2) */
21766 +       if (!d_unlinked(dentry) && !udba_none) {
21767 +               sigen = au_sigen(sb);
21768 +               err = au_digen_test(dentry, sigen);
21769 +               if (!err) {
21770 +                       di_read_lock_child(dentry, AuLock_IR);
21771 +                       err = au_dbrange_test(dentry);
21772 +                       if (unlikely(err)) {
21773 +                               di_read_unlock(dentry, AuLock_IR);
21774 +                               goto out;
21775 +                       }
21776 +               } else {
21777 +                       AuDebugOn(IS_ROOT(dentry));
21778 +                       di_write_lock_child(dentry);
21779 +                       err = au_dbrange_test(dentry);
21780 +                       if (!err)
21781 +                               err = au_reval_for_attr(dentry, sigen);
21782 +                       if (!err)
21783 +                               di_downgrade_lock(dentry, AuLock_IR);
21784 +                       else {
21785 +                               di_write_unlock(dentry);
21786 +                               goto out;
21787 +                       }
21788 +               }
21789 +       } else
21790 +               di_read_lock_child(dentry, AuLock_IR);
21791 +
21792 +body:
21793 +       if (!inode) {
21794 +               inode = d_inode(dentry);
21795 +               if (unlikely(!inode))
21796 +                       goto out;
21797 +       }
21798 +       bindex = au_ibtop(inode);
21799 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21800 +       h_sb = h_path->mnt->mnt_sb;
21801 +       if (!force
21802 +           && !au_test_fs_bad_iattr(h_sb)
21803 +           && udba_none)
21804 +               goto out; /* success */
21805 +
21806 +       if (au_dbtop(dentry) == bindex)
21807 +               h_path->dentry = au_h_dptr(dentry, bindex);
21808 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21809 +               h_path->dentry = au_plink_lkup(inode, bindex);
21810 +               if (IS_ERR(h_path->dentry))
21811 +                       /* pretending success */
21812 +                       h_path->dentry = NULL;
21813 +               else
21814 +                       dput(h_path->dentry);
21815 +       }
21816 +
21817 +out:
21818 +       return err;
21819 +}
21820 +
21821 +static int aufs_getattr(const struct path *path, struct kstat *st,
21822 +                       u32 request, unsigned int query)
21823 +{
21824 +       int err;
21825 +       unsigned char positive;
21826 +       struct path h_path;
21827 +       struct dentry *dentry;
21828 +       struct inode *inode;
21829 +       struct super_block *sb;
21830 +
21831 +       dentry = path->dentry;
21832 +       inode = d_inode(dentry);
21833 +       sb = dentry->d_sb;
21834 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21835 +       if (unlikely(err))
21836 +               goto out;
21837 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21838 +                               /*locked*/0);
21839 +       if (unlikely(err))
21840 +               goto out_si;
21841 +       if (unlikely(!h_path.dentry))
21842 +               /* illegally overlapped or something */
21843 +               goto out_fill; /* pretending success */
21844 +
21845 +       positive = d_is_positive(h_path.dentry);
21846 +       if (positive)
21847 +               /* no vfsub version */
21848 +               err = vfs_getattr(&h_path, st, request, query);
21849 +       if (!err) {
21850 +               if (positive)
21851 +                       au_refresh_iattr(inode, st,
21852 +                                        d_inode(h_path.dentry)->i_nlink);
21853 +               goto out_fill; /* success */
21854 +       }
21855 +       AuTraceErr(err);
21856 +       goto out_di;
21857 +
21858 +out_fill:
21859 +       generic_fillattr(inode, st);
21860 +out_di:
21861 +       di_read_unlock(dentry, AuLock_IR);
21862 +out_si:
21863 +       si_read_unlock(sb);
21864 +out:
21865 +       AuTraceErr(err);
21866 +       return err;
21867 +}
21868 +
21869 +/* ---------------------------------------------------------------------- */
21870 +
21871 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21872 +                                struct delayed_call *done)
21873 +{
21874 +       const char *ret;
21875 +       struct dentry *h_dentry;
21876 +       struct inode *h_inode;
21877 +       int err;
21878 +       aufs_bindex_t bindex;
21879 +
21880 +       ret = NULL; /* suppress a warning */
21881 +       err = -ECHILD;
21882 +       if (!dentry)
21883 +               goto out;
21884 +
21885 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21886 +       if (unlikely(err))
21887 +               goto out;
21888 +
21889 +       err = au_d_hashed_positive(dentry);
21890 +       if (unlikely(err))
21891 +               goto out_unlock;
21892 +
21893 +       err = -EINVAL;
21894 +       inode = d_inode(dentry);
21895 +       bindex = au_ibtop(inode);
21896 +       h_inode = au_h_iptr(inode, bindex);
21897 +       if (unlikely(!h_inode->i_op->get_link))
21898 +               goto out_unlock;
21899 +
21900 +       err = -EBUSY;
21901 +       h_dentry = NULL;
21902 +       if (au_dbtop(dentry) <= bindex) {
21903 +               h_dentry = au_h_dptr(dentry, bindex);
21904 +               if (h_dentry)
21905 +                       dget(h_dentry);
21906 +       }
21907 +       if (!h_dentry) {
21908 +               h_dentry = d_find_any_alias(h_inode);
21909 +               if (IS_ERR(h_dentry)) {
21910 +                       err = PTR_ERR(h_dentry);
21911 +                       goto out_unlock;
21912 +               }
21913 +       }
21914 +       if (unlikely(!h_dentry))
21915 +               goto out_unlock;
21916 +
21917 +       err = 0;
21918 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21919 +       AuDbgDentry(h_dentry);
21920 +       ret = vfs_get_link(h_dentry, done);
21921 +       dput(h_dentry);
21922 +       if (IS_ERR(ret))
21923 +               err = PTR_ERR(ret);
21924 +
21925 +out_unlock:
21926 +       aufs_read_unlock(dentry, AuLock_IR);
21927 +out:
21928 +       if (unlikely(err))
21929 +               ret = ERR_PTR(err);
21930 +       AuTraceErrPtr(ret);
21931 +       return ret;
21932 +}
21933 +
21934 +/* ---------------------------------------------------------------------- */
21935 +
21936 +static int au_is_special(struct inode *inode)
21937 +{
21938 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21939 +}
21940 +
21941 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21942 +                           int flags)
21943 +{
21944 +       int err;
21945 +       aufs_bindex_t bindex;
21946 +       struct super_block *sb;
21947 +       struct inode *h_inode;
21948 +       struct vfsmount *h_mnt;
21949 +
21950 +       sb = inode->i_sb;
21951 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21952 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21953 +
21954 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21955 +       lockdep_off();
21956 +       si_read_lock(sb, AuLock_FLUSH);
21957 +       ii_write_lock_child(inode);
21958 +
21959 +       err = 0;
21960 +       bindex = au_ibtop(inode);
21961 +       h_inode = au_h_iptr(inode, bindex);
21962 +       if (!au_test_ro(sb, bindex, inode)) {
21963 +               h_mnt = au_sbr_mnt(sb, bindex);
21964 +               err = vfsub_mnt_want_write(h_mnt);
21965 +               if (!err) {
21966 +                       err = vfsub_update_time(h_inode, ts, flags);
21967 +                       vfsub_mnt_drop_write(h_mnt);
21968 +               }
21969 +       } else if (au_is_special(h_inode)) {
21970 +               /*
21971 +                * Never copy-up here.
21972 +                * These special files may already be opened and used for
21973 +                * communicating. If we copied it up, then the communication
21974 +                * would be corrupted.
21975 +                */
21976 +               AuWarn1("timestamps for i%lu are ignored "
21977 +                       "since it is on readonly branch (hi%lu).\n",
21978 +                       inode->i_ino, h_inode->i_ino);
21979 +       } else if (flags & ~S_ATIME) {
21980 +               err = -EIO;
21981 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21982 +               AuDebugOn(1);
21983 +       }
21984 +
21985 +       if (!err)
21986 +               au_cpup_attr_timesizes(inode);
21987 +       ii_write_unlock(inode);
21988 +       si_read_unlock(sb);
21989 +       lockdep_on();
21990 +
21991 +       if (!err && (flags & S_VERSION))
21992 +               inode_inc_iversion(inode);
21993 +
21994 +       return err;
21995 +}
21996 +
21997 +/* ---------------------------------------------------------------------- */
21998 +
21999 +/* no getattr version will be set by module.c:aufs_init() */
22000 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
22001 +       aufs_iop[] = {
22002 +       [AuIop_SYMLINK] = {
22003 +               .permission     = aufs_permission,
22004 +#ifdef CONFIG_FS_POSIX_ACL
22005 +               .get_acl        = aufs_get_acl,
22006 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22007 +#endif
22008 +
22009 +               .setattr        = aufs_setattr,
22010 +               .getattr        = aufs_getattr,
22011 +
22012 +#ifdef CONFIG_AUFS_XATTR
22013 +               .listxattr      = aufs_listxattr,
22014 +#endif
22015 +
22016 +               .get_link       = aufs_get_link,
22017 +
22018 +               /* .update_time = aufs_update_time */
22019 +       },
22020 +       [AuIop_DIR] = {
22021 +               .create         = aufs_create,
22022 +               .lookup         = aufs_lookup,
22023 +               .link           = aufs_link,
22024 +               .unlink         = aufs_unlink,
22025 +               .symlink        = aufs_symlink,
22026 +               .mkdir          = aufs_mkdir,
22027 +               .rmdir          = aufs_rmdir,
22028 +               .mknod          = aufs_mknod,
22029 +               .rename         = aufs_rename,
22030 +
22031 +               .permission     = aufs_permission,
22032 +#ifdef CONFIG_FS_POSIX_ACL
22033 +               .get_acl        = aufs_get_acl,
22034 +               .set_acl        = aufs_set_acl,
22035 +#endif
22036 +
22037 +               .setattr        = aufs_setattr,
22038 +               .getattr        = aufs_getattr,
22039 +
22040 +#ifdef CONFIG_AUFS_XATTR
22041 +               .listxattr      = aufs_listxattr,
22042 +#endif
22043 +
22044 +               .update_time    = aufs_update_time,
22045 +               .atomic_open    = aufs_atomic_open,
22046 +               .tmpfile        = aufs_tmpfile
22047 +       },
22048 +       [AuIop_OTHER] = {
22049 +               .permission     = aufs_permission,
22050 +#ifdef CONFIG_FS_POSIX_ACL
22051 +               .get_acl        = aufs_get_acl,
22052 +               .set_acl        = aufs_set_acl,
22053 +#endif
22054 +
22055 +               .setattr        = aufs_setattr,
22056 +               .getattr        = aufs_getattr,
22057 +
22058 +#ifdef CONFIG_AUFS_XATTR
22059 +               .listxattr      = aufs_listxattr,
22060 +#endif
22061 +
22062 +               .update_time    = aufs_update_time
22063 +       }
22064 +};
22065 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22066 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22067 +++ linux/fs/aufs/i_op_del.c    2020-01-27 10:57:18.172204883 +0100
22068 @@ -0,0 +1,513 @@
22069 +// SPDX-License-Identifier: GPL-2.0
22070 +/*
22071 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22072 + *
22073 + * This program, aufs is free software; you can redistribute it and/or modify
22074 + * it under the terms of the GNU General Public License as published by
22075 + * the Free Software Foundation; either version 2 of the License, or
22076 + * (at your option) any later version.
22077 + *
22078 + * This program is distributed in the hope that it will be useful,
22079 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22080 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22081 + * GNU General Public License for more details.
22082 + *
22083 + * You should have received a copy of the GNU General Public License
22084 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22085 + */
22086 +
22087 +/*
22088 + * inode operations (del entry)
22089 + */
22090 +
22091 +#include <linux/iversion.h>
22092 +#include "aufs.h"
22093 +
22094 +/*
22095 + * decide if a new whiteout for @dentry is necessary or not.
22096 + * when it is necessary, prepare the parent dir for the upper branch whose
22097 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22098 + * be done by caller.
22099 + * return value:
22100 + * 0: wh is unnecessary
22101 + * plus: wh is necessary
22102 + * minus: error
22103 + */
22104 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22105 +{
22106 +       int need_wh, err;
22107 +       aufs_bindex_t btop;
22108 +       struct super_block *sb;
22109 +
22110 +       sb = dentry->d_sb;
22111 +       btop = au_dbtop(dentry);
22112 +       if (*bcpup < 0) {
22113 +               *bcpup = btop;
22114 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22115 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22116 +                       *bcpup = err;
22117 +                       if (unlikely(err < 0))
22118 +                               goto out;
22119 +               }
22120 +       } else
22121 +               AuDebugOn(btop < *bcpup
22122 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22123 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22124 +
22125 +       if (*bcpup != btop) {
22126 +               err = au_cpup_dirs(dentry, *bcpup);
22127 +               if (unlikely(err))
22128 +                       goto out;
22129 +               need_wh = 1;
22130 +       } else {
22131 +               struct au_dinfo *dinfo, *tmp;
22132 +
22133 +               need_wh = -ENOMEM;
22134 +               dinfo = au_di(dentry);
22135 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22136 +               if (tmp) {
22137 +                       au_di_cp(tmp, dinfo);
22138 +                       au_di_swap(tmp, dinfo);
22139 +                       /* returns the number of positive dentries */
22140 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22141 +                                                /* AuLkup_IGNORE_PERM */ 0);
22142 +                       au_di_swap(tmp, dinfo);
22143 +                       au_rw_write_unlock(&tmp->di_rwsem);
22144 +                       au_di_free(tmp);
22145 +               }
22146 +       }
22147 +       AuDbg("need_wh %d\n", need_wh);
22148 +       err = need_wh;
22149 +
22150 +out:
22151 +       return err;
22152 +}
22153 +
22154 +/*
22155 + * simple tests for the del-entry operations.
22156 + * following the checks in vfs, plus the parent-child relationship.
22157 + */
22158 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22159 +              struct dentry *h_parent, int isdir)
22160 +{
22161 +       int err;
22162 +       umode_t h_mode;
22163 +       struct dentry *h_dentry, *h_latest;
22164 +       struct inode *h_inode;
22165 +
22166 +       h_dentry = au_h_dptr(dentry, bindex);
22167 +       if (d_really_is_positive(dentry)) {
22168 +               err = -ENOENT;
22169 +               if (unlikely(d_is_negative(h_dentry)))
22170 +                       goto out;
22171 +               h_inode = d_inode(h_dentry);
22172 +               if (unlikely(!h_inode->i_nlink))
22173 +                       goto out;
22174 +
22175 +               h_mode = h_inode->i_mode;
22176 +               if (!isdir) {
22177 +                       err = -EISDIR;
22178 +                       if (unlikely(S_ISDIR(h_mode)))
22179 +                               goto out;
22180 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22181 +                       err = -ENOTDIR;
22182 +                       goto out;
22183 +               }
22184 +       } else {
22185 +               /* rename(2) case */
22186 +               err = -EIO;
22187 +               if (unlikely(d_is_positive(h_dentry)))
22188 +                       goto out;
22189 +       }
22190 +
22191 +       err = -ENOENT;
22192 +       /* expected parent dir is locked */
22193 +       if (unlikely(h_parent != h_dentry->d_parent))
22194 +               goto out;
22195 +       err = 0;
22196 +
22197 +       /*
22198 +        * rmdir a dir may break the consistency on some filesystem.
22199 +        * let's try heavy test.
22200 +        */
22201 +       err = -EACCES;
22202 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22203 +                    && au_test_h_perm(d_inode(h_parent),
22204 +                                      MAY_EXEC | MAY_WRITE)))
22205 +               goto out;
22206 +
22207 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22208 +       err = -EIO;
22209 +       if (IS_ERR(h_latest))
22210 +               goto out;
22211 +       if (h_latest == h_dentry)
22212 +               err = 0;
22213 +       dput(h_latest);
22214 +
22215 +out:
22216 +       return err;
22217 +}
22218 +
22219 +/*
22220 + * decide the branch where we operate for @dentry. the branch index will be set
22221 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22222 + * dir for reverting.
22223 + * when a new whiteout is necessary, create it.
22224 + */
22225 +static struct dentry*
22226 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22227 +                   struct au_dtime *dt, struct au_pin *pin)
22228 +{
22229 +       struct dentry *wh_dentry;
22230 +       struct super_block *sb;
22231 +       struct path h_path;
22232 +       int err, need_wh;
22233 +       unsigned int udba;
22234 +       aufs_bindex_t bcpup;
22235 +
22236 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22237 +       wh_dentry = ERR_PTR(need_wh);
22238 +       if (unlikely(need_wh < 0))
22239 +               goto out;
22240 +
22241 +       sb = dentry->d_sb;
22242 +       udba = au_opt_udba(sb);
22243 +       bcpup = *rbcpup;
22244 +       err = au_pin(pin, dentry, bcpup, udba,
22245 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22246 +       wh_dentry = ERR_PTR(err);
22247 +       if (unlikely(err))
22248 +               goto out;
22249 +
22250 +       h_path.dentry = au_pinned_h_parent(pin);
22251 +       if (udba != AuOpt_UDBA_NONE
22252 +           && au_dbtop(dentry) == bcpup) {
22253 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22254 +               wh_dentry = ERR_PTR(err);
22255 +               if (unlikely(err))
22256 +                       goto out_unpin;
22257 +       }
22258 +
22259 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22260 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22261 +       wh_dentry = NULL;
22262 +       if (!need_wh)
22263 +               goto out; /* success, no need to create whiteout */
22264 +
22265 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22266 +       if (IS_ERR(wh_dentry))
22267 +               goto out_unpin;
22268 +
22269 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22270 +       goto out; /* success */
22271 +
22272 +out_unpin:
22273 +       au_unpin(pin);
22274 +out:
22275 +       return wh_dentry;
22276 +}
22277 +
22278 +/*
22279 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22280 + * in order to be revertible and save time for removing many child whiteouts
22281 + * under the dir.
22282 + * returns 1 when there are too many child whiteout and caller should remove
22283 + * them asynchronously. returns 0 when the number of children is enough small to
22284 + * remove now or the branch fs is a remote fs.
22285 + * otherwise return an error.
22286 + */
22287 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22288 +                          struct au_nhash *whlist, struct inode *dir)
22289 +{
22290 +       int rmdir_later, err, dirwh;
22291 +       struct dentry *h_dentry;
22292 +       struct super_block *sb;
22293 +       struct inode *inode;
22294 +
22295 +       sb = dentry->d_sb;
22296 +       SiMustAnyLock(sb);
22297 +       h_dentry = au_h_dptr(dentry, bindex);
22298 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22299 +       if (unlikely(err))
22300 +               goto out;
22301 +
22302 +       /* stop monitoring */
22303 +       inode = d_inode(dentry);
22304 +       au_hn_free(au_hi(inode, bindex));
22305 +
22306 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22307 +               dirwh = au_sbi(sb)->si_dirwh;
22308 +               rmdir_later = (dirwh <= 1);
22309 +               if (!rmdir_later)
22310 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22311 +                                                             dirwh);
22312 +               if (rmdir_later)
22313 +                       return rmdir_later;
22314 +       }
22315 +
22316 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22317 +       if (unlikely(err)) {
22318 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22319 +                       h_dentry, bindex, err);
22320 +               err = 0;
22321 +       }
22322 +
22323 +out:
22324 +       AuTraceErr(err);
22325 +       return err;
22326 +}
22327 +
22328 +/*
22329 + * final procedure for deleting a entry.
22330 + * maintain dentry and iattr.
22331 + */
22332 +static void epilog(struct inode *dir, struct dentry *dentry,
22333 +                  aufs_bindex_t bindex)
22334 +{
22335 +       struct inode *inode;
22336 +
22337 +       inode = d_inode(dentry);
22338 +       d_drop(dentry);
22339 +       inode->i_ctime = dir->i_ctime;
22340 +
22341 +       au_dir_ts(dir, bindex);
22342 +       inode_inc_iversion(dir);
22343 +}
22344 +
22345 +/*
22346 + * when an error happened, remove the created whiteout and revert everything.
22347 + */
22348 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22349 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22350 +                    struct dentry *dentry, struct au_dtime *dt)
22351 +{
22352 +       int rerr;
22353 +       struct path h_path = {
22354 +               .dentry = wh_dentry,
22355 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22356 +       };
22357 +
22358 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22359 +       if (!rerr) {
22360 +               au_set_dbwh(dentry, bwh);
22361 +               au_dtime_revert(dt);
22362 +               return 0;
22363 +       }
22364 +
22365 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22366 +       return -EIO;
22367 +}
22368 +
22369 +/* ---------------------------------------------------------------------- */
22370 +
22371 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22372 +{
22373 +       int err;
22374 +       aufs_bindex_t bwh, bindex, btop;
22375 +       struct inode *inode, *h_dir, *delegated;
22376 +       struct dentry *parent, *wh_dentry;
22377 +       /* to reduce stack size */
22378 +       struct {
22379 +               struct au_dtime dt;
22380 +               struct au_pin pin;
22381 +               struct path h_path;
22382 +       } *a;
22383 +
22384 +       IMustLock(dir);
22385 +
22386 +       err = -ENOMEM;
22387 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22388 +       if (unlikely(!a))
22389 +               goto out;
22390 +
22391 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22392 +       if (unlikely(err))
22393 +               goto out_free;
22394 +       err = au_d_hashed_positive(dentry);
22395 +       if (unlikely(err))
22396 +               goto out_unlock;
22397 +       inode = d_inode(dentry);
22398 +       IMustLock(inode);
22399 +       err = -EISDIR;
22400 +       if (unlikely(d_is_dir(dentry)))
22401 +               goto out_unlock; /* possible? */
22402 +
22403 +       btop = au_dbtop(dentry);
22404 +       bwh = au_dbwh(dentry);
22405 +       bindex = -1;
22406 +       parent = dentry->d_parent; /* dir inode is locked */
22407 +       di_write_lock_parent(parent);
22408 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22409 +                                       &a->pin);
22410 +       err = PTR_ERR(wh_dentry);
22411 +       if (IS_ERR(wh_dentry))
22412 +               goto out_parent;
22413 +
22414 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22415 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22416 +       dget(a->h_path.dentry);
22417 +       if (bindex == btop) {
22418 +               h_dir = au_pinned_h_dir(&a->pin);
22419 +               delegated = NULL;
22420 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22421 +               if (unlikely(err == -EWOULDBLOCK)) {
22422 +                       pr_warn("cannot retry for NFSv4 delegation"
22423 +                               " for an internal unlink\n");
22424 +                       iput(delegated);
22425 +               }
22426 +       } else {
22427 +               /* dir inode is locked */
22428 +               h_dir = d_inode(wh_dentry->d_parent);
22429 +               IMustLock(h_dir);
22430 +               err = 0;
22431 +       }
22432 +
22433 +       if (!err) {
22434 +               vfsub_drop_nlink(inode);
22435 +               epilog(dir, dentry, bindex);
22436 +
22437 +               /* update target timestamps */
22438 +               if (bindex == btop) {
22439 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22440 +                       /*ignore*/
22441 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22442 +               } else
22443 +                       /* todo: this timestamp may be reverted later */
22444 +                       inode->i_ctime = h_dir->i_ctime;
22445 +               goto out_unpin; /* success */
22446 +       }
22447 +
22448 +       /* revert */
22449 +       if (wh_dentry) {
22450 +               int rerr;
22451 +
22452 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22453 +                                &a->dt);
22454 +               if (rerr)
22455 +                       err = rerr;
22456 +       }
22457 +
22458 +out_unpin:
22459 +       au_unpin(&a->pin);
22460 +       dput(wh_dentry);
22461 +       dput(a->h_path.dentry);
22462 +out_parent:
22463 +       di_write_unlock(parent);
22464 +out_unlock:
22465 +       aufs_read_unlock(dentry, AuLock_DW);
22466 +out_free:
22467 +       au_kfree_rcu(a);
22468 +out:
22469 +       return err;
22470 +}
22471 +
22472 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22473 +{
22474 +       int err, rmdir_later;
22475 +       aufs_bindex_t bwh, bindex, btop;
22476 +       struct inode *inode;
22477 +       struct dentry *parent, *wh_dentry, *h_dentry;
22478 +       struct au_whtmp_rmdir *args;
22479 +       /* to reduce stack size */
22480 +       struct {
22481 +               struct au_dtime dt;
22482 +               struct au_pin pin;
22483 +       } *a;
22484 +
22485 +       IMustLock(dir);
22486 +
22487 +       err = -ENOMEM;
22488 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22489 +       if (unlikely(!a))
22490 +               goto out;
22491 +
22492 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22493 +       if (unlikely(err))
22494 +               goto out_free;
22495 +       err = au_alive_dir(dentry);
22496 +       if (unlikely(err))
22497 +               goto out_unlock;
22498 +       inode = d_inode(dentry);
22499 +       IMustLock(inode);
22500 +       err = -ENOTDIR;
22501 +       if (unlikely(!d_is_dir(dentry)))
22502 +               goto out_unlock; /* possible? */
22503 +
22504 +       err = -ENOMEM;
22505 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22506 +       if (unlikely(!args))
22507 +               goto out_unlock;
22508 +
22509 +       parent = dentry->d_parent; /* dir inode is locked */
22510 +       di_write_lock_parent(parent);
22511 +       err = au_test_empty(dentry, &args->whlist);
22512 +       if (unlikely(err))
22513 +               goto out_parent;
22514 +
22515 +       btop = au_dbtop(dentry);
22516 +       bwh = au_dbwh(dentry);
22517 +       bindex = -1;
22518 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22519 +                                       &a->pin);
22520 +       err = PTR_ERR(wh_dentry);
22521 +       if (IS_ERR(wh_dentry))
22522 +               goto out_parent;
22523 +
22524 +       h_dentry = au_h_dptr(dentry, btop);
22525 +       dget(h_dentry);
22526 +       rmdir_later = 0;
22527 +       if (bindex == btop) {
22528 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22529 +               if (err > 0) {
22530 +                       rmdir_later = err;
22531 +                       err = 0;
22532 +               }
22533 +       } else {
22534 +               /* stop monitoring */
22535 +               au_hn_free(au_hi(inode, btop));
22536 +
22537 +               /* dir inode is locked */
22538 +               IMustLock(d_inode(wh_dentry->d_parent));
22539 +               err = 0;
22540 +       }
22541 +
22542 +       if (!err) {
22543 +               vfsub_dead_dir(inode);
22544 +               au_set_dbdiropq(dentry, -1);
22545 +               epilog(dir, dentry, bindex);
22546 +
22547 +               if (rmdir_later) {
22548 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22549 +                       args = NULL;
22550 +               }
22551 +
22552 +               goto out_unpin; /* success */
22553 +       }
22554 +
22555 +       /* revert */
22556 +       AuLabel(revert);
22557 +       if (wh_dentry) {
22558 +               int rerr;
22559 +
22560 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22561 +                                &a->dt);
22562 +               if (rerr)
22563 +                       err = rerr;
22564 +       }
22565 +
22566 +out_unpin:
22567 +       au_unpin(&a->pin);
22568 +       dput(wh_dentry);
22569 +       dput(h_dentry);
22570 +out_parent:
22571 +       di_write_unlock(parent);
22572 +       if (args)
22573 +               au_whtmp_rmdir_free(args);
22574 +out_unlock:
22575 +       aufs_read_unlock(dentry, AuLock_DW);
22576 +out_free:
22577 +       au_kfree_rcu(a);
22578 +out:
22579 +       AuTraceErr(err);
22580 +       return err;
22581 +}
22582 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22583 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22584 +++ linux/fs/aufs/i_op_ren.c    2020-01-27 10:57:18.172204883 +0100
22585 @@ -0,0 +1,1250 @@
22586 +// SPDX-License-Identifier: GPL-2.0
22587 +/*
22588 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22589 + *
22590 + * This program, aufs is free software; you can redistribute it and/or modify
22591 + * it under the terms of the GNU General Public License as published by
22592 + * the Free Software Foundation; either version 2 of the License, or
22593 + * (at your option) any later version.
22594 + *
22595 + * This program is distributed in the hope that it will be useful,
22596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22598 + * GNU General Public License for more details.
22599 + *
22600 + * You should have received a copy of the GNU General Public License
22601 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22602 + */
22603 +
22604 +/*
22605 + * inode operation (rename entry)
22606 + * todo: this is crazy monster
22607 + */
22608 +
22609 +#include <linux/iversion.h>
22610 +#include "aufs.h"
22611 +
22612 +enum { AuSRC, AuDST, AuSrcDst };
22613 +enum { AuPARENT, AuCHILD, AuParentChild };
22614 +
22615 +#define AuRen_ISDIR_SRC                1
22616 +#define AuRen_ISDIR_DST                (1 << 1)
22617 +#define AuRen_ISSAMEDIR                (1 << 2)
22618 +#define AuRen_WHSRC            (1 << 3)
22619 +#define AuRen_WHDST            (1 << 4)
22620 +#define AuRen_MNT_WRITE                (1 << 5)
22621 +#define AuRen_DT_DSTDIR                (1 << 6)
22622 +#define AuRen_DIROPQ_SRC       (1 << 7)
22623 +#define AuRen_DIROPQ_DST       (1 << 8)
22624 +#define AuRen_DIRREN           (1 << 9)
22625 +#define AuRen_DROPPED_SRC      (1 << 10)
22626 +#define AuRen_DROPPED_DST      (1 << 11)
22627 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22628 +#define au_fset_ren(flags, name) \
22629 +       do { (flags) |= AuRen_##name; } while (0)
22630 +#define au_fclr_ren(flags, name) \
22631 +       do { (flags) &= ~AuRen_##name; } while (0)
22632 +
22633 +#ifndef CONFIG_AUFS_DIRREN
22634 +#undef AuRen_DIRREN
22635 +#define AuRen_DIRREN           0
22636 +#endif
22637 +
22638 +struct au_ren_args {
22639 +       struct {
22640 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22641 +                       *wh_dentry;
22642 +               struct inode *dir, *inode;
22643 +               struct au_hinode *hdir, *hinode;
22644 +               struct au_dtime dt[AuParentChild];
22645 +               aufs_bindex_t btop, bdiropq;
22646 +       } sd[AuSrcDst];
22647 +
22648 +#define src_dentry     sd[AuSRC].dentry
22649 +#define src_dir                sd[AuSRC].dir
22650 +#define src_inode      sd[AuSRC].inode
22651 +#define src_h_dentry   sd[AuSRC].h_dentry
22652 +#define src_parent     sd[AuSRC].parent
22653 +#define src_h_parent   sd[AuSRC].h_parent
22654 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22655 +#define src_hdir       sd[AuSRC].hdir
22656 +#define src_hinode     sd[AuSRC].hinode
22657 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22658 +#define src_dt         sd[AuSRC].dt
22659 +#define src_btop       sd[AuSRC].btop
22660 +#define src_bdiropq    sd[AuSRC].bdiropq
22661 +
22662 +#define dst_dentry     sd[AuDST].dentry
22663 +#define dst_dir                sd[AuDST].dir
22664 +#define dst_inode      sd[AuDST].inode
22665 +#define dst_h_dentry   sd[AuDST].h_dentry
22666 +#define dst_parent     sd[AuDST].parent
22667 +#define dst_h_parent   sd[AuDST].h_parent
22668 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22669 +#define dst_hdir       sd[AuDST].hdir
22670 +#define dst_hinode     sd[AuDST].hinode
22671 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22672 +#define dst_dt         sd[AuDST].dt
22673 +#define dst_btop       sd[AuDST].btop
22674 +#define dst_bdiropq    sd[AuDST].bdiropq
22675 +
22676 +       struct dentry *h_trap;
22677 +       struct au_branch *br;
22678 +       struct path h_path;
22679 +       struct au_nhash whlist;
22680 +       aufs_bindex_t btgt, src_bwh;
22681 +
22682 +       struct {
22683 +               unsigned short auren_flags;
22684 +               unsigned char flags;    /* syscall parameter */
22685 +               unsigned char exchange;
22686 +       } __packed;
22687 +
22688 +       struct au_whtmp_rmdir *thargs;
22689 +       struct dentry *h_dst;
22690 +       struct au_hinode *h_root;
22691 +};
22692 +
22693 +/* ---------------------------------------------------------------------- */
22694 +
22695 +/*
22696 + * functions for reverting.
22697 + * when an error happened in a single rename systemcall, we should revert
22698 + * everything as if nothing happened.
22699 + * we don't need to revert the copied-up/down the parent dir since they are
22700 + * harmless.
22701 + */
22702 +
22703 +#define RevertFailure(fmt, ...) do { \
22704 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22705 +               ##__VA_ARGS__, err, rerr); \
22706 +       err = -EIO; \
22707 +} while (0)
22708 +
22709 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22710 +{
22711 +       int rerr;
22712 +       struct dentry *d;
22713 +#define src_or_dst(member) a->sd[idx].member
22714 +
22715 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22716 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22717 +       rerr = au_diropq_remove(d, a->btgt);
22718 +       au_hn_inode_unlock(src_or_dst(hinode));
22719 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22720 +       if (rerr)
22721 +               RevertFailure("remove diropq %pd", d);
22722 +
22723 +#undef src_or_dst_
22724 +}
22725 +
22726 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22727 +{
22728 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22729 +               au_ren_do_rev_diropq(err, a, AuSRC);
22730 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22731 +               au_ren_do_rev_diropq(err, a, AuDST);
22732 +}
22733 +
22734 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22735 +{
22736 +       int rerr;
22737 +       struct inode *delegated;
22738 +
22739 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22740 +                                         a->src_h_parent);
22741 +       rerr = PTR_ERR(a->h_path.dentry);
22742 +       if (IS_ERR(a->h_path.dentry)) {
22743 +               RevertFailure("lkup one %pd", a->src_dentry);
22744 +               return;
22745 +       }
22746 +
22747 +       delegated = NULL;
22748 +       rerr = vfsub_rename(a->dst_h_dir,
22749 +                           au_h_dptr(a->src_dentry, a->btgt),
22750 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22751 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22752 +               pr_warn("cannot retry for NFSv4 delegation"
22753 +                       " for an internal rename\n");
22754 +               iput(delegated);
22755 +       }
22756 +       d_drop(a->h_path.dentry);
22757 +       dput(a->h_path.dentry);
22758 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22759 +       if (rerr)
22760 +               RevertFailure("rename %pd", a->src_dentry);
22761 +}
22762 +
22763 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22764 +{
22765 +       int rerr;
22766 +       struct inode *delegated;
22767 +
22768 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22769 +                                         a->dst_h_parent);
22770 +       rerr = PTR_ERR(a->h_path.dentry);
22771 +       if (IS_ERR(a->h_path.dentry)) {
22772 +               RevertFailure("lkup one %pd", a->dst_dentry);
22773 +               return;
22774 +       }
22775 +       if (d_is_positive(a->h_path.dentry)) {
22776 +               d_drop(a->h_path.dentry);
22777 +               dput(a->h_path.dentry);
22778 +               return;
22779 +       }
22780 +
22781 +       delegated = NULL;
22782 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22783 +                           &delegated, a->flags);
22784 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22785 +               pr_warn("cannot retry for NFSv4 delegation"
22786 +                       " for an internal rename\n");
22787 +               iput(delegated);
22788 +       }
22789 +       d_drop(a->h_path.dentry);
22790 +       dput(a->h_path.dentry);
22791 +       if (!rerr)
22792 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22793 +       else
22794 +               RevertFailure("rename %pd", a->h_dst);
22795 +}
22796 +
22797 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22798 +{
22799 +       int rerr;
22800 +
22801 +       a->h_path.dentry = a->src_wh_dentry;
22802 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22803 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22804 +       if (rerr)
22805 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22806 +}
22807 +#undef RevertFailure
22808 +
22809 +/* ---------------------------------------------------------------------- */
22810 +
22811 +/*
22812 + * when we have to copyup the renaming entry, do it with the rename-target name
22813 + * in order to minimize the cost (the later actual rename is unnecessary).
22814 + * otherwise rename it on the target branch.
22815 + */
22816 +static int au_ren_or_cpup(struct au_ren_args *a)
22817 +{
22818 +       int err;
22819 +       struct dentry *d;
22820 +       struct inode *delegated;
22821 +
22822 +       d = a->src_dentry;
22823 +       if (au_dbtop(d) == a->btgt) {
22824 +               a->h_path.dentry = a->dst_h_dentry;
22825 +               AuDebugOn(au_dbtop(d) != a->btgt);
22826 +               delegated = NULL;
22827 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22828 +                                  a->dst_h_dir, &a->h_path, &delegated,
22829 +                                  a->flags);
22830 +               if (unlikely(err == -EWOULDBLOCK)) {
22831 +                       pr_warn("cannot retry for NFSv4 delegation"
22832 +                               " for an internal rename\n");
22833 +                       iput(delegated);
22834 +               }
22835 +       } else
22836 +               BUG();
22837 +
22838 +       if (!err && a->h_dst)
22839 +               /* it will be set to dinfo later */
22840 +               dget(a->h_dst);
22841 +
22842 +       return err;
22843 +}
22844 +
22845 +/* cf. aufs_rmdir() */
22846 +static int au_ren_del_whtmp(struct au_ren_args *a)
22847 +{
22848 +       int err;
22849 +       struct inode *dir;
22850 +
22851 +       dir = a->dst_dir;
22852 +       SiMustAnyLock(dir->i_sb);
22853 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22854 +                                    au_sbi(dir->i_sb)->si_dirwh)
22855 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22856 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22857 +               if (unlikely(err))
22858 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22859 +                               "ignored.\n", a->h_dst, err);
22860 +       } else {
22861 +               au_nhash_wh_free(&a->thargs->whlist);
22862 +               a->thargs->whlist = a->whlist;
22863 +               a->whlist.nh_num = 0;
22864 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22865 +               dput(a->h_dst);
22866 +               a->thargs = NULL;
22867 +       }
22868 +
22869 +       return 0;
22870 +}
22871 +
22872 +/* make it 'opaque' dir. */
22873 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22874 +{
22875 +       int err;
22876 +       struct dentry *d, *diropq;
22877 +#define src_or_dst(member) a->sd[idx].member
22878 +
22879 +       err = 0;
22880 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22881 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22882 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22883 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22884 +       diropq = au_diropq_create(d, a->btgt);
22885 +       au_hn_inode_unlock(src_or_dst(hinode));
22886 +       if (IS_ERR(diropq))
22887 +               err = PTR_ERR(diropq);
22888 +       else
22889 +               dput(diropq);
22890 +
22891 +#undef src_or_dst_
22892 +       return err;
22893 +}
22894 +
22895 +static int au_ren_diropq(struct au_ren_args *a)
22896 +{
22897 +       int err;
22898 +       unsigned char always;
22899 +       struct dentry *d;
22900 +
22901 +       err = 0;
22902 +       d = a->dst_dentry; /* already renamed on the branch */
22903 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22904 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22905 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22906 +           && a->btgt != au_dbdiropq(a->src_dentry)
22907 +           && (a->dst_wh_dentry
22908 +               || a->btgt <= au_dbdiropq(d)
22909 +               /* hide the lower to keep xino */
22910 +               /* the lowers may not be a dir, but we hide them anyway */
22911 +               || a->btgt < au_dbbot(d)
22912 +               || always)) {
22913 +               AuDbg("here\n");
22914 +               err = au_ren_do_diropq(a, AuSRC);
22915 +               if (unlikely(err))
22916 +                       goto out;
22917 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22918 +       }
22919 +       if (!a->exchange)
22920 +               goto out; /* success */
22921 +
22922 +       d = a->src_dentry; /* already renamed on the branch */
22923 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22924 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22925 +           && (a->btgt < au_dbdiropq(d)
22926 +               || a->btgt < au_dbbot(d)
22927 +               || always)) {
22928 +               AuDbgDentry(a->src_dentry);
22929 +               AuDbgDentry(a->dst_dentry);
22930 +               err = au_ren_do_diropq(a, AuDST);
22931 +               if (unlikely(err))
22932 +                       goto out_rev_src;
22933 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22934 +       }
22935 +       goto out; /* success */
22936 +
22937 +out_rev_src:
22938 +       AuDbg("err %d, reverting src\n", err);
22939 +       au_ren_rev_diropq(err, a);
22940 +out:
22941 +       return err;
22942 +}
22943 +
22944 +static int do_rename(struct au_ren_args *a)
22945 +{
22946 +       int err;
22947 +       struct dentry *d, *h_d;
22948 +
22949 +       if (!a->exchange) {
22950 +               /* prepare workqueue args for asynchronous rmdir */
22951 +               h_d = a->dst_h_dentry;
22952 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22953 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22954 +                   && d_is_positive(h_d)) {
22955 +                       err = -ENOMEM;
22956 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22957 +                                                        GFP_NOFS);
22958 +                       if (unlikely(!a->thargs))
22959 +                               goto out;
22960 +                       a->h_dst = dget(h_d);
22961 +               }
22962 +
22963 +               /* create whiteout for src_dentry */
22964 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22965 +                       a->src_bwh = au_dbwh(a->src_dentry);
22966 +                       AuDebugOn(a->src_bwh >= 0);
22967 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22968 +                                                       a->src_h_parent);
22969 +                       err = PTR_ERR(a->src_wh_dentry);
22970 +                       if (IS_ERR(a->src_wh_dentry))
22971 +                               goto out_thargs;
22972 +               }
22973 +
22974 +               /* lookup whiteout for dentry */
22975 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22976 +                       h_d = au_wh_lkup(a->dst_h_parent,
22977 +                                        &a->dst_dentry->d_name, a->br);
22978 +                       err = PTR_ERR(h_d);
22979 +                       if (IS_ERR(h_d))
22980 +                               goto out_whsrc;
22981 +                       if (d_is_negative(h_d))
22982 +                               dput(h_d);
22983 +                       else
22984 +                               a->dst_wh_dentry = h_d;
22985 +               }
22986 +
22987 +               /* rename dentry to tmpwh */
22988 +               if (a->thargs) {
22989 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22990 +                       if (unlikely(err))
22991 +                               goto out_whdst;
22992 +
22993 +                       d = a->dst_dentry;
22994 +                       au_set_h_dptr(d, a->btgt, NULL);
22995 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22996 +                       if (unlikely(err))
22997 +                               goto out_whtmp;
22998 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22999 +               }
23000 +       }
23001 +
23002 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23003 +#if 0 /* debugging */
23004 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23005 +              && d_is_positive(a->dst_h_dentry)
23006 +              && a->src_btop != a->btgt);
23007 +#endif
23008 +
23009 +       /* rename by vfs_rename or cpup */
23010 +       err = au_ren_or_cpup(a);
23011 +       if (unlikely(err))
23012 +               /* leave the copied-up one */
23013 +               goto out_whtmp;
23014 +
23015 +       /* make dir opaque */
23016 +       err = au_ren_diropq(a);
23017 +       if (unlikely(err))
23018 +               goto out_rename;
23019 +
23020 +       /* update target timestamps */
23021 +       if (a->exchange) {
23022 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23023 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23024 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23025 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23026 +       }
23027 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23028 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23029 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23030 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23031 +
23032 +       if (!a->exchange) {
23033 +               /* remove whiteout for dentry */
23034 +               if (a->dst_wh_dentry) {
23035 +                       a->h_path.dentry = a->dst_wh_dentry;
23036 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23037 +                                                 a->dst_dentry);
23038 +                       if (unlikely(err))
23039 +                               goto out_diropq;
23040 +               }
23041 +
23042 +               /* remove whtmp */
23043 +               if (a->thargs)
23044 +                       au_ren_del_whtmp(a); /* ignore this error */
23045 +
23046 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23047 +       }
23048 +       err = 0;
23049 +       goto out_success;
23050 +
23051 +out_diropq:
23052 +       au_ren_rev_diropq(err, a);
23053 +out_rename:
23054 +       au_ren_rev_rename(err, a);
23055 +       dput(a->h_dst);
23056 +out_whtmp:
23057 +       if (a->thargs)
23058 +               au_ren_rev_whtmp(err, a);
23059 +out_whdst:
23060 +       dput(a->dst_wh_dentry);
23061 +       a->dst_wh_dentry = NULL;
23062 +out_whsrc:
23063 +       if (a->src_wh_dentry)
23064 +               au_ren_rev_whsrc(err, a);
23065 +out_success:
23066 +       dput(a->src_wh_dentry);
23067 +       dput(a->dst_wh_dentry);
23068 +out_thargs:
23069 +       if (a->thargs) {
23070 +               dput(a->h_dst);
23071 +               au_whtmp_rmdir_free(a->thargs);
23072 +               a->thargs = NULL;
23073 +       }
23074 +out:
23075 +       return err;
23076 +}
23077 +
23078 +/* ---------------------------------------------------------------------- */
23079 +
23080 +/*
23081 + * test if @dentry dir can be rename destination or not.
23082 + * success means, it is a logically empty dir.
23083 + */
23084 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23085 +{
23086 +       return au_test_empty(dentry, whlist);
23087 +}
23088 +
23089 +/*
23090 + * test if @a->src_dentry dir can be rename source or not.
23091 + * if it can, return 0.
23092 + * success means,
23093 + * - it is a logically empty dir.
23094 + * - or, it exists on writable branch and has no children including whiteouts
23095 + *   on the lower branch unless DIRREN is on.
23096 + */
23097 +static int may_rename_srcdir(struct au_ren_args *a)
23098 +{
23099 +       int err;
23100 +       unsigned int rdhash;
23101 +       aufs_bindex_t btop, btgt;
23102 +       struct dentry *dentry;
23103 +       struct super_block *sb;
23104 +       struct au_sbinfo *sbinfo;
23105 +
23106 +       dentry = a->src_dentry;
23107 +       sb = dentry->d_sb;
23108 +       sbinfo = au_sbi(sb);
23109 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23110 +               au_fset_ren(a->auren_flags, DIRREN);
23111 +
23112 +       btgt = a->btgt;
23113 +       btop = au_dbtop(dentry);
23114 +       if (btop != btgt) {
23115 +               struct au_nhash whlist;
23116 +
23117 +               SiMustAnyLock(sb);
23118 +               rdhash = sbinfo->si_rdhash;
23119 +               if (!rdhash)
23120 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23121 +                                                          dentry));
23122 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23123 +               if (unlikely(err))
23124 +                       goto out;
23125 +               err = au_test_empty(dentry, &whlist);
23126 +               au_nhash_wh_free(&whlist);
23127 +               goto out;
23128 +       }
23129 +
23130 +       if (btop == au_dbtaildir(dentry))
23131 +               return 0; /* success */
23132 +
23133 +       err = au_test_empty_lower(dentry);
23134 +
23135 +out:
23136 +       if (err == -ENOTEMPTY) {
23137 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23138 +                       err = 0;
23139 +               } else {
23140 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23141 +                               "branches, is not supported\n");
23142 +                       err = -EXDEV;
23143 +               }
23144 +       }
23145 +       return err;
23146 +}
23147 +
23148 +/* side effect: sets whlist and h_dentry */
23149 +static int au_ren_may_dir(struct au_ren_args *a)
23150 +{
23151 +       int err;
23152 +       unsigned int rdhash;
23153 +       struct dentry *d;
23154 +
23155 +       d = a->dst_dentry;
23156 +       SiMustAnyLock(d->d_sb);
23157 +
23158 +       err = 0;
23159 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23160 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23161 +               if (!rdhash)
23162 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23163 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23164 +               if (unlikely(err))
23165 +                       goto out;
23166 +
23167 +               if (!a->exchange) {
23168 +                       au_set_dbtop(d, a->dst_btop);
23169 +                       err = may_rename_dstdir(d, &a->whlist);
23170 +                       au_set_dbtop(d, a->btgt);
23171 +               } else
23172 +                       err = may_rename_srcdir(a);
23173 +       }
23174 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23175 +       if (unlikely(err))
23176 +               goto out;
23177 +
23178 +       d = a->src_dentry;
23179 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23180 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23181 +               err = may_rename_srcdir(a);
23182 +               if (unlikely(err)) {
23183 +                       au_nhash_wh_free(&a->whlist);
23184 +                       a->whlist.nh_num = 0;
23185 +               }
23186 +       }
23187 +out:
23188 +       return err;
23189 +}
23190 +
23191 +/* ---------------------------------------------------------------------- */
23192 +
23193 +/*
23194 + * simple tests for rename.
23195 + * following the checks in vfs, plus the parent-child relationship.
23196 + */
23197 +static int au_may_ren(struct au_ren_args *a)
23198 +{
23199 +       int err, isdir;
23200 +       struct inode *h_inode;
23201 +
23202 +       if (a->src_btop == a->btgt) {
23203 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23204 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23205 +               if (unlikely(err))
23206 +                       goto out;
23207 +               err = -EINVAL;
23208 +               if (unlikely(a->src_h_dentry == a->h_trap))
23209 +                       goto out;
23210 +       }
23211 +
23212 +       err = 0;
23213 +       if (a->dst_btop != a->btgt)
23214 +               goto out;
23215 +
23216 +       err = -ENOTEMPTY;
23217 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23218 +               goto out;
23219 +
23220 +       err = -EIO;
23221 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23222 +       if (d_really_is_negative(a->dst_dentry)) {
23223 +               if (d_is_negative(a->dst_h_dentry))
23224 +                       err = au_may_add(a->dst_dentry, a->btgt,
23225 +                                        a->dst_h_parent, isdir);
23226 +       } else {
23227 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23228 +                       goto out;
23229 +               h_inode = d_inode(a->dst_h_dentry);
23230 +               if (h_inode->i_nlink)
23231 +                       err = au_may_del(a->dst_dentry, a->btgt,
23232 +                                        a->dst_h_parent, isdir);
23233 +       }
23234 +
23235 +out:
23236 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23237 +               err = -EIO;
23238 +       AuTraceErr(err);
23239 +       return err;
23240 +}
23241 +
23242 +/* ---------------------------------------------------------------------- */
23243 +
23244 +/*
23245 + * locking order
23246 + * (VFS)
23247 + * - src_dir and dir by lock_rename()
23248 + * - inode if exists
23249 + * (aufs)
23250 + * - lock all
23251 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23252 + *     + si_read_lock
23253 + *     + di_write_lock2_child()
23254 + *       + di_write_lock_child()
23255 + *        + ii_write_lock_child()
23256 + *       + di_write_lock_child2()
23257 + *        + ii_write_lock_child2()
23258 + *     + src_parent and parent
23259 + *       + di_write_lock_parent()
23260 + *        + ii_write_lock_parent()
23261 + *       + di_write_lock_parent2()
23262 + *        + ii_write_lock_parent2()
23263 + *   + lower src_dir and dir by vfsub_lock_rename()
23264 + *   + verify the every relationships between child and parent. if any
23265 + *     of them failed, unlock all and return -EBUSY.
23266 + */
23267 +static void au_ren_unlock(struct au_ren_args *a)
23268 +{
23269 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23270 +                           a->dst_h_parent, a->dst_hdir);
23271 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23272 +           && a->h_root)
23273 +               au_hn_inode_unlock(a->h_root);
23274 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23275 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23276 +}
23277 +
23278 +static int au_ren_lock(struct au_ren_args *a)
23279 +{
23280 +       int err;
23281 +       unsigned int udba;
23282 +
23283 +       err = 0;
23284 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23285 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23286 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23287 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23288 +
23289 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23290 +       if (unlikely(err))
23291 +               goto out;
23292 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23293 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23294 +               struct dentry *root;
23295 +               struct inode *dir;
23296 +
23297 +               /*
23298 +                * sbinfo is already locked, so this ii_read_lock is
23299 +                * unnecessary. but our debugging feature checks it.
23300 +                */
23301 +               root = a->src_inode->i_sb->s_root;
23302 +               if (root != a->src_parent && root != a->dst_parent) {
23303 +                       dir = d_inode(root);
23304 +                       ii_read_lock_parent3(dir);
23305 +                       a->h_root = au_hi(dir, a->btgt);
23306 +                       ii_read_unlock(dir);
23307 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23308 +               }
23309 +       }
23310 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23311 +                                     a->dst_h_parent, a->dst_hdir);
23312 +       udba = au_opt_udba(a->src_dentry->d_sb);
23313 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23314 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23315 +               err = au_busy_or_stale();
23316 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23317 +               err = au_h_verify(a->src_h_dentry, udba,
23318 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23319 +                                 a->br);
23320 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23321 +               err = au_h_verify(a->dst_h_dentry, udba,
23322 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23323 +                                 a->br);
23324 +       if (!err)
23325 +               goto out; /* success */
23326 +
23327 +       err = au_busy_or_stale();
23328 +       au_ren_unlock(a);
23329 +
23330 +out:
23331 +       return err;
23332 +}
23333 +
23334 +/* ---------------------------------------------------------------------- */
23335 +
23336 +static void au_ren_refresh_dir(struct au_ren_args *a)
23337 +{
23338 +       struct inode *dir;
23339 +
23340 +       dir = a->dst_dir;
23341 +       inode_inc_iversion(dir);
23342 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23343 +               /* is this updating defined in POSIX? */
23344 +               au_cpup_attr_timesizes(a->src_inode);
23345 +               au_cpup_attr_nlink(dir, /*force*/1);
23346 +       }
23347 +       au_dir_ts(dir, a->btgt);
23348 +
23349 +       if (a->exchange) {
23350 +               dir = a->src_dir;
23351 +               inode_inc_iversion(dir);
23352 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23353 +                       /* is this updating defined in POSIX? */
23354 +                       au_cpup_attr_timesizes(a->dst_inode);
23355 +                       au_cpup_attr_nlink(dir, /*force*/1);
23356 +               }
23357 +               au_dir_ts(dir, a->btgt);
23358 +       }
23359 +
23360 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23361 +               return;
23362 +
23363 +       dir = a->src_dir;
23364 +       inode_inc_iversion(dir);
23365 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23366 +               au_cpup_attr_nlink(dir, /*force*/1);
23367 +       au_dir_ts(dir, a->btgt);
23368 +}
23369 +
23370 +static void au_ren_refresh(struct au_ren_args *a)
23371 +{
23372 +       aufs_bindex_t bbot, bindex;
23373 +       struct dentry *d, *h_d;
23374 +       struct inode *i, *h_i;
23375 +       struct super_block *sb;
23376 +
23377 +       d = a->dst_dentry;
23378 +       d_drop(d);
23379 +       if (a->h_dst)
23380 +               /* already dget-ed by au_ren_or_cpup() */
23381 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23382 +
23383 +       i = a->dst_inode;
23384 +       if (i) {
23385 +               if (!a->exchange) {
23386 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23387 +                               vfsub_drop_nlink(i);
23388 +                       else {
23389 +                               vfsub_dead_dir(i);
23390 +                               au_cpup_attr_timesizes(i);
23391 +                       }
23392 +                       au_update_dbrange(d, /*do_put_zero*/1);
23393 +               } else
23394 +                       au_cpup_attr_nlink(i, /*force*/1);
23395 +       } else {
23396 +               bbot = a->btgt;
23397 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23398 +                       au_set_h_dptr(d, bindex, NULL);
23399 +               bbot = au_dbbot(d);
23400 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23401 +                       au_set_h_dptr(d, bindex, NULL);
23402 +               au_update_dbrange(d, /*do_put_zero*/0);
23403 +       }
23404 +
23405 +       if (a->exchange
23406 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23407 +               d_drop(a->src_dentry);
23408 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23409 +                       au_set_dbwh(a->src_dentry, -1);
23410 +               return;
23411 +       }
23412 +
23413 +       d = a->src_dentry;
23414 +       au_set_dbwh(d, -1);
23415 +       bbot = au_dbbot(d);
23416 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23417 +               h_d = au_h_dptr(d, bindex);
23418 +               if (h_d)
23419 +                       au_set_h_dptr(d, bindex, NULL);
23420 +       }
23421 +       au_set_dbbot(d, a->btgt);
23422 +
23423 +       sb = d->d_sb;
23424 +       i = a->src_inode;
23425 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23426 +               return; /* success */
23427 +
23428 +       bbot = au_ibbot(i);
23429 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23430 +               h_i = au_h_iptr(i, bindex);
23431 +               if (h_i) {
23432 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23433 +                       /* ignore this error */
23434 +                       au_set_h_iptr(i, bindex, NULL, 0);
23435 +               }
23436 +       }
23437 +       au_set_ibbot(i, a->btgt);
23438 +}
23439 +
23440 +/* ---------------------------------------------------------------------- */
23441 +
23442 +/* mainly for link(2) and rename(2) */
23443 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23444 +{
23445 +       aufs_bindex_t bdiropq, bwh;
23446 +       struct dentry *parent;
23447 +       struct au_branch *br;
23448 +
23449 +       parent = dentry->d_parent;
23450 +       IMustLock(d_inode(parent)); /* dir is locked */
23451 +
23452 +       bdiropq = au_dbdiropq(parent);
23453 +       bwh = au_dbwh(dentry);
23454 +       br = au_sbr(dentry->d_sb, btgt);
23455 +       if (au_br_rdonly(br)
23456 +           || (0 <= bdiropq && bdiropq < btgt)
23457 +           || (0 <= bwh && bwh < btgt))
23458 +               btgt = -1;
23459 +
23460 +       AuDbg("btgt %d\n", btgt);
23461 +       return btgt;
23462 +}
23463 +
23464 +/* sets src_btop, dst_btop and btgt */
23465 +static int au_ren_wbr(struct au_ren_args *a)
23466 +{
23467 +       int err;
23468 +       struct au_wr_dir_args wr_dir_args = {
23469 +               /* .force_btgt  = -1, */
23470 +               .flags          = AuWrDir_ADD_ENTRY
23471 +       };
23472 +
23473 +       a->src_btop = au_dbtop(a->src_dentry);
23474 +       a->dst_btop = au_dbtop(a->dst_dentry);
23475 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23476 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23477 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23478 +       wr_dir_args.force_btgt = a->src_btop;
23479 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23480 +               wr_dir_args.force_btgt = a->dst_btop;
23481 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23482 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23483 +       a->btgt = err;
23484 +       if (a->exchange)
23485 +               au_update_dbtop(a->dst_dentry);
23486 +
23487 +       return err;
23488 +}
23489 +
23490 +static void au_ren_dt(struct au_ren_args *a)
23491 +{
23492 +       a->h_path.dentry = a->src_h_parent;
23493 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23494 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23495 +               a->h_path.dentry = a->dst_h_parent;
23496 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23497 +       }
23498 +
23499 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23500 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23501 +           && !a->exchange)
23502 +               return;
23503 +
23504 +       a->h_path.dentry = a->src_h_dentry;
23505 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23506 +       if (d_is_positive(a->dst_h_dentry)) {
23507 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23508 +               a->h_path.dentry = a->dst_h_dentry;
23509 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23510 +       }
23511 +}
23512 +
23513 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23514 +{
23515 +       struct dentry *h_d;
23516 +       struct inode *h_inode;
23517 +
23518 +       au_dtime_revert(a->src_dt + AuPARENT);
23519 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23520 +               au_dtime_revert(a->dst_dt + AuPARENT);
23521 +
23522 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23523 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23524 +               h_inode = d_inode(h_d);
23525 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23526 +               au_dtime_revert(a->src_dt + AuCHILD);
23527 +               inode_unlock(h_inode);
23528 +
23529 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23530 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23531 +                       h_inode = d_inode(h_d);
23532 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23533 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23534 +                       inode_unlock(h_inode);
23535 +               }
23536 +       }
23537 +}
23538 +
23539 +/* ---------------------------------------------------------------------- */
23540 +
23541 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23542 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23543 +               unsigned int _flags)
23544 +{
23545 +       int err, lock_flags;
23546 +       void *rev;
23547 +       /* reduce stack space */
23548 +       struct au_ren_args *a;
23549 +       struct au_pin pin;
23550 +
23551 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23552 +       IMustLock(_src_dir);
23553 +       IMustLock(_dst_dir);
23554 +
23555 +       err = -EINVAL;
23556 +       if (unlikely(_flags & RENAME_WHITEOUT))
23557 +               goto out;
23558 +
23559 +       err = -ENOMEM;
23560 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23561 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23562 +       if (unlikely(!a))
23563 +               goto out;
23564 +
23565 +       a->flags = _flags;
23566 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23567 +                    && RENAME_EXCHANGE > U8_MAX);
23568 +       a->exchange = _flags & RENAME_EXCHANGE;
23569 +       a->src_dir = _src_dir;
23570 +       a->src_dentry = _src_dentry;
23571 +       a->src_inode = NULL;
23572 +       if (d_really_is_positive(a->src_dentry))
23573 +               a->src_inode = d_inode(a->src_dentry);
23574 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23575 +       a->dst_dir = _dst_dir;
23576 +       a->dst_dentry = _dst_dentry;
23577 +       a->dst_inode = NULL;
23578 +       if (d_really_is_positive(a->dst_dentry))
23579 +               a->dst_inode = d_inode(a->dst_dentry);
23580 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23581 +       if (a->dst_inode) {
23582 +               /*
23583 +                * if EXCHANGE && src is non-dir && dst is dir,
23584 +                * dst is not locked.
23585 +                */
23586 +               /* IMustLock(a->dst_inode); */
23587 +               au_igrab(a->dst_inode);
23588 +       }
23589 +
23590 +       err = -ENOTDIR;
23591 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23592 +       if (d_is_dir(a->src_dentry)) {
23593 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23594 +               if (unlikely(!a->exchange
23595 +                            && d_really_is_positive(a->dst_dentry)
23596 +                            && !d_is_dir(a->dst_dentry)))
23597 +                       goto out_free;
23598 +               lock_flags |= AuLock_DIRS;
23599 +       }
23600 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23601 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23602 +               if (unlikely(!a->exchange
23603 +                            && d_really_is_positive(a->src_dentry)
23604 +                            && !d_is_dir(a->src_dentry)))
23605 +                       goto out_free;
23606 +               lock_flags |= AuLock_DIRS;
23607 +       }
23608 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23609 +                                       lock_flags);
23610 +       if (unlikely(err))
23611 +               goto out_free;
23612 +
23613 +       err = au_d_hashed_positive(a->src_dentry);
23614 +       if (unlikely(err))
23615 +               goto out_unlock;
23616 +       err = -ENOENT;
23617 +       if (a->dst_inode) {
23618 +               /*
23619 +                * If it is a dir, VFS unhash it before this
23620 +                * function. It means we cannot rely upon d_unhashed().
23621 +                */
23622 +               if (unlikely(!a->dst_inode->i_nlink))
23623 +                       goto out_unlock;
23624 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23625 +                       err = au_d_hashed_positive(a->dst_dentry);
23626 +                       if (unlikely(err && !a->exchange))
23627 +                               goto out_unlock;
23628 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23629 +                       goto out_unlock;
23630 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23631 +               goto out_unlock;
23632 +
23633 +       /*
23634 +        * is it possible?
23635 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23636 +        * there may exist a problem somewhere else.
23637 +        */
23638 +       err = -EINVAL;
23639 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23640 +               goto out_unlock;
23641 +
23642 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23643 +       di_write_lock_parent(a->dst_parent);
23644 +
23645 +       /* which branch we process */
23646 +       err = au_ren_wbr(a);
23647 +       if (unlikely(err < 0))
23648 +               goto out_parent;
23649 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23650 +       a->h_path.mnt = au_br_mnt(a->br);
23651 +
23652 +       /* are they available to be renamed */
23653 +       err = au_ren_may_dir(a);
23654 +       if (unlikely(err))
23655 +               goto out_children;
23656 +
23657 +       /* prepare the writable parent dir on the same branch */
23658 +       if (a->dst_btop == a->btgt) {
23659 +               au_fset_ren(a->auren_flags, WHDST);
23660 +       } else {
23661 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23662 +               if (unlikely(err))
23663 +                       goto out_children;
23664 +       }
23665 +
23666 +       err = 0;
23667 +       if (!a->exchange) {
23668 +               if (a->src_dir != a->dst_dir) {
23669 +                       /*
23670 +                        * this temporary unlock is safe,
23671 +                        * because both dir->i_mutex are locked.
23672 +                        */
23673 +                       di_write_unlock(a->dst_parent);
23674 +                       di_write_lock_parent(a->src_parent);
23675 +                       err = au_wr_dir_need_wh(a->src_dentry,
23676 +                                               au_ftest_ren(a->auren_flags,
23677 +                                                            ISDIR_SRC),
23678 +                                               &a->btgt);
23679 +                       di_write_unlock(a->src_parent);
23680 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23681 +                                             /*isdir*/1);
23682 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23683 +               } else
23684 +                       err = au_wr_dir_need_wh(a->src_dentry,
23685 +                                               au_ftest_ren(a->auren_flags,
23686 +                                                            ISDIR_SRC),
23687 +                                               &a->btgt);
23688 +       }
23689 +       if (unlikely(err < 0))
23690 +               goto out_children;
23691 +       if (err)
23692 +               au_fset_ren(a->auren_flags, WHSRC);
23693 +
23694 +       /* cpup src */
23695 +       if (a->src_btop != a->btgt) {
23696 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23697 +                            au_opt_udba(a->src_dentry->d_sb),
23698 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23699 +               if (!err) {
23700 +                       struct au_cp_generic cpg = {
23701 +                               .dentry = a->src_dentry,
23702 +                               .bdst   = a->btgt,
23703 +                               .bsrc   = a->src_btop,
23704 +                               .len    = -1,
23705 +                               .pin    = &pin,
23706 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23707 +                       };
23708 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23709 +                       err = au_sio_cpup_simple(&cpg);
23710 +                       au_unpin(&pin);
23711 +               }
23712 +               if (unlikely(err))
23713 +                       goto out_children;
23714 +               a->src_btop = a->btgt;
23715 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23716 +               if (!a->exchange)
23717 +                       au_fset_ren(a->auren_flags, WHSRC);
23718 +       }
23719 +
23720 +       /* cpup dst */
23721 +       if (a->exchange && a->dst_inode
23722 +           && a->dst_btop != a->btgt) {
23723 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23724 +                            au_opt_udba(a->dst_dentry->d_sb),
23725 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23726 +               if (!err) {
23727 +                       struct au_cp_generic cpg = {
23728 +                               .dentry = a->dst_dentry,
23729 +                               .bdst   = a->btgt,
23730 +                               .bsrc   = a->dst_btop,
23731 +                               .len    = -1,
23732 +                               .pin    = &pin,
23733 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23734 +                       };
23735 +                       err = au_sio_cpup_simple(&cpg);
23736 +                       au_unpin(&pin);
23737 +               }
23738 +               if (unlikely(err))
23739 +                       goto out_children;
23740 +               a->dst_btop = a->btgt;
23741 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23742 +       }
23743 +
23744 +       /* lock them all */
23745 +       err = au_ren_lock(a);
23746 +       if (unlikely(err))
23747 +               /* leave the copied-up one */
23748 +               goto out_children;
23749 +
23750 +       if (!a->exchange) {
23751 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23752 +                       err = au_may_ren(a);
23753 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23754 +                       err = -ENAMETOOLONG;
23755 +               if (unlikely(err))
23756 +                       goto out_hdir;
23757 +       }
23758 +
23759 +       /* store timestamps to be revertible */
23760 +       au_ren_dt(a);
23761 +
23762 +       /* store dirren info */
23763 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23764 +               err = au_dr_rename(a->src_dentry, a->btgt,
23765 +                                  &a->dst_dentry->d_name, &rev);
23766 +               AuTraceErr(err);
23767 +               if (unlikely(err))
23768 +                       goto out_dt;
23769 +       }
23770 +
23771 +       /* here we go */
23772 +       err = do_rename(a);
23773 +       if (unlikely(err))
23774 +               goto out_dirren;
23775 +
23776 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23777 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23778 +
23779 +       /* update dir attributes */
23780 +       au_ren_refresh_dir(a);
23781 +
23782 +       /* dput/iput all lower dentries */
23783 +       au_ren_refresh(a);
23784 +
23785 +       goto out_hdir; /* success */
23786 +
23787 +out_dirren:
23788 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23789 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23790 +out_dt:
23791 +       au_ren_rev_dt(err, a);
23792 +out_hdir:
23793 +       au_ren_unlock(a);
23794 +out_children:
23795 +       au_nhash_wh_free(&a->whlist);
23796 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23797 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23798 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23799 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23800 +       }
23801 +out_parent:
23802 +       if (!err) {
23803 +               if (d_unhashed(a->src_dentry))
23804 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23805 +               if (d_unhashed(a->dst_dentry))
23806 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23807 +               if (!a->exchange)
23808 +                       d_move(a->src_dentry, a->dst_dentry);
23809 +               else {
23810 +                       d_exchange(a->src_dentry, a->dst_dentry);
23811 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23812 +                               d_drop(a->dst_dentry);
23813 +               }
23814 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23815 +                       d_drop(a->src_dentry);
23816 +       } else {
23817 +               au_update_dbtop(a->dst_dentry);
23818 +               if (!a->dst_inode)
23819 +                       d_drop(a->dst_dentry);
23820 +       }
23821 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23822 +               di_write_unlock(a->dst_parent);
23823 +       else
23824 +               di_write_unlock2(a->src_parent, a->dst_parent);
23825 +out_unlock:
23826 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23827 +out_free:
23828 +       iput(a->dst_inode);
23829 +       if (a->thargs)
23830 +               au_whtmp_rmdir_free(a->thargs);
23831 +       au_kfree_rcu(a);
23832 +out:
23833 +       AuTraceErr(err);
23834 +       return err;
23835 +}
23836 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23837 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23838 +++ linux/fs/aufs/Kconfig       2019-07-11 15:42:14.458904362 +0200
23839 @@ -0,0 +1,199 @@
23840 +# SPDX-License-Identifier: GPL-2.0
23841 +config AUFS_FS
23842 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23843 +       help
23844 +       Aufs is a stackable unification filesystem such as Unionfs,
23845 +       which unifies several directories and provides a merged single
23846 +       directory.
23847 +       In the early days, aufs was entirely re-designed and
23848 +       re-implemented Unionfs Version 1.x series. Introducing many
23849 +       original ideas, approaches and improvements, it becomes totally
23850 +       different from Unionfs while keeping the basic features.
23851 +
23852 +if AUFS_FS
23853 +choice
23854 +       prompt "Maximum number of branches"
23855 +       default AUFS_BRANCH_MAX_127
23856 +       help
23857 +       Specifies the maximum number of branches (or member directories)
23858 +       in a single aufs. The larger value consumes more system
23859 +       resources and has a minor impact to performance.
23860 +config AUFS_BRANCH_MAX_127
23861 +       bool "127"
23862 +       help
23863 +       Specifies the maximum number of branches (or member directories)
23864 +       in a single aufs. The larger value consumes more system
23865 +       resources and has a minor impact to performance.
23866 +config AUFS_BRANCH_MAX_511
23867 +       bool "511"
23868 +       help
23869 +       Specifies the maximum number of branches (or member directories)
23870 +       in a single aufs. The larger value consumes more system
23871 +       resources and has a minor impact to performance.
23872 +config AUFS_BRANCH_MAX_1023
23873 +       bool "1023"
23874 +       help
23875 +       Specifies the maximum number of branches (or member directories)
23876 +       in a single aufs. The larger value consumes more system
23877 +       resources and has a minor impact to performance.
23878 +config AUFS_BRANCH_MAX_32767
23879 +       bool "32767"
23880 +       help
23881 +       Specifies the maximum number of branches (or member directories)
23882 +       in a single aufs. The larger value consumes more system
23883 +       resources and has a minor impact to performance.
23884 +endchoice
23885 +
23886 +config AUFS_SBILIST
23887 +       bool
23888 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23889 +       default y
23890 +       help
23891 +       Automatic configuration for internal use.
23892 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23893 +
23894 +config AUFS_HNOTIFY
23895 +       bool "Detect direct branch access (bypassing aufs)"
23896 +       help
23897 +       If you want to modify files on branches directly, eg. bypassing aufs,
23898 +       and want aufs to detect the changes of them fully, then enable this
23899 +       option and use 'udba=notify' mount option.
23900 +       Currently there is only one available configuration, "fsnotify".
23901 +       It will have a negative impact to the performance.
23902 +       See detail in aufs.5.
23903 +
23904 +choice
23905 +       prompt "method" if AUFS_HNOTIFY
23906 +       default AUFS_HFSNOTIFY
23907 +config AUFS_HFSNOTIFY
23908 +       bool "fsnotify"
23909 +       select FSNOTIFY
23910 +endchoice
23911 +
23912 +config AUFS_EXPORT
23913 +       bool "NFS-exportable aufs"
23914 +       depends on EXPORTFS
23915 +       help
23916 +       If you want to export your mounted aufs via NFS, then enable this
23917 +       option. There are several requirements for this configuration.
23918 +       See detail in aufs.5.
23919 +
23920 +config AUFS_INO_T_64
23921 +       bool
23922 +       depends on AUFS_EXPORT
23923 +       depends on 64BIT && !(ALPHA || S390)
23924 +       default y
23925 +       help
23926 +       Automatic configuration for internal use.
23927 +       /* typedef unsigned long/int __kernel_ino_t */
23928 +       /* alpha and s390x are int */
23929 +
23930 +config AUFS_XATTR
23931 +       bool "support for XATTR/EA (including Security Labels)"
23932 +       help
23933 +       If your branch fs supports XATTR/EA and you want to make them
23934 +       available in aufs too, then enable this opsion and specify the
23935 +       branch attributes for EA.
23936 +       See detail in aufs.5.
23937 +
23938 +config AUFS_FHSM
23939 +       bool "File-based Hierarchical Storage Management"
23940 +       help
23941 +       Hierarchical Storage Management (or HSM) is a well-known feature
23942 +       in the storage world. Aufs provides this feature as file-based.
23943 +       with multiple branches.
23944 +       These multiple branches are prioritized, ie. the topmost one
23945 +       should be the fastest drive and be used heavily.
23946 +
23947 +config AUFS_RDU
23948 +       bool "Readdir in userspace"
23949 +       help
23950 +       Aufs has two methods to provide a merged view for a directory,
23951 +       by a user-space library and by kernel-space natively. The latter
23952 +       is always enabled but sometimes large and slow.
23953 +       If you enable this option, install the library in aufs2-util
23954 +       package, and set some environment variables for your readdir(3),
23955 +       then the work will be handled in user-space which generally
23956 +       shows better performance in most cases.
23957 +       See detail in aufs.5.
23958 +
23959 +config AUFS_DIRREN
23960 +       bool "Workaround for rename(2)-ing a directory"
23961 +       help
23962 +       By default, aufs returns EXDEV error in renameing a dir who has
23963 +       his child on the lower branch, since it is a bad idea to issue
23964 +       rename(2) internally for every lower branch. But user may not
23965 +       accept this behaviour. So here is a workaround to allow such
23966 +       rename(2) and store some extra infromation on the writable
23967 +       branch. Obviously this costs high (and I don't like it).
23968 +       To use this feature, you need to enable this configuration AND
23969 +       to specify the mount option `dirren.'
23970 +       See details in aufs.5 and the design documents.
23971 +
23972 +config AUFS_SHWH
23973 +       bool "Show whiteouts"
23974 +       help
23975 +       If you want to make the whiteouts in aufs visible, then enable
23976 +       this option and specify 'shwh' mount option. Although it may
23977 +       sounds like philosophy or something, but in technically it
23978 +       simply shows the name of whiteout with keeping its behaviour.
23979 +
23980 +config AUFS_BR_RAMFS
23981 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23982 +       help
23983 +       If you want to use ramfs as an aufs branch fs, then enable this
23984 +       option. Generally tmpfs is recommended.
23985 +       Aufs prohibited them to be a branch fs by default, because
23986 +       initramfs becomes unusable after switch_root or something
23987 +       generally. If you sets initramfs as an aufs branch and boot your
23988 +       system by switch_root, you will meet a problem easily since the
23989 +       files in initramfs may be inaccessible.
23990 +       Unless you are going to use ramfs as an aufs branch fs without
23991 +       switch_root or something, leave it N.
23992 +
23993 +config AUFS_BR_FUSE
23994 +       bool "Fuse fs as an aufs branch"
23995 +       depends on FUSE_FS
23996 +       select AUFS_POLL
23997 +       help
23998 +       If you want to use fuse-based userspace filesystem as an aufs
23999 +       branch fs, then enable this option.
24000 +       It implements the internal poll(2) operation which is
24001 +       implemented by fuse only (curretnly).
24002 +
24003 +config AUFS_POLL
24004 +       bool
24005 +       help
24006 +       Automatic configuration for internal use.
24007 +
24008 +config AUFS_BR_HFSPLUS
24009 +       bool "Hfsplus as an aufs branch"
24010 +       depends on HFSPLUS_FS
24011 +       default y
24012 +       help
24013 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24014 +       this option. This option introduces a small overhead at
24015 +       copying-up a file on hfsplus.
24016 +
24017 +config AUFS_BDEV_LOOP
24018 +       bool
24019 +       depends on BLK_DEV_LOOP
24020 +       default y
24021 +       help
24022 +       Automatic configuration for internal use.
24023 +       Convert =[ym] into =y.
24024 +
24025 +config AUFS_DEBUG
24026 +       bool "Debug aufs"
24027 +       help
24028 +       Enable this to compile aufs internal debug code.
24029 +       It will have a negative impact to the performance.
24030 +
24031 +config AUFS_MAGIC_SYSRQ
24032 +       bool
24033 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24034 +       default y
24035 +       help
24036 +       Automatic configuration for internal use.
24037 +       When aufs supports Magic SysRq, enabled automatically.
24038 +endif
24039 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24040 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
24041 +++ linux/fs/aufs/lcnt.h        2020-01-27 10:57:18.175538316 +0100
24042 @@ -0,0 +1,186 @@
24043 +/* SPDX-License-Identifier: GPL-2.0 */
24044 +/*
24045 + * Copyright (C) 2018-2020 Junjiro R. Okajima
24046 + *
24047 + * This program, aufs is free software; you can redistribute it and/or modify
24048 + * it under the terms of the GNU General Public License as published by
24049 + * the Free Software Foundation; either version 2 of the License, or
24050 + * (at your option) any later version.
24051 + *
24052 + * This program is distributed in the hope that it will be useful,
24053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24055 + * GNU General Public License for more details.
24056 + *
24057 + * You should have received a copy of the GNU General Public License
24058 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24059 + */
24060 +
24061 +/*
24062 + * simple long counter wrapper
24063 + */
24064 +
24065 +#ifndef __AUFS_LCNT_H__
24066 +#define __AUFS_LCNT_H__
24067 +
24068 +#ifdef __KERNEL__
24069 +
24070 +#include "debug.h"
24071 +
24072 +#define AuLCntATOMIC   1
24073 +#define AuLCntPCPUCNT  2
24074 +/*
24075 + * why does percpu_refcount require extra synchronize_rcu()s in
24076 + * au_br_do_free()
24077 + */
24078 +#define AuLCntPCPUREF  3
24079 +
24080 +/* #define AuLCntChosen        AuLCntATOMIC */
24081 +#define AuLCntChosen   AuLCntPCPUCNT
24082 +/* #define AuLCntChosen        AuLCntPCPUREF */
24083 +
24084 +#if AuLCntChosen == AuLCntATOMIC
24085 +#include <linux/atomic.h>
24086 +
24087 +typedef atomic_long_t au_lcnt_t;
24088 +
24089 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24090 +{
24091 +       atomic_long_set(cnt, 0);
24092 +       return 0;
24093 +}
24094 +
24095 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24096 +{
24097 +       /* empty */
24098 +}
24099 +
24100 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24101 +                              int do_sync __maybe_unused)
24102 +{
24103 +       /* empty */
24104 +}
24105 +
24106 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24107 +{
24108 +       atomic_long_inc(cnt);
24109 +}
24110 +
24111 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24112 +{
24113 +       atomic_long_dec(cnt);
24114 +}
24115 +
24116 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24117 +{
24118 +       return atomic_long_read(cnt);
24119 +}
24120 +#endif
24121 +
24122 +#if AuLCntChosen == AuLCntPCPUCNT
24123 +#include <linux/percpu_counter.h>
24124 +
24125 +typedef struct percpu_counter au_lcnt_t;
24126 +
24127 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24128 +{
24129 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24130 +}
24131 +
24132 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24133 +{
24134 +       /* empty */
24135 +}
24136 +
24137 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24138 +{
24139 +       percpu_counter_destroy(cnt);
24140 +}
24141 +
24142 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24143 +{
24144 +       percpu_counter_inc(cnt);
24145 +}
24146 +
24147 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24148 +{
24149 +       percpu_counter_dec(cnt);
24150 +}
24151 +
24152 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24153 +{
24154 +       s64 n;
24155 +
24156 +       n = percpu_counter_sum(cnt);
24157 +       BUG_ON(n < 0);
24158 +       if (LONG_MAX != LLONG_MAX
24159 +           && n > LONG_MAX)
24160 +               AuWarn1("%s\n", "wrap-around");
24161 +
24162 +       return n;
24163 +}
24164 +#endif
24165 +
24166 +#if AuLCntChosen == AuLCntPCPUREF
24167 +#include <linux/percpu-refcount.h>
24168 +
24169 +typedef struct percpu_ref au_lcnt_t;
24170 +
24171 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24172 +{
24173 +       if (!release)
24174 +               release = percpu_ref_exit;
24175 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24176 +}
24177 +
24178 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24179 +{
24180 +       synchronize_rcu();
24181 +}
24182 +
24183 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24184 +{
24185 +       percpu_ref_kill(cnt);
24186 +       if (do_sync)
24187 +               au_lcnt_wait_for_fin(cnt);
24188 +}
24189 +
24190 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24191 +{
24192 +       percpu_ref_get(cnt);
24193 +}
24194 +
24195 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24196 +{
24197 +       percpu_ref_put(cnt);
24198 +}
24199 +
24200 +/*
24201 + * avoid calling this func as possible.
24202 + */
24203 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24204 +{
24205 +       long l;
24206 +
24207 +       percpu_ref_switch_to_atomic_sync(cnt);
24208 +       l = atomic_long_read(&cnt->count);
24209 +       if (do_rev)
24210 +               percpu_ref_switch_to_percpu(cnt);
24211 +
24212 +       /* percpu_ref is initialized by 1 instead of 0 */
24213 +       return l - 1;
24214 +}
24215 +#endif
24216 +
24217 +#ifdef CONFIG_AUFS_DEBUG
24218 +#define AuLCntZero(val) do {                   \
24219 +       long l = val;                           \
24220 +       if (l)                                  \
24221 +               AuDbg("%s = %ld\n", #val, l);   \
24222 +} while (0)
24223 +#else
24224 +#define AuLCntZero(val)                do {} while (0)
24225 +#endif
24226 +
24227 +#endif /* __KERNEL__ */
24228 +#endif /* __AUFS_LCNT_H__ */
24229 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24230 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24231 +++ linux/fs/aufs/loop.c        2020-01-27 10:57:18.175538316 +0100
24232 @@ -0,0 +1,148 @@
24233 +// SPDX-License-Identifier: GPL-2.0
24234 +/*
24235 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24236 + *
24237 + * This program, aufs is free software; you can redistribute it and/or modify
24238 + * it under the terms of the GNU General Public License as published by
24239 + * the Free Software Foundation; either version 2 of the License, or
24240 + * (at your option) any later version.
24241 + *
24242 + * This program is distributed in the hope that it will be useful,
24243 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24244 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24245 + * GNU General Public License for more details.
24246 + *
24247 + * You should have received a copy of the GNU General Public License
24248 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24249 + */
24250 +
24251 +/*
24252 + * support for loopback block device as a branch
24253 + */
24254 +
24255 +#include "aufs.h"
24256 +
24257 +/* added into drivers/block/loop.c */
24258 +static struct file *(*backing_file_func)(struct super_block *sb);
24259 +
24260 +/*
24261 + * test if two lower dentries have overlapping branches.
24262 + */
24263 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24264 +{
24265 +       struct super_block *h_sb;
24266 +       struct file *backing_file;
24267 +
24268 +       if (unlikely(!backing_file_func)) {
24269 +               /* don't load "loop" module here */
24270 +               backing_file_func = symbol_get(loop_backing_file);
24271 +               if (unlikely(!backing_file_func))
24272 +                       /* "loop" module is not loaded */
24273 +                       return 0;
24274 +       }
24275 +
24276 +       h_sb = h_adding->d_sb;
24277 +       backing_file = backing_file_func(h_sb);
24278 +       if (!backing_file)
24279 +               return 0;
24280 +
24281 +       h_adding = backing_file->f_path.dentry;
24282 +       /*
24283 +        * h_adding can be local NFS.
24284 +        * in this case aufs cannot detect the loop.
24285 +        */
24286 +       if (unlikely(h_adding->d_sb == sb))
24287 +               return 1;
24288 +       return !!au_test_subdir(h_adding, sb->s_root);
24289 +}
24290 +
24291 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24292 +int au_test_loopback_kthread(void)
24293 +{
24294 +       int ret;
24295 +       struct task_struct *tsk = current;
24296 +       char c, comm[sizeof(tsk->comm)];
24297 +
24298 +       ret = 0;
24299 +       if (tsk->flags & PF_KTHREAD) {
24300 +               get_task_comm(comm, tsk);
24301 +               c = comm[4];
24302 +               ret = ('0' <= c && c <= '9'
24303 +                      && !strncmp(comm, "loop", 4));
24304 +       }
24305 +
24306 +       return ret;
24307 +}
24308 +
24309 +/* ---------------------------------------------------------------------- */
24310 +
24311 +#define au_warn_loopback_step  16
24312 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24313 +static unsigned long *au_warn_loopback_array;
24314 +
24315 +void au_warn_loopback(struct super_block *h_sb)
24316 +{
24317 +       int i, new_nelem;
24318 +       unsigned long *a, magic;
24319 +       static DEFINE_SPINLOCK(spin);
24320 +
24321 +       magic = h_sb->s_magic;
24322 +       spin_lock(&spin);
24323 +       a = au_warn_loopback_array;
24324 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24325 +               if (a[i] == magic) {
24326 +                       spin_unlock(&spin);
24327 +                       return;
24328 +               }
24329 +
24330 +       /* h_sb is new to us, print it */
24331 +       if (i < au_warn_loopback_nelem) {
24332 +               a[i] = magic;
24333 +               goto pr;
24334 +       }
24335 +
24336 +       /* expand the array */
24337 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24338 +       a = au_kzrealloc(au_warn_loopback_array,
24339 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24340 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24341 +                        /*may_shrink*/0);
24342 +       if (a) {
24343 +               au_warn_loopback_nelem = new_nelem;
24344 +               au_warn_loopback_array = a;
24345 +               a[i] = magic;
24346 +               goto pr;
24347 +       }
24348 +
24349 +       spin_unlock(&spin);
24350 +       AuWarn1("realloc failed, ignored\n");
24351 +       return;
24352 +
24353 +pr:
24354 +       spin_unlock(&spin);
24355 +       pr_warn("you may want to try another patch for loopback file "
24356 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24357 +}
24358 +
24359 +int au_loopback_init(void)
24360 +{
24361 +       int err;
24362 +       struct super_block *sb __maybe_unused;
24363 +
24364 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24365 +
24366 +       err = 0;
24367 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24368 +                                        sizeof(unsigned long), GFP_NOFS);
24369 +       if (unlikely(!au_warn_loopback_array))
24370 +               err = -ENOMEM;
24371 +
24372 +       return err;
24373 +}
24374 +
24375 +void au_loopback_fin(void)
24376 +{
24377 +       if (backing_file_func)
24378 +               symbol_put(loop_backing_file);
24379 +       au_kfree_try_rcu(au_warn_loopback_array);
24380 +}
24381 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24382 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24383 +++ linux/fs/aufs/loop.h        2020-01-27 10:57:18.175538316 +0100
24384 @@ -0,0 +1,55 @@
24385 +/* SPDX-License-Identifier: GPL-2.0 */
24386 +/*
24387 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24388 + *
24389 + * This program, aufs is free software; you can redistribute it and/or modify
24390 + * it under the terms of the GNU General Public License as published by
24391 + * the Free Software Foundation; either version 2 of the License, or
24392 + * (at your option) any later version.
24393 + *
24394 + * This program is distributed in the hope that it will be useful,
24395 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24396 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24397 + * GNU General Public License for more details.
24398 + *
24399 + * You should have received a copy of the GNU General Public License
24400 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24401 + */
24402 +
24403 +/*
24404 + * support for loopback mount as a branch
24405 + */
24406 +
24407 +#ifndef __AUFS_LOOP_H__
24408 +#define __AUFS_LOOP_H__
24409 +
24410 +#ifdef __KERNEL__
24411 +
24412 +struct dentry;
24413 +struct super_block;
24414 +
24415 +#ifdef CONFIG_AUFS_BDEV_LOOP
24416 +/* drivers/block/loop.c */
24417 +struct file *loop_backing_file(struct super_block *sb);
24418 +
24419 +/* loop.c */
24420 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24421 +int au_test_loopback_kthread(void);
24422 +void au_warn_loopback(struct super_block *h_sb);
24423 +
24424 +int au_loopback_init(void);
24425 +void au_loopback_fin(void);
24426 +#else
24427 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24428 +
24429 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24430 +          struct dentry *h_adding)
24431 +AuStubInt0(au_test_loopback_kthread, void)
24432 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24433 +
24434 +AuStubInt0(au_loopback_init, void)
24435 +AuStubVoid(au_loopback_fin, void)
24436 +#endif /* BLK_DEV_LOOP */
24437 +
24438 +#endif /* __KERNEL__ */
24439 +#endif /* __AUFS_LOOP_H__ */
24440 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24441 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24442 +++ linux/fs/aufs/magic.mk      2019-07-11 15:42:14.468904634 +0200
24443 @@ -0,0 +1,31 @@
24444 +# SPDX-License-Identifier: GPL-2.0
24445 +
24446 +# defined in ${srctree}/fs/fuse/inode.c
24447 +# tristate
24448 +ifdef CONFIG_FUSE_FS
24449 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24450 +endif
24451 +
24452 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24453 +# tristate
24454 +ifdef CONFIG_XFS_FS
24455 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24456 +endif
24457 +
24458 +# defined in ${srctree}/fs/configfs/mount.c
24459 +# tristate
24460 +ifdef CONFIG_CONFIGFS_FS
24461 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24462 +endif
24463 +
24464 +# defined in ${srctree}/fs/ubifs/ubifs.h
24465 +# tristate
24466 +ifdef CONFIG_UBIFS_FS
24467 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24468 +endif
24469 +
24470 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24471 +# tristate
24472 +ifdef CONFIG_HFSPLUS_FS
24473 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24474 +endif
24475 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24476 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24477 +++ linux/fs/aufs/Makefile      2019-07-11 15:42:14.462237786 +0200
24478 @@ -0,0 +1,46 @@
24479 +# SPDX-License-Identifier: GPL-2.0
24480 +
24481 +include ${src}/magic.mk
24482 +ifeq (${CONFIG_AUFS_FS},m)
24483 +include ${src}/conf.mk
24484 +endif
24485 +-include ${src}/priv_def.mk
24486 +
24487 +# cf. include/linux/kernel.h
24488 +# enable pr_debug
24489 +ccflags-y += -DDEBUG
24490 +# sparse requires the full pathname
24491 +ifdef M
24492 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24493 +else
24494 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24495 +endif
24496 +
24497 +obj-$(CONFIG_AUFS_FS) += aufs.o
24498 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24499 +       wkq.o vfsub.o dcsub.o \
24500 +       cpup.o whout.o wbr_policy.o \
24501 +       dinfo.o dentry.o \
24502 +       dynop.o \
24503 +       finfo.o file.o f_op.o \
24504 +       dir.o vdir.o \
24505 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24506 +       mvdown.o ioctl.o
24507 +
24508 +# all are boolean
24509 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24510 +aufs-$(CONFIG_SYSFS) += sysfs.o
24511 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24512 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24513 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24514 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24515 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24516 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24517 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24518 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24519 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24520 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24521 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24522 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24523 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24524 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24525 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24526 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24527 +++ linux/fs/aufs/module.c      2020-01-27 10:57:18.175538316 +0100
24528 @@ -0,0 +1,273 @@
24529 +// SPDX-License-Identifier: GPL-2.0
24530 +/*
24531 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24532 + *
24533 + * This program, aufs is free software; you can redistribute it and/or modify
24534 + * it under the terms of the GNU General Public License as published by
24535 + * the Free Software Foundation; either version 2 of the License, or
24536 + * (at your option) any later version.
24537 + *
24538 + * This program is distributed in the hope that it will be useful,
24539 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24540 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24541 + * GNU General Public License for more details.
24542 + *
24543 + * You should have received a copy of the GNU General Public License
24544 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24545 + */
24546 +
24547 +/*
24548 + * module global variables and operations
24549 + */
24550 +
24551 +#include <linux/module.h>
24552 +#include <linux/seq_file.h>
24553 +#include "aufs.h"
24554 +
24555 +/* shrinkable realloc */
24556 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24557 +{
24558 +       size_t sz;
24559 +       int diff;
24560 +
24561 +       sz = 0;
24562 +       diff = -1;
24563 +       if (p) {
24564 +#if 0 /* unused */
24565 +               if (!new_sz) {
24566 +                       au_kfree_rcu(p);
24567 +                       p = NULL;
24568 +                       goto out;
24569 +               }
24570 +#else
24571 +               AuDebugOn(!new_sz);
24572 +#endif
24573 +               sz = ksize(p);
24574 +               diff = au_kmidx_sub(sz, new_sz);
24575 +       }
24576 +       if (sz && !diff)
24577 +               goto out;
24578 +
24579 +       if (sz < new_sz)
24580 +               /* expand or SLOB */
24581 +               p = krealloc(p, new_sz, gfp);
24582 +       else if (new_sz < sz && may_shrink) {
24583 +               /* shrink */
24584 +               void *q;
24585 +
24586 +               q = kmalloc(new_sz, gfp);
24587 +               if (q) {
24588 +                       if (p) {
24589 +                               memcpy(q, p, new_sz);
24590 +                               au_kfree_try_rcu(p);
24591 +                       }
24592 +                       p = q;
24593 +               } else
24594 +                       p = NULL;
24595 +       }
24596 +
24597 +out:
24598 +       return p;
24599 +}
24600 +
24601 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24602 +                  int may_shrink)
24603 +{
24604 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24605 +       if (p && new_sz > nused)
24606 +               memset(p + nused, 0, new_sz - nused);
24607 +       return p;
24608 +}
24609 +
24610 +/* ---------------------------------------------------------------------- */
24611 +/*
24612 + * aufs caches
24613 + */
24614 +struct kmem_cache *au_cache[AuCache_Last];
24615 +
24616 +static void au_cache_fin(void)
24617 +{
24618 +       int i;
24619 +
24620 +       /*
24621 +        * Make sure all delayed rcu free inodes are flushed before we
24622 +        * destroy cache.
24623 +        */
24624 +       rcu_barrier();
24625 +
24626 +       /* excluding AuCache_HNOTIFY */
24627 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24628 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24629 +               kmem_cache_destroy(au_cache[i]);
24630 +               au_cache[i] = NULL;
24631 +       }
24632 +}
24633 +
24634 +static int __init au_cache_init(void)
24635 +{
24636 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24637 +       if (au_cache[AuCache_DINFO])
24638 +               /* SLAB_DESTROY_BY_RCU */
24639 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24640 +                                                      au_icntnr_init_once);
24641 +       if (au_cache[AuCache_ICNTNR])
24642 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24643 +                                                     au_fi_init_once);
24644 +       if (au_cache[AuCache_FINFO])
24645 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24646 +       if (au_cache[AuCache_VDIR])
24647 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24648 +       if (au_cache[AuCache_DEHSTR])
24649 +               return 0;
24650 +
24651 +       au_cache_fin();
24652 +       return -ENOMEM;
24653 +}
24654 +
24655 +/* ---------------------------------------------------------------------- */
24656 +
24657 +int au_dir_roflags;
24658 +
24659 +#ifdef CONFIG_AUFS_SBILIST
24660 +/*
24661 + * iterate_supers_type() doesn't protect us from
24662 + * remounting (branch management)
24663 + */
24664 +struct hlist_bl_head au_sbilist;
24665 +#endif
24666 +
24667 +/*
24668 + * functions for module interface.
24669 + */
24670 +MODULE_LICENSE("GPL");
24671 +/* MODULE_LICENSE("GPL v2"); */
24672 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24673 +MODULE_DESCRIPTION(AUFS_NAME
24674 +       " -- Advanced multi layered unification filesystem");
24675 +MODULE_VERSION(AUFS_VERSION);
24676 +MODULE_ALIAS_FS(AUFS_NAME);
24677 +
24678 +/* this module parameter has no meaning when SYSFS is disabled */
24679 +int sysaufs_brs = 1;
24680 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24681 +module_param_named(brs, sysaufs_brs, int, 0444);
24682 +
24683 +/* this module parameter has no meaning when USER_NS is disabled */
24684 +bool au_userns;
24685 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24686 +module_param_named(allow_userns, au_userns, bool, 0444);
24687 +
24688 +/* ---------------------------------------------------------------------- */
24689 +
24690 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24691 +
24692 +int au_seq_path(struct seq_file *seq, struct path *path)
24693 +{
24694 +       int err;
24695 +
24696 +       err = seq_path(seq, path, au_esc_chars);
24697 +       if (err >= 0)
24698 +               err = 0;
24699 +       else
24700 +               err = -ENOMEM;
24701 +
24702 +       return err;
24703 +}
24704 +
24705 +/* ---------------------------------------------------------------------- */
24706 +
24707 +static int __init aufs_init(void)
24708 +{
24709 +       int err, i;
24710 +       char *p;
24711 +
24712 +       p = au_esc_chars;
24713 +       for (i = 1; i <= ' '; i++)
24714 +               *p++ = i;
24715 +       *p++ = '\\';
24716 +       *p++ = '\x7f';
24717 +       *p = 0;
24718 +
24719 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24720 +
24721 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24722 +       for (i = 0; i < AuIop_Last; i++)
24723 +               aufs_iop_nogetattr[i].getattr = NULL;
24724 +
24725 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24726 +
24727 +       au_sbilist_init();
24728 +       sysaufs_brs_init();
24729 +       au_debug_init();
24730 +       au_dy_init();
24731 +       err = sysaufs_init();
24732 +       if (unlikely(err))
24733 +               goto out;
24734 +       err = dbgaufs_init();
24735 +       if (unlikely(err))
24736 +               goto out_sysaufs;
24737 +       err = au_procfs_init();
24738 +       if (unlikely(err))
24739 +               goto out_dbgaufs;
24740 +       err = au_wkq_init();
24741 +       if (unlikely(err))
24742 +               goto out_procfs;
24743 +       err = au_loopback_init();
24744 +       if (unlikely(err))
24745 +               goto out_wkq;
24746 +       err = au_hnotify_init();
24747 +       if (unlikely(err))
24748 +               goto out_loopback;
24749 +       err = au_sysrq_init();
24750 +       if (unlikely(err))
24751 +               goto out_hin;
24752 +       err = au_cache_init();
24753 +       if (unlikely(err))
24754 +               goto out_sysrq;
24755 +
24756 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24757 +       err = register_filesystem(&aufs_fs_type);
24758 +       if (unlikely(err))
24759 +               goto out_cache;
24760 +
24761 +       /* since we define pr_fmt, call printk directly */
24762 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24763 +       goto out; /* success */
24764 +
24765 +out_cache:
24766 +       au_cache_fin();
24767 +out_sysrq:
24768 +       au_sysrq_fin();
24769 +out_hin:
24770 +       au_hnotify_fin();
24771 +out_loopback:
24772 +       au_loopback_fin();
24773 +out_wkq:
24774 +       au_wkq_fin();
24775 +out_procfs:
24776 +       au_procfs_fin();
24777 +out_dbgaufs:
24778 +       dbgaufs_fin();
24779 +out_sysaufs:
24780 +       sysaufs_fin();
24781 +       au_dy_fin();
24782 +out:
24783 +       return err;
24784 +}
24785 +
24786 +static void __exit aufs_exit(void)
24787 +{
24788 +       unregister_filesystem(&aufs_fs_type);
24789 +       au_cache_fin();
24790 +       au_sysrq_fin();
24791 +       au_hnotify_fin();
24792 +       au_loopback_fin();
24793 +       au_wkq_fin();
24794 +       au_procfs_fin();
24795 +       dbgaufs_fin();
24796 +       sysaufs_fin();
24797 +       au_dy_fin();
24798 +}
24799 +
24800 +module_init(aufs_init);
24801 +module_exit(aufs_exit);
24802 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24803 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24804 +++ linux/fs/aufs/module.h      2020-01-27 10:57:18.175538316 +0100
24805 @@ -0,0 +1,166 @@
24806 +/* SPDX-License-Identifier: GPL-2.0 */
24807 +/*
24808 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24809 + *
24810 + * This program, aufs is free software; you can redistribute it and/or modify
24811 + * it under the terms of the GNU General Public License as published by
24812 + * the Free Software Foundation; either version 2 of the License, or
24813 + * (at your option) any later version.
24814 + *
24815 + * This program is distributed in the hope that it will be useful,
24816 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24817 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24818 + * GNU General Public License for more details.
24819 + *
24820 + * You should have received a copy of the GNU General Public License
24821 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24822 + */
24823 +
24824 +/*
24825 + * module initialization and module-global
24826 + */
24827 +
24828 +#ifndef __AUFS_MODULE_H__
24829 +#define __AUFS_MODULE_H__
24830 +
24831 +#ifdef __KERNEL__
24832 +
24833 +#include <linux/slab.h>
24834 +#include "debug.h"
24835 +#include "dentry.h"
24836 +#include "dir.h"
24837 +#include "file.h"
24838 +#include "inode.h"
24839 +
24840 +struct path;
24841 +struct seq_file;
24842 +
24843 +/* module parameters */
24844 +extern int sysaufs_brs;
24845 +extern bool au_userns;
24846 +
24847 +/* ---------------------------------------------------------------------- */
24848 +
24849 +extern int au_dir_roflags;
24850 +
24851 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24852 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24853 +                  int may_shrink);
24854 +
24855 +/*
24856 + * Comparing the size of the object with sizeof(struct rcu_head)
24857 + * case 1: object is always larger
24858 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24859 + * case 2: object is always smaller
24860 + *     --> au_kfree_small()
24861 + * case 3: object can be any size
24862 + *     --> au_kfree_try_rcu()
24863 + */
24864 +
24865 +static inline void au_kfree_do_rcu(const void *p)
24866 +{
24867 +       struct {
24868 +               struct rcu_head rcu;
24869 +       } *a = (void *)p;
24870 +
24871 +       kfree_rcu(a, rcu);
24872 +}
24873 +
24874 +#define au_kfree_rcu(_p) do {                                          \
24875 +               typeof(_p) p = (_p);                                    \
24876 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24877 +               if (p)                                                  \
24878 +                       au_kfree_do_rcu(p);                             \
24879 +       } while (0)
24880 +
24881 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24882 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24883 +
24884 +static inline void au_kfree_try_rcu(const void *p)
24885 +{
24886 +       if (!p)
24887 +               return;
24888 +       if (au_kfree_sz_test(p))
24889 +               au_kfree_do_rcu(p);
24890 +       else
24891 +               kfree(p);
24892 +}
24893 +
24894 +static inline void au_kfree_small(const void *p)
24895 +{
24896 +       if (!p)
24897 +               return;
24898 +       AuDebugOn(au_kfree_sz_test(p));
24899 +       kfree(p);
24900 +}
24901 +
24902 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24903 +{
24904 +#ifndef CONFIG_SLOB
24905 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24906 +#else
24907 +       return -1; /* SLOB is untested */
24908 +#endif
24909 +}
24910 +
24911 +int au_seq_path(struct seq_file *seq, struct path *path);
24912 +
24913 +#ifdef CONFIG_PROC_FS
24914 +/* procfs.c */
24915 +int __init au_procfs_init(void);
24916 +void au_procfs_fin(void);
24917 +#else
24918 +AuStubInt0(au_procfs_init, void);
24919 +AuStubVoid(au_procfs_fin, void);
24920 +#endif
24921 +
24922 +/* ---------------------------------------------------------------------- */
24923 +
24924 +/* kmem cache */
24925 +enum {
24926 +       AuCache_DINFO,
24927 +       AuCache_ICNTNR,
24928 +       AuCache_FINFO,
24929 +       AuCache_VDIR,
24930 +       AuCache_DEHSTR,
24931 +       AuCache_HNOTIFY, /* must be last */
24932 +       AuCache_Last
24933 +};
24934 +
24935 +extern struct kmem_cache *au_cache[AuCache_Last];
24936 +
24937 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24938 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24939 +#define AuCacheCtor(type, ctor)        \
24940 +       kmem_cache_create(#type, sizeof(struct type), \
24941 +                         __alignof__(struct type), AuCacheFlags, ctor)
24942 +
24943 +#define AuCacheFuncs(name, index)                                      \
24944 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24945 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24946 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24947 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24948 +                                                                       \
24949 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24950 +       { void *p = rcu;                                                \
24951 +               p -= offsetof(struct au_##name, rcu);                   \
24952 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24953 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24954 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24955 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24956 +                                                                       \
24957 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24958 +       { /* au_cache_free_##name##_norcu(p); */                        \
24959 +               au_cache_free_##name##_rcu(p); }
24960 +
24961 +AuCacheFuncs(dinfo, DINFO);
24962 +AuCacheFuncs(icntnr, ICNTNR);
24963 +AuCacheFuncs(finfo, FINFO);
24964 +AuCacheFuncs(vdir, VDIR);
24965 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24966 +#ifdef CONFIG_AUFS_HNOTIFY
24967 +AuCacheFuncs(hnotify, HNOTIFY);
24968 +#endif
24969 +
24970 +#endif /* __KERNEL__ */
24971 +#endif /* __AUFS_MODULE_H__ */
24972 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24973 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24974 +++ linux/fs/aufs/mvdown.c      2020-01-27 10:57:18.175538316 +0100
24975 @@ -0,0 +1,706 @@
24976 +// SPDX-License-Identifier: GPL-2.0
24977 +/*
24978 + * Copyright (C) 2011-2020 Junjiro R. Okajima
24979 + *
24980 + * This program, aufs is free software; you can redistribute it and/or modify
24981 + * it under the terms of the GNU General Public License as published by
24982 + * the Free Software Foundation; either version 2 of the License, or
24983 + * (at your option) any later version.
24984 + *
24985 + * This program is distributed in the hope that it will be useful,
24986 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24987 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24988 + * GNU General Public License for more details.
24989 + *
24990 + * You should have received a copy of the GNU General Public License
24991 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24992 + */
24993 +
24994 +/*
24995 + * move-down, opposite of copy-up
24996 + */
24997 +
24998 +#include "aufs.h"
24999 +
25000 +struct au_mvd_args {
25001 +       struct {
25002 +               struct super_block *h_sb;
25003 +               struct dentry *h_parent;
25004 +               struct au_hinode *hdir;
25005 +               struct inode *h_dir, *h_inode;
25006 +               struct au_pin pin;
25007 +       } info[AUFS_MVDOWN_NARRAY];
25008 +
25009 +       struct aufs_mvdown mvdown;
25010 +       struct dentry *dentry, *parent;
25011 +       struct inode *inode, *dir;
25012 +       struct super_block *sb;
25013 +       aufs_bindex_t bopq, bwh, bfound;
25014 +       unsigned char rename_lock;
25015 +};
25016 +
25017 +#define mvd_errno              mvdown.au_errno
25018 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25019 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25020 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25021 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25022 +
25023 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
25024 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
25025 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
25026 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
25027 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
25028 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
25029 +
25030 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
25031 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
25032 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
25033 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
25034 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
25035 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
25036 +
25037 +#define AU_MVD_PR(flag, ...) do {                      \
25038 +               if (flag)                               \
25039 +                       pr_err(__VA_ARGS__);            \
25040 +       } while (0)
25041 +
25042 +static int find_lower_writable(struct au_mvd_args *a)
25043 +{
25044 +       struct super_block *sb;
25045 +       aufs_bindex_t bindex, bbot;
25046 +       struct au_branch *br;
25047 +
25048 +       sb = a->sb;
25049 +       bindex = a->mvd_bsrc;
25050 +       bbot = au_sbbot(sb);
25051 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25052 +               for (bindex++; bindex <= bbot; bindex++) {
25053 +                       br = au_sbr(sb, bindex);
25054 +                       if (au_br_fhsm(br->br_perm)
25055 +                           && !sb_rdonly(au_br_sb(br)))
25056 +                               return bindex;
25057 +               }
25058 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25059 +               for (bindex++; bindex <= bbot; bindex++) {
25060 +                       br = au_sbr(sb, bindex);
25061 +                       if (!au_br_rdonly(br))
25062 +                               return bindex;
25063 +               }
25064 +       else
25065 +               for (bindex++; bindex <= bbot; bindex++) {
25066 +                       br = au_sbr(sb, bindex);
25067 +                       if (!sb_rdonly(au_br_sb(br))) {
25068 +                               if (au_br_rdonly(br))
25069 +                                       a->mvdown.flags
25070 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25071 +                               return bindex;
25072 +                       }
25073 +               }
25074 +
25075 +       return -1;
25076 +}
25077 +
25078 +/* make the parent dir on bdst */
25079 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25080 +{
25081 +       int err;
25082 +
25083 +       err = 0;
25084 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25085 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25086 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25087 +       a->mvd_h_dst_parent = NULL;
25088 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25089 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25090 +       if (!a->mvd_h_dst_parent) {
25091 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25092 +               if (unlikely(err)) {
25093 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25094 +                       goto out;
25095 +               }
25096 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25097 +       }
25098 +
25099 +out:
25100 +       AuTraceErr(err);
25101 +       return err;
25102 +}
25103 +
25104 +/* lock them all */
25105 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25106 +{
25107 +       int err;
25108 +       struct dentry *h_trap;
25109 +
25110 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25111 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25112 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25113 +                    au_opt_udba(a->sb),
25114 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25115 +       AuTraceErr(err);
25116 +       if (unlikely(err)) {
25117 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25118 +               goto out;
25119 +       }
25120 +
25121 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25122 +               a->rename_lock = 0;
25123 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25124 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25125 +                           au_opt_udba(a->sb),
25126 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25127 +               err = au_do_pin(&a->mvd_pin_src);
25128 +               AuTraceErr(err);
25129 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25130 +               if (unlikely(err)) {
25131 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25132 +                       goto out_dst;
25133 +               }
25134 +               goto out; /* success */
25135 +       }
25136 +
25137 +       a->rename_lock = 1;
25138 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25139 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25140 +                    au_opt_udba(a->sb),
25141 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25142 +       AuTraceErr(err);
25143 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25144 +       if (unlikely(err)) {
25145 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25146 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25147 +               goto out_dst;
25148 +       }
25149 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25150 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25151 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25152 +       if (h_trap) {
25153 +               err = (h_trap != a->mvd_h_src_parent);
25154 +               if (err)
25155 +                       err = (h_trap != a->mvd_h_dst_parent);
25156 +       }
25157 +       BUG_ON(err); /* it should never happen */
25158 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25159 +               err = -EBUSY;
25160 +               AuTraceErr(err);
25161 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25162 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25163 +               au_pin_hdir_lock(&a->mvd_pin_src);
25164 +               au_unpin(&a->mvd_pin_src);
25165 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25166 +               goto out_dst;
25167 +       }
25168 +       goto out; /* success */
25169 +
25170 +out_dst:
25171 +       au_unpin(&a->mvd_pin_dst);
25172 +out:
25173 +       AuTraceErr(err);
25174 +       return err;
25175 +}
25176 +
25177 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25178 +{
25179 +       if (!a->rename_lock)
25180 +               au_unpin(&a->mvd_pin_src);
25181 +       else {
25182 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25183 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25184 +               au_pin_hdir_lock(&a->mvd_pin_src);
25185 +               au_unpin(&a->mvd_pin_src);
25186 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25187 +       }
25188 +       au_unpin(&a->mvd_pin_dst);
25189 +}
25190 +
25191 +/* copy-down the file */
25192 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25193 +{
25194 +       int err;
25195 +       struct au_cp_generic cpg = {
25196 +               .dentry = a->dentry,
25197 +               .bdst   = a->mvd_bdst,
25198 +               .bsrc   = a->mvd_bsrc,
25199 +               .len    = -1,
25200 +               .pin    = &a->mvd_pin_dst,
25201 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25202 +       };
25203 +
25204 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25205 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25206 +               au_fset_cpup(cpg.flags, OVERWRITE);
25207 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25208 +               au_fset_cpup(cpg.flags, RWDST);
25209 +       err = au_sio_cpdown_simple(&cpg);
25210 +       if (unlikely(err))
25211 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25212 +
25213 +       AuTraceErr(err);
25214 +       return err;
25215 +}
25216 +
25217 +/*
25218 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25219 + * were sleeping
25220 + */
25221 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25222 +{
25223 +       int err;
25224 +       struct path h_path;
25225 +       struct au_branch *br;
25226 +       struct inode *delegated;
25227 +
25228 +       br = au_sbr(a->sb, a->mvd_bdst);
25229 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25230 +       err = PTR_ERR(h_path.dentry);
25231 +       if (IS_ERR(h_path.dentry)) {
25232 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25233 +               goto out;
25234 +       }
25235 +
25236 +       err = 0;
25237 +       if (d_is_positive(h_path.dentry)) {
25238 +               h_path.mnt = au_br_mnt(br);
25239 +               delegated = NULL;
25240 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25241 +                                  &delegated, /*force*/0);
25242 +               if (unlikely(err == -EWOULDBLOCK)) {
25243 +                       pr_warn("cannot retry for NFSv4 delegation"
25244 +                               " for an internal unlink\n");
25245 +                       iput(delegated);
25246 +               }
25247 +               if (unlikely(err))
25248 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25249 +       }
25250 +       dput(h_path.dentry);
25251 +
25252 +out:
25253 +       AuTraceErr(err);
25254 +       return err;
25255 +}
25256 +
25257 +/*
25258 + * unlink the topmost h_dentry
25259 + */
25260 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25261 +{
25262 +       int err;
25263 +       struct path h_path;
25264 +       struct inode *delegated;
25265 +
25266 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25267 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25268 +       delegated = NULL;
25269 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25270 +       if (unlikely(err == -EWOULDBLOCK)) {
25271 +               pr_warn("cannot retry for NFSv4 delegation"
25272 +                       " for an internal unlink\n");
25273 +               iput(delegated);
25274 +       }
25275 +       if (unlikely(err))
25276 +               AU_MVD_PR(dmsg, "unlink failed\n");
25277 +
25278 +       AuTraceErr(err);
25279 +       return err;
25280 +}
25281 +
25282 +/* Since mvdown succeeded, we ignore an error of this function */
25283 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25284 +{
25285 +       int err;
25286 +       struct au_branch *br;
25287 +
25288 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25289 +       br = au_sbr(a->sb, a->mvd_bsrc);
25290 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25291 +       if (!err) {
25292 +               br = au_sbr(a->sb, a->mvd_bdst);
25293 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25294 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25295 +       }
25296 +       if (!err)
25297 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25298 +       else
25299 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25300 +}
25301 +
25302 +/*
25303 + * copy-down the file and unlink the bsrc file.
25304 + * - unlink the bdst whout if exist
25305 + * - copy-down the file (with whtmp name and rename)
25306 + * - unlink the bsrc file
25307 + */
25308 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25309 +{
25310 +       int err;
25311 +
25312 +       err = au_do_mkdir(dmsg, a);
25313 +       if (!err)
25314 +               err = au_do_lock(dmsg, a);
25315 +       if (unlikely(err))
25316 +               goto out;
25317 +
25318 +       /*
25319 +        * do not revert the activities we made on bdst since they should be
25320 +        * harmless in aufs.
25321 +        */
25322 +
25323 +       err = au_do_cpdown(dmsg, a);
25324 +       if (!err)
25325 +               err = au_do_unlink_wh(dmsg, a);
25326 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25327 +               err = au_do_unlink(dmsg, a);
25328 +       if (unlikely(err))
25329 +               goto out_unlock;
25330 +
25331 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25332 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25333 +       if (find_lower_writable(a) < 0)
25334 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25335 +
25336 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25337 +               au_do_stfs(dmsg, a);
25338 +
25339 +       /* maintain internal array */
25340 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25341 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25342 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25343 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25344 +               au_set_ibtop(a->inode, a->mvd_bdst);
25345 +       } else {
25346 +               /* hide the lower */
25347 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25348 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25349 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25350 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25351 +       }
25352 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25353 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25354 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25355 +               au_set_ibbot(a->inode, a->mvd_bdst);
25356 +
25357 +out_unlock:
25358 +       au_do_unlock(dmsg, a);
25359 +out:
25360 +       AuTraceErr(err);
25361 +       return err;
25362 +}
25363 +
25364 +/* ---------------------------------------------------------------------- */
25365 +
25366 +/* make sure the file is idle */
25367 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25368 +{
25369 +       int err, plinked;
25370 +
25371 +       err = 0;
25372 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25373 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25374 +           && au_dcount(a->dentry) == 1
25375 +           && atomic_read(&a->inode->i_count) == 1
25376 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25377 +           && (!plinked || !au_plink_test(a->inode))
25378 +           && a->inode->i_nlink == 1)
25379 +               goto out;
25380 +
25381 +       err = -EBUSY;
25382 +       AU_MVD_PR(dmsg,
25383 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25384 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25385 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25386 +                 a->mvd_h_src_inode->i_nlink,
25387 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25388 +
25389 +out:
25390 +       AuTraceErr(err);
25391 +       return err;
25392 +}
25393 +
25394 +/* make sure the parent dir is fine */
25395 +static int au_mvd_args_parent(const unsigned char dmsg,
25396 +                             struct au_mvd_args *a)
25397 +{
25398 +       int err;
25399 +       aufs_bindex_t bindex;
25400 +
25401 +       err = 0;
25402 +       if (unlikely(au_alive_dir(a->parent))) {
25403 +               err = -ENOENT;
25404 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25405 +               goto out;
25406 +       }
25407 +
25408 +       a->bopq = au_dbdiropq(a->parent);
25409 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25410 +       AuDbg("b%d\n", bindex);
25411 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25412 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25413 +               err = -EINVAL;
25414 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25415 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25416 +                         a->bopq, a->mvd_bdst);
25417 +       }
25418 +
25419 +out:
25420 +       AuTraceErr(err);
25421 +       return err;
25422 +}
25423 +
25424 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25425 +                                   struct au_mvd_args *a)
25426 +{
25427 +       int err;
25428 +       struct au_dinfo *dinfo, *tmp;
25429 +
25430 +       /* lookup the next lower positive entry */
25431 +       err = -ENOMEM;
25432 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25433 +       if (unlikely(!tmp))
25434 +               goto out;
25435 +
25436 +       a->bfound = -1;
25437 +       a->bwh = -1;
25438 +       dinfo = au_di(a->dentry);
25439 +       au_di_cp(tmp, dinfo);
25440 +       au_di_swap(tmp, dinfo);
25441 +
25442 +       /* returns the number of positive dentries */
25443 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25444 +                            /* AuLkup_IGNORE_PERM */ 0);
25445 +       if (!err)
25446 +               a->bwh = au_dbwh(a->dentry);
25447 +       else if (err > 0)
25448 +               a->bfound = au_dbtop(a->dentry);
25449 +
25450 +       au_di_swap(tmp, dinfo);
25451 +       au_rw_write_unlock(&tmp->di_rwsem);
25452 +       au_di_free(tmp);
25453 +       if (unlikely(err < 0))
25454 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25455 +
25456 +       /*
25457 +        * here, we have these cases.
25458 +        * bfound == -1
25459 +        *      no positive dentry under bsrc. there are more sub-cases.
25460 +        *      bwh < 0
25461 +        *              there no whiteout, we can safely move-down.
25462 +        *      bwh <= bsrc
25463 +        *              impossible
25464 +        *      bsrc < bwh && bwh < bdst
25465 +        *              there is a whiteout on RO branch. cannot proceed.
25466 +        *      bwh == bdst
25467 +        *              there is a whiteout on the RW target branch. it should
25468 +        *              be removed.
25469 +        *      bdst < bwh
25470 +        *              there is a whiteout somewhere unrelated branch.
25471 +        * -1 < bfound && bfound <= bsrc
25472 +        *      impossible.
25473 +        * bfound < bdst
25474 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25475 +        *      proceed.
25476 +        * bfound == bdst
25477 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25478 +        *      error.
25479 +        * bdst < bfound
25480 +        *      found, after we create the file on bdst, it will be hidden.
25481 +        */
25482 +
25483 +       AuDebugOn(a->bfound == -1
25484 +                 && a->bwh != -1
25485 +                 && a->bwh <= a->mvd_bsrc);
25486 +       AuDebugOn(-1 < a->bfound
25487 +                 && a->bfound <= a->mvd_bsrc);
25488 +
25489 +       err = -EINVAL;
25490 +       if (a->bfound == -1
25491 +           && a->mvd_bsrc < a->bwh
25492 +           && a->bwh != -1
25493 +           && a->bwh < a->mvd_bdst) {
25494 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25495 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25496 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25497 +               goto out;
25498 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25499 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25500 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25501 +                         a->mvd_bdst, a->bfound);
25502 +               goto out;
25503 +       }
25504 +
25505 +       err = 0; /* success */
25506 +
25507 +out:
25508 +       AuTraceErr(err);
25509 +       return err;
25510 +}
25511 +
25512 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25513 +{
25514 +       int err;
25515 +
25516 +       err = 0;
25517 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25518 +           && a->bfound == a->mvd_bdst)
25519 +               err = -EEXIST;
25520 +       AuTraceErr(err);
25521 +       return err;
25522 +}
25523 +
25524 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25525 +{
25526 +       int err;
25527 +       struct au_branch *br;
25528 +
25529 +       err = -EISDIR;
25530 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25531 +               goto out;
25532 +
25533 +       err = -EINVAL;
25534 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25535 +               a->mvd_bsrc = au_ibtop(a->inode);
25536 +       else {
25537 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25538 +               if (unlikely(a->mvd_bsrc < 0
25539 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25540 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25541 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25542 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25543 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25544 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25545 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25546 +                       AU_MVD_PR(dmsg, "no upper\n");
25547 +                       goto out;
25548 +               }
25549 +       }
25550 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25551 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25552 +               AU_MVD_PR(dmsg, "on the bottom\n");
25553 +               goto out;
25554 +       }
25555 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25556 +       br = au_sbr(a->sb, a->mvd_bsrc);
25557 +       err = au_br_rdonly(br);
25558 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25559 +               if (unlikely(err))
25560 +                       goto out;
25561 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25562 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25563 +               if (err)
25564 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25565 +               /* go on */
25566 +       } else
25567 +               goto out;
25568 +
25569 +       err = -EINVAL;
25570 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25571 +               a->mvd_bdst = find_lower_writable(a);
25572 +               if (unlikely(a->mvd_bdst < 0)) {
25573 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25574 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25575 +                       goto out;
25576 +               }
25577 +       } else {
25578 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25579 +               if (unlikely(a->mvd_bdst < 0
25580 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25581 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25582 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25583 +                       goto out;
25584 +               }
25585 +       }
25586 +
25587 +       err = au_mvd_args_busy(dmsg, a);
25588 +       if (!err)
25589 +               err = au_mvd_args_parent(dmsg, a);
25590 +       if (!err)
25591 +               err = au_mvd_args_intermediate(dmsg, a);
25592 +       if (!err)
25593 +               err = au_mvd_args_exist(dmsg, a);
25594 +       if (!err)
25595 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25596 +
25597 +out:
25598 +       AuTraceErr(err);
25599 +       return err;
25600 +}
25601 +
25602 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25603 +{
25604 +       int err, e;
25605 +       unsigned char dmsg;
25606 +       struct au_mvd_args *args;
25607 +       struct inode *inode;
25608 +
25609 +       inode = d_inode(dentry);
25610 +       err = -EPERM;
25611 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25612 +               goto out;
25613 +
25614 +       err = -ENOMEM;
25615 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25616 +       if (unlikely(!args))
25617 +               goto out;
25618 +
25619 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25620 +       if (!err)
25621 +               /* VERIFY_WRITE */
25622 +               err = !access_ok(uarg, sizeof(*uarg));
25623 +       if (unlikely(err)) {
25624 +               err = -EFAULT;
25625 +               AuTraceErr(err);
25626 +               goto out_free;
25627 +       }
25628 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25629 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25630 +       args->mvdown.au_errno = 0;
25631 +       args->dentry = dentry;
25632 +       args->inode = inode;
25633 +       args->sb = dentry->d_sb;
25634 +
25635 +       err = -ENOENT;
25636 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25637 +       args->parent = dget_parent(dentry);
25638 +       args->dir = d_inode(args->parent);
25639 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25640 +       dput(args->parent);
25641 +       if (unlikely(args->parent != dentry->d_parent)) {
25642 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25643 +               goto out_dir;
25644 +       }
25645 +
25646 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25647 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25648 +       if (unlikely(err))
25649 +               goto out_inode;
25650 +
25651 +       di_write_lock_parent(args->parent);
25652 +       err = au_mvd_args(dmsg, args);
25653 +       if (unlikely(err))
25654 +               goto out_parent;
25655 +
25656 +       err = au_do_mvdown(dmsg, args);
25657 +       if (unlikely(err))
25658 +               goto out_parent;
25659 +
25660 +       au_cpup_attr_timesizes(args->dir);
25661 +       au_cpup_attr_timesizes(inode);
25662 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25663 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25664 +       /* au_digen_dec(dentry); */
25665 +
25666 +out_parent:
25667 +       di_write_unlock(args->parent);
25668 +       aufs_read_unlock(dentry, AuLock_DW);
25669 +out_inode:
25670 +       inode_unlock(inode);
25671 +out_dir:
25672 +       inode_unlock(args->dir);
25673 +out_free:
25674 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25675 +       if (unlikely(e))
25676 +               err = -EFAULT;
25677 +       au_kfree_rcu(args);
25678 +out:
25679 +       AuTraceErr(err);
25680 +       return err;
25681 +}
25682 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25683 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25684 +++ linux/fs/aufs/opts.c        2020-01-27 10:57:18.175538316 +0100
25685 @@ -0,0 +1,1880 @@
25686 +// SPDX-License-Identifier: GPL-2.0
25687 +/*
25688 + * Copyright (C) 2005-2020 Junjiro R. Okajima
25689 + *
25690 + * This program, aufs is free software; you can redistribute it and/or modify
25691 + * it under the terms of the GNU General Public License as published by
25692 + * the Free Software Foundation; either version 2 of the License, or
25693 + * (at your option) any later version.
25694 + *
25695 + * This program is distributed in the hope that it will be useful,
25696 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25697 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25698 + * GNU General Public License for more details.
25699 + *
25700 + * You should have received a copy of the GNU General Public License
25701 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25702 + */
25703 +
25704 +/*
25705 + * mount options/flags
25706 + */
25707 +
25708 +#include <linux/namei.h>
25709 +#include <linux/types.h> /* a distribution requires */
25710 +#include <linux/parser.h>
25711 +#include "aufs.h"
25712 +
25713 +/* ---------------------------------------------------------------------- */
25714 +
25715 +enum {
25716 +       Opt_br,
25717 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25718 +       Opt_idel, Opt_imod,
25719 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25720 +       Opt_rdblk_def, Opt_rdhash_def,
25721 +       Opt_xino, Opt_noxino,
25722 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25723 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25724 +       Opt_trunc_xib, Opt_notrunc_xib,
25725 +       Opt_shwh, Opt_noshwh,
25726 +       Opt_plink, Opt_noplink, Opt_list_plink,
25727 +       Opt_udba,
25728 +       Opt_dio, Opt_nodio,
25729 +       Opt_diropq_a, Opt_diropq_w,
25730 +       Opt_warn_perm, Opt_nowarn_perm,
25731 +       Opt_wbr_copyup, Opt_wbr_create,
25732 +       Opt_fhsm_sec,
25733 +       Opt_verbose, Opt_noverbose,
25734 +       Opt_sum, Opt_nosum, Opt_wsum,
25735 +       Opt_dirperm1, Opt_nodirperm1,
25736 +       Opt_dirren, Opt_nodirren,
25737 +       Opt_acl, Opt_noacl,
25738 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25739 +};
25740 +
25741 +static match_table_t options = {
25742 +       {Opt_br, "br=%s"},
25743 +       {Opt_br, "br:%s"},
25744 +
25745 +       {Opt_add, "add=%d:%s"},
25746 +       {Opt_add, "add:%d:%s"},
25747 +       {Opt_add, "ins=%d:%s"},
25748 +       {Opt_add, "ins:%d:%s"},
25749 +       {Opt_append, "append=%s"},
25750 +       {Opt_append, "append:%s"},
25751 +       {Opt_prepend, "prepend=%s"},
25752 +       {Opt_prepend, "prepend:%s"},
25753 +
25754 +       {Opt_del, "del=%s"},
25755 +       {Opt_del, "del:%s"},
25756 +       /* {Opt_idel, "idel:%d"}, */
25757 +       {Opt_mod, "mod=%s"},
25758 +       {Opt_mod, "mod:%s"},
25759 +       /* {Opt_imod, "imod:%d:%s"}, */
25760 +
25761 +       {Opt_dirwh, "dirwh=%d"},
25762 +
25763 +       {Opt_xino, "xino=%s"},
25764 +       {Opt_noxino, "noxino"},
25765 +       {Opt_trunc_xino, "trunc_xino"},
25766 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25767 +       {Opt_notrunc_xino, "notrunc_xino"},
25768 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25769 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25770 +       /* {Opt_zxino, "zxino=%s"}, */
25771 +       {Opt_trunc_xib, "trunc_xib"},
25772 +       {Opt_notrunc_xib, "notrunc_xib"},
25773 +
25774 +#ifdef CONFIG_PROC_FS
25775 +       {Opt_plink, "plink"},
25776 +#else
25777 +       {Opt_ignore_silent, "plink"},
25778 +#endif
25779 +
25780 +       {Opt_noplink, "noplink"},
25781 +
25782 +#ifdef CONFIG_AUFS_DEBUG
25783 +       {Opt_list_plink, "list_plink"},
25784 +#endif
25785 +
25786 +       {Opt_udba, "udba=%s"},
25787 +
25788 +       {Opt_dio, "dio"},
25789 +       {Opt_nodio, "nodio"},
25790 +
25791 +#ifdef CONFIG_AUFS_DIRREN
25792 +       {Opt_dirren, "dirren"},
25793 +       {Opt_nodirren, "nodirren"},
25794 +#else
25795 +       {Opt_ignore, "dirren"},
25796 +       {Opt_ignore_silent, "nodirren"},
25797 +#endif
25798 +
25799 +#ifdef CONFIG_AUFS_FHSM
25800 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25801 +#else
25802 +       {Opt_ignore, "fhsm_sec=%d"},
25803 +#endif
25804 +
25805 +       {Opt_diropq_a, "diropq=always"},
25806 +       {Opt_diropq_a, "diropq=a"},
25807 +       {Opt_diropq_w, "diropq=whiteouted"},
25808 +       {Opt_diropq_w, "diropq=w"},
25809 +
25810 +       {Opt_warn_perm, "warn_perm"},
25811 +       {Opt_nowarn_perm, "nowarn_perm"},
25812 +
25813 +       /* keep them temporary */
25814 +       {Opt_ignore_silent, "nodlgt"},
25815 +       {Opt_ignore, "clean_plink"},
25816 +
25817 +#ifdef CONFIG_AUFS_SHWH
25818 +       {Opt_shwh, "shwh"},
25819 +#endif
25820 +       {Opt_noshwh, "noshwh"},
25821 +
25822 +       {Opt_dirperm1, "dirperm1"},
25823 +       {Opt_nodirperm1, "nodirperm1"},
25824 +
25825 +       {Opt_verbose, "verbose"},
25826 +       {Opt_verbose, "v"},
25827 +       {Opt_noverbose, "noverbose"},
25828 +       {Opt_noverbose, "quiet"},
25829 +       {Opt_noverbose, "q"},
25830 +       {Opt_noverbose, "silent"},
25831 +
25832 +       {Opt_sum, "sum"},
25833 +       {Opt_nosum, "nosum"},
25834 +       {Opt_wsum, "wsum"},
25835 +
25836 +       {Opt_rdcache, "rdcache=%d"},
25837 +       {Opt_rdblk, "rdblk=%d"},
25838 +       {Opt_rdblk_def, "rdblk=def"},
25839 +       {Opt_rdhash, "rdhash=%d"},
25840 +       {Opt_rdhash_def, "rdhash=def"},
25841 +
25842 +       {Opt_wbr_create, "create=%s"},
25843 +       {Opt_wbr_create, "create_policy=%s"},
25844 +       {Opt_wbr_copyup, "cpup=%s"},
25845 +       {Opt_wbr_copyup, "copyup=%s"},
25846 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25847 +
25848 +       /* generic VFS flag */
25849 +#ifdef CONFIG_FS_POSIX_ACL
25850 +       {Opt_acl, "acl"},
25851 +       {Opt_noacl, "noacl"},
25852 +#else
25853 +       {Opt_ignore, "acl"},
25854 +       {Opt_ignore_silent, "noacl"},
25855 +#endif
25856 +
25857 +       /* internal use for the scripts */
25858 +       {Opt_ignore_silent, "si=%s"},
25859 +
25860 +       {Opt_br, "dirs=%s"},
25861 +       {Opt_ignore, "debug=%d"},
25862 +       {Opt_ignore, "delete=whiteout"},
25863 +       {Opt_ignore, "delete=all"},
25864 +       {Opt_ignore, "imap=%s"},
25865 +
25866 +       /* temporary workaround, due to old mount(8)? */
25867 +       {Opt_ignore_silent, "relatime"},
25868 +
25869 +       {Opt_err, NULL}
25870 +};
25871 +
25872 +/* ---------------------------------------------------------------------- */
25873 +
25874 +static const char *au_parser_pattern(int val, match_table_t tbl)
25875 +{
25876 +       struct match_token *p;
25877 +
25878 +       p = tbl;
25879 +       while (p->pattern) {
25880 +               if (p->token == val)
25881 +                       return p->pattern;
25882 +               p++;
25883 +       }
25884 +       BUG();
25885 +       return "??";
25886 +}
25887 +
25888 +static const char *au_optstr(int *val, match_table_t tbl)
25889 +{
25890 +       struct match_token *p;
25891 +       int v;
25892 +
25893 +       v = *val;
25894 +       if (!v)
25895 +               goto out;
25896 +       p = tbl;
25897 +       while (p->pattern) {
25898 +               if (p->token
25899 +                   && (v & p->token) == p->token) {
25900 +                       *val &= ~p->token;
25901 +                       return p->pattern;
25902 +               }
25903 +               p++;
25904 +       }
25905 +
25906 +out:
25907 +       return NULL;
25908 +}
25909 +
25910 +/* ---------------------------------------------------------------------- */
25911 +
25912 +static match_table_t brperm = {
25913 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25914 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25915 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25916 +       {0, NULL}
25917 +};
25918 +
25919 +static match_table_t brattr = {
25920 +       /* general */
25921 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25922 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25923 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25924 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25925 +#ifdef CONFIG_AUFS_FHSM
25926 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25927 +#endif
25928 +#ifdef CONFIG_AUFS_XATTR
25929 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25930 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25931 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25932 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25933 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25934 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25935 +#endif
25936 +
25937 +       /* ro/rr branch */
25938 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25939 +
25940 +       /* rw branch */
25941 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25942 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25943 +
25944 +       {0, NULL}
25945 +};
25946 +
25947 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25948 +{
25949 +       int attr, v;
25950 +       char *p;
25951 +
25952 +       attr = 0;
25953 +       do {
25954 +               p = strchr(str, '+');
25955 +               if (p)
25956 +                       *p = 0;
25957 +               v = match_token(str, table, args);
25958 +               if (v) {
25959 +                       if (v & AuBrAttr_CMOO_Mask)
25960 +                               attr &= ~AuBrAttr_CMOO_Mask;
25961 +                       attr |= v;
25962 +               } else {
25963 +                       if (p)
25964 +                               *p = '+';
25965 +                       pr_warn("ignored branch attribute %s\n", str);
25966 +                       break;
25967 +               }
25968 +               if (p)
25969 +                       str = p + 1;
25970 +       } while (p);
25971 +
25972 +       return attr;
25973 +}
25974 +
25975 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25976 +{
25977 +       int sz;
25978 +       const char *p;
25979 +       char *q;
25980 +
25981 +       q = str->a;
25982 +       *q = 0;
25983 +       p = au_optstr(&perm, brattr);
25984 +       if (p) {
25985 +               sz = strlen(p);
25986 +               memcpy(q, p, sz + 1);
25987 +               q += sz;
25988 +       } else
25989 +               goto out;
25990 +
25991 +       do {
25992 +               p = au_optstr(&perm, brattr);
25993 +               if (p) {
25994 +                       *q++ = '+';
25995 +                       sz = strlen(p);
25996 +                       memcpy(q, p, sz + 1);
25997 +                       q += sz;
25998 +               }
25999 +       } while (p);
26000 +
26001 +out:
26002 +       return q - str->a;
26003 +}
26004 +
26005 +static int noinline_for_stack br_perm_val(char *perm)
26006 +{
26007 +       int val, bad, sz;
26008 +       char *p;
26009 +       substring_t args[MAX_OPT_ARGS];
26010 +       au_br_perm_str_t attr;
26011 +
26012 +       p = strchr(perm, '+');
26013 +       if (p)
26014 +               *p = 0;
26015 +       val = match_token(perm, brperm, args);
26016 +       if (!val) {
26017 +               if (p)
26018 +                       *p = '+';
26019 +               pr_warn("ignored branch permission %s\n", perm);
26020 +               val = AuBrPerm_RO;
26021 +               goto out;
26022 +       }
26023 +       if (!p)
26024 +               goto out;
26025 +
26026 +       val |= br_attr_val(p + 1, brattr, args);
26027 +
26028 +       bad = 0;
26029 +       switch (val & AuBrPerm_Mask) {
26030 +       case AuBrPerm_RO:
26031 +       case AuBrPerm_RR:
26032 +               bad = val & AuBrWAttr_Mask;
26033 +               val &= ~AuBrWAttr_Mask;
26034 +               break;
26035 +       case AuBrPerm_RW:
26036 +               bad = val & AuBrRAttr_Mask;
26037 +               val &= ~AuBrRAttr_Mask;
26038 +               break;
26039 +       }
26040 +
26041 +       /*
26042 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26043 +        * does not treat it as an error, just warning.
26044 +        * this is a tiny guard for the user operation.
26045 +        */
26046 +       if (val & AuBrAttr_UNPIN) {
26047 +               bad |= AuBrAttr_UNPIN;
26048 +               val &= ~AuBrAttr_UNPIN;
26049 +       }
26050 +
26051 +       if (unlikely(bad)) {
26052 +               sz = au_do_optstr_br_attr(&attr, bad);
26053 +               AuDebugOn(!sz);
26054 +               pr_warn("ignored branch attribute %s\n", attr.a);
26055 +       }
26056 +
26057 +out:
26058 +       return val;
26059 +}
26060 +
26061 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26062 +{
26063 +       au_br_perm_str_t attr;
26064 +       const char *p;
26065 +       char *q;
26066 +       int sz;
26067 +
26068 +       q = str->a;
26069 +       p = au_optstr(&perm, brperm);
26070 +       AuDebugOn(!p || !*p);
26071 +       sz = strlen(p);
26072 +       memcpy(q, p, sz + 1);
26073 +       q += sz;
26074 +
26075 +       sz = au_do_optstr_br_attr(&attr, perm);
26076 +       if (sz) {
26077 +               *q++ = '+';
26078 +               memcpy(q, attr.a, sz + 1);
26079 +       }
26080 +
26081 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26082 +}
26083 +
26084 +/* ---------------------------------------------------------------------- */
26085 +
26086 +static match_table_t udbalevel = {
26087 +       {AuOpt_UDBA_REVAL, "reval"},
26088 +       {AuOpt_UDBA_NONE, "none"},
26089 +#ifdef CONFIG_AUFS_HNOTIFY
26090 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26091 +#ifdef CONFIG_AUFS_HFSNOTIFY
26092 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26093 +#endif
26094 +#endif
26095 +       {-1, NULL}
26096 +};
26097 +
26098 +static int noinline_for_stack udba_val(char *str)
26099 +{
26100 +       substring_t args[MAX_OPT_ARGS];
26101 +
26102 +       return match_token(str, udbalevel, args);
26103 +}
26104 +
26105 +const char *au_optstr_udba(int udba)
26106 +{
26107 +       return au_parser_pattern(udba, udbalevel);
26108 +}
26109 +
26110 +/* ---------------------------------------------------------------------- */
26111 +
26112 +static match_table_t au_wbr_create_policy = {
26113 +       {AuWbrCreate_TDP, "tdp"},
26114 +       {AuWbrCreate_TDP, "top-down-parent"},
26115 +       {AuWbrCreate_RR, "rr"},
26116 +       {AuWbrCreate_RR, "round-robin"},
26117 +       {AuWbrCreate_MFS, "mfs"},
26118 +       {AuWbrCreate_MFS, "most-free-space"},
26119 +       {AuWbrCreate_MFSV, "mfs:%d"},
26120 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26121 +
26122 +       /* top-down regardless the parent, and then mfs */
26123 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26124 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26125 +
26126 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26127 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26128 +       {AuWbrCreate_PMFS, "pmfs"},
26129 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26130 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26131 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26132 +
26133 +       {-1, NULL}
26134 +};
26135 +
26136 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26137 +                           struct au_opt_wbr_create *create)
26138 +{
26139 +       int err;
26140 +       unsigned long long ull;
26141 +
26142 +       err = 0;
26143 +       if (!match_u64(arg, &ull))
26144 +               create->mfsrr_watermark = ull;
26145 +       else {
26146 +               pr_err("bad integer in %s\n", str);
26147 +               err = -EINVAL;
26148 +       }
26149 +
26150 +       return err;
26151 +}
26152 +
26153 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26154 +                         struct au_opt_wbr_create *create)
26155 +{
26156 +       int n, err;
26157 +
26158 +       err = 0;
26159 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26160 +               create->mfs_second = n;
26161 +       else {
26162 +               pr_err("bad integer in %s\n", str);
26163 +               err = -EINVAL;
26164 +       }
26165 +
26166 +       return err;
26167 +}
26168 +
26169 +static int noinline_for_stack
26170 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26171 +{
26172 +       int err, e;
26173 +       substring_t args[MAX_OPT_ARGS];
26174 +
26175 +       err = match_token(str, au_wbr_create_policy, args);
26176 +       create->wbr_create = err;
26177 +       switch (err) {
26178 +       case AuWbrCreate_MFSRRV:
26179 +       case AuWbrCreate_TDMFSV:
26180 +       case AuWbrCreate_PMFSRRV:
26181 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26182 +               if (!e)
26183 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26184 +               if (unlikely(e))
26185 +                       err = e;
26186 +               break;
26187 +       case AuWbrCreate_MFSRR:
26188 +       case AuWbrCreate_TDMFS:
26189 +       case AuWbrCreate_PMFSRR:
26190 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26191 +               if (unlikely(e)) {
26192 +                       err = e;
26193 +                       break;
26194 +               }
26195 +               /*FALLTHROUGH*/
26196 +       case AuWbrCreate_MFS:
26197 +       case AuWbrCreate_PMFS:
26198 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26199 +               break;
26200 +       case AuWbrCreate_MFSV:
26201 +       case AuWbrCreate_PMFSV:
26202 +               e = au_wbr_mfs_sec(&args[0], str, create);
26203 +               if (unlikely(e))
26204 +                       err = e;
26205 +               break;
26206 +       }
26207 +
26208 +       return err;
26209 +}
26210 +
26211 +const char *au_optstr_wbr_create(int wbr_create)
26212 +{
26213 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26214 +}
26215 +
26216 +static match_table_t au_wbr_copyup_policy = {
26217 +       {AuWbrCopyup_TDP, "tdp"},
26218 +       {AuWbrCopyup_TDP, "top-down-parent"},
26219 +       {AuWbrCopyup_BUP, "bup"},
26220 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26221 +       {AuWbrCopyup_BU, "bu"},
26222 +       {AuWbrCopyup_BU, "bottom-up"},
26223 +       {-1, NULL}
26224 +};
26225 +
26226 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26227 +{
26228 +       substring_t args[MAX_OPT_ARGS];
26229 +
26230 +       return match_token(str, au_wbr_copyup_policy, args);
26231 +}
26232 +
26233 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26234 +{
26235 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26236 +}
26237 +
26238 +/* ---------------------------------------------------------------------- */
26239 +
26240 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26241 +
26242 +static void dump_opts(struct au_opts *opts)
26243 +{
26244 +#ifdef CONFIG_AUFS_DEBUG
26245 +       /* reduce stack space */
26246 +       union {
26247 +               struct au_opt_add *add;
26248 +               struct au_opt_del *del;
26249 +               struct au_opt_mod *mod;
26250 +               struct au_opt_xino *xino;
26251 +               struct au_opt_xino_itrunc *xino_itrunc;
26252 +               struct au_opt_wbr_create *create;
26253 +       } u;
26254 +       struct au_opt *opt;
26255 +
26256 +       opt = opts->opt;
26257 +       while (opt->type != Opt_tail) {
26258 +               switch (opt->type) {
26259 +               case Opt_add:
26260 +                       u.add = &opt->add;
26261 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26262 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26263 +                                 u.add->path.dentry);
26264 +                       break;
26265 +               case Opt_del:
26266 +               case Opt_idel:
26267 +                       u.del = &opt->del;
26268 +                       AuDbg("del {%s, %p}\n",
26269 +                             u.del->pathname, u.del->h_path.dentry);
26270 +                       break;
26271 +               case Opt_mod:
26272 +               case Opt_imod:
26273 +                       u.mod = &opt->mod;
26274 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26275 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26276 +                       break;
26277 +               case Opt_append:
26278 +                       u.add = &opt->add;
26279 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26280 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26281 +                                 u.add->path.dentry);
26282 +                       break;
26283 +               case Opt_prepend:
26284 +                       u.add = &opt->add;
26285 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26286 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26287 +                                 u.add->path.dentry);
26288 +                       break;
26289 +               case Opt_dirwh:
26290 +                       AuDbg("dirwh %d\n", opt->dirwh);
26291 +                       break;
26292 +               case Opt_rdcache:
26293 +                       AuDbg("rdcache %d\n", opt->rdcache);
26294 +                       break;
26295 +               case Opt_rdblk:
26296 +                       AuDbg("rdblk %u\n", opt->rdblk);
26297 +                       break;
26298 +               case Opt_rdblk_def:
26299 +                       AuDbg("rdblk_def\n");
26300 +                       break;
26301 +               case Opt_rdhash:
26302 +                       AuDbg("rdhash %u\n", opt->rdhash);
26303 +                       break;
26304 +               case Opt_rdhash_def:
26305 +                       AuDbg("rdhash_def\n");
26306 +                       break;
26307 +               case Opt_xino:
26308 +                       u.xino = &opt->xino;
26309 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26310 +                       break;
26311 +               case Opt_trunc_xino:
26312 +                       AuLabel(trunc_xino);
26313 +                       break;
26314 +               case Opt_notrunc_xino:
26315 +                       AuLabel(notrunc_xino);
26316 +                       break;
26317 +               case Opt_trunc_xino_path:
26318 +               case Opt_itrunc_xino:
26319 +                       u.xino_itrunc = &opt->xino_itrunc;
26320 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26321 +                       break;
26322 +               case Opt_noxino:
26323 +                       AuLabel(noxino);
26324 +                       break;
26325 +               case Opt_trunc_xib:
26326 +                       AuLabel(trunc_xib);
26327 +                       break;
26328 +               case Opt_notrunc_xib:
26329 +                       AuLabel(notrunc_xib);
26330 +                       break;
26331 +               case Opt_shwh:
26332 +                       AuLabel(shwh);
26333 +                       break;
26334 +               case Opt_noshwh:
26335 +                       AuLabel(noshwh);
26336 +                       break;
26337 +               case Opt_dirperm1:
26338 +                       AuLabel(dirperm1);
26339 +                       break;
26340 +               case Opt_nodirperm1:
26341 +                       AuLabel(nodirperm1);
26342 +                       break;
26343 +               case Opt_plink:
26344 +                       AuLabel(plink);
26345 +                       break;
26346 +               case Opt_noplink:
26347 +                       AuLabel(noplink);
26348 +                       break;
26349 +               case Opt_list_plink:
26350 +                       AuLabel(list_plink);
26351 +                       break;
26352 +               case Opt_udba:
26353 +                       AuDbg("udba %d, %s\n",
26354 +                                 opt->udba, au_optstr_udba(opt->udba));
26355 +                       break;
26356 +               case Opt_dio:
26357 +                       AuLabel(dio);
26358 +                       break;
26359 +               case Opt_nodio:
26360 +                       AuLabel(nodio);
26361 +                       break;
26362 +               case Opt_diropq_a:
26363 +                       AuLabel(diropq_a);
26364 +                       break;
26365 +               case Opt_diropq_w:
26366 +                       AuLabel(diropq_w);
26367 +                       break;
26368 +               case Opt_warn_perm:
26369 +                       AuLabel(warn_perm);
26370 +                       break;
26371 +               case Opt_nowarn_perm:
26372 +                       AuLabel(nowarn_perm);
26373 +                       break;
26374 +               case Opt_verbose:
26375 +                       AuLabel(verbose);
26376 +                       break;
26377 +               case Opt_noverbose:
26378 +                       AuLabel(noverbose);
26379 +                       break;
26380 +               case Opt_sum:
26381 +                       AuLabel(sum);
26382 +                       break;
26383 +               case Opt_nosum:
26384 +                       AuLabel(nosum);
26385 +                       break;
26386 +               case Opt_wsum:
26387 +                       AuLabel(wsum);
26388 +                       break;
26389 +               case Opt_wbr_create:
26390 +                       u.create = &opt->wbr_create;
26391 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26392 +                                 au_optstr_wbr_create(u.create->wbr_create));
26393 +                       switch (u.create->wbr_create) {
26394 +                       case AuWbrCreate_MFSV:
26395 +                       case AuWbrCreate_PMFSV:
26396 +                               AuDbg("%d sec\n", u.create->mfs_second);
26397 +                               break;
26398 +                       case AuWbrCreate_MFSRR:
26399 +                       case AuWbrCreate_TDMFS:
26400 +                               AuDbg("%llu watermark\n",
26401 +                                         u.create->mfsrr_watermark);
26402 +                               break;
26403 +                       case AuWbrCreate_MFSRRV:
26404 +                       case AuWbrCreate_TDMFSV:
26405 +                       case AuWbrCreate_PMFSRRV:
26406 +                               AuDbg("%llu watermark, %d sec\n",
26407 +                                         u.create->mfsrr_watermark,
26408 +                                         u.create->mfs_second);
26409 +                               break;
26410 +                       }
26411 +                       break;
26412 +               case Opt_wbr_copyup:
26413 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26414 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26415 +                       break;
26416 +               case Opt_fhsm_sec:
26417 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26418 +                       break;
26419 +               case Opt_dirren:
26420 +                       AuLabel(dirren);
26421 +                       break;
26422 +               case Opt_nodirren:
26423 +                       AuLabel(nodirren);
26424 +                       break;
26425 +               case Opt_acl:
26426 +                       AuLabel(acl);
26427 +                       break;
26428 +               case Opt_noacl:
26429 +                       AuLabel(noacl);
26430 +                       break;
26431 +               default:
26432 +                       BUG();
26433 +               }
26434 +               opt++;
26435 +       }
26436 +#endif
26437 +}
26438 +
26439 +void au_opts_free(struct au_opts *opts)
26440 +{
26441 +       struct au_opt *opt;
26442 +
26443 +       opt = opts->opt;
26444 +       while (opt->type != Opt_tail) {
26445 +               switch (opt->type) {
26446 +               case Opt_add:
26447 +               case Opt_append:
26448 +               case Opt_prepend:
26449 +                       path_put(&opt->add.path);
26450 +                       break;
26451 +               case Opt_del:
26452 +               case Opt_idel:
26453 +                       path_put(&opt->del.h_path);
26454 +                       break;
26455 +               case Opt_mod:
26456 +               case Opt_imod:
26457 +                       dput(opt->mod.h_root);
26458 +                       break;
26459 +               case Opt_xino:
26460 +                       fput(opt->xino.file);
26461 +                       break;
26462 +               }
26463 +               opt++;
26464 +       }
26465 +}
26466 +
26467 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26468 +                  aufs_bindex_t bindex)
26469 +{
26470 +       int err;
26471 +       struct au_opt_add *add = &opt->add;
26472 +       char *p;
26473 +
26474 +       add->bindex = bindex;
26475 +       add->perm = AuBrPerm_RO;
26476 +       add->pathname = opt_str;
26477 +       p = strchr(opt_str, '=');
26478 +       if (p) {
26479 +               *p++ = 0;
26480 +               if (*p)
26481 +                       add->perm = br_perm_val(p);
26482 +       }
26483 +
26484 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26485 +       if (!err) {
26486 +               if (!p) {
26487 +                       add->perm = AuBrPerm_RO;
26488 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26489 +                               add->perm = AuBrPerm_RR;
26490 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26491 +                               add->perm = AuBrPerm_RW;
26492 +               }
26493 +               opt->type = Opt_add;
26494 +               goto out;
26495 +       }
26496 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26497 +       err = -EINVAL;
26498 +
26499 +out:
26500 +       return err;
26501 +}
26502 +
26503 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26504 +{
26505 +       int err;
26506 +
26507 +       del->pathname = args[0].from;
26508 +       AuDbg("del path %s\n", del->pathname);
26509 +
26510 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26511 +       if (unlikely(err))
26512 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26513 +
26514 +       return err;
26515 +}
26516 +
26517 +#if 0 /* reserved for future use */
26518 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26519 +                             struct au_opt_del *del, substring_t args[])
26520 +{
26521 +       int err;
26522 +       struct dentry *root;
26523 +
26524 +       err = -EINVAL;
26525 +       root = sb->s_root;
26526 +       aufs_read_lock(root, AuLock_FLUSH);
26527 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26528 +               pr_err("out of bounds, %d\n", bindex);
26529 +               goto out;
26530 +       }
26531 +
26532 +       err = 0;
26533 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26534 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26535 +
26536 +out:
26537 +       aufs_read_unlock(root, !AuLock_IR);
26538 +       return err;
26539 +}
26540 +#endif
26541 +
26542 +static int noinline_for_stack
26543 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26544 +{
26545 +       int err;
26546 +       struct path path;
26547 +       char *p;
26548 +
26549 +       err = -EINVAL;
26550 +       mod->path = args[0].from;
26551 +       p = strchr(mod->path, '=');
26552 +       if (unlikely(!p)) {
26553 +               pr_err("no permission %s\n", args[0].from);
26554 +               goto out;
26555 +       }
26556 +
26557 +       *p++ = 0;
26558 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26559 +       if (unlikely(err)) {
26560 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26561 +               goto out;
26562 +       }
26563 +
26564 +       mod->perm = br_perm_val(p);
26565 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26566 +       mod->h_root = dget(path.dentry);
26567 +       path_put(&path);
26568 +
26569 +out:
26570 +       return err;
26571 +}
26572 +
26573 +#if 0 /* reserved for future use */
26574 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26575 +                             struct au_opt_mod *mod, substring_t args[])
26576 +{
26577 +       int err;
26578 +       struct dentry *root;
26579 +
26580 +       err = -EINVAL;
26581 +       root = sb->s_root;
26582 +       aufs_read_lock(root, AuLock_FLUSH);
26583 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26584 +               pr_err("out of bounds, %d\n", bindex);
26585 +               goto out;
26586 +       }
26587 +
26588 +       err = 0;
26589 +       mod->perm = br_perm_val(args[1].from);
26590 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26591 +             mod->path, mod->perm, args[1].from);
26592 +       mod->h_root = dget(au_h_dptr(root, bindex));
26593 +
26594 +out:
26595 +       aufs_read_unlock(root, !AuLock_IR);
26596 +       return err;
26597 +}
26598 +#endif
26599 +
26600 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26601 +                             substring_t args[])
26602 +{
26603 +       int err;
26604 +       struct file *file;
26605 +
26606 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26607 +       err = PTR_ERR(file);
26608 +       if (IS_ERR(file))
26609 +               goto out;
26610 +
26611 +       err = -EINVAL;
26612 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26613 +               fput(file);
26614 +               pr_err("%s must be outside\n", args[0].from);
26615 +               goto out;
26616 +       }
26617 +
26618 +       err = 0;
26619 +       xino->file = file;
26620 +       xino->path = args[0].from;
26621 +
26622 +out:
26623 +       return err;
26624 +}
26625 +
26626 +static int noinline_for_stack
26627 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26628 +                              struct au_opt_xino_itrunc *xino_itrunc,
26629 +                              substring_t args[])
26630 +{
26631 +       int err;
26632 +       aufs_bindex_t bbot, bindex;
26633 +       struct path path;
26634 +       struct dentry *root;
26635 +
26636 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26637 +       if (unlikely(err)) {
26638 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26639 +               goto out;
26640 +       }
26641 +
26642 +       xino_itrunc->bindex = -1;
26643 +       root = sb->s_root;
26644 +       aufs_read_lock(root, AuLock_FLUSH);
26645 +       bbot = au_sbbot(sb);
26646 +       for (bindex = 0; bindex <= bbot; bindex++) {
26647 +               if (au_h_dptr(root, bindex) == path.dentry) {
26648 +                       xino_itrunc->bindex = bindex;
26649 +                       break;
26650 +               }
26651 +       }
26652 +       aufs_read_unlock(root, !AuLock_IR);
26653 +       path_put(&path);
26654 +
26655 +       if (unlikely(xino_itrunc->bindex < 0)) {
26656 +               pr_err("no such branch %s\n", args[0].from);
26657 +               err = -EINVAL;
26658 +       }
26659 +
26660 +out:
26661 +       return err;
26662 +}
26663 +
26664 +/* called without aufs lock */
26665 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26666 +{
26667 +       int err, n, token;
26668 +       aufs_bindex_t bindex;
26669 +       unsigned char skipped;
26670 +       struct dentry *root;
26671 +       struct au_opt *opt, *opt_tail;
26672 +       char *opt_str;
26673 +       /* reduce the stack space */
26674 +       union {
26675 +               struct au_opt_xino_itrunc *xino_itrunc;
26676 +               struct au_opt_wbr_create *create;
26677 +       } u;
26678 +       struct {
26679 +               substring_t args[MAX_OPT_ARGS];
26680 +       } *a;
26681 +
26682 +       err = -ENOMEM;
26683 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26684 +       if (unlikely(!a))
26685 +               goto out;
26686 +
26687 +       root = sb->s_root;
26688 +       err = 0;
26689 +       bindex = 0;
26690 +       opt = opts->opt;
26691 +       opt_tail = opt + opts->max_opt - 1;
26692 +       opt->type = Opt_tail;
26693 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26694 +               err = -EINVAL;
26695 +               skipped = 0;
26696 +               token = match_token(opt_str, options, a->args);
26697 +               switch (token) {
26698 +               case Opt_br:
26699 +                       err = 0;
26700 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26701 +                              && *opt_str) {
26702 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26703 +                                             bindex++);
26704 +                               if (unlikely(!err && ++opt > opt_tail)) {
26705 +                                       err = -E2BIG;
26706 +                                       break;
26707 +                               }
26708 +                               opt->type = Opt_tail;
26709 +                               skipped = 1;
26710 +                       }
26711 +                       break;
26712 +               case Opt_add:
26713 +                       if (unlikely(match_int(&a->args[0], &n))) {
26714 +                               pr_err("bad integer in %s\n", opt_str);
26715 +                               break;
26716 +                       }
26717 +                       bindex = n;
26718 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26719 +                                     bindex);
26720 +                       if (!err)
26721 +                               opt->type = token;
26722 +                       break;
26723 +               case Opt_append:
26724 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26725 +                                     /*dummy bindex*/1);
26726 +                       if (!err)
26727 +                               opt->type = token;
26728 +                       break;
26729 +               case Opt_prepend:
26730 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26731 +                                     /*bindex*/0);
26732 +                       if (!err)
26733 +                               opt->type = token;
26734 +                       break;
26735 +               case Opt_del:
26736 +                       err = au_opts_parse_del(&opt->del, a->args);
26737 +                       if (!err)
26738 +                               opt->type = token;
26739 +                       break;
26740 +#if 0 /* reserved for future use */
26741 +               case Opt_idel:
26742 +                       del->pathname = "(indexed)";
26743 +                       if (unlikely(match_int(&args[0], &n))) {
26744 +                               pr_err("bad integer in %s\n", opt_str);
26745 +                               break;
26746 +                       }
26747 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26748 +                       if (!err)
26749 +                               opt->type = token;
26750 +                       break;
26751 +#endif
26752 +               case Opt_mod:
26753 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26754 +                       if (!err)
26755 +                               opt->type = token;
26756 +                       break;
26757 +#ifdef IMOD /* reserved for future use */
26758 +               case Opt_imod:
26759 +                       u.mod->path = "(indexed)";
26760 +                       if (unlikely(match_int(&a->args[0], &n))) {
26761 +                               pr_err("bad integer in %s\n", opt_str);
26762 +                               break;
26763 +                       }
26764 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26765 +                       if (!err)
26766 +                               opt->type = token;
26767 +                       break;
26768 +#endif
26769 +               case Opt_xino:
26770 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26771 +                       if (!err)
26772 +                               opt->type = token;
26773 +                       break;
26774 +
26775 +               case Opt_trunc_xino_path:
26776 +                       err = au_opts_parse_xino_itrunc_path
26777 +                               (sb, &opt->xino_itrunc, a->args);
26778 +                       if (!err)
26779 +                               opt->type = token;
26780 +                       break;
26781 +
26782 +               case Opt_itrunc_xino:
26783 +                       u.xino_itrunc = &opt->xino_itrunc;
26784 +                       if (unlikely(match_int(&a->args[0], &n))) {
26785 +                               pr_err("bad integer in %s\n", opt_str);
26786 +                               break;
26787 +                       }
26788 +                       u.xino_itrunc->bindex = n;
26789 +                       aufs_read_lock(root, AuLock_FLUSH);
26790 +                       if (n < 0 || au_sbbot(sb) < n) {
26791 +                               pr_err("out of bounds, %d\n", n);
26792 +                               aufs_read_unlock(root, !AuLock_IR);
26793 +                               break;
26794 +                       }
26795 +                       aufs_read_unlock(root, !AuLock_IR);
26796 +                       err = 0;
26797 +                       opt->type = token;
26798 +                       break;
26799 +
26800 +               case Opt_dirwh:
26801 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26802 +                               break;
26803 +                       err = 0;
26804 +                       opt->type = token;
26805 +                       break;
26806 +
26807 +               case Opt_rdcache:
26808 +                       if (unlikely(match_int(&a->args[0], &n))) {
26809 +                               pr_err("bad integer in %s\n", opt_str);
26810 +                               break;
26811 +                       }
26812 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26813 +                               pr_err("rdcache must be smaller than %d\n",
26814 +                                      AUFS_RDCACHE_MAX);
26815 +                               break;
26816 +                       }
26817 +                       opt->rdcache = n;
26818 +                       err = 0;
26819 +                       opt->type = token;
26820 +                       break;
26821 +               case Opt_rdblk:
26822 +                       if (unlikely(match_int(&a->args[0], &n)
26823 +                                    || n < 0
26824 +                                    || n > KMALLOC_MAX_SIZE)) {
26825 +                               pr_err("bad integer in %s\n", opt_str);
26826 +                               break;
26827 +                       }
26828 +                       if (unlikely(n && n < NAME_MAX)) {
26829 +                               pr_err("rdblk must be larger than %d\n",
26830 +                                      NAME_MAX);
26831 +                               break;
26832 +                       }
26833 +                       opt->rdblk = n;
26834 +                       err = 0;
26835 +                       opt->type = token;
26836 +                       break;
26837 +               case Opt_rdhash:
26838 +                       if (unlikely(match_int(&a->args[0], &n)
26839 +                                    || n < 0
26840 +                                    || n * sizeof(struct hlist_head)
26841 +                                    > KMALLOC_MAX_SIZE)) {
26842 +                               pr_err("bad integer in %s\n", opt_str);
26843 +                               break;
26844 +                       }
26845 +                       opt->rdhash = n;
26846 +                       err = 0;
26847 +                       opt->type = token;
26848 +                       break;
26849 +
26850 +               case Opt_trunc_xino:
26851 +               case Opt_notrunc_xino:
26852 +               case Opt_noxino:
26853 +               case Opt_trunc_xib:
26854 +               case Opt_notrunc_xib:
26855 +               case Opt_shwh:
26856 +               case Opt_noshwh:
26857 +               case Opt_dirperm1:
26858 +               case Opt_nodirperm1:
26859 +               case Opt_plink:
26860 +               case Opt_noplink:
26861 +               case Opt_list_plink:
26862 +               case Opt_dio:
26863 +               case Opt_nodio:
26864 +               case Opt_diropq_a:
26865 +               case Opt_diropq_w:
26866 +               case Opt_warn_perm:
26867 +               case Opt_nowarn_perm:
26868 +               case Opt_verbose:
26869 +               case Opt_noverbose:
26870 +               case Opt_sum:
26871 +               case Opt_nosum:
26872 +               case Opt_wsum:
26873 +               case Opt_rdblk_def:
26874 +               case Opt_rdhash_def:
26875 +               case Opt_dirren:
26876 +               case Opt_nodirren:
26877 +               case Opt_acl:
26878 +               case Opt_noacl:
26879 +                       err = 0;
26880 +                       opt->type = token;
26881 +                       break;
26882 +
26883 +               case Opt_udba:
26884 +                       opt->udba = udba_val(a->args[0].from);
26885 +                       if (opt->udba >= 0) {
26886 +                               err = 0;
26887 +                               opt->type = token;
26888 +                       } else
26889 +                               pr_err("wrong value, %s\n", opt_str);
26890 +                       break;
26891 +
26892 +               case Opt_wbr_create:
26893 +                       u.create = &opt->wbr_create;
26894 +                       u.create->wbr_create
26895 +                               = au_wbr_create_val(a->args[0].from, u.create);
26896 +                       if (u.create->wbr_create >= 0) {
26897 +                               err = 0;
26898 +                               opt->type = token;
26899 +                       } else
26900 +                               pr_err("wrong value, %s\n", opt_str);
26901 +                       break;
26902 +               case Opt_wbr_copyup:
26903 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26904 +                       if (opt->wbr_copyup >= 0) {
26905 +                               err = 0;
26906 +                               opt->type = token;
26907 +                       } else
26908 +                               pr_err("wrong value, %s\n", opt_str);
26909 +                       break;
26910 +
26911 +               case Opt_fhsm_sec:
26912 +                       if (unlikely(match_int(&a->args[0], &n)
26913 +                                    || n < 0)) {
26914 +                               pr_err("bad integer in %s\n", opt_str);
26915 +                               break;
26916 +                       }
26917 +                       if (sysaufs_brs) {
26918 +                               opt->fhsm_second = n;
26919 +                               opt->type = token;
26920 +                       } else
26921 +                               pr_warn("ignored %s\n", opt_str);
26922 +                       err = 0;
26923 +                       break;
26924 +
26925 +               case Opt_ignore:
26926 +                       pr_warn("ignored %s\n", opt_str);
26927 +                       /*FALLTHROUGH*/
26928 +               case Opt_ignore_silent:
26929 +                       skipped = 1;
26930 +                       err = 0;
26931 +                       break;
26932 +               case Opt_err:
26933 +                       pr_err("unknown option %s\n", opt_str);
26934 +                       break;
26935 +               }
26936 +
26937 +               if (!err && !skipped) {
26938 +                       if (unlikely(++opt > opt_tail)) {
26939 +                               err = -E2BIG;
26940 +                               opt--;
26941 +                               opt->type = Opt_tail;
26942 +                               break;
26943 +                       }
26944 +                       opt->type = Opt_tail;
26945 +               }
26946 +       }
26947 +
26948 +       au_kfree_rcu(a);
26949 +       dump_opts(opts);
26950 +       if (unlikely(err))
26951 +               au_opts_free(opts);
26952 +
26953 +out:
26954 +       return err;
26955 +}
26956 +
26957 +static int au_opt_wbr_create(struct super_block *sb,
26958 +                            struct au_opt_wbr_create *create)
26959 +{
26960 +       int err;
26961 +       struct au_sbinfo *sbinfo;
26962 +
26963 +       SiMustWriteLock(sb);
26964 +
26965 +       err = 1; /* handled */
26966 +       sbinfo = au_sbi(sb);
26967 +       if (sbinfo->si_wbr_create_ops->fin) {
26968 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26969 +               if (!err)
26970 +                       err = 1;
26971 +       }
26972 +
26973 +       sbinfo->si_wbr_create = create->wbr_create;
26974 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26975 +       switch (create->wbr_create) {
26976 +       case AuWbrCreate_MFSRRV:
26977 +       case AuWbrCreate_MFSRR:
26978 +       case AuWbrCreate_TDMFS:
26979 +       case AuWbrCreate_TDMFSV:
26980 +       case AuWbrCreate_PMFSRR:
26981 +       case AuWbrCreate_PMFSRRV:
26982 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26983 +               /*FALLTHROUGH*/
26984 +       case AuWbrCreate_MFS:
26985 +       case AuWbrCreate_MFSV:
26986 +       case AuWbrCreate_PMFS:
26987 +       case AuWbrCreate_PMFSV:
26988 +               sbinfo->si_wbr_mfs.mfs_expire
26989 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26990 +               break;
26991 +       }
26992 +
26993 +       if (sbinfo->si_wbr_create_ops->init)
26994 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26995 +
26996 +       return err;
26997 +}
26998 +
26999 +/*
27000 + * returns,
27001 + * plus: processed without an error
27002 + * zero: unprocessed
27003 + */
27004 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27005 +                        struct au_opts *opts)
27006 +{
27007 +       int err;
27008 +       struct au_sbinfo *sbinfo;
27009 +
27010 +       SiMustWriteLock(sb);
27011 +
27012 +       err = 1; /* handled */
27013 +       sbinfo = au_sbi(sb);
27014 +       switch (opt->type) {
27015 +       case Opt_udba:
27016 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27017 +               sbinfo->si_mntflags |= opt->udba;
27018 +               opts->given_udba |= opt->udba;
27019 +               break;
27020 +
27021 +       case Opt_plink:
27022 +               au_opt_set(sbinfo->si_mntflags, PLINK);
27023 +               break;
27024 +       case Opt_noplink:
27025 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27026 +                       au_plink_put(sb, /*verbose*/1);
27027 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
27028 +               break;
27029 +       case Opt_list_plink:
27030 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27031 +                       au_plink_list(sb);
27032 +               break;
27033 +
27034 +       case Opt_dio:
27035 +               au_opt_set(sbinfo->si_mntflags, DIO);
27036 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27037 +               break;
27038 +       case Opt_nodio:
27039 +               au_opt_clr(sbinfo->si_mntflags, DIO);
27040 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27041 +               break;
27042 +
27043 +       case Opt_fhsm_sec:
27044 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27045 +               break;
27046 +
27047 +       case Opt_diropq_a:
27048 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27049 +               break;
27050 +       case Opt_diropq_w:
27051 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27052 +               break;
27053 +
27054 +       case Opt_warn_perm:
27055 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27056 +               break;
27057 +       case Opt_nowarn_perm:
27058 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27059 +               break;
27060 +
27061 +       case Opt_verbose:
27062 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27063 +               break;
27064 +       case Opt_noverbose:
27065 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27066 +               break;
27067 +
27068 +       case Opt_sum:
27069 +               au_opt_set(sbinfo->si_mntflags, SUM);
27070 +               break;
27071 +       case Opt_wsum:
27072 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27073 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27074 +               break;
27075 +       case Opt_nosum:
27076 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27077 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27078 +               break;
27079 +
27080 +       case Opt_wbr_create:
27081 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27082 +               break;
27083 +       case Opt_wbr_copyup:
27084 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27085 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27086 +               break;
27087 +
27088 +       case Opt_dirwh:
27089 +               sbinfo->si_dirwh = opt->dirwh;
27090 +               break;
27091 +
27092 +       case Opt_rdcache:
27093 +               sbinfo->si_rdcache
27094 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27095 +               break;
27096 +       case Opt_rdblk:
27097 +               sbinfo->si_rdblk = opt->rdblk;
27098 +               break;
27099 +       case Opt_rdblk_def:
27100 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27101 +               break;
27102 +       case Opt_rdhash:
27103 +               sbinfo->si_rdhash = opt->rdhash;
27104 +               break;
27105 +       case Opt_rdhash_def:
27106 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27107 +               break;
27108 +
27109 +       case Opt_shwh:
27110 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27111 +               break;
27112 +       case Opt_noshwh:
27113 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27114 +               break;
27115 +
27116 +       case Opt_dirperm1:
27117 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27118 +               break;
27119 +       case Opt_nodirperm1:
27120 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27121 +               break;
27122 +
27123 +       case Opt_trunc_xino:
27124 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27125 +               break;
27126 +       case Opt_notrunc_xino:
27127 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27128 +               break;
27129 +
27130 +       case Opt_trunc_xino_path:
27131 +       case Opt_itrunc_xino:
27132 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27133 +                                   /*idx_begin*/0);
27134 +               if (!err)
27135 +                       err = 1;
27136 +               break;
27137 +
27138 +       case Opt_trunc_xib:
27139 +               au_fset_opts(opts->flags, TRUNC_XIB);
27140 +               break;
27141 +       case Opt_notrunc_xib:
27142 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27143 +               break;
27144 +
27145 +       case Opt_dirren:
27146 +               err = 1;
27147 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27148 +                       err = au_dr_opt_set(sb);
27149 +                       if (!err)
27150 +                               err = 1;
27151 +               }
27152 +               if (err == 1)
27153 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27154 +               break;
27155 +       case Opt_nodirren:
27156 +               err = 1;
27157 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27158 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27159 +                                                             DR_FLUSHED));
27160 +                       if (!err)
27161 +                               err = 1;
27162 +               }
27163 +               if (err == 1)
27164 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27165 +               break;
27166 +
27167 +       case Opt_acl:
27168 +               sb->s_flags |= SB_POSIXACL;
27169 +               break;
27170 +       case Opt_noacl:
27171 +               sb->s_flags &= ~SB_POSIXACL;
27172 +               break;
27173 +
27174 +       default:
27175 +               err = 0;
27176 +               break;
27177 +       }
27178 +
27179 +       return err;
27180 +}
27181 +
27182 +/*
27183 + * returns tri-state.
27184 + * plus: processed without an error
27185 + * zero: unprocessed
27186 + * minus: error
27187 + */
27188 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27189 +                    struct au_opts *opts)
27190 +{
27191 +       int err, do_refresh;
27192 +
27193 +       err = 0;
27194 +       switch (opt->type) {
27195 +       case Opt_append:
27196 +               opt->add.bindex = au_sbbot(sb) + 1;
27197 +               if (opt->add.bindex < 0)
27198 +                       opt->add.bindex = 0;
27199 +               goto add;
27200 +               /* Always goto add, not fallthrough */
27201 +       case Opt_prepend:
27202 +               opt->add.bindex = 0;
27203 +               /* fallthrough */
27204 +       add: /* indented label */
27205 +       case Opt_add:
27206 +               err = au_br_add(sb, &opt->add,
27207 +                               au_ftest_opts(opts->flags, REMOUNT));
27208 +               if (!err) {
27209 +                       err = 1;
27210 +                       au_fset_opts(opts->flags, REFRESH);
27211 +               }
27212 +               break;
27213 +
27214 +       case Opt_del:
27215 +       case Opt_idel:
27216 +               err = au_br_del(sb, &opt->del,
27217 +                               au_ftest_opts(opts->flags, REMOUNT));
27218 +               if (!err) {
27219 +                       err = 1;
27220 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27221 +                       au_fset_opts(opts->flags, REFRESH);
27222 +               }
27223 +               break;
27224 +
27225 +       case Opt_mod:
27226 +       case Opt_imod:
27227 +               err = au_br_mod(sb, &opt->mod,
27228 +                               au_ftest_opts(opts->flags, REMOUNT),
27229 +                               &do_refresh);
27230 +               if (!err) {
27231 +                       err = 1;
27232 +                       if (do_refresh)
27233 +                               au_fset_opts(opts->flags, REFRESH);
27234 +               }
27235 +               break;
27236 +       }
27237 +       return err;
27238 +}
27239 +
27240 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27241 +                      struct au_opt_xino **opt_xino,
27242 +                      struct au_opts *opts)
27243 +{
27244 +       int err;
27245 +
27246 +       err = 0;
27247 +       switch (opt->type) {
27248 +       case Opt_xino:
27249 +               err = au_xino_set(sb, &opt->xino,
27250 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27251 +               if (unlikely(err))
27252 +                       break;
27253 +
27254 +               *opt_xino = &opt->xino;
27255 +               break;
27256 +
27257 +       case Opt_noxino:
27258 +               au_xino_clr(sb);
27259 +               *opt_xino = (void *)-1;
27260 +               break;
27261 +       }
27262 +
27263 +       return err;
27264 +}
27265 +
27266 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27267 +                  unsigned int pending)
27268 +{
27269 +       int err, fhsm;
27270 +       aufs_bindex_t bindex, bbot;
27271 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27272 +       struct au_branch *br;
27273 +       struct au_wbr *wbr;
27274 +       struct dentry *root, *dentry;
27275 +       struct inode *dir, *h_dir;
27276 +       struct au_sbinfo *sbinfo;
27277 +       struct au_hinode *hdir;
27278 +
27279 +       SiMustAnyLock(sb);
27280 +
27281 +       sbinfo = au_sbi(sb);
27282 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27283 +
27284 +       if (!(sb_flags & SB_RDONLY)) {
27285 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27286 +                       pr_warn("first branch should be rw\n");
27287 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27288 +                       pr_warn_once("shwh should be used with ro\n");
27289 +       }
27290 +
27291 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27292 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27293 +               pr_warn_once("udba=*notify requires xino\n");
27294 +
27295 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27296 +               pr_warn_once("dirperm1 breaks the protection"
27297 +                            " by the permission bits on the lower branch\n");
27298 +
27299 +       err = 0;
27300 +       fhsm = 0;
27301 +       root = sb->s_root;
27302 +       dir = d_inode(root);
27303 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27304 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27305 +                                     UDBA_NONE);
27306 +       bbot = au_sbbot(sb);
27307 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27308 +               skip = 0;
27309 +               h_dir = au_h_iptr(dir, bindex);
27310 +               br = au_sbr(sb, bindex);
27311 +
27312 +               if ((br->br_perm & AuBrAttr_ICEX)
27313 +                   && !h_dir->i_op->listxattr)
27314 +                       br->br_perm &= ~AuBrAttr_ICEX;
27315 +#if 0 /* untested */
27316 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27317 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27318 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27319 +#endif
27320 +
27321 +               do_free = 0;
27322 +               wbr = br->br_wbr;
27323 +               if (wbr)
27324 +                       wbr_wh_read_lock(wbr);
27325 +
27326 +               if (!au_br_writable(br->br_perm)) {
27327 +                       do_free = !!wbr;
27328 +                       skip = (!wbr
27329 +                               || (!wbr->wbr_whbase
27330 +                                   && !wbr->wbr_plink
27331 +                                   && !wbr->wbr_orph));
27332 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27333 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27334 +                       skip = (!wbr || !wbr->wbr_whbase);
27335 +                       if (skip && wbr) {
27336 +                               if (do_plink)
27337 +                                       skip = !!wbr->wbr_plink;
27338 +                               else
27339 +                                       skip = !wbr->wbr_plink;
27340 +                       }
27341 +               } else {
27342 +                       /* skip = (br->br_whbase && br->br_ohph); */
27343 +                       skip = (wbr && wbr->wbr_whbase);
27344 +                       if (skip) {
27345 +                               if (do_plink)
27346 +                                       skip = !!wbr->wbr_plink;
27347 +                               else
27348 +                                       skip = !wbr->wbr_plink;
27349 +                       }
27350 +               }
27351 +               if (wbr)
27352 +                       wbr_wh_read_unlock(wbr);
27353 +
27354 +               if (can_no_dreval) {
27355 +                       dentry = br->br_path.dentry;
27356 +                       spin_lock(&dentry->d_lock);
27357 +                       if (dentry->d_flags &
27358 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27359 +                               can_no_dreval = 0;
27360 +                       spin_unlock(&dentry->d_lock);
27361 +               }
27362 +
27363 +               if (au_br_fhsm(br->br_perm)) {
27364 +                       fhsm++;
27365 +                       AuDebugOn(!br->br_fhsm);
27366 +               }
27367 +
27368 +               if (skip)
27369 +                       continue;
27370 +
27371 +               hdir = au_hi(dir, bindex);
27372 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27373 +               if (wbr)
27374 +                       wbr_wh_write_lock(wbr);
27375 +               err = au_wh_init(br, sb);
27376 +               if (wbr)
27377 +                       wbr_wh_write_unlock(wbr);
27378 +               au_hn_inode_unlock(hdir);
27379 +
27380 +               if (!err && do_free) {
27381 +                       au_kfree_rcu(wbr);
27382 +                       br->br_wbr = NULL;
27383 +               }
27384 +       }
27385 +
27386 +       if (can_no_dreval)
27387 +               au_fset_si(sbinfo, NO_DREVAL);
27388 +       else
27389 +               au_fclr_si(sbinfo, NO_DREVAL);
27390 +
27391 +       if (fhsm >= 2) {
27392 +               au_fset_si(sbinfo, FHSM);
27393 +               for (bindex = bbot; bindex >= 0; bindex--) {
27394 +                       br = au_sbr(sb, bindex);
27395 +                       if (au_br_fhsm(br->br_perm)) {
27396 +                               au_fhsm_set_bottom(sb, bindex);
27397 +                               break;
27398 +                       }
27399 +               }
27400 +       } else {
27401 +               au_fclr_si(sbinfo, FHSM);
27402 +               au_fhsm_set_bottom(sb, -1);
27403 +       }
27404 +
27405 +       return err;
27406 +}
27407 +
27408 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27409 +{
27410 +       int err;
27411 +       unsigned int tmp;
27412 +       aufs_bindex_t bindex, bbot;
27413 +       struct au_opt *opt;
27414 +       struct au_opt_xino *opt_xino, xino;
27415 +       struct au_sbinfo *sbinfo;
27416 +       struct au_branch *br;
27417 +       struct inode *dir;
27418 +
27419 +       SiMustWriteLock(sb);
27420 +
27421 +       err = 0;
27422 +       opt_xino = NULL;
27423 +       opt = opts->opt;
27424 +       while (err >= 0 && opt->type != Opt_tail)
27425 +               err = au_opt_simple(sb, opt++, opts);
27426 +       if (err > 0)
27427 +               err = 0;
27428 +       else if (unlikely(err < 0))
27429 +               goto out;
27430 +
27431 +       /* disable xino and udba temporary */
27432 +       sbinfo = au_sbi(sb);
27433 +       tmp = sbinfo->si_mntflags;
27434 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27435 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27436 +
27437 +       opt = opts->opt;
27438 +       while (err >= 0 && opt->type != Opt_tail)
27439 +               err = au_opt_br(sb, opt++, opts);
27440 +       if (err > 0)
27441 +               err = 0;
27442 +       else if (unlikely(err < 0))
27443 +               goto out;
27444 +
27445 +       bbot = au_sbbot(sb);
27446 +       if (unlikely(bbot < 0)) {
27447 +               err = -EINVAL;
27448 +               pr_err("no branches\n");
27449 +               goto out;
27450 +       }
27451 +
27452 +       if (au_opt_test(tmp, XINO))
27453 +               au_opt_set(sbinfo->si_mntflags, XINO);
27454 +       opt = opts->opt;
27455 +       while (!err && opt->type != Opt_tail)
27456 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27457 +       if (unlikely(err))
27458 +               goto out;
27459 +
27460 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27461 +       if (unlikely(err))
27462 +               goto out;
27463 +
27464 +       /* restore xino */
27465 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27466 +               xino.file = au_xino_def(sb);
27467 +               err = PTR_ERR(xino.file);
27468 +               if (IS_ERR(xino.file))
27469 +                       goto out;
27470 +
27471 +               err = au_xino_set(sb, &xino, /*remount*/0);
27472 +               fput(xino.file);
27473 +               if (unlikely(err))
27474 +                       goto out;
27475 +       }
27476 +
27477 +       /* restore udba */
27478 +       tmp &= AuOptMask_UDBA;
27479 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27480 +       sbinfo->si_mntflags |= tmp;
27481 +       bbot = au_sbbot(sb);
27482 +       for (bindex = 0; bindex <= bbot; bindex++) {
27483 +               br = au_sbr(sb, bindex);
27484 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27485 +               if (unlikely(err))
27486 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27487 +                               bindex, err);
27488 +               /* go on even if err */
27489 +       }
27490 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27491 +               dir = d_inode(sb->s_root);
27492 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27493 +       }
27494 +
27495 +out:
27496 +       return err;
27497 +}
27498 +
27499 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27500 +{
27501 +       int err, rerr;
27502 +       unsigned char no_dreval;
27503 +       struct inode *dir;
27504 +       struct au_opt_xino *opt_xino;
27505 +       struct au_opt *opt;
27506 +       struct au_sbinfo *sbinfo;
27507 +
27508 +       SiMustWriteLock(sb);
27509 +
27510 +       err = au_dr_opt_flush(sb);
27511 +       if (unlikely(err))
27512 +               goto out;
27513 +       au_fset_opts(opts->flags, DR_FLUSHED);
27514 +
27515 +       dir = d_inode(sb->s_root);
27516 +       sbinfo = au_sbi(sb);
27517 +       opt_xino = NULL;
27518 +       opt = opts->opt;
27519 +       while (err >= 0 && opt->type != Opt_tail) {
27520 +               err = au_opt_simple(sb, opt, opts);
27521 +               if (!err)
27522 +                       err = au_opt_br(sb, opt, opts);
27523 +               if (!err)
27524 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27525 +               opt++;
27526 +       }
27527 +       if (err > 0)
27528 +               err = 0;
27529 +       AuTraceErr(err);
27530 +       /* go on even err */
27531 +
27532 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27533 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27534 +       if (unlikely(rerr && !err))
27535 +               err = rerr;
27536 +
27537 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27538 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27539 +
27540 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27541 +               rerr = au_xib_trunc(sb);
27542 +               if (unlikely(rerr && !err))
27543 +                       err = rerr;
27544 +       }
27545 +
27546 +       /* will be handled by the caller */
27547 +       if (!au_ftest_opts(opts->flags, REFRESH)
27548 +           && (opts->given_udba
27549 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27550 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27551 +                   ))
27552 +               au_fset_opts(opts->flags, REFRESH);
27553 +
27554 +       AuDbg("status 0x%x\n", opts->flags);
27555 +
27556 +out:
27557 +       return err;
27558 +}
27559 +
27560 +/* ---------------------------------------------------------------------- */
27561 +
27562 +unsigned int au_opt_udba(struct super_block *sb)
27563 +{
27564 +       return au_mntflags(sb) & AuOptMask_UDBA;
27565 +}
27566 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27567 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27568 +++ linux/fs/aufs/opts.h        2020-01-27 10:57:18.175538316 +0100
27569 @@ -0,0 +1,225 @@
27570 +/* SPDX-License-Identifier: GPL-2.0 */
27571 +/*
27572 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27573 + *
27574 + * This program, aufs is free software; you can redistribute it and/or modify
27575 + * it under the terms of the GNU General Public License as published by
27576 + * the Free Software Foundation; either version 2 of the License, or
27577 + * (at your option) any later version.
27578 + *
27579 + * This program is distributed in the hope that it will be useful,
27580 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27581 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27582 + * GNU General Public License for more details.
27583 + *
27584 + * You should have received a copy of the GNU General Public License
27585 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27586 + */
27587 +
27588 +/*
27589 + * mount options/flags
27590 + */
27591 +
27592 +#ifndef __AUFS_OPTS_H__
27593 +#define __AUFS_OPTS_H__
27594 +
27595 +#ifdef __KERNEL__
27596 +
27597 +#include <linux/path.h>
27598 +
27599 +struct file;
27600 +
27601 +/* ---------------------------------------------------------------------- */
27602 +
27603 +/* mount flags */
27604 +#define AuOpt_XINO             1               /* external inode number bitmap
27605 +                                                  and translation table */
27606 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27607 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27608 +#define AuOpt_UDBA_REVAL       (1 << 3)
27609 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27610 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27611 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27612 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27613 +                                                  bits */
27614 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27615 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27616 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27617 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27618 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27619 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27620 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27621 +
27622 +#ifndef CONFIG_AUFS_HNOTIFY
27623 +#undef AuOpt_UDBA_HNOTIFY
27624 +#define AuOpt_UDBA_HNOTIFY     0
27625 +#endif
27626 +#ifndef CONFIG_AUFS_DIRREN
27627 +#undef AuOpt_DIRREN
27628 +#define AuOpt_DIRREN           0
27629 +#endif
27630 +#ifndef CONFIG_AUFS_SHWH
27631 +#undef AuOpt_SHWH
27632 +#define AuOpt_SHWH             0
27633 +#endif
27634 +
27635 +#define AuOpt_Def      (AuOpt_XINO \
27636 +                        | AuOpt_UDBA_REVAL \
27637 +                        | AuOpt_PLINK \
27638 +                        /* | AuOpt_DIRPERM1 */ \
27639 +                        | AuOpt_WARN_PERM)
27640 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27641 +                        | AuOpt_UDBA_REVAL \
27642 +                        | AuOpt_UDBA_HNOTIFY)
27643 +
27644 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27645 +#define au_opt_set(flags, name) do { \
27646 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27647 +       ((flags) |= AuOpt_##name); \
27648 +} while (0)
27649 +#define au_opt_set_udba(flags, name) do { \
27650 +       (flags) &= ~AuOptMask_UDBA; \
27651 +       ((flags) |= AuOpt_##name); \
27652 +} while (0)
27653 +#define au_opt_clr(flags, name) do { \
27654 +       ((flags) &= ~AuOpt_##name); \
27655 +} while (0)
27656 +
27657 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27658 +{
27659 +#ifdef CONFIG_PROC_FS
27660 +       return mntflags;
27661 +#else
27662 +       return mntflags & ~AuOpt_PLINK;
27663 +#endif
27664 +}
27665 +
27666 +/* ---------------------------------------------------------------------- */
27667 +
27668 +/* policies to select one among multiple writable branches */
27669 +enum {
27670 +       AuWbrCreate_TDP,        /* top down parent */
27671 +       AuWbrCreate_RR,         /* round robin */
27672 +       AuWbrCreate_MFS,        /* most free space */
27673 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27674 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27675 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27676 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27677 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27678 +       AuWbrCreate_PMFS,       /* parent and mfs */
27679 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27680 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27681 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27682 +
27683 +       AuWbrCreate_Def = AuWbrCreate_TDP
27684 +};
27685 +
27686 +enum {
27687 +       AuWbrCopyup_TDP,        /* top down parent */
27688 +       AuWbrCopyup_BUP,        /* bottom up parent */
27689 +       AuWbrCopyup_BU,         /* bottom up */
27690 +
27691 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27692 +};
27693 +
27694 +/* ---------------------------------------------------------------------- */
27695 +
27696 +struct au_opt_add {
27697 +       aufs_bindex_t   bindex;
27698 +       char            *pathname;
27699 +       int             perm;
27700 +       struct path     path;
27701 +};
27702 +
27703 +struct au_opt_del {
27704 +       char            *pathname;
27705 +       struct path     h_path;
27706 +};
27707 +
27708 +struct au_opt_mod {
27709 +       char            *path;
27710 +       int             perm;
27711 +       struct dentry   *h_root;
27712 +};
27713 +
27714 +struct au_opt_xino {
27715 +       char            *path;
27716 +       struct file     *file;
27717 +};
27718 +
27719 +struct au_opt_xino_itrunc {
27720 +       aufs_bindex_t   bindex;
27721 +};
27722 +
27723 +struct au_opt_wbr_create {
27724 +       int                     wbr_create;
27725 +       int                     mfs_second;
27726 +       unsigned long long      mfsrr_watermark;
27727 +};
27728 +
27729 +struct au_opt {
27730 +       int type;
27731 +       union {
27732 +               struct au_opt_xino      xino;
27733 +               struct au_opt_xino_itrunc xino_itrunc;
27734 +               struct au_opt_add       add;
27735 +               struct au_opt_del       del;
27736 +               struct au_opt_mod       mod;
27737 +               int                     dirwh;
27738 +               int                     rdcache;
27739 +               unsigned int            rdblk;
27740 +               unsigned int            rdhash;
27741 +               int                     udba;
27742 +               struct au_opt_wbr_create wbr_create;
27743 +               int                     wbr_copyup;
27744 +               unsigned int            fhsm_second;
27745 +       };
27746 +};
27747 +
27748 +/* opts flags */
27749 +#define AuOpts_REMOUNT         1
27750 +#define AuOpts_REFRESH         (1 << 1)
27751 +#define AuOpts_TRUNC_XIB       (1 << 2)
27752 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27753 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27754 +#define AuOpts_DR_FLUSHED      (1 << 5)
27755 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27756 +#define au_fset_opts(flags, name) \
27757 +       do { (flags) |= AuOpts_##name; } while (0)
27758 +#define au_fclr_opts(flags, name) \
27759 +       do { (flags) &= ~AuOpts_##name; } while (0)
27760 +
27761 +#ifndef CONFIG_AUFS_DIRREN
27762 +#undef AuOpts_DR_FLUSHED
27763 +#define AuOpts_DR_FLUSHED      0
27764 +#endif
27765 +
27766 +struct au_opts {
27767 +       struct au_opt   *opt;
27768 +       int             max_opt;
27769 +
27770 +       unsigned int    given_udba;
27771 +       unsigned int    flags;
27772 +       unsigned long   sb_flags;
27773 +};
27774 +
27775 +/* ---------------------------------------------------------------------- */
27776 +
27777 +/* opts.c */
27778 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27779 +const char *au_optstr_udba(int udba);
27780 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27781 +const char *au_optstr_wbr_create(int wbr_create);
27782 +
27783 +void au_opts_free(struct au_opts *opts);
27784 +struct super_block;
27785 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27786 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27787 +                  unsigned int pending);
27788 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27789 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27790 +
27791 +unsigned int au_opt_udba(struct super_block *sb);
27792 +
27793 +#endif /* __KERNEL__ */
27794 +#endif /* __AUFS_OPTS_H__ */
27795 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27796 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27797 +++ linux/fs/aufs/plink.c       2020-01-27 10:57:18.175538316 +0100
27798 @@ -0,0 +1,516 @@
27799 +// SPDX-License-Identifier: GPL-2.0
27800 +/*
27801 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27802 + *
27803 + * This program, aufs is free software; you can redistribute it and/or modify
27804 + * it under the terms of the GNU General Public License as published by
27805 + * the Free Software Foundation; either version 2 of the License, or
27806 + * (at your option) any later version.
27807 + *
27808 + * This program is distributed in the hope that it will be useful,
27809 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27810 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27811 + * GNU General Public License for more details.
27812 + *
27813 + * You should have received a copy of the GNU General Public License
27814 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27815 + */
27816 +
27817 +/*
27818 + * pseudo-link
27819 + */
27820 +
27821 +#include "aufs.h"
27822 +
27823 +/*
27824 + * the pseudo-link maintenance mode.
27825 + * during a user process maintains the pseudo-links,
27826 + * prohibit adding a new plink and branch manipulation.
27827 + *
27828 + * Flags
27829 + * NOPLM:
27830 + *     For entry functions which will handle plink, and i_mutex is already held
27831 + *     in VFS.
27832 + *     They cannot wait and should return an error at once.
27833 + *     Callers has to check the error.
27834 + * NOPLMW:
27835 + *     For entry functions which will handle plink, but i_mutex is not held
27836 + *     in VFS.
27837 + *     They can wait the plink maintenance mode to finish.
27838 + *
27839 + * They behave like F_SETLK and F_SETLKW.
27840 + * If the caller never handle plink, then both flags are unnecessary.
27841 + */
27842 +
27843 +int au_plink_maint(struct super_block *sb, int flags)
27844 +{
27845 +       int err;
27846 +       pid_t pid, ppid;
27847 +       struct task_struct *parent, *prev;
27848 +       struct au_sbinfo *sbi;
27849 +
27850 +       SiMustAnyLock(sb);
27851 +
27852 +       err = 0;
27853 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27854 +               goto out;
27855 +
27856 +       sbi = au_sbi(sb);
27857 +       pid = sbi->si_plink_maint_pid;
27858 +       if (!pid || pid == current->pid)
27859 +               goto out;
27860 +
27861 +       /* todo: it highly depends upon /sbin/mount.aufs */
27862 +       prev = NULL;
27863 +       parent = current;
27864 +       ppid = 0;
27865 +       rcu_read_lock();
27866 +       while (1) {
27867 +               parent = rcu_dereference(parent->real_parent);
27868 +               if (parent == prev)
27869 +                       break;
27870 +               ppid = task_pid_vnr(parent);
27871 +               if (pid == ppid) {
27872 +                       rcu_read_unlock();
27873 +                       goto out;
27874 +               }
27875 +               prev = parent;
27876 +       }
27877 +       rcu_read_unlock();
27878 +
27879 +       if (au_ftest_lock(flags, NOPLMW)) {
27880 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27881 +               /* AuDebugOn(!lockdep_depth(current)); */
27882 +               while (sbi->si_plink_maint_pid) {
27883 +                       si_read_unlock(sb);
27884 +                       /* gave up wake_up_bit() */
27885 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27886 +
27887 +                       if (au_ftest_lock(flags, FLUSH))
27888 +                               au_nwt_flush(&sbi->si_nowait);
27889 +                       si_noflush_read_lock(sb);
27890 +               }
27891 +       } else if (au_ftest_lock(flags, NOPLM)) {
27892 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27893 +               err = -EAGAIN;
27894 +       }
27895 +
27896 +out:
27897 +       return err;
27898 +}
27899 +
27900 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27901 +{
27902 +       spin_lock(&sbinfo->si_plink_maint_lock);
27903 +       sbinfo->si_plink_maint_pid = 0;
27904 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27905 +       wake_up_all(&sbinfo->si_plink_wq);
27906 +}
27907 +
27908 +int au_plink_maint_enter(struct super_block *sb)
27909 +{
27910 +       int err;
27911 +       struct au_sbinfo *sbinfo;
27912 +
27913 +       err = 0;
27914 +       sbinfo = au_sbi(sb);
27915 +       /* make sure i am the only one in this fs */
27916 +       si_write_lock(sb, AuLock_FLUSH);
27917 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27918 +               spin_lock(&sbinfo->si_plink_maint_lock);
27919 +               if (!sbinfo->si_plink_maint_pid)
27920 +                       sbinfo->si_plink_maint_pid = current->pid;
27921 +               else
27922 +                       err = -EBUSY;
27923 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27924 +       }
27925 +       si_write_unlock(sb);
27926 +
27927 +       return err;
27928 +}
27929 +
27930 +/* ---------------------------------------------------------------------- */
27931 +
27932 +#ifdef CONFIG_AUFS_DEBUG
27933 +void au_plink_list(struct super_block *sb)
27934 +{
27935 +       int i;
27936 +       struct au_sbinfo *sbinfo;
27937 +       struct hlist_bl_head *hbl;
27938 +       struct hlist_bl_node *pos;
27939 +       struct au_icntnr *icntnr;
27940 +
27941 +       SiMustAnyLock(sb);
27942 +
27943 +       sbinfo = au_sbi(sb);
27944 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27945 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27946 +
27947 +       for (i = 0; i < AuPlink_NHASH; i++) {
27948 +               hbl = sbinfo->si_plink + i;
27949 +               hlist_bl_lock(hbl);
27950 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27951 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27952 +               hlist_bl_unlock(hbl);
27953 +       }
27954 +}
27955 +#endif
27956 +
27957 +/* is the inode pseudo-linked? */
27958 +int au_plink_test(struct inode *inode)
27959 +{
27960 +       int found, i;
27961 +       struct au_sbinfo *sbinfo;
27962 +       struct hlist_bl_head *hbl;
27963 +       struct hlist_bl_node *pos;
27964 +       struct au_icntnr *icntnr;
27965 +
27966 +       sbinfo = au_sbi(inode->i_sb);
27967 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27968 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27969 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27970 +
27971 +       found = 0;
27972 +       i = au_plink_hash(inode->i_ino);
27973 +       hbl =  sbinfo->si_plink + i;
27974 +       hlist_bl_lock(hbl);
27975 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27976 +               if (&icntnr->vfs_inode == inode) {
27977 +                       found = 1;
27978 +                       break;
27979 +               }
27980 +       hlist_bl_unlock(hbl);
27981 +       return found;
27982 +}
27983 +
27984 +/* ---------------------------------------------------------------------- */
27985 +
27986 +/*
27987 + * generate a name for plink.
27988 + * the file will be stored under AUFS_WH_PLINKDIR.
27989 + */
27990 +/* 20 is max digits length of ulong 64 */
27991 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27992 +
27993 +static int plink_name(char *name, int len, struct inode *inode,
27994 +                     aufs_bindex_t bindex)
27995 +{
27996 +       int rlen;
27997 +       struct inode *h_inode;
27998 +
27999 +       h_inode = au_h_iptr(inode, bindex);
28000 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28001 +       return rlen;
28002 +}
28003 +
28004 +struct au_do_plink_lkup_args {
28005 +       struct dentry **errp;
28006 +       struct qstr *tgtname;
28007 +       struct dentry *h_parent;
28008 +       struct au_branch *br;
28009 +};
28010 +
28011 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28012 +                                      struct dentry *h_parent,
28013 +                                      struct au_branch *br)
28014 +{
28015 +       struct dentry *h_dentry;
28016 +       struct inode *h_inode;
28017 +
28018 +       h_inode = d_inode(h_parent);
28019 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28020 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
28021 +       inode_unlock_shared(h_inode);
28022 +       return h_dentry;
28023 +}
28024 +
28025 +static void au_call_do_plink_lkup(void *args)
28026 +{
28027 +       struct au_do_plink_lkup_args *a = args;
28028 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28029 +}
28030 +
28031 +/* lookup the plink-ed @inode under the branch at @bindex */
28032 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28033 +{
28034 +       struct dentry *h_dentry, *h_parent;
28035 +       struct au_branch *br;
28036 +       int wkq_err;
28037 +       char a[PLINK_NAME_LEN];
28038 +       struct qstr tgtname = QSTR_INIT(a, 0);
28039 +
28040 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28041 +
28042 +       br = au_sbr(inode->i_sb, bindex);
28043 +       h_parent = br->br_wbr->wbr_plink;
28044 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28045 +
28046 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28047 +               struct au_do_plink_lkup_args args = {
28048 +                       .errp           = &h_dentry,
28049 +                       .tgtname        = &tgtname,
28050 +                       .h_parent       = h_parent,
28051 +                       .br             = br
28052 +               };
28053 +
28054 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28055 +               if (unlikely(wkq_err))
28056 +                       h_dentry = ERR_PTR(wkq_err);
28057 +       } else
28058 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28059 +
28060 +       return h_dentry;
28061 +}
28062 +
28063 +/* create a pseudo-link */
28064 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28065 +                     struct dentry *h_dentry, struct au_branch *br)
28066 +{
28067 +       int err;
28068 +       struct path h_path = {
28069 +               .mnt = au_br_mnt(br)
28070 +       };
28071 +       struct inode *h_dir, *delegated;
28072 +
28073 +       h_dir = d_inode(h_parent);
28074 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28075 +again:
28076 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
28077 +       err = PTR_ERR(h_path.dentry);
28078 +       if (IS_ERR(h_path.dentry))
28079 +               goto out;
28080 +
28081 +       err = 0;
28082 +       /* wh.plink dir is not monitored */
28083 +       /* todo: is it really safe? */
28084 +       if (d_is_positive(h_path.dentry)
28085 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28086 +               delegated = NULL;
28087 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28088 +               if (unlikely(err == -EWOULDBLOCK)) {
28089 +                       pr_warn("cannot retry for NFSv4 delegation"
28090 +                               " for an internal unlink\n");
28091 +                       iput(delegated);
28092 +               }
28093 +               dput(h_path.dentry);
28094 +               h_path.dentry = NULL;
28095 +               if (!err)
28096 +                       goto again;
28097 +       }
28098 +       if (!err && d_is_negative(h_path.dentry)) {
28099 +               delegated = NULL;
28100 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28101 +               if (unlikely(err == -EWOULDBLOCK)) {
28102 +                       pr_warn("cannot retry for NFSv4 delegation"
28103 +                               " for an internal link\n");
28104 +                       iput(delegated);
28105 +               }
28106 +       }
28107 +       dput(h_path.dentry);
28108 +
28109 +out:
28110 +       inode_unlock(h_dir);
28111 +       return err;
28112 +}
28113 +
28114 +struct do_whplink_args {
28115 +       int *errp;
28116 +       struct qstr *tgt;
28117 +       struct dentry *h_parent;
28118 +       struct dentry *h_dentry;
28119 +       struct au_branch *br;
28120 +};
28121 +
28122 +static void call_do_whplink(void *args)
28123 +{
28124 +       struct do_whplink_args *a = args;
28125 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28126 +}
28127 +
28128 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28129 +                  aufs_bindex_t bindex, struct au_branch *br)
28130 +{
28131 +       int err, wkq_err;
28132 +       struct au_wbr *wbr;
28133 +       struct dentry *h_parent;
28134 +       char a[PLINK_NAME_LEN];
28135 +       struct qstr tgtname = QSTR_INIT(a, 0);
28136 +
28137 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28138 +       h_parent = wbr->wbr_plink;
28139 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28140 +
28141 +       /* always superio. */
28142 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28143 +               struct do_whplink_args args = {
28144 +                       .errp           = &err,
28145 +                       .tgt            = &tgtname,
28146 +                       .h_parent       = h_parent,
28147 +                       .h_dentry       = h_dentry,
28148 +                       .br             = br
28149 +               };
28150 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28151 +               if (unlikely(wkq_err))
28152 +                       err = wkq_err;
28153 +       } else
28154 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28155 +
28156 +       return err;
28157 +}
28158 +
28159 +/*
28160 + * create a new pseudo-link for @h_dentry on @bindex.
28161 + * the linked inode is held in aufs @inode.
28162 + */
28163 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28164 +                    struct dentry *h_dentry)
28165 +{
28166 +       struct super_block *sb;
28167 +       struct au_sbinfo *sbinfo;
28168 +       struct hlist_bl_head *hbl;
28169 +       struct hlist_bl_node *pos;
28170 +       struct au_icntnr *icntnr;
28171 +       int found, err, cnt, i;
28172 +
28173 +       sb = inode->i_sb;
28174 +       sbinfo = au_sbi(sb);
28175 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28176 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28177 +
28178 +       found = au_plink_test(inode);
28179 +       if (found)
28180 +               return;
28181 +
28182 +       i = au_plink_hash(inode->i_ino);
28183 +       hbl = sbinfo->si_plink + i;
28184 +       au_igrab(inode);
28185 +
28186 +       hlist_bl_lock(hbl);
28187 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28188 +               if (&icntnr->vfs_inode == inode) {
28189 +                       found = 1;
28190 +                       break;
28191 +               }
28192 +       }
28193 +       if (!found) {
28194 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28195 +               hlist_bl_add_head(&icntnr->plink, hbl);
28196 +       }
28197 +       hlist_bl_unlock(hbl);
28198 +       if (!found) {
28199 +               cnt = au_hbl_count(hbl);
28200 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28201 +               if (cnt > AUFS_PLINK_WARN)
28202 +                       AuWarn1(msg ", %d\n", cnt);
28203 +#undef msg
28204 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28205 +               if (unlikely(err)) {
28206 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28207 +                       au_hbl_del(&icntnr->plink, hbl);
28208 +                       iput(&icntnr->vfs_inode);
28209 +               }
28210 +       } else
28211 +               iput(&icntnr->vfs_inode);
28212 +}
28213 +
28214 +/* free all plinks */
28215 +void au_plink_put(struct super_block *sb, int verbose)
28216 +{
28217 +       int i, warned;
28218 +       struct au_sbinfo *sbinfo;
28219 +       struct hlist_bl_head *hbl;
28220 +       struct hlist_bl_node *pos, *tmp;
28221 +       struct au_icntnr *icntnr;
28222 +
28223 +       SiMustWriteLock(sb);
28224 +
28225 +       sbinfo = au_sbi(sb);
28226 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28227 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28228 +
28229 +       /* no spin_lock since sbinfo is write-locked */
28230 +       warned = 0;
28231 +       for (i = 0; i < AuPlink_NHASH; i++) {
28232 +               hbl = sbinfo->si_plink + i;
28233 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28234 +                       pr_warn("pseudo-link is not flushed");
28235 +                       warned = 1;
28236 +               }
28237 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28238 +                       iput(&icntnr->vfs_inode);
28239 +               INIT_HLIST_BL_HEAD(hbl);
28240 +       }
28241 +}
28242 +
28243 +void au_plink_clean(struct super_block *sb, int verbose)
28244 +{
28245 +       struct dentry *root;
28246 +
28247 +       root = sb->s_root;
28248 +       aufs_write_lock(root);
28249 +       if (au_opt_test(au_mntflags(sb), PLINK))
28250 +               au_plink_put(sb, verbose);
28251 +       aufs_write_unlock(root);
28252 +}
28253 +
28254 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28255 +{
28256 +       int do_put;
28257 +       aufs_bindex_t btop, bbot, bindex;
28258 +
28259 +       do_put = 0;
28260 +       btop = au_ibtop(inode);
28261 +       bbot = au_ibbot(inode);
28262 +       if (btop >= 0) {
28263 +               for (bindex = btop; bindex <= bbot; bindex++) {
28264 +                       if (!au_h_iptr(inode, bindex)
28265 +                           || au_ii_br_id(inode, bindex) != br_id)
28266 +                               continue;
28267 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28268 +                       do_put = 1;
28269 +                       break;
28270 +               }
28271 +               if (do_put)
28272 +                       for (bindex = btop; bindex <= bbot; bindex++)
28273 +                               if (au_h_iptr(inode, bindex)) {
28274 +                                       do_put = 0;
28275 +                                       break;
28276 +                               }
28277 +       } else
28278 +               do_put = 1;
28279 +
28280 +       return do_put;
28281 +}
28282 +
28283 +/* free the plinks on a branch specified by @br_id */
28284 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28285 +{
28286 +       struct au_sbinfo *sbinfo;
28287 +       struct hlist_bl_head *hbl;
28288 +       struct hlist_bl_node *pos, *tmp;
28289 +       struct au_icntnr *icntnr;
28290 +       struct inode *inode;
28291 +       int i, do_put;
28292 +
28293 +       SiMustWriteLock(sb);
28294 +
28295 +       sbinfo = au_sbi(sb);
28296 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28297 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28298 +
28299 +       /* no bit_lock since sbinfo is write-locked */
28300 +       for (i = 0; i < AuPlink_NHASH; i++) {
28301 +               hbl = sbinfo->si_plink + i;
28302 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28303 +                       inode = au_igrab(&icntnr->vfs_inode);
28304 +                       ii_write_lock_child(inode);
28305 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28306 +                       if (do_put) {
28307 +                               hlist_bl_del(&icntnr->plink);
28308 +                               iput(inode);
28309 +                       }
28310 +                       ii_write_unlock(inode);
28311 +                       iput(inode);
28312 +               }
28313 +       }
28314 +}
28315 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28316 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28317 +++ linux/fs/aufs/poll.c        2020-01-27 10:57:18.175538316 +0100
28318 @@ -0,0 +1,51 @@
28319 +// SPDX-License-Identifier: GPL-2.0
28320 +/*
28321 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28322 + *
28323 + * This program, aufs is free software; you can redistribute it and/or modify
28324 + * it under the terms of the GNU General Public License as published by
28325 + * the Free Software Foundation; either version 2 of the License, or
28326 + * (at your option) any later version.
28327 + *
28328 + * This program is distributed in the hope that it will be useful,
28329 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28330 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28331 + * GNU General Public License for more details.
28332 + *
28333 + * You should have received a copy of the GNU General Public License
28334 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28335 + */
28336 +
28337 +/*
28338 + * poll operation
28339 + * There is only one filesystem which implements ->poll operation, currently.
28340 + */
28341 +
28342 +#include "aufs.h"
28343 +
28344 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28345 +{
28346 +       __poll_t mask;
28347 +       struct file *h_file;
28348 +       struct super_block *sb;
28349 +
28350 +       /* We should pretend an error happened. */
28351 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28352 +       sb = file->f_path.dentry->d_sb;
28353 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28354 +
28355 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28356 +       if (IS_ERR(h_file)) {
28357 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28358 +               goto out;
28359 +       }
28360 +
28361 +       mask = vfs_poll(h_file, pt);
28362 +       fput(h_file); /* instead of au_read_post() */
28363 +
28364 +out:
28365 +       si_read_unlock(sb);
28366 +       if (mask & EPOLLERR)
28367 +               AuDbg("mask 0x%x\n", mask);
28368 +       return mask;
28369 +}
28370 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28371 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28372 +++ linux/fs/aufs/posix_acl.c   2020-01-27 10:57:18.175538316 +0100
28373 @@ -0,0 +1,105 @@
28374 +// SPDX-License-Identifier: GPL-2.0
28375 +/*
28376 + * Copyright (C) 2014-2020 Junjiro R. Okajima
28377 + *
28378 + * This program, aufs is free software; you can redistribute it and/or modify
28379 + * it under the terms of the GNU General Public License as published by
28380 + * the Free Software Foundation; either version 2 of the License, or
28381 + * (at your option) any later version.
28382 + *
28383 + * This program is distributed in the hope that it will be useful,
28384 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28385 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28386 + * GNU General Public License for more details.
28387 + *
28388 + * You should have received a copy of the GNU General Public License
28389 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28390 + */
28391 +
28392 +/*
28393 + * posix acl operations
28394 + */
28395 +
28396 +#include <linux/fs.h>
28397 +#include "aufs.h"
28398 +
28399 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28400 +{
28401 +       struct posix_acl *acl;
28402 +       int err;
28403 +       aufs_bindex_t bindex;
28404 +       struct inode *h_inode;
28405 +       struct super_block *sb;
28406 +
28407 +       acl = NULL;
28408 +       sb = inode->i_sb;
28409 +       si_read_lock(sb, AuLock_FLUSH);
28410 +       ii_read_lock_child(inode);
28411 +       if (!(sb->s_flags & SB_POSIXACL))
28412 +               goto out;
28413 +
28414 +       bindex = au_ibtop(inode);
28415 +       h_inode = au_h_iptr(inode, bindex);
28416 +       if (unlikely(!h_inode
28417 +                    || ((h_inode->i_mode & S_IFMT)
28418 +                        != (inode->i_mode & S_IFMT)))) {
28419 +               err = au_busy_or_stale();
28420 +               acl = ERR_PTR(err);
28421 +               goto out;
28422 +       }
28423 +
28424 +       /* always topmost only */
28425 +       acl = get_acl(h_inode, type);
28426 +       if (IS_ERR(acl))
28427 +               forget_cached_acl(inode, type);
28428 +       else
28429 +               set_cached_acl(inode, type, acl);
28430 +
28431 +out:
28432 +       ii_read_unlock(inode);
28433 +       si_read_unlock(sb);
28434 +
28435 +       AuTraceErrPtr(acl);
28436 +       return acl;
28437 +}
28438 +
28439 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28440 +{
28441 +       int err;
28442 +       ssize_t ssz;
28443 +       struct dentry *dentry;
28444 +       struct au_sxattr arg = {
28445 +               .type = AU_ACL_SET,
28446 +               .u.acl_set = {
28447 +                       .acl    = acl,
28448 +                       .type   = type
28449 +               },
28450 +       };
28451 +
28452 +       IMustLock(inode);
28453 +
28454 +       if (inode->i_ino == AUFS_ROOT_INO)
28455 +               dentry = dget(inode->i_sb->s_root);
28456 +       else {
28457 +               dentry = d_find_alias(inode);
28458 +               if (!dentry)
28459 +                       dentry = d_find_any_alias(inode);
28460 +               if (!dentry) {
28461 +                       pr_warn("cannot handle this inode, "
28462 +                               "please report to aufs-users ML\n");
28463 +                       err = -ENOENT;
28464 +                       goto out;
28465 +               }
28466 +       }
28467 +
28468 +       ssz = au_sxattr(dentry, inode, &arg);
28469 +       /* forget even it if succeeds since the branch might set differently */
28470 +       forget_cached_acl(inode, type);
28471 +       dput(dentry);
28472 +       err = ssz;
28473 +       if (ssz >= 0)
28474 +               err = 0;
28475 +
28476 +out:
28477 +       return err;
28478 +}
28479 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28480 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28481 +++ linux/fs/aufs/procfs.c      2020-08-03 09:14:43.142321747 +0200
28482 @@ -0,0 +1,170 @@
28483 +// SPDX-License-Identifier: GPL-2.0
28484 +/*
28485 + * Copyright (C) 2010-2020 Junjiro R. Okajima
28486 + *
28487 + * This program, aufs is free software; you can redistribute it and/or modify
28488 + * it under the terms of the GNU General Public License as published by
28489 + * the Free Software Foundation; either version 2 of the License, or
28490 + * (at your option) any later version.
28491 + *
28492 + * This program is distributed in the hope that it will be useful,
28493 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28494 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28495 + * GNU General Public License for more details.
28496 + *
28497 + * You should have received a copy of the GNU General Public License
28498 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28499 + */
28500 +
28501 +/*
28502 + * procfs interfaces
28503 + */
28504 +
28505 +#include <linux/proc_fs.h>
28506 +#include "aufs.h"
28507 +
28508 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28509 +{
28510 +       struct au_sbinfo *sbinfo;
28511 +
28512 +       sbinfo = file->private_data;
28513 +       if (sbinfo) {
28514 +               au_plink_maint_leave(sbinfo);
28515 +               kobject_put(&sbinfo->si_kobj);
28516 +       }
28517 +
28518 +       return 0;
28519 +}
28520 +
28521 +static void au_procfs_plm_write_clean(struct file *file)
28522 +{
28523 +       struct au_sbinfo *sbinfo;
28524 +
28525 +       sbinfo = file->private_data;
28526 +       if (sbinfo)
28527 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28528 +}
28529 +
28530 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28531 +{
28532 +       int err;
28533 +       struct super_block *sb;
28534 +       struct au_sbinfo *sbinfo;
28535 +       struct hlist_bl_node *pos;
28536 +
28537 +       err = -EBUSY;
28538 +       if (unlikely(file->private_data))
28539 +               goto out;
28540 +
28541 +       sb = NULL;
28542 +       /* don't use au_sbilist_lock() here */
28543 +       hlist_bl_lock(&au_sbilist);
28544 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28545 +               if (id == sysaufs_si_id(sbinfo)) {
28546 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28547 +                               sb = sbinfo->si_sb;
28548 +                       break;
28549 +               }
28550 +       hlist_bl_unlock(&au_sbilist);
28551 +
28552 +       err = -EINVAL;
28553 +       if (unlikely(!sb))
28554 +               goto out;
28555 +
28556 +       err = au_plink_maint_enter(sb);
28557 +       if (!err)
28558 +               /* keep kobject_get() */
28559 +               file->private_data = sbinfo;
28560 +       else
28561 +               kobject_put(&sbinfo->si_kobj);
28562 +out:
28563 +       return err;
28564 +}
28565 +
28566 +/*
28567 + * Accept a valid "si=xxxx" only.
28568 + * Once it is accepted successfully, accept "clean" too.
28569 + */
28570 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28571 +                                  size_t count, loff_t *ppos)
28572 +{
28573 +       ssize_t err;
28574 +       unsigned long id;
28575 +       /* last newline is allowed */
28576 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28577 +
28578 +       err = -EACCES;
28579 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28580 +               goto out;
28581 +
28582 +       err = -EINVAL;
28583 +       if (unlikely(count > sizeof(buf)))
28584 +               goto out;
28585 +
28586 +       err = copy_from_user(buf, ubuf, count);
28587 +       if (unlikely(err)) {
28588 +               err = -EFAULT;
28589 +               goto out;
28590 +       }
28591 +       buf[count] = 0;
28592 +
28593 +       err = -EINVAL;
28594 +       if (!strcmp("clean", buf)) {
28595 +               au_procfs_plm_write_clean(file);
28596 +               goto out_success;
28597 +       } else if (unlikely(strncmp("si=", buf, 3)))
28598 +               goto out;
28599 +
28600 +       err = kstrtoul(buf + 3, 16, &id);
28601 +       if (unlikely(err))
28602 +               goto out;
28603 +
28604 +       err = au_procfs_plm_write_si(file, id);
28605 +       if (unlikely(err))
28606 +               goto out;
28607 +
28608 +out_success:
28609 +       err = count; /* success */
28610 +out:
28611 +       return err;
28612 +}
28613 +
28614 +static const struct proc_ops au_procfs_plm_op = {
28615 +       .proc_write     = au_procfs_plm_write,
28616 +       .proc_release   = au_procfs_plm_release
28617 +};
28618 +
28619 +/* ---------------------------------------------------------------------- */
28620 +
28621 +static struct proc_dir_entry *au_procfs_dir;
28622 +
28623 +void au_procfs_fin(void)
28624 +{
28625 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28626 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28627 +}
28628 +
28629 +int __init au_procfs_init(void)
28630 +{
28631 +       int err;
28632 +       struct proc_dir_entry *entry;
28633 +
28634 +       err = -ENOMEM;
28635 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28636 +       if (unlikely(!au_procfs_dir))
28637 +               goto out;
28638 +
28639 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28640 +                           au_procfs_dir, &au_procfs_plm_op);
28641 +       if (unlikely(!entry))
28642 +               goto out_dir;
28643 +
28644 +       err = 0;
28645 +       goto out; /* success */
28646 +
28647 +
28648 +out_dir:
28649 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28650 +out:
28651 +       return err;
28652 +}
28653 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28654 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28655 +++ linux/fs/aufs/rdu.c 2020-01-27 10:57:18.178871751 +0100
28656 @@ -0,0 +1,384 @@
28657 +// SPDX-License-Identifier: GPL-2.0
28658 +/*
28659 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28660 + *
28661 + * This program, aufs is free software; you can redistribute it and/or modify
28662 + * it under the terms of the GNU General Public License as published by
28663 + * the Free Software Foundation; either version 2 of the License, or
28664 + * (at your option) any later version.
28665 + *
28666 + * This program is distributed in the hope that it will be useful,
28667 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28668 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28669 + * GNU General Public License for more details.
28670 + *
28671 + * You should have received a copy of the GNU General Public License
28672 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28673 + */
28674 +
28675 +/*
28676 + * readdir in userspace.
28677 + */
28678 +
28679 +#include <linux/compat.h>
28680 +#include <linux/fs_stack.h>
28681 +#include <linux/security.h>
28682 +#include "aufs.h"
28683 +
28684 +/* bits for struct aufs_rdu.flags */
28685 +#define        AuRdu_CALLED    1
28686 +#define        AuRdu_CONT      (1 << 1)
28687 +#define        AuRdu_FULL      (1 << 2)
28688 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28689 +#define au_fset_rdu(flags, name) \
28690 +       do { (flags) |= AuRdu_##name; } while (0)
28691 +#define au_fclr_rdu(flags, name) \
28692 +       do { (flags) &= ~AuRdu_##name; } while (0)
28693 +
28694 +struct au_rdu_arg {
28695 +       struct dir_context              ctx;
28696 +       struct aufs_rdu                 *rdu;
28697 +       union au_rdu_ent_ul             ent;
28698 +       unsigned long                   end;
28699 +
28700 +       struct super_block              *sb;
28701 +       int                             err;
28702 +};
28703 +
28704 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28705 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28706 +{
28707 +       int err, len;
28708 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28709 +       struct aufs_rdu *rdu = arg->rdu;
28710 +       struct au_rdu_ent ent;
28711 +
28712 +       err = 0;
28713 +       arg->err = 0;
28714 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28715 +       len = au_rdu_len(nlen);
28716 +       if (arg->ent.ul + len  < arg->end) {
28717 +               ent.ino = h_ino;
28718 +               ent.bindex = rdu->cookie.bindex;
28719 +               ent.type = d_type;
28720 +               ent.nlen = nlen;
28721 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28722 +                       ent.type = DT_UNKNOWN;
28723 +
28724 +               /* unnecessary to support mmap_sem since this is a dir */
28725 +               err = -EFAULT;
28726 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28727 +                       goto out;
28728 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28729 +                       goto out;
28730 +               /* the terminating NULL */
28731 +               if (__put_user(0, arg->ent.e->name + nlen))
28732 +                       goto out;
28733 +               err = 0;
28734 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28735 +               arg->ent.ul += len;
28736 +               rdu->rent++;
28737 +       } else {
28738 +               err = -EFAULT;
28739 +               au_fset_rdu(rdu->cookie.flags, FULL);
28740 +               rdu->full = 1;
28741 +               rdu->tail = arg->ent;
28742 +       }
28743 +
28744 +out:
28745 +       /* AuTraceErr(err); */
28746 +       return err;
28747 +}
28748 +
28749 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28750 +{
28751 +       int err;
28752 +       loff_t offset;
28753 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28754 +
28755 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28756 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28757 +       err = offset;
28758 +       if (unlikely(offset != cookie->h_pos))
28759 +               goto out;
28760 +
28761 +       err = 0;
28762 +       do {
28763 +               arg->err = 0;
28764 +               au_fclr_rdu(cookie->flags, CALLED);
28765 +               /* smp_mb(); */
28766 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28767 +               if (err >= 0)
28768 +                       err = arg->err;
28769 +       } while (!err
28770 +                && au_ftest_rdu(cookie->flags, CALLED)
28771 +                && !au_ftest_rdu(cookie->flags, FULL));
28772 +       cookie->h_pos = h_file->f_pos;
28773 +
28774 +out:
28775 +       AuTraceErr(err);
28776 +       return err;
28777 +}
28778 +
28779 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28780 +{
28781 +       int err;
28782 +       aufs_bindex_t bbot;
28783 +       struct au_rdu_arg arg = {
28784 +               .ctx = {
28785 +                       .actor = au_rdu_fill
28786 +               }
28787 +       };
28788 +       struct dentry *dentry;
28789 +       struct inode *inode;
28790 +       struct file *h_file;
28791 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28792 +
28793 +       /* VERIFY_WRITE */
28794 +       err = !access_ok(rdu->ent.e, rdu->sz);
28795 +       if (unlikely(err)) {
28796 +               err = -EFAULT;
28797 +               AuTraceErr(err);
28798 +               goto out;
28799 +       }
28800 +       rdu->rent = 0;
28801 +       rdu->tail = rdu->ent;
28802 +       rdu->full = 0;
28803 +       arg.rdu = rdu;
28804 +       arg.ent = rdu->ent;
28805 +       arg.end = arg.ent.ul;
28806 +       arg.end += rdu->sz;
28807 +
28808 +       err = -ENOTDIR;
28809 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28810 +               goto out;
28811 +
28812 +       err = security_file_permission(file, MAY_READ);
28813 +       AuTraceErr(err);
28814 +       if (unlikely(err))
28815 +               goto out;
28816 +
28817 +       dentry = file->f_path.dentry;
28818 +       inode = d_inode(dentry);
28819 +       inode_lock_shared(inode);
28820 +
28821 +       arg.sb = inode->i_sb;
28822 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28823 +       if (unlikely(err))
28824 +               goto out_mtx;
28825 +       err = au_alive_dir(dentry);
28826 +       if (unlikely(err))
28827 +               goto out_si;
28828 +       /* todo: reval? */
28829 +       fi_read_lock(file);
28830 +
28831 +       err = -EAGAIN;
28832 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28833 +                    && cookie->generation != au_figen(file)))
28834 +               goto out_unlock;
28835 +
28836 +       err = 0;
28837 +       if (!rdu->blk) {
28838 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28839 +               if (!rdu->blk)
28840 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28841 +       }
28842 +       bbot = au_fbtop(file);
28843 +       if (cookie->bindex < bbot)
28844 +               cookie->bindex = bbot;
28845 +       bbot = au_fbbot_dir(file);
28846 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28847 +       for (; !err && cookie->bindex <= bbot;
28848 +            cookie->bindex++, cookie->h_pos = 0) {
28849 +               h_file = au_hf_dir(file, cookie->bindex);
28850 +               if (!h_file)
28851 +                       continue;
28852 +
28853 +               au_fclr_rdu(cookie->flags, FULL);
28854 +               err = au_rdu_do(h_file, &arg);
28855 +               AuTraceErr(err);
28856 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28857 +                       break;
28858 +       }
28859 +       AuDbg("rent %llu\n", rdu->rent);
28860 +
28861 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28862 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28863 +               au_fset_rdu(cookie->flags, CONT);
28864 +               cookie->generation = au_figen(file);
28865 +       }
28866 +
28867 +       ii_read_lock_child(inode);
28868 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28869 +       ii_read_unlock(inode);
28870 +
28871 +out_unlock:
28872 +       fi_read_unlock(file);
28873 +out_si:
28874 +       si_read_unlock(arg.sb);
28875 +out_mtx:
28876 +       inode_unlock_shared(inode);
28877 +out:
28878 +       AuTraceErr(err);
28879 +       return err;
28880 +}
28881 +
28882 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28883 +{
28884 +       int err;
28885 +       ino_t ino;
28886 +       unsigned long long nent;
28887 +       union au_rdu_ent_ul *u;
28888 +       struct au_rdu_ent ent;
28889 +       struct super_block *sb;
28890 +
28891 +       err = 0;
28892 +       nent = rdu->nent;
28893 +       u = &rdu->ent;
28894 +       sb = file->f_path.dentry->d_sb;
28895 +       si_read_lock(sb, AuLock_FLUSH);
28896 +       while (nent-- > 0) {
28897 +               /* unnecessary to support mmap_sem since this is a dir */
28898 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28899 +               if (!err)
28900 +                       /* VERIFY_WRITE */
28901 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28902 +               if (unlikely(err)) {
28903 +                       err = -EFAULT;
28904 +                       AuTraceErr(err);
28905 +                       break;
28906 +               }
28907 +
28908 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28909 +               if (!ent.wh)
28910 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28911 +               else
28912 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28913 +                                       &ino);
28914 +               if (unlikely(err)) {
28915 +                       AuTraceErr(err);
28916 +                       break;
28917 +               }
28918 +
28919 +               err = __put_user(ino, &u->e->ino);
28920 +               if (unlikely(err)) {
28921 +                       err = -EFAULT;
28922 +                       AuTraceErr(err);
28923 +                       break;
28924 +               }
28925 +               u->ul += au_rdu_len(ent.nlen);
28926 +       }
28927 +       si_read_unlock(sb);
28928 +
28929 +       return err;
28930 +}
28931 +
28932 +/* ---------------------------------------------------------------------- */
28933 +
28934 +static int au_rdu_verify(struct aufs_rdu *rdu)
28935 +{
28936 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28937 +             "%llu, b%d, 0x%x, g%u}\n",
28938 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28939 +             rdu->blk,
28940 +             rdu->rent, rdu->shwh, rdu->full,
28941 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28942 +             rdu->cookie.generation);
28943 +
28944 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28945 +               return 0;
28946 +
28947 +       AuDbg("%u:%u\n",
28948 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28949 +       return -EINVAL;
28950 +}
28951 +
28952 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28953 +{
28954 +       long err, e;
28955 +       struct aufs_rdu rdu;
28956 +       void __user *p = (void __user *)arg;
28957 +
28958 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28959 +       if (unlikely(err)) {
28960 +               err = -EFAULT;
28961 +               AuTraceErr(err);
28962 +               goto out;
28963 +       }
28964 +       err = au_rdu_verify(&rdu);
28965 +       if (unlikely(err))
28966 +               goto out;
28967 +
28968 +       switch (cmd) {
28969 +       case AUFS_CTL_RDU:
28970 +               err = au_rdu(file, &rdu);
28971 +               if (unlikely(err))
28972 +                       break;
28973 +
28974 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28975 +               if (unlikely(e)) {
28976 +                       err = -EFAULT;
28977 +                       AuTraceErr(err);
28978 +               }
28979 +               break;
28980 +       case AUFS_CTL_RDU_INO:
28981 +               err = au_rdu_ino(file, &rdu);
28982 +               break;
28983 +
28984 +       default:
28985 +               /* err = -ENOTTY; */
28986 +               err = -EINVAL;
28987 +       }
28988 +
28989 +out:
28990 +       AuTraceErr(err);
28991 +       return err;
28992 +}
28993 +
28994 +#ifdef CONFIG_COMPAT
28995 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28996 +{
28997 +       long err, e;
28998 +       struct aufs_rdu rdu;
28999 +       void __user *p = compat_ptr(arg);
29000 +
29001 +       /* todo: get_user()? */
29002 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29003 +       if (unlikely(err)) {
29004 +               err = -EFAULT;
29005 +               AuTraceErr(err);
29006 +               goto out;
29007 +       }
29008 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29009 +       err = au_rdu_verify(&rdu);
29010 +       if (unlikely(err))
29011 +               goto out;
29012 +
29013 +       switch (cmd) {
29014 +       case AUFS_CTL_RDU:
29015 +               err = au_rdu(file, &rdu);
29016 +               if (unlikely(err))
29017 +                       break;
29018 +
29019 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29020 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29021 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29022 +               if (unlikely(e)) {
29023 +                       err = -EFAULT;
29024 +                       AuTraceErr(err);
29025 +               }
29026 +               break;
29027 +       case AUFS_CTL_RDU_INO:
29028 +               err = au_rdu_ino(file, &rdu);
29029 +               break;
29030 +
29031 +       default:
29032 +               /* err = -ENOTTY; */
29033 +               err = -EINVAL;
29034 +       }
29035 +
29036 +out:
29037 +       AuTraceErr(err);
29038 +       return err;
29039 +}
29040 +#endif
29041 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29042 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29043 +++ linux/fs/aufs/rwsem.h       2020-01-27 10:57:18.178871751 +0100
29044 @@ -0,0 +1,73 @@
29045 +/* SPDX-License-Identifier: GPL-2.0 */
29046 +/*
29047 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29048 + *
29049 + * This program, aufs is free software; you can redistribute it and/or modify
29050 + * it under the terms of the GNU General Public License as published by
29051 + * the Free Software Foundation; either version 2 of the License, or
29052 + * (at your option) any later version.
29053 + *
29054 + * This program is distributed in the hope that it will be useful,
29055 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29056 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29057 + * GNU General Public License for more details.
29058 + *
29059 + * You should have received a copy of the GNU General Public License
29060 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29061 + */
29062 +
29063 +/*
29064 + * simple read-write semaphore wrappers
29065 + */
29066 +
29067 +#ifndef __AUFS_RWSEM_H__
29068 +#define __AUFS_RWSEM_H__
29069 +
29070 +#ifdef __KERNEL__
29071 +
29072 +#include "debug.h"
29073 +
29074 +/* in the future, the name 'au_rwsem' will be totally gone */
29075 +#define au_rwsem       rw_semaphore
29076 +
29077 +/* to debug easier, do not make them inlined functions */
29078 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29079 +/* rwsem_is_locked() is unusable */
29080 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
29081 +                                         && debug_locks \
29082 +                                         && !lockdep_is_held_type(rw, 1))
29083 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
29084 +                                         && debug_locks \
29085 +                                         && !lockdep_is_held_type(rw, 0))
29086 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
29087 +                                         && debug_locks \
29088 +                                         && !lockdep_is_held(rw))
29089 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
29090 +                                         && debug_locks \
29091 +                                         && lockdep_is_held(rw))
29092 +
29093 +#define au_rw_init(rw) init_rwsem(rw)
29094 +
29095 +#define au_rw_init_wlock(rw) do {              \
29096 +               au_rw_init(rw);                 \
29097 +               down_write(rw);                 \
29098 +       } while (0)
29099 +
29100 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29101 +               au_rw_init(rw);                 \
29102 +               down_write_nested(rw, lsc);     \
29103 +       } while (0)
29104 +
29105 +#define au_rw_read_lock(rw)            down_read(rw)
29106 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29107 +#define au_rw_read_unlock(rw)          up_read(rw)
29108 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29109 +#define au_rw_write_lock(rw)           down_write(rw)
29110 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29111 +#define au_rw_write_unlock(rw)         up_write(rw)
29112 +/* why is not _nested version defined? */
29113 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29114 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29115 +
29116 +#endif /* __KERNEL__ */
29117 +#endif /* __AUFS_RWSEM_H__ */
29118 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29119 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29120 +++ linux/fs/aufs/sbinfo.c      2020-01-27 10:57:18.178871751 +0100
29121 @@ -0,0 +1,314 @@
29122 +// SPDX-License-Identifier: GPL-2.0
29123 +/*
29124 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29125 + *
29126 + * This program, aufs is free software; you can redistribute it and/or modify
29127 + * it under the terms of the GNU General Public License as published by
29128 + * the Free Software Foundation; either version 2 of the License, or
29129 + * (at your option) any later version.
29130 + *
29131 + * This program is distributed in the hope that it will be useful,
29132 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29133 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29134 + * GNU General Public License for more details.
29135 + *
29136 + * You should have received a copy of the GNU General Public License
29137 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29138 + */
29139 +
29140 +/*
29141 + * superblock private data
29142 + */
29143 +
29144 +#include <linux/iversion.h>
29145 +#include "aufs.h"
29146 +
29147 +/*
29148 + * they are necessary regardless sysfs is disabled.
29149 + */
29150 +void au_si_free(struct kobject *kobj)
29151 +{
29152 +       int i;
29153 +       struct au_sbinfo *sbinfo;
29154 +       char *locked __maybe_unused; /* debug only */
29155 +
29156 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29157 +       for (i = 0; i < AuPlink_NHASH; i++)
29158 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29159 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29160 +
29161 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29162 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29163 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29164 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29165 +
29166 +       dbgaufs_si_fin(sbinfo);
29167 +       au_rw_write_lock(&sbinfo->si_rwsem);
29168 +       au_br_free(sbinfo);
29169 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29170 +
29171 +       au_kfree_try_rcu(sbinfo->si_branch);
29172 +       mutex_destroy(&sbinfo->si_xib_mtx);
29173 +       AuRwDestroy(&sbinfo->si_rwsem);
29174 +
29175 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29176 +       /* si_nfiles is waited too */
29177 +       au_kfree_rcu(sbinfo);
29178 +}
29179 +
29180 +int au_si_alloc(struct super_block *sb)
29181 +{
29182 +       int err, i;
29183 +       struct au_sbinfo *sbinfo;
29184 +
29185 +       err = -ENOMEM;
29186 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29187 +       if (unlikely(!sbinfo))
29188 +               goto out;
29189 +
29190 +       /* will be reallocated separately */
29191 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29192 +       if (unlikely(!sbinfo->si_branch))
29193 +               goto out_sbinfo;
29194 +
29195 +       err = sysaufs_si_init(sbinfo);
29196 +       if (!err) {
29197 +               dbgaufs_si_null(sbinfo);
29198 +               err = dbgaufs_si_init(sbinfo);
29199 +               if (unlikely(err))
29200 +                       kobject_put(&sbinfo->si_kobj);
29201 +       }
29202 +       if (unlikely(err))
29203 +               goto out_br;
29204 +
29205 +       au_nwt_init(&sbinfo->si_nowait);
29206 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29207 +
29208 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29209 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29210 +
29211 +       sbinfo->si_bbot = -1;
29212 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29213 +
29214 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29215 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29216 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29217 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29218 +
29219 +       au_fhsm_init(sbinfo);
29220 +
29221 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29222 +
29223 +       sbinfo->si_xino_jiffy = jiffies;
29224 +       sbinfo->si_xino_expire
29225 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29226 +       mutex_init(&sbinfo->si_xib_mtx);
29227 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29228 +
29229 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29230 +
29231 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29232 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29233 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29234 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29235 +
29236 +       for (i = 0; i < AuPlink_NHASH; i++)
29237 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29238 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29239 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29240 +
29241 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29242 +
29243 +       /* with getattr by default */
29244 +       sbinfo->si_iop_array = aufs_iop;
29245 +
29246 +       /* leave other members for sysaufs and si_mnt. */
29247 +       sbinfo->si_sb = sb;
29248 +       sb->s_fs_info = sbinfo;
29249 +       si_pid_set(sb);
29250 +       return 0; /* success */
29251 +
29252 +out_br:
29253 +       au_kfree_try_rcu(sbinfo->si_branch);
29254 +out_sbinfo:
29255 +       au_kfree_rcu(sbinfo);
29256 +out:
29257 +       return err;
29258 +}
29259 +
29260 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29261 +{
29262 +       int err, sz;
29263 +       struct au_branch **brp;
29264 +
29265 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29266 +
29267 +       err = -ENOMEM;
29268 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29269 +       if (unlikely(!sz))
29270 +               sz = sizeof(*brp);
29271 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29272 +                          may_shrink);
29273 +       if (brp) {
29274 +               sbinfo->si_branch = brp;
29275 +               err = 0;
29276 +       }
29277 +
29278 +       return err;
29279 +}
29280 +
29281 +/* ---------------------------------------------------------------------- */
29282 +
29283 +unsigned int au_sigen_inc(struct super_block *sb)
29284 +{
29285 +       unsigned int gen;
29286 +       struct inode *inode;
29287 +
29288 +       SiMustWriteLock(sb);
29289 +
29290 +       gen = ++au_sbi(sb)->si_generation;
29291 +       au_update_digen(sb->s_root);
29292 +       inode = d_inode(sb->s_root);
29293 +       au_update_iigen(inode, /*half*/0);
29294 +       inode_inc_iversion(inode);
29295 +       return gen;
29296 +}
29297 +
29298 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29299 +{
29300 +       aufs_bindex_t br_id;
29301 +       int i;
29302 +       struct au_sbinfo *sbinfo;
29303 +
29304 +       SiMustWriteLock(sb);
29305 +
29306 +       sbinfo = au_sbi(sb);
29307 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29308 +               br_id = ++sbinfo->si_last_br_id;
29309 +               AuDebugOn(br_id < 0);
29310 +               if (br_id && au_br_index(sb, br_id) < 0)
29311 +                       return br_id;
29312 +       }
29313 +
29314 +       return -1;
29315 +}
29316 +
29317 +/* ---------------------------------------------------------------------- */
29318 +
29319 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29320 +int si_read_lock(struct super_block *sb, int flags)
29321 +{
29322 +       int err;
29323 +
29324 +       err = 0;
29325 +       if (au_ftest_lock(flags, FLUSH))
29326 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29327 +
29328 +       si_noflush_read_lock(sb);
29329 +       err = au_plink_maint(sb, flags);
29330 +       if (unlikely(err))
29331 +               si_read_unlock(sb);
29332 +
29333 +       return err;
29334 +}
29335 +
29336 +int si_write_lock(struct super_block *sb, int flags)
29337 +{
29338 +       int err;
29339 +
29340 +       if (au_ftest_lock(flags, FLUSH))
29341 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29342 +
29343 +       si_noflush_write_lock(sb);
29344 +       err = au_plink_maint(sb, flags);
29345 +       if (unlikely(err))
29346 +               si_write_unlock(sb);
29347 +
29348 +       return err;
29349 +}
29350 +
29351 +/* dentry and super_block lock. call at entry point */
29352 +int aufs_read_lock(struct dentry *dentry, int flags)
29353 +{
29354 +       int err;
29355 +       struct super_block *sb;
29356 +
29357 +       sb = dentry->d_sb;
29358 +       err = si_read_lock(sb, flags);
29359 +       if (unlikely(err))
29360 +               goto out;
29361 +
29362 +       if (au_ftest_lock(flags, DW))
29363 +               di_write_lock_child(dentry);
29364 +       else
29365 +               di_read_lock_child(dentry, flags);
29366 +
29367 +       if (au_ftest_lock(flags, GEN)) {
29368 +               err = au_digen_test(dentry, au_sigen(sb));
29369 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29370 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29371 +               else if (!err)
29372 +                       err = au_dbrange_test(dentry);
29373 +               if (unlikely(err))
29374 +                       aufs_read_unlock(dentry, flags);
29375 +       }
29376 +
29377 +out:
29378 +       return err;
29379 +}
29380 +
29381 +void aufs_read_unlock(struct dentry *dentry, int flags)
29382 +{
29383 +       if (au_ftest_lock(flags, DW))
29384 +               di_write_unlock(dentry);
29385 +       else
29386 +               di_read_unlock(dentry, flags);
29387 +       si_read_unlock(dentry->d_sb);
29388 +}
29389 +
29390 +void aufs_write_lock(struct dentry *dentry)
29391 +{
29392 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29393 +       di_write_lock_child(dentry);
29394 +}
29395 +
29396 +void aufs_write_unlock(struct dentry *dentry)
29397 +{
29398 +       di_write_unlock(dentry);
29399 +       si_write_unlock(dentry->d_sb);
29400 +}
29401 +
29402 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29403 +{
29404 +       int err;
29405 +       unsigned int sigen;
29406 +       struct super_block *sb;
29407 +
29408 +       sb = d1->d_sb;
29409 +       err = si_read_lock(sb, flags);
29410 +       if (unlikely(err))
29411 +               goto out;
29412 +
29413 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29414 +
29415 +       if (au_ftest_lock(flags, GEN)) {
29416 +               sigen = au_sigen(sb);
29417 +               err = au_digen_test(d1, sigen);
29418 +               AuDebugOn(!err && au_dbrange_test(d1));
29419 +               if (!err) {
29420 +                       err = au_digen_test(d2, sigen);
29421 +                       AuDebugOn(!err && au_dbrange_test(d2));
29422 +               }
29423 +               if (unlikely(err))
29424 +                       aufs_read_and_write_unlock2(d1, d2);
29425 +       }
29426 +
29427 +out:
29428 +       return err;
29429 +}
29430 +
29431 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29432 +{
29433 +       di_write_unlock2(d1, d2);
29434 +       si_read_unlock(d1->d_sb);
29435 +}
29436 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29437 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29438 +++ linux/fs/aufs/super.c       2020-01-27 10:57:18.178871751 +0100
29439 @@ -0,0 +1,1047 @@
29440 +// SPDX-License-Identifier: GPL-2.0
29441 +/*
29442 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29443 + *
29444 + * This program, aufs is free software; you can redistribute it and/or modify
29445 + * it under the terms of the GNU General Public License as published by
29446 + * the Free Software Foundation; either version 2 of the License, or
29447 + * (at your option) any later version.
29448 + *
29449 + * This program is distributed in the hope that it will be useful,
29450 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29451 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29452 + * GNU General Public License for more details.
29453 + *
29454 + * You should have received a copy of the GNU General Public License
29455 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29456 + */
29457 +
29458 +/*
29459 + * mount and super_block operations
29460 + */
29461 +
29462 +#include <linux/iversion.h>
29463 +#include <linux/mm.h>
29464 +#include <linux/seq_file.h>
29465 +#include <linux/statfs.h>
29466 +#include <linux/vmalloc.h>
29467 +#include "aufs.h"
29468 +
29469 +/*
29470 + * super_operations
29471 + */
29472 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29473 +{
29474 +       struct au_icntnr *c;
29475 +
29476 +       c = au_cache_alloc_icntnr();
29477 +       if (c) {
29478 +               au_icntnr_init(c);
29479 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29480 +               c->iinfo.ii_hinode = NULL;
29481 +               return &c->vfs_inode;
29482 +       }
29483 +       return NULL;
29484 +}
29485 +
29486 +static void aufs_destroy_inode(struct inode *inode)
29487 +{
29488 +       if (!au_is_bad_inode(inode))
29489 +               au_iinfo_fin(inode);
29490 +}
29491 +
29492 +static void aufs_free_inode(struct inode *inode)
29493 +{
29494 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29495 +}
29496 +
29497 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29498 +{
29499 +       struct inode *inode;
29500 +       int err;
29501 +
29502 +       inode = iget_locked(sb, ino);
29503 +       if (unlikely(!inode)) {
29504 +               inode = ERR_PTR(-ENOMEM);
29505 +               goto out;
29506 +       }
29507 +       if (!(inode->i_state & I_NEW))
29508 +               goto out;
29509 +
29510 +       err = au_xigen_new(inode);
29511 +       if (!err)
29512 +               err = au_iinfo_init(inode);
29513 +       if (!err)
29514 +               inode_inc_iversion(inode);
29515 +       else {
29516 +               iget_failed(inode);
29517 +               inode = ERR_PTR(err);
29518 +       }
29519 +
29520 +out:
29521 +       /* never return NULL */
29522 +       AuDebugOn(!inode);
29523 +       AuTraceErrPtr(inode);
29524 +       return inode;
29525 +}
29526 +
29527 +/* lock free root dinfo */
29528 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29529 +{
29530 +       int err;
29531 +       aufs_bindex_t bindex, bbot;
29532 +       struct path path;
29533 +       struct au_hdentry *hdp;
29534 +       struct au_branch *br;
29535 +       au_br_perm_str_t perm;
29536 +
29537 +       err = 0;
29538 +       bbot = au_sbbot(sb);
29539 +       bindex = 0;
29540 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29541 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29542 +               br = au_sbr(sb, bindex);
29543 +               path.mnt = au_br_mnt(br);
29544 +               path.dentry = hdp->hd_dentry;
29545 +               err = au_seq_path(seq, &path);
29546 +               if (!err) {
29547 +                       au_optstr_br_perm(&perm, br->br_perm);
29548 +                       seq_printf(seq, "=%s", perm.a);
29549 +                       if (bindex != bbot)
29550 +                               seq_putc(seq, ':');
29551 +               }
29552 +       }
29553 +       if (unlikely(err || seq_has_overflowed(seq)))
29554 +               err = -E2BIG;
29555 +
29556 +       return err;
29557 +}
29558 +
29559 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29560 +                      const char *append)
29561 +{
29562 +       char *p;
29563 +
29564 +       p = fmt;
29565 +       while (*pat != ':')
29566 +               *p++ = *pat++;
29567 +       *p++ = *pat++;
29568 +       strcpy(p, append);
29569 +       AuDebugOn(strlen(fmt) >= len);
29570 +}
29571 +
29572 +static void au_show_wbr_create(struct seq_file *m, int v,
29573 +                              struct au_sbinfo *sbinfo)
29574 +{
29575 +       const char *pat;
29576 +       char fmt[32];
29577 +       struct au_wbr_mfs *mfs;
29578 +
29579 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29580 +
29581 +       seq_puts(m, ",create=");
29582 +       pat = au_optstr_wbr_create(v);
29583 +       mfs = &sbinfo->si_wbr_mfs;
29584 +       switch (v) {
29585 +       case AuWbrCreate_TDP:
29586 +       case AuWbrCreate_RR:
29587 +       case AuWbrCreate_MFS:
29588 +       case AuWbrCreate_PMFS:
29589 +               seq_puts(m, pat);
29590 +               break;
29591 +       case AuWbrCreate_MFSRR:
29592 +       case AuWbrCreate_TDMFS:
29593 +       case AuWbrCreate_PMFSRR:
29594 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29595 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29596 +               break;
29597 +       case AuWbrCreate_MFSV:
29598 +       case AuWbrCreate_PMFSV:
29599 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29600 +               seq_printf(m, fmt,
29601 +                          jiffies_to_msecs(mfs->mfs_expire)
29602 +                          / MSEC_PER_SEC);
29603 +               break;
29604 +       case AuWbrCreate_MFSRRV:
29605 +       case AuWbrCreate_TDMFSV:
29606 +       case AuWbrCreate_PMFSRRV:
29607 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29608 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29609 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29610 +               break;
29611 +       default:
29612 +               BUG();
29613 +       }
29614 +}
29615 +
29616 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29617 +{
29618 +#ifdef CONFIG_SYSFS
29619 +       return 0;
29620 +#else
29621 +       int err;
29622 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29623 +       aufs_bindex_t bindex, brid;
29624 +       struct qstr *name;
29625 +       struct file *f;
29626 +       struct dentry *d, *h_root;
29627 +       struct au_branch *br;
29628 +
29629 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29630 +
29631 +       err = 0;
29632 +       f = au_sbi(sb)->si_xib;
29633 +       if (!f)
29634 +               goto out;
29635 +
29636 +       /* stop printing the default xino path on the first writable branch */
29637 +       h_root = NULL;
29638 +       bindex = au_xi_root(sb, f->f_path.dentry);
29639 +       if (bindex >= 0) {
29640 +               br = au_sbr_sb(sb, bindex);
29641 +               h_root = au_br_dentry(br);
29642 +       }
29643 +
29644 +       d = f->f_path.dentry;
29645 +       name = &d->d_name;
29646 +       /* safe ->d_parent because the file is unlinked */
29647 +       if (d->d_parent == h_root
29648 +           && name->len == len
29649 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29650 +               goto out;
29651 +
29652 +       seq_puts(seq, ",xino=");
29653 +       err = au_xino_path(seq, f);
29654 +
29655 +out:
29656 +       return err;
29657 +#endif
29658 +}
29659 +
29660 +/* seq_file will re-call me in case of too long string */
29661 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29662 +{
29663 +       int err;
29664 +       unsigned int mnt_flags, v;
29665 +       struct super_block *sb;
29666 +       struct au_sbinfo *sbinfo;
29667 +
29668 +#define AuBool(name, str) do { \
29669 +       v = au_opt_test(mnt_flags, name); \
29670 +       if (v != au_opt_test(AuOpt_Def, name)) \
29671 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29672 +} while (0)
29673 +
29674 +#define AuStr(name, str) do { \
29675 +       v = mnt_flags & AuOptMask_##name; \
29676 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29677 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29678 +} while (0)
29679 +
29680 +#define AuUInt(name, str, val) do { \
29681 +       if (val != AUFS_##name##_DEF) \
29682 +               seq_printf(m, "," #str "=%u", val); \
29683 +} while (0)
29684 +
29685 +       sb = dentry->d_sb;
29686 +       if (sb->s_flags & SB_POSIXACL)
29687 +               seq_puts(m, ",acl");
29688 +#if 0 /* reserved for future use */
29689 +       if (sb->s_flags & SB_I_VERSION)
29690 +               seq_puts(m, ",i_version");
29691 +#endif
29692 +
29693 +       /* lock free root dinfo */
29694 +       si_noflush_read_lock(sb);
29695 +       sbinfo = au_sbi(sb);
29696 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29697 +
29698 +       mnt_flags = au_mntflags(sb);
29699 +       if (au_opt_test(mnt_flags, XINO)) {
29700 +               err = au_show_xino(m, sb);
29701 +               if (unlikely(err))
29702 +                       goto out;
29703 +       } else
29704 +               seq_puts(m, ",noxino");
29705 +
29706 +       AuBool(TRUNC_XINO, trunc_xino);
29707 +       AuStr(UDBA, udba);
29708 +       AuBool(SHWH, shwh);
29709 +       AuBool(PLINK, plink);
29710 +       AuBool(DIO, dio);
29711 +       AuBool(DIRPERM1, dirperm1);
29712 +
29713 +       v = sbinfo->si_wbr_create;
29714 +       if (v != AuWbrCreate_Def)
29715 +               au_show_wbr_create(m, v, sbinfo);
29716 +
29717 +       v = sbinfo->si_wbr_copyup;
29718 +       if (v != AuWbrCopyup_Def)
29719 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29720 +
29721 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29722 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29723 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29724 +
29725 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29726 +
29727 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29728 +       AuUInt(RDCACHE, rdcache, v);
29729 +
29730 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29731 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29732 +
29733 +       au_fhsm_show(m, sbinfo);
29734 +
29735 +       AuBool(DIRREN, dirren);
29736 +       AuBool(SUM, sum);
29737 +       /* AuBool(SUM_W, wsum); */
29738 +       AuBool(WARN_PERM, warn_perm);
29739 +       AuBool(VERBOSE, verbose);
29740 +
29741 +out:
29742 +       /* be sure to print "br:" last */
29743 +       if (!sysaufs_brs) {
29744 +               seq_puts(m, ",br:");
29745 +               au_show_brs(m, sb);
29746 +       }
29747 +       si_read_unlock(sb);
29748 +       return 0;
29749 +
29750 +#undef AuBool
29751 +#undef AuStr
29752 +#undef AuUInt
29753 +}
29754 +
29755 +/* ---------------------------------------------------------------------- */
29756 +
29757 +/* sum mode which returns the summation for statfs(2) */
29758 +
29759 +static u64 au_add_till_max(u64 a, u64 b)
29760 +{
29761 +       u64 old;
29762 +
29763 +       old = a;
29764 +       a += b;
29765 +       if (old <= a)
29766 +               return a;
29767 +       return ULLONG_MAX;
29768 +}
29769 +
29770 +static u64 au_mul_till_max(u64 a, long mul)
29771 +{
29772 +       u64 old;
29773 +
29774 +       old = a;
29775 +       a *= mul;
29776 +       if (old <= a)
29777 +               return a;
29778 +       return ULLONG_MAX;
29779 +}
29780 +
29781 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29782 +{
29783 +       int err;
29784 +       long bsize, factor;
29785 +       u64 blocks, bfree, bavail, files, ffree;
29786 +       aufs_bindex_t bbot, bindex, i;
29787 +       unsigned char shared;
29788 +       struct path h_path;
29789 +       struct super_block *h_sb;
29790 +
29791 +       err = 0;
29792 +       bsize = LONG_MAX;
29793 +       files = 0;
29794 +       ffree = 0;
29795 +       blocks = 0;
29796 +       bfree = 0;
29797 +       bavail = 0;
29798 +       bbot = au_sbbot(sb);
29799 +       for (bindex = 0; bindex <= bbot; bindex++) {
29800 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29801 +               h_sb = h_path.mnt->mnt_sb;
29802 +               shared = 0;
29803 +               for (i = 0; !shared && i < bindex; i++)
29804 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29805 +               if (shared)
29806 +                       continue;
29807 +
29808 +               /* sb->s_root for NFS is unreliable */
29809 +               h_path.dentry = h_path.mnt->mnt_root;
29810 +               err = vfs_statfs(&h_path, buf);
29811 +               if (unlikely(err))
29812 +                       goto out;
29813 +
29814 +               if (bsize > buf->f_bsize) {
29815 +                       /*
29816 +                        * we will reduce bsize, so we have to expand blocks
29817 +                        * etc. to match them again
29818 +                        */
29819 +                       factor = (bsize / buf->f_bsize);
29820 +                       blocks = au_mul_till_max(blocks, factor);
29821 +                       bfree = au_mul_till_max(bfree, factor);
29822 +                       bavail = au_mul_till_max(bavail, factor);
29823 +                       bsize = buf->f_bsize;
29824 +               }
29825 +
29826 +               factor = (buf->f_bsize / bsize);
29827 +               blocks = au_add_till_max(blocks,
29828 +                               au_mul_till_max(buf->f_blocks, factor));
29829 +               bfree = au_add_till_max(bfree,
29830 +                               au_mul_till_max(buf->f_bfree, factor));
29831 +               bavail = au_add_till_max(bavail,
29832 +                               au_mul_till_max(buf->f_bavail, factor));
29833 +               files = au_add_till_max(files, buf->f_files);
29834 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29835 +       }
29836 +
29837 +       buf->f_bsize = bsize;
29838 +       buf->f_blocks = blocks;
29839 +       buf->f_bfree = bfree;
29840 +       buf->f_bavail = bavail;
29841 +       buf->f_files = files;
29842 +       buf->f_ffree = ffree;
29843 +       buf->f_frsize = 0;
29844 +
29845 +out:
29846 +       return err;
29847 +}
29848 +
29849 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29850 +{
29851 +       int err;
29852 +       struct path h_path;
29853 +       struct super_block *sb;
29854 +
29855 +       /* lock free root dinfo */
29856 +       sb = dentry->d_sb;
29857 +       si_noflush_read_lock(sb);
29858 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29859 +               /* sb->s_root for NFS is unreliable */
29860 +               h_path.mnt = au_sbr_mnt(sb, 0);
29861 +               h_path.dentry = h_path.mnt->mnt_root;
29862 +               err = vfs_statfs(&h_path, buf);
29863 +       } else
29864 +               err = au_statfs_sum(sb, buf);
29865 +       si_read_unlock(sb);
29866 +
29867 +       if (!err) {
29868 +               buf->f_type = AUFS_SUPER_MAGIC;
29869 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29870 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29871 +       }
29872 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29873 +
29874 +       return err;
29875 +}
29876 +
29877 +/* ---------------------------------------------------------------------- */
29878 +
29879 +static int aufs_sync_fs(struct super_block *sb, int wait)
29880 +{
29881 +       int err, e;
29882 +       aufs_bindex_t bbot, bindex;
29883 +       struct au_branch *br;
29884 +       struct super_block *h_sb;
29885 +
29886 +       err = 0;
29887 +       si_noflush_read_lock(sb);
29888 +       bbot = au_sbbot(sb);
29889 +       for (bindex = 0; bindex <= bbot; bindex++) {
29890 +               br = au_sbr(sb, bindex);
29891 +               if (!au_br_writable(br->br_perm))
29892 +                       continue;
29893 +
29894 +               h_sb = au_sbr_sb(sb, bindex);
29895 +               e = vfsub_sync_filesystem(h_sb, wait);
29896 +               if (unlikely(e && !err))
29897 +                       err = e;
29898 +               /* go on even if an error happens */
29899 +       }
29900 +       si_read_unlock(sb);
29901 +
29902 +       return err;
29903 +}
29904 +
29905 +/* ---------------------------------------------------------------------- */
29906 +
29907 +/* final actions when unmounting a file system */
29908 +static void aufs_put_super(struct super_block *sb)
29909 +{
29910 +       struct au_sbinfo *sbinfo;
29911 +
29912 +       sbinfo = au_sbi(sb);
29913 +       if (sbinfo)
29914 +               kobject_put(&sbinfo->si_kobj);
29915 +}
29916 +
29917 +/* ---------------------------------------------------------------------- */
29918 +
29919 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29920 +                    struct super_block *sb, void *arg)
29921 +{
29922 +       void *array;
29923 +       unsigned long long n, sz;
29924 +
29925 +       array = NULL;
29926 +       n = 0;
29927 +       if (!*hint)
29928 +               goto out;
29929 +
29930 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29931 +               array = ERR_PTR(-EMFILE);
29932 +               pr_err("hint %llu\n", *hint);
29933 +               goto out;
29934 +       }
29935 +
29936 +       sz = sizeof(array) * *hint;
29937 +       array = kzalloc(sz, GFP_NOFS);
29938 +       if (unlikely(!array))
29939 +               array = vzalloc(sz);
29940 +       if (unlikely(!array)) {
29941 +               array = ERR_PTR(-ENOMEM);
29942 +               goto out;
29943 +       }
29944 +
29945 +       n = cb(sb, array, *hint, arg);
29946 +       AuDebugOn(n > *hint);
29947 +
29948 +out:
29949 +       *hint = n;
29950 +       return array;
29951 +}
29952 +
29953 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29954 +                                      unsigned long long max __maybe_unused,
29955 +                                      void *arg)
29956 +{
29957 +       unsigned long long n;
29958 +       struct inode **p, *inode;
29959 +       struct list_head *head;
29960 +
29961 +       n = 0;
29962 +       p = a;
29963 +       head = arg;
29964 +       spin_lock(&sb->s_inode_list_lock);
29965 +       list_for_each_entry(inode, head, i_sb_list) {
29966 +               if (!au_is_bad_inode(inode)
29967 +                   && au_ii(inode)->ii_btop >= 0) {
29968 +                       spin_lock(&inode->i_lock);
29969 +                       if (atomic_read(&inode->i_count)) {
29970 +                               au_igrab(inode);
29971 +                               *p++ = inode;
29972 +                               n++;
29973 +                               AuDebugOn(n > max);
29974 +                       }
29975 +                       spin_unlock(&inode->i_lock);
29976 +               }
29977 +       }
29978 +       spin_unlock(&sb->s_inode_list_lock);
29979 +
29980 +       return n;
29981 +}
29982 +
29983 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29984 +{
29985 +       struct au_sbinfo *sbi;
29986 +
29987 +       sbi = au_sbi(sb);
29988 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29989 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29990 +}
29991 +
29992 +void au_iarray_free(struct inode **a, unsigned long long max)
29993 +{
29994 +       unsigned long long ull;
29995 +
29996 +       for (ull = 0; ull < max; ull++)
29997 +               iput(a[ull]);
29998 +       kvfree(a);
29999 +}
30000 +
30001 +/* ---------------------------------------------------------------------- */
30002 +
30003 +/*
30004 + * refresh dentry and inode at remount time.
30005 + */
30006 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30007 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30008 +                     struct dentry *parent)
30009 +{
30010 +       int err;
30011 +
30012 +       di_write_lock_child(dentry);
30013 +       di_read_lock_parent(parent, AuLock_IR);
30014 +       err = au_refresh_dentry(dentry, parent);
30015 +       if (!err && dir_flags)
30016 +               au_hn_reset(d_inode(dentry), dir_flags);
30017 +       di_read_unlock(parent, AuLock_IR);
30018 +       di_write_unlock(dentry);
30019 +
30020 +       return err;
30021 +}
30022 +
30023 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30024 +                          struct au_sbinfo *sbinfo,
30025 +                          const unsigned int dir_flags, unsigned int do_idop)
30026 +{
30027 +       int err;
30028 +       struct dentry *parent;
30029 +
30030 +       err = 0;
30031 +       parent = dget_parent(dentry);
30032 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30033 +               if (d_really_is_positive(dentry)) {
30034 +                       if (!d_is_dir(dentry))
30035 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30036 +                                                parent);
30037 +                       else {
30038 +                               err = au_do_refresh(dentry, dir_flags, parent);
30039 +                               if (unlikely(err))
30040 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30041 +                       }
30042 +               } else
30043 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30044 +               AuDbgDentry(dentry);
30045 +       }
30046 +       dput(parent);
30047 +
30048 +       if (!err) {
30049 +               if (do_idop)
30050 +                       au_refresh_dop(dentry, /*force_reval*/0);
30051 +       } else
30052 +               au_refresh_dop(dentry, /*force_reval*/1);
30053 +
30054 +       AuTraceErr(err);
30055 +       return err;
30056 +}
30057 +
30058 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30059 +{
30060 +       int err, i, j, ndentry, e;
30061 +       unsigned int sigen;
30062 +       struct au_dcsub_pages dpages;
30063 +       struct au_dpage *dpage;
30064 +       struct dentry **dentries, *d;
30065 +       struct au_sbinfo *sbinfo;
30066 +       struct dentry *root = sb->s_root;
30067 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30068 +
30069 +       if (do_idop)
30070 +               au_refresh_dop(root, /*force_reval*/0);
30071 +
30072 +       err = au_dpages_init(&dpages, GFP_NOFS);
30073 +       if (unlikely(err))
30074 +               goto out;
30075 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30076 +       if (unlikely(err))
30077 +               goto out_dpages;
30078 +
30079 +       sigen = au_sigen(sb);
30080 +       sbinfo = au_sbi(sb);
30081 +       for (i = 0; i < dpages.ndpage; i++) {
30082 +               dpage = dpages.dpages + i;
30083 +               dentries = dpage->dentries;
30084 +               ndentry = dpage->ndentry;
30085 +               for (j = 0; j < ndentry; j++) {
30086 +                       d = dentries[j];
30087 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30088 +                                           do_idop);
30089 +                       if (unlikely(e && !err))
30090 +                               err = e;
30091 +                       /* go on even err */
30092 +               }
30093 +       }
30094 +
30095 +out_dpages:
30096 +       au_dpages_free(&dpages);
30097 +out:
30098 +       return err;
30099 +}
30100 +
30101 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30102 +{
30103 +       int err, e;
30104 +       unsigned int sigen;
30105 +       unsigned long long max, ull;
30106 +       struct inode *inode, **array;
30107 +
30108 +       array = au_iarray_alloc(sb, &max);
30109 +       err = PTR_ERR(array);
30110 +       if (IS_ERR(array))
30111 +               goto out;
30112 +
30113 +       err = 0;
30114 +       sigen = au_sigen(sb);
30115 +       for (ull = 0; ull < max; ull++) {
30116 +               inode = array[ull];
30117 +               if (unlikely(!inode))
30118 +                       break;
30119 +
30120 +               e = 0;
30121 +               ii_write_lock_child(inode);
30122 +               if (au_iigen(inode, NULL) != sigen) {
30123 +                       e = au_refresh_hinode_self(inode);
30124 +                       if (unlikely(e)) {
30125 +                               au_refresh_iop(inode, /*force_getattr*/1);
30126 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30127 +                               if (!err)
30128 +                                       err = e;
30129 +                               /* go on even if err */
30130 +                       }
30131 +               }
30132 +               if (!e && do_idop)
30133 +                       au_refresh_iop(inode, /*force_getattr*/0);
30134 +               ii_write_unlock(inode);
30135 +       }
30136 +
30137 +       au_iarray_free(array, max);
30138 +
30139 +out:
30140 +       return err;
30141 +}
30142 +
30143 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30144 +{
30145 +       int err, e;
30146 +       unsigned int udba;
30147 +       aufs_bindex_t bindex, bbot;
30148 +       struct dentry *root;
30149 +       struct inode *inode;
30150 +       struct au_branch *br;
30151 +       struct au_sbinfo *sbi;
30152 +
30153 +       au_sigen_inc(sb);
30154 +       sbi = au_sbi(sb);
30155 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30156 +
30157 +       root = sb->s_root;
30158 +       DiMustNoWaiters(root);
30159 +       inode = d_inode(root);
30160 +       IiMustNoWaiters(inode);
30161 +
30162 +       udba = au_opt_udba(sb);
30163 +       bbot = au_sbbot(sb);
30164 +       for (bindex = 0; bindex <= bbot; bindex++) {
30165 +               br = au_sbr(sb, bindex);
30166 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30167 +               if (unlikely(err))
30168 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30169 +                               bindex, err);
30170 +               /* go on even if err */
30171 +       }
30172 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30173 +
30174 +       if (do_idop) {
30175 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30176 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30177 +                       sb->s_d_op = &aufs_dop_noreval;
30178 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30179 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30180 +               } else {
30181 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30182 +                       sb->s_d_op = &aufs_dop;
30183 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30184 +                       sbi->si_iop_array = aufs_iop;
30185 +               }
30186 +               pr_info("reset to %ps and %ps\n",
30187 +                       sb->s_d_op, sbi->si_iop_array);
30188 +       }
30189 +
30190 +       di_write_unlock(root);
30191 +       err = au_refresh_d(sb, do_idop);
30192 +       e = au_refresh_i(sb, do_idop);
30193 +       if (unlikely(e && !err))
30194 +               err = e;
30195 +       /* aufs_write_lock() calls ..._child() */
30196 +       di_write_lock_child(root);
30197 +
30198 +       au_cpup_attr_all(inode, /*force*/1);
30199 +
30200 +       if (unlikely(err))
30201 +               AuIOErr("refresh failed, ignored, %d\n", err);
30202 +}
30203 +
30204 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30205 +static int cvt_err(int err)
30206 +{
30207 +       AuTraceErr(err);
30208 +
30209 +       switch (err) {
30210 +       case -ENOENT:
30211 +       case -ENOTDIR:
30212 +       case -EEXIST:
30213 +       case -EIO:
30214 +               err = -EINVAL;
30215 +       }
30216 +       return err;
30217 +}
30218 +
30219 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30220 +{
30221 +       int err, do_dx;
30222 +       unsigned int mntflags;
30223 +       struct au_opts opts = {
30224 +               .opt = NULL
30225 +       };
30226 +       struct dentry *root;
30227 +       struct inode *inode;
30228 +       struct au_sbinfo *sbinfo;
30229 +
30230 +       err = 0;
30231 +       root = sb->s_root;
30232 +       if (!data || !*data) {
30233 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30234 +               if (!err) {
30235 +                       di_write_lock_child(root);
30236 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30237 +                       aufs_write_unlock(root);
30238 +               }
30239 +               goto out;
30240 +       }
30241 +
30242 +       err = -ENOMEM;
30243 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30244 +       if (unlikely(!opts.opt))
30245 +               goto out;
30246 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30247 +       opts.flags = AuOpts_REMOUNT;
30248 +       opts.sb_flags = *flags;
30249 +
30250 +       /* parse it before aufs lock */
30251 +       err = au_opts_parse(sb, data, &opts);
30252 +       if (unlikely(err))
30253 +               goto out_opts;
30254 +
30255 +       sbinfo = au_sbi(sb);
30256 +       inode = d_inode(root);
30257 +       inode_lock(inode);
30258 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30259 +       if (unlikely(err))
30260 +               goto out_mtx;
30261 +       di_write_lock_child(root);
30262 +
30263 +       /* au_opts_remount() may return an error */
30264 +       err = au_opts_remount(sb, &opts);
30265 +       au_opts_free(&opts);
30266 +
30267 +       if (au_ftest_opts(opts.flags, REFRESH))
30268 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30269 +
30270 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30271 +               mntflags = au_mntflags(sb);
30272 +               do_dx = !!au_opt_test(mntflags, DIO);
30273 +               au_dy_arefresh(do_dx);
30274 +       }
30275 +
30276 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30277 +       aufs_write_unlock(root);
30278 +
30279 +out_mtx:
30280 +       inode_unlock(inode);
30281 +out_opts:
30282 +       free_page((unsigned long)opts.opt);
30283 +out:
30284 +       err = cvt_err(err);
30285 +       AuTraceErr(err);
30286 +       return err;
30287 +}
30288 +
30289 +static const struct super_operations aufs_sop = {
30290 +       .alloc_inode    = aufs_alloc_inode,
30291 +       .destroy_inode  = aufs_destroy_inode,
30292 +       .free_inode     = aufs_free_inode,
30293 +       /* always deleting, no clearing */
30294 +       .drop_inode     = generic_delete_inode,
30295 +       .show_options   = aufs_show_options,
30296 +       .statfs         = aufs_statfs,
30297 +       .put_super      = aufs_put_super,
30298 +       .sync_fs        = aufs_sync_fs,
30299 +       .remount_fs     = aufs_remount_fs
30300 +};
30301 +
30302 +/* ---------------------------------------------------------------------- */
30303 +
30304 +static int alloc_root(struct super_block *sb)
30305 +{
30306 +       int err;
30307 +       struct inode *inode;
30308 +       struct dentry *root;
30309 +
30310 +       err = -ENOMEM;
30311 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30312 +       err = PTR_ERR(inode);
30313 +       if (IS_ERR(inode))
30314 +               goto out;
30315 +
30316 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30317 +       inode->i_fop = &aufs_dir_fop;
30318 +       inode->i_mode = S_IFDIR;
30319 +       set_nlink(inode, 2);
30320 +       unlock_new_inode(inode);
30321 +
30322 +       root = d_make_root(inode);
30323 +       if (unlikely(!root))
30324 +               goto out;
30325 +       err = PTR_ERR(root);
30326 +       if (IS_ERR(root))
30327 +               goto out;
30328 +
30329 +       err = au_di_init(root);
30330 +       if (!err) {
30331 +               sb->s_root = root;
30332 +               return 0; /* success */
30333 +       }
30334 +       dput(root);
30335 +
30336 +out:
30337 +       return err;
30338 +}
30339 +
30340 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30341 +                          int silent __maybe_unused)
30342 +{
30343 +       int err;
30344 +       struct au_opts opts = {
30345 +               .opt = NULL
30346 +       };
30347 +       struct au_sbinfo *sbinfo;
30348 +       struct dentry *root;
30349 +       struct inode *inode;
30350 +       char *arg = raw_data;
30351 +
30352 +       if (unlikely(!arg || !*arg)) {
30353 +               err = -EINVAL;
30354 +               pr_err("no arg\n");
30355 +               goto out;
30356 +       }
30357 +
30358 +       err = -ENOMEM;
30359 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30360 +       if (unlikely(!opts.opt))
30361 +               goto out;
30362 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30363 +       opts.sb_flags = sb->s_flags;
30364 +
30365 +       err = au_si_alloc(sb);
30366 +       if (unlikely(err))
30367 +               goto out_opts;
30368 +       sbinfo = au_sbi(sb);
30369 +
30370 +       /* all timestamps always follow the ones on the branch */
30371 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30372 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30373 +       sb->s_op = &aufs_sop;
30374 +       sb->s_d_op = &aufs_dop;
30375 +       sb->s_magic = AUFS_SUPER_MAGIC;
30376 +       sb->s_maxbytes = 0;
30377 +       sb->s_stack_depth = 1;
30378 +       au_export_init(sb);
30379 +       au_xattr_init(sb);
30380 +
30381 +       err = alloc_root(sb);
30382 +       if (unlikely(err)) {
30383 +               si_write_unlock(sb);
30384 +               goto out_info;
30385 +       }
30386 +       root = sb->s_root;
30387 +       inode = d_inode(root);
30388 +
30389 +       /*
30390 +        * actually we can parse options regardless aufs lock here.
30391 +        * but at remount time, parsing must be done before aufs lock.
30392 +        * so we follow the same rule.
30393 +        */
30394 +       ii_write_lock_parent(inode);
30395 +       aufs_write_unlock(root);
30396 +       err = au_opts_parse(sb, arg, &opts);
30397 +       if (unlikely(err))
30398 +               goto out_root;
30399 +
30400 +       /* lock vfs_inode first, then aufs. */
30401 +       inode_lock(inode);
30402 +       aufs_write_lock(root);
30403 +       err = au_opts_mount(sb, &opts);
30404 +       au_opts_free(&opts);
30405 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30406 +               sb->s_d_op = &aufs_dop_noreval;
30407 +               pr_info("%ps\n", sb->s_d_op);
30408 +               au_refresh_dop(root, /*force_reval*/0);
30409 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30410 +               au_refresh_iop(inode, /*force_getattr*/0);
30411 +       }
30412 +       aufs_write_unlock(root);
30413 +       inode_unlock(inode);
30414 +       if (!err)
30415 +               goto out_opts; /* success */
30416 +
30417 +out_root:
30418 +       dput(root);
30419 +       sb->s_root = NULL;
30420 +out_info:
30421 +       kobject_put(&sbinfo->si_kobj);
30422 +       sb->s_fs_info = NULL;
30423 +out_opts:
30424 +       free_page((unsigned long)opts.opt);
30425 +out:
30426 +       AuTraceErr(err);
30427 +       err = cvt_err(err);
30428 +       AuTraceErr(err);
30429 +       return err;
30430 +}
30431 +
30432 +/* ---------------------------------------------------------------------- */
30433 +
30434 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30435 +                                const char *dev_name __maybe_unused,
30436 +                                void *raw_data)
30437 +{
30438 +       struct dentry *root;
30439 +
30440 +       /* all timestamps always follow the ones on the branch */
30441 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30442 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30443 +       if (IS_ERR(root))
30444 +               goto out;
30445 +
30446 +       au_sbilist_add(root->d_sb);
30447 +
30448 +out:
30449 +       return root;
30450 +}
30451 +
30452 +static void aufs_kill_sb(struct super_block *sb)
30453 +{
30454 +       struct au_sbinfo *sbinfo;
30455 +
30456 +       sbinfo = au_sbi(sb);
30457 +       if (sbinfo) {
30458 +               au_sbilist_del(sb);
30459 +               aufs_write_lock(sb->s_root);
30460 +               au_fhsm_fin(sb);
30461 +               if (sbinfo->si_wbr_create_ops->fin)
30462 +                       sbinfo->si_wbr_create_ops->fin(sb);
30463 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30464 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30465 +                       au_remount_refresh(sb, /*do_idop*/0);
30466 +               }
30467 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30468 +                       au_plink_put(sb, /*verbose*/1);
30469 +               au_xino_clr(sb);
30470 +               au_dr_opt_flush(sb);
30471 +               sbinfo->si_sb = NULL;
30472 +               aufs_write_unlock(sb->s_root);
30473 +               au_nwt_flush(&sbinfo->si_nowait);
30474 +       }
30475 +       kill_anon_super(sb);
30476 +}
30477 +
30478 +struct file_system_type aufs_fs_type = {
30479 +       .name           = AUFS_FSTYPE,
30480 +       /* a race between rename and others */
30481 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30482 +       .mount          = aufs_mount,
30483 +       .kill_sb        = aufs_kill_sb,
30484 +       /* no need to __module_get() and module_put(). */
30485 +       .owner          = THIS_MODULE,
30486 +};
30487 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30488 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30489 +++ linux/fs/aufs/super.h       2020-01-27 10:57:18.178871751 +0100
30490 @@ -0,0 +1,589 @@
30491 +/* SPDX-License-Identifier: GPL-2.0 */
30492 +/*
30493 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30494 + *
30495 + * This program, aufs is free software; you can redistribute it and/or modify
30496 + * it under the terms of the GNU General Public License as published by
30497 + * the Free Software Foundation; either version 2 of the License, or
30498 + * (at your option) any later version.
30499 + *
30500 + * This program is distributed in the hope that it will be useful,
30501 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30502 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30503 + * GNU General Public License for more details.
30504 + *
30505 + * You should have received a copy of the GNU General Public License
30506 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30507 + */
30508 +
30509 +/*
30510 + * super_block operations
30511 + */
30512 +
30513 +#ifndef __AUFS_SUPER_H__
30514 +#define __AUFS_SUPER_H__
30515 +
30516 +#ifdef __KERNEL__
30517 +
30518 +#include <linux/fs.h>
30519 +#include <linux/kobject.h>
30520 +#include "hbl.h"
30521 +#include "lcnt.h"
30522 +#include "rwsem.h"
30523 +#include "wkq.h"
30524 +
30525 +/* policies to select one among multiple writable branches */
30526 +struct au_wbr_copyup_operations {
30527 +       int (*copyup)(struct dentry *dentry);
30528 +};
30529 +
30530 +#define AuWbr_DIR      1               /* target is a dir */
30531 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30532 +
30533 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30534 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30535 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30536 +
30537 +struct au_wbr_create_operations {
30538 +       int (*create)(struct dentry *dentry, unsigned int flags);
30539 +       int (*init)(struct super_block *sb);
30540 +       int (*fin)(struct super_block *sb);
30541 +};
30542 +
30543 +struct au_wbr_mfs {
30544 +       struct mutex    mfs_lock; /* protect this structure */
30545 +       unsigned long   mfs_jiffy;
30546 +       unsigned long   mfs_expire;
30547 +       aufs_bindex_t   mfs_bindex;
30548 +
30549 +       unsigned long long      mfsrr_bytes;
30550 +       unsigned long long      mfsrr_watermark;
30551 +};
30552 +
30553 +#define AuPlink_NHASH 100
30554 +static inline int au_plink_hash(ino_t ino)
30555 +{
30556 +       return ino % AuPlink_NHASH;
30557 +}
30558 +
30559 +/* File-based Hierarchical Storage Management */
30560 +struct au_fhsm {
30561 +#ifdef CONFIG_AUFS_FHSM
30562 +       /* allow only one process who can receive the notification */
30563 +       spinlock_t              fhsm_spin;
30564 +       pid_t                   fhsm_pid;
30565 +       wait_queue_head_t       fhsm_wqh;
30566 +       atomic_t                fhsm_readable;
30567 +
30568 +       /* these are protected by si_rwsem */
30569 +       unsigned long           fhsm_expire;
30570 +       aufs_bindex_t           fhsm_bottom;
30571 +#endif
30572 +};
30573 +
30574 +struct au_branch;
30575 +struct au_sbinfo {
30576 +       /* nowait tasks in the system-wide workqueue */
30577 +       struct au_nowait_tasks  si_nowait;
30578 +
30579 +       /*
30580 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30581 +        * rwsem for au_sbinfo is necessary.
30582 +        */
30583 +       struct au_rwsem         si_rwsem;
30584 +
30585 +       /*
30586 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30587 +        * remount.
30588 +        */
30589 +       au_lcnt_t               si_ninodes, si_nfiles;
30590 +
30591 +       /* branch management */
30592 +       unsigned int            si_generation;
30593 +
30594 +       /* see AuSi_ flags */
30595 +       unsigned char           au_si_status;
30596 +
30597 +       aufs_bindex_t           si_bbot;
30598 +
30599 +       /* dirty trick to keep br_id plus */
30600 +       unsigned int            si_last_br_id :
30601 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30602 +       struct au_branch        **si_branch;
30603 +
30604 +       /* policy to select a writable branch */
30605 +       unsigned char           si_wbr_copyup;
30606 +       unsigned char           si_wbr_create;
30607 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30608 +       struct au_wbr_create_operations *si_wbr_create_ops;
30609 +
30610 +       /* round robin */
30611 +       atomic_t                si_wbr_rr_next;
30612 +
30613 +       /* most free space */
30614 +       struct au_wbr_mfs       si_wbr_mfs;
30615 +
30616 +       /* File-based Hierarchical Storage Management */
30617 +       struct au_fhsm          si_fhsm;
30618 +
30619 +       /* mount flags */
30620 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30621 +       unsigned int            si_mntflags;
30622 +
30623 +       /* external inode number (bitmap and translation table) */
30624 +       vfs_readf_t             si_xread;
30625 +       vfs_writef_t            si_xwrite;
30626 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30627 +
30628 +       struct file             *si_xib;
30629 +       struct mutex            si_xib_mtx; /* protect xib members */
30630 +       unsigned long           *si_xib_buf;
30631 +       unsigned long           si_xib_last_pindex;
30632 +       int                     si_xib_next_bit;
30633 +
30634 +       unsigned long           si_xino_jiffy;
30635 +       unsigned long           si_xino_expire;
30636 +       /* reserved for future use */
30637 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30638 +
30639 +#ifdef CONFIG_AUFS_EXPORT
30640 +       /* i_generation */
30641 +       /* todo: make xigen file an array to support many inode numbers */
30642 +       struct file             *si_xigen;
30643 +       atomic_t                si_xigen_next;
30644 +#endif
30645 +
30646 +       /* dirty trick to support atomic_open */
30647 +       struct hlist_bl_head    si_aopen;
30648 +
30649 +       /* vdir parameters */
30650 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30651 +       unsigned int            si_rdblk;       /* deblk size */
30652 +       unsigned int            si_rdhash;      /* hash size */
30653 +
30654 +       /*
30655 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30656 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30657 +        * future fsck.aufs or kernel thread will remove them later.
30658 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30659 +        */
30660 +       unsigned int            si_dirwh;
30661 +
30662 +       /* pseudo_link list */
30663 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30664 +       wait_queue_head_t       si_plink_wq;
30665 +       spinlock_t              si_plink_maint_lock;
30666 +       pid_t                   si_plink_maint_pid;
30667 +
30668 +       /* file list */
30669 +       struct hlist_bl_head    si_files;
30670 +
30671 +       /* with/without getattr, brother of sb->s_d_op */
30672 +       const struct inode_operations *si_iop_array;
30673 +
30674 +       /*
30675 +        * sysfs and lifetime management.
30676 +        * this is not a small structure and it may be a waste of memory in case
30677 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30678 +        * but using sysfs is majority.
30679 +        */
30680 +       struct kobject          si_kobj;
30681 +#ifdef CONFIG_DEBUG_FS
30682 +       struct dentry            *si_dbgaufs;
30683 +       struct dentry            *si_dbgaufs_plink;
30684 +       struct dentry            *si_dbgaufs_xib;
30685 +#ifdef CONFIG_AUFS_EXPORT
30686 +       struct dentry            *si_dbgaufs_xigen;
30687 +#endif
30688 +#endif
30689 +
30690 +#ifdef CONFIG_AUFS_SBILIST
30691 +       struct hlist_bl_node    si_list;
30692 +#endif
30693 +
30694 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30695 +       struct super_block      *si_sb;
30696 +};
30697 +
30698 +/* sbinfo status flags */
30699 +/*
30700 + * set true when refresh_dirs() failed at remount time.
30701 + * then try refreshing dirs at access time again.
30702 + * if it is false, refreshing dirs at access time is unnecessary
30703 + */
30704 +#define AuSi_FAILED_REFRESH_DIR        1
30705 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30706 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30707 +
30708 +#ifndef CONFIG_AUFS_FHSM
30709 +#undef AuSi_FHSM
30710 +#define AuSi_FHSM              0
30711 +#endif
30712 +
30713 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30714 +                                          unsigned int flag)
30715 +{
30716 +       AuRwMustAnyLock(&sbi->si_rwsem);
30717 +       return sbi->au_si_status & flag;
30718 +}
30719 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30720 +#define au_fset_si(sbinfo, name) do { \
30721 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30722 +       (sbinfo)->au_si_status |= AuSi_##name; \
30723 +} while (0)
30724 +#define au_fclr_si(sbinfo, name) do { \
30725 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30726 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30727 +} while (0)
30728 +
30729 +/* ---------------------------------------------------------------------- */
30730 +
30731 +/* policy to select one among writable branches */
30732 +#define AuWbrCopyup(sbinfo, ...) \
30733 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30734 +#define AuWbrCreate(sbinfo, ...) \
30735 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30736 +
30737 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30738 +#define AuLock_DW              1               /* write-lock dentry */
30739 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30740 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30741 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30742 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30743 +                                               /* except RENAME_EXCHANGE */
30744 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30745 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30746 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30747 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30748 +#define au_fset_lock(flags, name) \
30749 +       do { (flags) |= AuLock_##name; } while (0)
30750 +#define au_fclr_lock(flags, name) \
30751 +       do { (flags) &= ~AuLock_##name; } while (0)
30752 +
30753 +/* ---------------------------------------------------------------------- */
30754 +
30755 +/* super.c */
30756 +extern struct file_system_type aufs_fs_type;
30757 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30758 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30759 +                                          unsigned long long max, void *arg);
30760 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30761 +                    struct super_block *sb, void *arg);
30762 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30763 +void au_iarray_free(struct inode **a, unsigned long long max);
30764 +
30765 +/* sbinfo.c */
30766 +void au_si_free(struct kobject *kobj);
30767 +int au_si_alloc(struct super_block *sb);
30768 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30769 +
30770 +unsigned int au_sigen_inc(struct super_block *sb);
30771 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30772 +
30773 +int si_read_lock(struct super_block *sb, int flags);
30774 +int si_write_lock(struct super_block *sb, int flags);
30775 +int aufs_read_lock(struct dentry *dentry, int flags);
30776 +void aufs_read_unlock(struct dentry *dentry, int flags);
30777 +void aufs_write_lock(struct dentry *dentry);
30778 +void aufs_write_unlock(struct dentry *dentry);
30779 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30780 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30781 +
30782 +/* wbr_policy.c */
30783 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30784 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30785 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30786 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30787 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30788 +
30789 +/* mvdown.c */
30790 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30791 +
30792 +#ifdef CONFIG_AUFS_FHSM
30793 +/* fhsm.c */
30794 +
30795 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30796 +{
30797 +       pid_t pid;
30798 +
30799 +       spin_lock(&fhsm->fhsm_spin);
30800 +       pid = fhsm->fhsm_pid;
30801 +       spin_unlock(&fhsm->fhsm_spin);
30802 +
30803 +       return pid;
30804 +}
30805 +
30806 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30807 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30808 +int au_fhsm_fd(struct super_block *sb, int oflags);
30809 +int au_fhsm_br_alloc(struct au_branch *br);
30810 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30811 +void au_fhsm_fin(struct super_block *sb);
30812 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30813 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30814 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30815 +#else
30816 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30817 +          int force)
30818 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30819 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30820 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30821 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30822 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30823 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30824 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30825 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30826 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30827 +#endif
30828 +
30829 +/* ---------------------------------------------------------------------- */
30830 +
30831 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30832 +{
30833 +       return sb->s_fs_info;
30834 +}
30835 +
30836 +/* ---------------------------------------------------------------------- */
30837 +
30838 +#ifdef CONFIG_AUFS_EXPORT
30839 +int au_test_nfsd(void);
30840 +void au_export_init(struct super_block *sb);
30841 +void au_xigen_inc(struct inode *inode);
30842 +int au_xigen_new(struct inode *inode);
30843 +int au_xigen_set(struct super_block *sb, struct path *path);
30844 +void au_xigen_clr(struct super_block *sb);
30845 +
30846 +static inline int au_busy_or_stale(void)
30847 +{
30848 +       if (!au_test_nfsd())
30849 +               return -EBUSY;
30850 +       return -ESTALE;
30851 +}
30852 +#else
30853 +AuStubInt0(au_test_nfsd, void)
30854 +AuStubVoid(au_export_init, struct super_block *sb)
30855 +AuStubVoid(au_xigen_inc, struct inode *inode)
30856 +AuStubInt0(au_xigen_new, struct inode *inode)
30857 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30858 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30859 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30860 +#endif /* CONFIG_AUFS_EXPORT */
30861 +
30862 +/* ---------------------------------------------------------------------- */
30863 +
30864 +#ifdef CONFIG_AUFS_SBILIST
30865 +/* module.c */
30866 +extern struct hlist_bl_head au_sbilist;
30867 +
30868 +static inline void au_sbilist_init(void)
30869 +{
30870 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30871 +}
30872 +
30873 +static inline void au_sbilist_add(struct super_block *sb)
30874 +{
30875 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30876 +}
30877 +
30878 +static inline void au_sbilist_del(struct super_block *sb)
30879 +{
30880 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30881 +}
30882 +
30883 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30884 +static inline void au_sbilist_lock(void)
30885 +{
30886 +       hlist_bl_lock(&au_sbilist);
30887 +}
30888 +
30889 +static inline void au_sbilist_unlock(void)
30890 +{
30891 +       hlist_bl_unlock(&au_sbilist);
30892 +}
30893 +#define AuGFP_SBILIST  GFP_ATOMIC
30894 +#else
30895 +AuStubVoid(au_sbilist_lock, void)
30896 +AuStubVoid(au_sbilist_unlock, void)
30897 +#define AuGFP_SBILIST  GFP_NOFS
30898 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30899 +#else
30900 +AuStubVoid(au_sbilist_init, void)
30901 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30902 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30903 +AuStubVoid(au_sbilist_lock, void)
30904 +AuStubVoid(au_sbilist_unlock, void)
30905 +#define AuGFP_SBILIST  GFP_NOFS
30906 +#endif
30907 +
30908 +/* ---------------------------------------------------------------------- */
30909 +
30910 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30911 +{
30912 +       /*
30913 +        * This function is a dynamic '__init' function actually,
30914 +        * so the tiny check for si_rwsem is unnecessary.
30915 +        */
30916 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30917 +#ifdef CONFIG_DEBUG_FS
30918 +       sbinfo->si_dbgaufs = NULL;
30919 +       sbinfo->si_dbgaufs_plink = NULL;
30920 +       sbinfo->si_dbgaufs_xib = NULL;
30921 +#ifdef CONFIG_AUFS_EXPORT
30922 +       sbinfo->si_dbgaufs_xigen = NULL;
30923 +#endif
30924 +#endif
30925 +}
30926 +
30927 +/* ---------------------------------------------------------------------- */
30928 +
30929 +/* current->atomic_flags */
30930 +/* this value should never corrupt the ones defined in linux/sched.h */
30931 +#define PFA_AUFS       0x10
30932 +
30933 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30934 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30935 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30936 +
30937 +static inline int si_pid_test(struct super_block *sb)
30938 +{
30939 +       return !!task_test_aufs(current);
30940 +}
30941 +
30942 +static inline void si_pid_clr(struct super_block *sb)
30943 +{
30944 +       AuDebugOn(!task_test_aufs(current));
30945 +       task_clear_aufs(current);
30946 +}
30947 +
30948 +static inline void si_pid_set(struct super_block *sb)
30949 +{
30950 +       AuDebugOn(task_test_aufs(current));
30951 +       task_set_aufs(current);
30952 +}
30953 +
30954 +/* ---------------------------------------------------------------------- */
30955 +
30956 +/* lock superblock. mainly for entry point functions */
30957 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30958 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30959 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30960 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30961 +/*
30962 +#define __si_read_trylock_nested(sb) \
30963 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30964 +#define __si_write_trylock_nested(sb) \
30965 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30966 +*/
30967 +
30968 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30969 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30970 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30971 +
30972 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30973 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30974 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30975 +
30976 +static inline void si_noflush_read_lock(struct super_block *sb)
30977 +{
30978 +       __si_read_lock(sb);
30979 +       si_pid_set(sb);
30980 +}
30981 +
30982 +static inline int si_noflush_read_trylock(struct super_block *sb)
30983 +{
30984 +       int locked;
30985 +
30986 +       locked = __si_read_trylock(sb);
30987 +       if (locked)
30988 +               si_pid_set(sb);
30989 +       return locked;
30990 +}
30991 +
30992 +static inline void si_noflush_write_lock(struct super_block *sb)
30993 +{
30994 +       __si_write_lock(sb);
30995 +       si_pid_set(sb);
30996 +}
30997 +
30998 +static inline int si_noflush_write_trylock(struct super_block *sb)
30999 +{
31000 +       int locked;
31001 +
31002 +       locked = __si_write_trylock(sb);
31003 +       if (locked)
31004 +               si_pid_set(sb);
31005 +       return locked;
31006 +}
31007 +
31008 +#if 0 /* reserved */
31009 +static inline int si_read_trylock(struct super_block *sb, int flags)
31010 +{
31011 +       if (au_ftest_lock(flags, FLUSH))
31012 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31013 +       return si_noflush_read_trylock(sb);
31014 +}
31015 +#endif
31016 +
31017 +static inline void si_read_unlock(struct super_block *sb)
31018 +{
31019 +       si_pid_clr(sb);
31020 +       __si_read_unlock(sb);
31021 +}
31022 +
31023 +#if 0 /* reserved */
31024 +static inline int si_write_trylock(struct super_block *sb, int flags)
31025 +{
31026 +       if (au_ftest_lock(flags, FLUSH))
31027 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31028 +       return si_noflush_write_trylock(sb);
31029 +}
31030 +#endif
31031 +
31032 +static inline void si_write_unlock(struct super_block *sb)
31033 +{
31034 +       si_pid_clr(sb);
31035 +       __si_write_unlock(sb);
31036 +}
31037 +
31038 +#if 0 /* reserved */
31039 +static inline void si_downgrade_lock(struct super_block *sb)
31040 +{
31041 +       __si_downgrade_lock(sb);
31042 +}
31043 +#endif
31044 +
31045 +/* ---------------------------------------------------------------------- */
31046 +
31047 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31048 +{
31049 +       SiMustAnyLock(sb);
31050 +       return au_sbi(sb)->si_bbot;
31051 +}
31052 +
31053 +static inline unsigned int au_mntflags(struct super_block *sb)
31054 +{
31055 +       SiMustAnyLock(sb);
31056 +       return au_sbi(sb)->si_mntflags;
31057 +}
31058 +
31059 +static inline unsigned int au_sigen(struct super_block *sb)
31060 +{
31061 +       SiMustAnyLock(sb);
31062 +       return au_sbi(sb)->si_generation;
31063 +}
31064 +
31065 +static inline struct au_branch *au_sbr(struct super_block *sb,
31066 +                                      aufs_bindex_t bindex)
31067 +{
31068 +       SiMustAnyLock(sb);
31069 +       return au_sbi(sb)->si_branch[0 + bindex];
31070 +}
31071 +
31072 +static inline loff_t au_xi_maxent(struct super_block *sb)
31073 +{
31074 +       SiMustAnyLock(sb);
31075 +       return au_sbi(sb)->si_ximaxent;
31076 +}
31077 +
31078 +#endif /* __KERNEL__ */
31079 +#endif /* __AUFS_SUPER_H__ */
31080 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31081 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31082 +++ linux/fs/aufs/sysaufs.c     2020-01-27 10:57:18.178871751 +0100
31083 @@ -0,0 +1,93 @@
31084 +// SPDX-License-Identifier: GPL-2.0
31085 +/*
31086 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31087 + *
31088 + * This program, aufs is free software; you can redistribute it and/or modify
31089 + * it under the terms of the GNU General Public License as published by
31090 + * the Free Software Foundation; either version 2 of the License, or
31091 + * (at your option) any later version.
31092 + *
31093 + * This program is distributed in the hope that it will be useful,
31094 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31095 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31096 + * GNU General Public License for more details.
31097 + *
31098 + * You should have received a copy of the GNU General Public License
31099 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31100 + */
31101 +
31102 +/*
31103 + * sysfs interface and lifetime management
31104 + * they are necessary regardless sysfs is disabled.
31105 + */
31106 +
31107 +#include <linux/random.h>
31108 +#include "aufs.h"
31109 +
31110 +unsigned long sysaufs_si_mask;
31111 +struct kset *sysaufs_kset;
31112 +
31113 +#define AuSiAttr(_name) { \
31114 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31115 +       .show   = sysaufs_si_##_name,                           \
31116 +}
31117 +
31118 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31119 +struct attribute *sysaufs_si_attrs[] = {
31120 +       &sysaufs_si_attr_xi_path.attr,
31121 +       NULL,
31122 +};
31123 +
31124 +static const struct sysfs_ops au_sbi_ops = {
31125 +       .show   = sysaufs_si_show
31126 +};
31127 +
31128 +static struct kobj_type au_sbi_ktype = {
31129 +       .release        = au_si_free,
31130 +       .sysfs_ops      = &au_sbi_ops,
31131 +       .default_attrs  = sysaufs_si_attrs
31132 +};
31133 +
31134 +/* ---------------------------------------------------------------------- */
31135 +
31136 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31137 +{
31138 +       int err;
31139 +
31140 +       sbinfo->si_kobj.kset = sysaufs_kset;
31141 +       /* cf. sysaufs_name() */
31142 +       err = kobject_init_and_add
31143 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31144 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31145 +
31146 +       return err;
31147 +}
31148 +
31149 +void sysaufs_fin(void)
31150 +{
31151 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31152 +       kset_unregister(sysaufs_kset);
31153 +}
31154 +
31155 +int __init sysaufs_init(void)
31156 +{
31157 +       int err;
31158 +
31159 +       do {
31160 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31161 +       } while (!sysaufs_si_mask);
31162 +
31163 +       err = -EINVAL;
31164 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31165 +       if (unlikely(!sysaufs_kset))
31166 +               goto out;
31167 +       err = PTR_ERR(sysaufs_kset);
31168 +       if (IS_ERR(sysaufs_kset))
31169 +               goto out;
31170 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31171 +       if (unlikely(err))
31172 +               kset_unregister(sysaufs_kset);
31173 +
31174 +out:
31175 +       return err;
31176 +}
31177 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31178 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31179 +++ linux/fs/aufs/sysaufs.h     2020-01-27 10:57:18.178871751 +0100
31180 @@ -0,0 +1,102 @@
31181 +/* SPDX-License-Identifier: GPL-2.0 */
31182 +/*
31183 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31184 + *
31185 + * This program, aufs is free software; you can redistribute it and/or modify
31186 + * it under the terms of the GNU General Public License as published by
31187 + * the Free Software Foundation; either version 2 of the License, or
31188 + * (at your option) any later version.
31189 + *
31190 + * This program is distributed in the hope that it will be useful,
31191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31193 + * GNU General Public License for more details.
31194 + *
31195 + * You should have received a copy of the GNU General Public License
31196 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31197 + */
31198 +
31199 +/*
31200 + * sysfs interface and mount lifetime management
31201 + */
31202 +
31203 +#ifndef __SYSAUFS_H__
31204 +#define __SYSAUFS_H__
31205 +
31206 +#ifdef __KERNEL__
31207 +
31208 +#include <linux/sysfs.h>
31209 +#include "module.h"
31210 +
31211 +struct super_block;
31212 +struct au_sbinfo;
31213 +
31214 +struct sysaufs_si_attr {
31215 +       struct attribute attr;
31216 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31217 +};
31218 +
31219 +/* ---------------------------------------------------------------------- */
31220 +
31221 +/* sysaufs.c */
31222 +extern unsigned long sysaufs_si_mask;
31223 +extern struct kset *sysaufs_kset;
31224 +extern struct attribute *sysaufs_si_attrs[];
31225 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31226 +int __init sysaufs_init(void);
31227 +void sysaufs_fin(void);
31228 +
31229 +/* ---------------------------------------------------------------------- */
31230 +
31231 +/* some people doesn't like to show a pointer in kernel */
31232 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31233 +{
31234 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31235 +}
31236 +
31237 +#define SysaufsSiNamePrefix    "si_"
31238 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31239 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31240 +{
31241 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31242 +                sysaufs_si_id(sbinfo));
31243 +}
31244 +
31245 +struct au_branch;
31246 +#ifdef CONFIG_SYSFS
31247 +/* sysfs.c */
31248 +extern struct attribute_group *sysaufs_attr_group;
31249 +
31250 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31251 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31252 +                        char *buf);
31253 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31254 +#ifdef CONFIG_COMPAT
31255 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31256 +#endif
31257 +
31258 +void sysaufs_br_init(struct au_branch *br);
31259 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31260 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31261 +
31262 +#define sysaufs_brs_init()     do {} while (0)
31263 +
31264 +#else
31265 +#define sysaufs_attr_group     NULL
31266 +
31267 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31268 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31269 +       struct attribute *attr, char *buf)
31270 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31271 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31272 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31273 +
31274 +static inline void sysaufs_brs_init(void)
31275 +{
31276 +       sysaufs_brs = 0;
31277 +}
31278 +
31279 +#endif /* CONFIG_SYSFS */
31280 +
31281 +#endif /* __KERNEL__ */
31282 +#endif /* __SYSAUFS_H__ */
31283 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31284 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31285 +++ linux/fs/aufs/sysfs.c       2020-01-27 10:57:18.178871751 +0100
31286 @@ -0,0 +1,374 @@
31287 +// SPDX-License-Identifier: GPL-2.0
31288 +/*
31289 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31290 + *
31291 + * This program, aufs is free software; you can redistribute it and/or modify
31292 + * it under the terms of the GNU General Public License as published by
31293 + * the Free Software Foundation; either version 2 of the License, or
31294 + * (at your option) any later version.
31295 + *
31296 + * This program is distributed in the hope that it will be useful,
31297 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31298 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31299 + * GNU General Public License for more details.
31300 + *
31301 + * You should have received a copy of the GNU General Public License
31302 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31303 + */
31304 +
31305 +/*
31306 + * sysfs interface
31307 + */
31308 +
31309 +#include <linux/compat.h>
31310 +#include <linux/seq_file.h>
31311 +#include "aufs.h"
31312 +
31313 +#ifdef CONFIG_AUFS_FS_MODULE
31314 +/* this entry violates the "one line per file" policy of sysfs */
31315 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31316 +                          char *buf)
31317 +{
31318 +       ssize_t err;
31319 +       static char *conf =
31320 +/* this file is generated at compiling */
31321 +#include "conf.str"
31322 +               ;
31323 +
31324 +       err = snprintf(buf, PAGE_SIZE, conf);
31325 +       if (unlikely(err >= PAGE_SIZE))
31326 +               err = -EFBIG;
31327 +       return err;
31328 +}
31329 +
31330 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31331 +#endif
31332 +
31333 +static struct attribute *au_attr[] = {
31334 +#ifdef CONFIG_AUFS_FS_MODULE
31335 +       &au_config_attr.attr,
31336 +#endif
31337 +       NULL,   /* need to NULL terminate the list of attributes */
31338 +};
31339 +
31340 +static struct attribute_group sysaufs_attr_group_body = {
31341 +       .attrs = au_attr
31342 +};
31343 +
31344 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31345 +
31346 +/* ---------------------------------------------------------------------- */
31347 +
31348 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31349 +{
31350 +       int err;
31351 +
31352 +       SiMustAnyLock(sb);
31353 +
31354 +       err = 0;
31355 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31356 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31357 +               seq_putc(seq, '\n');
31358 +       }
31359 +       return err;
31360 +}
31361 +
31362 +/*
31363 + * the lifetime of branch is independent from the entry under sysfs.
31364 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31365 + * unlinked.
31366 + */
31367 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31368 +                        aufs_bindex_t bindex, int idx)
31369 +{
31370 +       int err;
31371 +       struct path path;
31372 +       struct dentry *root;
31373 +       struct au_branch *br;
31374 +       au_br_perm_str_t perm;
31375 +
31376 +       AuDbg("b%d\n", bindex);
31377 +
31378 +       err = 0;
31379 +       root = sb->s_root;
31380 +       di_read_lock_parent(root, !AuLock_IR);
31381 +       br = au_sbr(sb, bindex);
31382 +
31383 +       switch (idx) {
31384 +       case AuBrSysfs_BR:
31385 +               path.mnt = au_br_mnt(br);
31386 +               path.dentry = au_h_dptr(root, bindex);
31387 +               err = au_seq_path(seq, &path);
31388 +               if (!err) {
31389 +                       au_optstr_br_perm(&perm, br->br_perm);
31390 +                       seq_printf(seq, "=%s\n", perm.a);
31391 +               }
31392 +               break;
31393 +       case AuBrSysfs_BRID:
31394 +               seq_printf(seq, "%d\n", br->br_id);
31395 +               break;
31396 +       }
31397 +       di_read_unlock(root, !AuLock_IR);
31398 +       if (unlikely(err || seq_has_overflowed(seq)))
31399 +               err = -E2BIG;
31400 +
31401 +       return err;
31402 +}
31403 +
31404 +/* ---------------------------------------------------------------------- */
31405 +
31406 +static struct seq_file *au_seq(char *p, ssize_t len)
31407 +{
31408 +       struct seq_file *seq;
31409 +
31410 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31411 +       if (seq) {
31412 +               /* mutex_init(&seq.lock); */
31413 +               seq->buf = p;
31414 +               seq->size = len;
31415 +               return seq; /* success */
31416 +       }
31417 +
31418 +       seq = ERR_PTR(-ENOMEM);
31419 +       return seq;
31420 +}
31421 +
31422 +#define SysaufsBr_PREFIX       "br"
31423 +#define SysaufsBrid_PREFIX     "brid"
31424 +
31425 +/* todo: file size may exceed PAGE_SIZE */
31426 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31427 +                       char *buf)
31428 +{
31429 +       ssize_t err;
31430 +       int idx;
31431 +       long l;
31432 +       aufs_bindex_t bbot;
31433 +       struct au_sbinfo *sbinfo;
31434 +       struct super_block *sb;
31435 +       struct seq_file *seq;
31436 +       char *name;
31437 +       struct attribute **cattr;
31438 +
31439 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31440 +       sb = sbinfo->si_sb;
31441 +
31442 +       /*
31443 +        * prevent a race condition between sysfs and aufs.
31444 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31445 +        * prohibits maintaining the sysfs entries.
31446 +        * hew we acquire read lock after sysfs_get_active_two().
31447 +        * on the other hand, the remount process may maintain the sysfs/aufs
31448 +        * entries after acquiring write lock.
31449 +        * it can cause a deadlock.
31450 +        * simply we gave up processing read here.
31451 +        */
31452 +       err = -EBUSY;
31453 +       if (unlikely(!si_noflush_read_trylock(sb)))
31454 +               goto out;
31455 +
31456 +       seq = au_seq(buf, PAGE_SIZE);
31457 +       err = PTR_ERR(seq);
31458 +       if (IS_ERR(seq))
31459 +               goto out_unlock;
31460 +
31461 +       name = (void *)attr->name;
31462 +       cattr = sysaufs_si_attrs;
31463 +       while (*cattr) {
31464 +               if (!strcmp(name, (*cattr)->name)) {
31465 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31466 +                               ->show(seq, sb);
31467 +                       goto out_seq;
31468 +               }
31469 +               cattr++;
31470 +       }
31471 +
31472 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31473 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31474 +               idx = AuBrSysfs_BRID;
31475 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31476 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31477 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31478 +               idx = AuBrSysfs_BR;
31479 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31480 +       } else
31481 +                 BUG();
31482 +
31483 +       err = kstrtol(name, 10, &l);
31484 +       if (!err) {
31485 +               bbot = au_sbbot(sb);
31486 +               if (l <= bbot)
31487 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31488 +               else
31489 +                       err = -ENOENT;
31490 +       }
31491 +
31492 +out_seq:
31493 +       if (!err) {
31494 +               err = seq->count;
31495 +               /* sysfs limit */
31496 +               if (unlikely(err == PAGE_SIZE))
31497 +                       err = -EFBIG;
31498 +       }
31499 +       au_kfree_rcu(seq);
31500 +out_unlock:
31501 +       si_read_unlock(sb);
31502 +out:
31503 +       return err;
31504 +}
31505 +
31506 +/* ---------------------------------------------------------------------- */
31507 +
31508 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31509 +{
31510 +       int err;
31511 +       int16_t brid;
31512 +       aufs_bindex_t bindex, bbot;
31513 +       size_t sz;
31514 +       char *buf;
31515 +       struct seq_file *seq;
31516 +       struct au_branch *br;
31517 +
31518 +       si_read_lock(sb, AuLock_FLUSH);
31519 +       bbot = au_sbbot(sb);
31520 +       err = bbot + 1;
31521 +       if (!arg)
31522 +               goto out;
31523 +
31524 +       err = -ENOMEM;
31525 +       buf = (void *)__get_free_page(GFP_NOFS);
31526 +       if (unlikely(!buf))
31527 +               goto out;
31528 +
31529 +       seq = au_seq(buf, PAGE_SIZE);
31530 +       err = PTR_ERR(seq);
31531 +       if (IS_ERR(seq))
31532 +               goto out_buf;
31533 +
31534 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31535 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31536 +               /* VERIFY_WRITE */
31537 +               err = !access_ok(arg, sizeof(*arg));
31538 +               if (unlikely(err))
31539 +                       break;
31540 +
31541 +               br = au_sbr(sb, bindex);
31542 +               brid = br->br_id;
31543 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31544 +               err = __put_user(brid, &arg->id);
31545 +               if (unlikely(err))
31546 +                       break;
31547 +
31548 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31549 +               err = __put_user(br->br_perm, &arg->perm);
31550 +               if (unlikely(err))
31551 +                       break;
31552 +
31553 +               err = au_seq_path(seq, &br->br_path);
31554 +               if (unlikely(err))
31555 +                       break;
31556 +               seq_putc(seq, '\0');
31557 +               if (!seq_has_overflowed(seq)) {
31558 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31559 +                       seq->count = 0;
31560 +                       if (unlikely(err))
31561 +                               break;
31562 +               } else {
31563 +                       err = -E2BIG;
31564 +                       goto out_seq;
31565 +               }
31566 +       }
31567 +       if (unlikely(err))
31568 +               err = -EFAULT;
31569 +
31570 +out_seq:
31571 +       au_kfree_rcu(seq);
31572 +out_buf:
31573 +       free_page((unsigned long)buf);
31574 +out:
31575 +       si_read_unlock(sb);
31576 +       return err;
31577 +}
31578 +
31579 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31580 +{
31581 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31582 +}
31583 +
31584 +#ifdef CONFIG_COMPAT
31585 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31586 +{
31587 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31588 +}
31589 +#endif
31590 +
31591 +/* ---------------------------------------------------------------------- */
31592 +
31593 +void sysaufs_br_init(struct au_branch *br)
31594 +{
31595 +       int i;
31596 +       struct au_brsysfs *br_sysfs;
31597 +       struct attribute *attr;
31598 +
31599 +       br_sysfs = br->br_sysfs;
31600 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31601 +               attr = &br_sysfs->attr;
31602 +               sysfs_attr_init(attr);
31603 +               attr->name = br_sysfs->name;
31604 +               attr->mode = 0444;
31605 +               br_sysfs++;
31606 +       }
31607 +}
31608 +
31609 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31610 +{
31611 +       struct au_branch *br;
31612 +       struct kobject *kobj;
31613 +       struct au_brsysfs *br_sysfs;
31614 +       int i;
31615 +       aufs_bindex_t bbot;
31616 +
31617 +       if (!sysaufs_brs)
31618 +               return;
31619 +
31620 +       kobj = &au_sbi(sb)->si_kobj;
31621 +       bbot = au_sbbot(sb);
31622 +       for (; bindex <= bbot; bindex++) {
31623 +               br = au_sbr(sb, bindex);
31624 +               br_sysfs = br->br_sysfs;
31625 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31626 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31627 +                       br_sysfs++;
31628 +               }
31629 +       }
31630 +}
31631 +
31632 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31633 +{
31634 +       int err, i;
31635 +       aufs_bindex_t bbot;
31636 +       struct kobject *kobj;
31637 +       struct au_branch *br;
31638 +       struct au_brsysfs *br_sysfs;
31639 +
31640 +       if (!sysaufs_brs)
31641 +               return;
31642 +
31643 +       kobj = &au_sbi(sb)->si_kobj;
31644 +       bbot = au_sbbot(sb);
31645 +       for (; bindex <= bbot; bindex++) {
31646 +               br = au_sbr(sb, bindex);
31647 +               br_sysfs = br->br_sysfs;
31648 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31649 +                        SysaufsBr_PREFIX "%d", bindex);
31650 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31651 +                        SysaufsBrid_PREFIX "%d", bindex);
31652 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31653 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31654 +                       if (unlikely(err))
31655 +                               pr_warn("failed %s under sysfs(%d)\n",
31656 +                                       br_sysfs->name, err);
31657 +                       br_sysfs++;
31658 +               }
31659 +       }
31660 +}
31661 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31662 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31663 +++ linux/fs/aufs/sysrq.c       2020-01-27 10:57:18.178871751 +0100
31664 @@ -0,0 +1,149 @@
31665 +// SPDX-License-Identifier: GPL-2.0
31666 +/*
31667 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31668 + *
31669 + * This program, aufs is free software; you can redistribute it and/or modify
31670 + * it under the terms of the GNU General Public License as published by
31671 + * the Free Software Foundation; either version 2 of the License, or
31672 + * (at your option) any later version.
31673 + *
31674 + * This program is distributed in the hope that it will be useful,
31675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31677 + * GNU General Public License for more details.
31678 + *
31679 + * You should have received a copy of the GNU General Public License
31680 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31681 + */
31682 +
31683 +/*
31684 + * magic sysrq handler
31685 + */
31686 +
31687 +/* #include <linux/sysrq.h> */
31688 +#include <linux/writeback.h>
31689 +#include "aufs.h"
31690 +
31691 +/* ---------------------------------------------------------------------- */
31692 +
31693 +static void sysrq_sb(struct super_block *sb)
31694 +{
31695 +       char *plevel;
31696 +       struct au_sbinfo *sbinfo;
31697 +       struct file *file;
31698 +       struct hlist_bl_head *files;
31699 +       struct hlist_bl_node *pos;
31700 +       struct au_finfo *finfo;
31701 +       struct inode *i;
31702 +
31703 +       plevel = au_plevel;
31704 +       au_plevel = KERN_WARNING;
31705 +
31706 +       /* since we define pr_fmt, call printk directly */
31707 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31708 +
31709 +       sbinfo = au_sbi(sb);
31710 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31711 +       pr("superblock\n");
31712 +       au_dpri_sb(sb);
31713 +
31714 +#if 0 /* reserved */
31715 +       do {
31716 +               int err, i, j, ndentry;
31717 +               struct au_dcsub_pages dpages;
31718 +               struct au_dpage *dpage;
31719 +
31720 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31721 +               if (unlikely(err))
31722 +                       break;
31723 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31724 +               if (!err)
31725 +                       for (i = 0; i < dpages.ndpage; i++) {
31726 +                               dpage = dpages.dpages + i;
31727 +                               ndentry = dpage->ndentry;
31728 +                               for (j = 0; j < ndentry; j++)
31729 +                                       au_dpri_dentry(dpage->dentries[j]);
31730 +                       }
31731 +               au_dpages_free(&dpages);
31732 +       } while (0);
31733 +#endif
31734 +
31735 +       pr("isolated inode\n");
31736 +       spin_lock(&sb->s_inode_list_lock);
31737 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31738 +               spin_lock(&i->i_lock);
31739 +               if (hlist_empty(&i->i_dentry))
31740 +                       au_dpri_inode(i);
31741 +               spin_unlock(&i->i_lock);
31742 +       }
31743 +       spin_unlock(&sb->s_inode_list_lock);
31744 +
31745 +       pr("files\n");
31746 +       files = &au_sbi(sb)->si_files;
31747 +       hlist_bl_lock(files);
31748 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31749 +               umode_t mode;
31750 +
31751 +               file = finfo->fi_file;
31752 +               mode = file_inode(file)->i_mode;
31753 +               if (!special_file(mode))
31754 +                       au_dpri_file(file);
31755 +       }
31756 +       hlist_bl_unlock(files);
31757 +       pr("done\n");
31758 +
31759 +#undef pr
31760 +       au_plevel = plevel;
31761 +}
31762 +
31763 +/* ---------------------------------------------------------------------- */
31764 +
31765 +/* module parameter */
31766 +static char *aufs_sysrq_key = "a";
31767 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31768 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31769 +
31770 +static void au_sysrq(int key __maybe_unused)
31771 +{
31772 +       struct au_sbinfo *sbinfo;
31773 +       struct hlist_bl_node *pos;
31774 +
31775 +       lockdep_off();
31776 +       au_sbilist_lock();
31777 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31778 +               sysrq_sb(sbinfo->si_sb);
31779 +       au_sbilist_unlock();
31780 +       lockdep_on();
31781 +}
31782 +
31783 +static struct sysrq_key_op au_sysrq_op = {
31784 +       .handler        = au_sysrq,
31785 +       .help_msg       = "Aufs",
31786 +       .action_msg     = "Aufs",
31787 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31788 +};
31789 +
31790 +/* ---------------------------------------------------------------------- */
31791 +
31792 +int __init au_sysrq_init(void)
31793 +{
31794 +       int err;
31795 +       char key;
31796 +
31797 +       err = -1;
31798 +       key = *aufs_sysrq_key;
31799 +       if ('a' <= key && key <= 'z')
31800 +               err = register_sysrq_key(key, &au_sysrq_op);
31801 +       if (unlikely(err))
31802 +               pr_err("err %d, sysrq=%c\n", err, key);
31803 +       return err;
31804 +}
31805 +
31806 +void au_sysrq_fin(void)
31807 +{
31808 +       int err;
31809 +
31810 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31811 +       if (unlikely(err))
31812 +               pr_err("err %d (ignored)\n", err);
31813 +}
31814 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31815 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31816 +++ linux/fs/aufs/vdir.c        2020-01-27 10:57:18.178871751 +0100
31817 @@ -0,0 +1,896 @@
31818 +// SPDX-License-Identifier: GPL-2.0
31819 +/*
31820 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31821 + *
31822 + * This program, aufs is free software; you can redistribute it and/or modify
31823 + * it under the terms of the GNU General Public License as published by
31824 + * the Free Software Foundation; either version 2 of the License, or
31825 + * (at your option) any later version.
31826 + *
31827 + * This program is distributed in the hope that it will be useful,
31828 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31829 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31830 + * GNU General Public License for more details.
31831 + *
31832 + * You should have received a copy of the GNU General Public License
31833 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31834 + */
31835 +
31836 +/*
31837 + * virtual or vertical directory
31838 + */
31839 +
31840 +#include <linux/iversion.h>
31841 +#include "aufs.h"
31842 +
31843 +static unsigned int calc_size(int nlen)
31844 +{
31845 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31846 +}
31847 +
31848 +static int set_deblk_end(union au_vdir_deblk_p *p,
31849 +                        union au_vdir_deblk_p *deblk_end)
31850 +{
31851 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31852 +               p->de->de_str.len = 0;
31853 +               /* smp_mb(); */
31854 +               return 0;
31855 +       }
31856 +       return -1; /* error */
31857 +}
31858 +
31859 +/* returns true or false */
31860 +static int is_deblk_end(union au_vdir_deblk_p *p,
31861 +                       union au_vdir_deblk_p *deblk_end)
31862 +{
31863 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31864 +               return !p->de->de_str.len;
31865 +       return 1;
31866 +}
31867 +
31868 +static unsigned char *last_deblk(struct au_vdir *vdir)
31869 +{
31870 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31871 +}
31872 +
31873 +/* ---------------------------------------------------------------------- */
31874 +
31875 +/* estimate the appropriate size for name hash table */
31876 +unsigned int au_rdhash_est(loff_t sz)
31877 +{
31878 +       unsigned int n;
31879 +
31880 +       n = UINT_MAX;
31881 +       sz >>= 10;
31882 +       if (sz < n)
31883 +               n = sz;
31884 +       if (sz < AUFS_RDHASH_DEF)
31885 +               n = AUFS_RDHASH_DEF;
31886 +       /* pr_info("n %u\n", n); */
31887 +       return n;
31888 +}
31889 +
31890 +/*
31891 + * the allocated memory has to be freed by
31892 + * au_nhash_wh_free() or au_nhash_de_free().
31893 + */
31894 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31895 +{
31896 +       struct hlist_head *head;
31897 +       unsigned int u;
31898 +       size_t sz;
31899 +
31900 +       sz = sizeof(*nhash->nh_head) * num_hash;
31901 +       head = kmalloc(sz, gfp);
31902 +       if (head) {
31903 +               nhash->nh_num = num_hash;
31904 +               nhash->nh_head = head;
31905 +               for (u = 0; u < num_hash; u++)
31906 +                       INIT_HLIST_HEAD(head++);
31907 +               return 0; /* success */
31908 +       }
31909 +
31910 +       return -ENOMEM;
31911 +}
31912 +
31913 +static void nhash_count(struct hlist_head *head)
31914 +{
31915 +#if 0 /* debugging */
31916 +       unsigned long n;
31917 +       struct hlist_node *pos;
31918 +
31919 +       n = 0;
31920 +       hlist_for_each(pos, head)
31921 +               n++;
31922 +       pr_info("%lu\n", n);
31923 +#endif
31924 +}
31925 +
31926 +static void au_nhash_wh_do_free(struct hlist_head *head)
31927 +{
31928 +       struct au_vdir_wh *pos;
31929 +       struct hlist_node *node;
31930 +
31931 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31932 +               au_kfree_rcu(pos);
31933 +}
31934 +
31935 +static void au_nhash_de_do_free(struct hlist_head *head)
31936 +{
31937 +       struct au_vdir_dehstr *pos;
31938 +       struct hlist_node *node;
31939 +
31940 +       hlist_for_each_entry_safe(pos, node, head, hash)
31941 +               au_cache_free_vdir_dehstr(pos);
31942 +}
31943 +
31944 +static void au_nhash_do_free(struct au_nhash *nhash,
31945 +                            void (*free)(struct hlist_head *head))
31946 +{
31947 +       unsigned int n;
31948 +       struct hlist_head *head;
31949 +
31950 +       n = nhash->nh_num;
31951 +       if (!n)
31952 +               return;
31953 +
31954 +       head = nhash->nh_head;
31955 +       while (n-- > 0) {
31956 +               nhash_count(head);
31957 +               free(head++);
31958 +       }
31959 +       au_kfree_try_rcu(nhash->nh_head);
31960 +}
31961 +
31962 +void au_nhash_wh_free(struct au_nhash *whlist)
31963 +{
31964 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31965 +}
31966 +
31967 +static void au_nhash_de_free(struct au_nhash *delist)
31968 +{
31969 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31970 +}
31971 +
31972 +/* ---------------------------------------------------------------------- */
31973 +
31974 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31975 +                           int limit)
31976 +{
31977 +       int num;
31978 +       unsigned int u, n;
31979 +       struct hlist_head *head;
31980 +       struct au_vdir_wh *pos;
31981 +
31982 +       num = 0;
31983 +       n = whlist->nh_num;
31984 +       head = whlist->nh_head;
31985 +       for (u = 0; u < n; u++, head++)
31986 +               hlist_for_each_entry(pos, head, wh_hash)
31987 +                       if (pos->wh_bindex == btgt && ++num > limit)
31988 +                               return 1;
31989 +       return 0;
31990 +}
31991 +
31992 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31993 +                                      unsigned char *name,
31994 +                                      unsigned int len)
31995 +{
31996 +       unsigned int v;
31997 +       /* const unsigned int magic_bit = 12; */
31998 +
31999 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32000 +
32001 +       v = 0;
32002 +       if (len > 8)
32003 +               len = 8;
32004 +       while (len--)
32005 +               v += *name++;
32006 +       /* v = hash_long(v, magic_bit); */
32007 +       v %= nhash->nh_num;
32008 +       return nhash->nh_head + v;
32009 +}
32010 +
32011 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32012 +                             int nlen)
32013 +{
32014 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32015 +}
32016 +
32017 +/* returns found or not */
32018 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32019 +{
32020 +       struct hlist_head *head;
32021 +       struct au_vdir_wh *pos;
32022 +       struct au_vdir_destr *str;
32023 +
32024 +       head = au_name_hash(whlist, name, nlen);
32025 +       hlist_for_each_entry(pos, head, wh_hash) {
32026 +               str = &pos->wh_str;
32027 +               AuDbg("%.*s\n", str->len, str->name);
32028 +               if (au_nhash_test_name(str, name, nlen))
32029 +                       return 1;
32030 +       }
32031 +       return 0;
32032 +}
32033 +
32034 +/* returns found(true) or not */
32035 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32036 +{
32037 +       struct hlist_head *head;
32038 +       struct au_vdir_dehstr *pos;
32039 +       struct au_vdir_destr *str;
32040 +
32041 +       head = au_name_hash(delist, name, nlen);
32042 +       hlist_for_each_entry(pos, head, hash) {
32043 +               str = pos->str;
32044 +               AuDbg("%.*s\n", str->len, str->name);
32045 +               if (au_nhash_test_name(str, name, nlen))
32046 +                       return 1;
32047 +       }
32048 +       return 0;
32049 +}
32050 +
32051 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32052 +                           unsigned char d_type)
32053 +{
32054 +#ifdef CONFIG_AUFS_SHWH
32055 +       wh->wh_ino = ino;
32056 +       wh->wh_type = d_type;
32057 +#endif
32058 +}
32059 +
32060 +/* ---------------------------------------------------------------------- */
32061 +
32062 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32063 +                      unsigned int d_type, aufs_bindex_t bindex,
32064 +                      unsigned char shwh)
32065 +{
32066 +       int err;
32067 +       struct au_vdir_destr *str;
32068 +       struct au_vdir_wh *wh;
32069 +
32070 +       AuDbg("%.*s\n", nlen, name);
32071 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32072 +
32073 +       err = -ENOMEM;
32074 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32075 +       if (unlikely(!wh))
32076 +               goto out;
32077 +
32078 +       err = 0;
32079 +       wh->wh_bindex = bindex;
32080 +       if (shwh)
32081 +               au_shwh_init_wh(wh, ino, d_type);
32082 +       str = &wh->wh_str;
32083 +       str->len = nlen;
32084 +       memcpy(str->name, name, nlen);
32085 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32086 +       /* smp_mb(); */
32087 +
32088 +out:
32089 +       return err;
32090 +}
32091 +
32092 +static int append_deblk(struct au_vdir *vdir)
32093 +{
32094 +       int err;
32095 +       unsigned long ul;
32096 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32097 +       union au_vdir_deblk_p p, deblk_end;
32098 +       unsigned char **o;
32099 +
32100 +       err = -ENOMEM;
32101 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32102 +                       GFP_NOFS, /*may_shrink*/0);
32103 +       if (unlikely(!o))
32104 +               goto out;
32105 +
32106 +       vdir->vd_deblk = o;
32107 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32108 +       if (p.deblk) {
32109 +               ul = vdir->vd_nblk++;
32110 +               vdir->vd_deblk[ul] = p.deblk;
32111 +               vdir->vd_last.ul = ul;
32112 +               vdir->vd_last.p.deblk = p.deblk;
32113 +               deblk_end.deblk = p.deblk + deblk_sz;
32114 +               err = set_deblk_end(&p, &deblk_end);
32115 +       }
32116 +
32117 +out:
32118 +       return err;
32119 +}
32120 +
32121 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32122 +                    unsigned int d_type, struct au_nhash *delist)
32123 +{
32124 +       int err;
32125 +       unsigned int sz;
32126 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32127 +       union au_vdir_deblk_p p, *room, deblk_end;
32128 +       struct au_vdir_dehstr *dehstr;
32129 +
32130 +       p.deblk = last_deblk(vdir);
32131 +       deblk_end.deblk = p.deblk + deblk_sz;
32132 +       room = &vdir->vd_last.p;
32133 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32134 +                 || !is_deblk_end(room, &deblk_end));
32135 +
32136 +       sz = calc_size(nlen);
32137 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32138 +               err = append_deblk(vdir);
32139 +               if (unlikely(err))
32140 +                       goto out;
32141 +
32142 +               p.deblk = last_deblk(vdir);
32143 +               deblk_end.deblk = p.deblk + deblk_sz;
32144 +               /* smp_mb(); */
32145 +               AuDebugOn(room->deblk != p.deblk);
32146 +       }
32147 +
32148 +       err = -ENOMEM;
32149 +       dehstr = au_cache_alloc_vdir_dehstr();
32150 +       if (unlikely(!dehstr))
32151 +               goto out;
32152 +
32153 +       dehstr->str = &room->de->de_str;
32154 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32155 +       room->de->de_ino = ino;
32156 +       room->de->de_type = d_type;
32157 +       room->de->de_str.len = nlen;
32158 +       memcpy(room->de->de_str.name, name, nlen);
32159 +
32160 +       err = 0;
32161 +       room->deblk += sz;
32162 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32163 +               err = append_deblk(vdir);
32164 +       /* smp_mb(); */
32165 +
32166 +out:
32167 +       return err;
32168 +}
32169 +
32170 +/* ---------------------------------------------------------------------- */
32171 +
32172 +void au_vdir_free(struct au_vdir *vdir)
32173 +{
32174 +       unsigned char **deblk;
32175 +
32176 +       deblk = vdir->vd_deblk;
32177 +       while (vdir->vd_nblk--)
32178 +               au_kfree_try_rcu(*deblk++);
32179 +       au_kfree_try_rcu(vdir->vd_deblk);
32180 +       au_cache_free_vdir(vdir);
32181 +}
32182 +
32183 +static struct au_vdir *alloc_vdir(struct file *file)
32184 +{
32185 +       struct au_vdir *vdir;
32186 +       struct super_block *sb;
32187 +       int err;
32188 +
32189 +       sb = file->f_path.dentry->d_sb;
32190 +       SiMustAnyLock(sb);
32191 +
32192 +       err = -ENOMEM;
32193 +       vdir = au_cache_alloc_vdir();
32194 +       if (unlikely(!vdir))
32195 +               goto out;
32196 +
32197 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32198 +       if (unlikely(!vdir->vd_deblk))
32199 +               goto out_free;
32200 +
32201 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32202 +       if (!vdir->vd_deblk_sz) {
32203 +               /* estimate the appropriate size for deblk */
32204 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32205 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32206 +       }
32207 +       vdir->vd_nblk = 0;
32208 +       vdir->vd_version = 0;
32209 +       vdir->vd_jiffy = 0;
32210 +       err = append_deblk(vdir);
32211 +       if (!err)
32212 +               return vdir; /* success */
32213 +
32214 +       au_kfree_try_rcu(vdir->vd_deblk);
32215 +
32216 +out_free:
32217 +       au_cache_free_vdir(vdir);
32218 +out:
32219 +       vdir = ERR_PTR(err);
32220 +       return vdir;
32221 +}
32222 +
32223 +static int reinit_vdir(struct au_vdir *vdir)
32224 +{
32225 +       int err;
32226 +       union au_vdir_deblk_p p, deblk_end;
32227 +
32228 +       while (vdir->vd_nblk > 1) {
32229 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32230 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32231 +               vdir->vd_nblk--;
32232 +       }
32233 +       p.deblk = vdir->vd_deblk[0];
32234 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32235 +       err = set_deblk_end(&p, &deblk_end);
32236 +       /* keep vd_dblk_sz */
32237 +       vdir->vd_last.ul = 0;
32238 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32239 +       vdir->vd_version = 0;
32240 +       vdir->vd_jiffy = 0;
32241 +       /* smp_mb(); */
32242 +       return err;
32243 +}
32244 +
32245 +/* ---------------------------------------------------------------------- */
32246 +
32247 +#define AuFillVdir_CALLED      1
32248 +#define AuFillVdir_WHABLE      (1 << 1)
32249 +#define AuFillVdir_SHWH                (1 << 2)
32250 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32251 +#define au_fset_fillvdir(flags, name) \
32252 +       do { (flags) |= AuFillVdir_##name; } while (0)
32253 +#define au_fclr_fillvdir(flags, name) \
32254 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32255 +
32256 +#ifndef CONFIG_AUFS_SHWH
32257 +#undef AuFillVdir_SHWH
32258 +#define AuFillVdir_SHWH                0
32259 +#endif
32260 +
32261 +struct fillvdir_arg {
32262 +       struct dir_context      ctx;
32263 +       struct file             *file;
32264 +       struct au_vdir          *vdir;
32265 +       struct au_nhash         delist;
32266 +       struct au_nhash         whlist;
32267 +       aufs_bindex_t           bindex;
32268 +       unsigned int            flags;
32269 +       int                     err;
32270 +};
32271 +
32272 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32273 +                   loff_t offset __maybe_unused, u64 h_ino,
32274 +                   unsigned int d_type)
32275 +{
32276 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32277 +       char *name = (void *)__name;
32278 +       struct super_block *sb;
32279 +       ino_t ino;
32280 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32281 +
32282 +       arg->err = 0;
32283 +       sb = arg->file->f_path.dentry->d_sb;
32284 +       au_fset_fillvdir(arg->flags, CALLED);
32285 +       /* smp_mb(); */
32286 +       if (nlen <= AUFS_WH_PFX_LEN
32287 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32288 +               if (test_known(&arg->delist, name, nlen)
32289 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32290 +                       goto out; /* already exists or whiteouted */
32291 +
32292 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32293 +               if (!arg->err) {
32294 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32295 +                               d_type = DT_UNKNOWN;
32296 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32297 +                                            d_type, &arg->delist);
32298 +               }
32299 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32300 +               name += AUFS_WH_PFX_LEN;
32301 +               nlen -= AUFS_WH_PFX_LEN;
32302 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32303 +                       goto out; /* already whiteouted */
32304 +
32305 +               ino = 0; /* just to suppress a warning */
32306 +               if (shwh)
32307 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32308 +                                            &ino);
32309 +               if (!arg->err) {
32310 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32311 +                               d_type = DT_UNKNOWN;
32312 +                       arg->err = au_nhash_append_wh
32313 +                               (&arg->whlist, name, nlen, ino, d_type,
32314 +                                arg->bindex, shwh);
32315 +               }
32316 +       }
32317 +
32318 +out:
32319 +       if (!arg->err)
32320 +               arg->vdir->vd_jiffy = jiffies;
32321 +       /* smp_mb(); */
32322 +       AuTraceErr(arg->err);
32323 +       return arg->err;
32324 +}
32325 +
32326 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32327 +                         struct au_nhash *whlist, struct au_nhash *delist)
32328 +{
32329 +#ifdef CONFIG_AUFS_SHWH
32330 +       int err;
32331 +       unsigned int nh, u;
32332 +       struct hlist_head *head;
32333 +       struct au_vdir_wh *pos;
32334 +       struct hlist_node *n;
32335 +       char *p, *o;
32336 +       struct au_vdir_destr *destr;
32337 +
32338 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32339 +
32340 +       err = -ENOMEM;
32341 +       o = p = (void *)__get_free_page(GFP_NOFS);
32342 +       if (unlikely(!p))
32343 +               goto out;
32344 +
32345 +       err = 0;
32346 +       nh = whlist->nh_num;
32347 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32348 +       p += AUFS_WH_PFX_LEN;
32349 +       for (u = 0; u < nh; u++) {
32350 +               head = whlist->nh_head + u;
32351 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32352 +                       destr = &pos->wh_str;
32353 +                       memcpy(p, destr->name, destr->len);
32354 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32355 +                                       pos->wh_ino, pos->wh_type, delist);
32356 +                       if (unlikely(err))
32357 +                               break;
32358 +               }
32359 +       }
32360 +
32361 +       free_page((unsigned long)o);
32362 +
32363 +out:
32364 +       AuTraceErr(err);
32365 +       return err;
32366 +#else
32367 +       return 0;
32368 +#endif
32369 +}
32370 +
32371 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32372 +{
32373 +       int err;
32374 +       unsigned int rdhash;
32375 +       loff_t offset;
32376 +       aufs_bindex_t bbot, bindex, btop;
32377 +       unsigned char shwh;
32378 +       struct file *hf, *file;
32379 +       struct super_block *sb;
32380 +
32381 +       file = arg->file;
32382 +       sb = file->f_path.dentry->d_sb;
32383 +       SiMustAnyLock(sb);
32384 +
32385 +       rdhash = au_sbi(sb)->si_rdhash;
32386 +       if (!rdhash)
32387 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32388 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32389 +       if (unlikely(err))
32390 +               goto out;
32391 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32392 +       if (unlikely(err))
32393 +               goto out_delist;
32394 +
32395 +       err = 0;
32396 +       arg->flags = 0;
32397 +       shwh = 0;
32398 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32399 +               shwh = 1;
32400 +               au_fset_fillvdir(arg->flags, SHWH);
32401 +       }
32402 +       btop = au_fbtop(file);
32403 +       bbot = au_fbbot_dir(file);
32404 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32405 +               hf = au_hf_dir(file, bindex);
32406 +               if (!hf)
32407 +                       continue;
32408 +
32409 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32410 +               err = offset;
32411 +               if (unlikely(offset))
32412 +                       break;
32413 +
32414 +               arg->bindex = bindex;
32415 +               au_fclr_fillvdir(arg->flags, WHABLE);
32416 +               if (shwh
32417 +                   || (bindex != bbot
32418 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32419 +                       au_fset_fillvdir(arg->flags, WHABLE);
32420 +               do {
32421 +                       arg->err = 0;
32422 +                       au_fclr_fillvdir(arg->flags, CALLED);
32423 +                       /* smp_mb(); */
32424 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32425 +                       if (err >= 0)
32426 +                               err = arg->err;
32427 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32428 +
32429 +               /*
32430 +                * dir_relax() may be good for concurrency, but aufs should not
32431 +                * use it since it will cause a lockdep problem.
32432 +                */
32433 +       }
32434 +
32435 +       if (!err && shwh)
32436 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32437 +
32438 +       au_nhash_wh_free(&arg->whlist);
32439 +
32440 +out_delist:
32441 +       au_nhash_de_free(&arg->delist);
32442 +out:
32443 +       return err;
32444 +}
32445 +
32446 +static int read_vdir(struct file *file, int may_read)
32447 +{
32448 +       int err;
32449 +       unsigned long expire;
32450 +       unsigned char do_read;
32451 +       struct fillvdir_arg arg = {
32452 +               .ctx = {
32453 +                       .actor = fillvdir
32454 +               }
32455 +       };
32456 +       struct inode *inode;
32457 +       struct au_vdir *vdir, *allocated;
32458 +
32459 +       err = 0;
32460 +       inode = file_inode(file);
32461 +       IMustLock(inode);
32462 +       IiMustWriteLock(inode);
32463 +       SiMustAnyLock(inode->i_sb);
32464 +
32465 +       allocated = NULL;
32466 +       do_read = 0;
32467 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32468 +       vdir = au_ivdir(inode);
32469 +       if (!vdir) {
32470 +               do_read = 1;
32471 +               vdir = alloc_vdir(file);
32472 +               err = PTR_ERR(vdir);
32473 +               if (IS_ERR(vdir))
32474 +                       goto out;
32475 +               err = 0;
32476 +               allocated = vdir;
32477 +       } else if (may_read
32478 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32479 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32480 +               do_read = 1;
32481 +               err = reinit_vdir(vdir);
32482 +               if (unlikely(err))
32483 +                       goto out;
32484 +       }
32485 +
32486 +       if (!do_read)
32487 +               return 0; /* success */
32488 +
32489 +       arg.file = file;
32490 +       arg.vdir = vdir;
32491 +       err = au_do_read_vdir(&arg);
32492 +       if (!err) {
32493 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32494 +               vdir->vd_version = inode_query_iversion(inode);
32495 +               vdir->vd_last.ul = 0;
32496 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32497 +               if (allocated)
32498 +                       au_set_ivdir(inode, allocated);
32499 +       } else if (allocated)
32500 +               au_vdir_free(allocated);
32501 +
32502 +out:
32503 +       return err;
32504 +}
32505 +
32506 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32507 +{
32508 +       int err, rerr;
32509 +       unsigned long ul, n;
32510 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32511 +
32512 +       AuDebugOn(tgt->vd_nblk != 1);
32513 +
32514 +       err = -ENOMEM;
32515 +       if (tgt->vd_nblk < src->vd_nblk) {
32516 +               unsigned char **p;
32517 +
32518 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32519 +                               GFP_NOFS, /*may_shrink*/0);
32520 +               if (unlikely(!p))
32521 +                       goto out;
32522 +               tgt->vd_deblk = p;
32523 +       }
32524 +
32525 +       if (tgt->vd_deblk_sz != deblk_sz) {
32526 +               unsigned char *p;
32527 +
32528 +               tgt->vd_deblk_sz = deblk_sz;
32529 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32530 +                               /*may_shrink*/1);
32531 +               if (unlikely(!p))
32532 +                       goto out;
32533 +               tgt->vd_deblk[0] = p;
32534 +       }
32535 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32536 +       tgt->vd_version = src->vd_version;
32537 +       tgt->vd_jiffy = src->vd_jiffy;
32538 +
32539 +       n = src->vd_nblk;
32540 +       for (ul = 1; ul < n; ul++) {
32541 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32542 +                                           GFP_NOFS);
32543 +               if (unlikely(!tgt->vd_deblk[ul]))
32544 +                       goto out;
32545 +               tgt->vd_nblk++;
32546 +       }
32547 +       tgt->vd_nblk = n;
32548 +       tgt->vd_last.ul = tgt->vd_last.ul;
32549 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32550 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32551 +               - src->vd_deblk[src->vd_last.ul];
32552 +       /* smp_mb(); */
32553 +       return 0; /* success */
32554 +
32555 +out:
32556 +       rerr = reinit_vdir(tgt);
32557 +       BUG_ON(rerr);
32558 +       return err;
32559 +}
32560 +
32561 +int au_vdir_init(struct file *file)
32562 +{
32563 +       int err;
32564 +       struct inode *inode;
32565 +       struct au_vdir *vdir_cache, *allocated;
32566 +
32567 +       /* test file->f_pos here instead of ctx->pos */
32568 +       err = read_vdir(file, !file->f_pos);
32569 +       if (unlikely(err))
32570 +               goto out;
32571 +
32572 +       allocated = NULL;
32573 +       vdir_cache = au_fvdir_cache(file);
32574 +       if (!vdir_cache) {
32575 +               vdir_cache = alloc_vdir(file);
32576 +               err = PTR_ERR(vdir_cache);
32577 +               if (IS_ERR(vdir_cache))
32578 +                       goto out;
32579 +               allocated = vdir_cache;
32580 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32581 +               /* test file->f_pos here instead of ctx->pos */
32582 +               err = reinit_vdir(vdir_cache);
32583 +               if (unlikely(err))
32584 +                       goto out;
32585 +       } else
32586 +               return 0; /* success */
32587 +
32588 +       inode = file_inode(file);
32589 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32590 +       if (!err) {
32591 +               file->f_version = inode_query_iversion(inode);
32592 +               if (allocated)
32593 +                       au_set_fvdir_cache(file, allocated);
32594 +       } else if (allocated)
32595 +               au_vdir_free(allocated);
32596 +
32597 +out:
32598 +       return err;
32599 +}
32600 +
32601 +static loff_t calc_offset(struct au_vdir *vdir)
32602 +{
32603 +       loff_t offset;
32604 +       union au_vdir_deblk_p p;
32605 +
32606 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32607 +       offset = vdir->vd_last.p.deblk - p.deblk;
32608 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32609 +       return offset;
32610 +}
32611 +
32612 +/* returns true or false */
32613 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32614 +{
32615 +       int valid;
32616 +       unsigned int deblk_sz;
32617 +       unsigned long ul, n;
32618 +       loff_t offset;
32619 +       union au_vdir_deblk_p p, deblk_end;
32620 +       struct au_vdir *vdir_cache;
32621 +
32622 +       valid = 1;
32623 +       vdir_cache = au_fvdir_cache(file);
32624 +       offset = calc_offset(vdir_cache);
32625 +       AuDbg("offset %lld\n", offset);
32626 +       if (ctx->pos == offset)
32627 +               goto out;
32628 +
32629 +       vdir_cache->vd_last.ul = 0;
32630 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32631 +       if (!ctx->pos)
32632 +               goto out;
32633 +
32634 +       valid = 0;
32635 +       deblk_sz = vdir_cache->vd_deblk_sz;
32636 +       ul = div64_u64(ctx->pos, deblk_sz);
32637 +       AuDbg("ul %lu\n", ul);
32638 +       if (ul >= vdir_cache->vd_nblk)
32639 +               goto out;
32640 +
32641 +       n = vdir_cache->vd_nblk;
32642 +       for (; ul < n; ul++) {
32643 +               p.deblk = vdir_cache->vd_deblk[ul];
32644 +               deblk_end.deblk = p.deblk + deblk_sz;
32645 +               offset = ul;
32646 +               offset *= deblk_sz;
32647 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32648 +                       unsigned int l;
32649 +
32650 +                       l = calc_size(p.de->de_str.len);
32651 +                       offset += l;
32652 +                       p.deblk += l;
32653 +               }
32654 +               if (!is_deblk_end(&p, &deblk_end)) {
32655 +                       valid = 1;
32656 +                       vdir_cache->vd_last.ul = ul;
32657 +                       vdir_cache->vd_last.p = p;
32658 +                       break;
32659 +               }
32660 +       }
32661 +
32662 +out:
32663 +       /* smp_mb(); */
32664 +       if (!valid)
32665 +               AuDbg("valid %d\n", !valid);
32666 +       return valid;
32667 +}
32668 +
32669 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32670 +{
32671 +       unsigned int l, deblk_sz;
32672 +       union au_vdir_deblk_p deblk_end;
32673 +       struct au_vdir *vdir_cache;
32674 +       struct au_vdir_de *de;
32675 +
32676 +       if (!seek_vdir(file, ctx))
32677 +               return 0;
32678 +
32679 +       vdir_cache = au_fvdir_cache(file);
32680 +       deblk_sz = vdir_cache->vd_deblk_sz;
32681 +       while (1) {
32682 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32683 +               deblk_end.deblk += deblk_sz;
32684 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32685 +                       de = vdir_cache->vd_last.p.de;
32686 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32687 +                             de->de_str.len, de->de_str.name, ctx->pos,
32688 +                             (unsigned long)de->de_ino, de->de_type);
32689 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32690 +                                              de->de_str.len, de->de_ino,
32691 +                                              de->de_type))) {
32692 +                               /* todo: ignore the error caused by udba? */
32693 +                               /* return err; */
32694 +                               return 0;
32695 +                       }
32696 +
32697 +                       l = calc_size(de->de_str.len);
32698 +                       vdir_cache->vd_last.p.deblk += l;
32699 +                       ctx->pos += l;
32700 +               }
32701 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32702 +                       vdir_cache->vd_last.ul++;
32703 +                       vdir_cache->vd_last.p.deblk
32704 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32705 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32706 +                       continue;
32707 +               }
32708 +               break;
32709 +       }
32710 +
32711 +       /* smp_mb(); */
32712 +       return 0;
32713 +}
32714 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32715 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32716 +++ linux/fs/aufs/vfsub.c       2020-08-03 09:14:46.095748745 +0200
32717 @@ -0,0 +1,895 @@
32718 +// SPDX-License-Identifier: GPL-2.0
32719 +/*
32720 + * Copyright (C) 2005-2020 Junjiro R. Okajima
32721 + *
32722 + * This program, aufs is free software; you can redistribute it and/or modify
32723 + * it under the terms of the GNU General Public License as published by
32724 + * the Free Software Foundation; either version 2 of the License, or
32725 + * (at your option) any later version.
32726 + *
32727 + * This program is distributed in the hope that it will be useful,
32728 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32729 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32730 + * GNU General Public License for more details.
32731 + *
32732 + * You should have received a copy of the GNU General Public License
32733 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32734 + */
32735 +
32736 +/*
32737 + * sub-routines for VFS
32738 + */
32739 +
32740 +#include <linux/mnt_namespace.h>
32741 +#include <linux/namei.h>
32742 +#include <linux/nsproxy.h>
32743 +#include <linux/security.h>
32744 +#include <linux/splice.h>
32745 +#include "aufs.h"
32746 +
32747 +#ifdef CONFIG_AUFS_BR_FUSE
32748 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32749 +{
32750 +       if (!au_test_fuse(h_sb) || !au_userns)
32751 +               return 0;
32752 +
32753 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32754 +}
32755 +#endif
32756 +
32757 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32758 +{
32759 +       int err;
32760 +
32761 +       lockdep_off();
32762 +       down_read(&h_sb->s_umount);
32763 +       err = __sync_filesystem(h_sb, wait);
32764 +       up_read(&h_sb->s_umount);
32765 +       lockdep_on();
32766 +
32767 +       return err;
32768 +}
32769 +
32770 +/* ---------------------------------------------------------------------- */
32771 +
32772 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32773 +{
32774 +       int err;
32775 +       struct kstat st;
32776 +       struct super_block *h_sb;
32777 +
32778 +       /* for remote fs, leave work for its getattr or d_revalidate */
32779 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32780 +       /* still some fs may acquire i_mutex. we need to skip them */
32781 +       err = 0;
32782 +       if (!did)
32783 +               did = &err;
32784 +       h_sb = h_path->dentry->d_sb;
32785 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32786 +       if (*did)
32787 +               err = vfsub_getattr(h_path, &st);
32788 +
32789 +       return err;
32790 +}
32791 +
32792 +/* ---------------------------------------------------------------------- */
32793 +
32794 +struct file *vfsub_dentry_open(struct path *path, int flags)
32795 +{
32796 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32797 +                          current_cred());
32798 +}
32799 +
32800 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32801 +{
32802 +       struct file *file;
32803 +
32804 +       lockdep_off();
32805 +       file = filp_open(path,
32806 +                        oflags /* | __FMODE_NONOTIFY */,
32807 +                        mode);
32808 +       lockdep_on();
32809 +       if (IS_ERR(file))
32810 +               goto out;
32811 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32812 +
32813 +out:
32814 +       return file;
32815 +}
32816 +
32817 +/*
32818 + * Ideally this function should call VFS:do_last() in order to keep all its
32819 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32820 + * structure such as nameidata. This is a second (or third) best approach.
32821 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32822 + */
32823 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32824 +                     struct vfsub_aopen_args *args)
32825 +{
32826 +       int err;
32827 +       struct au_branch *br = args->br;
32828 +       struct file *file = args->file;
32829 +       /* copied from linux/fs/namei.c:atomic_open() */
32830 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32831 +
32832 +       IMustLock(dir);
32833 +       AuDebugOn(!dir->i_op->atomic_open);
32834 +
32835 +       err = au_br_test_oflag(args->open_flag, br);
32836 +       if (unlikely(err))
32837 +               goto out;
32838 +
32839 +       au_lcnt_inc(&br->br_nfiles);
32840 +       file->f_path.dentry = DENTRY_NOT_SET;
32841 +       file->f_path.mnt = au_br_mnt(br);
32842 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32843 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32844 +                                    args->create_mode);
32845 +       if (unlikely(err < 0)) {
32846 +               au_lcnt_dec(&br->br_nfiles);
32847 +               goto out;
32848 +       }
32849 +
32850 +       /* temporary workaround for nfsv4 branch */
32851 +       if (au_test_nfs(dir->i_sb))
32852 +               nfs_mark_for_revalidate(dir);
32853 +
32854 +       if (file->f_mode & FMODE_CREATED)
32855 +               fsnotify_create(dir, dentry);
32856 +       if (!(file->f_mode & FMODE_OPENED)) {
32857 +               au_lcnt_dec(&br->br_nfiles);
32858 +               goto out;
32859 +       }
32860 +
32861 +       /* todo: call VFS:may_open() here */
32862 +       /* todo: ima_file_check() too? */
32863 +       if (!err && (args->open_flag & __FMODE_EXEC))
32864 +               err = deny_write_access(file);
32865 +       if (!err)
32866 +               fsnotify_open(file);
32867 +       else
32868 +               au_lcnt_dec(&br->br_nfiles);
32869 +       /* note that the file is created and still opened */
32870 +
32871 +out:
32872 +       return err;
32873 +}
32874 +
32875 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32876 +{
32877 +       int err;
32878 +
32879 +       err = kern_path(name, flags, path);
32880 +       if (!err && d_is_positive(path->dentry))
32881 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32882 +       return err;
32883 +}
32884 +
32885 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32886 +                                            struct dentry *parent, int len)
32887 +{
32888 +       struct path path = {
32889 +               .mnt = NULL
32890 +       };
32891 +
32892 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32893 +       if (IS_ERR(path.dentry))
32894 +               goto out;
32895 +       if (d_is_positive(path.dentry))
32896 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32897 +
32898 +out:
32899 +       AuTraceErrPtr(path.dentry);
32900 +       return path.dentry;
32901 +}
32902 +
32903 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32904 +                                   int len)
32905 +{
32906 +       struct path path = {
32907 +               .mnt = NULL
32908 +       };
32909 +
32910 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32911 +       IMustLock(d_inode(parent));
32912 +
32913 +       path.dentry = lookup_one_len(name, parent, len);
32914 +       if (IS_ERR(path.dentry))
32915 +               goto out;
32916 +       if (d_is_positive(path.dentry))
32917 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32918 +
32919 +out:
32920 +       AuTraceErrPtr(path.dentry);
32921 +       return path.dentry;
32922 +}
32923 +
32924 +void vfsub_call_lkup_one(void *args)
32925 +{
32926 +       struct vfsub_lkup_one_args *a = args;
32927 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32928 +}
32929 +
32930 +/* ---------------------------------------------------------------------- */
32931 +
32932 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32933 +                                struct dentry *d2, struct au_hinode *hdir2)
32934 +{
32935 +       struct dentry *d;
32936 +
32937 +       lockdep_off();
32938 +       d = lock_rename(d1, d2);
32939 +       lockdep_on();
32940 +       au_hn_suspend(hdir1);
32941 +       if (hdir1 != hdir2)
32942 +               au_hn_suspend(hdir2);
32943 +
32944 +       return d;
32945 +}
32946 +
32947 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32948 +                        struct dentry *d2, struct au_hinode *hdir2)
32949 +{
32950 +       au_hn_resume(hdir1);
32951 +       if (hdir1 != hdir2)
32952 +               au_hn_resume(hdir2);
32953 +       lockdep_off();
32954 +       unlock_rename(d1, d2);
32955 +       lockdep_on();
32956 +}
32957 +
32958 +/* ---------------------------------------------------------------------- */
32959 +
32960 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32961 +{
32962 +       int err;
32963 +       struct dentry *d;
32964 +
32965 +       IMustLock(dir);
32966 +
32967 +       d = path->dentry;
32968 +       path->dentry = d->d_parent;
32969 +       err = security_path_mknod(path, d, mode, 0);
32970 +       path->dentry = d;
32971 +       if (unlikely(err))
32972 +               goto out;
32973 +
32974 +       lockdep_off();
32975 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32976 +       lockdep_on();
32977 +       if (!err) {
32978 +               struct path tmp = *path;
32979 +               int did;
32980 +
32981 +               vfsub_update_h_iattr(&tmp, &did);
32982 +               if (did) {
32983 +                       tmp.dentry = path->dentry->d_parent;
32984 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32985 +               }
32986 +               /*ignore*/
32987 +       }
32988 +
32989 +out:
32990 +       return err;
32991 +}
32992 +
32993 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32994 +{
32995 +       int err;
32996 +       struct dentry *d;
32997 +
32998 +       IMustLock(dir);
32999 +
33000 +       d = path->dentry;
33001 +       path->dentry = d->d_parent;
33002 +       err = security_path_symlink(path, d, symname);
33003 +       path->dentry = d;
33004 +       if (unlikely(err))
33005 +               goto out;
33006 +
33007 +       lockdep_off();
33008 +       err = vfs_symlink(dir, path->dentry, symname);
33009 +       lockdep_on();
33010 +       if (!err) {
33011 +               struct path tmp = *path;
33012 +               int did;
33013 +
33014 +               vfsub_update_h_iattr(&tmp, &did);
33015 +               if (did) {
33016 +                       tmp.dentry = path->dentry->d_parent;
33017 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33018 +               }
33019 +               /*ignore*/
33020 +       }
33021 +
33022 +out:
33023 +       return err;
33024 +}
33025 +
33026 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33027 +{
33028 +       int err;
33029 +       struct dentry *d;
33030 +
33031 +       IMustLock(dir);
33032 +
33033 +       d = path->dentry;
33034 +       path->dentry = d->d_parent;
33035 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33036 +       path->dentry = d;
33037 +       if (unlikely(err))
33038 +               goto out;
33039 +
33040 +       lockdep_off();
33041 +       err = vfs_mknod(dir, path->dentry, mode, dev);
33042 +       lockdep_on();
33043 +       if (!err) {
33044 +               struct path tmp = *path;
33045 +               int did;
33046 +
33047 +               vfsub_update_h_iattr(&tmp, &did);
33048 +               if (did) {
33049 +                       tmp.dentry = path->dentry->d_parent;
33050 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33051 +               }
33052 +               /*ignore*/
33053 +       }
33054 +
33055 +out:
33056 +       return err;
33057 +}
33058 +
33059 +static int au_test_nlink(struct inode *inode)
33060 +{
33061 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33062 +
33063 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33064 +           || inode->i_nlink < link_max)
33065 +               return 0;
33066 +       return -EMLINK;
33067 +}
33068 +
33069 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33070 +              struct inode **delegated_inode)
33071 +{
33072 +       int err;
33073 +       struct dentry *d;
33074 +
33075 +       IMustLock(dir);
33076 +
33077 +       err = au_test_nlink(d_inode(src_dentry));
33078 +       if (unlikely(err))
33079 +               return err;
33080 +
33081 +       /* we don't call may_linkat() */
33082 +       d = path->dentry;
33083 +       path->dentry = d->d_parent;
33084 +       err = security_path_link(src_dentry, path, d);
33085 +       path->dentry = d;
33086 +       if (unlikely(err))
33087 +               goto out;
33088 +
33089 +       lockdep_off();
33090 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
33091 +       lockdep_on();
33092 +       if (!err) {
33093 +               struct path tmp = *path;
33094 +               int did;
33095 +
33096 +               /* fuse has different memory inode for the same inumber */
33097 +               vfsub_update_h_iattr(&tmp, &did);
33098 +               if (did) {
33099 +                       tmp.dentry = path->dentry->d_parent;
33100 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33101 +                       tmp.dentry = src_dentry;
33102 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33103 +               }
33104 +               /*ignore*/
33105 +       }
33106 +
33107 +out:
33108 +       return err;
33109 +}
33110 +
33111 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33112 +                struct inode *dir, struct path *path,
33113 +                struct inode **delegated_inode, unsigned int flags)
33114 +{
33115 +       int err;
33116 +       struct path tmp = {
33117 +               .mnt    = path->mnt
33118 +       };
33119 +       struct dentry *d;
33120 +
33121 +       IMustLock(dir);
33122 +       IMustLock(src_dir);
33123 +
33124 +       d = path->dentry;
33125 +       path->dentry = d->d_parent;
33126 +       tmp.dentry = src_dentry->d_parent;
33127 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33128 +       path->dentry = d;
33129 +       if (unlikely(err))
33130 +               goto out;
33131 +
33132 +       lockdep_off();
33133 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33134 +                        delegated_inode, flags);
33135 +       lockdep_on();
33136 +       if (!err) {
33137 +               int did;
33138 +
33139 +               tmp.dentry = d->d_parent;
33140 +               vfsub_update_h_iattr(&tmp, &did);
33141 +               if (did) {
33142 +                       tmp.dentry = src_dentry;
33143 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33144 +                       tmp.dentry = src_dentry->d_parent;
33145 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33146 +               }
33147 +               /*ignore*/
33148 +       }
33149 +
33150 +out:
33151 +       return err;
33152 +}
33153 +
33154 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33155 +{
33156 +       int err;
33157 +       struct dentry *d;
33158 +
33159 +       IMustLock(dir);
33160 +
33161 +       d = path->dentry;
33162 +       path->dentry = d->d_parent;
33163 +       err = security_path_mkdir(path, d, mode);
33164 +       path->dentry = d;
33165 +       if (unlikely(err))
33166 +               goto out;
33167 +
33168 +       lockdep_off();
33169 +       err = vfs_mkdir(dir, path->dentry, mode);
33170 +       lockdep_on();
33171 +       if (!err) {
33172 +               struct path tmp = *path;
33173 +               int did;
33174 +
33175 +               vfsub_update_h_iattr(&tmp, &did);
33176 +               if (did) {
33177 +                       tmp.dentry = path->dentry->d_parent;
33178 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33179 +               }
33180 +               /*ignore*/
33181 +       }
33182 +
33183 +out:
33184 +       return err;
33185 +}
33186 +
33187 +int vfsub_rmdir(struct inode *dir, struct path *path)
33188 +{
33189 +       int err;
33190 +       struct dentry *d;
33191 +
33192 +       IMustLock(dir);
33193 +
33194 +       d = path->dentry;
33195 +       path->dentry = d->d_parent;
33196 +       err = security_path_rmdir(path, d);
33197 +       path->dentry = d;
33198 +       if (unlikely(err))
33199 +               goto out;
33200 +
33201 +       lockdep_off();
33202 +       err = vfs_rmdir(dir, path->dentry);
33203 +       lockdep_on();
33204 +       if (!err) {
33205 +               struct path tmp = {
33206 +                       .dentry = path->dentry->d_parent,
33207 +                       .mnt    = path->mnt
33208 +               };
33209 +
33210 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33211 +       }
33212 +
33213 +out:
33214 +       return err;
33215 +}
33216 +
33217 +/* ---------------------------------------------------------------------- */
33218 +
33219 +/* todo: support mmap_sem? */
33220 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33221 +                    loff_t *ppos)
33222 +{
33223 +       ssize_t err;
33224 +
33225 +       lockdep_off();
33226 +       err = vfs_read(file, ubuf, count, ppos);
33227 +       lockdep_on();
33228 +       if (err >= 0)
33229 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33230 +       return err;
33231 +}
33232 +
33233 +/* todo: kernel_read()? */
33234 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33235 +                    loff_t *ppos)
33236 +{
33237 +       ssize_t err;
33238 +       mm_segment_t oldfs;
33239 +       union {
33240 +               void *k;
33241 +               char __user *u;
33242 +       } buf;
33243 +
33244 +       buf.k = kbuf;
33245 +       oldfs = get_fs();
33246 +       set_fs(KERNEL_DS);
33247 +       err = vfsub_read_u(file, buf.u, count, ppos);
33248 +       set_fs(oldfs);
33249 +       return err;
33250 +}
33251 +
33252 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33253 +                     loff_t *ppos)
33254 +{
33255 +       ssize_t err;
33256 +
33257 +       lockdep_off();
33258 +       err = vfs_write(file, ubuf, count, ppos);
33259 +       lockdep_on();
33260 +       if (err >= 0)
33261 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33262 +       return err;
33263 +}
33264 +
33265 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33266 +{
33267 +       ssize_t err;
33268 +       mm_segment_t oldfs;
33269 +       union {
33270 +               void *k;
33271 +               const char __user *u;
33272 +       } buf;
33273 +
33274 +       buf.k = kbuf;
33275 +       oldfs = get_fs();
33276 +       set_fs(KERNEL_DS);
33277 +       err = vfsub_write_u(file, buf.u, count, ppos);
33278 +       set_fs(oldfs);
33279 +       return err;
33280 +}
33281 +
33282 +int vfsub_flush(struct file *file, fl_owner_t id)
33283 +{
33284 +       int err;
33285 +
33286 +       err = 0;
33287 +       if (file->f_op->flush) {
33288 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33289 +                       err = file->f_op->flush(file, id);
33290 +               else {
33291 +                       lockdep_off();
33292 +                       err = file->f_op->flush(file, id);
33293 +                       lockdep_on();
33294 +               }
33295 +               if (!err)
33296 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33297 +               /*ignore*/
33298 +       }
33299 +       return err;
33300 +}
33301 +
33302 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33303 +{
33304 +       int err;
33305 +
33306 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33307 +
33308 +       lockdep_off();
33309 +       err = iterate_dir(file, ctx);
33310 +       lockdep_on();
33311 +       if (err >= 0)
33312 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33313 +
33314 +       return err;
33315 +}
33316 +
33317 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33318 +                    struct pipe_inode_info *pipe, size_t len,
33319 +                    unsigned int flags)
33320 +{
33321 +       long err;
33322 +
33323 +       lockdep_off();
33324 +       err = do_splice_to(in, ppos, pipe, len, flags);
33325 +       lockdep_on();
33326 +       file_accessed(in);
33327 +       if (err >= 0)
33328 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33329 +       return err;
33330 +}
33331 +
33332 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33333 +                      loff_t *ppos, size_t len, unsigned int flags)
33334 +{
33335 +       long err;
33336 +
33337 +       lockdep_off();
33338 +       err = do_splice_from(pipe, out, ppos, len, flags);
33339 +       lockdep_on();
33340 +       if (err >= 0)
33341 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33342 +       return err;
33343 +}
33344 +
33345 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33346 +{
33347 +       int err;
33348 +
33349 +       /* file can be NULL */
33350 +       lockdep_off();
33351 +       err = vfs_fsync(file, datasync);
33352 +       lockdep_on();
33353 +       if (!err) {
33354 +               if (!path) {
33355 +                       AuDebugOn(!file);
33356 +                       path = &file->f_path;
33357 +               }
33358 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33359 +       }
33360 +       return err;
33361 +}
33362 +
33363 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33364 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33365 +               struct file *h_file)
33366 +{
33367 +       int err;
33368 +       struct inode *h_inode;
33369 +       struct super_block *h_sb;
33370 +
33371 +       if (!h_file) {
33372 +               err = vfsub_truncate(h_path, length);
33373 +               goto out;
33374 +       }
33375 +
33376 +       h_inode = d_inode(h_path->dentry);
33377 +       h_sb = h_inode->i_sb;
33378 +       lockdep_off();
33379 +       sb_start_write(h_sb);
33380 +       lockdep_on();
33381 +       err = locks_verify_truncate(h_inode, h_file, length);
33382 +       if (!err)
33383 +               err = security_path_truncate(h_path);
33384 +       if (!err) {
33385 +               lockdep_off();
33386 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33387 +               lockdep_on();
33388 +       }
33389 +       lockdep_off();
33390 +       sb_end_write(h_sb);
33391 +       lockdep_on();
33392 +
33393 +out:
33394 +       return err;
33395 +}
33396 +
33397 +/* ---------------------------------------------------------------------- */
33398 +
33399 +struct au_vfsub_mkdir_args {
33400 +       int *errp;
33401 +       struct inode *dir;
33402 +       struct path *path;
33403 +       int mode;
33404 +};
33405 +
33406 +static void au_call_vfsub_mkdir(void *args)
33407 +{
33408 +       struct au_vfsub_mkdir_args *a = args;
33409 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33410 +}
33411 +
33412 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33413 +{
33414 +       int err, do_sio, wkq_err;
33415 +
33416 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33417 +       if (!do_sio) {
33418 +               lockdep_off();
33419 +               err = vfsub_mkdir(dir, path, mode);
33420 +               lockdep_on();
33421 +       } else {
33422 +               struct au_vfsub_mkdir_args args = {
33423 +                       .errp   = &err,
33424 +                       .dir    = dir,
33425 +                       .path   = path,
33426 +                       .mode   = mode
33427 +               };
33428 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33429 +               if (unlikely(wkq_err))
33430 +                       err = wkq_err;
33431 +       }
33432 +
33433 +       return err;
33434 +}
33435 +
33436 +struct au_vfsub_rmdir_args {
33437 +       int *errp;
33438 +       struct inode *dir;
33439 +       struct path *path;
33440 +};
33441 +
33442 +static void au_call_vfsub_rmdir(void *args)
33443 +{
33444 +       struct au_vfsub_rmdir_args *a = args;
33445 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33446 +}
33447 +
33448 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33449 +{
33450 +       int err, do_sio, wkq_err;
33451 +
33452 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33453 +       if (!do_sio) {
33454 +               lockdep_off();
33455 +               err = vfsub_rmdir(dir, path);
33456 +               lockdep_on();
33457 +       } else {
33458 +               struct au_vfsub_rmdir_args args = {
33459 +                       .errp   = &err,
33460 +                       .dir    = dir,
33461 +                       .path   = path
33462 +               };
33463 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33464 +               if (unlikely(wkq_err))
33465 +                       err = wkq_err;
33466 +       }
33467 +
33468 +       return err;
33469 +}
33470 +
33471 +/* ---------------------------------------------------------------------- */
33472 +
33473 +struct notify_change_args {
33474 +       int *errp;
33475 +       struct path *path;
33476 +       struct iattr *ia;
33477 +       struct inode **delegated_inode;
33478 +};
33479 +
33480 +static void call_notify_change(void *args)
33481 +{
33482 +       struct notify_change_args *a = args;
33483 +       struct inode *h_inode;
33484 +
33485 +       h_inode = d_inode(a->path->dentry);
33486 +       IMustLock(h_inode);
33487 +
33488 +       *a->errp = -EPERM;
33489 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33490 +               lockdep_off();
33491 +               *a->errp = notify_change(a->path->dentry, a->ia,
33492 +                                        a->delegated_inode);
33493 +               lockdep_on();
33494 +               if (!*a->errp)
33495 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33496 +       }
33497 +       AuTraceErr(*a->errp);
33498 +}
33499 +
33500 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33501 +                       struct inode **delegated_inode)
33502 +{
33503 +       int err;
33504 +       struct notify_change_args args = {
33505 +               .errp                   = &err,
33506 +               .path                   = path,
33507 +               .ia                     = ia,
33508 +               .delegated_inode        = delegated_inode
33509 +       };
33510 +
33511 +       call_notify_change(&args);
33512 +
33513 +       return err;
33514 +}
33515 +
33516 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33517 +                           struct inode **delegated_inode)
33518 +{
33519 +       int err, wkq_err;
33520 +       struct notify_change_args args = {
33521 +               .errp                   = &err,
33522 +               .path                   = path,
33523 +               .ia                     = ia,
33524 +               .delegated_inode        = delegated_inode
33525 +       };
33526 +
33527 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33528 +       if (unlikely(wkq_err))
33529 +               err = wkq_err;
33530 +
33531 +       return err;
33532 +}
33533 +
33534 +/* ---------------------------------------------------------------------- */
33535 +
33536 +struct unlink_args {
33537 +       int *errp;
33538 +       struct inode *dir;
33539 +       struct path *path;
33540 +       struct inode **delegated_inode;
33541 +};
33542 +
33543 +static void call_unlink(void *args)
33544 +{
33545 +       struct unlink_args *a = args;
33546 +       struct dentry *d = a->path->dentry;
33547 +       struct inode *h_inode;
33548 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33549 +                                     && au_dcount(d) == 1);
33550 +
33551 +       IMustLock(a->dir);
33552 +
33553 +       a->path->dentry = d->d_parent;
33554 +       *a->errp = security_path_unlink(a->path, d);
33555 +       a->path->dentry = d;
33556 +       if (unlikely(*a->errp))
33557 +               return;
33558 +
33559 +       if (!stop_sillyrename)
33560 +               dget(d);
33561 +       h_inode = NULL;
33562 +       if (d_is_positive(d)) {
33563 +               h_inode = d_inode(d);
33564 +               ihold(h_inode);
33565 +       }
33566 +
33567 +       lockdep_off();
33568 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33569 +       lockdep_on();
33570 +       if (!*a->errp) {
33571 +               struct path tmp = {
33572 +                       .dentry = d->d_parent,
33573 +                       .mnt    = a->path->mnt
33574 +               };
33575 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33576 +       }
33577 +
33578 +       if (!stop_sillyrename)
33579 +               dput(d);
33580 +       if (h_inode)
33581 +               iput(h_inode);
33582 +
33583 +       AuTraceErr(*a->errp);
33584 +}
33585 +
33586 +/*
33587 + * @dir: must be locked.
33588 + * @dentry: target dentry.
33589 + */
33590 +int vfsub_unlink(struct inode *dir, struct path *path,
33591 +                struct inode **delegated_inode, int force)
33592 +{
33593 +       int err;
33594 +       struct unlink_args args = {
33595 +               .errp                   = &err,
33596 +               .dir                    = dir,
33597 +               .path                   = path,
33598 +               .delegated_inode        = delegated_inode
33599 +       };
33600 +
33601 +       if (!force)
33602 +               call_unlink(&args);
33603 +       else {
33604 +               int wkq_err;
33605 +
33606 +               wkq_err = au_wkq_wait(call_unlink, &args);
33607 +               if (unlikely(wkq_err))
33608 +                       err = wkq_err;
33609 +       }
33610 +
33611 +       return err;
33612 +}
33613 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33614 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33615 +++ linux/fs/aufs/vfsub.h       2020-01-27 10:57:18.178871751 +0100
33616 @@ -0,0 +1,354 @@
33617 +/* SPDX-License-Identifier: GPL-2.0 */
33618 +/*
33619 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33620 + *
33621 + * This program, aufs is free software; you can redistribute it and/or modify
33622 + * it under the terms of the GNU General Public License as published by
33623 + * the Free Software Foundation; either version 2 of the License, or
33624 + * (at your option) any later version.
33625 + *
33626 + * This program is distributed in the hope that it will be useful,
33627 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33628 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33629 + * GNU General Public License for more details.
33630 + *
33631 + * You should have received a copy of the GNU General Public License
33632 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33633 + */
33634 +
33635 +/*
33636 + * sub-routines for VFS
33637 + */
33638 +
33639 +#ifndef __AUFS_VFSUB_H__
33640 +#define __AUFS_VFSUB_H__
33641 +
33642 +#ifdef __KERNEL__
33643 +
33644 +#include <linux/fs.h>
33645 +#include <linux/mount.h>
33646 +#include <linux/posix_acl.h>
33647 +#include <linux/xattr.h>
33648 +#include "debug.h"
33649 +
33650 +/* copied from linux/fs/internal.h */
33651 +/* todo: BAD approach!! */
33652 +extern void __mnt_drop_write(struct vfsmount *);
33653 +extern struct file *alloc_empty_file(int, const struct cred *);
33654 +
33655 +/* ---------------------------------------------------------------------- */
33656 +
33657 +/* lock subclass for lower inode */
33658 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33659 +/* reduce? gave up. */
33660 +enum {
33661 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33662 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33663 +       AuLsc_I_PARENT2,        /* copyup dirs */
33664 +       AuLsc_I_PARENT3,        /* copyup wh */
33665 +       AuLsc_I_CHILD,
33666 +       AuLsc_I_CHILD2,
33667 +       AuLsc_I_End
33668 +};
33669 +
33670 +/* to debug easier, do not make them inlined functions */
33671 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33672 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33673 +
33674 +/* ---------------------------------------------------------------------- */
33675 +
33676 +static inline void vfsub_drop_nlink(struct inode *inode)
33677 +{
33678 +       AuDebugOn(!inode->i_nlink);
33679 +       drop_nlink(inode);
33680 +}
33681 +
33682 +static inline void vfsub_dead_dir(struct inode *inode)
33683 +{
33684 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33685 +       inode->i_flags |= S_DEAD;
33686 +       clear_nlink(inode);
33687 +}
33688 +
33689 +static inline int vfsub_native_ro(struct inode *inode)
33690 +{
33691 +       return sb_rdonly(inode->i_sb)
33692 +               || IS_RDONLY(inode)
33693 +               /* || IS_APPEND(inode) */
33694 +               || IS_IMMUTABLE(inode);
33695 +}
33696 +
33697 +#ifdef CONFIG_AUFS_BR_FUSE
33698 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33699 +#else
33700 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33701 +#endif
33702 +
33703 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33704 +
33705 +/* ---------------------------------------------------------------------- */
33706 +
33707 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33708 +struct file *vfsub_dentry_open(struct path *path, int flags);
33709 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33710 +struct au_branch;
33711 +struct vfsub_aopen_args {
33712 +       struct file             *file;
33713 +       unsigned int            open_flag;
33714 +       umode_t                 create_mode;
33715 +       struct au_branch        *br;
33716 +};
33717 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33718 +                     struct vfsub_aopen_args *args);
33719 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33720 +
33721 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33722 +                                            struct dentry *parent, int len);
33723 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33724 +                                   int len);
33725 +
33726 +struct vfsub_lkup_one_args {
33727 +       struct dentry **errp;
33728 +       struct qstr *name;
33729 +       struct dentry *parent;
33730 +};
33731 +
33732 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33733 +                                           struct dentry *parent)
33734 +{
33735 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33736 +}
33737 +
33738 +void vfsub_call_lkup_one(void *args);
33739 +
33740 +/* ---------------------------------------------------------------------- */
33741 +
33742 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33743 +{
33744 +       int err;
33745 +
33746 +       lockdep_off();
33747 +       err = mnt_want_write(mnt);
33748 +       lockdep_on();
33749 +       return err;
33750 +}
33751 +
33752 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33753 +{
33754 +       lockdep_off();
33755 +       mnt_drop_write(mnt);
33756 +       lockdep_on();
33757 +}
33758 +
33759 +#if 0 /* reserved */
33760 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33761 +{
33762 +       lockdep_off();
33763 +       mnt_drop_write_file(file);
33764 +       lockdep_on();
33765 +}
33766 +#endif
33767 +
33768 +/* ---------------------------------------------------------------------- */
33769 +
33770 +struct au_hinode;
33771 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33772 +                                struct dentry *d2, struct au_hinode *hdir2);
33773 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33774 +                        struct dentry *d2, struct au_hinode *hdir2);
33775 +
33776 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33777 +                bool want_excl);
33778 +int vfsub_symlink(struct inode *dir, struct path *path,
33779 +                 const char *symname);
33780 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33781 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33782 +              struct path *path, struct inode **delegated_inode);
33783 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33784 +                struct inode *hdir, struct path *path,
33785 +                struct inode **delegated_inode, unsigned int flags);
33786 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33787 +int vfsub_rmdir(struct inode *dir, struct path *path);
33788 +
33789 +/* ---------------------------------------------------------------------- */
33790 +
33791 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33792 +                    loff_t *ppos);
33793 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33794 +                       loff_t *ppos);
33795 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33796 +                     loff_t *ppos);
33797 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33798 +                     loff_t *ppos);
33799 +int vfsub_flush(struct file *file, fl_owner_t id);
33800 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33801 +
33802 +static inline loff_t vfsub_f_size_read(struct file *file)
33803 +{
33804 +       return i_size_read(file_inode(file));
33805 +}
33806 +
33807 +static inline unsigned int vfsub_file_flags(struct file *file)
33808 +{
33809 +       unsigned int flags;
33810 +
33811 +       spin_lock(&file->f_lock);
33812 +       flags = file->f_flags;
33813 +       spin_unlock(&file->f_lock);
33814 +
33815 +       return flags;
33816 +}
33817 +
33818 +static inline int vfsub_file_execed(struct file *file)
33819 +{
33820 +       /* todo: direct access f_flags */
33821 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33822 +}
33823 +
33824 +#if 0 /* reserved */
33825 +static inline void vfsub_file_accessed(struct file *h_file)
33826 +{
33827 +       file_accessed(h_file);
33828 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33829 +}
33830 +#endif
33831 +
33832 +#if 0 /* reserved */
33833 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33834 +                                    struct dentry *h_dentry)
33835 +{
33836 +       struct path h_path = {
33837 +               .dentry = h_dentry,
33838 +               .mnt    = h_mnt
33839 +       };
33840 +       touch_atime(&h_path);
33841 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33842 +}
33843 +#endif
33844 +
33845 +static inline int vfsub_update_time(struct inode *h_inode,
33846 +                                   struct timespec64 *ts, int flags)
33847 +{
33848 +       return update_time(h_inode, ts, flags);
33849 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33850 +}
33851 +
33852 +#ifdef CONFIG_FS_POSIX_ACL
33853 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33854 +{
33855 +       int err;
33856 +
33857 +       err = posix_acl_chmod(h_inode, h_mode);
33858 +       if (err == -EOPNOTSUPP)
33859 +               err = 0;
33860 +       return err;
33861 +}
33862 +#else
33863 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33864 +#endif
33865 +
33866 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33867 +                    struct pipe_inode_info *pipe, size_t len,
33868 +                    unsigned int flags);
33869 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33870 +                      loff_t *ppos, size_t len, unsigned int flags);
33871 +
33872 +static inline long vfsub_truncate(struct path *path, loff_t length)
33873 +{
33874 +       long err;
33875 +
33876 +       lockdep_off();
33877 +       err = vfs_truncate(path, length);
33878 +       lockdep_on();
33879 +       return err;
33880 +}
33881 +
33882 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33883 +               struct file *h_file);
33884 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33885 +
33886 +/*
33887 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33888 + * ioctl.
33889 + */
33890 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33891 +                                           loff_t len)
33892 +{
33893 +       loff_t err;
33894 +
33895 +       lockdep_off();
33896 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33897 +       lockdep_on();
33898 +
33899 +       return err;
33900 +}
33901 +
33902 +/* copy_file_range(2) is a systemcall */
33903 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33904 +                                           struct file *dst, loff_t dst_pos,
33905 +                                           size_t len, unsigned int flags)
33906 +{
33907 +       ssize_t ssz;
33908 +
33909 +       lockdep_off();
33910 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33911 +       lockdep_on();
33912 +
33913 +       return ssz;
33914 +}
33915 +
33916 +/* ---------------------------------------------------------------------- */
33917 +
33918 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33919 +{
33920 +       loff_t err;
33921 +
33922 +       lockdep_off();
33923 +       err = vfs_llseek(file, offset, origin);
33924 +       lockdep_on();
33925 +       return err;
33926 +}
33927 +
33928 +/* ---------------------------------------------------------------------- */
33929 +
33930 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33931 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33932 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33933 +                           struct inode **delegated_inode);
33934 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33935 +                       struct inode **delegated_inode);
33936 +int vfsub_unlink(struct inode *dir, struct path *path,
33937 +                struct inode **delegated_inode, int force);
33938 +
33939 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33940 +{
33941 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33942 +}
33943 +
33944 +/* ---------------------------------------------------------------------- */
33945 +
33946 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33947 +                                const void *value, size_t size, int flags)
33948 +{
33949 +       int err;
33950 +
33951 +       lockdep_off();
33952 +       err = vfs_setxattr(dentry, name, value, size, flags);
33953 +       lockdep_on();
33954 +
33955 +       return err;
33956 +}
33957 +
33958 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33959 +{
33960 +       int err;
33961 +
33962 +       lockdep_off();
33963 +       err = vfs_removexattr(dentry, name);
33964 +       lockdep_on();
33965 +
33966 +       return err;
33967 +}
33968 +
33969 +#endif /* __KERNEL__ */
33970 +#endif /* __AUFS_VFSUB_H__ */
33971 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33972 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33973 +++ linux/fs/aufs/wbr_policy.c  2020-01-27 10:57:18.178871751 +0100
33974 @@ -0,0 +1,830 @@
33975 +// SPDX-License-Identifier: GPL-2.0
33976 +/*
33977 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33978 + *
33979 + * This program, aufs is free software; you can redistribute it and/or modify
33980 + * it under the terms of the GNU General Public License as published by
33981 + * the Free Software Foundation; either version 2 of the License, or
33982 + * (at your option) any later version.
33983 + *
33984 + * This program is distributed in the hope that it will be useful,
33985 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33986 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33987 + * GNU General Public License for more details.
33988 + *
33989 + * You should have received a copy of the GNU General Public License
33990 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33991 + */
33992 +
33993 +/*
33994 + * policies for selecting one among multiple writable branches
33995 + */
33996 +
33997 +#include <linux/statfs.h>
33998 +#include "aufs.h"
33999 +
34000 +/* subset of cpup_attr() */
34001 +static noinline_for_stack
34002 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34003 +{
34004 +       int err, sbits;
34005 +       struct iattr ia;
34006 +       struct inode *h_isrc;
34007 +
34008 +       h_isrc = d_inode(h_src);
34009 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34010 +       ia.ia_mode = h_isrc->i_mode;
34011 +       ia.ia_uid = h_isrc->i_uid;
34012 +       ia.ia_gid = h_isrc->i_gid;
34013 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34014 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34015 +       /* no delegation since it is just created */
34016 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34017 +
34018 +       /* is this nfs only? */
34019 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34020 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34021 +               ia.ia_mode = h_isrc->i_mode;
34022 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34023 +       }
34024 +
34025 +       return err;
34026 +}
34027 +
34028 +#define AuCpdown_PARENT_OPQ    1
34029 +#define AuCpdown_WHED          (1 << 1)
34030 +#define AuCpdown_MADE_DIR      (1 << 2)
34031 +#define AuCpdown_DIROPQ                (1 << 3)
34032 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34033 +#define au_fset_cpdown(flags, name) \
34034 +       do { (flags) |= AuCpdown_##name; } while (0)
34035 +#define au_fclr_cpdown(flags, name) \
34036 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34037 +
34038 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34039 +                            unsigned int *flags)
34040 +{
34041 +       int err;
34042 +       struct dentry *opq_dentry;
34043 +
34044 +       opq_dentry = au_diropq_create(dentry, bdst);
34045 +       err = PTR_ERR(opq_dentry);
34046 +       if (IS_ERR(opq_dentry))
34047 +               goto out;
34048 +       dput(opq_dentry);
34049 +       au_fset_cpdown(*flags, DIROPQ);
34050 +
34051 +out:
34052 +       return err;
34053 +}
34054 +
34055 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34056 +                           struct inode *dir, aufs_bindex_t bdst)
34057 +{
34058 +       int err;
34059 +       struct path h_path;
34060 +       struct au_branch *br;
34061 +
34062 +       br = au_sbr(dentry->d_sb, bdst);
34063 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34064 +       err = PTR_ERR(h_path.dentry);
34065 +       if (IS_ERR(h_path.dentry))
34066 +               goto out;
34067 +
34068 +       err = 0;
34069 +       if (d_is_positive(h_path.dentry)) {
34070 +               h_path.mnt = au_br_mnt(br);
34071 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34072 +                                         dentry);
34073 +       }
34074 +       dput(h_path.dentry);
34075 +
34076 +out:
34077 +       return err;
34078 +}
34079 +
34080 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34081 +                        struct au_pin *pin,
34082 +                        struct dentry *h_parent, void *arg)
34083 +{
34084 +       int err, rerr;
34085 +       aufs_bindex_t bopq, btop;
34086 +       struct path h_path;
34087 +       struct dentry *parent;
34088 +       struct inode *h_dir, *h_inode, *inode, *dir;
34089 +       unsigned int *flags = arg;
34090 +
34091 +       btop = au_dbtop(dentry);
34092 +       /* dentry is di-locked */
34093 +       parent = dget_parent(dentry);
34094 +       dir = d_inode(parent);
34095 +       h_dir = d_inode(h_parent);
34096 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34097 +       IMustLock(h_dir);
34098 +
34099 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34100 +       if (unlikely(err < 0))
34101 +               goto out;
34102 +       h_path.dentry = au_h_dptr(dentry, bdst);
34103 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34104 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34105 +       if (unlikely(err))
34106 +               goto out_put;
34107 +       au_fset_cpdown(*flags, MADE_DIR);
34108 +
34109 +       bopq = au_dbdiropq(dentry);
34110 +       au_fclr_cpdown(*flags, WHED);
34111 +       au_fclr_cpdown(*flags, DIROPQ);
34112 +       if (au_dbwh(dentry) == bdst)
34113 +               au_fset_cpdown(*flags, WHED);
34114 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34115 +               au_fset_cpdown(*flags, PARENT_OPQ);
34116 +       h_inode = d_inode(h_path.dentry);
34117 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34118 +       if (au_ftest_cpdown(*flags, WHED)) {
34119 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34120 +               if (unlikely(err)) {
34121 +                       inode_unlock(h_inode);
34122 +                       goto out_dir;
34123 +               }
34124 +       }
34125 +
34126 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34127 +       inode_unlock(h_inode);
34128 +       if (unlikely(err))
34129 +               goto out_opq;
34130 +
34131 +       if (au_ftest_cpdown(*flags, WHED)) {
34132 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34133 +               if (unlikely(err))
34134 +                       goto out_opq;
34135 +       }
34136 +
34137 +       inode = d_inode(dentry);
34138 +       if (au_ibbot(inode) < bdst)
34139 +               au_set_ibbot(inode, bdst);
34140 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34141 +                     au_hi_flags(inode, /*isdir*/1));
34142 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34143 +       goto out; /* success */
34144 +
34145 +       /* revert */
34146 +out_opq:
34147 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34148 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34149 +               rerr = au_diropq_remove(dentry, bdst);
34150 +               inode_unlock(h_inode);
34151 +               if (unlikely(rerr)) {
34152 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34153 +                               dentry, bdst, rerr);
34154 +                       err = -EIO;
34155 +                       goto out;
34156 +               }
34157 +       }
34158 +out_dir:
34159 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34160 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34161 +               if (unlikely(rerr)) {
34162 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34163 +                               dentry, bdst, rerr);
34164 +                       err = -EIO;
34165 +               }
34166 +       }
34167 +out_put:
34168 +       au_set_h_dptr(dentry, bdst, NULL);
34169 +       if (au_dbbot(dentry) == bdst)
34170 +               au_update_dbbot(dentry);
34171 +out:
34172 +       dput(parent);
34173 +       return err;
34174 +}
34175 +
34176 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34177 +{
34178 +       int err;
34179 +       unsigned int flags;
34180 +
34181 +       flags = 0;
34182 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34183 +
34184 +       return err;
34185 +}
34186 +
34187 +/* ---------------------------------------------------------------------- */
34188 +
34189 +/* policies for create */
34190 +
34191 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34192 +{
34193 +       int err, i, j, ndentry;
34194 +       aufs_bindex_t bopq;
34195 +       struct au_dcsub_pages dpages;
34196 +       struct au_dpage *dpage;
34197 +       struct dentry **dentries, *parent, *d;
34198 +
34199 +       err = au_dpages_init(&dpages, GFP_NOFS);
34200 +       if (unlikely(err))
34201 +               goto out;
34202 +       parent = dget_parent(dentry);
34203 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34204 +       if (unlikely(err))
34205 +               goto out_free;
34206 +
34207 +       err = bindex;
34208 +       for (i = 0; i < dpages.ndpage; i++) {
34209 +               dpage = dpages.dpages + i;
34210 +               dentries = dpage->dentries;
34211 +               ndentry = dpage->ndentry;
34212 +               for (j = 0; j < ndentry; j++) {
34213 +                       d = dentries[j];
34214 +                       di_read_lock_parent2(d, !AuLock_IR);
34215 +                       bopq = au_dbdiropq(d);
34216 +                       di_read_unlock(d, !AuLock_IR);
34217 +                       if (bopq >= 0 && bopq < err)
34218 +                               err = bopq;
34219 +               }
34220 +       }
34221 +
34222 +out_free:
34223 +       dput(parent);
34224 +       au_dpages_free(&dpages);
34225 +out:
34226 +       return err;
34227 +}
34228 +
34229 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34230 +{
34231 +       for (; bindex >= 0; bindex--)
34232 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34233 +                       return bindex;
34234 +       return -EROFS;
34235 +}
34236 +
34237 +/* top down parent */
34238 +static int au_wbr_create_tdp(struct dentry *dentry,
34239 +                            unsigned int flags __maybe_unused)
34240 +{
34241 +       int err;
34242 +       aufs_bindex_t btop, bindex;
34243 +       struct super_block *sb;
34244 +       struct dentry *parent, *h_parent;
34245 +
34246 +       sb = dentry->d_sb;
34247 +       btop = au_dbtop(dentry);
34248 +       err = btop;
34249 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34250 +               goto out;
34251 +
34252 +       err = -EROFS;
34253 +       parent = dget_parent(dentry);
34254 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34255 +               h_parent = au_h_dptr(parent, bindex);
34256 +               if (!h_parent || d_is_negative(h_parent))
34257 +                       continue;
34258 +
34259 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34260 +                       err = bindex;
34261 +                       break;
34262 +               }
34263 +       }
34264 +       dput(parent);
34265 +
34266 +       /* bottom up here */
34267 +       if (unlikely(err < 0)) {
34268 +               err = au_wbr_bu(sb, btop - 1);
34269 +               if (err >= 0)
34270 +                       err = au_wbr_nonopq(dentry, err);
34271 +       }
34272 +
34273 +out:
34274 +       AuDbg("b%d\n", err);
34275 +       return err;
34276 +}
34277 +
34278 +/* ---------------------------------------------------------------------- */
34279 +
34280 +/* an exception for the policy other than tdp */
34281 +static int au_wbr_create_exp(struct dentry *dentry)
34282 +{
34283 +       int err;
34284 +       aufs_bindex_t bwh, bdiropq;
34285 +       struct dentry *parent;
34286 +
34287 +       err = -1;
34288 +       bwh = au_dbwh(dentry);
34289 +       parent = dget_parent(dentry);
34290 +       bdiropq = au_dbdiropq(parent);
34291 +       if (bwh >= 0) {
34292 +               if (bdiropq >= 0)
34293 +                       err = min(bdiropq, bwh);
34294 +               else
34295 +                       err = bwh;
34296 +               AuDbg("%d\n", err);
34297 +       } else if (bdiropq >= 0) {
34298 +               err = bdiropq;
34299 +               AuDbg("%d\n", err);
34300 +       }
34301 +       dput(parent);
34302 +
34303 +       if (err >= 0)
34304 +               err = au_wbr_nonopq(dentry, err);
34305 +
34306 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34307 +               err = -1;
34308 +
34309 +       AuDbg("%d\n", err);
34310 +       return err;
34311 +}
34312 +
34313 +/* ---------------------------------------------------------------------- */
34314 +
34315 +/* round robin */
34316 +static int au_wbr_create_init_rr(struct super_block *sb)
34317 +{
34318 +       int err;
34319 +
34320 +       err = au_wbr_bu(sb, au_sbbot(sb));
34321 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34322 +       /* smp_mb(); */
34323 +
34324 +       AuDbg("b%d\n", err);
34325 +       return err;
34326 +}
34327 +
34328 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34329 +{
34330 +       int err, nbr;
34331 +       unsigned int u;
34332 +       aufs_bindex_t bindex, bbot;
34333 +       struct super_block *sb;
34334 +       atomic_t *next;
34335 +
34336 +       err = au_wbr_create_exp(dentry);
34337 +       if (err >= 0)
34338 +               goto out;
34339 +
34340 +       sb = dentry->d_sb;
34341 +       next = &au_sbi(sb)->si_wbr_rr_next;
34342 +       bbot = au_sbbot(sb);
34343 +       nbr = bbot + 1;
34344 +       for (bindex = 0; bindex <= bbot; bindex++) {
34345 +               if (!au_ftest_wbr(flags, DIR)) {
34346 +                       err = atomic_dec_return(next) + 1;
34347 +                       /* modulo for 0 is meaningless */
34348 +                       if (unlikely(!err))
34349 +                               err = atomic_dec_return(next) + 1;
34350 +               } else
34351 +                       err = atomic_read(next);
34352 +               AuDbg("%d\n", err);
34353 +               u = err;
34354 +               err = u % nbr;
34355 +               AuDbg("%d\n", err);
34356 +               if (!au_br_rdonly(au_sbr(sb, err)))
34357 +                       break;
34358 +               err = -EROFS;
34359 +       }
34360 +
34361 +       if (err >= 0)
34362 +               err = au_wbr_nonopq(dentry, err);
34363 +
34364 +out:
34365 +       AuDbg("%d\n", err);
34366 +       return err;
34367 +}
34368 +
34369 +/* ---------------------------------------------------------------------- */
34370 +
34371 +/* most free space */
34372 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34373 +{
34374 +       struct super_block *sb;
34375 +       struct au_branch *br;
34376 +       struct au_wbr_mfs *mfs;
34377 +       struct dentry *h_parent;
34378 +       aufs_bindex_t bindex, bbot;
34379 +       int err;
34380 +       unsigned long long b, bavail;
34381 +       struct path h_path;
34382 +       /* reduce the stack usage */
34383 +       struct kstatfs *st;
34384 +
34385 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34386 +       if (unlikely(!st)) {
34387 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34388 +               return;
34389 +       }
34390 +
34391 +       bavail = 0;
34392 +       sb = dentry->d_sb;
34393 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34394 +       MtxMustLock(&mfs->mfs_lock);
34395 +       mfs->mfs_bindex = -EROFS;
34396 +       mfs->mfsrr_bytes = 0;
34397 +       if (!parent) {
34398 +               bindex = 0;
34399 +               bbot = au_sbbot(sb);
34400 +       } else {
34401 +               bindex = au_dbtop(parent);
34402 +               bbot = au_dbtaildir(parent);
34403 +       }
34404 +
34405 +       for (; bindex <= bbot; bindex++) {
34406 +               if (parent) {
34407 +                       h_parent = au_h_dptr(parent, bindex);
34408 +                       if (!h_parent || d_is_negative(h_parent))
34409 +                               continue;
34410 +               }
34411 +               br = au_sbr(sb, bindex);
34412 +               if (au_br_rdonly(br))
34413 +                       continue;
34414 +
34415 +               /* sb->s_root for NFS is unreliable */
34416 +               h_path.mnt = au_br_mnt(br);
34417 +               h_path.dentry = h_path.mnt->mnt_root;
34418 +               err = vfs_statfs(&h_path, st);
34419 +               if (unlikely(err)) {
34420 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34421 +                       continue;
34422 +               }
34423 +
34424 +               /* when the available size is equal, select the lower one */
34425 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34426 +                            || sizeof(b) < sizeof(st->f_bsize));
34427 +               b = st->f_bavail * st->f_bsize;
34428 +               br->br_wbr->wbr_bytes = b;
34429 +               if (b >= bavail) {
34430 +                       bavail = b;
34431 +                       mfs->mfs_bindex = bindex;
34432 +                       mfs->mfs_jiffy = jiffies;
34433 +               }
34434 +       }
34435 +
34436 +       mfs->mfsrr_bytes = bavail;
34437 +       AuDbg("b%d\n", mfs->mfs_bindex);
34438 +       au_kfree_rcu(st);
34439 +}
34440 +
34441 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34442 +{
34443 +       int err;
34444 +       struct dentry *parent;
34445 +       struct super_block *sb;
34446 +       struct au_wbr_mfs *mfs;
34447 +
34448 +       err = au_wbr_create_exp(dentry);
34449 +       if (err >= 0)
34450 +               goto out;
34451 +
34452 +       sb = dentry->d_sb;
34453 +       parent = NULL;
34454 +       if (au_ftest_wbr(flags, PARENT))
34455 +               parent = dget_parent(dentry);
34456 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34457 +       mutex_lock(&mfs->mfs_lock);
34458 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34459 +           || mfs->mfs_bindex < 0
34460 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34461 +               au_mfs(dentry, parent);
34462 +       mutex_unlock(&mfs->mfs_lock);
34463 +       err = mfs->mfs_bindex;
34464 +       dput(parent);
34465 +
34466 +       if (err >= 0)
34467 +               err = au_wbr_nonopq(dentry, err);
34468 +
34469 +out:
34470 +       AuDbg("b%d\n", err);
34471 +       return err;
34472 +}
34473 +
34474 +static int au_wbr_create_init_mfs(struct super_block *sb)
34475 +{
34476 +       struct au_wbr_mfs *mfs;
34477 +
34478 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34479 +       mutex_init(&mfs->mfs_lock);
34480 +       mfs->mfs_jiffy = 0;
34481 +       mfs->mfs_bindex = -EROFS;
34482 +
34483 +       return 0;
34484 +}
34485 +
34486 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34487 +{
34488 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34489 +       return 0;
34490 +}
34491 +
34492 +/* ---------------------------------------------------------------------- */
34493 +
34494 +/* top down regardless parent, and then mfs */
34495 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34496 +                              unsigned int flags __maybe_unused)
34497 +{
34498 +       int err;
34499 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34500 +       unsigned long long watermark;
34501 +       struct super_block *sb;
34502 +       struct au_wbr_mfs *mfs;
34503 +       struct au_branch *br;
34504 +       struct dentry *parent;
34505 +
34506 +       sb = dentry->d_sb;
34507 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34508 +       mutex_lock(&mfs->mfs_lock);
34509 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34510 +           || mfs->mfs_bindex < 0)
34511 +               au_mfs(dentry, /*parent*/NULL);
34512 +       watermark = mfs->mfsrr_watermark;
34513 +       bmfs = mfs->mfs_bindex;
34514 +       mutex_unlock(&mfs->mfs_lock);
34515 +
34516 +       /* another style of au_wbr_create_exp() */
34517 +       bwh = au_dbwh(dentry);
34518 +       parent = dget_parent(dentry);
34519 +       btail = au_dbtaildir(parent);
34520 +       if (bwh >= 0 && bwh < btail)
34521 +               btail = bwh;
34522 +
34523 +       err = au_wbr_nonopq(dentry, btail);
34524 +       if (unlikely(err < 0))
34525 +               goto out;
34526 +       btail = err;
34527 +       bfound = -1;
34528 +       for (bindex = 0; bindex <= btail; bindex++) {
34529 +               br = au_sbr(sb, bindex);
34530 +               if (au_br_rdonly(br))
34531 +                       continue;
34532 +               if (br->br_wbr->wbr_bytes > watermark) {
34533 +                       bfound = bindex;
34534 +                       break;
34535 +               }
34536 +       }
34537 +       err = bfound;
34538 +       if (err < 0)
34539 +               err = bmfs;
34540 +
34541 +out:
34542 +       dput(parent);
34543 +       AuDbg("b%d\n", err);
34544 +       return err;
34545 +}
34546 +
34547 +/* ---------------------------------------------------------------------- */
34548 +
34549 +/* most free space and then round robin */
34550 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34551 +{
34552 +       int err;
34553 +       struct au_wbr_mfs *mfs;
34554 +
34555 +       err = au_wbr_create_mfs(dentry, flags);
34556 +       if (err >= 0) {
34557 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34558 +               mutex_lock(&mfs->mfs_lock);
34559 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34560 +                       err = au_wbr_create_rr(dentry, flags);
34561 +               mutex_unlock(&mfs->mfs_lock);
34562 +       }
34563 +
34564 +       AuDbg("b%d\n", err);
34565 +       return err;
34566 +}
34567 +
34568 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34569 +{
34570 +       int err;
34571 +
34572 +       au_wbr_create_init_mfs(sb); /* ignore */
34573 +       err = au_wbr_create_init_rr(sb);
34574 +
34575 +       return err;
34576 +}
34577 +
34578 +/* ---------------------------------------------------------------------- */
34579 +
34580 +/* top down parent and most free space */
34581 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34582 +{
34583 +       int err, e2;
34584 +       unsigned long long b;
34585 +       aufs_bindex_t bindex, btop, bbot;
34586 +       struct super_block *sb;
34587 +       struct dentry *parent, *h_parent;
34588 +       struct au_branch *br;
34589 +
34590 +       err = au_wbr_create_tdp(dentry, flags);
34591 +       if (unlikely(err < 0))
34592 +               goto out;
34593 +       parent = dget_parent(dentry);
34594 +       btop = au_dbtop(parent);
34595 +       bbot = au_dbtaildir(parent);
34596 +       if (btop == bbot)
34597 +               goto out_parent; /* success */
34598 +
34599 +       e2 = au_wbr_create_mfs(dentry, flags);
34600 +       if (e2 < 0)
34601 +               goto out_parent; /* success */
34602 +
34603 +       /* when the available size is equal, select upper one */
34604 +       sb = dentry->d_sb;
34605 +       br = au_sbr(sb, err);
34606 +       b = br->br_wbr->wbr_bytes;
34607 +       AuDbg("b%d, %llu\n", err, b);
34608 +
34609 +       for (bindex = btop; bindex <= bbot; bindex++) {
34610 +               h_parent = au_h_dptr(parent, bindex);
34611 +               if (!h_parent || d_is_negative(h_parent))
34612 +                       continue;
34613 +
34614 +               br = au_sbr(sb, bindex);
34615 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34616 +                       b = br->br_wbr->wbr_bytes;
34617 +                       err = bindex;
34618 +                       AuDbg("b%d, %llu\n", err, b);
34619 +               }
34620 +       }
34621 +
34622 +       if (err >= 0)
34623 +               err = au_wbr_nonopq(dentry, err);
34624 +
34625 +out_parent:
34626 +       dput(parent);
34627 +out:
34628 +       AuDbg("b%d\n", err);
34629 +       return err;
34630 +}
34631 +
34632 +/* ---------------------------------------------------------------------- */
34633 +
34634 +/*
34635 + * - top down parent
34636 + * - most free space with parent
34637 + * - most free space round-robin regardless parent
34638 + */
34639 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34640 +{
34641 +       int err;
34642 +       unsigned long long watermark;
34643 +       struct super_block *sb;
34644 +       struct au_branch *br;
34645 +       struct au_wbr_mfs *mfs;
34646 +
34647 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34648 +       if (unlikely(err < 0))
34649 +               goto out;
34650 +
34651 +       sb = dentry->d_sb;
34652 +       br = au_sbr(sb, err);
34653 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34654 +       mutex_lock(&mfs->mfs_lock);
34655 +       watermark = mfs->mfsrr_watermark;
34656 +       mutex_unlock(&mfs->mfs_lock);
34657 +       if (br->br_wbr->wbr_bytes < watermark)
34658 +               /* regardless the parent dir */
34659 +               err = au_wbr_create_mfsrr(dentry, flags);
34660 +
34661 +out:
34662 +       AuDbg("b%d\n", err);
34663 +       return err;
34664 +}
34665 +
34666 +/* ---------------------------------------------------------------------- */
34667 +
34668 +/* policies for copyup */
34669 +
34670 +/* top down parent */
34671 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34672 +{
34673 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34674 +}
34675 +
34676 +/* bottom up parent */
34677 +static int au_wbr_copyup_bup(struct dentry *dentry)
34678 +{
34679 +       int err;
34680 +       aufs_bindex_t bindex, btop;
34681 +       struct dentry *parent, *h_parent;
34682 +       struct super_block *sb;
34683 +
34684 +       err = -EROFS;
34685 +       sb = dentry->d_sb;
34686 +       parent = dget_parent(dentry);
34687 +       btop = au_dbtop(parent);
34688 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34689 +               h_parent = au_h_dptr(parent, bindex);
34690 +               if (!h_parent || d_is_negative(h_parent))
34691 +                       continue;
34692 +
34693 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34694 +                       err = bindex;
34695 +                       break;
34696 +               }
34697 +       }
34698 +       dput(parent);
34699 +
34700 +       /* bottom up here */
34701 +       if (unlikely(err < 0))
34702 +               err = au_wbr_bu(sb, btop - 1);
34703 +
34704 +       AuDbg("b%d\n", err);
34705 +       return err;
34706 +}
34707 +
34708 +/* bottom up */
34709 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34710 +{
34711 +       int err;
34712 +
34713 +       err = au_wbr_bu(dentry->d_sb, btop);
34714 +       AuDbg("b%d\n", err);
34715 +       if (err > btop)
34716 +               err = au_wbr_nonopq(dentry, err);
34717 +
34718 +       AuDbg("b%d\n", err);
34719 +       return err;
34720 +}
34721 +
34722 +static int au_wbr_copyup_bu(struct dentry *dentry)
34723 +{
34724 +       int err;
34725 +       aufs_bindex_t btop;
34726 +
34727 +       btop = au_dbtop(dentry);
34728 +       err = au_wbr_do_copyup_bu(dentry, btop);
34729 +       return err;
34730 +}
34731 +
34732 +/* ---------------------------------------------------------------------- */
34733 +
34734 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34735 +       [AuWbrCopyup_TDP] = {
34736 +               .copyup = au_wbr_copyup_tdp
34737 +       },
34738 +       [AuWbrCopyup_BUP] = {
34739 +               .copyup = au_wbr_copyup_bup
34740 +       },
34741 +       [AuWbrCopyup_BU] = {
34742 +               .copyup = au_wbr_copyup_bu
34743 +       }
34744 +};
34745 +
34746 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34747 +       [AuWbrCreate_TDP] = {
34748 +               .create = au_wbr_create_tdp
34749 +       },
34750 +       [AuWbrCreate_RR] = {
34751 +               .create = au_wbr_create_rr,
34752 +               .init   = au_wbr_create_init_rr
34753 +       },
34754 +       [AuWbrCreate_MFS] = {
34755 +               .create = au_wbr_create_mfs,
34756 +               .init   = au_wbr_create_init_mfs,
34757 +               .fin    = au_wbr_create_fin_mfs
34758 +       },
34759 +       [AuWbrCreate_MFSV] = {
34760 +               .create = au_wbr_create_mfs,
34761 +               .init   = au_wbr_create_init_mfs,
34762 +               .fin    = au_wbr_create_fin_mfs
34763 +       },
34764 +       [AuWbrCreate_MFSRR] = {
34765 +               .create = au_wbr_create_mfsrr,
34766 +               .init   = au_wbr_create_init_mfsrr,
34767 +               .fin    = au_wbr_create_fin_mfs
34768 +       },
34769 +       [AuWbrCreate_MFSRRV] = {
34770 +               .create = au_wbr_create_mfsrr,
34771 +               .init   = au_wbr_create_init_mfsrr,
34772 +               .fin    = au_wbr_create_fin_mfs
34773 +       },
34774 +       [AuWbrCreate_TDMFS] = {
34775 +               .create = au_wbr_create_tdmfs,
34776 +               .init   = au_wbr_create_init_mfs,
34777 +               .fin    = au_wbr_create_fin_mfs
34778 +       },
34779 +       [AuWbrCreate_TDMFSV] = {
34780 +               .create = au_wbr_create_tdmfs,
34781 +               .init   = au_wbr_create_init_mfs,
34782 +               .fin    = au_wbr_create_fin_mfs
34783 +       },
34784 +       [AuWbrCreate_PMFS] = {
34785 +               .create = au_wbr_create_pmfs,
34786 +               .init   = au_wbr_create_init_mfs,
34787 +               .fin    = au_wbr_create_fin_mfs
34788 +       },
34789 +       [AuWbrCreate_PMFSV] = {
34790 +               .create = au_wbr_create_pmfs,
34791 +               .init   = au_wbr_create_init_mfs,
34792 +               .fin    = au_wbr_create_fin_mfs
34793 +       },
34794 +       [AuWbrCreate_PMFSRR] = {
34795 +               .create = au_wbr_create_pmfsrr,
34796 +               .init   = au_wbr_create_init_mfsrr,
34797 +               .fin    = au_wbr_create_fin_mfs
34798 +       },
34799 +       [AuWbrCreate_PMFSRRV] = {
34800 +               .create = au_wbr_create_pmfsrr,
34801 +               .init   = au_wbr_create_init_mfsrr,
34802 +               .fin    = au_wbr_create_fin_mfs
34803 +       }
34804 +};
34805 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34806 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34807 +++ linux/fs/aufs/whout.c       2020-01-27 10:57:18.178871751 +0100
34808 @@ -0,0 +1,1062 @@
34809 +// SPDX-License-Identifier: GPL-2.0
34810 +/*
34811 + * Copyright (C) 2005-2020 Junjiro R. Okajima
34812 + *
34813 + * This program, aufs is free software; you can redistribute it and/or modify
34814 + * it under the terms of the GNU General Public License as published by
34815 + * the Free Software Foundation; either version 2 of the License, or
34816 + * (at your option) any later version.
34817 + *
34818 + * This program is distributed in the hope that it will be useful,
34819 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34820 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34821 + * GNU General Public License for more details.
34822 + *
34823 + * You should have received a copy of the GNU General Public License
34824 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34825 + */
34826 +
34827 +/*
34828 + * whiteout for logical deletion and opaque directory
34829 + */
34830 +
34831 +#include "aufs.h"
34832 +
34833 +#define WH_MASK                        0444
34834 +
34835 +/*
34836 + * If a directory contains this file, then it is opaque.  We start with the
34837 + * .wh. flag so that it is blocked by lookup.
34838 + */
34839 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34840 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34841 +
34842 +/*
34843 + * generate whiteout name, which is NOT terminated by NULL.
34844 + * @name: original d_name.name
34845 + * @len: original d_name.len
34846 + * @wh: whiteout qstr
34847 + * returns zero when succeeds, otherwise error.
34848 + * succeeded value as wh->name should be freed by kfree().
34849 + */
34850 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34851 +{
34852 +       char *p;
34853 +
34854 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34855 +               return -ENAMETOOLONG;
34856 +
34857 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34858 +       p = kmalloc(wh->len, GFP_NOFS);
34859 +       wh->name = p;
34860 +       if (p) {
34861 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34862 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34863 +               /* smp_mb(); */
34864 +               return 0;
34865 +       }
34866 +       return -ENOMEM;
34867 +}
34868 +
34869 +/* ---------------------------------------------------------------------- */
34870 +
34871 +/*
34872 + * test if the @wh_name exists under @h_parent.
34873 + * @try_sio specifies the necessary of super-io.
34874 + */
34875 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34876 +{
34877 +       int err;
34878 +       struct dentry *wh_dentry;
34879 +
34880 +       if (!try_sio)
34881 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34882 +       else
34883 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34884 +       err = PTR_ERR(wh_dentry);
34885 +       if (IS_ERR(wh_dentry)) {
34886 +               if (err == -ENAMETOOLONG)
34887 +                       err = 0;
34888 +               goto out;
34889 +       }
34890 +
34891 +       err = 0;
34892 +       if (d_is_negative(wh_dentry))
34893 +               goto out_wh; /* success */
34894 +
34895 +       err = 1;
34896 +       if (d_is_reg(wh_dentry))
34897 +               goto out_wh; /* success */
34898 +
34899 +       err = -EIO;
34900 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34901 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34902 +
34903 +out_wh:
34904 +       dput(wh_dentry);
34905 +out:
34906 +       return err;
34907 +}
34908 +
34909 +/*
34910 + * test if the @h_dentry sets opaque or not.
34911 + */
34912 +int au_diropq_test(struct dentry *h_dentry)
34913 +{
34914 +       int err;
34915 +       struct inode *h_dir;
34916 +
34917 +       h_dir = d_inode(h_dentry);
34918 +       err = au_wh_test(h_dentry, &diropq_name,
34919 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34920 +       return err;
34921 +}
34922 +
34923 +/*
34924 + * returns a negative dentry whose name is unique and temporary.
34925 + */
34926 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34927 +                            struct qstr *prefix)
34928 +{
34929 +       struct dentry *dentry;
34930 +       int i;
34931 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34932 +               *name, *p;
34933 +       /* strict atomic_t is unnecessary here */
34934 +       static unsigned short cnt;
34935 +       struct qstr qs;
34936 +
34937 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34938 +
34939 +       name = defname;
34940 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34941 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34942 +               dentry = ERR_PTR(-ENAMETOOLONG);
34943 +               if (unlikely(qs.len > NAME_MAX))
34944 +                       goto out;
34945 +               dentry = ERR_PTR(-ENOMEM);
34946 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34947 +               if (unlikely(!name))
34948 +                       goto out;
34949 +       }
34950 +
34951 +       /* doubly whiteout-ed */
34952 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34953 +       p = name + AUFS_WH_PFX_LEN * 2;
34954 +       memcpy(p, prefix->name, prefix->len);
34955 +       p += prefix->len;
34956 +       *p++ = '.';
34957 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34958 +
34959 +       qs.name = name;
34960 +       for (i = 0; i < 3; i++) {
34961 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34962 +               dentry = au_sio_lkup_one(&qs, h_parent);
34963 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34964 +                       goto out_name;
34965 +               dput(dentry);
34966 +       }
34967 +       /* pr_warn("could not get random name\n"); */
34968 +       dentry = ERR_PTR(-EEXIST);
34969 +       AuDbg("%.*s\n", AuLNPair(&qs));
34970 +       BUG();
34971 +
34972 +out_name:
34973 +       if (name != defname)
34974 +               au_kfree_try_rcu(name);
34975 +out:
34976 +       AuTraceErrPtr(dentry);
34977 +       return dentry;
34978 +}
34979 +
34980 +/*
34981 + * rename the @h_dentry on @br to the whiteouted temporary name.
34982 + */
34983 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34984 +{
34985 +       int err;
34986 +       struct path h_path = {
34987 +               .mnt = au_br_mnt(br)
34988 +       };
34989 +       struct inode *h_dir, *delegated;
34990 +       struct dentry *h_parent;
34991 +
34992 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34993 +       h_dir = d_inode(h_parent);
34994 +       IMustLock(h_dir);
34995 +
34996 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34997 +       err = PTR_ERR(h_path.dentry);
34998 +       if (IS_ERR(h_path.dentry))
34999 +               goto out;
35000 +
35001 +       /* under the same dir, no need to lock_rename() */
35002 +       delegated = NULL;
35003 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35004 +                          /*flags*/0);
35005 +       AuTraceErr(err);
35006 +       if (unlikely(err == -EWOULDBLOCK)) {
35007 +               pr_warn("cannot retry for NFSv4 delegation"
35008 +                       " for an internal rename\n");
35009 +               iput(delegated);
35010 +       }
35011 +       dput(h_path.dentry);
35012 +
35013 +out:
35014 +       AuTraceErr(err);
35015 +       return err;
35016 +}
35017 +
35018 +/* ---------------------------------------------------------------------- */
35019 +/*
35020 + * functions for removing a whiteout
35021 + */
35022 +
35023 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35024 +{
35025 +       int err, force;
35026 +       struct inode *delegated;
35027 +
35028 +       /*
35029 +        * forces superio when the dir has a sticky bit.
35030 +        * this may be a violation of unix fs semantics.
35031 +        */
35032 +       force = (h_dir->i_mode & S_ISVTX)
35033 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35034 +       delegated = NULL;
35035 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35036 +       if (unlikely(err == -EWOULDBLOCK)) {
35037 +               pr_warn("cannot retry for NFSv4 delegation"
35038 +                       " for an internal unlink\n");
35039 +               iput(delegated);
35040 +       }
35041 +       return err;
35042 +}
35043 +
35044 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35045 +                       struct dentry *dentry)
35046 +{
35047 +       int err;
35048 +
35049 +       err = do_unlink_wh(h_dir, h_path);
35050 +       if (!err && dentry)
35051 +               au_set_dbwh(dentry, -1);
35052 +
35053 +       return err;
35054 +}
35055 +
35056 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35057 +                         struct au_branch *br)
35058 +{
35059 +       int err;
35060 +       struct path h_path = {
35061 +               .mnt = au_br_mnt(br)
35062 +       };
35063 +
35064 +       err = 0;
35065 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
35066 +       if (IS_ERR(h_path.dentry))
35067 +               err = PTR_ERR(h_path.dentry);
35068 +       else {
35069 +               if (d_is_reg(h_path.dentry))
35070 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
35071 +               dput(h_path.dentry);
35072 +       }
35073 +
35074 +       return err;
35075 +}
35076 +
35077 +/* ---------------------------------------------------------------------- */
35078 +/*
35079 + * initialize/clean whiteout for a branch
35080 + */
35081 +
35082 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35083 +                       const int isdir)
35084 +{
35085 +       int err;
35086 +       struct inode *delegated;
35087 +
35088 +       if (d_is_negative(whpath->dentry))
35089 +               return;
35090 +
35091 +       if (isdir)
35092 +               err = vfsub_rmdir(h_dir, whpath);
35093 +       else {
35094 +               delegated = NULL;
35095 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35096 +               if (unlikely(err == -EWOULDBLOCK)) {
35097 +                       pr_warn("cannot retry for NFSv4 delegation"
35098 +                               " for an internal unlink\n");
35099 +                       iput(delegated);
35100 +               }
35101 +       }
35102 +       if (unlikely(err))
35103 +               pr_warn("failed removing %pd (%d), ignored.\n",
35104 +                       whpath->dentry, err);
35105 +}
35106 +
35107 +static int test_linkable(struct dentry *h_root)
35108 +{
35109 +       struct inode *h_dir = d_inode(h_root);
35110 +
35111 +       if (h_dir->i_op->link)
35112 +               return 0;
35113 +
35114 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35115 +              h_root, au_sbtype(h_root->d_sb));
35116 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35117 +}
35118 +
35119 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35120 +static int au_whdir(struct inode *h_dir, struct path *path)
35121 +{
35122 +       int err;
35123 +
35124 +       err = -EEXIST;
35125 +       if (d_is_negative(path->dentry)) {
35126 +               int mode = 0700;
35127 +
35128 +               if (au_test_nfs(path->dentry->d_sb))
35129 +                       mode |= 0111;
35130 +               err = vfsub_mkdir(h_dir, path, mode);
35131 +       } else if (d_is_dir(path->dentry))
35132 +               err = 0;
35133 +       else
35134 +               pr_err("unknown %pd exists\n", path->dentry);
35135 +
35136 +       return err;
35137 +}
35138 +
35139 +struct au_wh_base {
35140 +       const struct qstr *name;
35141 +       struct dentry *dentry;
35142 +};
35143 +
35144 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35145 +                         struct path *h_path)
35146 +{
35147 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35148 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35149 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35150 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35151 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35152 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35153 +}
35154 +
35155 +/*
35156 + * returns tri-state,
35157 + * minus: error, caller should print the message
35158 + * zero: success
35159 + * plus: error, caller should NOT print the message
35160 + */
35161 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35162 +                               int do_plink, struct au_wh_base base[],
35163 +                               struct path *h_path)
35164 +{
35165 +       int err;
35166 +       struct inode *h_dir;
35167 +
35168 +       h_dir = d_inode(h_root);
35169 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35170 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35171 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35172 +       if (do_plink) {
35173 +               err = test_linkable(h_root);
35174 +               if (unlikely(err)) {
35175 +                       err = 1;
35176 +                       goto out;
35177 +               }
35178 +
35179 +               err = au_whdir(h_dir, h_path);
35180 +               if (unlikely(err))
35181 +                       goto out;
35182 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35183 +       } else
35184 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35185 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35186 +       err = au_whdir(h_dir, h_path);
35187 +       if (unlikely(err))
35188 +               goto out;
35189 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35190 +
35191 +out:
35192 +       return err;
35193 +}
35194 +
35195 +/*
35196 + * for the moment, aufs supports the branch filesystem which does not support
35197 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35198 + * copyup failed. finally, such filesystem will not be used as the writable
35199 + * branch.
35200 + *
35201 + * returns tri-state, see above.
35202 + */
35203 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35204 +                        int do_plink, struct au_wh_base base[],
35205 +                        struct path *h_path)
35206 +{
35207 +       int err;
35208 +       struct inode *h_dir;
35209 +
35210 +       WbrWhMustWriteLock(wbr);
35211 +
35212 +       err = test_linkable(h_root);
35213 +       if (unlikely(err)) {
35214 +               err = 1;
35215 +               goto out;
35216 +       }
35217 +
35218 +       /*
35219 +        * todo: should this create be done in /sbin/mount.aufs helper?
35220 +        */
35221 +       err = -EEXIST;
35222 +       h_dir = d_inode(h_root);
35223 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35224 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35225 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35226 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35227 +               err = 0;
35228 +       else
35229 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35230 +       if (unlikely(err))
35231 +               goto out;
35232 +
35233 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35234 +       if (do_plink) {
35235 +               err = au_whdir(h_dir, h_path);
35236 +               if (unlikely(err))
35237 +                       goto out;
35238 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35239 +       } else
35240 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35241 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35242 +
35243 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35244 +       err = au_whdir(h_dir, h_path);
35245 +       if (unlikely(err))
35246 +               goto out;
35247 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35248 +
35249 +out:
35250 +       return err;
35251 +}
35252 +
35253 +/*
35254 + * initialize the whiteout base file/dir for @br.
35255 + */
35256 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35257 +{
35258 +       int err, i;
35259 +       const unsigned char do_plink
35260 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35261 +       struct inode *h_dir;
35262 +       struct path path = br->br_path;
35263 +       struct dentry *h_root = path.dentry;
35264 +       struct au_wbr *wbr = br->br_wbr;
35265 +       static const struct qstr base_name[] = {
35266 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35267 +                                         sizeof(AUFS_BASE_NAME) - 1),
35268 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35269 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35270 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35271 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35272 +       };
35273 +       struct au_wh_base base[] = {
35274 +               [AuBrWh_BASE] = {
35275 +                       .name   = base_name + AuBrWh_BASE,
35276 +                       .dentry = NULL
35277 +               },
35278 +               [AuBrWh_PLINK] = {
35279 +                       .name   = base_name + AuBrWh_PLINK,
35280 +                       .dentry = NULL
35281 +               },
35282 +               [AuBrWh_ORPH] = {
35283 +                       .name   = base_name + AuBrWh_ORPH,
35284 +                       .dentry = NULL
35285 +               }
35286 +       };
35287 +
35288 +       if (wbr)
35289 +               WbrWhMustWriteLock(wbr);
35290 +
35291 +       for (i = 0; i < AuBrWh_Last; i++) {
35292 +               /* doubly whiteouted */
35293 +               struct dentry *d;
35294 +
35295 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35296 +               err = PTR_ERR(d);
35297 +               if (IS_ERR(d))
35298 +                       goto out;
35299 +
35300 +               base[i].dentry = d;
35301 +               AuDebugOn(wbr
35302 +                         && wbr->wbr_wh[i]
35303 +                         && wbr->wbr_wh[i] != base[i].dentry);
35304 +       }
35305 +
35306 +       if (wbr)
35307 +               for (i = 0; i < AuBrWh_Last; i++) {
35308 +                       dput(wbr->wbr_wh[i]);
35309 +                       wbr->wbr_wh[i] = NULL;
35310 +               }
35311 +
35312 +       err = 0;
35313 +       if (!au_br_writable(br->br_perm)) {
35314 +               h_dir = d_inode(h_root);
35315 +               au_wh_init_ro(h_dir, base, &path);
35316 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35317 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35318 +               if (err > 0)
35319 +                       goto out;
35320 +               else if (err)
35321 +                       goto out_err;
35322 +       } else {
35323 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35324 +               if (err > 0)
35325 +                       goto out;
35326 +               else if (err)
35327 +                       goto out_err;
35328 +       }
35329 +       goto out; /* success */
35330 +
35331 +out_err:
35332 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35333 +              err, h_root, au_sbtype(h_root->d_sb));
35334 +out:
35335 +       for (i = 0; i < AuBrWh_Last; i++)
35336 +               dput(base[i].dentry);
35337 +       return err;
35338 +}
35339 +
35340 +/* ---------------------------------------------------------------------- */
35341 +/*
35342 + * whiteouts are all hard-linked usually.
35343 + * when its link count reaches a ceiling, we create a new whiteout base
35344 + * asynchronously.
35345 + */
35346 +
35347 +struct reinit_br_wh {
35348 +       struct super_block *sb;
35349 +       struct au_branch *br;
35350 +};
35351 +
35352 +static void reinit_br_wh(void *arg)
35353 +{
35354 +       int err;
35355 +       aufs_bindex_t bindex;
35356 +       struct path h_path;
35357 +       struct reinit_br_wh *a = arg;
35358 +       struct au_wbr *wbr;
35359 +       struct inode *dir, *delegated;
35360 +       struct dentry *h_root;
35361 +       struct au_hinode *hdir;
35362 +
35363 +       err = 0;
35364 +       wbr = a->br->br_wbr;
35365 +       /* big aufs lock */
35366 +       si_noflush_write_lock(a->sb);
35367 +       if (!au_br_writable(a->br->br_perm))
35368 +               goto out;
35369 +       bindex = au_br_index(a->sb, a->br->br_id);
35370 +       if (unlikely(bindex < 0))
35371 +               goto out;
35372 +
35373 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35374 +       dir = d_inode(a->sb->s_root);
35375 +       hdir = au_hi(dir, bindex);
35376 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35377 +       AuDebugOn(h_root != au_br_dentry(a->br));
35378 +
35379 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35380 +       wbr_wh_write_lock(wbr);
35381 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35382 +                         h_root, a->br);
35383 +       if (!err) {
35384 +               h_path.dentry = wbr->wbr_whbase;
35385 +               h_path.mnt = au_br_mnt(a->br);
35386 +               delegated = NULL;
35387 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35388 +                                  /*force*/0);
35389 +               if (unlikely(err == -EWOULDBLOCK)) {
35390 +                       pr_warn("cannot retry for NFSv4 delegation"
35391 +                               " for an internal unlink\n");
35392 +                       iput(delegated);
35393 +               }
35394 +       } else {
35395 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35396 +               err = 0;
35397 +       }
35398 +       dput(wbr->wbr_whbase);
35399 +       wbr->wbr_whbase = NULL;
35400 +       if (!err)
35401 +               err = au_wh_init(a->br, a->sb);
35402 +       wbr_wh_write_unlock(wbr);
35403 +       au_hn_inode_unlock(hdir);
35404 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35405 +       if (!err)
35406 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35407 +
35408 +out:
35409 +       if (wbr)
35410 +               atomic_dec(&wbr->wbr_wh_running);
35411 +       au_lcnt_dec(&a->br->br_count);
35412 +       si_write_unlock(a->sb);
35413 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35414 +       au_kfree_rcu(a);
35415 +       if (unlikely(err))
35416 +               AuIOErr("err %d\n", err);
35417 +}
35418 +
35419 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35420 +{
35421 +       int do_dec, wkq_err;
35422 +       struct reinit_br_wh *arg;
35423 +
35424 +       do_dec = 1;
35425 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35426 +               goto out;
35427 +
35428 +       /* ignore ENOMEM */
35429 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35430 +       if (arg) {
35431 +               /*
35432 +                * dec(wh_running), kfree(arg) and dec(br_count)
35433 +                * in reinit function
35434 +                */
35435 +               arg->sb = sb;
35436 +               arg->br = br;
35437 +               au_lcnt_inc(&br->br_count);
35438 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35439 +               if (unlikely(wkq_err)) {
35440 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35441 +                       au_lcnt_dec(&br->br_count);
35442 +                       au_kfree_rcu(arg);
35443 +               }
35444 +               do_dec = 0;
35445 +       }
35446 +
35447 +out:
35448 +       if (do_dec)
35449 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35450 +}
35451 +
35452 +/* ---------------------------------------------------------------------- */
35453 +
35454 +/*
35455 + * create the whiteout @wh.
35456 + */
35457 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35458 +                            struct dentry *wh)
35459 +{
35460 +       int err;
35461 +       struct path h_path = {
35462 +               .dentry = wh
35463 +       };
35464 +       struct au_branch *br;
35465 +       struct au_wbr *wbr;
35466 +       struct dentry *h_parent;
35467 +       struct inode *h_dir, *delegated;
35468 +
35469 +       h_parent = wh->d_parent; /* dir inode is locked */
35470 +       h_dir = d_inode(h_parent);
35471 +       IMustLock(h_dir);
35472 +
35473 +       br = au_sbr(sb, bindex);
35474 +       h_path.mnt = au_br_mnt(br);
35475 +       wbr = br->br_wbr;
35476 +       wbr_wh_read_lock(wbr);
35477 +       if (wbr->wbr_whbase) {
35478 +               delegated = NULL;
35479 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35480 +               if (unlikely(err == -EWOULDBLOCK)) {
35481 +                       pr_warn("cannot retry for NFSv4 delegation"
35482 +                               " for an internal link\n");
35483 +                       iput(delegated);
35484 +               }
35485 +               if (!err || err != -EMLINK)
35486 +                       goto out;
35487 +
35488 +               /* link count full. re-initialize br_whbase. */
35489 +               kick_reinit_br_wh(sb, br);
35490 +       }
35491 +
35492 +       /* return this error in this context */
35493 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35494 +       if (!err)
35495 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35496 +
35497 +out:
35498 +       wbr_wh_read_unlock(wbr);
35499 +       return err;
35500 +}
35501 +
35502 +/* ---------------------------------------------------------------------- */
35503 +
35504 +/*
35505 + * create or remove the diropq.
35506 + */
35507 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35508 +                               unsigned int flags)
35509 +{
35510 +       struct dentry *opq_dentry, *h_dentry;
35511 +       struct super_block *sb;
35512 +       struct au_branch *br;
35513 +       int err;
35514 +
35515 +       sb = dentry->d_sb;
35516 +       br = au_sbr(sb, bindex);
35517 +       h_dentry = au_h_dptr(dentry, bindex);
35518 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35519 +       if (IS_ERR(opq_dentry))
35520 +               goto out;
35521 +
35522 +       if (au_ftest_diropq(flags, CREATE)) {
35523 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35524 +               if (!err) {
35525 +                       au_set_dbdiropq(dentry, bindex);
35526 +                       goto out; /* success */
35527 +               }
35528 +       } else {
35529 +               struct path tmp = {
35530 +                       .dentry = opq_dentry,
35531 +                       .mnt    = au_br_mnt(br)
35532 +               };
35533 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35534 +               if (!err)
35535 +                       au_set_dbdiropq(dentry, -1);
35536 +       }
35537 +       dput(opq_dentry);
35538 +       opq_dentry = ERR_PTR(err);
35539 +
35540 +out:
35541 +       return opq_dentry;
35542 +}
35543 +
35544 +struct do_diropq_args {
35545 +       struct dentry **errp;
35546 +       struct dentry *dentry;
35547 +       aufs_bindex_t bindex;
35548 +       unsigned int flags;
35549 +};
35550 +
35551 +static void call_do_diropq(void *args)
35552 +{
35553 +       struct do_diropq_args *a = args;
35554 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35555 +}
35556 +
35557 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35558 +                            unsigned int flags)
35559 +{
35560 +       struct dentry *diropq, *h_dentry;
35561 +
35562 +       h_dentry = au_h_dptr(dentry, bindex);
35563 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35564 +               diropq = do_diropq(dentry, bindex, flags);
35565 +       else {
35566 +               int wkq_err;
35567 +               struct do_diropq_args args = {
35568 +                       .errp           = &diropq,
35569 +                       .dentry         = dentry,
35570 +                       .bindex         = bindex,
35571 +                       .flags          = flags
35572 +               };
35573 +
35574 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35575 +               if (unlikely(wkq_err))
35576 +                       diropq = ERR_PTR(wkq_err);
35577 +       }
35578 +
35579 +       return diropq;
35580 +}
35581 +
35582 +/* ---------------------------------------------------------------------- */
35583 +
35584 +/*
35585 + * lookup whiteout dentry.
35586 + * @h_parent: lower parent dentry which must exist and be locked
35587 + * @base_name: name of dentry which will be whiteouted
35588 + * returns dentry for whiteout.
35589 + */
35590 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35591 +                         struct au_branch *br)
35592 +{
35593 +       int err;
35594 +       struct qstr wh_name;
35595 +       struct dentry *wh_dentry;
35596 +
35597 +       err = au_wh_name_alloc(&wh_name, base_name);
35598 +       wh_dentry = ERR_PTR(err);
35599 +       if (!err) {
35600 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35601 +               au_kfree_try_rcu(wh_name.name);
35602 +       }
35603 +       return wh_dentry;
35604 +}
35605 +
35606 +/*
35607 + * link/create a whiteout for @dentry on @bindex.
35608 + */
35609 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35610 +                           struct dentry *h_parent)
35611 +{
35612 +       struct dentry *wh_dentry;
35613 +       struct super_block *sb;
35614 +       int err;
35615 +
35616 +       sb = dentry->d_sb;
35617 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35618 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35619 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35620 +               if (!err) {
35621 +                       au_set_dbwh(dentry, bindex);
35622 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35623 +               } else {
35624 +                       dput(wh_dentry);
35625 +                       wh_dentry = ERR_PTR(err);
35626 +               }
35627 +       }
35628 +
35629 +       return wh_dentry;
35630 +}
35631 +
35632 +/* ---------------------------------------------------------------------- */
35633 +
35634 +/* Delete all whiteouts in this directory on branch bindex. */
35635 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35636 +                          aufs_bindex_t bindex, struct au_branch *br)
35637 +{
35638 +       int err;
35639 +       unsigned long ul, n;
35640 +       struct qstr wh_name;
35641 +       char *p;
35642 +       struct hlist_head *head;
35643 +       struct au_vdir_wh *pos;
35644 +       struct au_vdir_destr *str;
35645 +
35646 +       err = -ENOMEM;
35647 +       p = (void *)__get_free_page(GFP_NOFS);
35648 +       wh_name.name = p;
35649 +       if (unlikely(!wh_name.name))
35650 +               goto out;
35651 +
35652 +       err = 0;
35653 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35654 +       p += AUFS_WH_PFX_LEN;
35655 +       n = whlist->nh_num;
35656 +       head = whlist->nh_head;
35657 +       for (ul = 0; !err && ul < n; ul++, head++) {
35658 +               hlist_for_each_entry(pos, head, wh_hash) {
35659 +                       if (pos->wh_bindex != bindex)
35660 +                               continue;
35661 +
35662 +                       str = &pos->wh_str;
35663 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35664 +                               memcpy(p, str->name, str->len);
35665 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35666 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35667 +                               if (!err)
35668 +                                       continue;
35669 +                               break;
35670 +                       }
35671 +                       AuIOErr("whiteout name too long %.*s\n",
35672 +                               str->len, str->name);
35673 +                       err = -EIO;
35674 +                       break;
35675 +               }
35676 +       }
35677 +       free_page((unsigned long)wh_name.name);
35678 +
35679 +out:
35680 +       return err;
35681 +}
35682 +
35683 +struct del_wh_children_args {
35684 +       int *errp;
35685 +       struct dentry *h_dentry;
35686 +       struct au_nhash *whlist;
35687 +       aufs_bindex_t bindex;
35688 +       struct au_branch *br;
35689 +};
35690 +
35691 +static void call_del_wh_children(void *args)
35692 +{
35693 +       struct del_wh_children_args *a = args;
35694 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35695 +}
35696 +
35697 +/* ---------------------------------------------------------------------- */
35698 +
35699 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35700 +{
35701 +       struct au_whtmp_rmdir *whtmp;
35702 +       int err;
35703 +       unsigned int rdhash;
35704 +
35705 +       SiMustAnyLock(sb);
35706 +
35707 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35708 +       if (unlikely(!whtmp)) {
35709 +               whtmp = ERR_PTR(-ENOMEM);
35710 +               goto out;
35711 +       }
35712 +
35713 +       /* no estimation for dir size */
35714 +       rdhash = au_sbi(sb)->si_rdhash;
35715 +       if (!rdhash)
35716 +               rdhash = AUFS_RDHASH_DEF;
35717 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35718 +       if (unlikely(err)) {
35719 +               au_kfree_rcu(whtmp);
35720 +               whtmp = ERR_PTR(err);
35721 +       }
35722 +
35723 +out:
35724 +       return whtmp;
35725 +}
35726 +
35727 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35728 +{
35729 +       if (whtmp->br)
35730 +               au_lcnt_dec(&whtmp->br->br_count);
35731 +       dput(whtmp->wh_dentry);
35732 +       iput(whtmp->dir);
35733 +       au_nhash_wh_free(&whtmp->whlist);
35734 +       au_kfree_rcu(whtmp);
35735 +}
35736 +
35737 +/*
35738 + * rmdir the whiteouted temporary named dir @h_dentry.
35739 + * @whlist: whiteouted children.
35740 + */
35741 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35742 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35743 +{
35744 +       int err;
35745 +       unsigned int h_nlink;
35746 +       struct path h_tmp;
35747 +       struct inode *wh_inode, *h_dir;
35748 +       struct au_branch *br;
35749 +
35750 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35751 +       IMustLock(h_dir);
35752 +
35753 +       br = au_sbr(dir->i_sb, bindex);
35754 +       wh_inode = d_inode(wh_dentry);
35755 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35756 +
35757 +       /*
35758 +        * someone else might change some whiteouts while we were sleeping.
35759 +        * it means this whlist may have an obsoleted entry.
35760 +        */
35761 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35762 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35763 +       else {
35764 +               int wkq_err;
35765 +               struct del_wh_children_args args = {
35766 +                       .errp           = &err,
35767 +                       .h_dentry       = wh_dentry,
35768 +                       .whlist         = whlist,
35769 +                       .bindex         = bindex,
35770 +                       .br             = br
35771 +               };
35772 +
35773 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35774 +               if (unlikely(wkq_err))
35775 +                       err = wkq_err;
35776 +       }
35777 +       inode_unlock(wh_inode);
35778 +
35779 +       if (!err) {
35780 +               h_tmp.dentry = wh_dentry;
35781 +               h_tmp.mnt = au_br_mnt(br);
35782 +               h_nlink = h_dir->i_nlink;
35783 +               err = vfsub_rmdir(h_dir, &h_tmp);
35784 +               /* some fs doesn't change the parent nlink in some cases */
35785 +               h_nlink -= h_dir->i_nlink;
35786 +       }
35787 +
35788 +       if (!err) {
35789 +               if (au_ibtop(dir) == bindex) {
35790 +                       /* todo: dir->i_mutex is necessary */
35791 +                       au_cpup_attr_timesizes(dir);
35792 +                       if (h_nlink)
35793 +                               vfsub_drop_nlink(dir);
35794 +               }
35795 +               return 0; /* success */
35796 +       }
35797 +
35798 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35799 +       return err;
35800 +}
35801 +
35802 +static void call_rmdir_whtmp(void *args)
35803 +{
35804 +       int err;
35805 +       aufs_bindex_t bindex;
35806 +       struct au_whtmp_rmdir *a = args;
35807 +       struct super_block *sb;
35808 +       struct dentry *h_parent;
35809 +       struct inode *h_dir;
35810 +       struct au_hinode *hdir;
35811 +
35812 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35813 +       /* inode_lock(a->dir); */
35814 +       err = -EROFS;
35815 +       sb = a->dir->i_sb;
35816 +       si_read_lock(sb, !AuLock_FLUSH);
35817 +       if (!au_br_writable(a->br->br_perm))
35818 +               goto out;
35819 +       bindex = au_br_index(sb, a->br->br_id);
35820 +       if (unlikely(bindex < 0))
35821 +               goto out;
35822 +
35823 +       err = -EIO;
35824 +       ii_write_lock_parent(a->dir);
35825 +       h_parent = dget_parent(a->wh_dentry);
35826 +       h_dir = d_inode(h_parent);
35827 +       hdir = au_hi(a->dir, bindex);
35828 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35829 +       if (unlikely(err))
35830 +               goto out_mnt;
35831 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35832 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35833 +                         a->br);
35834 +       if (!err)
35835 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35836 +       au_hn_inode_unlock(hdir);
35837 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35838 +
35839 +out_mnt:
35840 +       dput(h_parent);
35841 +       ii_write_unlock(a->dir);
35842 +out:
35843 +       /* inode_unlock(a->dir); */
35844 +       au_whtmp_rmdir_free(a);
35845 +       si_read_unlock(sb);
35846 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35847 +       if (unlikely(err))
35848 +               AuIOErr("err %d\n", err);
35849 +}
35850 +
35851 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35852 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35853 +{
35854 +       int wkq_err;
35855 +       struct super_block *sb;
35856 +
35857 +       IMustLock(dir);
35858 +
35859 +       /* all post-process will be done in do_rmdir_whtmp(). */
35860 +       sb = dir->i_sb;
35861 +       args->dir = au_igrab(dir);
35862 +       args->br = au_sbr(sb, bindex);
35863 +       au_lcnt_inc(&args->br->br_count);
35864 +       args->wh_dentry = dget(wh_dentry);
35865 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35866 +       if (unlikely(wkq_err)) {
35867 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35868 +               au_whtmp_rmdir_free(args);
35869 +       }
35870 +}
35871 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35872 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35873 +++ linux/fs/aufs/whout.h       2020-01-27 10:57:18.178871751 +0100
35874 @@ -0,0 +1,86 @@
35875 +/* SPDX-License-Identifier: GPL-2.0 */
35876 +/*
35877 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35878 + *
35879 + * This program, aufs is free software; you can redistribute it and/or modify
35880 + * it under the terms of the GNU General Public License as published by
35881 + * the Free Software Foundation; either version 2 of the License, or
35882 + * (at your option) any later version.
35883 + *
35884 + * This program is distributed in the hope that it will be useful,
35885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35887 + * GNU General Public License for more details.
35888 + *
35889 + * You should have received a copy of the GNU General Public License
35890 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35891 + */
35892 +
35893 +/*
35894 + * whiteout for logical deletion and opaque directory
35895 + */
35896 +
35897 +#ifndef __AUFS_WHOUT_H__
35898 +#define __AUFS_WHOUT_H__
35899 +
35900 +#ifdef __KERNEL__
35901 +
35902 +#include "dir.h"
35903 +
35904 +/* whout.c */
35905 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35906 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35907 +int au_diropq_test(struct dentry *h_dentry);
35908 +struct au_branch;
35909 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35910 +                            struct qstr *prefix);
35911 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35912 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35913 +                       struct dentry *dentry);
35914 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35915 +
35916 +/* diropq flags */
35917 +#define AuDiropq_CREATE        1
35918 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35919 +#define au_fset_diropq(flags, name) \
35920 +       do { (flags) |= AuDiropq_##name; } while (0)
35921 +#define au_fclr_diropq(flags, name) \
35922 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35923 +
35924 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35925 +                            unsigned int flags);
35926 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35927 +                         struct au_branch *br);
35928 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35929 +                           struct dentry *h_parent);
35930 +
35931 +/* real rmdir for the whiteout-ed dir */
35932 +struct au_whtmp_rmdir {
35933 +       struct inode *dir;
35934 +       struct au_branch *br;
35935 +       struct dentry *wh_dentry;
35936 +       struct au_nhash whlist;
35937 +};
35938 +
35939 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35940 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35941 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35942 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35943 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35944 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35945 +
35946 +/* ---------------------------------------------------------------------- */
35947 +
35948 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35949 +                                             aufs_bindex_t bindex)
35950 +{
35951 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35952 +}
35953 +
35954 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35955 +{
35956 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35957 +}
35958 +
35959 +#endif /* __KERNEL__ */
35960 +#endif /* __AUFS_WHOUT_H__ */
35961 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35962 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35963 +++ linux/fs/aufs/wkq.c 2020-08-03 09:12:33.764880472 +0200
35964 @@ -0,0 +1,372 @@
35965 +// SPDX-License-Identifier: GPL-2.0
35966 +/*
35967 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35968 + *
35969 + * This program, aufs is free software; you can redistribute it and/or modify
35970 + * it under the terms of the GNU General Public License as published by
35971 + * the Free Software Foundation; either version 2 of the License, or
35972 + * (at your option) any later version.
35973 + *
35974 + * This program is distributed in the hope that it will be useful,
35975 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35976 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35977 + * GNU General Public License for more details.
35978 + *
35979 + * You should have received a copy of the GNU General Public License
35980 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35981 + */
35982 +
35983 +/*
35984 + * workqueue for asynchronous/super-io operations
35985 + * todo: try new credential scheme
35986 + */
35987 +
35988 +#include <linux/module.h>
35989 +#include "aufs.h"
35990 +
35991 +/* internal workqueue named AUFS_WKQ_NAME */
35992 +
35993 +static struct workqueue_struct *au_wkq;
35994 +
35995 +struct au_wkinfo {
35996 +       struct work_struct wk;
35997 +       struct kobject *kobj;
35998 +
35999 +       unsigned int flags; /* see wkq.h */
36000 +
36001 +       au_wkq_func_t func;
36002 +       void *args;
36003 +
36004 +#ifdef CONFIG_LOCKDEP
36005 +       int dont_check;
36006 +       struct held_lock **hlock;
36007 +#endif
36008 +
36009 +       struct completion *comp;
36010 +};
36011 +
36012 +/* ---------------------------------------------------------------------- */
36013 +/*
36014 + * Aufs passes some operations to the workqueue such as the internal copyup.
36015 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36016 + * job run by workqueue depends upon the locks acquired in the other task.
36017 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36018 + * information too. And the job in the workqueue restores the info in order to
36019 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36020 + * correctly and expectedly.
36021 + */
36022 +
36023 +#ifndef CONFIG_LOCKDEP
36024 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36025 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36026 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36027 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36028 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36029 +#else
36030 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36031 +{
36032 +       wkinfo->hlock = NULL;
36033 +       wkinfo->dont_check = 0;
36034 +}
36035 +
36036 +/*
36037 + * 1: matched
36038 + * 0: unmatched
36039 + */
36040 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36041 +{
36042 +       static DEFINE_SPINLOCK(spin);
36043 +       static struct {
36044 +               char *name;
36045 +               struct lock_class_key *key;
36046 +       } a[] = {
36047 +               { .name = "&sbinfo->si_rwsem" },
36048 +               { .name = "&finfo->fi_rwsem" },
36049 +               { .name = "&dinfo->di_rwsem" },
36050 +               { .name = "&iinfo->ii_rwsem" }
36051 +       };
36052 +       static int set;
36053 +       int i;
36054 +
36055 +       /* lockless read from 'set.' see below */
36056 +       if (set == ARRAY_SIZE(a)) {
36057 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36058 +                       if (a[i].key == key)
36059 +                               goto match;
36060 +               goto unmatch;
36061 +       }
36062 +
36063 +       spin_lock(&spin);
36064 +       if (set)
36065 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36066 +                       if (a[i].key == key) {
36067 +                               spin_unlock(&spin);
36068 +                               goto match;
36069 +                       }
36070 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36071 +               if (a[i].key) {
36072 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36073 +                               spin_unlock(&spin);
36074 +                               goto match;
36075 +                       } else
36076 +                               continue;
36077 +               }
36078 +               if (strstr(a[i].name, name)) {
36079 +                       /*
36080 +                        * the order of these three lines is important for the
36081 +                        * lockless read above.
36082 +                        */
36083 +                       a[i].key = key;
36084 +                       spin_unlock(&spin);
36085 +                       set++;
36086 +                       /* AuDbg("%d, %s\n", set, name); */
36087 +                       goto match;
36088 +               }
36089 +       }
36090 +       spin_unlock(&spin);
36091 +       goto unmatch;
36092 +
36093 +match:
36094 +       return 1;
36095 +unmatch:
36096 +       return 0;
36097 +}
36098 +
36099 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36100 +{
36101 +       int err, n;
36102 +       struct task_struct *curr;
36103 +       struct held_lock **hl, *held_locks, *p;
36104 +
36105 +       err = 0;
36106 +       curr = current;
36107 +       wkinfo->dont_check = lockdep_recursing(curr);
36108 +       if (wkinfo->dont_check)
36109 +               goto out;
36110 +       n = curr->lockdep_depth;
36111 +       if (!n)
36112 +               goto out;
36113 +
36114 +       err = -ENOMEM;
36115 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36116 +       if (unlikely(!wkinfo->hlock))
36117 +               goto out;
36118 +
36119 +       err = 0;
36120 +#if 0 /* left for debugging */
36121 +       if (0 && au_debug_test())
36122 +               lockdep_print_held_locks(curr);
36123 +#endif
36124 +       held_locks = curr->held_locks;
36125 +       hl = wkinfo->hlock;
36126 +       while (n--) {
36127 +               p = held_locks++;
36128 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36129 +                       *hl++ = p;
36130 +       }
36131 +       *hl = NULL;
36132 +
36133 +out:
36134 +       return err;
36135 +}
36136 +
36137 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36138 +{
36139 +       au_kfree_try_rcu(wkinfo->hlock);
36140 +}
36141 +
36142 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36143 +{
36144 +       struct held_lock *p, **hl = wkinfo->hlock;
36145 +       int subclass;
36146 +
36147 +       if (wkinfo->dont_check)
36148 +               lockdep_off();
36149 +       if (!hl)
36150 +               return;
36151 +       while ((p = *hl++)) { /* assignment */
36152 +               subclass = lockdep_hlock_class(p)->subclass;
36153 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36154 +               if (p->read)
36155 +                       rwsem_acquire_read(p->instance, subclass, 0,
36156 +                                          /*p->acquire_ip*/_RET_IP_);
36157 +               else
36158 +                       rwsem_acquire(p->instance, subclass, 0,
36159 +                                     /*p->acquire_ip*/_RET_IP_);
36160 +       }
36161 +}
36162 +
36163 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36164 +{
36165 +       struct held_lock *p, **hl = wkinfo->hlock;
36166 +
36167 +       if (wkinfo->dont_check)
36168 +               lockdep_on();
36169 +       if (!hl)
36170 +               return;
36171 +       while ((p = *hl++)) /* assignment */
36172 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36173 +}
36174 +#endif
36175 +
36176 +static void wkq_func(struct work_struct *wk)
36177 +{
36178 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36179 +
36180 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36181 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36182 +
36183 +       au_wkq_lockdep_pre(wkinfo);
36184 +       wkinfo->func(wkinfo->args);
36185 +       au_wkq_lockdep_post(wkinfo);
36186 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36187 +               complete(wkinfo->comp);
36188 +       else {
36189 +               kobject_put(wkinfo->kobj);
36190 +               module_put(THIS_MODULE); /* todo: ?? */
36191 +               au_kfree_rcu(wkinfo);
36192 +       }
36193 +}
36194 +
36195 +/*
36196 + * Since struct completion is large, try allocating it dynamically.
36197 + */
36198 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36199 +
36200 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36201 +{
36202 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36203 +       if (*comp) {
36204 +               init_completion(*comp);
36205 +               wkinfo->comp = *comp;
36206 +               return 0;
36207 +       }
36208 +       return -ENOMEM;
36209 +}
36210 +
36211 +static void au_wkq_comp_free(struct completion *comp)
36212 +{
36213 +       au_kfree_rcu(comp);
36214 +}
36215 +
36216 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36217 +{
36218 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36219 +               if (au_wkq_test()) {
36220 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36221 +                               " due to a dead dir by UDBA,"
36222 +                               " or async xino write?\n");
36223 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36224 +               }
36225 +       } else
36226 +               au_dbg_verify_kthread();
36227 +
36228 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36229 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36230 +               queue_work(au_wkq, &wkinfo->wk);
36231 +       } else {
36232 +               INIT_WORK(&wkinfo->wk, wkq_func);
36233 +               schedule_work(&wkinfo->wk);
36234 +       }
36235 +}
36236 +
36237 +/*
36238 + * Be careful. It is easy to make deadlock happen.
36239 + * processA: lock, wkq and wait
36240 + * processB: wkq and wait, lock in wkq
36241 + * --> deadlock
36242 + */
36243 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36244 +{
36245 +       int err;
36246 +       AuWkqCompDeclare(comp);
36247 +       struct au_wkinfo wkinfo = {
36248 +               .flags  = flags,
36249 +               .func   = func,
36250 +               .args   = args
36251 +       };
36252 +
36253 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36254 +       if (unlikely(err))
36255 +               goto out;
36256 +       err = au_wkq_lockdep_alloc(&wkinfo);
36257 +       if (unlikely(err))
36258 +               goto out_comp;
36259 +       if (!err) {
36260 +               au_wkq_run(&wkinfo);
36261 +               /* no timeout, no interrupt */
36262 +               wait_for_completion(wkinfo.comp);
36263 +       }
36264 +       au_wkq_lockdep_free(&wkinfo);
36265 +
36266 +out_comp:
36267 +       au_wkq_comp_free(comp);
36268 +out:
36269 +       destroy_work_on_stack(&wkinfo.wk);
36270 +       return err;
36271 +}
36272 +
36273 +/*
36274 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36275 + * problem in a concurrent umounting.
36276 + */
36277 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36278 +                 unsigned int flags)
36279 +{
36280 +       int err;
36281 +       struct au_wkinfo *wkinfo;
36282 +
36283 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36284 +
36285 +       /*
36286 +        * wkq_func() must free this wkinfo.
36287 +        * it highly depends upon the implementation of workqueue.
36288 +        */
36289 +       err = 0;
36290 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36291 +       if (wkinfo) {
36292 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36293 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36294 +               wkinfo->func = func;
36295 +               wkinfo->args = args;
36296 +               wkinfo->comp = NULL;
36297 +               au_wkq_lockdep_init(wkinfo);
36298 +               kobject_get(wkinfo->kobj);
36299 +               __module_get(THIS_MODULE); /* todo: ?? */
36300 +
36301 +               au_wkq_run(wkinfo);
36302 +       } else {
36303 +               err = -ENOMEM;
36304 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36305 +       }
36306 +
36307 +       return err;
36308 +}
36309 +
36310 +/* ---------------------------------------------------------------------- */
36311 +
36312 +void au_nwt_init(struct au_nowait_tasks *nwt)
36313 +{
36314 +       atomic_set(&nwt->nw_len, 0);
36315 +       /* smp_mb(); */ /* atomic_set */
36316 +       init_waitqueue_head(&nwt->nw_wq);
36317 +}
36318 +
36319 +void au_wkq_fin(void)
36320 +{
36321 +       destroy_workqueue(au_wkq);
36322 +}
36323 +
36324 +int __init au_wkq_init(void)
36325 +{
36326 +       int err;
36327 +
36328 +       err = 0;
36329 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36330 +       if (IS_ERR(au_wkq))
36331 +               err = PTR_ERR(au_wkq);
36332 +       else if (!au_wkq)
36333 +               err = -ENOMEM;
36334 +
36335 +       return err;
36336 +}
36337 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36338 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36339 +++ linux/fs/aufs/wkq.h 2020-01-27 10:57:18.182205184 +0100
36340 @@ -0,0 +1,89 @@
36341 +/* SPDX-License-Identifier: GPL-2.0 */
36342 +/*
36343 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36344 + *
36345 + * This program, aufs is free software; you can redistribute it and/or modify
36346 + * it under the terms of the GNU General Public License as published by
36347 + * the Free Software Foundation; either version 2 of the License, or
36348 + * (at your option) any later version.
36349 + *
36350 + * This program is distributed in the hope that it will be useful,
36351 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36352 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36353 + * GNU General Public License for more details.
36354 + *
36355 + * You should have received a copy of the GNU General Public License
36356 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36357 + */
36358 +
36359 +/*
36360 + * workqueue for asynchronous/super-io operations
36361 + * todo: try new credentials management scheme
36362 + */
36363 +
36364 +#ifndef __AUFS_WKQ_H__
36365 +#define __AUFS_WKQ_H__
36366 +
36367 +#ifdef __KERNEL__
36368 +
36369 +#include <linux/wait.h>
36370 +
36371 +struct super_block;
36372 +
36373 +/* ---------------------------------------------------------------------- */
36374 +
36375 +/*
36376 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36377 + */
36378 +struct au_nowait_tasks {
36379 +       atomic_t                nw_len;
36380 +       wait_queue_head_t       nw_wq;
36381 +};
36382 +
36383 +/* ---------------------------------------------------------------------- */
36384 +
36385 +typedef void (*au_wkq_func_t)(void *args);
36386 +
36387 +/* wkq flags */
36388 +#define AuWkq_WAIT     1
36389 +#define AuWkq_NEST     (1 << 1)
36390 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36391 +#define au_fset_wkq(flags, name) \
36392 +       do { (flags) |= AuWkq_##name; } while (0)
36393 +#define au_fclr_wkq(flags, name) \
36394 +       do { (flags) &= ~AuWkq_##name; } while (0)
36395 +
36396 +/* wkq.c */
36397 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36398 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36399 +                 unsigned int flags);
36400 +void au_nwt_init(struct au_nowait_tasks *nwt);
36401 +int __init au_wkq_init(void);
36402 +void au_wkq_fin(void);
36403 +
36404 +/* ---------------------------------------------------------------------- */
36405 +
36406 +static inline int au_wkq_test(void)
36407 +{
36408 +       return current->flags & PF_WQ_WORKER;
36409 +}
36410 +
36411 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36412 +{
36413 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36414 +}
36415 +
36416 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36417 +{
36418 +       if (atomic_dec_and_test(&nwt->nw_len))
36419 +               wake_up_all(&nwt->nw_wq);
36420 +}
36421 +
36422 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36423 +{
36424 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36425 +       return 0;
36426 +}
36427 +
36428 +#endif /* __KERNEL__ */
36429 +#endif /* __AUFS_WKQ_H__ */
36430 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36431 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36432 +++ linux/fs/aufs/xattr.c       2020-08-03 09:14:46.095748745 +0200
36433 @@ -0,0 +1,356 @@
36434 +// SPDX-License-Identifier: GPL-2.0
36435 +/*
36436 + * Copyright (C) 2014-2020 Junjiro R. Okajima
36437 + *
36438 + * This program, aufs is free software; you can redistribute it and/or modify
36439 + * it under the terms of the GNU General Public License as published by
36440 + * the Free Software Foundation; either version 2 of the License, or
36441 + * (at your option) any later version.
36442 + *
36443 + * This program is distributed in the hope that it will be useful,
36444 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36445 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36446 + * GNU General Public License for more details.
36447 + *
36448 + * You should have received a copy of the GNU General Public License
36449 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36450 + */
36451 +
36452 +/*
36453 + * handling xattr functions
36454 + */
36455 +
36456 +#include <linux/fs.h>
36457 +#include <linux/posix_acl_xattr.h>
36458 +#include <linux/xattr.h>
36459 +#include "aufs.h"
36460 +
36461 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36462 +{
36463 +       if (!ignore_flags)
36464 +               goto out;
36465 +       switch (err) {
36466 +       case -ENOMEM:
36467 +       case -EDQUOT:
36468 +               goto out;
36469 +       }
36470 +
36471 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36472 +               err = 0;
36473 +               goto out;
36474 +       }
36475 +
36476 +#define cmp(brattr, prefix) do {                                       \
36477 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36478 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36479 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36480 +                               err = 0;                                \
36481 +                       goto out;                                       \
36482 +               }                                                       \
36483 +       } while (0)
36484 +
36485 +       cmp(SEC, SECURITY);
36486 +       cmp(SYS, SYSTEM);
36487 +       cmp(TR, TRUSTED);
36488 +       cmp(USR, USER);
36489 +#undef cmp
36490 +
36491 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36492 +               err = 0;
36493 +
36494 +out:
36495 +       return err;
36496 +}
36497 +
36498 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36499 +
36500 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36501 +                           char *name, char **buf, unsigned int ignore_flags,
36502 +                           unsigned int verbose)
36503 +{
36504 +       int err;
36505 +       ssize_t ssz;
36506 +       struct inode *h_idst;
36507 +
36508 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36509 +       err = ssz;
36510 +       if (unlikely(err <= 0)) {
36511 +               if (err == -ENODATA
36512 +                   || (err == -EOPNOTSUPP
36513 +                       && ((ignore_flags & au_xattr_out_of_list)
36514 +                           || (au_test_nfs_noacl(d_inode(h_src))
36515 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36516 +                                   || !strcmp(name,
36517 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36518 +                           ))
36519 +                       err = 0;
36520 +               if (err && (verbose || au_debug_test()))
36521 +                       pr_err("%s, err %d\n", name, err);
36522 +               goto out;
36523 +       }
36524 +
36525 +       /* unlock it temporary */
36526 +       h_idst = d_inode(h_dst);
36527 +       inode_unlock(h_idst);
36528 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36529 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36530 +       if (unlikely(err)) {
36531 +               if (verbose || au_debug_test())
36532 +                       pr_err("%s, err %d\n", name, err);
36533 +               err = au_xattr_ignore(err, name, ignore_flags);
36534 +       }
36535 +
36536 +out:
36537 +       return err;
36538 +}
36539 +
36540 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36541 +                 unsigned int verbose)
36542 +{
36543 +       int err, unlocked, acl_access, acl_default;
36544 +       ssize_t ssz;
36545 +       struct inode *h_isrc, *h_idst;
36546 +       char *value, *p, *o, *e;
36547 +
36548 +       /* try stopping to update the source inode while we are referencing */
36549 +       /* there should not be the parent-child relationship between them */
36550 +       h_isrc = d_inode(h_src);
36551 +       h_idst = d_inode(h_dst);
36552 +       inode_unlock(h_idst);
36553 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36554 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36555 +       unlocked = 0;
36556 +
36557 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36558 +       ssz = vfs_listxattr(h_src, NULL, 0);
36559 +       err = ssz;
36560 +       if (unlikely(err < 0)) {
36561 +               AuTraceErr(err);
36562 +               if (err == -ENODATA
36563 +                   || err == -EOPNOTSUPP)
36564 +                       err = 0;        /* ignore */
36565 +               goto out;
36566 +       }
36567 +
36568 +       err = 0;
36569 +       p = NULL;
36570 +       o = NULL;
36571 +       if (ssz) {
36572 +               err = -ENOMEM;
36573 +               p = kmalloc(ssz, GFP_NOFS);
36574 +               o = p;
36575 +               if (unlikely(!p))
36576 +                       goto out;
36577 +               err = vfs_listxattr(h_src, p, ssz);
36578 +       }
36579 +       inode_unlock_shared(h_isrc);
36580 +       unlocked = 1;
36581 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36582 +       if (unlikely(err < 0))
36583 +               goto out_free;
36584 +
36585 +       err = 0;
36586 +       e = p + ssz;
36587 +       value = NULL;
36588 +       acl_access = 0;
36589 +       acl_default = 0;
36590 +       while (!err && p < e) {
36591 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36592 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36593 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36594 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36595 +                                       - 1);
36596 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36597 +                                      verbose);
36598 +               p += strlen(p) + 1;
36599 +       }
36600 +       AuTraceErr(err);
36601 +       ignore_flags |= au_xattr_out_of_list;
36602 +       if (!err && !acl_access) {
36603 +               err = au_do_cpup_xattr(h_dst, h_src,
36604 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36605 +                                      ignore_flags, verbose);
36606 +               AuTraceErr(err);
36607 +       }
36608 +       if (!err && !acl_default) {
36609 +               err = au_do_cpup_xattr(h_dst, h_src,
36610 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36611 +                                      ignore_flags, verbose);
36612 +               AuTraceErr(err);
36613 +       }
36614 +
36615 +       au_kfree_try_rcu(value);
36616 +
36617 +out_free:
36618 +       au_kfree_try_rcu(o);
36619 +out:
36620 +       if (!unlocked)
36621 +               inode_unlock_shared(h_isrc);
36622 +       AuTraceErr(err);
36623 +       return err;
36624 +}
36625 +
36626 +/* ---------------------------------------------------------------------- */
36627 +
36628 +static int au_smack_reentering(struct super_block *sb)
36629 +{
36630 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36631 +       /*
36632 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36633 +        * i_op->getxattr(). ouch.
36634 +        */
36635 +       return si_pid_test(sb);
36636 +#else
36637 +       return 0;
36638 +#endif
36639 +}
36640 +
36641 +enum {
36642 +       AU_XATTR_LIST,
36643 +       AU_XATTR_GET
36644 +};
36645 +
36646 +struct au_lgxattr {
36647 +       int type;
36648 +       union {
36649 +               struct {
36650 +                       char    *list;
36651 +                       size_t  size;
36652 +               } list;
36653 +               struct {
36654 +                       const char      *name;
36655 +                       void            *value;
36656 +                       size_t          size;
36657 +               } get;
36658 +       } u;
36659 +};
36660 +
36661 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36662 +                         struct au_lgxattr *arg)
36663 +{
36664 +       ssize_t err;
36665 +       int reenter;
36666 +       struct path h_path;
36667 +       struct super_block *sb;
36668 +
36669 +       sb = dentry->d_sb;
36670 +       reenter = au_smack_reentering(sb);
36671 +       if (!reenter) {
36672 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36673 +               if (unlikely(err))
36674 +                       goto out;
36675 +       }
36676 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36677 +       if (unlikely(err))
36678 +               goto out_si;
36679 +       if (unlikely(!h_path.dentry))
36680 +               /* illegally overlapped or something */
36681 +               goto out_di; /* pretending success */
36682 +
36683 +       /* always topmost entry only */
36684 +       switch (arg->type) {
36685 +       case AU_XATTR_LIST:
36686 +               err = vfs_listxattr(h_path.dentry,
36687 +                                   arg->u.list.list, arg->u.list.size);
36688 +               break;
36689 +       case AU_XATTR_GET:
36690 +               AuDebugOn(d_is_negative(h_path.dentry));
36691 +               err = vfs_getxattr(h_path.dentry,
36692 +                                  arg->u.get.name, arg->u.get.value,
36693 +                                  arg->u.get.size);
36694 +               break;
36695 +       }
36696 +
36697 +out_di:
36698 +       if (!reenter)
36699 +               di_read_unlock(dentry, AuLock_IR);
36700 +out_si:
36701 +       if (!reenter)
36702 +               si_read_unlock(sb);
36703 +out:
36704 +       AuTraceErr(err);
36705 +       return err;
36706 +}
36707 +
36708 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36709 +{
36710 +       struct au_lgxattr arg = {
36711 +               .type = AU_XATTR_LIST,
36712 +               .u.list = {
36713 +                       .list   = list,
36714 +                       .size   = size
36715 +               },
36716 +       };
36717 +
36718 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36719 +}
36720 +
36721 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36722 +                          const char *name, void *value, size_t size)
36723 +{
36724 +       struct au_lgxattr arg = {
36725 +               .type = AU_XATTR_GET,
36726 +               .u.get = {
36727 +                       .name   = name,
36728 +                       .value  = value,
36729 +                       .size   = size
36730 +               },
36731 +       };
36732 +
36733 +       return au_lgxattr(dentry, inode, &arg);
36734 +}
36735 +
36736 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36737 +                      const char *name, const void *value, size_t size,
36738 +                      int flags)
36739 +{
36740 +       struct au_sxattr arg = {
36741 +               .type = AU_XATTR_SET,
36742 +               .u.set = {
36743 +                       .name   = name,
36744 +                       .value  = value,
36745 +                       .size   = size,
36746 +                       .flags  = flags
36747 +               },
36748 +       };
36749 +
36750 +       return au_sxattr(dentry, inode, &arg);
36751 +}
36752 +
36753 +/* ---------------------------------------------------------------------- */
36754 +
36755 +static int au_xattr_get(const struct xattr_handler *handler,
36756 +                       struct dentry *dentry, struct inode *inode,
36757 +                       const char *name, void *buffer, size_t size)
36758 +{
36759 +       return au_getxattr(dentry, inode, name, buffer, size);
36760 +}
36761 +
36762 +static int au_xattr_set(const struct xattr_handler *handler,
36763 +                       struct dentry *dentry, struct inode *inode,
36764 +                       const char *name, const void *value, size_t size,
36765 +                       int flags)
36766 +{
36767 +       return au_setxattr(dentry, inode, name, value, size, flags);
36768 +}
36769 +
36770 +static const struct xattr_handler au_xattr_handler = {
36771 +       .name   = "",
36772 +       .prefix = "",
36773 +       .get    = au_xattr_get,
36774 +       .set    = au_xattr_set
36775 +};
36776 +
36777 +static const struct xattr_handler *au_xattr_handlers[] = {
36778 +#ifdef CONFIG_FS_POSIX_ACL
36779 +       &posix_acl_access_xattr_handler,
36780 +       &posix_acl_default_xattr_handler,
36781 +#endif
36782 +       &au_xattr_handler, /* must be last */
36783 +       NULL
36784 +};
36785 +
36786 +void au_xattr_init(struct super_block *sb)
36787 +{
36788 +       sb->s_xattr = au_xattr_handlers;
36789 +}
36790 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36791 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36792 +++ linux/fs/aufs/xino.c        2020-04-03 08:16:49.834195677 +0200
36793 @@ -0,0 +1,1966 @@
36794 +// SPDX-License-Identifier: GPL-2.0
36795 +/*
36796 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36797 + *
36798 + * This program, aufs is free software; you can redistribute it and/or modify
36799 + * it under the terms of the GNU General Public License as published by
36800 + * the Free Software Foundation; either version 2 of the License, or
36801 + * (at your option) any later version.
36802 + *
36803 + * This program is distributed in the hope that it will be useful,
36804 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36805 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36806 + * GNU General Public License for more details.
36807 + *
36808 + * You should have received a copy of the GNU General Public License
36809 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36810 + */
36811 +
36812 +/*
36813 + * external inode number translation table and bitmap
36814 + *
36815 + * things to consider
36816 + * - the lifetime
36817 + *   + au_xino object
36818 + *   + XINO files (xino, xib, xigen)
36819 + *   + dynamic debugfs entries (xiN)
36820 + *   + static debugfs entries (xib, xigen)
36821 + *   + static sysfs entry (xi_path)
36822 + * - several entry points to handle them.
36823 + *   + mount(2) without xino option (default)
36824 + *   + mount(2) with xino option
36825 + *   + mount(2) with noxino option
36826 + *   + umount(2)
36827 + *   + remount with add/del branches
36828 + *   + remount with xino/noxino options
36829 + */
36830 +
36831 +#include <linux/seq_file.h>
36832 +#include <linux/statfs.h>
36833 +#include "aufs.h"
36834 +
36835 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36836 +                                    aufs_bindex_t bbot,
36837 +                                    struct super_block *h_sb)
36838 +{
36839 +       /* todo: try binary-search if the branches are many */
36840 +       for (; btop <= bbot; btop++)
36841 +               if (h_sb == au_sbr_sb(sb, btop))
36842 +                       return btop;
36843 +       return -1;
36844 +}
36845 +
36846 +/*
36847 + * find another branch who is on the same filesystem of the specified
36848 + * branch{@btgt}. search until @bbot.
36849 + */
36850 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36851 +                                 aufs_bindex_t bbot)
36852 +{
36853 +       aufs_bindex_t bindex;
36854 +       struct super_block *tgt_sb;
36855 +
36856 +       tgt_sb = au_sbr_sb(sb, btgt);
36857 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36858 +       if (bindex < 0)
36859 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36860 +
36861 +       return bindex;
36862 +}
36863 +
36864 +/* ---------------------------------------------------------------------- */
36865 +
36866 +/*
36867 + * stop unnecessary notify events at creating xino files
36868 + */
36869 +
36870 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36871 +{
36872 +       aufs_bindex_t bfound, bindex, bbot;
36873 +       struct dentry *parent;
36874 +       struct au_branch *br;
36875 +
36876 +       bfound = -1;
36877 +       parent = dentry->d_parent; /* safe d_parent access */
36878 +       bbot = au_sbbot(sb);
36879 +       for (bindex = 0; bindex <= bbot; bindex++) {
36880 +               br = au_sbr(sb, bindex);
36881 +               if (au_br_dentry(br) == parent) {
36882 +                       bfound = bindex;
36883 +                       break;
36884 +               }
36885 +       }
36886 +
36887 +       AuDbg("bfound b%d\n", bfound);
36888 +       return bfound;
36889 +}
36890 +
36891 +struct au_xino_lock_dir {
36892 +       struct au_hinode *hdir;
36893 +       struct dentry *parent;
36894 +       struct inode *dir;
36895 +};
36896 +
36897 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36898 +                                         unsigned int lsc)
36899 +{
36900 +       struct dentry *parent;
36901 +       struct inode *dir;
36902 +
36903 +       parent = dget_parent(dentry);
36904 +       dir = d_inode(parent);
36905 +       inode_lock_nested(dir, lsc);
36906 +#if 0 /* it should not happen */
36907 +       spin_lock(&dentry->d_lock);
36908 +       if (unlikely(dentry->d_parent != parent)) {
36909 +               spin_unlock(&dentry->d_lock);
36910 +               inode_unlock(dir);
36911 +               dput(parent);
36912 +               parent = NULL;
36913 +               goto out;
36914 +       }
36915 +       spin_unlock(&dentry->d_lock);
36916 +
36917 +out:
36918 +#endif
36919 +       return parent;
36920 +}
36921 +
36922 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36923 +                            struct au_xino_lock_dir *ldir)
36924 +{
36925 +       aufs_bindex_t bindex;
36926 +
36927 +       ldir->hdir = NULL;
36928 +       bindex = au_xi_root(sb, xipath->dentry);
36929 +       if (bindex >= 0) {
36930 +               /* rw branch root */
36931 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36932 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36933 +       } else {
36934 +               /* other */
36935 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36936 +                                                  AuLsc_I_PARENT);
36937 +               ldir->dir = d_inode(ldir->parent);
36938 +       }
36939 +}
36940 +
36941 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36942 +{
36943 +       if (ldir->hdir)
36944 +               au_hn_inode_unlock(ldir->hdir);
36945 +       else {
36946 +               inode_unlock(ldir->dir);
36947 +               dput(ldir->parent);
36948 +       }
36949 +}
36950 +
36951 +/* ---------------------------------------------------------------------- */
36952 +
36953 +/*
36954 + * create and set a new xino file
36955 + */
36956 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36957 +                           int wbrtop)
36958 +{
36959 +       struct file *file;
36960 +       struct dentry *h_parent, *d;
36961 +       struct inode *h_dir, *inode;
36962 +       int err;
36963 +       static DEFINE_MUTEX(mtx);
36964 +
36965 +       /*
36966 +        * at mount-time, and the xino file is the default path,
36967 +        * hnotify is disabled so we have no notify events to ignore.
36968 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36969 +        */
36970 +       if (!wbrtop)
36971 +               mutex_lock(&mtx);
36972 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36973 +                              /* | __FMODE_NONOTIFY */,
36974 +                              0666);
36975 +       if (IS_ERR(file)) {
36976 +               if (!wbrtop)
36977 +                       mutex_unlock(&mtx);
36978 +               if (!silent)
36979 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36980 +               return file;
36981 +       }
36982 +
36983 +       /* keep file count */
36984 +       err = 0;
36985 +       d = file->f_path.dentry;
36986 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36987 +       if (!wbrtop)
36988 +               mutex_unlock(&mtx);
36989 +       /* mnt_want_write() is unnecessary here */
36990 +       h_dir = d_inode(h_parent);
36991 +       inode = file_inode(file);
36992 +       /* no delegation since it is just created */
36993 +       if (inode->i_nlink)
36994 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36995 +                                  /*force*/0);
36996 +       inode_unlock(h_dir);
36997 +       dput(h_parent);
36998 +       if (unlikely(err)) {
36999 +               if (!silent)
37000 +                       pr_err("unlink %s(%d)\n", fpath, err);
37001 +               goto out;
37002 +       }
37003 +
37004 +       err = -EINVAL;
37005 +       if (unlikely(sb == d->d_sb)) {
37006 +               if (!silent)
37007 +                       pr_err("%s must be outside\n", fpath);
37008 +               goto out;
37009 +       }
37010 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37011 +               if (!silent)
37012 +                       pr_err("xino doesn't support %s(%s)\n",
37013 +                              fpath, au_sbtype(d->d_sb));
37014 +               goto out;
37015 +       }
37016 +       return file; /* success */
37017 +
37018 +out:
37019 +       fput(file);
37020 +       file = ERR_PTR(err);
37021 +       return file;
37022 +}
37023 +
37024 +/*
37025 + * create a new xinofile at the same place/path as @base.
37026 + */
37027 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37028 +                            struct file *copy_src)
37029 +{
37030 +       struct file *file;
37031 +       struct dentry *dentry, *parent;
37032 +       struct inode *dir, *delegated;
37033 +       struct qstr *name;
37034 +       struct path path;
37035 +       int err, do_unlock;
37036 +       struct au_xino_lock_dir ldir;
37037 +
37038 +       do_unlock = 1;
37039 +       au_xino_lock_dir(sb, base, &ldir);
37040 +       dentry = base->dentry;
37041 +       parent = dentry->d_parent; /* dir inode is locked */
37042 +       dir = d_inode(parent);
37043 +       IMustLock(dir);
37044 +
37045 +       name = &dentry->d_name;
37046 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37047 +       if (IS_ERR(path.dentry)) {
37048 +               file = (void *)path.dentry;
37049 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37050 +               goto out;
37051 +       }
37052 +
37053 +       /* no need to mnt_want_write() since we call dentry_open() later */
37054 +       err = vfs_create(dir, path.dentry, 0666, NULL);
37055 +       if (unlikely(err)) {
37056 +               file = ERR_PTR(err);
37057 +               pr_err("%pd create err %d\n", dentry, err);
37058 +               goto out_dput;
37059 +       }
37060 +
37061 +       path.mnt = base->mnt;
37062 +       file = vfsub_dentry_open(&path,
37063 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37064 +                                /* | __FMODE_NONOTIFY */);
37065 +       if (IS_ERR(file)) {
37066 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37067 +               goto out_dput;
37068 +       }
37069 +
37070 +       delegated = NULL;
37071 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37072 +       au_xino_unlock_dir(&ldir);
37073 +       do_unlock = 0;
37074 +       if (unlikely(err == -EWOULDBLOCK)) {
37075 +               pr_warn("cannot retry for NFSv4 delegation"
37076 +                       " for an internal unlink\n");
37077 +               iput(delegated);
37078 +       }
37079 +       if (unlikely(err)) {
37080 +               pr_err("%pd unlink err %d\n", dentry, err);
37081 +               goto out_fput;
37082 +       }
37083 +
37084 +       if (copy_src) {
37085 +               /* no one can touch copy_src xino */
37086 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37087 +               if (unlikely(err)) {
37088 +                       pr_err("%pd copy err %d\n", dentry, err);
37089 +                       goto out_fput;
37090 +               }
37091 +       }
37092 +       goto out_dput; /* success */
37093 +
37094 +out_fput:
37095 +       fput(file);
37096 +       file = ERR_PTR(err);
37097 +out_dput:
37098 +       dput(path.dentry);
37099 +out:
37100 +       if (do_unlock)
37101 +               au_xino_unlock_dir(&ldir);
37102 +       return file;
37103 +}
37104 +
37105 +struct file *au_xino_file1(struct au_xino *xi)
37106 +{
37107 +       struct file *file;
37108 +       unsigned int u, nfile;
37109 +
37110 +       file = NULL;
37111 +       nfile = xi->xi_nfile;
37112 +       for (u = 0; u < nfile; u++) {
37113 +               file = xi->xi_file[u];
37114 +               if (file)
37115 +                       break;
37116 +       }
37117 +
37118 +       return file;
37119 +}
37120 +
37121 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37122 +{
37123 +       int err;
37124 +       struct file *f;
37125 +       void *p;
37126 +
37127 +       if (file)
37128 +               get_file(file);
37129 +
37130 +       err = 0;
37131 +       f = NULL;
37132 +       if (idx < xi->xi_nfile) {
37133 +               f = xi->xi_file[idx];
37134 +               if (f)
37135 +                       fput(f);
37136 +       } else {
37137 +               p = au_kzrealloc(xi->xi_file,
37138 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37139 +                                sizeof(*xi->xi_file) * (idx + 1),
37140 +                                GFP_NOFS, /*may_shrink*/0);
37141 +               if (p) {
37142 +                       MtxMustLock(&xi->xi_mtx);
37143 +                       xi->xi_file = p;
37144 +                       xi->xi_nfile = idx + 1;
37145 +               } else {
37146 +                       err = -ENOMEM;
37147 +                       if (file)
37148 +                               fput(file);
37149 +                       goto out;
37150 +               }
37151 +       }
37152 +       xi->xi_file[idx] = file;
37153 +
37154 +out:
37155 +       return err;
37156 +}
37157 +
37158 +/*
37159 + * if @xinew->xi is not set, then create new xigen file.
37160 + */
37161 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37162 +{
37163 +       struct file *file;
37164 +       int err;
37165 +
37166 +       SiMustAnyLock(sb);
37167 +
37168 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37169 +       if (IS_ERR(file)) {
37170 +               err = PTR_ERR(file);
37171 +               pr_err("%s[%d], err %d\n",
37172 +                      xinew->xi ? "xino" : "xigen",
37173 +                      xinew->idx, err);
37174 +               goto out;
37175 +       }
37176 +
37177 +       if (xinew->xi)
37178 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37179 +       else {
37180 +               BUG();
37181 +               /* todo: make xigen file an array */
37182 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37183 +       }
37184 +       fput(file);
37185 +       if (unlikely(err))
37186 +               file = ERR_PTR(err);
37187 +
37188 +out:
37189 +       return file;
37190 +}
37191 +
37192 +/* ---------------------------------------------------------------------- */
37193 +
37194 +/*
37195 + * truncate xino files
37196 + */
37197 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37198 +                           int idx, struct kstatfs *st)
37199 +{
37200 +       int err;
37201 +       blkcnt_t blocks;
37202 +       struct file *file, *new_xino;
37203 +       struct au_xi_new xinew = {
37204 +               .idx = idx
37205 +       };
37206 +
37207 +       err = 0;
37208 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37209 +       file = au_xino_file(xinew.xi, idx);
37210 +       if (!file)
37211 +               goto out;
37212 +
37213 +       xinew.base = &file->f_path;
37214 +       err = vfs_statfs(xinew.base, st);
37215 +       if (unlikely(err)) {
37216 +               AuErr1("statfs err %d, ignored\n", err);
37217 +               err = 0;
37218 +               goto out;
37219 +       }
37220 +
37221 +       blocks = file_inode(file)->i_blocks;
37222 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37223 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37224 +
37225 +       xinew.copy_src = file;
37226 +       new_xino = au_xi_new(sb, &xinew);
37227 +       if (IS_ERR(new_xino)) {
37228 +               err = PTR_ERR(new_xino);
37229 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37230 +               goto out;
37231 +       }
37232 +
37233 +       err = vfs_statfs(&new_xino->f_path, st);
37234 +       if (!err)
37235 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37236 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37237 +                       st->f_bfree, st->f_blocks);
37238 +       else {
37239 +               AuErr1("statfs err %d, ignored\n", err);
37240 +               err = 0;
37241 +       }
37242 +
37243 +out:
37244 +       return err;
37245 +}
37246 +
37247 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37248 +{
37249 +       int err, i;
37250 +       unsigned long jiffy;
37251 +       aufs_bindex_t bbot;
37252 +       struct kstatfs *st;
37253 +       struct au_branch *br;
37254 +       struct au_xino *xi;
37255 +
37256 +       err = -ENOMEM;
37257 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37258 +       if (unlikely(!st))
37259 +               goto out;
37260 +
37261 +       err = -EINVAL;
37262 +       bbot = au_sbbot(sb);
37263 +       if (unlikely(bindex < 0 || bbot < bindex))
37264 +               goto out_st;
37265 +
37266 +       err = 0;
37267 +       jiffy = jiffies;
37268 +       br = au_sbr(sb, bindex);
37269 +       xi = br->br_xino;
37270 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37271 +               err = au_xino_do_trunc(sb, bindex, i, st);
37272 +       if (!err)
37273 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37274 +
37275 +out_st:
37276 +       au_kfree_rcu(st);
37277 +out:
37278 +       return err;
37279 +}
37280 +
37281 +struct xino_do_trunc_args {
37282 +       struct super_block *sb;
37283 +       struct au_branch *br;
37284 +       int idx;
37285 +};
37286 +
37287 +static void xino_do_trunc(void *_args)
37288 +{
37289 +       struct xino_do_trunc_args *args = _args;
37290 +       struct super_block *sb;
37291 +       struct au_branch *br;
37292 +       struct inode *dir;
37293 +       int err, idx;
37294 +       aufs_bindex_t bindex;
37295 +
37296 +       err = 0;
37297 +       sb = args->sb;
37298 +       dir = d_inode(sb->s_root);
37299 +       br = args->br;
37300 +       idx = args->idx;
37301 +
37302 +       si_noflush_write_lock(sb);
37303 +       ii_read_lock_parent(dir);
37304 +       bindex = au_br_index(sb, br->br_id);
37305 +       err = au_xino_trunc(sb, bindex, idx);
37306 +       ii_read_unlock(dir);
37307 +       if (unlikely(err))
37308 +               pr_warn("err b%d, (%d)\n", bindex, err);
37309 +       atomic_dec(&br->br_xino->xi_truncating);
37310 +       au_lcnt_dec(&br->br_count);
37311 +       si_write_unlock(sb);
37312 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37313 +       au_kfree_rcu(args);
37314 +}
37315 +
37316 +/*
37317 + * returns the index in the xi_file array whose corresponding file is necessary
37318 + * to truncate, or -1 which means no need to truncate.
37319 + */
37320 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37321 +{
37322 +       int err;
37323 +       unsigned int u;
37324 +       struct kstatfs st;
37325 +       struct au_sbinfo *sbinfo;
37326 +       struct au_xino *xi;
37327 +       struct file *file;
37328 +
37329 +       /* todo: si_xino_expire and the ratio should be customizable */
37330 +       sbinfo = au_sbi(sb);
37331 +       if (time_before(jiffies,
37332 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37333 +               return -1;
37334 +
37335 +       /* truncation border */
37336 +       xi = br->br_xino;
37337 +       for (u = 0; u < xi->xi_nfile; u++) {
37338 +               file = au_xino_file(xi, u);
37339 +               if (!file)
37340 +                       continue;
37341 +
37342 +               err = vfs_statfs(&file->f_path, &st);
37343 +               if (unlikely(err)) {
37344 +                       AuErr1("statfs err %d, ignored\n", err);
37345 +                       return -1;
37346 +               }
37347 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37348 +                   >= AUFS_XINO_DEF_TRUNC)
37349 +                       return u;
37350 +       }
37351 +
37352 +       return -1;
37353 +}
37354 +
37355 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37356 +{
37357 +       int idx;
37358 +       struct xino_do_trunc_args *args;
37359 +       int wkq_err;
37360 +
37361 +       idx = xino_trunc_test(sb, br);
37362 +       if (idx < 0)
37363 +               return;
37364 +
37365 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37366 +               goto out;
37367 +
37368 +       /* lock and kfree() will be called in trunc_xino() */
37369 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37370 +       if (unlikely(!args)) {
37371 +               AuErr1("no memory\n");
37372 +               goto out;
37373 +       }
37374 +
37375 +       au_lcnt_inc(&br->br_count);
37376 +       args->sb = sb;
37377 +       args->br = br;
37378 +       args->idx = idx;
37379 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37380 +       if (!wkq_err)
37381 +               return; /* success */
37382 +
37383 +       pr_err("wkq %d\n", wkq_err);
37384 +       au_lcnt_dec(&br->br_count);
37385 +       au_kfree_rcu(args);
37386 +
37387 +out:
37388 +       atomic_dec(&br->br_xino->xi_truncating);
37389 +}
37390 +
37391 +/* ---------------------------------------------------------------------- */
37392 +
37393 +struct au_xi_calc {
37394 +       int idx;
37395 +       loff_t pos;
37396 +};
37397 +
37398 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37399 +                      struct au_xi_calc *calc)
37400 +{
37401 +       loff_t maxent;
37402 +
37403 +       maxent = au_xi_maxent(sb);
37404 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37405 +       calc->pos *= sizeof(ino_t);
37406 +}
37407 +
37408 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37409 +                               struct au_xi_calc *calc)
37410 +{
37411 +       int err;
37412 +       struct file *file;
37413 +       struct au_xino *xi = br->br_xino;
37414 +       struct au_xi_new xinew = {
37415 +               .xi = xi
37416 +       };
37417 +
37418 +       SiMustAnyLock(sb);
37419 +
37420 +       err = 0;
37421 +       if (!xi)
37422 +               goto out;
37423 +
37424 +       mutex_lock(&xi->xi_mtx);
37425 +       file = au_xino_file(xi, calc->idx);
37426 +       if (file)
37427 +               goto out_mtx;
37428 +
37429 +       file = au_xino_file(xi, /*idx*/-1);
37430 +       AuDebugOn(!file);
37431 +       xinew.idx = calc->idx;
37432 +       xinew.base = &file->f_path;
37433 +       /* xinew.copy_src = NULL; */
37434 +       file = au_xi_new(sb, &xinew);
37435 +       if (IS_ERR(file))
37436 +               err = PTR_ERR(file);
37437 +
37438 +out_mtx:
37439 +       mutex_unlock(&xi->xi_mtx);
37440 +out:
37441 +       return err;
37442 +}
37443 +
37444 +struct au_xino_do_new_async_args {
37445 +       struct super_block *sb;
37446 +       struct au_branch *br;
37447 +       struct au_xi_calc calc;
37448 +       ino_t ino;
37449 +};
37450 +
37451 +struct au_xi_writing {
37452 +       struct hlist_bl_node node;
37453 +       ino_t h_ino, ino;
37454 +};
37455 +
37456 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37457 +                           struct au_xi_calc *calc, ino_t ino);
37458 +
37459 +static void au_xino_call_do_new_async(void *args)
37460 +{
37461 +       struct au_xino_do_new_async_args *a = args;
37462 +       struct au_branch *br;
37463 +       struct super_block *sb;
37464 +       struct au_sbinfo *sbi;
37465 +       struct inode *root;
37466 +       struct file *file;
37467 +       struct au_xi_writing *del, *p;
37468 +       struct hlist_bl_head *hbl;
37469 +       struct hlist_bl_node *pos;
37470 +       int err;
37471 +
37472 +       br = a->br;
37473 +       sb = a->sb;
37474 +       sbi = au_sbi(sb);
37475 +       si_noflush_read_lock(sb);
37476 +       root = d_inode(sb->s_root);
37477 +       ii_read_lock_child(root);
37478 +       err = au_xino_do_new_async(sb, br, &a->calc);
37479 +       if (unlikely(err)) {
37480 +               AuIOErr("err %d\n", err);
37481 +               goto out;
37482 +       }
37483 +
37484 +       file = au_xino_file(br->br_xino, a->calc.idx);
37485 +       AuDebugOn(!file);
37486 +       err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37487 +       if (unlikely(err)) {
37488 +               AuIOErr("err %d\n", err);
37489 +               goto out;
37490 +       }
37491 +
37492 +       del = NULL;
37493 +       hbl = &br->br_xino->xi_writing;
37494 +       hlist_bl_lock(hbl);
37495 +       au_hbl_for_each(pos, hbl) {
37496 +               p = container_of(pos, struct au_xi_writing, node);
37497 +               if (p->ino == a->ino) {
37498 +                       del = p;
37499 +                       hlist_bl_del(&p->node);
37500 +                       break;
37501 +               }
37502 +       }
37503 +       hlist_bl_unlock(hbl);
37504 +       au_kfree_rcu(del);
37505 +
37506 +out:
37507 +       au_lcnt_dec(&br->br_count);
37508 +       ii_read_unlock(root);
37509 +       si_read_unlock(sb);
37510 +       au_nwt_done(&sbi->si_nowait);
37511 +       au_kfree_rcu(a);
37512 +}
37513 +
37514 +/*
37515 + * create a new xino file asynchronously
37516 + */
37517 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37518 +                            struct au_xi_calc *calc, ino_t ino)
37519 +{
37520 +       int err;
37521 +       struct au_xino_do_new_async_args *arg;
37522 +
37523 +       err = -ENOMEM;
37524 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37525 +       if (unlikely(!arg))
37526 +               goto out;
37527 +
37528 +       arg->sb = sb;
37529 +       arg->br = br;
37530 +       arg->calc = *calc;
37531 +       arg->ino = ino;
37532 +       au_lcnt_inc(&br->br_count);
37533 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37534 +       if (unlikely(err)) {
37535 +               pr_err("wkq %d\n", err);
37536 +               au_lcnt_dec(&br->br_count);
37537 +               au_kfree_rcu(arg);
37538 +       }
37539 +
37540 +out:
37541 +       return err;
37542 +}
37543 +
37544 +/*
37545 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37546 + * at the position of @h_ino.
37547 + */
37548 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37549 +                ino_t *ino)
37550 +{
37551 +       int err;
37552 +       ssize_t sz;
37553 +       struct au_xi_calc calc;
37554 +       struct au_sbinfo *sbinfo;
37555 +       struct file *file;
37556 +       struct au_xino *xi;
37557 +       struct hlist_bl_head *hbl;
37558 +       struct hlist_bl_node *pos;
37559 +       struct au_xi_writing *p;
37560 +
37561 +       *ino = 0;
37562 +       if (!au_opt_test(au_mntflags(sb), XINO))
37563 +               return 0; /* no xino */
37564 +
37565 +       err = 0;
37566 +       au_xi_calc(sb, h_ino, &calc);
37567 +       xi = au_sbr(sb, bindex)->br_xino;
37568 +       file = au_xino_file(xi, calc.idx);
37569 +       if (!file) {
37570 +               hbl = &xi->xi_writing;
37571 +               hlist_bl_lock(hbl);
37572 +               au_hbl_for_each(pos, hbl) {
37573 +                       p = container_of(pos, struct au_xi_writing, node);
37574 +                       if (p->h_ino == h_ino) {
37575 +                               AuDbg("hi%llu, i%llu, found\n",
37576 +                                     (u64)p->h_ino, (u64)p->ino);
37577 +                               *ino = p->ino;
37578 +                               break;
37579 +                       }
37580 +               }
37581 +               hlist_bl_unlock(hbl);
37582 +               return 0;
37583 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37584 +               return 0; /* no xino */
37585 +
37586 +       sbinfo = au_sbi(sb);
37587 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
37588 +       if (sz == sizeof(*ino))
37589 +               return 0; /* success */
37590 +
37591 +       err = sz;
37592 +       if (unlikely(sz >= 0)) {
37593 +               err = -EIO;
37594 +               AuIOErr("xino read error (%zd)\n", sz);
37595 +       }
37596 +       return err;
37597 +}
37598 +
37599 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37600 +                           struct au_xi_calc *calc, ino_t ino)
37601 +{
37602 +       ssize_t sz;
37603 +
37604 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
37605 +       if (sz == sizeof(ino))
37606 +               return 0; /* success */
37607 +
37608 +       AuIOErr("write failed (%zd)\n", sz);
37609 +       return -EIO;
37610 +}
37611 +
37612 +/*
37613 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37614 + * at the position of @h_ino.
37615 + * even if @ino is zero, it is written to the xinofile and means no entry.
37616 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37617 + * try truncating it.
37618 + */
37619 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37620 +                 ino_t ino)
37621 +{
37622 +       int err;
37623 +       unsigned int mnt_flags;
37624 +       struct au_xi_calc calc;
37625 +       struct file *file;
37626 +       struct au_branch *br;
37627 +       struct au_xino *xi;
37628 +       struct au_xi_writing *p;
37629 +
37630 +       SiMustAnyLock(sb);
37631 +
37632 +       mnt_flags = au_mntflags(sb);
37633 +       if (!au_opt_test(mnt_flags, XINO))
37634 +               return 0;
37635 +
37636 +       au_xi_calc(sb, h_ino, &calc);
37637 +       br = au_sbr(sb, bindex);
37638 +       xi = br->br_xino;
37639 +       file = au_xino_file(xi, calc.idx);
37640 +       if (!file) {
37641 +               /* store the inum pair into the list */
37642 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37643 +               p->h_ino = h_ino;
37644 +               p->ino = ino;
37645 +               au_hbl_add(&p->node, &xi->xi_writing);
37646 +
37647 +               /* create and write a new xino file asynchronously */
37648 +               err = au_xino_new_async(sb, br, &calc, ino);
37649 +               if (!err)
37650 +                       return 0; /* success */
37651 +               goto out;
37652 +       }
37653 +
37654 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
37655 +       if (!err) {
37656 +               br = au_sbr(sb, bindex);
37657 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37658 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37659 +                       xino_try_trunc(sb, br);
37660 +               return 0; /* success */
37661 +       }
37662 +
37663 +out:
37664 +       AuIOErr("write failed (%d)\n", err);
37665 +       return -EIO;
37666 +}
37667 +
37668 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37669 +                             size_t size, loff_t *pos);
37670 +
37671 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37672 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37673 +                  loff_t *pos)
37674 +{
37675 +       ssize_t err;
37676 +       mm_segment_t oldfs;
37677 +       union {
37678 +               void *k;
37679 +               char __user *u;
37680 +       } buf;
37681 +       int i;
37682 +       const int prevent_endless = 10;
37683 +
37684 +       i = 0;
37685 +       buf.k = kbuf;
37686 +       oldfs = get_fs();
37687 +       set_fs(KERNEL_DS);
37688 +       do {
37689 +               err = func(file, buf.u, size, pos);
37690 +               if (err == -EINTR
37691 +                   && !au_wkq_test()
37692 +                   && fatal_signal_pending(current)) {
37693 +                       set_fs(oldfs);
37694 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37695 +                       BUG_ON(err == -EINTR);
37696 +                       oldfs = get_fs();
37697 +                       set_fs(KERNEL_DS);
37698 +               }
37699 +       } while (i++ < prevent_endless
37700 +                && (err == -EAGAIN || err == -EINTR));
37701 +       set_fs(oldfs);
37702 +
37703 +#if 0 /* reserved for future use */
37704 +       if (err > 0)
37705 +               fsnotify_access(file->f_path.dentry);
37706 +#endif
37707 +
37708 +       return err;
37709 +}
37710 +
37711 +struct xino_fread_args {
37712 +       ssize_t *errp;
37713 +       vfs_readf_t func;
37714 +       struct file *file;
37715 +       void *buf;
37716 +       size_t size;
37717 +       loff_t *pos;
37718 +};
37719 +
37720 +static void call_xino_fread(void *args)
37721 +{
37722 +       struct xino_fread_args *a = args;
37723 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37724 +}
37725 +
37726 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37727 +                             size_t size, loff_t *pos)
37728 +{
37729 +       ssize_t err;
37730 +       int wkq_err;
37731 +       struct xino_fread_args args = {
37732 +               .errp   = &err,
37733 +               .func   = func,
37734 +               .file   = file,
37735 +               .buf    = buf,
37736 +               .size   = size,
37737 +               .pos    = pos
37738 +       };
37739 +
37740 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37741 +       if (unlikely(wkq_err))
37742 +               err = wkq_err;
37743 +
37744 +       return err;
37745 +}
37746 +
37747 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37748 +                              size_t size, loff_t *pos);
37749 +
37750 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37751 +                             size_t size, loff_t *pos)
37752 +{
37753 +       ssize_t err;
37754 +       mm_segment_t oldfs;
37755 +       union {
37756 +               void *k;
37757 +               const char __user *u;
37758 +       } buf;
37759 +       int i;
37760 +       const int prevent_endless = 10;
37761 +
37762 +       i = 0;
37763 +       buf.k = kbuf;
37764 +       oldfs = get_fs();
37765 +       set_fs(KERNEL_DS);
37766 +       do {
37767 +               err = func(file, buf.u, size, pos);
37768 +               if (err == -EINTR
37769 +                   && !au_wkq_test()
37770 +                   && fatal_signal_pending(current)) {
37771 +                       set_fs(oldfs);
37772 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37773 +                       BUG_ON(err == -EINTR);
37774 +                       oldfs = get_fs();
37775 +                       set_fs(KERNEL_DS);
37776 +               }
37777 +       } while (i++ < prevent_endless
37778 +                && (err == -EAGAIN || err == -EINTR));
37779 +       set_fs(oldfs);
37780 +
37781 +#if 0 /* reserved for future use */
37782 +       if (err > 0)
37783 +               fsnotify_modify(file->f_path.dentry);
37784 +#endif
37785 +
37786 +       return err;
37787 +}
37788 +
37789 +struct do_xino_fwrite_args {
37790 +       ssize_t *errp;
37791 +       vfs_writef_t func;
37792 +       struct file *file;
37793 +       void *buf;
37794 +       size_t size;
37795 +       loff_t *pos;
37796 +};
37797 +
37798 +static void call_do_xino_fwrite(void *args)
37799 +{
37800 +       struct do_xino_fwrite_args *a = args;
37801 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37802 +}
37803 +
37804 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37805 +                              size_t size, loff_t *pos)
37806 +{
37807 +       ssize_t err;
37808 +       int wkq_err;
37809 +       struct do_xino_fwrite_args args = {
37810 +               .errp   = &err,
37811 +               .func   = func,
37812 +               .file   = file,
37813 +               .buf    = buf,
37814 +               .size   = size,
37815 +               .pos    = pos
37816 +       };
37817 +
37818 +       /*
37819 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37820 +        * users should care about quota and real 'filesystem full.'
37821 +        */
37822 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37823 +       if (unlikely(wkq_err))
37824 +               err = wkq_err;
37825 +
37826 +       return err;
37827 +}
37828 +
37829 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37830 +                   size_t size, loff_t *pos)
37831 +{
37832 +       ssize_t err;
37833 +
37834 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37835 +               lockdep_off();
37836 +               err = do_xino_fwrite(func, file, buf, size, pos);
37837 +               lockdep_on();
37838 +       } else {
37839 +               lockdep_off();
37840 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37841 +               lockdep_on();
37842 +       }
37843 +
37844 +       return err;
37845 +}
37846 +
37847 +/* ---------------------------------------------------------------------- */
37848 +
37849 +/*
37850 + * inode number bitmap
37851 + */
37852 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37853 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37854 +{
37855 +       ino_t ino;
37856 +
37857 +       AuDebugOn(bit < 0 || page_bits <= bit);
37858 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37859 +       return ino;
37860 +}
37861 +
37862 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37863 +{
37864 +       AuDebugOn(ino < AUFS_FIRST_INO);
37865 +       ino -= AUFS_FIRST_INO;
37866 +       *pindex = ino / page_bits;
37867 +       *bit = ino % page_bits;
37868 +}
37869 +
37870 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37871 +{
37872 +       int err;
37873 +       loff_t pos;
37874 +       ssize_t sz;
37875 +       struct au_sbinfo *sbinfo;
37876 +       struct file *xib;
37877 +       unsigned long *p;
37878 +
37879 +       sbinfo = au_sbi(sb);
37880 +       MtxMustLock(&sbinfo->si_xib_mtx);
37881 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37882 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37883 +
37884 +       if (pindex == sbinfo->si_xib_last_pindex)
37885 +               return 0;
37886 +
37887 +       xib = sbinfo->si_xib;
37888 +       p = sbinfo->si_xib_buf;
37889 +       pos = sbinfo->si_xib_last_pindex;
37890 +       pos *= PAGE_SIZE;
37891 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37892 +       if (unlikely(sz != PAGE_SIZE))
37893 +               goto out;
37894 +
37895 +       pos = pindex;
37896 +       pos *= PAGE_SIZE;
37897 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37898 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37899 +       else {
37900 +               memset(p, 0, PAGE_SIZE);
37901 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37902 +       }
37903 +       if (sz == PAGE_SIZE) {
37904 +               sbinfo->si_xib_last_pindex = pindex;
37905 +               return 0; /* success */
37906 +       }
37907 +
37908 +out:
37909 +       AuIOErr1("write failed (%zd)\n", sz);
37910 +       err = sz;
37911 +       if (sz >= 0)
37912 +               err = -EIO;
37913 +       return err;
37914 +}
37915 +
37916 +static void au_xib_clear_bit(struct inode *inode)
37917 +{
37918 +       int err, bit;
37919 +       unsigned long pindex;
37920 +       struct super_block *sb;
37921 +       struct au_sbinfo *sbinfo;
37922 +
37923 +       AuDebugOn(inode->i_nlink);
37924 +
37925 +       sb = inode->i_sb;
37926 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37927 +       AuDebugOn(page_bits <= bit);
37928 +       sbinfo = au_sbi(sb);
37929 +       mutex_lock(&sbinfo->si_xib_mtx);
37930 +       err = xib_pindex(sb, pindex);
37931 +       if (!err) {
37932 +               clear_bit(bit, sbinfo->si_xib_buf);
37933 +               sbinfo->si_xib_next_bit = bit;
37934 +       }
37935 +       mutex_unlock(&sbinfo->si_xib_mtx);
37936 +}
37937 +
37938 +/* ---------------------------------------------------------------------- */
37939 +
37940 +/*
37941 + * truncate a xino bitmap file
37942 + */
37943 +
37944 +/* todo: slow */
37945 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37946 +{
37947 +       int err, bit;
37948 +       ssize_t sz;
37949 +       unsigned long pindex;
37950 +       loff_t pos, pend;
37951 +       struct au_sbinfo *sbinfo;
37952 +       vfs_readf_t func;
37953 +       ino_t *ino;
37954 +       unsigned long *p;
37955 +
37956 +       err = 0;
37957 +       sbinfo = au_sbi(sb);
37958 +       MtxMustLock(&sbinfo->si_xib_mtx);
37959 +       p = sbinfo->si_xib_buf;
37960 +       func = sbinfo->si_xread;
37961 +       pend = vfsub_f_size_read(file);
37962 +       pos = 0;
37963 +       while (pos < pend) {
37964 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37965 +               err = sz;
37966 +               if (unlikely(sz <= 0))
37967 +                       goto out;
37968 +
37969 +               err = 0;
37970 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37971 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37972 +                               continue;
37973 +
37974 +                       xib_calc_bit(*ino, &pindex, &bit);
37975 +                       AuDebugOn(page_bits <= bit);
37976 +                       err = xib_pindex(sb, pindex);
37977 +                       if (!err)
37978 +                               set_bit(bit, p);
37979 +                       else
37980 +                               goto out;
37981 +               }
37982 +       }
37983 +
37984 +out:
37985 +       return err;
37986 +}
37987 +
37988 +static int xib_restore(struct super_block *sb)
37989 +{
37990 +       int err, i;
37991 +       unsigned int nfile;
37992 +       aufs_bindex_t bindex, bbot;
37993 +       void *page;
37994 +       struct au_branch *br;
37995 +       struct au_xino *xi;
37996 +       struct file *file;
37997 +
37998 +       err = -ENOMEM;
37999 +       page = (void *)__get_free_page(GFP_NOFS);
38000 +       if (unlikely(!page))
38001 +               goto out;
38002 +
38003 +       err = 0;
38004 +       bbot = au_sbbot(sb);
38005 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38006 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38007 +                       br = au_sbr(sb, bindex);
38008 +                       xi = br->br_xino;
38009 +                       nfile = xi->xi_nfile;
38010 +                       for (i = 0; i < nfile; i++) {
38011 +                               file = au_xino_file(xi, i);
38012 +                               if (file)
38013 +                                       err = do_xib_restore(sb, file, page);
38014 +                       }
38015 +               } else
38016 +                       AuDbg("skip shared b%d\n", bindex);
38017 +       free_page((unsigned long)page);
38018 +
38019 +out:
38020 +       return err;
38021 +}
38022 +
38023 +int au_xib_trunc(struct super_block *sb)
38024 +{
38025 +       int err;
38026 +       ssize_t sz;
38027 +       loff_t pos;
38028 +       struct au_sbinfo *sbinfo;
38029 +       unsigned long *p;
38030 +       struct file *file;
38031 +
38032 +       SiMustWriteLock(sb);
38033 +
38034 +       err = 0;
38035 +       sbinfo = au_sbi(sb);
38036 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38037 +               goto out;
38038 +
38039 +       file = sbinfo->si_xib;
38040 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38041 +               goto out;
38042 +
38043 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38044 +       err = PTR_ERR(file);
38045 +       if (IS_ERR(file))
38046 +               goto out;
38047 +       fput(sbinfo->si_xib);
38048 +       sbinfo->si_xib = file;
38049 +
38050 +       p = sbinfo->si_xib_buf;
38051 +       memset(p, 0, PAGE_SIZE);
38052 +       pos = 0;
38053 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38054 +       if (unlikely(sz != PAGE_SIZE)) {
38055 +               err = sz;
38056 +               AuIOErr("err %d\n", err);
38057 +               if (sz >= 0)
38058 +                       err = -EIO;
38059 +               goto out;
38060 +       }
38061 +
38062 +       mutex_lock(&sbinfo->si_xib_mtx);
38063 +       /* mnt_want_write() is unnecessary here */
38064 +       err = xib_restore(sb);
38065 +       mutex_unlock(&sbinfo->si_xib_mtx);
38066 +
38067 +out:
38068 +       return err;
38069 +}
38070 +
38071 +/* ---------------------------------------------------------------------- */
38072 +
38073 +struct au_xino *au_xino_alloc(unsigned int nfile)
38074 +{
38075 +       struct au_xino *xi;
38076 +
38077 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38078 +       if (unlikely(!xi))
38079 +               goto out;
38080 +       xi->xi_nfile = nfile;
38081 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38082 +       if (unlikely(!xi->xi_file))
38083 +               goto out_free;
38084 +
38085 +       xi->xi_nondir.total = 8; /* initial size */
38086 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38087 +                                     GFP_NOFS);
38088 +       if (unlikely(!xi->xi_nondir.array))
38089 +               goto out_file;
38090 +
38091 +       spin_lock_init(&xi->xi_nondir.spin);
38092 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38093 +       mutex_init(&xi->xi_mtx);
38094 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38095 +       atomic_set(&xi->xi_truncating, 0);
38096 +       kref_init(&xi->xi_kref);
38097 +       goto out; /* success */
38098 +
38099 +out_file:
38100 +       au_kfree_try_rcu(xi->xi_file);
38101 +out_free:
38102 +       au_kfree_rcu(xi);
38103 +       xi = NULL;
38104 +out:
38105 +       return xi;
38106 +}
38107 +
38108 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38109 +{
38110 +       int err;
38111 +       struct au_xino *xi;
38112 +
38113 +       err = 0;
38114 +       xi = au_xino_alloc(idx + 1);
38115 +       if (unlikely(!xi)) {
38116 +               err = -ENOMEM;
38117 +               goto out;
38118 +       }
38119 +
38120 +       if (file)
38121 +               get_file(file);
38122 +       xi->xi_file[idx] = file;
38123 +       AuDebugOn(br->br_xino);
38124 +       br->br_xino = xi;
38125 +
38126 +out:
38127 +       return err;
38128 +}
38129 +
38130 +static void au_xino_release(struct kref *kref)
38131 +{
38132 +       struct au_xino *xi;
38133 +       int i;
38134 +       unsigned long ul;
38135 +       struct hlist_bl_head *hbl;
38136 +       struct hlist_bl_node *pos, *n;
38137 +       struct au_xi_writing *p;
38138 +
38139 +       xi = container_of(kref, struct au_xino, xi_kref);
38140 +       for (i = 0; i < xi->xi_nfile; i++)
38141 +               if (xi->xi_file[i])
38142 +                       fput(xi->xi_file[i]);
38143 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38144 +               AuDebugOn(xi->xi_nondir.array[i]);
38145 +       mutex_destroy(&xi->xi_mtx);
38146 +       hbl = &xi->xi_writing;
38147 +       ul = au_hbl_count(hbl);
38148 +       if (unlikely(ul)) {
38149 +               pr_warn("xi_writing %lu\n", ul);
38150 +               hlist_bl_lock(hbl);
38151 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38152 +                       hlist_bl_del(&p->node);
38153 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38154 +                       kfree(p);
38155 +               }
38156 +               hlist_bl_unlock(hbl);
38157 +       }
38158 +       au_kfree_try_rcu(xi->xi_file);
38159 +       au_kfree_try_rcu(xi->xi_nondir.array);
38160 +       au_kfree_rcu(xi);
38161 +}
38162 +
38163 +int au_xino_put(struct au_branch *br)
38164 +{
38165 +       int ret;
38166 +       struct au_xino *xi;
38167 +
38168 +       ret = 0;
38169 +       xi = br->br_xino;
38170 +       if (xi) {
38171 +               br->br_xino = NULL;
38172 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38173 +       }
38174 +
38175 +       return ret;
38176 +}
38177 +
38178 +/* ---------------------------------------------------------------------- */
38179 +
38180 +/*
38181 + * xino mount option handlers
38182 + */
38183 +
38184 +/* xino bitmap */
38185 +static void xino_clear_xib(struct super_block *sb)
38186 +{
38187 +       struct au_sbinfo *sbinfo;
38188 +
38189 +       SiMustWriteLock(sb);
38190 +
38191 +       sbinfo = au_sbi(sb);
38192 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
38193 +       if (sbinfo->si_xib)
38194 +               fput(sbinfo->si_xib);
38195 +       sbinfo->si_xib = NULL;
38196 +       if (sbinfo->si_xib_buf)
38197 +               free_page((unsigned long)sbinfo->si_xib_buf);
38198 +       sbinfo->si_xib_buf = NULL;
38199 +}
38200 +
38201 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38202 +{
38203 +       int err;
38204 +       loff_t pos;
38205 +       struct au_sbinfo *sbinfo;
38206 +       struct file *file;
38207 +       struct super_block *xi_sb;
38208 +
38209 +       SiMustWriteLock(sb);
38210 +
38211 +       sbinfo = au_sbi(sb);
38212 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38213 +       err = PTR_ERR(file);
38214 +       if (IS_ERR(file))
38215 +               goto out;
38216 +       if (sbinfo->si_xib)
38217 +               fput(sbinfo->si_xib);
38218 +       sbinfo->si_xib = file;
38219 +       sbinfo->si_xread = vfs_readf(file);
38220 +       sbinfo->si_xwrite = vfs_writef(file);
38221 +       xi_sb = file_inode(file)->i_sb;
38222 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38223 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38224 +               err = -EIO;
38225 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38226 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38227 +               goto out_unset;
38228 +       }
38229 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38230 +
38231 +       err = -ENOMEM;
38232 +       if (!sbinfo->si_xib_buf)
38233 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38234 +       if (unlikely(!sbinfo->si_xib_buf))
38235 +               goto out_unset;
38236 +
38237 +       sbinfo->si_xib_last_pindex = 0;
38238 +       sbinfo->si_xib_next_bit = 0;
38239 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38240 +               pos = 0;
38241 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38242 +                                 PAGE_SIZE, &pos);
38243 +               if (unlikely(err != PAGE_SIZE))
38244 +                       goto out_free;
38245 +       }
38246 +       err = 0;
38247 +       goto out; /* success */
38248 +
38249 +out_free:
38250 +       if (sbinfo->si_xib_buf)
38251 +               free_page((unsigned long)sbinfo->si_xib_buf);
38252 +       sbinfo->si_xib_buf = NULL;
38253 +       if (err >= 0)
38254 +               err = -EIO;
38255 +out_unset:
38256 +       fput(sbinfo->si_xib);
38257 +       sbinfo->si_xib = NULL;
38258 +out:
38259 +       AuTraceErr(err);
38260 +       return err;
38261 +}
38262 +
38263 +/* xino for each branch */
38264 +static void xino_clear_br(struct super_block *sb)
38265 +{
38266 +       aufs_bindex_t bindex, bbot;
38267 +       struct au_branch *br;
38268 +
38269 +       bbot = au_sbbot(sb);
38270 +       for (bindex = 0; bindex <= bbot; bindex++) {
38271 +               br = au_sbr(sb, bindex);
38272 +               AuDebugOn(!br);
38273 +               au_xino_put(br);
38274 +       }
38275 +}
38276 +
38277 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38278 +                                 aufs_bindex_t bshared)
38279 +{
38280 +       struct au_branch *brshared;
38281 +
38282 +       brshared = au_sbr(sb, bshared);
38283 +       AuDebugOn(!brshared->br_xino);
38284 +       AuDebugOn(!brshared->br_xino->xi_file);
38285 +       if (br->br_xino != brshared->br_xino) {
38286 +               au_xino_get(brshared);
38287 +               au_xino_put(br);
38288 +               br->br_xino = brshared->br_xino;
38289 +       }
38290 +}
38291 +
38292 +struct au_xino_do_set_br {
38293 +       vfs_writef_t writef;
38294 +       struct au_branch *br;
38295 +       ino_t h_ino;
38296 +       aufs_bindex_t bshared;
38297 +};
38298 +
38299 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38300 +                            struct au_xino_do_set_br *args)
38301 +{
38302 +       int err;
38303 +       struct au_xi_calc calc;
38304 +       struct file *file;
38305 +       struct au_branch *br;
38306 +       struct au_xi_new xinew = {
38307 +               .base = path
38308 +       };
38309 +
38310 +       br = args->br;
38311 +       xinew.xi = br->br_xino;
38312 +       au_xi_calc(sb, args->h_ino, &calc);
38313 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38314 +       if (args->bshared >= 0)
38315 +               /* shared xino */
38316 +               au_xino_set_br_shared(sb, br, args->bshared);
38317 +       else if (!xinew.xi) {
38318 +               /* new xino */
38319 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38320 +               if (unlikely(err))
38321 +                       goto out;
38322 +       }
38323 +
38324 +       /* force re-creating */
38325 +       xinew.xi = br->br_xino;
38326 +       xinew.idx = calc.idx;
38327 +       mutex_lock(&xinew.xi->xi_mtx);
38328 +       file = au_xi_new(sb, &xinew);
38329 +       mutex_unlock(&xinew.xi->xi_mtx);
38330 +       err = PTR_ERR(file);
38331 +       if (IS_ERR(file))
38332 +               goto out;
38333 +       AuDebugOn(!file);
38334 +
38335 +       err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38336 +       if (unlikely(err))
38337 +               au_xino_put(br);
38338 +
38339 +out:
38340 +       AuTraceErr(err);
38341 +       return err;
38342 +}
38343 +
38344 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38345 +{
38346 +       int err;
38347 +       aufs_bindex_t bindex, bbot;
38348 +       struct au_xino_do_set_br args;
38349 +       struct inode *inode;
38350 +
38351 +       SiMustWriteLock(sb);
38352 +
38353 +       bbot = au_sbbot(sb);
38354 +       inode = d_inode(sb->s_root);
38355 +       args.writef = au_sbi(sb)->si_xwrite;
38356 +       for (bindex = 0; bindex <= bbot; bindex++) {
38357 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38358 +               args.br = au_sbr(sb, bindex);
38359 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38360 +               err = au_xino_do_set_br(sb, path, &args);
38361 +               if (unlikely(err))
38362 +                       break;
38363 +       }
38364 +
38365 +       AuTraceErr(err);
38366 +       return err;
38367 +}
38368 +
38369 +void au_xino_clr(struct super_block *sb)
38370 +{
38371 +       struct au_sbinfo *sbinfo;
38372 +
38373 +       au_xigen_clr(sb);
38374 +       xino_clear_xib(sb);
38375 +       xino_clear_br(sb);
38376 +       dbgaufs_brs_del(sb, 0);
38377 +       sbinfo = au_sbi(sb);
38378 +       /* lvalue, do not call au_mntflags() */
38379 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38380 +}
38381 +
38382 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38383 +{
38384 +       int err, skip;
38385 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38386 +       struct qstr *dname, *cur_name;
38387 +       struct file *cur_xino;
38388 +       struct au_sbinfo *sbinfo;
38389 +       struct path *path, *cur_path;
38390 +
38391 +       SiMustWriteLock(sb);
38392 +
38393 +       err = 0;
38394 +       sbinfo = au_sbi(sb);
38395 +       path = &xiopt->file->f_path;
38396 +       dentry = path->dentry;
38397 +       parent = dget_parent(dentry);
38398 +       if (remount) {
38399 +               skip = 0;
38400 +               cur_xino = sbinfo->si_xib;
38401 +               if (cur_xino) {
38402 +                       cur_path = &cur_xino->f_path;
38403 +                       cur_dentry = cur_path->dentry;
38404 +                       cur_parent = dget_parent(cur_dentry);
38405 +                       cur_name = &cur_dentry->d_name;
38406 +                       dname = &dentry->d_name;
38407 +                       skip = (cur_parent == parent
38408 +                               && au_qstreq(dname, cur_name));
38409 +                       dput(cur_parent);
38410 +               }
38411 +               if (skip)
38412 +                       goto out;
38413 +       }
38414 +
38415 +       au_opt_set(sbinfo->si_mntflags, XINO);
38416 +       err = au_xino_set_xib(sb, path);
38417 +       /* si_x{read,write} are set */
38418 +       if (!err)
38419 +               err = au_xigen_set(sb, path);
38420 +       if (!err)
38421 +               err = au_xino_set_br(sb, path);
38422 +       if (!err) {
38423 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38424 +               goto out; /* success */
38425 +       }
38426 +
38427 +       /* reset all */
38428 +       AuIOErr("failed setting xino(%d).\n", err);
38429 +       au_xino_clr(sb);
38430 +
38431 +out:
38432 +       dput(parent);
38433 +       return err;
38434 +}
38435 +
38436 +/*
38437 + * create a xinofile at the default place/path.
38438 + */
38439 +struct file *au_xino_def(struct super_block *sb)
38440 +{
38441 +       struct file *file;
38442 +       char *page, *p;
38443 +       struct au_branch *br;
38444 +       struct super_block *h_sb;
38445 +       struct path path;
38446 +       aufs_bindex_t bbot, bindex, bwr;
38447 +
38448 +       br = NULL;
38449 +       bbot = au_sbbot(sb);
38450 +       bwr = -1;
38451 +       for (bindex = 0; bindex <= bbot; bindex++) {
38452 +               br = au_sbr(sb, bindex);
38453 +               if (au_br_writable(br->br_perm)
38454 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38455 +                       bwr = bindex;
38456 +                       break;
38457 +               }
38458 +       }
38459 +
38460 +       if (bwr >= 0) {
38461 +               file = ERR_PTR(-ENOMEM);
38462 +               page = (void *)__get_free_page(GFP_NOFS);
38463 +               if (unlikely(!page))
38464 +                       goto out;
38465 +               path.mnt = au_br_mnt(br);
38466 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38467 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38468 +               file = (void *)p;
38469 +               if (!IS_ERR(p)) {
38470 +                       strcat(p, "/" AUFS_XINO_FNAME);
38471 +                       AuDbg("%s\n", p);
38472 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38473 +               }
38474 +               free_page((unsigned long)page);
38475 +       } else {
38476 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38477 +                                     /*wbrtop*/0);
38478 +               if (IS_ERR(file))
38479 +                       goto out;
38480 +               h_sb = file->f_path.dentry->d_sb;
38481 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38482 +                       pr_err("xino doesn't support %s(%s)\n",
38483 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38484 +                       fput(file);
38485 +                       file = ERR_PTR(-EINVAL);
38486 +               }
38487 +       }
38488 +
38489 +out:
38490 +       return file;
38491 +}
38492 +
38493 +/* ---------------------------------------------------------------------- */
38494 +
38495 +/*
38496 + * initialize the xinofile for the specified branch @br
38497 + * at the place/path where @base_file indicates.
38498 + * test whether another branch is on the same filesystem or not,
38499 + * if found then share the xinofile with another branch.
38500 + */
38501 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38502 +                   struct path *base)
38503 +{
38504 +       int err;
38505 +       struct au_xino_do_set_br args = {
38506 +               .h_ino  = h_ino,
38507 +               .br     = br
38508 +       };
38509 +
38510 +       args.writef = au_sbi(sb)->si_xwrite;
38511 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38512 +                                      au_br_sb(br));
38513 +       err = au_xino_do_set_br(sb, base, &args);
38514 +       if (unlikely(err))
38515 +               au_xino_put(br);
38516 +
38517 +       return err;
38518 +}
38519 +
38520 +/* ---------------------------------------------------------------------- */
38521 +
38522 +/*
38523 + * get an unused inode number from bitmap
38524 + */
38525 +ino_t au_xino_new_ino(struct super_block *sb)
38526 +{
38527 +       ino_t ino;
38528 +       unsigned long *p, pindex, ul, pend;
38529 +       struct au_sbinfo *sbinfo;
38530 +       struct file *file;
38531 +       int free_bit, err;
38532 +
38533 +       if (!au_opt_test(au_mntflags(sb), XINO))
38534 +               return iunique(sb, AUFS_FIRST_INO);
38535 +
38536 +       sbinfo = au_sbi(sb);
38537 +       mutex_lock(&sbinfo->si_xib_mtx);
38538 +       p = sbinfo->si_xib_buf;
38539 +       free_bit = sbinfo->si_xib_next_bit;
38540 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38541 +               goto out; /* success */
38542 +       free_bit = find_first_zero_bit(p, page_bits);
38543 +       if (free_bit < page_bits)
38544 +               goto out; /* success */
38545 +
38546 +       pindex = sbinfo->si_xib_last_pindex;
38547 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38548 +               err = xib_pindex(sb, ul);
38549 +               if (unlikely(err))
38550 +                       goto out_err;
38551 +               free_bit = find_first_zero_bit(p, page_bits);
38552 +               if (free_bit < page_bits)
38553 +                       goto out; /* success */
38554 +       }
38555 +
38556 +       file = sbinfo->si_xib;
38557 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38558 +       for (ul = pindex + 1; ul <= pend; ul++) {
38559 +               err = xib_pindex(sb, ul);
38560 +               if (unlikely(err))
38561 +                       goto out_err;
38562 +               free_bit = find_first_zero_bit(p, page_bits);
38563 +               if (free_bit < page_bits)
38564 +                       goto out; /* success */
38565 +       }
38566 +       BUG();
38567 +
38568 +out:
38569 +       set_bit(free_bit, p);
38570 +       sbinfo->si_xib_next_bit = free_bit + 1;
38571 +       pindex = sbinfo->si_xib_last_pindex;
38572 +       mutex_unlock(&sbinfo->si_xib_mtx);
38573 +       ino = xib_calc_ino(pindex, free_bit);
38574 +       AuDbg("i%lu\n", (unsigned long)ino);
38575 +       return ino;
38576 +out_err:
38577 +       mutex_unlock(&sbinfo->si_xib_mtx);
38578 +       AuDbg("i0\n");
38579 +       return 0;
38580 +}
38581 +
38582 +/* for s_op->delete_inode() */
38583 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38584 +{
38585 +       int err;
38586 +       unsigned int mnt_flags;
38587 +       aufs_bindex_t bindex, bbot, bi;
38588 +       unsigned char try_trunc;
38589 +       struct au_iinfo *iinfo;
38590 +       struct super_block *sb;
38591 +       struct au_hinode *hi;
38592 +       struct inode *h_inode;
38593 +       struct au_branch *br;
38594 +       vfs_writef_t xwrite;
38595 +       struct au_xi_calc calc;
38596 +       struct file *file;
38597 +
38598 +       AuDebugOn(au_is_bad_inode(inode));
38599 +
38600 +       sb = inode->i_sb;
38601 +       mnt_flags = au_mntflags(sb);
38602 +       if (!au_opt_test(mnt_flags, XINO)
38603 +           || inode->i_ino == AUFS_ROOT_INO)
38604 +               return;
38605 +
38606 +       if (unlinked) {
38607 +               au_xigen_inc(inode);
38608 +               au_xib_clear_bit(inode);
38609 +       }
38610 +
38611 +       iinfo = au_ii(inode);
38612 +       bindex = iinfo->ii_btop;
38613 +       if (bindex < 0)
38614 +               return;
38615 +
38616 +       xwrite = au_sbi(sb)->si_xwrite;
38617 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38618 +       hi = au_hinode(iinfo, bindex);
38619 +       bbot = iinfo->ii_bbot;
38620 +       for (; bindex <= bbot; bindex++, hi++) {
38621 +               h_inode = hi->hi_inode;
38622 +               if (!h_inode
38623 +                   || (!unlinked && h_inode->i_nlink))
38624 +                       continue;
38625 +
38626 +               /* inode may not be revalidated */
38627 +               bi = au_br_index(sb, hi->hi_id);
38628 +               if (bi < 0)
38629 +                       continue;
38630 +
38631 +               br = au_sbr(sb, bi);
38632 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38633 +               file = au_xino_file(br->br_xino, calc.idx);
38634 +               if (IS_ERR_OR_NULL(file))
38635 +                       continue;
38636 +
38637 +               err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
38638 +               if (!err && try_trunc
38639 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38640 +                       xino_try_trunc(sb, br);
38641 +       }
38642 +}
38643 +
38644 +/* ---------------------------------------------------------------------- */
38645 +
38646 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38647 +{
38648 +       int found, total, i;
38649 +
38650 +       found = -1;
38651 +       total = xi->xi_nondir.total;
38652 +       for (i = 0; i < total; i++) {
38653 +               if (xi->xi_nondir.array[i] != h_ino)
38654 +                       continue;
38655 +               found = i;
38656 +               break;
38657 +       }
38658 +
38659 +       return found;
38660 +}
38661 +
38662 +static int au_xinondir_expand(struct au_xino *xi)
38663 +{
38664 +       int err, sz;
38665 +       ino_t *p;
38666 +
38667 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38668 +
38669 +       err = -ENOMEM;
38670 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38671 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38672 +               goto out;
38673 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38674 +                        /*may_shrink*/0);
38675 +       if (p) {
38676 +               xi->xi_nondir.array = p;
38677 +               xi->xi_nondir.total <<= 1;
38678 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38679 +               err = 0;
38680 +       }
38681 +
38682 +out:
38683 +       return err;
38684 +}
38685 +
38686 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38687 +                      ino_t h_ino, int idx)
38688 +{
38689 +       struct au_xino *xi;
38690 +
38691 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38692 +       xi = au_sbr(sb, bindex)->br_xino;
38693 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38694 +
38695 +       spin_lock(&xi->xi_nondir.spin);
38696 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38697 +       xi->xi_nondir.array[idx] = 0;
38698 +       spin_unlock(&xi->xi_nondir.spin);
38699 +       wake_up_all(&xi->xi_nondir.wqh);
38700 +}
38701 +
38702 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38703 +                     int *idx)
38704 +{
38705 +       int err, found, empty;
38706 +       struct au_xino *xi;
38707 +
38708 +       err = 0;
38709 +       *idx = -1;
38710 +       if (!au_opt_test(au_mntflags(sb), XINO))
38711 +               goto out; /* no xino */
38712 +
38713 +       xi = au_sbr(sb, bindex)->br_xino;
38714 +
38715 +again:
38716 +       spin_lock(&xi->xi_nondir.spin);
38717 +       found = au_xinondir_find(xi, h_ino);
38718 +       if (found == -1) {
38719 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38720 +               if (empty == -1) {
38721 +                       empty = xi->xi_nondir.total;
38722 +                       err = au_xinondir_expand(xi);
38723 +                       if (unlikely(err))
38724 +                               goto out_unlock;
38725 +               }
38726 +               xi->xi_nondir.array[empty] = h_ino;
38727 +               *idx = empty;
38728 +       } else {
38729 +               spin_unlock(&xi->xi_nondir.spin);
38730 +               wait_event(xi->xi_nondir.wqh,
38731 +                          xi->xi_nondir.array[found] != h_ino);
38732 +               goto again;
38733 +       }
38734 +
38735 +out_unlock:
38736 +       spin_unlock(&xi->xi_nondir.spin);
38737 +out:
38738 +       return err;
38739 +}
38740 +
38741 +/* ---------------------------------------------------------------------- */
38742 +
38743 +int au_xino_path(struct seq_file *seq, struct file *file)
38744 +{
38745 +       int err;
38746 +
38747 +       err = au_seq_path(seq, &file->f_path);
38748 +       if (unlikely(err))
38749 +               goto out;
38750 +
38751 +#define Deleted "\\040(deleted)"
38752 +       seq->count -= sizeof(Deleted) - 1;
38753 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38754 +                        sizeof(Deleted) - 1));
38755 +#undef Deleted
38756 +
38757 +out:
38758 +       return err;
38759 +}
38760 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38761 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38762 +++ linux/include/uapi/linux/aufs_type.h        2020-08-03 09:14:46.095748745 +0200
38763 @@ -0,0 +1,452 @@
38764 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38765 +/*
38766 + * Copyright (C) 2005-2020 Junjiro R. Okajima
38767 + *
38768 + * This program, aufs is free software; you can redistribute it and/or modify
38769 + * it under the terms of the GNU General Public License as published by
38770 + * the Free Software Foundation; either version 2 of the License, or
38771 + * (at your option) any later version.
38772 + *
38773 + * This program is distributed in the hope that it will be useful,
38774 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38775 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38776 + * GNU General Public License for more details.
38777 + *
38778 + * You should have received a copy of the GNU General Public License
38779 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38780 + */
38781 +
38782 +#ifndef __AUFS_TYPE_H__
38783 +#define __AUFS_TYPE_H__
38784 +
38785 +#define AUFS_NAME      "aufs"
38786 +
38787 +#ifdef __KERNEL__
38788 +/*
38789 + * define it before including all other headers.
38790 + * sched.h may use pr_* macros before defining "current", so define the
38791 + * no-current version first, and re-define later.
38792 + */
38793 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38794 +#include <linux/sched.h>
38795 +#undef pr_fmt
38796 +#define pr_fmt(fmt) \
38797 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38798 +               (int)sizeof(current->comm), current->comm, current->pid
38799 +#include <linux/limits.h>
38800 +#else
38801 +#include <stdint.h>
38802 +#include <sys/types.h>
38803 +#include <limits.h>
38804 +#endif /* __KERNEL__ */
38805 +
38806 +#define AUFS_VERSION   "5.x-rcN-20200622"
38807 +
38808 +/* todo? move this to linux-2.6.19/include/magic.h */
38809 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38810 +
38811 +/* ---------------------------------------------------------------------- */
38812 +
38813 +#ifdef __KERNEL__
38814 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38815 +typedef int8_t aufs_bindex_t;
38816 +#define AUFS_BRANCH_MAX 127
38817 +#else
38818 +typedef int16_t aufs_bindex_t;
38819 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38820 +#define AUFS_BRANCH_MAX 511
38821 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38822 +#define AUFS_BRANCH_MAX 1023
38823 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38824 +#define AUFS_BRANCH_MAX 32767
38825 +#endif
38826 +#endif
38827 +
38828 +#ifndef AUFS_BRANCH_MAX
38829 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38830 +#endif
38831 +#endif /* __KERNEL__ */
38832 +
38833 +/* ---------------------------------------------------------------------- */
38834 +
38835 +#define AUFS_FSTYPE            AUFS_NAME
38836 +
38837 +#define AUFS_ROOT_INO          2
38838 +#define AUFS_FIRST_INO         11
38839 +
38840 +#define AUFS_WH_PFX            ".wh."
38841 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38842 +#define AUFS_WH_TMP_LEN                4
38843 +/* a limit for rmdir/rename a dir and copyup */
38844 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38845 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38846 +                               - 1                     /* dot */\
38847 +                               - AUFS_WH_TMP_LEN)      /* hex */
38848 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38849 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38850 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38851 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38852 +#define AUFS_DIRWH_DEF         3
38853 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38854 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38855 +#define AUFS_RDBLK_DEF         512 /* bytes */
38856 +#define AUFS_RDHASH_DEF                32
38857 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38858 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38859 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38860 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38861 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38862 +
38863 +/* pseudo-link maintenace under /proc */
38864 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38865 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38866 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38867 +
38868 +/* dirren, renamed dir */
38869 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38870 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38871 +/* whiteouted doubly */
38872 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38873 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38874 +
38875 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38876 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38877 +
38878 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38879 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38880 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38881 +
38882 +/* doubly whiteouted */
38883 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38884 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38885 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38886 +
38887 +/* branch permissions and attributes */
38888 +#define AUFS_BRPERM_RW         "rw"
38889 +#define AUFS_BRPERM_RO         "ro"
38890 +#define AUFS_BRPERM_RR         "rr"
38891 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38892 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38893 +#define AUFS_BRATTR_FHSM       "fhsm"
38894 +#define AUFS_BRATTR_UNPIN      "unpin"
38895 +#define AUFS_BRATTR_ICEX       "icex"
38896 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38897 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38898 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38899 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38900 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38901 +#define AUFS_BRRATTR_WH                "wh"
38902 +#define AUFS_BRWATTR_NLWH      "nolwh"
38903 +#define AUFS_BRWATTR_MOO       "moo"
38904 +
38905 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38906 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38907 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38908 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38909 +
38910 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38911 +#define AuBrAttr_COO_ALL       (1 << 4)
38912 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38913 +
38914 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38915 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38916 +                                                  branch. meaningless since
38917 +                                                  linux-3.18-rc1 */
38918 +
38919 +/* ignore error in copying XATTR */
38920 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38921 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38922 +#define AuBrAttr_ICEX_TR       (1 << 9)
38923 +#define AuBrAttr_ICEX_USR      (1 << 10)
38924 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38925 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38926 +                                | AuBrAttr_ICEX_SYS    \
38927 +                                | AuBrAttr_ICEX_TR     \
38928 +                                | AuBrAttr_ICEX_USR    \
38929 +                                | AuBrAttr_ICEX_OTH)
38930 +
38931 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38932 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38933 +
38934 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38935 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38936 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38937 +
38938 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38939 +
38940 +/* #warning test userspace */
38941 +#ifdef __KERNEL__
38942 +#ifndef CONFIG_AUFS_FHSM
38943 +#undef AuBrAttr_FHSM
38944 +#define AuBrAttr_FHSM          0
38945 +#endif
38946 +#ifndef CONFIG_AUFS_XATTR
38947 +#undef AuBrAttr_ICEX
38948 +#define AuBrAttr_ICEX          0
38949 +#undef AuBrAttr_ICEX_SEC
38950 +#define AuBrAttr_ICEX_SEC      0
38951 +#undef AuBrAttr_ICEX_SYS
38952 +#define AuBrAttr_ICEX_SYS      0
38953 +#undef AuBrAttr_ICEX_TR
38954 +#define AuBrAttr_ICEX_TR       0
38955 +#undef AuBrAttr_ICEX_USR
38956 +#define AuBrAttr_ICEX_USR      0
38957 +#undef AuBrAttr_ICEX_OTH
38958 +#define AuBrAttr_ICEX_OTH      0
38959 +#endif
38960 +#endif
38961 +
38962 +/* the longest combination */
38963 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38964 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38965 +                              "+" AUFS_BRATTR_COO_REG          \
38966 +                              "+" AUFS_BRATTR_FHSM             \
38967 +                              "+" AUFS_BRATTR_UNPIN            \
38968 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38969 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38970 +                              "+" AUFS_BRATTR_ICEX_USR         \
38971 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38972 +                              "+" AUFS_BRWATTR_NLWH)
38973 +
38974 +typedef struct {
38975 +       char a[AuBrPermStrSz];
38976 +} au_br_perm_str_t;
38977 +
38978 +static inline int au_br_writable(int brperm)
38979 +{
38980 +       return brperm & AuBrPerm_RW;
38981 +}
38982 +
38983 +static inline int au_br_whable(int brperm)
38984 +{
38985 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38986 +}
38987 +
38988 +static inline int au_br_wh_linkable(int brperm)
38989 +{
38990 +       return !(brperm & AuBrWAttr_NoLinkWH);
38991 +}
38992 +
38993 +static inline int au_br_cmoo(int brperm)
38994 +{
38995 +       return brperm & AuBrAttr_CMOO_Mask;
38996 +}
38997 +
38998 +static inline int au_br_fhsm(int brperm)
38999 +{
39000 +       return brperm & AuBrAttr_FHSM;
39001 +}
39002 +
39003 +/* ---------------------------------------------------------------------- */
39004 +
39005 +/* ioctl */
39006 +enum {
39007 +       /* readdir in userspace */
39008 +       AuCtl_RDU,
39009 +       AuCtl_RDU_INO,
39010 +
39011 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39012 +       AuCtl_IBUSY,    /* busy inode */
39013 +       AuCtl_MVDOWN,   /* move-down */
39014 +       AuCtl_BR,       /* info about branches */
39015 +       AuCtl_FHSM_FD   /* connection for fhsm */
39016 +};
39017 +
39018 +/* borrowed from linux/include/linux/kernel.h */
39019 +#ifndef ALIGN
39020 +#ifdef _GNU_SOURCE
39021 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39022 +#else
39023 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39024 +#endif
39025 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39026 +#endif
39027 +
39028 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39029 +#ifndef __aligned
39030 +#define __aligned(x)                   __attribute__((aligned(x)))
39031 +#endif
39032 +
39033 +#ifdef __KERNEL__
39034 +#ifndef __packed
39035 +#define __packed                       __attribute__((packed))
39036 +#endif
39037 +#endif
39038 +
39039 +struct au_rdu_cookie {
39040 +       uint64_t        h_pos;
39041 +       int16_t         bindex;
39042 +       uint8_t         flags;
39043 +       uint8_t         pad;
39044 +       uint32_t        generation;
39045 +} __aligned(8);
39046 +
39047 +struct au_rdu_ent {
39048 +       uint64_t        ino;
39049 +       int16_t         bindex;
39050 +       uint8_t         type;
39051 +       uint8_t         nlen;
39052 +       uint8_t         wh;
39053 +       char            name[];
39054 +} __aligned(8);
39055 +
39056 +static inline int au_rdu_len(int nlen)
39057 +{
39058 +       /* include the terminating NULL */
39059 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39060 +                    sizeof(uint64_t));
39061 +}
39062 +
39063 +union au_rdu_ent_ul {
39064 +       struct au_rdu_ent __user        *e;
39065 +       uint64_t                        ul;
39066 +};
39067 +
39068 +enum {
39069 +       AufsCtlRduV_SZ,
39070 +       AufsCtlRduV_End
39071 +};
39072 +
39073 +struct aufs_rdu {
39074 +       /* input */
39075 +       union {
39076 +               uint64_t        sz;     /* AuCtl_RDU */
39077 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39078 +       };
39079 +       union au_rdu_ent_ul     ent;
39080 +       uint16_t                verify[AufsCtlRduV_End];
39081 +
39082 +       /* input/output */
39083 +       uint32_t                blk;
39084 +
39085 +       /* output */
39086 +       union au_rdu_ent_ul     tail;
39087 +       /* number of entries which were added in a single call */
39088 +       uint64_t                rent;
39089 +       uint8_t                 full;
39090 +       uint8_t                 shwh;
39091 +
39092 +       struct au_rdu_cookie    cookie;
39093 +} __aligned(8);
39094 +
39095 +/* ---------------------------------------------------------------------- */
39096 +
39097 +/* dirren. the branch is identified by the filename who contains this */
39098 +struct au_drinfo {
39099 +       uint64_t ino;
39100 +       union {
39101 +               uint8_t oldnamelen;
39102 +               uint64_t _padding;
39103 +       };
39104 +       uint8_t oldname[];
39105 +} __aligned(8);
39106 +
39107 +struct au_drinfo_fdata {
39108 +       uint32_t magic;
39109 +       struct au_drinfo drinfo;
39110 +} __aligned(8);
39111 +
39112 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39113 +/* future */
39114 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39115 +
39116 +/* ---------------------------------------------------------------------- */
39117 +
39118 +struct aufs_wbr_fd {
39119 +       uint32_t        oflags;
39120 +       int16_t         brid;
39121 +} __aligned(8);
39122 +
39123 +/* ---------------------------------------------------------------------- */
39124 +
39125 +struct aufs_ibusy {
39126 +       uint64_t        ino, h_ino;
39127 +       int16_t         bindex;
39128 +} __aligned(8);
39129 +
39130 +/* ---------------------------------------------------------------------- */
39131 +
39132 +/* error code for move-down */
39133 +/* the actual message strings are implemented in aufs-util.git */
39134 +enum {
39135 +       EAU_MVDOWN_OPAQUE = 1,
39136 +       EAU_MVDOWN_WHITEOUT,
39137 +       EAU_MVDOWN_UPPER,
39138 +       EAU_MVDOWN_BOTTOM,
39139 +       EAU_MVDOWN_NOUPPER,
39140 +       EAU_MVDOWN_NOLOWERBR,
39141 +       EAU_Last
39142 +};
39143 +
39144 +/* flags for move-down */
39145 +#define AUFS_MVDOWN_DMSG       1
39146 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39147 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39148 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39149 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39150 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39151 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39152 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39153 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39154 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39155 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39156 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39157 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39158 +
39159 +/* index for move-down */
39160 +enum {
39161 +       AUFS_MVDOWN_UPPER,
39162 +       AUFS_MVDOWN_LOWER,
39163 +       AUFS_MVDOWN_NARRAY
39164 +};
39165 +
39166 +/*
39167 + * additional info of move-down
39168 + * number of free blocks and inodes.
39169 + * subset of struct kstatfs, but smaller and always 64bit.
39170 + */
39171 +struct aufs_stfs {
39172 +       uint64_t        f_blocks;
39173 +       uint64_t        f_bavail;
39174 +       uint64_t        f_files;
39175 +       uint64_t        f_ffree;
39176 +};
39177 +
39178 +struct aufs_stbr {
39179 +       int16_t                 brid;   /* optional input */
39180 +       int16_t                 bindex; /* output */
39181 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39182 +} __aligned(8);
39183 +
39184 +struct aufs_mvdown {
39185 +       uint32_t                flags;                  /* input/output */
39186 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39187 +       int8_t                  au_errno;               /* output */
39188 +} __aligned(8);
39189 +
39190 +/* ---------------------------------------------------------------------- */
39191 +
39192 +union aufs_brinfo {
39193 +       /* PATH_MAX may differ between kernel-space and user-space */
39194 +       char    _spacer[4096];
39195 +       struct {
39196 +               int16_t id;
39197 +               int     perm;
39198 +               char    path[];
39199 +       };
39200 +} __aligned(8);
39201 +
39202 +/* ---------------------------------------------------------------------- */
39203 +
39204 +#define AuCtlType              'A'
39205 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39206 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39207 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39208 +                                    struct aufs_wbr_fd)
39209 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39210 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39211 +                                     struct aufs_mvdown)
39212 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39213 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39214 +
39215 +#endif /* __AUFS_TYPE_H__ */
39216 SPDX-License-Identifier: GPL-2.0
39217 aufs5.x-rcN loopback patch
39218
39219 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39220 index 0479902a0cd50..758c8ee4d4e6b 100644
39221 --- a/drivers/block/loop.c
39222 +++ b/drivers/block/loop.c
39223 @@ -638,6 +638,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39224                         lo->use_dio);
39225  }
39226  
39227 +static struct file *loop_real_file(struct file *file)
39228 +{
39229 +       struct file *f = NULL;
39230 +
39231 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39232 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39233 +       return f;
39234 +}
39235 +
39236  static void loop_reread_partitions(struct loop_device *lo,
39237                                    struct block_device *bdev)
39238  {
39239 @@ -693,6 +702,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39240                           unsigned int arg)
39241  {
39242         struct file     *file = NULL, *old_file;
39243 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39244         int             error;
39245         bool            partscan;
39246  
39247 @@ -712,12 +722,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39248         file = fget(arg);
39249         if (!file)
39250                 goto out_err;
39251 +       f = loop_real_file(file);
39252 +       if (f) {
39253 +               virt_file = file;
39254 +               file = f;
39255 +               get_file(file);
39256 +       }
39257  
39258         error = loop_validate_file(file, bdev);
39259         if (error)
39260                 goto out_err;
39261  
39262         old_file = lo->lo_backing_file;
39263 +       old_virt_file = lo->lo_backing_virt_file;
39264  
39265         error = -EINVAL;
39266  
39267 @@ -729,6 +746,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39268         blk_mq_freeze_queue(lo->lo_queue);
39269         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39270         lo->lo_backing_file = file;
39271 +       lo->lo_backing_virt_file = virt_file;
39272         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39273         mapping_set_gfp_mask(file->f_mapping,
39274                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39275 @@ -742,6 +760,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39276          * dependency.
39277          */
39278         fput(old_file);
39279 +       if (old_virt_file)
39280 +               fput(old_virt_file);
39281         if (partscan)
39282                 loop_reread_partitions(lo, bdev);
39283         return 0;
39284 @@ -750,6 +770,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39285         mutex_unlock(&loop_ctl_mutex);
39286         if (file)
39287                 fput(file);
39288 +       if (virt_file)
39289 +               fput(virt_file);
39290         return error;
39291  }
39292  
39293 @@ -973,7 +995,7 @@ static void loop_update_rotational(struct loop_device *lo)
39294  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39295                        struct block_device *bdev, unsigned int arg)
39296  {
39297 -       struct file     *file;
39298 +       struct file     *file, *f, *virt_file = NULL;
39299         struct inode    *inode;
39300         struct address_space *mapping;
39301         struct block_device *claimed_bdev = NULL;
39302 @@ -989,6 +1011,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39303         file = fget(arg);
39304         if (!file)
39305                 goto out;
39306 +       f = loop_real_file(file);
39307 +       if (f) {
39308 +               virt_file = file;
39309 +               file = f;
39310 +               get_file(file);
39311 +       }
39312  
39313         /*
39314          * If we don't hold exclusive handle for the device, upgrade to it
39315 @@ -1037,6 +1065,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39316         lo->lo_device = bdev;
39317         lo->lo_flags = lo_flags;
39318         lo->lo_backing_file = file;
39319 +       lo->lo_backing_virt_file = virt_file;
39320         lo->transfer = NULL;
39321         lo->ioctl = NULL;
39322         lo->lo_sizelimit = 0;
39323 @@ -1090,6 +1119,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39324                 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
39325  out_putf:
39326         fput(file);
39327 +       if (virt_file)
39328 +               fput(virt_file);
39329  out:
39330         /* This is safe: open() is still holding a reference. */
39331         module_put(THIS_MODULE);
39332 @@ -1136,6 +1167,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39333  static int __loop_clr_fd(struct loop_device *lo, bool release)
39334  {
39335         struct file *filp = NULL;
39336 +       struct file *virt_filp = lo->lo_backing_virt_file;
39337         gfp_t gfp = lo->old_gfp_mask;
39338         struct block_device *bdev = lo->lo_device;
39339         int err = 0;
39340 @@ -1159,6 +1191,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39341  
39342         spin_lock_irq(&lo->lo_lock);
39343         lo->lo_backing_file = NULL;
39344 +       lo->lo_backing_virt_file = NULL;
39345         spin_unlock_irq(&lo->lo_lock);
39346  
39347         loop_release_xfer(lo);
39348 @@ -1242,6 +1275,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39349          */
39350         if (filp)
39351                 fput(filp);
39352 +       if (virt_filp)
39353 +               fput(virt_filp);
39354         return err;
39355  }
39356  
39357 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39358 index af75a5ee40944..1d847cb194ff6 100644
39359 --- a/drivers/block/loop.h
39360 +++ b/drivers/block/loop.h
39361 @@ -46,7 +46,7 @@ struct loop_device {
39362         int             (*ioctl)(struct loop_device *, int cmd, 
39363                                  unsigned long arg); 
39364  
39365 -       struct file *   lo_backing_file;
39366 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39367         struct block_device *lo_device;
39368         void            *key_data; 
39369  
39370 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39371 index 6fb4a4ed8cc7f..ba9a959f2db27 100644
39372 --- a/fs/aufs/f_op.c
39373 +++ b/fs/aufs/f_op.c
39374 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39375         if (IS_ERR(h_file))
39376                 goto out;
39377  
39378 -       if (au_test_loopback_kthread()) {
39379 +       if (0 && au_test_loopback_kthread()) {
39380                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39381                 if (file->f_mapping != h_file->f_mapping) {
39382                         file->f_mapping = h_file->f_mapping;
39383 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39384 index a8b63acc62045..9d97c3af5686a 100644
39385 --- a/fs/aufs/loop.c
39386 +++ b/fs/aufs/loop.c
39387 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39388                 symbol_put(loop_backing_file);
39389         au_kfree_try_rcu(au_warn_loopback_array);
39390  }
39391 +
39392 +/* ---------------------------------------------------------------------- */
39393 +
39394 +/* support the loopback block device insude aufs */
39395 +
39396 +struct file *aufs_real_loop(struct file *file)
39397 +{
39398 +       struct file *f;
39399 +
39400 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39401 +       fi_read_lock(file);
39402 +       f = au_hf_top(file);
39403 +       fi_read_unlock(file);
39404 +       AuDebugOn(!f);
39405 +       return f;
39406 +}
39407 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39408 index 94f4f80ae33bf..ca1194354aff4 100644
39409 --- a/fs/aufs/loop.h
39410 +++ b/fs/aufs/loop.h
39411 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39412  
39413  int au_loopback_init(void);
39414  void au_loopback_fin(void);
39415 +
39416 +struct file *aufs_real_loop(struct file *file);
39417  #else
39418  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39419  
39420 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39421  
39422  AuStubInt0(au_loopback_init, void)
39423  AuStubVoid(au_loopback_fin, void)
39424 +
39425 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39426  #endif /* BLK_DEV_LOOP */
39427  
39428  #endif /* __KERNEL__ */
39429 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39430 index 589dd01220201..801e0a7faec59 100644
39431 --- a/fs/aufs/super.c
39432 +++ b/fs/aufs/super.c
39433 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39434         .statfs         = aufs_statfs,
39435         .put_super      = aufs_put_super,
39436         .sync_fs        = aufs_sync_fs,
39437 -       .remount_fs     = aufs_remount_fs
39438 +       .remount_fs     = aufs_remount_fs,
39439 +#ifdef CONFIG_AUFS_BDEV_LOOP
39440 +       .real_loop      = aufs_real_loop
39441 +#endif
39442  };
39443  
39444  /* ---------------------------------------------------------------------- */
39445 diff --git a/include/linux/fs.h b/include/linux/fs.h
39446 index 916efd7e612b3..68f536f42b2a0 100644
39447 --- a/include/linux/fs.h
39448 +++ b/include/linux/fs.h
39449 @@ -1985,6 +1985,10 @@ struct super_operations {
39450                                   struct shrink_control *);
39451         long (*free_cached_objects)(struct super_block *,
39452                                     struct shrink_control *);
39453 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39454 +       /* and aufs */
39455 +       struct file *(*real_loop)(struct file *);
39456 +#endif
39457  };
39458  
39459  /*
This page took 3.333573 seconds and 3 git commands to generate.